proc.S 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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-hwdef.h>
  26. #include <asm/pgtable.h>
  27. #include "proc-macros.S"
  28. #ifdef CONFIG_ARM64_64K_PAGES
  29. #define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K
  30. #elif defined(CONFIG_ARM64_16K_PAGES)
  31. #define TCR_TG_FLAGS TCR_TG0_16K | TCR_TG1_16K
  32. #else /* CONFIG_ARM64_4K_PAGES */
  33. #define TCR_TG_FLAGS TCR_TG0_4K | TCR_TG1_4K
  34. #endif
  35. #define TCR_SMP_FLAGS TCR_SHARED
  36. /* PTWs cacheable, inner/outer WBWA */
  37. #define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA
  38. #define MAIR(attr, mt) ((attr) << ((mt) * 8))
  39. /*
  40. * cpu_do_idle()
  41. *
  42. * Idle the processor (wait for interrupt).
  43. */
  44. ENTRY(cpu_do_idle)
  45. dsb sy // WFI may enter a low-power mode
  46. wfi
  47. ret
  48. ENDPROC(cpu_do_idle)
  49. #ifdef CONFIG_CPU_PM
  50. /**
  51. * cpu_do_suspend - save CPU registers context
  52. *
  53. * x0: virtual address of context pointer
  54. */
  55. ENTRY(cpu_do_suspend)
  56. mrs x2, tpidr_el0
  57. mrs x3, tpidrro_el0
  58. mrs x4, contextidr_el1
  59. mrs x5, mair_el1
  60. mrs x6, cpacr_el1
  61. mrs x7, ttbr1_el1
  62. mrs x8, tcr_el1
  63. mrs x9, vbar_el1
  64. mrs x10, mdscr_el1
  65. mrs x11, oslsr_el1
  66. mrs x12, sctlr_el1
  67. stp x2, x3, [x0]
  68. stp x4, x5, [x0, #16]
  69. stp x6, x7, [x0, #32]
  70. stp x8, x9, [x0, #48]
  71. stp x10, x11, [x0, #64]
  72. str x12, [x0, #80]
  73. ret
  74. ENDPROC(cpu_do_suspend)
  75. /**
  76. * cpu_do_resume - restore CPU register context
  77. *
  78. * x0: Physical address of context pointer
  79. * x1: ttbr0_el1 to be restored
  80. *
  81. * Returns:
  82. * sctlr_el1 value in x0
  83. */
  84. ENTRY(cpu_do_resume)
  85. /*
  86. * Invalidate local tlb entries before turning on MMU
  87. */
  88. tlbi vmalle1
  89. ldp x2, x3, [x0]
  90. ldp x4, x5, [x0, #16]
  91. ldp x6, x7, [x0, #32]
  92. ldp x8, x9, [x0, #48]
  93. ldp x10, x11, [x0, #64]
  94. ldr x12, [x0, #80]
  95. msr tpidr_el0, x2
  96. msr tpidrro_el0, x3
  97. msr contextidr_el1, x4
  98. msr mair_el1, x5
  99. msr cpacr_el1, x6
  100. msr ttbr0_el1, x1
  101. msr ttbr1_el1, x7
  102. tcr_set_idmap_t0sz x8, x7
  103. msr tcr_el1, x8
  104. msr vbar_el1, x9
  105. msr mdscr_el1, x10
  106. /*
  107. * Restore oslsr_el1 by writing oslar_el1
  108. */
  109. ubfx x11, x11, #1, #1
  110. msr oslar_el1, x11
  111. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  112. mov x0, x12
  113. dsb nsh // Make sure local tlb invalidation completed
  114. isb
  115. ret
  116. ENDPROC(cpu_do_resume)
  117. #endif
  118. /*
  119. * cpu_do_switch_mm(pgd_phys, tsk)
  120. *
  121. * Set the translation table base pointer to be pgd_phys.
  122. *
  123. * - pgd_phys - physical address of new TTB
  124. */
  125. ENTRY(cpu_do_switch_mm)
  126. mmid x1, x1 // get mm->context.id
  127. bfi x0, x1, #48, #16 // set the ASID
  128. msr ttbr0_el1, x0 // set TTBR0
  129. isb
  130. ret
  131. ENDPROC(cpu_do_switch_mm)
  132. /*
  133. * __cpu_setup
  134. *
  135. * Initialise the processor for turning the MMU on. Return in x0 the
  136. * value of the SCTLR_EL1 register.
  137. */
  138. ENTRY(__cpu_setup)
  139. tlbi vmalle1 // Invalidate local TLB
  140. dsb nsh
  141. mov x0, #3 << 20
  142. msr cpacr_el1, x0 // Enable FP/ASIMD
  143. mov x0, #1 << 12 // Reset mdscr_el1 and disable
  144. msr mdscr_el1, x0 // access to the DCC from EL0
  145. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  146. /*
  147. * Memory region attributes for LPAE:
  148. *
  149. * n = AttrIndx[2:0]
  150. * n MAIR
  151. * DEVICE_nGnRnE 000 00000000
  152. * DEVICE_nGnRE 001 00000100
  153. * DEVICE_GRE 010 00001100
  154. * NORMAL_NC 011 01000100
  155. * NORMAL 100 11111111
  156. * NORMAL_WT 101 10111011
  157. */
  158. ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
  159. MAIR(0x04, MT_DEVICE_nGnRE) | \
  160. MAIR(0x0c, MT_DEVICE_GRE) | \
  161. MAIR(0x44, MT_NORMAL_NC) | \
  162. MAIR(0xff, MT_NORMAL) | \
  163. MAIR(0xbb, MT_NORMAL_WT)
  164. msr mair_el1, x5
  165. /*
  166. * Prepare SCTLR
  167. */
  168. adr x5, crval
  169. ldp w5, w6, [x5]
  170. mrs x0, sctlr_el1
  171. bic x0, x0, x5 // clear bits
  172. orr x0, x0, x6 // set bits
  173. /*
  174. * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
  175. * both user and kernel.
  176. */
  177. ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  178. TCR_TG_FLAGS | TCR_ASID16 | TCR_TBI0
  179. tcr_set_idmap_t0sz x10, x9
  180. /*
  181. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the IPS bits in
  182. * TCR_EL1.
  183. */
  184. mrs x9, ID_AA64MMFR0_EL1
  185. bfi x10, x9, #32, #3
  186. #ifdef CONFIG_ARM64_HW_AFDBM
  187. /*
  188. * Hardware update of the Access and Dirty bits.
  189. */
  190. mrs x9, ID_AA64MMFR1_EL1
  191. and x9, x9, #0xf
  192. cbz x9, 2f
  193. cmp x9, #2
  194. b.lt 1f
  195. orr x10, x10, #TCR_HD // hardware Dirty flag update
  196. 1: orr x10, x10, #TCR_HA // hardware Access flag update
  197. 2:
  198. #endif /* CONFIG_ARM64_HW_AFDBM */
  199. msr tcr_el1, x10
  200. ret // return to head.S
  201. ENDPROC(__cpu_setup)
  202. /*
  203. * We set the desired value explicitly, including those of the
  204. * reserved bits. The values of bits EE & E0E were set early in
  205. * el2_setup, which are left untouched below.
  206. *
  207. * n n T
  208. * U E WT T UD US IHBS
  209. * CE0 XWHW CZ ME TEEA S
  210. * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
  211. * 0011 0... 1101 ..0. ..0. 10.. .0.. .... < hardware reserved
  212. * .... .1.. .... 01.1 11.1 ..01 0.01 1101 < software settings
  213. */
  214. .type crval, #object
  215. crval:
  216. .word 0xfcffffff // clear
  217. .word 0x34d5d91d // set