head.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <asm/assembler.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/memory.h>
  28. #include <asm/thread_info.h>
  29. #include <asm/pgtable-hwdef.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/page.h>
  32. #include <asm/virt.h>
  33. /*
  34. * swapper_pg_dir is the virtual address of the initial page table. We place
  35. * the page tables 3 * PAGE_SIZE below KERNEL_RAM_VADDR. The idmap_pg_dir has
  36. * 2 pages and is placed below swapper_pg_dir.
  37. */
  38. #define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET)
  39. #if (KERNEL_RAM_VADDR & 0xfffff) != 0x80000
  40. #error KERNEL_RAM_VADDR must start at 0xXXX80000
  41. #endif
  42. #define SWAPPER_DIR_SIZE (3 * PAGE_SIZE)
  43. #define IDMAP_DIR_SIZE (2 * PAGE_SIZE)
  44. .globl swapper_pg_dir
  45. .equ swapper_pg_dir, KERNEL_RAM_VADDR - SWAPPER_DIR_SIZE
  46. .globl idmap_pg_dir
  47. .equ idmap_pg_dir, swapper_pg_dir - IDMAP_DIR_SIZE
  48. .macro pgtbl, ttb0, ttb1, phys
  49. add \ttb1, \phys, #TEXT_OFFSET - SWAPPER_DIR_SIZE
  50. sub \ttb0, \ttb1, #IDMAP_DIR_SIZE
  51. .endm
  52. #ifdef CONFIG_ARM64_64K_PAGES
  53. #define BLOCK_SHIFT PAGE_SHIFT
  54. #define BLOCK_SIZE PAGE_SIZE
  55. #else
  56. #define BLOCK_SHIFT SECTION_SHIFT
  57. #define BLOCK_SIZE SECTION_SIZE
  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. b stext // branch to kernel start, magic
  96. .long 0 // reserved
  97. .quad TEXT_OFFSET // Image load offset from start of RAM
  98. .quad 0 // reserved
  99. .quad 0 // reserved
  100. ENTRY(stext)
  101. mov x21, x0 // x21=FDT
  102. bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
  103. bl el2_setup // Drop to EL1
  104. mrs x22, midr_el1 // x22=cpuid
  105. mov x0, x22
  106. bl lookup_processor_type
  107. mov x23, x0 // x23=current cpu_table
  108. cbz x23, __error_p // invalid processor (x23=0)?
  109. bl __vet_fdt
  110. bl __create_page_tables // x25=TTBR0, x26=TTBR1
  111. /*
  112. * The following calls CPU specific code in a position independent
  113. * manner. See arch/arm64/mm/proc.S for details. x23 = base of
  114. * cpu_info structure selected by lookup_processor_type above.
  115. * On return, the CPU will be ready for the MMU to be turned on and
  116. * the TCR will have been set.
  117. */
  118. ldr x27, __switch_data // address to jump to after
  119. // MMU has been enabled
  120. adr lr, __enable_mmu // return (PIC) address
  121. ldr x12, [x23, #CPU_INFO_SETUP]
  122. add x12, x12, x28 // __virt_to_phys
  123. br x12 // initialise processor
  124. ENDPROC(stext)
  125. /*
  126. * If we're fortunate enough to boot at EL2, ensure that the world is
  127. * sane before dropping to EL1.
  128. */
  129. ENTRY(el2_setup)
  130. mrs x0, CurrentEL
  131. cmp x0, #PSR_MODE_EL2t
  132. ccmp x0, #PSR_MODE_EL2h, #0x4, ne
  133. ldr x0, =__boot_cpu_mode // Compute __boot_cpu_mode
  134. add x0, x0, x28
  135. b.eq 1f
  136. str wzr, [x0] // Remember we don't have EL2...
  137. ret
  138. /* Hyp configuration. */
  139. 1: ldr w1, =BOOT_CPU_MODE_EL2
  140. str w1, [x0, #4] // This CPU has EL2
  141. mov x0, #(1 << 31) // 64-bit EL1
  142. msr hcr_el2, x0
  143. /* Generic timers. */
  144. mrs x0, cnthctl_el2
  145. orr x0, x0, #3 // Enable EL1 physical timers
  146. msr cnthctl_el2, x0
  147. msr cntvoff_el2, xzr // Clear virtual offset
  148. /* Populate ID registers. */
  149. mrs x0, midr_el1
  150. mrs x1, mpidr_el1
  151. msr vpidr_el2, x0
  152. msr vmpidr_el2, x1
  153. /* sctlr_el1 */
  154. mov x0, #0x0800 // Set/clear RES{1,0} bits
  155. movk x0, #0x30d0, lsl #16
  156. msr sctlr_el1, x0
  157. /* Coprocessor traps. */
  158. mov x0, #0x33ff
  159. msr cptr_el2, x0 // Disable copro. traps to EL2
  160. #ifdef CONFIG_COMPAT
  161. msr hstr_el2, xzr // Disable CP15 traps to EL2
  162. #endif
  163. /* Stage-2 translation */
  164. msr vttbr_el2, xzr
  165. /* Hypervisor stub */
  166. adr x0, __hyp_stub_vectors
  167. msr vbar_el2, x0
  168. /* spsr */
  169. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  170. PSR_MODE_EL1h)
  171. msr spsr_el2, x0
  172. msr elr_el2, lr
  173. eret
  174. ENDPROC(el2_setup)
  175. /*
  176. * We need to find out the CPU boot mode long after boot, so we need to
  177. * store it in a writable variable.
  178. *
  179. * This is not in .bss, because we set it sufficiently early that the boot-time
  180. * zeroing of .bss would clobber it.
  181. */
  182. .pushsection .data
  183. ENTRY(__boot_cpu_mode)
  184. .long BOOT_CPU_MODE_EL2
  185. .long 0
  186. .popsection
  187. .align 3
  188. 2: .quad .
  189. .quad PAGE_OFFSET
  190. #ifdef CONFIG_SMP
  191. .pushsection .smp.pen.text, "ax"
  192. .align 3
  193. 1: .quad .
  194. .quad secondary_holding_pen_release
  195. /*
  196. * This provides a "holding pen" for platforms to hold all secondary
  197. * cores are held until we're ready for them to initialise.
  198. */
  199. ENTRY(secondary_holding_pen)
  200. bl __calc_phys_offset // x24=phys offset
  201. bl el2_setup // Drop to EL1
  202. mrs x0, mpidr_el1
  203. and x0, x0, #15 // CPU number
  204. adr x1, 1b
  205. ldp x2, x3, [x1]
  206. sub x1, x1, x2
  207. add x3, x3, x1
  208. pen: ldr x4, [x3]
  209. cmp x4, x0
  210. b.eq secondary_startup
  211. wfe
  212. b pen
  213. ENDPROC(secondary_holding_pen)
  214. .popsection
  215. ENTRY(secondary_startup)
  216. /*
  217. * Common entry point for secondary CPUs.
  218. */
  219. mrs x22, midr_el1 // x22=cpuid
  220. mov x0, x22
  221. bl lookup_processor_type
  222. mov x23, x0 // x23=current cpu_table
  223. cbz x23, __error_p // invalid processor (x23=0)?
  224. pgtbl x25, x26, x24 // x25=TTBR0, x26=TTBR1
  225. ldr x12, [x23, #CPU_INFO_SETUP]
  226. add x12, x12, x28 // __virt_to_phys
  227. blr x12 // initialise processor
  228. ldr x21, =secondary_data
  229. ldr x27, =__secondary_switched // address to jump to after enabling the MMU
  230. b __enable_mmu
  231. ENDPROC(secondary_startup)
  232. ENTRY(__secondary_switched)
  233. ldr x0, [x21] // get secondary_data.stack
  234. mov sp, x0
  235. mov x29, #0
  236. b secondary_start_kernel
  237. ENDPROC(__secondary_switched)
  238. #endif /* CONFIG_SMP */
  239. /*
  240. * Setup common bits before finally enabling the MMU. Essentially this is just
  241. * loading the page table pointer and vector base registers.
  242. *
  243. * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
  244. * the MMU.
  245. */
  246. __enable_mmu:
  247. ldr x5, =vectors
  248. msr vbar_el1, x5
  249. msr ttbr0_el1, x25 // load TTBR0
  250. msr ttbr1_el1, x26 // load TTBR1
  251. isb
  252. b __turn_mmu_on
  253. ENDPROC(__enable_mmu)
  254. /*
  255. * Enable the MMU. This completely changes the structure of the visible memory
  256. * space. You will not be able to trace execution through this.
  257. *
  258. * x0 = system control register
  259. * x27 = *virtual* address to jump to upon completion
  260. *
  261. * other registers depend on the function called upon completion
  262. */
  263. .align 6
  264. __turn_mmu_on:
  265. msr sctlr_el1, x0
  266. isb
  267. br x27
  268. ENDPROC(__turn_mmu_on)
  269. /*
  270. * Calculate the start of physical memory.
  271. */
  272. __calc_phys_offset:
  273. adr x0, 1f
  274. ldp x1, x2, [x0]
  275. sub x28, x0, x1 // x28 = PHYS_OFFSET - PAGE_OFFSET
  276. add x24, x2, x28 // x24 = PHYS_OFFSET
  277. ret
  278. ENDPROC(__calc_phys_offset)
  279. .align 3
  280. 1: .quad .
  281. .quad PAGE_OFFSET
  282. /*
  283. * Macro to populate the PGD for the corresponding block entry in the next
  284. * level (tbl) for the given virtual address.
  285. *
  286. * Preserves: pgd, tbl, virt
  287. * Corrupts: tmp1, tmp2
  288. */
  289. .macro create_pgd_entry, pgd, tbl, virt, tmp1, tmp2
  290. lsr \tmp1, \virt, #PGDIR_SHIFT
  291. and \tmp1, \tmp1, #PTRS_PER_PGD - 1 // PGD index
  292. orr \tmp2, \tbl, #3 // PGD entry table type
  293. str \tmp2, [\pgd, \tmp1, lsl #3]
  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, idmap=0
  303. lsr \phys, \phys, #BLOCK_SHIFT
  304. .if \idmap
  305. and \start, \phys, #PTRS_PER_PTE - 1 // table index
  306. .else
  307. lsr \start, \start, #BLOCK_SHIFT
  308. and \start, \start, #PTRS_PER_PTE - 1 // table index
  309. .endif
  310. orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry
  311. .ifnc \start,\end
  312. lsr \end, \end, #BLOCK_SHIFT
  313. and \end, \end, #PTRS_PER_PTE - 1 // table end index
  314. .endif
  315. 9999: str \phys, [\tbl, \start, lsl #3] // store the entry
  316. .ifnc \start,\end
  317. add \start, \start, #1 // next entry
  318. add \phys, \phys, #BLOCK_SIZE // next block
  319. cmp \start, \end
  320. b.ls 9999b
  321. .endif
  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. * - UART mapping if CONFIG_EARLY_PRINTK is enabled (TTBR1)
  330. */
  331. __create_page_tables:
  332. pgtbl x25, x26, x24 // idmap_pg_dir and swapper_pg_dir addresses
  333. /*
  334. * Clear the idmap and swapper page tables.
  335. */
  336. mov x0, x25
  337. add x6, x26, #SWAPPER_DIR_SIZE
  338. 1: stp xzr, xzr, [x0], #16
  339. stp xzr, xzr, [x0], #16
  340. stp xzr, xzr, [x0], #16
  341. stp xzr, xzr, [x0], #16
  342. cmp x0, x6
  343. b.lo 1b
  344. ldr x7, =MM_MMUFLAGS
  345. /*
  346. * Create the identity mapping.
  347. */
  348. add x0, x25, #PAGE_SIZE // section table address
  349. adr x3, __turn_mmu_on // virtual/physical address
  350. create_pgd_entry x25, x0, x3, x5, x6
  351. create_block_map x0, x7, x3, x5, x5, idmap=1
  352. /*
  353. * Map the kernel image (starting with PHYS_OFFSET).
  354. */
  355. add x0, x26, #PAGE_SIZE // section table address
  356. mov x5, #PAGE_OFFSET
  357. create_pgd_entry x26, x0, x5, x3, x6
  358. ldr x6, =KERNEL_END - 1
  359. mov x3, x24 // phys offset
  360. create_block_map x0, x7, x3, x5, x6
  361. /*
  362. * Map the FDT blob (maximum 2MB; must be within 512MB of
  363. * PHYS_OFFSET).
  364. */
  365. mov x3, x21 // FDT phys address
  366. and x3, x3, #~((1 << 21) - 1) // 2MB aligned
  367. mov x6, #PAGE_OFFSET
  368. sub x5, x3, x24 // subtract PHYS_OFFSET
  369. tst x5, #~((1 << 29) - 1) // within 512MB?
  370. csel x21, xzr, x21, ne // zero the FDT pointer
  371. b.ne 1f
  372. add x5, x5, x6 // __va(FDT blob)
  373. add x6, x5, #1 << 21 // 2MB for the FDT blob
  374. sub x6, x6, #1 // inclusive range
  375. create_block_map x0, x7, x3, x5, x6
  376. 1:
  377. #ifdef CONFIG_EARLY_PRINTK
  378. /*
  379. * Create the pgd entry for the UART mapping. The full mapping is done
  380. * later based earlyprintk kernel parameter.
  381. */
  382. ldr x5, =EARLYCON_IOBASE // UART virtual address
  383. add x0, x26, #2 * PAGE_SIZE // section table address
  384. create_pgd_entry x26, x0, x5, x6, x7
  385. #endif
  386. ret
  387. ENDPROC(__create_page_tables)
  388. .ltorg
  389. .align 3
  390. .type __switch_data, %object
  391. __switch_data:
  392. .quad __mmap_switched
  393. .quad __data_loc // x4
  394. .quad _data // x5
  395. .quad __bss_start // x6
  396. .quad _end // x7
  397. .quad processor_id // x4
  398. .quad __fdt_pointer // x5
  399. .quad memstart_addr // x6
  400. .quad init_thread_union + THREAD_START_SP // sp
  401. /*
  402. * The following fragment of code is executed with the MMU on in MMU mode, and
  403. * uses absolute addresses; this is not position independent.
  404. */
  405. __mmap_switched:
  406. adr x3, __switch_data + 8
  407. ldp x4, x5, [x3], #16
  408. ldp x6, x7, [x3], #16
  409. cmp x4, x5 // Copy data segment if needed
  410. 1: ccmp x5, x6, #4, ne
  411. b.eq 2f
  412. ldr x16, [x4], #8
  413. str x16, [x5], #8
  414. b 1b
  415. 2:
  416. 1: cmp x6, x7
  417. b.hs 2f
  418. str xzr, [x6], #8 // Clear BSS
  419. b 1b
  420. 2:
  421. ldp x4, x5, [x3], #16
  422. ldr x6, [x3], #8
  423. ldr x16, [x3]
  424. mov sp, x16
  425. str x22, [x4] // Save processor ID
  426. str x21, [x5] // Save FDT pointer
  427. str x24, [x6] // Save PHYS_OFFSET
  428. mov x29, #0
  429. b start_kernel
  430. ENDPROC(__mmap_switched)
  431. /*
  432. * Exception handling. Something went wrong and we can't proceed. We ought to
  433. * tell the user, but since we don't have any guarantee that we're even
  434. * running on the right architecture, we do virtually nothing.
  435. */
  436. __error_p:
  437. ENDPROC(__error_p)
  438. __error:
  439. 1: nop
  440. b 1b
  441. ENDPROC(__error)
  442. /*
  443. * This function gets the processor ID in w0 and searches the cpu_table[] for
  444. * a match. It returns a pointer to the struct cpu_info it found. The
  445. * cpu_table[] must end with an empty (all zeros) structure.
  446. *
  447. * This routine can be called via C code and it needs to work with the MMU
  448. * both disabled and enabled (the offset is calculated automatically).
  449. */
  450. ENTRY(lookup_processor_type)
  451. adr x1, __lookup_processor_type_data
  452. ldp x2, x3, [x1]
  453. sub x1, x1, x2 // get offset between VA and PA
  454. add x3, x3, x1 // convert VA to PA
  455. 1:
  456. ldp w5, w6, [x3] // load cpu_id_val and cpu_id_mask
  457. cbz w5, 2f // end of list?
  458. and w6, w6, w0
  459. cmp w5, w6
  460. b.eq 3f
  461. add x3, x3, #CPU_INFO_SZ
  462. b 1b
  463. 2:
  464. mov x3, #0 // unknown processor
  465. 3:
  466. mov x0, x3
  467. ret
  468. ENDPROC(lookup_processor_type)
  469. .align 3
  470. .type __lookup_processor_type_data, %object
  471. __lookup_processor_type_data:
  472. .quad .
  473. .quad cpu_table
  474. .size __lookup_processor_type_data, . - __lookup_processor_type_data
  475. /*
  476. * Determine validity of the x21 FDT pointer.
  477. * The dtb must be 8-byte aligned and live in the first 512M of memory.
  478. */
  479. __vet_fdt:
  480. tst x21, #0x7
  481. b.ne 1f
  482. cmp x21, x24
  483. b.lt 1f
  484. mov x0, #(1 << 29)
  485. add x0, x0, x24
  486. cmp x21, x0
  487. b.ge 1f
  488. ret
  489. 1:
  490. mov x21, #0
  491. ret
  492. ENDPROC(__vet_fdt)