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

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

commit eb64ff3780b8860f3c8cda2facfe4dd751dcb7eb
Author: zhangyu117 <[email protected]>
AuthorDate: Tue Apr 21 17:07:03 2026 +0800

    arch/tricore: support interrupt nested
    
    Split the single interrupt entry (tricore_doirq) into two paths:
    tricore_irq_direct: fast dispatch with re-enable for nesting,
    triggers a low-priority GPSR if context switch is needed.
    
    tricore_irq_sched: deferred context switch and signal delivery at
    the lowest SRPN, runs only after all nested interrupts complete.
    
    Defer signal delivery for the running task to the scheduling GPSR so
    up_schedule_sigaction() always has a saved context. Save and restore
    PPRS around this path to preserve the protection register set.
    
    Signed-off-by: zhangyu117 <[email protected]>
---
 arch/tricore/include/irq.h                         |  2 +
 arch/tricore/src/common/gnu/tricore_head.S         |  2 -
 arch/tricore/src/common/gnu/tricore_vectors.S      | 11 ++-
 arch/tricore/src/common/tasking/tricore_head.asm   |  2 -
 .../tricore/src/common/tasking/tricore_vectors.asm | 14 +++-
 arch/tricore/src/common/tricore_doirq.c            | 87 ++++++++++++++++------
 arch/tricore/src/common/tricore_irq.c              | 22 +++++-
 .../tricore/src/common/tricore_schedulesigaction.c | 13 ++++
 .../tc3xx/a2g-tc397-5v-tft/scripts/tricore.ld      | 10 +--
 .../tc3xx/a2g-tc397-5v-tft/scripts/tricore.lsl     | 12 +--
 .../tc4xx/triboard_tc4x9_com/scripts/tricore.ld    | 10 +--
 .../tc4xx/triboard_tc4x9_com/scripts/tricore.lsl   | 12 +--
 12 files changed, 136 insertions(+), 61 deletions(-)

diff --git a/arch/tricore/include/irq.h b/arch/tricore/include/irq.h
index 6c37faef001..ff569169920 100644
--- a/arch/tricore/include/irq.h
+++ b/arch/tricore/include/irq.h
@@ -172,6 +172,8 @@
 #define TRICORE_GPSR_IRQNUM(src_cpu, dest_cpu)  \
   TRICORE_SRC2IRQ(TRICORE_SRC_GPSR_BASE + \
                   ((src_cpu) * TRICORE_SRCNUM_PER_GPSR + (dest_cpu)) * 4)
+#define TRICORE_SCHEDIRQ \
+  TRICORE_GPSR_IRQNUM(up_cpu_index(), up_cpu_index())
 
 /* TriCore interrupt priority range, higher value means higher priority. */
 
diff --git a/arch/tricore/src/common/gnu/tricore_head.S 
b/arch/tricore/src/common/gnu/tricore_head.S
index 797766adfe9..5c444e25cf2 100644
--- a/arch/tricore/src/common/gnu/tricore_head.S
+++ b/arch/tricore/src/common/gnu/tricore_head.S
@@ -32,7 +32,6 @@
  ****************************************************************************/
 
 #define TRICORE_PSW_RESET  0x980   /* Supervisor, call depth counting on */
-#define TRICORE_BIV_VECTOR 0x1fe0  /* Single interrupt vector at INTTAB + 
0x1fe0 */
 
 #ifdef CONFIG_ARCH_CHIP_TC3X
 #  define TC3XX_WDTCPU0_CON0 0xf003624c  /* CPU0 watchdog CON0, 0xc apart per 
CPU */
@@ -156,7 +155,6 @@ tricore_head:
 
        movh.a  %a15, hi:__INTTAB_CPU0
        lea     %a15, [%a15]lo:__INTTAB_CPU0
-       lea     %a15, [%a15]TRICORE_BIV_VECTOR
        mov.d   %d15, %a15
        mtcr    TRICORE_CPU_BIV, %d15
        isync
diff --git a/arch/tricore/src/common/gnu/tricore_vectors.S 
b/arch/tricore/src/common/gnu/tricore_vectors.S
index b3ab7c57719..3a93f9f074c 100644
--- a/arch/tricore/src/common/gnu/tricore_vectors.S
+++ b/arch/tricore/src/common/gnu/tricore_vectors.S
@@ -133,8 +133,10 @@ tricore_traptab:
        .size   tricore_traptab, . - tricore_traptab
 
 /****************************************************************************
- * Interrupt vector table.  One entry serves all priorities; the startup
- * code sets the BIV priority bits accordingly.
+ * Interrupt vector table
+ *
+ *   SRPN 1 -> tricore_irq_sched (lowest priority, task switch only),
+ *   SRPN 2~MAX -> tricore_irq_direct (fast dispatch, nestable).
  ****************************************************************************/
 
        .section .inttab, "ax", @progbits
