NMSIS-Core  Version 1.5.0
NMSIS-Core support for Nuclei processor-based devices
Core CSR Register Access

Functions to access the Core CSR Registers. More...

Macros

#define __RV_CSR_SWAP(csr, val)
 CSR operation Macro for csrrw instruction. More...
 
#define __RV_CSR_READ(csr)
 CSR operation Macro for csrr instruction. More...
 
#define __RV_CSR_WRITE(csr, val)
 CSR operation Macro for csrw instruction. More...
 
#define __RV_CSR_READ_SET(csr, val)
 CSR operation Macro for csrrs instruction. More...
 
#define __RV_CSR_SET(csr, val)
 CSR operation Macro for csrs instruction. More...
 
#define __RV_CSR_READ_CLEAR(csr, val)
 CSR operation Macro for csrrc instruction. More...
 
#define __RV_CSR_CLEAR(csr, val)
 CSR operation Macro for csrc instruction. More...
 
#define __FENCE(p, s)   __ASM volatile ("fence " #p "," #s : : : "memory")
 Execute fence instruction, p -> pred, s -> succ. More...
 
#define __RWMB()   __FENCE(iorw,iorw)
 Read & Write Memory barrier. More...
 
#define __RMB()   __FENCE(ir,ir)
 Read Memory barrier. More...
 
#define __WMB()   __FENCE(ow,ow)
 Write Memory barrier. More...
 
#define __SMP_RWMB()   __FENCE(rw,rw)
 SMP Read & Write Memory barrier. More...
 
#define __SMP_RMB()   __FENCE(r,r)
 SMP Read Memory barrier. More...
 
#define __SMP_WMB()   __FENCE(w,w)
 SMP Write Memory barrier. More...
 
#define __CPU_RELAX()   __ASM volatile ("" : : : "memory")
 CPU relax for busy loop. More...
 

Functions

__STATIC_FORCEINLINE void __FENCE_I (void)
 Fence.i Instruction. More...
 
__STATIC_INLINE void __switch_mode (uint8_t mode, uintptr_t stack, void(*entry_point)(void))
 switch privilege from machine mode to others. More...
 
__STATIC_INLINE void __s_switch_mode (uint8_t mode, uintptr_t stack, void(*entry_point)(void))
 switch privilege from supervisor mode to others. More...
 
__STATIC_FORCEINLINE void __enable_irq (void)
 Enable IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_irq (void)
 Disable IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __enable_ext_irq (void)
 Enable External IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_ext_irq (void)
 Disable External IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __enable_timer_irq (void)
 Enable Timer IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_timer_irq (void)
 Disable Timer IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __enable_sw_irq (void)
 Enable software IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_sw_irq (void)
 Disable software IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_core_irq (uint32_t irq)
 Disable Core IRQ Interrupt. More...
 
__STATIC_FORCEINLINE void __enable_core_irq (uint32_t irq)
 Enable Core IRQ Interrupt. More...
 
__STATIC_FORCEINLINE uint32_t __get_core_irq_pending (uint32_t irq)
 Get Core IRQ Interrupt Pending status. More...
 
__STATIC_FORCEINLINE void __clear_core_irq_pending (uint32_t irq)
 Clear Core IRQ Interrupt Pending status. More...
 
__STATIC_FORCEINLINE void __enable_irq_s (void)
 Enable IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_irq_s (void)
 Disable IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_ext_irq_s (void)
 Enable External IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_ext_irq_s (void)
 Disable External IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_timer_irq_s (void)
 Enable Timer IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_timer_irq_s (void)
 Disable Timer IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_sw_irq_s (void)
 Enable software IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_sw_irq_s (void)
 Disable software IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_core_irq_s (uint32_t irq)
 Disable Core IRQ Interrupt in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_core_irq_s (uint32_t irq)
 Enable Core IRQ Interrupt in supervisor mode. More...
 
__STATIC_FORCEINLINE uint32_t __get_core_irq_pending_s (uint32_t irq)
 Get Core IRQ Interrupt Pending status in supervisor mode. More...
 
__STATIC_FORCEINLINE void __clear_core_irq_pending_s (uint32_t irq)
 Clear Core IRQ Interrupt Pending status in supervisor mode. More...
 
__STATIC_INLINE rv_counter_t __get_rv_cycle (void)
 Read whole 64 bits value of mcycle counter. More...
 
__STATIC_FORCEINLINE void __set_rv_cycle (rv_counter_t cycle)
 Set whole 64 bits value of mcycle counter. More...
 
__STATIC_INLINE rv_counter_t __get_rv_instret (void)
 Read whole 64 bits value of machine instruction-retired counter. More...
 
__STATIC_FORCEINLINE void __set_rv_instret (rv_counter_t instret)
 Set whole 64 bits value of machine instruction-retired counter. More...
 
