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