blob: 427ec8fc23fef91d9033385c3618a85c7da288f2 [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 {
Simon Glass6871dff2020-07-17 08:48:19 -060057 mp_run_func func;
Simon Glass4a30bbb2020-07-17 08:48:16 -060058 void *arg;
59 int logical_cpu_number;
60};
61
Simon Glassa9a44262015-04-29 22:25:59 -060062static struct mp_flight_plan mp_info;
63
Simon Glass4a30bbb2020-07-17 08:48:16 -060064/*
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 */
73static struct mp_callback **ap_callbacks;
Simon Glassa9a44262015-04-29 22:25:59 -060074
75static inline void barrier_wait(atomic_t *b)
76{
77 while (atomic_read(b) == 0)
78 asm("pause");
79 mfence();
80}
81
82static inline void release_barrier(atomic_t *b)
83{
84 mfence();
85 atomic_set(b, 1);
86}
87
Bin Menge5d05002015-06-23 12:18:50 +080088static 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 Glassa9a44262015-04-29 22:25:59 -060095/* Returns 1 if timeout waiting for APs. 0 if target APs found */
96static 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
114static 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 Yan2ee10002016-01-07 01:32:02 -0800129static int find_cpu_by_apic_id(int apic_id, struct udevice **devp)
Simon Glassa9a44262015-04-29 22:25:59 -0600130{
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 */
152static 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 Yan2ee10002016-01-07 01:32:02 -0800162 ret = find_cpu_by_apic_id(apic_id, &dev);
Simon Glassa9a44262015-04-29 22:25:59 -0600163 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 Glass4a30bbb2020-07-17 08:48:16 -0600171 /*
172 * Walk the flight plan, which only returns if CONFIG_SMP_AP_WORK is not
173 * enabled
174 */
Simon Glassa9a44262015-04-29 22:25:59 -0600175 ap_do_flight_plan(dev);
176
Simon Glassa9a44262015-04-29 22:25:59 -0600177done:
178 stop_this_cpu();
179}
180
181static 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
188static 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
202static 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 Glass17dbe892016-03-06 19:28:22 -0700218 printf("Cannot mirror all %d msrs\n", msr_count);
Simon Glassa9a44262015-04-29 22:25:59 -0600219 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 Yan60677622016-01-07 01:32:03 -0800236static int load_sipi_vector(atomic_t **ap_countp, int num_cpus)
Simon Glassa9a44262015-04-29 22:25:59 -0600237{
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 Yan60677622016-01-07 01:32:03 -0800265 size = params->stack_size * num_cpus;
Stephen Warren5923b592016-02-12 14:27:56 -0700266 stack = memalign(4096, size);
Simon Glassa9a44262015-04-29 22:25:59 -0600267 if (!stack)
268 return -ENOMEM;
269 params->stack_top = (u32)(stack + size);
Andy Shevchenko43b3ac52017-02-17 16:49:00 +0300270#if !defined(CONFIG_QEMU) && !defined(CONFIG_HAVE_FSP) && \
271 !defined(CONFIG_INTEL_MID)
Simon Glass8dda5872016-03-11 22:07:11 -0700272 params->microcode_ptr = ucode_base;
273 debug("Microcode at %x\n", params->microcode_ptr);
274#endif
Simon Glassa9a44262015-04-29 22:25:59 -0600275 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 = &params->ap_count;
284 atomic_set(*ap_countp, 0);
285 debug("SIPI vector is ready\n");
286
287 return 0;
288}
289
290static 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 Glass17dbe892016-03-06 19:28:22 -0700309static int apic_wait_timeout(int total_delay, const char *msg)
Simon Glassa9a44262015-04-29 22:25:59 -0600310{
311 int total = 0;
Simon Glassa9a44262015-04-29 22:25:59 -0600312
Simon Glass17dbe892016-03-06 19:28:22 -0700313 if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY))
314 return 0;
315
316 debug("Waiting for %s...", msg);
Simon Glassa9a44262015-04-29 22:25:59 -0600317 while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
Simon Glass17dbe892016-03-06 19:28:22 -0700318 udelay(50);
319 total += 50;
Simon Glassa9a44262015-04-29 22:25:59 -0600320 if (total >= total_delay) {
Simon Glass17dbe892016-03-06 19:28:22 -0700321 debug("timed out: aborting\n");
322 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600323 }
324 }
Simon Glass17dbe892016-03-06 19:28:22 -0700325 debug("done\n");
Simon Glassa9a44262015-04-29 22:25:59 -0600326
Simon Glass17dbe892016-03-06 19:28:22 -0700327 return 0;
Simon Glassa9a44262015-04-29 22:25:59 -0600328}
329
Simon Glassa3ee7b82020-07-17 08:48:10 -0600330/**
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 */
343static int start_aps(int num_aps, atomic_t *ap_count)
Simon Glassa9a44262015-04-29 22:25:59 -0600344{
345 int sipi_vector;
346 /* Max location is 4KiB below 1MiB */
347 const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
348
Simon Glassa3ee7b82020-07-17 08:48:10 -0600349 if (num_aps == 0)
Simon Glassa9a44262015-04-29 22:25:59 -0600350 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 Glassf9b58002019-04-25 21:58:41 -0600358 return -ENOSPC;
Simon Glassa9a44262015-04-29 22:25:59 -0600359 }
360
Simon Glassa3ee7b82020-07-17 08:48:10 -0600361 debug("Attempting to start %d APs\n", num_aps);
Simon Glassa9a44262015-04-29 22:25:59 -0600362
Simon Glass17dbe892016-03-06 19:28:22 -0700363 if (apic_wait_timeout(1000, "ICR not to be busy"))
364 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600365
366 /* Send INIT IPI to all but self */
Bin Menge5d05002015-06-23 12:18:50 +0800367 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 Glass17dbe892016-03-06 19:28:22 -0700370 debug("Waiting for 10ms after sending INIT\n");
Simon Glassa9a44262015-04-29 22:25:59 -0600371 mdelay(10);
372
373 /* Send 1st SIPI */
Simon Glass17dbe892016-03-06 19:28:22 -0700374 if (apic_wait_timeout(1000, "ICR not to be busy"))
375 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600376
Bin Menge5d05002015-06-23 12:18:50 +0800377 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 Glass17dbe892016-03-06 19:28:22 -0700380 if (apic_wait_timeout(10000, "first SIPI to complete"))
381 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600382
383 /* Wait for CPUs to check in up to 200 us */
Simon Glassa3ee7b82020-07-17 08:48:10 -0600384 wait_for_aps(ap_count, num_aps, 200, 15);
Simon Glassa9a44262015-04-29 22:25:59 -0600385
386 /* Send 2nd SIPI */
Simon Glass17dbe892016-03-06 19:28:22 -0700387 if (apic_wait_timeout(1000, "ICR not to be busy"))
388 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600389
Bin Menge5d05002015-06-23 12:18:50 +0800390 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 Glass17dbe892016-03-06 19:28:22 -0700393 if (apic_wait_timeout(10000, "second SIPI to complete"))
394 return -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600395
396 /* Wait for CPUs to check in */
Simon Glassa3ee7b82020-07-17 08:48:10 -0600397 if (wait_for_aps(ap_count, num_aps, 10000, 50)) {
Simon Glass17dbe892016-03-06 19:28:22 -0700398 debug("Not all APs checked in: %d/%d\n",
Simon Glassa3ee7b82020-07-17 08:48:10 -0600399 atomic_read(ap_count), num_aps);
Simon Glassf9b58002019-04-25 21:58:41 -0600400 return -EIO;
Simon Glassa9a44262015-04-29 22:25:59 -0600401 }
402
403 return 0;
404}
405
Simon Glass3307d0c2020-07-17 08:48:11 -0600406/**
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 */
416static int bsp_do_flight_plan(struct udevice *cpu, struct mp_flight_plan *plan,
417 int num_aps)
Simon Glassa9a44262015-04-29 22:25:59 -0600418{
419 int i;
420 int ret = 0;
421 const int timeout_us = 100000;
422 const int step_us = 100;
Simon Glassa9a44262015-04-29 22:25:59 -0600423
Simon Glasse40633d2020-07-17 08:48:08 -0600424 for (i = 0; i < plan->num_records; i++) {
425 struct mp_flight_record *rec = &plan->records[i];
Simon Glassa9a44262015-04-29 22:25:59 -0600426
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 Glass17dbe892016-03-06 19:28:22 -0700432 debug("MP record %d timeout\n", i);
Simon Glassf9b58002019-04-25 21:58:41 -0600433 ret = -ETIMEDOUT;
Simon Glassa9a44262015-04-29 22:25:59 -0600434 }
435 }
436
437 if (rec->bsp_call != NULL)
438 rec->bsp_call(cpu, rec->bsp_arg);
439
440 release_barrier(&rec->barrier);
441 }
Simon Glass3307d0c2020-07-17 08:48:11 -0600442
Simon Glassa9a44262015-04-29 22:25:59 -0600443 return ret;
444}
445
Simon Glass653781b2020-07-17 08:48:14 -0600446/**
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 */
454static int get_bsp(struct udevice **devp, int *cpu_countp)
Simon Glassa9a44262015-04-29 22:25:59 -0600455{
456 char processor_name[CPU_MAX_NAME_LEN];
Simon Glass653781b2020-07-17 08:48:14 -0600457 struct udevice *dev;
Simon Glassa9a44262015-04-29 22:25:59 -0600458 int apic_id;
459 int ret;
460
461 cpu_get_name(processor_name);
Simon Glass17dbe892016-03-06 19:28:22 -0700462 debug("CPU: %s\n", processor_name);
Simon Glassa9a44262015-04-29 22:25:59 -0600463
Simon Glassa9a44262015-04-29 22:25:59 -0600464 apic_id = lapicid();
Simon Glass653781b2020-07-17 08:48:14 -0600465 ret = find_cpu_by_apic_id(apic_id, &dev);
466 if (ret < 0) {
Simon Glassa9a44262015-04-29 22:25:59 -0600467 printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
468 return ret;
469 }
Simon Glass653781b2020-07-17 08:48:14 -0600470 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 Glassa9a44262015-04-29 22:25:59 -0600477
Simon Glass653781b2020-07-17 08:48:14 -0600478 return dev->req_seq >= 0 ? dev->req_seq : 0;
Simon Glassa9a44262015-04-29 22:25:59 -0600479}
480
Simon Glass4a30bbb2020-07-17 08:48:16 -0600481/**
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 */
490static 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 */
506static void store_callback(struct mp_callback **slot, struct mp_callback *val)
507{
508 *slot = val;
509 dmb();
510}
511
512/**
Simon Glass6871dff2020-07-17 08:48:19 -0600513 * 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 */
527static 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 Glass4a30bbb2020-07-17 08:48:16 -0600577 * 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 */
587static 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 Glass35ee0de2020-07-17 08:48:09 -0600620static int mp_init_cpu(struct udevice *cpu, void *unused)
621{
622 struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
623
Simon Glass35ee0de2020-07-17 08:48:09 -0600624 plat->ucode_version = microcode_read_rev();
625 plat->device_id = gd->arch.x86_device;
626
627 return device_probe(cpu);
628}
629
630static struct mp_flight_record mp_steps[] = {
631 MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
Simon Glass4a30bbb2020-07-17 08:48:16 -0600632 MP_FR_BLOCK_APS(ap_wait_for_instruction, NULL, NULL, NULL),
Simon Glass35ee0de2020-07-17 08:48:09 -0600633};
634
Simon Glass6871dff2020-07-17 08:48:19 -0600635int 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 Glass32d56952020-07-17 08:48:20 -0600671static void park_this_cpu(void *unused)
672{
673 stop_this_cpu();
674}
675
676int 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 Glasse40633d2020-07-17 08:48:08 -0600687int mp_init(void)
Simon Glassa9a44262015-04-29 22:25:59 -0600688{
Simon Glass3307d0c2020-07-17 08:48:11 -0600689 int num_aps, num_cpus;
Simon Glassa9a44262015-04-29 22:25:59 -0600690 atomic_t *ap_count;
691 struct udevice *cpu;
Simon Glassa9a44262015-04-29 22:25:59 -0600692 struct uclass *uc;
Simon Glassebb239a2020-07-17 08:48:13 -0600693 int ret;
Simon Glassa9a44262015-04-29 22:25:59 -0600694
Simon Glass4c8243d2019-12-06 21:42:55 -0700695 if (IS_ENABLED(CONFIG_QFW)) {
696 ret = qemu_cpu_fixup();
697 if (ret)
698 return ret;
699 }
Miao Yan35f54b22016-01-07 01:32:04 -0800700
Simon Glassebb239a2020-07-17 08:48:13 -0600701 /*
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 Glass653781b2020-07-17 08:48:14 -0600709 ret = get_bsp(&cpu, &num_cpus);
710 if (ret < 0) {
Simon Glassa9a44262015-04-29 22:25:59 -0600711 debug("Cannot init boot CPU: err=%d\n", ret);
712 return ret;
713 }
714
Bin Mengf967f9a2015-06-17 11:15:36 +0800715 if (num_cpus < 2)
716 debug("Warning: Only 1 CPU is detected\n");
717
718 ret = check_cpu_devices(num_cpus);
Simon Glassa9a44262015-04-29 22:25:59 -0600719 if (ret)
Simon Glass653781b2020-07-17 08:48:14 -0600720 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 -0600721
Simon Glass4a30bbb2020-07-17 08:48:16 -0600722 ap_callbacks = calloc(num_cpus, sizeof(struct mp_callback *));
723 if (!ap_callbacks)
724 return -ENOMEM;
725
Simon Glassa9a44262015-04-29 22:25:59 -0600726 /* Copy needed parameters so that APs have a reference to the plan */
Simon Glasse40633d2020-07-17 08:48:08 -0600727 mp_info.num_records = ARRAY_SIZE(mp_steps);
728 mp_info.records = mp_steps;
Simon Glassa9a44262015-04-29 22:25:59 -0600729
730 /* Load the SIPI vector */
Miao Yan60677622016-01-07 01:32:03 -0800731 ret = load_sipi_vector(&ap_count, num_cpus);
Simon Glassa9a44262015-04-29 22:25:59 -0600732 if (ap_count == NULL)
Simon Glassf9b58002019-04-25 21:58:41 -0600733 return -ENOENT;
Simon Glassa9a44262015-04-29 22:25:59 -0600734
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 Mengf967f9a2015-06-17 11:15:36 +0800742 num_aps = num_cpus - 1;
Simon Glassa9a44262015-04-29 22:25:59 -0600743 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 Glass3307d0c2020-07-17 08:48:11 -0600752 ret = bsp_do_flight_plan(cpu, &mp_info, num_aps);
Simon Glassa9a44262015-04-29 22:25:59 -0600753 if (ret) {
754 debug("CPU init failed: err=%d\n", ret);
755 return ret;
756 }
Simon Glass4943de62020-07-17 08:48:18 -0600757 gd->flags |= GD_FLG_SMP_READY;
Simon Glassa9a44262015-04-29 22:25:59 -0600758
759 return 0;
760}