blob: 90dec55b17a2bf32cc0ebd8db8f00e1230d26a8d [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>
29#include <asm/memory.h>
30#include <asm/thread_info.h>
31#include <asm/pgtable-hwdef.h>
32#include <asm/pgtable.h>
33#include <asm/page.h>
34
35/*
36 * swapper_pg_dir is the virtual address of the initial page table. We place
37 * the page tables 3 * PAGE_SIZE below KERNEL_RAM_VADDR. The idmap_pg_dir has
38 * 2 pages and is placed below swapper_pg_dir.
39 */
40#define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET)
41
42#if (KERNEL_RAM_VADDR & 0xfffff) != 0x80000
43#error KERNEL_RAM_VADDR must start at 0xXXX80000
44#endif
45
46#define SWAPPER_DIR_SIZE (3 * PAGE_SIZE)
47#define IDMAP_DIR_SIZE (2 * PAGE_SIZE)
48
49 .globl swapper_pg_dir
50 .equ swapper_pg_dir, KERNEL_RAM_VADDR - SWAPPER_DIR_SIZE
51
52 .globl idmap_pg_dir
53 .equ idmap_pg_dir, swapper_pg_dir - IDMAP_DIR_SIZE
54
55 .macro pgtbl, ttb0, ttb1, phys
56 add \ttb1, \phys, #TEXT_OFFSET - SWAPPER_DIR_SIZE
57 sub \ttb0, \ttb1, #IDMAP_DIR_SIZE
58 .endm
59
60#ifdef CONFIG_ARM64_64K_PAGES
61#define BLOCK_SHIFT PAGE_SHIFT
62#define BLOCK_SIZE PAGE_SIZE
63#else
64#define BLOCK_SHIFT SECTION_SHIFT
65#define BLOCK_SIZE SECTION_SIZE
66#endif
67
68#define KERNEL_START KERNEL_RAM_VADDR
69#define KERNEL_END _end
70
71/*
72 * Initial memory map attributes.
73 */
74#ifndef CONFIG_SMP
75#define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF
76#define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF
77#else
78#define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF | PTE_SHARED
79#define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S
80#endif
81
82#ifdef CONFIG_ARM64_64K_PAGES
83#define MM_MMUFLAGS PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
84#define IO_MMUFLAGS PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_XN | PTE_FLAGS
85#else
86#define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
87#define IO_MMUFLAGS PMD_ATTRINDX(MT_DEVICE_nGnRE) | PMD_SECT_XN | PMD_FLAGS
88#endif
89
90/*
91 * Kernel startup entry point.
92 * ---------------------------
93 *
94 * The requirements are:
95 * MMU = off, D-cache = off, I-cache = on or off,
96 * x0 = physical address to the FDT blob.
97 *
98 * This code is mostly position independent so you call this at
99 * __pa(PAGE_OFFSET + TEXT_OFFSET).
100 *
101 * Note that the callee-saved registers are used for storing variables
102 * that are useful before the MMU is enabled. The allocations are described
103 * in the entry routines.
104 */
105 __HEAD
106
107 /*
108 * DO NOT MODIFY. Image header expected by Linux boot-loaders.
109 */
110 b stext // branch to kernel start, magic
111 .long 0 // reserved
112 .quad TEXT_OFFSET // Image load offset from start of RAM
113 .quad 0 // reserved
114 .quad 0 // reserved
115
116ENTRY(stext)
117 mov x21, x0 // x21=FDT
118 bl el2_setup // Drop to EL1
119 mrs x22, midr_el1 // x22=cpuid
120 mov x0, x22
121 bl lookup_processor_type
122 mov x23, x0 // x23=current cpu_table
123 cbz x23, __error_p // invalid processor (x23=0)?
124 bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
125 bl __vet_fdt
126 bl __create_page_tables // x25=TTBR0, x26=TTBR1
127 /*
128 * The following calls CPU specific code in a position independent
129 * manner. See arch/arm64/mm/proc.S for details. x23 = base of
130 * cpu_info structure selected by lookup_processor_type above.
131 * On return, the CPU will be ready for the MMU to be turned on and
132 * the TCR will have been set.
133 */
134 ldr x27, __switch_data // address to jump to after
135 // MMU has been enabled
136 adr lr, __enable_mmu // return (PIC) address
137 ldr x12, [x23, #CPU_INFO_SETUP]
138 add x12, x12, x28 // __virt_to_phys
139 br x12 // initialise processor
140ENDPROC(stext)
141
142/*
143 * If we're fortunate enough to boot at EL2, ensure that the world is
144 * sane before dropping to EL1.
145 */
146ENTRY(el2_setup)
147 mrs x0, CurrentEL
148 cmp x0, #PSR_MODE_EL2t
149 ccmp x0, #PSR_MODE_EL2h, #0x4, ne
150 b.eq 1f
151 ret
152
153 /* Hyp configuration. */
1541: mov x0, #(1 << 31) // 64-bit EL1
155 msr hcr_el2, x0
156
157 /* Generic timers. */
158 mrs x0, cnthctl_el2
159 orr x0, x0, #3 // Enable EL1 physical timers
160 msr cnthctl_el2, x0
Will Deacon1f75ff02012-11-29 22:48:31 +0000161 msr cntvoff_el2, xzr // Clear virtual offset
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000162
163 /* Populate ID registers. */
164 mrs x0, midr_el1
165 mrs x1, mpidr_el1
166 msr vpidr_el2, x0
167 msr vmpidr_el2, x1
168
169 /* sctlr_el1 */
170 mov x0, #0x0800 // Set/clear RES{1,0} bits
171 movk x0, #0x30d0, lsl #16
172 msr sctlr_el1, x0
173
174 /* Coprocessor traps. */
175 mov x0, #0x33ff
176 msr cptr_el2, x0 // Disable copro. traps to EL2
177
178#ifdef CONFIG_COMPAT
179 msr hstr_el2, xzr // Disable CP15 traps to EL2
180#endif
181
182 /* spsr */
183 mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
184 PSR_MODE_EL1h)
185 msr spsr_el2, x0
186 msr elr_el2, lr
187 eret
188ENDPROC(el2_setup)
189
190 .align 3
1912: .quad .
192 .quad PAGE_OFFSET
193
194#ifdef CONFIG_SMP
195 .pushsection .smp.pen.text, "ax"
196 .align 3
1971: .quad .
198 .quad secondary_holding_pen_release
199
200 /*
201 * This provides a "holding pen" for platforms to hold all secondary
202 * cores are held until we're ready for them to initialise.
203 */
204ENTRY(secondary_holding_pen)
205 bl el2_setup // Drop to EL1
206 mrs x0, mpidr_el1
207 and x0, x0, #15 // CPU number
208 adr x1, 1b
209 ldp x2, x3, [x1]
210 sub x1, x1, x2
211 add x3, x3, x1
212pen: ldr x4, [x3]
213 cmp x4, x0
214 b.eq secondary_startup
215 wfe
216 b pen
217ENDPROC(secondary_holding_pen)
218 .popsection
219
220ENTRY(secondary_startup)
221 /*
222 * Common entry point for secondary CPUs.
223 */
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 cbz x23, __error_p // invalid processor (x23=0)?
229
230 bl __calc_phys_offset // x24=phys offset
231 pgtbl x25, x26, x24 // x25=TTBR0, x26=TTBR1
232 ldr x12, [x23, #CPU_INFO_SETUP]
233 add x12, x12, x28 // __virt_to_phys
234 blr x12 // initialise processor
235
236 ldr x21, =secondary_data
237 ldr x27, =__secondary_switched // address to jump to after enabling the MMU
238 b __enable_mmu
239ENDPROC(secondary_startup)
240
241ENTRY(__secondary_switched)
242 ldr x0, [x21] // get secondary_data.stack
243 mov sp, x0
244 mov x29, #0
245 b secondary_start_kernel
246ENDPROC(__secondary_switched)
247#endif /* CONFIG_SMP */
248
249/*
250 * Setup common bits before finally enabling the MMU. Essentially this is just
251 * loading the page table pointer and vector base registers.
252 *
253 * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
254 * the MMU.
255 */
256__enable_mmu:
257 ldr x5, =vectors
258 msr vbar_el1, x5
259 msr ttbr0_el1, x25 // load TTBR0
260 msr ttbr1_el1, x26 // load TTBR1
261 isb
262 b __turn_mmu_on
263ENDPROC(__enable_mmu)
264
265/*
266 * Enable the MMU. This completely changes the structure of the visible memory
267 * space. You will not be able to trace execution through this.
268 *
269 * x0 = system control register
270 * x27 = *virtual* address to jump to upon completion
271 *
272 * other registers depend on the function called upon completion
273 */
274 .align 6
275__turn_mmu_on:
276 msr sctlr_el1, x0
277 isb
278 br x27
279ENDPROC(__turn_mmu_on)
280
281/*
282 * Calculate the start of physical memory.
283 */
284__calc_phys_offset:
285 adr x0, 1f
286 ldp x1, x2, [x0]
287 sub x28, x0, x1 // x28 = PHYS_OFFSET - PAGE_OFFSET
288 add x24, x2, x28 // x24 = PHYS_OFFSET
289 ret
290ENDPROC(__calc_phys_offset)
291
292 .align 3
2931: .quad .
294 .quad PAGE_OFFSET
295
296/*
297 * Macro to populate the PGD for the corresponding block entry in the next
298 * level (tbl) for the given virtual address.
299 *
300 * Preserves: pgd, tbl, virt
301 * Corrupts: tmp1, tmp2
302 */
303 .macro create_pgd_entry, pgd, tbl, virt, tmp1, tmp2
304 lsr \tmp1, \virt, #PGDIR_SHIFT
305 and \tmp1, \tmp1, #PTRS_PER_PGD - 1 // PGD index
306 orr \tmp2, \tbl, #3 // PGD entry table type
307 str \tmp2, [\pgd, \tmp1, lsl #3]
308 .endm
309
310/*
311 * Macro to populate block entries in the page table for the start..end
312 * virtual range (inclusive).
313 *
314 * Preserves: tbl, flags
315 * Corrupts: phys, start, end, pstate
316 */
317 .macro create_block_map, tbl, flags, phys, start, end, idmap=0
318 lsr \phys, \phys, #BLOCK_SHIFT
319 .if \idmap
320 and \start, \phys, #PTRS_PER_PTE - 1 // table index
321 .else
322 lsr \start, \start, #BLOCK_SHIFT
323 and \start, \start, #PTRS_PER_PTE - 1 // table index
324 .endif
325 orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry
326 .ifnc \start,\end
327 lsr \end, \end, #BLOCK_SHIFT
328 and \end, \end, #PTRS_PER_PTE - 1 // table end index
329 .endif
3309999: str \phys, [\tbl, \start, lsl #3] // store the entry
331 .ifnc \start,\end
332 add \start, \start, #1 // next entry
333 add \phys, \phys, #BLOCK_SIZE // next block
334 cmp \start, \end
335 b.ls 9999b
336 .endif
337 .endm
338
339/*
340 * Setup the initial page tables. We only setup the barest amount which is
341 * required to get the kernel running. The following sections are required:
342 * - identity mapping to enable the MMU (low address, TTBR0)
343 * - first few MB of the kernel linear mapping to jump to once the MMU has
344 * been enabled, including the FDT blob (TTBR1)
345 */
346__create_page_tables:
347 pgtbl x25, x26, x24 // idmap_pg_dir and swapper_pg_dir addresses
348
349 /*
350 * Clear the idmap and swapper page tables.
351 */
352 mov x0, x25
353 add x6, x26, #SWAPPER_DIR_SIZE
3541: stp xzr, xzr, [x0], #16
355 stp xzr, xzr, [x0], #16
356 stp xzr, xzr, [x0], #16
357 stp xzr, xzr, [x0], #16
358 cmp x0, x6
359 b.lo 1b
360
361 ldr x7, =MM_MMUFLAGS
362
363 /*
364 * Create the identity mapping.
365 */
366 add x0, x25, #PAGE_SIZE // section table address
367 adr x3, __turn_mmu_on // virtual/physical address
368 create_pgd_entry x25, x0, x3, x5, x6
369 create_block_map x0, x7, x3, x5, x5, idmap=1
370
371 /*
372 * Map the kernel image (starting with PHYS_OFFSET).
373 */
374 add x0, x26, #PAGE_SIZE // section table address
375 mov x5, #PAGE_OFFSET
376 create_pgd_entry x26, x0, x5, x3, x6
377 ldr x6, =KERNEL_END - 1
378 mov x3, x24 // phys offset
379 create_block_map x0, x7, x3, x5, x6
380
381 /*
382 * Map the FDT blob (maximum 2MB; must be within 512MB of
383 * PHYS_OFFSET).
384 */
385 mov x3, x21 // FDT phys address
386 and x3, x3, #~((1 << 21) - 1) // 2MB aligned
387 mov x6, #PAGE_OFFSET
388 sub x5, x3, x24 // subtract PHYS_OFFSET
389 tst x5, #~((1 << 29) - 1) // within 512MB?
390 csel x21, xzr, x21, ne // zero the FDT pointer
391 b.ne 1f
392 add x5, x5, x6 // __va(FDT blob)
393 add x6, x5, #1 << 21 // 2MB for the FDT blob
394 sub x6, x6, #1 // inclusive range
395 create_block_map x0, x7, x3, x5, x6
3961:
397 ret
398ENDPROC(__create_page_tables)
399 .ltorg
400
401 .align 3
402 .type __switch_data, %object
403__switch_data:
404 .quad __mmap_switched
405 .quad __data_loc // x4
406 .quad _data // x5
407 .quad __bss_start // x6
408 .quad _end // x7
409 .quad processor_id // x4
410 .quad __fdt_pointer // x5
411 .quad memstart_addr // x6
412 .quad init_thread_union + THREAD_START_SP // sp
413
414/*
415 * The following fragment of code is executed with the MMU on in MMU mode, and
416 * uses absolute addresses; this is not position independent.
417 */
418__mmap_switched:
419 adr x3, __switch_data + 8
420
421 ldp x4, x5, [x3], #16
422 ldp x6, x7, [x3], #16
423 cmp x4, x5 // Copy data segment if needed
4241: ccmp x5, x6, #4, ne
425 b.eq 2f
426 ldr x16, [x4], #8
427 str x16, [x5], #8
428 b 1b
4292:
4301: cmp x6, x7
431 b.hs 2f
432 str xzr, [x6], #8 // Clear BSS
433 b 1b
4342:
435 ldp x4, x5, [x3], #16
436 ldr x6, [x3], #8
437 ldr x16, [x3]
438 mov sp, x16
439 str x22, [x4] // Save processor ID
440 str x21, [x5] // Save FDT pointer
441 str x24, [x6] // Save PHYS_OFFSET
442 mov x29, #0
443 b start_kernel
444ENDPROC(__mmap_switched)
445
446/*
447 * Exception handling. Something went wrong and we can't proceed. We ought to
448 * tell the user, but since we don't have any guarantee that we're even
449 * running on the right architecture, we do virtually nothing.
450 */
451__error_p:
452ENDPROC(__error_p)
453
454__error:
4551: nop
456 b 1b
457ENDPROC(__error)
458
459/*
460 * This function gets the processor ID in w0 and searches the cpu_table[] for
461 * a match. It returns a pointer to the struct cpu_info it found. The
462 * cpu_table[] must end with an empty (all zeros) structure.
463 *
464 * This routine can be called via C code and it needs to work with the MMU
465 * both disabled and enabled (the offset is calculated automatically).
466 */
467ENTRY(lookup_processor_type)
468 adr x1, __lookup_processor_type_data
469 ldp x2, x3, [x1]
470 sub x1, x1, x2 // get offset between VA and PA
471 add x3, x3, x1 // convert VA to PA
4721:
473 ldp w5, w6, [x3] // load cpu_id_val and cpu_id_mask
474 cbz w5, 2f // end of list?
475 and w6, w6, w0
476 cmp w5, w6
477 b.eq 3f
478 add x3, x3, #CPU_INFO_SZ
479 b 1b
4802:
481 mov x3, #0 // unknown processor
4823:
483 mov x0, x3
484 ret
485ENDPROC(lookup_processor_type)
486
487 .align 3
488 .type __lookup_processor_type_data, %object
489__lookup_processor_type_data:
490 .quad .
491 .quad cpu_table
492 .size __lookup_processor_type_data, . - __lookup_processor_type_data
493
494/*
495 * Determine validity of the x21 FDT pointer.
496 * The dtb must be 8-byte aligned and live in the first 512M of memory.
497 */
498__vet_fdt:
499 tst x21, #0x7
500 b.ne 1f
501 cmp x21, x24
502 b.lt 1f
503 mov x0, #(1 << 29)
504 add x0, x0, x24
505 cmp x21, x0
506 b.ge 1f
507 ret
5081:
509 mov x21, #0
510 ret
511ENDPROC(__vet_fdt)