xref: /netbsd-src/sys/arch/x86/x86/identcpu.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: identcpu.c,v 1.21 2011/01/19 21:39:41 jmcneill Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden,  and by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.21 2011/01/19 21:39:41 jmcneill Exp $");
34 
35 #include "opt_enhanced_speedstep.h"
36 #include "opt_intel_odcm.h"
37 #include "opt_intel_coretemp.h"
38 #include "opt_via_c7temp.h"
39 #include "opt_powernow_k8.h"
40 #include "opt_xen.h"
41 #ifdef i386	/* XXX */
42 #include "opt_powernow_k7.h"
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 
49 #include <uvm/uvm_extern.h>
50 
51 #include <machine/specialreg.h>
52 #include <machine/pio.h>
53 #include <machine/cpu.h>
54 
55 #include <x86/cputypes.h>
56 #include <x86/cacheinfo.h>
57 #include <x86/cpuvar.h>
58 #include <x86/cpu_msr.h>
59 #include <x86/powernow.h>
60 
61 static const struct x86_cache_info intel_cpuid_cache_info[] = INTEL_CACHE_INFO;
62 
63 static const struct x86_cache_info amd_cpuid_l2cache_assoc_info[] =
64 	AMD_L2CACHE_INFO;
65 
66 static const struct x86_cache_info amd_cpuid_l3cache_assoc_info[] =
67 	AMD_L3CACHE_INFO;
68 
69 int cpu_vendor;
70 char cpu_brand_string[49];
71 
72 /*
73  * Info for CTL_HW
74  */
75 char	cpu_model[120];
76 
77 /*
78  * Note: these are just the ones that may not have a cpuid instruction.
79  * We deal with the rest in a different way.
80  */
81 const int i386_nocpuid_cpus[] = {
82 	CPUVENDOR_INTEL, CPUCLASS_386,	/* CPU_386SX */
83 	CPUVENDOR_INTEL, CPUCLASS_386,	/* CPU_386   */
84 	CPUVENDOR_INTEL, CPUCLASS_486,	/* CPU_486SX */
85 	CPUVENDOR_INTEL, CPUCLASS_486, 	/* CPU_486   */
86 	CPUVENDOR_CYRIX, CPUCLASS_486,	/* CPU_486DLC */
87 	CPUVENDOR_CYRIX, CPUCLASS_486,	/* CPU_6x86 */
88 	CPUVENDOR_NEXGEN, CPUCLASS_386,	/* CPU_NX586 */
89 };
90 
91 static const char cpu_vendor_names[][10] = {
92 	"Unknown", "Intel", "NS/Cyrix", "NexGen", "AMD", "IDT/VIA", "Transmeta"
93 };
94 
95 static const struct x86_cache_info *
96 cache_info_lookup(const struct x86_cache_info *cai, uint8_t desc)
97 {
98 	int i;
99 
100 	for (i = 0; cai[i].cai_desc != 0; i++) {
101 		if (cai[i].cai_desc == desc)
102 			return (&cai[i]);
103 	}
104 
105 	return (NULL);
106 }
107 
108 
109 static void
110 cpu_probe_amd_cache(struct cpu_info *ci)
111 {
112 	const struct x86_cache_info *cp;
113 	struct x86_cache_info *cai;
114 	int family, model;
115 	u_int descs[4];
116 	u_int lfunc;
117 
118 	family = CPUID2FAMILY(ci->ci_signature);
119 	model = CPUID2MODEL(ci->ci_signature);
120 
121 	/*
122 	 * K5 model 0 has none of this info.
123 	 */
124 	if (family == 5 && model == 0)
125 		return;
126 
127 	/*
128 	 * Get extended values for K8 and up.
129 	 */
130 	if (family == 0xf) {
131 		family += CPUID2EXTFAMILY(ci->ci_signature);
132 		model += CPUID2EXTMODEL(ci->ci_signature);
133 	}
134 
135 	/*
136 	 * Determine the largest extended function value.
137 	 */
138 	x86_cpuid(0x80000000, descs);
139 	lfunc = descs[0];
140 
141 	/*
142 	 * Determine L1 cache/TLB info.
143 	 */
144 	if (lfunc < 0x80000005) {
145 		/* No L1 cache info available. */
146 		return;
147 	}
148 
149 	x86_cpuid(0x80000005, descs);
150 
151 	/*
152 	 * K6-III and higher have large page TLBs.
153 	 */
154 	if ((family == 5 && model >= 9) || family >= 6) {
155 		cai = &ci->ci_cinfo[CAI_ITLB2];
156 		cai->cai_totalsize = AMD_L1_EAX_ITLB_ENTRIES(descs[0]);
157 		cai->cai_associativity = AMD_L1_EAX_ITLB_ASSOC(descs[0]);
158 		cai->cai_linesize = (4 * 1024 * 1024);
159 
160 		cai = &ci->ci_cinfo[CAI_DTLB2];
161 		cai->cai_totalsize = AMD_L1_EAX_DTLB_ENTRIES(descs[0]);
162 		cai->cai_associativity = AMD_L1_EAX_DTLB_ASSOC(descs[0]);
163 		cai->cai_linesize = (4 * 1024 * 1024);
164 	}
165 
166 	cai = &ci->ci_cinfo[CAI_ITLB];
167 	cai->cai_totalsize = AMD_L1_EBX_ITLB_ENTRIES(descs[1]);
168 	cai->cai_associativity = AMD_L1_EBX_ITLB_ASSOC(descs[1]);
169 	cai->cai_linesize = (4 * 1024);
170 
171 	cai = &ci->ci_cinfo[CAI_DTLB];
172 	cai->cai_totalsize = AMD_L1_EBX_DTLB_ENTRIES(descs[1]);
173 	cai->cai_associativity = AMD_L1_EBX_DTLB_ASSOC(descs[1]);
174 	cai->cai_linesize = (4 * 1024);
175 
176 	cai = &ci->ci_cinfo[CAI_DCACHE];
177 	cai->cai_totalsize = AMD_L1_ECX_DC_SIZE(descs[2]);
178 	cai->cai_associativity = AMD_L1_ECX_DC_ASSOC(descs[2]);
179 	cai->cai_linesize = AMD_L1_EDX_IC_LS(descs[2]);
180 
181 	cai = &ci->ci_cinfo[CAI_ICACHE];
182 	cai->cai_totalsize = AMD_L1_EDX_IC_SIZE(descs[3]);
183 	cai->cai_associativity = AMD_L1_EDX_IC_ASSOC(descs[3]);
184 	cai->cai_linesize = AMD_L1_EDX_IC_LS(descs[3]);
185 
186 	/*
187 	 * Determine L2 cache/TLB info.
188 	 */
189 	if (lfunc < 0x80000006) {
190 		/* No L2 cache info available. */
191 		return;
192 	}
193 
194 	x86_cpuid(0x80000006, descs);
195 
196 	cai = &ci->ci_cinfo[CAI_L2CACHE];
197 	cai->cai_totalsize = AMD_L2_ECX_C_SIZE(descs[2]);
198 	cai->cai_associativity = AMD_L2_ECX_C_ASSOC(descs[2]);
199 	cai->cai_linesize = AMD_L2_ECX_C_LS(descs[2]);
200 
201 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
202 	    cai->cai_associativity);
203 	if (cp != NULL)
204 		cai->cai_associativity = cp->cai_associativity;
205 	else
206 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
207 
208 	if (family < 0xf) {
209 		/* No L3 cache info available. */
210 		return;
211 	}
212 
213 	cai = &ci->ci_cinfo[CAI_L3CACHE];
214 	cai->cai_totalsize = AMD_L3_EDX_C_SIZE(descs[3]);
215 	cai->cai_associativity = AMD_L3_EDX_C_ASSOC(descs[3]);
216 	cai->cai_linesize = AMD_L3_EDX_C_LS(descs[3]);
217 
218 	cp = cache_info_lookup(amd_cpuid_l3cache_assoc_info,
219 	    cai->cai_associativity);
220 	if (cp != NULL)
221 		cai->cai_associativity = cp->cai_associativity;
222 	else
223 		cai->cai_associativity = 0;	/* XXX Unknown reserved */
224 
225 	if (lfunc < 0x80000019) {
226 		/* No 1GB Page TLB */
227 		return;
228 	}
229 
230 	x86_cpuid(0x80000019, descs);
231 
232 	cai = &ci->ci_cinfo[CAI_L1_1GBDTLB];
233 	cai->cai_totalsize = AMD_L1_1GB_EAX_DTLB_ENTRIES(descs[1]);
234 	cai->cai_associativity = AMD_L1_1GB_EAX_DTLB_ASSOC(descs[1]);
235 	cai->cai_linesize = (1 * 1024);
236 
237 	cai = &ci->ci_cinfo[CAI_L1_1GBITLB];
238 	cai->cai_totalsize = AMD_L1_1GB_EAX_IUTLB_ENTRIES(descs[0]);
239 	cai->cai_associativity = AMD_L1_1GB_EAX_IUTLB_ASSOC(descs[0]);
240 	cai->cai_linesize = (1 * 1024);
241 
242 	cai = &ci->ci_cinfo[CAI_L2_1GBDTLB];
243 	cai->cai_totalsize = AMD_L2_1GB_EBX_DUTLB_ENTRIES(descs[1]);
244 	cai->cai_associativity = AMD_L2_1GB_EBX_DUTLB_ASSOC(descs[1]);
245 	cai->cai_linesize = (1 * 1024);
246 
247 	cai = &ci->ci_cinfo[CAI_L2_1GBITLB];
248 	cai->cai_totalsize = AMD_L2_1GB_EBX_IUTLB_ENTRIES(descs[0]);
249 	cai->cai_associativity = AMD_L2_1GB_EBX_IUTLB_ASSOC(descs[0]);
250 	cai->cai_linesize = (1 * 1024);
251 }
252 
253 static void
254 cpu_probe_k5(struct cpu_info *ci)
255 {
256 	int flag;
257 
258 	if (cpu_vendor != CPUVENDOR_AMD ||
259 	    CPUID2FAMILY(ci->ci_signature) != 5)
260 		return;
261 
262 	if (CPUID2MODEL(ci->ci_signature) == 0) {
263 		/*
264 		 * According to the AMD Processor Recognition App Note,
265 		 * the AMD-K5 Model 0 uses the wrong bit to indicate
266 		 * support for global PTEs, instead using bit 9 (APIC)
267 		 * rather than bit 13 (i.e. "0x200" vs. 0x2000".  Oops!).
268 		 */
269 		flag = ci->ci_feat_val[0];
270 		if ((flag & CPUID_APIC) != 0)
271 			flag = (flag & ~CPUID_APIC) | CPUID_PGE;
272 		ci->ci_feat_val[0] = flag;
273 	}
274 
275 	cpu_probe_amd_cache(ci);
276 }
277 
278 static void
279 cpu_probe_k678(struct cpu_info *ci)
280 {
281 	uint32_t descs[4];
282 
283 	if (cpu_vendor != CPUVENDOR_AMD ||
284 	    CPUID2FAMILY(ci->ci_signature) < 6)
285 		return;
286 
287 	/* Determine the extended feature flags. */
288 	x86_cpuid(0x80000000, descs);
289 	if (descs[0] >= 0x80000001) {
290 		x86_cpuid(0x80000001, descs);
291 		ci->ci_feat_val[3] = descs[2]; /* %ecx */
292 		ci->ci_feat_val[2] = descs[3]; /* %edx */
293 	}
294 
295 	cpu_probe_amd_cache(ci);
296 }
297 
298 static inline uint8_t
299 cyrix_read_reg(uint8_t reg)
300 {
301 
302 	outb(0x22, reg);
303 	return inb(0x23);
304 }
305 
306 static inline void
307 cyrix_write_reg(uint8_t reg, uint8_t data)
308 {
309 
310 	outb(0x22, reg);
311 	outb(0x23, data);
312 }
313 
314 static void
315 cpu_probe_cyrix_cmn(struct cpu_info *ci)
316 {
317 	/*
318 	 * i8254 latch check routine:
319 	 *     National Geode (formerly Cyrix MediaGX) has a serious bug in
320 	 *     its built-in i8254-compatible clock module (cs5510 cs5520).
321 	 *     Set the variable 'clock_broken_latch' to indicate it.
322 	 *
323 	 * This bug is not present in the cs5530, and the flag
324 	 * is disabled again in sys/arch/i386/pci/pcib.c if this later
325 	 * model device is detected. Ideally, this work-around should not
326 	 * even be in here, it should be in there. XXX
327 	 */
328 	uint8_t c3;
329 #ifndef XEN
330 	extern int clock_broken_latch;
331 
332 	switch (ci->ci_signature) {
333 	case 0x440:     /* Cyrix MediaGX */
334 	case 0x540:     /* GXm */
335 		clock_broken_latch = 1;
336 		break;
337 	}
338 #endif
339 
340 	/* set up various cyrix registers */
341 	/*
342 	 * Enable suspend on halt (powersave mode).
343 	 * When powersave mode is enabled, the TSC stops counting
344 	 * while the CPU is halted in idle() waiting for an interrupt.
345 	 * This means we can't use the TSC for interval time in
346 	 * microtime(9), and thus it is disabled here.
347 	 *
348 	 * It still makes a perfectly good cycle counter
349 	 * for program profiling, so long as you remember you're
350 	 * counting cycles, and not time. Further, if you don't
351 	 * mind not using powersave mode, the TSC works just fine,
352 	 * so this should really be optional. XXX
353 	 */
354 	cyrix_write_reg(0xc2, cyrix_read_reg(0xc2) | 0x08);
355 
356 	/*
357 	 * Do not disable the TSC on the Geode GX, it's reported to
358 	 * work fine.
359 	 */
360 	if (ci->ci_signature != 0x552)
361 		ci->ci_feat_val[0] &= ~CPUID_TSC;
362 
363 	/* enable access to ccr4/ccr5 */
364 	c3 = cyrix_read_reg(0xC3);
365 	cyrix_write_reg(0xC3, c3 | 0x10);
366 	/* cyrix's workaround  for the "coma bug" */
367 	cyrix_write_reg(0x31, cyrix_read_reg(0x31) | 0xf8);
368 	cyrix_write_reg(0x32, cyrix_read_reg(0x32) | 0x7f);
369 	cyrix_write_reg(0x33, cyrix_read_reg(0x33) & ~0xff);
370 	cyrix_write_reg(0x3c, cyrix_read_reg(0x3c) | 0x87);
371 	/* disable access to ccr4/ccr5 */
372 	cyrix_write_reg(0xC3, c3);
373 }
374 
375 static void
376 cpu_probe_cyrix(struct cpu_info *ci)
377 {
378 
379 	if (cpu_vendor != CPUVENDOR_CYRIX ||
380 	    CPUID2FAMILY(ci->ci_signature) < 4 ||
381 	    CPUID2FAMILY(ci->ci_signature) > 6)
382 		return;
383 
384 	cpu_probe_cyrix_cmn(ci);
385 }
386 
387 static void
388 cpu_probe_winchip(struct cpu_info *ci)
389 {
390 
391 	if (cpu_vendor != CPUVENDOR_IDT ||
392 	    CPUID2FAMILY(ci->ci_signature) != 5)
393 	    	return;
394 
395 	if (CPUID2MODEL(ci->ci_signature) == 4) {
396 		/* WinChip C6 */
397 		ci->ci_feat_val[0] &= ~CPUID_TSC;
398 	}
399 }
400 
401 static void
402 cpu_probe_c3(struct cpu_info *ci)
403 {
404 	u_int family, model, stepping, descs[4], lfunc, msr;
405 	struct x86_cache_info *cai;
406 
407 	if (cpu_vendor != CPUVENDOR_IDT ||
408 	    CPUID2FAMILY(ci->ci_signature) < 6)
409 	    	return;
410 
411 	family = CPUID2FAMILY(ci->ci_signature);
412 	model = CPUID2MODEL(ci->ci_signature);
413 	stepping = CPUID2STEPPING(ci->ci_signature);
414 
415 	/* Determine the largest extended function value. */
416 	x86_cpuid(0x80000000, descs);
417 	lfunc = descs[0];
418 
419 	/* Determine the extended feature flags. */
420 	if (lfunc >= 0x80000001) {
421 		x86_cpuid(0x80000001, descs);
422 		ci->ci_feat_val[2] = descs[3];
423 	}
424 
425 	if (family > 6 || model > 0x9 || (model == 0x9 && stepping >= 3)) {
426 		/* Nehemiah or Esther */
427 		x86_cpuid(0xc0000000, descs);
428 		lfunc = descs[0];
429 		if (lfunc >= 0xc0000001) {	/* has ACE, RNG */
430 		    int rng_enable = 0, ace_enable = 0;
431 		    x86_cpuid(0xc0000001, descs);
432 		    lfunc = descs[3];
433 		    ci->ci_feat_val[4] = lfunc;
434 		    /* Check for and enable RNG */
435 		    if (lfunc & CPUID_VIA_HAS_RNG) {
436 		    	if (!(lfunc & CPUID_VIA_DO_RNG)) {
437 			    rng_enable++;
438 			    ci->ci_feat_val[4] |= CPUID_VIA_HAS_RNG;
439 			}
440 		    }
441 		    /* Check for and enable ACE (AES-CBC) */
442 		    if (lfunc & CPUID_VIA_HAS_ACE) {
443 			if (!(lfunc & CPUID_VIA_DO_ACE)) {
444 			    ace_enable++;
445 			    ci->ci_feat_val[4] |= CPUID_VIA_DO_ACE;
446 			}
447 		    }
448 		    /* Check for and enable SHA */
449 		    if (lfunc & CPUID_VIA_HAS_PHE) {
450 			if (!(lfunc & CPUID_VIA_DO_PHE)) {
451 			    ace_enable++;
452 			    ci->ci_feat_val[4] |= CPUID_VIA_DO_PHE;
453 			}
454 		    }
455 		    /* Check for and enable ACE2 (AES-CTR) */
456 		    if (lfunc & CPUID_VIA_HAS_ACE2) {
457 			if (!(lfunc & CPUID_VIA_DO_ACE2)) {
458 			    ace_enable++;
459 			    ci->ci_feat_val[4] |= CPUID_VIA_DO_ACE2;
460 			}
461 		    }
462 		    /* Check for and enable PMM (modmult engine) */
463 		    if (lfunc & CPUID_VIA_HAS_PMM) {
464 			if (!(lfunc & CPUID_VIA_DO_PMM)) {
465 			    ace_enable++;
466 			    ci->ci_feat_val[4] |= CPUID_VIA_DO_PMM;
467 			}
468 		    }
469 
470 		    /* Actually do the enables. */
471 		    if (rng_enable) {
472 			msr = rdmsr(MSR_VIA_RNG);
473 			wrmsr(MSR_VIA_RNG, msr | MSR_VIA_RNG_ENABLE);
474 		    }
475 		    if (ace_enable) {
476 			msr = rdmsr(MSR_VIA_ACE);
477 			wrmsr(MSR_VIA_ACE, msr | MSR_VIA_ACE_ENABLE);
478 		    }
479 
480 		}
481 	}
482 
483 	/*
484 	 * Determine L1 cache/TLB info.
485 	 */
486 	if (lfunc < 0x80000005) {
487 		/* No L1 cache info available. */
488 		return;
489 	}
490 
491 	x86_cpuid(0x80000005, descs);
492 
493 	cai = &ci->ci_cinfo[CAI_ITLB];
494 	cai->cai_totalsize = VIA_L1_EBX_ITLB_ENTRIES(descs[1]);
495 	cai->cai_associativity = VIA_L1_EBX_ITLB_ASSOC(descs[1]);
496 	cai->cai_linesize = (4 * 1024);
497 
498 	cai = &ci->ci_cinfo[CAI_DTLB];
499 	cai->cai_totalsize = VIA_L1_EBX_DTLB_ENTRIES(descs[1]);
500 	cai->cai_associativity = VIA_L1_EBX_DTLB_ASSOC(descs[1]);
501 	cai->cai_linesize = (4 * 1024);
502 
503 	cai = &ci->ci_cinfo[CAI_DCACHE];
504 	cai->cai_totalsize = VIA_L1_ECX_DC_SIZE(descs[2]);
505 	cai->cai_associativity = VIA_L1_ECX_DC_ASSOC(descs[2]);
506 	cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[2]);
507 	if (family == 6 && model == 9 && stepping == 8) {
508 		/* Erratum: stepping 8 reports 4 when it should be 2 */
509 		cai->cai_associativity = 2;
510 	}
511 
512 	cai = &ci->ci_cinfo[CAI_ICACHE];
513 	cai->cai_totalsize = VIA_L1_EDX_IC_SIZE(descs[3]);
514 	cai->cai_associativity = VIA_L1_EDX_IC_ASSOC(descs[3]);
515 	cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[3]);
516 	if (family == 6 && model == 9 && stepping == 8) {
517 		/* Erratum: stepping 8 reports 4 when it should be 2 */
518 		cai->cai_associativity = 2;
519 	}
520 
521 	/*
522 	 * Determine L2 cache/TLB info.
523 	 */
524 	if (lfunc < 0x80000006) {
525 		/* No L2 cache info available. */
526 		return;
527 	}
528 
529 	x86_cpuid(0x80000006, descs);
530 
531 	cai = &ci->ci_cinfo[CAI_L2CACHE];
532 	if (family > 6 || model >= 9) {
533 		cai->cai_totalsize = VIA_L2N_ECX_C_SIZE(descs[2]);
534 		cai->cai_associativity = VIA_L2N_ECX_C_ASSOC(descs[2]);
535 		cai->cai_linesize = VIA_L2N_ECX_C_LS(descs[2]);
536 	} else {
537 		cai->cai_totalsize = VIA_L2_ECX_C_SIZE(descs[2]);
538 		cai->cai_associativity = VIA_L2_ECX_C_ASSOC(descs[2]);
539 		cai->cai_linesize = VIA_L2_ECX_C_LS(descs[2]);
540 	}
541 }
542 
543 static void
544 cpu_probe_geode(struct cpu_info *ci)
545 {
546 
547 	if (memcmp("Geode by NSC", ci->ci_vendor, 12) != 0 ||
548 	    CPUID2FAMILY(ci->ci_signature) != 5)
549 	    	return;
550 
551 	cpu_probe_cyrix_cmn(ci);
552 	cpu_probe_amd_cache(ci);
553 }
554 
555 void
556 cpu_probe(struct cpu_info *ci)
557 {
558 	const struct x86_cache_info *cai;
559 	u_int descs[4];
560 	int iterations, i, j;
561 	uint8_t desc;
562 	uint32_t miscbytes;
563 	uint32_t brand[12];
564 
565 	cpu_vendor = i386_nocpuid_cpus[cpu << 1];
566 	cpu_class = i386_nocpuid_cpus[(cpu << 1) + 1];
567 
568 	if (cpuid_level < 0)
569 		return;
570 
571 	for (i = 0; i < __arraycount(ci->ci_feat_val); i++) {
572 		ci->ci_feat_val[i] = 0;
573 	}
574 
575 	x86_cpuid(0, descs);
576 	cpuid_level = descs[0];
577 	ci->ci_vendor[0] = descs[1];
578 	ci->ci_vendor[2] = descs[2];
579 	ci->ci_vendor[1] = descs[3];
580 	ci->ci_vendor[3] = 0;
581 
582 	if (memcmp(ci->ci_vendor, "GenuineIntel", 12) == 0)
583 		cpu_vendor = CPUVENDOR_INTEL;
584 	else if (memcmp(ci->ci_vendor,  "AuthenticAMD", 12) == 0)
585 		cpu_vendor = CPUVENDOR_AMD;
586 	else if (memcmp(ci->ci_vendor,  "CyrixInstead", 12) == 0)
587 		cpu_vendor = CPUVENDOR_CYRIX;
588 	else if (memcmp(ci->ci_vendor,  "Geode by NSC", 12) == 0)
589 		cpu_vendor = CPUVENDOR_CYRIX;
590 	else if (memcmp(ci->ci_vendor, "CentaurHauls", 12) == 0)
591 		cpu_vendor = CPUVENDOR_IDT;
592 	else if (memcmp(ci->ci_vendor, "GenuineTMx86", 12) == 0)
593 		cpu_vendor = CPUVENDOR_TRANSMETA;
594 	else
595 		cpu_vendor = CPUVENDOR_UNKNOWN;
596 
597 	x86_cpuid(0x80000000, brand);
598 	if (brand[0] >= 0x80000004) {
599 		x86_cpuid(0x80000002, brand);
600 		x86_cpuid(0x80000003, brand + 4);
601 		x86_cpuid(0x80000004, brand + 8);
602 		for (i = 0; i < 48; i++) {
603 			if (((char *) brand)[i] != ' ')
604 				break;
605 		}
606 		memcpy(cpu_brand_string, ((char *) brand) + i, 48 - i);
607 	}
608 
609 	if (cpuid_level >= 1) {
610 		x86_cpuid(1, descs);
611 		ci->ci_signature = descs[0];
612 		miscbytes = descs[1];
613 		ci->ci_feat_val[1] = descs[2];
614 		ci->ci_feat_val[0] = descs[3];
615 
616 		/* Determine family + class. */
617 		cpu_class = CPUID2FAMILY(ci->ci_signature) + (CPUCLASS_386 - 3);
618 		if (cpu_class > CPUCLASS_686)
619 			cpu_class = CPUCLASS_686;
620 
621 		/* CLFLUSH line size is next 8 bits */
622 		if (ci->ci_feat_val[0] & CPUID_CFLUSH)
623 			ci->ci_cflush_lsize = ((miscbytes >> 8) & 0xff) << 3;
624 		ci->ci_initapicid = (miscbytes >> 24) & 0xff;
625 	}
626 
627 	if (cpuid_level >= 2) {
628 		/* Parse the cache info from `cpuid', if we have it. */
629 		x86_cpuid(2, descs);
630 		iterations = descs[0] & 0xff;
631 		while (iterations-- > 0) {
632 			for (i = 0; i < 4; i++) {
633 				if (descs[i] & 0x80000000)
634 					continue;
635 				for (j = 0; j < 4; j++) {
636 					if (i == 0 && j == 0)
637 						continue;
638 					desc = (descs[i] >> (j * 8)) & 0xff;
639 					if (desc == 0)
640 						continue;
641 					cai = cache_info_lookup(
642 					    intel_cpuid_cache_info, desc);
643 					if (cai != NULL) {
644 						ci->ci_cinfo[cai->cai_index] =
645 						    *cai;
646 					}
647 				}
648 			}
649 		}
650 	}
651 
652 	cpu_probe_k5(ci);
653 	cpu_probe_k678(ci);
654 	cpu_probe_cyrix(ci);
655 	cpu_probe_winchip(ci);
656 	cpu_probe_c3(ci);
657 	cpu_probe_geode(ci);
658 
659 	x86_cpu_topology(ci);
660 
661 	if (cpu_vendor != CPUVENDOR_AMD && (ci->ci_feat_val[0] & CPUID_TM) &&
662 	    (rdmsr(MSR_MISC_ENABLE) & (1 << 3)) == 0) {
663 		/* Enable thermal monitor 1. */
664 		wrmsr(MSR_MISC_ENABLE, rdmsr(MSR_MISC_ENABLE) | (1<<3));
665 	}
666 
667 	if (ci == &cpu_info_primary) {
668 		/* If first. Boot Processor is the cpu_feature reference. */
669 		for (i = 0; i < __arraycount(cpu_feature); i++) {
670 			cpu_feature[i] = ci->ci_feat_val[i];
671 		}
672 #ifndef XEN
673 		/* Early patch of text segment. */
674 		x86_patch(true);
675 #endif
676 	} else {
677 		/*
678 		 * If not first. Warn about cpu_feature mismatch for
679 		 * secondary CPUs.
680 		 */
681 		for (i = 0; i < __arraycount(cpu_feature); i++) {
682 			if (cpu_feature[i] != ci->ci_feat_val[i])
683 				aprint_error_dev(ci->ci_dev,
684 				    "feature mismatch: cpu_feature[%d] is "
685 				    "%#x, but CPU reported %#x\n",
686 				    i, cpu_feature[i], ci->ci_feat_val[i]);
687 		}
688 	}
689 }
690 
691 void
692 cpu_identify(struct cpu_info *ci)
693 {
694 
695 	snprintf(cpu_model, sizeof(cpu_model), "%s %d86-class",
696 	    cpu_vendor_names[cpu_vendor], cpu_class + 3);
697 	if (cpu_brand_string[0] != '\0') {
698 		aprint_normal(": %s", cpu_brand_string);
699 	} else {
700 		aprint_normal(": %s", cpu_model);
701 		if (ci->ci_data.cpu_cc_freq != 0)
702 			aprint_normal(", %dMHz",
703 			    (int)(ci->ci_data.cpu_cc_freq / 1000000));
704 	}
705 	if (ci->ci_signature != 0)
706 		aprint_normal(", id 0x%x", ci->ci_signature);
707 	aprint_normal("\n");
708 
709 	if (cpu_brand_string[0] == '\0') {
710 		strlcpy(cpu_brand_string, cpu_model, sizeof(cpu_brand_string));
711 	}
712 	if (cpu_class == CPUCLASS_386) {
713 		panic("NetBSD requires an 80486DX or later processor");
714 	}
715 	if (cpu == CPU_486DLC) {
716 		aprint_error("WARNING: BUGGY CYRIX CACHE\n");
717 	}
718 
719 	if ((cpu_vendor == CPUVENDOR_AMD) /* check enablement of an */
720 	  && (device_unit(ci->ci_dev) == 0) /* AMD feature only once */
721 	  && ((cpu_feature[3] & CPUID_SVM) == CPUID_SVM)
722 #if defined(XEN) && !defined(DOM0OPS)
723 	  && (false)  /* on Xen rdmsr is for Dom0 only */
724 #endif
725 	  )
726 	{
727 		uint64_t val;
728 
729 		val = rdmsr(MSR_VMCR);
730 		if (((val & VMCR_SVMED) == VMCR_SVMED)
731 		  && ((val & VMCR_LOCK) == VMCR_LOCK))
732 		{
733 			aprint_normal_dev(ci->ci_dev,
734 				"SVM disabled by the BIOS\n");
735 		}
736 	}
737 
738 #ifdef i386 /* XXX for now */
739 	if (cpu_vendor == CPUVENDOR_TRANSMETA) {
740 		u_int descs[4];
741 		x86_cpuid(0x80860000, descs);
742 		if (descs[0] >= 0x80860007)
743 			tmx86_init_longrun();
744 	}
745 
746 	/* If we have FXSAVE/FXRESTOR, use them. */
747 	if (cpu_feature[0] & CPUID_FXSR) {
748 		i386_use_fxsave = 1;
749 		/*
750 		 * If we have SSE/SSE2, enable XMM exceptions, and
751 		 * notify userland.
752 		 */
753 		if (cpu_feature[0] & CPUID_SSE)
754 			i386_has_sse = 1;
755 		if (cpu_feature[0] & CPUID_SSE2)
756 			i386_has_sse2 = 1;
757 	} else
758 		i386_use_fxsave = 0;
759 #endif	/* i386 */
760 
761 #ifdef ENHANCED_SPEEDSTEP
762 	if (cpu_feature[1] & CPUID2_EST) {
763 		if (rdmsr(MSR_MISC_ENABLE) & (1 << 16))
764 			est_init(cpu_vendor);
765 	}
766 #endif /* ENHANCED_SPEEDSTEP */
767 
768 #ifdef INTEL_CORETEMP
769 	if (cpu_vendor == CPUVENDOR_INTEL && cpuid_level >= 0x06)
770 		coretemp_register(ci);
771 #endif
772 
773 #ifdef VIA_C7TEMP
774 	if (cpu_vendor == CPUVENDOR_IDT &&
775 	    CPUID2FAMILY(ci->ci_signature) == 6 &&
776 	    CPUID2MODEL(ci->ci_signature) >= 0x9) {
777 		uint32_t descs[4];
778 
779 		x86_cpuid(0xc0000000, descs);
780 		if (descs[0] >= 0xc0000002)	/* has temp sensor */
781 			viac7temp_register(ci);
782 	}
783 #endif
784 
785 #if defined(POWERNOW_K7) || defined(POWERNOW_K8)
786 	if (cpu_vendor == CPUVENDOR_AMD && powernow_probe(ci)) {
787 		switch (CPUID2FAMILY(ci->ci_signature)) {
788 #ifdef POWERNOW_K7
789 		case 6:
790 			k7_powernow_init();
791 			break;
792 #endif
793 #ifdef POWERNOW_K8
794 		case 15:
795 			k8_powernow_init();
796 			break;
797 #endif
798 		default:
799 			break;
800 		}
801 	}
802 #endif /* POWERNOW_K7 || POWERNOW_K8 */
803 
804 #ifdef INTEL_ONDEMAND_CLOCKMOD
805 	if (cpuid_level >= 1) {
806 		clockmod_init();
807 	}
808 #endif
809 }
810