Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 9411ec60 authored by Alan Kao's avatar Alan Kao Committed by Palmer Dabbelt
Browse files

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: default avatarAlan 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: default avatarPalmer Dabbelt <palmer@sifive.com>
parent 9671f706
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -56,13 +56,12 @@ static inline void __switch_to_aux(struct task_struct *prev,
	fstate_restore(next, task_pt_regs(next));
}

#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_INITIAL)

extern bool has_fpu;
#else
#define has_fpu false
#define fstate_save(task, regs) do { } while (0)
#define fstate_restore(task, regs) do { } while (0)
#define __switch_to_aux(__prev, __next) do { } while (0)
#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_OFF)
#endif

extern struct task_struct *__switch_to(struct task_struct *,
@@ -72,6 +71,7 @@ extern struct task_struct *__switch_to(struct task_struct *,
do {							\
	struct task_struct *__prev = (prev);		\
	struct task_struct *__next = (next);		\
	if (has_fpu)					\
		__switch_to_aux(__prev, __next);	\
	((last) = __switch_to(__prev, __next));		\
} while (0)
+8 −0
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@
#include <asm/hwcap.h>

unsigned long elf_hwcap __read_mostly;
#ifdef CONFIG_FPU
bool has_fpu __read_mostly;
#endif

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

	pr_info("elf_hwcap is 0x%lx", elf_hwcap);

#ifdef CONFIG_FPU
	if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
		has_fpu = true;
#endif
}
+3 −1
Original line number Diff line number Diff line
@@ -76,7 +76,9 @@ void show_regs(struct pt_regs *regs)
void start_thread(struct pt_regs *regs, unsigned long pc,
	unsigned long sp)
{
	regs->sstatus = DEFAULT_SSTATUS;
	regs->sstatus = SR_SPIE;
	if (has_fpu)
		regs->sstatus |= SR_FS_INITIAL;
	regs->sepc = pc;
	regs->sp = sp;
	set_fs(USER_DS);
+4 −2
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ static long restore_sigcontext(struct pt_regs *regs,
	/* sc_regs is structured the same as the start of pt_regs */
	err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
	/* Restore the floating-point state. */
	if (has_fpu)
		err |= restore_fp_state(regs, &sc->sc_fpregs);
	return err;
}
@@ -150,6 +151,7 @@ static long setup_sigcontext(struct rt_sigframe __user *frame,
	/* sc_regs is structured the same as the start of pt_regs */
	err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
	/* Save the floating-point state. */
	if (has_fpu)
		err |= save_fp_state(regs, &sc->sc_fpregs);
	return err;
}