blob: 09cad24b8bd0c6ce486413afeea9537d69081f03 [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>
Vladimir Serbinenkoc16e9dfa2015-05-29 16:18:01 +020022
Stefan Reinauer5c554632012-04-04 00:09:50 +020023/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
24static const u8 power_limit_time_sec_to_msr[] = {
25 [0] = 0x00,
26 [1] = 0x0a,
27 [2] = 0x0b,
28 [3] = 0x4b,
29 [4] = 0x0c,
30 [5] = 0x2c,
31 [6] = 0x4c,
32 [7] = 0x6c,
33 [8] = 0x0d,
34 [10] = 0x2d,
35 [12] = 0x4d,
36 [14] = 0x6d,
37 [16] = 0x0e,
38 [20] = 0x2e,
39 [24] = 0x4e,
40 [28] = 0x6e,
41 [32] = 0x0f,
42 [40] = 0x2f,
43 [48] = 0x4f,
44 [56] = 0x6f,
45 [64] = 0x10,
46 [80] = 0x30,
47 [96] = 0x50,
48 [112] = 0x70,
49 [128] = 0x11,
50};
51
52/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
53static const u8 power_limit_time_msr_to_sec[] = {
54 [0x00] = 0,
55 [0x0a] = 1,
56 [0x0b] = 2,
57 [0x4b] = 3,
58 [0x0c] = 4,
59 [0x2c] = 5,
60 [0x4c] = 6,
61 [0x6c] = 7,
62 [0x0d] = 8,
63 [0x2d] = 10,
64 [0x4d] = 12,
65 [0x6d] = 14,
66 [0x0e] = 16,
67 [0x2e] = 20,
68 [0x4e] = 24,
69 [0x6e] = 28,
70 [0x0f] = 32,
71 [0x2f] = 40,
72 [0x4f] = 48,
73 [0x6f] = 56,
74 [0x10] = 64,
75 [0x30] = 80,
76 [0x50] = 96,
77 [0x70] = 112,
78 [0x11] = 128,
79};
80
Duncan Laurie77dbbac2012-06-25 09:51:59 -070081int cpu_config_tdp_levels(void)
82{
83 msr_t platform_info;
84
85 /* Minimum CPU revision */
86 if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID)
87 return 0;
88
89 /* Bits 34:33 indicate how many levels supported */
90 platform_info = rdmsr(MSR_PLATFORM_INFO);
91 return (platform_info.hi >> 1) & 3;
92}
93
Stefan Reinauer5c554632012-04-04 00:09:50 +020094/*
95 * Configure processor power limits if possible
96 * This must be done AFTER set of BIOS_RESET_CPL
97 */
98void set_power_limits(u8 power_limit_1_time)
99{
100 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
101 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700102 unsigned int power_unit;
103 unsigned int tdp, min_power, max_power, max_time;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200104 u8 power_limit_1_val;
105
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000106 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Stefan Reinauer5c554632012-04-04 00:09:50 +0200107 return;
108
109 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
110 return;
111
112 /* Get units */
113 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
114 power_unit = 2 << ((msr.lo & 0xf) - 1);
115
116 /* Get power defaults for this SKU */
117 msr = rdmsr(MSR_PKG_POWER_SKU);
118 tdp = msr.lo & 0x7fff;
119 min_power = (msr.lo >> 16) & 0x7fff;
120 max_power = msr.hi & 0x7fff;
121 max_time = (msr.hi >> 16) & 0x7f;
122
123 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
124
125 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
126 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
127
128 if (min_power > 0 && tdp < min_power)
129 tdp = min_power;
130
131 if (max_power > 0 && tdp > max_power)
132 tdp = max_power;
133
134 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
135
136 /* Set long term power limit to TDP */
137 limit.lo = 0;
138 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
139 limit.lo |= PKG_POWER_LIMIT_EN;
140 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
141 PKG_POWER_LIMIT_TIME_SHIFT;
142
143 /* Set short term power limit to 1.25 * TDP */
144 limit.hi = 0;
145 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
146 limit.hi |= PKG_POWER_LIMIT_EN;
147 /* Power limit 2 time is only programmable on SNB EP/EX */
148
149 wrmsr(MSR_PKG_POWER_LIMIT, limit);
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700150
151 /* Use nominal TDP values for CPUs with configurable TDP */
152 if (cpu_config_tdp_levels()) {
153 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
154 limit.hi = 0;
155 limit.lo = msr.lo & 0xff;
156 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
157 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200158}
159
160static void configure_c_states(void)
161{
162 msr_t msr;
163
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200164 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200165 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
166 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
167 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
168 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
169 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
170 msr.lo |= 7; // No package C-state limit
Patrick Rudolph573481b2020-03-02 14:21:32 +0100171
172 msr.lo |= (1 << 15); // Lock C-State MSR
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200173 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200174
Stefan Reinauer5c554632012-04-04 00:09:50 +0200175 msr = rdmsr(MSR_MISC_PWR_MGMT);
176 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
177 wrmsr(MSR_MISC_PWR_MGMT, msr);
178
179 msr = rdmsr(MSR_POWER_CTL);
180 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
181 msr.lo |= (1 << 1); // C1E Enable
182 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
183 wrmsr(MSR_POWER_CTL, msr);
184
185 /* C3 Interrupt Response Time Limit */
186 msr.hi = 0;
187 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
188 wrmsr(MSR_PKGC3_IRTL, msr);
189
190 /* C6 Interrupt Response Time Limit */
191 msr.hi = 0;
192 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
193 wrmsr(MSR_PKGC6_IRTL, msr);
194
195 /* C7 Interrupt Response Time Limit */
196 msr.hi = 0;
197 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
198 wrmsr(MSR_PKGC7_IRTL, msr);
199
200 /* Primary Plane Current Limit */
201 msr = rdmsr(MSR_PP0_CURRENT_CONFIG);
202 msr.lo &= ~0x1fff;
203 msr.lo |= PP0_CURRENT_LIMIT;
204 wrmsr(MSR_PP0_CURRENT_CONFIG, msr);
205
206 /* Secondary Plane Current Limit */
207 msr = rdmsr(MSR_PP1_CURRENT_CONFIG);
208 msr.lo &= ~0x1fff;
Duncan Laurie4e4320f2012-06-25 09:53:58 -0700209 if (cpuid_eax(1) >= 0x30600)
210 msr.lo |= PP1_CURRENT_LIMIT_IVB;
211 else
212 msr.lo |= PP1_CURRENT_LIMIT_SNB;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200213 wrmsr(MSR_PP1_CURRENT_CONFIG, msr);
214}
215
Duncan Laurie55632112012-07-16 12:19:00 -0700216static void configure_thermal_target(void)
217{
218 struct cpu_intel_model_206ax_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100219 struct device *lapic;
Duncan Laurie55632112012-07-16 12:19:00 -0700220 msr_t msr;
221
222 /* Find pointer to CPU configuration */
223 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
224 if (!lapic || !lapic->chip_info)
225 return;
226 conf = lapic->chip_info;
227
Martin Roth4c3ab732013-07-08 16:23:54 -0600228 /* Set TCC activation offset if supported */
Duncan Laurie55632112012-07-16 12:19:00 -0700229 msr = rdmsr(MSR_PLATFORM_INFO);
230 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
231 msr = rdmsr(MSR_TEMPERATURE_TARGET);
232 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
233 msr.lo |= (conf->tcc_offset & 0xf) << 24;
234 wrmsr(MSR_TEMPERATURE_TARGET, msr);
235 }
236}
237
Stefan Reinauer5c554632012-04-04 00:09:50 +0200238static void configure_misc(void)
239{
240 msr_t msr;
241
242 msr = rdmsr(IA32_MISC_ENABLE);
243 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700244 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200245 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
246 wrmsr(IA32_MISC_ENABLE, msr);
247
248 /* Disable Thermal interrupts */
249 msr.lo = 0;
250 msr.hi = 0;
251 wrmsr(IA32_THERM_INTERRUPT, msr);
252
253 /* Enable package critical interrupt only */
254 msr.lo = 1 << 4;
255 msr.hi = 0;
256 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
257}
258
Stefan Reinauer5c554632012-04-04 00:09:50 +0200259static void set_max_ratio(void)
260{
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700261 msr_t msr, perf_ctl;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200262
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700263 perf_ctl.hi = 0;
264
265 /* Check for configurable TDP option */
266 if (cpu_config_tdp_levels()) {
267 /* Set to nominal TDP ratio */
268 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
269 perf_ctl.lo = (msr.lo & 0xff) << 8;
270 } else {
271 /* Platform Info bits 15:8 give max ratio */
272 msr = rdmsr(MSR_PLATFORM_INFO);
273 perf_ctl.lo = msr.lo & 0xff00;
274 }
275 wrmsr(IA32_PERF_CTL, perf_ctl);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200276
277 printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700278 ((perf_ctl.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200279}
280
Michał Żygowski0d11dbf2020-10-31 21:43:25 +0100281unsigned int smbios_cpu_get_max_speed_mhz(void)
282{
283 msr_t msr;
284 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
285 return (msr.lo & 0xff) * SANDYBRIDGE_BCLK;
286}
287
288unsigned int smbios_cpu_get_current_speed_mhz(void)
289{
290 msr_t msr;
291 msr = rdmsr(MSR_PLATFORM_INFO);
292 return ((msr.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK;
293}
294
295unsigned int smbios_processor_external_clock(void)
296{
297 return SANDYBRIDGE_BCLK;
298}
299
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200300static void model_206ax_report(void)
301{
302 static const char *const mode[] = {"NOT ", ""};
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200303 char processor_name[49];
304 int vt, txt, aes;
Subrata Banik53b08c32018-12-10 14:11:35 +0530305 uint32_t cpu_id, cpu_feature_flag;
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200306
307 /* Print processor name */
308 fill_processor_name(processor_name);
309 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
310
311 /* Print platform ID */
312 printk(BIOS_INFO, "CPU: platform id %x\n", get_platform_id());
313
314 /* CPUID and features */
Subrata Banik53b08c32018-12-10 14:11:35 +0530315 cpu_id = cpu_get_cpuid();
316 printk(BIOS_INFO, "CPU: cpuid(1) 0x%x\n", cpu_id);
317
318 cpu_feature_flag = cpu_get_feature_flags_ecx();
319 aes = (cpu_feature_flag & CPUID_AES) ? 1 : 0;
320 txt = (cpu_feature_flag & CPUID_SMX) ? 1 : 0;
321 vt = (cpu_feature_flag & CPUID_VMX) ? 1 : 0;
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200322 printk(BIOS_INFO, "CPU: AES %ssupported\n", mode[aes]);
323 printk(BIOS_INFO, "CPU: TXT %ssupported\n", mode[txt]);
324 printk(BIOS_INFO, "CPU: VT %ssupported\n", mode[vt]);
325}
326
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100327static void model_206ax_init(struct device *cpu)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200328{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200329
Stefan Reinauer5c554632012-04-04 00:09:50 +0200330 /* Clear out pending MCEs */
Felix Heldacbf1542021-07-13 16:44:18 +0200331 /* This should only be done on a cold boot */
332 mca_clear_status();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200333
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200334 /* Print infos */
335 model_206ax_report();
Patrick Rudolph74203de2017-11-20 11:57:01 +0100336
Stefan Reinauer5c554632012-04-04 00:09:50 +0200337 /* Setup Page Attribute Tables (PAT) */
338 // TODO set up PAT
339
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200340 /* Enable the local CPU APICs */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200341 enable_lapic_tpr();
342 setup_lapic();
343
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600344 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600345 set_vmx_and_lock();
Marc Jones5986eda2012-10-25 09:37:19 -0600346
Stefan Reinauer5c554632012-04-04 00:09:50 +0200347 /* Configure C States */
348 configure_c_states();
349
350 /* Configure Enhanced SpeedStep and Thermal Sensors */
351 configure_misc();
352
Duncan Laurie55632112012-07-16 12:19:00 -0700353 /* Thermal throttle activation offset */
354 configure_thermal_target();
355
Michael Niewöhner63032432020-10-11 17:34:54 +0200356 set_aesni_lock();
Michael Niewöhner7f8767d2020-10-18 00:45:38 +0200357
Stefan Reinauer5c554632012-04-04 00:09:50 +0200358 /* Enable Direct Cache Access */
359 configure_dca_cap();
360
361 /* Set energy policy */
362 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
363
364 /* Set Max Ratio */
365 set_max_ratio();
366
367 /* Enable Turbo */
368 enable_turbo();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100369}
Sven Schnelle51676b12012-07-29 19:18:03 +0200370
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100371/* MP initialization support. */
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100372static void pre_mp_init(void)
373{
374 /* Setup MTRRs based on physical address size. */
375 x86_setup_mtrrs_with_detect();
376 x86_mtrr_check();
377}
378
379static int get_cpu_count(void)
380{
381 msr_t msr;
382 int num_threads;
383 int num_cores;
384
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200385 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100386 num_threads = (msr.lo >> 0) & 0xffff;
387 num_cores = (msr.lo >> 16) & 0xffff;
388 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
389 num_cores, num_threads);
390
391 return num_threads;
392}
393
394static void get_microcode_info(const void **microcode, int *parallel)
395{
Patrick Rudolph3fa23b82021-01-25 09:42:08 +0100396 *microcode = intel_microcode_find();
Patrick Rudolphce51b342021-01-11 09:21:58 +0100397 *parallel = !intel_ht_supported();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100398}
399
400static void per_cpu_smm_trigger(void)
401{
402 /* Relocate the SMM handler. */
403 smm_relocate();
404
405 /* After SMM relocation a 2nd microcode load is required. */
Patrick Rudolph3fa23b82021-01-25 09:42:08 +0100406 const void *microcode_patch = intel_microcode_find();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100407 intel_microcode_load_unlocked(microcode_patch);
408}
409
410static void post_mp_init(void)
411{
412 /* Now that all APs have been relocated as well as the BSP let SMIs
413 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300414 global_smi_enable();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100415
416 /* Lock down the SMRAM space. */
417 smm_lock();
418}
419
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100420static const struct mp_ops mp_ops = {
421 .pre_mp_init = pre_mp_init,
422 .get_cpu_count = get_cpu_count,
423 .get_smm_info = smm_info,
424 .get_microcode_info = get_microcode_info,
425 .pre_mp_smm_init = smm_initialize,
426 .per_cpu_smm_trigger = per_cpu_smm_trigger,
427 .relocation_handler = smm_relocation_handler,
428 .post_mp_init = post_mp_init,
429};
430
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300431void mp_init_cpus(struct bus *cpu_bus)
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100432{
433 if (mp_init_with_smm(cpu_bus, &mp_ops))
434 printk(BIOS_ERR, "MP initialization failure.\n");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200435}
436
437static struct device_operations cpu_dev_ops = {
438 .init = model_206ax_init,
439};
440
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100441static const struct cpu_device_id cpu_table[] = {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200442 { X86_VENDOR_INTEL, 0x206a0 }, /* Intel Sandybridge */
443 { X86_VENDOR_INTEL, 0x206a6 }, /* Intel Sandybridge D1 */
444 { X86_VENDOR_INTEL, 0x206a7 }, /* Intel Sandybridge D2/J1 */
Stefan Reinauer08067ba2012-10-15 13:47:04 -0700445 { X86_VENDOR_INTEL, 0x306a0 }, /* Intel IvyBridge */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200446 { X86_VENDOR_INTEL, 0x306a2 }, /* Intel IvyBridge */
447 { X86_VENDOR_INTEL, 0x306a4 }, /* Intel IvyBridge */
448 { X86_VENDOR_INTEL, 0x306a5 }, /* Intel IvyBridge */
449 { X86_VENDOR_INTEL, 0x306a6 }, /* Intel IvyBridge */
450 { X86_VENDOR_INTEL, 0x306a8 }, /* Intel IvyBridge */
451 { X86_VENDOR_INTEL, 0x306a9 }, /* Intel IvyBridge */
452 { 0, 0 },
453};
454
455static const struct cpu_driver driver __cpu_driver = {
456 .ops = &cpu_dev_ops,
457 .id_table = cpu_table,
Stefan Reinauer5c554632012-04-04 00:09:50 +0200458};