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

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

commit 03a9825c909139ab568b9c816639d61bc6b49a2b
Author: zhangyu117 <[email protected]>
AuthorDate: Thu Nov 26 11:12:27 2026 +0800

    arch/tricore: optimize interrupt handling
    
    1. Unify interrupt router register definitions and address calculations.
    2. Access SRC and SRB registers through NuttX helper functions.
    3. Simplify SRC routing, GPSR initialization and SMP IRQ affinity handling.
    
    Signed-off-by: zhangyu117 <[email protected]>
---
 arch/tricore/include/irq.h                 | 188 +++++++++++++------------
 arch/tricore/include/tc3x/irq.h            |  31 ++++-
 arch/tricore/include/tc4x/irq.h            |  36 ++++-
 arch/tricore/src/common/tricore_internal.h |   1 +
 arch/tricore/src/common/tricore_irq.c      | 212 +++++++++++++++++------------
 5 files changed, 275 insertions(+), 193 deletions(-)

diff --git a/arch/tricore/include/irq.h b/arch/tricore/include/irq.h
index 80070aed5ef..ae2782588a8 100644
--- a/arch/tricore/include/irq.h
+++ b/arch/tricore/include/irq.h
@@ -37,10 +37,11 @@
 #  include <syscall.h>
 #endif
 
+#include <arch/arch.h>
+
 /* Include chip-specific IRQ definitions (including IRQ numbers) */
 
 #include <arch/chip/irq.h>
-#include <arch/arch.h>
 
 /****************************************************************************
  * Pre-processor Prototypes
@@ -48,71 +49,72 @@
 
 /* Upper CSA */
 
-#define REG_UPCXI        0
-#define REG_PSW          1
-#define REG_A10          2
-#define REG_UA11         3
-#define REG_D8           4
-#define REG_D9           5
-#define REG_D10          6
-#define REG_D11          7
-#define REG_A12          8
-#define REG_A13          9
-#define REG_A14          10
-#define REG_A15          11
-#define REG_D12          12
-#define REG_D13          13
-#define REG_D14          14
-#define REG_D15          15
+#define REG_UPCXI           0
+#define REG_PSW             1
+#define REG_A10             2
+#define REG_UA11            3
+#define REG_D8              4
+#define REG_D9              5
+#define REG_D10             6
+#define REG_D11             7
+#define REG_A12             8
+#define REG_A13             9
+#define REG_A14             10
+#define REG_A15             11
+#define REG_D12             12
+#define REG_D13             13
+#define REG_D14             14
+#define REG_D15             15
 
 /* Lower CSA */
 
-#define REG_LPCXI        0
-#define REG_LA11         1
-#define REG_A2           2
-#define REG_A3           3
-#define REG_D0           4
-#define REG_D1           5
-#define REG_D2           6
-#define REG_D3           7
-#define REG_A4           8
-#define REG_A5           9
-#define REG_A6           10
-#define REG_A7           11
-#define REG_D4           12
-#define REG_D5           13
-#define REG_D6           14
-#define REG_D7           15
-
-#define REG_RA           REG_UA11
-#define REG_SP           REG_A10
-#define REG_UPC          REG_UA11
-
-#define REG_LPC          REG_LA11
-
-#define TC_CONTEXT_REGS  (16)
-#define TC_CONTEXT_SIZE  (sizeof(void *) * TC_CONTEXT_REGS)
-
-#define FCD_RESERVE_SIZE (TC_CONTEXT_SIZE * 4)
-
-#define XCPTCONTEXT_REGS (TC_CONTEXT_REGS * 2)
-#define XCPTCONTEXT_SIZE (sizeof(void *) * XCPTCONTEXT_REGS)
-
-#define NR_IRQS          (2048)
+#define REG_LPCXI           0
+#define REG_LA11            1
+#define REG_A2              2
+#define REG_A3              3
+#define REG_D0              4
+#define REG_D1              5
+#define REG_D2              6
+#define REG_D3              7
+#define REG_A4              8
+#define REG_A5              9
+#define REG_A6              10
+#define REG_A7              11
+#define REG_D4              12
+#define REG_D5              13
+#define REG_D6              14
+#define REG_D7              15
+
+#define REG_RA              REG_UA11
+#define REG_SP              REG_A10
+#define REG_UPC             REG_UA11
+#define REG_LPC             REG_LA11
+
+#define TC_CONTEXT_REGS     (16)
+#define TC_CONTEXT_SIZE     (4 * TC_CONTEXT_REGS)
+#define TC_CONTEXT_ALIGNED(addr) \
+  (((uintptr_t)(addr) & (TC_CONTEXT_SIZE - 1)) == 0)
+
+#define XCPTCONTEXT_REGS    (TC_CONTEXT_REGS * 2)
+#define XCPTCONTEXT_SIZE    (sizeof(void *) * XCPTCONTEXT_REGS)
+
+#define FCD_RESERVE_SIZE    (TC_CONTEXT_SIZE * 4)
 
 /* PSW: Program Status Word Register */
 
