2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4 * Copyright (c) 1997 KATO Takenori.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Id: machdep.c,v 1.193 1996/06/18 01:22:04 bde Exp
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
49 #include <sys/eventhandler.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53 #include <sys/power.h>
55 #include <machine/asmacros.h>
56 #include <machine/clock.h>
57 #include <machine/cputypes.h>
58 #include <machine/frame.h>
59 #include <machine/intr_machdep.h>
60 #include <machine/segments.h>
61 #include <machine/specialreg.h>
62 #include <machine/md_var.h>
64 #include <x86/isa/icu.h>
66 /* XXX - should be in header file: */
67 void printcpuinfo(void);
68 void identify_cpu(void);
69 void earlysetcpuclass(void);
70 void panicifcpuunsupported(void);
72 static u_int find_cpu_vendor_id(void);
73 static void print_AMD_info(void);
74 static void print_AMD_assoc(int i);
75 static void print_via_padlock_info(void);
78 char machine[] = "amd64";
81 extern int adaptive_machine_arch;
85 sysctl_hw_machine(SYSCTL_HANDLER_ARGS)
88 static const char machine32[] = "i386";
93 if ((req->flags & SCTL_MASK32) != 0 && adaptive_machine_arch)
94 error = SYSCTL_OUT(req, machine32, sizeof(machine32));
97 error = SYSCTL_OUT(req, machine, sizeof(machine));
101 SYSCTL_PROC(_hw, HW_MACHINE, machine, CTLTYPE_STRING | CTLFLAG_RD,
102 NULL, 0, sysctl_hw_machine, "A", "Machine class");
104 static char cpu_model[128];
105 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD,
106 cpu_model, 0, "Machine model");
108 static int hw_clockrate;
109 SYSCTL_INT(_hw, OID_AUTO, clockrate, CTLFLAG_RD,
110 &hw_clockrate, 0, "CPU instruction clock rate");
112 static eventhandler_tag tsc_post_tag;
114 static char cpu_brand[48];
120 { "Clawhammer", CPUCLASS_K8 }, /* CPU_CLAWHAMMER */
121 { "Sledgehammer", CPUCLASS_K8 }, /* CPU_SLEDGEHAMMER */
128 { INTEL_VENDOR_ID, CPU_VENDOR_INTEL }, /* GenuineIntel */
129 { AMD_VENDOR_ID, CPU_VENDOR_AMD }, /* AuthenticAMD */
130 { CENTAUR_VENDOR_ID, CPU_VENDOR_CENTAUR }, /* CentaurHauls */
140 cpu_class = amd64_cpus[cpu].cpu_class;
142 strncpy(cpu_model, amd64_cpus[cpu].cpu_name, sizeof (cpu_model));
144 /* Check for extended CPUID information and a processor name. */
145 if (cpu_exthigh >= 0x80000004) {
147 for (i = 0x80000002; i < 0x80000005; i++) {
149 memcpy(brand, regs, sizeof(regs));
150 brand += sizeof(regs);
154 switch (cpu_vendor_id) {
155 case CPU_VENDOR_INTEL:
156 /* Please make up your mind folks! */
157 strcat(cpu_model, "EM64T");
161 * Values taken from AMD Processor Recognition
162 * http://www.amd.com/K6/k6docs/pdf/20734g.pdf
163 * (also describes ``Features'' encodings.
165 strcpy(cpu_model, "AMD ");
166 if ((cpu_id & 0xf00) == 0xf00)
167 strcat(cpu_model, "AMD64 Processor");
169 strcat(cpu_model, "Unknown");
171 case CPU_VENDOR_CENTAUR:
172 strcpy(cpu_model, "VIA ");
173 if ((cpu_id & 0xff0) == 0x6f0)
174 strcat(cpu_model, "Nano Processor");
176 strcat(cpu_model, "Unknown");
179 strcat(cpu_model, "Unknown");
184 * Replace cpu_model with cpu_brand minus leading spaces if
188 while (*brand == ' ')
191 strcpy(cpu_model, brand);
193 printf("%s (", cpu_model);
197 hw_clockrate = (tsc_freq + 5000) / 1000000;
198 printf("%jd.%02d-MHz ",
199 (intmax_t)(tsc_freq + 4999) / 1000000,
200 (u_int)((tsc_freq + 4999) / 10000) % 100);
205 printf("Unknown"); /* will panic below... */
207 printf("-class CPU)\n");
209 printf(" Origin = \"%s\"", cpu_vendor);
211 printf(" Id = 0x%x", cpu_id);
213 if (cpu_vendor_id == CPU_VENDOR_INTEL ||
214 cpu_vendor_id == CPU_VENDOR_AMD ||
215 cpu_vendor_id == CPU_VENDOR_CENTAUR) {
216 printf(" Family = %x", CPUID_TO_FAMILY(cpu_id));
217 printf(" Model = %x", CPUID_TO_MODEL(cpu_id));
218 printf(" Stepping = %u", cpu_id & CPUID_STEPPING);
221 * AMD CPUID Specification
222 * http://support.amd.com/us/Embedded_TechDocs/25481.pdf
224 * Intel Processor Identification and CPUID Instruction
225 * http://www.intel.com/assets/pdf/appnote/241618.pdf
230 * Here we should probably set up flags indicating
231 * whether or not various features are available.
232 * The interesting ones are probably VME, PSE, PAE,
233 * and PGE. The code already assumes without bothering
234 * to check that all CPUs >= Pentium have a TSC and
237 printf("\n Features=0x%b", cpu_feature,
239 "\001FPU" /* Integral FPU */
240 "\002VME" /* Extended VM86 mode support */
241 "\003DE" /* Debugging Extensions (CR4.DE) */
242 "\004PSE" /* 4MByte page tables */
243 "\005TSC" /* Timestamp counter */
244 "\006MSR" /* Machine specific registers */
245 "\007PAE" /* Physical address extension */
246 "\010MCE" /* Machine Check support */
247 "\011CX8" /* CMPEXCH8 instruction */
248 "\012APIC" /* SMP local APIC */
249 "\013oldMTRR" /* Previous implementation of MTRR */
250 "\014SEP" /* Fast System Call */
251 "\015MTRR" /* Memory Type Range Registers */
252 "\016PGE" /* PG_G (global bit) support */
253 "\017MCA" /* Machine Check Architecture */
254 "\020CMOV" /* CMOV instruction */
255 "\021PAT" /* Page attributes table */
256 "\022PSE36" /* 36 bit address space support */
257 "\023PN" /* Processor Serial number */
258 "\024CLFLUSH" /* Has the CLFLUSH instruction */
260 "\026DTS" /* Debug Trace Store */
261 "\027ACPI" /* ACPI support */
262 "\030MMX" /* MMX instructions */
263 "\031FXSR" /* FXSAVE/FXRSTOR */
264 "\032SSE" /* Streaming SIMD Extensions */
265 "\033SSE2" /* Streaming SIMD Extensions #2 */
266 "\034SS" /* Self snoop */
267 "\035HTT" /* Hyperthreading (see EBX bit 16-23) */
268 "\036TM" /* Thermal Monitor clock slowdown */
269 "\037IA64" /* CPU can execute IA64 instructions */
270 "\040PBE" /* Pending Break Enable */
273 if (cpu_feature2 != 0) {
274 printf("\n Features2=0x%b", cpu_feature2,
276 "\001SSE3" /* SSE3 */
277 "\002PCLMULQDQ" /* Carry-Less Mul Quadword */
278 "\003DTES64" /* 64-bit Debug Trace */
279 "\004MON" /* MONITOR/MWAIT Instructions */
280 "\005DS_CPL" /* CPL Qualified Debug Store */
281 "\006VMX" /* Virtual Machine Extensions */
282 "\007SMX" /* Safer Mode Extensions */
283 "\010EST" /* Enhanced SpeedStep */
284 "\011TM2" /* Thermal Monitor 2 */
285 "\012SSSE3" /* SSSE3 */
286 "\013CNXT-ID" /* L1 context ID available */
288 "\015FMA" /* Fused Multiply Add */
289 "\016CX16" /* CMPXCHG16B Instruction */
290 "\017xTPR" /* Send Task Priority Messages*/
291 "\020PDCM" /* Perf/Debug Capability MSR */
293 "\022PCID" /* Process-context Identifiers*/
294 "\023DCA" /* Direct Cache Access */
295 "\024SSE4.1" /* SSE 4.1 */
296 "\025SSE4.2" /* SSE 4.2 */
297 "\026x2APIC" /* xAPIC Extensions */
298 "\027MOVBE" /* MOVBE Instruction */
299 "\030POPCNT" /* POPCNT Instruction */
300 "\031TSCDLT" /* TSC-Deadline Timer */
301 "\032AESNI" /* AES Crypto */
302 "\033XSAVE" /* XSAVE/XRSTOR States */
303 "\034OSXSAVE" /* OS-Enabled State Management*/
304 "\035AVX" /* Advanced Vector Extensions */
305 "\036F16C" /* Half-precision conversions */
306 "\037RDRAND" /* RDRAND Instruction */
307 "\040HV" /* Hypervisor */
311 if (amd_feature != 0) {
312 printf("\n AMD Features=0x%b", amd_feature,
314 "\001<s0>" /* Same */
315 "\002<s1>" /* Same */
316 "\003<s2>" /* Same */
317 "\004<s3>" /* Same */
318 "\005<s4>" /* Same */
319 "\006<s5>" /* Same */
320 "\007<s6>" /* Same */
321 "\010<s7>" /* Same */
322 "\011<s8>" /* Same */
323 "\012<s9>" /* Same */
324 "\013<b10>" /* Undefined */
325 "\014SYSCALL" /* Have SYSCALL/SYSRET */
326 "\015<s12>" /* Same */
327 "\016<s13>" /* Same */
328 "\017<s14>" /* Same */
329 "\020<s15>" /* Same */
330 "\021<s16>" /* Same */
331 "\022<s17>" /* Same */
332 "\023<b18>" /* Reserved, unknown */
333 "\024MP" /* Multiprocessor Capable */
334 "\025NX" /* Has EFER.NXE, NX */
335 "\026<b21>" /* Undefined */
336 "\027MMX+" /* AMD MMX Extensions */
337 "\030<s23>" /* Same */
338 "\031<s24>" /* Same */
339 "\032FFXSR" /* Fast FXSAVE/FXRSTOR */
340 "\033Page1GB" /* 1-GB large page support */
341 "\034RDTSCP" /* RDTSCP */
342 "\035<b28>" /* Undefined */
343 "\036LM" /* 64 bit long mode */
344 "\0373DNow!+" /* AMD 3DNow! Extensions */
345 "\0403DNow!" /* AMD 3DNow! */
349 if (amd_feature2 != 0) {
350 printf("\n AMD Features2=0x%b", amd_feature2,
352 "\001LAHF" /* LAHF/SAHF in long mode */
353 "\002CMP" /* CMP legacy */
354 "\003SVM" /* Secure Virtual Mode */
355 "\004ExtAPIC" /* Extended APIC register */
356 "\005CR8" /* CR8 in legacy mode */
357 "\006ABM" /* LZCNT instruction */
358 "\007SSE4A" /* SSE4A */
359 "\010MAS" /* Misaligned SSE mode */
360 "\011Prefetch" /* 3DNow! Prefetch/PrefetchW */
361 "\012OSVW" /* OS visible workaround */
362 "\013IBS" /* Instruction based sampling */
363 "\014XOP" /* XOP extended instructions */
364 "\015SKINIT" /* SKINIT/STGI */
365 "\016WDT" /* Watchdog timer */
367 "\020LWP" /* Lightweight Profiling */
368 "\021FMA4" /* 4-operand FMA instructions */
371 "\024NodeId" /* NodeId MSR support */
373 "\026TBM" /* Trailing Bit Manipulation */
374 "\027Topology" /* Topology Extensions */
387 if (via_feature_rng != 0 || via_feature_xcrypt != 0)
388 print_via_padlock_info();
390 if ((cpu_feature & CPUID_HTT) &&
391 cpu_vendor_id == CPU_VENDOR_AMD)
392 cpu_feature &= ~CPUID_HTT;
395 * If this CPU supports P-state invariant TSC then
396 * mention the capability.
398 if (tsc_is_invariant) {
399 printf("\n TSC: P-state invariant");
401 printf(", performance statistics");
406 /* Avoid ugly blank lines: only print newline when we have to. */
407 if (*cpu_vendor || cpu_id)
413 if (cpu_vendor_id == CPU_VENDOR_AMD)
418 panicifcpuunsupported(void)
422 #error "You need to specify a cpu type"
425 * Now that we have told the user what they have,
426 * let them know if that machine type isn't configured.
433 panic("CPU class not configured");
440 /* Update TSC freq with the value indicated by the caller. */
442 tsc_freq_changed(void *arg __unused, const struct cf_level *level, int status)
445 /* If there was an error during the transition, don't do anything. */
449 /* Total setting for this level gives the new frequency in MHz. */
450 hw_clockrate = level->total_set.freq;
454 hook_tsc_freq(void *arg __unused)
457 if (tsc_is_invariant)
460 tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
461 tsc_freq_changed, NULL, EVENTHANDLER_PRI_ANY);
464 SYSINIT(hook_tsc_freq, SI_SUB_CONFIGURE, SI_ORDER_ANY, hook_tsc_freq, NULL);
467 * Final stage of CPU identification.
476 ((u_int *)&cpu_vendor)[0] = regs[1];
477 ((u_int *)&cpu_vendor)[1] = regs[3];
478 ((u_int *)&cpu_vendor)[2] = regs[2];
479 cpu_vendor[12] = '\0';
480 cpu_vendor_id = find_cpu_vendor_id();
484 cpu_procinfo = regs[1];
485 cpu_feature = regs[3];
486 cpu_feature2 = regs[2];
489 * Clear "Limit CPUID Maxval" bit and get the largest standard CPUID
490 * function number again if it is set from BIOS. It is necessary
491 * for probing correct CPU topology later.
492 * XXX This is only done on the BSP package.
494 if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_high > 0 && cpu_high < 4) {
496 msr = rdmsr(MSR_IA32_MISC_ENABLE);
497 if ((msr & 0x400000ULL) != 0) {
498 wrmsr(MSR_IA32_MISC_ENABLE, msr & ~0x400000ULL);
504 if (cpu_vendor_id == CPU_VENDOR_INTEL ||
505 cpu_vendor_id == CPU_VENDOR_AMD ||
506 cpu_vendor_id == CPU_VENDOR_CENTAUR) {
507 do_cpuid(0x80000000, regs);
508 cpu_exthigh = regs[0];
510 if (cpu_exthigh >= 0x80000001) {
511 do_cpuid(0x80000001, regs);
512 amd_feature = regs[3] & ~(cpu_feature & 0x0183f3ff);
513 amd_feature2 = regs[2];
515 if (cpu_exthigh >= 0x80000007) {
516 do_cpuid(0x80000007, regs);
517 amd_pminfo = regs[3];
519 if (cpu_exthigh >= 0x80000008) {
520 do_cpuid(0x80000008, regs);
521 cpu_procinfo2 = regs[2];
525 cpu = CPU_CLAWHAMMER;
529 find_cpu_vendor_id(void)
533 for (i = 0; i < sizeof(cpu_vendors) / sizeof(cpu_vendors[0]); i++)
534 if (strcmp(cpu_vendor, cpu_vendors[i].vendor) == 0)
535 return (cpu_vendors[i].vendor_id);
540 print_AMD_assoc(int i)
543 printf(", fully associative\n");
545 printf(", %d-way associative\n", i);
549 print_AMD_l2_assoc(int i)
552 case 0: printf(", disabled/not present\n"); break;
553 case 1: printf(", direct mapped\n"); break;
554 case 2: printf(", 2-way associative\n"); break;
555 case 4: printf(", 4-way associative\n"); break;
556 case 6: printf(", 8-way associative\n"); break;
557 case 8: printf(", 16-way associative\n"); break;
558 case 15: printf(", fully associative\n"); break;
559 default: printf(", reserved configuration\n"); break;
568 if (cpu_exthigh < 0x80000005)
571 do_cpuid(0x80000005, regs);
572 printf("L1 2MB data TLB: %d entries", (regs[0] >> 16) & 0xff);
573 print_AMD_assoc(regs[0] >> 24);
575 printf("L1 2MB instruction TLB: %d entries", regs[0] & 0xff);
576 print_AMD_assoc((regs[0] >> 8) & 0xff);
578 printf("L1 4KB data TLB: %d entries", (regs[1] >> 16) & 0xff);
579 print_AMD_assoc(regs[1] >> 24);
581 printf("L1 4KB instruction TLB: %d entries", regs[1] & 0xff);
582 print_AMD_assoc((regs[1] >> 8) & 0xff);
584 printf("L1 data cache: %d kbytes", regs[2] >> 24);
585 printf(", %d bytes/line", regs[2] & 0xff);
586 printf(", %d lines/tag", (regs[2] >> 8) & 0xff);
587 print_AMD_assoc((regs[2] >> 16) & 0xff);
589 printf("L1 instruction cache: %d kbytes", regs[3] >> 24);
590 printf(", %d bytes/line", regs[3] & 0xff);
591 printf(", %d lines/tag", (regs[3] >> 8) & 0xff);
592 print_AMD_assoc((regs[3] >> 16) & 0xff);
594 if (cpu_exthigh >= 0x80000006) {
595 do_cpuid(0x80000006, regs);
596 if ((regs[0] >> 16) != 0) {
597 printf("L2 2MB data TLB: %d entries",
598 (regs[0] >> 16) & 0xfff);
599 print_AMD_l2_assoc(regs[0] >> 28);
600 printf("L2 2MB instruction TLB: %d entries",
602 print_AMD_l2_assoc((regs[0] >> 28) & 0xf);
604 printf("L2 2MB unified TLB: %d entries",
606 print_AMD_l2_assoc((regs[0] >> 28) & 0xf);
608 if ((regs[1] >> 16) != 0) {
609 printf("L2 4KB data TLB: %d entries",
610 (regs[1] >> 16) & 0xfff);
611 print_AMD_l2_assoc(regs[1] >> 28);
613 printf("L2 4KB instruction TLB: %d entries",
614 (regs[1] >> 16) & 0xfff);
615 print_AMD_l2_assoc((regs[1] >> 28) & 0xf);
617 printf("L2 4KB unified TLB: %d entries",
618 (regs[1] >> 16) & 0xfff);
619 print_AMD_l2_assoc((regs[1] >> 28) & 0xf);
621 printf("L2 unified cache: %d kbytes", regs[2] >> 16);
622 printf(", %d bytes/line", regs[2] & 0xff);
623 printf(", %d lines/tag", (regs[2] >> 8) & 0x0f);
624 print_AMD_l2_assoc((regs[2] >> 12) & 0x0f);
628 * Opteron Rev E shows a bug as in very rare occasions a read memory
629 * barrier is not performed as expected if it is followed by a
630 * non-atomic read-modify-write instruction.
631 * As long as that bug pops up very rarely (intensive machine usage
632 * on other operating systems generally generates one unexplainable
633 * crash any 2 months) and as long as a model specific fix would be
634 * impratical at this stage, print out a warning string if the broken
635 * model and family are identified.
637 if (CPUID_TO_FAMILY(cpu_id) == 0xf && CPUID_TO_MODEL(cpu_id) >= 0x20 &&
638 CPUID_TO_MODEL(cpu_id) <= 0x3f)
639 printf("WARNING: This architecture revision has known SMP "
640 "hardware bugs which may cause random instability\n");
644 print_via_padlock_info(void)
648 do_cpuid(0xc0000001, regs);
649 printf("\n VIA Padlock Features=0x%b", regs[3],
653 "\011AES-CTR" /* ACE2 */
654 "\013SHA1,SHA256" /* PHE */