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
13 #include <asm/cpu_x86.h>
14 #include <asm/cpu_common.h>
15 #include <asm/intel_regs.h>
18 #include <asm/turbo.h>
19 #include <asm/arch/cpu.h>
20 #include <asm/arch/pch.h>
21 #include <asm/arch/rcb.h>
23 struct cpu_broadwell_priv {
27 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
28 static const u8 power_limit_time_sec_to_msr[] = {
56 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
57 static const u8 power_limit_time_msr_to_sec[] = {
85 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
86 int arch_cpu_init(void)
93 * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate
94 * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly
95 * when a core is woken up
97 static int pcode_ready(void)
100 const int delay_step = 10;
104 if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) &
107 wait_count += delay_step;
109 } while (wait_count < 1000);
114 static u32 pcode_mailbox_read(u32 command)
120 debug("PCODE: mailbox timeout on wait ready\n");
124 /* Send command and start transaction */
125 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
129 debug("PCODE: mailbox timeout on completion\n");
134 return readl(MCHBAR_REG(BIOS_MAILBOX_DATA));
137 static int pcode_mailbox_write(u32 command, u32 data)
143 debug("PCODE: mailbox timeout on wait ready\n");
147 writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA));
149 /* Send command and start transaction */
150 writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
154 debug("PCODE: mailbox timeout on completion\n");
161 /* @dev is the CPU device */
162 static void initialize_vr_config(struct udevice *dev)
167 debug("Initializing VR config\n");
169 /* Configure VR_CURRENT_CONFIG */
170 msr = msr_read(MSR_VR_CURRENT_CONFIG);
172 * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid
175 msr.hi &= 0xc0000000;
176 msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */
177 msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */
178 msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */
179 msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */
180 /* Leave the max instantaneous current limit (12:0) to default */
181 msr_write(MSR_VR_CURRENT_CONFIG, msr);
183 /* Configure VR_MISC_CONFIG MSR */
184 msr = msr_read(MSR_VR_MISC_CONFIG);
185 /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */
186 msr.hi &= ~(0x3ff << (40 - 32));
187 msr.hi |= (0x200 << (40 - 32)); /* 1.0 */
188 /* Set IOUT_OFFSET to 0 */
190 /* Set entry ramp rate to slow */
191 msr.hi &= ~(1 << (51 - 32));
192 /* Enable decay mode on C-state entry */
193 msr.hi |= (1 << (52 - 32));
194 /* Set the slow ramp rate */
195 msr.hi &= ~(0x3 << (53 - 32));
196 /* Configure the C-state exit ramp rate */
197 ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
198 "intel,slow-ramp", -1);
200 /* Configured slow ramp rate */
201 msr.hi |= ((ramp & 0x3) << (53 - 32));
202 /* Set exit ramp rate to slow */
203 msr.hi &= ~(1 << (50 - 32));
205 /* Fast ramp rate / 4 */
206 msr.hi |= (0x01 << (53 - 32));
207 /* Set exit ramp rate to fast */
208 msr.hi |= (1 << (50 - 32));
210 /* Set MIN_VID (31:24) to allow CPU to have full control */
211 msr.lo &= ~0xff000000;
212 min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
214 msr.lo |= (min_vid & 0xff) << 24;
215 msr_write(MSR_VR_MISC_CONFIG, msr);
217 /* Configure VR_MISC_CONFIG2 MSR */
218 msr = msr_read(MSR_VR_MISC_CONFIG2);
221 * Allow CPU to control minimum voltage completely (15:8) and
222 * set the fast ramp voltage in 10mV steps
224 if (cpu_get_family_model() == BROADWELL_FAMILY_ULT)
225 msr.lo |= 0x006a; /* 1.56V */
227 msr.lo |= 0x006f; /* 1.60V */
228 msr_write(MSR_VR_MISC_CONFIG2, msr);
230 /* Set C9/C10 VCC Min */
231 pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f);
234 static int calibrate_24mhz_bclk(void)
243 /* A non-zero value initiates the PCODE calibration */
244 writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA));
245 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL,
246 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
252 err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff;
254 debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code);
256 /* Read the calibrated value */
257 writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION,
258 MCHBAR_REG(BIOS_MAILBOX_INTERFACE));
264 debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n",
265 readl(MCHBAR_REG(BIOS_MAILBOX_DATA)));
270 static void configure_pch_power_sharing(void)
272 u32 pch_power, pch_power_ext, pmsync, pmsync2;
275 /* Read PCH Power levels from PCODE */
276 pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER);
277 pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT);
279 debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power,
282 pmsync = readl(RCB_REG(PMSYNC_CONFIG));
283 pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2));
286 * Program PMSYNC_TPR_CONFIG PCH power limit values
287 * pmsync[0:4] = mailbox[0:5]
288 * pmsync[8:12] = mailbox[6:11]
289 * pmsync[16:20] = mailbox[12:17]
291 for (i = 0; i < 3; i++) {
292 u32 level = pch_power & 0x3f;
295 pmsync &= ~(0x1f << (i * 8));
296 pmsync |= (level & 0x1f) << (i * 8);
298 writel(pmsync, RCB_REG(PMSYNC_CONFIG));
301 * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values
302 * pmsync2[0:4] = mailbox[23:18]
303 * pmsync2[8:12] = mailbox_ext[6:11]
304 * pmsync2[16:20] = mailbox_ext[12:17]
305 * pmsync2[24:28] = mailbox_ext[18:22]
308 pmsync2 |= pch_power & 0x1f;
310 for (i = 1; i < 4; i++) {
311 u32 level = pch_power_ext & 0x3f;
314 pmsync2 &= ~(0x1f << (i * 8));
315 pmsync2 |= (level & 0x1f) << (i * 8);
317 writel(pmsync2, RCB_REG(PMSYNC_CONFIG2));
320 static int bsp_init_before_ap_bringup(struct udevice *dev)
324 initialize_vr_config(dev);
325 ret = calibrate_24mhz_bclk();
328 configure_pch_power_sharing();
333 static void set_max_ratio(void)
339 /* Check for configurable TDP option */
340 if (turbo_get_state() == TURBO_ENABLED) {
341 msr = msr_read(MSR_TURBO_RATIO_LIMIT);
342 perf_ctl.lo = (msr.lo & 0xff) << 8;
343 } else if (cpu_config_tdp_levels()) {
344 /* Set to nominal TDP ratio */
345 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
346 perf_ctl.lo = (msr.lo & 0xff) << 8;
348 /* Platform Info bits 15:8 give max ratio */
349 msr = msr_read(MSR_PLATFORM_INFO);
350 perf_ctl.lo = msr.lo & 0xff00;
352 msr_write(MSR_IA32_PERF_CTL, perf_ctl);
354 debug("cpu: frequency set to %d\n",
355 ((perf_ctl.lo >> 8) & 0xff) * INTEL_BCLK_MHZ);
358 int broadwell_init(struct udevice *dev)
360 struct cpu_broadwell_priv *priv = dev_get_priv(dev);
366 msr = msr_read(CORE_THREAD_COUNT_MSR);
367 num_threads = (msr.lo >> 0) & 0xffff;
368 num_cores = (msr.lo >> 16) & 0xffff;
369 debug("CPU has %u cores, %u threads enabled\n", num_cores,
372 priv->ht_disabled = num_threads == num_cores;
374 ret = bsp_init_before_ap_bringup(dev);
383 static void configure_mca(void)
386 const unsigned int mcg_cap_msr = 0x179;
390 msr = msr_read(mcg_cap_msr);
391 num_banks = msr.lo & 0xff;
395 * TODO(adurbin): This should only be done on a cold boot. Also, some
396 * of these banks are core vs package scope. For now every CPU clears
399 for (i = 0; i < num_banks; i++)
400 msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr);
403 static void enable_lapic_tpr(void)
407 msr = msr_read(MSR_PIC_MSG_CONTROL);
408 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
409 msr_write(MSR_PIC_MSG_CONTROL, msr);
412 static void configure_c_states(void)
416 msr = msr_read(MSR_PMG_CST_CONFIG_CONTROL);
417 msr.lo |= (1 << 31); /* Timed MWAIT Enable */
418 msr.lo |= (1 << 30); /* Package c-state Undemotion Enable */
419 msr.lo |= (1 << 29); /* Package c-state Demotion Enable */
420 msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */
421 msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */
422 msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */
423 msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */
424 msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */
425 /* The deepest package c-state defaults to factory-configured value */
426 msr_write(MSR_PMG_CST_CONFIG_CONTROL, msr);
428 msr = msr_read(MSR_MISC_PWR_MGMT);
429 msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */
430 msr_write(MSR_MISC_PWR_MGMT, msr);
432 msr = msr_read(MSR_POWER_CTL);
433 msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */
434 msr.lo |= (1 << 1); /* C1E Enable */
435 msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */
436 msr_write(MSR_POWER_CTL, msr);
438 /* C-state Interrupt Response Latency Control 0 - package C3 latency */
440 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT;
441 msr_write(MSR_C_STATE_LATENCY_CONTROL_0, msr);
443 /* C-state Interrupt Response Latency Control 1 */
445 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT;
446 msr_write(MSR_C_STATE_LATENCY_CONTROL_1, msr);
448 /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */
450 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT;
451 msr_write(MSR_C_STATE_LATENCY_CONTROL_2, msr);
453 /* C-state Interrupt Response Latency Control 3 - package C8 */
455 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT;
456 msr_write(MSR_C_STATE_LATENCY_CONTROL_3, msr);
458 /* C-state Interrupt Response Latency Control 4 - package C9 */
460 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT;
461 msr_write(MSR_C_STATE_LATENCY_CONTROL_4, msr);
463 /* C-state Interrupt Response Latency Control 5 - package C10 */
465 msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT;
466 msr_write(MSR_C_STATE_LATENCY_CONTROL_5, msr);
469 static void configure_misc(void)
473 msr = msr_read(MSR_IA32_MISC_ENABLE);
474 msr.lo |= MISC_ENABLE_FAST_STRING;
475 msr.lo |= MISC_ENABLE_TM1;
476 msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP;
477 msr_write(MSR_IA32_MISC_ENABLE, msr);
479 /* Disable thermal interrupts */
482 msr_write(MSR_IA32_THERM_INTERRUPT, msr);
484 /* Enable package critical interrupt only */
487 msr_write(MSR_IA32_PACKAGE_THERM_INTERRUPT, msr);
490 static void configure_dca_cap(void)
492 struct cpuid_result cpuid_regs;
495 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
496 cpuid_regs = cpuid(1);
497 if (cpuid_regs.ecx & (1 << 18)) {
498 msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP);
500 msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr);
504 static void set_energy_perf_bias(u8 policy)
509 /* Determine if energy efficient policy is supported */
510 ecx = cpuid_ecx(0x6);
511 if (!(ecx & (1 << 3)))
514 /* Energy Policy is bits 3:0 */
515 msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS);
517 msr.lo |= policy & 0xf;
518 msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr);
520 debug("cpu: energy policy set to %u\n", policy);
523 /* All CPUs including BSP will run the following function */
524 static void cpu_core_init(struct udevice *dev)
526 /* Clear out pending MCEs */
529 /* Enable the local cpu apics */
532 /* Configure C States */
533 configure_c_states();
535 /* Configure Enhanced SpeedStep and Thermal Sensors */
538 /* Thermal throttle activation offset */
539 cpu_configure_thermal_target(dev);
541 /* Enable Direct Cache Access */
544 /* Set energy policy */
545 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
552 * Configure processor power limits if possible
553 * This must be done AFTER set of BIOS_RESET_CPL
555 void cpu_set_power_limits(int power_limit_1_time)
560 uint tdp, min_power, max_power, max_time;
561 u8 power_limit_1_val;
563 msr = msr_read(MSR_PLATFORM_INFO);
564 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
565 power_limit_1_time = 28;
567 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
571 msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
572 power_unit = 2 << ((msr.lo & 0xf) - 1);
574 /* Get power defaults for this SKU */
575 msr = msr_read(MSR_PKG_POWER_SKU);
576 tdp = msr.lo & 0x7fff;
577 min_power = (msr.lo >> 16) & 0x7fff;
578 max_power = msr.hi & 0x7fff;
579 max_time = (msr.hi >> 16) & 0x7f;
581 debug("CPU TDP: %u Watts\n", tdp / power_unit);
583 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
584 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
586 if (min_power > 0 && tdp < min_power)
589 if (max_power > 0 && tdp > max_power)
592 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
594 /* Set long term power limit to TDP */
596 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
597 limit.lo |= PKG_POWER_LIMIT_EN;
598 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
599 PKG_POWER_LIMIT_TIME_SHIFT;
601 /* Set short term power limit to 1.25 * TDP */
603 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
604 limit.hi |= PKG_POWER_LIMIT_EN;
605 /* Power limit 2 time is only programmable on server SKU */
607 msr_write(MSR_PKG_POWER_LIMIT, limit);
609 /* Set power limit values in MCHBAR as well */
610 writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO));
611 writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI));
613 /* Set DDR RAPL power limit by copying from MMIO to MSR */
614 msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO));
615 msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI));
616 msr_write(MSR_DDR_RAPL_LIMIT, msr);
618 /* Use nominal TDP values for CPUs with configurable TDP */
619 if (cpu_config_tdp_levels()) {
620 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
622 limit.lo = msr.lo & 0xff;
623 msr_write(MSR_TURBO_ACTIVATION_RATIO, limit);
627 static int broadwell_get_info(struct udevice *dev, struct cpu_info *info)
629 return cpu_intel_get_info(info, INTEL_BCLK_MHZ);
632 static int broadwell_get_count(struct udevice *dev)
637 static int cpu_x86_broadwell_probe(struct udevice *dev)
641 return broadwell_init(dev);
647 static const struct cpu_ops cpu_x86_broadwell_ops = {
648 .get_desc = cpu_x86_get_desc,
649 .get_info = broadwell_get_info,
650 .get_count = broadwell_get_count,
651 .get_vendor = cpu_x86_get_vendor,
654 static const struct udevice_id cpu_x86_broadwell_ids[] = {
655 { .compatible = "intel,core-i3-gen5" },
659 U_BOOT_DRIVER(cpu_x86_broadwell_drv) = {
660 .name = "cpu_x86_broadwell",
662 .of_match = cpu_x86_broadwell_ids,
663 .bind = cpu_x86_bind,
664 .probe = cpu_x86_broadwell_probe,
665 .ops = &cpu_x86_broadwell_ops,
666 .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv),
667 .flags = DM_FLAG_PRE_RELOC,