blob: 8b00d57c880b216c3c34623a7b6a464b9ce90423 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass45b5a372015-04-29 22:25:59 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 *
Simon Glass45b5a372015-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 Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass45b5a372015-04-29 22:25:59 -060013#include <malloc.h>
Miao Yan18686592016-05-22 19:37:17 -070014#include <qfw.h>
Simon Glass45b5a372015-04-29 22:25:59 -060015#include <asm/atomic.h>
16#include <asm/cpu.h>
17#include <asm/interrupt.h>
18#include <asm/lapic.h>
Simon Glass6bcb6752016-03-11 22:07:09 -070019#include <asm/microcode.h>
Simon Glass45b5a372015-04-29 22:25:59 -060020#include <asm/mp.h>
Bin Menga2d73fd2015-06-23 12:18:50 +080021#include <asm/msr.h>
Simon Glass45b5a372015-04-29 22:25:59 -060022#include <asm/mtrr.h>
Bin Menga2d73fd2015-06-23 12:18:50 +080023#include <asm/processor.h>
Simon Glass45b5a372015-04-29 22:25:59 -060024#include <asm/sipi.h>
25#include <dm/device-internal.h>
26#include <dm/uclass-internal.h>
Miao Yande752c52016-01-07 01:32:04 -080027#include <dm/lists.h>
28#include <dm/root.h>
Simon Glassc05ed002020-05-10 11:40:11 -060029#include <linux/delay.h>
Simon Glass45b5a372015-04-29 22:25:59 -060030#include <linux/linkage.h>
31
Simon Glass8b097912015-07-31 09:31:31 -060032DECLARE_GLOBAL_DATA_PTR;
33
Bin Meng6e6f4ce2015-06-17 11:15:36 +080034/* Total CPUs include BSP */
35static int num_cpus;
36
Simon Glass45b5a372015-04-29 22:25:59 -060037/* This also needs to match the sipi.S assembly code for saved MSR encoding */
38struct saved_msr {
39 uint32_t index;
40 uint32_t lo;
41 uint32_t hi;
42} __packed;
43
Simon Glass45b5a372015-04-29 22:25:59 -060044struct mp_flight_plan {
45 int num_records;
46 struct mp_flight_record *records;
47};
48
49static struct mp_flight_plan mp_info;
50
51struct cpu_map {
52 struct udevice *dev;
53 int apic_id;
54 int err_code;
55};
56
57static inline void barrier_wait(atomic_t *b)
58{
59 while (atomic_read(b) == 0)
60 asm("pause");
61 mfence();
62}
63
64static inline void release_barrier(atomic_t *b)
65{
66 mfence();
67 atomic_set(b, 1);
68}
69
Bin Menga2d73fd2015-06-23 12:18:50 +080070static inline void stop_this_cpu(void)
71{
72 /* Called by an AP when it is ready to halt and wait for a new task */
73 for (;;)
74 cpu_hlt();
75}
76
Simon Glass45b5a372015-04-29 22:25:59 -060077/* Returns 1 if timeout waiting for APs. 0 if target APs found */
78static int wait_for_aps(atomic_t *val, int target, int total_delay,
79 int delay_step)
80{
81 int timeout = 0;
82 int delayed = 0;
83
84 while (atomic_read(val) != target) {
85 udelay(delay_step);
86 delayed += delay_step;
87 if (delayed >= total_delay) {
88 timeout = 1;
89 break;
90 }
91 }
92
93 return timeout;
94}
95
96static void ap_do_flight_plan(struct udevice *cpu)
97{
98 int i;
99
100 for (i = 0; i < mp_info.num_records; i++) {
101 struct mp_flight_record *rec = &mp_info.records[i];
102
103 atomic_inc(&rec->cpus_entered);
104 barrier_wait(&rec->barrier);
105
106 if (rec->ap_call != NULL)
107 rec->ap_call(cpu, rec->ap_arg);
108 }
109}
110
Miao Yan24fb4902016-01-07 01:32:02 -0800111static int find_cpu_by_apic_id(int apic_id, struct udevice **devp)
Simon Glass45b5a372015-04-29 22:25:59 -0600112{
113 struct udevice *dev;
114
115 *devp = NULL;
116 for (uclass_find_first_device(UCLASS_CPU, &dev);
117 dev;
118 uclass_find_next_device(&dev)) {
119 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
120
121 if (plat->cpu_id == apic_id) {
122 *devp = dev;
123 return 0;
124 }
125 }
126
127 return -ENOENT;
128}
129
130/*
131 * By the time APs call ap_init() caching has been setup, and microcode has
132 * been loaded
133 */
134static void ap_init(unsigned int cpu_index)
135{
136 struct udevice *dev;
137 int apic_id;
138 int ret;
139
140 /* Ensure the local apic is enabled */
141 enable_lapic();
142
143 apic_id = lapicid();
Miao Yan24fb4902016-01-07 01:32:02 -0800144 ret = find_cpu_by_apic_id(apic_id, &dev);
Simon Glass45b5a372015-04-29 22:25:59 -0600145 if (ret) {
146 debug("Unknown CPU apic_id %x\n", apic_id);
147 goto done;
148 }
149
150 debug("AP: slot %d apic_id %x, dev %s\n", cpu_index, apic_id,
151 dev ? dev->name : "(apic_id not found)");
152
153 /* Walk the flight plan */
154 ap_do_flight_plan(dev);
155
156 /* Park the AP */
157 debug("parking\n");
158done:
159 stop_this_cpu();
160}
161
162static const unsigned int fixed_mtrrs[NUM_FIXED_MTRRS] = {
163 MTRR_FIX_64K_00000_MSR, MTRR_FIX_16K_80000_MSR, MTRR_FIX_16K_A0000_MSR,
164 MTRR_FIX_4K_C0000_MSR, MTRR_FIX_4K_C8000_MSR, MTRR_FIX_4K_D0000_MSR,
165 MTRR_FIX_4K_D8000_MSR, MTRR_FIX_4K_E0000_MSR, MTRR_FIX_4K_E8000_MSR,
166 MTRR_FIX_4K_F0000_MSR, MTRR_FIX_4K_F8000_MSR,
167};
168
169static inline struct saved_msr *save_msr(int index, struct saved_msr *entry)
170{
171 msr_t msr;
172
173 msr = msr_read(index);
174 entry->index = index;
175 entry->lo = msr.lo;
176 entry->hi = msr.hi;
177
178 /* Return the next entry */
179 entry++;
180 return entry;
181}
182
183static int save_bsp_msrs(char *start, int size)
184{
185 int msr_count;
186 int num_var_mtrrs;
187 struct saved_msr *msr_entry;
188 int i;
189 msr_t msr;
190
191 /* Determine number of MTRRs need to be saved */
192 msr = msr_read(MTRR_CAP_MSR);
193 num_var_mtrrs = msr.lo & 0xff;
194
195 /* 2 * num_var_mtrrs for base and mask. +1 for IA32_MTRR_DEF_TYPE */
196 msr_count = 2 * num_var_mtrrs + NUM_FIXED_MTRRS + 1;
197
198 if ((msr_count * sizeof(struct saved_msr)) > size) {
Simon Glass2254e342016-03-06 19:28:22 -0700199 printf("Cannot mirror all %d msrs\n", msr_count);
Simon Glass45b5a372015-04-29 22:25:59 -0600200 return -ENOSPC;
201 }
202
203 msr_entry = (void *)start;
204 for (i = 0; i < NUM_FIXED_MTRRS; i++)
205 msr_entry = save_msr(fixed_mtrrs[i], msr_entry);
206
207 for (i = 0; i < num_var_mtrrs; i++) {
208 msr_entry = save_msr(MTRR_PHYS_BASE_MSR(i), msr_entry);
209 msr_entry = save_msr(MTRR_PHYS_MASK_MSR(i), msr_entry);
210 }
211
212 msr_entry = save_msr(MTRR_DEF_TYPE_MSR, msr_entry);
213
214 return msr_count;
215}
216
Miao Yanb28cecd2016-01-07 01:32:03 -0800217static int load_sipi_vector(atomic_t **ap_countp, int num_cpus)
Simon Glass45b5a372015-04-29 22:25:59 -0600218{
219 struct sipi_params_16bit *params16;
220 struct sipi_params *params;
221 static char msr_save[512];
222 char *stack;
223 ulong addr;
224 int code_len;
225 int size;
226 int ret;
227
228 /* Copy in the code */
229 code_len = ap_start16_code_end - ap_start16;
230 debug("Copying SIPI code to %x: %d bytes\n", AP_DEFAULT_BASE,
231 code_len);
232 memcpy((void *)AP_DEFAULT_BASE, ap_start16, code_len);
233
234 addr = AP_DEFAULT_BASE + (ulong)sipi_params_16bit - (ulong)ap_start16;
235 params16 = (struct sipi_params_16bit *)addr;
236 params16->ap_start = (uint32_t)ap_start;
237 params16->gdt = (uint32_t)gd->arch.gdt;
238 params16->gdt_limit = X86_GDT_SIZE - 1;
239 debug("gdt = %x, gdt_limit = %x\n", params16->gdt, params16->gdt_limit);
240
241 params = (struct sipi_params *)sipi_params;
242 debug("SIPI 32-bit params at %p\n", params);
243 params->idt_ptr = (uint32_t)x86_get_idt();
244
245 params->stack_size = CONFIG_AP_STACK_SIZE;
Miao Yanb28cecd2016-01-07 01:32:03 -0800246 size = params->stack_size * num_cpus;
Stephen Warren4fd64d02016-02-12 14:27:56 -0700247 stack = memalign(4096, size);
Simon Glass45b5a372015-04-29 22:25:59 -0600248 if (!stack)
249 return -ENOMEM;
250 params->stack_top = (u32)(stack + size);
Andy Shevchenko308c75e2017-02-17 16:49:00 +0300251#if !defined(CONFIG_QEMU) && !defined(CONFIG_HAVE_FSP) && \
252 !defined(CONFIG_INTEL_MID)
Simon Glasse77b62e2016-03-11 22:07:11 -0700253 params->microcode_ptr = ucode_base;
254 debug("Microcode at %x\n", params->microcode_ptr);
255#endif
Simon Glass45b5a372015-04-29 22:25:59 -0600256 params->msr_table_ptr = (u32)msr_save;
257 ret = save_bsp_msrs(msr_save, sizeof(msr_save));
258 if (ret < 0)
259 return ret;
260 params->msr_count = ret;
261
262 params->c_handler = (uint32_t)&ap_init;
263
264 *ap_countp = &params->ap_count;
265 atomic_set(*ap_countp, 0);
266 debug("SIPI vector is ready\n");
267
268 return 0;
269}
270
271static int check_cpu_devices(int expected_cpus)
272{
273 int i;
274
275 for (i = 0; i < expected_cpus; i++) {
276 struct udevice *dev;
277 int ret;
278
279 ret = uclass_find_device(UCLASS_CPU, i, &dev);
280 if (ret) {
281 debug("Cannot find CPU %d in device tree\n", i);
282 return ret;
283 }
284 }
285
286 return 0;
287}
288
289/* Returns 1 for timeout. 0 on success */
Simon Glass2254e342016-03-06 19:28:22 -0700290static int apic_wait_timeout(int total_delay, const char *msg)
Simon Glass45b5a372015-04-29 22:25:59 -0600291{
292 int total = 0;
Simon Glass45b5a372015-04-29 22:25:59 -0600293
Simon Glass2254e342016-03-06 19:28:22 -0700294 if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY))
295 return 0;
296
297 debug("Waiting for %s...", msg);
Simon Glass45b5a372015-04-29 22:25:59 -0600298 while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
Simon Glass2254e342016-03-06 19:28:22 -0700299 udelay(50);
300 total += 50;
Simon Glass45b5a372015-04-29 22:25:59 -0600301 if (total >= total_delay) {
Simon Glass2254e342016-03-06 19:28:22 -0700302 debug("timed out: aborting\n");
303 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600304 }
305 }
Simon Glass2254e342016-03-06 19:28:22 -0700306 debug("done\n");
Simon Glass45b5a372015-04-29 22:25:59 -0600307
Simon Glass2254e342016-03-06 19:28:22 -0700308 return 0;
Simon Glass45b5a372015-04-29 22:25:59 -0600309}
310
Simon Glass3a5752c2020-07-17 08:48:10 -0600311/**
312 * start_aps() - Start up the APs and count how many we find
313 *
314 * This is called on the boot processor to start up all the other processors
315 * (here called APs).
316 *
317 * @num_aps: Number of APs we expect to find
318 * @ap_count: Initially zero. Incremented by this function for each AP found
319 * @return 0 if all APs were set up correctly or there are none to set up,
320 * -ENOSPC if the SIPI vector is too high in memory,
321 * -ETIMEDOUT if the ICR is busy or the second SIPI fails to complete
322 * -EIO if not all APs check in correctly
323 */
324static int start_aps(int num_aps, atomic_t *ap_count)
Simon Glass45b5a372015-04-29 22:25:59 -0600325{
326 int sipi_vector;
327 /* Max location is 4KiB below 1MiB */
328 const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
329
Simon Glass3a5752c2020-07-17 08:48:10 -0600330 if (num_aps == 0)
Simon Glass45b5a372015-04-29 22:25:59 -0600331 return 0;
332
333 /* The vector is sent as a 4k aligned address in one byte */
334 sipi_vector = AP_DEFAULT_BASE >> 12;
335
336 if (sipi_vector > max_vector_loc) {
337 printf("SIPI vector too large! 0x%08x\n",
338 sipi_vector);
Simon Glass7b140232019-04-25 21:58:41 -0600339 return -ENOSPC;
Simon Glass45b5a372015-04-29 22:25:59 -0600340 }
341
Simon Glass3a5752c2020-07-17 08:48:10 -0600342 debug("Attempting to start %d APs\n", num_aps);
Simon Glass45b5a372015-04-29 22:25:59 -0600343
Simon Glass2254e342016-03-06 19:28:22 -0700344 if (apic_wait_timeout(1000, "ICR not to be busy"))
345 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600346
347 /* Send INIT IPI to all but self */
Bin Menga2d73fd2015-06-23 12:18:50 +0800348 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
349 lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
350 LAPIC_DM_INIT);
Simon Glass2254e342016-03-06 19:28:22 -0700351 debug("Waiting for 10ms after sending INIT\n");
Simon Glass45b5a372015-04-29 22:25:59 -0600352 mdelay(10);
353
354 /* Send 1st SIPI */
Simon Glass2254e342016-03-06 19:28:22 -0700355 if (apic_wait_timeout(1000, "ICR not to be busy"))
356 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600357
Bin Menga2d73fd2015-06-23 12:18:50 +0800358 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
359 lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
360 LAPIC_DM_STARTUP | sipi_vector);
Simon Glass2254e342016-03-06 19:28:22 -0700361 if (apic_wait_timeout(10000, "first SIPI to complete"))
362 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600363
364 /* Wait for CPUs to check in up to 200 us */
Simon Glass3a5752c2020-07-17 08:48:10 -0600365 wait_for_aps(ap_count, num_aps, 200, 15);
Simon Glass45b5a372015-04-29 22:25:59 -0600366
367 /* Send 2nd SIPI */
Simon Glass2254e342016-03-06 19:28:22 -0700368 if (apic_wait_timeout(1000, "ICR not to be busy"))
369 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600370
Bin Menga2d73fd2015-06-23 12:18:50 +0800371 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
372 lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
373 LAPIC_DM_STARTUP | sipi_vector);
Simon Glass2254e342016-03-06 19:28:22 -0700374 if (apic_wait_timeout(10000, "second SIPI to complete"))
375 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600376
377 /* Wait for CPUs to check in */
Simon Glass3a5752c2020-07-17 08:48:10 -0600378 if (wait_for_aps(ap_count, num_aps, 10000, 50)) {
Simon Glass2254e342016-03-06 19:28:22 -0700379 debug("Not all APs checked in: %d/%d\n",
Simon Glass3a5752c2020-07-17 08:48:10 -0600380 atomic_read(ap_count), num_aps);
Simon Glass7b140232019-04-25 21:58:41 -0600381 return -EIO;
Simon Glass45b5a372015-04-29 22:25:59 -0600382 }
383
384 return 0;
385}
386
Simon Glass78d57d62020-07-17 08:48:08 -0600387static int bsp_do_flight_plan(struct udevice *cpu, struct mp_flight_plan *plan)
Simon Glass45b5a372015-04-29 22:25:59 -0600388{
389 int i;
390 int ret = 0;
391 const int timeout_us = 100000;
392 const int step_us = 100;
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800393 int num_aps = num_cpus - 1;
Simon Glass45b5a372015-04-29 22:25:59 -0600394
Simon Glass78d57d62020-07-17 08:48:08 -0600395 for (i = 0; i < plan->num_records; i++) {
396 struct mp_flight_record *rec = &plan->records[i];
Simon Glass45b5a372015-04-29 22:25:59 -0600397
398 /* Wait for APs if the record is not released */
399 if (atomic_read(&rec->barrier) == 0) {
400 /* Wait for the APs to check in */
401 if (wait_for_aps(&rec->cpus_entered, num_aps,
402 timeout_us, step_us)) {
Simon Glass2254e342016-03-06 19:28:22 -0700403 debug("MP record %d timeout\n", i);
Simon Glass7b140232019-04-25 21:58:41 -0600404 ret = -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600405 }
406 }
407
408 if (rec->bsp_call != NULL)
409 rec->bsp_call(cpu, rec->bsp_arg);
410
411 release_barrier(&rec->barrier);
412 }
413 return ret;
414}
415
416static int init_bsp(struct udevice **devp)
417{
418 char processor_name[CPU_MAX_NAME_LEN];
419 int apic_id;
420 int ret;
421
422 cpu_get_name(processor_name);
Simon Glass2254e342016-03-06 19:28:22 -0700423 debug("CPU: %s\n", processor_name);
Simon Glass45b5a372015-04-29 22:25:59 -0600424
Simon Glass45b5a372015-04-29 22:25:59 -0600425 apic_id = lapicid();
Miao Yan24fb4902016-01-07 01:32:02 -0800426 ret = find_cpu_by_apic_id(apic_id, devp);
Simon Glass45b5a372015-04-29 22:25:59 -0600427 if (ret) {
428 printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
429 return ret;
430 }
431
432 return 0;
433}
434
Simon Glasse6248582020-07-17 08:48:09 -0600435static int mp_init_cpu(struct udevice *cpu, void *unused)
436{
437 struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
438
439 /*
440 * Multiple APs are brought up simultaneously and they may get the same
441 * seq num in the uclass_resolve_seq() during device_probe(). To avoid
442 * this, set req_seq to the reg number in the device tree in advance.
443 */
444 cpu->req_seq = dev_read_u32_default(cpu, "reg", -1);
445 plat->ucode_version = microcode_read_rev();
446 plat->device_id = gd->arch.x86_device;
447
448 return device_probe(cpu);
449}
450
451static struct mp_flight_record mp_steps[] = {
452 MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
453};
454
Simon Glass78d57d62020-07-17 08:48:08 -0600455int mp_init(void)
Simon Glass45b5a372015-04-29 22:25:59 -0600456{
457 int num_aps;
458 atomic_t *ap_count;
459 struct udevice *cpu;
460 int ret;
461
462 /* This will cause the CPUs devices to be bound */
463 struct uclass *uc;
464 ret = uclass_get(UCLASS_CPU, &uc);
465 if (ret)
466 return ret;
467
Simon Glassbaaeb922019-12-06 21:42:55 -0700468 if (IS_ENABLED(CONFIG_QFW)) {
469 ret = qemu_cpu_fixup();
470 if (ret)
471 return ret;
472 }
Miao Yande752c52016-01-07 01:32:04 -0800473
Simon Glass45b5a372015-04-29 22:25:59 -0600474 ret = init_bsp(&cpu);
475 if (ret) {
476 debug("Cannot init boot CPU: err=%d\n", ret);
477 return ret;
478 }
479
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800480 num_cpus = cpu_get_count(cpu);
481 if (num_cpus < 0) {
482 debug("Cannot get number of CPUs: err=%d\n", num_cpus);
483 return num_cpus;
484 }
485
486 if (num_cpus < 2)
487 debug("Warning: Only 1 CPU is detected\n");
488
489 ret = check_cpu_devices(num_cpus);
Simon Glass45b5a372015-04-29 22:25:59 -0600490 if (ret)
491 debug("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n");
492
493 /* Copy needed parameters so that APs have a reference to the plan */
Simon Glass78d57d62020-07-17 08:48:08 -0600494 mp_info.num_records = ARRAY_SIZE(mp_steps);
495 mp_info.records = mp_steps;
Simon Glass45b5a372015-04-29 22:25:59 -0600496
497 /* Load the SIPI vector */
Miao Yanb28cecd2016-01-07 01:32:03 -0800498 ret = load_sipi_vector(&ap_count, num_cpus);
Simon Glass45b5a372015-04-29 22:25:59 -0600499 if (ap_count == NULL)
Simon Glass7b140232019-04-25 21:58:41 -0600500 return -ENOENT;
Simon Glass45b5a372015-04-29 22:25:59 -0600501
502 /*
503 * Make sure SIPI data hits RAM so the APs that come up will see
504 * the startup code even if the caches are disabled
505 */
506 wbinvd();
507
508 /* Start the APs providing number of APs and the cpus_entered field */
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800509 num_aps = num_cpus - 1;
Simon Glass45b5a372015-04-29 22:25:59 -0600510 ret = start_aps(num_aps, ap_count);
511 if (ret) {
512 mdelay(1000);
513 debug("%d/%d eventually checked in?\n", atomic_read(ap_count),
514 num_aps);
515 return ret;
516 }
517
518 /* Walk the flight plan for the BSP */
Simon Glass78d57d62020-07-17 08:48:08 -0600519 ret = bsp_do_flight_plan(cpu, &mp_info);
Simon Glass45b5a372015-04-29 22:25:59 -0600520 if (ret) {
521 debug("CPU init failed: err=%d\n", ret);
522 return ret;
523 }
524
525 return 0;
526}