blob: 874ce4d08d0d32a0f8eb7b714d3f42c438bc7981 [file] [log] [blame]
Stefan Reinauer5c554632012-04-04 00:09:50 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
6 *
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
10 * the License.
11 *
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.
16 *
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,
20 * MA 02110-1301 USA
21 */
22
23#include <console/console.h>
24#include <device/device.h>
25#include <device/pci.h>
26#include <string.h>
27#include <arch/acpi.h>
28#include <cpu/cpu.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>
38#include <usbdebug.h>
39#include "model_206ax.h"
40
41/*
42 * List of suported C-states in this processor
43 *
44 * Latencies are typical worst-case package exit time in uS
45 * taken from the SandyBridge BIOS specification.
46 */
47static acpi_cstate_t cstate_map[] = {
48 { /* 0: C0 */
49 },{ /* 1: C1 */
50 .latency = 1,
51 .power = 1000,
52 .resource = {
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,
58 }
59 },
60 { /* 2: C1E */
61 .latency = 1,
62 .power = 1000,
63 .resource = {
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,
69 }
70 },
71 { /* 3: C3 */
72 .latency = 63,
73 .power = 500,
74 .resource = {
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,
80 }
81 },
82 { /* 4: C6 */
83 .latency = 87,
84 .power = 350,
85 .resource = {
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,
91 }
92 },
93 { /* 5: C7 */
94 .latency = 90,
95 .power = 200,
96 .resource = {
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,
102 }
103 },
104 { /* 6: C7S */
105 .latency = 90,
106 .power = 200,
107 .resource = {
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,
113 }
114 },
115 { 0 }
116};
117
118static const uint32_t microcode_updates[] = {
119 #include "x06_microcode.h"
120};
121
122static void enable_vmx(void)
123{
124 struct cpuid_result regs;
125 msr_t msr;
126 int enable = CONFIG_ENABLE_VMX;
127
128 msr = rdmsr(IA32_FEATURE_CONTROL);
129
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
133 * instruction
134 */
135 return;
136 }
137
138 regs = cpuid(1);
139 printk(BIOS_DEBUG, "%s VMX\n", enable ? "Enabling" : "Disabling");
140 if (regs.ecx & CPUID_VMX) {
141 if (enable)
142 msr.lo |= (1 << 2);
143 else
144 msr.lo &= ~(1 << 2);
145
146 if (regs.ecx & CPUID_SMX) {
147 if (enable)
148 msr.lo |= (1 << 1);
149 else
150 msr.lo &= ~(1 << 1);
151 }
152 }
153
154 wrmsr(IA32_FEATURE_CONTROL, msr);
155}
156
157/* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
158static const u8 power_limit_time_sec_to_msr[] = {
159 [0] = 0x00,
160 [1] = 0x0a,
161 [2] = 0x0b,
162 [3] = 0x4b,
163 [4] = 0x0c,
164 [5] = 0x2c,
165 [6] = 0x4c,
166 [7] = 0x6c,
167 [8] = 0x0d,
168 [10] = 0x2d,
169 [12] = 0x4d,
170 [14] = 0x6d,
171 [16] = 0x0e,
172 [20] = 0x2e,
173 [24] = 0x4e,
174 [28] = 0x6e,
175 [32] = 0x0f,
176 [40] = 0x2f,
177 [48] = 0x4f,
178 [56] = 0x6f,
179 [64] = 0x10,
180 [80] = 0x30,
181 [96] = 0x50,
182 [112] = 0x70,
183 [128] = 0x11,
184};
185
186/* Convert POWER_LIMIT_1_TIME MSR value to seconds */
187static const u8 power_limit_time_msr_to_sec[] = {
188 [0x00] = 0,
189 [0x0a] = 1,
190 [0x0b] = 2,
191 [0x4b] = 3,
192 [0x0c] = 4,
193 [0x2c] = 5,
194 [0x4c] = 6,
195 [0x6c] = 7,
196 [0x0d] = 8,
197 [0x2d] = 10,
198 [0x4d] = 12,
199 [0x6d] = 14,
200 [0x0e] = 16,
201 [0x2e] = 20,
202 [0x4e] = 24,
203 [0x6e] = 28,
204 [0x0f] = 32,
205 [0x2f] = 40,
206 [0x4f] = 48,
207 [0x6f] = 56,
208 [0x10] = 64,
209 [0x30] = 80,
210 [0x50] = 96,
211 [0x70] = 112,
212 [0x11] = 128,
213};
214
215/*
216 * Configure processor power limits if possible
217 * This must be done AFTER set of BIOS_RESET_CPL
218 */
219void set_power_limits(u8 power_limit_1_time)
220{
221 msr_t msr = rdmsr(MSR_PLATFORM_INFO);
222 msr_t limit;
223 unsigned power_unit;
224 unsigned tdp, min_power, max_power, max_time;
225 u8 power_limit_1_val;
226
227 if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
228 return;
229
230 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
231 return;
232
233 /* Get units */
234 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
235 power_unit = 2 << ((msr.lo & 0xf) - 1);
236
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;
243
244 printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
245
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];
248
249 if (min_power > 0 && tdp < min_power)
250 tdp = min_power;
251
252 if (max_power > 0 && tdp > max_power)
253 tdp = max_power;
254
255 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
256
257 /* Set long term power limit to TDP */
258 limit.lo = 0;
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;
263
264 /* Set short term power limit to 1.25 * TDP */
265 limit.hi = 0;
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 */
269
270 wrmsr(MSR_PKG_POWER_LIMIT, limit);
271}
272
273static void configure_c_states(void)
274{
275 msr_t msr;
276
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);
285
286 msr = rdmsr(MSR_PMG_IO_CAPTURE_BASE);
287 msr.lo &= ~0x7ffff;
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);
291
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);
295
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);
301
302 /* C3 Interrupt Response Time Limit */
303 msr.hi = 0;
304 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
305 wrmsr(MSR_PKGC3_IRTL, msr);
306
307 /* C6 Interrupt Response Time Limit */
308 msr.hi = 0;
309 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
310 wrmsr(MSR_PKGC6_IRTL, msr);
311
312 /* C7 Interrupt Response Time Limit */
313 msr.hi = 0;
314 msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
315 wrmsr(MSR_PKGC7_IRTL, msr);
316
317 /* Primary Plane Current Limit */
318 msr = rdmsr(MSR_PP0_CURRENT_CONFIG);
319 msr.lo &= ~0x1fff;
320 msr.lo |= PP0_CURRENT_LIMIT;
321 wrmsr(MSR_PP0_CURRENT_CONFIG, msr);
322
323 /* Secondary Plane Current Limit */
324 msr = rdmsr(MSR_PP1_CURRENT_CONFIG);
325 msr.lo &= ~0x1fff;
326 msr.lo |= PP1_CURRENT_LIMIT;
327 wrmsr(MSR_PP1_CURRENT_CONFIG, msr);
328}
329
330static void configure_misc(void)
331{
332 msr_t msr;
333
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);
339
340 /* Disable Thermal interrupts */
341 msr.lo = 0;
342 msr.hi = 0;
343 wrmsr(IA32_THERM_INTERRUPT, msr);
344
345 /* Enable package critical interrupt only */
346 msr.lo = 1 << 4;
347 msr.hi = 0;
348 wrmsr(IA32_PACKAGE_THERM_INTERRUPT, msr);
349}
350
351static void enable_lapic_tpr(void)
352{
353 msr_t msr;
354
355 msr = rdmsr(MSR_PIC_MSG_CONTROL);
356 msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
357 wrmsr(MSR_PIC_MSG_CONTROL, msr);
358}
359
360static void configure_dca_cap(void)
361{
362 struct cpuid_result cpuid_regs;
363 msr_t msr;
364
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);
369 msr.lo |= 1;
370 wrmsr(IA32_PLATFORM_DCA_CAP, msr);
371 }
372}
373
374static void set_max_ratio(void)
375{
376 msr_t msr;
377
378 /* Platform Info bits 15:8 give max ratio */
379 msr = rdmsr(MSR_PLATFORM_INFO);
380 msr.hi = 0;
381 msr.lo &= 0xff00;
382 wrmsr(IA32_PERF_CTL, msr);
383
384 printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
385 ((msr.lo >> 8) & 0xff) * 100);
386}
387
388static void set_energy_perf_bias(u8 policy)
389{
390 msr_t msr;
391
392 /* Energy Policy is bits 3:0 */
393 msr = rdmsr(IA32_ENERGY_PERFORMANCE_BIAS);
394 msr.lo &= ~0xf;
395 msr.lo |= policy & 0xf;
396 wrmsr(IA32_ENERGY_PERFORMANCE_BIAS, msr);
397
398 printk(BIOS_DEBUG, "model_x06ax: energy policy set to %u\n",
399 policy);
400}
401
402static void configure_mca(void)
403{
404 msr_t msr;
405 int i;
406
407 msr.lo = msr.hi = 0;
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);
411}
412
413#if CONFIG_USBDEBUG
414static unsigned ehci_debug_addr;
415#endif
416
417/*
418 * Initialize any extra cores/threads in this package.
419 */
420static void intel_cores_init(device_t cpu)
421{
422 struct cpuid_result result;
423 unsigned cores, threads, i;
424
425 result = cpuid_ext(0xb, 0); /* Threads per core */
426 threads = result.ebx & 0xff;
427
428 result = cpuid_ext(0xb, 1); /* Cores per package */
429 cores = result.ebx & 0xff;
430
431 /* Only initialize extra cores from BSP */
432 if (cpu->path.apic.apic_id)
433 return;
434
435 printk(BIOS_DEBUG, "CPU: %u has %u cores %u threads\n",
436 cpu->path.apic.apic_id, cores, threads);
437
438 for (i = 1; i < cores; ++i) {
439 struct device_path cpu_path;
440 device_t new;
441
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;
446
447 /* Update APIC ID if no hyperthreading */
448 if (threads == 1)
449 cpu_path.apic.apic_id <<= 1;
450
451 /* Allocate the new cpu device structure */
452 new = alloc_dev(cpu->bus, &cpu_path);
453 if (!new)
454 continue;
455
456 printk(BIOS_DEBUG, "CPU: %u has core %u\n",
457 cpu->path.apic.apic_id,
458 new->path.apic.apic_id);
459
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);
465 }
466 }
467}
468
469static void model_206ax_init(device_t cpu)
470{
471 char processor_name[49];
472 struct cpuid_result cpuid_regs;
473
474 /* Turn on caching if we haven't already */
475 x86_enable_cache();
476
477 /* Update the microcode */
478 intel_update_microcode(microcode_updates);
479
480 /* Clear out pending MCEs */
481 configure_mca();
482
483 /* Print processor name */
484 fill_processor_name(processor_name);
485 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
486
487#if CONFIG_USBDEBUG
488 // Is this caution really needed?
489 if(!ehci_debug_addr)
490 ehci_debug_addr = get_ehci_debug();
491 set_ehci_debug(0);
492#endif
493
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);
498 x86_mtrr_check();
499
500 /* Setup Page Attribute Tables (PAT) */
501 // TODO set up PAT
502
503#if CONFIG_USBDEBUG
504 set_ehci_debug(ehci_debug_addr);
505#endif
506
507 /* Enable the local cpu apics */
508 enable_lapic_tpr();
509 setup_lapic();
510
511 /* Enable virtualization if enabled in CMOS */
512 enable_vmx();
513
514 /* Configure C States */
515 configure_c_states();
516
517 /* Configure Enhanced SpeedStep and Thermal Sensors */
518 configure_misc();
519
520 /* Enable Direct Cache Access */
521 configure_dca_cap();
522
523 /* Set energy policy */
524 set_energy_perf_bias(ENERGY_POLICY_NORMAL);
525
526 /* Set Max Ratio */
527 set_max_ratio();
528
529 /* Enable Turbo */
530 enable_turbo();
531
532 /* Start up extra cores */
533 intel_cores_init(cpu);
534}
535
536static struct device_operations cpu_dev_ops = {
537 .init = model_206ax_init,
538};
539
540static 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 */
550 { 0, 0 },
551};
552
553static const struct cpu_driver driver __cpu_driver = {
554 .ops = &cpu_dev_ops,
555 .id_table = cpu_table,
556 .cstates = cstate_map,
557};
558