i386.c revision 1.52 1 /* $NetBSD: i386.c,v 1.52 2013/12/23 11:17:20 msaitoh 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 #ifndef lint
60 __RCSID("$NetBSD: i386.c,v 1.52 2013/12/23 11:17:20 msaitoh Exp $");
61 #endif /* not lint */
62
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/bitops.h>
66 #include <sys/sysctl.h>
67 #include <sys/ioctl.h>
68 #include <sys/cpuio.h>
69
70 #include <errno.h>
71 #include <string.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <err.h>
75 #include <assert.h>
76 #include <math.h>
77 #include <util.h>
78
79 #include <machine/specialreg.h>
80 #include <machine/cpu.h>
81
82 #include <x86/cpuvar.h>
83 #include <x86/cputypes.h>
84 #include <x86/cacheinfo.h>
85 #include <x86/cpu_ucode.h>
86
87 #include "../cpuctl.h"
88 #include "cpuctl_i386.h"
89
90 /* Size of buffer for printing humanized numbers */
91 #define HUMAN_BUFSIZE sizeof("999KB")
92
93 struct cpu_info {
94 const char *ci_dev;
95 int32_t ci_cpu_type; /* for cpu's without cpuid */
96 int32_t ci_cpuid_level; /* highest cpuid supported */
97 uint32_t ci_cpuid_extlevel; /* highest cpuid extended func lv */
98 uint32_t ci_signature; /* X86 cpuid type */
99 uint32_t ci_family; /* from ci_signature */
100 uint32_t ci_model; /* from ci_signature */
101 uint32_t ci_feat_val[8]; /* X86 CPUID feature bits
102 * [0] basic features %edx
103 * [1] basic features %ecx
104 * [2] extended features %edx
105 * [3] extended features %ecx
106 * [4] VIA padlock features
107 * [5] XCR0 bits (d:0 %eax)
108 * [6] xsave flags (d:1 %eax)
109 */
110 uint32_t ci_cpu_class; /* CPU class */
111 uint32_t ci_brand_id; /* Intel brand id */
112 uint32_t ci_vendor[4]; /* vendor string */
113 uint32_t ci_cpu_serial[3]; /* PIII serial number */
114 uint64_t ci_tsc_freq; /* cpu cycles/second */
115 uint8_t ci_packageid;
116 uint8_t ci_coreid;
117 uint8_t ci_smtid;
118 uint32_t ci_initapicid;
119
120 uint32_t ci_cur_xsave;
121 uint32_t ci_max_xsave;
122
123 struct x86_cache_info ci_cinfo[CAI_COUNT];
124 void (*ci_info)(struct cpu_info *);
125 };
126
127 struct cpu_nocpuid_nameclass {
128 int cpu_vendor;
129 const char *cpu_vendorname;
130 const char *cpu_name;
131 int cpu_class;
132 void (*cpu_setup)(struct cpu_info *);
133 void (*cpu_cacheinfo)(struct cpu_info *);
134 void (*cpu_info)(struct cpu_info *);
135 };
136
137 struct cpu_cpuid_nameclass {
138 const char *cpu_id;
139 int cpu_vendor;
140 const char *cpu_vendorname;
141 struct cpu_cpuid_family {
142 int cpu_class;
143 const char *cpu_models[256];
144 const char *cpu_model_default;
145 void (*cpu_setup)(struct cpu_info *);
146 void (*cpu_probe)(struct cpu_info *);
147 void (*cpu_info)(struct cpu_info *);
148 } cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
149 };
150
151 static const struct x86_cache_info intel_cpuid_cache_info[] = INTEL_CACHE_INFO;
152
153 /*
154 * Map Brand ID from cpuid instruction to brand name.
155 * Source: Table 3-24, Mapping of Brand Indices; and Intel 64 and IA-32
156 * Processor Brand Strings, Chapter 3 in "Intel (R) 64 and IA-32
157 * Architectures Software Developer's Manual, Volume 2A".
158 */
159 static const char * const i386_intel_brand[] = {
160 "", /* Unsupported */
161 "Celeron", /* Intel (R) Celeron (TM) processor */
162 "Pentium III", /* Intel (R) Pentium (R) III processor */
163 "Pentium III Xeon", /* Intel (R) Pentium (R) III Xeon (TM) processor */
164 "Pentium III", /* Intel (R) Pentium (R) III processor */
165 "", /* 0x05: Reserved */
166 "Mobile Pentium III", /* Mobile Intel (R) Pentium (R) III processor-M */
167 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
168 "Pentium 4", /* Intel (R) Pentium (R) 4 processor */
169 "Pentium 4", /* Intel (R) Pentium (R) 4 processor */
170 "Celeron", /* Intel (R) Celeron (TM) processor */
171 "Xeon", /* Intel (R) Xeon (TM) processor */
172 "Xeon MP", /* Intel (R) Xeon (TM) processor MP */
173 "", /* 0x0d: Reserved */
174 "Mobile Pentium 4", /* Mobile Intel (R) Pentium (R) 4 processor-M */
175 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
176 "", /* 0x10: Reserved */
177 "Mobile Genuine", /* Moblie Genuine Intel (R) processor */
178 "Celeron M", /* Intel (R) Celeron (R) M processor */
179 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
180 "Celeron", /* Intel (R) Celeron (R) processor */
181 "Mobile Genuine", /* Moblie Genuine Intel (R) processor */
182 "Pentium M", /* Intel (R) Pentium (R) M processor */
183 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
184 };
185
186 /*
187 * AMD processors don't have Brand IDs, so we need these names for probe.
188 */
189 static const char * const amd_brand[] = {
190 "",
191 "Duron", /* AMD Duron(tm) */
192 "MP", /* AMD Athlon(tm) MP */
193 "XP", /* AMD Athlon(tm) XP */
194 "4" /* AMD Athlon(tm) 4 */
195 };
196
197 static int cpu_vendor;
198 static char cpu_brand_string[49];
199 static char amd_brand_name[48];
200 static int use_pae, largepagesize;
201
202 /* Setup functions */
203 static void disable_tsc(struct cpu_info *);
204 static void amd_family5_setup(struct cpu_info *);
205 static void cyrix6x86_cpu_setup(struct cpu_info *);
206 static void winchip_cpu_setup(struct cpu_info *);
207 /* Brand/Model name functions */
208 static const char *intel_family6_name(struct cpu_info *);
209 static const char *amd_amd64_name(struct cpu_info *);
210 /* Probe functions */
211 static void amd_family6_probe(struct cpu_info *);
212 static void powernow_probe(struct cpu_info *);
213 static void intel_family_new_probe(struct cpu_info *);
214 static void via_cpu_probe(struct cpu_info *);
215 /* (Cache) Info functions */
216 static void intel_cpu_cacheinfo(struct cpu_info *);
217 static void amd_cpu_cacheinfo(struct cpu_info *);
218 static void via_cpu_cacheinfo(struct cpu_info *);
219 static void tmx86_get_longrun_status(u_int *, u_int *, u_int *);
220 static void transmeta_cpu_info(struct cpu_info *);
221 /* Common functions */
222 static void cpu_probe_base_features(struct cpu_info *, const char *);
223 static void cpu_probe_features(struct cpu_info *);
224 static void print_bits(const char *, const char *, const char *, uint32_t);
225 static void identifycpu_cpuids(struct cpu_info *);
226 static const char *print_cache_config(struct cpu_info *, int, const char *,
227 const char *);
228 static const char *print_tlb_config(struct cpu_info *, int, const char *,
229 const char *);
230 static const struct x86_cache_info *cache_info_lookup(
231 const struct x86_cache_info *, uint8_t);
232 static void x86_print_cacheinfo(struct cpu_info *);
233
234 /*
235 * Note: these are just the ones that may not have a cpuid instruction.
236 * We deal with the rest in a different way.
237 */
238 const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[] = {
239 { CPUVENDOR_INTEL, "Intel", "386SX", CPUCLASS_386,
240 NULL, NULL, NULL }, /* CPU_386SX */
241 { CPUVENDOR_INTEL, "Intel", "386DX", CPUCLASS_386,
242 NULL, NULL, NULL }, /* CPU_386 */
243 { CPUVENDOR_INTEL, "Intel", "486SX", CPUCLASS_486,
244 NULL, NULL, NULL }, /* CPU_486SX */
245 { CPUVENDOR_INTEL, "Intel", "486DX", CPUCLASS_486,
246 NULL, NULL, NULL }, /* CPU_486 */
247 { CPUVENDOR_CYRIX, "Cyrix", "486DLC", CPUCLASS_486,
248 NULL, NULL, NULL }, /* CPU_486DLC */
249 { CPUVENDOR_CYRIX, "Cyrix", "6x86", CPUCLASS_486,
250 NULL, NULL, NULL }, /* CPU_6x86 */
251 { CPUVENDOR_NEXGEN,"NexGen","586", CPUCLASS_386,
252 NULL, NULL, NULL }, /* CPU_NX586 */
253 };
254
255 const char *classnames[] = {
256 "386",
257 "486",
258 "586",
259 "686"
260 };
261
262 const char *modifiers[] = {
263 "",
264 "OverDrive",
265 "Dual",
266 ""
267 };
268
269 const struct cpu_cpuid_nameclass i386_cpuid_cpus[] = {
270 {
271 /*
272 * For Intel processors, check Chapter 35Model-specific
273 * registers (MSRS), in "Intel (R) 64 and IA-32 Architectures
274 * Software Developer's Manual, Volume 3C".
275 */
276 "GenuineIntel",
277 CPUVENDOR_INTEL,
278 "Intel",
279 /* Family 4 */
280 { {
281 CPUCLASS_486,
282 {
283 "486DX", "486DX", "486SX", "486DX2", "486SL",
284 "486SX2", 0, "486DX2 W/B Enhanced",
285 "486DX4", 0, 0, 0, 0, 0, 0, 0,
286 },
287 "486", /* Default */
288 NULL,
289 NULL,
290 intel_cpu_cacheinfo,
291 },
292 /* Family 5 */
293 {
294 CPUCLASS_586,
295 {
296 "Pentium (P5 A-step)", "Pentium (P5)",
297 "Pentium (P54C)", "Pentium (P24T)",
298 "Pentium/MMX", "Pentium", 0,
299 "Pentium (P54C)", "Pentium/MMX (Tillamook)",
300 0, 0, 0, 0, 0, 0, 0,
301 },
302 "Pentium", /* Default */
303 NULL,
304 NULL,
305 intel_cpu_cacheinfo,
306 },
307 /* Family 6 */
308 {
309 CPUCLASS_686,
310 {
311 [0x00] = "Pentium Pro (A-step)",
312 [0x01] = "Pentium Pro",
313 [0x03] = "Pentium II (Klamath)",
314 [0x04] = "Pentium Pro",
315 [0x05] = "Pentium II/Celeron (Deschutes)",
316 [0x06] = "Celeron (Mendocino)",
317 [0x07] = "Pentium III (Katmai)",
318 [0x08] = "Pentium III (Coppermine)",
319 [0x09] = "Pentium M (Banias)",
320 [0x0a] = "Pentium III Xeon (Cascades)",
321 [0x0b] = "Pentium III (Tualatin)",
322 [0x0d] = "Pentium M (Dothan)",
323 [0x0e] = "Pentium Core Duo, Core solo",
324 [0x0f] = "Xeon 30xx, 32xx, 51xx, 53xx, 73xx, "
325 "Core 2 Quad 6xxx, "
326 "Core 2 Extreme 6xxx, "
327 "Core 2 Duo 4xxx, 5xxx, 6xxx, 7xxx "
328 "and Pentium DC",
329 [0x15] = "EP80579 Integrated Processor",
330 [0x16] = "Celeron (45nm)",
331 [0x17] = "Xeon 31xx, 33xx, 52xx, 54xx, "
332 "Core 2 Quad 8xxx and 9xxx",
333 [0x1a] = "Core i7, Xeon 34xx, 35xx and 55xx "
334 "(Nehalem)",
335 [0x1c] = "Atom Family",
336 [0x1d] = "XeonMP 74xx (Nehalem)",
337 [0x1e] = "Core i7 and i5",
338 [0x1f] = "Core i7 and i5",
339 [0x25] = "Xeon 36xx & 56xx, i7, i5 and i3",
340 [0x26] = "Atom Family",
341 [0x27] = "Atom Family",
342 [0x2a] = "Xeon E3-12xx, 2nd gen i7, i5, "
343 "i3 2xxx",
344 [0x2c] = "Xeon 36xx & 56xx, i7, i5 and i3",
345 [0x2d] = "Xeon E5 Sandy Bridge family, "
346 "Core i7-39xx Extreme",
347 [0x2e] = "Xeon 75xx & 65xx",
348 [0x2f] = "Xeon E7 family",
349 [0x35] = "Atom Family",
350 [0x36] = "Atom S1000",
351 [0x37] = "Atom C2000, E3000",
352 [0x3a] = "Xeon E3-1200v2 and 3rd gen core, "
353 "Ivy Bridge",
354 [0x3c] = "4th gen Core, Xeon E3-12xx v3 "
355 "(Haswell)",
356 [0x3d] = "Next gen Core",
357 [0x3e] = "Xeon E5/E7, Ivy Bridge-EP",
358 [0x3f] = "Future gen Xeon",
359 [0x45] = "4th gen Core, Xeon E3-12xx v3 "
360 "(Haswell)",
361 [0x46] = "4th gen Core, Xeon E3-12xx v3 "
362 "(Haswell)",
363 [0x4d] = "Atom C2000, E3000",
364 },
365 "Pentium Pro, II or III", /* Default */
366 NULL,
367 intel_family_new_probe,
368 intel_cpu_cacheinfo,
369 },
370 /* Family > 6 */
371 {
372 CPUCLASS_686,
373 {
374 0, 0, 0, 0, 0, 0, 0, 0,
375 0, 0, 0, 0, 0, 0, 0, 0,
376 },
377 "Pentium 4", /* Default */
378 NULL,
379 intel_family_new_probe,
380 intel_cpu_cacheinfo,
381 } }
382 },
383 {
384 "AuthenticAMD",
385 CPUVENDOR_AMD,
386 "AMD",
387 /* Family 4 */
388 { {
389 CPUCLASS_486,
390 {
391 0, 0, 0, "Am486DX2 W/T",
392 0, 0, 0, "Am486DX2 W/B",
393 "Am486DX4 W/T or Am5x86 W/T 150",
394 "Am486DX4 W/B or Am5x86 W/B 150", 0, 0,
395 0, 0, "Am5x86 W/T 133/160",
396 "Am5x86 W/B 133/160",
397 },
398 "Am486 or Am5x86", /* Default */
399 NULL,
400 NULL,
401 NULL,
402 },
403 /* Family 5 */
404 {
405 CPUCLASS_586,
406 {
407 "K5", "K5", "K5", "K5", 0, 0, "K6",
408 "K6", "K6-2", "K6-III", "Geode LX", 0, 0,
409 "K6-2+/III+", 0, 0,
410 },
411 "K5 or K6", /* Default */
412 amd_family5_setup,
413 NULL,
414 amd_cpu_cacheinfo,
415 },
416 /* Family 6 */
417 {
418 CPUCLASS_686,
419 {
420 0, "Athlon Model 1", "Athlon Model 2",
421 "Duron", "Athlon Model 4 (Thunderbird)",
422 0, "Athlon", "Duron", "Athlon", 0,
423 "Athlon", 0, 0, 0, 0, 0,
424 },
425 "K7 (Athlon)", /* Default */
426 NULL,
427 amd_family6_probe,
428 amd_cpu_cacheinfo,
429 },
430 /* Family > 6 */
431 {
432 CPUCLASS_686,
433 {
434 0, 0, 0, 0, 0, 0, 0, 0,
435 0, 0, 0, 0, 0, 0, 0, 0,
436 },
437 "Unknown K8 (Athlon)", /* Default */
438 NULL,
439 amd_family6_probe,
440 amd_cpu_cacheinfo,
441 } }
442 },
443 {
444 "CyrixInstead",
445 CPUVENDOR_CYRIX,
446 "Cyrix",
447 /* Family 4 */
448 { {
449 CPUCLASS_486,
450 {
451 0, 0, 0,
452 "MediaGX",
453 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
454 },
455 "486", /* Default */
456 cyrix6x86_cpu_setup, /* XXX ?? */
457 NULL,
458 NULL,
459 },
460 /* Family 5 */
461 {
462 CPUCLASS_586,
463 {
464 0, 0, "6x86", 0,
465 "MMX-enhanced MediaGX (GXm)", /* or Geode? */
466 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
467 },
468 "6x86", /* Default */
469 cyrix6x86_cpu_setup,
470 NULL,
471 NULL,
472 },
473 /* Family 6 */
474 {
475 CPUCLASS_686,
476 {
477 "6x86MX", 0, 0, 0, 0, 0, 0, 0,
478 0, 0, 0, 0, 0, 0, 0, 0,
479 },
480 "6x86MX", /* Default */
481 cyrix6x86_cpu_setup,
482 NULL,
483 NULL,
484 },
485 /* Family > 6 */
486 {
487 CPUCLASS_686,
488 {
489 0, 0, 0, 0, 0, 0, 0, 0,
490 0, 0, 0, 0, 0, 0, 0, 0,
491 },
492 "Unknown 6x86MX", /* Default */
493 NULL,
494 NULL,
495 NULL,
496 } }
497 },
498 { /* MediaGX is now owned by National Semiconductor */
499 "Geode by NSC",
500 CPUVENDOR_CYRIX, /* XXX */
501 "National Semiconductor",
502 /* Family 4, NSC never had any of these */
503 { {
504 CPUCLASS_486,
505 {
506 0, 0, 0, 0, 0, 0, 0, 0,
507 0, 0, 0, 0, 0, 0, 0, 0,
508 },
509 "486 compatible", /* Default */
510 NULL,
511 NULL,
512 NULL,
513 },
514 /* Family 5: Geode family, formerly MediaGX */
515 {
516 CPUCLASS_586,
517 {
518 0, 0, 0, 0,
519 "Geode GX1",
520 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
521 },
522 "Geode", /* Default */
523 cyrix6x86_cpu_setup,
524 NULL,
525 amd_cpu_cacheinfo,
526 },
527 /* Family 6, not yet available from NSC */
528 {
529 CPUCLASS_686,
530 {
531 0, 0, 0, 0, 0, 0, 0, 0,
532 0, 0, 0, 0, 0, 0, 0, 0,
533 },
534 "Pentium Pro compatible", /* Default */
535 NULL,
536 NULL,
537 NULL,
538 },
539 /* Family > 6, not yet available from NSC */
540 {
541 CPUCLASS_686,
542 {
543 0, 0, 0, 0, 0, 0, 0, 0,
544 0, 0, 0, 0, 0, 0, 0, 0,
545 },
546 "Pentium Pro compatible", /* Default */
547 NULL,
548 NULL,
549 NULL,
550 } }
551 },
552 {
553 "CentaurHauls",
554 CPUVENDOR_IDT,
555 "IDT",
556 /* Family 4, IDT never had any of these */
557 { {
558 CPUCLASS_486,
559 {
560 0, 0, 0, 0, 0, 0, 0, 0,
561 0, 0, 0, 0, 0, 0, 0, 0,
562 },
563 "486 compatible", /* Default */
564 NULL,
565 NULL,
566 NULL,
567 },
568 /* Family 5 */
569 {
570 CPUCLASS_586,
571 {
572 0, 0, 0, 0, "WinChip C6", 0, 0, 0,
573 "WinChip 2", "WinChip 3", 0, 0, 0, 0, 0, 0,
574 },
575 "WinChip", /* Default */
576 winchip_cpu_setup,
577 NULL,
578 NULL,
579 },
580 /* Family 6, VIA acquired IDT Centaur design subsidiary */
581 {
582 CPUCLASS_686,
583 {
584 0, 0, 0, 0, 0, 0, "C3 Samuel",
585 "C3 Samuel 2/Ezra", "C3 Ezra-T",
586 "C3 Nehemiah", "C7 Esther", 0, 0, "C7 Esther",
587 0, "VIA Nano",
588 },
589 "Unknown VIA/IDT", /* Default */
590 NULL,
591 via_cpu_probe,
592 via_cpu_cacheinfo,
593 },
594 /* Family > 6, not yet available from VIA */
595 {
596 CPUCLASS_686,
597 {
598 0, 0, 0, 0, 0, 0, 0, 0,
599 0, 0, 0, 0, 0, 0, 0, 0,
600 },
601 "Pentium Pro compatible", /* Default */
602 NULL,
603 NULL,
604 NULL,
605 } }
606 },
607 {
608 "GenuineTMx86",
609 CPUVENDOR_TRANSMETA,
610 "Transmeta",
611 /* Family 4, Transmeta never had any of these */
612 { {
613 CPUCLASS_486,
614 {
615 0, 0, 0, 0, 0, 0, 0, 0,
616 0, 0, 0, 0, 0, 0, 0, 0,
617 },
618 "486 compatible", /* Default */
619 NULL,
620 NULL,
621 NULL,
622 },
623 /* Family 5 */
624 {
625 CPUCLASS_586,
626 {
627 0, 0, 0, 0, 0, 0, 0, 0,
628 0, 0, 0, 0, 0, 0, 0, 0,
629 },
630 "Crusoe", /* Default */
631 NULL,
632 NULL,
633 transmeta_cpu_info,
634 },
635 /* Family 6, not yet available from Transmeta */
636 {
637 CPUCLASS_686,
638 {
639 0, 0, 0, 0, 0, 0, 0, 0,
640 0, 0, 0, 0, 0, 0, 0, 0,
641 },
642 "Pentium Pro compatible", /* Default */
643 NULL,
644 NULL,
645 NULL,
646 },
647 /* Family > 6, not yet available from Transmeta */
648 {
649 CPUCLASS_686,
650 {
651 0, 0, 0, 0, 0, 0, 0, 0,
652 0, 0, 0, 0, 0, 0, 0, 0,
653 },
654 "Pentium Pro compatible", /* Default */
655 NULL,
656 NULL,
657 NULL,
658 } }
659 }
660 };
661
662 /*
663 * disable the TSC such that we don't use the TSC in microtime(9)
664 * because some CPUs got the implementation wrong.
665 */
666 static void
667 disable_tsc(struct cpu_info *ci)
668 {
669 if (ci->ci_feat_val[0] & CPUID_TSC) {
670 ci->ci_feat_val[0] &= ~CPUID_TSC;
671 aprint_error("WARNING: broken TSC disabled\n");
672 }
673 }
674
675 static void
676 amd_family5_setup(struct cpu_info *ci)
677 {
678
679 switch (ci->ci_model) {
680 case 0: /* AMD-K5 Model 0 */
681 /*
682 * According to the AMD Processor Recognition App Note,
683 * the AMD-K5 Model 0 uses the wrong bit to indicate
684 * support for global PTEs, instead using bit 9 (APIC)
685 * rather than bit 13 (i.e. "0x200" vs. 0x2000". Oops!).
686 */
687 if (ci->ci_feat_val[0] & CPUID_APIC)
688 ci->ci_feat_val[0] =
689 (ci->ci_feat_val[0] & ~CPUID_APIC) | CPUID_PGE;
690 /*
691 * XXX But pmap_pg_g is already initialized -- need to kick
692 * XXX the pmap somehow. How does the MP branch do this?
693 */
694 break;
695 }
696 }
697
698 static void
699 cyrix6x86_cpu_setup(struct cpu_info *ci)
700 {
701
702 /*
703 * Do not disable the TSC on the Geode GX, it's reported to
704 * work fine.
705 */
706 if (ci->ci_signature != 0x552)
707 disable_tsc(ci);
708 }
709
710 static void
711 winchip_cpu_setup(struct cpu_info *ci)
712 {
713 switch (ci->ci_model) {
714 case 4: /* WinChip C6 */
715 disable_tsc(ci);
716 }
717 }
718
719
720 static const char *
721 intel_family6_name(struct cpu_info *ci)
722 {
723 const char *ret = NULL;
724 u_int l2cache = ci->ci_cinfo[CAI_L2CACHE].cai_totalsize;
725
726 if (ci->ci_model == 5) {
727 switch (l2cache) {
728 case 0:
729 case 128 * 1024:
730 ret = "Celeron (Covington)";
731 break;
732 case 256 * 1024:
733 ret = "Mobile Pentium II (Dixon)";
734 break;
735 case 512 * 1024:
736 ret = "Pentium II";
737 break;
738 case 1 * 1024 * 1024:
739 case 2 * 1024 * 1024:
740 ret = "Pentium II Xeon";
741 break;
742 }
743 } else if (ci->ci_model == 6) {
744 switch (l2cache) {
745 case 256 * 1024:
746 case 512 * 1024:
747 ret = "Mobile Pentium II";
748 break;
749 }
750 } else if (ci->ci_model == 7) {
751 switch (l2cache) {
752 case 512 * 1024:
753 ret = "Pentium III";
754 break;
755 case 1 * 1024 * 1024:
756 case 2 * 1024 * 1024:
757 ret = "Pentium III Xeon";
758 break;
759 }
760 } else if (ci->ci_model >= 8) {
761 if (ci->ci_brand_id && ci->ci_brand_id < 0x10) {
762 switch (ci->ci_brand_id) {
763 case 0x3:
764 if (ci->ci_signature == 0x6B1)
765 ret = "Celeron";
766 break;
767 case 0x8:
768 if (ci->ci_signature >= 0xF13)
769 ret = "genuine processor";
770 break;
771 case 0xB:
772 if (ci->ci_signature >= 0xF13)
773 ret = "Xeon MP";
774 break;
775 case 0xE:
776 if (ci->ci_signature < 0xF13)
777 ret = "Xeon";
778 break;
779 }
780 if (ret == NULL)
781 ret = i386_intel_brand[ci->ci_brand_id];
782 }
783 }
784
785 return ret;
786 }
787
788 /*
789 * Identify AMD64 CPU names from cpuid.
790 *
791 * Based on:
792 * "Revision Guide for AMD Athlon 64 and AMD Opteron Processors"
793 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25759.pdf
794 * "Revision Guide for AMD NPT Family 0Fh Processors"
795 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
796 * and other miscellaneous reports.
797 *
798 * This is all rather pointless, these are cross 'brand' since the raw
799 * silicon is shared.
800 */
801 static const char *
802 amd_amd64_name(struct cpu_info *ci)
803 {
804 static char family_str[32];
805
806 /* Only called if family >= 15 */
807
808 switch (ci->ci_family) {
809 case 15:
810 switch (ci->ci_model) {
811 case 0x21: /* rev JH-E1/E6 */
812 case 0x41: /* rev JH-F2 */
813 return "Dual-Core Opteron";
814 case 0x23: /* rev JH-E6 (Toledo) */
815 return "Dual-Core Opteron or Athlon 64 X2";
816 case 0x43: /* rev JH-F2 (Windsor) */
817 return "Athlon 64 FX or Athlon 64 X2";
818 case 0x24: /* rev SH-E5 (Lancaster?) */
819 return "Mobile Athlon 64 or Turion 64";
820 case 0x05: /* rev SH-B0/B3/C0/CG (SledgeHammer?) */
821 return "Opteron or Athlon 64 FX";
822 case 0x15: /* rev SH-D0 */
823 case 0x25: /* rev SH-E4 */
824 return "Opteron";
825 case 0x27: /* rev DH-E4, SH-E4 */
826 return "Athlon 64 or Athlon 64 FX or Opteron";
827 case 0x48: /* rev BH-F2 */
828 return "Turion 64 X2";
829 case 0x04: /* rev SH-B0/C0/CG (ClawHammer) */
830 case 0x07: /* rev SH-CG (ClawHammer) */
831 case 0x0b: /* rev CH-CG */
832 case 0x14: /* rev SH-D0 */
833 case 0x17: /* rev SH-D0 */
834 case 0x1b: /* rev CH-D0 */
835 return "Athlon 64";
836 case 0x2b: /* rev BH-E4 (Manchester) */
837 case 0x4b: /* rev BH-F2 (Windsor) */
838 return "Athlon 64 X2";
839 case 0x6b: /* rev BH-G1 (Brisbane) */
840 return "Athlon X2 or Athlon 64 X2";
841 case 0x08: /* rev CH-CG */
842 case 0x0c: /* rev DH-CG (Newcastle) */
843 case 0x0e: /* rev DH-CG (Newcastle?) */
844 case 0x0f: /* rev DH-CG (Newcastle/Paris) */
845 case 0x18: /* rev CH-D0 */
846 case 0x1c: /* rev DH-D0 (Winchester) */
847 case 0x1f: /* rev DH-D0 (Winchester/Victoria) */
848 case 0x2c: /* rev DH-E3/E6 */
849 case 0x2f: /* rev DH-E3/E6 (Venice/Palermo) */
850 case 0x4f: /* rev DH-F2 (Orleans/Manila) */
851 case 0x5f: /* rev DH-F2 (Orleans/Manila) */
852 case 0x6f: /* rev DH-G1 */
853 return "Athlon 64 or Sempron";
854 default:
855 break;
856 }
857 return "Unknown AMD64 CPU";
858
859 #if 0
860 case 16:
861 return "Family 10h";
862 case 17:
863 return "Family 11h";
864 case 18:
865 return "Family 12h";
866 case 19:
867 return "Family 14h";
868 case 20:
869 return "Family 15h";
870 #endif
871
872 default:
873 break;
874 }
875
876 snprintf(family_str, sizeof family_str, "Family %xh", ci->ci_family);
877 return family_str;
878 }
879
880 static void
881 intel_family_new_probe(struct cpu_info *ci)
882 {
883 uint32_t descs[4];
884
885 x86_cpuid(0x80000000, descs);
886
887 /*
888 * Determine extended feature flags.
889 */
890 if (descs[0] >= 0x80000001) {
891 x86_cpuid(0x80000001, descs);
892 ci->ci_feat_val[2] |= descs[3];
893 ci->ci_feat_val[3] |= descs[2];
894 }
895 }
896
897 static void
898 via_cpu_probe(struct cpu_info *ci)
899 {
900 u_int stepping = CPUID_TO_STEPPING(ci->ci_signature);
901 u_int descs[4];
902 u_int lfunc;
903
904 /*
905 * Determine the largest extended function value.
906 */
907 x86_cpuid(0x80000000, descs);
908 lfunc = descs[0];
909
910 /*
911 * Determine the extended feature flags.
912 */
913 if (lfunc >= 0x80000001) {
914 x86_cpuid(0x80000001, descs);
915 ci->ci_feat_val[2] |= descs[3];
916 }
917
918 if (ci->ci_model < 0x9 || (ci->ci_model == 0x9 && stepping < 3))
919 return;
920
921 /* Nehemiah or Esther */
922 x86_cpuid(0xc0000000, descs);
923 lfunc = descs[0];
924 if (lfunc < 0xc0000001) /* no ACE, no RNG */
925 return;
926
927 x86_cpuid(0xc0000001, descs);
928 lfunc = descs[3];
929 ci->ci_feat_val[4] = lfunc;
930 }
931
932 static void
933 amd_family6_probe(struct cpu_info *ci)
934 {
935 uint32_t descs[4];
936 char *p;
937 size_t i;
938
939 x86_cpuid(0x80000000, descs);
940
941 /*
942 * Determine the extended feature flags.
943 */
944 if (descs[0] >= 0x80000001) {
945 x86_cpuid(0x80000001, descs);
946 ci->ci_feat_val[2] |= descs[3]; /* %edx */
947 ci->ci_feat_val[3] = descs[2]; /* %ecx */
948 }
949
950 if (*cpu_brand_string == '\0')
951 return;
952
953 for (i = 1; i < __arraycount(amd_brand); i++)
954 if ((p = strstr(cpu_brand_string, amd_brand[i])) != NULL) {
955 ci->ci_brand_id = i;
956 strlcpy(amd_brand_name, p, sizeof(amd_brand_name));
957 break;
958 }
959 }
960
961 static void
962 intel_cpu_cacheinfo(struct cpu_info *ci)
963 {
964 const struct x86_cache_info *cai;
965 u_int descs[4];
966 int iterations, i, j;
967 int type, level;
968 int ways, partitions, linesize, sets;
969 int caitype = -1;
970 int totalsize;
971 uint8_t desc;
972
973 /* Return if the cpu is old pre-cpuid instruction cpu */
974 if (ci->ci_cpu_type >= 0)
975 return;
976
977 if (ci->ci_cpuid_level < 2)
978 return;
979
980 /*
981 * Parse the cache info from `cpuid leaf 2', if we have it.
982 * XXX This is kinda ugly, but hey, so is the architecture...
983 */
984 x86_cpuid(2, descs);
985 iterations = descs[0] & 0xff;
986 while (iterations-- > 0) {
987 for (i = 0; i < 4; i++) {
988 if (descs[i] & 0x80000000)
989 continue;
990 for (j = 0; j < 4; j++) {
991 if (i == 0 && j == 0)
992 continue;
993 desc = (descs[i] >> (j * 8)) & 0xff;
994 if (desc == 0)
995 continue;
996 cai = cache_info_lookup(intel_cpuid_cache_info,
997 desc);
998 if (cai != NULL)
999 ci->ci_cinfo[cai->cai_index] = *cai;
1000 }
1001 }
1002 x86_cpuid(2, descs);
1003 }
1004
1005 if (ci->ci_cpuid_level < 4)
1006 return;
1007
1008 /* Parse the cache info from `cpuid leaf 4', if we have it. */
1009 for (i = 0; ; i++) {
1010 x86_cpuid2(4, i, descs);
1011 type = __SHIFTOUT(descs[0], CPUID_DCP_CACHETYPE);
1012 if (type == CPUID_DCP_CACHETYPE_N)
1013 break;
1014 level = __SHIFTOUT(descs[0], CPUID_DCP_CACHELEVEL);
1015 switch (level) {
1016 case 1:
1017 if (type == CPUID_DCP_CACHETYPE_I)
1018 caitype = CAI_ICACHE;
1019 else if (type == CPUID_DCP_CACHETYPE_D)
1020 caitype = CAI_DCACHE;
1021 else
1022 caitype = -1;
1023 break;
1024 case 2:
1025 if (type == CPUID_DCP_CACHETYPE_U)
1026 caitype = CAI_L2CACHE;
1027 else
1028 caitype = -1;
1029 break;
1030 case 3:
1031 if (type == CPUID_DCP_CACHETYPE_U)
1032 caitype = CAI_L3CACHE;
1033 else
1034 caitype = -1;
1035 break;
1036 default:
1037 caitype = -1;
1038 break;
1039 }
1040 if (caitype == -1) {
1041 printf("unknown cache level&type (%d & %d)\n",
1042 level, type);
1043 continue;
1044 }
1045 ways = __SHIFTOUT(descs[1], CPUID_DCP_WAYS) + 1;
1046 partitions =__SHIFTOUT(descs[1], CPUID_DCP_PARTITIONS)
1047 + 1;
1048 linesize = __SHIFTOUT(descs[1], CPUID_DCP_LINESIZE)
1049 + 1;
1050 sets = descs[2] + 1;
1051 totalsize = ways * partitions * linesize * sets;
1052 ci->ci_cinfo[caitype].cai_totalsize = totalsize;
1053 ci->ci_cinfo[caitype].cai_associativity = ways;
1054 ci->ci_cinfo[caitype].cai_linesize = linesize;
1055 }
1056 }
1057
1058 static const struct x86_cache_info amd_cpuid_l2cache_assoc_info[] =
1059 AMD_L2CACHE_INFO;
1060
1061 static const struct x86_cache_info amd_cpuid_l3cache_assoc_info[] =
1062 AMD_L3CACHE_INFO;
1063
1064 static void
1065 amd_cpu_cacheinfo(struct cpu_info *ci)
1066 {
1067 const struct x86_cache_info *cp;
1068 struct x86_cache_info *cai;
1069 u_int descs[4];
1070 u_int lfunc;
1071
1072 /*
1073 * K5 model 0 has none of this info.
1074 */
1075 if (ci->ci_family == 5 && ci->ci_model == 0)
1076 return;
1077
1078 /*
1079 * Determine the largest extended function value.
1080 */
1081 x86_cpuid(0x80000000, descs);
1082 lfunc = descs[0];
1083
1084 /*
1085 * Determine L1 cache/TLB info.
1086 */
1087 if (lfunc < 0x80000005) {
1088 /* No L1 cache info available. */
1089 return;
1090 }
1091
1092 x86_cpuid(0x80000005, descs);
1093
1094 /*
1095 * K6-III and higher have large page TLBs.
1096 */
1097 if ((ci->ci_family == 5 && ci->ci_model >= 9) || ci->ci_family >= 6) {
1098 cai = &ci->ci_cinfo[CAI_ITLB2];
1099 cai->cai_totalsize = AMD_L1_EAX_ITLB_ENTRIES(descs[0]);
1100 cai->cai_associativity = AMD_L1_EAX_ITLB_ASSOC(descs[0]);
1101 cai->cai_linesize = largepagesize;
1102
1103 cai = &ci->ci_cinfo[CAI_DTLB2];
1104 cai->cai_totalsize = AMD_L1_EAX_DTLB_ENTRIES(descs[0]);
1105 cai->cai_associativity = AMD_L1_EAX_DTLB_ASSOC(descs[0]);
1106 cai->cai_linesize = largepagesize;
1107 }
1108
1109 cai = &ci->ci_cinfo[CAI_ITLB];
1110 cai->cai_totalsize = AMD_L1_EBX_ITLB_ENTRIES(descs[1]);
1111 cai->cai_associativity = AMD_L1_EBX_ITLB_ASSOC(descs[1]);
1112 cai->cai_linesize = (4 * 1024);
1113
1114 cai = &ci->ci_cinfo[CAI_DTLB];
1115 cai->cai_totalsize = AMD_L1_EBX_DTLB_ENTRIES(descs[1]);
1116 cai->cai_associativity = AMD_L1_EBX_DTLB_ASSOC(descs[1]);
1117 cai->cai_linesize = (4 * 1024);
1118
1119 cai = &ci->ci_cinfo[CAI_DCACHE];
1120 cai->cai_totalsize = AMD_L1_ECX_DC_SIZE(descs[2]);
1121 cai->cai_associativity = AMD_L1_ECX_DC_ASSOC(descs[2]);
1122 cai->cai_linesize = AMD_L1_ECX_DC_LS(descs[2]);
1123
1124 cai = &ci->ci_cinfo[CAI_ICACHE];
1125 cai->cai_totalsize = AMD_L1_EDX_IC_SIZE(descs[3]);
1126 cai->cai_associativity = AMD_L1_EDX_IC_ASSOC(descs[3]);
1127 cai->cai_linesize = AMD_L1_EDX_IC_LS(descs[3]);
1128
1129 /*
1130 * Determine L2 cache/TLB info.
1131 */
1132 if (lfunc < 0x80000006) {
1133 /* No L2 cache info available. */
1134 return;
1135 }
1136
1137 x86_cpuid(0x80000006, descs);
1138
1139 cai = &ci->ci_cinfo[CAI_L2_ITLB];
1140 cai->cai_totalsize = AMD_L2_EBX_IUTLB_ENTRIES(descs[1]);
1141 cai->cai_associativity = AMD_L2_EBX_IUTLB_ASSOC(descs[1]);
1142 cai->cai_linesize = (4 * 1024);
1143 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1144 cai->cai_associativity);
1145 if (cp != NULL)
1146 cai->cai_associativity = cp->cai_associativity;
1147 else
1148 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1149
1150 cai = &ci->ci_cinfo[CAI_L2_ITLB2];
1151 cai->cai_totalsize = AMD_L2_EAX_IUTLB_ENTRIES(descs[0]);
1152 cai->cai_associativity = AMD_L2_EAX_IUTLB_ASSOC(descs[0]);
1153 cai->cai_linesize = largepagesize;
1154 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1155 cai->cai_associativity);
1156 if (cp != NULL)
1157 cai->cai_associativity = cp->cai_associativity;
1158 else
1159 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1160
1161 cai = &ci->ci_cinfo[CAI_L2_DTLB];
1162 cai->cai_totalsize = AMD_L2_EBX_DTLB_ENTRIES(descs[1]);
1163 cai->cai_associativity = AMD_L2_EBX_DTLB_ASSOC(descs[1]);
1164 cai->cai_linesize = (4 * 1024);
1165 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1166 cai->cai_associativity);
1167 if (cp != NULL)
1168 cai->cai_associativity = cp->cai_associativity;
1169 else
1170 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1171
1172 cai = &ci->ci_cinfo[CAI_L2_DTLB2];
1173 cai->cai_totalsize = AMD_L2_EAX_DTLB_ENTRIES(descs[0]);
1174 cai->cai_associativity = AMD_L2_EAX_DTLB_ASSOC(descs[0]);
1175 cai->cai_linesize = largepagesize;
1176 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1177 cai->cai_associativity);
1178 if (cp != NULL)
1179 cai->cai_associativity = cp->cai_associativity;
1180 else
1181 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1182
1183 cai = &ci->ci_cinfo[CAI_L2CACHE];
1184 cai->cai_totalsize = AMD_L2_ECX_C_SIZE(descs[2]);
1185 cai->cai_associativity = AMD_L2_ECX_C_ASSOC(descs[2]);
1186 cai->cai_linesize = AMD_L2_ECX_C_LS(descs[2]);
1187
1188 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1189 cai->cai_associativity);
1190 if (cp != NULL)
1191 cai->cai_associativity = cp->cai_associativity;
1192 else
1193 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1194
1195 /*
1196 * Determine L3 cache info on AMD Family 10h and newer processors
1197 */
1198 if (ci->ci_family >= 0x10) {
1199 cai = &ci->ci_cinfo[CAI_L3CACHE];
1200 cai->cai_totalsize = AMD_L3_EDX_C_SIZE(descs[3]);
1201 cai->cai_associativity = AMD_L3_EDX_C_ASSOC(descs[3]);
1202 cai->cai_linesize = AMD_L3_EDX_C_LS(descs[3]);
1203
1204 cp = cache_info_lookup(amd_cpuid_l3cache_assoc_info,
1205 cai->cai_associativity);
1206 if (cp != NULL)
1207 cai->cai_associativity = cp->cai_associativity;
1208 else
1209 cai->cai_associativity = 0; /* XXX Unkn/Rsvd */
1210 }
1211
1212 /*
1213 * Determine 1GB TLB info.
1214 */
1215 if (lfunc < 0x80000019) {
1216 /* No 1GB TLB info available. */
1217 return;
1218 }
1219
1220 x86_cpuid(0x80000019, descs);
1221
1222 cai = &ci->ci_cinfo[CAI_L1_1GBITLB];
1223 cai->cai_totalsize = AMD_L1_1GB_EAX_IUTLB_ENTRIES(descs[0]);
1224 cai->cai_associativity = AMD_L1_1GB_EAX_IUTLB_ASSOC(descs[0]);
1225 cai->cai_linesize = (1024 * 1024 * 1024);
1226 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1227 cai->cai_associativity);
1228 if (cp != NULL)
1229 cai->cai_associativity = cp->cai_associativity;
1230 else
1231 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1232
1233 cai = &ci->ci_cinfo[CAI_L1_1GBDTLB];
1234 cai->cai_totalsize = AMD_L1_1GB_EAX_DTLB_ENTRIES(descs[0]);
1235 cai->cai_associativity = AMD_L1_1GB_EAX_DTLB_ASSOC(descs[0]);
1236 cai->cai_linesize = (1024 * 1024 * 1024);
1237 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1238 cai->cai_associativity);
1239 if (cp != NULL)
1240 cai->cai_associativity = cp->cai_associativity;
1241 else
1242 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1243
1244 cai = &ci->ci_cinfo[CAI_L2_1GBITLB];
1245 cai->cai_totalsize = AMD_L2_1GB_EBX_IUTLB_ENTRIES(descs[1]);
1246 cai->cai_associativity = AMD_L2_1GB_EBX_IUTLB_ASSOC(descs[1]);
1247 cai->cai_linesize = (1024 * 1024 * 1024);
1248 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1249 cai->cai_associativity);
1250 if (cp != NULL)
1251 cai->cai_associativity = cp->cai_associativity;
1252 else
1253 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1254
1255 cai = &ci->ci_cinfo[CAI_L2_1GBDTLB];
1256 cai->cai_totalsize = AMD_L2_1GB_EBX_DUTLB_ENTRIES(descs[1]);
1257 cai->cai_associativity = AMD_L2_1GB_EBX_DUTLB_ASSOC(descs[1]);
1258 cai->cai_linesize = (1024 * 1024 * 1024);
1259 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1260 cai->cai_associativity);
1261 if (cp != NULL)
1262 cai->cai_associativity = cp->cai_associativity;
1263 else
1264 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1265 }
1266
1267 static void
1268 via_cpu_cacheinfo(struct cpu_info *ci)
1269 {
1270 struct x86_cache_info *cai;
1271 int stepping;
1272 u_int descs[4];
1273 u_int lfunc;
1274
1275 stepping = CPUID_TO_STEPPING(ci->ci_signature);
1276
1277 /*
1278 * Determine the largest extended function value.
1279 */
1280 x86_cpuid(0x80000000, descs);
1281 lfunc = descs[0];
1282
1283 /*
1284 * Determine L1 cache/TLB info.
1285 */
1286 if (lfunc < 0x80000005) {
1287 /* No L1 cache info available. */
1288 return;
1289 }
1290
1291 x86_cpuid(0x80000005, descs);
1292
1293 cai = &ci->ci_cinfo[CAI_ITLB];
1294 cai->cai_totalsize = VIA_L1_EBX_ITLB_ENTRIES(descs[1]);
1295 cai->cai_associativity = VIA_L1_EBX_ITLB_ASSOC(descs[1]);
1296 cai->cai_linesize = (4 * 1024);
1297
1298 cai = &ci->ci_cinfo[CAI_DTLB];
1299 cai->cai_totalsize = VIA_L1_EBX_DTLB_ENTRIES(descs[1]);
1300 cai->cai_associativity = VIA_L1_EBX_DTLB_ASSOC(descs[1]);
1301 cai->cai_linesize = (4 * 1024);
1302
1303 cai = &ci->ci_cinfo[CAI_DCACHE];
1304 cai->cai_totalsize = VIA_L1_ECX_DC_SIZE(descs[2]);
1305 cai->cai_associativity = VIA_L1_ECX_DC_ASSOC(descs[2]);
1306 cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[2]);
1307 if (ci->ci_model == 9 && stepping == 8) {
1308 /* Erratum: stepping 8 reports 4 when it should be 2 */
1309 cai->cai_associativity = 2;
1310 }
1311
1312 cai = &ci->ci_cinfo[CAI_ICACHE];
1313 cai->cai_totalsize = VIA_L1_EDX_IC_SIZE(descs[3]);
1314 cai->cai_associativity = VIA_L1_EDX_IC_ASSOC(descs[3]);
1315 cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[3]);
1316 if (ci->ci_model == 9 && stepping == 8) {
1317 /* Erratum: stepping 8 reports 4 when it should be 2 */
1318 cai->cai_associativity = 2;
1319 }
1320
1321 /*
1322 * Determine L2 cache/TLB info.
1323 */
1324 if (lfunc < 0x80000006) {
1325 /* No L2 cache info available. */
1326 return;
1327 }
1328
1329 x86_cpuid(0x80000006, descs);
1330
1331 cai = &ci->ci_cinfo[CAI_L2CACHE];
1332 if (ci->ci_model >= 9) {
1333 cai->cai_totalsize = VIA_L2N_ECX_C_SIZE(descs[2]);
1334 cai->cai_associativity = VIA_L2N_ECX_C_ASSOC(descs[2]);
1335 cai->cai_linesize = VIA_L2N_ECX_C_LS(descs[2]);
1336 } else {
1337 cai->cai_totalsize = VIA_L2_ECX_C_SIZE(descs[2]);
1338 cai->cai_associativity = VIA_L2_ECX_C_ASSOC(descs[2]);
1339 cai->cai_linesize = VIA_L2_ECX_C_LS(descs[2]);
1340 }
1341 }
1342
1343 static void
1344 tmx86_get_longrun_status(u_int *frequency, u_int *voltage, u_int *percentage)
1345 {
1346 u_int descs[4];
1347
1348 x86_cpuid(0x80860007, descs);
1349 *frequency = descs[0];
1350 *voltage = descs[1];
1351 *percentage = descs[2];
1352 }
1353
1354 static void
1355 transmeta_cpu_info(struct cpu_info *ci)
1356 {
1357 u_int descs[4], nreg;
1358 u_int frequency, voltage, percentage;
1359
1360 x86_cpuid(0x80860000, descs);
1361 nreg = descs[0];
1362 if (nreg >= 0x80860001) {
1363 x86_cpuid(0x80860001, descs);
1364 aprint_verbose_dev(ci->ci_dev, "Processor revision %u.%u.%u.%u\n",
1365 (descs[1] >> 24) & 0xff,
1366 (descs[1] >> 16) & 0xff,
1367 (descs[1] >> 8) & 0xff,
1368 descs[1] & 0xff);
1369 }
1370 if (nreg >= 0x80860002) {
1371 x86_cpuid(0x80860002, descs);
1372 aprint_verbose_dev(ci->ci_dev, "Code Morphing Software Rev: %u.%u.%u-%u-%u\n",
1373 (descs[1] >> 24) & 0xff,
1374 (descs[1] >> 16) & 0xff,
1375 (descs[1] >> 8) & 0xff,
1376 descs[1] & 0xff,
1377 descs[2]);
1378 }
1379 if (nreg >= 0x80860006) {
1380 union {
1381 char text[65];
1382 u_int descs[4][4];
1383 } info;
1384 int i;
1385
1386 for (i=0; i<4; i++) {
1387 x86_cpuid(0x80860003 + i, info.descs[i]);
1388 }
1389 info.text[64] = '\0';
1390 aprint_verbose_dev(ci->ci_dev, "%s\n", info.text);
1391 }
1392
1393 if (nreg >= 0x80860007) {
1394 tmx86_get_longrun_status(&frequency,
1395 &voltage, &percentage);
1396 aprint_verbose_dev(ci->ci_dev, "LongRun <%dMHz %dmV %d%%>\n",
1397 frequency, voltage, percentage);
1398 }
1399 }
1400
1401 static void
1402 cpu_probe_base_features(struct cpu_info *ci, const char *cpuname)
1403 {
1404 u_int descs[4];
1405 int i;
1406 uint32_t brand[12];
1407
1408 memset(ci, 0, sizeof(*ci));
1409 ci->ci_dev = cpuname;
1410
1411 ci->ci_cpu_type = x86_identify();
1412 if (ci->ci_cpu_type >= 0) {
1413 /* Old pre-cpuid instruction cpu */
1414 ci->ci_cpuid_level = -1;
1415 return;
1416 }
1417
1418 /*
1419 * This CPU supports cpuid instruction, so we can call x86_cpuid()
1420 * function.
1421 */
1422
1423 /*
1424 * Fn0000_0000:
1425 * - Save cpuid max level.
1426 * - Save vendor string.
1427 */
1428 x86_cpuid(0, descs);
1429 ci->ci_cpuid_level = descs[0];
1430 /* Save vendor string */
1431 ci->ci_vendor[0] = descs[1];
1432 ci->ci_vendor[2] = descs[2];
1433 ci->ci_vendor[1] = descs[3];
1434 ci->ci_vendor[3] = 0;
1435
1436 /*
1437 * Fn8000_0000:
1438 * - Get cpuid extended function's max level.
1439 */
1440 x86_cpuid(0x80000000, descs);
1441 if (descs[0] >= 0x80000000)
1442 ci->ci_cpuid_extlevel = descs[0];
1443 else {
1444 /* Set lower value than 0x80000000 */
1445 ci->ci_cpuid_extlevel = 0;
1446 }
1447
1448 /*
1449 * Fn8000_000[2-4]:
1450 * - Save brand string.
1451 */
1452 if (ci->ci_cpuid_extlevel >= 0x80000004) {
1453 x86_cpuid(0x80000002, brand);
1454 x86_cpuid(0x80000003, brand + 4);
1455 x86_cpuid(0x80000004, brand + 8);
1456 for (i = 0; i < 48; i++)
1457 if (((char *) brand)[i] != ' ')
1458 break;
1459 memcpy(cpu_brand_string, ((char *) brand) + i, 48 - i);
1460 }
1461
1462 if (ci->ci_cpuid_level < 1)
1463 return;
1464
1465 /*
1466 * Fn0000_0001:
1467 * - Get CPU family, model and stepping (from eax).
1468 * - Initial local APIC ID and brand ID (from ebx)
1469 * - CPUID2 (from ecx)
1470 * - CPUID (from edx)
1471 */
1472 x86_cpuid(1, descs);
1473 ci->ci_signature = descs[0];
1474
1475 /* Extract full family/model values */
1476 ci->ci_family = CPUID_TO_FAMILY(ci->ci_signature);
1477 ci->ci_model = CPUID_TO_MODEL(ci->ci_signature);
1478
1479 /* Brand is low order 8 bits of ebx */
1480 ci->ci_brand_id = descs[1] & 0xff;
1481 /* Initial local APIC ID */
1482 ci->ci_initapicid = (descs[1] >> 24) & 0xff;
1483
1484 ci->ci_feat_val[1] = descs[2];
1485 ci->ci_feat_val[0] = descs[3];
1486
1487 if (ci->ci_cpuid_level < 3)
1488 return;
1489
1490 /*
1491 * If the processor serial number misfeature is present and supported,
1492 * extract it here.
1493 */
1494 if ((ci->ci_feat_val[0] & CPUID_PN) != 0) {
1495 ci->ci_cpu_serial[0] = ci->ci_signature;
1496 x86_cpuid(3, descs);
1497 ci->ci_cpu_serial[2] = descs[2];
1498 ci->ci_cpu_serial[1] = descs[3];
1499 }
1500
1501 if (ci->ci_cpuid_level < 0xd)
1502 return;
1503
1504 /* Get support XCR0 bits */
1505 x86_cpuid2(0xd, 0, descs);
1506 ci->ci_feat_val[5] = descs[0]; /* Actually 64 bits */
1507 ci->ci_cur_xsave = descs[1];
1508 ci->ci_max_xsave = descs[2];
1509
1510 /* Additional flags (eg xsaveopt support) */
1511 x86_cpuid2(0xd, 1, descs);
1512 ci->ci_feat_val[6] = descs[0]; /* Actually 64 bits */
1513 }
1514
1515 static void
1516 cpu_probe_features(struct cpu_info *ci)
1517 {
1518 const struct cpu_cpuid_nameclass *cpup = NULL;
1519 unsigned int i;
1520
1521 if (ci->ci_cpuid_level < 1)
1522 return;
1523
1524 for (i = 0; i < __arraycount(i386_cpuid_cpus); i++) {
1525 if (!strncmp((char *)ci->ci_vendor,
1526 i386_cpuid_cpus[i].cpu_id, 12)) {
1527 cpup = &i386_cpuid_cpus[i];
1528 break;
1529 }
1530 }
1531
1532 if (cpup == NULL)
1533 return;
1534
1535 i = ci->ci_family - CPU_MINFAMILY;
1536
1537 if (i >= __arraycount(cpup->cpu_family))
1538 i = __arraycount(cpup->cpu_family) - 1;
1539
1540 if (cpup->cpu_family[i].cpu_probe == NULL)
1541 return;
1542
1543 (*cpup->cpu_family[i].cpu_probe)(ci);
1544 }
1545
1546 static void
1547 print_bits(const char *cpuname, const char *hdr, const char *fmt, uint32_t val)
1548 {
1549 char buf[32 * 16];
1550 char *bp;
1551
1552 #define MAX_LINE_LEN 79 /* get from command arg or 'stty cols' ? */
1553
1554 if (val == 0 || fmt == NULL)
1555 return;
1556
1557 snprintb_m(buf, sizeof(buf), fmt, val,
1558 MAX_LINE_LEN - strlen(cpuname) - 2 - strlen(hdr) - 1);
1559 bp = buf;
1560 while (*bp != '\0') {
1561 aprint_verbose("%s: %s %s\n", cpuname, hdr, bp);
1562 bp += strlen(bp) + 1;
1563 }
1564 }
1565
1566 static void
1567 identifycpu_cpuids(struct cpu_info *ci)
1568 {
1569 const char *cpuname = ci->ci_dev;
1570 u_int lp_max = 1; /* logical processors per package */
1571 u_int smt_max; /* smt per core */
1572 u_int core_max = 1; /* core per package */
1573 u_int smt_bits, core_bits;
1574 uint32_t descs[4];
1575 uint32_t highest_basic_info;
1576
1577 aprint_verbose("%s: Initial APIC ID %u\n", cpuname, ci->ci_initapicid);
1578 ci->ci_packageid = ci->ci_initapicid;
1579 ci->ci_coreid = 0;
1580 ci->ci_smtid = 0;
1581 if (cpu_vendor != CPUVENDOR_INTEL) {
1582 return;
1583 }
1584
1585 /*
1586 * 253668.pdf 7.10.2
1587 */
1588
1589 if ((ci->ci_feat_val[0] & CPUID_HTT) != 0) {
1590 x86_cpuid(1, descs);
1591 lp_max = (descs[1] >> 16) & 0xff;
1592 }
1593 x86_cpuid(0, descs);
1594 highest_basic_info = descs[0];
1595 if (highest_basic_info >= 4) {
1596 x86_cpuid2(4, 0, descs);
1597 core_max = (descs[0] >> 26) + 1;
1598 }
1599 assert(lp_max >= core_max);
1600 smt_max = lp_max / core_max;
1601 smt_bits = ilog2(smt_max - 1) + 1;
1602 core_bits = ilog2(core_max - 1) + 1;
1603 if (smt_bits + core_bits) {
1604 ci->ci_packageid = ci->ci_initapicid >> (smt_bits + core_bits);
1605 }
1606 aprint_verbose("%s: Cluster/Package ID %u\n", cpuname,
1607 ci->ci_packageid);
1608 if (core_bits) {
1609 u_int core_mask = __BITS(smt_bits, smt_bits + core_bits - 1);
1610
1611 ci->ci_coreid =
1612 __SHIFTOUT(ci->ci_initapicid, core_mask);
1613 aprint_verbose("%s: Core ID %u\n", cpuname, ci->ci_coreid);
1614 }
1615 if (smt_bits) {
1616 u_int smt_mask = __BITS((int)0, (int)(smt_bits - 1));
1617
1618 ci->ci_smtid = __SHIFTOUT(ci->ci_initapicid, smt_mask);
1619 aprint_verbose("%s: SMT ID %u\n", cpuname, ci->ci_smtid);
1620 }
1621 }
1622
1623 void
1624 identifycpu(int fd, const char *cpuname)
1625 {
1626 const char *name = "", *modifier, *vendorname, *brand = "";
1627 int class = CPUCLASS_386;
1628 unsigned int i;
1629 int modif, family;
1630 const struct cpu_cpuid_nameclass *cpup = NULL;
1631 const struct cpu_cpuid_family *cpufam;
1632 struct cpu_info *ci, cistore;
1633 size_t sz;
1634 struct cpu_ucode_version ucode;
1635 union {
1636 struct cpu_ucode_version_amd amd;
1637 struct cpu_ucode_version_intel1 intel1;
1638 } ucvers;
1639
1640 ci = &cistore;
1641 cpu_probe_base_features(ci, cpuname);
1642 cpu_probe_features(ci);
1643
1644 if (ci->ci_cpu_type >= 0) {
1645 /* Old pre-cpuid instruction cpu */
1646 if (ci->ci_cpu_type >= (int)__arraycount(i386_nocpuid_cpus))
1647 errx(1, "unknown cpu type %d", ci->ci_cpu_type);
1648 name = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_name;
1649 cpu_vendor = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_vendor;
1650 vendorname = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_vendorname;
1651 class = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_class;
1652 ci->ci_info = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_info;
1653 modifier = "";
1654 } else {
1655 /* CPU which support cpuid instruction */
1656 modif = (ci->ci_signature >> 12) & 0x3;
1657 family = ci->ci_family;
1658 if (family < CPU_MINFAMILY)
1659 errx(1, "identifycpu: strange family value");
1660 if (family > CPU_MAXFAMILY)
1661 family = CPU_MAXFAMILY;
1662
1663 for (i = 0; i < __arraycount(i386_cpuid_cpus); i++) {
1664 if (!strncmp((char *)ci->ci_vendor,
1665 i386_cpuid_cpus[i].cpu_id, 12)) {
1666 cpup = &i386_cpuid_cpus[i];
1667 break;
1668 }
1669 }
1670
1671 if (cpup == NULL) {
1672 cpu_vendor = CPUVENDOR_UNKNOWN;
1673 if (ci->ci_vendor[0] != '\0')
1674 vendorname = (char *)&ci->ci_vendor[0];
1675 else
1676 vendorname = "Unknown";
1677 class = family - 3;
1678 modifier = "";
1679 name = "";
1680 ci->ci_info = NULL;
1681 } else {
1682 cpu_vendor = cpup->cpu_vendor;
1683 vendorname = cpup->cpu_vendorname;
1684 modifier = modifiers[modif];
1685 cpufam = &cpup->cpu_family[family - CPU_MINFAMILY];
1686 name = cpufam->cpu_models[ci->ci_model];
1687 if (name == NULL || *name == '\0')
1688 name = cpufam->cpu_model_default;
1689 class = cpufam->cpu_class;
1690 ci->ci_info = cpufam->cpu_info;
1691
1692 if (cpu_vendor == CPUVENDOR_INTEL) {
1693 if (ci->ci_family == 6 && ci->ci_model >= 5) {
1694 const char *tmp;
1695 tmp = intel_family6_name(ci);
1696 if (tmp != NULL)
1697 name = tmp;
1698 }
1699 if (ci->ci_family == 15 &&
1700 ci->ci_brand_id <
1701 __arraycount(i386_intel_brand) &&
1702 i386_intel_brand[ci->ci_brand_id])
1703 name =
1704 i386_intel_brand[ci->ci_brand_id];
1705 }
1706
1707 if (cpu_vendor == CPUVENDOR_AMD) {
1708 if (ci->ci_family == 6 && ci->ci_model >= 6) {
1709 if (ci->ci_brand_id == 1)
1710 /*
1711 * It's Duron. We override the
1712 * name, since it might have
1713 * been misidentified as Athlon.
1714 */
1715 name =
1716 amd_brand[ci->ci_brand_id];
1717 else
1718 brand = amd_brand_name;
1719 }
1720 if (CPUID_TO_BASEFAMILY(ci->ci_signature)
1721 == 0xf) {
1722 /* Identify AMD64 CPU names. */
1723 const char *tmp;
1724 tmp = amd_amd64_name(ci);
1725 if (tmp != NULL)
1726 name = tmp;
1727 }
1728 }
1729
1730 if (cpu_vendor == CPUVENDOR_IDT && ci->ci_family >= 6)
1731 vendorname = "VIA";
1732 }
1733 }
1734
1735 ci->ci_cpu_class = class;
1736
1737 sz = sizeof(ci->ci_tsc_freq);
1738 (void)sysctlbyname("machdep.tsc_freq", &ci->ci_tsc_freq, &sz, NULL, 0);
1739 sz = sizeof(use_pae);
1740 (void)sysctlbyname("machdep.pae", &use_pae, &sz, NULL, 0);
1741 largepagesize = (use_pae ? 2 * 1024 * 1024 : 4 * 1024 * 1024);
1742
1743 /*
1744 * The 'cpu_brand_string' is much more useful than the 'cpu_model'
1745 * we try to determine from the family/model values.
1746 */
1747 if (*cpu_brand_string != '\0')
1748 aprint_normal("%s: \"%s\"\n", cpuname, cpu_brand_string);
1749
1750 aprint_normal("%s: %s", cpuname, vendorname);
1751 if (*modifier)
1752 aprint_normal(" %s", modifier);
1753 if (*name)
1754 aprint_normal(" %s", name);
1755 if (*brand)
1756 aprint_normal(" %s", brand);
1757 aprint_normal(" (%s-class)", classnames[class]);
1758
1759 if (ci->ci_tsc_freq != 0)
1760 aprint_normal(", %ju.%02ju MHz\n",
1761 ((uintmax_t)ci->ci_tsc_freq + 4999) / 1000000,
1762 (((uintmax_t)ci->ci_tsc_freq + 4999) / 10000) % 100);
1763
1764 aprint_normal_dev(ci->ci_dev, "family %#x model %#x stepping %#x",
1765 ci->ci_family, ci->ci_model, CPUID_TO_STEPPING(ci->ci_signature));
1766 if (ci->ci_signature != 0)
1767 aprint_normal(" (id %#x)", ci->ci_signature);
1768 aprint_normal("\n");
1769
1770 if (ci->ci_info)
1771 (*ci->ci_info)(ci);
1772
1773 /*
1774 * display CPU feature flags
1775 */
1776
1777 print_bits(cpuname, "features", CPUID_FLAGS1, ci->ci_feat_val[0]);
1778 print_bits(cpuname, "features1", CPUID2_FLAGS1, ci->ci_feat_val[1]);
1779
1780 /* These next two are actually common definitions! */
1781 print_bits(cpuname, "features2",
1782 cpu_vendor == CPUVENDOR_INTEL ? CPUID_INTEL_EXT_FLAGS
1783 : CPUID_EXT_FLAGS, ci->ci_feat_val[2]);
1784 print_bits(cpuname, "features3",
1785 cpu_vendor == CPUVENDOR_INTEL ? CPUID_INTEL_FLAGS4
1786 : CPUID_AMD_FLAGS4, ci->ci_feat_val[3]);
1787
1788 print_bits(cpuname, "padloack features", CPUID_FLAGS_PADLOCK,
1789 ci->ci_feat_val[4]);
1790
1791 print_bits(cpuname, "xsave features", XCR0_FLAGS1, ci->ci_feat_val[5]);
1792 print_bits(cpuname, "xsave instructions", CPUID_PES1_FLAGS,
1793 ci->ci_feat_val[6]);
1794
1795 if (ci->ci_max_xsave != 0) {
1796 aprint_normal("%s: xsave area size: current %d, maximum %d",
1797 cpuname, ci->ci_cur_xsave, ci->ci_max_xsave);
1798 aprint_normal(", xgetbv %sabled\n",
1799 ci->ci_feat_val[1] & CPUID2_OSXSAVE ? "en" : "dis");
1800 if (ci->ci_feat_val[1] & CPUID2_OSXSAVE)
1801 print_bits(cpuname, "enabled xsave", XCR0_FLAGS1,
1802 x86_xgetbv());
1803 }
1804
1805 x86_print_cacheinfo(ci);
1806
1807 if (ci->ci_cpuid_level >= 3 && (ci->ci_feat_val[0] & CPUID_PN)) {
1808 aprint_verbose("%s: serial number %04X-%04X-%04X-%04X-%04X-%04X\n",
1809 cpuname,
1810 ci->ci_cpu_serial[0] / 65536, ci->ci_cpu_serial[0] % 65536,
1811 ci->ci_cpu_serial[1] / 65536, ci->ci_cpu_serial[1] % 65536,
1812 ci->ci_cpu_serial[2] / 65536, ci->ci_cpu_serial[2] % 65536);
1813 }
1814
1815 if (ci->ci_cpu_class == CPUCLASS_386) {
1816 errx(1, "NetBSD requires an 80486 or later processor");
1817 }
1818
1819 if (ci->ci_cpu_type == CPU_486DLC) {
1820 #ifndef CYRIX_CACHE_WORKS
1821 aprint_error("WARNING: CYRIX 486DLC CACHE UNCHANGED.\n");
1822 #else
1823 #ifndef CYRIX_CACHE_REALLY_WORKS
1824 aprint_error("WARNING: CYRIX 486DLC CACHE ENABLED IN HOLD-FLUSH MODE.\n");
1825 #else
1826 aprint_error("WARNING: CYRIX 486DLC CACHE ENABLED.\n");
1827 #endif
1828 #endif
1829 }
1830
1831 /*
1832 * Everything past this point requires a Pentium or later.
1833 */
1834 if (ci->ci_cpuid_level < 0)
1835 return;
1836
1837 identifycpu_cpuids(ci);
1838
1839 #ifdef INTEL_CORETEMP
1840 if (cpu_vendor == CPUVENDOR_INTEL && ci->ci_cpuid_level >= 0x06)
1841 coretemp_register(ci);
1842 #endif
1843
1844 if (cpu_vendor == CPUVENDOR_AMD) {
1845 uint32_t data[4];
1846
1847 x86_cpuid(0x80000000, data);
1848 if (data[0] >= 0x80000007)
1849 powernow_probe(ci);
1850
1851 if ((data[0] >= 0x8000000a)
1852 && (ci->ci_feat_val[3] & CPUID_SVM) != 0) {
1853 x86_cpuid(0x8000000a, data);
1854 aprint_verbose("%s: SVM Rev. %d\n", cpuname,
1855 data[0] & 0xf);
1856 aprint_verbose("%s: SVM NASID %d\n", cpuname, data[1]);
1857 print_bits(cpuname, "SVM features", CPUID_AMD_SVM_FLAGS,
1858 data[3]);
1859 }
1860 } else if (cpu_vendor == CPUVENDOR_INTEL) {
1861 uint32_t data[4];
1862 uint32_t highest_basic_info;
1863 uint32_t bi_index;
1864
1865 x86_cpuid(0x00000000, data);
1866 highest_basic_info = data[0];
1867 aprint_verbose("%s: highest basic info %08x\n", cpuname,
1868 highest_basic_info);
1869 for (bi_index = 1; bi_index <= highest_basic_info; bi_index++) {
1870 x86_cpuid(bi_index, data);
1871 switch (bi_index) {
1872 case 6:
1873 print_bits(cpuname, "DSPM-eax",
1874 CPUID_DSPM_FLAGS, data[0]);
1875 print_bits(cpuname, "DSPM-ecx",
1876 CPUID_DSPM_FLAGS1, data[2]);
1877 break;
1878 case 7:
1879 aprint_verbose("%s: SEF highest subleaf %08x\n",
1880 cpuname, data[0]);
1881 print_bits(cpuname, "SEF-main", CPUID_SEF_FLAGS,
1882 data[1]);
1883 break;
1884 #if 0
1885 default:
1886 aprint_verbose("%s: basic %08x-eax %08x\n",
1887 cpuname, bi_index, data[0]);
1888 aprint_verbose("%s: basic %08x-ebx %08x\n",
1889 cpuname, bi_index, data[1]);
1890 aprint_verbose("%s: basic %08x-ecx %08x\n",
1891 cpuname, bi_index, data[2]);
1892 aprint_verbose("%s: basic %08x-edx %08x\n",
1893 cpuname, bi_index, data[3]);
1894 break;
1895 #endif
1896 }
1897 }
1898 }
1899
1900 #ifdef INTEL_ONDEMAND_CLOCKMOD
1901 clockmod_init();
1902 #endif
1903
1904 if (cpu_vendor == CPUVENDOR_AMD)
1905 ucode.loader_version = CPU_UCODE_LOADER_AMD;
1906 else if (cpu_vendor == CPUVENDOR_INTEL)
1907 ucode.loader_version = CPU_UCODE_LOADER_INTEL1;
1908 else
1909 return;
1910
1911 ucode.data = &ucvers;
1912 if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &ucode) < 0) {
1913 #ifdef __i386__
1914 struct cpu_ucode_version_64 ucode_64;
1915 if (errno != ENOTTY)
1916 return;
1917 /* Try the 64 bit ioctl */
1918 memset(&ucode_64, 0, sizeof ucode_64);
1919 ucode_64.data = &ucvers;
1920 ucode_64.loader_version = ucode.loader_version;
1921 if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION_64, &ucode_64) < 0)
1922 return;
1923 #endif
1924 }
1925
1926 if (cpu_vendor == CPUVENDOR_AMD)
1927 printf("%s: UCode version: 0x%"PRIx64"\n", cpuname, ucvers.amd.version);
1928 else if (cpu_vendor == CPUVENDOR_INTEL)
1929 printf("%s: microcode version 0x%x, platform ID %d\n", cpuname,
1930 ucvers.intel1.ucodeversion, ucvers.intel1.platformid);
1931 }
1932
1933 static const char *
1934 print_cache_config(struct cpu_info *ci, int cache_tag, const char *name,
1935 const char *sep)
1936 {
1937 struct x86_cache_info *cai = &ci->ci_cinfo[cache_tag];
1938 char human_num[HUMAN_BUFSIZE];
1939
1940 if (cai->cai_totalsize == 0)
1941 return sep;
1942
1943 if (sep == NULL)
1944 aprint_verbose_dev(ci->ci_dev, "");
1945 else
1946 aprint_verbose("%s", sep);
1947 if (name != NULL)
1948 aprint_verbose("%s ", name);
1949
1950 if (cai->cai_string != NULL) {
1951 aprint_verbose("%s ", cai->cai_string);
1952 } else {
1953 (void)humanize_number(human_num, sizeof(human_num),
1954 cai->cai_totalsize, "B", HN_AUTOSCALE, HN_NOSPACE);
1955 aprint_verbose("%s %dB/line ", human_num, cai->cai_linesize);
1956 }
1957 switch (cai->cai_associativity) {
1958 case 0:
1959 aprint_verbose("disabled");
1960 break;
1961 case 1:
1962 aprint_verbose("direct-mapped");
1963 break;
1964 case 0xff:
1965 aprint_verbose("fully associative");
1966 break;
1967 default:
1968 aprint_verbose("%d-way", cai->cai_associativity);
1969 break;
1970 }
1971 return ", ";
1972 }
1973
1974 static const char *
1975 print_tlb_config(struct cpu_info *ci, int cache_tag, const char *name,
1976 const char *sep)
1977 {
1978 struct x86_cache_info *cai = &ci->ci_cinfo[cache_tag];
1979 char human_num[HUMAN_BUFSIZE];
1980
1981 if (cai->cai_totalsize == 0)
1982 return sep;
1983
1984 if (sep == NULL)
1985 aprint_verbose_dev(ci->ci_dev, "");
1986 else
1987 aprint_verbose("%s", sep);
1988 if (name != NULL)
1989 aprint_verbose("%s ", name);
1990
1991 if (cai->cai_string != NULL) {
1992 aprint_verbose("%s", cai->cai_string);
1993 } else {
1994 (void)humanize_number(human_num, sizeof(human_num),
1995 cai->cai_linesize, "B", HN_AUTOSCALE, HN_NOSPACE);
1996 aprint_verbose("%d %s entries ", cai->cai_totalsize,
1997 human_num);
1998 switch (cai->cai_associativity) {
1999 case 0:
2000 aprint_verbose("disabled");
2001 break;
2002 case 1:
2003 aprint_verbose("direct-mapped");
2004 break;
2005 case 0xff:
2006 aprint_verbose("fully associative");
2007 break;
2008 default:
2009 aprint_verbose("%d-way", cai->cai_associativity);
2010 break;
2011 }
2012 }
2013 return ", ";
2014 }
2015
2016 static const struct x86_cache_info *
2017 cache_info_lookup(const struct x86_cache_info *cai, uint8_t desc)
2018 {
2019 int i;
2020
2021 for (i = 0; cai[i].cai_desc != 0; i++) {
2022 if (cai[i].cai_desc == desc)
2023 return (&cai[i]);
2024 }
2025
2026 return (NULL);
2027 }
2028
2029 static void
2030 x86_print_cacheinfo(struct cpu_info *ci)
2031 {
2032 const char *sep = NULL;
2033
2034 if (ci->ci_cinfo[CAI_ICACHE].cai_totalsize != 0 ||
2035 ci->ci_cinfo[CAI_DCACHE].cai_totalsize != 0) {
2036 sep = print_cache_config(ci, CAI_ICACHE, "I-cache", NULL);
2037 sep = print_cache_config(ci, CAI_DCACHE, "D-cache", sep);
2038 if (sep != NULL)
2039 aprint_verbose("\n");
2040 }
2041 if (ci->ci_cinfo[CAI_L2CACHE].cai_totalsize != 0) {
2042 sep = print_cache_config(ci, CAI_L2CACHE, "L2 cache", NULL);
2043 if (sep != NULL)
2044 aprint_verbose("\n");
2045 }
2046 if (ci->ci_cinfo[CAI_L3CACHE].cai_totalsize != 0) {
2047 sep = print_cache_config(ci, CAI_L3CACHE, "L3 cache", NULL);
2048 if (sep != NULL)
2049 aprint_verbose("\n");
2050 }
2051 if (ci->ci_cinfo[CAI_PREFETCH].cai_linesize != 0) {
2052 aprint_verbose_dev(ci->ci_dev, "%dB prefetching",
2053 ci->ci_cinfo[CAI_PREFETCH].cai_linesize);
2054 if (sep != NULL)
2055 aprint_verbose("\n");
2056 }
2057 if (ci->ci_cinfo[CAI_ITLB].cai_totalsize != 0) {
2058 sep = print_tlb_config(ci, CAI_ITLB, "ITLB", NULL);
2059 sep = print_tlb_config(ci, CAI_ITLB2, NULL, sep);
2060 if (sep != NULL)
2061 aprint_verbose("\n");
2062 }
2063 if (ci->ci_cinfo[CAI_DTLB].cai_totalsize != 0) {
2064 sep = print_tlb_config(ci, CAI_DTLB, "DTLB", NULL);
2065 sep = print_tlb_config(ci, CAI_DTLB2, NULL, sep);
2066 if (sep != NULL)
2067 aprint_verbose("\n");
2068 }
2069 if (ci->ci_cinfo[CAI_L2_ITLB].cai_totalsize != 0) {
2070 sep = print_tlb_config(ci, CAI_L2_ITLB, "L2 ITLB", NULL);
2071 sep = print_tlb_config(ci, CAI_L2_ITLB2, NULL, sep);
2072 if (sep != NULL)
2073 aprint_verbose("\n");
2074 }
2075 if (ci->ci_cinfo[CAI_L2_DTLB].cai_totalsize != 0) {
2076 sep = print_tlb_config(ci, CAI_L2_DTLB, "L2 DTLB", NULL);
2077 sep = print_tlb_config(ci, CAI_L2_DTLB2, NULL, sep);
2078 if (sep != NULL)
2079 aprint_verbose("\n");
2080 }
2081 if (ci->ci_cinfo[CAI_L2_STLB].cai_totalsize != 0) {
2082 sep = print_tlb_config(ci, CAI_L2_STLB, "L2 STLB", NULL);
2083 sep = print_tlb_config(ci, CAI_L2_STLB2, NULL, sep);
2084 if (sep != NULL)
2085 aprint_verbose("\n");
2086 }
2087 if (ci->ci_cinfo[CAI_L1_1GBITLB].cai_totalsize != 0) {
2088 sep = print_tlb_config(ci, CAI_L1_1GBITLB, "L1 1GB page ITLB",
2089 NULL);
2090 if (sep != NULL)
2091 aprint_verbose("\n");
2092 }
2093 if (ci->ci_cinfo[CAI_L1_1GBDTLB].cai_totalsize != 0) {
2094 sep = print_tlb_config(ci, CAI_L1_1GBDTLB, "L1 1GB page DTLB",
2095 NULL);
2096 if (sep != NULL)
2097 aprint_verbose("\n");
2098 }
2099 if (ci->ci_cinfo[CAI_L2_1GBITLB].cai_totalsize != 0) {
2100 sep = print_tlb_config(ci, CAI_L2_1GBITLB, "L2 1GB page ITLB",
2101 NULL);
2102 if (sep != NULL)
2103 aprint_verbose("\n");
2104 }
2105 if (ci->ci_cinfo[CAI_L2_1GBDTLB].cai_totalsize != 0) {
2106 sep = print_tlb_config(ci, CAI_L2_1GBDTLB, "L2 1GB page DTLB",
2107 NULL);
2108 if (sep != NULL)
2109 aprint_verbose("\n");
2110 }
2111 }
2112
2113 static void
2114 powernow_probe(struct cpu_info *ci)
2115 {
2116 uint32_t regs[4];
2117 char buf[256];
2118
2119 x86_cpuid(0x80000007, regs);
2120
2121 snprintb(buf, sizeof(buf), CPUID_APM_FLAGS, regs[3]);
2122 aprint_normal_dev(ci->ci_dev, "AMD Power Management features: %s\n",
2123 buf);
2124 }
2125
2126 int
2127 ucodeupdate_check(int fd, struct cpu_ucode *uc)
2128 {
2129 struct cpu_info ci;
2130 int loader_version, res;
2131 struct cpu_ucode_version versreq;
2132
2133 cpu_probe_base_features(&ci, "unknown");
2134
2135 if (!strcmp((char *)ci.ci_vendor, "AuthenticAMD"))
2136 loader_version = CPU_UCODE_LOADER_AMD;
2137 else if (!strcmp((char *)ci.ci_vendor, "GenuineIntel"))
2138 loader_version = CPU_UCODE_LOADER_INTEL1;
2139 else
2140 return -1;
2141
2142 /* check whether the kernel understands this loader version */
2143 versreq.loader_version = loader_version;
2144 versreq.data = 0;
2145 res = ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &versreq);
2146 if (res)
2147 return -1;
2148
2149 switch (loader_version) {
2150 case CPU_UCODE_LOADER_AMD:
2151 if (uc->cpu_nr != -1) {
2152 /* printf? */
2153 return -1;
2154 }
2155 uc->cpu_nr = CPU_UCODE_ALL_CPUS;
2156 break;
2157 case CPU_UCODE_LOADER_INTEL1:
2158 if (uc->cpu_nr == -1)
2159 uc->cpu_nr = CPU_UCODE_ALL_CPUS; /* for Xen */
2160 else
2161 uc->cpu_nr = CPU_UCODE_CURRENT_CPU;
2162 break;
2163 default: /* can't happen */
2164 return -1;
2165 }
2166 uc->loader_version = loader_version;
2167 return 0;
2168 }
2169