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