Angel Pons | 3bd1e3d | 2020-04-05 15:47:17 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
| 4 | #include <device/device.h> |
| 5 | #include <device/pci.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 6 | #include <cpu/x86/mtrr.h> |
| 7 | #include <cpu/x86/msr.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 8 | #include <cpu/x86/mp.h> |
Nico Huber | 6275e34 | 2018-11-21 00:11:35 +0100 | [diff] [blame] | 9 | #include <cpu/intel/common/common.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 10 | #include <cpu/intel/microcode.h> |
| 11 | #include <cpu/intel/speedstep.h> |
| 12 | #include <cpu/intel/turbo.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 13 | #include <cpu/x86/name.h> |
Kyösti Mälkki | faf20d3 | 2019-08-14 05:41:41 +0300 | [diff] [blame] | 14 | #include <cpu/intel/smm_reloc.h> |
Barnali Sarkar | 0a203d1 | 2017-05-04 18:02:17 +0530 | [diff] [blame] | 15 | #include <intelblocks/cpulib.h> |
Aaron Durbin | 93d5f40 | 2017-06-08 11:00:23 -0500 | [diff] [blame] | 16 | #include <intelblocks/fast_spi.h> |
Barnali Sarkar | 7327386 | 2017-06-13 20:22:33 +0530 | [diff] [blame] | 17 | #include <intelblocks/mp_init.h> |
Pratik Prajapati | a04aa3d | 2017-06-12 23:02:36 -0700 | [diff] [blame] | 18 | #include <intelblocks/sgx.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 19 | #include <soc/cpu.h> |
| 20 | #include <soc/msr.h> |
| 21 | #include <soc/pci_devs.h> |
| 22 | #include <soc/ramstage.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 23 | #include <soc/systemagent.h> |
Felix Held | d27ef5b | 2021-10-20 20:18:12 +0200 | [diff] [blame] | 24 | #include <types.h> |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 25 | |
Elyes HAOUAS | c338507 | 2019-03-21 15:38:06 +0100 | [diff] [blame] | 26 | #include "chip.h" |
| 27 | |
Subrata Banik | 56ab8e2 | 2022-01-07 13:40:19 +0000 | [diff] [blame] | 28 | bool cpu_soc_is_in_untrusted_mode(void) |
| 29 | { |
Subrata Banik | 37a55d1 | 2022-05-30 18:11:12 +0000 | [diff] [blame] | 30 | /* |
| 31 | * TODO: Add dynamic detection to identify if skylake SoC |
| 32 | * is in coffeelake board. |
| 33 | */ |
Subrata Banik | 56ab8e2 | 2022-01-07 13:40:19 +0000 | [diff] [blame] | 34 | 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 Banik | 37a55d1 | 2022-05-30 18:11:12 +0000 | [diff] [blame] | 42 | void 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 Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 54 | static void configure_misc(void) |
| 55 | { |
Kyösti Mälkki | d5f645c | 2019-09-28 00:20:27 +0300 | [diff] [blame] | 56 | config_t *conf = config_of_soc(); |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 57 | msr_t msr; |
| 58 | |
| 59 | msr = rdmsr(IA32_MISC_ENABLE); |
Lee Leahy | 1d14b3e | 2015-05-12 18:23:27 -0700 | [diff] [blame] | 60 | msr.lo |= (1 << 0); /* Fast String enable */ |
| 61 | msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */ |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 62 | wrmsr(IA32_MISC_ENABLE, msr); |
| 63 | |
Matt Delco | 54e9894 | 2020-03-09 12:41:09 -0700 | [diff] [blame] | 64 | /* Set EIST status */ |
| 65 | cpu_set_eist(conf->eist_enable); |
| 66 | |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 67 | /* 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 Prajapati | 79cfcde | 2016-03-08 12:34:06 -0800 | [diff] [blame] | 76 | |
Pratik Prajapati | 79cfcde | 2016-03-08 12:34:06 -0800 | [diff] [blame] | 77 | msr = rdmsr(MSR_POWER_CTL); |
Angel Pons | 4d794bd | 2021-10-11 14:00:54 +0200 | [diff] [blame] | 78 | msr.lo |= (1 << 0); /* Enable Bi-directional PROCHOT as an input */ |
Matthew Garrett | 13e7a2f | 2019-07-19 17:02:07 -0700 | [diff] [blame] | 79 | msr.lo |= (1 << 18); /* Enable Energy/Performance Bias control */ |
Cole Nelson | 63b6fea | 2018-06-15 15:51:54 -0700 | [diff] [blame] | 80 | msr.lo &= ~POWER_CTL_C1E_MASK; /* Disable C1E */ |
Pratik Prajapati | 79cfcde | 2016-03-08 12:34:06 -0800 | [diff] [blame] | 81 | msr.lo |= (1 << 23); /* Lock it */ |
| 82 | wrmsr(MSR_POWER_CTL, msr); |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Subrata Banik | 481b364 | 2017-05-12 11:29:43 +0530 | [diff] [blame] | 85 | static 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 Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 123 | /* All CPUs including BSP will run the following function. */ |
Elyes HAOUAS | 143fb46 | 2018-05-25 12:56:45 +0200 | [diff] [blame] | 124 | void soc_core_init(struct device *cpu) |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 125 | { |
Patrick Rudolph | fc36e9f | 2021-01-25 10:46:16 +0100 | [diff] [blame] | 126 | /* Configure Core PRMRR for SGX. */ |
| 127 | if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE)) |
| 128 | prmrr_core_configure(); |
| 129 | |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 130 | /* Clear out pending MCEs */ |
Pratik Prajapati | e816315 | 2017-08-28 12:27:57 -0700 | [diff] [blame] | 131 | /* 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 Banik | f91344c | 2019-05-06 19:23:26 +0530 | [diff] [blame] | 134 | mca_configure(); |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 135 | |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 136 | enable_lapic_tpr(); |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 137 | |
Subrata Banik | 481b364 | 2017-05-12 11:29:43 +0530 | [diff] [blame] | 138 | /* Configure c-state interrupt response time */ |
| 139 | configure_c_states(); |
| 140 | |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 141 | /* Configure Enhanced SpeedStep and Thermal Sensors */ |
| 142 | configure_misc(); |
| 143 | |
Michael Niewöhner | 6303243 | 2020-10-11 17:34:54 +0200 | [diff] [blame] | 144 | set_aesni_lock(); |
Michael Niewöhner | 7bdedcd | 2019-09-01 16:49:09 +0200 | [diff] [blame] | 145 | |
Subrata Banik | f004f66 | 2017-02-03 19:05:27 +0530 | [diff] [blame] | 146 | /* Enable ACPI Timer Emulation via MSR 0x121 */ |
| 147 | enable_pm_timer_emulation(); |
| 148 | |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 149 | /* 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 Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 157 | } |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 158 | |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 159 | static void per_cpu_smm_trigger(void) |
| 160 | { |
| 161 | /* Relocate the SMM handler. */ |
| 162 | smm_relocate(); |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 163 | } |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 164 | |
Angel Pons | 1b8e65d | 2021-02-19 18:29:58 +0100 | [diff] [blame] | 165 | void 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 Huber | 6275e34 | 2018-11-21 00:11:35 +0100 | [diff] [blame] | 177 | static void vmx_configure(void *unused) |
| 178 | { |
| 179 | set_feature_ctrl_vmx(); |
| 180 | } |
| 181 | |
| 182 | static void fc_lock_configure(void *unused) |
| 183 | { |
| 184 | set_feature_ctrl_lock(); |
| 185 | } |
| 186 | |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 187 | static void post_mp_init(void) |
| 188 | { |
Felix Held | 30f7031 | 2021-10-20 20:42:03 +0200 | [diff] [blame] | 189 | bool failure = false; |
Patrick Rudolph | be207b1 | 2019-07-26 14:22:09 +0200 | [diff] [blame] | 190 | |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 191 | /* Set Max Ratio */ |
Barnali Sarkar | 0a203d1 | 2017-05-04 18:02:17 +0530 | [diff] [blame] | 192 | cpu_set_max_ratio(); |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 193 | |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 194 | /* |
| 195 | * Now that all APs have been relocated as well as the BSP let SMIs |
| 196 | * start flowing. |
| 197 | */ |
Kyösti Mälkki | 040c531 | 2020-05-31 20:03:11 +0300 | [diff] [blame] | 198 | global_smi_enable_no_pwrbtn(); |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 199 | |
| 200 | /* Lock down the SMRAM space. */ |
Kyösti Mälkki | b490562 | 2019-07-12 08:02:35 +0300 | [diff] [blame] | 201 | if (CONFIG(HAVE_SMI_HANDLER)) |
| 202 | smm_lock(); |
Pratik Prajapati | 7a357eb | 2017-08-14 12:18:38 -0700 | [diff] [blame] | 203 | |
Felix Held | 82faefb | 2021-10-20 20:50:58 +0200 | [diff] [blame] | 204 | if (mp_run_on_all_cpus(vmx_configure, NULL) != CB_SUCCESS) |
Felix Held | 30f7031 | 2021-10-20 20:42:03 +0200 | [diff] [blame] | 205 | failure = true; |
Matt DeVillier | 969ef10 | 2018-03-21 20:47:52 -0500 | [diff] [blame] | 206 | |
Michael Niewöhner | 7736bfc | 2019-10-22 23:05:06 +0200 | [diff] [blame] | 207 | if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE)) |
Felix Held | 82faefb | 2021-10-20 20:50:58 +0200 | [diff] [blame] | 208 | if (mp_run_on_all_cpus(sgx_configure, NULL) != CB_SUCCESS) |
Felix Held | 30f7031 | 2021-10-20 20:42:03 +0200 | [diff] [blame] | 209 | failure = true; |
Nico Huber | 6275e34 | 2018-11-21 00:11:35 +0100 | [diff] [blame] | 210 | |
Felix Held | 82faefb | 2021-10-20 20:50:58 +0200 | [diff] [blame] | 211 | if (mp_run_on_all_cpus(fc_lock_configure, NULL) != CB_SUCCESS) |
Felix Held | 30f7031 | 2021-10-20 20:42:03 +0200 | [diff] [blame] | 212 | failure = true; |
Patrick Rudolph | be207b1 | 2019-07-26 14:22:09 +0200 | [diff] [blame] | 213 | |
Felix Held | 30f7031 | 2021-10-20 20:42:03 +0200 | [diff] [blame] | 214 | if (failure) |
Patrick Rudolph | be207b1 | 2019-07-26 14:22:09 +0200 | [diff] [blame] | 215 | printk(BIOS_CRIT, "CRITICAL ERROR: MP post init failed\n"); |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Arthur Heymans | 1999bc5 | 2021-02-16 13:20:13 +0100 | [diff] [blame] | 218 | static void soc_fsp_load(void) |
| 219 | { |
| 220 | fsps_load(); |
| 221 | } |
| 222 | |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 223 | static const struct mp_ops mp_ops = { |
Subrata Banik | a4b11e5c | 2017-02-03 18:57:49 +0530 | [diff] [blame] | 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 | */ |
Furquan Shaikh | c248044 | 2017-02-20 13:41:56 -0800 | [diff] [blame] | 229 | .pre_mp_init = soc_fsp_load, |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 230 | .get_cpu_count = get_cpu_count, |
| 231 | .get_smm_info = smm_info, |
| 232 | .get_microcode_info = get_microcode_info, |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 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 | |
Arthur Heymans | 829e8e6 | 2023-01-30 19:09:34 +0100 | [diff] [blame] | 239 | void mp_init_cpus(struct bus *cpu_bus) |
Aaron Durbin | 5822582 | 2016-05-03 17:45:59 -0500 | [diff] [blame] | 240 | { |
Felix Held | 4dd7d11 | 2021-10-20 23:31:43 +0200 | [diff] [blame] | 241 | /* TODO: Handle mp_init_with_smm failure? */ |
| 242 | mp_init_with_smm(cpu_bus, &mp_ops); |
Sumeet Pawnikar | 9d2f3de | 2016-12-22 13:48:46 +0530 | [diff] [blame] | 243 | |
| 244 | /* Thermal throttle activation offset */ |
Sumeet R Pawnikar | 360684b | 2020-06-18 15:56:11 +0530 | [diff] [blame] | 245 | configure_tcc_thermal_target(); |
Lee Leahy | b000513 | 2015-05-12 18:19:47 -0700 | [diff] [blame] | 246 | } |
Rizwan Qureshi | a7ff453 | 2015-07-23 22:40:53 +0530 | [diff] [blame] | 247 | |
| 248 | int soc_skip_ucode_update(u32 current_patch_id, u32 new_patch_id) |
| 249 | { |
Robbie Zhang | 7de0317 | 2017-02-21 14:00:31 -0800 | [diff] [blame] | 250 | msr_t msr1; |
| 251 | msr_t msr2; |
| 252 | |
| 253 | /* |
| 254 | * If PRMRR/SGX is supported the FIT microcode load will set the msr |
Rizwan Qureshi | a7ff453 | 2015-07-23 22:40:53 +0530 | [diff] [blame] | 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 |
Robbie Zhang | 7de0317 | 2017-02-21 14:00:31 -0800 | [diff] [blame] | 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. |
Rizwan Qureshi | a7ff453 | 2015-07-23 22:40:53 +0530 | [diff] [blame] | 261 | */ |
Robbie Zhang | 7de0317 | 2017-02-21 14:00:31 -0800 | [diff] [blame] | 262 | msr1 = rdmsr(MTRR_CAP_MSR); |
Elyes HAOUAS | f212cf3 | 2018-12-18 10:24:55 +0100 | [diff] [blame] | 263 | msr2 = rdmsr(MSR_PRMRR_PHYS_BASE); |
Robbie Zhang | 7de0317 | 2017-02-21 14:00:31 -0800 | [diff] [blame] | 264 | if (msr2.lo && (current_patch_id == new_patch_id - 1)) |
| 265 | return 0; |
| 266 | else |
Kyösti Mälkki | eadd251 | 2020-06-11 09:52:45 +0300 | [diff] [blame] | 267 | return (msr1.lo & MTRR_CAP_PRMRR) && |
Robbie Zhang | 7de0317 | 2017-02-21 14:00:31 -0800 | [diff] [blame] | 268 | (current_patch_id == new_patch_id - 1); |
Rizwan Qureshi | a7ff453 | 2015-07-23 22:40:53 +0530 | [diff] [blame] | 269 | } |