proc.S 5.0 KB

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