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/[email protected] Signed-off-by: Samuel Holland <[email protected]> Acked-by: Palmer Dabbelt <[email protected]> Reviewed-by: Palmer Dabbelt <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Acked-by: Christian König <[email protected]> Cc: Alex Deucher <[email protected]> Cc: Borislav Petkov (AMD) <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Huacai Chen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Masahiro Yamada <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nicolas Schier <[email protected]> Cc: Russell King <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: WANG Xuerui <[email protected]> Cc: Will Deacon <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
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)