blob: 74147ab6dd790ff9f9dcd18b4ebd01e2be9c4ae3 [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 <string.h>
21#include <arch/acpi.h>
22#include <cpu/cpu.h>
23#include <cpu/x86/mtrr.h>
24#include <cpu/x86/msr.h>
Aaron Durbin014baea2014-03-28 22:01:05 -050025#include <cpu/x86/mp.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050026#include <cpu/x86/lapic.h>
27#include <cpu/intel/microcode.h>
28#include <cpu/intel/speedstep.h>
29#include <cpu/intel/turbo.h>
30#include <cpu/x86/cache.h>
31#include <cpu/x86/name.h>
Aaron Durbin6a360042014-02-13 10:30:42 -060032#include <cpu/x86/smm.h>
Aaron Durbinf24262d2013-04-10 14:59:21 -050033#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050034#include <pc80/mc146818rtc.h>
Aaron Durbin7c351312013-04-10 14:46:25 -050035#include <northbridge/intel/haswell/haswell.h>
36#include <southbridge/intel/lynxpoint/pch.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050037#include "haswell.h"
38#include "chip.h"
39
Aaron Durbin7c351312013-04-10 14:46:25 -050040/* Intel suggested latency times in units of 1024ns. */
41#define C_STATE_LATENCY_CONTROL_0_LIMIT 0x42
42#define C_STATE_LATENCY_CONTROL_1_LIMIT 0x73
43#define C_STATE_LATENCY_CONTROL_2_LIMIT 0x91
44#define C_STATE_LATENCY_CONTROL_3_LIMIT 0xe4
45#define C_STATE_LATENCY_CONTROL_4_LIMIT 0x145
46#define C_STATE_LATENCY_CONTROL_5_LIMIT 0x1ef
47
48#define C_STATE_LATENCY_MICRO_SECONDS(limit, base) \
49 (((1 << ((base)*5)) * (limit)) / 1000)
50#define C_STATE_LATENCY_FROM_LAT_REG(reg) \
51 C_STATE_LATENCY_MICRO_SECONDS(C_STATE_LATENCY_CONTROL_ ##reg## _LIMIT, \
52 (IRTL_1024_NS >> 10))
53
Aaron Durbin76c37002012-10-30 09:03:43 -050054/*
Aaron Durbin7c351312013-04-10 14:46:25 -050055 * List of supported C-states in this processor. Only the ULT parts support C8,
56 * C9, and C10.
Aaron Durbin76c37002012-10-30 09:03:43 -050057 */
Aaron Durbin7c351312013-04-10 14:46:25 -050058enum {
59 C_STATE_C0, /* 0 */
60 C_STATE_C1, /* 1 */
61 C_STATE_C1E, /* 2 */
62 C_STATE_C3, /* 3 */
63 C_STATE_C6_SHORT_LAT, /* 4 */
64 C_STATE_C6_LONG_LAT, /* 5 */
65 C_STATE_C7_SHORT_LAT, /* 6 */
66 C_STATE_C7_LONG_LAT, /* 7 */
67 C_STATE_C7S_SHORT_LAT, /* 8 */
68 C_STATE_C7S_LONG_LAT, /* 9 */
69 C_STATE_C8, /* 10 */
70 C_STATE_C9, /* 11 */
71 C_STATE_C10, /* 12 */
72 NUM_C_STATES
Aaron Durbin76c37002012-10-30 09:03:43 -050073};
Aaron Durbin7c351312013-04-10 14:46:25 -050074
75#define MWAIT_RES(state, sub_state) \
76 { \
77 .addrl = (((state) << 4) | (sub_state)), \
78 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
79 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
80 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
81 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
82 }
83
84static acpi_cstate_t cstate_map[NUM_C_STATES] = {
85 [C_STATE_C0] = { },
86 [C_STATE_C1] = {
87 .latency = 0,
88 .power = 1000,
89 .resource = MWAIT_RES(0,0),
90 },
91 [C_STATE_C1E] = {
92 .latency = 0,
93 .power = 1000,
94 .resource = MWAIT_RES(0,1),
95 },
96 [C_STATE_C3] = {
97 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
98 .power = 900,
99 .resource = MWAIT_RES(1, 0),
100 },
101 [C_STATE_C6_SHORT_LAT] = {
102 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
103 .power = 800,
104 .resource = MWAIT_RES(2, 0),
105 },
106 [C_STATE_C6_LONG_LAT] = {
107 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
108 .power = 800,
109 .resource = MWAIT_RES(2, 1),
110 },
111 [C_STATE_C7_SHORT_LAT] = {
112 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
113 .power = 700,
114 .resource = MWAIT_RES(3, 0),
115 },
116 [C_STATE_C7_LONG_LAT] = {
117 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
118 .power = 700,
119 .resource = MWAIT_RES(3, 1),
120 },
121 [C_STATE_C7S_SHORT_LAT] = {
122 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
123 .power = 700,
124 .resource = MWAIT_RES(3, 2),
125 },
126 [C_STATE_C7S_LONG_LAT] = {
127 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
128 .power = 700,
129 .resource = MWAIT_RES(3, 3),
130 },
131 [C_STATE_C8] = {
132 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
133 .power = 600,
134 .resource = MWAIT_RES(4, 0),
135 },
136 [C_STATE_C9] = {
137 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
138 .power = 500,
139 .resource = MWAIT_RES(5, 0),
140 },
141 [C_STATE_C10] = {
142 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
143 .power = 400,
144 .resource = MWAIT_RES(6, 0),
145 },
146};
Aaron Durbin76c37002012-10-30 09:03:43 -0500147
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500148static void enable_vmx(void)
149{
150 struct cpuid_result regs;
151 msr_t msr;
152 int enable = IS_ENABLED(CONFIG_ENABLE_VMX);
153
154 regs = cpuid(1);
155 /* Check that the VMX is supported before reading or writing the MSR. */
156 if (!((regs.ecx & CPUID_VMX) || (regs.ecx & CPUID_SMX)))
157 return;
158
159 msr = rdmsr(IA32_FEATURE_CONTROL);
160
161 if (msr.lo & (1 << 0)) {
162 printk(BIOS_ERR, "VMX is locked, so %s will do nothing\n", __func__);
163 /* VMX locked. If we set it again we get an illegal
164 * instruction
165 */
166 return;
167 }
168
169 /* The IA32_FEATURE_CONTROL MSR may initialize with random values.
170 * It must be cleared regardless of VMX config setting.
171 */
172 msr.hi = msr.lo = 0;
173
174 printk(BIOS_DEBUG, "%s VMX\n", enable ? "Enabling" : "Disabling");
175
176 if (enable) {
177 msr.lo |= (1 << 2);
178 if (regs.ecx & CPUID_SMX)
179 msr.lo |= (1 << 1);
180 }
181
182 wrmsr(IA32_FEATURE_CONTROL, msr);
183
184 msr.lo |= (1 << 0); /* Set lock bit */
185
186 wrmsr(IA32_FEATURE_CONTROL, msr);
187}
188
Aaron Durbin76c37002012-10-30 09:03:43 -0500189/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
190static const u8 power_limit_time_sec_to_msr[] = {
191 [0] = 0x00,
192 [1] = 0x0a,
193 [2] = 0x0b,
194 [3] = 0x4b,
195 [4] = 0x0c,
196 [5] = 0x2c,
197 [6] = 0x4c,
198 [7] = 0x6c,
199 [8] = 0x0d,
200 [10] = 0x2d,
201 [12] = 0x4d,
202 [14] = 0x6d,
203 [16] = 0x0e,
204 [20] = 0x2e,
205 [24] = 0x4e,
206 [28] = 0x6e,
207 [32] = 0x0f,
208 [40] = 0x2f,
209 [48] = 0x4f,
210 [56] = 0x6f,
211 [64] = 0x10,
212 [80] = 0x30,
213 [96] = 0x50,
214 [112] = 0x70,
215 [128] = 0x11,
216};
217
218/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
219static const u8 power_limit_time_msr_to_sec[] = {
220 [0x00] = 0,
221 [0x0a] = 1,
222 [0x0b] = 2,
223 [0x4b] = 3,
224 [0x0c] = 4,
225 [0x2c] = 5,
226 [0x4c] = 6,
227 [0x6c] = 7,
228 [0x0d] = 8,
229 [0x2d] = 10,
230 [0x4d] = 12,
231 [0x6d] = 14,
232 [0x0e] = 16,
233 [0x2e] = 20,
234 [0x4e] = 24,
235 [0x6e] = 28,
236 [0x0f] = 32,
237 [0x2f] = 40,
238 [0x4f] = 48,
239 [0x6f] = 56,
240 [0x10] = 64,
241 [0x30] = 80,
242 [0x50] = 96,
243 [0x70] = 112,
244 [0x11] = 128,
245};
246
Duncan Laurie118d1052013-07-09 15:34:25 -0700247int haswell_family_model(void)
248{
249 return cpuid_eax(1) & 0x0fff0ff0;
250}
251
252int haswell_stepping(void)
253{
254 return cpuid_eax(1) & 0xf;
255}
256
Aaron Durbin7c351312013-04-10 14:46:25 -0500257/* Dynamically determine if the part is ULT. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700258int haswell_is_ult(void)
Aaron Durbin7c351312013-04-10 14:46:25 -0500259{
260 static int ult = -1;
261
262 if (ult < 0)
Duncan Laurie118d1052013-07-09 15:34:25 -0700263 ult = !!(haswell_family_model() == HASWELL_FAMILY_ULT);
Aaron Durbin7c351312013-04-10 14:46:25 -0500264
265 return ult;
266}
267
Aaron Durbinf24262d2013-04-10 14:59:21 -0500268/* The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
269 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
270 * when a core is woken up. */
271static int pcode_ready(void)
272{
273 int wait_count;
274 const int delay_step = 10;
275
276 wait_count = 0;
277 do {
278 if (!(MCHBAR32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
279 return 0;
280 wait_count += delay_step;
281 udelay(delay_step);
282 } while (wait_count < 1000);
283
284 return -1;
285}
286
287static void calibrate_24mhz_bclk(void)
288{
289 int err_code;
290
291 if (pcode_ready() < 0) {
292 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
293 return;
294 }
295
296 /* A non-zero value initiates the PCODE calibration. */
297 MCHBAR32(BIOS_MAILBOX_DATA) = ~0;
298 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
299 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL;
300
301 if (pcode_ready() < 0) {
302 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
303 return;
304 }
305
306 err_code = MCHBAR32(BIOS_MAILBOX_INTERFACE) & 0xff;
307
308 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration response: %d\n",
309 err_code);
310
311 /* Read the calibrated value. */
312 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
313 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION;
314
315 if (pcode_ready() < 0) {
316 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
317 return;
318 }
319
320 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration value: 0x%08x\n",
321 MCHBAR32(BIOS_MAILBOX_DATA));
322}
323
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700324static u32 pcode_mailbox_read(u32 command)
325{
326 if (pcode_ready() < 0) {
327 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
328 return 0;
329 }
330
331 /* Send command and start transaction */
332 MCHBAR32(BIOS_MAILBOX_INTERFACE) = command | MAILBOX_RUN_BUSY;
333
334 if (pcode_ready() < 0) {
335 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
336 return 0;
337 }
338
339 /* Read mailbox */
340 return MCHBAR32(BIOS_MAILBOX_DATA);
341}
342
Aaron Durbin16cbf892013-07-03 16:21:28 -0500343static void initialize_vr_config(void)
344{
345 msr_t msr;
346
347 printk(BIOS_DEBUG, "Initializing VR config.\n");
348
349 /* Configure VR_CURRENT_CONFIG. */
350 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
351 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
352 * on ULT systems. */
353 msr.hi &= 0xc0000000;
354 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
355 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
356 msr.hi |= (0x0f << (32 - 32)); /* PSI1 threshold - 15A. */
357
Duncan Laurie118d1052013-07-09 15:34:25 -0700358 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500359 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
360 /* Leave the max instantaneous current limit (12:0) to default. */
361 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
362
363 /* Configure VR_MISC_CONFIG MSR. */
364 msr = rdmsr(MSR_VR_MISC_CONFIG);
365 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
366 msr.hi &= ~(0x3ff << (40 - 32));
367 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
368 /* Set IOUT_OFFSET to 0. */
369 msr.hi &= ~0xff;
370 /* Set exit ramp rate to fast. */
371 msr.hi |= (1 << (50 - 32));
372 /* Set entry ramp rate to slow. */
373 msr.hi &= ~(1 << (51 - 32));
374 /* Enable decay mode on C-state entry. */
375 msr.hi |= (1 << (52 - 32));
376 /* Set the slow ramp rate to be fast ramp rate / 4 */
377 msr.hi &= ~(0x3 << (53 - 32));
378 msr.hi |= (0x01 << (53 - 32));
379 /* Set MIN_VID (31:24) to allow CPU to have full control. */
380 msr.lo &= ~0xff000000;
381 wrmsr(MSR_VR_MISC_CONFIG, msr);
382
383 /* Configure VR_MISC_CONFIG2 MSR. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700384 if (haswell_is_ult()) {
Aaron Durbin16cbf892013-07-03 16:21:28 -0500385 msr = rdmsr(MSR_VR_MISC_CONFIG2);
386 msr.lo &= ~0xffff;
387 /* Allow CPU to control minimum voltage completely (15:8) and
388 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
389 msr.lo |= 0x006f;
390 wrmsr(MSR_VR_MISC_CONFIG2, msr);
391 }
392}
393
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700394static void configure_pch_power_sharing(void)
395{
396 u32 pch_power, pch_power_ext, pmsync, pmsync2;
397 int i;
398
399 /* Read PCH Power levels from PCODE */
400 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
401 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
402
403 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
404 pch_power, pch_power_ext);
405
406 pmsync = RCBA32(PMSYNC_CONFIG);
407 pmsync2 = RCBA32(PMSYNC_CONFIG2);
408
409 /* Program PMSYNC_TPR_CONFIG PCH power limit values
410 * pmsync[0:4] = mailbox[0:5]
411 * pmsync[8:12] = mailbox[6:11]
412 * pmsync[16:20] = mailbox[12:17]
413 */
414 for (i = 0; i < 3; i++) {
415 u32 level = pch_power & 0x3f;
416 pch_power >>= 6;
417 pmsync &= ~(0x1f << (i * 8));
418 pmsync |= (level & 0x1f) << (i * 8);
419 }
420 RCBA32(PMSYNC_CONFIG) = pmsync;
421
422 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
423 * pmsync2[0:4] = mailbox[23:18]
424 * pmsync2[8:12] = mailbox_ext[6:11]
425 * pmsync2[16:20] = mailbox_ext[12:17]
426 * pmsync2[24:28] = mailbox_ext[18:22]
427 */
428 pmsync2 &= ~0x1f;
429 pmsync2 |= pch_power & 0x1f;
430
431 for (i = 1; i < 4; i++) {
432 u32 level = pch_power_ext & 0x3f;
433 pch_power_ext >>= 6;
434 pmsync2 &= ~(0x1f << (i * 8));
435 pmsync2 |= (level & 0x1f) << (i * 8);
436 }
437 RCBA32(PMSYNC_CONFIG2) = pmsync2;
438}
439
Aaron Durbin76c37002012-10-30 09:03:43 -0500440int cpu_config_tdp_levels(void)
441{
442 msr_t platform_info;
443
444 /* Bits 34:33 indicate how many levels supported */
445 platform_info = rdmsr(MSR_PLATFORM_INFO);
446 return (platform_info.hi >> 1) & 3;
447}
448
449/*
450 * Configure processor power limits if possible
451 * This must be done AFTER set of BIOS_RESET_CPL
452 */
453void set_power_limits(u8 power_limit_1_time)
454{
455 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
456 msr_t limit;
457 unsigned power_unit;
458 unsigned tdp, min_power, max_power, max_time;
459 u8 power_limit_1_val;
460
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000461 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
462 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500463
464 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
465 return;
466
467 /* Get units */
468 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
469 power_unit = 2 << ((msr.lo & 0xf) - 1);
470
471 /* Get power defaults for this SKU */
472 msr = rdmsr(MSR_PKG_POWER_SKU);
473 tdp = msr.lo & 0x7fff;
474 min_power = (msr.lo >> 16) & 0x7fff;
475 max_power = msr.hi & 0x7fff;
476 max_time = (msr.hi >> 16) & 0x7f;
477
478 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
479
480 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
481 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
482
483 if (min_power > 0 && tdp < min_power)
484 tdp = min_power;
485
486 if (max_power > 0 && tdp > max_power)
487 tdp = max_power;
488
489 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
490
491 /* Set long term power limit to TDP */
492 limit.lo = 0;
493 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
494 limit.lo |= PKG_POWER_LIMIT_EN;
495 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
496 PKG_POWER_LIMIT_TIME_SHIFT;
497
498 /* Set short term power limit to 1.25 * TDP */
499 limit.hi = 0;
500 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
501 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700502 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500503
504 wrmsr(MSR_PKG_POWER_LIMIT, limit);
505
Duncan Lauriec70353f2013-06-28 14:40:38 -0700506 /* Set power limit values in MCHBAR as well */
507 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
508 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
509
510 /* Set DDR RAPL power limit by copying from MMIO to MSR */
511 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
512 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
513 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
514
Aaron Durbin76c37002012-10-30 09:03:43 -0500515 /* Use nominal TDP values for CPUs with configurable TDP */
516 if (cpu_config_tdp_levels()) {
517 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
518 limit.hi = 0;
519 limit.lo = msr.lo & 0xff;
520 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
521 }
522}
523
Aaron Durbin76c37002012-10-30 09:03:43 -0500524static void configure_c_states(void)
525{
526 msr_t msr;
527
528 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500529 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
530 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500531 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
532 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
533 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
534 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
535 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700536 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500537 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
538
539 msr = rdmsr(MSR_PMG_IO_CAPTURE_BASE);
Aaron Durbin7c351312013-04-10 14:46:25 -0500540 msr.lo &= ~0xffff;
541 msr.lo |= (get_pmbase() + 0x14); // LVL_2 base address
542 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500543 wrmsr(MSR_PMG_IO_CAPTURE_BASE, msr);
544
545 msr = rdmsr(MSR_MISC_PWR_MGMT);
546 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
547 wrmsr(MSR_MISC_PWR_MGMT, msr);
548
549 msr = rdmsr(MSR_POWER_CTL);
550 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
551 msr.lo |= (1 << 1); // C1E Enable
552 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
553 wrmsr(MSR_POWER_CTL, msr);
554
Aaron Durbin7c351312013-04-10 14:46:25 -0500555 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500556 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500557 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
558 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500559
Aaron Durbin7c351312013-04-10 14:46:25 -0500560 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500561 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500562 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
563 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500564
Aaron Durbin7c351312013-04-10 14:46:25 -0500565 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500566 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500567 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
568 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500569
Aaron Durbin7c351312013-04-10 14:46:25 -0500570 /* Haswell ULT only supoprts the 3-5 latency response registers.*/
Duncan Laurie118d1052013-07-09 15:34:25 -0700571 if (haswell_is_ult()) {
Aaron Durbin7c351312013-04-10 14:46:25 -0500572 /* C-state Interrupt Response Latency Control 3 - package C8 */
573 msr.hi = 0;
574 msr.lo = IRTL_VALID | IRTL_1024_NS |
575 C_STATE_LATENCY_CONTROL_3_LIMIT;
576 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500577
Aaron Durbin7c351312013-04-10 14:46:25 -0500578 /* C-state Interrupt Response Latency Control 4 - package C9 */
579 msr.hi = 0;
580 msr.lo = IRTL_VALID | IRTL_1024_NS |
581 C_STATE_LATENCY_CONTROL_4_LIMIT;
582 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
583
584 /* C-state Interrupt Response Latency Control 5 - package C10 */
585 msr.hi = 0;
586 msr.lo = IRTL_VALID | IRTL_1024_NS |
587 C_STATE_LATENCY_CONTROL_5_LIMIT;
588 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
589 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500590}
Aaron Durbin76c37002012-10-30 09:03:43 -0500591
592static void configure_thermal_target(void)
593{
594 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100595 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500596 msr_t msr;
597
598 /* Find pointer to CPU configuration */
599 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
600 if (!lapic || !lapic->chip_info)
601 return;
602 conf = lapic->chip_info;
603
Martin Roth4c3ab732013-07-08 16:23:54 -0600604 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500605 msr = rdmsr(MSR_PLATFORM_INFO);
606 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
607 msr = rdmsr(MSR_TEMPERATURE_TARGET);
608 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
609 msr.lo |= (conf->tcc_offset & 0xf) << 24;
610 wrmsr(MSR_TEMPERATURE_TARGET, msr);
611 }
612}
613
614static void configure_misc(void)
615{
616 msr_t msr;
617
618 msr = rdmsr(IA32_MISC_ENABLE);
619 msr.lo |= (1 << 0); /* Fast String enable */
620 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
621 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
622 wrmsr(IA32_MISC_ENABLE, msr);
623
624 /* Disable Thermal interrupts */
625 msr.lo = 0;
626 msr.hi = 0;
627 wrmsr(IA32_THERM_INTERRUPT, msr);
628
629 /* Enable package critical interrupt only */
630 msr.lo = 1 << 4;
631 msr.hi = 0;
632 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
633}
634
635static void enable_lapic_tpr(void)
636{
637 msr_t msr;
638
639 msr = rdmsr(MSR_PIC_MSG_CONTROL);
640 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
641 wrmsr(MSR_PIC_MSG_CONTROL, msr);
642}
643
644static void configure_dca_cap(void)
645{
646 struct cpuid_result cpuid_regs;
647 msr_t msr;
648
649 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
650 cpuid_regs = cpuid(1);
651 if (cpuid_regs.ecx & (1 << 18)) {
652 msr = rdmsr(IA32_PLATFORM_DCA_CAP);
653 msr.lo |= 1;
654 wrmsr(IA32_PLATFORM_DCA_CAP, msr);
655 }
656}
657
658static void set_max_ratio(void)
659{
660 msr_t msr, perf_ctl;
661
662 perf_ctl.hi = 0;
663
664 /* Check for configurable TDP option */
665 if (cpu_config_tdp_levels()) {
666 /* Set to nominal TDP ratio */
667 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
668 perf_ctl.lo = (msr.lo & 0xff) << 8;
669 } else {
670 /* Platform Info bits 15:8 give max ratio */
671 msr = rdmsr(MSR_PLATFORM_INFO);
672 perf_ctl.lo = msr.lo & 0xff00;
673 }
674 wrmsr(IA32_PERF_CTL, perf_ctl);
675
676 printk(BIOS_DEBUG, "haswell: frequency set to %d\n",
677 ((perf_ctl.lo >> 8) & 0xff) * HASWELL_BCLK);
678}
679
680static void set_energy_perf_bias(u8 policy)
681{
682 msr_t msr;
Aaron Durbindc278f82012-12-11 17:15:13 -0600683 int ecx;
684
685 /* Determine if energy efficient policy is supported. */
686 ecx = cpuid_ecx(0x6);
687 if (!(ecx & (1 << 3)))
688 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500689
690 /* Energy Policy is bits 3:0 */
691 msr = rdmsr(IA32_ENERGY_PERFORMANCE_BIAS);
692 msr.lo &= ~0xf;
693 msr.lo |= policy & 0xf;
694 wrmsr(IA32_ENERGY_PERFORMANCE_BIAS, msr);
695
696 printk(BIOS_DEBUG, "haswell: energy policy set to %u\n",
697 policy);
698}
699
700static void configure_mca(void)
701{
702 msr_t msr;
Aaron Durbin24614af2013-01-12 01:07:28 -0600703 const unsigned int mcg_cap_msr = 0x179;
Aaron Durbin76c37002012-10-30 09:03:43 -0500704 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600705 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500706
Aaron Durbin24614af2013-01-12 01:07:28 -0600707 msr = rdmsr(mcg_cap_msr);
708 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500709 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600710 /* TODO(adurbin): This should only be done on a cold boot. Also, some
711 * of these banks are core vs package scope. For now every CPU clears
712 * every bank. */
713 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500714 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
715}
716
Aaron Durbin305b1f02013-01-15 08:27:05 -0600717/* All CPUs including BSP will run the following function. */
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100718static void haswell_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600719{
720 /* Clear out pending MCEs */
721 configure_mca();
722
Aaron Durbin76c37002012-10-30 09:03:43 -0500723 /* Enable the local cpu apics */
724 enable_lapic_tpr();
725 setup_lapic();
726
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500727 /* Enable virtualization if Kconfig option is set */
728 enable_vmx();
729
Aaron Durbin76c37002012-10-30 09:03:43 -0500730 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500731 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500732
733 /* Configure Enhanced SpeedStep and Thermal Sensors */
734 configure_misc();
735
736 /* Thermal throttle activation offset */
737 configure_thermal_target();
738
739 /* Enable Direct Cache Access */
740 configure_dca_cap();
741
742 /* Set energy policy */
743 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
744
745 /* Set Max Ratio */
746 set_max_ratio();
747
748 /* Enable Turbo */
749 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600750}
Aaron Durbin76c37002012-10-30 09:03:43 -0500751
Aaron Durbin014baea2014-03-28 22:01:05 -0500752/* MP initialization support. */
753static const void *microcode_patch;
Aaron Durbin463af332016-05-03 17:26:35 -0500754static int ht_disabled;
Aaron Durbin014baea2014-03-28 22:01:05 -0500755
Aaron Durbin463af332016-05-03 17:26:35 -0500756static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500757{
Aaron Durbin463af332016-05-03 17:26:35 -0500758 /* Setup MTRRs based on physical address size. */
759 x86_setup_mtrrs_with_detect();
760 x86_mtrr_check();
761
762 initialize_vr_config();
763
764 if (haswell_is_ult()) {
765 calibrate_24mhz_bclk();
766 configure_pch_power_sharing();
767 }
Aaron Durbin014baea2014-03-28 22:01:05 -0500768}
769
Aaron Durbin463af332016-05-03 17:26:35 -0500770static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500771{
Aaron Durbin463af332016-05-03 17:26:35 -0500772 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500773 int num_threads;
774 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500775
776 msr = rdmsr(CORE_THREAD_COUNT_MSR);
777 num_threads = (msr.lo >> 0) & 0xffff;
778 num_cores = (msr.lo >> 16) & 0xffff;
779 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
780 num_cores, num_threads);
781
782 ht_disabled = num_threads == num_cores;
Aaron Durbin305b1f02013-01-15 08:27:05 -0600783
Aaron Durbin463af332016-05-03 17:26:35 -0500784 return num_threads;
785}
Aaron Durbin7af20692013-01-14 14:54:41 -0600786
Aaron Durbin463af332016-05-03 17:26:35 -0500787static void get_microcode_info(const void **microcode, int *parallel)
788{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600789 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500790 *microcode = microcode_patch;
791 *parallel = 1;
792}
Aaron Durbin7af20692013-01-14 14:54:41 -0600793
Aaron Durbin463af332016-05-03 17:26:35 -0500794static int adjust_apic_id(int index, int apic_id)
795{
Aaron Durbin014baea2014-03-28 22:01:05 -0500796 if (ht_disabled)
Aaron Durbin463af332016-05-03 17:26:35 -0500797 return 2 * index;
Aaron Durbin014baea2014-03-28 22:01:05 -0500798 else
Aaron Durbin463af332016-05-03 17:26:35 -0500799 return index;
800}
Aaron Durbin305b1f02013-01-15 08:27:05 -0600801
Aaron Durbin463af332016-05-03 17:26:35 -0500802static void per_cpu_smm_trigger(void)
803{
804 /* Relocate the SMM handler. */
805 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600806
Aaron Durbin463af332016-05-03 17:26:35 -0500807 /* After SMM relocation a 2nd microcode load is required. */
808 intel_microcode_load_unlocked(microcode_patch);
809}
810
811static void post_mp_init(void)
812{
813 /* Now that all APs have been relocated as well as the BSP let SMIs
814 * start flowing. */
815 southbridge_smm_enable_smi();
816
817 /* Lock down the SMRAM space. */
818 smm_lock();
819}
820
821static const struct mp_ops mp_ops = {
822 .pre_mp_init = pre_mp_init,
823 .get_cpu_count = get_cpu_count,
824 .get_smm_info = smm_info,
825 .get_microcode_info = get_microcode_info,
826 .adjust_cpu_apic_entry = adjust_apic_id,
827 .pre_mp_smm_init = smm_initialize,
828 .per_cpu_smm_trigger = per_cpu_smm_trigger,
829 .relocation_handler = smm_relocation_handler,
830 .post_mp_init = post_mp_init,
831};
832
833void bsp_init_and_start_aps(struct bus *cpu_bus)
834{
835 if (mp_init_with_smm(cpu_bus, &mp_ops)) {
Aaron Durbin014baea2014-03-28 22:01:05 -0500836 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin305b1f02013-01-15 08:27:05 -0600837 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500838}
839
840static struct device_operations cpu_dev_ops = {
841 .init = haswell_init,
842};
843
844static struct cpu_device_id cpu_table[] = {
845 { X86_VENDOR_INTEL, 0x306c1 }, /* Intel Haswell 4+2 A0 */
846 { X86_VENDOR_INTEL, 0x306c2 }, /* Intel Haswell 4+2 B0 */
Duncan Laurie512540492012-12-17 11:24:45 -0800847 { X86_VENDOR_INTEL, 0x40650 }, /* Intel Haswell ULT B0 */
848 { X86_VENDOR_INTEL, 0x40651 }, /* Intel Haswell ULT B1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500849 { 0, 0 },
850};
851
852static const struct cpu_driver driver __cpu_driver = {
853 .ops = &cpu_dev_ops,
854 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500855 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500856};