proc.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_cache_off()
  43. *
  44. * Turn the CPU D-cache off.
  45. */
  46. ENTRY(cpu_cache_off)
  47. mrs x0, sctlr_el1
  48. bic x0, x0, #1 << 2 // clear SCTLR.C
  49. msr sctlr_el1, x0
  50. isb
  51. ret
  52. ENDPROC(cpu_cache_off)
  53. /*
  54. * cpu_reset(loc)
  55. *
  56. * Perform a soft reset of the system. Put the CPU into the same state
  57. * as it would be if it had been reset, and branch to what would be the
  58. * reset vector. It must be executed with the flat identity mapping.
  59. *
  60. * - loc - location to jump to for soft reset
  61. */
  62. .align 5
  63. ENTRY(cpu_reset)
  64. mrs x1, sctlr_el1
  65. bic x1, x1, #1
  66. msr sctlr_el1, x1 // disable the MMU
  67. isb
  68. ret x0
  69. ENDPROC(cpu_reset)
  70. /*
  71. * cpu_do_idle()
  72. *
  73. * Idle the processor (wait for interrupt).
  74. */
  75. ENTRY(cpu_do_idle)
  76. dsb sy // WFI may enter a low-power mode
  77. wfi
  78. ret
  79. ENDPROC(cpu_do_idle)
  80. #ifdef CONFIG_ARM64_CPU_SUSPEND
  81. /**
  82. * cpu_do_suspend - save CPU registers context
  83. *
  84. * x0: virtual address of context pointer
  85. */
  86. ENTRY(cpu_do_suspend)
  87. mrs x2, tpidr_el0
  88. mrs x3, tpidrro_el0
  89. mrs x4, contextidr_el1
  90. mrs x5, mair_el1
  91. mrs x6, cpacr_el1
  92. mrs x7, ttbr1_el1
  93. mrs x8, tcr_el1
  94. mrs x9, vbar_el1
  95. mrs x10, mdscr_el1
  96. mrs x11, oslsr_el1
  97. mrs x12, sctlr_el1
  98. stp x2, x3, [x0]
  99. stp x4, x5, [x0, #16]
  100. stp x6, x7, [x0, #32]
  101. stp x8, x9, [x0, #48]
  102. stp x10, x11, [x0, #64]
  103. str x12, [x0, #80]
  104. ret
  105. ENDPROC(cpu_do_suspend)
  106. /**
  107. * cpu_do_resume - restore CPU register context
  108. *
  109. * x0: Physical address of context pointer
  110. * x1: ttbr0_el1 to be restored
  111. *
  112. * Returns:
  113. * sctlr_el1 value in x0
  114. */
  115. ENTRY(cpu_do_resume)
  116. /*
  117. * Invalidate local tlb entries before turning on MMU
  118. */
  119. tlbi vmalle1
  120. ldp x2, x3, [x0]
  121. ldp x4, x5, [x0, #16]
  122. ldp x6, x7, [x0, #32]
  123. ldp x8, x9, [x0, #48]
  124. ldp x10, x11, [x0, #64]
  125. ldr x12, [x0, #80]
  126. msr tpidr_el0, x2
  127. msr tpidrro_el0, x3
  128. msr contextidr_el1, x4
  129. msr mair_el1, x5
  130. msr cpacr_el1, x6
  131. msr ttbr0_el1, x1
  132. msr ttbr1_el1, x7
  133. msr tcr_el1, x8
  134. msr vbar_el1, x9
  135. msr mdscr_el1, x10
  136. /*
  137. * Restore oslsr_el1 by writing oslar_el1
  138. */
  139. ubfx x11, x11, #1, #1
  140. msr oslar_el1, x11
  141. mov x0, x12
  142. dsb nsh // Make sure local tlb invalidation completed
  143. isb
  144. ret
  145. ENDPROC(cpu_do_resume)
  146. #endif
  147. /*
  148. * cpu_do_switch_mm(pgd_phys, tsk)
  149. *
  150. * Set the translation table base pointer to be pgd_phys.
  151. *
  152. * - pgd_phys - physical address of new TTB
  153. */
  154. ENTRY(cpu_do_switch_mm)
  155. mmid w1, x1 // get mm->context.id
  156. bfi x0, x1, #48, #16 // set the ASID
  157. msr ttbr0_el1, x0 // set TTBR0
  158. isb
  159. ret
  160. ENDPROC(cpu_do_switch_mm)
  161. .section ".text.init", #alloc, #execinstr
  162. /*
  163. * __cpu_setup
  164. *
  165. * Initialise the processor for turning the MMU on. Return in x0 the
  166. * value of the SCTLR_EL1 register.
  167. */
  168. ENTRY(__cpu_setup)
  169. ic iallu // I+BTB cache invalidate
  170. tlbi vmalle1is // invalidate I + D TLBs
  171. dsb ish
  172. mov x0, #3 << 20
  173. msr cpacr_el1, x0 // Enable FP/ASIMD
  174. msr mdscr_el1, xzr // Reset mdscr_el1
  175. /*
  176. * Memory region attributes for LPAE:
  177. *
  178. * n = AttrIndx[2:0]
  179. * n MAIR
  180. * DEVICE_nGnRnE 000 00000000
  181. * DEVICE_nGnRE 001 00000100
  182. * DEVICE_GRE 010 00001100
  183. * NORMAL_NC 011 01000100
  184. * NORMAL 100 11111111
  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. msr mair_el1, x5
  192. /*
  193. * Prepare SCTLR
  194. */
  195. adr x5, crval
  196. ldp w5, w6, [x5]
  197. mrs x0, sctlr_el1
  198. bic x0, x0, x5 // clear bits
  199. orr x0, x0, x6 // set bits
  200. /*
  201. * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
  202. * both user and kernel.
  203. */
  204. ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  205. TCR_TG_FLAGS | TCR_ASID16 | TCR_TBI0
  206. /*
  207. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the IPS bits in
  208. * TCR_EL1.
  209. */
  210. mrs x9, ID_AA64MMFR0_EL1
  211. bfi x10, x9, #32, #3
  212. msr tcr_el1, x10
  213. ret // return to head.S
  214. ENDPROC(__cpu_setup)
  215. /*
  216. * n n T
  217. * U E WT T UD US IHBS
  218. * CE0 XWHW CZ ME TEEA S
  219. * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
  220. * 0011 0... 1101 ..0. ..0. 10.. .... .... < hardware reserved
  221. * .... .1.. .... 01.1 11.1 ..01 0001 1101 < software settings
  222. */
  223. .type crval, #object
  224. crval:
  225. .word 0x000802e2 // clear
  226. .word 0x0405d11d // set