Undoing all HDAMA commits from LNXI from r2005->2003
[coreboot.git] / src / cpu / amd / model_fxx / model_fxx_init.c
1 /* Needed so the AMD K8 runs correctly.  */
2 /* this should be done by Eric
3  * 2004.11 yhlu add d0 e0 support
4  * 2004.12 yhlu add dual core support
5  * 2005.02 yhlu add e0 memory hole support
6 */
7 #include <console/console.h>
8 #include <cpu/x86/msr.h>
9 #include <cpu/amd/mtrr.h>
10 #include <device/device.h>
11 #include <device/device.h>
12 #include <device/pci.h>
13 #include <string.h>
14 #include <cpu/x86/msr.h>
15 #include <cpu/x86/pae.h>
16 #include <pc80/mc146818rtc.h>
17 #include <cpu/x86/lapic.h>
18 #include "../../../northbridge/amd/amdk8/amdk8.h"
19 #include "../../../northbridge/amd/amdk8/cpu_rev.c"
20 #include <cpu/cpu.h>
21 #include <cpu/x86/cache.h>
22 #include <cpu/x86/mtrr.h>
23 #include <cpu/x86/mem.h>
24
25 #if CONFIG_LOGICAL_CPUS==1
26 #include <cpu/amd/dualcore.h>
27 #endif
28
29 #include "model_fxx_msr.h"
30
31 #define MCI_STATUS 0x401
32
33 static inline msr_t rdmsr_amd(unsigned index)
34 {
35         msr_t result;
36         __asm__ __volatile__ (
37                 "rdmsr"
38                 : "=a" (result.lo), "=d" (result.hi)
39                 : "c" (index), "D" (0x9c5a203a)
40                 );
41         return result;
42 }
43
44 static inline void wrmsr_amd(unsigned index, msr_t msr)
45 {
46         __asm__ __volatile__ (
47                 "wrmsr"
48                 : /* No outputs */
49                 : "c" (index), "a" (msr.lo), "d" (msr.hi), "D" (0x9c5a203a)
50                 );
51 }
52
53
54
55 #define MTRR_COUNT 8
56 #define ZERO_CHUNK_KB 0x800UL /* 2M */
57 #define TOLM_KB 0x400000UL
58
59 struct mtrr {
60         msr_t base;
61         msr_t mask;
62 };
63 struct mtrr_state {
64         struct mtrr mtrrs[MTRR_COUNT];
65         msr_t top_mem, top_mem2;
66         msr_t def_type;
67 };
68
69 static void save_mtrr_state(struct mtrr_state *state)
70 {
71         int i;
72         for(i = 0; i < MTRR_COUNT; i++) {
73                 state->mtrrs[i].base = rdmsr(MTRRphysBase_MSR(i));
74                 state->mtrrs[i].mask = rdmsr(MTRRphysMask_MSR(i));
75         }
76         state->top_mem  = rdmsr(TOP_MEM);
77         state->top_mem2 = rdmsr(TOP_MEM2);
78         state->def_type = rdmsr(MTRRdefType_MSR);
79 }
80
81 static void restore_mtrr_state(struct mtrr_state *state)
82 {
83         int i;
84         disable_cache();
85
86         for(i = 0; i < MTRR_COUNT; i++) {
87                 wrmsr(MTRRphysBase_MSR(i), state->mtrrs[i].base);
88                 wrmsr(MTRRphysMask_MSR(i), state->mtrrs[i].mask);
89         }
90         wrmsr(TOP_MEM,         state->top_mem);
91         wrmsr(TOP_MEM2,        state->top_mem2);
92         wrmsr(MTRRdefType_MSR, state->def_type);
93
94         enable_cache();
95 }
96
97
98 #if 0
99 static void print_mtrr_state(struct mtrr_state *state)
100 {
101         int i;
102         for(i = 0; i < MTRR_COUNT; i++) {
103                 printk_debug("var mtrr %d: %08x%08x mask: %08x%08x\n",
104                         i,
105                         state->mtrrs[i].base.hi, state->mtrrs[i].base.lo,
106                         state->mtrrs[i].mask.hi, state->mtrrs[i].mask.lo);
107         }
108         printk_debug("top_mem:  %08x%08x\n",
109                 state->top_mem.hi, state->top_mem.lo);
110         printk_debug("top_mem2: %08x%08x\n",
111                 state->top_mem2.hi, state->top_mem2.lo);
112         printk_debug("def_type: %08x%08x\n",
113                 state->def_type.hi, state->def_type.lo);
114 }
115 #endif
116
117 static void set_init_ecc_mtrrs(void)
118 {
119         msr_t msr;
120         int i;
121         disable_cache();
122
123         /* First clear all of the msrs to be safe */
124         for(i = 0; i < MTRR_COUNT; i++) {
125                 msr_t zero;
126                 zero.lo = zero.hi = 0;
127                 wrmsr(MTRRphysBase_MSR(i), zero);
128                 wrmsr(MTRRphysMask_MSR(i), zero);
129         }
130
131         /* Write back cache the first 1MB */
132         msr.hi = 0x00000000;
133         msr.lo = 0x00000000 | MTRR_TYPE_WRBACK;
134         wrmsr(MTRRphysBase_MSR(0), msr);
135         msr.hi = 0x000000ff;
136         msr.lo = ~((CONFIG_LB_MEM_TOPK << 10) - 1) | 0x800;
137         wrmsr(MTRRphysMask_MSR(0), msr);
138
139         /* Set the default type to write combining */
140         msr.hi = 0x00000000;
141         msr.lo = 0xc00 | MTRR_TYPE_WRCOMB;
142         wrmsr(MTRRdefType_MSR, msr);
143
144         /* Set TOP_MEM to 4G */
145         msr.hi = 0x00000001;
146         msr.lo = 0x00000000;
147         wrmsr(TOP_MEM, msr);
148
149         enable_cache();
150 }
151
152 static void init_ecc_memory(unsigned node_id)
153 {
154         unsigned long startk, begink, endk;
155 #if K8_E0_MEM_HOLE_SIZEK != 0
156         unsigned long hole_startk = 0, hole_endk = 0;
157 #endif
158         unsigned long basek;
159         struct mtrr_state mtrr_state;
160         device_t f1_dev, f2_dev, f3_dev;
161         int enable_scrubbing;
162         uint32_t dcl;
163
164         f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
165         if (!f1_dev) {
166                 die("Cannot find cpu function 1\n");
167         }
168         f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
169         if (!f2_dev) {
170                 die("Cannot find cpu function 2\n");
171         }
172         f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
173         if (!f3_dev) {
174                 die("Cannot find cpu function 3\n");
175         }
176
177         /* See if we scrubbing should be enabled */
178         enable_scrubbing = 1;
179         get_option(&enable_scrubbing, "hw_scrubber");
180
181         /* Enable cache scrubbing at the lowest possible rate */
182         if (enable_scrubbing) {
183                 pci_write_config32(f3_dev, SCRUB_CONTROL,
184                         (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_NONE << 0));
185         } else {
186                 pci_write_config32(f3_dev, SCRUB_CONTROL,
187                         (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
188                 printk_debug("Scrubbing Disabled\n");
189         }
190         
191
192         /* If ecc support is not enabled don't touch memory */
193         dcl = pci_read_config32(f2_dev, DRAM_CONFIG_LOW);
194         if (!(dcl & DCL_DimmEccEn)) {
195                 printk_debug("ECC Disabled\n");
196                 return;
197         }
198
199         startk = (pci_read_config32(f1_dev, 0x40 + (node_id*8)) & 0xffff0000) >> 2;
200         endk   = ((pci_read_config32(f1_dev, 0x44 + (node_id*8)) & 0xffff0000) >> 2) + 0x4000;
201
202 #if K8_E0_MEM_HOLE_SIZEK != 0
203         if (!is_cpu_pre_e0()) {
204                 uint32_t val;
205                 val = pci_read_config32(f1_dev, 0xf0);
206                 if((val & 1)==1) {
207                         hole_startk = ((val & (0xff<<24)) >> 10);
208                         hole_endk = ((val & (0xff<<8))<<(16-10)) - startk;
209                         hole_endk += hole_startk;
210                 }
211         }
212 #endif
213         
214
215         /* Don't start too early */
216         begink = startk;
217         if (begink < CONFIG_LB_MEM_TOPK) {
218                 begink = CONFIG_LB_MEM_TOPK;
219         }
220         printk_debug("Clearing memory %uK - %uK: ", startk, endk);
221
222         /* Save the normal state */
223         save_mtrr_state(&mtrr_state);
224
225         /* Switch to the init ecc state */
226         set_init_ecc_mtrrs();
227         disable_lapic();
228
229         /* Walk through 2M chunks and zero them */
230         for(basek = begink; basek < endk; 
231                 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) 
232         {
233                 unsigned long limitk;
234                 unsigned long size;
235                 void *addr;
236
237 #if K8_E0_MEM_HOLE_SIZEK != 0
238                 if ((basek >= hole_startk) && (basek < hole_endk)) continue;
239 #endif
240                 /* Report every 64M */
241                 if ((basek % (64*1024)) == 0) {
242                         /* Restore the normal state */
243                         map_2M_page(0);
244                         restore_mtrr_state(&mtrr_state);
245                         enable_lapic();
246
247                         /* Print a status message */
248                         printk_debug("%c", (basek >= TOLM_KB)?'+':'-');
249
250                         /* Return to the initialization state */
251                         set_init_ecc_mtrrs();
252                         disable_lapic();
253                 }
254
255                 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
256                 if (limitk > endk) {
257                         limitk = endk;
258                 }
259                 size = (limitk - basek) << 10;
260                 addr = map_2M_page(basek >> 11);
261                 if (addr == MAPPING_ERROR) {
262                         printk_err("Cannot map page: %x\n", basek >> 11);
263                         continue;
264                 }
265
266                 /* clear memory 2M (limitk - basek) */
267                 addr = (void *)(((uint32_t)addr) | ((basek & 0x7ff) << 10));
268                 clear_memory(addr, size);
269         }
270         /* Restore the normal state */
271         map_2M_page(0);
272         restore_mtrr_state(&mtrr_state);
273         enable_lapic();
274
275         /* Set the scrub base address registers */
276         pci_write_config32(f3_dev, SCRUB_ADDR_LOW,  startk << 10);
277         pci_write_config32(f3_dev, SCRUB_ADDR_HIGH, startk >> 22);
278
279         /* Enable the scrubber? */
280         if (enable_scrubbing) {
281                 /* Enable scrubbing at the lowest possible rate */
282                 pci_write_config32(f3_dev, SCRUB_CONTROL,
283                         (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_84ms << 0));
284         }
285
286         printk_debug(" done\n");
287 }
288
289 static inline void k8_errata(void)
290 {
291         msr_t msr;
292         if (is_cpu_pre_c0()) {
293                 /* Erratum 63... */
294                 msr = rdmsr(HWCR_MSR);
295                 msr.lo |= (1 << 6);
296                 wrmsr(HWCR_MSR, msr);
297
298                 /* Erratum 69... */
299                 msr = rdmsr_amd(BU_CFG_MSR);
300                 msr.hi |= (1 << (45 - 32));
301                 wrmsr_amd(BU_CFG_MSR, msr);
302
303                 /* Erratum 81... */
304                 msr = rdmsr_amd(DC_CFG_MSR);
305                 msr.lo |=  (1 << 10);
306                 wrmsr_amd(DC_CFG_MSR, msr);
307                         
308         }
309         /* I can't touch this msr on early buggy cpus */
310         if (!is_cpu_pre_b3()) {
311
312                 /* Erratum 89 ... */
313                 msr = rdmsr(NB_CFG_MSR);
314                 msr.lo |= 1 << 3;
315                 
316                 if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
317                         /* D0 later don't need it */
318                         /* Erratum 86 Disable data masking on C0 and 
319                          * later processor revs.
320                          * FIXME this is only needed if ECC is enabled.
321                          */
322                         msr.hi |= 1 << (36 - 32);
323                 }       
324                 wrmsr(NB_CFG_MSR, msr);
325         }
326         
327         /* Erratum 97 ... */
328         if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
329                 msr = rdmsr_amd(DC_CFG_MSR);
330                 msr.lo |= 1 << 3;
331                 wrmsr_amd(DC_CFG_MSR, msr);
332         }       
333         
334         /* Erratum 94 ... */
335         if (is_cpu_pre_d0()) {
336                 msr = rdmsr_amd(IC_CFG_MSR);
337                 msr.lo |= 1 << 11;
338                 wrmsr_amd(IC_CFG_MSR, msr);
339         }
340
341         /* Erratum 91 prefetch miss is handled in the kernel */
342
343         /* Erratum 106 ... */
344         msr = rdmsr_amd(LS_CFG_MSR);
345         msr.lo |= 1 << 25;
346         wrmsr_amd(LS_CFG_MSR, msr);
347
348         /* Erratum 107 ... */
349         msr = rdmsr_amd(BU_CFG_MSR);
350         msr.hi |= 1 << (43 - 32);
351         wrmsr_amd(BU_CFG_MSR, msr);
352
353         if(is_cpu_d0()) {
354                 /* Erratum 110 ...*/
355                 msr = rdmsr_amd(CPU_ID_HYPER_EXT_FEATURES);
356                 msr.hi |=1;
357                 wrmsr_amd(CPU_ID_HYPER_EXT_FEATURES, msr);
358         }
359
360         if (!is_cpu_pre_e0()) {
361                 /* Erratum 110 ... */
362                 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
363                 msr.hi |=1;
364                 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
365         }
366
367         /* Erratum 122 */
368         msr = rdmsr(HWCR_MSR);
369         msr.lo |= 1 << 6;
370         wrmsr(HWCR_MSR, msr);
371         
372 }
373
374 void model_fxx_init(device_t dev)
375 {
376         unsigned long i;
377         msr_t msr;
378 #if CONFIG_LOGICAL_CPUS
379         struct node_core_id id;
380         unsigned siblings;
381         id.coreid=0;
382 #else
383         unsigned nodeid;
384 #endif
385
386         /* Turn on caching if we haven't already */
387         x86_enable_cache();
388         amd_setup_mtrrs();
389         x86_mtrr_check();
390
391         disable_cache();
392         
393         /* zero the machine check error status registers */
394         msr.lo = 0;
395         msr.hi = 0;
396         for(i=0; i<5; i++) {
397                 wrmsr(MCI_STATUS + (i*4),msr);
398         }
399
400         k8_errata();
401         
402         enable_cache();
403
404         /* Enable the local cpu apics */
405         setup_lapic();
406
407 #if CONFIG_LOGICAL_CPUS == 1
408         siblings = cpuid_ecx(0x80000008) & 0xff;
409
410         id = get_node_core_id(read_nb_cfg_54()); // pre e0 nb_cfg_54 can not be set
411
412         if(siblings>0) {
413                 msr = rdmsr_amd(CPU_ID_FEATURES_MSR);
414                 msr.lo |= 1 << 28; 
415                 wrmsr_amd(CPU_ID_FEATURES_MSR, msr);
416
417                 msr = rdmsr_amd(LOGICAL_CPUS_NUM_MSR);
418                 msr.lo = (siblings+1)<<16; 
419                 wrmsr_amd(LOGICAL_CPUS_NUM_MSR, msr);
420
421                 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
422                 msr.hi |= 1<<(33-32); 
423                 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
424         } 
425         
426
427         /* Is this a bad location?  In particular can another node prefecth
428          * data from this node before we have initialized it?
429          */
430         if (id.coreid == 0) init_ecc_memory(id.nodeid); // only do it for core 0
431 #else
432         /* Is this a bad location?  In particular can another node prefecth
433          * data from this node before we have initialized it?
434          */
435         nodeid = lapicid() & 0xf;
436         init_ecc_memory(nodeid);
437 #endif
438
439 #if CONFIG_LOGICAL_CPUS==1
440         /* Start up my cpu siblings */
441 //      if(id.coreid==0)  amd_sibling_init(dev); // Don't need core1 is already be put in the CPU BUS in bus_cpu_scan
442 #endif
443
444 }
445
446 static struct device_operations cpu_dev_ops = {
447         .init = model_fxx_init,
448 };
449 static struct cpu_device_id cpu_table[] = {
450         { X86_VENDOR_AMD, 0xf50 }, /* B3 */
451         { X86_VENDOR_AMD, 0xf51 }, /* SH7-B3 */
452         { X86_VENDOR_AMD, 0xf58 }, /* SH7-C0 */
453         { X86_VENDOR_AMD, 0xf48 },
454 #if 1
455         { X86_VENDOR_AMD, 0xf5A }, /* SH7-CG */
456         { X86_VENDOR_AMD, 0xf4A },
457         { X86_VENDOR_AMD, 0xf7A },
458         { X86_VENDOR_AMD, 0xfc0 }, /* DH7-CG */
459         { X86_VENDOR_AMD, 0xfe0 },
460         { X86_VENDOR_AMD, 0xff0 },
461         { X86_VENDOR_AMD, 0xf82 }, /* CH7-CG */
462         { X86_VENDOR_AMD, 0xfb2 },
463 //AMD_D0_SUPPORT
464         { X86_VENDOR_AMD, 0x10f50 }, /* SH7-D0 */
465         { X86_VENDOR_AMD, 0x10f40 },
466         { X86_VENDOR_AMD, 0x10f70 },
467         { X86_VENDOR_AMD, 0x10fc0 }, /* DH7-D0 */
468         { X86_VENDOR_AMD, 0x10ff0 },
469         { X86_VENDOR_AMD, 0x10f80 }, /* CH7-D0 */
470         { X86_VENDOR_AMD, 0x10fb0 },
471 //AMD_E0_SUPPORT
472         { X86_VENDOR_AMD, 0x20f50 }, /* SH8-E0*/
473         { X86_VENDOR_AMD, 0x20f40 },
474         { X86_VENDOR_AMD, 0x20f70 },
475         { X86_VENDOR_AMD, 0x20fc0 }, /* DH8-E0 */ /* DH-E3 */
476         { X86_VENDOR_AMD, 0x20ff0 },
477         { X86_VENDOR_AMD, 0x20f10 }, /* JH8-E1 */
478         { X86_VENDOR_AMD, 0x20f30 },
479         { X86_VENDOR_AMD, 0x20f51 }, /* SH-E4 */
480         { X86_VENDOR_AMD, 0x20f71 },
481         { X86_VENDOR_AMD, 0x20f42 }, /* SH-E5 */
482         { X86_VENDOR_AMD, 0x20ff2 }, /* DH-E6 */
483         { X86_VENDOR_AMD, 0x20fc2 },
484         { X86_VENDOR_AMD, 0x20f12 }, /* JH-E6 */
485         { X86_VENDOR_AMD, 0x20f32 },
486 #endif
487
488         { 0, 0 },
489 };
490 static struct cpu_driver model_fxx __cpu_driver = {
491         .ops      = &cpu_dev_ops,
492         .id_table = cpu_table,
493 };