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
8 * 2005.08 yhlu add microcode support
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>
16 #include <cpu/x86/msr.h>
17 #include <cpu/x86/pae.h>
18 #include <pc80/mc146818rtc.h>
19 #include <cpu/x86/lapic.h>
21 #include "../../../northbridge/amd/amdk8/amdk8.h"
23 #include <cpu/amd/model_fxx_rev.h>
25 #include <cpu/x86/cache.h>
26 #include <cpu/x86/mtrr.h>
27 #include <cpu/x86/mem.h>
29 #include <cpu/amd/dualcore.h>
31 #include <cpu/amd/model_fxx_msr.h>
33 int is_e0_later_in_bsp(int nodeid)
38 if(nodeid==0) { // we don't need to do that for node 0 in core0/node0
39 return !is_cpu_pre_e0();
41 // d0 will be treated as e0 with this methods, but the d0 nb_cfg_54 always 0
43 dev = dev_find_slot(0, PCI_DEVFN(0x18+nodeid,2));
45 val_old = pci_read_config32(dev, 0x80);
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
58 #define MCI_STATUS 0x401
60 static inline msr_t rdmsr_amd(unsigned index)
63 __asm__ __volatile__ (
65 : "=a" (result.lo), "=d" (result.hi)
66 : "c" (index), "D" (0x9c5a203a)
71 static inline void wrmsr_amd(unsigned index, msr_t msr)
73 __asm__ __volatile__ (
76 : "c" (index), "a" (msr.lo), "d" (msr.hi), "D" (0x9c5a203a)
82 #define ZERO_CHUNK_KB 0x800UL /* 2M */
83 #define TOLM_KB 0x400000UL
90 struct mtrr mtrrs[MTRR_COUNT];
91 msr_t top_mem, top_mem2;
95 static void save_mtrr_state(struct mtrr_state *state)
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));
102 state->top_mem = rdmsr(TOP_MEM);
103 state->top_mem2 = rdmsr(TOP_MEM2);
104 state->def_type = rdmsr(MTRRdefType_MSR);
107 static void restore_mtrr_state(struct mtrr_state *state)
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);
116 wrmsr(TOP_MEM, state->top_mem);
117 wrmsr(TOP_MEM2, state->top_mem2);
118 wrmsr(MTRRdefType_MSR, state->def_type);
125 static void print_mtrr_state(struct mtrr_state *state)
128 for(i = 0; i < MTRR_COUNT; i++) {
129 printk_debug("var mtrr %d: %08x%08x mask: %08x%08x\n",
131 state->mtrrs[i].base.hi, state->mtrrs[i].base.lo,
132 state->mtrrs[i].mask.hi, state->mtrrs[i].mask.lo);
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);
143 static void set_init_ecc_mtrrs(void)
149 /* First clear all of the msrs to be safe */
150 for(i = 0; i < MTRR_COUNT; i++) {
152 zero.lo = zero.hi = 0;
153 wrmsr(MTRRphysBase_MSR(i), zero);
154 wrmsr(MTRRphysMask_MSR(i), zero);
157 /* Write back cache the first 1MB */
159 msr.lo = 0x00000000 | MTRR_TYPE_WRBACK;
160 wrmsr(MTRRphysBase_MSR(0), msr);
162 msr.lo = ~((CONFIG_LB_MEM_TOPK << 10) - 1) | 0x800;
163 wrmsr(MTRRphysMask_MSR(0), msr);
165 /* Set the default type to write combining */
167 msr.lo = 0xc00 | MTRR_TYPE_WRCOMB;
168 wrmsr(MTRRdefType_MSR, msr);
170 /* Set TOP_MEM to 4G */
179 static void init_ecc_memory(unsigned node_id)
181 unsigned long startk, begink, endk;
182 unsigned long hole_startk = 0;
184 struct mtrr_state mtrr_state;
185 device_t f1_dev, f2_dev, f3_dev;
186 int enable_scrubbing;
189 f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
191 die("Cannot find cpu function 1\n");
193 f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
195 die("Cannot find cpu function 2\n");
197 f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
199 die("Cannot find cpu function 3\n");
202 /* See if we scrubbing should be enabled */
203 enable_scrubbing = 1;
204 get_option(&enable_scrubbing, "hw_scrubber");
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));
211 pci_write_config32(f3_dev, SCRUB_CONTROL,
212 (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
213 printk_debug("Scrubbing Disabled\n");
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");
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;
227 #if K8_HW_MEM_HOLE_SIZEK != 0
228 if (!is_cpu_pre_e0())
232 val = pci_read_config32(f1_dev, 0xf0);
234 hole_startk = ((val & (0xff<<24)) >> 10);
240 /* Don't start too early */
242 if (begink < CONFIG_LB_MEM_TOPK) {
243 begink = CONFIG_LB_MEM_TOPK;
245 printk_debug("Clearing memory %uK - %uK: ", begink, endk);
247 /* Save the normal state */
248 save_mtrr_state(&mtrr_state);
250 /* Switch to the init ecc state */
251 set_init_ecc_mtrrs();
254 /* Walk through 2M chunks and zero them */
255 for(basek = begink; basek < endk;
256 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1)))
258 unsigned long limitk;
262 #if K8_HW_MEM_HOLE_SIZEK != 0
263 if ( hole_startk != 0 ) {
264 if ((basek >= hole_startk) && (basek < 4*1024*1024)) continue;
268 /* Report every 64M */
269 if ((basek % (64*1024)) == 0) {
271 /* Restore the normal state */
273 restore_mtrr_state(&mtrr_state);
276 /* Print a status message */
277 printk_debug("%c", (basek >= TOLM_KB)?'+':'-');
279 /* Return to the initialization state */
280 set_init_ecc_mtrrs();
285 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
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);
296 /* clear memory 2M (limitk - basek) */
297 addr = (void *)(((uint32_t)addr) | ((basek & 0x7ff) << 10));
298 clear_memory(addr, size);
300 /* Restore the normal state */
302 restore_mtrr_state(&mtrr_state);
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);
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));
316 printk_debug(" done\n");
319 static inline void k8_errata(void)
322 if (is_cpu_pre_c0()) {
324 msr = rdmsr(HWCR_MSR);
326 wrmsr(HWCR_MSR, msr);
329 msr = rdmsr_amd(BU_CFG_MSR);
330 msr.hi |= (1 << (45 - 32));
331 wrmsr_amd(BU_CFG_MSR, msr);
334 msr = rdmsr_amd(DC_CFG_MSR);
336 wrmsr_amd(DC_CFG_MSR, msr);
339 /* I can't touch this msr on early buggy cpus */
340 if (!is_cpu_pre_b3()) {
343 msr = rdmsr(NB_CFG_MSR);
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.
352 msr.hi |= 1 << (36 - 32);
354 wrmsr(NB_CFG_MSR, msr);
358 if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
359 msr = rdmsr_amd(DC_CFG_MSR);
361 wrmsr_amd(DC_CFG_MSR, msr);
365 if (is_cpu_pre_d0()) {
366 msr = rdmsr_amd(IC_CFG_MSR);
368 wrmsr_amd(IC_CFG_MSR, msr);
371 /* Erratum 91 prefetch miss is handled in the kernel */
373 /* Erratum 106 ... */
374 msr = rdmsr_amd(LS_CFG_MSR);
376 wrmsr_amd(LS_CFG_MSR, msr);
378 /* Erratum 107 ... */
379 msr = rdmsr_amd(BU_CFG_MSR);
380 msr.hi |= 1 << (43 - 32);
381 wrmsr_amd(BU_CFG_MSR, msr);
385 msr = rdmsr_amd(CPU_ID_HYPER_EXT_FEATURES);
387 wrmsr_amd(CPU_ID_HYPER_EXT_FEATURES, msr);
390 if (!is_cpu_pre_e0())
392 /* Erratum 110 ... */
393 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
395 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
399 msr = rdmsr(HWCR_MSR);
401 wrmsr(HWCR_MSR, msr);
406 extern void model_fxx_update_microcode(unsigned cpu_deviceid);
408 void model_fxx_init(device_t dev)
412 struct node_core_id id;
413 #if CONFIG_LOGICAL_CPUS == 1
417 /* Turn on caching if we haven't already */
422 /* Update the microcode */
423 model_fxx_update_microcode(dev->device);
427 /* zero the machine check error status registers */
431 wrmsr(MCI_STATUS + (i*4),msr);
438 /* Enable the local cpu apics */
441 #if CONFIG_LOGICAL_CPUS == 1
442 siblings = cpuid_ecx(0x80000008) & 0xff;
445 msr = rdmsr_amd(CPU_ID_FEATURES_MSR);
447 wrmsr_amd(CPU_ID_FEATURES_MSR, msr);
449 msr = rdmsr_amd(LOGICAL_CPUS_NUM_MSR);
450 msr.lo = (siblings+1)<<16;
451 wrmsr_amd(LOGICAL_CPUS_NUM_MSR, msr);
453 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
454 msr.hi |= 1<<(33-32);
455 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
460 id = get_node_core_id(read_nb_cfg_54()); // pre e0 nb_cfg_54 can not be set
462 /* Is this a bad location? In particular can another node prefecth
463 * data from this node before we have initialized it?
465 if (id.coreid == 0) init_ecc_memory(id.nodeid); // only do it for core 0
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
474 static struct device_operations cpu_dev_ops = {
475 .init = model_fxx_init,
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 },
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 },
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 },
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 },
517 static struct cpu_driver model_fxx __cpu_driver = {
519 .id_table = cpu_table,