xref: /linux/arch/x86/kernel/cpu/mtrr/generic.c (revision d5f66d5d10611978c3a93cc94a811d74e0cf6cbc)
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>
11a1a499a3SJaswinder Singh Rajput 
12a1a499a3SJaswinder Singh Rajput #include <asm/processor-flags.h>
13*d5f66d5dSJuergen Gross #include <asm/cacheinfo.h>
14a1a499a3SJaswinder Singh Rajput #include <asm/cpufeature.h>
15a1a499a3SJaswinder Singh Rajput #include <asm/tlbflush.h>
162ec1df41SThomas Gleixner #include <asm/mtrr.h>
172ec1df41SThomas Gleixner #include <asm/msr.h>
18eb243d1dSIngo Molnar #include <asm/memtype.h>
19a1a499a3SJaswinder Singh Rajput 
202ec1df41SThomas Gleixner #include "mtrr.h"
212ec1df41SThomas Gleixner 
222ec1df41SThomas Gleixner struct fixed_range_block {
232ec1df41SThomas Gleixner 	int base_msr;		/* start address of an MTRR block */
242ec1df41SThomas Gleixner 	int ranges;		/* number of MTRRs in this block  */
252ec1df41SThomas Gleixner };
262ec1df41SThomas Gleixner 
272ec1df41SThomas Gleixner static struct fixed_range_block fixed_range_blocks[] = {
28a036c7a3SJaswinder Singh Rajput 	{ MSR_MTRRfix64K_00000, 1 }, /* one   64k MTRR  */
297d9d55e4SJaswinder Singh Rajput 	{ MSR_MTRRfix16K_80000, 2 }, /* two   16k MTRRs */
30ba5673ffSJaswinder Singh Rajput 	{ MSR_MTRRfix4K_C0000,  8 }, /* eight  4k MTRRs */
312ec1df41SThomas Gleixner 	{}
322ec1df41SThomas Gleixner };
332ec1df41SThomas Gleixner 
342ec1df41SThomas Gleixner static unsigned long smp_changes_mask;
352e5d9c85Svenkatesh.pallipadi@intel.com static int mtrr_state_set;
3695ffa243SYinghai Lu u64 mtrr_tom2;
372ec1df41SThomas Gleixner 
38a1a499a3SJaswinder Singh Rajput struct mtrr_state_type mtrr_state;
39932d27a7SSheng Yang EXPORT_SYMBOL_GPL(mtrr_state);
40932d27a7SSheng Yang 
41a1a499a3SJaswinder Singh Rajput /*
423ff42da5SAndreas Herrmann  * BIOS is expected to clear MtrrFixDramModEn bit, see for example
433ff42da5SAndreas Herrmann  * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
443ff42da5SAndreas Herrmann  * Opteron Processors" (26094 Rev. 3.30 February 2006), section
453ff42da5SAndreas Herrmann  * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
466a6256f9SAdam Buchbinder  * to 1 during BIOS initialization of the fixed MTRRs, then cleared to
473ff42da5SAndreas Herrmann  * 0 for operation."
483ff42da5SAndreas Herrmann  */
493ff42da5SAndreas Herrmann static inline void k8_check_syscfg_dram_mod_en(void)
503ff42da5SAndreas Herrmann {
513ff42da5SAndreas Herrmann 	u32 lo, hi;
523ff42da5SAndreas Herrmann 
533ff42da5SAndreas Herrmann 	if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
543ff42da5SAndreas Herrmann 	      (boot_cpu_data.x86 >= 0x0f)))
553ff42da5SAndreas Herrmann 		return;
563ff42da5SAndreas Herrmann 
57059e5c32SBrijesh Singh 	rdmsr(MSR_AMD64_SYSCFG, lo, hi);
583ff42da5SAndreas Herrmann 	if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
591b74dde7SChen Yucong 		pr_err(FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
603ff42da5SAndreas Herrmann 		       " not cleared by BIOS, clearing this bit\n",
613ff42da5SAndreas Herrmann 		       smp_processor_id());
623ff42da5SAndreas Herrmann 		lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
63059e5c32SBrijesh Singh 		mtrr_wrmsr(MSR_AMD64_SYSCFG, lo, hi);
643ff42da5SAndreas Herrmann 	}
653ff42da5SAndreas Herrmann }
663ff42da5SAndreas Herrmann 
67351e5a70SVenkatesh Pallipadi /* Get the size of contiguous MTRR range */
68351e5a70SVenkatesh Pallipadi static u64 get_mtrr_size(u64 mask)
69351e5a70SVenkatesh Pallipadi {
70351e5a70SVenkatesh Pallipadi 	u64 size;
71351e5a70SVenkatesh Pallipadi 
72351e5a70SVenkatesh Pallipadi 	mask >>= PAGE_SHIFT;
73351e5a70SVenkatesh Pallipadi 	mask |= size_or_mask;
74351e5a70SVenkatesh Pallipadi 	size = -mask;
75351e5a70SVenkatesh Pallipadi 	size <<= PAGE_SHIFT;
76351e5a70SVenkatesh Pallipadi 	return size;
77351e5a70SVenkatesh Pallipadi }
78351e5a70SVenkatesh Pallipadi 
792e5d9c85Svenkatesh.pallipadi@intel.com /*
80a7f07cfbSVenkatesh Pallipadi  * Check and return the effective type for MTRR-MTRR type overlap.
81a7f07cfbSVenkatesh Pallipadi  * Returns 1 if the effective type is UNCACHEABLE, else returns 0
82a7f07cfbSVenkatesh Pallipadi  */
83a7f07cfbSVenkatesh Pallipadi static int check_type_overlap(u8 *prev, u8 *curr)
84a7f07cfbSVenkatesh Pallipadi {
85a7f07cfbSVenkatesh Pallipadi 	if (*prev == MTRR_TYPE_UNCACHABLE || *curr == MTRR_TYPE_UNCACHABLE) {
86a7f07cfbSVenkatesh Pallipadi 		*prev = MTRR_TYPE_UNCACHABLE;
87a7f07cfbSVenkatesh Pallipadi 		*curr = MTRR_TYPE_UNCACHABLE;
88a7f07cfbSVenkatesh Pallipadi 		return 1;
89a7f07cfbSVenkatesh Pallipadi 	}
90a7f07cfbSVenkatesh Pallipadi 
91a7f07cfbSVenkatesh Pallipadi 	if ((*prev == MTRR_TYPE_WRBACK && *curr == MTRR_TYPE_WRTHROUGH) ||
92a7f07cfbSVenkatesh Pallipadi 	    (*prev == MTRR_TYPE_WRTHROUGH && *curr == MTRR_TYPE_WRBACK)) {
93a7f07cfbSVenkatesh Pallipadi 		*prev = MTRR_TYPE_WRTHROUGH;
94a7f07cfbSVenkatesh Pallipadi 		*curr = MTRR_TYPE_WRTHROUGH;
95a7f07cfbSVenkatesh Pallipadi 	}
96a7f07cfbSVenkatesh Pallipadi 
97a7f07cfbSVenkatesh Pallipadi 	if (*prev != *curr) {
98a7f07cfbSVenkatesh Pallipadi 		*prev = MTRR_TYPE_UNCACHABLE;
99a7f07cfbSVenkatesh Pallipadi 		*curr = MTRR_TYPE_UNCACHABLE;
100a7f07cfbSVenkatesh Pallipadi 		return 1;
101a7f07cfbSVenkatesh Pallipadi 	}
102a7f07cfbSVenkatesh Pallipadi 
103a7f07cfbSVenkatesh Pallipadi 	return 0;
104a7f07cfbSVenkatesh Pallipadi }
105a7f07cfbSVenkatesh Pallipadi 
1060cc705f5SToshi Kani /**
1070cc705f5SToshi Kani  * mtrr_type_lookup_fixed - look up memory type in MTRR fixed entries
1080cc705f5SToshi Kani  *
1090cc705f5SToshi Kani  * Return the MTRR fixed memory type of 'start'.
1100cc705f5SToshi Kani  *
1110cc705f5SToshi Kani  * MTRR fixed entries are divided into the following ways:
1120cc705f5SToshi Kani  *  0x00000 - 0x7FFFF : This range is divided into eight 64KB sub-ranges
1130cc705f5SToshi Kani  *  0x80000 - 0xBFFFF : This range is divided into sixteen 16KB sub-ranges
1140cc705f5SToshi Kani  *  0xC0000 - 0xFFFFF : This range is divided into sixty-four 4KB sub-ranges
1150cc705f5SToshi Kani  *
1160cc705f5SToshi Kani  * Return Values:
1170cc705f5SToshi Kani  * MTRR_TYPE_(type)  - Matched memory type
1180cc705f5SToshi Kani  * MTRR_TYPE_INVALID - Unmatched
1192e5d9c85Svenkatesh.pallipadi@intel.com  */
1200cc705f5SToshi Kani static u8 mtrr_type_lookup_fixed(u64 start, u64 end)
1210cc705f5SToshi Kani {
1220cc705f5SToshi Kani 	int idx;
1230cc705f5SToshi Kani 
1240cc705f5SToshi Kani 	if (start >= 0x100000)
1250cc705f5SToshi Kani 		return MTRR_TYPE_INVALID;
1260cc705f5SToshi Kani 
1270cc705f5SToshi Kani 	/* 0x0 - 0x7FFFF */
1280cc705f5SToshi Kani 	if (start < 0x80000) {
1290cc705f5SToshi Kani 		idx = 0;
1300cc705f5SToshi Kani 		idx += (start >> 16);
1310cc705f5SToshi Kani 		return mtrr_state.fixed_ranges[idx];
1320cc705f5SToshi Kani 	/* 0x80000 - 0xBFFFF */
1330cc705f5SToshi Kani 	} else if (start < 0xC0000) {
1340cc705f5SToshi Kani 		idx = 1 * 8;
1350cc705f5SToshi Kani 		idx += ((start - 0x80000) >> 14);
1360cc705f5SToshi Kani 		return mtrr_state.fixed_ranges[idx];
1370cc705f5SToshi Kani 	}
1380cc705f5SToshi Kani 
1390cc705f5SToshi Kani 	/* 0xC0000 - 0xFFFFF */
1400cc705f5SToshi Kani 	idx = 3 * 8;
1410cc705f5SToshi Kani 	idx += ((start - 0xC0000) >> 12);
1420cc705f5SToshi Kani 	return mtrr_state.fixed_ranges[idx];
1430cc705f5SToshi Kani }
1440cc705f5SToshi Kani 
1450cc705f5SToshi Kani /**
1460cc705f5SToshi Kani  * mtrr_type_lookup_variable - look up memory type in MTRR variable entries
1470cc705f5SToshi Kani  *
1480cc705f5SToshi Kani  * Return Value:
1490cc705f5SToshi Kani  * MTRR_TYPE_(type) - Matched memory type or default memory type (unmatched)
1500cc705f5SToshi Kani  *
151b73522e0SToshi Kani  * Output Arguments:
1520cc705f5SToshi Kani  * repeat - Set to 1 when [start:end] spanned across MTRR range and type
1530cc705f5SToshi Kani  *	    returned corresponds only to [start:*partial_end].  Caller has
1540cc705f5SToshi Kani  *	    to lookup again for [*partial_end:end].
155b73522e0SToshi Kani  *
156b73522e0SToshi Kani  * uniform - Set to 1 when an MTRR covers the region uniformly, i.e. the
157b73522e0SToshi Kani  *	     region is fully covered by a single MTRR entry or the default
158b73522e0SToshi Kani  *	     type.
1590cc705f5SToshi Kani  */
1600cc705f5SToshi Kani static u8 mtrr_type_lookup_variable(u64 start, u64 end, u64 *partial_end,
161b73522e0SToshi Kani 				    int *repeat, u8 *uniform)
1622e5d9c85Svenkatesh.pallipadi@intel.com {
1632e5d9c85Svenkatesh.pallipadi@intel.com 	int i;
1642e5d9c85Svenkatesh.pallipadi@intel.com 	u64 base, mask;
1652e5d9c85Svenkatesh.pallipadi@intel.com 	u8 prev_match, curr_match;
1662e5d9c85Svenkatesh.pallipadi@intel.com 
167351e5a70SVenkatesh Pallipadi 	*repeat = 0;
168b73522e0SToshi Kani 	*uniform = 1;
1692e5d9c85Svenkatesh.pallipadi@intel.com 
1703d3ca416SToshi Kani 	prev_match = MTRR_TYPE_INVALID;
1712e5d9c85Svenkatesh.pallipadi@intel.com 	for (i = 0; i < num_var_ranges; ++i) {
1727f0431e3SToshi Kani 		unsigned short start_state, end_state, inclusive;
1732e5d9c85Svenkatesh.pallipadi@intel.com 
1742e5d9c85Svenkatesh.pallipadi@intel.com 		if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11)))
1752e5d9c85Svenkatesh.pallipadi@intel.com 			continue;
1762e5d9c85Svenkatesh.pallipadi@intel.com 
1772e5d9c85Svenkatesh.pallipadi@intel.com 		base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) +
1782e5d9c85Svenkatesh.pallipadi@intel.com 		       (mtrr_state.var_ranges[i].base_lo & PAGE_MASK);
1792e5d9c85Svenkatesh.pallipadi@intel.com 		mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) +
1802e5d9c85Svenkatesh.pallipadi@intel.com 		       (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK);
1812e5d9c85Svenkatesh.pallipadi@intel.com 
1822e5d9c85Svenkatesh.pallipadi@intel.com 		start_state = ((start & mask) == (base & mask));
1832e5d9c85Svenkatesh.pallipadi@intel.com 		end_state = ((end & mask) == (base & mask));
1847f0431e3SToshi Kani 		inclusive = ((start < base) && (end > base));
185351e5a70SVenkatesh Pallipadi 
1867f0431e3SToshi Kani 		if ((start_state != end_state) || inclusive) {
187351e5a70SVenkatesh Pallipadi 			/*
188351e5a70SVenkatesh Pallipadi 			 * We have start:end spanning across an MTRR.
1897f0431e3SToshi Kani 			 * We split the region into either
1907f0431e3SToshi Kani 			 *
1917f0431e3SToshi Kani 			 * - start_state:1
192351e5a70SVenkatesh Pallipadi 			 * (start:mtrr_end)(mtrr_end:end)
1937f0431e3SToshi Kani 			 * - end_state:1
194351e5a70SVenkatesh Pallipadi 			 * (start:mtrr_start)(mtrr_start:end)
1957f0431e3SToshi Kani 			 * - inclusive:1
1967f0431e3SToshi Kani 			 * (start:mtrr_start)(mtrr_start:mtrr_end)(mtrr_end:end)
1977f0431e3SToshi Kani 			 *
198351e5a70SVenkatesh Pallipadi 			 * depending on kind of overlap.
1997f0431e3SToshi Kani 			 *
2007f0431e3SToshi Kani 			 * Return the type of the first region and a pointer
2017f0431e3SToshi Kani 			 * to the start of next region so that caller will be
2027f0431e3SToshi Kani 			 * advised to lookup again after having adjusted start
2037f0431e3SToshi Kani 			 * and end.
2047f0431e3SToshi Kani 			 *
2050cc705f5SToshi Kani 			 * Note: This way we handle overlaps with multiple
2060cc705f5SToshi Kani 			 * entries and the default type properly.
207351e5a70SVenkatesh Pallipadi 			 */
208351e5a70SVenkatesh Pallipadi 			if (start_state)
209351e5a70SVenkatesh Pallipadi 				*partial_end = base + get_mtrr_size(mask);
210351e5a70SVenkatesh Pallipadi 			else
211351e5a70SVenkatesh Pallipadi 				*partial_end = base;
212351e5a70SVenkatesh Pallipadi 
213351e5a70SVenkatesh Pallipadi 			if (unlikely(*partial_end <= start)) {
214351e5a70SVenkatesh Pallipadi 				WARN_ON(1);
215351e5a70SVenkatesh Pallipadi 				*partial_end = start + PAGE_SIZE;
216351e5a70SVenkatesh Pallipadi 			}
217351e5a70SVenkatesh Pallipadi 
218351e5a70SVenkatesh Pallipadi 			end = *partial_end - 1; /* end is inclusive */
219351e5a70SVenkatesh Pallipadi 			*repeat = 1;
220b73522e0SToshi Kani 			*uniform = 0;
221351e5a70SVenkatesh Pallipadi 		}
2222e5d9c85Svenkatesh.pallipadi@intel.com 
223a1a499a3SJaswinder Singh Rajput 		if ((start & mask) != (base & mask))
2242e5d9c85Svenkatesh.pallipadi@intel.com 			continue;
2252e5d9c85Svenkatesh.pallipadi@intel.com 
2262e5d9c85Svenkatesh.pallipadi@intel.com 		curr_match = mtrr_state.var_ranges[i].base_lo & 0xff;
2273d3ca416SToshi Kani 		if (prev_match == MTRR_TYPE_INVALID) {
2282e5d9c85Svenkatesh.pallipadi@intel.com 			prev_match = curr_match;
2292e5d9c85Svenkatesh.pallipadi@intel.com 			continue;
2302e5d9c85Svenkatesh.pallipadi@intel.com 		}
2312e5d9c85Svenkatesh.pallipadi@intel.com 
232b73522e0SToshi Kani 		*uniform = 0;
233a7f07cfbSVenkatesh Pallipadi 		if (check_type_overlap(&prev_match, &curr_match))
234a7f07cfbSVenkatesh Pallipadi 			return curr_match;
2352e5d9c85Svenkatesh.pallipadi@intel.com 	}
2362e5d9c85Svenkatesh.pallipadi@intel.com 
2373d3ca416SToshi Kani 	if (prev_match != MTRR_TYPE_INVALID)
2382e5d9c85Svenkatesh.pallipadi@intel.com 		return prev_match;
2392e5d9c85Svenkatesh.pallipadi@intel.com 
2402e5d9c85Svenkatesh.pallipadi@intel.com 	return mtrr_state.def_type;
2412e5d9c85Svenkatesh.pallipadi@intel.com }
2422e5d9c85Svenkatesh.pallipadi@intel.com 
2430cc705f5SToshi Kani /**
2440cc705f5SToshi Kani  * mtrr_type_lookup - look up memory type in MTRR
2450cc705f5SToshi Kani  *
2460cc705f5SToshi Kani  * Return Values:
2470cc705f5SToshi Kani  * MTRR_TYPE_(type)  - The effective MTRR type for the region
2480cc705f5SToshi Kani  * MTRR_TYPE_INVALID - MTRR is disabled
249b73522e0SToshi Kani  *
250b73522e0SToshi Kani  * Output Argument:
251b73522e0SToshi Kani  * uniform - Set to 1 when an MTRR covers the region uniformly, i.e. the
252b73522e0SToshi Kani  *	     region is fully covered by a single MTRR entry or the default
253b73522e0SToshi Kani  *	     type.
254351e5a70SVenkatesh Pallipadi  */
255b73522e0SToshi Kani u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
256351e5a70SVenkatesh Pallipadi {
257b73522e0SToshi Kani 	u8 type, prev_type, is_uniform = 1, dummy;
258351e5a70SVenkatesh Pallipadi 	int repeat;
259351e5a70SVenkatesh Pallipadi 	u64 partial_end;
260351e5a70SVenkatesh Pallipadi 
261cb7f4a8bSYing-Tsun Huang 	/* Make end inclusive instead of exclusive */
262cb7f4a8bSYing-Tsun Huang 	end--;
263cb7f4a8bSYing-Tsun Huang 
2640cc705f5SToshi Kani 	if (!mtrr_state_set)
2650cc705f5SToshi Kani 		return MTRR_TYPE_INVALID;
2660cc705f5SToshi Kani 
2670cc705f5SToshi Kani 	if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED))
2680cc705f5SToshi Kani 		return MTRR_TYPE_INVALID;
2690cc705f5SToshi Kani 
2700cc705f5SToshi Kani 	/*
2710cc705f5SToshi Kani 	 * Look up the fixed ranges first, which take priority over
2720cc705f5SToshi Kani 	 * the variable ranges.
2730cc705f5SToshi Kani 	 */
2740cc705f5SToshi Kani 	if ((start < 0x100000) &&
2750cc705f5SToshi Kani 	    (mtrr_state.have_fixed) &&
276b73522e0SToshi Kani 	    (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) {
277b73522e0SToshi Kani 		is_uniform = 0;
278b73522e0SToshi Kani 		type = mtrr_type_lookup_fixed(start, end);
279b73522e0SToshi Kani 		goto out;
280b73522e0SToshi Kani 	}
2810cc705f5SToshi Kani 
2820cc705f5SToshi Kani 	/*
2830cc705f5SToshi Kani 	 * Look up the variable ranges.  Look of multiple ranges matching
2840cc705f5SToshi Kani 	 * this address and pick type as per MTRR precedence.
2850cc705f5SToshi Kani 	 */
286b73522e0SToshi Kani 	type = mtrr_type_lookup_variable(start, end, &partial_end,
287b73522e0SToshi Kani 					 &repeat, &is_uniform);
288351e5a70SVenkatesh Pallipadi 
289351e5a70SVenkatesh Pallipadi 	/*
290351e5a70SVenkatesh Pallipadi 	 * Common path is with repeat = 0.
291351e5a70SVenkatesh Pallipadi 	 * However, we can have cases where [start:end] spans across some
2920cc705f5SToshi Kani 	 * MTRR ranges and/or the default type.  Do repeated lookups for
2930cc705f5SToshi Kani 	 * that case here.
294351e5a70SVenkatesh Pallipadi 	 */
295351e5a70SVenkatesh Pallipadi 	while (repeat) {
296351e5a70SVenkatesh Pallipadi 		prev_type = type;
297351e5a70SVenkatesh Pallipadi 		start = partial_end;
298b73522e0SToshi Kani 		is_uniform = 0;
299b73522e0SToshi Kani 		type = mtrr_type_lookup_variable(start, end, &partial_end,
300b73522e0SToshi Kani 						 &repeat, &dummy);
301351e5a70SVenkatesh Pallipadi 
302351e5a70SVenkatesh Pallipadi 		if (check_type_overlap(&prev_type, &type))
303b73522e0SToshi Kani 			goto out;
304351e5a70SVenkatesh Pallipadi 	}
305351e5a70SVenkatesh Pallipadi 
3060cc705f5SToshi Kani 	if (mtrr_tom2 && (start >= (1ULL<<32)) && (end < mtrr_tom2))
307b73522e0SToshi Kani 		type = MTRR_TYPE_WRBACK;
3080cc705f5SToshi Kani 
309b73522e0SToshi Kani out:
310b73522e0SToshi Kani 	*uniform = is_uniform;
311351e5a70SVenkatesh Pallipadi 	return type;
312351e5a70SVenkatesh Pallipadi }
313351e5a70SVenkatesh Pallipadi 
3142ec1df41SThomas Gleixner /* Get the MSR pair relating to a var range */
3152ec1df41SThomas Gleixner static void
3162ec1df41SThomas Gleixner get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
3172ec1df41SThomas Gleixner {
3182ec1df41SThomas Gleixner 	rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
3192ec1df41SThomas Gleixner 	rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
3202ec1df41SThomas Gleixner }
3212ec1df41SThomas Gleixner 
322a1a499a3SJaswinder Singh Rajput /* Fill the MSR pair relating to a var range */
32395ffa243SYinghai Lu void fill_mtrr_var_range(unsigned int index,
32495ffa243SYinghai Lu 		u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
32595ffa243SYinghai Lu {
32695ffa243SYinghai Lu 	struct mtrr_var_range *vr;
32795ffa243SYinghai Lu 
32895ffa243SYinghai Lu 	vr = mtrr_state.var_ranges;
32995ffa243SYinghai Lu 
33095ffa243SYinghai Lu 	vr[index].base_lo = base_lo;
33195ffa243SYinghai Lu 	vr[index].base_hi = base_hi;
33295ffa243SYinghai Lu 	vr[index].mask_lo = mask_lo;
33395ffa243SYinghai Lu 	vr[index].mask_hi = mask_hi;
33495ffa243SYinghai Lu }
33595ffa243SYinghai Lu 
336a1a499a3SJaswinder Singh Rajput static void get_fixed_ranges(mtrr_type *frs)
3372ec1df41SThomas Gleixner {
3382ec1df41SThomas Gleixner 	unsigned int *p = (unsigned int *)frs;
3392ec1df41SThomas Gleixner 	int i;
3402ec1df41SThomas Gleixner 
3413ff42da5SAndreas Herrmann 	k8_check_syscfg_dram_mod_en();
3423ff42da5SAndreas Herrmann 
343a036c7a3SJaswinder Singh Rajput 	rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
3442ec1df41SThomas Gleixner 
3452ec1df41SThomas Gleixner 	for (i = 0; i < 2; i++)
3467d9d55e4SJaswinder Singh Rajput 		rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
3472ec1df41SThomas Gleixner 	for (i = 0; i < 8; i++)
348ba5673ffSJaswinder Singh Rajput 		rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
3492ec1df41SThomas Gleixner }
3502ec1df41SThomas Gleixner 
3512ec1df41SThomas Gleixner void mtrr_save_fixed_ranges(void *info)
3522ec1df41SThomas Gleixner {
353362f924bSBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_MTRR))
3542ec1df41SThomas Gleixner 		get_fixed_ranges(mtrr_state.fixed_ranges);
3552ec1df41SThomas Gleixner }
3562ec1df41SThomas Gleixner 
357d4c90e37SYinghai Lu static unsigned __initdata last_fixed_start;
358d4c90e37SYinghai Lu static unsigned __initdata last_fixed_end;
359d4c90e37SYinghai Lu static mtrr_type __initdata last_fixed_type;
360d4c90e37SYinghai Lu 
361d4c90e37SYinghai Lu static void __init print_fixed_last(void)
362d4c90e37SYinghai Lu {
363d4c90e37SYinghai Lu 	if (!last_fixed_end)
364d4c90e37SYinghai Lu 		return;
365d4c90e37SYinghai Lu 
366a1a499a3SJaswinder Singh Rajput 	pr_debug("  %05X-%05X %s\n", last_fixed_start,
367d4c90e37SYinghai Lu 		 last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
368d4c90e37SYinghai Lu 
369d4c90e37SYinghai Lu 	last_fixed_end = 0;
370d4c90e37SYinghai Lu }
371d4c90e37SYinghai Lu 
372d4c90e37SYinghai Lu static void __init update_fixed_last(unsigned base, unsigned end,
373d4c90e37SYinghai Lu 				     mtrr_type type)
374d4c90e37SYinghai Lu {
375d4c90e37SYinghai Lu 	last_fixed_start = base;
376d4c90e37SYinghai Lu 	last_fixed_end = end;
377d4c90e37SYinghai Lu 	last_fixed_type = type;
378d4c90e37SYinghai Lu }
379d4c90e37SYinghai Lu 
380a1a499a3SJaswinder Singh Rajput static void __init
381a1a499a3SJaswinder Singh Rajput print_fixed(unsigned base, unsigned step, const mtrr_type *types)
3822ec1df41SThomas Gleixner {
3832ec1df41SThomas Gleixner 	unsigned i;
3842ec1df41SThomas Gleixner 
385d4c90e37SYinghai Lu 	for (i = 0; i < 8; ++i, ++types, base += step) {
386d4c90e37SYinghai Lu 		if (last_fixed_end == 0) {
387d4c90e37SYinghai Lu 			update_fixed_last(base, base + step, *types);
388d4c90e37SYinghai Lu 			continue;
389d4c90e37SYinghai Lu 		}
390d4c90e37SYinghai Lu 		if (last_fixed_end == base && last_fixed_type == *types) {
391d4c90e37SYinghai Lu 			last_fixed_end = base + step;
392d4c90e37SYinghai Lu 			continue;
393d4c90e37SYinghai Lu 		}
394d4c90e37SYinghai Lu 		/* new segments: gap or different type */
395d4c90e37SYinghai Lu 		print_fixed_last();
396d4c90e37SYinghai Lu 		update_fixed_last(base, base + step, *types);
397d4c90e37SYinghai Lu 	}
3982ec1df41SThomas Gleixner }
3992ec1df41SThomas Gleixner 
4008ad97905SYinghai Lu static void __init print_mtrr_state(void)
4018ad97905SYinghai Lu {
4028ad97905SYinghai Lu 	unsigned int i;
4038ad97905SYinghai Lu 	int high_width;
4048ad97905SYinghai Lu 
405a1a499a3SJaswinder Singh Rajput 	pr_debug("MTRR default type: %s\n",
406d4c90e37SYinghai Lu 		 mtrr_attrib_to_str(mtrr_state.def_type));
4078ad97905SYinghai Lu 	if (mtrr_state.have_fixed) {
408a1a499a3SJaswinder Singh Rajput 		pr_debug("MTRR fixed ranges %sabled:\n",
4099b3aca62SToshi Kani 			((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) &&
4109b3aca62SToshi Kani 			 (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ?
4119b3aca62SToshi Kani 			 "en" : "dis");
4128ad97905SYinghai Lu 		print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
4138ad97905SYinghai Lu 		for (i = 0; i < 2; ++i)
414a1a499a3SJaswinder Singh Rajput 			print_fixed(0x80000 + i * 0x20000, 0x04000,
415a1a499a3SJaswinder Singh Rajput 				    mtrr_state.fixed_ranges + (i + 1) * 8);
4168ad97905SYinghai Lu 		for (i = 0; i < 8; ++i)
417a1a499a3SJaswinder Singh Rajput 			print_fixed(0xC0000 + i * 0x08000, 0x01000,
418a1a499a3SJaswinder Singh Rajput 				    mtrr_state.fixed_ranges + (i + 3) * 8);
419d4c90e37SYinghai Lu 
420d4c90e37SYinghai Lu 		/* tail */
421d4c90e37SYinghai Lu 		print_fixed_last();
4228ad97905SYinghai Lu 	}
423a1a499a3SJaswinder Singh Rajput 	pr_debug("MTRR variable ranges %sabled:\n",
4249b3aca62SToshi Kani 		 mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis");
425a7101d15SJan Beulich 	high_width = (__ffs64(size_or_mask) - (32 - PAGE_SHIFT) + 3) / 4;
426a1a499a3SJaswinder Singh Rajput 
4278ad97905SYinghai Lu 	for (i = 0; i < num_var_ranges; ++i) {
4288ad97905SYinghai Lu 		if (mtrr_state.var_ranges[i].mask_lo & (1 << 11))
429a1a499a3SJaswinder Singh Rajput 			pr_debug("  %u base %0*X%05X000 mask %0*X%05X000 %s\n",
4308ad97905SYinghai Lu 				 i,
4318ad97905SYinghai Lu 				 high_width,
4328ad97905SYinghai Lu 				 mtrr_state.var_ranges[i].base_hi,
4338ad97905SYinghai Lu 				 mtrr_state.var_ranges[i].base_lo >> 12,
4348ad97905SYinghai Lu 				 high_width,
4358ad97905SYinghai Lu 				 mtrr_state.var_ranges[i].mask_hi,
4368ad97905SYinghai Lu 				 mtrr_state.var_ranges[i].mask_lo >> 12,
4378ad97905SYinghai Lu 				 mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff));
4388ad97905SYinghai Lu 		else
439a1a499a3SJaswinder Singh Rajput 			pr_debug("  %u disabled\n", i);
4408ad97905SYinghai Lu 	}
441a1a499a3SJaswinder Singh Rajput 	if (mtrr_tom2)
442a1a499a3SJaswinder Singh Rajput 		pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
4438ad97905SYinghai Lu }
4448ad97905SYinghai Lu 
445ad025a73SToshi Kani /* PAT setup for BP. We need to go through sync steps here */
446ad025a73SToshi Kani void __init mtrr_bp_pat_init(void)
447ad025a73SToshi Kani {
448ad025a73SToshi Kani 	unsigned long flags;
449ad025a73SToshi Kani 
450ad025a73SToshi Kani 	local_irq_save(flags);
451*d5f66d5dSJuergen Gross 	cache_disable();
452ad025a73SToshi Kani 
453ad025a73SToshi Kani 	pat_init();
454ad025a73SToshi Kani 
455*d5f66d5dSJuergen Gross 	cache_enable();
456ad025a73SToshi Kani 	local_irq_restore(flags);
457ad025a73SToshi Kani }
458ad025a73SToshi Kani 
4592ec1df41SThomas Gleixner /* Grab all of the MTRR state for this CPU into *state */
460f9626104SLuis R. Rodriguez bool __init get_mtrr_state(void)
4612ec1df41SThomas Gleixner {
4622ec1df41SThomas Gleixner 	struct mtrr_var_range *vrs;
463a1a499a3SJaswinder Singh Rajput 	unsigned lo, dummy;
464a1a499a3SJaswinder Singh Rajput 	unsigned int i;
4652ec1df41SThomas Gleixner 
4662ec1df41SThomas Gleixner 	vrs = mtrr_state.var_ranges;
4672ec1df41SThomas Gleixner 
468d9bcc01dSJaswinder Singh Rajput 	rdmsr(MSR_MTRRcap, lo, dummy);
4692ec1df41SThomas Gleixner 	mtrr_state.have_fixed = (lo >> 8) & 1;
4702ec1df41SThomas Gleixner 
4712ec1df41SThomas Gleixner 	for (i = 0; i < num_var_ranges; i++)
4722ec1df41SThomas Gleixner 		get_mtrr_var_range(i, &vrs[i]);
4732ec1df41SThomas Gleixner 	if (mtrr_state.have_fixed)
4742ec1df41SThomas Gleixner 		get_fixed_ranges(mtrr_state.fixed_ranges);
4752ec1df41SThomas Gleixner 
47652650257SJaswinder Singh Rajput 	rdmsr(MSR_MTRRdefType, lo, dummy);
4772ec1df41SThomas Gleixner 	mtrr_state.def_type = (lo & 0xff);
4782ec1df41SThomas Gleixner 	mtrr_state.enabled = (lo & 0xc00) >> 10;
4792ec1df41SThomas Gleixner 
48035605a10SYinghai Lu 	if (amd_special_default_mtrr()) {
4810da72a4aSThomas Gleixner 		unsigned low, high;
482a1a499a3SJaswinder Singh Rajput 
48335605a10SYinghai Lu 		/* TOP_MEM2 */
4840da72a4aSThomas Gleixner 		rdmsr(MSR_K8_TOP_MEM2, low, high);
48595ffa243SYinghai Lu 		mtrr_tom2 = high;
48695ffa243SYinghai Lu 		mtrr_tom2 <<= 32;
48795ffa243SYinghai Lu 		mtrr_tom2 |= low;
4888004dd96SYinghai Lu 		mtrr_tom2 &= 0xffffff800000ULL;
48935605a10SYinghai Lu 	}
4902ec1df41SThomas Gleixner 
4918ad97905SYinghai Lu 	print_mtrr_state();
4928ad97905SYinghai Lu 
4932e5d9c85Svenkatesh.pallipadi@intel.com 	mtrr_state_set = 1;
4942e5d9c85Svenkatesh.pallipadi@intel.com 
495f9626104SLuis R. Rodriguez 	return !!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED);
4962ec1df41SThomas Gleixner }
4972ec1df41SThomas Gleixner 
498a1a499a3SJaswinder Singh Rajput /* Some BIOS's are messed up and don't set all MTRRs the same! */
4992ec1df41SThomas Gleixner void __init mtrr_state_warn(void)
5002ec1df41SThomas Gleixner {
5012ec1df41SThomas Gleixner 	unsigned long mask = smp_changes_mask;
5022ec1df41SThomas Gleixner 
5032ec1df41SThomas Gleixner 	if (!mask)
5042ec1df41SThomas Gleixner 		return;
5052ec1df41SThomas Gleixner 	if (mask & MTRR_CHANGE_MASK_FIXED)
5061b74dde7SChen Yucong 		pr_warn("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
5072ec1df41SThomas Gleixner 	if (mask & MTRR_CHANGE_MASK_VARIABLE)
5081b74dde7SChen Yucong 		pr_warn("mtrr: your CPUs had inconsistent variable MTRR settings\n");
5092ec1df41SThomas Gleixner 	if (mask & MTRR_CHANGE_MASK_DEFTYPE)
5101b74dde7SChen Yucong 		pr_warn("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
511a1a499a3SJaswinder Singh Rajput 
5121b74dde7SChen Yucong 	pr_info("mtrr: probably your BIOS does not setup all CPUs.\n");
5131b74dde7SChen Yucong 	pr_info("mtrr: corrected configuration.\n");
5142ec1df41SThomas Gleixner }
5152ec1df41SThomas Gleixner 
516a1a499a3SJaswinder Singh Rajput /*
517a1a499a3SJaswinder Singh Rajput  * Doesn't attempt to pass an error out to MTRR users
518a1a499a3SJaswinder Singh Rajput  * because it's quite complicated in some cases and probably not
519a1a499a3SJaswinder Singh Rajput  * worth it because the best error handling is to ignore it.
520a1a499a3SJaswinder Singh Rajput  */
5212ec1df41SThomas Gleixner void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
5222ec1df41SThomas Gleixner {
523a1a499a3SJaswinder Singh Rajput 	if (wrmsr_safe(msr, a, b) < 0) {
5241b74dde7SChen Yucong 		pr_err("MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
5252ec1df41SThomas Gleixner 			smp_processor_id(), msr, a, b);
5262ec1df41SThomas Gleixner 	}
527a1a499a3SJaswinder Singh Rajput }
5282ec1df41SThomas Gleixner 
5292ec1df41SThomas Gleixner /**
530a1a499a3SJaswinder Singh Rajput  * set_fixed_range - checks & updates a fixed-range MTRR if it
531a1a499a3SJaswinder Singh Rajput  *		     differs from the value it should have
5321d3381ebSRandy Dunlap  * @msr: MSR address of the MTTR which should be checked and updated
5331d3381ebSRandy Dunlap  * @changed: pointer which indicates whether the MTRR needed to be changed
5341d3381ebSRandy Dunlap  * @msrwords: pointer to the MSR values which the MSR should have
5352ec1df41SThomas Gleixner  */
5362d2ee8deSPaul Jimenez static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
5372ec1df41SThomas Gleixner {
5382ec1df41SThomas Gleixner 	unsigned lo, hi;
5392ec1df41SThomas Gleixner 
5402ec1df41SThomas Gleixner 	rdmsr(msr, lo, hi);
5412ec1df41SThomas Gleixner 
5422ec1df41SThomas Gleixner 	if (lo != msrwords[0] || hi != msrwords[1]) {
5432ec1df41SThomas Gleixner 		mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
5442d2ee8deSPaul Jimenez 		*changed = true;
5452ec1df41SThomas Gleixner 	}
5462ec1df41SThomas Gleixner }
5472ec1df41SThomas Gleixner 
5481d3381ebSRandy Dunlap /**
5491d3381ebSRandy Dunlap  * generic_get_free_region - Get a free MTRR.
5501d3381ebSRandy Dunlap  * @base: The starting (base) address of the region.
5511d3381ebSRandy Dunlap  * @size: The size (in bytes) of the region.
5521d3381ebSRandy Dunlap  * @replace_reg: mtrr index to be replaced; set to invalid value if none.
5531d3381ebSRandy Dunlap  *
5541d3381ebSRandy Dunlap  * Returns: The index of the region on success, else negative on error.
5552ec1df41SThomas Gleixner  */
556a1a499a3SJaswinder Singh Rajput int
557a1a499a3SJaswinder Singh Rajput generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
5582ec1df41SThomas Gleixner {
5592ec1df41SThomas Gleixner 	unsigned long lbase, lsize;
560a1a499a3SJaswinder Singh Rajput 	mtrr_type ltype;
561a1a499a3SJaswinder Singh Rajput 	int i, max;
5622ec1df41SThomas Gleixner 
5632ec1df41SThomas Gleixner 	max = num_var_ranges;
5642ec1df41SThomas Gleixner 	if (replace_reg >= 0 && replace_reg < max)
5652ec1df41SThomas Gleixner 		return replace_reg;
566a1a499a3SJaswinder Singh Rajput 
5672ec1df41SThomas Gleixner 	for (i = 0; i < max; ++i) {
5682ec1df41SThomas Gleixner 		mtrr_if->get(i, &lbase, &lsize, &ltype);
5692ec1df41SThomas Gleixner 		if (lsize == 0)
5702ec1df41SThomas Gleixner 			return i;
5712ec1df41SThomas Gleixner 	}
572a1a499a3SJaswinder Singh Rajput 
5732ec1df41SThomas Gleixner 	return -ENOSPC;
5742ec1df41SThomas Gleixner }
5752ec1df41SThomas Gleixner 
5762ec1df41SThomas Gleixner static void generic_get_mtrr(unsigned int reg, unsigned long *base,
5772ec1df41SThomas Gleixner 			     unsigned long *size, mtrr_type *type)
5782ec1df41SThomas Gleixner {
579d5c78673SYinghai Lu 	u32 mask_lo, mask_hi, base_lo, base_hi;
580d5c78673SYinghai Lu 	unsigned int hi;
581d5c78673SYinghai Lu 	u64 tmp, mask;
5822ec1df41SThomas Gleixner 
5838ad97905SYinghai Lu 	/*
5848ad97905SYinghai Lu 	 * get_mtrr doesn't need to update mtrr_state, also it could be called
5858ad97905SYinghai Lu 	 * from any cpu, so try to print it out directly.
5868ad97905SYinghai Lu 	 */
587fa10ba64SAndi Kleen 	get_cpu();
58863516ef6SYinghai Lu 
5892ec1df41SThomas Gleixner 	rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
5908ad97905SYinghai Lu 
5912ec1df41SThomas Gleixner 	if ((mask_lo & 0x800) == 0) {
5922ec1df41SThomas Gleixner 		/*  Invalid (i.e. free) range */
5932ec1df41SThomas Gleixner 		*base = 0;
5942ec1df41SThomas Gleixner 		*size = 0;
5952ec1df41SThomas Gleixner 		*type = 0;
59663516ef6SYinghai Lu 		goto out_put_cpu;
5972ec1df41SThomas Gleixner 	}
5982ec1df41SThomas Gleixner 
5992ec1df41SThomas Gleixner 	rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
6002ec1df41SThomas Gleixner 
60163516ef6SYinghai Lu 	/* Work out the shifted address mask: */
602d5c78673SYinghai Lu 	tmp = (u64)mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT;
603d5c78673SYinghai Lu 	mask = size_or_mask | tmp;
60463516ef6SYinghai Lu 
60563516ef6SYinghai Lu 	/* Expand tmp with high bits to all 1s: */
606d5c78673SYinghai Lu 	hi = fls64(tmp);
60738cc1c3dSYinghai Lu 	if (hi > 0) {
608d5c78673SYinghai Lu 		tmp |= ~((1ULL<<(hi - 1)) - 1);
60938cc1c3dSYinghai Lu 
610d5c78673SYinghai Lu 		if (tmp != mask) {
6111b74dde7SChen Yucong 			pr_warn("mtrr: your BIOS has configured an incorrect mask, fixing it.\n");
612373d4d09SRusty Russell 			add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
613d5c78673SYinghai Lu 			mask = tmp;
61438cc1c3dSYinghai Lu 		}
61538cc1c3dSYinghai Lu 	}
6162ec1df41SThomas Gleixner 
61763516ef6SYinghai Lu 	/*
61863516ef6SYinghai Lu 	 * This works correctly if size is a power of two, i.e. a
61963516ef6SYinghai Lu 	 * contiguous range:
62063516ef6SYinghai Lu 	 */
621d5c78673SYinghai Lu 	*size = -mask;
622d5c78673SYinghai Lu 	*base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
6232ec1df41SThomas Gleixner 	*type = base_lo & 0xff;
6248ad97905SYinghai Lu 
62563516ef6SYinghai Lu out_put_cpu:
62663516ef6SYinghai Lu 	put_cpu();
6272ec1df41SThomas Gleixner }
6282ec1df41SThomas Gleixner 
6292ec1df41SThomas Gleixner /**
630a1a499a3SJaswinder Singh Rajput  * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
631a1a499a3SJaswinder Singh Rajput  *		      differ from the saved set
6321d3381ebSRandy Dunlap  * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
6332ec1df41SThomas Gleixner  */
6342ec1df41SThomas Gleixner static int set_fixed_ranges(mtrr_type *frs)
6352ec1df41SThomas Gleixner {
6362ec1df41SThomas Gleixner 	unsigned long long *saved = (unsigned long long *)frs;
6372d2ee8deSPaul Jimenez 	bool changed = false;
6382ec1df41SThomas Gleixner 	int block = -1, range;
6392ec1df41SThomas Gleixner 
6403ff42da5SAndreas Herrmann 	k8_check_syscfg_dram_mod_en();
6413ff42da5SAndreas Herrmann 
642a1a499a3SJaswinder Singh Rajput 	while (fixed_range_blocks[++block].ranges) {
6432ec1df41SThomas Gleixner 		for (range = 0; range < fixed_range_blocks[block].ranges; range++)
6442ec1df41SThomas Gleixner 			set_fixed_range(fixed_range_blocks[block].base_msr + range,
6452ec1df41SThomas Gleixner 					&changed, (unsigned int *)saved++);
646a1a499a3SJaswinder Singh Rajput 	}
6472ec1df41SThomas Gleixner 
6482ec1df41SThomas Gleixner 	return changed;
6492ec1df41SThomas Gleixner }
6502ec1df41SThomas Gleixner 
651a1a499a3SJaswinder Singh Rajput /*
652a1a499a3SJaswinder Singh Rajput  * Set the MSR pair relating to a var range.
653a1a499a3SJaswinder Singh Rajput  * Returns true if changes are made.
654a1a499a3SJaswinder Singh Rajput  */
6552d2ee8deSPaul Jimenez static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
6562ec1df41SThomas Gleixner {
6572ec1df41SThomas Gleixner 	unsigned int lo, hi;
6582d2ee8deSPaul Jimenez 	bool changed = false;
6592ec1df41SThomas Gleixner 
6602ec1df41SThomas Gleixner 	rdmsr(MTRRphysBase_MSR(index), lo, hi);
6612ec1df41SThomas Gleixner 	if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL)
6622ec1df41SThomas Gleixner 	    || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
6632ec1df41SThomas Gleixner 		(hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
664a1a499a3SJaswinder Singh Rajput 
6652ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
6662d2ee8deSPaul Jimenez 		changed = true;
6672ec1df41SThomas Gleixner 	}
6682ec1df41SThomas Gleixner 
6692ec1df41SThomas Gleixner 	rdmsr(MTRRphysMask_MSR(index), lo, hi);
6702ec1df41SThomas Gleixner 
6712ec1df41SThomas Gleixner 	if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL)
6722ec1df41SThomas Gleixner 	    || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
6732ec1df41SThomas Gleixner 		(hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
6742ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
6752d2ee8deSPaul Jimenez 		changed = true;
6762ec1df41SThomas Gleixner 	}
6772ec1df41SThomas Gleixner 	return changed;
6782ec1df41SThomas Gleixner }
6792ec1df41SThomas Gleixner 
6802ec1df41SThomas Gleixner static u32 deftype_lo, deftype_hi;
6812ec1df41SThomas Gleixner 
6821d3381ebSRandy Dunlap /**
6831d3381ebSRandy Dunlap  * set_mtrr_state - Set the MTRR state for this CPU.
6841d3381ebSRandy Dunlap  *
68501c97c73SJuergen Gross  * NOTE: The CPU must already be in a safe state for MTRR changes, including
68601c97c73SJuergen Gross  *       measures that only a single CPU can be active in set_mtrr_state() in
68701c97c73SJuergen Gross  *       order to not be subject to races for usage of deftype_lo. This is
688*d5f66d5dSJuergen Gross  *       accomplished by taking cache_disable_lock.
6891d3381ebSRandy Dunlap  * RETURNS: 0 if no changes made, else a mask indicating what was changed.
6902ec1df41SThomas Gleixner  */
6911d3381ebSRandy Dunlap static unsigned long set_mtrr_state(void)
6922ec1df41SThomas Gleixner {
6932ec1df41SThomas Gleixner 	unsigned long change_mask = 0;
694a1a499a3SJaswinder Singh Rajput 	unsigned int i;
6952ec1df41SThomas Gleixner 
696a1a499a3SJaswinder Singh Rajput 	for (i = 0; i < num_var_ranges; i++) {
6972ec1df41SThomas Gleixner 		if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
6982ec1df41SThomas Gleixner 			change_mask |= MTRR_CHANGE_MASK_VARIABLE;
699a1a499a3SJaswinder Singh Rajput 	}
7002ec1df41SThomas Gleixner 
7012ec1df41SThomas Gleixner 	if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
7022ec1df41SThomas Gleixner 		change_mask |= MTRR_CHANGE_MASK_FIXED;
7032ec1df41SThomas Gleixner 
704a1a499a3SJaswinder Singh Rajput 	/*
705a1a499a3SJaswinder Singh Rajput 	 * Set_mtrr_restore restores the old value of MTRRdefType,
706a1a499a3SJaswinder Singh Rajput 	 * so to set it we fiddle with the saved value:
707a1a499a3SJaswinder Singh Rajput 	 */
7082ec1df41SThomas Gleixner 	if ((deftype_lo & 0xff) != mtrr_state.def_type
7092ec1df41SThomas Gleixner 	    || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) {
710a1a499a3SJaswinder Singh Rajput 
711a1a499a3SJaswinder Singh Rajput 		deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type |
712a1a499a3SJaswinder Singh Rajput 			     (mtrr_state.enabled << 10);
7132ec1df41SThomas Gleixner 		change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
7142ec1df41SThomas Gleixner 	}
7152ec1df41SThomas Gleixner 
7162ec1df41SThomas Gleixner 	return change_mask;
7172ec1df41SThomas Gleixner }
7182ec1df41SThomas Gleixner 
7192ec1df41SThomas Gleixner /*
720*d5f66d5dSJuergen Gross  * Disable and enable caches. Needed for changing MTRRs and the PAT MSR.
721*d5f66d5dSJuergen Gross  *
722a1a499a3SJaswinder Singh Rajput  * Since we are disabling the cache don't allow any interrupts,
723a1a499a3SJaswinder Singh Rajput  * they would run extremely slow and would only increase the pain.
724a1a499a3SJaswinder Singh Rajput  *
725a1a499a3SJaswinder Singh Rajput  * The caller must ensure that local interrupts are disabled and
726*d5f66d5dSJuergen Gross  * are reenabled after cache_enable() has been called.
7272ec1df41SThomas Gleixner  */
728*d5f66d5dSJuergen Gross static unsigned long saved_cr4;
729*d5f66d5dSJuergen Gross static DEFINE_RAW_SPINLOCK(cache_disable_lock);
730*d5f66d5dSJuergen Gross 
731*d5f66d5dSJuergen Gross void cache_disable(void) __acquires(cache_disable_lock)
7322ec1df41SThomas Gleixner {
7332ec1df41SThomas Gleixner 	unsigned long cr0;
7342ec1df41SThomas Gleixner 
735a1a499a3SJaswinder Singh Rajput 	/*
736a1a499a3SJaswinder Singh Rajput 	 * Note that this is not ideal
737a1a499a3SJaswinder Singh Rajput 	 * since the cache is only flushed/disabled for this CPU while the
738a1a499a3SJaswinder Singh Rajput 	 * MTRRs are changed, but changing this requires more invasive
739a1a499a3SJaswinder Singh Rajput 	 * changes to the way the kernel boots
740a1a499a3SJaswinder Singh Rajput 	 */
7412ec1df41SThomas Gleixner 
742*d5f66d5dSJuergen Gross 	raw_spin_lock(&cache_disable_lock);
7432ec1df41SThomas Gleixner 
7442ec1df41SThomas Gleixner 	/* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
7457ebad705SDave Jones 	cr0 = read_cr0() | X86_CR0_CD;
7462ec1df41SThomas Gleixner 	write_cr0(cr0);
747fd329f27SRicardo Neri 
748fd329f27SRicardo Neri 	/*
749fd329f27SRicardo Neri 	 * Cache flushing is the most time-consuming step when programming
750fd329f27SRicardo Neri 	 * the MTRRs. Fortunately, as per the Intel Software Development
751fd329f27SRicardo Neri 	 * Manual, we can skip it if the processor supports cache self-
752fd329f27SRicardo Neri 	 * snooping.
753fd329f27SRicardo Neri 	 */
754fd329f27SRicardo Neri 	if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
7552ec1df41SThomas Gleixner 		wbinvd();
7562ec1df41SThomas Gleixner 
7572ec1df41SThomas Gleixner 	/* Save value of CR4 and clear Page Global Enable (bit 7) */
758c109bf95SBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_PGE)) {
759*d5f66d5dSJuergen Gross 		saved_cr4 = __read_cr4();
760*d5f66d5dSJuergen Gross 		__write_cr4(saved_cr4 & ~X86_CR4_PGE);
7612ec1df41SThomas Gleixner 	}
7622ec1df41SThomas Gleixner 
7632ec1df41SThomas Gleixner 	/* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */
764ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
7652faf153bSThomas Gleixner 	flush_tlb_local();
7662ec1df41SThomas Gleixner 
7672ec1df41SThomas Gleixner 	/* Save MTRR state */
76852650257SJaswinder Singh Rajput 	rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
7692ec1df41SThomas Gleixner 
7702ec1df41SThomas Gleixner 	/* Disable MTRRs, and set the default type to uncached */
77152650257SJaswinder Singh Rajput 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
772fd329f27SRicardo Neri 
773fd329f27SRicardo Neri 	/* Again, only flush caches if we have to. */
774fd329f27SRicardo Neri 	if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
7758dbf4a30SAjaykumar Hotchandani 		wbinvd();
7762ec1df41SThomas Gleixner }
7772ec1df41SThomas Gleixner 
778*d5f66d5dSJuergen Gross void cache_enable(void) __releases(cache_disable_lock)
7792ec1df41SThomas Gleixner {
7802ec1df41SThomas Gleixner 	/* Flush TLBs (no need to flush caches - they are disabled) */
781ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
7822faf153bSThomas Gleixner 	flush_tlb_local();
7832ec1df41SThomas Gleixner 
7842ec1df41SThomas Gleixner 	/* Intel (P6) standard MTRRs */
78552650257SJaswinder Singh Rajput 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
7862ec1df41SThomas Gleixner 
7872ec1df41SThomas Gleixner 	/* Enable caches */
788a3d7b7ddSH. Peter Anvin 	write_cr0(read_cr0() & ~X86_CR0_CD);
7892ec1df41SThomas Gleixner 
7902ec1df41SThomas Gleixner 	/* Restore value of CR4 */
791c109bf95SBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_PGE))
792*d5f66d5dSJuergen Gross 		__write_cr4(saved_cr4);
793*d5f66d5dSJuergen Gross 	raw_spin_unlock(&cache_disable_lock);
7942ec1df41SThomas Gleixner }
7952ec1df41SThomas Gleixner 
7962ec1df41SThomas Gleixner static void generic_set_all(void)
7972ec1df41SThomas Gleixner {
7982ec1df41SThomas Gleixner 	unsigned long mask, count;
7992ec1df41SThomas Gleixner 	unsigned long flags;
8002ec1df41SThomas Gleixner 
8012ec1df41SThomas Gleixner 	local_irq_save(flags);
802*d5f66d5dSJuergen Gross 	cache_disable();
8032ec1df41SThomas Gleixner 
8042ec1df41SThomas Gleixner 	/* Actually set the state */
8052ec1df41SThomas Gleixner 	mask = set_mtrr_state();
8062ec1df41SThomas Gleixner 
8072e5d9c85Svenkatesh.pallipadi@intel.com 	/* also set PAT */
8082e5d9c85Svenkatesh.pallipadi@intel.com 	pat_init();
8092e5d9c85Svenkatesh.pallipadi@intel.com 
810*d5f66d5dSJuergen Gross 	cache_enable();
8112ec1df41SThomas Gleixner 	local_irq_restore(flags);
8122ec1df41SThomas Gleixner 
8132ec1df41SThomas Gleixner 	/* Use the atomic bitops to update the global mask */
8140e96f31eSJordan Borgner 	for (count = 0; count < sizeof(mask) * 8; ++count) {
8152ec1df41SThomas Gleixner 		if (mask & 0x01)
8162ec1df41SThomas Gleixner 			set_bit(count, &smp_changes_mask);
8172ec1df41SThomas Gleixner 		mask >>= 1;
8182ec1df41SThomas Gleixner 	}
8192ec1df41SThomas Gleixner 
8202ec1df41SThomas Gleixner }
8212ec1df41SThomas Gleixner 
822a1a499a3SJaswinder Singh Rajput /**
823a1a499a3SJaswinder Singh Rajput  * generic_set_mtrr - set variable MTRR register on the local CPU.
824a1a499a3SJaswinder Singh Rajput  *
825a1a499a3SJaswinder Singh Rajput  * @reg: The register to set.
826a1a499a3SJaswinder Singh Rajput  * @base: The base address of the region.
827a1a499a3SJaswinder Singh Rajput  * @size: The size of the region. If this is 0 the region is disabled.
828a1a499a3SJaswinder Singh Rajput  * @type: The type of the region.
829a1a499a3SJaswinder Singh Rajput  *
830a1a499a3SJaswinder Singh Rajput  * Returns nothing.
831a1a499a3SJaswinder Singh Rajput  */
8322ec1df41SThomas Gleixner static void generic_set_mtrr(unsigned int reg, unsigned long base,
8332ec1df41SThomas Gleixner 			     unsigned long size, mtrr_type type)
8342ec1df41SThomas Gleixner {
8352ec1df41SThomas Gleixner 	unsigned long flags;
8362ec1df41SThomas Gleixner 	struct mtrr_var_range *vr;
8372ec1df41SThomas Gleixner 
8382ec1df41SThomas Gleixner 	vr = &mtrr_state.var_ranges[reg];
8392ec1df41SThomas Gleixner 
8402ec1df41SThomas Gleixner 	local_irq_save(flags);
841*d5f66d5dSJuergen Gross 	cache_disable();
8422ec1df41SThomas Gleixner 
8432ec1df41SThomas Gleixner 	if (size == 0) {
844a1a499a3SJaswinder Singh Rajput 		/*
845a1a499a3SJaswinder Singh Rajput 		 * The invalid bit is kept in the mask, so we simply
846a1a499a3SJaswinder Singh Rajput 		 * clear the relevant mask register to disable a range.
847a1a499a3SJaswinder Singh Rajput 		 */
8482ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
8492ec1df41SThomas Gleixner 		memset(vr, 0, sizeof(struct mtrr_var_range));
8502ec1df41SThomas Gleixner 	} else {
8512ec1df41SThomas Gleixner 		vr->base_lo = base << PAGE_SHIFT | type;
8522ec1df41SThomas Gleixner 		vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT);
8532ec1df41SThomas Gleixner 		vr->mask_lo = -size << PAGE_SHIFT | 0x800;
8542ec1df41SThomas Gleixner 		vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT);
8552ec1df41SThomas Gleixner 
8562ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
8572ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
8582ec1df41SThomas Gleixner 	}
8592ec1df41SThomas Gleixner 
860*d5f66d5dSJuergen Gross 	cache_enable();
8612ec1df41SThomas Gleixner 	local_irq_restore(flags);
8622ec1df41SThomas Gleixner }
8632ec1df41SThomas Gleixner 
864a1a499a3SJaswinder Singh Rajput int generic_validate_add_page(unsigned long base, unsigned long size,
865a1a499a3SJaswinder Singh Rajput 			      unsigned int type)
8662ec1df41SThomas Gleixner {
8672ec1df41SThomas Gleixner 	unsigned long lbase, last;
8682ec1df41SThomas Gleixner 
869a1a499a3SJaswinder Singh Rajput 	/*
870a1a499a3SJaswinder Singh Rajput 	 * For Intel PPro stepping <= 7
871a1a499a3SJaswinder Singh Rajput 	 * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
872a1a499a3SJaswinder Singh Rajput 	 */
8732ec1df41SThomas Gleixner 	if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 &&
8742ec1df41SThomas Gleixner 	    boot_cpu_data.x86_model == 1 &&
875b399151cSJia Zhang 	    boot_cpu_data.x86_stepping <= 7) {
8762ec1df41SThomas Gleixner 		if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
8771b74dde7SChen Yucong 			pr_warn("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
8782ec1df41SThomas Gleixner 			return -EINVAL;
8792ec1df41SThomas Gleixner 		}
8802ec1df41SThomas Gleixner 		if (!(base + size < 0x70000 || base > 0x7003F) &&
8812ec1df41SThomas Gleixner 		    (type == MTRR_TYPE_WRCOMB
8822ec1df41SThomas Gleixner 		     || type == MTRR_TYPE_WRBACK)) {
8831b74dde7SChen Yucong 			pr_warn("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
8842ec1df41SThomas Gleixner 			return -EINVAL;
8852ec1df41SThomas Gleixner 		}
8862ec1df41SThomas Gleixner 	}
8872ec1df41SThomas Gleixner 
888a1a499a3SJaswinder Singh Rajput 	/*
889a1a499a3SJaswinder Singh Rajput 	 * Check upper bits of base and last are equal and lower bits are 0
890a1a499a3SJaswinder Singh Rajput 	 * for base and 1 for last
891a1a499a3SJaswinder Singh Rajput 	 */
8922ec1df41SThomas Gleixner 	last = base + size - 1;
8932ec1df41SThomas Gleixner 	for (lbase = base; !(lbase & 1) && (last & 1);
894a1a499a3SJaswinder Singh Rajput 	     lbase = lbase >> 1, last = last >> 1)
895a1a499a3SJaswinder Singh Rajput 		;
8962ec1df41SThomas Gleixner 	if (lbase != last) {
8971b74dde7SChen Yucong 		pr_warn("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
8982ec1df41SThomas Gleixner 		return -EINVAL;
8992ec1df41SThomas Gleixner 	}
9002ec1df41SThomas Gleixner 	return 0;
9012ec1df41SThomas Gleixner }
9022ec1df41SThomas Gleixner 
9032ec1df41SThomas Gleixner static int generic_have_wrcomb(void)
9042ec1df41SThomas Gleixner {
9052ec1df41SThomas Gleixner 	unsigned long config, dummy;
906d9bcc01dSJaswinder Singh Rajput 	rdmsr(MSR_MTRRcap, config, dummy);
907a1a499a3SJaswinder Singh Rajput 	return config & (1 << 10);
9082ec1df41SThomas Gleixner }
9092ec1df41SThomas Gleixner 
9102ec1df41SThomas Gleixner int positive_have_wrcomb(void)
9112ec1df41SThomas Gleixner {
9122ec1df41SThomas Gleixner 	return 1;
9132ec1df41SThomas Gleixner }
9142ec1df41SThomas Gleixner 
915a1a499a3SJaswinder Singh Rajput /*
916a1a499a3SJaswinder Singh Rajput  * Generic structure...
9172ec1df41SThomas Gleixner  */
9183b9cfc0aSEmese Revfy const struct mtrr_ops generic_mtrr_ops = {
9192ec1df41SThomas Gleixner 	.set_all		= generic_set_all,
9202ec1df41SThomas Gleixner 	.get			= generic_get_mtrr,
9212ec1df41SThomas Gleixner 	.get_free_region	= generic_get_free_region,
9222ec1df41SThomas Gleixner 	.set			= generic_set_mtrr,
9232ec1df41SThomas Gleixner 	.validate_add_page	= generic_validate_add_page,
9242ec1df41SThomas Gleixner 	.have_wrcomb		= generic_have_wrcomb,
9252ec1df41SThomas Gleixner };
926