blob: 80d4d0da4adf7bbef2c0f864197469a9e87f4866 [file] [log] [blame]
Martin Roth9df9e9392016-01-12 15:55:28 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Aaron Durbinb21e3622016-12-07 00:32:19 -060014#include <bootstate.h>
Aaron Durbinf6ada1c2016-02-10 10:52:47 -060015#include <boot/coreboot_tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016#include <console/console.h>
17#include <cpu/cpu.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000018#include <string.h>
Aaron Durbinb21e3622016-12-07 00:32:19 -060019#include <cpu/x86/mp.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000020#include <cpu/x86/lapic.h>
Aaron Durbine0969ae2016-02-10 10:56:06 -060021#include <cpu/x86/tsc.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000022#include <arch/cpu.h>
23#include <device/path.h>
24#include <device/device.h>
25#include <smp/spinlock.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000026
Stefan Reinauer96938852015-06-18 01:23:48 -070027#ifndef __x86_64__
Eric Biedermanb78c1972004-10-14 20:54:17 +000028/* Standard macro to see if a specific flag is changeable */
29static inline int flag_is_changeable_p(uint32_t flag)
Eric Biederman8ca8d762003-04-22 19:02:15 +000030{
Eric Biedermanb78c1972004-10-14 20:54:17 +000031 uint32_t f1, f2;
Eric Biederman8ca8d762003-04-22 19:02:15 +000032
Eric Biedermanb78c1972004-10-14 20:54:17 +000033 asm(
34 "pushfl\n\t"
35 "pushfl\n\t"
36 "popl %0\n\t"
37 "movl %0,%1\n\t"
38 "xorl %2,%0\n\t"
39 "pushl %0\n\t"
40 "popfl\n\t"
41 "pushfl\n\t"
42 "popl %0\n\t"
43 "popfl\n\t"
44 : "=&r" (f1), "=&r" (f2)
45 : "ir" (flag));
46 return ((f1^f2) & flag) != 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000047}
48
Eric Biedermanb78c1972004-10-14 20:54:17 +000049/*
50 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
51 * by the fact that they preserve the flags across the division of 5/2.
52 * PII and PPro exhibit this behavior too, but they have cpuid available.
53 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000054
Eric Biedermanb78c1972004-10-14 20:54:17 +000055/*
56 * Perform the Cyrix 5/2 test. A Cyrix won't change
57 * the flags, while other 486 chips will.
58 */
59static inline int test_cyrix_52div(void)
60{
61 unsigned int test;
62
63 __asm__ __volatile__(
64 "sahf\n\t" /* clear flags (%eax = 0x0005) */
65 "div %b2\n\t" /* divide 5 by 2 */
66 "lahf" /* store flags into %ah */
67 : "=a" (test)
68 : "0" (5), "q" (2)
69 : "cc");
70
71 /* AH is 0x02 on Cyrix after the divide.. */
72 return (unsigned char) (test >> 8) == 0x02;
73}
74
75/*
76 * Detect a NexGen CPU running without BIOS hypercode new enough
77 * to have CPUID. (Thanks to Herbert Oppmann)
78 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000079
Eric Biedermanb78c1972004-10-14 20:54:17 +000080static int deep_magic_nexgen_probe(void)
81{
82 int ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +000083
Eric Biedermanb78c1972004-10-14 20:54:17 +000084 __asm__ __volatile__ (
85 " movw $0x5555, %%ax\n"
86 " xorw %%dx,%%dx\n"
87 " movw $2, %%cx\n"
88 " divw %%cx\n"
89 " movl $0, %%eax\n"
90 " jnz 1f\n"
91 " movl $1, %%eax\n"
Stefan Reinauer14e22772010-04-27 06:56:47 +000092 "1:\n"
Lee Leahy024b13d2017-03-16 13:41:11 -070093 : "=a" (ret) : : "cx", "dx");
Eric Biedermanb78c1972004-10-14 20:54:17 +000094 return ret;
95}
Stefan Reinauer96938852015-06-18 01:23:48 -070096#endif
Eric Biedermanb78c1972004-10-14 20:54:17 +000097
Elyes HAOUAS777ea892016-07-29 07:40:41 +020098/* List of CPU vendor strings along with their normalized
Eric Biedermanb78c1972004-10-14 20:54:17 +000099 * id values.
100 */
101static struct {
102 int vendor;
103 const char *name;
104} x86_vendors[] = {
105 { X86_VENDOR_INTEL, "GenuineIntel", },
106 { X86_VENDOR_CYRIX, "CyrixInstead", },
Stefan Reinauer14e22772010-04-27 06:56:47 +0000107 { X86_VENDOR_AMD, "AuthenticAMD", },
Eric Biedermanb78c1972004-10-14 20:54:17 +0000108 { X86_VENDOR_UMC, "UMC UMC UMC ", },
109 { X86_VENDOR_NEXGEN, "NexGenDriven", },
110 { X86_VENDOR_CENTAUR, "CentaurHauls", },
Martin Rothe3690102016-01-06 15:21:02 -0700111 { X86_VENDOR_RISE, "RiseRiseRise", },
112 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
Eric Biedermanb78c1972004-10-14 20:54:17 +0000113 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
114 { X86_VENDOR_NSC, "Geode by NSC", },
115 { X86_VENDOR_SIS, "SiS SiS SiS ", },
116};
117
Elyes HAOUAS6c9737b2018-07-08 12:30:02 +0200118static const char *const x86_vendor_name[] = {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000119 [X86_VENDOR_INTEL] = "Intel",
120 [X86_VENDOR_CYRIX] = "Cyrix",
121 [X86_VENDOR_AMD] = "AMD",
122 [X86_VENDOR_UMC] = "UMC",
123 [X86_VENDOR_NEXGEN] = "NexGen",
124 [X86_VENDOR_CENTAUR] = "Centaur",
125 [X86_VENDOR_RISE] = "Rise",
126 [X86_VENDOR_TRANSMETA] = "Transmeta",
127 [X86_VENDOR_NSC] = "NSC",
128 [X86_VENDOR_SIS] = "SiS",
129};
130
131static const char *cpu_vendor_name(int vendor)
132{
133 const char *name;
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200134 name = "<invalid CPU vendor>";
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000135 if ((vendor < (ARRAY_SIZE(x86_vendor_name))) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000136 (x86_vendor_name[vendor] != 0))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000137 name = x86_vendor_name[vendor];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000138 return name;
139}
140
141static void identify_cpu(struct device *cpu)
142{
143 char vendor_name[16];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000144 int i;
145
146 vendor_name[0] = '\0'; /* Unset */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000147
Stefan Reinauer96938852015-06-18 01:23:48 -0700148#ifndef __x86_64__
Eric Biedermanb78c1972004-10-14 20:54:17 +0000149 /* Find the id and vendor_name */
Rudolf Marek06253cd2012-02-25 23:51:12 +0100150 if (!cpu_have_cpuid()) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000151 /* Its a 486 if we can modify the AC flag */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700152 if (flag_is_changeable_p(X86_EFLAGS_AC))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000153 cpu->device = 0x00000400; /* 486 */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700154 else
Eric Biedermanb78c1972004-10-14 20:54:17 +0000155 cpu->device = 0x00000300; /* 386 */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700156 if ((cpu->device == 0x00000400) && test_cyrix_52div())
Eric Biedermanb78c1972004-10-14 20:54:17 +0000157 memcpy(vendor_name, "CyrixInstead", 13);
158 /* If we ever care we can enable cpuid here */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000159 /* Detect NexGen with old hypercode */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700160 else if (deep_magic_nexgen_probe())
Eric Biedermanb78c1972004-10-14 20:54:17 +0000161 memcpy(vendor_name, "NexGenDriven", 13);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000162 }
Stefan Reinauer96938852015-06-18 01:23:48 -0700163#endif
Rudolf Marek06253cd2012-02-25 23:51:12 +0100164 if (cpu_have_cpuid()) {
Stefan Reinauer4b556a12009-05-26 12:33:06 +0000165 int cpuid_level;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000166 struct cpuid_result result;
167 result = cpuid(0x00000000);
Lee Leahy024b13d2017-03-16 13:41:11 -0700168 cpuid_level = result.eax;
169 vendor_name[0] = (result.ebx >> 0) & 0xff;
170 vendor_name[1] = (result.ebx >> 8) & 0xff;
171 vendor_name[2] = (result.ebx >> 16) & 0xff;
172 vendor_name[3] = (result.ebx >> 24) & 0xff;
173 vendor_name[4] = (result.edx >> 0) & 0xff;
174 vendor_name[5] = (result.edx >> 8) & 0xff;
175 vendor_name[6] = (result.edx >> 16) & 0xff;
176 vendor_name[7] = (result.edx >> 24) & 0xff;
177 vendor_name[8] = (result.ecx >> 0) & 0xff;
178 vendor_name[9] = (result.ecx >> 8) & 0xff;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000179 vendor_name[10] = (result.ecx >> 16) & 0xff;
180 vendor_name[11] = (result.ecx >> 24) & 0xff;
181 vendor_name[12] = '\0';
Stefan Reinauer14e22772010-04-27 06:56:47 +0000182
Eric Biedermanb78c1972004-10-14 20:54:17 +0000183 /* Intel-defined flags: level 0x00000001 */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700184 if (cpuid_level >= 0x00000001)
Subrata Banik53b08c32018-12-10 14:11:35 +0530185 cpu->device = cpu_get_cpuid();
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700186 else
Eric Biedermanb78c1972004-10-14 20:54:17 +0000187 /* Have CPUID level 0 only unheard of */
188 cpu->device = 0x00000400;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000189 }
190 cpu->vendor = X86_VENDOR_UNKNOWN;
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200191 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000192 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
193 cpu->vendor = x86_vendors[i].vendor;
194 break;
195 }
196 }
197}
198
Stefan Reinauer6293d302012-04-03 16:07:56 -0700199struct cpu_driver *find_cpu_driver(struct device *cpu)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000200{
201 struct cpu_driver *driver;
Aaron Durbin03758152015-09-03 17:23:08 -0500202 for (driver = _cpu_drivers; driver < _ecpu_drivers; driver++) {
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100203 const struct cpu_device_id *id;
Stefan Reinauer6293d302012-04-03 16:07:56 -0700204 for (id = driver->id_table;
205 id->vendor != X86_VENDOR_INVALID; id++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000206 if ((cpu->vendor == id->vendor) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000207 (cpu->device == id->device))
Stefan Reinauer6293d302012-04-03 16:07:56 -0700208 return driver;
Lee Leahy0b5678f2017-03-16 16:01:40 -0700209 if (id->vendor == X86_VENDOR_ANY)
Gerd Hoffmanncbf30732013-05-31 09:23:26 +0200210 return driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000211 }
212 }
Stefan Reinauer6293d302012-04-03 16:07:56 -0700213 return NULL;
214}
215
216static void set_cpu_ops(struct device *cpu)
217{
218 struct cpu_driver *driver = find_cpu_driver(cpu);
219 cpu->ops = driver ? driver->ops : NULL;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000220}
221
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700222void cpu_initialize(unsigned int index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000223{
224 /* Because we busy wait at the printk spinlock.
225 * It is important to keep the number of printed messages
226 * from secondary cpus to a minimum, when debugging is
227 * disabled.
228 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000229 struct device *cpu;
Sven Schnelle51676b12012-07-29 19:18:03 +0200230 struct cpu_info *info;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000231 struct cpuinfo_x86 c;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000232
Sven Schnelle51676b12012-07-29 19:18:03 +0200233 info = cpu_info();
Eric Biederman83b991a2003-10-11 06:20:25 +0000234
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700235 printk(BIOS_INFO, "Initializing CPU #%d\n", index);
Kyösti Mälkkibc8c9962012-07-10 10:19:40 +0300236
Sven Schnelle51676b12012-07-29 19:18:03 +0200237 cpu = info->cpu;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700238 if (!cpu)
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200239 die("CPU: missing CPU device structure");
Li-Ta Lo8cb91dc2004-03-26 18:34:48 +0000240
Damien Zammit149c4c52015-11-28 21:27:05 +1100241 if (cpu->initialized)
242 return;
243
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700244 post_log_path(cpu);
245
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200246 /* Find what type of CPU we are dealing with */
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000247 identify_cpu(cpu);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000248 printk(BIOS_DEBUG, "CPU: vendor %s device %x\n",
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000249 cpu_vendor_name(cpu->vendor), cpu->device);
Li-Ta Lofd3f2d72004-05-12 16:34:46 +0000250
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000251 get_fms(&c, cpu->device);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000252
Stefan Reinauer5f5436f2010-04-25 20:42:02 +0000253 printk(BIOS_DEBUG, "CPU: family %02x, model %02x, stepping %02x\n",
254 c.x86, c.x86_model, c.x86_mask);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000255
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000256 /* Lookup the cpu's operations */
257 set_cpu_ops(cpu);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000258
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200259 if (!cpu->ops) {
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000260 /* mask out the stepping and try again */
261 cpu->device -= c.x86_mask;
Steven J. Magnanieccc3572005-09-14 13:53:45 +0000262 set_cpu_ops(cpu);
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000263 cpu->device += c.x86_mask;
Lee Leahy0b5678f2017-03-16 16:01:40 -0700264 if (!cpu->ops)
265 die("Unknown cpu");
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200266 printk(BIOS_DEBUG, "Using generic CPU ops (good)\n");
Ronald G. Minnich43225bc2005-11-22 00:07:02 +0000267 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000268
Sven Schnelle51676b12012-07-29 19:18:03 +0200269
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200270 /* Initialize the CPU */
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000271 if (cpu->ops && cpu->ops->init) {
272 cpu->enabled = 1;
273 cpu->initialized = 1;
274 cpu->ops->init(cpu);
275 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700276 post_log_clear();
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000277
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700278 printk(BIOS_INFO, "CPU #%d initialized\n", index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000279}
Aaron Durbinf6ada1c2016-02-10 10:52:47 -0600280
281void lb_arch_add_records(struct lb_header *header)
282{
Aaron Durbine0969ae2016-02-10 10:56:06 -0600283 uint32_t freq_khz;
284 struct lb_tsc_info *tsc_info;
285
286 /* Don't advertise a TSC rate unless it's constant. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800287 if (!CONFIG(TSC_CONSTANT_RATE))
Aaron Durbine0969ae2016-02-10 10:56:06 -0600288 return;
289
290 freq_khz = tsc_freq_mhz() * 1000;
291
292 /* No use exposing a TSC frequency that is zero. */
293 if (freq_khz == 0)
294 return;
295
296 tsc_info = (void *)lb_new_record(header);
297 tsc_info->tag = LB_TAG_TSC_INFO;
298 tsc_info->size = sizeof(*tsc_info);
299 tsc_info->freq_khz = freq_khz;
Aaron Durbinf6ada1c2016-02-10 10:52:47 -0600300}
Aaron Durbinb21e3622016-12-07 00:32:19 -0600301
302void arch_bootstate_coreboot_exit(void)
303{
304 /* APs are already parked by existing infrastructure. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800305 if (!CONFIG(PARALLEL_MP_AP_WORK))
Aaron Durbinb21e3622016-12-07 00:32:19 -0600306 return;
307
308 /* APs are waiting for work. Last thing to do is park them. */
Furquan Shaikhd6630d12018-03-29 00:10:02 -0700309 mp_park_aps();
Aaron Durbinb21e3622016-12-07 00:32:19 -0600310}