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
12 #include <asm/cpu_x86.h>
13 #include <asm/cpu_common.h>
14 #include <asm/intel_regs.h>
15 #include <asm/lpc_common.h>
19 #include <asm/turbo.h>
20 #include <asm/arch/cpu.h>
21 #include <asm/arch/pch.h>
22 #include <asm/arch/rcb.h>
24 struct cpu_broadwell_priv {
28 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
29 static const u8 power_limit_time_sec_to_msr[] = {
57 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
58 static const u8 power_limit_time_msr_to_sec[] = {
86 int arch_cpu_init_dm(void)
91 /* Start up the LPC so we have serial */
92 ret = uclass_first_device(UCLASS_LPC, &dev);
97 ret = cpu_set_flex_ratio_to_tdp_nominal();
104 void set_max_freq(void)
106 msr_t msr, perf_ctl, platform_info;
108 /* Check for configurable TDP option */
109 platform_info = msr_read(MSR_PLATFORM_INFO);
111 if ((platform_info.hi >> 1) & 3) {
112 /* Set to nominal TDP ratio */
113 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
114 perf_ctl.lo = (msr.lo & 0xff) << 8;
116 /* Platform Info bits 15:8 give max ratio */
117 msr = msr_read(MSR_PLATFORM_INFO);
118 perf_ctl.lo = msr.lo & 0xff00;
122 msr_write(IA32_PERF_CTL, perf_ctl);
124 debug("CPU: frequency set to %d MHz\n",
125 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
128 int arch_cpu_init(void)
130 post_code(POST_CPU_INIT);
132 return x86_cpu_init_f();
141 ret = cpu_common_init();
144 gd->arch.pei_boot_mode = PEI_BOOT_NONE;
149 int print_cpuinfo(void)
151 char processor_name[CPU_MAX_NAME_LEN];
154 /* Print processor name */
155 name = cpu_get_name(processor_name);
156 printf("CPU: %s\n", name);
161 void board_debug_uart_init(void)
163 struct udevice *bus = NULL;
165 /* com1 / com2 decode range */
166 pci_x86_write_config(bus, PCH_DEV_LPC, LPC_IO_DEC, 1 << 4, PCI_SIZE_16);
168 pci_x86_write_config(bus, PCH_DEV_LPC, LPC_EN, COMA_LPC_EN,
173 * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
174 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
175 * when a core is woken up
177 static int pcode_ready(void)
180 const int delay_step = 10;
184 if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) &
187 wait_count += delay_step;
189 } while (wait_count < 1000);
194 static u32 pcode_mailbox_read(u32 command)
200 debug("PCODE: mailbox timeout on wait ready\n");
204 /* Send command and start transaction */
205 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
209 debug("PCODE: mailbox timeout on completion\n");
214 return readl(MCHBAR_REG(BIOS_MAILBOX_DATA));
217 static int pcode_mailbox_write(u32 command, u32 data)
223 debug("PCODE: mailbox timeout on wait ready\n");
227 writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA));
229 /* Send command and start transaction */
230 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
234 debug("PCODE: mailbox timeout on completion\n");
241 /* @dev is the CPU device */
242 static void initialize_vr_config(struct udevice *dev)
247 debug("Initializing VR config\n");
249 /* Configure VR_CURRENT_CONFIG */
250 msr = msr_read(MSR_VR_CURRENT_CONFIG);
252 * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
255 msr.hi &= 0xc0000000;
256 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */
257 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */
258 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */
259 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
260 /* Leave the max instantaneous current limit (12:0) to default */
261 msr_write(MSR_VR_CURRENT_CONFIG, msr);
263 /* Configure VR_MISC_CONFIG MSR */
264 msr = msr_read(MSR_VR_MISC_CONFIG);
265 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */
266 msr.hi &= ~(0x3ff << (40 - 32));
267 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
268 /* Set IOUT_OFFSET to 0 */
270 /* Set entry ramp rate to slow */
271 msr.hi &= ~(1 << (51 - 32));
272 /* Enable decay mode on C-state entry */
273 msr.hi |= (1 << (52 - 32));
274 /* Set the slow ramp rate */
275 msr.hi &= ~(0x3 << (53 - 32));
276 /* Configure the C-state exit ramp rate */
277 ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
278 "intel,slow-ramp", -1);
280 /* Configured slow ramp rate */
281 msr.hi |= ((ramp & 0x3) << (53 - 32));
282 /* Set exit ramp rate to slow */
283 msr.hi &= ~(1 << (50 - 32));
285 /* Fast ramp rate / 4 */
286 msr.hi |= (0x01 << (53 - 32));
287 /* Set exit ramp rate to fast */
288 msr.hi |= (1 << (50 - 32));
290 /* Set MIN_VID (31:24) to allow CPU to have full control */
291 msr.lo &= ~0xff000000;
292 min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
294 msr.lo |= (min_vid & 0xff) << 24;
295 msr_write(MSR_VR_MISC_CONFIG, msr);
297 /* Configure VR_MISC_CONFIG2 MSR */
298 msr = msr_read(MSR_VR_MISC_CONFIG2);
301 * Allow CPU to control minimum voltage completely (15:8) and
302 * set the fast ramp voltage in 10mV steps
304 if (cpu_get_family_model() == BROADWELL_FAMILY_ULT)
305 msr.lo |= 0x006a; /* 1.56V */
307 msr.lo |= 0x006f; /* 1.60V */
308 msr_write(MSR_VR_MISC_CONFIG2, msr);
310 /* Set C9/C10 VCC Min */
311 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
314 static int calibrate_24mhz_bclk(void)
323 /* A non-zero value initiates the PCODE calibration */
324 writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA));
325 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL,
326 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
332 err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff;
334 debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code);
336 /* Read the calibrated value */
337 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION,
338 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
344 debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n",
345 readl(MCHBAR_REG(BIOS_MAILBOX_DATA)));
350 static void configure_pch_power_sharing(void)
352 u32 pch_power, pch_power_ext, pmsync, pmsync2;
355 /* Read PCH Power levels from PCODE */
356 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
357 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
359 debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power,
362 pmsync = readl(RCB_REG(PMSYNC_CONFIG));
363 pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2));
366 * Program PMSYNC_TPR_CONFIG PCH power limit values
367 * pmsync[0:4] = mailbox[0:5]
368 * pmsync[8:12] = mailbox[6:11]
369 * pmsync[16:20] = mailbox[12:17]
371 for (i = 0; i < 3; i++) {
372 u32 level = pch_power & 0x3f;
374 pmsync &= ~(0x1f << (i * 8));
375 pmsync |= (level & 0x1f) << (i * 8);
377 writel(pmsync, RCB_REG(PMSYNC_CONFIG));
380 * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
381 * pmsync2[0:4] = mailbox[23:18]
382 * pmsync2[8:12] = mailbox_ext[6:11]
383 * pmsync2[16:20] = mailbox_ext[12:17]
384 * pmsync2[24:28] = mailbox_ext[18:22]
387 pmsync2 |= pch_power & 0x1f;
389 for (i = 1; i < 4; i++) {
390 u32 level = pch_power_ext & 0x3f;
392 pmsync2 &= ~(0x1f << (i * 8));
393 pmsync2 |= (level & 0x1f) << (i * 8);
395 writel(pmsync2, RCB_REG(PMSYNC_CONFIG2));
398 static int bsp_init_before_ap_bringup(struct udevice *dev)
402 initialize_vr_config(dev);
403 ret = calibrate_24mhz_bclk();
406 configure_pch_power_sharing();
411 int cpu_config_tdp_levels(void)
415 /* Bits 34:33 indicate how many levels supported */
416 platform_info = msr_read(MSR_PLATFORM_INFO);
417 return (platform_info.hi >> 1) & 3;
420 static void set_max_ratio(void)
426 /* Check for configurable TDP option */
427 if (turbo_get_state() == TURBO_ENABLED) {
428 msr = msr_read(MSR_NHM_TURBO_RATIO_LIMIT);
429 perf_ctl.lo = (msr.lo & 0xff) << 8;
430 } else if (cpu_config_tdp_levels()) {
431 /* Set to nominal TDP ratio */
432 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
433 perf_ctl.lo = (msr.lo & 0xff) << 8;
435 /* Platform Info bits 15:8 give max ratio */
436 msr = msr_read(MSR_PLATFORM_INFO);
437 perf_ctl.lo = msr.lo & 0xff00;
439 msr_write(IA32_PERF_CTL, perf_ctl);
441 debug("cpu: frequency set to %d\n",
442 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
445 int broadwell_init(struct udevice *dev)
447 struct cpu_broadwell_priv *priv = dev_get_priv(dev);
453 msr = msr_read(CORE_THREAD_COUNT_MSR);
454 num_threads = (msr.lo >> 0) & 0xffff;
455 num_cores = (msr.lo >> 16) & 0xffff;
456 debug("CPU has %u cores, %u threads enabled\n", num_cores,
459 priv->ht_disabled = num_threads == num_cores;
461 ret = bsp_init_before_ap_bringup(dev);
470 static void configure_mca(void)
473 const unsigned int mcg_cap_msr = 0x179;
477 msr = msr_read(mcg_cap_msr);
478 num_banks = msr.lo & 0xff;
482 * TODO(adurbin): This should only be done on a cold boot. Also, some
483 * of these banks are core vs package scope. For now every CPU clears
486 for (i = 0; i < num_banks; i++)
487 msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr);
490 static void enable_lapic_tpr(void)
494 msr = msr_read(MSR_PIC_MSG_CONTROL);
495 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
496 msr_write(MSR_PIC_MSG_CONTROL, msr);
500 static void configure_c_states(void)
504 msr = msr_read(MSR_PMG_CST_CONFIG_CONTROL);
505 msr.lo |= (1 << 31); /* Timed MWAIT Enable */
506 msr.lo |= (1 << 30); /* Package c-state Undemotion Enable */
507 msr.lo |= (1 << 29); /* Package c-state Demotion Enable */
508 msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */
509 msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */
510 msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */
511 msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */
512 msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */
513 /* The deepest package c-state defaults to factory-configured value */
514 msr_write(MSR_PMG_CST_CONFIG_CONTROL, msr);
516 msr = msr_read(MSR_MISC_PWR_MGMT);
517 msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */
518 msr_write(MSR_MISC_PWR_MGMT, msr);
520 msr = msr_read(MSR_POWER_CTL);
521 msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */
522 msr.lo |= (1 << 1); /* C1E Enable */
523 msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */
524 msr_write(MSR_POWER_CTL, msr);
526 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
528 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
529 msr_write(MSR_C_STATE_LATENCY_CONTROL_0, msr);
531 /* C-state Interrupt Response Latency Control 1 */
533 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
534 msr_write(MSR_C_STATE_LATENCY_CONTROL_1, msr);
536 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
538 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
539 msr_write(MSR_C_STATE_LATENCY_CONTROL_2, msr);
541 /* C-state Interrupt Response Latency Control 3 - package C8 */
543 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
544 msr_write(MSR_C_STATE_LATENCY_CONTROL_3, msr);
546 /* C-state Interrupt Response Latency Control 4 - package C9 */
548 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
549 msr_write(MSR_C_STATE_LATENCY_CONTROL_4, msr);
551 /* C-state Interrupt Response Latency Control 5 - package C10 */
553 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
554 msr_write(MSR_C_STATE_LATENCY_CONTROL_5, msr);
557 static void configure_misc(void)
561 msr = msr_read(MSR_IA32_MISC_ENABLE);
562 msr.lo |= (1 << 0); /* Fast String enable */
563 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
564 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
565 msr_write(MSR_IA32_MISC_ENABLE, msr);
567 /* Disable thermal interrupts */
570 msr_write(MSR_IA32_THERM_INTERRUPT, msr);
572 /* Enable package critical interrupt only */
575 msr_write(MSR_IA32_PACKAGE_THERM_INTERRUPT, msr);
578 static void configure_thermal_target(struct udevice *dev)
583 tcc_offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
584 "intel,tcc-offset", 0);
586 /* Set TCC activaiton offset if supported */
587 msr = msr_read(MSR_PLATFORM_INFO);
588 if ((msr.lo & (1 << 30)) && tcc_offset) {
589 msr = msr_read(MSR_TEMPERATURE_TARGET);
590 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
591 msr.lo |= (tcc_offset & 0xf) << 24;
592 msr_write(MSR_TEMPERATURE_TARGET, msr);
596 static void configure_dca_cap(void)
598 struct cpuid_result cpuid_regs;
601 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
602 cpuid_regs = cpuid(1);
603 if (cpuid_regs.ecx & (1 << 18)) {
604 msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP);
606 msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr);
610 static void set_energy_perf_bias(u8 policy)
615 /* Determine if energy efficient policy is supported */
616 ecx = cpuid_ecx(0x6);
617 if (!(ecx & (1 << 3)))
620 /* Energy Policy is bits 3:0 */
621 msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS);
623 msr.lo |= policy & 0xf;
624 msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr);
626 debug("cpu: energy policy set to %u\n", policy);
629 /* All CPUs including BSP will run the following function */
630 static void cpu_core_init(struct udevice *dev)
632 /* Clear out pending MCEs */
635 /* Enable the local cpu apics */
638 /* Configure C States */
639 configure_c_states();
641 /* Configure Enhanced SpeedStep and Thermal Sensors */
644 /* Thermal throttle activation offset */
645 configure_thermal_target(dev);
647 /* Enable Direct Cache Access */
650 /* Set energy policy */
651 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
658 * Configure processor power limits if possible
659 * This must be done AFTER set of BIOS_RESET_CPL
661 void cpu_set_power_limits(int power_limit_1_time)
666 unsigned tdp, min_power, max_power, max_time;
667 u8 power_limit_1_val;
669 msr = msr_read(MSR_PLATFORM_INFO);
670 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
671 power_limit_1_time = 28;
673 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
677 msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
678 power_unit = 2 << ((msr.lo & 0xf) - 1);
680 /* Get power defaults for this SKU */
681 msr = msr_read(MSR_PKG_POWER_SKU);
682 tdp = msr.lo & 0x7fff;
683 min_power = (msr.lo >> 16) & 0x7fff;
684 max_power = msr.hi & 0x7fff;
685 max_time = (msr.hi >> 16) & 0x7f;
687 debug("CPU TDP: %u Watts\n", tdp / power_unit);
689 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
690 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
692 if (min_power > 0 && tdp < min_power)
695 if (max_power > 0 && tdp > max_power)
698 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
700 /* Set long term power limit to TDP */
702 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
703 limit.lo |= PKG_POWER_LIMIT_EN;
704 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
705 PKG_POWER_LIMIT_TIME_SHIFT;
707 /* Set short term power limit to 1.25 * TDP */
709 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
710 limit.hi |= PKG_POWER_LIMIT_EN;
711 /* Power limit 2 time is only programmable on server SKU */
713 msr_write(MSR_PKG_POWER_LIMIT, limit);
715 /* Set power limit values in MCHBAR as well */
716 writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO));
717 writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI));
719 /* Set DDR RAPL power limit by copying from MMIO to MSR */
720 msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO));
721 msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI));
722 msr_write(MSR_DDR_RAPL_LIMIT, msr);
724 /* Use nominal TDP values for CPUs with configurable TDP */
725 if (cpu_config_tdp_levels()) {
726 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
728 limit.lo = msr.lo & 0xff;
729 msr_write(MSR_TURBO_ACTIVATION_RATIO, limit);
733 static int broadwell_get_info(struct udevice *dev, struct cpu_info *info)
737 msr = msr_read(IA32_PERF_CTL);
738 info->cpu_freq = ((msr.lo >> 8) & 0xff) * BROADWELL_BCLK * 1000000;
739 info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
740 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID;
745 static int broadwell_get_count(struct udevice *dev)
750 static int cpu_x86_broadwell_probe(struct udevice *dev)
754 return broadwell_init(dev);
760 static const struct cpu_ops cpu_x86_broadwell_ops = {
761 .get_desc = cpu_x86_get_desc,
762 .get_info = broadwell_get_info,
763 .get_count = broadwell_get_count,
764 .get_vendor = cpu_x86_get_vendor,
767 static const struct udevice_id cpu_x86_broadwell_ids[] = {
768 { .compatible = "intel,core-i3-gen5" },
772 U_BOOT_DRIVER(cpu_x86_broadwell_drv) = {
773 .name = "cpu_x86_broadwell",
775 .of_match = cpu_x86_broadwell_ids,
776 .bind = cpu_x86_bind,
777 .probe = cpu_x86_broadwell_probe,
778 .ops = &cpu_x86_broadwell_ops,
779 .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv),
780 .flags = DM_FLAG_PRE_RELOC,