Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / cpu / intel / model_6ex / model_6ex_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-1624-m206e839.h"
38         #include "microcode-1729-m206ec54.h"
39         #include "microcode-1869-m806ec59.h"
40         /*  Dummy terminator  */
41         0x0, 0x0, 0x0, 0x0,
42         0x0, 0x0, 0x0, 0x0,
43         0x0, 0x0, 0x0, 0x0,
44         0x0, 0x0, 0x0, 0x0,
45 };
46
47 static inline void strcpy(char *dst, char *src)
48 {
49         while (*src) *dst++ = *src++;
50 }
51
52 static void fill_processor_name(char *processor_name)
53 {
54         struct cpuid_result regs;
55         char temp_processor_name[49];
56         char *processor_name_start;
57         unsigned int *name_as_ints = (unsigned int *)temp_processor_name;
58         int i;
59
60         for (i=0; i<3; i++) {
61                 regs = cpuid(0x80000002 + i);
62                 name_as_ints[i*4 + 0] = regs.eax;
63                 name_as_ints[i*4 + 1] = regs.ebx;
64                 name_as_ints[i*4 + 2] = regs.ecx;
65                 name_as_ints[i*4 + 3] = regs.edx;
66         }
67
68         temp_processor_name[48] = 0;
69
70         /* Skip leading spaces */
71         processor_name_start = temp_processor_name;
72         while (*processor_name_start == ' ')
73                 processor_name_start++;
74
75         memset(processor_name, 0, 49);
76         strcpy(processor_name, processor_name_start);
77 }
78
79 #define IA32_FEATURE_CONTROL 0x003a
80
81 #define CPUID_VMX (1 << 5)
82 #define CPUID_SMX (1 << 6)
83 static void enable_vmx(void)
84 {
85         struct cpuid_result regs;
86         msr_t msr;
87
88         msr = rdmsr(IA32_FEATURE_CONTROL);
89
90         if (msr.lo & (1 << 0)) {
91                 /* VMX locked. If we set it again we get an illegal
92                  * instruction
93                  */
94                 return;
95         }
96
97         regs = cpuid(1);
98         if (regs.ecx & CPUID_VMX) {
99                 msr.lo |= (1 << 2);
100                 if (regs.ecx & CPUID_SMX)
101                         msr.lo |= (1 << 1);
102         }
103
104         wrmsr(IA32_FEATURE_CONTROL, msr);
105
106         msr.lo |= (1 << 0); /* Set lock bit */
107
108         wrmsr(IA32_FEATURE_CONTROL, msr);
109 }
110
111 #define PMG_CST_CONFIG_CONTROL  0xe2
112 #define PMG_IO_BASE_ADDR        0xe3
113 #define PMG_IO_CAPTURE_ADDR     0xe4
114
115 /* MWAIT coordination I/O base address. This must match
116  * the \_PR_.CPU0 PM base address.
117  */
118 #define PMB0_BASE 0x510
119
120 /* PMB1: I/O port that triggers SMI once cores are in the same state.
121  * See CSM Trigger, at PMG_CST_CONFIG_CONTROL[6:4]
122  */
123 #define PMB1_BASE 0x800
124 #define HIGHEST_CLEVEL          3
125 static void configure_c_states(void)
126 {
127         msr_t msr;
128
129         msr = rdmsr(PMG_CST_CONFIG_CONTROL);
130         msr.lo |= (1 << 15); // config lock until next reset.
131         msr.lo |= (1 << 10); // Enable I/O MWAIT redirection for C-States
132         msr.lo &= ~(1 << 9); // Issue a single stop grant cycle upon stpclk
133         // TODO Do we want Deep C4 and  Dynamic L2 shrinking?
134
135         /* Number of supported C-States */
136         msr.lo &= ~7;
137         msr.lo |= HIGHEST_CLEVEL; // support at most C3
138
139         wrmsr(PMG_CST_CONFIG_CONTROL, msr);
140
141         /* Set Processor MWAIT IO BASE (P_BLK) */
142         msr.hi = 0;
143         msr.lo = ((PMB0_BASE + 4) & 0xffff) | (((PMB1_BASE + 9) & 0xffff) << 16);
144         wrmsr(PMG_IO_BASE_ADDR, msr);
145
146         /* set C_LVL controls */
147         msr.hi = 0;
148         msr.lo = (PMB0_BASE + 4) | ((HIGHEST_CLEVEL - 2) << 16); // -2 because LVL0+1 aren't counted
149         wrmsr(PMG_IO_CAPTURE_ADDR, msr);
150 }
151
152 #define IA32_MISC_ENABLE        0x1a0
153 static void configure_misc(void)
154 {
155         msr_t msr;
156
157         msr = rdmsr(IA32_MISC_ENABLE);
158         msr.lo |= (1 << 3);     /* TM1 enable */
159         msr.lo |= (1 << 13);    /* TM2 enable */
160         msr.lo |= (1 << 17);    /* Bidirectional PROCHOT# */
161
162         msr.lo |= (1 << 10);    /* FERR# multiplexing */
163
164         // TODO: Only if  IA32_PLATFORM_ID[17] = 0 and IA32_PLATFORM_ID[50] = 1
165         msr.lo |= (1 << 16);    /* Enhanced SpeedStep Enable */
166
167         // TODO Do we want Deep C4 and  Dynamic L2 shrinking?
168         wrmsr(IA32_MISC_ENABLE, msr);
169
170         msr.lo |= (1 << 20);    /* Lock Enhanced SpeedStep Enable */
171         wrmsr(IA32_MISC_ENABLE, msr);
172 }
173
174 #define PIC_SENS_CFG    0x1aa
175 static void configure_pic_thermal_sensors(void)
176 {
177         msr_t msr;
178
179         msr = rdmsr(PIC_SENS_CFG);
180
181         msr.lo |= (1 << 21); // inter-core lock TM1
182         msr.lo |= (1 << 4); // Enable bypass filter
183
184         wrmsr(PIC_SENS_CFG, msr);
185 }
186
187 #if CONFIG_USBDEBUG_DIRECT
188 static unsigned ehci_debug_addr;
189 #endif
190
191 static void model_6ex_init(device_t cpu)
192 {
193         char processor_name[49];
194
195         /* Turn on caching if we haven't already */
196         x86_enable_cache();
197
198         /* Update the microcode */
199         intel_update_microcode(microcode_updates);
200
201         /* Print processor name */
202         fill_processor_name(processor_name);
203         printk(BIOS_INFO, "CPU: %s.\n", processor_name);
204
205 #if CONFIG_USBDEBUG_DIRECT
206         // Is this caution really needed?
207         if(!ehci_debug_addr)
208                 ehci_debug_addr = get_ehci_debug();
209         set_ehci_debug(0);
210 #endif
211
212         /* Setup MTRRs */
213         x86_setup_mtrrs(36);
214         x86_mtrr_check();
215
216 #if CONFIG_USBDEBUG_DIRECT
217         set_ehci_debug(ehci_debug_addr);
218 #endif
219
220         /* Enable the local cpu apics */
221         setup_lapic();
222
223         /* Enable virtualization */
224         enable_vmx();
225
226         /* Configure C States */
227         configure_c_states();
228
229         /* Configure Enhanced SpeedStep and Thermal Sensors */
230         configure_misc();
231
232         /* PIC thermal sensor control */
233         configure_pic_thermal_sensors();
234
235         /* Start up my cpu siblings */
236         intel_sibling_init(cpu);
237 }
238
239 static struct device_operations cpu_dev_ops = {
240         .init     = model_6ex_init,
241 };
242
243 static struct cpu_device_id cpu_table[] = {
244         { X86_VENDOR_INTEL, 0x06e0 }, /* Intel Core Solo/Core Duo */
245         { X86_VENDOR_INTEL, 0x06e8 }, /* Intel Core Solo/Core Duo */
246         { X86_VENDOR_INTEL, 0x06ec }, /* Intel Core Solo/Core Duo */
247         { 0, 0 },
248 };
249
250 static const struct cpu_driver driver __cpu_driver = {
251         .ops      = &cpu_dev_ops,
252         .id_table = cpu_table,
253 };
254