lift apic id fix
[coreboot.git] / src / cpu / amd / model_fxx / model_fxx_init.c
1 /* Needed so the AMD K8 runs correctly.  */
2 #include <console/console.h>
3 #include <cpu/x86/msr.h>
4 #include <cpu/amd/mtrr.h>
5 #include <device/device.h>
6 #include <device/device.h>
7 #include <device/pci.h>
8 #include <string.h>
9 #include <cpu/x86/msr.h>
10 #include <cpu/x86/pae.h>
11 #include <pc80/mc146818rtc.h>
12 #include <cpu/x86/lapic.h>
13 #include "../../../northbridge/amd/amdk8/amdk8.h"
14 #include "../../../northbridge/amd/amdk8/cpu_rev.c"
15 #include <cpu/cpu.h>
16 #include <cpu/x86/cache.h>
17 #include <cpu/x86/mtrr.h>
18 #include <cpu/x86/mem.h>
19 #include "model_fxx_msr.h"
20
21 #define MCI_STATUS 0x401
22
23 static inline msr_t rdmsr_amd(unsigned index)
24 {
25         msr_t result;
26         __asm__ __volatile__ (
27                 "rdmsr"
28                 : "=a" (result.lo), "=d" (result.hi)
29                 : "c" (index), "D" (0x9c5a203a)
30                 );
31         return result;
32 }
33
34 static inline void wrmsr_amd(unsigned index, msr_t msr)
35 {
36         __asm__ __volatile__ (
37                 "wrmsr"
38                 : /* No outputs */
39                 : "c" (index), "a" (msr.lo), "d" (msr.hi), "D" (0x9c5a203a)
40                 );
41 }
42
43
44
45 #define MTRR_COUNT 8
46 #define ZERO_CHUNK_KB 0x800UL /* 2M */
47 #define TOLM_KB 0x400000UL
48
49 struct mtrr {
50         msr_t base;
51         msr_t mask;
52 };
53 struct mtrr_state {
54         struct mtrr mtrrs[MTRR_COUNT];
55         msr_t top_mem, top_mem2;
56         msr_t def_type;
57 };
58
59 static void save_mtrr_state(struct mtrr_state *state)
60 {
61         int i;
62         for(i = 0; i < MTRR_COUNT; i++) {
63                 state->mtrrs[i].base = rdmsr(MTRRphysBase_MSR(i));
64                 state->mtrrs[i].mask = rdmsr(MTRRphysMask_MSR(i));
65         }
66         state->top_mem  = rdmsr(TOP_MEM);
67         state->top_mem2 = rdmsr(TOP_MEM2);
68         state->def_type = rdmsr(MTRRdefType_MSR);
69 }
70
71 static void restore_mtrr_state(struct mtrr_state *state)
72 {
73         int i;
74         disable_cache();
75
76         for(i = 0; i < MTRR_COUNT; i++) {
77                 wrmsr(MTRRphysBase_MSR(i), state->mtrrs[i].base);
78                 wrmsr(MTRRphysMask_MSR(i), state->mtrrs[i].mask);
79         }
80         wrmsr(TOP_MEM,         state->top_mem);
81         wrmsr(TOP_MEM2,        state->top_mem2);
82         wrmsr(MTRRdefType_MSR, state->def_type);
83
84         enable_cache();
85 }
86
87
88 #if 0
89 static void print_mtrr_state(struct mtrr_state *state)
90 {
91         int i;
92         for(i = 0; i < MTRR_COUNT; i++) {
93                 printk_debug("var mtrr %d: %08x%08x mask: %08x%08x\n",
94                         i,
95                         state->mtrrs[i].base.hi, state->mtrrs[i].base.lo,
96                         state->mtrrs[i].mask.hi, state->mtrrs[i].mask.lo);
97         }
98         printk_debug("top_mem:  %08x%08x\n",
99                 state->top_mem.hi, state->top_mem.lo);
100         printk_debug("top_mem2: %08x%08x\n",
101                 state->top_mem2.hi, state->top_mem2.lo);
102         printk_debug("def_type: %08x%08x\n",
103                 state->def_type.hi, state->def_type.lo);
104 }
105 #endif
106
107 static void set_init_ecc_mtrrs(void)
108 {
109         msr_t msr;
110         int i;
111         disable_cache();
112
113         /* First clear all of the msrs to be safe */
114         for(i = 0; i < MTRR_COUNT; i++) {
115                 msr_t zero;
116                 zero.lo = zero.hi = 0;
117                 wrmsr(MTRRphysBase_MSR(i), zero);
118                 wrmsr(MTRRphysMask_MSR(i), zero);
119         }
120
121         /* Write back cache the first 1MB */
122         msr.hi = 0x00000000;
123         msr.lo = 0x00000000 | MTRR_TYPE_WRBACK;
124         wrmsr(MTRRphysBase_MSR(0), msr);
125         msr.hi = 0x000000ff;
126         msr.lo = ~((CONFIG_LB_MEM_TOPK << 10) - 1) | 0x800;
127         wrmsr(MTRRphysMask_MSR(0), msr);
128
129         /* Set the default type to write combining */
130         msr.hi = 0x00000000;
131         msr.lo = 0xc00 | MTRR_TYPE_WRCOMB;
132         wrmsr(MTRRdefType_MSR, msr);
133
134         /* Set TOP_MEM to 4G */
135         msr.hi = 0x00000001;
136         msr.lo = 0x00000000;
137         wrmsr(TOP_MEM, msr);
138
139         enable_cache();
140 }
141
142
143 static void init_ecc_memory(unsigned node_id)
144 {
145         unsigned long startk, begink, endk;
146         unsigned long basek;
147         struct mtrr_state mtrr_state;
148         device_t f1_dev, f2_dev, f3_dev;
149         int enable_scrubbing;
150         uint32_t dcl;
151         
152         f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
153         if (!f1_dev) {
154                 die("Cannot find cpu function 1\n");
155         }
156         f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
157         if (!f2_dev) {
158                 die("Cannot find cpu function 2\n");
159         }
160         f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
161         if (!f3_dev) {
162                 die("Cannot find cpu function 3\n");
163         }
164
165         /* See if we scrubbing should be enabled */
166         enable_scrubbing = 1;
167         get_option(&enable_scrubbing, "hw_scrubber");
168
169         /* Enable cache scrubbing at the lowest possible rate */
170         if (enable_scrubbing) {
171                 pci_write_config32(f3_dev, SCRUB_CONTROL,
172                         (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_NONE << 0));
173         } else {
174                 pci_write_config32(f3_dev, SCRUB_CONTROL,
175                         (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
176                 printk_debug("Scrubbing Disabled\n");
177         }
178         
179
180         /* If ecc support is not enabled don't touch memory */
181         dcl = pci_read_config32(f2_dev, DRAM_CONFIG_LOW);
182         if (!(dcl & DCL_DimmEccEn)) {
183                 return;
184         }
185
186         startk = (pci_read_config32(f1_dev, 0x40 + (node_id*8)) & 0xffff0000) >> 2;
187         endk   = ((pci_read_config32(f1_dev, 0x44 + (node_id*8)) & 0xffff0000) >> 2) + 0x4000;
188
189         /* Don't start too early */
190         begink = startk;
191         if (begink < CONFIG_LB_MEM_TOPK) {
192                 begink = CONFIG_LB_MEM_TOPK;
193         }
194         printk_debug("Clearing memory %uK - %uK: ", startk, endk);
195
196         /* Save the normal state */
197         save_mtrr_state(&mtrr_state);
198
199         /* Switch to the init ecc state */
200         set_init_ecc_mtrrs();
201         disable_lapic();
202
203         /* Walk through 2M chunks and zero them */
204         for(basek = begink; basek < endk; basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) {
205                 unsigned long limitk;
206                 unsigned long size;
207                 void *addr;
208
209                 /* Report every 64M */
210                 if ((basek % (64*1024)) == 0) {
211                         /* Restore the normal state */
212                         map_2M_page(0);
213                         restore_mtrr_state(&mtrr_state);
214                         enable_lapic();
215
216                         /* Print a status message */
217                         printk_debug("%c", (basek >= TOLM_KB)?'+':'-');
218
219                         /* Return to the initialization state */
220                         set_init_ecc_mtrrs();
221                         disable_lapic();
222                 }
223                 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
224                 if (limitk > endk) {
225                         limitk = endk;
226                 }
227                 size = (limitk - basek) << 10;
228                 addr = map_2M_page(basek >> 11);
229                 addr = (void *)(((uint32_t)addr) | ((basek & 0x7ff) << 10));
230                 if (addr == MAPPING_ERROR) {
231                         continue;
232                 }
233
234                 /* clear memory 2M (limitk - basek) */
235                 clear_memory(addr, size);
236         }
237         /* Restore the normal state */
238         map_2M_page(0);
239         restore_mtrr_state(&mtrr_state);
240         enable_lapic();
241
242         /* Set the scrub base address registers */
243         pci_write_config32(f3_dev, SCRUB_ADDR_LOW,  startk << 10);
244         pci_write_config32(f3_dev, SCRUB_ADDR_HIGH, startk >> 22);
245
246         /* Enable the scrubber? */
247         if (enable_scrubbing) {
248                 /* Enable scrubbing at the lowest possible rate */
249                 pci_write_config32(f3_dev, SCRUB_CONTROL,
250                         (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_84ms << 0));
251         }
252
253         printk_debug(" done\n");
254 }
255
256 static inline void k8_errata(void)
257 {
258         msr_t msr;
259         if (is_cpu_pre_c0()) {
260                 /* Erratum 63... */
261                 msr = rdmsr(HWCR_MSR);
262                 msr.lo |= (1 << 6);
263                 wrmsr(HWCR_MSR, msr);
264
265                 /* Erratum 69... */
266                 msr = rdmsr_amd(BU_CFG_MSR);
267                 msr.hi |= (1 << (45 - 32));
268                 wrmsr_amd(BU_CFG_MSR, msr);
269
270                 /* Erratum 81... */
271                 msr = rdmsr_amd(DC_CFG_MSR);
272                 msr.lo |=  (1 << 10);
273                 wrmsr_amd(DC_CFG_MSR, msr);
274                         
275         }
276         /* I can't touch this msr on early buggy cpus */
277         if (!is_cpu_pre_b3()) {
278
279                 /* Erratum 89 ... */
280                 msr = rdmsr(NB_CFG_MSR);
281                 msr.lo |= 1 << 3;
282                 
283                 if (!is_cpu_pre_c0()) {
284                         /* Erratum 86 Disable data masking on C0 and 
285                          * later processor revs.
286                          * FIXME this is only needed if ECC is enabled.
287                          */
288                         msr.hi |= 1 << (36 - 32);
289                 }       
290                 wrmsr(NB_CFG_MSR, msr);
291         }
292         
293         /* Erratum 97 ... */
294         if (!is_cpu_pre_c0()) {
295                 msr = rdmsr_amd(DC_CFG_MSR);
296                 msr.lo |= 1 << 3;
297                 wrmsr_amd(DC_CFG_MSR, msr);
298         }       
299         
300         /* Erratum 94 ... */
301         msr = rdmsr_amd(IC_CFG_MSR);
302         msr.lo |= 1 << 11;
303         wrmsr_amd(IC_CFG_MSR, msr);
304
305         /* Erratum 91 prefetch miss is handled in the kernel */
306 }
307
308 void model_fxx_init(device_t dev)
309 {
310         unsigned long mmio_basek, tomk;
311         unsigned long i;
312         msr_t msr;
313         unsigned nodeid;
314
315         /* Turn on caching if we haven't already */
316         x86_enable_cache();
317         amd_setup_mtrrs();
318         x86_mtrr_check();
319
320         disable_cache();
321         
322         /* zero the machine check error status registers */
323         msr.lo = 0;
324         msr.hi = 0;
325         for(i=0; i<5; i++) {
326                 wrmsr(MCI_STATUS + (i*4),msr);
327         }
328
329         k8_errata();
330         
331         enable_cache();
332
333         /* Is this a bad location?  In particular can another node prefecth
334          * data from this node before we have initialized it?
335          */
336         nodeid = lapicid() & 0xf;
337         init_ecc_memory(nodeid);
338
339         /* Enable the local cpu apics */
340         setup_lapic();
341 }
342
343 static struct device_operations cpu_dev_ops = {
344         .init = model_fxx_init,
345 };
346 static struct cpu_device_id cpu_table[] = {
347         { X86_VENDOR_AMD, 0xf50 }, /* B3 */
348         { X86_VENDOR_AMD, 0xf51 }, /* SH7-B3 */
349         { X86_VENDOR_AMD, 0xf58 }, /* SH7-C0 */
350         { X86_VENDOR_AMD, 0xf48 },
351 #if 1
352         { X86_VENDOR_AMD, 0xf5A }, /* SH7-CG */
353         { X86_VENDOR_AMD, 0xf4A },
354         { X86_VENDOR_AMD, 0xf7A },
355         { X86_VENDOR_AMD, 0xfc0 }, /* DH7-CG */
356         { X86_VENDOR_AMD, 0xfe0 },
357         { X86_VENDOR_AMD, 0xff0 },
358         { X86_VENDOR_AMD, 0xf82 }, /* CH7-CG */
359         { X86_VENDOR_AMD, 0xfb2 },
360 #endif
361         { 0, 0 },
362 };
363 static struct cpu_driver model_fxx __cpu_driver = {
364         .ops      = &cpu_dev_ops,
365         .id_table = cpu_table,
366 };