Debian Codegraph @

src/soc/intel/xeon_sp/spr/cpu.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/debug.h>
4 #include <cpu/intel/common/common.h>
5 #include <cpu/intel/microcode.h>
6 #include <cpu/intel/smm_reloc.h>
7 #include <cpu/intel/turbo.h>
8 #include <cpu/x86/mp.h>
9 #include <cpu/x86/topology.h>
10 #include <intelblocks/cpulib.h>
11 #include <intelblocks/mp_init.h>
12 #include <intelpch/lockdown.h>
13 #include <soc/msr.h>
14 #include <soc/pm.h>
15 #include <soc/smmrelocate.h>
16 #include <soc/util.h>
17
18 #include "chip.h"
19
20 static const void *microcode_patch;
21
22 static const config_t *chip_config = NULL;
23
24 bool cpu_soc_is_in_untrusted_mode(void)
25 {
26 return false;
27 }
28
29 void cpu_soc_bios_done(void)
30 {
31 }
32
33 static void xeon_configure_mca(void)
34 {
35 msr_t msr;
36 struct cpuid_result cpuid_regs;
37
38 /*
39 * Check feature flag in CPUID.(EAX=1):EDX[7]==1 MCE
40 * and CPUID.(EAX=1):EDX[14]==1 MCA
41 */
42 cpuid_regs = cpuid(1);
43 if ((cpuid_regs.edx & (1 << 7 | 1 << 14)) != (1 << 7 | 1 << 14))
44 return;
45
46 msr = rdmsr(IA32_MCG_CAP);
47 if (msr.lo & IA32_MCG_CAP_CTL_P_MASK) {
48 /* Enable all error logging */
49 msr.lo = msr.hi = 0xffffffff;
50 wrmsr(IA32_MCG_CTL, msr);
51 }
52
53 mca_configure();
54 }
55
56 /*
57 * On server platforms the FIT mechanism only updates the microcode on
58 * the BSP. Loading MCU on AP in parallel seems to fail in 10% of the cases
59 * so do it serialized.
60 */
61 void get_microcode_info(const void **microcode, int *parallel)
62 {
63 *microcode = intel_microcode_find();
64 *parallel = 0;
65 }
66
67 static void each_cpu_init(struct device *cpu)
68 {
69 msr_t msr;
70
71 printk(BIOS_SPEW, "%s dev: %s, cpu: %lu, apic_id: 0x%x, package_id: 0x%x\n",
72 __func__, dev_path(cpu), cpu_index(), cpu->path.apic.apic_id,
73 cpu->path.apic.package_id);
74
75 assert (cpu->path.apic.node_id < CONFIG_MAX_SOCKET);
76
77 /*
78 * Enable PWR_PERF_PLTFRM_OVR and PROCHOT_LOCK.
79 * The value set by FSP is 20_005f, we set it to 1a_00a4_005b.
80 */
81 msr = rdmsr(MSR_POWER_CTL);
82 msr.lo |= (0x16 << RESERVED1_SHIFT) | PWR_PERF_PLTFRM_OVR | PROCHOT_LOCK;
83 msr.hi = 0x1a;
84 wrmsr(MSR_POWER_CTL, msr);
85
86 /* Set static, idle, dynamic load line impedance */
87 msr = rdmsr(MSR_VR_MISC_CONFIG);
88 msr.lo = 0x1a1a1a;
89 wrmsr(MSR_VR_MISC_CONFIG, msr);
90
91 /* Set current limitation */
92 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
93 msr.lo = 0x1130;
94 msr.lo |= CURRENT_LIMIT_LOCK;
95 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
96
97 /* Set Turbo Ratio Limits */
98 if (chip_config->turbo_ratio_limit) {
99 msr.lo = chip_config->turbo_ratio_limit & 0xffffffff;
100 msr.hi = (chip_config->turbo_ratio_limit >> 32) & 0xffffffff;
101 wrmsr(MSR_TURBO_RATIO_LIMIT, msr);
102 }
103
104 /* Set Turbo Ratio Limit Cores */
105 if (chip_config->turbo_ratio_limit_cores) {
106 msr.lo = chip_config->turbo_ratio_limit_cores & 0xffffffff;
107 msr.hi = (chip_config->turbo_ratio_limit_cores >> 32) & 0xffffffff;
108 wrmsr(MSR_TURBO_RATIO_LIMIT_CORES, msr);
109 }
110
111 /* Set energy policy */
112 msr = rdmsr(MSR_ENERGY_PERF_BIAS_CONFIG);
113 msr.lo = 0x178fa038;
114 wrmsr(MSR_ENERGY_PERF_BIAS_CONFIG, msr);
115
116 msr.hi = 0x158d20;
117 msr.lo = 0x00158af0;
118 wrmsr(PACKAGE_RAPL_LIMIT, msr);
119
120 /*
121 * Set HWP base feature, EPP reg enumeration, lock thermal and msr
122 * This is package level MSR. Need to check if it updates correctly on
123 * multi-socket platform.
124 */
125 msr = rdmsr(MSR_MISC_PWR_MGMT);
126 if (!(msr.lo & LOCK_MISC_PWR_MGMT_MSR)) { /* if already locked skip update */
127 msr.lo = (HWP_ENUM_ENABLE | HWP_EPP_ENUM_ENABLE | LOCK_MISC_PWR_MGMT_MSR
128 | LOCK_THERM_INT);
129 wrmsr(MSR_MISC_PWR_MGMT, msr);
130 }
131
132 /* Enable Fast Strings */
133 msr = rdmsr(IA32_MISC_ENABLE);
134 msr.lo |= FAST_STRINGS_ENABLE_BIT;
135 wrmsr(IA32_MISC_ENABLE, msr);
136 /* Enable Turbo */
137 enable_turbo();
138
139 /* Enable speed step. */
140 if (get_turbo_state() == TURBO_ENABLED) {
141 msr = rdmsr(IA32_MISC_ENABLE);
142 msr.lo |= SPEED_STEP_ENABLE_BIT;
143 wrmsr(IA32_MISC_ENABLE, msr);
144 }
145
146 /* Lock the supported Cstates */
147 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
148 msr.lo |= CST_CFG_LOCK_MASK;
149 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
150
151 /* Disable all writes to overclocking limits MSR */
152 msr = rdmsr(MSR_FLEX_RATIO);
153 msr.lo |= MSR_FLEX_RATIO_OC_LOCK;
154 wrmsr(MSR_FLEX_RATIO, msr);
155
156 /* Lock Power Plane Limit MSR */
157 msr = rdmsr(MSR_DRAM_PLANE_POWER_LIMIT);
158 msr.hi |= MSR_HI_PP_PWR_LIM_LOCK;
159 wrmsr(MSR_DRAM_PLANE_POWER_LIMIT, msr);
160
161 /* Clear out pending MCEs */
162 xeon_configure_mca();
163
164 /* Enable Vmx */
165 // set_vmx_and_lock();
166 /* only lock. let vmx enable by FSP */
167 set_feature_ctrl_lock();
168 }
169
170 static struct device_operations cpu_dev_ops = {
171 .init = each_cpu_init,
172 };
173
174 static const struct cpu_device_id cpu_table[] = {
175 {X86_VENDOR_INTEL, CPUID_SAPPHIRERAPIDS_SP_D, CPUID_EXACT_MATCH_MASK},
176 {X86_VENDOR_INTEL, CPUID_SAPPHIRERAPIDS_SP_E0, CPUID_EXACT_MATCH_MASK},
177 {X86_VENDOR_INTEL, CPUID_SAPPHIRERAPIDS_SP_E2, CPUID_EXACT_MATCH_MASK},
178 {X86_VENDOR_INTEL, CPUID_SAPPHIRERAPIDS_SP_E3, CPUID_EXACT_MATCH_MASK},
179 {X86_VENDOR_INTEL, CPUID_SAPPHIRERAPIDS_SP_E4, CPUID_EXACT_MATCH_MASK},
180 {X86_VENDOR_INTEL, CPUID_SAPPHIRERAPIDS_SP_Ex, CPUID_EXACT_MATCH_MASK},
181 {X86_VENDOR_INTEL, CPUID_EMERALDRAPIDS, CPUID_EXACT_MATCH_MASK},
182 CPU_TABLE_END
183 };
184
185 static const struct cpu_driver driver __cpu_driver = {
186 .ops = &cpu_dev_ops,
187 .id_table = cpu_table,
188 };
189
190 static void set_max_turbo_freq(void)
191 {
192 msr_t msr, perf_ctl;
193
194 FUNC_ENTER();
195 perf_ctl.hi = 0;
196
197 /* Check for configurable TDP option */
198 if (get_turbo_state() == TURBO_ENABLED) {
199 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
200 perf_ctl.lo = (msr.lo & 0xff) << 8;
201 } else if (cpu_config_tdp_levels()) {
202 /* Set to nominal TDP ratio */
203 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
204 perf_ctl.lo = (msr.lo & 0xff) << 8;
205 } else {
206 /* Platform Info bits 15:8 give max ratio */
207 msr = rdmsr(MSR_PLATFORM_INFO);
208 perf_ctl.lo = msr.lo & 0xff00;
209 }
210 wrmsr(IA32_PERF_CTL, perf_ctl);
211
212 printk(BIOS_DEBUG, "cpu: frequency set to %d\n",
213 ((perf_ctl.lo >> 8) & 0xff) * CONFIG_CPU_BCLK_MHZ);
214 FUNC_EXIT();
215 }
216
217 /*
218 * Do essential initialization tasks before APs can be fired up
219 */
220 static void pre_mp_init(void)
221 {
222 x86_setup_mtrrs_with_detect();
223 x86_mtrr_check();
224 }
225
226 static void post_mp_init(void)
227 {
228 /* Set Max Ratio */
229 set_max_turbo_freq();
230
231 if (CONFIG(HAVE_SMI_HANDLER)) {
232 global_smi_enable();
233 if (get_lockdown_config() == CHIPSET_LOCKDOWN_COREBOOT)
234 pmc_lock_smi();
235 }
236 }
237
238 static const struct mp_ops mp_ops = {
239 .pre_mp_init = pre_mp_init,
240 .get_cpu_count = get_platform_thread_count,
241 #if CONFIG(HAVE_SMI_HANDLER)
242 .get_smm_info = get_smm_info,
243 .pre_mp_smm_init = smm_southbridge_clear_state,
244 .relocation_handler = smm_relocation_handler,
245 #endif
246 .get_microcode_info = get_microcode_info,
247 .post_mp_init = post_mp_init,
248 };
249
250 void mp_init_cpus(struct bus *bus)
251 {
252 /*
253 * chip_config is used in cpu device callback. Other than cpu 0,
254 * rest of the CPU devices do not have chip_info updated.
255 */
256 chip_config = bus->dev->chip_info;
257
258 microcode_patch = intel_microcode_find();
259 intel_microcode_load_unlocked(microcode_patch);
260
261 enum cb_err ret = mp_init_with_smm(bus, &mp_ops);
262 if (ret != CB_SUCCESS)
263 printk(BIOS_ERR, "MP initialization failure %d.\n", ret);
264 }