blob: 9fcb527a167e4c788204c003db577a5ec1380034 [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <console/console.h>
4#include <device/device.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <cpu/cpu.h>
7#include <cpu/x86/mtrr.h>
8#include <cpu/x86/msr.h>
Aaron Durbin014baea2014-03-28 22:01:05 -05009#include <cpu/x86/mp.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050010#include <cpu/x86/lapic.h>
11#include <cpu/intel/microcode.h>
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +030012#include <cpu/intel/smm_reloc.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050013#include <cpu/intel/speedstep.h>
14#include <cpu/intel/turbo.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050015#include <cpu/x86/name.h>
Aaron Durbinf24262d2013-04-10 14:59:21 -050016#include <delay.h>
Aaron Durbin7c351312013-04-10 14:46:25 -050017#include <northbridge/intel/haswell/haswell.h>
18#include <southbridge/intel/lynxpoint/pch.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060019#include <cpu/intel/common/common.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include "haswell.h"
21#include "chip.h"
22
Aaron Durbin76c37002012-10-30 09:03:43 -050023/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
24static const u8 power_limit_time_sec_to_msr[] = {
25 [0] = 0x00,
26 [1] = 0x0a,
27 [2] = 0x0b,
28 [3] = 0x4b,
29 [4] = 0x0c,
30 [5] = 0x2c,
31 [6] = 0x4c,
32 [7] = 0x6c,
33 [8] = 0x0d,
34 [10] = 0x2d,
35 [12] = 0x4d,
36 [14] = 0x6d,
37 [16] = 0x0e,
38 [20] = 0x2e,
39 [24] = 0x4e,
40 [28] = 0x6e,
41 [32] = 0x0f,
42 [40] = 0x2f,
43 [48] = 0x4f,
44 [56] = 0x6f,
45 [64] = 0x10,
46 [80] = 0x30,
47 [96] = 0x50,
48 [112] = 0x70,
49 [128] = 0x11,
50};
51
52/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
53static const u8 power_limit_time_msr_to_sec[] = {
54 [0x00] = 0,
55 [0x0a] = 1,
56 [0x0b] = 2,
57 [0x4b] = 3,
58 [0x0c] = 4,
59 [0x2c] = 5,
60 [0x4c] = 6,
61 [0x6c] = 7,
62 [0x0d] = 8,
63 [0x2d] = 10,
64 [0x4d] = 12,
65 [0x6d] = 14,
66 [0x0e] = 16,
67 [0x2e] = 20,
68 [0x4e] = 24,
69 [0x6e] = 28,
70 [0x0f] = 32,
71 [0x2f] = 40,
72 [0x4f] = 48,
73 [0x6f] = 56,
74 [0x10] = 64,
75 [0x30] = 80,
76 [0x50] = 96,
77 [0x70] = 112,
78 [0x11] = 128,
79};
80
Angel Pons5d92aa52020-10-14 00:02:37 +020081/* The core 100MHz BCLK is disabled in deeper c-states. One needs to calibrate
82 * the 100MHz BCLK against the 24MHz BCLK to restore the clocks properly
Aaron Durbinf24262d2013-04-10 14:59:21 -050083 * when a core is woken up. */
84static int pcode_ready(void)
85{
86 int wait_count;
87 const int delay_step = 10;
88
89 wait_count = 0;
90 do {
Angel Pons7811a452021-03-27 20:05:22 +010091 if (!(mchbar_read32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
Aaron Durbinf24262d2013-04-10 14:59:21 -050092 return 0;
93 wait_count += delay_step;
94 udelay(delay_step);
95 } while (wait_count < 1000);
96
97 return -1;
98}
99
100static void calibrate_24mhz_bclk(void)
101{
102 int err_code;
103
104 if (pcode_ready() < 0) {
105 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
106 return;
107 }
108
109 /* A non-zero value initiates the PCODE calibration. */
Angel Pons7811a452021-03-27 20:05:22 +0100110 mchbar_write32(BIOS_MAILBOX_DATA, ~0);
111 mchbar_write32(BIOS_MAILBOX_INTERFACE,
112 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL);
Aaron Durbinf24262d2013-04-10 14:59:21 -0500113
114 if (pcode_ready() < 0) {
115 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
116 return;
117 }
118
Angel Pons7811a452021-03-27 20:05:22 +0100119 err_code = mchbar_read32(BIOS_MAILBOX_INTERFACE) & 0xff;
Aaron Durbinf24262d2013-04-10 14:59:21 -0500120
Angel Pons5d92aa52020-10-14 00:02:37 +0200121 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration response: %d\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500122 err_code);
123
124 /* Read the calibrated value. */
Angel Pons7811a452021-03-27 20:05:22 +0100125 mchbar_write32(BIOS_MAILBOX_INTERFACE,
126 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION);
Aaron Durbinf24262d2013-04-10 14:59:21 -0500127
128 if (pcode_ready() < 0) {
129 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
130 return;
131 }
132
Angel Pons5d92aa52020-10-14 00:02:37 +0200133 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration value: 0x%08x\n",
Angel Pons7811a452021-03-27 20:05:22 +0100134 mchbar_read32(BIOS_MAILBOX_DATA));
Aaron Durbinf24262d2013-04-10 14:59:21 -0500135}
136
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700137static u32 pcode_mailbox_read(u32 command)
138{
139 if (pcode_ready() < 0) {
140 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
141 return 0;
142 }
143
144 /* Send command and start transaction */
Angel Pons7811a452021-03-27 20:05:22 +0100145 mchbar_write32(BIOS_MAILBOX_INTERFACE, command | MAILBOX_RUN_BUSY);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700146
147 if (pcode_ready() < 0) {
148 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
149 return 0;
150 }
151
152 /* Read mailbox */
Angel Pons7811a452021-03-27 20:05:22 +0100153 return mchbar_read32(BIOS_MAILBOX_DATA);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700154}
155
Angel Pons1c7ba622020-10-29 00:01:29 +0100156static int pcode_mailbox_write(u32 command, u32 data)
157{
158 if (pcode_ready() < 0) {
159 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
160 return -1;
161 }
162
Angel Pons7811a452021-03-27 20:05:22 +0100163 mchbar_write32(BIOS_MAILBOX_DATA, data);
Angel Pons1c7ba622020-10-29 00:01:29 +0100164
165 /* Send command and start transaction */
Angel Pons7811a452021-03-27 20:05:22 +0100166 mchbar_write32(BIOS_MAILBOX_INTERFACE, command | MAILBOX_RUN_BUSY);
Angel Pons1c7ba622020-10-29 00:01:29 +0100167
168 if (pcode_ready() < 0) {
169 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
170 return -1;
171 }
172
173 return 0;
174}
175
Aaron Durbin16cbf892013-07-03 16:21:28 -0500176static void initialize_vr_config(void)
177{
Angel Pons242fd282020-10-28 23:48:56 +0100178 struct cpu_vr_config vr_config = { 0 };
Aaron Durbin16cbf892013-07-03 16:21:28 -0500179 msr_t msr;
180
Angel Pons242fd282020-10-28 23:48:56 +0100181 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
182
183 if (lapic && lapic->chip_info) {
184 const struct cpu_intel_haswell_config *conf = lapic->chip_info;
185
186 vr_config = conf->vr_config;
187 }
188
Aaron Durbin16cbf892013-07-03 16:21:28 -0500189 printk(BIOS_DEBUG, "Initializing VR config.\n");
190
191 /* Configure VR_CURRENT_CONFIG. */
192 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
193 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
194 * on ULT systems. */
195 msr.hi &= 0xc0000000;
196 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
197 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
Angel Pons9dcd1c12020-10-28 22:41:26 +0100198 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A. */
Aaron Durbin16cbf892013-07-03 16:21:28 -0500199
Duncan Laurie118d1052013-07-09 15:34:25 -0700200 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500201 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
202 /* Leave the max instantaneous current limit (12:0) to default. */
203 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
204
205 /* Configure VR_MISC_CONFIG MSR. */
206 msr = rdmsr(MSR_VR_MISC_CONFIG);
207 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
208 msr.hi &= ~(0x3ff << (40 - 32));
209 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
210 /* Set IOUT_OFFSET to 0. */
211 msr.hi &= ~0xff;
212 /* Set exit ramp rate to fast. */
213 msr.hi |= (1 << (50 - 32));
214 /* Set entry ramp rate to slow. */
215 msr.hi &= ~(1 << (51 - 32));
216 /* Enable decay mode on C-state entry. */
217 msr.hi |= (1 << (52 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100218 /* Set the slow ramp rate */
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300219 if (haswell_is_ult()) {
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300220 msr.hi &= ~(0x3 << (53 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100221 /* Configure the C-state exit ramp rate. */
222 if (vr_config.slow_ramp_rate_enable) {
223 /* Configured slow ramp rate. */
224 msr.hi |= ((vr_config.slow_ramp_rate_set & 0x3) << (53 - 32));
225 /* Set exit ramp rate to slow. */
226 msr.hi &= ~(1 << (50 - 32));
227 } else {
228 /* Fast ramp rate / 4. */
229 msr.hi |= (1 << (53 - 32));
230 }
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300231 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500232 /* Set MIN_VID (31:24) to allow CPU to have full control. */
233 msr.lo &= ~0xff000000;
Angel Pons242fd282020-10-28 23:48:56 +0100234 msr.lo |= (vr_config.cpu_min_vid & 0xff) << 24;
Aaron Durbin16cbf892013-07-03 16:21:28 -0500235 wrmsr(MSR_VR_MISC_CONFIG, msr);
236
237 /* Configure VR_MISC_CONFIG2 MSR. */
Angel Pons4c95f102020-10-28 19:38:12 +0100238 if (!haswell_is_ult())
239 return;
240
241 msr = rdmsr(MSR_VR_MISC_CONFIG2);
242 msr.lo &= ~0xffff;
243 /* Allow CPU to control minimum voltage completely (15:8) and
Angel Ponsc86b1192020-10-28 23:53:45 +0100244 set the fast ramp voltage in 10mV steps. */
245 if (cpu_family_model() == BROADWELL_FAMILY_ULT)
246 msr.lo |= 0x006a; /* 1.56V */
247 else
248 msr.lo |= 0x006f; /* 1.60V */
Angel Pons4c95f102020-10-28 19:38:12 +0100249 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Angel Pons1c7ba622020-10-29 00:01:29 +0100250
251 /* Set C9/C10 VCC Min */
252 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500253}
254
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700255static void configure_pch_power_sharing(void)
256{
257 u32 pch_power, pch_power_ext, pmsync, pmsync2;
258 int i;
259
260 /* Read PCH Power levels from PCODE */
261 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
262 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
263
264 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700265 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700266
267 pmsync = RCBA32(PMSYNC_CONFIG);
268 pmsync2 = RCBA32(PMSYNC_CONFIG2);
269
270 /* Program PMSYNC_TPR_CONFIG PCH power limit values
271 * pmsync[0:4] = mailbox[0:5]
272 * pmsync[8:12] = mailbox[6:11]
273 * pmsync[16:20] = mailbox[12:17]
274 */
275 for (i = 0; i < 3; i++) {
276 u32 level = pch_power & 0x3f;
277 pch_power >>= 6;
278 pmsync &= ~(0x1f << (i * 8));
279 pmsync |= (level & 0x1f) << (i * 8);
280 }
281 RCBA32(PMSYNC_CONFIG) = pmsync;
282
283 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
284 * pmsync2[0:4] = mailbox[23:18]
285 * pmsync2[8:12] = mailbox_ext[6:11]
286 * pmsync2[16:20] = mailbox_ext[12:17]
287 * pmsync2[24:28] = mailbox_ext[18:22]
288 */
289 pmsync2 &= ~0x1f;
290 pmsync2 |= pch_power & 0x1f;
291
292 for (i = 1; i < 4; i++) {
293 u32 level = pch_power_ext & 0x3f;
294 pch_power_ext >>= 6;
295 pmsync2 &= ~(0x1f << (i * 8));
296 pmsync2 |= (level & 0x1f) << (i * 8);
297 }
298 RCBA32(PMSYNC_CONFIG2) = pmsync2;
299}
300
Aaron Durbin76c37002012-10-30 09:03:43 -0500301int cpu_config_tdp_levels(void)
302{
303 msr_t platform_info;
304
305 /* Bits 34:33 indicate how many levels supported */
306 platform_info = rdmsr(MSR_PLATFORM_INFO);
307 return (platform_info.hi >> 1) & 3;
308}
309
310/*
311 * Configure processor power limits if possible
312 * This must be done AFTER set of BIOS_RESET_CPL
313 */
314void set_power_limits(u8 power_limit_1_time)
315{
316 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
317 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700318 unsigned int power_unit;
319 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500320 u8 power_limit_1_val;
321
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000322 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100323 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500324
325 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
326 return;
327
328 /* Get units */
329 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
330 power_unit = 2 << ((msr.lo & 0xf) - 1);
331
332 /* Get power defaults for this SKU */
333 msr = rdmsr(MSR_PKG_POWER_SKU);
334 tdp = msr.lo & 0x7fff;
335 min_power = (msr.lo >> 16) & 0x7fff;
336 max_power = msr.hi & 0x7fff;
337 max_time = (msr.hi >> 16) & 0x7f;
338
339 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
340
341 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
342 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
343
344 if (min_power > 0 && tdp < min_power)
345 tdp = min_power;
346
347 if (max_power > 0 && tdp > max_power)
348 tdp = max_power;
349
350 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
351
352 /* Set long term power limit to TDP */
353 limit.lo = 0;
354 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
355 limit.lo |= PKG_POWER_LIMIT_EN;
356 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
357 PKG_POWER_LIMIT_TIME_SHIFT;
358
359 /* Set short term power limit to 1.25 * TDP */
360 limit.hi = 0;
361 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
362 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700363 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500364
365 wrmsr(MSR_PKG_POWER_LIMIT, limit);
366
Duncan Lauriec70353f2013-06-28 14:40:38 -0700367 /* Set power limit values in MCHBAR as well */
Angel Pons7811a452021-03-27 20:05:22 +0100368 mchbar_write32(MCH_PKG_POWER_LIMIT_LO, limit.lo);
369 mchbar_write32(MCH_PKG_POWER_LIMIT_HI, limit.hi);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700370
371 /* Set DDR RAPL power limit by copying from MMIO to MSR */
Angel Pons7811a452021-03-27 20:05:22 +0100372 msr.lo = mchbar_read32(MCH_DDR_POWER_LIMIT_LO);
373 msr.hi = mchbar_read32(MCH_DDR_POWER_LIMIT_HI);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700374 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
375
Aaron Durbin76c37002012-10-30 09:03:43 -0500376 /* Use nominal TDP values for CPUs with configurable TDP */
377 if (cpu_config_tdp_levels()) {
378 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
379 limit.hi = 0;
380 limit.lo = msr.lo & 0xff;
381 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
382 }
383}
384
Aaron Durbin76c37002012-10-30 09:03:43 -0500385static void configure_c_states(void)
386{
Angel Ponsc89d2a282020-10-28 22:23:02 +0100387 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
388
389 const bool timed_mwait_capable = !!(msr.hi & TIMED_MWAIT_SUPPORTED);
Aaron Durbin76c37002012-10-30 09:03:43 -0500390
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200391 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500392 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
393 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500394 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
395 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
396 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
397 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
398 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Angel Ponsc89d2a282020-10-28 22:23:02 +0100399
400 if (timed_mwait_capable)
401 msr.lo |= (1 << 31); // Timed MWAIT Enable
402
Duncan Laurie1c097102013-05-07 13:19:56 -0700403 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200404 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500405
Aaron Durbin76c37002012-10-30 09:03:43 -0500406 msr = rdmsr(MSR_MISC_PWR_MGMT);
407 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
408 wrmsr(MSR_MISC_PWR_MGMT, msr);
409
410 msr = rdmsr(MSR_POWER_CTL);
411 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
412 msr.lo |= (1 << 1); // C1E Enable
413 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
414 wrmsr(MSR_POWER_CTL, msr);
415
Aaron Durbin7c351312013-04-10 14:46:25 -0500416 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500417 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500418 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
419 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500420
Aaron Durbin7c351312013-04-10 14:46:25 -0500421 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500422 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500423 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
424 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500425
Aaron Durbin7c351312013-04-10 14:46:25 -0500426 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500427 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500428 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
429 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500430
Angel Pons4c95f102020-10-28 19:38:12 +0100431 /* Only Haswell ULT supports the 3-5 latency response registers */
432 if (!haswell_is_ult())
433 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500434
Angel Pons4c95f102020-10-28 19:38:12 +0100435 /* C-state Interrupt Response Latency Control 3 - package C8 */
436 msr.hi = 0;
437 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
438 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500439
Angel Pons4c95f102020-10-28 19:38:12 +0100440 /* C-state Interrupt Response Latency Control 4 - package C9 */
441 msr.hi = 0;
442 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
443 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
444
445 /* C-state Interrupt Response Latency Control 5 - package C10 */
446 msr.hi = 0;
447 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
448 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500449}
Aaron Durbin76c37002012-10-30 09:03:43 -0500450
451static void configure_thermal_target(void)
452{
453 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100454 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500455 msr_t msr;
456
457 /* Find pointer to CPU configuration */
458 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
459 if (!lapic || !lapic->chip_info)
460 return;
461 conf = lapic->chip_info;
462
Martin Roth4c3ab732013-07-08 16:23:54 -0600463 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500464 msr = rdmsr(MSR_PLATFORM_INFO);
465 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
466 msr = rdmsr(MSR_TEMPERATURE_TARGET);
467 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
468 msr.lo |= (conf->tcc_offset & 0xf) << 24;
469 wrmsr(MSR_TEMPERATURE_TARGET, msr);
470 }
471}
472
473static void configure_misc(void)
474{
475 msr_t msr;
476
477 msr = rdmsr(IA32_MISC_ENABLE);
478 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700479 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500480 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
481 wrmsr(IA32_MISC_ENABLE, msr);
482
483 /* Disable Thermal interrupts */
484 msr.lo = 0;
485 msr.hi = 0;
486 wrmsr(IA32_THERM_INTERRUPT, msr);
487
488 /* Enable package critical interrupt only */
489 msr.lo = 1 << 4;
490 msr.hi = 0;
491 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
492}
493
Aaron Durbin76c37002012-10-30 09:03:43 -0500494static void set_max_ratio(void)
495{
496 msr_t msr, perf_ctl;
497
498 perf_ctl.hi = 0;
499
500 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100501 if (get_turbo_state() == TURBO_ENABLED) {
502 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
503 perf_ctl.lo = (msr.lo & 0xff) << 8;
504 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500505 /* Set to nominal TDP ratio */
506 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
507 perf_ctl.lo = (msr.lo & 0xff) << 8;
508 } else {
509 /* Platform Info bits 15:8 give max ratio */
510 msr = rdmsr(MSR_PLATFORM_INFO);
511 perf_ctl.lo = msr.lo & 0xff00;
512 }
513 wrmsr(IA32_PERF_CTL, perf_ctl);
514
Angel Ponsf6cf49272020-09-25 01:14:24 +0200515 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100516 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500517}
518
Aaron Durbin76c37002012-10-30 09:03:43 -0500519static void configure_mca(void)
520{
521 msr_t msr;
522 int i;
Aaron Durbin24614af2013-01-12 01:07:28 -0600523 int num_banks;
Aaron Durbin76c37002012-10-30 09:03:43 -0500524
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200525 msr = rdmsr(IA32_MCG_CAP);
Aaron Durbin24614af2013-01-12 01:07:28 -0600526 num_banks = msr.lo & 0xff;
Angel Pons1515a482021-06-13 22:33:06 +0200527
528 /* Enable all error reporting */
529 msr.lo = msr.hi = ~0;
530 for (i = 0; i < num_banks; i++)
531 wrmsr(IA32_MC0_CTL + (i * 4), msr);
532
Aaron Durbin76c37002012-10-30 09:03:43 -0500533 msr.lo = msr.hi = 0;
Aaron Durbin24614af2013-01-12 01:07:28 -0600534 /* TODO(adurbin): This should only be done on a cold boot. Also, some
535 * of these banks are core vs package scope. For now every CPU clears
536 * every bank. */
537 for (i = 0; i < num_banks; i++)
Aaron Durbin76c37002012-10-30 09:03:43 -0500538 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
539}
540
Aaron Durbin305b1f02013-01-15 08:27:05 -0600541/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100542static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600543{
544 /* Clear out pending MCEs */
545 configure_mca();
546
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200547 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500548 enable_lapic_tpr();
549 setup_lapic();
550
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600551 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600552 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500553
Aaron Durbin76c37002012-10-30 09:03:43 -0500554 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500555 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500556
557 /* Configure Enhanced SpeedStep and Thermal Sensors */
558 configure_misc();
559
560 /* Thermal throttle activation offset */
561 configure_thermal_target();
562
563 /* Enable Direct Cache Access */
564 configure_dca_cap();
565
566 /* Set energy policy */
567 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
568
Aaron Durbin76c37002012-10-30 09:03:43 -0500569 /* Enable Turbo */
570 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600571}
Aaron Durbin76c37002012-10-30 09:03:43 -0500572
Aaron Durbin014baea2014-03-28 22:01:05 -0500573/* MP initialization support. */
574static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500575
Aaron Durbin463af332016-05-03 17:26:35 -0500576static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500577{
Aaron Durbin463af332016-05-03 17:26:35 -0500578 /* Setup MTRRs based on physical address size. */
579 x86_setup_mtrrs_with_detect();
580 x86_mtrr_check();
581
582 initialize_vr_config();
583
Angel Pons4c95f102020-10-28 19:38:12 +0100584 if (!haswell_is_ult())
585 return;
586
587 calibrate_24mhz_bclk();
588 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500589}
590
Aaron Durbin463af332016-05-03 17:26:35 -0500591static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500592{
Aaron Durbin463af332016-05-03 17:26:35 -0500593 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500594 int num_threads;
595 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500596
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200597 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500598 num_threads = (msr.lo >> 0) & 0xffff;
599 num_cores = (msr.lo >> 16) & 0xffff;
600 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
601 num_cores, num_threads);
602
Aaron Durbin463af332016-05-03 17:26:35 -0500603 return num_threads;
604}
Aaron Durbin7af20692013-01-14 14:54:41 -0600605
Aaron Durbin463af332016-05-03 17:26:35 -0500606static void get_microcode_info(const void **microcode, int *parallel)
607{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600608 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500609 *microcode = microcode_patch;
610 *parallel = 1;
611}
Aaron Durbin7af20692013-01-14 14:54:41 -0600612
Aaron Durbin463af332016-05-03 17:26:35 -0500613static void per_cpu_smm_trigger(void)
614{
615 /* Relocate the SMM handler. */
616 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600617
Aaron Durbin463af332016-05-03 17:26:35 -0500618 /* After SMM relocation a 2nd microcode load is required. */
619 intel_microcode_load_unlocked(microcode_patch);
620}
621
622static void post_mp_init(void)
623{
Angel Pons053deb82020-10-28 22:40:02 +0100624 /* Set Max Ratio */
625 set_max_ratio();
626
Aaron Durbin463af332016-05-03 17:26:35 -0500627 /* Now that all APs have been relocated as well as the BSP let SMIs
628 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300629 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500630
631 /* Lock down the SMRAM space. */
632 smm_lock();
633}
634
635static const struct mp_ops mp_ops = {
636 .pre_mp_init = pre_mp_init,
637 .get_cpu_count = get_cpu_count,
638 .get_smm_info = smm_info,
639 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500640 .pre_mp_smm_init = smm_initialize,
641 .per_cpu_smm_trigger = per_cpu_smm_trigger,
642 .relocation_handler = smm_relocation_handler,
643 .post_mp_init = post_mp_init,
644};
645
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300646void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500647{
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700648 if (mp_init_with_smm(cpu_bus, &mp_ops))
Aaron Durbin014baea2014-03-28 22:01:05 -0500649 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500650}
651
652static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100653 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500654};
655
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100656static const struct cpu_device_id cpu_table[] = {
Angel Pons8b0636e2020-10-28 21:48:29 +0100657 { X86_VENDOR_INTEL, CPUID_HASWELL_A0 },
658 { X86_VENDOR_INTEL, CPUID_HASWELL_B0 },
659 { X86_VENDOR_INTEL, CPUID_HASWELL_C0 },
660 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_B0 },
661 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_C0 },
662 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_B0 },
663 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_C0 },
Angel Ponsf542b7b2020-10-29 01:02:03 +0100664 { X86_VENDOR_INTEL, CPUID_BROADWELL_C0 },
665 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_C0 },
666 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_D0 },
667 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_E0 },
Aaron Durbin76c37002012-10-30 09:03:43 -0500668 { 0, 0 },
669};
670
671static const struct cpu_driver driver __cpu_driver = {
672 .ops = &cpu_dev_ops,
673 .id_table = cpu_table,
Aaron Durbin76c37002012-10-30 09:03:43 -0500674};