-#define PSW_CDE         (1 << 7) /* Bits 7: Call Depth Count Enable */
-#define PSW_GW          (1 << 8) /* Global address register write permission */
-#define PSW_IS          (1 << 9) /* Bits 9: Interrupt Stack Control */
-#define PSW_IO          (10)     /* Bits 10-11: Access Privilege Level Control 
(I/O Privilege) */
+#define PSW_CDE             (1 << 7) /* Bits 7: Call Depth Count Enable */
+#define PSW_GW              (1 << 8) /* Global address register write 
permission */
+#define PSW_IS              (1 << 9) /* Bits 9: Interrupt Stack Control */
+#define PSW_IO              (10)     /* Bits 10-11: Access Privilege Level 
Control (I/O Privilege) */
 #  define PSW_MODE_MASK     (3 << PSW_IO)
 #  define PSW_IO_USER0      (0 << PSW_IO)
 #  define PSW_IO_USER1      (1 << PSW_IO)
 #  define PSW_IO_SUPERVISOR (2 << PSW_IO)
+#define PSW_STATUS_BITS     (31 << 27) /* Bits 27-31: Execution Effect Status 
Bits */
 
 #define PSW_PRS_KERNEL_SET  (0)
 #define PSW_PRS_USER_SET    (1)
+
 #ifdef CONFIG_ARCH_HAVE_PPRS
   #define PSW_PRS_MASK                           (0)
   #define PSW_PRS_USER                           (0)
@@ -154,18 +156,20 @@
 #define FCX_FCXS_MASK   (0xf    << FCX_FCXS)
 #define FCX_FREE        (FCX_FCXS_MASK | FCX_FCXO_MASK) /* Free CSA 
manipulation */
 
-#define TRICORE_SRCNUM_PER_GPSR  8
-#define TRICORE_SRC2IRQ(src_addr) \
-  (((uintptr_t)(src_addr) - TRICORE_SRC_BASE) / 4)
+#define TRICORE_SRCNUM_PER_GPSR 8
+#define TRICORE_SRC_SPACING     4
+
+#define TRICORE_SRC2IRQ(src) \
+  (((uintptr_t)(src) - TRICORE_SRC_BASE) / TRICORE_SRC_SPACING)
 #define TRICORE_IRQ2SRC(irq) \
-  ((volatile uint32_t *)(TRICORE_SRC_BASE + (irq) * 4))
-#define TRICORE_GPSR_IRQNUM(src_cpu, dest_cpu)  \
+  (TRICORE_SRC_BASE + (irq) * TRICORE_SRC_SPACING)
+#define TRICORE_GPSR2IRQ(src_cpu, dest_cpu) \
   TRICORE_SRC2IRQ(TRICORE_SRC_GPSR_BASE + \
-                  ((src_cpu) * TRICORE_SRCNUM_PER_GPSR + (dest_cpu)) * 4)
+                  (src_cpu) * TRICORE_SRCNUM_PER_GPSR * TRICORE_SRC_SPACING + \
+                  (dest_cpu) * TRICORE_SRC_SPACING)
 
 #ifdef CONFIG_ARCH_IRQPRIO
-#  define TRICORE_SCHEDIRQ \
-     TRICORE_GPSR_IRQNUM(up_cpu_index(), up_cpu_index())
+#  define TRICORE_SCHEDIRQ TRICORE_GPSR2IRQ(up_this_cpu(), up_this_cpu())
 #endif
 
 /* TriCore interrupt priority range, higher value means higher priority. */
