blob: 5cf48eadeddbb8b2a23c8edbb0138c97bc1f7a6f [file] [log] [blame]
Angel Pons3bd1e3d2020-04-05 15:47:17 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Lee Leahyb0005132015-05-12 18:19:47 -07002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
Lee Leahyb0005132015-05-12 18:19:47 -07006#include <cpu/x86/mtrr.h>
7#include <cpu/x86/msr.h>
Lee Leahyb0005132015-05-12 18:19:47 -07008#include <cpu/x86/mp.h>
Nico Huber6275e342018-11-21 00:11:35 +01009#include <cpu/intel/common/common.h>
Lee Leahyb0005132015-05-12 18:19:47 -070010#include <cpu/intel/microcode.h>
11#include <cpu/intel/speedstep.h>
12#include <cpu/intel/turbo.h>
Lee Leahyb0005132015-05-12 18:19:47 -070013#include <cpu/x86/name.h>
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +030014#include <cpu/intel/smm_reloc.h>
Barnali Sarkar0a203d12017-05-04 18:02:17 +053015#include <intelblocks/cpulib.h>
Aaron Durbin93d5f402017-06-08 11:00:23 -050016#include <intelblocks/fast_spi.h>
Barnali Sarkar73273862017-06-13 20:22:33 +053017#include <intelblocks/mp_init.h>
Pratik Prajapatia04aa3d2017-06-12 23:02:36 -070018#include <intelblocks/sgx.h>
Lee Leahyb0005132015-05-12 18:19:47 -070019#include <soc/cpu.h>
20#include <soc/msr.h>
21#include <soc/pci_devs.h>
22#include <soc/ramstage.h>
Lee Leahyb0005132015-05-12 18:19:47 -070023#include <soc/systemagent.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020024#include <types.h>
Lee Leahyb0005132015-05-12 18:19:47 -070025
Elyes HAOUASc3385072019-03-21 15:38:06 +010026#include "chip.h"
27
Subrata Banik56ab8e22022-01-07 13:40:19 +000028bool cpu_soc_is_in_untrusted_mode(void)
29{
Subrata Banik37a55d12022-05-30 18:11:12 +000030 /*
31 * TODO: Add dynamic detection to identify if skylake SoC
32 * is in coffeelake board.
33 */
Subrata Banik56ab8e22022-01-07 13:40:19 +000034 if (!CONFIG(MAINBOARD_SUPPORTS_COFFEELAKE_CPU))
35 return false;
36
37 /* IA_UNTRUSTED_MODE is not supported in Sky Lake */
38 msr_t msr = rdmsr(MSR_BIOS_DONE);
39 return !!(msr.lo & ENABLE_IA_UNTRUSTED);
40}
41
Subrata Banik37a55d12022-05-30 18:11:12 +000042void cpu_soc_bios_done(void)
43{
44 if (!CONFIG(MAINBOARD_SUPPORTS_COFFEELAKE_CPU))
45 return;
46
47 msr_t msr;
48
49 msr = rdmsr(MSR_BIOS_DONE);
50 msr.lo |= ENABLE_IA_UNTRUSTED;
51 wrmsr(MSR_BIOS_DONE, msr);
52}
53
Lee Leahyb0005132015-05-12 18:19:47 -070054static void configure_misc(void)
55{
Kyösti Mälkkid5f645c2019-09-28 00:20:27 +030056 config_t *conf = config_of_soc();
Lee Leahyb0005132015-05-12 18:19:47 -070057 msr_t msr;
58
59 msr = rdmsr(IA32_MISC_ENABLE);
Lee Leahy1d14b3e2015-05-12 18:23:27 -070060 msr.lo |= (1 << 0); /* Fast String enable */
61 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Lee Leahyb0005132015-05-12 18:19:47 -070062 wrmsr(IA32_MISC_ENABLE, msr);
63
Matt Delco54e98942020-03-09 12:41:09 -070064 /* Set EIST status */
65 cpu_set_eist(conf->eist_enable);
66
Lee Leahyb0005132015-05-12 18:19:47 -070067 /* Disable Thermal interrupts */
68 msr.lo = 0;
69 msr.hi = 0;
70 wrmsr(IA32_THERM_INTERRUPT, msr);
71
72 /* Enable package critical interrupt only */
73 msr.lo = 1 << 4;
74 msr.hi = 0;
75 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
Pratik Prajapati79cfcde2016-03-08 12:34:06 -080076
Pratik Prajapati79cfcde2016-03-08 12:34:06 -080077 msr = rdmsr(MSR_POWER_CTL);
Angel Pons4d794bd2021-10-11 14:00:54 +020078 msr.lo |= (1 << 0); /* Enable Bi-directional PROCHOT as an input */
Matthew Garrett13e7a2f2019-07-19 17:02:07 -070079 msr.lo |= (1 << 18); /* Enable Energy/Performance Bias control */
Cole Nelson63b6fea2018-06-15 15:51:54 -070080 msr.lo &= ~POWER_CTL_C1E_MASK; /* Disable C1E */
Pratik Prajapati79cfcde2016-03-08 12:34:06 -080081 msr.lo |= (1 << 23); /* Lock it */
82 wrmsr(MSR_POWER_CTL, msr);
Lee Leahyb0005132015-05-12 18:19:47 -070083}
84
Subrata Banik481b3642017-05-12 11:29:43 +053085static void configure_c_states(void)
86{
87 msr_t msr;
88
89 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
90 msr.hi = 0;
91 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
92 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
93
94 /* C-state Interrupt Response Latency Control 1 - package C6/C7 short */
95 msr.hi = 0;
96 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
97 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
98
99 /* C-state Interrupt Response Latency Control 2 - package C6/C7 long */
100 msr.hi = 0;
101 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
102 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
103
104 /* C-state Interrupt Response Latency Control 3 - package C8 */
105 msr.hi = 0;
106 msr.lo = IRTL_VALID | IRTL_1024_NS |
107 C_STATE_LATENCY_CONTROL_3_LIMIT;
108 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
109
110 /* C-state Interrupt Response Latency Control 4 - package C9 */
111 msr.hi = 0;
112 msr.lo = IRTL_VALID | IRTL_1024_NS |
113 C_STATE_LATENCY_CONTROL_4_LIMIT;
114 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
115
116 /* C-state Interrupt Response Latency Control 5 - package C10 */
117 msr.hi = 0;
118 msr.lo = IRTL_VALID | IRTL_1024_NS |
119 C_STATE_LATENCY_CONTROL_5_LIMIT;
120 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
121}
122
Lee Leahyb0005132015-05-12 18:19:47 -0700123/* All CPUs including BSP will run the following function. */
Elyes HAOUAS143fb462018-05-25 12:56:45 +0200124void soc_core_init(struct device *cpu)
Lee Leahyb0005132015-05-12 18:19:47 -0700125{
Patrick Rudolphfc36e9f2021-01-25 10:46:16 +0100126 /* Configure Core PRMRR for SGX. */
127 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
128 prmrr_core_configure();
129
Lee Leahyb0005132015-05-12 18:19:47 -0700130 /* Clear out pending MCEs */
Pratik Prajapatie8163152017-08-28 12:27:57 -0700131 /* TODO(adurbin): This should only be done on a cold boot. Also, some
132 * of these banks are core vs package scope. For now every CPU clears
133 * every bank. */
Subrata Banikf91344c2019-05-06 19:23:26 +0530134 mca_configure();
Lee Leahyb0005132015-05-12 18:19:47 -0700135
Lee Leahyb0005132015-05-12 18:19:47 -0700136 enable_lapic_tpr();
Lee Leahyb0005132015-05-12 18:19:47 -0700137
Subrata Banik481b3642017-05-12 11:29:43 +0530138 /* Configure c-state interrupt response time */
139 configure_c_states();
140
Lee Leahyb0005132015-05-12 18:19:47 -0700141 /* Configure Enhanced SpeedStep and Thermal Sensors */
142 configure_misc();
143
Michael Niewöhner63032432020-10-11 17:34:54 +0200144 set_aesni_lock();
Michael Niewöhner7bdedcd2019-09-01 16:49:09 +0200145
Subrata Banikf004f662017-02-03 19:05:27 +0530146 /* Enable ACPI Timer Emulation via MSR 0x121 */
147 enable_pm_timer_emulation();
148
Lee Leahyb0005132015-05-12 18:19:47 -0700149 /* Enable Direct Cache Access */
150 configure_dca_cap();
151
152 /* Set energy policy */
153 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
154
155 /* Enable Turbo */
156 enable_turbo();
Aaron Durbin58225822016-05-03 17:45:59 -0500157}
Lee Leahyb0005132015-05-12 18:19:47 -0700158
Aaron Durbin58225822016-05-03 17:45:59 -0500159static void per_cpu_smm_trigger(void)
160{
161 /* Relocate the SMM handler. */
162 smm_relocate();
Aaron Durbin58225822016-05-03 17:45:59 -0500163}
Lee Leahyb0005132015-05-12 18:19:47 -0700164
Angel Pons1b8e65d2021-02-19 18:29:58 +0100165void smm_lock(void)
166{
167 struct device *sa_dev = pcidev_path_on_root(SA_DEVFN_ROOT);
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.
172 */
173 printk(BIOS_DEBUG, "Locking SMM.\n");
174 pci_write_config8(sa_dev, SMRAM, D_LCK | G_SMRAME | C_BASE_SEG);
175}
176
Nico Huber6275e342018-11-21 00:11:35 +0100177static void vmx_configure(void *unused)
178{
179 set_feature_ctrl_vmx();
180}
181
182static void fc_lock_configure(void *unused)
183{
184 set_feature_ctrl_lock();
185}
186
Aaron Durbin58225822016-05-03 17:45:59 -0500187static void post_mp_init(void)
188{
Felix Held30f70312021-10-20 20:42:03 +0200189 bool failure = false;
Patrick Rudolphbe207b12019-07-26 14:22:09 +0200190
Lee Leahyb0005132015-05-12 18:19:47 -0700191 /* Set Max Ratio */
Barnali Sarkar0a203d12017-05-04 18:02:17 +0530192 cpu_set_max_ratio();
Lee Leahyb0005132015-05-12 18:19:47 -0700193
Aaron Durbin58225822016-05-03 17:45:59 -0500194 /*
195 * Now that all APs have been relocated as well as the BSP let SMIs
196 * start flowing.
197 */
Kyösti Mälkki040c5312020-05-31 20:03:11 +0300198 global_smi_enable_no_pwrbtn();
Aaron Durbin58225822016-05-03 17:45:59 -0500199
200 /* Lock down the SMRAM space. */
Kyösti Mälkkib4905622019-07-12 08:02:35 +0300201 if (CONFIG(HAVE_SMI_HANDLER))
202 smm_lock();
Pratik Prajapati7a357eb2017-08-14 12:18:38 -0700203
Felix Held82faefb2021-10-20 20:50:58 +0200204 if (mp_run_on_all_cpus(vmx_configure, NULL) != CB_SUCCESS)
Felix Held30f70312021-10-20 20:42:03 +0200205 failure = true;
Matt DeVillier969ef102018-03-21 20:47:52 -0500206
Michael Niewöhner7736bfc2019-10-22 23:05:06 +0200207 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
Felix Held82faefb2021-10-20 20:50:58 +0200208 if (mp_run_on_all_cpus(sgx_configure, NULL) != CB_SUCCESS)
Felix Held30f70312021-10-20 20:42:03 +0200209 failure = true;
Nico Huber6275e342018-11-21 00:11:35 +0100210
Felix Held82faefb2021-10-20 20:50:58 +0200211 if (mp_run_on_all_cpus(fc_lock_configure, NULL) != CB_SUCCESS)
Felix Held30f70312021-10-20 20:42:03 +0200212 failure = true;
Patrick Rudolphbe207b12019-07-26 14:22:09 +0200213
Felix Held30f70312021-10-20 20:42:03 +0200214 if (failure)
Patrick Rudolphbe207b12019-07-26 14:22:09 +0200215 printk(BIOS_CRIT, "CRITICAL ERROR: MP post init failed\n");
Aaron Durbin58225822016-05-03 17:45:59 -0500216}
217
Arthur Heymans1999bc52021-02-16 13:20:13 +0100218static void soc_fsp_load(void)
219{
220 fsps_load();
221}
222
Aaron Durbin58225822016-05-03 17:45:59 -0500223static const struct mp_ops mp_ops = {
Subrata Banika4b11e5c2017-02-03 18:57:49 +0530224 /*
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 */
Furquan Shaikhc2480442017-02-20 13:41:56 -0800229 .pre_mp_init = soc_fsp_load,
Aaron Durbin58225822016-05-03 17:45:59 -0500230 .get_cpu_count = get_cpu_count,
231 .get_smm_info = smm_info,
232 .get_microcode_info = get_microcode_info,
Aaron Durbin58225822016-05-03 17:45:59 -0500233 .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
Arthur Heymans829e8e62023-01-30 19:09:34 +0100239void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin58225822016-05-03 17:45:59 -0500240{
Felix Held4dd7d112021-10-20 23:31:43 +0200241 /* TODO: Handle mp_init_with_smm failure? */
242 mp_init_with_smm(cpu_bus, &mp_ops);
Sumeet Pawnikar9d2f3de2016-12-22 13:48:46 +0530243
244 /* Thermal throttle activation offset */
Sumeet R Pawnikar360684b2020-06-18 15:56:11 +0530245 configure_tcc_thermal_target();
Lee Leahyb0005132015-05-12 18:19:47 -0700246}
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530247
248int soc_skip_ucode_update(u32 current_patch_id, u32 new_patch_id)
249{
Robbie Zhang7de03172017-02-21 14:00:31 -0800250 msr_t msr1;
251 msr_t msr2;
252
253 /*
254 * If PRMRR/SGX is supported the FIT microcode load will set the msr
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530255 * 0x08b with the Patch revision id one less than the id in the
256 * microcode binary. The PRMRR support is indicated in the MSR
Robbie Zhang7de03172017-02-21 14:00:31 -0800257 * 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.
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530261 */
Robbie Zhang7de03172017-02-21 14:00:31 -0800262 msr1 = rdmsr(MTRR_CAP_MSR);
Elyes HAOUASf212cf32018-12-18 10:24:55 +0100263 msr2 = rdmsr(MSR_PRMRR_PHYS_BASE);
Robbie Zhang7de03172017-02-21 14:00:31 -0800264 if (msr2.lo && (current_patch_id == new_patch_id - 1))
265 return 0;
266 else
Kyösti Mälkkieadd2512020-06-11 09:52:45 +0300267 return (msr1.lo & MTRR_CAP_PRMRR) &&
Robbie Zhang7de03172017-02-21 14:00:31 -0800268 (current_patch_id == new_patch_id - 1);
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530269}