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 428e0a3f255472b8f1c7a699896efbe3b8e8e039 Author: zhangyu117 <[email protected]> AuthorDate: Thu Nov 26 11:20:55 2026 +0800 arch/tricore: optimize tls_get() using global registers TriCore provides A8 and A9 as global registers for software use, with write access controlled by PSW.GW. Cache the current task TLS pointer in A8 on context switches so user-space tls_get() can read it directly. Before this patch, mutex_lock took 2754 ns; after it, 354 ns. Signed-off-by: zhangyu117 <[email protected]> --- arch/tricore/include/arch.h | 32 ++++++++++++++++++++++++++++++ arch/tricore/include/irq.h | 3 +++ arch/tricore/src/common/tricore_doirq.c | 4 ++++ arch/tricore/src/common/tricore_internal.h | 12 +++++++++++ arch/tricore/src/common/tricore_svcall.c | 1 + 5 files changed, 52 insertions(+) diff --git a/arch/tricore/include/arch.h b/arch/tricore/include/arch.h index 19bb5896dd5..4f072f06369 100644 --- a/arch/tricore/include/arch.h +++ b/arch/tricore/include/arch.h @@ -52,6 +52,38 @@ #define tricore_addr2csa(addr) ((uintptr_t)(((((uintptr_t)(addr)) & 0xF0000000) >> 12) \ | (((uintptr_t)(addr) & 0x003FFFC0) >> 6))) +/* Read and write global address registers */ + +#ifdef CONFIG_TRICORE_TOOLCHAIN_TASKING +# define read_sysreg(reg) \ + ({ \ + uint32_t __val; \ + __asm__ __volatile__("mov.d\t %0, "#reg"" : "=d" (__val) \ + :: "memory"); \ + __val; \ + }) + +# define write_sysreg(__val, reg) \ + ({ \ + __asm__ __volatile__("mov.a\t "#reg", %0" :: "d" (__val) \ + : "memory"); \ + }) +#else +# define read_sysreg(reg) \ + ({ \ + uint32_t __val; \ + __asm__ __volatile__("mov.d\t %0, %%"#reg"" : "=d" (__val) \ + :: "memory"); \ + __val; \ + }) + +# define write_sysreg(__val, reg) \ + ({ \ + __asm__ __volatile__("mov.a\t %%"#reg", %0" :: "d" (__val) \ + : "memory"); \ + }) +#endif + /* MTCR and MFCR - move to / from Core Special Function Register */ #define tricore_mtcr(reg, val) \ diff --git a/arch/tricore/include/irq.h b/arch/tricore/include/irq.h index 888dd84846a..ee56131bdc1 100644 --- a/arch/tricore/include/irq.h +++ b/arch/tricore/include/irq.h @@ -103,6 +103,7 @@ /* 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_MODE_MASK (3 << PSW_IO) @@ -194,6 +195,8 @@ #define FPU_TRAP_FZE_SHIFT 20 #define FPU_TRAP_TCL_SHIFT 1 +#define up_tls_info() ((struct tls_info_s *)read_sysreg(a8)) + #ifndef __ASSEMBLY__ #ifdef __cplusplus diff --git a/arch/tricore/src/common/tricore_doirq.c b/arch/tricore/src/common/tricore_doirq.c index 8be1e7e94c2..3d8579d1c6f 100644 --- a/arch/tricore/src/common/tricore_doirq.c +++ b/arch/tricore/src/common/tricore_doirq.c @@ -154,6 +154,10 @@ void tricore_irq_sched(void) tcb = this_task(); #endif + /* Update the TLS pointer */ + + tricore_set_tls_info(tcb->stack_alloc_ptr); + /* Record the new "running" task when context switch occurred. * g_running_tasks[] is only used by assertion logic for reporting * crashes. diff --git a/arch/tricore/src/common/tricore_internal.h b/arch/tricore/src/common/tricore_internal.h index 7296f109461..8474e9a76c7 100644 --- a/arch/tricore/src/common/tricore_internal.h +++ b/arch/tricore/src/common/tricore_internal.h @@ -117,6 +117,18 @@ #define tricore_fullcontextrestore() sys_call0(SYS_restore_context) +/* Update tls_info when task switches */ + +#define tricore_set_tls_info(val) \ + do \ + { \ + tricore_mtcr(TRICORE_CPU_PSW, \ + tricore_mfcr(TRICORE_CPU_PSW) | PSW_GW); \ + write_sysreg(val, a8); \ + tricore_mtcr(TRICORE_CPU_PSW, \ + tricore_mfcr(TRICORE_CPU_PSW) & (~PSW_GW)); \ + } while (0) + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/arch/tricore/src/common/tricore_svcall.c b/arch/tricore/src/common/tricore_svcall.c index 1f674e0eb30..6757a207b75 100644 --- a/arch/tricore/src/common/tricore_svcall.c +++ b/arch/tricore/src/common/tricore_svcall.c @@ -125,6 +125,7 @@ void tricore_svcall(volatile void *trap) nxsched_switch_context(*running_task, tcb); case SYS_restore_context: + tricore_set_tls_info(tcb->stack_alloc_ptr); *running_task = tcb; tricore_mtcr(TRICORE_CPU_PCXI, tricore_addr2csa(tcb->xcp.regs)); UP_ISB();
