el2_setup.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #ifndef __ARM_KVM_INIT_H__
  7. #define __ARM_KVM_INIT_H__
  8. #ifndef __ASSEMBLER__
  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_hcr val
  16. mov_q x0, \val
  17. /*
  18. * Compliant CPUs advertise their VHE-onlyness with
  19. * ID_AA64MMFR4_EL1.E2H0 < 0. On such CPUs HCR_EL2.E2H is RES1, but it
  20. * can reset into an UNKNOWN state and might not read as 1 until it has
  21. * been initialized explicitly.
  22. * Initialize HCR_EL2.E2H so that later code can rely upon HCR_EL2.E2H
  23. * indicating whether the CPU is running in E2H mode.
  24. */
  25. mrs_s x1, SYS_ID_AA64MMFR4_EL1
  26. sbfx x1, x1, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
  27. cmp x1, #0
  28. b.lt .LnE2H0_\@
  29. /*
  30. * Unfortunately, HCR_EL2.E2H can be RES1 even if not advertised
  31. * as such via ID_AA64MMFR4_EL1.E2H0:
  32. *
  33. * - Fruity CPUs predate the !FEAT_E2H0 relaxation, and seem to
  34. * have HCR_EL2.E2H implemented as RAO/WI.
  35. *
  36. * - On CPUs that lack FEAT_FGT, a hypervisor can't trap guest
  37. * reads of ID_AA64MMFR4_EL1 to advertise !FEAT_E2H0. NV
  38. * guests on these hosts can write to HCR_EL2.E2H without
  39. * trapping to the hypervisor, but these writes have no
  40. * functional effect.
  41. *
  42. * Handle both cases by checking for an essential VHE property
  43. * (system register remapping) to decide whether we're
  44. * effectively VHE-only or not.
  45. */
  46. msr_hcr_el2 x0 // Setup HCR_EL2 as nVHE
  47. isb
  48. mov x1, #1 // Write something to FAR_EL1
  49. msr far_el1, x1
  50. isb
  51. mov x1, #2 // Try to overwrite it via FAR_EL2
  52. msr far_el2, x1
  53. isb
  54. mrs x1, far_el1 // If we see the latest write in FAR_EL1,
  55. cmp x1, #2 // we can safely assume we are VHE only.
  56. b.ne .LnVHE_\@ // Otherwise, we know that nVHE works.
  57. .LnE2H0_\@:
  58. orr x0, x0, #HCR_E2H
  59. msr_hcr_el2 x0
  60. isb
  61. .LnVHE_\@:
  62. .endm
  63. .macro __init_el2_sctlr
  64. mov_q x0, INIT_SCTLR_EL2_MMU_OFF
  65. msr sctlr_el2, x0
  66. isb
  67. .endm
  68. .macro __init_el2_hcrx
  69. mrs x0, id_aa64mmfr1_el1
  70. ubfx x0, x0, #ID_AA64MMFR1_EL1_HCX_SHIFT, #4
  71. cbz x0, .Lskip_hcrx_\@
  72. mov_q x0, (HCRX_EL2_MSCEn | HCRX_EL2_TCR2En | HCRX_EL2_EnFPM)
  73. /* Enable GCS if supported */
  74. mrs_s x1, SYS_ID_AA64PFR1_EL1
  75. ubfx x1, x1, #ID_AA64PFR1_EL1_GCS_SHIFT, #4
  76. cbz x1, .Lskip_gcs_hcrx_\@
  77. orr x0, x0, #HCRX_EL2_GCSEn
  78. .Lskip_gcs_hcrx_\@:
  79. /* Enable LS64, LS64_V if supported */
  80. mrs_s x1, SYS_ID_AA64ISAR1_EL1
  81. ubfx x1, x1, #ID_AA64ISAR1_EL1_LS64_SHIFT, #4
  82. cbz x1, .Lset_hcrx_\@
  83. orr x0, x0, #HCRX_EL2_EnALS
  84. cmp x1, #ID_AA64ISAR1_EL1_LS64_LS64_V
  85. b.lt .Lset_hcrx_\@
  86. orr x0, x0, #HCRX_EL2_EnASR
  87. .Lset_hcrx_\@:
  88. msr_s SYS_HCRX_EL2, x0
  89. .Lskip_hcrx_\@:
  90. .endm
  91. /* Check if running in host at EL2 mode, i.e., (h)VHE. Jump to fail if not. */
  92. .macro __check_hvhe fail, tmp
  93. mrs \tmp, hcr_el2
  94. and \tmp, \tmp, #HCR_E2H
  95. cbz \tmp, \fail
  96. .endm
  97. /*
  98. * Allow Non-secure EL1 and EL0 to access physical timer and counter.
  99. * This is not necessary for VHE, since the host kernel runs in EL2,
  100. * and EL0 accesses are configured in the later stage of boot process.
  101. * Note that when HCR_EL2.E2H == 1, CNTHCTL_EL2 has the same bit layout
  102. * as CNTKCTL_EL1, and CNTKCTL_EL1 accessing instructions are redefined
  103. * to access CNTHCTL_EL2. This allows the kernel designed to run at EL1
  104. * to transparently mess with the EL0 bits via CNTKCTL_EL1 access in
  105. * EL2.
  106. */
  107. .macro __init_el2_timers
  108. mov x0, #3 // Enable EL1 physical timers
  109. __check_hvhe .LnVHE_\@, x1
  110. lsl x0, x0, #10
  111. .LnVHE_\@:
  112. msr cnthctl_el2, x0
  113. msr cntvoff_el2, xzr // Clear virtual offset
  114. .endm
  115. /* Branch to skip_label if SPE version is less than given version */
  116. .macro __spe_vers_imp skip_label, version, tmp
  117. mrs \tmp, id_aa64dfr0_el1
  118. ubfx \tmp, \tmp, #ID_AA64DFR0_EL1_PMSVer_SHIFT, #4
  119. cmp \tmp, \version
  120. b.lt \skip_label
  121. .endm
  122. .macro __init_el2_debug
  123. mrs x1, id_aa64dfr0_el1
  124. ubfx x0, x1, #ID_AA64DFR0_EL1_PMUVer_SHIFT, #4
  125. cmp x0, #ID_AA64DFR0_EL1_PMUVer_NI
  126. ccmp x0, #ID_AA64DFR0_EL1_PMUVer_IMP_DEF, #4, ne
  127. b.eq .Lskip_pmu_\@ // Skip if no PMU present or IMP_DEF
  128. mrs x0, pmcr_el0 // Disable debug access traps
  129. ubfx x0, x0, #11, #5 // to EL2 and allow access to
  130. .Lskip_pmu_\@:
  131. csel x2, xzr, x0, eq // all PMU counters from EL1
  132. /* Statistical profiling */
  133. __spe_vers_imp .Lskip_spe_\@, ID_AA64DFR0_EL1_PMSVer_IMP, x0 // Skip if SPE not present
  134. mrs_s x0, SYS_PMBIDR_EL1 // If SPE available at EL2,
  135. and x0, x0, #(1 << PMBIDR_EL1_P_SHIFT)
  136. cbnz x0, .Lskip_spe_el2_\@ // then permit sampling of physical
  137. mov x0, #(1 << PMSCR_EL2_PCT_SHIFT | \
  138. 1 << PMSCR_EL2_PA_SHIFT)
  139. msr_s SYS_PMSCR_EL2, x0 // addresses and physical counter
  140. .Lskip_spe_el2_\@:
  141. mov x0, #MDCR_EL2_E2PB_MASK
  142. orr x2, x2, x0 // If we don't have VHE, then
  143. // use EL1&0 translation.
  144. .Lskip_spe_\@:
  145. /* Trace buffer */
  146. ubfx x0, x1, #ID_AA64DFR0_EL1_TraceBuffer_SHIFT, #4
  147. cbz x0, .Lskip_trace_\@ // Skip if TraceBuffer is not present
  148. mrs_s x0, SYS_TRBIDR_EL1
  149. and x0, x0, TRBIDR_EL1_P
  150. cbnz x0, .Lskip_trace_\@ // If TRBE is available at EL2
  151. mov x0, #MDCR_EL2_E2TB_MASK
  152. orr x2, x2, x0 // allow the EL1&0 translation
  153. // to own it.
  154. .Lskip_trace_\@:
  155. msr mdcr_el2, x2 // Configure debug traps
  156. .endm
  157. /* LORegions */
  158. .macro __init_el2_lor
  159. mrs x1, id_aa64mmfr1_el1
  160. ubfx x0, x1, #ID_AA64MMFR1_EL1_LO_SHIFT, 4
  161. cbz x0, .Lskip_lor_\@
  162. msr_s SYS_LORC_EL1, xzr
  163. .Lskip_lor_\@:
  164. .endm
  165. /* Stage-2 translation */
  166. .macro __init_el2_stage2
  167. msr vttbr_el2, xzr
  168. .endm
  169. /* GICv3 system register access */
  170. .macro __init_el2_gicv3
  171. mrs x0, id_aa64pfr0_el1
  172. ubfx x0, x0, #ID_AA64PFR0_EL1_GIC_SHIFT, #4
  173. cbz x0, .Lskip_gicv3_\@
  174. mrs_s x0, SYS_ICC_SRE_EL2
  175. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  176. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  177. msr_s SYS_ICC_SRE_EL2, x0
  178. isb // Make sure SRE is now set
  179. mrs_s x0, SYS_ICC_SRE_EL2 // Read SRE back,
  180. tbz x0, #0, .Lskip_gicv3_\@ // and check that it sticks
  181. msr_s SYS_ICH_HCR_EL2, xzr // Reset ICH_HCR_EL2 to defaults
  182. .Lskip_gicv3_\@:
  183. .endm
  184. /* GICv5 system register access */
  185. .macro __init_el2_gicv5
  186. mrs_s x0, SYS_ID_AA64PFR2_EL1
  187. ubfx x0, x0, #ID_AA64PFR2_EL1_GCIE_SHIFT, #4
  188. cbz x0, .Lskip_gicv5_\@
  189. mov x0, #(ICH_HFGITR_EL2_GICRCDNMIA | \
  190. ICH_HFGITR_EL2_GICRCDIA | \
  191. ICH_HFGITR_EL2_GICCDDI | \
  192. ICH_HFGITR_EL2_GICCDEOI | \
  193. ICH_HFGITR_EL2_GICCDHM | \
  194. ICH_HFGITR_EL2_GICCDRCFG | \
  195. ICH_HFGITR_EL2_GICCDPEND | \
  196. ICH_HFGITR_EL2_GICCDAFF | \
  197. ICH_HFGITR_EL2_GICCDPRI | \
  198. ICH_HFGITR_EL2_GICCDDIS | \
  199. ICH_HFGITR_EL2_GICCDEN)
  200. msr_s SYS_ICH_HFGITR_EL2, x0 // Disable instruction traps
  201. mov_q x0, (ICH_HFGRTR_EL2_ICC_PPI_ACTIVERn_EL1 | \
  202. ICH_HFGRTR_EL2_ICC_PPI_PRIORITYRn_EL1 | \
  203. ICH_HFGRTR_EL2_ICC_PPI_PENDRn_EL1 | \
  204. ICH_HFGRTR_EL2_ICC_PPI_ENABLERn_EL1 | \
  205. ICH_HFGRTR_EL2_ICC_PPI_HMRn_EL1 | \
  206. ICH_HFGRTR_EL2_ICC_IAFFIDR_EL1 | \
  207. ICH_HFGRTR_EL2_ICC_ICSR_EL1 | \
  208. ICH_HFGRTR_EL2_ICC_PCR_EL1 | \
  209. ICH_HFGRTR_EL2_ICC_HPPIR_EL1 | \
  210. ICH_HFGRTR_EL2_ICC_CR0_EL1 | \
  211. ICH_HFGRTR_EL2_ICC_IDRn_EL1 | \
  212. ICH_HFGRTR_EL2_ICC_APR_EL1)
  213. msr_s SYS_ICH_HFGRTR_EL2, x0 // Disable reg read traps
  214. mov_q x0, (ICH_HFGWTR_EL2_ICC_PPI_ACTIVERn_EL1 | \
  215. ICH_HFGWTR_EL2_ICC_PPI_PRIORITYRn_EL1 | \
  216. ICH_HFGWTR_EL2_ICC_PPI_PENDRn_EL1 | \
  217. ICH_HFGWTR_EL2_ICC_PPI_ENABLERn_EL1 | \
  218. ICH_HFGWTR_EL2_ICC_ICSR_EL1 | \
  219. ICH_HFGWTR_EL2_ICC_PCR_EL1 | \
  220. ICH_HFGWTR_EL2_ICC_CR0_EL1 | \
  221. ICH_HFGWTR_EL2_ICC_APR_EL1)
  222. msr_s SYS_ICH_HFGWTR_EL2, x0 // Disable reg write traps
  223. .Lskip_gicv5_\@:
  224. .endm
  225. .macro __init_el2_hstr
  226. msr hstr_el2, xzr // Disable CP15 traps to EL2
  227. .endm
  228. /* Virtual CPU ID registers */
  229. .macro __init_el2_nvhe_idregs
  230. mrs x0, midr_el1
  231. mrs x1, mpidr_el1
  232. msr vpidr_el2, x0
  233. msr vmpidr_el2, x1
  234. .endm
  235. /* Coprocessor traps */
  236. .macro __init_el2_cptr
  237. __check_hvhe .LnVHE_\@, x1
  238. mov x0, #CPACR_EL1_FPEN
  239. msr cpacr_el1, x0
  240. b .Lskip_set_cptr_\@
  241. .LnVHE_\@:
  242. mov x0, #0x33ff
  243. msr cptr_el2, x0 // Disable copro. traps to EL2
  244. .Lskip_set_cptr_\@:
  245. .endm
  246. /*
  247. * Configure BRBE to permit recording cycle counts and branch mispredicts.
  248. *
  249. * At any EL, to record cycle counts BRBE requires that both BRBCR_EL2.CC=1 and
  250. * BRBCR_EL1.CC=1.
  251. *
  252. * At any EL, to record branch mispredicts BRBE requires that both
  253. * BRBCR_EL2.MPRED=1 and BRBCR_EL1.MPRED=1.
  254. *
  255. * Set {CC,MPRED} in BRBCR_EL2 in case nVHE mode is used and we are
  256. * executing in EL1.
  257. */
  258. .macro __init_el2_brbe
  259. mrs x1, id_aa64dfr0_el1
  260. ubfx x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
  261. cbz x1, .Lskip_brbe_\@
  262. mov_q x0, BRBCR_ELx_CC | BRBCR_ELx_MPRED
  263. msr_s SYS_BRBCR_EL2, x0
  264. .Lskip_brbe_\@:
  265. .endm
  266. /* Disable any fine grained traps */
  267. .macro __init_el2_fgt
  268. mrs x1, id_aa64mmfr0_el1
  269. ubfx x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
  270. cbz x1, .Lskip_fgt_\@
  271. mov x0, xzr
  272. mov x2, xzr
  273. /* If SPEv1p2 is implemented, */
  274. __spe_vers_imp .Lskip_spe_fgt_\@, #ID_AA64DFR0_EL1_PMSVer_V1P2, x1
  275. /* Disable PMSNEVFR_EL1 read and write traps */
  276. orr x0, x0, #HDFGRTR_EL2_nPMSNEVFR_EL1_MASK
  277. orr x2, x2, #HDFGWTR_EL2_nPMSNEVFR_EL1_MASK
  278. .Lskip_spe_fgt_\@:
  279. mrs x1, id_aa64dfr0_el1
  280. ubfx x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
  281. cbz x1, .Lskip_brbe_fgt_\@
  282. /*
  283. * Disable read traps for the following registers
  284. *
  285. * [BRBSRC|BRBTGT|RBINF]_EL1
  286. * [BRBSRCINJ|BRBTGTINJ|BRBINFINJ|BRBTS]_EL1
  287. */
  288. orr x0, x0, #HDFGRTR_EL2_nBRBDATA_MASK
  289. /*
  290. * Disable write traps for the following registers
  291. *
  292. * [BRBSRCINJ|BRBTGTINJ|BRBINFINJ|BRBTS]_EL1
  293. */
  294. orr x2, x2, #HDFGWTR_EL2_nBRBDATA_MASK
  295. /* Disable read and write traps for [BRBCR|BRBFCR]_EL1 */
  296. orr x0, x0, #HDFGRTR_EL2_nBRBCTL_MASK
  297. orr x2, x2, #HDFGWTR_EL2_nBRBCTL_MASK
  298. /* Disable read traps for BRBIDR_EL1 */
  299. orr x0, x0, #HDFGRTR_EL2_nBRBIDR_MASK
  300. .Lskip_brbe_fgt_\@:
  301. .Lset_debug_fgt_\@:
  302. msr_s SYS_HDFGRTR_EL2, x0
  303. msr_s SYS_HDFGWTR_EL2, x2
  304. mov x0, xzr
  305. mov x2, xzr
  306. mrs x1, id_aa64dfr0_el1
  307. ubfx x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
  308. cbz x1, .Lskip_brbe_insn_fgt_\@
  309. /* Disable traps for BRBIALL instruction */
  310. orr x2, x2, #HFGITR_EL2_nBRBIALL_MASK
  311. /* Disable traps for BRBINJ instruction */
  312. orr x2, x2, #HFGITR_EL2_nBRBINJ_MASK
  313. .Lskip_brbe_insn_fgt_\@:
  314. mrs x1, id_aa64pfr1_el1
  315. ubfx x1, x1, #ID_AA64PFR1_EL1_SME_SHIFT, #4
  316. cbz x1, .Lskip_sme_fgt_\@
  317. /* Disable nVHE traps of TPIDR2 and SMPRI */
  318. orr x0, x0, #HFGRTR_EL2_nSMPRI_EL1_MASK
  319. orr x0, x0, #HFGRTR_EL2_nTPIDR2_EL0_MASK
  320. .Lskip_sme_fgt_\@:
  321. mrs_s x1, SYS_ID_AA64MMFR3_EL1
  322. ubfx x1, x1, #ID_AA64MMFR3_EL1_S1PIE_SHIFT, #4
  323. cbz x1, .Lskip_pie_fgt_\@
  324. /* Disable trapping of PIR_EL1 / PIRE0_EL1 */
  325. orr x0, x0, #HFGRTR_EL2_nPIR_EL1
  326. orr x0, x0, #HFGRTR_EL2_nPIRE0_EL1
  327. .Lskip_pie_fgt_\@:
  328. mrs_s x1, SYS_ID_AA64MMFR3_EL1
  329. ubfx x1, x1, #ID_AA64MMFR3_EL1_S1POE_SHIFT, #4
  330. cbz x1, .Lskip_poe_fgt_\@
  331. /* Disable trapping of POR_EL0 */
  332. orr x0, x0, #HFGRTR_EL2_nPOR_EL0
  333. .Lskip_poe_fgt_\@:
  334. /* GCS depends on PIE so we don't check it if PIE is absent */
  335. mrs_s x1, SYS_ID_AA64PFR1_EL1
  336. ubfx x1, x1, #ID_AA64PFR1_EL1_GCS_SHIFT, #4
  337. cbz x1, .Lskip_gce_fgt_\@
  338. /* Disable traps of access to GCS registers at EL0 and EL1 */
  339. orr x0, x0, #HFGRTR_EL2_nGCS_EL1_MASK
  340. orr x0, x0, #HFGRTR_EL2_nGCS_EL0_MASK
  341. .Lskip_gce_fgt_\@:
  342. .Lset_fgt_\@:
  343. msr_s SYS_HFGRTR_EL2, x0
  344. msr_s SYS_HFGWTR_EL2, x0
  345. msr_s SYS_HFGITR_EL2, x2
  346. mrs x1, id_aa64pfr0_el1 // AMU traps UNDEF without AMU
  347. ubfx x1, x1, #ID_AA64PFR0_EL1_AMU_SHIFT, #4
  348. cbz x1, .Lskip_amu_fgt_\@
  349. msr_s SYS_HAFGRTR_EL2, xzr
  350. .Lskip_amu_fgt_\@:
  351. .Lskip_fgt_\@:
  352. .endm
  353. .macro __init_el2_fgt2
  354. mrs x1, id_aa64mmfr0_el1
  355. ubfx x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
  356. cmp x1, #ID_AA64MMFR0_EL1_FGT_FGT2
  357. b.lt .Lskip_fgt2_\@
  358. mov x0, xzr
  359. mrs x1, id_aa64dfr0_el1
  360. ubfx x1, x1, #ID_AA64DFR0_EL1_PMUVer_SHIFT, #4
  361. cmp x1, #ID_AA64DFR0_EL1_PMUVer_V3P9
  362. b.lt .Lskip_pmuv3p9_\@
  363. orr x0, x0, #HDFGRTR2_EL2_nPMICNTR_EL0
  364. orr x0, x0, #HDFGRTR2_EL2_nPMICFILTR_EL0
  365. orr x0, x0, #HDFGRTR2_EL2_nPMUACR_EL1
  366. .Lskip_pmuv3p9_\@:
  367. /* If SPE is implemented, */
  368. __spe_vers_imp .Lskip_spefds_\@, ID_AA64DFR0_EL1_PMSVer_IMP, x1
  369. /* we can read PMSIDR and */
  370. mrs_s x1, SYS_PMSIDR_EL1
  371. and x1, x1, #PMSIDR_EL1_FDS
  372. /* if FEAT_SPE_FDS is implemented, */
  373. cbz x1, .Lskip_spefds_\@
  374. /* disable traps of PMSDSFR to EL2. */
  375. orr x0, x0, #HDFGRTR2_EL2_nPMSDSFR_EL1
  376. .Lskip_spefds_\@:
  377. msr_s SYS_HDFGRTR2_EL2, x0
  378. msr_s SYS_HDFGWTR2_EL2, x0
  379. msr_s SYS_HFGRTR2_EL2, xzr
  380. msr_s SYS_HFGWTR2_EL2, xzr
  381. msr_s SYS_HFGITR2_EL2, xzr
  382. .Lskip_fgt2_\@:
  383. .endm
  384. /**
  385. * Initialize EL2 registers to sane values. This should be called early on all
  386. * cores that were booted in EL2. Note that everything gets initialised as
  387. * if VHE was not available. The kernel context will be upgraded to VHE
  388. * if possible later on in the boot process
  389. *
  390. * Regs: x0, x1 and x2 are clobbered.
  391. */
  392. .macro init_el2_state
  393. __init_el2_sctlr
  394. __init_el2_hcrx
  395. __init_el2_timers
  396. __init_el2_debug
  397. __init_el2_brbe
  398. __init_el2_lor
  399. __init_el2_stage2
  400. __init_el2_gicv3
  401. __init_el2_gicv5
  402. __init_el2_hstr
  403. __init_el2_nvhe_idregs
  404. __init_el2_cptr
  405. __init_el2_fgt
  406. __init_el2_fgt2
  407. .endm
  408. #ifndef __KVM_NVHE_HYPERVISOR__
  409. // This will clobber tmp1 and tmp2, and expect tmp1 to contain
  410. // the id register value as read from the HW
  411. .macro __check_override idreg, fld, width, pass, fail, tmp1, tmp2
  412. ubfx \tmp1, \tmp1, #\fld, #\width
  413. cbz \tmp1, \fail
  414. adr_l \tmp1, \idreg\()_override
  415. ldr \tmp2, [\tmp1, FTR_OVR_VAL_OFFSET]
  416. ldr \tmp1, [\tmp1, FTR_OVR_MASK_OFFSET]
  417. ubfx \tmp2, \tmp2, #\fld, #\width
  418. ubfx \tmp1, \tmp1, #\fld, #\width
  419. cmp \tmp1, xzr
  420. and \tmp2, \tmp2, \tmp1
  421. csinv \tmp2, \tmp2, xzr, ne
  422. cbnz \tmp2, \pass
  423. b \fail
  424. .endm
  425. // This will clobber tmp1 and tmp2
  426. .macro check_override idreg, fld, pass, fail, tmp1, tmp2
  427. mrs \tmp1, \idreg\()_el1
  428. __check_override \idreg \fld 4 \pass \fail \tmp1 \tmp2
  429. .endm
  430. #else
  431. // This will clobber tmp
  432. .macro __check_override idreg, fld, width, pass, fail, tmp, ignore
  433. ldr_l \tmp, \idreg\()_el1_sys_val
  434. ubfx \tmp, \tmp, #\fld, #\width
  435. cbnz \tmp, \pass
  436. b \fail
  437. .endm
  438. .macro check_override idreg, fld, pass, fail, tmp, ignore
  439. __check_override \idreg \fld 4 \pass \fail \tmp \ignore
  440. .endm
  441. #endif
  442. .macro finalise_el2_state
  443. check_override id_aa64pfr0, ID_AA64PFR0_EL1_MPAM_SHIFT, .Linit_mpam_\@, .Lskip_mpam_\@, x1, x2
  444. .Linit_mpam_\@:
  445. msr_s SYS_MPAM2_EL2, xzr // use the default partition
  446. // and disable lower traps
  447. mrs_s x0, SYS_MPAMIDR_EL1
  448. tbz x0, #MPAMIDR_EL1_HAS_HCR_SHIFT, .Lskip_mpam_\@ // skip if no MPAMHCR reg
  449. msr_s SYS_MPAMHCR_EL2, xzr // clear TRAP_MPAMIDR_EL1 -> EL2
  450. .Lskip_mpam_\@:
  451. check_override id_aa64pfr1, ID_AA64PFR1_EL1_GCS_SHIFT, .Linit_gcs_\@, .Lskip_gcs_\@, x1, x2
  452. .Linit_gcs_\@:
  453. msr_s SYS_GCSCR_EL1, xzr
  454. msr_s SYS_GCSCRE0_EL1, xzr
  455. .Lskip_gcs_\@:
  456. check_override id_aa64pfr0, ID_AA64PFR0_EL1_SVE_SHIFT, .Linit_sve_\@, .Lskip_sve_\@, x1, x2
  457. .Linit_sve_\@: /* SVE register access */
  458. __check_hvhe .Lcptr_nvhe_\@, x1
  459. // (h)VHE case
  460. mrs x0, cpacr_el1 // Disable SVE traps
  461. orr x0, x0, #CPACR_EL1_ZEN
  462. msr cpacr_el1, x0
  463. b .Lskip_set_cptr_\@
  464. .Lcptr_nvhe_\@: // nVHE case
  465. mrs x0, cptr_el2 // Disable SVE traps
  466. bic x0, x0, #CPTR_EL2_TZ
  467. msr cptr_el2, x0
  468. .Lskip_set_cptr_\@:
  469. isb
  470. mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector
  471. msr_s SYS_ZCR_EL2, x1 // length for EL1.
  472. .Lskip_sve_\@:
  473. check_override id_aa64pfr1, ID_AA64PFR1_EL1_SME_SHIFT, .Linit_sme_\@, .Lskip_sme_\@, x1, x2
  474. .Linit_sme_\@: /* SME register access and priority mapping */
  475. __check_hvhe .Lcptr_nvhe_sme_\@, x1
  476. // (h)VHE case
  477. mrs x0, cpacr_el1 // Disable SME traps
  478. orr x0, x0, #CPACR_EL1_SMEN
  479. msr cpacr_el1, x0
  480. b .Lskip_set_cptr_sme_\@
  481. .Lcptr_nvhe_sme_\@: // nVHE case
  482. mrs x0, cptr_el2 // Disable SME traps
  483. bic x0, x0, #CPTR_EL2_TSM
  484. msr cptr_el2, x0
  485. .Lskip_set_cptr_sme_\@:
  486. isb
  487. mrs x1, sctlr_el2
  488. orr x1, x1, #SCTLR_ELx_ENTP2 // Disable TPIDR2 traps
  489. msr sctlr_el2, x1
  490. isb
  491. mov x0, #0 // SMCR controls
  492. // Full FP in SM?
  493. mrs_s x1, SYS_ID_AA64SMFR0_EL1
  494. __check_override id_aa64smfr0, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, .Linit_sme_fa64_\@, .Lskip_sme_fa64_\@, x1, x2
  495. .Linit_sme_fa64_\@:
  496. orr x0, x0, SMCR_ELx_FA64_MASK
  497. .Lskip_sme_fa64_\@:
  498. // ZT0 available?
  499. mrs_s x1, SYS_ID_AA64SMFR0_EL1
  500. __check_override id_aa64smfr0, ID_AA64SMFR0_EL1_SMEver_SHIFT, 4, .Linit_sme_zt0_\@, .Lskip_sme_zt0_\@, x1, x2
  501. .Linit_sme_zt0_\@:
  502. orr x0, x0, SMCR_ELx_EZT0_MASK
  503. .Lskip_sme_zt0_\@:
  504. orr x0, x0, #SMCR_ELx_LEN_MASK // Enable full SME vector
  505. msr_s SYS_SMCR_EL2, x0 // length for EL1.
  506. mrs_s x1, SYS_SMIDR_EL1 // Priority mapping supported?
  507. ubfx x1, x1, #SMIDR_EL1_SMPS_SHIFT, #1
  508. cbz x1, .Lskip_sme_\@
  509. msr_s SYS_SMPRIMAP_EL2, xzr // Make all priorities equal
  510. .Lskip_sme_\@:
  511. .endm
  512. #endif /* __ARM_KVM_INIT_H__ */