blob: 0bce493495e9ac6675635f97114e1800712b25fc [file] [log] [blame]
Catalin Marinas9703d9d2012-03-05 11:49:27 +00001/*
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
23#include <linux/linkage.h>
24#include <linux/init.h>
25
26#include <asm/assembler.h>
27#include <asm/ptrace.h>
28#include <asm/asm-offsets.h>
Catalin Marinasc218bca2014-03-26 18:25:55 +000029#include <asm/cache.h>
Javi Merino0359b0e2012-08-29 18:32:18 +010030#include <asm/cputype.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000031#include <asm/memory.h>
32#include <asm/thread_info.h>
33#include <asm/pgtable-hwdef.h>
34#include <asm/pgtable.h>
35#include <asm/page.h>
Marc Zyngierf35a9202012-10-26 15:40:05 +010036#include <asm/virt.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000037
Catalin Marinas9703d9d2012-03-05 11:49:27 +000038#define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET)
39
Mark Rutlandda57a362014-06-24 16:51:37 +010040#if (TEXT_OFFSET & 0xf) != 0
41#error TEXT_OFFSET must be at least 16B aligned
42#elif (PAGE_OFFSET & 0xfffff) != 0
43#error PAGE_OFFSET must be at least 2MB aligned
44#elif TEXT_OFFSET > 0xfffff
45#error TEXT_OFFSET must be less than 2MB
Catalin Marinas9703d9d2012-03-05 11:49:27 +000046#endif
47
Mark Rutlandbd00cd52014-06-24 16:51:35 +010048 .macro pgtbl, ttb0, ttb1, virt_to_phys
49 ldr \ttb1, =swapper_pg_dir
50 ldr \ttb0, =idmap_pg_dir
51 add \ttb1, \ttb1, \virt_to_phys
52 add \ttb0, \ttb0, \virt_to_phys
Catalin Marinas9703d9d2012-03-05 11:49:27 +000053 .endm
54
55#ifdef CONFIG_ARM64_64K_PAGES
56#define BLOCK_SHIFT PAGE_SHIFT
57#define BLOCK_SIZE PAGE_SIZE
Catalin Marinas383c2792014-07-21 15:54:50 +010058#define TABLE_SHIFT PMD_SHIFT
Catalin Marinas9703d9d2012-03-05 11:49:27 +000059#else
60#define BLOCK_SHIFT SECTION_SHIFT
61#define BLOCK_SIZE SECTION_SIZE
Catalin Marinas383c2792014-07-21 15:54:50 +010062#define TABLE_SHIFT PUD_SHIFT
Catalin Marinas9703d9d2012-03-05 11:49:27 +000063#endif
64
65#define KERNEL_START KERNEL_RAM_VADDR
66#define KERNEL_END _end
67
68/*
69 * Initial memory map attributes.
70 */
71#ifndef CONFIG_SMP
72#define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF
73#define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF
74#else
75#define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF | PTE_SHARED
76#define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S
77#endif
78
79#ifdef CONFIG_ARM64_64K_PAGES
80#define MM_MMUFLAGS PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
Catalin Marinas9703d9d2012-03-05 11:49:27 +000081#else
82#define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
Catalin Marinas9703d9d2012-03-05 11:49:27 +000083#endif
84
85/*
86 * Kernel startup entry point.
87 * ---------------------------
88 *
89 * The requirements are:
90 * MMU = off, D-cache = off, I-cache = on or off,
91 * x0 = physical address to the FDT blob.
92 *
93 * This code is mostly position independent so you call this at
94 * __pa(PAGE_OFFSET + TEXT_OFFSET).
95 *
96 * Note that the callee-saved registers are used for storing variables
97 * that are useful before the MMU is enabled. The allocations are described
98 * in the entry routines.
99 */
100 __HEAD
101
102 /*
103 * DO NOT MODIFY. Image header expected by Linux boot-loaders.
104 */
Mark Salter3c7f2552014-04-15 22:47:52 -0400105#ifdef CONFIG_EFI
106efi_head:
107 /*
108 * This add instruction has no meaningful effect except that
109 * its opcode forms the magic "MZ" signature required by UEFI.
110 */
111 add x13, x18, #0x16
112 b stext
113#else
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000114 b stext // branch to kernel start, magic
115 .long 0 // reserved
Mark Salter3c7f2552014-04-15 22:47:52 -0400116#endif
Mark Rutlanda2c1d732014-06-24 16:51:36 +0100117 .quad _kernel_offset_le // Image load offset from start of RAM, little-endian
118 .quad _kernel_size_le // Effective size of kernel image, little-endian
119 .quad _kernel_flags_le // Informative flags, little-endian
Roy Franz4370eec2013-08-15 00:10:00 +0100120 .quad 0 // reserved
121 .quad 0 // reserved
122 .quad 0 // reserved
123 .byte 0x41 // Magic number, "ARM\x64"
124 .byte 0x52
125 .byte 0x4d
126 .byte 0x64
Mark Salter3c7f2552014-04-15 22:47:52 -0400127#ifdef CONFIG_EFI
128 .long pe_header - efi_head // Offset to the PE header.
129#else
Roy Franz4370eec2013-08-15 00:10:00 +0100130 .word 0 // reserved
Mark Salter3c7f2552014-04-15 22:47:52 -0400131#endif
132
133#ifdef CONFIG_EFI
134 .align 3
135pe_header:
136 .ascii "PE"
137 .short 0
138coff_header:
139 .short 0xaa64 // AArch64
140 .short 2 // nr_sections
141 .long 0 // TimeDateStamp
142 .long 0 // PointerToSymbolTable
143 .long 1 // NumberOfSymbols
144 .short section_table - optional_header // SizeOfOptionalHeader
145 .short 0x206 // Characteristics.
146 // IMAGE_FILE_DEBUG_STRIPPED |
147 // IMAGE_FILE_EXECUTABLE_IMAGE |
148 // IMAGE_FILE_LINE_NUMS_STRIPPED
149optional_header:
150 .short 0x20b // PE32+ format
151 .byte 0x02 // MajorLinkerVersion
152 .byte 0x14 // MinorLinkerVersion
153 .long _edata - stext // SizeOfCode
154 .long 0 // SizeOfInitializedData
155 .long 0 // SizeOfUninitializedData
156 .long efi_stub_entry - efi_head // AddressOfEntryPoint
157 .long stext - efi_head // BaseOfCode
158
159extra_header_fields:
160 .quad 0 // ImageBase
161 .long 0x20 // SectionAlignment
162 .long 0x8 // FileAlignment
163 .short 0 // MajorOperatingSystemVersion
164 .short 0 // MinorOperatingSystemVersion
165 .short 0 // MajorImageVersion
166 .short 0 // MinorImageVersion
167 .short 0 // MajorSubsystemVersion
168 .short 0 // MinorSubsystemVersion
169 .long 0 // Win32VersionValue
170
171 .long _edata - efi_head // SizeOfImage
172
173 // Everything before the kernel image is considered part of the header
174 .long stext - efi_head // SizeOfHeaders
175 .long 0 // CheckSum
176 .short 0xa // Subsystem (EFI application)
177 .short 0 // DllCharacteristics
178 .quad 0 // SizeOfStackReserve
179 .quad 0 // SizeOfStackCommit
180 .quad 0 // SizeOfHeapReserve
181 .quad 0 // SizeOfHeapCommit
182 .long 0 // LoaderFlags
183 .long 0x6 // NumberOfRvaAndSizes
184
185 .quad 0 // ExportTable
186 .quad 0 // ImportTable
187 .quad 0 // ResourceTable
188 .quad 0 // ExceptionTable
189 .quad 0 // CertificationTable
190 .quad 0 // BaseRelocationTable
191
192 // Section table
193section_table:
194
195 /*
196 * The EFI application loader requires a relocation section
197 * because EFI applications must be relocatable. This is a
198 * dummy section as far as we are concerned.
199 */
200 .ascii ".reloc"
201 .byte 0
202 .byte 0 // end of 0 padding of section name
203 .long 0
204 .long 0
205 .long 0 // SizeOfRawData
206 .long 0 // PointerToRawData
207 .long 0 // PointerToRelocations
208 .long 0 // PointerToLineNumbers
209 .short 0 // NumberOfRelocations
210 .short 0 // NumberOfLineNumbers
211 .long 0x42100040 // Characteristics (section flags)
212
213
214 .ascii ".text"
215 .byte 0
216 .byte 0
217 .byte 0 // end of 0 padding of section name
218 .long _edata - stext // VirtualSize
219 .long stext - efi_head // VirtualAddress
220 .long _edata - stext // SizeOfRawData
221 .long stext - efi_head // PointerToRawData
222
223 .long 0 // PointerToRelocations (0 for executables)
224 .long 0 // PointerToLineNumbers (0 for executables)
225 .short 0 // NumberOfRelocations (0 for executables)
226 .short 0 // NumberOfLineNumbers (0 for executables)
227 .long 0xe0500020 // Characteristics (section flags)
228 .align 5
229#endif
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000230
231ENTRY(stext)
232 mov x21, x0 // x21=FDT
Matthew Leach828e9832013-10-11 14:52:16 +0100233 bl el2_setup // Drop to EL1, w20=cpu_boot_mode
Marc Zyngierf35a9202012-10-26 15:40:05 +0100234 bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
Matthew Leach828e9832013-10-11 14:52:16 +0100235 bl set_cpu_boot_mode_flag
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000236 mrs x22, midr_el1 // x22=cpuid
237 mov x0, x22
238 bl lookup_processor_type
239 mov x23, x0 // x23=current cpu_table
240 cbz x23, __error_p // invalid processor (x23=0)?
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000241 bl __vet_fdt
242 bl __create_page_tables // x25=TTBR0, x26=TTBR1
243 /*
244 * The following calls CPU specific code in a position independent
245 * manner. See arch/arm64/mm/proc.S for details. x23 = base of
246 * cpu_info structure selected by lookup_processor_type above.
247 * On return, the CPU will be ready for the MMU to be turned on and
248 * the TCR will have been set.
249 */
250 ldr x27, __switch_data // address to jump to after
251 // MMU has been enabled
252 adr lr, __enable_mmu // return (PIC) address
253 ldr x12, [x23, #CPU_INFO_SETUP]
254 add x12, x12, x28 // __virt_to_phys
255 br x12 // initialise processor
256ENDPROC(stext)
257
258/*
259 * If we're fortunate enough to boot at EL2, ensure that the world is
260 * sane before dropping to EL1.
Matthew Leach828e9832013-10-11 14:52:16 +0100261 *
262 * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x20 if
263 * booted in EL1 or EL2 respectively.
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000264 */
265ENTRY(el2_setup)
266 mrs x0, CurrentEL
Marc Zyngier974c8e42014-06-06 14:16:21 +0100267 cmp x0, #CurrentEL_EL2
Matthew Leach9cf71722013-10-11 14:52:17 +0100268 b.ne 1f
269 mrs x0, sctlr_el2
270CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2
271CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2
272 msr sctlr_el2, x0
273 b 2f
2741: mrs x0, sctlr_el1
275CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1
276CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1
277 msr sctlr_el1, x0
Matthew Leach828e9832013-10-11 14:52:16 +0100278 mov w20, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
Matthew Leach9cf71722013-10-11 14:52:17 +0100279 isb
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000280 ret
281
282 /* Hyp configuration. */
Matthew Leach9cf71722013-10-11 14:52:17 +01002832: mov x0, #(1 << 31) // 64-bit EL1
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000284 msr hcr_el2, x0
285
286 /* Generic timers. */
287 mrs x0, cnthctl_el2
288 orr x0, x0, #3 // Enable EL1 physical timers
289 msr cnthctl_el2, x0
Will Deacon1f75ff02012-11-29 22:48:31 +0000290 msr cntvoff_el2, xzr // Clear virtual offset
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000291
292 /* Populate ID registers. */
293 mrs x0, midr_el1
294 mrs x1, mpidr_el1
295 msr vpidr_el2, x0
296 msr vmpidr_el2, x1
297
298 /* sctlr_el1 */
299 mov x0, #0x0800 // Set/clear RES{1,0} bits
Matthew Leach9cf71722013-10-11 14:52:17 +0100300CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems
301CPU_LE( movk x0, #0x30d0, lsl #16 ) // Clear EE and E0E on LE systems
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000302 msr sctlr_el1, x0
303
304 /* Coprocessor traps. */
305 mov x0, #0x33ff
306 msr cptr_el2, x0 // Disable copro. traps to EL2
307
308#ifdef CONFIG_COMPAT
309 msr hstr_el2, xzr // Disable CP15 traps to EL2
310#endif
311
Marc Zyngier7dbfbe52012-11-06 19:27:59 +0000312 /* Stage-2 translation */
313 msr vttbr_el2, xzr
314
Marc Zyngier712c6ff2012-10-19 17:46:27 +0100315 /* Hypervisor stub */
316 adr x0, __hyp_stub_vectors
317 msr vbar_el2, x0
318
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000319 /* spsr */
320 mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
321 PSR_MODE_EL1h)
322 msr spsr_el2, x0
323 msr elr_el2, lr
Matthew Leach828e9832013-10-11 14:52:16 +0100324 mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000325 eret
326ENDPROC(el2_setup)
327
Marc Zyngierf35a9202012-10-26 15:40:05 +0100328/*
Matthew Leach828e9832013-10-11 14:52:16 +0100329 * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
330 * in x20. See arch/arm64/include/asm/virt.h for more info.
331 */
332ENTRY(set_cpu_boot_mode_flag)
333 ldr x1, =__boot_cpu_mode // Compute __boot_cpu_mode
334 add x1, x1, x28
335 cmp w20, #BOOT_CPU_MODE_EL2
336 b.ne 1f
337 add x1, x1, #4
Will Deacond0488592014-05-02 16:24:13 +01003381: str w20, [x1] // This CPU has booted in EL1
339 dmb sy
340 dc ivac, x1 // Invalidate potentially stale cache line
Matthew Leach828e9832013-10-11 14:52:16 +0100341 ret
342ENDPROC(set_cpu_boot_mode_flag)
343
344/*
Marc Zyngierf35a9202012-10-26 15:40:05 +0100345 * We need to find out the CPU boot mode long after boot, so we need to
346 * store it in a writable variable.
347 *
348 * This is not in .bss, because we set it sufficiently early that the boot-time
349 * zeroing of .bss would clobber it.
350 */
Catalin Marinasc218bca2014-03-26 18:25:55 +0000351 .pushsection .data..cacheline_aligned
Marc Zyngierf35a9202012-10-26 15:40:05 +0100352ENTRY(__boot_cpu_mode)
Catalin Marinasc218bca2014-03-26 18:25:55 +0000353 .align L1_CACHE_SHIFT
Marc Zyngierf35a9202012-10-26 15:40:05 +0100354 .long BOOT_CPU_MODE_EL2
355 .long 0
356 .popsection
357
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000358 .align 3
3592: .quad .
360 .quad PAGE_OFFSET
361
362#ifdef CONFIG_SMP
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000363 .align 3
3641: .quad .
365 .quad secondary_holding_pen_release
366
367 /*
368 * This provides a "holding pen" for platforms to hold all secondary
369 * cores are held until we're ready for them to initialise.
370 */
371ENTRY(secondary_holding_pen)
Matthew Leach828e9832013-10-11 14:52:16 +0100372 bl el2_setup // Drop to EL1, w20=cpu_boot_mode
373 bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
374 bl set_cpu_boot_mode_flag
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000375 mrs x0, mpidr_el1
Javi Merino0359b0e2012-08-29 18:32:18 +0100376 ldr x1, =MPIDR_HWID_BITMASK
377 and x0, x0, x1
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000378 adr x1, 1b
379 ldp x2, x3, [x1]
380 sub x1, x1, x2
381 add x3, x3, x1
382pen: ldr x4, [x3]
383 cmp x4, x0
384 b.eq secondary_startup
385 wfe
386 b pen
387ENDPROC(secondary_holding_pen)
Mark Rutland652af892013-10-24 20:30:16 +0100388
389 /*
390 * Secondary entry point that jumps straight into the kernel. Only to
391 * be used where CPUs are brought online dynamically by the kernel.
392 */
393ENTRY(secondary_entry)
Mark Rutland652af892013-10-24 20:30:16 +0100394 bl el2_setup // Drop to EL1
Lorenzo Pieralisi85cc00e2013-11-18 18:56:42 +0000395 bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
396 bl set_cpu_boot_mode_flag
Mark Rutland652af892013-10-24 20:30:16 +0100397 b secondary_startup
398ENDPROC(secondary_entry)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000399
400ENTRY(secondary_startup)
401 /*
402 * Common entry point for secondary CPUs.
403 */
404 mrs x22, midr_el1 // x22=cpuid
405 mov x0, x22
406 bl lookup_processor_type
407 mov x23, x0 // x23=current cpu_table
408 cbz x23, __error_p // invalid processor (x23=0)?
409
Mark Rutlandbd00cd52014-06-24 16:51:35 +0100410 pgtbl x25, x26, x28 // x25=TTBR0, x26=TTBR1
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000411 ldr x12, [x23, #CPU_INFO_SETUP]
412 add x12, x12, x28 // __virt_to_phys
413 blr x12 // initialise processor
414
415 ldr x21, =secondary_data
416 ldr x27, =__secondary_switched // address to jump to after enabling the MMU
417 b __enable_mmu
418ENDPROC(secondary_startup)
419
420ENTRY(__secondary_switched)
421 ldr x0, [x21] // get secondary_data.stack
422 mov sp, x0
423 mov x29, #0
424 b secondary_start_kernel
425ENDPROC(__secondary_switched)
426#endif /* CONFIG_SMP */
427
428/*
429 * Setup common bits before finally enabling the MMU. Essentially this is just
430 * loading the page table pointer and vector base registers.
431 *
432 * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
433 * the MMU.
434 */
435__enable_mmu:
436 ldr x5, =vectors
437 msr vbar_el1, x5
438 msr ttbr0_el1, x25 // load TTBR0
439 msr ttbr1_el1, x26 // load TTBR1
440 isb
441 b __turn_mmu_on
442ENDPROC(__enable_mmu)
443
444/*
445 * Enable the MMU. This completely changes the structure of the visible memory
446 * space. You will not be able to trace execution through this.
447 *
448 * x0 = system control register
449 * x27 = *virtual* address to jump to upon completion
450 *
451 * other registers depend on the function called upon completion
Mark Rutland909a4062014-06-24 16:51:34 +0100452 *
453 * We align the entire function to the smallest power of two larger than it to
454 * ensure it fits within a single block map entry. Otherwise were PHYS_OFFSET
455 * close to the end of a 512MB or 1GB block we might require an additional
456 * table to map the entire function.
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000457 */
Mark Rutland909a4062014-06-24 16:51:34 +0100458 .align 4
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000459__turn_mmu_on:
460 msr sctlr_el1, x0
461 isb
462 br x27
463ENDPROC(__turn_mmu_on)
464
465/*
466 * Calculate the start of physical memory.
467 */
468__calc_phys_offset:
469 adr x0, 1f
470 ldp x1, x2, [x0]
471 sub x28, x0, x1 // x28 = PHYS_OFFSET - PAGE_OFFSET
472 add x24, x2, x28 // x24 = PHYS_OFFSET
473 ret
474ENDPROC(__calc_phys_offset)
475
476 .align 3
4771: .quad .
478 .quad PAGE_OFFSET
479
480/*
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100481 * Macro to create a table entry to the next page.
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000482 *
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100483 * tbl: page table address
484 * virt: virtual address
485 * shift: #imm page table shift
486 * ptrs: #imm pointers per table page
487 *
488 * Preserves: virt
489 * Corrupts: tmp1, tmp2
490 * Returns: tbl -> next level table page address
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000491 */
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100492 .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
493 lsr \tmp1, \virt, #\shift
494 and \tmp1, \tmp1, #\ptrs - 1 // table index
495 add \tmp2, \tbl, #PAGE_SIZE
496 orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
497 str \tmp2, [\tbl, \tmp1, lsl #3]
498 add \tbl, \tbl, #PAGE_SIZE // next level table page
Jungseok Leec79b9542014-05-12 18:40:51 +0900499 .endm
500
501/*
502 * Macro to populate the PGD (and possibily PUD) for the corresponding
503 * block entry in the next level (tbl) for the given virtual address.
504 *
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100505 * Preserves: tbl, next, virt
506 * Corrupts: tmp1, tmp2
Jungseok Leec79b9542014-05-12 18:40:51 +0900507 */
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100508 .macro create_pgd_entry, tbl, virt, tmp1, tmp2
509 create_table_entry \tbl, \virt, PGDIR_SHIFT, PTRS_PER_PGD, \tmp1, \tmp2
Catalin Marinas383c2792014-07-21 15:54:50 +0100510#if SWAPPER_PGTABLE_LEVELS == 3
511 create_table_entry \tbl, \virt, TABLE_SHIFT, PTRS_PER_PTE, \tmp1, \tmp2
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100512#endif
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000513 .endm
514
515/*
516 * Macro to populate block entries in the page table for the start..end
517 * virtual range (inclusive).
518 *
519 * Preserves: tbl, flags
520 * Corrupts: phys, start, end, pstate
521 */
Catalin Marinasea8c2e12014-02-17 12:03:25 +0000522 .macro create_block_map, tbl, flags, phys, start, end
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000523 lsr \phys, \phys, #BLOCK_SHIFT
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000524 lsr \start, \start, #BLOCK_SHIFT
525 and \start, \start, #PTRS_PER_PTE - 1 // table index
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000526 orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000527 lsr \end, \end, #BLOCK_SHIFT
528 and \end, \end, #PTRS_PER_PTE - 1 // table end index
Catalin Marinas9703d9d2012-03-05 11:49:27 +00005299999: str \phys, [\tbl, \start, lsl #3] // store the entry
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000530 add \start, \start, #1 // next entry
531 add \phys, \phys, #BLOCK_SIZE // next block
532 cmp \start, \end
533 b.ls 9999b
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000534 .endm
535
536/*
537 * Setup the initial page tables. We only setup the barest amount which is
538 * required to get the kernel running. The following sections are required:
539 * - identity mapping to enable the MMU (low address, TTBR0)
540 * - first few MB of the kernel linear mapping to jump to once the MMU has
541 * been enabled, including the FDT blob (TTBR1)
Mark Salterbf4b5582014-04-07 15:39:52 -0700542 * - pgd entry for fixed mappings (TTBR1)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000543 */
544__create_page_tables:
Mark Rutlandbd00cd52014-06-24 16:51:35 +0100545 pgtbl x25, x26, x28 // idmap_pg_dir and swapper_pg_dir addresses
Catalin Marinasc218bca2014-03-26 18:25:55 +0000546 mov x27, lr
547
548 /*
549 * Invalidate the idmap and swapper page tables to avoid potential
550 * dirty cache lines being evicted.
551 */
552 mov x0, x25
553 add x1, x26, #SWAPPER_DIR_SIZE
554 bl __inval_cache_range
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000555
556 /*
557 * Clear the idmap and swapper page tables.
558 */
559 mov x0, x25
560 add x6, x26, #SWAPPER_DIR_SIZE
5611: stp xzr, xzr, [x0], #16
562 stp xzr, xzr, [x0], #16
563 stp xzr, xzr, [x0], #16
564 stp xzr, xzr, [x0], #16
565 cmp x0, x6
566 b.lo 1b
567
568 ldr x7, =MM_MMUFLAGS
569
570 /*
571 * Create the identity mapping.
572 */
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100573 mov x0, x25 // idmap_pg_dir
Catalin Marinasea8c2e12014-02-17 12:03:25 +0000574 ldr x3, =KERNEL_START
575 add x3, x3, x28 // __pa(KERNEL_START)
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100576 create_pgd_entry x0, x3, x5, x6
Catalin Marinasea8c2e12014-02-17 12:03:25 +0000577 ldr x6, =KERNEL_END
578 mov x5, x3 // __pa(KERNEL_START)
579 add x6, x6, x28 // __pa(KERNEL_END)
580 create_block_map x0, x7, x3, x5, x6
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000581
582 /*
583 * Map the kernel image (starting with PHYS_OFFSET).
584 */
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100585 mov x0, x26 // swapper_pg_dir
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000586 mov x5, #PAGE_OFFSET
Catalin Marinasb4a0d8b2014-07-16 12:10:33 +0100587 create_pgd_entry x0, x5, x3, x6
Catalin Marinasea8c2e12014-02-17 12:03:25 +0000588 ldr x6, =KERNEL_END
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000589 mov x3, x24 // phys offset
590 create_block_map x0, x7, x3, x5, x6
591
592 /*
593 * Map the FDT blob (maximum 2MB; must be within 512MB of
594 * PHYS_OFFSET).
595 */
596 mov x3, x21 // FDT phys address
597 and x3, x3, #~((1 << 21) - 1) // 2MB aligned
598 mov x6, #PAGE_OFFSET
599 sub x5, x3, x24 // subtract PHYS_OFFSET
600 tst x5, #~((1 << 29) - 1) // within 512MB?
601 csel x21, xzr, x21, ne // zero the FDT pointer
602 b.ne 1f
603 add x5, x5, x6 // __va(FDT blob)
604 add x6, x5, #1 << 21 // 2MB for the FDT blob
605 sub x6, x6, #1 // inclusive range
606 create_block_map x0, x7, x3, x5, x6
6071:
Catalin Marinas2475ff92012-10-23 14:55:08 +0100608 /*
Catalin Marinasc218bca2014-03-26 18:25:55 +0000609 * Since the page tables have been populated with non-cacheable
610 * accesses (MMU disabled), invalidate the idmap and swapper page
611 * tables again to remove any speculatively loaded cache lines.
612 */
613 mov x0, x25
614 add x1, x26, #SWAPPER_DIR_SIZE
615 bl __inval_cache_range
616
617 mov lr, x27
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000618 ret
619ENDPROC(__create_page_tables)
620 .ltorg
621
622 .align 3
623 .type __switch_data, %object
624__switch_data:
625 .quad __mmap_switched
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000626 .quad __bss_start // x6
Mark Rutlandbd00cd52014-06-24 16:51:35 +0100627 .quad __bss_stop // x7
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000628 .quad processor_id // x4
629 .quad __fdt_pointer // x5
630 .quad memstart_addr // x6
631 .quad init_thread_union + THREAD_START_SP // sp
632
633/*
634 * The following fragment of code is executed with the MMU on in MMU mode, and
635 * uses absolute addresses; this is not position independent.
636 */
637__mmap_switched:
638 adr x3, __switch_data + 8
639
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000640 ldp x6, x7, [x3], #16
Catalin Marinas9703d9d2012-03-05 11:49:27 +00006411: cmp x6, x7
642 b.hs 2f
643 str xzr, [x6], #8 // Clear BSS
644 b 1b
6452:
646 ldp x4, x5, [x3], #16
647 ldr x6, [x3], #8
648 ldr x16, [x3]
649 mov sp, x16
650 str x22, [x4] // Save processor ID
651 str x21, [x5] // Save FDT pointer
652 str x24, [x6] // Save PHYS_OFFSET
653 mov x29, #0
654 b start_kernel
655ENDPROC(__mmap_switched)
656
657/*
658 * Exception handling. Something went wrong and we can't proceed. We ought to
659 * tell the user, but since we don't have any guarantee that we're even
660 * running on the right architecture, we do virtually nothing.
661 */
662__error_p:
663ENDPROC(__error_p)
664
665__error:
6661: nop
667 b 1b
668ENDPROC(__error)
669
670/*
671 * This function gets the processor ID in w0 and searches the cpu_table[] for
672 * a match. It returns a pointer to the struct cpu_info it found. The
673 * cpu_table[] must end with an empty (all zeros) structure.
674 *
675 * This routine can be called via C code and it needs to work with the MMU
676 * both disabled and enabled (the offset is calculated automatically).
677 */
678ENTRY(lookup_processor_type)
679 adr x1, __lookup_processor_type_data
680 ldp x2, x3, [x1]
681 sub x1, x1, x2 // get offset between VA and PA
682 add x3, x3, x1 // convert VA to PA
6831:
684 ldp w5, w6, [x3] // load cpu_id_val and cpu_id_mask
685 cbz w5, 2f // end of list?
686 and w6, w6, w0
687 cmp w5, w6
688 b.eq 3f
689 add x3, x3, #CPU_INFO_SZ
690 b 1b
6912:
692 mov x3, #0 // unknown processor
6933:
694 mov x0, x3
695 ret
696ENDPROC(lookup_processor_type)
697
698 .align 3
699 .type __lookup_processor_type_data, %object
700__lookup_processor_type_data:
701 .quad .
702 .quad cpu_table
703 .size __lookup_processor_type_data, . - __lookup_processor_type_data
704
705/*
706 * Determine validity of the x21 FDT pointer.
707 * The dtb must be 8-byte aligned and live in the first 512M of memory.
708 */
709__vet_fdt:
710 tst x21, #0x7
711 b.ne 1f
712 cmp x21, x24
713 b.lt 1f
714 mov x0, #(1 << 29)
715 add x0, x0, x24
716 cmp x21, x0
717 b.ge 1f
718 ret
7191:
720 mov x21, #0
721 ret
722ENDPROC(__vet_fdt)