blob: 0449235ba5e708175e034386e73563ab96a5900b [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
10 * the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050016 */
17
18#include <console/console.h>
19#include <device/device.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include <arch/acpi.h>
21#include <cpu/cpu.h>
22#include <cpu/x86/mtrr.h>
23#include <cpu/x86/msr.h>
Aaron Durbin014baea2014-03-28 22:01:05 -050024#include <cpu/x86/mp.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050025#include <cpu/x86/lapic.h>
26#include <cpu/intel/microcode.h>
27#include <cpu/intel/speedstep.h>
28#include <cpu/intel/turbo.h>
29#include <cpu/x86/cache.h>
30#include <cpu/x86/name.h>
Aaron Durbin6a360042014-02-13 10:30:42 -060031#include <cpu/x86/smm.h>
Aaron Durbinf24262d2013-04-10 14:59:21 -050032#include <delay.h>
Aaron Durbin7c351312013-04-10 14:46:25 -050033#include <northbridge/intel/haswell/haswell.h>
34#include <southbridge/intel/lynxpoint/pch.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060035#include <cpu/intel/common/common.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050036#include "haswell.h"
37#include "chip.h"
38
Aaron Durbin7c351312013-04-10 14:46:25 -050039/* Intel suggested latency times in units of 1024ns. */
40#define C_STATE_LATENCY_CONTROL_0_LIMIT 0x42
41#define C_STATE_LATENCY_CONTROL_1_LIMIT 0x73
42#define C_STATE_LATENCY_CONTROL_2_LIMIT 0x91
43#define C_STATE_LATENCY_CONTROL_3_LIMIT 0xe4
44#define C_STATE_LATENCY_CONTROL_4_LIMIT 0x145
45#define C_STATE_LATENCY_CONTROL_5_LIMIT 0x1ef
46
47#define C_STATE_LATENCY_MICRO_SECONDS(limit, base) \
48 (((1 << ((base)*5)) * (limit)) / 1000)
49#define C_STATE_LATENCY_FROM_LAT_REG(reg) \
50 C_STATE_LATENCY_MICRO_SECONDS(C_STATE_LATENCY_CONTROL_ ##reg## _LIMIT, \
Lee Leahy7b5f12b92017-03-15 17:16:59 -070051 (IRTL_1024_NS >> 10))
Aaron Durbin7c351312013-04-10 14:46:25 -050052
Aaron Durbin76c37002012-10-30 09:03:43 -050053/*
Aaron Durbin7c351312013-04-10 14:46:25 -050054 * List of supported C-states in this processor. Only the ULT parts support C8,
55 * C9, and C10.
Aaron Durbin76c37002012-10-30 09:03:43 -050056 */
Aaron Durbin7c351312013-04-10 14:46:25 -050057enum {
58 C_STATE_C0, /* 0 */
59 C_STATE_C1, /* 1 */
60 C_STATE_C1E, /* 2 */
61 C_STATE_C3, /* 3 */
62 C_STATE_C6_SHORT_LAT, /* 4 */
63 C_STATE_C6_LONG_LAT, /* 5 */
64 C_STATE_C7_SHORT_LAT, /* 6 */
65 C_STATE_C7_LONG_LAT, /* 7 */
66 C_STATE_C7S_SHORT_LAT, /* 8 */
67 C_STATE_C7S_LONG_LAT, /* 9 */
68 C_STATE_C8, /* 10 */
69 C_STATE_C9, /* 11 */
70 C_STATE_C10, /* 12 */
71 NUM_C_STATES
Aaron Durbin76c37002012-10-30 09:03:43 -050072};
Aaron Durbin7c351312013-04-10 14:46:25 -050073
74#define MWAIT_RES(state, sub_state) \
75 { \
76 .addrl = (((state) << 4) | (sub_state)), \
77 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
78 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
79 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
80 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
81 }
82
83static acpi_cstate_t cstate_map[NUM_C_STATES] = {
84 [C_STATE_C0] = { },
85 [C_STATE_C1] = {
86 .latency = 0,
87 .power = 1000,
Lee Leahy9d62e7e2017-03-15 17:40:50 -070088 .resource = MWAIT_RES(0, 0),
Aaron Durbin7c351312013-04-10 14:46:25 -050089 },
90 [C_STATE_C1E] = {
91 .latency = 0,
92 .power = 1000,
Lee Leahy9d62e7e2017-03-15 17:40:50 -070093 .resource = MWAIT_RES(0, 1),
Aaron Durbin7c351312013-04-10 14:46:25 -050094 },
95 [C_STATE_C3] = {
96 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
97 .power = 900,
98 .resource = MWAIT_RES(1, 0),
99 },
100 [C_STATE_C6_SHORT_LAT] = {
101 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
102 .power = 800,
103 .resource = MWAIT_RES(2, 0),
104 },
105 [C_STATE_C6_LONG_LAT] = {
106 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
107 .power = 800,
108 .resource = MWAIT_RES(2, 1),
109 },
110 [C_STATE_C7_SHORT_LAT] = {
111 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
112 .power = 700,
113 .resource = MWAIT_RES(3, 0),
114 },
115 [C_STATE_C7_LONG_LAT] = {
116 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
117 .power = 700,
118 .resource = MWAIT_RES(3, 1),
119 },
120 [C_STATE_C7S_SHORT_LAT] = {
121 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
122 .power = 700,
123 .resource = MWAIT_RES(3, 2),
124 },
125 [C_STATE_C7S_LONG_LAT] = {
126 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
127 .power = 700,
128 .resource = MWAIT_RES(3, 3),
129 },
130 [C_STATE_C8] = {
131 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
132 .power = 600,
133 .resource = MWAIT_RES(4, 0),
134 },
135 [C_STATE_C9] = {
136 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
137 .power = 500,
138 .resource = MWAIT_RES(5, 0),
139 },
140 [C_STATE_C10] = {
141 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
142 .power = 400,
143 .resource = MWAIT_RES(6, 0),
144 },
145};
Aaron Durbin76c37002012-10-30 09:03:43 -0500146
147/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
148static const u8 power_limit_time_sec_to_msr[] = {
149 [0] = 0x00,
150 [1] = 0x0a,
151 [2] = 0x0b,
152 [3] = 0x4b,
153 [4] = 0x0c,
154 [5] = 0x2c,
155 [6] = 0x4c,
156 [7] = 0x6c,
157 [8] = 0x0d,
158 [10] = 0x2d,
159 [12] = 0x4d,
160 [14] = 0x6d,
161 [16] = 0x0e,
162 [20] = 0x2e,
163 [24] = 0x4e,
164 [28] = 0x6e,
165 [32] = 0x0f,
166 [40] = 0x2f,
167 [48] = 0x4f,
168 [56] = 0x6f,
169 [64] = 0x10,
170 [80] = 0x30,
171 [96] = 0x50,
172 [112] = 0x70,
173 [128] = 0x11,
174};
175
176/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
177static const u8 power_limit_time_msr_to_sec[] = {
178 [0x00] = 0,
179 [0x0a] = 1,
180 [0x0b] = 2,
181 [0x4b] = 3,
182 [0x0c] = 4,
183 [0x2c] = 5,
184 [0x4c] = 6,
185 [0x6c] = 7,
186 [0x0d] = 8,
187 [0x2d] = 10,
188 [0x4d] = 12,
189 [0x6d] = 14,
190 [0x0e] = 16,
191 [0x2e] = 20,
192 [0x4e] = 24,
193 [0x6e] = 28,
194 [0x0f] = 32,
195 [0x2f] = 40,
196 [0x4f] = 48,
197 [0x6f] = 56,
198 [0x10] = 64,
199 [0x30] = 80,
200 [0x50] = 96,
201 [0x70] = 112,
202 [0x11] = 128,
203};
204
Duncan Laurie118d1052013-07-09 15:34:25 -0700205int haswell_family_model(void)
206{
207 return cpuid_eax(1) & 0x0fff0ff0;
208}
209
210int haswell_stepping(void)
211{
212 return cpuid_eax(1) & 0xf;
213}
214
Aaron Durbin7c351312013-04-10 14:46:25 -0500215/* Dynamically determine if the part is ULT. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700216int haswell_is_ult(void)
Aaron Durbin7c351312013-04-10 14:46:25 -0500217{
218 static int ult = -1;
219
220 if (ult < 0)
Duncan Laurie118d1052013-07-09 15:34:25 -0700221 ult = !!(haswell_family_model() == HASWELL_FAMILY_ULT);
Aaron Durbin7c351312013-04-10 14:46:25 -0500222
223 return ult;
224}
225
Aaron Durbinf24262d2013-04-10 14:59:21 -0500226/* The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
227 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
228 * when a core is woken up. */
229static int pcode_ready(void)
230{
231 int wait_count;
232 const int delay_step = 10;
233
234 wait_count = 0;
235 do {
236 if (!(MCHBAR32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
237 return 0;
238 wait_count += delay_step;
239 udelay(delay_step);
240 } while (wait_count < 1000);
241
242 return -1;
243}
244
245static void calibrate_24mhz_bclk(void)
246{
247 int err_code;
248
249 if (pcode_ready() < 0) {
250 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
251 return;
252 }
253
254 /* A non-zero value initiates the PCODE calibration. */
255 MCHBAR32(BIOS_MAILBOX_DATA) = ~0;
256 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
257 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL;
258
259 if (pcode_ready() < 0) {
260 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
261 return;
262 }
263
264 err_code = MCHBAR32(BIOS_MAILBOX_INTERFACE) & 0xff;
265
266 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration response: %d\n",
267 err_code);
268
269 /* Read the calibrated value. */
270 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
271 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION;
272
273 if (pcode_ready() < 0) {
274 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
275 return;
276 }
277
278 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration value: 0x%08x\n",
279 MCHBAR32(BIOS_MAILBOX_DATA));
280}
281
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700282static u32 pcode_mailbox_read(u32 command)
283{
284 if (pcode_ready() < 0) {
285 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
286 return 0;
287 }
288
289 /* Send command and start transaction */
290 MCHBAR32(BIOS_MAILBOX_INTERFACE) = command | MAILBOX_RUN_BUSY;
291
292 if (pcode_ready() < 0) {
293 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
294 return 0;
295 }
296
297 /* Read mailbox */
298 return MCHBAR32(BIOS_MAILBOX_DATA);
299}
300
Aaron Durbin16cbf892013-07-03 16:21:28 -0500301static void initialize_vr_config(void)
302{
303 msr_t msr;
304
305 printk(BIOS_DEBUG, "Initializing VR config.\n");
306
307 /* Configure VR_CURRENT_CONFIG. */
308 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
309 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
310 * on ULT systems. */
311 msr.hi &= 0xc0000000;
312 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
313 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
314 msr.hi |= (0x0f << (32 - 32)); /* PSI1 threshold - 15A. */
315
Duncan Laurie118d1052013-07-09 15:34:25 -0700316 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500317 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
318 /* Leave the max instantaneous current limit (12:0) to default. */
319 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
320
321 /* Configure VR_MISC_CONFIG MSR. */
322 msr = rdmsr(MSR_VR_MISC_CONFIG);
323 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
324 msr.hi &= ~(0x3ff << (40 - 32));
325 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
326 /* Set IOUT_OFFSET to 0. */
327 msr.hi &= ~0xff;
328 /* Set exit ramp rate to fast. */
329 msr.hi |= (1 << (50 - 32));
330 /* Set entry ramp rate to slow. */
331 msr.hi &= ~(1 << (51 - 32));
332 /* Enable decay mode on C-state entry. */
333 msr.hi |= (1 << (52 - 32));
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300334 if (haswell_is_ult()) {
335 /* Set the slow ramp rate to be fast ramp rate / 4 */
336 msr.hi &= ~(0x3 << (53 - 32));
337 msr.hi |= (0x01 << (53 - 32));
338 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500339 /* Set MIN_VID (31:24) to allow CPU to have full control. */
340 msr.lo &= ~0xff000000;
341 wrmsr(MSR_VR_MISC_CONFIG, msr);
342
343 /* Configure VR_MISC_CONFIG2 MSR. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700344 if (haswell_is_ult()) {
Aaron Durbin16cbf892013-07-03 16:21:28 -0500345 msr = rdmsr(MSR_VR_MISC_CONFIG2);
346 msr.lo &= ~0xffff;
347 /* Allow CPU to control minimum voltage completely (15:8) and
348 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
349 msr.lo |= 0x006f;
350 wrmsr(MSR_VR_MISC_CONFIG2, msr);
351 }
352}
353
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700354static void configure_pch_power_sharing(void)
355{
356 u32 pch_power, pch_power_ext, pmsync, pmsync2;
357 int i;
358
359 /* Read PCH Power levels from PCODE */
360 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
361 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
362
363 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700364 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700365
366 pmsync = RCBA32(PMSYNC_CONFIG);
367 pmsync2 = RCBA32(PMSYNC_CONFIG2);
368
369 /* Program PMSYNC_TPR_CONFIG PCH power limit values
370 * pmsync[0:4] = mailbox[0:5]
371 * pmsync[8:12] = mailbox[6:11]
372 * pmsync[16:20] = mailbox[12:17]
373 */
374 for (i = 0; i < 3; i++) {
375 u32 level = pch_power & 0x3f;
376 pch_power >>= 6;
377 pmsync &= ~(0x1f << (i * 8));
378 pmsync |= (level & 0x1f) << (i * 8);
379 }
380 RCBA32(PMSYNC_CONFIG) = pmsync;
381
382 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
383 * pmsync2[0:4] = mailbox[23:18]
384 * pmsync2[8:12] = mailbox_ext[6:11]
385 * pmsync2[16:20] = mailbox_ext[12:17]
386 * pmsync2[24:28] = mailbox_ext[18:22]
387 */
388 pmsync2 &= ~0x1f;
389 pmsync2 |= pch_power & 0x1f;
390
391 for (i = 1; i < 4; i++) {
392 u32 level = pch_power_ext & 0x3f;
393 pch_power_ext >>= 6;
394 pmsync2 &= ~(0x1f << (i * 8));
395 pmsync2 |= (level & 0x1f) << (i * 8);
396 }
397 RCBA32(PMSYNC_CONFIG2) = pmsync2;
398}
399
Aaron Durbin76c37002012-10-30 09:03:43 -0500400int cpu_config_tdp_levels(void)
401{
402 msr_t platform_info;
403
404 /* Bits 34:33 indicate how many levels supported */
405 platform_info = rdmsr(MSR_PLATFORM_INFO);
406 return (platform_info.hi >> 1) & 3;
407}
408
409/*
410 * Configure processor power limits if possible
411 * This must be done AFTER set of BIOS_RESET_CPL
412 */
413void set_power_limits(u8 power_limit_1_time)
414{
415 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
416 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700417 unsigned int power_unit;
418 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500419 u8 power_limit_1_val;
420
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000421 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Lee Leahycdc50482017-03-15 18:26:18 -0700422 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr)
423 - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500424
425 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
426 return;
427
428 /* Get units */
429 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
430 power_unit = 2 << ((msr.lo & 0xf) - 1);
431
432 /* Get power defaults for this SKU */
433 msr = rdmsr(MSR_PKG_POWER_SKU);
434 tdp = msr.lo & 0x7fff;
435 min_power = (msr.lo >> 16) & 0x7fff;
436 max_power = msr.hi & 0x7fff;
437 max_time = (msr.hi >> 16) & 0x7f;
438
439 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
440
441 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
442 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
443
444 if (min_power > 0 && tdp < min_power)
445 tdp = min_power;
446
447 if (max_power > 0 && tdp > max_power)
448 tdp = max_power;
449
450 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
451
452 /* Set long term power limit to TDP */
453 limit.lo = 0;
454 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
455 limit.lo |= PKG_POWER_LIMIT_EN;
456 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
457 PKG_POWER_LIMIT_TIME_SHIFT;
458
459 /* Set short term power limit to 1.25 * TDP */
460 limit.hi = 0;
461 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
462 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700463 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500464
465 wrmsr(MSR_PKG_POWER_LIMIT, limit);
466
Duncan Lauriec70353f2013-06-28 14:40:38 -0700467 /* Set power limit values in MCHBAR as well */
468 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
469 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
470
471 /* Set DDR RAPL power limit by copying from MMIO to MSR */
472 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
473 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
474 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
475
Aaron Durbin76c37002012-10-30 09:03:43 -0500476 /* Use nominal TDP values for CPUs with configurable TDP */
477 if (cpu_config_tdp_levels()) {
478 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
479 limit.hi = 0;
480 limit.lo = msr.lo & 0xff;
481 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
482 }
483}
484
Aaron Durbin76c37002012-10-30 09:03:43 -0500485static void configure_c_states(void)
486{
487 msr_t msr;
488
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200489 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500490 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
491 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500492 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
493 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
494 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
495 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
496 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700497 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200498 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500499
500 msr = rdmsr(MSR_PMG_IO_CAPTURE_BASE);
Aaron Durbin7c351312013-04-10 14:46:25 -0500501 msr.lo &= ~0xffff;
502 msr.lo |= (get_pmbase() + 0x14); // LVL_2 base address
503 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500504 wrmsr(MSR_PMG_IO_CAPTURE_BASE, msr);
505
506 msr = rdmsr(MSR_MISC_PWR_MGMT);
507 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
508 wrmsr(MSR_MISC_PWR_MGMT, msr);
509
510 msr = rdmsr(MSR_POWER_CTL);
511 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
512 msr.lo |= (1 << 1); // C1E Enable
513 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
514 wrmsr(MSR_POWER_CTL, msr);
515
Aaron Durbin7c351312013-04-10 14:46:25 -0500516 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500517 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500518 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
519 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500520
Aaron Durbin7c351312013-04-10 14:46:25 -0500521 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500522 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500523 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
524 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500525
Aaron Durbin7c351312013-04-10 14:46:25 -0500526 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500527 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500528 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
529 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500530
Aaron Durbin7c351312013-04-10 14:46:25 -0500531 /* Haswell ULT only supoprts the 3-5 latency response registers.*/
Duncan Laurie118d1052013-07-09 15:34:25 -0700532 if (haswell_is_ult()) {
Aaron Durbin7c351312013-04-10 14:46:25 -0500533 /* C-state Interrupt Response Latency Control 3 - package C8 */
534 msr.hi = 0;
535 msr.lo = IRTL_VALID | IRTL_1024_NS |
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700536 C_STATE_LATENCY_CONTROL_3_LIMIT;
Aaron Durbin7c351312013-04-10 14:46:25 -0500537 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500538
Aaron Durbin7c351312013-04-10 14:46:25 -0500539 /* C-state Interrupt Response Latency Control 4 - package C9 */
540 msr.hi = 0;
541 msr.lo = IRTL_VALID | IRTL_1024_NS |
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700542 C_STATE_LATENCY_CONTROL_4_LIMIT;
Aaron Durbin7c351312013-04-10 14:46:25 -0500543 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
544
545 /* C-state Interrupt Response Latency Control 5 - package C10 */
546 msr.hi = 0;
547 msr.lo = IRTL_VALID | IRTL_1024_NS |
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700548 C_STATE_LATENCY_CONTROL_5_LIMIT;
Aaron Durbin7c351312013-04-10 14:46:25 -0500549 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
550 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500551}
Aaron Durbin76c37002012-10-30 09:03:43 -0500552
553static void configure_thermal_target(void)
554{
555 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100556 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500557 msr_t msr;
558
559 /* Find pointer to CPU configuration */
560 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
561 if (!lapic || !lapic->chip_info)
562 return;
563 conf = lapic->chip_info;
564
Martin Roth4c3ab732013-07-08 16:23:54 -0600565 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500566 msr = rdmsr(MSR_PLATFORM_INFO);
567 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
568 msr = rdmsr(MSR_TEMPERATURE_TARGET);
569 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
570 msr.lo |= (conf->tcc_offset & 0xf) << 24;
571 wrmsr(MSR_TEMPERATURE_TARGET, msr);
572 }
573}
574
575static void configure_misc(void)
576{
577 msr_t msr;
578
579 msr = rdmsr(IA32_MISC_ENABLE);
580 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700581 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500582 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
583 wrmsr(IA32_MISC_ENABLE, msr);
584
585 /* Disable Thermal interrupts */
586 msr.lo = 0;
587 msr.hi = 0;
588 wrmsr(IA32_THERM_INTERRUPT, msr);
589
590 /* Enable package critical interrupt only */
591 msr.lo = 1 << 4;
592 msr.hi = 0;
593 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
594}
595
596static void enable_lapic_tpr(void)
597{
598 msr_t msr;
599
600 msr = rdmsr(MSR_PIC_MSG_CONTROL);
601 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
602 wrmsr(MSR_PIC_MSG_CONTROL, msr);
603}
604
605static void configure_dca_cap(void)
606{
Subrata Banik53b08c32018-12-10 14:11:35 +0530607 uint32_t feature_flag;
Aaron Durbin76c37002012-10-30 09:03:43 -0500608 msr_t msr;
609
610 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
Subrata Banik53b08c32018-12-10 14:11:35 +0530611 feature_flag = cpu_get_feature_flags_ecx();
612 if (feature_flag & CPUID_DCA) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500613 msr = rdmsr(IA32_PLATFORM_DCA_CAP);
614 msr.lo |= 1;
615 wrmsr(IA32_PLATFORM_DCA_CAP, msr);
616 }
617}
618
619static void set_max_ratio(void)
620{
621 msr_t msr, perf_ctl;
622
623 perf_ctl.hi = 0;
624
625 /* Check for configurable TDP option */
626 if (cpu_config_tdp_levels()) {
627 /* Set to nominal TDP ratio */
628 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
629 perf_ctl.lo = (msr.lo & 0xff) << 8;
630 } else {
631 /* Platform Info bits 15:8 give max ratio */
632 msr = rdmsr(MSR_PLATFORM_INFO);
633 perf_ctl.lo = msr.lo & 0xff00;
634 }
635 wrmsr(IA32_PERF_CTL, perf_ctl);
636
637 printk(BIOS_DEBUG, "haswell: frequency set to %d\n",
638 ((perf_ctl.lo >> 8) & 0xff) * HASWELL_BCLK);
639}
640
641static void set_energy_perf_bias(u8 policy)
642{
643 msr_t msr;
Aaron Durbindc278f82012-12-11 17:15:13 -0600644 int ecx;
645
646 /* Determine if energy efficient policy is supported. */
647 ecx = cpuid_ecx(0x6);
648 if (!(ecx & (1 << 3)))
649 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500650
651 /* Energy Policy is bits 3:0 */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200652 msr = rdmsr(IA32_ENERGY_PERF_BIAS);
Aaron Durbin76c37002012-10-30 09:03:43 -0500653 msr.lo &= ~0xf;
654 msr.lo |= policy & 0xf;
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200655 wrmsr(IA32_ENERGY_PERF_BIAS, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500656
657 printk(BIOS_DEBUG, "haswell: energy policy set to %u\n",
658 policy);
659}
660
661static void configure_mca(void)
662{
663 msr_t msr;
664 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600665 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500666
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200667 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600668 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500669 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600670 /* TODO(adurbin): This should only be done on a cold boot. Also, some
671 * of these banks are core vs package scope. For now every CPU clears
672 * every bank. */
673 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500674 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
675}
676
Aaron Durbin305b1f02013-01-15 08:27:05 -0600677/* All CPUs including BSP will run the following function. */
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100678static void haswell_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600679{
680 /* Clear out pending MCEs */
681 configure_mca();
682
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200683 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500684 enable_lapic_tpr();
685 setup_lapic();
686
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600687 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600688 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500689
Aaron Durbin76c37002012-10-30 09:03:43 -0500690 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500691 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500692
693 /* Configure Enhanced SpeedStep and Thermal Sensors */
694 configure_misc();
695
696 /* Thermal throttle activation offset */
697 configure_thermal_target();
698
699 /* Enable Direct Cache Access */
700 configure_dca_cap();
701
702 /* Set energy policy */
703 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
704
705 /* Set Max Ratio */
706 set_max_ratio();
707
708 /* Enable Turbo */
709 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600710}
Aaron Durbin76c37002012-10-30 09:03:43 -0500711
Aaron Durbin014baea2014-03-28 22:01:05 -0500712/* MP initialization support. */
713static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500714
Aaron Durbin463af332016-05-03 17:26:35 -0500715static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500716{
Aaron Durbin463af332016-05-03 17:26:35 -0500717 /* Setup MTRRs based on physical address size. */
718 x86_setup_mtrrs_with_detect();
719 x86_mtrr_check();
720
721 initialize_vr_config();
722
723 if (haswell_is_ult()) {
724 calibrate_24mhz_bclk();
725 configure_pch_power_sharing();
726 }
Aaron Durbin014baea2014-03-28 22:01:05 -0500727}
728
Aaron Durbin463af332016-05-03 17:26:35 -0500729static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500730{
Aaron Durbin463af332016-05-03 17:26:35 -0500731 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500732 int num_threads;
733 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500734
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200735 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500736 num_threads = (msr.lo >> 0) & 0xffff;
737 num_cores = (msr.lo >> 16) & 0xffff;
738 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
739 num_cores, num_threads);
740
Aaron Durbin463af332016-05-03 17:26:35 -0500741 return num_threads;
742}
Aaron Durbin7af20692013-01-14 14:54:41 -0600743
Aaron Durbin463af332016-05-03 17:26:35 -0500744static void get_microcode_info(const void **microcode, int *parallel)
745{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600746 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500747 *microcode = microcode_patch;
748 *parallel = 1;
749}
Aaron Durbin7af20692013-01-14 14:54:41 -0600750
Aaron Durbin463af332016-05-03 17:26:35 -0500751static void per_cpu_smm_trigger(void)
752{
753 /* Relocate the SMM handler. */
754 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600755
Aaron Durbin463af332016-05-03 17:26:35 -0500756 /* After SMM relocation a 2nd microcode load is required. */
757 intel_microcode_load_unlocked(microcode_patch);
758}
759
760static void post_mp_init(void)
761{
762 /* Now that all APs have been relocated as well as the BSP let SMIs
763 * start flowing. */
764 southbridge_smm_enable_smi();
765
766 /* Lock down the SMRAM space. */
767 smm_lock();
768}
769
770static const struct mp_ops mp_ops = {
771 .pre_mp_init = pre_mp_init,
772 .get_cpu_count = get_cpu_count,
773 .get_smm_info = smm_info,
774 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500775 .pre_mp_smm_init = smm_initialize,
776 .per_cpu_smm_trigger = per_cpu_smm_trigger,
777 .relocation_handler = smm_relocation_handler,
778 .post_mp_init = post_mp_init,
779};
780
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300781void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500782{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700783 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500784 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500785}
786
787static struct device_operations cpu_dev_ops = {
788 .init = haswell_init,
789};
790
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100791static const struct cpu_device_id cpu_table[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -0500792 { X86_VENDOR_INTEL, 0x306c1 }, /* Intel Haswell 4+2 A0 */
793 { X86_VENDOR_INTEL, 0x306c2 }, /* Intel Haswell 4+2 B0 */
Tristan Corrick22f97002018-10-31 02:22:39 +1300794 { X86_VENDOR_INTEL, 0x306c3 }, /* Intel Haswell C0 */
Duncan Laurie512540492012-12-17 11:24:45 -0800795 { X86_VENDOR_INTEL, 0x40650 }, /* Intel Haswell ULT B0 */
796 { X86_VENDOR_INTEL, 0x40651 }, /* Intel Haswell ULT B1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500797 { 0, 0 },
798};
799
800static const struct cpu_driver driver __cpu_driver = {
801 .ops = &cpu_dev_ops,
802 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500803 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500804};