blob: 1647db48555efeb3bb6756e4f476a587cd17d72c [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>
8#include <cpu/x86/lapic.h>
9#include <cpu/x86/mp.h>
Nico Huber6275e342018-11-21 00:11:35 +010010#include <cpu/intel/common/common.h>
Lee Leahyb0005132015-05-12 18:19:47 -070011#include <cpu/intel/microcode.h>
12#include <cpu/intel/speedstep.h>
13#include <cpu/intel/turbo.h>
Lee Leahyb0005132015-05-12 18:19:47 -070014#include <cpu/x86/name.h>
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +030015#include <cpu/intel/smm_reloc.h>
Barnali Sarkar0a203d12017-05-04 18:02:17 +053016#include <intelblocks/cpulib.h>
Aaron Durbin93d5f402017-06-08 11:00:23 -050017#include <intelblocks/fast_spi.h>
Barnali Sarkar73273862017-06-13 20:22:33 +053018#include <intelblocks/mp_init.h>
Pratik Prajapatia04aa3d2017-06-12 23:02:36 -070019#include <intelblocks/sgx.h>
Lee Leahyb0005132015-05-12 18:19:47 -070020#include <soc/cpu.h>
21#include <soc/msr.h>
22#include <soc/pci_devs.h>
23#include <soc/ramstage.h>
Lee Leahyb0005132015-05-12 18:19:47 -070024#include <soc/systemagent.h>
Lee Leahyb0005132015-05-12 18:19:47 -070025
Elyes HAOUASc3385072019-03-21 15:38:06 +010026#include "chip.h"
27
Lee Leahyb0005132015-05-12 18:19:47 -070028static void configure_misc(void)
29{
Kyösti Mälkkid5f645c2019-09-28 00:20:27 +030030 config_t *conf = config_of_soc();
Lee Leahyb0005132015-05-12 18:19:47 -070031 msr_t msr;
32
33 msr = rdmsr(IA32_MISC_ENABLE);
Lee Leahy1d14b3e2015-05-12 18:23:27 -070034 msr.lo |= (1 << 0); /* Fast String enable */
35 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Lee Leahyb0005132015-05-12 18:19:47 -070036 wrmsr(IA32_MISC_ENABLE, msr);
37
Matt Delco54e98942020-03-09 12:41:09 -070038 /* Set EIST status */
39 cpu_set_eist(conf->eist_enable);
40
Lee Leahyb0005132015-05-12 18:19:47 -070041 /* Disable Thermal interrupts */
42 msr.lo = 0;
43 msr.hi = 0;
44 wrmsr(IA32_THERM_INTERRUPT, msr);
45
46 /* Enable package critical interrupt only */
47 msr.lo = 1 << 4;
48 msr.hi = 0;
49 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
Pratik Prajapati79cfcde2016-03-08 12:34:06 -080050
Pratik Prajapati79cfcde2016-03-08 12:34:06 -080051 msr = rdmsr(MSR_POWER_CTL);
52 msr.lo |= (1 << 0); /* Enable Bi-directional PROCHOT as an input*/
Matthew Garrett13e7a2f2019-07-19 17:02:07 -070053 msr.lo |= (1 << 18); /* Enable Energy/Performance Bias control */
Cole Nelson63b6fea2018-06-15 15:51:54 -070054 msr.lo &= ~POWER_CTL_C1E_MASK; /* Disable C1E */
Pratik Prajapati79cfcde2016-03-08 12:34:06 -080055 msr.lo |= (1 << 23); /* Lock it */
56 wrmsr(MSR_POWER_CTL, msr);
Lee Leahyb0005132015-05-12 18:19:47 -070057}
58
Subrata Banik481b3642017-05-12 11:29:43 +053059static void configure_c_states(void)
60{
61 msr_t msr;
62
63 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
64 msr.hi = 0;
65 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
66 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
67
68 /* C-state Interrupt Response Latency Control 1 - package C6/C7 short */
69 msr.hi = 0;
70 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
71 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
72
73 /* C-state Interrupt Response Latency Control 2 - package C6/C7 long */
74 msr.hi = 0;
75 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
76 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
77
78 /* C-state Interrupt Response Latency Control 3 - package C8 */
79 msr.hi = 0;
80 msr.lo = IRTL_VALID | IRTL_1024_NS |
81 C_STATE_LATENCY_CONTROL_3_LIMIT;
82 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
83
84 /* C-state Interrupt Response Latency Control 4 - package C9 */
85 msr.hi = 0;
86 msr.lo = IRTL_VALID | IRTL_1024_NS |
87 C_STATE_LATENCY_CONTROL_4_LIMIT;
88 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
89
90 /* C-state Interrupt Response Latency Control 5 - package C10 */
91 msr.hi = 0;
92 msr.lo = IRTL_VALID | IRTL_1024_NS |
93 C_STATE_LATENCY_CONTROL_5_LIMIT;
94 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
95}
96
Lee Leahyb0005132015-05-12 18:19:47 -070097/* All CPUs including BSP will run the following function. */
Elyes HAOUAS143fb462018-05-25 12:56:45 +020098void soc_core_init(struct device *cpu)
Lee Leahyb0005132015-05-12 18:19:47 -070099{
100 /* Clear out pending MCEs */
Pratik Prajapatie8163152017-08-28 12:27:57 -0700101 /* TODO(adurbin): This should only be done on a cold boot. Also, some
102 * of these banks are core vs package scope. For now every CPU clears
103 * every bank. */
Subrata Banikf91344c2019-05-06 19:23:26 +0530104 mca_configure();
Lee Leahyb0005132015-05-12 18:19:47 -0700105
Elyes HAOUAS038e7242016-07-29 18:31:16 +0200106 /* Enable the local CPU apics */
Lee Leahyb0005132015-05-12 18:19:47 -0700107 enable_lapic_tpr();
108 setup_lapic();
109
Subrata Banik481b3642017-05-12 11:29:43 +0530110 /* Configure c-state interrupt response time */
111 configure_c_states();
112
Lee Leahyb0005132015-05-12 18:19:47 -0700113 /* Configure Enhanced SpeedStep and Thermal Sensors */
114 configure_misc();
115
Michael Niewöhner63032432020-10-11 17:34:54 +0200116 set_aesni_lock();
Michael Niewöhner7bdedcd2019-09-01 16:49:09 +0200117
Subrata Banikf004f662017-02-03 19:05:27 +0530118 /* Enable ACPI Timer Emulation via MSR 0x121 */
119 enable_pm_timer_emulation();
120
Lee Leahyb0005132015-05-12 18:19:47 -0700121 /* Enable Direct Cache Access */
122 configure_dca_cap();
123
124 /* Set energy policy */
125 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
126
127 /* Enable Turbo */
128 enable_turbo();
Robbie Zhang7de03172017-02-21 14:00:31 -0800129
Pratik Prajapati7a357eb2017-08-14 12:18:38 -0700130 /* Configure Core PRMRR for SGX. */
Michael Niewöhner7736bfc2019-10-22 23:05:06 +0200131 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
Michael Niewöhner6e66d7b2019-10-08 12:00:24 +0200132 prmrr_core_configure();
Aaron Durbin58225822016-05-03 17:45:59 -0500133}
Lee Leahyb0005132015-05-12 18:19:47 -0700134
Aaron Durbin58225822016-05-03 17:45:59 -0500135static void per_cpu_smm_trigger(void)
136{
137 /* Relocate the SMM handler. */
138 smm_relocate();
Aaron Durbin58225822016-05-03 17:45:59 -0500139}
Lee Leahyb0005132015-05-12 18:19:47 -0700140
Nico Huber6275e342018-11-21 00:11:35 +0100141static void vmx_configure(void *unused)
142{
143 set_feature_ctrl_vmx();
144}
145
146static void fc_lock_configure(void *unused)
147{
148 set_feature_ctrl_lock();
149}
150
Aaron Durbin58225822016-05-03 17:45:59 -0500151static void post_mp_init(void)
152{
Patrick Rudolphbe207b12019-07-26 14:22:09 +0200153 int ret = 0;
154
Lee Leahyb0005132015-05-12 18:19:47 -0700155 /* Set Max Ratio */
Barnali Sarkar0a203d12017-05-04 18:02:17 +0530156 cpu_set_max_ratio();
Lee Leahyb0005132015-05-12 18:19:47 -0700157
Aaron Durbin58225822016-05-03 17:45:59 -0500158 /*
159 * Now that all APs have been relocated as well as the BSP let SMIs
160 * start flowing.
161 */
Kyösti Mälkki040c5312020-05-31 20:03:11 +0300162 global_smi_enable_no_pwrbtn();
Aaron Durbin58225822016-05-03 17:45:59 -0500163
164 /* Lock down the SMRAM space. */
Kyösti Mälkkib4905622019-07-12 08:02:35 +0300165 if (CONFIG(HAVE_SMI_HANDLER))
166 smm_lock();
Pratik Prajapati7a357eb2017-08-14 12:18:38 -0700167
Patrick Rudolph5ec97ce2019-07-26 14:47:32 +0200168 ret |= mp_run_on_all_cpus(vmx_configure, NULL);
Matt DeVillier969ef102018-03-21 20:47:52 -0500169
Michael Niewöhner7736bfc2019-10-22 23:05:06 +0200170 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE))
Michael Niewöhner6e66d7b2019-10-08 12:00:24 +0200171 ret |= mp_run_on_all_cpus(sgx_configure, NULL);
Nico Huber6275e342018-11-21 00:11:35 +0100172
Patrick Rudolph5ec97ce2019-07-26 14:47:32 +0200173 ret |= mp_run_on_all_cpus(fc_lock_configure, NULL);
Patrick Rudolphbe207b12019-07-26 14:22:09 +0200174
175 if (ret)
176 printk(BIOS_CRIT, "CRITICAL ERROR: MP post init failed\n");
Aaron Durbin58225822016-05-03 17:45:59 -0500177}
178
179static const struct mp_ops mp_ops = {
Subrata Banika4b11e5c2017-02-03 18:57:49 +0530180 /*
181 * Skip Pre MP init MTRR programming as MTRRs are mirrored from BSP,
182 * that are set prior to ramstage.
183 * Real MTRRs programming are being done after resource allocation.
184 */
Furquan Shaikhc2480442017-02-20 13:41:56 -0800185 .pre_mp_init = soc_fsp_load,
Aaron Durbin58225822016-05-03 17:45:59 -0500186 .get_cpu_count = get_cpu_count,
187 .get_smm_info = smm_info,
188 .get_microcode_info = get_microcode_info,
Aaron Durbin58225822016-05-03 17:45:59 -0500189 .pre_mp_smm_init = smm_initialize,
190 .per_cpu_smm_trigger = per_cpu_smm_trigger,
191 .relocation_handler = smm_relocation_handler,
192 .post_mp_init = post_mp_init,
193};
194
Pratik Prajapati9cd6a262017-08-14 13:57:46 -0700195void soc_init_cpus(struct bus *cpu_bus)
Aaron Durbin58225822016-05-03 17:45:59 -0500196{
Lee Leahyf4c4ab92017-03-16 17:08:03 -0700197 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin58225822016-05-03 17:45:59 -0500198 printk(BIOS_ERR, "MP initialization failure.\n");
Sumeet Pawnikar9d2f3de2016-12-22 13:48:46 +0530199
200 /* Thermal throttle activation offset */
Sumeet R Pawnikar360684b2020-06-18 15:56:11 +0530201 configure_tcc_thermal_target();
Lee Leahyb0005132015-05-12 18:19:47 -0700202}
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530203
204int soc_skip_ucode_update(u32 current_patch_id, u32 new_patch_id)
205{
Robbie Zhang7de03172017-02-21 14:00:31 -0800206 msr_t msr1;
207 msr_t msr2;
208
209 /*
210 * If PRMRR/SGX is supported the FIT microcode load will set the msr
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530211 * 0x08b with the Patch revision id one less than the id in the
212 * microcode binary. The PRMRR support is indicated in the MSR
Robbie Zhang7de03172017-02-21 14:00:31 -0800213 * MTRRCAP[12]. If SGX is not enabled, check and avoid reloading the
214 * same microcode during CPU initialization. If SGX is enabled, as
215 * part of SGX BIOS initialization steps, the same microcode needs to
216 * be reloaded after the core PRMRR MSRs are programmed.
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530217 */
Robbie Zhang7de03172017-02-21 14:00:31 -0800218 msr1 = rdmsr(MTRR_CAP_MSR);
Elyes HAOUASf212cf32018-12-18 10:24:55 +0100219 msr2 = rdmsr(MSR_PRMRR_PHYS_BASE);
Robbie Zhang7de03172017-02-21 14:00:31 -0800220 if (msr2.lo && (current_patch_id == new_patch_id - 1))
221 return 0;
222 else
Kyösti Mälkkieadd2512020-06-11 09:52:45 +0300223 return (msr1.lo & MTRR_CAP_PRMRR) &&
Robbie Zhang7de03172017-02-21 14:00:31 -0800224 (current_patch_id == new_patch_id - 1);
Rizwan Qureshia7ff4532015-07-23 22:40:53 +0530225}