Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / cpu / intel / model_106cx / model_106cx_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 the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <string.h>
24 #include <cpu/cpu.h>
25 #include <cpu/x86/mtrr.h>
26 #include <cpu/x86/msr.h>
27 #include <cpu/x86/lapic.h>
28 #include <cpu/intel/microcode.h>
29 #include <cpu/intel/hyperthreading.h>
30 #include <cpu/x86/cache.h>
31 #include <cpu/x86/mtrr.h>
32 #include <usbdebug_direct.h>
33
34 static const uint32_t microcode_updates[] = {
35         /*  Dummy terminator  */
36         0x0, 0x0, 0x0, 0x0,
37         0x0, 0x0, 0x0, 0x0,
38         0x0, 0x0, 0x0, 0x0,
39         0x0, 0x0, 0x0, 0x0,
40 };
41
42 static inline void strcpy(char *dst, char *src)
43 {
44         while (*src) *dst++ = *src++;
45 }
46
47 static void fill_processor_name(char *processor_name)
48 {
49         struct cpuid_result regs;
50         char temp_processor_name[49];
51         char *processor_name_start;
52         unsigned int *name_as_ints = (unsigned int *)temp_processor_name;
53         int i;
54
55         for (i=0; i<3; i++) {
56                 regs = cpuid(0x80000002 + i);
57                 name_as_ints[i*4 + 0] = regs.eax;
58                 name_as_ints[i*4 + 1] = regs.ebx;
59                 name_as_ints[i*4 + 2] = regs.ecx;
60                 name_as_ints[i*4 + 3] = regs.edx;
61         }
62
63         temp_processor_name[48] = 0;
64
65         /* Skip leading spaces */
66         processor_name_start = temp_processor_name;
67         while (*processor_name_start == ' ')
68                 processor_name_start++;
69
70         memset(processor_name, 0, 49);
71         strcpy(processor_name, processor_name_start);
72 }
73
74 #define IA32_FEATURE_CONTROL 0x003a
75
76 #define CPUID_VMX (1 << 5)
77 #define CPUID_SMX (1 << 6)
78 static void enable_vmx(void)
79 {
80         struct cpuid_result regs;
81         msr_t msr;
82
83         msr = rdmsr(IA32_FEATURE_CONTROL);
84
85         if (msr.lo & (1 << 0)) {
86                 /* VMX locked. If we set it again we get an illegal
87                  * instruction
88                  */
89                 return;
90         }
91
92         regs = cpuid(1);
93         if (regs.ecx & CPUID_VMX) {
94                 msr.lo |= (1 << 2);
95                 if (regs.ecx & CPUID_SMX)
96                         msr.lo |= (1 << 1);
97         }
98
99         wrmsr(IA32_FEATURE_CONTROL, msr);
100
101         msr.lo |= (1 << 0); /* Set lock bit */
102
103         wrmsr(IA32_FEATURE_CONTROL, msr);
104 }
105
106 #define PMG_CST_CONFIG_CONTROL  0xe2
107 #define PMG_IO_BASE_ADDR        0xe3
108 #define PMG_IO_CAPTURE_ADDR     0xe4
109 #define PMB0 0x510 /* analogous to P_BLK in cpu.asl */
110 #define PMB1 0x0        /* IO port that triggers SMI once cores are in the same state.
111                         See CSM Trigger, at PMG_CST_CONFIG_CONTROL[6:4] */
112 #define HIGHEST_CLEVEL          3
113 static void configure_c_states(void)
114 {
115         msr_t msr;
116
117         msr = rdmsr(PMG_CST_CONFIG_CONTROL);
118         msr.lo |= (1 << 15); // Lock configuration
119         msr.lo |= (1 << 10); // redirect IO-based CState transition requests to MWAIT
120         msr.lo &= ~(1 << 9); // Issue a single stop grant cycle upon stpclk
121         msr.lo &= ~7; msr.lo |= HIGHEST_CLEVEL; // support at most C3
122         // TODO Do we want Deep C4 and  Dynamic L2 shrinking?
123         wrmsr(PMG_CST_CONFIG_CONTROL, msr);
124
125         // set P_BLK address
126         msr = rdmsr(PMG_IO_BASE_ADDR);
127         msr.lo = (PMB0 + 4) | (PMB1 << 16);
128         wrmsr(PMG_IO_BASE_ADDR, msr);
129
130         // set C_LVL controls
131         msr = rdmsr(PMG_IO_CAPTURE_ADDR);
132         msr.lo = (PMB0 + 4) | ((HIGHEST_CLEVEL - 2) << 16); // -2 because LVL0+1 aren't counted
133         wrmsr(PMG_IO_CAPTURE_ADDR, msr);
134 }
135
136 #define IA32_MISC_ENABLE        0x1a0
137 static void configure_misc(void)
138 {
139         msr_t msr;
140
141         msr = rdmsr(IA32_MISC_ENABLE);
142         msr.lo |= (1 << 3);     /* TM1 enable */
143         msr.lo |= (1 << 13);    /* TM2 enable */
144         msr.lo |= (1 << 17);    /* Bidirectional PROCHOT# */
145
146         msr.lo |= (1 << 10);    /* FERR# multiplexing */
147
148         // TODO: Only if  IA32_PLATFORM_ID[17] = 0 and IA32_PLATFORM_ID[50] = 1
149         msr.lo |= (1 << 16);    /* Enhanced SpeedStep Enable */
150
151         // TODO Do we want Deep C4 and  Dynamic L2 shrinking?
152         wrmsr(IA32_MISC_ENABLE, msr);
153
154         msr.lo |= (1 << 20);    /* Lock Enhanced SpeedStep Enable */
155         wrmsr(IA32_MISC_ENABLE, msr);
156 }
157
158 #if CONFIG_USBDEBUG_DIRECT
159 static unsigned ehci_debug_addr;
160 #endif
161
162 static void model_106cx_init(device_t cpu)
163 {
164         char processor_name[49];
165
166         /* Turn on caching if we haven't already */
167         x86_enable_cache();
168
169         /* Update the microcode */
170         intel_update_microcode(microcode_updates);
171
172         /* Print processor name */
173         fill_processor_name(processor_name);
174         printk(BIOS_INFO, "CPU: %s.\n", processor_name);
175
176 #if CONFIG_USBDEBUG_DIRECT
177         // Is this caution really needed?
178         if(!ehci_debug_addr)
179                 ehci_debug_addr = get_ehci_debug();
180         set_ehci_debug(0);
181 #endif
182
183         /* Setup MTRRs */
184         x86_setup_mtrrs(32);
185         x86_mtrr_check();
186
187 #if CONFIG_USBDEBUG_DIRECT
188         set_ehci_debug(ehci_debug_addr);
189 #endif
190
191         /* Enable the local cpu apics */
192         setup_lapic();
193
194         /* Enable virtualization */
195         enable_vmx();
196
197         /* Configure C States */
198         configure_c_states();
199
200         /* Configure Enhanced SpeedStep and Thermal Sensors */
201         configure_misc();
202
203         /* TODO: PIC thermal sensor control */
204
205         /* Start up my cpu siblings */
206         intel_sibling_init(cpu);
207 }
208
209 static struct device_operations cpu_dev_ops = {
210         .init     = model_106cx_init,
211 };
212
213 static struct cpu_device_id cpu_table[] = {
214         { X86_VENDOR_INTEL, 0x106c0 }, /* Intel Atom 230 */
215         { 0, 0 },
216 };
217
218 static const struct cpu_driver driver __cpu_driver = {
219         .ops      = &cpu_dev_ops,
220         .id_table = cpu_table,
221 };
222