blob: 77fdb6f5b3b2a75db9050d84a1213eca0df17ca6 [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
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000043static 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
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070049/* 2 MTRRS are reserved for the operating system */
50#define BIOS_MTRRS 6
51#define OS_MTRRS 2
52#define MTRRS (BIOS_MTRRS + OS_MTRRS)
53
54static int total_mtrrs = MTRRS;
55static int bios_mtrrs = BIOS_MTRRS;
56
57static void detect_var_mtrrs(void)
58{
59 msr_t msr;
60
61 msr = rdmsr(MTRRcap_MSR);
62
63 total_mtrrs = msr.lo & 0xff;
64 bios_mtrrs = total_mtrrs - OS_MTRRS;
65}
66
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000067void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000068{
69 msr_t msr;
70
71 msr = rdmsr(MTRRdefType_MSR);
72 msr.lo |= 0xc00;
73 wrmsr(MTRRdefType_MSR, msr);
74}
75
76static void enable_var_mtrr(void)
77{
78 msr_t msr;
79
80 msr = rdmsr(MTRRdefType_MSR);
Kevin O'Connor5bb9fd62011-01-19 06:32:35 +000081 msr.lo |= MTRRdefTypeEn;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000082 wrmsr(MTRRdefType_MSR, msr);
83}
84
85/* setting variable mtrr, comes from linux kernel source */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000086static void set_var_mtrr(
Stefan Reinauer14e22772010-04-27 06:56:47 +000087 unsigned int reg, unsigned long basek, unsigned long sizek,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000088 unsigned char type, unsigned address_bits)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000089{
90 msr_t base, mask;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000091 unsigned address_mask_high;
92
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070093 if (reg >= total_mtrrs)
94 return;
Yinghai Lud4b278c2006-10-04 20:46:15 +000095
96 // it is recommended that we disable and enable cache when we
97 // do this.
98 if (sizek == 0) {
99 disable_cache();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000100
Yinghai Lud4b278c2006-10-04 20:46:15 +0000101 msr_t zero;
102 zero.lo = zero.hi = 0;
103 /* The invalid bit is kept in the mask, so we simply clear the
104 relevant mask register to disable a range. */
105 wrmsr (MTRRphysMask_MSR(reg), zero);
106
107 enable_cache();
108 return;
109 }
110
111
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000112 address_mask_high = ((1u << (address_bits - 32u)) - 1u);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000113
114 base.hi = basek >> 22;
115 base.lo = basek << 10;
116
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000117 printk(BIOS_SPEW, "ADDRESS_MASK_HIGH=%#x\n", address_mask_high);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000118
119 if (sizek < 4*1024*1024) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000120 mask.hi = address_mask_high;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000121 mask.lo = ~((sizek << 10) -1);
122 }
123 else {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000124 mask.hi = address_mask_high & (~((sizek >> 22) -1));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000125 mask.lo = 0;
126 }
127
Stefan Reinauer14e22772010-04-27 06:56:47 +0000128 // it is recommended that we disable and enable cache when we
129 // do this.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000130 disable_cache();
Yinghai Lud4b278c2006-10-04 20:46:15 +0000131
132 /* Bit 32-35 of MTRRphysMask should be set to 1 */
133 base.lo |= type;
Kevin O'Connor5bb9fd62011-01-19 06:32:35 +0000134 mask.lo |= MTRRphysMaskValid;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000135 wrmsr (MTRRphysBase_MSR(reg), base);
136 wrmsr (MTRRphysMask_MSR(reg), mask);
137
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000138 enable_cache();
Denis 'GNUtoo' Carikli7c2d0582012-05-26 00:13:22 +0200139
140 printk(BIOS_DEBUG, "Setting variable MTRR %d, base: %4ldMB, range: %4ldMB, type %s\n",
141 reg, basek >>10, sizek >> 10,
142 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
143 ((type==MTRR_TYPE_WRBACK)?"WB":"Other")
144 );
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000145}
146
147/* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
148static inline unsigned int fms(unsigned int x)
149{
150 int r;
151
152 __asm__("bsrl %1,%0\n\t"
153 "jnz 1f\n\t"
154 "movl $0,%0\n"
155 "1:" : "=r" (r) : "g" (x));
156 return r;
157}
158
Marc Jones5cbdc1e2009-04-01 22:07:53 +0000159/* fls: find least sigificant bit set */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000160static inline unsigned int fls(unsigned int x)
161{
162 int r;
163
164 __asm__("bsfl %1,%0\n\t"
165 "jnz 1f\n\t"
166 "movl $32,%0\n"
167 "1:" : "=r" (r) : "g" (x));
168 return r;
169}
170
171/* setting up variable and fixed mtrr
172 *
173 * From Intel Vol. III Section 9.12.4, the Range Size and Base Alignment has some kind of requirement:
174 * 1. The range size must be 2^N byte for N >= 12 (i.e 4KB minimum).
175 * 2. The base address must be 2^N aligned, where the N here is equal to the N in previous
176 * requirement. So a 8K range must be 8K aligned not 4K aligned.
177 *
178 * These requirement is meet by "decompositing" the ramsize into Sum(Cn * 2^n, n = [0..N], Cn = [0, 1]).
179 * For Cm = 1, there is a WB range of 2^m size at base address Sum(Cm * 2^m, m = [N..n]).
180 * A 124MB (128MB - 4MB SMA) example:
181 * ramsize = 124MB == 64MB (at 0MB) + 32MB (at 64MB) + 16MB (at 96MB ) + 8MB (at 112MB) + 4MB (120MB).
182 * But this wastes a lot of MTRR registers so we use another more "aggresive" way with Uncacheable Regions.
183 *
184 * In the Uncacheable Region scheme, we try to cover the whole ramsize by one WB region as possible,
185 * If (an only if) this can not be done we will try to decomposite the ramesize, the mathematical formula
186 * whould be ramsize = Sum(Cn * 2^n, n = [0..N], Cn = [-1, 0, 1]). For Cn = -1, a Uncachable Region is used.
187 * The same 124MB example:
188 * ramsize = 124MB == 128MB WB (at 0MB) + 4MB UC (at 124MB)
189 * or a 156MB (128MB + 32MB - 4MB SMA) example:
190 * ramsize = 156MB == 128MB WB (at 0MB) + 32MB WB (at 128MB) + 4MB UC (at 156MB)
191 */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000192
193static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
194{
195 unsigned int i;
196 unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
197 msr_t msr;
198 msr.lo = msr.hi = 0; /* Shut up gcc */
199 for(i = first; i < last; i++) {
200 /* When I switch to a new msr read it in */
201 if (fixed_msr != i >> 3) {
202 /* But first write out the old msr */
203 if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
204 disable_cache();
205 wrmsr(mtrr_msr[fixed_msr], msr);
206 enable_cache();
207 }
208 fixed_msr = i>>3;
209 msr = rdmsr(mtrr_msr[fixed_msr]);
210 }
211 if ((i & 7) < 4) {
212 msr.lo &= ~(0xff << ((i&3)*8));
213 msr.lo |= type << ((i&3)*8);
214 } else {
215 msr.hi &= ~(0xff << ((i&3)*8));
216 msr.hi |= type << ((i&3)*8);
217 }
218 }
219 /* Write out the final msr */
220 if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
221 disable_cache();
222 wrmsr(mtrr_msr[fixed_msr], msr);
223 enable_cache();
224 }
225}
226
227static unsigned fixed_mtrr_index(unsigned long addrk)
228{
229 unsigned index;
230 index = (addrk - 0) >> 6;
231 if (index >= 8) {
232 index = ((addrk - 8*64) >> 4) + 8;
233 }
234 if (index >= 24) {
235 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
236 }
237 if (index > NUM_FIXED_RANGES) {
238 index = NUM_FIXED_RANGES;
239 }
240 return index;
241}
242
Stefan Reinauer14e22772010-04-27 06:56:47 +0000243static unsigned int range_to_mtrr(unsigned int reg,
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000244 unsigned long range_startk, unsigned long range_sizek,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000245 unsigned long next_range_startk, unsigned char type,
246 unsigned int address_bits, unsigned int above4gb)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000247{
Duncan Laurie7389fa92011-12-22 10:59:40 -0800248 unsigned long hole_startk = 0, hole_sizek = 0;
249
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000250 if (!range_sizek) {
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000251 /* If there's no MTRR hole, this function will bail out
252 * here when called for the hole.
253 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000254 printk(BIOS_SPEW, "Zero-sized MTRR range @%ldKB\n", range_startk);
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000255 return reg;
256 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000257
Duncan Laurie7389fa92011-12-22 10:59:40 -0800258 if (reg >= bios_mtrrs) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000259 printk(BIOS_ERR, "Warning: Out of MTRRs for base: %4ldMB, range: %ldMB, type %s\n",
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000260 range_startk >>10, range_sizek >> 10,
261 (type==MTRR_TYPE_UNCACHEABLE)?"UC":
262 ((type==MTRR_TYPE_WRBACK)?"WB":"Other") );
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000263 return reg;
264 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000265
Duncan Laurie7b678922012-01-09 22:05:18 -0800266#define MIN_ALIGN 0x10000 /* 64MB */
267
268 if (above4gb == 2 && type == MTRR_TYPE_WRBACK &&
269 range_sizek > MIN_ALIGN && range_sizek % MIN_ALIGN) {
Duncan Laurie7389fa92011-12-22 10:59:40 -0800270 /*
Duncan Laurie7b678922012-01-09 22:05:18 -0800271 * If this range is not divisible then instead
Duncan Laurie7389fa92011-12-22 10:59:40 -0800272 * make a larger range and carve out an uncached hole.
273 */
274 hole_startk = range_startk + range_sizek;
Duncan Laurie7b678922012-01-09 22:05:18 -0800275 hole_sizek = MIN_ALIGN - (range_sizek % MIN_ALIGN);
Duncan Laurie7389fa92011-12-22 10:59:40 -0800276 range_sizek += hole_sizek;
277 }
278
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000279 while(range_sizek) {
280 unsigned long max_align, align;
281 unsigned long sizek;
282 /* Compute the maximum size I can make a range */
283 max_align = fls(range_startk);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000284 align = fms(range_sizek);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000285 if (align > max_align) {
286 align = max_align;
287 }
288 sizek = 1 << align;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000289
290 /* if range is above 4GB, MTRR is needed
291 * only if above4gb flag is set
292 */
293 if (range_startk < 0x100000000ull / 1024 || above4gb)
294 set_var_mtrr(reg++, range_startk, sizek, type, address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000295 range_startk += sizek;
296 range_sizek -= sizek;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800297 if (reg >= bios_mtrrs) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000298 printk(BIOS_ERR, "Running out of variable MTRRs!\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000299 break;
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000300 }
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000301 }
Duncan Laurie7389fa92011-12-22 10:59:40 -0800302
303 if (hole_sizek) {
304 printk(BIOS_DEBUG, "Adding hole at %ldMB-%ldMB\n",
Duncan Laurie527fc742012-01-06 15:49:30 -0800305 hole_startk >> 10, (hole_startk + hole_sizek) >> 10);
Duncan Laurie7389fa92011-12-22 10:59:40 -0800306 reg = range_to_mtrr(reg, hole_startk, hole_sizek,
307 next_range_startk, MTRR_TYPE_UNCACHEABLE,
308 address_bits, above4gb);
309 }
310
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000311 return reg;
312}
313
Stefan Reinauer14e22772010-04-27 06:56:47 +0000314static unsigned long resk(uint64_t value)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000315{
316 unsigned long resultk;
317 if (value < (1ULL << 42)) {
318 resultk = value >> 10;
319 }
320 else {
321 resultk = 0xffffffff;
322 }
323 return resultk;
324}
325
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000326static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
327{
328 unsigned int start_mtrr;
329 unsigned int last_mtrr;
330 start_mtrr = fixed_mtrr_index(resk(res->base));
331 last_mtrr = fixed_mtrr_index(resk((res->base + res->size)));
332 if (start_mtrr >= NUM_FIXED_RANGES) {
333 return;
334 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000335 printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: WB\n",
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000336 start_mtrr, last_mtrr);
337 set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000338
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000339}
340
341struct var_mtrr_state {
342 unsigned long range_startk, range_sizek;
343 unsigned int reg;
Yinghai Lu63601872005-01-27 22:48:12 +0000344 unsigned long hole_startk, hole_sizek;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000345 unsigned int address_bits;
346 unsigned int above4gb; /* Set if MTRRs are needed for DRAM above 4GB */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000347};
348
349void set_var_mtrr_resource(void *gp, struct device *dev, struct resource *res)
350{
351 struct var_mtrr_state *state = gp;
352 unsigned long basek, sizek;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800353 if (state->reg >= bios_mtrrs)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000354 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300355
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000356 basek = resk(res->base);
357 sizek = resk(res->size);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300358
359 if (res->flags & IORESOURCE_UMA_FB) {
360 /* FIXME: could I use Write-Combining for Frame Buffer ? */
361 state->reg = range_to_mtrr(state->reg, basek, sizek, 0,
362 MTRR_TYPE_UNCACHEABLE, state->address_bits, state->above4gb);
363 return;
364 }
365
366 if (!(res->flags & IORESOURCE_CACHEABLE))
367 return;
368
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000369 /* See if I can merge with the last range
370 * Either I am below 1M and the fixed mtrrs handle it, or
371 * the ranges touch.
372 */
373 if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
374 unsigned long endk = basek + sizek;
375 state->range_sizek = endk - state->range_startk;
376 return;
377 }
378 /* Write the range mtrrs */
379 if (state->range_sizek != 0) {
Duncan Laurie7389fa92011-12-22 10:59:40 -0800380 if (state->hole_sizek == 0 && state->above4gb != 2) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000381 /* We need to put that on to hole */
382 unsigned long endk = basek + sizek;
Yinghai Lu63601872005-01-27 22:48:12 +0000383 state->hole_startk = state->range_startk + state->range_sizek;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000384 state->hole_sizek = basek - state->hole_startk;
385 state->range_sizek = endk - state->range_startk;
Yinghai Lu63601872005-01-27 22:48:12 +0000386 return;
387 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000388 state->reg = range_to_mtrr(state->reg, state->range_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000389 state->range_sizek, basek, MTRR_TYPE_WRBACK,
390 state->address_bits, state->above4gb);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300391
Stefan Reinauer14e22772010-04-27 06:56:47 +0000392 state->reg = range_to_mtrr(state->reg, state->hole_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000393 state->hole_sizek, basek, MTRR_TYPE_UNCACHEABLE,
394 state->address_bits, state->above4gb);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300395
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000396 state->range_startk = 0;
397 state->range_sizek = 0;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000398 state->hole_startk = 0;
399 state->hole_sizek = 0;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000400 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000401 /* Allocate an msr */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000402 printk(BIOS_SPEW, " Allocate an msr - basek = %08lx, sizek = %08lx,\n", basek, sizek);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000403 state->range_startk = basek;
404 state->range_sizek = sizek;
405}
406
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000407void x86_setup_fixed_mtrrs(void)
408{
409 /* Try this the simple way of incrementally adding together
Stefan Reinauer14e22772010-04-27 06:56:47 +0000410 * mtrrs. If this doesn't work out we can get smart again
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000411 * and clear out the mtrrs.
412 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000413
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000414 printk(BIOS_DEBUG, "\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000415 /* Initialized the fixed_mtrrs to uncached */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000416 printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: UC\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000417 0, NUM_FIXED_RANGES);
418 set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
419
420 /* Now see which of the fixed mtrrs cover ram.
421 */
422 search_global_resources(
423 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
424 set_fixed_mtrr_resource, NULL);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000425 printk(BIOS_DEBUG, "DONE fixed MTRRs\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000426
427 /* enable fixed MTRR */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000428 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000429 enable_fixed_mtrr();
430
431}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000432
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000433void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000434/* this routine needs to know how many address bits a given processor
Stefan Reinauer14e22772010-04-27 06:56:47 +0000435 * supports. CPUs get grumpy when you set too many bits in
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000436 * their mtrr registers :( I would generically call cpuid here
437 * and find out how many physically supported but some cpus are
438 * buggy, and report more bits then they actually support.
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000439 * If above4gb flag is set, variable MTRR ranges must be used to
440 * set cacheability of DRAM above 4GB. If above4gb flag is clear,
441 * some other mechanism is controlling cacheability of DRAM above 4GB.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000442 */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000443{
444 /* Try this the simple way of incrementally adding together
Stefan Reinauer14e22772010-04-27 06:56:47 +0000445 * mtrrs. If this doesn't work out we can get smart again
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000446 * and clear out the mtrrs.
447 */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000448 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000449
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000450 /* Cache as many memory areas as possible */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000451 /* FIXME is there an algorithm for computing the optimal set of mtrrs?
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000452 * In some cases it is definitely possible to do better.
453 */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000454 var_state.range_startk = 0;
455 var_state.range_sizek = 0;
Yinghai Lu63601872005-01-27 22:48:12 +0000456 var_state.hole_startk = 0;
457 var_state.hole_sizek = 0;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000458 var_state.reg = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000459 var_state.address_bits = address_bits;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000460 var_state.above4gb = above4gb;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000461
Duncan Laurie7389fa92011-12-22 10:59:40 -0800462 /* Detect number of variable MTRRs */
463 if (above4gb == 2)
464 detect_var_mtrrs();
465
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300466 search_global_resources(IORESOURCE_MEM, IORESOURCE_MEM,
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000467 set_var_mtrr_resource, &var_state);
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000468
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000469 /* Write the last range */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000470 var_state.reg = range_to_mtrr(var_state.reg, var_state.range_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000471 var_state.range_sizek, 0, MTRR_TYPE_WRBACK,
472 var_state.address_bits, var_state.above4gb);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300473
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000474 var_state.reg = range_to_mtrr(var_state.reg, var_state.hole_startk,
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000475 var_state.hole_sizek, 0, MTRR_TYPE_UNCACHEABLE,
476 var_state.address_bits, var_state.above4gb);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300477
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000478 printk(BIOS_DEBUG, "DONE variable MTRRs\n");
479 printk(BIOS_DEBUG, "Clear out the extra MTRR's\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000480 /* Clear out the extra MTRR's */
Duncan Laurie7389fa92011-12-22 10:59:40 -0800481 while(var_state.reg < total_mtrrs) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000482 set_var_mtrr(var_state.reg++, 0, 0, 0, var_state.address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000483 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700484
485#if CONFIG_CACHE_ROM
486 /* Enable Caching and speculative Reads for the
487 * complete ROM now that we actually have RAM.
488 */
489 if (boot_cpu() && (acpi_slp_type != 3)) {
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -0700490 set_var_mtrr(total_mtrrs - 1, (4096 - 8)*1024, 8 * 1024,
Stefan Reinauer00093a82011-11-02 16:12:34 -0700491 MTRR_TYPE_WRPROT, address_bits);
492 }
493#endif
494
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000495 printk(BIOS_SPEW, "call enable_var_mtrr()\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000496 enable_var_mtrr();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000497 printk(BIOS_SPEW, "Leave %s\n", __func__);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000498 post_code(0x6A);
499}
500
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000501
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100502void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000503{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100504 int address_size;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000505 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100506 address_size = cpu_phys_address_size();
507 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size);
508 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000509}
510
511
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000512int x86_mtrr_check(void)
513{
514 /* Only Pentium Pro and later have MTRR */
515 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000516 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000517
518 msr = rdmsr(0x2ff);
519 msr.lo >>= 10;
520
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000521 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000522 if (msr.lo & 0x01)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000523 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000524 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000525 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000526
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000527 printk(BIOS_DEBUG, "Variable MTRRs: ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000528 if (msr.lo & 0x02)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000529 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000530 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000531 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000532
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000533 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000534
535 post_code(0x93);
536 return ((int) msr.lo);
537}