blob: 831fd7035d1bbda80a1abd15785399be71796c37 [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 Glass78d57d62020-07-17 08:48:08 -060044static struct mp_flight_record mp_steps[] = {
45 MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
46};
Simon Glass45b5a372015-04-29 22:25:59 -060047
48struct mp_flight_plan {
49 int num_records;
50 struct mp_flight_record *records;
51};
52
53static struct mp_flight_plan mp_info;
54
55struct cpu_map {
56 struct udevice *dev;
57 int apic_id;
58 int err_code;
59};
60
61static inline void barrier_wait(atomic_t *b)
62{
63 while (atomic_read(b) == 0)
64 asm("pause");
65 mfence();
66}
67
68static inline void release_barrier(atomic_t *b)
69{
70 mfence();
71 atomic_set(b, 1);
72}
73
Bin Menga2d73fd2015-06-23 12:18:50 +080074static inline void stop_this_cpu(void)
75{
76 /* Called by an AP when it is ready to halt and wait for a new task */
77 for (;;)
78 cpu_hlt();
79}
80
Simon Glass45b5a372015-04-29 22:25:59 -060081/* Returns 1 if timeout waiting for APs. 0 if target APs found */
82static int wait_for_aps(atomic_t *val, int target, int total_delay,
83 int delay_step)
84{
85 int timeout = 0;
86 int delayed = 0;
87
88 while (atomic_read(val) != target) {
89 udelay(delay_step);
90 delayed += delay_step;
91 if (delayed >= total_delay) {
92 timeout = 1;
93 break;
94 }
95 }
96
97 return timeout;
98}
99
100static void ap_do_flight_plan(struct udevice *cpu)
101{
102 int i;
103
104 for (i = 0; i < mp_info.num_records; i++) {
105 struct mp_flight_record *rec = &mp_info.records[i];
106
107 atomic_inc(&rec->cpus_entered);
108 barrier_wait(&rec->barrier);
109
110 if (rec->ap_call != NULL)
111 rec->ap_call(cpu, rec->ap_arg);
112 }
113}
114
Miao Yan24fb4902016-01-07 01:32:02 -0800115static int find_cpu_by_apic_id(int apic_id, struct udevice **devp)
Simon Glass45b5a372015-04-29 22:25:59 -0600116{
117 struct udevice *dev;
118
119 *devp = NULL;
120 for (uclass_find_first_device(UCLASS_CPU, &dev);
121 dev;
122 uclass_find_next_device(&dev)) {
123 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
124
125 if (plat->cpu_id == apic_id) {
126 *devp = dev;
127 return 0;
128 }
129 }
130
131 return -ENOENT;
132}
133
134/*
135 * By the time APs call ap_init() caching has been setup, and microcode has
136 * been loaded
137 */
138static void ap_init(unsigned int cpu_index)
139{
140 struct udevice *dev;
141 int apic_id;
142 int ret;
143
144 /* Ensure the local apic is enabled */
145 enable_lapic();
146
147 apic_id = lapicid();
Miao Yan24fb4902016-01-07 01:32:02 -0800148 ret = find_cpu_by_apic_id(apic_id, &dev);
Simon Glass45b5a372015-04-29 22:25:59 -0600149 if (ret) {
150 debug("Unknown CPU apic_id %x\n", apic_id);
151 goto done;
152 }
153
154 debug("AP: slot %d apic_id %x, dev %s\n", cpu_index, apic_id,
155 dev ? dev->name : "(apic_id not found)");
156
157 /* Walk the flight plan */
158 ap_do_flight_plan(dev);
159
160 /* Park the AP */
161 debug("parking\n");
162done:
163 stop_this_cpu();
164}
165
166static const unsigned int fixed_mtrrs[NUM_FIXED_MTRRS] = {
167 MTRR_FIX_64K_00000_MSR, MTRR_FIX_16K_80000_MSR, MTRR_FIX_16K_A0000_MSR,
168 MTRR_FIX_4K_C0000_MSR, MTRR_FIX_4K_C8000_MSR, MTRR_FIX_4K_D0000_MSR,
169 MTRR_FIX_4K_D8000_MSR, MTRR_FIX_4K_E0000_MSR, MTRR_FIX_4K_E8000_MSR,
170 MTRR_FIX_4K_F0000_MSR, MTRR_FIX_4K_F8000_MSR,
171};
172
173static inline struct saved_msr *save_msr(int index, struct saved_msr *entry)
174{
175 msr_t msr;
176
177 msr = msr_read(index);
178 entry->index = index;
179 entry->lo = msr.lo;
180 entry->hi = msr.hi;
181
182 /* Return the next entry */
183 entry++;
184 return entry;
185}
186
187static int save_bsp_msrs(char *start, int size)
188{
189 int msr_count;
190 int num_var_mtrrs;
191 struct saved_msr *msr_entry;
192 int i;
193 msr_t msr;
194
195 /* Determine number of MTRRs need to be saved */
196 msr = msr_read(MTRR_CAP_MSR);
197 num_var_mtrrs = msr.lo & 0xff;
198
199 /* 2 * num_var_mtrrs for base and mask. +1 for IA32_MTRR_DEF_TYPE */
200 msr_count = 2 * num_var_mtrrs + NUM_FIXED_MTRRS + 1;
201
202 if ((msr_count * sizeof(struct saved_msr)) > size) {
Simon Glass2254e342016-03-06 19:28:22 -0700203 printf("Cannot mirror all %d msrs\n", msr_count);
Simon Glass45b5a372015-04-29 22:25:59 -0600204 return -ENOSPC;
205 }
206
207 msr_entry = (void *)start;
208 for (i = 0; i < NUM_FIXED_MTRRS; i++)
209 msr_entry = save_msr(fixed_mtrrs[i], msr_entry);
210
211 for (i = 0; i < num_var_mtrrs; i++) {
212 msr_entry = save_msr(MTRR_PHYS_BASE_MSR(i), msr_entry);
213 msr_entry = save_msr(MTRR_PHYS_MASK_MSR(i), msr_entry);
214 }
215
216 msr_entry = save_msr(MTRR_DEF_TYPE_MSR, msr_entry);
217
218 return msr_count;
219}
220
Miao Yanb28cecd2016-01-07 01:32:03 -0800221static int load_sipi_vector(atomic_t **ap_countp, int num_cpus)
Simon Glass45b5a372015-04-29 22:25:59 -0600222{
223 struct sipi_params_16bit *params16;
224 struct sipi_params *params;
225 static char msr_save[512];
226 char *stack;
227 ulong addr;
228 int code_len;
229 int size;
230 int ret;
231
232 /* Copy in the code */
233 code_len = ap_start16_code_end - ap_start16;
234 debug("Copying SIPI code to %x: %d bytes\n", AP_DEFAULT_BASE,
235 code_len);
236 memcpy((void *)AP_DEFAULT_BASE, ap_start16, code_len);
237
238 addr = AP_DEFAULT_BASE + (ulong)sipi_params_16bit - (ulong)ap_start16;
239 params16 = (struct sipi_params_16bit *)addr;
240 params16->ap_start = (uint32_t)ap_start;
241 params16->gdt = (uint32_t)gd->arch.gdt;
242 params16->gdt_limit = X86_GDT_SIZE - 1;
243 debug("gdt = %x, gdt_limit = %x\n", params16->gdt, params16->gdt_limit);
244
245 params = (struct sipi_params *)sipi_params;
246 debug("SIPI 32-bit params at %p\n", params);
247 params->idt_ptr = (uint32_t)x86_get_idt();
248
249 params->stack_size = CONFIG_AP_STACK_SIZE;
Miao Yanb28cecd2016-01-07 01:32:03 -0800250 size = params->stack_size * num_cpus;
Stephen Warren4fd64d02016-02-12 14:27:56 -0700251 stack = memalign(4096, size);
Simon Glass45b5a372015-04-29 22:25:59 -0600252 if (!stack)
253 return -ENOMEM;
254 params->stack_top = (u32)(stack + size);
Andy Shevchenko308c75e2017-02-17 16:49:00 +0300255#if !defined(CONFIG_QEMU) && !defined(CONFIG_HAVE_FSP) && \
256 !defined(CONFIG_INTEL_MID)
Simon Glasse77b62e2016-03-11 22:07:11 -0700257 params->microcode_ptr = ucode_base;
258 debug("Microcode at %x\n", params->microcode_ptr);
259#endif
Simon Glass45b5a372015-04-29 22:25:59 -0600260 params->msr_table_ptr = (u32)msr_save;
261 ret = save_bsp_msrs(msr_save, sizeof(msr_save));
262 if (ret < 0)
263 return ret;
264 params->msr_count = ret;
265
266 params->c_handler = (uint32_t)&ap_init;
267
268 *ap_countp = &params->ap_count;
269 atomic_set(*ap_countp, 0);
270 debug("SIPI vector is ready\n");
271
272 return 0;
273}
274
275static int check_cpu_devices(int expected_cpus)
276{
277 int i;
278
279 for (i = 0; i < expected_cpus; i++) {
280 struct udevice *dev;
281 int ret;
282
283 ret = uclass_find_device(UCLASS_CPU, i, &dev);
284 if (ret) {
285 debug("Cannot find CPU %d in device tree\n", i);
286 return ret;
287 }
288 }
289
290 return 0;
291}
292
293/* Returns 1 for timeout. 0 on success */
Simon Glass2254e342016-03-06 19:28:22 -0700294static int apic_wait_timeout(int total_delay, const char *msg)
Simon Glass45b5a372015-04-29 22:25:59 -0600295{
296 int total = 0;
Simon Glass45b5a372015-04-29 22:25:59 -0600297
Simon Glass2254e342016-03-06 19:28:22 -0700298 if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY))
299 return 0;
300
301 debug("Waiting for %s...", msg);
Simon Glass45b5a372015-04-29 22:25:59 -0600302 while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
Simon Glass2254e342016-03-06 19:28:22 -0700303 udelay(50);
304 total += 50;
Simon Glass45b5a372015-04-29 22:25:59 -0600305 if (total >= total_delay) {
Simon Glass2254e342016-03-06 19:28:22 -0700306 debug("timed out: aborting\n");
307 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600308 }
309 }
Simon Glass2254e342016-03-06 19:28:22 -0700310 debug("done\n");
Simon Glass45b5a372015-04-29 22:25:59 -0600311
Simon Glass2254e342016-03-06 19:28:22 -0700312 return 0;
Simon Glass45b5a372015-04-29 22:25:59 -0600313}
314
315static int start_aps(int ap_count, atomic_t *num_aps)
316{
317 int sipi_vector;
318 /* Max location is 4KiB below 1MiB */
319 const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
320
321 if (ap_count == 0)
322 return 0;
323
324 /* The vector is sent as a 4k aligned address in one byte */
325 sipi_vector = AP_DEFAULT_BASE >> 12;
326
327 if (sipi_vector > max_vector_loc) {
328 printf("SIPI vector too large! 0x%08x\n",
329 sipi_vector);
Simon Glass7b140232019-04-25 21:58:41 -0600330 return -ENOSPC;
Simon Glass45b5a372015-04-29 22:25:59 -0600331 }
332
333 debug("Attempting to start %d APs\n", ap_count);
334
Simon Glass2254e342016-03-06 19:28:22 -0700335 if (apic_wait_timeout(1000, "ICR not to be busy"))
336 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600337
338 /* Send INIT IPI to all but self */
Bin Menga2d73fd2015-06-23 12:18:50 +0800339 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
340 lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
341 LAPIC_DM_INIT);
Simon Glass2254e342016-03-06 19:28:22 -0700342 debug("Waiting for 10ms after sending INIT\n");
Simon Glass45b5a372015-04-29 22:25:59 -0600343 mdelay(10);
344
345 /* Send 1st SIPI */
Simon Glass2254e342016-03-06 19:28:22 -0700346 if (apic_wait_timeout(1000, "ICR not to be busy"))
347 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600348
Bin Menga2d73fd2015-06-23 12:18:50 +0800349 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
350 lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
351 LAPIC_DM_STARTUP | sipi_vector);
Simon Glass2254e342016-03-06 19:28:22 -0700352 if (apic_wait_timeout(10000, "first SIPI to complete"))
353 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600354
355 /* Wait for CPUs to check in up to 200 us */
356 wait_for_aps(num_aps, ap_count, 200, 15);
357
358 /* Send 2nd SIPI */
Simon Glass2254e342016-03-06 19:28:22 -0700359 if (apic_wait_timeout(1000, "ICR not to be busy"))
360 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600361
Bin Menga2d73fd2015-06-23 12:18:50 +0800362 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
363 lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
364 LAPIC_DM_STARTUP | sipi_vector);
Simon Glass2254e342016-03-06 19:28:22 -0700365 if (apic_wait_timeout(10000, "second SIPI to complete"))
366 return -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600367
368 /* Wait for CPUs to check in */
369 if (wait_for_aps(num_aps, ap_count, 10000, 50)) {
Simon Glass2254e342016-03-06 19:28:22 -0700370 debug("Not all APs checked in: %d/%d\n",
Simon Glass45b5a372015-04-29 22:25:59 -0600371 atomic_read(num_aps), ap_count);
Simon Glass7b140232019-04-25 21:58:41 -0600372 return -EIO;
Simon Glass45b5a372015-04-29 22:25:59 -0600373 }
374
375 return 0;
376}
377
Simon Glass78d57d62020-07-17 08:48:08 -0600378static int bsp_do_flight_plan(struct udevice *cpu, struct mp_flight_plan *plan)
Simon Glass45b5a372015-04-29 22:25:59 -0600379{
380 int i;
381 int ret = 0;
382 const int timeout_us = 100000;
383 const int step_us = 100;
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800384 int num_aps = num_cpus - 1;
Simon Glass45b5a372015-04-29 22:25:59 -0600385
Simon Glass78d57d62020-07-17 08:48:08 -0600386 for (i = 0; i < plan->num_records; i++) {
387 struct mp_flight_record *rec = &plan->records[i];
Simon Glass45b5a372015-04-29 22:25:59 -0600388
389 /* Wait for APs if the record is not released */
390 if (atomic_read(&rec->barrier) == 0) {
391 /* Wait for the APs to check in */
392 if (wait_for_aps(&rec->cpus_entered, num_aps,
393 timeout_us, step_us)) {
Simon Glass2254e342016-03-06 19:28:22 -0700394 debug("MP record %d timeout\n", i);
Simon Glass7b140232019-04-25 21:58:41 -0600395 ret = -ETIMEDOUT;
Simon Glass45b5a372015-04-29 22:25:59 -0600396 }
397 }
398
399 if (rec->bsp_call != NULL)
400 rec->bsp_call(cpu, rec->bsp_arg);
401
402 release_barrier(&rec->barrier);
403 }
404 return ret;
405}
406
407static int init_bsp(struct udevice **devp)
408{
409 char processor_name[CPU_MAX_NAME_LEN];
410 int apic_id;
411 int ret;
412
413 cpu_get_name(processor_name);
Simon Glass2254e342016-03-06 19:28:22 -0700414 debug("CPU: %s\n", processor_name);
Simon Glass45b5a372015-04-29 22:25:59 -0600415
Simon Glass45b5a372015-04-29 22:25:59 -0600416 apic_id = lapicid();
Miao Yan24fb4902016-01-07 01:32:02 -0800417 ret = find_cpu_by_apic_id(apic_id, devp);
Simon Glass45b5a372015-04-29 22:25:59 -0600418 if (ret) {
419 printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
420 return ret;
421 }
422
423 return 0;
424}
425
Simon Glass78d57d62020-07-17 08:48:08 -0600426int mp_init(void)
Simon Glass45b5a372015-04-29 22:25:59 -0600427{
428 int num_aps;
429 atomic_t *ap_count;
430 struct udevice *cpu;
431 int ret;
432
433 /* This will cause the CPUs devices to be bound */
434 struct uclass *uc;
435 ret = uclass_get(UCLASS_CPU, &uc);
436 if (ret)
437 return ret;
438
Simon Glassbaaeb922019-12-06 21:42:55 -0700439 if (IS_ENABLED(CONFIG_QFW)) {
440 ret = qemu_cpu_fixup();
441 if (ret)
442 return ret;
443 }
Miao Yande752c52016-01-07 01:32:04 -0800444
Simon Glass45b5a372015-04-29 22:25:59 -0600445 ret = init_bsp(&cpu);
446 if (ret) {
447 debug("Cannot init boot CPU: err=%d\n", ret);
448 return ret;
449 }
450
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800451 num_cpus = cpu_get_count(cpu);
452 if (num_cpus < 0) {
453 debug("Cannot get number of CPUs: err=%d\n", num_cpus);
454 return num_cpus;
455 }
456
457 if (num_cpus < 2)
458 debug("Warning: Only 1 CPU is detected\n");
459
460 ret = check_cpu_devices(num_cpus);
Simon Glass45b5a372015-04-29 22:25:59 -0600461 if (ret)
462 debug("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n");
463
464 /* Copy needed parameters so that APs have a reference to the plan */
Simon Glass78d57d62020-07-17 08:48:08 -0600465 mp_info.num_records = ARRAY_SIZE(mp_steps);
466 mp_info.records = mp_steps;
Simon Glass45b5a372015-04-29 22:25:59 -0600467
468 /* Load the SIPI vector */
Miao Yanb28cecd2016-01-07 01:32:03 -0800469 ret = load_sipi_vector(&ap_count, num_cpus);
Simon Glass45b5a372015-04-29 22:25:59 -0600470 if (ap_count == NULL)
Simon Glass7b140232019-04-25 21:58:41 -0600471 return -ENOENT;
Simon Glass45b5a372015-04-29 22:25:59 -0600472
473 /*
474 * Make sure SIPI data hits RAM so the APs that come up will see
475 * the startup code even if the caches are disabled
476 */
477 wbinvd();
478
479 /* Start the APs providing number of APs and the cpus_entered field */
Bin Meng6e6f4ce2015-06-17 11:15:36 +0800480 num_aps = num_cpus - 1;
Simon Glass45b5a372015-04-29 22:25:59 -0600481 ret = start_aps(num_aps, ap_count);
482 if (ret) {
483 mdelay(1000);
484 debug("%d/%d eventually checked in?\n", atomic_read(ap_count),
485 num_aps);
486 return ret;
487 }
488
489 /* Walk the flight plan for the BSP */
Simon Glass78d57d62020-07-17 08:48:08 -0600490 ret = bsp_do_flight_plan(cpu, &mp_info);
Simon Glass45b5a372015-04-29 22:25:59 -0600491 if (ret) {
492 debug("CPU init failed: err=%d\n", ret);
493 return ret;
494 }
495
496 return 0;
497}
498
499int mp_init_cpu(struct udevice *cpu, void *unused)
500{
Simon Glass6bcb6752016-03-11 22:07:09 -0700501 struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
502
Bin Mengecfeada2015-08-09 23:58:39 -0700503 /*
504 * Multiple APs are brought up simultaneously and they may get the same
505 * seq num in the uclass_resolve_seq() during device_probe(). To avoid
506 * this, set req_seq to the reg number in the device tree in advance.
507 */
Simon Glasscb1cb712020-07-17 08:48:07 -0600508 cpu->req_seq = dev_read_u32_default(cpu, "reg", -1);
Simon Glass6bcb6752016-03-11 22:07:09 -0700509 plat->ucode_version = microcode_read_rev();
510 plat->device_id = gd->arch.x86_device;
Bin Mengecfeada2015-08-09 23:58:39 -0700511
Simon Glass45b5a372015-04-29 22:25:59 -0600512 return device_probe(cpu);
513}