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 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef __ARCH_HELPERS_H__ |
| 32 | #define __ARCH_HELPERS_H__ |
| 33 | |
| 34 | #include <arch.h> /* for additional register definitions */ |
| 35 | #include <stdint.h> |
Antonio Nino Diaz | e40306b | 2017-01-13 15:03:07 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 37 | |
| 38 | /********************************************************************** |
| 39 | * Macros which create inline functions to read or write CPU system |
| 40 | * registers |
| 41 | *********************************************************************/ |
| 42 | |
| 43 | #define _DEFINE_COPROCR_WRITE_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \ |
| 44 | static inline void write_## _name(u_register_t v) \ |
| 45 | { \ |
| 46 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 47 | } |
| 48 | |
| 49 | #define _DEFINE_COPROCR_READ_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \ |
| 50 | static inline u_register_t read_ ## _name(void) \ |
| 51 | { \ |
| 52 | u_register_t v; \ |
| 53 | __asm__ volatile ("mrc "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : "=r" (v));\ |
| 54 | return v; \ |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * The undocumented %Q and %R extended asm are used to implemented the below |
| 59 | * 64 bit `mrrc` and `mcrr` instructions. It works only on Little Endian |
| 60 | * systems for GCC versions < 4.6. Above GCC 4.6, both Little Endian and |
| 61 | * Big Endian systems generate the right instruction encoding. |
| 62 | */ |
| 63 | #if !(__GNUC__ > (4) || __GNUC__ == (4) && __GNUC_MINOR__ >= (6)) |
| 64 | #error "GCC 4.6 or above is required to build AArch32 Trusted Firmware" |
| 65 | #endif |
| 66 | |
| 67 | #define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \ |
| 68 | static inline void write64_## _name(uint64_t v) \ |
| 69 | { \ |
| 70 | __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\ |
| 71 | } |
| 72 | |
| 73 | #define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \ |
| 74 | static inline uint64_t read64_## _name(void) \ |
| 75 | { uint64_t v; \ |
| 76 | __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\ |
| 77 | return v; \ |
| 78 | } |
| 79 | |
| 80 | #define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \ |
| 81 | static inline u_register_t read_ ## _name(void) \ |
| 82 | { \ |
| 83 | u_register_t v; \ |
| 84 | __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \ |
| 85 | return v; \ |
| 86 | } |
| 87 | |
| 88 | #define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \ |
| 89 | static inline void write_ ## _name(u_register_t v) \ |
| 90 | { \ |
| 91 | __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \ |
| 92 | } |
| 93 | |
| 94 | #define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \ |
| 95 | static inline void write_ ## _name(const u_register_t v) \ |
| 96 | { \ |
| 97 | __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \ |
| 98 | } |
| 99 | |
| 100 | /* Define read function for coproc register */ |
| 101 | #define DEFINE_COPROCR_READ_FUNC(_name, ...) \ |
| 102 | _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) |
| 103 | |
| 104 | /* Define read & write function for coproc register */ |
| 105 | #define DEFINE_COPROCR_RW_FUNCS(_name, ...) \ |
| 106 | _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) \ |
| 107 | _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__) |
| 108 | |
| 109 | /* Define 64 bit read function for coproc register */ |
| 110 | #define DEFINE_COPROCR_READ_FUNC_64(_name, ...) \ |
| 111 | _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) |
| 112 | |
| 113 | /* Define 64 bit read & write function for coproc register */ |
| 114 | #define DEFINE_COPROCR_RW_FUNCS_64(_name, ...) \ |
| 115 | _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) \ |
| 116 | _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__) |
| 117 | |
| 118 | /* Define read & write function for system register */ |
| 119 | #define DEFINE_SYSREG_RW_FUNCS(_name) \ |
| 120 | _DEFINE_SYSREG_READ_FUNC(_name, _name) \ |
| 121 | _DEFINE_SYSREG_WRITE_FUNC(_name, _name) |
| 122 | |
| 123 | /********************************************************************** |
| 124 | * Macros to create inline functions for tlbi operations |
| 125 | *********************************************************************/ |
| 126 | |
| 127 | #define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 128 | static inline void tlbi##_op(void) \ |
| 129 | { \ |
| 130 | u_register_t v = 0; \ |
| 131 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 132 | } |
| 133 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 134 | #define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 135 | static inline void bpi##_op(void) \ |
| 136 | { \ |
| 137 | u_register_t v = 0; \ |
| 138 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 139 | } |
| 140 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 141 | #define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 142 | static inline void tlbi##_op(u_register_t v) \ |
| 143 | { \ |
| 144 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 145 | } |
| 146 | |
| 147 | /* Define function for simple TLBI operation */ |
| 148 | #define DEFINE_TLBIOP_FUNC(_op, ...) \ |
| 149 | _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__) |
| 150 | |
| 151 | /* Define function for TLBI operation with register parameter */ |
| 152 | #define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \ |
| 153 | _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__) |
| 154 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 155 | /* Define function for simple BPI operation */ |
| 156 | #define DEFINE_BPIOP_FUNC(_op, ...) \ |
| 157 | _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__) |
| 158 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 159 | /********************************************************************** |
| 160 | * Macros to create inline functions for DC operations |
| 161 | *********************************************************************/ |
| 162 | #define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \ |
| 163 | static inline void dc##_op(u_register_t v) \ |
| 164 | { \ |
| 165 | __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\ |
| 166 | } |
| 167 | |
| 168 | /* Define function for DC operation with register parameter */ |
| 169 | #define DEFINE_DCOP_PARAM_FUNC(_op, ...) \ |
| 170 | _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__) |
| 171 | |
| 172 | /********************************************************************** |
| 173 | * Macros to create inline functions for system instructions |
| 174 | *********************************************************************/ |
| 175 | /* Define function for simple system instruction */ |
| 176 | #define DEFINE_SYSOP_FUNC(_op) \ |
| 177 | static inline void _op(void) \ |
| 178 | { \ |
| 179 | __asm__ (#_op); \ |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* Define function for system instruction with type specifier */ |
| 184 | #define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \ |
| 185 | static inline void _op ## _type(void) \ |
| 186 | { \ |
| 187 | __asm__ (#_op " " #_type); \ |
| 188 | } |
| 189 | |
| 190 | /* Define function for system instruction with register parameter */ |
| 191 | #define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \ |
| 192 | static inline void _op ## _type(u_register_t v) \ |
| 193 | { \ |
| 194 | __asm__ (#_op " " #_type ", %0" : : "r" (v)); \ |
| 195 | } |
| 196 | |
| 197 | void flush_dcache_range(uintptr_t addr, size_t size); |
| 198 | void clean_dcache_range(uintptr_t addr, size_t size); |
| 199 | void inv_dcache_range(uintptr_t addr, size_t size); |
| 200 | |
Antonio Nino Diaz | e40306b | 2017-01-13 15:03:07 +0000 | [diff] [blame] | 201 | void dcsw_op_louis(u_register_t op_type); |
| 202 | void dcsw_op_all(u_register_t op_type); |
| 203 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 204 | void disable_mmu_secure(void); |
| 205 | void disable_mmu_icache_secure(void); |
| 206 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 207 | DEFINE_SYSOP_FUNC(wfi) |
| 208 | DEFINE_SYSOP_FUNC(wfe) |
| 209 | DEFINE_SYSOP_FUNC(sev) |
| 210 | DEFINE_SYSOP_TYPE_FUNC(dsb, sy) |
| 211 | DEFINE_SYSOP_TYPE_FUNC(dmb, sy) |
| 212 | DEFINE_SYSOP_TYPE_FUNC(dsb, ish) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 213 | DEFINE_SYSOP_TYPE_FUNC(dsb, ishst) |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 214 | DEFINE_SYSOP_TYPE_FUNC(dmb, ish) |
| 215 | DEFINE_SYSOP_FUNC(isb) |
| 216 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 217 | void __dead2 smc(uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3, |
| 218 | uint32_t r4, uint32_t r5, uint32_t r6, uint32_t r7); |
| 219 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 220 | DEFINE_SYSREG_RW_FUNCS(spsr) |
| 221 | DEFINE_SYSREG_RW_FUNCS(cpsr) |
| 222 | |
| 223 | /******************************************************************************* |
| 224 | * System register accessor prototypes |
| 225 | ******************************************************************************/ |
| 226 | DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR) |
| 227 | DEFINE_COPROCR_READ_FUNC(midr, MIDR) |
| 228 | DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1) |
| 229 | DEFINE_COPROCR_READ_FUNC(isr, ISR) |
| 230 | DEFINE_COPROCR_READ_FUNC(clidr, CLIDR) |
| 231 | DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64) |
| 232 | |
| 233 | DEFINE_COPROCR_RW_FUNCS(scr, SCR) |
| 234 | DEFINE_COPROCR_RW_FUNCS(ctr, CTR) |
| 235 | DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR) |
| 236 | DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR) |
| 237 | DEFINE_COPROCR_RW_FUNCS(hcr, HCR) |
| 238 | DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR) |
| 239 | DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ) |
| 240 | DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL) |
| 241 | DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0) |
| 242 | DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1) |
| 243 | DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR) |
| 244 | DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0) |
| 245 | DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64) |
| 246 | DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1) |
| 247 | DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR) |
| 248 | DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR) |
| 249 | DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64) |
| 250 | DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64) |
| 251 | DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64) |
| 252 | DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR) |
| 253 | |
| 254 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE) |
| 255 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE) |
| 256 | DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE) |
| 257 | DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR) |
| 258 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1) |
| 259 | DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0) |
| 260 | DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0) |
| 261 | DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1) |
| 262 | DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0) |
| 263 | DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1) |
| 264 | DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0) |
| 265 | DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1) |
| 266 | |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 267 | DEFINE_COPROCR_RW_FUNCS(hdcr, HDCR) |
David Cunado | c14b08e | 2016-11-25 00:21:59 +0000 | [diff] [blame] | 268 | DEFINE_COPROCR_RW_FUNCS(cnthp_ctl, CNTHP_CTL) |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 269 | DEFINE_COPROCR_READ_FUNC(pmcr, PMCR) |
| 270 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 271 | /* |
| 272 | * TLBI operation prototypes |
| 273 | */ |
| 274 | DEFINE_TLBIOP_FUNC(all, TLBIALL) |
| 275 | DEFINE_TLBIOP_FUNC(allis, TLBIALLIS) |
| 276 | DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA) |
| 277 | DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 278 | DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS) |
| 279 | |
| 280 | /* |
| 281 | * BPI operation prototypes. |
| 282 | */ |
| 283 | DEFINE_BPIOP_FUNC(allis, BPIALLIS) |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * DC operation prototypes |
| 287 | */ |
| 288 | DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC) |
| 289 | DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC) |
| 290 | DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC) |
| 291 | |
| 292 | /* Previously defined accessor functions with incomplete register names */ |
| 293 | #define dsb() dsbsy() |
| 294 | |
| 295 | #define IS_IN_SECURE() \ |
| 296 | (GET_NS_BIT(read_scr()) == 0) |
| 297 | |
| 298 | /* |
| 299 | * If EL3 is AArch32, then secure PL1 and monitor mode correspond to EL3 |
| 300 | */ |
| 301 | #define IS_IN_EL3() \ |
| 302 | ((GET_M32(read_cpsr()) == MODE32_mon) || \ |
| 303 | (IS_IN_SECURE() && (GET_M32(read_cpsr()) != MODE32_usr))) |
| 304 | |
| 305 | /* Macros for compatibility with AArch64 system registers */ |
| 306 | #define read_mpidr_el1() read_mpidr() |
| 307 | |
| 308 | #define read_scr_el3() read_scr() |
| 309 | #define write_scr_el3(_v) write_scr(_v) |
| 310 | |
| 311 | #define read_hcr_el2() read_hcr() |
| 312 | #define write_hcr_el2(_v) write_hcr(_v) |
| 313 | |
| 314 | #define read_cpacr_el1() read_cpacr() |
| 315 | #define write_cpacr_el1(_v) write_cpacr(_v) |
| 316 | |
| 317 | #define read_cntfrq_el0() read_cntfrq() |
| 318 | #define write_cntfrq_el0(_v) write_cntfrq(_v) |
| 319 | #define read_isr_el1() read_isr() |
| 320 | |
| 321 | #define read_cntpct_el0() read64_cntpct() |
| 322 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 323 | #define read_ctr_el0() read_ctr() |
| 324 | |
Soby Mathew | c6820d1 | 2016-05-09 17:49:55 +0100 | [diff] [blame] | 325 | #endif /* __ARCH_HELPERS_H__ */ |