Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 5 | * Based on code from the coreboot file of the same name |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <cpu.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 13 | #include <malloc.h> |
Miao Yan | 9210627 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 14 | #include <qfw.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 15 | #include <asm/atomic.h> |
| 16 | #include <asm/cpu.h> |
| 17 | #include <asm/interrupt.h> |
| 18 | #include <asm/lapic.h> |
Simon Glass | c17d450 | 2016-03-11 22:07:09 -0700 | [diff] [blame] | 19 | #include <asm/microcode.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 20 | #include <asm/mp.h> |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 21 | #include <asm/msr.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 22 | #include <asm/mtrr.h> |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 23 | #include <asm/processor.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 24 | #include <asm/sipi.h> |
| 25 | #include <dm/device-internal.h> |
| 26 | #include <dm/uclass-internal.h> |
Miao Yan | 35f54b2 | 2016-01-07 01:32:04 -0800 | [diff] [blame] | 27 | #include <dm/lists.h> |
| 28 | #include <dm/root.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 29 | #include <linux/delay.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 30 | #include <linux/linkage.h> |
| 31 | |
Simon Glass | daa93d9 | 2015-07-31 09:31:31 -0600 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 34 | /* Total CPUs include BSP */ |
| 35 | static int num_cpus; |
| 36 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 37 | /* This also needs to match the sipi.S assembly code for saved MSR encoding */ |
| 38 | struct saved_msr { |
| 39 | uint32_t index; |
| 40 | uint32_t lo; |
| 41 | uint32_t hi; |
| 42 | } __packed; |
| 43 | |
| 44 | |
| 45 | struct mp_flight_plan { |
| 46 | int num_records; |
| 47 | struct mp_flight_record *records; |
| 48 | }; |
| 49 | |
| 50 | static struct mp_flight_plan mp_info; |
| 51 | |
| 52 | struct cpu_map { |
| 53 | struct udevice *dev; |
| 54 | int apic_id; |
| 55 | int err_code; |
| 56 | }; |
| 57 | |
| 58 | static inline void barrier_wait(atomic_t *b) |
| 59 | { |
| 60 | while (atomic_read(b) == 0) |
| 61 | asm("pause"); |
| 62 | mfence(); |
| 63 | } |
| 64 | |
| 65 | static inline void release_barrier(atomic_t *b) |
| 66 | { |
| 67 | mfence(); |
| 68 | atomic_set(b, 1); |
| 69 | } |
| 70 | |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 71 | static inline void stop_this_cpu(void) |
| 72 | { |
| 73 | /* Called by an AP when it is ready to halt and wait for a new task */ |
| 74 | for (;;) |
| 75 | cpu_hlt(); |
| 76 | } |
| 77 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 78 | /* Returns 1 if timeout waiting for APs. 0 if target APs found */ |
| 79 | static int wait_for_aps(atomic_t *val, int target, int total_delay, |
| 80 | int delay_step) |
| 81 | { |
| 82 | int timeout = 0; |
| 83 | int delayed = 0; |
| 84 | |
| 85 | while (atomic_read(val) != target) { |
| 86 | udelay(delay_step); |
| 87 | delayed += delay_step; |
| 88 | if (delayed >= total_delay) { |
| 89 | timeout = 1; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return timeout; |
| 95 | } |
| 96 | |
| 97 | static void ap_do_flight_plan(struct udevice *cpu) |
| 98 | { |
| 99 | int i; |
| 100 | |
| 101 | for (i = 0; i < mp_info.num_records; i++) { |
| 102 | struct mp_flight_record *rec = &mp_info.records[i]; |
| 103 | |
| 104 | atomic_inc(&rec->cpus_entered); |
| 105 | barrier_wait(&rec->barrier); |
| 106 | |
| 107 | if (rec->ap_call != NULL) |
| 108 | rec->ap_call(cpu, rec->ap_arg); |
| 109 | } |
| 110 | } |
| 111 | |
Miao Yan | 2ee1000 | 2016-01-07 01:32:02 -0800 | [diff] [blame] | 112 | static int find_cpu_by_apic_id(int apic_id, struct udevice **devp) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 113 | { |
| 114 | struct udevice *dev; |
| 115 | |
| 116 | *devp = NULL; |
| 117 | for (uclass_find_first_device(UCLASS_CPU, &dev); |
| 118 | dev; |
| 119 | uclass_find_next_device(&dev)) { |
| 120 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
| 121 | |
| 122 | if (plat->cpu_id == apic_id) { |
| 123 | *devp = dev; |
| 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return -ENOENT; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * By the time APs call ap_init() caching has been setup, and microcode has |
| 133 | * been loaded |
| 134 | */ |
| 135 | static void ap_init(unsigned int cpu_index) |
| 136 | { |
| 137 | struct udevice *dev; |
| 138 | int apic_id; |
| 139 | int ret; |
| 140 | |
| 141 | /* Ensure the local apic is enabled */ |
| 142 | enable_lapic(); |
| 143 | |
| 144 | apic_id = lapicid(); |
Miao Yan | 2ee1000 | 2016-01-07 01:32:02 -0800 | [diff] [blame] | 145 | ret = find_cpu_by_apic_id(apic_id, &dev); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 146 | if (ret) { |
| 147 | debug("Unknown CPU apic_id %x\n", apic_id); |
| 148 | goto done; |
| 149 | } |
| 150 | |
| 151 | debug("AP: slot %d apic_id %x, dev %s\n", cpu_index, apic_id, |
| 152 | dev ? dev->name : "(apic_id not found)"); |
| 153 | |
| 154 | /* Walk the flight plan */ |
| 155 | ap_do_flight_plan(dev); |
| 156 | |
| 157 | /* Park the AP */ |
| 158 | debug("parking\n"); |
| 159 | done: |
| 160 | stop_this_cpu(); |
| 161 | } |
| 162 | |
| 163 | static const unsigned int fixed_mtrrs[NUM_FIXED_MTRRS] = { |
| 164 | MTRR_FIX_64K_00000_MSR, MTRR_FIX_16K_80000_MSR, MTRR_FIX_16K_A0000_MSR, |
| 165 | MTRR_FIX_4K_C0000_MSR, MTRR_FIX_4K_C8000_MSR, MTRR_FIX_4K_D0000_MSR, |
| 166 | MTRR_FIX_4K_D8000_MSR, MTRR_FIX_4K_E0000_MSR, MTRR_FIX_4K_E8000_MSR, |
| 167 | MTRR_FIX_4K_F0000_MSR, MTRR_FIX_4K_F8000_MSR, |
| 168 | }; |
| 169 | |
| 170 | static inline struct saved_msr *save_msr(int index, struct saved_msr *entry) |
| 171 | { |
| 172 | msr_t msr; |
| 173 | |
| 174 | msr = msr_read(index); |
| 175 | entry->index = index; |
| 176 | entry->lo = msr.lo; |
| 177 | entry->hi = msr.hi; |
| 178 | |
| 179 | /* Return the next entry */ |
| 180 | entry++; |
| 181 | return entry; |
| 182 | } |
| 183 | |
| 184 | static int save_bsp_msrs(char *start, int size) |
| 185 | { |
| 186 | int msr_count; |
| 187 | int num_var_mtrrs; |
| 188 | struct saved_msr *msr_entry; |
| 189 | int i; |
| 190 | msr_t msr; |
| 191 | |
| 192 | /* Determine number of MTRRs need to be saved */ |
| 193 | msr = msr_read(MTRR_CAP_MSR); |
| 194 | num_var_mtrrs = msr.lo & 0xff; |
| 195 | |
| 196 | /* 2 * num_var_mtrrs for base and mask. +1 for IA32_MTRR_DEF_TYPE */ |
| 197 | msr_count = 2 * num_var_mtrrs + NUM_FIXED_MTRRS + 1; |
| 198 | |
| 199 | if ((msr_count * sizeof(struct saved_msr)) > size) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 200 | printf("Cannot mirror all %d msrs\n", msr_count); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 201 | return -ENOSPC; |
| 202 | } |
| 203 | |
| 204 | msr_entry = (void *)start; |
| 205 | for (i = 0; i < NUM_FIXED_MTRRS; i++) |
| 206 | msr_entry = save_msr(fixed_mtrrs[i], msr_entry); |
| 207 | |
| 208 | for (i = 0; i < num_var_mtrrs; i++) { |
| 209 | msr_entry = save_msr(MTRR_PHYS_BASE_MSR(i), msr_entry); |
| 210 | msr_entry = save_msr(MTRR_PHYS_MASK_MSR(i), msr_entry); |
| 211 | } |
| 212 | |
| 213 | msr_entry = save_msr(MTRR_DEF_TYPE_MSR, msr_entry); |
| 214 | |
| 215 | return msr_count; |
| 216 | } |
| 217 | |
Miao Yan | 6067762 | 2016-01-07 01:32:03 -0800 | [diff] [blame] | 218 | static int load_sipi_vector(atomic_t **ap_countp, int num_cpus) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 219 | { |
| 220 | struct sipi_params_16bit *params16; |
| 221 | struct sipi_params *params; |
| 222 | static char msr_save[512]; |
| 223 | char *stack; |
| 224 | ulong addr; |
| 225 | int code_len; |
| 226 | int size; |
| 227 | int ret; |
| 228 | |
| 229 | /* Copy in the code */ |
| 230 | code_len = ap_start16_code_end - ap_start16; |
| 231 | debug("Copying SIPI code to %x: %d bytes\n", AP_DEFAULT_BASE, |
| 232 | code_len); |
| 233 | memcpy((void *)AP_DEFAULT_BASE, ap_start16, code_len); |
| 234 | |
| 235 | addr = AP_DEFAULT_BASE + (ulong)sipi_params_16bit - (ulong)ap_start16; |
| 236 | params16 = (struct sipi_params_16bit *)addr; |
| 237 | params16->ap_start = (uint32_t)ap_start; |
| 238 | params16->gdt = (uint32_t)gd->arch.gdt; |
| 239 | params16->gdt_limit = X86_GDT_SIZE - 1; |
| 240 | debug("gdt = %x, gdt_limit = %x\n", params16->gdt, params16->gdt_limit); |
| 241 | |
| 242 | params = (struct sipi_params *)sipi_params; |
| 243 | debug("SIPI 32-bit params at %p\n", params); |
| 244 | params->idt_ptr = (uint32_t)x86_get_idt(); |
| 245 | |
| 246 | params->stack_size = CONFIG_AP_STACK_SIZE; |
Miao Yan | 6067762 | 2016-01-07 01:32:03 -0800 | [diff] [blame] | 247 | size = params->stack_size * num_cpus; |
Stephen Warren | 5923b59 | 2016-02-12 14:27:56 -0700 | [diff] [blame] | 248 | stack = memalign(4096, size); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 249 | if (!stack) |
| 250 | return -ENOMEM; |
| 251 | params->stack_top = (u32)(stack + size); |
Andy Shevchenko | 43b3ac5 | 2017-02-17 16:49:00 +0300 | [diff] [blame] | 252 | #if !defined(CONFIG_QEMU) && !defined(CONFIG_HAVE_FSP) && \ |
| 253 | !defined(CONFIG_INTEL_MID) |
Simon Glass | 8dda587 | 2016-03-11 22:07:11 -0700 | [diff] [blame] | 254 | params->microcode_ptr = ucode_base; |
| 255 | debug("Microcode at %x\n", params->microcode_ptr); |
| 256 | #endif |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 257 | params->msr_table_ptr = (u32)msr_save; |
| 258 | ret = save_bsp_msrs(msr_save, sizeof(msr_save)); |
| 259 | if (ret < 0) |
| 260 | return ret; |
| 261 | params->msr_count = ret; |
| 262 | |
| 263 | params->c_handler = (uint32_t)&ap_init; |
| 264 | |
| 265 | *ap_countp = ¶ms->ap_count; |
| 266 | atomic_set(*ap_countp, 0); |
| 267 | debug("SIPI vector is ready\n"); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int check_cpu_devices(int expected_cpus) |
| 273 | { |
| 274 | int i; |
| 275 | |
| 276 | for (i = 0; i < expected_cpus; i++) { |
| 277 | struct udevice *dev; |
| 278 | int ret; |
| 279 | |
| 280 | ret = uclass_find_device(UCLASS_CPU, i, &dev); |
| 281 | if (ret) { |
| 282 | debug("Cannot find CPU %d in device tree\n", i); |
| 283 | return ret; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* Returns 1 for timeout. 0 on success */ |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 291 | static int apic_wait_timeout(int total_delay, const char *msg) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 292 | { |
| 293 | int total = 0; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 294 | |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 295 | if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) |
| 296 | return 0; |
| 297 | |
| 298 | debug("Waiting for %s...", msg); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 299 | while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 300 | udelay(50); |
| 301 | total += 50; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 302 | if (total >= total_delay) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 303 | debug("timed out: aborting\n"); |
| 304 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 305 | } |
| 306 | } |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 307 | debug("done\n"); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 308 | |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 309 | return 0; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int start_aps(int ap_count, atomic_t *num_aps) |
| 313 | { |
| 314 | int sipi_vector; |
| 315 | /* Max location is 4KiB below 1MiB */ |
| 316 | const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12; |
| 317 | |
| 318 | if (ap_count == 0) |
| 319 | return 0; |
| 320 | |
| 321 | /* The vector is sent as a 4k aligned address in one byte */ |
| 322 | sipi_vector = AP_DEFAULT_BASE >> 12; |
| 323 | |
| 324 | if (sipi_vector > max_vector_loc) { |
| 325 | printf("SIPI vector too large! 0x%08x\n", |
| 326 | sipi_vector); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 327 | return -ENOSPC; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | debug("Attempting to start %d APs\n", ap_count); |
| 331 | |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 332 | if (apic_wait_timeout(1000, "ICR not to be busy")) |
| 333 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 334 | |
| 335 | /* Send INIT IPI to all but self */ |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 336 | lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0)); |
| 337 | lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | |
| 338 | LAPIC_DM_INIT); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 339 | debug("Waiting for 10ms after sending INIT\n"); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 340 | mdelay(10); |
| 341 | |
| 342 | /* Send 1st SIPI */ |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 343 | if (apic_wait_timeout(1000, "ICR not to be busy")) |
| 344 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 345 | |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 346 | lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0)); |
| 347 | lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | |
| 348 | LAPIC_DM_STARTUP | sipi_vector); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 349 | if (apic_wait_timeout(10000, "first SIPI to complete")) |
| 350 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 351 | |
| 352 | /* Wait for CPUs to check in up to 200 us */ |
| 353 | wait_for_aps(num_aps, ap_count, 200, 15); |
| 354 | |
| 355 | /* Send 2nd SIPI */ |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 356 | if (apic_wait_timeout(1000, "ICR not to be busy")) |
| 357 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 358 | |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 359 | lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0)); |
| 360 | lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | |
| 361 | LAPIC_DM_STARTUP | sipi_vector); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 362 | if (apic_wait_timeout(10000, "second SIPI to complete")) |
| 363 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 364 | |
| 365 | /* Wait for CPUs to check in */ |
| 366 | if (wait_for_aps(num_aps, ap_count, 10000, 50)) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 367 | debug("Not all APs checked in: %d/%d\n", |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 368 | atomic_read(num_aps), ap_count); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 369 | return -EIO; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static int bsp_do_flight_plan(struct udevice *cpu, struct mp_params *mp_params) |
| 376 | { |
| 377 | int i; |
| 378 | int ret = 0; |
| 379 | const int timeout_us = 100000; |
| 380 | const int step_us = 100; |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 381 | int num_aps = num_cpus - 1; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 382 | |
| 383 | for (i = 0; i < mp_params->num_records; i++) { |
| 384 | struct mp_flight_record *rec = &mp_params->flight_plan[i]; |
| 385 | |
| 386 | /* Wait for APs if the record is not released */ |
| 387 | if (atomic_read(&rec->barrier) == 0) { |
| 388 | /* Wait for the APs to check in */ |
| 389 | if (wait_for_aps(&rec->cpus_entered, num_aps, |
| 390 | timeout_us, step_us)) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 391 | debug("MP record %d timeout\n", i); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 392 | ret = -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | if (rec->bsp_call != NULL) |
| 397 | rec->bsp_call(cpu, rec->bsp_arg); |
| 398 | |
| 399 | release_barrier(&rec->barrier); |
| 400 | } |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static int init_bsp(struct udevice **devp) |
| 405 | { |
| 406 | char processor_name[CPU_MAX_NAME_LEN]; |
| 407 | int apic_id; |
| 408 | int ret; |
| 409 | |
| 410 | cpu_get_name(processor_name); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 411 | debug("CPU: %s\n", processor_name); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 412 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 413 | apic_id = lapicid(); |
Miao Yan | 2ee1000 | 2016-01-07 01:32:02 -0800 | [diff] [blame] | 414 | ret = find_cpu_by_apic_id(apic_id, devp); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 415 | if (ret) { |
| 416 | printf("Cannot find boot CPU, APIC ID %d\n", apic_id); |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | int mp_init(struct mp_params *p) |
| 424 | { |
| 425 | int num_aps; |
| 426 | atomic_t *ap_count; |
| 427 | struct udevice *cpu; |
| 428 | int ret; |
| 429 | |
| 430 | /* This will cause the CPUs devices to be bound */ |
| 431 | struct uclass *uc; |
| 432 | ret = uclass_get(UCLASS_CPU, &uc); |
| 433 | if (ret) |
| 434 | return ret; |
| 435 | |
Simon Glass | 4c8243d | 2019-12-06 21:42:55 -0700 | [diff] [blame] | 436 | if (IS_ENABLED(CONFIG_QFW)) { |
| 437 | ret = qemu_cpu_fixup(); |
| 438 | if (ret) |
| 439 | return ret; |
| 440 | } |
Miao Yan | 35f54b2 | 2016-01-07 01:32:04 -0800 | [diff] [blame] | 441 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 442 | ret = init_bsp(&cpu); |
| 443 | if (ret) { |
| 444 | debug("Cannot init boot CPU: err=%d\n", ret); |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | if (p == NULL || p->flight_plan == NULL || p->num_records < 1) { |
| 449 | printf("Invalid MP parameters\n"); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 450 | return -EINVAL; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 451 | } |
| 452 | |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 453 | num_cpus = cpu_get_count(cpu); |
| 454 | if (num_cpus < 0) { |
| 455 | debug("Cannot get number of CPUs: err=%d\n", num_cpus); |
| 456 | return num_cpus; |
| 457 | } |
| 458 | |
| 459 | if (num_cpus < 2) |
| 460 | debug("Warning: Only 1 CPU is detected\n"); |
| 461 | |
| 462 | ret = check_cpu_devices(num_cpus); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 463 | if (ret) |
| 464 | debug("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n"); |
| 465 | |
| 466 | /* Copy needed parameters so that APs have a reference to the plan */ |
| 467 | mp_info.num_records = p->num_records; |
| 468 | mp_info.records = p->flight_plan; |
| 469 | |
| 470 | /* Load the SIPI vector */ |
Miao Yan | 6067762 | 2016-01-07 01:32:03 -0800 | [diff] [blame] | 471 | ret = load_sipi_vector(&ap_count, num_cpus); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 472 | if (ap_count == NULL) |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 473 | return -ENOENT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * Make sure SIPI data hits RAM so the APs that come up will see |
| 477 | * the startup code even if the caches are disabled |
| 478 | */ |
| 479 | wbinvd(); |
| 480 | |
| 481 | /* Start the APs providing number of APs and the cpus_entered field */ |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 482 | num_aps = num_cpus - 1; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 483 | ret = start_aps(num_aps, ap_count); |
| 484 | if (ret) { |
| 485 | mdelay(1000); |
| 486 | debug("%d/%d eventually checked in?\n", atomic_read(ap_count), |
| 487 | num_aps); |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /* Walk the flight plan for the BSP */ |
| 492 | ret = bsp_do_flight_plan(cpu, p); |
| 493 | if (ret) { |
| 494 | debug("CPU init failed: err=%d\n", ret); |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | int mp_init_cpu(struct udevice *cpu, void *unused) |
| 502 | { |
Simon Glass | c17d450 | 2016-03-11 22:07:09 -0700 | [diff] [blame] | 503 | struct cpu_platdata *plat = dev_get_parent_platdata(cpu); |
| 504 | |
Bin Meng | 76868d1 | 2015-08-09 23:58:39 -0700 | [diff] [blame] | 505 | /* |
| 506 | * Multiple APs are brought up simultaneously and they may get the same |
| 507 | * seq num in the uclass_resolve_seq() during device_probe(). To avoid |
| 508 | * this, set req_seq to the reg number in the device tree in advance. |
| 509 | */ |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 510 | cpu->req_seq = fdtdec_get_int(gd->fdt_blob, dev_of_offset(cpu), "reg", |
| 511 | -1); |
Simon Glass | c17d450 | 2016-03-11 22:07:09 -0700 | [diff] [blame] | 512 | plat->ucode_version = microcode_read_rev(); |
| 513 | plat->device_id = gd->arch.x86_device; |
Bin Meng | 76868d1 | 2015-08-09 23:58:39 -0700 | [diff] [blame] | 514 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 515 | return device_probe(cpu); |
| 516 | } |