Skip to contentSkip to conversation

arch: riscv: cpu: Add callback to init each core

RFC PATCH New Green Wan 2 participants 5 messages Mar 22, 2021 – Mar 23, 2021

Subsystems (2)
lore
Share & tools
lore

Green Wan

author
#0 lore raw

RFC PATCH 1/1 arch: riscv: cpu: Add callback to init each core

I am looking for a proper place to add some M-mode code in SPL to configure features of each core as early as possible. Try to start the discussion by sending this RFC.

I tried mach_cpu_init() as the start. And not sure whether I used it correctly, it seems executed only by the core wins lottery in ./arch/riscv/cpu/start.S.

Is there a proper place to add some inits for each core like CSR in section 7.6 in document of U74-MC Core Complex Manual[1].

There are some alternatives to do it such as

a) sending IPI to each cores
Have to wait for IPI init for each core. And the boot core have to perform the init by itself and send IPIs to other cores. It might not be straightforward.
b) hook callback, (like this patch)
c) hard code in ./arch/riscv/cpu/start.c

[1] https://sifive.cdn.prismic.io/sifive/aee0dd4c-d156-496e-a6c4-db0cf54bbe68_sifive_U74MC_rtl_full_20G1.03.00_manual.pdf

---
 arch/riscv/cpu/start.S       | 5 +++++
 arch/riscv/lib/spl.c         | 4 ++++
 board/sifive/unmatched/spl.c | 7 +++++++
 3 files changed, 16 insertions(+)
arch/riscv/cpu/start.S +5 -0
index 8589509e01..2629f7e4cc 100644
@@ -102,6 +102,11 @@ call_board_init_f_0:
 	mv	a0, sp
 	jal	board_init_f_alloc_reserve
 
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+call_each_cpu_core:
+	jal	mach_cpu_core_init
+#endif
+
 	/*
 	 * Save global data pointer for later. We don't set it here because it
 	 * is not initialized yet.
arch/riscv/lib/spl.c +4 -0
index 8baee07bea..e8261d9c19 100644
@@ -14,6 +14,10 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+__weak void mach_cpu_core_init(void)
+{
+}
+
 __weak int spl_board_init_f(void)
 {
 	return 0;
board/sifive/unmatched/spl.c +7 -0
index 5e1333b09a..cc466f703a 100644
@@ -22,6 +22,13 @@
 #define MODE_SELECT_SD		0xb
 #define MODE_SELECT_MASK	GENMASK(3, 0)
 
+void mach_cpu_core_init(void)
+{
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+	asm volatile ("csrwi 0x7c1, 0");
+#endif
+}
+
 int spl_board_init_f(void)
 {
 	int ret;
Signature · 1 line
2.31.0
#1 lore raw

RFC PATCH 1/1 arch: riscv: cpu: Add callback to init each core

Quoted 35 lines · Green Wan

I am looking for a proper place to add some M-mode code in SPL to configure features of each core as early as possible. Try to start the discussion by sending this RFC.

I tried mach_cpu_init() as the start. And not sure whether I used it correctly, it seems executed only by the core wins lottery in ./arch/riscv/cpu/start.S.

Is there a proper place to add some inits for each core like CSR in section 7.6 in document of U74-MC Core Complex Manual[1].

There are some alternatives to do it such as

a) sending IPI to each cores
Have to wait for IPI init for each core. And the boot core have to perform the init by itself and send IPIs to other cores. It might not be straightforward.
b) hook callback, (like this patch)
c) hard code in ./arch/riscv/cpu/start.c

[1] https://sifive.cdn.prismic.io/sifive/aee0dd4c-d156-496e-a6c4-db0cf54bbe68_sifive_U74MC_rtl_full_20G1.03.00_manual.pdf

---
 arch/riscv/cpu/start.S       | 5 +++++
 arch/riscv/lib/spl.c         | 4 ++++
 board/sifive/unmatched/spl.c | 7 +++++++
 3 files changed, 16 insertions(+)
arch/riscv/cpu/start.S +0 -0
index 8589509e01..2629f7e4cc 100644

arch/riscv/cpu/start.S call_board_init_f_0: · line 108

Quoted 7 lines
arch/riscv/cpu/start.S +4 -0
@@ -102,6 +102,11 @@ call_board_init_f_0:
        mv      a0, sp
        jal     board_init_f_alloc_reserve
 
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+call_each_cpu_core:
+       jal     mach_cpu_core_init
+#endif

Inserting it here potentially corrupts the return value of board_init_f_alloc_reserve(), and you need to insert it after sp is initialized for harts other than the boot hart.

Quoted 28 lines
arch/riscv/cpu/start.S +1 -0
+
        /*
         * Save global data pointer for later. We don't set it here because it
         * is not initialized yet.
arch/riscv/lib/spl.c +4 -0
index 8baee07bea..e8261d9c19 100644
@@ -14,6 +14,10 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+__weak void mach_cpu_core_init(void)
+{
+}
+
 __weak int spl_board_init_f(void)
 {
        return 0;
board/sifive/unmatched/spl.c +1 -0
index 5e1333b09a..cc466f703a 100644
@@ -22,6 +22,13 @@
 #define MODE_SELECT_SD         0xb
 #define MODE_SELECT_MASK       GENMASK(3, 0)
 
+void mach_cpu_core_init(void)

board/sifive/unmatched/spl.c

Quoted 10 lines
board/sifive/unmatched/spl.c +6 -1
+{
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+       asm volatile ("csrwi 0x7c1, 0");
+#endif
+}
+
 int spl_board_init_f(void)
 {
        int ret;
--

Regards,
Bin

author
#2 lore raw

RFC PATCH 1/1 arch: riscv: cpu: Add callback to init each core

Hi Bin,

I can move it to the place right after stacks for harts are set up and right before picking up the lottery hart. (like below) How about the function name? Do we come up with a better function name? mach_cpu_core_init() is similar to mach_cpu_init() in include/init.h. It could cause confusion.

116 /* setup stack */
117 #if CONFIG_IS_ENABLED(SMP)

