blob: 2ab77b304432bc4e53a298d051d5245cd8ae2e07 [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>
Felix Heldd27ef5b2021-10-20 20:18:12 +020020#include <types.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050021#include "haswell.h"
22#include "chip.h"
23
Aaron Durbin76c37002012-10-30 09:03:43 -050024/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
25static const u8 power_limit_time_sec_to_msr[] = {
26 [0] = 0x00,
27 [1] = 0x0a,
28 [2] = 0x0b,
29 [3] = 0x4b,
30 [4] = 0x0c,
31 [5] = 0x2c,
32 [6] = 0x4c,
33 [7] = 0x6c,
34 [8] = 0x0d,
35 [10] = 0x2d,
36 [12] = 0x4d,
37 [14] = 0x6d,
38 [16] = 0x0e,
39 [20] = 0x2e,
40 [24] = 0x4e,
41 [28] = 0x6e,
42 [32] = 0x0f,
43 [40] = 0x2f,
44 [48] = 0x4f,
45 [56] = 0x6f,
46 [64] = 0x10,
47 [80] = 0x30,
48 [96] = 0x50,
49 [112] = 0x70,
50 [128] = 0x11,
51};
52
53/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
54static const u8 power_limit_time_msr_to_sec[] = {
55 [0x00] = 0,
56 [0x0a] = 1,
57 [0x0b] = 2,
58 [0x4b] = 3,
59 [0x0c] = 4,
60 [0x2c] = 5,
61 [0x4c] = 6,
62 [0x6c] = 7,
63 [0x0d] = 8,
64 [0x2d] = 10,
65 [0x4d] = 12,
66 [0x6d] = 14,
67 [0x0e] = 16,
68 [0x2e] = 20,
69 [0x4e] = 24,
70 [0x6e] = 28,
71 [0x0f] = 32,
72 [0x2f] = 40,
73 [0x4f] = 48,
74 [0x6f] = 56,
75 [0x10] = 64,
76 [0x30] = 80,
77 [0x50] = 96,
78 [0x70] = 112,
79 [0x11] = 128,
80};
81
Angel Pons5d92aa52020-10-14 00:02:37 +020082/* The core 100MHz BCLK is disabled in deeper c-states. One needs to calibrate
83 * the 100MHz BCLK against the 24MHz BCLK to restore the clocks properly
Aaron Durbinf24262d2013-04-10 14:59:21 -050084 * when a core is woken up. */
85static int pcode_ready(void)
86{
87 int wait_count;
88 const int delay_step = 10;
89
90 wait_count = 0;
91 do {
Angel Pons7811a452021-03-27 20:05:22 +010092 if (!(mchbar_read32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
Aaron Durbinf24262d2013-04-10 14:59:21 -050093 return 0;
94 wait_count += delay_step;
95 udelay(delay_step);
96 } while (wait_count < 1000);
97
98 return -1;
99}
100
101static void calibrate_24mhz_bclk(void)
102{
103 int err_code;
104
105 if (pcode_ready() < 0) {
106 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
107 return;
108 }
109
110 /* A non-zero value initiates the PCODE calibration. */
Angel Pons7811a452021-03-27 20:05:22 +0100111 mchbar_write32(BIOS_MAILBOX_DATA, ~0);
112 mchbar_write32(BIOS_MAILBOX_INTERFACE,
113 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL);
Aaron Durbinf24262d2013-04-10 14:59:21 -0500114
115 if (pcode_ready() < 0) {
116 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
117 return;
118 }
119
Angel Pons7811a452021-03-27 20:05:22 +0100120 err_code = mchbar_read32(BIOS_MAILBOX_INTERFACE) & 0xff;
Aaron Durbinf24262d2013-04-10 14:59:21 -0500121
Angel Pons5d92aa52020-10-14 00:02:37 +0200122 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration response: %d\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500123 err_code);
124
125 /* Read the calibrated value. */
Angel Pons7811a452021-03-27 20:05:22 +0100126 mchbar_write32(BIOS_MAILBOX_INTERFACE,
127 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION);
Aaron Durbinf24262d2013-04-10 14:59:21 -0500128
129 if (pcode_ready() < 0) {
130 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
131 return;
132 }
133
Angel Pons5d92aa52020-10-14 00:02:37 +0200134 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration value: 0x%08x\n",
Angel Pons7811a452021-03-27 20:05:22 +0100135 mchbar_read32(BIOS_MAILBOX_DATA));
Aaron Durbinf24262d2013-04-10 14:59:21 -0500136}
137
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700138static u32 pcode_mailbox_read(u32 command)
139{
140 if (pcode_ready() < 0) {
141 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
142 return 0;
143 }
144
145 /* Send command and start transaction */
Angel Pons7811a452021-03-27 20:05:22 +0100146 mchbar_write32(BIOS_MAILBOX_INTERFACE, command | MAILBOX_RUN_BUSY);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700147
148 if (pcode_ready() < 0) {
149 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
150 return 0;
151 }
152
153 /* Read mailbox */
Angel Pons7811a452021-03-27 20:05:22 +0100154 return mchbar_read32(BIOS_MAILBOX_DATA);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700155}
156
Angel Pons1c7ba622020-10-29 00:01:29 +0100157static int pcode_mailbox_write(u32 command, u32 data)
158{
159 if (pcode_ready() < 0) {
160 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
161 return -1;
162 }
163
Angel Pons7811a452021-03-27 20:05:22 +0100164 mchbar_write32(BIOS_MAILBOX_DATA, data);
Angel Pons1c7ba622020-10-29 00:01:29 +0100165
166 /* Send command and start transaction */
Angel Pons7811a452021-03-27 20:05:22 +0100167 mchbar_write32(BIOS_MAILBOX_INTERFACE, command | MAILBOX_RUN_BUSY);
Angel Pons1c7ba622020-10-29 00:01:29 +0100168
169 if (pcode_ready() < 0) {
170 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
171 return -1;
172 }
173
174 return 0;
175}
176
Aaron Durbin16cbf892013-07-03 16:21:28 -0500177static void initialize_vr_config(void)
178{
Angel Pons242fd282020-10-28 23:48:56 +0100179 struct cpu_vr_config vr_config = { 0 };
Aaron Durbin16cbf892013-07-03 16:21:28 -0500180 msr_t msr;
181
Angel Pons242fd282020-10-28 23:48:56 +0100182 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
183
184 if (lapic && lapic->chip_info) {
185 const struct cpu_intel_haswell_config *conf = lapic->chip_info;
186
187 vr_config = conf->vr_config;
188 }
189
Aaron Durbin16cbf892013-07-03 16:21:28 -0500190 printk(BIOS_DEBUG, "Initializing VR config.\n");
191
192 /* Configure VR_CURRENT_CONFIG. */
193 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
194 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
195 * on ULT systems. */
196 msr.hi &= 0xc0000000;
197 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
198 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
Angel Pons9dcd1c12020-10-28 22:41:26 +0100199 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A. */
Aaron Durbin16cbf892013-07-03 16:21:28 -0500200
Duncan Laurie118d1052013-07-09 15:34:25 -0700201 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500202 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
203 /* Leave the max instantaneous current limit (12:0) to default. */
204 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
205
206 /* Configure VR_MISC_CONFIG MSR. */
207 msr = rdmsr(MSR_VR_MISC_CONFIG);
208 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
209 msr.hi &= ~(0x3ff << (40 - 32));
210 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
211 /* Set IOUT_OFFSET to 0. */
212 msr.hi &= ~0xff;
213 /* Set exit ramp rate to fast. */
214 msr.hi |= (1 << (50 - 32));
215 /* Set entry ramp rate to slow. */
216 msr.hi &= ~(1 << (51 - 32));
217 /* Enable decay mode on C-state entry. */
218 msr.hi |= (1 << (52 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100219 /* Set the slow ramp rate */
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300220 if (haswell_is_ult()) {
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300221 msr.hi &= ~(0x3 << (53 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100222 /* Configure the C-state exit ramp rate. */
223 if (vr_config.slow_ramp_rate_enable) {
224 /* Configured slow ramp rate. */
225 msr.hi |= ((vr_config.slow_ramp_rate_set & 0x3) << (53 - 32));
226 /* Set exit ramp rate to slow. */
227 msr.hi &= ~(1 << (50 - 32));
228 } else {
229 /* Fast ramp rate / 4. */
230 msr.hi |= (1 << (53 - 32));
231 }
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300232 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500233 /* Set MIN_VID (31:24) to allow CPU to have full control. */
234 msr.lo &= ~0xff000000;
Angel Pons242fd282020-10-28 23:48:56 +0100235 msr.lo |= (vr_config.cpu_min_vid & 0xff) << 24;
Aaron Durbin16cbf892013-07-03 16:21:28 -0500236 wrmsr(MSR_VR_MISC_CONFIG, msr);
237
238 /* Configure VR_MISC_CONFIG2 MSR. */
Angel Pons4c95f102020-10-28 19:38:12 +0100239 if (!haswell_is_ult())
240 return;
241
242 msr = rdmsr(MSR_VR_MISC_CONFIG2);
243 msr.lo &= ~0xffff;
244 /* Allow CPU to control minimum voltage completely (15:8) and
Angel Ponsc86b1192020-10-28 23:53:45 +0100245 set the fast ramp voltage in 10mV steps. */
246 if (cpu_family_model() == BROADWELL_FAMILY_ULT)
247 msr.lo |= 0x006a; /* 1.56V */
248 else
249 msr.lo |= 0x006f; /* 1.60V */
Angel Pons4c95f102020-10-28 19:38:12 +0100250 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Angel Pons1c7ba622020-10-29 00:01:29 +0100251
252 /* Set C9/C10 VCC Min */
253 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500254}
255
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700256static void configure_pch_power_sharing(void)
257{
258 u32 pch_power, pch_power_ext, pmsync, pmsync2;
259 int i;
260
261 /* Read PCH Power levels from PCODE */
262 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
263 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
264
265 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700266 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700267
268 pmsync = RCBA32(PMSYNC_CONFIG);
269 pmsync2 = RCBA32(PMSYNC_CONFIG2);
270
271 /* Program PMSYNC_TPR_CONFIG PCH power limit values
272 * pmsync[0:4] = mailbox[0:5]
273 * pmsync[8:12] = mailbox[6:11]
274 * pmsync[16:20] = mailbox[12:17]
275 */
276 for (i = 0; i < 3; i++) {
277 u32 level = pch_power & 0x3f;
278 pch_power >>= 6;
279 pmsync &= ~(0x1f << (i * 8));
280 pmsync |= (level & 0x1f) << (i * 8);
281 }
282 RCBA32(PMSYNC_CONFIG) = pmsync;
283
284 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
285 * pmsync2[0:4] = mailbox[23:18]
286 * pmsync2[8:12] = mailbox_ext[6:11]
287 * pmsync2[16:20] = mailbox_ext[12:17]
288 * pmsync2[24:28] = mailbox_ext[18:22]
289 */
290 pmsync2 &= ~0x1f;
291 pmsync2 |= pch_power & 0x1f;
292
293 for (i = 1; i < 4; i++) {
294 u32 level = pch_power_ext & 0x3f;
295 pch_power_ext >>= 6;
296 pmsync2 &= ~(0x1f << (i * 8));
297 pmsync2 |= (level & 0x1f) << (i * 8);
298 }
299 RCBA32(PMSYNC_CONFIG2) = pmsync2;
300}
301
Aaron Durbin76c37002012-10-30 09:03:43 -0500302int cpu_config_tdp_levels(void)
303{
304 msr_t platform_info;
305
306 /* Bits 34:33 indicate how many levels supported */
307 platform_info = rdmsr(MSR_PLATFORM_INFO);
308 return (platform_info.hi >> 1) & 3;
309}
310
311/*
312 * Configure processor power limits if possible
313 * This must be done AFTER set of BIOS_RESET_CPL
314 */
315void set_power_limits(u8 power_limit_1_time)
316{
317 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
318 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700319 unsigned int power_unit;
320 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500321 u8 power_limit_1_val;
322
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000323 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100324 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500325
326 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
327 return;
328
329 /* Get units */
330 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
331 power_unit = 2 << ((msr.lo & 0xf) - 1);
332
333 /* Get power defaults for this SKU */
334 msr = rdmsr(MSR_PKG_POWER_SKU);
335 tdp = msr.lo & 0x7fff;
336 min_power = (msr.lo >> 16) & 0x7fff;
337 max_power = msr.hi & 0x7fff;
338 max_time = (msr.hi >> 16) & 0x7f;
339
340 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
341
342 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
343 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
344
345 if (min_power > 0 && tdp < min_power)
346 tdp = min_power;
347
348 if (max_power > 0 && tdp > max_power)
349 tdp = max_power;
350
351 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
352
353 /* Set long term power limit to TDP */
354 limit.lo = 0;
355 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
356 limit.lo |= PKG_POWER_LIMIT_EN;
357 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
358 PKG_POWER_LIMIT_TIME_SHIFT;
359
360 /* Set short term power limit to 1.25 * TDP */
361 limit.hi = 0;
362 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
363 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700364 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500365
366 wrmsr(MSR_PKG_POWER_LIMIT, limit);
367
Duncan Lauriec70353f2013-06-28 14:40:38 -0700368 /* Set power limit values in MCHBAR as well */
Angel Pons7811a452021-03-27 20:05:22 +0100369 mchbar_write32(MCH_PKG_POWER_LIMIT_LO, limit.lo);
370 mchbar_write32(MCH_PKG_POWER_LIMIT_HI, limit.hi);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700371
372 /* Set DDR RAPL power limit by copying from MMIO to MSR */
Angel Pons7811a452021-03-27 20:05:22 +0100373 msr.lo = mchbar_read32(MCH_DDR_POWER_LIMIT_LO);
374 msr.hi = mchbar_read32(MCH_DDR_POWER_LIMIT_HI);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700375 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
376
Aaron Durbin76c37002012-10-30 09:03:43 -0500377 /* Use nominal TDP values for CPUs with configurable TDP */
378 if (cpu_config_tdp_levels()) {
379 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
380 limit.hi = 0;
381 limit.lo = msr.lo & 0xff;
382 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
383 }
384}
385
Aaron Durbin76c37002012-10-30 09:03:43 -0500386static void configure_c_states(void)
387{
Angel Ponsc89d2a282020-10-28 22:23:02 +0100388 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
389
390 const bool timed_mwait_capable = !!(msr.hi & TIMED_MWAIT_SUPPORTED);
Aaron Durbin76c37002012-10-30 09:03:43 -0500391
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200392 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500393 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
394 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500395 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
396 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
397 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
398 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
Angel Ponscb70d832021-10-11 14:26:42 +0200399 msr.lo |= (1 << 15); // Lock bits 15:0
Aaron Durbin76c37002012-10-30 09:03:43 -0500400 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
Angel Ponsc89d2a282020-10-28 22:23:02 +0100401
402 if (timed_mwait_capable)
403 msr.lo |= (1 << 31); // Timed MWAIT Enable
404
Duncan Laurie1c097102013-05-07 13:19:56 -0700405 /* The deepest package c-state defaults to factory-configured value. */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200406 wrmsr(MSR_PKG_CST_CONFIG_CONTROL, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500407
Aaron Durbin76c37002012-10-30 09:03:43 -0500408 msr = rdmsr(MSR_MISC_PWR_MGMT);
409 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
410 wrmsr(MSR_MISC_PWR_MGMT, msr);
411
412 msr = rdmsr(MSR_POWER_CTL);
413 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
414 msr.lo |= (1 << 1); // C1E Enable
415 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
416 wrmsr(MSR_POWER_CTL, msr);
417
Aaron Durbin7c351312013-04-10 14:46:25 -0500418 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
Aaron Durbin76c37002012-10-30 09:03:43 -0500419 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500420 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
421 wrmsr(MSR_C_STATE_LATENCY_CONTROL_0, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500422
Aaron Durbin7c351312013-04-10 14:46:25 -0500423 /* C-state Interrupt Response Latency Control 1 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500424 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500425 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
426 wrmsr(MSR_C_STATE_LATENCY_CONTROL_1, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500427
Aaron Durbin7c351312013-04-10 14:46:25 -0500428 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
Aaron Durbin76c37002012-10-30 09:03:43 -0500429 msr.hi = 0;
Aaron Durbin7c351312013-04-10 14:46:25 -0500430 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
431 wrmsr(MSR_C_STATE_LATENCY_CONTROL_2, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500432
Angel Pons4c95f102020-10-28 19:38:12 +0100433 /* Only Haswell ULT supports the 3-5 latency response registers */
434 if (!haswell_is_ult())
435 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500436
Angel Pons4c95f102020-10-28 19:38:12 +0100437 /* C-state Interrupt Response Latency Control 3 - package C8 */
438 msr.hi = 0;
439 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
440 wrmsr(MSR_C_STATE_LATENCY_CONTROL_3, msr);
Aaron Durbin7c351312013-04-10 14:46:25 -0500441
Angel Pons4c95f102020-10-28 19:38:12 +0100442 /* C-state Interrupt Response Latency Control 4 - package C9 */
443 msr.hi = 0;
444 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
445 wrmsr(MSR_C_STATE_LATENCY_CONTROL_4, msr);
446
447 /* C-state Interrupt Response Latency Control 5 - package C10 */
448 msr.hi = 0;
449 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
450 wrmsr(MSR_C_STATE_LATENCY_CONTROL_5, msr);
Aaron Durbin76c37002012-10-30 09:03:43 -0500451}
Aaron Durbin76c37002012-10-30 09:03:43 -0500452
453static void configure_thermal_target(void)
454{
455 struct cpu_intel_haswell_config *conf;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100456 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -0500457 msr_t msr;
458
459 /* Find pointer to CPU configuration */
460 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
461 if (!lapic || !lapic->chip_info)
462 return;
463 conf = lapic->chip_info;
464
Martin Roth4c3ab732013-07-08 16:23:54 -0600465 /* Set TCC activation offset if supported */
Aaron Durbin76c37002012-10-30 09:03:43 -0500466 msr = rdmsr(MSR_PLATFORM_INFO);
467 if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
468 msr = rdmsr(MSR_TEMPERATURE_TARGET);
469 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
470 msr.lo |= (conf->tcc_offset & 0xf) << 24;
471 wrmsr(MSR_TEMPERATURE_TARGET, msr);
472 }
473}
474
475static void configure_misc(void)
476{
477 msr_t msr;
478
479 msr = rdmsr(IA32_MISC_ENABLE);
480 msr.lo |= (1 << 0); /* Fast String enable */
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700481 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
Aaron Durbin76c37002012-10-30 09:03:43 -0500482 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
483 wrmsr(IA32_MISC_ENABLE, msr);
484
485 /* Disable Thermal interrupts */
486 msr.lo = 0;
487 msr.hi = 0;
488 wrmsr(IA32_THERM_INTERRUPT, msr);
489
490 /* Enable package critical interrupt only */
491 msr.lo = 1 << 4;
492 msr.hi = 0;
493 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
494}
495
Aaron Durbin76c37002012-10-30 09:03:43 -0500496static void set_max_ratio(void)
497{
498 msr_t msr, perf_ctl;
499
500 perf_ctl.hi = 0;
501
502 /* Check for configurable TDP option */
Angel Pons053deb82020-10-28 22:40:02 +0100503 if (get_turbo_state() == TURBO_ENABLED) {
504 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
505 perf_ctl.lo = (msr.lo & 0xff) << 8;
506 } else if (cpu_config_tdp_levels()) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500507 /* Set to nominal TDP ratio */
508 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
509 perf_ctl.lo = (msr.lo & 0xff) << 8;
510 } else {
511 /* Platform Info bits 15:8 give max ratio */
512 msr = rdmsr(MSR_PLATFORM_INFO);
513 perf_ctl.lo = msr.lo & 0xff00;
514 }
515 wrmsr(IA32_PERF_CTL, perf_ctl);
516
Angel Ponsf6cf49272020-09-25 01:14:24 +0200517 printk(BIOS_DEBUG, "CPU: frequency set to %d\n",
Angel Ponsca965492020-10-28 19:15:36 +0100518 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
Aaron Durbin76c37002012-10-30 09:03:43 -0500519}
520
Aaron Durbin76c37002012-10-30 09:03:43 -0500521static void configure_mca(void)
522{
523 msr_t msr;
524 int i;
Felix Heldbad21a42021-07-13 01:55:52 +0200525 const unsigned int num_banks = mca_get_bank_count();
Angel Pons1515a482021-06-13 22:33:06 +0200526
527 /* Enable all error reporting */
528 msr.lo = msr.hi = ~0;
529 for (i = 0; i < num_banks; i++)
Felix Held1b46e762021-07-13 00:54:32 +0200530 wrmsr(IA32_MC_CTL(i), msr);
Angel Pons1515a482021-06-13 22:33:06 +0200531
Aaron Durbin24614af2013-01-12 01:07:28 -0600532 /* TODO(adurbin): This should only be done on a cold boot. Also, some
533 * of these banks are core vs package scope. For now every CPU clears
534 * every bank. */
Felix Heldacbf1542021-07-13 16:44:18 +0200535 mca_clear_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500536}
537
Aaron Durbin305b1f02013-01-15 08:27:05 -0600538/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100539static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600540{
541 /* Clear out pending MCEs */
542 configure_mca();
543
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200544 /* Enable the local CPU APICs */
Aaron Durbin76c37002012-10-30 09:03:43 -0500545 enable_lapic_tpr();
546 setup_lapic();
547
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600548 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600549 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500550
Aaron Durbin76c37002012-10-30 09:03:43 -0500551 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500552 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500553
554 /* Configure Enhanced SpeedStep and Thermal Sensors */
555 configure_misc();
556
557 /* Thermal throttle activation offset */
558 configure_thermal_target();
559
560 /* Enable Direct Cache Access */
561 configure_dca_cap();
562
563 /* Set energy policy */
564 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
565
Aaron Durbin76c37002012-10-30 09:03:43 -0500566 /* Enable Turbo */
567 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600568}
Aaron Durbin76c37002012-10-30 09:03:43 -0500569
Aaron Durbin014baea2014-03-28 22:01:05 -0500570/* MP initialization support. */
571static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500572
Aaron Durbin463af332016-05-03 17:26:35 -0500573static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500574{
Aaron Durbin463af332016-05-03 17:26:35 -0500575 /* Setup MTRRs based on physical address size. */
576 x86_setup_mtrrs_with_detect();
577 x86_mtrr_check();
578
579 initialize_vr_config();
580
Angel Pons4c95f102020-10-28 19:38:12 +0100581 if (!haswell_is_ult())
582 return;
583
584 calibrate_24mhz_bclk();
585 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500586}
587
Aaron Durbin463af332016-05-03 17:26:35 -0500588static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500589{
Aaron Durbin463af332016-05-03 17:26:35 -0500590 msr_t msr;
Aaron Durbin014baea2014-03-28 22:01:05 -0500591 int num_threads;
592 int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500593
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200594 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500595 num_threads = (msr.lo >> 0) & 0xffff;
596 num_cores = (msr.lo >> 16) & 0xffff;
597 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
598 num_cores, num_threads);
599
Aaron Durbin463af332016-05-03 17:26:35 -0500600 return num_threads;
601}
Aaron Durbin7af20692013-01-14 14:54:41 -0600602
Aaron Durbin463af332016-05-03 17:26:35 -0500603static void get_microcode_info(const void **microcode, int *parallel)
604{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600605 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500606 *microcode = microcode_patch;
607 *parallel = 1;
608}
Aaron Durbin7af20692013-01-14 14:54:41 -0600609
Aaron Durbin463af332016-05-03 17:26:35 -0500610static void per_cpu_smm_trigger(void)
611{
612 /* Relocate the SMM handler. */
613 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600614
Aaron Durbin463af332016-05-03 17:26:35 -0500615 /* After SMM relocation a 2nd microcode load is required. */
616 intel_microcode_load_unlocked(microcode_patch);
617}
618
619static void post_mp_init(void)
620{
Angel Pons053deb82020-10-28 22:40:02 +0100621 /* Set Max Ratio */
622 set_max_ratio();
623
Aaron Durbin463af332016-05-03 17:26:35 -0500624 /* Now that all APs have been relocated as well as the BSP let SMIs
625 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300626 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500627
628 /* Lock down the SMRAM space. */
629 smm_lock();
630}
631
632static const struct mp_ops mp_ops = {
633 .pre_mp_init = pre_mp_init,
634 .get_cpu_count = get_cpu_count,
635 .get_smm_info = smm_info,
636 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500637 .pre_mp_smm_init = smm_initialize,
638 .per_cpu_smm_trigger = per_cpu_smm_trigger,
639 .relocation_handler = smm_relocation_handler,
640 .post_mp_init = post_mp_init,
641};
642
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300643void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500644{
Felix Held4dd7d112021-10-20 23:31:43 +0200645 /* TODO: Handle mp_init_with_smm failure? */
646 mp_init_with_smm(cpu_bus, &mp_ops);
Aaron Durbin76c37002012-10-30 09:03:43 -0500647}
648
649static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100650 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500651};
652
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100653static const struct cpu_device_id cpu_table[] = {
Angel Pons8b0636e2020-10-28 21:48:29 +0100654 { X86_VENDOR_INTEL, CPUID_HASWELL_A0 },
655 { X86_VENDOR_INTEL, CPUID_HASWELL_B0 },
656 { X86_VENDOR_INTEL, CPUID_HASWELL_C0 },
657 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_B0 },
658 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_C0 },
659 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_B0 },
660 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_C0 },
Angel Ponsf542b7b2020-10-29 01:02:03 +0100661 { X86_VENDOR_INTEL, CPUID_BROADWELL_C0 },
662 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_C0 },
663 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_D0 },
664 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_E0 },
Aaron Durbin76c37002012-10-30 09:03:43 -0500665 { 0, 0 },
666};
667
668static const struct cpu_driver driver __cpu_driver = {
669 .ops = &cpu_dev_ops,
670 .id_table = cpu_table,
Aaron Durbin76c37002012-10-30 09:03:43 -0500671};