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> |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 18 | #include <asm/io.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 19 | #include <asm/lapic.h> |
Simon Glass | c17d450 | 2016-03-11 22:07:09 -0700 | [diff] [blame] | 20 | #include <asm/microcode.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 21 | #include <asm/mp.h> |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 22 | #include <asm/msr.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 23 | #include <asm/mtrr.h> |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 24 | #include <asm/processor.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 25 | #include <asm/sipi.h> |
| 26 | #include <dm/device-internal.h> |
| 27 | #include <dm/uclass-internal.h> |
Miao Yan | 35f54b2 | 2016-01-07 01:32:04 -0800 | [diff] [blame] | 28 | #include <dm/lists.h> |
| 29 | #include <dm/root.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 30 | #include <linux/delay.h> |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 31 | #include <linux/linkage.h> |
| 32 | |
Simon Glass | daa93d9 | 2015-07-31 09:31:31 -0600 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 35 | /* This also needs to match the sipi.S assembly code for saved MSR encoding */ |
| 36 | struct saved_msr { |
| 37 | uint32_t index; |
| 38 | uint32_t lo; |
| 39 | uint32_t hi; |
| 40 | } __packed; |
| 41 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 42 | struct mp_flight_plan { |
| 43 | int num_records; |
| 44 | struct mp_flight_record *records; |
| 45 | }; |
| 46 | |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 47 | /** |
| 48 | * struct mp_callback - Callback information for APs |
| 49 | * |
| 50 | * @func: Function to run |
| 51 | * @arg: Argument to pass to the function |
| 52 | * @logical_cpu_number: Either a CPU number (i.e. dev->req_seq) or a special |
| 53 | * value like MP_SELECT_BSP. It tells the AP whether it should process this |
| 54 | * callback |
| 55 | */ |
| 56 | struct mp_callback { |
| 57 | /** |
| 58 | * func() - Function to call on the AP |
| 59 | * |
| 60 | * @arg: Argument to pass |
| 61 | */ |
| 62 | void (*func)(void *arg); |
| 63 | void *arg; |
| 64 | int logical_cpu_number; |
| 65 | }; |
| 66 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 67 | static struct mp_flight_plan mp_info; |
| 68 | |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 69 | /* |
| 70 | * ap_callbacks - Callback mailbox array |
| 71 | * |
| 72 | * Array of callback, one entry for each available CPU, indexed by the CPU |
| 73 | * number, which is dev->req_seq. The entry for the main CPU is never used. |
| 74 | * When this is NULL, there is no pending work for the CPU to run. When |
| 75 | * non-NULL it points to the mp_callback structure. This is shared between all |
| 76 | * CPUs, so should only be written by the main CPU. |
| 77 | */ |
| 78 | static struct mp_callback **ap_callbacks; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 79 | |
| 80 | static inline void barrier_wait(atomic_t *b) |
| 81 | { |
| 82 | while (atomic_read(b) == 0) |
| 83 | asm("pause"); |
| 84 | mfence(); |
| 85 | } |
| 86 | |
| 87 | static inline void release_barrier(atomic_t *b) |
| 88 | { |
| 89 | mfence(); |
| 90 | atomic_set(b, 1); |
| 91 | } |
| 92 | |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 93 | static inline void stop_this_cpu(void) |
| 94 | { |
| 95 | /* Called by an AP when it is ready to halt and wait for a new task */ |
| 96 | for (;;) |
| 97 | cpu_hlt(); |
| 98 | } |
| 99 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 100 | /* Returns 1 if timeout waiting for APs. 0 if target APs found */ |
| 101 | static int wait_for_aps(atomic_t *val, int target, int total_delay, |
| 102 | int delay_step) |
| 103 | { |
| 104 | int timeout = 0; |
| 105 | int delayed = 0; |
| 106 | |
| 107 | while (atomic_read(val) != target) { |
| 108 | udelay(delay_step); |
| 109 | delayed += delay_step; |
| 110 | if (delayed >= total_delay) { |
| 111 | timeout = 1; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return timeout; |
| 117 | } |
| 118 | |
| 119 | static void ap_do_flight_plan(struct udevice *cpu) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < mp_info.num_records; i++) { |
| 124 | struct mp_flight_record *rec = &mp_info.records[i]; |
| 125 | |
| 126 | atomic_inc(&rec->cpus_entered); |
| 127 | barrier_wait(&rec->barrier); |
| 128 | |
| 129 | if (rec->ap_call != NULL) |
| 130 | rec->ap_call(cpu, rec->ap_arg); |
| 131 | } |
| 132 | } |
| 133 | |
Miao Yan | 2ee1000 | 2016-01-07 01:32:02 -0800 | [diff] [blame] | 134 | 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] | 135 | { |
| 136 | struct udevice *dev; |
| 137 | |
| 138 | *devp = NULL; |
| 139 | for (uclass_find_first_device(UCLASS_CPU, &dev); |
| 140 | dev; |
| 141 | uclass_find_next_device(&dev)) { |
| 142 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
| 143 | |
| 144 | if (plat->cpu_id == apic_id) { |
| 145 | *devp = dev; |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return -ENOENT; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * By the time APs call ap_init() caching has been setup, and microcode has |
| 155 | * been loaded |
| 156 | */ |
| 157 | static void ap_init(unsigned int cpu_index) |
| 158 | { |
| 159 | struct udevice *dev; |
| 160 | int apic_id; |
| 161 | int ret; |
| 162 | |
| 163 | /* Ensure the local apic is enabled */ |
| 164 | enable_lapic(); |
| 165 | |
| 166 | apic_id = lapicid(); |
Miao Yan | 2ee1000 | 2016-01-07 01:32:02 -0800 | [diff] [blame] | 167 | ret = find_cpu_by_apic_id(apic_id, &dev); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 168 | if (ret) { |
| 169 | debug("Unknown CPU apic_id %x\n", apic_id); |
| 170 | goto done; |
| 171 | } |
| 172 | |
| 173 | debug("AP: slot %d apic_id %x, dev %s\n", cpu_index, apic_id, |
| 174 | dev ? dev->name : "(apic_id not found)"); |
| 175 | |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 176 | /* |
| 177 | * Walk the flight plan, which only returns if CONFIG_SMP_AP_WORK is not |
| 178 | * enabled |
| 179 | */ |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 180 | ap_do_flight_plan(dev); |
| 181 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 182 | done: |
| 183 | stop_this_cpu(); |
| 184 | } |
| 185 | |
| 186 | static const unsigned int fixed_mtrrs[NUM_FIXED_MTRRS] = { |
| 187 | MTRR_FIX_64K_00000_MSR, MTRR_FIX_16K_80000_MSR, MTRR_FIX_16K_A0000_MSR, |
| 188 | MTRR_FIX_4K_C0000_MSR, MTRR_FIX_4K_C8000_MSR, MTRR_FIX_4K_D0000_MSR, |
| 189 | MTRR_FIX_4K_D8000_MSR, MTRR_FIX_4K_E0000_MSR, MTRR_FIX_4K_E8000_MSR, |
| 190 | MTRR_FIX_4K_F0000_MSR, MTRR_FIX_4K_F8000_MSR, |
| 191 | }; |
| 192 | |
| 193 | static inline struct saved_msr *save_msr(int index, struct saved_msr *entry) |
| 194 | { |
| 195 | msr_t msr; |
| 196 | |
| 197 | msr = msr_read(index); |
| 198 | entry->index = index; |
| 199 | entry->lo = msr.lo; |
| 200 | entry->hi = msr.hi; |
| 201 | |
| 202 | /* Return the next entry */ |
| 203 | entry++; |
| 204 | return entry; |
| 205 | } |
| 206 | |
| 207 | static int save_bsp_msrs(char *start, int size) |
| 208 | { |
| 209 | int msr_count; |
| 210 | int num_var_mtrrs; |
| 211 | struct saved_msr *msr_entry; |
| 212 | int i; |
| 213 | msr_t msr; |
| 214 | |
| 215 | /* Determine number of MTRRs need to be saved */ |
| 216 | msr = msr_read(MTRR_CAP_MSR); |
| 217 | num_var_mtrrs = msr.lo & 0xff; |
| 218 | |
| 219 | /* 2 * num_var_mtrrs for base and mask. +1 for IA32_MTRR_DEF_TYPE */ |
| 220 | msr_count = 2 * num_var_mtrrs + NUM_FIXED_MTRRS + 1; |
| 221 | |
| 222 | if ((msr_count * sizeof(struct saved_msr)) > size) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 223 | printf("Cannot mirror all %d msrs\n", msr_count); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 224 | return -ENOSPC; |
| 225 | } |
| 226 | |
| 227 | msr_entry = (void *)start; |
| 228 | for (i = 0; i < NUM_FIXED_MTRRS; i++) |
| 229 | msr_entry = save_msr(fixed_mtrrs[i], msr_entry); |
| 230 | |
| 231 | for (i = 0; i < num_var_mtrrs; i++) { |
| 232 | msr_entry = save_msr(MTRR_PHYS_BASE_MSR(i), msr_entry); |
| 233 | msr_entry = save_msr(MTRR_PHYS_MASK_MSR(i), msr_entry); |
| 234 | } |
| 235 | |
| 236 | msr_entry = save_msr(MTRR_DEF_TYPE_MSR, msr_entry); |
| 237 | |
| 238 | return msr_count; |
| 239 | } |
| 240 | |
Miao Yan | 6067762 | 2016-01-07 01:32:03 -0800 | [diff] [blame] | 241 | static int load_sipi_vector(atomic_t **ap_countp, int num_cpus) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 242 | { |
| 243 | struct sipi_params_16bit *params16; |
| 244 | struct sipi_params *params; |
| 245 | static char msr_save[512]; |
| 246 | char *stack; |
| 247 | ulong addr; |
| 248 | int code_len; |
| 249 | int size; |
| 250 | int ret; |
| 251 | |
| 252 | /* Copy in the code */ |
| 253 | code_len = ap_start16_code_end - ap_start16; |
| 254 | debug("Copying SIPI code to %x: %d bytes\n", AP_DEFAULT_BASE, |
| 255 | code_len); |
| 256 | memcpy((void *)AP_DEFAULT_BASE, ap_start16, code_len); |
| 257 | |
| 258 | addr = AP_DEFAULT_BASE + (ulong)sipi_params_16bit - (ulong)ap_start16; |
| 259 | params16 = (struct sipi_params_16bit *)addr; |
| 260 | params16->ap_start = (uint32_t)ap_start; |
| 261 | params16->gdt = (uint32_t)gd->arch.gdt; |
| 262 | params16->gdt_limit = X86_GDT_SIZE - 1; |
| 263 | debug("gdt = %x, gdt_limit = %x\n", params16->gdt, params16->gdt_limit); |
| 264 | |
| 265 | params = (struct sipi_params *)sipi_params; |
| 266 | debug("SIPI 32-bit params at %p\n", params); |
| 267 | params->idt_ptr = (uint32_t)x86_get_idt(); |
| 268 | |
| 269 | params->stack_size = CONFIG_AP_STACK_SIZE; |
Miao Yan | 6067762 | 2016-01-07 01:32:03 -0800 | [diff] [blame] | 270 | size = params->stack_size * num_cpus; |
Stephen Warren | 5923b59 | 2016-02-12 14:27:56 -0700 | [diff] [blame] | 271 | stack = memalign(4096, size); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 272 | if (!stack) |
| 273 | return -ENOMEM; |
| 274 | params->stack_top = (u32)(stack + size); |
Andy Shevchenko | 43b3ac5 | 2017-02-17 16:49:00 +0300 | [diff] [blame] | 275 | #if !defined(CONFIG_QEMU) && !defined(CONFIG_HAVE_FSP) && \ |
| 276 | !defined(CONFIG_INTEL_MID) |
Simon Glass | 8dda587 | 2016-03-11 22:07:11 -0700 | [diff] [blame] | 277 | params->microcode_ptr = ucode_base; |
| 278 | debug("Microcode at %x\n", params->microcode_ptr); |
| 279 | #endif |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 280 | params->msr_table_ptr = (u32)msr_save; |
| 281 | ret = save_bsp_msrs(msr_save, sizeof(msr_save)); |
| 282 | if (ret < 0) |
| 283 | return ret; |
| 284 | params->msr_count = ret; |
| 285 | |
| 286 | params->c_handler = (uint32_t)&ap_init; |
| 287 | |
| 288 | *ap_countp = ¶ms->ap_count; |
| 289 | atomic_set(*ap_countp, 0); |
| 290 | debug("SIPI vector is ready\n"); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int check_cpu_devices(int expected_cpus) |
| 296 | { |
| 297 | int i; |
| 298 | |
| 299 | for (i = 0; i < expected_cpus; i++) { |
| 300 | struct udevice *dev; |
| 301 | int ret; |
| 302 | |
| 303 | ret = uclass_find_device(UCLASS_CPU, i, &dev); |
| 304 | if (ret) { |
| 305 | debug("Cannot find CPU %d in device tree\n", i); |
| 306 | return ret; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /* Returns 1 for timeout. 0 on success */ |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 314 | static int apic_wait_timeout(int total_delay, const char *msg) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 315 | { |
| 316 | int total = 0; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 317 | |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 318 | if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) |
| 319 | return 0; |
| 320 | |
| 321 | debug("Waiting for %s...", msg); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 322 | while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 323 | udelay(50); |
| 324 | total += 50; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 325 | if (total >= total_delay) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 326 | debug("timed out: aborting\n"); |
| 327 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 328 | } |
| 329 | } |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 330 | debug("done\n"); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 331 | |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 332 | return 0; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 333 | } |
| 334 | |
Simon Glass | a3ee7b8 | 2020-07-17 08:48:10 -0600 | [diff] [blame] | 335 | /** |
| 336 | * start_aps() - Start up the APs and count how many we find |
| 337 | * |
| 338 | * This is called on the boot processor to start up all the other processors |
| 339 | * (here called APs). |
| 340 | * |
| 341 | * @num_aps: Number of APs we expect to find |
| 342 | * @ap_count: Initially zero. Incremented by this function for each AP found |
| 343 | * @return 0 if all APs were set up correctly or there are none to set up, |
| 344 | * -ENOSPC if the SIPI vector is too high in memory, |
| 345 | * -ETIMEDOUT if the ICR is busy or the second SIPI fails to complete |
| 346 | * -EIO if not all APs check in correctly |
| 347 | */ |
| 348 | static int start_aps(int num_aps, atomic_t *ap_count) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 349 | { |
| 350 | int sipi_vector; |
| 351 | /* Max location is 4KiB below 1MiB */ |
| 352 | const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12; |
| 353 | |
Simon Glass | a3ee7b8 | 2020-07-17 08:48:10 -0600 | [diff] [blame] | 354 | if (num_aps == 0) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | /* The vector is sent as a 4k aligned address in one byte */ |
| 358 | sipi_vector = AP_DEFAULT_BASE >> 12; |
| 359 | |
| 360 | if (sipi_vector > max_vector_loc) { |
| 361 | printf("SIPI vector too large! 0x%08x\n", |
| 362 | sipi_vector); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 363 | return -ENOSPC; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 364 | } |
| 365 | |
Simon Glass | a3ee7b8 | 2020-07-17 08:48:10 -0600 | [diff] [blame] | 366 | debug("Attempting to start %d APs\n", num_aps); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 367 | |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 368 | if (apic_wait_timeout(1000, "ICR not to be busy")) |
| 369 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 370 | |
| 371 | /* Send INIT IPI to all but self */ |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 372 | lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0)); |
| 373 | lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | |
| 374 | LAPIC_DM_INIT); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 375 | debug("Waiting for 10ms after sending INIT\n"); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 376 | mdelay(10); |
| 377 | |
| 378 | /* Send 1st SIPI */ |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 379 | if (apic_wait_timeout(1000, "ICR not to be busy")) |
| 380 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 381 | |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 382 | lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0)); |
| 383 | lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | |
| 384 | LAPIC_DM_STARTUP | sipi_vector); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 385 | if (apic_wait_timeout(10000, "first SIPI to complete")) |
| 386 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 387 | |
| 388 | /* Wait for CPUs to check in up to 200 us */ |
Simon Glass | a3ee7b8 | 2020-07-17 08:48:10 -0600 | [diff] [blame] | 389 | wait_for_aps(ap_count, num_aps, 200, 15); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 390 | |
| 391 | /* Send 2nd SIPI */ |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 392 | if (apic_wait_timeout(1000, "ICR not to be busy")) |
| 393 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 394 | |
Bin Meng | e5d0500 | 2015-06-23 12:18:50 +0800 | [diff] [blame] | 395 | lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0)); |
| 396 | lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | |
| 397 | LAPIC_DM_STARTUP | sipi_vector); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 398 | if (apic_wait_timeout(10000, "second SIPI to complete")) |
| 399 | return -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 400 | |
| 401 | /* Wait for CPUs to check in */ |
Simon Glass | a3ee7b8 | 2020-07-17 08:48:10 -0600 | [diff] [blame] | 402 | if (wait_for_aps(ap_count, num_aps, 10000, 50)) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 403 | debug("Not all APs checked in: %d/%d\n", |
Simon Glass | a3ee7b8 | 2020-07-17 08:48:10 -0600 | [diff] [blame] | 404 | atomic_read(ap_count), num_aps); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 405 | return -EIO; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Simon Glass | 3307d0c | 2020-07-17 08:48:11 -0600 | [diff] [blame] | 411 | /** |
| 412 | * bsp_do_flight_plan() - Do the flight plan on the BSP |
| 413 | * |
| 414 | * This runs the flight plan on the main CPU used to boot U-Boot |
| 415 | * |
| 416 | * @cpu: Device for the main CPU |
| 417 | * @plan: Flight plan to run |
| 418 | * @num_aps: Number of APs (CPUs other than the BSP) |
| 419 | * @returns 0 on success, -ETIMEDOUT if an AP failed to come up |
| 420 | */ |
| 421 | static int bsp_do_flight_plan(struct udevice *cpu, struct mp_flight_plan *plan, |
| 422 | int num_aps) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 423 | { |
| 424 | int i; |
| 425 | int ret = 0; |
| 426 | const int timeout_us = 100000; |
| 427 | const int step_us = 100; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 428 | |
Simon Glass | e40633d | 2020-07-17 08:48:08 -0600 | [diff] [blame] | 429 | for (i = 0; i < plan->num_records; i++) { |
| 430 | struct mp_flight_record *rec = &plan->records[i]; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 431 | |
| 432 | /* Wait for APs if the record is not released */ |
| 433 | if (atomic_read(&rec->barrier) == 0) { |
| 434 | /* Wait for the APs to check in */ |
| 435 | if (wait_for_aps(&rec->cpus_entered, num_aps, |
| 436 | timeout_us, step_us)) { |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 437 | debug("MP record %d timeout\n", i); |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 438 | ret = -ETIMEDOUT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | if (rec->bsp_call != NULL) |
| 443 | rec->bsp_call(cpu, rec->bsp_arg); |
| 444 | |
| 445 | release_barrier(&rec->barrier); |
| 446 | } |
Simon Glass | 3307d0c | 2020-07-17 08:48:11 -0600 | [diff] [blame] | 447 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 448 | return ret; |
| 449 | } |
| 450 | |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 451 | /** |
| 452 | * get_bsp() - Get information about the bootstrap processor |
| 453 | * |
| 454 | * @devp: If non-NULL, returns CPU device corresponding to the BSP |
| 455 | * @cpu_countp: If non-NULL, returns the total number of CPUs |
| 456 | * @return CPU number of the BSP, or -ve on error. If multiprocessing is not |
| 457 | * enabled, returns 0 |
| 458 | */ |
| 459 | static int get_bsp(struct udevice **devp, int *cpu_countp) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 460 | { |
| 461 | char processor_name[CPU_MAX_NAME_LEN]; |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 462 | struct udevice *dev; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 463 | int apic_id; |
| 464 | int ret; |
| 465 | |
| 466 | cpu_get_name(processor_name); |
Simon Glass | 17dbe89 | 2016-03-06 19:28:22 -0700 | [diff] [blame] | 467 | debug("CPU: %s\n", processor_name); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 468 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 469 | apic_id = lapicid(); |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 470 | ret = find_cpu_by_apic_id(apic_id, &dev); |
| 471 | if (ret < 0) { |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 472 | printf("Cannot find boot CPU, APIC ID %d\n", apic_id); |
| 473 | return ret; |
| 474 | } |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 475 | ret = cpu_get_count(dev); |
| 476 | if (ret < 0) |
| 477 | return log_msg_ret("count", ret); |
| 478 | if (devp) |
| 479 | *devp = dev; |
| 480 | if (cpu_countp) |
| 481 | *cpu_countp = ret; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 482 | |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 483 | return dev->req_seq >= 0 ? dev->req_seq : 0; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 484 | } |
| 485 | |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 486 | /** |
| 487 | * read_callback() - Read the pointer in a callback slot |
| 488 | * |
| 489 | * This is called by APs to read their callback slot to see if there is a |
| 490 | * pointer to new instructions |
| 491 | * |
| 492 | * @slot: Pointer to the AP's callback slot |
| 493 | * @return value of that pointer |
| 494 | */ |
| 495 | static struct mp_callback *read_callback(struct mp_callback **slot) |
| 496 | { |
| 497 | dmb(); |
| 498 | |
| 499 | return *slot; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * store_callback() - Store a pointer to the callback slot |
| 504 | * |
| 505 | * This is called by APs to write NULL into the callback slot when they have |
| 506 | * finished the work requested by the BSP. |
| 507 | * |
| 508 | * @slot: Pointer to the AP's callback slot |
| 509 | * @val: Value to write (e.g. NULL) |
| 510 | */ |
| 511 | static void store_callback(struct mp_callback **slot, struct mp_callback *val) |
| 512 | { |
| 513 | *slot = val; |
| 514 | dmb(); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * ap_wait_for_instruction() - Wait for and process requests from the main CPU |
| 519 | * |
| 520 | * This is called by APs (here, everything other than the main boot CPU) to |
| 521 | * await instructions. They arrive in the form of a function call and argument, |
| 522 | * which is then called. This uses a simple mailbox with atomic read/set |
| 523 | * |
| 524 | * @cpu: CPU that is waiting |
| 525 | * @unused: Optional argument provided by struct mp_flight_record, not used here |
| 526 | * @return Does not return |
| 527 | */ |
| 528 | static int ap_wait_for_instruction(struct udevice *cpu, void *unused) |
| 529 | { |
| 530 | struct mp_callback lcb; |
| 531 | struct mp_callback **per_cpu_slot; |
| 532 | |
| 533 | if (!IS_ENABLED(CONFIG_SMP_AP_WORK)) |
| 534 | return 0; |
| 535 | |
| 536 | per_cpu_slot = &ap_callbacks[cpu->req_seq]; |
| 537 | |
| 538 | while (1) { |
| 539 | struct mp_callback *cb = read_callback(per_cpu_slot); |
| 540 | |
| 541 | if (!cb) { |
| 542 | asm ("pause"); |
| 543 | continue; |
| 544 | } |
| 545 | |
| 546 | /* Copy to local variable before using the value */ |
| 547 | memcpy(&lcb, cb, sizeof(lcb)); |
| 548 | mfence(); |
| 549 | if (lcb.logical_cpu_number == MP_SELECT_ALL || |
| 550 | lcb.logical_cpu_number == MP_SELECT_APS || |
| 551 | cpu->req_seq == lcb.logical_cpu_number) |
| 552 | lcb.func(lcb.arg); |
| 553 | |
| 554 | /* Indicate we are finished */ |
| 555 | store_callback(per_cpu_slot, NULL); |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Simon Glass | 35ee0de | 2020-07-17 08:48:09 -0600 | [diff] [blame] | 561 | static int mp_init_cpu(struct udevice *cpu, void *unused) |
| 562 | { |
| 563 | struct cpu_platdata *plat = dev_get_parent_platdata(cpu); |
| 564 | |
Simon Glass | 35ee0de | 2020-07-17 08:48:09 -0600 | [diff] [blame] | 565 | plat->ucode_version = microcode_read_rev(); |
| 566 | plat->device_id = gd->arch.x86_device; |
| 567 | |
| 568 | return device_probe(cpu); |
| 569 | } |
| 570 | |
| 571 | static struct mp_flight_record mp_steps[] = { |
| 572 | MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL), |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 573 | MP_FR_BLOCK_APS(ap_wait_for_instruction, NULL, NULL, NULL), |
Simon Glass | 35ee0de | 2020-07-17 08:48:09 -0600 | [diff] [blame] | 574 | }; |
| 575 | |
Simon Glass | e40633d | 2020-07-17 08:48:08 -0600 | [diff] [blame] | 576 | int mp_init(void) |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 577 | { |
Simon Glass | 3307d0c | 2020-07-17 08:48:11 -0600 | [diff] [blame] | 578 | int num_aps, num_cpus; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 579 | atomic_t *ap_count; |
| 580 | struct udevice *cpu; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 581 | struct uclass *uc; |
Simon Glass | ebb239a | 2020-07-17 08:48:13 -0600 | [diff] [blame] | 582 | int ret; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 583 | |
Simon Glass | 4c8243d | 2019-12-06 21:42:55 -0700 | [diff] [blame] | 584 | if (IS_ENABLED(CONFIG_QFW)) { |
| 585 | ret = qemu_cpu_fixup(); |
| 586 | if (ret) |
| 587 | return ret; |
| 588 | } |
Miao Yan | 35f54b2 | 2016-01-07 01:32:04 -0800 | [diff] [blame] | 589 | |
Simon Glass | ebb239a | 2020-07-17 08:48:13 -0600 | [diff] [blame] | 590 | /* |
| 591 | * Multiple APs are brought up simultaneously and they may get the same |
| 592 | * seq num in the uclass_resolve_seq() during device_probe(). To avoid |
| 593 | * this, set req_seq to the reg number in the device tree in advance. |
| 594 | */ |
| 595 | uclass_id_foreach_dev(UCLASS_CPU, cpu, uc) |
| 596 | cpu->req_seq = dev_read_u32_default(cpu, "reg", -1); |
| 597 | |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 598 | ret = get_bsp(&cpu, &num_cpus); |
| 599 | if (ret < 0) { |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 600 | debug("Cannot init boot CPU: err=%d\n", ret); |
| 601 | return ret; |
| 602 | } |
| 603 | |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 604 | if (num_cpus < 2) |
| 605 | debug("Warning: Only 1 CPU is detected\n"); |
| 606 | |
| 607 | ret = check_cpu_devices(num_cpus); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 608 | if (ret) |
Simon Glass | 653781b | 2020-07-17 08:48:14 -0600 | [diff] [blame] | 609 | log_warning("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n"); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 610 | |
Simon Glass | 4a30bbb | 2020-07-17 08:48:16 -0600 | [diff] [blame] | 611 | ap_callbacks = calloc(num_cpus, sizeof(struct mp_callback *)); |
| 612 | if (!ap_callbacks) |
| 613 | return -ENOMEM; |
| 614 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 615 | /* Copy needed parameters so that APs have a reference to the plan */ |
Simon Glass | e40633d | 2020-07-17 08:48:08 -0600 | [diff] [blame] | 616 | mp_info.num_records = ARRAY_SIZE(mp_steps); |
| 617 | mp_info.records = mp_steps; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 618 | |
| 619 | /* Load the SIPI vector */ |
Miao Yan | 6067762 | 2016-01-07 01:32:03 -0800 | [diff] [blame] | 620 | ret = load_sipi_vector(&ap_count, num_cpus); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 621 | if (ap_count == NULL) |
Simon Glass | f9b5800 | 2019-04-25 21:58:41 -0600 | [diff] [blame] | 622 | return -ENOENT; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Make sure SIPI data hits RAM so the APs that come up will see |
| 626 | * the startup code even if the caches are disabled |
| 627 | */ |
| 628 | wbinvd(); |
| 629 | |
| 630 | /* Start the APs providing number of APs and the cpus_entered field */ |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 631 | num_aps = num_cpus - 1; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 632 | ret = start_aps(num_aps, ap_count); |
| 633 | if (ret) { |
| 634 | mdelay(1000); |
| 635 | debug("%d/%d eventually checked in?\n", atomic_read(ap_count), |
| 636 | num_aps); |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | /* Walk the flight plan for the BSP */ |
Simon Glass | 3307d0c | 2020-07-17 08:48:11 -0600 | [diff] [blame] | 641 | ret = bsp_do_flight_plan(cpu, &mp_info, num_aps); |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 642 | if (ret) { |
| 643 | debug("CPU init failed: err=%d\n", ret); |
| 644 | return ret; |
| 645 | } |
Simon Glass | 4943de6 | 2020-07-17 08:48:18 -0600 | [diff] [blame^] | 646 | gd->flags |= GD_FLG_SMP_READY; |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 647 | |
| 648 | return 0; |
| 649 | } |