rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <compiler.h> |
| 24 | #include <debug.h> |
| 25 | #include <arch.h> |
| 26 | #include <arch/ops.h> |
| 27 | #include <arch/arm64.h> |
| 28 | #include <arch/arm64/mmu.h> |
| 29 | #include <arch/mp.h> |
| 30 | #include <kernel/thread.h> |
| 31 | #if WITH_KERNEL_VM |
| 32 | #include <kernel/vm.h> |
| 33 | #endif |
| 34 | #include <lk/init.h> |
| 35 | #include <lk/main.h> |
| 36 | #include <platform.h> |
| 37 | #include <target.h> |
| 38 | #include <trace.h> |
| 39 | |
| 40 | #define LOCAL_TRACE 0 |
| 41 | |
| 42 | #if WITH_SMP |
| 43 | /* smp boot lock */ |
| 44 | static spin_lock_t arm_boot_cpu_lock = 1; |
| 45 | static volatile int secondaries_to_init = 0; |
| 46 | __WEAK const uint8_t *linear_cpuid_map = NULL; |
| 47 | #endif |
| 48 | |
| 49 | static void arm64_cpu_early_init(void) |
| 50 | { |
| 51 | /* set the vector base */ |
| 52 | ARM64_WRITE_SYSREG(VBAR_EL1, (uint64_t)&arm64_exception_base); |
| 53 | |
| 54 | /* switch to EL1 */ |
| 55 | unsigned int current_el = ARM64_READ_SYSREG(CURRENTEL) >> 2; |
| 56 | if (current_el > 1) { |
| 57 | arm64_elX_to_el1(); |
| 58 | } |
| 59 | |
| 60 | arch_enable_fiqs(); |
| 61 | } |
| 62 | |
| 63 | void arch_early_init(void) |
| 64 | { |
| 65 | arm64_cpu_early_init(); |
| 66 | platform_init_mmu_mappings(); |
| 67 | } |
| 68 | |
| 69 | void arch_init(void) |
| 70 | { |
| 71 | #if WITH_SMP |
| 72 | arch_mp_init_percpu(); |
| 73 | |
| 74 | LTRACEF("midr_el1 0x%llx\n", ARM64_READ_SYSREG(midr_el1)); |
| 75 | |
| 76 | secondaries_to_init = SMP_MAX_CPUS - 1; /* TODO: get count from somewhere else, or add cpus as they boot */ |
| 77 | |
| 78 | lk_init_secondary_cpus(secondaries_to_init); |
| 79 | |
| 80 | LTRACEF("releasing %d secondary cpus\n", secondaries_to_init); |
| 81 | |
| 82 | /* release the secondary cpus */ |
| 83 | spin_unlock(&arm_boot_cpu_lock); |
| 84 | |
| 85 | /* flush the release of the lock, since the secondary cpus are running without cache on */ |
| 86 | arch_clean_cache_range((addr_t)&arm_boot_cpu_lock, sizeof(arm_boot_cpu_lock)); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void arch_quiesce(void) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void arch_idle(void) |
| 95 | { |
| 96 | __asm__ volatile("wfi"); |
| 97 | } |
| 98 | |
| 99 | void arch_chain_load(void *entry, ulong arg0, ulong arg1, ulong arg2, ulong arg3) |
| 100 | { |
| 101 | LTRACEF("entry %p, args 0x%lx 0x%lx 0x%lx 0x%lx\n", entry, arg0, arg1, arg2, arg3); |
| 102 | |
| 103 | arch_disable_ints(); |
| 104 | |
| 105 | /* give target and platform a chance to put hardware into a suitable |
| 106 | * state for chain loading. |
| 107 | */ |
| 108 | target_quiesce(); |
| 109 | platform_quiesce(); |
| 110 | |
| 111 | paddr_t entry_pa; |
| 112 | paddr_t loader_pa; |
| 113 | |
| 114 | #if WITH_KERNEL_VM |
| 115 | entry_pa = kvaddr_to_paddr(entry); |
| 116 | if (entry_pa == (paddr_t)NULL) { |
| 117 | panic("error translating entry physical address\n"); |
| 118 | } |
| 119 | |
| 120 | LTRACEF("entry pa 0x%lx\n", entry_pa); |
| 121 | |
| 122 | loader_pa = kvaddr_to_paddr((void *)&arm64_chain_load); |
| 123 | if (loader_pa == (paddr_t)NULL) { |
| 124 | panic("error translating loader physical address\n"); |
| 125 | } |
| 126 | |
| 127 | LTRACEF("loader pa 0x%lx\n", loader_pa); |
| 128 | |
| 129 | /* TTBR0_EL1 already contains the physical address mapping */ |
| 130 | ARM64_WRITE_SYSREG(tcr_el1, (uint64_t)MMU_TCR_FLAGS_IDENT); |
| 131 | #else |
| 132 | entry_pa = (paddr_t)entry; |
| 133 | loader_pa = (paddr_t)&arm64_chain_load; |
| 134 | #endif |
| 135 | |
| 136 | LTRACEF("disabling instruction/data cache\n"); |
| 137 | arch_disable_cache(UCACHE); |
| 138 | |
| 139 | /* put the booting cpu back into close to a default state */ |
| 140 | arch_quiesce(); |
| 141 | |
| 142 | LTRACEF("branching to physical address of loader\n"); |
| 143 | |
| 144 | /* branch to the physical address version of the chain loader routine */ |
| 145 | void (*loader)(paddr_t entry, ulong, ulong, ulong, ulong) __NO_RETURN = (void *)loader_pa; |
| 146 | loader(entry_pa, arg0, arg1, arg2, arg3); |
| 147 | } |
| 148 | |
| 149 | #if WITH_SMP |
| 150 | void arm64_secondary_entry(ulong asm_cpu_num) |
| 151 | { |
| 152 | uint cpu = arch_curr_cpu_num(); |
| 153 | if (cpu != asm_cpu_num) |
| 154 | return; |
| 155 | |
| 156 | arm64_cpu_early_init(); |
| 157 | |
| 158 | spin_lock(&arm_boot_cpu_lock); |
| 159 | spin_unlock(&arm_boot_cpu_lock); |
| 160 | |
| 161 | /* run early secondary cpu init routines up to the threading level */ |
| 162 | lk_init_level(LK_INIT_FLAG_SECONDARY_CPUS, LK_INIT_LEVEL_EARLIEST, LK_INIT_LEVEL_THREADING - 1); |
| 163 | |
| 164 | arch_mp_init_percpu(); |
| 165 | |
| 166 | LTRACEF("cpu num %d\n", cpu); |
| 167 | |
| 168 | /* we're done, tell the main cpu we're up */ |
| 169 | atomic_add(&secondaries_to_init, -1); |
| 170 | __asm__ volatile("sev"); |
| 171 | |
| 172 | lk_secondary_cpu_entry(); |
| 173 | } |
| 174 | #endif |
| 175 | |