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 | |
| 40 | #if (KERNEL_RAM_VADDR & 0xfffff) != 0x80000 |
| 41 | #error KERNEL_RAM_VADDR must start at 0xXXX80000 |
| 42 | #endif |
| 43 | |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame^] | 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 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 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 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 75 | #else |
| 76 | #define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 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 | */ |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 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 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 108 | b stext // branch to kernel start, magic |
| 109 | .long 0 // reserved |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 110 | #endif |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 111 | .quad TEXT_OFFSET // Image load offset from start of RAM |
| 112 | .quad 0 // reserved |
| 113 | .quad 0 // reserved |
Roy Franz | 4370eec | 2013-08-15 00:10:00 +0100 | [diff] [blame] | 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 |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 121 | #ifdef CONFIG_EFI |
| 122 | .long pe_header - efi_head // Offset to the PE header. |
| 123 | #else |
Roy Franz | 4370eec | 2013-08-15 00:10:00 +0100 | [diff] [blame] | 124 | .word 0 // reserved |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 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 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 224 | |
| 225 | ENTRY(stext) |
| 226 | mov x21, x0 // x21=FDT |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 227 | bl el2_setup // Drop to EL1, w20=cpu_boot_mode |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 228 | bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 229 | bl set_cpu_boot_mode_flag |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 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)? |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 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. |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 255 | * |
| 256 | * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x20 if |
| 257 | * booted in EL1 or EL2 respectively. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 258 | */ |
| 259 | ENTRY(el2_setup) |
| 260 | mrs x0, CurrentEL |
Marc Zyngier | 974c8e4 | 2014-06-06 14:16:21 +0100 | [diff] [blame] | 261 | cmp x0, #CurrentEL_EL2 |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 262 | b.ne 1f |
| 263 | mrs x0, sctlr_el2 |
| 264 | CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2 |
| 265 | CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2 |
| 266 | msr sctlr_el2, x0 |
| 267 | b 2f |
| 268 | 1: mrs x0, sctlr_el1 |
| 269 | CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1 |
| 270 | CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1 |
| 271 | msr sctlr_el1, x0 |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 272 | mov w20, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1 |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 273 | isb |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 274 | ret |
| 275 | |
| 276 | /* Hyp configuration. */ |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 277 | 2: mov x0, #(1 << 31) // 64-bit EL1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 278 | msr hcr_el2, x0 |
| 279 | |
| 280 | /* Generic timers. */ |
| 281 | mrs x0, cnthctl_el2 |
| 282 | orr x0, x0, #3 // Enable EL1 physical timers |
| 283 | msr cnthctl_el2, x0 |
Will Deacon | 1f75ff0 | 2012-11-29 22:48:31 +0000 | [diff] [blame] | 284 | msr cntvoff_el2, xzr // Clear virtual offset |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 285 | |
| 286 | /* Populate ID registers. */ |
| 287 | mrs x0, midr_el1 |
| 288 | mrs x1, mpidr_el1 |
| 289 | msr vpidr_el2, x0 |
| 290 | msr vmpidr_el2, x1 |
| 291 | |
| 292 | /* sctlr_el1 */ |
| 293 | mov x0, #0x0800 // Set/clear RES{1,0} bits |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 294 | CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems |
| 295 | 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] | 296 | msr sctlr_el1, x0 |
| 297 | |
| 298 | /* Coprocessor traps. */ |
| 299 | mov x0, #0x33ff |
| 300 | msr cptr_el2, x0 // Disable copro. traps to EL2 |
| 301 | |
| 302 | #ifdef CONFIG_COMPAT |
| 303 | msr hstr_el2, xzr // Disable CP15 traps to EL2 |
| 304 | #endif |
| 305 | |
Marc Zyngier | 7dbfbe5 | 2012-11-06 19:27:59 +0000 | [diff] [blame] | 306 | /* Stage-2 translation */ |
| 307 | msr vttbr_el2, xzr |
| 308 | |
Marc Zyngier | 712c6ff | 2012-10-19 17:46:27 +0100 | [diff] [blame] | 309 | /* Hypervisor stub */ |
| 310 | adr x0, __hyp_stub_vectors |
| 311 | msr vbar_el2, x0 |
| 312 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 313 | /* spsr */ |
| 314 | mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\ |
| 315 | PSR_MODE_EL1h) |
| 316 | msr spsr_el2, x0 |
| 317 | msr elr_el2, lr |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 318 | mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 319 | eret |
| 320 | ENDPROC(el2_setup) |
| 321 | |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 322 | /* |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 323 | * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed |
| 324 | * in x20. See arch/arm64/include/asm/virt.h for more info. |
| 325 | */ |
| 326 | ENTRY(set_cpu_boot_mode_flag) |
| 327 | ldr x1, =__boot_cpu_mode // Compute __boot_cpu_mode |
| 328 | add x1, x1, x28 |
| 329 | cmp w20, #BOOT_CPU_MODE_EL2 |
| 330 | b.ne 1f |
| 331 | add x1, x1, #4 |
Will Deacon | d048859 | 2014-05-02 16:24:13 +0100 | [diff] [blame] | 332 | 1: str w20, [x1] // This CPU has booted in EL1 |
| 333 | dmb sy |
| 334 | dc ivac, x1 // Invalidate potentially stale cache line |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 335 | ret |
| 336 | ENDPROC(set_cpu_boot_mode_flag) |
| 337 | |
| 338 | /* |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 339 | * We need to find out the CPU boot mode long after boot, so we need to |
| 340 | * store it in a writable variable. |
| 341 | * |
| 342 | * This is not in .bss, because we set it sufficiently early that the boot-time |
| 343 | * zeroing of .bss would clobber it. |
| 344 | */ |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 345 | .pushsection .data..cacheline_aligned |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 346 | ENTRY(__boot_cpu_mode) |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 347 | .align L1_CACHE_SHIFT |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 348 | .long BOOT_CPU_MODE_EL2 |
| 349 | .long 0 |
| 350 | .popsection |
| 351 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 352 | .align 3 |
| 353 | 2: .quad . |
| 354 | .quad PAGE_OFFSET |
| 355 | |
| 356 | #ifdef CONFIG_SMP |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 357 | .align 3 |
| 358 | 1: .quad . |
| 359 | .quad secondary_holding_pen_release |
| 360 | |
| 361 | /* |
| 362 | * This provides a "holding pen" for platforms to hold all secondary |
| 363 | * cores are held until we're ready for them to initialise. |
| 364 | */ |
| 365 | ENTRY(secondary_holding_pen) |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 366 | bl el2_setup // Drop to EL1, w20=cpu_boot_mode |
| 367 | bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET |
| 368 | bl set_cpu_boot_mode_flag |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 369 | mrs x0, mpidr_el1 |
Javi Merino | 0359b0e | 2012-08-29 18:32:18 +0100 | [diff] [blame] | 370 | ldr x1, =MPIDR_HWID_BITMASK |
| 371 | and x0, x0, x1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 372 | adr x1, 1b |
| 373 | ldp x2, x3, [x1] |
| 374 | sub x1, x1, x2 |
| 375 | add x3, x3, x1 |
| 376 | pen: ldr x4, [x3] |
| 377 | cmp x4, x0 |
| 378 | b.eq secondary_startup |
| 379 | wfe |
| 380 | b pen |
| 381 | ENDPROC(secondary_holding_pen) |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 382 | |
| 383 | /* |
| 384 | * Secondary entry point that jumps straight into the kernel. Only to |
| 385 | * be used where CPUs are brought online dynamically by the kernel. |
| 386 | */ |
| 387 | ENTRY(secondary_entry) |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 388 | bl el2_setup // Drop to EL1 |
Lorenzo Pieralisi | 85cc00e | 2013-11-18 18:56:42 +0000 | [diff] [blame] | 389 | bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET |
| 390 | bl set_cpu_boot_mode_flag |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 391 | b secondary_startup |
| 392 | ENDPROC(secondary_entry) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 393 | |
| 394 | ENTRY(secondary_startup) |
| 395 | /* |
| 396 | * Common entry point for secondary CPUs. |
| 397 | */ |
| 398 | mrs x22, midr_el1 // x22=cpuid |
| 399 | mov x0, x22 |
| 400 | bl lookup_processor_type |
| 401 | mov x23, x0 // x23=current cpu_table |
| 402 | cbz x23, __error_p // invalid processor (x23=0)? |
| 403 | |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame^] | 404 | pgtbl x25, x26, x28 // x25=TTBR0, x26=TTBR1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 405 | ldr x12, [x23, #CPU_INFO_SETUP] |
| 406 | add x12, x12, x28 // __virt_to_phys |
| 407 | blr x12 // initialise processor |
| 408 | |
| 409 | ldr x21, =secondary_data |
| 410 | ldr x27, =__secondary_switched // address to jump to after enabling the MMU |
| 411 | b __enable_mmu |
| 412 | ENDPROC(secondary_startup) |
| 413 | |
| 414 | ENTRY(__secondary_switched) |
| 415 | ldr x0, [x21] // get secondary_data.stack |
| 416 | mov sp, x0 |
| 417 | mov x29, #0 |
| 418 | b secondary_start_kernel |
| 419 | ENDPROC(__secondary_switched) |
| 420 | #endif /* CONFIG_SMP */ |
| 421 | |
| 422 | /* |
| 423 | * Setup common bits before finally enabling the MMU. Essentially this is just |
| 424 | * loading the page table pointer and vector base registers. |
| 425 | * |
| 426 | * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on |
| 427 | * the MMU. |
| 428 | */ |
| 429 | __enable_mmu: |
| 430 | ldr x5, =vectors |
| 431 | msr vbar_el1, x5 |
| 432 | msr ttbr0_el1, x25 // load TTBR0 |
| 433 | msr ttbr1_el1, x26 // load TTBR1 |
| 434 | isb |
| 435 | b __turn_mmu_on |
| 436 | ENDPROC(__enable_mmu) |
| 437 | |
| 438 | /* |
| 439 | * Enable the MMU. This completely changes the structure of the visible memory |
| 440 | * space. You will not be able to trace execution through this. |
| 441 | * |
| 442 | * x0 = system control register |
| 443 | * x27 = *virtual* address to jump to upon completion |
| 444 | * |
| 445 | * other registers depend on the function called upon completion |
Mark Rutland | 909a406 | 2014-06-24 16:51:34 +0100 | [diff] [blame] | 446 | * |
| 447 | * We align the entire function to the smallest power of two larger than it to |
| 448 | * ensure it fits within a single block map entry. Otherwise were PHYS_OFFSET |
| 449 | * close to the end of a 512MB or 1GB block we might require an additional |
| 450 | * table to map the entire function. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 451 | */ |
Mark Rutland | 909a406 | 2014-06-24 16:51:34 +0100 | [diff] [blame] | 452 | .align 4 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 453 | __turn_mmu_on: |
| 454 | msr sctlr_el1, x0 |
| 455 | isb |
| 456 | br x27 |
| 457 | ENDPROC(__turn_mmu_on) |
| 458 | |
| 459 | /* |
| 460 | * Calculate the start of physical memory. |
| 461 | */ |
| 462 | __calc_phys_offset: |
| 463 | adr x0, 1f |
| 464 | ldp x1, x2, [x0] |
| 465 | sub x28, x0, x1 // x28 = PHYS_OFFSET - PAGE_OFFSET |
| 466 | add x24, x2, x28 // x24 = PHYS_OFFSET |
| 467 | ret |
| 468 | ENDPROC(__calc_phys_offset) |
| 469 | |
| 470 | .align 3 |
| 471 | 1: .quad . |
| 472 | .quad PAGE_OFFSET |
| 473 | |
| 474 | /* |
| 475 | * Macro to populate the PGD for the corresponding block entry in the next |
| 476 | * level (tbl) for the given virtual address. |
| 477 | * |
| 478 | * Preserves: pgd, tbl, virt |
| 479 | * Corrupts: tmp1, tmp2 |
| 480 | */ |
| 481 | .macro create_pgd_entry, pgd, tbl, virt, tmp1, tmp2 |
| 482 | lsr \tmp1, \virt, #PGDIR_SHIFT |
| 483 | and \tmp1, \tmp1, #PTRS_PER_PGD - 1 // PGD index |
| 484 | orr \tmp2, \tbl, #3 // PGD entry table type |
| 485 | str \tmp2, [\pgd, \tmp1, lsl #3] |
| 486 | .endm |
| 487 | |
| 488 | /* |
| 489 | * Macro to populate block entries in the page table for the start..end |
| 490 | * virtual range (inclusive). |
| 491 | * |
| 492 | * Preserves: tbl, flags |
| 493 | * Corrupts: phys, start, end, pstate |
| 494 | */ |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 495 | .macro create_block_map, tbl, flags, phys, start, end |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 496 | lsr \phys, \phys, #BLOCK_SHIFT |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 497 | lsr \start, \start, #BLOCK_SHIFT |
| 498 | and \start, \start, #PTRS_PER_PTE - 1 // table index |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 499 | orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 500 | lsr \end, \end, #BLOCK_SHIFT |
| 501 | and \end, \end, #PTRS_PER_PTE - 1 // table end index |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 502 | 9999: str \phys, [\tbl, \start, lsl #3] // store the entry |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 503 | add \start, \start, #1 // next entry |
| 504 | add \phys, \phys, #BLOCK_SIZE // next block |
| 505 | cmp \start, \end |
| 506 | b.ls 9999b |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 507 | .endm |
| 508 | |
| 509 | /* |
| 510 | * Setup the initial page tables. We only setup the barest amount which is |
| 511 | * required to get the kernel running. The following sections are required: |
| 512 | * - identity mapping to enable the MMU (low address, TTBR0) |
| 513 | * - first few MB of the kernel linear mapping to jump to once the MMU has |
| 514 | * been enabled, including the FDT blob (TTBR1) |
Mark Salter | bf4b558 | 2014-04-07 15:39:52 -0700 | [diff] [blame] | 515 | * - pgd entry for fixed mappings (TTBR1) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 516 | */ |
| 517 | __create_page_tables: |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame^] | 518 | pgtbl x25, x26, x28 // idmap_pg_dir and swapper_pg_dir addresses |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 519 | mov x27, lr |
| 520 | |
| 521 | /* |
| 522 | * Invalidate the idmap and swapper page tables to avoid potential |
| 523 | * dirty cache lines being evicted. |
| 524 | */ |
| 525 | mov x0, x25 |
| 526 | add x1, x26, #SWAPPER_DIR_SIZE |
| 527 | bl __inval_cache_range |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * Clear the idmap and swapper page tables. |
| 531 | */ |
| 532 | mov x0, x25 |
| 533 | add x6, x26, #SWAPPER_DIR_SIZE |
| 534 | 1: stp xzr, xzr, [x0], #16 |
| 535 | stp xzr, xzr, [x0], #16 |
| 536 | stp xzr, xzr, [x0], #16 |
| 537 | stp xzr, xzr, [x0], #16 |
| 538 | cmp x0, x6 |
| 539 | b.lo 1b |
| 540 | |
| 541 | ldr x7, =MM_MMUFLAGS |
| 542 | |
| 543 | /* |
| 544 | * Create the identity mapping. |
| 545 | */ |
| 546 | add x0, x25, #PAGE_SIZE // section table address |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 547 | ldr x3, =KERNEL_START |
| 548 | add x3, x3, x28 // __pa(KERNEL_START) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 549 | create_pgd_entry x25, x0, x3, x5, x6 |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 550 | ldr x6, =KERNEL_END |
| 551 | mov x5, x3 // __pa(KERNEL_START) |
| 552 | add x6, x6, x28 // __pa(KERNEL_END) |
| 553 | create_block_map x0, x7, x3, x5, x6 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * Map the kernel image (starting with PHYS_OFFSET). |
| 557 | */ |
| 558 | add x0, x26, #PAGE_SIZE // section table address |
| 559 | mov x5, #PAGE_OFFSET |
| 560 | create_pgd_entry x26, x0, x5, x3, x6 |
Catalin Marinas | ea8c2e1 | 2014-02-17 12:03:25 +0000 | [diff] [blame] | 561 | ldr x6, =KERNEL_END |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 562 | mov x3, x24 // phys offset |
| 563 | create_block_map x0, x7, x3, x5, x6 |
| 564 | |
| 565 | /* |
| 566 | * Map the FDT blob (maximum 2MB; must be within 512MB of |
| 567 | * PHYS_OFFSET). |
| 568 | */ |
| 569 | mov x3, x21 // FDT phys address |
| 570 | and x3, x3, #~((1 << 21) - 1) // 2MB aligned |
| 571 | mov x6, #PAGE_OFFSET |
| 572 | sub x5, x3, x24 // subtract PHYS_OFFSET |
| 573 | tst x5, #~((1 << 29) - 1) // within 512MB? |
| 574 | csel x21, xzr, x21, ne // zero the FDT pointer |
| 575 | b.ne 1f |
| 576 | add x5, x5, x6 // __va(FDT blob) |
| 577 | add x6, x5, #1 << 21 // 2MB for the FDT blob |
| 578 | sub x6, x6, #1 // inclusive range |
| 579 | create_block_map x0, x7, x3, x5, x6 |
| 580 | 1: |
Catalin Marinas | 2475ff9 | 2012-10-23 14:55:08 +0100 | [diff] [blame] | 581 | /* |
Mark Salter | bf4b558 | 2014-04-07 15:39:52 -0700 | [diff] [blame] | 582 | * Create the pgd entry for the fixed mappings. |
Catalin Marinas | 2475ff9 | 2012-10-23 14:55:08 +0100 | [diff] [blame] | 583 | */ |
Mark Salter | bf4b558 | 2014-04-07 15:39:52 -0700 | [diff] [blame] | 584 | ldr x5, =FIXADDR_TOP // Fixed mapping virtual address |
Catalin Marinas | 2475ff9 | 2012-10-23 14:55:08 +0100 | [diff] [blame] | 585 | add x0, x26, #2 * PAGE_SIZE // section table address |
| 586 | create_pgd_entry x26, x0, x5, x6, x7 |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * Since the page tables have been populated with non-cacheable |
| 590 | * accesses (MMU disabled), invalidate the idmap and swapper page |
| 591 | * tables again to remove any speculatively loaded cache lines. |
| 592 | */ |
| 593 | mov x0, x25 |
| 594 | add x1, x26, #SWAPPER_DIR_SIZE |
| 595 | bl __inval_cache_range |
| 596 | |
| 597 | mov lr, x27 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 598 | ret |
| 599 | ENDPROC(__create_page_tables) |
| 600 | .ltorg |
| 601 | |
| 602 | .align 3 |
| 603 | .type __switch_data, %object |
| 604 | __switch_data: |
| 605 | .quad __mmap_switched |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 606 | .quad __bss_start // x6 |
Mark Rutland | bd00cd5 | 2014-06-24 16:51:35 +0100 | [diff] [blame^] | 607 | .quad __bss_stop // x7 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 608 | .quad processor_id // x4 |
| 609 | .quad __fdt_pointer // x5 |
| 610 | .quad memstart_addr // x6 |
| 611 | .quad init_thread_union + THREAD_START_SP // sp |
| 612 | |
| 613 | /* |
| 614 | * The following fragment of code is executed with the MMU on in MMU mode, and |
| 615 | * uses absolute addresses; this is not position independent. |
| 616 | */ |
| 617 | __mmap_switched: |
| 618 | adr x3, __switch_data + 8 |
| 619 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 620 | ldp x6, x7, [x3], #16 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 621 | 1: cmp x6, x7 |
| 622 | b.hs 2f |
| 623 | str xzr, [x6], #8 // Clear BSS |
| 624 | b 1b |
| 625 | 2: |
| 626 | ldp x4, x5, [x3], #16 |
| 627 | ldr x6, [x3], #8 |
| 628 | ldr x16, [x3] |
| 629 | mov sp, x16 |
| 630 | str x22, [x4] // Save processor ID |
| 631 | str x21, [x5] // Save FDT pointer |
| 632 | str x24, [x6] // Save PHYS_OFFSET |
| 633 | mov x29, #0 |
| 634 | b start_kernel |
| 635 | ENDPROC(__mmap_switched) |
| 636 | |
| 637 | /* |
| 638 | * Exception handling. Something went wrong and we can't proceed. We ought to |
| 639 | * tell the user, but since we don't have any guarantee that we're even |
| 640 | * running on the right architecture, we do virtually nothing. |
| 641 | */ |
| 642 | __error_p: |
| 643 | ENDPROC(__error_p) |
| 644 | |
| 645 | __error: |
| 646 | 1: nop |
| 647 | b 1b |
| 648 | ENDPROC(__error) |
| 649 | |
| 650 | /* |
| 651 | * This function gets the processor ID in w0 and searches the cpu_table[] for |
| 652 | * a match. It returns a pointer to the struct cpu_info it found. The |
| 653 | * cpu_table[] must end with an empty (all zeros) structure. |
| 654 | * |
| 655 | * This routine can be called via C code and it needs to work with the MMU |
| 656 | * both disabled and enabled (the offset is calculated automatically). |
| 657 | */ |
| 658 | ENTRY(lookup_processor_type) |
| 659 | adr x1, __lookup_processor_type_data |
| 660 | ldp x2, x3, [x1] |
| 661 | sub x1, x1, x2 // get offset between VA and PA |
| 662 | add x3, x3, x1 // convert VA to PA |
| 663 | 1: |
| 664 | ldp w5, w6, [x3] // load cpu_id_val and cpu_id_mask |
| 665 | cbz w5, 2f // end of list? |
| 666 | and w6, w6, w0 |
| 667 | cmp w5, w6 |
| 668 | b.eq 3f |
| 669 | add x3, x3, #CPU_INFO_SZ |
| 670 | b 1b |
| 671 | 2: |
| 672 | mov x3, #0 // unknown processor |
| 673 | 3: |
| 674 | mov x0, x3 |
| 675 | ret |
| 676 | ENDPROC(lookup_processor_type) |
| 677 | |
| 678 | .align 3 |
| 679 | .type __lookup_processor_type_data, %object |
| 680 | __lookup_processor_type_data: |
| 681 | .quad . |
| 682 | .quad cpu_table |
| 683 | .size __lookup_processor_type_data, . - __lookup_processor_type_data |
| 684 | |
| 685 | /* |
| 686 | * Determine validity of the x21 FDT pointer. |
| 687 | * The dtb must be 8-byte aligned and live in the first 512M of memory. |
| 688 | */ |
| 689 | __vet_fdt: |
| 690 | tst x21, #0x7 |
| 691 | b.ne 1f |
| 692 | cmp x21, x24 |
| 693 | b.lt 1f |
| 694 | mov x0, #(1 << 29) |
| 695 | add x0, x0, x24 |
| 696 | cmp x21, x0 |
| 697 | b.ge 1f |
| 698 | ret |
| 699 | 1: |
| 700 | mov x21, #0 |
| 701 | ret |
| 702 | ENDPROC(__vet_fdt) |