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