blob: 81ea6da91d1575972ff8f40e9fbf944eb9d153e8 [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
Aaron Durbinbb4e79a2013-03-26 14:09:47 -05007 * Copyright 2013 Google Inc.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *
24 * Reference: Intel Architecture Software Developer's Manual, Volume 3: System Programming
25 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000026
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000027#include <stddef.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050028#include <stdlib.h>
29#include <string.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000030#include <console/console.h>
31#include <device/device.h>
32#include <cpu/x86/msr.h>
33#include <cpu/x86/mtrr.h>
34#include <cpu/x86/cache.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070035#include <cpu/x86/lapic.h>
Sven Schnelleadfbcb792012-01-10 12:01:43 +010036#include <arch/cpu.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070037#include <arch/acpi.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050038#include <memrange.h>
Aaron Durbin57686f82013-03-20 15:50:59 -050039#if CONFIG_X86_AMD_FIXED_MTRRS
40#include <cpu/amd/mtrr.h>
41#define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
42#else
43#define MTRR_FIXED_WRBACK_BITS 0
44#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000045
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070046/* 2 MTRRS are reserved for the operating system */
47#define BIOS_MTRRS 6
48#define OS_MTRRS 2
49#define MTRRS (BIOS_MTRRS + OS_MTRRS)
50
51static int total_mtrrs = MTRRS;
52static int bios_mtrrs = BIOS_MTRRS;
53
54static void detect_var_mtrrs(void)
55{
56 msr_t msr;
57
58 msr = rdmsr(MTRRcap_MSR);
59
60 total_mtrrs = msr.lo & 0xff;
61 bios_mtrrs = total_mtrrs - OS_MTRRS;
62}
63
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000064void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000065{
66 msr_t msr;
67
68 msr = rdmsr(MTRRdefType_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050069 msr.lo |= MTRRdefTypeEn | MTRRdefTypeFixEn;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000070 wrmsr(MTRRdefType_MSR, msr);
71}
72
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050073static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000074{
75 msr_t msr;
76
77 msr = rdmsr(MTRRdefType_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050078 msr.lo &= ~0xff;
79 msr.lo |= MTRRdefTypeEn | deftype;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000080 wrmsr(MTRRdefType_MSR, msr);
81}
82
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000083/* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
84static inline unsigned int fms(unsigned int x)
85{
86 int r;
87
88 __asm__("bsrl %1,%0\n\t"
89 "jnz 1f\n\t"
90 "movl $0,%0\n"
91 "1:" : "=r" (r) : "g" (x));
92 return r;
93}
94
Marc Jones5cbdc1e2009-04-01 22:07:53 +000095/* fls: find least sigificant bit set */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000096static inline unsigned int fls(unsigned int x)
97{
98 int r;
99
100 __asm__("bsfl %1,%0\n\t"
101 "jnz 1f\n\t"
102 "movl $32,%0\n"
103 "1:" : "=r" (r) : "g" (x));
104 return r;
105}
106
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500107#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000108
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500109/* MTRRs are at a 4KiB granularity. Therefore all address calculations can
110 * be done with 32-bit numbers. This allows for the MTRR code to handle
111 * up to 2^44 bytes (16 TiB) of address space. */
112#define RANGE_SHIFT 12
113#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
114 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
115#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
116#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
117#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
118
119/* The minimum alignment while handling variable MTRR ranges is 64MiB. */
120#define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20)
121/* Helpful constants. */
122#define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20)
123#define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
124
125static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000126{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500127 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000128}
129
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500130static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000131{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500132 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000133}
134
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500135static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000136{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500137 static struct memranges *addr_space;
138 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800139
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500140 /* In order to handle some chipsets not being able to pre-determine
141 * uncacheable ranges, such as graphics memory, at resource inseration
142 * time remove unacheable regions from the cacheable ones. */
143 if (addr_space == NULL) {
144 struct range_entry *r;
145 const unsigned long mask = IORESOURCE_CACHEABLE;
146
147 addr_space = &addr_space_storage;
148
149 /* Collect cacheable and uncacheable address ranges. The
150 * uncacheable regions take precedence over the cacheable
151 * regions. */
152 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
153 memranges_add_resources(addr_space, mask, 0,
154 MTRR_TYPE_UNCACHEABLE);
155
156 /* The address space below 4GiB is special. It needs to be
157 * covered entirly by range entries so that MTRR calculations
158 * can be properly done for the full 32-bit address space.
159 * Therefore, ensure holes are filled up to 4GiB as
160 * uncacheable */
161 memranges_fill_holes_up_to(addr_space,
162 RANGE_TO_PHYS_ADDR(RANGE_4GB),
163 MTRR_TYPE_UNCACHEABLE);
164
165 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
166 memranges_each_entry(r, addr_space)
167 printk(BIOS_DEBUG,
168 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
169 range_entry_base(r), range_entry_end(r),
170 range_entry_size(r), range_entry_tag(r));
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000171 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000172
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500173 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000174}
175
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500176/* Fixed MTRR descriptor. This structure defines the step size and begin
177 * and end (exclusive) address covered by a set of fixe MTRR MSRs.
178 * It also describes the offset in byte intervals to store the calculated MTRR
179 * type in an array. */
180struct fixed_mtrr_desc {
181 uint32_t begin;
182 uint32_t end;
183 uint32_t step;
184 int range_index;
185 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000186};
187
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500188/* Shared MTRR calculations. Can be reused by APs. */
189static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
190
191/* Fixed MTRR descriptors. */
192static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
193 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
194 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRRfix64K_00000_MSR },
195 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
196 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRRfix16K_80000_MSR },
197 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
198 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRRfix4K_C0000_MSR },
199};
200
201static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000202{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500203 static int fixed_mtrr_types_initialized;
204 struct memranges *phys_addr_space;
205 struct range_entry *r;
206 const struct fixed_mtrr_desc *desc;
207 const struct fixed_mtrr_desc *last_desc;
208 uint32_t begin;
209 uint32_t end;
210 int type_index;
211
212 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000213 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300214
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500215 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300216
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500217 /* Set all fixed ranges to uncacheable first. */
218 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300219
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500220 desc = &fixed_mtrr_desc[0];
221 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
222 type_index = desc->range_index;
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300223
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500224 memranges_each_entry(r, phys_addr_space) {
225 begin = range_entry_base_mtrr_addr(r);
226 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300227
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500228 if (begin >= last_desc->end)
229 break;
230
231 if (end > last_desc->end)
232 end = last_desc->end;
233
234 /* Get to the correct fixed mtrr descriptor. */
235 while (begin >= desc->end)
236 desc++;
237
238 type_index = desc->range_index;
239 type_index += (begin - desc->begin) / desc->step;
240
241 while (begin != end) {
242 unsigned char type;
243
244 type = range_entry_tag(r);
245 printk(MTRR_VERBOSE_LEVEL,
246 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
247 begin, begin + desc->step, type, type_index);
248 if (type == MTRR_TYPE_WRBACK)
249 type |= MTRR_FIXED_WRBACK_BITS;
250 fixed_mtrr_types[type_index] = type;
251 type_index++;
252 begin += desc->step;
253 if (begin == desc->end)
254 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000255 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000256 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500257 fixed_mtrr_types_initialized = 1;
258}
259
260static void commit_fixed_mtrrs(void)
261{
262 int i;
263 int j;
264 int msr_num;
265 int type_index;
266 /* 8 ranges per msr. */
267 msr_t fixed_msrs[NUM_FIXED_MTRRS];
268 unsigned long msr_index[NUM_FIXED_MTRRS];
269
270 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
271
272 disable_cache();
273
274 msr_num = 0;
275 type_index = 0;
276 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
277 const struct fixed_mtrr_desc *desc;
278 int num_ranges;
279
280 desc = &fixed_mtrr_desc[i];
281 num_ranges = (desc->end - desc->begin) / desc->step;
282 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
283 msr_index[msr_num] = desc->msr_index_base +
284 (j / RANGES_PER_FIXED_MTRR);
285 fixed_msrs[msr_num].lo |=
286 fixed_mtrr_types[type_index++] << 0;
287 fixed_msrs[msr_num].lo |=
288 fixed_mtrr_types[type_index++] << 8;
289 fixed_msrs[msr_num].lo |=
290 fixed_mtrr_types[type_index++] << 16;
291 fixed_msrs[msr_num].lo |=
292 fixed_mtrr_types[type_index++] << 24;
293 fixed_msrs[msr_num].hi |=
294 fixed_mtrr_types[type_index++] << 0;
295 fixed_msrs[msr_num].hi |=
296 fixed_mtrr_types[type_index++] << 8;
297 fixed_msrs[msr_num].hi |=
298 fixed_mtrr_types[type_index++] << 16;
299 fixed_msrs[msr_num].hi |=
300 fixed_mtrr_types[type_index++] << 24;
301 msr_num++;
302 }
303 }
304
305 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) {
306 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
307 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
308 wrmsr(msr_index[i], fixed_msrs[i]);
309 }
310
311 enable_cache();
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000312}
313
Aaron Durbin57686f82013-03-20 15:50:59 -0500314void x86_setup_fixed_mtrrs_no_enable(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000315{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500316 calc_fixed_mtrrs();
317 commit_fixed_mtrrs();
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000318}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000319
Aaron Durbin57686f82013-03-20 15:50:59 -0500320void x86_setup_fixed_mtrrs(void)
321{
322 x86_setup_fixed_mtrrs_no_enable();
323
324 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
325 enable_fixed_mtrr();
326}
327
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500328struct var_mtrr_state {
329 struct memranges *addr_space;
330 int above4gb;
331 int address_bits;
332 int commit_mtrrs;
333 int mtrr_index;
334 int def_mtrr_type;
335};
Aaron Durbin57686f82013-03-20 15:50:59 -0500336
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500337static void clear_var_mtrr(int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000338{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500339 msr_t msr_val;
340
341 msr_val = rdmsr(MTRRphysMask_MSR(index));
342 msr_val.lo &= ~MTRRphysMaskValid;
343 wrmsr(MTRRphysMask_MSR(index), msr_val);
344}
345
346static void write_var_mtrr(struct var_mtrr_state *var_state,
347 uint32_t base, uint32_t size, int mtrr_type)
348{
349 msr_t msr_val;
350 unsigned long msr_index;
351 resource_t rbase;
352 resource_t rsize;
353 resource_t mask;
354
355 /* Some variable MTRRs are attempted to be saved for the OS use.
356 * However, it's more important to try to map the full address space
357 * properly. */
358 if (var_state->mtrr_index >= bios_mtrrs)
359 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
360 if (var_state->mtrr_index >= total_mtrrs) {
361 printk(BIOS_ERR, "ERROR: Not enough MTTRs available!\n");
362 return;
363 }
364
365 rbase = base;
366 rsize = size;
367
368 rbase = RANGE_TO_PHYS_ADDR(rbase);
369 rsize = RANGE_TO_PHYS_ADDR(rsize);
370 rsize = -rsize;
371
372 mask = (1ULL << var_state->address_bits) - 1;
373 rsize = rsize & mask;
374
375 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
376 var_state->mtrr_index, rbase, rsize, mtrr_type);
377
378 msr_val.lo = rbase;
379 msr_val.lo |= mtrr_type;
380
381 msr_val.hi = rbase >> 32;
382 msr_index = MTRRphysBase_MSR(var_state->mtrr_index);
383 wrmsr(msr_index, msr_val);
384
385 msr_val.lo = rsize;
386 msr_val.lo |= MTRRphysMaskValid;
387 msr_val.hi = rsize >> 32;
388 msr_index = MTRRphysMask_MSR(var_state->mtrr_index);
389 wrmsr(msr_index, msr_val);
390}
391
392static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
393 uint32_t base, uint32_t size, int mtrr_type)
394{
395 while (size != 0) {
396 uint32_t addr_lsb;
397 uint32_t size_msb;
398 uint32_t mtrr_size;
399
400 addr_lsb = fls(base);
401 size_msb = fms(size);
402
403 /* All MTRR entries need to have their base aligned to the mask
404 * size. The maximum size is calculated by a function of the
405 * min base bit set and maximum size bit set. */
406 if (addr_lsb > size_msb)
407 mtrr_size = 1 << size_msb;
408 else
409 mtrr_size = 1 << addr_lsb;
410
411 if (var_state->commit_mtrrs)
412 write_var_mtrr(var_state, base, mtrr_size, mtrr_type);
413
414 size -= mtrr_size;
415 base += mtrr_size;
416 var_state->mtrr_index++;
417 }
418}
419
420static void setup_var_mtrrs_by_state(struct var_mtrr_state *var_state)
421{
422 struct range_entry *r;
423
424 /*
425 * For each range that meets the non-default type process it in the
426 * following manner:
427 * +------------------+ c2 = end
428 * | 0 or more bytes |
429 * +------------------+ b2 = c1 = ALIGN_DOWN(end)
430 * | |
431 * +------------------+ b1 = a2 = ALIGN_UP(begin)
432 * | 0 or more bytes |
433 * +------------------+ a1 = begin
434 *
435 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000436 */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500437 memranges_each_entry(r, var_state->addr_space) {
438 uint32_t a1, a2, b1, b2, c1, c2;
439 int mtrr_type = range_entry_tag(r);
440
441 /* Skip default type. */
442 if (var_state->def_mtrr_type == mtrr_type)
443 continue;
444
445 a1 = range_entry_base_mtrr_addr(r);
446 c2 = range_entry_end_mtrr_addr(r);
447
448 /* The end address is under 1MiB. The fixed MTRRs take
449 * precedence over the variable ones. Therefore this range
450 * can be ignored. */
451 if (c2 < RANGE_1MB)
452 continue;
453
454 /* Again, the fixed MTRRs take precedence so the beginning
455 * of the range can be set to 0 if it starts below 1MiB. */
456 if (a1 < RANGE_1MB)
457 a1 = 0;
458
459 /* If the range starts above 4GiB the processing is done. */
460 if (!var_state->above4gb && a1 >= RANGE_4GB)
461 break;
462
463 /* Clip the upper address to 4GiB if addresses above 4GiB
464 * are not being processed. */
465 if (!var_state->above4gb && c2 > RANGE_4GB)
466 c2 = RANGE_4GB;
467
468 /* Don't align up or down on the range if it is smaller
469 * than the minimum granularity. */
470 if ((c2 - a1) < MTRR_MIN_ALIGN) {
471 calc_var_mtrr_range(var_state, a1, c2 - a1, mtrr_type);
472 continue;
473 }
474
475 b1 = a2 = ALIGN_UP(a1, MTRR_MIN_ALIGN);
476 b2 = c1 = ALIGN_DOWN(c2, MTRR_MIN_ALIGN);
477
478 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
479 calc_var_mtrr_range(var_state, b1, b2 - b1, mtrr_type);
480 calc_var_mtrr_range(var_state, c1, c2 - c1, mtrr_type);
481 }
482}
483
484static int calc_var_mtrrs(struct memranges *addr_space,
485 int above4gb, int address_bits)
486{
487 int wb_deftype_count;
488 int uc_deftype_count;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000489 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000490
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500491 /* The default MTRR cacheability type is determined by calculating
492 * the number of MTTRs required for each MTTR type as if it was the
493 * default. */
494 var_state.addr_space = addr_space;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000495 var_state.above4gb = above4gb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500496 var_state.address_bits = address_bits;
497 var_state.commit_mtrrs = 0;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000498
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500499 var_state.mtrr_index = 0;
500 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
501 setup_var_mtrrs_by_state(&var_state);
502 wb_deftype_count = var_state.mtrr_index;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800503
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500504 var_state.mtrr_index = 0;
505 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
506 setup_var_mtrrs_by_state(&var_state);
507 uc_deftype_count = var_state.mtrr_index;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000508
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500509 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
510 wb_deftype_count, uc_deftype_count);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300511
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500512 if (wb_deftype_count < uc_deftype_count) {
513 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
514 return MTRR_TYPE_WRBACK;
515 }
516 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
517 return MTRR_TYPE_UNCACHEABLE;
518}
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300519
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500520static void commit_var_mtrrs(struct memranges *addr_space, int def_type,
521 int above4gb, int address_bits)
522{
523 struct var_mtrr_state var_state;
524 int i;
525
526 var_state.addr_space = addr_space;
527 var_state.above4gb = above4gb;
528 var_state.address_bits = address_bits;
529 /* Write the MSRs. */
530 var_state.commit_mtrrs = 1;
531 var_state.mtrr_index = 0;
532 var_state.def_mtrr_type = def_type;
533 setup_var_mtrrs_by_state(&var_state);
534
535 /* Clear all remaining variable MTTRs. */
536 for (i = var_state.mtrr_index; i < total_mtrrs; i++)
537 clear_var_mtrr(i);
538}
539
540void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
541{
542 static int mtrr_default_type = -1;
543 struct memranges *addr_space;
544
545 addr_space = get_physical_address_space();
546
547 if (mtrr_default_type == -1) {
548 if (above4gb == 2)
549 detect_var_mtrrs();
550 mtrr_default_type =
551 calc_var_mtrrs(addr_space, !!above4gb, address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000552 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700553
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500554 disable_cache();
555 commit_var_mtrrs(addr_space, mtrr_default_type, !!above4gb,
556 address_bits);
557 enable_var_mtrr(mtrr_default_type);
558 enable_cache();
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000559}
560
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100561void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000562{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100563 int address_size;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000564 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100565 address_size = cpu_phys_address_size();
566 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size);
567 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000568}
569
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000570int x86_mtrr_check(void)
571{
572 /* Only Pentium Pro and later have MTRR */
573 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000574 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000575
576 msr = rdmsr(0x2ff);
577 msr.lo >>= 10;
578
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000579 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000580 if (msr.lo & 0x01)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000581 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000582 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000583 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000584
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000585 printk(BIOS_DEBUG, "Variable MTRRs: ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000586 if (msr.lo & 0x02)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000587 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000588 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000589 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000590
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000591 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000592
593 post_code(0x93);
594 return ((int) msr.lo);
595}