blob: 9de6b3819813acae88c1a160ae5d72e0e60c59ef [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer5c554632012-04-04 00:09:50 +02002
3#include <console/console.h>
4#include <device/device.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05306#include <arch/cpu.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +02007#include <cpu/cpu.h>
8#include <cpu/x86/mtrr.h>
9#include <cpu/x86/msr.h>
10#include <cpu/x86/lapic.h>
Arthur Heymansedbf5d92018-01-25 20:03:42 +010011#include <cpu/x86/mp.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020012#include <cpu/intel/microcode.h>
13#include <cpu/intel/speedstep.h>
14#include <cpu/intel/turbo.h>
15#include <cpu/x86/cache.h>
16#include <cpu/x86/name.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020017#include "model_206ax.h"
Duncan Laurie55632112012-07-16 12:19:00 -070018#include "chip.h"
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030019#include <cpu/intel/smm_reloc.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060020#include <cpu/intel/common/common.h>
Michał Żygowski0d11dbf2020-10-31 21:43:25 +010021#include <smbios.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020022#include <types.h>
Vladimir Serbinenkoc16e9dfa2015-05-29 16:18:01 +020023
Stefan Reinauer5c554632012-04-04 00:09:50 +020024/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
25static 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 */
54static 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
Duncan Laurie77dbbac2012-06-25 09:51:59 -070082int 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
Stefan Reinauer5c554632012-04-04 00:09:50 +020095/*
96 * Configure processor power limits if possible
97 * This must be done AFTER set of BIOS_RESET_CPL
98 */
99void set_power_limits(u8 power_limit_1_time)
100{
101 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
102 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700103 unsigned int power_unit;
104 unsigned int tdp, min_power, max_power, max_time;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200105 u8 power_limit_1_val;
106
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000107 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Stefan Reinauer5c554632012-04-04 00:09:50 +0200108 return;
109
110 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
111 return;
112
113 /* Get units */
114 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
115 power_unit = 2 << ((msr.lo & 0xf) - 1);
116
117 /* Get power defaults for this SKU */
118 msr = rdmsr(MSR_PKG_POWER_SKU);
119 tdp = msr.lo & 0x7fff;
120 min_power = (msr.lo >> 16) & 0x7fff;
121 max_power = msr.hi & 0x7fff;
122 max_time = (msr.hi >> 16) & 0x7f;
123
124 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
125
126 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
127 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
128
129 if (min_power > 0 && tdp < min_power)
130 tdp = min_power;
131
132 if (max_power > 0 && tdp > max_power)
133 tdp = max_power;
134
135 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
136
137 /* Set long term power limit to TDP */
138 limit.lo = 0;
139 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
140 limit.lo |= PKG_POWER_LIMIT_EN;
141 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
142 PKG_POWER_LIMIT_TIME_SHIFT;
143
144 /* Set short term power limit to 1.25 * TDP */
145 limit.hi = 0;
146 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
147 limit.hi |= PKG_POWER_LIMIT_EN;
148 /* Power limit 2 time is only programmable on SNB EP/EX */
149
150 wrmsr(MSR_PKG_POWER_LIMIT, limit);
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700151
152 /* Use nominal TDP values for CPUs with configurable TDP */
153 if (cpu_config_tdp_levels()) {
154 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
155 limit.hi = 0;
156 limit.lo = msr.lo & 0xff;
157 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
158 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200159}
160
161static void configure_c_states(void)
162{
163 msr_t msr;
164
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200165 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200166 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
167 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
168 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
169 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
170 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
171 msr.lo |= 7; // No package C-state limit
Patrick Rudolph573481b2020-03-02 14:21:32 +0100172
173 msr.lo |= (1 << 15); // Lock C-State MSR
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200174 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200175
Stefan Reinauer5c554632012-04-04 00:09:50 +0200176 msr = rdmsr(MSR_MISC_PWR_MGMT);
177 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
178 wrmsr(MSR_MISC_PWR_MGMT, msr);
179
180 msr = rdmsr(MSR_POWER_CTL);
181 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
182 msr.lo |= (1 << 1); // C1E Enable
183 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
184 wrmsr(MSR_POWER_CTL, msr);
185
186 /* C3 Interrupt Response Time Limit */
187 msr.hi = 0;
188 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
189 wrmsr(MSR_PKGC3_IRTL, msr);
190
191 /* C6 Interrupt Response Time Limit */
192 msr.hi = 0;
193 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
194 wrmsr(MSR_PKGC6_IRTL, msr);
195
196 /* C7 Interrupt Response Time Limit */
197 msr.hi = 0;
198 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
199 wrmsr(MSR_PKGC7_IRTL, msr);
200
201 /* Primary Plane Current Limit */
202 msr = rdmsr(MSR_PP0_CURRENT_CONFIG);
203 msr.lo &= ~0x1fff;
204 msr.lo |= PP0_CURRENT_LIMIT;
205 wrmsr(MSR_PP0_CURRENT_CONFIG, msr);
206
207 /* Secondary Plane Current Limit */
208 msr = rdmsr(MSR_PP1_CURRENT_CONFIG);
209 msr.lo &= ~0x1fff;
Duncan Laurie4e4320f2012-06-25 09:53:58 -0700210 if (cpuid_eax(1) >= 0x30600)
211 msr.lo |= PP1_CURRENT_LIMIT_IVB;
212 else
213 msr.lo |= PP1_CURRENT_LIMIT_SNB;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200214 wrmsr(MSR_PP1_CURRENT_CONFIG, msr);
215}
216
Duncan Laurie55632112012-07-16 12:19:00 -0700217static void configure_thermal_target(void)
218{
219 struct cpu_intel_model_206ax_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100220 struct device *lapic;
Duncan Laurie55632112012-07-16 12:19:00 -0700221 msr_t msr;
222
223 /* Find pointer to CPU configuration */
224 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
225 if (!lapic || !lapic->chip_info)
226 return;
227 conf = lapic->chip_info;
228
Martin Roth4c3ab732013-07-08 16:23:54 -0600229 /* Set TCC activation offset if supported */
Duncan Laurie55632112012-07-16 12:19:00 -0700230 msr = rdmsr(MSR_PLATFORM_INFO);
231 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
232 msr = rdmsr(MSR_TEMPERATURE_TARGET);
233 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
234 msr.lo |= (conf->tcc_offset & 0xf) << 24;
235 wrmsr(MSR_TEMPERATURE_TARGET, msr);
236 }
237}
238
Stefan Reinauer5c554632012-04-04 00:09:50 +0200239static void configure_misc(void)
240{
241 msr_t msr;
242
243 msr = rdmsr(IA32_MISC_ENABLE);
244 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700245 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200246 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
247 wrmsr(IA32_MISC_ENABLE, msr);
248
249 /* Disable Thermal interrupts */
250 msr.lo = 0;
251 msr.hi = 0;
252 wrmsr(IA32_THERM_INTERRUPT, msr);
253
254 /* Enable package critical interrupt only */
255 msr.lo = 1 << 4;
256 msr.hi = 0;
257 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
258}
259
Stefan Reinauer5c554632012-04-04 00:09:50 +0200260static void set_max_ratio(void)
261{
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700262 msr_t msr, perf_ctl;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200263
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700264 perf_ctl.hi = 0;
265
266 /* Check for configurable TDP option */
267 if (cpu_config_tdp_levels()) {
268 /* Set to nominal TDP ratio */
269 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
270 perf_ctl.lo = (msr.lo & 0xff) << 8;
271 } else {
272 /* Platform Info bits 15:8 give max ratio */
273 msr = rdmsr(MSR_PLATFORM_INFO);
274 perf_ctl.lo = msr.lo & 0xff00;
275 }
276 wrmsr(IA32_PERF_CTL, perf_ctl);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200277
278 printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700279 ((perf_ctl.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200280}
281
Michał Żygowski0d11dbf2020-10-31 21:43:25 +0100282unsigned int smbios_cpu_get_max_speed_mhz(void)
283{
284 msr_t msr;
285 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
286 return (msr.lo & 0xff) * SANDYBRIDGE_BCLK;
287}
288
289unsigned int smbios_cpu_get_current_speed_mhz(void)
290{
291 msr_t msr;
292 msr = rdmsr(MSR_PLATFORM_INFO);
293 return ((msr.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK;
294}
295
296unsigned int smbios_processor_external_clock(void)
297{
298 return SANDYBRIDGE_BCLK;
299}
300
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200301static void model_206ax_report(void)
302{
303 static const char *const mode[] = {"NOT ", ""};
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200304 char processor_name[49];
305 int vt, txt, aes;
Subrata Banik53b08c32018-12-10 14:11:35 +0530306 uint32_t cpu_id, cpu_feature_flag;
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200307
308 /* Print processor name */
309 fill_processor_name(processor_name);
310 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
311
312 /* Print platform ID */
313 printk(BIOS_INFO, "CPU: platform id %x\n", get_platform_id());
314
315 /* CPUID and features */
Subrata Banik53b08c32018-12-10 14:11:35 +0530316 cpu_id = cpu_get_cpuid();
317 printk(BIOS_INFO, "CPU: cpuid(1) 0x%x\n", cpu_id);
318
319 cpu_feature_flag = cpu_get_feature_flags_ecx();
320 aes = (cpu_feature_flag & CPUID_AES) ? 1 : 0;
321 txt = (cpu_feature_flag & CPUID_SMX) ? 1 : 0;
322 vt = (cpu_feature_flag & CPUID_VMX) ? 1 : 0;
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200323 printk(BIOS_INFO, "CPU: AES %ssupported\n", mode[aes]);
324 printk(BIOS_INFO, "CPU: TXT %ssupported\n", mode[txt]);
325 printk(BIOS_INFO, "CPU: VT %ssupported\n", mode[vt]);
326}
327
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100328static void model_206ax_init(struct device *cpu)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200329{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200330
Stefan Reinauer5c554632012-04-04 00:09:50 +0200331 /* Clear out pending MCEs */
Felix Heldacbf1542021-07-13 16:44:18 +0200332 /* This should only be done on a cold boot */
333 mca_clear_status();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200334
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200335 /* Print infos */
336 model_206ax_report();
Patrick Rudolph74203de2017-11-20 11:57:01 +0100337
Stefan Reinauer5c554632012-04-04 00:09:50 +0200338 /* Setup Page Attribute Tables (PAT) */
339 // TODO set up PAT
340
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200341 /* Enable the local CPU APICs */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200342 enable_lapic_tpr();
343 setup_lapic();
344
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600345 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600346 set_vmx_and_lock();
Marc Jones5986eda2012-10-25 09:37:19 -0600347
Stefan Reinauer5c554632012-04-04 00:09:50 +0200348 /* Configure C States */
349 configure_c_states();
350
351 /* Configure Enhanced SpeedStep and Thermal Sensors */
352 configure_misc();
353
Duncan Laurie55632112012-07-16 12:19:00 -0700354 /* Thermal throttle activation offset */
355 configure_thermal_target();
356
Michael Niewöhner63032432020-10-11 17:34:54 +0200357 set_aesni_lock();
Michael Niewöhner7f8767d2020-10-18 00:45:38 +0200358
Stefan Reinauer5c554632012-04-04 00:09:50 +0200359 /* Enable Direct Cache Access */
360 configure_dca_cap();
361
362 /* Set energy policy */
363 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
364
365 /* Set Max Ratio */
366 set_max_ratio();
367
368 /* Enable Turbo */
369 enable_turbo();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100370}
Sven Schnelle51676b12012-07-29 19:18:03 +0200371
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100372/* MP initialization support. */
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100373static void pre_mp_init(void)
374{
375 /* Setup MTRRs based on physical address size. */
376 x86_setup_mtrrs_with_detect();
377 x86_mtrr_check();
378}
379
380static int get_cpu_count(void)
381{
382 msr_t msr;
Angel Pons04c497a2021-11-03 16:30:10 +0100383 unsigned int num_threads;
384 unsigned int num_cores;
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100385
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200386 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100387 num_threads = (msr.lo >> 0) & 0xffff;
388 num_cores = (msr.lo >> 16) & 0xffff;
389 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
390 num_cores, num_threads);
391
392 return num_threads;
393}
394
395static void get_microcode_info(const void **microcode, int *parallel)
396{
Patrick Rudolph3fa23b82021-01-25 09:42:08 +0100397 *microcode = intel_microcode_find();
Patrick Rudolphce51b342021-01-11 09:21:58 +0100398 *parallel = !intel_ht_supported();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100399}
400
401static void per_cpu_smm_trigger(void)
402{
403 /* Relocate the SMM handler. */
404 smm_relocate();
405
406 /* After SMM relocation a 2nd microcode load is required. */
Patrick Rudolph3fa23b82021-01-25 09:42:08 +0100407 const void *microcode_patch = intel_microcode_find();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100408 intel_microcode_load_unlocked(microcode_patch);
409}
410
411static void post_mp_init(void)
412{
413 /* Now that all APs have been relocated as well as the BSP let SMIs
414 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300415 global_smi_enable();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100416
417 /* Lock down the SMRAM space. */
418 smm_lock();
419}
420
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100421static const struct mp_ops mp_ops = {
422 .pre_mp_init = pre_mp_init,
423 .get_cpu_count = get_cpu_count,
424 .get_smm_info = smm_info,
425 .get_microcode_info = get_microcode_info,
426 .pre_mp_smm_init = smm_initialize,
427 .per_cpu_smm_trigger = per_cpu_smm_trigger,
428 .relocation_handler = smm_relocation_handler,
429 .post_mp_init = post_mp_init,
430};
431
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300432void mp_init_cpus(struct bus *cpu_bus)
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100433{
Felix Held4dd7d112021-10-20 23:31:43 +0200434 /* TODO: Handle mp_init_with_smm failure? */
435 mp_init_with_smm(cpu_bus, &mp_ops);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200436}
437
438static struct device_operations cpu_dev_ops = {
439 .init = model_206ax_init,
440};
441
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100442static const struct cpu_device_id cpu_table[] = {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200443 { X86_VENDOR_INTEL, 0x206a0 }, /* Intel Sandybridge */
444 { X86_VENDOR_INTEL, 0x206a6 }, /* Intel Sandybridge D1 */
445 { X86_VENDOR_INTEL, 0x206a7 }, /* Intel Sandybridge D2/J1 */
Stefan Reinauer08067ba2012-10-15 13:47:04 -0700446 { X86_VENDOR_INTEL, 0x306a0 }, /* Intel IvyBridge */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200447 { X86_VENDOR_INTEL, 0x306a2 }, /* Intel IvyBridge */
448 { X86_VENDOR_INTEL, 0x306a4 }, /* Intel IvyBridge */
449 { X86_VENDOR_INTEL, 0x306a5 }, /* Intel IvyBridge */
450 { X86_VENDOR_INTEL, 0x306a6 }, /* Intel IvyBridge */
451 { X86_VENDOR_INTEL, 0x306a8 }, /* Intel IvyBridge */
452 { X86_VENDOR_INTEL, 0x306a9 }, /* Intel IvyBridge */
453 { 0, 0 },
454};
455
456static const struct cpu_driver driver __cpu_driver = {
457 .ops = &cpu_dev_ops,
458 .id_table = cpu_table,
Stefan Reinauer5c554632012-04-04 00:09:50 +0200459};