blob: b1f8bba8155af4de4fe68501fbe507aa30b8730c [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
Angel Ponsc86b1192020-10-28 23:53:45 +0100297 set the fast ramp voltage in 10mV steps. */
298 if (cpu_family_model() == BROADWELL_FAMILY_ULT)
299 msr.lo |= 0x006a; /* 1.56V */
300 else
301 msr.lo |= 0x006f; /* 1.60V */
Angel Pons4c95f102020-10-28 19:38:12 +0100302 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500303}
304
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700305static void configure_pch_power_sharing(void)
306{
307 u32 pch_power, pch_power_ext, pmsync, pmsync2;
308 int i;
309
310 /* Read PCH Power levels from PCODE */
311 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
312 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
313
314 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700315 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700316
317 pmsync = RCBA32(PMSYNC_CONFIG);
318 pmsync2 = RCBA32(PMSYNC_CONFIG2);
319
320 /* Program PMSYNC_TPR_CONFIG PCH power limit values
321 * pmsync[0:4] = mailbox[0:5]
322 * pmsync[8:12] = mailbox[6:11]
323 * pmsync[16:20] = mailbox[12:17]
324 */
325 for (i = 0; i < 3; i++) {
326 u32 level = pch_power & 0x3f;
327 pch_power >>= 6;
328 pmsync &= ~(0x1f << (i * 8));
329 pmsync |= (level & 0x1f) << (i * 8);
330 }
331 RCBA32(PMSYNC_CONFIG) = pmsync;
332
333 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
334 * pmsync2[0:4] = mailbox[23:18]
335 * pmsync2[8:12] = mailbox_ext[6:11]
336 * pmsync2[16:20] = mailbox_ext[12:17]
337 * pmsync2[24:28] = mailbox_ext[18:22]
338 */
339 pmsync2 &= ~0x1f;
340 pmsync2 |= pch_power & 0x1f;
341
342 for (i = 1; i < 4; i++) {
343 u32 level = pch_power_ext & 0x3f;
344 pch_power_ext >>= 6;
345 pmsync2 &= ~(0x1f << (i * 8));
346 pmsync2 |= (level & 0x1f) << (i * 8);
347 }
348 RCBA32(PMSYNC_CONFIG2) = pmsync2;
349}
350
Aaron Durbin76c37002012-10-30 09:03:43 -0500351int cpu_config_tdp_levels(void)
352{
353 msr_t platform_info;
354
355 /* Bits 34:33 indicate how many levels supported */
356 platform_info = rdmsr(MSR_PLATFORM_INFO);
357 return (platform_info.hi >> 1) & 3;
358}
359
360/*
361 * Configure processor power limits if possible
362 * This must be done AFTER set of BIOS_RESET_CPL
363 */
364void set_power_limits(u8 power_limit_1_time)
365{
366 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
367 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700368 unsigned int power_unit;
369 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500370 u8 power_limit_1_val;
371
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000372 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100373 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500374
375 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
376 return;
377
378 /* Get units */
379 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
380 power_unit = 2 << ((msr.lo & 0xf) - 1);
381
382 /* Get power defaults for this SKU */
383 msr = rdmsr(MSR_PKG_POWER_SKU);
384 tdp = msr.lo & 0x7fff;
385 min_power = (msr.lo >> 16) & 0x7fff;
386 max_power = msr.hi & 0x7fff;
387 max_time = (msr.hi >> 16) & 0x7f;
388
389 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
390
391 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
392 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
393
394 if (min_power > 0 && tdp < min_power)
395 tdp = min_power;
396
397 if (max_power > 0 && tdp > max_power)
398 tdp = max_power;
399
400 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
401
402 /* Set long term power limit to TDP */
403 limit.lo = 0;
404 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
405 limit.lo |= PKG_POWER_LIMIT_EN;
406 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
407 PKG_POWER_LIMIT_TIME_SHIFT;
408
409 /* Set short term power limit to 1.25 * TDP */
410 limit.hi = 0;
411 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
412 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700413 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500414
415 wrmsr(MSR_PKG_POWER_LIMIT, limit);
416
Duncan Lauriec70353f2013-06-28 14:40:38 -0700417 /* Set power limit values in MCHBAR as well */
418 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
419 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
420
421 /* Set DDR RAPL power limit by copying from MMIO to MSR */
422 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
423 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
424 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
425
Aaron Durbin76c37002012-10-30 09:03:43 -0500426 /* Use nominal TDP values for CPUs with configurable TDP */
427 if (cpu_config_tdp_levels()) {
428 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
429 limit.hi = 0;
430 limit.lo = msr.lo & 0xff;
431 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
432 }
433}
434
Aaron Durbin76c37002012-10-30 09:03:43 -0500435static void configure_c_states(void)
436{
Angel Ponsc89d2a282020-10-28 22:23:02 +0100437 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
438
439 const bool timed_mwait_capable = !!(msr.hi & TIMED_MWAIT_SUPPORTED);
Aaron Durbin76c37002012-10-30 09:03:43 -0500440
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200441 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500442 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
443 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500444 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
445 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
446 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
447 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
448 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Angel Ponsc89d2a282020-10-28 22:23:02 +0100449
450 if (timed_mwait_capable)
451 msr.lo |= (1 << 31); // Timed MWAIT Enable
452
Duncan Laurie1c097102013-05-07 13:19:56 -0700453 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200454 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500455
Aaron Durbin76c37002012-10-30 09:03:43 -0500456 msr = rdmsr(MSR_MISC_PWR_MGMT);
457 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
458 wrmsr(MSR_MISC_PWR_MGMT, msr);
459
460 msr = rdmsr(MSR_POWER_CTL);
461 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
462 msr.lo |= (1 << 1); // C1E Enable
463 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
464 wrmsr(MSR_POWER_CTL, msr);
465
Aaron Durbin7c351312013-04-10 14:46:25 -0500466 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500467 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500468 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
469 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500470
Aaron Durbin7c351312013-04-10 14:46:25 -0500471 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500472 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500473 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
474 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500475
Aaron Durbin7c351312013-04-10 14:46:25 -0500476 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500477 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500478 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
479 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500480
Angel Pons4c95f102020-10-28 19:38:12 +0100481 /* Only Haswell ULT supports the 3-5 latency response registers */
482 if (!haswell_is_ult())
483 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500484
Angel Pons4c95f102020-10-28 19:38:12 +0100485 /* C-state Interrupt Response Latency Control 3 - package C8 */
486 msr.hi = 0;
487 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
488 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500489
Angel Pons4c95f102020-10-28 19:38:12 +0100490 /* C-state Interrupt Response Latency Control 4 - package C9 */
491 msr.hi = 0;
492 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
493 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
494
495 /* C-state Interrupt Response Latency Control 5 - package C10 */
496 msr.hi = 0;
497 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
498 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500499}
Aaron Durbin76c37002012-10-30 09:03:43 -0500500
501static void configure_thermal_target(void)
502{
503 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100504 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500505 msr_t msr;
506
507 /* Find pointer to CPU configuration */
508 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
509 if (!lapic || !lapic->chip_info)
510 return;
511 conf = lapic->chip_info;
512
Martin Roth4c3ab732013-07-08 16:23:54 -0600513 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500514 msr = rdmsr(MSR_PLATFORM_INFO);
515 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
516 msr = rdmsr(MSR_TEMPERATURE_TARGET);
517 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
518 msr.lo |= (conf->tcc_offset & 0xf) << 24;
519 wrmsr(MSR_TEMPERATURE_TARGET, msr);
520 }
521}
522
523static void configure_misc(void)
524{
525 msr_t msr;
526
527 msr = rdmsr(IA32_MISC_ENABLE);
528 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700529 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500530 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
531 wrmsr(IA32_MISC_ENABLE, msr);
532
533 /* Disable Thermal interrupts */
534 msr.lo = 0;
535 msr.hi = 0;
536 wrmsr(IA32_THERM_INTERRUPT, msr);
537
538 /* Enable package critical interrupt only */
539 msr.lo = 1 << 4;
540 msr.hi = 0;
541 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
542}
543
Aaron Durbin76c37002012-10-30 09:03:43 -0500544static void set_max_ratio(void)
545{
546 msr_t msr, perf_ctl;
547
548 perf_ctl.hi = 0;
549
550 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100551 if (get_turbo_state() == TURBO_ENABLED) {
552 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
553 perf_ctl.lo = (msr.lo & 0xff) << 8;
554 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500555 /* Set to nominal TDP ratio */
556 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
557 perf_ctl.lo = (msr.lo & 0xff) << 8;
558 } else {
559 /* Platform Info bits 15:8 give max ratio */
560 msr = rdmsr(MSR_PLATFORM_INFO);
561 perf_ctl.lo = msr.lo & 0xff00;
562 }
563 wrmsr(IA32_PERF_CTL, perf_ctl);
564
Angel Ponsf6cf49272020-09-25 01:14:24 +0200565 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100566 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500567}
568
Aaron Durbin76c37002012-10-30 09:03:43 -0500569static void configure_mca(void)
570{
571 msr_t msr;
572 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600573 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500574
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200575 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600576 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500577 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600578 /* TODO(adurbin): This should only be done on a cold boot. Also, some
579 * of these banks are core vs package scope. For now every CPU clears
580 * every bank. */
581 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500582 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
583}
584
Aaron Durbin305b1f02013-01-15 08:27:05 -0600585/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100586static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600587{
588 /* Clear out pending MCEs */
589 configure_mca();
590
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200591 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500592 enable_lapic_tpr();
593 setup_lapic();
594
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600595 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600596 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500597
Aaron Durbin76c37002012-10-30 09:03:43 -0500598 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500599 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500600
601 /* Configure Enhanced SpeedStep and Thermal Sensors */
602 configure_misc();
603
604 /* Thermal throttle activation offset */
605 configure_thermal_target();
606
607 /* Enable Direct Cache Access */
608 configure_dca_cap();
609
610 /* Set energy policy */
611 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
612
Aaron Durbin76c37002012-10-30 09:03:43 -0500613 /* Enable Turbo */
614 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600615}
Aaron Durbin76c37002012-10-30 09:03:43 -0500616
Aaron Durbin014baea2014-03-28 22:01:05 -0500617/* MP initialization support. */
618static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500619
Aaron Durbin463af332016-05-03 17:26:35 -0500620static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500621{
Aaron Durbin463af332016-05-03 17:26:35 -0500622 /* Setup MTRRs based on physical address size. */
623 x86_setup_mtrrs_with_detect();
624 x86_mtrr_check();
625
626 initialize_vr_config();
627
Angel Pons4c95f102020-10-28 19:38:12 +0100628 if (!haswell_is_ult())
629 return;
630
631 calibrate_24mhz_bclk();
632 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500633}
634
Aaron Durbin463af332016-05-03 17:26:35 -0500635static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500636{
Aaron Durbin463af332016-05-03 17:26:35 -0500637 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500638 int num_threads;
639 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500640
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200641 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500642 num_threads = (msr.lo >> 0) & 0xffff;
643 num_cores = (msr.lo >> 16) & 0xffff;
644 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
645 num_cores, num_threads);
646
Aaron Durbin463af332016-05-03 17:26:35 -0500647 return num_threads;
648}
Aaron Durbin7af20692013-01-14 14:54:41 -0600649
Aaron Durbin463af332016-05-03 17:26:35 -0500650static void get_microcode_info(const void **microcode, int *parallel)
651{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600652 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500653 *microcode = microcode_patch;
654 *parallel = 1;
655}
Aaron Durbin7af20692013-01-14 14:54:41 -0600656
Aaron Durbin463af332016-05-03 17:26:35 -0500657static void per_cpu_smm_trigger(void)
658{
659 /* Relocate the SMM handler. */
660 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600661
Aaron Durbin463af332016-05-03 17:26:35 -0500662 /* After SMM relocation a 2nd microcode load is required. */
663 intel_microcode_load_unlocked(microcode_patch);
664}
665
666static void post_mp_init(void)
667{
Angel Pons053deb82020-10-28 22:40:02 +0100668 /* Set Max Ratio */
669 set_max_ratio();
670
Aaron Durbin463af332016-05-03 17:26:35 -0500671 /* Now that all APs have been relocated as well as the BSP let SMIs
672 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300673 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500674
675 /* Lock down the SMRAM space. */
676 smm_lock();
677}
678
679static const struct mp_ops mp_ops = {
680 .pre_mp_init = pre_mp_init,
681 .get_cpu_count = get_cpu_count,
682 .get_smm_info = smm_info,
683 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500684 .pre_mp_smm_init = smm_initialize,
685 .per_cpu_smm_trigger = per_cpu_smm_trigger,
686 .relocation_handler = smm_relocation_handler,
687 .post_mp_init = post_mp_init,
688};
689
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300690void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500691{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700692 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500693 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500694}
695
696static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100697 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500698};
699
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100700static const struct cpu_device_id cpu_table[] = {
Angel Pons8b0636e2020-10-28 21:48:29 +0100701 { X86_VENDOR_INTEL, CPUID_HASWELL_A0 },
702 { X86_VENDOR_INTEL, CPUID_HASWELL_B0 },
703 { X86_VENDOR_INTEL, CPUID_HASWELL_C0 },
704 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_B0 },
705 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_C0 },
706 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_B0 },
707 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_C0 },
Aaron Durbin76c37002012-10-30 09:03:43 -0500708 { 0, 0 },
709};
710
711static const struct cpu_driver driver __cpu_driver = {
712 .ops = &cpu_dev_ops,
713 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500714 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500715};