@@ -218,29 +222,34 @@ struct xcptcontext
   uintptr_t *saved_regs;
 #endif
 
-#ifdef CONFIG_BUILD_PROTECTED
-  /* This is the saved address to use when returning from a user-space
-   * signal handler.
+  /* Register save area with XCPTCONTEXT_SIZE, only valid when:
+   * 1.The task isn't running or
+   * 2.The task is interrupted
+   * otherwise task is running, and regs contain the stale value.
    */
 
-  uint32_t sigreturn;
-#endif
+  uintptr_t *regs;
 
 #ifdef CONFIG_LIB_SYSCALL
-  uint8_t nsyscalls;
+  /* The following array holds information needed to return from each nested
+   * system call.
+   */
+
+  int nsyscalls;
   uintptr_t *syscall_regs[CONFIG_SYS_NNEST];
 #ifdef CONFIG_ARCH_HAVE_PPRS
   uint32_t syscall_pprs[CONFIG_SYS_NNEST];
 #endif
 #endif
 
-  /* Register save area with XCPTCONTEXT_SIZE, only valid when:
-   * 1.The task isn't running or
-   * 2.The task is interrupted
-   * otherwise task is running, and regs contain the stale value.
+#ifdef CONFIG_BUILD_PROTECTED
+  /* This is the saved address to use when returning from a user-space
+   * signal handler.
    */
 
-  uintptr_t *regs;
+  uint32_t sigreturn;
+
+#endif
 
 #ifdef CONFIG_ARCH_HAVE_PPRS
   uint32_t pprs;
@@ -294,14 +303,7 @@ void up_irq_enable(void);
 
 noinstrument_function static inline_function uintptr_t up_getpc(void)
 {
-#ifdef CONFIG_TRICORE_TOOLCHAIN_TASKING
-  return (uintptr_t)__mfcr(TRICORE_CPU_PC);
-#else
-  uintptr_t pc;
-  __asm__ __volatile__ ("mfcr %0,%1": "=d" (pc) :"i"(TRICORE_CPU_PC):
-                        "memory");
-  return pc;
-#endif
+  return (uintptr_t)tricore_mfcr(TRICORE_CPU_PC);
 }
 
 noinstrument_function static inline_function uintptr_t up_getsp(void)
@@ -332,28 +334,25 @@ noinstrument_function static inline_function uintptr_t 
up_getsp(void)
 
 noinstrument_function static inline_function irqstate_t up_irq_save(void)
 {
+  irqstate_t flags;
+
 #ifdef CONFIG_ARCH_HIPRI_INTERRUPT
-  irqstate_t icr;
-  irqstate_t ccpn;
+  uint32_t icr = tricore_mfcr(TRICORE_CPU_ICR);
 
-  icr  = tricore_mfcr(TRICORE_CPU_ICR);
-  ccpn = icr & TRICORE_ICR_CCPN_MASK;
+  flags = icr & TRICORE_ICR_CCPN_MASK;
 
-  if (TRICORE_IRQ_DISABLE_PRIORITY > ccpn)
+  if (TRICORE_IRQ_DISABLE_PRIORITY > flags)
     {
       icr = (icr & ~TRICORE_ICR_CCPN_MASK) |
             TRICORE_IRQ_DISABLE_PRIORITY;
       tricore_mtcr(TRICORE_CPU_ICR, icr);
       UP_ISB();
     }
-
-  return ccpn;
 #else
-  irqstate_t flags;
+  __asm__ __volatile__ ("disable %0" : "=d"(flags) :: "memory");
+#endif
 
-  __asm__ __volatile__ ("disable %0" : "=d"(flags) : : "memory");
   return flags;
-#endif
 }
 
 /****************************************************************************
@@ -371,9 +370,8 @@ noinstrument_function static inline_function
 void up_irq_restore(irqstate_t flags)
 {
 #ifdef CONFIG_ARCH_HIPRI_INTERRUPT
-  irqstate_t icr;
+  uint32_t icr = tricore_mfcr(TRICORE_CPU_ICR);
 
-  icr = tricore_mfcr(TRICORE_CPU_ICR);
   if (flags < (icr & TRICORE_ICR_CCPN_MASK))
     {
       icr = (icr & ~TRICORE_ICR_CCPN_MASK) | flags;
@@ -381,7 +379,7 @@ void up_irq_restore(irqstate_t flags)
       UP_ISB();
     }
 #else
-  __asm__ __volatile__ ("restore %0" : : "d"(flags) : "memory");
+  __asm__ __volatile__ ("restore %0" :: "d"(flags) : "memory");
 #endif
 }
 
@@ -423,7 +421,7 @@ static inline_function uintptr_t up_getusrsp(void *regs)
     }
   else
     {
-       csaregs += TC_CONTEXT_REGS;
+      csaregs += TC_CONTEXT_REGS;
     }
 
   return csaregs[REG_SP];
@@ -441,7 +439,7 @@ static inline_function uintptr_t up_getusrsp(void *regs)
       {                                                           \
         sys_call0(SYS_switch_context);                            \
       }                                                           \
-      UNUSED(rtcb);                                               \
+    UNUSED(rtcb);                                                 \
   } while (0)
 
 /****************************************************************************
diff --git a/arch/tricore/include/tc3x/irq.h b/arch/tricore/include/tc3x/irq.h
index ae0b9f82846..48115c59804 100644
--- a/arch/tricore/include/tc3x/irq.h
+++ b/arch/tricore/include/tc3x/irq.h
@@ -31,21 +31,44 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 /****************************************************************************
- * Pre-processor Prototypes
+ * Pre-processor Definitions
  ****************************************************************************/
 
