blob: 609d1e76db134ccface90f58e30fc20e079ef897 [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 *
Lee Leahyc5917072017-03-15 16:38:51 -070020 * Reference: Intel Architecture Software Developer's Manual, Volume 3: System
21 * Programming
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000022 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000023
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024#include <stddef.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050025#include <stdlib.h>
26#include <string.h>
Aaron Durbinbebf6692013-04-24 20:59:43 -050027#include <bootstate.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000028#include <console/console.h>
29#include <device/device.h>
Aaron Durbinca4f4b82014-02-08 15:41:52 -060030#include <device/pci_ids.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050031#include <cpu/cpu.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000032#include <cpu/x86/msr.h>
33#include <cpu/x86/mtrr.h>
34#include <cpu/x86/cache.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070035#include <cpu/x86/lapic.h>
Sven Schnelleadfbcb792012-01-10 12:01:43 +010036#include <arch/cpu.h>
Stefan Reinauer00093a82011-11-02 16:12:34 -070037#include <arch/acpi.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050038#include <memrange.h>
Aaron Durbin57686f82013-03-20 15:50:59 -050039#if CONFIG_X86_AMD_FIXED_MTRRS
40#include <cpu/amd/mtrr.h>
41#define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
42#else
43#define MTRR_FIXED_WRBACK_BITS 0
44#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000045
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070046/* 2 MTRRS are reserved for the operating system */
47#define BIOS_MTRRS 6
48#define OS_MTRRS 2
49#define MTRRS (BIOS_MTRRS + OS_MTRRS)
Gabe Black7756fe72014-02-25 01:40:34 -080050/*
Isaac Christensen81f90c52014-09-24 14:59:32 -060051 * Static storage size for variable MTRRs. It's sized sufficiently large to
52 * handle different types of CPUs. Empirically, 16 variable MTRRs has not
Gabe Black7756fe72014-02-25 01:40:34 -080053 * yet been observed.
54 */
55#define NUM_MTRR_STATIC_STORAGE 16
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070056
57static int total_mtrrs = MTRRS;
58static int bios_mtrrs = BIOS_MTRRS;
59
60static void detect_var_mtrrs(void)
61{
62 msr_t msr;
63
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070064 msr = rdmsr(MTRR_CAP_MSR);
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070065
66 total_mtrrs = msr.lo & 0xff;
Gabe Black7756fe72014-02-25 01:40:34 -080067
68 if (total_mtrrs > NUM_MTRR_STATIC_STORAGE) {
69 printk(BIOS_WARNING,
70 "MTRRs detected (%d) > NUM_MTRR_STATIC_STORAGE (%d)\n",
71 total_mtrrs, NUM_MTRR_STATIC_STORAGE);
72 total_mtrrs = NUM_MTRR_STATIC_STORAGE;
73 }
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070074 bios_mtrrs = total_mtrrs - OS_MTRRS;
75}
76
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000077void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000078{
79 msr_t msr;
80
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070081 msr = rdmsr(MTRR_DEF_TYPE_MSR);
82 msr.lo |= MTRR_DEF_TYPE_EN | MTRR_DEF_TYPE_FIX_EN;
83 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000084}
85
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050086static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000087{
88 msr_t msr;
89
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070090 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050091 msr.lo &= ~0xff;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070092 msr.lo |= MTRR_DEF_TYPE_EN | deftype;
93 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000094}
95
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050096#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000097
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050098/* MTRRs are at a 4KiB granularity. Therefore all address calculations can
99 * be done with 32-bit numbers. This allows for the MTRR code to handle
100 * up to 2^44 bytes (16 TiB) of address space. */
101#define RANGE_SHIFT 12
102#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
103 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
104#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
105#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
106#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
107
108/* The minimum alignment while handling variable MTRR ranges is 64MiB. */
109#define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20)
110/* Helpful constants. */
111#define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20)
112#define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
113
Aaron Durbine3834422013-03-28 20:48:51 -0500114/*
115 * The default MTRR type selection uses 3 approaches for selecting the
116 * optimal number of variable MTRRs. For each range do 3 calculations:
117 * 1. UC as default type with no holes at top of range.
118 * 2. UC as default using holes at top of range.
119 * 3. WB as default.
120 * If using holes is optimal for a range when UC is the default type the
121 * tag is updated to direct the commit routine to use a hole at the top
122 * of a range.
123 */
124#define MTRR_ALGO_SHIFT (8)
125#define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1)
126/* If the default type is UC use the hole carving algorithm for a range. */
127#define MTRR_RANGE_UC_USE_HOLE (1 << MTRR_ALGO_SHIFT)
128
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500129static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000130{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500131 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000132}
133
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500134static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000135{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500136 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000137}
138
Aaron Durbine3834422013-03-28 20:48:51 -0500139static inline int range_entry_mtrr_type(struct range_entry *r)
140{
141 return range_entry_tag(r) & MTRR_TAG_MASK;
142}
143
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600144static int filter_vga_wrcomb(struct device *dev, struct resource *res)
145{
146 /* Only handle PCI devices. */
147 if (dev->path.type != DEVICE_PATH_PCI)
148 return 0;
149
150 /* Only handle VGA class devices. */
151 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
152 return 0;
153
154 /* Add resource as write-combining in the address space. */
155 return 1;
156}
157
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600158static void print_physical_address_space(const struct memranges *addr_space,
159 const char *identifier)
160{
161 const struct range_entry *r;
162
163 if (identifier)
164 printk(BIOS_DEBUG, "MTRR: %s Physical address space:\n",
165 identifier);
166 else
167 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
168
169 memranges_each_entry(r, addr_space)
170 printk(BIOS_DEBUG,
171 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
172 range_entry_base(r), range_entry_end(r),
173 range_entry_size(r), range_entry_tag(r));
174}
175
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500176static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000177{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500178 static struct memranges *addr_space;
179 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800180
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500181 /* In order to handle some chipsets not being able to pre-determine
Martin Roth4c3ab732013-07-08 16:23:54 -0600182 * uncacheable ranges, such as graphics memory, at resource insertion
183 * time remove uncacheable regions from the cacheable ones. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500184 if (addr_space == NULL) {
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500185 unsigned long mask;
186 unsigned long match;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500187
188 addr_space = &addr_space_storage;
189
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500190 mask = IORESOURCE_CACHEABLE;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500191 /* Collect cacheable and uncacheable address ranges. The
192 * uncacheable regions take precedence over the cacheable
193 * regions. */
194 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
195 memranges_add_resources(addr_space, mask, 0,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700196 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500197
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500198 /* Handle any write combining resources. Only prefetchable
Vladimir Serbinenko30fe6122014-02-05 23:25:28 +0100199 * resources are appropriate for this MTRR type. */
200 match = IORESOURCE_PREFETCH;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500201 mask |= match;
Lee Leahyc5917072017-03-15 16:38:51 -0700202 memranges_add_resources_filter(addr_space, mask, match,
203 MTRR_TYPE_WRCOMB, filter_vga_wrcomb);
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500204
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500205 /* The address space below 4GiB is special. It needs to be
Martin Roth2f914032016-01-15 10:20:11 -0700206 * covered entirely by range entries so that MTRR calculations
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500207 * can be properly done for the full 32-bit address space.
208 * Therefore, ensure holes are filled up to 4GiB as
209 * uncacheable */
210 memranges_fill_holes_up_to(addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700211 RANGE_TO_PHYS_ADDR(RANGE_4GB),
212 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500213
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600214 print_physical_address_space(addr_space, NULL);
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000215 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000216
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500217 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000218}
219
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500220/* Fixed MTRR descriptor. This structure defines the step size and begin
Martin Roth4c3ab732013-07-08 16:23:54 -0600221 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500222 * It also describes the offset in byte intervals to store the calculated MTRR
223 * type in an array. */
224struct fixed_mtrr_desc {
225 uint32_t begin;
226 uint32_t end;
227 uint32_t step;
228 int range_index;
229 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000230};
231
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500232/* Shared MTRR calculations. Can be reused by APs. */
233static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
234
235/* Fixed MTRR descriptors. */
236static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
237 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700238 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500239 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700240 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500241 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700242 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500243};
244
245static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000246{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500247 static int fixed_mtrr_types_initialized;
248 struct memranges *phys_addr_space;
249 struct range_entry *r;
250 const struct fixed_mtrr_desc *desc;
251 const struct fixed_mtrr_desc *last_desc;
252 uint32_t begin;
253 uint32_t end;
254 int type_index;
255
256 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000257 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300258
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500259 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300260
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500261 /* Set all fixed ranges to uncacheable first. */
262 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300263
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500264 desc = &fixed_mtrr_desc[0];
265 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300266
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500267 memranges_each_entry(r, phys_addr_space) {
268 begin = range_entry_base_mtrr_addr(r);
269 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300270
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500271 if (begin >= last_desc->end)
272 break;
273
274 if (end > last_desc->end)
275 end = last_desc->end;
276
277 /* Get to the correct fixed mtrr descriptor. */
278 while (begin >= desc->end)
279 desc++;
280
281 type_index = desc->range_index;
282 type_index += (begin - desc->begin) / desc->step;
283
284 while (begin != end) {
285 unsigned char type;
286
287 type = range_entry_tag(r);
288 printk(MTRR_VERBOSE_LEVEL,
289 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
290 begin, begin + desc->step, type, type_index);
291 if (type == MTRR_TYPE_WRBACK)
292 type |= MTRR_FIXED_WRBACK_BITS;
293 fixed_mtrr_types[type_index] = type;
294 type_index++;
295 begin += desc->step;
296 if (begin == desc->end)
297 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000298 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000299 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500300 fixed_mtrr_types_initialized = 1;
301}
302
303static void commit_fixed_mtrrs(void)
304{
305 int i;
306 int j;
307 int msr_num;
308 int type_index;
309 /* 8 ranges per msr. */
310 msr_t fixed_msrs[NUM_FIXED_MTRRS];
311 unsigned long msr_index[NUM_FIXED_MTRRS];
312
313 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
314
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500315 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
Gabe Black7756fe72014-02-25 01:40:34 -0800346 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500347 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
348 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500349
Gabe Black7756fe72014-02-25 01:40:34 -0800350 disable_cache();
351 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
352 wrmsr(msr_index[i], fixed_msrs[i]);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500353 enable_cache();
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000354}
355
Aaron Durbin57686f82013-03-20 15:50:59 -0500356void x86_setup_fixed_mtrrs_no_enable(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000357{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500358 calc_fixed_mtrrs();
359 commit_fixed_mtrrs();
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000360}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000361
Aaron Durbin57686f82013-03-20 15:50:59 -0500362void x86_setup_fixed_mtrrs(void)
363{
364 x86_setup_fixed_mtrrs_no_enable();
365
366 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
367 enable_fixed_mtrr();
368}
369
Gabe Black7756fe72014-02-25 01:40:34 -0800370struct var_mtrr_regs {
371 msr_t base;
372 msr_t mask;
373};
374
375struct var_mtrr_solution {
376 int mtrr_default_type;
377 int num_used;
378 struct var_mtrr_regs regs[NUM_MTRR_STATIC_STORAGE];
379};
380
381/* Global storage for variable MTRR solution. */
382static struct var_mtrr_solution mtrr_global_solution;
383
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500384struct var_mtrr_state {
385 struct memranges *addr_space;
386 int above4gb;
387 int address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800388 int prepare_msrs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500389 int mtrr_index;
390 int def_mtrr_type;
Gabe Black7756fe72014-02-25 01:40:34 -0800391 struct var_mtrr_regs *regs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500392};
Aaron Durbin57686f82013-03-20 15:50:59 -0500393
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500394static void clear_var_mtrr(int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000395{
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600396 msr_t msr = { .lo = 0, .hi = 0 };
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500397
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600398 wrmsr(MTRR_PHYS_BASE(index), msr);
399 wrmsr(MTRR_PHYS_MASK(index), msr);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500400}
401
Gabe Black7756fe72014-02-25 01:40:34 -0800402static void prep_var_mtrr(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700403 uint32_t base, uint32_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500404{
Gabe Black7756fe72014-02-25 01:40:34 -0800405 struct var_mtrr_regs *regs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500406 resource_t rbase;
407 resource_t rsize;
408 resource_t mask;
409
410 /* Some variable MTRRs are attempted to be saved for the OS use.
411 * However, it's more important to try to map the full address space
412 * properly. */
413 if (var_state->mtrr_index >= bios_mtrrs)
414 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
415 if (var_state->mtrr_index >= total_mtrrs) {
Paul Menzel6a70dbc2015-10-15 12:41:53 +0200416 printk(BIOS_ERR, "ERROR: Not enough MTRRs available! MTRR index"
417 "is %d with %d MTTRs in total.\n",
418 var_state->mtrr_index, total_mtrrs);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500419 return;
420 }
421
422 rbase = base;
423 rsize = size;
424
425 rbase = RANGE_TO_PHYS_ADDR(rbase);
426 rsize = RANGE_TO_PHYS_ADDR(rsize);
427 rsize = -rsize;
428
429 mask = (1ULL << var_state->address_bits) - 1;
430 rsize = rsize & mask;
431
432 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
433 var_state->mtrr_index, rbase, rsize, mtrr_type);
434
Gabe Black7756fe72014-02-25 01:40:34 -0800435 regs = &var_state->regs[var_state->mtrr_index];
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500436
Gabe Black7756fe72014-02-25 01:40:34 -0800437 regs->base.lo = rbase;
438 regs->base.lo |= mtrr_type;
439 regs->base.hi = rbase >> 32;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500440
Gabe Black7756fe72014-02-25 01:40:34 -0800441 regs->mask.lo = rsize;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700442 regs->mask.lo |= MTRR_PHYS_MASK_VALID;
Gabe Black7756fe72014-02-25 01:40:34 -0800443 regs->mask.hi = rsize >> 32;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500444}
445
446static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700447 uint32_t base, uint32_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500448{
449 while (size != 0) {
450 uint32_t addr_lsb;
451 uint32_t size_msb;
452 uint32_t mtrr_size;
453
454 addr_lsb = fls(base);
455 size_msb = fms(size);
456
457 /* All MTRR entries need to have their base aligned to the mask
458 * size. The maximum size is calculated by a function of the
459 * min base bit set and maximum size bit set. */
460 if (addr_lsb > size_msb)
461 mtrr_size = 1 << size_msb;
462 else
463 mtrr_size = 1 << addr_lsb;
464
Gabe Black7756fe72014-02-25 01:40:34 -0800465 if (var_state->prepare_msrs)
466 prep_var_mtrr(var_state, base, mtrr_size, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500467
468 size -= mtrr_size;
469 base += mtrr_size;
470 var_state->mtrr_index++;
471 }
472}
473
Aaron Durbine3834422013-03-28 20:48:51 -0500474static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700475 struct range_entry *r)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500476{
Aaron Durbine3834422013-03-28 20:48:51 -0500477 uint32_t a1, a2, b1, b2;
478 int mtrr_type;
479 struct range_entry *next;
480
481 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600482 * Determine MTRRs based on the following algorithm for the given entry:
Aaron Durbine3834422013-03-28 20:48:51 -0500483 * +------------------+ b2 = ALIGN_UP(end)
484 * | 0 or more bytes | <-- hole is carved out between b1 and b2
485 * +------------------+ a2 = b1 = end
486 * | |
487 * +------------------+ a1 = begin
488 *
489 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
490 */
491 mtrr_type = range_entry_mtrr_type(r);
492
493 a1 = range_entry_base_mtrr_addr(r);
494 a2 = range_entry_end_mtrr_addr(r);
495
Aaron Durbina38677b2016-07-21 14:26:34 -0500496 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500497 * precedence over the variable ones. Therefore this range
498 * can be ignored. */
Aaron Durbina38677b2016-07-21 14:26:34 -0500499 if (a2 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500500 return;
501
502 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500503 * of the range can be set to 0 if it starts at or below 1MiB. */
504 if (a1 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500505 a1 = 0;
506
507 /* If the range starts above 4GiB the processing is done. */
508 if (!var_state->above4gb && a1 >= RANGE_4GB)
509 return;
510
511 /* Clip the upper address to 4GiB if addresses above 4GiB
512 * are not being processed. */
513 if (!var_state->above4gb && a2 > RANGE_4GB)
514 a2 = RANGE_4GB;
515
Aaron Durbin53924242013-03-29 11:48:27 -0500516 next = memranges_next_entry(var_state->addr_space, r);
517
Aaron Durbine3834422013-03-28 20:48:51 -0500518 b1 = a2;
Aaron Durbin53924242013-03-29 11:48:27 -0500519
Martin Roth4c3ab732013-07-08 16:23:54 -0600520 /* First check if a1 is >= 4GiB and the current entry is the last
Aaron Durbin53924242013-03-29 11:48:27 -0500521 * entry. If so perform an optimization of covering a larger range
522 * defined by the base address' alignment. */
523 if (a1 >= RANGE_4GB && next == NULL) {
524 uint32_t addr_lsb;
525
526 addr_lsb = fls(a1);
527 b2 = (1 << addr_lsb) + a1;
528 if (b2 >= a2) {
529 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
530 return;
531 }
532 }
533
534 /* Handle the min alignment roundup case. */
Aaron Durbine3834422013-03-28 20:48:51 -0500535 b2 = ALIGN_UP(a2, MTRR_MIN_ALIGN);
536
537 /* Check against the next range. If the current range_entry is the
538 * last entry then carving a hole is no problem. If the current entry
539 * isn't the last entry then check that the last entry covers the
540 * entire hole range with the default mtrr type. */
Aaron Durbine3834422013-03-28 20:48:51 -0500541 if (next != NULL &&
542 (range_entry_mtrr_type(next) != var_state->def_mtrr_type ||
543 range_entry_end_mtrr_addr(next) < b2)) {
544 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
545 return;
546 }
547
548 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
549 calc_var_mtrr_range(var_state, b1, b2 - b1, var_state->def_mtrr_type);
550}
551
552static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700553 struct range_entry *r)
Aaron Durbine3834422013-03-28 20:48:51 -0500554{
555 uint32_t a1, a2, b1, b2, c1, c2;
556 int mtrr_type;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500557
558 /*
559 * For each range that meets the non-default type process it in the
560 * following manner:
561 * +------------------+ c2 = end
562 * | 0 or more bytes |
563 * +------------------+ b2 = c1 = ALIGN_DOWN(end)
564 * | |
565 * +------------------+ b1 = a2 = ALIGN_UP(begin)
566 * | 0 or more bytes |
567 * +------------------+ a1 = begin
568 *
569 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000570 */
Aaron Durbine3834422013-03-28 20:48:51 -0500571 mtrr_type = range_entry_mtrr_type(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500572
Aaron Durbine3834422013-03-28 20:48:51 -0500573 a1 = range_entry_base_mtrr_addr(r);
574 c2 = range_entry_end_mtrr_addr(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500575
Aaron Durbina38677b2016-07-21 14:26:34 -0500576 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500577 * precedence over the variable ones. Therefore this range
578 * can be ignored. */
Aaron Durbina38677b2016-07-21 14:26:34 -0500579 if (c2 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500580 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500581
Aaron Durbine3834422013-03-28 20:48:51 -0500582 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500583 * of the range can be set to 0 if it starts at or below 1MiB. */
584 if (a1 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500585 a1 = 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500586
Aaron Durbine3834422013-03-28 20:48:51 -0500587 /* If the range starts above 4GiB the processing is done. */
588 if (!var_state->above4gb && a1 >= RANGE_4GB)
589 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500590
Aaron Durbine3834422013-03-28 20:48:51 -0500591 /* Clip the upper address to 4GiB if addresses above 4GiB
592 * are not being processed. */
593 if (!var_state->above4gb && c2 > RANGE_4GB)
594 c2 = RANGE_4GB;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500595
Aaron Durbine3834422013-03-28 20:48:51 -0500596 /* Don't align up or down on the range if it is smaller
597 * than the minimum granularity. */
598 if ((c2 - a1) < MTRR_MIN_ALIGN) {
599 calc_var_mtrr_range(var_state, a1, c2 - a1, mtrr_type);
600 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500601 }
Aaron Durbine3834422013-03-28 20:48:51 -0500602
603 b1 = a2 = ALIGN_UP(a1, MTRR_MIN_ALIGN);
604 b2 = c1 = ALIGN_DOWN(c2, MTRR_MIN_ALIGN);
605
606 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
607 calc_var_mtrr_range(var_state, b1, b2 - b1, mtrr_type);
608 calc_var_mtrr_range(var_state, c1, c2 - c1, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500609}
610
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600611static void __calc_var_mtrrs(struct memranges *addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700612 int above4gb, int address_bits,
613 int *num_def_wb_mtrrs, int *num_def_uc_mtrrs)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500614{
615 int wb_deftype_count;
616 int uc_deftype_count;
Aaron Durbine3834422013-03-28 20:48:51 -0500617 struct range_entry *r;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000618 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000619
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500620 /* The default MTRR cacheability type is determined by calculating
Paul Menzel4fe98132014-01-25 15:55:28 +0100621 * the number of MTRRs required for each MTRR type as if it was the
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500622 * default. */
623 var_state.addr_space = addr_space;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000624 var_state.above4gb = above4gb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500625 var_state.address_bits = address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800626 var_state.prepare_msrs = 0;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000627
Aaron Durbine3834422013-03-28 20:48:51 -0500628 wb_deftype_count = 0;
629 uc_deftype_count = 0;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800630
Aaron Durbine3834422013-03-28 20:48:51 -0500631 /*
632 * For each range do 3 calculations:
633 * 1. UC as default type with no holes at top of range.
634 * 2. UC as default using holes at top of range.
635 * 3. WB as default.
Martin Roth4c3ab732013-07-08 16:23:54 -0600636 * The lowest count is then used as default after totaling all
637 * MTRRs. Note that the optimal algorithm for UC default is marked in
Aaron Durbine3834422013-03-28 20:48:51 -0500638 * the tag of each range regardless of final decision. UC takes
Martin Roth4c3ab732013-07-08 16:23:54 -0600639 * precedence in the MTRR architecture. Therefore, only holes can be
Aaron Durbine3834422013-03-28 20:48:51 -0500640 * used when the type of the region is MTRR_TYPE_WRBACK with
641 * MTRR_TYPE_UNCACHEABLE as the default type.
642 */
643 memranges_each_entry(r, var_state.addr_space) {
644 int mtrr_type;
645
646 mtrr_type = range_entry_mtrr_type(r);
647
648 if (mtrr_type != MTRR_TYPE_UNCACHEABLE) {
649 int uc_hole_count;
650 int uc_no_hole_count;
651
652 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
653 var_state.mtrr_index = 0;
654
655 /* No hole calculation. */
656 calc_var_mtrrs_without_hole(&var_state, r);
657 uc_no_hole_count = var_state.mtrr_index;
658
659 /* Hole calculation only if type is WB. The 64 number
660 * is a count that is unachievable, thus making it
661 * a default large number in the case of not doing
662 * the hole calculation. */
663 uc_hole_count = 64;
664 if (mtrr_type == MTRR_TYPE_WRBACK) {
665 var_state.mtrr_index = 0;
666 calc_var_mtrrs_with_hole(&var_state, r);
667 uc_hole_count = var_state.mtrr_index;
668 }
669
670 /* Mark the entry with the optimal algorithm. */
671 if (uc_no_hole_count < uc_hole_count) {
672 uc_deftype_count += uc_no_hole_count;
673 } else {
674 unsigned long new_tag;
675
676 new_tag = mtrr_type | MTRR_RANGE_UC_USE_HOLE;
677 range_entry_update_tag(r, new_tag);
678 uc_deftype_count += uc_hole_count;
679 }
680 }
681
682 if (mtrr_type != MTRR_TYPE_WRBACK) {
683 var_state.mtrr_index = 0;
684 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
685 calc_var_mtrrs_without_hole(&var_state, r);
686 wb_deftype_count += var_state.mtrr_index;
687 }
688 }
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600689 *num_def_wb_mtrrs = wb_deftype_count;
690 *num_def_uc_mtrrs = uc_deftype_count;
691}
692
693static int calc_var_mtrrs(struct memranges *addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700694 int above4gb, int address_bits)
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600695{
696 int wb_deftype_count = 0;
697 int uc_deftype_count = 0;
698
699 __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700700 &uc_deftype_count);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600701
702 if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) {
703 printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. "
704 "WB/UC MTRR counts: %d/%d > %d.\n",
705 wb_deftype_count, uc_deftype_count, bios_mtrrs);
706 memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700707 MTRR_TYPE_UNCACHEABLE);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600708 __calc_var_mtrrs(addr_space, above4gb, address_bits,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700709 &wb_deftype_count, &uc_deftype_count);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600710 }
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000711
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500712 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
713 wb_deftype_count, uc_deftype_count);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300714
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500715 if (wb_deftype_count < uc_deftype_count) {
716 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
717 return MTRR_TYPE_WRBACK;
718 }
719 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
720 return MTRR_TYPE_UNCACHEABLE;
721}
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300722
Gabe Black7756fe72014-02-25 01:40:34 -0800723static void prepare_var_mtrrs(struct memranges *addr_space, int def_type,
724 int above4gb, int address_bits,
725 struct var_mtrr_solution *sol)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500726{
Aaron Durbine3834422013-03-28 20:48:51 -0500727 struct range_entry *r;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500728 struct var_mtrr_state var_state;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500729
730 var_state.addr_space = addr_space;
731 var_state.above4gb = above4gb;
732 var_state.address_bits = address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800733 /* Prepare the MSRs. */
734 var_state.prepare_msrs = 1;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500735 var_state.mtrr_index = 0;
736 var_state.def_mtrr_type = def_type;
Gabe Black7756fe72014-02-25 01:40:34 -0800737 var_state.regs = &sol->regs[0];
Aaron Durbine3834422013-03-28 20:48:51 -0500738
739 memranges_each_entry(r, var_state.addr_space) {
740 if (range_entry_mtrr_type(r) == def_type)
741 continue;
742
743 if (def_type == MTRR_TYPE_UNCACHEABLE &&
744 (range_entry_tag(r) & MTRR_RANGE_UC_USE_HOLE))
745 calc_var_mtrrs_with_hole(&var_state, r);
746 else
747 calc_var_mtrrs_without_hole(&var_state, r);
748 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500749
Gabe Black7756fe72014-02-25 01:40:34 -0800750 /* Update the solution. */
751 sol->num_used = var_state.mtrr_index;
752}
753
Aaron Durbind9762f72017-06-12 12:48:38 -0500754static int commit_var_mtrrs(const struct var_mtrr_solution *sol)
Gabe Black7756fe72014-02-25 01:40:34 -0800755{
756 int i;
757
Aaron Durbind9762f72017-06-12 12:48:38 -0500758 if (sol->num_used > total_mtrrs) {
759 printk(BIOS_WARNING, "Not enough MTRRs: %d vs %d\n",
760 sol->num_used, total_mtrrs);
761 return -1;
762 }
763
Isaac Christensen81f90c52014-09-24 14:59:32 -0600764 /* Write out the variable MTRRs. */
Gabe Black7756fe72014-02-25 01:40:34 -0800765 disable_cache();
766 for (i = 0; i < sol->num_used; i++) {
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700767 wrmsr(MTRR_PHYS_BASE(i), sol->regs[i].base);
768 wrmsr(MTRR_PHYS_MASK(i), sol->regs[i].mask);
Gabe Black7756fe72014-02-25 01:40:34 -0800769 }
770 /* Clear the ones that are unused. */
771 for (; i < total_mtrrs; i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500772 clear_var_mtrr(i);
Isaac Christensen81f90c52014-09-24 14:59:32 -0600773 enable_var_mtrr(sol->mtrr_default_type);
Gabe Black7756fe72014-02-25 01:40:34 -0800774 enable_cache();
775
Aaron Durbind9762f72017-06-12 12:48:38 -0500776 return 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500777}
778
779void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
780{
Gabe Black7756fe72014-02-25 01:40:34 -0800781 static struct var_mtrr_solution *sol = NULL;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500782 struct memranges *addr_space;
783
784 addr_space = get_physical_address_space();
785
Gabe Black7756fe72014-02-25 01:40:34 -0800786 if (sol == NULL) {
Gabe Black7756fe72014-02-25 01:40:34 -0800787 sol = &mtrr_global_solution;
788 sol->mtrr_default_type =
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500789 calc_var_mtrrs(addr_space, !!above4gb, address_bits);
Gabe Black7756fe72014-02-25 01:40:34 -0800790 prepare_var_mtrrs(addr_space, sol->mtrr_default_type,
791 !!above4gb, address_bits, sol);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000792 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700793
Gabe Black7756fe72014-02-25 01:40:34 -0800794 commit_var_mtrrs(sol);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000795}
796
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100797void x86_setup_mtrrs(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000798{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100799 int address_size;
Aaron Durbine63be892016-03-07 16:05:36 -0600800
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000801 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100802 address_size = cpu_phys_address_size();
Aaron Durbine63be892016-03-07 16:05:36 -0600803 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n",
804 address_size);
805 /* Always handle addresses above 4GiB. */
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100806 x86_setup_var_mtrrs(address_size, 1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000807}
808
Aaron Durbine63be892016-03-07 16:05:36 -0600809void x86_setup_mtrrs_with_detect(void)
810{
811 detect_var_mtrrs();
812 x86_setup_mtrrs();
813}
814
Kyösti Mälkki38a8fb02014-06-30 13:48:18 +0300815void x86_mtrr_check(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000816{
817 /* Only Pentium Pro and later have MTRR */
818 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000819 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000820
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700821 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000822
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000823 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700824 if (msr.lo & MTRR_DEF_TYPE_FIX_EN)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000825 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000826 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000827 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000828
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000829 printk(BIOS_DEBUG, "Variable MTRRs: ");
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700830 if (msr.lo & MTRR_DEF_TYPE_EN)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000831 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000832 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000833 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000834
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000835 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000836
837 post_code(0x93);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000838}
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600839
840static bool put_back_original_solution;
841
842void mtrr_use_temp_range(uintptr_t begin, size_t size, int type)
843{
844 const struct range_entry *r;
845 const struct memranges *orig;
846 struct var_mtrr_solution sol;
847 struct memranges addr_space;
848 const int above4gb = 1; /* Cover above 4GiB by default. */
849 int address_bits;
850
851 /* Make a copy of the original address space and tweak it with the
852 * provided range. */
853 memranges_init_empty(&addr_space, NULL, 0);
854 orig = get_physical_address_space();
855 memranges_each_entry(r, orig) {
856 unsigned long tag = range_entry_tag(r);
857
858 /* Remove any special tags from original solution. */
859 tag &= ~MTRR_RANGE_UC_USE_HOLE;
860
861 /* Remove any write combining MTRRs from the temporary
862 * solution as it just fragments the address space. */
863 if (tag == MTRR_TYPE_WRCOMB)
864 tag = MTRR_TYPE_UNCACHEABLE;
865
866 memranges_insert(&addr_space, range_entry_base(r),
867 range_entry_size(r), tag);
868 }
869
870 /* Place new range into the address space. */
871 memranges_insert(&addr_space, begin, size, type);
872
873 print_physical_address_space(&addr_space, "TEMPORARY");
874
875 /* Calculate a new solution with the updated address space. */
876 address_bits = cpu_phys_address_size();
877 memset(&sol, 0, sizeof(sol));
878 sol.mtrr_default_type =
879 calc_var_mtrrs(&addr_space, above4gb, address_bits);
880 prepare_var_mtrrs(&addr_space, sol.mtrr_default_type,
881 above4gb, address_bits, &sol);
Aaron Durbind9762f72017-06-12 12:48:38 -0500882
883 if (commit_var_mtrrs(&sol) < 0)
884 printk(BIOS_WARNING, "Unable to insert temporary MTRR range: 0x%016llx - 0x%016llx size 0x%08llx type %d\n",
885 (long long)begin, (long long)begin + size,
886 (long long)size, type);
887 else
888 put_back_original_solution = true;
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600889
890 memranges_teardown(&addr_space);
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600891}
892
893static void remove_temp_solution(void *unused)
894{
895 if (put_back_original_solution)
896 commit_var_mtrrs(&mtrr_global_solution);
897}
898
899BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, remove_temp_solution, NULL);
900BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, remove_temp_solution, NULL);