Revision: linuxbios@linuxbios.org--devel/freebios--devel--2.0--patch-36
[coreboot.git] / src / cpu / amd / mtrr / amd_mtrr.c
1 #include <console/console.h>
2 #include <device/device.h>
3 #include <cpu/x86/mtrr.h>
4 #include <cpu/amd/mtrr.h>
5 #include <cpu/x86/cache.h>
6 #include <cpu/x86/msr.h>
7
8 static unsigned long resk(uint64_t value) 
9 {
10         unsigned long resultk;
11         if (value < (1ULL << 42)) {
12                 resultk = value >> 10;
13         }
14         else {
15                 resultk = 0xffffffff;
16         }
17         return resultk;
18 }
19
20 static unsigned fixed_mtrr_index(unsigned long addrk)
21 {
22         unsigned index;
23         index = (addrk - 0) >> 6;
24         if (index >= 8) {
25                 index = ((addrk - 8*64) >> 4) + 8;
26         }
27         if (index >= 24) {
28                 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
29         }
30         if (index > NUM_FIXED_RANGES) {
31                 index = NUM_FIXED_RANGES;
32         }
33         return index;
34 }
35
36 static unsigned int mtrr_msr[] = {
37         MTRRfix64K_00000_MSR, MTRRfix16K_80000_MSR, MTRRfix16K_A0000_MSR,
38         MTRRfix4K_C0000_MSR, MTRRfix4K_C8000_MSR, MTRRfix4K_D0000_MSR, MTRRfix4K_D8000_MSR,
39         MTRRfix4K_E0000_MSR, MTRRfix4K_E8000_MSR, MTRRfix4K_F0000_MSR, MTRRfix4K_F8000_MSR,
40 };
41
42 static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
43 {
44         unsigned int i;
45         unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
46         msr_t msr;
47         msr.lo = msr.hi = 0; /* Shut up gcc */
48         for (i = first; i < last; i++) {
49                 /* When I switch to a new msr read it in */
50                 if (fixed_msr != i >> 3) {
51                         /* But first write out the old msr */
52                         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
53                                 disable_cache();
54                                 wrmsr(mtrr_msr[fixed_msr], msr);
55                                 enable_cache();
56                         }
57                         fixed_msr = i>>3;
58                         msr = rdmsr(mtrr_msr[fixed_msr]);
59                 }
60                 if ((i & 7) < 4) {
61                         msr.lo &= ~(0xff << ((i&3)*8));
62                         msr.lo |= type << ((i&3)*8);
63                 } else {
64                         msr.hi &= ~(0xff << ((i&3)*8));
65                         msr.hi |= type << ((i&3)*8);
66                 }
67         }
68         /* Write out the final msr */
69         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
70                 disable_cache();
71                 wrmsr(mtrr_msr[fixed_msr], msr);
72                 enable_cache();
73         }
74 }
75
76 struct mem_state {
77         unsigned long mmio_basek, tomk;
78 };
79 static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
80 {
81         struct mem_state *state = gp;
82         unsigned long topk;
83         unsigned int start_mtrr;
84         unsigned int last_mtrr;
85
86         topk = resk(res->base + res->size);
87         if (state->tomk < topk) {
88                 state->tomk = topk;
89         }
90         if ((topk < 4*1024*1024) && (state->mmio_basek < topk)) {
91                 state->mmio_basek = topk;
92         }
93         start_mtrr = fixed_mtrr_index(resk(res->base));
94         last_mtrr  = fixed_mtrr_index(resk((res->base + res->size)));
95         if (start_mtrr >= NUM_FIXED_RANGES) {
96                 return;
97         }
98         printk_debug("Setting fixed MTRRs(%d-%d) Type: WB, RdMEM, WrMEM\n",
99                      start_mtrr, last_mtrr);
100         set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM);
101         
102 }
103
104 void amd_setup_mtrrs(void)
105 {
106         struct mem_state state;
107         unsigned long i;
108         msr_t msr;
109
110         /* Enable the access to AMD RdDram and WrDram extension bits */
111         msr = rdmsr(SYSCFG_MSR);
112         msr.lo |= SYSCFG_MSR_MtrrFixDramModEn;
113         wrmsr(SYSCFG_MSR, msr);
114
115         printk_debug("\n");
116         /* Initialized the fixed_mtrrs to uncached */
117         printk_debug("Setting fixed MTRRs(%d-%d) type: UC\n", 
118                      0, NUM_FIXED_RANGES);
119         set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
120
121         /* Except for the PCI MMIO hole just before 4GB there are no
122          * significant holes in the address space, so just account
123          * for those two and move on.
124          */
125         state.mmio_basek = state.tomk = 0;
126         search_global_resources(
127                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
128                 set_fixed_mtrr_resource, &state);
129         printk_debug("DONE fixed MTRRs\n");
130         if (state.mmio_basek > state.tomk) {
131                 state.mmio_basek = state.tomk;
132         }
133         /* Round state.mmio_basek down to the nearst size that will fit in TOP_MEM */
134         state.mmio_basek = state.mmio_basek & ~TOP_MEM_MASK_KB;
135         /* Round state.tomk up to the next greater size that will fit in TOP_MEM */
136         state.tomk = (state.tomk + TOP_MEM_MASK_KB) & ~TOP_MEM_MASK_KB;
137
138         disable_cache();
139
140         /* Setup TOP_MEM */
141         msr.hi = state.mmio_basek >> 22;
142         msr.lo = state.mmio_basek << 10;
143         wrmsr(TOP_MEM, msr);
144
145         /* Setup TOP_MEM2 */
146         msr.hi = state.tomk >> 22;
147         msr.lo = state.tomk << 10;
148         wrmsr(TOP_MEM2, msr);
149
150         /* zero the IORR's before we enable to prevent
151          * undefined side effects.
152          */
153         msr.lo = msr.hi = 0;
154         for(i = IORR_FIRST; i <= IORR_LAST; i++) {
155                 wrmsr(i, msr);
156         }
157
158         /* Enable Variable Mtrrs 
159          * Enable the RdMem and WrMem bits in the fixed mtrrs.
160          * Disable access to the RdMem and WrMem in the fixed mtrr.
161          */
162         msr = rdmsr(SYSCFG_MSR);
163         msr.lo |= SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_TOM2En;
164         msr.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
165         wrmsr(SYSCFG_MSR, msr);
166
167         enable_cache();
168
169         /* Now that I have mapped what is memory and what is not
170          * Setup the mtrrs so we can cache the memory.
171          */
172         x86_setup_mtrrs();
173 }