blob: 68c76431b76cbaccda2c95e3c0999e6edcff5c19 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
22
23#include <console/console.h>
24#include <device/device.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050025#include <string.h>
26#include <arch/acpi.h>
27#include <cpu/cpu.h>
28#include <cpu/x86/mtrr.h>
29#include <cpu/x86/msr.h>
Aaron Durbin014baea2014-03-28 22:01:05 -050030#include <cpu/x86/mp.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050031#include <cpu/x86/lapic.h>
32#include <cpu/intel/microcode.h>
33#include <cpu/intel/speedstep.h>
34#include <cpu/intel/turbo.h>
35#include <cpu/x86/cache.h>
36#include <cpu/x86/name.h>
Aaron Durbin6a360042014-02-13 10:30:42 -060037#include <cpu/x86/smm.h>
Aaron Durbinf24262d2013-04-10 14:59:21 -050038#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050039#include <pc80/mc146818rtc.h>
Aaron Durbin7c351312013-04-10 14:46:25 -050040#include <northbridge/intel/haswell/haswell.h>
41#include <southbridge/intel/lynxpoint/pch.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050042#include "haswell.h"
43#include "chip.h"
44
Aaron Durbin7c351312013-04-10 14:46:25 -050045/* Intel suggested latency times in units of 1024ns. */
46#define C_STATE_LATENCY_CONTROL_0_LIMIT 0x42
47#define C_STATE_LATENCY_CONTROL_1_LIMIT 0x73
48#define C_STATE_LATENCY_CONTROL_2_LIMIT 0x91
49#define C_STATE_LATENCY_CONTROL_3_LIMIT 0xe4
50#define C_STATE_LATENCY_CONTROL_4_LIMIT 0x145
51#define C_STATE_LATENCY_CONTROL_5_LIMIT 0x1ef
52
53#define C_STATE_LATENCY_MICRO_SECONDS(limit, base) \
54 (((1 << ((base)*5)) * (limit)) / 1000)
55#define C_STATE_LATENCY_FROM_LAT_REG(reg) \
56 C_STATE_LATENCY_MICRO_SECONDS(C_STATE_LATENCY_CONTROL_ ##reg## _LIMIT, \
57 (IRTL_1024_NS >> 10))
58
Aaron Durbin76c37002012-10-30 09:03:43 -050059/*
Aaron Durbin7c351312013-04-10 14:46:25 -050060 * List of supported C-states in this processor. Only the ULT parts support C8,
61 * C9, and C10.
Aaron Durbin76c37002012-10-30 09:03:43 -050062 */
Aaron Durbin7c351312013-04-10 14:46:25 -050063enum {
64 C_STATE_C0, /* 0 */
65 C_STATE_C1, /* 1 */
66 C_STATE_C1E, /* 2 */
67 C_STATE_C3, /* 3 */
68 C_STATE_C6_SHORT_LAT, /* 4 */
69 C_STATE_C6_LONG_LAT, /* 5 */
70 C_STATE_C7_SHORT_LAT, /* 6 */
71 C_STATE_C7_LONG_LAT, /* 7 */
72 C_STATE_C7S_SHORT_LAT, /* 8 */
73 C_STATE_C7S_LONG_LAT, /* 9 */
74 C_STATE_C8, /* 10 */
75 C_STATE_C9, /* 11 */
76 C_STATE_C10, /* 12 */
77 NUM_C_STATES
Aaron Durbin76c37002012-10-30 09:03:43 -050078};
Aaron Durbin7c351312013-04-10 14:46:25 -050079
80#define MWAIT_RES(state, sub_state) \
81 { \
82 .addrl = (((state) << 4) | (sub_state)), \
83 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
84 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
85 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
86 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
87 }
88
89static acpi_cstate_t cstate_map[NUM_C_STATES] = {
90 [C_STATE_C0] = { },
91 [C_STATE_C1] = {
92 .latency = 0,
93 .power = 1000,
94 .resource = MWAIT_RES(0,0),
95 },
96 [C_STATE_C1E] = {
97 .latency = 0,
98 .power = 1000,
99 .resource = MWAIT_RES(0,1),
100 },
101 [C_STATE_C3] = {
102 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
103 .power = 900,
104 .resource = MWAIT_RES(1, 0),
105 },
106 [C_STATE_C6_SHORT_LAT] = {
107 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
108 .power = 800,
109 .resource = MWAIT_RES(2, 0),
110 },
111 [C_STATE_C6_LONG_LAT] = {
112 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
113 .power = 800,
114 .resource = MWAIT_RES(2, 1),
115 },
116 [C_STATE_C7_SHORT_LAT] = {
117 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
118 .power = 700,
119 .resource = MWAIT_RES(3, 0),
120 },
121 [C_STATE_C7_LONG_LAT] = {
122 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
123 .power = 700,
124 .resource = MWAIT_RES(3, 1),
125 },
126 [C_STATE_C7S_SHORT_LAT] = {
127 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
128 .power = 700,
129 .resource = MWAIT_RES(3, 2),
130 },
131 [C_STATE_C7S_LONG_LAT] = {
132 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
133 .power = 700,
134 .resource = MWAIT_RES(3, 3),
135 },
136 [C_STATE_C8] = {
137 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
138 .power = 600,
139 .resource = MWAIT_RES(4, 0),
140 },
141 [C_STATE_C9] = {
142 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
143 .power = 500,
144 .resource = MWAIT_RES(5, 0),
145 },
146 [C_STATE_C10] = {
147 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
148 .power = 400,
149 .resource = MWAIT_RES(6, 0),
150 },
151};
Aaron Durbin76c37002012-10-30 09:03:43 -0500152
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500153static void enable_vmx(void)
154{
155 struct cpuid_result regs;
156 msr_t msr;
157 int enable = IS_ENABLED(CONFIG_ENABLE_VMX);
158
159 regs = cpuid(1);
160 /* Check that the VMX is supported before reading or writing the MSR. */
161 if (!((regs.ecx & CPUID_VMX) || (regs.ecx & CPUID_SMX)))
162 return;
163
164 msr = rdmsr(IA32_FEATURE_CONTROL);
165
166 if (msr.lo & (1 << 0)) {
167 printk(BIOS_ERR, "VMX is locked, so %s will do nothing\n", __func__);
168 /* VMX locked. If we set it again we get an illegal
169 * instruction
170 */
171 return;
172 }
173
174 /* The IA32_FEATURE_CONTROL MSR may initialize with random values.
175 * It must be cleared regardless of VMX config setting.
176 */
177 msr.hi = msr.lo = 0;
178
179 printk(BIOS_DEBUG, "%s VMX\n", enable ? "Enabling" : "Disabling");
180
181 if (enable) {
182 msr.lo |= (1 << 2);
183 if (regs.ecx & CPUID_SMX)
184 msr.lo |= (1 << 1);
185 }
186
187 wrmsr(IA32_FEATURE_CONTROL, msr);
188
189 msr.lo |= (1 << 0); /* Set lock bit */
190
191 wrmsr(IA32_FEATURE_CONTROL, msr);
192}
193
Aaron Durbin76c37002012-10-30 09:03:43 -0500194/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
195static const u8 power_limit_time_sec_to_msr[] = {
196 [0] = 0x00,
197 [1] = 0x0a,
198 [2] = 0x0b,
199 [3] = 0x4b,
200 [4] = 0x0c,
201 [5] = 0x2c,
202 [6] = 0x4c,
203 [7] = 0x6c,
204 [8] = 0x0d,
205 [10] = 0x2d,
206 [12] = 0x4d,
207 [14] = 0x6d,
208 [16] = 0x0e,
209 [20] = 0x2e,
210 [24] = 0x4e,
211 [28] = 0x6e,
212 [32] = 0x0f,
213 [40] = 0x2f,
214 [48] = 0x4f,
215 [56] = 0x6f,
216 [64] = 0x10,
217 [80] = 0x30,
218 [96] = 0x50,
219 [112] = 0x70,
220 [128] = 0x11,
221};
222
223/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
224static const u8 power_limit_time_msr_to_sec[] = {
225 [0x00] = 0,
226 [0x0a] = 1,
227 [0x0b] = 2,
228 [0x4b] = 3,
229 [0x0c] = 4,
230 [0x2c] = 5,
231 [0x4c] = 6,
232 [0x6c] = 7,
233 [0x0d] = 8,
234 [0x2d] = 10,
235 [0x4d] = 12,
236 [0x6d] = 14,
237 [0x0e] = 16,
238 [0x2e] = 20,
239 [0x4e] = 24,
240 [0x6e] = 28,
241 [0x0f] = 32,
242 [0x2f] = 40,
243 [0x4f] = 48,
244 [0x6f] = 56,
245 [0x10] = 64,
246 [0x30] = 80,
247 [0x50] = 96,
248 [0x70] = 112,
249 [0x11] = 128,
250};
251
Duncan Laurie118d1052013-07-09 15:34:25 -0700252int haswell_family_model(void)
253{
254 return cpuid_eax(1) & 0x0fff0ff0;
255}
256
257int haswell_stepping(void)
258{
259 return cpuid_eax(1) & 0xf;
260}
261
Aaron Durbin7c351312013-04-10 14:46:25 -0500262/* Dynamically determine if the part is ULT. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700263int haswell_is_ult(void)
Aaron Durbin7c351312013-04-10 14:46:25 -0500264{
265 static int ult = -1;
266
267 if (ult < 0)
Duncan Laurie118d1052013-07-09 15:34:25 -0700268 ult = !!(haswell_family_model() == HASWELL_FAMILY_ULT);
Aaron Durbin7c351312013-04-10 14:46:25 -0500269
270 return ult;
271}
272
Aaron Durbinf24262d2013-04-10 14:59:21 -0500273/* The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
274 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
275 * when a core is woken up. */
276static int pcode_ready(void)
277{
278 int wait_count;
279 const int delay_step = 10;
280
281 wait_count = 0;
282 do {
283 if (!(MCHBAR32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
284 return 0;
285 wait_count += delay_step;
286 udelay(delay_step);
287 } while (wait_count < 1000);
288
289 return -1;
290}
291
292static void calibrate_24mhz_bclk(void)
293{
294 int err_code;
295
296 if (pcode_ready() < 0) {
297 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
298 return;
299 }
300
301 /* A non-zero value initiates the PCODE calibration. */
302 MCHBAR32(BIOS_MAILBOX_DATA) = ~0;
303 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
304 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL;
305
306 if (pcode_ready() < 0) {
307 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
308 return;
309 }
310
311 err_code = MCHBAR32(BIOS_MAILBOX_INTERFACE) & 0xff;
312
313 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration response: %d\n",
314 err_code);
315
316 /* Read the calibrated value. */
317 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
318 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION;
319
320 if (pcode_ready() < 0) {
321 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
322 return;
323 }
324
325 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration value: 0x%08x\n",
326 MCHBAR32(BIOS_MAILBOX_DATA));
327}
328
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700329static u32 pcode_mailbox_read(u32 command)
330{
331 if (pcode_ready() < 0) {
332 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
333 return 0;
334 }
335
336 /* Send command and start transaction */
337 MCHBAR32(BIOS_MAILBOX_INTERFACE) = command | MAILBOX_RUN_BUSY;
338
339 if (pcode_ready() < 0) {
340 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
341 return 0;
342 }
343
344 /* Read mailbox */
345 return MCHBAR32(BIOS_MAILBOX_DATA);
346}
347
Aaron Durbin16cbf892013-07-03 16:21:28 -0500348static void initialize_vr_config(void)
349{
350 msr_t msr;
351
352 printk(BIOS_DEBUG, "Initializing VR config.\n");
353
354 /* Configure VR_CURRENT_CONFIG. */
355 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
356 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
357 * on ULT systems. */
358 msr.hi &= 0xc0000000;
359 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
360 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
361 msr.hi |= (0x0f << (32 - 32)); /* PSI1 threshold - 15A. */
362
Duncan Laurie118d1052013-07-09 15:34:25 -0700363 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500364 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
365 /* Leave the max instantaneous current limit (12:0) to default. */
366 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
367
368 /* Configure VR_MISC_CONFIG MSR. */
369 msr = rdmsr(MSR_VR_MISC_CONFIG);
370 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
371 msr.hi &= ~(0x3ff << (40 - 32));
372 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
373 /* Set IOUT_OFFSET to 0. */
374 msr.hi &= ~0xff;
375 /* Set exit ramp rate to fast. */
376 msr.hi |= (1 << (50 - 32));
377 /* Set entry ramp rate to slow. */
378 msr.hi &= ~(1 << (51 - 32));
379 /* Enable decay mode on C-state entry. */
380 msr.hi |= (1 << (52 - 32));
381 /* Set the slow ramp rate to be fast ramp rate / 4 */
382 msr.hi &= ~(0x3 << (53 - 32));
383 msr.hi |= (0x01 << (53 - 32));
384 /* Set MIN_VID (31:24) to allow CPU to have full control. */
385 msr.lo &= ~0xff000000;
386 wrmsr(MSR_VR_MISC_CONFIG, msr);
387
388 /* Configure VR_MISC_CONFIG2 MSR. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700389 if (haswell_is_ult()) {
Aaron Durbin16cbf892013-07-03 16:21:28 -0500390 msr = rdmsr(MSR_VR_MISC_CONFIG2);
391 msr.lo &= ~0xffff;
392 /* Allow CPU to control minimum voltage completely (15:8) and
393 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
394 msr.lo |= 0x006f;
395 wrmsr(MSR_VR_MISC_CONFIG2, msr);
396 }
397}
398
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700399static void configure_pch_power_sharing(void)
400{
401 u32 pch_power, pch_power_ext, pmsync, pmsync2;
402 int i;
403
404 /* Read PCH Power levels from PCODE */
405 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
406 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
407
408 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
409 pch_power, pch_power_ext);
410
411 pmsync = RCBA32(PMSYNC_CONFIG);
412 pmsync2 = RCBA32(PMSYNC_CONFIG2);
413
414 /* Program PMSYNC_TPR_CONFIG PCH power limit values
415 * pmsync[0:4] = mailbox[0:5]
416 * pmsync[8:12] = mailbox[6:11]
417 * pmsync[16:20] = mailbox[12:17]
418 */
419 for (i = 0; i < 3; i++) {
420 u32 level = pch_power & 0x3f;
421 pch_power >>= 6;
422 pmsync &= ~(0x1f << (i * 8));
423 pmsync |= (level & 0x1f) << (i * 8);
424 }
425 RCBA32(PMSYNC_CONFIG) = pmsync;
426
427 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
428 * pmsync2[0:4] = mailbox[23:18]
429 * pmsync2[8:12] = mailbox_ext[6:11]
430 * pmsync2[16:20] = mailbox_ext[12:17]
431 * pmsync2[24:28] = mailbox_ext[18:22]
432 */
433 pmsync2 &= ~0x1f;
434 pmsync2 |= pch_power & 0x1f;
435
436 for (i = 1; i < 4; i++) {
437 u32 level = pch_power_ext & 0x3f;
438 pch_power_ext >>= 6;
439 pmsync2 &= ~(0x1f << (i * 8));
440 pmsync2 |= (level & 0x1f) << (i * 8);
441 }
442 RCBA32(PMSYNC_CONFIG2) = pmsync2;
443}
444
Aaron Durbin76c37002012-10-30 09:03:43 -0500445int cpu_config_tdp_levels(void)
446{
447 msr_t platform_info;
448
449 /* Bits 34:33 indicate how many levels supported */
450 platform_info = rdmsr(MSR_PLATFORM_INFO);
451 return (platform_info.hi >> 1) & 3;
452}
453
454/*
455 * Configure processor power limits if possible
456 * This must be done AFTER set of BIOS_RESET_CPL
457 */
458void set_power_limits(u8 power_limit_1_time)
459{
460 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
461 msr_t limit;
462 unsigned power_unit;
463 unsigned tdp, min_power, max_power, max_time;
464 u8 power_limit_1_val;
465
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000466 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
467 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500468
469 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
470 return;
471
472 /* Get units */
473 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
474 power_unit = 2 << ((msr.lo & 0xf) - 1);
475
476 /* Get power defaults for this SKU */
477 msr = rdmsr(MSR_PKG_POWER_SKU);
478 tdp = msr.lo & 0x7fff;
479 min_power = (msr.lo >> 16) & 0x7fff;
480 max_power = msr.hi & 0x7fff;
481 max_time = (msr.hi >> 16) & 0x7f;
482
483 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
484
485 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
486 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
487
488 if (min_power > 0 && tdp < min_power)
489 tdp = min_power;
490
491 if (max_power > 0 && tdp > max_power)
492 tdp = max_power;
493
494 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
495
496 /* Set long term power limit to TDP */
497 limit.lo = 0;
498 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
499 limit.lo |= PKG_POWER_LIMIT_EN;
500 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
501 PKG_POWER_LIMIT_TIME_SHIFT;
502
503 /* Set short term power limit to 1.25 * TDP */
504 limit.hi = 0;
505 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
506 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700507 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500508
509 wrmsr(MSR_PKG_POWER_LIMIT, limit);
510
Duncan Lauriec70353f2013-06-28 14:40:38 -0700511 /* Set power limit values in MCHBAR as well */
512 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
513 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
514
515 /* Set DDR RAPL power limit by copying from MMIO to MSR */
516 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
517 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
518 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
519
Aaron Durbin76c37002012-10-30 09:03:43 -0500520 /* Use nominal TDP values for CPUs with configurable TDP */
521 if (cpu_config_tdp_levels()) {
522 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
523 limit.hi = 0;
524 limit.lo = msr.lo & 0xff;
525 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
526 }
527}
528
Aaron Durbin76c37002012-10-30 09:03:43 -0500529static void configure_c_states(void)
530{
531 msr_t msr;
532
533 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500534 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
535 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500536 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
537 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
538 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
539 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
540 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700541 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500542 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
543
544 msr = rdmsr(MSR_PMG_IO_CAPTURE_BASE);
Aaron Durbin7c351312013-04-10 14:46:25 -0500545 msr.lo &= ~0xffff;
546 msr.lo |= (get_pmbase() + 0x14); // LVL_2 base address
547 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500548 wrmsr(MSR_PMG_IO_CAPTURE_BASE, msr);
549
550 msr = rdmsr(MSR_MISC_PWR_MGMT);
551 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
552 wrmsr(MSR_MISC_PWR_MGMT, msr);
553
554 msr = rdmsr(MSR_POWER_CTL);
555 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
556 msr.lo |= (1 << 1); // C1E Enable
557 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
558 wrmsr(MSR_POWER_CTL, msr);
559
Aaron Durbin7c351312013-04-10 14:46:25 -0500560 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
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_0_LIMIT;
563 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500564
Aaron Durbin7c351312013-04-10 14:46:25 -0500565 /* C-state Interrupt Response Latency Control 1 */
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_1_LIMIT;
568 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500569
Aaron Durbin7c351312013-04-10 14:46:25 -0500570 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500571 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500572 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
573 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500574
Aaron Durbin7c351312013-04-10 14:46:25 -0500575 /* Haswell ULT only supoprts the 3-5 latency response registers.*/
Duncan Laurie118d1052013-07-09 15:34:25 -0700576 if (haswell_is_ult()) {
Aaron Durbin7c351312013-04-10 14:46:25 -0500577 /* C-state Interrupt Response Latency Control 3 - package C8 */
578 msr.hi = 0;
579 msr.lo = IRTL_VALID | IRTL_1024_NS |
580 C_STATE_LATENCY_CONTROL_3_LIMIT;
581 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500582
Aaron Durbin7c351312013-04-10 14:46:25 -0500583 /* C-state Interrupt Response Latency Control 4 - package C9 */
584 msr.hi = 0;
585 msr.lo = IRTL_VALID | IRTL_1024_NS |
586 C_STATE_LATENCY_CONTROL_4_LIMIT;
587 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
588
589 /* C-state Interrupt Response Latency Control 5 - package C10 */
590 msr.hi = 0;
591 msr.lo = IRTL_VALID | IRTL_1024_NS |
592 C_STATE_LATENCY_CONTROL_5_LIMIT;
593 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
594 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500595}
Aaron Durbin76c37002012-10-30 09:03:43 -0500596
597static void configure_thermal_target(void)
598{
599 struct cpu_intel_haswell_config *conf;
600 device_t lapic;
601 msr_t msr;
602
603 /* Find pointer to CPU configuration */
604 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
605 if (!lapic || !lapic->chip_info)
606 return;
607 conf = lapic->chip_info;
608
Martin Roth4c3ab732013-07-08 16:23:54 -0600609 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500610 msr = rdmsr(MSR_PLATFORM_INFO);
611 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
612 msr = rdmsr(MSR_TEMPERATURE_TARGET);
613 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
614 msr.lo |= (conf->tcc_offset & 0xf) << 24;
615 wrmsr(MSR_TEMPERATURE_TARGET, msr);
616 }
617}
618
619static void configure_misc(void)
620{
621 msr_t msr;
622
623 msr = rdmsr(IA32_MISC_ENABLE);
624 msr.lo |= (1 << 0); /* Fast String enable */
625 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
626 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
627 wrmsr(IA32_MISC_ENABLE, msr);
628
629 /* Disable Thermal interrupts */
630 msr.lo = 0;
631 msr.hi = 0;
632 wrmsr(IA32_THERM_INTERRUPT, msr);
633
634 /* Enable package critical interrupt only */
635 msr.lo = 1 << 4;
636 msr.hi = 0;
637 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
638}
639
640static void enable_lapic_tpr(void)
641{
642 msr_t msr;
643
644 msr = rdmsr(MSR_PIC_MSG_CONTROL);
645 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
646 wrmsr(MSR_PIC_MSG_CONTROL, msr);
647}
648
649static void configure_dca_cap(void)
650{
651 struct cpuid_result cpuid_regs;
652 msr_t msr;
653
654 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
655 cpuid_regs = cpuid(1);
656 if (cpuid_regs.ecx & (1 << 18)) {
657 msr = rdmsr(IA32_PLATFORM_DCA_CAP);
658 msr.lo |= 1;
659 wrmsr(IA32_PLATFORM_DCA_CAP, msr);
660 }
661}
662
663static void set_max_ratio(void)
664{
665 msr_t msr, perf_ctl;
666
667 perf_ctl.hi = 0;
668
669 /* Check for configurable TDP option */
670 if (cpu_config_tdp_levels()) {
671 /* Set to nominal TDP ratio */
672 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
673 perf_ctl.lo = (msr.lo & 0xff) << 8;
674 } else {
675 /* Platform Info bits 15:8 give max ratio */
676 msr = rdmsr(MSR_PLATFORM_INFO);
677 perf_ctl.lo = msr.lo & 0xff00;
678 }
679 wrmsr(IA32_PERF_CTL, perf_ctl);
680
681 printk(BIOS_DEBUG, "haswell: frequency set to %d\n",
682 ((perf_ctl.lo >> 8) & 0xff) * HASWELL_BCLK);
683}
684
685static void set_energy_perf_bias(u8 policy)
686{
687 msr_t msr;
Aaron Durbindc278f82012-12-11 17:15:13 -0600688 int ecx;
689
690 /* Determine if energy efficient policy is supported. */
691 ecx = cpuid_ecx(0x6);
692 if (!(ecx & (1 << 3)))
693 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500694
695 /* Energy Policy is bits 3:0 */
696 msr = rdmsr(IA32_ENERGY_PERFORMANCE_BIAS);
697 msr.lo &= ~0xf;
698 msr.lo |= policy & 0xf;
699 wrmsr(IA32_ENERGY_PERFORMANCE_BIAS, msr);
700
701 printk(BIOS_DEBUG, "haswell: energy policy set to %u\n",
702 policy);
703}
704
705static void configure_mca(void)
706{
707 msr_t msr;
Aaron Durbin24614af2013-01-12 01:07:28 -0600708 const unsigned int mcg_cap_msr = 0x179;
Aaron Durbin76c37002012-10-30 09:03:43 -0500709 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600710 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500711
Aaron Durbin24614af2013-01-12 01:07:28 -0600712 msr = rdmsr(mcg_cap_msr);
713 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500714 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600715 /* TODO(adurbin): This should only be done on a cold boot. Also, some
716 * of these banks are core vs package scope. For now every CPU clears
717 * every bank. */
718 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500719 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
720}
721
Aaron Durbin305b1f02013-01-15 08:27:05 -0600722static void bsp_init_before_ap_bringup(struct bus *cpu_bus)
Aaron Durbin76c37002012-10-30 09:03:43 -0500723{
Aaron Durbin7af20692013-01-14 14:54:41 -0600724 /* Setup MTRRs based on physical address size. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500725 x86_setup_fixed_mtrrs();
Aaron Durbin7af20692013-01-14 14:54:41 -0600726 x86_setup_var_mtrrs(cpuid_eax(0x80000008) & 0xff, 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500727 x86_mtrr_check();
728
Aaron Durbin16cbf892013-07-03 16:21:28 -0500729 initialize_vr_config();
730
Duncan Laurie118d1052013-07-09 15:34:25 -0700731 if (haswell_is_ult()) {
Aaron Durbinf24262d2013-04-10 14:59:21 -0500732 calibrate_24mhz_bclk();
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700733 configure_pch_power_sharing();
734 }
Aaron Durbin7af20692013-01-14 14:54:41 -0600735}
736
Aaron Durbin305b1f02013-01-15 08:27:05 -0600737/* All CPUs including BSP will run the following function. */
738static void haswell_init(device_t cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600739{
740 /* Clear out pending MCEs */
741 configure_mca();
742
Aaron Durbin76c37002012-10-30 09:03:43 -0500743 /* Enable the local cpu apics */
744 enable_lapic_tpr();
745 setup_lapic();
746
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500747 /* Enable virtualization if Kconfig option is set */
748 enable_vmx();
749
Aaron Durbin76c37002012-10-30 09:03:43 -0500750 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500751 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500752
753 /* Configure Enhanced SpeedStep and Thermal Sensors */
754 configure_misc();
755
756 /* Thermal throttle activation offset */
757 configure_thermal_target();
758
759 /* Enable Direct Cache Access */
760 configure_dca_cap();
761
762 /* Set energy policy */
763 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
764
765 /* Set Max Ratio */
766 set_max_ratio();
767
768 /* Enable Turbo */
769 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600770}
Aaron Durbin76c37002012-10-30 09:03:43 -0500771
Aaron Durbin014baea2014-03-28 22:01:05 -0500772/* MP initialization support. */
773static const void *microcode_patch;
774int ht_disabled;
775
776static int adjust_apic_id_ht_disabled(int index, int apic_id)
777{
778 return 2 * index;
779}
780
781static void relocate_and_load_microcode(void *unused)
782{
783 /* Relocate the SMM handler. */
784 smm_relocate();
785
786 /* After SMM relocation a 2nd microcode load is required. */
787 intel_microcode_load_unlocked(microcode_patch);
788}
789
790static void enable_smis(void *unused)
791{
792 /* Now that all APs have been relocated as well as the BSP let SMIs
793 * start flowing. */
794 southbridge_smm_enable_smi();
795
796 /* Lock down the SMRAM space. */
797 smm_lock();
798}
799
800static struct mp_flight_record mp_steps[] = {
801 MP_FR_NOBLOCK_APS(relocate_and_load_microcode, NULL,
802 relocate_and_load_microcode, NULL),
803 MP_FR_BLOCK_APS(mp_initialize_cpu, NULL, mp_initialize_cpu, NULL),
804 /* Wait for APs to finish initialization before proceeding. */
805 MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
806};
807
Aaron Durbin7af20692013-01-14 14:54:41 -0600808void bsp_init_and_start_aps(struct bus *cpu_bus)
809{
Aaron Durbin6a360042014-02-13 10:30:42 -0600810 void *smm_save_area;
Aaron Durbin014baea2014-03-28 22:01:05 -0500811 int num_threads;
812 int num_cores;
813 msr_t msr;
814 struct mp_params mp_params;
815
816 msr = rdmsr(CORE_THREAD_COUNT_MSR);
817 num_threads = (msr.lo >> 0) & 0xffff;
818 num_cores = (msr.lo >> 16) & 0xffff;
819 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
820 num_cores, num_threads);
821
822 ht_disabled = num_threads == num_cores;
Aaron Durbin305b1f02013-01-15 08:27:05 -0600823
Martin Roth4c3ab732013-07-08 16:23:54 -0600824 /* Perform any necessary BSP initialization before APs are brought up.
825 * This call also allows the BSP to prepare for any secondary effects
Aaron Durbin7af20692013-01-14 14:54:41 -0600826 * from calling cpu_initialize() such as smm_init(). */
Aaron Durbin305b1f02013-01-15 08:27:05 -0600827 bsp_init_before_ap_bringup(cpu_bus);
Aaron Durbin7af20692013-01-14 14:54:41 -0600828
Aaron Durbin305b1f02013-01-15 08:27:05 -0600829 microcode_patch = intel_microcode_find();
Aaron Durbin7af20692013-01-14 14:54:41 -0600830
Aaron Durbin6a360042014-02-13 10:30:42 -0600831 /* Save default SMM area before relocation occurs. */
832 smm_save_area = backup_default_smm_area();
833
Aaron Durbin014baea2014-03-28 22:01:05 -0500834 mp_params.num_cpus = num_threads;
835 mp_params.parallel_microcode_load = 1;
836 if (ht_disabled)
837 mp_params.adjust_apic_id = adjust_apic_id_ht_disabled;
838 else
839 mp_params.adjust_apic_id = NULL;
840 mp_params.flight_plan = &mp_steps[0];
841 mp_params.num_records = ARRAY_SIZE(mp_steps);
842 mp_params.microcode_pointer = microcode_patch;
Aaron Durbin305b1f02013-01-15 08:27:05 -0600843
Aaron Durbin014baea2014-03-28 22:01:05 -0500844 /* Load relocation and permeanent handlers. Then initiate relocation. */
845 if (smm_initialize())
846 printk(BIOS_CRIT, "SMM Initialiazation failed...\n");
Aaron Durbin305b1f02013-01-15 08:27:05 -0600847
Aaron Durbin014baea2014-03-28 22:01:05 -0500848 if (mp_init(cpu_bus, &mp_params)) {
849 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin305b1f02013-01-15 08:27:05 -0600850 }
851
Aaron Durbin6a360042014-02-13 10:30:42 -0600852 /* Restore the default SMM region. */
853 restore_default_smm_area(smm_save_area);
Aaron Durbin76c37002012-10-30 09:03:43 -0500854}
855
856static struct device_operations cpu_dev_ops = {
857 .init = haswell_init,
858};
859
860static struct cpu_device_id cpu_table[] = {
861 { X86_VENDOR_INTEL, 0x306c1 }, /* Intel Haswell 4+2 A0 */
862 { X86_VENDOR_INTEL, 0x306c2 }, /* Intel Haswell 4+2 B0 */
Duncan Laurie512540492012-12-17 11:24:45 -0800863 { X86_VENDOR_INTEL, 0x40650 }, /* Intel Haswell ULT B0 */
864 { X86_VENDOR_INTEL, 0x40651 }, /* Intel Haswell ULT B1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500865 { 0, 0 },
866};
867
868static const struct cpu_driver driver __cpu_driver = {
869 .ops = &cpu_dev_ops,
870 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500871 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500872};