blob: 08312fbebf43e09ff5947c3d0f684e41c1a886be [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#include <cpu/amd/mtrr.h>
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -060040#if IS_ENABLED(CONFIG_X86_AMD_FIXED_MTRRS)
Aaron Durbin57686f82013-03-20 15:50:59 -050041#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
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -060086void fixed_mtrrs_expose_amd_rwdram(void)
87{
88 msr_t syscfg;
89
90 if (!IS_ENABLED(CONFIG_X86_AMD_FIXED_MTRRS))
91 return;
92
93 syscfg = rdmsr(SYSCFG_MSR);
94 syscfg.lo |= SYSCFG_MSR_MtrrFixDramModEn;
95 wrmsr(SYSCFG_MSR, syscfg);
96}
97
98void fixed_mtrrs_hide_amd_rwdram(void)
99{
100 msr_t syscfg;
101
102 if (!IS_ENABLED(CONFIG_X86_AMD_FIXED_MTRRS))
103 return;
104
105 syscfg = rdmsr(SYSCFG_MSR);
106 syscfg.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
107 wrmsr(SYSCFG_MSR, syscfg);
108}
109
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500110static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000111{
112 msr_t msr;
113
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700114 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500115 msr.lo &= ~0xff;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700116 msr.lo |= MTRR_DEF_TYPE_EN | deftype;
117 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000118}
119
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500120#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000121
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500122/* MTRRs are at a 4KiB granularity. Therefore all address calculations can
123 * be done with 32-bit numbers. This allows for the MTRR code to handle
124 * up to 2^44 bytes (16 TiB) of address space. */
125#define RANGE_SHIFT 12
126#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
127 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
128#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
129#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
130#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
131
132/* The minimum alignment while handling variable MTRR ranges is 64MiB. */
133#define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20)
134/* Helpful constants. */
135#define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20)
136#define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
137
Aaron Durbine3834422013-03-28 20:48:51 -0500138/*
139 * The default MTRR type selection uses 3 approaches for selecting the
140 * optimal number of variable MTRRs. For each range do 3 calculations:
141 * 1. UC as default type with no holes at top of range.
142 * 2. UC as default using holes at top of range.
143 * 3. WB as default.
144 * If using holes is optimal for a range when UC is the default type the
145 * tag is updated to direct the commit routine to use a hole at the top
146 * of a range.
147 */
148#define MTRR_ALGO_SHIFT (8)
149#define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1)
150/* If the default type is UC use the hole carving algorithm for a range. */
151#define MTRR_RANGE_UC_USE_HOLE (1 << MTRR_ALGO_SHIFT)
152
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500153static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000154{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500155 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000156}
157
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500158static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000159{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500160 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000161}
162
Aaron Durbine3834422013-03-28 20:48:51 -0500163static inline int range_entry_mtrr_type(struct range_entry *r)
164{
165 return range_entry_tag(r) & MTRR_TAG_MASK;
166}
167
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600168static int filter_vga_wrcomb(struct device *dev, struct resource *res)
169{
170 /* Only handle PCI devices. */
171 if (dev->path.type != DEVICE_PATH_PCI)
172 return 0;
173
174 /* Only handle VGA class devices. */
175 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
176 return 0;
177
178 /* Add resource as write-combining in the address space. */
179 return 1;
180}
181
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600182static void print_physical_address_space(const struct memranges *addr_space,
183 const char *identifier)
184{
185 const struct range_entry *r;
186
187 if (identifier)
188 printk(BIOS_DEBUG, "MTRR: %s Physical address space:\n",
189 identifier);
190 else
191 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
192
193 memranges_each_entry(r, addr_space)
194 printk(BIOS_DEBUG,
195 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
196 range_entry_base(r), range_entry_end(r),
197 range_entry_size(r), range_entry_tag(r));
198}
199
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500200static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000201{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500202 static struct memranges *addr_space;
203 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800204
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500205 /* In order to handle some chipsets not being able to pre-determine
Martin Roth4c3ab732013-07-08 16:23:54 -0600206 * uncacheable ranges, such as graphics memory, at resource insertion
207 * time remove uncacheable regions from the cacheable ones. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500208 if (addr_space == NULL) {
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500209 unsigned long mask;
210 unsigned long match;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500211
212 addr_space = &addr_space_storage;
213
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500214 mask = IORESOURCE_CACHEABLE;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500215 /* Collect cacheable and uncacheable address ranges. The
216 * uncacheable regions take precedence over the cacheable
217 * regions. */
218 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
219 memranges_add_resources(addr_space, mask, 0,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700220 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500221
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500222 /* Handle any write combining resources. Only prefetchable
Vladimir Serbinenko30fe6122014-02-05 23:25:28 +0100223 * resources are appropriate for this MTRR type. */
224 match = IORESOURCE_PREFETCH;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500225 mask |= match;
Lee Leahyc5917072017-03-15 16:38:51 -0700226 memranges_add_resources_filter(addr_space, mask, match,
227 MTRR_TYPE_WRCOMB, filter_vga_wrcomb);
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500228
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500229 /* The address space below 4GiB is special. It needs to be
Martin Roth2f914032016-01-15 10:20:11 -0700230 * covered entirely by range entries so that MTRR calculations
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500231 * can be properly done for the full 32-bit address space.
232 * Therefore, ensure holes are filled up to 4GiB as
233 * uncacheable */
234 memranges_fill_holes_up_to(addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700235 RANGE_TO_PHYS_ADDR(RANGE_4GB),
236 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500237
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600238 print_physical_address_space(addr_space, NULL);
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000239 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000240
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500241 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000242}
243
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500244/* Fixed MTRR descriptor. This structure defines the step size and begin
Martin Roth4c3ab732013-07-08 16:23:54 -0600245 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500246 * It also describes the offset in byte intervals to store the calculated MTRR
247 * type in an array. */
248struct fixed_mtrr_desc {
249 uint32_t begin;
250 uint32_t end;
251 uint32_t step;
252 int range_index;
253 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000254};
255
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500256/* Shared MTRR calculations. Can be reused by APs. */
257static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
258
259/* Fixed MTRR descriptors. */
260static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
261 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700262 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500263 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700264 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500265 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700266 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500267};
268
269static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000270{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500271 static int fixed_mtrr_types_initialized;
272 struct memranges *phys_addr_space;
273 struct range_entry *r;
274 const struct fixed_mtrr_desc *desc;
275 const struct fixed_mtrr_desc *last_desc;
276 uint32_t begin;
277 uint32_t end;
278 int type_index;
279
280 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000281 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300282
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500283 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300284
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500285 /* Set all fixed ranges to uncacheable first. */
286 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300287
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500288 desc = &fixed_mtrr_desc[0];
289 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300290
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500291 memranges_each_entry(r, phys_addr_space) {
292 begin = range_entry_base_mtrr_addr(r);
293 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300294
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500295 if (begin >= last_desc->end)
296 break;
297
298 if (end > last_desc->end)
299 end = last_desc->end;
300
301 /* Get to the correct fixed mtrr descriptor. */
302 while (begin >= desc->end)
303 desc++;
304
305 type_index = desc->range_index;
306 type_index += (begin - desc->begin) / desc->step;
307
308 while (begin != end) {
309 unsigned char type;
310
311 type = range_entry_tag(r);
312 printk(MTRR_VERBOSE_LEVEL,
313 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
314 begin, begin + desc->step, type, type_index);
315 if (type == MTRR_TYPE_WRBACK)
316 type |= MTRR_FIXED_WRBACK_BITS;
317 fixed_mtrr_types[type_index] = type;
318 type_index++;
319 begin += desc->step;
320 if (begin == desc->end)
321 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000322 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000323 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500324 fixed_mtrr_types_initialized = 1;
325}
326
327static void commit_fixed_mtrrs(void)
328{
329 int i;
330 int j;
331 int msr_num;
332 int type_index;
333 /* 8 ranges per msr. */
334 msr_t fixed_msrs[NUM_FIXED_MTRRS];
335 unsigned long msr_index[NUM_FIXED_MTRRS];
336
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -0600337 fixed_mtrrs_expose_amd_rwdram();
338
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500339 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
340
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500341 msr_num = 0;
342 type_index = 0;
343 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
344 const struct fixed_mtrr_desc *desc;
345 int num_ranges;
346
347 desc = &fixed_mtrr_desc[i];
348 num_ranges = (desc->end - desc->begin) / desc->step;
349 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
350 msr_index[msr_num] = desc->msr_index_base +
351 (j / RANGES_PER_FIXED_MTRR);
352 fixed_msrs[msr_num].lo |=
353 fixed_mtrr_types[type_index++] << 0;
354 fixed_msrs[msr_num].lo |=
355 fixed_mtrr_types[type_index++] << 8;
356 fixed_msrs[msr_num].lo |=
357 fixed_mtrr_types[type_index++] << 16;
358 fixed_msrs[msr_num].lo |=
359 fixed_mtrr_types[type_index++] << 24;
360 fixed_msrs[msr_num].hi |=
361 fixed_mtrr_types[type_index++] << 0;
362 fixed_msrs[msr_num].hi |=
363 fixed_mtrr_types[type_index++] << 8;
364 fixed_msrs[msr_num].hi |=
365 fixed_mtrr_types[type_index++] << 16;
366 fixed_msrs[msr_num].hi |=
367 fixed_mtrr_types[type_index++] << 24;
368 msr_num++;
369 }
370 }
371
Gabe Black7756fe72014-02-25 01:40:34 -0800372 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500373 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
374 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500375
Gabe Black7756fe72014-02-25 01:40:34 -0800376 disable_cache();
377 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
378 wrmsr(msr_index[i], fixed_msrs[i]);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500379 enable_cache();
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -0600380 fixed_mtrrs_hide_amd_rwdram();
381
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000382}
383
Aaron Durbin57686f82013-03-20 15:50:59 -0500384void x86_setup_fixed_mtrrs_no_enable(void)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000385{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500386 calc_fixed_mtrrs();
387 commit_fixed_mtrrs();
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000388}
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000389
Aaron Durbin57686f82013-03-20 15:50:59 -0500390void x86_setup_fixed_mtrrs(void)
391{
392 x86_setup_fixed_mtrrs_no_enable();
393
394 printk(BIOS_SPEW, "call enable_fixed_mtrr()\n");
395 enable_fixed_mtrr();
396}
397
Gabe Black7756fe72014-02-25 01:40:34 -0800398struct var_mtrr_regs {
399 msr_t base;
400 msr_t mask;
401};
402
403struct var_mtrr_solution {
404 int mtrr_default_type;
405 int num_used;
406 struct var_mtrr_regs regs[NUM_MTRR_STATIC_STORAGE];
407};
408
409/* Global storage for variable MTRR solution. */
410static struct var_mtrr_solution mtrr_global_solution;
411
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500412struct var_mtrr_state {
413 struct memranges *addr_space;
414 int above4gb;
415 int address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800416 int prepare_msrs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500417 int mtrr_index;
418 int def_mtrr_type;
Gabe Black7756fe72014-02-25 01:40:34 -0800419 struct var_mtrr_regs *regs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500420};
Aaron Durbin57686f82013-03-20 15:50:59 -0500421
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500422static void clear_var_mtrr(int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000423{
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600424 msr_t msr = { .lo = 0, .hi = 0 };
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500425
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600426 wrmsr(MTRR_PHYS_BASE(index), msr);
427 wrmsr(MTRR_PHYS_MASK(index), msr);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500428}
429
Gabe Black7756fe72014-02-25 01:40:34 -0800430static void prep_var_mtrr(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700431 uint32_t base, uint32_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500432{
Gabe Black7756fe72014-02-25 01:40:34 -0800433 struct var_mtrr_regs *regs;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500434 resource_t rbase;
435 resource_t rsize;
436 resource_t mask;
437
438 /* Some variable MTRRs are attempted to be saved for the OS use.
439 * However, it's more important to try to map the full address space
440 * properly. */
441 if (var_state->mtrr_index >= bios_mtrrs)
442 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
443 if (var_state->mtrr_index >= total_mtrrs) {
Paul Menzel6a70dbc2015-10-15 12:41:53 +0200444 printk(BIOS_ERR, "ERROR: Not enough MTRRs available! MTRR index"
445 "is %d with %d MTTRs in total.\n",
446 var_state->mtrr_index, total_mtrrs);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500447 return;
448 }
449
450 rbase = base;
451 rsize = size;
452
453 rbase = RANGE_TO_PHYS_ADDR(rbase);
454 rsize = RANGE_TO_PHYS_ADDR(rsize);
455 rsize = -rsize;
456
457 mask = (1ULL << var_state->address_bits) - 1;
458 rsize = rsize & mask;
459
460 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
461 var_state->mtrr_index, rbase, rsize, mtrr_type);
462
Gabe Black7756fe72014-02-25 01:40:34 -0800463 regs = &var_state->regs[var_state->mtrr_index];
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500464
Gabe Black7756fe72014-02-25 01:40:34 -0800465 regs->base.lo = rbase;
466 regs->base.lo |= mtrr_type;
467 regs->base.hi = rbase >> 32;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500468
Gabe Black7756fe72014-02-25 01:40:34 -0800469 regs->mask.lo = rsize;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700470 regs->mask.lo |= MTRR_PHYS_MASK_VALID;
Gabe Black7756fe72014-02-25 01:40:34 -0800471 regs->mask.hi = rsize >> 32;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500472}
473
474static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700475 uint32_t base, uint32_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500476{
477 while (size != 0) {
478 uint32_t addr_lsb;
479 uint32_t size_msb;
480 uint32_t mtrr_size;
481
482 addr_lsb = fls(base);
483 size_msb = fms(size);
484
485 /* All MTRR entries need to have their base aligned to the mask
486 * size. The maximum size is calculated by a function of the
487 * min base bit set and maximum size bit set. */
488 if (addr_lsb > size_msb)
489 mtrr_size = 1 << size_msb;
490 else
491 mtrr_size = 1 << addr_lsb;
492
Gabe Black7756fe72014-02-25 01:40:34 -0800493 if (var_state->prepare_msrs)
494 prep_var_mtrr(var_state, base, mtrr_size, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500495
496 size -= mtrr_size;
497 base += mtrr_size;
498 var_state->mtrr_index++;
499 }
500}
501
Aaron Durbine3834422013-03-28 20:48:51 -0500502static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700503 struct range_entry *r)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500504{
Aaron Durbine3834422013-03-28 20:48:51 -0500505 uint32_t a1, a2, b1, b2;
506 int mtrr_type;
507 struct range_entry *next;
508
509 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600510 * Determine MTRRs based on the following algorithm for the given entry:
Aaron Durbine3834422013-03-28 20:48:51 -0500511 * +------------------+ b2 = ALIGN_UP(end)
512 * | 0 or more bytes | <-- hole is carved out between b1 and b2
513 * +------------------+ a2 = b1 = end
514 * | |
515 * +------------------+ a1 = begin
516 *
517 * Thus, there are 3 sub-ranges to configure variable MTRRs for.
518 */
519 mtrr_type = range_entry_mtrr_type(r);
520
521 a1 = range_entry_base_mtrr_addr(r);
522 a2 = range_entry_end_mtrr_addr(r);
523
Aaron Durbina38677b2016-07-21 14:26:34 -0500524 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500525 * precedence over the variable ones. Therefore this range
526 * can be ignored. */
Aaron Durbina38677b2016-07-21 14:26:34 -0500527 if (a2 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500528 return;
529
530 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500531 * of the range can be set to 0 if it starts at or below 1MiB. */
532 if (a1 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500533 a1 = 0;
534
535 /* If the range starts above 4GiB the processing is done. */
536 if (!var_state->above4gb && a1 >= RANGE_4GB)
537 return;
538
539 /* Clip the upper address to 4GiB if addresses above 4GiB
540 * are not being processed. */
541 if (!var_state->above4gb && a2 > RANGE_4GB)
542 a2 = RANGE_4GB;
543
Aaron Durbin53924242013-03-29 11:48:27 -0500544 next = memranges_next_entry(var_state->addr_space, r);
545
Aaron Durbine3834422013-03-28 20:48:51 -0500546 b1 = a2;
Aaron Durbin53924242013-03-29 11:48:27 -0500547
Martin Roth4c3ab732013-07-08 16:23:54 -0600548 /* First check if a1 is >= 4GiB and the current entry is the last
Aaron Durbin53924242013-03-29 11:48:27 -0500549 * entry. If so perform an optimization of covering a larger range
550 * defined by the base address' alignment. */
551 if (a1 >= RANGE_4GB && next == NULL) {
552 uint32_t addr_lsb;
553
554 addr_lsb = fls(a1);
555 b2 = (1 << addr_lsb) + a1;
556 if (b2 >= a2) {
557 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
558 return;
559 }
560 }
561
562 /* Handle the min alignment roundup case. */
Aaron Durbine3834422013-03-28 20:48:51 -0500563 b2 = ALIGN_UP(a2, MTRR_MIN_ALIGN);
564
565 /* Check against the next range. If the current range_entry is the
566 * last entry then carving a hole is no problem. If the current entry
567 * isn't the last entry then check that the last entry covers the
568 * entire hole range with the default mtrr type. */
Aaron Durbine3834422013-03-28 20:48:51 -0500569 if (next != NULL &&
570 (range_entry_mtrr_type(next) != var_state->def_mtrr_type ||
571 range_entry_end_mtrr_addr(next) < b2)) {
572 calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type);
573 return;
574 }
575
576 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
577 calc_var_mtrr_range(var_state, b1, b2 - b1, var_state->def_mtrr_type);
578}
579
580static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700581 struct range_entry *r)
Aaron Durbine3834422013-03-28 20:48:51 -0500582{
Nico Huberceb52712017-10-06 19:08:51 +0200583 const int mtrr_type = range_entry_mtrr_type(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500584
Nico Huberceb52712017-10-06 19:08:51 +0200585 uint32_t base = range_entry_base_mtrr_addr(r);
586 uint32_t end = range_entry_end_mtrr_addr(r);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500587
Aaron Durbina38677b2016-07-21 14:26:34 -0500588 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500589 * precedence over the variable ones. Therefore this range
590 * can be ignored. */
Nico Huberceb52712017-10-06 19:08:51 +0200591 if (end <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500592 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500593
Aaron Durbine3834422013-03-28 20:48:51 -0500594 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500595 * of the range can be set to 0 if it starts at or below 1MiB. */
Nico Huberceb52712017-10-06 19:08:51 +0200596 if (base <= RANGE_1MB)
597 base = 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500598
Aaron Durbine3834422013-03-28 20:48:51 -0500599 /* If the range starts above 4GiB the processing is done. */
Nico Huberceb52712017-10-06 19:08:51 +0200600 if (!var_state->above4gb && base >= RANGE_4GB)
Aaron Durbine3834422013-03-28 20:48:51 -0500601 return;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500602
Aaron Durbine3834422013-03-28 20:48:51 -0500603 /* Clip the upper address to 4GiB if addresses above 4GiB
604 * are not being processed. */
Nico Huberceb52712017-10-06 19:08:51 +0200605 if (!var_state->above4gb && end > RANGE_4GB)
606 end = RANGE_4GB;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500607
Nico Huberceb52712017-10-06 19:08:51 +0200608 calc_var_mtrr_range(var_state, base, end - base, 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);