blob: b52376885fedcce5856c366a9345b8955d30c2bc [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Martin Roth9df9e9392016-01-12 15:55:28 -07003
Aaron Durbinb21e3622016-12-07 00:32:19 -06004#include <bootstate.h>
Aaron Durbinf6ada1c2016-02-10 10:52:47 -06005#include <boot/coreboot_tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00006#include <console/console.h>
7#include <cpu/cpu.h>
Kyösti Mälkkice39ba92020-01-04 16:15:50 +02008#include <post.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00009#include <string.h>
Aaron Durbinb21e3622016-12-07 00:32:19 -060010#include <cpu/x86/mp.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000011#include <cpu/x86/lapic.h>
Aaron Durbine0969ae2016-02-10 10:56:06 -060012#include <cpu/x86/tsc.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000013#include <arch/cpu.h>
14#include <device/path.h>
15#include <device/device.h>
16#include <smp/spinlock.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000017
Stefan Reinauer96938852015-06-18 01:23:48 -070018#ifndef __x86_64__
Eric Biedermanb78c1972004-10-14 20:54:17 +000019/* Standard macro to see if a specific flag is changeable */
20static inline int flag_is_changeable_p(uint32_t flag)
Eric Biederman8ca8d762003-04-22 19:02:15 +000021{
Eric Biedermanb78c1972004-10-14 20:54:17 +000022 uint32_t f1, f2;
Eric Biederman8ca8d762003-04-22 19:02:15 +000023
Eric Biedermanb78c1972004-10-14 20:54:17 +000024 asm(
25 "pushfl\n\t"
26 "pushfl\n\t"
27 "popl %0\n\t"
28 "movl %0,%1\n\t"
29 "xorl %2,%0\n\t"
30 "pushl %0\n\t"
31 "popfl\n\t"
32 "pushfl\n\t"
33 "popl %0\n\t"
34 "popfl\n\t"
35 : "=&r" (f1), "=&r" (f2)
36 : "ir" (flag));
37 return ((f1^f2) & flag) != 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000038}
39
Eric Biedermanb78c1972004-10-14 20:54:17 +000040/*
41 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
42 * by the fact that they preserve the flags across the division of 5/2.
43 * PII and PPro exhibit this behavior too, but they have cpuid available.
44 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000045
Eric Biedermanb78c1972004-10-14 20:54:17 +000046/*
47 * Perform the Cyrix 5/2 test. A Cyrix won't change
48 * the flags, while other 486 chips will.
49 */
50static inline int test_cyrix_52div(void)
51{
52 unsigned int test;
53
54 __asm__ __volatile__(
55 "sahf\n\t" /* clear flags (%eax = 0x0005) */
56 "div %b2\n\t" /* divide 5 by 2 */
57 "lahf" /* store flags into %ah */
58 : "=a" (test)
59 : "0" (5), "q" (2)
60 : "cc");
61
62 /* AH is 0x02 on Cyrix after the divide.. */
63 return (unsigned char) (test >> 8) == 0x02;
64}
65
66/*
67 * Detect a NexGen CPU running without BIOS hypercode new enough
68 * to have CPUID. (Thanks to Herbert Oppmann)
69 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000070
Eric Biedermanb78c1972004-10-14 20:54:17 +000071static int deep_magic_nexgen_probe(void)
72{
73 int ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +000074
Eric Biedermanb78c1972004-10-14 20:54:17 +000075 __asm__ __volatile__ (
76 " movw $0x5555, %%ax\n"
77 " xorw %%dx,%%dx\n"
78 " movw $2, %%cx\n"
79 " divw %%cx\n"
80 " movl $0, %%eax\n"
81 " jnz 1f\n"
82 " movl $1, %%eax\n"
Stefan Reinauer14e22772010-04-27 06:56:47 +000083 "1:\n"
Lee Leahy024b13d2017-03-16 13:41:11 -070084 : "=a" (ret) : : "cx", "dx");
Eric Biedermanb78c1972004-10-14 20:54:17 +000085 return ret;
86}
Stefan Reinauer96938852015-06-18 01:23:48 -070087#endif
Eric Biedermanb78c1972004-10-14 20:54:17 +000088
Elyes HAOUAS777ea892016-07-29 07:40:41 +020089/* List of CPU vendor strings along with their normalized
Eric Biedermanb78c1972004-10-14 20:54:17 +000090 * id values.
91 */
92static struct {
93 int vendor;
94 const char *name;
95} x86_vendors[] = {
96 { X86_VENDOR_INTEL, "GenuineIntel", },
97 { X86_VENDOR_CYRIX, "CyrixInstead", },
Stefan Reinauer14e22772010-04-27 06:56:47 +000098 { X86_VENDOR_AMD, "AuthenticAMD", },
Eric Biedermanb78c1972004-10-14 20:54:17 +000099 { X86_VENDOR_UMC, "UMC UMC UMC ", },
100 { X86_VENDOR_NEXGEN, "NexGenDriven", },
101 { X86_VENDOR_CENTAUR, "CentaurHauls", },
Martin Rothe3690102016-01-06 15:21:02 -0700102 { X86_VENDOR_RISE, "RiseRiseRise", },
103 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
Eric Biedermanb78c1972004-10-14 20:54:17 +0000104 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
105 { X86_VENDOR_NSC, "Geode by NSC", },
106 { X86_VENDOR_SIS, "SiS SiS SiS ", },
Jinke Fan8de6cb92019-05-18 18:07:56 +0800107 { X86_VENDOR_HYGON, "HygonGenuine", },
Eric Biedermanb78c1972004-10-14 20:54:17 +0000108};
109
Elyes HAOUAS6c9737b2018-07-08 12:30:02 +0200110static const char *const x86_vendor_name[] = {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000111 [X86_VENDOR_INTEL] = "Intel",
112 [X86_VENDOR_CYRIX] = "Cyrix",
113 [X86_VENDOR_AMD] = "AMD",
114 [X86_VENDOR_UMC] = "UMC",
115 [X86_VENDOR_NEXGEN] = "NexGen",
116 [X86_VENDOR_CENTAUR] = "Centaur",
117 [X86_VENDOR_RISE] = "Rise",
118 [X86_VENDOR_TRANSMETA] = "Transmeta",
119 [X86_VENDOR_NSC] = "NSC",
120 [X86_VENDOR_SIS] = "SiS",
Jinke Fan8de6cb92019-05-18 18:07:56 +0800121 [X86_VENDOR_HYGON] = "Hygon",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000122};
123
124static const char *cpu_vendor_name(int vendor)
125{
126 const char *name;
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200127 name = "<invalid CPU vendor>";
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000128 if ((vendor < (ARRAY_SIZE(x86_vendor_name))) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000129 (x86_vendor_name[vendor] != 0))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000130 name = x86_vendor_name[vendor];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000131 return name;
132}
133
134static void identify_cpu(struct device *cpu)
135{
136 char vendor_name[16];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000137 int i;
138
139 vendor_name[0] = '\0'; /* Unset */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000140
Stefan Reinauer96938852015-06-18 01:23:48 -0700141#ifndef __x86_64__
Eric Biedermanb78c1972004-10-14 20:54:17 +0000142 /* Find the id and vendor_name */
Rudolf Marek06253cd2012-02-25 23:51:12 +0100143 if (!cpu_have_cpuid()) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000144 /* Its a 486 if we can modify the AC flag */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700145 if (flag_is_changeable_p(X86_EFLAGS_AC))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000146 cpu->device = 0x00000400; /* 486 */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700147 else
Eric Biedermanb78c1972004-10-14 20:54:17 +0000148 cpu->device = 0x00000300; /* 386 */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700149 if ((cpu->device == 0x00000400) && test_cyrix_52div())
Eric Biedermanb78c1972004-10-14 20:54:17 +0000150 memcpy(vendor_name, "CyrixInstead", 13);
151 /* If we ever care we can enable cpuid here */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000152 /* Detect NexGen with old hypercode */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700153 else if (deep_magic_nexgen_probe())
Eric Biedermanb78c1972004-10-14 20:54:17 +0000154 memcpy(vendor_name, "NexGenDriven", 13);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000155 }
Stefan Reinauer96938852015-06-18 01:23:48 -0700156#endif
Rudolf Marek06253cd2012-02-25 23:51:12 +0100157 if (cpu_have_cpuid()) {
Stefan Reinauer4b556a12009-05-26 12:33:06 +0000158 int cpuid_level;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000159 struct cpuid_result result;
160 result = cpuid(0x00000000);
Lee Leahy024b13d2017-03-16 13:41:11 -0700161 cpuid_level = result.eax;
162 vendor_name[0] = (result.ebx >> 0) & 0xff;
163 vendor_name[1] = (result.ebx >> 8) & 0xff;
164 vendor_name[2] = (result.ebx >> 16) & 0xff;
165 vendor_name[3] = (result.ebx >> 24) & 0xff;
166 vendor_name[4] = (result.edx >> 0) & 0xff;
167 vendor_name[5] = (result.edx >> 8) & 0xff;
168 vendor_name[6] = (result.edx >> 16) & 0xff;
169 vendor_name[7] = (result.edx >> 24) & 0xff;
170 vendor_name[8] = (result.ecx >> 0) & 0xff;
171 vendor_name[9] = (result.ecx >> 8) & 0xff;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000172 vendor_name[10] = (result.ecx >> 16) & 0xff;
173 vendor_name[11] = (result.ecx >> 24) & 0xff;
174 vendor_name[12] = '\0';
Stefan Reinauer14e22772010-04-27 06:56:47 +0000175
Eric Biedermanb78c1972004-10-14 20:54:17 +0000176 /* Intel-defined flags: level 0x00000001 */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700177 if (cpuid_level >= 0x00000001)
Subrata Banik53b08c32018-12-10 14:11:35 +0530178 cpu->device = cpu_get_cpuid();
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700179 else
Eric Biedermanb78c1972004-10-14 20:54:17 +0000180 /* Have CPUID level 0 only unheard of */
181 cpu->device = 0x00000400;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000182 }
183 cpu->vendor = X86_VENDOR_UNKNOWN;
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200184 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000185 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
186 cpu->vendor = x86_vendors[i].vendor;
187 break;
188 }
189 }
190}
191
Stefan Reinauer6293d302012-04-03 16:07:56 -0700192struct cpu_driver *find_cpu_driver(struct device *cpu)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000193{
194 struct cpu_driver *driver;
Aaron Durbin03758152015-09-03 17:23:08 -0500195 for (driver = _cpu_drivers; driver < _ecpu_drivers; driver++) {
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100196 const struct cpu_device_id *id;
Stefan Reinauer6293d302012-04-03 16:07:56 -0700197 for (id = driver->id_table;
198 id->vendor != X86_VENDOR_INVALID; id++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000199 if ((cpu->vendor == id->vendor) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000200 (cpu->device == id->device))
Stefan Reinauer6293d302012-04-03 16:07:56 -0700201 return driver;
Lee Leahy0b5678f2017-03-16 16:01:40 -0700202 if (id->vendor == X86_VENDOR_ANY)
Gerd Hoffmanncbf30732013-05-31 09:23:26 +0200203 return driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000204 }
205 }
Stefan Reinauer6293d302012-04-03 16:07:56 -0700206 return NULL;
207}
208
209static void set_cpu_ops(struct device *cpu)
210{
211 struct cpu_driver *driver = find_cpu_driver(cpu);
212 cpu->ops = driver ? driver->ops : NULL;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000213}
214
Elyes HAOUASa5b0bc42020-02-20 20:04:29 +0100215/* Keep track of default APIC ids for SMM. */
Subrata Banik7bc90362019-05-10 11:58:37 +0530216static int cpus_default_apic_id[CONFIG_MAX_CPUS];
217
218/*
219 * When CPUID executes with EAX set to 1, additional processor identification
220 * information is returned to EBX register:
221 * Default APIC ID: EBX[31-24] - this number is the 8 bit ID that is assigned
222 * to the local APIC on the processor during power on.
223 */
224static int initial_lapicid(void)
225{
226 return cpuid_ebx(1) >> 24;
227}
228
229/* Function to keep track of cpu default apic_id */
230void cpu_add_map_entry(unsigned int index)
231{
232 cpus_default_apic_id[index] = initial_lapicid();
233}
234
235/* Returns default APIC id based on logical_cpu number or < 0 on failure. */
236int cpu_get_apic_id(int logical_cpu)
237{
238 if (logical_cpu >= CONFIG_MAX_CPUS || logical_cpu < 0)
239 return -1;
240
241 return cpus_default_apic_id[logical_cpu];
242}
243
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700244void cpu_initialize(unsigned int index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000245{
246 /* Because we busy wait at the printk spinlock.
247 * It is important to keep the number of printed messages
248 * from secondary cpus to a minimum, when debugging is
249 * disabled.
250 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000251 struct device *cpu;
Sven Schnelle51676b12012-07-29 19:18:03 +0200252 struct cpu_info *info;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000253 struct cpuinfo_x86 c;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000254
Sven Schnelle51676b12012-07-29 19:18:03 +0200255 info = cpu_info();
Eric Biederman83b991a2003-10-11 06:20:25 +0000256
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700257 printk(BIOS_INFO, "Initializing CPU #%d\n", index);
Kyösti Mälkkibc8c9962012-07-10 10:19:40 +0300258
Sven Schnelle51676b12012-07-29 19:18:03 +0200259 cpu = info->cpu;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700260 if (!cpu)
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200261 die("CPU: missing CPU device structure");
Li-Ta Lo8cb91dc2004-03-26 18:34:48 +0000262
Damien Zammit149c4c52015-11-28 21:27:05 +1100263 if (cpu->initialized)
264 return;
265
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700266 post_log_path(cpu);
267
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200268 /* Find what type of CPU we are dealing with */
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000269 identify_cpu(cpu);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000270 printk(BIOS_DEBUG, "CPU: vendor %s device %x\n",
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000271 cpu_vendor_name(cpu->vendor), cpu->device);
Li-Ta Lofd3f2d72004-05-12 16:34:46 +0000272
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000273 get_fms(&c, cpu->device);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000274
Stefan Reinauer5f5436f2010-04-25 20:42:02 +0000275 printk(BIOS_DEBUG, "CPU: family %02x, model %02x, stepping %02x\n",
276 c.x86, c.x86_model, c.x86_mask);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000277
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000278 /* Lookup the cpu's operations */
279 set_cpu_ops(cpu);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000280
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200281 if (!cpu->ops) {
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000282 /* mask out the stepping and try again */
283 cpu->device -= c.x86_mask;
Steven J. Magnanieccc3572005-09-14 13:53:45 +0000284 set_cpu_ops(cpu);
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000285 cpu->device += c.x86_mask;
Lee Leahy0b5678f2017-03-16 16:01:40 -0700286 if (!cpu->ops)
287 die("Unknown cpu");
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200288 printk(BIOS_DEBUG, "Using generic CPU ops (good)\n");
Ronald G. Minnich43225bc2005-11-22 00:07:02 +0000289 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000290
Sven Schnelle51676b12012-07-29 19:18:03 +0200291
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200292 /* Initialize the CPU */
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000293 if (cpu->ops && cpu->ops->init) {
294 cpu->enabled = 1;
295 cpu->initialized = 1;
296 cpu->ops->init(cpu);
297 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700298 post_log_clear();
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000299
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700300 printk(BIOS_INFO, "CPU #%d initialized\n", index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000301}
Aaron Durbinf6ada1c2016-02-10 10:52:47 -0600302
303void lb_arch_add_records(struct lb_header *header)
304{
Aaron Durbine0969ae2016-02-10 10:56:06 -0600305 uint32_t freq_khz;
306 struct lb_tsc_info *tsc_info;
307
308 /* Don't advertise a TSC rate unless it's constant. */
Kyösti Mälkki0d6ddf82019-10-31 14:52:20 +0200309 if (!tsc_constant_rate())
Aaron Durbine0969ae2016-02-10 10:56:06 -0600310 return;
311
312 freq_khz = tsc_freq_mhz() * 1000;
313
314 /* No use exposing a TSC frequency that is zero. */
315 if (freq_khz == 0)
316 return;
317
318 tsc_info = (void *)lb_new_record(header);
319 tsc_info->tag = LB_TAG_TSC_INFO;
320 tsc_info->size = sizeof(*tsc_info);
321 tsc_info->freq_khz = freq_khz;
Aaron Durbinf6ada1c2016-02-10 10:52:47 -0600322}
Aaron Durbinb21e3622016-12-07 00:32:19 -0600323
324void arch_bootstate_coreboot_exit(void)
325{
326 /* APs are already parked by existing infrastructure. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800327 if (!CONFIG(PARALLEL_MP_AP_WORK))
Aaron Durbinb21e3622016-12-07 00:32:19 -0600328 return;
329
330 /* APs are waiting for work. Last thing to do is park them. */
Furquan Shaikhd6630d12018-03-29 00:10:02 -0700331 mp_park_aps();
Aaron Durbinb21e3622016-12-07 00:32:19 -0600332}
Subrata Banik095c9312019-05-06 19:51:34 +0530333
334/*
335 * Previously cpu_index() implementation assumes that cpu_index()
336 * function will always getting called from coreboot context
337 * (ESP stack pointer will always refer to coreboot).
338 *
339 * But with FSP_USES_MP_SERVICES_PPI implementation in coreboot this
340 * assumption might not be true, where FSP context (stack pointer refers
341 * to FSP) will request to get cpu_index().
342 *
343 * Hence new logic to use cpuid to fetch lapic id and matches with
344 * cpus_default_apic_id[] variable to return correct cpu_index().
345 */
Jacob Garberbc674762019-05-14 11:21:41 -0600346int cpu_index(void)
Subrata Banik095c9312019-05-06 19:51:34 +0530347{
348 int i;
349 int lapic_id = initial_lapicid();
350
351 for (i = 0; i < CONFIG_MAX_CPUS; i++) {
352 if (cpu_get_apic_id(i) == lapic_id)
353 return i;
354 }
355 return -1;
356}
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700357
358uintptr_t cpu_get_lapic_addr(void)
359{
360 return LOCAL_APIC_ADDR;
361}