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>
24 #include <cpu/amd/microcode.h>
26 #include <cpu/x86/cache.h>
27 #include <cpu/x86/mtrr.h>
29 #include <cpu/amd/multicore.h>
31 #include <cpu/amd/model_fxx_msr.h>
33 #if CONFIG_WAIT_BEFORE_CPUS_INIT
34 void cpus_ready_for_init(void)
36 #if CONFIG_MEM_TRAIN_SEQ == 1
37 struct sys_info *sysinfox = (struct sys_info *)((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
38 // wait for ap memory to trained
39 wait_all_core0_mem_trained(sysinfox);
44 #if CONFIG_K8_REV_F_SUPPORT == 0
45 int is_e0_later_in_bsp(int nodeid)
50 if (nodeid == 0) { // we don't need to do that for node 0 in core0/node0
51 return !is_cpu_pre_e0();
53 // d0 will be treated as e0 with this methods, but the d0 nb_cfg_54 always 0
55 dev = dev_find_slot(0, PCI_DEVFN(0x18 + nodeid, 2));
58 val_old = pci_read_config32(dev, 0x80);
61 pci_write_config32(dev, 0x80, val);
62 val = pci_read_config32(dev, 0x80);
63 e0_later = !!(val & (1 << 3));
64 if (e0_later) { // pre_e0 bit 3 always be 0 and can not be changed
65 pci_write_config32(dev, 0x80, val_old); // restore it
72 #if CONFIG_K8_REV_F_SUPPORT == 1
73 int is_cpu_f0_in_bsp(int nodeid)
77 dev = dev_find_slot(0, PCI_DEVFN(0x18 + nodeid, 3));
78 dword = pci_read_config32(dev, 0xfc);
79 return (dword & 0xfff00) == 0x40f00;
83 #define MCI_STATUS 0x401
85 static inline msr_t rdmsr_amd(u32 index)
90 :"=a"(result.lo), "=d"(result.hi)
91 :"c"(index), "D"(0x9c5a203a)
96 static inline void wrmsr_amd(u32 index, msr_t msr)
101 :"c"(index), "a"(msr.lo), "d"(msr.hi), "D"(0x9c5a203a)
106 #define ZERO_CHUNK_KB 0x800UL /* 2M */
107 #define TOLM_KB 0x400000UL
114 struct mtrr mtrrs[MTRR_COUNT];
115 msr_t top_mem, top_mem2;
119 static void save_mtrr_state(struct mtrr_state *state)
122 for (i = 0; i < MTRR_COUNT; i++) {
123 state->mtrrs[i].base = rdmsr(MTRRphysBase_MSR(i));
124 state->mtrrs[i].mask = rdmsr(MTRRphysMask_MSR(i));
126 state->top_mem = rdmsr(TOP_MEM);
127 state->top_mem2 = rdmsr(TOP_MEM2);
128 state->def_type = rdmsr(MTRRdefType_MSR);
131 static void restore_mtrr_state(struct mtrr_state *state)
136 for (i = 0; i < MTRR_COUNT; i++) {
137 wrmsr(MTRRphysBase_MSR(i), state->mtrrs[i].base);
138 wrmsr(MTRRphysMask_MSR(i), state->mtrrs[i].mask);
140 wrmsr(TOP_MEM, state->top_mem);
141 wrmsr(TOP_MEM2, state->top_mem2);
142 wrmsr(MTRRdefType_MSR, state->def_type);
148 static void print_mtrr_state(struct mtrr_state *state)
151 for (i = 0; i < MTRR_COUNT; i++) {
152 printk(BIOS_DEBUG, "var mtrr %d: %08x%08x mask: %08x%08x\n",
154 state->mtrrs[i].base.hi, state->mtrrs[i].base.lo,
155 state->mtrrs[i].mask.hi, state->mtrrs[i].mask.lo);
157 printk(BIOS_DEBUG, "top_mem: %08x%08x\n",
158 state->top_mem.hi, state->top_mem.lo);
159 printk(BIOS_DEBUG, "top_mem2: %08x%08x\n",
160 state->top_mem2.hi, state->top_mem2.lo);
161 printk(BIOS_DEBUG, "def_type: %08x%08x\n",
162 state->def_type.hi, state->def_type.lo);
166 static void set_init_ecc_mtrrs(void)
172 /* First clear all of the msrs to be safe */
173 for (i = 0; i < MTRR_COUNT; i++) {
175 zero.lo = zero.hi = 0;
176 wrmsr(MTRRphysBase_MSR(i), zero);
177 wrmsr(MTRRphysMask_MSR(i), zero);
180 /* Write back cache the first 1MB */
182 msr.lo = 0x00000000 | MTRR_TYPE_WRBACK;
183 wrmsr(MTRRphysBase_MSR(0), msr);
185 msr.lo = ~((CONFIG_RAMTOP) - 1) | 0x800;
186 wrmsr(MTRRphysMask_MSR(0), msr);
188 /* Set the default type to write combining */
190 msr.lo = 0xc00 | MTRR_TYPE_WRCOMB;
191 wrmsr(MTRRdefType_MSR, msr);
193 /* Set TOP_MEM to 4G */
201 static inline void clear_2M_ram(unsigned long basek,
202 struct mtrr_state *mtrr_state)
204 unsigned long limitk;
208 /* Report every 64M */
209 if ((basek % (64 * 1024)) == 0) {
211 /* Restore the normal state */
213 restore_mtrr_state(mtrr_state);
216 /* Print a status message */
217 printk(BIOS_DEBUG, "%c", (basek >= TOLM_KB) ? '+' : '-');
219 /* Return to the initialization state */
220 set_init_ecc_mtrrs();
225 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
227 /* couldn't happen, memory must on 2M boundary */
232 size = (limitk - basek) << 10;
233 addr = map_2M_page(basek >> 11);
234 if (addr == MAPPING_ERROR) {
235 printk(BIOS_ERR, "Cannot map page: %lx\n", basek >> 11);
239 /* clear memory 2M (limitk - basek) */
240 addr = (void *)(((uint32_t) addr) | ((basek & 0x7ff) << 10));
241 memset(addr, 0, size);
244 static void init_ecc_memory(unsigned node_id)
246 unsigned long startk, begink, endk;
248 struct mtrr_state mtrr_state;
250 device_t f1_dev, f2_dev, f3_dev;
251 int enable_scrubbing;
254 f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
256 die("Cannot find cpu function 1\n");
258 f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
260 die("Cannot find cpu function 2\n");
262 f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
264 die("Cannot find cpu function 3\n");
267 /* See if we scrubbing should be enabled */
268 enable_scrubbing = 1;
269 get_option(&enable_scrubbing, "hw_scrubber");
271 /* Enable cache scrubbing at the lowest possible rate */
272 if (enable_scrubbing) {
273 pci_write_config32(f3_dev, SCRUB_CONTROL,
274 (SCRUB_84ms << 16) | (SCRUB_84ms << 8) |
277 pci_write_config32(f3_dev, SCRUB_CONTROL,
278 (SCRUB_NONE << 16) | (SCRUB_NONE << 8) |
280 printk(BIOS_DEBUG, "Scrubbing Disabled\n");
283 /* If ecc support is not enabled don't touch memory */
284 dcl = pci_read_config32(f2_dev, DRAM_CONFIG_LOW);
285 if (!(dcl & DCL_DimmEccEn)) {
286 printk(BIOS_DEBUG, "ECC Disabled\n");
291 (pci_read_config32(f1_dev, 0x40 + (node_id * 8)) & 0xffff0000) >> 2;
293 ((pci_read_config32(f1_dev, 0x44 + (node_id * 8)) & 0xffff0000) >>
296 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
297 unsigned long hole_startk = 0;
299 #if CONFIG_K8_REV_F_SUPPORT == 0
300 if (!is_cpu_pre_e0()) {
304 val = pci_read_config32(f1_dev, 0xf0);
306 hole_startk = ((val & (0xff << 24)) >> 10);
308 #if CONFIG_K8_REV_F_SUPPORT == 0
313 /* Don't start too early */
315 if (begink < (CONFIG_RAMTOP >> 10)) {
316 begink = (CONFIG_RAMTOP >> 10);
319 printk(BIOS_DEBUG, "Clearing memory %luK - %luK: ", begink, endk);
321 /* Save the normal state */
322 save_mtrr_state(&mtrr_state);
324 /* Switch to the init ecc state */
325 set_init_ecc_mtrrs();
328 /* Walk through 2M chunks and zero them */
329 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
330 /* here hole_startk can not be equal to begink, never. Also hole_startk is in 2M boundary, 64M? */
331 if ((hole_startk != 0)
332 && ((begink < hole_startk) && (endk > (4 * 1024 * 1024)))) {
333 for (basek = begink; basek < hole_startk;
334 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) {
335 clear_2M_ram(basek, &mtrr_state);
337 for (basek = 4 * 1024 * 1024; basek < endk;
338 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) {
339 clear_2M_ram(basek, &mtrr_state);
343 for (basek = begink; basek < endk;
344 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) {
345 clear_2M_ram(basek, &mtrr_state);
348 /* Restore the normal state */
350 restore_mtrr_state(&mtrr_state);
353 /* Set the scrub base address registers */
354 pci_write_config32(f3_dev, SCRUB_ADDR_LOW, startk << 10);
355 pci_write_config32(f3_dev, SCRUB_ADDR_HIGH, startk >> 22);
357 /* Enable the scrubber? */
358 if (enable_scrubbing) {
359 /* Enable scrubbing at the lowest possible rate */
360 pci_write_config32(f3_dev, SCRUB_CONTROL,
361 (SCRUB_84ms << 16) | (SCRUB_84ms << 8) |
365 printk(BIOS_DEBUG, " done\n");
368 static inline void k8_errata(void)
371 #if CONFIG_K8_REV_F_SUPPORT == 0
372 if (is_cpu_pre_c0()) {
374 msr = rdmsr(HWCR_MSR);
376 wrmsr(HWCR_MSR, msr);
379 msr = rdmsr_amd(BU_CFG_MSR);
380 msr.hi |= (1 << (45 - 32));
381 wrmsr_amd(BU_CFG_MSR, msr);
384 msr = rdmsr_amd(DC_CFG_MSR);
386 wrmsr_amd(DC_CFG_MSR, msr);
389 /* I can't touch this msr on early buggy cpus */
390 if (!is_cpu_pre_b3()) {
393 msr = rdmsr(NB_CFG_MSR);
396 if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
397 /* D0 later don't need it */
398 /* Erratum 86 Disable data masking on C0 and
399 * later processor revs.
400 * FIXME this is only needed if ECC is enabled.
402 msr.hi |= 1 << (36 - 32);
404 wrmsr(NB_CFG_MSR, msr);
408 if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
409 msr = rdmsr_amd(DC_CFG_MSR);
411 wrmsr_amd(DC_CFG_MSR, msr);
415 if (is_cpu_pre_d0()) {
416 msr = rdmsr_amd(IC_CFG_MSR);
418 wrmsr_amd(IC_CFG_MSR, msr);
421 /* Erratum 91 prefetch miss is handled in the kernel */
423 /* Erratum 106 ... */
424 msr = rdmsr_amd(LS_CFG_MSR);
426 wrmsr_amd(LS_CFG_MSR, msr);
428 /* Erratum 107 ... */
429 msr = rdmsr_amd(BU_CFG_MSR);
430 msr.hi |= 1 << (43 - 32);
431 wrmsr_amd(BU_CFG_MSR, msr);
434 /* Erratum 110 ... */
435 msr = rdmsr_amd(CPU_ID_HYPER_EXT_FEATURES);
437 wrmsr_amd(CPU_ID_HYPER_EXT_FEATURES, msr);
441 #if CONFIG_K8_REV_F_SUPPORT == 0
442 if (!is_cpu_pre_e0())
445 /* Erratum 110 ... */
446 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
448 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
452 msr = rdmsr(HWCR_MSR);
454 wrmsr(HWCR_MSR, msr);
456 #if CONFIG_K8_REV_F_SUPPORT == 1
458 msr = rdmsr(NB_CFG_MSR);
460 wrmsr(NB_CFG_MSR, msr);
466 static unsigned ehci_debug_addr;
469 static void model_fxx_init(device_t dev)
473 struct node_core_id id;
476 if (!ehci_debug_addr)
477 ehci_debug_addr = get_ehci_debug();
481 /* Turn on caching if we haven't already */
487 set_ehci_debug(ehci_debug_addr);
490 /* Update the microcode */
491 model_fxx_update_microcode(dev->device);
495 /* zero the machine check error status registers */
498 for (i = 0; i < 5; i++) {
499 wrmsr(MCI_STATUS + (i * 4), msr);
504 /* Set SMMLOCK to avoid exploits messing with SMM */
505 msr = rdmsr(HWCR_MSR);
507 wrmsr(HWCR_MSR, msr);
511 /* Set the processor name string */
512 init_processor_name();
514 /* Enable the local cpu apics */
517 #if CONFIG_LOGICAL_CPUS == 1
518 u32 siblings = cpuid_ecx(0x80000008) & 0xff;
521 msr = rdmsr_amd(CPU_ID_FEATURES_MSR);
523 wrmsr_amd(CPU_ID_FEATURES_MSR, msr);
525 msr = rdmsr_amd(LOGICAL_CPUS_NUM_MSR);
526 msr.lo = (siblings + 1) << 16;
527 wrmsr_amd(LOGICAL_CPUS_NUM_MSR, msr);
529 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
530 msr.hi |= 1 << (33 - 32);
531 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
535 id = get_node_core_id(read_nb_cfg_54()); // pre e0 nb_cfg_54 can not be set
537 /* Is this a bad location? In particular can another node prefecth
538 * data from this node before we have initialized it?
541 init_ecc_memory(id.nodeid); // only do it for core 0
544 static struct device_operations cpu_dev_ops = {
545 .init = model_fxx_init,
548 static struct cpu_device_id cpu_table[] = {
549 #if CONFIG_K8_REV_F_SUPPORT == 0
550 { X86_VENDOR_AMD, 0xf40 }, /* SH-B0 (socket 754) */
551 { X86_VENDOR_AMD, 0xf50 }, /* SH-B0 (socket 940) */
552 { X86_VENDOR_AMD, 0xf51 }, /* SH-B3 (socket 940) */
553 { X86_VENDOR_AMD, 0xf58 }, /* SH-C0 (socket 940) */
554 { X86_VENDOR_AMD, 0xf48 }, /* SH-C0 (socket 754) */
555 { X86_VENDOR_AMD, 0xf5a }, /* SH-CG (socket 940) */
556 { X86_VENDOR_AMD, 0xf4a }, /* SH-CG (socket 754) */
557 { X86_VENDOR_AMD, 0xf7a }, /* SH-CG (socket 939) */
558 { X86_VENDOR_AMD, 0xfc0 }, /* DH-CG (socket 754) */
559 { X86_VENDOR_AMD, 0xfe0 }, /* DH-CG (socket 754) */
560 { X86_VENDOR_AMD, 0xff0 }, /* DH-CG (socket 939) */
561 { X86_VENDOR_AMD, 0xf82 }, /* CH-CG (socket 754) */
562 { X86_VENDOR_AMD, 0xfb2 }, /* CH-CG (socket 939) */
565 { X86_VENDOR_AMD, 0x10f50 }, /* SH-D0 (socket 940) */
566 { X86_VENDOR_AMD, 0x10f40 }, /* SH-D0 (socket 754) */
567 { X86_VENDOR_AMD, 0x10f70 }, /* SH-D0 (socket 939) */
568 { X86_VENDOR_AMD, 0x10fc0 }, /* DH-D0 (socket 754) */
569 { X86_VENDOR_AMD, 0x10ff0 }, /* DH-D0 (socket 939) */
570 { X86_VENDOR_AMD, 0x10f80 }, /* CH-D0 (socket 754) */
571 { X86_VENDOR_AMD, 0x10fb0 }, /* CH-D0 (socket 939) */
574 { X86_VENDOR_AMD, 0x20f50 }, /* SH-E0 */
575 { X86_VENDOR_AMD, 0x20f40 },
576 { X86_VENDOR_AMD, 0x20f70 },
577 { X86_VENDOR_AMD, 0x20fc0 }, /* DH-E3 (socket 754) */
578 { X86_VENDOR_AMD, 0x20ff0 }, /* DH-E3 (socket 939) */
579 { X86_VENDOR_AMD, 0x20f10 }, /* JH-E1 (socket 940) */
580 { X86_VENDOR_AMD, 0x20f51 }, /* SH-E4 (socket 940) */
581 { X86_VENDOR_AMD, 0x20f71 }, /* SH-E4 (socket 939) */
582 { X86_VENDOR_AMD, 0x20fb1 }, /* BH-E4 (socket 939) */
583 { X86_VENDOR_AMD, 0x20f42 }, /* SH-E5 (socket 754) */
584 { X86_VENDOR_AMD, 0x20ff2 }, /* DH-E6 (socket 939) */
585 { X86_VENDOR_AMD, 0x20fc2 }, /* DH-E6 (socket 754) */
586 { X86_VENDOR_AMD, 0x20f12 }, /* JH-E6 (socket 940) */
587 { X86_VENDOR_AMD, 0x20f32 }, /* JH-E6 (socket 939) */
588 { X86_VENDOR_AMD, 0x30ff2 }, /* E4 ? */
591 #if CONFIG_K8_REV_F_SUPPORT == 1
595 * See Revision Guide for AMD NPT Family 0Fh Processors,
596 * Publication #33610, Revision: 3.30, February 2008.
598 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
600 { X86_VENDOR_AMD, 0x40f50 }, /* SH-F0 (socket F/1207) */
601 { X86_VENDOR_AMD, 0x40f70 }, /* SH-F0 (socket AM2) */
602 { X86_VENDOR_AMD, 0x40f40 }, /* SH-F0 (socket S1g1) */
603 { X86_VENDOR_AMD, 0x40f11 }, /* JH-F1 (socket F/1207) */
604 { X86_VENDOR_AMD, 0x40f31 }, /* JH-F1 (socket AM2) */
605 { X86_VENDOR_AMD, 0x40f01 }, /* JH-F1 (socket S1g1) */
607 { X86_VENDOR_AMD, 0x40f12 }, /* JH-F2 (socket F/1207) */
608 { X86_VENDOR_AMD, 0x40f32 }, /* JH-F2 (socket AM2) */
609 { X86_VENDOR_AMD, 0x40fb2 }, /* BH-F2 (socket AM2) */
610 { X86_VENDOR_AMD, 0x40f82 }, /* BH-F2 (socket S1g1) */
611 { X86_VENDOR_AMD, 0x40ff2 }, /* DH-F2 (socket AM2) */
612 { X86_VENDOR_AMD, 0x50ff2 }, /* DH-F2 (socket AM2) */
613 { X86_VENDOR_AMD, 0x40fc2 }, /* DH-F2 (socket S1g1) */
614 { X86_VENDOR_AMD, 0x40f13 }, /* JH-F3 (socket F/1207) */
615 { X86_VENDOR_AMD, 0x40f33 }, /* JH-F3 (socket AM2) */
616 { X86_VENDOR_AMD, 0x50fd3 }, /* JH-F3 (socket F/1207) */
617 { X86_VENDOR_AMD, 0xc0f13 }, /* JH-F3 (socket F/1207) */
618 { X86_VENDOR_AMD, 0x50ff3 }, /* DH-F3 (socket AM2) */
619 { X86_VENDOR_AMD, 0x60fb1 }, /* BH-G1 (socket AM2) */
620 { X86_VENDOR_AMD, 0x60f81 }, /* BH-G1 (socket S1g1) */
621 { X86_VENDOR_AMD, 0x60fb2 }, /* BH-G2 (socket AM2) */
622 { X86_VENDOR_AMD, 0x60f82 }, /* BH-G2 (socket S1g1) */
623 { X86_VENDOR_AMD, 0x70ff1 }, /* DH-G1 (socket AM2) */
624 { X86_VENDOR_AMD, 0x60ff2 }, /* DH-G2 (socket AM2) */
625 { X86_VENDOR_AMD, 0x70ff2 }, /* DH-G2 (socket AM2) */
626 { X86_VENDOR_AMD, 0x60fc2 }, /* DH-G2 (socket S1g1) */
627 { X86_VENDOR_AMD, 0x70fc2 }, /* DH-G2 (socket S1g1) */
633 static const struct cpu_driver model_fxx __cpu_driver = {
635 .id_table = cpu_table,