blob: bddfe1ae12faac2d8b89f6a86f4a332876334fa3 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
2#include <cpu/cpu.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00003#include <arch/io.h>
4#include <string.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +00005#include <cpu/x86/mtrr.h>
6#include <cpu/x86/msr.h>
7#include <cpu/x86/lapic.h>
8#include <arch/cpu.h>
9#include <device/path.h>
10#include <device/device.h>
11#include <smp/spinlock.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000012
Eric Biedermanb78c1972004-10-14 20:54:17 +000013/* Standard macro to see if a specific flag is changeable */
14static inline int flag_is_changeable_p(uint32_t flag)
Eric Biederman8ca8d762003-04-22 19:02:15 +000015{
Eric Biedermanb78c1972004-10-14 20:54:17 +000016 uint32_t f1, f2;
Eric Biederman8ca8d762003-04-22 19:02:15 +000017
Eric Biedermanb78c1972004-10-14 20:54:17 +000018 asm(
19 "pushfl\n\t"
20 "pushfl\n\t"
21 "popl %0\n\t"
22 "movl %0,%1\n\t"
23 "xorl %2,%0\n\t"
24 "pushl %0\n\t"
25 "popfl\n\t"
26 "pushfl\n\t"
27 "popl %0\n\t"
28 "popfl\n\t"
29 : "=&r" (f1), "=&r" (f2)
30 : "ir" (flag));
31 return ((f1^f2) & flag) != 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000032}
33
Eric Biedermanb78c1972004-10-14 20:54:17 +000034/* Probe for the CPUID instruction */
Rudolf Marek06253cd2012-02-25 23:51:12 +010035int cpu_have_cpuid(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +000036{
Eric Biedermanb78c1972004-10-14 20:54:17 +000037 return flag_is_changeable_p(X86_EFLAGS_ID);
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"
Eric Biedermanb78c1972004-10-14 20:54:17 +000084 : "=a" (ret) : : "cx", "dx" );
85 return ret;
86}
87
88/* List of cpu vendor strings along with their normalized
89 * id values.
90 */
91static struct {
92 int vendor;
93 const char *name;
94} x86_vendors[] = {
95 { X86_VENDOR_INTEL, "GenuineIntel", },
96 { X86_VENDOR_CYRIX, "CyrixInstead", },
Stefan Reinauer14e22772010-04-27 06:56:47 +000097 { X86_VENDOR_AMD, "AuthenticAMD", },
Eric Biedermanb78c1972004-10-14 20:54:17 +000098 { X86_VENDOR_UMC, "UMC UMC UMC ", },
99 { X86_VENDOR_NEXGEN, "NexGenDriven", },
100 { X86_VENDOR_CENTAUR, "CentaurHauls", },
101 { X86_VENDOR_RISE, "RiseRiseRise", },
102 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
103 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
104 { X86_VENDOR_NSC, "Geode by NSC", },
105 { X86_VENDOR_SIS, "SiS SiS SiS ", },
106};
107
108static const char *x86_vendor_name[] = {
109 [X86_VENDOR_INTEL] = "Intel",
110 [X86_VENDOR_CYRIX] = "Cyrix",
111 [X86_VENDOR_AMD] = "AMD",
112 [X86_VENDOR_UMC] = "UMC",
113 [X86_VENDOR_NEXGEN] = "NexGen",
114 [X86_VENDOR_CENTAUR] = "Centaur",
115 [X86_VENDOR_RISE] = "Rise",
116 [X86_VENDOR_TRANSMETA] = "Transmeta",
117 [X86_VENDOR_NSC] = "NSC",
118 [X86_VENDOR_SIS] = "SiS",
119};
120
121static const char *cpu_vendor_name(int vendor)
122{
123 const char *name;
124 name = "<invalid cpu vendor>";
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000125 if ((vendor < (ARRAY_SIZE(x86_vendor_name))) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000126 (x86_vendor_name[vendor] != 0))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000127 {
128 name = x86_vendor_name[vendor];
129 }
130 return name;
131}
132
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100133static int cpu_cpuid_extended_level(void)
134{
135 return cpuid_eax(0x80000000);
136}
137
138#define CPUID_FEATURE_PAE (1 << 6)
139#define CPUID_FEATURE_PSE36 (1 << 17)
140
141int cpu_phys_address_size(void)
142{
Rudolf Marek06253cd2012-02-25 23:51:12 +0100143 if (!(cpu_have_cpuid()))
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100144 return 32;
145
Sven Schnelle7363ca32012-01-31 22:10:28 +0100146 if (cpu_cpuid_extended_level() >= 0x80000008)
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100147 return cpuid_eax(0x80000008) & 0xff;
148
Kyösti Mälkki730c0ee2012-02-29 21:17:18 +0200149 if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100150 return 36;
151 return 32;
152}
Eric Biedermanb78c1972004-10-14 20:54:17 +0000153static void identify_cpu(struct device *cpu)
154{
155 char vendor_name[16];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000156 int i;
157
158 vendor_name[0] = '\0'; /* Unset */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000159
160 /* Find the id and vendor_name */
Rudolf Marek06253cd2012-02-25 23:51:12 +0100161 if (!cpu_have_cpuid()) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000162 /* Its a 486 if we can modify the AC flag */
163 if (flag_is_changeable_p(X86_EFLAGS_AC)) {
164 cpu->device = 0x00000400; /* 486 */
165 } else {
166 cpu->device = 0x00000300; /* 386 */
167 }
168 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
169 memcpy(vendor_name, "CyrixInstead", 13);
170 /* If we ever care we can enable cpuid here */
171 }
172 /* Detect NexGen with old hypercode */
173 else if (deep_magic_nexgen_probe()) {
174 memcpy(vendor_name, "NexGenDriven", 13);
175 }
176 }
Rudolf Marek06253cd2012-02-25 23:51:12 +0100177 if (cpu_have_cpuid()) {
Stefan Reinauer4b556a12009-05-26 12:33:06 +0000178 int cpuid_level;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000179 struct cpuid_result result;
180 result = cpuid(0x00000000);
181 cpuid_level = result.eax;
182 vendor_name[ 0] = (result.ebx >> 0) & 0xff;
183 vendor_name[ 1] = (result.ebx >> 8) & 0xff;
184 vendor_name[ 2] = (result.ebx >> 16) & 0xff;
185 vendor_name[ 3] = (result.ebx >> 24) & 0xff;
186 vendor_name[ 4] = (result.edx >> 0) & 0xff;
187 vendor_name[ 5] = (result.edx >> 8) & 0xff;
188 vendor_name[ 6] = (result.edx >> 16) & 0xff;
189 vendor_name[ 7] = (result.edx >> 24) & 0xff;
190 vendor_name[ 8] = (result.ecx >> 0) & 0xff;
191 vendor_name[ 9] = (result.ecx >> 8) & 0xff;
192 vendor_name[10] = (result.ecx >> 16) & 0xff;
193 vendor_name[11] = (result.ecx >> 24) & 0xff;
194 vendor_name[12] = '\0';
Stefan Reinauer14e22772010-04-27 06:56:47 +0000195
Eric Biedermanb78c1972004-10-14 20:54:17 +0000196 /* Intel-defined flags: level 0x00000001 */
197 if (cpuid_level >= 0x00000001) {
198 cpu->device = cpuid_eax(0x00000001);
199 }
200 else {
201 /* Have CPUID level 0 only unheard of */
202 cpu->device = 0x00000400;
203 }
204 }
205 cpu->vendor = X86_VENDOR_UNKNOWN;
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000206 for(i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000207 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
208 cpu->vendor = x86_vendors[i].vendor;
209 break;
210 }
211 }
212}
213
Stefan Reinauer6293d302012-04-03 16:07:56 -0700214struct cpu_driver *find_cpu_driver(struct device *cpu)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000215{
216 struct cpu_driver *driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000217 for (driver = cpu_drivers; driver < ecpu_drivers; driver++) {
218 struct cpu_device_id *id;
Stefan Reinauer6293d302012-04-03 16:07:56 -0700219 for (id = driver->id_table;
220 id->vendor != X86_VENDOR_INVALID; id++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000221 if ((cpu->vendor == id->vendor) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +0000222 (cpu->device == id->device))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000223 {
Stefan Reinauer6293d302012-04-03 16:07:56 -0700224 return driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000225 }
Gerd Hoffmanncbf30732013-05-31 09:23:26 +0200226 if (X86_VENDOR_ANY == id->vendor)
227 return driver;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000228 }
229 }
Stefan Reinauer6293d302012-04-03 16:07:56 -0700230 return NULL;
231}
232
233static void set_cpu_ops(struct device *cpu)
234{
235 struct cpu_driver *driver = find_cpu_driver(cpu);
236 cpu->ops = driver ? driver->ops : NULL;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000237}
238
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700239void cpu_initialize(unsigned int index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000240{
241 /* Because we busy wait at the printk spinlock.
242 * It is important to keep the number of printed messages
243 * from secondary cpus to a minimum, when debugging is
244 * disabled.
245 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000246 struct device *cpu;
Sven Schnelle51676b12012-07-29 19:18:03 +0200247 struct cpu_info *info;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000248 struct cpuinfo_x86 c;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000249
Sven Schnelle51676b12012-07-29 19:18:03 +0200250 info = cpu_info();
Eric Biederman83b991a2003-10-11 06:20:25 +0000251
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700252 printk(BIOS_INFO, "Initializing CPU #%d\n", index);
Kyösti Mälkkibc8c9962012-07-10 10:19:40 +0300253
Sven Schnelle51676b12012-07-29 19:18:03 +0200254 cpu = info->cpu;
255 if (!cpu) {
256 die("CPU: missing cpu device structure");
257 }
Li-Ta Lo8cb91dc2004-03-26 18:34:48 +0000258
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000259 /* Find what type of cpu we are dealing with */
260 identify_cpu(cpu);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000261 printk(BIOS_DEBUG, "CPU: vendor %s device %x\n",
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000262 cpu_vendor_name(cpu->vendor), cpu->device);
Li-Ta Lofd3f2d72004-05-12 16:34:46 +0000263
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000264 get_fms(&c, cpu->device);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000265
Stefan Reinauer5f5436f2010-04-25 20:42:02 +0000266 printk(BIOS_DEBUG, "CPU: family %02x, model %02x, stepping %02x\n",
267 c.x86, c.x86_model, c.x86_mask);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000268
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000269 /* Lookup the cpu's operations */
270 set_cpu_ops(cpu);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000271
Stefan Reinauer14e22772010-04-27 06:56:47 +0000272 if(!cpu->ops) {
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000273 /* mask out the stepping and try again */
274 cpu->device -= c.x86_mask;
Steven J. Magnanieccc3572005-09-14 13:53:45 +0000275 set_cpu_ops(cpu);
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000276 cpu->device += c.x86_mask;
277 if(!cpu->ops) die("Unknown cpu");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000278 printk(BIOS_DEBUG, "Using generic cpu ops (good)\n");
Ronald G. Minnich43225bc2005-11-22 00:07:02 +0000279 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000280
Sven Schnelle51676b12012-07-29 19:18:03 +0200281
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000282 /* Initialize the cpu */
283 if (cpu->ops && cpu->ops->init) {
284 cpu->enabled = 1;
285 cpu->initialized = 1;
286 cpu->ops->init(cpu);
287 }
288
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700289 printk(BIOS_INFO, "CPU #%d initialized\n", index);
Yinghai Lu30b4abe2007-04-06 19:57:42 +0000290
Eric Biedermanb78c1972004-10-14 20:54:17 +0000291 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000292}
293