blob: e81a6a729110ced7edc7b5cd1d19cb165f9d7aa8 [file] [log] [blame]
Thomas Jourdan1a692d82009-07-01 17:01:17 +00001/*
2 * This file is part of the coreboot project.
Stefan Reinauer14e22772010-04-27 06:56:47 +00003 *
Thomas Jourdan1a692d82009-07-01 17:01:17 +00004 * Copyright (C) 2007-2009 coresystems GmbH
Nico Huber68d7c7a2012-10-02 11:46:11 +02005 * 2012 secunet Security Networks AG
Thomas Jourdan1a692d82009-07-01 17:01:17 +00006 *
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 <cpu/cpu.h>
28#include <cpu/x86/mtrr.h>
29#include <cpu/x86/msr.h>
30#include <cpu/x86/lapic.h>
31#include <cpu/intel/microcode.h>
Stefan Reinauer2a27b202010-12-11 22:14:44 +000032#include <cpu/intel/speedstep.h>
Sven Schnelle51676b12012-07-29 19:18:03 +020033#include <cpu/intel/hyperthreading.h>
Thomas Jourdan1a692d82009-07-01 17:01:17 +000034#include <cpu/x86/cache.h>
Uwe Hermannaac8f662010-09-29 09:54:16 +000035#include <cpu/x86/name.h>
Thomas Jourdan1a692d82009-07-01 17:01:17 +000036
Nico Huber68d7c7a2012-10-02 11:46:11 +020037#include "chip.h"
38
Thomas Jourdan1a692d82009-07-01 17:01:17 +000039static const uint32_t microcode_updates[] = {
Stefan Reinauerc104cb02010-10-18 00:21:39 +000040 #include "microcode-2618-m441067AA07.h"
41 #include "microcode-2626-m1010677705.h"
42 #include "microcode-2498-m101067660C.h"
43 #include "microcode-2497-m041067660C.h"
44 #include "microcode-2499-m401067660C.h"
45 #include "microcode-2617-m111067AA07.h"
46 #include "microcode-2619-mA01067AA07.h"
47 #include "microcode-2623-m011067660C.h"
48 #include "microcode-2501-m801067660C.h"
49
Thomas Jourdan1a692d82009-07-01 17:01:17 +000050 /* Dummy terminator */
51 0x0, 0x0, 0x0, 0x0,
52 0x0, 0x0, 0x0, 0x0,
53 0x0, 0x0, 0x0, 0x0,
54 0x0, 0x0, 0x0, 0x0,
55};
56
Thomas Jourdan1a692d82009-07-01 17:01:17 +000057static void init_timer(void)
58{
59 /* Set the apic timer to no interrupts and periodic mode */
60 lapic_write(LAPIC_LVTT, (1 << 17)|(1<< 16)|(0 << 12)|(0 << 0));
61
62 /* Set the divider to 1, no divider */
63 lapic_write(LAPIC_TDCR, LAPIC_TDR_DIV_1);
64
65 /* Set the initial counter to 0xffffffff */
66 lapic_write(LAPIC_TMICT, 0xffffffff);
67}
68
Thomas Jourdan1a692d82009-07-01 17:01:17 +000069#define IA32_FEATURE_CONTROL 0x003a
70
71#define CPUID_VMX (1 << 5)
72#define CPUID_SMX (1 << 6)
73static void enable_vmx(void)
74{
75 struct cpuid_result regs;
76 msr_t msr;
77
78 msr = rdmsr(IA32_FEATURE_CONTROL);
79
80 if (msr.lo & (1 << 0)) {
81 /* VMX locked. If we set it again we get an illegal
82 * instruction
83 */
84 return;
85 }
86
87 regs = cpuid(1);
88 if (regs.ecx & CPUID_VMX) {
89 msr.lo |= (1 << 2);
90 if (regs.ecx & CPUID_SMX)
91 msr.lo |= (1 << 1);
92 }
93
94 wrmsr(IA32_FEATURE_CONTROL, msr);
95
96 msr.lo |= (1 << 0); /* Set lock bit */
97
98 wrmsr(IA32_FEATURE_CONTROL, msr);
99}
100
Nico Huber68d7c7a2012-10-02 11:46:11 +0200101#define MSR_BBL_CR_CTL3 0x11e
102#define MSR_FSB_FREQ 0xcd
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000103
Nico Huber68d7c7a2012-10-02 11:46:11 +0200104static void configure_c_states(const int quad)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000105{
106 msr_t msr;
107
Nico Huber68d7c7a2012-10-02 11:46:11 +0200108 /* Find pointer to CPU configuration. */
109 const device_t lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
110 const struct cpu_intel_model_1067x_config *const conf =
111 (lapic && lapic->chip_info) ? lapic->chip_info : NULL;
112
113 /* Is C5 requested and supported? */
114 const int c5 = conf && conf->c5 &&
115 (rdmsr(MSR_BBL_CR_CTL3).lo & (3 << 30)) &&
116 !(rdmsr(MSR_FSB_FREQ).lo & (1 << 31));
117 /* Is C6 requested and supported? */
118 const int c6 = conf && conf->c6 &&
119 ((cpuid_edx(5) >> (6 * 4)) & 0xf) && c5;
120
121 const int cst_range = (c6 ? 6 : (c5 ? 5 : 4)) - 2; /* zero means lvl2 */
122
Patrick Georgi644e83b2013-02-09 15:35:30 +0100123 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000124 msr.lo &= ~(1 << 9); // Issue a single stop grant cycle upon stpclk
Nico Huber68d7c7a2012-10-02 11:46:11 +0200125 msr.lo |= (1 << 8);
126 if (quad) {
127 msr.lo = (msr.lo & ~(7 << 0)) | (4 << 0);
128 }
129 if (c5) {
130 msr.lo &= ~(1 << 13);
131 msr.lo &= ~(7 << 0);
132 msr.lo |= (1 << 3); /* Enable dynamic L2. */
133 msr.lo |= (1 << 14); /* Enable deeper sleep */
134 }
135 /* Next two fields seem to be mutually exclusive: */
136 msr.lo &= ~(7 << 4);
137 msr.lo |= (1 << 10); /* Enable IO MWAIT redirection. */
138 if (c6)
139 msr.lo |= (1 << 25);
Patrick Georgi644e83b2013-02-09 15:35:30 +0100140 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000141
142 /* Set Processor MWAIT IO BASE */
143 msr.hi = 0;
144 msr.lo = ((PMB0_BASE + 4) & 0xffff) | (((PMB1_BASE + 9) & 0xffff) << 16);
Patrick Georgi644e83b2013-02-09 15:35:30 +0100145 wrmsr(MSR_PMG_IO_BASE_ADDR, msr);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000146
147 /* Set IO Capture Address */
148 msr.hi = 0;
Nico Huber68d7c7a2012-10-02 11:46:11 +0200149 msr.lo = ((PMB0_BASE + 4) & 0xffff) | ((cst_range & 0xffff) << 16);
Patrick Georgi644e83b2013-02-09 15:35:30 +0100150 wrmsr(MSR_PMG_IO_CAPTURE_ADDR, msr);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200151
152 if (c5) {
153 msr = rdmsr(MSR_BBL_CR_CTL3);
154 msr.lo &= ~(7 << 25);
155 msr.lo |= (2 << 25);
156 msr.lo &= ~(3 << 30);
157 msr.lo |= (1 << 30);
158 wrmsr(MSR_BBL_CR_CTL3, msr);
159 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000160}
161
Nico Huber68d7c7a2012-10-02 11:46:11 +0200162static void configure_p_states(const char stepping, const char cores)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000163{
164 msr_t msr;
165
Nico Huber68d7c7a2012-10-02 11:46:11 +0200166 /* Find pointer to CPU configuration. */
167 const device_t lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
168 struct cpu_intel_model_1067x_config *const conf =
169 (lapic && lapic->chip_info) ? lapic->chip_info : NULL;
170
171 msr = rdmsr(MSR_EXTENDED_CONFIG);
172 if (conf->slfm && (msr.lo & (1 << 27))) /* Super LFM supported? */
173 msr.lo |= (1 << 28); /* Enable Super LFM. */
174 wrmsr(MSR_EXTENDED_CONFIG, msr);
175
176 if (rdmsr(MSR_FSB_CLOCK_VCC).hi & (1 << (63 - 32))) {
177 /* Turbo supported? */
178 if ((stepping == 0xa) && (cores < 4)) {
179 msr = rdmsr(MSR_FSB_FREQ);
180 msr.lo |= (1 << 3); /* Enable hysteresis. */
181 wrmsr(MSR_FSB_FREQ, msr);
182 }
183 msr = rdmsr(IA32_PERF_CTL);
184 msr.hi &= ~(1 << (32 - 32)); /* Clear turbo disable. */
185 wrmsr(IA32_PERF_CTL, msr);
186 }
187
Patrick Georgi644e83b2013-02-09 15:35:30 +0100188 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200189 msr.lo &= ~(1 << 11); /* Enable hw coordination. */
190 msr.lo |= (1 << 15); /* Lock config until next reset. */
Patrick Georgi644e83b2013-02-09 15:35:30 +0100191 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200192}
193
194#define MSR_EMTTM_CR_TABLE(x) (0xa8 + (x))
195#define MSR_EMTTM_TABLE_NUM 6
196static void configure_emttm_tables(void)
197{
198 int i;
199 int num_states, pstate_idx;
200 msr_t msr;
201 sst_table_t pstates;
202
203 /* Gather p-state information. */
204 speedstep_gen_pstates(&pstates);
205
206 /* Never turbo mode or Super LFM. */
207 num_states = pstates.num_states;
208 if (pstates.states[0].is_turbo)
209 --num_states;
210 if (pstates.states[pstates.num_states - 1].is_slfm)
211 --num_states;
212 /* Repeat lowest p-state if we haven't enough states. */
213 const int num_lowest_pstate =
214 (num_states < MSR_EMTTM_TABLE_NUM)
215 ? (MSR_EMTTM_TABLE_NUM - num_states) + 1
216 : 1;
217 /* Start from the lowest entry but skip Super LFM. */
218 if (pstates.states[pstates.num_states - 1].is_slfm)
219 pstate_idx = pstates.num_states - 2;
220 else
221 pstate_idx = pstates.num_states - 1;
222 for (i = 0; i < MSR_EMTTM_TABLE_NUM; ++i) {
223 if (i >= num_lowest_pstate)
224 --pstate_idx;
225 const sst_state_t *const pstate = &pstates.states[pstate_idx];
226 printk(BIOS_DEBUG, "writing P-State %d: %d, %d, "
227 "%2d, 0x%02x, %d; encoded: 0x%04x\n",
228 pstate_idx, pstate->dynfsb, pstate->nonint,
229 pstate->ratio, pstate->vid, pstate->power,
230 SPEEDSTEP_ENCODE_STATE(*pstate));
231 msr.hi = 0;
232 msr.lo = SPEEDSTEP_ENCODE_STATE(pstates.states[pstate_idx]) &
233 /* Don't set half ratios. */
234 ~SPEEDSTEP_RATIO_NONINT;
235 wrmsr(MSR_EMTTM_CR_TABLE(i), msr);
236 }
237
238 msr = rdmsr(MSR_EMTTM_CR_TABLE(5));
239 msr.lo |= (1 << 31); /* lock tables */
240 wrmsr(MSR_EMTTM_CR_TABLE(5), msr);
241}
242
243static void configure_misc(const int eist, const int tm2, const int emttm)
244{
245 msr_t msr;
246
247 const u32 sub_cstates = cpuid_edx(5);
248
249 msr = rdmsr(IA32_MISC_ENABLES);
250 msr.lo |= (1 << 3); /* TM1 enable */
251 if (tm2)
252 msr.lo |= (1 << 13); /* TM2 enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000253 msr.lo |= (1 << 17); /* Bidirectional PROCHOT# */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200254 msr.lo |= (1 << 18); /* MONITOR/MWAIT enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000255
256 msr.lo |= (1 << 10); /* FERR# multiplexing */
257
Nico Huber68d7c7a2012-10-02 11:46:11 +0200258 if (eist)
259 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000260
261 /* Enable C2E */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200262 if (((sub_cstates >> (2 * 4)) & 0xf) >= 2) {
263 msr.lo |= (1 << 26);
264 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000265
266 /* Enable C4E */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200267 if (((sub_cstates >> (4 * 4)) & 0xf) >= 2) {
268 msr.hi |= (1 << (32 - 32)); // C4E
269 msr.hi |= (1 << (33 - 32)); // Hard C4E
270 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000271
Nico Huber68d7c7a2012-10-02 11:46:11 +0200272 /* Enable EMTTM */
273 if (emttm)
274 msr.hi |= (1 << (36 - 32));
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000275
Nico Huber68d7c7a2012-10-02 11:46:11 +0200276 /* Enable turbo mode */
277 if (rdmsr(MSR_FSB_CLOCK_VCC).hi & (1 << (63 - 32)))
278 msr.hi &= ~(1 << (38 - 32));
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000279
Nico Huber68d7c7a2012-10-02 11:46:11 +0200280 wrmsr(IA32_MISC_ENABLES, msr);
281
282 if (eist) {
283 msr.lo |= (1 << 20); /* Lock Enhanced SpeedStep Enable */
284 wrmsr(IA32_MISC_ENABLES, msr);
285 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000286}
287
288#define PIC_SENS_CFG 0x1aa
Nico Huber68d7c7a2012-10-02 11:46:11 +0200289static void configure_pic_thermal_sensors(const int tm2, const int quad)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000290{
291 msr_t msr;
292
293 msr = rdmsr(PIC_SENS_CFG);
294
Nico Huber68d7c7a2012-10-02 11:46:11 +0200295 if (quad)
296 msr.lo |= (1 << 31);
297 else
298 msr.lo &= ~(1 << 31);
299 if (tm2)
300 msr.lo |= (1 << 20); /* Enable TM1 if TM2 fails. */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000301 msr.lo |= (1 << 21); // inter-core lock TM1
Nico Huber68d7c7a2012-10-02 11:46:11 +0200302 msr.lo |= (1 << 4); // Enable bypass filter /* What does it do? */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000303
304 wrmsr(PIC_SENS_CFG, msr);
305}
306
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000307#if CONFIG_USBDEBUG
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000308static unsigned ehci_debug_addr;
309#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +0000310
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000311static void model_1067x_init(device_t cpu)
312{
313 char processor_name[49];
314
Nico Huber68d7c7a2012-10-02 11:46:11 +0200315
316 /* Gather some information: */
317
318 const struct cpuid_result cpuid1 = cpuid(1);
319
320 /* Read stepping. */
321 const char stepping = cpuid1.eax & 0xf;
322 /* Read number of cores. */
323 const char cores = (cpuid1.ebx >> 16) & 0xf;
324 /* Is this a quad core? */
325 const char quad = cores > 2;
326 /* Is this even a multiprocessor? */
327 const char mp = cores > 1;
328
329 /* Enable EMTTM on uni- and on multi-processors if it's not disabled. */
330 const char emttm = !mp || !(rdmsr(MSR_EXTENDED_CONFIG).lo & 4);
331
332 /* Is enhanced speedstep supported? */
333 const char eist = (cpuid1.ecx & (1 << 7)) &&
334 !(rdmsr(IA32_PLATFORM_ID).lo & (1 << 17));
335 /* Test for TM2 only if EIST is available. */
336 const char tm2 = eist && (cpuid1.ecx & (1 << 8));
337
338
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000339 /* Turn on caching if we haven't already */
340 x86_enable_cache();
341
342 /* Update the microcode */
343 intel_update_microcode(microcode_updates);
344
345 /* Print processor name */
346 fill_processor_name(processor_name);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000347 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000348
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000349#if CONFIG_USBDEBUG
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000350 // Is this caution really needed?
Stefan Reinauer14e22772010-04-27 06:56:47 +0000351 if(!ehci_debug_addr)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000352 ehci_debug_addr = get_ehci_debug();
353 set_ehci_debug(0);
354#endif
355
356 /* Setup MTRRs */
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100357 x86_setup_mtrrs();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000358 x86_mtrr_check();
359
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000360#if CONFIG_USBDEBUG
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000361 set_ehci_debug(ehci_debug_addr);
362#endif
363
364 /* Enable the local cpu apics */
365 setup_lapic();
366
367 /* Initialize the APIC timer */
368 init_timer();
369
370 /* Enable virtualization */
371 enable_vmx();
372
373 /* Configure C States */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200374 configure_c_states(quad);
375
376 /* Configure P States */
377 configure_p_states(stepping, cores);
378
379 /* EMTTM */
380 if (emttm)
381 configure_emttm_tables();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000382
383 /* Configure Enhanced SpeedStep and Thermal Sensors */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200384 configure_misc(eist, tm2, emttm);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000385
386 /* PIC thermal sensor control */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200387 configure_pic_thermal_sensors(tm2, quad);
Sven Schnelle51676b12012-07-29 19:18:03 +0200388
389 /* Start up my cpu siblings */
390 intel_sibling_init(cpu);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000391}
392
393static struct device_operations cpu_dev_ops = {
394 .init = model_1067x_init,
395};
396
397static struct cpu_device_id cpu_table[] = {
398 { X86_VENDOR_INTEL, 0x10676 }, /* Intel Core 2 Solo/Core Duo */
Stefan Reinauerc104cb02010-10-18 00:21:39 +0000399 { X86_VENDOR_INTEL, 0x10677 },
400 { X86_VENDOR_INTEL, 0x1067A },
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000401 { 0, 0 },
402};
403
404static const struct cpu_driver driver __cpu_driver = {
405 .ops = &cpu_dev_ops,
406 .id_table = cpu_table,
407};
408
Nico Huber68d7c7a2012-10-02 11:46:11 +0200409struct chip_operations cpu_intel_model_1067x_ops = {
410 CHIP_NAME("Intel Penryn CPU")
411};