+#define NR_IRQS 2048
+
 #define TRICORE_UART_RX_IRQ      21
 
-/* Interrupt router: the IRQ number is the index of the service request
- * control (SRC) register from TRICORE_SRC_BASE.
+/* Interrupt router: the irq number is the index of service request
+ * control (SRC).
  */
 
 #define TRICORE_SRC_BASE         0xf0038000  /* SRC_CPU0SB, IRQ 0 */
 #define TRICORE_SRC_GPSR_BASE    0xf0038990  /* SRC_GPSR0SR0 */
 #define TRICORE_INT_SRB_BASE     0xf0037010  /* INT_SRB0 */
+#define TRICORE_INT_SRB_SPACING  0x4         /* INT_SRB1 - INT_SRB0 */
 #define TRICORE_INT_LASR_BASE    0xf0037204  /* INT_LASR0 */
-#define TRICORE_INT_LASR_SPACING 0x10        /* INT_LASRn spacing */
+#define TRICORE_INT_LASR_SPACING 0x10        /* INT_LASR1 - INT_LASR0 */
+
+/* SRC register layout (TC3xx):
+ *   [7:0]   SRPN   - Service request priority number
+ *   [9:8]   Reserved
+ *   [10]    SRE    - Service request enable
+ *   [13:11] TOS    - Type of service (3-bit, CPU0-5/DMA)
+ *   [15:14] Reserved
+ *   [20:16] ECC    - Error correction code
+ *   [23:21] Reserved
+ *   [24]    SRR    - Service request flag
+ *   [25]    CLRR   - Request clear
+ *   [26]    SETR   - Request set
+ *   [27]    IOV    - Interrupt trigger overflow
+ *   [28]    IOVCLR - Interrupt trigger overflow clear
+ *   [29]    SWS    - Software sticky
+ *   [30]    SWSCLR - Software sticky clear
+ *   [31]    Reserved
+ */
 
 #define TRICORE_SRC_SRPN_MASK    0xff        /* Service request priority */
 #define TRICORE_SRC_SRE          (1 << 10)   /* Service request enable */
diff --git a/arch/tricore/include/tc4x/irq.h b/arch/tricore/include/tc4x/irq.h
index af944a80964..c56249bd7d2 100644
--- a/arch/tricore/include/tc4x/irq.h
+++ b/arch/tricore/include/tc4x/irq.h
@@ -31,21 +31,45 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 /****************************************************************************
- * Pre-processor Prototypes
+ * Pre-processor Definitions
  ****************************************************************************/
 
+#define NR_IRQS 2048
+
 #define TRICORE_UART_RX_IRQ      173
 