__STATIC_INLINE rv_counter_t __get_rv_time (void)
 Read whole 64 bits value of real-time clock. More...
 
__STATIC_FORCEINLINE unsigned long __read_cycle_csr (void)
 Read the CYCLE register. More...
 
__STATIC_FORCEINLINE unsigned long __read_instret_csr (void)
 Read the INSTRET register. More...
 
__STATIC_FORCEINLINE unsigned long __read_time_csr (void)
 Read the TIME register. More...
 
__STATIC_FORCEINLINE unsigned long __get_cluster_id (void)
 Get cluster id of current cluster. More...
 
__STATIC_FORCEINLINE unsigned long __get_hart_index (void)
 Get hart index of current cluster. More...
 
__STATIC_FORCEINLINE unsigned long __get_hart_id (void)
 Get hart id of current cluster. More...
 
__STATIC_FORCEINLINE unsigned long __get_cluster_id_s (void)
 Get cluster id of current cluster in supervisor mode. More...
 
__STATIC_FORCEINLINE unsigned long __get_hart_index_s (void)
 Get hart index of current cluster in supervisor mode. More...
 
__STATIC_FORCEINLINE unsigned long __get_hart_id_s (void)
 Get hart id of current cluster in supervisor mode. More...
 

Detailed Description

Functions to access the Core CSR Registers.

The following functions or macros provide access to Core CSR registers.

Macro Definition Documentation

◆ __CPU_RELAX

#define __CPU_RELAX ( )    __ASM volatile ("" : : : "memory")

CPU relax for busy loop.

Definition at line 868 of file core_feature_base.h.

◆ __FENCE

#define __FENCE (   p,
 
)    __ASM volatile ("fence " #p "," #s : : : "memory")

Execute fence instruction, p -> pred, s -> succ.

the FENCE instruction ensures that all memory accesses from instructions preceding the fence in program order (the predecessor set) appear earlier in the global memory order than memory accesses from instructions appearing after the fence in program order (the successor set). For details, please refer to The RISC-V Instruction Set Manual

Parameters
ppredecessor set, such as iorw, rw, r, w
ssuccessor set, such as iorw, rw, r, w

Definition at line 833 of file core_feature_base.h.

◆ __RMB

#define __RMB ( )    __FENCE(ir,ir)

Read Memory barrier.

Definition at line 853 of file core_feature_base.h.

◆ __RV_CSR_CLEAR

#define __RV_CSR_CLEAR (   csr,
  val 
)

CSR operation Macro for csrc instruction.

Set csr register to be csr_content & ~val

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrc instruction

Definition at line 799 of file core_feature_base.h.

◆ __RV_CSR_READ

#define __RV_CSR_READ (   csr)

CSR operation Macro for csrr instruction.

Read the content of csr register to __v and return it

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
Returns
the CSR register value

Definition at line 707 of file core_feature_base.h.

◆ __RV_CSR_READ_CLEAR

#define __RV_CSR_READ_CLEAR (   csr,
  val 
)

CSR operation Macro for csrrc instruction.

Read the content of csr register to __v, then set csr register to be __v & ~val, then return __v

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrrc instruction
Returns
the CSR register value before written

Definition at line 781 of file core_feature_base.h.

◆ __RV_CSR_READ_SET

#define __RV_CSR_READ_SET (   csr,
  val 
)

CSR operation Macro for csrrs instruction.

Read the content of csr register to __v, then set csr register to be __v | val, then return __v

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrrs instruction
Returns
the CSR register value before written

Definition at line 744 of file core_feature_base.h.

◆ __RV_CSR_SET

#define __RV_CSR_SET (   csr,
  val 
)

CSR operation Macro for csrs instruction.

Set csr register to be csr_content | val

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrs instruction

Definition at line 762 of file core_feature_base.h.

◆ __RV_CSR_SWAP

#define __RV_CSR_SWAP (   csr,
  val 
)

CSR operation Macro for csrrw instruction.

Read the content of csr register to __v, then write content of val into csr register, then return __v

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valvalue to store into the CSR register
Returns
the CSR register value before written

Definition at line 689 of file core_feature_base.h.

◆ __RV_CSR_WRITE

#define __RV_CSR_WRITE (   csr,
  val 
)

CSR operation Macro for csrw instruction.

Write the content of val to csr register

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valvalue to store into the CSR register

Definition at line 725 of file core_feature_base.h.

◆ __RWMB

#define __RWMB ( )    __FENCE(iorw,iorw)

Read & Write Memory barrier.

Definition at line 850 of file core_feature_base.h.

◆ __SMP_RMB

#define __SMP_RMB ( )    __FENCE(r,r)

SMP Read Memory barrier.

Definition at line 862 of file core_feature_base.h.

◆ __SMP_RWMB

#define __SMP_RWMB ( )    __FENCE(rw,rw)

SMP Read & Write Memory barrier.

Definition at line 859 of file core_feature_base.h.

