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