blob: 8bb10916873f37f9b11bcb5750b76631d6b2efa9 [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>
Stefan Reinauer5c554632012-04-04 00:09:50 +02005#include <cpu/cpu.h>
6#include <cpu/x86/mtrr.h>
7#include <cpu/x86/msr.h>
Arthur Heymansedbf5d92018-01-25 20:03:42 +01008#include <cpu/x86/mp.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +02009#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>
Stefan Reinauer5c554632012-04-04 00:09:50 +020014#include "model_206ax.h"
Duncan Laurie55632112012-07-16 12:19:00 -070015#include "chip.h"
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030016#include <cpu/intel/smm_reloc.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060017#include <cpu/intel/common/common.h>
Michał Żygowski0d11dbf2020-10-31 21:43:25 +010018#include <smbios.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020019#include <types.h>
Vladimir Serbinenkoc16e9dfa2015-05-29 16:18:01 +020020
Stefan Reinauer5c554632012-04-04 00:09:50 +020021/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
22static const u8 power_limit_time_sec_to_msr[] = {
23 [0] = 0x00,
24 [1] = 0x0a,
25 [2] = 0x0b,
26 [3] = 0x4b,
27 [4] = 0x0c,
28 [5] = 0x2c,
29 [6] = 0x4c,
30 [7] = 0x6c,
31 [8] = 0x0d,
32 [10] = 0x2d,
33 [12] = 0x4d,
34 [14] = 0x6d,
35 [16] = 0x0e,
36 [20] = 0x2e,
37 [24] = 0x4e,
38 [28] = 0x6e,
39 [32] = 0x0f,
40 [40] = 0x2f,
41 [48] = 0x4f,
42 [56] = 0x6f,
43 [64] = 0x10,
44 [80] = 0x30,
45 [96] = 0x50,
46 [112] = 0x70,
47 [128] = 0x11,
48};
49
50/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
51static const u8 power_limit_time_msr_to_sec[] = {
52 [0x00] = 0,
53 [0x0a] = 1,
54 [0x0b] = 2,
55 [0x4b] = 3,
56 [0x0c] = 4,
57 [0x2c] = 5,
58 [0x4c] = 6,
59 [0x6c] = 7,
60 [0x0d] = 8,
61 [0x2d] = 10,
62 [0x4d] = 12,
63 [0x6d] = 14,
64 [0x0e] = 16,
65 [0x2e] = 20,
66 [0x4e] = 24,
67 [0x6e] = 28,
68 [0x0f] = 32,
69 [0x2f] = 40,
70 [0x4f] = 48,
71 [0x6f] = 56,
72 [0x10] = 64,
73 [0x30] = 80,
74 [0x50] = 96,
75 [0x70] = 112,
76 [0x11] = 128,
77};
78
Duncan Laurie77dbbac2012-06-25 09:51:59 -070079int cpu_config_tdp_levels(void)
80{
81 msr_t platform_info;
82
83 /* Minimum CPU revision */
84 if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID)
85 return 0;
86
87 /* Bits 34:33 indicate how many levels supported */
88 platform_info = rdmsr(MSR_PLATFORM_INFO);
89 return (platform_info.hi >> 1) & 3;
90}
91
Stefan Reinauer5c554632012-04-04 00:09:50 +020092/*
93 * Configure processor power limits if possible
94 * This must be done AFTER set of BIOS_RESET_CPL
95 */
96void set_power_limits(u8 power_limit_1_time)
97{
98 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
99 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700100 unsigned int power_unit;
101 unsigned int tdp, min_power, max_power, max_time;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200102 u8 power_limit_1_val;
103
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000104 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Stefan Reinauer5c554632012-04-04 00:09:50 +0200105 return;
106
107 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
108 return;
109
110 /* Get units */
111 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
112 power_unit = 2 << ((msr.lo & 0xf) - 1);
113
114 /* Get power defaults for this SKU */
115 msr = rdmsr(MSR_PKG_POWER_SKU);
116 tdp = msr.lo & 0x7fff;
117 min_power = (msr.lo >> 16) & 0x7fff;
118 max_power = msr.hi & 0x7fff;
119 max_time = (msr.hi >> 16) & 0x7f;
120
121 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
122
123 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
124 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
125
126 if (min_power > 0 && tdp < min_power)
127 tdp = min_power;
128
129 if (max_power > 0 && tdp > max_power)
130 tdp = max_power;
131
132 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
133
134 /* Set long term power limit to TDP */
135 limit.lo = 0;
136 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
137 limit.lo |= PKG_POWER_LIMIT_EN;
138 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
139 PKG_POWER_LIMIT_TIME_SHIFT;
140
141 /* Set short term power limit to 1.25 * TDP */
142 limit.hi = 0;
143 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
144 limit.hi |= PKG_POWER_LIMIT_EN;
145 /* Power limit 2 time is only programmable on SNB EP/EX */
146
147 wrmsr(MSR_PKG_POWER_LIMIT, limit);
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700148
149 /* Use nominal TDP values for CPUs with configurable TDP */
150 if (cpu_config_tdp_levels()) {
151 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
152 limit.hi = 0;
153 limit.lo = msr.lo & 0xff;
154 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
155 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200156}
157
158static void configure_c_states(void)
159{
160 msr_t msr;
161
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200162 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200163 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
164 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
165 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
166 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
167 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
168 msr.lo |= 7; // No package C-state limit
Patrick Rudolph573481b2020-03-02 14:21:32 +0100169
170 msr.lo |= (1 << 15); // Lock C-State MSR
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200171 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200172
Stefan Reinauer5c554632012-04-04 00:09:50 +0200173 msr = rdmsr(MSR_MISC_PWR_MGMT);
174 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
175 wrmsr(MSR_MISC_PWR_MGMT, msr);
176
177 msr = rdmsr(MSR_POWER_CTL);
178 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
179 msr.lo |= (1 << 1); // C1E Enable
180 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
181 wrmsr(MSR_POWER_CTL, msr);
182
183 /* C3 Interrupt Response Time Limit */
184 msr.hi = 0;
185 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
186 wrmsr(MSR_PKGC3_IRTL, msr);
187
188 /* C6 Interrupt Response Time Limit */
189 msr.hi = 0;
190 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
191 wrmsr(MSR_PKGC6_IRTL, msr);
192
193 /* C7 Interrupt Response Time Limit */
194 msr.hi = 0;
195 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
196 wrmsr(MSR_PKGC7_IRTL, msr);
197
198 /* Primary Plane Current Limit */
199 msr = rdmsr(MSR_PP0_CURRENT_CONFIG);
200 msr.lo &= ~0x1fff;
201 msr.lo |= PP0_CURRENT_LIMIT;
202 wrmsr(MSR_PP0_CURRENT_CONFIG, msr);
203
204 /* Secondary Plane Current Limit */
205 msr = rdmsr(MSR_PP1_CURRENT_CONFIG);
206 msr.lo &= ~0x1fff;
Duncan Laurie4e4320f2012-06-25 09:53:58 -0700207 if (cpuid_eax(1) >= 0x30600)
208 msr.lo |= PP1_CURRENT_LIMIT_IVB;
209 else
210 msr.lo |= PP1_CURRENT_LIMIT_SNB;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200211 wrmsr(MSR_PP1_CURRENT_CONFIG, msr);
212}
213
Duncan Laurie55632112012-07-16 12:19:00 -0700214static void configure_thermal_target(void)
215{
216 struct cpu_intel_model_206ax_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100217 struct device *lapic;
Duncan Laurie55632112012-07-16 12:19:00 -0700218 msr_t msr;
219
220 /* Find pointer to CPU configuration */
221 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
222 if (!lapic || !lapic->chip_info)
223 return;
224 conf = lapic->chip_info;
225
Martin Roth4c3ab732013-07-08 16:23:54 -0600226 /* Set TCC activation offset if supported */
Duncan Laurie55632112012-07-16 12:19:00 -0700227 msr = rdmsr(MSR_PLATFORM_INFO);
228 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
229 msr = rdmsr(MSR_TEMPERATURE_TARGET);
230 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
231 msr.lo |= (conf->tcc_offset & 0xf) << 24;
232 wrmsr(MSR_TEMPERATURE_TARGET, msr);
233 }
234}
235
Stefan Reinauer5c554632012-04-04 00:09:50 +0200236static void configure_misc(void)
237{
238 msr_t msr;
239
240 msr = rdmsr(IA32_MISC_ENABLE);
241 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700242 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200243 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
244 wrmsr(IA32_MISC_ENABLE, msr);
245
246 /* Disable Thermal interrupts */
247 msr.lo = 0;
248 msr.hi = 0;
249 wrmsr(IA32_THERM_INTERRUPT, msr);
250
251 /* Enable package critical interrupt only */
252 msr.lo = 1 << 4;
253 msr.hi = 0;
254 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
255}
256
Stefan Reinauer5c554632012-04-04 00:09:50 +0200257static void set_max_ratio(void)
258{
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700259 msr_t msr, perf_ctl;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200260
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700261 perf_ctl.hi = 0;
262
263 /* Check for configurable TDP option */
264 if (cpu_config_tdp_levels()) {
265 /* Set to nominal TDP ratio */
266 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
267 perf_ctl.lo = (msr.lo & 0xff) << 8;
268 } else {
269 /* Platform Info bits 15:8 give max ratio */
270 msr = rdmsr(MSR_PLATFORM_INFO);
271 perf_ctl.lo = msr.lo & 0xff00;
272 }
273 wrmsr(IA32_PERF_CTL, perf_ctl);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200274
275 printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700276 ((perf_ctl.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200277}
278
Michał Żygowski0d11dbf2020-10-31 21:43:25 +0100279unsigned int smbios_cpu_get_max_speed_mhz(void)
280{
281 msr_t msr;
282 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
283 return (msr.lo & 0xff) * SANDYBRIDGE_BCLK;
284}
285
286unsigned int smbios_cpu_get_current_speed_mhz(void)
287{
288 msr_t msr;
289 msr = rdmsr(MSR_PLATFORM_INFO);
290 return ((msr.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK;
291}
292
293unsigned int smbios_processor_external_clock(void)
294{
295 return SANDYBRIDGE_BCLK;
296}
297
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200298static void model_206ax_report(void)
299{
300 static const char *const mode[] = {"NOT ", ""};
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200301 char processor_name[49];
302 int vt, txt, aes;
Subrata Banik53b08c32018-12-10 14:11:35 +0530303 uint32_t cpu_id, cpu_feature_flag;
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200304
305 /* Print processor name */
306 fill_processor_name(processor_name);
307 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
308
309 /* Print platform ID */
310 printk(BIOS_INFO, "CPU: platform id %x\n", get_platform_id());
311
312 /* CPUID and features */
Subrata Banik53b08c32018-12-10 14:11:35 +0530313 cpu_id = cpu_get_cpuid();
314 printk(BIOS_INFO, "CPU: cpuid(1) 0x%x\n", cpu_id);
315
316 cpu_feature_flag = cpu_get_feature_flags_ecx();
317 aes = (cpu_feature_flag & CPUID_AES) ? 1 : 0;
318 txt = (cpu_feature_flag & CPUID_SMX) ? 1 : 0;
319 vt = (cpu_feature_flag & CPUID_VMX) ? 1 : 0;
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200320 printk(BIOS_INFO, "CPU: AES %ssupported\n", mode[aes]);
321 printk(BIOS_INFO, "CPU: TXT %ssupported\n", mode[txt]);
322 printk(BIOS_INFO, "CPU: VT %ssupported\n", mode[vt]);
323}
324
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100325static void model_206ax_init(struct device *cpu)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200326{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200327 /* Clear out pending MCEs */
Felix Heldacbf1542021-07-13 16:44:18 +0200328 /* This should only be done on a cold boot */
329 mca_clear_status();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200330
Patrick Rudolph9e1b9b52018-07-27 17:25:05 +0200331 /* Print infos */
332 model_206ax_report();
Patrick Rudolph74203de2017-11-20 11:57:01 +0100333
Stefan Reinauer5c554632012-04-04 00:09:50 +0200334 /* Setup Page Attribute Tables (PAT) */
335 // TODO set up PAT
336
Stefan Reinauer5c554632012-04-04 00:09:50 +0200337 enable_lapic_tpr();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200338
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600339 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600340 set_vmx_and_lock();
Marc Jones5986eda2012-10-25 09:37:19 -0600341
Stefan Reinauer5c554632012-04-04 00:09:50 +0200342 /* Configure C States */
343 configure_c_states();
344
345 /* Configure Enhanced SpeedStep and Thermal Sensors */
346 configure_misc();
347
Duncan Laurie55632112012-07-16 12:19:00 -0700348 /* Thermal throttle activation offset */
349 configure_thermal_target();
350
Michael Niewöhner63032432020-10-11 17:34:54 +0200351 set_aesni_lock();
Michael Niewöhner7f8767d2020-10-18 00:45:38 +0200352
Stefan Reinauer5c554632012-04-04 00:09:50 +0200353 /* Enable Direct Cache Access */
354 configure_dca_cap();
355
356 /* Set energy policy */
357 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
358
359 /* Set Max Ratio */
360 set_max_ratio();
361
362 /* Enable Turbo */
363 enable_turbo();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100364}
Sven Schnelle51676b12012-07-29 19:18:03 +0200365
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100366/* MP initialization support. */
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100367static void pre_mp_init(void)
368{
369 /* Setup MTRRs based on physical address size. */
370 x86_setup_mtrrs_with_detect();
371 x86_mtrr_check();
372}
373
374static int get_cpu_count(void)
375{
376 msr_t msr;
Angel Pons04c497a2021-11-03 16:30:10 +0100377 unsigned int num_threads;
378 unsigned int num_cores;
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100379
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200380 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100381 num_threads = (msr.lo >> 0) & 0xffff;
382 num_cores = (msr.lo >> 16) & 0xffff;
383 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
384 num_cores, num_threads);
385
386 return num_threads;
387}
388
389static void get_microcode_info(const void **microcode, int *parallel)
390{
Patrick Rudolph3fa23b82021-01-25 09:42:08 +0100391 *microcode = intel_microcode_find();
Patrick Rudolphce51b342021-01-11 09:21:58 +0100392 *parallel = !intel_ht_supported();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100393}
394
395static void per_cpu_smm_trigger(void)
396{
397 /* Relocate the SMM handler. */
398 smm_relocate();
399
400 /* After SMM relocation a 2nd microcode load is required. */
Patrick Rudolph3fa23b82021-01-25 09:42:08 +0100401 const void *microcode_patch = intel_microcode_find();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100402 intel_microcode_load_unlocked(microcode_patch);
403}
404
405static void post_mp_init(void)
406{
407 /* Now that all APs have been relocated as well as the BSP let SMIs
408 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300409 global_smi_enable();
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100410
411 /* Lock down the SMRAM space. */
412 smm_lock();
413}
414
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100415static const struct mp_ops mp_ops = {
416 .pre_mp_init = pre_mp_init,
417 .get_cpu_count = get_cpu_count,
418 .get_smm_info = smm_info,
419 .get_microcode_info = get_microcode_info,
420 .pre_mp_smm_init = smm_initialize,
421 .per_cpu_smm_trigger = per_cpu_smm_trigger,
422 .relocation_handler = smm_relocation_handler,
423 .post_mp_init = post_mp_init,
424};
425
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300426void mp_init_cpus(struct bus *cpu_bus)
Arthur Heymansedbf5d92018-01-25 20:03:42 +0100427{
Felix Held4dd7d112021-10-20 23:31:43 +0200428 /* TODO: Handle mp_init_with_smm failure? */
429 mp_init_with_smm(cpu_bus, &mp_ops);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200430}
431
432static struct device_operations cpu_dev_ops = {
433 .init = model_206ax_init,
434};
435
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100436static const struct cpu_device_id cpu_table[] = {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200437 { X86_VENDOR_INTEL, 0x206a0 }, /* Intel Sandybridge */
438 { X86_VENDOR_INTEL, 0x206a6 }, /* Intel Sandybridge D1 */
439 { X86_VENDOR_INTEL, 0x206a7 }, /* Intel Sandybridge D2/J1 */
Stefan Reinauer08067ba2012-10-15 13:47:04 -0700440 { X86_VENDOR_INTEL, 0x306a0 }, /* Intel IvyBridge */
Stefan Reinauer5c554632012-04-04 00:09:50 +0200441 { X86_VENDOR_INTEL, 0x306a2 }, /* Intel IvyBridge */
442 { X86_VENDOR_INTEL, 0x306a4 }, /* Intel IvyBridge */
443 { X86_VENDOR_INTEL, 0x306a5 }, /* Intel IvyBridge */
444 { X86_VENDOR_INTEL, 0x306a6 }, /* Intel IvyBridge */
445 { X86_VENDOR_INTEL, 0x306a8 }, /* Intel IvyBridge */
446 { X86_VENDOR_INTEL, 0x306a9 }, /* Intel IvyBridge */
447 { 0, 0 },
448};
449
450static const struct cpu_driver driver __cpu_driver = {
451 .ops = &cpu_dev_ops,
452 .id_table = cpu_table,
Stefan Reinauer5c554632012-04-04 00:09:50 +0200453};