-/* Interrupt router: the IRQ number is the index of the service request
- * control (SRC) register from TRICORE_SRC_BASE.
+/* Interrupt router: the irq number is the index of service request
+ * control (SRC).
  */
 
 #define TRICORE_SRC_BASE         0xf4432000  /* SRC_CPU0SB, IRQ 0 */
-#define TRICORE_SRC_GPSR_BASE    0xf4433460  /* SRC_GPSR0SR0 */
+#ifdef CONFIG_ARCH_CHIP_TC4DX
+#  define TRICORE_SRC_GPSR_BASE  0xf4433460  /* SRC_GPSR0SR0, TC4DX */
+#else
+#  define TRICORE_SRC_GPSR_BASE  0xf4433420  /* SRC_GPSR0SR0, TC48X/TC4ZX */
+#endif
 #define TRICORE_INT_SRB_BASE     0xf4430b00  /* INT_SRB0 */
-#define TRICORE_INT_LASR_BASE    0xf4430c20  /* INT_LASR0 */
-#define TRICORE_INT_LASR_SPACING 0x34        /* INT_LASRn spacing */
+#define TRICORE_INT_SRB_SPACING  0x4         /* INT_SRB1 - INT_SRB0 */
+#define TRICORE_INT_LASR_BASE    0xf4430c20  /* INT_ICU0_LASR */
+#define TRICORE_INT_LASR_SPACING 0x34
+
+/* SRC register layout (TC4xx):
+ *   [7:0]   SRPN   - Service request priority number
+ *   [10:8]  VM     - Virtual machine
+ *   [11]    CS     - Cyber security, set by the CSRM core
+ *   [15:12] TOS    - Type of service (4-bit)
+ *   [22:16] Reserved
+ *   [23]    SRE    - Service request enable
+ *   [24]    SRR    - Service request status
+ *   [25]    CLRR   - Service request clear
+ *   [26]    SETR   - Service request set
+ *   [27]    IOV    - Interrupt overflow status
+ *   [28]    IOVCLR - Interrupt overflow clear
+ *   [31:29] Reserved
+ */
 
 #define TRICORE_SRC_SRPN_MASK    0xff        /* Service request priority */
 #define TRICORE_SRC_VM_MASK      (0x7 << 8)  /* Virtual machine */
diff --git a/arch/tricore/src/common/tricore_internal.h 
b/arch/tricore/src/common/tricore_internal.h
index 8474e9a76c7..e46bd0df6c7 100644
--- a/arch/tricore/src/common/tricore_internal.h
+++ b/arch/tricore/src/common/tricore_internal.h
@@ -199,6 +199,7 @@ static inline_function int tricore_irq_from_lasr(void)
 void tricore_ack_irq(int irq);
 int tricore_srpn_to_irq(int srpn);
 int tricore_irq_to_srpn(int irq);
+void tricore_src_init(int irq, uint8_t tos, uint8_t srpn);
 
 /* Startup ******************************************************************/
 
diff --git a/arch/tricore/src/common/tricore_irq.c 
b/arch/tricore/src/common/tricore_irq.c
index 97082287ef0..1693815888a 100644
--- a/arch/tricore/src/common/tricore_irq.c
+++ b/arch/tricore/src/common/tricore_irq.c
@@ -29,6 +29,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <nuttx/debug.h>
+#include <sched.h>
 
 #include <nuttx/arch.h>
 #include <nuttx/irq.h>
@@ -78,63 +79,17 @@ static spinlock_t g_irqlock = SP_UNLOCKED;
  ****************************************************************************/
 
 /****************************************************************************
- * Name: tricore_src_init
- *
- * Description:
- *   Route a service request to a service provider (TOS), a core or for
- *   example the DMA, with a priority and clear a pending request.  The
- *   enable bit is left as it is.
- *
+ * Name: tricore_src_enable
  ****************************************************************************/
 
-static void tricore_src_init(volatile uint32_t *src, int tos, int srpn)
+inline_function
+static void tricore_src_enable(int irq)
 {
-  uint32_t regval = *src;
-
-  regval &= ~(TRICORE_SRC_SRPN_MASK | TRICORE_SRC_TOS_MASK);
-#ifdef TRICORE_SRC_VM_MASK
-  regval &= ~TRICORE_SRC_VM_MASK;
-#endif
+  uintptr_t src = TRICORE_IRQ2SRC(irq);
 
-  regval |= (srpn & TRICORE_SRC_SRPN_MASK) |
-            ((tos << TRICORE_SRC_TOS_SHIFT) & TRICORE_SRC_TOS_MASK);
-  *src = regval | TRICORE_SRC_CLRR;
+  putreg32(getreg32(src) | TRICORE_SRC_SRE, src);
 }
 
