xref: /netbsd-src/sys/arch/x86/x86/identcpu.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: identcpu.c,v 1.18 2010/01/18 16:40:18 rmind 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.18 2010/01/18 16:40:18 rmind 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_feature_flags;
270 		if ((flag & CPUID_APIC) != 0)
271 			flag = (flag & ~CPUID_APIC) | CPUID_PGE;
272 		ci->ci_feature_flags = 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_feature3_flags |= descs[3]; /* %edx */
292 		ci->ci_feature4_flags = descs[2];  /* %ecx */
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_feature_flags &= ~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_feature_flags &= ~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_feature_flags |= 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_padlock_flags = 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_padlock_flags |= 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_padlock_flags |= 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_padlock_flags |= 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_padlock_flags |= 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_padlock_flags |= 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 	x86_cpuid(0, descs);
572 	cpuid_level = descs[0];
573 	ci->ci_vendor[0] = descs[1];
574 	ci->ci_vendor[2] = descs[2];
575 	ci->ci_vendor[1] = descs[3];
576 	ci->ci_vendor[3] = 0;
577 
578 	if (memcmp(ci->ci_vendor, "GenuineIntel", 12) == 0)
579 		cpu_vendor = CPUVENDOR_INTEL;
580 	else if (memcmp(ci->ci_vendor,  "AuthenticAMD", 12) == 0)
581 		cpu_vendor = CPUVENDOR_AMD;
582 	else if (memcmp(ci->ci_vendor,  "CyrixInstead", 12) == 0)
583 		cpu_vendor = CPUVENDOR_CYRIX;
584 	else if (memcmp(ci->ci_vendor,  "Geode by NSC", 12) == 0)
585 		cpu_vendor = CPUVENDOR_CYRIX;
586 	else if (memcmp(ci->ci_vendor, "CentaurHauls", 12) == 0)
587 		cpu_vendor = CPUVENDOR_IDT;
588 	else if (memcmp(ci->ci_vendor, "GenuineTMx86", 12) == 0)
589 		cpu_vendor = CPUVENDOR_TRANSMETA;
590 	else
591 		cpu_vendor = CPUVENDOR_UNKNOWN;
592 
593 	x86_cpuid(0x80000000, brand);
594 	if (brand[0] >= 0x80000004) {
595 		x86_cpuid(0x80000002, brand);
596 		x86_cpuid(0x80000003, brand + 4);
597 		x86_cpuid(0x80000004, brand + 8);
598 		for (i = 0; i < 48; i++) {
599 			if (((char *) brand)[i] != ' ')
600 				break;
601 		}
602 		memcpy(cpu_brand_string, ((char *) brand) + i, 48 - i);
603 	}
604 
605 	if (cpuid_level >= 1) {
606 		x86_cpuid(1, descs);
607 		ci->ci_signature = descs[0];
608 		miscbytes = descs[1];
609 		ci->ci_feature2_flags = descs[2];
610 		ci->ci_feature_flags = descs[3];
611 
612 		/* Determine family + class. */
613 		cpu_class = CPUID2FAMILY(ci->ci_signature) + (CPUCLASS_386 - 3);
614 		if (cpu_class > CPUCLASS_686)
615 			cpu_class = CPUCLASS_686;
616 
617 		/* CLFLUSH line size is next 8 bits */
618 		if (ci->ci_feature_flags & CPUID_CFLUSH)
619 			ci->ci_cflush_lsize = ((miscbytes >> 8) & 0xff) << 3;
620 		ci->ci_initapicid = (miscbytes >> 24) & 0xff;
621 	}
622 
623 	if (cpuid_level >= 2) {
624 		/* Parse the cache info from `cpuid', if we have it. */
625 		x86_cpuid(2, descs);
626 		iterations = descs[0] & 0xff;
627 		while (iterations-- > 0) {
628 			for (i = 0; i < 4; i++) {
629 				if (descs[i] & 0x80000000)
630 					continue;
631 				for (j = 0; j < 4; j++) {
632 					if (i == 0 && j == 0)
633 						continue;
634 					desc = (descs[i] >> (j * 8)) & 0xff;
635 					if (desc == 0)
636 						continue;
637 					cai = cache_info_lookup(
638 					    intel_cpuid_cache_info, desc);
639 					if (cai != NULL) {
640 						ci->ci_cinfo[cai->cai_index] =
641 						    *cai;
642 					}
643 				}
644 			}
645 		}
646 	}
647 
648 	cpu_probe_k5(ci);
649 	cpu_probe_k678(ci);
650 	cpu_probe_cyrix(ci);
651 	cpu_probe_winchip(ci);
652 	cpu_probe_c3(ci);
653 	cpu_probe_geode(ci);
654 
655 	x86_cpu_topology(ci);
656 
657 	if (cpu_vendor != CPUVENDOR_AMD && (ci->ci_feature_flags & CPUID_TM) &&
658 	    (rdmsr(MSR_MISC_ENABLE) & (1 << 3)) == 0) {
659 		/* Enable thermal monitor 1. */
660 		wrmsr(MSR_MISC_ENABLE, rdmsr(MSR_MISC_ENABLE) | (1<<3));
661 	}
662 
663 	if ((cpu_feature | cpu_feature2) == 0) {
664 		/* If first. */
665 		cpu_feature = ci->ci_feature_flags;
666 		cpu_feature2 = ci->ci_feature2_flags;
667 		/* Early patch of text segment. */
668 #ifndef XEN
669 		x86_patch(true);
670 #endif
671 	} else {
672 		/* If not first. */
673 		cpu_feature &= ci->ci_feature_flags;
674 		cpu_feature2 &= ci->ci_feature2_flags;
675 	}
676 }
677 
678 void
679 cpu_identify(struct cpu_info *ci)
680 {
681 
682 	snprintf(cpu_model, sizeof(cpu_model), "%s %d86-class",
683 	    cpu_vendor_names[cpu_vendor], cpu_class + 3);
684 	aprint_normal(": %s", cpu_model);
685 	if (ci->ci_data.cpu_cc_freq != 0)
686 		aprint_normal(", %dMHz", (int)(ci->ci_data.cpu_cc_freq / 1000000));
687 	if (ci->ci_signature != 0)
688 		aprint_normal(", id 0x%x", ci->ci_signature);
689 	aprint_normal("\n");
690 
691 	if (cpu_brand_string[0] == '\0') {
692 		strlcpy(cpu_brand_string, cpu_model, sizeof(cpu_brand_string));
693 	}
694 	if (cpu_class == CPUCLASS_386) {
695 		panic("NetBSD requires an 80486DX or later processor");
696 	}
697 	if (cpu == CPU_486DLC) {
698 		aprint_error("WARNING: BUGGY CYRIX CACHE\n");
699 	}
700 
701 	if ((cpu_vendor == CPUVENDOR_AMD) /* check enablement of an */
702 	  && (device_unit(ci->ci_dev) == 0) /* AMD feature only once */
703 	  && ((ci->ci_feature4_flags & CPUID_SVM) == CPUID_SVM)
704 #if defined(XEN) && !defined(DOM0OPS)
705 	  && (false)  /* on Xen rdmsr is for Dom0 only */
706 #endif
707 	  )
708 	{
709 		uint64_t val;
710 
711 		val = rdmsr(MSR_VMCR);
712 		if (((val & VMCR_SVMED) == VMCR_SVMED)
713 		  && ((val & VMCR_LOCK) == VMCR_LOCK))
714 		{
715 			aprint_normal_dev(ci->ci_dev,
716 				"SVM disabled by the BIOS\n");
717 		}
718 	}
719 
720 #ifdef i386 /* XXX for now */
721 	if (cpu_vendor == CPUVENDOR_TRANSMETA) {
722 		u_int descs[4];
723 		x86_cpuid(0x80860000, descs);
724 		if (descs[0] >= 0x80860007)
725 			tmx86_init_longrun();
726 	}
727 
728 	/* If we have FXSAVE/FXRESTOR, use them. */
729 	if (cpu_feature & CPUID_FXSR) {
730 		i386_use_fxsave = 1;
731 		/*
732 		 * If we have SSE/SSE2, enable XMM exceptions, and
733 		 * notify userland.
734 		 */
735 		if (cpu_feature & CPUID_SSE)
736 			i386_has_sse = 1;
737 		if (cpu_feature & CPUID_SSE2)
738 			i386_has_sse2 = 1;
739 	} else
740 		i386_use_fxsave = 0;
741 #endif	/* i386 */
742 
743 #ifdef ENHANCED_SPEEDSTEP
744 	if (cpu_feature2 & CPUID2_EST) {
745 		if (rdmsr(MSR_MISC_ENABLE) & (1 << 16))
746 			est_init(cpu_vendor);
747 	}
748 #endif /* ENHANCED_SPEEDSTEP */
749 
750 #ifdef INTEL_CORETEMP
751 	if (cpu_vendor == CPUVENDOR_INTEL && cpuid_level >= 0x06)
752 		coretemp_register(ci);
753 #endif
754 
755 #ifdef VIA_C7TEMP
756 	if (cpu_vendor == CPUVENDOR_IDT &&
757 	    CPUID2FAMILY(ci->ci_signature) == 6 &&
758 	    CPUID2MODEL(ci->ci_signature) >= 0x9) {
759 		uint32_t descs[4];
760 
761 		x86_cpuid(0xc0000000, descs);
762 		if (descs[0] >= 0xc0000002)	/* has temp sensor */
763 			viac7temp_register(ci);
764 	}
765 #endif
766 
767 #if defined(POWERNOW_K7) || defined(POWERNOW_K8)
768 	if (cpu_vendor == CPUVENDOR_AMD && powernow_probe(ci)) {
769 		switch (CPUID2FAMILY(ci->ci_signature)) {
770 #ifdef POWERNOW_K7
771 		case 6:
772 			k7_powernow_init();
773 			break;
774 #endif
775 #ifdef POWERNOW_K8
776 		case 15:
777 			k8_powernow_init();
778 			break;
779 #endif
780 		default:
781 			break;
782 		}
783 	}
784 #endif /* POWERNOW_K7 || POWERNOW_K8 */
785 
786 #ifdef INTEL_ONDEMAND_CLOCKMOD
787 	if (cpuid_level >= 1) {
788 		clockmod_init();
789 	}
790 #endif
791 }
792