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