◆ __SMP_WMB

#define __SMP_WMB ( )    __FENCE(w,w)

SMP Write Memory barrier.

Definition at line 865 of file core_feature_base.h.

◆ __WMB

#define __WMB ( )    __FENCE(ow,ow)

Write Memory barrier.

Definition at line 856 of file core_feature_base.h.

Function Documentation

◆ __clear_core_irq_pending()

__STATIC_FORCEINLINE void __clear_core_irq_pending ( uint32_t  irq)

Clear Core IRQ Interrupt Pending status.

Clear Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1057 of file core_feature_base.h.

1058 {
1059  __RV_CSR_CLEAR(CSR_MIP, 1UL << irq);
1060 }
#define __RV_CSR_CLEAR(csr, val)
CSR operation Macro for csrc instruction.
#define CSR_MIP

References __RV_CSR_CLEAR, and CSR_MIP.

◆ __clear_core_irq_pending_s()

__STATIC_FORCEINLINE void __clear_core_irq_pending_s ( uint32_t  irq)

Clear Core IRQ Interrupt Pending status in supervisor mode.

Clear Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1189 of file core_feature_base.h.

1190 {
1191  __RV_CSR_CLEAR(CSR_SIP, 1UL << irq);
1192 }
#define CSR_SIP

References __RV_CSR_CLEAR, and CSR_SIP.

◆ __disable_core_irq()

__STATIC_FORCEINLINE void __disable_core_irq ( uint32_t  irq)

Disable Core IRQ Interrupt.

Disable Core IRQ interrupt by clearing the irq bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1024 of file core_feature_base.h.

1025 {
1026  __RV_CSR_CLEAR(CSR_MIE, 1UL << irq);
1027 }
#define CSR_MIE

References __RV_CSR_CLEAR, and CSR_MIE.

◆ __disable_core_irq_s()

__STATIC_FORCEINLINE void __disable_core_irq_s ( uint32_t  irq)

Disable Core IRQ Interrupt in supervisor mode.

Disable Core IRQ interrupt by clearing the irq bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1156 of file core_feature_base.h.

1157 {
1158  __RV_CSR_CLEAR(CSR_SIE, 1UL << irq);
1159 }
#define CSR_SIE

References __RV_CSR_CLEAR, and CSR_SIE.

◆ __disable_ext_irq()

__STATIC_FORCEINLINE void __disable_ext_irq ( void  )

Disable External IRQ Interrupts.

Disables External IRQ interrupts by clearing the MEIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 969 of file core_feature_base.h.

970 {
972 }
#define MIE_MEIE

References __RV_CSR_CLEAR, CSR_MIE, and MIE_MEIE.

◆ __disable_ext_irq_s()

__STATIC_FORCEINLINE void __disable_ext_irq_s ( void  )

Disable External IRQ Interrupts in supervisor mode.

Disables External IRQ interrupts by clearing the SEIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1101 of file core_feature_base.h.

1102 {
1104 }
#define SIE_SEIE

References __RV_CSR_CLEAR, CSR_SIE, and SIE_SEIE.

◆ __disable_irq()

__STATIC_FORCEINLINE void __disable_irq ( void  )

Disable IRQ Interrupts.

Disables IRQ interrupts by clearing the MIE-bit in the MSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 947 of file core_feature_base.h.

948 {
950 }
#define MSTATUS_MIE
#define CSR_MSTATUS

References __RV_CSR_CLEAR, CSR_MSTATUS, and MSTATUS_MIE.

◆ __disable_irq_s()

__STATIC_FORCEINLINE void __disable_irq_s ( void  )

Disable IRQ Interrupts in supervisor mode.

Disables IRQ interrupts by clearing the SIE-bit in the SSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 1079 of file core_feature_base.h.

1080 {
1082 }
#define SSTATUS_SIE
#define CSR_SSTATUS

References __RV_CSR_CLEAR, CSR_SSTATUS, and SSTATUS_SIE.

◆ __disable_sw_irq()

__STATIC_FORCEINLINE void __disable_sw_irq ( void  )

Disable software IRQ Interrupts.

Disables software IRQ interrupts by clearing the MSIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1013 of file core_feature_base.h.

1014 {
1016 }
#define MIE_MSIE

References __RV_CSR_CLEAR, CSR_MIE, and MIE_MSIE.

◆ __disable_sw_irq_s()

__STATIC_FORCEINLINE void __disable_sw_irq_s ( void  )

Disable software IRQ Interrupts in supervisor mode.

Disables software IRQ interrupts by clearing the SSIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1145 of file core_feature_base.h.

1146 {
1148 }
#define SIE_SSIE

References __RV_CSR_CLEAR, CSR_SIE, and SIE_SSIE.

◆ __disable_timer_irq()

__STATIC_FORCEINLINE void __disable_timer_irq ( void  )

Disable Timer IRQ Interrupts.

