blob: 8dccfefa042f1a43b8cbea6e1a394a541ae63e46 [file] [log] [blame]
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00001/*
Stefan Reinauercdc5cc62007-04-24 18:40:02 +00002 * mtrr.c: setting MTRR to decent values for cache initialization on P6
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00003 *
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 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000025
Yinghai Lu953e0f62005-01-06 04:55:19 +000026/*
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000027 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
Yinghai Lu953e0f62005-01-06 04:55:19 +000031*/
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000032
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000033#include <stddef.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000034#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>
Stefan Reinauer00093a82011-11-02 16:12:34 -070039#include <cpu/x86/lapic.h>
Sven Schnelleadfbcb792012-01-10 12:01:43 +010040#include <arch/cpu.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070041#include <arch/acpi.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000042
Myles Watsonec0ee642009-10-19 16:21:30 +000043#if CONFIG_GFXUMA
Stefan Reinauer7f86ed12009-02-12 16:02:16 +000044extern uint64_t uma_memory_base, uma_memory_size;
45#endif
46
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000047static 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
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000053void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000054{
55 msr_t msr;
56
57 msr = rdmsr(MTRRdefType_MSR);
58 msr.lo |= 0xc00;
59 wrmsr(MTRRdefType_MSR, msr);
60}
61
62static void enable_var_mtrr(void)
63{
64 msr_t msr;
65
66 msr = rdmsr(MTRRdefType_MSR);
Kevin O'Connor5bb9fd62011-01-19 06:32:35 +000067 msr.lo |= MTRRdefTypeEn;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000068 wrmsr(MTRRdefType_MSR, msr);
69}
70
71/* setting variable mtrr, comes from linux kernel source */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000072static void set_var_mtrr(
Stefan Reinauer14e22772010-04-27 06:56:47 +000073 unsigned int reg, unsigned long basek, unsigned long sizek,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000074 unsigned char type, unsigned address_bits)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000075{
76 msr_t base, mask;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000077 unsigned address_mask_high;
78
Yinghai Lud4b278c2006-10-04 20:46:15 +000079 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();
Stefan Reinauer14e22772010-04-27 06:56:47 +000086
Yinghai Lud4b278c2006-10-04 20:46:15 +000087 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
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000098 address_mask_high = ((1u << (address_bits - 32u)) - 1u);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000099
100 base.hi = basek >> 22;
101 base.lo = basek << 10;
102
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000103 printk(BIOS_SPEW, "ADDRESS_MASK_HIGH=%#x\n", address_mask_high);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000104
105 if (sizek < 4*1024*1024) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000106 mask.hi = address_mask_high;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000107 mask.lo = ~((sizek << 10) -1);
108 }
109 else {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000110 mask.hi = address_mask_high & (~((sizek >> 22) -1));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000111 mask.lo = 0;
112 }
113
Stefan Reinauer14e22772010-04-27 06:56:47 +0000114 // it is recommended that we disable and enable cache when we
115 // do this.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000116 disable_cache();
Yinghai Lud4b278c2006-10-04 20:46:15 +0000117
118 /* Bit 32-35 of MTRRphysMask should be set to 1 */
119 base.lo |= type;
Kevin O'Connor5bb9fd62011-01-19 06:32:35 +0000120 mask.lo |= MTRRphysMaskValid;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000121 wrmsr (MTRRphysBase_MSR(reg), base);
122 wrmsr (MTRRphysMask_MSR(reg), mask);
123
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000124 enable_cache();
125}
126
127/* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
128static 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
Marc Jones5cbdc1e2009-04-01 22:07:53 +0000139/* fls: find least sigificant bit set */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000140static 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 */
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000173#if 1
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000174#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
Duncan Laurie7389fa92011-12-22 10:59:40 -0800182static int total_mtrrs = MTRRS;
183static int bios_mtrrs = BIOS_MTRRS;
184
185static 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}
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000194
195static 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
229static 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
Stefan Reinauer14e22772010-04-27 06:56:47 +0000245static unsigned int range_to_mtrr(unsigned int reg,
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000246 unsigned long range_startk, unsigned long range_sizek,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000247 unsigned long next_range_startk, unsigned char type,
248 unsigned int address_bits, unsigned int above4gb)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000249{
Duncan Laurie7389fa92011-12-22 10:59:40 -0800250 unsigned long hole_startk = 0, hole_sizek = 0;
251
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000252 if (!range_sizek) {
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000253 /* If there's no MTRR hole, this function will bail out
254 * here when called for the hole.
255 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000256 printk(BIOS_SPEW, "Zero-sized MTRR range @%ldKB\n", range_startk);
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000257 return reg;
258 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000259
Duncan Laurie7389fa92011-12-22 10:59:40 -0800260 if (reg >= bios_mtrrs) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000261 printk(BIOS_ERR, "Warning: Out of MTRRs for base: %4ldMB, range: %ldMB, type %s\n",
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000262 range_startk >>10, range_sizek >> 10,
263 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
264 ((type==MTRR_TYPE_WRBACK)?"WB":"Other") );
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000265 return reg;
266 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000267
Duncan Laurie7389fa92011-12-22 10:59:40 -0800268 if (above4gb == 2 && type == MTRR_TYPE_WRBACK && range_sizek % 0x4000) {
269 /*
270 * If this range is not divisible by 16MB then instead
271 * make a larger range and carve out an uncached hole.
272 */
273 hole_startk = range_startk + range_sizek;
274 hole_sizek = 0x4000 - (range_sizek % 0x4000);
275 range_sizek += hole_sizek;
276 }
277
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000278 while(range_sizek) {
279 unsigned long max_align, align;
280 unsigned long sizek;
281 /* Compute the maximum size I can make a range */
282 max_align = fls(range_startk);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000283 align = fms(range_sizek);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000284 if (align > max_align) {
285 align = max_align;
286 }
287 sizek = 1 << align;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000288 printk(BIOS_DEBUG, "Setting variable MTRR %d, base: %4ldMB, range: %4ldMB, type %s\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000289 reg, range_startk >>10, sizek >> 10,
290 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
291 ((type==MTRR_TYPE_WRBACK)?"WB":"Other")
Yinghai Lu63601872005-01-27 22:48:12 +0000292 );
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000293
294 /* if range is above 4GB, MTRR is needed
295 * only if above4gb flag is set
296 */
297 if (range_startk < 0x100000000ull / 1024 || above4gb)
298 set_var_mtrr(reg++, range_startk, sizek, type, address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000299 range_startk += sizek;
300 range_sizek -= sizek;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800301 if (reg >= bios_mtrrs) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000302 printk(BIOS_ERR, "Running out of variable MTRRs!\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000303 break;
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000304 }
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000305 }
Duncan Laurie7389fa92011-12-22 10:59:40 -0800306
307 if (hole_sizek) {
308 printk(BIOS_DEBUG, "Adding hole at %ldMB-%ldMB\n",
309 hole_startk, hole_startk + hole_sizek);
310 reg = range_to_mtrr(reg, hole_startk, hole_sizek,
311 next_range_startk, MTRR_TYPE_UNCACHEABLE,
312 address_bits, above4gb);
313 }
314
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000315 return reg;
316}
317
Stefan Reinauer14e22772010-04-27 06:56:47 +0000318static unsigned long resk(uint64_t value)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000319{
320 unsigned long resultk;
321 if (value < (1ULL << 42)) {
322 resultk = value >> 10;
323 }
324 else {
325 resultk = 0xffffffff;
326 }
327 return resultk;
328}
329
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000330static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
331{
332 unsigned int start_mtrr;
333 unsigned int last_mtrr;
334 start_mtrr = fixed_mtrr_index(resk(res->base));
335 last_mtrr = fixed_mtrr_index(resk((res->base + res->size)));
336 if (start_mtrr >= NUM_FIXED_RANGES) {
337 return;
338 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000339 printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: WB\n",
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000340 start_mtrr, last_mtrr);
341 set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000342
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000343}
344
Yinghai Lu21332b82007-04-06 19:49:05 +0000345#ifndef CONFIG_VAR_MTRR_HOLE
346#define CONFIG_VAR_MTRR_HOLE 1
347#endif
348
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000349struct var_mtrr_state {
350 unsigned long range_startk, range_sizek;
351 unsigned int reg;
Yinghai Lu63601872005-01-27 22:48:12 +0000352 unsigned long hole_startk, hole_sizek;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000353 unsigned int address_bits;
354 unsigned int above4gb; /* Set if MTRRs are needed for DRAM above 4GB */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000355};
356
357void set_var_mtrr_resource(void *gp, struct device *dev, struct resource *res)
358{
359 struct var_mtrr_state *state = gp;
360 unsigned long basek, sizek;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800361 if (state->reg >= bios_mtrrs)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000362 return;
363 basek = resk(res->base);
364 sizek = resk(res->size);
365 /* See if I can merge with the last range
366 * Either I am below 1M and the fixed mtrrs handle it, or
367 * the ranges touch.
368 */
369 if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
370 unsigned long endk = basek + sizek;
371 state->range_sizek = endk - state->range_startk;
372 return;
373 }
374 /* Write the range mtrrs */
375 if (state->range_sizek != 0) {
Yinghai Lu21332b82007-04-06 19:49:05 +0000376#if CONFIG_VAR_MTRR_HOLE
Duncan Laurie7389fa92011-12-22 10:59:40 -0800377 if (state->hole_sizek == 0 && state->above4gb != 2) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000378 /* We need to put that on to hole */
379 unsigned long endk = basek + sizek;
Yinghai Lu63601872005-01-27 22:48:12 +0000380 state->hole_startk = state->range_startk + state->range_sizek;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000381 state->hole_sizek = basek - state->hole_startk;
382 state->range_sizek = endk - state->range_startk;
Yinghai Lu63601872005-01-27 22:48:12 +0000383 return;
384 }
Yinghai Lu21332b82007-04-06 19:49:05 +0000385#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +0000386 state->reg = range_to_mtrr(state->reg, state->range_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000387 state->range_sizek, basek, MTRR_TYPE_WRBACK,
388 state->address_bits, state->above4gb);
Yinghai Lu21332b82007-04-06 19:49:05 +0000389#if CONFIG_VAR_MTRR_HOLE
Stefan Reinauer14e22772010-04-27 06:56:47 +0000390 state->reg = range_to_mtrr(state->reg, state->hole_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000391 state->hole_sizek, basek, MTRR_TYPE_UNCACHEABLE,
392 state->address_bits, state->above4gb);
Yinghai Lu21332b82007-04-06 19:49:05 +0000393#endif
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000394 state->range_startk = 0;
395 state->range_sizek = 0;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000396 state->hole_startk = 0;
397 state->hole_sizek = 0;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000398 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000399 /* Allocate an msr */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000400 printk(BIOS_SPEW, " Allocate an msr - basek = %08lx, sizek = %08lx,\n", basek, sizek);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000401 state->range_startk = basek;
402 state->range_sizek = sizek;
403}
404
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000405void x86_setup_fixed_mtrrs(void)
406{
407 /* Try this the simple way of incrementally adding together
Stefan Reinauer14e22772010-04-27 06:56:47 +0000408 * mtrrs. If this doesn't work out we can get smart again
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000409 * and clear out the mtrrs.
410 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000411
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000412 printk(BIOS_DEBUG, "\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000413 /* Initialized the fixed_mtrrs to uncached */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000414 printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: UC\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000415 0, NUM_FIXED_RANGES);
416 set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
417
418 /* Now see which of the fixed mtrrs cover ram.
419 */
420 search_global_resources(
421 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
422 set_fixed_mtrr_resource, NULL);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000423 printk(BIOS_DEBUG, "DONE fixed MTRRs\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000424
425 /* enable fixed MTRR */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000426 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000427 enable_fixed_mtrr();
428
429}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000430
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000431void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000432/* this routine needs to know how many address bits a given processor
Stefan Reinauer14e22772010-04-27 06:56:47 +0000433 * supports. CPUs get grumpy when you set too many bits in
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000434 * their mtrr registers :( I would generically call cpuid here
435 * and find out how many physically supported but some cpus are
436 * buggy, and report more bits then they actually support.
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000437 * If above4gb flag is set, variable MTRR ranges must be used to
438 * set cacheability of DRAM above 4GB. If above4gb flag is clear,
439 * some other mechanism is controlling cacheability of DRAM above 4GB.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000440 */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000441{
442 /* Try this the simple way of incrementally adding together
Stefan Reinauer14e22772010-04-27 06:56:47 +0000443 * mtrrs. If this doesn't work out we can get smart again
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000444 * and clear out the mtrrs.
445 */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000446 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000447
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000448 /* Cache as many memory areas as possible */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000449 /* FIXME is there an algorithm for computing the optimal set of mtrrs?
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000450 * In some cases it is definitely possible to do better.
451 */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000452 var_state.range_startk = 0;
453 var_state.range_sizek = 0;
Yinghai Lu63601872005-01-27 22:48:12 +0000454 var_state.hole_startk = 0;
455 var_state.hole_sizek = 0;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000456 var_state.reg = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000457 var_state.address_bits = address_bits;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000458 var_state.above4gb = above4gb;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000459
Duncan Laurie7389fa92011-12-22 10:59:40 -0800460 /* Detect number of variable MTRRs */
461 if (above4gb == 2)
462 detect_var_mtrrs();
463
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000464 search_global_resources(
465 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
466 set_var_mtrr_resource, &var_state);
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000467
Zheng Baoedee9eb2009-08-11 03:18:11 +0000468#if (CONFIG_GFXUMA == 1) /* UMA or SP. */
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000469 /* For now we assume the UMA space is at the end of memory below 4GB */
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000470 if (var_state.hole_startk || var_state.hole_sizek) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000471 printk(BIOS_DEBUG, "Warning: Can't set up MTRR hole for UMA due to pre-existing MTRR hole.\n");
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000472 } else {
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000473#if CONFIG_VAR_MTRR_HOLE
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000474 // Increase the base range and set up UMA as an UC hole instead
Duncan Laurie7389fa92011-12-22 10:59:40 -0800475 if (above4gb != 2)
476 var_state.range_sizek += (uma_memory_size >> 10);
Yinghai Lu953e0f62005-01-06 04:55:19 +0000477
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000478 var_state.hole_startk = (uma_memory_base >> 10);
479 var_state.hole_sizek = (uma_memory_size >> 10);
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000480#endif
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000481 }
482#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000483 /* Write the last range */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000484 var_state.reg = range_to_mtrr(var_state.reg, var_state.range_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000485 var_state.range_sizek, 0, MTRR_TYPE_WRBACK,
486 var_state.address_bits, var_state.above4gb);
Yinghai Lu21332b82007-04-06 19:49:05 +0000487#if CONFIG_VAR_MTRR_HOLE
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000488 var_state.reg = range_to_mtrr(var_state.reg, var_state.hole_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000489 var_state.hole_sizek, 0, MTRR_TYPE_UNCACHEABLE,
490 var_state.address_bits, var_state.above4gb);
Yinghai Lu21332b82007-04-06 19:49:05 +0000491#endif
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000492 printk(BIOS_DEBUG, "DONE variable MTRRs\n");
493 printk(BIOS_DEBUG, "Clear out the extra MTRR's\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000494 /* Clear out the extra MTRR's */
Duncan Laurie7389fa92011-12-22 10:59:40 -0800495 while(var_state.reg < total_mtrrs) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000496 set_var_mtrr(var_state.reg++, 0, 0, 0, var_state.address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000497 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700498
499#if CONFIG_CACHE_ROM
500 /* Enable Caching and speculative Reads for the
501 * complete ROM now that we actually have RAM.
502 */
503 if (boot_cpu() && (acpi_slp_type != 3)) {
Duncan Laurie7389fa92011-12-22 10:59:40 -0800504 set_var_mtrr(total_mtrrs-1, (4096-4)*1024, 4*1024,
Stefan Reinauer00093a82011-11-02 16:12:34 -0700505 MTRR_TYPE_WRPROT, address_bits);
506 }
507#endif
508
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000509 printk(BIOS_SPEW, "call enable_var_mtrr()\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000510 enable_var_mtrr();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000511 printk(BIOS_SPEW, "Leave %s\n", __func__);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000512 post_code(0x6A);
513}
514
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000515
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100516void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000517{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100518 int address_size;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000519 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100520 address_size = cpu_phys_address_size();
521 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size);
522 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000523}
524
525
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000526int x86_mtrr_check(void)
527{
528 /* Only Pentium Pro and later have MTRR */
529 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000530 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000531
532 msr = rdmsr(0x2ff);
533 msr.lo >>= 10;
534
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000535 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000536 if (msr.lo & 0x01)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000537 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000538 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000539 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000540
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000541 printk(BIOS_DEBUG, "Variable MTRRs: ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000542 if (msr.lo & 0x02)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000543 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000544 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000545 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000546
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000547 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000548
549 post_code(0x93);
550 return ((int) msr.lo);
551}