blob: 2b7839be2c631d0f689cb204a3da76a5d9344579 [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
101#define PMG_CST_CONFIG_CONTROL 0xe2
102#define PMG_IO_BASE_ADDR 0xe3
103#define PMG_IO_CAPTURE_ADDR 0xe4
Nico Huber68d7c7a2012-10-02 11:46:11 +0200104#define MSR_BBL_CR_CTL3 0x11e
105#define MSR_FSB_FREQ 0xcd
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000106
Nico Huber68d7c7a2012-10-02 11:46:11 +0200107static void configure_c_states(const int quad)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000108{
109 msr_t msr;
110
Nico Huber68d7c7a2012-10-02 11:46:11 +0200111 /* Find pointer to CPU configuration. */
112 const device_t lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
113 const struct cpu_intel_model_1067x_config *const conf =
114 (lapic && lapic->chip_info) ? lapic->chip_info : NULL;
115
116 /* Is C5 requested and supported? */
117 const int c5 = conf && conf->c5 &&
118 (rdmsr(MSR_BBL_CR_CTL3).lo & (3 << 30)) &&
119 !(rdmsr(MSR_FSB_FREQ).lo & (1 << 31));
120 /* Is C6 requested and supported? */
121 const int c6 = conf && conf->c6 &&
122 ((cpuid_edx(5) >> (6 * 4)) & 0xf) && c5;
123
124 const int cst_range = (c6 ? 6 : (c5 ? 5 : 4)) - 2; /* zero means lvl2 */
125
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000126 msr = rdmsr(PMG_CST_CONFIG_CONTROL);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000127 msr.lo &= ~(1 << 9); // Issue a single stop grant cycle upon stpclk
Nico Huber68d7c7a2012-10-02 11:46:11 +0200128 msr.lo |= (1 << 8);
129 if (quad) {
130 msr.lo = (msr.lo & ~(7 << 0)) | (4 << 0);
131 }
132 if (c5) {
133 msr.lo &= ~(1 << 13);
134 msr.lo &= ~(7 << 0);
135 msr.lo |= (1 << 3); /* Enable dynamic L2. */
136 msr.lo |= (1 << 14); /* Enable deeper sleep */
137 }
138 /* Next two fields seem to be mutually exclusive: */
139 msr.lo &= ~(7 << 4);
140 msr.lo |= (1 << 10); /* Enable IO MWAIT redirection. */
141 if (c6)
142 msr.lo |= (1 << 25);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000143 wrmsr(PMG_CST_CONFIG_CONTROL, msr);
144
145 /* Set Processor MWAIT IO BASE */
146 msr.hi = 0;
147 msr.lo = ((PMB0_BASE + 4) & 0xffff) | (((PMB1_BASE + 9) & 0xffff) << 16);
148 wrmsr(PMG_IO_BASE_ADDR, msr);
149
150 /* Set IO Capture Address */
151 msr.hi = 0;
Nico Huber68d7c7a2012-10-02 11:46:11 +0200152 msr.lo = ((PMB0_BASE + 4) & 0xffff) | ((cst_range & 0xffff) << 16);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000153 wrmsr(PMG_IO_CAPTURE_ADDR, msr);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200154
155 if (c5) {
156 msr = rdmsr(MSR_BBL_CR_CTL3);
157 msr.lo &= ~(7 << 25);
158 msr.lo |= (2 << 25);
159 msr.lo &= ~(3 << 30);
160 msr.lo |= (1 << 30);
161 wrmsr(MSR_BBL_CR_CTL3, msr);
162 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000163}
164
Nico Huber68d7c7a2012-10-02 11:46:11 +0200165static void configure_p_states(const char stepping, const char cores)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000166{
167 msr_t msr;
168
Nico Huber68d7c7a2012-10-02 11:46:11 +0200169 /* Find pointer to CPU configuration. */
170 const device_t lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
171 struct cpu_intel_model_1067x_config *const conf =
172 (lapic && lapic->chip_info) ? lapic->chip_info : NULL;
173
174 msr = rdmsr(MSR_EXTENDED_CONFIG);
175 if (conf->slfm && (msr.lo & (1 << 27))) /* Super LFM supported? */
176 msr.lo |= (1 << 28); /* Enable Super LFM. */
177 wrmsr(MSR_EXTENDED_CONFIG, msr);
178
179 if (rdmsr(MSR_FSB_CLOCK_VCC).hi & (1 << (63 - 32))) {
180 /* Turbo supported? */
181 if ((stepping == 0xa) && (cores < 4)) {
182 msr = rdmsr(MSR_FSB_FREQ);
183 msr.lo |= (1 << 3); /* Enable hysteresis. */
184 wrmsr(MSR_FSB_FREQ, msr);
185 }
186 msr = rdmsr(IA32_PERF_CTL);
187 msr.hi &= ~(1 << (32 - 32)); /* Clear turbo disable. */
188 wrmsr(IA32_PERF_CTL, msr);
189 }
190
191 msr = rdmsr(PMG_CST_CONFIG_CONTROL);
192 msr.lo &= ~(1 << 11); /* Enable hw coordination. */
193 msr.lo |= (1 << 15); /* Lock config until next reset. */
194 wrmsr(PMG_CST_CONFIG_CONTROL, msr);
195}
196
197#define MSR_EMTTM_CR_TABLE(x) (0xa8 + (x))
198#define MSR_EMTTM_TABLE_NUM 6
199static void configure_emttm_tables(void)
200{
201 int i;
202 int num_states, pstate_idx;
203 msr_t msr;
204 sst_table_t pstates;
205
206 /* Gather p-state information. */
207 speedstep_gen_pstates(&pstates);
208
209 /* Never turbo mode or Super LFM. */
210 num_states = pstates.num_states;
211 if (pstates.states[0].is_turbo)
212 --num_states;
213 if (pstates.states[pstates.num_states - 1].is_slfm)
214 --num_states;
215 /* Repeat lowest p-state if we haven't enough states. */
216 const int num_lowest_pstate =
217 (num_states < MSR_EMTTM_TABLE_NUM)
218 ? (MSR_EMTTM_TABLE_NUM - num_states) + 1
219 : 1;
220 /* Start from the lowest entry but skip Super LFM. */
221 if (pstates.states[pstates.num_states - 1].is_slfm)
222 pstate_idx = pstates.num_states - 2;
223 else
224 pstate_idx = pstates.num_states - 1;
225 for (i = 0; i < MSR_EMTTM_TABLE_NUM; ++i) {
226 if (i >= num_lowest_pstate)
227 --pstate_idx;
228 const sst_state_t *const pstate = &pstates.states[pstate_idx];
229 printk(BIOS_DEBUG, "writing P-State %d: %d, %d, "
230 "%2d, 0x%02x, %d; encoded: 0x%04x\n",
231 pstate_idx, pstate->dynfsb, pstate->nonint,
232 pstate->ratio, pstate->vid, pstate->power,
233 SPEEDSTEP_ENCODE_STATE(*pstate));
234 msr.hi = 0;
235 msr.lo = SPEEDSTEP_ENCODE_STATE(pstates.states[pstate_idx]) &
236 /* Don't set half ratios. */
237 ~SPEEDSTEP_RATIO_NONINT;
238 wrmsr(MSR_EMTTM_CR_TABLE(i), msr);
239 }
240
241 msr = rdmsr(MSR_EMTTM_CR_TABLE(5));
242 msr.lo |= (1 << 31); /* lock tables */
243 wrmsr(MSR_EMTTM_CR_TABLE(5), msr);
244}
245
246static void configure_misc(const int eist, const int tm2, const int emttm)
247{
248 msr_t msr;
249
250 const u32 sub_cstates = cpuid_edx(5);
251
252 msr = rdmsr(IA32_MISC_ENABLES);
253 msr.lo |= (1 << 3); /* TM1 enable */
254 if (tm2)
255 msr.lo |= (1 << 13); /* TM2 enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000256 msr.lo |= (1 << 17); /* Bidirectional PROCHOT# */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200257 msr.lo |= (1 << 18); /* MONITOR/MWAIT enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000258
259 msr.lo |= (1 << 10); /* FERR# multiplexing */
260
Nico Huber68d7c7a2012-10-02 11:46:11 +0200261 if (eist)
262 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000263
264 /* Enable C2E */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200265 if (((sub_cstates >> (2 * 4)) & 0xf) >= 2) {
266 msr.lo |= (1 << 26);
267 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000268
269 /* Enable C4E */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200270 if (((sub_cstates >> (4 * 4)) & 0xf) >= 2) {
271 msr.hi |= (1 << (32 - 32)); // C4E
272 msr.hi |= (1 << (33 - 32)); // Hard C4E
273 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000274
Nico Huber68d7c7a2012-10-02 11:46:11 +0200275 /* Enable EMTTM */
276 if (emttm)
277 msr.hi |= (1 << (36 - 32));
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000278
Nico Huber68d7c7a2012-10-02 11:46:11 +0200279 /* Enable turbo mode */
280 if (rdmsr(MSR_FSB_CLOCK_VCC).hi & (1 << (63 - 32)))
281 msr.hi &= ~(1 << (38 - 32));
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000282
Nico Huber68d7c7a2012-10-02 11:46:11 +0200283 wrmsr(IA32_MISC_ENABLES, msr);
284
285 if (eist) {
286 msr.lo |= (1 << 20); /* Lock Enhanced SpeedStep Enable */
287 wrmsr(IA32_MISC_ENABLES, msr);
288 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000289}
290
291#define PIC_SENS_CFG 0x1aa
Nico Huber68d7c7a2012-10-02 11:46:11 +0200292static void configure_pic_thermal_sensors(const int tm2, const int quad)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000293{
294 msr_t msr;
295
296 msr = rdmsr(PIC_SENS_CFG);
297
Nico Huber68d7c7a2012-10-02 11:46:11 +0200298 if (quad)
299 msr.lo |= (1 << 31);
300 else
301 msr.lo &= ~(1 << 31);
302 if (tm2)
303 msr.lo |= (1 << 20); /* Enable TM1 if TM2 fails. */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000304 msr.lo |= (1 << 21); // inter-core lock TM1
Nico Huber68d7c7a2012-10-02 11:46:11 +0200305 msr.lo |= (1 << 4); // Enable bypass filter /* What does it do? */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000306
307 wrmsr(PIC_SENS_CFG, msr);
308}
309
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000310#if CONFIG_USBDEBUG
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000311static unsigned ehci_debug_addr;
312#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +0000313
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000314static void model_1067x_init(device_t cpu)
315{
316 char processor_name[49];
317
Nico Huber68d7c7a2012-10-02 11:46:11 +0200318
319 /* Gather some information: */
320
321 const struct cpuid_result cpuid1 = cpuid(1);
322
323 /* Read stepping. */
324 const char stepping = cpuid1.eax & 0xf;
325 /* Read number of cores. */
326 const char cores = (cpuid1.ebx >> 16) & 0xf;
327 /* Is this a quad core? */
328 const char quad = cores > 2;
329 /* Is this even a multiprocessor? */
330 const char mp = cores > 1;
331
332 /* Enable EMTTM on uni- and on multi-processors if it's not disabled. */
333 const char emttm = !mp || !(rdmsr(MSR_EXTENDED_CONFIG).lo & 4);
334
335 /* Is enhanced speedstep supported? */
336 const char eist = (cpuid1.ecx & (1 << 7)) &&
337 !(rdmsr(IA32_PLATFORM_ID).lo & (1 << 17));
338 /* Test for TM2 only if EIST is available. */
339 const char tm2 = eist && (cpuid1.ecx & (1 << 8));
340
341
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000342 /* Turn on caching if we haven't already */
343 x86_enable_cache();
344
345 /* Update the microcode */
346 intel_update_microcode(microcode_updates);
347
348 /* Print processor name */
349 fill_processor_name(processor_name);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000350 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000351
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000352#if CONFIG_USBDEBUG
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000353 // Is this caution really needed?
Stefan Reinauer14e22772010-04-27 06:56:47 +0000354 if(!ehci_debug_addr)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000355 ehci_debug_addr = get_ehci_debug();
356 set_ehci_debug(0);
357#endif
358
359 /* Setup MTRRs */
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100360 x86_setup_mtrrs();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000361 x86_mtrr_check();
362
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000363#if CONFIG_USBDEBUG
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000364 set_ehci_debug(ehci_debug_addr);
365#endif
366
367 /* Enable the local cpu apics */
368 setup_lapic();
369
370 /* Initialize the APIC timer */
371 init_timer();
372
373 /* Enable virtualization */
374 enable_vmx();
375
376 /* Configure C States */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200377 configure_c_states(quad);
378
379 /* Configure P States */
380 configure_p_states(stepping, cores);
381
382 /* EMTTM */
383 if (emttm)
384 configure_emttm_tables();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000385
386 /* Configure Enhanced SpeedStep and Thermal Sensors */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200387 configure_misc(eist, tm2, emttm);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000388
389 /* PIC thermal sensor control */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200390 configure_pic_thermal_sensors(tm2, quad);
Sven Schnelle51676b12012-07-29 19:18:03 +0200391
392 /* Start up my cpu siblings */
393 intel_sibling_init(cpu);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000394}
395
396static struct device_operations cpu_dev_ops = {
397 .init = model_1067x_init,
398};
399
400static struct cpu_device_id cpu_table[] = {
401 { X86_VENDOR_INTEL, 0x10676 }, /* Intel Core 2 Solo/Core Duo */
Stefan Reinauerc104cb02010-10-18 00:21:39 +0000402 { X86_VENDOR_INTEL, 0x10677 },
403 { X86_VENDOR_INTEL, 0x1067A },
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000404 { 0, 0 },
405};
406
407static const struct cpu_driver driver __cpu_driver = {
408 .ops = &cpu_dev_ops,
409 .id_table = cpu_table,
410};
411
Nico Huber68d7c7a2012-10-02 11:46:11 +0200412struct chip_operations cpu_intel_model_1067x_ops = {
413 CHIP_NAME("Intel Penryn CPU")
414};