arch: riscv: cpu: Add callback to init each core
Subsystems (2)
v5 cover arch: riscv: cpu: Add callback to init each core
Add a callback interface, harts_early_init() to perform riscv CPU hart-dependent configuration for each hart. harts_early_init() is placed after stack of harts are initialized and before main boot hart is picked up. Several conditions should be aware of or avoided are listed:
- cannot access gd
At the moment, gd hasn't initialized yet.
- all operations in harts_early_init() should only affect core itself For example, the operations for board level should be moved to mach_xxx() or board_init_xxx() functions instead.
- Common resource need protection if multicore
Since all harts might enter harts_early_init() in parallel, common resource need a mechanism to handle race condition.
- A reference implementation is added in ./arch/riscv/cpu/cpu.c The implementation is declared with '__weak' and can be overridden.
Changelogs:
v5
- rename riscv_hart_early_init to harts_early_init
- rephrase dummy for harts_early_init() to callback but keep current comments not specific for customizing CSRs only.
- Fix nit in [2/2] and add reviewed-by
v4
- Revising the comments for riscv_hart_early_init() in [1/2]
- Remove unnecessary braces and add reviewed-by in [2/2]
Green Wan (2):
arch: riscv: cpu: Add callback to init each core
board: sifive: unmatched: clear feature disable CSR
arch/riscv/cpu/cpu.c | 15 +++++++++++++++ arch/riscv/cpu/start.S | 14 ++++++++++++++ board/sifive/unmatched/spl.c | 15 +++++++++++++++ 3 files changed, 44 insertions(+)
Signature · 1 line
2.31.0
RFC PATCH v5 1/2 arch: riscv: cpu: Add callback to init each core
Add a callback harts_early_init() to ./arch/riscv/cpu/start.S to allow different riscv hart perform setup code for each hart as early as possible. Since all the harts enter the callback, they must be able to run the same setup.
--- arch/riscv/cpu/cpu.c | 15 +++++++++++++++ arch/riscv/cpu/start.S | 12 ++++++++++++ 2 files changed, 27 insertions(+)
arch/riscv/cpu/cpu.c +15 -0
@@ -140,3 +140,18 @@ int arch_early_init_r(void) {return riscv_cpu_probe (); } + +/** +* harts_early_init() - A callback function called by +* ./arch/riscv/cpu/start.S to allow to disable/enable features of each +* core. For example, turn on or off the functional block of CPU harts. +* +* In a multi-core system, this function must not access shared resources. +* +* Any access to such resources would probably be better done with +* available_harts_lock held. However, I doubt that any such access will be +* necessary. +*/ +__weak void harts_early_init (void ) +{ +}
arch/riscv/cpu/start.S +12 -0
@@ -117,6 +117,18 @@ call_board_init_f_0: mv sp, a0 #endif +#if CONFIG_IS_ENABLED(RISCV_MMODE) + /* + * Jump to harts_early_init() to perform init for each core. Not + * expect to access gd since gd is not initialized. All operations in the + * function should affect core itself only. In multi-core system, any access + * to common resource or registers outside core should be avoided or need a + * protection for multicore. + */ +call_harts_early_init: + jal harts_early_init +#endif + #ifndef CONFIG_XIP /* * Pick hart to initialize global data and run U-Boot. The other harts
Signature · 1 line
2.31.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
RFC PATCH v5 1/2 arch: riscv: cpu: Add callback to init each core
Quoted 6 lines
From: Green Wan [mailto:green.wan at sifive.com]
Sent: Tuesday, April 13, 2021 5:32 PM
Cc: Green Wan; Rick Jian-Zhi Chen(???); Paul Walmsley; Pragnesh Patel; Sean Anderson; Bin Meng; Simon Glass; Atish Patra; Leo Yu-Chi Liang(???); Brad Kim; open list
Subject: [RFC PATCH v5 1/2] arch: riscv: cpu: Add callback to init each coreAdd a callback
harts_early_init()to./arch/riscv/cpu/start.Sto
Please don't describe the file path because in the following log it can be found obviously.
or you can reword as:
Add a callback harts_early_init() to start.S
Quoted 15 lines
allow different riscv hart perform setup code for each hart as early as possible. Since all the harts enter the callback, they must be able to run the same setup.
Signed-off-by Green Wan <[email protected]>--- arch/riscv/cpu/cpu.c | 15 +++++++++++++++ arch/riscv/cpu/start.S | 12 ++++++++++++ 2 files changed, 27 insertions(+)arch/riscv/cpu/cpu.c +0
-0
arch/riscv/cpu/cpu.c int arch_early_init_r(void) · line 146
Quoted 7 lines
arch/riscv/cpu/cpu.c +4
-0@@ -140,3 +140,18 @@ int arch_early_init_r(void) {return riscv_cpu_probe (); } + +/** +* harts_early_init() - A callback function called by +* ./arch/riscv/cpu/start.S to allow to disable/enable features of each
Don't describe the called path._weak function is a common usage in U-Boot, if someone grep the U-Boot and will know how to use this callback.
allow to disable/enable features ...
it shall be reword as:
configure feature settings of each hart (Because someone maybe not just enable or disable features but need to adjust the default value or somewhat)
arch/riscv/cpu/cpu.c int arch_early_init_r(void) · line 147
Quoted 1 line
arch/riscv/cpu/cpu.c +1
-0+* core. For example, turn on or off the functional block of CPU harts.
Same meaning as disable/enable features of each hart. It is unnecessary to describe again.
arch/riscv/cpu/cpu.c int arch_early_init_r(void) · line 152
Quoted 5 lines
arch/riscv/cpu/cpu.c +5
-0+* +* In a multi-core system, this function must not access shared resources. +* +* Any access to such resources would probably be better done with +* available_harts_lock held. However, I doubt that any such access will be
The point is atomic instruction but not the available_harts_lock, it is only a variable allocated in memory.
You may describe that:
Memory access shall be careful here, it shall take care race conditions.
Quoted 11 lines
arch/riscv/cpu/cpu.c +5
-0+* necessary. +*/ +__weak void harts_early_init (void ) +{ +}arch/riscv/cpu/start.S +0
-0@@ -117,6 +117,18 @@ call_board_init_f_0: mv sp, a0
arch/riscv/cpu/start.S call_board_init_f_0: · line 127
Quoted 10 lines
arch/riscv/cpu/start.S +8
-0#endif +#if CONFIG_IS_ENABLED(RISCV_MMODE) + /* + * Jump to harts_early_init() to perform init for each core. Not + * expect to access gd since gd is not initialized. All operations in the + * function should affect core itself only. In multi-core system, any access + * to common resource or registers outside core should be avoided or need a + * protection for multicore. + */
Don't describe too much duplicate words for harts_early_init in start.S and cpu.c.
Here you just describe this callback can configure not standard or customized CSRs.
And describe race condition issue of memory access in SMP system for harts_early_init() in cpu.c
Thanks,
Rick
Quoted 9 lines
arch/riscv/cpu/start.S +4
-1+call_harts_early_init: + jal harts_early_init +#endif + #ifndef CONFIG_XIP /* * Pick hart to initialize global data and run U-Boot. The other harts --2.31.0
RFC PATCH v5 1/2 arch: riscv: cpu: Add callback to init each core
Thanks, Rick,
It's very helpful. I'll fix them.
Quoted 103 lines · Rick Chen
Quoted 7 lines
From: Green Wan [mailto:green.wan at sifive.com]
Sent: Tuesday, April 13, 2021 5:32 PM
Cc: Green Wan; Rick Jian-Zhi Chen(???); Paul Walmsley; Pragnesh Patel; Sean Anderson; Bin Meng; Simon Glass; Atish Patra; Leo Yu-Chi Liang(???); Brad Kim; open list
Subject: [RFC PATCH v5 1/2] arch: riscv: cpu: Add callback to init each coreAdd a callback
harts_early_init()to./arch/riscv/cpu/start.StoPlease don't describe the file path because in the following log it can be found obviously.
or you can reword as:
Add a callbackharts_early_init()tostart.SQuoted 15 lines
allow different riscv hart perform setup code for each hart as early as possible. Since all the harts enter the callback, they must be able to run the same setup.
Signed-off-by Green Wan <[email protected]>--- arch/riscv/cpu/cpu.c | 15 +++++++++++++++ arch/riscv/cpu/start.S | 12 ++++++++++++ 2 files changed, 27 insertions(+)arch/riscv/cpu/cpu.c +0
-0arch/riscv/cpu/cpu.c int arch_early_init_r(void) · line 146
Quoted 7 lines
arch/riscv/cpu/cpu.c +4
-0@@ -140,3 +140,18 @@ int arch_early_init_r(void) {return riscv_cpu_probe (); } + +/** +* harts_early_init() - A callback function called by +* ./arch/riscv/cpu/start.S to allow to disable/enable features of each Don't describe the called path.
_weakfunction is a common usage in U-Boot, if someone grep the U-Boot and will know how to use this callback.allow to disable/enable features ...
it shall be reword as:
configure feature settings of each hart (Because someone maybe not just enable or disable features but need to adjust the default value or somewhat)arch/riscv/cpu/cpu.c int arch_early_init_r(void) · line 147
Quoted 1 line
arch/riscv/cpu/cpu.c +1
-0+* core. For example, turn on or off the functional block of CPU harts. Same meaning as disable/enable features of each hart. It is unnecessary to describe again.
arch/riscv/cpu/cpu.c int arch_early_init_r(void) · line 152
Quoted 5 lines
arch/riscv/cpu/cpu.c +5
-0+* +* In a multi-core system, this function must not access shared resources. +* +* Any access to such resources would probably be better done with +* available_harts_lock held. However, I doubt that any such access will be The point is atomic instruction but not the
available_harts_lock, it is only a variable allocated in memory.
You may describe that:
Memory access shall be careful here, it shall take care race conditions.Quoted 11 lines
arch/riscv/cpu/cpu.c +5
-0+* necessary. +*/ +__weak void harts_early_init (void ) +{ +}arch/riscv/cpu/start.S +0
-0@@ -117,6 +117,18 @@ call_board_init_f_0: mv sp, a0arch/riscv/cpu/start.S call_board_init_f_0: · line 127
Quoted 10 lines
arch/riscv/cpu/start.S +8
-0#endif +#if CONFIG_IS_ENABLED(RISCV_MMODE) + /* + * Jump to harts_early_init() to perform init for each core. Not + * expect to access gd since gd is not initialized. All operations in the + * function should affect core itself only. In multi-core system, any access + * to common resource or registers outside core should be avoided or need a + * protection for multicore. + */Don't describe too much duplicate words for
harts_early_initinstart.Sandcpu.c.
Here you just describe this callback can configure not standard or customized CSRs.
And describe race condition issue of memory access in SMP system forharts_early_init()incpu.cThanks,
RickQuoted 9 lines
arch/riscv/cpu/start.S +4
-1+call_harts_early_init: + jal harts_early_init +#endif + #ifndef CONFIG_XIP /* * Pick hart to initialize global data and run U-Boot. The other harts --2.31.0
RFC PATCH v5 2/2 board: sifive: unmatched: clear feature disable CSR
In reply to Green WanClear feature disable CSR to turn on all features of hart. The detail is specified at section, 'SiFive Feature Disable CSR', in user manual
--- board/sifive/unmatched/spl.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
board/sifive/unmatched/spl.c +15 -0
@@ -12,6 +12,7 @@#include <log.h> #include <linux/delay.h> #include <linux/io.h> +#include <asm/csr.h> #include <asm/gpio.h> #include <asm/arch/gpio.h> #include <asm/arch/spl.h>
@@ -22,6 +23,20 @@#define MODE_SELECT_SD 0xb#define MODE_SELECT_MASK GENMASK(3, 0) +#define CSR_U74_FEATURE_DISABLE 0x7c1 + +void harts_early_init (void ) +{ +/* +* Feature Disable CSR +* +* Clear feature disable CSR to '0' to turn on all features for +* each core. This operation must be in M-mode. +*/ +if (CONFIG_IS_ENABLED (RISCV_MMODE )) +csr_write (CSR_U74_FEATURE_DISABLE ,0 ); +} +int spl_board_init_f (void ) {int ret;
Signature · 1 line
2.31.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
RFC PATCH v5 2/2 board: sifive: unmatched: clear feature disable CSR
Hi Green,
Quoted 8 lines
From: Green Wan [mailto:green.wan at sifive.com]
Sent: Tuesday, April 13, 2021 5:32 PM
Cc: Green Wan; Sean Anderson; Bin Meng; Rick Jian-Zhi Chen(???); Paul Walmsley; Pragnesh Patel; Bin Meng; Simon Glass; Atish Patra; Leo Yu-Chi Liang(???); Brad Kim; open list
Subject: [RFC PATCH v5 2/2] board: sifive: unmatched: clear feature disable CSRClear feature disable CSR to turn on all features of hart. The detail is specified at section, 'SiFive Feature Disable CSR', in user manual
Quoted 7 lines
Signed-off-by Green Wan <[email protected]>Reviewed-by Sean Anderson <[email protected]>Reviewed-by Bin Meng <[email protected]>--- board/sifive/unmatched/spl.c | 15 +++++++++++++++
Is this CSR depends on unmatched board ?
I just wonder why not add in /arch/riscv/cpu/ and create fu74, just like /arch/riscv/cpu/fu540/spl.c ?
Thanks,
Rick
Quoted 37 lines
1 file changed, 15 insertions(+)board/sifive/unmatched/spl.c +15
-1@@ -12,6 +12,7 @@#include <log.h> #include <linux/delay.h> #include <linux/io.h> +#include <asm/csr.h> #include <asm/gpio.h> #include <asm/arch/gpio.h> #include <asm/arch/spl.h> @@ -22,6 +23,20 @@#define MODE_SELECT_SD 0xb#define MODE_SELECT_MASK GENMASK(3, 0) +#define CSR_U74_FEATURE_DISABLE 0x7c1 + +void harts_early_init (void ) +{ +/* +* Feature Disable CSR +* +* Clear feature disable CSR to '0' to turn on all features for +* each core. This operation must be in M-mode. +*/ +if (CONFIG_IS_ENABLED (RISCV_MMODE )) +csr_write (CSR_U74_FEATURE_DISABLE ,0 ); +} +int spl_board_init_f (void ) {int ret; --2.31.0
RFC PATCH v5 2/2 board: sifive: unmatched: clear feature disable CSR
Quoted 19 lines · Rick Chen
Hi Green,
Quoted 8 lines
From: Green Wan [mailto:green.wan at sifive.com]
Sent: Tuesday, April 13, 2021 5:32 PM
Cc: Green Wan; Sean Anderson; Bin Meng; Rick Jian-Zhi Chen(???); Paul Walmsley; Pragnesh Patel; Bin Meng; Simon Glass; Atish Patra; Leo Yu-Chi Liang(???); Brad Kim; open list
Subject: [RFC PATCH v5 2/2] board: sifive: unmatched: clear feature disable CSRClear feature disable CSR to turn on all features of hart. The detail is specified at section, 'SiFive Feature Disable CSR', in user manual
Quoted 7 lines
Signed-off-by Green Wan <[email protected]>Reviewed-by Sean Anderson <[email protected]>Reviewed-by Bin Meng <[email protected]>--- board/sifive/unmatched/spl.c | 15 +++++++++++++++
Quoted 3 lines
Is this CSR depends on unmatched board ?
I just wonder why not add in /arch/riscv/cpu/ and create fu74, just like /arch/riscv/cpu/fu540/spl.c ?
You're right. This CSR doesn't depend on the Unmatched. Moving to arch/riscv/cpu/fu740/spl.c makes more sense. will do it.
Quoted 41 lines
Thanks,
RickQuoted 37 lines
1 file changed, 15 insertions(+)board/sifive/unmatched/spl.c +15
-1@@ -12,6 +12,7 @@#include <log.h> #include <linux/delay.h> #include <linux/io.h> +#include <asm/csr.h> #include <asm/gpio.h> #include <asm/arch/gpio.h> #include <asm/arch/spl.h> @@ -22,6 +23,20 @@#define MODE_SELECT_SD 0xb#define MODE_SELECT_MASK GENMASK(3, 0) +#define CSR_U74_FEATURE_DISABLE 0x7c1 + +void harts_early_init (void ) +{ +/* +* Feature Disable CSR +* +* Clear feature disable CSR to '0' to turn on all features for +* each core. This operation must be in M-mode. +*/ +if (CONFIG_IS_ENABLED (RISCV_MMODE )) +csr_write (CSR_U74_FEATURE_DISABLE ,0 ); +} +int spl_board_init_f (void ) {int ret; --2.31.0