blob: 10f09938d2b4ee8c2c564c55f58ed44650488e3c [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
153/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
154static const u8 power_limit_time_sec_to_msr[] = {
155 [0] = 0x00,
156 [1] = 0x0a,
157 [2] = 0x0b,
158 [3] = 0x4b,
159 [4] = 0x0c,
160 [5] = 0x2c,
161 [6] = 0x4c,
162 [7] = 0x6c,
163 [8] = 0x0d,
164 [10] = 0x2d,
165 [12] = 0x4d,
166 [14] = 0x6d,
167 [16] = 0x0e,
168 [20] = 0x2e,
169 [24] = 0x4e,
170 [28] = 0x6e,
171 [32] = 0x0f,
172 [40] = 0x2f,
173 [48] = 0x4f,
174 [56] = 0x6f,
175 [64] = 0x10,
176 [80] = 0x30,
177 [96] = 0x50,
178 [112] = 0x70,
179 [128] = 0x11,
180};
181
182/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
183static const u8 power_limit_time_msr_to_sec[] = {
184 [0x00] = 0,
185 [0x0a] = 1,
186 [0x0b] = 2,
187 [0x4b] = 3,
188 [0x0c] = 4,
189 [0x2c] = 5,
190 [0x4c] = 6,
191 [0x6c] = 7,
192 [0x0d] = 8,
193 [0x2d] = 10,
194 [0x4d] = 12,
195 [0x6d] = 14,
196 [0x0e] = 16,
197 [0x2e] = 20,
198 [0x4e] = 24,
199 [0x6e] = 28,
200 [0x0f] = 32,
201 [0x2f] = 40,
202 [0x4f] = 48,
203 [0x6f] = 56,
204 [0x10] = 64,
205 [0x30] = 80,
206 [0x50] = 96,
207 [0x70] = 112,
208 [0x11] = 128,
209};
210
Duncan Laurie118d1052013-07-09 15:34:25 -0700211int haswell_family_model(void)
212{
213 return cpuid_eax(1) & 0x0fff0ff0;
214}
215
216int haswell_stepping(void)
217{
218 return cpuid_eax(1) & 0xf;
219}
220
Aaron Durbin7c351312013-04-10 14:46:25 -0500221/* Dynamically determine if the part is ULT. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700222int haswell_is_ult(void)
Aaron Durbin7c351312013-04-10 14:46:25 -0500223{
224 static int ult = -1;
225
226 if (ult < 0)
Duncan Laurie118d1052013-07-09 15:34:25 -0700227 ult = !!(haswell_family_model() == HASWELL_FAMILY_ULT);
Aaron Durbin7c351312013-04-10 14:46:25 -0500228
229 return ult;
230}
231
Aaron Durbinf24262d2013-04-10 14:59:21 -0500232/* The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
233 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
234 * when a core is woken up. */
235static int pcode_ready(void)
236{
237 int wait_count;
238 const int delay_step = 10;
239
240 wait_count = 0;
241 do {
242 if (!(MCHBAR32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
243 return 0;
244 wait_count += delay_step;
245 udelay(delay_step);
246 } while (wait_count < 1000);
247
248 return -1;
249}
250
251static void calibrate_24mhz_bclk(void)
252{
253 int err_code;
254
255 if (pcode_ready() < 0) {
256 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
257 return;
258 }
259
260 /* A non-zero value initiates the PCODE calibration. */
261 MCHBAR32(BIOS_MAILBOX_DATA) = ~0;
262 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
263 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL;
264
265 if (pcode_ready() < 0) {
266 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
267 return;
268 }
269
270 err_code = MCHBAR32(BIOS_MAILBOX_INTERFACE) & 0xff;
271
272 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration response: %d\n",
273 err_code);
274
275 /* Read the calibrated value. */
276 MCHBAR32(BIOS_MAILBOX_INTERFACE) =
277 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION;
278
279 if (pcode_ready() < 0) {
280 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
281 return;
282 }
283
284 printk(BIOS_DEBUG, "PCODE: 24MHz BLCK calibration value: 0x%08x\n",
285 MCHBAR32(BIOS_MAILBOX_DATA));
286}
287
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700288static u32 pcode_mailbox_read(u32 command)
289{
290 if (pcode_ready() < 0) {
291 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
292 return 0;
293 }
294
295 /* Send command and start transaction */
296 MCHBAR32(BIOS_MAILBOX_INTERFACE) = command | MAILBOX_RUN_BUSY;
297
298 if (pcode_ready() < 0) {
299 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
300 return 0;
301 }
302
303 /* Read mailbox */
304 return MCHBAR32(BIOS_MAILBOX_DATA);
305}
306
Aaron Durbin16cbf892013-07-03 16:21:28 -0500307static void initialize_vr_config(void)
308{
309 msr_t msr;
310
311 printk(BIOS_DEBUG, "Initializing VR config.\n");
312
313 /* Configure VR_CURRENT_CONFIG. */
314 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
315 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
316 * on ULT systems. */
317 msr.hi &= 0xc0000000;
318 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
319 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
320 msr.hi |= (0x0f << (32 - 32)); /* PSI1 threshold - 15A. */
321
Duncan Laurie118d1052013-07-09 15:34:25 -0700322 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500323 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
324 /* Leave the max instantaneous current limit (12:0) to default. */
325 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
326
327 /* Configure VR_MISC_CONFIG MSR. */
328 msr = rdmsr(MSR_VR_MISC_CONFIG);
329 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
330 msr.hi &= ~(0x3ff << (40 - 32));
331 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
332 /* Set IOUT_OFFSET to 0. */
333 msr.hi &= ~0xff;
334 /* Set exit ramp rate to fast. */
335 msr.hi |= (1 << (50 - 32));
336 /* Set entry ramp rate to slow. */
337 msr.hi &= ~(1 << (51 - 32));
338 /* Enable decay mode on C-state entry. */
339 msr.hi |= (1 << (52 - 32));
340 /* Set the slow ramp rate to be fast ramp rate / 4 */
341 msr.hi &= ~(0x3 << (53 - 32));
342 msr.hi |= (0x01 << (53 - 32));
343 /* Set MIN_VID (31:24) to allow CPU to have full control. */
344 msr.lo &= ~0xff000000;
345 wrmsr(MSR_VR_MISC_CONFIG, msr);
346
347 /* Configure VR_MISC_CONFIG2 MSR. */
Duncan Laurie118d1052013-07-09 15:34:25 -0700348 if (haswell_is_ult()) {
Aaron Durbin16cbf892013-07-03 16:21:28 -0500349 msr = rdmsr(MSR_VR_MISC_CONFIG2);
350 msr.lo &= ~0xffff;
351 /* Allow CPU to control minimum voltage completely (15:8) and
352 * set the fast ramp voltage to 1110mV (0x6f in 10mV steps). */
353 msr.lo |= 0x006f;
354 wrmsr(MSR_VR_MISC_CONFIG2, msr);
355 }
356}
357
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700358static void configure_pch_power_sharing(void)
359{
360 u32 pch_power, pch_power_ext, pmsync, pmsync2;
361 int i;
362
363 /* Read PCH Power levels from PCODE */
364 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
365 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
366
367 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
368 pch_power, pch_power_ext);
369
370 pmsync = RCBA32(PMSYNC_CONFIG);
371 pmsync2 = RCBA32(PMSYNC_CONFIG2);
372
373 /* Program PMSYNC_TPR_CONFIG PCH power limit values
374 * pmsync[0:4] = mailbox[0:5]
375 * pmsync[8:12] = mailbox[6:11]
376 * pmsync[16:20] = mailbox[12:17]
377 */
378 for (i = 0; i < 3; i++) {
379 u32 level = pch_power & 0x3f;
380 pch_power >>= 6;
381 pmsync &= ~(0x1f << (i * 8));
382 pmsync |= (level & 0x1f) << (i * 8);
383 }
384 RCBA32(PMSYNC_CONFIG) = pmsync;
385
386 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
387 * pmsync2[0:4] = mailbox[23:18]
388 * pmsync2[8:12] = mailbox_ext[6:11]
389 * pmsync2[16:20] = mailbox_ext[12:17]
390 * pmsync2[24:28] = mailbox_ext[18:22]
391 */
392 pmsync2 &= ~0x1f;
393 pmsync2 |= pch_power & 0x1f;
394
395 for (i = 1; i < 4; i++) {
396 u32 level = pch_power_ext & 0x3f;
397 pch_power_ext >>= 6;
398 pmsync2 &= ~(0x1f << (i * 8));
399 pmsync2 |= (level & 0x1f) << (i * 8);
400 }
401 RCBA32(PMSYNC_CONFIG2) = pmsync2;
402}
403
Aaron Durbin76c37002012-10-30 09:03:43 -0500404int cpu_config_tdp_levels(void)
405{
406 msr_t platform_info;
407
408 /* Bits 34:33 indicate how many levels supported */
409 platform_info = rdmsr(MSR_PLATFORM_INFO);
410 return (platform_info.hi >> 1) & 3;
411}
412
413/*
414 * Configure processor power limits if possible
415 * This must be done AFTER set of BIOS_RESET_CPL
416 */
417void set_power_limits(u8 power_limit_1_time)
418{
419 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
420 msr_t limit;
421 unsigned power_unit;
422 unsigned tdp, min_power, max_power, max_time;
423 u8 power_limit_1_val;
424
425 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
Duncan Lauriec70353f2013-06-28 14:40:38 -0700426 power_limit_1_time = 28;
Aaron Durbin76c37002012-10-30 09:03:43 -0500427
428 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
429 return;
430
431 /* Get units */
432 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
433 power_unit = 2 << ((msr.lo & 0xf) - 1);
434
435 /* Get power defaults for this SKU */
436 msr = rdmsr(MSR_PKG_POWER_SKU);
437 tdp = msr.lo & 0x7fff;
438 min_power = (msr.lo >> 16) & 0x7fff;
439 max_power = msr.hi & 0x7fff;
440 max_time = (msr.hi >> 16) & 0x7f;
441
442 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
443
444 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
445 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
446
447 if (min_power > 0 && tdp < min_power)
448 tdp = min_power;
449
450 if (max_power > 0 && tdp > max_power)
451 tdp = max_power;
452
453 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
454
455 /* Set long term power limit to TDP */
456 limit.lo = 0;
457 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
458 limit.lo |= PKG_POWER_LIMIT_EN;
459 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
460 PKG_POWER_LIMIT_TIME_SHIFT;
461
462 /* Set short term power limit to 1.25 * TDP */
463 limit.hi = 0;
464 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
465 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700466 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500467
468 wrmsr(MSR_PKG_POWER_LIMIT, limit);
469
Duncan Lauriec70353f2013-06-28 14:40:38 -0700470 /* Set power limit values in MCHBAR as well */
471 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo;
472 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
473
474 /* Set DDR RAPL power limit by copying from MMIO to MSR */
475 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
476 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
477 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
478
Aaron Durbin76c37002012-10-30 09:03:43 -0500479 /* Use nominal TDP values for CPUs with configurable TDP */
480 if (cpu_config_tdp_levels()) {
481 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
482 limit.hi = 0;
483 limit.lo = msr.lo & 0xff;
484 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
485 }
486}
487
Aaron Durbin76c37002012-10-30 09:03:43 -0500488static void configure_c_states(void)
489{
490 msr_t msr;
491
492 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500493 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
494 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500495 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
496 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
497 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
498 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
499 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Duncan Laurie1c097102013-05-07 13:19:56 -0700500 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500501 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
502
503 msr = rdmsr(MSR_PMG_IO_CAPTURE_BASE);
Aaron Durbin7c351312013-04-10 14:46:25 -0500504 msr.lo &= ~0xffff;
505 msr.lo |= (get_pmbase() + 0x14); // LVL_2 base address
506 /* The deepest package c-state defaults to factory-configured value. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500507 wrmsr(MSR_PMG_IO_CAPTURE_BASE, msr);
508
509 msr = rdmsr(MSR_MISC_PWR_MGMT);
510 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
511 wrmsr(MSR_MISC_PWR_MGMT, msr);
512
513 msr = rdmsr(MSR_POWER_CTL);
514 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
515 msr.lo |= (1 << 1); // C1E Enable
516 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
517 wrmsr(MSR_POWER_CTL, msr);
518
Aaron Durbin7c351312013-04-10 14:46:25 -0500519 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500520 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500521 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
522 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500523
Aaron Durbin7c351312013-04-10 14:46:25 -0500524 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500525 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500526 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
527 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500528
Aaron Durbin7c351312013-04-10 14:46:25 -0500529 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500530 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500531 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
532 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500533
Aaron Durbin7c351312013-04-10 14:46:25 -0500534 /* Haswell ULT only supoprts the 3-5 latency response registers.*/
Duncan Laurie118d1052013-07-09 15:34:25 -0700535 if (haswell_is_ult()) {
Aaron Durbin7c351312013-04-10 14:46:25 -0500536 /* C-state Interrupt Response Latency Control 3 - package C8 */
537 msr.hi = 0;
538 msr.lo = IRTL_VALID | IRTL_1024_NS |
539 C_STATE_LATENCY_CONTROL_3_LIMIT;
540 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500541
Aaron Durbin7c351312013-04-10 14:46:25 -0500542 /* C-state Interrupt Response Latency Control 4 - package C9 */
543 msr.hi = 0;
544 msr.lo = IRTL_VALID | IRTL_1024_NS |
545 C_STATE_LATENCY_CONTROL_4_LIMIT;
546 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
547
548 /* C-state Interrupt Response Latency Control 5 - package C10 */
549 msr.hi = 0;
550 msr.lo = IRTL_VALID | IRTL_1024_NS |
551 C_STATE_LATENCY_CONTROL_5_LIMIT;
552 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
553 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500554}
Aaron Durbin76c37002012-10-30 09:03:43 -0500555
556static void configure_thermal_target(void)
557{
558 struct cpu_intel_haswell_config *conf;
559 device_t lapic;
560 msr_t msr;
561
562 /* Find pointer to CPU configuration */
563 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
564 if (!lapic || !lapic->chip_info)
565 return;
566 conf = lapic->chip_info;
567
Martin Roth4c3ab732013-07-08 16:23:54 -0600568 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500569 msr = rdmsr(MSR_PLATFORM_INFO);
570 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
571 msr = rdmsr(MSR_TEMPERATURE_TARGET);
572 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
573 msr.lo |= (conf->tcc_offset & 0xf) << 24;
574 wrmsr(MSR_TEMPERATURE_TARGET, msr);
575 }
576}
577
578static void configure_misc(void)
579{
580 msr_t msr;
581
582 msr = rdmsr(IA32_MISC_ENABLE);
583 msr.lo |= (1 << 0); /* Fast String enable */
584 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
585 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
586 wrmsr(IA32_MISC_ENABLE, msr);
587
588 /* Disable Thermal interrupts */
589 msr.lo = 0;
590 msr.hi = 0;
591 wrmsr(IA32_THERM_INTERRUPT, msr);
592
593 /* Enable package critical interrupt only */
594 msr.lo = 1 << 4;
595 msr.hi = 0;
596 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
597}
598
599static void enable_lapic_tpr(void)
600{
601 msr_t msr;
602
603 msr = rdmsr(MSR_PIC_MSG_CONTROL);
604 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
605 wrmsr(MSR_PIC_MSG_CONTROL, msr);
606}
607
608static void configure_dca_cap(void)
609{
610 struct cpuid_result cpuid_regs;
611 msr_t msr;
612
613 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
614 cpuid_regs = cpuid(1);
615 if (cpuid_regs.ecx & (1 << 18)) {
616 msr = rdmsr(IA32_PLATFORM_DCA_CAP);
617 msr.lo |= 1;
618 wrmsr(IA32_PLATFORM_DCA_CAP, msr);
619 }
620}
621
622static void set_max_ratio(void)
623{
624 msr_t msr, perf_ctl;
625
626 perf_ctl.hi = 0;
627
628 /* Check for configurable TDP option */
629 if (cpu_config_tdp_levels()) {
630 /* Set to nominal TDP ratio */
631 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
632 perf_ctl.lo = (msr.lo & 0xff) << 8;
633 } else {
634 /* Platform Info bits 15:8 give max ratio */
635 msr = rdmsr(MSR_PLATFORM_INFO);
636 perf_ctl.lo = msr.lo & 0xff00;
637 }
638 wrmsr(IA32_PERF_CTL, perf_ctl);
639
640 printk(BIOS_DEBUG, "haswell: frequency set to %d\n",
641 ((perf_ctl.lo >> 8) & 0xff) * HASWELL_BCLK);
642}
643
644static void set_energy_perf_bias(u8 policy)
645{
646 msr_t msr;
Aaron Durbindc278f82012-12-11 17:15:13 -0600647 int ecx;
648
649 /* Determine if energy efficient policy is supported. */
650 ecx = cpuid_ecx(0x6);
651 if (!(ecx & (1 << 3)))
652 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500653
654 /* Energy Policy is bits 3:0 */
655 msr = rdmsr(IA32_ENERGY_PERFORMANCE_BIAS);
656 msr.lo &= ~0xf;
657 msr.lo |= policy & 0xf;
658 wrmsr(IA32_ENERGY_PERFORMANCE_BIAS, msr);
659
660 printk(BIOS_DEBUG, "haswell: energy policy set to %u\n",
661 policy);
662}
663
664static void configure_mca(void)
665{
666 msr_t msr;
Aaron Durbin24614af2013-01-12 01:07:28 -0600667 const unsigned int mcg_cap_msr = 0x179;
Aaron Durbin76c37002012-10-30 09:03:43 -0500668 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600669 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500670
Aaron Durbin24614af2013-01-12 01:07:28 -0600671 msr = rdmsr(mcg_cap_msr);
672 num_banks = msr.lo & 0xff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500673 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600674 /* TODO(adurbin): This should only be done on a cold boot. Also, some
675 * of these banks are core vs package scope. For now every CPU clears
676 * every bank. */
677 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500678 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
679}
680
Aaron Durbin305b1f02013-01-15 08:27:05 -0600681static void bsp_init_before_ap_bringup(struct bus *cpu_bus)
Aaron Durbin76c37002012-10-30 09:03:43 -0500682{
Aaron Durbin7af20692013-01-14 14:54:41 -0600683 /* Setup MTRRs based on physical address size. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500684 x86_setup_fixed_mtrrs();
Aaron Durbin7af20692013-01-14 14:54:41 -0600685 x86_setup_var_mtrrs(cpuid_eax(0x80000008) & 0xff, 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500686 x86_mtrr_check();
687
Aaron Durbin16cbf892013-07-03 16:21:28 -0500688 initialize_vr_config();
689
Duncan Laurie118d1052013-07-09 15:34:25 -0700690 if (haswell_is_ult()) {
Aaron Durbinf24262d2013-04-10 14:59:21 -0500691 calibrate_24mhz_bclk();
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700692 configure_pch_power_sharing();
693 }
Aaron Durbin7af20692013-01-14 14:54:41 -0600694}
695
Aaron Durbin305b1f02013-01-15 08:27:05 -0600696/* All CPUs including BSP will run the following function. */
697static void haswell_init(device_t cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600698{
699 /* Clear out pending MCEs */
700 configure_mca();
701
Aaron Durbin76c37002012-10-30 09:03:43 -0500702 /* Enable the local cpu apics */
703 enable_lapic_tpr();
704 setup_lapic();
705
706 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500707 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500708
709 /* Configure Enhanced SpeedStep and Thermal Sensors */
710 configure_misc();
711
712 /* Thermal throttle activation offset */
713 configure_thermal_target();
714
715 /* Enable Direct Cache Access */
716 configure_dca_cap();
717
718 /* Set energy policy */
719 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
720
721 /* Set Max Ratio */
722 set_max_ratio();
723
724 /* Enable Turbo */
725 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600726}
Aaron Durbin76c37002012-10-30 09:03:43 -0500727
Aaron Durbin014baea2014-03-28 22:01:05 -0500728/* MP initialization support. */
729static const void *microcode_patch;
730int ht_disabled;
731
732static int adjust_apic_id_ht_disabled(int index, int apic_id)
733{
734 return 2 * index;
735}
736
737static void relocate_and_load_microcode(void *unused)
738{
739 /* Relocate the SMM handler. */
740 smm_relocate();
741
742 /* After SMM relocation a 2nd microcode load is required. */
743 intel_microcode_load_unlocked(microcode_patch);
744}
745
746static void enable_smis(void *unused)
747{
748 /* Now that all APs have been relocated as well as the BSP let SMIs
749 * start flowing. */
750 southbridge_smm_enable_smi();
751
752 /* Lock down the SMRAM space. */
753 smm_lock();
754}
755
756static struct mp_flight_record mp_steps[] = {
757 MP_FR_NOBLOCK_APS(relocate_and_load_microcode, NULL,
758 relocate_and_load_microcode, NULL),
759 MP_FR_BLOCK_APS(mp_initialize_cpu, NULL, mp_initialize_cpu, NULL),
760 /* Wait for APs to finish initialization before proceeding. */
761 MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
762};
763
Aaron Durbin7af20692013-01-14 14:54:41 -0600764void bsp_init_and_start_aps(struct bus *cpu_bus)
765{
Aaron Durbin6a360042014-02-13 10:30:42 -0600766 void *smm_save_area;
Aaron Durbin014baea2014-03-28 22:01:05 -0500767 int num_threads;
768 int num_cores;
769 msr_t msr;
770 struct mp_params mp_params;
771
772 msr = rdmsr(CORE_THREAD_COUNT_MSR);
773 num_threads = (msr.lo >> 0) & 0xffff;
774 num_cores = (msr.lo >> 16) & 0xffff;
775 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
776 num_cores, num_threads);
777
778 ht_disabled = num_threads == num_cores;
Aaron Durbin305b1f02013-01-15 08:27:05 -0600779
Martin Roth4c3ab732013-07-08 16:23:54 -0600780 /* Perform any necessary BSP initialization before APs are brought up.
781 * This call also allows the BSP to prepare for any secondary effects
Aaron Durbin7af20692013-01-14 14:54:41 -0600782 * from calling cpu_initialize() such as smm_init(). */
Aaron Durbin305b1f02013-01-15 08:27:05 -0600783 bsp_init_before_ap_bringup(cpu_bus);
Aaron Durbin7af20692013-01-14 14:54:41 -0600784
Aaron Durbin305b1f02013-01-15 08:27:05 -0600785 microcode_patch = intel_microcode_find();
Aaron Durbin7af20692013-01-14 14:54:41 -0600786
Aaron Durbin6a360042014-02-13 10:30:42 -0600787 /* Save default SMM area before relocation occurs. */
788 smm_save_area = backup_default_smm_area();
789
Aaron Durbin014baea2014-03-28 22:01:05 -0500790 mp_params.num_cpus = num_threads;
791 mp_params.parallel_microcode_load = 1;
792 if (ht_disabled)
793 mp_params.adjust_apic_id = adjust_apic_id_ht_disabled;
794 else
795 mp_params.adjust_apic_id = NULL;
796 mp_params.flight_plan = &mp_steps[0];
797 mp_params.num_records = ARRAY_SIZE(mp_steps);
798 mp_params.microcode_pointer = microcode_patch;
Aaron Durbin305b1f02013-01-15 08:27:05 -0600799
Aaron Durbin014baea2014-03-28 22:01:05 -0500800 /* Load relocation and permeanent handlers. Then initiate relocation. */
801 if (smm_initialize())
802 printk(BIOS_CRIT, "SMM Initialiazation failed...\n");
Aaron Durbin305b1f02013-01-15 08:27:05 -0600803
Aaron Durbin014baea2014-03-28 22:01:05 -0500804 if (mp_init(cpu_bus, &mp_params)) {
805 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin305b1f02013-01-15 08:27:05 -0600806 }
807
Aaron Durbin6a360042014-02-13 10:30:42 -0600808 /* Restore the default SMM region. */
809 restore_default_smm_area(smm_save_area);
Aaron Durbin76c37002012-10-30 09:03:43 -0500810}
811
812static struct device_operations cpu_dev_ops = {
813 .init = haswell_init,
814};
815
816static struct cpu_device_id cpu_table[] = {
817 { X86_VENDOR_INTEL, 0x306c1 }, /* Intel Haswell 4+2 A0 */
818 { X86_VENDOR_INTEL, 0x306c2 }, /* Intel Haswell 4+2 B0 */
Duncan Laurie512540492012-12-17 11:24:45 -0800819 { X86_VENDOR_INTEL, 0x40650 }, /* Intel Haswell ULT B0 */
820 { X86_VENDOR_INTEL, 0x40651 }, /* Intel Haswell ULT B1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500821 { 0, 0 },
822};
823
824static const struct cpu_driver driver __cpu_driver = {
825 .ops = &cpu_dev_ops,
826 .id_table = cpu_table,
Aaron Durbin7c351312013-04-10 14:46:25 -0500827 .cstates = cstate_map,
Aaron Durbin76c37002012-10-30 09:03:43 -0500828};
829