@@ -142,5 +144,8 @@ tricore_traptab:
        .global tricore_inttab
        .type   tricore_inttab, @function
 tricore_inttab:
-       INTSLOT tricore_doirq
+       INTSLOT tricore_irq_sched
+       .rept   TRICORE_IRQ_MAX_PRIORITY - 1
+       INTSLOT tricore_irq_direct
+       .endr
        .size   tricore_inttab, . - tricore_inttab
diff --git a/arch/tricore/src/common/tasking/tricore_head.asm 
b/arch/tricore/src/common/tasking/tricore_head.asm
index bcb69ebeb12..a5024c9ef10 100644
--- a/arch/tricore/src/common/tasking/tricore_head.asm
+++ b/arch/tricore/src/common/tasking/tricore_head.asm
@@ -32,7 +32,6 @@
  ****************************************************************************/
 
 #define TRICORE_PSW_RESET  0x980   /* Supervisor, call depth counting on */
-#define TRICORE_BIV_VECTOR 0x1fe0  /* Single interrupt vector at INTTAB + 
0x1fe0 */
 
 #ifdef CONFIG_ARCH_CHIP_TC3X
 #  define TC3XX_WDTCPU0_CON0 0xf003624c  /* CPU0 watchdog CON0, 0xc apart per 
CPU */
@@ -180,7 +179,6 @@ tricore_head:
 
        movh.a  a15, #@HIS(__INTTAB_CPU0)
        lea     a15, [a15]@LOS(__INTTAB_CPU0)
-       lea     a15, [a15]TRICORE_BIV_VECTOR
        mov.d   d15, a15
        mtcr    #TRICORE_CPU_BIV, d15
        isync
diff --git a/arch/tricore/src/common/tasking/tricore_vectors.asm 
b/arch/tricore/src/common/tasking/tricore_vectors.asm
index cf36b93dc38..1937f3fa1ea 100644
--- a/arch/tricore/src/common/tasking/tricore_vectors.asm
+++ b/arch/tricore/src/common/tasking/tricore_vectors.asm
@@ -40,7 +40,8 @@
        .extern tricore_svcall
        .extern tricore_fcutrap_recover
        .extern g_fcu_csa_base
-       .extern tricore_doirq
+       .extern tricore_irq_sched
+       .extern tricore_irq_direct
 
 TRAPSLOT       .macro  cls, entry
        svlcx
@@ -137,8 +138,10 @@ ctx_notfcu:
        .size   tricore_traptab, * - tricore_traptab
 
 ;****************************************************************************
-; Interrupt vector table.  One entry serves all priorities; the startup
-; code sets the BIV priority bits accordingly.
+; Interrupt vector table
+;
+;   SRPN 1 -> tricore_irq_sched (lowest priority, task switch only),
+;   SRPN 2~MAX -> tricore_irq_direct (fast dispatch, nestable).
 ;****************************************************************************
 
        .sdecl  ".text.inttab",code,protect
@@ -146,7 +149,10 @@ ctx_notfcu:
        .align  32
        .global tricore_inttab
 tricore_inttab:        .type   func
-       INTSLOT tricore_doirq
+       INTSLOT tricore_irq_sched
+       .dup    TRICORE_IRQ_MAX_PRIORITY - 1
+       INTSLOT tricore_irq_direct
+       .endm
        .size   tricore_inttab, * - tricore_inttab
 
        .end
diff --git a/arch/tricore/src/common/tricore_doirq.c 
b/arch/tricore/src/common/tricore_doirq.c
index 84fda848276..97a486c1665 100644
--- a/arch/tricore/src/common/tricore_doirq.c
+++ b/arch/tricore/src/common/tricore_doirq.c
@@ -46,37 +46,80 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: tricore_doirq
+ * Name: tricore_irq_direct
  *
  * Description:
- *   Common interrupt handler, entered from the interrupt vector in
- *   tricore_vectors.S/.asm with the lower context saved.
+ *   Fast interrupt path (analogous to ARM-M exception_direct).
+ *   Only dispatches the IRQ handler, no context switch.
+ *   If a context switch is needed, triggers the scheduling GPSR which
+ *   will execute after all higher-priority interrupts have returned.
  *
  ****************************************************************************/
 
