Skip to content

Commit 77acc6b

Browse files
SiFiveHollandakpm00
authored andcommitted
riscv: add support for kernel-mode FPU
This is motivated by the amdgpu DRM driver, which needs floating-point code to support recent hardware. That code is not performance-critical, so only provide a minimal non-preemptible implementation for now. Support is limited to riscv64 because riscv32 requires runtime (libgcc) assistance to convert between doubles and 64-bit integers. Link: https://lkml.kernel.org/r/20240329072441.591471-12-samuel.holland@sifive.com Signed-off-by: Samuel Holland <samuel.holland@sifive.com> Acked-by: Palmer Dabbelt <palmer@rivosinc.com> Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Acked-by: Christian König <christian.koenig@amd.com> Cc: Alex Deucher <alexander.deucher@amd.com> Cc: Borislav Petkov (AMD) <bp@alien8.de> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Huacai Chen <chenhuacai@kernel.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Masahiro Yamada <masahiroy@kernel.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nicolas Schier <nicolas@fjasle.eu> Cc: Russell King <linux@armlinux.org.uk> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: WANG Xuerui <git@xen0n.name> Cc: Will Deacon <will@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent b0b8a15 commit 77acc6b

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ config RISCV
2727
select ARCH_HAS_GCOV_PROFILE_ALL
2828
select ARCH_HAS_GIGANTIC_PAGE
2929
select ARCH_HAS_KCOV
30+
select ARCH_HAS_KERNEL_FPU_SUPPORT if 64BIT && FPU
3031
select ARCH_HAS_MEMBARRIER_CALLBACKS
3132
select ARCH_HAS_MEMBARRIER_SYNC_CORE
3233
select ARCH_HAS_MMIOWB

arch/riscv/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64i
8484

8585
KBUILD_AFLAGS += -march=$(riscv-march-y)
8686

87+
# For C code built with floating-point support, exclude V but keep F and D.
88+
CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/')
89+
8790
KBUILD_CFLAGS += -mno-save-restore
8891
KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)
8992

arch/riscv/include/asm/fpu.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#ifndef _ASM_RISCV_FPU_H
7+
#define _ASM_RISCV_FPU_H
8+
9+
#include <asm/switch_to.h>
10+
11+
#define kernel_fpu_available() has_fpu()
12+
13+
void kernel_fpu_begin(void);
14+
void kernel_fpu_end(void);
15+
16+
#endif /* ! _ASM_RISCV_FPU_H */

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ obj-$(CONFIG_RISCV_MISALIGNED) += unaligned_access_speed.o
6767
obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) += copy-unaligned.o
6868

6969
obj-$(CONFIG_FPU) += fpu.o
70+
obj-$(CONFIG_FPU) += kernel_mode_fpu.o
7071
obj-$(CONFIG_RISCV_ISA_V) += vector.o
7172
obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o
7273
obj-$(CONFIG_SMP) += smpboot.o

arch/riscv/kernel/kernel_mode_fpu.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#include <linux/export.h>
7+
#include <linux/preempt.h>
8+
9+
#include <asm/csr.h>
10+
#include <asm/fpu.h>
11+
#include <asm/processor.h>
12+
#include <asm/switch_to.h>
13+
14+
void kernel_fpu_begin(void)
15+
{
16+
preempt_disable();
17+
fstate_save(current, task_pt_regs(current));
18+
csr_set(CSR_SSTATUS, SR_FS);
19+
}
20+
EXPORT_SYMBOL_GPL(kernel_fpu_begin);
21+
22+
void kernel_fpu_end(void)
23+
{
24+
csr_clear(CSR_SSTATUS, SR_FS);
25+
fstate_restore(current, task_pt_regs(current));
26+
preempt_enable();
27+
}
28+
EXPORT_SYMBOL_GPL(kernel_fpu_end);

0 commit comments

Comments
 (0)