xref: /dflybsd-src/sys/platform/pc64/x86_64/initcpu.c (revision 45dd33f25a21807f2e759f0267b1faf6c6581700)
1b2b3ffcdSSimon Schubert /*-
2b2b3ffcdSSimon Schubert  * Copyright (c) KATO Takenori, 1997, 1998.
3b2b3ffcdSSimon Schubert  * Copyright (c) 2008 The DragonFly Project.
4b2b3ffcdSSimon Schubert  *
5b2b3ffcdSSimon Schubert  * All rights reserved.  Unpublished rights reserved under the copyright
6b2b3ffcdSSimon Schubert  * laws of Japan.
7b2b3ffcdSSimon Schubert  *
8b2b3ffcdSSimon Schubert  * Redistribution and use in source and binary forms, with or without
9b2b3ffcdSSimon Schubert  * modification, are permitted provided that the following conditions
10b2b3ffcdSSimon Schubert  * are met:
11b2b3ffcdSSimon Schubert  *
12b2b3ffcdSSimon Schubert  * 1. Redistributions of source code must retain the above copyright
13b2b3ffcdSSimon Schubert  *    notice, this list of conditions and the following disclaimer as
14b2b3ffcdSSimon Schubert  *    the first lines of this file unmodified.
15b2b3ffcdSSimon Schubert  * 2. Redistributions in binary form must reproduce the above copyright
16b2b3ffcdSSimon Schubert  *    notice, this list of conditions and the following disclaimer in the
17b2b3ffcdSSimon Schubert  *    documentation and/or other materials provided with the distribution.
18b2b3ffcdSSimon Schubert  *
19b2b3ffcdSSimon Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20b2b3ffcdSSimon Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21b2b3ffcdSSimon Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22b2b3ffcdSSimon Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23b2b3ffcdSSimon Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24b2b3ffcdSSimon Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b2b3ffcdSSimon Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b2b3ffcdSSimon Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b2b3ffcdSSimon Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28b2b3ffcdSSimon Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b2b3ffcdSSimon Schubert  */
30b2b3ffcdSSimon Schubert 
31b2b3ffcdSSimon Schubert #include "opt_cpu.h"
32b2b3ffcdSSimon Schubert 
33b2b3ffcdSSimon Schubert #include <sys/param.h>
34b2b3ffcdSSimon Schubert #include <sys/kernel.h>
35b2b3ffcdSSimon Schubert #include <sys/systm.h>
36b2b3ffcdSSimon Schubert #include <sys/sysctl.h>
37b2b3ffcdSSimon Schubert 
38db2ec6f8SSascha Wildner #include <machine/clock.h>
39b2b3ffcdSSimon Schubert #include <machine/cputypes.h>
40b2b3ffcdSSimon Schubert #include <machine/md_var.h>
41b2b3ffcdSSimon Schubert #include <machine/specialreg.h>
42b2b3ffcdSSimon Schubert #include <machine/smp.h>
43b2b3ffcdSSimon Schubert 
44b2b3ffcdSSimon Schubert #include <vm/vm.h>
45b2b3ffcdSSimon Schubert #include <vm/pmap.h>
46b2b3ffcdSSimon Schubert 
478b257adbSImre Vadász static int tsc_ignore_cpuid = 0;
488b257adbSImre Vadász TUNABLE_INT("hw.tsc_ignore_cpuid", &tsc_ignore_cpuid);
498b257adbSImre Vadász 
50b2b3ffcdSSimon Schubert static int	hw_instruction_sse;
51b2b3ffcdSSimon Schubert SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD,
52b2b3ffcdSSimon Schubert     &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU");
53b2b3ffcdSSimon Schubert 
546a01f94dSzrj int	cpu_type;		/* XXX CPU_CLAWHAMMER */
55b2b3ffcdSSimon Schubert u_int	cpu_feature;		/* Feature flags */
56b2b3ffcdSSimon Schubert u_int	cpu_feature2;		/* Feature flags */
57b2b3ffcdSSimon Schubert u_int	amd_feature;		/* AMD feature flags */
58b2b3ffcdSSimon Schubert u_int	amd_feature2;		/* AMD feature flags */
59b2b3ffcdSSimon Schubert u_int	via_feature_rng;	/* VIA RNG features */
60b2b3ffcdSSimon Schubert u_int	via_feature_xcrypt;	/* VIA ACE features */
61b2b3ffcdSSimon Schubert u_int	cpu_high;		/* Highest arg to CPUID */
62b2b3ffcdSSimon Schubert u_int	cpu_exthigh;		/* Highest arg to extended CPUID */
63b2b3ffcdSSimon Schubert u_int	cpu_id;			/* Stepping ID */
64b2b3ffcdSSimon Schubert u_int	cpu_procinfo;		/* HyperThreading Info / Brand Index / CLFUSH */
65b2b3ffcdSSimon Schubert u_int	cpu_procinfo2;		/* Multicore info */
66b2b3ffcdSSimon Schubert char	cpu_vendor[20];		/* CPU Origin code */
67b2b3ffcdSSimon Schubert u_int	cpu_vendor_id;		/* CPU vendor ID */
68b2b3ffcdSSimon Schubert u_int	cpu_fxsr;		/* SSE enabled */
697c656f7bSMatthew Dillon u_int	cpu_xsave;		/* Using XSAVE */
7008771751SVenkatesh Srinivas u_int	cpu_clflush_line_size = 32;	/* Default CLFLUSH line size */
712431688fSSascha Wildner u_int	cpu_stdext_feature;
72d7883524SMatthew Dillon u_int	cpu_stdext_feature2;
73d7883524SMatthew Dillon u_int	cpu_stdext_feature3;
74d7883524SMatthew Dillon u_long	cpu_ia32_arch_caps;
75e6dc2aa9SSepherosa Ziehau u_int	cpu_thermal_feature;
765e4f4f11SSepherosa Ziehau u_int	cpu_mwait_feature;
771f9bc256SSepherosa Ziehau u_int	cpu_mwait_extemu;
7808771751SVenkatesh Srinivas 
7908771751SVenkatesh Srinivas /*
8008771751SVenkatesh Srinivas  * -1: automatic (enable on h/w, disable on VMs)
8108771751SVenkatesh Srinivas  * 0: disable
8208771751SVenkatesh Srinivas  * 1: enable (where available)
8308771751SVenkatesh Srinivas  */
8408771751SVenkatesh Srinivas static int hw_clflush_enable = -1;
8508771751SVenkatesh Srinivas 
8608771751SVenkatesh Srinivas SYSCTL_INT(_hw, OID_AUTO, clflush_enable, CTLFLAG_RD, &hw_clflush_enable, 0,
8708771751SVenkatesh Srinivas 	   "");
88b2b3ffcdSSimon Schubert 
89b2b3ffcdSSimon Schubert SYSCTL_UINT(_hw, OID_AUTO, via_feature_rng, CTLFLAG_RD,
90b2b3ffcdSSimon Schubert 	&via_feature_rng, 0, "VIA C3/C7 RNG feature available in CPU");
91b2b3ffcdSSimon Schubert SYSCTL_UINT(_hw, OID_AUTO, via_feature_xcrypt, CTLFLAG_RD,
92b2b3ffcdSSimon Schubert 	&via_feature_xcrypt, 0, "VIA C3/C7 xcrypt feature available in CPU");
93b2b3ffcdSSimon Schubert 
94b2b3ffcdSSimon Schubert /*
95b2b3ffcdSSimon Schubert  * Initialize special VIA C3/C7 features
96b2b3ffcdSSimon Schubert  */
97b2b3ffcdSSimon Schubert static void
init_via(void)98b2b3ffcdSSimon Schubert init_via(void)
99b2b3ffcdSSimon Schubert {
100b2b3ffcdSSimon Schubert 	u_int regs[4], val;
101b2b3ffcdSSimon Schubert 	u_int64_t msreg;
102b2b3ffcdSSimon Schubert 
103b2b3ffcdSSimon Schubert 	do_cpuid(0xc0000000, regs);
104b2b3ffcdSSimon Schubert 	val = regs[0];
105b2b3ffcdSSimon Schubert 	if (val >= 0xc0000001) {
106b2b3ffcdSSimon Schubert 		do_cpuid(0xc0000001, regs);
107b2b3ffcdSSimon Schubert 		val = regs[3];
108b2b3ffcdSSimon Schubert 	} else
109b2b3ffcdSSimon Schubert 		val = 0;
110b2b3ffcdSSimon Schubert 
111b2b3ffcdSSimon Schubert 	/* Enable RNG if present and disabled */
112b2b3ffcdSSimon Schubert 	if (val & VIA_CPUID_HAS_RNG) {
113b2b3ffcdSSimon Schubert 		if (!(val & VIA_CPUID_DO_RNG)) {
114b2b3ffcdSSimon Schubert 			msreg = rdmsr(0x110B);
115b2b3ffcdSSimon Schubert 			msreg |= 0x40;
116b2b3ffcdSSimon Schubert 			wrmsr(0x110B, msreg);
117b2b3ffcdSSimon Schubert 		}
118b2b3ffcdSSimon Schubert 		via_feature_rng = VIA_HAS_RNG;
119b2b3ffcdSSimon Schubert 	}
120b2b3ffcdSSimon Schubert 	/* Enable AES engine if present and disabled */
121b2b3ffcdSSimon Schubert 	if (val & VIA_CPUID_HAS_ACE) {
122b2b3ffcdSSimon Schubert 		if (!(val & VIA_CPUID_DO_ACE)) {
123b2b3ffcdSSimon Schubert 			msreg = rdmsr(0x1107);
124b2b3ffcdSSimon Schubert 			msreg |= (0x01 << 28);
125b2b3ffcdSSimon Schubert 			wrmsr(0x1107, msreg);
126b2b3ffcdSSimon Schubert 		}
127b2b3ffcdSSimon Schubert 		via_feature_xcrypt |= VIA_HAS_AES;
128b2b3ffcdSSimon Schubert 	}
129b2b3ffcdSSimon Schubert 	/* Enable ACE2 engine if present and disabled */
130b2b3ffcdSSimon Schubert 	if (val & VIA_CPUID_HAS_ACE2) {
131b2b3ffcdSSimon Schubert 		if (!(val & VIA_CPUID_DO_ACE2)) {
132b2b3ffcdSSimon Schubert 			msreg = rdmsr(0x1107);
133b2b3ffcdSSimon Schubert 			msreg |= (0x01 << 28);
134b2b3ffcdSSimon Schubert 			wrmsr(0x1107, msreg);
135b2b3ffcdSSimon Schubert 		}
136b2b3ffcdSSimon Schubert 		via_feature_xcrypt |= VIA_HAS_AESCTR;
137b2b3ffcdSSimon Schubert 	}
138b2b3ffcdSSimon Schubert 	/* Enable SHA engine if present and disabled */
139b2b3ffcdSSimon Schubert 	if (val & VIA_CPUID_HAS_PHE) {
140b2b3ffcdSSimon Schubert 		if (!(val & VIA_CPUID_DO_PHE)) {
141b2b3ffcdSSimon Schubert 			msreg = rdmsr(0x1107);
142b2b3ffcdSSimon Schubert 			msreg |= (0x01 << 28/**/);
143b2b3ffcdSSimon Schubert 			wrmsr(0x1107, msreg);
144b2b3ffcdSSimon Schubert 		}
145b2b3ffcdSSimon Schubert 		via_feature_xcrypt |= VIA_HAS_SHA;
146b2b3ffcdSSimon Schubert 	}
147b2b3ffcdSSimon Schubert 	/* Enable MM engine if present and disabled */
148b2b3ffcdSSimon Schubert 	if (val & VIA_CPUID_HAS_PMM) {
149b2b3ffcdSSimon Schubert 		if (!(val & VIA_CPUID_DO_PMM)) {
150b2b3ffcdSSimon Schubert 			msreg = rdmsr(0x1107);
151b2b3ffcdSSimon Schubert 			msreg |= (0x01 << 28/**/);
152b2b3ffcdSSimon Schubert 			wrmsr(0x1107, msreg);
153b2b3ffcdSSimon Schubert 		}
154b2b3ffcdSSimon Schubert 		via_feature_xcrypt |= VIA_HAS_MM;
155b2b3ffcdSSimon Schubert 	}
156b2b3ffcdSSimon Schubert }
157b2b3ffcdSSimon Schubert 
158d26086c2SSepherosa Ziehau static enum vmm_guest_type
detect_vmm(void)159d26086c2SSepherosa Ziehau detect_vmm(void)
160d26086c2SSepherosa Ziehau {
161d26086c2SSepherosa Ziehau 	enum vmm_guest_type guest;
16212e11217SImre Vadász 	char vendor[16];
163d26086c2SSepherosa Ziehau 
164d26086c2SSepherosa Ziehau 	/*
165d26086c2SSepherosa Ziehau 	 * [RFC] CPUID usage for interaction between Hypervisors and Linux.
166d26086c2SSepherosa Ziehau 	 * http://lkml.org/lkml/2008/10/1/246
167d26086c2SSepherosa Ziehau 	 *
168d26086c2SSepherosa Ziehau 	 * KB1009458: Mechanisms to determine if software is running in
169d26086c2SSepherosa Ziehau 	 * a VMware virtual machine
170d26086c2SSepherosa Ziehau 	 * http://kb.vmware.com/kb/1009458
171d26086c2SSepherosa Ziehau 	 */
172d26086c2SSepherosa Ziehau 	if (cpu_feature2 & CPUID2_VMM) {
173d26086c2SSepherosa Ziehau 		u_int regs[4];
174d26086c2SSepherosa Ziehau 
175d26086c2SSepherosa Ziehau 		do_cpuid(0x40000000, regs);
17612e11217SImre Vadász 		((u_int *)&vendor)[0] = regs[1];
17712e11217SImre Vadász 		((u_int *)&vendor)[1] = regs[2];
17812e11217SImre Vadász 		((u_int *)&vendor)[2] = regs[3];
17912e11217SImre Vadász 		vendor[12] = '\0';
180d26086c2SSepherosa Ziehau 		if (regs[0] >= 0x40000000) {
18112e11217SImre Vadász 			memcpy(vmm_vendor, vendor, 13);
182d26086c2SSepherosa Ziehau 			if (strcmp(vmm_vendor, "VMwareVMware") == 0)
183d26086c2SSepherosa Ziehau 				return VMM_GUEST_VMWARE;
184d26086c2SSepherosa Ziehau 			else if (strcmp(vmm_vendor, "Microsoft Hv") == 0)
185d26086c2SSepherosa Ziehau 				return VMM_GUEST_HYPERV;
18612e11217SImre Vadász 			else if (strcmp(vmm_vendor, "KVMKVMKVM") == 0)
18712e11217SImre Vadász 				return VMM_GUEST_KVM;
188*45dd33f2SAaron LI 			else if (strcmp(vmm_vendor, "___ NVMM ___") == 0)
189*45dd33f2SAaron LI 				return VMM_GUEST_NVMM;
19012e11217SImre Vadász 		} else if (regs[0] == 0) {
19112e11217SImre Vadász 			/* Also detect old KVM versions with regs[0] == 0 */
19212e11217SImre Vadász 			if (strcmp(vendor, "KVMKVMKVM") == 0) {
19312e11217SImre Vadász 				memcpy(vmm_vendor, vendor, 13);
19412e11217SImre Vadász 				return VMM_GUEST_KVM;
19512e11217SImre Vadász 			}
196d26086c2SSepherosa Ziehau 		}
197d26086c2SSepherosa Ziehau 	}
198d26086c2SSepherosa Ziehau 
199d26086c2SSepherosa Ziehau 	guest = detect_virtual();
200d26086c2SSepherosa Ziehau 	if (guest == VMM_GUEST_NONE && (cpu_feature2 & CPUID2_VMM))
201d26086c2SSepherosa Ziehau 		guest = VMM_GUEST_UNKNOWN;
202d26086c2SSepherosa Ziehau 	return guest;
203d26086c2SSepherosa Ziehau }
204d26086c2SSepherosa Ziehau 
205b2b3ffcdSSimon Schubert /*
206b2b3ffcdSSimon Schubert  * Initialize CPU control registers
207b2b3ffcdSSimon Schubert  */
208b2b3ffcdSSimon Schubert void
initializecpu(int cpu)20920a6d9dbSMatthew Dillon initializecpu(int cpu)
210b2b3ffcdSSimon Schubert {
211b2b3ffcdSSimon Schubert 	uint64_t msr;
212b2b3ffcdSSimon Schubert 
21348c77f2bSMatthew Dillon 	/*
21448c77f2bSMatthew Dillon 	 * Check for FXSR and SSE support and enable if available
21548c77f2bSMatthew Dillon 	 */
2169463bdafSAaron LI 	if ((cpu_feature & CPUID_SSE) && (cpu_feature & CPUID_FXSR)) {
217fb3360aeSAaron LI 		load_cr4(rcr4() | CR4_OSFXSR | CR4_OSXMMEXCPT);
218b2b3ffcdSSimon Schubert 		cpu_fxsr = hw_instruction_sse = 1;
219b2b3ffcdSSimon Schubert 	}
220e43754b8SMatthew Dillon 
2215a1223c1SSascha Wildner 	if (cpu == 0) {
22288416a26SMatthew Dillon 		/* Check if we are running in a hypervisor. */
223d26086c2SSepherosa Ziehau 		vmm_guest = detect_vmm();
2245a1223c1SSascha Wildner 	}
22588416a26SMatthew Dillon 
2265cf56a8dSAlex Hornung #if !defined(CPU_DISABLE_AVX)
2277c656f7bSMatthew Dillon 	/* Use XSAVE if supported */
2287c656f7bSMatthew Dillon 	if (cpu_feature2 & CPUID2_XSAVE) {
229fb3360aeSAaron LI 		load_cr4(rcr4() | CR4_OSXSAVE);
2305cf56a8dSAlex Hornung 
2315cf56a8dSAlex Hornung 		/* Adjust size of savefpu in npx.h before adding to mask.*/
2327c656f7bSMatthew Dillon 		npx_xcr0_mask = CPU_XFEATURE_X87 | CPU_XFEATURE_SSE;
2337c656f7bSMatthew Dillon 		if (cpu_feature2 & CPUID2_AVX)
2347c656f7bSMatthew Dillon 			npx_xcr0_mask |= CPU_XFEATURE_YMM;
2357c656f7bSMatthew Dillon 
23623bf518bSAaron LI 		load_xcr(0, npx_xcr0_mask);
2375cf56a8dSAlex Hornung 		cpu_xsave = 1;
2385cf56a8dSAlex Hornung 	}
2395cf56a8dSAlex Hornung #endif
2405cf56a8dSAlex Hornung 
241afea4d5aSMatthew Dillon 	if (cpu_vendor_id == CPU_VENDOR_AMD) {
242afea4d5aSMatthew Dillon 		switch((cpu_id & 0xFF0000)) {
243afea4d5aSMatthew Dillon 		case 0x100000:
244afea4d5aSMatthew Dillon 		case 0x120000:
245afea4d5aSMatthew Dillon 			/*
246afea4d5aSMatthew Dillon 			 * Errata 721 is the cpu bug found by your's truly
247afea4d5aSMatthew Dillon 			 * (Matthew Dillon).  It is a bug where a sequence
248afea4d5aSMatthew Dillon 			 * of 5 or more popq's + a retq, under involved
249afea4d5aSMatthew Dillon 			 * deep recursion circumstances, can cause the %rsp
250afea4d5aSMatthew Dillon 			 * to not be properly updated, almost always
251afea4d5aSMatthew Dillon 			 * resulting in a seg-fault soon after.
25288416a26SMatthew Dillon 			 *
25388416a26SMatthew Dillon 			 * Do not install the workaround when we are running
25488416a26SMatthew Dillon 			 * in a virtual machine.
255afea4d5aSMatthew Dillon 			 */
25688416a26SMatthew Dillon 			if (vmm_guest)
25788416a26SMatthew Dillon 				break;
25888416a26SMatthew Dillon 
25988416a26SMatthew Dillon 			msr = rdmsr(MSR_AMD_DE_CFG);
260afea4d5aSMatthew Dillon 			if ((msr & 1) == 0) {
26120a6d9dbSMatthew Dillon 				if (cpu == 0)
26220a6d9dbSMatthew Dillon 					kprintf("Errata 721 workaround "
26320a6d9dbSMatthew Dillon 						"installed\n");
264afea4d5aSMatthew Dillon 				msr |= 1;
26588416a26SMatthew Dillon 				wrmsr(MSR_AMD_DE_CFG, msr);
266afea4d5aSMatthew Dillon 			}
267afea4d5aSMatthew Dillon 			break;
268afea4d5aSMatthew Dillon 		}
2691a2e394aSSepherosa Ziehau 
2701a2e394aSSepherosa Ziehau 		/*
27137af6e05SMatthew Dillon 		 * BIOS may fail to set InitApicIdCpuIdLo to 1 as it should
27237af6e05SMatthew Dillon 		 * per BKDG.  So, do it here or otherwise some tools could
27337af6e05SMatthew Dillon 		 * be confused by Initial Local APIC ID reported with
27437af6e05SMatthew Dillon 		 * CPUID Function 1 in EBX.
27537af6e05SMatthew Dillon 		 */
27637af6e05SMatthew Dillon 		if (CPUID_TO_FAMILY(cpu_id) == 0x10) {
27737af6e05SMatthew Dillon 			if ((cpu_feature2 & CPUID2_VMM) == 0) {
27837af6e05SMatthew Dillon 				msr = rdmsr(0xc001001f);
27937af6e05SMatthew Dillon 				msr |= (uint64_t)1 << 54;
28037af6e05SMatthew Dillon 				wrmsr(0xc001001f, msr);
28137af6e05SMatthew Dillon 			}
28237af6e05SMatthew Dillon 		}
28337af6e05SMatthew Dillon 
28437af6e05SMatthew Dillon 		/*
28537af6e05SMatthew Dillon 		 * BIOS may configure Family 10h processors to convert
28637af6e05SMatthew Dillon 		 * WC+ cache type to CD.  That can hurt performance of
28737af6e05SMatthew Dillon 		 * guest VMs using nested paging.
28837af6e05SMatthew Dillon 		 *
28937af6e05SMatthew Dillon 		 * The relevant MSR bit is not documented in the BKDG,
29037af6e05SMatthew Dillon 		 * the fix is borrowed from Linux.
29137af6e05SMatthew Dillon 		 */
29237af6e05SMatthew Dillon 		if (CPUID_TO_FAMILY(cpu_id) == 0x10) {
29337af6e05SMatthew Dillon 			if ((cpu_feature2 & CPUID2_VMM) == 0) {
29437af6e05SMatthew Dillon 				msr = rdmsr(0xc001102a);
29537af6e05SMatthew Dillon 				msr &= ~((uint64_t)1 << 24);
29637af6e05SMatthew Dillon 				wrmsr(0xc001102a, msr);
29737af6e05SMatthew Dillon 			}
29837af6e05SMatthew Dillon 		}
29937af6e05SMatthew Dillon 
30037af6e05SMatthew Dillon 		/*
3011a2e394aSSepherosa Ziehau 		 * Work around Erratum 793: Specific Combination of Writes
3021a2e394aSSepherosa Ziehau 		 * to Write Combined Memory Types and Locked Instructions
3031a2e394aSSepherosa Ziehau 		 * May Cause Core Hang.  See Revision Guide for AMD Family
3041a2e394aSSepherosa Ziehau 		 * 16h Models 00h-0Fh Processors, revision 3.04 or later,
3051a2e394aSSepherosa Ziehau 		 * publication 51810.
3061a2e394aSSepherosa Ziehau 		 */
3071a2e394aSSepherosa Ziehau 		if (CPUID_TO_FAMILY(cpu_id) == 0x16 &&
3081a2e394aSSepherosa Ziehau 		    CPUID_TO_MODEL(cpu_id) <= 0xf) {
3091a2e394aSSepherosa Ziehau 			if ((cpu_feature2 & CPUID2_VMM) == 0) {
3101a2e394aSSepherosa Ziehau 				msr = rdmsr(0xc0011020);
3111a2e394aSSepherosa Ziehau 				msr |= (uint64_t)1 << 15;
3121a2e394aSSepherosa Ziehau 				wrmsr(0xc0011020, msr);
3131a2e394aSSepherosa Ziehau 			}
3141a2e394aSSepherosa Ziehau 		}
315afea4d5aSMatthew Dillon 	}
316afea4d5aSMatthew Dillon 
317b2b3ffcdSSimon Schubert 	if ((amd_feature & AMDID_NX) != 0) {
318b2b3ffcdSSimon Schubert 		msr = rdmsr(MSR_EFER) | EFER_NXE;
319b2b3ffcdSSimon Schubert 		wrmsr(MSR_EFER, msr);
32032d3bd25SSascha Wildner #if 0 /* JG */
321b2b3ffcdSSimon Schubert 		pg_nx = PG_NX;
322b2b3ffcdSSimon Schubert #endif
323b2b3ffcdSSimon Schubert 	}
324b2b3ffcdSSimon Schubert 	if (cpu_vendor_id == CPU_VENDOR_CENTAUR &&
3257deac8ceSSascha Wildner 	    CPUID_TO_FAMILY(cpu_id) == 0x6 &&
3267deac8ceSSascha Wildner 	    CPUID_TO_MODEL(cpu_id) >= 0xf)
327b2b3ffcdSSimon Schubert 		init_via();
32808771751SVenkatesh Srinivas 
32908771751SVenkatesh Srinivas 	TUNABLE_INT_FETCH("hw.clflush_enable", &hw_clflush_enable);
33008771751SVenkatesh Srinivas 	if (cpu_feature & CPUID_CLFSH) {
33108771751SVenkatesh Srinivas 		cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8;
33208771751SVenkatesh Srinivas 
33308771751SVenkatesh Srinivas 		if (hw_clflush_enable == 0 ||
33408771751SVenkatesh Srinivas 		    ((hw_clflush_enable == -1) && vmm_guest))
33508771751SVenkatesh Srinivas 			cpu_feature &= ~CPUID_CLFSH;
33608771751SVenkatesh Srinivas 	}
33719d996daSImre Vadász 
33819d996daSImre Vadász 	/* Set TSC_AUX register to the cpuid, for using rdtscp in userland. */
33919d996daSImre Vadász 	if ((amd_feature & AMDID_RDTSCP) != 0)
340a7c3d105SAaron LI 		wrmsr(MSR_TSC_AUX, cpu);
341b2b3ffcdSSimon Schubert }
3428b257adbSImre Vadász 
3438b257adbSImre Vadász /*
3448b257adbSImre Vadász  * This method should be at least as good as calibrating the TSC based on the
3458b257adbSImre Vadász  * HPET timer, since the HPET runs with the core crystal clock apparently.
3468b257adbSImre Vadász  */
3478b257adbSImre Vadász static void
detect_tsc_frequency(void)3488b257adbSImre Vadász detect_tsc_frequency(void)
3498b257adbSImre Vadász {
3508b257adbSImre Vadász 	int cpu_family, cpu_model;
3518b257adbSImre Vadász 	u_int regs[4];
3528b257adbSImre Vadász 	uint64_t crystal = 0;
3538b257adbSImre Vadász 
3548b257adbSImre Vadász 	cpu_model = CPUID_TO_MODEL(cpu_id);
3558b257adbSImre Vadász 	cpu_family = CPUID_TO_FAMILY(cpu_id);
3568b257adbSImre Vadász 
3578b257adbSImre Vadász 	if (cpu_vendor_id != CPU_VENDOR_INTEL)
3588b257adbSImre Vadász 		return;
3598b257adbSImre Vadász 
3608b257adbSImre Vadász 	if (cpu_high < 0x15)
3618b257adbSImre Vadász 		return;
3628b257adbSImre Vadász 
3638b257adbSImre Vadász 	do_cpuid(0x15, regs);
3648b257adbSImre Vadász 	if (regs[0] == 0 || regs[1] == 0)
3658b257adbSImre Vadász 		return;
3668b257adbSImre Vadász 
3678b257adbSImre Vadász 	if (regs[2] == 0) {
3688b257adbSImre Vadász 		/* For some families the SDM contains the core crystal clock. */
3698b257adbSImre Vadász 		if (cpu_family == 0x6) {
3708b257adbSImre Vadász 			switch (cpu_model) {
3718b257adbSImre Vadász 			case 0x55:	/* Xeon Scalable */
3728b257adbSImre Vadász 				crystal = 25000000;	/* 25 MHz */
3738b257adbSImre Vadász 				break;
3748b257adbSImre Vadász 			/* Skylake */
3758b257adbSImre Vadász 			case 0x4e:
3768b257adbSImre Vadász 			case 0x5e:
3778b257adbSImre Vadász 			/* Kabylake/Coffeelake */
3788b257adbSImre Vadász 			case 0x8e:
3798b257adbSImre Vadász 			case 0x9e:
3808b257adbSImre Vadász 				crystal = 24000000;	/* 24 MHz */
3818b257adbSImre Vadász 				break;
3828b257adbSImre Vadász 			case 0x5c:	/* Goldmont Atom */
3838b257adbSImre Vadász 				crystal = 19200000;	/* 19.2 MHz */
3848b257adbSImre Vadász 				break;
3858b257adbSImre Vadász 			default:
3868b257adbSImre Vadász 				break;
3878b257adbSImre Vadász 			}
3888b257adbSImre Vadász 		}
3898b257adbSImre Vadász 	} else {
3908b257adbSImre Vadász 		crystal = regs[2];
3918b257adbSImre Vadász 	}
3928b257adbSImre Vadász 
3938b257adbSImre Vadász 	if (crystal == 0)
3948b257adbSImre Vadász 		return;
3958b257adbSImre Vadász 
3968b257adbSImre Vadász 	kprintf("TSC crystal clock: %ju Hz, TSC/crystal ratio: %u/%u\n",
3978b257adbSImre Vadász 	    crystal, regs[1], regs[0]);
3988b257adbSImre Vadász 
3998b257adbSImre Vadász 	if (tsc_ignore_cpuid == 0) {
4008b257adbSImre Vadász 		tsc_frequency = (crystal * regs[1]) / regs[0];
4018b257adbSImre Vadász 		i8254_cputimer_disable = 1;
4028b257adbSImre Vadász 	}
4038b257adbSImre Vadász }
4048b257adbSImre Vadász 
4058b257adbSImre Vadász TIMECOUNTER_INIT(cpuid_tsc_frequency, detect_tsc_frequency);
406