From patchwork Mon Mar 6 09:59:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 64570 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1760862wrd; Mon, 6 Mar 2023 02:25:59 -0800 (PST) X-Google-Smtp-Source: AK7set/+gKLyZQqq1bf9HoXDl+R7KdcYsIOa95VN8Bpirs947UQrB4vw6LsBTMF5wby6yXQ9SIMu X-Received: by 2002:a17:907:608b:b0:873:1b57:b27f with SMTP id ht11-20020a170907608b00b008731b57b27fmr13407696ejc.61.1678098358950; Mon, 06 Mar 2023 02:25:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678098358; cv=none; d=google.com; s=arc-20160816; b=KNCsuEJmtarengX4uDez2qdfnkzj5OSDKEl1Au++sQq2MRdos/sobpgQz1PXHxZW+x B6zQ2n0IoSo2bfVwdB/kOAzK/avYbPqxHgZT9YvrMTxWQ+ZvjDGDxGsWuZiSFk5qeglu IXceFGvCwncM+rfrV53B5hTQsmyaobz6vWvIhNkZ50LmptZSXGo6sx5d8xAndKcJaR3W o2eZXySz/tb625D7yYPMiwHpsldFCM1VyIpTAGFMgghz9AEUw33gEN7Am9iYDs94MB5+ 6eMgRn2Z7R+6PHmhEcmxbovhsOig3X6RekaHHYJThc5Oq0n4jSEZSRDGYD8tHa1r8Lwp 88JQ== 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=ndx6WHcRcM6Ev2nhqCa6QNHetPBUu97wGS/I6gAOc3c=; b=RmTIJYJkqh6yYNFoQgDDegzbaP4KALBgUJ1wNlxW1g0YXPQWglLTimmmuJhAQeNj7M AUbhwH8qexNj+JGhCEQYGNencYhuxso5MUKSFD+uO4eAHhfpbwjMtrAgIvLeoudgkOKv TIzRz1vLBeXw7kajZR5b3jvqyzD5kUjxISwYo1d4dZTQVxEp3glsMjptWY8DwSGpITcK Q46V1KoDd4qsMvoosx2zg2ZyEtLU4iTKMy//FcX/fdtWmhn9aIZg4WlGWOhGP6rpJL5a kxIHYPAjSj+kMi0tvOLT1atM/C9tt6/HI8ODHsRt7uIDbeOv9jg+dw99nGeMH0whhUug reeg== 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 f24-20020a056402069800b004acd2bb0015si3101774edy.346.2023.03.06.02.25.19; Mon, 06 Mar 2023 02:25:58 -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 S230173AbjCFKAI (ORCPT + 99 others); Mon, 6 Mar 2023 05:00:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230206AbjCFJ7y (ORCPT ); Mon, 6 Mar 2023 04:59:54 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 224441C593; Mon, 6 Mar 2023 01:59:46 -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 ams.source.kernel.org (Postfix) with ESMTPS id 84900B80D76; Mon, 6 Mar 2023 09:59:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91C72C433D2; Mon, 6 Mar 2023 09:59:41 +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 V3] LoongArch: Provide kernel fpu functions Date: Mon, 6 Mar 2023 17:59:34 +0800 Message-Id: <20230306095934.609589-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,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?1759613664674442509?= X-GMAIL-MSGID: =?utf-8?q?1759613664674442509?= 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. arch/loongarch/include/asm/fpu.h | 3 +++ arch/loongarch/kernel/Makefile | 2 +- arch/loongarch/kernel/kfpu.c | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 45 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..cd2a18fecdcc --- /dev/null +++ b/arch/loongarch/kernel/kfpu.c @@ -0,0 +1,41 @@ +// 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) +{ + if (this_cpu_read(in_kernel_fpu)) + return; + + preempt_disable(); + this_cpu_write(in_kernel_fpu, true); + + if (!is_fpu_owner()) + enable_fpu(); + else + _save_fp(¤t->thread.fpu); +} +EXPORT_SYMBOL(kernel_fpu_begin); + +void kernel_fpu_end(void) +{ + if (!this_cpu_read(in_kernel_fpu)) + return; + + if (!is_fpu_owner()) + disable_fpu(); + else + _restore_fp(¤t->thread.fpu); + + this_cpu_write(in_kernel_fpu, false); + preempt_enable(); +} +EXPORT_SYMBOL(kernel_fpu_end);