xref: /freebsd/sys/amd64/amd64/initcpu.c (revision b7c60aadbbd5c846a250c05791fe7406d6d78bf4)
1 /*-
2  * Copyright (c) KATO Takenori, 1997, 1998.
3  *
4  * All rights reserved.  Unpublished rights reserved under the copyright
5  * laws of Japan.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
17  *
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.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_cpu.h"
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/sysctl.h>
39 
40 #include <machine/cputypes.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
43 
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 
47 static int	hw_instruction_sse;
48 SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD,
49     &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU");
50 /*
51  * -1: automatic (default)
52  *  0: keep enable CLFLUSH
53  *  1: force disable CLFLUSH
54  */
55 static int	hw_clflush_disable = -1;
56 
57 int	cpu;			/* Are we 386, 386sx, 486, etc? */
58 u_int	cpu_feature;		/* Feature flags */
59 u_int	cpu_feature2;		/* Feature flags */
60 u_int	amd_feature;		/* AMD feature flags */
61 u_int	amd_feature2;		/* AMD feature flags */
62 u_int	amd_pminfo;		/* AMD advanced power management info */
63 u_int	via_feature_rng;	/* VIA RNG features */
64 u_int	via_feature_xcrypt;	/* VIA ACE features */
65 u_int	cpu_high;		/* Highest arg to CPUID */
66 u_int	cpu_exthigh;		/* Highest arg to extended CPUID */
67 u_int	cpu_id;			/* Stepping ID */
68 u_int	cpu_procinfo;		/* HyperThreading Info / Brand Index / CLFUSH */
69 u_int	cpu_procinfo2;		/* Multicore info */
70 char	cpu_vendor[20];		/* CPU Origin code */
71 u_int	cpu_vendor_id;		/* CPU vendor ID */
72 u_int	cpu_fxsr;		/* SSE enabled */
73 u_int	cpu_mxcsr_mask;		/* Valid bits in mxcsr */
74 u_int	cpu_clflush_line_size = 32;
75 u_int	cpu_max_ext_state_size;
76 
77 SYSCTL_UINT(_hw, OID_AUTO, via_feature_rng, CTLFLAG_RD,
78 	&via_feature_rng, 0, "VIA RNG feature available in CPU");
79 SYSCTL_UINT(_hw, OID_AUTO, via_feature_xcrypt, CTLFLAG_RD,
80 	&via_feature_xcrypt, 0, "VIA xcrypt feature available in CPU");
81 
82 /*
83  * Initialize special VIA features
84  */
85 static void
86 init_via(void)
87 {
88 	u_int regs[4], val;
89 
90 	/*
91 	 * Check extended CPUID for PadLock features.
92 	 *
93 	 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
94 	 */
95 	do_cpuid(0xc0000000, regs);
96 	if (regs[0] >= 0xc0000001) {
97 		do_cpuid(0xc0000001, regs);
98 		val = regs[3];
99 	} else
100 		return;
101 
102 	/* Enable RNG if present. */
103 	if ((val & VIA_CPUID_HAS_RNG) != 0) {
104 		via_feature_rng = VIA_HAS_RNG;
105 		wrmsr(0x110B, rdmsr(0x110B) | VIA_CPUID_DO_RNG);
106 	}
107 
108 	/* Enable PadLock if present. */
109 	if ((val & VIA_CPUID_HAS_ACE) != 0)
110 		via_feature_xcrypt |= VIA_HAS_AES;
111 	if ((val & VIA_CPUID_HAS_ACE2) != 0)
112 		via_feature_xcrypt |= VIA_HAS_AESCTR;
113 	if ((val & VIA_CPUID_HAS_PHE) != 0)
114 		via_feature_xcrypt |= VIA_HAS_SHA;
115 	if ((val & VIA_CPUID_HAS_PMM) != 0)
116 		via_feature_xcrypt |= VIA_HAS_MM;
117 	if (via_feature_xcrypt != 0)
118 		wrmsr(0x1107, rdmsr(0x1107) | (1 << 28));
119 }
120 
121 /*
122  * Initialize CPU control registers
123  */
124 void
125 initializecpu(void)
126 {
127 	uint64_t msr;
128 
129 	if ((cpu_feature & CPUID_XMM) && (cpu_feature & CPUID_FXSR)) {
130 		load_cr4(rcr4() | CR4_FXSR | CR4_XMM);
131 		cpu_fxsr = hw_instruction_sse = 1;
132 	}
133 	if ((amd_feature & AMDID_NX) != 0) {
134 		msr = rdmsr(MSR_EFER) | EFER_NXE;
135 		wrmsr(MSR_EFER, msr);
136 		pg_nx = PG_NX;
137 	}
138 	if (cpu_vendor_id == CPU_VENDOR_CENTAUR)
139 		init_via();
140 }
141 
142 void
143 initializecpucache()
144 {
145 
146 	/*
147 	 * CPUID with %eax = 1, %ebx returns
148 	 * Bits 15-8: CLFLUSH line size
149 	 * 	(Value * 8 = cache line size in bytes)
150 	 */
151 	if ((cpu_feature & CPUID_CLFSH) != 0)
152 		cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8;
153 	/*
154 	 * XXXKIB: (temporary) hack to work around traps generated
155 	 * when CLFLUSHing APIC register window under virtualization
156 	 * environments.  These environments tend to disable the
157 	 * CPUID_SS feature even though the native CPU supports it.
158 	 */
159 	TUNABLE_INT_FETCH("hw.clflush_disable", &hw_clflush_disable);
160 	if (vm_guest != VM_GUEST_NO && hw_clflush_disable == -1)
161 		cpu_feature &= ~CPUID_CLFSH;
162 	/*
163 	 * Allow to disable CLFLUSH feature manually by
164 	 * hw.clflush_disable tunable.
165 	 */
166 	if (hw_clflush_disable == 1)
167 		cpu_feature &= ~CPUID_CLFSH;
168 }
169