Debian Codegraph @

src/cpu/intel/model_206ax/model_206ax_init.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <cpu/cpu.h>
6 #include <cpu/x86/mtrr.h>
7 #include <cpu/x86/msr.h>
8 #include <cpu/x86/mp.h>
9 #include <cpu/intel/microcode.h>
10 #include <cpu/intel/speedstep.h>
11 #include <cpu/intel/turbo.h>
12 #include <cpu/x86/cache.h>
13 #include <cpu/x86/name.h>
14 #include "commonlib/bsd/helpers.h"
15 #include "model_206ax.h"
16 #include "chip.h"
17 #include <cpu/intel/smm_reloc.h>
18 #include <cpu/intel/common/common.h>
19 #include <smbios.h>
20 #include <smp/node.h>
21 #include <static.h>
22 #include <types.h>
23
24 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
25 static const u8 power_limit_time_sec_to_msr[] = {
26 [0] = 0x00,
27 [1] = 0x0a,
28 [2] = 0x0b,
29 [3] = 0x4b,
30 [4] = 0x0c,
31 [5] = 0x2c,
32 [6] = 0x4c,
33 [7] = 0x6c,
34 [8] = 0x0d,
35 [10] = 0x2d,
36 [12] = 0x4d,
37 [14] = 0x6d,
38 [16] = 0x0e,
39 [20] = 0x2e,
40 [24] = 0x4e,
41 [28] = 0x6e,
42 [32] = 0x0f,
43 [40] = 0x2f,
44 [48] = 0x4f,
45 [56] = 0x6f,
46 [64] = 0x10,
47 [80] = 0x30,
48 [96] = 0x50,
49 [112] = 0x70,
50 [128] = 0x11,
51 };
52
53 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
54 static const u8 power_limit_time_msr_to_sec[] = {
55 [0x00] = 0,
56 [0x0a] = 1,
57 [0x0b] = 2,
58 [0x4b] = 3,
59 [0x0c] = 4,
60 [0x2c] = 5,
61 [0x4c] = 6,
62 [0x6c] = 7,
63 [0x0d] = 8,
64 [0x2d] = 10,
65 [0x4d] = 12,
66 [0x6d] = 14,
67 [0x0e] = 16,
68 [0x2e] = 20,
69 [0x4e] = 24,
70 [0x6e] = 28,
71 [0x0f] = 32,
72 [0x2f] = 40,
73 [0x4f] = 48,
74 [0x6f] = 56,
75 [0x10] = 64,
76 [0x30] = 80,
77 [0x50] = 96,
78 [0x70] = 112,
79 [0x11] = 128,
80 };
81
82 int cpu_config_tdp_levels(void)
83 {
84 msr_t platform_info;
85
86 /* Minimum CPU revision */
87 if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID)
88 return 0;
89
90 /* Bits 34:33 indicate how many levels supported */
91 platform_info = rdmsr(MSR_PLATFORM_INFO);
92 return (platform_info.hi >> 1) & 3;
93 }
94
95 /*
96 * Configure processor power limits if possible
97 * This must be done AFTER set of BIOS_RESET_CPL
98 */
99 void set_power_limits(u8 power_limit_1_time)
100 {
101 struct cpu_intel_model_206ax_config *conf = DEV_PTR(cpu_bus)->chip_info;
102 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
103 msr_t limit;
104 unsigned int power_unit;
105 unsigned int tdp, min_power, max_power, max_time;
106 u8 power_limit_1_val;
107
108 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
109 return;
110
111 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
112 return;
113
114 /* Get units */
115 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
116 power_unit = 2 << ((msr.lo & 0xf) - 1);
117
118 /* Get power defaults for this SKU */
119 msr = rdmsr(MSR_PKG_POWER_SKU);
120 tdp = msr.lo & 0x7fff;
121 min_power = (msr.lo >> 16) & 0x7fff;
122 max_power = msr.hi & 0x7fff;
123 max_time = (msr.hi >> 16) & 0x7f;
124
125 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
126
127 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
128 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
129
130 if (min_power > 0 && tdp < min_power)
131 tdp = min_power;
132
133 if (max_power > 0 && tdp > max_power)
134 tdp = max_power;
135
136 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
137
138 limit.lo = 0;
139 if (conf->pl1_mw) {
140 printk(BIOS_DEBUG, "Setting PL1 to %u milliwatts\n", conf->pl1_mw);
141 limit.lo |= ((conf->pl1_mw * power_unit) / 1000) & PKG_POWER_LIMIT_MASK;
142 } else {
143 /* Set long term power limit to TDP */
144 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
145 }
146 if (conf->pl2_clamp) {
147 printk(BIOS_DEBUG, "Enabling PL1 clamping limitation\n");
148 limit.lo |= PKG_POWER_LIMIT_CLAMP;
149 }
150 limit.lo |= PKG_POWER_LIMIT_EN;
151 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
152 PKG_POWER_LIMIT_TIME_SHIFT;
153
154 limit.hi = 0;
155 if (conf->pl2_mw) {
156 printk(BIOS_DEBUG, "Setting PL2 to %u milliwatts\n", conf->pl2_mw);
157 limit.hi |= ((conf->pl2_mw * power_unit) / 1000) & PKG_POWER_LIMIT_MASK;
158 } else {
159 /* Set short term power limit to 1.25 * TDP */
160 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
161 }
162 if (conf->pl2_clamp) {
163 printk(BIOS_DEBUG, "Enabling PL2 clamping limitation\n");
164 limit.hi |= PKG_POWER_LIMIT_CLAMP;
165 }
166 limit.hi |= PKG_POWER_LIMIT_EN;
167 /* Power limit 2 time is only programmable on SNB EP/EX */
168
169 wrmsr(MSR_PKG_POWER_LIMIT, limit);
170
171 /* Use nominal TDP values for CPUs with configurable TDP */
172 if (cpu_config_tdp_levels()) {
173 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
174 limit.hi = 0;
175 limit.lo = msr.lo & 0xff;
176 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
177 }
178 }
179
180 static void configure_turbo_ratio_limits(struct cpu_intel_model_206ax_config *conf)
181 {
182 msr_t msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
183
184 for (int i = 0; i < ARRAY_SIZE(conf->turbo_limits.raw); i++) {
185 const int shift = i * 8;
186 const int limit = conf->turbo_limits.raw[i];
187
188 if (limit) {
189 msr.lo &= ~(0xff << shift);
190 msr.lo |= (limit << shift);
191 }
192 }
193
194 wrmsr(MSR_TURBO_RATIO_LIMIT, msr);
195 }
196
197 static void configure_c_states(struct device *dev)
198 {
199 struct cpu_intel_model_206ax_config *conf = dev->upstream->dev->chip_info;
200 msr_t msr;
201
202 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
203 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
204 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
205 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
206 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
207 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
208 msr.lo |= 7; // No package C-state limit
209
210 msr.lo |= (1 << 15); // Lock C-State MSR
211 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
212
213 if (boot_cpu()) {
214 /*
215 * The following MSRs are in scope 'Package', thus it's sufficient
216 * to write them once on one core.
217 */
218
219 msr = rdmsr(MSR_MISC_PWR_MGMT);
220 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
221 wrmsr(MSR_MISC_PWR_MGMT, msr);
222
223 msr = rdmsr(MSR_POWER_CTL);
224 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
225 msr.lo |= (1 << 1); // C1E Enable
226 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
227 wrmsr(MSR_POWER_CTL, msr);
228
229 /* C3 Interrupt Response Time Limit */
230 msr.hi = 0;
231 if (IS_IVY_CPU(cpu_get_cpuid()))
232 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x3b;
233 else
234 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
235 wrmsr(MSR_PKGC3_IRTL, msr);
236
237 /* C6 Interrupt Response Time Limit */
238 msr.hi = 0;
239 if (IS_IVY_CPU(cpu_get_cpuid()))
240 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
241 else
242 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
243 wrmsr(MSR_PKGC6_IRTL, msr);
244
245 /* C7 Interrupt Response Time Limit */
246 msr.hi = 0;
247 if (IS_IVY_CPU(cpu_get_cpuid()))
248 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x57;
249 else
250 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
251 wrmsr(MSR_PKGC7_IRTL, msr);
252
253 /* Primary Plane Current Limit (Icc) */
254 msr = rdmsr(MSR_PP0_CURRENT_CONFIG);
255 msr.lo &= ~0x1fff;
256 if (conf->pp0_current_limit) {
257 /* Fill in board specific maximum current supported by VR */
258 msr.lo |= conf->pp0_current_limit * 8;
259 } else {
260 printk(BIOS_INFO, "%s: PP0 current limit not set in devicetree\n", dev_path(dev));
261 /*
262 * The default value might over-stress the voltage regulator or
263 * prevent OC on boards with regulators that can handle currents
264 * above the Intel recommendation.
265 */
266 msr.lo |= PP0_CURRENT_LIMIT;
267 }
268 for (int i = 0; i < VR12_PSI_MAX; i++) {
269 /*
270 * Light load optimization. Depending on the VR output filter the
271 * number of phases can be reduced at light load. This is a board
272 * specific setting.
273 */
274 if (conf->pp0_psi[i].phases != VR12_KEEP_DEFAULT) {
275 msr.hi &= ~(0x3ff << (i * 10));
276 msr.hi |= (conf->pp0_psi[i].phases - 1) << (i * 10 + 7);
277 msr.hi |= conf->pp0_psi[i].current << (i * 10);
278 } else {
279 printk(BIOS_INFO, "%s: PP0 PSI%d not set in devicetree\n", dev_path(dev), i);
280 }
281 }
282 msr.lo |= PP0_CURRENT_LIMIT_LOCK;
283 wrmsr(MSR_PP0_CURRENT_CONFIG, msr);
284
285 /* Secondary Plane Current Limit (IAXG) */
286 msr = rdmsr(MSR_PP1_CURRENT_CONFIG);
287 msr.lo &= ~0x1fff;
288 if (conf->pp1_current_limit) {
289 /* Fill in board specific maximum current supported by VR */
290 msr.lo |= conf->pp1_current_limit * 8;
291 } else {
292 printk(BIOS_INFO, "%s: PP1 current limit not set in devicetree\n", dev_path(dev));
293 /*
294 * The default value might over-stress the voltage regulator or
295 * prevent OC on boards with regulators that can handle currents
296 * above the Intel recommendation.
297 */
298 if (IS_IVY_CPU(cpu_get_cpuid()))
299 msr.lo |= PP1_CURRENT_LIMIT_IVB;
300 else
301 msr.lo |= PP1_CURRENT_LIMIT_SNB;
302 }
303 for (int i = 0; i < VR12_PSI_MAX; i++) {
304 /*
305 * Light load optimization. Depending on the VR output filter the
306 * number of phases can be reduced at light load. This is a board
307 * specific setting.
308 */
309 if (conf->pp1_psi[i].phases != VR12_KEEP_DEFAULT) {
310 msr.hi &= ~(0x3ff << (i * 10));
311 msr.hi |= (conf->pp1_psi[i].phases - 1) << (i * 10 + 7);
312 msr.hi |= conf->pp1_psi[i].current << (i * 10);
313 } else {
314 printk(BIOS_INFO, "%s: PP1 PSI%d not set in devicetree\n", dev_path(dev), i);
315 }
316 }
317 msr.lo |= PP1_CURRENT_LIMIT_LOCK;
318 wrmsr(MSR_PP1_CURRENT_CONFIG, msr);
319 }
320
321 msr = rdmsr(MSR_PLATFORM_INFO);
322 if (msr.lo & PLATFORM_INFO_SET_TURBO_LIMIT) {
323 configure_turbo_ratio_limits(conf);
324 } else {
325 printk(BIOS_INFO, "%s: Programmable ratio limit for turbo mode is disabled\n",
326 dev_path(dev));
327 }
328 }
329
330 static void configure_thermal_target(struct device *dev)
331 {
332 struct cpu_intel_model_206ax_config *conf = dev->upstream->dev->chip_info;
333 msr_t msr;
334
335 if (boot_cpu()) {
336 /*
337 * The following MSR is in scope 'Package', thus it's sufficient
338 * to write it once on one core.
339 */
340
341 /* Set TCC activation offset if supported */
342 msr = rdmsr(MSR_PLATFORM_INFO);
343 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
344 msr = rdmsr(MSR_TEMPERATURE_TARGET);
345 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
346 msr.lo |= (conf->tcc_offset & 0xf) << 24;
347 wrmsr(MSR_TEMPERATURE_TARGET, msr);
348 }
349 }
350 }
351
352 static void configure_misc(void)
353 {
354 msr_t msr;
355
356 msr = rdmsr(IA32_MISC_ENABLE);
357 msr.lo |= (1 << 0); /* Fast String enable */
358 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
359 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
360 wrmsr(IA32_MISC_ENABLE, msr);
361
362 /* Disable Thermal interrupts */
363 msr.lo = 0;
364 msr.hi = 0;
365 wrmsr(IA32_THERM_INTERRUPT, msr);
366
367 if (boot_cpu()) {
368 /*
369 * The following MSR is in scope 'Package', thus it's sufficient
370 * to write it once on one core.
371 */
372
373 /* Enable package critical interrupt only */
374 msr.lo = 1 << 4;
375 msr.hi = 0;
376 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
377 }
378 }
379
380 static void set_max_ratio(void)
381 {
382 msr_t msr, perf_ctl;
383
384 perf_ctl.hi = 0;
385
386 /* Check for configurable TDP option */
387 if (cpu_config_tdp_levels()) {
388 /* Set to nominal TDP ratio */
389 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
390 perf_ctl.lo = (msr.lo & 0xff) << 8;
391 } else {
392 /* Platform Info bits 15:8 give max ratio */
393 msr = rdmsr(MSR_PLATFORM_INFO);
394 perf_ctl.lo = msr.lo & 0xff00;
395 }
396 wrmsr(IA32_PERF_CTL, perf_ctl);
397
398 printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
399 ((perf_ctl.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK);
400 }
401
402 unsigned int smbios_cpu_get_max_speed_mhz(void)
403 {
404 msr_t msr;
405 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
406 return (msr.lo & 0xff) * SANDYBRIDGE_BCLK;
407 }
408
409 unsigned int smbios_cpu_get_current_speed_mhz(void)
410 {
411 msr_t msr;
412 msr = rdmsr(MSR_PLATFORM_INFO);
413 return ((msr.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK;
414 }
415
416 unsigned int smbios_processor_external_clock(void)
417 {
418 return SANDYBRIDGE_BCLK;
419 }
420
421 static void model_206ax_report(void)
422 {
423 static const char *const mode[] = {"NOT ", ""};
424 char processor_name[49];
425 int vt, txt, aes;
426 uint32_t cpu_id, cpu_feature_flag;
427
428 /* Print processor name */
429 fill_processor_name(processor_name);
430 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
431
432 /* CPUID and features */
433 cpu_id = cpu_get_cpuid();
434 printk(BIOS_INFO, "CPU: cpuid(1) 0x%x\n", cpu_id);
435
436 cpu_feature_flag = cpu_get_feature_flags_ecx();
437 aes = (cpu_feature_flag & CPUID_AES) ? 1 : 0;
438 txt = (cpu_feature_flag & CPUID_SMX) ? 1 : 0;
439 vt = (cpu_feature_flag & CPUID_VMX) ? 1 : 0;
440 printk(BIOS_INFO, "CPU: AES %ssupported\n", mode[aes]);
441 printk(BIOS_INFO, "CPU: TXT %ssupported\n", mode[txt]);
442 printk(BIOS_INFO, "CPU: VT %ssupported\n", mode[vt]);
443 }
444
445 static void model_206ax_init(struct device *cpu)
446 {
447 /* Clear out pending MCEs */
448 /* This should only be done on a cold boot */
449 mca_clear_status();
450
451 /* Print infos */
452 model_206ax_report();
453
454 /* Setup Page Attribute Tables (PAT) */
455 // TODO set up PAT
456
457 enable_lapic_tpr();
458
459 /* Set virtualization based on Kconfig option */
460 set_vmx_and_lock();
461
462 /* Configure C States */
463 configure_c_states(cpu);
464
465 /* Configure Enhanced SpeedStep and Thermal Sensors */
466 configure_misc();
467
468 /* Thermal throttle activation offset */
469 configure_thermal_target(cpu);
470
471 set_aesni_lock();
472
473 /* Enable Direct Cache Access */
474 configure_dca_cap();
475
476 /* Set energy policy */
477 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
478
479 /* Set Max Ratio */
480 set_max_ratio();
481
482 /* Enable Turbo */
483 enable_turbo();
484 }
485
486 /* MP initialization support. */
487 static void pre_mp_init(void)
488 {
489 /* Setup MTRRs based on physical address size. */
490 x86_setup_mtrrs_with_detect();
491 x86_mtrr_check();
492 }
493
494 static int get_cpu_count(void)
495 {
496 msr_t msr;
497 unsigned int num_threads;
498 unsigned int num_cores;
499
500 msr = rdmsr(MSR_CORE_THREAD_COUNT);
501 num_threads = (msr.lo >> 0) & 0xffff;
502 num_cores = (msr.lo >> 16) & 0xffff;
503 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
504 num_cores, num_threads);
505
506 return num_threads;
507 }
508
509 static void get_microcode_info(const void **microcode, int *parallel)
510 {
511 *microcode = intel_microcode_find();
512 *parallel = !intel_ht_supported();
513 }
514
515 static void per_cpu_smm_trigger(void)
516 {
517 /* Relocate the SMM handler. */
518 smm_relocate();
519
520 /* After SMM relocation a 2nd microcode load is required. */
521 const void *microcode_patch = intel_microcode_find();
522 intel_microcode_load_unlocked(microcode_patch);
523 }
524
525 static void post_mp_init(void)
526 {
527 /* Now that all APs have been relocated as well as the BSP let SMIs
528 * start flowing. */
529 global_smi_enable();
530
531 /* Lock down the SMRAM space. */
532 smm_lock();
533 }
534
535 static const struct mp_ops mp_ops = {
536 .pre_mp_init = pre_mp_init,
537 .get_cpu_count = get_cpu_count,
538 .get_smm_info = smm_info,
539 .get_microcode_info = get_microcode_info,
540 .pre_mp_smm_init = smm_initialize,
541 .per_cpu_smm_trigger = per_cpu_smm_trigger,
542 .relocation_handler = smm_relocation_handler,
543 .post_mp_init = post_mp_init,
544 };
545
546 void mp_init_cpus(struct bus *cpu_bus)
547 {
548 /* TODO: Handle mp_init_with_smm failure? */
549 mp_init_with_smm(cpu_bus, &mp_ops);
550
551 /* pre_mp_init made the flash not cacheable. Reset to WP for performance. */
552 mtrr_use_temp_range(CACHE_ROM_BASE, CAR_CACHE_ROM_SIZE,
553 MTRR_TYPE_WRPROT);
554 }
555
556 static struct device_operations cpu_dev_ops = {
557 .init = model_206ax_init,
558 };
559
560 static const struct cpu_device_id cpu_table[] = {
561 { X86_VENDOR_INTEL, 0x206a0, CPUID_ALL_STEPPINGS_MASK }, /* Intel Sandybridge */
562 { X86_VENDOR_INTEL, 0x306a0, CPUID_ALL_STEPPINGS_MASK }, /* Intel IvyBridge */
563 CPU_TABLE_END
564 };
565
566 static const struct cpu_driver driver __cpu_driver = {
567 .ops = &cpu_dev_ops,
568 .id_table = cpu_table,
569 };