blob: cb7ecdc96345523d921743249309975a0f0353a9 [file] [log] [blame]
Elyes HAOUAS3a7346c2020-05-07 07:46:17 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00003/*
Martin Rothd57ace22019-08-31 10:48:37 -06004 * mtrr.c: setting MTRR to decent values for cache initialization on P6
5 * Derived from intel_set_mtrr in intel_subr.c and mtrr.c in linux kernel
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00006 *
Lee Leahyc5917072017-03-15 16:38:51 -07007 * Reference: Intel Architecture Software Developer's Manual, Volume 3: System
8 * Programming
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00009 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000010
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000011#include <stddef.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050012#include <string.h>
Aaron Durbinbebf6692013-04-24 20:59:43 -050013#include <bootstate.h>
Elyes HAOUASd26844c2019-06-21 07:31:40 +020014#include <commonlib/helpers.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000015#include <console/console.h>
16#include <device/device.h>
Aaron Durbinca4f4b82014-02-08 15:41:52 -060017#include <device/pci_ids.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050018#include <cpu/cpu.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000019#include <cpu/x86/msr.h>
20#include <cpu/x86/mtrr.h>
21#include <cpu/x86/cache.h>
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050022#include <memrange.h>
Aaron Durbin57686f82013-03-20 15:50:59 -050023#include <cpu/amd/mtrr.h>
Richard Spiegelb28025a2019-02-20 11:00:19 -070024#include <assert.h>
Julius Wernercd49cce2019-03-05 16:53:33 -080025#if CONFIG(X86_AMD_FIXED_MTRRS)
Aaron Durbin57686f82013-03-20 15:50:59 -050026#define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
27#else
28#define MTRR_FIXED_WRBACK_BITS 0
29#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000030
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070031/* 2 MTRRS are reserved for the operating system */
32#define BIOS_MTRRS 6
33#define OS_MTRRS 2
34#define MTRRS (BIOS_MTRRS + OS_MTRRS)
Gabe Black7756fe72014-02-25 01:40:34 -080035/*
Isaac Christensen81f90c52014-09-24 14:59:32 -060036 * Static storage size for variable MTRRs. It's sized sufficiently large to
37 * handle different types of CPUs. Empirically, 16 variable MTRRs has not
Gabe Black7756fe72014-02-25 01:40:34 -080038 * yet been observed.
39 */
40#define NUM_MTRR_STATIC_STORAGE 16
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070041
42static int total_mtrrs = MTRRS;
43static int bios_mtrrs = BIOS_MTRRS;
44
45static void detect_var_mtrrs(void)
46{
47 msr_t msr;
48
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070049 msr = rdmsr(MTRR_CAP_MSR);
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070050
51 total_mtrrs = msr.lo & 0xff;
Gabe Black7756fe72014-02-25 01:40:34 -080052
53 if (total_mtrrs > NUM_MTRR_STATIC_STORAGE) {
54 printk(BIOS_WARNING,
55 "MTRRs detected (%d) > NUM_MTRR_STATIC_STORAGE (%d)\n",
56 total_mtrrs, NUM_MTRR_STATIC_STORAGE);
57 total_mtrrs = NUM_MTRR_STATIC_STORAGE;
58 }
Stefan Reinauerc00dfbc2012-04-03 16:24:37 -070059 bios_mtrrs = total_mtrrs - OS_MTRRS;
60}
61
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000062void enable_fixed_mtrr(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000063{
64 msr_t msr;
65
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070066 msr = rdmsr(MTRR_DEF_TYPE_MSR);
67 msr.lo |= MTRR_DEF_TYPE_EN | MTRR_DEF_TYPE_FIX_EN;
68 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000069}
70
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -060071void fixed_mtrrs_expose_amd_rwdram(void)
72{
73 msr_t syscfg;
74
Julius Wernercd49cce2019-03-05 16:53:33 -080075 if (!CONFIG(X86_AMD_FIXED_MTRRS))
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -060076 return;
77
78 syscfg = rdmsr(SYSCFG_MSR);
79 syscfg.lo |= SYSCFG_MSR_MtrrFixDramModEn;
80 wrmsr(SYSCFG_MSR, syscfg);
81}
82
83void fixed_mtrrs_hide_amd_rwdram(void)
84{
85 msr_t syscfg;
86
Julius Wernercd49cce2019-03-05 16:53:33 -080087 if (!CONFIG(X86_AMD_FIXED_MTRRS))
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -060088 return;
89
90 syscfg = rdmsr(SYSCFG_MSR);
91 syscfg.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
92 wrmsr(SYSCFG_MSR, syscfg);
93}
94
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050095static void enable_var_mtrr(unsigned char deftype)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000096{
97 msr_t msr;
98
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070099 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500100 msr.lo &= ~0xff;
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700101 msr.lo |= MTRR_DEF_TYPE_EN | deftype;
102 wrmsr(MTRR_DEF_TYPE_MSR, msr);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000103}
104
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500105#define MTRR_VERBOSE_LEVEL BIOS_NEVER
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000106
Jonathan Zhang320ad932020-10-14 15:07:51 -0700107/* MTRRs are at a 4KiB granularity. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500108#define RANGE_SHIFT 12
109#define ADDR_SHIFT_TO_RANGE_SHIFT(x) \
110 (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT)
111#define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT)
112#define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT)
113#define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR)
114
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500115/* Helpful constants. */
Jonathan Zhang320ad932020-10-14 15:07:51 -0700116#define RANGE_1MB PHYS_TO_RANGE_ADDR(1ULL << 20)
117#define RANGE_4GB (1ULL << (ADDR_SHIFT_TO_RANGE_SHIFT(32)))
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500118
Aaron Durbine3834422013-03-28 20:48:51 -0500119#define MTRR_ALGO_SHIFT (8)
120#define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1)
Aaron Durbine3834422013-03-28 20:48:51 -0500121
Jonathan Zhang320ad932020-10-14 15:07:51 -0700122static inline uint64_t range_entry_base_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000123{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500124 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000125}
126
Jonathan Zhang320ad932020-10-14 15:07:51 -0700127static inline uint64_t range_entry_end_mtrr_addr(struct range_entry *r)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000128{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500129 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000130}
131
Aaron Durbine3834422013-03-28 20:48:51 -0500132static inline int range_entry_mtrr_type(struct range_entry *r)
133{
134 return range_entry_tag(r) & MTRR_TAG_MASK;
135}
136
Aaron Durbinca4f4b82014-02-08 15:41:52 -0600137static int filter_vga_wrcomb(struct device *dev, struct resource *res)
138{
139 /* Only handle PCI devices. */
140 if (dev->path.type != DEVICE_PATH_PCI)
141 return 0;
142
143 /* Only handle VGA class devices. */
144 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
145 return 0;
146
147 /* Add resource as write-combining in the address space. */
148 return 1;
149}
150
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600151static void print_physical_address_space(const struct memranges *addr_space,
152 const char *identifier)
153{
154 const struct range_entry *r;
155
156 if (identifier)
157 printk(BIOS_DEBUG, "MTRR: %s Physical address space:\n",
158 identifier);
159 else
160 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
161
162 memranges_each_entry(r, addr_space)
163 printk(BIOS_DEBUG,
164 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
165 range_entry_base(r), range_entry_end(r),
166 range_entry_size(r), range_entry_tag(r));
167}
168
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500169static struct memranges *get_physical_address_space(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000170{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500171 static struct memranges *addr_space;
172 static struct memranges addr_space_storage;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800173
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500174 /* In order to handle some chipsets not being able to pre-determine
Martin Roth4c3ab732013-07-08 16:23:54 -0600175 * uncacheable ranges, such as graphics memory, at resource insertion
176 * time remove uncacheable regions from the cacheable ones. */
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500177 if (addr_space == NULL) {
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500178 unsigned long mask;
179 unsigned long match;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500180
181 addr_space = &addr_space_storage;
182
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500183 mask = IORESOURCE_CACHEABLE;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500184 /* Collect cacheable and uncacheable address ranges. The
185 * uncacheable regions take precedence over the cacheable
186 * regions. */
187 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
188 memranges_add_resources(addr_space, mask, 0,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700189 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500190
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500191 /* Handle any write combining resources. Only prefetchable
Vladimir Serbinenko30fe6122014-02-05 23:25:28 +0100192 * resources are appropriate for this MTRR type. */
193 match = IORESOURCE_PREFETCH;
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500194 mask |= match;
Lee Leahyc5917072017-03-15 16:38:51 -0700195 memranges_add_resources_filter(addr_space, mask, match,
196 MTRR_TYPE_WRCOMB, filter_vga_wrcomb);
Aaron Durbin9b027fe2013-03-26 14:10:34 -0500197
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500198 /* The address space below 4GiB is special. It needs to be
Martin Roth2f914032016-01-15 10:20:11 -0700199 * covered entirely by range entries so that MTRR calculations
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500200 * can be properly done for the full 32-bit address space.
201 * Therefore, ensure holes are filled up to 4GiB as
202 * uncacheable */
203 memranges_fill_holes_up_to(addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700204 RANGE_TO_PHYS_ADDR(RANGE_4GB),
205 MTRR_TYPE_UNCACHEABLE);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500206
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600207 print_physical_address_space(addr_space, NULL);
Carl-Daniel Hailfinger7dde1da2009-02-11 16:57:32 +0000208 }
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000209
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500210 return addr_space;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000211}
212
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500213/* Fixed MTRR descriptor. This structure defines the step size and begin
Martin Roth4c3ab732013-07-08 16:23:54 -0600214 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500215 * It also describes the offset in byte intervals to store the calculated MTRR
216 * type in an array. */
217struct fixed_mtrr_desc {
218 uint32_t begin;
219 uint32_t end;
220 uint32_t step;
221 int range_index;
222 int msr_index_base;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000223};
224
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500225/* Shared MTRR calculations. Can be reused by APs. */
226static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
227
228/* Fixed MTRR descriptors. */
229static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
230 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700231 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500232 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700233 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500234 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700235 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 },
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500236};
237
238static void calc_fixed_mtrrs(void)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000239{
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500240 static int fixed_mtrr_types_initialized;
241 struct memranges *phys_addr_space;
242 struct range_entry *r;
243 const struct fixed_mtrr_desc *desc;
244 const struct fixed_mtrr_desc *last_desc;
245 uint32_t begin;
246 uint32_t end;
247 int type_index;
248
249 if (fixed_mtrr_types_initialized)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000250 return;
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300251
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500252 phys_addr_space = get_physical_address_space();
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300253
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500254 /* Set all fixed ranges to uncacheable first. */
255 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300256
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500257 desc = &fixed_mtrr_desc[0];
258 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300259
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500260 memranges_each_entry(r, phys_addr_space) {
261 begin = range_entry_base_mtrr_addr(r);
262 end = range_entry_end_mtrr_addr(r);
Kyösti Mälkki2d42b342012-07-12 00:18:22 +0300263
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500264 if (begin >= last_desc->end)
265 break;
266
267 if (end > last_desc->end)
268 end = last_desc->end;
269
270 /* Get to the correct fixed mtrr descriptor. */
271 while (begin >= desc->end)
272 desc++;
273
274 type_index = desc->range_index;
275 type_index += (begin - desc->begin) / desc->step;
276
277 while (begin != end) {
278 unsigned char type;
279
280 type = range_entry_tag(r);
281 printk(MTRR_VERBOSE_LEVEL,
282 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
283 begin, begin + desc->step, type, type_index);
284 if (type == MTRR_TYPE_WRBACK)
285 type |= MTRR_FIXED_WRBACK_BITS;
286 fixed_mtrr_types[type_index] = type;
287 type_index++;
288 begin += desc->step;
289 if (begin == desc->end)
290 desc++;
Yinghai Lu63601872005-01-27 22:48:12 +0000291 }
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000292 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500293 fixed_mtrr_types_initialized = 1;
294}
295
296static void commit_fixed_mtrrs(void)
297{
298 int i;
299 int j;
300 int msr_num;
301 int type_index;
302 /* 8 ranges per msr. */
303 msr_t fixed_msrs[NUM_FIXED_MTRRS];
304 unsigned long msr_index[NUM_FIXED_MTRRS];
305
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -0600306 fixed_mtrrs_expose_amd_rwdram();
307
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500308 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
309
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500310 msr_num = 0;
311 type_index = 0;
312 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
313 const struct fixed_mtrr_desc *desc;
314 int num_ranges;
315
316 desc = &fixed_mtrr_desc[i];
317 num_ranges = (desc->end - desc->begin) / desc->step;
318 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
319 msr_index[msr_num] = desc->msr_index_base +
320 (j / RANGES_PER_FIXED_MTRR);
321 fixed_msrs[msr_num].lo |=
322 fixed_mtrr_types[type_index++] << 0;
323 fixed_msrs[msr_num].lo |=
324 fixed_mtrr_types[type_index++] << 8;
325 fixed_msrs[msr_num].lo |=
326 fixed_mtrr_types[type_index++] << 16;
327 fixed_msrs[msr_num].lo |=
328 fixed_mtrr_types[type_index++] << 24;
329 fixed_msrs[msr_num].hi |=
330 fixed_mtrr_types[type_index++] << 0;
331 fixed_msrs[msr_num].hi |=
332 fixed_mtrr_types[type_index++] << 8;
333 fixed_msrs[msr_num].hi |=
334 fixed_mtrr_types[type_index++] << 16;
335 fixed_msrs[msr_num].hi |=
336 fixed_mtrr_types[type_index++] << 24;
337 msr_num++;
338 }
339 }
340
Jacob Garber5b922722019-05-28 11:47:49 -0600341 /* Ensure that both arrays were fully initialized */
342 ASSERT(msr_num == NUM_FIXED_MTRRS)
343
Gabe Black7756fe72014-02-25 01:40:34 -0800344 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500345 printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
346 msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500347
Gabe Black7756fe72014-02-25 01:40:34 -0800348 disable_cache();
349 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
350 wrmsr(msr_index[i], fixed_msrs[i]);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500351 enable_cache();
Marshall Dawsonc0dbeda2017-10-19 09:45:16 -0600352 fixed_mtrrs_hide_amd_rwdram();
353
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,
Jonathan Zhang320ad932020-10-14 15:07:51 -0700403 uint64_t base, uint64_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) {
Jonathan Neuschäferbb3a5ef2018-04-09 20:14:19 +0200416 printk(BIOS_ERR, "ERROR: Not enough MTRRs available! MTRR index is %d with %d MTRRs in total.\n",
Paul Menzel6a70dbc2015-10-15 12:41:53 +0200417 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
Jonathan Zhang320ad932020-10-14 15:07:51 -0700445/*
446 * fls64: find least significant bit set in a 64-bit word
447 * As samples, fls64(0x0) = 64; fls64(0x4400) = 10;
448 * fls64(0x40400000000) = 34.
449 */
450static uint32_t fls64(uint64_t x)
451{
452 uint32_t lo = (uint32_t)x;
453 if (lo)
454 return fls(lo);
455 uint32_t hi = x >> 32;
456 return fls(hi) + 32;
457}
458
459/*
460 * fms64: find most significant bit set in a 64-bit word
461 * As samples, fms64(0x0) = 0; fms64(0x4400) = 14;
462 * fms64(0x40400000000) = 42.
463 */
464static uint32_t fms64(uint64_t x)
465{
466 uint32_t hi = (uint32_t)(x >> 32);
467 if (!hi)
468 return fms((uint32_t)x);
469 return fms(hi) + 32;
470}
471
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500472static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
Jonathan Zhang320ad932020-10-14 15:07:51 -0700473 uint64_t base, uint64_t size, int mtrr_type)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500474{
475 while (size != 0) {
476 uint32_t addr_lsb;
477 uint32_t size_msb;
Jonathan Zhang320ad932020-10-14 15:07:51 -0700478 uint64_t mtrr_size;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500479
Jonathan Zhang320ad932020-10-14 15:07:51 -0700480 addr_lsb = fls64(base);
481 size_msb = fms64(size);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500482
483 /* All MTRR entries need to have their base aligned to the mask
484 * size. The maximum size is calculated by a function of the
485 * min base bit set and maximum size bit set. */
486 if (addr_lsb > size_msb)
Jonathan Zhang8f594b72020-10-23 15:20:22 -0700487 mtrr_size = 1ULL << size_msb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500488 else
Jonathan Zhang8f594b72020-10-23 15:20:22 -0700489 mtrr_size = 1ULL << addr_lsb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500490
Gabe Black7756fe72014-02-25 01:40:34 -0800491 if (var_state->prepare_msrs)
492 prep_var_mtrr(var_state, base, mtrr_size, mtrr_type);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500493
494 size -= mtrr_size;
495 base += mtrr_size;
496 var_state->mtrr_index++;
497 }
498}
499
Jonathan Zhang320ad932020-10-14 15:07:51 -0700500static uint64_t optimize_var_mtrr_hole(const uint64_t base,
501 const uint64_t hole,
Nico Huberbd5fb662017-10-07 13:40:19 +0200502 const uint64_t limit,
503 const int carve_hole)
504{
505 /*
506 * With default type UC, we can potentially optimize a WB
507 * range with unaligned upper end, by aligning it up and
508 * carving the added "hole" out again.
509 *
510 * To optimize the upper end of the hole, we will test
511 * how many MTRRs calc_var_mtrr_range() will spend for any
512 * alignment of the hole's upper end.
513 *
514 * We take four parameters, the lower end of the WB range
515 * `base`, upper end of the WB range as start of the `hole`,
516 * a `limit` how far we may align the upper end of the hole
517 * up and a flag `carve_hole` whether we should count MTRRs
518 * for carving the hole out. We return the optimal upper end
519 * for the hole (which may be the same as the end of the WB
520 * range in case we don't gain anything by aligning up).
521 */
522
523 const int dont_care = 0;
524 struct var_mtrr_state var_state = { 0, };
525
526 unsigned int align, best_count;
527 uint32_t best_end = hole;
528
529 /* calculate MTRR count for the WB range alone (w/o a hole) */
530 calc_var_mtrr_range(&var_state, base, hole - base, dont_care);
531 best_count = var_state.mtrr_index;
532 var_state.mtrr_index = 0;
533
534 for (align = fls(hole) + 1; align <= fms(hole); ++align) {
535 const uint64_t hole_end = ALIGN_UP((uint64_t)hole, 1 << align);
536 if (hole_end > limit)
537 break;
538
539 /* calculate MTRR count for this alignment */
540 calc_var_mtrr_range(
541 &var_state, base, hole_end - base, dont_care);
542 if (carve_hole)
543 calc_var_mtrr_range(
544 &var_state, hole, hole_end - hole, dont_care);
545
546 if (var_state.mtrr_index < best_count) {
547 best_count = var_state.mtrr_index;
548 best_end = hole_end;
549 }
550 var_state.mtrr_index = 0;
551 }
552
553 return best_end;
554}
555
Aaron Durbine3834422013-03-28 20:48:51 -0500556static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700557 struct range_entry *r)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500558{
Jonathan Zhang320ad932020-10-14 15:07:51 -0700559 uint64_t a1, a2, b1, b2;
Nico Huberbd5fb662017-10-07 13:40:19 +0200560 int mtrr_type, carve_hole;
Aaron Durbine3834422013-03-28 20:48:51 -0500561
562 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600563 * Determine MTRRs based on the following algorithm for the given entry:
Aaron Durbine3834422013-03-28 20:48:51 -0500564 * +------------------+ b2 = ALIGN_UP(end)
565 * | 0 or more bytes | <-- hole is carved out between b1 and b2
Nico Huberbd5fb662017-10-07 13:40:19 +0200566 * +------------------+ a2 = b1 = original end
Aaron Durbine3834422013-03-28 20:48:51 -0500567 * | |
568 * +------------------+ a1 = begin
569 *
Nico Huberbd5fb662017-10-07 13:40:19 +0200570 * Thus, there are up to 2 sub-ranges to configure variable MTRRs for.
Aaron Durbine3834422013-03-28 20:48:51 -0500571 */
572 mtrr_type = range_entry_mtrr_type(r);
573
574 a1 = range_entry_base_mtrr_addr(r);
575 a2 = range_entry_end_mtrr_addr(r);
576
Aaron Durbina38677b2016-07-21 14:26:34 -0500577 /* The end address is within the first 1MiB. The fixed MTRRs take
Aaron Durbine3834422013-03-28 20:48:51 -0500578 * precedence over the variable ones. Therefore this range
579 * can be ignored. */
Aaron Durbina38677b2016-07-21 14:26:34 -0500580 if (a2 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500581 return;
582
583 /* Again, the fixed MTRRs take precedence so the beginning
Aaron Durbina38677b2016-07-21 14:26:34 -0500584 * of the range can be set to 0 if it starts at or below 1MiB. */
585 if (a1 <= RANGE_1MB)
Aaron Durbine3834422013-03-28 20:48:51 -0500586 a1 = 0;
587
588 /* If the range starts above 4GiB the processing is done. */
589 if (!var_state->above4gb && a1 >= RANGE_4GB)
590 return;
591
592 /* Clip the upper address to 4GiB if addresses above 4GiB
593 * are not being processed. */
594 if (!var_state->above4gb && a2 > RANGE_4GB)
595 a2 = RANGE_4GB;
596
597 b1 = a2;
Nico Huber64f0bcb2017-10-07 16:37:04 +0200598 b2 = a2;
599 carve_hole = 0;
Aaron Durbin53924242013-03-29 11:48:27 -0500600
Nico Huber64f0bcb2017-10-07 16:37:04 +0200601 /* We only consider WB type ranges for hole-carving. */
602 if (mtrr_type == MTRR_TYPE_WRBACK) {
603 struct range_entry *next;
604 uint64_t b2_limit;
605 /*
606 * Depending on the type of the next range, there are three
607 * different situations to handle:
608 *
609 * 1. WB range is last in address space:
610 * Aligning up, up to the next power of 2, may gain us
611 * something.
612 *
613 * 2. The next range is of type UC:
614 * We may align up, up to the _end_ of the next range. If
615 * there is a gap between the current and the next range,
616 * it would have been covered by the default type UC anyway.
617 *
618 * 3. The next range is not of type UC:
619 * We may align up, up to the _base_ of the next range. This
620 * may either be the end of the current range (if the next
621 * range follows immediately) or the end of the gap between
622 * the ranges.
623 */
624 next = memranges_next_entry(var_state->addr_space, r);
625 if (next == NULL) {
626 b2_limit = ALIGN_UP((uint64_t)b1, 1 << fms(b1));
627 /* If it's the last range above 4GiB, we won't carve
628 the hole out. If an OS wanted to move MMIO there,
629 it would have to override the MTRR setting using
630 PAT just like it would with WB as default type. */
631 carve_hole = a1 < RANGE_4GB;
632 } else if (range_entry_mtrr_type(next)
633 == MTRR_TYPE_UNCACHEABLE) {
634 b2_limit = range_entry_end_mtrr_addr(next);
635 carve_hole = 1;
636 } else {
637 b2_limit = range_entry_base_mtrr_addr(next);
638 carve_hole = 1;
639 }
640 b2 = optimize_var_mtrr_hole(a1, b1, b2_limit, carve_hole);
Aaron Durbin53924242013-03-29 11:48:27 -0500641 }
Aaron Durbine3834422013-03-28 20:48:51 -0500642
643 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
Nico Huberbd5fb662017-10-07 13:40:19 +0200644 if (carve_hole && b2 != b1) {
645 calc_var_mtrr_range(var_state, b1, b2 - b1,
646 MTRR_TYPE_UNCACHEABLE);
647 }
Aaron Durbine3834422013-03-28 20:48:51 -0500648}
649
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600650static void __calc_var_mtrrs(struct memranges *addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700651 int above4gb, int address_bits,
652 int *num_def_wb_mtrrs, int *num_def_uc_mtrrs)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500653{
654 int wb_deftype_count;
655 int uc_deftype_count;
Aaron Durbine3834422013-03-28 20:48:51 -0500656 struct range_entry *r;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000657 struct var_mtrr_state var_state;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000658
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500659 /* The default MTRR cacheability type is determined by calculating
Paul Menzel4fe98132014-01-25 15:55:28 +0100660 * the number of MTRRs required for each MTRR type as if it was the
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500661 * default. */
662 var_state.addr_space = addr_space;
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000663 var_state.above4gb = above4gb;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500664 var_state.address_bits = address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800665 var_state.prepare_msrs = 0;
Stefan Reinauer7f86ed12009-02-12 16:02:16 +0000666
Aaron Durbine3834422013-03-28 20:48:51 -0500667 wb_deftype_count = 0;
668 uc_deftype_count = 0;
Duncan Laurie7389fa92011-12-22 10:59:40 -0800669
Aaron Durbine3834422013-03-28 20:48:51 -0500670 /*
Nico Huber64f0bcb2017-10-07 16:37:04 +0200671 * For each range do 2 calculations:
672 * 1. UC as default type with possible holes at top of range.
673 * 2. WB as default.
Martin Roth4c3ab732013-07-08 16:23:54 -0600674 * The lowest count is then used as default after totaling all
Nico Huber64f0bcb2017-10-07 16:37:04 +0200675 * MTRRs. UC takes precedence in the MTRR architecture. There-
676 * fore, only holes can be used when the type of the region is
677 * MTRR_TYPE_WRBACK with MTRR_TYPE_UNCACHEABLE as the default
678 * type.
Aaron Durbine3834422013-03-28 20:48:51 -0500679 */
680 memranges_each_entry(r, var_state.addr_space) {
681 int mtrr_type;
682
683 mtrr_type = range_entry_mtrr_type(r);
684
685 if (mtrr_type != MTRR_TYPE_UNCACHEABLE) {
Aaron Durbine3834422013-03-28 20:48:51 -0500686 var_state.mtrr_index = 0;
Nico Huber64f0bcb2017-10-07 16:37:04 +0200687 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
688 calc_var_mtrrs_with_hole(&var_state, r);
689 uc_deftype_count += var_state.mtrr_index;
Aaron Durbine3834422013-03-28 20:48:51 -0500690 }
691
692 if (mtrr_type != MTRR_TYPE_WRBACK) {
693 var_state.mtrr_index = 0;
694 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
Nico Huber64f0bcb2017-10-07 16:37:04 +0200695 calc_var_mtrrs_with_hole(&var_state, r);
Aaron Durbine3834422013-03-28 20:48:51 -0500696 wb_deftype_count += var_state.mtrr_index;
697 }
698 }
Jonathan Zhang320ad932020-10-14 15:07:51 -0700699
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600700 *num_def_wb_mtrrs = wb_deftype_count;
701 *num_def_uc_mtrrs = uc_deftype_count;
702}
703
704static int calc_var_mtrrs(struct memranges *addr_space,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700705 int above4gb, int address_bits)
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600706{
707 int wb_deftype_count = 0;
708 int uc_deftype_count = 0;
709
710 __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700711 &uc_deftype_count);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600712
713 if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) {
714 printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. "
715 "WB/UC MTRR counts: %d/%d > %d.\n",
716 wb_deftype_count, uc_deftype_count, bios_mtrrs);
717 memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700718 MTRR_TYPE_UNCACHEABLE);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600719 __calc_var_mtrrs(addr_space, above4gb, address_bits,
Lee Leahya07d0dd2017-03-15 14:25:22 -0700720 &wb_deftype_count, &uc_deftype_count);
Aaron Durbin5b9e3b62014-02-05 16:00:43 -0600721 }
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000722
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500723 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
724 wb_deftype_count, uc_deftype_count);
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300725
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500726 if (wb_deftype_count < uc_deftype_count) {
727 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
728 return MTRR_TYPE_WRBACK;
729 }
730 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
731 return MTRR_TYPE_UNCACHEABLE;
732}
Kyösti Mälkkiffc1fb32012-07-11 14:40:19 +0300733
Gabe Black7756fe72014-02-25 01:40:34 -0800734static void prepare_var_mtrrs(struct memranges *addr_space, int def_type,
735 int above4gb, int address_bits,
736 struct var_mtrr_solution *sol)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500737{
Aaron Durbine3834422013-03-28 20:48:51 -0500738 struct range_entry *r;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500739 struct var_mtrr_state var_state;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500740
741 var_state.addr_space = addr_space;
742 var_state.above4gb = above4gb;
743 var_state.address_bits = address_bits;
Gabe Black7756fe72014-02-25 01:40:34 -0800744 /* Prepare the MSRs. */
745 var_state.prepare_msrs = 1;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500746 var_state.mtrr_index = 0;
747 var_state.def_mtrr_type = def_type;
Gabe Black7756fe72014-02-25 01:40:34 -0800748 var_state.regs = &sol->regs[0];
Aaron Durbine3834422013-03-28 20:48:51 -0500749
750 memranges_each_entry(r, var_state.addr_space) {
751 if (range_entry_mtrr_type(r) == def_type)
752 continue;
Nico Huber64f0bcb2017-10-07 16:37:04 +0200753 calc_var_mtrrs_with_hole(&var_state, r);
Aaron Durbine3834422013-03-28 20:48:51 -0500754 }
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500755
Gabe Black7756fe72014-02-25 01:40:34 -0800756 /* Update the solution. */
757 sol->num_used = var_state.mtrr_index;
758}
759
Aaron Durbind9762f72017-06-12 12:48:38 -0500760static int commit_var_mtrrs(const struct var_mtrr_solution *sol)
Gabe Black7756fe72014-02-25 01:40:34 -0800761{
762 int i;
763
Aaron Durbind9762f72017-06-12 12:48:38 -0500764 if (sol->num_used > total_mtrrs) {
765 printk(BIOS_WARNING, "Not enough MTRRs: %d vs %d\n",
766 sol->num_used, total_mtrrs);
767 return -1;
768 }
769
Isaac Christensen81f90c52014-09-24 14:59:32 -0600770 /* Write out the variable MTRRs. */
Gabe Black7756fe72014-02-25 01:40:34 -0800771 disable_cache();
772 for (i = 0; i < sol->num_used; i++) {
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700773 wrmsr(MTRR_PHYS_BASE(i), sol->regs[i].base);
774 wrmsr(MTRR_PHYS_MASK(i), sol->regs[i].mask);
Gabe Black7756fe72014-02-25 01:40:34 -0800775 }
776 /* Clear the ones that are unused. */
777 for (; i < total_mtrrs; i++)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500778 clear_var_mtrr(i);
Isaac Christensen81f90c52014-09-24 14:59:32 -0600779 enable_var_mtrr(sol->mtrr_default_type);
Gabe Black7756fe72014-02-25 01:40:34 -0800780 enable_cache();
781
Aaron Durbind9762f72017-06-12 12:48:38 -0500782 return 0;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500783}
784
785void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb)
786{
Gabe Black7756fe72014-02-25 01:40:34 -0800787 static struct var_mtrr_solution *sol = NULL;
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500788 struct memranges *addr_space;
789
790 addr_space = get_physical_address_space();
791
Gabe Black7756fe72014-02-25 01:40:34 -0800792 if (sol == NULL) {
Gabe Black7756fe72014-02-25 01:40:34 -0800793 sol = &mtrr_global_solution;
794 sol->mtrr_default_type =
Aaron Durbinbb4e79a2013-03-26 14:09:47 -0500795 calc_var_mtrrs(addr_space, !!above4gb, address_bits);
Gabe Black7756fe72014-02-25 01:40:34 -0800796 prepare_var_mtrrs(addr_space, sol->mtrr_default_type,
797 !!above4gb, address_bits, sol);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000798 }
Stefan Reinauer00093a82011-11-02 16:12:34 -0700799
Gabe Black7756fe72014-02-25 01:40:34 -0800800 commit_var_mtrrs(sol);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000801}
802
Aaron Durbin1ebbb162020-05-28 10:17:34 -0600803static void _x86_setup_mtrrs(unsigned int above4gb)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000804{
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100805 int address_size;
Aaron Durbine63be892016-03-07 16:05:36 -0600806
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000807 x86_setup_fixed_mtrrs();
Sven Schnelleadfbcb792012-01-10 12:01:43 +0100808 address_size = cpu_phys_address_size();
Aaron Durbine63be892016-03-07 16:05:36 -0600809 printk(BIOS_DEBUG, "CPU physical address size: %d bits\n",
810 address_size);
Aaron Durbin1ebbb162020-05-28 10:17:34 -0600811 x86_setup_var_mtrrs(address_size, above4gb);
812}
813
814void x86_setup_mtrrs(void)
815{
Aaron Durbine63be892016-03-07 16:05:36 -0600816 /* Always handle addresses above 4GiB. */
Aaron Durbin1ebbb162020-05-28 10:17:34 -0600817 _x86_setup_mtrrs(1);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000818}
819
Aaron Durbine63be892016-03-07 16:05:36 -0600820void x86_setup_mtrrs_with_detect(void)
821{
822 detect_var_mtrrs();
Aaron Durbin1ebbb162020-05-28 10:17:34 -0600823 /* Always handle addresses above 4GiB. */
824 _x86_setup_mtrrs(1);
825}
826
827void x86_setup_mtrrs_with_detect_no_above_4gb(void)
828{
829 detect_var_mtrrs();
830 _x86_setup_mtrrs(0);
Aaron Durbine63be892016-03-07 16:05:36 -0600831}
832
Kyösti Mälkki38a8fb02014-06-30 13:48:18 +0300833void x86_mtrr_check(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000834{
835 /* Only Pentium Pro and later have MTRR */
836 msr_t msr;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000837 printk(BIOS_DEBUG, "\nMTRR check\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000838
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700839 msr = rdmsr(MTRR_DEF_TYPE_MSR);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000840
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000841 printk(BIOS_DEBUG, "Fixed MTRRs : ");
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700842 if (msr.lo & MTRR_DEF_TYPE_FIX_EN)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000843 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000844 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000845 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000846
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000847 printk(BIOS_DEBUG, "Variable MTRRs: ");
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700848 if (msr.lo & MTRR_DEF_TYPE_EN)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000849 printk(BIOS_DEBUG, "Enabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000850 else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000851 printk(BIOS_DEBUG, "Disabled\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000852
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000853 printk(BIOS_DEBUG, "\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000854
855 post_code(0x93);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000856}
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600857
858static bool put_back_original_solution;
859
860void mtrr_use_temp_range(uintptr_t begin, size_t size, int type)
861{
862 const struct range_entry *r;
863 const struct memranges *orig;
864 struct var_mtrr_solution sol;
865 struct memranges addr_space;
866 const int above4gb = 1; /* Cover above 4GiB by default. */
867 int address_bits;
868
869 /* Make a copy of the original address space and tweak it with the
870 * provided range. */
871 memranges_init_empty(&addr_space, NULL, 0);
872 orig = get_physical_address_space();
873 memranges_each_entry(r, orig) {
874 unsigned long tag = range_entry_tag(r);
875
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600876 /* Remove any write combining MTRRs from the temporary
877 * solution as it just fragments the address space. */
878 if (tag == MTRR_TYPE_WRCOMB)
879 tag = MTRR_TYPE_UNCACHEABLE;
880
881 memranges_insert(&addr_space, range_entry_base(r),
882 range_entry_size(r), tag);
883 }
884
885 /* Place new range into the address space. */
886 memranges_insert(&addr_space, begin, size, type);
887
888 print_physical_address_space(&addr_space, "TEMPORARY");
889
890 /* Calculate a new solution with the updated address space. */
891 address_bits = cpu_phys_address_size();
892 memset(&sol, 0, sizeof(sol));
893 sol.mtrr_default_type =
894 calc_var_mtrrs(&addr_space, above4gb, address_bits);
895 prepare_var_mtrrs(&addr_space, sol.mtrr_default_type,
896 above4gb, address_bits, &sol);
Aaron Durbind9762f72017-06-12 12:48:38 -0500897
898 if (commit_var_mtrrs(&sol) < 0)
899 printk(BIOS_WARNING, "Unable to insert temporary MTRR range: 0x%016llx - 0x%016llx size 0x%08llx type %d\n",
900 (long long)begin, (long long)begin + size,
901 (long long)size, type);
902 else
903 put_back_original_solution = true;
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600904
905 memranges_teardown(&addr_space);
Aaron Durbin2bebd7b2016-11-10 15:15:35 -0600906}
907
908static void remove_temp_solution(void *unused)
909{
910 if (put_back_original_solution)
911 commit_var_mtrrs(&mtrr_global_solution);
912}
913
914BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, remove_temp_solution, NULL);
915BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, remove_temp_solution, NULL);