Skip to content

Commit 883e9d3

Browse files
Nicolas Pitrecarlescufi
authored andcommitted
riscv: translate CPU numbers to hartid values for IPI
Given the Zephyr CPU number is no longer tied to the hartid, we must consider the actual hartid when sending an IPI to a given CPU. Since those hartids can be anything, let's just save them in the cpu structure as each CPU is brought online. While at it, throw in some `get_hart_msip()` cleanups. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent 26d7bd4 commit 883e9d3

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

arch/riscv/core/smp.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ void arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
3434
void z_riscv_secondary_cpu_init(int cpu_num)
3535
{
3636
csr_write(mscratch, &_kernel.cpus[cpu_num]);
37+
#ifdef CONFIG_SMP
38+
_kernel.cpus[cpu_num].arch.hartid = csr_read(mhartid);
39+
_kernel.cpus[cpu_num].arch.online = true;
40+
#endif
3741
#ifdef CONFIG_THREAD_LOCAL_STORAGE
3842
__asm__("mv tp, %0" : : "r" (z_idle_threads[cpu_num].tls));
3943
#endif
@@ -50,13 +54,9 @@ void z_riscv_secondary_cpu_init(int cpu_num)
5054
}
5155

5256
#ifdef CONFIG_SMP
53-
static uintptr_t *get_hart_msip(int hart_id)
57+
static uint32_t *get_hart_msip(int hart_id)
5458
{
55-
#ifdef CONFIG_64BIT
56-
return (uintptr_t *)(uint64_t)(RISCV_MSIP_BASE + (hart_id * 4));
57-
#else
58-
return (uintptr_t *)(RISCV_MSIP_BASE + (hart_id * 4));
59-
#endif
59+
return (uint32_t *)(unsigned long)(RISCV_MSIP_BASE + (hart_id * 4));
6060
}
6161

6262
void arch_sched_ipi(void)
@@ -71,8 +71,8 @@ void arch_sched_ipi(void)
7171
unsigned int num_cpus = arch_num_cpus();
7272

7373
for (i = 0U; i < num_cpus; i++) {
74-
if (i != id) {
75-
volatile uint32_t *r = (uint32_t *)get_hart_msip(i);
74+
if (i != id && _kernel.cpus[i].arch.online) {
75+
volatile uint32_t *r = get_hart_msip(_kernel.cpus[i].arch.hartid);
7676
*r = 1U;
7777
}
7878
}
@@ -84,7 +84,7 @@ static void sched_ipi_handler(const void *unused)
8484
{
8585
ARG_UNUSED(unused);
8686

87-
volatile uint32_t *r = (uint32_t *)get_hart_msip(_current_cpu->id);
87+
volatile uint32_t *r = get_hart_msip(csr_read(mhartid));
8888
*r = 0U;
8989

9090
z_sched_ipi();

arch/riscv/include/kernel_arch_func.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ static ALWAYS_INLINE void arch_kernel_init(void)
3232
#if defined(CONFIG_SMP) || defined(CONFIG_USERSPACE)
3333
csr_write(mscratch, &_kernel.cpus[0]);
3434
#endif
35+
#ifdef CONFIG_SMP
36+
_kernel.cpus[0].arch.hartid = csr_read(mhartid);
37+
_kernel.cpus[0].arch.online = true;
38+
#endif
3539
#ifdef CONFIG_RISCV_PMP
3640
z_riscv_pmp_init();
3741
#endif

include/zephyr/arch/riscv/structs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ struct _cpu_arch {
1414
unsigned long user_exc_tmp0;
1515
unsigned long user_exc_tmp1;
1616
#endif
17+
#ifdef CONFIG_SMP
18+
unsigned long hartid;
19+
bool online;
20+
#endif
1721
};
1822

1923
#endif /* ZEPHYR_INCLUDE_RISCV_STRUCTS_H_ */

0 commit comments

Comments
 (0)