head.S 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * Low-level CPU initialisation
  3. * Based on arch/arm/kernel/head.S
  4. *
  5. * Copyright (C) 1994-2002 Russell King
  6. * Copyright (C) 2003-2012 ARM Ltd.
  7. * Authors: Catalin Marinas <catalin.marinas@arm.com>
  8. * Will Deacon <will.deacon@arm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/linkage.h>
  23. #include <linux/init.h>
  24. #include <linux/irqchip/arm-gic-v3.h>
  25. #include <asm/assembler.h>
  26. #include <asm/ptrace.h>
  27. #include <asm/asm-offsets.h>
  28. #include <asm/cache.h>
  29. #include <asm/cputype.h>
  30. #include <asm/memory.h>
  31. #include <asm/thread_info.h>
  32. #include <asm/pgtable-hwdef.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/page.h>
  35. #include <asm/virt.h>
  36. #define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET)
  37. #if (TEXT_OFFSET & 0xfff) != 0
  38. #error TEXT_OFFSET must be at least 4KB aligned
  39. #elif (PAGE_OFFSET & 0x1fffff) != 0
  40. #error PAGE_OFFSET must be at least 2MB aligned
  41. #elif TEXT_OFFSET > 0x1fffff
  42. #error TEXT_OFFSET must be less than 2MB
  43. #endif
  44. #ifdef CONFIG_ARM64_64K_PAGES
  45. #define BLOCK_SHIFT PAGE_SHIFT
  46. #define BLOCK_SIZE PAGE_SIZE
  47. #define TABLE_SHIFT PMD_SHIFT
  48. #else
  49. #define BLOCK_SHIFT SECTION_SHIFT
  50. #define BLOCK_SIZE SECTION_SIZE
  51. #define TABLE_SHIFT PUD_SHIFT
  52. #endif
  53. #define KERNEL_START _text
  54. #define KERNEL_END _end
  55. /*
  56. * Initial memory map attributes.
  57. */
  58. #ifndef CONFIG_SMP
  59. #define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF
  60. #define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF
  61. #else
  62. #define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF | PTE_SHARED
  63. #define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S
  64. #endif
  65. #ifdef CONFIG_ARM64_64K_PAGES
  66. #define MM_MMUFLAGS PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
  67. #else
  68. #define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
  69. #endif
  70. /*
  71. * Kernel startup entry point.
  72. * ---------------------------
  73. *
  74. * The requirements are:
  75. * MMU = off, D-cache = off, I-cache = on or off,
  76. * x0 = physical address to the FDT blob.
  77. *
  78. * This code is mostly position independent so you call this at
  79. * __pa(PAGE_OFFSET + TEXT_OFFSET).
  80. *
  81. * Note that the callee-saved registers are used for storing variables
  82. * that are useful before the MMU is enabled. The allocations are described
  83. * in the entry routines.
  84. */
  85. __HEAD
  86. /*
  87. * DO NOT MODIFY. Image header expected by Linux boot-loaders.
  88. */
  89. #ifdef CONFIG_EFI
  90. efi_head:
  91. /*
  92. * This add instruction has no meaningful effect except that
  93. * its opcode forms the magic "MZ" signature required by UEFI.
  94. */
  95. add x13, x18, #0x16
  96. b stext
  97. #else
  98. b stext // branch to kernel start, magic
  99. .long 0 // reserved
  100. #endif
  101. .quad _kernel_offset_le // Image load offset from start of RAM, little-endian
  102. .quad _kernel_size_le // Effective size of kernel image, little-endian
  103. .quad _kernel_flags_le // Informative flags, little-endian
  104. .quad 0 // reserved
  105. .quad 0 // reserved
  106. .quad 0 // reserved
  107. .byte 0x41 // Magic number, "ARM\x64"
  108. .byte 0x52
  109. .byte 0x4d
  110. .byte 0x64
  111. #ifdef CONFIG_EFI
  112. .long pe_header - efi_head // Offset to the PE header.
  113. #else
  114. .word 0 // reserved
  115. #endif
  116. #ifdef CONFIG_EFI
  117. .globl stext_offset
  118. .set stext_offset, stext - efi_head
  119. .align 3
  120. pe_header:
  121. .ascii "PE"
  122. .short 0
  123. coff_header:
  124. .short 0xaa64 // AArch64
  125. .short 2 // nr_sections
  126. .long 0 // TimeDateStamp
  127. .long 0 // PointerToSymbolTable
  128. .long 1 // NumberOfSymbols
  129. .short section_table - optional_header // SizeOfOptionalHeader
  130. .short 0x206 // Characteristics.
  131. // IMAGE_FILE_DEBUG_STRIPPED |
  132. // IMAGE_FILE_EXECUTABLE_IMAGE |
  133. // IMAGE_FILE_LINE_NUMS_STRIPPED
  134. optional_header:
  135. .short 0x20b // PE32+ format
  136. .byte 0x02 // MajorLinkerVersion
  137. .byte 0x14 // MinorLinkerVersion
  138. .long _end - stext // SizeOfCode
  139. .long 0 // SizeOfInitializedData
  140. .long 0 // SizeOfUninitializedData
  141. .long efi_stub_entry - efi_head // AddressOfEntryPoint
  142. .long stext_offset // BaseOfCode
  143. extra_header_fields:
  144. .quad 0 // ImageBase
  145. .long 0x1000 // SectionAlignment
  146. .long PECOFF_FILE_ALIGNMENT // FileAlignment
  147. .short 0 // MajorOperatingSystemVersion
  148. .short 0 // MinorOperatingSystemVersion
  149. .short 0 // MajorImageVersion
  150. .short 0 // MinorImageVersion
  151. .short 0 // MajorSubsystemVersion
  152. .short 0 // MinorSubsystemVersion
  153. .long 0 // Win32VersionValue
  154. .long _end - efi_head // SizeOfImage
  155. // Everything before the kernel image is considered part of the header
  156. .long stext_offset // SizeOfHeaders
  157. .long 0 // CheckSum
  158. .short 0xa // Subsystem (EFI application)
  159. .short 0 // DllCharacteristics
  160. .quad 0 // SizeOfStackReserve
  161. .quad 0 // SizeOfStackCommit
  162. .quad 0 // SizeOfHeapReserve
  163. .quad 0 // SizeOfHeapCommit
  164. .long 0 // LoaderFlags
  165. .long 0x6 // NumberOfRvaAndSizes
  166. .quad 0 // ExportTable
  167. .quad 0 // ImportTable
  168. .quad 0 // ResourceTable
  169. .quad 0 // ExceptionTable
  170. .quad 0 // CertificationTable
  171. .quad 0 // BaseRelocationTable
  172. // Section table
  173. section_table:
  174. /*
  175. * The EFI application loader requires a relocation section
  176. * because EFI applications must be relocatable. This is a
  177. * dummy section as far as we are concerned.
  178. */
  179. .ascii ".reloc"
  180. .byte 0
  181. .byte 0 // end of 0 padding of section name
  182. .long 0
  183. .long 0
  184. .long 0 // SizeOfRawData
  185. .long 0 // PointerToRawData
  186. .long 0 // PointerToRelocations
  187. .long 0 // PointerToLineNumbers
  188. .short 0 // NumberOfRelocations
  189. .short 0 // NumberOfLineNumbers
  190. .long 0x42100040 // Characteristics (section flags)
  191. .ascii ".text"
  192. .byte 0
  193. .byte 0
  194. .byte 0 // end of 0 padding of section name
  195. .long _end - stext // VirtualSize
  196. .long stext_offset // VirtualAddress
  197. .long _edata - stext // SizeOfRawData
  198. .long stext_offset // PointerToRawData
  199. .long 0 // PointerToRelocations (0 for executables)
  200. .long 0 // PointerToLineNumbers (0 for executables)
  201. .short 0 // NumberOfRelocations (0 for executables)
  202. .short 0 // NumberOfLineNumbers (0 for executables)
  203. .long 0xe0500020 // Characteristics (section flags)
  204. /*
  205. * EFI will load stext onwards at the 4k section alignment
  206. * described in the PE/COFF header. To ensure that instruction
  207. * sequences using an adrp and a :lo12: immediate will function
  208. * correctly at this alignment, we must ensure that stext is
  209. * placed at a 4k boundary in the Image to begin with.
  210. */
  211. .align 12
  212. #endif
  213. ENTRY(stext)
  214. bl preserve_boot_args
  215. bl el2_setup // Drop to EL1, w20=cpu_boot_mode
  216. adrp x24, __PHYS_OFFSET
  217. bl set_cpu_boot_mode_flag
  218. bl __vet_fdt
  219. bl __create_page_tables // x25=TTBR0, x26=TTBR1
  220. /*
  221. * The following calls CPU setup code, see arch/arm64/mm/proc.S for
  222. * details.
  223. * On return, the CPU will be ready for the MMU to be turned on and
  224. * the TCR will have been set.
  225. */
  226. ldr x27, =__mmap_switched // address to jump to after
  227. // MMU has been enabled
  228. adr_l lr, __enable_mmu // return (PIC) address
  229. b __cpu_setup // initialise processor
  230. ENDPROC(stext)
  231. /*
  232. * Preserve the arguments passed by the bootloader in x0 .. x3
  233. */
  234. preserve_boot_args:
  235. mov x21, x0 // x21=FDT
  236. adr_l x0, boot_args // record the contents of
  237. stp x21, x1, [x0] // x0 .. x3 at kernel entry
  238. stp x2, x3, [x0, #16]
  239. dmb sy // needed before dc ivac with
  240. // MMU off
  241. add x1, x0, #0x20 // 4 x 8 bytes
  242. b __inval_cache_range // tail call
  243. ENDPROC(preserve_boot_args)
  244. /*
  245. * Determine validity of the x21 FDT pointer.
  246. * The dtb must be 8-byte aligned and live in the first 512M of memory.
  247. */
  248. __vet_fdt:
  249. tst x21, #0x7
  250. b.ne 1f
  251. cmp x21, x24
  252. b.lt 1f
  253. mov x0, #(1 << 29)
  254. add x0, x0, x24
  255. cmp x21, x0
  256. b.ge 1f
  257. ret
  258. 1:
  259. mov x21, #0
  260. ret
  261. ENDPROC(__vet_fdt)
  262. /*
  263. * Macro to create a table entry to the next page.
  264. *
  265. * tbl: page table address
  266. * virt: virtual address
  267. * shift: #imm page table shift
  268. * ptrs: #imm pointers per table page
  269. *
  270. * Preserves: virt
  271. * Corrupts: tmp1, tmp2
  272. * Returns: tbl -> next level table page address
  273. */
  274. .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
  275. lsr \tmp1, \virt, #\shift
  276. and \tmp1, \tmp1, #\ptrs - 1 // table index
  277. add \tmp2, \tbl, #PAGE_SIZE
  278. orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
  279. str \tmp2, [\tbl, \tmp1, lsl #3]
  280. add \tbl, \tbl, #PAGE_SIZE // next level table page
  281. .endm
  282. /*
  283. * Macro to populate the PGD (and possibily PUD) for the corresponding
  284. * block entry in the next level (tbl) for the given virtual address.
  285. *
  286. * Preserves: tbl, next, virt
  287. * Corrupts: tmp1, tmp2
  288. */
  289. .macro create_pgd_entry, tbl, virt, tmp1, tmp2
  290. create_table_entry \tbl, \virt, PGDIR_SHIFT, PTRS_PER_PGD, \tmp1, \tmp2
  291. #if SWAPPER_PGTABLE_LEVELS == 3
  292. create_table_entry \tbl, \virt, TABLE_SHIFT, PTRS_PER_PTE, \tmp1, \tmp2
  293. #endif
  294. .endm
  295. /*
  296. * Macro to populate block entries in the page table for the start..end
  297. * virtual range (inclusive).
  298. *
  299. * Preserves: tbl, flags
  300. * Corrupts: phys, start, end, pstate
  301. */
  302. .macro create_block_map, tbl, flags, phys, start, end
  303. lsr \phys, \phys, #BLOCK_SHIFT
  304. lsr \start, \start, #BLOCK_SHIFT
  305. and \start, \start, #PTRS_PER_PTE - 1 // table index
  306. orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry
  307. lsr \end, \end, #BLOCK_SHIFT
  308. and \end, \end, #PTRS_PER_PTE - 1 // table end index
  309. 9999: str \phys, [\tbl, \start, lsl #3] // store the entry
  310. add \start, \start, #1 // next entry
  311. add \phys, \phys, #BLOCK_SIZE // next block
  312. cmp \start, \end
  313. b.ls 9999b
  314. .endm
  315. /*
  316. * Setup the initial page tables. We only setup the barest amount which is
  317. * required to get the kernel running. The following sections are required:
  318. * - identity mapping to enable the MMU (low address, TTBR0)
  319. * - first few MB of the kernel linear mapping to jump to once the MMU has
  320. * been enabled, including the FDT blob (TTBR1)
  321. * - pgd entry for fixed mappings (TTBR1)
  322. */
  323. __create_page_tables:
  324. adrp x25, idmap_pg_dir
  325. adrp x26, swapper_pg_dir
  326. mov x27, lr
  327. /*
  328. * Invalidate the idmap and swapper page tables to avoid potential
  329. * dirty cache lines being evicted.
  330. */
  331. mov x0, x25
  332. add x1, x26, #SWAPPER_DIR_SIZE
  333. bl __inval_cache_range
  334. /*
  335. * Clear the idmap and swapper page tables.
  336. */
  337. mov x0, x25
  338. add x6, x26, #SWAPPER_DIR_SIZE
  339. 1: stp xzr, xzr, [x0], #16
  340. stp xzr, xzr, [x0], #16
  341. stp xzr, xzr, [x0], #16
  342. stp xzr, xzr, [x0], #16
  343. cmp x0, x6
  344. b.lo 1b
  345. ldr x7, =MM_MMUFLAGS
  346. /*
  347. * Create the identity mapping.
  348. */
  349. mov x0, x25 // idmap_pg_dir
  350. adrp x3, KERNEL_START // __pa(KERNEL_START)
  351. #ifndef CONFIG_ARM64_VA_BITS_48
  352. #define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3)
  353. #define EXTRA_PTRS (1 << (48 - EXTRA_SHIFT))
  354. /*
  355. * If VA_BITS < 48, it may be too small to allow for an ID mapping to be
  356. * created that covers system RAM if that is located sufficiently high
  357. * in the physical address space. So for the ID map, use an extended
  358. * virtual range in that case, by configuring an additional translation
  359. * level.
  360. * First, we have to verify our assumption that the current value of
  361. * VA_BITS was chosen such that all translation levels are fully
  362. * utilised, and that lowering T0SZ will always result in an additional
  363. * translation level to be configured.
  364. */
  365. #if VA_BITS != EXTRA_SHIFT
  366. #error "Mismatch between VA_BITS and page size/number of translation levels"
  367. #endif
  368. /*
  369. * Calculate the maximum allowed value for TCR_EL1.T0SZ so that the
  370. * entire kernel image can be ID mapped. As T0SZ == (64 - #bits used),
  371. * this number conveniently equals the number of leading zeroes in
  372. * the physical address of KERNEL_END.
  373. */
  374. adrp x5, KERNEL_END
  375. clz x5, x5
  376. cmp x5, TCR_T0SZ(VA_BITS) // default T0SZ small enough?
  377. b.ge 1f // .. then skip additional level
  378. adr_l x6, idmap_t0sz
  379. str x5, [x6]
  380. dmb sy
  381. dc ivac, x6 // Invalidate potentially stale cache line
  382. create_table_entry x0, x3, EXTRA_SHIFT, EXTRA_PTRS, x5, x6
  383. 1:
  384. #endif
  385. create_pgd_entry x0, x3, x5, x6
  386. mov x5, x3 // __pa(KERNEL_START)
  387. adr_l x6, KERNEL_END // __pa(KERNEL_END)
  388. create_block_map x0, x7, x3, x5, x6
  389. /*
  390. * Map the kernel image (starting with PHYS_OFFSET).
  391. */
  392. mov x0, x26 // swapper_pg_dir
  393. mov x5, #PAGE_OFFSET
  394. create_pgd_entry x0, x5, x3, x6
  395. ldr x6, =KERNEL_END // __va(KERNEL_END)
  396. mov x3, x24 // phys offset
  397. create_block_map x0, x7, x3, x5, x6
  398. /*
  399. * Map the FDT blob (maximum 2MB; must be within 512MB of
  400. * PHYS_OFFSET).
  401. */
  402. mov x3, x21 // FDT phys address
  403. and x3, x3, #~((1 << 21) - 1) // 2MB aligned
  404. mov x6, #PAGE_OFFSET
  405. sub x5, x3, x24 // subtract PHYS_OFFSET
  406. tst x5, #~((1 << 29) - 1) // within 512MB?
  407. csel x21, xzr, x21, ne // zero the FDT pointer
  408. b.ne 1f
  409. add x5, x5, x6 // __va(FDT blob)
  410. add x6, x5, #1 << 21 // 2MB for the FDT blob
  411. sub x6, x6, #1 // inclusive range
  412. create_block_map x0, x7, x3, x5, x6
  413. 1:
  414. /*
  415. * Since the page tables have been populated with non-cacheable
  416. * accesses (MMU disabled), invalidate the idmap and swapper page
  417. * tables again to remove any speculatively loaded cache lines.
  418. */
  419. mov x0, x25
  420. add x1, x26, #SWAPPER_DIR_SIZE
  421. dmb sy
  422. bl __inval_cache_range
  423. mov lr, x27
  424. ret
  425. ENDPROC(__create_page_tables)
  426. .ltorg
  427. /*
  428. * The following fragment of code is executed with the MMU enabled.
  429. */
  430. .set initial_sp, init_thread_union + THREAD_START_SP
  431. __mmap_switched:
  432. adr_l x6, __bss_start
  433. adr_l x7, __bss_stop
  434. 1: cmp x6, x7
  435. b.hs 2f
  436. str xzr, [x6], #8 // Clear BSS
  437. b 1b
  438. 2:
  439. adr_l sp, initial_sp, x4
  440. str_l x21, __fdt_pointer, x5 // Save FDT pointer
  441. str_l x24, memstart_addr, x6 // Save PHYS_OFFSET
  442. mov x29, #0
  443. b start_kernel
  444. ENDPROC(__mmap_switched)
  445. /*
  446. * end early head section, begin head code that is also used for
  447. * hotplug and needs to have the same protections as the text region
  448. */
  449. .section ".text","ax"
  450. /*
  451. * If we're fortunate enough to boot at EL2, ensure that the world is
  452. * sane before dropping to EL1.
  453. *
  454. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x20 if
  455. * booted in EL1 or EL2 respectively.
  456. */
  457. ENTRY(el2_setup)
  458. mrs x0, CurrentEL
  459. cmp x0, #CurrentEL_EL2
  460. b.ne 1f
  461. mrs x0, sctlr_el2
  462. CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2
  463. CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2
  464. msr sctlr_el2, x0
  465. b 2f
  466. 1: mrs x0, sctlr_el1
  467. CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1
  468. CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1
  469. msr sctlr_el1, x0
  470. mov w20, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
  471. isb
  472. ret
  473. /* Hyp configuration. */
  474. 2: mov x0, #(1 << 31) // 64-bit EL1
  475. msr hcr_el2, x0
  476. /* Generic timers. */
  477. mrs x0, cnthctl_el2
  478. orr x0, x0, #3 // Enable EL1 physical timers
  479. msr cnthctl_el2, x0
  480. msr cntvoff_el2, xzr // Clear virtual offset
  481. #ifdef CONFIG_ARM_GIC_V3
  482. /* GICv3 system register access */
  483. mrs x0, id_aa64pfr0_el1
  484. ubfx x0, x0, #24, #4
  485. cmp x0, #1
  486. b.ne 3f
  487. mrs_s x0, ICC_SRE_EL2
  488. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  489. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  490. msr_s ICC_SRE_EL2, x0
  491. isb // Make sure SRE is now set
  492. msr_s ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults
  493. 3:
  494. #endif
  495. /* Populate ID registers. */
  496. mrs x0, midr_el1
  497. mrs x1, mpidr_el1
  498. msr vpidr_el2, x0
  499. msr vmpidr_el2, x1
  500. /* sctlr_el1 */
  501. mov x0, #0x0800 // Set/clear RES{1,0} bits
  502. CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems
  503. CPU_LE( movk x0, #0x30d0, lsl #16 ) // Clear EE and E0E on LE systems
  504. msr sctlr_el1, x0
  505. /* Coprocessor traps. */
  506. mov x0, #0x33ff
  507. msr cptr_el2, x0 // Disable copro. traps to EL2
  508. #ifdef CONFIG_COMPAT
  509. msr hstr_el2, xzr // Disable CP15 traps to EL2
  510. #endif
  511. /* Stage-2 translation */
  512. msr vttbr_el2, xzr
  513. /* Hypervisor stub */
  514. adrp x0, __hyp_stub_vectors
  515. add x0, x0, #:lo12:__hyp_stub_vectors
  516. msr vbar_el2, x0
  517. /* spsr */
  518. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  519. PSR_MODE_EL1h)
  520. msr spsr_el2, x0
  521. msr elr_el2, lr
  522. mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  523. eret
  524. ENDPROC(el2_setup)
  525. /*
  526. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  527. * in x20. See arch/arm64/include/asm/virt.h for more info.
  528. */
  529. ENTRY(set_cpu_boot_mode_flag)
  530. adr_l x1, __boot_cpu_mode
  531. cmp w20, #BOOT_CPU_MODE_EL2
  532. b.ne 1f
  533. add x1, x1, #4
  534. 1: str w20, [x1] // This CPU has booted in EL1
  535. dmb sy
  536. dc ivac, x1 // Invalidate potentially stale cache line
  537. ret
  538. ENDPROC(set_cpu_boot_mode_flag)
  539. /*
  540. * We need to find out the CPU boot mode long after boot, so we need to
  541. * store it in a writable variable.
  542. *
  543. * This is not in .bss, because we set it sufficiently early that the boot-time
  544. * zeroing of .bss would clobber it.
  545. */
  546. .pushsection .data..cacheline_aligned
  547. .align L1_CACHE_SHIFT
  548. ENTRY(__boot_cpu_mode)
  549. .long BOOT_CPU_MODE_EL2
  550. .long BOOT_CPU_MODE_EL1
  551. .popsection
  552. #ifdef CONFIG_SMP
  553. /*
  554. * This provides a "holding pen" for platforms to hold all secondary
  555. * cores are held until we're ready for them to initialise.
  556. */
  557. ENTRY(secondary_holding_pen)
  558. bl el2_setup // Drop to EL1, w20=cpu_boot_mode
  559. bl set_cpu_boot_mode_flag
  560. mrs x0, mpidr_el1
  561. ldr x1, =MPIDR_HWID_BITMASK
  562. and x0, x0, x1
  563. adr_l x3, secondary_holding_pen_release
  564. pen: ldr x4, [x3]
  565. cmp x4, x0
  566. b.eq secondary_startup
  567. wfe
  568. b pen
  569. ENDPROC(secondary_holding_pen)
  570. /*
  571. * Secondary entry point that jumps straight into the kernel. Only to
  572. * be used where CPUs are brought online dynamically by the kernel.
  573. */
  574. ENTRY(secondary_entry)
  575. bl el2_setup // Drop to EL1
  576. bl set_cpu_boot_mode_flag
  577. b secondary_startup
  578. ENDPROC(secondary_entry)
  579. ENTRY(secondary_startup)
  580. /*
  581. * Common entry point for secondary CPUs.
  582. */
  583. adrp x25, idmap_pg_dir
  584. adrp x26, swapper_pg_dir
  585. bl __cpu_setup // initialise processor
  586. ldr x21, =secondary_data
  587. ldr x27, =__secondary_switched // address to jump to after enabling the MMU
  588. b __enable_mmu
  589. ENDPROC(secondary_startup)
  590. ENTRY(__secondary_switched)
  591. ldr x0, [x21] // get secondary_data.stack
  592. mov sp, x0
  593. mov x29, #0
  594. b secondary_start_kernel
  595. ENDPROC(__secondary_switched)
  596. #endif /* CONFIG_SMP */
  597. /*
  598. * Enable the MMU.
  599. *
  600. * x0 = SCTLR_EL1 value for turning on the MMU.
  601. * x27 = *virtual* address to jump to upon completion
  602. *
  603. * other registers depend on the function called upon completion
  604. */
  605. __enable_mmu:
  606. ldr x5, =vectors
  607. msr vbar_el1, x5
  608. msr ttbr0_el1, x25 // load TTBR0
  609. msr ttbr1_el1, x26 // load TTBR1
  610. isb
  611. msr sctlr_el1, x0
  612. isb
  613. br x27
  614. ENDPROC(__enable_mmu)