6606a92e9b69dce9f2307dd3091b8fef8a564e0c
[coreboot.git] / src / cpu / amd / mtrr / amd_earlymtrr.c
1 #ifndef AMD_EARLYMTRR_C
2 #define AMD_EARLYMTRR_C
3 #include <cpu/x86/mtrr.h>
4 #include <cpu/amd/mtrr.h>
5 #include "cpu/x86/mtrr/earlymtrr.c"
6
7 /* the fixed and variable MTTRs are power-up with random values,
8  * clear them to MTRR_TYPE_UNCACHEABLE for safty.
9  */             
10 static void do_amd_early_mtrr_init(const unsigned long *mtrr_msrs)
11 {               
12         /* Precondition:
13          *   The cache is not enabled in cr0 nor in MTRRdefType_MSR
14          *   entry32.inc ensures the cache is not enabled in cr0
15          */
16         msr_t msr;
17         const unsigned long *msr_addr;
18         unsigned long cr0;
19
20         /* Enable the access to AMD RdDram and WrDram extension bits */
21         msr = rdmsr(SYSCFG_MSR);
22         msr.lo |= SYSCFG_MSR_MtrrFixDramModEn;
23         wrmsr(SYSCFG_MSR, msr);
24
25         /* Inialize all of the relevant msrs to 0 */
26         msr.lo = 0;
27         msr.hi = 0;
28         unsigned long msr_nr;
29         for(msr_addr = mtrr_msrs; (msr_nr = *msr_addr); msr_addr++) {
30                 wrmsr(msr_nr, msr);
31         }
32
33         /* Disable the access to AMD RdDram and WrDram extension bits */
34         msr = rdmsr(SYSCFG_MSR);
35         msr.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
36         wrmsr(SYSCFG_MSR, msr);
37
38         /* Enable memory access for 0 - 1MB using top_mem */
39         msr.hi = 0;
40         msr.lo = (((CONFIG_LB_MEM_TOPK << 10) + TOP_MEM_MASK) & ~TOP_MEM_MASK);
41         wrmsr(TOP_MEM, msr);
42
43         /* Enable caching for 0 - 1MB using variable mtrr */
44 #if 0
45         set_var_mtrr(0, 0x00000000, (CONFIG_LB_MEM_TOPK << 10), MTRR_TYPE_WRBACK);
46 #else
47         msr = rdmsr(0x200);
48         msr.hi = 0x00000000;
49         msr.lo &= 0x00000f00;
50         msr.lo |= 0x00000000 | MTRR_TYPE_WRBACK;
51         wrmsr(0x200, msr);
52
53         msr = rdmsr(0x201);
54         msr.hi = 0x0000000f;
55         msr.lo &= 0x000007ff;
56         msr.lo |= (~((CONFIG_LB_MEM_TOPK << 10) - 1)) | 0x800;
57         wrmsr(0x201, msr);
58 #endif
59
60 #if defined(XIP_ROM_SIZE)
61         /* enable write through caching so we can do execute in place
62          * on the flash rom.
63          */
64         set_var_mtrr(1, XIP_ROM_BASE, XIP_ROM_SIZE, MTRR_TYPE_WRBACK);
65 #endif
66
67         /* Set the default memory type and enable fixed and variable MTRRs 
68          */
69         /* Enable Variable MTRRs */
70         msr.hi = 0x00000000;
71         msr.lo = 0x00000800;
72         wrmsr(MTRRdefType_MSR, msr);
73
74         /* Enable the MTRRs in SYSCFG */
75         msr = rdmsr(SYSCFG_MSR);
76         msr.lo |= SYSCFG_MSR_MtrrVarDramEn;
77         wrmsr(SYSCFG_MSR, msr);
78         
79 }
80
81 static void amd_early_mtrr_init(void)
82 {
83         static const unsigned long mtrr_msrs[] = {
84                 /* fixed mtrr */
85                 0x250, 0x258, 0x259,
86                 0x268, 0x269, 0x26A,
87                 0x26B, 0x26C, 0x26D,
88                 0x26E, 0x26F,
89                 /* var mtrr */
90                 0x200, 0x201, 0x202, 0x203,
91                 0x204, 0x205, 0x206, 0x207,
92                 0x208, 0x209, 0x20A, 0x20B,
93                 0x20C, 0x20D, 0x20E, 0x20F,
94                 /* var iorr */
95                 0xC0010016, 0xC0010017, 0xC0010018, 0xC0010019,
96                 /* mem top */
97                 0xC001001A, 0xC001001D,
98                 /* NULL end of table */
99                 0
100         };
101
102         /* wbinvd which is called in disable_cache() causes hangs on Opterons
103          * if there is no data in the cache.
104          * At this point we should not have the cache enabled so don't bother
105          * disabling it.
106          */
107         /* disable_cache(); */
108         do_amd_early_mtrr_init(mtrr_msrs);
109
110         enable_cache();
111 }
112
113 #endif /* AMD_EARLYMTRR_C */