d716efbd483eea31d4838f9d85a2b96407c5b5a2
[coreboot.git] / src / cpu / intel / model_6fx / model_6fx_init.c
1 /*
2  * This file is part of the coreboot project.
3  * 
4  * Copyright (C) 2007-2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <string.h>
26 #include <cpu/cpu.h>
27 #include <cpu/x86/mtrr.h>
28 #include <cpu/x86/msr.h>
29 #include <cpu/x86/lapic.h>
30 #include <cpu/intel/microcode.h>
31 #include <cpu/intel/hyperthreading.h>
32 #include <cpu/x86/cache.h>
33 #include <cpu/x86/mtrr.h>
34 #include <usbdebug_direct.h>
35
36 static const uint32_t microcode_updates[] = {
37         #include "microcode-2127-m206f6c7.h"
38         #include "microcode-2129-m206f257.h"
39         #include "microcode-2334-m016fbB6.h"
40         #include "microcode-2335-m086fbB6.h"
41         #include "microcode-2336-m106fbB6.h"
42         #include "microcode-2337-m806fbB6.h"
43         #include "microcode-2339-m046fbB7.h"
44         #include "microcode-2340-m406fbB7.h"
45         #include "microcode-2346-m16fda3.h"
46         #include "microcode-2347-m206fda3.h"
47         #include "microcode-2348-m806fda3.h"
48         #include "microcode-2374-m16f6cb.h"
49         #include "microcode-2376-m46f6cd.h"
50         #include "microcode-2380-m106f768.h"
51         #include "microcode-2381-m406f769.h"
52         #include "microcode-2385-m806fa94.h"
53         #include "microcode-2389-m16f25a.h"
54         /*  Dummy terminator  */
55         0x0, 0x0, 0x0, 0x0,
56         0x0, 0x0, 0x0, 0x0,
57         0x0, 0x0, 0x0, 0x0,
58         0x0, 0x0, 0x0, 0x0,
59 };
60
61 static inline void strcpy(char *dst, char *src) 
62 {
63         while (*src) *dst++ = *src++;
64 }
65
66 static void fill_processor_name(char *processor_name)
67 {
68         struct cpuid_result regs;
69         char temp_processor_name[49];
70         char *processor_name_start;
71         unsigned int *name_as_ints = (unsigned int *)temp_processor_name;
72         int i;
73
74         for (i=0; i<3; i++) {
75                 regs = cpuid(0x80000002 + i);
76                 name_as_ints[i*4 + 0] = regs.eax;
77                 name_as_ints[i*4 + 1] = regs.ebx;
78                 name_as_ints[i*4 + 2] = regs.ecx;
79                 name_as_ints[i*4 + 3] = regs.edx;
80         }
81
82         temp_processor_name[48] = 0;
83
84         /* Skip leading spaces */
85         processor_name_start = temp_processor_name;
86         while (*processor_name_start == ' ') 
87                 processor_name_start++;
88
89         memset(processor_name, 0, 49);
90         strcpy(processor_name, processor_name_start);
91 }
92
93 #define IA32_FEATURE_CONTROL 0x003a
94
95 #define CPUID_VMX (1 << 5)
96 #define CPUID_SMX (1 << 6)
97 static void enable_vmx(void)
98 {
99         struct cpuid_result regs;
100         msr_t msr;
101
102         msr = rdmsr(IA32_FEATURE_CONTROL);
103
104         if (msr.lo & (1 << 0)) {
105                 /* VMX locked. If we set it again we get an illegal
106                  * instruction
107                  */
108                 return;
109         }
110
111         regs = cpuid(1);
112         if (regs.ecx & CPUID_VMX) {
113                 msr.lo |= (1 << 2);
114                 if (regs.ecx & CPUID_SMX)
115                         msr.lo |= (1 << 1);
116         }
117
118         wrmsr(IA32_FEATURE_CONTROL, msr);
119
120         msr.lo |= (1 << 0); /* Set lock bit */
121
122         wrmsr(IA32_FEATURE_CONTROL, msr);
123 }
124
125 #define PMG_CST_CONFIG_CONTROL  0xe2
126 #define PMG_IO_BASE_ADDR        0xe3
127 #define PMG_IO_CAPTURE_ADDR     0xe4
128
129 #define PMB0_BASE               0x580
130 #define PMB1_BASE               0x800
131 #define CST_RANGE               2
132 static void configure_c_states(void)
133 {
134         msr_t msr;
135
136         msr = rdmsr(PMG_CST_CONFIG_CONTROL);
137
138         msr.lo |= (1 << 15); // config lock until next reset
139         msr.lo |= (1 << 14); // Deeper Sleep
140         msr.lo |= (1 << 10); // Enable IO MWAIT redirection
141         msr.lo &= ~(1 << 9); // Issue a  single stop grant cycle upon stpclk
142         msr.lo |= (1 << 3); // Dynamic L2
143
144         wrmsr(PMG_CST_CONFIG_CONTROL, msr);
145
146         /* Set Processor MWAIT IO BASE */
147         msr.hi = 0;
148         msr.lo = ((PMB0_BASE + 4) & 0xffff) | (((PMB1_BASE + 9) & 0xffff) << 16);
149         wrmsr(PMG_IO_BASE_ADDR, msr);
150
151         /* Set IO Capture Address */
152         msr.hi = 0;
153         msr.lo = ((PMB0_BASE + 4) & 0xffff) | (( CST_RANGE & 0xffff) << 16);
154         wrmsr(PMG_IO_CAPTURE_ADDR, msr);
155 }
156
157 #define IA32_MISC_ENABLE        0x1a0
158 static void configure_misc(void)
159 {
160         msr_t msr;
161
162         msr = rdmsr(IA32_MISC_ENABLE);
163         msr.lo |= (1 << 3);     /* TM1 enable */
164         msr.lo |= (1 << 13);    /* TM2 enable */
165         msr.lo |= (1 << 17);    /* Bidirectional PROCHOT# */
166
167         msr.lo |= (1 << 10);    /* FERR# multiplexing */
168
169         // TODO: Only if  IA32_PLATFORM_ID[17] = 0 and IA32_PLATFORM_ID[50] = 1
170         msr.lo |= (1 << 16);    /* Enhanced SpeedStep Enable */
171
172         /* Enable C2E */
173         msr.lo |= (1 << 26);
174
175         /* Enable C4E */
176         /* TODO This should only be done on mobile CPUs, see cpuid 5 */
177         msr.hi |= (1 << (32 - 32)); // C4E
178         msr.hi |= (1 << (33 - 32)); // Hard C4E
179
180         /* Enable EMTTM. */
181         /* NOTE: We leave the EMTTM_CR_TABLE0-5 at their default values */
182         msr.hi |= (1 << (36 - 32));
183
184         wrmsr(IA32_MISC_ENABLE, msr);
185
186         msr.lo |= (1 << 20);    /* Lock Enhanced SpeedStep Enable */
187         wrmsr(IA32_MISC_ENABLE, msr);
188 }
189
190 #define PIC_SENS_CFG    0x1aa
191 static void configure_pic_thermal_sensors(void)
192 {
193         msr_t msr;
194
195         msr = rdmsr(PIC_SENS_CFG);
196
197         msr.lo |= (1 << 21); // inter-core lock TM1
198         msr.lo |= (1 << 4); // Enable bypass filter
199
200         wrmsr(PIC_SENS_CFG, msr);
201 }
202
203 #if CONFIG_USBDEBUG_DIRECT
204 static unsigned ehci_debug_addr;
205 #endif
206                 
207 static void model_6fx_init(device_t cpu)
208 {
209         char processor_name[49];
210
211         /* Turn on caching if we haven't already */
212         x86_enable_cache();
213
214         /* Update the microcode */
215         intel_update_microcode(microcode_updates);
216
217         /* Print processor name */
218         fill_processor_name(processor_name);
219         printk_info("CPU: %s.\n", processor_name);
220
221 #if CONFIG_USBDEBUG_DIRECT
222         // Is this caution really needed?
223         if(!ehci_debug_addr) 
224                 ehci_debug_addr = get_ehci_debug();
225         set_ehci_debug(0);
226 #endif
227
228         /* Setup MTRRs */
229         x86_setup_mtrrs(36);
230         x86_mtrr_check();
231
232 #if CONFIG_USBDEBUG_DIRECT
233         set_ehci_debug(ehci_debug_addr);
234 #endif
235
236         /* Enable the local cpu apics */
237         setup_lapic();
238
239         /* Enable virtualization */
240         enable_vmx();
241
242         /* Configure C States */
243         configure_c_states();
244
245         /* Configure Enhanced SpeedStep and Thermal Sensors */
246         configure_misc();
247
248         /* PIC thermal sensor control */
249         configure_pic_thermal_sensors();
250
251         /* Start up my cpu siblings */
252         intel_sibling_init(cpu);
253 }
254
255 static struct device_operations cpu_dev_ops = {
256         .init     = model_6fx_init,
257 };
258
259 static struct cpu_device_id cpu_table[] = {
260         { X86_VENDOR_INTEL, 0x06f0 }, /* Intel Core 2 Solo/Core Duo */
261         { X86_VENDOR_INTEL, 0x06f2 }, /* Intel Core 2 Solo/Core Duo */
262         { X86_VENDOR_INTEL, 0x06f6 }, /* Intel Core 2 Solo/Core Duo */
263         { X86_VENDOR_INTEL, 0x06f7 }, /* Intel Core 2 Solo/Core Duo */
264         { X86_VENDOR_INTEL, 0x06fa }, /* Intel Core 2 Solo/Core Duo */
265         { X86_VENDOR_INTEL, 0x06fb }, /* Intel Core 2 Solo/Core Duo */
266         { X86_VENDOR_INTEL, 0x06fd }, /* Intel Core 2 Solo/Core Duo */
267         { 0, 0 },
268 };
269
270 static const struct cpu_driver driver __cpu_driver = {
271         .ops      = &cpu_dev_ops,
272         .id_table = cpu_table,
273 };
274