Make MTRR min hole alignment 64MB
[coreboot.git] / src / cpu / x86 / mtrr / mtrr.c
1 /*
2  * 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 #include <cpu/x86/lapic.h>
40 #include <arch/cpu.h>
41 #include <arch/acpi.h>
42
43 #if CONFIG_GFXUMA
44 extern uint64_t uma_memory_base, uma_memory_size;
45 #endif
46
47 static unsigned int mtrr_msr[] = {
48         MTRRfix64K_00000_MSR, MTRRfix16K_80000_MSR, MTRRfix16K_A0000_MSR,
49         MTRRfix4K_C0000_MSR, MTRRfix4K_C8000_MSR, MTRRfix4K_D0000_MSR, MTRRfix4K_D8000_MSR,
50         MTRRfix4K_E0000_MSR, MTRRfix4K_E8000_MSR, MTRRfix4K_F0000_MSR, MTRRfix4K_F8000_MSR,
51 };
52
53 void enable_fixed_mtrr(void)
54 {
55         msr_t msr;
56
57         msr = rdmsr(MTRRdefType_MSR);
58         msr.lo |= 0xc00;
59         wrmsr(MTRRdefType_MSR, msr);
60 }
61
62 static void enable_var_mtrr(void)
63 {
64         msr_t msr;
65
66         msr = rdmsr(MTRRdefType_MSR);
67         msr.lo |= MTRRdefTypeEn;
68         wrmsr(MTRRdefType_MSR, msr);
69 }
70
71 /* setting variable mtrr, comes from linux kernel source */
72 static void set_var_mtrr(
73         unsigned int reg, unsigned long basek, unsigned long sizek,
74         unsigned char type, unsigned address_bits)
75 {
76         msr_t base, mask;
77         unsigned address_mask_high;
78
79         if (reg >= 8)
80                 return;
81
82         // it is recommended that we disable and enable cache when we
83         // do this.
84         if (sizek == 0) {
85                 disable_cache();
86
87                 msr_t zero;
88                 zero.lo = zero.hi = 0;
89                 /* The invalid bit is kept in the mask, so we simply clear the
90                    relevant mask register to disable a range. */
91                 wrmsr (MTRRphysMask_MSR(reg), zero);
92
93                 enable_cache();
94                 return;
95         }
96
97
98         address_mask_high = ((1u << (address_bits - 32u)) - 1u);
99
100         base.hi = basek >> 22;
101         base.lo  = basek << 10;
102
103         printk(BIOS_SPEW, "ADDRESS_MASK_HIGH=%#x\n", address_mask_high);
104
105         if (sizek < 4*1024*1024) {
106                 mask.hi = address_mask_high;
107                 mask.lo = ~((sizek << 10) -1);
108         }
109         else {
110                 mask.hi = address_mask_high & (~((sizek >> 22) -1));
111                 mask.lo = 0;
112         }
113
114         // it is recommended that we disable and enable cache when we
115         // do this.
116         disable_cache();
117
118         /* Bit 32-35 of MTRRphysMask should be set to 1 */
119         base.lo |= type;
120         mask.lo |= MTRRphysMaskValid;
121         wrmsr (MTRRphysBase_MSR(reg), base);
122         wrmsr (MTRRphysMask_MSR(reg), mask);
123
124         enable_cache();
125 }
126
127 /* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
128 static inline unsigned int fms(unsigned int x)
129 {
130         int r;
131
132         __asm__("bsrl %1,%0\n\t"
133                 "jnz 1f\n\t"
134                 "movl $0,%0\n"
135                 "1:" : "=r" (r) : "g" (x));
136         return r;
137 }
138
139 /* fls: find least sigificant bit set */
140 static inline unsigned int fls(unsigned int x)
141 {
142         int r;
143
144         __asm__("bsfl %1,%0\n\t"
145                 "jnz 1f\n\t"
146                 "movl $32,%0\n"
147                 "1:" : "=r" (r) : "g" (x));
148         return r;
149 }
150
151 /* setting up variable and fixed mtrr
152  *
153  * From Intel Vol. III Section 9.12.4, the Range Size and Base Alignment has some kind of requirement:
154  *      1. The range size must be 2^N byte for N >= 12 (i.e 4KB minimum).
155  *      2. The base address must be 2^N aligned, where the N here is equal to the N in previous
156  *         requirement. So a 8K range must be 8K aligned not 4K aligned.
157  *
158  * These requirement is meet by "decompositing" the ramsize into Sum(Cn * 2^n, n = [0..N], Cn = [0, 1]).
159  * For Cm = 1, there is a WB range of 2^m size at base address Sum(Cm * 2^m, m = [N..n]).
160  * A 124MB (128MB - 4MB SMA) example:
161  *      ramsize = 124MB == 64MB (at 0MB) + 32MB (at 64MB) + 16MB (at 96MB ) + 8MB (at 112MB) + 4MB (120MB).
162  * But this wastes a lot of MTRR registers so we use another more "aggresive" way with Uncacheable Regions.
163  *
164  * In the Uncacheable Region scheme, we try to cover the whole ramsize by one WB region as possible,
165  * If (an only if) this can not be done we will try to decomposite the ramesize, the mathematical formula
166  * whould be ramsize = Sum(Cn * 2^n, n = [0..N], Cn = [-1, 0, 1]). For Cn = -1, a Uncachable Region is used.
167  * The same 124MB example:
168  *      ramsize = 124MB == 128MB WB (at 0MB) + 4MB UC (at 124MB)
169  * or a 156MB (128MB + 32MB - 4MB SMA) example:
170  *      ramsize = 156MB == 128MB WB (at 0MB) + 32MB WB (at 128MB) + 4MB UC (at 156MB)
171  */
172 /* 2 MTRRS are reserved for the operating system */
173 #if 1
174 #define BIOS_MTRRS 6
175 #define OS_MTRRS   2
176 #else
177 #define BIOS_MTRRS 8
178 #define OS_MTRRS   0
179 #endif
180 #define MTRRS        (BIOS_MTRRS + OS_MTRRS)
181
182 static int total_mtrrs = MTRRS;
183 static int bios_mtrrs = BIOS_MTRRS;
184
185 static void detect_var_mtrrs(void)
186 {
187         msr_t msr;
188
189         msr = rdmsr(MTRRcap_MSR);
190
191         total_mtrrs = msr.lo & 0xff;
192         bios_mtrrs = total_mtrrs - 2;
193 }
194
195 static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
196 {
197         unsigned int i;
198         unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
199         msr_t msr;
200         msr.lo = msr.hi = 0; /* Shut up gcc */
201         for(i = first; i < last; i++) {
202                 /* When I switch to a new msr read it in */
203                 if (fixed_msr != i >> 3) {
204                         /* But first write out the old msr */
205                         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
206                                 disable_cache();
207                                 wrmsr(mtrr_msr[fixed_msr], msr);
208                                 enable_cache();
209                         }
210                         fixed_msr = i>>3;
211                         msr = rdmsr(mtrr_msr[fixed_msr]);
212                 }
213                 if ((i & 7) < 4) {
214                         msr.lo &= ~(0xff << ((i&3)*8));
215                         msr.lo |= type << ((i&3)*8);
216                 } else {
217                         msr.hi &= ~(0xff << ((i&3)*8));
218                         msr.hi |= type << ((i&3)*8);
219                 }
220         }
221         /* Write out the final msr */
222         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
223                 disable_cache();
224                 wrmsr(mtrr_msr[fixed_msr], msr);
225                 enable_cache();
226         }
227 }
228
229 static unsigned fixed_mtrr_index(unsigned long addrk)
230 {
231         unsigned index;
232         index = (addrk - 0) >> 6;
233         if (index >= 8) {
234                 index = ((addrk - 8*64) >> 4) + 8;
235         }
236         if (index >= 24) {
237                 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
238         }
239         if (index > NUM_FIXED_RANGES) {
240                 index = NUM_FIXED_RANGES;
241         }
242         return index;
243 }
244
245 static unsigned int range_to_mtrr(unsigned int reg,
246         unsigned long range_startk, unsigned long range_sizek,
247         unsigned long next_range_startk, unsigned char type,
248         unsigned int address_bits, unsigned int above4gb)
249 {
250         unsigned long hole_startk = 0, hole_sizek = 0;
251
252         if (!range_sizek) {
253                 /* If there's no MTRR hole, this function will bail out
254                  * here when called for the hole.
255                  */
256                 printk(BIOS_SPEW, "Zero-sized MTRR range @%ldKB\n", range_startk);
257                 return reg;
258         }
259
260         if (reg >= bios_mtrrs) {
261                 printk(BIOS_ERR, "Warning: Out of MTRRs for base: %4ldMB, range: %ldMB, type %s\n",
262                                 range_startk >>10, range_sizek >> 10,
263                                 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
264                                    ((type==MTRR_TYPE_WRBACK)?"WB":"Other") );
265                 return reg;
266         }
267
268 #define MIN_ALIGN 0x10000 /* 64MB */
269
270         if (above4gb == 2 && type == MTRR_TYPE_WRBACK &&
271             range_sizek > MIN_ALIGN && range_sizek % MIN_ALIGN) {
272                 /*
273                  * If this range is not divisible then instead
274                  * make a larger range and carve out an uncached hole.
275                  */
276                 hole_startk = range_startk + range_sizek;
277                 hole_sizek = MIN_ALIGN - (range_sizek % MIN_ALIGN);
278                 range_sizek += hole_sizek;
279         }
280
281         while(range_sizek) {
282                 unsigned long max_align, align;
283                 unsigned long sizek;
284                 /* Compute the maximum size I can make a range */
285                 max_align = fls(range_startk);
286                 align = fms(range_sizek);
287                 if (align > max_align) {
288                         align = max_align;
289                 }
290                 sizek = 1 << align;
291                 printk(BIOS_DEBUG, "Setting variable MTRR %d, base: %4ldMB, range: %4ldMB, type %s\n",
292                         reg, range_startk >>10, sizek >> 10,
293                         (type==MTRR_TYPE_UNCACHEABLE)?"UC":
294                             ((type==MTRR_TYPE_WRBACK)?"WB":"Other")
295                         );
296
297                 /* if range is above 4GB, MTRR is needed
298                  * only if above4gb flag is set
299                  */
300                 if (range_startk < 0x100000000ull / 1024 || above4gb)
301                         set_var_mtrr(reg++, range_startk, sizek, type, address_bits);
302                 range_startk += sizek;
303                 range_sizek -= sizek;
304                 if (reg >= bios_mtrrs) {
305                         printk(BIOS_ERR, "Running out of variable MTRRs!\n");
306                         break;
307                 }
308         }
309
310         if (hole_sizek) {
311                 printk(BIOS_DEBUG, "Adding hole at %ldMB-%ldMB\n",
312                        hole_startk >> 10, (hole_startk + hole_sizek) >> 10);
313                 reg = range_to_mtrr(reg, hole_startk, hole_sizek,
314                               next_range_startk, MTRR_TYPE_UNCACHEABLE,
315                               address_bits, above4gb);
316         }
317
318         return reg;
319 }
320
321 static unsigned long resk(uint64_t value)
322 {
323         unsigned long resultk;
324         if (value < (1ULL << 42)) {
325                 resultk = value >> 10;
326         }
327         else {
328                 resultk = 0xffffffff;
329         }
330         return resultk;
331 }
332
333 static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
334 {
335         unsigned int start_mtrr;
336         unsigned int last_mtrr;
337         start_mtrr = fixed_mtrr_index(resk(res->base));
338         last_mtrr  = fixed_mtrr_index(resk((res->base + res->size)));
339         if (start_mtrr >= NUM_FIXED_RANGES) {
340                 return;
341         }
342         printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: WB\n",
343                 start_mtrr, last_mtrr);
344         set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK);
345
346 }
347
348 #ifndef CONFIG_VAR_MTRR_HOLE
349 #define CONFIG_VAR_MTRR_HOLE 1
350 #endif
351
352 struct var_mtrr_state {
353         unsigned long range_startk, range_sizek;
354         unsigned int reg;
355         unsigned long hole_startk, hole_sizek;
356         unsigned int address_bits;
357         unsigned int above4gb; /* Set if MTRRs are needed for DRAM above 4GB */
358 };
359
360 void set_var_mtrr_resource(void *gp, struct device *dev, struct resource *res)
361 {
362         struct var_mtrr_state *state = gp;
363         unsigned long basek, sizek;
364         if (state->reg >= bios_mtrrs)
365                 return;
366         basek = resk(res->base);
367         sizek = resk(res->size);
368         /* See if I can merge with the last range
369          * Either I am below 1M and the fixed mtrrs handle it, or
370          * the ranges touch.
371          */
372         if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
373                 unsigned long endk = basek + sizek;
374                 state->range_sizek = endk - state->range_startk;
375                 return;
376         }
377         /* Write the range mtrrs */
378         if (state->range_sizek != 0) {
379 #if CONFIG_VAR_MTRR_HOLE
380                 if (state->hole_sizek == 0 && state->above4gb != 2) {
381                         /* We need to put that on to hole */
382                         unsigned long endk = basek + sizek;
383                         state->hole_startk = state->range_startk + state->range_sizek;
384                         state->hole_sizek  = basek - state->hole_startk;
385                         state->range_sizek = endk - state->range_startk;
386                         return;
387                 }
388 #endif
389                 state->reg = range_to_mtrr(state->reg, state->range_startk,
390                         state->range_sizek, basek, MTRR_TYPE_WRBACK,
391                         state->address_bits, state->above4gb);
392 #if CONFIG_VAR_MTRR_HOLE
393                 state->reg = range_to_mtrr(state->reg, state->hole_startk,
394                         state->hole_sizek, basek, MTRR_TYPE_UNCACHEABLE,
395                         state->address_bits, state->above4gb);
396 #endif
397                 state->range_startk = 0;
398                 state->range_sizek = 0;
399                 state->hole_startk = 0;
400                 state->hole_sizek = 0;
401         }
402         /* Allocate an msr */
403         printk(BIOS_SPEW, " Allocate an msr - basek = %08lx, sizek = %08lx,\n", basek, sizek);
404         state->range_startk = basek;
405         state->range_sizek  = sizek;
406 }
407
408 void x86_setup_fixed_mtrrs(void)
409 {
410         /* Try this the simple way of incrementally adding together
411          * mtrrs.  If this doesn't work out we can get smart again
412          * and clear out the mtrrs.
413          */
414
415         printk(BIOS_DEBUG, "\n");
416         /* Initialized the fixed_mtrrs to uncached */
417         printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: UC\n",
418                 0, NUM_FIXED_RANGES);
419         set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
420
421         /* Now see which of the fixed mtrrs cover ram.
422                  */
423         search_global_resources(
424                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
425                 set_fixed_mtrr_resource, NULL);
426         printk(BIOS_DEBUG, "DONE fixed MTRRs\n");
427
428         /* enable fixed MTRR */
429         printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
430         enable_fixed_mtrr();
431
432 }
433
434 void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
435 /* this routine needs to know how many address bits a given processor
436  * supports.  CPUs get grumpy when you set too many bits in
437  * their mtrr registers :(  I would generically call cpuid here
438  * and find out how many physically supported but some cpus are
439  * buggy, and report more bits then they actually support.
440  * If above4gb flag is set, variable MTRR ranges must be used to
441  * set cacheability of DRAM above 4GB. If above4gb flag is clear,
442  * some other mechanism is controlling cacheability of DRAM above 4GB.
443  */
444 {
445         /* Try this the simple way of incrementally adding together
446          * mtrrs.  If this doesn't work out we can get smart again
447          * and clear out the mtrrs.
448          */
449         struct var_mtrr_state var_state;
450
451         /* Cache as many memory areas as possible */
452         /* FIXME is there an algorithm for computing the optimal set of mtrrs?
453          * In some cases it is definitely possible to do better.
454          */
455         var_state.range_startk = 0;
456         var_state.range_sizek = 0;
457         var_state.hole_startk = 0;
458         var_state.hole_sizek = 0;
459         var_state.reg = 0;
460         var_state.address_bits = address_bits;
461         var_state.above4gb = above4gb;
462
463         /* Detect number of variable MTRRs */
464         if (above4gb == 2)
465                 detect_var_mtrrs();
466
467         search_global_resources(
468                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
469                 set_var_mtrr_resource, &var_state);
470
471 #if (CONFIG_GFXUMA == 1) /* UMA or SP. */
472         /* For now we assume the UMA space is at the end of memory below 4GB */
473         if (var_state.hole_startk || var_state.hole_sizek) {
474                 printk(BIOS_DEBUG, "Warning: Can't set up MTRR hole for UMA due to pre-existing MTRR hole.\n");
475         } else {
476 #if CONFIG_VAR_MTRR_HOLE
477                 // Increase the base range and set up UMA as an UC hole instead
478                 if (above4gb != 2)
479                         var_state.range_sizek += (uma_memory_size >> 10);
480
481                 var_state.hole_startk = (uma_memory_base >> 10);
482                 var_state.hole_sizek = (uma_memory_size >> 10);
483 #endif
484         }
485 #endif
486         /* Write the last range */
487         var_state.reg = range_to_mtrr(var_state.reg, var_state.range_startk,
488                 var_state.range_sizek, 0, MTRR_TYPE_WRBACK,
489                 var_state.address_bits, var_state.above4gb);
490 #if CONFIG_VAR_MTRR_HOLE
491         var_state.reg = range_to_mtrr(var_state.reg, var_state.hole_startk,
492                 var_state.hole_sizek, 0, MTRR_TYPE_UNCACHEABLE,
493                 var_state.address_bits, var_state.above4gb);
494 #endif
495         printk(BIOS_DEBUG, "DONE variable MTRRs\n");
496         printk(BIOS_DEBUG, "Clear out the extra MTRR's\n");
497         /* Clear out the extra MTRR's */
498         while(var_state.reg < total_mtrrs) {
499                 set_var_mtrr(var_state.reg++, 0, 0, 0, var_state.address_bits);
500         }
501
502 #if CONFIG_CACHE_ROM
503         /* Enable Caching and speculative Reads for the
504          * complete ROM now that we actually have RAM.
505          */
506         if (boot_cpu() && (acpi_slp_type != 3)) {
507                 set_var_mtrr(total_mtrrs-1, (4096-4)*1024, 4*1024,
508                         MTRR_TYPE_WRPROT, address_bits);
509         }
510 #endif
511
512         printk(BIOS_SPEW, "call enable_var_mtrr()\n");
513         enable_var_mtrr();
514         printk(BIOS_SPEW, "Leave %s\n", __func__);
515         post_code(0x6A);
516 }
517
518
519 void x86_setup_mtrrs(void)
520 {
521         int address_size;
522         x86_setup_fixed_mtrrs();
523         address_size = cpu_phys_address_size();
524         printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size);
525         x86_setup_var_mtrrs(address_size, 1);
526 }
527
528
529 int x86_mtrr_check(void)
530 {
531         /* Only Pentium Pro and later have MTRR */
532         msr_t msr;
533         printk(BIOS_DEBUG, "\nMTRR check\n");
534
535         msr = rdmsr(0x2ff);
536         msr.lo >>= 10;
537
538         printk(BIOS_DEBUG, "Fixed MTRRs   : ");
539         if (msr.lo & 0x01)
540                 printk(BIOS_DEBUG, "Enabled\n");
541         else
542                 printk(BIOS_DEBUG, "Disabled\n");
543
544         printk(BIOS_DEBUG, "Variable MTRRs: ");
545         if (msr.lo & 0x02)
546                 printk(BIOS_DEBUG, "Enabled\n");
547         else
548                 printk(BIOS_DEBUG, "Disabled\n");
549
550         printk(BIOS_DEBUG, "\n");
551
552         post_code(0x93);
553         return ((int) msr.lo);
554 }