Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef __ARCH_HELPERS_H__ |
| 8 | #define __ARCH_HELPERS_H__ |
| 9 | |
| 10 | #include <arch.h> /* for additional register definitions */ |
| 11 | #include <misc_utils.h> |
| 12 | #include <stdint.h> |
| 13 | #include <types.h> |
| 14 | |
| 15 | /********************************************************************** |
| 16 | * Macros which create inline functions to read or write CPU system |
| 17 | * registers |
| 18 | *********************************************************************/ |
| 19 | |
| 20 | #define _DEFINE_COPROCR_WRITE_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \ |
| 21 | static inline void write_## _name(u_register_t v) \ |
| 22 | { \ |
| 23 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 24 | } |
| 25 | |
| 26 | #define _DEFINE_COPROCR_READ_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \ |
| 27 | static inline u_register_t read_ ## _name(void) \ |
| 28 | { \ |
| 29 | u_register_t v; \ |
| 30 | __asm__ volatile ("mrc "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : "=r" (v));\ |
| 31 | return v; \ |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * The undocumented %Q and %R extended asm are used to implemented the below |
| 36 | * 64 bit `mrrc` and `mcrr` instructions. |
| 37 | */ |
| 38 | |
| 39 | #define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \ |
| 40 | static inline void write64_## _name(uint64_t v) \ |
| 41 | { \ |
| 42 | __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\ |
| 43 | } |
| 44 | |
| 45 | #define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \ |
| 46 | static inline uint64_t read64_## _name(void) \ |
| 47 | { uint64_t v; \ |
| 48 | __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\ |
| 49 | return v; \ |
| 50 | } |
| 51 | |
| 52 | #define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \ |
| 53 | static inline u_register_t read_ ## _name(void) \ |
| 54 | { \ |
| 55 | u_register_t v; \ |
| 56 | __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \ |
| 57 | return v; \ |
| 58 | } |
| 59 | |
| 60 | #define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \ |
| 61 | static inline void write_ ## _name(u_register_t v) \ |
| 62 | { \ |
| 63 | __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \ |
| 64 | } |
| 65 | |
| 66 | #define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \ |
| 67 | static inline void write_ ## _name(const u_register_t v) \ |
| 68 | { \ |
| 69 | __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \ |
| 70 | } |
| 71 | |
| 72 | /* Define read function for coproc register */ |
| 73 | #define DEFINE_COPROCR_READ_FUNC(_name, ...) \ |
| 74 | _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) |
| 75 | |
| 76 | /* Define read & write function for coproc register */ |
| 77 | #define DEFINE_COPROCR_RW_FUNCS(_name, ...) \ |
| 78 | _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) \ |
| 79 | _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__) |
| 80 | |
| 81 | /* Define 64 bit read function for coproc register */ |
| 82 | #define DEFINE_COPROCR_READ_FUNC_64(_name, ...) \ |
| 83 | _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) |
| 84 | |
| 85 | /* Define 64 bit write function for coproc register */ |
| 86 | #define DEFINE_COPROCR_WRITE_FUNC_64(_name, ...) \ |
| 87 | _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__) |
| 88 | |
| 89 | /* Define 64 bit read & write function for coproc register */ |
| 90 | #define DEFINE_COPROCR_RW_FUNCS_64(_name, ...) \ |
| 91 | _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) \ |
| 92 | _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__) |
| 93 | |
| 94 | /* Define read & write function for system register */ |
| 95 | #define DEFINE_SYSREG_RW_FUNCS(_name) \ |
| 96 | _DEFINE_SYSREG_READ_FUNC(_name, _name) \ |
| 97 | _DEFINE_SYSREG_WRITE_FUNC(_name, _name) |
| 98 | |
| 99 | /********************************************************************** |
| 100 | * Macros to create inline functions for tlbi operations |
| 101 | *********************************************************************/ |
| 102 | |
| 103 | #define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 104 | static inline void tlbi##_op(void) \ |
| 105 | { \ |
| 106 | u_register_t v = 0; \ |
| 107 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 108 | } |
| 109 | |
| 110 | #define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 111 | static inline void tlbi##_op(u_register_t v) \ |
| 112 | { \ |
| 113 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 114 | } |
| 115 | |
| 116 | #define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 117 | static inline void bpi##_op(void) \ |
| 118 | { \ |
| 119 | u_register_t v = 0; \ |
| 120 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 121 | } |
| 122 | |
| 123 | /* Define function for simple TLBI operation */ |
| 124 | #define DEFINE_TLBIOP_FUNC(_op, ...) \ |
| 125 | _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__) |
| 126 | |
| 127 | /* Define function for TLBI operation with register parameter */ |
| 128 | #define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \ |
| 129 | _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__) |
| 130 | |
| 131 | /* Define function for simple BPI operation */ |
| 132 | #define DEFINE_BPIOP_FUNC(_op, ...) \ |
| 133 | _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__) |
| 134 | |
| 135 | /********************************************************************** |
| 136 | * Macros to create inline functions for DC operations |
| 137 | *********************************************************************/ |
| 138 | #define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 139 | static inline void dc##_op(u_register_t v) \ |
| 140 | { \ |
| 141 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 142 | } |
| 143 | |
| 144 | /* Define function for DC operation with register parameter */ |
| 145 | #define DEFINE_DCOP_PARAM_FUNC(_op, ...) \ |
| 146 | _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__) |
| 147 | |
| 148 | /********************************************************************** |
| 149 | * Macros to create inline functions for system instructions |
| 150 | *********************************************************************/ |
| 151 | /* Define function for simple system instruction */ |
| 152 | #define DEFINE_SYSOP_FUNC(_op) \ |
| 153 | static inline void _op(void) \ |
| 154 | { \ |
| 155 | __asm__ (#_op); \ |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* Define function for system instruction with type specifier */ |
| 160 | #define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \ |
| 161 | static inline void _op ## _type(void) \ |
| 162 | { \ |
| 163 | __asm__ (#_op " " #_type); \ |
| 164 | } |
| 165 | |
| 166 | /* Define function for system instruction with register parameter */ |
| 167 | #define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \ |
| 168 | static inline void _op ## _type(u_register_t v) \ |
| 169 | { \ |
| 170 | __asm__ (#_op " " #_type ", %0" : : "r" (v)); \ |
| 171 | } |
| 172 | |
| 173 | void flush_dcache_range(uintptr_t addr, size_t size); |
| 174 | void clean_dcache_range(uintptr_t addr, size_t size); |
| 175 | void inv_dcache_range(uintptr_t addr, size_t size); |
| 176 | |
| 177 | void dcsw_op_louis(u_register_t op_type); |
| 178 | void dcsw_op_all(u_register_t op_type); |
| 179 | |
| 180 | DEFINE_SYSOP_FUNC(wfi) |
| 181 | DEFINE_SYSOP_FUNC(wfe) |
| 182 | DEFINE_SYSOP_FUNC(sev) |
| 183 | DEFINE_SYSOP_TYPE_FUNC(dsb, sy) |
| 184 | DEFINE_SYSOP_TYPE_FUNC(dmb, sy) |
| 185 | DEFINE_SYSOP_TYPE_FUNC(dsb, ish) |
| 186 | DEFINE_SYSOP_TYPE_FUNC(dsb, ishst) |
| 187 | DEFINE_SYSOP_TYPE_FUNC(dmb, ish) |
| 188 | DEFINE_SYSOP_TYPE_FUNC(dmb, ishst) |
| 189 | DEFINE_SYSOP_FUNC(isb) |
| 190 | |
| 191 | DEFINE_SYSREG_RW_FUNCS(spsr) |
| 192 | DEFINE_SYSREG_RW_FUNCS(cpsr) |
| 193 | |
| 194 | /******************************************************************************* |
| 195 | * System register accessor prototypes |
| 196 | ******************************************************************************/ |
| 197 | DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR) |
| 198 | DEFINE_COPROCR_READ_FUNC(midr, MIDR) |
| 199 | DEFINE_COPROCR_READ_FUNC(id_pfr0, ID_PFR0) |
| 200 | DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1) |
| 201 | DEFINE_COPROCR_READ_FUNC(isr, ISR) |
| 202 | DEFINE_COPROCR_READ_FUNC(clidr, CLIDR) |
| 203 | DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64) |
| 204 | |
| 205 | DEFINE_COPROCR_RW_FUNCS(scr, SCR) |
| 206 | DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR) |
| 207 | DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR) |
| 208 | DEFINE_COPROCR_RW_FUNCS(hcr, HCR) |
| 209 | DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR) |
| 210 | DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ) |
| 211 | DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL) |
| 212 | DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0) |
| 213 | DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1) |
| 214 | DEFINE_COPROCR_RW_FUNCS(hmair0, HMAIR0) |
| 215 | DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR) |
| 216 | DEFINE_COPROCR_RW_FUNCS(htcr, HTCR) |
| 217 | DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0) |
| 218 | DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64) |
| 219 | DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1) |
| 220 | DEFINE_COPROCR_RW_FUNCS_64(httbr, HTTBR_64) |
| 221 | DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR) |
| 222 | DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR) |
| 223 | DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64) |
| 224 | DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64) |
| 225 | DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64) |
| 226 | DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR) |
| 227 | DEFINE_COPROCR_RW_FUNCS(cnthp_ctl_el2, CNTHP_CTL) |
| 228 | DEFINE_COPROCR_RW_FUNCS(cnthp_tval_el2, CNTHP_TVAL) |
| 229 | DEFINE_COPROCR_RW_FUNCS_64(cnthp_cval_el2, CNTHP_CVAL_64) |
| 230 | |
| 231 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE) |
| 232 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE) |
| 233 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE) |
| 234 | DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR) |
| 235 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1) |
| 236 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el1, ICC_IGRPEN1) |
| 237 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0) |
| 238 | DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0) |
| 239 | DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1) |
| 240 | DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0) |
| 241 | DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1) |
| 242 | DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0) |
| 243 | DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1) |
| 244 | DEFINE_COPROCR_WRITE_FUNC_64(icc_sgi1r, ICC_SGI1R_EL1_64) |
| 245 | |
| 246 | DEFINE_COPROCR_RW_FUNCS(amcntenset0, AMCNTENSET0) |
| 247 | DEFINE_COPROCR_RW_FUNCS(amcntenset1, AMCNTENSET1) |
| 248 | DEFINE_COPROCR_RW_FUNCS(amcntenclr0, AMCNTENCLR0) |
| 249 | DEFINE_COPROCR_RW_FUNCS(amcntenclr1, AMCNTENCLR1) |
| 250 | |
| 251 | DEFINE_COPROCR_RW_FUNCS_64(amevcntr00, AMEVCNTR00) |
| 252 | DEFINE_COPROCR_RW_FUNCS_64(amevcntr01, AMEVCNTR01) |
| 253 | DEFINE_COPROCR_RW_FUNCS_64(amevcntr02, AMEVCNTR02) |
| 254 | DEFINE_COPROCR_RW_FUNCS_64(amevcntr03, AMEVCNTR03) |
| 255 | |
| 256 | /* |
| 257 | * TLBI operation prototypes |
| 258 | */ |
| 259 | DEFINE_TLBIOP_FUNC(all, TLBIALL) |
| 260 | DEFINE_TLBIOP_FUNC(allis, TLBIALLIS) |
| 261 | DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA) |
| 262 | DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA) |
| 263 | DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS) |
| 264 | DEFINE_TLBIOP_PARAM_FUNC(mvahis, TLBIMVAHIS) |
| 265 | |
| 266 | /* |
| 267 | * BPI operation prototypes. |
| 268 | */ |
| 269 | DEFINE_BPIOP_FUNC(allis, BPIALLIS) |
| 270 | |
| 271 | /* |
| 272 | * DC operation prototypes |
| 273 | */ |
| 274 | DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC) |
| 275 | DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC) |
| 276 | DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC) |
| 277 | |
| 278 | /* Helper functions to manipulate CPSR */ |
| 279 | static inline void enable_irq(void) |
| 280 | { |
| 281 | /* |
| 282 | * The compiler memory barrier will prevent the compiler from |
| 283 | * scheduling non-volatile memory access after the write to the |
| 284 | * register. |
| 285 | * |
| 286 | * This could happen if some initialization code issues non-volatile |
| 287 | * accesses to an area used by an interrupt handler, in the assumption |
| 288 | * that it is safe as the interrupts are disabled at the time it does |
| 289 | * that (according to program order). However, non-volatile accesses |
| 290 | * are not necessarily in program order relatively with volatile inline |
| 291 | * assembly statements (and volatile accesses). |
| 292 | */ |
| 293 | COMPILER_BARRIER(); |
| 294 | __asm__ volatile ("cpsie i"); |
| 295 | isb(); |
| 296 | } |
| 297 | |
| 298 | static inline void enable_serror(void) |
| 299 | { |
| 300 | COMPILER_BARRIER(); |
| 301 | __asm__ volatile ("cpsie a"); |
| 302 | isb(); |
| 303 | } |
| 304 | |
| 305 | static inline void enable_fiq(void) |
| 306 | { |
| 307 | COMPILER_BARRIER(); |
| 308 | __asm__ volatile ("cpsie f"); |
| 309 | isb(); |
| 310 | } |
| 311 | |
| 312 | static inline void disable_irq(void) |
| 313 | { |
| 314 | COMPILER_BARRIER(); |
| 315 | __asm__ volatile ("cpsid i"); |
| 316 | isb(); |
| 317 | } |
| 318 | |
| 319 | static inline void disable_serror(void) |
| 320 | { |
| 321 | COMPILER_BARRIER(); |
| 322 | __asm__ volatile ("cpsid a"); |
| 323 | isb(); |
| 324 | } |
| 325 | |
| 326 | static inline void disable_fiq(void) |
| 327 | { |
| 328 | COMPILER_BARRIER(); |
| 329 | __asm__ volatile ("cpsid f"); |
| 330 | isb(); |
| 331 | } |
| 332 | |
| 333 | /* Previously defined accessor functions with incomplete register names */ |
| 334 | #define dsb() dsbsy() |
| 335 | |
| 336 | /* |
| 337 | * Helper function to detect the processor mode. |
| 338 | */ |
| 339 | #define IS_IN_HYP() (GET_M32(read_cpsr()) == MODE32_hyp) |
| 340 | #define IS_IN_SVC() (GET_M32(read_cpsr()) == MODE32_svc) |
| 341 | #define IS_IN_MON() (GET_M32(read_cpsr()) == MODE32_mon) |
| 342 | #define IS_IN_EL2() IS_IN_HYP() |
| 343 | |
| 344 | /* Accessor functions defined for compatibility with AArch32 register names */ |
| 345 | |
| 346 | #define read_mpidr_el1() read_mpidr() |
| 347 | #define read_daif() read_cpsr() |
| 348 | #define write_daif(flags) write_cpsr(flags) |
| 349 | #define read_cntfrq_el0() read_cntfrq() |
| 350 | #define read_cntpct_el0() read64_cntpct() |
| 351 | #define read_cnthp_cval_el2() read64_cnthp_cval_el2() |
| 352 | #define write_cnthp_cval_el2(v) write64_cnthp_cval_el2(v) |
| 353 | #define read_amcntenset0_el0() read_amcntenset0() |
| 354 | #define read_amcntenset1_el0() read_amcntenset1() |
| 355 | |
| 356 | void disable_mmu_icache(void); |
| 357 | |
| 358 | #endif /* __ARCH_HELPERS_H__ */ |