1 /* Needed so the AMD K8 runs correctly. */
2 #include <console/console.h>
3 #include <cpu/x86/msr.h>
4 #include <cpu/amd/mtrr.h>
5 #include <device/device.h>
6 #include <device/device.h>
7 #include <device/pci.h>
9 #include <cpu/x86/msr.h>
10 #include <cpu/x86/pae.h>
11 #include <pc80/mc146818rtc.h>
12 #include <cpu/x86/lapic.h>
13 #include "../../../northbridge/amd/amdk8/amdk8.h"
14 #include "../../../northbridge/amd/amdk8/cpu_rev.c"
16 #include <cpu/x86/cache.h>
17 #include <cpu/x86/mtrr.h>
18 #include <cpu/x86/mem.h>
19 #include "model_fxx_msr.h"
21 #define MCI_STATUS 0x401
23 static inline msr_t rdmsr_amd(unsigned index)
26 __asm__ __volatile__ (
28 : "=a" (result.lo), "=d" (result.hi)
29 : "c" (index), "D" (0x9c5a203a)
34 static inline void wrmsr_amd(unsigned index, msr_t msr)
36 __asm__ __volatile__ (
39 : "c" (index), "a" (msr.lo), "d" (msr.hi), "D" (0x9c5a203a)
46 #define ZERO_CHUNK_KB 0x800UL /* 2M */
47 #define TOLM_KB 0x400000UL
54 struct mtrr mtrrs[MTRR_COUNT];
55 msr_t top_mem, top_mem2;
59 static void save_mtrr_state(struct mtrr_state *state)
62 for(i = 0; i < MTRR_COUNT; i++) {
63 state->mtrrs[i].base = rdmsr(MTRRphysBase_MSR(i));
64 state->mtrrs[i].mask = rdmsr(MTRRphysMask_MSR(i));
66 state->top_mem = rdmsr(TOP_MEM);
67 state->top_mem2 = rdmsr(TOP_MEM2);
68 state->def_type = rdmsr(MTRRdefType_MSR);
71 static void restore_mtrr_state(struct mtrr_state *state)
76 for(i = 0; i < MTRR_COUNT; i++) {
77 wrmsr(MTRRphysBase_MSR(i), state->mtrrs[i].base);
78 wrmsr(MTRRphysMask_MSR(i), state->mtrrs[i].mask);
80 wrmsr(TOP_MEM, state->top_mem);
81 wrmsr(TOP_MEM2, state->top_mem2);
82 wrmsr(MTRRdefType_MSR, state->def_type);
89 static void print_mtrr_state(struct mtrr_state *state)
92 for(i = 0; i < MTRR_COUNT; i++) {
93 printk_debug("var mtrr %d: %08x%08x mask: %08x%08x\n",
95 state->mtrrs[i].base.hi, state->mtrrs[i].base.lo,
96 state->mtrrs[i].mask.hi, state->mtrrs[i].mask.lo);
98 printk_debug("top_mem: %08x%08x\n",
99 state->top_mem.hi, state->top_mem.lo);
100 printk_debug("top_mem2: %08x%08x\n",
101 state->top_mem2.hi, state->top_mem2.lo);
102 printk_debug("def_type: %08x%08x\n",
103 state->def_type.hi, state->def_type.lo);
107 static void set_init_ecc_mtrrs(void)
113 /* First clear all of the msrs to be safe */
114 for(i = 0; i < MTRR_COUNT; i++) {
116 zero.lo = zero.hi = 0;
117 wrmsr(MTRRphysBase_MSR(i), zero);
118 wrmsr(MTRRphysMask_MSR(i), zero);
121 /* Write back cache the first 1MB */
123 msr.lo = 0x00000000 | MTRR_TYPE_WRBACK;
124 wrmsr(MTRRphysBase_MSR(0), msr);
126 msr.lo = ~((CONFIG_LB_MEM_TOPK << 10) - 1) | 0x800;
127 wrmsr(MTRRphysMask_MSR(0), msr);
129 /* Set the default type to write combining */
131 msr.lo = 0xc00 | MTRR_TYPE_WRCOMB;
132 wrmsr(MTRRdefType_MSR, msr);
134 /* Set TOP_MEM to 4G */
143 static void init_ecc_memory(unsigned node_id)
145 unsigned long startk, begink, endk;
147 struct mtrr_state mtrr_state;
148 device_t f1_dev, f2_dev, f3_dev;
149 int enable_scrubbing;
152 f1_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 1));
154 die("Cannot find cpu function 1\n");
156 f2_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 2));
158 die("Cannot find cpu function 2\n");
160 f3_dev = dev_find_slot(0, PCI_DEVFN(0x18 + node_id, 3));
162 die("Cannot find cpu function 3\n");
165 /* See if we scrubbing should be enabled */
166 enable_scrubbing = 1;
167 get_option(&enable_scrubbing, "hw_scrubber");
169 /* Enable cache scrubbing at the lowest possible rate */
170 if (enable_scrubbing) {
171 pci_write_config32(f3_dev, SCRUB_CONTROL,
172 (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_NONE << 0));
174 pci_write_config32(f3_dev, SCRUB_CONTROL,
175 (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
176 printk_debug("Scrubbing Disabled\n");
180 /* If ecc support is not enabled don't touch memory */
181 dcl = pci_read_config32(f2_dev, DRAM_CONFIG_LOW);
182 if (!(dcl & DCL_DimmEccEn)) {
186 startk = (pci_read_config32(f1_dev, 0x40 + (node_id*8)) & 0xffff0000) >> 2;
187 endk = ((pci_read_config32(f1_dev, 0x44 + (node_id*8)) & 0xffff0000) >> 2) + 0x4000;
189 /* Don't start too early */
191 if (begink < CONFIG_LB_MEM_TOPK) {
192 begink = CONFIG_LB_MEM_TOPK;
194 printk_debug("Clearing memory %uK - %uK: ", startk, endk);
196 /* Save the normal state */
197 save_mtrr_state(&mtrr_state);
199 /* Switch to the init ecc state */
200 set_init_ecc_mtrrs();
203 /* Walk through 2M chunks and zero them */
204 for(basek = begink; basek < endk; basek = ((basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1))) {
205 unsigned long limitk;
209 /* Report every 64M */
210 if ((basek % (64*1024)) == 0) {
211 /* Restore the normal state */
213 restore_mtrr_state(&mtrr_state);
216 /* Print a status message */
217 printk_debug("%c", (basek >= TOLM_KB)?'+':'-');
219 /* Return to the initialization state */
220 set_init_ecc_mtrrs();
223 limitk = (basek + ZERO_CHUNK_KB) & ~(ZERO_CHUNK_KB - 1);
227 size = (limitk - basek) << 10;
228 addr = map_2M_page(basek >> 11);
229 addr = (void *)(((uint32_t)addr) | ((basek & 0x7ff) << 10));
230 if (addr == MAPPING_ERROR) {
234 /* clear memory 2M (limitk - basek) */
235 clear_memory(addr, size);
237 /* Restore the normal state */
239 restore_mtrr_state(&mtrr_state);
242 /* Set the scrub base address registers */
243 pci_write_config32(f3_dev, SCRUB_ADDR_LOW, startk << 10);
244 pci_write_config32(f3_dev, SCRUB_ADDR_HIGH, startk >> 22);
246 /* Enable the scrubber? */
247 if (enable_scrubbing) {
248 /* Enable scrubbing at the lowest possible rate */
249 pci_write_config32(f3_dev, SCRUB_CONTROL,
250 (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_84ms << 0));
253 printk_debug(" done\n");
256 static inline void k8_errata(void)
259 if (is_cpu_pre_c0()) {
261 msr = rdmsr(HWCR_MSR);
263 wrmsr(HWCR_MSR, msr);
266 msr = rdmsr_amd(BU_CFG_MSR);
267 msr.hi |= (1 << (45 - 32));
268 wrmsr_amd(BU_CFG_MSR, msr);
271 msr = rdmsr_amd(DC_CFG_MSR);
273 wrmsr_amd(DC_CFG_MSR, msr);
276 /* I can't touch this msr on early buggy cpus */
277 if (!is_cpu_pre_b3()) {
280 msr = rdmsr(NB_CFG_MSR);
283 if (!is_cpu_pre_c0()) {
284 /* Erratum 86 Disable data masking on C0 and
285 * later processor revs.
286 * FIXME this is only needed if ECC is enabled.
288 msr.hi |= 1 << (36 - 32);
290 wrmsr(NB_CFG_MSR, msr);
294 if (!is_cpu_pre_c0()) {
295 msr = rdmsr_amd(DC_CFG_MSR);
297 wrmsr_amd(DC_CFG_MSR, msr);
301 msr = rdmsr_amd(IC_CFG_MSR);
303 wrmsr_amd(IC_CFG_MSR, msr);
305 /* Erratum 91 prefetch miss is handled in the kernel */
308 void model_fxx_init(device_t dev)
310 unsigned long mmio_basek, tomk;
315 /* Turn on caching if we haven't already */
322 /* zero the machine check error status registers */
326 wrmsr(MCI_STATUS + (i*4),msr);
333 /* Is this a bad location? In particular can another node prefecth
334 * data from this node before we have initialized it?
336 nodeid = lapicid() & 0xf;
337 init_ecc_memory(nodeid);
339 /* Enable the local cpu apics */
343 static struct device_operations cpu_dev_ops = {
344 .init = model_fxx_init,
346 static struct cpu_device_id cpu_table[] = {
347 { X86_VENDOR_AMD, 0xf50 }, /* B3 */
348 { X86_VENDOR_AMD, 0xf51 }, /* SH7-B3 */
349 { X86_VENDOR_AMD, 0xf58 }, /* SH7-C0 */
350 { X86_VENDOR_AMD, 0xf48 },
352 { X86_VENDOR_AMD, 0xf5A }, /* SH7-CG */
353 { X86_VENDOR_AMD, 0xf4A },
354 { X86_VENDOR_AMD, 0xf7A },
355 { X86_VENDOR_AMD, 0xfc0 }, /* DH7-CG */
356 { X86_VENDOR_AMD, 0xfe0 },
357 { X86_VENDOR_AMD, 0xff0 },
358 { X86_VENDOR_AMD, 0xf82 }, /* CH7-CG */
359 { X86_VENDOR_AMD, 0xfb2 },
363 static struct cpu_driver model_fxx __cpu_driver = {
365 .id_table = cpu_table,