From patchwork Fri Mar 10 02:51:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 67187 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp655795wrd; Thu, 9 Mar 2023 19:07:27 -0800 (PST) X-Google-Smtp-Source: AK7set8UIytGsYh1fJeCfy0l4Hevpx2nb6LEeziP61o19eC5v6PatDdVHV4NuZS9qZTr8NstY/K+ X-Received: by 2002:a1f:9d87:0:b0:400:f66d:26b0 with SMTP id g129-20020a1f9d87000000b00400f66d26b0mr10486453vke.3.1678417647425; Thu, 09 Mar 2023 19:07:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678417647; cv=none; d=google.com; s=arc-20160816; b=OeFBQyMvr7BUq0Q5/dsFRvinweGK2KSwWKcrLxISDETH5TLXD2XUiBqYQ1B3HIfVLk 44t92IJ4ECLkBPYNUIkXG+877ABFewMO5/9CiEfJ+489gQAbuhtvdNEFJa8WyuRGVyRy KTwGtHIeYdQLta9VDzq10fINf3H8X/xV3xJuNj4m4rKw4XTYo/R05Nf1zCIHcLr/nlh9 9I+gTLtv5zirJTG/nCSJnGIkPKAjWdHX8B/tFowfG1i05I51vjHbgNmYRqrvFkrJVsV2 SUReHbpSnFGjljrTtYr78CMFmjDeDnlNJ/daIVMy+hB9wnLt5VUSnr5ULwixLFeugx9Z XLeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=0FaRYAP0P2N1MhCfx4ckXyZaxEo7evX9Ao2LEegznrE=; b=u6x+VBwjFMYCl0/hBHAWdWrF1ygMChohoQNxNfMzgKr8CCa4Gg2KesiMeFPZkqa0oo x4MON1ncdMobK6orwEC2AcSfxJoLIPgoirAnYrESDXRXIf9tDwKjrZ37zsp7kUOC3c9E PMNRFNa35vg3r8UELW8gIzg6PaTzyQuiOr0HIaRElAgTvZ/LOwxYe2oC2AFX8PKp2Sa6 NbVZ6b5hIj4Waw/feIYdWhIi5AqzvMyol7mEJ/fVNTgP/3eGIKm8ig2o7qYkuLdKULZd CPOtr4NxWwwMyxB5AReLYo3e9t4QURWBqVSF2SzqhvWuOrTjHaiQMhst7VMp56nMI8Ve 8Bag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 8-20020a1f1108000000b003aee12ca412si491936vkr.81.2023.03.09.19.07.12; Thu, 09 Mar 2023 19:07:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229827AbjCJCv4 (ORCPT + 99 others); Thu, 9 Mar 2023 21:51:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229473AbjCJCvx (ORCPT ); Thu, 9 Mar 2023 21:51:53 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D01F915545; Thu, 9 Mar 2023 18:51:51 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 607BA601D2; Fri, 10 Mar 2023 02:51:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 203F1C433D2; Fri, 10 Mar 2023 02:51:47 +0000 (UTC) From: Huacai Chen To: Arnd Bergmann , Huacai Chen Cc: loongarch@lists.linux.dev, linux-arch@vger.kernel.org, Xuefeng Li , Guo Ren , Xuerui Wang , Jiaxun Yang , linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn, Huacai Chen Subject: [PATCH V4] LoongArch: Provide kernel fpu functions Date: Fri, 10 Mar 2023 10:51:36 +0800 Message-Id: <20230310025136.964745-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759948463172617400?= X-GMAIL-MSGID: =?utf-8?q?1759948463172617400?= Provide kernel_fpu_begin()/kernel_fpu_end() to allow the kernel itself to use fpu. They can be used by some other kernel components, e.g., the AMDGPU graphic driver for DCN. Reported-by: WANG Xuerui Tested-by: WANG Xuerui Signed-off-by: Huacai Chen --- V2: Use non-GPL exports and update commit messages. V3: Add spaces for coding style. V4: WARN_ON unexpected requests and use GPL exports again. arch/loongarch/include/asm/fpu.h | 3 +++ arch/loongarch/kernel/Makefile | 2 +- arch/loongarch/kernel/kfpu.c | 43 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 arch/loongarch/kernel/kfpu.c diff --git a/arch/loongarch/include/asm/fpu.h b/arch/loongarch/include/asm/fpu.h index 358b254d9c1d..192f8e35d912 100644 --- a/arch/loongarch/include/asm/fpu.h +++ b/arch/loongarch/include/asm/fpu.h @@ -21,6 +21,9 @@ struct sigcontext; +extern void kernel_fpu_begin(void); +extern void kernel_fpu_end(void); + extern void _init_fpu(unsigned int); extern void _save_fp(struct loongarch_fpu *); extern void _restore_fp(struct loongarch_fpu *); diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile index 78d4e3384305..9a72d91cd104 100644 --- a/arch/loongarch/kernel/Makefile +++ b/arch/loongarch/kernel/Makefile @@ -13,7 +13,7 @@ obj-y += head.o cpu-probe.o cacheinfo.o env.o setup.o entry.o genex.o \ obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_EFI) += efi.o -obj-$(CONFIG_CPU_HAS_FPU) += fpu.o +obj-$(CONFIG_CPU_HAS_FPU) += fpu.o kfpu.o obj-$(CONFIG_ARCH_STRICT_ALIGN) += unaligned.o diff --git a/arch/loongarch/kernel/kfpu.c b/arch/loongarch/kernel/kfpu.c new file mode 100644 index 000000000000..aa68a1f6dec4 --- /dev/null +++ b/arch/loongarch/kernel/kfpu.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 Loongson Technology Corporation Limited + */ + +#include +#include +#include +#include + +static DEFINE_PER_CPU(bool, in_kernel_fpu); + +void kernel_fpu_begin(void) +{ + preempt_disable(); + + WARN_ON(this_cpu_read(in_kernel_fpu)); + + this_cpu_write(in_kernel_fpu, true); + + if (!is_fpu_owner()) + enable_fpu(); + else + _save_fp(¤t->thread.fpu); + + write_fcsr(LOONGARCH_FCSR0, 0); +} +EXPORT_SYMBOL_GPL(kernel_fpu_begin); + +void kernel_fpu_end(void) +{ + WARN_ON(!this_cpu_read(in_kernel_fpu)); + + if (!is_fpu_owner()) + disable_fpu(); + else + _restore_fp(¤t->thread.fpu); + + this_cpu_write(in_kernel_fpu, false); + + preempt_enable(); +} +EXPORT_SYMBOL_GPL(kernel_fpu_end);