xref: /linux/arch/x86/kernel/cpu/mtrr/generic.c (revision 9b61b2069681b60d0d0bedbd0fe3c70123dddb19)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a1a499a3SJaswinder Singh Rajput /*
3a1a499a3SJaswinder Singh Rajput  * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong
4a1a499a3SJaswinder Singh Rajput  * because MTRRs can span up to 40 bits (36bits on most modern x86)
5a1a499a3SJaswinder Singh Rajput  */
6a1a499a3SJaswinder Singh Rajput 
7186f4360SPaul Gortmaker #include <linux/export.h>
82ec1df41SThomas Gleixner #include <linux/init.h>
9a1a499a3SJaswinder Singh Rajput #include <linux/io.h>
102ec1df41SThomas Gleixner #include <linux/mm.h>
1129055dc7SJuergen Gross #include <linux/cc_platform.h>
12a1a499a3SJaswinder Singh Rajput #include <asm/processor-flags.h>
13d5f66d5dSJuergen Gross #include <asm/cacheinfo.h>
14a1a499a3SJaswinder Singh Rajput #include <asm/cpufeature.h>
1529055dc7SJuergen Gross #include <asm/hypervisor.h>
1629055dc7SJuergen Gross #include <asm/mshyperv.h>
17a1a499a3SJaswinder Singh Rajput #include <asm/tlbflush.h>
182ec1df41SThomas Gleixner #include <asm/mtrr.h>
192ec1df41SThomas Gleixner #include <asm/msr.h>
20eb243d1dSIngo Molnar #include <asm/memtype.h>
21a1a499a3SJaswinder Singh Rajput 
222ec1df41SThomas Gleixner #include "mtrr.h"
232ec1df41SThomas Gleixner 
242ec1df41SThomas Gleixner struct fixed_range_block {
252ec1df41SThomas Gleixner 	int base_msr;		/* start address of an MTRR block */
262ec1df41SThomas Gleixner 	int ranges;		/* number of MTRRs in this block  */
272ec1df41SThomas Gleixner };
282ec1df41SThomas Gleixner 
292ec1df41SThomas Gleixner static struct fixed_range_block fixed_range_blocks[] = {
30a036c7a3SJaswinder Singh Rajput 	{ MSR_MTRRfix64K_00000, 1 }, /* one   64k MTRR  */
317d9d55e4SJaswinder Singh Rajput 	{ MSR_MTRRfix16K_80000, 2 }, /* two   16k MTRRs */
32ba5673ffSJaswinder Singh Rajput 	{ MSR_MTRRfix4K_C0000,  8 }, /* eight  4k MTRRs */
332ec1df41SThomas Gleixner 	{}
342ec1df41SThomas Gleixner };
352ec1df41SThomas Gleixner 
36061b984aSJuergen Gross struct cache_map {
37061b984aSJuergen Gross 	u64 start;
38061b984aSJuergen Gross 	u64 end;
39061b984aSJuergen Gross 	u64 flags;
40061b984aSJuergen Gross 	u64 type:8;
41061b984aSJuergen Gross 	u64 fixed:1;
42061b984aSJuergen Gross };
43061b984aSJuergen Gross 
447c1dee73SBorislav Petkov (AMD) bool mtrr_debug;
45a4316603SJuergen Gross 
mtrr_param_setup(char * str)46a4316603SJuergen Gross static int __init mtrr_param_setup(char *str)
47a4316603SJuergen Gross {
48a4316603SJuergen Gross 	int rc = 0;
49a4316603SJuergen Gross 
50a4316603SJuergen Gross 	if (!str)
51a4316603SJuergen Gross 		return -EINVAL;
52a4316603SJuergen Gross 	if (!strcmp(str, "debug"))
53a4316603SJuergen Gross 		mtrr_debug = true;
54a4316603SJuergen Gross 	else
55a4316603SJuergen Gross 		rc = -EINVAL;
56a4316603SJuergen Gross 
57a4316603SJuergen Gross 	return rc;
58a4316603SJuergen Gross }
59a4316603SJuergen Gross early_param("mtrr", mtrr_param_setup);
60a4316603SJuergen Gross 
61061b984aSJuergen Gross /*
62061b984aSJuergen Gross  * CACHE_MAP_MAX is the maximum number of memory ranges in cache_map, where
63061b984aSJuergen Gross  * no 2 adjacent ranges have the same cache mode (those would be merged).
64061b984aSJuergen Gross  * The number is based on the worst case:
65061b984aSJuergen Gross  * - no two adjacent fixed MTRRs share the same cache mode
66061b984aSJuergen Gross  * - one variable MTRR is spanning a huge area with mode WB
67061b984aSJuergen Gross  * - 255 variable MTRRs with mode UC all overlap with the WB MTRR, creating 2
68061b984aSJuergen Gross  *   additional ranges each (result like "ababababa...aba" with a = WB, b = UC),
69061b984aSJuergen Gross  *   accounting for MTRR_MAX_VAR_RANGES * 2 - 1 range entries
70061b984aSJuergen Gross  * - a TOP_MEM2 area (even with overlapping an UC MTRR can't add 2 range entries
71061b984aSJuergen Gross  *   to the possible maximum, as it always starts at 4GB, thus it can't be in
72061b984aSJuergen Gross  *   the middle of that MTRR, unless that MTRR starts at 0, which would remove
73061b984aSJuergen Gross  *   the initial "a" from the "abababa" pattern above)
74061b984aSJuergen Gross  * The map won't contain ranges with no matching MTRR (those fall back to the
75061b984aSJuergen Gross  * default cache mode).
76061b984aSJuergen Gross  */
77061b984aSJuergen Gross #define CACHE_MAP_MAX	(MTRR_NUM_FIXED_RANGES + MTRR_MAX_VAR_RANGES * 2)
78061b984aSJuergen Gross 
79061b984aSJuergen Gross static struct cache_map init_cache_map[CACHE_MAP_MAX] __initdata;
80061b984aSJuergen Gross static struct cache_map *cache_map __refdata = init_cache_map;
81061b984aSJuergen Gross static unsigned int cache_map_size = CACHE_MAP_MAX;
82061b984aSJuergen Gross static unsigned int cache_map_n;
83061b984aSJuergen Gross static unsigned int cache_map_fixed;
84061b984aSJuergen Gross 
852ec1df41SThomas Gleixner static unsigned long smp_changes_mask;
862e5d9c85Svenkatesh.pallipadi@intel.com static int mtrr_state_set;
8795ffa243SYinghai Lu u64 mtrr_tom2;
882ec1df41SThomas Gleixner 
89a1a499a3SJaswinder Singh Rajput struct mtrr_state_type mtrr_state;
90932d27a7SSheng Yang EXPORT_SYMBOL_GPL(mtrr_state);
91932d27a7SSheng Yang 
92d053b481SJuergen Gross /* Reserved bits in the high portion of the MTRRphysBaseN MSR. */
93d053b481SJuergen Gross u32 phys_hi_rsvd;
94f6b98064SJuergen Gross 
95a1a499a3SJaswinder Singh Rajput /*
963ff42da5SAndreas Herrmann  * BIOS is expected to clear MtrrFixDramModEn bit, see for example
973ff42da5SAndreas Herrmann  * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
983ff42da5SAndreas Herrmann  * Opteron Processors" (26094 Rev. 3.30 February 2006), section
993ff42da5SAndreas Herrmann  * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
1006a6256f9SAdam Buchbinder  * to 1 during BIOS initialization of the fixed MTRRs, then cleared to
1013ff42da5SAndreas Herrmann  * 0 for operation."
1023ff42da5SAndreas Herrmann  */
k8_check_syscfg_dram_mod_en(void)1033ff42da5SAndreas Herrmann static inline void k8_check_syscfg_dram_mod_en(void)
1043ff42da5SAndreas Herrmann {
1053ff42da5SAndreas Herrmann 	u32 lo, hi;
1063ff42da5SAndreas Herrmann 
1073ff42da5SAndreas Herrmann 	if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
1083ff42da5SAndreas Herrmann 	      (boot_cpu_data.x86 >= 0x0f)))
1093ff42da5SAndreas Herrmann 		return;
1103ff42da5SAndreas Herrmann 
111*0ecaefb3SBorislav Petkov (AMD) 	if (cc_platform_has(CC_ATTR_HOST_SEV_SNP))
112e3fd08afSAshish Kalra 		return;
113e3fd08afSAshish Kalra 
114059e5c32SBrijesh Singh 	rdmsr(MSR_AMD64_SYSCFG, lo, hi);
1153ff42da5SAndreas Herrmann 	if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
1161b74dde7SChen Yucong 		pr_err(FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
1173ff42da5SAndreas Herrmann 		       " not cleared by BIOS, clearing this bit\n",
1183ff42da5SAndreas Herrmann 		       smp_processor_id());
1193ff42da5SAndreas Herrmann 		lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
120059e5c32SBrijesh Singh 		mtrr_wrmsr(MSR_AMD64_SYSCFG, lo, hi);
1213ff42da5SAndreas Herrmann 	}
1223ff42da5SAndreas Herrmann }
1233ff42da5SAndreas Herrmann 
124351e5a70SVenkatesh Pallipadi /* Get the size of contiguous MTRR range */
get_mtrr_size(u64 mask)125351e5a70SVenkatesh Pallipadi static u64 get_mtrr_size(u64 mask)
126351e5a70SVenkatesh Pallipadi {
127351e5a70SVenkatesh Pallipadi 	u64 size;
128351e5a70SVenkatesh Pallipadi 
129d053b481SJuergen Gross 	mask |= (u64)phys_hi_rsvd << 32;
130351e5a70SVenkatesh Pallipadi 	size = -mask;
131d053b481SJuergen Gross 
132351e5a70SVenkatesh Pallipadi 	return size;
133351e5a70SVenkatesh Pallipadi }
134351e5a70SVenkatesh Pallipadi 
get_var_mtrr_state(unsigned int reg,u64 * start,u64 * size)135061b984aSJuergen Gross static u8 get_var_mtrr_state(unsigned int reg, u64 *start, u64 *size)
136061b984aSJuergen Gross {
137061b984aSJuergen Gross 	struct mtrr_var_range *mtrr = mtrr_state.var_ranges + reg;
138061b984aSJuergen Gross 
139061b984aSJuergen Gross 	if (!(mtrr->mask_lo & MTRR_PHYSMASK_V))
140061b984aSJuergen Gross 		return MTRR_TYPE_INVALID;
141061b984aSJuergen Gross 
142061b984aSJuergen Gross 	*start = (((u64)mtrr->base_hi) << 32) + (mtrr->base_lo & PAGE_MASK);
143061b984aSJuergen Gross 	*size = get_mtrr_size((((u64)mtrr->mask_hi) << 32) +
144061b984aSJuergen Gross 			      (mtrr->mask_lo & PAGE_MASK));
145061b984aSJuergen Gross 
146061b984aSJuergen Gross 	return mtrr->base_lo & MTRR_PHYSBASE_TYPE;
147061b984aSJuergen Gross }
148061b984aSJuergen Gross 
get_effective_type(u8 type1,u8 type2)1491ca12099SJuergen Gross static u8 get_effective_type(u8 type1, u8 type2)
1501ca12099SJuergen Gross {
1511ca12099SJuergen Gross 	if (type1 == MTRR_TYPE_UNCACHABLE || type2 == MTRR_TYPE_UNCACHABLE)
1521ca12099SJuergen Gross 		return MTRR_TYPE_UNCACHABLE;
1531ca12099SJuergen Gross 
1541ca12099SJuergen Gross 	if ((type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH) ||
1551ca12099SJuergen Gross 	    (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK))
1561ca12099SJuergen Gross 		return MTRR_TYPE_WRTHROUGH;
1571ca12099SJuergen Gross 
1581ca12099SJuergen Gross 	if (type1 != type2)
1591ca12099SJuergen Gross 		return MTRR_TYPE_UNCACHABLE;
1601ca12099SJuergen Gross 
1611ca12099SJuergen Gross 	return type1;
1621ca12099SJuergen Gross }
1631ca12099SJuergen Gross 
rm_map_entry_at(int idx)164061b984aSJuergen Gross static void rm_map_entry_at(int idx)
165061b984aSJuergen Gross {
166061b984aSJuergen Gross 	cache_map_n--;
167061b984aSJuergen Gross 	if (cache_map_n > idx) {
168061b984aSJuergen Gross 		memmove(cache_map + idx, cache_map + idx + 1,
169061b984aSJuergen Gross 			sizeof(*cache_map) * (cache_map_n - idx));
170061b984aSJuergen Gross 	}
171061b984aSJuergen Gross }
172061b984aSJuergen Gross 
173061b984aSJuergen Gross /*
174061b984aSJuergen Gross  * Add an entry into cache_map at a specific index.  Merges adjacent entries if
175061b984aSJuergen Gross  * appropriate.  Return the number of merges for correcting the scan index
176061b984aSJuergen Gross  * (this is needed as merging will reduce the number of entries, which will
177061b984aSJuergen Gross  * result in skipping entries in future iterations if the scan index isn't
178061b984aSJuergen Gross  * corrected).
179061b984aSJuergen Gross  * Note that the corrected index can never go below -1 (resulting in being 0 in
180061b984aSJuergen Gross  * the next scan iteration), as "2" is returned only if the current index is
181061b984aSJuergen Gross  * larger than zero.
182061b984aSJuergen Gross  */
add_map_entry_at(u64 start,u64 end,u8 type,int idx)183061b984aSJuergen Gross static int add_map_entry_at(u64 start, u64 end, u8 type, int idx)
184061b984aSJuergen Gross {
185061b984aSJuergen Gross 	bool merge_prev = false, merge_next = false;
186061b984aSJuergen Gross 
187061b984aSJuergen Gross 	if (start >= end)
188061b984aSJuergen Gross 		return 0;
189061b984aSJuergen Gross 
190061b984aSJuergen Gross 	if (idx > 0) {
191061b984aSJuergen Gross 		struct cache_map *prev = cache_map + idx - 1;
192061b984aSJuergen Gross 
193061b984aSJuergen Gross 		if (!prev->fixed && start == prev->end && type == prev->type)
194061b984aSJuergen Gross 			merge_prev = true;
195061b984aSJuergen Gross 	}
196061b984aSJuergen Gross 
197061b984aSJuergen Gross 	if (idx < cache_map_n) {
198061b984aSJuergen Gross 		struct cache_map *next = cache_map + idx;
199061b984aSJuergen Gross 
200061b984aSJuergen Gross 		if (!next->fixed && end == next->start && type == next->type)
201061b984aSJuergen Gross 			merge_next = true;
202061b984aSJuergen Gross 	}
203061b984aSJuergen Gross 
204061b984aSJuergen Gross 	if (merge_prev && merge_next) {
205061b984aSJuergen Gross 		cache_map[idx - 1].end = cache_map[idx].end;
206061b984aSJuergen Gross 		rm_map_entry_at(idx);
207061b984aSJuergen Gross 		return 2;
208061b984aSJuergen Gross 	}
209061b984aSJuergen Gross 	if (merge_prev) {
210061b984aSJuergen Gross 		cache_map[idx - 1].end = end;
211061b984aSJuergen Gross 		return 1;
212061b984aSJuergen Gross 	}
213061b984aSJuergen Gross 	if (merge_next) {
214061b984aSJuergen Gross 		cache_map[idx].start = start;
215061b984aSJuergen Gross 		return 1;
216061b984aSJuergen Gross 	}
217061b984aSJuergen Gross 
218061b984aSJuergen Gross 	/* Sanity check: the array should NEVER be too small! */
219061b984aSJuergen Gross 	if (cache_map_n == cache_map_size) {
220061b984aSJuergen Gross 		WARN(1, "MTRR cache mode memory map exhausted!\n");
221061b984aSJuergen Gross 		cache_map_n = cache_map_fixed;
222061b984aSJuergen Gross 		return 0;
223061b984aSJuergen Gross 	}
224061b984aSJuergen Gross 
225061b984aSJuergen Gross 	if (cache_map_n > idx) {
226061b984aSJuergen Gross 		memmove(cache_map + idx + 1, cache_map + idx,
227061b984aSJuergen Gross 			sizeof(*cache_map) * (cache_map_n - idx));
228061b984aSJuergen Gross 	}
229061b984aSJuergen Gross 
230061b984aSJuergen Gross 	cache_map[idx].start = start;
231061b984aSJuergen Gross 	cache_map[idx].end = end;
232061b984aSJuergen Gross 	cache_map[idx].type = type;
233061b984aSJuergen Gross 	cache_map[idx].fixed = 0;
234061b984aSJuergen Gross 	cache_map_n++;
235061b984aSJuergen Gross 
236061b984aSJuergen Gross 	return 0;
237061b984aSJuergen Gross }
238061b984aSJuergen Gross 
239061b984aSJuergen Gross /* Clear a part of an entry. Return 1 if start of entry is still valid. */
clr_map_range_at(u64 start,u64 end,int idx)240061b984aSJuergen Gross static int clr_map_range_at(u64 start, u64 end, int idx)
241061b984aSJuergen Gross {
242061b984aSJuergen Gross 	int ret = start != cache_map[idx].start;
243061b984aSJuergen Gross 	u64 tmp;
244061b984aSJuergen Gross 
245061b984aSJuergen Gross 	if (start == cache_map[idx].start && end == cache_map[idx].end) {
246061b984aSJuergen Gross 		rm_map_entry_at(idx);
247061b984aSJuergen Gross 	} else if (start == cache_map[idx].start) {
248061b984aSJuergen Gross 		cache_map[idx].start = end;
249061b984aSJuergen Gross 	} else if (end == cache_map[idx].end) {
250061b984aSJuergen Gross 		cache_map[idx].end = start;
251061b984aSJuergen Gross 	} else {
252061b984aSJuergen Gross 		tmp = cache_map[idx].end;
253061b984aSJuergen Gross 		cache_map[idx].end = start;
254061b984aSJuergen Gross 		add_map_entry_at(end, tmp, cache_map[idx].type, idx + 1);
255061b984aSJuergen Gross 	}
256061b984aSJuergen Gross 
257061b984aSJuergen Gross 	return ret;
258061b984aSJuergen Gross }
259061b984aSJuergen Gross 
260061b984aSJuergen Gross /*
261061b984aSJuergen Gross  * Add MTRR to the map.  The current map is scanned and each part of the MTRR
262061b984aSJuergen Gross  * either overlapping with an existing entry or with a hole in the map is
263061b984aSJuergen Gross  * handled separately.
264061b984aSJuergen Gross  */
add_map_entry(u64 start,u64 end,u8 type)265061b984aSJuergen Gross static void add_map_entry(u64 start, u64 end, u8 type)
266061b984aSJuergen Gross {
267061b984aSJuergen Gross 	u8 new_type, old_type;
268061b984aSJuergen Gross 	u64 tmp;
269061b984aSJuergen Gross 	int i;
270061b984aSJuergen Gross 
271061b984aSJuergen Gross 	for (i = 0; i < cache_map_n && start < end; i++) {
272061b984aSJuergen Gross 		if (start >= cache_map[i].end)
273061b984aSJuergen Gross 			continue;
274061b984aSJuergen Gross 
275061b984aSJuergen Gross 		if (start < cache_map[i].start) {
276061b984aSJuergen Gross 			/* Region start has no overlap. */
277061b984aSJuergen Gross 			tmp = min(end, cache_map[i].start);
278061b984aSJuergen Gross 			i -= add_map_entry_at(start, tmp,  type, i);
279061b984aSJuergen Gross 			start = tmp;
280061b984aSJuergen Gross 			continue;
281061b984aSJuergen Gross 		}
282061b984aSJuergen Gross 
283061b984aSJuergen Gross 		new_type = get_effective_type(type, cache_map[i].type);
284061b984aSJuergen Gross 		old_type = cache_map[i].type;
285061b984aSJuergen Gross 
286061b984aSJuergen Gross 		if (cache_map[i].fixed || new_type == old_type) {
287061b984aSJuergen Gross 			/* Cut off start of new entry. */
288061b984aSJuergen Gross 			start = cache_map[i].end;
289061b984aSJuergen Gross 			continue;
290061b984aSJuergen Gross 		}
291061b984aSJuergen Gross 
292061b984aSJuergen Gross 		/* Handle only overlapping part of region. */
293061b984aSJuergen Gross 		tmp = min(end, cache_map[i].end);
294061b984aSJuergen Gross 		i += clr_map_range_at(start, tmp, i);
295061b984aSJuergen Gross 		i -= add_map_entry_at(start, tmp, new_type, i);
296061b984aSJuergen Gross 		start = tmp;
297061b984aSJuergen Gross 	}
298061b984aSJuergen Gross 
299061b984aSJuergen Gross 	/* Add rest of region after last map entry (rest might be empty). */
300061b984aSJuergen Gross 	add_map_entry_at(start, end, type, i);
301061b984aSJuergen Gross }
302061b984aSJuergen Gross 
303061b984aSJuergen Gross /* Add variable MTRRs to cache map. */
map_add_var(void)304061b984aSJuergen Gross static void map_add_var(void)
305061b984aSJuergen Gross {
306061b984aSJuergen Gross 	u64 start, size;
307061b984aSJuergen Gross 	unsigned int i;
308061b984aSJuergen Gross 	u8 type;
309061b984aSJuergen Gross 
310061b984aSJuergen Gross 	/*
311061b984aSJuergen Gross 	 * Add AMD TOP_MEM2 area.  Can't be added in mtrr_build_map(), as it
312061b984aSJuergen Gross 	 * needs to be added again when rebuilding the map due to potentially
313061b984aSJuergen Gross 	 * having moved as a result of variable MTRRs for memory below 4GB.
314061b984aSJuergen Gross 	 */
315061b984aSJuergen Gross 	if (mtrr_tom2) {
316061b984aSJuergen Gross 		add_map_entry(BIT_ULL(32), mtrr_tom2, MTRR_TYPE_WRBACK);
317061b984aSJuergen Gross 		cache_map[cache_map_n - 1].fixed = 1;
318061b984aSJuergen Gross 	}
319061b984aSJuergen Gross 
320061b984aSJuergen Gross 	for (i = 0; i < num_var_ranges; i++) {
321061b984aSJuergen Gross 		type = get_var_mtrr_state(i, &start, &size);
322061b984aSJuergen Gross 		if (type != MTRR_TYPE_INVALID)
323061b984aSJuergen Gross 			add_map_entry(start, start + size, type);
324061b984aSJuergen Gross 	}
325061b984aSJuergen Gross }
326061b984aSJuergen Gross 
327061b984aSJuergen Gross /*
328061b984aSJuergen Gross  * Rebuild map by replacing variable entries.  Needs to be called when MTRR
329061b984aSJuergen Gross  * registers are being changed after boot, as such changes could include
330061b984aSJuergen Gross  * removals of registers, which are complicated to handle without rebuild of
331061b984aSJuergen Gross  * the map.
332061b984aSJuergen Gross  */
generic_rebuild_map(void)333061b984aSJuergen Gross void generic_rebuild_map(void)
334061b984aSJuergen Gross {
335061b984aSJuergen Gross 	if (mtrr_if != &generic_mtrr_ops)
336061b984aSJuergen Gross 		return;
337061b984aSJuergen Gross 
338061b984aSJuergen Gross 	cache_map_n = cache_map_fixed;
339061b984aSJuergen Gross 
340061b984aSJuergen Gross 	map_add_var();
341061b984aSJuergen Gross }
342061b984aSJuergen Gross 
get_cache_map_size(void)343061b984aSJuergen Gross static unsigned int __init get_cache_map_size(void)
344061b984aSJuergen Gross {
345061b984aSJuergen Gross 	return cache_map_fixed + 2 * num_var_ranges + (mtrr_tom2 != 0);
346061b984aSJuergen Gross }
347061b984aSJuergen Gross 
348061b984aSJuergen Gross /* Build the cache_map containing the cache modes per memory range. */
mtrr_build_map(void)349061b984aSJuergen Gross void __init mtrr_build_map(void)
350061b984aSJuergen Gross {
351061b984aSJuergen Gross 	u64 start, end, size;
352061b984aSJuergen Gross 	unsigned int i;
353061b984aSJuergen Gross 	u8 type;
354061b984aSJuergen Gross 
355061b984aSJuergen Gross 	/* Add fixed MTRRs, optimize for adjacent entries with same type. */
356061b984aSJuergen Gross 	if (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED) {
357061b984aSJuergen Gross 		/*
358061b984aSJuergen Gross 		 * Start with 64k size fixed entries, preset 1st one (hence the
359061b984aSJuergen Gross 		 * loop below is starting with index 1).
360061b984aSJuergen Gross 		 */
361061b984aSJuergen Gross 		start = 0;
362061b984aSJuergen Gross 		end = size = 0x10000;
363061b984aSJuergen Gross 		type = mtrr_state.fixed_ranges[0];
364061b984aSJuergen Gross 
365061b984aSJuergen Gross 		for (i = 1; i < MTRR_NUM_FIXED_RANGES; i++) {
366061b984aSJuergen Gross 			/* 8 64k entries, then 16 16k ones, rest 4k. */
367061b984aSJuergen Gross 			if (i == 8 || i == 24)
368061b984aSJuergen Gross 				size >>= 2;
369061b984aSJuergen Gross 
370061b984aSJuergen Gross 			if (mtrr_state.fixed_ranges[i] != type) {
371061b984aSJuergen Gross 				add_map_entry(start, end, type);
372061b984aSJuergen Gross 				start = end;
373061b984aSJuergen Gross 				type = mtrr_state.fixed_ranges[i];
374061b984aSJuergen Gross 			}
375061b984aSJuergen Gross 			end += size;
376061b984aSJuergen Gross 		}
377061b984aSJuergen Gross 		add_map_entry(start, end, type);
378061b984aSJuergen Gross 	}
379061b984aSJuergen Gross 
380061b984aSJuergen Gross 	/* Mark fixed, they take precedence. */
381061b984aSJuergen Gross 	for (i = 0; i < cache_map_n; i++)
382061b984aSJuergen Gross 		cache_map[i].fixed = 1;
383061b984aSJuergen Gross 	cache_map_fixed = cache_map_n;
384061b984aSJuergen Gross 
385061b984aSJuergen Gross 	map_add_var();
386061b984aSJuergen Gross 
387061b984aSJuergen Gross 	pr_info("MTRR map: %u entries (%u fixed + %u variable; max %u), built from %u variable MTRRs\n",
388061b984aSJuergen Gross 		cache_map_n, cache_map_fixed, cache_map_n - cache_map_fixed,
389061b984aSJuergen Gross 		get_cache_map_size(), num_var_ranges + (mtrr_tom2 != 0));
390a4316603SJuergen Gross 
391a4316603SJuergen Gross 	if (mtrr_debug) {
392a4316603SJuergen Gross 		for (i = 0; i < cache_map_n; i++) {
393a4316603SJuergen Gross 			pr_info("%3u: %016llx-%016llx %s\n", i,
394a4316603SJuergen Gross 				cache_map[i].start, cache_map[i].end - 1,
395a4316603SJuergen Gross 				mtrr_attrib_to_str(cache_map[i].type));
396a4316603SJuergen Gross 		}
397a4316603SJuergen Gross 	}
398061b984aSJuergen Gross }
399061b984aSJuergen Gross 
400061b984aSJuergen Gross /* Copy the cache_map from __initdata memory to dynamically allocated one. */
mtrr_copy_map(void)401061b984aSJuergen Gross void __init mtrr_copy_map(void)
402061b984aSJuergen Gross {
403061b984aSJuergen Gross 	unsigned int new_size = get_cache_map_size();
404061b984aSJuergen Gross 
405061b984aSJuergen Gross 	if (!mtrr_state.enabled || !new_size) {
406061b984aSJuergen Gross 		cache_map = NULL;
407061b984aSJuergen Gross 		return;
408061b984aSJuergen Gross 	}
409061b984aSJuergen Gross 
410061b984aSJuergen Gross 	mutex_lock(&mtrr_mutex);
411061b984aSJuergen Gross 
412061b984aSJuergen Gross 	cache_map = kcalloc(new_size, sizeof(*cache_map), GFP_KERNEL);
413061b984aSJuergen Gross 	if (cache_map) {
414061b984aSJuergen Gross 		memmove(cache_map, init_cache_map,
415061b984aSJuergen Gross 			cache_map_n * sizeof(*cache_map));
416061b984aSJuergen Gross 		cache_map_size = new_size;
417061b984aSJuergen Gross 	} else {
418061b984aSJuergen Gross 		mtrr_state.enabled = 0;
419061b984aSJuergen Gross 		pr_err("MTRRs disabled due to allocation failure for lookup map.\n");
420061b984aSJuergen Gross 	}
421061b984aSJuergen Gross 
422061b984aSJuergen Gross 	mutex_unlock(&mtrr_mutex);
423061b984aSJuergen Gross }
424061b984aSJuergen Gross 
4250cc705f5SToshi Kani /**
42629055dc7SJuergen Gross  * mtrr_overwrite_state - set static MTRR state
42729055dc7SJuergen Gross  *
42829055dc7SJuergen Gross  * Used to set MTRR state via different means (e.g. with data obtained from
42929055dc7SJuergen Gross  * a hypervisor).
43029055dc7SJuergen Gross  * Is allowed only for special cases when running virtualized. Must be called
43129055dc7SJuergen Gross  * from the x86_init.hyper.init_platform() hook.  It can be called only once.
43229055dc7SJuergen Gross  * The MTRR state can't be changed afterwards.  To ensure that, X86_FEATURE_MTRR
43329055dc7SJuergen Gross  * is cleared.
4344e15b91cSBorislav Petkov (AMD)  *
4354e15b91cSBorislav Petkov (AMD)  * @var: MTRR variable range array to use
4364e15b91cSBorislav Petkov (AMD)  * @num_var: length of the @var array
4374e15b91cSBorislav Petkov (AMD)  * @def_type: default caching type
43829055dc7SJuergen Gross  */
mtrr_overwrite_state(struct mtrr_var_range * var,unsigned int num_var,mtrr_type def_type)43929055dc7SJuergen Gross void mtrr_overwrite_state(struct mtrr_var_range *var, unsigned int num_var,
44029055dc7SJuergen Gross 			  mtrr_type def_type)
44129055dc7SJuergen Gross {
44229055dc7SJuergen Gross 	unsigned int i;
44329055dc7SJuergen Gross 
44429055dc7SJuergen Gross 	/* Only allowed to be called once before mtrr_bp_init(). */
44529055dc7SJuergen Gross 	if (WARN_ON_ONCE(mtrr_state_set))
44629055dc7SJuergen Gross 		return;
44729055dc7SJuergen Gross 
44829055dc7SJuergen Gross 	/* Only allowed when running virtualized. */
44929055dc7SJuergen Gross 	if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
45029055dc7SJuergen Gross 		return;
45129055dc7SJuergen Gross 
45229055dc7SJuergen Gross 	/*
45329055dc7SJuergen Gross 	 * Only allowed for special virtualization cases:
45429055dc7SJuergen Gross 	 * - when running as Hyper-V, SEV-SNP guest using vTOM
45529055dc7SJuergen Gross 	 * - when running as Xen PV guest
45629055dc7SJuergen Gross 	 * - when running as SEV-SNP or TDX guest to avoid unnecessary
45729055dc7SJuergen Gross 	 *   VMM communication/Virtualization exceptions (#VC, #VE)
45829055dc7SJuergen Gross 	 */
45929055dc7SJuergen Gross 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) &&
46029055dc7SJuergen Gross 	    !hv_is_isolation_supported() &&
46129055dc7SJuergen Gross 	    !cpu_feature_enabled(X86_FEATURE_XENPV) &&
46229055dc7SJuergen Gross 	    !cpu_feature_enabled(X86_FEATURE_TDX_GUEST))
46329055dc7SJuergen Gross 		return;
46429055dc7SJuergen Gross 
46529055dc7SJuergen Gross 	/* Disable MTRR in order to disable MTRR modifications. */
46629055dc7SJuergen Gross 	setup_clear_cpu_cap(X86_FEATURE_MTRR);
46729055dc7SJuergen Gross 
46829055dc7SJuergen Gross 	if (var) {
46929055dc7SJuergen Gross 		if (num_var > MTRR_MAX_VAR_RANGES) {
47029055dc7SJuergen Gross 			pr_warn("Trying to overwrite MTRR state with %u variable entries\n",
47129055dc7SJuergen Gross 				num_var);
47229055dc7SJuergen Gross 			num_var = MTRR_MAX_VAR_RANGES;
47329055dc7SJuergen Gross 		}
47429055dc7SJuergen Gross 		for (i = 0; i < num_var; i++)
47529055dc7SJuergen Gross 			mtrr_state.var_ranges[i] = var[i];
47629055dc7SJuergen Gross 		num_var_ranges = num_var;
47729055dc7SJuergen Gross 	}
47829055dc7SJuergen Gross 
47929055dc7SJuergen Gross 	mtrr_state.def_type = def_type;
48029055dc7SJuergen Gross 	mtrr_state.enabled |= MTRR_STATE_MTRR_ENABLED;
48129055dc7SJuergen Gross 
48229055dc7SJuergen Gross 	mtrr_state_set = 1;
48329055dc7SJuergen Gross }
48429055dc7SJuergen Gross 
type_merge(u8 type,u8 new_type,u8 * uniform)4858227f40aSJuergen Gross static u8 type_merge(u8 type, u8 new_type, u8 *uniform)
4868227f40aSJuergen Gross {
4878227f40aSJuergen Gross 	u8 effective_type;
4888227f40aSJuergen Gross 
4898227f40aSJuergen Gross 	if (type == MTRR_TYPE_INVALID)
4908227f40aSJuergen Gross 		return new_type;
4918227f40aSJuergen Gross 
4928227f40aSJuergen Gross 	effective_type = get_effective_type(type, new_type);
4938227f40aSJuergen Gross 	if (type != effective_type)
4948227f40aSJuergen Gross 		*uniform = 0;
4958227f40aSJuergen Gross 
4968227f40aSJuergen Gross 	return effective_type;
4978227f40aSJuergen Gross }
4988227f40aSJuergen Gross 
49929055dc7SJuergen Gross /**
5000cc705f5SToshi Kani  * mtrr_type_lookup - look up memory type in MTRR
5010cc705f5SToshi Kani  *
5024e15b91cSBorislav Petkov (AMD)  * @start: Begin of the physical address range
5034e15b91cSBorislav Petkov (AMD)  * @end: End of the physical address range
5044e15b91cSBorislav Petkov (AMD)  * @uniform: output argument:
5054e15b91cSBorislav Petkov (AMD)  *  - 1: the returned MTRR type is valid for the whole region
5064e15b91cSBorislav Petkov (AMD)  *  - 0: otherwise
5074e15b91cSBorislav Petkov (AMD)  *
5080cc705f5SToshi Kani  * Return Values:
5090cc705f5SToshi Kani  * MTRR_TYPE_(type)  - The effective MTRR type for the region
5100cc705f5SToshi Kani  * MTRR_TYPE_INVALID - MTRR is disabled
511351e5a70SVenkatesh Pallipadi  */
mtrr_type_lookup(u64 start,u64 end,u8 * uniform)512b73522e0SToshi Kani u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
513351e5a70SVenkatesh Pallipadi {
5148227f40aSJuergen Gross 	u8 type = MTRR_TYPE_INVALID;
5158227f40aSJuergen Gross 	unsigned int i;
516351e5a70SVenkatesh Pallipadi 
5178227f40aSJuergen Gross 	if (!mtrr_state_set) {
5188227f40aSJuergen Gross 		/* Uniformity is unknown. */
5198227f40aSJuergen Gross 		*uniform = 0;
520973df194SJuergen Gross 		return MTRR_TYPE_UNCACHABLE;
5218227f40aSJuergen Gross 	}
5228227f40aSJuergen Gross 
5238227f40aSJuergen Gross 	*uniform = 1;
5240cc705f5SToshi Kani 
5250cc705f5SToshi Kani 	if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED))
526973df194SJuergen Gross 		return MTRR_TYPE_UNCACHABLE;
5270cc705f5SToshi Kani 
5288227f40aSJuergen Gross 	for (i = 0; i < cache_map_n && start < end; i++) {
5298227f40aSJuergen Gross 		/* Region after current map entry? -> continue with next one. */
5308227f40aSJuergen Gross 		if (start >= cache_map[i].end)
5318227f40aSJuergen Gross 			continue;
5328227f40aSJuergen Gross 
5338227f40aSJuergen Gross 		/* Start of region not covered by current map entry? */
5348227f40aSJuergen Gross 		if (start < cache_map[i].start) {
5358227f40aSJuergen Gross 			/* At least some part of region has default type. */
5368227f40aSJuergen Gross 			type = type_merge(type, mtrr_state.def_type, uniform);
5378227f40aSJuergen Gross 			/* End of region not covered, too? -> lookup done. */
5388227f40aSJuergen Gross 			if (end <= cache_map[i].start)
5398227f40aSJuergen Gross 				return type;
540b73522e0SToshi Kani 		}
5410cc705f5SToshi Kani 
5428227f40aSJuergen Gross 		/* At least part of region covered by map entry. */
5438227f40aSJuergen Gross 		type = type_merge(type, cache_map[i].type, uniform);
544351e5a70SVenkatesh Pallipadi 
5458227f40aSJuergen Gross 		start = cache_map[i].end;
546351e5a70SVenkatesh Pallipadi 	}
547351e5a70SVenkatesh Pallipadi 
5488227f40aSJuergen Gross 	/* End of region past last entry in map? -> use default type. */
5498227f40aSJuergen Gross 	if (start < end)
5508227f40aSJuergen Gross 		type = type_merge(type, mtrr_state.def_type, uniform);
5510cc705f5SToshi Kani 
552351e5a70SVenkatesh Pallipadi 	return type;
553351e5a70SVenkatesh Pallipadi }
554351e5a70SVenkatesh Pallipadi 
5552ec1df41SThomas Gleixner /* Get the MSR pair relating to a var range */
5562ec1df41SThomas Gleixner static void
get_mtrr_var_range(unsigned int index,struct mtrr_var_range * vr)5572ec1df41SThomas Gleixner get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
5582ec1df41SThomas Gleixner {
5592ec1df41SThomas Gleixner 	rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
5602ec1df41SThomas Gleixner 	rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
5612ec1df41SThomas Gleixner }
5622ec1df41SThomas Gleixner 
563a1a499a3SJaswinder Singh Rajput /* Fill the MSR pair relating to a var range */
fill_mtrr_var_range(unsigned int index,u32 base_lo,u32 base_hi,u32 mask_lo,u32 mask_hi)56495ffa243SYinghai Lu void fill_mtrr_var_range(unsigned int index,
56595ffa243SYinghai Lu 		u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
56695ffa243SYinghai Lu {
56795ffa243SYinghai Lu 	struct mtrr_var_range *vr;
56895ffa243SYinghai Lu 
56995ffa243SYinghai Lu 	vr = mtrr_state.var_ranges;
57095ffa243SYinghai Lu 
57195ffa243SYinghai Lu 	vr[index].base_lo = base_lo;
57295ffa243SYinghai Lu 	vr[index].base_hi = base_hi;
57395ffa243SYinghai Lu 	vr[index].mask_lo = mask_lo;
57495ffa243SYinghai Lu 	vr[index].mask_hi = mask_hi;
57595ffa243SYinghai Lu }
57695ffa243SYinghai Lu 
get_fixed_ranges(mtrr_type * frs)577a1a499a3SJaswinder Singh Rajput static void get_fixed_ranges(mtrr_type *frs)
5782ec1df41SThomas Gleixner {
5792ec1df41SThomas Gleixner 	unsigned int *p = (unsigned int *)frs;
5802ec1df41SThomas Gleixner 	int i;
5812ec1df41SThomas Gleixner 
5823ff42da5SAndreas Herrmann 	k8_check_syscfg_dram_mod_en();
5833ff42da5SAndreas Herrmann 
584a036c7a3SJaswinder Singh Rajput 	rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
5852ec1df41SThomas Gleixner 
5862ec1df41SThomas Gleixner 	for (i = 0; i < 2; i++)
5877d9d55e4SJaswinder Singh Rajput 		rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
5882ec1df41SThomas Gleixner 	for (i = 0; i < 8; i++)
589ba5673ffSJaswinder Singh Rajput 		rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
5902ec1df41SThomas Gleixner }
5912ec1df41SThomas Gleixner 
mtrr_save_fixed_ranges(void * info)5922ec1df41SThomas Gleixner void mtrr_save_fixed_ranges(void *info)
5932ec1df41SThomas Gleixner {
594362f924bSBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_MTRR))
5952ec1df41SThomas Gleixner 		get_fixed_ranges(mtrr_state.fixed_ranges);
5962ec1df41SThomas Gleixner }
5972ec1df41SThomas Gleixner 
598d4c90e37SYinghai Lu static unsigned __initdata last_fixed_start;
599d4c90e37SYinghai Lu static unsigned __initdata last_fixed_end;
600d4c90e37SYinghai Lu static mtrr_type __initdata last_fixed_type;
601d4c90e37SYinghai Lu 
print_fixed_last(void)602d4c90e37SYinghai Lu static void __init print_fixed_last(void)
603d4c90e37SYinghai Lu {
604d4c90e37SYinghai Lu 	if (!last_fixed_end)
605d4c90e37SYinghai Lu 		return;
606d4c90e37SYinghai Lu 
607a4316603SJuergen Gross 	pr_info("  %05X-%05X %s\n", last_fixed_start,
608d4c90e37SYinghai Lu 		last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
609d4c90e37SYinghai Lu 
610d4c90e37SYinghai Lu 	last_fixed_end = 0;
611d4c90e37SYinghai Lu }
612d4c90e37SYinghai Lu 
update_fixed_last(unsigned base,unsigned end,mtrr_type type)613d4c90e37SYinghai Lu static void __init update_fixed_last(unsigned base, unsigned end,
614d4c90e37SYinghai Lu 				     mtrr_type type)
615d4c90e37SYinghai Lu {
616d4c90e37SYinghai Lu 	last_fixed_start = base;
617d4c90e37SYinghai Lu 	last_fixed_end = end;
618d4c90e37SYinghai Lu 	last_fixed_type = type;
619d4c90e37SYinghai Lu }
620d4c90e37SYinghai Lu 
621a1a499a3SJaswinder Singh Rajput static void __init
print_fixed(unsigned base,unsigned step,const mtrr_type * types)622a1a499a3SJaswinder Singh Rajput print_fixed(unsigned base, unsigned step, const mtrr_type *types)
6232ec1df41SThomas Gleixner {
6242ec1df41SThomas Gleixner 	unsigned i;
6252ec1df41SThomas Gleixner 
626d4c90e37SYinghai Lu 	for (i = 0; i < 8; ++i, ++types, base += step) {
627d4c90e37SYinghai Lu 		if (last_fixed_end == 0) {
628d4c90e37SYinghai Lu 			update_fixed_last(base, base + step, *types);
629d4c90e37SYinghai Lu 			continue;
630d4c90e37SYinghai Lu 		}
631d4c90e37SYinghai Lu 		if (last_fixed_end == base && last_fixed_type == *types) {
632d4c90e37SYinghai Lu 			last_fixed_end = base + step;
633d4c90e37SYinghai Lu 			continue;
634d4c90e37SYinghai Lu 		}
635d4c90e37SYinghai Lu 		/* new segments: gap or different type */
636d4c90e37SYinghai Lu 		print_fixed_last();
637d4c90e37SYinghai Lu 		update_fixed_last(base, base + step, *types);
638d4c90e37SYinghai Lu 	}
6392ec1df41SThomas Gleixner }
6402ec1df41SThomas Gleixner 
print_mtrr_state(void)6418ad97905SYinghai Lu static void __init print_mtrr_state(void)
6428ad97905SYinghai Lu {
6438ad97905SYinghai Lu 	unsigned int i;
6448ad97905SYinghai Lu 	int high_width;
6458ad97905SYinghai Lu 
646a4316603SJuergen Gross 	pr_info("MTRR default type: %s\n",
647d4c90e37SYinghai Lu 		mtrr_attrib_to_str(mtrr_state.def_type));
6488ad97905SYinghai Lu 	if (mtrr_state.have_fixed) {
649a4316603SJuergen Gross 		pr_info("MTRR fixed ranges %sabled:\n",
6509b3aca62SToshi Kani 			((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) &&
6519b3aca62SToshi Kani 			 (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ?
6529b3aca62SToshi Kani 			 "en" : "dis");
6538ad97905SYinghai Lu 		print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
6548ad97905SYinghai Lu 		for (i = 0; i < 2; ++i)
655a1a499a3SJaswinder Singh Rajput 			print_fixed(0x80000 + i * 0x20000, 0x04000,
656a1a499a3SJaswinder Singh Rajput 				    mtrr_state.fixed_ranges + (i + 1) * 8);
6578ad97905SYinghai Lu 		for (i = 0; i < 8; ++i)
658a1a499a3SJaswinder Singh Rajput 			print_fixed(0xC0000 + i * 0x08000, 0x01000,
659a1a499a3SJaswinder Singh Rajput 				    mtrr_state.fixed_ranges + (i + 3) * 8);
660d4c90e37SYinghai Lu 
661d4c90e37SYinghai Lu 		/* tail */
662d4c90e37SYinghai Lu 		print_fixed_last();
6638ad97905SYinghai Lu 	}
664a4316603SJuergen Gross 	pr_info("MTRR variable ranges %sabled:\n",
6659b3aca62SToshi Kani 		mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis");
666f6b98064SJuergen Gross 	high_width = (boot_cpu_data.x86_phys_bits - (32 - PAGE_SHIFT) + 3) / 4;
667a1a499a3SJaswinder Singh Rajput 
6688ad97905SYinghai Lu 	for (i = 0; i < num_var_ranges; ++i) {
669d053b481SJuergen Gross 		if (mtrr_state.var_ranges[i].mask_lo & MTRR_PHYSMASK_V)
670a4316603SJuergen Gross 			pr_info("  %u base %0*X%05X000 mask %0*X%05X000 %s\n",
6718ad97905SYinghai Lu 				i,
6728ad97905SYinghai Lu 				high_width,
6738ad97905SYinghai Lu 				mtrr_state.var_ranges[i].base_hi,
6748ad97905SYinghai Lu 				mtrr_state.var_ranges[i].base_lo >> 12,
6758ad97905SYinghai Lu 				high_width,
6768ad97905SYinghai Lu 				mtrr_state.var_ranges[i].mask_hi,
6778ad97905SYinghai Lu 				mtrr_state.var_ranges[i].mask_lo >> 12,
678d053b481SJuergen Gross 				mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo &
679d053b481SJuergen Gross 						    MTRR_PHYSBASE_TYPE));
6808ad97905SYinghai Lu 		else
681a4316603SJuergen Gross 			pr_info("  %u disabled\n", i);
6828ad97905SYinghai Lu 	}
683a1a499a3SJaswinder Singh Rajput 	if (mtrr_tom2)
684a4316603SJuergen Gross 		pr_info("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
6858ad97905SYinghai Lu }
6868ad97905SYinghai Lu 
6872ec1df41SThomas Gleixner /* Grab all of the MTRR state for this CPU into *state */
get_mtrr_state(void)688f9626104SLuis R. Rodriguez bool __init get_mtrr_state(void)
6892ec1df41SThomas Gleixner {
6902ec1df41SThomas Gleixner 	struct mtrr_var_range *vrs;
691a1a499a3SJaswinder Singh Rajput 	unsigned lo, dummy;
692a1a499a3SJaswinder Singh Rajput 	unsigned int i;
6932ec1df41SThomas Gleixner 
6942ec1df41SThomas Gleixner 	vrs = mtrr_state.var_ranges;
6952ec1df41SThomas Gleixner 
696d9bcc01dSJaswinder Singh Rajput 	rdmsr(MSR_MTRRcap, lo, dummy);
697d053b481SJuergen Gross 	mtrr_state.have_fixed = lo & MTRR_CAP_FIX;
6982ec1df41SThomas Gleixner 
6992ec1df41SThomas Gleixner 	for (i = 0; i < num_var_ranges; i++)
7002ec1df41SThomas Gleixner 		get_mtrr_var_range(i, &vrs[i]);
7012ec1df41SThomas Gleixner 	if (mtrr_state.have_fixed)
7022ec1df41SThomas Gleixner 		get_fixed_ranges(mtrr_state.fixed_ranges);
7032ec1df41SThomas Gleixner 
70452650257SJaswinder Singh Rajput 	rdmsr(MSR_MTRRdefType, lo, dummy);
705d053b481SJuergen Gross 	mtrr_state.def_type = lo & MTRR_DEF_TYPE_TYPE;
706d053b481SJuergen Gross 	mtrr_state.enabled = (lo & MTRR_DEF_TYPE_ENABLE) >> MTRR_STATE_SHIFT;
7072ec1df41SThomas Gleixner 
70835605a10SYinghai Lu 	if (amd_special_default_mtrr()) {
7090da72a4aSThomas Gleixner 		unsigned low, high;
710a1a499a3SJaswinder Singh Rajput 
71135605a10SYinghai Lu 		/* TOP_MEM2 */
7120da72a4aSThomas Gleixner 		rdmsr(MSR_K8_TOP_MEM2, low, high);
71395ffa243SYinghai Lu 		mtrr_tom2 = high;
71495ffa243SYinghai Lu 		mtrr_tom2 <<= 32;
71595ffa243SYinghai Lu 		mtrr_tom2 |= low;
7168004dd96SYinghai Lu 		mtrr_tom2 &= 0xffffff800000ULL;
71735605a10SYinghai Lu 	}
7182ec1df41SThomas Gleixner 
719a4316603SJuergen Gross 	if (mtrr_debug)
7208ad97905SYinghai Lu 		print_mtrr_state();
7218ad97905SYinghai Lu 
7222e5d9c85Svenkatesh.pallipadi@intel.com 	mtrr_state_set = 1;
7232e5d9c85Svenkatesh.pallipadi@intel.com 
724f9626104SLuis R. Rodriguez 	return !!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED);
7252ec1df41SThomas Gleixner }
7262ec1df41SThomas Gleixner 
727a1a499a3SJaswinder Singh Rajput /* Some BIOS's are messed up and don't set all MTRRs the same! */
mtrr_state_warn(void)7282ec1df41SThomas Gleixner void __init mtrr_state_warn(void)
7292ec1df41SThomas Gleixner {
7302ec1df41SThomas Gleixner 	unsigned long mask = smp_changes_mask;
7312ec1df41SThomas Gleixner 
7322ec1df41SThomas Gleixner 	if (!mask)
7332ec1df41SThomas Gleixner 		return;
7342ec1df41SThomas Gleixner 	if (mask & MTRR_CHANGE_MASK_FIXED)
7351b74dde7SChen Yucong 		pr_warn("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
7362ec1df41SThomas Gleixner 	if (mask & MTRR_CHANGE_MASK_VARIABLE)
7371b74dde7SChen Yucong 		pr_warn("mtrr: your CPUs had inconsistent variable MTRR settings\n");
7382ec1df41SThomas Gleixner 	if (mask & MTRR_CHANGE_MASK_DEFTYPE)
7391b74dde7SChen Yucong 		pr_warn("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
740a1a499a3SJaswinder Singh Rajput 
7411b74dde7SChen Yucong 	pr_info("mtrr: probably your BIOS does not setup all CPUs.\n");
7421b74dde7SChen Yucong 	pr_info("mtrr: corrected configuration.\n");
7432ec1df41SThomas Gleixner }
7442ec1df41SThomas Gleixner 
745a1a499a3SJaswinder Singh Rajput /*
746a1a499a3SJaswinder Singh Rajput  * Doesn't attempt to pass an error out to MTRR users
747a1a499a3SJaswinder Singh Rajput  * because it's quite complicated in some cases and probably not
748a1a499a3SJaswinder Singh Rajput  * worth it because the best error handling is to ignore it.
749a1a499a3SJaswinder Singh Rajput  */
mtrr_wrmsr(unsigned msr,unsigned a,unsigned b)7502ec1df41SThomas Gleixner void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
7512ec1df41SThomas Gleixner {
752a1a499a3SJaswinder Singh Rajput 	if (wrmsr_safe(msr, a, b) < 0) {
7531b74dde7SChen Yucong 		pr_err("MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
7542ec1df41SThomas Gleixner 			smp_processor_id(), msr, a, b);
7552ec1df41SThomas Gleixner 	}
756a1a499a3SJaswinder Singh Rajput }
7572ec1df41SThomas Gleixner 
7582ec1df41SThomas Gleixner /**
759a1a499a3SJaswinder Singh Rajput  * set_fixed_range - checks & updates a fixed-range MTRR if it
760a1a499a3SJaswinder Singh Rajput  *		     differs from the value it should have
7611d3381ebSRandy Dunlap  * @msr: MSR address of the MTTR which should be checked and updated
7621d3381ebSRandy Dunlap  * @changed: pointer which indicates whether the MTRR needed to be changed
7631d3381ebSRandy Dunlap  * @msrwords: pointer to the MSR values which the MSR should have
7642ec1df41SThomas Gleixner  */
set_fixed_range(int msr,bool * changed,unsigned int * msrwords)7652d2ee8deSPaul Jimenez static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
7662ec1df41SThomas Gleixner {
7672ec1df41SThomas Gleixner 	unsigned lo, hi;
7682ec1df41SThomas Gleixner 
7692ec1df41SThomas Gleixner 	rdmsr(msr, lo, hi);
7702ec1df41SThomas Gleixner 
7712ec1df41SThomas Gleixner 	if (lo != msrwords[0] || hi != msrwords[1]) {
7722ec1df41SThomas Gleixner 		mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
7732d2ee8deSPaul Jimenez 		*changed = true;
7742ec1df41SThomas Gleixner 	}
7752ec1df41SThomas Gleixner }
7762ec1df41SThomas Gleixner 
7771d3381ebSRandy Dunlap /**
7781d3381ebSRandy Dunlap  * generic_get_free_region - Get a free MTRR.
7791d3381ebSRandy Dunlap  * @base: The starting (base) address of the region.
7801d3381ebSRandy Dunlap  * @size: The size (in bytes) of the region.
7811d3381ebSRandy Dunlap  * @replace_reg: mtrr index to be replaced; set to invalid value if none.
7821d3381ebSRandy Dunlap  *
7831d3381ebSRandy Dunlap  * Returns: The index of the region on success, else negative on error.
7842ec1df41SThomas Gleixner  */
785a1a499a3SJaswinder Singh Rajput int
generic_get_free_region(unsigned long base,unsigned long size,int replace_reg)786a1a499a3SJaswinder Singh Rajput generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
7872ec1df41SThomas Gleixner {
7882ec1df41SThomas Gleixner 	unsigned long lbase, lsize;
789a1a499a3SJaswinder Singh Rajput 	mtrr_type ltype;
790a1a499a3SJaswinder Singh Rajput 	int i, max;
7912ec1df41SThomas Gleixner 
7922ec1df41SThomas Gleixner 	max = num_var_ranges;
7932ec1df41SThomas Gleixner 	if (replace_reg >= 0 && replace_reg < max)
7942ec1df41SThomas Gleixner 		return replace_reg;
795a1a499a3SJaswinder Singh Rajput 
7962ec1df41SThomas Gleixner 	for (i = 0; i < max; ++i) {
7972ec1df41SThomas Gleixner 		mtrr_if->get(i, &lbase, &lsize, &ltype);
7982ec1df41SThomas Gleixner 		if (lsize == 0)
7992ec1df41SThomas Gleixner 			return i;
8002ec1df41SThomas Gleixner 	}
801a1a499a3SJaswinder Singh Rajput 
8022ec1df41SThomas Gleixner 	return -ENOSPC;
8032ec1df41SThomas Gleixner }
8042ec1df41SThomas Gleixner 
generic_get_mtrr(unsigned int reg,unsigned long * base,unsigned long * size,mtrr_type * type)8052ec1df41SThomas Gleixner static void generic_get_mtrr(unsigned int reg, unsigned long *base,
8062ec1df41SThomas Gleixner 			     unsigned long *size, mtrr_type *type)
8072ec1df41SThomas Gleixner {
808d5c78673SYinghai Lu 	u32 mask_lo, mask_hi, base_lo, base_hi;
809d5c78673SYinghai Lu 	unsigned int hi;
810d5c78673SYinghai Lu 	u64 tmp, mask;
8112ec1df41SThomas Gleixner 
8128ad97905SYinghai Lu 	/*
8138ad97905SYinghai Lu 	 * get_mtrr doesn't need to update mtrr_state, also it could be called
8148ad97905SYinghai Lu 	 * from any cpu, so try to print it out directly.
8158ad97905SYinghai Lu 	 */
816fa10ba64SAndi Kleen 	get_cpu();
81763516ef6SYinghai Lu 
8182ec1df41SThomas Gleixner 	rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
8198ad97905SYinghai Lu 
820d053b481SJuergen Gross 	if (!(mask_lo & MTRR_PHYSMASK_V)) {
8212ec1df41SThomas Gleixner 		/*  Invalid (i.e. free) range */
8222ec1df41SThomas Gleixner 		*base = 0;
8232ec1df41SThomas Gleixner 		*size = 0;
8242ec1df41SThomas Gleixner 		*type = 0;
82563516ef6SYinghai Lu 		goto out_put_cpu;
8262ec1df41SThomas Gleixner 	}
8272ec1df41SThomas Gleixner 
8282ec1df41SThomas Gleixner 	rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
8292ec1df41SThomas Gleixner 
83063516ef6SYinghai Lu 	/* Work out the shifted address mask: */
831d053b481SJuergen Gross 	tmp = (u64)mask_hi << 32 | (mask_lo & PAGE_MASK);
832d053b481SJuergen Gross 	mask = (u64)phys_hi_rsvd << 32 | tmp;
83363516ef6SYinghai Lu 
83463516ef6SYinghai Lu 	/* Expand tmp with high bits to all 1s: */
835d5c78673SYinghai Lu 	hi = fls64(tmp);
83638cc1c3dSYinghai Lu 	if (hi > 0) {
837d5c78673SYinghai Lu 		tmp |= ~((1ULL<<(hi - 1)) - 1);
83838cc1c3dSYinghai Lu 
839d5c78673SYinghai Lu 		if (tmp != mask) {
8401b74dde7SChen Yucong 			pr_warn("mtrr: your BIOS has configured an incorrect mask, fixing it.\n");
841373d4d09SRusty Russell 			add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
842d5c78673SYinghai Lu 			mask = tmp;
84338cc1c3dSYinghai Lu 		}
84438cc1c3dSYinghai Lu 	}
8452ec1df41SThomas Gleixner 
84663516ef6SYinghai Lu 	/*
84763516ef6SYinghai Lu 	 * This works correctly if size is a power of two, i.e. a
84863516ef6SYinghai Lu 	 * contiguous range:
84963516ef6SYinghai Lu 	 */
850d053b481SJuergen Gross 	*size = -mask >> PAGE_SHIFT;
851d5c78673SYinghai Lu 	*base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
852d053b481SJuergen Gross 	*type = base_lo & MTRR_PHYSBASE_TYPE;
8538ad97905SYinghai Lu 
85463516ef6SYinghai Lu out_put_cpu:
85563516ef6SYinghai Lu 	put_cpu();
8562ec1df41SThomas Gleixner }
8572ec1df41SThomas Gleixner 
8582ec1df41SThomas Gleixner /**
859a1a499a3SJaswinder Singh Rajput  * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
860a1a499a3SJaswinder Singh Rajput  *		      differ from the saved set
8611d3381ebSRandy Dunlap  * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
8622ec1df41SThomas Gleixner  */
set_fixed_ranges(mtrr_type * frs)8632ec1df41SThomas Gleixner static int set_fixed_ranges(mtrr_type *frs)
8642ec1df41SThomas Gleixner {
8652ec1df41SThomas Gleixner 	unsigned long long *saved = (unsigned long long *)frs;
8662d2ee8deSPaul Jimenez 	bool changed = false;
8672ec1df41SThomas Gleixner 	int block = -1, range;
8682ec1df41SThomas Gleixner 
8693ff42da5SAndreas Herrmann 	k8_check_syscfg_dram_mod_en();
8703ff42da5SAndreas Herrmann 
871a1a499a3SJaswinder Singh Rajput 	while (fixed_range_blocks[++block].ranges) {
8722ec1df41SThomas Gleixner 		for (range = 0; range < fixed_range_blocks[block].ranges; range++)
8732ec1df41SThomas Gleixner 			set_fixed_range(fixed_range_blocks[block].base_msr + range,
8742ec1df41SThomas Gleixner 					&changed, (unsigned int *)saved++);
875a1a499a3SJaswinder Singh Rajput 	}
8762ec1df41SThomas Gleixner 
8772ec1df41SThomas Gleixner 	return changed;
8782ec1df41SThomas Gleixner }
8792ec1df41SThomas Gleixner 
880a1a499a3SJaswinder Singh Rajput /*
881a1a499a3SJaswinder Singh Rajput  * Set the MSR pair relating to a var range.
882a1a499a3SJaswinder Singh Rajput  * Returns true if changes are made.
883a1a499a3SJaswinder Singh Rajput  */
set_mtrr_var_ranges(unsigned int index,struct mtrr_var_range * vr)8842d2ee8deSPaul Jimenez static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
8852ec1df41SThomas Gleixner {
8862ec1df41SThomas Gleixner 	unsigned int lo, hi;
8872d2ee8deSPaul Jimenez 	bool changed = false;
8882ec1df41SThomas Gleixner 
8892ec1df41SThomas Gleixner 	rdmsr(MTRRphysBase_MSR(index), lo, hi);
890d053b481SJuergen Gross 	if ((vr->base_lo & ~MTRR_PHYSBASE_RSVD) != (lo & ~MTRR_PHYSBASE_RSVD)
891d053b481SJuergen Gross 	    || (vr->base_hi & ~phys_hi_rsvd) != (hi & ~phys_hi_rsvd)) {
892a1a499a3SJaswinder Singh Rajput 
8932ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
8942d2ee8deSPaul Jimenez 		changed = true;
8952ec1df41SThomas Gleixner 	}
8962ec1df41SThomas Gleixner 
8972ec1df41SThomas Gleixner 	rdmsr(MTRRphysMask_MSR(index), lo, hi);
8982ec1df41SThomas Gleixner 
899d053b481SJuergen Gross 	if ((vr->mask_lo & ~MTRR_PHYSMASK_RSVD) != (lo & ~MTRR_PHYSMASK_RSVD)
900d053b481SJuergen Gross 	    || (vr->mask_hi & ~phys_hi_rsvd) != (hi & ~phys_hi_rsvd)) {
9012ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
9022d2ee8deSPaul Jimenez 		changed = true;
9032ec1df41SThomas Gleixner 	}
9042ec1df41SThomas Gleixner 	return changed;
9052ec1df41SThomas Gleixner }
9062ec1df41SThomas Gleixner 
9072ec1df41SThomas Gleixner static u32 deftype_lo, deftype_hi;
9082ec1df41SThomas Gleixner 
9091d3381ebSRandy Dunlap /**
9101d3381ebSRandy Dunlap  * set_mtrr_state - Set the MTRR state for this CPU.
9111d3381ebSRandy Dunlap  *
91201c97c73SJuergen Gross  * NOTE: The CPU must already be in a safe state for MTRR changes, including
91301c97c73SJuergen Gross  *       measures that only a single CPU can be active in set_mtrr_state() in
91401c97c73SJuergen Gross  *       order to not be subject to races for usage of deftype_lo. This is
915d5f66d5dSJuergen Gross  *       accomplished by taking cache_disable_lock.
9161d3381ebSRandy Dunlap  * RETURNS: 0 if no changes made, else a mask indicating what was changed.
9172ec1df41SThomas Gleixner  */
set_mtrr_state(void)9181d3381ebSRandy Dunlap static unsigned long set_mtrr_state(void)
9192ec1df41SThomas Gleixner {
9202ec1df41SThomas Gleixner 	unsigned long change_mask = 0;
921a1a499a3SJaswinder Singh Rajput 	unsigned int i;
9222ec1df41SThomas Gleixner 
923a1a499a3SJaswinder Singh Rajput 	for (i = 0; i < num_var_ranges; i++) {
9242ec1df41SThomas Gleixner 		if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
9252ec1df41SThomas Gleixner 			change_mask |= MTRR_CHANGE_MASK_VARIABLE;
926a1a499a3SJaswinder Singh Rajput 	}
9272ec1df41SThomas Gleixner 
9282ec1df41SThomas Gleixner 	if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
9292ec1df41SThomas Gleixner 		change_mask |= MTRR_CHANGE_MASK_FIXED;
9302ec1df41SThomas Gleixner 
931a1a499a3SJaswinder Singh Rajput 	/*
932a1a499a3SJaswinder Singh Rajput 	 * Set_mtrr_restore restores the old value of MTRRdefType,
933a1a499a3SJaswinder Singh Rajput 	 * so to set it we fiddle with the saved value:
934a1a499a3SJaswinder Singh Rajput 	 */
935d053b481SJuergen Gross 	if ((deftype_lo & MTRR_DEF_TYPE_TYPE) != mtrr_state.def_type ||
936d053b481SJuergen Gross 	    ((deftype_lo & MTRR_DEF_TYPE_ENABLE) >> MTRR_STATE_SHIFT) != mtrr_state.enabled) {
937a1a499a3SJaswinder Singh Rajput 
938d053b481SJuergen Gross 		deftype_lo = (deftype_lo & MTRR_DEF_TYPE_DISABLE) |
939d053b481SJuergen Gross 			     mtrr_state.def_type |
940d053b481SJuergen Gross 			     (mtrr_state.enabled << MTRR_STATE_SHIFT);
9412ec1df41SThomas Gleixner 		change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
9422ec1df41SThomas Gleixner 	}
9432ec1df41SThomas Gleixner 
9442ec1df41SThomas Gleixner 	return change_mask;
9452ec1df41SThomas Gleixner }
9462ec1df41SThomas Gleixner 
mtrr_disable(void)9474ad7149eSJuergen Gross void mtrr_disable(void)
9484ad7149eSJuergen Gross {
9494ad7149eSJuergen Gross 	/* Save MTRR state */
9504ad7149eSJuergen Gross 	rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
9514ad7149eSJuergen Gross 
9524ad7149eSJuergen Gross 	/* Disable MTRRs, and set the default type to uncached */
953d053b481SJuergen Gross 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & MTRR_DEF_TYPE_DISABLE, deftype_hi);
9544ad7149eSJuergen Gross }
9554ad7149eSJuergen Gross 
mtrr_enable(void)9564ad7149eSJuergen Gross void mtrr_enable(void)
9574ad7149eSJuergen Gross {
9584ad7149eSJuergen Gross 	/* Intel (P6) standard MTRRs */
9594ad7149eSJuergen Gross 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
9604ad7149eSJuergen Gross }
9614ad7149eSJuergen Gross 
mtrr_generic_set_state(void)9627d71db53SJuergen Gross void mtrr_generic_set_state(void)
9632ec1df41SThomas Gleixner {
9642ec1df41SThomas Gleixner 	unsigned long mask, count;
9652ec1df41SThomas Gleixner 
9662ec1df41SThomas Gleixner 	/* Actually set the state */
9672ec1df41SThomas Gleixner 	mask = set_mtrr_state();
9682ec1df41SThomas Gleixner 
9692ec1df41SThomas Gleixner 	/* Use the atomic bitops to update the global mask */
9700e96f31eSJordan Borgner 	for (count = 0; count < sizeof(mask) * 8; ++count) {
9712ec1df41SThomas Gleixner 		if (mask & 0x01)
9722ec1df41SThomas Gleixner 			set_bit(count, &smp_changes_mask);
9732ec1df41SThomas Gleixner 		mask >>= 1;
9742ec1df41SThomas Gleixner 	}
9752ec1df41SThomas Gleixner }
9762ec1df41SThomas Gleixner 
977a1a499a3SJaswinder Singh Rajput /**
978a1a499a3SJaswinder Singh Rajput  * generic_set_mtrr - set variable MTRR register on the local CPU.
979a1a499a3SJaswinder Singh Rajput  *
980a1a499a3SJaswinder Singh Rajput  * @reg: The register to set.
981a1a499a3SJaswinder Singh Rajput  * @base: The base address of the region.
982a1a499a3SJaswinder Singh Rajput  * @size: The size of the region. If this is 0 the region is disabled.
983a1a499a3SJaswinder Singh Rajput  * @type: The type of the region.
984a1a499a3SJaswinder Singh Rajput  *
985a1a499a3SJaswinder Singh Rajput  * Returns nothing.
986a1a499a3SJaswinder Singh Rajput  */
generic_set_mtrr(unsigned int reg,unsigned long base,unsigned long size,mtrr_type type)9872ec1df41SThomas Gleixner static void generic_set_mtrr(unsigned int reg, unsigned long base,
9882ec1df41SThomas Gleixner 			     unsigned long size, mtrr_type type)
9892ec1df41SThomas Gleixner {
9902ec1df41SThomas Gleixner 	unsigned long flags;
9912ec1df41SThomas Gleixner 	struct mtrr_var_range *vr;
9922ec1df41SThomas Gleixner 
9932ec1df41SThomas Gleixner 	vr = &mtrr_state.var_ranges[reg];
9942ec1df41SThomas Gleixner 
9952ec1df41SThomas Gleixner 	local_irq_save(flags);
996d5f66d5dSJuergen Gross 	cache_disable();
9972ec1df41SThomas Gleixner 
9982ec1df41SThomas Gleixner 	if (size == 0) {
999a1a499a3SJaswinder Singh Rajput 		/*
1000a1a499a3SJaswinder Singh Rajput 		 * The invalid bit is kept in the mask, so we simply
1001a1a499a3SJaswinder Singh Rajput 		 * clear the relevant mask register to disable a range.
1002a1a499a3SJaswinder Singh Rajput 		 */
10032ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
10042ec1df41SThomas Gleixner 		memset(vr, 0, sizeof(struct mtrr_var_range));
10052ec1df41SThomas Gleixner 	} else {
10062ec1df41SThomas Gleixner 		vr->base_lo = base << PAGE_SHIFT | type;
1007d053b481SJuergen Gross 		vr->base_hi = (base >> (32 - PAGE_SHIFT)) & ~phys_hi_rsvd;
1008d053b481SJuergen Gross 		vr->mask_lo = -size << PAGE_SHIFT | MTRR_PHYSMASK_V;
1009d053b481SJuergen Gross 		vr->mask_hi = (-size >> (32 - PAGE_SHIFT)) & ~phys_hi_rsvd;
10102ec1df41SThomas Gleixner 
10112ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
10122ec1df41SThomas Gleixner 		mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
10132ec1df41SThomas Gleixner 	}
10142ec1df41SThomas Gleixner 
1015d5f66d5dSJuergen Gross 	cache_enable();
10162ec1df41SThomas Gleixner 	local_irq_restore(flags);
10172ec1df41SThomas Gleixner }
10182ec1df41SThomas Gleixner 
generic_validate_add_page(unsigned long base,unsigned long size,unsigned int type)1019a1a499a3SJaswinder Singh Rajput int generic_validate_add_page(unsigned long base, unsigned long size,
1020a1a499a3SJaswinder Singh Rajput 			      unsigned int type)
10212ec1df41SThomas Gleixner {
10222ec1df41SThomas Gleixner 	unsigned long lbase, last;
10232ec1df41SThomas Gleixner 
1024a1a499a3SJaswinder Singh Rajput 	/*
1025a1a499a3SJaswinder Singh Rajput 	 * For Intel PPro stepping <= 7
1026a1a499a3SJaswinder Singh Rajput 	 * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
1027a1a499a3SJaswinder Singh Rajput 	 */
102803409069SJuergen Gross 	if (mtrr_if == &generic_mtrr_ops && boot_cpu_data.x86 == 6 &&
10292ec1df41SThomas Gleixner 	    boot_cpu_data.x86_model == 1 &&
1030b399151cSJia Zhang 	    boot_cpu_data.x86_stepping <= 7) {
10312ec1df41SThomas Gleixner 		if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
10321b74dde7SChen Yucong 			pr_warn("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
10332ec1df41SThomas Gleixner 			return -EINVAL;
10342ec1df41SThomas Gleixner 		}
10352ec1df41SThomas Gleixner 		if (!(base + size < 0x70000 || base > 0x7003F) &&
10362ec1df41SThomas Gleixner 		    (type == MTRR_TYPE_WRCOMB
10372ec1df41SThomas Gleixner 		     || type == MTRR_TYPE_WRBACK)) {
10381b74dde7SChen Yucong 			pr_warn("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
10392ec1df41SThomas Gleixner 			return -EINVAL;
10402ec1df41SThomas Gleixner 		}
10412ec1df41SThomas Gleixner 	}
10422ec1df41SThomas Gleixner 
1043a1a499a3SJaswinder Singh Rajput 	/*
1044a1a499a3SJaswinder Singh Rajput 	 * Check upper bits of base and last are equal and lower bits are 0
1045a1a499a3SJaswinder Singh Rajput 	 * for base and 1 for last
1046a1a499a3SJaswinder Singh Rajput 	 */
10472ec1df41SThomas Gleixner 	last = base + size - 1;
10482ec1df41SThomas Gleixner 	for (lbase = base; !(lbase & 1) && (last & 1);
1049a1a499a3SJaswinder Singh Rajput 	     lbase = lbase >> 1, last = last >> 1)
1050a1a499a3SJaswinder Singh Rajput 		;
10512ec1df41SThomas Gleixner 	if (lbase != last) {
10521b74dde7SChen Yucong 		pr_warn("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
10532ec1df41SThomas Gleixner 		return -EINVAL;
10542ec1df41SThomas Gleixner 	}
10552ec1df41SThomas Gleixner 	return 0;
10562ec1df41SThomas Gleixner }
10572ec1df41SThomas Gleixner 
generic_have_wrcomb(void)10582ec1df41SThomas Gleixner static int generic_have_wrcomb(void)
10592ec1df41SThomas Gleixner {
10602ec1df41SThomas Gleixner 	unsigned long config, dummy;
1061d9bcc01dSJaswinder Singh Rajput 	rdmsr(MSR_MTRRcap, config, dummy);
1062d053b481SJuergen Gross 	return config & MTRR_CAP_WC;
10632ec1df41SThomas Gleixner }
10642ec1df41SThomas Gleixner 
positive_have_wrcomb(void)10652ec1df41SThomas Gleixner int positive_have_wrcomb(void)
10662ec1df41SThomas Gleixner {
10672ec1df41SThomas Gleixner 	return 1;
10682ec1df41SThomas Gleixner }
10692ec1df41SThomas Gleixner 
1070a1a499a3SJaswinder Singh Rajput /*
1071a1a499a3SJaswinder Singh Rajput  * Generic structure...
10722ec1df41SThomas Gleixner  */
10733b9cfc0aSEmese Revfy const struct mtrr_ops generic_mtrr_ops = {
10742ec1df41SThomas Gleixner 	.get			= generic_get_mtrr,
10752ec1df41SThomas Gleixner 	.get_free_region	= generic_get_free_region,
10762ec1df41SThomas Gleixner 	.set			= generic_set_mtrr,
10772ec1df41SThomas Gleixner 	.validate_add_page	= generic_validate_add_page,
10782ec1df41SThomas Gleixner 	.have_wrcomb		= generic_have_wrcomb,
10792ec1df41SThomas Gleixner };
1080