Home | History | Annotate | Line # | Download | only in include
sysreg.h revision 1.25
      1  1.25    simonb /* $NetBSD: sysreg.h,v 1.25 2022/11/15 14:33:33 simonb Exp $ */
      2   1.4      maxv 
      3   1.4      maxv /*
      4   1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5   1.1      matt  * All rights reserved.
      6   1.1      matt  *
      7   1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9   1.1      matt  *
     10   1.1      matt  * Redistribution and use in source and binary forms, with or without
     11   1.1      matt  * modification, are permitted provided that the following conditions
     12   1.1      matt  * are met:
     13   1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14   1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15   1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      matt  *    documentation and/or other materials provided with the distribution.
     18   1.1      matt  *
     19   1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      matt  */
     31   1.1      matt 
     32   1.1      matt #ifndef _RISCV_SYSREG_H_
     33  1.15    simonb #define	_RISCV_SYSREG_H_
     34   1.1      matt 
     35   1.1      matt #ifndef _KERNEL
     36   1.1      matt #include <sys/param.h>
     37   1.1      matt #endif
     38   1.1      matt 
     39  1.13     skrll #include <riscv/reg.h>
     40  1.13     skrll 
     41  1.15    simonb #define	FCSR_FMASK	0	// no exception bits
     42  1.15    simonb #define	FCSR_FRM	__BITS(7,5)
     43  1.15    simonb #define	 FCSR_FRM_RNE	0b000	// Round Nearest, ties to Even
     44  1.15    simonb #define	 FCSR_FRM_RTZ	0b001	// Round Towards Zero
     45  1.15    simonb #define	 FCSR_FRM_RDN	0b010	// Round DowN (-infinity)
     46  1.15    simonb #define	 FCSR_FRM_RUP	0b011	// Round UP (+infinity)
     47  1.15    simonb #define	 FCSR_FRM_RMM	0b100	// Round to nearest, ties to Max Magnitude
     48  1.15    simonb #define	 FCSR_FRM_DYN	0b111	// Dynamic rounding
     49  1.15    simonb #define	FCSR_FFLAGS	__BITS(4,0)	// Sticky bits
     50  1.15    simonb #define	FCSR_NV		__BIT(4)	// iNValid operation
     51  1.15    simonb #define	FCSR_DZ		__BIT(3)	// Divide by Zero
     52  1.15    simonb #define	FCSR_OF		__BIT(2)	// OverFlow
     53  1.15    simonb #define	FCSR_UF		__BIT(1)	// UnderFlow
     54  1.15    simonb #define	FCSR_NX		__BIT(0)	// iNeXact
     55   1.1      matt 
     56   1.1      matt static inline uint32_t
     57   1.1      matt riscvreg_fcsr_read(void)
     58   1.1      matt {
     59   1.1      matt 	uint32_t __fcsr;
     60   1.1      matt 	__asm("frcsr %0" : "=r"(__fcsr));
     61   1.1      matt 	return __fcsr;
     62   1.1      matt }
     63   1.1      matt 
     64   1.1      matt 
     65   1.1      matt static inline uint32_t
     66   1.1      matt riscvreg_fcsr_write(uint32_t __new)
     67   1.1      matt {
     68   1.1      matt 	uint32_t __old;
     69   1.1      matt 	__asm("fscsr %0, %1" : "=r"(__old) : "r"(__new));
     70   1.1      matt 	return __old;
     71   1.1      matt }
     72   1.1      matt 
     73   1.1      matt static inline uint32_t
     74   1.1      matt riscvreg_fcsr_read_fflags(void)
     75   1.1      matt {
     76   1.1      matt 	uint32_t __old;
     77   1.1      matt 	__asm("frflags %0" : "=r"(__old));
     78   1.1      matt 	return __SHIFTOUT(__old, FCSR_FFLAGS);
     79   1.1      matt }
     80   1.1      matt 
     81   1.1      matt static inline uint32_t
     82   1.1      matt riscvreg_fcsr_write_fflags(uint32_t __new)
     83   1.1      matt {
     84   1.1      matt 	uint32_t __old;
     85   1.1      matt 	__new = __SHIFTIN(__new, FCSR_FFLAGS);
     86   1.1      matt 	__asm("fsflags %0, %1" : "=r"(__old) : "r"(__new));
     87   1.1      matt 	return __SHIFTOUT(__old, FCSR_FFLAGS);
     88   1.1      matt }
     89   1.1      matt 
     90   1.1      matt static inline uint32_t
     91   1.1      matt riscvreg_fcsr_read_frm(void)
     92   1.1      matt {
     93   1.1      matt 	uint32_t __old;
     94   1.1      matt 	__asm("frrm\t%0" : "=r"(__old));
     95   1.1      matt 	return __SHIFTOUT(__old, FCSR_FRM);
     96   1.1      matt }
     97   1.1      matt 
     98   1.1      matt static inline uint32_t
     99   1.1      matt riscvreg_fcsr_write_frm(uint32_t __new)
    100   1.1      matt {
    101   1.1      matt 	uint32_t __old;
    102   1.1      matt 	__new = __SHIFTIN(__new, FCSR_FRM);
    103  1.17    simonb 	__asm __volatile("fsrm\t%0, %1" : "=r"(__old) : "r"(__new));
    104   1.1      matt 	return __SHIFTOUT(__old, FCSR_FRM);
    105   1.1      matt }
    106   1.1      matt 
    107  1.25    simonb 
    108  1.25    simonb #define	RISCVREG_READ_INLINE(regname)					\
    109  1.25    simonb static inline uintptr_t							\
    110  1.25    simonb csr_##regname##_read(void)						\
    111  1.25    simonb {									\
    112  1.25    simonb 	uintptr_t __rv;							\
    113  1.25    simonb 	asm volatile("csrr %0, " #regname : "=r"(__rv) :: "memory");	\
    114  1.25    simonb 	return __rv;							\
    115  1.25    simonb }
    116  1.25    simonb 
    117  1.25    simonb #define	RISCVREG_WRITE_INLINE(regname)					\
    118  1.25    simonb static inline void							\
    119  1.25    simonb csr_##regname##_write(uintptr_t __val)					\
    120  1.25    simonb {									\
    121  1.25    simonb 	asm volatile("csrw " #regname ", %0" :: "r"(__val) : "memory");	\
    122  1.25    simonb }
    123  1.25    simonb 
    124  1.25    simonb #define	RISCVREG_SET_INLINE(regname)					\
    125  1.25    simonb static inline void							\
    126  1.25    simonb csr_##regname##_set(uintptr_t __mask)					\
    127  1.25    simonb {									\
    128  1.25    simonb 	if (__builtin_constant_p(__mask) && __mask < 0x20) {		\
    129  1.25    simonb 		asm volatile("csrsi " #regname ", %0" :: "i"(__mask) :	\
    130  1.25    simonb 		    "memory");						\
    131  1.25    simonb 	} else {							\
    132  1.25    simonb 		asm volatile("csrs " #regname ", %0" :: "r"(__mask) :	\
    133  1.25    simonb 		    "memory");						\
    134  1.25    simonb 	}								\
    135  1.25    simonb }
    136  1.25    simonb 
    137  1.25    simonb #define	RISCVREG_CLEAR_INLINE(regname)					\
    138  1.25    simonb static inline void							\
    139  1.25    simonb csr_##regname##_clear(uintptr_t __mask)					\
    140  1.25    simonb {									\
    141  1.25    simonb 	if (__builtin_constant_p(__mask) && __mask < 0x20) {		\
    142  1.25    simonb 		asm volatile("csrci " #regname ", %0" :: "i"(__mask) :	\
    143  1.25    simonb 		    "memory");						\
    144  1.25    simonb 	} else {							\
    145  1.25    simonb 		asm volatile("csrc " #regname ", %0" :: "r"(__mask) :	\
    146  1.25    simonb 		    "memory");						\
    147  1.25    simonb 	}								\
    148  1.25    simonb }
    149  1.25    simonb 
    150  1.25    simonb #define	RISCVREG_READ_WRITE_INLINE(regname)				\
    151  1.25    simonb RISCVREG_READ_INLINE(regname)						\
    152  1.25    simonb RISCVREG_WRITE_INLINE(regname)
    153  1.25    simonb #define	RISCVREG_SET_CLEAR_INLINE(regname)				\
    154  1.25    simonb RISCVREG_SET_INLINE(regname)						\
    155  1.25    simonb RISCVREG_CLEAR_INLINE(regname)
    156  1.25    simonb #define	RISCVREG_READ_SET_CLEAR_INLINE(regname)				\
    157  1.25    simonb RISCVREG_READ_INLINE(regname)						\
    158  1.25    simonb RISCVREG_SET_CLEAR_INLINE(regname)
    159  1.25    simonb #define	RISCVREG_READ_WRITE_SET_CLEAR_INLINE(regname)			\
    160  1.25    simonb RISCVREG_READ_WRITE_INLINE(regname)					\
    161  1.25    simonb RISCVREG_SET_CLEAR_INLINE(regname)
    162  1.25    simonb 
    163   1.9     skrll /* Supervisor Status Register */
    164  1.25    simonb RISCVREG_READ_SET_CLEAR_INLINE(sstatus)		// supervisor status register
    165   1.9     skrll #ifdef _LP64
    166  1.15    simonb #define	SR_WPRI		__BITS(62, 34) | __BITS(31,20) | __BIT(17) | \
    167   1.9     skrll 			    __BITS(12,9) | __BITS(7,6) | __BITS(3,2)
    168  1.15    simonb #define	SR_SD		__BIT(63)
    169   1.9     skrll 			/* Bits 62-34 are WPRI */
    170  1.15    simonb #define	SR_UXL		__BITS(33,32)
    171  1.15    simonb #define	 SR_UXL_32	1
    172  1.15    simonb #define	 SR_UXL_64	2
    173  1.15    simonb #define	 SR_UXL_128	3
    174   1.9     skrll 			/* Bits 31-20 are WPRI*/
    175   1.9     skrll #else
    176  1.15    simonb #define	SR_WPRI		__BITS(30,20) | __BIT(17) | __BITS(12,9) | \
    177   1.9     skrll 			    __BITS(7,6) | __BITS(3,2)
    178  1.15    simonb #define	SR_SD		__BIT(31)
    179   1.9     skrll 			/* Bits 30-20 are WPRI*/
    180   1.9     skrll #endif /* _LP64 */
    181   1.9     skrll 
    182   1.9     skrll /* Both RV32 and RV64 have the bottom 20 bits shared */
    183  1.15    simonb #define	SR_MXR		__BIT(19)
    184  1.15    simonb #define	SR_SUM		__BIT(18)
    185   1.9     skrll 			/* Bit 17 is WPRI */
    186  1.15    simonb #define	SR_XS		__BITS(16,15)
    187  1.15    simonb #define	SR_FS		__BITS(14,13)
    188  1.15    simonb #define	 SR_FS_OFF	0
    189  1.15    simonb #define	 SR_FS_INITIAL	1
    190  1.15    simonb #define	 SR_FS_CLEAN	2
    191  1.15    simonb #define	 SR_FS_DIRTY	3
    192   1.9     skrll 
    193   1.9     skrll 			/* Bits 12-9 are WPRI */
    194  1.15    simonb #define	SR_SPP		__BIT(8)
    195   1.9     skrll 			/* Bits 7-6 are WPRI */
    196  1.15    simonb #define	SR_SPIE		__BIT(5)
    197  1.15    simonb #define	SR_UPIE		__BIT(4)
    198   1.9     skrll 			/* Bits 3-2 are WPRI */
    199  1.15    simonb #define	SR_SIE		__BIT(1)
    200  1.15    simonb #define	SR_UIE		__BIT(0)
    201   1.9     skrll 
    202   1.9     skrll /* Supervisor interrupt registers */
    203  1.11  christos /* ... interrupt pending register (sip) */
    204  1.25    simonb RISCVREG_READ_SET_CLEAR_INLINE(sip)		// supervisor interrupt pending
    205  1.19     skrll 			/* Bit (XLEN-1) - 10 is WIRI */
    206  1.15    simonb #define	SIP_SEIP	__BIT(9)
    207  1.15    simonb #define	SIP_UEIP	__BIT(8)
    208   1.9     skrll 			/* Bit 7-6 is WIRI */
    209  1.15    simonb #define	SIP_STIP	__BIT(5)
    210  1.15    simonb #define	SIP_UTIP	__BIT(4)
    211   1.9     skrll 			/* Bit 3-2 is WIRI */
    212  1.15    simonb #define	SIP_SSIP	__BIT(1)
    213  1.15    simonb #define	SIP_USIP	__BIT(0)
    214   1.9     skrll 
    215  1.11  christos /* ... interrupt-enable register (sie) */
    216  1.25    simonb RISCVREG_READ_SET_CLEAR_INLINE(sie)		// supervisor interrupt enable
    217   1.9     skrll 			/* Bit (XLEN-1) - 10 is WIRI */
    218  1.15    simonb #define	SIE_SEIE	__BIT(9)
    219  1.15    simonb #define	SIE_UEIE	__BIT(8)
    220   1.9     skrll 			/* Bit 7-6 is WIRI */
    221  1.15    simonb #define	SIE_STIE	__BIT(5)
    222  1.15    simonb #define	SIE_UTIE	__BIT(4)
    223   1.9     skrll 			/* Bit 3-2 is WIRI */
    224  1.15    simonb #define	SIE_SSIE	__BIT(1)
    225  1.15    simonb #define	SIE_USIE	__BIT(0)
    226   1.9     skrll 
    227   1.9     skrll /* Mask for all interrupts */
    228  1.15    simonb #define	SIE_IM		(SIE_SEI|SIE_UEIE|SIE_STIE|SIE_UTIE|SIE_SSIE|SIE_USIE)
    229   1.1      matt 
    230   1.1      matt #ifdef _LP64
    231  1.10     skrll #define	SR_USER		(SR_UIE)
    232  1.10     skrll #define	SR_USER32	(SR_USER)
    233  1.10     skrll #define	SR_KERNEL	(SR_SIE | SR_UIE)
    234   1.1      matt #else
    235  1.10     skrll #define	SR_USER		(SR_UIE)
    236  1.10     skrll #define	SR_KERNEL	(SR_SIE | SR_UIE)
    237   1.1      matt #endif
    238   1.1      matt 
    239   1.1      matt // Cause register
    240  1.21    simonb #define	CAUSE_INTERRUPT_P(cause)	((cause) & __BIT(XLEN-1))
    241  1.21    simonb #define	CAUSE_CODE(cause)		((cause) & __BITS(XLEN-2, 0))
    242  1.20    simonb 
    243  1.20    simonb // Cause register - exceptions
    244  1.15    simonb #define	CAUSE_FETCH_MISALIGNED		0
    245  1.15    simonb #define	CAUSE_FETCH_ACCESS		1
    246  1.15    simonb #define	CAUSE_ILLEGAL_INSTRUCTION	2
    247  1.15    simonb #define	CAUSE_BREAKPOINT		3
    248  1.15    simonb #define	CAUSE_LOAD_MISALIGNED		4
    249  1.15    simonb #define	CAUSE_LOAD_ACCESS		5
    250  1.15    simonb #define	CAUSE_STORE_MISALIGNED		6
    251  1.15    simonb #define	CAUSE_STORE_ACCESS		7
    252  1.15    simonb #define	CAUSE_USER_ECALL		8
    253  1.20    simonb #define	CAUSE_SYSCALL			CAUSE_USER_ECALL /* convenience alias */
    254  1.15    simonb #define	CAUSE_SUPERVISOR_ECALL		9
    255   1.6     skrll /* 10 is reserved */
    256  1.15    simonb #define	CAUSE_MACHINE_ECALL		11
    257  1.15    simonb #define	CAUSE_FETCH_PAGE_FAULT		12
    258  1.15    simonb #define	CAUSE_LOAD_PAGE_FAULT		13
    259   1.6     skrll /* 14 is Reserved */
    260  1.15    simonb #define	CAUSE_STORE_PAGE_FAULT		15
    261  1.20    simonb /* >= 16 is reserved/custom */
    262  1.20    simonb 
    263  1.24     skrll // Cause register - interrupts
    264  1.20    simonb #define	IRQ_SUPERVISOR_SOFTWARE	1
    265  1.20    simonb #define	IRQ_MACHINE_SOFTWARE	3
    266  1.20    simonb #define	IRQ_SUPERVISOR_TIMER	5
    267  1.20    simonb #define	IRQ_MACHINE_TIMER	7
    268  1.20    simonb #define	IRQ_SUPERVISOR_EXTERNAL	9
    269  1.20    simonb #define	IRQ_MACHINE_EXTERNAL	11
    270   1.1      matt 
    271  1.25    simonb RISCVREG_READ_INLINE(time)
    272  1.25    simonb #ifdef _LP64
    273  1.25    simonb RISCVREG_READ_INLINE(cycle)
    274  1.25    simonb #else /* !_LP64 */
    275   1.1      matt static inline uint64_t
    276  1.25    simonb csr_cycle_read(void)
    277   1.1      matt {
    278   1.1      matt 	uint32_t __hi0, __hi1, __lo0;
    279   1.1      matt 	do {
    280   1.1      matt 		__asm __volatile(
    281   1.5     skrll 			"csrr\t%[__hi0], cycleh"
    282   1.1      matt 		"\n\t"	"csrr\t%[__lo0], cycle"
    283   1.1      matt 		"\n\t"	"csrr\t%[__hi1], cycleh"
    284   1.1      matt 		   :	[__hi0] "=r"(__hi0),
    285   1.1      matt 			[__lo0] "=r"(__lo0),
    286   1.1      matt 			[__hi1] "=r"(__hi1));
    287   1.1      matt 	} while (__hi0 != __hi1);
    288   1.1      matt 	return ((uint64_t)__hi0 << 32) | (uint64_t)__lo0;
    289   1.1      matt }
    290  1.25    simonb #endif /* !_LP64 */
    291   1.1      matt 
    292   1.4      maxv #ifdef _LP64
    293  1.15    simonb #define	SATP_MODE		__BITS(63,60)
    294  1.16    simonb #define	 SATP_MODE_BARE		0
    295  1.15    simonb #define	 SATP_MODE_SV39		8
    296  1.15    simonb #define	 SATP_MODE_SV48		9
    297  1.16    simonb #define	 SATP_MODE_SV57		10
    298  1.18     skrll #define	 SATP_MODE_SV64		11
    299  1.15    simonb #define	SATP_ASID		__BITS(59,44)
    300  1.15    simonb #define	SATP_PPN		__BITS(43,0)
    301   1.4      maxv #else
    302  1.15    simonb #define	SATP_MODE		__BIT(31)
    303  1.16    simonb #define	 SATP_MODE_BARE		0
    304  1.15    simonb #define	 SATP_MODE_SV32		1
    305  1.15    simonb #define	SATP_ASID		__BITS(30,22)
    306  1.15    simonb #define	SATP_PPN		__BITS(21,0)
    307   1.4      maxv #endif
    308   1.2      matt 
    309  1.25    simonb RISCVREG_READ_WRITE_INLINE(satp)
    310  1.13     skrll 
    311  1.25    simonb /* Fake "ASID" CSR (a field of SATP register) functions */
    312   1.2      matt static inline uint32_t
    313  1.25    simonb csr_asid_read(void)
    314   1.2      matt {
    315  1.25    simonb 	uintptr_t satp = csr_satp_read();
    316   1.4      maxv 	return __SHIFTOUT(satp, SATP_ASID);
    317   1.2      matt }
    318   1.2      matt 
    319   1.2      matt static inline void
    320  1.25    simonb csr_asid_write(uint32_t asid)
    321   1.2      matt {
    322   1.4      maxv 	uintptr_t satp;
    323  1.25    simonb 
    324  1.25    simonb 	satp = csr_satp_read();
    325   1.4      maxv 	satp &= ~SATP_ASID;
    326  1.14     skrll 	satp |= __SHIFTIN(asid, SATP_ASID);
    327  1.25    simonb 	csr_satp_write(satp);
    328   1.2      matt }
    329   1.2      matt 
    330   1.1      matt #endif /* _RISCV_SYSREG_H_ */
    331