add NC support to spare mtrrs for 64G memory stored
[coreboot.git] / src / cpu / x86 / mtrr / mtrr.c
1 /*
2  * intel_mtrr.c: setting MTRR to decent values for cache initialization on P6
3  *
4  * Derived from intel_set_mtrr in intel_subr.c and mtrr.c in linux kernel
5  *
6  * Copyright 2000 Silicon Integrated System Corporation
7  *
8  *      This program is free software; you can redistribute it and/or modify
9  *      it under the terms of the GNU General Public License as published by
10  *      the Free Software Foundation; either version 2 of the License, or
11  *      (at your option) any later version.
12  *
13  *      This program is distributed in the hope that it will be useful,
14  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *      GNU General Public License for more details.
17  *
18  *      You should have received a copy of the GNU General Public License
19  *      along with this program; if not, write to the Free Software
20  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *
23  * Reference: Intel Architecture Software Developer's Manual, Volume 3: System Programming
24  */
25 /*
26         2005.1 yhlu add NC support to spare mtrrs for 64G memory stored
27 */
28 #include <stddef.h>
29 #include <console/console.h>
30 #include <device/device.h>
31 #include <cpu/x86/msr.h>
32 #include <cpu/x86/mtrr.h>
33 #include <cpu/x86/cache.h>
34
35 #warning "FIXME I do not properly handle address more than 36 physical address bits"
36
37 //#define k8 0
38 #define k8 1
39
40 #if k8
41 # define ADDRESS_BITS 40
42 #else
43 # define ADDRESS_BITS 36
44 #endif
45 #define ADDRESS_BITS_HIGH (ADDRESS_BITS - 32)
46 #define ADDRESS_MASK_HIGH ((1u << ADDRESS_BITS_HIGH) - 1)
47
48 static unsigned int mtrr_msr[] = {
49         MTRRfix64K_00000_MSR, MTRRfix16K_80000_MSR, MTRRfix16K_A0000_MSR,
50         MTRRfix4K_C0000_MSR, MTRRfix4K_C8000_MSR, MTRRfix4K_D0000_MSR, MTRRfix4K_D8000_MSR,
51         MTRRfix4K_E0000_MSR, MTRRfix4K_E8000_MSR, MTRRfix4K_F0000_MSR, MTRRfix4K_F8000_MSR,
52 };
53
54
55 static void enable_fixed_mtrr(void)
56 {
57         msr_t msr;
58
59         msr = rdmsr(MTRRdefType_MSR);
60         msr.lo |= 0xc00;
61         wrmsr(MTRRdefType_MSR, msr);
62 }
63
64 static void enable_var_mtrr(void)
65 {
66         msr_t msr;
67
68         msr = rdmsr(MTRRdefType_MSR);
69         msr.lo |= 0x800;
70         wrmsr(MTRRdefType_MSR, msr);
71 }
72
73 /* setting variable mtrr, comes from linux kernel source */
74 static void set_var_mtrr(unsigned int reg, unsigned long basek, unsigned long sizek, unsigned char type)
75 {
76         msr_t base, mask;
77
78         base.hi = basek >> 22;
79         base.lo  = basek << 10;
80
81        //printk_debug("ADDRESS_MASK_HIGH=%#x\n", ADDRESS_MASK_HIGH);
82
83         if (sizek < 4*1024*1024) {
84                 mask.hi = ADDRESS_MASK_HIGH;
85                 mask.lo = ~((sizek << 10) -1);
86         }
87         else {
88                 mask.hi = ADDRESS_MASK_HIGH & (~((sizek >> 22) -1));
89                 mask.lo = 0;
90         }
91
92         if (reg >= 8)
93                 return;
94
95         // it is recommended that we disable and enable cache when we 
96         // do this. 
97         disable_cache();
98         if (sizek == 0) {
99                 msr_t zero;
100                 zero.lo = zero.hi = 0;
101                 /* The invalid bit is kept in the mask, so we simply clear the
102                    relevant mask register to disable a range. */
103                 wrmsr (MTRRphysMask_MSR(reg), zero);
104         } else {
105                 /* Bit 32-35 of MTRRphysMask should be set to 1 */
106                 base.lo |= type;
107                 mask.lo |= 0x800;
108                 wrmsr (MTRRphysBase_MSR(reg), base);
109                 wrmsr (MTRRphysMask_MSR(reg), mask);
110         }
111         enable_cache();
112 }
113
114 /* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
115 static inline unsigned int fms(unsigned int x)
116 {
117         int r;
118
119         __asm__("bsrl %1,%0\n\t"
120                 "jnz 1f\n\t"
121                 "movl $0,%0\n"
122                 "1:" : "=r" (r) : "g" (x));
123         return r;
124 }
125
126 /* fms: find least sigificant bit set */
127 static inline unsigned int fls(unsigned int x)
128 {
129         int r;
130
131         __asm__("bsfl %1,%0\n\t"
132                 "jnz 1f\n\t"
133                 "movl $32,%0\n"
134                 "1:" : "=r" (r) : "g" (x));
135         return r;
136 }
137
138 /* setting up variable and fixed mtrr
139  *
140  * From Intel Vol. III Section 9.12.4, the Range Size and Base Alignment has some kind of requirement:
141  *      1. The range size must be 2^N byte for N >= 12 (i.e 4KB minimum).
142  *      2. The base address must be 2^N aligned, where the N here is equal to the N in previous
143  *         requirement. So a 8K range must be 8K aligned not 4K aligned.
144  *
145  * These requirement is meet by "decompositing" the ramsize into Sum(Cn * 2^n, n = [0..N], Cn = [0, 1]).
146  * For Cm = 1, there is a WB range of 2^m size at base address Sum(Cm * 2^m, m = [N..n]).
147  * A 124MB (128MB - 4MB SMA) example:
148  *      ramsize = 124MB == 64MB (at 0MB) + 32MB (at 64MB) + 16MB (at 96MB ) + 8MB (at 112MB) + 4MB (120MB).
149  * But this wastes a lot of MTRR registers so we use another more "aggresive" way with Uncacheable Regions.
150  *
151  * In the Uncacheable Region scheme, we try to cover the whole ramsize by one WB region as possible,
152  * If (an only if) this can not be done we will try to decomposite the ramesize, the mathematical formula
153  * whould be ramsize = Sum(Cn * 2^n, n = [0..N], Cn = [-1, 0, 1]). For Cn = -1, a Uncachable Region is used.
154  * The same 124MB example:
155  *      ramsize = 124MB == 128MB WB (at 0MB) + 4MB UC (at 124MB)
156  * or a 156MB (128MB + 32MB - 4MB SMA) example:
157  *      ramsize = 156MB == 128MB WB (at 0MB) + 32MB WB (at 128MB) + 4MB UC (at 156MB)
158  */
159 /* 2 MTRRS are reserved for the operating system */
160 #if 0
161 #define BIOS_MTRRS 6
162 #define OS_MTRRS   2
163 #else
164 #define BIOS_MTRRS 8
165 #define OS_MTRRS   0
166 #endif
167 #define MTRRS        (BIOS_MTRRS + OS_MTRRS)
168
169
170 static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
171 {
172         unsigned int i;
173         unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
174         msr_t msr;
175         msr.lo = msr.hi = 0; /* Shut up gcc */
176         for(i = first; i < last; i++) {
177                 /* When I switch to a new msr read it in */
178                 if (fixed_msr != i >> 3) {
179                         /* But first write out the old msr */
180                         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
181                                 disable_cache();
182                                 wrmsr(mtrr_msr[fixed_msr], msr);
183                                 enable_cache();
184                         }
185                         fixed_msr = i>>3;
186                         msr = rdmsr(mtrr_msr[fixed_msr]);
187                 }
188                 if ((i & 7) < 4) {
189                         msr.lo &= ~(0xff << ((i&3)*8));
190                         msr.lo |= type << ((i&3)*8);
191                 } else {
192                         msr.hi &= ~(0xff << ((i&3)*8));
193                         msr.hi |= type << ((i&3)*8);
194                 }
195         }
196         /* Write out the final msr */
197         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
198                 disable_cache();
199                 wrmsr(mtrr_msr[fixed_msr], msr);
200                 enable_cache();
201         }
202 }
203
204 static unsigned fixed_mtrr_index(unsigned long addrk)
205 {
206         unsigned index;
207         index = (addrk - 0) >> 6;
208         if (index >= 8) {
209                 index = ((addrk - 8*64) >> 4) + 8;
210         }
211         if (index >= 24) {
212                 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
213         }
214         if (index > NUM_FIXED_RANGES) {
215                 index = NUM_FIXED_RANGES;
216         }
217         return index;
218 }
219
220 static unsigned int range_to_mtrr(unsigned int reg, 
221         unsigned long range_startk, unsigned long range_sizek,
222         unsigned long next_range_startk)
223 {
224         if (!range_sizek || (reg >= BIOS_MTRRS)) {
225                 return reg;
226         }
227         if(next_range_startk == 4096*1024) {// There is a hole below 4G, We need to use UC to spare mtrr
228                 unsigned long sizek;
229                 sizek = 4096*1024;
230                 printk_debug("Setting variable MTRR %d, base: %4dMB, range: %4dMB, type WB\n",
231                         reg, range_startk >>10, sizek >> 10);
232                 set_var_mtrr(reg++, range_startk, sizek, MTRR_TYPE_WRBACK);
233                 while(range_sizek) {
234                         unsigned long max_align, align;
235                         /* Compute the maximum size I can make a range */
236                         max_align = fls(range_startk);
237                         align = fms(range_sizek);
238                         if (align > max_align) {
239                                 align = max_align;
240                         }
241                         sizek = 1 << align;
242                         range_startk += sizek;
243                         range_sizek -= sizek;
244                 }
245                 
246                 range_startk = 4096*1024 - sizek;
247                 printk_debug("Setting variable MTRR %d, base: %4dMB, range: %4dMB, type NC\n",
248                         reg, range_startk >>10, sizek >> 10);
249                 set_var_mtrr(reg++, range_startk, sizek, MTRR_TYPE_UNCACHEABLE);
250                 return reg;
251         }
252         while(range_sizek) {
253                 unsigned long max_align, align;
254                 unsigned long sizek;
255                 /* Compute the maximum size I can make a range */
256                 max_align = fls(range_startk);
257                 align = fms(range_sizek); 
258                 if (align > max_align) {
259                         align = max_align;
260                 }
261                 sizek = 1 << align;
262                 printk_debug("Setting variable MTRR %d, base: %4dMB, range: %4dMB, type WB\n",
263                         reg, range_startk >>10, sizek >> 10);
264                 set_var_mtrr(reg++, range_startk, sizek, MTRR_TYPE_WRBACK);
265                 range_startk += sizek;
266                 range_sizek -= sizek;
267                 if (reg >= BIOS_MTRRS)
268                         break;
269         }
270         return reg;
271 }
272
273 static unsigned long resk(uint64_t value) 
274 {
275         unsigned long resultk;
276         if (value < (1ULL << 42)) {
277                 resultk = value >> 10;
278         }
279         else {
280                 resultk = 0xffffffff;
281         }
282         return resultk;
283 }
284
285 static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
286 {
287         unsigned int start_mtrr;
288         unsigned int last_mtrr;
289         start_mtrr = fixed_mtrr_index(resk(res->base));
290         last_mtrr  = fixed_mtrr_index(resk((res->base + res->size)));
291         if (start_mtrr >= NUM_FIXED_RANGES) {
292                 return;
293         }
294         printk_debug("Setting fixed MTRRs(%d-%d) Type: WB\n",
295                 start_mtrr, last_mtrr);
296         set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK);
297         
298 }
299
300 struct var_mtrr_state {
301         unsigned long range_startk, range_sizek;
302         unsigned int reg;
303 };
304
305 void set_var_mtrr_resource(void *gp, struct device *dev, struct resource *res)
306 {
307         struct var_mtrr_state *state = gp;
308         unsigned long basek, sizek;
309         if (state->reg >= BIOS_MTRRS)
310                 return;
311         basek = resk(res->base);
312         sizek = resk(res->size);
313         /* See if I can merge with the last range
314          * Either I am below 1M and the fixed mtrrs handle it, or
315          * the ranges touch.
316          */
317         if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
318                 unsigned long endk = basek + sizek;
319                 state->range_sizek = endk - state->range_startk;
320                 return;
321         }
322         /* Write the range mtrrs */
323         if (state->range_sizek != 0) {
324                 state->reg = range_to_mtrr(state->reg, state->range_startk, state->range_sizek, basek);
325                 state->range_startk = 0;
326                 state->range_sizek = 0;
327         }
328         /* Allocate an msr */
329         state->range_startk = basek;
330         state->range_sizek  = sizek;
331 }
332
333 void x86_setup_mtrrs(void)
334 {
335         /* Try this the simple way of incrementally adding together
336          * mtrrs.  If this doesn't work out we can get smart again 
337          * and clear out the mtrrs.
338          */
339         struct var_mtrr_state var_state;
340 #if !k8
341         printk_debug("\n");
342         /* Initialized the fixed_mtrrs to uncached */
343         printk_debug("Setting fixed MTRRs(%d-%d) type: UC\n", 
344                 0, NUM_FIXED_RANGES);
345         set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
346
347         /* Now see which of the fixed mtrrs cover ram.
348          */
349         search_global_resources(
350                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
351                 set_fixed_mtrr_resource, NULL);
352         printk_debug("DONE fixed MTRRs\n");
353 #endif
354
355         /* Cache as many memory areas as possible */
356         /* FIXME is there an algorithm for computing the optimal set of mtrrs? 
357          * In some cases it is definitely possible to do better.
358          */
359         var_state.range_startk = 0;
360         var_state.range_sizek = 0;
361         var_state.reg = 0;
362         search_global_resources(
363                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
364                 set_var_mtrr_resource, &var_state);
365
366         /* Write the last range */
367         var_state.reg = range_to_mtrr(var_state.reg, var_state.range_startk, var_state.range_sizek, 0);
368         printk_debug("DONE variable MTRRs\n");
369         printk_debug("Clear out the extra MTRR's\n");
370         /* Clear out the extra MTRR's */
371         while(var_state.reg < MTRRS) {
372                 set_var_mtrr(var_state.reg++, 0, 0, 0);
373         }
374         /* enable fixed MTRR */
375         printk_spew("call enable_fixed_mtrr()\n");
376         enable_fixed_mtrr();
377         printk_spew("call enable_var_mtrr()\n");
378         enable_var_mtrr();
379         printk_spew("Leave %s\n", __FUNCTION__);
380         post_code(0x6A);
381 }
382
383 int x86_mtrr_check(void)
384 {
385         /* Only Pentium Pro and later have MTRR */
386         msr_t msr;
387         printk_debug("\nMTRR check\n");
388
389         msr = rdmsr(0x2ff);
390         msr.lo >>= 10;
391
392         printk_debug("Fixed MTRRs   : ");
393         if (msr.lo & 0x01)
394                 printk_debug("Enabled\n");
395         else
396                 printk_debug("Disabled\n");
397
398         printk_debug("Variable MTRRs: ");
399         if (msr.lo & 0x02)
400                 printk_debug("Enabled\n");
401         else
402                 printk_debug("Disabled\n");
403
404         printk_debug("\n");
405
406         post_code(0x93);
407         return ((int) msr.lo);
408 }