el2_setup.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #ifndef __ARM_KVM_INIT_H__
  7. #define __ARM_KVM_INIT_H__
  8. #ifndef __ASSEMBLY__
  9. #error Assembly-only header
  10. #endif
  11. #include <asm/kvm_arm.h>
  12. #include <asm/ptrace.h>
  13. #include <asm/sysreg.h>
  14. #include <linux/irqchip/arm-gic-v3.h>
  15. .macro __init_el2_sctlr
  16. mov_q x0, INIT_SCTLR_EL2_MMU_OFF
  17. msr sctlr_el2, x0
  18. isb
  19. .endm
  20. /*
  21. * Allow Non-secure EL1 and EL0 to access physical timer and counter.
  22. * This is not necessary for VHE, since the host kernel runs in EL2,
  23. * and EL0 accesses are configured in the later stage of boot process.
  24. * Note that when HCR_EL2.E2H == 1, CNTHCTL_EL2 has the same bit layout
  25. * as CNTKCTL_EL1, and CNTKCTL_EL1 accessing instructions are redefined
  26. * to access CNTHCTL_EL2. This allows the kernel designed to run at EL1
  27. * to transparently mess with the EL0 bits via CNTKCTL_EL1 access in
  28. * EL2.
  29. */
  30. .macro __init_el2_timers
  31. mov x0, #3 // Enable EL1 physical timers
  32. msr cnthctl_el2, x0
  33. msr cntvoff_el2, xzr // Clear virtual offset
  34. .endm
  35. .macro __init_el2_debug
  36. mrs x1, id_aa64dfr0_el1
  37. sbfx x0, x1, #ID_AA64DFR0_EL1_PMUVer_SHIFT, #4
  38. cmp x0, #1
  39. b.lt .Lskip_pmu_\@ // Skip if no PMU present
  40. mrs x0, pmcr_el0 // Disable debug access traps
  41. ubfx x0, x0, #11, #5 // to EL2 and allow access to
  42. .Lskip_pmu_\@:
  43. csel x2, xzr, x0, lt // all PMU counters from EL1
  44. /* Statistical profiling */
  45. ubfx x0, x1, #ID_AA64DFR0_EL1_PMSVer_SHIFT, #4
  46. cbz x0, .Lskip_spe_\@ // Skip if SPE not present
  47. mrs_s x0, SYS_PMBIDR_EL1 // If SPE available at EL2,
  48. and x0, x0, #(1 << SYS_PMBIDR_EL1_P_SHIFT)
  49. cbnz x0, .Lskip_spe_el2_\@ // then permit sampling of physical
  50. mov x0, #(1 << SYS_PMSCR_EL2_PCT_SHIFT | \
  51. 1 << SYS_PMSCR_EL2_PA_SHIFT)
  52. msr_s SYS_PMSCR_EL2, x0 // addresses and physical counter
  53. .Lskip_spe_el2_\@:
  54. mov x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
  55. orr x2, x2, x0 // If we don't have VHE, then
  56. // use EL1&0 translation.
  57. .Lskip_spe_\@:
  58. /* Trace buffer */
  59. ubfx x0, x1, #ID_AA64DFR0_EL1_TraceBuffer_SHIFT, #4
  60. cbz x0, .Lskip_trace_\@ // Skip if TraceBuffer is not present
  61. mrs_s x0, SYS_TRBIDR_EL1
  62. and x0, x0, TRBIDR_PROG
  63. cbnz x0, .Lskip_trace_\@ // If TRBE is available at EL2
  64. mov x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
  65. orr x2, x2, x0 // allow the EL1&0 translation
  66. // to own it.
  67. .Lskip_trace_\@:
  68. msr mdcr_el2, x2 // Configure debug traps
  69. .endm
  70. /* LORegions */
  71. .macro __init_el2_lor
  72. mrs x1, id_aa64mmfr1_el1
  73. ubfx x0, x1, #ID_AA64MMFR1_EL1_LO_SHIFT, 4
  74. cbz x0, .Lskip_lor_\@
  75. msr_s SYS_LORC_EL1, xzr
  76. .Lskip_lor_\@:
  77. .endm
  78. /* Stage-2 translation */
  79. .macro __init_el2_stage2
  80. msr vttbr_el2, xzr
  81. .endm
  82. /* GICv3 system register access */
  83. .macro __init_el2_gicv3
  84. mrs x0, id_aa64pfr0_el1
  85. ubfx x0, x0, #ID_AA64PFR0_EL1_GIC_SHIFT, #4
  86. cbz x0, .Lskip_gicv3_\@
  87. mrs_s x0, SYS_ICC_SRE_EL2
  88. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  89. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  90. msr_s SYS_ICC_SRE_EL2, x0
  91. isb // Make sure SRE is now set
  92. mrs_s x0, SYS_ICC_SRE_EL2 // Read SRE back,
  93. tbz x0, #0, .Lskip_gicv3_\@ // and check that it sticks
  94. msr_s SYS_ICH_HCR_EL2, xzr // Reset ICH_HCR_EL2 to defaults
  95. .Lskip_gicv3_\@:
  96. .endm
  97. .macro __init_el2_hstr
  98. msr hstr_el2, xzr // Disable CP15 traps to EL2
  99. .endm
  100. /* Virtual CPU ID registers */
  101. .macro __init_el2_nvhe_idregs
  102. mrs x0, midr_el1
  103. mrs x1, mpidr_el1
  104. msr vpidr_el2, x0
  105. msr vmpidr_el2, x1
  106. .endm
  107. /* Coprocessor traps */
  108. .macro __init_el2_nvhe_cptr
  109. mov x0, #0x33ff
  110. msr cptr_el2, x0 // Disable copro. traps to EL2
  111. .endm
  112. /* Disable any fine grained traps */
  113. .macro __init_el2_fgt
  114. mrs x1, id_aa64mmfr0_el1
  115. ubfx x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
  116. cbz x1, .Lskip_fgt_\@
  117. mov x0, xzr
  118. mrs x1, id_aa64dfr0_el1
  119. ubfx x1, x1, #ID_AA64DFR0_EL1_PMSVer_SHIFT, #4
  120. cmp x1, #3
  121. b.lt .Lset_debug_fgt_\@
  122. /* Disable PMSNEVFR_EL1 read and write traps */
  123. orr x0, x0, #(1 << 62)
  124. .Lset_debug_fgt_\@:
  125. msr_s SYS_HDFGRTR_EL2, x0
  126. msr_s SYS_HDFGWTR_EL2, x0
  127. mov x0, xzr
  128. mrs x1, id_aa64pfr1_el1
  129. ubfx x1, x1, #ID_AA64PFR1_EL1_SME_SHIFT, #4
  130. cbz x1, .Lset_fgt_\@
  131. /* Disable nVHE traps of TPIDR2 and SMPRI */
  132. orr x0, x0, #HFGxTR_EL2_nSMPRI_EL1_MASK
  133. orr x0, x0, #HFGxTR_EL2_nTPIDR2_EL0_MASK
  134. .Lset_fgt_\@:
  135. msr_s SYS_HFGRTR_EL2, x0
  136. msr_s SYS_HFGWTR_EL2, x0
  137. msr_s SYS_HFGITR_EL2, xzr
  138. mrs x1, id_aa64pfr0_el1 // AMU traps UNDEF without AMU
  139. ubfx x1, x1, #ID_AA64PFR0_EL1_AMU_SHIFT, #4
  140. cbz x1, .Lskip_fgt_\@
  141. msr_s SYS_HAFGRTR_EL2, xzr
  142. .Lskip_fgt_\@:
  143. .endm
  144. .macro __init_el2_nvhe_prepare_eret
  145. mov x0, #INIT_PSTATE_EL1
  146. msr spsr_el2, x0
  147. .endm
  148. /**
  149. * Initialize EL2 registers to sane values. This should be called early on all
  150. * cores that were booted in EL2. Note that everything gets initialised as
  151. * if VHE was not evailable. The kernel context will be upgraded to VHE
  152. * if possible later on in the boot process
  153. *
  154. * Regs: x0, x1 and x2 are clobbered.
  155. */
  156. .macro init_el2_state
  157. __init_el2_sctlr
  158. __init_el2_timers
  159. __init_el2_debug
  160. __init_el2_lor
  161. __init_el2_stage2
  162. __init_el2_gicv3
  163. __init_el2_hstr
  164. __init_el2_nvhe_idregs
  165. __init_el2_nvhe_cptr
  166. __init_el2_fgt
  167. __init_el2_nvhe_prepare_eret
  168. .endm
  169. #ifndef __KVM_NVHE_HYPERVISOR__
  170. // This will clobber tmp1 and tmp2, and expect tmp1 to contain
  171. // the id register value as read from the HW
  172. .macro __check_override idreg, fld, width, pass, fail, tmp1, tmp2
  173. ubfx \tmp1, \tmp1, #\fld, #\width
  174. cbz \tmp1, \fail
  175. adr_l \tmp1, \idreg\()_override
  176. ldr \tmp2, [\tmp1, FTR_OVR_VAL_OFFSET]
  177. ldr \tmp1, [\tmp1, FTR_OVR_MASK_OFFSET]
  178. ubfx \tmp2, \tmp2, #\fld, #\width
  179. ubfx \tmp1, \tmp1, #\fld, #\width
  180. cmp \tmp1, xzr
  181. and \tmp2, \tmp2, \tmp1
  182. csinv \tmp2, \tmp2, xzr, ne
  183. cbnz \tmp2, \pass
  184. b \fail
  185. .endm
  186. // This will clobber tmp1 and tmp2
  187. .macro check_override idreg, fld, pass, fail, tmp1, tmp2
  188. mrs \tmp1, \idreg\()_el1
  189. __check_override \idreg \fld 4 \pass \fail \tmp1 \tmp2
  190. .endm
  191. #else
  192. // This will clobber tmp
  193. .macro __check_override idreg, fld, width, pass, fail, tmp, ignore
  194. ldr_l \tmp, \idreg\()_el1_sys_val
  195. ubfx \tmp, \tmp, #\fld, #\width
  196. cbnz \tmp, \pass
  197. b \fail
  198. .endm
  199. .macro check_override idreg, fld, pass, fail, tmp, ignore
  200. __check_override \idreg \fld 4 \pass \fail \tmp \ignore
  201. .endm
  202. #endif
  203. .macro finalise_el2_state
  204. check_override id_aa64pfr0, ID_AA64PFR0_EL1_SVE_SHIFT, .Linit_sve_\@, .Lskip_sve_\@, x1, x2
  205. .Linit_sve_\@: /* SVE register access */
  206. mrs x0, cptr_el2 // Disable SVE traps
  207. bic x0, x0, #CPTR_EL2_TZ
  208. msr cptr_el2, x0
  209. isb
  210. mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector
  211. msr_s SYS_ZCR_EL2, x1 // length for EL1.
  212. .Lskip_sve_\@:
  213. check_override id_aa64pfr1, ID_AA64PFR1_EL1_SME_SHIFT, .Linit_sme_\@, .Lskip_sme_\@, x1, x2
  214. .Linit_sme_\@: /* SME register access and priority mapping */
  215. mrs x0, cptr_el2 // Disable SME traps
  216. bic x0, x0, #CPTR_EL2_TSM
  217. msr cptr_el2, x0
  218. isb
  219. mrs x1, sctlr_el2
  220. orr x1, x1, #SCTLR_ELx_ENTP2 // Disable TPIDR2 traps
  221. msr sctlr_el2, x1
  222. isb
  223. mov x0, #0 // SMCR controls
  224. // Full FP in SM?
  225. mrs_s x1, SYS_ID_AA64SMFR0_EL1
  226. __check_override id_aa64smfr0, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, .Linit_sme_fa64_\@, .Lskip_sme_fa64_\@, x1, x2
  227. .Linit_sme_fa64_\@:
  228. orr x0, x0, SMCR_ELx_FA64_MASK
  229. .Lskip_sme_fa64_\@:
  230. orr x0, x0, #SMCR_ELx_LEN_MASK // Enable full SME vector
  231. msr_s SYS_SMCR_EL2, x0 // length for EL1.
  232. mrs_s x1, SYS_SMIDR_EL1 // Priority mapping supported?
  233. ubfx x1, x1, #SMIDR_EL1_SMPS_SHIFT, #1
  234. cbz x1, .Lskip_sme_\@
  235. msr_s SYS_SMPRIMAP_EL2, xzr // Make all priorities equal
  236. mrs x1, id_aa64mmfr1_el1 // HCRX_EL2 present?
  237. ubfx x1, x1, #ID_AA64MMFR1_EL1_HCX_SHIFT, #4
  238. cbz x1, .Lskip_sme_\@
  239. mrs_s x1, SYS_HCRX_EL2
  240. orr x1, x1, #HCRX_EL2_SMPME_MASK // Enable priority mapping
  241. msr_s SYS_HCRX_EL2, x1
  242. .Lskip_sme_\@:
  243. .endm
  244. #endif /* __ARM_KVM_INIT_H__ */