118         /* tp: hart id */
119         slli    t0, tp, CONFIG_STACK_SIZE_SHIFT
120         sub     sp, a0, t0

121 #else
122 mv sp, a0
123 #endif
124

move it here

125 #ifndef CONFIG_XIP
126 /*
127 * Pick hart to initialize global data and run U-Boot. The
other harts
128 * wait for initialization to complete.
129 */

130         la      t0, hart_lottery
131         li      t1, 1
Quoted 92 lines · Bin Meng
Quoted 14 lines · Green Wan

I am looking for a proper place to add some M-mode code in SPL to configure features of each core as early as possible. Try to start the discussion by sending this RFC.

I tried mach_cpu_init() as the start. And not sure whether I used it correctly, it seems executed only by the core wins lottery in ./arch/riscv/cpu/start.S.

Is there a proper place to add some inits for each core like CSR in section 7.6 in document of U74-MC Core Complex Manual[1].

There are some alternatives to do it such as

Quoted 8 lines

a) sending IPI to each cores
Have to wait for IPI init for each core. And the boot core have to perform the init by itself and send IPIs to other cores. It might not be straightforward.
b) hook callback, (like this patch)
c) hard code in ./arch/riscv/cpu/start.c

[1]

https://sifive.cdn.prismic.io/sifive/aee0dd4c-d156-496e-a6c4-db0cf54bbe68_sifive_U74MC_rtl_full_20G1.03.00_manual.pdf

Quoted 13 lines
---
 arch/riscv/cpu/start.S       | 5 +++++
 arch/riscv/lib/spl.c         | 4 ++++
 board/sifive/unmatched/spl.c | 7 +++++++
 3 files changed, 16 insertions(+)
arch/riscv/cpu/start.S +0 -0
index 8589509e01..2629f7e4cc 100644

arch/riscv/cpu/start.S call_board_init_f_0: · line 108

Quoted 7 lines
arch/riscv/cpu/start.S +4 -0
@@ -102,6 +102,11 @@ call_board_init_f_0:
        mv      a0, sp
        jal     board_init_f_alloc_reserve
 
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+call_each_cpu_core:
+       jal     mach_cpu_core_init
+#endif

Inserting it here potentially corrupts the return value of board_init_f_alloc_reserve(), and you need to insert it after sp is initialized for harts other than the boot hart.

arch/riscv/cpu/start.S call_board_init_f_0:

Quoted 3 lines
arch/riscv/cpu/start.S +1 -0
+
        /*
         * Save global data pointer for later. We don't set it here

because it

Quoted 25 lines
arch/riscv/cpu/start.S +0 -0
         * is not initialized yet.
arch/riscv/lib/spl.c +4 -0
index 8baee07bea..e8261d9c19 100644
@@ -14,6 +14,10 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+__weak void mach_cpu_core_init(void)
+{
+}
+
 __weak int spl_board_init_f(void)
 {
        return 0;
board/sifive/unmatched/spl.c +1 -0
index 5e1333b09a..cc466f703a 100644
@@ -22,6 +22,13 @@
 #define MODE_SELECT_SD         0xb
 #define MODE_SELECT_MASK       GENMASK(3, 0)
 
+void mach_cpu_core_init(void)

board/sifive/unmatched/spl.c

Quoted 10 lines
board/sifive/unmatched/spl.c +6 -1
+{
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+       asm volatile ("csrwi 0x7c1, 0");
+#endif
+}
+
 int spl_board_init_f(void)
 {
        int ret;
--

Regards,
Bin

#3 lore raw

RFC PATCH 1/1 arch: riscv: cpu: Add callback to init each core

Hi Green,

Quoted 4 lines · Green Wan

Hi Bin,

I can move it to the place right after stacks for harts are set up and right before picking up the lottery hart. (like below) How about the function name? Do we come up with a better function name? mach_cpu_core_init() is similar to mach_cpu_init() in include/init.h. It could cause confusion.

How about riscv_core_early_init(), or riscv_hart_early_init()?

Regards,
Bin

author
#4 lore raw

RFC PATCH 1/1 arch: riscv: cpu: Add callback to init each core

Quoted 6 lines · Bin Meng

Hi Green,

Quoted 3 lines · Green Wan

Hi Bin,

Quoted 8 lines
Quoted 1 line

I can move it to the place right after stacks for harts are set up and

right before picking up the lottery hart. (like below) How about the function name? Do we come up with a better function name? mach_cpu_core_init() is similar to mach_cpu_init() in include/init.h. It could cause confusion.

How about riscv_core_early_init(), or riscv_hart_early_init()?

I'll vote for riscv_hart_early_init() and create the v2 patch based on it.

Thanks,

Quoted 3 lines

Regards,
Bin