-#ifdef CONFIG_ARCH_HAVE_IRQTRIGGER
-/****************************************************************************
- * Name: tricore_gpsrinitialize
- *
- * Description:
- *   Perform gpsr initialization for the CPU
- *
- ****************************************************************************/
-
-static void tricore_gpsrinitialize(void)
-{
-  int cpu = up_cpu_index();
-  int tos = TRICORE_SRC_TOS_CPU(cpu);
-  int i;
-
-  /* Cpux gpsr init */
-
-  for (i = 0; i < CONFIG_NCPUS; i++)
-    {
-      int irq = TRICORE_GPSR_IRQNUM(i, cpu);
-
-      tricore_src_init(TRICORE_IRQ2SRC(irq), tos,
-                       tricore_irq_to_srpn(irq));
-    }
-
-  /* Cpucs gpsr init */
-
-#ifndef CONFIG_ARCH_CHIP_TC3X
-  tricore_src_init(TRICORE_IRQ2SRC(TRICORE_GPSR_IRQNUM(6, cpu)), tos,
-                   tricore_irq_to_srpn(TRICORE_GPSR_IRQNUM(6, cpu)));
-#endif
-}
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -227,6 +182,45 @@ int tricore_srpn_to_irq(int srpn)
   return g_srpn_to_irq[srpn];
 }
 
+#ifdef CONFIG_ARCH_HAVE_IRQTRIGGER
+/****************************************************************************
+ * Name: tricore_gpsrinitialize
+ *
+ * Description:
+ *   Perform gpsr initialization for the CPU
+ *
+ ****************************************************************************/
+
+static inline_function void tricore_gpsrinitialize(void)
+{
+  int cpu = up_cpu_index();
+  int irq;
+  int i;
+
+  /* Cpux gpsr init */
+
+  for (i = 0; i < CONFIG_NCPUS; i++)
+    {
+      irq = TRICORE_GPSR2IRQ(i, cpu);
+
+      tricore_src_init(irq, TRICORE_SRC_TOS_CPU(cpu),
+                       tricore_irq_to_srpn(irq));
+    }
+
+  /* Cpucs gpsr init */
+
+#ifdef CONFIG_HAVE_SECURITY_CORE
+  irq = TRICORE_GPSR2IRQ(6, cpu);
+  tricore_src_init(irq, TRICORE_SRC_TOS_CPU(cpu), tricore_irq_to_srpn(irq));
+#endif
+}
+
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
 /****************************************************************************
  * Name: up_irq_enable
  *
@@ -275,9 +269,7 @@ void up_irqinitialize(void)
 
 void up_disable_irq(int irq)
 {
-  volatile uint32_t *src = TRICORE_IRQ2SRC(irq);
-
-  *src = TRICORE_SRC_TOS_MASK;
+  putreg32(TRICORE_SRC_TOS_MASK, TRICORE_IRQ2SRC(irq));
 }
 
 /****************************************************************************
@@ -290,35 +282,9 @@ void up_disable_irq(int irq)
 
 void up_enable_irq(int irq)
 {
-  volatile uint32_t *src = TRICORE_IRQ2SRC(irq);
-
-  tricore_src_init(src, TRICORE_SRC_TOS_CPU(up_cpu_index()),
+  tricore_src_init(irq, TRICORE_SRC_TOS_CPU(up_cpu_index()),
                    tricore_irq_to_srpn(irq));
-  *src |= TRICORE_SRC_SRE;
-}
-
-/****************************************************************************
- * Name: up_affinity_irq
- *
- * Description:
- *   Set an IRQ affinity by software.
- *
- ****************************************************************************/
-
-void up_affinity_irq(int irq, cpu_set_t cpuset)
-{
-  volatile uint32_t *src = TRICORE_IRQ2SRC(irq);
-  int irq_prio = tricore_irq_to_srpn(irq);
-
-  *src &= ~TRICORE_SRC_SRE;
-
-  /* Only support interrupt routing mode 0,
-   * so routing to the first cpu in cpuset.
-   */
-
-  tricore_src_init(src, TRICORE_SRC_TOS_CPU(ffs(cpuset) - 1),
-                   irq_prio);
-  *src |= TRICORE_SRC_SRE;
+  tricore_src_enable(irq);
 }
 
 /****************************************************************************
@@ -331,7 +297,9 @@ void up_affinity_irq(int irq, cpu_set_t cpuset)
 
 void tricore_ack_irq(int irq)
 {
-  *TRICORE_IRQ2SRC(irq) |= TRICORE_SRC_CLRR;
+  uintptr_t src = TRICORE_IRQ2SRC(irq);
+
+  putreg32(getreg32(src) | TRICORE_SRC_CLRR, src);
 }
 
 #ifdef CONFIG_ARCH_HAVE_IRQTRIGGER
@@ -345,8 +313,10 @@ void tricore_ack_irq(int irq)
 
 void up_trigger_irq(int irq, cpu_set_t cpuset)
 {
-  *(volatile uint32_t *)(TRICORE_INT_SRB_BASE + up_cpu_index() * 4) =
-    cpuset;
+  uintptr_t srb = TRICORE_INT_SRB_BASE +
+                  up_cpu_index() * TRICORE_INT_SRB_SPACING;
+
+  putreg32(cpuset, srb);
 }
 #endif
 
@@ -354,9 +324,9 @@ void up_trigger_irq(int irq, cpu_set_t cpuset)
  * Name: up_prioritize_irq
  *
  * Description:
- *   Set the priority (SRPN) of an IRQ. This is the only way to assign an
- *   SRPN above TRICORE_IRQ_DISABLE_PRIORITY when
- *   CONFIG_ARCH_HIPRI_INTERRUPT is enabled.
+ *   Set the priority (SRPN) of an IRQ.This is the only way to assign an SRPN
+ *   above TRICORE_IRQ_DISABLE_PRIORITY When CONFIG_ARCH_HIPRI_INTERRUPT is
+ *   enabled.
  *
  ****************************************************************************/
 
