blob: d46e591dddbf4680520ea8cd6e44e5506750b450 [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
Eric Biederman8ca8d762003-04-22 19:02:15 +000014#include <console/console.h>
15#include <cpu/cpu.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016#include <arch/io.h>
17#include <string.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000018#include <cpu/x86/mtrr.h>
19#include <cpu/x86/msr.h>
20#include <cpu/x86/lapic.h>
21#include <arch/cpu.h>
22#include <device/path.h>
23#include <device/device.h>
24#include <smp/spinlock.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000025
Stefan Reinauer96938852015-06-18 01:23:48 -070026#ifndef __x86_64__
Eric Biedermanb78c1972004-10-14 20:54:17 +000027/* Standard macro to see if a specific flag is changeable */
28static inline int flag_is_changeable_p(uint32_t flag)
Eric Biederman8ca8d762003-04-22 19:02:15 +000029{
Eric Biedermanb78c1972004-10-14 20:54:17 +000030 uint32_t f1, f2;
Eric Biederman8ca8d762003-04-22 19:02:15 +000031
Eric Biedermanb78c1972004-10-14 20:54:17 +000032 asm(
33 "pushfl\n\t"
34 "pushfl\n\t"
35 "popl %0\n\t"
36 "movl %0,%1\n\t"
37 "xorl %2,%0\n\t"
38 "pushl %0\n\t"
39 "popfl\n\t"
40 "pushfl\n\t"
41 "popl %0\n\t"
42 "popfl\n\t"
43 : "=&r" (f1), "=&r" (f2)
44 : "ir" (flag));
45 return ((f1^f2) & flag) != 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000046}
47
Eric Biedermanb78c1972004-10-14 20:54:17 +000048/*
49 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
50 * by the fact that they preserve the flags across the division of 5/2.
51 * PII and PPro exhibit this behavior too, but they have cpuid available.
52 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000053
Eric Biedermanb78c1972004-10-14 20:54:17 +000054/*
55 * Perform the Cyrix 5/2 test. A Cyrix won't change
56 * the flags, while other 486 chips will.
57 */
58static inline int test_cyrix_52div(void)
59{
60 unsigned int test;
61
62 __asm__ __volatile__(
63 "sahf\n\t" /* clear flags (%eax = 0x0005) */
64 "div %b2\n\t" /* divide 5 by 2 */
65 "lahf" /* store flags into %ah */
66 : "=a" (test)
67 : "0" (5), "q" (2)
68 : "cc");
69
70 /* AH is 0x02 on Cyrix after the divide.. */
71 return (unsigned char) (test >> 8) == 0x02;
72}
73
74/*
75 * Detect a NexGen CPU running without BIOS hypercode new enough
76 * to have CPUID. (Thanks to Herbert Oppmann)
77 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000078
Eric Biedermanb78c1972004-10-14 20:54:17 +000079static int deep_magic_nexgen_probe(void)
80{
81 int ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +000082
Eric Biedermanb78c1972004-10-14 20:54:17 +000083 __asm__ __volatile__ (
84 " movw $0x5555, %%ax\n"
85 " xorw %%dx,%%dx\n"
86 " movw $2, %%cx\n"
87 " divw %%cx\n"
88 " movl $0, %%eax\n"
89 " jnz 1f\n"
90 " movl $1, %%eax\n"
Stefan Reinauer14e22772010-04-27 06:56:47 +000091 "1:\n"
Eric Biedermanb78c1972004-10-14 20:54:17 +000092 : "=a" (ret) : : "cx", "dx" );
93 return ret;
94}
Stefan Reinauer96938852015-06-18 01:23:48 -070095#endif
Eric Biedermanb78c1972004-10-14 20:54:17 +000096
97/* List of cpu vendor strings along with their normalized
98 * id values.
99 */
100static struct {
101 int vendor;
102 const char *name;
103} x86_vendors[] = {
104 { X86_VENDOR_INTEL, "GenuineIntel", },
105 { X86_VENDOR_CYRIX, "CyrixInstead", },
Stefan Reinauer14e22772010-04-27 06:56:47 +0000106 { X86_VENDOR_AMD, "AuthenticAMD", },
Eric Biedermanb78c1972004-10-14 20:54:17 +0000107 { X86_VENDOR_UMC, "UMC UMC UMC ", },
108 { X86_VENDOR_NEXGEN, "NexGenDriven", },
109 { X86_VENDOR_CENTAUR, "CentaurHauls", },
Martin Rothe3690102016-01-06 15:21:02 -0700110 { X86_VENDOR_RISE, "RiseRiseRise", },
111 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
Eric Biedermanb78c1972004-10-14 20:54:17 +0000112 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
113 { X86_VENDOR_NSC, "Geode by NSC", },
114 { X86_VENDOR_SIS, "SiS SiS SiS ", },
115};
116
117static const char *x86_vendor_name[] = {
118 [X86_VENDOR_INTEL] = "Intel",
119 [X86_VENDOR_CYRIX] = "Cyrix",
120 [X86_VENDOR_AMD] = "AMD",
121 [X86_VENDOR_UMC] = "UMC",
122 [X86_VENDOR_NEXGEN] = "NexGen",
123 [X86_VENDOR_CENTAUR] = "Centaur",
124 [X86_VENDOR_RISE] = "Rise",
125 [X86_VENDOR_TRANSMETA] = "Transmeta",
126 [X86_VENDOR_NSC] = "NSC",
127 [X86_VENDOR_SIS] = "SiS",
128};
129
130static const char *cpu_vendor_name(int vendor)
131{
132 const char *name;
133 name = "<invalid cpu vendor>";
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000134 if ((vendor < (ARRAY_SIZE(x86_vendor_name))) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000135 (x86_vendor_name[vendor] != 0))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000136 {
137 name = x86_vendor_name[vendor];
138 }
139 return name;
140}
141
142static void identify_cpu(struct device *cpu)
143{
144 char vendor_name[16];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000145 int i;
146
147 vendor_name[0] = '\0'; /* Unset */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000148
Stefan Reinauer96938852015-06-18 01:23:48 -0700149#ifndef __x86_64__
Eric Biedermanb78c1972004-10-14 20:54:17 +0000150 /* Find the id and vendor_name */
Rudolf Marek06253cd2012-02-25 23:51:12 +0100151 if (!cpu_have_cpuid()) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000152 /* Its a 486 if we can modify the AC flag */
153 if (flag_is_changeable_p(X86_EFLAGS_AC)) {
154 cpu->device = 0x00000400; /* 486 */
155 } else {
156 cpu->device = 0x00000300; /* 386 */
157 }
158 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
159 memcpy(vendor_name, "CyrixInstead", 13);
160 /* If we ever care we can enable cpuid here */
161 }
162 /* Detect NexGen with old hypercode */
163 else if (deep_magic_nexgen_probe()) {
164 memcpy(vendor_name, "NexGenDriven", 13);
165 }
166 }
Stefan Reinauer96938852015-06-18 01:23:48 -0700167#endif
Rudolf Marek06253cd2012-02-25 23:51:12 +0100168 if (cpu_have_cpuid()) {
Stefan Reinauer4b556a12009-05-26 12:33:06 +0000169 int cpuid_level;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000170 struct cpuid_result result;
171 result = cpuid(0x00000000);
172 cpuid_level = result.eax;
173 vendor_name[ 0] = (result.ebx >> 0) & 0xff;
174 vendor_name[ 1] = (result.ebx >> 8) & 0xff;
175 vendor_name[ 2] = (result.ebx >> 16) & 0xff;
176 vendor_name[ 3] = (result.ebx >> 24) & 0xff;
177 vendor_name[ 4] = (result.edx >> 0) & 0xff;
178 vendor_name[ 5] = (result.edx >> 8) & 0xff;
179 vendor_name[ 6] = (result.edx >> 16) & 0xff;
180 vendor_name[ 7] = (result.edx >> 24) & 0xff;
181 vendor_name[ 8] = (result.ecx >> 0) & 0xff;
182 vendor_name[ 9] = (result.ecx >> 8) & 0xff;
183 vendor_name[10] = (result.ecx >> 16) & 0xff;
184 vendor_name[11] = (result.ecx >> 24) & 0xff;
185 vendor_name[12] = '\0';
Stefan Reinauer14e22772010-04-27 06:56:47 +0000186
Eric Biedermanb78c1972004-10-14 20:54:17 +0000187 /* Intel-defined flags: level 0x00000001 */
188 if (cpuid_level >= 0x00000001) {
189 cpu->device = cpuid_eax(0x00000001);
190 }
191 else {
192 /* Have CPUID level 0 only unheard of */
193 cpu->device = 0x00000400;
194 }
195 }
196 cpu->vendor = X86_VENDOR_UNKNOWN;
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000197 for(i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000198 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
199 cpu->vendor = x86_vendors[i].vendor;
200 break;
201 }
202 }
203}
204
Stefan Reinauer6293d302012-04-03 16:07:56 -0700205struct cpu_driver *find_cpu_driver(struct device *cpu)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000206{
207 struct cpu_driver *driver;
Aaron Durbin03758152015-09-03 17:23:08 -0500208 for (driver = _cpu_drivers; driver < _ecpu_drivers; driver++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000209 struct cpu_device_id *id;
Stefan Reinauer6293d302012-04-03 16:07:56 -0700210 for (id = driver->id_table;
211 id->vendor != X86_VENDOR_INVALID; id++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000212 if ((cpu->vendor == id->vendor) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000213 (cpu->device == id->device))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000214 {
Stefan Reinauer6293d302012-04-03 16:07:56 -0700215 return driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000216 }
Gerd Hoffmanncbf30732013-05-31 09:23:26 +0200217 if (X86_VENDOR_ANY == id->vendor)
218 return driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000219 }
220 }
Stefan Reinauer6293d302012-04-03 16:07:56 -0700221 return NULL;
222}
223
224static void set_cpu_ops(struct device *cpu)
225{
226 struct cpu_driver *driver = find_cpu_driver(cpu);
227 cpu->ops = driver ? driver->ops : NULL;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000228}
229
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700230void cpu_initialize(unsigned int index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231{
232 /* Because we busy wait at the printk spinlock.
233 * It is important to keep the number of printed messages
234 * from secondary cpus to a minimum, when debugging is
235 * disabled.
236 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000237 struct device *cpu;
Sven Schnelle51676b12012-07-29 19:18:03 +0200238 struct cpu_info *info;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000239 struct cpuinfo_x86 c;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
Sven Schnelle51676b12012-07-29 19:18:03 +0200241 info = cpu_info();
Eric Biederman83b991a2003-10-11 06:20:25 +0000242
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700243 printk(BIOS_INFO, "Initializing CPU #%d\n", index);
Kyösti Mälkkibc8c9962012-07-10 10:19:40 +0300244
Sven Schnelle51676b12012-07-29 19:18:03 +0200245 cpu = info->cpu;
246 if (!cpu) {
247 die("CPU: missing cpu device structure");
248 }
Li-Ta Lo8cb91dc2004-03-26 18:34:48 +0000249
Damien Zammit149c4c52015-11-28 21:27:05 +1100250 if (cpu->initialized)
251 return;
252
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700253 post_log_path(cpu);
254
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000255 /* Find what type of cpu we are dealing with */
256 identify_cpu(cpu);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000257 printk(BIOS_DEBUG, "CPU: vendor %s device %x\n",
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000258 cpu_vendor_name(cpu->vendor), cpu->device);
Li-Ta Lofd3f2d72004-05-12 16:34:46 +0000259
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000260 get_fms(&c, cpu->device);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000261
Stefan Reinauer5f5436f2010-04-25 20:42:02 +0000262 printk(BIOS_DEBUG, "CPU: family %02x, model %02x, stepping %02x\n",
263 c.x86, c.x86_model, c.x86_mask);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000264
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000265 /* Lookup the cpu's operations */
266 set_cpu_ops(cpu);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000267
Stefan Reinauer14e22772010-04-27 06:56:47 +0000268 if(!cpu->ops) {
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000269 /* mask out the stepping and try again */
270 cpu->device -= c.x86_mask;
Steven J. Magnanieccc3572005-09-14 13:53:45 +0000271 set_cpu_ops(cpu);
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000272 cpu->device += c.x86_mask;
273 if(!cpu->ops) die("Unknown cpu");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000274 printk(BIOS_DEBUG, "Using generic cpu ops (good)\n");
Ronald G. Minnich43225bc2005-11-22 00:07:02 +0000275 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000276
Sven Schnelle51676b12012-07-29 19:18:03 +0200277
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000278 /* Initialize the cpu */
279 if (cpu->ops && cpu->ops->init) {
280 cpu->enabled = 1;
281 cpu->initialized = 1;
282 cpu->ops->init(cpu);
283 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700284 post_log_clear();
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000285
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700286 printk(BIOS_INFO, "CPU #%d initialized\n", index);
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000287
Eric Biedermanb78c1972004-10-14 20:54:17 +0000288 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000289}