Skip to content

Commit 737dc4f

Browse files
pussuwxiaoxiang781216
authored andcommitted
arch/riscv: Implement cpuid mapping
Implement hartid<->cpuid mapping for RISC-V. This is necessary for some platforms which cannot use 1:1 mapping between logical and physical CPU / core IDs. One example is MPFS where hart0 cannot be used for NuttX SMP as it is a less capable "monitor" core (E51) compared to the application cores hart1...3 (E54). Why not just use a generic offset then? We also need the physical hart ID for many things: - Communication between harts (IPI) - External interrupt acknowledgment (interrupt claim for specific CPU) - Communication to SBI Thus, create procedures that can do this translation: - The default mapping is still logical=physical. - Another flavor is to use the existing CONFIG_ARCH_RV_HARTID_BASE config variable, which is just a simple offset - The final flavor is to overload hartid<->cpuid on a per chip basis (no example for this is provided yet)
1 parent 2195b47 commit 737dc4f

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

arch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ config ARCH_RISCV
9595
select ARCH_HAVE_THREAD_LOCAL
9696
select ARCH_HAVE_POWEROFF
9797
select ARCH_HAVE_LAZYFPU if ARCH_HAVE_FPU
98+
select ARCH_HAVE_CPUID_MAPPING if ARCH_HAVE_MULTICPU
9899
---help---
99100
RISC-V 32 and 64-bit RV32 / RV64 architectures.
100101

arch/risc-v/include/irq.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,14 +709,25 @@ irqstate_t up_irq_enable(void);
709709
int up_cpu_index(void) noinstrument_function;
710710
#endif /* CONFIG_ARCH_HAVE_MULTICPU */
711711

712+
/****************************************************************************
713+
* Name: up_this_cpu
714+
*
715+
* Description:
716+
* Return the logical core number. Default implementation is 1:1 mapping,
717+
* i.e. physical=logical.
718+
*
719+
****************************************************************************/
720+
721+
int up_this_cpu(void);
722+
712723
/****************************************************************************
713724
* Inline Functions
714725
****************************************************************************/
715726

716727
static inline_function uintreg_t *up_current_regs(void)
717728
{
718729
#ifdef CONFIG_SMP
719-
return (uintreg_t *)g_current_regs[up_cpu_index()];
730+
return (uintreg_t *)g_current_regs[up_this_cpu()];
720731
#else
721732
return (uintreg_t *)g_current_regs[0];
722733
#endif
@@ -725,7 +736,7 @@ static inline_function uintreg_t *up_current_regs(void)
725736
static inline_function void up_set_current_regs(uintreg_t *regs)
726737
{
727738
#ifdef CONFIG_SMP
728-
g_current_regs[up_cpu_index()] = regs;
739+
g_current_regs[up_this_cpu()] = regs;
729740
#else
730741
g_current_regs[0] = regs;
731742
#endif

arch/risc-v/src/common/riscv_cpuindex.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,47 @@
4545

4646
int up_cpu_index(void)
4747
{
48-
return (int)riscv_mhartid() - CONFIG_ARCH_RV_HARTID_BASE;
48+
return (int)riscv_mhartid();
49+
}
50+
51+
/****************************************************************************
52+
* Name: up_this_cpu
53+
*
54+
* Description:
55+
* Return the logical core number. Default implementation is 1:1 mapping,
56+
* i.e. physical=logical.
57+
*
58+
****************************************************************************/
59+
60+
int up_this_cpu(void)
61+
{
62+
return riscv_hartid_to_cpuid((int)riscv_mhartid());
63+
}
64+
65+
/****************************************************************************
66+
* Name: riscv_hartid_to_cpuid
67+
*
68+
* Description:
69+
* Convert physical core number to logical core number. Default
70+
* implementation is 1:1 mapping, i.e. physical=logical.
71+
*
72+
****************************************************************************/
73+
74+
int weak_function riscv_hartid_to_cpuid(int cpu)
75+
{
76+
return cpu + CONFIG_ARCH_RV_HARTID_BASE;
77+
}
78+
79+
/****************************************************************************
80+
* Name: riscv_cpuid_to_hartid
81+
*
82+
* Description:
83+
* Convert logical core number to physical core number. Default
84+
* implementation is 1:1 mapping, i.e. physical=logical.
85+
*
86+
****************************************************************************/
87+
88+
int weak_function riscv_cpuid_to_hartid(int cpu)
89+
{
90+
return cpu - CONFIG_ARCH_RV_HARTID_BASE;
4991
}

arch/risc-v/src/common/riscv_cpustart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void riscv_cpu_boot(int cpu)
8686
#ifdef CONFIG_RISCV_PERCPU_SCRATCH
8787
/* Initialize the per CPU areas */
8888

89-
riscv_percpu_add_hart((uintptr_t)cpu);
89+
riscv_percpu_add_hart(riscv_cpuid_to_hartid(cpu));
9090
#endif
9191

9292
#ifdef CONFIG_BUILD_KERNEL

arch/risc-v/src/common/riscv_internal.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,28 @@ int riscv_smp_call_handler(int irq, void *c, void *arg);
427427

428428
uintptr_t riscv_mhartid(void);
429429

430+
/****************************************************************************
431+
* Name: riscv_hartid_to_cpuid
432+
*
433+
* Description:
434+
* Convert physical core number to logical core number. Default
435+
* implementation is 1:1 mapping, i.e. physical=logical.
436+
*
437+
****************************************************************************/
438+
439+
int riscv_hartid_to_cpuid(int cpu);
440+
441+
/****************************************************************************
442+
* Name: riscv_cpuid_to_hartid
443+
*
444+
* Description:
445+
* Convert logical core number to physical core number. Default
446+
* implementation is 1:1 mapping, i.e. physical=logical.
447+
*
448+
****************************************************************************/
449+
450+
int riscv_cpuid_to_hartid(int cpu);
451+
430452
/* If kernel runs in Supervisor mode, a system call trampoline is needed */
431453

432454
#ifdef CONFIG_ARCH_USE_S_MODE

arch/risc-v/src/common/riscv_ipi.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
static inline void riscv_ipi_send(int cpu)
3737
{
3838
#if defined(CONFIG_ARCH_USE_S_MODE)
39-
riscv_sbi_send_ipi(0x1, cpu);
39+
riscv_sbi_send_ipi(0x1, riscv_cpuid_to_hartid(cpu));
4040
#elif defined(RISCV_IPI)
41-
putreg32(1, (uintptr_t)RISCV_IPI + (4 * cpu));
41+
putreg32(1, (uintptr_t)RISCV_IPI + (4 * riscv_cpuid_to_hartid(cpu)));
4242
#else
4343
# error "No IPI support for this SoC"
4444
#endif
@@ -47,7 +47,7 @@ static inline void riscv_ipi_send(int cpu)
4747
static inline void riscv_ipi_clear(int cpu)
4848
{
4949
#if defined(RISCV_IPI) && !defined(CONFIG_ARCH_USE_S_MODE)
50-
putreg32(0, (uintptr_t)RISCV_IPI + (4 * cpu));
50+
putreg32(0, (uintptr_t)RISCV_IPI + (4 * riscv_cpuid_to_hartid(cpu)));
5151
#endif
5252
CLEAR_CSR(CSR_IP, IP_SIP);
5353
}

0 commit comments

Comments
 (0)