Disables Timer IRQ interrupts by clearing the MTIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 991 of file core_feature_base.h.

992 {
994 }
#define MIE_MTIE

References __RV_CSR_CLEAR, CSR_MIE, and MIE_MTIE.

◆ __disable_timer_irq_s()

__STATIC_FORCEINLINE void __disable_timer_irq_s ( void  )

Disable Timer IRQ Interrupts in supervisor mode.

Disables Timer IRQ interrupts by clearing the STIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1123 of file core_feature_base.h.

1124 {
1126 }
#define SIE_STIE

References __RV_CSR_CLEAR, CSR_SIE, and SIE_STIE.

◆ __enable_core_irq()

__STATIC_FORCEINLINE void __enable_core_irq ( uint32_t  irq)

Enable Core IRQ Interrupt.

Enable Core IRQ interrupt by setting the irq bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1035 of file core_feature_base.h.

1036 {
1037  __RV_CSR_SET(CSR_MIE, 1UL << irq);
1038 }
#define __RV_CSR_SET(csr, val)
CSR operation Macro for csrs instruction.

References __RV_CSR_SET, and CSR_MIE.

◆ __enable_core_irq_s()

__STATIC_FORCEINLINE void __enable_core_irq_s ( uint32_t  irq)

Enable Core IRQ Interrupt in supervisor mode.

Enable Core IRQ interrupt by setting the irq bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1167 of file core_feature_base.h.

1168 {
1169  __RV_CSR_SET(CSR_SIE, 1UL << irq);
1170 }

References __RV_CSR_SET, and CSR_SIE.

◆ __enable_ext_irq()

__STATIC_FORCEINLINE void __enable_ext_irq ( void  )

Enable External IRQ Interrupts.

Enables External IRQ interrupts by setting the MEIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 958 of file core_feature_base.h.

959 {
961 }

References __RV_CSR_SET, CSR_MIE, and MIE_MEIE.

Referenced by PLIC_Register_IRQ().

◆ __enable_ext_irq_s()

__STATIC_FORCEINLINE void __enable_ext_irq_s ( void  )

Enable External IRQ Interrupts in supervisor mode.

Enables External IRQ interrupts by setting the SEIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1090 of file core_feature_base.h.

1091 {
1093 }

References __RV_CSR_SET, CSR_SIE, and SIE_SEIE.

Referenced by PLIC_Register_IRQ_S().

◆ __enable_irq()

__STATIC_FORCEINLINE void __enable_irq ( void  )

Enable IRQ Interrupts.

Enables IRQ interrupts by setting the MIE-bit in the MSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 936 of file core_feature_base.h.

937 {
939 }

References __RV_CSR_SET, CSR_MSTATUS, and MSTATUS_MIE.

◆ __enable_irq_s()

__STATIC_FORCEINLINE void __enable_irq_s ( void  )

Enable IRQ Interrupts in supervisor mode.

Enables IRQ interrupts by setting the SIE-bit in the SSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 1068 of file core_feature_base.h.

1069 {
1071 }

References __RV_CSR_SET, CSR_SSTATUS, and SSTATUS_SIE.

◆ __enable_sw_irq()

__STATIC_FORCEINLINE void __enable_sw_irq ( void  )

Enable software IRQ Interrupts.

Enables software IRQ interrupts by setting the MSIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1002 of file core_feature_base.h.

1003 {
1005 }

References __RV_CSR_SET, CSR_MIE, and MIE_MSIE.

Referenced by Core_Register_IRQ().

◆ __enable_sw_irq_s()

__STATIC_FORCEINLINE void __enable_sw_irq_s ( void  )

Enable software IRQ Interrupts in supervisor mode.

Enables software IRQ interrupts by setting the SSIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1134 of file core_feature_base.h.

1135 {
1137 }

References __RV_CSR_SET, CSR_SIE, and SIE_SSIE.

Referenced by Core_Register_IRQ_S().

◆ __enable_timer_irq()

__STATIC_FORCEINLINE void __enable_timer_irq ( void  )

Enable Timer IRQ Interrupts.

Enables Timer IRQ interrupts by setting the MTIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 980 of file core_feature_base.h.

981 {
983 }

References __RV_CSR_SET, CSR_MIE, and MIE_MTIE.

Referenced by Core_Register_IRQ().

◆ __enable_timer_irq_s()

__STATIC_FORCEINLINE void __enable_timer_irq_s ( void  )

Enable Timer IRQ Interrupts in supervisor mode.

Enables Timer IRQ interrupts by setting the STIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1112 of file core_feature_base.h.

1113 {
1115 }

References __RV_CSR_SET, CSR_SIE, and SIE_STIE.

Referenced by Core_Register_IRQ_S().

◆ __FENCE_I()

__STATIC_FORCEINLINE void __FENCE_I ( void  )

Fence.i Instruction.

