blob: 0e48876d10fc3ebc1077601c3078efcc5c49115a [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <console/console.h>
4#include <device/device.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <cpu/cpu.h>
7#include <cpu/x86/mtrr.h>
8#include <cpu/x86/msr.h>
Aaron Durbin014baea2014-03-28 22:01:05 -05009#include <cpu/x86/mp.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050010#include <cpu/x86/lapic.h>
11#include <cpu/intel/microcode.h>
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +030012#include <cpu/intel/smm_reloc.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050013#include <cpu/intel/speedstep.h>
14#include <cpu/intel/turbo.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050015#include <cpu/x86/name.h>
Aaron Durbinf24262d2013-04-10 14:59:21 -050016#include <delay.h>
Aaron Durbin7c351312013-04-10 14:46:25 -050017#include <northbridge/intel/haswell/haswell.h>
18#include <southbridge/intel/lynxpoint/pch.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060019#include <cpu/intel/common/common.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include "haswell.h"
21#include "chip.h"
22
Aaron Durbin7c351312013-04-10 14:46:25 -050023#define MWAIT_RES(state, sub_state) \
24 { \
25 .addrl = (((state) << 4) | (sub_state)), \
26 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
27 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
28 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
29 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
30 }
31
32static acpi_cstate_t cstate_map[NUM_C_STATES] = {
33 [C_STATE_C0] = { },
34 [C_STATE_C1] = {
35 .latency = 0,
36 .power = 1000,
Lee Leahy9d62e7e2017-03-15 17:40:50 -070037 .resource = MWAIT_RES(0, 0),
Aaron Durbin7c351312013-04-10 14:46:25 -050038 },
39 [C_STATE_C1E] = {
40 .latency = 0,
41 .power = 1000,
Lee Leahy9d62e7e2017-03-15 17:40:50 -070042 .resource = MWAIT_RES(0, 1),
Aaron Durbin7c351312013-04-10 14:46:25 -050043 },
44 [C_STATE_C3] = {
45 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
46 .power = 900,
47 .resource = MWAIT_RES(1, 0),
48 },
49 [C_STATE_C6_SHORT_LAT] = {
50 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
51 .power = 800,
52 .resource = MWAIT_RES(2, 0),
53 },
54 [C_STATE_C6_LONG_LAT] = {
55 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
56 .power = 800,
57 .resource = MWAIT_RES(2, 1),
58 },
59 [C_STATE_C7_SHORT_LAT] = {
60 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
61 .power = 700,
62 .resource = MWAIT_RES(3, 0),
63 },
64 [C_STATE_C7_LONG_LAT] = {
65 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
66 .power = 700,
67 .resource = MWAIT_RES(3, 1),
68 },
69 [C_STATE_C7S_SHORT_LAT] = {
70 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
71 .power = 700,
72 .resource = MWAIT_RES(3, 2),
73 },
74 [C_STATE_C7S_LONG_LAT] = {
75 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
76 .power = 700,
77 .resource = MWAIT_RES(3, 3),
78 },
79 [C_STATE_C8] = {
80 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
81 .power = 600,
82 .resource = MWAIT_RES(4, 0),
83 },
84 [C_STATE_C9] = {
85 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
86 .power = 500,
87 .resource = MWAIT_RES(5, 0),
88 },
89 [C_STATE_C10] = {
90 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
91 .power = 400,
92 .resource = MWAIT_RES(6, 0),
93 },
94};
Aaron Durbin76c37002012-10-30 09:03:43 -050095
96/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
97static const u8 power_limit_time_sec_to_msr[] = {
98 [0] = 0x00,
99 [1] = 0x0a,
100 [2] = 0x0b,
101 [3] = 0x4b,
102 [4] = 0x0c,
103 [5] = 0x2c,
104 [6] = 0x4c,
105 [7] = 0x6c,
106 [8] = 0x0d,
107 [10] = 0x2d,
108 [12] = 0x4d,
109 [14] = 0x6d,
110 [16] = 0x0e,
111 [20] = 0x2e,
112 [24] = 0x4e,
113 [28] = 0x6e,
114 [32] = 0x0f,
115 [40] = 0x2f,
116 [48] = 0x4f,
117 [56] = 0x6f,
118 [64] = 0x10,
119 [80] = 0x30,
120 [96] = 0x50,
121 [112] = 0x70,
122 [128] = 0x11,
123};
124
125/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
126static const u8 power_limit_time_msr_to_sec[] = {
127 [0x00] = 0,
128 [0x0a] = 1,
129 [0x0b] = 2,
130 [0x4b] = 3,
131 [0x0c] = 4,
132 [0x2c] = 5,
133 [0x4c] = 6,
134 [0x6c] = 7,
135 [0x0d] = 8,
136 [0x2d] = 10,
137 [0x4d] = 12,
138 [0x6d] = 14,
139 [0x0e] = 16,
140 [0x2e] = 20,
141 [0x4e] = 24,
142 [0x6e] = 28,
143 [0x0f] = 32,
144 [0x2f] = 40,
145 [0x4f] = 48,
146 [0x6f] = 56,
147 [0x10] = 64,
148 [0x30] = 80,
149 [0x50] = 96,
150 [0x70] = 112,
151 [0x11] = 128,
152};
153
Angel Pons5d92aa52020-10-14 00:02:37 +0200154/* The core 100MHz BCLK is disabled in deeper c-states. One needs to calibrate
155 * the 100MHz BCLK against the 24MHz BCLK to restore the clocks properly
Aaron Durbinf24262d2013-04-10 14:59:21 -0500156 * when a core is woken up. */
157static int pcode_ready(void)
158{
159 int wait_count;
160 const int delay_step = 10;
161
162 wait_count = 0;
163 do {
164 if (!(MCHBAR32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
165 return 0;
166 wait_count += delay_step;
167 udelay(delay_step);
168 } while (wait_count < 1000);
169
170 return -1;
171}
172
173static void calibrate_24mhz_bclk(void)
174{
175 int err_code;
176
177 if (pcode_ready() < 0) {
178 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
179 return;
180 }
181
182 /* A non-zero value initiates the PCODE calibration. */
183 MCHBAR32(BIOS_MAILBOX_DATA) = ~0;
184 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
185 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL;
186
187 if (pcode_ready() < 0) {
188 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
189 return;
190 }
191
192 err_code = MCHBAR32(BIOS_MAILBOX_INTERFACE) & 0xff;
193
Angel Pons5d92aa52020-10-14 00:02:37 +0200194 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration response: %d\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500195 err_code);
196
197 /* Read the calibrated value. */
198 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
199 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION;
200
201 if (pcode_ready() < 0) {
202 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
203 return;
204 }
205
Angel Pons5d92aa52020-10-14 00:02:37 +0200206 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration value: 0x%08x\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500207 MCHBAR32(BIOS_MAILBOX_DATA));
208}
209
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700210static u32 pcode_mailbox_read(u32 command)
211{
212 if (pcode_ready() < 0) {
213 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
214 return 0;
215 }
216
217 /* Send command and start transaction */
218 MCHBAR32(BIOS_MAILBOX_INTERFACE) = command | MAILBOX_RUN_BUSY;
219
220 if (pcode_ready() < 0) {
221 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
222 return 0;
223 }
224
225 /* Read mailbox */
226 return MCHBAR32(BIOS_MAILBOX_DATA);
227}
228
Aaron Durbin16cbf892013-07-03 16:21:28 -0500229static void initialize_vr_config(void)
230{
Angel Pons242fd282020-10-28 23:48:56 +0100231 struct cpu_vr_config vr_config = { 0 };
Aaron Durbin16cbf892013-07-03 16:21:28 -0500232 msr_t msr;
233
Angel Pons242fd282020-10-28 23:48:56 +0100234 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
235
236 if (lapic && lapic->chip_info) {
237 const struct cpu_intel_haswell_config *conf = lapic->chip_info;
238
239 vr_config = conf->vr_config;
240 }
241
Aaron Durbin16cbf892013-07-03 16:21:28 -0500242 printk(BIOS_DEBUG, "Initializing VR config.\n");
243
244 /* Configure VR_CURRENT_CONFIG. */
245 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
246 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
247 * on ULT systems. */
248 msr.hi &= 0xc0000000;
249 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
250 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
Angel Pons9dcd1c12020-10-28 22:41:26 +0100251 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A. */
Aaron Durbin16cbf892013-07-03 16:21:28 -0500252
Duncan Laurie118d1052013-07-09 15:34:25 -0700253 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500254 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
255 /* Leave the max instantaneous current limit (12:0) to default. */
256 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
257
258 /* Configure VR_MISC_CONFIG MSR. */
259 msr = rdmsr(MSR_VR_MISC_CONFIG);
260 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
261 msr.hi &= ~(0x3ff << (40 - 32));
262 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
263 /* Set IOUT_OFFSET to 0. */
264 msr.hi &= ~0xff;
265 /* Set exit ramp rate to fast. */
266 msr.hi |= (1 << (50 - 32));
267 /* Set entry ramp rate to slow. */
268 msr.hi &= ~(1 << (51 - 32));
269 /* Enable decay mode on C-state entry. */
270 msr.hi |= (1 << (52 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100271 /* Set the slow ramp rate */
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300272 if (haswell_is_ult()) {
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300273 msr.hi &= ~(0x3 << (53 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100274 /* Configure the C-state exit ramp rate. */
275 if (vr_config.slow_ramp_rate_enable) {
276 /* Configured slow ramp rate. */
277 msr.hi |= ((vr_config.slow_ramp_rate_set & 0x3) << (53 - 32));
278 /* Set exit ramp rate to slow. */
279 msr.hi &= ~(1 << (50 - 32));
280 } else {
281 /* Fast ramp rate / 4. */
282 msr.hi |= (1 << (53 - 32));
283 }
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300284 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500285 /* Set MIN_VID (31:24) to allow CPU to have full control. */
286 msr.lo &= ~0xff000000;
Angel Pons242fd282020-10-28 23:48:56 +0100287 msr.lo |= (vr_config.cpu_min_vid & 0xff) << 24;
Aaron Durbin16cbf892013-07-03 16:21:28 -0500288 wrmsr(MSR_VR_MISC_CONFIG, msr);
289
290 /* Configure VR_MISC_CONFIG2 MSR. */
Angel Pons4c95f102020-10-28 19:38:12 +0100291 if (!haswell_is_ult())
292 return;
293
294 msr = rdmsr(MSR_VR_MISC_CONFIG2);
295 msr.lo &= ~0xffff;
296 /* Allow CPU to control minimum voltage completely (15:8) and
297 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
298 msr.lo |= 0x006f;
299 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500300}
301
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700302static void configure_pch_power_sharing(void)
303{
304 u32 pch_power, pch_power_ext, pmsync, pmsync2;
305 int i;
306
307 /* Read PCH Power levels from PCODE */
308 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
309 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
310
311 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700312 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700313
314 pmsync = RCBA32(PMSYNC_CONFIG);
315 pmsync2 = RCBA32(PMSYNC_CONFIG2);
316
317 /* Program PMSYNC_TPR_CONFIG PCH power limit values
318 * pmsync[0:4] = mailbox[0:5]
319 * pmsync[8:12] = mailbox[6:11]
320 * pmsync[16:20] = mailbox[12:17]
321 */
322 for (i = 0; i < 3; i++) {
323 u32 level = pch_power & 0x3f;
324 pch_power >>= 6;
325 pmsync &= ~(0x1f << (i * 8));
326 pmsync |= (level & 0x1f) << (i * 8);
327 }
328 RCBA32(PMSYNC_CONFIG) = pmsync;
329
330 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
331 * pmsync2[0:4] = mailbox[23:18]
332 * pmsync2[8:12] = mailbox_ext[6:11]
333 * pmsync2[16:20] = mailbox_ext[12:17]
334 * pmsync2[24:28] = mailbox_ext[18:22]
335 */
336 pmsync2 &= ~0x1f;
337 pmsync2 |= pch_power & 0x1f;
338
339 for (i = 1; i < 4; i++) {
340 u32 level = pch_power_ext & 0x3f;
341 pch_power_ext >>= 6;
342 pmsync2 &= ~(0x1f << (i * 8));
343 pmsync2 |= (level & 0x1f) << (i * 8);
344 }
345 RCBA32(PMSYNC_CONFIG2) = pmsync2;
346}
347
Aaron Durbin76c37002012-10-30 09:03:43 -0500348int cpu_config_tdp_levels(void)
349{
350 msr_t platform_info;
351
352 /* Bits 34:33 indicate how many levels supported */
353 platform_info = rdmsr(MSR_PLATFORM_INFO);
354 return (platform_info.hi >> 1) & 3;
355}
356
357/*
358 * Configure processor power limits if possible
359 * This must be done AFTER set of BIOS_RESET_CPL
360 */
361void set_power_limits(u8 power_limit_1_time)
362{
363 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
364 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700365 unsigned int power_unit;
366 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500367 u8 power_limit_1_val;
368
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000369 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100370 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500371
372 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
373 return;
374
375 /* Get units */
376 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
377 power_unit = 2 << ((msr.lo & 0xf) - 1);
378
379 /* Get power defaults for this SKU */
380 msr = rdmsr(MSR_PKG_POWER_SKU);
381 tdp = msr.lo & 0x7fff;
382 min_power = (msr.lo >> 16) & 0x7fff;
383 max_power = msr.hi & 0x7fff;
384 max_time = (msr.hi >> 16) & 0x7f;
385
386 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
387
388 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
389 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
390
391 if (min_power > 0 && tdp < min_power)
392 tdp = min_power;
393
394 if (max_power > 0 && tdp > max_power)
395 tdp = max_power;
396
397 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
398
399 /* Set long term power limit to TDP */
400 limit.lo = 0;
401 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
402 limit.lo |= PKG_POWER_LIMIT_EN;
403 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
404 PKG_POWER_LIMIT_TIME_SHIFT;
405
406 /* Set short term power limit to 1.25 * TDP */
407 limit.hi = 0;
408 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
409 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700410 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500411
412 wrmsr(MSR_PKG_POWER_LIMIT, limit);
413
Duncan Lauriec70353f2013-06-28 14:40:38 -0700414 /* Set power limit values in MCHBAR as well */
415 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
416 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
417
418 /* Set DDR RAPL power limit by copying from MMIO to MSR */
419 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
420 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
421 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
422
Aaron Durbin76c37002012-10-30 09:03:43 -0500423 /* Use nominal TDP values for CPUs with configurable TDP */
424 if (cpu_config_tdp_levels()) {
425 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
426 limit.hi = 0;
427 limit.lo = msr.lo & 0xff;
428 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
429 }
430}
431
Aaron Durbin76c37002012-10-30 09:03:43 -0500432static void configure_c_states(void)
433{
434 msr_t msr;
435
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200436 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500437 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
438 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500439 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
440 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
441 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
442 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
443 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700444 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200445 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500446
Aaron Durbin76c37002012-10-30 09:03:43 -0500447 msr = rdmsr(MSR_MISC_PWR_MGMT);
448 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
449 wrmsr(MSR_MISC_PWR_MGMT, msr);
450
451 msr = rdmsr(MSR_POWER_CTL);
452 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
453 msr.lo |= (1 << 1); // C1E Enable
454 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
455 wrmsr(MSR_POWER_CTL, msr);
456
Aaron Durbin7c351312013-04-10 14:46:25 -0500457 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500458 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500459 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
460 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500461
Aaron Durbin7c351312013-04-10 14:46:25 -0500462 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500463 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500464 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
465 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500466
Aaron Durbin7c351312013-04-10 14:46:25 -0500467 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500468 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500469 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
470 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500471
Angel Pons4c95f102020-10-28 19:38:12 +0100472 /* Only Haswell ULT supports the 3-5 latency response registers */
473 if (!haswell_is_ult())
474 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500475
Angel Pons4c95f102020-10-28 19:38:12 +0100476 /* C-state Interrupt Response Latency Control 3 - package C8 */
477 msr.hi = 0;
478 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
479 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500480
Angel Pons4c95f102020-10-28 19:38:12 +0100481 /* C-state Interrupt Response Latency Control 4 - package C9 */
482 msr.hi = 0;
483 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
484 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
485
486 /* C-state Interrupt Response Latency Control 5 - package C10 */
487 msr.hi = 0;
488 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
489 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500490}
Aaron Durbin76c37002012-10-30 09:03:43 -0500491
492static void configure_thermal_target(void)
493{
494 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100495 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500496 msr_t msr;
497
498 /* Find pointer to CPU configuration */
499 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
500 if (!lapic || !lapic->chip_info)
501 return;
502 conf = lapic->chip_info;
503
Martin Roth4c3ab732013-07-08 16:23:54 -0600504 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500505 msr = rdmsr(MSR_PLATFORM_INFO);
506 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
507 msr = rdmsr(MSR_TEMPERATURE_TARGET);
508 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
509 msr.lo |= (conf->tcc_offset & 0xf) << 24;
510 wrmsr(MSR_TEMPERATURE_TARGET, msr);
511 }
512}
513
514static void configure_misc(void)
515{
516 msr_t msr;
517
518 msr = rdmsr(IA32_MISC_ENABLE);
519 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700520 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500521 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
522 wrmsr(IA32_MISC_ENABLE, msr);
523
524 /* Disable Thermal interrupts */
525 msr.lo = 0;
526 msr.hi = 0;
527 wrmsr(IA32_THERM_INTERRUPT, msr);
528
529 /* Enable package critical interrupt only */
530 msr.lo = 1 << 4;
531 msr.hi = 0;
532 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
533}
534
Aaron Durbin76c37002012-10-30 09:03:43 -0500535static void set_max_ratio(void)
536{
537 msr_t msr, perf_ctl;
538
539 perf_ctl.hi = 0;
540
541 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100542 if (get_turbo_state() == TURBO_ENABLED) {
543 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
544 perf_ctl.lo = (msr.lo & 0xff) << 8;
545 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500546 /* Set to nominal TDP ratio */
547 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
548 perf_ctl.lo = (msr.lo & 0xff) << 8;
549 } else {
550 /* Platform Info bits 15:8 give max ratio */
551 msr = rdmsr(MSR_PLATFORM_INFO);
552 perf_ctl.lo = msr.lo & 0xff00;
553 }
554 wrmsr(IA32_PERF_CTL, perf_ctl);
555
Angel Ponsf6cf49272020-09-25 01:14:24 +0200556 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100557 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500558}
559
Aaron Durbin76c37002012-10-30 09:03:43 -0500560static void configure_mca(void)
561{
562 msr_t msr;
563 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600564 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500565
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200566 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600567 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500568 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600569 /* TODO(adurbin): This should only be done on a cold boot. Also, some
570 * of these banks are core vs package scope. For now every CPU clears
571 * every bank. */
572 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500573 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
574}
575
Aaron Durbin305b1f02013-01-15 08:27:05 -0600576/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100577static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600578{
579 /* Clear out pending MCEs */
580 configure_mca();
581
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200582 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500583 enable_lapic_tpr();
584 setup_lapic();
585
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600586 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600587 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500588
Aaron Durbin76c37002012-10-30 09:03:43 -0500589 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500590 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500591
592 /* Configure Enhanced SpeedStep and Thermal Sensors */
593 configure_misc();
594
595 /* Thermal throttle activation offset */
596 configure_thermal_target();
597
598 /* Enable Direct Cache Access */
599 configure_dca_cap();
600
601 /* Set energy policy */
602 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
603
Aaron Durbin76c37002012-10-30 09:03:43 -0500604 /* Enable Turbo */
605 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600606}
Aaron Durbin76c37002012-10-30 09:03:43 -0500607
Aaron Durbin014baea2014-03-28 22:01:05 -0500608/* MP initialization support. */
609static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500610
Aaron Durbin463af332016-05-03 17:26:35 -0500611static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500612{
Aaron Durbin463af332016-05-03 17:26:35 -0500613 /* Setup MTRRs based on physical address size. */
614 x86_setup_mtrrs_with_detect();
615 x86_mtrr_check();
616
617 initialize_vr_config();
618
Angel Pons4c95f102020-10-28 19:38:12 +0100619 if (!haswell_is_ult())
620 return;
621
622 calibrate_24mhz_bclk();
623 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500624}
625
Aaron Durbin463af332016-05-03 17:26:35 -0500626static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500627{
Aaron Durbin463af332016-05-03 17:26:35 -0500628 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500629 int num_threads;
630 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500631
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200632 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500633 num_threads = (msr.lo >> 0) & 0xffff;
634 num_cores = (msr.lo >> 16) & 0xffff;
635 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
636 num_cores, num_threads);
637
Aaron Durbin463af332016-05-03 17:26:35 -0500638 return num_threads;
639}
Aaron Durbin7af20692013-01-14 14:54:41 -0600640
Aaron Durbin463af332016-05-03 17:26:35 -0500641static void get_microcode_info(const void **microcode, int *parallel)
642{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600643 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500644 *microcode = microcode_patch;
645 *parallel = 1;
646}
Aaron Durbin7af20692013-01-14 14:54:41 -0600647
Aaron Durbin463af332016-05-03 17:26:35 -0500648static void per_cpu_smm_trigger(void)
649{
650 /* Relocate the SMM handler. */
651 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600652
Aaron Durbin463af332016-05-03 17:26:35 -0500653 /* After SMM relocation a 2nd microcode load is required. */
654 intel_microcode_load_unlocked(microcode_patch);
655}
656
657static void post_mp_init(void)
658{
Angel Pons053deb82020-10-28 22:40:02 +0100659 /* Set Max Ratio */
660 set_max_ratio();
661
Aaron Durbin463af332016-05-03 17:26:35 -0500662 /* Now that all APs have been relocated as well as the BSP let SMIs
663 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300664 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500665
666 /* Lock down the SMRAM space. */
667 smm_lock();
668}
669
670static const struct mp_ops mp_ops = {
671 .pre_mp_init = pre_mp_init,
672 .get_cpu_count = get_cpu_count,
673 .get_smm_info = smm_info,
674 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500675 .pre_mp_smm_init = smm_initialize,
676 .per_cpu_smm_trigger = per_cpu_smm_trigger,
677 .relocation_handler = smm_relocation_handler,
678 .post_mp_init = post_mp_init,
679};
680
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300681void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500682{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700683 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500684 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500685}
686
687static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100688 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500689};
690
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100691static const struct cpu_device_id cpu_table[] = {
Angel Pons8b0636e2020-10-28 21:48:29 +0100692 { X86_VENDOR_INTEL, CPUID_HASWELL_A0 },
693 { X86_VENDOR_INTEL, CPUID_HASWELL_B0 },
694 { X86_VENDOR_INTEL, CPUID_HASWELL_C0 },
695 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_B0 },
696 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_C0 },
697 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_B0 },
698 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_C0 },
Aaron Durbin76c37002012-10-30 09:03:43 -0500699 { 0, 0 },
700};
701
702static const struct cpu_driver driver __cpu_driver = {
703 .ops = &cpu_dev_ops,
704 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500705 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500706};