head.S 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 KERNEL_RAM_VADDR (PAGE_OFFSET + 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. .macro pgtbl, ttb0, ttb1, virt_to_phys
  45. ldr \ttb1, =swapper_pg_dir
  46. ldr \ttb0, =idmap_pg_dir
  47. add \ttb1, \ttb1, \virt_to_phys
  48. add \ttb0, \ttb0, \virt_to_phys
  49. .endm
  50. #ifdef CONFIG_ARM64_64K_PAGES
  51. #define BLOCK_SHIFT PAGE_SHIFT
  52. #define BLOCK_SIZE PAGE_SIZE
  53. #define TABLE_SHIFT PMD_SHIFT
  54. #else
  55. #define BLOCK_SHIFT SECTION_SHIFT
  56. #define BLOCK_SIZE SECTION_SIZE
  57. #define TABLE_SHIFT PUD_SHIFT
  58. #endif
  59. #define KERNEL_START KERNEL_RAM_VADDR
  60. #define KERNEL_END _end
  61. /*
  62. * Initial memory map attributes.
  63. */
  64. #ifndef CONFIG_SMP
  65. #define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF
  66. #define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF
  67. #else
  68. #define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF | PTE_SHARED
  69. #define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S
  70. #endif
  71. #ifdef CONFIG_ARM64_64K_PAGES
  72. #define MM_MMUFLAGS PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
  73. #else
  74. #define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
  75. #endif
  76. /*
  77. * Kernel startup entry point.
  78. * ---------------------------
  79. *
  80. * The requirements are:
  81. * MMU = off, D-cache = off, I-cache = on or off,
  82. * x0 = physical address to the FDT blob.
  83. *
  84. * This code is mostly position independent so you call this at
  85. * __pa(PAGE_OFFSET + TEXT_OFFSET).
  86. *
  87. * Note that the callee-saved registers are used for storing variables
  88. * that are useful before the MMU is enabled. The allocations are described
  89. * in the entry routines.
  90. */
  91. __HEAD
  92. /*
  93. * DO NOT MODIFY. Image header expected by Linux boot-loaders.
  94. */
  95. #ifdef CONFIG_EFI
  96. efi_head:
  97. /*
  98. * This add instruction has no meaningful effect except that
  99. * its opcode forms the magic "MZ" signature required by UEFI.
  100. */
  101. add x13, x18, #0x16
  102. b stext
  103. #else
  104. b stext // branch to kernel start, magic
  105. .long 0 // reserved
  106. #endif
  107. .quad _kernel_offset_le // Image load offset from start of RAM, little-endian
  108. .quad _kernel_size_le // Effective size of kernel image, little-endian
  109. .quad _kernel_flags_le // Informative flags, little-endian
  110. .quad 0 // reserved
  111. .quad 0 // reserved
  112. .quad 0 // reserved
  113. .byte 0x41 // Magic number, "ARM\x64"
  114. .byte 0x52
  115. .byte 0x4d
  116. .byte 0x64
  117. #ifdef CONFIG_EFI
  118. .long pe_header - efi_head // Offset to the PE header.
  119. #else
  120. .word 0 // reserved
  121. #endif
  122. #ifdef CONFIG_EFI
  123. .globl stext_offset
  124. .set stext_offset, stext - efi_head
  125. .align 3
  126. pe_header:
  127. .ascii "PE"
  128. .short 0
  129. coff_header:
  130. .short 0xaa64 // AArch64
  131. .short 2 // nr_sections
  132. .long 0 // TimeDateStamp
  133. .long 0 // PointerToSymbolTable
  134. .long 1 // NumberOfSymbols
  135. .short section_table - optional_header // SizeOfOptionalHeader
  136. .short 0x206 // Characteristics.
  137. // IMAGE_FILE_DEBUG_STRIPPED |
  138. // IMAGE_FILE_EXECUTABLE_IMAGE |
  139. // IMAGE_FILE_LINE_NUMS_STRIPPED
  140. optional_header:
  141. .short 0x20b // PE32+ format
  142. .byte 0x02 // MajorLinkerVersion
  143. .byte 0x14 // MinorLinkerVersion
  144. .long _end - stext // SizeOfCode
  145. .long 0 // SizeOfInitializedData
  146. .long 0 // SizeOfUninitializedData
  147. .long efi_stub_entry - efi_head // AddressOfEntryPoint
  148. .long stext_offset // BaseOfCode
  149. extra_header_fields:
  150. .quad 0 // ImageBase
  151. .long 0x1000 // SectionAlignment
  152. .long PECOFF_FILE_ALIGNMENT // FileAlignment
  153. .short 0 // MajorOperatingSystemVersion
  154. .short 0 // MinorOperatingSystemVersion
  155. .short 0 // MajorImageVersion
  156. .short 0 // MinorImageVersion
  157. .short 0 // MajorSubsystemVersion
  158. .short 0 // MinorSubsystemVersion
  159. .long 0 // Win32VersionValue
  160. .long _end - efi_head // SizeOfImage
  161. // Everything before the kernel image is considered part of the header
  162. .long stext_offset // SizeOfHeaders
  163. .long 0 // CheckSum
  164. .short 0xa // Subsystem (EFI application)
  165. .short 0 // DllCharacteristics
  166. .quad 0 // SizeOfStackReserve
  167. .quad 0 // SizeOfStackCommit
  168. .quad 0 // SizeOfHeapReserve
  169. .quad 0 // SizeOfHeapCommit
  170. .long 0 // LoaderFlags
  171. .long 0x6 // NumberOfRvaAndSizes
  172. .quad 0 // ExportTable
  173. .quad 0 // ImportTable
  174. .quad 0 // ResourceTable
  175. .quad 0 // ExceptionTable
  176. .quad 0 // CertificationTable
  177. .quad 0 // BaseRelocationTable
  178. // Section table
  179. section_table:
  180. /*
  181. * The EFI application loader requires a relocation section
  182. * because EFI applications must be relocatable. This is a
  183. * dummy section as far as we are concerned.
  184. */
  185. .ascii ".reloc"
  186. .byte 0
  187. .byte 0 // end of 0 padding of section name
  188. .long 0
  189. .long 0
  190. .long 0 // SizeOfRawData
  191. .long 0 // PointerToRawData
  192. .long 0 // PointerToRelocations
  193. .long 0 // PointerToLineNumbers
  194. .short 0 // NumberOfRelocations
  195. .short 0 // NumberOfLineNumbers
  196. .long 0x42100040 // Characteristics (section flags)
  197. .ascii ".text"
  198. .byte 0
  199. .byte 0
  200. .byte 0 // end of 0 padding of section name
  201. .long _end - stext // VirtualSize
  202. .long stext_offset // VirtualAddress
  203. .long _edata - stext // SizeOfRawData
  204. .long stext_offset // PointerToRawData
  205. .long 0 // PointerToRelocations (0 for executables)
  206. .long 0 // PointerToLineNumbers (0 for executables)
  207. .short 0 // NumberOfRelocations (0 for executables)
  208. .short 0 // NumberOfLineNumbers (0 for executables)
  209. .long 0xe0500020 // Characteristics (section flags)
  210. /*
  211. * EFI will load stext onwards at the 4k section alignment
  212. * described in the PE/COFF header. To ensure that instruction
  213. * sequences using an adrp and a :lo12: immediate will function
  214. * correctly at this alignment, we must ensure that stext is
  215. * placed at a 4k boundary in the Image to begin with.
  216. */
  217. .align 12
  218. #endif
  219. ENTRY(stext)
  220. mov x21, x0 // x21=FDT
  221. bl el2_setup // Drop to EL1, w20=cpu_boot_mode
  222. bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
  223. bl set_cpu_boot_mode_flag
  224. mrs x22, midr_el1 // x22=cpuid
  225. mov x0, x22
  226. bl lookup_processor_type
  227. mov x23, x0 // x23=current cpu_table
  228. /*
  229. * __error_p may end up out of range for cbz if text areas are
  230. * aligned up to section sizes.
  231. */
  232. cbnz x23, 1f // invalid processor (x23=0)?
  233. b __error_p
  234. 1:
  235. bl __vet_fdt
  236. bl __create_page_tables // x25=TTBR0, x26=TTBR1
  237. /*
  238. * The following calls CPU specific code in a position independent
  239. * manner. See arch/arm64/mm/proc.S for details. x23 = base of
  240. * cpu_info structure selected by lookup_processor_type above.
  241. * On return, the CPU will be ready for the MMU to be turned on and
  242. * the TCR will have been set.
  243. */
  244. ldr x27, __switch_data // address to jump to after
  245. // MMU has been enabled
  246. adrp lr, __enable_mmu // return (PIC) address
  247. add lr, lr, #:lo12:__enable_mmu
  248. ldr x12, [x23, #CPU_INFO_SETUP]
  249. add x12, x12, x28 // __virt_to_phys
  250. br x12 // initialise processor
  251. ENDPROC(stext)
  252. /*
  253. * Determine validity of the x21 FDT pointer.
  254. * The dtb must be 8-byte aligned and live in the first 512M of memory.
  255. */
  256. __vet_fdt:
  257. tst x21, #0x7
  258. b.ne 1f
  259. cmp x21, x24
  260. b.lt 1f
  261. mov x0, #(1 << 29)
  262. add x0, x0, x24
  263. cmp x21, x0
  264. b.ge 1f
  265. ret
  266. 1:
  267. mov x21, #0
  268. ret
  269. ENDPROC(__vet_fdt)
  270. /*
  271. * Macro to create a table entry to the next page.
  272. *
  273. * tbl: page table address
  274. * virt: virtual address
  275. * shift: #imm page table shift
  276. * ptrs: #imm pointers per table page
  277. *
  278. * Preserves: virt
  279. * Corrupts: tmp1, tmp2
  280. * Returns: tbl -> next level table page address
  281. */
  282. .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
  283. lsr \tmp1, \virt, #\shift
  284. and \tmp1, \tmp1, #\ptrs - 1 // table index
  285. add \tmp2, \tbl, #PAGE_SIZE
  286. orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
  287. str \tmp2, [\tbl, \tmp1, lsl #3]
  288. add \tbl, \tbl, #PAGE_SIZE // next level table page
  289. .endm
  290. /*
  291. * Macro to populate the PGD (and possibily PUD) for the corresponding
  292. * block entry in the next level (tbl) for the given virtual address.
  293. *
  294. * Preserves: tbl, next, virt
  295. * Corrupts: tmp1, tmp2
  296. */
  297. .macro create_pgd_entry, tbl, virt, tmp1, tmp2
  298. create_table_entry \tbl, \virt, PGDIR_SHIFT, PTRS_PER_PGD, \tmp1, \tmp2
  299. #if SWAPPER_PGTABLE_LEVELS == 3
  300. create_table_entry \tbl, \virt, TABLE_SHIFT, PTRS_PER_PTE, \tmp1, \tmp2
  301. #endif
  302. .endm
  303. /*
  304. * Macro to populate block entries in the page table for the start..end
  305. * virtual range (inclusive).
  306. *
  307. * Preserves: tbl, flags
  308. * Corrupts: phys, start, end, pstate
  309. */
  310. .macro create_block_map, tbl, flags, phys, start, end
  311. lsr \phys, \phys, #BLOCK_SHIFT
  312. lsr \start, \start, #BLOCK_SHIFT
  313. and \start, \start, #PTRS_PER_PTE - 1 // table index
  314. orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry
  315. lsr \end, \end, #BLOCK_SHIFT
  316. and \end, \end, #PTRS_PER_PTE - 1 // table end index
  317. 9999: str \phys, [\tbl, \start, lsl #3] // store the entry
  318. add \start, \start, #1 // next entry
  319. add \phys, \phys, #BLOCK_SIZE // next block
  320. cmp \start, \end
  321. b.ls 9999b
  322. .endm
  323. /*
  324. * Setup the initial page tables. We only setup the barest amount which is
  325. * required to get the kernel running. The following sections are required:
  326. * - identity mapping to enable the MMU (low address, TTBR0)
  327. * - first few MB of the kernel linear mapping to jump to once the MMU has
  328. * been enabled, including the FDT blob (TTBR1)
  329. * - pgd entry for fixed mappings (TTBR1)
  330. */
  331. __create_page_tables:
  332. pgtbl x25, x26, x28 // idmap_pg_dir and swapper_pg_dir addresses
  333. mov x27, lr
  334. /*
  335. * Invalidate the idmap and swapper page tables to avoid potential
  336. * dirty cache lines being evicted.
  337. */
  338. mov x0, x25
  339. add x1, x26, #SWAPPER_DIR_SIZE
  340. bl __inval_cache_range
  341. /*
  342. * Clear the idmap and swapper page tables.
  343. */
  344. mov x0, x25
  345. add x6, x26, #SWAPPER_DIR_SIZE
  346. 1: stp xzr, xzr, [x0], #16
  347. stp xzr, xzr, [x0], #16
  348. stp xzr, xzr, [x0], #16
  349. stp xzr, xzr, [x0], #16
  350. cmp x0, x6
  351. b.lo 1b
  352. ldr x7, =MM_MMUFLAGS
  353. /*
  354. * Create the identity mapping.
  355. */
  356. mov x0, x25 // idmap_pg_dir
  357. ldr x3, =KERNEL_START
  358. add x3, x3, x28 // __pa(KERNEL_START)
  359. create_pgd_entry x0, x3, x5, x6
  360. ldr x6, =KERNEL_END
  361. mov x5, x3 // __pa(KERNEL_START)
  362. add x6, x6, x28 // __pa(KERNEL_END)
  363. create_block_map x0, x7, x3, x5, x6
  364. /*
  365. * Map the kernel image (starting with PHYS_OFFSET).
  366. */
  367. mov x0, x26 // swapper_pg_dir
  368. mov x5, #PAGE_OFFSET
  369. create_pgd_entry x0, x5, x3, x6
  370. ldr x6, =KERNEL_END
  371. mov x3, x24 // phys offset
  372. create_block_map x0, x7, x3, x5, x6
  373. /*
  374. * Map the FDT blob (maximum 2MB; must be within 512MB of
  375. * PHYS_OFFSET).
  376. */
  377. mov x3, x21 // FDT phys address
  378. and x3, x3, #~((1 << 21) - 1) // 2MB aligned
  379. mov x6, #PAGE_OFFSET
  380. sub x5, x3, x24 // subtract PHYS_OFFSET
  381. tst x5, #~((1 << 29) - 1) // within 512MB?
  382. csel x21, xzr, x21, ne // zero the FDT pointer
  383. b.ne 1f
  384. add x5, x5, x6 // __va(FDT blob)
  385. add x6, x5, #1 << 21 // 2MB for the FDT blob
  386. sub x6, x6, #1 // inclusive range
  387. create_block_map x0, x7, x3, x5, x6
  388. 1:
  389. /*
  390. * Since the page tables have been populated with non-cacheable
  391. * accesses (MMU disabled), invalidate the idmap and swapper page
  392. * tables again to remove any speculatively loaded cache lines.
  393. */
  394. mov x0, x25
  395. add x1, x26, #SWAPPER_DIR_SIZE
  396. bl __inval_cache_range
  397. mov lr, x27
  398. ret
  399. ENDPROC(__create_page_tables)
  400. .ltorg
  401. .align 3
  402. .type __switch_data, %object
  403. __switch_data:
  404. .quad __mmap_switched
  405. .quad __bss_start // x6
  406. .quad __bss_stop // x7
  407. .quad processor_id // x4
  408. .quad __fdt_pointer // x5
  409. .quad memstart_addr // x6
  410. .quad init_thread_union + THREAD_START_SP // sp
  411. /*
  412. * The following fragment of code is executed with the MMU on in MMU mode, and
  413. * uses absolute addresses; this is not position independent.
  414. */
  415. __mmap_switched:
  416. adr x3, __switch_data + 8
  417. ldp x6, x7, [x3], #16
  418. 1: cmp x6, x7
  419. b.hs 2f
  420. str xzr, [x6], #8 // Clear BSS
  421. b 1b
  422. 2:
  423. ldp x4, x5, [x3], #16
  424. ldr x6, [x3], #8
  425. ldr x16, [x3]
  426. mov sp, x16
  427. str x22, [x4] // Save processor ID
  428. str x21, [x5] // Save FDT pointer
  429. str x24, [x6] // Save PHYS_OFFSET
  430. mov x29, #0
  431. b start_kernel
  432. ENDPROC(__mmap_switched)
  433. /*
  434. * end early head section, begin head code that is also used for
  435. * hotplug and needs to have the same protections as the text region
  436. */
  437. .section ".text","ax"
  438. /*
  439. * If we're fortunate enough to boot at EL2, ensure that the world is
  440. * sane before dropping to EL1.
  441. *
  442. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x20 if
  443. * booted in EL1 or EL2 respectively.
  444. */
  445. ENTRY(el2_setup)
  446. mrs x0, CurrentEL
  447. cmp x0, #CurrentEL_EL2
  448. b.ne 1f
  449. mrs x0, sctlr_el2
  450. CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2
  451. CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2
  452. msr sctlr_el2, x0
  453. b 2f
  454. 1: mrs x0, sctlr_el1
  455. CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1
  456. CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1
  457. msr sctlr_el1, x0
  458. mov w20, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
  459. isb
  460. ret
  461. /* Hyp configuration. */
  462. 2: mov x0, #(1 << 31) // 64-bit EL1
  463. msr hcr_el2, x0
  464. /* Generic timers. */
  465. mrs x0, cnthctl_el2
  466. orr x0, x0, #3 // Enable EL1 physical timers
  467. msr cnthctl_el2, x0
  468. msr cntvoff_el2, xzr // Clear virtual offset
  469. #ifdef CONFIG_ARM_GIC_V3
  470. /* GICv3 system register access */
  471. mrs x0, id_aa64pfr0_el1
  472. ubfx x0, x0, #24, #4
  473. cmp x0, #1
  474. b.ne 3f
  475. mrs_s x0, ICC_SRE_EL2
  476. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  477. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  478. msr_s ICC_SRE_EL2, x0
  479. isb // Make sure SRE is now set
  480. msr_s ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults
  481. 3:
  482. #endif
  483. /* Populate ID registers. */
  484. mrs x0, midr_el1
  485. mrs x1, mpidr_el1
  486. msr vpidr_el2, x0
  487. msr vmpidr_el2, x1
  488. /* sctlr_el1 */
  489. mov x0, #0x0800 // Set/clear RES{1,0} bits
  490. CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems
  491. CPU_LE( movk x0, #0x30d0, lsl #16 ) // Clear EE and E0E on LE systems
  492. msr sctlr_el1, x0
  493. /* Coprocessor traps. */
  494. mov x0, #0x33ff
  495. msr cptr_el2, x0 // Disable copro. traps to EL2
  496. #ifdef CONFIG_COMPAT
  497. msr hstr_el2, xzr // Disable CP15 traps to EL2
  498. #endif
  499. /* Stage-2 translation */
  500. msr vttbr_el2, xzr
  501. /* Hypervisor stub */
  502. adrp x0, __hyp_stub_vectors
  503. add x0, x0, #:lo12:__hyp_stub_vectors
  504. msr vbar_el2, x0
  505. /* spsr */
  506. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  507. PSR_MODE_EL1h)
  508. msr spsr_el2, x0
  509. msr elr_el2, lr
  510. mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  511. eret
  512. ENDPROC(el2_setup)
  513. /*
  514. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  515. * in x20. See arch/arm64/include/asm/virt.h for more info.
  516. */
  517. ENTRY(set_cpu_boot_mode_flag)
  518. ldr x1, =__boot_cpu_mode // Compute __boot_cpu_mode
  519. add x1, x1, x28
  520. cmp w20, #BOOT_CPU_MODE_EL2
  521. b.ne 1f
  522. add x1, x1, #4
  523. 1: str w20, [x1] // This CPU has booted in EL1
  524. dmb sy
  525. dc ivac, x1 // Invalidate potentially stale cache line
  526. ret
  527. ENDPROC(set_cpu_boot_mode_flag)
  528. /*
  529. * We need to find out the CPU boot mode long after boot, so we need to
  530. * store it in a writable variable.
  531. *
  532. * This is not in .bss, because we set it sufficiently early that the boot-time
  533. * zeroing of .bss would clobber it.
  534. */
  535. .pushsection .data..cacheline_aligned
  536. ENTRY(__boot_cpu_mode)
  537. .align L1_CACHE_SHIFT
  538. .long BOOT_CPU_MODE_EL2
  539. .long 0
  540. .popsection
  541. #ifdef CONFIG_SMP
  542. .align 3
  543. 1: .quad .
  544. .quad secondary_holding_pen_release
  545. /*
  546. * This provides a "holding pen" for platforms to hold all secondary
  547. * cores are held until we're ready for them to initialise.
  548. */
  549. ENTRY(secondary_holding_pen)
  550. bl el2_setup // Drop to EL1, w20=cpu_boot_mode
  551. bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
  552. bl set_cpu_boot_mode_flag
  553. mrs x0, mpidr_el1
  554. ldr x1, =MPIDR_HWID_BITMASK
  555. and x0, x0, x1
  556. adr x1, 1b
  557. ldp x2, x3, [x1]
  558. sub x1, x1, x2
  559. add x3, x3, x1
  560. pen: ldr x4, [x3]
  561. cmp x4, x0
  562. b.eq secondary_startup
  563. wfe
  564. b pen
  565. ENDPROC(secondary_holding_pen)
  566. /*
  567. * Secondary entry point that jumps straight into the kernel. Only to
  568. * be used where CPUs are brought online dynamically by the kernel.
  569. */
  570. ENTRY(secondary_entry)
  571. bl el2_setup // Drop to EL1
  572. bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
  573. bl set_cpu_boot_mode_flag
  574. b secondary_startup
  575. ENDPROC(secondary_entry)
  576. ENTRY(secondary_startup)
  577. /*
  578. * Common entry point for secondary CPUs.
  579. */
  580. mrs x22, midr_el1 // x22=cpuid
  581. mov x0, x22
  582. bl lookup_processor_type
  583. mov x23, x0 // x23=current cpu_table
  584. cbz x23, __error_p // invalid processor (x23=0)?
  585. pgtbl x25, x26, x28 // x25=TTBR0, x26=TTBR1
  586. ldr x12, [x23, #CPU_INFO_SETUP]
  587. add x12, x12, x28 // __virt_to_phys
  588. blr x12 // initialise processor
  589. ldr x21, =secondary_data
  590. ldr x27, =__secondary_switched // address to jump to after enabling the MMU
  591. b __enable_mmu
  592. ENDPROC(secondary_startup)
  593. ENTRY(__secondary_switched)
  594. ldr x0, [x21] // get secondary_data.stack
  595. mov sp, x0
  596. mov x29, #0
  597. b secondary_start_kernel
  598. ENDPROC(__secondary_switched)
  599. #endif /* CONFIG_SMP */
  600. /*
  601. * Setup common bits before finally enabling the MMU. Essentially this is just
  602. * loading the page table pointer and vector base registers.
  603. *
  604. * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
  605. * the MMU.
  606. */
  607. __enable_mmu:
  608. ldr x5, =vectors
  609. msr vbar_el1, x5
  610. msr ttbr0_el1, x25 // load TTBR0
  611. msr ttbr1_el1, x26 // load TTBR1
  612. isb
  613. b __turn_mmu_on
  614. ENDPROC(__enable_mmu)
  615. /*
  616. * Enable the MMU. This completely changes the structure of the visible memory
  617. * space. You will not be able to trace execution through this.
  618. *
  619. * x0 = system control register
  620. * x27 = *virtual* address to jump to upon completion
  621. *
  622. * other registers depend on the function called upon completion
  623. *
  624. * We align the entire function to the smallest power of two larger than it to
  625. * ensure it fits within a single block map entry. Otherwise were PHYS_OFFSET
  626. * close to the end of a 512MB or 1GB block we might require an additional
  627. * table to map the entire function.
  628. */
  629. .align 4
  630. __turn_mmu_on:
  631. msr sctlr_el1, x0
  632. isb
  633. br x27
  634. ENDPROC(__turn_mmu_on)
  635. /*
  636. * Calculate the start of physical memory.
  637. */
  638. __calc_phys_offset:
  639. adr x0, 1f
  640. ldp x1, x2, [x0]
  641. sub x28, x0, x1 // x28 = PHYS_OFFSET - PAGE_OFFSET
  642. add x24, x2, x28 // x24 = PHYS_OFFSET
  643. ret
  644. ENDPROC(__calc_phys_offset)
  645. .align 3
  646. 1: .quad .
  647. .quad PAGE_OFFSET
  648. /*
  649. * Exception handling. Something went wrong and we can't proceed. We ought to
  650. * tell the user, but since we don't have any guarantee that we're even
  651. * running on the right architecture, we do virtually nothing.
  652. */
  653. __error_p:
  654. ENDPROC(__error_p)
  655. __error:
  656. 1: nop
  657. b 1b
  658. ENDPROC(__error)
  659. /*
  660. * This function gets the processor ID in w0 and searches the cpu_table[] for
  661. * a match. It returns a pointer to the struct cpu_info it found. The
  662. * cpu_table[] must end with an empty (all zeros) structure.
  663. *
  664. * This routine can be called via C code and it needs to work with the MMU
  665. * both disabled and enabled (the offset is calculated automatically).
  666. */
  667. ENTRY(lookup_processor_type)
  668. adr x1, __lookup_processor_type_data
  669. ldp x2, x3, [x1]
  670. sub x1, x1, x2 // get offset between VA and PA
  671. add x3, x3, x1 // convert VA to PA
  672. 1:
  673. ldp w5, w6, [x3] // load cpu_id_val and cpu_id_mask
  674. cbz w5, 2f // end of list?
  675. and w6, w6, w0
  676. cmp w5, w6
  677. b.eq 3f
  678. add x3, x3, #CPU_INFO_SZ
  679. b 1b
  680. 2:
  681. mov x3, #0 // unknown processor
  682. 3:
  683. mov x0, x3
  684. ret
  685. ENDPROC(lookup_processor_type)
  686. .align 3
  687. .type __lookup_processor_type_data, %object
  688. __lookup_processor_type_data:
  689. .quad .
  690. .quad cpu_table
  691. .size __lookup_processor_type_data, . - __lookup_processor_type_data