The FENCE.I instruction is used to synchronize the instruction and data streams.

Definition at line 841 of file core_feature_base.h.

842 {
843 #if defined(CPU_SERIES) && CPU_SERIES == 100
844 #else
845  __ASM volatile("fence.i");
846 #endif
847 }
#define __ASM
Pass information from the compiler to the assembler.
Definition: nmsis_gcc.h:55

References __ASM.

Referenced by __ECLIC_SetVector(), __ECLIC_SetVector_S(), _premain_init(), and PMA_SetRegion().

◆ __get_cluster_id()

__STATIC_FORCEINLINE unsigned long __get_cluster_id ( void  )

Get cluster id of current cluster.

This function will get cluster id of current cluster in a multiple cluster system

Returns
The cluster id of current cluster
Remarks
mhartid bit 15-8 is designed for cluster id in nuclei subsystem reference design
Attention
function is allowed in machine mode only

Definition at line 1392 of file core_feature_base.h.

1393 {
1394  unsigned long id;
1395 
1396  id = (__RV_CSR_READ(CSR_MHARTID) >> 8) & 0xFF;
1397  return id;
1398 }
#define __RV_CSR_READ(csr)
CSR operation Macro for csrr instruction.
#define CSR_MHARTID

References __RV_CSR_READ, and CSR_MHARTID.

◆ __get_cluster_id_s()

__STATIC_FORCEINLINE unsigned long __get_cluster_id_s ( void  )

Get cluster id of current cluster in supervisor mode.

This function will get cluster id of current cluster in a multiple cluster system

Returns
The cluster id of current cluster
Remarks
hartid bit 15-8 is designed for cluster id in nuclei subsystem reference design
Attention
function is allowed in machine/supervisor mode, currently only present in 600/900 series from 2024 released version

Definition at line 1444 of file core_feature_base.h.

1445 {
1446  unsigned long id;
1447 
1448  id = (__RV_CSR_READ(CSR_SHARTID) >> 8) & 0xFF;
1449  return id;
1450 }
#define CSR_SHARTID

References __RV_CSR_READ, and CSR_SHARTID.

◆ __get_core_irq_pending()

__STATIC_FORCEINLINE uint32_t __get_core_irq_pending ( uint32_t  irq)

Get Core IRQ Interrupt Pending status.

Get Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1046 of file core_feature_base.h.

1047 {
1048  return ((__RV_CSR_READ(CSR_MIP) >> irq) & 0x1);
1049 }

References __RV_CSR_READ, and CSR_MIP.

◆ __get_core_irq_pending_s()

__STATIC_FORCEINLINE uint32_t __get_core_irq_pending_s ( uint32_t  irq)

Get Core IRQ Interrupt Pending status in supervisor mode.

Get Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 1178 of file core_feature_base.h.

1179 {
1180  return ((__RV_CSR_READ(CSR_SIP) >> irq) & 0x1);
1181 }

References __RV_CSR_READ, and CSR_SIP.

◆ __get_hart_id()

__STATIC_FORCEINLINE unsigned long __get_hart_id ( void  )

Get hart id of current cluster.

This function will get hart id of current cluster in a multiple cluster system

Returns
The hart id of current cluster
Remarks
it will return full hartid not part of it for reference subsystem design, if your reference subsystem design has hartid offset, please define __HARTID_OFFSET in <Device>.h
Attention
function is allowed in machine mode only

Definition at line 1428 of file core_feature_base.h.

1429 {
1430  unsigned long id;
1431  id = __RV_CSR_READ(CSR_MHARTID);
1432  return id;
1433 }

References __RV_CSR_READ, and CSR_MHARTID.

Referenced by _premain_init(), system_default_exception_handler(), and SystemBannerPrint().

◆ __get_hart_id_s()

__STATIC_FORCEINLINE unsigned long __get_hart_id_s ( void  )

Get hart id of current cluster in supervisor mode.

This function will get hart id of current cluster in a multiple cluster system

Returns
The hart id of current cluster
Remarks
it will return full hartid not part of it for reference subsystem design, if your reference subsystem design has hartid offset, please define __HARTID_OFFSET in <Device>.h
Attention
function is allowed in machine/supervisor mode, currently only present in 600/900 series from 2024 released version

Definition at line 1482 of file core_feature_base.h.

1483 {
1484  unsigned long id;
1485  id = __RV_CSR_READ(CSR_SHARTID);
1486  return id;
1487 }

References __RV_CSR_READ, and CSR_SHARTID.

◆ __get_hart_index()

__STATIC_FORCEINLINE unsigned long __get_hart_index ( void  )

Get hart index of current cluster.

This function will get hart index of current cluster in a multiple cluster system, hart index is hartid - hartid offset, for example if your hartid is 1, and offset is 1, then hart index is 0

Returns
The hart index of current cluster
Attention
function is allowed in machine mode only

