2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 #include <console/console.h>
24 #include <device/device.h>
25 #include <device/pci.h>
27 #include <arch/acpi.h>
29 #include <cpu/x86/mtrr.h>
30 #include <cpu/x86/msr.h>
31 #include <cpu/x86/lapic.h>
32 #include <cpu/intel/microcode.h>
33 #include <cpu/intel/speedstep.h>
34 #include <cpu/intel/turbo.h>
35 #include <cpu/x86/cache.h>
36 #include <cpu/x86/name.h>
37 #include <pc80/mc146818rtc.h>
39 #include "model_206ax.h"
42 * List of suported C-states in this processor
44 * Latencies are typical worst-case package exit time in uS
45 * taken from the SandyBridge BIOS specification.
47 static acpi_cstate_t cstate_map[] = {
53 .addrl = 0x00, /* MWAIT State 0 */
54 .space_id = ACPI_ADDRESS_SPACE_FIXED,
55 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL,
56 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT,
57 .resv = ACPI_FFIXEDHW_FLAG_HW_COORD,
64 .addrl = 0x01, /* MWAIT State 0 Sub-state 1 */
65 .space_id = ACPI_ADDRESS_SPACE_FIXED,
66 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL,
67 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT,
68 .resv = ACPI_FFIXEDHW_FLAG_HW_COORD,
75 .addrl = 0x10, /* MWAIT State 1 */
76 .space_id = ACPI_ADDRESS_SPACE_FIXED,
77 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL,
78 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT,
79 .resv = ACPI_FFIXEDHW_FLAG_HW_COORD,
86 .addrl = 0x20, /* MWAIT State 2 */
87 .space_id = ACPI_ADDRESS_SPACE_FIXED,
88 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL,
89 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT,
90 .resv = ACPI_FFIXEDHW_FLAG_HW_COORD,
97 .addrl = 0x30, /* MWAIT State 3 */
98 .space_id = ACPI_ADDRESS_SPACE_FIXED,
99 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL,
100 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT,
101 .resv = ACPI_FFIXEDHW_FLAG_HW_COORD,
108 .addrl = 0x31, /* MWAIT State 3 Sub-state 1 */
109 .space_id = ACPI_ADDRESS_SPACE_FIXED,
110 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL,
111 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT,
112 .resv = ACPI_FFIXEDHW_FLAG_HW_COORD,
118 static const uint32_t microcode_updates[] = {
119 #include "x06_microcode.h"
122 static void enable_vmx(void)
124 struct cpuid_result regs;
126 int enable = CONFIG_ENABLE_VMX;
128 msr = rdmsr(IA32_FEATURE_CONTROL);
130 if (msr.lo & (1 << 0)) {
131 printk(BIOS_ERR, "VMX is locked, so enable_vmx will do nothing\n");
132 /* VMX locked. If we set it again we get an illegal
139 printk(BIOS_DEBUG, "%s VMX\n", enable ? "Enabling" : "Disabling");
140 if (regs.ecx & CPUID_VMX) {
146 if (regs.ecx & CPUID_SMX) {
154 wrmsr(IA32_FEATURE_CONTROL, msr);
157 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
158 static const u8 power_limit_time_sec_to_msr[] = {
186 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
187 static const u8 power_limit_time_msr_to_sec[] = {
216 * Configure processor power limits if possible
217 * This must be done AFTER set of BIOS_RESET_CPL
219 void set_power_limits(u8 power_limit_1_time)
221 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
224 unsigned tdp, min_power, max_power, max_time;
225 u8 power_limit_1_val;
227 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
230 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
234 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
235 power_unit = 2 << ((msr.lo & 0xf) - 1);
237 /* Get power defaults for this SKU */
238 msr = rdmsr(MSR_PKG_POWER_SKU);
239 tdp = msr.lo & 0x7fff;
240 min_power = (msr.lo >> 16) & 0x7fff;
241 max_power = msr.hi & 0x7fff;
242 max_time = (msr.hi >> 16) & 0x7f;
244 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
246 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
247 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
249 if (min_power > 0 && tdp < min_power)
252 if (max_power > 0 && tdp > max_power)
255 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
257 /* Set long term power limit to TDP */
259 limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
260 limit.lo |= PKG_POWER_LIMIT_EN;
261 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
262 PKG_POWER_LIMIT_TIME_SHIFT;
264 /* Set short term power limit to 1.25 * TDP */
266 limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
267 limit.hi |= PKG_POWER_LIMIT_EN;
268 /* Power limit 2 time is only programmable on SNB EP/EX */
270 wrmsr(MSR_PKG_POWER_LIMIT, limit);
273 static void configure_c_states(void)
277 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
278 msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
279 msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
280 msr.lo |= (1 << 26); // C1 Auto Demotion Enable
281 msr.lo |= (1 << 25); // C3 Auto Demotion Enable
282 msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
283 msr.lo |= 7; // No package C-state limit
284 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
286 msr = rdmsr(MSR_PMG_IO_CAPTURE_BASE);
288 msr.lo |= (PMB0_BASE + 4); // LVL_2 base address
289 msr.lo |= (2 << 16); // CST Range: C7 is max C-state
290 wrmsr(MSR_PMG_IO_CAPTURE_BASE, msr);
292 msr = rdmsr(MSR_MISC_PWR_MGMT);
293 msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
294 wrmsr(MSR_MISC_PWR_MGMT, msr);
296 msr = rdmsr(MSR_POWER_CTL);
297 msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
298 msr.lo |= (1 << 1); // C1E Enable
299 msr.lo |= (1 << 0); // Bi-directional PROCHOT#
300 wrmsr(MSR_POWER_CTL, msr);
302 /* C3 Interrupt Response Time Limit */
304 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
305 wrmsr(MSR_PKGC3_IRTL, msr);
307 /* C6 Interrupt Response Time Limit */
309 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
310 wrmsr(MSR_PKGC6_IRTL, msr);
312 /* C7 Interrupt Response Time Limit */
314 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
315 wrmsr(MSR_PKGC7_IRTL, msr);
317 /* Primary Plane Current Limit */
318 msr = rdmsr(MSR_PP0_CURRENT_CONFIG);
320 msr.lo |= PP0_CURRENT_LIMIT;
321 wrmsr(MSR_PP0_CURRENT_CONFIG, msr);
323 /* Secondary Plane Current Limit */
324 msr = rdmsr(MSR_PP1_CURRENT_CONFIG);
326 msr.lo |= PP1_CURRENT_LIMIT;
327 wrmsr(MSR_PP1_CURRENT_CONFIG, msr);
330 static void configure_misc(void)
334 msr = rdmsr(IA32_MISC_ENABLE);
335 msr.lo |= (1 << 0); /* Fast String enable */
336 msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
337 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
338 wrmsr(IA32_MISC_ENABLE, msr);
340 /* Disable Thermal interrupts */
343 wrmsr(IA32_THERM_INTERRUPT, msr);
345 /* Enable package critical interrupt only */
348 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
351 static void enable_lapic_tpr(void)
355 msr = rdmsr(MSR_PIC_MSG_CONTROL);
356 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
357 wrmsr(MSR_PIC_MSG_CONTROL, msr);
360 static void configure_dca_cap(void)
362 struct cpuid_result cpuid_regs;
365 /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
366 cpuid_regs = cpuid(1);
367 if (cpuid_regs.ecx & (1 << 18)) {
368 msr = rdmsr(IA32_PLATFORM_DCA_CAP);
370 wrmsr(IA32_PLATFORM_DCA_CAP, msr);
374 static void set_max_ratio(void)
378 /* Platform Info bits 15:8 give max ratio */
379 msr = rdmsr(MSR_PLATFORM_INFO);
382 wrmsr(IA32_PERF_CTL, msr);
384 printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
385 ((msr.lo >> 8) & 0xff) * 100);
388 static void set_energy_perf_bias(u8 policy)
392 /* Energy Policy is bits 3:0 */
393 msr = rdmsr(IA32_ENERGY_PERFORMANCE_BIAS);
395 msr.lo |= policy & 0xf;
396 wrmsr(IA32_ENERGY_PERFORMANCE_BIAS, msr);
398 printk(BIOS_DEBUG, "model_x06ax: energy policy set to %u\n",
402 static void configure_mca(void)
408 /* This should only be done on a cold boot */
409 for (i = 0; i < 7; i++)
410 wrmsr(IA32_MC0_STATUS + (i * 4), msr);
414 static unsigned ehci_debug_addr;
418 * Initialize any extra cores/threads in this package.
420 static void intel_cores_init(device_t cpu)
422 struct cpuid_result result;
423 unsigned cores, threads, i;
425 result = cpuid_ext(0xb, 0); /* Threads per core */
426 threads = result.ebx & 0xff;
428 result = cpuid_ext(0xb, 1); /* Cores per package */
429 cores = result.ebx & 0xff;
431 /* Only initialize extra cores from BSP */
432 if (cpu->path.apic.apic_id)
435 printk(BIOS_DEBUG, "CPU: %u has %u cores %u threads\n",
436 cpu->path.apic.apic_id, cores, threads);
438 for (i = 1; i < cores; ++i) {
439 struct device_path cpu_path;
442 /* Build the cpu device path */
443 cpu_path.type = DEVICE_PATH_APIC;
444 cpu_path.apic.apic_id =
445 cpu->path.apic.apic_id + i;
447 /* Update APIC ID if no hyperthreading */
449 cpu_path.apic.apic_id <<= 1;
451 /* Allocate the new cpu device structure */
452 new = alloc_dev(cpu->bus, &cpu_path);
456 printk(BIOS_DEBUG, "CPU: %u has core %u\n",
457 cpu->path.apic.apic_id,
458 new->path.apic.apic_id);
460 /* Start the new cpu */
461 if (!start_cpu(new)) {
462 /* Record the error in cpu? */
463 printk(BIOS_ERR, "CPU %u would not start!\n",
464 new->path.apic.apic_id);
469 static void model_206ax_init(device_t cpu)
471 char processor_name[49];
472 struct cpuid_result cpuid_regs;
474 /* Turn on caching if we haven't already */
477 /* Update the microcode */
478 intel_update_microcode(microcode_updates);
480 /* Clear out pending MCEs */
483 /* Print processor name */
484 fill_processor_name(processor_name);
485 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
488 // Is this caution really needed?
490 ehci_debug_addr = get_ehci_debug();
494 /* Setup MTRRs based on physical address size */
495 cpuid_regs = cpuid(0x80000008);
496 x86_setup_fixed_mtrrs();
497 x86_setup_var_mtrrs(cpuid_regs.eax & 0xff, 2);
500 /* Setup Page Attribute Tables (PAT) */
504 set_ehci_debug(ehci_debug_addr);
507 /* Enable the local cpu apics */
511 /* Enable virtualization if enabled in CMOS */
514 /* Configure C States */
515 configure_c_states();
517 /* Configure Enhanced SpeedStep and Thermal Sensors */
520 /* Enable Direct Cache Access */
523 /* Set energy policy */
524 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
532 /* Start up extra cores */
533 intel_cores_init(cpu);
536 static struct device_operations cpu_dev_ops = {
537 .init = model_206ax_init,
540 static struct cpu_device_id cpu_table[] = {
541 { X86_VENDOR_INTEL, 0x206a0 }, /* Intel Sandybridge */
542 { X86_VENDOR_INTEL, 0x206a6 }, /* Intel Sandybridge D1 */
543 { X86_VENDOR_INTEL, 0x206a7 }, /* Intel Sandybridge D2/J1 */
544 { X86_VENDOR_INTEL, 0x306a2 }, /* Intel IvyBridge */
545 { X86_VENDOR_INTEL, 0x306a4 }, /* Intel IvyBridge */
546 { X86_VENDOR_INTEL, 0x306a5 }, /* Intel IvyBridge */
547 { X86_VENDOR_INTEL, 0x306a6 }, /* Intel IvyBridge */
548 { X86_VENDOR_INTEL, 0x306a8 }, /* Intel IvyBridge */
549 { X86_VENDOR_INTEL, 0x306a9 }, /* Intel IvyBridge */
553 static const struct cpu_driver driver __cpu_driver = {
555 .id_table = cpu_table,
556 .cstates = cstate_map,