Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 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> |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 29 | #include <asm/cache.h> |
Javi Merino | 0359b0e | 2012-08-29 18:32:18 +0100 | [diff] [blame] | 30 | #include <asm/cputype.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 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> |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 36 | #include <asm/virt.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 37 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 38 | #define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET) |
| 39 | |
Mark Rutland | da57a36 | 2014-06-24 16:51:37 +0100 | [diff] [blame] | 40 | #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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame] | 48 | .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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 53 | .endm |
| 54 | |
| 55 | #ifdef CONFIG_ARM64_64K_PAGES |
| 56 | #define BLOCK_SHIFT PAGE_SHIFT |
| 57 | #define BLOCK_SIZE PAGE_SIZE |
Catalin Marinas | 383c279 | 2014-07-21 15:54:50 +0100 | [diff] [blame^] | 58 | #define TABLE_SHIFT PMD_SHIFT |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 59 | #else |
| 60 | #define BLOCK_SHIFT SECTION_SHIFT |
| 61 | #define BLOCK_SIZE SECTION_SIZE |
Catalin Marinas | 383c279 | 2014-07-21 15:54:50 +0100 | [diff] [blame^] | 62 | #define TABLE_SHIFT PUD_SHIFT |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 63 | #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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 81 | #else |
| 82 | #define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 83 | #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 Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 105 | #ifdef CONFIG_EFI |
| 106 | efi_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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 114 | b stext // branch to kernel start, magic |
| 115 | .long 0 // reserved |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 116 | #endif |
Mark Rutland | a2c1d73 | 2014-06-24 16:51:36 +0100 | [diff] [blame] | 117 | .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 Franz | 4370eec | 2013-08-15 00:10:00 +0100 | [diff] [blame] | 120 | .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 Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 127 | #ifdef CONFIG_EFI |
| 128 | .long pe_header - efi_head // Offset to the PE header. |
| 129 | #else |
Roy Franz | 4370eec | 2013-08-15 00:10:00 +0100 | [diff] [blame] | 130 | .word 0 // reserved |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | #ifdef CONFIG_EFI |
| 134 | .align 3 |
| 135 | pe_header: |
| 136 | .ascii "PE" |
| 137 | .short 0 |
| 138 | coff_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 |
| 149 | optional_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 | |
| 159 | extra_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 |
| 193 | section_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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 230 | |
| 231 | ENTRY(stext) |
| 232 | mov x21, x0 // x21=FDT |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 233 | bl el2_setup // Drop to EL1, w20=cpu_boot_mode |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 234 | bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 235 | bl set_cpu_boot_mode_flag |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 236 | 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 241 | 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 |
| 256 | ENDPROC(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 Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 261 | * |
| 262 | * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x20 if |
| 263 | * booted in EL1 or EL2 respectively. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 264 | */ |
| 265 | ENTRY(el2_setup) |
| 266 | mrs x0, CurrentEL |
Marc Zyngier | 974c8e4 | 2014-06-06 14:16:21 +0100 | [diff] [blame] | 267 | cmp x0, #CurrentEL_EL2 |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 268 | b.ne 1f |
| 269 | mrs x0, sctlr_el2 |
| 270 | CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2 |
| 271 | CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2 |
| 272 | msr sctlr_el2, x0 |
| 273 | b 2f |
| 274 | 1: mrs x0, sctlr_el1 |
| 275 | CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1 |
| 276 | CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1 |
| 277 | msr sctlr_el1, x0 |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 278 | mov w20, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1 |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 279 | isb |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 280 | ret |
| 281 | |
| 282 | /* Hyp configuration. */ |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 283 | 2: mov x0, #(1 << 31) // 64-bit EL1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 284 | 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 Deacon | 1f75ff0 | 2012-11-29 22:48:31 +0000 | [diff] [blame] | 290 | msr cntvoff_el2, xzr // Clear virtual offset |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 291 | |
| 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 Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 300 | CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems |
| 301 | CPU_LE( movk x0, #0x30d0, lsl #16 ) // Clear EE and E0E on LE systems |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 302 | 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 Zyngier | 7dbfbe5 | 2012-11-06 19:27:59 +0000 | [diff] [blame] | 312 | /* Stage-2 translation */ |
| 313 | msr vttbr_el2, xzr |
| 314 | |
Marc Zyngier | 712c6ff | 2012-10-19 17:46:27 +0100 | [diff] [blame] | 315 | /* Hypervisor stub */ |
| 316 | adr x0, __hyp_stub_vectors |
| 317 | msr vbar_el2, x0 |
| 318 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 319 | /* 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 Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 324 | mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 325 | eret |
| 326 | ENDPROC(el2_setup) |
| 327 | |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 328 | /* |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 329 | * 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 | */ |
| 332 | ENTRY(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 Deacon | d048859 | 2014-05-02 16:24:13 +0100 | [diff] [blame] | 338 | 1: str w20, [x1] // This CPU has booted in EL1 |
| 339 | dmb sy |
| 340 | dc ivac, x1 // Invalidate potentially stale cache line |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 341 | ret |
| 342 | ENDPROC(set_cpu_boot_mode_flag) |
| 343 | |
| 344 | /* |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 345 | * 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 Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 351 | .pushsection .data..cacheline_aligned |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 352 | ENTRY(__boot_cpu_mode) |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 353 | .align L1_CACHE_SHIFT |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 354 | .long BOOT_CPU_MODE_EL2 |
| 355 | .long 0 |
| 356 | .popsection |
| 357 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 358 | .align 3 |
| 359 | 2: .quad . |
| 360 | .quad PAGE_OFFSET |
| 361 | |
| 362 | #ifdef CONFIG_SMP |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 363 | .align 3 |
| 364 | 1: .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 | */ |
| 371 | ENTRY(secondary_holding_pen) |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 372 | 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 375 | mrs x0, mpidr_el1 |
Javi Merino | 0359b0e | 2012-08-29 18:32:18 +0100 | [diff] [blame] | 376 | ldr x1, =MPIDR_HWID_BITMASK |
| 377 | and x0, x0, x1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 378 | adr x1, 1b |
| 379 | ldp x2, x3, [x1] |
| 380 | sub x1, x1, x2 |
| 381 | add x3, x3, x1 |
| 382 | pen: ldr x4, [x3] |
| 383 | cmp x4, x0 |
| 384 | b.eq secondary_startup |
| 385 | wfe |
| 386 | b pen |
| 387 | ENDPROC(secondary_holding_pen) |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 388 | |
| 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 | */ |
| 393 | ENTRY(secondary_entry) |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 394 | bl el2_setup // Drop to EL1 |
Lorenzo Pieralisi | 85cc00e | 2013-11-18 18:56:42 +0000 | [diff] [blame] | 395 | bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET |
| 396 | bl set_cpu_boot_mode_flag |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 397 | b secondary_startup |
| 398 | ENDPROC(secondary_entry) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 399 | |
| 400 | ENTRY(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 Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame] | 410 | pgtbl x25, x26, x28 // x25=TTBR0, x26=TTBR1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 411 | 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 |
| 418 | ENDPROC(secondary_startup) |
| 419 | |
| 420 | ENTRY(__secondary_switched) |
| 421 | ldr x0, [x21] // get secondary_data.stack |
| 422 | mov sp, x0 |
| 423 | mov x29, #0 |
| 424 | b secondary_start_kernel |
| 425 | ENDPROC(__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 |
| 442 | ENDPROC(__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 Rutland | 909a406 | 2014-06-24 16:51:34 +0100 | [diff] [blame] | 452 | * |
| 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 457 | */ |
Mark Rutland | 909a406 | 2014-06-24 16:51:34 +0100 | [diff] [blame] | 458 | .align 4 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 459 | __turn_mmu_on: |
| 460 | msr sctlr_el1, x0 |
| 461 | isb |
| 462 | br x27 |
| 463 | ENDPROC(__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 |
| 474 | ENDPROC(__calc_phys_offset) |
| 475 | |
| 476 | .align 3 |
| 477 | 1: .quad . |
| 478 | .quad PAGE_OFFSET |
| 479 | |
| 480 | /* |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 481 | * Macro to create a table entry to the next page. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 482 | * |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 483 | * 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 491 | */ |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 492 | .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 Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 499 | .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 Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 505 | * Preserves: tbl, next, virt |
| 506 | * Corrupts: tmp1, tmp2 |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 507 | */ |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 508 | .macro create_pgd_entry, tbl, virt, tmp1, tmp2 |
| 509 | create_table_entry \tbl, \virt, PGDIR_SHIFT, PTRS_PER_PGD, \tmp1, \tmp2 |
Catalin Marinas | 383c279 | 2014-07-21 15:54:50 +0100 | [diff] [blame^] | 510 | #if SWAPPER_PGTABLE_LEVELS == 3 |
| 511 | create_table_entry \tbl, \virt, TABLE_SHIFT, PTRS_PER_PTE, \tmp1, \tmp2 |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 512 | #endif |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 513 | .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 Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 522 | .macro create_block_map, tbl, flags, phys, start, end |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 523 | lsr \phys, \phys, #BLOCK_SHIFT |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 524 | lsr \start, \start, #BLOCK_SHIFT |
| 525 | and \start, \start, #PTRS_PER_PTE - 1 // table index |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 526 | orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 527 | lsr \end, \end, #BLOCK_SHIFT |
| 528 | and \end, \end, #PTRS_PER_PTE - 1 // table end index |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 529 | 9999: str \phys, [\tbl, \start, lsl #3] // store the entry |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 530 | add \start, \start, #1 // next entry |
| 531 | add \phys, \phys, #BLOCK_SIZE // next block |
| 532 | cmp \start, \end |
| 533 | b.ls 9999b |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 534 | .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 Salter | bf4b558 | 2014-04-07 15:39:52 -0700 | [diff] [blame] | 542 | * - pgd entry for fixed mappings (TTBR1) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 543 | */ |
| 544 | __create_page_tables: |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame] | 545 | pgtbl x25, x26, x28 // idmap_pg_dir and swapper_pg_dir addresses |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 546 | 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 555 | |
| 556 | /* |
| 557 | * Clear the idmap and swapper page tables. |
| 558 | */ |
| 559 | mov x0, x25 |
| 560 | add x6, x26, #SWAPPER_DIR_SIZE |
| 561 | 1: 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 Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 573 | mov x0, x25 // idmap_pg_dir |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 574 | ldr x3, =KERNEL_START |
| 575 | add x3, x3, x28 // __pa(KERNEL_START) |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 576 | create_pgd_entry x0, x3, x5, x6 |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 577 | 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 581 | |
| 582 | /* |
| 583 | * Map the kernel image (starting with PHYS_OFFSET). |
| 584 | */ |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 585 | mov x0, x26 // swapper_pg_dir |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 586 | mov x5, #PAGE_OFFSET |
Catalin Marinas | b4a0d8b | 2014-07-16 12:10:33 +0100 | [diff] [blame] | 587 | create_pgd_entry x0, x5, x3, x6 |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 588 | ldr x6, =KERNEL_END |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 589 | 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 |
| 607 | 1: |
Catalin Marinas | 2475ff9 | 2012-10-23 14:55:08 +0100 | [diff] [blame] | 608 | /* |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 609 | * 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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 618 | ret |
| 619 | ENDPROC(__create_page_tables) |
| 620 | .ltorg |
| 621 | |
| 622 | .align 3 |
| 623 | .type __switch_data, %object |
| 624 | __switch_data: |
| 625 | .quad __mmap_switched |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 626 | .quad __bss_start // x6 |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame] | 627 | .quad __bss_stop // x7 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 628 | .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 Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 640 | ldp x6, x7, [x3], #16 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 641 | 1: cmp x6, x7 |
| 642 | b.hs 2f |
| 643 | str xzr, [x6], #8 // Clear BSS |
| 644 | b 1b |
| 645 | 2: |
| 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 |
| 655 | ENDPROC(__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: |
| 663 | ENDPROC(__error_p) |
| 664 | |
| 665 | __error: |
| 666 | 1: nop |
| 667 | b 1b |
| 668 | ENDPROC(__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 | */ |
| 678 | ENTRY(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 |
| 683 | 1: |
| 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 |
| 691 | 2: |
| 692 | mov x3, #0 // unknown processor |
| 693 | 3: |
| 694 | mov x0, x3 |
| 695 | ret |
| 696 | ENDPROC(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 |
| 719 | 1: |
| 720 | mov x21, #0 |
| 721 | ret |
| 722 | ENDPROC(__vet_fdt) |