proc.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. mrs x11, tpidr_el1
  67. mrs x12, sp_el0
  68. stp x2, x3, [x0]
  69. stp x4, xzr, [x0, #16]
  70. stp x5, x6, [x0, #32]
  71. stp x7, x8, [x0, #48]
  72. stp x9, x10, [x0, #64]
  73. stp x11, x12, [x0, #80]
  74. ret
  75. ENDPROC(cpu_do_suspend)
  76. /**
  77. * cpu_do_resume - restore CPU register context
  78. *
  79. * x0: Address of context pointer
  80. */
  81. .pushsection ".idmap.text", "ax"
  82. ENTRY(cpu_do_resume)
  83. ldp x2, x3, [x0]
  84. ldp x4, x5, [x0, #16]
  85. ldp x6, x8, [x0, #32]
  86. ldp x9, x10, [x0, #48]
  87. ldp x11, x12, [x0, #64]
  88. ldp x13, x14, [x0, #80]
  89. msr tpidr_el0, x2
  90. msr tpidrro_el0, x3
  91. msr contextidr_el1, x4
  92. msr cpacr_el1, x6
  93. /* Don't change t0sz here, mask those bits when restoring */
  94. mrs x5, tcr_el1
  95. bfi x8, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  96. msr tcr_el1, x8
  97. msr vbar_el1, x9
  98. /*
  99. * __cpu_setup() cleared MDSCR_EL1.MDE and friends, before unmasking
  100. * debug exceptions. By restoring MDSCR_EL1 here, we may take a debug
  101. * exception. Mask them until local_dbg_restore() in cpu_suspend()
  102. * resets them.
  103. */
  104. disable_dbg
  105. msr mdscr_el1, x10
  106. msr sctlr_el1, x12
  107. msr tpidr_el1, x13
  108. msr sp_el0, x14
  109. /*
  110. * Restore oslsr_el1 by writing oslar_el1
  111. */
  112. ubfx x11, x11, #1, #1
  113. msr oslar_el1, x11
  114. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  115. isb
  116. ret
  117. ENDPROC(cpu_do_resume)
  118. .popsection
  119. #endif
  120. /*
  121. * cpu_do_switch_mm(pgd_phys, tsk)
  122. *
  123. * Set the translation table base pointer to be pgd_phys.
  124. *
  125. * - pgd_phys - physical address of new TTB
  126. */
  127. ENTRY(cpu_do_switch_mm)
  128. mmid x1, x1 // get mm->context.id
  129. bfi x0, x1, #48, #16 // set the ASID
  130. msr ttbr0_el1, x0 // set TTBR0
  131. isb
  132. post_ttbr0_update_workaround
  133. ret
  134. ENDPROC(cpu_do_switch_mm)
  135. .pushsection ".idmap.text", "ax"
  136. /*
  137. * void idmap_cpu_replace_ttbr1(phys_addr_t new_pgd)
  138. *
  139. * This is the low-level counterpart to cpu_replace_ttbr1, and should not be
  140. * called by anything else. It can only be executed from a TTBR0 mapping.
  141. */
  142. ENTRY(idmap_cpu_replace_ttbr1)
  143. mrs x2, daif
  144. msr daifset, #0xf
  145. adrp x1, empty_zero_page
  146. msr ttbr1_el1, x1
  147. isb
  148. tlbi vmalle1
  149. dsb nsh
  150. isb
  151. msr ttbr1_el1, x0
  152. isb
  153. msr daif, x2
  154. ret
  155. ENDPROC(idmap_cpu_replace_ttbr1)
  156. .popsection
  157. /*
  158. * __cpu_setup
  159. *
  160. * Initialise the processor for turning the MMU on. Return in x0 the
  161. * value of the SCTLR_EL1 register.
  162. */
  163. .pushsection ".idmap.text", "ax"
  164. ENTRY(__cpu_setup)
  165. tlbi vmalle1 // Invalidate local TLB
  166. dsb nsh
  167. mov x0, #3 << 20
  168. msr cpacr_el1, x0 // Enable FP/ASIMD
  169. mov x0, #1 << 12 // Reset mdscr_el1 and disable
  170. msr mdscr_el1, x0 // access to the DCC from EL0
  171. isb // Unmask debug exceptions now,
  172. enable_dbg // since this is per-cpu
  173. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  174. /*
  175. * Memory region attributes for LPAE:
  176. *
  177. * n = AttrIndx[2:0]
  178. * n MAIR
  179. * DEVICE_nGnRnE 000 00000000
  180. * DEVICE_nGnRE 001 00000100
  181. * DEVICE_GRE 010 00001100
  182. * NORMAL_NC 011 01000100
  183. * NORMAL 100 11111111
  184. * NORMAL_WT 101 10111011
  185. */
  186. ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
  187. MAIR(0x04, MT_DEVICE_nGnRE) | \
  188. MAIR(0x0c, MT_DEVICE_GRE) | \
  189. MAIR(0x44, MT_NORMAL_NC) | \
  190. MAIR(0xff, MT_NORMAL) | \
  191. MAIR(0xbb, MT_NORMAL_WT)
  192. msr mair_el1, x5
  193. /*
  194. * Prepare SCTLR
  195. */
  196. adr x5, crval
  197. ldp w5, w6, [x5]
  198. mrs x0, sctlr_el1
  199. bic x0, x0, x5 // clear bits
  200. orr x0, x0, x6 // set bits
  201. /*
  202. * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
  203. * both user and kernel.
  204. */
  205. ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  206. TCR_TG_FLAGS | TCR_ASID16 | TCR_TBI0
  207. tcr_set_idmap_t0sz x10, x9
  208. /*
  209. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the IPS bits in
  210. * TCR_EL1.
  211. */
  212. mrs x9, ID_AA64MMFR0_EL1
  213. bfi x10, x9, #32, #3
  214. #ifdef CONFIG_ARM64_HW_AFDBM
  215. /*
  216. * Hardware update of the Access and Dirty bits.
  217. */
  218. mrs x9, ID_AA64MMFR1_EL1
  219. and x9, x9, #0xf
  220. cbz x9, 2f
  221. cmp x9, #2
  222. b.lt 1f
  223. orr x10, x10, #TCR_HD // hardware Dirty flag update
  224. 1: orr x10, x10, #TCR_HA // hardware Access flag update
  225. 2:
  226. #endif /* CONFIG_ARM64_HW_AFDBM */
  227. msr tcr_el1, x10
  228. ret // return to head.S
  229. ENDPROC(__cpu_setup)
  230. /*
  231. * We set the desired value explicitly, including those of the
  232. * reserved bits. The values of bits EE & E0E were set early in
  233. * el2_setup, which are left untouched below.
  234. *
  235. * n n T
  236. * U E WT T UD US IHBS
  237. * CE0 XWHW CZ ME TEEA S
  238. * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
  239. * 0011 0... 1101 ..0. ..0. 10.. .0.. .... < hardware reserved
  240. * .... .1.. .... 01.1 11.1 ..01 0.01 1101 < software settings
  241. */
  242. .type crval, #object
  243. crval:
  244. .word 0xfcffffff // clear
  245. .word 0x34d5d91d // set
  246. .popsection