Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions arch/risc-v/src/common/riscv_percpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include <nuttx/config.h>
#include <nuttx/irq.h>
#include <nuttx/queue.h>
#include <nuttx/spinlock.h>

#include <arch/barriers.h>
Expand Down Expand Up @@ -60,7 +59,6 @@ static_assert(RISCV_PERCPU_KSP == offsetof(riscv_percpu_t, ksp),
****************************************************************************/

static riscv_percpu_t g_percpu[HART_CNT];
static sq_queue_t g_freelist;
static uintptr_t g_initialized;
static spinlock_t g_percpu_spin;

Expand Down Expand Up @@ -96,17 +94,13 @@ static void riscv_percpu_init(void)
goto out_with_lock;
}

sq_init(&g_freelist);

for (i = 0; i < HART_CNT; i++)
{
/* Set interrupt stack (if any) */

#if CONFIG_ARCH_INTERRUPTSTACK > 15
g_percpu[i].irq_stack = (uintptr_t)g_intstacktop - i * INT_STACK_SIZE;
#endif

sq_addlast((struct sq_entry_s *) &g_percpu[i], &g_freelist);
}

out_with_lock:
Expand All @@ -131,18 +125,22 @@ static void riscv_percpu_init(void)
void riscv_percpu_add_hart(uintptr_t hartid)
{
riscv_percpu_t *percpu;
irqstate_t flags;
int cpu;

/* Make sure we are initialized */

riscv_percpu_init();

/* Get free entry for this hart, this must not fail */
/* Get the per CPU entry corresponding to this hart. */

flags = spin_lock_irqsave(&g_percpu_spin);
percpu = (riscv_percpu_t *)sq_remfirst(&g_freelist);
spin_unlock_irqrestore(&g_percpu_spin, flags);
DEBUGASSERT(percpu);
cpu = riscv_hartid_to_cpuid(hartid);

if (cpu < 0 || cpu >= HART_CNT)
{
PANIC();
}

percpu = &g_percpu[cpu];

/* Assign hartid, stack has already been assigned */

Expand Down
18 changes: 7 additions & 11 deletions arch/risc-v/src/common/riscv_percpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,16 @@
* will set up [m/s]scratch to point to the CPUs own area
*/

union riscv_percpu_s
struct riscv_percpu_s
{
union riscv_percpu_s *next; /* For sl list linkage */
struct
{
struct tcb_s *tcb; /* Current thread TCB */
uintreg_t hartid; /* Hart ID */
uintreg_t irq_stack; /* Interrupt stack */
uintreg_t usp; /* Area to store user sp */
uintreg_t ksp; /* Area to load kernel sp */
};
struct tcb_s *tcb; /* Current thread TCB */
uintreg_t hartid; /* Hart ID */
uintreg_t irq_stack; /* Interrupt stack */
uintreg_t usp; /* Area to store user sp */
uintreg_t ksp; /* Area to load kernel sp */
};

typedef union riscv_percpu_s riscv_percpu_t;
typedef struct riscv_percpu_s riscv_percpu_t;

/****************************************************************************
* Public Function Prototypes
Expand Down
Loading