1a1a499a3SJaswinder Singh Rajput /* 2a1a499a3SJaswinder Singh Rajput * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong 3a1a499a3SJaswinder Singh Rajput * because MTRRs can span up to 40 bits (36bits on most modern x86) 4a1a499a3SJaswinder Singh Rajput */ 5a1a499a3SJaswinder Singh Rajput #define DEBUG 6a1a499a3SJaswinder Singh Rajput 7a1a499a3SJaswinder Singh Rajput #include <linux/module.h> 82ec1df41SThomas Gleixner #include <linux/init.h> 9a1a499a3SJaswinder Singh Rajput #include <linux/io.h> 102ec1df41SThomas Gleixner #include <linux/mm.h> 11a1a499a3SJaswinder Singh Rajput 12a1a499a3SJaswinder Singh Rajput #include <asm/processor-flags.h> 13a1a499a3SJaswinder Singh Rajput #include <asm/cpufeature.h> 14a1a499a3SJaswinder Singh Rajput #include <asm/tlbflush.h> 152ec1df41SThomas Gleixner #include <asm/mtrr.h> 162ec1df41SThomas Gleixner #include <asm/msr.h> 172e5d9c85Svenkatesh.pallipadi@intel.com #include <asm/pat.h> 18a1a499a3SJaswinder Singh Rajput 192ec1df41SThomas Gleixner #include "mtrr.h" 202ec1df41SThomas Gleixner 212ec1df41SThomas Gleixner struct fixed_range_block { 222ec1df41SThomas Gleixner int base_msr; /* start address of an MTRR block */ 232ec1df41SThomas Gleixner int ranges; /* number of MTRRs in this block */ 242ec1df41SThomas Gleixner }; 252ec1df41SThomas Gleixner 262ec1df41SThomas Gleixner static struct fixed_range_block fixed_range_blocks[] = { 27a036c7a3SJaswinder Singh Rajput { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */ 287d9d55e4SJaswinder Singh Rajput { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */ 29ba5673ffSJaswinder Singh Rajput { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */ 302ec1df41SThomas Gleixner {} 312ec1df41SThomas Gleixner }; 322ec1df41SThomas Gleixner 332ec1df41SThomas Gleixner static unsigned long smp_changes_mask; 342e5d9c85Svenkatesh.pallipadi@intel.com static int mtrr_state_set; 3595ffa243SYinghai Lu u64 mtrr_tom2; 362ec1df41SThomas Gleixner 37a1a499a3SJaswinder Singh Rajput struct mtrr_state_type mtrr_state; 38932d27a7SSheng Yang EXPORT_SYMBOL_GPL(mtrr_state); 39932d27a7SSheng Yang 40a1a499a3SJaswinder Singh Rajput /* 413ff42da5SAndreas Herrmann * BIOS is expected to clear MtrrFixDramModEn bit, see for example 423ff42da5SAndreas Herrmann * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD 433ff42da5SAndreas Herrmann * Opteron Processors" (26094 Rev. 3.30 February 2006), section 443ff42da5SAndreas Herrmann * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set 453ff42da5SAndreas Herrmann * to 1 during BIOS initalization of the fixed MTRRs, then cleared to 463ff42da5SAndreas Herrmann * 0 for operation." 473ff42da5SAndreas Herrmann */ 483ff42da5SAndreas Herrmann static inline void k8_check_syscfg_dram_mod_en(void) 493ff42da5SAndreas Herrmann { 503ff42da5SAndreas Herrmann u32 lo, hi; 513ff42da5SAndreas Herrmann 523ff42da5SAndreas Herrmann if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && 533ff42da5SAndreas Herrmann (boot_cpu_data.x86 >= 0x0f))) 543ff42da5SAndreas Herrmann return; 553ff42da5SAndreas Herrmann 563ff42da5SAndreas Herrmann rdmsr(MSR_K8_SYSCFG, lo, hi); 573ff42da5SAndreas Herrmann if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) { 583ff42da5SAndreas Herrmann printk(KERN_ERR FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]" 593ff42da5SAndreas Herrmann " not cleared by BIOS, clearing this bit\n", 603ff42da5SAndreas Herrmann smp_processor_id()); 613ff42da5SAndreas Herrmann lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY; 623ff42da5SAndreas Herrmann mtrr_wrmsr(MSR_K8_SYSCFG, lo, hi); 633ff42da5SAndreas Herrmann } 643ff42da5SAndreas Herrmann } 653ff42da5SAndreas Herrmann 66351e5a70SVenkatesh Pallipadi /* Get the size of contiguous MTRR range */ 67351e5a70SVenkatesh Pallipadi static u64 get_mtrr_size(u64 mask) 68351e5a70SVenkatesh Pallipadi { 69351e5a70SVenkatesh Pallipadi u64 size; 70351e5a70SVenkatesh Pallipadi 71351e5a70SVenkatesh Pallipadi mask >>= PAGE_SHIFT; 72351e5a70SVenkatesh Pallipadi mask |= size_or_mask; 73351e5a70SVenkatesh Pallipadi size = -mask; 74351e5a70SVenkatesh Pallipadi size <<= PAGE_SHIFT; 75351e5a70SVenkatesh Pallipadi return size; 76351e5a70SVenkatesh Pallipadi } 77351e5a70SVenkatesh Pallipadi 782e5d9c85Svenkatesh.pallipadi@intel.com /* 79a7f07cfbSVenkatesh Pallipadi * Check and return the effective type for MTRR-MTRR type overlap. 80a7f07cfbSVenkatesh Pallipadi * Returns 1 if the effective type is UNCACHEABLE, else returns 0 81a7f07cfbSVenkatesh Pallipadi */ 82a7f07cfbSVenkatesh Pallipadi static int check_type_overlap(u8 *prev, u8 *curr) 83a7f07cfbSVenkatesh Pallipadi { 84a7f07cfbSVenkatesh Pallipadi if (*prev == MTRR_TYPE_UNCACHABLE || *curr == MTRR_TYPE_UNCACHABLE) { 85a7f07cfbSVenkatesh Pallipadi *prev = MTRR_TYPE_UNCACHABLE; 86a7f07cfbSVenkatesh Pallipadi *curr = MTRR_TYPE_UNCACHABLE; 87a7f07cfbSVenkatesh Pallipadi return 1; 88a7f07cfbSVenkatesh Pallipadi } 89a7f07cfbSVenkatesh Pallipadi 90a7f07cfbSVenkatesh Pallipadi if ((*prev == MTRR_TYPE_WRBACK && *curr == MTRR_TYPE_WRTHROUGH) || 91a7f07cfbSVenkatesh Pallipadi (*prev == MTRR_TYPE_WRTHROUGH && *curr == MTRR_TYPE_WRBACK)) { 92a7f07cfbSVenkatesh Pallipadi *prev = MTRR_TYPE_WRTHROUGH; 93a7f07cfbSVenkatesh Pallipadi *curr = MTRR_TYPE_WRTHROUGH; 94a7f07cfbSVenkatesh Pallipadi } 95a7f07cfbSVenkatesh Pallipadi 96a7f07cfbSVenkatesh Pallipadi if (*prev != *curr) { 97a7f07cfbSVenkatesh Pallipadi *prev = MTRR_TYPE_UNCACHABLE; 98a7f07cfbSVenkatesh Pallipadi *curr = MTRR_TYPE_UNCACHABLE; 99a7f07cfbSVenkatesh Pallipadi return 1; 100a7f07cfbSVenkatesh Pallipadi } 101a7f07cfbSVenkatesh Pallipadi 102a7f07cfbSVenkatesh Pallipadi return 0; 103a7f07cfbSVenkatesh Pallipadi } 104a7f07cfbSVenkatesh Pallipadi 1050cc705f5SToshi Kani /** 1060cc705f5SToshi Kani * mtrr_type_lookup_fixed - look up memory type in MTRR fixed entries 1070cc705f5SToshi Kani * 1080cc705f5SToshi Kani * Return the MTRR fixed memory type of 'start'. 1090cc705f5SToshi Kani * 1100cc705f5SToshi Kani * MTRR fixed entries are divided into the following ways: 1110cc705f5SToshi Kani * 0x00000 - 0x7FFFF : This range is divided into eight 64KB sub-ranges 1120cc705f5SToshi Kani * 0x80000 - 0xBFFFF : This range is divided into sixteen 16KB sub-ranges 1130cc705f5SToshi Kani * 0xC0000 - 0xFFFFF : This range is divided into sixty-four 4KB sub-ranges 1140cc705f5SToshi Kani * 1150cc705f5SToshi Kani * Return Values: 1160cc705f5SToshi Kani * MTRR_TYPE_(type) - Matched memory type 1170cc705f5SToshi Kani * MTRR_TYPE_INVALID - Unmatched 1182e5d9c85Svenkatesh.pallipadi@intel.com */ 1190cc705f5SToshi Kani static u8 mtrr_type_lookup_fixed(u64 start, u64 end) 1200cc705f5SToshi Kani { 1210cc705f5SToshi Kani int idx; 1220cc705f5SToshi Kani 1230cc705f5SToshi Kani if (start >= 0x100000) 1240cc705f5SToshi Kani return MTRR_TYPE_INVALID; 1250cc705f5SToshi Kani 1260cc705f5SToshi Kani /* 0x0 - 0x7FFFF */ 1270cc705f5SToshi Kani if (start < 0x80000) { 1280cc705f5SToshi Kani idx = 0; 1290cc705f5SToshi Kani idx += (start >> 16); 1300cc705f5SToshi Kani return mtrr_state.fixed_ranges[idx]; 1310cc705f5SToshi Kani /* 0x80000 - 0xBFFFF */ 1320cc705f5SToshi Kani } else if (start < 0xC0000) { 1330cc705f5SToshi Kani idx = 1 * 8; 1340cc705f5SToshi Kani idx += ((start - 0x80000) >> 14); 1350cc705f5SToshi Kani return mtrr_state.fixed_ranges[idx]; 1360cc705f5SToshi Kani } 1370cc705f5SToshi Kani 1380cc705f5SToshi Kani /* 0xC0000 - 0xFFFFF */ 1390cc705f5SToshi Kani idx = 3 * 8; 1400cc705f5SToshi Kani idx += ((start - 0xC0000) >> 12); 1410cc705f5SToshi Kani return mtrr_state.fixed_ranges[idx]; 1420cc705f5SToshi Kani } 1430cc705f5SToshi Kani 1440cc705f5SToshi Kani /** 1450cc705f5SToshi Kani * mtrr_type_lookup_variable - look up memory type in MTRR variable entries 1460cc705f5SToshi Kani * 1470cc705f5SToshi Kani * Return Value: 1480cc705f5SToshi Kani * MTRR_TYPE_(type) - Matched memory type or default memory type (unmatched) 1490cc705f5SToshi Kani * 150b73522e0SToshi Kani * Output Arguments: 1510cc705f5SToshi Kani * repeat - Set to 1 when [start:end] spanned across MTRR range and type 1520cc705f5SToshi Kani * returned corresponds only to [start:*partial_end]. Caller has 1530cc705f5SToshi Kani * to lookup again for [*partial_end:end]. 154b73522e0SToshi Kani * 155b73522e0SToshi Kani * uniform - Set to 1 when an MTRR covers the region uniformly, i.e. the 156b73522e0SToshi Kani * region is fully covered by a single MTRR entry or the default 157b73522e0SToshi Kani * type. 1580cc705f5SToshi Kani */ 1590cc705f5SToshi Kani static u8 mtrr_type_lookup_variable(u64 start, u64 end, u64 *partial_end, 160b73522e0SToshi Kani int *repeat, u8 *uniform) 1612e5d9c85Svenkatesh.pallipadi@intel.com { 1622e5d9c85Svenkatesh.pallipadi@intel.com int i; 1632e5d9c85Svenkatesh.pallipadi@intel.com u64 base, mask; 1642e5d9c85Svenkatesh.pallipadi@intel.com u8 prev_match, curr_match; 1652e5d9c85Svenkatesh.pallipadi@intel.com 166351e5a70SVenkatesh Pallipadi *repeat = 0; 167b73522e0SToshi Kani *uniform = 1; 1682e5d9c85Svenkatesh.pallipadi@intel.com 1690cc705f5SToshi Kani /* Make end inclusive instead of exclusive */ 1702e5d9c85Svenkatesh.pallipadi@intel.com end--; 1712e5d9c85Svenkatesh.pallipadi@intel.com 1723d3ca416SToshi Kani prev_match = MTRR_TYPE_INVALID; 1732e5d9c85Svenkatesh.pallipadi@intel.com for (i = 0; i < num_var_ranges; ++i) { 1747f0431e3SToshi Kani unsigned short start_state, end_state, inclusive; 1752e5d9c85Svenkatesh.pallipadi@intel.com 1762e5d9c85Svenkatesh.pallipadi@intel.com if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11))) 1772e5d9c85Svenkatesh.pallipadi@intel.com continue; 1782e5d9c85Svenkatesh.pallipadi@intel.com 1792e5d9c85Svenkatesh.pallipadi@intel.com base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) + 1802e5d9c85Svenkatesh.pallipadi@intel.com (mtrr_state.var_ranges[i].base_lo & PAGE_MASK); 1812e5d9c85Svenkatesh.pallipadi@intel.com mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) + 1822e5d9c85Svenkatesh.pallipadi@intel.com (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK); 1832e5d9c85Svenkatesh.pallipadi@intel.com 1842e5d9c85Svenkatesh.pallipadi@intel.com start_state = ((start & mask) == (base & mask)); 1852e5d9c85Svenkatesh.pallipadi@intel.com end_state = ((end & mask) == (base & mask)); 1867f0431e3SToshi Kani inclusive = ((start < base) && (end > base)); 187351e5a70SVenkatesh Pallipadi 1887f0431e3SToshi Kani if ((start_state != end_state) || inclusive) { 189351e5a70SVenkatesh Pallipadi /* 190351e5a70SVenkatesh Pallipadi * We have start:end spanning across an MTRR. 1917f0431e3SToshi Kani * We split the region into either 1927f0431e3SToshi Kani * 1937f0431e3SToshi Kani * - start_state:1 194351e5a70SVenkatesh Pallipadi * (start:mtrr_end)(mtrr_end:end) 1957f0431e3SToshi Kani * - end_state:1 196351e5a70SVenkatesh Pallipadi * (start:mtrr_start)(mtrr_start:end) 1977f0431e3SToshi Kani * - inclusive:1 1987f0431e3SToshi Kani * (start:mtrr_start)(mtrr_start:mtrr_end)(mtrr_end:end) 1997f0431e3SToshi Kani * 200351e5a70SVenkatesh Pallipadi * depending on kind of overlap. 2017f0431e3SToshi Kani * 2027f0431e3SToshi Kani * Return the type of the first region and a pointer 2037f0431e3SToshi Kani * to the start of next region so that caller will be 2047f0431e3SToshi Kani * advised to lookup again after having adjusted start 2057f0431e3SToshi Kani * and end. 2067f0431e3SToshi Kani * 2070cc705f5SToshi Kani * Note: This way we handle overlaps with multiple 2080cc705f5SToshi Kani * entries and the default type properly. 209351e5a70SVenkatesh Pallipadi */ 210351e5a70SVenkatesh Pallipadi if (start_state) 211351e5a70SVenkatesh Pallipadi *partial_end = base + get_mtrr_size(mask); 212351e5a70SVenkatesh Pallipadi else 213351e5a70SVenkatesh Pallipadi *partial_end = base; 214351e5a70SVenkatesh Pallipadi 215351e5a70SVenkatesh Pallipadi if (unlikely(*partial_end <= start)) { 216351e5a70SVenkatesh Pallipadi WARN_ON(1); 217351e5a70SVenkatesh Pallipadi *partial_end = start + PAGE_SIZE; 218351e5a70SVenkatesh Pallipadi } 219351e5a70SVenkatesh Pallipadi 220351e5a70SVenkatesh Pallipadi end = *partial_end - 1; /* end is inclusive */ 221351e5a70SVenkatesh Pallipadi *repeat = 1; 222b73522e0SToshi Kani *uniform = 0; 223351e5a70SVenkatesh Pallipadi } 2242e5d9c85Svenkatesh.pallipadi@intel.com 225a1a499a3SJaswinder Singh Rajput if ((start & mask) != (base & mask)) 2262e5d9c85Svenkatesh.pallipadi@intel.com continue; 2272e5d9c85Svenkatesh.pallipadi@intel.com 2282e5d9c85Svenkatesh.pallipadi@intel.com curr_match = mtrr_state.var_ranges[i].base_lo & 0xff; 2293d3ca416SToshi Kani if (prev_match == MTRR_TYPE_INVALID) { 2302e5d9c85Svenkatesh.pallipadi@intel.com prev_match = curr_match; 2312e5d9c85Svenkatesh.pallipadi@intel.com continue; 2322e5d9c85Svenkatesh.pallipadi@intel.com } 2332e5d9c85Svenkatesh.pallipadi@intel.com 234b73522e0SToshi Kani *uniform = 0; 235a7f07cfbSVenkatesh Pallipadi if (check_type_overlap(&prev_match, &curr_match)) 236a7f07cfbSVenkatesh Pallipadi return curr_match; 2372e5d9c85Svenkatesh.pallipadi@intel.com } 2382e5d9c85Svenkatesh.pallipadi@intel.com 2393d3ca416SToshi Kani if (prev_match != MTRR_TYPE_INVALID) 2402e5d9c85Svenkatesh.pallipadi@intel.com return prev_match; 2412e5d9c85Svenkatesh.pallipadi@intel.com 2422e5d9c85Svenkatesh.pallipadi@intel.com return mtrr_state.def_type; 2432e5d9c85Svenkatesh.pallipadi@intel.com } 2442e5d9c85Svenkatesh.pallipadi@intel.com 2450cc705f5SToshi Kani /** 2460cc705f5SToshi Kani * mtrr_type_lookup - look up memory type in MTRR 2470cc705f5SToshi Kani * 2480cc705f5SToshi Kani * Return Values: 2490cc705f5SToshi Kani * MTRR_TYPE_(type) - The effective MTRR type for the region 2500cc705f5SToshi Kani * MTRR_TYPE_INVALID - MTRR is disabled 251b73522e0SToshi Kani * 252b73522e0SToshi Kani * Output Argument: 253b73522e0SToshi Kani * uniform - Set to 1 when an MTRR covers the region uniformly, i.e. the 254b73522e0SToshi Kani * region is fully covered by a single MTRR entry or the default 255b73522e0SToshi Kani * type. 256351e5a70SVenkatesh Pallipadi */ 257b73522e0SToshi Kani u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform) 258351e5a70SVenkatesh Pallipadi { 259b73522e0SToshi Kani u8 type, prev_type, is_uniform = 1, dummy; 260351e5a70SVenkatesh Pallipadi int repeat; 261351e5a70SVenkatesh Pallipadi u64 partial_end; 262351e5a70SVenkatesh Pallipadi 2630cc705f5SToshi Kani if (!mtrr_state_set) 2640cc705f5SToshi Kani return MTRR_TYPE_INVALID; 2650cc705f5SToshi Kani 2660cc705f5SToshi Kani if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) 2670cc705f5SToshi Kani return MTRR_TYPE_INVALID; 2680cc705f5SToshi Kani 2690cc705f5SToshi Kani /* 2700cc705f5SToshi Kani * Look up the fixed ranges first, which take priority over 2710cc705f5SToshi Kani * the variable ranges. 2720cc705f5SToshi Kani */ 2730cc705f5SToshi Kani if ((start < 0x100000) && 2740cc705f5SToshi Kani (mtrr_state.have_fixed) && 275b73522e0SToshi Kani (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) { 276b73522e0SToshi Kani is_uniform = 0; 277b73522e0SToshi Kani type = mtrr_type_lookup_fixed(start, end); 278b73522e0SToshi Kani goto out; 279b73522e0SToshi Kani } 2800cc705f5SToshi Kani 2810cc705f5SToshi Kani /* 2820cc705f5SToshi Kani * Look up the variable ranges. Look of multiple ranges matching 2830cc705f5SToshi Kani * this address and pick type as per MTRR precedence. 2840cc705f5SToshi Kani */ 285b73522e0SToshi Kani type = mtrr_type_lookup_variable(start, end, &partial_end, 286b73522e0SToshi Kani &repeat, &is_uniform); 287351e5a70SVenkatesh Pallipadi 288351e5a70SVenkatesh Pallipadi /* 289351e5a70SVenkatesh Pallipadi * Common path is with repeat = 0. 290351e5a70SVenkatesh Pallipadi * However, we can have cases where [start:end] spans across some 2910cc705f5SToshi Kani * MTRR ranges and/or the default type. Do repeated lookups for 2920cc705f5SToshi Kani * that case here. 293351e5a70SVenkatesh Pallipadi */ 294351e5a70SVenkatesh Pallipadi while (repeat) { 295351e5a70SVenkatesh Pallipadi prev_type = type; 296351e5a70SVenkatesh Pallipadi start = partial_end; 297b73522e0SToshi Kani is_uniform = 0; 298b73522e0SToshi Kani type = mtrr_type_lookup_variable(start, end, &partial_end, 299b73522e0SToshi Kani &repeat, &dummy); 300351e5a70SVenkatesh Pallipadi 301351e5a70SVenkatesh Pallipadi if (check_type_overlap(&prev_type, &type)) 302b73522e0SToshi Kani goto out; 303351e5a70SVenkatesh Pallipadi } 304351e5a70SVenkatesh Pallipadi 3050cc705f5SToshi Kani if (mtrr_tom2 && (start >= (1ULL<<32)) && (end < mtrr_tom2)) 306b73522e0SToshi Kani type = MTRR_TYPE_WRBACK; 3070cc705f5SToshi Kani 308b73522e0SToshi Kani out: 309b73522e0SToshi Kani *uniform = is_uniform; 310351e5a70SVenkatesh Pallipadi return type; 311351e5a70SVenkatesh Pallipadi } 312351e5a70SVenkatesh Pallipadi 3132ec1df41SThomas Gleixner /* Get the MSR pair relating to a var range */ 3142ec1df41SThomas Gleixner static void 3152ec1df41SThomas Gleixner get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr) 3162ec1df41SThomas Gleixner { 3172ec1df41SThomas Gleixner rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi); 3182ec1df41SThomas Gleixner rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi); 3192ec1df41SThomas Gleixner } 3202ec1df41SThomas Gleixner 321a1a499a3SJaswinder Singh Rajput /* Fill the MSR pair relating to a var range */ 32295ffa243SYinghai Lu void fill_mtrr_var_range(unsigned int index, 32395ffa243SYinghai Lu u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi) 32495ffa243SYinghai Lu { 32595ffa243SYinghai Lu struct mtrr_var_range *vr; 32695ffa243SYinghai Lu 32795ffa243SYinghai Lu vr = mtrr_state.var_ranges; 32895ffa243SYinghai Lu 32995ffa243SYinghai Lu vr[index].base_lo = base_lo; 33095ffa243SYinghai Lu vr[index].base_hi = base_hi; 33195ffa243SYinghai Lu vr[index].mask_lo = mask_lo; 33295ffa243SYinghai Lu vr[index].mask_hi = mask_hi; 33395ffa243SYinghai Lu } 33495ffa243SYinghai Lu 335a1a499a3SJaswinder Singh Rajput static void get_fixed_ranges(mtrr_type *frs) 3362ec1df41SThomas Gleixner { 3372ec1df41SThomas Gleixner unsigned int *p = (unsigned int *)frs; 3382ec1df41SThomas Gleixner int i; 3392ec1df41SThomas Gleixner 3403ff42da5SAndreas Herrmann k8_check_syscfg_dram_mod_en(); 3413ff42da5SAndreas Herrmann 342a036c7a3SJaswinder Singh Rajput rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]); 3432ec1df41SThomas Gleixner 3442ec1df41SThomas Gleixner for (i = 0; i < 2; i++) 3457d9d55e4SJaswinder Singh Rajput rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]); 3462ec1df41SThomas Gleixner for (i = 0; i < 8; i++) 347ba5673ffSJaswinder Singh Rajput rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]); 3482ec1df41SThomas Gleixner } 3492ec1df41SThomas Gleixner 3502ec1df41SThomas Gleixner void mtrr_save_fixed_ranges(void *info) 3512ec1df41SThomas Gleixner { 3522ec1df41SThomas Gleixner if (cpu_has_mtrr) 3532ec1df41SThomas Gleixner get_fixed_ranges(mtrr_state.fixed_ranges); 3542ec1df41SThomas Gleixner } 3552ec1df41SThomas Gleixner 356d4c90e37SYinghai Lu static unsigned __initdata last_fixed_start; 357d4c90e37SYinghai Lu static unsigned __initdata last_fixed_end; 358d4c90e37SYinghai Lu static mtrr_type __initdata last_fixed_type; 359d4c90e37SYinghai Lu 360d4c90e37SYinghai Lu static void __init print_fixed_last(void) 361d4c90e37SYinghai Lu { 362d4c90e37SYinghai Lu if (!last_fixed_end) 363d4c90e37SYinghai Lu return; 364d4c90e37SYinghai Lu 365a1a499a3SJaswinder Singh Rajput pr_debug(" %05X-%05X %s\n", last_fixed_start, 366d4c90e37SYinghai Lu last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type)); 367d4c90e37SYinghai Lu 368d4c90e37SYinghai Lu last_fixed_end = 0; 369d4c90e37SYinghai Lu } 370d4c90e37SYinghai Lu 371d4c90e37SYinghai Lu static void __init update_fixed_last(unsigned base, unsigned end, 372d4c90e37SYinghai Lu mtrr_type type) 373d4c90e37SYinghai Lu { 374d4c90e37SYinghai Lu last_fixed_start = base; 375d4c90e37SYinghai Lu last_fixed_end = end; 376d4c90e37SYinghai Lu last_fixed_type = type; 377d4c90e37SYinghai Lu } 378d4c90e37SYinghai Lu 379a1a499a3SJaswinder Singh Rajput static void __init 380a1a499a3SJaswinder Singh Rajput print_fixed(unsigned base, unsigned step, const mtrr_type *types) 3812ec1df41SThomas Gleixner { 3822ec1df41SThomas Gleixner unsigned i; 3832ec1df41SThomas Gleixner 384d4c90e37SYinghai Lu for (i = 0; i < 8; ++i, ++types, base += step) { 385d4c90e37SYinghai Lu if (last_fixed_end == 0) { 386d4c90e37SYinghai Lu update_fixed_last(base, base + step, *types); 387d4c90e37SYinghai Lu continue; 388d4c90e37SYinghai Lu } 389d4c90e37SYinghai Lu if (last_fixed_end == base && last_fixed_type == *types) { 390d4c90e37SYinghai Lu last_fixed_end = base + step; 391d4c90e37SYinghai Lu continue; 392d4c90e37SYinghai Lu } 393d4c90e37SYinghai Lu /* new segments: gap or different type */ 394d4c90e37SYinghai Lu print_fixed_last(); 395d4c90e37SYinghai Lu update_fixed_last(base, base + step, *types); 396d4c90e37SYinghai Lu } 3972ec1df41SThomas Gleixner } 3982ec1df41SThomas Gleixner 3992e5d9c85Svenkatesh.pallipadi@intel.com static void prepare_set(void); 4002e5d9c85Svenkatesh.pallipadi@intel.com static void post_set(void); 4012e5d9c85Svenkatesh.pallipadi@intel.com 4028ad97905SYinghai Lu static void __init print_mtrr_state(void) 4038ad97905SYinghai Lu { 4048ad97905SYinghai Lu unsigned int i; 4058ad97905SYinghai Lu int high_width; 4068ad97905SYinghai Lu 407a1a499a3SJaswinder Singh Rajput pr_debug("MTRR default type: %s\n", 408d4c90e37SYinghai Lu mtrr_attrib_to_str(mtrr_state.def_type)); 4098ad97905SYinghai Lu if (mtrr_state.have_fixed) { 410a1a499a3SJaswinder Singh Rajput pr_debug("MTRR fixed ranges %sabled:\n", 4119b3aca62SToshi Kani ((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) && 4129b3aca62SToshi Kani (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ? 4139b3aca62SToshi Kani "en" : "dis"); 4148ad97905SYinghai Lu print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0); 4158ad97905SYinghai Lu for (i = 0; i < 2; ++i) 416a1a499a3SJaswinder Singh Rajput print_fixed(0x80000 + i * 0x20000, 0x04000, 417a1a499a3SJaswinder Singh Rajput mtrr_state.fixed_ranges + (i + 1) * 8); 4188ad97905SYinghai Lu for (i = 0; i < 8; ++i) 419a1a499a3SJaswinder Singh Rajput print_fixed(0xC0000 + i * 0x08000, 0x01000, 420a1a499a3SJaswinder Singh Rajput mtrr_state.fixed_ranges + (i + 3) * 8); 421d4c90e37SYinghai Lu 422d4c90e37SYinghai Lu /* tail */ 423d4c90e37SYinghai Lu print_fixed_last(); 4248ad97905SYinghai Lu } 425a1a499a3SJaswinder Singh Rajput pr_debug("MTRR variable ranges %sabled:\n", 4269b3aca62SToshi Kani mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis"); 427a7101d15SJan Beulich high_width = (__ffs64(size_or_mask) - (32 - PAGE_SHIFT) + 3) / 4; 428a1a499a3SJaswinder Singh Rajput 4298ad97905SYinghai Lu for (i = 0; i < num_var_ranges; ++i) { 4308ad97905SYinghai Lu if (mtrr_state.var_ranges[i].mask_lo & (1 << 11)) 431a1a499a3SJaswinder Singh Rajput pr_debug(" %u base %0*X%05X000 mask %0*X%05X000 %s\n", 4328ad97905SYinghai Lu i, 4338ad97905SYinghai Lu high_width, 4348ad97905SYinghai Lu mtrr_state.var_ranges[i].base_hi, 4358ad97905SYinghai Lu mtrr_state.var_ranges[i].base_lo >> 12, 4368ad97905SYinghai Lu high_width, 4378ad97905SYinghai Lu mtrr_state.var_ranges[i].mask_hi, 4388ad97905SYinghai Lu mtrr_state.var_ranges[i].mask_lo >> 12, 4398ad97905SYinghai Lu mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff)); 4408ad97905SYinghai Lu else 441a1a499a3SJaswinder Singh Rajput pr_debug(" %u disabled\n", i); 4428ad97905SYinghai Lu } 443a1a499a3SJaswinder Singh Rajput if (mtrr_tom2) 444a1a499a3SJaswinder Singh Rajput pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20); 4458ad97905SYinghai Lu } 4468ad97905SYinghai Lu 4472ec1df41SThomas Gleixner /* Grab all of the MTRR state for this CPU into *state */ 448*f9626104SLuis R. Rodriguez bool __init get_mtrr_state(void) 4492ec1df41SThomas Gleixner { 4502ec1df41SThomas Gleixner struct mtrr_var_range *vrs; 4512e5d9c85Svenkatesh.pallipadi@intel.com unsigned long flags; 452a1a499a3SJaswinder Singh Rajput unsigned lo, dummy; 453a1a499a3SJaswinder Singh Rajput unsigned int i; 4542ec1df41SThomas Gleixner 4552ec1df41SThomas Gleixner vrs = mtrr_state.var_ranges; 4562ec1df41SThomas Gleixner 457d9bcc01dSJaswinder Singh Rajput rdmsr(MSR_MTRRcap, lo, dummy); 4582ec1df41SThomas Gleixner mtrr_state.have_fixed = (lo >> 8) & 1; 4592ec1df41SThomas Gleixner 4602ec1df41SThomas Gleixner for (i = 0; i < num_var_ranges; i++) 4612ec1df41SThomas Gleixner get_mtrr_var_range(i, &vrs[i]); 4622ec1df41SThomas Gleixner if (mtrr_state.have_fixed) 4632ec1df41SThomas Gleixner get_fixed_ranges(mtrr_state.fixed_ranges); 4642ec1df41SThomas Gleixner 46552650257SJaswinder Singh Rajput rdmsr(MSR_MTRRdefType, lo, dummy); 4662ec1df41SThomas Gleixner mtrr_state.def_type = (lo & 0xff); 4672ec1df41SThomas Gleixner mtrr_state.enabled = (lo & 0xc00) >> 10; 4682ec1df41SThomas Gleixner 46935605a10SYinghai Lu if (amd_special_default_mtrr()) { 4700da72a4aSThomas Gleixner unsigned low, high; 471a1a499a3SJaswinder Singh Rajput 47235605a10SYinghai Lu /* TOP_MEM2 */ 4730da72a4aSThomas Gleixner rdmsr(MSR_K8_TOP_MEM2, low, high); 47495ffa243SYinghai Lu mtrr_tom2 = high; 47595ffa243SYinghai Lu mtrr_tom2 <<= 32; 47695ffa243SYinghai Lu mtrr_tom2 |= low; 4778004dd96SYinghai Lu mtrr_tom2 &= 0xffffff800000ULL; 47835605a10SYinghai Lu } 4792ec1df41SThomas Gleixner 4808ad97905SYinghai Lu print_mtrr_state(); 4818ad97905SYinghai Lu 4822e5d9c85Svenkatesh.pallipadi@intel.com mtrr_state_set = 1; 4832e5d9c85Svenkatesh.pallipadi@intel.com 4842e5d9c85Svenkatesh.pallipadi@intel.com /* PAT setup for BP. We need to go through sync steps here */ 4852e5d9c85Svenkatesh.pallipadi@intel.com local_irq_save(flags); 4862e5d9c85Svenkatesh.pallipadi@intel.com prepare_set(); 4872e5d9c85Svenkatesh.pallipadi@intel.com 4882e5d9c85Svenkatesh.pallipadi@intel.com pat_init(); 4892e5d9c85Svenkatesh.pallipadi@intel.com 4902e5d9c85Svenkatesh.pallipadi@intel.com post_set(); 4912e5d9c85Svenkatesh.pallipadi@intel.com local_irq_restore(flags); 492*f9626104SLuis R. Rodriguez 493*f9626104SLuis R. Rodriguez return !!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED); 4942ec1df41SThomas Gleixner } 4952ec1df41SThomas Gleixner 496a1a499a3SJaswinder Singh Rajput /* Some BIOS's are messed up and don't set all MTRRs the same! */ 4972ec1df41SThomas Gleixner void __init mtrr_state_warn(void) 4982ec1df41SThomas Gleixner { 4992ec1df41SThomas Gleixner unsigned long mask = smp_changes_mask; 5002ec1df41SThomas Gleixner 5012ec1df41SThomas Gleixner if (!mask) 5022ec1df41SThomas Gleixner return; 5032ec1df41SThomas Gleixner if (mask & MTRR_CHANGE_MASK_FIXED) 504a1a499a3SJaswinder Singh Rajput pr_warning("mtrr: your CPUs had inconsistent fixed MTRR settings\n"); 5052ec1df41SThomas Gleixner if (mask & MTRR_CHANGE_MASK_VARIABLE) 506a1a499a3SJaswinder Singh Rajput pr_warning("mtrr: your CPUs had inconsistent variable MTRR settings\n"); 5072ec1df41SThomas Gleixner if (mask & MTRR_CHANGE_MASK_DEFTYPE) 508a1a499a3SJaswinder Singh Rajput pr_warning("mtrr: your CPUs had inconsistent MTRRdefType settings\n"); 509a1a499a3SJaswinder Singh Rajput 5102ec1df41SThomas Gleixner printk(KERN_INFO "mtrr: probably your BIOS does not setup all CPUs.\n"); 5112ec1df41SThomas Gleixner printk(KERN_INFO "mtrr: corrected configuration.\n"); 5122ec1df41SThomas Gleixner } 5132ec1df41SThomas Gleixner 514a1a499a3SJaswinder Singh Rajput /* 515a1a499a3SJaswinder Singh Rajput * Doesn't attempt to pass an error out to MTRR users 516a1a499a3SJaswinder Singh Rajput * because it's quite complicated in some cases and probably not 517a1a499a3SJaswinder Singh Rajput * worth it because the best error handling is to ignore it. 518a1a499a3SJaswinder Singh Rajput */ 5192ec1df41SThomas Gleixner void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b) 5202ec1df41SThomas Gleixner { 521a1a499a3SJaswinder Singh Rajput if (wrmsr_safe(msr, a, b) < 0) { 5222ec1df41SThomas Gleixner printk(KERN_ERR 5232ec1df41SThomas Gleixner "MTRR: CPU %u: Writing MSR %x to %x:%x failed\n", 5242ec1df41SThomas Gleixner smp_processor_id(), msr, a, b); 5252ec1df41SThomas Gleixner } 526a1a499a3SJaswinder Singh Rajput } 5272ec1df41SThomas Gleixner 5282ec1df41SThomas Gleixner /** 529a1a499a3SJaswinder Singh Rajput * set_fixed_range - checks & updates a fixed-range MTRR if it 530a1a499a3SJaswinder Singh Rajput * differs from the value it should have 5311d3381ebSRandy Dunlap * @msr: MSR address of the MTTR which should be checked and updated 5321d3381ebSRandy Dunlap * @changed: pointer which indicates whether the MTRR needed to be changed 5331d3381ebSRandy Dunlap * @msrwords: pointer to the MSR values which the MSR should have 5342ec1df41SThomas Gleixner */ 5352d2ee8deSPaul Jimenez static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords) 5362ec1df41SThomas Gleixner { 5372ec1df41SThomas Gleixner unsigned lo, hi; 5382ec1df41SThomas Gleixner 5392ec1df41SThomas Gleixner rdmsr(msr, lo, hi); 5402ec1df41SThomas Gleixner 5412ec1df41SThomas Gleixner if (lo != msrwords[0] || hi != msrwords[1]) { 5422ec1df41SThomas Gleixner mtrr_wrmsr(msr, msrwords[0], msrwords[1]); 5432d2ee8deSPaul Jimenez *changed = true; 5442ec1df41SThomas Gleixner } 5452ec1df41SThomas Gleixner } 5462ec1df41SThomas Gleixner 5471d3381ebSRandy Dunlap /** 5481d3381ebSRandy Dunlap * generic_get_free_region - Get a free MTRR. 5491d3381ebSRandy Dunlap * @base: The starting (base) address of the region. 5501d3381ebSRandy Dunlap * @size: The size (in bytes) of the region. 5511d3381ebSRandy Dunlap * @replace_reg: mtrr index to be replaced; set to invalid value if none. 5521d3381ebSRandy Dunlap * 5531d3381ebSRandy Dunlap * Returns: The index of the region on success, else negative on error. 5542ec1df41SThomas Gleixner */ 555a1a499a3SJaswinder Singh Rajput int 556a1a499a3SJaswinder Singh Rajput generic_get_free_region(unsigned long base, unsigned long size, int replace_reg) 5572ec1df41SThomas Gleixner { 5582ec1df41SThomas Gleixner unsigned long lbase, lsize; 559a1a499a3SJaswinder Singh Rajput mtrr_type ltype; 560a1a499a3SJaswinder Singh Rajput int i, max; 5612ec1df41SThomas Gleixner 5622ec1df41SThomas Gleixner max = num_var_ranges; 5632ec1df41SThomas Gleixner if (replace_reg >= 0 && replace_reg < max) 5642ec1df41SThomas Gleixner return replace_reg; 565a1a499a3SJaswinder Singh Rajput 5662ec1df41SThomas Gleixner for (i = 0; i < max; ++i) { 5672ec1df41SThomas Gleixner mtrr_if->get(i, &lbase, &lsize, <ype); 5682ec1df41SThomas Gleixner if (lsize == 0) 5692ec1df41SThomas Gleixner return i; 5702ec1df41SThomas Gleixner } 571a1a499a3SJaswinder Singh Rajput 5722ec1df41SThomas Gleixner return -ENOSPC; 5732ec1df41SThomas Gleixner } 5742ec1df41SThomas Gleixner 5752ec1df41SThomas Gleixner static void generic_get_mtrr(unsigned int reg, unsigned long *base, 5762ec1df41SThomas Gleixner unsigned long *size, mtrr_type *type) 5772ec1df41SThomas Gleixner { 578d5c78673SYinghai Lu u32 mask_lo, mask_hi, base_lo, base_hi; 579d5c78673SYinghai Lu unsigned int hi; 580d5c78673SYinghai Lu u64 tmp, mask; 5812ec1df41SThomas Gleixner 5828ad97905SYinghai Lu /* 5838ad97905SYinghai Lu * get_mtrr doesn't need to update mtrr_state, also it could be called 5848ad97905SYinghai Lu * from any cpu, so try to print it out directly. 5858ad97905SYinghai Lu */ 586fa10ba64SAndi Kleen get_cpu(); 58763516ef6SYinghai Lu 5882ec1df41SThomas Gleixner rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi); 5898ad97905SYinghai Lu 5902ec1df41SThomas Gleixner if ((mask_lo & 0x800) == 0) { 5912ec1df41SThomas Gleixner /* Invalid (i.e. free) range */ 5922ec1df41SThomas Gleixner *base = 0; 5932ec1df41SThomas Gleixner *size = 0; 5942ec1df41SThomas Gleixner *type = 0; 59563516ef6SYinghai Lu goto out_put_cpu; 5962ec1df41SThomas Gleixner } 5972ec1df41SThomas Gleixner 5982ec1df41SThomas Gleixner rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi); 5992ec1df41SThomas Gleixner 60063516ef6SYinghai Lu /* Work out the shifted address mask: */ 601d5c78673SYinghai Lu tmp = (u64)mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT; 602d5c78673SYinghai Lu mask = size_or_mask | tmp; 60363516ef6SYinghai Lu 60463516ef6SYinghai Lu /* Expand tmp with high bits to all 1s: */ 605d5c78673SYinghai Lu hi = fls64(tmp); 60638cc1c3dSYinghai Lu if (hi > 0) { 607d5c78673SYinghai Lu tmp |= ~((1ULL<<(hi - 1)) - 1); 60838cc1c3dSYinghai Lu 609d5c78673SYinghai Lu if (tmp != mask) { 610942fa3b6SAlan Cox printk(KERN_WARNING "mtrr: your BIOS has configured an incorrect mask, fixing it.\n"); 611373d4d09SRusty Russell add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); 612d5c78673SYinghai Lu mask = tmp; 61338cc1c3dSYinghai Lu } 61438cc1c3dSYinghai Lu } 6152ec1df41SThomas Gleixner 61663516ef6SYinghai Lu /* 61763516ef6SYinghai Lu * This works correctly if size is a power of two, i.e. a 61863516ef6SYinghai Lu * contiguous range: 61963516ef6SYinghai Lu */ 620d5c78673SYinghai Lu *size = -mask; 621d5c78673SYinghai Lu *base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT; 6222ec1df41SThomas Gleixner *type = base_lo & 0xff; 6238ad97905SYinghai Lu 62463516ef6SYinghai Lu out_put_cpu: 62563516ef6SYinghai Lu put_cpu(); 6262ec1df41SThomas Gleixner } 6272ec1df41SThomas Gleixner 6282ec1df41SThomas Gleixner /** 629a1a499a3SJaswinder Singh Rajput * set_fixed_ranges - checks & updates the fixed-range MTRRs if they 630a1a499a3SJaswinder Singh Rajput * differ from the saved set 6311d3381ebSRandy Dunlap * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges() 6322ec1df41SThomas Gleixner */ 6332ec1df41SThomas Gleixner static int set_fixed_ranges(mtrr_type *frs) 6342ec1df41SThomas Gleixner { 6352ec1df41SThomas Gleixner unsigned long long *saved = (unsigned long long *)frs; 6362d2ee8deSPaul Jimenez bool changed = false; 6372ec1df41SThomas Gleixner int block = -1, range; 6382ec1df41SThomas Gleixner 6393ff42da5SAndreas Herrmann k8_check_syscfg_dram_mod_en(); 6403ff42da5SAndreas Herrmann 641a1a499a3SJaswinder Singh Rajput while (fixed_range_blocks[++block].ranges) { 6422ec1df41SThomas Gleixner for (range = 0; range < fixed_range_blocks[block].ranges; range++) 6432ec1df41SThomas Gleixner set_fixed_range(fixed_range_blocks[block].base_msr + range, 6442ec1df41SThomas Gleixner &changed, (unsigned int *)saved++); 645a1a499a3SJaswinder Singh Rajput } 6462ec1df41SThomas Gleixner 6472ec1df41SThomas Gleixner return changed; 6482ec1df41SThomas Gleixner } 6492ec1df41SThomas Gleixner 650a1a499a3SJaswinder Singh Rajput /* 651a1a499a3SJaswinder Singh Rajput * Set the MSR pair relating to a var range. 652a1a499a3SJaswinder Singh Rajput * Returns true if changes are made. 653a1a499a3SJaswinder Singh Rajput */ 6542d2ee8deSPaul Jimenez static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr) 6552ec1df41SThomas Gleixner { 6562ec1df41SThomas Gleixner unsigned int lo, hi; 6572d2ee8deSPaul Jimenez bool changed = false; 6582ec1df41SThomas Gleixner 6592ec1df41SThomas Gleixner rdmsr(MTRRphysBase_MSR(index), lo, hi); 6602ec1df41SThomas Gleixner if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL) 6612ec1df41SThomas Gleixner || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) != 6622ec1df41SThomas Gleixner (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) { 663a1a499a3SJaswinder Singh Rajput 6642ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi); 6652d2ee8deSPaul Jimenez changed = true; 6662ec1df41SThomas Gleixner } 6672ec1df41SThomas Gleixner 6682ec1df41SThomas Gleixner rdmsr(MTRRphysMask_MSR(index), lo, hi); 6692ec1df41SThomas Gleixner 6702ec1df41SThomas Gleixner if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL) 6712ec1df41SThomas Gleixner || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) != 6722ec1df41SThomas Gleixner (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) { 6732ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi); 6742d2ee8deSPaul Jimenez changed = true; 6752ec1df41SThomas Gleixner } 6762ec1df41SThomas Gleixner return changed; 6772ec1df41SThomas Gleixner } 6782ec1df41SThomas Gleixner 6792ec1df41SThomas Gleixner static u32 deftype_lo, deftype_hi; 6802ec1df41SThomas Gleixner 6811d3381ebSRandy Dunlap /** 6821d3381ebSRandy Dunlap * set_mtrr_state - Set the MTRR state for this CPU. 6831d3381ebSRandy Dunlap * 6841d3381ebSRandy Dunlap * NOTE: The CPU must already be in a safe state for MTRR changes. 6851d3381ebSRandy Dunlap * RETURNS: 0 if no changes made, else a mask indicating what was changed. 6862ec1df41SThomas Gleixner */ 6871d3381ebSRandy Dunlap static unsigned long set_mtrr_state(void) 6882ec1df41SThomas Gleixner { 6892ec1df41SThomas Gleixner unsigned long change_mask = 0; 690a1a499a3SJaswinder Singh Rajput unsigned int i; 6912ec1df41SThomas Gleixner 692a1a499a3SJaswinder Singh Rajput for (i = 0; i < num_var_ranges; i++) { 6932ec1df41SThomas Gleixner if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i])) 6942ec1df41SThomas Gleixner change_mask |= MTRR_CHANGE_MASK_VARIABLE; 695a1a499a3SJaswinder Singh Rajput } 6962ec1df41SThomas Gleixner 6972ec1df41SThomas Gleixner if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges)) 6982ec1df41SThomas Gleixner change_mask |= MTRR_CHANGE_MASK_FIXED; 6992ec1df41SThomas Gleixner 700a1a499a3SJaswinder Singh Rajput /* 701a1a499a3SJaswinder Singh Rajput * Set_mtrr_restore restores the old value of MTRRdefType, 702a1a499a3SJaswinder Singh Rajput * so to set it we fiddle with the saved value: 703a1a499a3SJaswinder Singh Rajput */ 7042ec1df41SThomas Gleixner if ((deftype_lo & 0xff) != mtrr_state.def_type 7052ec1df41SThomas Gleixner || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) { 706a1a499a3SJaswinder Singh Rajput 707a1a499a3SJaswinder Singh Rajput deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type | 708a1a499a3SJaswinder Singh Rajput (mtrr_state.enabled << 10); 7092ec1df41SThomas Gleixner change_mask |= MTRR_CHANGE_MASK_DEFTYPE; 7102ec1df41SThomas Gleixner } 7112ec1df41SThomas Gleixner 7122ec1df41SThomas Gleixner return change_mask; 7132ec1df41SThomas Gleixner } 7142ec1df41SThomas Gleixner 7152ec1df41SThomas Gleixner 716a1a499a3SJaswinder Singh Rajput static unsigned long cr4; 71740d6753eSThomas Gleixner static DEFINE_RAW_SPINLOCK(set_atomicity_lock); 7182ec1df41SThomas Gleixner 7192ec1df41SThomas Gleixner /* 720a1a499a3SJaswinder Singh Rajput * Since we are disabling the cache don't allow any interrupts, 721a1a499a3SJaswinder Singh Rajput * they would run extremely slow and would only increase the pain. 722a1a499a3SJaswinder Singh Rajput * 723a1a499a3SJaswinder Singh Rajput * The caller must ensure that local interrupts are disabled and 724a1a499a3SJaswinder Singh Rajput * are reenabled after post_set() has been called. 7252ec1df41SThomas Gleixner */ 7262ec1df41SThomas Gleixner static void prepare_set(void) __acquires(set_atomicity_lock) 7272ec1df41SThomas Gleixner { 7282ec1df41SThomas Gleixner unsigned long cr0; 7292ec1df41SThomas Gleixner 730a1a499a3SJaswinder Singh Rajput /* 731a1a499a3SJaswinder Singh Rajput * Note that this is not ideal 732a1a499a3SJaswinder Singh Rajput * since the cache is only flushed/disabled for this CPU while the 733a1a499a3SJaswinder Singh Rajput * MTRRs are changed, but changing this requires more invasive 734a1a499a3SJaswinder Singh Rajput * changes to the way the kernel boots 735a1a499a3SJaswinder Singh Rajput */ 7362ec1df41SThomas Gleixner 73740d6753eSThomas Gleixner raw_spin_lock(&set_atomicity_lock); 7382ec1df41SThomas Gleixner 7392ec1df41SThomas Gleixner /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */ 7407ebad705SDave Jones cr0 = read_cr0() | X86_CR0_CD; 7412ec1df41SThomas Gleixner write_cr0(cr0); 7422ec1df41SThomas Gleixner wbinvd(); 7432ec1df41SThomas Gleixner 7442ec1df41SThomas Gleixner /* Save value of CR4 and clear Page Global Enable (bit 7) */ 7452ec1df41SThomas Gleixner if (cpu_has_pge) { 7461e02ce4cSAndy Lutomirski cr4 = __read_cr4(); 7471e02ce4cSAndy Lutomirski __write_cr4(cr4 & ~X86_CR4_PGE); 7482ec1df41SThomas Gleixner } 7492ec1df41SThomas Gleixner 7502ec1df41SThomas Gleixner /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */ 751ec659934SMel Gorman count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL); 7522ec1df41SThomas Gleixner __flush_tlb(); 7532ec1df41SThomas Gleixner 7542ec1df41SThomas Gleixner /* Save MTRR state */ 75552650257SJaswinder Singh Rajput rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi); 7562ec1df41SThomas Gleixner 7572ec1df41SThomas Gleixner /* Disable MTRRs, and set the default type to uncached */ 75852650257SJaswinder Singh Rajput mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi); 7598dbf4a30SAjaykumar Hotchandani wbinvd(); 7602ec1df41SThomas Gleixner } 7612ec1df41SThomas Gleixner 7622ec1df41SThomas Gleixner static void post_set(void) __releases(set_atomicity_lock) 7632ec1df41SThomas Gleixner { 7642ec1df41SThomas Gleixner /* Flush TLBs (no need to flush caches - they are disabled) */ 765ec659934SMel Gorman count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL); 7662ec1df41SThomas Gleixner __flush_tlb(); 7672ec1df41SThomas Gleixner 7682ec1df41SThomas Gleixner /* Intel (P6) standard MTRRs */ 76952650257SJaswinder Singh Rajput mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi); 7702ec1df41SThomas Gleixner 7712ec1df41SThomas Gleixner /* Enable caches */ 772a3d7b7ddSH. Peter Anvin write_cr0(read_cr0() & ~X86_CR0_CD); 7732ec1df41SThomas Gleixner 7742ec1df41SThomas Gleixner /* Restore value of CR4 */ 7752ec1df41SThomas Gleixner if (cpu_has_pge) 7761e02ce4cSAndy Lutomirski __write_cr4(cr4); 77740d6753eSThomas Gleixner raw_spin_unlock(&set_atomicity_lock); 7782ec1df41SThomas Gleixner } 7792ec1df41SThomas Gleixner 7802ec1df41SThomas Gleixner static void generic_set_all(void) 7812ec1df41SThomas Gleixner { 7822ec1df41SThomas Gleixner unsigned long mask, count; 7832ec1df41SThomas Gleixner unsigned long flags; 7842ec1df41SThomas Gleixner 7852ec1df41SThomas Gleixner local_irq_save(flags); 7862ec1df41SThomas Gleixner prepare_set(); 7872ec1df41SThomas Gleixner 7882ec1df41SThomas Gleixner /* Actually set the state */ 7892ec1df41SThomas Gleixner mask = set_mtrr_state(); 7902ec1df41SThomas Gleixner 7912e5d9c85Svenkatesh.pallipadi@intel.com /* also set PAT */ 7922e5d9c85Svenkatesh.pallipadi@intel.com pat_init(); 7932e5d9c85Svenkatesh.pallipadi@intel.com 7942ec1df41SThomas Gleixner post_set(); 7952ec1df41SThomas Gleixner local_irq_restore(flags); 7962ec1df41SThomas Gleixner 7972ec1df41SThomas Gleixner /* Use the atomic bitops to update the global mask */ 7982ec1df41SThomas Gleixner for (count = 0; count < sizeof mask * 8; ++count) { 7992ec1df41SThomas Gleixner if (mask & 0x01) 8002ec1df41SThomas Gleixner set_bit(count, &smp_changes_mask); 8012ec1df41SThomas Gleixner mask >>= 1; 8022ec1df41SThomas Gleixner } 8032ec1df41SThomas Gleixner 8042ec1df41SThomas Gleixner } 8052ec1df41SThomas Gleixner 806a1a499a3SJaswinder Singh Rajput /** 807a1a499a3SJaswinder Singh Rajput * generic_set_mtrr - set variable MTRR register on the local CPU. 808a1a499a3SJaswinder Singh Rajput * 809a1a499a3SJaswinder Singh Rajput * @reg: The register to set. 810a1a499a3SJaswinder Singh Rajput * @base: The base address of the region. 811a1a499a3SJaswinder Singh Rajput * @size: The size of the region. If this is 0 the region is disabled. 812a1a499a3SJaswinder Singh Rajput * @type: The type of the region. 813a1a499a3SJaswinder Singh Rajput * 814a1a499a3SJaswinder Singh Rajput * Returns nothing. 815a1a499a3SJaswinder Singh Rajput */ 8162ec1df41SThomas Gleixner static void generic_set_mtrr(unsigned int reg, unsigned long base, 8172ec1df41SThomas Gleixner unsigned long size, mtrr_type type) 8182ec1df41SThomas Gleixner { 8192ec1df41SThomas Gleixner unsigned long flags; 8202ec1df41SThomas Gleixner struct mtrr_var_range *vr; 8212ec1df41SThomas Gleixner 8222ec1df41SThomas Gleixner vr = &mtrr_state.var_ranges[reg]; 8232ec1df41SThomas Gleixner 8242ec1df41SThomas Gleixner local_irq_save(flags); 8252ec1df41SThomas Gleixner prepare_set(); 8262ec1df41SThomas Gleixner 8272ec1df41SThomas Gleixner if (size == 0) { 828a1a499a3SJaswinder Singh Rajput /* 829a1a499a3SJaswinder Singh Rajput * The invalid bit is kept in the mask, so we simply 830a1a499a3SJaswinder Singh Rajput * clear the relevant mask register to disable a range. 831a1a499a3SJaswinder Singh Rajput */ 8322ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0); 8332ec1df41SThomas Gleixner memset(vr, 0, sizeof(struct mtrr_var_range)); 8342ec1df41SThomas Gleixner } else { 8352ec1df41SThomas Gleixner vr->base_lo = base << PAGE_SHIFT | type; 8362ec1df41SThomas Gleixner vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT); 8372ec1df41SThomas Gleixner vr->mask_lo = -size << PAGE_SHIFT | 0x800; 8382ec1df41SThomas Gleixner vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT); 8392ec1df41SThomas Gleixner 8402ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi); 8412ec1df41SThomas Gleixner mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi); 8422ec1df41SThomas Gleixner } 8432ec1df41SThomas Gleixner 8442ec1df41SThomas Gleixner post_set(); 8452ec1df41SThomas Gleixner local_irq_restore(flags); 8462ec1df41SThomas Gleixner } 8472ec1df41SThomas Gleixner 848a1a499a3SJaswinder Singh Rajput int generic_validate_add_page(unsigned long base, unsigned long size, 849a1a499a3SJaswinder Singh Rajput unsigned int type) 8502ec1df41SThomas Gleixner { 8512ec1df41SThomas Gleixner unsigned long lbase, last; 8522ec1df41SThomas Gleixner 853a1a499a3SJaswinder Singh Rajput /* 854a1a499a3SJaswinder Singh Rajput * For Intel PPro stepping <= 7 855a1a499a3SJaswinder Singh Rajput * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF 856a1a499a3SJaswinder Singh Rajput */ 8572ec1df41SThomas Gleixner if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 && 8582ec1df41SThomas Gleixner boot_cpu_data.x86_model == 1 && 8592ec1df41SThomas Gleixner boot_cpu_data.x86_mask <= 7) { 8602ec1df41SThomas Gleixner if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) { 861a1a499a3SJaswinder Singh Rajput pr_warning("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base); 8622ec1df41SThomas Gleixner return -EINVAL; 8632ec1df41SThomas Gleixner } 8642ec1df41SThomas Gleixner if (!(base + size < 0x70000 || base > 0x7003F) && 8652ec1df41SThomas Gleixner (type == MTRR_TYPE_WRCOMB 8662ec1df41SThomas Gleixner || type == MTRR_TYPE_WRBACK)) { 867a1a499a3SJaswinder Singh Rajput pr_warning("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n"); 8682ec1df41SThomas Gleixner return -EINVAL; 8692ec1df41SThomas Gleixner } 8702ec1df41SThomas Gleixner } 8712ec1df41SThomas Gleixner 872a1a499a3SJaswinder Singh Rajput /* 873a1a499a3SJaswinder Singh Rajput * Check upper bits of base and last are equal and lower bits are 0 874a1a499a3SJaswinder Singh Rajput * for base and 1 for last 875a1a499a3SJaswinder Singh Rajput */ 8762ec1df41SThomas Gleixner last = base + size - 1; 8772ec1df41SThomas Gleixner for (lbase = base; !(lbase & 1) && (last & 1); 878a1a499a3SJaswinder Singh Rajput lbase = lbase >> 1, last = last >> 1) 879a1a499a3SJaswinder Singh Rajput ; 8802ec1df41SThomas Gleixner if (lbase != last) { 881a1a499a3SJaswinder Singh Rajput pr_warning("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size); 8822ec1df41SThomas Gleixner return -EINVAL; 8832ec1df41SThomas Gleixner } 8842ec1df41SThomas Gleixner return 0; 8852ec1df41SThomas Gleixner } 8862ec1df41SThomas Gleixner 8872ec1df41SThomas Gleixner static int generic_have_wrcomb(void) 8882ec1df41SThomas Gleixner { 8892ec1df41SThomas Gleixner unsigned long config, dummy; 890d9bcc01dSJaswinder Singh Rajput rdmsr(MSR_MTRRcap, config, dummy); 891a1a499a3SJaswinder Singh Rajput return config & (1 << 10); 8922ec1df41SThomas Gleixner } 8932ec1df41SThomas Gleixner 8942ec1df41SThomas Gleixner int positive_have_wrcomb(void) 8952ec1df41SThomas Gleixner { 8962ec1df41SThomas Gleixner return 1; 8972ec1df41SThomas Gleixner } 8982ec1df41SThomas Gleixner 899a1a499a3SJaswinder Singh Rajput /* 900a1a499a3SJaswinder Singh Rajput * Generic structure... 9012ec1df41SThomas Gleixner */ 9023b9cfc0aSEmese Revfy const struct mtrr_ops generic_mtrr_ops = { 9032ec1df41SThomas Gleixner .use_intel_if = 1, 9042ec1df41SThomas Gleixner .set_all = generic_set_all, 9052ec1df41SThomas Gleixner .get = generic_get_mtrr, 9062ec1df41SThomas Gleixner .get_free_region = generic_get_free_region, 9072ec1df41SThomas Gleixner .set = generic_set_mtrr, 9082ec1df41SThomas Gleixner .validate_add_page = generic_validate_add_page, 9092ec1df41SThomas Gleixner .have_wrcomb = generic_have_wrcomb, 9102ec1df41SThomas Gleixner }; 911