Definition at line 1408 of file core_feature_base.h.

1409 {
1410  unsigned long id;
1411 #ifdef __HARTID_OFFSET
1412  id = __RV_CSR_READ(CSR_MHARTID) - __HARTID_OFFSET;
1413 #else
1414  id = __RV_CSR_READ(CSR_MHARTID);
1415 #endif
1416  return id;
1417 }

References __RV_CSR_READ, and CSR_MHARTID.

◆ __get_hart_index_s()

__STATIC_FORCEINLINE unsigned long __get_hart_index_s ( void  )

Get hart index of current cluster in supervisor mode.

This function will get hart index of current cluster in a multiple cluster system, hart index is hartid - hartid offset, for example if your hartid is 1, and offset is 1, then hart index is 0

Returns
The hart index of current cluster
Attention
function is allowed in machine/supervisor mode, currently only present in 600/900 series from 2024 released version

Definition at line 1461 of file core_feature_base.h.

1462 {
1463  unsigned long id;
1464 #ifdef __HARTID_OFFSET
1465  id = __RV_CSR_READ(CSR_SHARTID) - __HARTID_OFFSET;
1466 #else
1467  id = __RV_CSR_READ(CSR_SHARTID);
1468 #endif
1469  return id;
1470 }

References __RV_CSR_READ, and CSR_SHARTID.

◆ __get_rv_cycle()

__STATIC_INLINE rv_counter_t __get_rv_cycle ( void  )

Read whole 64 bits value of mcycle counter.

This function will read the whole 64 bits of MCYCLE register

Returns
The whole 64 bits value of MCYCLE
Remarks
It will work for both RV32 and RV64 to get full 64bits value of MCYCLE

Definition at line 1200 of file core_feature_base.h.

1201 {
1202  __RWMB(); // Make sure previous memory and io operation finished
1203 #if __RISCV_XLEN == 32
1204 
1205 #if defined(CPU_SERIES) && CPU_SERIES == 100
1206  return __RV_CSR_READ(CSR_MCYCLE);
1207 #else
1208  volatile uint32_t high0, low, high;
1209  uint64_t full;
1210 
1211  high0 = __RV_CSR_READ(CSR_MCYCLEH);
1212  low = __RV_CSR_READ(CSR_MCYCLE);
1213  high = __RV_CSR_READ(CSR_MCYCLEH);
1214  if (high0 != high) {
1215  low = __RV_CSR_READ(CSR_MCYCLE);
1216  }
1217  full = (((uint64_t)high) << 32) | low;
1218  return full;
1219 #endif
1220 
1221 #elif __RISCV_XLEN == 64
1222  return (uint64_t)__RV_CSR_READ(CSR_MCYCLE);
1223 #else // TODO Need cover for XLEN=128 case in future
1224  return (uint64_t)__RV_CSR_READ(CSR_MCYCLE);
1225 #endif
1226 }
#define __RWMB()
Read & Write Memory barrier.
#define CSR_MCYCLE
#define CSR_MCYCLEH

References __RV_CSR_READ, __RWMB, CSR_MCYCLE, and CSR_MCYCLEH.

Referenced by __get_hpm_counter().

◆ __get_rv_instret()

__STATIC_INLINE rv_counter_t __get_rv_instret ( void  )

Read whole 64 bits value of machine instruction-retired counter.

This function will read the whole 64 bits of MINSTRET register

Returns
The whole 64 bits value of MINSTRET
Remarks
It will work for both RV32 and RV64 to get full 64bits value of MINSTRET

Definition at line 1255 of file core_feature_base.h.

1256 {
1257  __RWMB(); // Make sure previous memory and io operation finished
1258 #if __RISCV_XLEN == 32
1259 #if defined(CPU_SERIES) && CPU_SERIES == 100
1260  return __RV_CSR_READ(CSR_MINSTRET);
1261 #else
1262  volatile uint32_t high0, low, high;
1263  uint64_t full;
1264 
1265  high0 = __RV_CSR_READ(CSR_MINSTRETH);
1266  low = __RV_CSR_READ(CSR_MINSTRET);
1267  high = __RV_CSR_READ(CSR_MINSTRETH);
1268  if (high0 != high) {
1269  low = __RV_CSR_READ(CSR_MINSTRET);
1270  }
1271  full = (((uint64_t)high) << 32) | low;
1272  return full;
1273 #endif
1274 #elif __RISCV_XLEN == 64
1275  return (uint64_t)__RV_CSR_READ(CSR_MINSTRET);
1276 #else // TODO Need cover for XLEN=128 case in future
1277  return (uint64_t)__RV_CSR_READ(CSR_MINSTRET);
1278 #endif
1279 }
#define CSR_MINSTRET
#define CSR_MINSTRETH

References __RV_CSR_READ, __RWMB, CSR_MINSTRET, and CSR_MINSTRETH.

