[V4] LoongArch: Provide kernel fpu functions
Commit Message
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 <kernel@xen0n.name>
Tested-by: WANG Xuerui <kernel@xen0n.name>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
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
Comments
On 2023/3/10 10:51, Huacai Chen wrote:
> 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.
Actually, as Christoph suggested earlier, you should probably send this
together with the amdgpu modifications so that "actual usage" of the
interface is established.
>
> Reported-by: WANG Xuerui <kernel@xen0n.name>
> Tested-by: WANG Xuerui <kernel@xen0n.name>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> 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.
We should probably provide more archaeology or legal evidence as to why
the in-kernel FPU ops must be GPL-only. After all the status quo is that
among all arches that provide such ops, only the x86 ones are exported
GPL, so if loongarch should go GPL for this interface then probably
everyone else should too. It's not only affecting loongarch IMO.
>
> 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
>
On Fri, 2023-03-10 at 14:33 +0800, WANG Xuerui wrote:
> We should probably provide more archaeology or legal evidence as to why
> the in-kernel FPU ops must be GPL-only. After all the status quo is that
> among all arches that provide such ops, only the x86 ones are exported
> GPL, so if loongarch should go GPL for this interface then probably
> everyone else should too. It's not only affecting loongarch IMO.
If it's forced by GPL we should change the other arch's similar function
to EXPORT_GPL immediately.
@@ -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 *);
@@ -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
new file mode 100644
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 Loongson Technology Corporation Limited
+ */
+
+#include <linux/cpu.h>
+#include <linux/init.h>
+#include <asm/fpu.h>
+#include <asm/smp.h>
+
+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);