blob: 920a7e7bb3acad7b09963c8c1cd2dbc6c385ad4c [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{
252 msr_t msr;
253
254 printk(BIOS_DEBUG, "Initializing VR config.\n");
255
256 /* Configure VR_CURRENT_CONFIG. */
257 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
258 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
259 * on ULT systems. */
260 msr.hi &= 0xc0000000;
261 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
262 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
263 msr.hi |= (0x0f << (32 - 32)); /* PSI1 threshold - 15A. */
264
Duncan Laurie118d1052013-07-09 15:34:25 -0700265 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500266 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
267 /* Leave the max instantaneous current limit (12:0) to default. */
268 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
269
270 /* Configure VR_MISC_CONFIG MSR. */
271 msr = rdmsr(MSR_VR_MISC_CONFIG);
272 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
273 msr.hi &= ~(0x3ff << (40 - 32));
274 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
275 /* Set IOUT_OFFSET to 0. */
276 msr.hi &= ~0xff;
277 /* Set exit ramp rate to fast. */
278 msr.hi |= (1 << (50 - 32));
279 /* Set entry ramp rate to slow. */
280 msr.hi &= ~(1 << (51 - 32));
281 /* Enable decay mode on C-state entry. */
282 msr.hi |= (1 << (52 - 32));
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300283 if (haswell_is_ult()) {
284 /* Set the slow ramp rate to be fast ramp rate / 4 */
285 msr.hi &= ~(0x3 << (53 - 32));
286 msr.hi |= (0x01 << (53 - 32));
287 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500288 /* Set MIN_VID (31:24) to allow CPU to have full control. */
289 msr.lo &= ~0xff000000;
290 wrmsr(MSR_VR_MISC_CONFIG, msr);
291
292 /* Configure VR_MISC_CONFIG2 MSR. */
Angel Pons4c95f102020-10-28 19:38:12 +0100293 if (!haswell_is_ult())
294 return;
295
296 msr = rdmsr(MSR_VR_MISC_CONFIG2);
297 msr.lo &= ~0xffff;
298 /* Allow CPU to control minimum voltage completely (15:8) and
299 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
300 msr.lo |= 0x006f;
301 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500302}
303
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700304static void configure_pch_power_sharing(void)
305{
306 u32 pch_power, pch_power_ext, pmsync, pmsync2;
307 int i;
308
309 /* Read PCH Power levels from PCODE */
310 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
311 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
312
313 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700314 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700315
316 pmsync = RCBA32(PMSYNC_CONFIG);
317 pmsync2 = RCBA32(PMSYNC_CONFIG2);
318
319 /* Program PMSYNC_TPR_CONFIG PCH power limit values
320 * pmsync[0:4] = mailbox[0:5]
321 * pmsync[8:12] = mailbox[6:11]
322 * pmsync[16:20] = mailbox[12:17]
323 */
324 for (i = 0; i < 3; i++) {
325 u32 level = pch_power & 0x3f;
326 pch_power >>= 6;
327 pmsync &= ~(0x1f << (i * 8));
328 pmsync |= (level & 0x1f) << (i * 8);
329 }
330 RCBA32(PMSYNC_CONFIG) = pmsync;
331
332 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
333 * pmsync2[0:4] = mailbox[23:18]
334 * pmsync2[8:12] = mailbox_ext[6:11]
335 * pmsync2[16:20] = mailbox_ext[12:17]
336 * pmsync2[24:28] = mailbox_ext[18:22]
337 */
338 pmsync2 &= ~0x1f;
339 pmsync2 |= pch_power & 0x1f;
340
341 for (i = 1; i < 4; i++) {
342 u32 level = pch_power_ext & 0x3f;
343 pch_power_ext >>= 6;
344 pmsync2 &= ~(0x1f << (i * 8));
345 pmsync2 |= (level & 0x1f) << (i * 8);
346 }
347 RCBA32(PMSYNC_CONFIG2) = pmsync2;
348}
349
Aaron Durbin76c37002012-10-30 09:03:43 -0500350int cpu_config_tdp_levels(void)
351{
352 msr_t platform_info;
353
354 /* Bits 34:33 indicate how many levels supported */
355 platform_info = rdmsr(MSR_PLATFORM_INFO);
356 return (platform_info.hi >> 1) & 3;
357}
358
359/*
360 * Configure processor power limits if possible
361 * This must be done AFTER set of BIOS_RESET_CPL
362 */
363void set_power_limits(u8 power_limit_1_time)
364{
365 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
366 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700367 unsigned int power_unit;
368 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500369 u8 power_limit_1_val;
370
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000371 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100372 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500373
374 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
375 return;
376
377 /* Get units */
378 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
379 power_unit = 2 << ((msr.lo & 0xf) - 1);
380
381 /* Get power defaults for this SKU */
382 msr = rdmsr(MSR_PKG_POWER_SKU);
383 tdp = msr.lo & 0x7fff;
384 min_power = (msr.lo >> 16) & 0x7fff;
385 max_power = msr.hi & 0x7fff;
386 max_time = (msr.hi >> 16) & 0x7f;
387
388 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
389
390 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
391 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
392
393 if (min_power > 0 && tdp < min_power)
394 tdp = min_power;
395
396 if (max_power > 0 && tdp > max_power)
397 tdp = max_power;
398
399 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
400
401 /* Set long term power limit to TDP */
402 limit.lo = 0;
403 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
404 limit.lo |= PKG_POWER_LIMIT_EN;
405 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
406 PKG_POWER_LIMIT_TIME_SHIFT;
407
408 /* Set short term power limit to 1.25 * TDP */
409 limit.hi = 0;
410 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
411 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700412 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500413
414 wrmsr(MSR_PKG_POWER_LIMIT, limit);
415
Duncan Lauriec70353f2013-06-28 14:40:38 -0700416 /* Set power limit values in MCHBAR as well */
417 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
418 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
419
420 /* Set DDR RAPL power limit by copying from MMIO to MSR */
421 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
422 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
423 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
424
Aaron Durbin76c37002012-10-30 09:03:43 -0500425 /* Use nominal TDP values for CPUs with configurable TDP */
426 if (cpu_config_tdp_levels()) {
427 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
428 limit.hi = 0;
429 limit.lo = msr.lo & 0xff;
430 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
431 }
432}
433
Aaron Durbin76c37002012-10-30 09:03:43 -0500434static void configure_c_states(void)
435{
436 msr_t msr;
437
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200438 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500439 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
440 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500441 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
442 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
443 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
444 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
445 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700446 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200447 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500448
Aaron Durbin76c37002012-10-30 09:03:43 -0500449 msr = rdmsr(MSR_MISC_PWR_MGMT);
450 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
451 wrmsr(MSR_MISC_PWR_MGMT, msr);
452
453 msr = rdmsr(MSR_POWER_CTL);
454 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
455 msr.lo |= (1 << 1); // C1E Enable
456 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
457 wrmsr(MSR_POWER_CTL, msr);
458
Aaron Durbin7c351312013-04-10 14:46:25 -0500459 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500460 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500461 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
462 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500463
Aaron Durbin7c351312013-04-10 14:46:25 -0500464 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500465 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500466 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
467 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500468
Aaron Durbin7c351312013-04-10 14:46:25 -0500469 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500470 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500471 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
472 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500473
Angel Pons4c95f102020-10-28 19:38:12 +0100474 /* Only Haswell ULT supports the 3-5 latency response registers */
475 if (!haswell_is_ult())
476 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500477
Angel Pons4c95f102020-10-28 19:38:12 +0100478 /* C-state Interrupt Response Latency Control 3 - package C8 */
479 msr.hi = 0;
480 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
481 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500482
Angel Pons4c95f102020-10-28 19:38:12 +0100483 /* C-state Interrupt Response Latency Control 4 - package C9 */
484 msr.hi = 0;
485 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
486 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
487
488 /* C-state Interrupt Response Latency Control 5 - package C10 */
489 msr.hi = 0;
490 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
491 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500492}
Aaron Durbin76c37002012-10-30 09:03:43 -0500493
494static void configure_thermal_target(void)
495{
496 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100497 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500498 msr_t msr;
499
500 /* Find pointer to CPU configuration */
501 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
502 if (!lapic || !lapic->chip_info)
503 return;
504 conf = lapic->chip_info;
505
Martin Roth4c3ab732013-07-08 16:23:54 -0600506 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500507 msr = rdmsr(MSR_PLATFORM_INFO);
508 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
509 msr = rdmsr(MSR_TEMPERATURE_TARGET);
510 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
511 msr.lo |= (conf->tcc_offset & 0xf) << 24;
512 wrmsr(MSR_TEMPERATURE_TARGET, msr);
513 }
514}
515
516static void configure_misc(void)
517{
518 msr_t msr;
519
520 msr = rdmsr(IA32_MISC_ENABLE);
521 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700522 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500523 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
524 wrmsr(IA32_MISC_ENABLE, msr);
525
526 /* Disable Thermal interrupts */
527 msr.lo = 0;
528 msr.hi = 0;
529 wrmsr(IA32_THERM_INTERRUPT, msr);
530
531 /* Enable package critical interrupt only */
532 msr.lo = 1 << 4;
533 msr.hi = 0;
534 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
535}
536
Aaron Durbin76c37002012-10-30 09:03:43 -0500537static void set_max_ratio(void)
538{
539 msr_t msr, perf_ctl;
540
541 perf_ctl.hi = 0;
542
543 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100544 if (get_turbo_state() == TURBO_ENABLED) {
545 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
546 perf_ctl.lo = (msr.lo & 0xff) << 8;
547 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500548 /* Set to nominal TDP ratio */
549 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
550 perf_ctl.lo = (msr.lo & 0xff) << 8;
551 } else {
552 /* Platform Info bits 15:8 give max ratio */
553 msr = rdmsr(MSR_PLATFORM_INFO);
554 perf_ctl.lo = msr.lo & 0xff00;
555 }
556 wrmsr(IA32_PERF_CTL, perf_ctl);
557
Angel Ponsf6cf49272020-09-25 01:14:24 +0200558 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100559 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500560}
561
Aaron Durbin76c37002012-10-30 09:03:43 -0500562static void configure_mca(void)
563{
564 msr_t msr;
565 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600566 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500567
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200568 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600569 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500570 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600571 /* TODO(adurbin): This should only be done on a cold boot. Also, some
572 * of these banks are core vs package scope. For now every CPU clears
573 * every bank. */
574 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500575 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
576}
577
Aaron Durbin305b1f02013-01-15 08:27:05 -0600578/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100579static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600580{
581 /* Clear out pending MCEs */
582 configure_mca();
583
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200584 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500585 enable_lapic_tpr();
586 setup_lapic();
587
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600588 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600589 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500590
Aaron Durbin76c37002012-10-30 09:03:43 -0500591 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500592 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500593
594 /* Configure Enhanced SpeedStep and Thermal Sensors */
595 configure_misc();
596
597 /* Thermal throttle activation offset */
598 configure_thermal_target();
599
600 /* Enable Direct Cache Access */
601 configure_dca_cap();
602
603 /* Set energy policy */
604 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
605
Aaron Durbin76c37002012-10-30 09:03:43 -0500606 /* Enable Turbo */
607 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600608}
Aaron Durbin76c37002012-10-30 09:03:43 -0500609
Aaron Durbin014baea2014-03-28 22:01:05 -0500610/* MP initialization support. */
611static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500612
Aaron Durbin463af332016-05-03 17:26:35 -0500613static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500614{
Aaron Durbin463af332016-05-03 17:26:35 -0500615 /* Setup MTRRs based on physical address size. */
616 x86_setup_mtrrs_with_detect();
617 x86_mtrr_check();
618
619 initialize_vr_config();
620
Angel Pons4c95f102020-10-28 19:38:12 +0100621 if (!haswell_is_ult())
622 return;
623
624 calibrate_24mhz_bclk();
625 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500626}
627
Aaron Durbin463af332016-05-03 17:26:35 -0500628static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500629{
Aaron Durbin463af332016-05-03 17:26:35 -0500630 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500631 int num_threads;
632 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500633
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200634 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500635 num_threads = (msr.lo >> 0) & 0xffff;
636 num_cores = (msr.lo >> 16) & 0xffff;
637 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
638 num_cores, num_threads);
639
Aaron Durbin463af332016-05-03 17:26:35 -0500640 return num_threads;
641}
Aaron Durbin7af20692013-01-14 14:54:41 -0600642
Aaron Durbin463af332016-05-03 17:26:35 -0500643static void get_microcode_info(const void **microcode, int *parallel)
644{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600645 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500646 *microcode = microcode_patch;
647 *parallel = 1;
648}
Aaron Durbin7af20692013-01-14 14:54:41 -0600649
Aaron Durbin463af332016-05-03 17:26:35 -0500650static void per_cpu_smm_trigger(void)
651{
652 /* Relocate the SMM handler. */
653 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600654
Aaron Durbin463af332016-05-03 17:26:35 -0500655 /* After SMM relocation a 2nd microcode load is required. */
656 intel_microcode_load_unlocked(microcode_patch);
657}
658
659static void post_mp_init(void)
660{
Angel Pons053deb82020-10-28 22:40:02 +0100661 /* Set Max Ratio */
662 set_max_ratio();
663
Aaron Durbin463af332016-05-03 17:26:35 -0500664 /* Now that all APs have been relocated as well as the BSP let SMIs
665 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300666 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500667
668 /* Lock down the SMRAM space. */
669 smm_lock();
670}
671
672static const struct mp_ops mp_ops = {
673 .pre_mp_init = pre_mp_init,
674 .get_cpu_count = get_cpu_count,
675 .get_smm_info = smm_info,
676 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500677 .pre_mp_smm_init = smm_initialize,
678 .per_cpu_smm_trigger = per_cpu_smm_trigger,
679 .relocation_handler = smm_relocation_handler,
680 .post_mp_init = post_mp_init,
681};
682
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300683void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500684{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700685 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500686 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500687}
688
689static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100690 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500691};
692
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100693static const struct cpu_device_id cpu_table[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -0500694 { X86_VENDOR_INTEL, 0x306c1 }, /* Intel Haswell 4+2 A0 */
695 { X86_VENDOR_INTEL, 0x306c2 }, /* Intel Haswell 4+2 B0 */
Tristan Corrick22f97002018-10-31 02:22:39 +1300696 { X86_VENDOR_INTEL, 0x306c3 }, /* Intel Haswell C0 */
Duncan Laurie512540492012-12-17 11:24:45 -0800697 { X86_VENDOR_INTEL, 0x40650 }, /* Intel Haswell ULT B0 */
698 { X86_VENDOR_INTEL, 0x40651 }, /* Intel Haswell ULT B1 */
Iru Cai27126f12020-07-30 23:04:03 +0800699 { X86_VENDOR_INTEL, 0x40660 }, /* Intel Crystal Well C0 */
700 { X86_VENDOR_INTEL, 0x40661 }, /* Intel Crystal Well C1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500701 { 0, 0 },
702};
703
704static const struct cpu_driver driver __cpu_driver = {
705 .ops = &cpu_dev_ops,
706 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500707 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500708};