1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2016, Linaro Limited 4 * Copyright (c) 2014, STMicroelectronics International N.V. 5 */ 6 7 #ifndef ARM32_H 8 #define ARM32_H 9 10 #include <sys/cdefs.h> 11 #include <stdint.h> 12 #include <util.h> 13 14 #define CPSR_MODE_MASK ARM32_CPSR_MODE_MASK 15 #define CPSR_MODE_USR ARM32_CPSR_MODE_USR 16 #define CPSR_MODE_FIQ ARM32_CPSR_MODE_FIQ 17 #define CPSR_MODE_IRQ ARM32_CPSR_MODE_IRQ 18 #define CPSR_MODE_SVC ARM32_CPSR_MODE_SVC 19 #define CPSR_MODE_MON ARM32_CPSR_MODE_MON 20 #define CPSR_MODE_ABT ARM32_CPSR_MODE_ABT 21 #define CPSR_MODE_UND ARM32_CPSR_MODE_UND 22 #define CPSR_MODE_SYS ARM32_CPSR_MODE_SYS 23 24 #define CPSR_T ARM32_CPSR_T 25 #define CPSR_F_SHIFT ARM32_CPSR_F_SHIFT 26 #define CPSR_F ARM32_CPSR_F 27 #define CPSR_I ARM32_CPSR_I 28 #define CPSR_A ARM32_CPSR_A 29 #define CPSR_FIA ARM32_CPSR_FIA 30 #define CPSR_IT_MASK ARM32_CPSR_IT_MASK 31 #define CPSR_IT_MASK1 ARM32_CPSR_IT_MASK1 32 #define CPSR_IT_MASK2 ARM32_CPSR_IT_MASK2 33 34 #define PMCR_DP BIT32(5) 35 36 #define SCR_NS BIT32(0) 37 #define SCR_IRQ BIT32(1) 38 #define SCR_FIQ BIT32(2) 39 #define SCR_EA BIT32(3) 40 #define SCR_FW BIT32(4) 41 #define SCR_AW BIT32(5) 42 #define SCR_NET BIT32(6) 43 #define SCR_SCD BIT32(7) 44 #define SCR_HCE BIT32(8) 45 #define SCR_SIF BIT32(9) 46 47 #define SCTLR_M BIT32(0) 48 #define SCTLR_A BIT32(1) 49 #define SCTLR_C BIT32(2) 50 #define SCTLR_CP15BEN BIT32(5) 51 #define SCTLR_SW BIT32(10) 52 #define SCTLR_Z BIT32(11) 53 #define SCTLR_I BIT32(12) 54 #define SCTLR_V BIT32(13) 55 #define SCTLR_RR BIT32(14) 56 #define SCTLR_HA BIT32(17) 57 #define SCTLR_WXN BIT32(19) 58 #define SCTLR_UWXN BIT32(20) 59 #define SCTLR_FI BIT32(21) 60 #define SCTLR_VE BIT32(24) 61 #define SCTLR_EE BIT32(25) 62 #define SCTLR_NMFI BIT32(26) 63 #define SCTLR_TRE BIT32(28) 64 #define SCTLR_AFE BIT32(29) 65 #define SCTLR_TE BIT32(30) 66 67 /* Only valid for Cortex-A15 */ 68 #define ACTLR_CA15_ENABLE_INVALIDATE_BTB BIT(0) 69 /* Only valid for Cortex-A8 */ 70 #define ACTLR_CA8_ENABLE_INVALIDATE_BTB BIT(6) 71 /* Only valid for Cortex-A9 */ 72 #define ACTLR_CA9_WFLZ BIT(3) 73 74 #define ACTLR_SMP BIT32(6) 75 76 #define NSACR_CP10 BIT32(10) 77 #define NSACR_CP11 BIT32(11) 78 #define NSACR_NSD32DIS BIT32(14) 79 #define NSACR_NSASEDIS BIT32(15) 80 #define NSACR_NS_L2ERR BIT32(17) 81 #define NSACR_NS_SMP BIT32(18) 82 83 #define CPACR_ASEDIS BIT32(31) 84 #define CPACR_D32DIS BIT32(30) 85 #define CPACR_CP(co_proc, access) SHIFT_U32((access), ((co_proc) * 2)) 86 #define CPACR_CP_ACCESS_DENIED 0x0 87 #define CPACR_CP_ACCESS_PL1_ONLY 0x1 88 #define CPACR_CP_ACCESS_FULL 0x3 89 90 91 #define DACR_DOMAIN(num, perm) SHIFT_U32((perm), ((num) * 2)) 92 #define DACR_DOMAIN_PERM_NO_ACCESS 0x0 93 #define DACR_DOMAIN_PERM_CLIENT 0x1 94 #define DACR_DOMAIN_PERM_MANAGER 0x3 95 96 #define PAR_F BIT32(0) 97 #define PAR_SS BIT32(1) 98 #define PAR_LPAE BIT32(11) 99 #define PAR_PA_SHIFT 12 100 #define PAR32_PA_MASK (BIT32(20) - 1) 101 #define PAR64_PA_MASK (BIT64(28) - 1) 102 103 /* 104 * TTBCR has different register layout if LPAE is enabled or not. 105 * TTBCR.EAE == 0 => LPAE is not enabled 106 * TTBCR.EAE == 1 => LPAE is enabled 107 */ 108 #define TTBCR_EAE BIT32(31) 109 110 /* When TTBCR.EAE == 0 */ 111 #define TTBCR_PD0 BIT32(4) 112 #define TTBCR_PD1 BIT32(5) 113 114 /* When TTBCR.EAE == 1 */ 115 #define TTBCR_T0SZ_SHIFT 0 116 #define TTBCR_EPD0 BIT32(7) 117 #define TTBCR_IRGN0_SHIFT 8 118 #define TTBCR_ORGN0_SHIFT 10 119 #define TTBCR_SH0_SHIFT 12 120 #define TTBCR_T1SZ_SHIFT 16 121 #define TTBCR_A1 BIT32(22) 122 #define TTBCR_EPD1 BIT32(23) 123 #define TTBCR_IRGN1_SHIFT 24 124 #define TTBCR_ORGN1_SHIFT 26 125 #define TTBCR_SH1_SHIFT 28 126 127 /* Normal memory, Inner/Outer Non-cacheable */ 128 #define TTBCR_XRGNX_NC 0x0 129 /* Normal memory, Inner/Outer Write-Back Write-Allocate Cacheable */ 130 #define TTBCR_XRGNX_WB 0x1 131 /* Normal memory, Inner/Outer Write-Through Cacheable */ 132 #define TTBCR_XRGNX_WT 0x2 133 /* Normal memory, Inner/Outer Write-Back no Write-Allocate Cacheable */ 134 #define TTBCR_XRGNX_WBWA 0x3 135 136 /* Non-shareable */ 137 #define TTBCR_SHX_NSH 0x0 138 /* Outer Shareable */ 139 #define TTBCR_SHX_OSH 0x2 140 /* Inner Shareable */ 141 #define TTBCR_SHX_ISH 0x3 142 143 #define TTBR_ASID_MASK 0xff 144 #define TTBR_ASID_SHIFT 48 145 146 #define TLBI_MVA_SHIFT 12 147 #define TLBI_ASID_MASK 0xff 148 149 #define FSR_LPAE BIT32(9) 150 #define FSR_WNR BIT32(11) 151 152 /* Valid if FSR.LPAE is 1 */ 153 #define FSR_STATUS_MASK (BIT32(6) - 1) 154 155 /* Valid if FSR.LPAE is 0 */ 156 #define FSR_FS_MASK (BIT32(10) | (BIT32(4) - 1)) 157 158 /* ID_PFR1 bit fields */ 159 #define IDPFR1_VIRT_SHIFT 12 160 #define IDPFR1_VIRT_MASK (0xF << IDPFR1_VIRT_SHIFT) 161 #define IDPFR1_GENTIMER_SHIFT 16 162 #define IDPFR1_GENTIMER_MASK (0xF << IDPFR1_GENTIMER_SHIFT) 163 164 #ifndef __ASSEMBLER__ 165 #include <generated/arm32_sysreg.h> 166 #ifdef CFG_ARM_GICV3 167 #include <generated/arm32_gicv3_sysreg.h> 168 #endif 169 170 static inline void isb(void) 171 { 172 asm volatile ("isb"); 173 } 174 175 static inline void dsb(void) 176 { 177 asm volatile ("dsb"); 178 } 179 180 static inline void dsb_ish(void) 181 { 182 asm volatile ("dsb ish"); 183 } 184 185 static inline void dsb_ishst(void) 186 { 187 asm volatile ("dsb ishst"); 188 } 189 190 static inline void dmb(void) 191 { 192 asm volatile ("dmb"); 193 } 194 195 static inline void sev(void) 196 { 197 asm volatile ("sev"); 198 } 199 200 static inline void wfe(void) 201 { 202 asm volatile ("wfe"); 203 } 204 205 static inline uint32_t read_cpsr(void) 206 { 207 uint32_t cpsr; 208 209 asm volatile ("mrs %[cpsr], cpsr" 210 : [cpsr] "=r" (cpsr) 211 ); 212 return cpsr; 213 } 214 215 static inline void write_cpsr(uint32_t cpsr) 216 { 217 asm volatile ("msr cpsr_fsxc, %[cpsr]" 218 : : [cpsr] "r" (cpsr) 219 ); 220 } 221 222 static inline uint32_t read_spsr(void) 223 { 224 uint32_t spsr; 225 226 asm volatile ("mrs %[spsr], spsr" 227 : [spsr] "=r" (spsr) 228 ); 229 return spsr; 230 } 231 232 static inline void wfi(void) 233 { 234 asm volatile("wfi"); 235 } 236 237 static __always_inline uint32_t read_pc(void) 238 { 239 uint32_t val; 240 241 asm volatile ("adr %0, ." : "=r" (val)); 242 return val; 243 } 244 245 static __always_inline uint32_t read_sp(void) 246 { 247 uint32_t val; 248 249 asm volatile ("mov %0, sp" : "=r" (val)); 250 return val; 251 } 252 253 static __always_inline uint32_t read_lr(void) 254 { 255 uint32_t val; 256 257 asm volatile ("mov %0, lr" : "=r" (val)); 258 return val; 259 } 260 261 static __always_inline uint32_t read_fp(void) 262 { 263 uint32_t val; 264 265 asm volatile ("mov %0, fp" : "=r" (val)); 266 return val; 267 } 268 269 static __always_inline uint32_t read_r7(void) 270 { 271 uint32_t val; 272 273 asm volatile ("mov %0, r7" : "=r" (val)); 274 return val; 275 } 276 277 #endif /*__ASSEMBLER__*/ 278 279 #endif /*ARM32_H*/ 280