2 * Copyright (c) KATO Takenori, 1997, 1998.
4 * All rights reserved. Unpublished rights reserved under the copyright
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/sysctl.h>
41 #include <machine/cputypes.h>
42 #include <machine/md_var.h>
43 #include <machine/specialreg.h>
48 static int hw_instruction_sse;
49 SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD,
50 &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU");
52 * -1: automatic (default)
53 * 0: keep enable CLFLUSH
54 * 1: force disable CLFLUSH
56 static int hw_clflush_disable = -1;
64 * Work around Erratum 721 for Family 10h and 12h processors.
65 * These processors may incorrectly update the stack pointer
66 * after a long series of push and/or near-call instructions,
67 * or a long series of pop and/or near-return instructions.
69 * http://support.amd.com/us/Processor_TechDocs/41322_10h_Rev_Gd.pdf
70 * http://support.amd.com/us/Processor_TechDocs/44739_12h_Rev_Gd.pdf
72 * Hypervisors do not provide access to the errata MSR,
73 * causing #GP exception on attempt to apply the errata. The
74 * MSR write shall be done on host and persist globally
75 * anyway, so do not try to do it when under virtualization.
77 switch (CPUID_TO_FAMILY(cpu_id)) {
80 if ((cpu_feature2 & CPUID2_HV) == 0)
81 wrmsr(0xc0011029, rdmsr(0xc0011029) | 1);
86 * BIOS may fail to set InitApicIdCpuIdLo to 1 as it should per BKDG.
87 * So, do it here or otherwise some tools could be confused by
88 * Initial Local APIC ID reported with CPUID Function 1 in EBX.
90 if (CPUID_TO_FAMILY(cpu_id) == 0x10) {
91 if ((cpu_feature2 & CPUID2_HV) == 0) {
92 msr = rdmsr(MSR_NB_CFG1);
93 msr |= (uint64_t)1 << 54;
94 wrmsr(MSR_NB_CFG1, msr);
99 * BIOS may configure Family 10h processors to convert WC+ cache type
100 * to CD. That can hurt performance of guest VMs using nested paging.
101 * The relevant MSR bit is not documented in the BKDG,
102 * the fix is borrowed from Linux.
104 if (CPUID_TO_FAMILY(cpu_id) == 0x10) {
105 if ((cpu_feature2 & CPUID2_HV) == 0) {
106 msr = rdmsr(0xc001102a);
107 msr &= ~((uint64_t)1 << 24);
108 wrmsr(0xc001102a, msr);
113 * Work around Erratum 793: Specific Combination of Writes to Write
114 * Combined Memory Types and Locked Instructions May Cause Core Hang.
115 * See Revision Guide for AMD Family 16h Models 00h-0Fh Processors,
116 * revision 3.04 or later, publication 51810.
118 if (CPUID_TO_FAMILY(cpu_id) == 0x16 && CPUID_TO_MODEL(cpu_id) <= 0xf) {
119 if ((cpu_feature2 & CPUID2_HV) == 0) {
120 msr = rdmsr(0xc0011020);
121 msr |= (uint64_t)1 << 15;
122 wrmsr(0xc0011020, msr);
128 * Initialize special VIA features
136 * Check extended CPUID for PadLock features.
138 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
140 do_cpuid(0xc0000000, regs);
141 if (regs[0] >= 0xc0000001) {
142 do_cpuid(0xc0000001, regs);
147 /* Enable RNG if present. */
148 if ((val & VIA_CPUID_HAS_RNG) != 0) {
149 via_feature_rng = VIA_HAS_RNG;
150 wrmsr(0x110B, rdmsr(0x110B) | VIA_CPUID_DO_RNG);
153 /* Enable PadLock if present. */
154 if ((val & VIA_CPUID_HAS_ACE) != 0)
155 via_feature_xcrypt |= VIA_HAS_AES;
156 if ((val & VIA_CPUID_HAS_ACE2) != 0)
157 via_feature_xcrypt |= VIA_HAS_AESCTR;
158 if ((val & VIA_CPUID_HAS_PHE) != 0)
159 via_feature_xcrypt |= VIA_HAS_SHA;
160 if ((val & VIA_CPUID_HAS_PMM) != 0)
161 via_feature_xcrypt |= VIA_HAS_MM;
162 if (via_feature_xcrypt != 0)
163 wrmsr(0x1107, rdmsr(0x1107) | (1 << 28));
167 * Initialize CPU control registers
176 if ((cpu_feature & CPUID_XMM) && (cpu_feature & CPUID_FXSR)) {
177 cr4 |= CR4_FXSR | CR4_XMM;
178 cpu_fxsr = hw_instruction_sse = 1;
180 if (cpu_stdext_feature & CPUID_STDEXT_FSGSBASE)
184 * Postpone enabling the SMEP on the boot CPU until the page
185 * tables are switched from the boot loader identity mapping
186 * to the kernel tables. The boot loader enables the U bit in
189 if (!IS_BSP() && (cpu_stdext_feature & CPUID_STDEXT_SMEP))
192 if ((amd_feature & AMDID_NX) != 0) {
193 msr = rdmsr(MSR_EFER) | EFER_NXE;
194 wrmsr(MSR_EFER, msr);
197 switch (cpu_vendor_id) {
201 case CPU_VENDOR_CENTAUR:
208 initializecpucache(void)
212 * CPUID with %eax = 1, %ebx returns
213 * Bits 15-8: CLFLUSH line size
214 * (Value * 8 = cache line size in bytes)
216 if ((cpu_feature & CPUID_CLFSH) != 0)
217 cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8;
219 * XXXKIB: (temporary) hack to work around traps generated
220 * when CLFLUSHing APIC register window under virtualization
221 * environments. These environments tend to disable the
222 * CPUID_SS feature even though the native CPU supports it.
224 TUNABLE_INT_FETCH("hw.clflush_disable", &hw_clflush_disable);
225 if (vm_guest != VM_GUEST_NO && hw_clflush_disable == -1) {
226 cpu_feature &= ~CPUID_CLFSH;
227 cpu_stdext_feature &= ~CPUID_STDEXT_CLFLUSHOPT;
231 * The kernel's use of CLFLUSH{,OPT} can be disabled manually
232 * by setting the hw.clflush_disable tunable.
234 if (hw_clflush_disable == 1) {
235 cpu_feature &= ~CPUID_CLFSH;
236 cpu_stdext_feature &= ~CPUID_STDEXT_CLFLUSHOPT;