1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Google, Inc
5 * Based on code from coreboot src/soc/intel/broadwell/cpu.c
14 #include <asm/cpu_x86.h>
15 #include <asm/cpu_common.h>
16 #include <asm/intel_regs.h>
19 #include <asm/turbo.h>
20 #include <asm/arch/cpu.h>
21 #include <asm/arch/pch.h>
22 #include <asm/arch/rcb.h>
23 #include <linux/delay.h>
25 struct cpu_broadwell_priv {
29 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
30 static const u8 power_limit_time_sec_to_msr[] = {
58 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
59 static const u8 power_limit_time_msr_to_sec[] = {
87 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
88 int arch_cpu_init(void)
95 * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
96 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
97 * when a core is woken up
99 static int pcode_ready(void)
102 const int delay_step = 10;
106 if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) &
109 wait_count += delay_step;
111 } while (wait_count < 1000);
116 static u32 pcode_mailbox_read(u32 command)
122 debug("PCODE: mailbox timeout on wait ready\n");
126 /* Send command and start transaction */
127 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
131 debug("PCODE: mailbox timeout on completion\n");
136 return readl(MCHBAR_REG(BIOS_MAILBOX_DATA));
139 static int pcode_mailbox_write(u32 command, u32 data)
145 debug("PCODE: mailbox timeout on wait ready\n");
149 writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA));
151 /* Send command and start transaction */
152 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
156 debug("PCODE: mailbox timeout on completion\n");
163 /* @dev is the CPU device */
164 static void initialize_vr_config(struct udevice *dev)
169 debug("Initializing VR config\n");
171 /* Configure VR_CURRENT_CONFIG */
172 msr = msr_read(MSR_VR_CURRENT_CONFIG);
174 * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
177 msr.hi &= 0xc0000000;
178 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */
179 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */
180 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */
181 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
182 /* Leave the max instantaneous current limit (12:0) to default */
183 msr_write(MSR_VR_CURRENT_CONFIG, msr);
185 /* Configure VR_MISC_CONFIG MSR */
186 msr = msr_read(MSR_VR_MISC_CONFIG);
187 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */
188 msr.hi &= ~(0x3ff << (40 - 32));
189 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
190 /* Set IOUT_OFFSET to 0 */
192 /* Set entry ramp rate to slow */
193 msr.hi &= ~(1 << (51 - 32));
194 /* Enable decay mode on C-state entry */
195 msr.hi |= (1 << (52 - 32));
196 /* Set the slow ramp rate */
197 msr.hi &= ~(0x3 << (53 - 32));
198 /* Configure the C-state exit ramp rate */
199 ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
200 "intel,slow-ramp", -1);
202 /* Configured slow ramp rate */
203 msr.hi |= ((ramp & 0x3) << (53 - 32));
204 /* Set exit ramp rate to slow */
205 msr.hi &= ~(1 << (50 - 32));
207 /* Fast ramp rate / 4 */
208 msr.hi |= (0x01 << (53 - 32));
209 /* Set exit ramp rate to fast */
210 msr.hi |= (1 << (50 - 32));
212 /* Set MIN_VID (31:24) to allow CPU to have full control */
213 msr.lo &= ~0xff000000;
214 min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
216 msr.lo |= (min_vid & 0xff) << 24;
217 msr_write(MSR_VR_MISC_CONFIG, msr);
219 /* Configure VR_MISC_CONFIG2 MSR */
220 msr = msr_read(MSR_VR_MISC_CONFIG2);
223 * Allow CPU to control minimum voltage completely (15:8) and
224 * set the fast ramp voltage in 10mV steps
226 if (cpu_get_family_model() == BROADWELL_FAMILY_ULT)
227 msr.lo |= 0x006a; /* 1.56V */
229 msr.lo |= 0x006f; /* 1.60V */
230 msr_write(MSR_VR_MISC_CONFIG2, msr);
232 /* Set C9/C10 VCC Min */
233 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
236 static int calibrate_24mhz_bclk(void)
245 /* A non-zero value initiates the PCODE calibration */
246 writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA));
247 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL,
248 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
254 err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff;
256 debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code);
258 /* Read the calibrated value */
259 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION,
260 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
266 debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n",
267 readl(MCHBAR_REG(BIOS_MAILBOX_DATA)));
272 static void configure_pch_power_sharing(void)
274 u32 pch_power, pch_power_ext, pmsync, pmsync2;
277 /* Read PCH Power levels from PCODE */
278 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
279 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
281 debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power,
284 pmsync = readl(RCB_REG(PMSYNC_CONFIG));
285 pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2));
288 * Program PMSYNC_TPR_CONFIG PCH power limit values
289 * pmsync[0:4] = mailbox[0:5]
290 * pmsync[8:12] = mailbox[6:11]
291 * pmsync[16:20] = mailbox[12:17]
293 for (i = 0; i < 3; i++) {
294 u32 level = pch_power & 0x3f;
297 pmsync &= ~(0x1f << (i * 8));
298 pmsync |= (level & 0x1f) << (i * 8);
300 writel(pmsync, RCB_REG(PMSYNC_CONFIG));
303 * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
304 * pmsync2[0:4] = mailbox[23:18]
305 * pmsync2[8:12] = mailbox_ext[6:11]
306 * pmsync2[16:20] = mailbox_ext[12:17]
307 * pmsync2[24:28] = mailbox_ext[18:22]
310 pmsync2 |= pch_power & 0x1f;
312 for (i = 1; i < 4; i++) {
313 u32 level = pch_power_ext & 0x3f;
316 pmsync2 &= ~(0x1f << (i * 8));
317 pmsync2 |= (level & 0x1f) << (i * 8);
319 writel(pmsync2, RCB_REG(PMSYNC_CONFIG2));
322 static int bsp_init_before_ap_bringup(struct udevice *dev)
326 initialize_vr_config(dev);
327 ret = calibrate_24mhz_bclk();
330 configure_pch_power_sharing();
335 static void set_max_ratio(void)
341 /* Check for configurable TDP option */
342 if (turbo_get_state() == TURBO_ENABLED) {
343 msr = msr_read(MSR_TURBO_RATIO_LIMIT);
344 perf_ctl.lo = (msr.lo & 0xff) << 8;
345 } else if (cpu_config_tdp_levels()) {
346 /* Set to nominal TDP ratio */
347 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
348 perf_ctl.lo = (msr.lo & 0xff) << 8;
350 /* Platform Info bits 15:8 give max ratio */
351 msr = msr_read(MSR_PLATFORM_INFO);
352 perf_ctl.lo = msr.lo & 0xff00;
354 msr_write(MSR_IA32_PERF_CTL, perf_ctl);
356 debug("cpu: frequency set to %d\n",
357 ((perf_ctl.lo >> 8) & 0xff) * INTEL_BCLK_MHZ);
360 int broadwell_init(struct udevice *dev)
362 struct cpu_broadwell_priv *priv = dev_get_priv(dev);
368 msr = msr_read(CORE_THREAD_COUNT_MSR);
369 num_threads = (msr.lo >> 0) & 0xffff;
370 num_cores = (msr.lo >> 16) & 0xffff;
371 debug("CPU has %u cores, %u threads enabled\n", num_cores,
374 priv->ht_disabled = num_threads == num_cores;
376 ret = bsp_init_before_ap_bringup(dev);
385 static void configure_mca(void)
388 const unsigned int mcg_cap_msr = 0x179;
392 msr = msr_read(mcg_cap_msr);
393 num_banks = msr.lo & 0xff;
397 * TODO(adurbin): This should only be done on a cold boot. Also, some
398 * of these banks are core vs package scope. For now every CPU clears
401 for (i = 0; i < num_banks; i++)
402 msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr);
405 static void enable_lapic_tpr(void)
409 msr = msr_read(MSR_PIC_MSG_CONTROL);
410 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
411 msr_write(MSR_PIC_MSG_CONTROL, msr);
414 static void configure_c_states(void)
418 msr = msr_read(MSR_PMG_CST_CONFIG_CONTROL);
419 msr.lo |= (1 << 31); /* Timed MWAIT Enable */
420 msr.lo |= (1 << 30); /* Package c-state Undemotion Enable */
421 msr.lo |= (1 << 29); /* Package c-state Demotion Enable */
422 msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */
423 msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */
424 msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */
425 msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */
426 msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */
427 /* The deepest package c-state defaults to factory-configured value */
428 msr_write(MSR_PMG_CST_CONFIG_CONTROL, msr);
430 msr = msr_read(MSR_MISC_PWR_MGMT);
431 msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */
432 msr_write(MSR_MISC_PWR_MGMT, msr);
434 msr = msr_read(MSR_POWER_CTL);
435 msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */
436 msr.lo |= (1 << 1); /* C1E Enable */
437 msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */
438 msr_write(MSR_POWER_CTL, msr);
440 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
442 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
443 msr_write(MSR_C_STATE_LATENCY_CONTROL_0, msr);
445 /* C-state Interrupt Response Latency Control 1 */
447 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
448 msr_write(MSR_C_STATE_LATENCY_CONTROL_1, msr);
450 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
452 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
453 msr_write(MSR_C_STATE_LATENCY_CONTROL_2, msr);
455 /* C-state Interrupt Response Latency Control 3 - package C8 */
457 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
458 msr_write(MSR_C_STATE_LATENCY_CONTROL_3, msr);
460 /* C-state Interrupt Response Latency Control 4 - package C9 */
462 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
463 msr_write(MSR_C_STATE_LATENCY_CONTROL_4, msr);
465 /* C-state Interrupt Response Latency Control 5 - package C10 */
467 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
468 msr_write(MSR_C_STATE_LATENCY_CONTROL_5, msr);
471 static void configure_misc(void)
475 msr = msr_read(MSR_IA32_MISC_ENABLE);
476 msr.lo |= MISC_ENABLE_FAST_STRING;
477 msr.lo |= MISC_ENABLE_TM1;
478 msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP;
479 msr_write(MSR_IA32_MISC_ENABLE, msr);
481 /* Disable thermal interrupts */
484 msr_write(MSR_IA32_THERM_INTERRUPT, msr);
486 /* Enable package critical interrupt only */
489 msr_write(MSR_IA32_PACKAGE_THERM_INTERRUPT, msr);
492 static void configure_dca_cap(void)
494 struct cpuid_result cpuid_regs;
497 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
498 cpuid_regs = cpuid(1);
499 if (cpuid_regs.ecx & (1 << 18)) {
500 msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP);
502 msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr);
506 static void set_energy_perf_bias(u8 policy)
511 /* Determine if energy efficient policy is supported */
512 ecx = cpuid_ecx(0x6);
513 if (!(ecx & (1 << 3)))
516 /* Energy Policy is bits 3:0 */
517 msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS);
519 msr.lo |= policy & 0xf;
520 msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr);
522 debug("cpu: energy policy set to %u\n", policy);
525 /* All CPUs including BSP will run the following function */
526 static void cpu_core_init(struct udevice *dev)
528 /* Clear out pending MCEs */
531 /* Enable the local cpu apics */
534 /* Configure C States */
535 configure_c_states();
537 /* Configure Enhanced SpeedStep and Thermal Sensors */
540 /* Thermal throttle activation offset */
541 cpu_configure_thermal_target(dev);
543 /* Enable Direct Cache Access */
546 /* Set energy policy */
547 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
554 * Configure processor power limits if possible
555 * This must be done AFTER set of BIOS_RESET_CPL
557 void cpu_set_power_limits(int power_limit_1_time)
562 uint tdp, min_power, max_power, max_time;
563 u8 power_limit_1_val;
565 msr = msr_read(MSR_PLATFORM_INFO);
566 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
567 power_limit_1_time = 28;
569 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
573 msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
574 power_unit = 2 << ((msr.lo & 0xf) - 1);
576 /* Get power defaults for this SKU */
577 msr = msr_read(MSR_PKG_POWER_SKU);
578 tdp = msr.lo & 0x7fff;
579 min_power = (msr.lo >> 16) & 0x7fff;
580 max_power = msr.hi & 0x7fff;
581 max_time = (msr.hi >> 16) & 0x7f;
583 debug("CPU TDP: %u Watts\n", tdp / power_unit);
585 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
586 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
588 if (min_power > 0 && tdp < min_power)
591 if (max_power > 0 && tdp > max_power)
594 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
596 /* Set long term power limit to TDP */
598 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
599 limit.lo |= PKG_POWER_LIMIT_EN;
600 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
601 PKG_POWER_LIMIT_TIME_SHIFT;
603 /* Set short term power limit to 1.25 * TDP */
605 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
606 limit.hi |= PKG_POWER_LIMIT_EN;
607 /* Power limit 2 time is only programmable on server SKU */
609 msr_write(MSR_PKG_POWER_LIMIT, limit);
611 /* Set power limit values in MCHBAR as well */
612 writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO));
613 writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI));
615 /* Set DDR RAPL power limit by copying from MMIO to MSR */
616 msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO));
617 msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI));
618 msr_write(MSR_DDR_RAPL_LIMIT, msr);
620 /* Use nominal TDP values for CPUs with configurable TDP */
621 if (cpu_config_tdp_levels()) {
622 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
624 limit.lo = msr.lo & 0xff;
625 msr_write(MSR_TURBO_ACTIVATION_RATIO, limit);
629 static int broadwell_get_info(const struct udevice *dev, struct cpu_info *info)
631 return cpu_intel_get_info(info, INTEL_BCLK_MHZ);
634 static int broadwell_get_count(const struct udevice *dev)
639 static int cpu_x86_broadwell_probe(struct udevice *dev)
643 return broadwell_init(dev);
649 static const struct cpu_ops cpu_x86_broadwell_ops = {
650 .get_desc = cpu_x86_get_desc,
651 .get_info = broadwell_get_info,
652 .get_count = broadwell_get_count,
653 .get_vendor = cpu_x86_get_vendor,
656 static const struct udevice_id cpu_x86_broadwell_ids[] = {
657 { .compatible = "intel,core-i3-gen5" },
661 U_BOOT_DRIVER(cpu_x86_broadwell_drv) = {
662 .name = "cpu_x86_broadwell",
664 .of_match = cpu_x86_broadwell_ids,
665 .bind = cpu_x86_bind,
666 .probe = cpu_x86_broadwell_probe,
667 .ops = &cpu_x86_broadwell_ops,
668 .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv),
669 .flags = DM_FLAG_PRE_RELOC,