blob: cdaa791de325515468e1aeb353e448bdd5b3afb3 [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>
Aaron Durbinbebf6692013-04-24 20:59:43 -050030#include <bootstate.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000031#include <console/console.h>
32#include <device/device.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050033#include <cpu/cpu.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000034#include <cpu/x86/msr.h>
35#include <cpu/x86/mtrr.h>
36#include <cpu/x86/cache.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070037#include <cpu/x86/lapic.h>
Sven Schnelleadfbcb792012-01-10 12:01:43 +010038#include <arch/cpu.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070039#include <arch/acpi.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050040#include <memrange.h>
Aaron Durbin57686f82013-03-20 15:50:59 -050041#if CONFIG_X86_AMD_FIXED_MTRRS
42#include <cpu/amd/mtrr.h>
43#define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
44#else
45#define MTRR_FIXED_WRBACK_BITS 0
46#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000047
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070048/* 2 MTRRS are reserved for the operating system */
49#define BIOS_MTRRS 6
50#define OS_MTRRS 2
51#define MTRRS (BIOS_MTRRS + OS_MTRRS)
52
53static int total_mtrrs = MTRRS;
54static int bios_mtrrs = BIOS_MTRRS;
55
56static void detect_var_mtrrs(void)
57{
58 msr_t msr;
59
60 msr = rdmsr(MTRRcap_MSR);
61
62 total_mtrrs = msr.lo & 0xff;
63 bios_mtrrs = total_mtrrs - OS_MTRRS;
64}
65
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000066void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000067{
68 msr_t msr;
69
70 msr = rdmsr(MTRRdefType_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050071 msr.lo |= MTRRdefTypeEn | MTRRdefTypeFixEn;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000072 wrmsr(MTRRdefType_MSR, msr);
73}
74
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050075static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000076{
77 msr_t msr;
78
79 msr = rdmsr(MTRRdefType_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050080 msr.lo &= ~0xff;
81 msr.lo |= MTRRdefTypeEn | deftype;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000082 wrmsr(MTRRdefType_MSR, msr);
83}
84
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000085/* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
86static inline unsigned int fms(unsigned int x)
87{
88 int r;
89
90 __asm__("bsrl %1,%0\n\t"
91 "jnz 1f\n\t"
92 "movl $0,%0\n"
93 "1:" : "=r" (r) : "g" (x));
94 return r;
95}
96
Martin Roth4c3ab732013-07-08 16:23:54 -060097/* fls: find least significant bit set */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000098static inline unsigned int fls(unsigned int x)
99{
100 int r;
101
102 __asm__("bsfl %1,%0\n\t"
103 "jnz 1f\n\t"
104 "movl $32,%0\n"
105 "1:" : "=r" (r) : "g" (x));
106 return r;
107}
108
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500109#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000110
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500111/* MTRRs are at a 4KiB granularity. Therefore all address calculations can
112 * be done with 32-bit numbers. This allows for the MTRR code to handle
113 * up to 2^44 bytes (16 TiB) of address space. */
114#define RANGE_SHIFT 12
115#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
116 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
117#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
118#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
119#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
120
121/* The minimum alignment while handling variable MTRR ranges is 64MiB. */
122#define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20)
123/* Helpful constants. */
124#define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20)
125#define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
126
Aaron Durbine3834422013-03-28 20:48:51 -0500127/*
128 * The default MTRR type selection uses 3 approaches for selecting the
129 * optimal number of variable MTRRs. For each range do 3 calculations:
130 * 1. UC as default type with no holes at top of range.
131 * 2. UC as default using holes at top of range.
132 * 3. WB as default.
133 * If using holes is optimal for a range when UC is the default type the
134 * tag is updated to direct the commit routine to use a hole at the top
135 * of a range.
136 */
137#define MTRR_ALGO_SHIFT (8)
138#define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1)
139/* If the default type is UC use the hole carving algorithm for a range. */
140#define MTRR_RANGE_UC_USE_HOLE (1 << MTRR_ALGO_SHIFT)
141
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500142static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000143{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500144 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000145}
146
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500147static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000148{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500149 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000150}
151
Aaron Durbine3834422013-03-28 20:48:51 -0500152static inline int range_entry_mtrr_type(struct range_entry *r)
153{
154 return range_entry_tag(r) & MTRR_TAG_MASK;
155}
156
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500157static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000158{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500159 static struct memranges *addr_space;
160 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800161
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500162 /* In order to handle some chipsets not being able to pre-determine
Martin Roth4c3ab732013-07-08 16:23:54 -0600163 * uncacheable ranges, such as graphics memory, at resource insertion
164 * time remove uncacheable regions from the cacheable ones. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500165 if (addr_space == NULL) {
166 struct range_entry *r;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500167 unsigned long mask;
168 unsigned long match;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500169
170 addr_space = &addr_space_storage;
171
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500172 mask = IORESOURCE_CACHEABLE;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500173 /* Collect cacheable and uncacheable address ranges. The
174 * uncacheable regions take precedence over the cacheable
175 * regions. */
176 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
177 memranges_add_resources(addr_space, mask, 0,
178 MTRR_TYPE_UNCACHEABLE);
179
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500180 /* Handle any write combining resources. Only prefetchable
181 * resources with the IORESOURCE_WRCOMB flag are appropriate
182 * for this MTRR type. */
183 match = IORESOURCE_PREFETCH | IORESOURCE_WRCOMB;
184 mask |= match;
185 memranges_add_resources(addr_space, mask, match,
186 MTRR_TYPE_WRCOMB);
187
Aaron Durbin77a5b402013-03-26 12:47:47 -0500188#if CONFIG_CACHE_ROM
189 /* Add a write-protect region covering the ROM size
190 * when CONFIG_CACHE_ROM is enabled. The ROM is assumed
191 * to be located at 4GiB - rom size. */
192 resource_t rom_base = RANGE_TO_PHYS_ADDR(
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200193 RANGE_4GB - PHYS_TO_RANGE_ADDR(CACHE_ROM_SIZE));
194 memranges_insert(addr_space, rom_base, CACHE_ROM_SIZE,
Aaron Durbin77a5b402013-03-26 12:47:47 -0500195 MTRR_TYPE_WRPROT);
196#endif
197
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500198 /* The address space below 4GiB is special. It needs to be
199 * covered entirly by range entries so that MTRR calculations
200 * can be properly done for the full 32-bit address space.
201 * Therefore, ensure holes are filled up to 4GiB as
202 * uncacheable */
203 memranges_fill_holes_up_to(addr_space,
204 RANGE_TO_PHYS_ADDR(RANGE_4GB),
205 MTRR_TYPE_UNCACHEABLE);
206
207 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
208 memranges_each_entry(r, addr_space)
209 printk(BIOS_DEBUG,
210 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
211 range_entry_base(r), range_entry_end(r),
212 range_entry_size(r), range_entry_tag(r));
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000213 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000214
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500215 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000216}
217
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500218/* Fixed MTRR descriptor. This structure defines the step size and begin
Martin Roth4c3ab732013-07-08 16:23:54 -0600219 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500220 * It also describes the offset in byte intervals to store the calculated MTRR
221 * type in an array. */
222struct fixed_mtrr_desc {
223 uint32_t begin;
224 uint32_t end;
225 uint32_t step;
226 int range_index;
227 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000228};
229
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500230/* Shared MTRR calculations. Can be reused by APs. */
231static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
232
233/* Fixed MTRR descriptors. */
234static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
235 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
236 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRRfix64K_00000_MSR },
237 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
238 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRRfix16K_80000_MSR },
239 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
240 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRRfix4K_C0000_MSR },
241};
242
243static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000244{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500245 static int fixed_mtrr_types_initialized;
246 struct memranges *phys_addr_space;
247 struct range_entry *r;
248 const struct fixed_mtrr_desc *desc;
249 const struct fixed_mtrr_desc *last_desc;
250 uint32_t begin;
251 uint32_t end;
252 int type_index;
253
254 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000255 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300256
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500257 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300258
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500259 /* Set all fixed ranges to uncacheable first. */
260 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300261
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500262 desc = &fixed_mtrr_desc[0];
263 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300264
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500265 memranges_each_entry(r, phys_addr_space) {
266 begin = range_entry_base_mtrr_addr(r);
267 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300268
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500269 if (begin >= last_desc->end)
270 break;
271
272 if (end > last_desc->end)
273 end = last_desc->end;
274
275 /* Get to the correct fixed mtrr descriptor. */
276 while (begin >= desc->end)
277 desc++;
278
279 type_index = desc->range_index;
280 type_index += (begin - desc->begin) / desc->step;
281
282 while (begin != end) {
283 unsigned char type;
284
285 type = range_entry_tag(r);
286 printk(MTRR_VERBOSE_LEVEL,
287 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
288 begin, begin + desc->step, type, type_index);
289 if (type == MTRR_TYPE_WRBACK)
290 type |= MTRR_FIXED_WRBACK_BITS;
291 fixed_mtrr_types[type_index] = type;
292 type_index++;
293 begin += desc->step;
294 if (begin == desc->end)
295 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000296 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000297 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500298 fixed_mtrr_types_initialized = 1;
299}
300
301static void commit_fixed_mtrrs(void)
302{
303 int i;
304 int j;
305 int msr_num;
306 int type_index;
307 /* 8 ranges per msr. */
308 msr_t fixed_msrs[NUM_FIXED_MTRRS];
309 unsigned long msr_index[NUM_FIXED_MTRRS];
310
311 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
312
313 disable_cache();
314
315 msr_num = 0;
316 type_index = 0;
317 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
318 const struct fixed_mtrr_desc *desc;
319 int num_ranges;
320
321 desc = &fixed_mtrr_desc[i];
322 num_ranges = (desc->end - desc->begin) / desc->step;
323 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
324 msr_index[msr_num] = desc->msr_index_base +
325 (j / RANGES_PER_FIXED_MTRR);
326 fixed_msrs[msr_num].lo |=
327 fixed_mtrr_types[type_index++] << 0;
328 fixed_msrs[msr_num].lo |=
329 fixed_mtrr_types[type_index++] << 8;
330 fixed_msrs[msr_num].lo |=
331 fixed_mtrr_types[type_index++] << 16;
332 fixed_msrs[msr_num].lo |=
333 fixed_mtrr_types[type_index++] << 24;
334 fixed_msrs[msr_num].hi |=
335 fixed_mtrr_types[type_index++] << 0;
336 fixed_msrs[msr_num].hi |=
337 fixed_mtrr_types[type_index++] << 8;
338 fixed_msrs[msr_num].hi |=
339 fixed_mtrr_types[type_index++] << 16;
340 fixed_msrs[msr_num].hi |=
341 fixed_mtrr_types[type_index++] << 24;
342 msr_num++;
343 }
344 }
345
346 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) {
347 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
348 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
349 wrmsr(msr_index[i], fixed_msrs[i]);
350 }
351
352 enable_cache();
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000353}
354
Aaron Durbin57686f82013-03-20 15:50:59 -0500355void x86_setup_fixed_mtrrs_no_enable(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000356{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500357 calc_fixed_mtrrs();
358 commit_fixed_mtrrs();
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000359}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000360
Aaron Durbin57686f82013-03-20 15:50:59 -0500361void x86_setup_fixed_mtrrs(void)
362{
363 x86_setup_fixed_mtrrs_no_enable();
364
365 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
366 enable_fixed_mtrr();
367}
368
Aaron Durbin77a5b402013-03-26 12:47:47 -0500369/* Keep track of the MTRR that covers the ROM for caching purposes. */
370#if CONFIG_CACHE_ROM
371static long rom_cache_mtrr = -1;
372
Aaron Durbinbc07f5d2013-03-26 13:09:39 -0500373long x86_mtrr_rom_cache_var_index(void)
374{
375 return rom_cache_mtrr;
376}
377
Aaron Durbin77a5b402013-03-26 12:47:47 -0500378void x86_mtrr_enable_rom_caching(void)
379{
380 msr_t msr_val;
381 unsigned long index;
382
383 if (rom_cache_mtrr < 0)
384 return;
385
386 index = rom_cache_mtrr;
387 disable_cache();
388 msr_val = rdmsr(MTRRphysBase_MSR(index));
389 msr_val.lo &= ~0xff;
390 msr_val.lo |= MTRR_TYPE_WRPROT;
391 wrmsr(MTRRphysBase_MSR(index), msr_val);
392 enable_cache();
393}
394
395void x86_mtrr_disable_rom_caching(void)
396{
397 msr_t msr_val;
398 unsigned long index;
399
400 if (rom_cache_mtrr < 0)
401 return;
402
403 index = rom_cache_mtrr;
404 disable_cache();
405 msr_val = rdmsr(MTRRphysBase_MSR(index));
406 msr_val.lo &= ~0xff;
407 wrmsr(MTRRphysBase_MSR(index), msr_val);
408 enable_cache();
409}
Aaron Durbinebf142a2013-03-29 16:23:23 -0500410
Aaron Durbinbebf6692013-04-24 20:59:43 -0500411static void disable_cache_rom(void *unused)
Aaron Durbinebf142a2013-03-29 16:23:23 -0500412{
413 x86_mtrr_disable_rom_caching();
414}
Aaron Durbinbebf6692013-04-24 20:59:43 -0500415
416BOOT_STATE_INIT_ENTRIES(disable_rom_cache_bscb) = {
417 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY,
418 disable_cache_rom, NULL),
419 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT,
420 disable_cache_rom, NULL),
421};
Aaron Durbin77a5b402013-03-26 12:47:47 -0500422#endif
423
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500424struct var_mtrr_state {
425 struct memranges *addr_space;
426 int above4gb;
427 int address_bits;
428 int commit_mtrrs;
429 int mtrr_index;
430 int def_mtrr_type;
431};
Aaron Durbin57686f82013-03-20 15:50:59 -0500432
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500433static void clear_var_mtrr(int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000434{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500435 msr_t msr_val;
436
437 msr_val = rdmsr(MTRRphysMask_MSR(index));
438 msr_val.lo &= ~MTRRphysMaskValid;
439 wrmsr(MTRRphysMask_MSR(index), msr_val);
440}
441
442static void write_var_mtrr(struct var_mtrr_state *var_state,
443 uint32_t base, uint32_t size, int mtrr_type)
444{
445 msr_t msr_val;
446 unsigned long msr_index;
447 resource_t rbase;
448 resource_t rsize;
449 resource_t mask;
450
451 /* Some variable MTRRs are attempted to be saved for the OS use.
452 * However, it's more important to try to map the full address space
453 * properly. */
454 if (var_state->mtrr_index >= bios_mtrrs)
455 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
456 if (var_state->mtrr_index >= total_mtrrs) {
Paul Menzel4fe98132014-01-25 15:55:28 +0100457 printk(BIOS_ERR, "ERROR: Not enough MTRRs available!\n");
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500458 return;
459 }
460
461 rbase = base;
462 rsize = size;
463
464 rbase = RANGE_TO_PHYS_ADDR(rbase);
465 rsize = RANGE_TO_PHYS_ADDR(rsize);
466 rsize = -rsize;
467
468 mask = (1ULL << var_state->address_bits) - 1;
469 rsize = rsize & mask;
470
Aaron Durbin77a5b402013-03-26 12:47:47 -0500471#if CONFIG_CACHE_ROM
472 /* CONFIG_CACHE_ROM allocates an MTRR specifically for allowing
473 * one to turn on caching for faster ROM access. However, it is
474 * left to the MTRR callers to enable it. */
475 if (mtrr_type == MTRR_TYPE_WRPROT) {
476 mtrr_type = MTRR_TYPE_UNCACHEABLE;
477 if (rom_cache_mtrr < 0)
478 rom_cache_mtrr = var_state->mtrr_index;
479 }
480#endif
481
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500482 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
483 var_state->mtrr_index, rbase, rsize, mtrr_type);
484
485 msr_val.lo = rbase;
486 msr_val.lo |= mtrr_type;
487
488 msr_val.hi = rbase >> 32;
489 msr_index = MTRRphysBase_MSR(var_state->mtrr_index);
490 wrmsr(msr_index, msr_val);
491
492 msr_val.lo = rsize;
493 msr_val.lo |= MTRRphysMaskValid;
494 msr_val.hi = rsize >> 32;
495 msr_index = MTRRphysMask_MSR(var_state->mtrr_index);
496 wrmsr(msr_index, msr_val);
497}
498
499static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
500 uint32_t base, uint32_t size, int mtrr_type)
501{
502 while (size != 0) {
503 uint32_t addr_lsb;
504 uint32_t size_msb;
505 uint32_t mtrr_size;
506
507 addr_lsb = fls(base);
508 size_msb = fms(size);
509
510 /* All MTRR entries need to have their base aligned to the mask
511 * size. The maximum size is calculated by a function of the
512 * min base bit set and maximum size bit set. */
513 if (addr_lsb > size_msb)
514 mtrr_size = 1 << size_msb;
515 else
516 mtrr_size = 1 << addr_lsb;
517
518 if (var_state->commit_mtrrs)
519 write_var_mtrr(var_state, base, mtrr_size, mtrr_type);
520
521 size -= mtrr_size;
522 base += mtrr_size;
523 var_state->mtrr_index++;
524 }
525}
526
Aaron Durbine3834422013-03-28 20:48:51 -0500527static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
528 struct range_entry *r)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500529{
Aaron Durbine3834422013-03-28 20:48:51 -0500530 uint32_t a1, a2, b1, b2;
531 int mtrr_type;
532 struct range_entry *next;
533
534 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600535 * Determine MTRRs based on the following algorithm for the given entry:
Aaron Durbine3834422013-03-28 20:48:51 -0500536 * +------------------+ b2 = ALIGN_UP(end)
537 * | 0 or more bytes | <-- hole is carved out between b1 and b2
538 * +------------------+ a2 = b1 = end
539 * | |
540 * +------------------+ a1 = begin
541 *
542 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
543 */
544 mtrr_type = range_entry_mtrr_type(r);
545
546 a1 = range_entry_base_mtrr_addr(r);
547 a2 = range_entry_end_mtrr_addr(r);
548
549 /* The end address is under 1MiB. The fixed MTRRs take
550 * precedence over the variable ones. Therefore this range
551 * can be ignored. */
552 if (a2 < RANGE_1MB)
553 return;
554
555 /* Again, the fixed MTRRs take precedence so the beginning
556 * of the range can be set to 0 if it starts below 1MiB. */
557 if (a1 < RANGE_1MB)
558 a1 = 0;
559
560 /* If the range starts above 4GiB the processing is done. */
561 if (!var_state->above4gb && a1 >= RANGE_4GB)
562 return;
563
564 /* Clip the upper address to 4GiB if addresses above 4GiB
565 * are not being processed. */
566 if (!var_state->above4gb && a2 > RANGE_4GB)
567 a2 = RANGE_4GB;
568
Aaron Durbin53924242013-03-29 11:48:27 -0500569 next = memranges_next_entry(var_state->addr_space, r);
570
Aaron Durbine3834422013-03-28 20:48:51 -0500571 b1 = a2;
Aaron Durbin53924242013-03-29 11:48:27 -0500572
Martin Roth4c3ab732013-07-08 16:23:54 -0600573 /* First check if a1 is >= 4GiB and the current entry is the last
Aaron Durbin53924242013-03-29 11:48:27 -0500574 * entry. If so perform an optimization of covering a larger range
575 * defined by the base address' alignment. */
576 if (a1 >= RANGE_4GB && next == NULL) {
577 uint32_t addr_lsb;
578
579 addr_lsb = fls(a1);
580 b2 = (1 << addr_lsb) + a1;
581 if (b2 >= a2) {
582 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
583 return;
584 }
585 }
586
587 /* Handle the min alignment roundup case. */
Aaron Durbine3834422013-03-28 20:48:51 -0500588 b2 = ALIGN_UP(a2, MTRR_MIN_ALIGN);
589
590 /* Check against the next range. If the current range_entry is the
591 * last entry then carving a hole is no problem. If the current entry
592 * isn't the last entry then check that the last entry covers the
593 * entire hole range with the default mtrr type. */
Aaron Durbine3834422013-03-28 20:48:51 -0500594 if (next != NULL &&
595 (range_entry_mtrr_type(next) != var_state->def_mtrr_type ||
596 range_entry_end_mtrr_addr(next) < b2)) {
597 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
598 return;
599 }
600
601 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
602 calc_var_mtrr_range(var_state, b1, b2 - b1, var_state->def_mtrr_type);
603}
604
605static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state,
606 struct range_entry *r)
607{
608 uint32_t a1, a2, b1, b2, c1, c2;
609 int mtrr_type;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500610
611 /*
612 * For each range that meets the non-default type process it in the
613 * following manner:
614 * +------------------+ c2 = end
615 * | 0 or more bytes |
616 * +------------------+ b2 = c1 = ALIGN_DOWN(end)
617 * | |
618 * +------------------+ b1 = a2 = ALIGN_UP(begin)
619 * | 0 or more bytes |
620 * +------------------+ a1 = begin
621 *
622 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000623 */
Aaron Durbine3834422013-03-28 20:48:51 -0500624 mtrr_type = range_entry_mtrr_type(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500625
Aaron Durbine3834422013-03-28 20:48:51 -0500626 a1 = range_entry_base_mtrr_addr(r);
627 c2 = range_entry_end_mtrr_addr(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500628
Aaron Durbine3834422013-03-28 20:48:51 -0500629 /* The end address is under 1MiB. The fixed MTRRs take
630 * precedence over the variable ones. Therefore this range
631 * can be ignored. */
632 if (c2 < RANGE_1MB)
633 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500634
Aaron Durbine3834422013-03-28 20:48:51 -0500635 /* Again, the fixed MTRRs take precedence so the beginning
636 * of the range can be set to 0 if it starts below 1MiB. */
637 if (a1 < RANGE_1MB)
638 a1 = 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500639
Aaron Durbine3834422013-03-28 20:48:51 -0500640 /* If the range starts above 4GiB the processing is done. */
641 if (!var_state->above4gb && a1 >= RANGE_4GB)
642 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500643
Aaron Durbine3834422013-03-28 20:48:51 -0500644 /* Clip the upper address to 4GiB if addresses above 4GiB
645 * are not being processed. */
646 if (!var_state->above4gb && c2 > RANGE_4GB)
647 c2 = RANGE_4GB;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500648
Aaron Durbine3834422013-03-28 20:48:51 -0500649 /* Don't align up or down on the range if it is smaller
650 * than the minimum granularity. */
651 if ((c2 - a1) < MTRR_MIN_ALIGN) {
652 calc_var_mtrr_range(var_state, a1, c2 - a1, mtrr_type);
653 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500654 }
Aaron Durbine3834422013-03-28 20:48:51 -0500655
656 b1 = a2 = ALIGN_UP(a1, MTRR_MIN_ALIGN);
657 b2 = c1 = ALIGN_DOWN(c2, MTRR_MIN_ALIGN);
658
659 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
660 calc_var_mtrr_range(var_state, b1, b2 - b1, mtrr_type);
661 calc_var_mtrr_range(var_state, c1, c2 - c1, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500662}
663
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600664static void __calc_var_mtrrs(struct memranges *addr_space,
665 int above4gb, int address_bits,
666 int *num_def_wb_mtrrs, int *num_def_uc_mtrrs)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500667{
668 int wb_deftype_count;
669 int uc_deftype_count;
Aaron Durbine3834422013-03-28 20:48:51 -0500670 struct range_entry *r;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000671 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000672
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500673 /* The default MTRR cacheability type is determined by calculating
Paul Menzel4fe98132014-01-25 15:55:28 +0100674 * the number of MTRRs required for each MTRR type as if it was the
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500675 * default. */
676 var_state.addr_space = addr_space;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000677 var_state.above4gb = above4gb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500678 var_state.address_bits = address_bits;
679 var_state.commit_mtrrs = 0;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000680
Aaron Durbine3834422013-03-28 20:48:51 -0500681 wb_deftype_count = 0;
682 uc_deftype_count = 0;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800683
Aaron Durbine3834422013-03-28 20:48:51 -0500684 /*
685 * For each range do 3 calculations:
686 * 1. UC as default type with no holes at top of range.
687 * 2. UC as default using holes at top of range.
688 * 3. WB as default.
Martin Roth4c3ab732013-07-08 16:23:54 -0600689 * The lowest count is then used as default after totaling all
690 * MTRRs. Note that the optimal algorithm for UC default is marked in
Aaron Durbine3834422013-03-28 20:48:51 -0500691 * the tag of each range regardless of final decision. UC takes
Martin Roth4c3ab732013-07-08 16:23:54 -0600692 * precedence in the MTRR architecture. Therefore, only holes can be
Aaron Durbine3834422013-03-28 20:48:51 -0500693 * used when the type of the region is MTRR_TYPE_WRBACK with
694 * MTRR_TYPE_UNCACHEABLE as the default type.
695 */
696 memranges_each_entry(r, var_state.addr_space) {
697 int mtrr_type;
698
699 mtrr_type = range_entry_mtrr_type(r);
700
701 if (mtrr_type != MTRR_TYPE_UNCACHEABLE) {
702 int uc_hole_count;
703 int uc_no_hole_count;
704
705 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
706 var_state.mtrr_index = 0;
707
708 /* No hole calculation. */
709 calc_var_mtrrs_without_hole(&var_state, r);
710 uc_no_hole_count = var_state.mtrr_index;
711
712 /* Hole calculation only if type is WB. The 64 number
713 * is a count that is unachievable, thus making it
714 * a default large number in the case of not doing
715 * the hole calculation. */
716 uc_hole_count = 64;
717 if (mtrr_type == MTRR_TYPE_WRBACK) {
718 var_state.mtrr_index = 0;
719 calc_var_mtrrs_with_hole(&var_state, r);
720 uc_hole_count = var_state.mtrr_index;
721 }
722
723 /* Mark the entry with the optimal algorithm. */
724 if (uc_no_hole_count < uc_hole_count) {
725 uc_deftype_count += uc_no_hole_count;
726 } else {
727 unsigned long new_tag;
728
729 new_tag = mtrr_type | MTRR_RANGE_UC_USE_HOLE;
730 range_entry_update_tag(r, new_tag);
731 uc_deftype_count += uc_hole_count;
732 }
733 }
734
735 if (mtrr_type != MTRR_TYPE_WRBACK) {
736 var_state.mtrr_index = 0;
737 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
738 calc_var_mtrrs_without_hole(&var_state, r);
739 wb_deftype_count += var_state.mtrr_index;
740 }
741 }
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600742 *num_def_wb_mtrrs = wb_deftype_count;
743 *num_def_uc_mtrrs = uc_deftype_count;
744}
745
746static int calc_var_mtrrs(struct memranges *addr_space,
747 int above4gb, int address_bits)
748{
749 int wb_deftype_count = 0;
750 int uc_deftype_count = 0;
751
752 __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count,
753 &uc_deftype_count);
754
755 if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) {
756 printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. "
757 "WB/UC MTRR counts: %d/%d > %d.\n",
758 wb_deftype_count, uc_deftype_count, bios_mtrrs);
759 memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB,
760 MTRR_TYPE_UNCACHEABLE);
761 __calc_var_mtrrs(addr_space, above4gb, address_bits,
762 &wb_deftype_count, &uc_deftype_count);
763 }
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000764
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500765 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
766 wb_deftype_count, uc_deftype_count);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300767
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500768 if (wb_deftype_count < uc_deftype_count) {
769 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
770 return MTRR_TYPE_WRBACK;
771 }
772 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
773 return MTRR_TYPE_UNCACHEABLE;
774}
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300775
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500776static void commit_var_mtrrs(struct memranges *addr_space, int def_type,
777 int above4gb, int address_bits)
778{
Aaron Durbine3834422013-03-28 20:48:51 -0500779 struct range_entry *r;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500780 struct var_mtrr_state var_state;
781 int i;
782
783 var_state.addr_space = addr_space;
784 var_state.above4gb = above4gb;
785 var_state.address_bits = address_bits;
786 /* Write the MSRs. */
787 var_state.commit_mtrrs = 1;
788 var_state.mtrr_index = 0;
789 var_state.def_mtrr_type = def_type;
Aaron Durbine3834422013-03-28 20:48:51 -0500790
791 memranges_each_entry(r, var_state.addr_space) {
792 if (range_entry_mtrr_type(r) == def_type)
793 continue;
794
795 if (def_type == MTRR_TYPE_UNCACHEABLE &&
796 (range_entry_tag(r) & MTRR_RANGE_UC_USE_HOLE))
797 calc_var_mtrrs_with_hole(&var_state, r);
798 else
799 calc_var_mtrrs_without_hole(&var_state, r);
800 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500801
Paul Menzel4fe98132014-01-25 15:55:28 +0100802 /* Clear all remaining variable MTRRs. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500803 for (i = var_state.mtrr_index; i < total_mtrrs; i++)
804 clear_var_mtrr(i);
805}
806
807void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
808{
809 static int mtrr_default_type = -1;
810 struct memranges *addr_space;
811
812 addr_space = get_physical_address_space();
813
814 if (mtrr_default_type == -1) {
815 if (above4gb == 2)
816 detect_var_mtrrs();
817 mtrr_default_type =
818 calc_var_mtrrs(addr_space, !!above4gb, address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000819 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700820
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500821 disable_cache();
822 commit_var_mtrrs(addr_space, mtrr_default_type, !!above4gb,
823 address_bits);
824 enable_var_mtrr(mtrr_default_type);
825 enable_cache();
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000826}
827
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100828void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000829{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100830 int address_size;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000831 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100832 address_size = cpu_phys_address_size();
833 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size);
834 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000835}
836
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000837int x86_mtrr_check(void)
838{
839 /* Only Pentium Pro and later have MTRR */
840 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000841 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000842
843 msr = rdmsr(0x2ff);
844 msr.lo >>= 10;
845
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000846 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000847 if (msr.lo & 0x01)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000848 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000849 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000850 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000851
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000852 printk(BIOS_DEBUG, "Variable MTRRs: ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000853 if (msr.lo & 0x02)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000854 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000855 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000856 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000857
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000858 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000859
860 post_code(0x93);
861 return ((int) msr.lo);
862}