blob: d85a86982c39ef510c29b8cc682db38dce0aa6e0 [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 Durbinca4f4b82014-02-08 15:41:52 -060033#include <device/pci_ids.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050034#include <cpu/cpu.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000035#include <cpu/x86/msr.h>
36#include <cpu/x86/mtrr.h>
37#include <cpu/x86/cache.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070038#include <cpu/x86/lapic.h>
Sven Schnelleadfbcb792012-01-10 12:01:43 +010039#include <arch/cpu.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070040#include <arch/acpi.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050041#include <memrange.h>
Aaron Durbin57686f82013-03-20 15:50:59 -050042#if CONFIG_X86_AMD_FIXED_MTRRS
43#include <cpu/amd/mtrr.h>
44#define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
45#else
46#define MTRR_FIXED_WRBACK_BITS 0
47#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000048
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);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050072 msr.lo |= MTRRdefTypeEn | MTRRdefTypeFixEn;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000073 wrmsr(MTRRdefType_MSR, msr);
74}
75
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050076static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000077{
78 msr_t msr;
79
80 msr = rdmsr(MTRRdefType_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050081 msr.lo &= ~0xff;
82 msr.lo |= MTRRdefTypeEn | deftype;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000083 wrmsr(MTRRdefType_MSR, msr);
84}
85
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000086/* fms: find most sigificant bit set, stolen from Linux Kernel Source. */
87static inline unsigned int fms(unsigned int x)
88{
89 int r;
90
91 __asm__("bsrl %1,%0\n\t"
92 "jnz 1f\n\t"
93 "movl $0,%0\n"
94 "1:" : "=r" (r) : "g" (x));
95 return r;
96}
97
Martin Roth4c3ab732013-07-08 16:23:54 -060098/* fls: find least significant bit set */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000099static inline unsigned int fls(unsigned int x)
100{
101 int r;
102
103 __asm__("bsfl %1,%0\n\t"
104 "jnz 1f\n\t"
105 "movl $32,%0\n"
106 "1:" : "=r" (r) : "g" (x));
107 return r;
108}
109
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500110#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000111
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500112/* MTRRs are at a 4KiB granularity. Therefore all address calculations can
113 * be done with 32-bit numbers. This allows for the MTRR code to handle
114 * up to 2^44 bytes (16 TiB) of address space. */
115#define RANGE_SHIFT 12
116#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
117 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
118#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
119#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
120#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
121
122/* The minimum alignment while handling variable MTRR ranges is 64MiB. */
123#define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20)
124/* Helpful constants. */
125#define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20)
126#define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
127
Aaron Durbine3834422013-03-28 20:48:51 -0500128/*
129 * The default MTRR type selection uses 3 approaches for selecting the
130 * optimal number of variable MTRRs. For each range do 3 calculations:
131 * 1. UC as default type with no holes at top of range.
132 * 2. UC as default using holes at top of range.
133 * 3. WB as default.
134 * If using holes is optimal for a range when UC is the default type the
135 * tag is updated to direct the commit routine to use a hole at the top
136 * of a range.
137 */
138#define MTRR_ALGO_SHIFT (8)
139#define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1)
140/* If the default type is UC use the hole carving algorithm for a range. */
141#define MTRR_RANGE_UC_USE_HOLE (1 << MTRR_ALGO_SHIFT)
142
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500143static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000144{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500145 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000146}
147
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500148static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000149{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500150 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000151}
152
Aaron Durbine3834422013-03-28 20:48:51 -0500153static inline int range_entry_mtrr_type(struct range_entry *r)
154{
155 return range_entry_tag(r) & MTRR_TAG_MASK;
156}
157
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600158static int filter_vga_wrcomb(struct device *dev, struct resource *res)
159{
160 /* Only handle PCI devices. */
161 if (dev->path.type != DEVICE_PATH_PCI)
162 return 0;
163
164 /* Only handle VGA class devices. */
165 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
166 return 0;
167
168 /* Add resource as write-combining in the address space. */
169 return 1;
170}
171
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500172static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000173{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500174 static struct memranges *addr_space;
175 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800176
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500177 /* In order to handle some chipsets not being able to pre-determine
Martin Roth4c3ab732013-07-08 16:23:54 -0600178 * uncacheable ranges, such as graphics memory, at resource insertion
179 * time remove uncacheable regions from the cacheable ones. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500180 if (addr_space == NULL) {
181 struct range_entry *r;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500182 unsigned long mask;
183 unsigned long match;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500184
185 addr_space = &addr_space_storage;
186
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500187 mask = IORESOURCE_CACHEABLE;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500188 /* Collect cacheable and uncacheable address ranges. The
189 * uncacheable regions take precedence over the cacheable
190 * regions. */
191 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
192 memranges_add_resources(addr_space, mask, 0,
193 MTRR_TYPE_UNCACHEABLE);
194
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500195 /* Handle any write combining resources. Only prefetchable
Vladimir Serbinenko30fe6122014-02-05 23:25:28 +0100196 * resources are appropriate for this MTRR type. */
197 match = IORESOURCE_PREFETCH;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500198 mask |= match;
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600199 memranges_add_resources_filter(addr_space, mask, match, MTRR_TYPE_WRCOMB,
200 filter_vga_wrcomb);
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500201
Aaron Durbin77a5b402013-03-26 12:47:47 -0500202#if CONFIG_CACHE_ROM
203 /* Add a write-protect region covering the ROM size
204 * when CONFIG_CACHE_ROM is enabled. The ROM is assumed
205 * to be located at 4GiB - rom size. */
206 resource_t rom_base = RANGE_TO_PHYS_ADDR(
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200207 RANGE_4GB - PHYS_TO_RANGE_ADDR(CACHE_ROM_SIZE));
208 memranges_insert(addr_space, rom_base, CACHE_ROM_SIZE,
Aaron Durbin77a5b402013-03-26 12:47:47 -0500209 MTRR_TYPE_WRPROT);
210#endif
211
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500212 /* The address space below 4GiB is special. It needs to be
213 * covered entirly by range entries so that MTRR calculations
214 * can be properly done for the full 32-bit address space.
215 * Therefore, ensure holes are filled up to 4GiB as
216 * uncacheable */
217 memranges_fill_holes_up_to(addr_space,
218 RANGE_TO_PHYS_ADDR(RANGE_4GB),
219 MTRR_TYPE_UNCACHEABLE);
220
221 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
222 memranges_each_entry(r, addr_space)
223 printk(BIOS_DEBUG,
224 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
225 range_entry_base(r), range_entry_end(r),
226 range_entry_size(r), range_entry_tag(r));
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000227 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000228
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500229 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000230}
231
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500232/* Fixed MTRR descriptor. This structure defines the step size and begin
Martin Roth4c3ab732013-07-08 16:23:54 -0600233 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500234 * It also describes the offset in byte intervals to store the calculated MTRR
235 * type in an array. */
236struct fixed_mtrr_desc {
237 uint32_t begin;
238 uint32_t end;
239 uint32_t step;
240 int range_index;
241 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000242};
243
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500244/* Shared MTRR calculations. Can be reused by APs. */
245static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
246
247/* Fixed MTRR descriptors. */
248static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
249 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
250 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRRfix64K_00000_MSR },
251 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
252 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRRfix16K_80000_MSR },
253 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
254 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRRfix4K_C0000_MSR },
255};
256
257static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000258{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500259 static int fixed_mtrr_types_initialized;
260 struct memranges *phys_addr_space;
261 struct range_entry *r;
262 const struct fixed_mtrr_desc *desc;
263 const struct fixed_mtrr_desc *last_desc;
264 uint32_t begin;
265 uint32_t end;
266 int type_index;
267
268 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000269 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300270
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500271 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300272
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500273 /* Set all fixed ranges to uncacheable first. */
274 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300275
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500276 desc = &fixed_mtrr_desc[0];
277 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300278
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500279 memranges_each_entry(r, phys_addr_space) {
280 begin = range_entry_base_mtrr_addr(r);
281 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300282
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500283 if (begin >= last_desc->end)
284 break;
285
286 if (end > last_desc->end)
287 end = last_desc->end;
288
289 /* Get to the correct fixed mtrr descriptor. */
290 while (begin >= desc->end)
291 desc++;
292
293 type_index = desc->range_index;
294 type_index += (begin - desc->begin) / desc->step;
295
296 while (begin != end) {
297 unsigned char type;
298
299 type = range_entry_tag(r);
300 printk(MTRR_VERBOSE_LEVEL,
301 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
302 begin, begin + desc->step, type, type_index);
303 if (type == MTRR_TYPE_WRBACK)
304 type |= MTRR_FIXED_WRBACK_BITS;
305 fixed_mtrr_types[type_index] = type;
306 type_index++;
307 begin += desc->step;
308 if (begin == desc->end)
309 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000310 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000311 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500312 fixed_mtrr_types_initialized = 1;
313}
314
315static void commit_fixed_mtrrs(void)
316{
317 int i;
318 int j;
319 int msr_num;
320 int type_index;
321 /* 8 ranges per msr. */
322 msr_t fixed_msrs[NUM_FIXED_MTRRS];
323 unsigned long msr_index[NUM_FIXED_MTRRS];
324
325 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
326
327 disable_cache();
328
329 msr_num = 0;
330 type_index = 0;
331 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
332 const struct fixed_mtrr_desc *desc;
333 int num_ranges;
334
335 desc = &fixed_mtrr_desc[i];
336 num_ranges = (desc->end - desc->begin) / desc->step;
337 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
338 msr_index[msr_num] = desc->msr_index_base +
339 (j / RANGES_PER_FIXED_MTRR);
340 fixed_msrs[msr_num].lo |=
341 fixed_mtrr_types[type_index++] << 0;
342 fixed_msrs[msr_num].lo |=
343 fixed_mtrr_types[type_index++] << 8;
344 fixed_msrs[msr_num].lo |=
345 fixed_mtrr_types[type_index++] << 16;
346 fixed_msrs[msr_num].lo |=
347 fixed_mtrr_types[type_index++] << 24;
348 fixed_msrs[msr_num].hi |=
349 fixed_mtrr_types[type_index++] << 0;
350 fixed_msrs[msr_num].hi |=
351 fixed_mtrr_types[type_index++] << 8;
352 fixed_msrs[msr_num].hi |=
353 fixed_mtrr_types[type_index++] << 16;
354 fixed_msrs[msr_num].hi |=
355 fixed_mtrr_types[type_index++] << 24;
356 msr_num++;
357 }
358 }
359
360 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) {
361 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
362 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
363 wrmsr(msr_index[i], fixed_msrs[i]);
364 }
365
366 enable_cache();
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000367}
368
Aaron Durbin57686f82013-03-20 15:50:59 -0500369void x86_setup_fixed_mtrrs_no_enable(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000370{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500371 calc_fixed_mtrrs();
372 commit_fixed_mtrrs();
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000373}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000374
Aaron Durbin57686f82013-03-20 15:50:59 -0500375void x86_setup_fixed_mtrrs(void)
376{
377 x86_setup_fixed_mtrrs_no_enable();
378
379 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
380 enable_fixed_mtrr();
381}
382
Aaron Durbin77a5b402013-03-26 12:47:47 -0500383/* Keep track of the MTRR that covers the ROM for caching purposes. */
384#if CONFIG_CACHE_ROM
385static long rom_cache_mtrr = -1;
386
Aaron Durbinbc07f5d2013-03-26 13:09:39 -0500387long x86_mtrr_rom_cache_var_index(void)
388{
389 return rom_cache_mtrr;
390}
391
Aaron Durbin77a5b402013-03-26 12:47:47 -0500392void x86_mtrr_enable_rom_caching(void)
393{
394 msr_t msr_val;
395 unsigned long index;
396
397 if (rom_cache_mtrr < 0)
398 return;
399
400 index = rom_cache_mtrr;
401 disable_cache();
402 msr_val = rdmsr(MTRRphysBase_MSR(index));
403 msr_val.lo &= ~0xff;
404 msr_val.lo |= MTRR_TYPE_WRPROT;
405 wrmsr(MTRRphysBase_MSR(index), msr_val);
406 enable_cache();
407}
408
409void x86_mtrr_disable_rom_caching(void)
410{
411 msr_t msr_val;
412 unsigned long index;
413
414 if (rom_cache_mtrr < 0)
415 return;
416
417 index = rom_cache_mtrr;
418 disable_cache();
419 msr_val = rdmsr(MTRRphysBase_MSR(index));
420 msr_val.lo &= ~0xff;
421 wrmsr(MTRRphysBase_MSR(index), msr_val);
422 enable_cache();
423}
Aaron Durbinebf142a2013-03-29 16:23:23 -0500424
Aaron Durbinbebf6692013-04-24 20:59:43 -0500425static void disable_cache_rom(void *unused)
Aaron Durbinebf142a2013-03-29 16:23:23 -0500426{
427 x86_mtrr_disable_rom_caching();
428}
Aaron Durbinbebf6692013-04-24 20:59:43 -0500429
430BOOT_STATE_INIT_ENTRIES(disable_rom_cache_bscb) = {
431 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY,
432 disable_cache_rom, NULL),
433 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT,
434 disable_cache_rom, NULL),
435};
Aaron Durbin77a5b402013-03-26 12:47:47 -0500436#endif
437
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500438struct var_mtrr_state {
439 struct memranges *addr_space;
440 int above4gb;
441 int address_bits;
442 int commit_mtrrs;
443 int mtrr_index;
444 int def_mtrr_type;
445};
Aaron Durbin57686f82013-03-20 15:50:59 -0500446
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500447static void clear_var_mtrr(int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000448{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500449 msr_t msr_val;
450
451 msr_val = rdmsr(MTRRphysMask_MSR(index));
452 msr_val.lo &= ~MTRRphysMaskValid;
453 wrmsr(MTRRphysMask_MSR(index), msr_val);
454}
455
456static void write_var_mtrr(struct var_mtrr_state *var_state,
457 uint32_t base, uint32_t size, int mtrr_type)
458{
459 msr_t msr_val;
460 unsigned long msr_index;
461 resource_t rbase;
462 resource_t rsize;
463 resource_t mask;
464
465 /* Some variable MTRRs are attempted to be saved for the OS use.
466 * However, it's more important to try to map the full address space
467 * properly. */
468 if (var_state->mtrr_index >= bios_mtrrs)
469 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
470 if (var_state->mtrr_index >= total_mtrrs) {
Paul Menzel4fe98132014-01-25 15:55:28 +0100471 printk(BIOS_ERR, "ERROR: Not enough MTRRs available!\n");
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500472 return;
473 }
474
475 rbase = base;
476 rsize = size;
477
478 rbase = RANGE_TO_PHYS_ADDR(rbase);
479 rsize = RANGE_TO_PHYS_ADDR(rsize);
480 rsize = -rsize;
481
482 mask = (1ULL << var_state->address_bits) - 1;
483 rsize = rsize & mask;
484
Aaron Durbin77a5b402013-03-26 12:47:47 -0500485#if CONFIG_CACHE_ROM
486 /* CONFIG_CACHE_ROM allocates an MTRR specifically for allowing
487 * one to turn on caching for faster ROM access. However, it is
488 * left to the MTRR callers to enable it. */
489 if (mtrr_type == MTRR_TYPE_WRPROT) {
490 mtrr_type = MTRR_TYPE_UNCACHEABLE;
491 if (rom_cache_mtrr < 0)
492 rom_cache_mtrr = var_state->mtrr_index;
493 }
494#endif
495
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500496 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
497 var_state->mtrr_index, rbase, rsize, mtrr_type);
498
499 msr_val.lo = rbase;
500 msr_val.lo |= mtrr_type;
501
502 msr_val.hi = rbase >> 32;
503 msr_index = MTRRphysBase_MSR(var_state->mtrr_index);
504 wrmsr(msr_index, msr_val);
505
506 msr_val.lo = rsize;
507 msr_val.lo |= MTRRphysMaskValid;
508 msr_val.hi = rsize >> 32;
509 msr_index = MTRRphysMask_MSR(var_state->mtrr_index);
510 wrmsr(msr_index, msr_val);
511}
512
513static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
514 uint32_t base, uint32_t size, int mtrr_type)
515{
516 while (size != 0) {
517 uint32_t addr_lsb;
518 uint32_t size_msb;
519 uint32_t mtrr_size;
520
521 addr_lsb = fls(base);
522 size_msb = fms(size);
523
524 /* All MTRR entries need to have their base aligned to the mask
525 * size. The maximum size is calculated by a function of the
526 * min base bit set and maximum size bit set. */
527 if (addr_lsb > size_msb)
528 mtrr_size = 1 << size_msb;
529 else
530 mtrr_size = 1 << addr_lsb;
531
532 if (var_state->commit_mtrrs)
533 write_var_mtrr(var_state, base, mtrr_size, mtrr_type);
534
535 size -= mtrr_size;
536 base += mtrr_size;
537 var_state->mtrr_index++;
538 }
539}
540
Aaron Durbine3834422013-03-28 20:48:51 -0500541static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
542 struct range_entry *r)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500543{
Aaron Durbine3834422013-03-28 20:48:51 -0500544 uint32_t a1, a2, b1, b2;
545 int mtrr_type;
546 struct range_entry *next;
547
548 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600549 * Determine MTRRs based on the following algorithm for the given entry:
Aaron Durbine3834422013-03-28 20:48:51 -0500550 * +------------------+ b2 = ALIGN_UP(end)
551 * | 0 or more bytes | <-- hole is carved out between b1 and b2
552 * +------------------+ a2 = b1 = end
553 * | |
554 * +------------------+ a1 = begin
555 *
556 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
557 */
558 mtrr_type = range_entry_mtrr_type(r);
559
560 a1 = range_entry_base_mtrr_addr(r);
561 a2 = range_entry_end_mtrr_addr(r);
562
563 /* The end address is under 1MiB. The fixed MTRRs take
564 * precedence over the variable ones. Therefore this range
565 * can be ignored. */
566 if (a2 < RANGE_1MB)
567 return;
568
569 /* Again, the fixed MTRRs take precedence so the beginning
570 * of the range can be set to 0 if it starts below 1MiB. */
571 if (a1 < RANGE_1MB)
572 a1 = 0;
573
574 /* If the range starts above 4GiB the processing is done. */
575 if (!var_state->above4gb && a1 >= RANGE_4GB)
576 return;
577
578 /* Clip the upper address to 4GiB if addresses above 4GiB
579 * are not being processed. */
580 if (!var_state->above4gb && a2 > RANGE_4GB)
581 a2 = RANGE_4GB;
582
Aaron Durbin53924242013-03-29 11:48:27 -0500583 next = memranges_next_entry(var_state->addr_space, r);
584
Aaron Durbine3834422013-03-28 20:48:51 -0500585 b1 = a2;
Aaron Durbin53924242013-03-29 11:48:27 -0500586
Martin Roth4c3ab732013-07-08 16:23:54 -0600587 /* First check if a1 is >= 4GiB and the current entry is the last
Aaron Durbin53924242013-03-29 11:48:27 -0500588 * entry. If so perform an optimization of covering a larger range
589 * defined by the base address' alignment. */
590 if (a1 >= RANGE_4GB && next == NULL) {
591 uint32_t addr_lsb;
592
593 addr_lsb = fls(a1);
594 b2 = (1 << addr_lsb) + a1;
595 if (b2 >= a2) {
596 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
597 return;
598 }
599 }
600
601 /* Handle the min alignment roundup case. */
Aaron Durbine3834422013-03-28 20:48:51 -0500602 b2 = ALIGN_UP(a2, MTRR_MIN_ALIGN);
603
604 /* Check against the next range. If the current range_entry is the
605 * last entry then carving a hole is no problem. If the current entry
606 * isn't the last entry then check that the last entry covers the
607 * entire hole range with the default mtrr type. */
Aaron Durbine3834422013-03-28 20:48:51 -0500608 if (next != NULL &&
609 (range_entry_mtrr_type(next) != var_state->def_mtrr_type ||
610 range_entry_end_mtrr_addr(next) < b2)) {
611 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
612 return;
613 }
614
615 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
616 calc_var_mtrr_range(var_state, b1, b2 - b1, var_state->def_mtrr_type);
617}
618
619static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state,
620 struct range_entry *r)
621{
622 uint32_t a1, a2, b1, b2, c1, c2;
623 int mtrr_type;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500624
625 /*
626 * For each range that meets the non-default type process it in the
627 * following manner:
628 * +------------------+ c2 = end
629 * | 0 or more bytes |
630 * +------------------+ b2 = c1 = ALIGN_DOWN(end)
631 * | |
632 * +------------------+ b1 = a2 = ALIGN_UP(begin)
633 * | 0 or more bytes |
634 * +------------------+ a1 = begin
635 *
636 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000637 */
Aaron Durbine3834422013-03-28 20:48:51 -0500638 mtrr_type = range_entry_mtrr_type(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500639
Aaron Durbine3834422013-03-28 20:48:51 -0500640 a1 = range_entry_base_mtrr_addr(r);
641 c2 = range_entry_end_mtrr_addr(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500642
Aaron Durbine3834422013-03-28 20:48:51 -0500643 /* The end address is under 1MiB. The fixed MTRRs take
644 * precedence over the variable ones. Therefore this range
645 * can be ignored. */
646 if (c2 < RANGE_1MB)
647 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500648
Aaron Durbine3834422013-03-28 20:48:51 -0500649 /* Again, the fixed MTRRs take precedence so the beginning
650 * of the range can be set to 0 if it starts below 1MiB. */
651 if (a1 < RANGE_1MB)
652 a1 = 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500653
Aaron Durbine3834422013-03-28 20:48:51 -0500654 /* If the range starts above 4GiB the processing is done. */
655 if (!var_state->above4gb && a1 >= RANGE_4GB)
656 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500657
Aaron Durbine3834422013-03-28 20:48:51 -0500658 /* Clip the upper address to 4GiB if addresses above 4GiB
659 * are not being processed. */
660 if (!var_state->above4gb && c2 > RANGE_4GB)
661 c2 = RANGE_4GB;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500662
Aaron Durbine3834422013-03-28 20:48:51 -0500663 /* Don't align up or down on the range if it is smaller
664 * than the minimum granularity. */
665 if ((c2 - a1) < MTRR_MIN_ALIGN) {
666 calc_var_mtrr_range(var_state, a1, c2 - a1, mtrr_type);
667 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500668 }
Aaron Durbine3834422013-03-28 20:48:51 -0500669
670 b1 = a2 = ALIGN_UP(a1, MTRR_MIN_ALIGN);
671 b2 = c1 = ALIGN_DOWN(c2, MTRR_MIN_ALIGN);
672
673 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
674 calc_var_mtrr_range(var_state, b1, b2 - b1, mtrr_type);
675 calc_var_mtrr_range(var_state, c1, c2 - c1, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500676}
677
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600678static void __calc_var_mtrrs(struct memranges *addr_space,
679 int above4gb, int address_bits,
680 int *num_def_wb_mtrrs, int *num_def_uc_mtrrs)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500681{
682 int wb_deftype_count;
683 int uc_deftype_count;
Aaron Durbine3834422013-03-28 20:48:51 -0500684 struct range_entry *r;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000685 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000686
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500687 /* The default MTRR cacheability type is determined by calculating
Paul Menzel4fe98132014-01-25 15:55:28 +0100688 * the number of MTRRs required for each MTRR type as if it was the
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500689 * default. */
690 var_state.addr_space = addr_space;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000691 var_state.above4gb = above4gb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500692 var_state.address_bits = address_bits;
693 var_state.commit_mtrrs = 0;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000694
Aaron Durbine3834422013-03-28 20:48:51 -0500695 wb_deftype_count = 0;
696 uc_deftype_count = 0;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800697
Aaron Durbine3834422013-03-28 20:48:51 -0500698 /*
699 * For each range do 3 calculations:
700 * 1. UC as default type with no holes at top of range.
701 * 2. UC as default using holes at top of range.
702 * 3. WB as default.
Martin Roth4c3ab732013-07-08 16:23:54 -0600703 * The lowest count is then used as default after totaling all
704 * MTRRs. Note that the optimal algorithm for UC default is marked in
Aaron Durbine3834422013-03-28 20:48:51 -0500705 * the tag of each range regardless of final decision. UC takes
Martin Roth4c3ab732013-07-08 16:23:54 -0600706 * precedence in the MTRR architecture. Therefore, only holes can be
Aaron Durbine3834422013-03-28 20:48:51 -0500707 * used when the type of the region is MTRR_TYPE_WRBACK with
708 * MTRR_TYPE_UNCACHEABLE as the default type.
709 */
710 memranges_each_entry(r, var_state.addr_space) {
711 int mtrr_type;
712
713 mtrr_type = range_entry_mtrr_type(r);
714
715 if (mtrr_type != MTRR_TYPE_UNCACHEABLE) {
716 int uc_hole_count;
717 int uc_no_hole_count;
718
719 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
720 var_state.mtrr_index = 0;
721
722 /* No hole calculation. */
723 calc_var_mtrrs_without_hole(&var_state, r);
724 uc_no_hole_count = var_state.mtrr_index;
725
726 /* Hole calculation only if type is WB. The 64 number
727 * is a count that is unachievable, thus making it
728 * a default large number in the case of not doing
729 * the hole calculation. */
730 uc_hole_count = 64;
731 if (mtrr_type == MTRR_TYPE_WRBACK) {
732 var_state.mtrr_index = 0;
733 calc_var_mtrrs_with_hole(&var_state, r);
734 uc_hole_count = var_state.mtrr_index;
735 }
736
737 /* Mark the entry with the optimal algorithm. */
738 if (uc_no_hole_count < uc_hole_count) {
739 uc_deftype_count += uc_no_hole_count;
740 } else {
741 unsigned long new_tag;
742
743 new_tag = mtrr_type | MTRR_RANGE_UC_USE_HOLE;
744 range_entry_update_tag(r, new_tag);
745 uc_deftype_count += uc_hole_count;
746 }
747 }
748
749 if (mtrr_type != MTRR_TYPE_WRBACK) {
750 var_state.mtrr_index = 0;
751 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
752 calc_var_mtrrs_without_hole(&var_state, r);
753 wb_deftype_count += var_state.mtrr_index;
754 }
755 }
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600756 *num_def_wb_mtrrs = wb_deftype_count;
757 *num_def_uc_mtrrs = uc_deftype_count;
758}
759
760static int calc_var_mtrrs(struct memranges *addr_space,
761 int above4gb, int address_bits)
762{
763 int wb_deftype_count = 0;
764 int uc_deftype_count = 0;
765
766 __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count,
767 &uc_deftype_count);
768
769 if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) {
770 printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. "
771 "WB/UC MTRR counts: %d/%d > %d.\n",
772 wb_deftype_count, uc_deftype_count, bios_mtrrs);
773 memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB,
774 MTRR_TYPE_UNCACHEABLE);
775 __calc_var_mtrrs(addr_space, above4gb, address_bits,
776 &wb_deftype_count, &uc_deftype_count);
777 }
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000778
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500779 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
780 wb_deftype_count, uc_deftype_count);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300781
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500782 if (wb_deftype_count < uc_deftype_count) {
783 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
784 return MTRR_TYPE_WRBACK;
785 }
786 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
787 return MTRR_TYPE_UNCACHEABLE;
788}
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300789
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500790static void commit_var_mtrrs(struct memranges *addr_space, int def_type,
791 int above4gb, int address_bits)
792{
Aaron Durbine3834422013-03-28 20:48:51 -0500793 struct range_entry *r;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500794 struct var_mtrr_state var_state;
795 int i;
796
797 var_state.addr_space = addr_space;
798 var_state.above4gb = above4gb;
799 var_state.address_bits = address_bits;
800 /* Write the MSRs. */
801 var_state.commit_mtrrs = 1;
802 var_state.mtrr_index = 0;
803 var_state.def_mtrr_type = def_type;
Aaron Durbine3834422013-03-28 20:48:51 -0500804
805 memranges_each_entry(r, var_state.addr_space) {
806 if (range_entry_mtrr_type(r) == def_type)
807 continue;
808
809 if (def_type == MTRR_TYPE_UNCACHEABLE &&
810 (range_entry_tag(r) & MTRR_RANGE_UC_USE_HOLE))
811 calc_var_mtrrs_with_hole(&var_state, r);
812 else
813 calc_var_mtrrs_without_hole(&var_state, r);
814 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500815
Paul Menzel4fe98132014-01-25 15:55:28 +0100816 /* Clear all remaining variable MTRRs. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500817 for (i = var_state.mtrr_index; i < total_mtrrs; i++)
818 clear_var_mtrr(i);
819}
820
821void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
822{
823 static int mtrr_default_type = -1;
824 struct memranges *addr_space;
825
826 addr_space = get_physical_address_space();
827
828 if (mtrr_default_type == -1) {
829 if (above4gb == 2)
830 detect_var_mtrrs();
831 mtrr_default_type =
832 calc_var_mtrrs(addr_space, !!above4gb, address_bits);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000833 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700834
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500835 disable_cache();
836 commit_var_mtrrs(addr_space, mtrr_default_type, !!above4gb,
837 address_bits);
838 enable_var_mtrr(mtrr_default_type);
839 enable_cache();
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000840}
841
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100842void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000843{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100844 int address_size;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000845 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100846 address_size = cpu_phys_address_size();
847 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size);
848 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000849}
850
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000851int x86_mtrr_check(void)
852{
853 /* Only Pentium Pro and later have MTRR */
854 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000855 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000856
857 msr = rdmsr(0x2ff);
858 msr.lo >>= 10;
859
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000860 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000861 if (msr.lo & 0x01)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000862 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000863 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000864 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000865
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000866 printk(BIOS_DEBUG, "Variable MTRRs: ");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000867 if (msr.lo & 0x02)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000868 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000869 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000870 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000871
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000872 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000873
874 post_code(0x93);
875 return ((int) msr.lo);
876}