Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | e40306b | 2017-01-13 15:03:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __ARCH_HELPERS_H__ |
| 8 | #define __ARCH_HELPERS_H__ |
| 9 | |
| 10 | #include <arch.h> /* for additional register definitions */ |
| 11 | #include <stdint.h> |
Antonio Nino Diaz | e40306b | 2017-01-13 15:03:07 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 13 | |
| 14 | /********************************************************************** |
| 15 | * Macros which create inline functions to read or write CPU system |
| 16 | * registers |
| 17 | *********************************************************************/ |
| 18 | |
| 19 | #define _DEFINE_COPROCR_WRITE_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \ |
| 20 | static inline void write_## _name(u_register_t v) \ |
| 21 | { \ |
| 22 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 23 | } |
| 24 | |
| 25 | #define _DEFINE_COPROCR_READ_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \ |
| 26 | static inline u_register_t read_ ## _name(void) \ |
| 27 | { \ |
| 28 | u_register_t v; \ |
| 29 | __asm__ volatile ("mrc "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : "=r" (v));\ |
| 30 | return v; \ |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * The undocumented %Q and %R extended asm are used to implemented the below |
| 35 | * 64 bit `mrrc` and `mcrr` instructions. It works only on Little Endian |
| 36 | * systems for GCC versions < 4.6. Above GCC 4.6, both Little Endian and |
| 37 | * Big Endian systems generate the right instruction encoding. |
| 38 | */ |
dp-arm | 320e844 | 2017-05-02 12:00:08 +0100 | [diff] [blame] | 39 | #if !(__clang__ || __GNUC__ > (4) || __GNUC__ == (4) && __GNUC_MINOR__ >= (6)) |
| 40 | #error "clang or GCC 4.6 or above is required to build AArch32 Trusted Firmware" |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \ |
| 44 | static inline void write64_## _name(uint64_t v) \ |
| 45 | { \ |
| 46 | __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\ |
| 47 | } |
| 48 | |
| 49 | #define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \ |
| 50 | static inline uint64_t read64_## _name(void) \ |
| 51 | { uint64_t v; \ |
| 52 | __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\ |
| 53 | return v; \ |
| 54 | } |
| 55 | |
| 56 | #define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \ |
| 57 | static inline u_register_t read_ ## _name(void) \ |
| 58 | { \ |
| 59 | u_register_t v; \ |
| 60 | __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \ |
| 61 | return v; \ |
| 62 | } |
| 63 | |
| 64 | #define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \ |
| 65 | static inline void write_ ## _name(u_register_t v) \ |
| 66 | { \ |
| 67 | __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \ |
| 68 | } |
| 69 | |
| 70 | #define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \ |
| 71 | static inline void write_ ## _name(const u_register_t v) \ |
| 72 | { \ |
| 73 | __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \ |
| 74 | } |
| 75 | |
| 76 | /* Define read function for coproc register */ |
| 77 | #define DEFINE_COPROCR_READ_FUNC(_name, ...) \ |
| 78 | _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) |
| 79 | |
| 80 | /* Define read & write function for coproc register */ |
| 81 | #define DEFINE_COPROCR_RW_FUNCS(_name, ...) \ |
| 82 | _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) \ |
| 83 | _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__) |
| 84 | |
| 85 | /* Define 64 bit read function for coproc register */ |
| 86 | #define DEFINE_COPROCR_READ_FUNC_64(_name, ...) \ |
| 87 | _DEFINE_COPROCR_READ_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 | |
Dimitris Papastamos | 12f8be5 | 2017-06-20 09:25:10 +0100 | [diff] [blame] | 103 | #if ERRATA_A57_813419 |
| 104 | /* |
| 105 | * Define function for TLBI instruction with type specifier that |
| 106 | * implements the workaround for errata 813419 of Cortex-A57 |
| 107 | */ |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 108 | #define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 109 | static inline void tlbi##_op(void) \ |
| 110 | { \ |
| 111 | u_register_t v = 0; \ |
| 112 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
Dimitris Papastamos | 12f8be5 | 2017-06-20 09:25:10 +0100 | [diff] [blame] | 113 | __asm__ volatile ("dsb ish");\ |
| 114 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Dimitris Papastamos | 12f8be5 | 2017-06-20 09:25:10 +0100 | [diff] [blame] | 117 | #define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 118 | static inline void tlbi##_op(u_register_t v) \ |
| 119 | { \ |
| 120 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 121 | __asm__ volatile ("dsb ish");\ |
| 122 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 123 | } |
| 124 | #else |
| 125 | #define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 126 | static inline void tlbi##_op(void) \ |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 127 | { \ |
| 128 | u_register_t v = 0; \ |
| 129 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 130 | } |
| 131 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 132 | #define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 133 | static inline void tlbi##_op(u_register_t v) \ |
| 134 | { \ |
| 135 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 136 | } |
Dimitris Papastamos | 12f8be5 | 2017-06-20 09:25:10 +0100 | [diff] [blame] | 137 | #endif /* ERRATA_A57_813419 */ |
| 138 | |
| 139 | #define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 140 | static inline void bpi##_op(void) \ |
| 141 | { \ |
| 142 | u_register_t v = 0; \ |
| 143 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 144 | } |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 145 | |
| 146 | /* Define function for simple TLBI operation */ |
| 147 | #define DEFINE_TLBIOP_FUNC(_op, ...) \ |
| 148 | _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__) |
| 149 | |
| 150 | /* Define function for TLBI operation with register parameter */ |
| 151 | #define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \ |
| 152 | _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__) |
| 153 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 154 | /* Define function for simple BPI operation */ |
| 155 | #define DEFINE_BPIOP_FUNC(_op, ...) \ |
| 156 | _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__) |
| 157 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 158 | /********************************************************************** |
| 159 | * Macros to create inline functions for DC operations |
| 160 | *********************************************************************/ |
| 161 | #define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 162 | static inline void dc##_op(u_register_t v) \ |
| 163 | { \ |
| 164 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 165 | } |
| 166 | |
| 167 | /* Define function for DC operation with register parameter */ |
| 168 | #define DEFINE_DCOP_PARAM_FUNC(_op, ...) \ |
| 169 | _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__) |
| 170 | |
| 171 | /********************************************************************** |
| 172 | * Macros to create inline functions for system instructions |
| 173 | *********************************************************************/ |
| 174 | /* Define function for simple system instruction */ |
| 175 | #define DEFINE_SYSOP_FUNC(_op) \ |
| 176 | static inline void _op(void) \ |
| 177 | { \ |
| 178 | __asm__ (#_op); \ |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /* Define function for system instruction with type specifier */ |
| 183 | #define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \ |
| 184 | static inline void _op ## _type(void) \ |
| 185 | { \ |
| 186 | __asm__ (#_op " " #_type); \ |
| 187 | } |
| 188 | |
| 189 | /* Define function for system instruction with register parameter */ |
| 190 | #define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \ |
| 191 | static inline void _op ## _type(u_register_t v) \ |
| 192 | { \ |
| 193 | __asm__ (#_op " " #_type ", %0" : : "r" (v)); \ |
| 194 | } |
| 195 | |
| 196 | void flush_dcache_range(uintptr_t addr, size_t size); |
| 197 | void clean_dcache_range(uintptr_t addr, size_t size); |
| 198 | void inv_dcache_range(uintptr_t addr, size_t size); |
| 199 | |
Antonio Nino Diaz | e40306b | 2017-01-13 15:03:07 +0000 | [diff] [blame] | 200 | void dcsw_op_louis(u_register_t op_type); |
| 201 | void dcsw_op_all(u_register_t op_type); |
| 202 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 203 | void disable_mmu_secure(void); |
| 204 | void disable_mmu_icache_secure(void); |
| 205 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 206 | DEFINE_SYSOP_FUNC(wfi) |
| 207 | DEFINE_SYSOP_FUNC(wfe) |
| 208 | DEFINE_SYSOP_FUNC(sev) |
| 209 | DEFINE_SYSOP_TYPE_FUNC(dsb, sy) |
| 210 | DEFINE_SYSOP_TYPE_FUNC(dmb, sy) |
Yatharth Kochar | 2694cba | 2016-11-14 12:00:41 +0000 | [diff] [blame] | 211 | DEFINE_SYSOP_TYPE_FUNC(dmb, st) |
| 212 | DEFINE_SYSOP_TYPE_FUNC(dmb, ld) |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 213 | DEFINE_SYSOP_TYPE_FUNC(dsb, ish) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 214 | DEFINE_SYSOP_TYPE_FUNC(dsb, ishst) |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 215 | DEFINE_SYSOP_TYPE_FUNC(dmb, ish) |
| 216 | DEFINE_SYSOP_FUNC(isb) |
| 217 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 218 | void __dead2 smc(uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3, |
| 219 | uint32_t r4, uint32_t r5, uint32_t r6, uint32_t r7); |
| 220 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 221 | DEFINE_SYSREG_RW_FUNCS(spsr) |
| 222 | DEFINE_SYSREG_RW_FUNCS(cpsr) |
| 223 | |
| 224 | /******************************************************************************* |
| 225 | * System register accessor prototypes |
| 226 | ******************************************************************************/ |
| 227 | DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR) |
| 228 | DEFINE_COPROCR_READ_FUNC(midr, MIDR) |
| 229 | DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1) |
| 230 | DEFINE_COPROCR_READ_FUNC(isr, ISR) |
| 231 | DEFINE_COPROCR_READ_FUNC(clidr, CLIDR) |
| 232 | DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64) |
| 233 | |
| 234 | DEFINE_COPROCR_RW_FUNCS(scr, SCR) |
| 235 | DEFINE_COPROCR_RW_FUNCS(ctr, CTR) |
| 236 | DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR) |
| 237 | DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR) |
| 238 | DEFINE_COPROCR_RW_FUNCS(hcr, HCR) |
| 239 | DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR) |
| 240 | DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ) |
| 241 | DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL) |
| 242 | DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0) |
| 243 | DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1) |
| 244 | DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR) |
| 245 | DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0) |
| 246 | DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64) |
| 247 | DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1) |
| 248 | DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR) |
| 249 | DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR) |
| 250 | DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64) |
| 251 | DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64) |
| 252 | DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64) |
| 253 | DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR) |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 254 | DEFINE_COPROCR_RW_FUNCS(hstr, HSTR) |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 255 | |
| 256 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE) |
| 257 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE) |
| 258 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE) |
| 259 | DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR) |
| 260 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1) |
| 261 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0) |
| 262 | DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0) |
| 263 | DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1) |
| 264 | DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0) |
| 265 | DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1) |
| 266 | DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0) |
| 267 | DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1) |
| 268 | |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 269 | DEFINE_COPROCR_RW_FUNCS(hdcr, HDCR) |
David Cunado | c14b08e | 2016-11-25 00:21:59 +0000 | [diff] [blame] | 270 | DEFINE_COPROCR_RW_FUNCS(cnthp_ctl, CNTHP_CTL) |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 271 | DEFINE_COPROCR_READ_FUNC(pmcr, PMCR) |
| 272 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 273 | /* |
| 274 | * TLBI operation prototypes |
| 275 | */ |
| 276 | DEFINE_TLBIOP_FUNC(all, TLBIALL) |
| 277 | DEFINE_TLBIOP_FUNC(allis, TLBIALLIS) |
| 278 | DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA) |
| 279 | DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 280 | DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS) |
| 281 | |
| 282 | /* |
| 283 | * BPI operation prototypes. |
| 284 | */ |
| 285 | DEFINE_BPIOP_FUNC(allis, BPIALLIS) |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * DC operation prototypes |
| 289 | */ |
| 290 | DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC) |
| 291 | DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC) |
| 292 | DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC) |
| 293 | |
| 294 | /* Previously defined accessor functions with incomplete register names */ |
| 295 | #define dsb() dsbsy() |
| 296 | |
| 297 | #define IS_IN_SECURE() \ |
| 298 | (GET_NS_BIT(read_scr()) == 0) |
| 299 | |
| 300 | /* |
| 301 | * If EL3 is AArch32, then secure PL1 and monitor mode correspond to EL3 |
| 302 | */ |
| 303 | #define IS_IN_EL3() \ |
| 304 | ((GET_M32(read_cpsr()) == MODE32_mon) || \ |
| 305 | (IS_IN_SECURE() && (GET_M32(read_cpsr()) != MODE32_usr))) |
| 306 | |
| 307 | /* Macros for compatibility with AArch64 system registers */ |
| 308 | #define read_mpidr_el1() read_mpidr() |
| 309 | |
| 310 | #define read_scr_el3() read_scr() |
| 311 | #define write_scr_el3(_v) write_scr(_v) |
| 312 | |
| 313 | #define read_hcr_el2() read_hcr() |
| 314 | #define write_hcr_el2(_v) write_hcr(_v) |
| 315 | |
| 316 | #define read_cpacr_el1() read_cpacr() |
| 317 | #define write_cpacr_el1(_v) write_cpacr(_v) |
| 318 | |
| 319 | #define read_cntfrq_el0() read_cntfrq() |
| 320 | #define write_cntfrq_el0(_v) write_cntfrq(_v) |
| 321 | #define read_isr_el1() read_isr() |
| 322 | |
| 323 | #define read_cntpct_el0() read64_cntpct() |
| 324 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 325 | #define read_ctr_el0() read_ctr() |
| 326 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 327 | #endif /* __ARCH_HELPERS_H__ */ |