proc.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Based on arch/arm/mm/proc.S
  3. *
  4. * Copyright (C) 2001 Deep Blue Solutions Ltd.
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Author: Catalin Marinas <catalin.marinas@arm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/linkage.h>
  22. #include <asm/assembler.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/hwcap.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/pgtable-hwdef.h>
  27. #include <asm/cpufeature.h>
  28. #include <asm/alternative.h>
  29. #ifdef CONFIG_ARM64_64K_PAGES
  30. #define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K
  31. #elif defined(CONFIG_ARM64_16K_PAGES)
  32. #define TCR_TG_FLAGS TCR_TG0_16K | TCR_TG1_16K
  33. #else /* CONFIG_ARM64_4K_PAGES */
  34. #define TCR_TG_FLAGS TCR_TG0_4K | TCR_TG1_4K
  35. #endif
  36. #define TCR_SMP_FLAGS TCR_SHARED
  37. /* PTWs cacheable, inner/outer WBWA */
  38. #define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA
  39. #define MAIR(attr, mt) ((attr) << ((mt) * 8))
  40. /*
  41. * cpu_do_idle()
  42. *
  43. * Idle the processor (wait for interrupt).
  44. */
  45. ENTRY(cpu_do_idle)
  46. dsb sy // WFI may enter a low-power mode
  47. wfi
  48. ret
  49. ENDPROC(cpu_do_idle)
  50. #ifdef CONFIG_CPU_PM
  51. /**
  52. * cpu_do_suspend - save CPU registers context
  53. *
  54. * x0: virtual address of context pointer
  55. */
  56. ENTRY(cpu_do_suspend)
  57. mrs x2, tpidr_el0
  58. mrs x3, tpidrro_el0
  59. mrs x4, contextidr_el1
  60. mrs x5, cpacr_el1
  61. mrs x6, tcr_el1
  62. mrs x7, vbar_el1
  63. mrs x8, mdscr_el1
  64. mrs x9, oslsr_el1
  65. mrs x10, sctlr_el1
  66. stp x2, x3, [x0]
  67. stp x4, xzr, [x0, #16]
  68. stp x5, x6, [x0, #32]
  69. stp x7, x8, [x0, #48]
  70. stp x9, x10, [x0, #64]
  71. ret
  72. ENDPROC(cpu_do_suspend)
  73. /**
  74. * cpu_do_resume - restore CPU register context
  75. *
  76. * x0: Address of context pointer
  77. */
  78. .pushsection ".idmap.text", "awx"
  79. ENTRY(cpu_do_resume)
  80. ldp x2, x3, [x0]
  81. ldp x4, x5, [x0, #16]
  82. ldp x6, x8, [x0, #32]
  83. ldp x9, x10, [x0, #48]
  84. ldp x11, x12, [x0, #64]
  85. msr tpidr_el0, x2
  86. msr tpidrro_el0, x3
  87. msr contextidr_el1, x4
  88. msr cpacr_el1, x6
  89. /* Don't change t0sz here, mask those bits when restoring */
  90. mrs x5, tcr_el1
  91. bfi x8, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  92. msr tcr_el1, x8
  93. msr vbar_el1, x9
  94. /*
  95. * __cpu_setup() cleared MDSCR_EL1.MDE and friends, before unmasking
  96. * debug exceptions. By restoring MDSCR_EL1 here, we may take a debug
  97. * exception. Mask them until local_dbg_restore() in cpu_suspend()
  98. * resets them.
  99. */
  100. disable_dbg
  101. msr mdscr_el1, x10
  102. msr sctlr_el1, x12
  103. /*
  104. * Restore oslsr_el1 by writing oslar_el1
  105. */
  106. ubfx x11, x11, #1, #1
  107. msr oslar_el1, x11
  108. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  109. isb
  110. ret
  111. ENDPROC(cpu_do_resume)
  112. .popsection
  113. #endif
  114. /*
  115. * cpu_do_switch_mm(pgd_phys, tsk)
  116. *
  117. * Set the translation table base pointer to be pgd_phys.
  118. *
  119. * - pgd_phys - physical address of new TTB
  120. */
  121. ENTRY(cpu_do_switch_mm)
  122. mrs x2, ttbr1_el1
  123. mmid x1, x1 // get mm->context.id
  124. bfi x2, x1, #48, #16 // set the ASID
  125. msr ttbr1_el1, x2 // in TTBR1 (since TCR.A1 is set)
  126. isb
  127. msr ttbr0_el1, x0 // now update TTBR0
  128. isb
  129. b post_ttbr_update_workaround // Back to C code...
  130. ENDPROC(cpu_do_switch_mm)
  131. .pushsection ".idmap.text", "awx"
  132. .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2
  133. adrp \tmp1, empty_zero_page
  134. msr ttbr1_el1, \tmp1
  135. isb
  136. tlbi vmalle1
  137. dsb nsh
  138. isb
  139. .endm
  140. /*
  141. * void idmap_cpu_replace_ttbr1(phys_addr_t new_pgd)
  142. *
  143. * This is the low-level counterpart to cpu_replace_ttbr1, and should not be
  144. * called by anything else. It can only be executed from a TTBR0 mapping.
  145. */
  146. ENTRY(idmap_cpu_replace_ttbr1)
  147. mrs x2, daif
  148. msr daifset, #0xf
  149. __idmap_cpu_set_reserved_ttbr1 x1, x3
  150. msr ttbr1_el1, x0
  151. isb
  152. msr daif, x2
  153. ret
  154. ENDPROC(idmap_cpu_replace_ttbr1)
  155. .popsection
  156. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  157. .pushsection ".idmap.text", "awx"
  158. .macro __idmap_kpti_get_pgtable_ent, type
  159. dc cvac, cur_\()\type\()p // Ensure any existing dirty
  160. dmb sy // lines are written back before
  161. ldr \type, [cur_\()\type\()p] // loading the entry
  162. tbz \type, #0, next_\()\type // Skip invalid entries
  163. .endm
  164. .macro __idmap_kpti_put_pgtable_ent_ng, type
  165. orr \type, \type, #PTE_NG // Same bit for blocks and pages
  166. str \type, [cur_\()\type\()p] // Update the entry and ensure
  167. dmb sy // that it is visible to all
  168. dc civac, cur_\()\type\()p // CPUs.
  169. .endm
  170. /*
  171. * void __kpti_install_ng_mappings(int cpu, int num_cpus, phys_addr_t swapper)
  172. *
  173. * Called exactly once from stop_machine context by each CPU found during boot.
  174. */
  175. __idmap_kpti_flag:
  176. .long 1
  177. ENTRY(idmap_kpti_install_ng_mappings)
  178. cpu .req w0
  179. num_cpus .req w1
  180. swapper_pa .req x2
  181. swapper_ttb .req x3
  182. flag_ptr .req x4
  183. cur_pgdp .req x5
  184. end_pgdp .req x6
  185. pgd .req x7
  186. cur_pudp .req x8
  187. end_pudp .req x9
  188. pud .req x10
  189. cur_pmdp .req x11
  190. end_pmdp .req x12
  191. pmd .req x13
  192. cur_ptep .req x14
  193. end_ptep .req x15
  194. pte .req x16
  195. mrs swapper_ttb, ttbr1_el1
  196. adr flag_ptr, __idmap_kpti_flag
  197. cbnz cpu, __idmap_kpti_secondary
  198. /* We're the boot CPU. Wait for the others to catch up */
  199. sevl
  200. 1: wfe
  201. ldaxr w18, [flag_ptr]
  202. eor w18, w18, num_cpus
  203. cbnz w18, 1b
  204. /* We need to walk swapper, so turn off the MMU. */
  205. mrs x18, sctlr_el1
  206. bic x18, x18, #SCTLR_ELx_M
  207. msr sctlr_el1, x18
  208. isb
  209. /* Everybody is enjoying the idmap, so we can rewrite swapper. */
  210. /* PGD */
  211. mov cur_pgdp, swapper_pa
  212. add end_pgdp, cur_pgdp, #(PTRS_PER_PGD * 8)
  213. do_pgd: __idmap_kpti_get_pgtable_ent pgd
  214. tbnz pgd, #1, walk_puds
  215. __idmap_kpti_put_pgtable_ent_ng pgd
  216. next_pgd:
  217. add cur_pgdp, cur_pgdp, #8
  218. cmp cur_pgdp, end_pgdp
  219. b.ne do_pgd
  220. /* Publish the updated tables and nuke all the TLBs */
  221. dsb sy
  222. tlbi vmalle1is
  223. dsb ish
  224. isb
  225. /* We're done: fire up the MMU again */
  226. mrs x18, sctlr_el1
  227. orr x18, x18, #SCTLR_ELx_M
  228. msr sctlr_el1, x18
  229. isb
  230. /* Set the flag to zero to indicate that we're all done */
  231. str wzr, [flag_ptr]
  232. ret
  233. /* PUD */
  234. walk_puds:
  235. .if CONFIG_PGTABLE_LEVELS > 3
  236. pte_to_phys cur_pudp, pgd
  237. add end_pudp, cur_pudp, #(PTRS_PER_PUD * 8)
  238. do_pud: __idmap_kpti_get_pgtable_ent pud
  239. tbnz pud, #1, walk_pmds
  240. __idmap_kpti_put_pgtable_ent_ng pud
  241. next_pud:
  242. add cur_pudp, cur_pudp, 8
  243. cmp cur_pudp, end_pudp
  244. b.ne do_pud
  245. b next_pgd
  246. .else /* CONFIG_PGTABLE_LEVELS <= 3 */
  247. mov pud, pgd
  248. b walk_pmds
  249. next_pud:
  250. b next_pgd
  251. .endif
  252. /* PMD */
  253. walk_pmds:
  254. .if CONFIG_PGTABLE_LEVELS > 2
  255. pte_to_phys cur_pmdp, pud
  256. add end_pmdp, cur_pmdp, #(PTRS_PER_PMD * 8)
  257. do_pmd: __idmap_kpti_get_pgtable_ent pmd
  258. tbnz pmd, #1, walk_ptes
  259. __idmap_kpti_put_pgtable_ent_ng pmd
  260. next_pmd:
  261. add cur_pmdp, cur_pmdp, #8
  262. cmp cur_pmdp, end_pmdp
  263. b.ne do_pmd
  264. b next_pud
  265. .else /* CONFIG_PGTABLE_LEVELS <= 2 */
  266. mov pmd, pud
  267. b walk_ptes
  268. next_pmd:
  269. b next_pud
  270. .endif
  271. /* PTE */
  272. walk_ptes:
  273. pte_to_phys cur_ptep, pmd
  274. add end_ptep, cur_ptep, #(PTRS_PER_PTE * 8)
  275. do_pte: __idmap_kpti_get_pgtable_ent pte
  276. __idmap_kpti_put_pgtable_ent_ng pte
  277. next_pte:
  278. add cur_ptep, cur_ptep, #8
  279. cmp cur_ptep, end_ptep
  280. b.ne do_pte
  281. b next_pmd
  282. /* Secondary CPUs end up here */
  283. __idmap_kpti_secondary:
  284. /* Uninstall swapper before surgery begins */
  285. __idmap_cpu_set_reserved_ttbr1 x18, x17
  286. /* Increment the flag to let the boot CPU we're ready */
  287. 1: ldxr w18, [flag_ptr]
  288. add w18, w18, #1
  289. stxr w17, w18, [flag_ptr]
  290. cbnz w17, 1b
  291. /* Wait for the boot CPU to finish messing around with swapper */
  292. sevl
  293. 1: wfe
  294. ldxr w18, [flag_ptr]
  295. cbnz w18, 1b
  296. /* All done, act like nothing happened */
  297. msr ttbr1_el1, swapper_ttb
  298. isb
  299. ret
  300. .unreq cpu
  301. .unreq num_cpus
  302. .unreq swapper_pa
  303. .unreq swapper_ttb
  304. .unreq flag_ptr
  305. .unreq cur_pgdp
  306. .unreq end_pgdp
  307. .unreq pgd
  308. .unreq cur_pudp
  309. .unreq end_pudp
  310. .unreq pud
  311. .unreq cur_pmdp
  312. .unreq end_pmdp
  313. .unreq pmd
  314. .unreq cur_ptep
  315. .unreq end_ptep
  316. .unreq pte
  317. ENDPROC(idmap_kpti_install_ng_mappings)
  318. .popsection
  319. #endif
  320. /*
  321. * __cpu_setup
  322. *
  323. * Initialise the processor for turning the MMU on. Return in x0 the
  324. * value of the SCTLR_EL1 register.
  325. */
  326. .pushsection ".idmap.text", "awx"
  327. ENTRY(__cpu_setup)
  328. tlbi vmalle1 // Invalidate local TLB
  329. dsb nsh
  330. mov x0, #3 << 20
  331. msr cpacr_el1, x0 // Enable FP/ASIMD
  332. mov x0, #1 << 12 // Reset mdscr_el1 and disable
  333. msr mdscr_el1, x0 // access to the DCC from EL0
  334. isb // Unmask debug exceptions now,
  335. enable_dbg // since this is per-cpu
  336. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  337. /*
  338. * Memory region attributes for LPAE:
  339. *
  340. * n = AttrIndx[2:0]
  341. * n MAIR
  342. * DEVICE_nGnRnE 000 00000000
  343. * DEVICE_nGnRE 001 00000100
  344. * DEVICE_GRE 010 00001100
  345. * NORMAL_NC 011 01000100
  346. * NORMAL 100 11111111
  347. * NORMAL_WT 101 10111011
  348. */
  349. ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
  350. MAIR(0x04, MT_DEVICE_nGnRE) | \
  351. MAIR(0x0c, MT_DEVICE_GRE) | \
  352. MAIR(0x44, MT_NORMAL_NC) | \
  353. MAIR(0xff, MT_NORMAL) | \
  354. MAIR(0xbb, MT_NORMAL_WT)
  355. msr mair_el1, x5
  356. /*
  357. * Prepare SCTLR
  358. */
  359. adr x5, crval
  360. ldp w5, w6, [x5]
  361. mrs x0, sctlr_el1
  362. bic x0, x0, x5 // clear bits
  363. orr x0, x0, x6 // set bits
  364. /*
  365. * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
  366. * both user and kernel.
  367. */
  368. ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  369. TCR_TG_FLAGS | TCR_ASID16 | TCR_TBI0 | TCR_A1
  370. tcr_set_idmap_t0sz x10, x9
  371. /*
  372. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the IPS bits in
  373. * TCR_EL1.
  374. */
  375. mrs x9, ID_AA64MMFR0_EL1
  376. bfi x10, x9, #32, #3
  377. #ifdef CONFIG_ARM64_HW_AFDBM
  378. /*
  379. * Hardware update of the Access and Dirty bits.
  380. */
  381. mrs x9, ID_AA64MMFR1_EL1
  382. and x9, x9, #0xf
  383. cbz x9, 2f
  384. cmp x9, #2
  385. b.lt 1f
  386. #ifdef CONFIG_ARM64_ERRATUM_1024718
  387. /* Disable hardware DBM on Cortex-A55 r0p0, r0p1 & r1p0 */
  388. cpu_midr_match MIDR_CORTEX_A55, MIDR_CPU_VAR_REV(0, 0), MIDR_CPU_VAR_REV(1, 0), x1, x2, x3, x4
  389. cbnz x1, 1f
  390. #endif
  391. orr x10, x10, #TCR_HD // hardware Dirty flag update
  392. 1: orr x10, x10, #TCR_HA // hardware Access flag update
  393. 2:
  394. #endif /* CONFIG_ARM64_HW_AFDBM */
  395. msr tcr_el1, x10
  396. ret // return to head.S
  397. ENDPROC(__cpu_setup)
  398. /*
  399. * We set the desired value explicitly, including those of the
  400. * reserved bits. The values of bits EE & E0E were set early in
  401. * el2_setup, which are left untouched below.
  402. *
  403. * n n T
  404. * U E WT T UD US IHBS
  405. * CE0 XWHW CZ ME TEEA S
  406. * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
  407. * 0011 0... 1101 ..0. ..0. 10.. .0.. .... < hardware reserved
  408. * .... .1.. .... 01.1 11.1 ..01 0.01 1101 < software settings
  409. */
  410. .type crval, #object
  411. crval:
  412. .word 0xfcffffff // clear
  413. .word 0x34d5d91d // set
  414. .popsection