blob: 9b7536de5785823aa42e54574e7308a6a1bbbb4b [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.
Thomas Jourdan1a692d82009-07-01 17:01:17 +000016 */
17
18#include <console/console.h>
19#include <device/device.h>
Thomas Jourdan1a692d82009-07-01 17:01:17 +000020#include <string.h>
21#include <cpu/cpu.h>
22#include <cpu/x86/mtrr.h>
23#include <cpu/x86/msr.h>
24#include <cpu/x86/lapic.h>
25#include <cpu/intel/microcode.h>
Stefan Reinauer2a27b202010-12-11 22:14:44 +000026#include <cpu/intel/speedstep.h>
Sven Schnelle51676b12012-07-29 19:18:03 +020027#include <cpu/intel/hyperthreading.h>
Thomas Jourdan1a692d82009-07-01 17:01:17 +000028#include <cpu/x86/cache.h>
Uwe Hermannaac8f662010-09-29 09:54:16 +000029#include <cpu/x86/name.h>
Matt DeVilliered6fe2f2016-12-14 16:12:43 -060030#include <cpu/intel/common/common.h>
Nico Huber68d7c7a2012-10-02 11:46:11 +020031#include "chip.h"
32
Thomas Jourdan1a692d82009-07-01 17:01:17 +000033static void init_timer(void)
34{
Elyes HAOUASd6e96862016-08-21 10:12:15 +020035 /* Set the APIC timer to no interrupts and periodic mode */
Lee Leahy9d62e7e2017-03-15 17:40:50 -070036 lapic_write(LAPIC_LVTT, (1 << 17) | (1 << 16) | (0 << 12) | (0 << 0));
Thomas Jourdan1a692d82009-07-01 17:01:17 +000037
38 /* Set the divider to 1, no divider */
39 lapic_write(LAPIC_TDCR, LAPIC_TDR_DIV_1);
40
41 /* Set the initial counter to 0xffffffff */
42 lapic_write(LAPIC_TMICT, 0xffffffff);
43}
44
Nico Huber68d7c7a2012-10-02 11:46:11 +020045#define MSR_BBL_CR_CTL3 0x11e
Thomas Jourdan1a692d82009-07-01 17:01:17 +000046
Nico Huber68d7c7a2012-10-02 11:46:11 +020047static void configure_c_states(const int quad)
Thomas Jourdan1a692d82009-07-01 17:01:17 +000048{
49 msr_t msr;
50
Nico Huber68d7c7a2012-10-02 11:46:11 +020051 /* Find pointer to CPU configuration. */
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110052 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
Nico Huber68d7c7a2012-10-02 11:46:11 +020053 const struct cpu_intel_model_1067x_config *const conf =
54 (lapic && lapic->chip_info) ? lapic->chip_info : NULL;
55
56 /* Is C5 requested and supported? */
57 const int c5 = conf && conf->c5 &&
58 (rdmsr(MSR_BBL_CR_CTL3).lo & (3 << 30)) &&
59 !(rdmsr(MSR_FSB_FREQ).lo & (1 << 31));
60 /* Is C6 requested and supported? */
61 const int c6 = conf && conf->c6 &&
62 ((cpuid_edx(5) >> (6 * 4)) & 0xf) && c5;
63
64 const int cst_range = (c6 ? 6 : (c5 ? 5 : 4)) - 2; /* zero means lvl2 */
65
Patrick Georgi644e83b2013-02-09 15:35:30 +010066 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Thomas Jourdan1a692d82009-07-01 17:01:17 +000067 msr.lo &= ~(1 << 9); // Issue a single stop grant cycle upon stpclk
Nico Huber68d7c7a2012-10-02 11:46:11 +020068 msr.lo |= (1 << 8);
Lee Leahy26eeb0f2017-03-15 18:08:50 -070069 if (quad)
Nico Huber68d7c7a2012-10-02 11:46:11 +020070 msr.lo = (msr.lo & ~(7 << 0)) | (4 << 0);
Nico Huber68d7c7a2012-10-02 11:46:11 +020071 if (c5) {
72 msr.lo &= ~(1 << 13);
73 msr.lo &= ~(7 << 0);
74 msr.lo |= (1 << 3); /* Enable dynamic L2. */
75 msr.lo |= (1 << 14); /* Enable deeper sleep */
76 }
77 /* Next two fields seem to be mutually exclusive: */
78 msr.lo &= ~(7 << 4);
79 msr.lo |= (1 << 10); /* Enable IO MWAIT redirection. */
80 if (c6)
81 msr.lo |= (1 << 25);
Patrick Georgi644e83b2013-02-09 15:35:30 +010082 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
Thomas Jourdan1a692d82009-07-01 17:01:17 +000083
84 /* Set Processor MWAIT IO BASE */
85 msr.hi = 0;
86 msr.lo = ((PMB0_BASE + 4) & 0xffff) | (((PMB1_BASE + 9) & 0xffff) << 16);
Patrick Georgi644e83b2013-02-09 15:35:30 +010087 wrmsr(MSR_PMG_IO_BASE_ADDR, msr);
Thomas Jourdan1a692d82009-07-01 17:01:17 +000088
89 /* Set IO Capture Address */
90 msr.hi = 0;
Nico Huber68d7c7a2012-10-02 11:46:11 +020091 msr.lo = ((PMB0_BASE + 4) & 0xffff) | ((cst_range & 0xffff) << 16);
Patrick Georgi644e83b2013-02-09 15:35:30 +010092 wrmsr(MSR_PMG_IO_CAPTURE_ADDR, msr);
Nico Huber68d7c7a2012-10-02 11:46:11 +020093
94 if (c5) {
95 msr = rdmsr(MSR_BBL_CR_CTL3);
96 msr.lo &= ~(7 << 25);
97 msr.lo |= (2 << 25);
98 msr.lo &= ~(3 << 30);
99 msr.lo |= (1 << 30);
100 wrmsr(MSR_BBL_CR_CTL3, msr);
101 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000102}
103
Nico Huber68d7c7a2012-10-02 11:46:11 +0200104static void configure_p_states(const char stepping, const char cores)
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. */
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100109 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200110 struct cpu_intel_model_1067x_config *const conf =
111 (lapic && lapic->chip_info) ? lapic->chip_info : NULL;
112
113 msr = rdmsr(MSR_EXTENDED_CONFIG);
Patrick Georgif17c58b2014-08-09 20:48:12 +0200114 /* Super LFM supported? */
115 if (conf && conf->slfm && (msr.lo & (1 << 27)))
Nico Huber68d7c7a2012-10-02 11:46:11 +0200116 msr.lo |= (1 << 28); /* Enable Super LFM. */
117 wrmsr(MSR_EXTENDED_CONFIG, msr);
118
119 if (rdmsr(MSR_FSB_CLOCK_VCC).hi & (1 << (63 - 32))) {
120 /* Turbo supported? */
121 if ((stepping == 0xa) && (cores < 4)) {
122 msr = rdmsr(MSR_FSB_FREQ);
123 msr.lo |= (1 << 3); /* Enable hysteresis. */
124 wrmsr(MSR_FSB_FREQ, msr);
125 }
126 msr = rdmsr(IA32_PERF_CTL);
127 msr.hi &= ~(1 << (32 - 32)); /* Clear turbo disable. */
128 wrmsr(IA32_PERF_CTL, msr);
129 }
130
Patrick Georgi644e83b2013-02-09 15:35:30 +0100131 msr = rdmsr(MSR_PMG_CST_CONFIG_CONTROL);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200132 msr.lo &= ~(1 << 11); /* Enable hw coordination. */
133 msr.lo |= (1 << 15); /* Lock config until next reset. */
Patrick Georgi644e83b2013-02-09 15:35:30 +0100134 wrmsr(MSR_PMG_CST_CONFIG_CONTROL, msr);
Nico Huber68d7c7a2012-10-02 11:46:11 +0200135}
136
137#define MSR_EMTTM_CR_TABLE(x) (0xa8 + (x))
138#define MSR_EMTTM_TABLE_NUM 6
139static void configure_emttm_tables(void)
140{
141 int i;
142 int num_states, pstate_idx;
143 msr_t msr;
144 sst_table_t pstates;
145
146 /* Gather p-state information. */
147 speedstep_gen_pstates(&pstates);
148
149 /* Never turbo mode or Super LFM. */
150 num_states = pstates.num_states;
151 if (pstates.states[0].is_turbo)
152 --num_states;
153 if (pstates.states[pstates.num_states - 1].is_slfm)
154 --num_states;
155 /* Repeat lowest p-state if we haven't enough states. */
156 const int num_lowest_pstate =
157 (num_states < MSR_EMTTM_TABLE_NUM)
158 ? (MSR_EMTTM_TABLE_NUM - num_states) + 1
159 : 1;
160 /* Start from the lowest entry but skip Super LFM. */
161 if (pstates.states[pstates.num_states - 1].is_slfm)
162 pstate_idx = pstates.num_states - 2;
163 else
164 pstate_idx = pstates.num_states - 1;
165 for (i = 0; i < MSR_EMTTM_TABLE_NUM; ++i) {
166 if (i >= num_lowest_pstate)
167 --pstate_idx;
168 const sst_state_t *const pstate = &pstates.states[pstate_idx];
169 printk(BIOS_DEBUG, "writing P-State %d: %d, %d, "
170 "%2d, 0x%02x, %d; encoded: 0x%04x\n",
171 pstate_idx, pstate->dynfsb, pstate->nonint,
172 pstate->ratio, pstate->vid, pstate->power,
173 SPEEDSTEP_ENCODE_STATE(*pstate));
174 msr.hi = 0;
175 msr.lo = SPEEDSTEP_ENCODE_STATE(pstates.states[pstate_idx]) &
176 /* Don't set half ratios. */
177 ~SPEEDSTEP_RATIO_NONINT;
178 wrmsr(MSR_EMTTM_CR_TABLE(i), msr);
179 }
180
181 msr = rdmsr(MSR_EMTTM_CR_TABLE(5));
182 msr.lo |= (1 << 31); /* lock tables */
183 wrmsr(MSR_EMTTM_CR_TABLE(5), msr);
184}
185
186static void configure_misc(const int eist, const int tm2, const int emttm)
187{
188 msr_t msr;
189
190 const u32 sub_cstates = cpuid_edx(5);
191
192 msr = rdmsr(IA32_MISC_ENABLES);
193 msr.lo |= (1 << 3); /* TM1 enable */
194 if (tm2)
195 msr.lo |= (1 << 13); /* TM2 enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000196 msr.lo |= (1 << 17); /* Bidirectional PROCHOT# */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200197 msr.lo |= (1 << 18); /* MONITOR/MWAIT enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000198
199 msr.lo |= (1 << 10); /* FERR# multiplexing */
200
Nico Huber68d7c7a2012-10-02 11:46:11 +0200201 if (eist)
202 msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000203
204 /* Enable C2E */
Lee Leahy26eeb0f2017-03-15 18:08:50 -0700205 if (((sub_cstates >> (2 * 4)) & 0xf) >= 2)
Nico Huber68d7c7a2012-10-02 11:46:11 +0200206 msr.lo |= (1 << 26);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000207
208 /* Enable C4E */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200209 if (((sub_cstates >> (4 * 4)) & 0xf) >= 2) {
210 msr.hi |= (1 << (32 - 32)); // C4E
211 msr.hi |= (1 << (33 - 32)); // Hard C4E
212 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000213
Nico Huber68d7c7a2012-10-02 11:46:11 +0200214 /* Enable EMTTM */
215 if (emttm)
216 msr.hi |= (1 << (36 - 32));
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000217
Nico Huber68d7c7a2012-10-02 11:46:11 +0200218 /* Enable turbo mode */
219 if (rdmsr(MSR_FSB_CLOCK_VCC).hi & (1 << (63 - 32)))
220 msr.hi &= ~(1 << (38 - 32));
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000221
Nico Huber68d7c7a2012-10-02 11:46:11 +0200222 wrmsr(IA32_MISC_ENABLES, msr);
223
224 if (eist) {
225 msr.lo |= (1 << 20); /* Lock Enhanced SpeedStep Enable */
226 wrmsr(IA32_MISC_ENABLES, msr);
227 }
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000228}
229
230#define PIC_SENS_CFG 0x1aa
Nico Huber68d7c7a2012-10-02 11:46:11 +0200231static void configure_pic_thermal_sensors(const int tm2, const int quad)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000232{
233 msr_t msr;
234
235 msr = rdmsr(PIC_SENS_CFG);
236
Nico Huber68d7c7a2012-10-02 11:46:11 +0200237 if (quad)
238 msr.lo |= (1 << 31);
239 else
240 msr.lo &= ~(1 << 31);
241 if (tm2)
242 msr.lo |= (1 << 20); /* Enable TM1 if TM2 fails. */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000243 msr.lo |= (1 << 21); // inter-core lock TM1
Nico Huber68d7c7a2012-10-02 11:46:11 +0200244 msr.lo |= (1 << 4); // Enable bypass filter /* What does it do? */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000245
246 wrmsr(PIC_SENS_CFG, msr);
247}
248
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100249static void model_1067x_init(struct device *cpu)
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000250{
251 char processor_name[49];
252
Nico Huber68d7c7a2012-10-02 11:46:11 +0200253
254 /* Gather some information: */
255
256 const struct cpuid_result cpuid1 = cpuid(1);
257
258 /* Read stepping. */
259 const char stepping = cpuid1.eax & 0xf;
260 /* Read number of cores. */
261 const char cores = (cpuid1.ebx >> 16) & 0xf;
262 /* Is this a quad core? */
263 const char quad = cores > 2;
264 /* Is this even a multiprocessor? */
265 const char mp = cores > 1;
266
267 /* Enable EMTTM on uni- and on multi-processors if it's not disabled. */
268 const char emttm = !mp || !(rdmsr(MSR_EXTENDED_CONFIG).lo & 4);
269
270 /* Is enhanced speedstep supported? */
271 const char eist = (cpuid1.ecx & (1 << 7)) &&
272 !(rdmsr(IA32_PLATFORM_ID).lo & (1 << 17));
273 /* Test for TM2 only if EIST is available. */
274 const char tm2 = eist && (cpuid1.ecx & (1 << 8));
275
276
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000277 /* Turn on caching if we haven't already */
278 x86_enable_cache();
279
280 /* Update the microcode */
Alexandru Gagniuc2c38f502013-12-06 23:14:54 -0600281 intel_update_microcode_from_cbfs();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000282
283 /* Print processor name */
284 fill_processor_name(processor_name);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000285 printk(BIOS_INFO, "CPU: %s.\n", processor_name);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000286
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000287 /* Setup MTRRs */
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100288 x86_setup_mtrrs();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000289 x86_mtrr_check();
290
Elyes HAOUASd6e96862016-08-21 10:12:15 +0200291 /* Enable the local CPU APICs */
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000292 setup_lapic();
293
294 /* Initialize the APIC timer */
295 init_timer();
296
Matt DeVilliered6fe2f2016-12-14 16:12:43 -0600297 /* Set virtualization based on Kconfig option */
298 set_vmx();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000299
300 /* Configure C States */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200301 configure_c_states(quad);
302
303 /* Configure P States */
304 configure_p_states(stepping, cores);
305
306 /* EMTTM */
307 if (emttm)
308 configure_emttm_tables();
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000309
310 /* Configure Enhanced SpeedStep and Thermal Sensors */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200311 configure_misc(eist, tm2, emttm);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000312
313 /* PIC thermal sensor control */
Nico Huber68d7c7a2012-10-02 11:46:11 +0200314 configure_pic_thermal_sensors(tm2, quad);
Sven Schnelle51676b12012-07-29 19:18:03 +0200315
Elyes HAOUASd82be922016-07-28 18:58:27 +0200316 /* Start up my CPU siblings */
Sven Schnelle51676b12012-07-29 19:18:03 +0200317 intel_sibling_init(cpu);
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000318}
319
320static struct device_operations cpu_dev_ops = {
321 .init = model_1067x_init,
322};
323
324static struct cpu_device_id cpu_table[] = {
325 { X86_VENDOR_INTEL, 0x10676 }, /* Intel Core 2 Solo/Core Duo */
Stefan Reinauerc104cb02010-10-18 00:21:39 +0000326 { X86_VENDOR_INTEL, 0x10677 },
327 { X86_VENDOR_INTEL, 0x1067A },
Thomas Jourdan1a692d82009-07-01 17:01:17 +0000328 { 0, 0 },
329};
330
331static const struct cpu_driver driver __cpu_driver = {
332 .ops = &cpu_dev_ops,
333 .id_table = cpu_table,
334};
335
Nico Huber68d7c7a2012-10-02 11:46:11 +0200336struct chip_operations cpu_intel_model_1067x_ops = {
337 CHIP_NAME("Intel Penryn CPU")
338};