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