1*6020faf6SRick Chen /* 2*6020faf6SRick Chen * Copyright (c) 2017 Microsemi Corporation. 3*6020faf6SRick Chen * Padmarao Begari, Microsemi Corporation <padmarao.begari@microsemi.com> 4*6020faf6SRick Chen * 5*6020faf6SRick Chen * SPDX-License-Identifier: GPL-2.0+ 6*6020faf6SRick Chen */ 7*6020faf6SRick Chen 8*6020faf6SRick Chen #ifndef RISCV_CSR_ENCODING_H 9*6020faf6SRick Chen #define RISCV_CSR_ENCODING_H 10*6020faf6SRick Chen 11*6020faf6SRick Chen #define MSTATUS_UIE 0x00000001 12*6020faf6SRick Chen #define MSTATUS_SIE 0x00000002 13*6020faf6SRick Chen #define MSTATUS_HIE 0x00000004 14*6020faf6SRick Chen #define MSTATUS_MIE 0x00000008 15*6020faf6SRick Chen #define MSTATUS_UPIE 0x00000010 16*6020faf6SRick Chen #define MSTATUS_SPIE 0x00000020 17*6020faf6SRick Chen #define MSTATUS_HPIE 0x00000040 18*6020faf6SRick Chen #define MSTATUS_MPIE 0x00000080 19*6020faf6SRick Chen #define MSTATUS_SPP 0x00000100 20*6020faf6SRick Chen #define MSTATUS_HPP 0x00000600 21*6020faf6SRick Chen #define MSTATUS_MPP 0x00001800 22*6020faf6SRick Chen #define MSTATUS_FS 0x00006000 23*6020faf6SRick Chen #define MSTATUS_XS 0x00018000 24*6020faf6SRick Chen #define MSTATUS_MPRV 0x00020000 25*6020faf6SRick Chen #define MSTATUS_PUM 0x00040000 26*6020faf6SRick Chen #define MSTATUS_VM 0x1F000000 27*6020faf6SRick Chen #define MSTATUS32_SD 0x80000000 28*6020faf6SRick Chen #define MSTATUS64_SD 0x8000000000000000 29*6020faf6SRick Chen 30*6020faf6SRick Chen #define MCAUSE32_CAUSE 0x7FFFFFFF 31*6020faf6SRick Chen #define MCAUSE64_CAUSE 0x7FFFFFFFFFFFFFFF 32*6020faf6SRick Chen #define MCAUSE32_INT 0x80000000 33*6020faf6SRick Chen #define MCAUSE64_INT 0x8000000000000000 34*6020faf6SRick Chen 35*6020faf6SRick Chen #define SSTATUS_UIE 0x00000001 36*6020faf6SRick Chen #define SSTATUS_SIE 0x00000002 37*6020faf6SRick Chen #define SSTATUS_UPIE 0x00000010 38*6020faf6SRick Chen #define SSTATUS_SPIE 0x00000020 39*6020faf6SRick Chen #define SSTATUS_SPP 0x00000100 40*6020faf6SRick Chen #define SSTATUS_FS 0x00006000 41*6020faf6SRick Chen #define SSTATUS_XS 0x00018000 42*6020faf6SRick Chen #define SSTATUS_PUM 0x00040000 43*6020faf6SRick Chen #define SSTATUS32_SD 0x80000000 44*6020faf6SRick Chen #define SSTATUS64_SD 0x8000000000000000 45*6020faf6SRick Chen 46*6020faf6SRick Chen #define MIP_SSIP BIT(IRQ_S_SOFT) 47*6020faf6SRick Chen #define MIP_HSIP BIT(IRQ_H_SOFT) 48*6020faf6SRick Chen #define MIP_MSIP BIT(IRQ_M_SOFT) 49*6020faf6SRick Chen #define MIP_STIP BIT(IRQ_S_TIMER) 50*6020faf6SRick Chen #define MIP_HTIP BIT(IRQ_H_TIMER) 51*6020faf6SRick Chen #define MIP_MTIP BIT(IRQ_M_TIMER) 52*6020faf6SRick Chen #define MIP_SEIP BIT(IRQ_S_EXT) 53*6020faf6SRick Chen #define MIP_HEIP BIT(IRQ_H_EXT) 54*6020faf6SRick Chen #define MIP_MEIP BIT(IRQ_M_EXT) 55*6020faf6SRick Chen 56*6020faf6SRick Chen #define SIP_SSIP MIP_SSIP 57*6020faf6SRick Chen #define SIP_STIP MIP_STIP 58*6020faf6SRick Chen 59*6020faf6SRick Chen #define PRV_U 0 60*6020faf6SRick Chen #define PRV_S 1 61*6020faf6SRick Chen #define PRV_H 2 62*6020faf6SRick Chen #define PRV_M 3 63*6020faf6SRick Chen 64*6020faf6SRick Chen #define VM_MBARE 0 65*6020faf6SRick Chen #define VM_MBB 1 66*6020faf6SRick Chen #define VM_MBBID 2 67*6020faf6SRick Chen #define VM_SV32 8 68*6020faf6SRick Chen #define VM_SV39 9 69*6020faf6SRick Chen #define VM_SV48 10 70*6020faf6SRick Chen 71*6020faf6SRick Chen #define IRQ_S_SOFT 1 72*6020faf6SRick Chen #define IRQ_H_SOFT 2 73*6020faf6SRick Chen #define IRQ_M_SOFT 3 74*6020faf6SRick Chen #define IRQ_S_TIMER 5 75*6020faf6SRick Chen #define IRQ_H_TIMER 6 76*6020faf6SRick Chen #define IRQ_M_TIMER 7 77*6020faf6SRick Chen #define IRQ_S_EXT 9 78*6020faf6SRick Chen #define IRQ_H_EXT 10 79*6020faf6SRick Chen #define IRQ_M_EXT 11 80*6020faf6SRick Chen #define IRQ_COP 12 81*6020faf6SRick Chen #define IRQ_HOST 13 82*6020faf6SRick Chen 83*6020faf6SRick Chen #define DEFAULT_RSTVEC 0x00001000 84*6020faf6SRick Chen #define DEFAULT_NMIVEC 0x00001004 85*6020faf6SRick Chen #define DEFAULT_MTVEC 0x00001010 86*6020faf6SRick Chen #define CONFIG_STRING_ADDR 0x0000100C 87*6020faf6SRick Chen #define EXT_IO_BASE 0x40000000 88*6020faf6SRick Chen #define DRAM_BASE 0x80000000 89*6020faf6SRick Chen 90*6020faf6SRick Chen // page table entry (PTE) fields 91*6020faf6SRick Chen #define PTE_V 0x001 // Valid 92*6020faf6SRick Chen #define PTE_TYPE 0x01E // Type 93*6020faf6SRick Chen #define PTE_R 0x020 // Referenced 94*6020faf6SRick Chen #define PTE_D 0x040 // Dirty 95*6020faf6SRick Chen #define PTE_SOFT 0x380 // Reserved for Software 96*6020faf6SRick Chen 97*6020faf6SRick Chen #define PTE_TYPE_TABLE 0x00 98*6020faf6SRick Chen #define PTE_TYPE_TABLE_GLOBAL 0x02 99*6020faf6SRick Chen #define PTE_TYPE_URX_SR 0x04 100*6020faf6SRick Chen #define PTE_TYPE_URWX_SRW 0x06 101*6020faf6SRick Chen #define PTE_TYPE_UR_SR 0x08 102*6020faf6SRick Chen #define PTE_TYPE_URW_SRW 0x0A 103*6020faf6SRick Chen #define PTE_TYPE_URX_SRX 0x0C 104*6020faf6SRick Chen #define PTE_TYPE_URWX_SRWX0x0E 105*6020faf6SRick Chen #define PTE_TYPE_SR 0x10 106*6020faf6SRick Chen #define PTE_TYPE_SRW 0x12 107*6020faf6SRick Chen #define PTE_TYPE_SRX 0x14 108*6020faf6SRick Chen #define PTE_TYPE_SRWX 0x16 109*6020faf6SRick Chen #define PTE_TYPE_SR_GLOBAL 0x18 110*6020faf6SRick Chen #define PTE_TYPE_SRW_GLOBAL 0x1A 111*6020faf6SRick Chen #define PTE_TYPE_SRX_GLOBAL 0x1C 112*6020faf6SRick Chen #define PTE_TYPE_SRWX_GLOBAL 0x1E 113*6020faf6SRick Chen 114*6020faf6SRick Chen #define PTE_PPN_SHIFT 10 115*6020faf6SRick Chen 116*6020faf6SRick Chen #define PTE_TABLE(PTE) ((0x0000000AU >> ((PTE) & 0x1F)) & 1) 117*6020faf6SRick Chen #define PTE_UR(PTE) ((0x0000AAA0U >> ((PTE) & 0x1F)) & 1) 118*6020faf6SRick Chen #define PTE_UW(PTE) ((0x00008880U >> ((PTE) & 0x1F)) & 1) 119*6020faf6SRick Chen #define PTE_UX(PTE) ((0x0000A0A0U >> ((PTE) & 0x1F)) & 1) 120*6020faf6SRick Chen #define PTE_SR(PTE) ((0xAAAAAAA0U >> ((PTE) & 0x1F)) & 1) 121*6020faf6SRick Chen #define PTE_SW(PTE) ((0x88888880U >> ((PTE) & 0x1F)) & 1) 122*6020faf6SRick Chen #define PTE_SX(PTE) ((0xA0A0A000U >> ((PTE) & 0x1F)) & 1) 123*6020faf6SRick Chen 124*6020faf6SRick Chen #define PTE_CHECK_PERM(PTE, SUPERVISOR, STORE, FETCH) \ 125*6020faf6SRick Chen ((STORE) ? ((SUPERVISOR) ? PTE_SW(PTE) : PTE_UW(PTE)) : \ 126*6020faf6SRick Chen (FETCH) ? ((SUPERVISOR) ? PTE_SX(PTE) : PTE_UX(PTE)) : \ 127*6020faf6SRick Chen ((SUPERVISOR) ? PTE_SR(PTE) : PTE_UR(PTE))) 128*6020faf6SRick Chen 129*6020faf6SRick Chen #ifdef __riscv 130*6020faf6SRick Chen #ifdef CONFIG_64BIT 131*6020faf6SRick Chen # define MSTATUS_SD MSTATUS64_SD 132*6020faf6SRick Chen # define SSTATUS_SD SSTATUS64_SD 133*6020faf6SRick Chen # define MCAUSE_INT MCAUSE64_INT 134*6020faf6SRick Chen # define MCAUSE_CAUSE MCAUSE64_CAUSE 135*6020faf6SRick Chen # define RISCV_PGLEVEL_BITS 9 136*6020faf6SRick Chen #else 137*6020faf6SRick Chen # define MSTATUS_SD MSTATUS32_SD 138*6020faf6SRick Chen # define SSTATUS_SD SSTATUS32_SD 139*6020faf6SRick Chen # define RISCV_PGLEVEL_BITS 10 140*6020faf6SRick Chen # define MCAUSE_INT MCAUSE32_INT 141*6020faf6SRick Chen # define MCAUSE_CAUSE MCAUSE32_CAUSE 142*6020faf6SRick Chen #endif 143*6020faf6SRick Chen #define RISCV_PGSHIFT 12 144*6020faf6SRick Chen #define RISCV_PGSIZE BIT(RISCV_PGSHIFT) 145*6020faf6SRick Chen 146*6020faf6SRick Chen #ifndef __ASSEMBLER__ 147*6020faf6SRick Chen 148*6020faf6SRick Chen #ifdef __GNUC__ 149*6020faf6SRick Chen 150*6020faf6SRick Chen #define read_csr(reg) ({ unsigned long __tmp; \ 151*6020faf6SRick Chen asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \ 152*6020faf6SRick Chen __tmp; }) 153*6020faf6SRick Chen 154*6020faf6SRick Chen #define write_csr(reg, val) ({ \ 155*6020faf6SRick Chen if (__builtin_constant_p(val) && (unsigned long)(val) < 32) \ 156*6020faf6SRick Chen asm volatile ("csrw " #reg ", %0" :: "i"(val)); \ 157*6020faf6SRick Chen else \ 158*6020faf6SRick Chen asm volatile ("csrw " #reg ", %0" :: "r"(val)); }) 159*6020faf6SRick Chen 160*6020faf6SRick Chen #define swap_csr(reg, val) ({ unsigned long __tmp; \ 161*6020faf6SRick Chen if (__builtin_constant_p(val) && (unsigned long)(val) < 32) \ 162*6020faf6SRick Chen asm volatile ("csrrw %0, " #reg ", %1" : "=r"(__tmp) : "i"(val)); \ 163*6020faf6SRick Chen else \ 164*6020faf6SRick Chen asm volatile ("csrrw %0, " #reg ", %1" : "=r"(__tmp) : "r"(val)); \ 165*6020faf6SRick Chen __tmp; }) 166*6020faf6SRick Chen 167*6020faf6SRick Chen #define set_csr(reg, bit) ({ unsigned long __tmp; \ 168*6020faf6SRick Chen if (__builtin_constant_p(bit) && (unsigned long)(bit) < 32) \ 169*6020faf6SRick Chen asm volatile ("csrrs %0, " #reg ", %1" : "=r"(__tmp) : "i"(bit)); \ 170*6020faf6SRick Chen else \ 171*6020faf6SRick Chen asm volatile ("csrrs %0, " #reg ", %1" : "=r"(__tmp) : "r"(bit)); \ 172*6020faf6SRick Chen __tmp; }) 173*6020faf6SRick Chen 174*6020faf6SRick Chen #define clear_csr(reg, bit) ({ unsigned long __tmp; \ 175*6020faf6SRick Chen if (__builtin_constant_p(bit) && (unsigned long)(bit) < 32) \ 176*6020faf6SRick Chen asm volatile ("csrrc %0, " #reg ", %1" : "=r"(__tmp) : "i"(bit)); \ 177*6020faf6SRick Chen else \ 178*6020faf6SRick Chen asm volatile ("csrrc %0, " #reg ", %1" : "=r"(__tmp) : "r"(bit)); \ 179*6020faf6SRick Chen __tmp; }) 180*6020faf6SRick Chen 181*6020faf6SRick Chen #define rdtime() read_csr(time) 182*6020faf6SRick Chen #define rdcycle() read_csr(cycle) 183*6020faf6SRick Chen #define rdinstret() read_csr(instret) 184*6020faf6SRick Chen 185*6020faf6SRick Chen #endif 186*6020faf6SRick Chen #endif 187*6020faf6SRick Chen #endif 188*6020faf6SRick Chen #endif 189