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