-void tricore_doirq(void)
+void tricore_irq_direct(void)
 {
   struct tcb_s **running_task = &g_running_tasks[this_cpu()];
-  struct tcb_s *tcb = this_task();
-
-#ifdef CONFIG_SUPPRESS_INTERRUPTS
-  PANIC();
-#else
+  uintptr_t *regs = tricore_csa2addr(tricore_mfcr(TRICORE_CPU_PCXI));
   int irq = tricore_irq_from_lasr();
+
+  up_irq_enable();
+
+  tricore_ack_irq(irq);
+  irq_dispatch(irq, regs);
+
+  if (*running_task != this_task())
+    {
+      cpu_set_t cpuset;
+
+      CPU_ZERO(&cpuset);
+      CPU_SET(up_cpu_index(), &cpuset);
+      up_trigger_irq(TRICORE_SCHEDIRQ, cpuset);
+    }
+
+  tricore_jump_backto_link();
+}
+
+/****************************************************************************
+ * Name: tricore_irq_sched
+ *
+ * Description:
+ *   Scheduling GPSR path for context switch (analogous to arm_doirq).
+ *   This is triggered by tricore_irq_direct when it detects that a
+ *   context switch is needed.
+ *
+ *   Because the GPSR SRPN is the lowest priority, this only executes
+ *   after all normal interrupts have completed, guaranteeing safe
+ *   context switch without race conditions.
+ *
+ ****************************************************************************/
+
+void tricore_irq_sched(void)
+{
+  struct tcb_s **running_task = &g_running_tasks[this_cpu()];
   uintptr_t *regs = tricore_csa2addr(tricore_mfcr(TRICORE_CPU_PCXI));
+  struct tcb_s *tcb;
 
   if (*running_task != NULL)
     {
       (*running_task)->xcp.regs = regs;
-    }
+      tricore_store_pprs(*running_task);
 
-  board_autoled_on(LED_INIRQ);
+#ifdef CONFIG_ARCH_IRQPRIO
+      if ((*running_task)->sigdeliver && !(*running_task)->xcp.saved_regs)
+        {
+          up_schedule_sigaction(*running_task);
+        }
+#endif
+    }
 
-  /* Deliver the IRQ */
+#ifdef CONFIG_ARCH_HIPRI_INTERRUPT
+  up_irq_save();
+  up_irq_enable();
+#endif
 
-  tricore_ack_irq(irq);
+  board_autoled_on(LED_INIRQ);
 
-  irq_dispatch(irq, regs);
+  tcb = this_task();
 
   /* Check for a context switch. */
 
@@ -97,22 +140,20 @@ void tricore_doirq(void)
       tcb = this_task();
 #endif
 
-      /* Updata PPRS register */
-
-      tricore_store_pprs(*running_task);
-      tricore_restore_pprs(tcb);
-
       /* Record the new "running" task when context switch occurred.
        * g_running_tasks[] is only used by assertion logic for reporting
        * crashes.
        */
 
       *running_task = tcb;
-
-      tricore_mtcr(TRICORE_CPU_PCXI, tricore_addr2csa(tcb->xcp.regs));
-      UP_ISB();
     }
 
