macro.h 8.3 KB

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