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
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>
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"
21 #include <cpu/x86/cache.h>
22 #include <cpu/x86/mtrr.h>
23 #include <cpu/x86/mem.h>
25 #if CONFIG_LOGICAL_CPUS==1
26 #include <cpu/amd/dualcore.h>
29 #include "model_fxx_msr.h"
31 #define MCI_STATUS 0x401
33 static inline msr_t rdmsr_amd(unsigned index)
36 __asm__ __volatile__ (
38 : "=a" (result.lo), "=d" (result.hi)
39 : "c" (index), "D" (0x9c5a203a)
44 static inline void wrmsr_amd(unsigned index, msr_t msr)
46 __asm__ __volatile__ (
49 : "c" (index), "a" (msr.lo), "d" (msr.hi), "D" (0x9c5a203a)
56 #define ZERO_CHUNK_KB 0x800UL /* 2M */
57 #define TOLM_KB 0x400000UL
64 struct mtrr mtrrs[MTRR_COUNT];
65 msr_t top_mem, top_mem2;
69 static void save_mtrr_state(struct mtrr_state *state)
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));
76 state->top_mem = rdmsr(TOP_MEM);
77 state->top_mem2 = rdmsr(TOP_MEM2);
78 state->def_type = rdmsr(MTRRdefType_MSR);
81 static void restore_mtrr_state(struct mtrr_state *state)
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);
90 wrmsr(TOP_MEM, state->top_mem);
91 wrmsr(TOP_MEM2, state->top_mem2);
92 wrmsr(MTRRdefType_MSR, state->def_type);
99 static void print_mtrr_state(struct mtrr_state *state)
102 for(i = 0; i < MTRR_COUNT; i++) {
103 printk_debug("var mtrr %d: %08x%08x mask: %08x%08x\n",
105 state->mtrrs[i].base.hi, state->mtrrs[i].base.lo,
106 state->mtrrs[i].mask.hi, state->mtrrs[i].mask.lo);
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);
117 static void set_init_ecc_mtrrs(void)
123 /* First clear all of the msrs to be safe */
124 for(i = 0; i < MTRR_COUNT; i++) {
126 zero.lo = zero.hi = 0;
127 wrmsr(MTRRphysBase_MSR(i), zero);
128 wrmsr(MTRRphysMask_MSR(i), zero);
131 /* Write back cache the first 1MB */
133 msr.lo = 0x00000000 | MTRR_TYPE_WRBACK;
134 wrmsr(MTRRphysBase_MSR(0), msr);
136 msr.lo = ~((CONFIG_LB_MEM_TOPK << 10) - 1) | 0x800;
137 wrmsr(MTRRphysMask_MSR(0), msr);
139 /* Set the default type to write combining */
141 msr.lo = 0xc00 | MTRR_TYPE_WRCOMB;
142 wrmsr(MTRRdefType_MSR, msr);
144 /* Set TOP_MEM to 4G */
152 static void init_ecc_memory(unsigned node_id)
154 unsigned long startk, begink, endk;
155 #if K8_E0_MEM_HOLE_SIZEK != 0
156 unsigned long hole_startk = 0, hole_endk = 0;
159 struct mtrr_state mtrr_state;
160 device_t f1_dev, f2_dev, f3_dev;
161 int enable_scrubbing;
164 f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
166 die("Cannot find cpu function 1\n");
168 f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
170 die("Cannot find cpu function 2\n");
172 f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
174 die("Cannot find cpu function 3\n");
177 /* See if we scrubbing should be enabled */
178 enable_scrubbing = 1;
179 get_option(&enable_scrubbing, "hw_scrubber");
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));
186 pci_write_config32(f3_dev, SCRUB_CONTROL,
187 (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
188 printk_debug("Scrubbing Disabled\n");
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");
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;
202 #if K8_E0_MEM_HOLE_SIZEK != 0
203 if (!is_cpu_pre_e0()) {
205 val = pci_read_config32(f1_dev, 0xf0);
207 hole_startk = ((val & (0xff<<24)) >> 10);
208 hole_endk = ((val & (0xff<<8))<<(16-10)) - startk;
209 hole_endk += hole_startk;
215 /* Don't start too early */
217 if (begink < CONFIG_LB_MEM_TOPK) {
218 begink = CONFIG_LB_MEM_TOPK;
220 printk_debug("Clearing memory %uK - %uK: ", startk, endk);
222 /* Save the normal state */
223 save_mtrr_state(&mtrr_state);
225 /* Switch to the init ecc state */
226 set_init_ecc_mtrrs();
229 /* Walk through 2M chunks and zero them */
230 for(basek = begink; basek < endk;
231 basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1)))
233 unsigned long limitk;
237 #if K8_E0_MEM_HOLE_SIZEK != 0
238 if ((basek >= hole_startk) && (basek < hole_endk)) continue;
240 /* Report every 64M */
241 if ((basek % (64*1024)) == 0) {
242 /* Restore the normal state */
244 restore_mtrr_state(&mtrr_state);
247 /* Print a status message */
248 printk_debug("%c", (basek >= TOLM_KB)?'+':'-');
250 /* Return to the initialization state */
251 set_init_ecc_mtrrs();
255 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
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);
266 /* clear memory 2M (limitk - basek) */
267 addr = (void *)(((uint32_t)addr) | ((basek & 0x7ff) << 10));
268 clear_memory(addr, size);
270 /* Restore the normal state */
272 restore_mtrr_state(&mtrr_state);
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);
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));
286 printk_debug(" done\n");
289 static inline void k8_errata(void)
292 if (is_cpu_pre_c0()) {
294 msr = rdmsr(HWCR_MSR);
296 wrmsr(HWCR_MSR, msr);
299 msr = rdmsr_amd(BU_CFG_MSR);
300 msr.hi |= (1 << (45 - 32));
301 wrmsr_amd(BU_CFG_MSR, msr);
304 msr = rdmsr_amd(DC_CFG_MSR);
306 wrmsr_amd(DC_CFG_MSR, msr);
309 /* I can't touch this msr on early buggy cpus */
310 if (!is_cpu_pre_b3()) {
313 msr = rdmsr(NB_CFG_MSR);
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.
322 msr.hi |= 1 << (36 - 32);
324 wrmsr(NB_CFG_MSR, msr);
328 if (!is_cpu_pre_c0() && is_cpu_pre_d0()) {
329 msr = rdmsr_amd(DC_CFG_MSR);
331 wrmsr_amd(DC_CFG_MSR, msr);
335 if (is_cpu_pre_d0()) {
336 msr = rdmsr_amd(IC_CFG_MSR);
338 wrmsr_amd(IC_CFG_MSR, msr);
341 /* Erratum 91 prefetch miss is handled in the kernel */
343 /* Erratum 106 ... */
344 msr = rdmsr_amd(LS_CFG_MSR);
346 wrmsr_amd(LS_CFG_MSR, msr);
348 /* Erratum 107 ... */
349 msr = rdmsr_amd(BU_CFG_MSR);
350 msr.hi |= 1 << (43 - 32);
351 wrmsr_amd(BU_CFG_MSR, msr);
355 msr = rdmsr_amd(CPU_ID_HYPER_EXT_FEATURES);
357 wrmsr_amd(CPU_ID_HYPER_EXT_FEATURES, msr);
360 if (!is_cpu_pre_e0()) {
361 /* Erratum 110 ... */
362 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
364 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
368 msr = rdmsr(HWCR_MSR);
370 wrmsr(HWCR_MSR, msr);
374 void model_fxx_init(device_t dev)
378 #if CONFIG_LOGICAL_CPUS
379 struct node_core_id id;
386 /* Turn on caching if we haven't already */
393 /* zero the machine check error status registers */
397 wrmsr(MCI_STATUS + (i*4),msr);
404 /* Enable the local cpu apics */
407 #if CONFIG_LOGICAL_CPUS == 1
408 siblings = cpuid_ecx(0x80000008) & 0xff;
410 id = get_node_core_id(read_nb_cfg_54()); // pre e0 nb_cfg_54 can not be set
413 msr = rdmsr_amd(CPU_ID_FEATURES_MSR);
415 wrmsr_amd(CPU_ID_FEATURES_MSR, msr);
417 msr = rdmsr_amd(LOGICAL_CPUS_NUM_MSR);
418 msr.lo = (siblings+1)<<16;
419 wrmsr_amd(LOGICAL_CPUS_NUM_MSR, msr);
421 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
422 msr.hi |= 1<<(33-32);
423 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
427 /* Is this a bad location? In particular can another node prefecth
428 * data from this node before we have initialized it?
430 if (id.coreid == 0) init_ecc_memory(id.nodeid); // only do it for core 0
432 /* Is this a bad location? In particular can another node prefecth
433 * data from this node before we have initialized it?
435 nodeid = lapicid() & 0xf;
436 init_ecc_memory(nodeid);
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
446 static struct device_operations cpu_dev_ops = {
447 .init = model_fxx_init,
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 },
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 },
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 },
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 },
490 static struct cpu_driver model_fxx __cpu_driver = {
492 .id_table = cpu_table,