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