Referenced by __get_hpm_counter().

◆ __get_rv_time()

__STATIC_INLINE rv_counter_t __get_rv_time ( void  )

Read whole 64 bits value of real-time clock.

This function will read the whole 64 bits of TIME register

Returns
The whole 64 bits value of TIME CSR
Remarks
It will work for both RV32 and RV64 to get full 64bits value of TIME
Attention
only available when user mode available

Definition at line 1309 of file core_feature_base.h.

1310 {
1311  __RWMB(); // Make sure previous memory and io operation finished
1312 #if __RISCV_XLEN == 32
1313 #if defined(CPU_SERIES) && CPU_SERIES == 100
1314  // NOTE: when CSR_MIRGB_INFO CSR exist and not zero, it means eclic and systimer present
1315  if (__RV_CSR_READ(CSR_MIRGB_INFO) == 0) {
1316  return __RV_CSR_READ(CSR_MTIME);
1317  }
1318 #if defined(__SYSTIMER_PRESENT) && (__SYSTIMER_PRESENT == 1)
1319  return *(uint32_t *) (__SYSTIMER_BASEADDR);
1320 #else
1321  return 0;
1322 #endif
1323 #else
1324  volatile uint32_t high0, low, high;
1325  uint64_t full;
1326 
1327  high0 = __RV_CSR_READ(CSR_TIMEH);
1328  low = __RV_CSR_READ(CSR_TIME);
1329  high = __RV_CSR_READ(CSR_TIMEH);
1330  if (high0 != high) {
1331  low = __RV_CSR_READ(CSR_TIME);
1332  }
1333  full = (((uint64_t)high) << 32) | low;
1334  return full;
1335 #endif
1336 #elif __RISCV_XLEN == 64
1337  return (uint64_t)__RV_CSR_READ(CSR_TIME);
1338 #else // TODO Need cover for XLEN=128 case in future
1339  return (uint64_t)__RV_CSR_READ(CSR_TIME);
1340 #endif
1341 }
#define CSR_TIMEH
#define CSR_TIME
#define CSR_MIRGB_INFO
#define CSR_MTIME

References __RV_CSR_READ, __RWMB, CSR_MIRGB_INFO, CSR_MTIME, CSR_TIME, and CSR_TIMEH.

Referenced by SysTick_Config_S(), and SysTick_Reload_S().

◆ __read_cycle_csr()

__STATIC_FORCEINLINE unsigned long __read_cycle_csr ( void  )

Read the CYCLE register.

This function will read the CYCLE register without taking the CYCLEH register into account

Returns
32 bits value when XLEN=32 64 bits value when XLEN=64 TODO: XLEN=128 need to be supported

Definition at line 1351 of file core_feature_base.h.

1352 {
1353  __RWMB(); // Make sure previous memory and io operation finished
1354  return __RV_CSR_READ(CSR_CYCLE);
1355 }
#define CSR_CYCLE

References __RV_CSR_READ, __RWMB, and CSR_CYCLE.

Referenced by __read_hpm_counter().

◆ __read_instret_csr()

__STATIC_FORCEINLINE unsigned long __read_instret_csr ( void  )

Read the INSTRET register.

This function will read the INSTRET register without taking the INSTRETH register into account

Returns
32 bits value when XLEN=32 64 bits value when XLEN=64 TODO: XLEN=128 need to be supported

Definition at line 1365 of file core_feature_base.h.

1366 {
1367  __RWMB(); // Make sure previous memory and io operation finished
1368  return __RV_CSR_READ(CSR_INSTRET);
1369 }
#define CSR_INSTRET

References __RV_CSR_READ, __RWMB, and CSR_INSTRET.

Referenced by __read_hpm_counter().

◆ __read_time_csr()

__STATIC_FORCEINLINE unsigned long __read_time_csr ( void  )

Read the TIME register.

This function will read the TIME register without taking the TIMEH register into account

Returns
32 bits value when XLEN=32 64 bits value when XLEN=64 TODO: XLEN=128 need to be supported

Definition at line 1379 of file core_feature_base.h.

1380 {
1381  __RWMB(); // Make sure previous memory and io operation finished
1382  return __RV_CSR_READ(CSR_TIME);
1383 }

References __RV_CSR_READ, __RWMB, and CSR_TIME.

◆ __s_switch_mode()

__STATIC_INLINE void __s_switch_mode ( uint8_t  mode,
uintptr_t  stack,
void(*)(void)  entry_point 
)

switch privilege from supervisor mode to others.

Execute into entry_point in mode(user) with given stack

Parameters
modeprivilege mode
stackpredefined stack, size should set enough
entry_pointa function pointer to execute

Definition at line 908 of file core_feature_base.h.

