blob: f79cb8fc98cfb4f21317c0797d2037753bbf9e40 [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 *
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000019 *
20 * Reference: Intel Architecture Software Developer's Manual, Volume 3: System Programming
21 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000022
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000023#include <stddef.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050024#include <stdlib.h>
25#include <string.h>
Aaron Durbinbebf6692013-04-24 20:59:43 -050026#include <bootstate.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000027#include <console/console.h>
28#include <device/device.h>
Aaron Durbinca4f4b82014-02-08 15:41:52 -060029#include <device/pci_ids.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050030#include <cpu/cpu.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000031#include <cpu/x86/msr.h>
32#include <cpu/x86/mtrr.h>
33#include <cpu/x86/cache.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070034#include <cpu/x86/lapic.h>
Sven Schnelleadfbcb792012-01-10 12:01:43 +010035#include <arch/cpu.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070036#include <arch/acpi.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050037#include <memrange.h>
Aaron Durbin57686f82013-03-20 15:50:59 -050038#if CONFIG_X86_AMD_FIXED_MTRRS
39#include <cpu/amd/mtrr.h>
40#define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
41#else
42#define MTRR_FIXED_WRBACK_BITS 0
43#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000044
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070045/* 2 MTRRS are reserved for the operating system */
46#define BIOS_MTRRS 6
47#define OS_MTRRS 2
48#define MTRRS (BIOS_MTRRS + OS_MTRRS)
Gabe Black7756fe72014-02-25 01:40:34 -080049/*
Isaac Christensen81f90c52014-09-24 14:59:32 -060050 * Static storage size for variable MTRRs. It's sized sufficiently large to
51 * handle different types of CPUs. Empirically, 16 variable MTRRs has not
Gabe Black7756fe72014-02-25 01:40:34 -080052 * yet been observed.
53 */
54#define NUM_MTRR_STATIC_STORAGE 16
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070055
56static int total_mtrrs = MTRRS;
57static int bios_mtrrs = BIOS_MTRRS;
58
59static void detect_var_mtrrs(void)
60{
61 msr_t msr;
62
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070063 msr = rdmsr(MTRR_CAP_MSR);
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070064
65 total_mtrrs = msr.lo & 0xff;
Gabe Black7756fe72014-02-25 01:40:34 -080066
67 if (total_mtrrs > NUM_MTRR_STATIC_STORAGE) {
68 printk(BIOS_WARNING,
69 "MTRRs detected (%d) > NUM_MTRR_STATIC_STORAGE (%d)\n",
70 total_mtrrs, NUM_MTRR_STATIC_STORAGE);
71 total_mtrrs = NUM_MTRR_STATIC_STORAGE;
72 }
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070073 bios_mtrrs = total_mtrrs - OS_MTRRS;
74}
75
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000076void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000077{
78 msr_t msr;
79
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070080 msr = rdmsr(MTRR_DEF_TYPE_MSR);
81 msr.lo |= MTRR_DEF_TYPE_EN | MTRR_DEF_TYPE_FIX_EN;
82 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000083}
84
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050085static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000086{
87 msr_t msr;
88
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070089 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050090 msr.lo &= ~0xff;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070091 msr.lo |= MTRR_DEF_TYPE_EN | deftype;
92 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000093}
94
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050095#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000096
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050097/* MTRRs are at a 4KiB granularity. Therefore all address calculations can
98 * be done with 32-bit numbers. This allows for the MTRR code to handle
99 * up to 2^44 bytes (16 TiB) of address space. */
100#define RANGE_SHIFT 12
101#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
102 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
103#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
104#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
105#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
106
107/* The minimum alignment while handling variable MTRR ranges is 64MiB. */
108#define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20)
109/* Helpful constants. */
110#define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20)
111#define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
112
Aaron Durbine3834422013-03-28 20:48:51 -0500113/*
114 * The default MTRR type selection uses 3 approaches for selecting the
115 * optimal number of variable MTRRs. For each range do 3 calculations:
116 * 1. UC as default type with no holes at top of range.
117 * 2. UC as default using holes at top of range.
118 * 3. WB as default.
119 * If using holes is optimal for a range when UC is the default type the
120 * tag is updated to direct the commit routine to use a hole at the top
121 * of a range.
122 */
123#define MTRR_ALGO_SHIFT (8)
124#define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1)
125/* If the default type is UC use the hole carving algorithm for a range. */
126#define MTRR_RANGE_UC_USE_HOLE (1 << MTRR_ALGO_SHIFT)
127
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500128static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000129{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500130 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000131}
132
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500133static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000134{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500135 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000136}
137
Aaron Durbine3834422013-03-28 20:48:51 -0500138static inline int range_entry_mtrr_type(struct range_entry *r)
139{
140 return range_entry_tag(r) & MTRR_TAG_MASK;
141}
142
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600143static int filter_vga_wrcomb(struct device *dev, struct resource *res)
144{
145 /* Only handle PCI devices. */
146 if (dev->path.type != DEVICE_PATH_PCI)
147 return 0;
148
149 /* Only handle VGA class devices. */
150 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
151 return 0;
152
153 /* Add resource as write-combining in the address space. */
154 return 1;
155}
156
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600157static void print_physical_address_space(const struct memranges *addr_space,
158 const char *identifier)
159{
160 const struct range_entry *r;
161
162 if (identifier)
163 printk(BIOS_DEBUG, "MTRR: %s Physical address space:\n",
164 identifier);
165 else
166 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
167
168 memranges_each_entry(r, addr_space)
169 printk(BIOS_DEBUG,
170 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
171 range_entry_base(r), range_entry_end(r),
172 range_entry_size(r), range_entry_tag(r));
173}
174
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500175static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000176{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500177 static struct memranges *addr_space;
178 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800179
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500180 /* In order to handle some chipsets not being able to pre-determine
Martin Roth4c3ab732013-07-08 16:23:54 -0600181 * uncacheable ranges, such as graphics memory, at resource insertion
182 * time remove uncacheable regions from the cacheable ones. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500183 if (addr_space == NULL) {
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500184 unsigned long mask;
185 unsigned long match;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500186
187 addr_space = &addr_space_storage;
188
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500189 mask = IORESOURCE_CACHEABLE;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500190 /* Collect cacheable and uncacheable address ranges. The
191 * uncacheable regions take precedence over the cacheable
192 * regions. */
193 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
194 memranges_add_resources(addr_space, mask, 0,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700195 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500196
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500197 /* Handle any write combining resources. Only prefetchable
Vladimir Serbinenko30fe6122014-02-05 23:25:28 +0100198 * resources are appropriate for this MTRR type. */
199 match = IORESOURCE_PREFETCH;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500200 mask |= match;
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600201 memranges_add_resources_filter(addr_space, mask, match, MTRR_TYPE_WRCOMB,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700202 filter_vga_wrcomb);
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500203
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500204 /* The address space below 4GiB is special. It needs to be
Martin Roth2f914032016-01-15 10:20:11 -0700205 * covered entirely by range entries so that MTRR calculations
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500206 * can be properly done for the full 32-bit address space.
207 * Therefore, ensure holes are filled up to 4GiB as
208 * uncacheable */
209 memranges_fill_holes_up_to(addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700210 RANGE_TO_PHYS_ADDR(RANGE_4GB),
211 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500212
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600213 print_physical_address_space(addr_space, NULL);
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000214 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000215
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500216 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000217}
218
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500219/* Fixed MTRR descriptor. This structure defines the step size and begin
Martin Roth4c3ab732013-07-08 16:23:54 -0600220 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500221 * It also describes the offset in byte intervals to store the calculated MTRR
222 * type in an array. */
223struct fixed_mtrr_desc {
224 uint32_t begin;
225 uint32_t end;
226 uint32_t step;
227 int range_index;
228 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000229};
230
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500231/* Shared MTRR calculations. Can be reused by APs. */
232static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
233
234/* Fixed MTRR descriptors. */
235static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
236 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700237 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500238 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700239 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500240 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700241 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500242};
243
244static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000245{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500246 static int fixed_mtrr_types_initialized;
247 struct memranges *phys_addr_space;
248 struct range_entry *r;
249 const struct fixed_mtrr_desc *desc;
250 const struct fixed_mtrr_desc *last_desc;
251 uint32_t begin;
252 uint32_t end;
253 int type_index;
254
255 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000256 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300257
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500258 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300259
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500260 /* Set all fixed ranges to uncacheable first. */
261 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300262
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500263 desc = &fixed_mtrr_desc[0];
264 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300265
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500266 memranges_each_entry(r, phys_addr_space) {
267 begin = range_entry_base_mtrr_addr(r);
268 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300269
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500270 if (begin >= last_desc->end)
271 break;
272
273 if (end > last_desc->end)
274 end = last_desc->end;
275
276 /* Get to the correct fixed mtrr descriptor. */
277 while (begin >= desc->end)
278 desc++;
279
280 type_index = desc->range_index;
281 type_index += (begin - desc->begin) / desc->step;
282
283 while (begin != end) {
284 unsigned char type;
285
286 type = range_entry_tag(r);
287 printk(MTRR_VERBOSE_LEVEL,
288 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
289 begin, begin + desc->step, type, type_index);
290 if (type == MTRR_TYPE_WRBACK)
291 type |= MTRR_FIXED_WRBACK_BITS;
292 fixed_mtrr_types[type_index] = type;
293 type_index++;
294 begin += desc->step;
295 if (begin == desc->end)
296 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000297 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000298 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500299 fixed_mtrr_types_initialized = 1;
300}
301
302static void commit_fixed_mtrrs(void)
303{
304 int i;
305 int j;
306 int msr_num;
307 int type_index;
308 /* 8 ranges per msr. */
309 msr_t fixed_msrs[NUM_FIXED_MTRRS];
310 unsigned long msr_index[NUM_FIXED_MTRRS];
311
312 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
313
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500314 msr_num = 0;
315 type_index = 0;
316 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
317 const struct fixed_mtrr_desc *desc;
318 int num_ranges;
319
320 desc = &fixed_mtrr_desc[i];
321 num_ranges = (desc->end - desc->begin) / desc->step;
322 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
323 msr_index[msr_num] = desc->msr_index_base +
324 (j / RANGES_PER_FIXED_MTRR);
325 fixed_msrs[msr_num].lo |=
326 fixed_mtrr_types[type_index++] << 0;
327 fixed_msrs[msr_num].lo |=
328 fixed_mtrr_types[type_index++] << 8;
329 fixed_msrs[msr_num].lo |=
330 fixed_mtrr_types[type_index++] << 16;
331 fixed_msrs[msr_num].lo |=
332 fixed_mtrr_types[type_index++] << 24;
333 fixed_msrs[msr_num].hi |=
334 fixed_mtrr_types[type_index++] << 0;
335 fixed_msrs[msr_num].hi |=
336 fixed_mtrr_types[type_index++] << 8;
337 fixed_msrs[msr_num].hi |=
338 fixed_mtrr_types[type_index++] << 16;
339 fixed_msrs[msr_num].hi |=
340 fixed_mtrr_types[type_index++] << 24;
341 msr_num++;
342 }
343 }
344
Gabe Black7756fe72014-02-25 01:40:34 -0800345 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500346 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
347 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500348
Gabe Black7756fe72014-02-25 01:40:34 -0800349 disable_cache();
350 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
351 wrmsr(msr_index[i], fixed_msrs[i]);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500352 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
Gabe Black7756fe72014-02-25 01:40:34 -0800369struct var_mtrr_regs {
370 msr_t base;
371 msr_t mask;
372};
373
374struct var_mtrr_solution {
375 int mtrr_default_type;
376 int num_used;
377 struct var_mtrr_regs regs[NUM_MTRR_STATIC_STORAGE];
378};
379
380/* Global storage for variable MTRR solution. */
381static struct var_mtrr_solution mtrr_global_solution;
382
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500383struct var_mtrr_state {
384 struct memranges *addr_space;
385 int above4gb;
386 int address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800387 int prepare_msrs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500388 int mtrr_index;
389 int def_mtrr_type;
Gabe Black7756fe72014-02-25 01:40:34 -0800390 struct var_mtrr_regs *regs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500391};
Aaron Durbin57686f82013-03-20 15:50:59 -0500392
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500393static void clear_var_mtrr(int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000394{
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600395 msr_t msr = { .lo = 0, .hi = 0 };
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500396
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600397 wrmsr(MTRR_PHYS_BASE(index), msr);
398 wrmsr(MTRR_PHYS_MASK(index), msr);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500399}
400
Gabe Black7756fe72014-02-25 01:40:34 -0800401static void prep_var_mtrr(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700402 uint32_t base, uint32_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500403{
Gabe Black7756fe72014-02-25 01:40:34 -0800404 struct var_mtrr_regs *regs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500405 resource_t rbase;
406 resource_t rsize;
407 resource_t mask;
408
409 /* Some variable MTRRs are attempted to be saved for the OS use.
410 * However, it's more important to try to map the full address space
411 * properly. */
412 if (var_state->mtrr_index >= bios_mtrrs)
413 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
414 if (var_state->mtrr_index >= total_mtrrs) {
Paul Menzel6a70dbc2015-10-15 12:41:53 +0200415 printk(BIOS_ERR, "ERROR: Not enough MTRRs available! MTRR index"
416 "is %d with %d MTTRs in total.\n",
417 var_state->mtrr_index, total_mtrrs);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500418 return;
419 }
420
421 rbase = base;
422 rsize = size;
423
424 rbase = RANGE_TO_PHYS_ADDR(rbase);
425 rsize = RANGE_TO_PHYS_ADDR(rsize);
426 rsize = -rsize;
427
428 mask = (1ULL << var_state->address_bits) - 1;
429 rsize = rsize & mask;
430
431 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
432 var_state->mtrr_index, rbase, rsize, mtrr_type);
433
Gabe Black7756fe72014-02-25 01:40:34 -0800434 regs = &var_state->regs[var_state->mtrr_index];
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500435
Gabe Black7756fe72014-02-25 01:40:34 -0800436 regs->base.lo = rbase;
437 regs->base.lo |= mtrr_type;
438 regs->base.hi = rbase >> 32;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500439
Gabe Black7756fe72014-02-25 01:40:34 -0800440 regs->mask.lo = rsize;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700441 regs->mask.lo |= MTRR_PHYS_MASK_VALID;
Gabe Black7756fe72014-02-25 01:40:34 -0800442 regs->mask.hi = rsize >> 32;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500443}
444
445static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700446 uint32_t base, uint32_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500447{
448 while (size != 0) {
449 uint32_t addr_lsb;
450 uint32_t size_msb;
451 uint32_t mtrr_size;
452
453 addr_lsb = fls(base);
454 size_msb = fms(size);
455
456 /* All MTRR entries need to have their base aligned to the mask
457 * size. The maximum size is calculated by a function of the
458 * min base bit set and maximum size bit set. */
459 if (addr_lsb > size_msb)
460 mtrr_size = 1 << size_msb;
461 else
462 mtrr_size = 1 << addr_lsb;
463
Gabe Black7756fe72014-02-25 01:40:34 -0800464 if (var_state->prepare_msrs)
465 prep_var_mtrr(var_state, base, mtrr_size, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500466
467 size -= mtrr_size;
468 base += mtrr_size;
469 var_state->mtrr_index++;
470 }
471}
472
Aaron Durbine3834422013-03-28 20:48:51 -0500473static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700474 struct range_entry *r)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500475{
Aaron Durbine3834422013-03-28 20:48:51 -0500476 uint32_t a1, a2, b1, b2;
477 int mtrr_type;
478 struct range_entry *next;
479
480 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600481 * Determine MTRRs based on the following algorithm for the given entry:
Aaron Durbine3834422013-03-28 20:48:51 -0500482 * +------------------+ b2 = ALIGN_UP(end)
483 * | 0 or more bytes | <-- hole is carved out between b1 and b2
484 * +------------------+ a2 = b1 = end
485 * | |
486 * +------------------+ a1 = begin
487 *
488 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
489 */
490 mtrr_type = range_entry_mtrr_type(r);
491
492 a1 = range_entry_base_mtrr_addr(r);
493 a2 = range_entry_end_mtrr_addr(r);
494
Aaron Durbina38677b2016-07-21 14:26:34 -0500495 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500496 * precedence over the variable ones. Therefore this range
497 * can be ignored. */
Aaron Durbina38677b2016-07-21 14:26:34 -0500498 if (a2 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500499 return;
500
501 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500502 * of the range can be set to 0 if it starts at or below 1MiB. */
503 if (a1 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500504 a1 = 0;
505
506 /* If the range starts above 4GiB the processing is done. */
507 if (!var_state->above4gb && a1 >= RANGE_4GB)
508 return;
509
510 /* Clip the upper address to 4GiB if addresses above 4GiB
511 * are not being processed. */
512 if (!var_state->above4gb && a2 > RANGE_4GB)
513 a2 = RANGE_4GB;
514
Aaron Durbin53924242013-03-29 11:48:27 -0500515 next = memranges_next_entry(var_state->addr_space, r);
516
Aaron Durbine3834422013-03-28 20:48:51 -0500517 b1 = a2;
Aaron Durbin53924242013-03-29 11:48:27 -0500518
Martin Roth4c3ab732013-07-08 16:23:54 -0600519 /* First check if a1 is >= 4GiB and the current entry is the last
Aaron Durbin53924242013-03-29 11:48:27 -0500520 * entry. If so perform an optimization of covering a larger range
521 * defined by the base address' alignment. */
522 if (a1 >= RANGE_4GB && next == NULL) {
523 uint32_t addr_lsb;
524
525 addr_lsb = fls(a1);
526 b2 = (1 << addr_lsb) + a1;
527 if (b2 >= a2) {
528 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
529 return;
530 }
531 }
532
533 /* Handle the min alignment roundup case. */
Aaron Durbine3834422013-03-28 20:48:51 -0500534 b2 = ALIGN_UP(a2, MTRR_MIN_ALIGN);
535
536 /* Check against the next range. If the current range_entry is the
537 * last entry then carving a hole is no problem. If the current entry
538 * isn't the last entry then check that the last entry covers the
539 * entire hole range with the default mtrr type. */
Aaron Durbine3834422013-03-28 20:48:51 -0500540 if (next != NULL &&
541 (range_entry_mtrr_type(next) != var_state->def_mtrr_type ||
542 range_entry_end_mtrr_addr(next) < b2)) {
543 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
544 return;
545 }
546
547 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
548 calc_var_mtrr_range(var_state, b1, b2 - b1, var_state->def_mtrr_type);
549}
550
551static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700552 struct range_entry *r)
Aaron Durbine3834422013-03-28 20:48:51 -0500553{
554 uint32_t a1, a2, b1, b2, c1, c2;
555 int mtrr_type;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500556
557 /*
558 * For each range that meets the non-default type process it in the
559 * following manner:
560 * +------------------+ c2 = end
561 * | 0 or more bytes |
562 * +------------------+ b2 = c1 = ALIGN_DOWN(end)
563 * | |
564 * +------------------+ b1 = a2 = ALIGN_UP(begin)
565 * | 0 or more bytes |
566 * +------------------+ a1 = begin
567 *
568 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000569 */
Aaron Durbine3834422013-03-28 20:48:51 -0500570 mtrr_type = range_entry_mtrr_type(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500571
Aaron Durbine3834422013-03-28 20:48:51 -0500572 a1 = range_entry_base_mtrr_addr(r);
573 c2 = range_entry_end_mtrr_addr(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500574
Aaron Durbina38677b2016-07-21 14:26:34 -0500575 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500576 * precedence over the variable ones. Therefore this range
577 * can be ignored. */
Aaron Durbina38677b2016-07-21 14:26:34 -0500578 if (c2 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500579 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500580
Aaron Durbine3834422013-03-28 20:48:51 -0500581 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500582 * of the range can be set to 0 if it starts at or below 1MiB. */
583 if (a1 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500584 a1 = 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500585
Aaron Durbine3834422013-03-28 20:48:51 -0500586 /* If the range starts above 4GiB the processing is done. */
587 if (!var_state->above4gb && a1 >= RANGE_4GB)
588 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500589
Aaron Durbine3834422013-03-28 20:48:51 -0500590 /* Clip the upper address to 4GiB if addresses above 4GiB
591 * are not being processed. */
592 if (!var_state->above4gb && c2 > RANGE_4GB)
593 c2 = RANGE_4GB;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500594
Aaron Durbine3834422013-03-28 20:48:51 -0500595 /* Don't align up or down on the range if it is smaller
596 * than the minimum granularity. */
597 if ((c2 - a1) < MTRR_MIN_ALIGN) {
598 calc_var_mtrr_range(var_state, a1, c2 - a1, mtrr_type);
599 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500600 }
Aaron Durbine3834422013-03-28 20:48:51 -0500601
602 b1 = a2 = ALIGN_UP(a1, MTRR_MIN_ALIGN);
603 b2 = c1 = ALIGN_DOWN(c2, MTRR_MIN_ALIGN);
604
605 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
606 calc_var_mtrr_range(var_state, b1, b2 - b1, mtrr_type);
607 calc_var_mtrr_range(var_state, c1, c2 - c1, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500608}
609
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600610static void __calc_var_mtrrs(struct memranges *addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700611 int above4gb, int address_bits,
612 int *num_def_wb_mtrrs, int *num_def_uc_mtrrs)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500613{
614 int wb_deftype_count;
615 int uc_deftype_count;
Aaron Durbine3834422013-03-28 20:48:51 -0500616 struct range_entry *r;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000617 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000618
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500619 /* The default MTRR cacheability type is determined by calculating
Paul Menzel4fe98132014-01-25 15:55:28 +0100620 * the number of MTRRs required for each MTRR type as if it was the
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500621 * default. */
622 var_state.addr_space = addr_space;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000623 var_state.above4gb = above4gb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500624 var_state.address_bits = address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800625 var_state.prepare_msrs = 0;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000626
Aaron Durbine3834422013-03-28 20:48:51 -0500627 wb_deftype_count = 0;
628 uc_deftype_count = 0;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800629
Aaron Durbine3834422013-03-28 20:48:51 -0500630 /*
631 * For each range do 3 calculations:
632 * 1. UC as default type with no holes at top of range.
633 * 2. UC as default using holes at top of range.
634 * 3. WB as default.
Martin Roth4c3ab732013-07-08 16:23:54 -0600635 * The lowest count is then used as default after totaling all
636 * MTRRs. Note that the optimal algorithm for UC default is marked in
Aaron Durbine3834422013-03-28 20:48:51 -0500637 * the tag of each range regardless of final decision. UC takes
Martin Roth4c3ab732013-07-08 16:23:54 -0600638 * precedence in the MTRR architecture. Therefore, only holes can be
Aaron Durbine3834422013-03-28 20:48:51 -0500639 * used when the type of the region is MTRR_TYPE_WRBACK with
640 * MTRR_TYPE_UNCACHEABLE as the default type.
641 */
642 memranges_each_entry(r, var_state.addr_space) {
643 int mtrr_type;
644
645 mtrr_type = range_entry_mtrr_type(r);
646
647 if (mtrr_type != MTRR_TYPE_UNCACHEABLE) {
648 int uc_hole_count;
649 int uc_no_hole_count;
650
651 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
652 var_state.mtrr_index = 0;
653
654 /* No hole calculation. */
655 calc_var_mtrrs_without_hole(&var_state, r);
656 uc_no_hole_count = var_state.mtrr_index;
657
658 /* Hole calculation only if type is WB. The 64 number
659 * is a count that is unachievable, thus making it
660 * a default large number in the case of not doing
661 * the hole calculation. */
662 uc_hole_count = 64;
663 if (mtrr_type == MTRR_TYPE_WRBACK) {
664 var_state.mtrr_index = 0;
665 calc_var_mtrrs_with_hole(&var_state, r);
666 uc_hole_count = var_state.mtrr_index;
667 }
668
669 /* Mark the entry with the optimal algorithm. */
670 if (uc_no_hole_count < uc_hole_count) {
671 uc_deftype_count += uc_no_hole_count;
672 } else {
673 unsigned long new_tag;
674
675 new_tag = mtrr_type | MTRR_RANGE_UC_USE_HOLE;
676 range_entry_update_tag(r, new_tag);
677 uc_deftype_count += uc_hole_count;
678 }
679 }
680
681 if (mtrr_type != MTRR_TYPE_WRBACK) {
682 var_state.mtrr_index = 0;
683 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
684 calc_var_mtrrs_without_hole(&var_state, r);
685 wb_deftype_count += var_state.mtrr_index;
686 }
687 }
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600688 *num_def_wb_mtrrs = wb_deftype_count;
689 *num_def_uc_mtrrs = uc_deftype_count;
690}
691
692static int calc_var_mtrrs(struct memranges *addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700693 int above4gb, int address_bits)
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600694{
695 int wb_deftype_count = 0;
696 int uc_deftype_count = 0;
697
698 __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700699 &uc_deftype_count);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600700
701 if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) {
702 printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. "
703 "WB/UC MTRR counts: %d/%d > %d.\n",
704 wb_deftype_count, uc_deftype_count, bios_mtrrs);
705 memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700706 MTRR_TYPE_UNCACHEABLE);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600707 __calc_var_mtrrs(addr_space, above4gb, address_bits,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700708 &wb_deftype_count, &uc_deftype_count);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600709 }
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000710
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500711 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
712 wb_deftype_count, uc_deftype_count);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300713
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500714 if (wb_deftype_count < uc_deftype_count) {
715 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
716 return MTRR_TYPE_WRBACK;
717 }
718 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
719 return MTRR_TYPE_UNCACHEABLE;
720}
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300721
Gabe Black7756fe72014-02-25 01:40:34 -0800722static void prepare_var_mtrrs(struct memranges *addr_space, int def_type,
723 int above4gb, int address_bits,
724 struct var_mtrr_solution *sol)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500725{
Aaron Durbine3834422013-03-28 20:48:51 -0500726 struct range_entry *r;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500727 struct var_mtrr_state var_state;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500728
729 var_state.addr_space = addr_space;
730 var_state.above4gb = above4gb;
731 var_state.address_bits = address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800732 /* Prepare the MSRs. */
733 var_state.prepare_msrs = 1;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500734 var_state.mtrr_index = 0;
735 var_state.def_mtrr_type = def_type;
Gabe Black7756fe72014-02-25 01:40:34 -0800736 var_state.regs = &sol->regs[0];
Aaron Durbine3834422013-03-28 20:48:51 -0500737
738 memranges_each_entry(r, var_state.addr_space) {
739 if (range_entry_mtrr_type(r) == def_type)
740 continue;
741
742 if (def_type == MTRR_TYPE_UNCACHEABLE &&
743 (range_entry_tag(r) & MTRR_RANGE_UC_USE_HOLE))
744 calc_var_mtrrs_with_hole(&var_state, r);
745 else
746 calc_var_mtrrs_without_hole(&var_state, r);
747 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500748
Gabe Black7756fe72014-02-25 01:40:34 -0800749 /* Update the solution. */
750 sol->num_used = var_state.mtrr_index;
751}
752
753static void commit_var_mtrrs(const struct var_mtrr_solution *sol)
754{
755 int i;
756
Isaac Christensen81f90c52014-09-24 14:59:32 -0600757 /* Write out the variable MTRRs. */
Gabe Black7756fe72014-02-25 01:40:34 -0800758 disable_cache();
759 for (i = 0; i < sol->num_used; i++) {
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700760 wrmsr(MTRR_PHYS_BASE(i), sol->regs[i].base);
761 wrmsr(MTRR_PHYS_MASK(i), sol->regs[i].mask);
Gabe Black7756fe72014-02-25 01:40:34 -0800762 }
763 /* Clear the ones that are unused. */
764 for (; i < total_mtrrs; i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500765 clear_var_mtrr(i);
Isaac Christensen81f90c52014-09-24 14:59:32 -0600766 enable_var_mtrr(sol->mtrr_default_type);
Gabe Black7756fe72014-02-25 01:40:34 -0800767 enable_cache();
768
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500769}
770
771void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
772{
Gabe Black7756fe72014-02-25 01:40:34 -0800773 static struct var_mtrr_solution *sol = NULL;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500774 struct memranges *addr_space;
775
776 addr_space = get_physical_address_space();
777
Gabe Black7756fe72014-02-25 01:40:34 -0800778 if (sol == NULL) {
Gabe Black7756fe72014-02-25 01:40:34 -0800779 sol = &mtrr_global_solution;
780 sol->mtrr_default_type =
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500781 calc_var_mtrrs(addr_space, !!above4gb, address_bits);
Gabe Black7756fe72014-02-25 01:40:34 -0800782 prepare_var_mtrrs(addr_space, sol->mtrr_default_type,
783 !!above4gb, address_bits, sol);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000784 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700785
Gabe Black7756fe72014-02-25 01:40:34 -0800786 commit_var_mtrrs(sol);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000787}
788
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100789void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000790{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100791 int address_size;
Aaron Durbine63be892016-03-07 16:05:36 -0600792
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000793 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100794 address_size = cpu_phys_address_size();
Aaron Durbine63be892016-03-07 16:05:36 -0600795 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n",
796 address_size);
797 /* Always handle addresses above 4GiB. */
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100798 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000799}
800
Aaron Durbine63be892016-03-07 16:05:36 -0600801void x86_setup_mtrrs_with_detect(void)
802{
803 detect_var_mtrrs();
804 x86_setup_mtrrs();
805}
806
Kyösti Mälkki38a8fb02014-06-30 13:48:18 +0300807void x86_mtrr_check(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000808{
809 /* Only Pentium Pro and later have MTRR */
810 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000811 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000812
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700813 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000814
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000815 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700816 if (msr.lo & MTRR_DEF_TYPE_FIX_EN)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000817 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000818 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000819 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000820
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000821 printk(BIOS_DEBUG, "Variable MTRRs: ");
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700822 if (msr.lo & MTRR_DEF_TYPE_EN)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000823 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000824 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000825 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000826
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000827 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000828
829 post_code(0x93);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000830}
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600831
832static bool put_back_original_solution;
833
834void mtrr_use_temp_range(uintptr_t begin, size_t size, int type)
835{
836 const struct range_entry *r;
837 const struct memranges *orig;
838 struct var_mtrr_solution sol;
839 struct memranges addr_space;
840 const int above4gb = 1; /* Cover above 4GiB by default. */
841 int address_bits;
842
843 /* Make a copy of the original address space and tweak it with the
844 * provided range. */
845 memranges_init_empty(&addr_space, NULL, 0);
846 orig = get_physical_address_space();
847 memranges_each_entry(r, orig) {
848 unsigned long tag = range_entry_tag(r);
849
850 /* Remove any special tags from original solution. */
851 tag &= ~MTRR_RANGE_UC_USE_HOLE;
852
853 /* Remove any write combining MTRRs from the temporary
854 * solution as it just fragments the address space. */
855 if (tag == MTRR_TYPE_WRCOMB)
856 tag = MTRR_TYPE_UNCACHEABLE;
857
858 memranges_insert(&addr_space, range_entry_base(r),
859 range_entry_size(r), tag);
860 }
861
862 /* Place new range into the address space. */
863 memranges_insert(&addr_space, begin, size, type);
864
865 print_physical_address_space(&addr_space, "TEMPORARY");
866
867 /* Calculate a new solution with the updated address space. */
868 address_bits = cpu_phys_address_size();
869 memset(&sol, 0, sizeof(sol));
870 sol.mtrr_default_type =
871 calc_var_mtrrs(&addr_space, above4gb, address_bits);
872 prepare_var_mtrrs(&addr_space, sol.mtrr_default_type,
873 above4gb, address_bits, &sol);
874 commit_var_mtrrs(&sol);
875
876 memranges_teardown(&addr_space);
877 put_back_original_solution = true;
878}
879
880static void remove_temp_solution(void *unused)
881{
882 if (put_back_original_solution)
883 commit_var_mtrrs(&mtrr_global_solution);
884}
885
886BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, remove_temp_solution, NULL);
887BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, remove_temp_solution, NULL);