@@ -401,3 +371,69 @@ int up_prioritize_irq(int irq, int priority)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: up_affinity_irq
+ *
+ * Description:
+ *   Set an IRQ affinity by software.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+void up_affinity_irq(int irq, cpu_set_t cpuset)
+{
+  bool enabled = (getreg32(TRICORE_IRQ2SRC(irq)) & TRICORE_SRC_SRE) != 0;
+  int cpu;
+
+  for (cpu = 0; cpu < CONFIG_NCPUS; cpu++)
+    {
+      if (CPU_ISSET(cpu, &cpuset))
+        {
+          tricore_src_init(irq, TRICORE_SRC_TOS_CPU(cpu),
+                           tricore_irq_to_srpn(irq));
+
+          if (enabled)
+            {
+              tricore_src_enable(irq);
+            }
+
+          return;
+        }
+    }
+}
+#endif
+
+/****************************************************************************
+ * Name: tricore_src_init
+ ****************************************************************************/
+
+void tricore_src_init(int irq, uint8_t tos, uint8_t srpn)
+{
+  uintptr_t src = TRICORE_IRQ2SRC(irq);
+  uint32_t regval = getreg32(src);
+
+  /* Clear VM (virtual machine) and SRE (service request enable). */
+
+  regval &= ~TRICORE_SRC_SRE;
+#ifdef TRICORE_SRC_VM_MASK
+  regval &= ~TRICORE_SRC_VM_MASK;
+#endif
+
+  /* Update tos (target cpu) and srpn (priority) fields. */
+
+  regval &= ~(TRICORE_SRC_TOS_MASK | TRICORE_SRC_SRPN_MASK);
+  regval |= srpn | (tos << TRICORE_SRC_TOS_SHIFT);
+
+  /* Cyber security */
+
+#ifdef CONFIG_TRICORE_CSRM
+  regval |= TRICORE_SRC_CS;
+#endif
+
+  /* Clear pending request */
+
+  regval |= TRICORE_SRC_CLRR;
+
+  putreg32(regval, src);
+}

Reply via email to