Remove some duplicate #include files (trivial).
[coreboot.git] / src / cpu / intel / model_1067x / model_1067x_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/name.h>
34
35 static const uint32_t microcode_updates[] = {
36         /*  Dummy terminator  */
37         0x0, 0x0, 0x0, 0x0,
38         0x0, 0x0, 0x0, 0x0,
39         0x0, 0x0, 0x0, 0x0,
40         0x0, 0x0, 0x0, 0x0,
41 };
42
43 static void init_timer(void)
44 {
45         /* Set the apic timer to no interrupts and periodic mode */
46         lapic_write(LAPIC_LVTT, (1 << 17)|(1<< 16)|(0 << 12)|(0 << 0));
47
48         /* Set the divider to 1, no divider */
49         lapic_write(LAPIC_TDCR, LAPIC_TDR_DIV_1);
50
51         /* Set the initial counter to 0xffffffff */
52         lapic_write(LAPIC_TMICT, 0xffffffff);
53 }
54
55 #define IA32_FEATURE_CONTROL 0x003a
56
57 #define CPUID_VMX (1 << 5)
58 #define CPUID_SMX (1 << 6)
59 static void enable_vmx(void)
60 {
61         struct cpuid_result regs;
62         msr_t msr;
63
64         msr = rdmsr(IA32_FEATURE_CONTROL);
65
66         if (msr.lo & (1 << 0)) {
67                 /* VMX locked. If we set it again we get an illegal
68                  * instruction
69                  */
70                 return;
71         }
72
73         regs = cpuid(1);
74         if (regs.ecx & CPUID_VMX) {
75                 msr.lo |= (1 << 2);
76                 if (regs.ecx & CPUID_SMX)
77                         msr.lo |= (1 << 1);
78         }
79
80         wrmsr(IA32_FEATURE_CONTROL, msr);
81
82         msr.lo |= (1 << 0); /* Set lock bit */
83
84         wrmsr(IA32_FEATURE_CONTROL, msr);
85 }
86
87 #define PMG_CST_CONFIG_CONTROL  0xe2
88 #define PMG_IO_BASE_ADDR        0xe3
89 #define PMG_IO_CAPTURE_ADDR     0xe4
90
91 #define PMB0_BASE               0x580
92 #define PMB1_BASE               0x800
93 #define CST_RANGE               2
94 static void configure_c_states(void)
95 {
96         msr_t msr;
97
98         msr = rdmsr(PMG_CST_CONFIG_CONTROL);
99
100         msr.lo |= (1 << 15); // config lock until next reset
101         msr.lo |= (1 << 14); // Deeper Sleep
102         msr.lo |= (1 << 10); // Enable IO MWAIT redirection
103         msr.lo &= ~(1 << 9); // Issue a  single stop grant cycle upon stpclk
104         msr.lo |= (1 << 3); // Dynamic L2
105
106         wrmsr(PMG_CST_CONFIG_CONTROL, msr);
107
108         /* Set Processor MWAIT IO BASE */
109         msr.hi = 0;
110         msr.lo = ((PMB0_BASE + 4) & 0xffff) | (((PMB1_BASE + 9) & 0xffff) << 16);
111         wrmsr(PMG_IO_BASE_ADDR, msr);
112
113         /* Set IO Capture Address */
114         msr.hi = 0;
115         msr.lo = ((PMB0_BASE + 4) & 0xffff) | (( CST_RANGE & 0xffff) << 16);
116         wrmsr(PMG_IO_CAPTURE_ADDR, msr);
117 }
118
119 #define IA32_MISC_ENABLE        0x1a0
120 static void configure_misc(void)
121 {
122         msr_t msr;
123
124         msr = rdmsr(IA32_MISC_ENABLE);
125         msr.lo |= (1 << 3);     /* TM1 enable */
126         msr.lo |= (1 << 13);    /* TM2 enable */
127         msr.lo |= (1 << 17);    /* Bidirectional PROCHOT# */
128
129         msr.lo |= (1 << 10);    /* FERR# multiplexing */
130
131         // TODO: Only if  IA32_PLATFORM_ID[17] = 0 and IA32_PLATFORM_ID[50] = 1
132         msr.lo |= (1 << 16);    /* Enhanced SpeedStep Enable */
133
134         /* Enable C2E */
135         msr.lo |= (1 << 26);
136
137         /* Enable C4E */
138         /* TODO This should only be done on mobile CPUs, see cpuid 5 */
139         msr.hi |= (1 << (32 - 32)); // C4E
140         msr.hi |= (1 << (33 - 32)); // Hard C4E
141
142         /* Enable EMTTM. */
143         /* NOTE: We leave the EMTTM_CR_TABLE0-5 at their default values */
144         msr.hi |= (1 << (36 - 32));
145
146         wrmsr(IA32_MISC_ENABLE, msr);
147
148         msr.lo |= (1 << 20);    /* Lock Enhanced SpeedStep Enable */
149         wrmsr(IA32_MISC_ENABLE, msr);
150 }
151
152 #define PIC_SENS_CFG    0x1aa
153 static void configure_pic_thermal_sensors(void)
154 {
155         msr_t msr;
156
157         msr = rdmsr(PIC_SENS_CFG);
158
159         msr.lo |= (1 << 21); // inter-core lock TM1
160         msr.lo |= (1 << 4); // Enable bypass filter
161
162         wrmsr(PIC_SENS_CFG, msr);
163 }
164
165 #if CONFIG_USBDEBUG
166 static unsigned ehci_debug_addr;
167 #endif
168
169 static void model_1067x_init(device_t cpu)
170 {
171         char processor_name[49];
172
173         /* Turn on caching if we haven't already */
174         x86_enable_cache();
175
176         /* Update the microcode */
177         intel_update_microcode(microcode_updates);
178
179         /* Print processor name */
180         fill_processor_name(processor_name);
181         printk(BIOS_INFO, "CPU: %s.\n", processor_name);
182
183 #if CONFIG_USBDEBUG
184         // Is this caution really needed?
185         if(!ehci_debug_addr)
186                 ehci_debug_addr = get_ehci_debug();
187         set_ehci_debug(0);
188 #endif
189
190         /* Setup MTRRs */
191         x86_setup_mtrrs(36);
192         x86_mtrr_check();
193
194 #if CONFIG_USBDEBUG
195         set_ehci_debug(ehci_debug_addr);
196 #endif
197
198         /* Enable the local cpu apics */
199         setup_lapic();
200
201         /* Initialize the APIC timer */
202         init_timer();
203
204         /* Enable virtualization */
205         enable_vmx();
206
207         /* Configure C States */
208         configure_c_states();
209
210         /* Configure Enhanced SpeedStep and Thermal Sensors */
211         configure_misc();
212
213         /* PIC thermal sensor control */
214         configure_pic_thermal_sensors();
215
216         /* Start up my cpu siblings */
217         intel_sibling_init(cpu);
218 }
219
220 static struct device_operations cpu_dev_ops = {
221         .init     = model_1067x_init,
222 };
223
224 static struct cpu_device_id cpu_table[] = {
225         { X86_VENDOR_INTEL, 0x10676 }, /* Intel Core 2 Solo/Core Duo */
226         { 0, 0 },
227 };
228
229 static const struct cpu_driver driver __cpu_driver = {
230         .ops      = &cpu_dev_ops,
231         .id_table = cpu_table,
232 };
233