diff options
Diffstat (limited to 'libcpu')
36 files changed, 1380 insertions, 245 deletions
diff --git a/libcpu/Kconfig b/libcpu/Kconfig index 7f3dfca11f..ccf213e022 100644 --- a/libcpu/Kconfig +++ b/libcpu/Kconfig @@ -24,9 +24,7 @@ config ARCH_USING_HW_ATOMIC_16 config ARCH_CPU_BIG_ENDIAN bool -config ARCH_ARM_BOOTWITH_FLUSH_CACHE - bool - default n +orsource "arm/Kconfig" config ARCH_CPU_STACK_GROWS_UPWARD bool @@ -42,97 +40,6 @@ config ARCH_MM_MMU config ARCH_MM_MPU bool -config ARCH_ARM - bool - -config ARCH_ARM_CORTEX_M - bool - select ARCH_ARM - -config ARCH_ARM_CORTEX_R - bool - select ARCH_ARM - -config ARCH_ARM_CORTEX_FPU - bool - -config ARCH_ARM_CORTEX_SECURE - bool - -config ARCH_ARM_CORTEX_M0 - bool - select ARCH_ARM_CORTEX_M - -config ARCH_ARM_CORTEX_M3 - bool - select ARCH_ARM_CORTEX_M - select RT_USING_CPU_FFS - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_MPU - bool - depends on ARCH_ARM - select ARCH_MM_MPU - -config ARCH_ARM_CORTEX_M4 - bool - select ARCH_ARM_CORTEX_M - select RT_USING_CPU_FFS - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_CORTEX_M7 - bool - select ARCH_ARM_CORTEX_M - select RT_USING_CPU_FFS - select RT_USING_CACHE - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_CORTEX_M85 - bool - select ARCH_ARM_CORTEX_M - select RT_USING_CPU_FFS - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_CORTEX_M23 - bool - select ARCH_ARM_CORTEX_M - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_CORTEX_M33 - bool - select ARCH_ARM_CORTEX_M - select RT_USING_CPU_FFS - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_CORTEX_R - bool - select ARCH_ARM - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - -config ARCH_ARM_CORTEX_R52 - bool - select ARCH_ARM_CORTEX_R - -config ARCH_ARM_MMU - bool - select RT_USING_CACHE - select ARCH_MM_MMU - depends on ARCH_ARM - if RT_USING_SMART config KERNEL_VADDR_START hex "The virtural address of kernel start" @@ -141,87 +48,8 @@ if RT_USING_SMART default 0xffffffc000000000 if ARCH_RISCV && ARCH_REMAP_KERNEL default 0x80000000 if ARCH_RISCV depends on ARCH_MM_MMU - - config RT_IOREMAP_LATE - bool "Support to create IO mapping in the kernel address space after system initlalization." - default n - depends on ARCH_ARM_CORTEX_A - depends on ARCH_MM_MMU endif -config ARCH_ARM_ARM9 - bool - select ARCH_ARM - -config ARCH_ARM_ARM11 - bool - select ARCH_ARM - -config ARCH_ARM_CORTEX_A - bool - select ARCH_ARM - select ARCH_ARM_MMU - select RT_USING_CPU_FFS - select RT_USING_HW_ATOMIC - select ARCH_USING_HW_ATOMIC_8 - select ARCH_USING_HW_ATOMIC_16 - - if ARCH_ARM_CORTEX_A - config RT_SMP_AUTO_BOOT - bool - default n - - config RT_USING_GIC_V2 - bool - default n - - config RT_USING_GIC_V3 - bool - default n - - config RT_NO_USING_GIC - bool - default y if !RT_USING_GIC_V2 && !RT_USING_GIC_V3 - endif - -config ARCH_ARM_CORTEX_A5 - bool - select ARCH_ARM_CORTEX_A - -config ARCH_ARM_CORTEX_A7 - bool - select ARCH_ARM_CORTEX_A - -config ARCH_ARM_CORTEX_A8 - bool - select ARCH_ARM_CORTEX_A - -config ARCH_ARM_CORTEX_A9 - bool - select ARCH_ARM_CORTEX_A - -config ARCH_ARM_CORTEX_A55 - bool - select ARCH_ARM_CORTEX_A - -config ARCH_ARM_SECURE_MODE - bool "Running in secure mode [ARM Cortex-A]" - default n - depends on ARCH_ARM_CORTEX_A - -config RT_BACKTRACE_FUNCTION_NAME - bool "To show function name when backtrace." - default n - depends on ARCH_ARM_CORTEX_A - -config ARCH_ARMV8 - bool - select ARCH_ARM - select ARCH_ARM_MMU - select RT_USING_CPU_FFS - select ARCH_USING_ASID - select ARCH_USING_IRQ_CTX_LIST - config ARCH_MIPS bool @@ -288,6 +116,10 @@ config ARCH_RISCV64 select ARCH_CPU_64BIT bool +config ARCH_RISCV_XUANTIE + select ARCH_RISCV + bool + if ARCH_RISCV orsource "./risc-v/Kconfig" endif @@ -304,6 +136,14 @@ if ARCH_RISCV64 select ARCH_USING_NEW_CTX_SWITCH help Using the common64 implementation under ./libcpu/risc-v + +if RT_USING_SMART + config CONFIG_XUANTIE_SVPBMT + int + depends on ARCH_RISCV_XUANTIE + default 1 +endif + endif config ARCH_REMAP_KERNEL diff --git a/libcpu/arm/Kconfig b/libcpu/arm/Kconfig new file mode 100644 index 0000000000..80a52ec350 --- /dev/null +++ b/libcpu/arm/Kconfig @@ -0,0 +1,176 @@ +config ARCH_ARM_BOOTWITH_FLUSH_CACHE + bool + default n + +config ARCH_ARM + bool + +config ARCH_ARM_CORTEX_M + bool + select ARCH_ARM + +config ARCH_ARM_CORTEX_R + bool + select ARCH_ARM + +config ARCH_ARM_CORTEX_FPU + bool + +config ARCH_ARM_CORTEX_SECURE + bool + +config ARCH_ARM_CORTEX_M0 + bool + select ARCH_ARM_CORTEX_M + +config ARCH_ARM_CORTEX_M3 + bool + select ARCH_ARM_CORTEX_M + select RT_USING_CPU_FFS + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_MPU + bool + depends on ARCH_ARM + select ARCH_MM_MPU + +config ARCH_ARM_CORTEX_M4 + bool + select ARCH_ARM_CORTEX_M + select RT_USING_CPU_FFS + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_CORTEX_M7 + bool + select ARCH_ARM_CORTEX_M + select RT_USING_CPU_FFS + select RT_USING_CACHE + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_CORTEX_M85 + bool + select ARCH_ARM_CORTEX_M + select RT_USING_CPU_FFS + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_CORTEX_M23 + bool + select ARCH_ARM_CORTEX_M + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_CORTEX_M33 + bool + select ARCH_ARM_CORTEX_M + select RT_USING_CPU_FFS + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_CORTEX_R + bool + select ARCH_ARM + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + +config ARCH_ARM_CORTEX_R52 + bool + select ARCH_ARM_CORTEX_R + +config ARCH_ARM_MMU + bool + select RT_USING_CACHE + select ARCH_MM_MMU + depends on ARCH_ARM + +if RT_USING_SMART + config RT_IOREMAP_LATE + bool "Support to create IO mapping in the kernel address space after system initlalization." + default n + depends on ARCH_ARM_CORTEX_A + depends on ARCH_MM_MMU +endif + +config ARCH_ARM_ARM9 + bool + select ARCH_ARM + +config ARCH_ARM_ARM11 + bool + select ARCH_ARM + +config ARCH_ARM_CORTEX_A + bool + select ARCH_ARM + select ARCH_ARM_MMU + select RT_USING_CPU_FFS + select RT_USING_HW_ATOMIC + select ARCH_USING_HW_ATOMIC_8 + select ARCH_USING_HW_ATOMIC_16 + + if ARCH_ARM_CORTEX_A + config RT_SMP_AUTO_BOOT + bool + default n + + config RT_USING_GIC_V2 + bool + default n + + config RT_USING_GIC_V3 + bool + default n + + config RT_NO_USING_GIC + bool + default y if !RT_USING_GIC_V2 && !RT_USING_GIC_V3 + endif + +config ARCH_ARM_CORTEX_A5 + bool + select ARCH_ARM_CORTEX_A + +config ARCH_ARM_CORTEX_A7 + bool + select ARCH_ARM_CORTEX_A + +config ARCH_ARM_CORTEX_A8 + bool + select ARCH_ARM_CORTEX_A + +config ARCH_ARM_CORTEX_A9 + bool + select ARCH_ARM_CORTEX_A + +config ARCH_ARM_CORTEX_A55 + bool + select ARCH_ARM_CORTEX_A + +config ARCH_ARM_SECURE_MODE + bool "Running in secure mode [ARM Cortex-A]" + default n + depends on ARCH_ARM_CORTEX_A + +config RT_BACKTRACE_FUNCTION_NAME + bool "To show function name when backtrace." + default n + depends on ARCH_ARM_CORTEX_A + +config ARCH_ARMV8 + bool + select ARCH_ARM + select ARCH_ARM_MMU + select RT_USING_CPU_FFS + select ARCH_USING_ASID + select ARCH_USING_IRQ_CTX_LIST + diff --git a/libcpu/arm/cortex-a/gic.c b/libcpu/arm/cortex-a/gic.c index c1e050df40..7a15724d94 100644 --- a/libcpu/arm/cortex-a/gic.c +++ b/libcpu/arm/cortex-a/gic.c @@ -19,43 +19,43 @@ struct arm_gic { - rt_uint32_t offset; /* the first interrupt index in the vector table */ + rt_uint32_t offset; /* the first interrupt index in the vector table */ - rt_uint32_t dist_hw_base; /* the base address of the gic distributor */ - rt_uint32_t cpu_hw_base; /* the base addrees of the gic cpu interface */ + rt_uint32_t dist_hw_base; /* the base address of the gic distributor */ + rt_uint32_t cpu_hw_base; /* the base addrees of the gic cpu interface */ }; static struct arm_gic _gic_table[ARM_GIC_MAX_NR]; /** Macro to access the Generic Interrupt Controller Interface (GICC) */ -#define GIC_CPU_CTRL(hw_base) __REG32((hw_base) + 0x00U) /* GICC_CTLR */ -#define GIC_CPU_PRIMASK(hw_base) __REG32((hw_base) + 0x04U) /* GICC_PMR */ -#define GIC_CPU_BINPOINT(hw_base) __REG32((hw_base) + 0x08U) /* GICC_BPR */ -#define GIC_CPU_INTACK(hw_base) __REG32((hw_base) + 0x0cU) /* GICC_IAR */ -#define GIC_CPU_EOI(hw_base) __REG32((hw_base) + 0x10U) /* GICC_EOIR */ -#define GIC_CPU_RUNNINGPRI(hw_base) __REG32((hw_base) + 0x14U) /* GICC_RPR */ -#define GIC_CPU_HIGHPRI(hw_base) __REG32((hw_base) + 0x18U) /* GICC_HPPIR */ -#define GIC_CPU_IIDR(hw_base) __REG32((hw_base) + 0xFCU) /* GICC_IIDR */ +#define GIC_CPU_CTRL(hw_base) __REG32((hw_base) + 0x00U) /* GICC_CTLR */ +#define GIC_CPU_PRIMASK(hw_base) __REG32((hw_base) + 0x04U) /* GICC_PMR */ +#define GIC_CPU_BINPOINT(hw_base) __REG32((hw_base) + 0x08U) /* GICC_BPR */ +#define GIC_CPU_INTACK(hw_base) __REG32((hw_base) + 0x0cU) /* GICC_IAR */ +#define GIC_CPU_EOI(hw_base) __REG32((hw_base) + 0x10U) /* GICC_EOIR */ +#define GIC_CPU_RUNNINGPRI(hw_base) __REG32((hw_base) + 0x14U) /* GICC_RPR */ +#define GIC_CPU_HIGHPRI(hw_base) __REG32((hw_base) + 0x18U) /* GICC_HPPIR */ +#define GIC_CPU_IIDR(hw_base) __REG32((hw_base) + 0xFCU) /* GICC_IIDR */ /** Macro to access the Generic Interrupt Controller Distributor (GICD) */ -#define GIC_DIST_CTRL(hw_base) __REG32((hw_base) + 0x000U) /* GICD_CTLR */ -#define GIC_DIST_TYPE(hw_base) __REG32((hw_base) + 0x004U) /* GICD_TYPER */ -#define GIC_DIST_IGROUP(hw_base, n) __REG32((hw_base) + 0x080U + ((n)/32U) * 4U) /* GICD_IGROUPRn */ -#define GIC_DIST_ENABLE_SET(hw_base, n) __REG32((hw_base) + 0x100U + ((n)/32U) * 4U) /* GICD_ISENABLERn */ -#define GIC_DIST_ENABLE_CLEAR(hw_base, n) __REG32((hw_base) + 0x180U + ((n)/32U) * 4U) /* GICD_ICENABLERn */ -#define GIC_DIST_PENDING_SET(hw_base, n) __REG32((hw_base) + 0x200U + ((n)/32U) * 4U) /* GICD_ISPENDRn */ -#define GIC_DIST_PENDING_CLEAR(hw_base, n) __REG32((hw_base) + 0x280U + ((n)/32U) * 4U) /* GICD_ICPENDRn */ -#define GIC_DIST_ACTIVE_SET(hw_base, n) __REG32((hw_base) + 0x300U + ((n)/32U) * 4U) /* GICD_ISACTIVERn */ -#define GIC_DIST_ACTIVE_CLEAR(hw_base, n) __REG32((hw_base) + 0x380U + ((n)/32U) * 4U) /* GICD_ICACTIVERn */ -#define GIC_DIST_PRI(hw_base, n) __REG32((hw_base) + 0x400U + ((n)/4U) * 4U) /* GICD_IPRIORITYRn */ -#define GIC_DIST_TARGET(hw_base, n) __REG32((hw_base) + 0x800U + ((n)/4U) * 4U) /* GICD_ITARGETSRn */ -#define GIC_DIST_CONFIG(hw_base, n) __REG32((hw_base) + 0xc00U + ((n)/16U) * 4U) /* GICD_ICFGRn */ -#define GIC_DIST_SOFTINT(hw_base) __REG32((hw_base) + 0xf00U) /* GICD_SGIR */ -#define GIC_DIST_CPENDSGI(hw_base, n) __REG32((hw_base) + 0xf10U + ((n)/4U) * 4U) /* GICD_CPENDSGIRn */ -#define GIC_DIST_SPENDSGI(hw_base, n) __REG32((hw_base) + 0xf20U + ((n)/4U) * 4U) /* GICD_SPENDSGIRn */ -#define GIC_DIST_ICPIDR2(hw_base) __REG32((hw_base) + 0xfe8U) /* ICPIDR2 */ +#define GIC_DIST_CTRL(hw_base) __REG32((hw_base) + 0x000U) /* GICD_CTLR */ +#define GIC_DIST_TYPE(hw_base) __REG32((hw_base) + 0x004U) /* GICD_TYPER */ +#define GIC_DIST_IGROUP(hw_base, n) __REG32((hw_base) + 0x080U + ((n) / 32U) * 4U) /* GICD_IGROUPRn */ +#define GIC_DIST_ENABLE_SET(hw_base, n) __REG32((hw_base) + 0x100U + ((n) / 32U) * 4U) /* GICD_ISENABLERn */ +#define GIC_DIST_ENABLE_CLEAR(hw_base, n) __REG32((hw_base) + 0x180U + ((n) / 32U) * 4U) /* GICD_ICENABLERn */ +#define GIC_DIST_PENDING_SET(hw_base, n) __REG32((hw_base) + 0x200U + ((n) / 32U) * 4U) /* GICD_ISPENDRn */ +#define GIC_DIST_PENDING_CLEAR(hw_base, n) __REG32((hw_base) + 0x280U + ((n) / 32U) * 4U) /* GICD_ICPENDRn */ +#define GIC_DIST_ACTIVE_SET(hw_base, n) __REG32((hw_base) + 0x300U + ((n) / 32U) * 4U) /* GICD_ISACTIVERn */ +#define GIC_DIST_ACTIVE_CLEAR(hw_base, n) __REG32((hw_base) + 0x380U + ((n) / 32U) * 4U) /* GICD_ICACTIVERn */ +#define GIC_DIST_PRI(hw_base, n) __REG32((hw_base) + 0x400U + ((n) / 4U) * 4U) /* GICD_IPRIORITYRn */ +#define GIC_DIST_TARGET(hw_base, n) __REG32((hw_base) + 0x800U + ((n) / 4U) * 4U) /* GICD_ITARGETSRn */ +#define GIC_DIST_CONFIG(hw_base, n) __REG32((hw_base) + 0xc00U + ((n) / 16U) * 4U) /* GICD_ICFGRn */ +#define GIC_DIST_SOFTINT(hw_base) __REG32((hw_base) + 0xf00U) /* GICD_SGIR */ +#define GIC_DIST_CPENDSGI(hw_base, n) __REG32((hw_base) + 0xf10U + ((n) / 4U) * 4U) /* GICD_CPENDSGIRn */ +#define GIC_DIST_SPENDSGI(hw_base, n) __REG32((hw_base) + 0xf20U + ((n) / 4U) * 4U) /* GICD_SPENDSGIRn */ +#define GIC_DIST_ICPIDR2(hw_base) __REG32((hw_base) + 0xfe8U) /* ICPIDR2 */ static unsigned int _gic_max_irq; @@ -173,12 +173,12 @@ rt_uint32_t arm_gic_get_pending_irq(rt_uint32_t index, int irq) if (irq >= 16U) { - pend = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32U)) & 0x1UL; /* GICD_ISPENDRn */ + pend = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32U)) & 0x1UL; /* GICD_ISPENDRn */ } else { /* INTID 0-15 Software Generated Interrupt */ - pend = (GIC_DIST_SPENDSGI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4U) * 8U)) & 0xFFUL; /* GICD_SPENDSGIRn */ + pend = (GIC_DIST_SPENDSGI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4U) * 8U)) & 0xFFUL; /* GICD_SPENDSGIRn */ /* No CPU identification offered */ if (pend != 0U) { @@ -240,7 +240,7 @@ void arm_gic_clear_pending_irq(rt_uint32_t index, int irq) } else { - mask = 1U << ((irq % 4U) * 8U); + mask = 1U << ((irq % 4U) * 8U); GIC_DIST_CPENDSGI(_gic_table[index].dist_hw_base, irq) = mask; } } @@ -337,8 +337,8 @@ void arm_gic_set_cpu(rt_uint32_t index, int irq, unsigned int cpumask) old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq); - old_tgt &= ~(0x0FFUL << ((irq % 4U)*8U)); - old_tgt |= cpumask << ((irq % 4U)*8U); + old_tgt &= ~(0x0FFUL << ((irq % 4U) * 8U)); + old_tgt |= cpumask << ((irq % 4U) * 8U); GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt; } @@ -385,7 +385,6 @@ void arm_gic_set_priority(rt_uint32_t index, int irq, rt_uint32_t priority) GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) = mask; } - /** * @brief Get the priority for the specific interrupt * @@ -590,7 +589,7 @@ void arm_gic_set_group(rt_uint32_t index, int irq, rt_uint32_t group) igroupr = GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq); shift = (irq % 32U); igroupr &= (~(1U << shift)); - igroupr |= ( (group & 0x1U) << shift); + igroupr |= ((group & 0x1U) << shift); GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) = igroupr; } @@ -651,9 +650,13 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) * platform maximum. */ if (_gic_max_irq > 1020U) + { _gic_max_irq = 1020U; + } if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */ + { _gic_max_irq = ARM_GIC_NR_IRQS; + } cpumask |= cpumask << 8U; cpumask |= cpumask << 16U; @@ -663,19 +666,27 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) /* Set all global interrupts to be level triggered, active low. */ for (i = 32U; i < _gic_max_irq; i += 16U) + { GIC_DIST_CONFIG(dist_base, i) = 0x0U; + } /* Set all global interrupts to this CPU only. */ for (i = 32U; i < _gic_max_irq; i += 4U) + { GIC_DIST_TARGET(dist_base, i) = cpumask; + } /* Set priority on all interrupts. */ for (i = 0U; i < _gic_max_irq; i += 4U) + { GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0U; + } /* Disable all interrupts. */ for (i = 0U; i < _gic_max_irq; i += 32U) + { GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffffU; + } #if 0 /* All interrupts defaults to IGROUP1(IRQ). */ @@ -683,7 +694,9 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) GIC_DIST_IGROUP(dist_base, i) = 0xffffffff; #endif for (i = 0U; i < _gic_max_irq; i += 32U) + { GIC_DIST_IGROUP(dist_base, i) = 0U; + } /* Enable group0 and group1 interrupt forwarding. */ GIC_DIST_CTRL(dist_base) = 0x01U; @@ -730,8 +743,8 @@ void arm_gic_dump_type(rt_uint32_t index) unsigned int gic_type; gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base); - rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n", - (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4U) & 0xfUL, + rt_kprintf("GICv%u on 0x%08x, max IRQs: %u, %s security extension(%08x)\n", + (unsigned int)((GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4U) & 0xfUL), _gic_table[index].dist_hw_base, _gic_max_irq, gic_type & (1U << 10U) ? "has" : "no", @@ -748,7 +761,7 @@ void arm_gic_dump(rt_uint32_t index) unsigned int i, k; k = GIC_CPU_HIGHPRI(_gic_table[index].cpu_hw_base); - rt_kprintf("--- high pending priority: %d(%08x)\n", k, k); + rt_kprintf("--- high pending priority: %u(%08x)\n", k, k); rt_kprintf("--- hw mask ---\n"); for (i = 0U; i < _gic_max_irq / 32U; i++) { diff --git a/libcpu/arm/cortex-a/gicv3.c b/libcpu/arm/cortex-a/gicv3.c index 7d0b9f28f5..db71d7c4a3 100644 --- a/libcpu/arm/cortex-a/gicv3.c +++ b/libcpu/arm/cortex-a/gicv3.c @@ -311,8 +311,8 @@ void arm_gic_set_system_register_enable_mask(rt_uint32_t index, rt_uint32_t valu value &= 0xFFUL; /* set priority mask */ __set_gicv3_reg(ICC_SRE, value); - __asm__ volatile ("isb 0xF":: - :"memory"); + __asm__ volatile("isb 0xF" :: + : "memory"); } rt_uint32_t arm_gic_get_system_register_enable_mask(rt_uint32_t index) @@ -384,10 +384,10 @@ void arm_gic_send_affinity_sgi(rt_uint32_t index, int irq, rt_uint32_t cpu_mask, sgi_val = (1ULL << 40) | ((irq & 0x0FULL) << 24); //Interrupts routed to all PEs in the system, excluding "self". /* Write the ICC_SGI1R registers */ __asm__ volatile("dsb 0xF" :: - : "memory"); + : "memory"); __set_cp64(15, 0, sgi_val, 12); __asm__ volatile("isb 0xF" :: - : "memory"); + : "memory"); } else { @@ -401,10 +401,10 @@ void arm_gic_send_affinity_sgi(rt_uint32_t index, int irq, rt_uint32_t cpu_mask, ((cluster_id >> 24) & 0xFFull) << GIC_RSGI_AFF3_OFFSET); __asm__ volatile("dsb 0xF" :: - : "memory"); + : "memory"); __set_cp64(15, 0, sgi_val, 12); __asm__ volatile("isb 0xF" :: - : "memory"); + : "memory"); } } } @@ -504,9 +504,13 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) * platform maximum. */ if (_gic_max_irq > 1020U) + { _gic_max_irq = 1020U; + } if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */ + { _gic_max_irq = ARM_GIC_NR_IRQS; + } GIC_DIST_CTRL(dist_base) = 0x0U; /* Wait for register write pending */ @@ -514,7 +518,9 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) /* Set all global interrupts to be level triggered, active low. */ for (i = 32U; i < _gic_max_irq; i += 16U) + { GIC_DIST_CONFIG(dist_base, i) = 0x0U; + } arm_gicv3_wait_rwp(0, 32); @@ -530,7 +536,9 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) /* Set priority on spi interrupts. */ for (i = 32U; i < _gic_max_irq; i += 4U) + { GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0U; + } arm_gicv3_wait_rwp(0, 32); /* Disable all interrupts. */ @@ -543,7 +551,9 @@ int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start) arm_gicv3_wait_rwp(0, 32); /* All interrupts defaults to IGROUP1(IRQ). */ for (i = 0U; i < _gic_max_irq; i += 32U) + { GIC_DIST_IGROUP(dist_base, i) = 0xffffffffU; + } arm_gicv3_wait_rwp(0, 32); @@ -660,8 +670,8 @@ void arm_gic_dump_type(rt_uint32_t index) unsigned int gic_type; gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base); - rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n", - (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4U) & 0xfUL, + rt_kprintf("GICv%u on 0x%08x, max IRQs: %u, %s security extension(%08x)\n", + (unsigned int)((GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4U) & 0xfUL), _gic_table[index].dist_hw_base, _gic_max_irq, gic_type & (1U << 10U) ? "has" : "no", @@ -673,7 +683,7 @@ void arm_gic_dump(rt_uint32_t index) unsigned int i, k; k = arm_gic_get_high_pending_irq(0); - rt_kprintf("--- high pending priority: %d(%08x)\n", k, k); + rt_kprintf("--- high pending priority: %u(%08x)\n", k, k); rt_kprintf("--- hw mask ---\n"); for (i = 0U; i < _gic_max_irq / 32U; i++) { diff --git a/libcpu/arm/cortex-a/gicv3.h b/libcpu/arm/cortex-a/gicv3.h index d379552871..b95f9cbfb4 100644 --- a/libcpu/arm/cortex-a/gicv3.h +++ b/libcpu/arm/cortex-a/gicv3.h @@ -52,7 +52,7 @@ /** Macro to access the Distributor Control Register (GICD_CTLR) */ -#define GICD_CTLR_RWP (1<<31) +#define GICD_CTLR_RWP (1u << 31) #define GICD_CTLR_E1NWF (1<<7) #define GICD_CTLR_DS (1<<6) #define GICD_CTLR_ARE_NS (1<<5) @@ -63,7 +63,7 @@ /** Macro to access the Redistributor Control Register (GICR_CTLR) */ -#define GICR_CTLR_UWP (1<<31) +#define GICR_CTLR_UWP (1u << 31) #define GICR_CTLR_DPG1S (1<<26) #define GICR_CTLR_DPG1NS (1<<25) #define GICR_CTLR_DPG0 (1<<24) diff --git a/libcpu/arm/cortex-m0/context_gcc.S b/libcpu/arm/cortex-m0/context_gcc.S index 59a81bc580..4c65bf7622 100644 --- a/libcpu/arm/cortex-m0/context_gcc.S +++ b/libcpu/arm/cortex-m0/context_gcc.S @@ -173,9 +173,12 @@ rt_hw_context_switch_to: /* restore MSP */ LDR R0, =SCB_VTOR LDR R0, [R0] + CMP R0, #0 + BEQ _restore_msp_done LDR R0, [R0] NOP MSR MSP, R0 +_restore_msp_done: /* enable interrupts at processor level */ CPSIE I @@ -197,7 +200,13 @@ rt_hw_interrupt_thread_switch: .type HardFault_Handler, %function HardFault_Handler: /* get current context */ - MRS R0, PSP /* get fault thread stack pointer */ + MRS R0, MSP /* get fault context from handler. */ + MOV R1, LR /* Cortex-M0 has no Thumb-2 TST immediate */ + MOVS R2, #0x04 + TST R1, R2 /* if(!EXC_RETURN[2]) */ + BEQ _get_sp_done + MRS R0, PSP /* get fault context from thread. */ +_get_sp_done: PUSH {LR} BL rt_hw_hard_fault_exception POP {PC} diff --git a/libcpu/arm/cortex-m0/context_iar.S b/libcpu/arm/cortex-m0/context_iar.S index f00d03417f..114ad82ae8 100644 --- a/libcpu/arm/cortex-m0/context_iar.S +++ b/libcpu/arm/cortex-m0/context_iar.S @@ -179,9 +179,12 @@ rt_hw_context_switch_to: ; restore MSP LDR r0, =SCB_VTOR LDR r0, [r0] + CMP r0, #0 + BEQ _restore_msp_done LDR r0, [r0] NOP MSR msp, r0 +_restore_msp_done ; enable interrupts at processor level CPSIE I @@ -202,7 +205,13 @@ rt_hw_interrupt_thread_switch: HardFault_Handler: ; get current context - MRS r0, psp ; get fault thread stack pointer + MRS r0, msp ; get fault context from handler. + MOV r1, lr ; Cortex-M0 has no Thumb-2 TST immediate + MOVS r2, #0x04 + TST r1, r2 ; if(!EXC_RETURN[2]) + BEQ _get_sp_done + MRS r0, psp ; get fault context from thread. +_get_sp_done PUSH {lr} BL rt_hw_hard_fault_exception POP {pc} diff --git a/libcpu/arm/cortex-m0/context_rvds.S b/libcpu/arm/cortex-m0/context_rvds.S index 3f1a7e267a..31732da834 100644 --- a/libcpu/arm/cortex-m0/context_rvds.S +++ b/libcpu/arm/cortex-m0/context_rvds.S @@ -183,8 +183,11 @@ rt_hw_context_switch_to PROC ; restore MSP LDR r0, =SCB_VTOR LDR r0, [r0] + CMP r0, #0 + BEQ _restore_msp_done LDR r0, [r0] MSR msp, r0 +_restore_msp_done ; enable interrupts at processor level CPSIE I @@ -208,7 +211,13 @@ HardFault_Handler PROC EXPORT HardFault_Handler ; get current context - MRS r0, psp ; get fault thread stack pointer + MRS r0, msp ; get fault context from handler. + MOV r1, lr ; Cortex-M0 has no Thumb-2 TST immediate + MOVS r2, #0x04 + TST r1, r2 ; if(!EXC_RETURN[2]) + BEQ _get_sp_done + MRS r0, psp ; get fault context from thread. +_get_sp_done PUSH {lr} BL rt_hw_hard_fault_exception POP {pc} diff --git a/libcpu/arm/cortex-m23/context_gcc.S b/libcpu/arm/cortex-m23/context_gcc.S index abec76f934..e9aab94670 100644 --- a/libcpu/arm/cortex-m23/context_gcc.S +++ b/libcpu/arm/cortex-m23/context_gcc.S @@ -174,9 +174,12 @@ rt_hw_context_switch_to: /* restore MSP */ LDR R0, =SCB_VTOR LDR R0, [R0] + CMP R0, #0 + BEQ _restore_msp_done LDR R0, [R0] NOP MSR MSP, R0 +_restore_msp_done: /* enable interrupts at processor level */ CPSIE I @@ -198,7 +201,13 @@ rt_hw_interrupt_thread_switch: .type HardFault_Handler, %function HardFault_Handler: /* get current context */ - MRS R0, PSP /* get fault thread stack pointer */ + MRS R0, MSP /* get fault context from handler. */ + MOV R1, LR /* Cortex-M23 baseline has no Thumb-2 TST immediate */ + MOVS R2, #0x04 + TST R1, R2 /* if(!EXC_RETURN[2]) */ + BEQ _get_sp_done + MRS R0, PSP /* get fault context from thread. */ +_get_sp_done: PUSH {LR} BL rt_hw_hard_fault_exception POP {PC} diff --git a/libcpu/arm/cortex-m23/context_iar.S b/libcpu/arm/cortex-m23/context_iar.S index 7600ef3b77..ba58f10aff 100644 --- a/libcpu/arm/cortex-m23/context_iar.S +++ b/libcpu/arm/cortex-m23/context_iar.S @@ -180,9 +180,12 @@ rt_hw_context_switch_to: ; restore MSP LDR r0, =SCB_VTOR LDR r0, [r0] + CMP r0, #0 + BEQ _restore_msp_done LDR r0, [r0] NOP MSR msp, r0 +_restore_msp_done ; enable interrupts at processor level CPSIE I @@ -203,7 +206,13 @@ rt_hw_interrupt_thread_switch: HardFault_Handler: ; get current context - MRS r0, psp ; get fault thread stack pointer + MRS r0, msp ; get fault context from handler. + MOV r1, lr ; Cortex-M23 baseline has no Thumb-2 TST immediate + MOVS r2, #0x04 + TST r1, r2 ; if(!EXC_RETURN[2]) + BEQ _get_sp_done + MRS r0, psp ; get fault context from thread. +_get_sp_done PUSH {lr} BL rt_hw_hard_fault_exception POP {pc} diff --git a/libcpu/arm/cortex-m23/context_rvds.S b/libcpu/arm/cortex-m23/context_rvds.S index 38c3c36f31..7918477073 100644 --- a/libcpu/arm/cortex-m23/context_rvds.S +++ b/libcpu/arm/cortex-m23/context_rvds.S @@ -184,8 +184,11 @@ rt_hw_context_switch_to PROC ; restore MSP LDR r0, =SCB_VTOR LDR r0, [r0] + CMP r0, #0 + BEQ _restore_msp_done LDR r0, [r0] MSR msp, r0 +_restore_msp_done ; enable interrupts at processor level CPSIE I @@ -209,7 +212,13 @@ HardFault_Handler PROC EXPORT HardFault_Handler ; get current context - MRS r0, psp ; get fault thread stack pointer + MRS r0, msp ; get fault context from handler. + MOV r1, lr ; Cortex-M23 baseline has no Thumb-2 TST immediate + MOVS r2, #0x04 + TST r1, r2 ; if(!EXC_RETURN[2]) + BEQ _get_sp_done + MRS r0, psp ; get fault context from thread. +_get_sp_done PUSH {lr} BL rt_hw_hard_fault_exception POP {pc} diff --git a/libcpu/rh850/SConscript b/libcpu/rh850/SConscript new file mode 100644 index 0000000000..38b62b632a --- /dev/null +++ b/libcpu/rh850/SConscript @@ -0,0 +1,14 @@ +# RT-Thread building script for bridge + +import os +from building import * + +Import('rtconfig') + +cwd = GetCurrentDir() +group = SConscript(os.path.join('common', 'SConscript')) + +if os.path.isfile(os.path.join(cwd, rtconfig.CPU, 'SConscript')): + group = group + SConscript(os.path.join(rtconfig.CPU, 'SConscript')) + +Return('group') diff --git a/libcpu/rh850/common/Kconfig b/libcpu/rh850/common/Kconfig new file mode 100644 index 0000000000..99c37c8a7a --- /dev/null +++ b/libcpu/rh850/common/Kconfig @@ -0,0 +1,10 @@ +menu "RT-Thread RH850 CPU" + + config RT_USING_FPU + bool "Using Float Point Unit" + default y + help + Build for the hardware FPU and save its context. + If disabled, the code is built with -msoft-float. + +endmenu diff --git a/libcpu/rh850/common/SConscript b/libcpu/rh850/common/SConscript new file mode 100644 index 0000000000..a70265676b --- /dev/null +++ b/libcpu/rh850/common/SConscript @@ -0,0 +1,13 @@ +# RT-Thread building script for component + +from building import * + +Import('rtconfig') + +cwd = GetCurrentDir() +src = Glob('*.c') + Glob('*_gcc.S') +CPPPATH = [cwd] + +group = DefineGroup('CPU', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/libcpu/rh850/common/context_gcc.S b/libcpu/rh850/common/context_gcc.S new file mode 100644 index 0000000000..7a5086d65d --- /dev/null +++ b/libcpu/rh850/common/context_gcc.S @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#include <rtconfig.h> +#include "rh850_asm.h" +#include "rh850_core.h" + +#if defined(RT_USING_FPU) && RH850_CORE_HAS_SYNCE +#define RH850_SYNCE 1 +#endif +#if defined(RT_USING_FPU) && RH850_CORE_HAS_FPEC +#define RH850_FPEC 1 +#endif + +/* Layout of struct rt_hw_stack_frame */ + .macro SAVE_CONTEXT +#ifdef RH850_SYNCE + /* FPU results and FPI reports are complete before FPSR is read */ + synce +#endif + pushsp r1-r2 + pushsp r4-r31 + add -16, sp + rh850_stsr RH850_SR_EIPC, r10 + st.w r10, 0[sp] + rh850_stsr RH850_SR_EIPSW, r10 + st.w r10, 4[sp] +#ifdef RT_USING_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, 8[sp] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, 12[sp] +#endif + .endm + +/* While FPEC.FPIVD is 1, keep FPEPC and the FPSR cause bits */ + .macro RESTORE_FPU + ld.w 8[sp], r10 +#ifdef RH850_FPEC + rh850_stsr RH850_SR_FPEC, r11 + andi RH850_FPEC_FPIVD, r11, r11 + bz .Lfpu_frame\@ + rh850_stsr RH850_SR_FPSR, r11 + andi RH850_FPSR_XC_MASK, r11, r11 + andi RH850_FPSR_XC_MASK, r10, r12 + xor r12, r10 + or r11, r10 + ldsr r10, RH850_SR_FPSR + br .Lfpu_done\@ +.Lfpu_frame\@: +#endif + ldsr r10, RH850_SR_FPSR + ld.w 12[sp], r10 + ldsr r10, RH850_SR_FPEPC +.Lfpu_done\@: + .endm + + .macro RESTORE_CONTEXT +#ifdef RT_USING_FPU + RESTORE_FPU +#endif + ld.w 4[sp], r10 + ldsr r10, RH850_SR_EIPSW + ld.w 0[sp], r10 + ldsr r10, RH850_SR_EIPC + addi 16, sp, sp + popsp r4-r31 + popsp r1-r2 + eiret + .endm + + .text + + .globl C_SYM(rt_hw_interrupt_disable) + .type C_SYM(rt_hw_interrupt_disable), @function +C_SYM(rt_hw_interrupt_disable): + stsr 5, r10, 0 + di + jmp [lp] + + .globl C_SYM(rt_hw_interrupt_enable) + .type C_SYM(rt_hw_interrupt_enable), @function +C_SYM(rt_hw_interrupt_enable): + andi RH850_PSW_ID, r6, r10 + bnz 1f + ei +1: + jmp [lp] + +/* EP = 1, so that EIRET leaves ISPR as it is */ + .globl C_SYM(rt_hw_context_switch_to) + .type C_SYM(rt_hw_context_switch_to), @function +C_SYM(rt_hw_context_switch_to): + rh850_stsr RH850_SR_PSW, r10 + ori RH850_PSW_EP | RH850_PSW_ID, r10, r10 + ldsr r10, RH850_SR_PSW + ld.w 0[r6], sp + RESTORE_CONTEXT + +/* PSW.ID is 1 here, so a pending FPI of this thread is reported as a fault */ + .globl C_SYM(rt_hw_context_switch) + .type C_SYM(rt_hw_context_switch), @function +C_SYM(rt_hw_context_switch): +#ifdef RH850_FPEC + synce + rh850_stsr RH850_SR_FPEC, r10 + andi RH850_FPEC_FPIVD, r10, r10 + bz 1f + jr C_SYM(rh850_fpi_pending) +1: +#endif + trap 0 + jmp [lp] + + .globl rh850_trap0_entry +rh850_trap0_entry: + SAVE_CONTEXT + st.w sp, 0[r6] + ld.w 0[r7], sp + RESTORE_CONTEXT + +/* Return state and the GCC call-used registers of a nested interrupt */ + .macro SAVE_NESTED +#ifdef RH850_SYNCE + synce +#endif + pushsp r1-r2 + pushsp r5-r19 + pushsp r30-r31 + add -16, sp + rh850_stsr RH850_SR_EIPC, r10 + st.w r10, 0[sp] + rh850_stsr RH850_SR_EIPSW, r10 + st.w r10, 4[sp] +#ifdef RT_USING_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, 8[sp] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, 12[sp] +#endif + .endm + + .macro RESTORE_NESTED +#ifdef RT_USING_FPU + RESTORE_FPU +#endif + ld.w 4[sp], r10 + ldsr r10, RH850_SR_EIPSW + ld.w 0[sp], r10 + ldsr r10, RH850_SR_EIPC + addi 16, sp, sp + popsp r30-r31 + popsp r5-r19 + popsp r1-r2 + eiret + .endm + + .globl rh850_eiint_entry +rh850_eiint_entry: + addi -8, sp, sp + st.w r10, 0[sp] + st.w r11, 4[sp] + mov hilo(C_SYM(rh850_irq_nest)), r10 + ld.w 0[r10], r11 + cmp r0, r11 + ld.w 0[sp], r10 + ld.w 4[sp], r11 + /* movea keeps the flags of cmp */ + movea 8, sp, sp + bne 1f + + SAVE_CONTEXT + mov hilo(C_SYM(rh850_thread_sp)), r10 + st.w sp, 0[r10] + mov hilo(__stack_top), sp + br 2f + +1: + SAVE_NESTED + +2: + mov hilo(C_SYM(rh850_irq_nest)), r10 + ld.w 0[r10], r11 + add 1, r11 + st.w r11, 0[r10] + /* EIIC is read before EI: a nested interrupt overwrites it */ + rh850_stsr RH850_SR_EIIC, r6 + jarl C_SYM(rh850_irq_handle), lp + mov hilo(C_SYM(rh850_irq_nest)), r10 + ld.w 0[r10], r11 + add -1, r11 + st.w r11, 0[r10] + cmp r0, r11 + bne 3f + + mov hilo(C_SYM(rt_thread_switch_interrupt_flag)), r10 + ld.w 0[r10], r11 + cmp r0, r11 + be 4f +#ifdef RH850_FPEC + /* A pending FPI is taken after EIRET: keep the switch for a later exit */ + rh850_stsr RH850_SR_FPEC, r11 + andi RH850_FPEC_FPIVD, r11, r11 + bnz 4f +#endif + st.w r0, 0[r10] + mov hilo(C_SYM(rh850_thread_sp)), r10 + ld.w 0[r10], r11 + mov hilo(C_SYM(rt_interrupt_from_thread)), r10 + ld.w 0[r10], r10 + st.w r11, 0[r10] + mov hilo(C_SYM(rt_interrupt_to_thread)), r10 + ld.w 0[r10], r10 + ld.w 0[r10], sp + /* EIRET with EP = 0 ends the interrupt in ISPR */ + RESTORE_CONTEXT + +4: + mov hilo(C_SYM(rh850_thread_sp)), r10 + ld.w 0[r10], sp + RESTORE_CONTEXT + +3: + RESTORE_NESTED diff --git a/libcpu/rh850/common/cpuport.c b/libcpu/rh850/common/cpuport.c new file mode 100644 index 0000000000..6f4c0859c1 --- /dev/null +++ b/libcpu/rh850/common/cpuport.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#include <rthw.h> +#include <rtthread.h> +#include "rh850_cpu.h" +#include "rh850_core.h" + +#ifdef RT_USING_FPU +#define THREAD_PSW (RH850_PSW_EBV | RH850_PSW_CU0) +#else +#define THREAD_PSW RH850_PSW_EBV +#endif + +volatile rt_ubase_t rt_interrupt_from_thread; +volatile rt_ubase_t rt_interrupt_to_thread; +volatile rt_ubase_t rt_thread_switch_interrupt_flag; + +rt_uint32_t rh850_irq_nest; +rt_ubase_t rh850_thread_sp; + +rt_weak int rt_hw_cpu_id(void) +{ + return 0; +} + +rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, + rt_uint8_t *stack_addr, void *texit) +{ + struct rt_hw_stack_frame *frame; + rt_ubase_t stk; + + stk = RT_ALIGN_DOWN((rt_ubase_t)stack_addr + sizeof(rt_ubase_t), 8); + frame = (struct rt_hw_stack_frame *)(stk - sizeof(*frame)); + rt_memset(frame, 0, sizeof(*frame)); + + frame->eipc = (rt_ubase_t)tentry; + frame->eipsw = THREAD_PSW; +#ifdef RT_USING_FPU + frame->fpsr = RH850_FPSR_FS; +#endif + frame->FRAME_GPR(6) = (rt_ubase_t)parameter; + frame->FRAME_GPR(31) = (rt_ubase_t)texit; + + return (rt_uint8_t *)frame; +} + +void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, + rt_thread_t from_thread, + rt_thread_t to_thread) +{ + RT_UNUSED(from_thread); + RT_UNUSED(to_thread); + + if (rt_thread_switch_interrupt_flag == 0) + { + rt_interrupt_from_thread = from; + rt_thread_switch_interrupt_flag = 1; + } + rt_interrupt_to_thread = to; +} + +void rh850_irq_handle(rt_uint32_t eiic) +{ + rt_interrupt_enter(); + rh850_ei(); + rt_hw_interrupt_dispatch(eiic); + rh850_di(); + rt_interrupt_leave(); +} diff --git a/libcpu/rh850/common/rh850_asm.h b/libcpu/rh850/common/rh850_asm.h new file mode 100644 index 0000000000..97ff5b0212 --- /dev/null +++ b/libcpu/rh850/common/rh850_asm.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#ifndef __RH850_ASM_H__ +#define __RH850_ASM_H__ + +#include "rh850_sysreg.h" + +/* GCC v850 puts __USER_LABEL_PREFIX__ ("_") in front of C symbols */ +#define RH850_CAT2(a, b) a##b +#define RH850_CAT(a, b) RH850_CAT2(a, b) +#define C_SYM(s) RH850_CAT(__USER_LABEL_PREFIX__, s) + +/* clang-format off */ +#ifdef __ASSEMBLER__ + .macro rh850_stsr id, sel, reg + stsr \id, \reg, \sel + .endm +#endif +/* clang-format on */ + +#endif diff --git a/libcpu/rh850/common/rh850_cpu.h b/libcpu/rh850/common/rh850_cpu.h new file mode 100644 index 0000000000..546204b2c0 --- /dev/null +++ b/libcpu/rh850/common/rh850_cpu.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#ifndef __RH850_CPU_H__ +#define __RH850_CPU_H__ + +#include <rtdef.h> +#include "rh850_sysreg.h" + +#define rh850_stsr_(id, sel) \ + ({ \ + rt_uint32_t v_; \ + __asm__ volatile("stsr %1, %0, %2" : "=r"(v_) : "i"(id), "i"(sel)); \ + v_; \ + }) +#define RH850_STSR(sr) rh850_stsr_(sr) + +rt_inline void rh850_di(void) +{ + __asm__ volatile("di" ::: "memory"); +} + +rt_inline void rh850_ei(void) +{ + __asm__ volatile("ei" ::: "memory"); +} + +rt_inline void rh850_syncp(void) +{ + __asm__ volatile("syncp" ::: "memory"); +} + +rt_inline void rh850_halt(void) +{ + __asm__ volatile("halt" ::: "memory"); +} + +/* Layout of SAVE_CONTEXT in context_gcc.S and of the fault entries */ +struct rt_hw_stack_frame +{ + rt_ubase_t eipc; + rt_ubase_t eipsw; + rt_ubase_t fpsr; + rt_ubase_t fpepc; + rt_ubase_t r31_r4[28]; + rt_ubase_t r2; + rt_ubase_t r1; +}; + +#define FRAME_GPR(n) r31_r4[31 - (n)] + +extern volatile rt_ubase_t rt_interrupt_from_thread; +extern volatile rt_ubase_t rt_interrupt_to_thread; +extern volatile rt_ubase_t rt_thread_switch_interrupt_flag; + +void rt_hw_interrupt_dispatch(rt_uint32_t eiic); + +#endif diff --git a/libcpu/rh850/common/rh850_sysreg.h b/libcpu/rh850/common/rh850_sysreg.h new file mode 100644 index 0000000000..d23bc4d980 --- /dev/null +++ b/libcpu/rh850/common/rh850_sysreg.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#ifndef __RH850_SYSREG_H__ +#define __RH850_SYSREG_H__ + +#define RH850_SR_EIPC 0, 0 +#define RH850_SR_EIPSW 1, 0 +#define RH850_SR_FEPC 2, 0 +#define RH850_SR_FEPSW 3, 0 +#define RH850_SR_PSW 5, 0 +#define RH850_SR_FPSR 6, 0 +#define RH850_SR_FPEPC 7, 0 +#define RH850_SR_FPST 8, 0 +#define RH850_SR_FPCC 9, 0 +#define RH850_SR_EIIC 13, 0 +#define RH850_SR_FEIC 14, 0 +#define RH850_SR_CTPC 16, 0 +#define RH850_SR_EIWR 28, 0 +#define RH850_SR_FEWR 29, 0 +#define RH850_SR_EBASE 3, 1 +#define RH850_SR_INTBP 4, 1 + +#define RH850_PSW_ID 0x00000020 +#define RH850_PSW_EP 0x00000040 +#define RH850_PSW_EBV 0x00008000 +#define RH850_PSW_CU0 0x00010000 + +#endif diff --git a/libcpu/rh850/common/trap.c b/libcpu/rh850/common/trap.c new file mode 100644 index 0000000000..5a6a3c0cf2 --- /dev/null +++ b/libcpu/rh850/common/trap.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#include <stdarg.h> +#include <rthw.h> +#include <rtthread.h> +#include "rh850_cpu.h" + +static rt_err_t (*rh850_exception_hook)(void *context); + +void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context)) +{ + rh850_exception_hook = exception_handle; +} + +static void fault_print(const char *fmt, ...) +{ + char buf[96]; + va_list args; + + va_start(args, fmt); + rt_vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + rt_hw_console_output(buf); +} + +/* While a switch from an interrupt is held back, "from" still runs */ +static rt_thread_t running_thread(void) +{ + if (rt_thread_switch_interrupt_flag != 0) + { + return rt_container_of((rt_ubase_t *)rt_interrupt_from_thread, + struct rt_thread, sp); + } + + return rt_thread_self(); +} + +static void __attribute__((noreturn)) fault_halt(void *context) +{ + if (rh850_exception_hook != RT_NULL) + { + rh850_exception_hook(context); + } + + for (;;) + { + rh850_halt(); + } +} + +void rh850_fault(rt_uint32_t fe, rt_uint32_t cause, + struct rt_hw_stack_frame *frame) +{ + rt_thread_t thread = running_thread(); + int n; + + fault_print("\n%s exception: cause 0x%08x, thread %s\n", fe ? "FE" : "EI", + cause, thread ? thread->parent.name : "-"); + fault_print("pc 0x%08x psw 0x%08x sp 0x%08x\n", frame->eipc, + frame->eipsw, (rt_ubase_t)(frame + 1)); + fault_print("fpsr 0x%08x fpepc 0x%08x\n", frame->fpsr, frame->fpepc); + fault_print("r1 0x%08x r2 0x%08x\n", frame->r1, frame->r2); + for (n = 4; n <= 31; n += 4) + { + fault_print("r%-2d 0x%08x r%-2d 0x%08x r%-2d 0x%08x r%-2d 0x%08x\n", + n, frame->FRAME_GPR(n), n + 1, frame->FRAME_GPR(n + 1), + n + 2, frame->FRAME_GPR(n + 2), n + 3, + frame->FRAME_GPR(n + 3)); + } + + fault_halt(frame); +} + +void rh850_fpi_pending(rt_ubase_t from) +{ + rt_thread_t thread = + rt_container_of((rt_ubase_t *)from, struct rt_thread, sp); + + fault_print("\nFPI pending at a thread switch: thread %s, fpepc 0x%08x\n", + thread->parent.name, RH850_STSR(RH850_SR_FPEPC)); + + fault_halt(RT_NULL); +} diff --git a/libcpu/rh850/common/vector_gcc.S b/libcpu/rh850/common/vector_gcc.S new file mode 100644 index 0000000000..88f235401d --- /dev/null +++ b/libcpu/rh850/common/vector_gcc.S @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#include <rtconfig.h> +#include "rh850_asm.h" + + .macro vector target + .balign 16 + jr \target + .endm + + .section .vectors, "ax" + .balign 512 + .globl rh850_vectors +rh850_vectors: + vector __start /* 000H RESET */ + vector rh850_fe_fault /* 010H SYSERR */ + vector rh850_fe_fault /* 020H reserved */ + vector rh850_fe_fault /* 030H FETRAP */ + vector rh850_trap0_entry /* 040H TRAP0 */ + vector rh850_ei_fault /* 050H TRAP1 */ + vector rh850_fe_fault /* 060H RIE */ + vector rh850_ei_fault /* 070H FPE, FPI */ + vector rh850_fe_fault /* 080H UCPOP */ + vector rh850_fe_fault /* 090H MIP, MDP */ + vector rh850_fe_fault /* 0A0H PIE */ + vector rh850_fe_fault /* 0B0H reserved */ + vector rh850_fe_fault /* 0C0H MAE */ + vector rh850_fe_fault /* 0D0H reserved */ + vector rh850_fe_fault /* 0E0H FENMI */ + vector rh850_fe_fault /* 0F0H FEINT */ + .rept 16 + vector rh850_eiint_entry /* 100H-1F0H EIINT priority 0-15 */ + .endr + + .text + + .macro FAULT_FRAME pcid, pcsel, pswid, pswsel + pushsp r1-r2 + pushsp r4-r31 + add -16, sp + rh850_stsr \pcid, \pcsel, r10 + st.w r10, 0[sp] + rh850_stsr \pswid, \pswsel, r10 + st.w r10, 4[sp] +#ifdef RT_USING_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, 8[sp] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, 12[sp] +#else + st.w r0, 8[sp] + st.w r0, 12[sp] +#endif + mov sp, r8 + .endm + +rh850_fe_fault: + FAULT_FRAME RH850_SR_FEPC, RH850_SR_FEPSW + mov 1, r6 + rh850_stsr RH850_SR_FEIC, r7 + jr C_SYM(rh850_fault) + +rh850_ei_fault: + FAULT_FRAME RH850_SR_EIPC, RH850_SR_EIPSW + mov 0, r6 + rh850_stsr RH850_SR_EIIC, r7 + jr C_SYM(rh850_fault) diff --git a/libcpu/rh850/g4kh/SConscript b/libcpu/rh850/g4kh/SConscript new file mode 100644 index 0000000000..207ce6cf0f --- /dev/null +++ b/libcpu/rh850/g4kh/SConscript @@ -0,0 +1,13 @@ +# RT-Thread building script for component + +from building import * + +Import('rtconfig') + +cwd = GetCurrentDir() +src = [] +CPPPATH = [cwd] + +group = DefineGroup('CPU', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/libcpu/rh850/g4kh/rh850_core.h b/libcpu/rh850/g4kh/rh850_core.h new file mode 100644 index 0000000000..c8fd9cabe0 --- /dev/null +++ b/libcpu/rh850/g4kh/rh850_core.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#ifndef __RH850_CORE_G4KH_H__ +#define __RH850_CORE_G4KH_H__ + +#include "rh850_sysreg.h" + +#define RH850_CORE_HAS_FPU 1 +#define RH850_CORE_HAS_SYNCE 1 +#define RH850_CORE_HAS_FPEC 1 + +#define RH850_FPSR_FS 0x00020000 +#define RH850_FPSR_RM_SHIFT 18 +#define RH850_FPSR_RM_MASK 0x000c0000 +#define RH850_FPSR_XC_MASK 0x0000fc00 + +#define RH850_SR_FPEC 11, 0 +#define RH850_FPEC_FPIVD 0x00000001 + +#define RH850_SR_MEA 6, 2 +#define RH850_SR_MEI 8, 2 +#define RH850_SR_RBIP 18, 2 + +#define RH850_CORE_MPU_ENTRIES 24 +#define RH850_SR_MCA 8, 5 +#define RH850_SR_MCS 9, 5 +#define RH850_SR_MCR 11, 5 +#define RH850_SR_MCI 12, 5 +#define RH850_SR_MPIDX 16, 5 +#define RH850_SR_MPLA 20, 5 +#define RH850_SR_MPUA 21, 5 +#define RH850_SR_MPAT 22, 5 +#define RH850_SR_MPID0 24, 5 +#define RH850_SR_MPID1 25, 5 +#define RH850_SR_MPID2 26, 5 +#define RH850_SR_MPID3 27, 5 +#define RH850_SR_MPID4 28, 5 +#define RH850_SR_MPID5 29, 5 +#define RH850_SR_MPID6 30, 5 +#define RH850_SR_MPID7 31, 5 + +#define RH850_SR_ICTAGL 16, 4 +#define RH850_SR_ICTAGH 17, 4 +#define RH850_SR_ICDATL 18, 4 +#define RH850_SR_ICDATH 19, 4 +#define RH850_SR_ICERR 28, 4 + +#endif diff --git a/libcpu/rh850/g4kh/rh850_core.inc b/libcpu/rh850/g4kh/rh850_core.inc new file mode 100644 index 0000000000..c809bd651b --- /dev/null +++ b/libcpu/rh850/g4kh/rh850_core.inc @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2026, Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-06 Parthiban first version + */ + +#include "rh850_core.h" + +/* clang-format off */ + + .macro rh850_core_clear_error_regs + ldsr r0, RH850_SR_MEA + ldsr r0, RH850_SR_MEI + ldsr r0, RH850_SR_RBIP + .endm + +/* PSW.CU0 must already be in effect */ + .macro rh850_core_init_fpu tmp + ldsr r0, RH850_SR_FPST + ldsr r0, RH850_SR_FPCC + mov hilo(RH850_FPSR_FS), \tmp + ldsr \tmp, RH850_SR_FPSR + ldsr r0, RH850_SR_FPEPC + .endm + + .macro rh850_core_clear_mpu idx, end + ldsr r0, RH850_SR_MCA + ldsr r0, RH850_SR_MCS + ldsr r0, RH850_SR_MCR + ldsr r0, RH850_SR_MCI + mov r0, \idx + movea RH850_CORE_MPU_ENTRIES, r0, \end +1: + ldsr \idx, RH850_SR_MPIDX + ldsr r0, RH850_SR_MPLA + ldsr r0, RH850_SR_MPUA + ldsr r0, RH850_SR_MPAT + add 1, \idx + cmp \end, \idx + bl 1b + ldsr r0, RH850_SR_MPIDX + ldsr r0, RH850_SR_MPID0 + ldsr r0, RH850_SR_MPID1 + ldsr r0, RH850_SR_MPID2 + ldsr r0, RH850_SR_MPID3 + ldsr r0, RH850_SR_MPID4 + ldsr r0, RH850_SR_MPID5 + ldsr r0, RH850_SR_MPID6 + ldsr r0, RH850_SR_MPID7 + .endm + + .macro rh850_core_clear_icache + ldsr r0, RH850_SR_ICTAGL + ldsr r0, RH850_SR_ICTAGH + ldsr r0, RH850_SR_ICDATL + ldsr r0, RH850_SR_ICDATH + ldsr r0, RH850_SR_ICERR + .endm diff --git a/libcpu/risc-v/common/context_gcc.S b/libcpu/risc-v/common/context_gcc.S index e0533c942c..e637172838 100644 --- a/libcpu/risc-v/common/context_gcc.S +++ b/libcpu/risc-v/common/context_gcc.S @@ -48,11 +48,54 @@ li s0, (SR_PP) or s1, s1, s0 SAVE_REG s1, REG_IDX(XSTATUS) + +#ifdef ARCH_RISCV_FPU + /* Save the callee-saved FP registers and fcsr. */ + li t0, SR_FS + csrs CSR_STATUS, t0 + addi t1, sp, RT_HW_SWITCH_CONTEXT_FPU_BASE * SZREG + CTX_FREG_S f8, FPU_CTX_SW_FS0_OFF(t1) + CTX_FREG_S f9, FPU_CTX_SW_FS1_OFF(t1) + CTX_FREG_S f18, FPU_CTX_SW_FS2_OFF(t1) + CTX_FREG_S f19, FPU_CTX_SW_FS3_OFF(t1) + CTX_FREG_S f20, FPU_CTX_SW_FS4_OFF(t1) + CTX_FREG_S f21, FPU_CTX_SW_FS5_OFF(t1) + CTX_FREG_S f22, FPU_CTX_SW_FS6_OFF(t1) + CTX_FREG_S f23, FPU_CTX_SW_FS7_OFF(t1) + CTX_FREG_S f24, FPU_CTX_SW_FS8_OFF(t1) + CTX_FREG_S f25, FPU_CTX_SW_FS9_OFF(t1) + CTX_FREG_S f26, FPU_CTX_SW_FS10_OFF(t1) + CTX_FREG_S f27, FPU_CTX_SW_FS11_OFF(t1) + csrr t0, CSR_FCSR + REG_S t0, RT_HW_SWITCH_CONTEXT_FCSR * SZREG(sp) +#endif /* ARCH_RISCV_FPU */ .endm .macro RESTORE_CONTEXT LOAD_REG s1, REG_IDX(XSTATUS) csrw CSR_STATUS, s1 + +#ifdef ARCH_RISCV_FPU + /* Restore the callee-saved FP registers and fcsr. */ + li t0, SR_FS + csrs CSR_STATUS, t0 + addi t1, sp, RT_HW_SWITCH_CONTEXT_FPU_BASE * SZREG + CTX_FREG_L f8, FPU_CTX_SW_FS0_OFF(t1) + CTX_FREG_L f9, FPU_CTX_SW_FS1_OFF(t1) + CTX_FREG_L f18, FPU_CTX_SW_FS2_OFF(t1) + CTX_FREG_L f19, FPU_CTX_SW_FS3_OFF(t1) + CTX_FREG_L f20, FPU_CTX_SW_FS4_OFF(t1) + CTX_FREG_L f21, FPU_CTX_SW_FS5_OFF(t1) + CTX_FREG_L f22, FPU_CTX_SW_FS6_OFF(t1) + CTX_FREG_L f23, FPU_CTX_SW_FS7_OFF(t1) + CTX_FREG_L f24, FPU_CTX_SW_FS8_OFF(t1) + CTX_FREG_L f25, FPU_CTX_SW_FS9_OFF(t1) + CTX_FREG_L f26, FPU_CTX_SW_FS10_OFF(t1) + CTX_FREG_L f27, FPU_CTX_SW_FS11_OFF(t1) + REG_L t0, RT_HW_SWITCH_CONTEXT_FCSR * SZREG(sp) + csrw CSR_FCSR, t0 +#endif /* ARCH_RISCV_FPU */ + #ifndef ARCH_RISCV_32E LOAD_REG s11, REG_IDX(S11) LOAD_REG s10, REG_IDX(S10) @@ -183,6 +226,13 @@ rt_hw_context_switch_interrupt: li TMP, (K_XSTATUS_DEFAULT & ~SR_PIE) REG_S TMP, REG_IDX(XSTATUS) * SZREG(sp) REG_S ra, REG_IDX(RA) * SZREG(sp) +#ifdef ARCH_RISCV_FPU + /* + * The interrupted thread's FP state is in its trap frame and is restored + * by RESTORE_ALL. Use a sane fcsr until then. + */ + REG_S zero, RT_HW_SWITCH_CONTEXT_FCSR * SZREG(sp) +#endif /* ARCH_RISCV_FPU */ /* Save SP of from-thread */ mv a0, sp diff --git a/libcpu/risc-v/common/include/asm.h b/libcpu/risc-v/common/include/asm.h index 500247a57e..616e5335bc 100644 --- a/libcpu/risc-v/common/include/asm.h +++ b/libcpu/risc-v/common/include/asm.h @@ -20,6 +20,7 @@ #include <csr.h> #include <stack.h> +/* clang-format off */ #ifdef __GNUC__ .macro LOAD_GLOBAL_POINTER .option push @@ -55,9 +56,13 @@ .endm .macro SAVE_ALL +#if CTX_FRAME_PAD_NR > 0 + /* Keep the trap frame 16-byte aligned */ + addi sp, sp, -CTX_FRAME_PAD_NR * SZREG +#endif #ifdef ARCH_RISCV_FPU - /* Reserve float registers */ - addi sp, sp, -CTX_FPU_REG_NR * SZREG + /* Reserve float registers and fcsr */ + addi sp, sp, -(CTX_FPU_REG_NR + CTX_FCSR_REG_NR) * SZREG #endif #ifdef ARCH_RISCV_VECTOR /* Reserve float registers */ @@ -151,6 +156,9 @@ CTX_FREG_S f30, FPU_CTX_F30_OFF(t1) CTX_FREG_S f31, FPU_CTX_F31_OFF(t1) + csrr t2, CSR_FCSR + REG_S t2, FPU_CTX_FCSR_OFF(t1) + /* Clean FS domain */ csrc CSR_STATUS, t0 @@ -166,7 +174,7 @@ beqz t0, 0f /* push vector frame */ - addi t1, sp, (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR) * SZREG + addi t1, sp, (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR + CTX_FCSR_REG_NR) * SZREG SAVE_VECTOR t1 0: @@ -195,7 +203,7 @@ beqz t0, 0f /* Restore vector frame */ - addi t1, sp, (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR) * SZREG + addi t1, sp, (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR + CTX_FCSR_REG_NR) * SZREG RESTORE_VECTOR t1 0: @@ -240,6 +248,9 @@ CTX_FREG_L f30, FPU_CTX_F30_OFF(t2) CTX_FREG_L f31, FPU_CTX_F31_OFF(t2) + REG_L t1, FPU_CTX_FCSR_OFF(t2) + csrw CSR_FCSR, t1 + /* Clean FS domain */ csrc CSR_STATUS, t0 diff --git a/libcpu/risc-v/common/include/context_ext.h b/libcpu/risc-v/common/include/context_ext.h index 2c2676e2b6..1ed13d3446 100644 --- a/libcpu/risc-v/common/include/context_ext.h +++ b/libcpu/risc-v/common/include/context_ext.h @@ -19,8 +19,14 @@ #endif /* Number of XLEN-sized stack slots occupied by 32 floating-point registers. */ #define CTX_FPU_REG_NR (32 * __riscv_flen / __riscv_xlen) +/* Slots needed for the callee-saved fs0-fs11 in the switch frame. */ +#define CTX_FPU_SWITCH_REG_NR (CTX_FPU_REG_NR * 12 / 32) +/* The fcsr is always one XLEN-sized slot. */ +#define CTX_FCSR_REG_NR 1 #else #define CTX_FPU_REG_NR 0 +#define CTX_FPU_SWITCH_REG_NR 0 +#define CTX_FCSR_REG_NR 0 #endif #ifdef __ASSEMBLY__ @@ -66,6 +72,23 @@ #define FPU_CTX_F30_OFF FPU_CTX_REG_OFF(30) #define FPU_CTX_F31_OFF FPU_CTX_REG_OFF(31) +/* Compact slots for the callee-saved fs0-fs11 in the switch frame. */ +#define FPU_CTX_SW_FS0_OFF FPU_CTX_REG_OFF(0) +#define FPU_CTX_SW_FS1_OFF FPU_CTX_REG_OFF(1) +#define FPU_CTX_SW_FS2_OFF FPU_CTX_REG_OFF(2) +#define FPU_CTX_SW_FS3_OFF FPU_CTX_REG_OFF(3) +#define FPU_CTX_SW_FS4_OFF FPU_CTX_REG_OFF(4) +#define FPU_CTX_SW_FS5_OFF FPU_CTX_REG_OFF(5) +#define FPU_CTX_SW_FS6_OFF FPU_CTX_REG_OFF(6) +#define FPU_CTX_SW_FS7_OFF FPU_CTX_REG_OFF(7) +#define FPU_CTX_SW_FS8_OFF FPU_CTX_REG_OFF(8) +#define FPU_CTX_SW_FS9_OFF FPU_CTX_REG_OFF(9) +#define FPU_CTX_SW_FS10_OFF FPU_CTX_REG_OFF(10) +#define FPU_CTX_SW_FS11_OFF FPU_CTX_REG_OFF(11) + +/* The fcsr follows the saved floating-point register area. */ +#define FPU_CTX_FCSR_OFF FPU_CTX_REG_OFF(32) + #if __riscv_flen == 64 #define CTX_FREG_L fld #define CTX_FREG_S fsd diff --git a/libcpu/risc-v/common/include/csr.h b/libcpu/risc-v/common/include/csr.h index f5ddca9c40..66e42e8f07 100644 --- a/libcpu/risc-v/common/include/csr.h +++ b/libcpu/risc-v/common/include/csr.h @@ -13,6 +13,9 @@ #include <asm-generic.h> +/* Floating-point control and status register */ +#define CSR_FCSR 0x003 + /* Status register flags */ #define SR_SIE _UL(0x00000002) /* Supervisor Interrupt Enable */ #define SR_MIE _UL(0x00000008) /* Machine Interrupt Enable */ diff --git a/libcpu/risc-v/common/include/stack.h b/libcpu/risc-v/common/include/stack.h index c7d531eea0..9a2d94b28f 100644 --- a/libcpu/risc-v/common/include/stack.h +++ b/libcpu/risc-v/common/include/stack.h @@ -46,7 +46,13 @@ #define CTX_GENERAL_REG_NR 34 #define FRAME_OFF_SP (33 * SZREG) #endif -#define CTX_ALL_REG_NR (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR + CTX_VECTOR_REG_NR) +#define CTX_ALL_REG_NR_RAW (CTX_GENERAL_REG_NR + CTX_FPU_REG_NR + \ + CTX_FCSR_REG_NR + CTX_VECTOR_REG_NR) +/* The RISC-V psABI requires a 16-byte aligned stack on procedure entry. */ +#define CTX_FRAME_ALIGN_NR (16 / (__riscv_xlen / 8)) +#define CTX_ALL_REG_NR ((CTX_ALL_REG_NR_RAW + CTX_FRAME_ALIGN_NR - 1) / \ + CTX_FRAME_ALIGN_NR * CTX_FRAME_ALIGN_NR) +#define CTX_FRAME_PAD_NR (CTX_ALL_REG_NR - CTX_ALL_REG_NR_RAW) #define FRAME_OFF_XSTATUS (2 * SZREG) #define FRAME_OFF_XCAUSE (3 * SZREG) @@ -61,7 +67,7 @@ #define RT_HW_SWITCH_CONTEXT_RA 3 #define RT_HW_SWITCH_CONTEXT_TP 4 #ifdef ARCH_RISCV_32E -#define RT_HW_SWITCH_CONTEXT_SIZE 8 /* Padding for alignment */ +#define RT_HW_SWITCH_CONTEXT_BASE_SIZE 8 /* Padding for alignment */ #else #define RT_HW_SWITCH_CONTEXT_S2 5 #define RT_HW_SWITCH_CONTEXT_S3 6 @@ -73,9 +79,22 @@ #define RT_HW_SWITCH_CONTEXT_S9 12 #define RT_HW_SWITCH_CONTEXT_S10 13 #define RT_HW_SWITCH_CONTEXT_S11 14 -#define RT_HW_SWITCH_CONTEXT_SIZE 16 /* Padding for alignment */ +#define RT_HW_SWITCH_CONTEXT_BASE_SIZE 16 /* Padding for alignment */ #endif +/* Callee-saved FP registers and fcsr are appended to the switch frame. */ +#define RT_HW_SWITCH_CONTEXT_FPU_BASE RT_HW_SWITCH_CONTEXT_BASE_SIZE +#define RT_HW_SWITCH_CONTEXT_FCSR (RT_HW_SWITCH_CONTEXT_FPU_BASE + \ + CTX_FPU_SWITCH_REG_NR) +#define RT_HW_SWITCH_CONTEXT_SIZE_RAW (RT_HW_SWITCH_CONTEXT_FCSR + \ + CTX_FCSR_REG_NR) +/* Keep the stack pointer 16-byte aligned. */ +#define RT_HW_SWITCH_CONTEXT_ALIGN (16 / (__riscv_xlen / 8)) +#define RT_HW_SWITCH_CONTEXT_SIZE (((RT_HW_SWITCH_CONTEXT_SIZE_RAW + \ + RT_HW_SWITCH_CONTEXT_ALIGN - 1) / \ + RT_HW_SWITCH_CONTEXT_ALIGN) * \ + RT_HW_SWITCH_CONTEXT_ALIGN) + #ifndef __ASSEMBLY__ typedef struct rt_hw_switch_frame { @@ -132,10 +151,14 @@ struct rt_hw_stack_frame #ifdef ARCH_RISCV_FPU rt_ubase_t f[CTX_FPU_REG_NR]; /* f0~f31 */ + rt_ubase_t fcsr; #endif #ifdef ARCH_RISCV_VECTOR rt_ubase_t v[CTX_VECTOR_REG_NR]; #endif +#if CTX_FRAME_PAD_NR > 0 + rt_ubase_t pad[CTX_FRAME_PAD_NR]; /* Keep the frame 16-byte aligned */ +#endif }; RT_STATIC_ASSERT(riscv_trap_frame_size, diff --git a/libcpu/risc-v/common/interrupt.c b/libcpu/risc-v/common/interrupt.c index c571616e18..3c4e886eda 100644 --- a/libcpu/risc-v/common/interrupt.c +++ b/libcpu/risc-v/common/interrupt.c @@ -430,9 +430,12 @@ static rt_bool_t handler_irq(int hwirq, void *data) #if INTC_TYPE != INTC_TYPE_CLIC static rt_bool_t handler_plic_irq(int hwirq, void *data) { + rt_bool_t res = handler_irq(hwirq, data); + + /* Complete after handling to avoid re-pending level interrupts. */ plic_irq_ack_or_eoi(rt_hw_cpu_id(), hwirq); - return handler_irq(hwirq, data); + return res; } void plic_init(void) diff --git a/libcpu/risc-v/common/mmu.c b/libcpu/risc-v/common/mmu.c index 8a15c24f2d..8acd2cbc0c 100644 --- a/libcpu/risc-v/common/mmu.c +++ b/libcpu/risc-v/common/mmu.c @@ -136,6 +136,7 @@ static int mmu_map_early(rt_ubase_t *tbl, rt_uint8_t max_level, rt_ubase_t va, rt_ubase_t pa, rt_ubase_t attr, rt_bool_t flush) { rt_ubase_t page, *table, off, level_shift; + rt_ubase_t pv_off = flush ? rt_kmem_pvoff() : 0; attr = mmu_attr_fixup(attr); @@ -168,7 +169,7 @@ static int mmu_map_early(rt_ubase_t *tbl, rt_uint8_t max_level, return MMU_MAP_ERROR_NOPAGE; } - rt_memset((void *)page, 0, ARCH_PAGE_SIZE); + rt_memset((void *)(page - pv_off), 0, ARCH_PAGE_SIZE); table[off] = ((page >> ARCH_PAGE_SHIFT) << PTE_BITS) | PTE_V; if (flush) @@ -185,7 +186,7 @@ static int mmu_map_early(rt_ubase_t *tbl, rt_uint8_t max_level, /* Step into the next level page table */ page = (table[off] >> PTE_BITS) << ARCH_PAGE_SHIFT; - table = (rt_ubase_t *)page; + table = (rt_ubase_t *)(page - pv_off); level_shift -= VPN_BITS; } @@ -934,6 +935,12 @@ int rt_hw_mmu_map_init(rt_aspace_t aspace, void *v_address, rt_ubase_t size, #if __riscv_xlen == 32 && !defined(RT_USING_SMART) /* The RV32 ioremap and MPR regions are placed below the kernel image. */ aspace_start = (rt_ubase_t)v_address - rt_mpr_size; +#elif defined(RT_USING_SMART) + /* The runtime-sized MPR can extend below the linked kernel base. */ + if ((rt_ubase_t)v_address - rt_mpr_size < aspace_start) + { + aspace_start = (rt_ubase_t)v_address - rt_mpr_size; + } #endif rt_aspace_init(aspace, (void *)aspace_start, 0UL - aspace_start, vtable); @@ -1395,10 +1402,28 @@ void *rt_hw_mmu_pgtbl_create(void) (void *)rt_kernel_space.page_table + i * ARCH_PAGE_SIZE, ARCH_PAGE_SIZE); } +#else + rt_memcpy(mmu_table, rt_kernel_space.page_table, ARCH_PAGE_SIZE); +#endif + +#ifdef RT_USING_SMART + /* Do not share early identity mappings with the user address space. */ + rt_size_t user_start = get_top_vpn(USER_VADDR_START, satp_level_4k); + rt_size_t user_end = get_top_vpn(USER_VADDR_TOP - 1, satp_level_4k); + + for (int i = 0; i < (1UL << alloc_bits); ++i) + { + rt_ubase_t *table = (void *)mmu_table + i * ARCH_PAGE_SIZE; + + rt_memset(table + user_start, 0, + (user_end - user_start + 1) * sizeof(*table)); + } +#endif + +#ifdef MMU_PERCPU rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, mmu_table, RT_CPUS_NR * ARCH_PAGE_SIZE); #else - rt_memcpy(mmu_table, rt_kernel_space.page_table, ARCH_PAGE_SIZE); rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, mmu_table, ARCH_PAGE_SIZE); #endif diff --git a/libcpu/risc-v/common/pic/pic-riscv-aplic.c b/libcpu/risc-v/common/pic/pic-riscv-aplic.c index 8cc1d39997..9482809dee 100644 --- a/libcpu/risc-v/common/pic/pic-riscv-aplic.c +++ b/libcpu/risc-v/common/pic/pic-riscv-aplic.c @@ -236,12 +236,25 @@ static void aplic_irq_mask(struct rt_pic_irq *pirq) static void aplic_msi_irq_eoi(struct rt_pic_irq *pirq) { struct riscv_aplic *aplic = pirq->pic->priv_data; + rt_uint32_t input; switch (pirq->mode) { case RT_IRQ_MODE_LEVEL_HIGH: case RT_IRQ_MODE_LEVEL_LOW: - HWREG32(aplic->regs + APLIC_SETIPNUM_LE) = pirq->hwirq; + /* + * in_clrip reads the rectified input, including source polarity. + * Retrigger only while the device still asserts its interrupt. + * Older QEMU versions also set pending for deasserted inputs on + * setipnum_le writes, causing an endless stream of spurious MSIs. + */ + input = HWREG32(aplic->regs + APLIC_CLRIP_BASE + + (pirq->hwirq / APLIC_IRQBITS_PER_REG) * sizeof(rt_uint32_t)); + + if (input & RT_BIT(pirq->hwirq % APLIC_IRQBITS_PER_REG)) + { + HWREG32(aplic->regs + APLIC_SETIPNUM_LE) = pirq->hwirq; + } break; default: diff --git a/libcpu/risc-v/common/pic/pic-sifive-plic.c b/libcpu/risc-v/common/pic/pic-sifive-plic.c index 393b64a8a0..ce46485b4e 100644 --- a/libcpu/risc-v/common/pic/pic-sifive-plic.c +++ b/libcpu/risc-v/common/pic/pic-sifive-plic.c @@ -170,11 +170,41 @@ static void plic_irq_mask_raw(int cpu, int hwirq) static void plic_irq_ack_or_eoi(int cpu, int hwirq) { struct plic_percpu *percpu = &plic_percpu[cpu]; + rt_uint32_t *enable_reg, enable_mask; - if (percpu->valid) + if (!percpu->valid) + { + return; + } + + enable_reg = percpu->enable_base + (hwirq / 32) * sizeof(rt_uint32_t); + enable_mask = 1U << (hwirq % 32); + + /* + * The PLIC ignores completion for a disabled source. + * Temporarily enable it if the handler masked the interrupt. + */ + if (HWREG32(enable_reg) & enable_mask) { HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq; } + else + { + rt_ubase_t level = rt_spin_lock_irqsave(&percpu->enable_lock); + + if (HWREG32(enable_reg) & enable_mask) + { + HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq; + } + else + { + HWREG32(enable_reg) |= enable_mask; + HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq; + HWREG32(enable_reg) &= ~enable_mask; + } + + rt_spin_unlock_irqrestore(&percpu->enable_lock, level); + } } static rt_bool_t plic_handler_isr_raw(struct plic *plic, diff --git a/libcpu/risc-v/common/trap.c b/libcpu/risc-v/common/trap.c index 64074399fc..2646660ded 100644 --- a/libcpu/risc-v/common/trap.c +++ b/libcpu/risc-v/common/trap.c @@ -33,6 +33,8 @@ extern long list_thread(void); #endif +void rt_hw_show_register(struct rt_hw_stack_frame *regs); + #ifdef RT_USING_LWP #include <mm_aspace.h> #include <lwp.h> @@ -46,10 +48,14 @@ static void _check_fault(struct rt_hw_stack_frame *regs, uint32_t pc_adj, char * { rt_thread_t th; rt_bool_t is_user_fault = !(regs->xstatus & SR_PP); + rt_base_t saved_stat = 0; if (is_user_fault) { + /* User backtrace and process teardown can acquire sleeping locks. */ + saved_stat = csr_read_set(CSR_STATUS, SR_IE); rt_kprintf("%s! pc = %p\n", info, regs->epc - pc_adj); + rt_hw_show_register(regs); } /* User stack backtrace */ @@ -65,6 +71,8 @@ static void _check_fault(struct rt_hw_stack_frame *regs, uint32_t pc_adj, char * lwp_core_dump(regs, pc_adj); #endif sys_exit_group(-1); + /* Only reached when there is no LWP to terminate */ + csr_write(CSR_STATUS, saved_stat); } } @@ -296,6 +304,7 @@ void rt_hw_trap(struct rt_hw_stack_frame *regs) { rt_ubase_t xcause = regs->xcause; rt_ubase_t tval = csr_read_clear(CSR_TVAL, 0); + rt_bool_t is_user_fault = !(regs->xstatus & SR_PP); /* IRQ */ if (xcause & CAUSE_IRQ_FLAG) @@ -364,16 +373,23 @@ void rt_hw_trap(struct rt_hw_stack_frame *regs) case EXC_INST_PAGE_FAULT: case EXC_LOAD_PAGE_FAULT: case EXC_STORE_PAGE_FAULT: - #ifdef RT_USING_SMART - if (!(regs->xstatus & SR_PP) || +#ifdef RT_USING_SMART + if (is_user_fault || (tval >= USER_VADDR_START && tval < USER_VADDR_TOP)) { if (user_fault_fixable(regs, xcause, tval)) { return; } + + /* A failed user mapping must terminate only the LWP. */ + if (is_user_fault && lwp_self()) + { + _check_fault(regs, 0, "user fault"); + return; + } } - #endif /* RT_USING_SMART */ +#endif /* RT_USING_SMART */ default: LOG_E("Unhandle Execption: (%d) %s, TVAL = %p", xcause, diff --git a/libcpu/risc-v/common64/mmu.c b/libcpu/risc-v/common64/mmu.c index f09100c3d4..163bf32f31 100644 --- a/libcpu/risc-v/common64/mmu.c +++ b/libcpu/risc-v/common64/mmu.c @@ -12,6 +12,7 @@ */ #include <rtthread.h> +#include <rthw.h> #include <stddef.h> #include <stdint.h> @@ -85,6 +86,23 @@ void rt_hw_aspace_switch(rt_aspace_t aspace) uint32_t hartid = rt_hw_cpu_id(); uintptr_t ptr = (uintptr_t)aspace->page_table + (uintptr_t)(hartid * ARCH_PAGE_SIZE); uintptr_t page_table = (uintptr_t)rt_kmem_v2p((void *)ptr); + + if (page_table == (uintptr_t)ARCH_MAP_FAILED) + { + /* + * During early K230 bring-up the page table pointer held by the kernel + * aspace can still be a low physical address, before the kernel has + * switched to the high virtual mapping. The formal kernel aspace only + * maps the high virtual range, so rt_kmem_v2p() cannot translate that + * low pointer, while SATP needs the physical address of the page + * table. Use such a low pointer as-is; a high virtual address that + * fails to translate is a real error and is not rewritten here. + */ + if (ptr < KERNEL_VADDR_START) + { + page_table = ptr; + } + } #ifndef RT_USING_SMP current_mmu_table = aspace->page_table; #else @@ -632,14 +650,14 @@ static inline void _init_region(void *vaddr, size_t size) { rt_ioremap_start = vaddr; rt_ioremap_size = size; - rt_mpr_start = rt_ioremap_start - rt_mpr_size; + rt_mpr_start = (void *)((rt_ubase_t)rt_ioremap_start - rt_mpr_size); LOG_D("rt_ioremap_start: %p, rt_mpr_start: %p", rt_ioremap_start, rt_mpr_start); } #else static inline void _init_region(void *vaddr, size_t size) { - rt_mpr_start = vaddr - rt_mpr_size; + rt_mpr_start = (void *)((rt_ubase_t)vaddr - rt_mpr_size); } #endif @@ -1057,6 +1075,9 @@ void rt_hw_mem_setup_early(void *pgtbl, rt_uint64_t hartid) vs += EARLY_MAP_PAGE_SIZE_2M; } #endif + /* flush page table entries from data cache before enabling MMU */ + rt_hw_cpu_dcache_clean(early_pgtbl, ARCH_PAGE_SIZE); + /* apply new mapping */ asm volatile("sfence.vma x0, x0"); write_csr(satp, SATP_BASE | ((size_t)early_pgtbl >> PAGE_OFFSET_BIT)); @@ -1078,6 +1099,25 @@ rt_inline int _pgtbl_order(void) return order; } +#ifdef RT_USING_SMART +/* + * Early identity mappings may overlap the user address range. Clear those + * entries so they are not inherited by a user address space. + */ +static void _clear_user_pgtbl_entries(rt_ubase_t *table) +{ + int i; + /* Use the runtime top-level index so Sv39/Sv48/Sv57 all work. */ + int start_idx = GET_LVL_INDEX(USER_VADDR_START, 1); + int end_idx = GET_LVL_INDEX(USER_VADDR_TOP - 1, 1); + + for (i = start_idx; i <= end_idx; i++) + { + table[i] = 0; + } +} +#endif /* RT_USING_SMART */ + /** * @brief Creates and initializes a new MMU page table. * @@ -1111,6 +1151,9 @@ void *rt_hw_mmu_pgtbl_create(void) rt_ubase_t *dst = (rt_ubase_t *)((rt_ubase_t)mmu_table + (rt_ubase_t)(h * ARCH_PAGE_SIZE)); rt_memcpy(dst, src, ARCH_PAGE_SIZE); +#ifdef RT_USING_SMART + _clear_user_pgtbl_entries(dst); +#endif } rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, mmu_table, (rt_ubase_t)(1 << order) * ARCH_PAGE_SIZE); @@ -1121,6 +1164,11 @@ void *rt_hw_mmu_pgtbl_create(void) return RT_NULL; } rt_memcpy(mmu_table, rt_kernel_space.page_table, ARCH_PAGE_SIZE); + +#ifdef RT_USING_SMART + _clear_user_pgtbl_entries(mmu_table); +#endif + rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, mmu_table, ARCH_PAGE_SIZE); #endif diff --git a/libcpu/xtensa/esp32s3/SConscript b/libcpu/xtensa/esp32s3/SConscript new file mode 100644 index 0000000000..062d3a623e --- /dev/null +++ b/libcpu/xtensa/esp32s3/SConscript @@ -0,0 +1,25 @@ +# RT-Thread building script for ESP32-S3 (Xtensa LX7) CPU port + +from building import * +import os +from env_package import find_package_path + +cwd = GetCurrentDir() +src = Glob('*.c') + Glob('*_gcc.S') + +# Resolve ESP-IDF package the same way idf_port does (for xtensa_context.h). +bsp_root = os.path.normpath(os.path.join(cwd, '..', '..', '..', 'bsp', 'ESP', 'ESP32_S3')) +PKG_IDF = find_package_path(bsp_root, 'ESP-IDF-latest', + marker='components/xtensa/esp32s3/include') + +_xtensa_freertos_inc = PKG_IDF + '/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos' +_xtensa_portable = PKG_IDF + '/components/freertos/FreeRTOS-Kernel/portable/xtensa' +_xtensa_inc = PKG_IDF + '/components/xtensa/include' +_xtensa_s3_inc = PKG_IDF + '/components/xtensa/esp32s3/include' + +CPPPATH = [cwd, _xtensa_freertos_inc, _xtensa_portable, _xtensa_inc, _xtensa_s3_inc] +ASFLAGS = ' '.join(['-I' + p for p in CPPPATH]) + +group = DefineGroup('CPU', src, depend = [''], CPPPATH = CPPPATH, ASFLAGS = ASFLAGS) + +Return('group') |
