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 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * |
| 24 | * Reference: Intel Architecture Software Developer's Manual, Volume 3: System Programming |
| 25 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 26 | |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 27 | #include <stddef.h> |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Aaron Durbin | bebf669 | 2013-04-24 20:59:43 -0500 | [diff] [blame] | 30 | #include <bootstate.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 31 | #include <console/console.h> |
| 32 | #include <device/device.h> |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 33 | #include <device/pci_ids.h> |
Aaron Durbin | ebf142a | 2013-03-29 16:23:23 -0500 | [diff] [blame] | 34 | #include <cpu/cpu.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 35 | #include <cpu/x86/msr.h> |
| 36 | #include <cpu/x86/mtrr.h> |
| 37 | #include <cpu/x86/cache.h> |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 38 | #include <cpu/x86/lapic.h> |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 39 | #include <arch/cpu.h> |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 40 | #include <arch/acpi.h> |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 41 | #include <memrange.h> |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 42 | #if CONFIG_X86_AMD_FIXED_MTRRS |
| 43 | #include <cpu/amd/mtrr.h> |
| 44 | #define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM) |
| 45 | #else |
| 46 | #define MTRR_FIXED_WRBACK_BITS 0 |
| 47 | #endif |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 48 | |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 49 | /* 2 MTRRS are reserved for the operating system */ |
| 50 | #define BIOS_MTRRS 6 |
| 51 | #define OS_MTRRS 2 |
| 52 | #define MTRRS (BIOS_MTRRS + OS_MTRRS) |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 53 | /* |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame^] | 54 | * Static storage size for variable MTRRs. It's sized sufficiently large to |
| 55 | * handle different types of CPUs. Empirically, 16 variable MTRRs has not |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 56 | * yet been observed. |
| 57 | */ |
| 58 | #define NUM_MTRR_STATIC_STORAGE 16 |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 59 | |
| 60 | static int total_mtrrs = MTRRS; |
| 61 | static int bios_mtrrs = BIOS_MTRRS; |
| 62 | |
| 63 | static void detect_var_mtrrs(void) |
| 64 | { |
| 65 | msr_t msr; |
| 66 | |
| 67 | msr = rdmsr(MTRRcap_MSR); |
| 68 | |
| 69 | total_mtrrs = msr.lo & 0xff; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 70 | |
| 71 | if (total_mtrrs > NUM_MTRR_STATIC_STORAGE) { |
| 72 | printk(BIOS_WARNING, |
| 73 | "MTRRs detected (%d) > NUM_MTRR_STATIC_STORAGE (%d)\n", |
| 74 | total_mtrrs, NUM_MTRR_STATIC_STORAGE); |
| 75 | total_mtrrs = NUM_MTRR_STATIC_STORAGE; |
| 76 | } |
Stefan Reinauer | c00dfbc | 2012-04-03 16:24:37 -0700 | [diff] [blame] | 77 | bios_mtrrs = total_mtrrs - OS_MTRRS; |
| 78 | } |
| 79 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 80 | void enable_fixed_mtrr(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 81 | { |
| 82 | msr_t msr; |
| 83 | |
| 84 | msr = rdmsr(MTRRdefType_MSR); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 85 | msr.lo |= MTRRdefTypeEn | MTRRdefTypeFixEn; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 86 | wrmsr(MTRRdefType_MSR, msr); |
| 87 | } |
| 88 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 89 | static void enable_var_mtrr(unsigned char deftype) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 90 | { |
| 91 | msr_t msr; |
| 92 | |
| 93 | msr = rdmsr(MTRRdefType_MSR); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 94 | msr.lo &= ~0xff; |
| 95 | msr.lo |= MTRRdefTypeEn | deftype; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 96 | wrmsr(MTRRdefType_MSR, msr); |
| 97 | } |
| 98 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 99 | /* fms: find most sigificant bit set, stolen from Linux Kernel Source. */ |
| 100 | static inline unsigned int fms(unsigned int x) |
| 101 | { |
| 102 | int r; |
| 103 | |
| 104 | __asm__("bsrl %1,%0\n\t" |
| 105 | "jnz 1f\n\t" |
| 106 | "movl $0,%0\n" |
| 107 | "1:" : "=r" (r) : "g" (x)); |
| 108 | return r; |
| 109 | } |
| 110 | |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 111 | /* fls: find least significant bit set */ |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 112 | static inline unsigned int fls(unsigned int x) |
| 113 | { |
| 114 | int r; |
| 115 | |
| 116 | __asm__("bsfl %1,%0\n\t" |
| 117 | "jnz 1f\n\t" |
| 118 | "movl $32,%0\n" |
| 119 | "1:" : "=r" (r) : "g" (x)); |
| 120 | return r; |
| 121 | } |
| 122 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 123 | #define MTRR_VERBOSE_LEVEL BIOS_NEVER |
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 | /* MTRRs are at a 4KiB granularity. Therefore all address calculations can |
| 126 | * be done with 32-bit numbers. This allows for the MTRR code to handle |
| 127 | * up to 2^44 bytes (16 TiB) of address space. */ |
| 128 | #define RANGE_SHIFT 12 |
| 129 | #define ADDR_SHIFT_TO_RANGE_SHIFT(x) \ |
| 130 | (((x) > RANGE_SHIFT) ? ((x) - RANGE_SHIFT) : RANGE_SHIFT) |
| 131 | #define PHYS_TO_RANGE_ADDR(x) ((x) >> RANGE_SHIFT) |
| 132 | #define RANGE_TO_PHYS_ADDR(x) (((resource_t)(x)) << RANGE_SHIFT) |
| 133 | #define NUM_FIXED_MTRRS (NUM_FIXED_RANGES / RANGES_PER_FIXED_MTRR) |
| 134 | |
| 135 | /* The minimum alignment while handling variable MTRR ranges is 64MiB. */ |
| 136 | #define MTRR_MIN_ALIGN PHYS_TO_RANGE_ADDR(64 << 20) |
| 137 | /* Helpful constants. */ |
| 138 | #define RANGE_1MB PHYS_TO_RANGE_ADDR(1 << 20) |
| 139 | #define RANGE_4GB (1 << (ADDR_SHIFT_TO_RANGE_SHIFT(32))) |
| 140 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 141 | /* |
| 142 | * The default MTRR type selection uses 3 approaches for selecting the |
| 143 | * optimal number of variable MTRRs. For each range do 3 calculations: |
| 144 | * 1. UC as default type with no holes at top of range. |
| 145 | * 2. UC as default using holes at top of range. |
| 146 | * 3. WB as default. |
| 147 | * If using holes is optimal for a range when UC is the default type the |
| 148 | * tag is updated to direct the commit routine to use a hole at the top |
| 149 | * of a range. |
| 150 | */ |
| 151 | #define MTRR_ALGO_SHIFT (8) |
| 152 | #define MTRR_TAG_MASK ((1 << MTRR_ALGO_SHIFT) - 1) |
| 153 | /* If the default type is UC use the hole carving algorithm for a range. */ |
| 154 | #define MTRR_RANGE_UC_USE_HOLE (1 << MTRR_ALGO_SHIFT) |
| 155 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 156 | 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] | 157 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 158 | return PHYS_TO_RANGE_ADDR(range_entry_base(r)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 161 | 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] | 162 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 163 | return PHYS_TO_RANGE_ADDR(range_entry_end(r)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 166 | static inline int range_entry_mtrr_type(struct range_entry *r) |
| 167 | { |
| 168 | return range_entry_tag(r) & MTRR_TAG_MASK; |
| 169 | } |
| 170 | |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 171 | static int filter_vga_wrcomb(struct device *dev, struct resource *res) |
| 172 | { |
| 173 | /* Only handle PCI devices. */ |
| 174 | if (dev->path.type != DEVICE_PATH_PCI) |
| 175 | return 0; |
| 176 | |
| 177 | /* Only handle VGA class devices. */ |
| 178 | if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)) |
| 179 | return 0; |
| 180 | |
| 181 | /* Add resource as write-combining in the address space. */ |
| 182 | return 1; |
| 183 | } |
| 184 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 185 | static struct memranges *get_physical_address_space(void) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 186 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 187 | static struct memranges *addr_space; |
| 188 | static struct memranges addr_space_storage; |
Duncan Laurie | 7389fa9 | 2011-12-22 10:59:40 -0800 | [diff] [blame] | 189 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 190 | /* In order to handle some chipsets not being able to pre-determine |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 191 | * uncacheable ranges, such as graphics memory, at resource insertion |
| 192 | * time remove uncacheable regions from the cacheable ones. */ |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 193 | if (addr_space == NULL) { |
| 194 | struct range_entry *r; |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 195 | unsigned long mask; |
| 196 | unsigned long match; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 197 | |
| 198 | addr_space = &addr_space_storage; |
| 199 | |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 200 | mask = IORESOURCE_CACHEABLE; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 201 | /* Collect cacheable and uncacheable address ranges. The |
| 202 | * uncacheable regions take precedence over the cacheable |
| 203 | * regions. */ |
| 204 | memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK); |
| 205 | memranges_add_resources(addr_space, mask, 0, |
| 206 | MTRR_TYPE_UNCACHEABLE); |
| 207 | |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 208 | /* Handle any write combining resources. Only prefetchable |
Vladimir Serbinenko | 30fe612 | 2014-02-05 23:25:28 +0100 | [diff] [blame] | 209 | * resources are appropriate for this MTRR type. */ |
| 210 | match = IORESOURCE_PREFETCH; |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 211 | mask |= match; |
Aaron Durbin | ca4f4b8 | 2014-02-08 15:41:52 -0600 | [diff] [blame] | 212 | memranges_add_resources_filter(addr_space, mask, match, MTRR_TYPE_WRCOMB, |
| 213 | filter_vga_wrcomb); |
Aaron Durbin | 9b027fe | 2013-03-26 14:10:34 -0500 | [diff] [blame] | 214 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 215 | /* The address space below 4GiB is special. It needs to be |
| 216 | * covered entirly by range entries so that MTRR calculations |
| 217 | * can be properly done for the full 32-bit address space. |
| 218 | * Therefore, ensure holes are filled up to 4GiB as |
| 219 | * uncacheable */ |
| 220 | memranges_fill_holes_up_to(addr_space, |
| 221 | RANGE_TO_PHYS_ADDR(RANGE_4GB), |
| 222 | MTRR_TYPE_UNCACHEABLE); |
| 223 | |
| 224 | printk(BIOS_DEBUG, "MTRR: Physical address space:\n"); |
| 225 | memranges_each_entry(r, addr_space) |
| 226 | printk(BIOS_DEBUG, |
| 227 | "0x%016llx - 0x%016llx size 0x%08llx type %ld\n", |
| 228 | range_entry_base(r), range_entry_end(r), |
| 229 | range_entry_size(r), range_entry_tag(r)); |
Carl-Daniel Hailfinger | 7dde1da | 2009-02-11 16:57:32 +0000 | [diff] [blame] | 230 | } |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 231 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 232 | return addr_space; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 235 | /* Fixed MTRR descriptor. This structure defines the step size and begin |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 236 | * and end (exclusive) address covered by a set of fixed MTRR MSRs. |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 237 | * It also describes the offset in byte intervals to store the calculated MTRR |
| 238 | * type in an array. */ |
| 239 | struct fixed_mtrr_desc { |
| 240 | uint32_t begin; |
| 241 | uint32_t end; |
| 242 | uint32_t step; |
| 243 | int range_index; |
| 244 | int msr_index_base; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 247 | /* Shared MTRR calculations. Can be reused by APs. */ |
| 248 | static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES]; |
| 249 | |
| 250 | /* Fixed MTRR descriptors. */ |
| 251 | static const struct fixed_mtrr_desc fixed_mtrr_desc[] = { |
| 252 | { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000), |
| 253 | PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRRfix64K_00000_MSR }, |
| 254 | { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000), |
| 255 | PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRRfix16K_80000_MSR }, |
| 256 | { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000), |
| 257 | PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRRfix4K_C0000_MSR }, |
| 258 | }; |
| 259 | |
| 260 | static void calc_fixed_mtrrs(void) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 261 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 262 | static int fixed_mtrr_types_initialized; |
| 263 | struct memranges *phys_addr_space; |
| 264 | struct range_entry *r; |
| 265 | const struct fixed_mtrr_desc *desc; |
| 266 | const struct fixed_mtrr_desc *last_desc; |
| 267 | uint32_t begin; |
| 268 | uint32_t end; |
| 269 | int type_index; |
| 270 | |
| 271 | if (fixed_mtrr_types_initialized) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 272 | return; |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 273 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 274 | phys_addr_space = get_physical_address_space(); |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 275 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 276 | /* Set all fixed ranges to uncacheable first. */ |
| 277 | 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] | 278 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 279 | desc = &fixed_mtrr_desc[0]; |
| 280 | 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] | 281 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 282 | memranges_each_entry(r, phys_addr_space) { |
| 283 | begin = range_entry_base_mtrr_addr(r); |
| 284 | end = range_entry_end_mtrr_addr(r); |
Kyösti Mälkki | 2d42b34 | 2012-07-12 00:18:22 +0300 | [diff] [blame] | 285 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 286 | if (begin >= last_desc->end) |
| 287 | break; |
| 288 | |
| 289 | if (end > last_desc->end) |
| 290 | end = last_desc->end; |
| 291 | |
| 292 | /* Get to the correct fixed mtrr descriptor. */ |
| 293 | while (begin >= desc->end) |
| 294 | desc++; |
| 295 | |
| 296 | type_index = desc->range_index; |
| 297 | type_index += (begin - desc->begin) / desc->step; |
| 298 | |
| 299 | while (begin != end) { |
| 300 | unsigned char type; |
| 301 | |
| 302 | type = range_entry_tag(r); |
| 303 | printk(MTRR_VERBOSE_LEVEL, |
| 304 | "MTRR addr 0x%x-0x%x set to %d type @ %d\n", |
| 305 | begin, begin + desc->step, type, type_index); |
| 306 | if (type == MTRR_TYPE_WRBACK) |
| 307 | type |= MTRR_FIXED_WRBACK_BITS; |
| 308 | fixed_mtrr_types[type_index] = type; |
| 309 | type_index++; |
| 310 | begin += desc->step; |
| 311 | if (begin == desc->end) |
| 312 | desc++; |
Yinghai Lu | 6360187 | 2005-01-27 22:48:12 +0000 | [diff] [blame] | 313 | } |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 314 | } |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 315 | fixed_mtrr_types_initialized = 1; |
| 316 | } |
| 317 | |
| 318 | static void commit_fixed_mtrrs(void) |
| 319 | { |
| 320 | int i; |
| 321 | int j; |
| 322 | int msr_num; |
| 323 | int type_index; |
| 324 | /* 8 ranges per msr. */ |
| 325 | msr_t fixed_msrs[NUM_FIXED_MTRRS]; |
| 326 | unsigned long msr_index[NUM_FIXED_MTRRS]; |
| 327 | |
| 328 | memset(&fixed_msrs, 0, sizeof(fixed_msrs)); |
| 329 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 330 | msr_num = 0; |
| 331 | type_index = 0; |
| 332 | for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) { |
| 333 | const struct fixed_mtrr_desc *desc; |
| 334 | int num_ranges; |
| 335 | |
| 336 | desc = &fixed_mtrr_desc[i]; |
| 337 | num_ranges = (desc->end - desc->begin) / desc->step; |
| 338 | for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) { |
| 339 | msr_index[msr_num] = desc->msr_index_base + |
| 340 | (j / RANGES_PER_FIXED_MTRR); |
| 341 | fixed_msrs[msr_num].lo |= |
| 342 | fixed_mtrr_types[type_index++] << 0; |
| 343 | fixed_msrs[msr_num].lo |= |
| 344 | fixed_mtrr_types[type_index++] << 8; |
| 345 | fixed_msrs[msr_num].lo |= |
| 346 | fixed_mtrr_types[type_index++] << 16; |
| 347 | fixed_msrs[msr_num].lo |= |
| 348 | fixed_mtrr_types[type_index++] << 24; |
| 349 | fixed_msrs[msr_num].hi |= |
| 350 | fixed_mtrr_types[type_index++] << 0; |
| 351 | fixed_msrs[msr_num].hi |= |
| 352 | fixed_mtrr_types[type_index++] << 8; |
| 353 | fixed_msrs[msr_num].hi |= |
| 354 | fixed_mtrr_types[type_index++] << 16; |
| 355 | fixed_msrs[msr_num].hi |= |
| 356 | fixed_mtrr_types[type_index++] << 24; |
| 357 | msr_num++; |
| 358 | } |
| 359 | } |
| 360 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 361 | for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 362 | printk(BIOS_DEBUG, "MTRR: Fixed MSR 0x%lx 0x%08x%08x\n", |
| 363 | msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 364 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 365 | disable_cache(); |
| 366 | for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++) |
| 367 | wrmsr(msr_index[i], fixed_msrs[i]); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 368 | enable_cache(); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 371 | void x86_setup_fixed_mtrrs_no_enable(void) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 372 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 373 | calc_fixed_mtrrs(); |
| 374 | commit_fixed_mtrrs(); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 375 | } |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 376 | |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 377 | void x86_setup_fixed_mtrrs(void) |
| 378 | { |
| 379 | x86_setup_fixed_mtrrs_no_enable(); |
| 380 | |
| 381 | printk(BIOS_SPEW, "call enable_fixed_mtrr()\n"); |
| 382 | enable_fixed_mtrr(); |
| 383 | } |
| 384 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 385 | struct var_mtrr_regs { |
| 386 | msr_t base; |
| 387 | msr_t mask; |
| 388 | }; |
| 389 | |
| 390 | struct var_mtrr_solution { |
| 391 | int mtrr_default_type; |
| 392 | int num_used; |
| 393 | struct var_mtrr_regs regs[NUM_MTRR_STATIC_STORAGE]; |
| 394 | }; |
| 395 | |
| 396 | /* Global storage for variable MTRR solution. */ |
| 397 | static struct var_mtrr_solution mtrr_global_solution; |
| 398 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 399 | struct var_mtrr_state { |
| 400 | struct memranges *addr_space; |
| 401 | int above4gb; |
| 402 | int address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 403 | int prepare_msrs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 404 | int mtrr_index; |
| 405 | int def_mtrr_type; |
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 | }; |
Aaron Durbin | 57686f8 | 2013-03-20 15:50:59 -0500 | [diff] [blame] | 408 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 409 | static void clear_var_mtrr(int index) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 410 | { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 411 | msr_t msr_val; |
| 412 | |
| 413 | msr_val = rdmsr(MTRRphysMask_MSR(index)); |
| 414 | msr_val.lo &= ~MTRRphysMaskValid; |
| 415 | wrmsr(MTRRphysMask_MSR(index), msr_val); |
| 416 | } |
| 417 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 418 | static void prep_var_mtrr(struct var_mtrr_state *var_state, |
| 419 | uint32_t base, uint32_t size, int mtrr_type) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 420 | { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 421 | struct var_mtrr_regs *regs; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 422 | resource_t rbase; |
| 423 | resource_t rsize; |
| 424 | resource_t mask; |
| 425 | |
| 426 | /* Some variable MTRRs are attempted to be saved for the OS use. |
| 427 | * However, it's more important to try to map the full address space |
| 428 | * properly. */ |
| 429 | if (var_state->mtrr_index >= bios_mtrrs) |
| 430 | printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n"); |
| 431 | if (var_state->mtrr_index >= total_mtrrs) { |
Paul Menzel | 4fe9813 | 2014-01-25 15:55:28 +0100 | [diff] [blame] | 432 | printk(BIOS_ERR, "ERROR: Not enough MTRRs available!\n"); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
| 436 | rbase = base; |
| 437 | rsize = size; |
| 438 | |
| 439 | rbase = RANGE_TO_PHYS_ADDR(rbase); |
| 440 | rsize = RANGE_TO_PHYS_ADDR(rsize); |
| 441 | rsize = -rsize; |
| 442 | |
| 443 | mask = (1ULL << var_state->address_bits) - 1; |
| 444 | rsize = rsize & mask; |
| 445 | |
| 446 | printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n", |
| 447 | var_state->mtrr_index, rbase, rsize, mtrr_type); |
| 448 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 449 | regs = &var_state->regs[var_state->mtrr_index]; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 450 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 451 | regs->base.lo = rbase; |
| 452 | regs->base.lo |= mtrr_type; |
| 453 | regs->base.hi = rbase >> 32; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 454 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 455 | regs->mask.lo = rsize; |
| 456 | regs->mask.lo |= MTRRphysMaskValid; |
| 457 | regs->mask.hi = rsize >> 32; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void calc_var_mtrr_range(struct var_mtrr_state *var_state, |
| 461 | uint32_t base, uint32_t size, int mtrr_type) |
| 462 | { |
| 463 | while (size != 0) { |
| 464 | uint32_t addr_lsb; |
| 465 | uint32_t size_msb; |
| 466 | uint32_t mtrr_size; |
| 467 | |
| 468 | addr_lsb = fls(base); |
| 469 | size_msb = fms(size); |
| 470 | |
| 471 | /* All MTRR entries need to have their base aligned to the mask |
| 472 | * size. The maximum size is calculated by a function of the |
| 473 | * min base bit set and maximum size bit set. */ |
| 474 | if (addr_lsb > size_msb) |
| 475 | mtrr_size = 1 << size_msb; |
| 476 | else |
| 477 | mtrr_size = 1 << addr_lsb; |
| 478 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 479 | if (var_state->prepare_msrs) |
| 480 | prep_var_mtrr(var_state, base, mtrr_size, mtrr_type); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 481 | |
| 482 | size -= mtrr_size; |
| 483 | base += mtrr_size; |
| 484 | var_state->mtrr_index++; |
| 485 | } |
| 486 | } |
| 487 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 488 | static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state, |
| 489 | struct range_entry *r) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 490 | { |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 491 | uint32_t a1, a2, b1, b2; |
| 492 | int mtrr_type; |
| 493 | struct range_entry *next; |
| 494 | |
| 495 | /* |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 496 | * Determine MTRRs based on the following algorithm for the given entry: |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 497 | * +------------------+ b2 = ALIGN_UP(end) |
| 498 | * | 0 or more bytes | <-- hole is carved out between b1 and b2 |
| 499 | * +------------------+ a2 = b1 = end |
| 500 | * | | |
| 501 | * +------------------+ a1 = begin |
| 502 | * |
| 503 | * Thus, there are 3 sub-ranges to configure variable MTRRs for. |
| 504 | */ |
| 505 | mtrr_type = range_entry_mtrr_type(r); |
| 506 | |
| 507 | a1 = range_entry_base_mtrr_addr(r); |
| 508 | a2 = range_entry_end_mtrr_addr(r); |
| 509 | |
| 510 | /* The end address is under 1MiB. The fixed MTRRs take |
| 511 | * precedence over the variable ones. Therefore this range |
| 512 | * can be ignored. */ |
| 513 | if (a2 < RANGE_1MB) |
| 514 | return; |
| 515 | |
| 516 | /* Again, the fixed MTRRs take precedence so the beginning |
| 517 | * of the range can be set to 0 if it starts below 1MiB. */ |
| 518 | if (a1 < RANGE_1MB) |
| 519 | a1 = 0; |
| 520 | |
| 521 | /* If the range starts above 4GiB the processing is done. */ |
| 522 | if (!var_state->above4gb && a1 >= RANGE_4GB) |
| 523 | return; |
| 524 | |
| 525 | /* Clip the upper address to 4GiB if addresses above 4GiB |
| 526 | * are not being processed. */ |
| 527 | if (!var_state->above4gb && a2 > RANGE_4GB) |
| 528 | a2 = RANGE_4GB; |
| 529 | |
Aaron Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 530 | next = memranges_next_entry(var_state->addr_space, r); |
| 531 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 532 | b1 = a2; |
Aaron Durbin | 5392424 | 2013-03-29 11:48:27 -0500 | [diff] [blame] | 533 | |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 534 | /* 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] | 535 | * entry. If so perform an optimization of covering a larger range |
| 536 | * defined by the base address' alignment. */ |
| 537 | if (a1 >= RANGE_4GB && next == NULL) { |
| 538 | uint32_t addr_lsb; |
| 539 | |
| 540 | addr_lsb = fls(a1); |
| 541 | b2 = (1 << addr_lsb) + a1; |
| 542 | if (b2 >= a2) { |
| 543 | calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type); |
| 544 | return; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* Handle the min alignment roundup case. */ |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 549 | b2 = ALIGN_UP(a2, MTRR_MIN_ALIGN); |
| 550 | |
| 551 | /* Check against the next range. If the current range_entry is the |
| 552 | * last entry then carving a hole is no problem. If the current entry |
| 553 | * isn't the last entry then check that the last entry covers the |
| 554 | * entire hole range with the default mtrr type. */ |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 555 | if (next != NULL && |
| 556 | (range_entry_mtrr_type(next) != var_state->def_mtrr_type || |
| 557 | range_entry_end_mtrr_addr(next) < b2)) { |
| 558 | calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type); |
| 563 | calc_var_mtrr_range(var_state, b1, b2 - b1, var_state->def_mtrr_type); |
| 564 | } |
| 565 | |
| 566 | static void calc_var_mtrrs_without_hole(struct var_mtrr_state *var_state, |
| 567 | struct range_entry *r) |
| 568 | { |
| 569 | uint32_t a1, a2, b1, b2, c1, c2; |
| 570 | int mtrr_type; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * For each range that meets the non-default type process it in the |
| 574 | * following manner: |
| 575 | * +------------------+ c2 = end |
| 576 | * | 0 or more bytes | |
| 577 | * +------------------+ b2 = c1 = ALIGN_DOWN(end) |
| 578 | * | | |
| 579 | * +------------------+ b1 = a2 = ALIGN_UP(begin) |
| 580 | * | 0 or more bytes | |
| 581 | * +------------------+ a1 = begin |
| 582 | * |
| 583 | * Thus, there are 3 sub-ranges to configure variable MTRRs for. |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 584 | */ |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 585 | mtrr_type = range_entry_mtrr_type(r); |
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 | a1 = range_entry_base_mtrr_addr(r); |
| 588 | c2 = range_entry_end_mtrr_addr(r); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 589 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 590 | /* The end address is under 1MiB. The fixed MTRRs take |
| 591 | * precedence over the variable ones. Therefore this range |
| 592 | * can be ignored. */ |
| 593 | if (c2 < RANGE_1MB) |
| 594 | return; |
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 | /* Again, the fixed MTRRs take precedence so the beginning |
| 597 | * of the range can be set to 0 if it starts below 1MiB. */ |
| 598 | if (a1 < RANGE_1MB) |
| 599 | a1 = 0; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 600 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 601 | /* If the range starts above 4GiB the processing is done. */ |
| 602 | if (!var_state->above4gb && a1 >= RANGE_4GB) |
| 603 | return; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 604 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 605 | /* Clip the upper address to 4GiB if addresses above 4GiB |
| 606 | * are not being processed. */ |
| 607 | if (!var_state->above4gb && c2 > RANGE_4GB) |
| 608 | c2 = RANGE_4GB; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 609 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 610 | /* Don't align up or down on the range if it is smaller |
| 611 | * than the minimum granularity. */ |
| 612 | if ((c2 - a1) < MTRR_MIN_ALIGN) { |
| 613 | calc_var_mtrr_range(var_state, a1, c2 - a1, mtrr_type); |
| 614 | return; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 615 | } |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 616 | |
| 617 | b1 = a2 = ALIGN_UP(a1, MTRR_MIN_ALIGN); |
| 618 | b2 = c1 = ALIGN_DOWN(c2, MTRR_MIN_ALIGN); |
| 619 | |
| 620 | calc_var_mtrr_range(var_state, a1, a2 - a1, mtrr_type); |
| 621 | calc_var_mtrr_range(var_state, b1, b2 - b1, mtrr_type); |
| 622 | calc_var_mtrr_range(var_state, c1, c2 - c1, mtrr_type); |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 625 | static void __calc_var_mtrrs(struct memranges *addr_space, |
| 626 | int above4gb, int address_bits, |
| 627 | int *num_def_wb_mtrrs, int *num_def_uc_mtrrs) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 628 | { |
| 629 | int wb_deftype_count; |
| 630 | int uc_deftype_count; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 631 | struct range_entry *r; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 632 | struct var_mtrr_state var_state; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 633 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 634 | /* The default MTRR cacheability type is determined by calculating |
Paul Menzel | 4fe9813 | 2014-01-25 15:55:28 +0100 | [diff] [blame] | 635 | * 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] | 636 | * default. */ |
| 637 | var_state.addr_space = addr_space; |
Scott Duplichan | f3cce2f | 2010-11-13 19:07:59 +0000 | [diff] [blame] | 638 | var_state.above4gb = above4gb; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 639 | var_state.address_bits = address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 640 | var_state.prepare_msrs = 0; |
Stefan Reinauer | 7f86ed1 | 2009-02-12 16:02:16 +0000 | [diff] [blame] | 641 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 642 | wb_deftype_count = 0; |
| 643 | uc_deftype_count = 0; |
Duncan Laurie | 7389fa9 | 2011-12-22 10:59:40 -0800 | [diff] [blame] | 644 | |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 645 | /* |
| 646 | * For each range do 3 calculations: |
| 647 | * 1. UC as default type with no holes at top of range. |
| 648 | * 2. UC as default using holes at top of range. |
| 649 | * 3. WB as default. |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 650 | * The lowest count is then used as default after totaling all |
| 651 | * MTRRs. Note that the optimal algorithm for UC default is marked in |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 652 | * the tag of each range regardless of final decision. UC takes |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 653 | * precedence in the MTRR architecture. Therefore, only holes can be |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 654 | * used when the type of the region is MTRR_TYPE_WRBACK with |
| 655 | * MTRR_TYPE_UNCACHEABLE as the default type. |
| 656 | */ |
| 657 | memranges_each_entry(r, var_state.addr_space) { |
| 658 | int mtrr_type; |
| 659 | |
| 660 | mtrr_type = range_entry_mtrr_type(r); |
| 661 | |
| 662 | if (mtrr_type != MTRR_TYPE_UNCACHEABLE) { |
| 663 | int uc_hole_count; |
| 664 | int uc_no_hole_count; |
| 665 | |
| 666 | var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE; |
| 667 | var_state.mtrr_index = 0; |
| 668 | |
| 669 | /* No hole calculation. */ |
| 670 | calc_var_mtrrs_without_hole(&var_state, r); |
| 671 | uc_no_hole_count = var_state.mtrr_index; |
| 672 | |
| 673 | /* Hole calculation only if type is WB. The 64 number |
| 674 | * is a count that is unachievable, thus making it |
| 675 | * a default large number in the case of not doing |
| 676 | * the hole calculation. */ |
| 677 | uc_hole_count = 64; |
| 678 | if (mtrr_type == MTRR_TYPE_WRBACK) { |
| 679 | var_state.mtrr_index = 0; |
| 680 | calc_var_mtrrs_with_hole(&var_state, r); |
| 681 | uc_hole_count = var_state.mtrr_index; |
| 682 | } |
| 683 | |
| 684 | /* Mark the entry with the optimal algorithm. */ |
| 685 | if (uc_no_hole_count < uc_hole_count) { |
| 686 | uc_deftype_count += uc_no_hole_count; |
| 687 | } else { |
| 688 | unsigned long new_tag; |
| 689 | |
| 690 | new_tag = mtrr_type | MTRR_RANGE_UC_USE_HOLE; |
| 691 | range_entry_update_tag(r, new_tag); |
| 692 | uc_deftype_count += uc_hole_count; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (mtrr_type != MTRR_TYPE_WRBACK) { |
| 697 | var_state.mtrr_index = 0; |
| 698 | var_state.def_mtrr_type = MTRR_TYPE_WRBACK; |
| 699 | calc_var_mtrrs_without_hole(&var_state, r); |
| 700 | wb_deftype_count += var_state.mtrr_index; |
| 701 | } |
| 702 | } |
Aaron Durbin | 5b9e3b6 | 2014-02-05 16:00:43 -0600 | [diff] [blame] | 703 | *num_def_wb_mtrrs = wb_deftype_count; |
| 704 | *num_def_uc_mtrrs = uc_deftype_count; |
| 705 | } |
| 706 | |
| 707 | static int calc_var_mtrrs(struct memranges *addr_space, |
| 708 | int above4gb, int address_bits) |
| 709 | { |
| 710 | int wb_deftype_count = 0; |
| 711 | int uc_deftype_count = 0; |
| 712 | |
| 713 | __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count, |
| 714 | &uc_deftype_count); |
| 715 | |
| 716 | if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) { |
| 717 | printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. " |
| 718 | "WB/UC MTRR counts: %d/%d > %d.\n", |
| 719 | wb_deftype_count, uc_deftype_count, bios_mtrrs); |
| 720 | memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB, |
| 721 | MTRR_TYPE_UNCACHEABLE); |
| 722 | __calc_var_mtrrs(addr_space, above4gb, address_bits, |
| 723 | &wb_deftype_count, &uc_deftype_count); |
| 724 | } |
Scott Duplichan | f3cce2f | 2010-11-13 19:07:59 +0000 | [diff] [blame] | 725 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 726 | printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n", |
| 727 | wb_deftype_count, uc_deftype_count); |
Kyösti Mälkki | ffc1fb3 | 2012-07-11 14:40:19 +0300 | [diff] [blame] | 728 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 729 | if (wb_deftype_count < uc_deftype_count) { |
| 730 | printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n"); |
| 731 | return MTRR_TYPE_WRBACK; |
| 732 | } |
| 733 | printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n"); |
| 734 | return MTRR_TYPE_UNCACHEABLE; |
| 735 | } |
Kyösti Mälkki | ffc1fb3 | 2012-07-11 14:40:19 +0300 | [diff] [blame] | 736 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 737 | static void prepare_var_mtrrs(struct memranges *addr_space, int def_type, |
| 738 | int above4gb, int address_bits, |
| 739 | struct var_mtrr_solution *sol) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 740 | { |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 741 | struct range_entry *r; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 742 | struct var_mtrr_state var_state; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 743 | |
| 744 | var_state.addr_space = addr_space; |
| 745 | var_state.above4gb = above4gb; |
| 746 | var_state.address_bits = address_bits; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 747 | /* Prepare the MSRs. */ |
| 748 | var_state.prepare_msrs = 1; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 749 | var_state.mtrr_index = 0; |
| 750 | var_state.def_mtrr_type = def_type; |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 751 | var_state.regs = &sol->regs[0]; |
Aaron Durbin | e383442 | 2013-03-28 20:48:51 -0500 | [diff] [blame] | 752 | |
| 753 | memranges_each_entry(r, var_state.addr_space) { |
| 754 | if (range_entry_mtrr_type(r) == def_type) |
| 755 | continue; |
| 756 | |
| 757 | if (def_type == MTRR_TYPE_UNCACHEABLE && |
| 758 | (range_entry_tag(r) & MTRR_RANGE_UC_USE_HOLE)) |
| 759 | calc_var_mtrrs_with_hole(&var_state, r); |
| 760 | else |
| 761 | calc_var_mtrrs_without_hole(&var_state, r); |
| 762 | } |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 763 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 764 | /* Update the solution. */ |
| 765 | sol->num_used = var_state.mtrr_index; |
| 766 | } |
| 767 | |
| 768 | static void commit_var_mtrrs(const struct var_mtrr_solution *sol) |
| 769 | { |
| 770 | int i; |
| 771 | |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame^] | 772 | /* Write out the variable MTRRs. */ |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 773 | disable_cache(); |
| 774 | for (i = 0; i < sol->num_used; i++) { |
| 775 | wrmsr(MTRRphysBase_MSR(i), sol->regs[i].base); |
| 776 | wrmsr(MTRRphysMask_MSR(i), sol->regs[i].mask); |
| 777 | } |
| 778 | /* Clear the ones that are unused. */ |
| 779 | for (; i < total_mtrrs; i++) |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 780 | clear_var_mtrr(i); |
Isaac Christensen | 81f90c5 | 2014-09-24 14:59:32 -0600 | [diff] [blame^] | 781 | enable_var_mtrr(sol->mtrr_default_type); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 782 | enable_cache(); |
| 783 | |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb) |
| 787 | { |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 788 | static struct var_mtrr_solution *sol = NULL; |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 789 | struct memranges *addr_space; |
| 790 | |
| 791 | addr_space = get_physical_address_space(); |
| 792 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 793 | if (sol == NULL) { |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 794 | if (above4gb == 2) |
| 795 | detect_var_mtrrs(); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 796 | sol = &mtrr_global_solution; |
| 797 | sol->mtrr_default_type = |
Aaron Durbin | bb4e79a | 2013-03-26 14:09:47 -0500 | [diff] [blame] | 798 | calc_var_mtrrs(addr_space, !!above4gb, address_bits); |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 799 | prepare_var_mtrrs(addr_space, sol->mtrr_default_type, |
| 800 | !!above4gb, address_bits, sol); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 801 | } |
Stefan Reinauer | 00093a8 | 2011-11-02 16:12:34 -0700 | [diff] [blame] | 802 | |
Gabe Black | 7756fe7 | 2014-02-25 01:40:34 -0800 | [diff] [blame] | 803 | commit_var_mtrrs(sol); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 806 | void x86_setup_mtrrs(void) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 807 | { |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 808 | int address_size; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 809 | x86_setup_fixed_mtrrs(); |
Sven Schnelle | adfbcb79 | 2012-01-10 12:01:43 +0100 | [diff] [blame] | 810 | address_size = cpu_phys_address_size(); |
| 811 | printk(BIOS_DEBUG, "CPU physical address size: %d bits\n", address_size); |
| 812 | x86_setup_var_mtrrs(address_size, 1); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 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 | |
Kyösti Mälkki | 599cda8 | 2014-06-25 01:22:17 +0300 | [diff] [blame] | 821 | msr = rdmsr(MTRRdefType_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 : "); |
Kyösti Mälkki | 599cda8 | 2014-06-25 01:22:17 +0300 | [diff] [blame] | 824 | if (msr.lo & MTRRdefTypeFixEn) |
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: "); |
Kyösti Mälkki | 599cda8 | 2014-06-25 01:22:17 +0300 | [diff] [blame] | 830 | if (msr.lo & MTRRdefTypeEn) |
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 | } |