blob: 4f6720850ef43f6cc85dc098496e367bebd21c40 [file] [log] [blame]
Angel Ponsf5627e82020-04-05 15:46:52 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Pratik Prajapati01eda282017-08-17 21:09:45 -07002
3#include <console/console.h>
4#include <device/pci.h>
Pratik Prajapati01eda282017-08-17 21:09:45 -07005#include <cpu/x86/lapic.h>
6#include <cpu/x86/mp.h>
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +02007#include <cpu/x86/msr.h>
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +03008#include <cpu/intel/smm_reloc.h>
Pratik Prajapati01eda282017-08-17 21:09:45 -07009#include <cpu/intel/turbo.h>
10#include <intelblocks/cpulib.h>
11#include <intelblocks/mp_init.h>
Pratik Prajapati01eda282017-08-17 21:09:45 -070012#include <soc/cpu.h>
13#include <soc/msr.h>
14#include <soc/pci_devs.h>
Sumeet Pawnikarc896e92e2019-01-08 19:52:54 +053015#include <soc/systemagent.h>
Ronak Kanabar69a95652019-02-19 20:10:23 +053016#include <cpu/x86/mtrr.h>
17#include <cpu/intel/microcode.h>
Ronak Kanabara432f382019-03-16 21:26:43 +053018#include <cpu/intel/common/common.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020019#include <types.h>
Sumeet Pawnikarc896e92e2019-01-08 19:52:54 +053020
Elyes HAOUASc3385072019-03-21 15:38:06 +010021#include "chip.h"
22
Pratik Prajapati01eda282017-08-17 21:09:45 -070023static void soc_fsp_load(void)
24{
Kyösti Mälkkicc93c6e2021-01-09 22:53:52 +020025 fsps_load();
Pratik Prajapati01eda282017-08-17 21:09:45 -070026}
27
Pratik Prajapati01eda282017-08-17 21:09:45 -070028static void configure_misc(void)
29{
Pratik Prajapati01eda282017-08-17 21:09:45 -070030 msr_t msr;
31
Angel Ponsbda02b02020-09-28 01:10:40 +020032 config_t *conf = config_of_soc();
33
Pratik Prajapati01eda282017-08-17 21:09:45 -070034 msr = rdmsr(IA32_MISC_ENABLE);
35 msr.lo |= (1 << 0); /* Fast String enable */
36 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Matt Delco54e98942020-03-09 12:41:09 -070037 wrmsr(IA32_MISC_ENABLE, msr);
38
Subrata Banik6d569162019-04-10 12:19:27 +053039 /* Set EIST status */
40 cpu_set_eist(conf->eist_enable);
Pratik Prajapati01eda282017-08-17 21:09:45 -070041
42 /* Disable Thermal interrupts */
43 msr.lo = 0;
44 msr.hi = 0;
45 wrmsr(IA32_THERM_INTERRUPT, msr);
46
47 /* Enable package critical interrupt only */
48 msr.lo = 1 << 4;
49 msr.hi = 0;
50 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
51
52 /* Enable PROCHOT */
53 msr = rdmsr(MSR_POWER_CTL);
Angel Pons4d794bd2021-10-11 14:00:54 +020054 msr.lo |= (1 << 0); /* Enable Bi-directional PROCHOT as an input */
Angel Ponsa0f8dc32021-10-11 14:01:55 +020055 msr.lo |= (1 << 18); /* Enable Energy/Performance Bias control */
Pratik Prajapati01eda282017-08-17 21:09:45 -070056 msr.lo |= (1 << 23); /* Lock it */
57 wrmsr(MSR_POWER_CTL, msr);
58}
59
Nico Huber234e7ec2021-07-27 10:26:31 +000060static void configure_c_states(const config_t *const cfg)
Pratik Prajapati01eda282017-08-17 21:09:45 -070061{
62 msr_t msr;
63
Nico Huber234e7ec2021-07-27 10:26:31 +000064 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
65 if (cfg->max_package_c_state && (msr.lo & 0xf) >= cfg->max_package_c_state) {
66 msr.lo = (msr.lo & ~0xf) | ((cfg->max_package_c_state - 1) & 0xf);
Nico Huber234e7ec2021-07-27 10:26:31 +000067 }
Angel Pons0c7a2502021-10-11 13:53:15 +020068 msr.lo |= CST_CFG_LOCK_MASK;
69 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Nico Huber234e7ec2021-07-27 10:26:31 +000070
Nico Huber327c04a2021-07-26 13:34:59 +000071 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
72 msr.hi = 0;
73 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
74 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
75
Pratik Prajapati01eda282017-08-17 21:09:45 -070076 /* C-state Interrupt Response Latency Control 1 - package C6/C7 short */
77 msr.hi = 0;
Nico Huber327c04a2021-07-26 13:34:59 +000078 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
Pratik Prajapati01eda282017-08-17 21:09:45 -070079 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
80
81 /* C-state Interrupt Response Latency Control 2 - package C6/C7 long */
82 msr.hi = 0;
Nico Huber327c04a2021-07-26 13:34:59 +000083 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
Pratik Prajapati01eda282017-08-17 21:09:45 -070084 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
85
86 /* C-state Interrupt Response Latency Control 3 - package C8 */
87 msr.hi = 0;
Nico Huberad5b8b82021-07-26 13:43:24 +000088 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
Pratik Prajapati01eda282017-08-17 21:09:45 -070089 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
90
91 /* C-state Interrupt Response Latency Control 4 - package C9 */
92 msr.hi = 0;
Nico Huberad5b8b82021-07-26 13:43:24 +000093 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
Pratik Prajapati01eda282017-08-17 21:09:45 -070094 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
95
96 /* C-state Interrupt Response Latency Control 5 - package C10 */
97 msr.hi = 0;
Nico Huberad5b8b82021-07-26 13:43:24 +000098 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
Pratik Prajapati01eda282017-08-17 21:09:45 -070099 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
100}
101
102/* All CPUs including BSP will run the following function. */
Elyes HAOUAS3c8b5d02018-05-27 16:57:24 +0200103void soc_core_init(struct device *cpu)
Pratik Prajapati01eda282017-08-17 21:09:45 -0700104{
Patrick Rudolphe9b08302021-02-16 11:52:38 +0100105 config_t *cfg = config_of_soc();
106
Pratik Prajapati01eda282017-08-17 21:09:45 -0700107 /* Clear out pending MCEs */
Pratik Prajapati2ad1ddb2017-08-28 12:28:24 -0700108 /* TODO(adurbin): This should only be done on a cold boot. Also, some
109 * of these banks are core vs package scope. For now every CPU clears
110 * every bank. */
Subrata Banikf91344c2019-05-06 19:23:26 +0530111 mca_configure();
Pratik Prajapati01eda282017-08-17 21:09:45 -0700112
113 /* Enable the local CPU apics */
114 enable_lapic_tpr();
115 setup_lapic();
116
117 /* Configure c-state interrupt response time */
Nico Huber234e7ec2021-07-27 10:26:31 +0000118 configure_c_states(cfg);
Pratik Prajapati01eda282017-08-17 21:09:45 -0700119
120 /* Configure Enhanced SpeedStep and Thermal Sensors */
121 configure_misc();
122
Michael Niewöhner5611cfd2020-10-11 13:04:02 +0200123 set_aesni_lock();
124
Lijian Zhao0f5d7b92018-10-05 10:31:11 -0700125 enable_pm_timer_emulation();
126
Pratik Prajapati01eda282017-08-17 21:09:45 -0700127 /* Enable Direct Cache Access */
128 configure_dca_cap();
129
130 /* Set energy policy */
131 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
132
Patrick Rudolphe9b08302021-02-16 11:52:38 +0100133 if (cfg->cpu_turbo_disable)
134 disable_turbo();
135 else
136 enable_turbo();
Ronak Kanabara432f382019-03-16 21:26:43 +0530137
138 /* Enable Vmx */
139 set_vmx_and_lock();
Lijian Zhaof0eb9992017-09-14 14:51:12 -0700140}
Pratik Prajapati01eda282017-08-17 21:09:45 -0700141
Lijian Zhaof0eb9992017-09-14 14:51:12 -0700142static void per_cpu_smm_trigger(void)
143{
144 /* Relocate the SMM handler. */
145 smm_relocate();
Pratik Prajapati01eda282017-08-17 21:09:45 -0700146}
147
Angel Pons1b8e65d2021-02-19 18:29:58 +0100148void smm_lock(void)
149{
150 struct device *sa_dev = pcidev_path_on_root(SA_DEVFN_ROOT);
151 /*
152 * LOCK the SMM memory window and enable normal SMM.
153 * After running this function, only a full reset can
154 * make the SMM registers writable again.
155 */
156 printk(BIOS_DEBUG, "Locking SMM.\n");
157 pci_write_config8(sa_dev, SMRAM, D_LCK | G_SMRAME | C_BASE_SEG);
158}
159
Pratik Prajapati01eda282017-08-17 21:09:45 -0700160static void post_mp_init(void)
161{
162 /* Set Max Ratio */
163 cpu_set_max_ratio();
Lijian Zhaof0eb9992017-09-14 14:51:12 -0700164
165 /*
166 * Now that all APs have been relocated as well as the BSP let SMIs
167 * start flowing.
168 */
Kyösti Mälkki040c5312020-05-31 20:03:11 +0300169 global_smi_enable_no_pwrbtn();
Lijian Zhaof0eb9992017-09-14 14:51:12 -0700170
171 /* Lock down the SMRAM space. */
172 smm_lock();
Pratik Prajapati01eda282017-08-17 21:09:45 -0700173}
174
175static const struct mp_ops mp_ops = {
176 /*
177 * Skip Pre MP init MTRR programming as MTRRs are mirrored from BSP,
178 * that are set prior to ramstage.
179 * Real MTRRs programming are being done after resource allocation.
180 */
181 .pre_mp_init = soc_fsp_load,
182 .get_cpu_count = get_cpu_count,
Lijian Zhaof0eb9992017-09-14 14:51:12 -0700183 .get_smm_info = smm_info,
Pratik Prajapati01eda282017-08-17 21:09:45 -0700184 .get_microcode_info = get_microcode_info,
Lijian Zhaof0eb9992017-09-14 14:51:12 -0700185 .pre_mp_smm_init = smm_initialize,
186 .per_cpu_smm_trigger = per_cpu_smm_trigger,
187 .relocation_handler = smm_relocation_handler,
Pratik Prajapati01eda282017-08-17 21:09:45 -0700188 .post_mp_init = post_mp_init,
189};
190
191void soc_init_cpus(struct bus *cpu_bus)
192{
Felix Held4dd7d112021-10-20 23:31:43 +0200193 /* TODO: Handle mp_init_with_smm failure? */
194 mp_init_with_smm(cpu_bus, &mp_ops);
John Su31269642019-01-10 14:53:26 +0800195
196 /* Thermal throttle activation offset */
Sumeet R Pawnikar360684b2020-06-18 15:56:11 +0530197 configure_tcc_thermal_target();
Pratik Prajapati01eda282017-08-17 21:09:45 -0700198}
Ronak Kanabar69a95652019-02-19 20:10:23 +0530199
200int soc_skip_ucode_update(u32 current_patch_id, u32 new_patch_id)
201{
202 msr_t msr1;
203 msr_t msr2;
204
205 /*
Ronak Kanabar69a95652019-02-19 20:10:23 +0530206 * If PRMRR/SGX is supported the FIT microcode load will set the msr
207 * 0x08b with the Patch revision id one less than the id in the
208 * microcode binary. The PRMRR support is indicated in the MSR
209 * MTRRCAP[12]. If SGX is not enabled, check and avoid reloading the
210 * same microcode during CPU initialization. If SGX is enabled, as
211 * part of SGX BIOS initialization steps, the same microcode needs to
212 * be reloaded after the core PRMRR MSRs are programmed.
213 */
214 msr1 = rdmsr(MTRR_CAP_MSR);
215 msr2 = rdmsr(MSR_PRMRR_PHYS_BASE);
216 if (msr2.lo && (current_patch_id == new_patch_id - 1))
217 return 0;
218
Kyösti Mälkkieadd2512020-06-11 09:52:45 +0300219 return (msr1.lo & MTRR_CAP_PRMRR) &&
Ronak Kanabar69a95652019-02-19 20:10:23 +0530220 (current_patch_id == new_patch_id - 1);
221}