Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
| 2 | #include <cpu/cpu.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 3 | #include <arch/io.h> |
| 4 | #include <string.h> |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 5 | #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 Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 12 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 13 | #ifndef __x86_64__ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 14 | /* Standard macro to see if a specific flag is changeable */ |
| 15 | static inline int flag_is_changeable_p(uint32_t flag) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 17 | uint32_t f1, f2; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 18 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 19 | asm( |
| 20 | "pushfl\n\t" |
| 21 | "pushfl\n\t" |
| 22 | "popl %0\n\t" |
| 23 | "movl %0,%1\n\t" |
| 24 | "xorl %2,%0\n\t" |
| 25 | "pushl %0\n\t" |
| 26 | "popfl\n\t" |
| 27 | "pushfl\n\t" |
| 28 | "popl %0\n\t" |
| 29 | "popfl\n\t" |
| 30 | : "=&r" (f1), "=&r" (f2) |
| 31 | : "ir" (flag)); |
| 32 | return ((f1^f2) & flag) != 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected |
| 37 | * by the fact that they preserve the flags across the division of 5/2. |
| 38 | * PII and PPro exhibit this behavior too, but they have cpuid available. |
| 39 | */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 40 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 41 | /* |
| 42 | * Perform the Cyrix 5/2 test. A Cyrix won't change |
| 43 | * the flags, while other 486 chips will. |
| 44 | */ |
| 45 | static inline int test_cyrix_52div(void) |
| 46 | { |
| 47 | unsigned int test; |
| 48 | |
| 49 | __asm__ __volatile__( |
| 50 | "sahf\n\t" /* clear flags (%eax = 0x0005) */ |
| 51 | "div %b2\n\t" /* divide 5 by 2 */ |
| 52 | "lahf" /* store flags into %ah */ |
| 53 | : "=a" (test) |
| 54 | : "0" (5), "q" (2) |
| 55 | : "cc"); |
| 56 | |
| 57 | /* AH is 0x02 on Cyrix after the divide.. */ |
| 58 | return (unsigned char) (test >> 8) == 0x02; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Detect a NexGen CPU running without BIOS hypercode new enough |
| 63 | * to have CPUID. (Thanks to Herbert Oppmann) |
| 64 | */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 65 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 66 | static int deep_magic_nexgen_probe(void) |
| 67 | { |
| 68 | int ret; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 69 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 70 | __asm__ __volatile__ ( |
| 71 | " movw $0x5555, %%ax\n" |
| 72 | " xorw %%dx,%%dx\n" |
| 73 | " movw $2, %%cx\n" |
| 74 | " divw %%cx\n" |
| 75 | " movl $0, %%eax\n" |
| 76 | " jnz 1f\n" |
| 77 | " movl $1, %%eax\n" |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 78 | "1:\n" |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 79 | : "=a" (ret) : : "cx", "dx" ); |
| 80 | return ret; |
| 81 | } |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 82 | #endif |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 83 | |
| 84 | /* List of cpu vendor strings along with their normalized |
| 85 | * id values. |
| 86 | */ |
| 87 | static struct { |
| 88 | int vendor; |
| 89 | const char *name; |
| 90 | } x86_vendors[] = { |
| 91 | { X86_VENDOR_INTEL, "GenuineIntel", }, |
| 92 | { X86_VENDOR_CYRIX, "CyrixInstead", }, |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 93 | { X86_VENDOR_AMD, "AuthenticAMD", }, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 94 | { X86_VENDOR_UMC, "UMC UMC UMC ", }, |
| 95 | { X86_VENDOR_NEXGEN, "NexGenDriven", }, |
| 96 | { X86_VENDOR_CENTAUR, "CentaurHauls", }, |
| 97 | { X86_VENDOR_RISE, "RiseRiseRise", }, |
| 98 | { X86_VENDOR_TRANSMETA, "GenuineTMx86", }, |
| 99 | { X86_VENDOR_TRANSMETA, "TransmetaCPU", }, |
| 100 | { X86_VENDOR_NSC, "Geode by NSC", }, |
| 101 | { X86_VENDOR_SIS, "SiS SiS SiS ", }, |
| 102 | }; |
| 103 | |
| 104 | static const char *x86_vendor_name[] = { |
| 105 | [X86_VENDOR_INTEL] = "Intel", |
| 106 | [X86_VENDOR_CYRIX] = "Cyrix", |
| 107 | [X86_VENDOR_AMD] = "AMD", |
| 108 | [X86_VENDOR_UMC] = "UMC", |
| 109 | [X86_VENDOR_NEXGEN] = "NexGen", |
| 110 | [X86_VENDOR_CENTAUR] = "Centaur", |
| 111 | [X86_VENDOR_RISE] = "Rise", |
| 112 | [X86_VENDOR_TRANSMETA] = "Transmeta", |
| 113 | [X86_VENDOR_NSC] = "NSC", |
| 114 | [X86_VENDOR_SIS] = "SiS", |
| 115 | }; |
| 116 | |
| 117 | static const char *cpu_vendor_name(int vendor) |
| 118 | { |
| 119 | const char *name; |
| 120 | name = "<invalid cpu vendor>"; |
Carl-Daniel Hailfinger | 2ee6779 | 2008-10-01 12:52:52 +0000 | [diff] [blame] | 121 | if ((vendor < (ARRAY_SIZE(x86_vendor_name))) && |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 122 | (x86_vendor_name[vendor] != 0)) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 123 | { |
| 124 | name = x86_vendor_name[vendor]; |
| 125 | } |
| 126 | return name; |
| 127 | } |
| 128 | |
| 129 | static void identify_cpu(struct device *cpu) |
| 130 | { |
| 131 | char vendor_name[16]; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 132 | int i; |
| 133 | |
| 134 | vendor_name[0] = '\0'; /* Unset */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 135 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 136 | #ifndef __x86_64__ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 137 | /* Find the id and vendor_name */ |
Rudolf Marek | 06253cd | 2012-02-25 23:51:12 +0100 | [diff] [blame] | 138 | if (!cpu_have_cpuid()) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 139 | /* Its a 486 if we can modify the AC flag */ |
| 140 | if (flag_is_changeable_p(X86_EFLAGS_AC)) { |
| 141 | cpu->device = 0x00000400; /* 486 */ |
| 142 | } else { |
| 143 | cpu->device = 0x00000300; /* 386 */ |
| 144 | } |
| 145 | if ((cpu->device == 0x00000400) && test_cyrix_52div()) { |
| 146 | memcpy(vendor_name, "CyrixInstead", 13); |
| 147 | /* If we ever care we can enable cpuid here */ |
| 148 | } |
| 149 | /* Detect NexGen with old hypercode */ |
| 150 | else if (deep_magic_nexgen_probe()) { |
| 151 | memcpy(vendor_name, "NexGenDriven", 13); |
| 152 | } |
| 153 | } |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 154 | #endif |
Rudolf Marek | 06253cd | 2012-02-25 23:51:12 +0100 | [diff] [blame] | 155 | if (cpu_have_cpuid()) { |
Stefan Reinauer | 4b556a1 | 2009-05-26 12:33:06 +0000 | [diff] [blame] | 156 | int cpuid_level; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 157 | struct cpuid_result result; |
| 158 | result = cpuid(0x00000000); |
| 159 | cpuid_level = result.eax; |
| 160 | vendor_name[ 0] = (result.ebx >> 0) & 0xff; |
| 161 | vendor_name[ 1] = (result.ebx >> 8) & 0xff; |
| 162 | vendor_name[ 2] = (result.ebx >> 16) & 0xff; |
| 163 | vendor_name[ 3] = (result.ebx >> 24) & 0xff; |
| 164 | vendor_name[ 4] = (result.edx >> 0) & 0xff; |
| 165 | vendor_name[ 5] = (result.edx >> 8) & 0xff; |
| 166 | vendor_name[ 6] = (result.edx >> 16) & 0xff; |
| 167 | vendor_name[ 7] = (result.edx >> 24) & 0xff; |
| 168 | vendor_name[ 8] = (result.ecx >> 0) & 0xff; |
| 169 | vendor_name[ 9] = (result.ecx >> 8) & 0xff; |
| 170 | vendor_name[10] = (result.ecx >> 16) & 0xff; |
| 171 | vendor_name[11] = (result.ecx >> 24) & 0xff; |
| 172 | vendor_name[12] = '\0'; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 173 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 174 | /* Intel-defined flags: level 0x00000001 */ |
| 175 | if (cpuid_level >= 0x00000001) { |
| 176 | cpu->device = cpuid_eax(0x00000001); |
| 177 | } |
| 178 | else { |
| 179 | /* Have CPUID level 0 only unheard of */ |
| 180 | cpu->device = 0x00000400; |
| 181 | } |
| 182 | } |
| 183 | cpu->vendor = X86_VENDOR_UNKNOWN; |
Carl-Daniel Hailfinger | 2ee6779 | 2008-10-01 12:52:52 +0000 | [diff] [blame] | 184 | for(i = 0; i < ARRAY_SIZE(x86_vendors); i++) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 185 | if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) { |
| 186 | cpu->vendor = x86_vendors[i].vendor; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Stefan Reinauer | 6293d30 | 2012-04-03 16:07:56 -0700 | [diff] [blame] | 192 | struct cpu_driver *find_cpu_driver(struct device *cpu) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 193 | { |
| 194 | struct cpu_driver *driver; |
Aaron Durbin | 0375815 | 2015-09-03 17:23:08 -0500 | [diff] [blame^] | 195 | for (driver = _cpu_drivers; driver < _ecpu_drivers; driver++) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 196 | struct cpu_device_id *id; |
Stefan Reinauer | 6293d30 | 2012-04-03 16:07:56 -0700 | [diff] [blame] | 197 | for (id = driver->id_table; |
| 198 | id->vendor != X86_VENDOR_INVALID; id++) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 199 | if ((cpu->vendor == id->vendor) && |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 200 | (cpu->device == id->device)) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 201 | { |
Stefan Reinauer | 6293d30 | 2012-04-03 16:07:56 -0700 | [diff] [blame] | 202 | return driver; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 203 | } |
Gerd Hoffmann | cbf3073 | 2013-05-31 09:23:26 +0200 | [diff] [blame] | 204 | if (X86_VENDOR_ANY == id->vendor) |
| 205 | return driver; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
Stefan Reinauer | 6293d30 | 2012-04-03 16:07:56 -0700 | [diff] [blame] | 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | static void set_cpu_ops(struct device *cpu) |
| 212 | { |
| 213 | struct cpu_driver *driver = find_cpu_driver(cpu); |
| 214 | cpu->ops = driver ? driver->ops : NULL; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 217 | void cpu_initialize(unsigned int index) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 218 | { |
| 219 | /* Because we busy wait at the printk spinlock. |
| 220 | * It is important to keep the number of printed messages |
| 221 | * from secondary cpus to a minimum, when debugging is |
| 222 | * disabled. |
| 223 | */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 224 | struct device *cpu; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 225 | struct cpu_info *info; |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 226 | struct cpuinfo_x86 c; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 227 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 228 | info = cpu_info(); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 229 | |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 230 | printk(BIOS_INFO, "Initializing CPU #%d\n", index); |
Kyösti Mälkki | bc8c996 | 2012-07-10 10:19:40 +0300 | [diff] [blame] | 231 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 232 | cpu = info->cpu; |
| 233 | if (!cpu) { |
| 234 | die("CPU: missing cpu device structure"); |
| 235 | } |
Li-Ta Lo | 8cb91dc | 2004-03-26 18:34:48 +0000 | [diff] [blame] | 236 | |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 237 | post_log_path(cpu); |
| 238 | |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 239 | /* Find what type of cpu we are dealing with */ |
| 240 | identify_cpu(cpu); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 241 | printk(BIOS_DEBUG, "CPU: vendor %s device %x\n", |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 242 | cpu_vendor_name(cpu->vendor), cpu->device); |
Li-Ta Lo | fd3f2d7 | 2004-05-12 16:34:46 +0000 | [diff] [blame] | 243 | |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 244 | get_fms(&c, cpu->device); |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 245 | |
Stefan Reinauer | 5f5436f | 2010-04-25 20:42:02 +0000 | [diff] [blame] | 246 | printk(BIOS_DEBUG, "CPU: family %02x, model %02x, stepping %02x\n", |
| 247 | c.x86, c.x86_model, c.x86_mask); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 248 | |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 249 | /* Lookup the cpu's operations */ |
| 250 | set_cpu_ops(cpu); |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 251 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 252 | if(!cpu->ops) { |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 253 | /* mask out the stepping and try again */ |
| 254 | cpu->device -= c.x86_mask; |
Steven J. Magnani | eccc357 | 2005-09-14 13:53:45 +0000 | [diff] [blame] | 255 | set_cpu_ops(cpu); |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 256 | cpu->device += c.x86_mask; |
| 257 | if(!cpu->ops) die("Unknown cpu"); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 258 | printk(BIOS_DEBUG, "Using generic cpu ops (good)\n"); |
Ronald G. Minnich | 43225bc | 2005-11-22 00:07:02 +0000 | [diff] [blame] | 259 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 260 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 261 | |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 262 | /* Initialize the cpu */ |
| 263 | if (cpu->ops && cpu->ops->init) { |
| 264 | cpu->enabled = 1; |
| 265 | cpu->initialized = 1; |
| 266 | cpu->ops->init(cpu); |
| 267 | } |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 268 | post_log_clear(); |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 269 | |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 270 | printk(BIOS_INFO, "CPU #%d initialized\n", index); |
Yinghai Lu | 30b4abe | 2007-04-06 19:57:42 +0000 | [diff] [blame] | 271 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 272 | return; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 273 | } |