+  /* The saved context may also reference a signal-delivery CSA. */
+
+  tricore_restore_pprs(tcb);
+  tricore_mtcr(TRICORE_CPU_PCXI, tricore_addr2csa(tcb->xcp.regs));
+  UP_ISB();
+
   /* (*running_task)->xcp.regs is about to become invalid and
    * will be marked as NULL to avoid misusage. the same applies
    * to (*running_task)->xcp.pprs.
@@ -122,7 +163,5 @@ void tricore_doirq(void)
   tricore_change_pprs(*running_task, UINT32_MAX);
 
   board_autoled_off(LED_INIRQ);
-#endif
-
   tricore_jump_backto_link();
 }
diff --git a/arch/tricore/src/common/tricore_irq.c 
b/arch/tricore/src/common/tricore_irq.c
index a29d67760cf..51a11dd3668 100644
--- a/arch/tricore/src/common/tricore_irq.c
+++ b/arch/tricore/src/common/tricore_irq.c
@@ -162,7 +162,19 @@ int tricore_irq_to_srpn(int irq)
 
   flags = spin_lock_irqsave(&g_irqlock);
 
-  for (i = TRICORE_IRQ_MIN_PRIORITY; i <= TRICORE_IRQ_MAX_PRIORITY; i++)
+  /* Scheduling GPSR always gets the lowest priority */
+
+  if (irq == TRICORE_SCHEDIRQ)
+    {
+      g_srpn_to_irq[TRICORE_IRQ_MIN_PRIORITY] = irq;
+      spin_unlock_irqrestore(&g_irqlock, flags);
+
+      return TRICORE_IRQ_MIN_PRIORITY;
+    }
+
+  /* Lookup: search the full SRPN range [MIN + 1, MAX] */
+
+  for (i = TRICORE_IRQ_MIN_PRIORITY + 1; i <= TRICORE_IRQ_MAX_PRIORITY; i++)
     {
       if (g_srpn_to_irq[i] == irq)
         {
@@ -243,6 +255,7 @@ void up_irqinitialize(void)
 #endif
 
   tricore_color_intstack();
+  up_enable_irq(TRICORE_SCHEDIRQ);
   up_irq_enable();
 }
 
@@ -347,8 +360,9 @@ int up_prioritize_irq(int irq, int priority)
   irqstate_t flags;
   int i;
 
-  DEBUGASSERT(priority >= TRICORE_IRQ_MIN_PRIORITY &&
-              priority <= TRICORE_IRQ_MAX_PRIORITY);
+  DEBUGASSERT(priority > TRICORE_IRQ_MIN_PRIORITY &&
+              priority <= TRICORE_IRQ_MAX_PRIORITY &&
+              irq != TRICORE_SCHEDIRQ);
 
   flags = spin_lock_irqsave(&g_irqlock);
 
@@ -366,7 +380,7 @@ int up_prioritize_irq(int irq, int priority)
 
   /* Clear old mapping if this IRQ had a different SRPN */
 
-  for (i = TRICORE_IRQ_MIN_PRIORITY; i <= TRICORE_IRQ_MAX_PRIORITY; i++)
+  for (i = TRICORE_IRQ_MIN_PRIORITY + 1; i <= TRICORE_IRQ_MAX_PRIORITY; i++)
     {
       if (g_srpn_to_irq[i] == irq)
         {
diff --git a/arch/tricore/src/common/tricore_schedulesigaction.c 
b/arch/tricore/src/common/tricore_schedulesigaction.c
index 27427c2f3ce..8bb567dd273 100644
--- a/arch/tricore/src/common/tricore_schedulesigaction.c
+++ b/arch/tricore/src/common/tricore_schedulesigaction.c
@@ -80,6 +80,19 @@
 
 void up_schedule_sigaction(struct tcb_s *tcb)
 {
+#ifdef CONFIG_ARCH_IRQPRIO
+  if (tcb == g_running_tasks[this_cpu()] &&
+      tricore_irq_from_lasr() != TRICORE_SCHEDIRQ)
+    {
+      cpu_set_t cpuset;
+
+      CPU_ZERO(&cpuset);
+      CPU_SET(up_cpu_index(), &cpuset);
+      up_trigger_irq(TRICORE_SCHEDIRQ, cpuset);
+      return;
+    }
+#endif
+
   /* Save the context registers.  These will be
    * restored by the signal trampoline after the signals have
    * been delivered.
diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.ld 
b/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.ld
index e68bb68c611..58d9a8991b8 100644
--- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.ld
+++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.ld
@@ -76,8 +76,8 @@ PROVIDE(__start5 = 0x80f00000);
 
 SECTIONS
 {
-  /* Reset entry, the trap table at +0x100 and the interrupt vector at
-   * +0x1fe0, which BIV points at with all priority bits set
+  /* Reset entry at +0, full BIV table at +0x20, trap table at +0x2000,
+   * and text at +0x2100.
    */
 
   .start ORIGIN(pfls) :
@@ -85,19 +85,19 @@ SECTIONS
     KEEP(*(.start))
   } > pfls
 
-  .traptab ORIGIN(pfls) + 0x100 :
+  .traptab ORIGIN(pfls) + 0x2000 :
   {
     __TRAPTAB_CPU0 = .;
     KEEP(*(.traptab))
   } > pfls
 
-  .intvec ORIGIN(pfls) + 0x1fe0 :
+  .intvec ORIGIN(pfls) + 0x20 :
   {
     __INTTAB_CPU0 = ORIGIN(pfls);
     KEEP(*(.inttab))
   } > pfls
 
-  .text :
+  .text ORIGIN(pfls) + 0x2100 :
   {
     *(.text .text.*)
     *(.gnu.linkonce.t.*)
diff --git a/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.lsl 
b/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.lsl
index d9615ec1b06..f233e81024e 100644
--- a/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.lsl
+++ b/boards/tricore/tc3xx/a2g-tc397-5v-tft/scripts/tricore.lsl
@@ -100,8 +100,8 @@ section_setup mpe:tc0:linear
 
 section_layout mpe:tc0:linear
 {
-  /* Reset entry, the trap table at +0x100 and the interrupt vector at
-   * +0x1fe0, which BIV points at with all priority bits set
+  /* Reset entry at +0, full BIV table at +0x20, trap table at +0x2000,
+   * and text at +0x2100.
    */
 
   group start (ordered, run_addr = PFLS_BASE)
@@ -109,17 +109,17 @@ section_layout mpe:tc0:linear
     select ".text.start";
   }
 
-  group traptab (ordered, run_addr = PFLS_BASE + 0x100)
+  group traptab (ordered, run_addr = PFLS_BASE + 0x2000)
   {
     select ".text.traptab";
   }
 
-  group intvec (ordered, run_addr = PFLS_BASE + 0x1fe0)
+  group intvec (ordered, run_addr = PFLS_BASE + 0x20)
   {
     select ".text.inttab";
   }
 
-  group text (run_addr = mem:mpe:pfls)
+  group text (ordered, run_addr = PFLS_BASE + 0x2100)
   {
     select ".text*";
     select ".rodata*";
@@ -170,7 +170,7 @@ section_layout mpe:tc0:linear
     reserved "csa" (size = CSA_SIZE);
   }
 
-  "__TRAPTAB_CPU0" := PFLS_BASE + 0x100;
+  "__TRAPTAB_CPU0" := PFLS_BASE + 0x2000;
   "__INTTAB_CPU0" := PFLS_BASE;
 
   "__a0_mem" := sizeof(group:a0) > 0 ? addressof(group:a0) + 0x8000 : 0;
diff --git a/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.ld 
b/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.ld
index fc63b06acd8..4155608965a 100644
--- a/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.ld
+++ b/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.ld
@@ -82,8 +82,8 @@ PROVIDE(__start5 = 0x8092c000);
 
 SECTIONS
 {
-  /* Reset entry, the trap table at +0x100 and the interrupt vector at
-   * +0x1fe0, which BIV points at with all priority bits set
+  /* Reset entry at +0, full BIV table at +0x20, trap table at +0x2000,
+   * and text at +0x2100.
    */
 
   .start ORIGIN(pfls) :
@@ -91,19 +91,19 @@ SECTIONS
     KEEP(*(.start))
   } > pfls
 
-  .traptab ORIGIN(pfls) + 0x100 :
+  .traptab ORIGIN(pfls) + 0x2000 :
   {
     __TRAPTAB_CPU0 = .;
     KEEP(*(.traptab))
   } > pfls
 
-  .intvec ORIGIN(pfls) + 0x1fe0 :
+  .intvec ORIGIN(pfls) + 0x20 :
   {
     __INTTAB_CPU0 = ORIGIN(pfls);
     KEEP(*(.inttab))
   } > pfls
 
-  .text :
+  .text ORIGIN(pfls) + 0x2100 :
   {
     *(.text .text.*)
     *(.gnu.linkonce.t.*)
diff --git a/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.lsl 
b/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.lsl
index e911e50fe06..076c5961641 100644
--- a/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.lsl
+++ b/boards/tricore/tc4xx/triboard_tc4x9_com/scripts/tricore.lsl
@@ -112,8 +112,8 @@ section_setup mpe:tc0:linear
 
 section_layout mpe:tc0:linear
 {
-  /* Reset entry, the trap table at +0x100 and the interrupt vector at
-   * +0x1fe0, which BIV points at with all priority bits set
+  /* Reset entry at +0, full BIV table at +0x20, trap table at +0x2000,
+   * and text at +0x2100.
    */
 
   group start (ordered, run_addr = PFLS_BASE)
@@ -121,17 +121,17 @@ section_layout mpe:tc0:linear
     select ".text.start";
   }
 
-  group traptab (ordered, run_addr = PFLS_BASE + 0x100)
+  group traptab (ordered, run_addr = PFLS_BASE + 0x2000)
   {
     select ".text.traptab";
   }
 
-  group intvec (ordered, run_addr = PFLS_BASE + 0x1fe0)
+  group intvec (ordered, run_addr = PFLS_BASE + 0x20)
   {
     select ".text.inttab";
   }
 
-  group text (run_addr = mem:mpe:pfls)
+  group text (ordered, run_addr = PFLS_BASE + 0x2100)
   {
     select ".text*";
     select ".rodata*";
@@ -180,7 +180,7 @@ section_layout mpe:tc0:linear
     reserved "csa" (size = CSA_SIZE);
   }
 
-  "__TRAPTAB_CPU0" := PFLS_BASE + 0x100;
+  "__TRAPTAB_CPU0" := PFLS_BASE + 0x2000;
   "__INTTAB_CPU0" := PFLS_BASE;
 
   "__a0_mem" := sizeof(group:a0) > 0 ? addressof(group:a0) + 0x8000 : 0;

Reply via email to