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>
17 #include <asm/turbo.h>
18 #include <asm/arch/cpu.h>
19 #include <asm/arch/pch.h>
20 #include <asm/arch/rcb.h>
22 struct cpu_broadwell_priv {
26 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
27 static const u8 power_limit_time_sec_to_msr[] = {
55 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
56 static const u8 power_limit_time_msr_to_sec[] = {
84 int arch_cpu_init_dm(void)
89 /* Start up the LPC so we have serial */
90 ret = uclass_first_device(UCLASS_LPC, &dev);
95 ret = cpu_set_flex_ratio_to_tdp_nominal();
102 void set_max_freq(void)
104 msr_t msr, perf_ctl, platform_info;
106 /* Check for configurable TDP option */
107 platform_info = msr_read(MSR_PLATFORM_INFO);
109 if ((platform_info.hi >> 1) & 3) {
110 /* Set to nominal TDP ratio */
111 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
112 perf_ctl.lo = (msr.lo & 0xff) << 8;
114 /* Platform Info bits 15:8 give max ratio */
115 msr = msr_read(MSR_PLATFORM_INFO);
116 perf_ctl.lo = msr.lo & 0xff00;
120 msr_write(IA32_PERF_CTL, perf_ctl);
122 debug("CPU: frequency set to %d MHz\n",
123 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
126 int arch_cpu_init(void)
128 post_code(POST_CPU_INIT);
130 return x86_cpu_init_f();
139 ret = cpu_common_init();
142 gd->arch.pei_boot_mode = PEI_BOOT_NONE;
147 int print_cpuinfo(void)
149 char processor_name[CPU_MAX_NAME_LEN];
152 /* Print processor name */
153 name = cpu_get_name(processor_name);
154 printf("CPU: %s\n", name);
160 * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
161 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
162 * when a core is woken up
164 static int pcode_ready(void)
167 const int delay_step = 10;
171 if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) &
174 wait_count += delay_step;
176 } while (wait_count < 1000);
181 static u32 pcode_mailbox_read(u32 command)
187 debug("PCODE: mailbox timeout on wait ready\n");
191 /* Send command and start transaction */
192 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
196 debug("PCODE: mailbox timeout on completion\n");
201 return readl(MCHBAR_REG(BIOS_MAILBOX_DATA));
204 static int pcode_mailbox_write(u32 command, u32 data)
210 debug("PCODE: mailbox timeout on wait ready\n");
214 writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA));
216 /* Send command and start transaction */
217 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
221 debug("PCODE: mailbox timeout on completion\n");
228 /* @dev is the CPU device */
229 static void initialize_vr_config(struct udevice *dev)
234 debug("Initializing VR config\n");
236 /* Configure VR_CURRENT_CONFIG */
237 msr = msr_read(MSR_VR_CURRENT_CONFIG);
239 * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
242 msr.hi &= 0xc0000000;
243 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */
244 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */
245 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */
246 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
247 /* Leave the max instantaneous current limit (12:0) to default */
248 msr_write(MSR_VR_CURRENT_CONFIG, msr);
250 /* Configure VR_MISC_CONFIG MSR */
251 msr = msr_read(MSR_VR_MISC_CONFIG);
252 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */
253 msr.hi &= ~(0x3ff << (40 - 32));
254 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
255 /* Set IOUT_OFFSET to 0 */
257 /* Set entry ramp rate to slow */
258 msr.hi &= ~(1 << (51 - 32));
259 /* Enable decay mode on C-state entry */
260 msr.hi |= (1 << (52 - 32));
261 /* Set the slow ramp rate */
262 msr.hi &= ~(0x3 << (53 - 32));
263 /* Configure the C-state exit ramp rate */
264 ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
265 "intel,slow-ramp", -1);
267 /* Configured slow ramp rate */
268 msr.hi |= ((ramp & 0x3) << (53 - 32));
269 /* Set exit ramp rate to slow */
270 msr.hi &= ~(1 << (50 - 32));
272 /* Fast ramp rate / 4 */
273 msr.hi |= (0x01 << (53 - 32));
274 /* Set exit ramp rate to fast */
275 msr.hi |= (1 << (50 - 32));
277 /* Set MIN_VID (31:24) to allow CPU to have full control */
278 msr.lo &= ~0xff000000;
279 min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
281 msr.lo |= (min_vid & 0xff) << 24;
282 msr_write(MSR_VR_MISC_CONFIG, msr);
284 /* Configure VR_MISC_CONFIG2 MSR */
285 msr = msr_read(MSR_VR_MISC_CONFIG2);
288 * Allow CPU to control minimum voltage completely (15:8) and
289 * set the fast ramp voltage in 10mV steps
291 if (cpu_get_family_model() == BROADWELL_FAMILY_ULT)
292 msr.lo |= 0x006a; /* 1.56V */
294 msr.lo |= 0x006f; /* 1.60V */
295 msr_write(MSR_VR_MISC_CONFIG2, msr);
297 /* Set C9/C10 VCC Min */
298 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
301 static int calibrate_24mhz_bclk(void)
310 /* A non-zero value initiates the PCODE calibration */
311 writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA));
312 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL,
313 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
319 err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff;
321 debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code);
323 /* Read the calibrated value */
324 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION,
325 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
331 debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n",
332 readl(MCHBAR_REG(BIOS_MAILBOX_DATA)));
337 static void configure_pch_power_sharing(void)
339 u32 pch_power, pch_power_ext, pmsync, pmsync2;
342 /* Read PCH Power levels from PCODE */
343 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
344 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
346 debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power,
349 pmsync = readl(RCB_REG(PMSYNC_CONFIG));
350 pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2));
353 * Program PMSYNC_TPR_CONFIG PCH power limit values
354 * pmsync[0:4] = mailbox[0:5]
355 * pmsync[8:12] = mailbox[6:11]
356 * pmsync[16:20] = mailbox[12:17]
358 for (i = 0; i < 3; i++) {
359 u32 level = pch_power & 0x3f;
361 pmsync &= ~(0x1f << (i * 8));
362 pmsync |= (level & 0x1f) << (i * 8);
364 writel(pmsync, RCB_REG(PMSYNC_CONFIG));
367 * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
368 * pmsync2[0:4] = mailbox[23:18]
369 * pmsync2[8:12] = mailbox_ext[6:11]
370 * pmsync2[16:20] = mailbox_ext[12:17]
371 * pmsync2[24:28] = mailbox_ext[18:22]
374 pmsync2 |= pch_power & 0x1f;
376 for (i = 1; i < 4; i++) {
377 u32 level = pch_power_ext & 0x3f;
379 pmsync2 &= ~(0x1f << (i * 8));
380 pmsync2 |= (level & 0x1f) << (i * 8);
382 writel(pmsync2, RCB_REG(PMSYNC_CONFIG2));
385 static int bsp_init_before_ap_bringup(struct udevice *dev)
389 initialize_vr_config(dev);
390 ret = calibrate_24mhz_bclk();
393 configure_pch_power_sharing();
398 int cpu_config_tdp_levels(void)
402 /* Bits 34:33 indicate how many levels supported */
403 platform_info = msr_read(MSR_PLATFORM_INFO);
404 return (platform_info.hi >> 1) & 3;
407 static void set_max_ratio(void)
413 /* Check for configurable TDP option */
414 if (turbo_get_state() == TURBO_ENABLED) {
415 msr = msr_read(MSR_NHM_TURBO_RATIO_LIMIT);
416 perf_ctl.lo = (msr.lo & 0xff) << 8;
417 } else if (cpu_config_tdp_levels()) {
418 /* Set to nominal TDP ratio */
419 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
420 perf_ctl.lo = (msr.lo & 0xff) << 8;
422 /* Platform Info bits 15:8 give max ratio */
423 msr = msr_read(MSR_PLATFORM_INFO);
424 perf_ctl.lo = msr.lo & 0xff00;
426 msr_write(IA32_PERF_CTL, perf_ctl);
428 debug("cpu: frequency set to %d\n",
429 ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
432 int broadwell_init(struct udevice *dev)
434 struct cpu_broadwell_priv *priv = dev_get_priv(dev);
440 msr = msr_read(CORE_THREAD_COUNT_MSR);
441 num_threads = (msr.lo >> 0) & 0xffff;
442 num_cores = (msr.lo >> 16) & 0xffff;
443 debug("CPU has %u cores, %u threads enabled\n", num_cores,
446 priv->ht_disabled = num_threads == num_cores;
448 ret = bsp_init_before_ap_bringup(dev);
457 static void configure_mca(void)
460 const unsigned int mcg_cap_msr = 0x179;
464 msr = msr_read(mcg_cap_msr);
465 num_banks = msr.lo & 0xff;
469 * TODO(adurbin): This should only be done on a cold boot. Also, some
470 * of these banks are core vs package scope. For now every CPU clears
473 for (i = 0; i < num_banks; i++)
474 msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr);
477 static void enable_lapic_tpr(void)
481 msr = msr_read(MSR_PIC_MSG_CONTROL);
482 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
483 msr_write(MSR_PIC_MSG_CONTROL, msr);
487 static void configure_c_states(void)
491 msr = msr_read(MSR_PMG_CST_CONFIG_CONTROL);
492 msr.lo |= (1 << 31); /* Timed MWAIT Enable */
493 msr.lo |= (1 << 30); /* Package c-state Undemotion Enable */
494 msr.lo |= (1 << 29); /* Package c-state Demotion Enable */
495 msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */
496 msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */
497 msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */
498 msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */
499 msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */
500 /* The deepest package c-state defaults to factory-configured value */
501 msr_write(MSR_PMG_CST_CONFIG_CONTROL, msr);
503 msr = msr_read(MSR_MISC_PWR_MGMT);
504 msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */
505 msr_write(MSR_MISC_PWR_MGMT, msr);
507 msr = msr_read(MSR_POWER_CTL);
508 msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */
509 msr.lo |= (1 << 1); /* C1E Enable */
510 msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */
511 msr_write(MSR_POWER_CTL, msr);
513 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
515 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
516 msr_write(MSR_C_STATE_LATENCY_CONTROL_0, msr);
518 /* C-state Interrupt Response Latency Control 1 */
520 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
521 msr_write(MSR_C_STATE_LATENCY_CONTROL_1, msr);
523 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
525 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
526 msr_write(MSR_C_STATE_LATENCY_CONTROL_2, msr);
528 /* C-state Interrupt Response Latency Control 3 - package C8 */
530 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
531 msr_write(MSR_C_STATE_LATENCY_CONTROL_3, msr);
533 /* C-state Interrupt Response Latency Control 4 - package C9 */
535 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
536 msr_write(MSR_C_STATE_LATENCY_CONTROL_4, msr);
538 /* C-state Interrupt Response Latency Control 5 - package C10 */
540 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
541 msr_write(MSR_C_STATE_LATENCY_CONTROL_5, msr);
544 static void configure_misc(void)
548 msr = msr_read(MSR_IA32_MISC_ENABLE);
549 msr.lo |= (1 << 0); /* Fast String enable */
550 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
551 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
552 msr_write(MSR_IA32_MISC_ENABLE, msr);
554 /* Disable thermal interrupts */
557 msr_write(MSR_IA32_THERM_INTERRUPT, msr);
559 /* Enable package critical interrupt only */
562 msr_write(MSR_IA32_PACKAGE_THERM_INTERRUPT, msr);
565 static void configure_thermal_target(struct udevice *dev)
570 tcc_offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
571 "intel,tcc-offset", 0);
573 /* Set TCC activaiton offset if supported */
574 msr = msr_read(MSR_PLATFORM_INFO);
575 if ((msr.lo & (1 << 30)) && tcc_offset) {
576 msr = msr_read(MSR_TEMPERATURE_TARGET);
577 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
578 msr.lo |= (tcc_offset & 0xf) << 24;
579 msr_write(MSR_TEMPERATURE_TARGET, msr);
583 static void configure_dca_cap(void)
585 struct cpuid_result cpuid_regs;
588 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
589 cpuid_regs = cpuid(1);
590 if (cpuid_regs.ecx & (1 << 18)) {
591 msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP);
593 msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr);
597 static void set_energy_perf_bias(u8 policy)
602 /* Determine if energy efficient policy is supported */
603 ecx = cpuid_ecx(0x6);
604 if (!(ecx & (1 << 3)))
607 /* Energy Policy is bits 3:0 */
608 msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS);
610 msr.lo |= policy & 0xf;
611 msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr);
613 debug("cpu: energy policy set to %u\n", policy);
616 /* All CPUs including BSP will run the following function */
617 static void cpu_core_init(struct udevice *dev)
619 /* Clear out pending MCEs */
622 /* Enable the local cpu apics */
625 /* Configure C States */
626 configure_c_states();
628 /* Configure Enhanced SpeedStep and Thermal Sensors */
631 /* Thermal throttle activation offset */
632 configure_thermal_target(dev);
634 /* Enable Direct Cache Access */
637 /* Set energy policy */
638 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
645 * Configure processor power limits if possible
646 * This must be done AFTER set of BIOS_RESET_CPL
648 void cpu_set_power_limits(int power_limit_1_time)
653 unsigned tdp, min_power, max_power, max_time;
654 u8 power_limit_1_val;
656 msr = msr_read(MSR_PLATFORM_INFO);
657 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
658 power_limit_1_time = 28;
660 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
664 msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
665 power_unit = 2 << ((msr.lo & 0xf) - 1);
667 /* Get power defaults for this SKU */
668 msr = msr_read(MSR_PKG_POWER_SKU);
669 tdp = msr.lo & 0x7fff;
670 min_power = (msr.lo >> 16) & 0x7fff;
671 max_power = msr.hi & 0x7fff;
672 max_time = (msr.hi >> 16) & 0x7f;
674 debug("CPU TDP: %u Watts\n", tdp / power_unit);
676 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
677 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
679 if (min_power > 0 && tdp < min_power)
682 if (max_power > 0 && tdp > max_power)
685 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
687 /* Set long term power limit to TDP */
689 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
690 limit.lo |= PKG_POWER_LIMIT_EN;
691 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
692 PKG_POWER_LIMIT_TIME_SHIFT;
694 /* Set short term power limit to 1.25 * TDP */
696 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
697 limit.hi |= PKG_POWER_LIMIT_EN;
698 /* Power limit 2 time is only programmable on server SKU */
700 msr_write(MSR_PKG_POWER_LIMIT, limit);
702 /* Set power limit values in MCHBAR as well */
703 writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO));
704 writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI));
706 /* Set DDR RAPL power limit by copying from MMIO to MSR */
707 msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO));
708 msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI));
709 msr_write(MSR_DDR_RAPL_LIMIT, msr);
711 /* Use nominal TDP values for CPUs with configurable TDP */
712 if (cpu_config_tdp_levels()) {
713 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
715 limit.lo = msr.lo & 0xff;
716 msr_write(MSR_TURBO_ACTIVATION_RATIO, limit);
720 static int broadwell_get_info(struct udevice *dev, struct cpu_info *info)
724 msr = msr_read(IA32_PERF_CTL);
725 info->cpu_freq = ((msr.lo >> 8) & 0xff) * BROADWELL_BCLK * 1000000;
726 info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
727 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID;
732 static int broadwell_get_count(struct udevice *dev)
737 static int cpu_x86_broadwell_probe(struct udevice *dev)
741 return broadwell_init(dev);
747 static const struct cpu_ops cpu_x86_broadwell_ops = {
748 .get_desc = cpu_x86_get_desc,
749 .get_info = broadwell_get_info,
750 .get_count = broadwell_get_count,
751 .get_vendor = cpu_x86_get_vendor,
754 static const struct udevice_id cpu_x86_broadwell_ids[] = {
755 { .compatible = "intel,core-i3-gen5" },
759 U_BOOT_DRIVER(cpu_x86_broadwell_drv) = {
760 .name = "cpu_x86_broadwell",
762 .of_match = cpu_x86_broadwell_ids,
763 .bind = cpu_x86_bind,
764 .probe = cpu_x86_broadwell_probe,
765 .ops = &cpu_x86_broadwell_ops,
766 .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv),
767 .flags = DM_FLAG_PRE_RELOC,