proc.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. ENTRY(cpu_soft_restart)
  71. /* Save address of cpu_reset() and reset address */
  72. mov x19, x0
  73. mov x20, x1
  74. /* Turn D-cache off */
  75. bl cpu_cache_off
  76. /* Push out all dirty data, and ensure cache is empty */
  77. bl flush_cache_all
  78. mov x0, x20
  79. ret x19
  80. ENDPROC(cpu_soft_restart)
  81. /*
  82. * cpu_do_idle()
  83. *
  84. * Idle the processor (wait for interrupt).
  85. */
  86. ENTRY(cpu_do_idle)
  87. dsb sy // WFI may enter a low-power mode
  88. wfi
  89. ret
  90. ENDPROC(cpu_do_idle)
  91. #ifdef CONFIG_CPU_PM
  92. /**
  93. * cpu_do_suspend - save CPU registers context
  94. *
  95. * x0: virtual address of context pointer
  96. */
  97. ENTRY(cpu_do_suspend)
  98. mrs x2, tpidr_el0
  99. mrs x3, tpidrro_el0
  100. mrs x4, contextidr_el1
  101. mrs x5, mair_el1
  102. mrs x6, cpacr_el1
  103. mrs x7, ttbr1_el1
  104. mrs x8, tcr_el1
  105. mrs x9, vbar_el1
  106. mrs x10, mdscr_el1
  107. mrs x11, oslsr_el1
  108. mrs x12, sctlr_el1
  109. stp x2, x3, [x0]
  110. stp x4, x5, [x0, #16]
  111. stp x6, x7, [x0, #32]
  112. stp x8, x9, [x0, #48]
  113. stp x10, x11, [x0, #64]
  114. str x12, [x0, #80]
  115. ret
  116. ENDPROC(cpu_do_suspend)
  117. /**
  118. * cpu_do_resume - restore CPU register context
  119. *
  120. * x0: Physical address of context pointer
  121. * x1: ttbr0_el1 to be restored
  122. *
  123. * Returns:
  124. * sctlr_el1 value in x0
  125. */
  126. ENTRY(cpu_do_resume)
  127. /*
  128. * Invalidate local tlb entries before turning on MMU
  129. */
  130. tlbi vmalle1
  131. ldp x2, x3, [x0]
  132. ldp x4, x5, [x0, #16]
  133. ldp x6, x7, [x0, #32]
  134. ldp x8, x9, [x0, #48]
  135. ldp x10, x11, [x0, #64]
  136. ldr x12, [x0, #80]
  137. msr tpidr_el0, x2
  138. msr tpidrro_el0, x3
  139. msr contextidr_el1, x4
  140. msr mair_el1, x5
  141. msr cpacr_el1, x6
  142. msr ttbr0_el1, x1
  143. msr ttbr1_el1, x7
  144. tcr_set_idmap_t0sz x8, x7
  145. msr tcr_el1, x8
  146. msr vbar_el1, x9
  147. msr mdscr_el1, x10
  148. /*
  149. * Restore oslsr_el1 by writing oslar_el1
  150. */
  151. ubfx x11, x11, #1, #1
  152. msr oslar_el1, x11
  153. mov x0, x12
  154. dsb nsh // Make sure local tlb invalidation completed
  155. isb
  156. ret
  157. ENDPROC(cpu_do_resume)
  158. #endif
  159. /*
  160. * cpu_do_switch_mm(pgd_phys, tsk)
  161. *
  162. * Set the translation table base pointer to be pgd_phys.
  163. *
  164. * - pgd_phys - physical address of new TTB
  165. */
  166. ENTRY(cpu_do_switch_mm)
  167. mmid w1, x1 // get mm->context.id
  168. bfi x0, x1, #48, #16 // set the ASID
  169. msr ttbr0_el1, x0 // set TTBR0
  170. isb
  171. ret
  172. ENDPROC(cpu_do_switch_mm)
  173. .section ".text.init", #alloc, #execinstr
  174. /*
  175. * __cpu_setup
  176. *
  177. * Initialise the processor for turning the MMU on. Return in x0 the
  178. * value of the SCTLR_EL1 register.
  179. */
  180. ENTRY(__cpu_setup)
  181. ic iallu // I+BTB cache invalidate
  182. tlbi vmalle1is // invalidate I + D TLBs
  183. dsb ish
  184. mov x0, #3 << 20
  185. msr cpacr_el1, x0 // Enable FP/ASIMD
  186. msr mdscr_el1, xzr // Reset mdscr_el1
  187. /*
  188. * Memory region attributes for LPAE:
  189. *
  190. * n = AttrIndx[2:0]
  191. * n MAIR
  192. * DEVICE_nGnRnE 000 00000000
  193. * DEVICE_nGnRE 001 00000100
  194. * DEVICE_GRE 010 00001100
  195. * NORMAL_NC 011 01000100
  196. * NORMAL 100 11111111
  197. */
  198. ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
  199. MAIR(0x04, MT_DEVICE_nGnRE) | \
  200. MAIR(0x0c, MT_DEVICE_GRE) | \
  201. MAIR(0x44, MT_NORMAL_NC) | \
  202. MAIR(0xff, MT_NORMAL)
  203. msr mair_el1, x5
  204. /*
  205. * Prepare SCTLR
  206. */
  207. adr x5, crval
  208. ldp w5, w6, [x5]
  209. mrs x0, sctlr_el1
  210. bic x0, x0, x5 // clear bits
  211. orr x0, x0, x6 // set bits
  212. /*
  213. * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
  214. * both user and kernel.
  215. */
  216. ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  217. TCR_TG_FLAGS | TCR_ASID16 | TCR_TBI0
  218. tcr_set_idmap_t0sz x10, x9
  219. /*
  220. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the IPS bits in
  221. * TCR_EL1.
  222. */
  223. mrs x9, ID_AA64MMFR0_EL1
  224. bfi x10, x9, #32, #3
  225. msr tcr_el1, x10
  226. ret // return to head.S
  227. ENDPROC(__cpu_setup)
  228. /*
  229. * We set the desired value explicitly, including those of the
  230. * reserved bits. The values of bits EE & E0E were set early in
  231. * el2_setup, which are left untouched below.
  232. *
  233. * n n T
  234. * U E WT T UD US IHBS
  235. * CE0 XWHW CZ ME TEEA S
  236. * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
  237. * 0011 0... 1101 ..0. ..0. 10.. .0.. .... < hardware reserved
  238. * .... .1.. .... 01.1 11.1 ..01 0.01 1101 < software settings
  239. */
  240. .type crval, #object
  241. crval:
  242. .word 0xfcffffff // clear
  243. .word 0x34d5d91d // set