add k8 processor name handling as required by the k8 revision guide.
[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 static inline void clear_2M_ram(unsigned long basek, struct mtrr_state *mtrr_state) 
179 {
180                 unsigned long limitk;
181                 unsigned long size;
182                 void *addr;
183
184                 /* Report every 64M */
185                 if ((basek % (64*1024)) == 0) {
186
187                         /* Restore the normal state */
188                         map_2M_page(0);
189                         restore_mtrr_state(mtrr_state);
190                         enable_lapic();
191
192                         /* Print a status message */
193                         printk_debug("%c", (basek >= TOLM_KB)?'+':'-');
194
195                         /* Return to the initialization state */
196                         set_init_ecc_mtrrs();
197                         disable_lapic();
198
199                 }
200
201                 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
202 #if 0
203                 /* couldn't happen, memory must on 2M boundary */
204                 if(limitk>endk) {
205                         limitk = enk; 
206                 }
207 #endif
208                 size = (limitk - basek) << 10;
209                 addr = map_2M_page(basek >> 11);
210                 if (addr == MAPPING_ERROR) {
211                         printk_err("Cannot map page: %x\n", basek >> 11);
212                         return;
213                 }
214
215                 /* clear memory 2M (limitk - basek) */
216                 addr = (void *)(((uint32_t)addr) | ((basek & 0x7ff) << 10));
217                 clear_memory(addr, size);
218 }
219
220 static void init_ecc_memory(unsigned node_id)
221 {
222         unsigned long startk, begink, endk;
223         unsigned long hole_startk = 0;
224         unsigned long basek;
225         struct mtrr_state mtrr_state;
226
227         device_t f1_dev, f2_dev, f3_dev;
228         int enable_scrubbing;
229         uint32_t dcl;
230
231         f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
232         if (!f1_dev) {
233                 die("Cannot find cpu function 1\n");
234         }
235         f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
236         if (!f2_dev) {
237                 die("Cannot find cpu function 2\n");
238         }
239         f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
240         if (!f3_dev) {
241                 die("Cannot find cpu function 3\n");
242         }
243
244         /* See if we scrubbing should be enabled */
245         enable_scrubbing = 1;
246         get_option(&enable_scrubbing, "hw_scrubber");
247
248         /* Enable cache scrubbing at the lowest possible rate */
249         if (enable_scrubbing) {
250                 pci_write_config32(f3_dev, SCRUB_CONTROL,
251                         (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_NONE << 0));
252         } else {
253                 pci_write_config32(f3_dev, SCRUB_CONTROL,
254                         (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
255                 printk_debug("Scrubbing Disabled\n");
256         }
257         
258
259         /* If ecc support is not enabled don't touch memory */
260         dcl = pci_read_config32(f2_dev, DRAM_CONFIG_LOW);
261         if (!(dcl & DCL_DimmEccEn)) {
262                 printk_debug("ECC Disabled\n");
263                 return;
264         }
265
266         startk = (pci_read_config32(f1_dev, 0x40 + (node_id*8)) & 0xffff0000) >> 2;
267         endk   = ((pci_read_config32(f1_dev, 0x44 + (node_id*8)) & 0xffff0000) >> 2) + 0x4000;
268
269 #if K8_HW_MEM_HOLE_SIZEK != 0
270         if (!is_cpu_pre_e0()) 
271         {
272
273                 uint32_t val;
274                 val = pci_read_config32(f1_dev, 0xf0);
275                 if(val & 1) {
276                         hole_startk = ((val & (0xff<<24)) >> 10);
277                 }
278         }
279 #endif
280         
281
282         /* Don't start too early */
283         begink = startk;
284         if (begink < CONFIG_LB_MEM_TOPK) { 
285                 begink = CONFIG_LB_MEM_TOPK;
286         }
287
288         printk_debug("Clearing memory %uK - %uK: ", begink, endk);
289
290         /* Save the normal state */
291         save_mtrr_state(&mtrr_state);
292
293         /* Switch to the init ecc state */
294         set_init_ecc_mtrrs();
295         disable_lapic();
296
297         /* Walk through 2M chunks and zero them */
298 #if K8_HW_MEM_HOLE_SIZEK != 0
299         /* here hole_startk can not be equal to begink, never. Also hole_startk is in 2M boundary, 64M? */
300         if ( (hole_startk != 0) && ((begink < hole_startk) && (endk>(4*1024*1024)))) {
301                         for(basek = begink; basek < hole_startk;
302                                 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1)))
303                         {
304                                 clear_2M_ram(basek, &mtrr_state);
305                         }
306                         for(basek = 4*1024*1024; basek < endk;
307                                 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1)))
308                         {
309                                 clear_2M_ram(basek, &mtrr_state);
310                         }
311         }
312         else 
313 #endif
314         for(basek = begink; basek < endk;
315                 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) 
316         {
317                 clear_2M_ram(basek, &mtrr_state);
318         }
319
320
321         /* Restore the normal state */
322         map_2M_page(0);
323         restore_mtrr_state(&mtrr_state);
324         enable_lapic();
325
326         /* Set the scrub base address registers */
327         pci_write_config32(f3_dev, SCRUB_ADDR_LOW,  startk << 10);
328         pci_write_config32(f3_dev, SCRUB_ADDR_HIGH, startk >> 22);
329
330         /* Enable the scrubber? */
331         if (enable_scrubbing) {
332                 /* Enable scrubbing at the lowest possible rate */
333                 pci_write_config32(f3_dev, SCRUB_CONTROL,
334                         (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_84ms << 0));
335         }
336
337         printk_debug(" done\n");
338 }
339
340 static inline void k8_errata(void)
341 {
342         msr_t msr;
343         if (is_cpu_pre_c0()) {
344                 /* Erratum 63... */
345                 msr = rdmsr(HWCR_MSR);
346                 msr.lo |= (1 << 6);
347                 wrmsr(HWCR_MSR, msr);
348
349                 /* Erratum 69... */
350                 msr = rdmsr_amd(BU_CFG_MSR);
351                 msr.hi |= (1 << (45 - 32));
352                 wrmsr_amd(BU_CFG_MSR, msr);
353
354                 /* Erratum 81... */
355                 msr = rdmsr_amd(DC_CFG_MSR);
356                 msr.lo |=  (1 << 10);
357                 wrmsr_amd(DC_CFG_MSR, msr);
358                         
359         }
360         /* I can't touch this msr on early buggy cpus */
361         if (!is_cpu_pre_b3()) {
362
363                 /* Erratum 89 ... */
364                 msr = rdmsr(NB_CFG_MSR);
365                 msr.lo |= 1 << 3;
366                 
367                 if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
368                         /* D0 later don't need it */
369                         /* Erratum 86 Disable data masking on C0 and 
370                          * later processor revs.
371                          * FIXME this is only needed if ECC is enabled.
372                          */
373                         msr.hi |= 1 << (36 - 32);
374                 }
375                 wrmsr(NB_CFG_MSR, msr);
376         }
377         
378         /* Erratum 97 ... */
379         if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
380                 msr = rdmsr_amd(DC_CFG_MSR);
381                 msr.lo |= 1 << 3;
382                 wrmsr_amd(DC_CFG_MSR, msr);
383         }       
384         
385         /* Erratum 94 ... */
386         if (is_cpu_pre_d0()) {
387                 msr = rdmsr_amd(IC_CFG_MSR);
388                 msr.lo |= 1 << 11;
389                 wrmsr_amd(IC_CFG_MSR, msr);
390         }
391
392         /* Erratum 91 prefetch miss is handled in the kernel */
393
394         /* Erratum 106 ... */
395         msr = rdmsr_amd(LS_CFG_MSR);
396         msr.lo |= 1 << 25;
397         wrmsr_amd(LS_CFG_MSR, msr);
398
399         /* Erratum 107 ... */
400         msr = rdmsr_amd(BU_CFG_MSR);
401         msr.hi |= 1 << (43 - 32);
402         wrmsr_amd(BU_CFG_MSR, msr);
403
404         if(is_cpu_d0()) {
405                 /* Erratum 110 ...*/
406                 msr = rdmsr_amd(CPU_ID_HYPER_EXT_FEATURES);
407                 msr.hi |=1;
408                 wrmsr_amd(CPU_ID_HYPER_EXT_FEATURES, msr);
409         }
410
411         if (!is_cpu_pre_e0()) 
412         {
413                 /* Erratum 110 ... */
414                 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
415                 msr.hi |=1;
416                 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
417         }
418
419         /* Erratum 122 */
420         msr = rdmsr(HWCR_MSR);
421         msr.lo |= 1 << 6;
422         wrmsr(HWCR_MSR, msr);
423
424 }
425
426
427 extern void model_fxx_update_microcode(unsigned cpu_deviceid);
428 int init_processor_name(void);
429
430 void model_fxx_init(device_t dev)
431 {
432         unsigned long i;
433         msr_t msr;
434         struct node_core_id id;
435 #if CONFIG_LOGICAL_CPUS == 1
436         unsigned siblings;
437 #endif
438
439         /* Turn on caching if we haven't already */
440         x86_enable_cache();
441         amd_setup_mtrrs();
442         x86_mtrr_check();
443
444         /* Update the microcode */
445         model_fxx_update_microcode(dev->device);
446
447         disable_cache();
448         
449         /* zero the machine check error status registers */
450         msr.lo = 0;
451         msr.hi = 0;
452         for(i=0; i<5; i++) {
453                 wrmsr(MCI_STATUS + (i*4),msr);
454         }
455
456         k8_errata();
457         
458         init_processor_name();
459         
460         enable_cache();
461
462         /* Enable the local cpu apics */
463         setup_lapic();
464
465 #if CONFIG_LOGICAL_CPUS == 1
466         siblings = cpuid_ecx(0x80000008) & 0xff;
467
468         if(siblings>0) {
469                 msr = rdmsr_amd(CPU_ID_FEATURES_MSR);
470                 msr.lo |= 1 << 28; 
471                 wrmsr_amd(CPU_ID_FEATURES_MSR, msr);
472
473                 msr = rdmsr_amd(LOGICAL_CPUS_NUM_MSR);
474                 msr.lo = (siblings+1)<<16; 
475                 wrmsr_amd(LOGICAL_CPUS_NUM_MSR, msr);
476
477                 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
478                 msr.hi |= 1<<(33-32); 
479                 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
480         } 
481
482 #endif
483
484         id = get_node_core_id(read_nb_cfg_54()); // pre e0 nb_cfg_54 can not be set
485
486         /* Is this a bad location?  In particular can another node prefecth
487          * data from this node before we have initialized it?
488          */
489         if (id.coreid == 0) init_ecc_memory(id.nodeid); // only do it for core 0
490
491 #if CONFIG_LOGICAL_CPUS==1
492         /* Start up my cpu siblings */
493 //      if(id.coreid==0)  amd_sibling_init(dev); // Don't need core1 is already be put in the CPU BUS in bus_cpu_scan
494 #endif
495
496 }
497
498 static struct device_operations cpu_dev_ops = {
499         .init = model_fxx_init,
500 };
501 static struct cpu_device_id cpu_table[] = {
502         { X86_VENDOR_AMD, 0xf50 }, /* B3 */
503         { X86_VENDOR_AMD, 0xf51 }, /* SH7-B3 */
504         { X86_VENDOR_AMD, 0xf58 }, /* SH7-C0 */
505         { X86_VENDOR_AMD, 0xf48 },
506
507         { X86_VENDOR_AMD, 0xf5A }, /* SH7-CG */
508         { X86_VENDOR_AMD, 0xf4A },
509         { X86_VENDOR_AMD, 0xf7A },
510         { X86_VENDOR_AMD, 0xfc0 }, /* DH7-CG */
511         { X86_VENDOR_AMD, 0xfe0 },
512         { X86_VENDOR_AMD, 0xff0 },
513         { X86_VENDOR_AMD, 0xf82 }, /* CH7-CG */
514         { X86_VENDOR_AMD, 0xfb2 },
515 //AMD_D0_SUPPORT
516         { X86_VENDOR_AMD, 0x10f50 }, /* SH7-D0 */
517         { X86_VENDOR_AMD, 0x10f40 },
518         { X86_VENDOR_AMD, 0x10f70 },
519         { X86_VENDOR_AMD, 0x10fc0 }, /* DH7-D0 */
520         { X86_VENDOR_AMD, 0x10ff0 },
521         { X86_VENDOR_AMD, 0x10f80 }, /* CH7-D0 */
522         { X86_VENDOR_AMD, 0x10fb0 },
523 //AMD_E0_SUPPORT
524         { X86_VENDOR_AMD, 0x20f50 }, /* SH8-E0*/
525         { X86_VENDOR_AMD, 0x20f40 },
526         { X86_VENDOR_AMD, 0x20f70 },
527         { X86_VENDOR_AMD, 0x20fc0 }, /* DH8-E0 */ /* DH-E3 */
528         { X86_VENDOR_AMD, 0x20ff0 },
529         { X86_VENDOR_AMD, 0x20f10 }, /* JH8-E1 */
530         { X86_VENDOR_AMD, 0x20f30 },
531         { X86_VENDOR_AMD, 0x20f51 }, /* SH-E4 */
532         { X86_VENDOR_AMD, 0x20f71 },
533         { X86_VENDOR_AMD, 0x20f42 }, /* SH-E5 */
534         { X86_VENDOR_AMD, 0x20ff2 }, /* DH-E6 */
535         { X86_VENDOR_AMD, 0x20fc2 },
536         { X86_VENDOR_AMD, 0x20f12 }, /* JH-E6 */
537         { X86_VENDOR_AMD, 0x20f32 },
538
539         { 0, 0 },
540 };
541 static struct cpu_driver model_fxx __cpu_driver = {
542         .ops      = &cpu_dev_ops,
543         .id_table = cpu_table,
544 };