Debian Codegraph @

src/soc/intel/common/block/cpu/cpulib.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpigen.h>
4 #include <assert.h>
5 #include <console/console.h>
6 #include <cpu/cpu.h>
7 #include <cpu/intel/common/common.h>
8 #include <cpu/intel/turbo.h>
9 #include <cpu/x86/msr.h>
10 #include <cpu/x86/mtrr.h>
11 #include <intelblocks/cpulib.h>
12 #include <intelblocks/fast_spi.h>
13 #include <intelblocks/msr.h>
14 #include <smp/node.h>
15 #include <soc/soc_chip.h>
16 #include <static.h>
17 #include <types.h>
18
19 #define CPUID_PROCESSOR_FREQUENCY 0X16
20 #define CPUID_HYBRID_INFORMATION 0x1a
21
22 /* Structured Extended Feature Flags */
23 #define HYBRID_FEATURE BIT(15)
24
25 /*
26 * Set PERF_CTL MSR (0x199) P_Req with
27 * Turbo Ratio which is the Maximum Ratio.
28 */
29 void cpu_set_max_ratio(void)
30 {
31 /* Check for configurable TDP option */
32 if (get_turbo_state() == TURBO_ENABLED)
33 cpu_set_p_state_to_turbo_ratio();
34 }
35
36 /*
37 * Get the TDP Nominal Ratio from MSR 0x648 Bits 7:0.
38 */
39 u8 cpu_get_tdp_nominal_ratio(void)
40 {
41 u8 nominal_ratio;
42 msr_t msr;
43
44 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
45 nominal_ratio = msr.lo & 0xff;
46 return nominal_ratio;
47 }
48
49 /*
50 * Read PLATFORM_INFO MSR (0xCE).
51 * Return Value of Bit 34:33 (CONFIG_TDP_LEVELS).
52 *
53 * Possible values of Bit 34:33 are -
54 * 00 : Config TDP not supported
55 * 01 : One Additional TDP level supported
56 * 10 : Two Additional TDP level supported
57 * 11 : Reserved
58 */
59 int cpu_config_tdp_levels(void)
60 {
61 msr_t platform_info;
62
63 /* Bits 34:33 indicate how many levels supported */
64 platform_info = rdmsr(MSR_PLATFORM_INFO);
65 return (platform_info.hi >> 1) & 3;
66 }
67
68 static void set_perf_control_msr(msr_t msr)
69 {
70 wrmsr(IA32_PERF_CTL, msr);
71 printk(BIOS_DEBUG, "CPU: frequency set to %d MHz\n",
72 ((msr.lo >> 8) & 0xff) * CONFIG_CPU_BCLK_MHZ);
73 }
74
75 /*
76 * TURBO_RATIO_LIMIT MSR (0x1AD) Bits 31:0 indicates the
77 * factory configured values for of 1-core, 2-core, 3-core
78 * and 4-core turbo ratio limits for all processors.
79 *
80 * 7:0 - MAX_TURBO_1_CORE
81 * 15:8 - MAX_TURBO_2_CORES
82 * 23:16 - MAX_TURBO_3_CORES
83 * 31:24 - MAX_TURBO_4_CORES
84 *
85 * Set PERF_CTL MSR (0x199) P_Req with that value.
86 */
87 void cpu_set_p_state_to_turbo_ratio(void)
88 {
89 msr_t msr, perf_ctl;
90
91 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
92 perf_ctl.lo = (msr.lo & 0xff) << 8;
93 perf_ctl.hi = 0;
94
95 set_perf_control_msr(perf_ctl);
96 }
97
98 /*
99 * CONFIG_TDP_NOMINAL MSR (0x648) Bits 7:0 tells Nominal
100 * TDP level ratio to be used for specific processor (in units
101 * of 100MHz).
102 *
103 * Set PERF_CTL MSR (0x199) P_Req with that value.
104 */
105 void cpu_set_p_state_to_nominal_tdp_ratio(void)
106 {
107 msr_t msr, perf_ctl;
108
109 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
110 perf_ctl.lo = (msr.lo & 0xff) << 8;
111 perf_ctl.hi = 0;
112
113 set_perf_control_msr(perf_ctl);
114 }
115
116 /*
117 * PLATFORM_INFO MSR (0xCE) Bits 15:8 tells
118 * MAX_NON_TURBO_LIM_RATIO.
119 *
120 * Set PERF_CTL MSR (0x199) P_Req with that value.
121 */
122 void cpu_set_p_state_to_max_non_turbo_ratio(void)
123 {
124 msr_t perf_ctl;
125
126 /* Platform Info bits 15:8 give max ratio */
127 perf_ctl.lo = (cpu_get_max_non_turbo_ratio() << 8) & 0xff00;
128 perf_ctl.hi = 0;
129
130 set_perf_control_msr(perf_ctl);
131 }
132
133 /*
134 * Set PERF_CTL MSR (0x199) P_Req with the value
135 * for maximum efficiency. This value is reported in PLATFORM_INFO MSR (0xCE)
136 * in Bits 47:40 and is extracted with cpu_get_min_ratio().
137 */
138 void cpu_set_p_state_to_min_clock_ratio(void)
139 {
140 uint32_t min_ratio;
141 msr_t perf_ctl;
142
143 /* Read the minimum ratio for the best efficiency. */
144 min_ratio = cpu_get_min_ratio();
145 perf_ctl.lo = (min_ratio << 8) & 0xff00;
146 perf_ctl.hi = 0;
147
148 set_perf_control_msr(perf_ctl);
149 }
150
151 /*
152 * Get the Burst/Turbo Mode State from MSR IA32_MISC_ENABLE 0x1A0
153 * Bit 38 - TURBO_MODE_DISABLE Bit to get state ENABLED / DISABLED.
154 * Also check for the cpuid 0x6 to check whether Burst mode unsupported.
155 */
156 int cpu_get_burst_mode_state(void)
157 {
158 msr_t msr;
159 unsigned int eax;
160 int burst_en, burst_cap, burst_state = BURST_MODE_UNKNOWN;
161
162 eax = cpuid_eax(0x6);
163 burst_cap = eax & 0x2;
164 msr = rdmsr(IA32_MISC_ENABLE);
165 burst_en = !(msr.hi & BURST_MODE_DISABLE);
166
167 if (!burst_cap && burst_en) {
168 burst_state = BURST_MODE_UNAVAILABLE;
169 } else if (burst_cap && !burst_en) {
170 burst_state = BURST_MODE_DISABLED;
171 } else if (burst_cap && burst_en) {
172 burst_state = BURST_MODE_ENABLED;
173 }
174 return burst_state;
175 }
176
177 bool cpu_is_hybrid_supported(void)
178 {
179 struct cpuid_result cpuid_regs;
180
181 /* CPUID.(EAX=07H, ECX=00H):EDX[15] indicates CPU is hybrid CPU or not*/
182 cpuid_regs = cpuid_ext(CPUID_STRUCT_EXTENDED_FEATURE_FLAGS, 0);
183 return !!(cpuid_regs.edx & HYBRID_FEATURE);
184 }
185
186 /*
187 * The function must be called if CPU is hybrid. If CPU is hybrid, the CPU type
188 * information is available in the Hybrid Information Enumeration Leaf(EAX=0x1A, ECX=0).
189 */
190 uint8_t cpu_get_cpu_type(void)
191 {
192 union cpuid_nat_model_id_and_core_type {
193 struct {
194 u32 native_mode_id:24;
195 u32 core_type:8;
196 } bits;
197 u32 hybrid_info;
198 };
199 union cpuid_nat_model_id_and_core_type eax;
200
201 eax.hybrid_info = cpuid_eax(CPUID_HYBRID_INFORMATION);
202 return (u8)eax.bits.core_type;
203 }
204
205 /* It gets CPU bus frequency in MHz */
206 uint32_t cpu_get_bus_frequency(void)
207 {
208 return cpuid_ecx(CPUID_PROCESSOR_FREQUENCY);
209 }
210
211 /*
212 * Program CPU Burst mode
213 * true = Enable Burst mode.
214 * false = Disable Burst mode.
215 */
216 void cpu_burst_mode(bool burst_mode_status)
217 {
218 msr_t msr;
219
220 msr = rdmsr(IA32_MISC_ENABLE);
221 if (burst_mode_status)
222 msr.hi &= ~BURST_MODE_DISABLE;
223 else
224 msr.hi |= BURST_MODE_DISABLE;
225 wrmsr(IA32_MISC_ENABLE, msr);
226 }
227
228 /*
229 * Program Enhanced Intel Speed Step Technology
230 * true = Enable EIST.
231 * false = Disable EIST.
232 */
233 void cpu_set_eist(bool eist_status)
234 {
235 msr_t msr;
236
237 msr = rdmsr(IA32_MISC_ENABLE);
238 if (eist_status)
239 msr.lo |= (1 << 16);
240 else
241 msr.lo &= ~(1 << 16);
242 wrmsr(IA32_MISC_ENABLE, msr);
243 }
244
245 /*
246 * This function fills in the number of Cores(physical) and Threads(virtual)
247 * of the CPU in the function arguments. It also returns if the number of cores
248 * and number of threads are equal.
249 */
250 int cpu_read_topology(unsigned int *num_phys, unsigned int *num_virt)
251 {
252 msr_t msr;
253 msr = rdmsr(MSR_CORE_THREAD_COUNT);
254 *num_virt = (msr.lo >> 0) & 0xffff;
255 *num_phys = (msr.lo >> 16) & 0xffff;
256 return (*num_virt == *num_phys);
257 }
258
259 int cpu_get_coord_type(void)
260 {
261 return HW_ALL;
262 }
263
264 uint32_t cpu_get_min_ratio(void)
265 {
266 msr_t msr;
267 /* Get bus ratio limits and calculate clock speeds */
268 msr = rdmsr(MSR_PLATFORM_INFO);
269 return ((msr.hi >> 8) & 0xff); /* Max Efficiency Ratio */
270 }
271
272 uint32_t cpu_get_max_ratio(void)
273 {
274 msr_t msr;
275 uint32_t ratio_max;
276 if (cpu_config_tdp_levels()) {
277 /* Set max ratio to nominal TDP ratio */
278 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
279 ratio_max = msr.lo & 0xff;
280 } else {
281 msr = rdmsr(MSR_PLATFORM_INFO);
282 /* Max Non-Turbo Ratio */
283 ratio_max = (msr.lo >> 8) & 0xff;
284 }
285 return ratio_max;
286 }
287
288 uint8_t cpu_get_max_non_turbo_ratio(void)
289 {
290 msr_t msr;
291
292 /*
293 * PLATFORM_INFO(0xCE) MSR Bits[15:8] tells
294 * MAX_NON_TURBO_LIM_RATIO
295 */
296 msr = rdmsr(MSR_PLATFORM_INFO);
297 return ((msr.lo >> 8) & 0xff);
298 }
299
300 void configure_tcc_thermal_target(void)
301 {
302 const config_t *conf = config_of_soc();
303 msr_t msr;
304
305 if (!conf->tcc_offset)
306 return;
307
308 /* Set TCC activation offset */
309 msr = rdmsr(MSR_PLATFORM_INFO);
310 if ((msr.lo & BIT(30))) {
311 msr = rdmsr(MSR_TEMPERATURE_TARGET);
312 msr.lo &= ~(0xf << 24);
313 msr.lo |= (conf->tcc_offset & 0xf) << 24;
314 wrmsr(MSR_TEMPERATURE_TARGET, msr);
315 }
316
317 /*
318 * SoCs prior to Comet Lake/Cannon Lake do not support the time window
319 * bits, so return early.
320 */
321 if (CONFIG(SOC_INTEL_APOLLOLAKE) || CONFIG(SOC_INTEL_SKYLAKE) ||
322 CONFIG(SOC_INTEL_KABYLAKE) || CONFIG(SOC_INTEL_BRASWELL) ||
323 CONFIG(SOC_INTEL_BROADWELL) || CONFIG(SOC_INTEL_SKYLAKE_SP))
324 return;
325
326 /* Time Window Tau Bits [6:0] */
327 msr = rdmsr(MSR_TEMPERATURE_TARGET);
328 msr.lo &= ~0x7f;
329 msr.lo |= 0xe6; /* setting 100ms thermal time window */
330 wrmsr(MSR_TEMPERATURE_TARGET, msr);
331 }
332
333 uint32_t cpu_get_bus_clock(void)
334 {
335 /* CPU bus clock is set by default here to 100MHz.
336 * This function returns the bus clock in KHz.
337 */
338 return CONFIG_CPU_BCLK_MHZ * KHz;
339 }
340
341 uint32_t cpu_get_power_max(void)
342 {
343 msr_t msr;
344 int power_unit;
345
346 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
347 power_unit = 2 << ((msr.lo & 0xf) - 1);
348 msr = rdmsr(MSR_PKG_POWER_SKU);
349 return (msr.lo & 0x7fff) * 1000 / power_unit;
350 }
351
352 uint32_t cpu_get_max_turbo_ratio(void)
353 {
354 msr_t msr;
355 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
356 return msr.lo & 0xff;
357 }
358
359 void mca_configure(void)
360 {
361 int i;
362 const unsigned int num_banks = mca_get_bank_count();
363
364 printk(BIOS_DEBUG, "Clearing out pending MCEs\n");
365
366 mca_clear_status();
367
368 for (i = 0; i < num_banks; i++) {
369 /* Initialize machine checks */
370 wrmsr(IA32_MC_CTL(i),
371 (msr_t) {.lo = 0xffffffff, .hi = 0xffffffff});
372 }
373 }
374
375 void cpu_lt_lock_memory(void)
376 {
377 msr_set(MSR_LT_CONTROL, LT_CONTROL_LOCK);
378 }
379
380 bool is_sgx_supported(void)
381 {
382 struct cpuid_result cpuid_regs;
383 msr_t msr;
384
385 /* EBX[2] is feature capability */
386 cpuid_regs = cpuid_ext(CPUID_STRUCT_EXTENDED_FEATURE_FLAGS, 0x0);
387 msr = rdmsr(MTRR_CAP_MSR); /* Bit 12 is PRMRR enablement */
388 return ((cpuid_regs.ebx & SGX_SUPPORTED) && (msr.lo & MTRR_CAP_PRMRR));
389 }
390
391 static bool is_sgx_configured_and_supported(void)
392 {
393 return CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_ENABLE) && is_sgx_supported();
394 }
395
396 bool is_keylocker_supported(void)
397 {
398 struct cpuid_result cpuid_regs;
399 msr_t msr;
400
401 /* ECX[23] is feature capability */
402 cpuid_regs = cpuid_ext(CPUID_STRUCT_EXTENDED_FEATURE_FLAGS, 0x0);
403 msr = rdmsr(MTRR_CAP_MSR); /* Bit 12 is PRMRR enablement */
404 return ((cpuid_regs.ecx & KEYLOCKER_SUPPORTED) && (msr.lo & MTRR_CAP_PRMRR));
405 }
406
407 static bool is_keylocker_configured_and_supported(void)
408 {
409 return CONFIG(INTEL_KEYLOCKER) && is_keylocker_supported();
410 }
411
412 static bool check_prm_features_enabled(void)
413 {
414 /*
415 * Key Locker and SGX are the features that need PRM.
416 * If either of them are enabled return true, otherwise false
417 * */
418 return is_sgx_configured_and_supported() ||
419 is_keylocker_configured_and_supported();
420 }
421
422 int get_valid_prmrr_size(void)
423 {
424 msr_t msr;
425 int i;
426 int valid_size;
427
428 /* If none of the features that need PRM are enabled then return 0 */
429 if (!check_prm_features_enabled())
430 return 0;
431
432 if (!CONFIG_SOC_INTEL_COMMON_BLOCK_PRMRR_SIZE)
433 return 0;
434
435 msr = rdmsr(MSR_PRMRR_VALID_CONFIG);
436 if (!msr.lo) {
437 printk(BIOS_WARNING, "PRMRR not supported.\n");
438 return 0;
439 }
440
441 printk(BIOS_DEBUG, "MSR_PRMRR_VALID_CONFIG = 0x%08x\n", msr.lo);
442
443 /* find the first (greatest) value that is lower than or equal to the selected size */
444 for (i = 8; i >= 0; i--) {
445 valid_size = msr.lo & (1 << i);
446
447 if (valid_size && valid_size <= CONFIG_SOC_INTEL_COMMON_BLOCK_PRMRR_SIZE)
448 break;
449 else if (i == 0)
450 valid_size = 0;
451 }
452
453 if (!valid_size) {
454 printk(BIOS_WARNING, "Unsupported PRMRR size of %i MiB, check your config!\n",
455 CONFIG_SOC_INTEL_COMMON_BLOCK_PRMRR_SIZE);
456 return 0;
457 }
458
459 printk(BIOS_DEBUG, "PRMRR size set to %i MiB\n", valid_size);
460
461 valid_size *= MiB;
462
463 return valid_size;
464 }
465
466 static void sync_core_prmrr(void)
467 {
468 static msr_t msr_base, msr_mask;
469
470 if (boot_cpu()) {
471 msr_base = rdmsr(MSR_PRMRR_BASE_0);
472 msr_mask = rdmsr(MSR_PRMRR_PHYS_MASK);
473 } else if (!intel_ht_sibling()) {
474 wrmsr(MSR_PRMRR_BASE_0, msr_base);
475 wrmsr(MSR_PRMRR_PHYS_MASK, msr_mask);
476 }
477 }
478
479 void init_core_prmrr(void)
480 {
481 msr_t msr = rdmsr(MTRR_CAP_MSR);
482
483 if (msr.lo & MTRR_CAP_PRMRR)
484 sync_core_prmrr();
485 }
486
487 void set_tme_core_activate(void)
488 {
489 msr_t msr = { .lo = 0, .hi = 0 };
490
491 wrmsr(MSR_CORE_MKTME_ACTIVATION, msr);
492 }
493
494 /* Provide the max turbo frequency of the CPU */
495 unsigned int smbios_cpu_get_max_speed_mhz(void)
496 {
497 return cpu_get_max_turbo_ratio() * CONFIG_CPU_BCLK_MHZ;
498 }
499
500 void disable_three_strike_error(void)
501 {
502 msr_t msr;
503
504 msr = rdmsr(MSR_PREFETCH_CTL);
505 msr.lo = msr.lo | DISABLE_CPU_ERROR;
506 wrmsr(MSR_PREFETCH_CTL, msr);
507 }
508
509 void disable_signaling_three_strike_event(void)
510 {
511 msr_t msr;
512
513 msr = rdmsr(MSR_DISABLE_SIGNALING_THREE_STRIKE_EVENT);
514 msr.lo = msr.lo | THREE_STRIKE_COUNT;
515 wrmsr(MSR_DISABLE_SIGNALING_THREE_STRIKE_EVENT, msr);
516 }