blob: dd1970c0335a2e277c79e0d37e19a344a97d10e6 [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
23/*
Aaron Durbin7c351312013-04-10 14:46:25 -050024 * List of supported C-states in this processor. Only the ULT parts support C8,
25 * C9, and C10.
Aaron Durbin76c37002012-10-30 09:03:43 -050026 */
Aaron Durbin7c351312013-04-10 14:46:25 -050027enum {
28 C_STATE_C0, /* 0 */
29 C_STATE_C1, /* 1 */
30 C_STATE_C1E, /* 2 */
31 C_STATE_C3, /* 3 */
32 C_STATE_C6_SHORT_LAT, /* 4 */
33 C_STATE_C6_LONG_LAT, /* 5 */
34 C_STATE_C7_SHORT_LAT, /* 6 */
35 C_STATE_C7_LONG_LAT, /* 7 */
36 C_STATE_C7S_SHORT_LAT, /* 8 */
37 C_STATE_C7S_LONG_LAT, /* 9 */
38 C_STATE_C8, /* 10 */
39 C_STATE_C9, /* 11 */
40 C_STATE_C10, /* 12 */
41 NUM_C_STATES
Aaron Durbin76c37002012-10-30 09:03:43 -050042};
Aaron Durbin7c351312013-04-10 14:46:25 -050043
44#define MWAIT_RES(state, sub_state) \
45 { \
46 .addrl = (((state) << 4) | (sub_state)), \
47 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
48 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
49 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
50 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
51 }
52
53static acpi_cstate_t cstate_map[NUM_C_STATES] = {
54 [C_STATE_C0] = { },
55 [C_STATE_C1] = {
56 .latency = 0,
57 .power = 1000,
Lee Leahy9d62e7e2017-03-15 17:40:50 -070058 .resource = MWAIT_RES(0, 0),
Aaron Durbin7c351312013-04-10 14:46:25 -050059 },
60 [C_STATE_C1E] = {
61 .latency = 0,
62 .power = 1000,
Lee Leahy9d62e7e2017-03-15 17:40:50 -070063 .resource = MWAIT_RES(0, 1),
Aaron Durbin7c351312013-04-10 14:46:25 -050064 },
65 [C_STATE_C3] = {
66 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
67 .power = 900,
68 .resource = MWAIT_RES(1, 0),
69 },
70 [C_STATE_C6_SHORT_LAT] = {
71 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
72 .power = 800,
73 .resource = MWAIT_RES(2, 0),
74 },
75 [C_STATE_C6_LONG_LAT] = {
76 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
77 .power = 800,
78 .resource = MWAIT_RES(2, 1),
79 },
80 [C_STATE_C7_SHORT_LAT] = {
81 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
82 .power = 700,
83 .resource = MWAIT_RES(3, 0),
84 },
85 [C_STATE_C7_LONG_LAT] = {
86 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
87 .power = 700,
88 .resource = MWAIT_RES(3, 1),
89 },
90 [C_STATE_C7S_SHORT_LAT] = {
91 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
92 .power = 700,
93 .resource = MWAIT_RES(3, 2),
94 },
95 [C_STATE_C7S_LONG_LAT] = {
96 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
97 .power = 700,
98 .resource = MWAIT_RES(3, 3),
99 },
100 [C_STATE_C8] = {
101 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
102 .power = 600,
103 .resource = MWAIT_RES(4, 0),
104 },
105 [C_STATE_C9] = {
106 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
107 .power = 500,
108 .resource = MWAIT_RES(5, 0),
109 },
110 [C_STATE_C10] = {
111 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
112 .power = 400,
113 .resource = MWAIT_RES(6, 0),
114 },
115};
Aaron Durbin76c37002012-10-30 09:03:43 -0500116
117/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
118static const u8 power_limit_time_sec_to_msr[] = {
119 [0] = 0x00,
120 [1] = 0x0a,
121 [2] = 0x0b,
122 [3] = 0x4b,
123 [4] = 0x0c,
124 [5] = 0x2c,
125 [6] = 0x4c,
126 [7] = 0x6c,
127 [8] = 0x0d,
128 [10] = 0x2d,
129 [12] = 0x4d,
130 [14] = 0x6d,
131 [16] = 0x0e,
132 [20] = 0x2e,
133 [24] = 0x4e,
134 [28] = 0x6e,
135 [32] = 0x0f,
136 [40] = 0x2f,
137 [48] = 0x4f,
138 [56] = 0x6f,
139 [64] = 0x10,
140 [80] = 0x30,
141 [96] = 0x50,
142 [112] = 0x70,
143 [128] = 0x11,
144};
145
146/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
147static const u8 power_limit_time_msr_to_sec[] = {
148 [0x00] = 0,
149 [0x0a] = 1,
150 [0x0b] = 2,
151 [0x4b] = 3,
152 [0x0c] = 4,
153 [0x2c] = 5,
154 [0x4c] = 6,
155 [0x6c] = 7,
156 [0x0d] = 8,
157 [0x2d] = 10,
158 [0x4d] = 12,
159 [0x6d] = 14,
160 [0x0e] = 16,
161 [0x2e] = 20,
162 [0x4e] = 24,
163 [0x6e] = 28,
164 [0x0f] = 32,
165 [0x2f] = 40,
166 [0x4f] = 48,
167 [0x6f] = 56,
168 [0x10] = 64,
169 [0x30] = 80,
170 [0x50] = 96,
171 [0x70] = 112,
172 [0x11] = 128,
173};
174
Angel Pons5d92aa52020-10-14 00:02:37 +0200175/* The core 100MHz BCLK is disabled in deeper c-states. One needs to calibrate
176 * the 100MHz BCLK against the 24MHz BCLK to restore the clocks properly
Aaron Durbinf24262d2013-04-10 14:59:21 -0500177 * when a core is woken up. */
178static int pcode_ready(void)
179{
180 int wait_count;
181 const int delay_step = 10;
182
183 wait_count = 0;
184 do {
185 if (!(MCHBAR32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
186 return 0;
187 wait_count += delay_step;
188 udelay(delay_step);
189 } while (wait_count < 1000);
190
191 return -1;
192}
193
194static void calibrate_24mhz_bclk(void)
195{
196 int err_code;
197
198 if (pcode_ready() < 0) {
199 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
200 return;
201 }
202
203 /* A non-zero value initiates the PCODE calibration. */
204 MCHBAR32(BIOS_MAILBOX_DATA) = ~0;
205 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
206 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL;
207
208 if (pcode_ready() < 0) {
209 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
210 return;
211 }
212
213 err_code = MCHBAR32(BIOS_MAILBOX_INTERFACE) & 0xff;
214
Angel Pons5d92aa52020-10-14 00:02:37 +0200215 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration response: %d\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500216 err_code);
217
218 /* Read the calibrated value. */
219 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
220 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION;
221
222 if (pcode_ready() < 0) {
223 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
224 return;
225 }
226
Angel Pons5d92aa52020-10-14 00:02:37 +0200227 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration value: 0x%08x\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500228 MCHBAR32(BIOS_MAILBOX_DATA));
229}
230
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700231static u32 pcode_mailbox_read(u32 command)
232{
233 if (pcode_ready() < 0) {
234 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
235 return 0;
236 }
237
238 /* Send command and start transaction */
239 MCHBAR32(BIOS_MAILBOX_INTERFACE) = command | MAILBOX_RUN_BUSY;
240
241 if (pcode_ready() < 0) {
242 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
243 return 0;
244 }
245
246 /* Read mailbox */
247 return MCHBAR32(BIOS_MAILBOX_DATA);
248}
249
Aaron Durbin16cbf892013-07-03 16:21:28 -0500250static void initialize_vr_config(void)
251{
Angel Pons242fd282020-10-28 23:48:56 +0100252 struct cpu_vr_config vr_config = { 0 };
Aaron Durbin16cbf892013-07-03 16:21:28 -0500253 msr_t msr;
254
Angel Pons242fd282020-10-28 23:48:56 +0100255 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
256
257 if (lapic && lapic->chip_info) {
258 const struct cpu_intel_haswell_config *conf = lapic->chip_info;
259
260 vr_config = conf->vr_config;
261 }
262
Aaron Durbin16cbf892013-07-03 16:21:28 -0500263 printk(BIOS_DEBUG, "Initializing VR config.\n");
264
265 /* Configure VR_CURRENT_CONFIG. */
266 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
267 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
268 * on ULT systems. */
269 msr.hi &= 0xc0000000;
270 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
271 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
Angel Pons9dcd1c12020-10-28 22:41:26 +0100272 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A. */
Aaron Durbin16cbf892013-07-03 16:21:28 -0500273
Duncan Laurie118d1052013-07-09 15:34:25 -0700274 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500275 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
276 /* Leave the max instantaneous current limit (12:0) to default. */
277 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
278
279 /* Configure VR_MISC_CONFIG MSR. */
280 msr = rdmsr(MSR_VR_MISC_CONFIG);
281 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
282 msr.hi &= ~(0x3ff << (40 - 32));
283 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
284 /* Set IOUT_OFFSET to 0. */
285 msr.hi &= ~0xff;
286 /* Set exit ramp rate to fast. */
287 msr.hi |= (1 << (50 - 32));
288 /* Set entry ramp rate to slow. */
289 msr.hi &= ~(1 << (51 - 32));
290 /* Enable decay mode on C-state entry. */
291 msr.hi |= (1 << (52 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100292 /* Set the slow ramp rate */
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300293 if (haswell_is_ult()) {
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300294 msr.hi &= ~(0x3 << (53 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100295 /* Configure the C-state exit ramp rate. */
296 if (vr_config.slow_ramp_rate_enable) {
297 /* Configured slow ramp rate. */
298 msr.hi |= ((vr_config.slow_ramp_rate_set & 0x3) << (53 - 32));
299 /* Set exit ramp rate to slow. */
300 msr.hi &= ~(1 << (50 - 32));
301 } else {
302 /* Fast ramp rate / 4. */
303 msr.hi |= (1 << (53 - 32));
304 }
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300305 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500306 /* Set MIN_VID (31:24) to allow CPU to have full control. */
307 msr.lo &= ~0xff000000;
Angel Pons242fd282020-10-28 23:48:56 +0100308 msr.lo |= (vr_config.cpu_min_vid & 0xff) << 24;
Aaron Durbin16cbf892013-07-03 16:21:28 -0500309 wrmsr(MSR_VR_MISC_CONFIG, msr);
310
311 /* Configure VR_MISC_CONFIG2 MSR. */
Angel Pons4c95f102020-10-28 19:38:12 +0100312 if (!haswell_is_ult())
313 return;
314
315 msr = rdmsr(MSR_VR_MISC_CONFIG2);
316 msr.lo &= ~0xffff;
317 /* Allow CPU to control minimum voltage completely (15:8) and
318 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
319 msr.lo |= 0x006f;
320 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500321}
322
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700323static void configure_pch_power_sharing(void)
324{
325 u32 pch_power, pch_power_ext, pmsync, pmsync2;
326 int i;
327
328 /* Read PCH Power levels from PCODE */
329 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
330 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
331
332 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700333 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700334
335 pmsync = RCBA32(PMSYNC_CONFIG);
336 pmsync2 = RCBA32(PMSYNC_CONFIG2);
337
338 /* Program PMSYNC_TPR_CONFIG PCH power limit values
339 * pmsync[0:4] = mailbox[0:5]
340 * pmsync[8:12] = mailbox[6:11]
341 * pmsync[16:20] = mailbox[12:17]
342 */
343 for (i = 0; i < 3; i++) {
344 u32 level = pch_power & 0x3f;
345 pch_power >>= 6;
346 pmsync &= ~(0x1f << (i * 8));
347 pmsync |= (level & 0x1f) << (i * 8);
348 }
349 RCBA32(PMSYNC_CONFIG) = pmsync;
350
351 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
352 * pmsync2[0:4] = mailbox[23:18]
353 * pmsync2[8:12] = mailbox_ext[6:11]
354 * pmsync2[16:20] = mailbox_ext[12:17]
355 * pmsync2[24:28] = mailbox_ext[18:22]
356 */
357 pmsync2 &= ~0x1f;
358 pmsync2 |= pch_power & 0x1f;
359
360 for (i = 1; i < 4; i++) {
361 u32 level = pch_power_ext & 0x3f;
362 pch_power_ext >>= 6;
363 pmsync2 &= ~(0x1f << (i * 8));
364 pmsync2 |= (level & 0x1f) << (i * 8);
365 }
366 RCBA32(PMSYNC_CONFIG2) = pmsync2;
367}
368
Aaron Durbin76c37002012-10-30 09:03:43 -0500369int cpu_config_tdp_levels(void)
370{
371 msr_t platform_info;
372
373 /* Bits 34:33 indicate how many levels supported */
374 platform_info = rdmsr(MSR_PLATFORM_INFO);
375 return (platform_info.hi >> 1) & 3;
376}
377
378/*
379 * Configure processor power limits if possible
380 * This must be done AFTER set of BIOS_RESET_CPL
381 */
382void set_power_limits(u8 power_limit_1_time)
383{
384 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
385 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700386 unsigned int power_unit;
387 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500388 u8 power_limit_1_val;
389
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000390 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100391 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500392
393 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
394 return;
395
396 /* Get units */
397 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
398 power_unit = 2 << ((msr.lo & 0xf) - 1);
399
400 /* Get power defaults for this SKU */
401 msr = rdmsr(MSR_PKG_POWER_SKU);
402 tdp = msr.lo & 0x7fff;
403 min_power = (msr.lo >> 16) & 0x7fff;
404 max_power = msr.hi & 0x7fff;
405 max_time = (msr.hi >> 16) & 0x7f;
406
407 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
408
409 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
410 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
411
412 if (min_power > 0 && tdp < min_power)
413 tdp = min_power;
414
415 if (max_power > 0 && tdp > max_power)
416 tdp = max_power;
417
418 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
419
420 /* Set long term power limit to TDP */
421 limit.lo = 0;
422 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
423 limit.lo |= PKG_POWER_LIMIT_EN;
424 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
425 PKG_POWER_LIMIT_TIME_SHIFT;
426
427 /* Set short term power limit to 1.25 * TDP */
428 limit.hi = 0;
429 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
430 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700431 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500432
433 wrmsr(MSR_PKG_POWER_LIMIT, limit);
434
Duncan Lauriec70353f2013-06-28 14:40:38 -0700435 /* Set power limit values in MCHBAR as well */
436 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
437 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
438
439 /* Set DDR RAPL power limit by copying from MMIO to MSR */
440 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
441 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
442 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
443
Aaron Durbin76c37002012-10-30 09:03:43 -0500444 /* Use nominal TDP values for CPUs with configurable TDP */
445 if (cpu_config_tdp_levels()) {
446 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
447 limit.hi = 0;
448 limit.lo = msr.lo & 0xff;
449 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
450 }
451}
452
Aaron Durbin76c37002012-10-30 09:03:43 -0500453static void configure_c_states(void)
454{
455 msr_t msr;
456
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200457 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500458 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
459 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500460 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
461 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
462 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
463 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
464 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700465 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200466 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500467
Aaron Durbin76c37002012-10-30 09:03:43 -0500468 msr = rdmsr(MSR_MISC_PWR_MGMT);
469 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
470 wrmsr(MSR_MISC_PWR_MGMT, msr);
471
472 msr = rdmsr(MSR_POWER_CTL);
473 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
474 msr.lo |= (1 << 1); // C1E Enable
475 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
476 wrmsr(MSR_POWER_CTL, msr);
477
Aaron Durbin7c351312013-04-10 14:46:25 -0500478 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500479 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500480 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
481 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500482
Aaron Durbin7c351312013-04-10 14:46:25 -0500483 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500484 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500485 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
486 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500487
Aaron Durbin7c351312013-04-10 14:46:25 -0500488 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500489 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500490 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
491 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500492
Angel Pons4c95f102020-10-28 19:38:12 +0100493 /* Only Haswell ULT supports the 3-5 latency response registers */
494 if (!haswell_is_ult())
495 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500496
Angel Pons4c95f102020-10-28 19:38:12 +0100497 /* C-state Interrupt Response Latency Control 3 - package C8 */
498 msr.hi = 0;
499 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
500 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500501
Angel Pons4c95f102020-10-28 19:38:12 +0100502 /* C-state Interrupt Response Latency Control 4 - package C9 */
503 msr.hi = 0;
504 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
505 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
506
507 /* C-state Interrupt Response Latency Control 5 - package C10 */
508 msr.hi = 0;
509 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
510 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500511}
Aaron Durbin76c37002012-10-30 09:03:43 -0500512
513static void configure_thermal_target(void)
514{
515 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100516 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500517 msr_t msr;
518
519 /* Find pointer to CPU configuration */
520 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
521 if (!lapic || !lapic->chip_info)
522 return;
523 conf = lapic->chip_info;
524
Martin Roth4c3ab732013-07-08 16:23:54 -0600525 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500526 msr = rdmsr(MSR_PLATFORM_INFO);
527 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
528 msr = rdmsr(MSR_TEMPERATURE_TARGET);
529 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
530 msr.lo |= (conf->tcc_offset & 0xf) << 24;
531 wrmsr(MSR_TEMPERATURE_TARGET, msr);
532 }
533}
534
535static void configure_misc(void)
536{
537 msr_t msr;
538
539 msr = rdmsr(IA32_MISC_ENABLE);
540 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700541 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500542 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
543 wrmsr(IA32_MISC_ENABLE, msr);
544
545 /* Disable Thermal interrupts */
546 msr.lo = 0;
547 msr.hi = 0;
548 wrmsr(IA32_THERM_INTERRUPT, msr);
549
550 /* Enable package critical interrupt only */
551 msr.lo = 1 << 4;
552 msr.hi = 0;
553 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
554}
555
Aaron Durbin76c37002012-10-30 09:03:43 -0500556static void set_max_ratio(void)
557{
558 msr_t msr, perf_ctl;
559
560 perf_ctl.hi = 0;
561
562 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100563 if (get_turbo_state() == TURBO_ENABLED) {
564 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
565 perf_ctl.lo = (msr.lo & 0xff) << 8;
566 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500567 /* Set to nominal TDP ratio */
568 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
569 perf_ctl.lo = (msr.lo & 0xff) << 8;
570 } else {
571 /* Platform Info bits 15:8 give max ratio */
572 msr = rdmsr(MSR_PLATFORM_INFO);
573 perf_ctl.lo = msr.lo & 0xff00;
574 }
575 wrmsr(IA32_PERF_CTL, perf_ctl);
576
Angel Ponsf6cf49272020-09-25 01:14:24 +0200577 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100578 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500579}
580
Aaron Durbin76c37002012-10-30 09:03:43 -0500581static void configure_mca(void)
582{
583 msr_t msr;
584 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600585 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500586
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200587 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600588 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500589 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600590 /* TODO(adurbin): This should only be done on a cold boot. Also, some
591 * of these banks are core vs package scope. For now every CPU clears
592 * every bank. */
593 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500594 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
595}
596
Aaron Durbin305b1f02013-01-15 08:27:05 -0600597/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100598static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600599{
600 /* Clear out pending MCEs */
601 configure_mca();
602
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200603 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500604 enable_lapic_tpr();
605 setup_lapic();
606
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600607 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600608 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500609
Aaron Durbin76c37002012-10-30 09:03:43 -0500610 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500611 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500612
613 /* Configure Enhanced SpeedStep and Thermal Sensors */
614 configure_misc();
615
616 /* Thermal throttle activation offset */
617 configure_thermal_target();
618
619 /* Enable Direct Cache Access */
620 configure_dca_cap();
621
622 /* Set energy policy */
623 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
624
Aaron Durbin76c37002012-10-30 09:03:43 -0500625 /* Enable Turbo */
626 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600627}
Aaron Durbin76c37002012-10-30 09:03:43 -0500628
Aaron Durbin014baea2014-03-28 22:01:05 -0500629/* MP initialization support. */
630static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500631
Aaron Durbin463af332016-05-03 17:26:35 -0500632static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500633{
Aaron Durbin463af332016-05-03 17:26:35 -0500634 /* Setup MTRRs based on physical address size. */
635 x86_setup_mtrrs_with_detect();
636 x86_mtrr_check();
637
638 initialize_vr_config();
639
Angel Pons4c95f102020-10-28 19:38:12 +0100640 if (!haswell_is_ult())
641 return;
642
643 calibrate_24mhz_bclk();
644 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500645}
646
Aaron Durbin463af332016-05-03 17:26:35 -0500647static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500648{
Aaron Durbin463af332016-05-03 17:26:35 -0500649 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500650 int num_threads;
651 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500652
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200653 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500654 num_threads = (msr.lo >> 0) & 0xffff;
655 num_cores = (msr.lo >> 16) & 0xffff;
656 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
657 num_cores, num_threads);
658
Aaron Durbin463af332016-05-03 17:26:35 -0500659 return num_threads;
660}
Aaron Durbin7af20692013-01-14 14:54:41 -0600661
Aaron Durbin463af332016-05-03 17:26:35 -0500662static void get_microcode_info(const void **microcode, int *parallel)
663{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600664 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500665 *microcode = microcode_patch;
666 *parallel = 1;
667}
Aaron Durbin7af20692013-01-14 14:54:41 -0600668
Aaron Durbin463af332016-05-03 17:26:35 -0500669static void per_cpu_smm_trigger(void)
670{
671 /* Relocate the SMM handler. */
672 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600673
Aaron Durbin463af332016-05-03 17:26:35 -0500674 /* After SMM relocation a 2nd microcode load is required. */
675 intel_microcode_load_unlocked(microcode_patch);
676}
677
678static void post_mp_init(void)
679{
Angel Pons053deb82020-10-28 22:40:02 +0100680 /* Set Max Ratio */
681 set_max_ratio();
682
Aaron Durbin463af332016-05-03 17:26:35 -0500683 /* Now that all APs have been relocated as well as the BSP let SMIs
684 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300685 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500686
687 /* Lock down the SMRAM space. */
688 smm_lock();
689}
690
691static const struct mp_ops mp_ops = {
692 .pre_mp_init = pre_mp_init,
693 .get_cpu_count = get_cpu_count,
694 .get_smm_info = smm_info,
695 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500696 .pre_mp_smm_init = smm_initialize,
697 .per_cpu_smm_trigger = per_cpu_smm_trigger,
698 .relocation_handler = smm_relocation_handler,
699 .post_mp_init = post_mp_init,
700};
701
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300702void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500703{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700704 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500705 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500706}
707
708static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100709 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500710};
711
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100712static const struct cpu_device_id cpu_table[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -0500713 { X86_VENDOR_INTEL, 0x306c1 }, /* Intel Haswell 4+2 A0 */
714 { X86_VENDOR_INTEL, 0x306c2 }, /* Intel Haswell 4+2 B0 */
Tristan Corrick22f97002018-10-31 02:22:39 +1300715 { X86_VENDOR_INTEL, 0x306c3 }, /* Intel Haswell C0 */
Duncan Laurie512540492012-12-17 11:24:45 -0800716 { X86_VENDOR_INTEL, 0x40650 }, /* Intel Haswell ULT B0 */
717 { X86_VENDOR_INTEL, 0x40651 }, /* Intel Haswell ULT B1 */
Iru Cai27126f12020-07-30 23:04:03 +0800718 { X86_VENDOR_INTEL, 0x40660 }, /* Intel Crystal Well C0 */
719 { X86_VENDOR_INTEL, 0x40661 }, /* Intel Crystal Well C1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500720 { 0, 0 },
721};
722
723static const struct cpu_driver driver __cpu_driver = {
724 .ops = &cpu_dev_ops,
725 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500726 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500727};