proc.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifndef CONFIG_SMP
  29. /* PTWs cacheable, inner/outer WBWA not shareable */
  30. #define TCR_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA
  31. #else
  32. /* PTWs cacheable, inner/outer WBWA shareable */
  33. #define TCR_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA | TCR_SHARED
  34. #endif
  35. #define MAIR(attr, mt) ((attr) << ((mt) * 8))
  36. /*
  37. * cpu_cache_off()
  38. *
  39. * Turn the CPU D-cache off.
  40. */
  41. ENTRY(cpu_cache_off)
  42. mrs x0, sctlr_el1
  43. bic x0, x0, #1 << 2 // clear SCTLR.C
  44. msr sctlr_el1, x0
  45. isb
  46. ret
  47. ENDPROC(cpu_cache_off)
  48. /*
  49. * cpu_reset(loc)
  50. *
  51. * Perform a soft reset of the system. Put the CPU into the same state
  52. * as it would be if it had been reset, and branch to what would be the
  53. * reset vector. It must be executed with the flat identity mapping.
  54. *
  55. * - loc - location to jump to for soft reset
  56. */
  57. .align 5
  58. ENTRY(cpu_reset)
  59. mrs x1, sctlr_el1
  60. bic x1, x1, #1
  61. msr sctlr_el1, x1 // disable the MMU
  62. isb
  63. ret x0
  64. ENDPROC(cpu_reset)
  65. /*
  66. * cpu_do_idle()
  67. *
  68. * Idle the processor (wait for interrupt).
  69. */
  70. ENTRY(cpu_do_idle)
  71. dsb sy // WFI may enter a low-power mode
  72. wfi
  73. ret
  74. ENDPROC(cpu_do_idle)
  75. #ifdef CONFIG_ARM64_CPU_SUSPEND
  76. /**
  77. * cpu_do_suspend - save CPU registers context
  78. *
  79. * x0: virtual address of context pointer
  80. */
  81. ENTRY(cpu_do_suspend)
  82. mrs x2, tpidr_el0
  83. mrs x3, tpidrro_el0
  84. mrs x4, contextidr_el1
  85. mrs x5, mair_el1
  86. mrs x6, cpacr_el1
  87. mrs x7, ttbr1_el1
  88. mrs x8, tcr_el1
  89. mrs x9, vbar_el1
  90. mrs x10, mdscr_el1
  91. mrs x11, oslsr_el1
  92. mrs x12, sctlr_el1
  93. stp x2, x3, [x0]
  94. stp x4, x5, [x0, #16]
  95. stp x6, x7, [x0, #32]
  96. stp x8, x9, [x0, #48]
  97. stp x10, x11, [x0, #64]
  98. str x12, [x0, #80]
  99. ret
  100. ENDPROC(cpu_do_suspend)
  101. /**
  102. * cpu_do_resume - restore CPU register context
  103. *
  104. * x0: Physical address of context pointer
  105. * x1: ttbr0_el1 to be restored
  106. *
  107. * Returns:
  108. * sctlr_el1 value in x0
  109. */
  110. ENTRY(cpu_do_resume)
  111. /*
  112. * Invalidate local tlb entries before turning on MMU
  113. */
  114. tlbi vmalle1
  115. ldp x2, x3, [x0]
  116. ldp x4, x5, [x0, #16]
  117. ldp x6, x7, [x0, #32]
  118. ldp x8, x9, [x0, #48]
  119. ldp x10, x11, [x0, #64]
  120. ldr x12, [x0, #80]
  121. msr tpidr_el0, x2
  122. msr tpidrro_el0, x3
  123. msr contextidr_el1, x4
  124. msr mair_el1, x5
  125. msr cpacr_el1, x6
  126. msr ttbr0_el1, x1
  127. msr ttbr1_el1, x7
  128. msr tcr_el1, x8
  129. msr vbar_el1, x9
  130. msr mdscr_el1, x10
  131. /*
  132. * Restore oslsr_el1 by writing oslar_el1
  133. */
  134. ubfx x11, x11, #1, #1
  135. msr oslar_el1, x11
  136. mov x0, x12
  137. dsb nsh // Make sure local tlb invalidation completed
  138. isb
  139. ret
  140. ENDPROC(cpu_do_resume)
  141. #endif
  142. /*
  143. * cpu_do_switch_mm(pgd_phys, tsk)
  144. *
  145. * Set the translation table base pointer to be pgd_phys.
  146. *
  147. * - pgd_phys - physical address of new TTB
  148. */
  149. ENTRY(cpu_do_switch_mm)
  150. mmid w1, x1 // get mm->context.id
  151. bfi x0, x1, #48, #16 // set the ASID
  152. msr ttbr0_el1, x0 // set TTBR0
  153. isb
  154. ret
  155. ENDPROC(cpu_do_switch_mm)
  156. .section ".text.init", #alloc, #execinstr
  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. ENTRY(__cpu_setup)
  164. /*
  165. * Preserve the link register across the function call.
  166. */
  167. mov x28, lr
  168. bl __flush_dcache_all
  169. mov lr, x28
  170. ic iallu // I+BTB cache invalidate
  171. tlbi vmalle1is // invalidate I + D TLBs
  172. dsb sy
  173. mov x0, #3 << 20
  174. msr cpacr_el1, x0 // Enable FP/ASIMD
  175. msr mdscr_el1, xzr // Reset mdscr_el1
  176. /*
  177. * Memory region attributes for LPAE:
  178. *
  179. * n = AttrIndx[2:0]
  180. * n MAIR
  181. * DEVICE_nGnRnE 000 00000000
  182. * DEVICE_nGnRE 001 00000100
  183. * DEVICE_GRE 010 00001100
  184. * NORMAL_NC 011 01000100
  185. * NORMAL 100 11111111
  186. */
  187. ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
  188. MAIR(0x04, MT_DEVICE_nGnRE) | \
  189. MAIR(0x0c, MT_DEVICE_GRE) | \
  190. MAIR(0x44, MT_NORMAL_NC) | \
  191. MAIR(0xff, MT_NORMAL)
  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_FLAGS | TCR_IPS_40BIT | \
  206. TCR_ASID16 | TCR_TBI0 | (1 << 31)
  207. #ifdef CONFIG_ARM64_64K_PAGES
  208. orr x10, x10, TCR_TG0_64K
  209. orr x10, x10, TCR_TG1_64K
  210. #endif
  211. msr tcr_el1, x10
  212. ret // return to head.S
  213. ENDPROC(__cpu_setup)
  214. /*
  215. * n n T
  216. * U E WT T UD US IHBS
  217. * CE0 XWHW CZ ME TEEA S
  218. * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
  219. * 0011 0... 1101 ..0. ..0. 10.. .... .... < hardware reserved
  220. * .... .1.. .... 01.1 11.1 ..01 0001 1101 < software settings
  221. */
  222. .type crval, #object
  223. crval:
  224. .word 0x000802e2 // clear
  225. .word 0x0405d11d // set