macro.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * include/asm-arm/macro.h
  4. *
  5. * Copyright (C) 2009 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  6. */
  7. #ifndef __ASM_ARM_MACRO_H__
  8. #define __ASM_ARM_MACRO_H__
  9. #ifdef CONFIG_ARM64
  10. #include <asm/system.h>
  11. #endif
  12. #ifdef __ASSEMBLY__
  13. /*
  14. * These macros provide a convenient way to write 8, 16 and 32 bit data
  15. * to any address.
  16. * Registers r4 and r5 are used, any data in these registers are
  17. * overwritten by the macros.
  18. * The macros are valid for any ARM architecture, they do not implement
  19. * any memory barriers so caution is recommended when using these when the
  20. * caches are enabled or on a multi-core system.
  21. */
  22. .macro write32, addr, data
  23. ldr r4, =\addr
  24. ldr r5, =\data
  25. str r5, [r4]
  26. .endm
  27. .macro write16, addr, data
  28. ldr r4, =\addr
  29. ldrh r5, =\data
  30. strh r5, [r4]
  31. .endm
  32. .macro write8, addr, data
  33. ldr r4, =\addr
  34. ldrb r5, =\data
  35. strb r5, [r4]
  36. .endm
  37. /*
  38. * This macro generates a loop that can be used for delays in the code.
  39. * Register r4 is used, any data in this register is overwritten by the
  40. * macro.
  41. * The macro is valid for any ARM architeture. The actual time spent in the
  42. * loop will vary from CPU to CPU though.
  43. */
  44. .macro wait_timer, time
  45. ldr r4, =\time
  46. 1:
  47. nop
  48. subs r4, r4, #1
  49. bcs 1b
  50. .endm
  51. #ifdef CONFIG_ARM64
  52. /*
  53. * Register aliases.
  54. */
  55. lr .req x30
  56. /*
  57. * Branch according to exception level
  58. */
  59. .macro switch_el, xreg, el3_label, el2_label, el1_label
  60. mrs \xreg, CurrentEL
  61. cmp \xreg, #0x8
  62. b.gt \el3_label
  63. b.eq \el2_label
  64. b.lt \el1_label
  65. .endm
  66. /*
  67. * Branch if we are not in the highest exception level
  68. */
  69. .macro branch_if_not_highest_el, xreg, label
  70. switch_el \xreg, 3f, 2f, 1f
  71. 2: mrs \xreg, ID_AA64PFR0_EL1
  72. and \xreg, \xreg, #(ID_AA64PFR0_EL1_EL3)
  73. cbnz \xreg, \label
  74. b 3f
  75. 1: mrs \xreg, ID_AA64PFR0_EL1
  76. and \xreg, \xreg, #(ID_AA64PFR0_EL1_EL3 | ID_AA64PFR0_EL1_EL2)
  77. cbnz \xreg, \label
  78. 3:
  79. .endm
  80. /*
  81. * Branch if current processor is a Cortex-A57 core.
  82. */
  83. .macro branch_if_a57_core, xreg, a57_label
  84. mrs \xreg, midr_el1
  85. lsr \xreg, \xreg, #4
  86. and \xreg, \xreg, #0x00000FFF
  87. cmp \xreg, #0xD07 /* Cortex-A57 MPCore processor. */
  88. b.eq \a57_label
  89. .endm
  90. /*
  91. * Branch if current processor is a Cortex-A53 core.
  92. */
  93. .macro branch_if_a53_core, xreg, a53_label
  94. mrs \xreg, midr_el1
  95. lsr \xreg, \xreg, #4
  96. and \xreg, \xreg, #0x00000FFF
  97. cmp \xreg, #0xD03 /* Cortex-A53 MPCore processor. */
  98. b.eq \a53_label
  99. .endm
  100. /*
  101. * Branch if current processor is a slave,
  102. * choose processor with all zero affinity value as the master.
  103. */
  104. .macro branch_if_slave, xreg, slave_label
  105. #ifdef CONFIG_ARMV8_MULTIENTRY
  106. mrs \xreg, mpidr_el1
  107. and \xreg, \xreg, 0xffffffffff /* clear bits [63:40] */
  108. and \xreg, \xreg, ~0x00ff000000 /* also clear bits [31:24] */
  109. cbnz \xreg, \slave_label
  110. #endif
  111. .endm
  112. /*
  113. * Branch if current processor is a master,
  114. * choose processor with all zero affinity value as the master.
  115. */
  116. .macro branch_if_master, xreg, master_label
  117. #ifdef CONFIG_ARMV8_MULTIENTRY
  118. mrs \xreg, mpidr_el1
  119. and \xreg, \xreg, 0xffffffffff /* clear bits [63:40] */
  120. and \xreg, \xreg, ~0x00ff000000 /* also clear bits [31:24] */
  121. cbz \xreg, \master_label
  122. #else
  123. b \master_label
  124. #endif
  125. .endm
  126. /*
  127. * Switch from EL3 to EL2 for ARMv8
  128. * @ep: kernel entry point
  129. * @flag: The execution state flag for lower exception
  130. * level, ES_TO_AARCH64 or ES_TO_AARCH32
  131. * @tmp: temporary register
  132. *
  133. * For loading 32-bit OS, x1 is machine nr and x2 is ftaddr.
  134. * For loading 64-bit OS, x0 is physical address to the FDT blob.
  135. * They will be passed to the guest.
  136. */
  137. .macro armv8_switch_to_el2_m, ep, flag, tmp
  138. msr cptr_el3, xzr /* Disable coprocessor traps to EL3 */
  139. mov \tmp, #CPTR_EL2_RES1
  140. msr cptr_el2, \tmp /* Disable coprocessor traps to EL2 */
  141. /* Initialize Generic Timers */
  142. msr cntvoff_el2, xzr
  143. /* Initialize SCTLR_EL2
  144. *
  145. * setting RES1 bits (29,28,23,22,18,16,11,5,4) to 1
  146. * and RES0 bits (31,30,27,26,24,21,20,17,15-13,10-6) +
  147. * EE,WXN,I,SA,C,A,M to 0
  148. */
  149. ldr \tmp, =(SCTLR_EL2_RES1 | SCTLR_EL2_EE_LE |\
  150. SCTLR_EL2_WXN_DIS | SCTLR_EL2_ICACHE_DIS |\
  151. SCTLR_EL2_SA_DIS | SCTLR_EL2_DCACHE_DIS |\
  152. SCTLR_EL2_ALIGN_DIS | SCTLR_EL2_MMU_DIS)
  153. msr sctlr_el2, \tmp
  154. mov \tmp, sp
  155. msr sp_el2, \tmp /* Migrate SP */
  156. mrs \tmp, vbar_el3
  157. msr vbar_el2, \tmp /* Migrate VBAR */
  158. /* Check switch to AArch64 EL2 or AArch32 Hypervisor mode */
  159. cmp \flag, #ES_TO_AARCH32
  160. b.eq 1f
  161. /*
  162. * The next lower exception level is AArch64, 64bit EL2 | HCE |
  163. * RES1 (Bits[5:4]) | Non-secure EL0/EL1.
  164. * and the SMD depends on requirements.
  165. */
  166. #ifdef CONFIG_ARMV8_PSCI
  167. ldr \tmp, =(SCR_EL3_RW_AARCH64 | SCR_EL3_HCE_EN |\
  168. SCR_EL3_RES1 | SCR_EL3_NS_EN)
  169. #else
  170. ldr \tmp, =(SCR_EL3_RW_AARCH64 | SCR_EL3_HCE_EN |\
  171. SCR_EL3_SMD_DIS | SCR_EL3_RES1 |\
  172. SCR_EL3_NS_EN)
  173. #endif
  174. #ifdef CONFIG_ARMV8_EA_EL3_FIRST
  175. orr \tmp, \tmp, #SCR_EL3_EA_EN
  176. #endif
  177. msr scr_el3, \tmp
  178. /* Return to the EL2_SP2 mode from EL3 */
  179. ldr \tmp, =(SPSR_EL_DEBUG_MASK | SPSR_EL_SERR_MASK |\
  180. SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
  181. SPSR_EL_M_AARCH64 | SPSR_EL_M_EL2H)
  182. msr spsr_el3, \tmp
  183. msr elr_el3, \ep
  184. eret
  185. 1:
  186. /*
  187. * The next lower exception level is AArch32, 32bit EL2 | HCE |
  188. * SMD | RES1 (Bits[5:4]) | Non-secure EL0/EL1.
  189. */
  190. ldr \tmp, =(SCR_EL3_RW_AARCH32 | SCR_EL3_HCE_EN |\
  191. SCR_EL3_SMD_DIS | SCR_EL3_RES1 |\
  192. SCR_EL3_NS_EN)
  193. msr scr_el3, \tmp
  194. /* Return to AArch32 Hypervisor mode */
  195. ldr \tmp, =(SPSR_EL_END_LE | SPSR_EL_ASYN_MASK |\
  196. SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
  197. SPSR_EL_T_A32 | SPSR_EL_M_AARCH32 |\
  198. SPSR_EL_M_HYP)
  199. msr spsr_el3, \tmp
  200. msr elr_el3, \ep
  201. eret
  202. .endm
  203. /*
  204. * Switch from EL2 to EL1 for ARMv8
  205. * @ep: kernel entry point
  206. * @flag: The execution state flag for lower exception
  207. * level, ES_TO_AARCH64 or ES_TO_AARCH32
  208. * @tmp: temporary register
  209. *
  210. * For loading 32-bit OS, x1 is machine nr and x2 is ftaddr.
  211. * For loading 64-bit OS, x0 is physical address to the FDT blob.
  212. * They will be passed to the guest.
  213. */
  214. .macro armv8_switch_to_el1_m, ep, flag, tmp, tmp2
  215. /* Initialize Generic Timers */
  216. mrs \tmp, cnthctl_el2
  217. /* Enable EL1 access to timers */
  218. orr \tmp, \tmp, #(CNTHCTL_EL2_EL1PCEN_EN |\
  219. CNTHCTL_EL2_EL1PCTEN_EN)
  220. msr cnthctl_el2, \tmp
  221. msr cntvoff_el2, xzr
  222. /* Initilize MPID/MPIDR registers */
  223. mrs \tmp, midr_el1
  224. msr vpidr_el2, \tmp
  225. mrs \tmp, mpidr_el1
  226. msr vmpidr_el2, \tmp
  227. /* Disable coprocessor traps */
  228. mov \tmp, #CPTR_EL2_RES1
  229. msr cptr_el2, \tmp /* Disable coprocessor traps to EL2 */
  230. msr hstr_el2, xzr /* Disable coprocessor traps to EL2 */
  231. mov \tmp, #CPACR_EL1_FPEN_EN
  232. msr cpacr_el1, \tmp /* Enable FP/SIMD at EL1 */
  233. /* SCTLR_EL1 initialization
  234. *
  235. * setting RES1 bits (29,28,23,22,20,11) to 1
  236. * and RES0 bits (31,30,27,21,17,13,10,6) +
  237. * UCI,EE,EOE,WXN,nTWE,nTWI,UCT,DZE,I,UMA,SED,ITD,
  238. * CP15BEN,SA0,SA,C,A,M to 0
  239. */
  240. ldr \tmp, =(SCTLR_EL1_RES1 | SCTLR_EL1_UCI_DIS |\
  241. SCTLR_EL1_EE_LE | SCTLR_EL1_WXN_DIS |\
  242. SCTLR_EL1_NTWE_DIS | SCTLR_EL1_NTWI_DIS |\
  243. SCTLR_EL1_UCT_DIS | SCTLR_EL1_DZE_DIS |\
  244. SCTLR_EL1_ICACHE_DIS | SCTLR_EL1_UMA_DIS |\
  245. SCTLR_EL1_SED_EN | SCTLR_EL1_ITD_EN |\
  246. SCTLR_EL1_CP15BEN_DIS | SCTLR_EL1_SA0_DIS |\
  247. SCTLR_EL1_SA_DIS | SCTLR_EL1_DCACHE_DIS |\
  248. SCTLR_EL1_ALIGN_DIS | SCTLR_EL1_MMU_DIS)
  249. msr sctlr_el1, \tmp
  250. mov \tmp, sp
  251. msr sp_el1, \tmp /* Migrate SP */
  252. mrs \tmp, vbar_el2
  253. msr vbar_el1, \tmp /* Migrate VBAR */
  254. /* Check switch to AArch64 EL1 or AArch32 Supervisor mode */
  255. cmp \flag, #ES_TO_AARCH32
  256. b.eq 1f
  257. /* Initialize HCR_EL2 */
  258. /* Only disable PAuth traps if PAuth is supported */
  259. mrs \tmp, id_aa64isar1_el1
  260. ldr \tmp2, =(ID_AA64ISAR1_EL1_GPI | ID_AA64ISAR1_EL1_GPA | \
  261. ID_AA64ISAR1_EL1_API | ID_AA64ISAR1_EL1_APA)
  262. tst \tmp, \tmp2
  263. mov \tmp2, #(HCR_EL2_RW_AARCH64 | HCR_EL2_HCD_DIS)
  264. orr \tmp, \tmp2, #(HCR_EL2_APK | HCR_EL2_API)
  265. csel \tmp, \tmp2, \tmp, eq
  266. msr hcr_el2, \tmp
  267. /* Return to the EL1_SP1 mode from EL2 */
  268. ldr \tmp, =(SPSR_EL_DEBUG_MASK | SPSR_EL_SERR_MASK |\
  269. SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
  270. SPSR_EL_M_AARCH64 | SPSR_EL_M_EL1H)
  271. msr spsr_el2, \tmp
  272. msr elr_el2, \ep
  273. eret
  274. 1:
  275. /* Initialize HCR_EL2 */
  276. ldr \tmp, =(HCR_EL2_RW_AARCH32 | HCR_EL2_HCD_DIS)
  277. msr hcr_el2, \tmp
  278. /* Return to AArch32 Supervisor mode from EL2 */
  279. ldr \tmp, =(SPSR_EL_END_LE | SPSR_EL_ASYN_MASK |\
  280. SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
  281. SPSR_EL_T_A32 | SPSR_EL_M_AARCH32 |\
  282. SPSR_EL_M_SVC)
  283. msr spsr_el2, \tmp
  284. msr elr_el2, \ep
  285. eret
  286. .endm
  287. #if defined(CONFIG_GICV3)
  288. .macro gic_wait_for_interrupt_m xreg1
  289. 0 : wfi
  290. mrs \xreg1, ICC_IAR1_EL1
  291. msr ICC_EOIR1_EL1, \xreg1
  292. cbnz \xreg1, 0b
  293. .endm
  294. #elif defined(CONFIG_GICV2)
  295. .macro gic_wait_for_interrupt_m xreg1, wreg2
  296. 0 : wfi
  297. ldr \wreg2, [\xreg1, GICC_AIAR]
  298. str \wreg2, [\xreg1, GICC_AEOIR]
  299. and \wreg2, \wreg2, #0x3ff
  300. cbnz \wreg2, 0b
  301. .endm
  302. #endif
  303. #endif /* CONFIG_ARM64 */
  304. #endif /* __ASSEMBLY__ */
  305. #endif /* __ASM_ARM_MACRO_H__ */