909 {
910  unsigned long val = 0;
911 
912  /* Set SPP to the requested privilege mode */
913  val = __RV_CSR_READ(CSR_SSTATUS);
914  val = __RV_INSERT_FIELD(val, SSTATUS_SPP, mode);
915 
916  /* Set previous SIE disabled */
917  val = __RV_INSERT_FIELD(val, SSTATUS_SPIE, 0);
918 
920 
921  /* Set the entry point in SEPC */
922  __RV_CSR_WRITE(CSR_SEPC, (unsigned long)entry_point);
923 
924  /* Set the register file */
925  __ASM volatile("mv sp, %0" ::"r"(stack));
926 
927  __ASM volatile("sret");
928 }
#define SSTATUS_SPIE
#define SSTATUS_SPP
#define __RV_CSR_WRITE(csr, val)
CSR operation Macro for csrw instruction.
#define CSR_SEPC

References __ASM, __RV_CSR_READ, __RV_CSR_WRITE, CSR_SEPC, CSR_SSTATUS, SSTATUS_SPIE, and SSTATUS_SPP.

◆ __set_rv_cycle()

__STATIC_FORCEINLINE void __set_rv_cycle ( rv_counter_t  cycle)

Set whole 64 bits value of mcycle counter.

This function will set the whole 64 bits of MCYCLE register

Remarks
It will work for both RV32 and RV64 to set full 64bits value of MCYCLE

Definition at line 1233 of file core_feature_base.h.

1234 {
1235 #if __RISCV_XLEN == 32
1236 #if defined(CPU_SERIES) && CPU_SERIES == 100
1237  __RV_CSR_WRITE(CSR_MCYCLE, (uint32_t)(cycle));
1238 #else
1239  __RV_CSR_WRITE(CSR_MCYCLE, 0); // prevent carry
1240  __RV_CSR_WRITE(CSR_MCYCLEH, (uint32_t)(cycle >> 32));
1241  __RV_CSR_WRITE(CSR_MCYCLE, (uint32_t)(cycle));
1242 #endif
1243 #elif __RISCV_XLEN == 64
1244  __RV_CSR_WRITE(CSR_MCYCLE, cycle);
1245 #else // TODO Need cover for XLEN=128 case in future
1246 #endif
1247 }

References __RV_CSR_WRITE, CSR_MCYCLE, and CSR_MCYCLEH.

◆ __set_rv_instret()

__STATIC_FORCEINLINE void __set_rv_instret ( rv_counter_t  instret)

Set whole 64 bits value of machine instruction-retired counter.

This function will set the whole 64 bits of MINSTRET register

Remarks
It will work for both RV32 and RV64 to set full 64bits value of MINSTRET

Definition at line 1286 of file core_feature_base.h.

1287 {
1288 #if __RISCV_XLEN == 32
1289 #if defined(CPU_SERIES) && CPU_SERIES == 100
1290  __RV_CSR_WRITE(CSR_MINSTRET, (uint32_t)(instret));
1291 #else
1292  __RV_CSR_WRITE(CSR_MINSTRET, 0); // prevent carry
1293  __RV_CSR_WRITE(CSR_MINSTRETH, (uint32_t)(instret >> 32));
1294  __RV_CSR_WRITE(CSR_MINSTRET, (uint32_t)(instret));
1295 #endif
1296 #elif __RISCV_XLEN == 64
1297  __RV_CSR_WRITE(CSR_MINSTRET, instret);
1298 #else // TODO Need cover for XLEN=128 case in future
1299 #endif
1300 }

References __RV_CSR_WRITE, CSR_MINSTRET, and CSR_MINSTRETH.

◆ __switch_mode()

__STATIC_INLINE void __switch_mode ( uint8_t  mode,
uintptr_t  stack,
void(*)(void)  entry_point 
)

switch privilege from machine mode to others.

Execute into entry_point in mode(supervisor or user) with given stack

Parameters
modeprivilege mode
stackpredefined stack, size should set enough
entry_pointa function pointer to execute

Definition at line 878 of file core_feature_base.h.

879 {
880  unsigned long val = 0;
881 
882  /* Set MPP to the requested privilege mode */
883  val = __RV_CSR_READ(CSR_MSTATUS);
884  val = __RV_INSERT_FIELD(val, MSTATUS_MPP, mode);
885 
886  /* Set previous MIE disabled */
887  val = __RV_INSERT_FIELD(val, MSTATUS_MPIE, 0);
888 
890 
891  /* Set the entry point in MEPC */
892  __RV_CSR_WRITE(CSR_MEPC, (unsigned long)entry_point);
893 
894  /* Set the register file */
895  __ASM volatile("mv sp, %0" ::"r"(stack));
896 
897  __ASM volatile("mret");
898 }
#define MSTATUS_MPIE
#define MSTATUS_MPP
#define CSR_MEPC

References __ASM, __RV_CSR_READ, __RV_CSR_WRITE, CSR_MEPC, CSR_MSTATUS, MSTATUS_MPIE, and MSTATUS_MPP.