This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 907a521b15d arch/risc-v: assign per-cpu data by logical cpu
907a521b15d is described below

commit 907a521b15db6b1dceb4159ce1921c4f77124895
Author: Megha Rajput <[email protected]>
AuthorDate: Sun Aug 30 10:51:03 2026 +0000

    arch/risc-v: assign per-cpu data by logical cpu
    
    Assign the per-CPU area using the HART to CPU mapping instead of the
    order in which HARTs register. This keeps interrupt stack assignment
    consistent with the logical CPU and avoids incorrect per-CPU IRQ stack
    selection when HART boot order differs.
    
    Signed-off-by: Megha Rajput <[email protected]>
---
 arch/risc-v/src/common/riscv_percpu.c | 22 ++++++++++------------
 arch/risc-v/src/common/riscv_percpu.h | 18 +++++++-----------
 2 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_percpu.c 
b/arch/risc-v/src/common/riscv_percpu.c
index 47178ea3476..a5cf68ce8c5 100644
--- a/arch/risc-v/src/common/riscv_percpu.c
+++ b/arch/risc-v/src/common/riscv_percpu.c
@@ -26,7 +26,6 @@
 
 #include <nuttx/config.h>
 #include <nuttx/irq.h>
-#include <nuttx/queue.h>
 #include <nuttx/spinlock.h>
 
 #include <arch/barriers.h>
@@ -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;
 
@@ -96,8 +94,6 @@ 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) */
@@ -105,8 +101,6 @@ static void riscv_percpu_init(void)
 #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:
@@ -131,18 +125,22 @@ out_with_lock:
 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 */
 
diff --git a/arch/risc-v/src/common/riscv_percpu.h 
b/arch/risc-v/src/common/riscv_percpu.h
index 4e6ea279763..cd2bf8934c2 100644
--- a/arch/risc-v/src/common/riscv_percpu.h
+++ b/arch/risc-v/src/common/riscv_percpu.h
@@ -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

Reply via email to