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