Skip to content

Commit 9411ec6

Browse files
NonerKaopalmer-dabbelt
authored andcommitted
Auto-detect whether a FPU exists
We expect that a kernel with CONFIG_FPU=y can still support no-FPU machines. To do so, the kernel should first examine the existence of a FPU, then do nothing if a FPU does exist; otherwise, it should disable/bypass all FPU-related functions. In this patch, a new global variable, has_fpu, is created and determined when parsing the hardware capability from device tree during booting. This variable is used in those FPU-related functions. Signed-off-by: Alan Kao <alankao@andestech.com> Cc: Greentime Hu <greentime@andestech.com> Cc: Vincent Chen <vincentc@andestech.com> Cc: Zong Li <zong@andestech.com> Cc: Nick Hu <nickhu@andestech.com> Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
1 parent 9671f70 commit 9411ec6

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

arch/riscv/include/asm/switch_to.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ static inline void __switch_to_aux(struct task_struct *prev,
5656
fstate_restore(next, task_pt_regs(next));
5757
}
5858

59-
#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_INITIAL)
60-
59+
extern bool has_fpu;
6160
#else
61+
#define has_fpu false
6262
#define fstate_save(task, regs) do { } while (0)
6363
#define fstate_restore(task, regs) do { } while (0)
6464
#define __switch_to_aux(__prev, __next) do { } while (0)
65-
#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_OFF)
6665
#endif
6766

6867
extern struct task_struct *__switch_to(struct task_struct *,
@@ -72,7 +71,8 @@ extern struct task_struct *__switch_to(struct task_struct *,
7271
do { \
7372
struct task_struct *__prev = (prev); \
7473
struct task_struct *__next = (next); \
75-
__switch_to_aux(__prev, __next); \
74+
if (has_fpu) \
75+
__switch_to_aux(__prev, __next); \
7676
((last) = __switch_to(__prev, __next)); \
7777
} while (0)
7878

arch/riscv/kernel/cpufeature.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <asm/hwcap.h>
2323

2424
unsigned long elf_hwcap __read_mostly;
25+
#ifdef CONFIG_FPU
26+
bool has_fpu __read_mostly;
27+
#endif
2528

2629
void riscv_fill_hwcap(void)
2730
{
@@ -58,4 +61,9 @@ void riscv_fill_hwcap(void)
5861
elf_hwcap |= isa2hwcap[(unsigned char)(isa[i])];
5962

6063
pr_info("elf_hwcap is 0x%lx", elf_hwcap);
64+
65+
#ifdef CONFIG_FPU
66+
if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
67+
has_fpu = true;
68+
#endif
6169
}

arch/riscv/kernel/process.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ void show_regs(struct pt_regs *regs)
7676
void start_thread(struct pt_regs *regs, unsigned long pc,
7777
unsigned long sp)
7878
{
79-
regs->sstatus = DEFAULT_SSTATUS;
79+
regs->sstatus = SR_SPIE;
80+
if (has_fpu)
81+
regs->sstatus |= SR_FS_INITIAL;
8082
regs->sepc = pc;
8183
regs->sp = sp;
8284
set_fs(USER_DS);

arch/riscv/kernel/signal.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ static long restore_sigcontext(struct pt_regs *regs,
9898
/* sc_regs is structured the same as the start of pt_regs */
9999
err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
100100
/* Restore the floating-point state. */
101-
err |= restore_fp_state(regs, &sc->sc_fpregs);
101+
if (has_fpu)
102+
err |= restore_fp_state(regs, &sc->sc_fpregs);
102103
return err;
103104
}
104105

@@ -150,7 +151,8 @@ static long setup_sigcontext(struct rt_sigframe __user *frame,
150151
/* sc_regs is structured the same as the start of pt_regs */
151152
err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
152153
/* Save the floating-point state. */
153-
err |= save_fp_state(regs, &sc->sc_fpregs);
154+
if (has_fpu)
155+
err |= save_fp_state(regs, &sc->sc_fpregs);
154156
return err;
155157
}
156158

0 commit comments

Comments
 (0)