blob: 9881bb880fcbb2b0953b17cbc7a8d59715faa4c8 [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{
Angel Ponsc89d2a282020-10-28 22:23:02 +0100434 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
435
436 const bool timed_mwait_capable = !!(msr.hi & TIMED_MWAIT_SUPPORTED);
Aaron Durbin76c37002012-10-30 09:03:43 -0500437
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
Angel Ponsc89d2a282020-10-28 22:23:02 +0100446
447 if (timed_mwait_capable)
448 msr.lo |= (1 << 31); // Timed MWAIT Enable
449
Duncan Laurie1c097102013-05-07 13:19:56 -0700450 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200451 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500452
Aaron Durbin76c37002012-10-30 09:03:43 -0500453 msr = rdmsr(MSR_MISC_PWR_MGMT);
454 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
455 wrmsr(MSR_MISC_PWR_MGMT, msr);
456
457 msr = rdmsr(MSR_POWER_CTL);
458 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
459 msr.lo |= (1 << 1); // C1E Enable
460 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
461 wrmsr(MSR_POWER_CTL, msr);
462
Aaron Durbin7c351312013-04-10 14:46:25 -0500463 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500464 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500465 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
466 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500467
Aaron Durbin7c351312013-04-10 14:46:25 -0500468 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500469 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500470 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
471 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500472
Aaron Durbin7c351312013-04-10 14:46:25 -0500473 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500474 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500475 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
476 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500477
Angel Pons4c95f102020-10-28 19:38:12 +0100478 /* Only Haswell ULT supports the 3-5 latency response registers */
479 if (!haswell_is_ult())
480 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500481
Angel Pons4c95f102020-10-28 19:38:12 +0100482 /* C-state Interrupt Response Latency Control 3 - package C8 */
483 msr.hi = 0;
484 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
485 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500486
Angel Pons4c95f102020-10-28 19:38:12 +0100487 /* C-state Interrupt Response Latency Control 4 - package C9 */
488 msr.hi = 0;
489 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
490 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
491
492 /* C-state Interrupt Response Latency Control 5 - package C10 */
493 msr.hi = 0;
494 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
495 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500496}
Aaron Durbin76c37002012-10-30 09:03:43 -0500497
498static void configure_thermal_target(void)
499{
500 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100501 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500502 msr_t msr;
503
504 /* Find pointer to CPU configuration */
505 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
506 if (!lapic || !lapic->chip_info)
507 return;
508 conf = lapic->chip_info;
509
Martin Roth4c3ab732013-07-08 16:23:54 -0600510 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500511 msr = rdmsr(MSR_PLATFORM_INFO);
512 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
513 msr = rdmsr(MSR_TEMPERATURE_TARGET);
514 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
515 msr.lo |= (conf->tcc_offset & 0xf) << 24;
516 wrmsr(MSR_TEMPERATURE_TARGET, msr);
517 }
518}
519
520static void configure_misc(void)
521{
522 msr_t msr;
523
524 msr = rdmsr(IA32_MISC_ENABLE);
525 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700526 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500527 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
528 wrmsr(IA32_MISC_ENABLE, msr);
529
530 /* Disable Thermal interrupts */
531 msr.lo = 0;
532 msr.hi = 0;
533 wrmsr(IA32_THERM_INTERRUPT, msr);
534
535 /* Enable package critical interrupt only */
536 msr.lo = 1 << 4;
537 msr.hi = 0;
538 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
539}
540
Aaron Durbin76c37002012-10-30 09:03:43 -0500541static void set_max_ratio(void)
542{
543 msr_t msr, perf_ctl;
544
545 perf_ctl.hi = 0;
546
547 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100548 if (get_turbo_state() == TURBO_ENABLED) {
549 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
550 perf_ctl.lo = (msr.lo & 0xff) << 8;
551 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500552 /* Set to nominal TDP ratio */
553 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
554 perf_ctl.lo = (msr.lo & 0xff) << 8;
555 } else {
556 /* Platform Info bits 15:8 give max ratio */
557 msr = rdmsr(MSR_PLATFORM_INFO);
558 perf_ctl.lo = msr.lo & 0xff00;
559 }
560 wrmsr(IA32_PERF_CTL, perf_ctl);
561
Angel Ponsf6cf49272020-09-25 01:14:24 +0200562 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100563 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500564}
565
Aaron Durbin76c37002012-10-30 09:03:43 -0500566static void configure_mca(void)
567{
568 msr_t msr;
569 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600570 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500571
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200572 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600573 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500574 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600575 /* TODO(adurbin): This should only be done on a cold boot. Also, some
576 * of these banks are core vs package scope. For now every CPU clears
577 * every bank. */
578 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500579 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
580}
581
Aaron Durbin305b1f02013-01-15 08:27:05 -0600582/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100583static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600584{
585 /* Clear out pending MCEs */
586 configure_mca();
587
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200588 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500589 enable_lapic_tpr();
590 setup_lapic();
591
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600592 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600593 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500594
Aaron Durbin76c37002012-10-30 09:03:43 -0500595 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500596 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500597
598 /* Configure Enhanced SpeedStep and Thermal Sensors */
599 configure_misc();
600
601 /* Thermal throttle activation offset */
602 configure_thermal_target();
603
604 /* Enable Direct Cache Access */
605 configure_dca_cap();
606
607 /* Set energy policy */
608 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
609
Aaron Durbin76c37002012-10-30 09:03:43 -0500610 /* Enable Turbo */
611 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600612}
Aaron Durbin76c37002012-10-30 09:03:43 -0500613
Aaron Durbin014baea2014-03-28 22:01:05 -0500614/* MP initialization support. */
615static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500616
Aaron Durbin463af332016-05-03 17:26:35 -0500617static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500618{
Aaron Durbin463af332016-05-03 17:26:35 -0500619 /* Setup MTRRs based on physical address size. */
620 x86_setup_mtrrs_with_detect();
621 x86_mtrr_check();
622
623 initialize_vr_config();
624
Angel Pons4c95f102020-10-28 19:38:12 +0100625 if (!haswell_is_ult())
626 return;
627
628 calibrate_24mhz_bclk();
629 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500630}
631
Aaron Durbin463af332016-05-03 17:26:35 -0500632static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500633{
Aaron Durbin463af332016-05-03 17:26:35 -0500634 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500635 int num_threads;
636 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500637
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200638 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500639 num_threads = (msr.lo >> 0) & 0xffff;
640 num_cores = (msr.lo >> 16) & 0xffff;
641 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
642 num_cores, num_threads);
643
Aaron Durbin463af332016-05-03 17:26:35 -0500644 return num_threads;
645}
Aaron Durbin7af20692013-01-14 14:54:41 -0600646
Aaron Durbin463af332016-05-03 17:26:35 -0500647static void get_microcode_info(const void **microcode, int *parallel)
648{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600649 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500650 *microcode = microcode_patch;
651 *parallel = 1;
652}
Aaron Durbin7af20692013-01-14 14:54:41 -0600653
Aaron Durbin463af332016-05-03 17:26:35 -0500654static void per_cpu_smm_trigger(void)
655{
656 /* Relocate the SMM handler. */
657 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600658
Aaron Durbin463af332016-05-03 17:26:35 -0500659 /* After SMM relocation a 2nd microcode load is required. */
660 intel_microcode_load_unlocked(microcode_patch);
661}
662
663static void post_mp_init(void)
664{
Angel Pons053deb82020-10-28 22:40:02 +0100665 /* Set Max Ratio */
666 set_max_ratio();
667
Aaron Durbin463af332016-05-03 17:26:35 -0500668 /* Now that all APs have been relocated as well as the BSP let SMIs
669 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300670 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500671
672 /* Lock down the SMRAM space. */
673 smm_lock();
674}
675
676static const struct mp_ops mp_ops = {
677 .pre_mp_init = pre_mp_init,
678 .get_cpu_count = get_cpu_count,
679 .get_smm_info = smm_info,
680 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500681 .pre_mp_smm_init = smm_initialize,
682 .per_cpu_smm_trigger = per_cpu_smm_trigger,
683 .relocation_handler = smm_relocation_handler,
684 .post_mp_init = post_mp_init,
685};
686
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300687void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500688{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700689 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500690 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500691}
692
693static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100694 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500695};
696
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100697static const struct cpu_device_id cpu_table[] = {
Angel Pons8b0636e2020-10-28 21:48:29 +0100698 { X86_VENDOR_INTEL, CPUID_HASWELL_A0 },
699 { X86_VENDOR_INTEL, CPUID_HASWELL_B0 },
700 { X86_VENDOR_INTEL, CPUID_HASWELL_C0 },
701 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_B0 },
702 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_C0 },
703 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_B0 },
704 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_C0 },
Aaron Durbin76c37002012-10-30 09:03:43 -0500705 { 0, 0 },
706};
707
708static const struct cpu_driver driver __cpu_driver = {
709 .ops = &cpu_dev_ops,
710 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500711 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500712};