Skip to contentSkip to conversation

riscv: Add support to no-FPU systems

PATCH Superseded Alan Kao 4 participants 8 messages Jun 27, 2018 – Aug 5, 2018 RISC-V

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Alan Kao

author
#0 lore raw

PATCH v2 1/1 riscv: Add support to no-FPU systems

This patch adds an option, CONFIG_FPU, to enable/disable floating procedures. Also, some style issues are fixed.

Cc Greentime Hu <[email protected]>
Cc Zong Li <[email protected]>
---
 arch/riscv/Kconfig                 |  9 ++++
 arch/riscv/Makefile                | 19 +++----
 arch/riscv/include/asm/switch_to.h |  6 +++
 arch/riscv/kernel/entry.S          |  3 +-
 arch/riscv/kernel/process.c        |  7 ++-
 arch/riscv/kernel/signal.c         | 82 +++++++++++++++++++++---------
 6 files changed, 90 insertions(+), 36 deletions(-)
arch/riscv/Kconfig +9 -0
index 6debcc4afc72..6069597ba73f 100644
@@ -232,6 +232,15 @@ config RISCV_BASE_PMU
 
 endmenu
 
+config FPU
+	bool "FPU support"
+	default y
+	help
+	  Say N here if you want to disable all floating-point related procedure
+	  in the kernel.
+
+	  If you don't know what to do here, say Y.
+
 endmenu
 
 menu "Kernel type"
arch/riscv/Makefile +8 -11
index 6d4a5f6c3f4f..ad3033739430 100644
@@ -26,7 +26,6 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
 
 	KBUILD_CFLAGS += -mabi=lp64
 	KBUILD_AFLAGS += -mabi=lp64
-	KBUILD_MARCH = rv64im
 	LDFLAGS += -melf64lriscv
 else
 	BITS := 32
@@ -34,22 +33,20 @@ else
 
 	KBUILD_CFLAGS += -mabi=ilp32
 	KBUILD_AFLAGS += -mabi=ilp32
-	KBUILD_MARCH = rv32im
 	LDFLAGS += -melf32lriscv
 endif
 
 KBUILD_CFLAGS += -Wall
 
-ifeq ($(CONFIG_RISCV_ISA_A),y)
-	KBUILD_ARCH_A = a
-endif
-ifeq ($(CONFIG_RISCV_ISA_C),y)
-	KBUILD_ARCH_C = c
-endif
-
-KBUILD_AFLAGS += -march=$(KBUILD_MARCH)$(KBUILD_ARCH_A)fd$(KBUILD_ARCH_C)
+# ISA string setting
+riscv-march-$(CONFIG_ARCH_RV32I)      := rv32im
+riscv-march-$(CONFIG_ARCH_RV64I)      := rv64im
+riscv-march-$(CONFIG_RISCV_ISA_A)     := $(riscv-march-y)a
+riscv-march-$(CONFIG_FPU)             := $(riscv-march-y)fd
+riscv-march-$(CONFIG_RISCV_ISA_C)     := $(riscv-march-y)c
+KBUILD_CFLAGS += -march=$(riscv-march-y)
+KBUILD_AFLAGS += -march=$(riscv-march-y)
 
-KBUILD_CFLAGS += -march=$(KBUILD_MARCH)$(KBUILD_ARCH_A)$(KBUILD_ARCH_C)
 KBUILD_CFLAGS += -mno-save-restore
 KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)
 
arch/riscv/include/asm/switch_to.h +6 -0
index dd6b05bff75b..de333c012227 100644
@@ -18,6 +18,7 @@
 #include <asm/ptrace.h>
 #include <asm/csr.h>
 
+#ifdef CONFIG_FPU
 extern void __fstate_save(struct task_struct *save_to);
 extern void __fstate_restore(struct task_struct *restore_from);
 
@@ -54,6 +55,11 @@ static inline void __switch_to_aux(struct task_struct *prev,
 		fstate_save(prev, regs);
 	fstate_restore(next, task_pt_regs(next));
 }
+#else
+#define fstate_save(task, regs) do { } while (0)
+#define fstate_restore(task, regs) do { } while (0)
+#define __switch_to_aux(__prev, __next)
+#endif
 
 extern struct task_struct *__switch_to(struct task_struct *,
 				       struct task_struct *);
