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