2 * mtrr.c: setting MTRR to decent values for cache initialization on P6
4 * Derived from intel_set_mtrr in intel_subr.c and mtrr.c in linux kernel
6 * Copyright 2000 Silicon Integrated System Corporation
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.
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.
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.
23 * Reference: Intel Architecture Software Developer's Manual, Volume 3: System Programming
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
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>
41 extern uint64_t uma_memory_base, uma_memory_size;
44 static unsigned int mtrr_msr[] = {
45 MTRRfix64K_00000_MSR, MTRRfix16K_80000_MSR, MTRRfix16K_A0000_MSR,
46 MTRRfix4K_C0000_MSR, MTRRfix4K_C8000_MSR, MTRRfix4K_D0000_MSR, MTRRfix4K_D8000_MSR,
47 MTRRfix4K_E0000_MSR, MTRRfix4K_E8000_MSR, MTRRfix4K_F0000_MSR, MTRRfix4K_F8000_MSR,
51 void enable_fixed_mtrr(void)
55 msr = rdmsr(MTRRdefType_MSR);
57 wrmsr(MTRRdefType_MSR, msr);
60 static void enable_var_mtrr(void)
64 msr = rdmsr(MTRRdefType_MSR);
66 wrmsr(MTRRdefType_MSR, msr);
69 /* setting variable mtrr, comes from linux kernel source */
70 static void set_var_mtrr(
71 unsigned int reg, unsigned long basek, unsigned long sizek,
72 unsigned char type, unsigned address_bits)
75 unsigned address_mask_high;
80 // it is recommended that we disable and enable cache when we
86 zero.lo = zero.hi = 0;
87 /* The invalid bit is kept in the mask, so we simply clear the
88 relevant mask register to disable a range. */
89 wrmsr (MTRRphysMask_MSR(reg), zero);
96 address_mask_high = ((1u << (address_bits - 32u)) - 1u);
98 base.hi = basek >> 22;
99 base.lo = basek << 10;
101 printk(BIOS_SPEW, "ADDRESS_MASK_HIGH=%#x\n", address_mask_high);
103 if (sizek < 4*1024*1024) {
104 mask.hi = address_mask_high;
105 mask.lo = ~((sizek << 10) -1);
108 mask.hi = address_mask_high & (~((sizek >> 22) -1));
112 // it is recommended that we disable and enable cache when we
116 /* Bit 32-35 of MTRRphysMask should be set to 1 */
119 wrmsr (MTRRphysBase_MSR(reg), base);
120 wrmsr (MTRRphysMask_MSR(reg), mask);
125 /* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
126 static inline unsigned int fms(unsigned int x)
130 __asm__("bsrl %1,%0\n\t"
133 "1:" : "=r" (r) : "g" (x));
137 /* fls: find least sigificant bit set */
138 static inline unsigned int fls(unsigned int x)
142 __asm__("bsfl %1,%0\n\t"
145 "1:" : "=r" (r) : "g" (x));
149 /* setting up variable and fixed mtrr
151 * From Intel Vol. III Section 9.12.4, the Range Size and Base Alignment has some kind of requirement:
152 * 1. The range size must be 2^N byte for N >= 12 (i.e 4KB minimum).
153 * 2. The base address must be 2^N aligned, where the N here is equal to the N in previous
154 * requirement. So a 8K range must be 8K aligned not 4K aligned.
156 * These requirement is meet by "decompositing" the ramsize into Sum(Cn * 2^n, n = [0..N], Cn = [0, 1]).
157 * For Cm = 1, there is a WB range of 2^m size at base address Sum(Cm * 2^m, m = [N..n]).
158 * A 124MB (128MB - 4MB SMA) example:
159 * ramsize = 124MB == 64MB (at 0MB) + 32MB (at 64MB) + 16MB (at 96MB ) + 8MB (at 112MB) + 4MB (120MB).
160 * But this wastes a lot of MTRR registers so we use another more "aggresive" way with Uncacheable Regions.
162 * In the Uncacheable Region scheme, we try to cover the whole ramsize by one WB region as possible,
163 * If (an only if) this can not be done we will try to decomposite the ramesize, the mathematical formula
164 * whould be ramsize = Sum(Cn * 2^n, n = [0..N], Cn = [-1, 0, 1]). For Cn = -1, a Uncachable Region is used.
165 * The same 124MB example:
166 * ramsize = 124MB == 128MB WB (at 0MB) + 4MB UC (at 124MB)
167 * or a 156MB (128MB + 32MB - 4MB SMA) example:
168 * ramsize = 156MB == 128MB WB (at 0MB) + 32MB WB (at 128MB) + 4MB UC (at 156MB)
170 /* 2 MTRRS are reserved for the operating system */
178 #define MTRRS (BIOS_MTRRS + OS_MTRRS)
181 static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
184 unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
186 msr.lo = msr.hi = 0; /* Shut up gcc */
187 for(i = first; i < last; i++) {
188 /* When I switch to a new msr read it in */
189 if (fixed_msr != i >> 3) {
190 /* But first write out the old msr */
191 if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
193 wrmsr(mtrr_msr[fixed_msr], msr);
197 msr = rdmsr(mtrr_msr[fixed_msr]);
200 msr.lo &= ~(0xff << ((i&3)*8));
201 msr.lo |= type << ((i&3)*8);
203 msr.hi &= ~(0xff << ((i&3)*8));
204 msr.hi |= type << ((i&3)*8);
207 /* Write out the final msr */
208 if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
210 wrmsr(mtrr_msr[fixed_msr], msr);
215 static unsigned fixed_mtrr_index(unsigned long addrk)
218 index = (addrk - 0) >> 6;
220 index = ((addrk - 8*64) >> 4) + 8;
223 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
225 if (index > NUM_FIXED_RANGES) {
226 index = NUM_FIXED_RANGES;
231 static unsigned int range_to_mtrr(unsigned int reg,
232 unsigned long range_startk, unsigned long range_sizek,
233 unsigned long next_range_startk, unsigned char type,
234 unsigned int address_bits, unsigned int above4gb)
237 /* If there's no MTRR hole, this function will bail out
238 * here when called for the hole.
240 printk(BIOS_SPEW, "Zero-sized MTRR range @%ldKB\n", range_startk);
244 if (reg >= BIOS_MTRRS) {
245 printk(BIOS_ERR, "Warning: Out of MTRRs for base: %4ldMB, range: %ldMB, type %s\n",
246 range_startk >>10, range_sizek >> 10,
247 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
248 ((type==MTRR_TYPE_WRBACK)?"WB":"Other") );
253 unsigned long max_align, align;
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) {
262 printk(BIOS_DEBUG, "Setting variable MTRR %d, base: %4ldMB, range: %4ldMB, type %s\n",
263 reg, range_startk >>10, sizek >> 10,
264 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
265 ((type==MTRR_TYPE_WRBACK)?"WB":"Other")
268 /* if range is above 4GB, MTRR is needed
269 * only if above4gb flag is set
271 if (range_startk < 0x100000000ull / 1024 || above4gb)
272 set_var_mtrr(reg++, range_startk, sizek, type, address_bits);
273 range_startk += sizek;
274 range_sizek -= sizek;
275 if (reg >= BIOS_MTRRS) {
276 printk(BIOS_ERR, "Running out of variable MTRRs!\n");
283 static unsigned long resk(uint64_t value)
285 unsigned long resultk;
286 if (value < (1ULL << 42)) {
287 resultk = value >> 10;
290 resultk = 0xffffffff;
295 static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
297 unsigned int start_mtrr;
298 unsigned int last_mtrr;
299 start_mtrr = fixed_mtrr_index(resk(res->base));
300 last_mtrr = fixed_mtrr_index(resk((res->base + res->size)));
301 if (start_mtrr >= NUM_FIXED_RANGES) {
304 printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: WB\n",
305 start_mtrr, last_mtrr);
306 set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK);
310 #ifndef CONFIG_VAR_MTRR_HOLE
311 #define CONFIG_VAR_MTRR_HOLE 1
314 struct var_mtrr_state {
315 unsigned long range_startk, range_sizek;
317 unsigned long hole_startk, hole_sizek;
318 unsigned int address_bits;
319 unsigned int above4gb; /* Set if MTRRs are needed for DRAM above 4GB */
322 void set_var_mtrr_resource(void *gp, struct device *dev, struct resource *res)
324 struct var_mtrr_state *state = gp;
325 unsigned long basek, sizek;
326 if (state->reg >= BIOS_MTRRS)
328 basek = resk(res->base);
329 sizek = resk(res->size);
330 /* See if I can merge with the last range
331 * Either I am below 1M and the fixed mtrrs handle it, or
334 if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
335 unsigned long endk = basek + sizek;
336 state->range_sizek = endk - state->range_startk;
339 /* Write the range mtrrs */
340 if (state->range_sizek != 0) {
341 #if CONFIG_VAR_MTRR_HOLE
342 if (state->hole_sizek == 0) {
343 /* We need to put that on to hole */
344 unsigned long endk = basek + sizek;
345 state->hole_startk = state->range_startk + state->range_sizek;
346 state->hole_sizek = basek - state->hole_startk;
347 state->range_sizek = endk - state->range_startk;
351 state->reg = range_to_mtrr(state->reg, state->range_startk,
352 state->range_sizek, basek, MTRR_TYPE_WRBACK,
353 state->address_bits, state->above4gb);
354 #if CONFIG_VAR_MTRR_HOLE
355 state->reg = range_to_mtrr(state->reg, state->hole_startk,
356 state->hole_sizek, basek, MTRR_TYPE_UNCACHEABLE,
357 state->address_bits, state->above4gb);
359 state->range_startk = 0;
360 state->range_sizek = 0;
361 state->hole_startk = 0;
362 state->hole_sizek = 0;
364 /* Allocate an msr */
365 printk(BIOS_SPEW, " Allocate an msr - basek = %08lx, sizek = %08lx,\n", basek, sizek);
366 state->range_startk = basek;
367 state->range_sizek = sizek;
370 void x86_setup_fixed_mtrrs(void)
372 /* Try this the simple way of incrementally adding together
373 * mtrrs. If this doesn't work out we can get smart again
374 * and clear out the mtrrs.
377 printk(BIOS_DEBUG, "\n");
378 /* Initialized the fixed_mtrrs to uncached */
379 printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: UC\n",
380 0, NUM_FIXED_RANGES);
381 set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
383 /* Now see which of the fixed mtrrs cover ram.
385 search_global_resources(
386 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
387 set_fixed_mtrr_resource, NULL);
388 printk(BIOS_DEBUG, "DONE fixed MTRRs\n");
390 /* enable fixed MTRR */
391 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
396 void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
397 /* this routine needs to know how many address bits a given processor
398 * supports. CPUs get grumpy when you set too many bits in
399 * their mtrr registers :( I would generically call cpuid here
400 * and find out how many physically supported but some cpus are
401 * buggy, and report more bits then they actually support.
402 * If above4gb flag is set, variable MTRR ranges must be used to
403 * set cacheability of DRAM above 4GB. If above4gb flag is clear,
404 * some other mechanism is controlling cacheability of DRAM above 4GB.
407 /* Try this the simple way of incrementally adding together
408 * mtrrs. If this doesn't work out we can get smart again
409 * and clear out the mtrrs.
411 struct var_mtrr_state var_state;
413 /* Cache as many memory areas as possible */
414 /* FIXME is there an algorithm for computing the optimal set of mtrrs?
415 * In some cases it is definitely possible to do better.
417 var_state.range_startk = 0;
418 var_state.range_sizek = 0;
419 var_state.hole_startk = 0;
420 var_state.hole_sizek = 0;
422 var_state.address_bits = address_bits;
423 var_state.above4gb = above4gb;
425 search_global_resources(
426 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
427 set_var_mtrr_resource, &var_state);
429 #if (CONFIG_GFXUMA == 1) /* UMA or SP. */
430 /* For now we assume the UMA space is at the end of memory below 4GB */
431 if (var_state.hole_startk || var_state.hole_sizek) {
432 printk(BIOS_DEBUG, "Warning: Can't set up MTRR hole for UMA due to pre-existing MTRR hole.\n");
434 #if CONFIG_VAR_MTRR_HOLE
435 // Increase the base range and set up UMA as an UC hole instead
436 var_state.range_sizek += (uma_memory_size >> 10);
438 var_state.hole_startk = (uma_memory_base >> 10);
439 var_state.hole_sizek = (uma_memory_size >> 10);
443 /* Write the last range */
444 var_state.reg = range_to_mtrr(var_state.reg, var_state.range_startk,
445 var_state.range_sizek, 0, MTRR_TYPE_WRBACK,
446 var_state.address_bits, var_state.above4gb);
447 #if CONFIG_VAR_MTRR_HOLE
448 var_state.reg = range_to_mtrr(var_state.reg, var_state.hole_startk,
449 var_state.hole_sizek, 0, MTRR_TYPE_UNCACHEABLE,
450 var_state.address_bits, var_state.above4gb);
452 printk(BIOS_DEBUG, "DONE variable MTRRs\n");
453 printk(BIOS_DEBUG, "Clear out the extra MTRR's\n");
454 /* Clear out the extra MTRR's */
455 while(var_state.reg < MTRRS) {
456 set_var_mtrr(var_state.reg++, 0, 0, 0, var_state.address_bits);
458 printk(BIOS_SPEW, "call enable_var_mtrr()\n");
460 printk(BIOS_SPEW, "Leave %s\n", __func__);
465 void x86_setup_mtrrs(unsigned address_bits)
467 x86_setup_fixed_mtrrs();
468 x86_setup_var_mtrrs(address_bits, 1);
472 int x86_mtrr_check(void)
474 /* Only Pentium Pro and later have MTRR */
476 printk(BIOS_DEBUG, "\nMTRR check\n");
481 printk(BIOS_DEBUG, "Fixed MTRRs : ");
483 printk(BIOS_DEBUG, "Enabled\n");
485 printk(BIOS_DEBUG, "Disabled\n");
487 printk(BIOS_DEBUG, "Variable MTRRs: ");
489 printk(BIOS_DEBUG, "Enabled\n");
491 printk(BIOS_DEBUG, "Disabled\n");
493 printk(BIOS_DEBUG, "\n");
496 return ((int) msr.lo);