arm64: head.S: remove unnecessary function alignment
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / head.S
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
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/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
37 /*
38  * swapper_pg_dir is the virtual address of the initial page table. We place
39  * the page tables 3 * PAGE_SIZE below KERNEL_RAM_VADDR. The idmap_pg_dir has
40  * 2 pages and is placed below swapper_pg_dir.
41  */
42 #define KERNEL_RAM_VADDR        (PAGE_OFFSET + TEXT_OFFSET)
43
44 #if (KERNEL_RAM_VADDR & 0xfffff) != 0x80000
45 #error KERNEL_RAM_VADDR must start at 0xXXX80000
46 #endif
47
48 #define SWAPPER_DIR_SIZE        (3 * PAGE_SIZE)
49 #define IDMAP_DIR_SIZE          (2 * PAGE_SIZE)
50
51         .globl  swapper_pg_dir
52         .equ    swapper_pg_dir, KERNEL_RAM_VADDR - SWAPPER_DIR_SIZE
53
54         .globl  idmap_pg_dir
55         .equ    idmap_pg_dir, swapper_pg_dir - IDMAP_DIR_SIZE
56
57         .macro  pgtbl, ttb0, ttb1, phys
58         add     \ttb1, \phys, #TEXT_OFFSET - SWAPPER_DIR_SIZE
59         sub     \ttb0, \ttb1, #IDMAP_DIR_SIZE
60         .endm
61
62 #ifdef CONFIG_ARM64_64K_PAGES
63 #define BLOCK_SHIFT     PAGE_SHIFT
64 #define BLOCK_SIZE      PAGE_SIZE
65 #else
66 #define BLOCK_SHIFT     SECTION_SHIFT
67 #define BLOCK_SIZE      SECTION_SIZE
68 #endif
69
70 #define KERNEL_START    KERNEL_RAM_VADDR
71 #define KERNEL_END      _end
72
73 /*
74  * Initial memory map attributes.
75  */
76 #ifndef CONFIG_SMP
77 #define PTE_FLAGS       PTE_TYPE_PAGE | PTE_AF
78 #define PMD_FLAGS       PMD_TYPE_SECT | PMD_SECT_AF
79 #else
80 #define PTE_FLAGS       PTE_TYPE_PAGE | PTE_AF | PTE_SHARED
81 #define PMD_FLAGS       PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S
82 #endif
83
84 #ifdef CONFIG_ARM64_64K_PAGES
85 #define MM_MMUFLAGS     PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
86 #else
87 #define MM_MMUFLAGS     PMD_ATTRINDX(MT_NORMAL) | 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         .quad   0                               // reserved
116         .quad   0                               // reserved
117         .quad   0                               // reserved
118         .byte   0x41                            // Magic number, "ARM\x64"
119         .byte   0x52
120         .byte   0x4d
121         .byte   0x64
122         .word   0                               // reserved
123
124 ENTRY(stext)
125         mov     x21, x0                         // x21=FDT
126         bl      __calc_phys_offset              // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
127         bl      el2_setup                       // Drop to EL1
128         mrs     x22, midr_el1                   // x22=cpuid
129         mov     x0, x22
130         bl      lookup_processor_type
131         mov     x23, x0                         // x23=current cpu_table
132         cbz     x23, __error_p                  // invalid processor (x23=0)?
133         bl      __vet_fdt
134         bl      __create_page_tables            // x25=TTBR0, x26=TTBR1
135         /*
136          * The following calls CPU specific code in a position independent
137          * manner. See arch/arm64/mm/proc.S for details. x23 = base of
138          * cpu_info structure selected by lookup_processor_type above.
139          * On return, the CPU will be ready for the MMU to be turned on and
140          * the TCR will have been set.
141          */
142         ldr     x27, __switch_data              // address to jump to after
143                                                 // MMU has been enabled
144         adr     lr, __enable_mmu                // return (PIC) address
145         ldr     x12, [x23, #CPU_INFO_SETUP]
146         add     x12, x12, x28                   // __virt_to_phys
147         br      x12                             // initialise processor
148 ENDPROC(stext)
149
150 /*
151  * If we're fortunate enough to boot at EL2, ensure that the world is
152  * sane before dropping to EL1.
153  */
154 ENTRY(el2_setup)
155         mrs     x0, CurrentEL
156         cmp     x0, #PSR_MODE_EL2t
157         ccmp    x0, #PSR_MODE_EL2h, #0x4, ne
158         ldr     x0, =__boot_cpu_mode            // Compute __boot_cpu_mode
159         add     x0, x0, x28
160         b.eq    1f
161         str     wzr, [x0]                       // Remember we don't have EL2...
162         ret
163
164         /* Hyp configuration. */
165 1:      ldr     w1, =BOOT_CPU_MODE_EL2
166         str     w1, [x0, #4]                    // This CPU has EL2
167         mov     x0, #(1 << 31)                  // 64-bit EL1
168         msr     hcr_el2, x0
169
170         /* Generic timers. */
171         mrs     x0, cnthctl_el2
172         orr     x0, x0, #3                      // Enable EL1 physical timers
173         msr     cnthctl_el2, x0
174         msr     cntvoff_el2, xzr                // Clear virtual offset
175
176         /* Populate ID registers. */
177         mrs     x0, midr_el1
178         mrs     x1, mpidr_el1
179         msr     vpidr_el2, x0
180         msr     vmpidr_el2, x1
181
182         /* sctlr_el1 */
183         mov     x0, #0x0800                     // Set/clear RES{1,0} bits
184         movk    x0, #0x30d0, lsl #16
185         msr     sctlr_el1, x0
186
187         /* Coprocessor traps. */
188         mov     x0, #0x33ff
189         msr     cptr_el2, x0                    // Disable copro. traps to EL2
190
191 #ifdef CONFIG_COMPAT
192         msr     hstr_el2, xzr                   // Disable CP15 traps to EL2
193 #endif
194
195         /* Stage-2 translation */
196         msr     vttbr_el2, xzr
197
198         /* Hypervisor stub */
199         adr     x0, __hyp_stub_vectors
200         msr     vbar_el2, x0
201
202         /* spsr */
203         mov     x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
204                       PSR_MODE_EL1h)
205         msr     spsr_el2, x0
206         msr     elr_el2, lr
207         eret
208 ENDPROC(el2_setup)
209
210 /*
211  * We need to find out the CPU boot mode long after boot, so we need to
212  * store it in a writable variable.
213  *
214  * This is not in .bss, because we set it sufficiently early that the boot-time
215  * zeroing of .bss would clobber it.
216  */
217         .pushsection    .data
218 ENTRY(__boot_cpu_mode)
219         .long   BOOT_CPU_MODE_EL2
220         .long   0
221         .popsection
222
223         .align  3
224 2:      .quad   .
225         .quad   PAGE_OFFSET
226
227 #ifdef CONFIG_SMP
228         .align  3
229 1:      .quad   .
230         .quad   secondary_holding_pen_release
231
232         /*
233          * This provides a "holding pen" for platforms to hold all secondary
234          * cores are held until we're ready for them to initialise.
235          */
236 ENTRY(secondary_holding_pen)
237         bl      __calc_phys_offset              // x24=phys offset
238         bl      el2_setup                       // Drop to EL1
239         mrs     x0, mpidr_el1
240         ldr     x1, =MPIDR_HWID_BITMASK
241         and     x0, x0, x1
242         adr     x1, 1b
243         ldp     x2, x3, [x1]
244         sub     x1, x1, x2
245         add     x3, x3, x1
246 pen:    ldr     x4, [x3]
247         cmp     x4, x0
248         b.eq    secondary_startup
249         wfe
250         b       pen
251 ENDPROC(secondary_holding_pen)
252
253         /*
254          * Secondary entry point that jumps straight into the kernel. Only to
255          * be used where CPUs are brought online dynamically by the kernel.
256          */
257 ENTRY(secondary_entry)
258         bl      __calc_phys_offset              // x2=phys offset
259         bl      el2_setup                       // Drop to EL1
260         b       secondary_startup
261 ENDPROC(secondary_entry)
262
263 ENTRY(secondary_startup)
264         /*
265          * Common entry point for secondary CPUs.
266          */
267         mrs     x22, midr_el1                   // x22=cpuid
268         mov     x0, x22
269         bl      lookup_processor_type
270         mov     x23, x0                         // x23=current cpu_table
271         cbz     x23, __error_p                  // invalid processor (x23=0)?
272
273         pgtbl   x25, x26, x24                   // x25=TTBR0, x26=TTBR1
274         ldr     x12, [x23, #CPU_INFO_SETUP]
275         add     x12, x12, x28                   // __virt_to_phys
276         blr     x12                             // initialise processor
277
278         ldr     x21, =secondary_data
279         ldr     x27, =__secondary_switched      // address to jump to after enabling the MMU
280         b       __enable_mmu
281 ENDPROC(secondary_startup)
282
283 ENTRY(__secondary_switched)
284         ldr     x0, [x21]                       // get secondary_data.stack
285         mov     sp, x0
286         mov     x29, #0
287         b       secondary_start_kernel
288 ENDPROC(__secondary_switched)
289 #endif  /* CONFIG_SMP */
290
291 /*
292  * Setup common bits before finally enabling the MMU. Essentially this is just
293  * loading the page table pointer and vector base registers.
294  *
295  * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
296  * the MMU.
297  */
298 __enable_mmu:
299         ldr     x5, =vectors
300         msr     vbar_el1, x5
301         msr     ttbr0_el1, x25                  // load TTBR0
302         msr     ttbr1_el1, x26                  // load TTBR1
303         isb
304         b       __turn_mmu_on
305 ENDPROC(__enable_mmu)
306
307 /*
308  * Enable the MMU. This completely changes the structure of the visible memory
309  * space. You will not be able to trace execution through this.
310  *
311  *  x0  = system control register
312  *  x27 = *virtual* address to jump to upon completion
313  *
314  * other registers depend on the function called upon completion
315  *
316  * We align the entire function to the smallest power of two larger than it to
317  * ensure it fits within a single block map entry. Otherwise were PHYS_OFFSET
318  * close to the end of a 512MB or 1GB block we might require an additional
319  * table to map the entire function.
320  */
321         .align  4
322 __turn_mmu_on:
323         msr     sctlr_el1, x0
324         isb
325         br      x27
326 ENDPROC(__turn_mmu_on)
327
328 /*
329  * Calculate the start of physical memory.
330  */
331 __calc_phys_offset:
332         adr     x0, 1f
333         ldp     x1, x2, [x0]
334         sub     x28, x0, x1                     // x28 = PHYS_OFFSET - PAGE_OFFSET
335         add     x24, x2, x28                    // x24 = PHYS_OFFSET
336         ret
337 ENDPROC(__calc_phys_offset)
338
339         .align 3
340 1:      .quad   .
341         .quad   PAGE_OFFSET
342
343 /*
344  * Macro to populate the PGD for the corresponding block entry in the next
345  * level (tbl) for the given virtual address.
346  *
347  * Preserves:   pgd, tbl, virt
348  * Corrupts:    tmp1, tmp2
349  */
350         .macro  create_pgd_entry, pgd, tbl, virt, tmp1, tmp2
351         lsr     \tmp1, \virt, #PGDIR_SHIFT
352         and     \tmp1, \tmp1, #PTRS_PER_PGD - 1 // PGD index
353         orr     \tmp2, \tbl, #3                 // PGD entry table type
354         str     \tmp2, [\pgd, \tmp1, lsl #3]
355         .endm
356
357 /*
358  * Macro to populate block entries in the page table for the start..end
359  * virtual range (inclusive).
360  *
361  * Preserves:   tbl, flags
362  * Corrupts:    phys, start, end, pstate
363  */
364         .macro  create_block_map, tbl, flags, phys, start, end
365         lsr     \phys, \phys, #BLOCK_SHIFT
366         lsr     \start, \start, #BLOCK_SHIFT
367         and     \start, \start, #PTRS_PER_PTE - 1       // table index
368         orr     \phys, \flags, \phys, lsl #BLOCK_SHIFT  // table entry
369         lsr     \end, \end, #BLOCK_SHIFT
370         and     \end, \end, #PTRS_PER_PTE - 1           // table end index
371 9999:   str     \phys, [\tbl, \start, lsl #3]           // store the entry
372         add     \start, \start, #1                      // next entry
373         add     \phys, \phys, #BLOCK_SIZE               // next block
374         cmp     \start, \end
375         b.ls    9999b
376         .endm
377
378 /*
379  * Setup the initial page tables. We only setup the barest amount which is
380  * required to get the kernel running. The following sections are required:
381  *   - identity mapping to enable the MMU (low address, TTBR0)
382  *   - first few MB of the kernel linear mapping to jump to once the MMU has
383  *     been enabled, including the FDT blob (TTBR1)
384  *   - pgd entry for fixed mappings (TTBR1)
385  */
386 __create_page_tables:
387         pgtbl   x25, x26, x24                   // idmap_pg_dir and swapper_pg_dir addresses
388
389         /*
390          * Clear the idmap and swapper page tables.
391          */
392         mov     x0, x25
393         add     x6, x26, #SWAPPER_DIR_SIZE
394 1:      stp     xzr, xzr, [x0], #16
395         stp     xzr, xzr, [x0], #16
396         stp     xzr, xzr, [x0], #16
397         stp     xzr, xzr, [x0], #16
398         cmp     x0, x6
399         b.lo    1b
400
401         ldr     x7, =MM_MMUFLAGS
402
403         /*
404          * Create the identity mapping.
405          */
406         add     x0, x25, #PAGE_SIZE             // section table address
407         ldr     x3, =KERNEL_START
408         add     x3, x3, x28                     // __pa(KERNEL_START)
409         create_pgd_entry x25, x0, x3, x5, x6
410         ldr     x6, =KERNEL_END
411         mov     x5, x3                          // __pa(KERNEL_START)
412         add     x6, x6, x28                     // __pa(KERNEL_END)
413         create_block_map x0, x7, x3, x5, x6
414
415         /*
416          * Map the kernel image (starting with PHYS_OFFSET).
417          */
418         add     x0, x26, #PAGE_SIZE             // section table address
419         mov     x5, #PAGE_OFFSET
420         create_pgd_entry x26, x0, x5, x3, x6
421         ldr     x6, =KERNEL_END
422         mov     x3, x24                         // phys offset
423         create_block_map x0, x7, x3, x5, x6
424
425         /*
426          * Map the FDT blob (maximum 2MB; must be within 512MB of
427          * PHYS_OFFSET).
428          */
429         mov     x3, x21                         // FDT phys address
430         and     x3, x3, #~((1 << 21) - 1)       // 2MB aligned
431         mov     x6, #PAGE_OFFSET
432         sub     x5, x3, x24                     // subtract PHYS_OFFSET
433         tst     x5, #~((1 << 29) - 1)           // within 512MB?
434         csel    x21, xzr, x21, ne               // zero the FDT pointer
435         b.ne    1f
436         add     x5, x5, x6                      // __va(FDT blob)
437         add     x6, x5, #1 << 21                // 2MB for the FDT blob
438         sub     x6, x6, #1                      // inclusive range
439         create_block_map x0, x7, x3, x5, x6
440 1:
441         /*
442          * Create the pgd entry for the fixed mappings.
443          */
444         ldr     x5, =FIXADDR_TOP                // Fixed mapping virtual address
445         add     x0, x26, #2 * PAGE_SIZE         // section table address
446         create_pgd_entry x26, x0, x5, x6, x7
447         ret
448 ENDPROC(__create_page_tables)
449         .ltorg
450
451         .align  3
452         .type   __switch_data, %object
453 __switch_data:
454         .quad   __mmap_switched
455         .quad   __bss_start                     // x6
456         .quad   _end                            // x7
457         .quad   processor_id                    // x4
458         .quad   __fdt_pointer                   // x5
459         .quad   memstart_addr                   // x6
460         .quad   init_thread_union + THREAD_START_SP // sp
461
462 /*
463  * The following fragment of code is executed with the MMU on in MMU mode, and
464  * uses absolute addresses; this is not position independent.
465  */
466 __mmap_switched:
467         adr     x3, __switch_data + 8
468
469         ldp     x6, x7, [x3], #16
470 1:      cmp     x6, x7
471         b.hs    2f
472         str     xzr, [x6], #8                   // Clear BSS
473         b       1b
474 2:
475         ldp     x4, x5, [x3], #16
476         ldr     x6, [x3], #8
477         ldr     x16, [x3]
478         mov     sp, x16
479         str     x22, [x4]                       // Save processor ID
480         str     x21, [x5]                       // Save FDT pointer
481         str     x24, [x6]                       // Save PHYS_OFFSET
482         mov     x29, #0
483         b       start_kernel
484 ENDPROC(__mmap_switched)
485
486 /*
487  * Exception handling. Something went wrong and we can't proceed. We ought to
488  * tell the user, but since we don't have any guarantee that we're even
489  * running on the right architecture, we do virtually nothing.
490  */
491 __error_p:
492 ENDPROC(__error_p)
493
494 __error:
495 1:      nop
496         b       1b
497 ENDPROC(__error)
498
499 /*
500  * This function gets the processor ID in w0 and searches the cpu_table[] for
501  * a match. It returns a pointer to the struct cpu_info it found. The
502  * cpu_table[] must end with an empty (all zeros) structure.
503  *
504  * This routine can be called via C code and it needs to work with the MMU
505  * both disabled and enabled (the offset is calculated automatically).
506  */
507 ENTRY(lookup_processor_type)
508         adr     x1, __lookup_processor_type_data
509         ldp     x2, x3, [x1]
510         sub     x1, x1, x2                      // get offset between VA and PA
511         add     x3, x3, x1                      // convert VA to PA
512 1:
513         ldp     w5, w6, [x3]                    // load cpu_id_val and cpu_id_mask
514         cbz     w5, 2f                          // end of list?
515         and     w6, w6, w0
516         cmp     w5, w6
517         b.eq    3f
518         add     x3, x3, #CPU_INFO_SZ
519         b       1b
520 2:
521         mov     x3, #0                          // unknown processor
522 3:
523         mov     x0, x3
524         ret
525 ENDPROC(lookup_processor_type)
526
527         .align  3
528         .type   __lookup_processor_type_data, %object
529 __lookup_processor_type_data:
530         .quad   .
531         .quad   cpu_table
532         .size   __lookup_processor_type_data, . - __lookup_processor_type_data
533
534 /*
535  * Determine validity of the x21 FDT pointer.
536  * The dtb must be 8-byte aligned and live in the first 512M of memory.
537  */
538 __vet_fdt:
539         tst     x21, #0x7
540         b.ne    1f
541         cmp     x21, x24
542         b.lt    1f
543         mov     x0, #(1 << 29)
544         add     x0, x0, x24
545         cmp     x21, x0
546         b.ge    1f
547         ret
548 1:
549         mov     x21, #0
550         ret
551 ENDPROC(__vet_fdt)