Debian Codegraph @

src/soc/intel/apollolake/cpu.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <acpi/acpi.h>
4 #include <assert.h>
5 #include <console/console.h>
6 #include "chip.h"
7 #include <cpu/cpu.h>
8 #include <cpu/x86/mp.h>
9 #include <cpu/intel/microcode.h>
10 #include <cpu/intel/turbo.h>
11 #include <cpu/intel/common/common.h>
12 #include <cpu/x86/msr.h>
13 #include <cpu/x86/mtrr.h>
14 #include <cpu/x86/smm.h>
15 #include <cpu/intel/em64t100_save_state.h>
16 #include <cpu/intel/smm_reloc.h>
17 #include <device/device.h>
18 #include <device/pci.h>
19 #include <fsp/api.h>
20 #include <intelblocks/cpulib.h>
21 #include <intelblocks/fast_spi.h>
22 #include <intelblocks/mp_init.h>
23 #include <intelblocks/msr.h>
24 #include <intelblocks/sgx.h>
25 #include <reg_script.h>
26 #include <soc/cpu.h>
27 #include <soc/iomap.h>
28 #include <soc/pci_devs.h>
29 #include <soc/pm.h>
30 #include <types.h>
31
32 static const struct reg_script core_msr_script[] = {
33 #if !CONFIG(SOC_INTEL_GEMINILAKE)
34 /* Enable C-state and IO/MWAIT redirect */
35 REG_MSR_WRITE(MSR_PKG_CST_CONFIG_CONTROL,
36 (PKG_C_STATE_LIMIT_C2_MASK | CORE_C_STATE_LIMIT_C10_MASK
37 | IO_MWAIT_REDIRECT_MASK | CST_CFG_LOCK_MASK)),
38 /* Power Management I/O base address for I/O trapping to C-states */
39 REG_MSR_WRITE(MSR_PMG_IO_CAPTURE_BASE,
40 (ACPI_PMIO_CST_REG | (PMG_IO_BASE_CST_RNG_BLK_SIZE << 16))),
41 /* Disable support for MONITOR and MWAIT instructions */
42 REG_MSR_RMW(IA32_MISC_ENABLE, ~MONITOR_MWAIT_DIS_MASK, 0),
43 #endif
44 /* Disable C1E */
45 REG_MSR_RMW(MSR_POWER_CTL, ~POWER_CTL_C1E_MASK, 0),
46 REG_SCRIPT_END
47 };
48
49 bool cpu_soc_is_in_untrusted_mode(void)
50 {
51 msr_t msr;
52
53 msr = rdmsr(MSR_POWER_MISC);
54 return !!(msr.lo & ENABLE_IA_UNTRUSTED);
55 }
56
57 void cpu_soc_bios_done(void)
58 {
59 msr_t msr;
60
61 msr = rdmsr(MSR_POWER_MISC);
62 msr.lo |= ENABLE_IA_UNTRUSTED;
63 wrmsr(MSR_POWER_MISC, msr);
64 }
65
66 void soc_core_init(struct device *cpu)
67 {
68 /* Configure Core PRMRR for SGX. */
69 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
70 prmrr_core_configure();
71
72 /* Clear out pending MCEs */
73 /* TODO(adurbin): Some of these banks are core vs package
74 scope. For now every CPU clears every bank. */
75 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE) || acpi_get_sleep_type() == ACPI_S5)
76 mca_configure();
77
78 /* Set core MSRs */
79 reg_script_run(core_msr_script);
80
81 set_aesni_lock();
82
83 /* Set virtualization based on Kconfig option */
84 set_vmx_and_lock();
85
86 /*
87 * Enable ACPI PM timer emulation, which also lets microcode know
88 * location of ACPI_BASE_ADDRESS. This also enables other features
89 * implemented in microcode.
90 */
91 enable_pm_timer_emulation();
92
93 /* Set Max Non-Turbo ratio if RAPL is disabled. */
94 if (CONFIG(SOC_INTEL_DISABLE_POWER_LIMITS)) {
95 cpu_set_p_state_to_max_non_turbo_ratio();
96 /* Disable speed step */
97 cpu_set_eist(false);
98 } else if (CONFIG(SOC_INTEL_SET_MIN_CLOCK_RATIO)) {
99 cpu_set_p_state_to_min_clock_ratio();
100 /* Disable speed step */
101 cpu_set_eist(false);
102 }
103 }
104
105 #if !CONFIG(SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)
106 static void soc_init_core(struct device *cpu)
107 {
108 soc_core_init(cpu);
109 }
110
111 static struct device_operations cpu_dev_ops = {
112 .init = soc_init_core,
113 };
114
115 static const struct cpu_device_id cpu_table[] = {
116 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_A0, CPUID_EXACT_MATCH_MASK },
117 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_B0, CPUID_EXACT_MATCH_MASK },
118 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_E0, CPUID_EXACT_MATCH_MASK },
119 { X86_VENDOR_INTEL, CPUID_GLK_A0, CPUID_EXACT_MATCH_MASK },
120 { X86_VENDOR_INTEL, CPUID_GLK_B0, CPUID_EXACT_MATCH_MASK },
121 { X86_VENDOR_INTEL, CPUID_GLK_R0, CPUID_EXACT_MATCH_MASK },
122 CPU_TABLE_END
123 };
124
125 static const struct cpu_driver driver __cpu_driver = {
126 .ops = &cpu_dev_ops,
127 .id_table = cpu_table,
128 };
129 #endif
130
131 /*
132 * MP and SMM loading initialization.
133 */
134 struct smm_relocation_attrs {
135 uint32_t smbase;
136 uint32_t smrr_base;
137 uint32_t smrr_mask;
138 };
139
140 static struct smm_relocation_attrs relo_attrs;
141
142 /*
143 * Do essential initialization tasks before APs can be fired up.
144 *
145 * IF (CONFIG(SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)) -
146 * Skip Pre MP init MTRR programming, as MTRRs are mirrored from BSP,
147 * that are set prior to ramstage.
148 * Real MTRRs are programmed after resource allocation.
149 *
150 * Do FSP loading before MP Init to ensure that the FSP component stored in
151 * external stage cache in TSEG does not flush off due to SMM relocation
152 * during MP Init stage.
153 *
154 * ELSE -
155 * Enable MTRRs on the BSP. This creates the MTRR solution that the
156 * APs will use. Otherwise APs will try to apply the incomplete solution
157 * as the BSP is calculating it.
158 */
159 static void pre_mp_init(void)
160 {
161 if (CONFIG(SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)) {
162 fsps_load();
163 return;
164 }
165 x86_setup_mtrrs_with_detect();
166 x86_mtrr_check();
167 }
168
169 #if !CONFIG(SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)
170 static void read_cpu_topology(unsigned int *num_phys, unsigned int *num_virt)
171 {
172 msr_t msr;
173 msr = rdmsr(MSR_CORE_THREAD_COUNT);
174 *num_virt = (msr.lo >> 0) & 0xffff;
175 *num_phys = (msr.lo >> 16) & 0xffff;
176 }
177
178 /* Find CPU topology */
179 int get_cpu_count(void)
180 {
181 unsigned int num_virt_cores, num_phys_cores;
182
183 read_cpu_topology(&num_phys_cores, &num_virt_cores);
184
185 printk(BIOS_DEBUG, "Detected %u core, %u thread CPU.\n",
186 num_phys_cores, num_virt_cores);
187
188 return num_virt_cores;
189 }
190
191 void get_microcode_info(const void **microcode, int *parallel)
192 {
193 *microcode = intel_microcode_find();
194 *parallel = 1;
195
196 /* Make sure BSP is using the microcode from cbfs */
197 intel_microcode_load_unlocked(*microcode);
198 }
199 #endif
200
201 static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
202 size_t *smm_save_state_size)
203 {
204 uintptr_t smm_base;
205 size_t smm_size;
206 uintptr_t handler_base;
207 size_t handler_size;
208
209 /* All range registers are aligned to 4KiB */
210 const uint32_t rmask = ~((1 << 12) - 1);
211
212 /* Initialize global tracking state. */
213 smm_region(&smm_base, &smm_size);
214 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
215
216 relo_attrs.smbase = smm_base;
217 relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK;
218 relo_attrs.smrr_mask = ~(smm_size - 1) & rmask;
219 relo_attrs.smrr_mask |= MTRR_PHYS_MASK_VALID;
220
221 *perm_smbase = handler_base;
222 *perm_smsize = handler_size;
223 *smm_save_state_size = sizeof(em64t100_smm_state_save_area_t);
224 }
225
226 static void relocation_handler(int cpu, uintptr_t curr_smbase,
227 uintptr_t staggered_smbase)
228 {
229 msr_t smrr;
230 em64t100_smm_state_save_area_t *smm_state;
231 /* Set up SMRR. */
232 smrr.lo = relo_attrs.smrr_base;
233 smrr.hi = 0;
234 wrmsr(IA32_SMRR_PHYS_BASE, smrr);
235 smrr.lo = relo_attrs.smrr_mask;
236 smrr.hi = 0;
237 wrmsr(IA32_SMRR_PHYS_MASK, smrr);
238 smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + curr_smbase);
239 smm_state->smbase = staggered_smbase;
240 }
241 /*
242 * CPU initialization recipe
243 *
244 * Note that no microcode update is passed to the init function. CSE updates
245 * the microcode on all cores before releasing them from reset. That means that
246 * the BSP and all APs will come up with the same microcode revision.
247 */
248
249 static void post_mp_init(void)
250 {
251 global_smi_enable();
252
253 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
254 mp_run_on_all_cpus(sgx_configure, NULL);
255 }
256
257 static const struct mp_ops mp_ops = {
258 .pre_mp_init = pre_mp_init,
259 .get_cpu_count = get_cpu_count,
260 .get_smm_info = get_smm_info,
261 .get_microcode_info = get_microcode_info,
262 .pre_mp_smm_init = smm_southbridge_clear_state,
263 .relocation_handler = relocation_handler,
264 .post_mp_init = post_mp_init,
265 };
266
267 void mp_init_cpus(struct bus *cpu_bus)
268 {
269 /* Clear for take-off */
270 /* TODO: Handle mp_init_with_smm failure? */
271 mp_init_with_smm(cpu_bus, &mp_ops);
272
273 /* MTRR setup happens later, so we're done here. */
274 if (CONFIG(SOC_INTEL_COMMON_BLOCK_CPU_MPINIT))
275 return;
276
277 /* Temporarily cache the memory-mapped boot media. */
278 if (CONFIG(BOOT_DEVICE_MEMORY_MAPPED) &&
279 CONFIG(BOOT_DEVICE_SPI_FLASH))
280 fast_spi_cache_bios_region();
281 }
282
283 #if CONFIG(SOC_INTEL_GEMINILAKE)
284 int soc_skip_ucode_update(u32 current_patch_id, u32 new_patch_id)
285 {
286 /*
287 * If PRMRR/SGX is supported the FIT microcode load will set the msr
288 * 0x08b with the Patch revision id one less than the id in the
289 * microcode binary. The PRMRR support is indicated in the MSR
290 * MTRRCAP[12]. If SGX is not enabled, check and avoid reloading the
291 * same microcode during CPU initialization. If SGX is enabled, as
292 * part of SGX BIOS initialization steps, the same microcode needs to
293 * be reloaded after the core PRMRR MSRs are programmed.
294 */
295 const msr_t mtrr_cap = rdmsr(MTRR_CAP_MSR);
296 if (mtrr_cap.lo & MTRR_CAP_PRMRR) {
297 const msr_t prmrr_phys_base = rdmsr(MSR_PRMRR_PHYS_BASE);
298 if (prmrr_phys_base.raw) {
299 return 0;
300 }
301 }
302 return current_patch_id == new_patch_id - 1;
303 }
304 #endif