arch/riscv/kernel/entry.S +2 -1
index 9aaf6c986771..89867c9aa4f5 100644
@@ -357,6 +357,7 @@ ENTRY(__switch_to)
 	ret
 ENDPROC(__switch_to)
 
+#ifdef CONFIG_FPU
 ENTRY(__fstate_save)
 	li  a2,  TASK_THREAD_F0
 	add a0, a0, a2
@@ -442,7 +443,7 @@ ENTRY(__fstate_restore)
 	csrc sstatus, t1
 	ret
 ENDPROC(__fstate_restore)
-
+#endif
 
 	.section ".rodata"
 	/* Exception vector table */
arch/riscv/kernel/process.c +6 -1
index cb209139ba53..99d20283bb62 100644
@@ -83,7 +83,12 @@ void show_regs(struct pt_regs *regs)
 void start_thread(struct pt_regs *regs, unsigned long pc,
 	unsigned long sp)
 {
-	regs->sstatus = SR_SPIE /* User mode, irqs on */ | SR_FS_INITIAL;
+	/* User mode, irqs on */
+#ifdef CONFIG_FPU
+	regs->sstatus = SR_SPIE | SR_FS_INITIAL;
+#else
+	regs->sstatus = SR_SPIE | SR_FS_OFF;
+#endif
 	regs->sepc = pc;
 	regs->sp = sp;
 	set_fs(USER_DS);
arch/riscv/kernel/signal.c +59 -23

139 lines not on this page · raw message

Signature · 1 line
2.18.0
#1 lore raw
Quoted 30 lines
arch/riscv/Makefile +0 -10
index 6d4a5f6c3f4f..ad3033739430 100644
@@ -26,7 +26,6 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
 
 	KBUILD_CFLAGS += -mabi=lp64
 	KBUILD_AFLAGS += -mabi=lp64
-	KBUILD_MARCH = rv64im
 	LDFLAGS += -melf64lriscv
 else
 	BITS := 32
@@ -34,22 +33,20 @@ else
 
 	KBUILD_CFLAGS += -mabi=ilp32
 	KBUILD_AFLAGS += -mabi=ilp32
-	KBUILD_MARCH = rv32im
 	LDFLAGS += -melf32lriscv
 endif
 
 KBUILD_CFLAGS += -Wall
 
-ifeq ($(CONFIG_RISCV_ISA_A),y)
-	KBUILD_ARCH_A = a
-endif
-ifeq ($(CONFIG_RISCV_ISA_C),y)
-	KBUILD_ARCH_C = c
-endif
-
-KBUILD_AFLAGS += -march=$(KBUILD_MARCH)$(KBUILD_ARCH_A)fd$(KBUILD_ARCH_C)

arch/riscv/Makefile else · line 52

Quoted 10 lines
arch/riscv/Makefile +8 -1
+# ISA string setting
+riscv-march-$(CONFIG_ARCH_RV32I)      := rv32im
+riscv-march-$(CONFIG_ARCH_RV64I)      := rv64im
+riscv-march-$(CONFIG_RISCV_ISA_A)     := $(riscv-march-y)a
+riscv-march-$(CONFIG_FPU)             := $(riscv-march-y)fd
+riscv-march-$(CONFIG_RISCV_ISA_C)     := $(riscv-march-y)c
+KBUILD_CFLAGS += -march=$(riscv-march-y)
+KBUILD_AFLAGS += -march=$(riscv-march-y)
 
-KBUILD_CFLAGS += -march=$(KBUILD_MARCH)$(KBUILD_ARCH_A)$(KBUILD_ARCH_C)

I think the cleanup part here should be split into a separate patch with a changelog for it. Just do iscv-march-y += fd for now, and then change it in the actual nofpu patch.

arch/riscv/Makefile ENTRY(__fstate_restore) · line 446

Quoted 10 lines
arch/riscv/Makefile +2 -1
+#ifdef CONFIG_FPU
 ENTRY(__fstate_save)
 	li  a2,  TASK_THREAD_F0
 	add a0, a0, a2
@@ -442,7 +443,7 @@ ENTRY(__fstate_restore)
 	csrc sstatus, t1
 	ret
 ENDPROC(__fstate_restore)
-
+#endif

I'm tempted to move the fpu save/restore routines into a new conditionally compiled fpu.S file. Palmer, what do you think?

Quoted 5 lines
arch/riscv/kernel/process.c +0 -0
index cb209139ba53..99d20283bb62 100644

arch/riscv/kernel/process.c void show_regs(struct pt_regs *regs) · line 91

Quoted 10 lines
arch/riscv/kernel/process.c +6 -1
@@ -83,7 +83,12 @@ void show_regs(struct pt_regs *regs)
 void start_thread(struct pt_regs *regs, unsigned long pc,
 	unsigned long sp)
 {
-	regs->sstatus = SR_SPIE /* User mode, irqs on */ | SR_FS_INITIAL;
+	/* User mode, irqs on */
+#ifdef CONFIG_FPU
+	regs->sstatus = SR_SPIE | SR_FS_INITIAL;
+#else
+	regs->sstatus = SR_SPIE | SR_FS_OFF;
+#endif

Just provide two different DEFAULT_SSTATUS values in switch_to.h based on the CONFIG_FPU ifdef there. And I'd be really tempted to remove the comment..

arch/riscv/kernel/process.c void show_regs(struct pt_regs *regs) · line 94

Quoted 5 lines
arch/riscv/kernel/process.c +3 -2
-static long restore_d_state(struct pt_regs *regs,
-	struct __riscv_d_ext_state __user *state)
+#ifdef CONFIG_FPU
+static inline long __restore_d_state(struct pt_regs *regs,
+				     struct __riscv_d_ext_state __user *state)

FYI, I find function delcarations much more readable if the continuing line is indented using two tabs always, especially if we have such very long paramewter declarations. But your style is also fairly common, so this is not a hard requirement.

Also the refactoring in signal.c should probably be a separate prep patch as well, so that the nofpu patch just adds the ifdef and nofpu stubs.

arch/riscv/kernel/process.c void show_regs(struct pt_regs *regs)

Quoted 4 lines
arch/riscv/kernel/process.c +3 -0
+	/* Restore the floating-point state. */
+	err = restore_d_state(regs, &sc->sc_fpregs);
+
 	return err;

This could be:

	return restore_d_state(regs, &sc->sc_fpregs);

But then again I think we could just remove restore_sigcontext and setup_sigcontext and opencode them in their only callers.

#2 lore raw

Btw, I wonder if we just just move __restore_d_state/__save_d_state to assembly. They just call assembly routines to start with, so they might be a natural fit for the new fpu.S file.

#3 lore raw
In reply to Alan Kao
Quoted 23 lines · alankao at andestech.com

This patch adds an option, CONFIG_FPU, to enable/disable floating procedures. Also, some style issues are fixed.

Cc Greentime Hu <[email protected]>
Cc Zong Li <[email protected]>
---
 arch/riscv/Kconfig                 |  9 ++++
 arch/riscv/Makefile                | 19 +++----
 arch/riscv/include/asm/switch_to.h |  6 +++
 arch/riscv/kernel/entry.S          |  3 +-
 arch/riscv/kernel/process.c        |  7 ++-
 arch/riscv/kernel/signal.c         | 82 +++++++++++++++++++++---------
 6 files changed, 90 insertions(+), 36 deletions(-)
arch/riscv/Kconfig +0 -0
index 6debcc4afc72..6069597ba73f 100644
@@ -232,6 +232,15 @@ config RISCV_BASE_PMU
 
 endmenu
 

arch/riscv/Kconfig config RISCV_BASE_PMU · line 244

Quoted 10 lines
arch/riscv/Kconfig +9 -0
+config FPU
+	bool "FPU support"
+	default y
+	help
+	  Say N here if you want to disable all floating-point related procedure
+	  in the kernel.
+
+	  If you don't know what to do here, say Y.
+
 endmenu

Sorry for letting this slide for a bit. While I'm not opposed to a solution that requires a FPU Kconfig option, it'd be a bit better if we could detect this at boot time. I think this should be possible because at one point this actually worked and we could boot the same kernel on FPU and no-FPU systems.

If that's not possible then we'll have to take something like this. There were some comments on this v2 but I don't see a v3, did I miss one?

#4 lore raw
Quoted 35 lines · Palmer Dabbelt
Quoted 34 lines · alankao at andestech.com

This patch adds an option, CONFIG_FPU, to enable/disable floating procedures. Also, some style issues are fixed.

Cc Greentime Hu <[email protected]>
Cc Zong Li <[email protected]>
---
 arch/riscv/Kconfig                 |  9 ++++
 arch/riscv/Makefile                | 19 +++----
 arch/riscv/include/asm/switch_to.h |  6 +++
 arch/riscv/kernel/entry.S          |  3 +-
 arch/riscv/kernel/process.c        |  7 ++-
 arch/riscv/kernel/signal.c         | 82 +++++++++++++++++++++---------
 6 files changed, 90 insertions(+), 36 deletions(-)
arch/riscv/Kconfig +5 -0
index 6debcc4afc72..6069597ba73f 100644
@@ -232,6 +232,15 @@ config RISCV_BASE_PMU
 
 endmenu
 
+config FPU
+       bool "FPU support"
+       default y
+       help
+         Say N here if you want to disable all floating-point related

procedure

arch/riscv/Kconfig +4 -0
+         in the kernel.
+
+         If you don't know what to do here, say Y.
+
Quoted 8 lines
Quoted 1 line

endmenu

Sorry for letting this slide for a bit. While I'm not opposed to a solution that requires a FPU Kconfig option, it'd be a bit better if we could detect this at boot time. I think this should be possible because at one point this actually worked and we could boot the same kernel on FPU and no-FPU systems.

I believe it would suffice to have start_thread set sstatus.FS to OFF
for no-FPU systems (vs. INITIAL for systems with FPU). The ISA
string in the devicetree should indicate whether F/D extensions are present.

That said, it makes sense to me to additionally provide the Kconfig option. This would elide the sstatus.SD check for no-FPU systems, shaving a couple instructions off the context-switch path. It would also enable mimicking the behavior of a no-FPU system even when the FPU is present.

Quoted 3 lines

If that's not possible then we'll have to take something like this. There were some comments on this v2 but I don't see a v3, did I miss one?

#5 lore raw
Quoted 50 lines · Andrew Waterman
Quoted 35 lines · Palmer Dabbelt
Quoted 34 lines · alankao at andestech.com

This patch adds an option, CONFIG_FPU, to enable/disable floating procedures. Also, some style issues are fixed.

Cc Greentime Hu <[email protected]>
Cc Zong Li <[email protected]>
---
 arch/riscv/Kconfig                 |  9 ++++
 arch/riscv/Makefile                | 19 +++----
 arch/riscv/include/asm/switch_to.h |  6 +++
 arch/riscv/kernel/entry.S          |  3 +-
 arch/riscv/kernel/process.c        |  7 ++-
 arch/riscv/kernel/signal.c         | 82 +++++++++++++++++++++---------
 6 files changed, 90 insertions(+), 36 deletions(-)
arch/riscv/Kconfig +5 -0
index 6debcc4afc72..6069597ba73f 100644
@@ -232,6 +232,15 @@ config RISCV_BASE_PMU
 
 endmenu
 
+config FPU
+       bool "FPU support"
+       default y
+       help
+         Say N here if you want to disable all floating-point related

procedure

arch/riscv/Kconfig +4 -0
+         in the kernel.
+
+         If you don't know what to do here, say Y.
+
Quoted 8 lines
Quoted 1 line

endmenu

Sorry for letting this slide for a bit. While I'm not opposed to a solution that requires a FPU Kconfig option, it'd be a bit better if we could detect this at boot time. I think this should be possible because at one point this actually worked and we could boot the same kernel on FPU and no-FPU systems.

I believe it would suffice to have start_thread set sstatus.FS to OFF
for no-FPU systems (vs. INITIAL for systems with FPU). The ISA
string in the devicetree should indicate whether F/D extensions are present.

Quoted 5 lines

That said, it makes sense to me to additionally provide the Kconfig option. This would elide the sstatus.SD check for no-FPU systems, shaving a couple instructions off the context-switch path. It would also enable mimicking the behavior of a no-FPU system even when the FPU is present.

That sounds like a good argument. Do you mind submitting a two-part patch set, to:

* Allow FPU kernels to detect and run correctly on non-FPU systems. You should be able to detect these very early by writing to sstatus, or later by looking at the device tree.
* Add a Kconfig option to disable the FPU entirely (which is pretty much this patch).

Thanks!

Quoted 4 lines
Quoted 4 lines

If that's not possible then we'll have to take something like this. There were some comments on this v2 but I don't see a v3, did I miss one?

author
#6 lore raw
Quoted 60 lines · Palmer Dabbelt
Quoted 50 lines · Andrew Waterman
Quoted 35 lines · Palmer Dabbelt
Quoted 34 lines · alankao at andestech.com

This patch adds an option, CONFIG_FPU, to enable/disable floating procedures. Also, some style issues are fixed.

Cc Greentime Hu <[email protected]>
Cc Zong Li <[email protected]>

---

arch/riscv/Kconfig                 |  9 ++++
arch/riscv/Makefile                | 19 +++----
arch/riscv/include/asm/switch_to.h |  6 +++
arch/riscv/kernel/entry.S          |  3 +-
arch/riscv/kernel/process.c        |  7 ++-
arch/riscv/kernel/signal.c         | 82 +++++++++++++++++++++---------
6 files changed, 90 insertions(+), 36 deletions(-)
arch/riscv/Kconfig +0 -0
index 6debcc4afc72..6069597ba73f 100644
@@ -232,6 +232,15 @@ config RISCV_BASE_PMU
 

endmenu

arch/riscv/Kconfig +5 -0
+config FPU
+       bool "FPU support"
+       default y
+       help
+         Say N here if you want to disable all floating-point related

procedure

arch/riscv/Kconfig +4 -0
+         in the kernel.
+
+         If you don't know what to do here, say Y.
+
Quoted 8 lines
Quoted 1 line

endmenu

Sorry for letting this slide for a bit. While I'm not opposed to a solution that requires a FPU Kconfig option, it'd be a bit better if we could detect this at boot time. I think this should be possible because at one point this actually worked and we could boot the same kernel on FPU and no-FPU systems.

I believe it would suffice to have start_thread set sstatus.FS to OFF
for no-FPU systems (vs. INITIAL for systems with FPU). The ISA
string in the devicetree should indicate whether F/D extensions are present.

Quoted 5 lines

That said, it makes sense to me to additionally provide the Kconfig option. This would elide the sstatus.SD check for no-FPU systems, shaving a couple instructions off the context-switch path. It would also enable mimicking the behavior of a no-FPU system even when the FPU is present.

That sounds like a good argument. Do you mind submitting a two-part patch set, to:

Quoted 8 lines

* Allow FPU kernels to detect and run correctly on non-FPU systems. You should be able to detect these very early by writing to sstatus, or later by looking at the device tree.
* Add a Kconfig option to disable the FPU entirely (which is pretty much this patch).

Thanks!

Thanks for the feedback from all of you.

We will fix v3 according to Christph's suggestions, and append a new patch of the auto-detecting feature.

author
#7 lore raw
In reply to Palmer Dabbelt
Quoted 41 lines · Palmer Dabbelt
Quoted 32 lines · alankao at andestech.com

This patch adds an option, CONFIG_FPU, to enable/disable floating procedures. Also, some style issues are fixed.

Cc Greentime Hu <[email protected]>
Cc Zong Li <[email protected]>

---

arch/riscv/Kconfig                 |  9 ++++
arch/riscv/Makefile                | 19 +++----
arch/riscv/include/asm/switch_to.h |  6 +++
arch/riscv/kernel/entry.S          |  3 +-
arch/riscv/kernel/process.c        |  7 ++-
arch/riscv/kernel/signal.c         | 82 +++++++++++++++++++++---------
6 files changed, 90 insertions(+), 36 deletions(-)
arch/riscv/Kconfig +0 -0
index 6debcc4afc72..6069597ba73f 100644
@@ -232,6 +232,15 @@ config RISCV_BASE_PMU
 

endmenu

arch/riscv/Kconfig +9 -0
+config FPU
+	bool "FPU support"
+	default y
+	help
+	  Say N here if you want to disable all floating-point related procedure
+	  in the kernel.
+
+	  If you don't know what to do here, say Y.
+
Quoted 1 line

endmenu

Sorry for letting this slide for a bit. While I'm not opposed to a solution that requires a FPU Kconfig option, it'd be a bit better if we could detect this at boot time. I think this should be possible because at one point this actually worked and we could boot the same kernel on FPU and no-FPU systems.

Quoted 2 lines

If that's not possible then we'll have to take something like this. There were some comments on this v2 but I don't see a v3, did I miss one?

I have been refatoring this into a patchset containing logically indenpendent patches. It will be sent soon after some sanity checks.