Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | cdc5cc6 | 2007-04-24 18:40:02 +0000 | [diff] [blame] | 2 | * mtrr.c: setting MTRR to decent values for cache initialization on P6 |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 3 | * |
| 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 7 | * Copyright 2013 Google Inc. |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 8 | * |
| 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 Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 19 | * |
Lee Leahy | c591707 | 2017-03-15 16:38:51 -0700 | [diff] [blame] | 20 | * Reference: Intel Architecture Software Developer's Manual, Volume 3: System |
| 21 | * Programming |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 22 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 23 | |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 24 | #include <stddef.h> |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Aaron Durbin | bebf669 | 2013-04-24 20:59:43 -0500 | [diff] [blame] | 27 | #include <bootstate.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 28 | #include <console/console.h> |
| 29 | #include <device/device.h> |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 30 | #include <device/pci_ids.h> |
Aaron Durbin | ebf142a | 2013-03-29 16:23:23 -0500 | [diff] [blame] | 31 | #include <cpu/cpu.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 32 | #include <cpu/x86/msr.h> |
| 33 | #include <cpu/x86/mtrr.h> |
| 34 | #include <cpu/x86/cache.h> |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 35 | #include <cpu/x86/lapic.h> |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 36 | #include <arch/cpu.h> |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 37 | #include <arch/acpi.h> |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 38 | #include <memrange.h> |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 39 | #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 Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 45 | |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 46 | /* 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 Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 50 | /* |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame] | 51 | * 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 Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 53 | * yet been observed. |
| 54 | */ |
| 55 | #define NUM_MTRR_STATIC_STORAGE 16 |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 56 | |
| 57 | static int total_mtrrs = MTRRS; |
| 58 | static int bios_mtrrs = BIOS_MTRRS; |
| 59 | |
| 60 | static void detect_var_mtrrs(void) |
| 61 | { |
| 62 | msr_t msr; |
| 63 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 64 | msr = rdmsr(MTRR_CAP_MSR); |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 65 | |
| 66 | total_mtrrs = msr.lo & 0xff; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 67 | |
| 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 Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 74 | bios_mtrrs = total_mtrrs - OS_MTRRS; |
| 75 | } |
| 76 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 77 | void enable_fixed_mtrr(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 78 | { |
| 79 | msr_t msr; |
| 80 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 81 | 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 Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 86 | static void enable_var_mtrr(unsigned char deftype) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 87 | { |
| 88 | msr_t msr; |
| 89 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 90 | msr = rdmsr(MTRR_DEF_TYPE_MSR); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 91 | msr.lo &= ~0xff; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 92 | msr.lo |= MTRR_DEF_TYPE_EN | deftype; |
| 93 | wrmsr(MTRR_DEF_TYPE_MSR, msr); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 96 | #define MTRR_VERBOSE_LEVEL BIOS_NEVER |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 97 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 98 | /* 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 Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 114 | /* |
| 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 129 | static inline uint32_t range_entry_base_mtrr_addr(struct range_entry *r) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 130 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 131 | return PHYS_TO_RANGE_ADDR(range_entry_base(r)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 134 | static inline uint32_t range_entry_end_mtrr_addr(struct range_entry *r) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 135 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 136 | return PHYS_TO_RANGE_ADDR(range_entry_end(r)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 139 | static inline int range_entry_mtrr_type(struct range_entry *r) |
| 140 | { |
| 141 | return range_entry_tag(r) & MTRR_TAG_MASK; |
| 142 | } |
| 143 | |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 144 | static 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 Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 158 | static 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 176 | static struct memranges *get_physical_address_space(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 177 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 178 | static struct memranges *addr_space; |
| 179 | static struct memranges addr_space_storage; |
Duncan Laurie | 7389fa9 | 2011-12-22 10:59:40 -0800 | [diff] [blame] | 180 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 181 | /* In order to handle some chipsets not being able to pre-determine |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 182 | * uncacheable ranges, such as graphics memory, at resource insertion |
| 183 | * time remove uncacheable regions from the cacheable ones. */ |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 184 | if (addr_space == NULL) { |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 185 | unsigned long mask; |
| 186 | unsigned long match; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 187 | |
| 188 | addr_space = &addr_space_storage; |
| 189 | |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 190 | mask = IORESOURCE_CACHEABLE; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 191 | /* 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 Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 196 | MTRR_TYPE_UNCACHEABLE); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 197 | |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 198 | /* Handle any write combining resources. Only prefetchable |
Vladimir Serbinenko | 30fe612 | 2014-02-05 23:25:28 +0100 | [diff] [blame] | 199 | * resources are appropriate for this MTRR type. */ |
| 200 | match = IORESOURCE_PREFETCH; |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 201 | mask |= match; |
Lee Leahy | c591707 | 2017-03-15 16:38:51 -0700 | [diff] [blame] | 202 | memranges_add_resources_filter(addr_space, mask, match, |
| 203 | MTRR_TYPE_WRCOMB, filter_vga_wrcomb); |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 204 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 205 | /* The address space below 4GiB is special. It needs to be |
Martin Roth | 2f91403 | 2016-01-15 10:20:11 -0700 | [diff] [blame] | 206 | * covered entirely by range entries so that MTRR calculations |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 207 | * 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 Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 211 | RANGE_TO_PHYS_ADDR(RANGE_4GB), |
| 212 | MTRR_TYPE_UNCACHEABLE); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 213 | |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 214 | print_physical_address_space(addr_space, NULL); |
Carl-Daniel Hailfinger | 7dde1da | 2009-02-11 16:57:32 +0000 | [diff] [blame] | 215 | } |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 216 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 217 | return addr_space; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 220 | /* Fixed MTRR descriptor. This structure defines the step size and begin |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 221 | * and end (exclusive) address covered by a set of fixed MTRR MSRs. |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 222 | * It also describes the offset in byte intervals to store the calculated MTRR |
| 223 | * type in an array. */ |
| 224 | struct 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 Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 232 | /* Shared MTRR calculations. Can be reused by APs. */ |
| 233 | static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES]; |
| 234 | |
| 235 | /* Fixed MTRR descriptors. */ |
| 236 | static const struct fixed_mtrr_desc fixed_mtrr_desc[] = { |
| 237 | { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000), |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 238 | PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 }, |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 239 | { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000), |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 240 | PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 }, |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 241 | { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000), |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 242 | PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 }, |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | static void calc_fixed_mtrrs(void) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 246 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 247 | 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 Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 257 | return; |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 258 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 259 | phys_addr_space = get_physical_address_space(); |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 260 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 261 | /* Set all fixed ranges to uncacheable first. */ |
| 262 | memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES); |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 263 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 264 | desc = &fixed_mtrr_desc[0]; |
| 265 | last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1]; |
Kyösti Mälkki | 1ec5e74 | 2012-07-26 23:51:20 +0300 | [diff] [blame] | 266 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 267 | 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älkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 270 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 271 | 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 Lu | 6360187 | 2005-01-27 22:48:12 +0000 | [diff] [blame] | 298 | } |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 299 | } |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 300 | fixed_mtrr_types_initialized = 1; |
| 301 | } |
| 302 | |
| 303 | static 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 315 | 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 Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 346 | for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 347 | 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 349 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 350 | disable_cache(); |
| 351 | for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) |
| 352 | wrmsr(msr_index[i], fixed_msrs[i]); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 353 | enable_cache(); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 356 | void x86_setup_fixed_mtrrs_no_enable(void) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 357 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 358 | calc_fixed_mtrrs(); |
| 359 | commit_fixed_mtrrs(); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 360 | } |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 361 | |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 362 | void 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 Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 370 | struct var_mtrr_regs { |
| 371 | msr_t base; |
| 372 | msr_t mask; |
| 373 | }; |
| 374 | |
| 375 | struct 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. */ |
| 382 | static struct var_mtrr_solution mtrr_global_solution; |
| 383 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 384 | struct var_mtrr_state { |
| 385 | struct memranges *addr_space; |
| 386 | int above4gb; |
| 387 | int address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 388 | int prepare_msrs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 389 | int mtrr_index; |
| 390 | int def_mtrr_type; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 391 | struct var_mtrr_regs *regs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 392 | }; |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 393 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 394 | static void clear_var_mtrr(int index) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 395 | { |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 396 | msr_t msr = { .lo = 0, .hi = 0 }; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 397 | |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 398 | wrmsr(MTRR_PHYS_BASE(index), msr); |
| 399 | wrmsr(MTRR_PHYS_MASK(index), msr); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 400 | } |
| 401 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 402 | static void prep_var_mtrr(struct var_mtrr_state *var_state, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 403 | uint32_t base, uint32_t size, int mtrr_type) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 404 | { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 405 | struct var_mtrr_regs *regs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 406 | 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 Menzel | 6a70dbc | 2015-10-15 12:41:53 +0200 | [diff] [blame] | 416 | 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 419 | 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 Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 435 | regs = &var_state->regs[var_state->mtrr_index]; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 436 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 437 | regs->base.lo = rbase; |
| 438 | regs->base.lo |= mtrr_type; |
| 439 | regs->base.hi = rbase >> 32; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 440 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 441 | regs->mask.lo = rsize; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 442 | regs->mask.lo |= MTRR_PHYS_MASK_VALID; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 443 | regs->mask.hi = rsize >> 32; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static void calc_var_mtrr_range(struct var_mtrr_state *var_state, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 447 | uint32_t base, uint32_t size, int mtrr_type) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 448 | { |
| 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 Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 465 | if (var_state->prepare_msrs) |
| 466 | prep_var_mtrr(var_state, base, mtrr_size, mtrr_type); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 467 | |
| 468 | size -= mtrr_size; |
| 469 | base += mtrr_size; |
| 470 | var_state->mtrr_index++; |
| 471 | } |
| 472 | } |
| 473 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 474 | static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 475 | struct range_entry *r) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 476 | { |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 477 | uint32_t a1, a2, b1, b2; |
| 478 | int mtrr_type; |
| 479 | struct range_entry *next; |
| 480 | |
| 481 | /* |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 482 | * Determine MTRRs based on the following algorithm for the given entry: |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 483 | * +------------------+ 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 Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 496 | /* The end address is within the first 1MiB. The fixed MTRRs take |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 497 | * precedence over the variable ones. Therefore this range |
| 498 | * can be ignored. */ |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 499 | if (a2 <= RANGE_1MB) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 500 | return; |
| 501 | |
| 502 | /* Again, the fixed MTRRs take precedence so the beginning |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 503 | * of the range can be set to 0 if it starts at or below 1MiB. */ |
| 504 | if (a1 <= RANGE_1MB) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 505 | 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 Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 516 | next = memranges_next_entry(var_state->addr_space, r); |
| 517 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 518 | b1 = a2; |
Aaron Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 519 | |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 520 | /* First check if a1 is >= 4GiB and the current entry is the last |
Aaron Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 521 | * 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 Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 535 | 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 Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 541 | 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 | |
| 552 | static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 553 | struct range_entry *r) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 554 | { |
| 555 | uint32_t a1, a2, b1, b2, c1, c2; |
| 556 | int mtrr_type; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 557 | |
| 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 Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 570 | */ |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 571 | mtrr_type = range_entry_mtrr_type(r); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 572 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 573 | a1 = range_entry_base_mtrr_addr(r); |
| 574 | c2 = range_entry_end_mtrr_addr(r); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 575 | |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 576 | /* The end address is within the first 1MiB. The fixed MTRRs take |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 577 | * precedence over the variable ones. Therefore this range |
| 578 | * can be ignored. */ |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 579 | if (c2 <= RANGE_1MB) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 580 | return; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 581 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 582 | /* Again, the fixed MTRRs take precedence so the beginning |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 583 | * of the range can be set to 0 if it starts at or below 1MiB. */ |
| 584 | if (a1 <= RANGE_1MB) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 585 | a1 = 0; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 586 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 587 | /* If the range starts above 4GiB the processing is done. */ |
| 588 | if (!var_state->above4gb && a1 >= RANGE_4GB) |
| 589 | return; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 590 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 591 | /* 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 595 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 596 | /* 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 601 | } |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 602 | |
| 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 609 | } |
| 610 | |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 611 | static void __calc_var_mtrrs(struct memranges *addr_space, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 612 | int above4gb, int address_bits, |
| 613 | int *num_def_wb_mtrrs, int *num_def_uc_mtrrs) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 614 | { |
| 615 | int wb_deftype_count; |
| 616 | int uc_deftype_count; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 617 | struct range_entry *r; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 618 | struct var_mtrr_state var_state; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 619 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 620 | /* The default MTRR cacheability type is determined by calculating |
Paul Menzel | 4fe9813 | 2014-01-25 15:55:28 +0100 | [diff] [blame] | 621 | * the number of MTRRs required for each MTRR type as if it was the |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 622 | * default. */ |
| 623 | var_state.addr_space = addr_space; |
Scott Duplichan | f3cce2f | 2010-11-13 19:07:59 +0000 | [diff] [blame] | 624 | var_state.above4gb = above4gb; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 625 | var_state.address_bits = address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 626 | var_state.prepare_msrs = 0; |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 627 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 628 | wb_deftype_count = 0; |
| 629 | uc_deftype_count = 0; |
Duncan Laurie | 7389fa9 | 2011-12-22 10:59:40 -0800 | [diff] [blame] | 630 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 631 | /* |
| 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 Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 636 | * 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 Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 638 | * the tag of each range regardless of final decision. UC takes |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 639 | * precedence in the MTRR architecture. Therefore, only holes can be |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 640 | * 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 Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 689 | *num_def_wb_mtrrs = wb_deftype_count; |
| 690 | *num_def_uc_mtrrs = uc_deftype_count; |
| 691 | } |
| 692 | |
| 693 | static int calc_var_mtrrs(struct memranges *addr_space, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 694 | int above4gb, int address_bits) |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 695 | { |
| 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 Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 700 | &uc_deftype_count); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 701 | |
| 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 Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 707 | MTRR_TYPE_UNCACHEABLE); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 708 | __calc_var_mtrrs(addr_space, above4gb, address_bits, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 709 | &wb_deftype_count, &uc_deftype_count); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 710 | } |
Scott Duplichan | f3cce2f | 2010-11-13 19:07:59 +0000 | [diff] [blame] | 711 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 712 | printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n", |
| 713 | wb_deftype_count, uc_deftype_count); |
Kyösti Mälkki | ffc1fb3 | 2012-07-11 14:40:19 +0300 | [diff] [blame] | 714 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 715 | 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älkki | ffc1fb3 | 2012-07-11 14:40:19 +0300 | [diff] [blame] | 722 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 723 | static void prepare_var_mtrrs(struct memranges *addr_space, int def_type, |
| 724 | int above4gb, int address_bits, |
| 725 | struct var_mtrr_solution *sol) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 726 | { |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 727 | struct range_entry *r; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 728 | struct var_mtrr_state var_state; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 729 | |
| 730 | var_state.addr_space = addr_space; |
| 731 | var_state.above4gb = above4gb; |
| 732 | var_state.address_bits = address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 733 | /* Prepare the MSRs. */ |
| 734 | var_state.prepare_msrs = 1; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 735 | var_state.mtrr_index = 0; |
| 736 | var_state.def_mtrr_type = def_type; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 737 | var_state.regs = &sol->regs[0]; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 738 | |
| 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 Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 749 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 750 | /* Update the solution. */ |
| 751 | sol->num_used = var_state.mtrr_index; |
| 752 | } |
| 753 | |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame^] | 754 | static int commit_var_mtrrs(const struct var_mtrr_solution *sol) |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 755 | { |
| 756 | int i; |
| 757 | |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame^] | 758 | 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 Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame] | 764 | /* Write out the variable MTRRs. */ |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 765 | disable_cache(); |
| 766 | for (i = 0; i < sol->num_used; i++) { |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 767 | wrmsr(MTRR_PHYS_BASE(i), sol->regs[i].base); |
| 768 | wrmsr(MTRR_PHYS_MASK(i), sol->regs[i].mask); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 769 | } |
| 770 | /* Clear the ones that are unused. */ |
| 771 | for (; i < total_mtrrs; i++) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 772 | clear_var_mtrr(i); |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame] | 773 | enable_var_mtrr(sol->mtrr_default_type); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 774 | enable_cache(); |
| 775 | |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame^] | 776 | return 0; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb) |
| 780 | { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 781 | static struct var_mtrr_solution *sol = NULL; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 782 | struct memranges *addr_space; |
| 783 | |
| 784 | addr_space = get_physical_address_space(); |
| 785 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 786 | if (sol == NULL) { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 787 | sol = &mtrr_global_solution; |
| 788 | sol->mtrr_default_type = |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 789 | calc_var_mtrrs(addr_space, !!above4gb, address_bits); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 790 | prepare_var_mtrrs(addr_space, sol->mtrr_default_type, |
| 791 | !!above4gb, address_bits, sol); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 792 | } |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 793 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 794 | commit_var_mtrrs(sol); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 797 | void x86_setup_mtrrs(void) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 798 | { |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 799 | int address_size; |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 800 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 801 | x86_setup_fixed_mtrrs(); |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 802 | address_size = cpu_phys_address_size(); |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 803 | printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", |
| 804 | address_size); |
| 805 | /* Always handle addresses above 4GiB. */ |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 806 | x86_setup_var_mtrrs(address_size, 1); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 809 | void x86_setup_mtrrs_with_detect(void) |
| 810 | { |
| 811 | detect_var_mtrrs(); |
| 812 | x86_setup_mtrrs(); |
| 813 | } |
| 814 | |
Kyösti Mälkki | 38a8fb0 | 2014-06-30 13:48:18 +0300 | [diff] [blame] | 815 | void x86_mtrr_check(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 816 | { |
| 817 | /* Only Pentium Pro and later have MTRR */ |
| 818 | msr_t msr; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 819 | printk(BIOS_DEBUG, "\nMTRR check\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 820 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 821 | msr = rdmsr(MTRR_DEF_TYPE_MSR); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 822 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 823 | printk(BIOS_DEBUG, "Fixed MTRRs : "); |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 824 | if (msr.lo & MTRR_DEF_TYPE_FIX_EN) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 825 | printk(BIOS_DEBUG, "Enabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 826 | else |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 827 | printk(BIOS_DEBUG, "Disabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 828 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 829 | printk(BIOS_DEBUG, "Variable MTRRs: "); |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 830 | if (msr.lo & MTRR_DEF_TYPE_EN) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 831 | printk(BIOS_DEBUG, "Enabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 832 | else |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 833 | printk(BIOS_DEBUG, "Disabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 834 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 835 | printk(BIOS_DEBUG, "\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 836 | |
| 837 | post_code(0x93); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 838 | } |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 839 | |
| 840 | static bool put_back_original_solution; |
| 841 | |
| 842 | void 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 Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame^] | 882 | |
| 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 Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 889 | |
| 890 | memranges_teardown(&addr_space); |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static void remove_temp_solution(void *unused) |
| 894 | { |
| 895 | if (put_back_original_solution) |
| 896 | commit_var_mtrrs(&mtrr_global_solution); |
| 897 | } |
| 898 | |
| 899 | BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, remove_temp_solution, NULL); |
| 900 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, remove_temp_solution, NULL); |