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