blob: 7b3f3ad6cfc1cfcdbf2abf8fa3e6f54a1c4d74e8 [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>
Aaron Durbin76c37002012-10-30 09:03:43 -05005#include <cpu/cpu.h>
6#include <cpu/x86/mtrr.h>
7#include <cpu/x86/msr.h>
Aaron Durbin014baea2014-03-28 22:01:05 -05008#include <cpu/x86/mp.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05009#include <cpu/intel/microcode.h>
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +030010#include <cpu/intel/smm_reloc.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050011#include <cpu/intel/speedstep.h>
12#include <cpu/intel/turbo.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050013#include <cpu/x86/name.h>
Aaron Durbinf24262d2013-04-10 14:59:21 -050014#include <delay.h>
Aaron Durbin7c351312013-04-10 14:46:25 -050015#include <northbridge/intel/haswell/haswell.h>
16#include <southbridge/intel/lynxpoint/pch.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060017#include <cpu/intel/common/common.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020018#include <types.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include "haswell.h"
20#include "chip.h"
21
Aaron Durbin76c37002012-10-30 09:03:43 -050022/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
23static const u8 power_limit_time_sec_to_msr[] = {
24 [0] = 0x00,
25 [1] = 0x0a,
26 [2] = 0x0b,
27 [3] = 0x4b,
28 [4] = 0x0c,
29 [5] = 0x2c,
30 [6] = 0x4c,
31 [7] = 0x6c,
32 [8] = 0x0d,
33 [10] = 0x2d,
34 [12] = 0x4d,
35 [14] = 0x6d,
36 [16] = 0x0e,
37 [20] = 0x2e,
38 [24] = 0x4e,
39 [28] = 0x6e,
40 [32] = 0x0f,
41 [40] = 0x2f,
42 [48] = 0x4f,
43 [56] = 0x6f,
44 [64] = 0x10,
45 [80] = 0x30,
46 [96] = 0x50,
47 [112] = 0x70,
48 [128] = 0x11,
49};
50
51/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
52static const u8 power_limit_time_msr_to_sec[] = {
53 [0x00] = 0,
54 [0x0a] = 1,
55 [0x0b] = 2,
56 [0x4b] = 3,
57 [0x0c] = 4,
58 [0x2c] = 5,
59 [0x4c] = 6,
60 [0x6c] = 7,
61 [0x0d] = 8,
62 [0x2d] = 10,
63 [0x4d] = 12,
64 [0x6d] = 14,
65 [0x0e] = 16,
66 [0x2e] = 20,
67 [0x4e] = 24,
68 [0x6e] = 28,
69 [0x0f] = 32,
70 [0x2f] = 40,
71 [0x4f] = 48,
72 [0x6f] = 56,
73 [0x10] = 64,
74 [0x30] = 80,
75 [0x50] = 96,
76 [0x70] = 112,
77 [0x11] = 128,
78};
79
Angel Pons5d92aa52020-10-14 00:02:37 +020080/* The core 100MHz BCLK is disabled in deeper c-states. One needs to calibrate
81 * the 100MHz BCLK against the 24MHz BCLK to restore the clocks properly
Aaron Durbinf24262d2013-04-10 14:59:21 -050082 * when a core is woken up. */
83static int pcode_ready(void)
84{
85 int wait_count;
86 const int delay_step = 10;
87
88 wait_count = 0;
89 do {
Angel Pons7811a452021-03-27 20:05:22 +010090 if (!(mchbar_read32(BIOS_MAILBOX_INTERFACE) & MAILBOX_RUN_BUSY))
Aaron Durbinf24262d2013-04-10 14:59:21 -050091 return 0;
92 wait_count += delay_step;
93 udelay(delay_step);
94 } while (wait_count < 1000);
95
96 return -1;
97}
98
99static void calibrate_24mhz_bclk(void)
100{
101 int err_code;
102
103 if (pcode_ready() < 0) {
104 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
105 return;
106 }
107
108 /* A non-zero value initiates the PCODE calibration. */
Angel Pons7811a452021-03-27 20:05:22 +0100109 mchbar_write32(BIOS_MAILBOX_DATA, ~0);
110 mchbar_write32(BIOS_MAILBOX_INTERFACE,
111 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL);
Aaron Durbinf24262d2013-04-10 14:59:21 -0500112
113 if (pcode_ready() < 0) {
114 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
115 return;
116 }
117
Angel Pons7811a452021-03-27 20:05:22 +0100118 err_code = mchbar_read32(BIOS_MAILBOX_INTERFACE) & 0xff;
Aaron Durbinf24262d2013-04-10 14:59:21 -0500119
Angel Pons5d92aa52020-10-14 00:02:37 +0200120 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration response: %d\n",
Aaron Durbinf24262d2013-04-10 14:59:21 -0500121 err_code);
122
123 /* Read the calibrated value. */
Angel Pons7811a452021-03-27 20:05:22 +0100124 mchbar_write32(BIOS_MAILBOX_INTERFACE,
125 MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION);
Aaron Durbinf24262d2013-04-10 14:59:21 -0500126
127 if (pcode_ready() < 0) {
128 printk(BIOS_ERR, "PCODE: mailbox timeout on read.\n");
129 return;
130 }
131
Angel Pons5d92aa52020-10-14 00:02:37 +0200132 printk(BIOS_DEBUG, "PCODE: 24MHz BCLK calibration value: 0x%08x\n",
Angel Pons7811a452021-03-27 20:05:22 +0100133 mchbar_read32(BIOS_MAILBOX_DATA));
Aaron Durbinf24262d2013-04-10 14:59:21 -0500134}
135
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700136static u32 pcode_mailbox_read(u32 command)
137{
138 if (pcode_ready() < 0) {
139 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
140 return 0;
141 }
142
143 /* Send command and start transaction */
Angel Pons7811a452021-03-27 20:05:22 +0100144 mchbar_write32(BIOS_MAILBOX_INTERFACE, command | MAILBOX_RUN_BUSY);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700145
146 if (pcode_ready() < 0) {
147 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
148 return 0;
149 }
150
151 /* Read mailbox */
Angel Pons7811a452021-03-27 20:05:22 +0100152 return mchbar_read32(BIOS_MAILBOX_DATA);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700153}
154
Angel Pons1c7ba622020-10-29 00:01:29 +0100155static int pcode_mailbox_write(u32 command, u32 data)
156{
157 if (pcode_ready() < 0) {
158 printk(BIOS_ERR, "PCODE: mailbox timeout on wait ready.\n");
159 return -1;
160 }
161
Angel Pons7811a452021-03-27 20:05:22 +0100162 mchbar_write32(BIOS_MAILBOX_DATA, data);
Angel Pons1c7ba622020-10-29 00:01:29 +0100163
164 /* Send command and start transaction */
Angel Pons7811a452021-03-27 20:05:22 +0100165 mchbar_write32(BIOS_MAILBOX_INTERFACE, command | MAILBOX_RUN_BUSY);
Angel Pons1c7ba622020-10-29 00:01:29 +0100166
167 if (pcode_ready() < 0) {
168 printk(BIOS_ERR, "PCODE: mailbox timeout on completion.\n");
169 return -1;
170 }
171
172 return 0;
173}
174
Aaron Durbin16cbf892013-07-03 16:21:28 -0500175static void initialize_vr_config(void)
176{
Angel Pons242fd282020-10-28 23:48:56 +0100177 struct cpu_vr_config vr_config = { 0 };
Aaron Durbin16cbf892013-07-03 16:21:28 -0500178 msr_t msr;
179
Angel Pons242fd282020-10-28 23:48:56 +0100180 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
181
182 if (lapic && lapic->chip_info) {
183 const struct cpu_intel_haswell_config *conf = lapic->chip_info;
184
185 vr_config = conf->vr_config;
186 }
187
Aaron Durbin16cbf892013-07-03 16:21:28 -0500188 printk(BIOS_DEBUG, "Initializing VR config.\n");
189
190 /* Configure VR_CURRENT_CONFIG. */
191 msr = rdmsr(MSR_VR_CURRENT_CONFIG);
192 /* Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
193 * on ULT systems. */
194 msr.hi &= 0xc0000000;
195 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A. */
196 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A. */
Angel Pons9dcd1c12020-10-28 22:41:26 +0100197 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A. */
Aaron Durbin16cbf892013-07-03 16:21:28 -0500198
Duncan Laurie118d1052013-07-09 15:34:25 -0700199 if (haswell_is_ult())
Aaron Durbin16cbf892013-07-03 16:21:28 -0500200 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
201 /* Leave the max instantaneous current limit (12:0) to default. */
202 wrmsr(MSR_VR_CURRENT_CONFIG, msr);
203
204 /* Configure VR_MISC_CONFIG MSR. */
205 msr = rdmsr(MSR_VR_MISC_CONFIG);
206 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format. */
207 msr.hi &= ~(0x3ff << (40 - 32));
208 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
209 /* Set IOUT_OFFSET to 0. */
210 msr.hi &= ~0xff;
211 /* Set exit ramp rate to fast. */
212 msr.hi |= (1 << (50 - 32));
213 /* Set entry ramp rate to slow. */
214 msr.hi &= ~(1 << (51 - 32));
215 /* Enable decay mode on C-state entry. */
216 msr.hi |= (1 << (52 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100217 /* Set the slow ramp rate */
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300218 if (haswell_is_ult()) {
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300219 msr.hi &= ~(0x3 << (53 - 32));
Angel Pons242fd282020-10-28 23:48:56 +0100220 /* Configure the C-state exit ramp rate. */
221 if (vr_config.slow_ramp_rate_enable) {
222 /* Configured slow ramp rate. */
223 msr.hi |= ((vr_config.slow_ramp_rate_set & 0x3) << (53 - 32));
224 /* Set exit ramp rate to slow. */
225 msr.hi &= ~(1 << (50 - 32));
226 } else {
227 /* Fast ramp rate / 4. */
228 msr.hi |= (1 << (53 - 32));
229 }
Tristan Corrickfdf907e2018-10-31 02:27:12 +1300230 }
Aaron Durbin16cbf892013-07-03 16:21:28 -0500231 /* Set MIN_VID (31:24) to allow CPU to have full control. */
232 msr.lo &= ~0xff000000;
Angel Pons242fd282020-10-28 23:48:56 +0100233 msr.lo |= (vr_config.cpu_min_vid & 0xff) << 24;
Aaron Durbin16cbf892013-07-03 16:21:28 -0500234 wrmsr(MSR_VR_MISC_CONFIG, msr);
235
236 /* Configure VR_MISC_CONFIG2 MSR. */
Angel Pons4c95f102020-10-28 19:38:12 +0100237 if (!haswell_is_ult())
238 return;
239
240 msr = rdmsr(MSR_VR_MISC_CONFIG2);
241 msr.lo &= ~0xffff;
242 /* Allow CPU to control minimum voltage completely (15:8) and
Angel Ponsc86b1192020-10-28 23:53:45 +0100243 set the fast ramp voltage in 10mV steps. */
244 if (cpu_family_model() == BROADWELL_FAMILY_ULT)
245 msr.lo |= 0x006a; /* 1.56V */
246 else
247 msr.lo |= 0x006f; /* 1.60V */
Angel Pons4c95f102020-10-28 19:38:12 +0100248 wrmsr(MSR_VR_MISC_CONFIG2, msr);
Angel Pons1c7ba622020-10-29 00:01:29 +0100249
250 /* Set C9/C10 VCC Min */
251 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
Aaron Durbin16cbf892013-07-03 16:21:28 -0500252}
253
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700254static void configure_pch_power_sharing(void)
255{
256 u32 pch_power, pch_power_ext, pmsync, pmsync2;
257 int i;
258
259 /* Read PCH Power levels from PCODE */
260 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
261 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
262
263 printk(BIOS_INFO, "PCH Power: PCODE Levels 0x%08x 0x%08x\n",
Lee Leahy7b5f12b92017-03-15 17:16:59 -0700264 pch_power, pch_power_ext);
Duncan Lauriee1e87e02013-04-26 10:35:19 -0700265
266 pmsync = RCBA32(PMSYNC_CONFIG);
267 pmsync2 = RCBA32(PMSYNC_CONFIG2);
268
269 /* Program PMSYNC_TPR_CONFIG PCH power limit values
270 * pmsync[0:4] = mailbox[0:5]
271 * pmsync[8:12] = mailbox[6:11]
272 * pmsync[16:20] = mailbox[12:17]
273 */
274 for (i = 0; i < 3; i++) {
275 u32 level = pch_power & 0x3f;
276 pch_power >>= 6;
277 pmsync &= ~(0x1f << (i * 8));
278 pmsync |= (level & 0x1f) << (i * 8);
279 }
280 RCBA32(PMSYNC_CONFIG) = pmsync;
281
282 /* Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
283 * pmsync2[0:4] = mailbox[23:18]
284 * pmsync2[8:12] = mailbox_ext[6:11]
285 * pmsync2[16:20] = mailbox_ext[12:17]
286 * pmsync2[24:28] = mailbox_ext[18:22]
287 */
288 pmsync2 &= ~0x1f;
289 pmsync2 |= pch_power & 0x1f;
290
291 for (i = 1; i < 4; i++) {
292 u32 level = pch_power_ext & 0x3f;
293 pch_power_ext >>= 6;
294 pmsync2 &= ~(0x1f << (i * 8));
295 pmsync2 |= (level & 0x1f) << (i * 8);
296 }
297 RCBA32(PMSYNC_CONFIG2) = pmsync2;
298}
299
Aaron Durbin76c37002012-10-30 09:03:43 -0500300int cpu_config_tdp_levels(void)
301{
302 msr_t platform_info;
303
304 /* Bits 34:33 indicate how many levels supported */
305 platform_info = rdmsr(MSR_PLATFORM_INFO);
306 return (platform_info.hi >> 1) & 3;
307}
308
309/*
310 * Configure processor power limits if possible
311 * This must be done AFTER set of BIOS_RESET_CPL
312 */
313void set_power_limits(u8 power_limit_1_time)
314{
315 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
316 msr_t limit;
Lee Leahy73a28942017-03-15 17:52:06 -0700317 unsigned int power_unit;
318 unsigned int tdp, min_power, max_power, max_time;
Aaron Durbin76c37002012-10-30 09:03:43 -0500319 u8 power_limit_1_val;
320
Edward O'Callaghan5cfef132014-08-03 20:00:47 +1000321 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
Angel Pons4c95f102020-10-28 19:38:12 +0100322 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500323
324 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
325 return;
326
327 /* Get units */
328 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
329 power_unit = 2 << ((msr.lo & 0xf) - 1);
330
331 /* Get power defaults for this SKU */
332 msr = rdmsr(MSR_PKG_POWER_SKU);
333 tdp = msr.lo & 0x7fff;
334 min_power = (msr.lo >> 16) & 0x7fff;
335 max_power = msr.hi & 0x7fff;
336 max_time = (msr.hi >> 16) & 0x7f;
337
338 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
339
340 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
341 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
342
343 if (min_power > 0 && tdp < min_power)
344 tdp = min_power;
345
346 if (max_power > 0 && tdp > max_power)
347 tdp = max_power;
348
349 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
350
351 /* Set long term power limit to TDP */
352 limit.lo = 0;
353 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
354 limit.lo |= PKG_POWER_LIMIT_EN;
355 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
356 PKG_POWER_LIMIT_TIME_SHIFT;
357
358 /* Set short term power limit to 1.25 * TDP */
359 limit.hi = 0;
360 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
361 limit.hi |= PKG_POWER_LIMIT_EN;
Duncan Lauriec70353f2013-06-28 14:40:38 -0700362 /* Power limit 2 time is only programmable on server SKU */
Aaron Durbin76c37002012-10-30 09:03:43 -0500363
364 wrmsr(MSR_PKG_POWER_LIMIT, limit);
365
Duncan Lauriec70353f2013-06-28 14:40:38 -0700366 /* Set power limit values in MCHBAR as well */
Angel Pons7811a452021-03-27 20:05:22 +0100367 mchbar_write32(MCH_PKG_POWER_LIMIT_LO, limit.lo);
368 mchbar_write32(MCH_PKG_POWER_LIMIT_HI, limit.hi);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700369
370 /* Set DDR RAPL power limit by copying from MMIO to MSR */
Angel Pons7811a452021-03-27 20:05:22 +0100371 msr.lo = mchbar_read32(MCH_DDR_POWER_LIMIT_LO);
372 msr.hi = mchbar_read32(MCH_DDR_POWER_LIMIT_HI);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700373 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
374
Aaron Durbin76c37002012-10-30 09:03:43 -0500375 /* Use nominal TDP values for CPUs with configurable TDP */
376 if (cpu_config_tdp_levels()) {
377 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
378 limit.hi = 0;
379 limit.lo = msr.lo & 0xff;
380 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
381 }
382}
383
Aaron Durbin76c37002012-10-30 09:03:43 -0500384static void configure_c_states(void)
385{
Angel Ponsc89d2a282020-10-28 22:23:02 +0100386 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
387
388 const bool timed_mwait_capable = !!(msr.hi & TIMED_MWAIT_SUPPORTED);
Aaron Durbin76c37002012-10-30 09:03:43 -0500389
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +0200390 msr = rdmsr(MSR_PKG_CST_CONFIG_CONTROL);
Aaron Durbin7c351312013-04-10 14:46:25 -0500391 msr.lo |= (1 << 30); // Package c-state Undemotion Enable
392 msr.lo |= (1 << 29); // Package c-state Demotion Enable
Aaron Durbin76c37002012-10-30 09:03:43 -0500393 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
394 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
395 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
396 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
Angel Ponscb70d832021-10-11 14:26:42 +0200397 msr.lo |= (1 << 15); // Lock bits 15:0
Aaron Durbin76c37002012-10-30 09:03:43 -0500398 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;
Felix Heldbad21a42021-07-13 01:55:52 +0200523 const unsigned int num_banks = mca_get_bank_count();
Angel Pons1515a482021-06-13 22:33:06 +0200524
525 /* Enable all error reporting */
526 msr.lo = msr.hi = ~0;
527 for (i = 0; i < num_banks; i++)
Felix Held1b46e762021-07-13 00:54:32 +0200528 wrmsr(IA32_MC_CTL(i), msr);
Angel Pons1515a482021-06-13 22:33:06 +0200529
Aaron Durbin24614af2013-01-12 01:07:28 -0600530 /* TODO(adurbin): This should only be done on a cold boot. Also, some
531 * of these banks are core vs package scope. For now every CPU clears
532 * every bank. */
Felix Heldacbf1542021-07-13 16:44:18 +0200533 mca_clear_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500534}
535
Aaron Durbin305b1f02013-01-15 08:27:05 -0600536/* All CPUs including BSP will run the following function. */
Angel Pons4c95f102020-10-28 19:38:12 +0100537static void cpu_core_init(struct device *cpu)
Aaron Durbin7af20692013-01-14 14:54:41 -0600538{
539 /* Clear out pending MCEs */
540 configure_mca();
541
Aaron Durbin76c37002012-10-30 09:03:43 -0500542 enable_lapic_tpr();
Aaron Durbin76c37002012-10-30 09:03:43 -0500543
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600544 /* Set virtualization based on Kconfig option */
Matt DeVillierf9aed652018-12-15 15:57:33 -0600545 set_vmx_and_lock();
Matt DeVillierb2a14fb2014-07-07 18:48:16 -0500546
Aaron Durbin76c37002012-10-30 09:03:43 -0500547 /* Configure C States */
Aaron Durbin7c351312013-04-10 14:46:25 -0500548 configure_c_states();
Aaron Durbin76c37002012-10-30 09:03:43 -0500549
550 /* Configure Enhanced SpeedStep and Thermal Sensors */
551 configure_misc();
552
553 /* Thermal throttle activation offset */
554 configure_thermal_target();
555
556 /* Enable Direct Cache Access */
557 configure_dca_cap();
558
559 /* Set energy policy */
560 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
561
Aaron Durbin76c37002012-10-30 09:03:43 -0500562 /* Enable Turbo */
563 enable_turbo();
Aaron Durbin7af20692013-01-14 14:54:41 -0600564}
Aaron Durbin76c37002012-10-30 09:03:43 -0500565
Aaron Durbin014baea2014-03-28 22:01:05 -0500566/* MP initialization support. */
567static const void *microcode_patch;
Aaron Durbin014baea2014-03-28 22:01:05 -0500568
Aaron Durbin463af332016-05-03 17:26:35 -0500569static void pre_mp_init(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500570{
Aaron Durbin463af332016-05-03 17:26:35 -0500571 /* Setup MTRRs based on physical address size. */
572 x86_setup_mtrrs_with_detect();
573 x86_mtrr_check();
574
575 initialize_vr_config();
576
Angel Pons4c95f102020-10-28 19:38:12 +0100577 if (!haswell_is_ult())
578 return;
579
580 calibrate_24mhz_bclk();
581 configure_pch_power_sharing();
Aaron Durbin014baea2014-03-28 22:01:05 -0500582}
583
Aaron Durbin463af332016-05-03 17:26:35 -0500584static int get_cpu_count(void)
Aaron Durbin014baea2014-03-28 22:01:05 -0500585{
Aaron Durbin463af332016-05-03 17:26:35 -0500586 msr_t msr;
Angel Pons04c497a2021-11-03 16:30:10 +0100587 unsigned int num_threads;
588 unsigned int num_cores;
Aaron Durbin014baea2014-03-28 22:01:05 -0500589
Elyes HAOUASa6a396d2019-05-26 13:25:30 +0200590 msr = rdmsr(MSR_CORE_THREAD_COUNT);
Aaron Durbin014baea2014-03-28 22:01:05 -0500591 num_threads = (msr.lo >> 0) & 0xffff;
592 num_cores = (msr.lo >> 16) & 0xffff;
593 printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
594 num_cores, num_threads);
595
Aaron Durbin463af332016-05-03 17:26:35 -0500596 return num_threads;
597}
Aaron Durbin7af20692013-01-14 14:54:41 -0600598
Aaron Durbin463af332016-05-03 17:26:35 -0500599static void get_microcode_info(const void **microcode, int *parallel)
600{
Aaron Durbin305b1f02013-01-15 08:27:05 -0600601 microcode_patch = intel_microcode_find();
Aaron Durbin463af332016-05-03 17:26:35 -0500602 *microcode = microcode_patch;
603 *parallel = 1;
604}
Aaron Durbin7af20692013-01-14 14:54:41 -0600605
Aaron Durbin463af332016-05-03 17:26:35 -0500606static void per_cpu_smm_trigger(void)
607{
608 /* Relocate the SMM handler. */
609 smm_relocate();
Aaron Durbin305b1f02013-01-15 08:27:05 -0600610
Aaron Durbin463af332016-05-03 17:26:35 -0500611 /* After SMM relocation a 2nd microcode load is required. */
612 intel_microcode_load_unlocked(microcode_patch);
613}
614
615static void post_mp_init(void)
616{
Angel Pons053deb82020-10-28 22:40:02 +0100617 /* Set Max Ratio */
618 set_max_ratio();
619
Aaron Durbin463af332016-05-03 17:26:35 -0500620 /* Now that all APs have been relocated as well as the BSP let SMIs
621 * start flowing. */
Kyösti Mälkki0778c862020-06-10 12:44:03 +0300622 global_smi_enable();
Aaron Durbin463af332016-05-03 17:26:35 -0500623
624 /* Lock down the SMRAM space. */
625 smm_lock();
626}
627
628static const struct mp_ops mp_ops = {
629 .pre_mp_init = pre_mp_init,
630 .get_cpu_count = get_cpu_count,
631 .get_smm_info = smm_info,
632 .get_microcode_info = get_microcode_info,
Aaron Durbin463af332016-05-03 17:26:35 -0500633 .pre_mp_smm_init = smm_initialize,
634 .per_cpu_smm_trigger = per_cpu_smm_trigger,
635 .relocation_handler = smm_relocation_handler,
636 .post_mp_init = post_mp_init,
637};
638
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300639void mp_init_cpus(struct bus *cpu_bus)
Aaron Durbin463af332016-05-03 17:26:35 -0500640{
Felix Held4dd7d112021-10-20 23:31:43 +0200641 /* TODO: Handle mp_init_with_smm failure? */
642 mp_init_with_smm(cpu_bus, &mp_ops);
Aaron Durbin76c37002012-10-30 09:03:43 -0500643}
644
645static struct device_operations cpu_dev_ops = {
Angel Pons4c95f102020-10-28 19:38:12 +0100646 .init = cpu_core_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500647};
648
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100649static const struct cpu_device_id cpu_table[] = {
Angel Pons8b0636e2020-10-28 21:48:29 +0100650 { X86_VENDOR_INTEL, CPUID_HASWELL_A0 },
651 { X86_VENDOR_INTEL, CPUID_HASWELL_B0 },
652 { X86_VENDOR_INTEL, CPUID_HASWELL_C0 },
653 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_B0 },
654 { X86_VENDOR_INTEL, CPUID_HASWELL_ULT_C0 },
655 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_B0 },
656 { X86_VENDOR_INTEL, CPUID_CRYSTALWELL_C0 },
Angel Ponsf542b7b2020-10-29 01:02:03 +0100657 { X86_VENDOR_INTEL, CPUID_BROADWELL_C0 },
658 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_C0 },
659 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_D0 },
660 { X86_VENDOR_INTEL, CPUID_BROADWELL_ULT_E0 },
Aaron Durbin76c37002012-10-30 09:03:43 -0500661 { 0, 0 },
662};
663
664static const struct cpu_driver driver __cpu_driver = {
665 .ops = &cpu_dev_ops,
666 .id_table = cpu_table,
Aaron Durbin76c37002012-10-30 09:03:43 -0500667};