hyp-stub.S 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) 2012 Linaro Limited.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/irqchip/arm-gic-v3.h>
  7. #include <linux/linkage.h>
  8. #include <asm/assembler.h>
  9. #include <asm/virt.h>
  10. .arch armv7-a
  11. #ifndef ZIMAGE
  12. /*
  13. * For the kernel proper, we need to find out the CPU boot mode long after
  14. * boot, so we need to store it in a writable variable.
  15. *
  16. * This is not in .bss, because we set it sufficiently early that the boot-time
  17. * zeroing of .bss would clobber it.
  18. */
  19. .data
  20. .align 2
  21. ENTRY(__boot_cpu_mode)
  22. .long 0
  23. .text
  24. /*
  25. * Save the primary CPU boot mode. Requires 2 scratch registers.
  26. */
  27. .macro store_primary_cpu_mode reg1, reg2
  28. mrs \reg1, cpsr
  29. and \reg1, \reg1, #MODE_MASK
  30. str_l \reg1, __boot_cpu_mode, \reg2
  31. .endm
  32. /*
  33. * Compare the current mode with the one saved on the primary CPU.
  34. * If they don't match, record that fact. The Z bit indicates
  35. * if there's a match or not.
  36. * Requires 2 additional scratch registers.
  37. */
  38. .macro compare_cpu_mode_with_primary mode, reg1, reg2
  39. adr_l \reg2, __boot_cpu_mode
  40. ldr \reg1, [\reg2]
  41. cmp \mode, \reg1 @ matches primary CPU boot mode?
  42. orrne \reg1, \reg1, #BOOT_CPU_MODE_MISMATCH
  43. strne \reg1, [\reg2] @ record what happened and give up
  44. .endm
  45. #else /* ZIMAGE */
  46. .macro store_primary_cpu_mode reg1:req, reg2:req
  47. .endm
  48. /*
  49. * The zImage loader only runs on one CPU, so we don't bother with mult-CPU
  50. * consistency checking:
  51. */
  52. .macro compare_cpu_mode_with_primary mode, reg1, reg2
  53. cmp \mode, \mode
  54. .endm
  55. #endif /* ZIMAGE */
  56. /*
  57. * Hypervisor stub installation functions.
  58. *
  59. * These must be called with the MMU and D-cache off.
  60. * They are not ABI compliant and are only intended to be called from the kernel
  61. * entry points in head.S.
  62. */
  63. @ Call this from the primary CPU
  64. ENTRY(__hyp_stub_install)
  65. store_primary_cpu_mode r4, r5
  66. ENDPROC(__hyp_stub_install)
  67. @ fall through...
  68. @ Secondary CPUs should call here
  69. ENTRY(__hyp_stub_install_secondary)
  70. mrs r4, cpsr
  71. and r4, r4, #MODE_MASK
  72. /*
  73. * If the secondary has booted with a different mode, give up
  74. * immediately.
  75. */
  76. compare_cpu_mode_with_primary r4, r5, r6
  77. retne lr
  78. /*
  79. * Once we have given up on one CPU, we do not try to install the
  80. * stub hypervisor on the remaining ones: because the saved boot mode
  81. * is modified, it can't compare equal to the CPSR mode field any
  82. * more.
  83. *
  84. * Otherwise...
  85. */
  86. cmp r4, #HYP_MODE
  87. retne lr @ give up if the CPU is not in HYP mode
  88. /*
  89. * Configure HSCTLR to set correct exception endianness/instruction set
  90. * state etc.
  91. * Turn off all traps
  92. * Eventually, CPU-specific code might be needed -- assume not for now
  93. *
  94. * This code relies on the "eret" instruction to synchronize the
  95. * various coprocessor accesses. This is done when we switch to SVC
  96. * (see safe_svcmode_maskall).
  97. */
  98. @ Now install the hypervisor stub:
  99. W(adr) r7, __hyp_stub_vectors
  100. mcr p15, 4, r7, c12, c0, 0 @ set hypervisor vector base (HVBAR)
  101. @ Disable all traps, so we don't get any nasty surprise
  102. mov r7, #0
  103. mcr p15, 4, r7, c1, c1, 0 @ HCR
  104. mcr p15, 4, r7, c1, c1, 2 @ HCPTR
  105. mcr p15, 4, r7, c1, c1, 3 @ HSTR
  106. THUMB( orr r7, #(1 << 30) ) @ HSCTLR.TE
  107. ARM_BE8(orr r7, r7, #(1 << 25)) @ HSCTLR.EE
  108. mcr p15, 4, r7, c1, c0, 0 @ HSCTLR
  109. mrc p15, 4, r7, c1, c1, 1 @ HDCR
  110. and r7, #0x1f @ Preserve HPMN
  111. mcr p15, 4, r7, c1, c1, 1 @ HDCR
  112. @ Make sure NS-SVC is initialised appropriately
  113. mrc p15, 0, r7, c1, c0, 0 @ SCTLR
  114. orr r7, #(1 << 5) @ CP15 barriers enabled
  115. bic r7, #(3 << 7) @ Clear SED/ITD for v8 (RES0 for v7)
  116. bic r7, #(3 << 19) @ WXN and UWXN disabled
  117. mcr p15, 0, r7, c1, c0, 0 @ SCTLR
  118. mrc p15, 0, r7, c0, c0, 0 @ MIDR
  119. mcr p15, 4, r7, c0, c0, 0 @ VPIDR
  120. mrc p15, 0, r7, c0, c0, 5 @ MPIDR
  121. mcr p15, 4, r7, c0, c0, 5 @ VMPIDR
  122. #if !defined(ZIMAGE) && defined(CONFIG_ARM_ARCH_TIMER)
  123. @ make CNTP_* and CNTPCT accessible from PL1
  124. mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
  125. ubfx r7, r7, #16, #4
  126. teq r7, #0
  127. beq 1f
  128. mrc p15, 4, r7, c14, c1, 0 @ CNTHCTL
  129. orr r7, r7, #3 @ PL1PCEN | PL1PCTEN
  130. mcr p15, 4, r7, c14, c1, 0 @ CNTHCTL
  131. mov r7, #0
  132. mcrr p15, 4, r7, r7, c14 @ CNTVOFF
  133. @ Disable virtual timer in case it was counting
  134. mrc p15, 0, r7, c14, c3, 1 @ CNTV_CTL
  135. bic r7, #1 @ Clear ENABLE
  136. mcr p15, 0, r7, c14, c3, 1 @ CNTV_CTL
  137. 1:
  138. #endif
  139. #ifdef CONFIG_ARM_GIC_V3
  140. @ Check whether GICv3 system registers are available
  141. mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
  142. ubfx r7, r7, #28, #4
  143. teq r7, #0
  144. beq 2f
  145. @ Enable system register accesses
  146. mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE
  147. orr r7, r7, #(ICC_SRE_EL2_ENABLE | ICC_SRE_EL2_SRE)
  148. mcr p15, 4, r7, c12, c9, 5 @ ICC_HSRE
  149. isb
  150. @ SRE bit could be forced to 0 by firmware.
  151. @ Check whether it sticks before accessing any other sysreg
  152. mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE
  153. tst r7, #ICC_SRE_EL2_SRE
  154. beq 2f
  155. mov r7, #0
  156. mcr p15, 4, r7, c12, c11, 0 @ ICH_HCR
  157. 2:
  158. #endif
  159. bx lr @ The boot CPU mode is left in r4.
  160. ENDPROC(__hyp_stub_install_secondary)
  161. __hyp_stub_do_trap:
  162. #ifdef ZIMAGE
  163. teq r0, #HVC_SET_VECTORS
  164. bne 1f
  165. /* Only the ZIMAGE stubs can change the HYP vectors */
  166. mcr p15, 4, r1, c12, c0, 0 @ set HVBAR
  167. b __hyp_stub_exit
  168. #endif
  169. 1: teq r0, #HVC_SOFT_RESTART
  170. bne 2f
  171. bx r1
  172. 2: ldr r0, =HVC_STUB_ERR
  173. __ERET
  174. __hyp_stub_exit:
  175. mov r0, #0
  176. __ERET
  177. ENDPROC(__hyp_stub_do_trap)
  178. /*
  179. * __hyp_set_vectors is only used when ZIMAGE must bounce between HYP
  180. * and SVC. For the kernel itself, the vectors are set once and for
  181. * all by the stubs.
  182. */
  183. ENTRY(__hyp_set_vectors)
  184. mov r1, r0
  185. mov r0, #HVC_SET_VECTORS
  186. __HVC(0)
  187. ret lr
  188. ENDPROC(__hyp_set_vectors)
  189. ENTRY(__hyp_soft_restart)
  190. mov r1, r0
  191. mov r0, #HVC_SOFT_RESTART
  192. __HVC(0)
  193. ret lr
  194. ENDPROC(__hyp_soft_restart)
  195. .align 5
  196. ENTRY(__hyp_stub_vectors)
  197. __hyp_stub_reset: W(b) .
  198. __hyp_stub_und: W(b) .
  199. __hyp_stub_svc: W(b) .
  200. __hyp_stub_pabort: W(b) .
  201. __hyp_stub_dabort: W(b) .
  202. __hyp_stub_trap: W(b) __hyp_stub_do_trap
  203. __hyp_stub_irq: W(b) .
  204. __hyp_stub_fiq: W(b) .
  205. ENDPROC(__hyp_stub_vectors)