head.S 19 KB

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