blob: 69a23829b9cefe38259b7021fe16c78be7bcd242 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa9a44262015-04-29 22:25:59 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 *
Simon Glassa9a44262015-04-29 22:25:59 -06005 * 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 Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassa9a44262015-04-29 22:25:59 -060013#include <malloc.h>
Miao Yan92106272016-05-22 19:37:17 -070014#include <qfw.h>
Simon Glassa9a44262015-04-29 22:25:59 -060015#include <asm/atomic.h>
16#include <asm/cpu.h>
17#include <asm/interrupt.h>
Simon Glass4a30bbb2020-07-17 08:48:16 -060018#include <asm/io.h>
Simon Glassa9a44262015-04-29 22:25:59 -060019#include <asm/lapic.h>
Simon Glassc17d4502016-03-11 22:07:09 -070020#include <asm/microcode.h>
Simon Glassa9a44262015-04-29 22:25:59 -060021#include <asm/mp.h>
Bin Menge5d05002015-06-23 12:18:50 +080022#include <asm/msr.h>
Simon Glassa9a44262015-04-29 22:25:59 -060023#include <asm/mtrr.h>
Bin Menge5d05002015-06-23 12:18:50 +080024#include <asm/processor.h>
Simon Glassa9a44262015-04-29 22:25:59 -060025#include <asm/sipi.h>
26#include <dm/device-internal.h>
27#include <dm/uclass-internal.h>
Miao Yan35f54b22016-01-07 01:32:04 -080028#include <dm/lists.h>
29#include <dm/root.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Simon Glassa9a44262015-04-29 22:25:59 -060031#include <linux/linkage.h>
32
Simon Glassdaa93d92015-07-31 09:31:31 -060033DECLARE_GLOBAL_DATA_PTR;
34
Simon Glassa9a44262015-04-29 22:25:59 -060035/* This also needs to match the sipi.S assembly code for saved MSR encoding */
36struct saved_msr {
37 uint32_t index;
38 uint32_t lo;
39 uint32_t hi;
40} __packed;
41
Simon Glassa9a44262015-04-29 22:25:59 -060042struct mp_flight_plan {
43 int num_records;
44 struct mp_flight_record *records;
45};
46
Simon Glass4a30bbb2020-07-17 08:48:16 -060047/**
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 */
56struct 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 Glassa9a44262015-04-29 22:25:59 -060067static struct mp_flight_plan mp_info;
68
Simon Glass4a30bbb2020-07-17 08:48:16 -060069/*
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 */
78static struct mp_callback **ap_callbacks;
Simon Glassa9a44262015-04-29 22:25:59 -060079
80static inline void barrier_wait(atomic_t *b)
81{
82 while (atomic_read(b) == 0)
83 asm("pause");
84 mfence();
85}
86
87static inline void release_barrier(atomic_t *b)
88{
89 mfence();
90 atomic_set(b, 1);
91}
92
Bin Menge5d05002015-06-23 12:18:50 +080093static 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 Glassa9a44262015-04-29 22:25:59 -0600100/* Returns 1 if timeout waiting for APs. 0 if target APs found */
101static 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
119static 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 Yan2ee10002016-01-07 01:32:02 -0800134static int find_cpu_by_apic_id(int apic_id, struct udevice **devp)
Simon Glassa9a44262015-04-29 22:25:59 -0600135{
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 */
157static 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 Yan2ee10002016-01-07 01:32:02 -0800167 ret = find_cpu_by_apic_id(apic_id, &dev);
Simon Glassa9a44262015-04-29 22:25:59 -0600168 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 Glass4a30bbb2020-07-17 08:48:16 -0600176 /*
177 * Walk the flight plan, which only returns if CONFIG_SMP_AP_WORK is not
178 * enabled
179 */
Simon Glassa9a44262015-04-29 22:25:59 -0600180 ap_do_flight_plan(dev);
181
Simon Glassa9a44262015-04-29 22:25:59 -0600182done:
183 stop_this_cpu();
184}
185
186static 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
193static 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
207static 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 Glass17dbe892016-03-06 19:28:22 -0700223 printf("Cannot mirror all %d msrs\n", msr_count);
Simon Glassa9a44262015-04-29 22:25:59 -0600224 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 Yan60677622016-01-07 01:32:03 -0800241static int load_sipi_vector(atomic_t **ap_countp, int num_cpus)
Simon Glassa9a44262015-04-29 22:25:59 -0600242{
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 Yan60677622016-01-07 01:32:03 -0800270 size = params->stack_size * num_cpus;
Stephen Warren5923b592016-02-12 14:27:56 -0700271 stack = memalign(4096, size);
Simon Glassa9a44262015-04-29 22:25:59 -0600272 if (!stack)
273 return -ENOMEM;
274 params->stack_top = (u32)(stack + size);
Andy Shevchenko43b3ac52017-02-17 16:49:00 +0300275#if !defined(CONFIG_QEMU) && !defined(CONFIG_HAVE_FSP) && \
276 !defined(CONFIG_INTEL_MID)
Simon Glass8dda5872016-03-11 22:07:11 -0700277 params->microcode_ptr = ucode_base;
278 debug("Microcode at %x\n", params->microcode_ptr);
279#endif
Simon Glassa9a44262015-04-29 22:25:59 -0600280 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 = &params->ap_count;
289 atomic_set(*ap_countp, 0);
290 debug("SIPI vector is ready\n");
291
292 return 0;
293}
294
295static 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 Glass17dbe892016-03-06 19:28:22 -0700314static int apic_wait_timeout(int total_delay, const char *msg)
Simon Glassa9a44262015-04-29 22:25:59 -0600315{
316 int total = 0;
Simon Glassa9a44262015-04-29 22:25:59 -0600317
Simon Glass17dbe892016-03-06 19:28:22 -0700318 if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY))
319 return 0;
320
321 debug("Waiting for %s...", msg);
Simon Glassa9a44262015-04-29 22:25:59 -0600322 while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
Simon Glass17dbe892016-03-06 19:28:22 -0700323 udelay(50);
324 total += 50;
Simon Glassa9a44262015-04-29 22:25:59 -0600325 if (total >= total_delay) {
Simon Glass17dbe892016-03-06 19:28:22 -0700326 debug("timed out: aborting\n");
327 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600328 }
329 }
Simon Glass17dbe892016-03-06 19:28:22 -0700330 debug("done\n");
Simon Glassa9a44262015-04-29 22:25:59 -0600331
Simon Glass17dbe892016-03-06 19:28:22 -0700332 return 0;
Simon Glassa9a44262015-04-29 22:25:59 -0600333}
334
Simon Glassa3ee7b82020-07-17 08:48:10 -0600335/**
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 */
348static int start_aps(int num_aps, atomic_t *ap_count)
Simon Glassa9a44262015-04-29 22:25:59 -0600349{
350 int sipi_vector;
351 /* Max location is 4KiB below 1MiB */
352 const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
353
Simon Glassa3ee7b82020-07-17 08:48:10 -0600354 if (num_aps == 0)
Simon Glassa9a44262015-04-29 22:25:59 -0600355 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 Glassf9b58002019-04-25 21:58:41 -0600363 return -ENOSPC;
Simon Glassa9a44262015-04-29 22:25:59 -0600364 }
365
Simon Glassa3ee7b82020-07-17 08:48:10 -0600366 debug("Attempting to start %d APs\n", num_aps);
Simon Glassa9a44262015-04-29 22:25:59 -0600367
Simon Glass17dbe892016-03-06 19:28:22 -0700368 if (apic_wait_timeout(1000, "ICR not to be busy"))
369 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600370
371 /* Send INIT IPI to all but self */
Bin Menge5d05002015-06-23 12:18:50 +0800372 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 Glass17dbe892016-03-06 19:28:22 -0700375 debug("Waiting for 10ms after sending INIT\n");
Simon Glassa9a44262015-04-29 22:25:59 -0600376 mdelay(10);
377
378 /* Send 1st SIPI */
Simon Glass17dbe892016-03-06 19:28:22 -0700379 if (apic_wait_timeout(1000, "ICR not to be busy"))
380 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600381
Bin Menge5d05002015-06-23 12:18:50 +0800382 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 Glass17dbe892016-03-06 19:28:22 -0700385 if (apic_wait_timeout(10000, "first SIPI to complete"))
386 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600387
388 /* Wait for CPUs to check in up to 200 us */
Simon Glassa3ee7b82020-07-17 08:48:10 -0600389 wait_for_aps(ap_count, num_aps, 200, 15);
Simon Glassa9a44262015-04-29 22:25:59 -0600390
391 /* Send 2nd SIPI */
Simon Glass17dbe892016-03-06 19:28:22 -0700392 if (apic_wait_timeout(1000, "ICR not to be busy"))
393 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600394
Bin Menge5d05002015-06-23 12:18:50 +0800395 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 Glass17dbe892016-03-06 19:28:22 -0700398 if (apic_wait_timeout(10000, "second SIPI to complete"))
399 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600400
401 /* Wait for CPUs to check in */
Simon Glassa3ee7b82020-07-17 08:48:10 -0600402 if (wait_for_aps(ap_count, num_aps, 10000, 50)) {
Simon Glass17dbe892016-03-06 19:28:22 -0700403 debug("Not all APs checked in: %d/%d\n",
Simon Glassa3ee7b82020-07-17 08:48:10 -0600404 atomic_read(ap_count), num_aps);
Simon Glassf9b58002019-04-25 21:58:41 -0600405 return -EIO;
Simon Glassa9a44262015-04-29 22:25:59 -0600406 }
407
408 return 0;
409}
410
Simon Glass3307d0c2020-07-17 08:48:11 -0600411/**
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 */
421static int bsp_do_flight_plan(struct udevice *cpu, struct mp_flight_plan *plan,
422 int num_aps)
Simon Glassa9a44262015-04-29 22:25:59 -0600423{
424 int i;
425 int ret = 0;
426 const int timeout_us = 100000;
427 const int step_us = 100;
Simon Glassa9a44262015-04-29 22:25:59 -0600428
Simon Glasse40633d2020-07-17 08:48:08 -0600429 for (i = 0; i < plan->num_records; i++) {
430 struct mp_flight_record *rec = &plan->records[i];
Simon Glassa9a44262015-04-29 22:25:59 -0600431
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 Glass17dbe892016-03-06 19:28:22 -0700437 debug("MP record %d timeout\n", i);
Simon Glassf9b58002019-04-25 21:58:41 -0600438 ret = -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600439 }
440 }
441
442 if (rec->bsp_call != NULL)
443 rec->bsp_call(cpu, rec->bsp_arg);
444
445 release_barrier(&rec->barrier);
446 }
Simon Glass3307d0c2020-07-17 08:48:11 -0600447
Simon Glassa9a44262015-04-29 22:25:59 -0600448 return ret;
449}
450
Simon Glass653781b2020-07-17 08:48:14 -0600451/**
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 */
459static int get_bsp(struct udevice **devp, int *cpu_countp)
Simon Glassa9a44262015-04-29 22:25:59 -0600460{
461 char processor_name[CPU_MAX_NAME_LEN];
Simon Glass653781b2020-07-17 08:48:14 -0600462 struct udevice *dev;
Simon Glassa9a44262015-04-29 22:25:59 -0600463 int apic_id;
464 int ret;
465
466 cpu_get_name(processor_name);
Simon Glass17dbe892016-03-06 19:28:22 -0700467 debug("CPU: %s\n", processor_name);
Simon Glassa9a44262015-04-29 22:25:59 -0600468
Simon Glassa9a44262015-04-29 22:25:59 -0600469 apic_id = lapicid();
Simon Glass653781b2020-07-17 08:48:14 -0600470 ret = find_cpu_by_apic_id(apic_id, &dev);
471 if (ret < 0) {
Simon Glassa9a44262015-04-29 22:25:59 -0600472 printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
473 return ret;
474 }
Simon Glass653781b2020-07-17 08:48:14 -0600475 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 Glassa9a44262015-04-29 22:25:59 -0600482
Simon Glass653781b2020-07-17 08:48:14 -0600483 return dev->req_seq >= 0 ? dev->req_seq : 0;
Simon Glassa9a44262015-04-29 22:25:59 -0600484}
485
Simon Glass4a30bbb2020-07-17 08:48:16 -0600486/**
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 */
495static 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 */
511static 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 */
528static 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 Glass35ee0de2020-07-17 08:48:09 -0600561static int mp_init_cpu(struct udevice *cpu, void *unused)
562{
563 struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
564
Simon Glass35ee0de2020-07-17 08:48:09 -0600565 plat->ucode_version = microcode_read_rev();
566 plat->device_id = gd->arch.x86_device;
567
568 return device_probe(cpu);
569}
570
571static struct mp_flight_record mp_steps[] = {
572 MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
Simon Glass4a30bbb2020-07-17 08:48:16 -0600573 MP_FR_BLOCK_APS(ap_wait_for_instruction, NULL, NULL, NULL),
Simon Glass35ee0de2020-07-17 08:48:09 -0600574};
575
Simon Glasse40633d2020-07-17 08:48:08 -0600576int mp_init(void)
Simon Glassa9a44262015-04-29 22:25:59 -0600577{
Simon Glass3307d0c2020-07-17 08:48:11 -0600578 int num_aps, num_cpus;
Simon Glassa9a44262015-04-29 22:25:59 -0600579 atomic_t *ap_count;
580 struct udevice *cpu;
Simon Glassa9a44262015-04-29 22:25:59 -0600581 struct uclass *uc;
Simon Glassebb239a2020-07-17 08:48:13 -0600582 int ret;
Simon Glassa9a44262015-04-29 22:25:59 -0600583
Simon Glass4c8243d2019-12-06 21:42:55 -0700584 if (IS_ENABLED(CONFIG_QFW)) {
585 ret = qemu_cpu_fixup();
586 if (ret)
587 return ret;
588 }
Miao Yan35f54b22016-01-07 01:32:04 -0800589
Simon Glassebb239a2020-07-17 08:48:13 -0600590 /*
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 Glass653781b2020-07-17 08:48:14 -0600598 ret = get_bsp(&cpu, &num_cpus);
599 if (ret < 0) {
Simon Glassa9a44262015-04-29 22:25:59 -0600600 debug("Cannot init boot CPU: err=%d\n", ret);
601 return ret;
602 }
603
Bin Mengf967f9a2015-06-17 11:15:36 +0800604 if (num_cpus < 2)
605 debug("Warning: Only 1 CPU is detected\n");
606
607 ret = check_cpu_devices(num_cpus);
Simon Glassa9a44262015-04-29 22:25:59 -0600608 if (ret)
Simon Glass653781b2020-07-17 08:48:14 -0600609 log_warning("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n");
Simon Glassa9a44262015-04-29 22:25:59 -0600610
Simon Glass4a30bbb2020-07-17 08:48:16 -0600611 ap_callbacks = calloc(num_cpus, sizeof(struct mp_callback *));
612 if (!ap_callbacks)
613 return -ENOMEM;
614
Simon Glassa9a44262015-04-29 22:25:59 -0600615 /* Copy needed parameters so that APs have a reference to the plan */
Simon Glasse40633d2020-07-17 08:48:08 -0600616 mp_info.num_records = ARRAY_SIZE(mp_steps);
617 mp_info.records = mp_steps;
Simon Glassa9a44262015-04-29 22:25:59 -0600618
619 /* Load the SIPI vector */
Miao Yan60677622016-01-07 01:32:03 -0800620 ret = load_sipi_vector(&ap_count, num_cpus);
Simon Glassa9a44262015-04-29 22:25:59 -0600621 if (ap_count == NULL)
Simon Glassf9b58002019-04-25 21:58:41 -0600622 return -ENOENT;
Simon Glassa9a44262015-04-29 22:25:59 -0600623
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 Mengf967f9a2015-06-17 11:15:36 +0800631 num_aps = num_cpus - 1;
Simon Glassa9a44262015-04-29 22:25:59 -0600632 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 Glass3307d0c2020-07-17 08:48:11 -0600641 ret = bsp_do_flight_plan(cpu, &mp_info, num_aps);
Simon Glassa9a44262015-04-29 22:25:59 -0600642 if (ret) {
643 debug("CPU init failed: err=%d\n", ret);
644 return ret;
645 }
Simon Glass4943de62020-07-17 08:48:18 -0600646 gd->flags |= GD_FLG_SMP_READY;
Simon Glassa9a44262015-04-29 22:25:59 -0600647
648 return 0;
649}