Elyes HAOUAS | 3a7346c | 2020-05-07 07:46:17 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 3 | /* |
Martin Roth | d57ace2 | 2019-08-31 10:48:37 -0600 | [diff] [blame] | 4 | * 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 Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 6 | * |
Lee Leahy | c591707 | 2017-03-15 16:38:51 -0700 | [diff] [blame] | 7 | * Reference: Intel Architecture Software Developer's Manual, Volume 3: System |
| 8 | * Programming |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 9 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 10 | |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 11 | #include <stddef.h> |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 12 | #include <string.h> |
Aaron Durbin | bebf669 | 2013-04-24 20:59:43 -0500 | [diff] [blame] | 13 | #include <bootstate.h> |
Elyes HAOUAS | d26844c | 2019-06-21 07:31:40 +0200 | [diff] [blame] | 14 | #include <commonlib/helpers.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 15 | #include <console/console.h> |
| 16 | #include <device/device.h> |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 17 | #include <device/pci_ids.h> |
Aaron Durbin | ebf142a | 2013-03-29 16:23:23 -0500 | [diff] [blame] | 18 | #include <cpu/cpu.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 19 | #include <cpu/x86/msr.h> |
| 20 | #include <cpu/x86/mtrr.h> |
| 21 | #include <cpu/x86/cache.h> |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 22 | #include <memrange.h> |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 23 | #include <cpu/amd/mtrr.h> |
Richard Spiegel | b28025a | 2019-02-20 11:00:19 -0700 | [diff] [blame] | 24 | #include <assert.h> |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 25 | #if CONFIG(X86_AMD_FIXED_MTRRS) |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 26 | #define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM) |
| 27 | #else |
| 28 | #define MTRR_FIXED_WRBACK_BITS 0 |
| 29 | #endif |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 30 | |
Tim Wawrzynczak | 6fcc46d | 2021-04-19 13:47:36 -0600 | [diff] [blame] | 31 | #define MIN_MTRRS 8 |
| 32 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 33 | /* |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame] | 34 | * Static storage size for variable MTRRs. It's sized sufficiently large to |
| 35 | * handle different types of CPUs. Empirically, 16 variable MTRRs has not |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 36 | * yet been observed. |
| 37 | */ |
| 38 | #define NUM_MTRR_STATIC_STORAGE 16 |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 39 | |
Tim Wawrzynczak | 6fcc46d | 2021-04-19 13:47:36 -0600 | [diff] [blame] | 40 | static int total_mtrrs; |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 41 | |
| 42 | static void detect_var_mtrrs(void) |
| 43 | { |
| 44 | msr_t msr; |
| 45 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 46 | msr = rdmsr(MTRR_CAP_MSR); |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 47 | |
| 48 | total_mtrrs = msr.lo & 0xff; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 49 | |
| 50 | if (total_mtrrs > NUM_MTRR_STATIC_STORAGE) { |
| 51 | printk(BIOS_WARNING, |
| 52 | "MTRRs detected (%d) > NUM_MTRR_STATIC_STORAGE (%d)\n", |
| 53 | total_mtrrs, NUM_MTRR_STATIC_STORAGE); |
| 54 | total_mtrrs = NUM_MTRR_STATIC_STORAGE; |
| 55 | } |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 58 | void enable_fixed_mtrr(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 59 | { |
| 60 | msr_t msr; |
| 61 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 62 | msr = rdmsr(MTRR_DEF_TYPE_MSR); |
| 63 | msr.lo |= MTRR_DEF_TYPE_EN | MTRR_DEF_TYPE_FIX_EN; |
| 64 | wrmsr(MTRR_DEF_TYPE_MSR, msr); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 67 | void fixed_mtrrs_expose_amd_rwdram(void) |
| 68 | { |
| 69 | msr_t syscfg; |
| 70 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 71 | if (!CONFIG(X86_AMD_FIXED_MTRRS)) |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 72 | return; |
| 73 | |
| 74 | syscfg = rdmsr(SYSCFG_MSR); |
| 75 | syscfg.lo |= SYSCFG_MSR_MtrrFixDramModEn; |
| 76 | wrmsr(SYSCFG_MSR, syscfg); |
| 77 | } |
| 78 | |
| 79 | void fixed_mtrrs_hide_amd_rwdram(void) |
| 80 | { |
| 81 | msr_t syscfg; |
| 82 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 83 | if (!CONFIG(X86_AMD_FIXED_MTRRS)) |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 84 | return; |
| 85 | |
| 86 | syscfg = rdmsr(SYSCFG_MSR); |
| 87 | syscfg.lo &= ~SYSCFG_MSR_MtrrFixDramModEn; |
| 88 | wrmsr(SYSCFG_MSR, syscfg); |
| 89 | } |
| 90 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 91 | static void enable_var_mtrr(unsigned char deftype) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 92 | { |
| 93 | msr_t msr; |
| 94 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 95 | msr = rdmsr(MTRR_DEF_TYPE_MSR); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 96 | msr.lo &= ~0xff; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 97 | msr.lo |= MTRR_DEF_TYPE_EN | deftype; |
| 98 | wrmsr(MTRR_DEF_TYPE_MSR, msr); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 101 | #define MTRR_VERBOSE_LEVEL BIOS_NEVER |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 102 | |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 103 | /* MTRRs are at a 4KiB granularity. */ |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 104 | #define RANGE_SHIFT 12 |
| 105 | #define ADDR_SHIFT_TO_RANGE_SHIFT(x) \ |
| 106 | (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT) |
| 107 | #define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT) |
| 108 | #define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT) |
| 109 | #define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR) |
| 110 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 111 | /* Helpful constants. */ |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 112 | #define RANGE_1MB PHYS_TO_RANGE_ADDR(1ULL << 20) |
| 113 | #define RANGE_4GB (1ULL << (ADDR_SHIFT_TO_RANGE_SHIFT(32))) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 114 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 115 | #define MTRR_ALGO_SHIFT (8) |
| 116 | #define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 117 | |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 118 | static inline uint64_t range_entry_base_mtrr_addr(struct range_entry *r) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 119 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 120 | return PHYS_TO_RANGE_ADDR(range_entry_base(r)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 123 | static inline uint64_t range_entry_end_mtrr_addr(struct range_entry *r) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 124 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 125 | return PHYS_TO_RANGE_ADDR(range_entry_end(r)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 128 | static inline int range_entry_mtrr_type(struct range_entry *r) |
| 129 | { |
| 130 | return range_entry_tag(r) & MTRR_TAG_MASK; |
| 131 | } |
| 132 | |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 133 | static int filter_vga_wrcomb(struct device *dev, struct resource *res) |
| 134 | { |
| 135 | /* Only handle PCI devices. */ |
| 136 | if (dev->path.type != DEVICE_PATH_PCI) |
| 137 | return 0; |
| 138 | |
| 139 | /* Only handle VGA class devices. */ |
| 140 | if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)) |
| 141 | return 0; |
| 142 | |
| 143 | /* Add resource as write-combining in the address space. */ |
| 144 | return 1; |
| 145 | } |
| 146 | |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 147 | static void print_physical_address_space(const struct memranges *addr_space, |
| 148 | const char *identifier) |
| 149 | { |
| 150 | const struct range_entry *r; |
| 151 | |
| 152 | if (identifier) |
| 153 | printk(BIOS_DEBUG, "MTRR: %s Physical address space:\n", |
| 154 | identifier); |
| 155 | else |
| 156 | printk(BIOS_DEBUG, "MTRR: Physical address space:\n"); |
| 157 | |
| 158 | memranges_each_entry(r, addr_space) |
| 159 | printk(BIOS_DEBUG, |
| 160 | "0x%016llx - 0x%016llx size 0x%08llx type %ld\n", |
| 161 | range_entry_base(r), range_entry_end(r), |
| 162 | range_entry_size(r), range_entry_tag(r)); |
| 163 | } |
| 164 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 165 | static struct memranges *get_physical_address_space(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 166 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 167 | static struct memranges *addr_space; |
| 168 | static struct memranges addr_space_storage; |
Duncan Laurie | 7389fa9 | 2011-12-22 10:59:40 -0800 | [diff] [blame] | 169 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 170 | /* In order to handle some chipsets not being able to pre-determine |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 171 | * uncacheable ranges, such as graphics memory, at resource insertion |
| 172 | * time remove uncacheable regions from the cacheable ones. */ |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 173 | if (addr_space == NULL) { |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 174 | unsigned long mask; |
| 175 | unsigned long match; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 176 | |
| 177 | addr_space = &addr_space_storage; |
| 178 | |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 179 | mask = IORESOURCE_CACHEABLE; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 180 | /* Collect cacheable and uncacheable address ranges. The |
| 181 | * uncacheable regions take precedence over the cacheable |
| 182 | * regions. */ |
| 183 | memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK); |
| 184 | memranges_add_resources(addr_space, mask, 0, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 185 | MTRR_TYPE_UNCACHEABLE); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 186 | |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 187 | /* Handle any write combining resources. Only prefetchable |
Vladimir Serbinenko | 30fe612 | 2014-02-05 23:25:28 +0100 | [diff] [blame] | 188 | * resources are appropriate for this MTRR type. */ |
| 189 | match = IORESOURCE_PREFETCH; |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 190 | mask |= match; |
Lee Leahy | c591707 | 2017-03-15 16:38:51 -0700 | [diff] [blame] | 191 | memranges_add_resources_filter(addr_space, mask, match, |
| 192 | MTRR_TYPE_WRCOMB, filter_vga_wrcomb); |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 193 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 194 | /* The address space below 4GiB is special. It needs to be |
Martin Roth | 2f91403 | 2016-01-15 10:20:11 -0700 | [diff] [blame] | 195 | * covered entirely by range entries so that MTRR calculations |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 196 | * can be properly done for the full 32-bit address space. |
| 197 | * Therefore, ensure holes are filled up to 4GiB as |
| 198 | * uncacheable */ |
| 199 | memranges_fill_holes_up_to(addr_space, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 200 | RANGE_TO_PHYS_ADDR(RANGE_4GB), |
| 201 | MTRR_TYPE_UNCACHEABLE); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 202 | |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 203 | print_physical_address_space(addr_space, NULL); |
Carl-Daniel Hailfinger | 7dde1da | 2009-02-11 16:57:32 +0000 | [diff] [blame] | 204 | } |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 205 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 206 | return addr_space; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 209 | /* Fixed MTRR descriptor. This structure defines the step size and begin |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 210 | * and end (exclusive) address covered by a set of fixed MTRR MSRs. |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 211 | * It also describes the offset in byte intervals to store the calculated MTRR |
| 212 | * type in an array. */ |
| 213 | struct fixed_mtrr_desc { |
| 214 | uint32_t begin; |
| 215 | uint32_t end; |
| 216 | uint32_t step; |
| 217 | int range_index; |
| 218 | int msr_index_base; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 221 | /* Shared MTRR calculations. Can be reused by APs. */ |
| 222 | static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES]; |
| 223 | |
| 224 | /* Fixed MTRR descriptors. */ |
| 225 | static const struct fixed_mtrr_desc fixed_mtrr_desc[] = { |
| 226 | { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000), |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 227 | PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 }, |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 228 | { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000), |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 229 | PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 }, |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 230 | { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000), |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 231 | PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 }, |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static void calc_fixed_mtrrs(void) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 235 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 236 | static int fixed_mtrr_types_initialized; |
| 237 | struct memranges *phys_addr_space; |
| 238 | struct range_entry *r; |
| 239 | const struct fixed_mtrr_desc *desc; |
| 240 | const struct fixed_mtrr_desc *last_desc; |
| 241 | uint32_t begin; |
| 242 | uint32_t end; |
| 243 | int type_index; |
| 244 | |
| 245 | if (fixed_mtrr_types_initialized) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 246 | return; |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 247 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 248 | phys_addr_space = get_physical_address_space(); |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 249 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 250 | /* Set all fixed ranges to uncacheable first. */ |
| 251 | 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] | 252 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 253 | desc = &fixed_mtrr_desc[0]; |
| 254 | 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] | 255 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 256 | memranges_each_entry(r, phys_addr_space) { |
| 257 | begin = range_entry_base_mtrr_addr(r); |
| 258 | end = range_entry_end_mtrr_addr(r); |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 259 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 260 | if (begin >= last_desc->end) |
| 261 | break; |
| 262 | |
| 263 | if (end > last_desc->end) |
| 264 | end = last_desc->end; |
| 265 | |
| 266 | /* Get to the correct fixed mtrr descriptor. */ |
| 267 | while (begin >= desc->end) |
| 268 | desc++; |
| 269 | |
| 270 | type_index = desc->range_index; |
| 271 | type_index += (begin - desc->begin) / desc->step; |
| 272 | |
| 273 | while (begin != end) { |
| 274 | unsigned char type; |
| 275 | |
| 276 | type = range_entry_tag(r); |
| 277 | printk(MTRR_VERBOSE_LEVEL, |
| 278 | "MTRR addr 0x%x-0x%x set to %d type @ %d\n", |
| 279 | begin, begin + desc->step, type, type_index); |
| 280 | if (type == MTRR_TYPE_WRBACK) |
| 281 | type |= MTRR_FIXED_WRBACK_BITS; |
| 282 | fixed_mtrr_types[type_index] = type; |
| 283 | type_index++; |
| 284 | begin += desc->step; |
| 285 | if (begin == desc->end) |
| 286 | desc++; |
Yinghai Lu | 6360187 | 2005-01-27 22:48:12 +0000 | [diff] [blame] | 287 | } |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 288 | } |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 289 | fixed_mtrr_types_initialized = 1; |
| 290 | } |
| 291 | |
| 292 | static void commit_fixed_mtrrs(void) |
| 293 | { |
| 294 | int i; |
| 295 | int j; |
| 296 | int msr_num; |
| 297 | int type_index; |
| 298 | /* 8 ranges per msr. */ |
| 299 | msr_t fixed_msrs[NUM_FIXED_MTRRS]; |
| 300 | unsigned long msr_index[NUM_FIXED_MTRRS]; |
| 301 | |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 302 | fixed_mtrrs_expose_amd_rwdram(); |
| 303 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 304 | memset(&fixed_msrs, 0, sizeof(fixed_msrs)); |
| 305 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 306 | msr_num = 0; |
| 307 | type_index = 0; |
| 308 | for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) { |
| 309 | const struct fixed_mtrr_desc *desc; |
| 310 | int num_ranges; |
| 311 | |
| 312 | desc = &fixed_mtrr_desc[i]; |
| 313 | num_ranges = (desc->end - desc->begin) / desc->step; |
| 314 | for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) { |
| 315 | msr_index[msr_num] = desc->msr_index_base + |
| 316 | (j / RANGES_PER_FIXED_MTRR); |
| 317 | fixed_msrs[msr_num].lo |= |
| 318 | fixed_mtrr_types[type_index++] << 0; |
| 319 | fixed_msrs[msr_num].lo |= |
| 320 | fixed_mtrr_types[type_index++] << 8; |
| 321 | fixed_msrs[msr_num].lo |= |
| 322 | fixed_mtrr_types[type_index++] << 16; |
| 323 | fixed_msrs[msr_num].lo |= |
| 324 | fixed_mtrr_types[type_index++] << 24; |
| 325 | fixed_msrs[msr_num].hi |= |
| 326 | fixed_mtrr_types[type_index++] << 0; |
| 327 | fixed_msrs[msr_num].hi |= |
| 328 | fixed_mtrr_types[type_index++] << 8; |
| 329 | fixed_msrs[msr_num].hi |= |
| 330 | fixed_mtrr_types[type_index++] << 16; |
| 331 | fixed_msrs[msr_num].hi |= |
| 332 | fixed_mtrr_types[type_index++] << 24; |
| 333 | msr_num++; |
| 334 | } |
| 335 | } |
| 336 | |
Jacob Garber | 5b92272 | 2019-05-28 11:47:49 -0600 | [diff] [blame] | 337 | /* Ensure that both arrays were fully initialized */ |
| 338 | ASSERT(msr_num == NUM_FIXED_MTRRS) |
| 339 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 340 | for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 341 | printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n", |
| 342 | msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 343 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 344 | disable_cache(); |
| 345 | for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) |
| 346 | wrmsr(msr_index[i], fixed_msrs[i]); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 347 | enable_cache(); |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 348 | fixed_mtrrs_hide_amd_rwdram(); |
| 349 | |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 352 | void x86_setup_fixed_mtrrs_no_enable(void) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 353 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 354 | calc_fixed_mtrrs(); |
| 355 | commit_fixed_mtrrs(); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 356 | } |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 357 | |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 358 | void x86_setup_fixed_mtrrs(void) |
| 359 | { |
| 360 | x86_setup_fixed_mtrrs_no_enable(); |
| 361 | |
| 362 | printk(BIOS_SPEW, "call enable_fixed_mtrr()\n"); |
| 363 | enable_fixed_mtrr(); |
| 364 | } |
| 365 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 366 | struct var_mtrr_regs { |
| 367 | msr_t base; |
| 368 | msr_t mask; |
| 369 | }; |
| 370 | |
| 371 | struct var_mtrr_solution { |
| 372 | int mtrr_default_type; |
| 373 | int num_used; |
| 374 | struct var_mtrr_regs regs[NUM_MTRR_STATIC_STORAGE]; |
| 375 | }; |
| 376 | |
| 377 | /* Global storage for variable MTRR solution. */ |
| 378 | static struct var_mtrr_solution mtrr_global_solution; |
| 379 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 380 | struct var_mtrr_state { |
| 381 | struct memranges *addr_space; |
| 382 | int above4gb; |
| 383 | int address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 384 | int prepare_msrs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 385 | int mtrr_index; |
| 386 | int def_mtrr_type; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 387 | struct var_mtrr_regs *regs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 388 | }; |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 389 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 390 | static void clear_var_mtrr(int index) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 391 | { |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 392 | msr_t msr = { .lo = 0, .hi = 0 }; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 393 | |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 394 | wrmsr(MTRR_PHYS_BASE(index), msr); |
| 395 | wrmsr(MTRR_PHYS_MASK(index), msr); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Tim Wawrzynczak | 6fcc46d | 2021-04-19 13:47:36 -0600 | [diff] [blame] | 398 | static int get_os_reserved_mtrrs(void) |
| 399 | { |
| 400 | return CONFIG(RESERVE_MTRRS_FOR_OS) ? 2 : 0; |
| 401 | } |
| 402 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 403 | static void prep_var_mtrr(struct var_mtrr_state *var_state, |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 404 | uint64_t base, uint64_t size, int mtrr_type) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 405 | { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 406 | struct var_mtrr_regs *regs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 407 | resource_t rbase; |
| 408 | resource_t rsize; |
| 409 | resource_t mask; |
| 410 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 411 | if (var_state->mtrr_index >= total_mtrrs) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame^] | 412 | printk(BIOS_ERR, "Not enough MTRRs available! MTRR index is %d with %d MTRRs in total.\n", |
Paul Menzel | 6a70dbc | 2015-10-15 12:41:53 +0200 | [diff] [blame] | 413 | var_state->mtrr_index, total_mtrrs); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
Tim Wawrzynczak | 6fcc46d | 2021-04-19 13:47:36 -0600 | [diff] [blame] | 417 | /* |
| 418 | * If desired, 2 variable MTRRs are attempted to be saved for the OS to |
| 419 | * use. However, it's more important to try to map the full address |
| 420 | * space properly. |
| 421 | */ |
| 422 | if (var_state->mtrr_index >= total_mtrrs - get_os_reserved_mtrrs()) |
| 423 | printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n"); |
| 424 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 425 | rbase = base; |
| 426 | rsize = size; |
| 427 | |
| 428 | rbase = RANGE_TO_PHYS_ADDR(rbase); |
| 429 | rsize = RANGE_TO_PHYS_ADDR(rsize); |
| 430 | rsize = -rsize; |
| 431 | |
| 432 | mask = (1ULL << var_state->address_bits) - 1; |
| 433 | rsize = rsize & mask; |
| 434 | |
| 435 | printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n", |
| 436 | var_state->mtrr_index, rbase, rsize, mtrr_type); |
| 437 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 438 | regs = &var_state->regs[var_state->mtrr_index]; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 439 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 440 | regs->base.lo = rbase; |
| 441 | regs->base.lo |= mtrr_type; |
| 442 | regs->base.hi = rbase >> 32; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 443 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 444 | regs->mask.lo = rsize; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 445 | regs->mask.lo |= MTRR_PHYS_MASK_VALID; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 446 | regs->mask.hi = rsize >> 32; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 449 | /* |
| 450 | * fls64: find least significant bit set in a 64-bit word |
| 451 | * As samples, fls64(0x0) = 64; fls64(0x4400) = 10; |
| 452 | * fls64(0x40400000000) = 34. |
| 453 | */ |
| 454 | static uint32_t fls64(uint64_t x) |
| 455 | { |
| 456 | uint32_t lo = (uint32_t)x; |
| 457 | if (lo) |
| 458 | return fls(lo); |
| 459 | uint32_t hi = x >> 32; |
| 460 | return fls(hi) + 32; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * fms64: find most significant bit set in a 64-bit word |
| 465 | * As samples, fms64(0x0) = 0; fms64(0x4400) = 14; |
| 466 | * fms64(0x40400000000) = 42. |
| 467 | */ |
| 468 | static uint32_t fms64(uint64_t x) |
| 469 | { |
| 470 | uint32_t hi = (uint32_t)(x >> 32); |
| 471 | if (!hi) |
| 472 | return fms((uint32_t)x); |
| 473 | return fms(hi) + 32; |
| 474 | } |
| 475 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 476 | static void calc_var_mtrr_range(struct var_mtrr_state *var_state, |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 477 | uint64_t base, uint64_t size, int mtrr_type) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 478 | { |
| 479 | while (size != 0) { |
| 480 | uint32_t addr_lsb; |
| 481 | uint32_t size_msb; |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 482 | uint64_t mtrr_size; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 483 | |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 484 | addr_lsb = fls64(base); |
| 485 | size_msb = fms64(size); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 486 | |
| 487 | /* All MTRR entries need to have their base aligned to the mask |
| 488 | * size. The maximum size is calculated by a function of the |
| 489 | * min base bit set and maximum size bit set. */ |
| 490 | if (addr_lsb > size_msb) |
Jonathan Zhang | 8f594b7 | 2020-10-23 15:20:22 -0700 | [diff] [blame] | 491 | mtrr_size = 1ULL << size_msb; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 492 | else |
Jonathan Zhang | 8f594b7 | 2020-10-23 15:20:22 -0700 | [diff] [blame] | 493 | mtrr_size = 1ULL << addr_lsb; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 494 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 495 | if (var_state->prepare_msrs) |
| 496 | prep_var_mtrr(var_state, base, mtrr_size, mtrr_type); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 497 | |
| 498 | size -= mtrr_size; |
| 499 | base += mtrr_size; |
| 500 | var_state->mtrr_index++; |
| 501 | } |
| 502 | } |
| 503 | |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 504 | static uint64_t optimize_var_mtrr_hole(const uint64_t base, |
| 505 | const uint64_t hole, |
Nico Huber | bd5fb66 | 2017-10-07 13:40:19 +0200 | [diff] [blame] | 506 | const uint64_t limit, |
| 507 | const int carve_hole) |
| 508 | { |
| 509 | /* |
| 510 | * With default type UC, we can potentially optimize a WB |
| 511 | * range with unaligned upper end, by aligning it up and |
| 512 | * carving the added "hole" out again. |
| 513 | * |
| 514 | * To optimize the upper end of the hole, we will test |
| 515 | * how many MTRRs calc_var_mtrr_range() will spend for any |
| 516 | * alignment of the hole's upper end. |
| 517 | * |
| 518 | * We take four parameters, the lower end of the WB range |
| 519 | * `base`, upper end of the WB range as start of the `hole`, |
| 520 | * a `limit` how far we may align the upper end of the hole |
| 521 | * up and a flag `carve_hole` whether we should count MTRRs |
| 522 | * for carving the hole out. We return the optimal upper end |
| 523 | * for the hole (which may be the same as the end of the WB |
| 524 | * range in case we don't gain anything by aligning up). |
| 525 | */ |
| 526 | |
| 527 | const int dont_care = 0; |
| 528 | struct var_mtrr_state var_state = { 0, }; |
| 529 | |
| 530 | unsigned int align, best_count; |
| 531 | uint32_t best_end = hole; |
| 532 | |
| 533 | /* calculate MTRR count for the WB range alone (w/o a hole) */ |
| 534 | calc_var_mtrr_range(&var_state, base, hole - base, dont_care); |
| 535 | best_count = var_state.mtrr_index; |
| 536 | var_state.mtrr_index = 0; |
| 537 | |
| 538 | for (align = fls(hole) + 1; align <= fms(hole); ++align) { |
| 539 | const uint64_t hole_end = ALIGN_UP((uint64_t)hole, 1 << align); |
| 540 | if (hole_end > limit) |
| 541 | break; |
| 542 | |
| 543 | /* calculate MTRR count for this alignment */ |
| 544 | calc_var_mtrr_range( |
| 545 | &var_state, base, hole_end - base, dont_care); |
| 546 | if (carve_hole) |
| 547 | calc_var_mtrr_range( |
| 548 | &var_state, hole, hole_end - hole, dont_care); |
| 549 | |
| 550 | if (var_state.mtrr_index < best_count) { |
| 551 | best_count = var_state.mtrr_index; |
| 552 | best_end = hole_end; |
| 553 | } |
| 554 | var_state.mtrr_index = 0; |
| 555 | } |
| 556 | |
| 557 | return best_end; |
| 558 | } |
| 559 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 560 | 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] | 561 | struct range_entry *r) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 562 | { |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 563 | uint64_t a1, a2, b1, b2; |
Nico Huber | bd5fb66 | 2017-10-07 13:40:19 +0200 | [diff] [blame] | 564 | int mtrr_type, carve_hole; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 565 | |
| 566 | /* |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 567 | * Determine MTRRs based on the following algorithm for the given entry: |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 568 | * +------------------+ b2 = ALIGN_UP(end) |
| 569 | * | 0 or more bytes | <-- hole is carved out between b1 and b2 |
Nico Huber | bd5fb66 | 2017-10-07 13:40:19 +0200 | [diff] [blame] | 570 | * +------------------+ a2 = b1 = original end |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 571 | * | | |
| 572 | * +------------------+ a1 = begin |
| 573 | * |
Nico Huber | bd5fb66 | 2017-10-07 13:40:19 +0200 | [diff] [blame] | 574 | * Thus, there are up to 2 sub-ranges to configure variable MTRRs for. |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 575 | */ |
| 576 | mtrr_type = range_entry_mtrr_type(r); |
| 577 | |
| 578 | a1 = range_entry_base_mtrr_addr(r); |
| 579 | a2 = range_entry_end_mtrr_addr(r); |
| 580 | |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 581 | /* The end address is within the first 1MiB. The fixed MTRRs take |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 582 | * precedence over the variable ones. Therefore this range |
| 583 | * can be ignored. */ |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 584 | if (a2 <= RANGE_1MB) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 585 | return; |
| 586 | |
| 587 | /* Again, the fixed MTRRs take precedence so the beginning |
Aaron Durbin | a38677b | 2016-07-21 14:26:34 -0500 | [diff] [blame] | 588 | * of the range can be set to 0 if it starts at or below 1MiB. */ |
| 589 | if (a1 <= RANGE_1MB) |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 590 | a1 = 0; |
| 591 | |
| 592 | /* If the range starts above 4GiB the processing is done. */ |
| 593 | if (!var_state->above4gb && a1 >= RANGE_4GB) |
| 594 | return; |
| 595 | |
| 596 | /* Clip the upper address to 4GiB if addresses above 4GiB |
| 597 | * are not being processed. */ |
| 598 | if (!var_state->above4gb && a2 > RANGE_4GB) |
| 599 | a2 = RANGE_4GB; |
| 600 | |
| 601 | b1 = a2; |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 602 | b2 = a2; |
| 603 | carve_hole = 0; |
Aaron Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 604 | |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 605 | /* We only consider WB type ranges for hole-carving. */ |
| 606 | if (mtrr_type == MTRR_TYPE_WRBACK) { |
| 607 | struct range_entry *next; |
| 608 | uint64_t b2_limit; |
| 609 | /* |
| 610 | * Depending on the type of the next range, there are three |
| 611 | * different situations to handle: |
| 612 | * |
| 613 | * 1. WB range is last in address space: |
| 614 | * Aligning up, up to the next power of 2, may gain us |
| 615 | * something. |
| 616 | * |
| 617 | * 2. The next range is of type UC: |
| 618 | * We may align up, up to the _end_ of the next range. If |
| 619 | * there is a gap between the current and the next range, |
| 620 | * it would have been covered by the default type UC anyway. |
| 621 | * |
| 622 | * 3. The next range is not of type UC: |
| 623 | * We may align up, up to the _base_ of the next range. This |
| 624 | * may either be the end of the current range (if the next |
| 625 | * range follows immediately) or the end of the gap between |
| 626 | * the ranges. |
| 627 | */ |
| 628 | next = memranges_next_entry(var_state->addr_space, r); |
| 629 | if (next == NULL) { |
| 630 | b2_limit = ALIGN_UP((uint64_t)b1, 1 << fms(b1)); |
| 631 | /* If it's the last range above 4GiB, we won't carve |
| 632 | the hole out. If an OS wanted to move MMIO there, |
| 633 | it would have to override the MTRR setting using |
| 634 | PAT just like it would with WB as default type. */ |
| 635 | carve_hole = a1 < RANGE_4GB; |
| 636 | } else if (range_entry_mtrr_type(next) |
| 637 | == MTRR_TYPE_UNCACHEABLE) { |
| 638 | b2_limit = range_entry_end_mtrr_addr(next); |
| 639 | carve_hole = 1; |
| 640 | } else { |
| 641 | b2_limit = range_entry_base_mtrr_addr(next); |
| 642 | carve_hole = 1; |
| 643 | } |
| 644 | b2 = optimize_var_mtrr_hole(a1, b1, b2_limit, carve_hole); |
Aaron Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 645 | } |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 646 | |
| 647 | calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type); |
Nico Huber | bd5fb66 | 2017-10-07 13:40:19 +0200 | [diff] [blame] | 648 | if (carve_hole && b2 != b1) { |
| 649 | calc_var_mtrr_range(var_state, b1, b2 - b1, |
| 650 | MTRR_TYPE_UNCACHEABLE); |
| 651 | } |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 652 | } |
| 653 | |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 654 | static void __calc_var_mtrrs(struct memranges *addr_space, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 655 | int above4gb, int address_bits, |
| 656 | int *num_def_wb_mtrrs, int *num_def_uc_mtrrs) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 657 | { |
| 658 | int wb_deftype_count; |
| 659 | int uc_deftype_count; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 660 | struct range_entry *r; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 661 | struct var_mtrr_state var_state; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 662 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 663 | /* The default MTRR cacheability type is determined by calculating |
Paul Menzel | 4fe9813 | 2014-01-25 15:55:28 +0100 | [diff] [blame] | 664 | * 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] | 665 | * default. */ |
| 666 | var_state.addr_space = addr_space; |
Scott Duplichan | f3cce2f | 2010-11-13 19:07:59 +0000 | [diff] [blame] | 667 | var_state.above4gb = above4gb; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 668 | var_state.address_bits = address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 669 | var_state.prepare_msrs = 0; |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 670 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 671 | wb_deftype_count = 0; |
| 672 | uc_deftype_count = 0; |
Duncan Laurie | 7389fa9 | 2011-12-22 10:59:40 -0800 | [diff] [blame] | 673 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 674 | /* |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 675 | * For each range do 2 calculations: |
| 676 | * 1. UC as default type with possible holes at top of range. |
| 677 | * 2. WB as default. |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 678 | * The lowest count is then used as default after totaling all |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 679 | * MTRRs. UC takes precedence in the MTRR architecture. There- |
| 680 | * fore, only holes can be used when the type of the region is |
| 681 | * MTRR_TYPE_WRBACK with MTRR_TYPE_UNCACHEABLE as the default |
| 682 | * type. |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 683 | */ |
| 684 | memranges_each_entry(r, var_state.addr_space) { |
| 685 | int mtrr_type; |
| 686 | |
| 687 | mtrr_type = range_entry_mtrr_type(r); |
| 688 | |
| 689 | if (mtrr_type != MTRR_TYPE_UNCACHEABLE) { |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 690 | var_state.mtrr_index = 0; |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 691 | var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE; |
| 692 | calc_var_mtrrs_with_hole(&var_state, r); |
| 693 | uc_deftype_count += var_state.mtrr_index; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | if (mtrr_type != MTRR_TYPE_WRBACK) { |
| 697 | var_state.mtrr_index = 0; |
| 698 | var_state.def_mtrr_type = MTRR_TYPE_WRBACK; |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 699 | calc_var_mtrrs_with_hole(&var_state, r); |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 700 | wb_deftype_count += var_state.mtrr_index; |
| 701 | } |
| 702 | } |
Jonathan Zhang | 320ad93 | 2020-10-14 15:07:51 -0700 | [diff] [blame] | 703 | |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 704 | *num_def_wb_mtrrs = wb_deftype_count; |
| 705 | *num_def_uc_mtrrs = uc_deftype_count; |
| 706 | } |
| 707 | |
| 708 | static int calc_var_mtrrs(struct memranges *addr_space, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 709 | int above4gb, int address_bits) |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 710 | { |
| 711 | int wb_deftype_count = 0; |
| 712 | int uc_deftype_count = 0; |
| 713 | |
| 714 | __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 715 | &uc_deftype_count); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 716 | |
Tim Wawrzynczak | 6fcc46d | 2021-04-19 13:47:36 -0600 | [diff] [blame] | 717 | const int bios_mtrrs = total_mtrrs - get_os_reserved_mtrrs(); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 718 | if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) { |
| 719 | printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. " |
| 720 | "WB/UC MTRR counts: %d/%d > %d.\n", |
| 721 | wb_deftype_count, uc_deftype_count, bios_mtrrs); |
| 722 | memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 723 | MTRR_TYPE_UNCACHEABLE); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 724 | __calc_var_mtrrs(addr_space, above4gb, address_bits, |
Lee Leahy | a07d0dd | 2017-03-15 14:25:22 -0700 | [diff] [blame] | 725 | &wb_deftype_count, &uc_deftype_count); |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 726 | } |
Scott Duplichan | f3cce2f | 2010-11-13 19:07:59 +0000 | [diff] [blame] | 727 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 728 | printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n", |
| 729 | wb_deftype_count, uc_deftype_count); |
Kyösti Mälkki | ffc1fb3 | 2012-07-11 14:40:19 +0300 | [diff] [blame] | 730 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 731 | if (wb_deftype_count < uc_deftype_count) { |
| 732 | printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n"); |
| 733 | return MTRR_TYPE_WRBACK; |
| 734 | } |
| 735 | printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n"); |
| 736 | return MTRR_TYPE_UNCACHEABLE; |
| 737 | } |
Kyösti Mälkki | ffc1fb3 | 2012-07-11 14:40:19 +0300 | [diff] [blame] | 738 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 739 | static void prepare_var_mtrrs(struct memranges *addr_space, int def_type, |
| 740 | int above4gb, int address_bits, |
| 741 | struct var_mtrr_solution *sol) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 742 | { |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 743 | struct range_entry *r; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 744 | struct var_mtrr_state var_state; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 745 | |
| 746 | var_state.addr_space = addr_space; |
| 747 | var_state.above4gb = above4gb; |
| 748 | var_state.address_bits = address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 749 | /* Prepare the MSRs. */ |
| 750 | var_state.prepare_msrs = 1; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 751 | var_state.mtrr_index = 0; |
| 752 | var_state.def_mtrr_type = def_type; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 753 | var_state.regs = &sol->regs[0]; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 754 | |
| 755 | memranges_each_entry(r, var_state.addr_space) { |
| 756 | if (range_entry_mtrr_type(r) == def_type) |
| 757 | continue; |
Nico Huber | 64f0bcb | 2017-10-07 16:37:04 +0200 | [diff] [blame] | 758 | calc_var_mtrrs_with_hole(&var_state, r); |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 759 | } |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 760 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 761 | /* Update the solution. */ |
| 762 | sol->num_used = var_state.mtrr_index; |
| 763 | } |
| 764 | |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame] | 765 | static int commit_var_mtrrs(const struct var_mtrr_solution *sol) |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 766 | { |
| 767 | int i; |
| 768 | |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame] | 769 | if (sol->num_used > total_mtrrs) { |
| 770 | printk(BIOS_WARNING, "Not enough MTRRs: %d vs %d\n", |
| 771 | sol->num_used, total_mtrrs); |
| 772 | return -1; |
| 773 | } |
| 774 | |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame] | 775 | /* Write out the variable MTRRs. */ |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 776 | disable_cache(); |
| 777 | for (i = 0; i < sol->num_used; i++) { |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 778 | wrmsr(MTRR_PHYS_BASE(i), sol->regs[i].base); |
| 779 | wrmsr(MTRR_PHYS_MASK(i), sol->regs[i].mask); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 780 | } |
| 781 | /* Clear the ones that are unused. */ |
| 782 | for (; i < total_mtrrs; i++) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 783 | clear_var_mtrr(i); |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame] | 784 | enable_var_mtrr(sol->mtrr_default_type); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 785 | enable_cache(); |
| 786 | |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame] | 787 | return 0; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb) |
| 791 | { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 792 | static struct var_mtrr_solution *sol = NULL; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 793 | struct memranges *addr_space; |
| 794 | |
| 795 | addr_space = get_physical_address_space(); |
| 796 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 797 | if (sol == NULL) { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 798 | sol = &mtrr_global_solution; |
| 799 | sol->mtrr_default_type = |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 800 | calc_var_mtrrs(addr_space, !!above4gb, address_bits); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 801 | prepare_var_mtrrs(addr_space, sol->mtrr_default_type, |
| 802 | !!above4gb, address_bits, sol); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 803 | } |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 804 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 805 | commit_var_mtrrs(sol); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Aaron Durbin | 1ebbb16 | 2020-05-28 10:17:34 -0600 | [diff] [blame] | 808 | static void _x86_setup_mtrrs(unsigned int above4gb) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 809 | { |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 810 | int address_size; |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 811 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 812 | x86_setup_fixed_mtrrs(); |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 813 | address_size = cpu_phys_address_size(); |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 814 | printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", |
| 815 | address_size); |
Aaron Durbin | 1ebbb16 | 2020-05-28 10:17:34 -0600 | [diff] [blame] | 816 | x86_setup_var_mtrrs(address_size, above4gb); |
| 817 | } |
| 818 | |
| 819 | void x86_setup_mtrrs(void) |
| 820 | { |
Tim Wawrzynczak | 6fcc46d | 2021-04-19 13:47:36 -0600 | [diff] [blame] | 821 | /* Without detect, assume the minimum */ |
| 822 | total_mtrrs = MIN_MTRRS; |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 823 | /* Always handle addresses above 4GiB. */ |
Aaron Durbin | 1ebbb16 | 2020-05-28 10:17:34 -0600 | [diff] [blame] | 824 | _x86_setup_mtrrs(1); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 827 | void x86_setup_mtrrs_with_detect(void) |
| 828 | { |
| 829 | detect_var_mtrrs(); |
Aaron Durbin | 1ebbb16 | 2020-05-28 10:17:34 -0600 | [diff] [blame] | 830 | /* Always handle addresses above 4GiB. */ |
| 831 | _x86_setup_mtrrs(1); |
| 832 | } |
| 833 | |
| 834 | void x86_setup_mtrrs_with_detect_no_above_4gb(void) |
| 835 | { |
| 836 | detect_var_mtrrs(); |
| 837 | _x86_setup_mtrrs(0); |
Aaron Durbin | e63be89 | 2016-03-07 16:05:36 -0600 | [diff] [blame] | 838 | } |
| 839 | |
Kyösti Mälkki | 38a8fb0 | 2014-06-30 13:48:18 +0300 | [diff] [blame] | 840 | void x86_mtrr_check(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 841 | { |
| 842 | /* Only Pentium Pro and later have MTRR */ |
| 843 | msr_t msr; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 844 | printk(BIOS_DEBUG, "\nMTRR check\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 845 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 846 | msr = rdmsr(MTRR_DEF_TYPE_MSR); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 847 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 848 | printk(BIOS_DEBUG, "Fixed MTRRs : "); |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 849 | if (msr.lo & MTRR_DEF_TYPE_FIX_EN) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 850 | printk(BIOS_DEBUG, "Enabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 851 | else |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 852 | printk(BIOS_DEBUG, "Disabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 853 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 854 | printk(BIOS_DEBUG, "Variable MTRRs: "); |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 855 | if (msr.lo & MTRR_DEF_TYPE_EN) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 856 | printk(BIOS_DEBUG, "Enabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 857 | else |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 858 | printk(BIOS_DEBUG, "Disabled\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 859 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 860 | printk(BIOS_DEBUG, "\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 861 | |
| 862 | post_code(0x93); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 863 | } |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 864 | |
| 865 | static bool put_back_original_solution; |
| 866 | |
| 867 | void mtrr_use_temp_range(uintptr_t begin, size_t size, int type) |
| 868 | { |
| 869 | const struct range_entry *r; |
| 870 | const struct memranges *orig; |
| 871 | struct var_mtrr_solution sol; |
| 872 | struct memranges addr_space; |
| 873 | const int above4gb = 1; /* Cover above 4GiB by default. */ |
| 874 | int address_bits; |
| 875 | |
| 876 | /* Make a copy of the original address space and tweak it with the |
| 877 | * provided range. */ |
| 878 | memranges_init_empty(&addr_space, NULL, 0); |
| 879 | orig = get_physical_address_space(); |
| 880 | memranges_each_entry(r, orig) { |
| 881 | unsigned long tag = range_entry_tag(r); |
| 882 | |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 883 | /* Remove any write combining MTRRs from the temporary |
| 884 | * solution as it just fragments the address space. */ |
| 885 | if (tag == MTRR_TYPE_WRCOMB) |
| 886 | tag = MTRR_TYPE_UNCACHEABLE; |
| 887 | |
| 888 | memranges_insert(&addr_space, range_entry_base(r), |
| 889 | range_entry_size(r), tag); |
| 890 | } |
| 891 | |
| 892 | /* Place new range into the address space. */ |
| 893 | memranges_insert(&addr_space, begin, size, type); |
| 894 | |
| 895 | print_physical_address_space(&addr_space, "TEMPORARY"); |
| 896 | |
| 897 | /* Calculate a new solution with the updated address space. */ |
| 898 | address_bits = cpu_phys_address_size(); |
| 899 | memset(&sol, 0, sizeof(sol)); |
| 900 | sol.mtrr_default_type = |
| 901 | calc_var_mtrrs(&addr_space, above4gb, address_bits); |
| 902 | prepare_var_mtrrs(&addr_space, sol.mtrr_default_type, |
| 903 | above4gb, address_bits, &sol); |
Aaron Durbin | d9762f7 | 2017-06-12 12:48:38 -0500 | [diff] [blame] | 904 | |
| 905 | if (commit_var_mtrrs(&sol) < 0) |
| 906 | printk(BIOS_WARNING, "Unable to insert temporary MTRR range: 0x%016llx - 0x%016llx size 0x%08llx type %d\n", |
| 907 | (long long)begin, (long long)begin + size, |
| 908 | (long long)size, type); |
| 909 | else |
| 910 | put_back_original_solution = true; |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 911 | |
| 912 | memranges_teardown(&addr_space); |
Aaron Durbin | 2bebd7b | 2016-11-10 15:15:35 -0600 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static void remove_temp_solution(void *unused) |
| 916 | { |
| 917 | if (put_back_original_solution) |
| 918 | commit_var_mtrrs(&mtrr_global_solution); |
| 919 | } |
| 920 | |
| 921 | BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, remove_temp_solution, NULL); |
| 922 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, remove_temp_solution, NULL); |