1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2a1a499a3SJaswinder Singh Rajput /* 3a1a499a3SJaswinder Singh Rajput * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong 4a1a499a3SJaswinder Singh Rajput * because MTRRs can span up to 40 bits (36bits on most modern x86) 5a1a499a3SJaswinder Singh Rajput */ 6a1a499a3SJaswinder Singh Rajput 7186f4360SPaul Gortmaker #include <linux/export.h> 82ec1df41SThomas Gleixner #include <linux/init.h> 9a1a499a3SJaswinder Singh Rajput #include <linux/io.h> 102ec1df41SThomas Gleixner #include <linux/mm.h> 1129055dc7SJuergen Gross #include <linux/cc_platform.h> 12a1a499a3SJaswinder Singh Rajput #include <asm/processor-flags.h> 13d5f66d5dSJuergen Gross #include <asm/cacheinfo.h> 14a1a499a3SJaswinder Singh Rajput #include <asm/cpufeature.h> 1529055dc7SJuergen Gross #include <asm/hypervisor.h> 1629055dc7SJuergen Gross #include <asm/mshyperv.h> 17a1a499a3SJaswinder Singh Rajput #include <asm/tlbflush.h> 182ec1df41SThomas Gleixner #include <asm/mtrr.h> 192ec1df41SThomas Gleixner #include <asm/msr.h> 20eb243d1dSIngo Molnar #include <asm/memtype.h> 21a1a499a3SJaswinder Singh Rajput 222ec1df41SThomas Gleixner #include "mtrr.h" 232ec1df41SThomas Gleixner 242ec1df41SThomas Gleixner struct fixed_range_block { 252ec1df41SThomas Gleixner int base_msr; /* start address of an MTRR block */ 262ec1df41SThomas Gleixner int ranges; /* number of MTRRs in this block */ 272ec1df41SThomas Gleixner }; 282ec1df41SThomas Gleixner 292ec1df41SThomas Gleixner static struct fixed_range_block fixed_range_blocks[] = { 30a036c7a3SJaswinder Singh Rajput { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */ 317d9d55e4SJaswinder Singh Rajput { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */ 32ba5673ffSJaswinder Singh Rajput { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */ 332ec1df41SThomas Gleixner {} 342ec1df41SThomas Gleixner }; 352ec1df41SThomas Gleixner 36061b984aSJuergen Gross struct cache_map { 37061b984aSJuergen Gross u64 start; 38061b984aSJuergen Gross u64 end; 39061b984aSJuergen Gross u64 flags; 40061b984aSJuergen Gross u64 type:8; 41061b984aSJuergen Gross u64 fixed:1; 42061b984aSJuergen Gross }; 43061b984aSJuergen Gross 44a4316603SJuergen Gross static bool mtrr_debug; 45a4316603SJuergen Gross 46a4316603SJuergen Gross static int __init mtrr_param_setup(char *str) 47a4316603SJuergen Gross { 48a4316603SJuergen Gross int rc = 0; 49a4316603SJuergen Gross 50a4316603SJuergen Gross if (!str) 51a4316603SJuergen Gross return -EINVAL; 52a4316603SJuergen Gross if (!strcmp(str, "debug")) 53a4316603SJuergen Gross mtrr_debug = true; 54a4316603SJuergen Gross else 55a4316603SJuergen Gross rc = -EINVAL; 56a4316603SJuergen Gross 57a4316603SJuergen Gross return rc; 58a4316603SJuergen Gross } 59a4316603SJuergen Gross early_param("mtrr", mtrr_param_setup); 60a4316603SJuergen Gross 61061b984aSJuergen Gross /* 62061b984aSJuergen Gross * CACHE_MAP_MAX is the maximum number of memory ranges in cache_map, where 63061b984aSJuergen Gross * no 2 adjacent ranges have the same cache mode (those would be merged). 64061b984aSJuergen Gross * The number is based on the worst case: 65061b984aSJuergen Gross * - no two adjacent fixed MTRRs share the same cache mode 66061b984aSJuergen Gross * - one variable MTRR is spanning a huge area with mode WB 67061b984aSJuergen Gross * - 255 variable MTRRs with mode UC all overlap with the WB MTRR, creating 2 68061b984aSJuergen Gross * additional ranges each (result like "ababababa...aba" with a = WB, b = UC), 69061b984aSJuergen Gross * accounting for MTRR_MAX_VAR_RANGES * 2 - 1 range entries 70061b984aSJuergen Gross * - a TOP_MEM2 area (even with overlapping an UC MTRR can't add 2 range entries 71061b984aSJuergen Gross * to the possible maximum, as it always starts at 4GB, thus it can't be in 72061b984aSJuergen Gross * the middle of that MTRR, unless that MTRR starts at 0, which would remove 73061b984aSJuergen Gross * the initial "a" from the "abababa" pattern above) 74061b984aSJuergen Gross * The map won't contain ranges with no matching MTRR (those fall back to the 75061b984aSJuergen Gross * default cache mode). 76061b984aSJuergen Gross */ 77061b984aSJuergen Gross #define CACHE_MAP_MAX (MTRR_NUM_FIXED_RANGES + MTRR_MAX_VAR_RANGES * 2) 78061b984aSJuergen Gross 79061b984aSJuergen Gross static struct cache_map init_cache_map[CACHE_MAP_MAX] __initdata; 80061b984aSJuergen Gross static struct cache_map *cache_map __refdata = init_cache_map; 81061b984aSJuergen Gross static unsigned int cache_map_size = CACHE_MAP_MAX; 82061b984aSJuergen Gross static unsigned int cache_map_n; 83061b984aSJuergen Gross static unsigned int cache_map_fixed; 84061b984aSJuergen Gross 852ec1df41SThomas Gleixner static unsigned long smp_changes_mask; 862e5d9c85Svenkatesh.pallipadi@intel.com static int mtrr_state_set; 8795ffa243SYinghai Lu u64 mtrr_tom2; 882ec1df41SThomas Gleixner 89a1a499a3SJaswinder Singh Rajput struct mtrr_state_type mtrr_state; 90932d27a7SSheng Yang EXPORT_SYMBOL_GPL(mtrr_state); 91932d27a7SSheng Yang 92d053b481SJuergen Gross /* Reserved bits in the high portion of the MTRRphysBaseN MSR. */ 93d053b481SJuergen Gross u32 phys_hi_rsvd; 94f6b98064SJuergen Gross 95a1a499a3SJaswinder Singh Rajput /* 963ff42da5SAndreas Herrmann * BIOS is expected to clear MtrrFixDramModEn bit, see for example 973ff42da5SAndreas Herrmann * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD 983ff42da5SAndreas Herrmann * Opteron Processors" (26094 Rev. 3.30 February 2006), section 993ff42da5SAndreas Herrmann * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set 1006a6256f9SAdam Buchbinder * to 1 during BIOS initialization of the fixed MTRRs, then cleared to 1013ff42da5SAndreas Herrmann * 0 for operation." 1023ff42da5SAndreas Herrmann */ 1033ff42da5SAndreas Herrmann static inline void k8_check_syscfg_dram_mod_en(void) 1043ff42da5SAndreas Herrmann { 1053ff42da5SAndreas Herrmann u32 lo, hi; 1063ff42da5SAndreas Herrmann 1073ff42da5SAndreas Herrmann if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && 1083ff42da5SAndreas Herrmann (boot_cpu_data.x86 >= 0x0f))) 1093ff42da5SAndreas Herrmann return; 1103ff42da5SAndreas Herrmann 111059e5c32SBrijesh Singh rdmsr(MSR_AMD64_SYSCFG, lo, hi); 1123ff42da5SAndreas Herrmann if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) { 1131b74dde7SChen Yucong pr_err(FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]" 1143ff42da5SAndreas Herrmann " not cleared by BIOS, clearing this bit\n", 1153ff42da5SAndreas Herrmann smp_processor_id()); 1163ff42da5SAndreas Herrmann lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY; 117059e5c32SBrijesh Singh mtrr_wrmsr(MSR_AMD64_SYSCFG, lo, hi); 1183ff42da5SAndreas Herrmann } 1193ff42da5SAndreas Herrmann } 1203ff42da5SAndreas Herrmann 121351e5a70SVenkatesh Pallipadi /* Get the size of contiguous MTRR range */ 122351e5a70SVenkatesh Pallipadi static u64 get_mtrr_size(u64 mask) 123351e5a70SVenkatesh Pallipadi { 124351e5a70SVenkatesh Pallipadi u64 size; 125351e5a70SVenkatesh Pallipadi 126d053b481SJuergen Gross mask |= (u64)phys_hi_rsvd << 32; 127351e5a70SVenkatesh Pallipadi size = -mask; 128d053b481SJuergen Gross 129351e5a70SVenkatesh Pallipadi return size; 130351e5a70SVenkatesh Pallipadi } 131351e5a70SVenkatesh Pallipadi 132061b984aSJuergen Gross static u8 get_var_mtrr_state(unsigned int reg, u64 *start, u64 *size) 133061b984aSJuergen Gross { 134061b984aSJuergen Gross struct mtrr_var_range *mtrr = mtrr_state.var_ranges + reg; 135061b984aSJuergen Gross 136061b984aSJuergen Gross if (!(mtrr->mask_lo & MTRR_PHYSMASK_V)) 137061b984aSJuergen Gross return MTRR_TYPE_INVALID; 138061b984aSJuergen Gross 139061b984aSJuergen Gross *start = (((u64)mtrr->base_hi) << 32) + (mtrr->base_lo & PAGE_MASK); 140061b984aSJuergen Gross *size = get_mtrr_size((((u64)mtrr->mask_hi) << 32) + 141061b984aSJuergen Gross (mtrr->mask_lo & PAGE_MASK)); 142061b984aSJuergen Gross 143061b984aSJuergen Gross return mtrr->base_lo & MTRR_PHYSBASE_TYPE; 144061b984aSJuergen Gross } 145061b984aSJuergen Gross 1461ca12099SJuergen Gross static u8 get_effective_type(u8 type1, u8 type2) 1471ca12099SJuergen Gross { 1481ca12099SJuergen Gross if (type1 == MTRR_TYPE_UNCACHABLE || type2 == MTRR_TYPE_UNCACHABLE) 1491ca12099SJuergen Gross return MTRR_TYPE_UNCACHABLE; 1501ca12099SJuergen Gross 1511ca12099SJuergen Gross if ((type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH) || 1521ca12099SJuergen Gross (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK)) 1531ca12099SJuergen Gross return MTRR_TYPE_WRTHROUGH; 1541ca12099SJuergen Gross 1551ca12099SJuergen Gross if (type1 != type2) 1561ca12099SJuergen Gross return MTRR_TYPE_UNCACHABLE; 1571ca12099SJuergen Gross 1581ca12099SJuergen Gross return type1; 1591ca12099SJuergen Gross } 1601ca12099SJuergen Gross 161061b984aSJuergen Gross static void rm_map_entry_at(int idx) 162061b984aSJuergen Gross { 163061b984aSJuergen Gross cache_map_n--; 164061b984aSJuergen Gross if (cache_map_n > idx) { 165061b984aSJuergen Gross memmove(cache_map + idx, cache_map + idx + 1, 166061b984aSJuergen Gross sizeof(*cache_map) * (cache_map_n - idx)); 167061b984aSJuergen Gross } 168061b984aSJuergen Gross } 169061b984aSJuergen Gross 170061b984aSJuergen Gross /* 171061b984aSJuergen Gross * Add an entry into cache_map at a specific index. Merges adjacent entries if 172061b984aSJuergen Gross * appropriate. Return the number of merges for correcting the scan index 173061b984aSJuergen Gross * (this is needed as merging will reduce the number of entries, which will 174061b984aSJuergen Gross * result in skipping entries in future iterations if the scan index isn't 175061b984aSJuergen Gross * corrected). 176061b984aSJuergen Gross * Note that the corrected index can never go below -1 (resulting in being 0 in 177061b984aSJuergen Gross * the next scan iteration), as "2" is returned only if the current index is 178061b984aSJuergen Gross * larger than zero. 179061b984aSJuergen Gross */ 180061b984aSJuergen Gross static int add_map_entry_at(u64 start, u64 end, u8 type, int idx) 181061b984aSJuergen Gross { 182061b984aSJuergen Gross bool merge_prev = false, merge_next = false; 183061b984aSJuergen Gross 184061b984aSJuergen Gross if (start >= end) 185061b984aSJuergen Gross return 0; 186061b984aSJuergen Gross 187061b984aSJuergen Gross if (idx > 0) { 188061b984aSJuergen Gross struct cache_map *prev = cache_map + idx - 1; 189061b984aSJuergen Gross 190061b984aSJuergen Gross if (!prev->fixed && start == prev->end && type == prev->type) 191061b984aSJuergen Gross merge_prev = true; 192061b984aSJuergen Gross } 193061b984aSJuergen Gross 194061b984aSJuergen Gross if (idx < cache_map_n) { 195061b984aSJuergen Gross struct cache_map *next = cache_map + idx; 196061b984aSJuergen Gross 197061b984aSJuergen Gross if (!next->fixed && end == next->start && type == next->type) 198061b984aSJuergen Gross merge_next = true; 199061b984aSJuergen Gross } 200061b984aSJuergen Gross 201061b984aSJuergen Gross if (merge_prev && merge_next) { 202061b984aSJuergen Gross cache_map[idx - 1].end = cache_map[idx].end; 203061b984aSJuergen Gross rm_map_entry_at(idx); 204061b984aSJuergen Gross return 2; 205061b984aSJuergen Gross } 206061b984aSJuergen Gross if (merge_prev) { 207061b984aSJuergen Gross cache_map[idx - 1].end = end; 208061b984aSJuergen Gross return 1; 209061b984aSJuergen Gross } 210061b984aSJuergen Gross if (merge_next) { 211061b984aSJuergen Gross cache_map[idx].start = start; 212061b984aSJuergen Gross return 1; 213061b984aSJuergen Gross } 214061b984aSJuergen Gross 215061b984aSJuergen Gross /* Sanity check: the array should NEVER be too small! */ 216061b984aSJuergen Gross if (cache_map_n == cache_map_size) { 217061b984aSJuergen Gross WARN(1, "MTRR cache mode memory map exhausted!\n"); 218061b984aSJuergen Gross cache_map_n = cache_map_fixed; 219061b984aSJuergen Gross return 0; 220061b984aSJuergen Gross } 221061b984aSJuergen Gross 222061b984aSJuergen Gross if (cache_map_n > idx) { 223061b984aSJuergen Gross memmove(cache_map + idx + 1, cache_map + idx, 224061b984aSJuergen Gross sizeof(*cache_map) * (cache_map_n - idx)); 225061b984aSJuergen Gross } 226061b984aSJuergen Gross 227061b984aSJuergen Gross cache_map[idx].start = start; 228061b984aSJuergen Gross cache_map[idx].end = end; 229061b984aSJuergen Gross cache_map[idx].type = type; 230061b984aSJuergen Gross cache_map[idx].fixed = 0; 231061b984aSJuergen Gross cache_map_n++; 232061b984aSJuergen Gross 233061b984aSJuergen Gross return 0; 234061b984aSJuergen Gross } 235061b984aSJuergen Gross 236061b984aSJuergen Gross /* Clear a part of an entry. Return 1 if start of entry is still valid. */ 237061b984aSJuergen Gross static int clr_map_range_at(u64 start, u64 end, int idx) 238061b984aSJuergen Gross { 239061b984aSJuergen Gross int ret = start != cache_map[idx].start; 240061b984aSJuergen Gross u64 tmp; 241061b984aSJuergen Gross 242061b984aSJuergen Gross if (start == cache_map[idx].start && end == cache_map[idx].end) { 243061b984aSJuergen Gross rm_map_entry_at(idx); 244061b984aSJuergen Gross } else if (start == cache_map[idx].start) { 245061b984aSJuergen Gross cache_map[idx].start = end; 246061b984aSJuergen Gross } else if (end == cache_map[idx].end) { 247061b984aSJuergen Gross cache_map[idx].end = start; 248061b984aSJuergen Gross } else { 249061b984aSJuergen Gross tmp = cache_map[idx].end; 250061b984aSJuergen Gross cache_map[idx].end = start; 251061b984aSJuergen Gross add_map_entry_at(end, tmp, cache_map[idx].type, idx + 1); 252061b984aSJuergen Gross } 253061b984aSJuergen Gross 254061b984aSJuergen Gross return ret; 255061b984aSJuergen Gross } 256061b984aSJuergen Gross 257061b984aSJuergen Gross /* 258061b984aSJuergen Gross * Add MTRR to the map. The current map is scanned and each part of the MTRR 259061b984aSJuergen Gross * either overlapping with an existing entry or with a hole in the map is 260061b984aSJuergen Gross * handled separately. 261061b984aSJuergen Gross */ 262061b984aSJuergen Gross static void add_map_entry(u64 start, u64 end, u8 type) 263061b984aSJuergen Gross { 264061b984aSJuergen Gross u8 new_type, old_type; 265061b984aSJuergen Gross u64 tmp; 266061b984aSJuergen Gross int i; 267061b984aSJuergen Gross 268061b984aSJuergen Gross for (i = 0; i < cache_map_n && start < end; i++) { 269061b984aSJuergen Gross if (start >= cache_map[i].end) 270061b984aSJuergen Gross continue; 271061b984aSJuergen Gross 272061b984aSJuergen Gross if (start < cache_map[i].start) { 273061b984aSJuergen Gross /* Region start has no overlap. */ 274061b984aSJuergen Gross tmp = min(end, cache_map[i].start); 275061b984aSJuergen Gross i -= add_map_entry_at(start, tmp, type, i); 276061b984aSJuergen Gross start = tmp; 277061b984aSJuergen Gross continue; 278061b984aSJuergen Gross } 279061b984aSJuergen Gross 280061b984aSJuergen Gross new_type = get_effective_type(type, cache_map[i].type); 281061b984aSJuergen Gross old_type = cache_map[i].type; 282061b984aSJuergen Gross 283061b984aSJuergen Gross if (cache_map[i].fixed || new_type == old_type) { 284061b984aSJuergen Gross /* Cut off start of new entry. */ 285061b984aSJuergen Gross start = cache_map[i].end; 286061b984aSJuergen Gross continue; 287061b984aSJuergen Gross } 288061b984aSJuergen Gross 289061b984aSJuergen Gross /* Handle only overlapping part of region. */ 290061b984aSJuergen Gross tmp = min(end, cache_map[i].end); 291061b984aSJuergen Gross i += clr_map_range_at(start, tmp, i); 292061b984aSJuergen Gross i -= add_map_entry_at(start, tmp, new_type, i); 293061b984aSJuergen Gross start = tmp; 294061b984aSJuergen Gross } 295061b984aSJuergen Gross 296061b984aSJuergen Gross /* Add rest of region after last map entry (rest might be empty). */ 297061b984aSJuergen Gross add_map_entry_at(start, end, type, i); 298061b984aSJuergen Gross } 299061b984aSJuergen Gross 300061b984aSJuergen Gross /* Add variable MTRRs to cache map. */ 301061b984aSJuergen Gross static void map_add_var(void) 302061b984aSJuergen Gross { 303061b984aSJuergen Gross u64 start, size; 304061b984aSJuergen Gross unsigned int i; 305061b984aSJuergen Gross u8 type; 306061b984aSJuergen Gross 307061b984aSJuergen Gross /* 308061b984aSJuergen Gross * Add AMD TOP_MEM2 area. Can't be added in mtrr_build_map(), as it 309061b984aSJuergen Gross * needs to be added again when rebuilding the map due to potentially 310061b984aSJuergen Gross * having moved as a result of variable MTRRs for memory below 4GB. 311061b984aSJuergen Gross */ 312061b984aSJuergen Gross if (mtrr_tom2) { 313061b984aSJuergen Gross add_map_entry(BIT_ULL(32), mtrr_tom2, MTRR_TYPE_WRBACK); 314061b984aSJuergen Gross cache_map[cache_map_n - 1].fixed = 1; 315061b984aSJuergen Gross } 316061b984aSJuergen Gross 317061b984aSJuergen Gross for (i = 0; i < num_var_ranges; i++) { 318061b984aSJuergen Gross type = get_var_mtrr_state(i, &start, &size); 319061b984aSJuergen Gross if (type != MTRR_TYPE_INVALID) 320061b984aSJuergen Gross add_map_entry(start, start + size, type); 321061b984aSJuergen Gross } 322061b984aSJuergen Gross } 323061b984aSJuergen Gross 324061b984aSJuergen Gross /* 325061b984aSJuergen Gross * Rebuild map by replacing variable entries. Needs to be called when MTRR 326061b984aSJuergen Gross * registers are being changed after boot, as such changes could include 327061b984aSJuergen Gross * removals of registers, which are complicated to handle without rebuild of 328061b984aSJuergen Gross * the map. 329061b984aSJuergen Gross */ 330061b984aSJuergen Gross void generic_rebuild_map(void) 331061b984aSJuergen Gross { 332061b984aSJuergen Gross if (mtrr_if != &generic_mtrr_ops) 333061b984aSJuergen Gross return; 334061b984aSJuergen Gross 335061b984aSJuergen Gross cache_map_n = cache_map_fixed; 336061b984aSJuergen Gross 337061b984aSJuergen Gross map_add_var(); 338061b984aSJuergen Gross } 339061b984aSJuergen Gross 340061b984aSJuergen Gross static unsigned int __init get_cache_map_size(void) 341061b984aSJuergen Gross { 342061b984aSJuergen Gross return cache_map_fixed + 2 * num_var_ranges + (mtrr_tom2 != 0); 343061b984aSJuergen Gross } 344061b984aSJuergen Gross 345061b984aSJuergen Gross /* Build the cache_map containing the cache modes per memory range. */ 346061b984aSJuergen Gross void __init mtrr_build_map(void) 347061b984aSJuergen Gross { 348061b984aSJuergen Gross u64 start, end, size; 349061b984aSJuergen Gross unsigned int i; 350061b984aSJuergen Gross u8 type; 351061b984aSJuergen Gross 352061b984aSJuergen Gross /* Add fixed MTRRs, optimize for adjacent entries with same type. */ 353061b984aSJuergen Gross if (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED) { 354061b984aSJuergen Gross /* 355061b984aSJuergen Gross * Start with 64k size fixed entries, preset 1st one (hence the 356061b984aSJuergen Gross * loop below is starting with index 1). 357061b984aSJuergen Gross */ 358061b984aSJuergen Gross start = 0; 359061b984aSJuergen Gross end = size = 0x10000; 360061b984aSJuergen Gross type = mtrr_state.fixed_ranges[0]; 361061b984aSJuergen Gross 362061b984aSJuergen Gross for (i = 1; i < MTRR_NUM_FIXED_RANGES; i++) { 363061b984aSJuergen Gross /* 8 64k entries, then 16 16k ones, rest 4k. */ 364061b984aSJuergen Gross if (i == 8 || i == 24) 365061b984aSJuergen Gross size >>= 2; 366061b984aSJuergen Gross 367061b984aSJuergen Gross if (mtrr_state.fixed_ranges[i] != type) { 368061b984aSJuergen Gross add_map_entry(start, end, type); 369061b984aSJuergen Gross start = end; 370061b984aSJuergen Gross type = mtrr_state.fixed_ranges[i]; 371061b984aSJuergen Gross } 372061b984aSJuergen Gross end += size; 373061b984aSJuergen Gross } 374061b984aSJuergen Gross add_map_entry(start, end, type); 375061b984aSJuergen Gross } 376061b984aSJuergen Gross 377061b984aSJuergen Gross /* Mark fixed, they take precedence. */ 378061b984aSJuergen Gross for (i = 0; i < cache_map_n; i++) 379061b984aSJuergen Gross cache_map[i].fixed = 1; 380061b984aSJuergen Gross cache_map_fixed = cache_map_n; 381061b984aSJuergen Gross 382061b984aSJuergen Gross map_add_var(); 383061b984aSJuergen Gross 384061b984aSJuergen Gross pr_info("MTRR map: %u entries (%u fixed + %u variable; max %u), built from %u variable MTRRs\n", 385061b984aSJuergen Gross cache_map_n, cache_map_fixed, cache_map_n - cache_map_fixed, 386061b984aSJuergen Gross get_cache_map_size(), num_var_ranges + (mtrr_tom2 != 0)); 387a4316603SJuergen Gross 388a4316603SJuergen Gross if (mtrr_debug) { 389a4316603SJuergen Gross for (i = 0; i < cache_map_n; i++) { 390a4316603SJuergen Gross pr_info("%3u: %016llx-%016llx %s\n", i, 391a4316603SJuergen Gross cache_map[i].start, cache_map[i].end - 1, 392a4316603SJuergen Gross mtrr_attrib_to_str(cache_map[i].type)); 393a4316603SJuergen Gross } 394a4316603SJuergen Gross } 395061b984aSJuergen Gross } 396061b984aSJuergen Gross 397061b984aSJuergen Gross /* Copy the cache_map from __initdata memory to dynamically allocated one. */ 398061b984aSJuergen Gross void __init mtrr_copy_map(void) 399061b984aSJuergen Gross { 400061b984aSJuergen Gross unsigned int new_size = get_cache_map_size(); 401061b984aSJuergen Gross 402061b984aSJuergen Gross if (!mtrr_state.enabled || !new_size) { 403061b984aSJuergen Gross cache_map = NULL; 404061b984aSJuergen Gross return; 405061b984aSJuergen Gross } 406061b984aSJuergen Gross 407061b984aSJuergen Gross mutex_lock(&mtrr_mutex); 408061b984aSJuergen Gross 409061b984aSJuergen Gross cache_map = kcalloc(new_size, sizeof(*cache_map), GFP_KERNEL); 410061b984aSJuergen Gross if (cache_map) { 411061b984aSJuergen Gross memmove(cache_map, init_cache_map, 412061b984aSJuergen Gross cache_map_n * sizeof(*cache_map)); 413061b984aSJuergen Gross cache_map_size = new_size; 414061b984aSJuergen Gross } else { 415061b984aSJuergen Gross mtrr_state.enabled = 0; 416061b984aSJuergen Gross pr_err("MTRRs disabled due to allocation failure for lookup map.\n"); 417061b984aSJuergen Gross } 418061b984aSJuergen Gross 419061b984aSJuergen Gross mutex_unlock(&mtrr_mutex); 420061b984aSJuergen Gross } 421061b984aSJuergen Gross 4220cc705f5SToshi Kani /** 42329055dc7SJuergen Gross * mtrr_overwrite_state - set static MTRR state 42429055dc7SJuergen Gross * 42529055dc7SJuergen Gross * Used to set MTRR state via different means (e.g. with data obtained from 42629055dc7SJuergen Gross * a hypervisor). 42729055dc7SJuergen Gross * Is allowed only for special cases when running virtualized. Must be called 42829055dc7SJuergen Gross * from the x86_init.hyper.init_platform() hook. It can be called only once. 42929055dc7SJuergen Gross * The MTRR state can't be changed afterwards. To ensure that, X86_FEATURE_MTRR 43029055dc7SJuergen Gross * is cleared. 43129055dc7SJuergen Gross */ 43229055dc7SJuergen Gross void mtrr_overwrite_state(struct mtrr_var_range *var, unsigned int num_var, 43329055dc7SJuergen Gross mtrr_type def_type) 43429055dc7SJuergen Gross { 43529055dc7SJuergen Gross unsigned int i; 43629055dc7SJuergen Gross 43729055dc7SJuergen Gross /* Only allowed to be called once before mtrr_bp_init(). */ 43829055dc7SJuergen Gross if (WARN_ON_ONCE(mtrr_state_set)) 43929055dc7SJuergen Gross return; 44029055dc7SJuergen Gross 44129055dc7SJuergen Gross /* Only allowed when running virtualized. */ 44229055dc7SJuergen Gross if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) 44329055dc7SJuergen Gross return; 44429055dc7SJuergen Gross 44529055dc7SJuergen Gross /* 44629055dc7SJuergen Gross * Only allowed for special virtualization cases: 44729055dc7SJuergen Gross * - when running as Hyper-V, SEV-SNP guest using vTOM 44829055dc7SJuergen Gross * - when running as Xen PV guest 44929055dc7SJuergen Gross * - when running as SEV-SNP or TDX guest to avoid unnecessary 45029055dc7SJuergen Gross * VMM communication/Virtualization exceptions (#VC, #VE) 45129055dc7SJuergen Gross */ 45229055dc7SJuergen Gross if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && 45329055dc7SJuergen Gross !hv_is_isolation_supported() && 45429055dc7SJuergen Gross !cpu_feature_enabled(X86_FEATURE_XENPV) && 45529055dc7SJuergen Gross !cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) 45629055dc7SJuergen Gross return; 45729055dc7SJuergen Gross 45829055dc7SJuergen Gross /* Disable MTRR in order to disable MTRR modifications. */ 45929055dc7SJuergen Gross setup_clear_cpu_cap(X86_FEATURE_MTRR); 46029055dc7SJuergen Gross 46129055dc7SJuergen Gross if (var) { 46229055dc7SJuergen Gross if (num_var > MTRR_MAX_VAR_RANGES) { 46329055dc7SJuergen Gross pr_warn("Trying to overwrite MTRR state with %u variable entries\n", 46429055dc7SJuergen Gross num_var); 46529055dc7SJuergen Gross num_var = MTRR_MAX_VAR_RANGES; 46629055dc7SJuergen Gross } 46729055dc7SJuergen Gross for (i = 0; i < num_var; i++) 46829055dc7SJuergen Gross mtrr_state.var_ranges[i] = var[i]; 46929055dc7SJuergen Gross num_var_ranges = num_var; 47029055dc7SJuergen Gross } 47129055dc7SJuergen Gross 47229055dc7SJuergen Gross mtrr_state.def_type = def_type; 47329055dc7SJuergen Gross mtrr_state.enabled |= MTRR_STATE_MTRR_ENABLED; 47429055dc7SJuergen Gross 47529055dc7SJuergen Gross mtrr_state_set = 1; 47629055dc7SJuergen Gross } 47729055dc7SJuergen Gross 478*8227f40aSJuergen Gross static u8 type_merge(u8 type, u8 new_type, u8 *uniform) 479*8227f40aSJuergen Gross { 480*8227f40aSJuergen Gross u8 effective_type; 481*8227f40aSJuergen Gross 482*8227f40aSJuergen Gross if (type == MTRR_TYPE_INVALID) 483*8227f40aSJuergen Gross return new_type; 484*8227f40aSJuergen Gross 485*8227f40aSJuergen Gross effective_type = get_effective_type(type, new_type); 486*8227f40aSJuergen Gross if (type != effective_type) 487*8227f40aSJuergen Gross *uniform = 0; 488*8227f40aSJuergen Gross 489*8227f40aSJuergen Gross return effective_type; 490*8227f40aSJuergen Gross } 491*8227f40aSJuergen Gross 49229055dc7SJuergen Gross /** 4930cc705f5SToshi Kani * mtrr_type_lookup - look up memory type in MTRR 4940cc705f5SToshi Kani * 4950cc705f5SToshi Kani * Return Values: 4960cc705f5SToshi Kani * MTRR_TYPE_(type) - The effective MTRR type for the region 4970cc705f5SToshi Kani * MTRR_TYPE_INVALID - MTRR is disabled 498b73522e0SToshi Kani * 499b73522e0SToshi Kani * Output Argument: 500*8227f40aSJuergen Gross * uniform - Set to 1 when the returned MTRR type is valid for the whole 501*8227f40aSJuergen Gross * region, set to 0 else. 502351e5a70SVenkatesh Pallipadi */ 503b73522e0SToshi Kani u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform) 504351e5a70SVenkatesh Pallipadi { 505*8227f40aSJuergen Gross u8 type = MTRR_TYPE_INVALID; 506*8227f40aSJuergen Gross unsigned int i; 507351e5a70SVenkatesh Pallipadi 508*8227f40aSJuergen Gross if (!mtrr_state_set) { 509*8227f40aSJuergen Gross /* Uniformity is unknown. */ 510*8227f40aSJuergen Gross *uniform = 0; 5110cc705f5SToshi Kani return MTRR_TYPE_INVALID; 512*8227f40aSJuergen Gross } 513*8227f40aSJuergen Gross 514*8227f40aSJuergen Gross *uniform = 1; 5150cc705f5SToshi Kani 5160cc705f5SToshi Kani if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) 5170cc705f5SToshi Kani return MTRR_TYPE_INVALID; 5180cc705f5SToshi Kani 519*8227f40aSJuergen Gross for (i = 0; i < cache_map_n && start < end; i++) { 520*8227f40aSJuergen Gross /* Region after current map entry? -> continue with next one. */ 521*8227f40aSJuergen Gross if (start >= cache_map[i].end) 522*8227f40aSJuergen Gross continue; 523*8227f40aSJuergen Gross 524*8227f40aSJuergen Gross /* Start of region not covered by current map entry? */ 525*8227f40aSJuergen Gross if (start < cache_map[i].start) { 526*8227f40aSJuergen Gross /* At least some part of region has default type. */ 527*8227f40aSJuergen Gross type = type_merge(type, mtrr_state.def_type, uniform); 528*8227f40aSJuergen Gross /* End of region not covered, too? -> lookup done. */ 529*8227f40aSJuergen Gross if (end <= cache_map[i].start) 530*8227f40aSJuergen Gross return type; 531b73522e0SToshi Kani } 5320cc705f5SToshi Kani 533*8227f40aSJuergen Gross /* At least part of region covered by map entry. */ 534*8227f40aSJuergen Gross type = type_merge(type, cache_map[i].type, uniform); 535351e5a70SVenkatesh Pallipadi 536*8227f40aSJuergen Gross start = cache_map[i].end; 537351e5a70SVenkatesh Pallipadi } 538351e5a70SVenkatesh Pallipadi 539*8227f40aSJuergen Gross /* End of region past last entry in map? -> use default type. */ 540*8227f40aSJuergen Gross if (start < end) 541*8227f40aSJuergen Gross type = type_merge(type, mtrr_state.def_type, uniform); 5420cc705f5SToshi Kani 543351e5a70SVenkatesh Pallipadi return type; 544351e5a70SVenkatesh Pallipadi } 545351e5a70SVenkatesh Pallipadi 5462ec1df41SThomas Gleixner /* Get the MSR pair relating to a var range */ 5472ec1df41SThomas Gleixner static void 5482ec1df41SThomas Gleixner get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr) 5492ec1df41SThomas Gleixner { 5502ec1df41SThomas Gleixner rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi); 5512ec1df41SThomas Gleixner rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi); 5522ec1df41SThomas Gleixner } 5532ec1df41SThomas Gleixner 554a1a499a3SJaswinder Singh Rajput /* Fill the MSR pair relating to a var range */ 55595ffa243SYinghai Lu void fill_mtrr_var_range(unsigned int index, 55695ffa243SYinghai Lu u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi) 55795ffa243SYinghai Lu { 55895ffa243SYinghai Lu struct mtrr_var_range *vr; 55995ffa243SYinghai Lu 56095ffa243SYinghai Lu vr = mtrr_state.var_ranges; 56195ffa243SYinghai Lu 56295ffa243SYinghai Lu vr[index].base_lo = base_lo; 56395ffa243SYinghai Lu vr[index].base_hi = base_hi; 56495ffa243SYinghai Lu vr[index].mask_lo = mask_lo; 56595ffa243SYinghai Lu vr[index].mask_hi = mask_hi; 56695ffa243SYinghai Lu } 56795ffa243SYinghai Lu 568a1a499a3SJaswinder Singh Rajput static void get_fixed_ranges(mtrr_type *frs) 5692ec1df41SThomas Gleixner { 5702ec1df41SThomas Gleixner unsigned int *p = (unsigned int *)frs; 5712ec1df41SThomas Gleixner int i; 5722ec1df41SThomas Gleixner 5733ff42da5SAndreas Herrmann k8_check_syscfg_dram_mod_en(); 5743ff42da5SAndreas Herrmann 575a036c7a3SJaswinder Singh Rajput rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]); 5762ec1df41SThomas Gleixner 5772ec1df41SThomas Gleixner for (i = 0; i < 2; i++) 5787d9d55e4SJaswinder Singh Rajput rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]); 5792ec1df41SThomas Gleixner for (i = 0; i < 8; i++) 580ba5673ffSJaswinder Singh Rajput rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]); 5812ec1df41SThomas Gleixner } 5822ec1df41SThomas Gleixner 5832ec1df41SThomas Gleixner void mtrr_save_fixed_ranges(void *info) 5842ec1df41SThomas Gleixner { 585362f924bSBorislav Petkov if (boot_cpu_has(X86_FEATURE_MTRR)) 5862ec1df41SThomas Gleixner get_fixed_ranges(mtrr_state.fixed_ranges); 5872ec1df41SThomas Gleixner } 5882ec1df41SThomas Gleixner 589d4c90e37SYinghai Lu static unsigned __initdata last_fixed_start; 590d4c90e37SYinghai Lu static unsigned __initdata last_fixed_end; 591d4c90e37SYinghai Lu static mtrr_type __initdata last_fixed_type; 592d4c90e37SYinghai Lu 593d4c90e37SYinghai Lu static void __init print_fixed_last(void) 594d4c90e37SYinghai Lu { 595d4c90e37SYinghai Lu if (!last_fixed_end) 596d4c90e37SYinghai Lu return; 597d4c90e37SYinghai Lu 598a4316603SJuergen Gross pr_info(" %05X-%05X %s\n", last_fixed_start, 599d4c90e37SYinghai Lu last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type)); 600d4c90e37SYinghai Lu 601d4c90e37SYinghai Lu last_fixed_end = 0; 602d4c90e37SYinghai Lu } 603d4c90e37SYinghai Lu 604d4c90e37SYinghai Lu static void __init update_fixed_last(unsigned base, unsigned end, 605d4c90e37SYinghai Lu mtrr_type type) 606d4c90e37SYinghai Lu { 607d4c90e37SYinghai Lu last_fixed_start = base; 608d4c90e37SYinghai Lu last_fixed_end = end; 609d4c90e37SYinghai Lu last_fixed_type = type; 610d4c90e37SYinghai Lu } 611d4c90e37SYinghai Lu 612a1a499a3SJaswinder Singh Rajput static void __init 613a1a499a3SJaswinder Singh Rajput print_fixed(unsigned base, unsigned step, const mtrr_type *types) 6142ec1df41SThomas Gleixner { 6152ec1df41SThomas Gleixner unsigned i; 6162ec1df41SThomas Gleixner 617d4c90e37SYinghai Lu for (i = 0; i < 8; ++i, ++types, base += step) { 618d4c90e37SYinghai Lu if (last_fixed_end == 0) { 619d4c90e37SYinghai Lu update_fixed_last(base, base + step, *types); 620d4c90e37SYinghai Lu continue; 621d4c90e37SYinghai Lu } 622d4c90e37SYinghai Lu if (last_fixed_end == base && last_fixed_type == *types) { 623d4c90e37SYinghai Lu last_fixed_end = base + step; 624d4c90e37SYinghai Lu continue; 625d4c90e37SYinghai Lu } 626d4c90e37SYinghai Lu /* new segments: gap or different type */ 627d4c90e37SYinghai Lu print_fixed_last(); 628d4c90e37SYinghai Lu update_fixed_last(base, base + step, *types); 629d4c90e37SYinghai Lu } 6302ec1df41SThomas Gleixner } 6312ec1df41SThomas Gleixner 6328ad97905SYinghai Lu static void __init print_mtrr_state(void) 6338ad97905SYinghai Lu { 6348ad97905SYinghai Lu unsigned int i; 6358ad97905SYinghai Lu int high_width; 6368ad97905SYinghai Lu 637a4316603SJuergen Gross pr_info("MTRR default type: %s\n", 638d4c90e37SYinghai Lu mtrr_attrib_to_str(mtrr_state.def_type)); 6398ad97905SYinghai Lu if (mtrr_state.have_fixed) { 640a4316603SJuergen Gross pr_info("MTRR fixed ranges %sabled:\n", 6419b3aca62SToshi Kani ((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) && 6429b3aca62SToshi Kani (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ? 6439b3aca62SToshi Kani "en" : "dis"); 6448ad97905SYinghai Lu print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0); 6458ad97905SYinghai Lu for (i = 0; i < 2; ++i) 646a1a499a3SJaswinder Singh Rajput print_fixed(0x80000 + i * 0x20000, 0x04000, 647a1a499a3SJaswinder Singh Rajput mtrr_state.fixed_ranges + (i + 1) * 8); 6488ad97905SYinghai Lu for (i = 0; i < 8; ++i) 649a1a499a3SJaswinder Singh Rajput print_fixed(0xC0000 + i * 0x08000, 0x01000, 650a1a499a3SJaswinder Singh Rajput mtrr_state.fixed_ranges + (i + 3) * 8); 651d4c90e37SYinghai Lu 652d4c90e37SYinghai Lu /* tail */ 653d4c90e37SYinghai Lu print_fixed_last(); 6548ad97905SYinghai Lu } 655a4316603SJuergen Gross pr_info("MTRR variable ranges %sabled:\n", 6569b3aca62SToshi Kani mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis"); 657f6b98064SJuergen Gross high_width = (boot_cpu_data.x86_phys_bits - (32 - PAGE_SHIFT) + 3) / 4; 658a1a499a3SJaswinder Singh Rajput 6598ad97905SYinghai Lu for (i = 0; i < num_var_ranges; ++i) { 660d053b481SJuergen Gross if (mtrr_state.var_ranges[i].mask_lo & MTRR_PHYSMASK_V) 661a4316603SJuergen Gross pr_info(" %u base %0*X%05X000 mask %0*X%05X000 %s\n", 6628ad97905SYinghai Lu i, 6638ad97905SYinghai Lu high_width, 6648ad97905SYinghai Lu mtrr_state.var_ranges[i].base_hi, 6658ad97905SYinghai Lu mtrr_state.var_ranges[i].base_lo >> 12, 6668ad97905SYinghai Lu high_width, 6678ad97905SYinghai Lu mtrr_state.var_ranges[i].mask_hi, 6688ad97905SYinghai Lu mtrr_state.var_ranges[i].mask_lo >> 12, 669d053b481SJuergen Gross mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 670d053b481SJuergen Gross MTRR_PHYSBASE_TYPE)); 6718ad97905SYinghai Lu else 672a4316603SJuergen Gross pr_info(" %u disabled\n", i); 6738ad97905SYinghai Lu } 674a1a499a3SJaswinder Singh Rajput if (mtrr_tom2) 675a4316603SJuergen Gross pr_info("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20); 6768ad97905SYinghai Lu } 6778ad97905SYinghai Lu 6782ec1df41SThomas Gleixner /* Grab all of the MTRR state for this CPU into *state */ 679f9626104SLuis R. Rodriguez bool __init get_mtrr_state(void) 6802ec1df41SThomas Gleixner { 6812ec1df41SThomas Gleixner struct mtrr_var_range *vrs; 682a1a499a3SJaswinder Singh Rajput unsigned lo, dummy; 683a1a499a3SJaswinder Singh Rajput unsigned int i; 6842ec1df41SThomas Gleixner 6852ec1df41SThomas Gleixner vrs = mtrr_state.var_ranges; 6862ec1df41SThomas Gleixner 687d9bcc01dSJaswinder Singh Rajput rdmsr(MSR_MTRRcap, lo, dummy); 688d053b481SJuergen Gross mtrr_state.have_fixed = lo & MTRR_CAP_FIX; 6892ec1df41SThomas Gleixner 6902ec1df41SThomas Gleixner for (i = 0; i < num_var_ranges; i++) 6912ec1df41SThomas Gleixner get_mtrr_var_range(i, &vrs[i]); 6922ec1df41SThomas Gleixner if (mtrr_state.have_fixed) 6932ec1df41SThomas Gleixner get_fixed_ranges(mtrr_state.fixed_ranges); 6942ec1df41SThomas Gleixner 69552650257SJaswinder Singh Rajput rdmsr(MSR_MTRRdefType, lo, dummy); 696d053b481SJuergen Gross mtrr_state.def_type = lo & MTRR_DEF_TYPE_TYPE; 697d053b481SJuergen Gross mtrr_state.enabled = (lo & MTRR_DEF_TYPE_ENABLE) >> MTRR_STATE_SHIFT; 6982ec1df41SThomas Gleixner 69935605a10SYinghai Lu if (amd_special_default_mtrr()) { 7000da72a4aSThomas Gleixner unsigned low, high; 701a1a499a3SJaswinder Singh Rajput 70235605a10SYinghai Lu /* TOP_MEM2 */ 7030da72a4aSThomas Gleixner rdmsr(MSR_K8_TOP_MEM2, low, high); 70495ffa243SYinghai Lu mtrr_tom2 = high; 70595ffa243SYinghai Lu mtrr_tom2 <<= 32; 70695ffa243SYinghai Lu mtrr_tom2 |= low; 7078004dd96SYinghai Lu mtrr_tom2 &= 0xffffff800000ULL; 70835605a10SYinghai Lu } 7092ec1df41SThomas Gleixner 710a4316603SJuergen Gross if (mtrr_debug) 7118ad97905SYinghai Lu print_mtrr_state(); 7128ad97905SYinghai Lu 7132e5d9c85Svenkatesh.pallipadi@intel.com mtrr_state_set = 1; 7142e5d9c85Svenkatesh.pallipadi@intel.com 715f9626104SLuis R. Rodriguez return !!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED); 7162ec1df41SThomas Gleixner } 7172ec1df41SThomas Gleixner 718a1a499a3SJaswinder Singh Rajput /* Some BIOS's are messed up and don't set all MTRRs the same! */ 7192ec1df41SThomas Gleixner void __init mtrr_state_warn(void) 7202ec1df41SThomas Gleixner { 7212ec1df41SThomas Gleixner unsigned long mask = smp_changes_mask; 7222ec1df41SThomas Gleixner 7232ec1df41SThomas Gleixner if (!mask) 7242ec1df41SThomas Gleixner return; 7252ec1df41SThomas Gleixner if (mask & MTRR_CHANGE_MASK_FIXED) 7261b74dde7SChen Yucong pr_warn("mtrr: your CPUs had inconsistent fixed MTRR settings\n"); 7272ec1df41SThomas Gleixner if (mask & MTRR_CHANGE_MASK_VARIABLE) 7281b74dde7SChen Yucong pr_warn("mtrr: your CPUs had inconsistent variable MTRR settings\n"); 7292ec1df41SThomas Gleixner if (mask & MTRR_CHANGE_MASK_DEFTYPE) 7301b74dde7SChen Yucong pr_warn("mtrr: your CPUs had inconsistent MTRRdefType settings\n"); 731a1a499a3SJaswinder Singh Rajput 7321b74dde7SChen Yucong pr_info("mtrr: probably your BIOS does not setup all CPUs.\n"); 7331b74dde7SChen Yucong pr_info("mtrr: corrected configuration.\n"); 7342ec1df41SThomas Gleixner } 7352ec1df41SThomas Gleixner 736a1a499a3SJaswinder Singh Rajput /* 737a1a499a3SJaswinder Singh Rajput * Doesn't attempt to pass an error out to MTRR users 738a1a499a3SJaswinder Singh Rajput * because it's quite complicated in some cases and probably not 739a1a499a3SJaswinder Singh Rajput * worth it because the best error handling is to ignore it. 740a1a499a3SJaswinder Singh Rajput */ 7412ec1df41SThomas Gleixner void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b) 7422ec1df41SThomas Gleixner { 743a1a499a3SJaswinder Singh Rajput if (wrmsr_safe(msr, a, b) < 0) { 7441b74dde7SChen Yucong pr_err("MTRR: CPU %u: Writing MSR %x to %x:%x failed\n", 7452ec1df41SThomas Gleixner smp_processor_id(), msr, a, b); 7462ec1df41SThomas Gleixner } 747a1a499a3SJaswinder Singh Rajput } 7482ec1df41SThomas Gleixner 7492ec1df41SThomas Gleixner /** 750a1a499a3SJaswinder Singh Rajput * set_fixed_range - checks & updates a fixed-range MTRR if it 751a1a499a3SJaswinder Singh Rajput * differs from the value it should have 7521d3381ebSRandy Dunlap * @msr: MSR address of the MTTR which should be checked and updated 7531d3381ebSRandy Dunlap * @changed: pointer which indicates whether the MTRR needed to be changed 7541d3381ebSRandy Dunlap * @msrwords: pointer to the MSR values which the MSR should have 7552ec1df41SThomas Gleixner */ 7562d2ee8deSPaul Jimenez static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords) 7572ec1df41SThomas Gleixner { 7582ec1df41SThomas Gleixner unsigned lo, hi; 7592ec1df41SThomas Gleixner 7602ec1df41SThomas Gleixner rdmsr(msr, lo, hi); 7612ec1df41SThomas Gleixner 7622ec1df41SThomas Gleixner if (lo != msrwords[0] || hi != msrwords[1]) { 7632ec1df41SThomas Gleixner mtrr_wrmsr(msr, msrwords[0], msrwords[1]); 7642d2ee8deSPaul Jimenez *changed = true; 7652ec1df41SThomas Gleixner } 7662ec1df41SThomas Gleixner } 7672ec1df41SThomas Gleixner 7681d3381ebSRandy Dunlap /** 7691d3381ebSRandy Dunlap * generic_get_free_region - Get a free MTRR. 7701d3381ebSRandy Dunlap * @base: The starting (base) address of the region. 7711d3381ebSRandy Dunlap * @size: The size (in bytes) of the region. 7721d3381ebSRandy Dunlap * @replace_reg: mtrr index to be replaced; set to invalid value if none. 7731d3381ebSRandy Dunlap * 7741d3381ebSRandy Dunlap * Returns: The index of the region on success, else negative on error. 7752ec1df41SThomas Gleixner */ 776a1a499a3SJaswinder Singh Rajput int 777a1a499a3SJaswinder Singh Rajput generic_get_free_region(unsigned long base, unsigned long size, int replace_reg) 7782ec1df41SThomas Gleixner { 7792ec1df41SThomas Gleixner unsigned long lbase, lsize; 780a1a499a3SJaswinder Singh Rajput mtrr_type ltype; 781a1a499a3SJaswinder Singh Rajput int i, max; 7822ec1df41SThomas Gleixner 7832ec1df41SThomas Gleixner max = num_var_ranges; 7842ec1df41SThomas Gleixner if (replace_reg >= 0 && replace_reg < max) 7852ec1df41SThomas Gleixner return replace_reg; 786a1a499a3SJaswinder Singh Rajput 7872ec1df41SThomas Gleixner for (i = 0; i < max; ++i) { 7882ec1df41SThomas Gleixner mtrr_if->get(i, &lbase, &lsize, <ype); 7892ec1df41SThomas Gleixner if (lsize == 0) 7902ec1df41SThomas Gleixner return i; 7912ec1df41SThomas Gleixner } 792a1a499a3SJaswinder Singh Rajput 7932ec1df41SThomas Gleixner return -ENOSPC; 7942ec1df41SThomas Gleixner } 7952ec1df41SThomas Gleixner 7962ec1df41SThomas Gleixner static void generic_get_mtrr(unsigned int reg, unsigned long *base, 7972ec1df41SThomas Gleixner unsigned long *size, mtrr_type *type) 7982ec1df41SThomas Gleixner { 799d5c78673SYinghai Lu u32 mask_lo, mask_hi, base_lo, base_hi; 800d5c78673SYinghai Lu unsigned int hi; 801d5c78673SYinghai Lu u64 tmp, mask; 8022ec1df41SThomas Gleixner 8038ad97905SYinghai Lu /* 8048ad97905SYinghai Lu * get_mtrr doesn't need to update mtrr_state, also it could be called 8058ad97905SYinghai Lu * from any cpu, so try to print it out directly. 8068ad97905SYinghai Lu */ 807fa10ba64SAndi Kleen get_cpu(); 80863516ef6SYinghai Lu 8092ec1df41SThomas Gleixner rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi); 8108ad97905SYinghai Lu 811d053b481SJuergen Gross if (!(mask_lo & MTRR_PHYSMASK_V)) { 8122ec1df41SThomas Gleixner /* Invalid (i.e. free) range */ 8132ec1df41SThomas Gleixner *base = 0; 8142ec1df41SThomas Gleixner *size = 0; 8152ec1df41SThomas Gleixner *type = 0; 81663516ef6SYinghai Lu goto out_put_cpu; 8172ec1df41SThomas Gleixner } 8182ec1df41SThomas Gleixner 8192ec1df41SThomas Gleixner rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi); 8202ec1df41SThomas Gleixner 82163516ef6SYinghai Lu /* Work out the shifted address mask: */ 822d053b481SJuergen Gross tmp = (u64)mask_hi << 32 | (mask_lo & PAGE_MASK); 823d053b481SJuergen Gross mask = (u64)phys_hi_rsvd << 32 | tmp; 82463516ef6SYinghai Lu 82563516ef6SYinghai Lu /* Expand tmp with high bits to all 1s: */ 826d5c78673SYinghai Lu hi = fls64(tmp); 82738cc1c3dSYinghai Lu if (hi > 0) { 828d5c78673SYinghai Lu tmp |= ~((1ULL<<(hi - 1)) - 1); 82938cc1c3dSYinghai Lu 830d5c78673SYinghai Lu if (tmp != mask) { 8311b74dde7SChen Yucong pr_warn("mtrr: your BIOS has configured an incorrect mask, fixing it.\n"); 832373d4d09SRusty Russell add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); 833d5c78673SYinghai Lu mask = tmp; 83438cc1c3dSYinghai Lu } 83538cc1c3dSYinghai Lu } 8362ec1df41SThomas Gleixner 83763516ef6SYinghai Lu /* 83863516ef6SYinghai Lu * This works correctly if size is a power of two, i.e. a 83963516ef6SYinghai Lu * contiguous range: 84063516ef6SYinghai Lu */ 841d053b481SJuergen Gross *size = -mask >> PAGE_SHIFT; 842d5c78673SYinghai Lu *base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT; 843d053b481SJuergen Gross *type = base_lo & MTRR_PHYSBASE_TYPE; 8448ad97905SYinghai Lu 84563516ef6SYinghai Lu out_put_cpu: 84663516ef6SYinghai Lu put_cpu(); 8472ec1df41SThomas Gleixner } 8482ec1df41SThomas Gleixner 8492ec1df41SThomas Gleixner /** 850a1a499a3SJaswinder Singh Rajput * set_fixed_ranges - checks & updates the fixed-range MTRRs if they 851a1a499a3SJaswinder Singh Rajput * differ from the saved set 8521d3381ebSRandy Dunlap * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges() 8532ec1df41SThomas Gleixner */ 8542ec1df41SThomas Gleixner static int set_fixed_ranges(mtrr_type *frs) 8552ec1df41SThomas Gleixner { 8562ec1df41SThomas Gleixner unsigned long long *saved = (unsigned long long *)frs; 8572d2ee8deSPaul Jimenez bool changed = false; 8582ec1df41SThomas Gleixner int block = -1, range; 8592ec1df41SThomas Gleixner 8603ff42da5SAndreas Herrmann k8_check_syscfg_dram_mod_en(); 8613ff42da5SAndreas Herrmann 862a1a499a3SJaswinder Singh Rajput while (fixed_range_blocks[++block].ranges) { 8632ec1df41SThomas Gleixner for (range = 0; range < fixed_range_blocks[block].ranges; range++) 8642ec1df41SThomas Gleixner set_fixed_range(fixed_range_blocks[block].base_msr + range, 8652ec1df41SThomas Gleixner &changed, (unsigned int *)saved++); 866a1a499a3SJaswinder Singh Rajput } 8672ec1df41SThomas Gleixner 8682ec1df41SThomas Gleixner return changed; 8692ec1df41SThomas Gleixner } 8702ec1df41SThomas Gleixner 871a1a499a3SJaswinder Singh Rajput /* 872a1a499a3SJaswinder Singh Rajput * Set the MSR pair relating to a var range. 873a1a499a3SJaswinder Singh Rajput * Returns true if changes are made. 874a1a499a3SJaswinder Singh Rajput */ 8752d2ee8deSPaul Jimenez static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr) 8762ec1df41SThomas Gleixner { 8772ec1df41SThomas Gleixner unsigned int lo, hi; 8782d2ee8deSPaul Jimenez bool changed = false; 8792ec1df41SThomas Gleixner 8802ec1df41SThomas Gleixner rdmsr(MTRRphysBase_MSR(index), lo, hi); 881d053b481SJuergen Gross if ((vr->base_lo & ~MTRR_PHYSBASE_RSVD) != (lo & ~MTRR_PHYSBASE_RSVD) 882d053b481SJuergen Gross || (vr->base_hi & ~phys_hi_rsvd) != (hi & ~phys_hi_rsvd)) { 883a1a499a3SJaswinder Singh Rajput 8842ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi); 8852d2ee8deSPaul Jimenez changed = true; 8862ec1df41SThomas Gleixner } 8872ec1df41SThomas Gleixner 8882ec1df41SThomas Gleixner rdmsr(MTRRphysMask_MSR(index), lo, hi); 8892ec1df41SThomas Gleixner 890d053b481SJuergen Gross if ((vr->mask_lo & ~MTRR_PHYSMASK_RSVD) != (lo & ~MTRR_PHYSMASK_RSVD) 891d053b481SJuergen Gross || (vr->mask_hi & ~phys_hi_rsvd) != (hi & ~phys_hi_rsvd)) { 8922ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi); 8932d2ee8deSPaul Jimenez changed = true; 8942ec1df41SThomas Gleixner } 8952ec1df41SThomas Gleixner return changed; 8962ec1df41SThomas Gleixner } 8972ec1df41SThomas Gleixner 8982ec1df41SThomas Gleixner static u32 deftype_lo, deftype_hi; 8992ec1df41SThomas Gleixner 9001d3381ebSRandy Dunlap /** 9011d3381ebSRandy Dunlap * set_mtrr_state - Set the MTRR state for this CPU. 9021d3381ebSRandy Dunlap * 90301c97c73SJuergen Gross * NOTE: The CPU must already be in a safe state for MTRR changes, including 90401c97c73SJuergen Gross * measures that only a single CPU can be active in set_mtrr_state() in 90501c97c73SJuergen Gross * order to not be subject to races for usage of deftype_lo. This is 906d5f66d5dSJuergen Gross * accomplished by taking cache_disable_lock. 9071d3381ebSRandy Dunlap * RETURNS: 0 if no changes made, else a mask indicating what was changed. 9082ec1df41SThomas Gleixner */ 9091d3381ebSRandy Dunlap static unsigned long set_mtrr_state(void) 9102ec1df41SThomas Gleixner { 9112ec1df41SThomas Gleixner unsigned long change_mask = 0; 912a1a499a3SJaswinder Singh Rajput unsigned int i; 9132ec1df41SThomas Gleixner 914a1a499a3SJaswinder Singh Rajput for (i = 0; i < num_var_ranges; i++) { 9152ec1df41SThomas Gleixner if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i])) 9162ec1df41SThomas Gleixner change_mask |= MTRR_CHANGE_MASK_VARIABLE; 917a1a499a3SJaswinder Singh Rajput } 9182ec1df41SThomas Gleixner 9192ec1df41SThomas Gleixner if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges)) 9202ec1df41SThomas Gleixner change_mask |= MTRR_CHANGE_MASK_FIXED; 9212ec1df41SThomas Gleixner 922a1a499a3SJaswinder Singh Rajput /* 923a1a499a3SJaswinder Singh Rajput * Set_mtrr_restore restores the old value of MTRRdefType, 924a1a499a3SJaswinder Singh Rajput * so to set it we fiddle with the saved value: 925a1a499a3SJaswinder Singh Rajput */ 926d053b481SJuergen Gross if ((deftype_lo & MTRR_DEF_TYPE_TYPE) != mtrr_state.def_type || 927d053b481SJuergen Gross ((deftype_lo & MTRR_DEF_TYPE_ENABLE) >> MTRR_STATE_SHIFT) != mtrr_state.enabled) { 928a1a499a3SJaswinder Singh Rajput 929d053b481SJuergen Gross deftype_lo = (deftype_lo & MTRR_DEF_TYPE_DISABLE) | 930d053b481SJuergen Gross mtrr_state.def_type | 931d053b481SJuergen Gross (mtrr_state.enabled << MTRR_STATE_SHIFT); 9322ec1df41SThomas Gleixner change_mask |= MTRR_CHANGE_MASK_DEFTYPE; 9332ec1df41SThomas Gleixner } 9342ec1df41SThomas Gleixner 9352ec1df41SThomas Gleixner return change_mask; 9362ec1df41SThomas Gleixner } 9372ec1df41SThomas Gleixner 9384ad7149eSJuergen Gross void mtrr_disable(void) 9394ad7149eSJuergen Gross { 9404ad7149eSJuergen Gross /* Save MTRR state */ 9414ad7149eSJuergen Gross rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi); 9424ad7149eSJuergen Gross 9434ad7149eSJuergen Gross /* Disable MTRRs, and set the default type to uncached */ 944d053b481SJuergen Gross mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & MTRR_DEF_TYPE_DISABLE, deftype_hi); 9454ad7149eSJuergen Gross } 9464ad7149eSJuergen Gross 9474ad7149eSJuergen Gross void mtrr_enable(void) 9484ad7149eSJuergen Gross { 9494ad7149eSJuergen Gross /* Intel (P6) standard MTRRs */ 9504ad7149eSJuergen Gross mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi); 9514ad7149eSJuergen Gross } 9524ad7149eSJuergen Gross 9537d71db53SJuergen Gross void mtrr_generic_set_state(void) 9542ec1df41SThomas Gleixner { 9552ec1df41SThomas Gleixner unsigned long mask, count; 9562ec1df41SThomas Gleixner 9572ec1df41SThomas Gleixner /* Actually set the state */ 9582ec1df41SThomas Gleixner mask = set_mtrr_state(); 9592ec1df41SThomas Gleixner 9602ec1df41SThomas Gleixner /* Use the atomic bitops to update the global mask */ 9610e96f31eSJordan Borgner for (count = 0; count < sizeof(mask) * 8; ++count) { 9622ec1df41SThomas Gleixner if (mask & 0x01) 9632ec1df41SThomas Gleixner set_bit(count, &smp_changes_mask); 9642ec1df41SThomas Gleixner mask >>= 1; 9652ec1df41SThomas Gleixner } 9662ec1df41SThomas Gleixner } 9672ec1df41SThomas Gleixner 968a1a499a3SJaswinder Singh Rajput /** 969a1a499a3SJaswinder Singh Rajput * generic_set_mtrr - set variable MTRR register on the local CPU. 970a1a499a3SJaswinder Singh Rajput * 971a1a499a3SJaswinder Singh Rajput * @reg: The register to set. 972a1a499a3SJaswinder Singh Rajput * @base: The base address of the region. 973a1a499a3SJaswinder Singh Rajput * @size: The size of the region. If this is 0 the region is disabled. 974a1a499a3SJaswinder Singh Rajput * @type: The type of the region. 975a1a499a3SJaswinder Singh Rajput * 976a1a499a3SJaswinder Singh Rajput * Returns nothing. 977a1a499a3SJaswinder Singh Rajput */ 9782ec1df41SThomas Gleixner static void generic_set_mtrr(unsigned int reg, unsigned long base, 9792ec1df41SThomas Gleixner unsigned long size, mtrr_type type) 9802ec1df41SThomas Gleixner { 9812ec1df41SThomas Gleixner unsigned long flags; 9822ec1df41SThomas Gleixner struct mtrr_var_range *vr; 9832ec1df41SThomas Gleixner 9842ec1df41SThomas Gleixner vr = &mtrr_state.var_ranges[reg]; 9852ec1df41SThomas Gleixner 9862ec1df41SThomas Gleixner local_irq_save(flags); 987d5f66d5dSJuergen Gross cache_disable(); 9882ec1df41SThomas Gleixner 9892ec1df41SThomas Gleixner if (size == 0) { 990a1a499a3SJaswinder Singh Rajput /* 991a1a499a3SJaswinder Singh Rajput * The invalid bit is kept in the mask, so we simply 992a1a499a3SJaswinder Singh Rajput * clear the relevant mask register to disable a range. 993a1a499a3SJaswinder Singh Rajput */ 9942ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0); 9952ec1df41SThomas Gleixner memset(vr, 0, sizeof(struct mtrr_var_range)); 9962ec1df41SThomas Gleixner } else { 9972ec1df41SThomas Gleixner vr->base_lo = base << PAGE_SHIFT | type; 998d053b481SJuergen Gross vr->base_hi = (base >> (32 - PAGE_SHIFT)) & ~phys_hi_rsvd; 999d053b481SJuergen Gross vr->mask_lo = -size << PAGE_SHIFT | MTRR_PHYSMASK_V; 1000d053b481SJuergen Gross vr->mask_hi = (-size >> (32 - PAGE_SHIFT)) & ~phys_hi_rsvd; 10012ec1df41SThomas Gleixner 10022ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi); 10032ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi); 10042ec1df41SThomas Gleixner } 10052ec1df41SThomas Gleixner 1006d5f66d5dSJuergen Gross cache_enable(); 10072ec1df41SThomas Gleixner local_irq_restore(flags); 10082ec1df41SThomas Gleixner } 10092ec1df41SThomas Gleixner 1010a1a499a3SJaswinder Singh Rajput int generic_validate_add_page(unsigned long base, unsigned long size, 1011a1a499a3SJaswinder Singh Rajput unsigned int type) 10122ec1df41SThomas Gleixner { 10132ec1df41SThomas Gleixner unsigned long lbase, last; 10142ec1df41SThomas Gleixner 1015a1a499a3SJaswinder Singh Rajput /* 1016a1a499a3SJaswinder Singh Rajput * For Intel PPro stepping <= 7 1017a1a499a3SJaswinder Singh Rajput * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF 1018a1a499a3SJaswinder Singh Rajput */ 101903409069SJuergen Gross if (mtrr_if == &generic_mtrr_ops && boot_cpu_data.x86 == 6 && 10202ec1df41SThomas Gleixner boot_cpu_data.x86_model == 1 && 1021b399151cSJia Zhang boot_cpu_data.x86_stepping <= 7) { 10222ec1df41SThomas Gleixner if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) { 10231b74dde7SChen Yucong pr_warn("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base); 10242ec1df41SThomas Gleixner return -EINVAL; 10252ec1df41SThomas Gleixner } 10262ec1df41SThomas Gleixner if (!(base + size < 0x70000 || base > 0x7003F) && 10272ec1df41SThomas Gleixner (type == MTRR_TYPE_WRCOMB 10282ec1df41SThomas Gleixner || type == MTRR_TYPE_WRBACK)) { 10291b74dde7SChen Yucong pr_warn("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n"); 10302ec1df41SThomas Gleixner return -EINVAL; 10312ec1df41SThomas Gleixner } 10322ec1df41SThomas Gleixner } 10332ec1df41SThomas Gleixner 1034a1a499a3SJaswinder Singh Rajput /* 1035a1a499a3SJaswinder Singh Rajput * Check upper bits of base and last are equal and lower bits are 0 1036a1a499a3SJaswinder Singh Rajput * for base and 1 for last 1037a1a499a3SJaswinder Singh Rajput */ 10382ec1df41SThomas Gleixner last = base + size - 1; 10392ec1df41SThomas Gleixner for (lbase = base; !(lbase & 1) && (last & 1); 1040a1a499a3SJaswinder Singh Rajput lbase = lbase >> 1, last = last >> 1) 1041a1a499a3SJaswinder Singh Rajput ; 10422ec1df41SThomas Gleixner if (lbase != last) { 10431b74dde7SChen Yucong pr_warn("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size); 10442ec1df41SThomas Gleixner return -EINVAL; 10452ec1df41SThomas Gleixner } 10462ec1df41SThomas Gleixner return 0; 10472ec1df41SThomas Gleixner } 10482ec1df41SThomas Gleixner 10492ec1df41SThomas Gleixner static int generic_have_wrcomb(void) 10502ec1df41SThomas Gleixner { 10512ec1df41SThomas Gleixner unsigned long config, dummy; 1052d9bcc01dSJaswinder Singh Rajput rdmsr(MSR_MTRRcap, config, dummy); 1053d053b481SJuergen Gross return config & MTRR_CAP_WC; 10542ec1df41SThomas Gleixner } 10552ec1df41SThomas Gleixner 10562ec1df41SThomas Gleixner int positive_have_wrcomb(void) 10572ec1df41SThomas Gleixner { 10582ec1df41SThomas Gleixner return 1; 10592ec1df41SThomas Gleixner } 10602ec1df41SThomas Gleixner 1061a1a499a3SJaswinder Singh Rajput /* 1062a1a499a3SJaswinder Singh Rajput * Generic structure... 10632ec1df41SThomas Gleixner */ 10643b9cfc0aSEmese Revfy const struct mtrr_ops generic_mtrr_ops = { 10652ec1df41SThomas Gleixner .get = generic_get_mtrr, 10662ec1df41SThomas Gleixner .get_free_region = generic_get_free_region, 10672ec1df41SThomas Gleixner .set = generic_set_mtrr, 10682ec1df41SThomas Gleixner .validate_add_page = generic_validate_add_page, 10692ec1df41SThomas Gleixner .have_wrcomb = generic_have_wrcomb, 10702ec1df41SThomas Gleixner }; 1071