17#ifndef ZEPHYR_INCLUDE_ARCH_RISCV_ARCH_H_
18#define ZEPHYR_INCLUDE_ARCH_RISCV_ARCH_H_
27#if defined(CONFIG_USERSPACE)
36#define ARCH_STACK_PTR_ALIGN 16
38#define Z_RISCV_STACK_PMP_ALIGN \
39 MAX(CONFIG_PMP_GRANULARITY, ARCH_STACK_PTR_ALIGN)
41#ifdef CONFIG_PMP_STACK_GUARD
51#ifdef CONFIG_PMP_POWER_OF_TWO_ALIGNMENT
52#define Z_RISCV_STACK_GUARD_SIZE \
53 Z_POW2_CEIL(MAX(sizeof(struct arch_esf) + CONFIG_PMP_STACK_GUARD_MIN_SIZE, \
54 Z_RISCV_STACK_PMP_ALIGN))
55#define ARCH_KERNEL_STACK_OBJ_ALIGN Z_RISCV_STACK_GUARD_SIZE
57#define Z_RISCV_STACK_GUARD_SIZE \
58 ROUND_UP(sizeof(struct arch_esf) + CONFIG_PMP_STACK_GUARD_MIN_SIZE, \
59 Z_RISCV_STACK_PMP_ALIGN)
60#define ARCH_KERNEL_STACK_OBJ_ALIGN Z_RISCV_STACK_PMP_ALIGN
75#define ARCH_KERNEL_STACK_RESERVED Z_RISCV_STACK_GUARD_SIZE
78#define Z_RISCV_STACK_GUARD_SIZE 0
81#ifdef CONFIG_PMP_POWER_OF_TWO_ALIGNMENT
118#define ARCH_THREAD_STACK_RESERVED Z_RISCV_STACK_GUARD_SIZE
119#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
120 Z_POW2_CEIL(MAX(MAX(size, CONFIG_PRIVILEGED_STACK_SIZE), \
121 Z_RISCV_STACK_PMP_ALIGN))
122#define ARCH_THREAD_STACK_OBJ_ALIGN(size) \
123 ARCH_THREAD_STACK_SIZE_ADJUST(size)
142#define ARCH_THREAD_STACK_RESERVED \
143 ROUND_UP(Z_RISCV_STACK_GUARD_SIZE + CONFIG_PRIVILEGED_STACK_SIZE, \
144 Z_RISCV_STACK_PMP_ALIGN)
145#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
146 ROUND_UP(size, Z_RISCV_STACK_PMP_ALIGN)
147#define ARCH_THREAD_STACK_OBJ_ALIGN(size) Z_RISCV_STACK_PMP_ALIGN
162#define MSTATUS_IEN (1UL << 3)
164#define MSTATUS_MPP_U (PRV_U << 11)
166#define MSTATUS_MPP_S (PRV_S << 11)
168#define MSTATUS_MPP_M (PRV_M << 11)
169#define MSTATUS_MPIE_EN (1UL << 7)
171#define MSTATUS_FS_OFF (0UL << 13)
172#define MSTATUS_FS_INIT (1UL << 13)
173#define MSTATUS_FS_CLEAN (2UL << 13)
174#define MSTATUS_FS_DIRTY (3UL << 13)
191#ifdef CONFIG_RISCV_S_MODE
192#define RV_STATUS_DEF_RESTORE (SSTATUS_SPP | SSTATUS_SPIE)
194#define RV_STATUS_DEF_RESTORE (MSTATUS_MPP_M | MSTATUS_MPIE_EN)
200#ifdef CONFIG_RISCV_S_MODE
202#define RV_STATUS_PP SSTATUS_SPP
204#define RV_STATUS_PP_U 0
207#define RV_STATUS_PP MSTATUS_MPP
209#define RV_STATUS_PP_U PRV_U
220#ifdef CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_CODE
221#define ARCH_IRQ_VECTOR_JUMP_CODE(v) "j " STRINGIFY(v)
234#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
236#define K_MEM_PARTITION_P_RW_U_RO ((k_mem_partition_attr_t) \
238#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
240#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
242#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
244#define K_MEM_PARTITION_P_NA_U_NA ((k_mem_partition_attr_t) \
248#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
249 {PMP_R | PMP_W | PMP_X})
250#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
261#define K_MEM_PARTITION_IS_WRITABLE(attr) (attr.pmp_attr & PMP_W)
272#define K_MEM_PARTITION_IS_EXECUTABLE(attr) (attr.pmp_attr & PMP_X)
283extern void z_irq_spurious(
const void *unused);
286#ifdef CONFIG_RISCV_S_MODE
288#define RV_STATUS_CSR "sstatus"
290#define RV_STATUS_IE SSTATUS_SIE
292#define RV_STATUS_PIE SSTATUS_SPIE
295#define RV_STATUS_CSR "mstatus"
297#define RV_STATUS_IE MSTATUS_IEN
299#define RV_STATUS_PIE MSTATUS_MPIE_EN
318static ALWAYS_INLINE void z_riscv_status_set(
unsigned long val)
320 __asm__
volatile (
"csrs " RV_STATUS_CSR ", %0" : :
"rK" (val) :
"memory");
326static ALWAYS_INLINE void z_riscv_status_clear(
unsigned long val)
328 __asm__
volatile (
"csrc " RV_STATUS_CSR ", %0" : :
"rK" (val) :
"memory");
337#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
338 return z_soc_irq_lock();
357#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
358 z_soc_irq_unlock(key);
369#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
370 return z_soc_irq_unlocked(key);
379#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
383 unsigned int key = z_soc_irq_lock();
384 bool enabled = z_soc_irq_unlocked(key);
386 z_soc_irq_unlock(key);
389 unsigned int status = z_riscv_status_read();
397 __asm__
volatile(
"nop");
422#if defined(CONFIG_RISCV_PRIVILEGED)
static ALWAYS_INLINE void arch_nop(void)
Definition arch.h:351
Header file for bit reverse operations.
RISC-V public interrupt handling.
RISCV specific syscall header.
Per-arch thread definition.
RISCV public error handling.
Public interface for configuring interrupts.
static ALWAYS_INLINE unsigned int arch_irq_lock(void)
Definition arch.h:72
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
Definition arch.h:83
uint64_t sys_clock_cycle_get_64(void)
uint32_t sys_clock_cycle_get_32(void)
static uint32_t arch_k_cycle_get_32(void)
Definition arch.h:114
static ALWAYS_INLINE bool arch_cpu_irqs_are_enabled(void)
Implementation of arch_cpu_irqs_are_enabled.
Definition arch.h:102
static uint64_t arch_k_cycle_get_64(void)
Definition arch.h:121
static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
Definition arch.h:96
#define RV_STATUS_IE
Interrupt-enable bit in the status CSR (M-mode: MIE).
Definition arch.h:297
#define RV_STATUS_CSR
Name of the interrupt-status CSR as a string literal (M-mode).
Definition arch.h:295
Header file for the single-linked list API.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
unsigned int pmp_update_nr
Definition arch.h:280
Definition arm_mpu_v7m.h:155
uint8_t pmp_attr
Definition arch.h:276
Software-managed ISR table.