Debian Codegraph @

src/soc/intel/skylake/cpu.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <cpu/x86/mtrr.h>
7 #include <cpu/x86/msr.h>
8 #include <cpu/x86/mp.h>
9 #include <cpu/intel/common/common.h>
10 #include <cpu/intel/microcode.h>
11 #include <cpu/intel/speedstep.h>
12 #include <cpu/intel/turbo.h>
13 #include <cpu/x86/name.h>
14 #include <cpu/intel/smm_reloc.h>
15 #include <intelblocks/cpulib.h>
16 #include <intelblocks/fast_spi.h>
17 #include <intelblocks/mp_init.h>
18 #include <intelblocks/sgx.h>
19 #include <soc/cpu.h>
20 #include <soc/msr.h>
21 #include <soc/pci_devs.h>
22 #include <soc/ramstage.h>
23 #include <soc/systemagent.h>
24 #include <static.h>
25 #include <types.h>
26
27 #include "chip.h"
28
29 bool cpu_soc_is_in_untrusted_mode(void)
30 {
31 /*
32 * TODO: Add dynamic detection to identify if skylake SoC
33 * is in coffeelake board.
34 */
35 if (!CONFIG(MAINBOARD_SUPPORTS_COFFEELAKE_CPU))
36 return false;
37
38 /* IA_UNTRUSTED_MODE is not supported in Sky Lake */
39 msr_t msr = rdmsr(MSR_BIOS_DONE);
40 return !!(msr.lo & ENABLE_IA_UNTRUSTED);
41 }
42
43 void cpu_soc_bios_done(void)
44 {
45 if (!CONFIG(MAINBOARD_SUPPORTS_COFFEELAKE_CPU))
46 return;
47
48 msr_t msr;
49
50 msr = rdmsr(MSR_BIOS_DONE);
51 msr.lo |= ENABLE_IA_UNTRUSTED;
52 wrmsr(MSR_BIOS_DONE, msr);
53 }
54
55 static void configure_misc(void)
56 {
57 config_t *conf = config_of_soc();
58 msr_t msr;
59
60 msr = rdmsr(IA32_MISC_ENABLE);
61 msr.lo |= (1 << 0); /* Fast String enable */
62 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
63 wrmsr(IA32_MISC_ENABLE, msr);
64
65 /* Set EIST status */
66 cpu_set_eist(conf->eist_enable);
67
68 /* Disable Thermal interrupts */
69 msr.lo = 0;
70 msr.hi = 0;
71 wrmsr(IA32_THERM_INTERRUPT, msr);
72
73 /* Enable package critical interrupt only */
74 msr.lo = 1 << 4;
75 msr.hi = 0;
76 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
77
78 msr = rdmsr(MSR_POWER_CTL);
79 msr.lo |= (1 << 0); /* Enable Bi-directional PROCHOT as an input */
80 msr.lo |= (1 << 18); /* Enable Energy/Performance Bias control */
81 msr.lo &= ~POWER_CTL_C1E_MASK; /* Disable C1E */
82 msr.lo |= (1 << 23); /* Lock it */
83 wrmsr(MSR_POWER_CTL, msr);
84 }
85
86 static void configure_c_states(void)
87 {
88 msr_t msr;
89
90 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
91 msr.hi = 0;
92 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
93 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
94
95 /* C-state Interrupt Response Latency Control 1 - package C6/C7 short */
96 msr.hi = 0;
97 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
98 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
99
100 /* C-state Interrupt Response Latency Control 2 - package C6/C7 long */
101 msr.hi = 0;
102 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
103 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
104
105 /* C-state Interrupt Response Latency Control 3 - package C8 */
106 msr.hi = 0;
107 msr.lo = IRTL_VALID | IRTL_1024_NS |
108 C_STATE_LATENCY_CONTROL_3_LIMIT;
109 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
110
111 /* C-state Interrupt Response Latency Control 4 - package C9 */
112 msr.hi = 0;
113 msr.lo = IRTL_VALID | IRTL_1024_NS |
114 C_STATE_LATENCY_CONTROL_4_LIMIT;
115 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
116
117 /* C-state Interrupt Response Latency Control 5 - package C10 */
118 msr.hi = 0;
119 msr.lo = IRTL_VALID | IRTL_1024_NS |
120 C_STATE_LATENCY_CONTROL_5_LIMIT;
121 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
122 }
123
124 /* All CPUs including BSP will run the following function. */
125 void soc_core_init(struct device *cpu)
126 {
127 /* Configure Core PRMRR for SGX. */
128 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
129 prmrr_core_configure();
130
131 /* Clear out pending MCEs */
132 /* TODO(adurbin): This should only be done on a cold boot. Also, some
133 * of these banks are core vs package scope. For now every CPU clears
134 * every bank. */
135 mca_configure();
136
137 enable_lapic_tpr();
138
139 /* Configure c-state interrupt response time */
140 configure_c_states();
141
142 /* Configure Enhanced SpeedStep and Thermal Sensors */
143 configure_misc();
144
145 set_aesni_lock();
146
147 /* Enable ACPI Timer Emulation via MSR 0x121 */
148 enable_pm_timer_emulation();
149
150 /* Enable Direct Cache Access */
151 configure_dca_cap();
152
153 /* Set energy policy */
154 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
155
156 /* Enable Turbo */
157 enable_turbo();
158 }
159
160 static void per_cpu_smm_trigger(void)
161 {
162 /* Relocate the SMM handler. */
163 smm_relocate();
164 }
165
166 void smm_lock(void)
167 {
168 /*
169 * LOCK the SMM memory window and enable normal SMM.
170 * After running this function, only a full reset can
171 * make the SMM registers writable again. D_LCK bit
172 * requires the PCI 0xcf8/0xcfc I/O access.
173 */
174 printk(BIOS_DEBUG, "Locking SMM.\n");
175 pci_io_write_config8(SA_DEVFN_ROOT, SMRAM, D_LCK | G_SMRAME | C_BASE_SEG);
176 }
177
178 static void vmx_configure(void *unused)
179 {
180 set_feature_ctrl_vmx();
181 }
182
183 static void fc_lock_configure(void *unused)
184 {
185 set_feature_ctrl_lock();
186 }
187
188 static void post_mp_init(void)
189 {
190 bool failure = false;
191
192 /* Set Max Ratio */
193 cpu_set_max_ratio();
194
195 /*
196 * Now that all APs have been relocated as well as the BSP let SMIs
197 * start flowing.
198 */
199 global_smi_enable_no_pwrbtn();
200
201 /* Lock down the SMRAM space. */
202 smm_lock();
203
204 if (mp_run_on_all_cpus(vmx_configure, NULL) != CB_SUCCESS)
205 failure = true;
206
207 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
208 if (mp_run_on_all_cpus(sgx_configure, NULL) != CB_SUCCESS)
209 failure = true;
210
211 if (mp_run_on_all_cpus(fc_lock_configure, NULL) != CB_SUCCESS)
212 failure = true;
213
214 if (failure)
215 printk(BIOS_CRIT, "CRITICAL ERROR: MP post init failed\n");
216 }
217
218 static void soc_fsp_load(void)
219 {
220 fsps_load();
221 }
222
223 static const struct mp_ops mp_ops = {
224 /*
225 * Skip Pre MP init MTRR programming as MTRRs are mirrored from BSP,
226 * that are set prior to ramstage.
227 * Real MTRRs programming are being done after resource allocation.
228 */
229 .pre_mp_init = soc_fsp_load,
230 .get_cpu_count = get_cpu_count,
231 .get_smm_info = smm_info,
232 .get_microcode_info = get_microcode_info,
233 .pre_mp_smm_init = smm_initialize,
234 .per_cpu_smm_trigger = per_cpu_smm_trigger,
235 .relocation_handler = smm_relocation_handler,
236 .post_mp_init = post_mp_init,
237 };
238
239 void mp_init_cpus(struct bus *cpu_bus)
240 {
241 /* TODO: Handle mp_init_with_smm failure? */
242 mp_init_with_smm(cpu_bus, &mp_ops);
243
244 /* Thermal throttle activation offset */
245 configure_tcc_thermal_target();
246 }
247
248 int soc_skip_ucode_update(u32 current_patch_id, u32 new_patch_id)
249 {
250 msr_t msr1;
251 msr_t msr2;
252
253 /*
254 * If PRMRR/SGX is supported the FIT microcode load will set the msr
255 * 0x08b with the Patch revision id one less than the id in the
256 * microcode binary. The PRMRR support is indicated in the MSR
257 * MTRRCAP[12]. If SGX is not enabled, check and avoid reloading the
258 * same microcode during CPU initialization. If SGX is enabled, as
259 * part of SGX BIOS initialization steps, the same microcode needs to
260 * be reloaded after the core PRMRR MSRs are programmed.
261 */
262 msr1 = rdmsr(MTRR_CAP_MSR);
263 msr2 = rdmsr(MSR_PRMRR_PHYS_BASE);
264 if (msr2.lo && (current_patch_id == new_patch_id - 1))
265 return 0;
266 else
267 return (msr1.lo & MTRR_CAP_PRMRR) &&
268 (current_patch_id == new_patch_id - 1);
269 }