blob: 9aa5dc76ff4a156304a3c864776c3665628004e4 [file] [log] [blame]
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +05301/*
2 * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong
3 * because MTRRs can span upto 40 bits (36bits on most modern x86)
4 */
5#define DEBUG
6
7#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/slab.h>
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053010#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053012
13#include <asm/processor-flags.h>
14#include <asm/cpufeature.h>
15#include <asm/tlbflush.h>
16#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/mtrr.h>
18#include <asm/msr.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070019#include <asm/pat.h>
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "mtrr.h"
22
Bernhard Kaindlde938c52007-05-02 19:27:17 +020023struct fixed_range_block {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053024 int base_msr; /* start address of an MTRR block */
25 int ranges; /* number of MTRRs in this block */
Bernhard Kaindlde938c52007-05-02 19:27:17 +020026};
27
28static struct fixed_range_block fixed_range_blocks[] = {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053029 { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */
30 { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */
31 { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */
Bernhard Kaindlde938c52007-05-02 19:27:17 +020032 {}
33};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static unsigned long smp_changes_mask;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070036static int mtrr_state_set;
Yinghai Lu95ffa242008-04-29 03:52:33 -070037u64 mtrr_tom2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053039struct mtrr_state_type mtrr_state;
Sheng Yang932d27a2008-10-09 16:01:53 +080040EXPORT_SYMBOL_GPL(mtrr_state);
41
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +053042/*
Andreas Herrmann3ff42da2009-03-12 17:39:37 +010043 * BIOS is expected to clear MtrrFixDramModEn bit, see for example
44 * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
45 * Opteron Processors" (26094 Rev. 3.30 February 2006), section
46 * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
47 * to 1 during BIOS initalization of the fixed MTRRs, then cleared to
48 * 0 for operation."
49 */
50static inline void k8_check_syscfg_dram_mod_en(void)
51{
52 u32 lo, hi;
53
54 if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
55 (boot_cpu_data.x86 >= 0x0f)))
56 return;
57
58 rdmsr(MSR_K8_SYSCFG, lo, hi);
59 if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
60 printk(KERN_ERR FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
61 " not cleared by BIOS, clearing this bit\n",
62 smp_processor_id());
63 lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
64 mtrr_wrmsr(MSR_K8_SYSCFG, lo, hi);
65 }
66}
67
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070068/*
69 * Returns the effective MTRR type for the region
70 * Error returns:
71 * - 0xFE - when the range is "not entirely covered" by _any_ var range MTRR
72 * - 0xFF - when MTRR is not enabled
73 */
74u8 mtrr_type_lookup(u64 start, u64 end)
75{
76 int i;
77 u64 base, mask;
78 u8 prev_match, curr_match;
79
80 if (!mtrr_state_set)
81 return 0xFF;
82
83 if (!mtrr_state.enabled)
84 return 0xFF;
85
86 /* Make end inclusive end, instead of exclusive */
87 end--;
88
89 /* Look in fixed ranges. Just return the type as per start */
90 if (mtrr_state.have_fixed && (start < 0x100000)) {
91 int idx;
92
93 if (start < 0x80000) {
94 idx = 0;
95 idx += (start >> 16);
96 return mtrr_state.fixed_ranges[idx];
97 } else if (start < 0xC0000) {
98 idx = 1 * 8;
99 idx += ((start - 0x80000) >> 14);
100 return mtrr_state.fixed_ranges[idx];
101 } else if (start < 0x1000000) {
102 idx = 3 * 8;
103 idx += ((start - 0xC0000) >> 12);
104 return mtrr_state.fixed_ranges[idx];
105 }
106 }
107
108 /*
109 * Look in variable ranges
110 * Look of multiple ranges matching this address and pick type
111 * as per MTRR precedence
112 */
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530113 if (!(mtrr_state.enabled & 2))
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700114 return mtrr_state.def_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700115
116 prev_match = 0xFF;
117 for (i = 0; i < num_var_ranges; ++i) {
118 unsigned short start_state, end_state;
119
120 if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11)))
121 continue;
122
123 base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) +
124 (mtrr_state.var_ranges[i].base_lo & PAGE_MASK);
125 mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) +
126 (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK);
127
128 start_state = ((start & mask) == (base & mask));
129 end_state = ((end & mask) == (base & mask));
130 if (start_state != end_state)
131 return 0xFE;
132
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530133 if ((start & mask) != (base & mask))
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700134 continue;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700135
136 curr_match = mtrr_state.var_ranges[i].base_lo & 0xff;
137 if (prev_match == 0xFF) {
138 prev_match = curr_match;
139 continue;
140 }
141
142 if (prev_match == MTRR_TYPE_UNCACHABLE ||
143 curr_match == MTRR_TYPE_UNCACHABLE) {
144 return MTRR_TYPE_UNCACHABLE;
145 }
146
147 if ((prev_match == MTRR_TYPE_WRBACK &&
148 curr_match == MTRR_TYPE_WRTHROUGH) ||
149 (prev_match == MTRR_TYPE_WRTHROUGH &&
150 curr_match == MTRR_TYPE_WRBACK)) {
151 prev_match = MTRR_TYPE_WRTHROUGH;
152 curr_match = MTRR_TYPE_WRTHROUGH;
153 }
154
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530155 if (prev_match != curr_match)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700156 return MTRR_TYPE_UNCACHABLE;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700157 }
158
Yinghai Lu95ffa242008-04-29 03:52:33 -0700159 if (mtrr_tom2) {
160 if (start >= (1ULL<<32) && (end < mtrr_tom2))
Yinghai Lu35605a12008-03-24 16:02:01 -0700161 return MTRR_TYPE_WRBACK;
162 }
163
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700164 if (prev_match != 0xFF)
165 return prev_match;
166
167 return mtrr_state.def_type;
168}
169
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530170/* Get the MSR pair relating to a var range */
Yinghai Lubf8c4812007-06-20 12:23:39 +0200171static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
173{
174 rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
175 rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
176}
177
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530178/* Fill the MSR pair relating to a var range */
Yinghai Lu95ffa242008-04-29 03:52:33 -0700179void fill_mtrr_var_range(unsigned int index,
180 u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
181{
182 struct mtrr_var_range *vr;
183
184 vr = mtrr_state.var_ranges;
185
186 vr[index].base_lo = base_lo;
187 vr[index].base_hi = base_hi;
188 vr[index].mask_lo = mask_lo;
189 vr[index].mask_hi = mask_hi;
190}
191
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530192static void get_fixed_ranges(mtrr_type *frs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530194 unsigned int *p = (unsigned int *)frs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int i;
196
Andreas Herrmann3ff42da2009-03-12 17:39:37 +0100197 k8_check_syscfg_dram_mod_en();
198
Jaswinder Singh Rajputa036c7a2009-05-14 12:10:43 +0530199 rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 for (i = 0; i < 2; i++)
Jaswinder Singh Rajput7d9d55e2009-05-14 12:15:32 +0530202 rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for (i = 0; i < 8; i++)
Jaswinder Singh Rajputba5673f2009-05-14 12:29:25 +0530204 rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Bernhard Kaindl2b3b4832007-05-02 19:27:17 +0200207void mtrr_save_fixed_ranges(void *info)
208{
Andrew Morton84288ad2007-07-01 12:06:48 -0700209 if (cpu_has_mtrr)
210 get_fixed_ranges(mtrr_state.fixed_ranges);
Bernhard Kaindl2b3b4832007-05-02 19:27:17 +0200211}
212
Yinghai Lud4c90e32009-03-13 14:08:49 -0700213static unsigned __initdata last_fixed_start;
214static unsigned __initdata last_fixed_end;
215static mtrr_type __initdata last_fixed_type;
216
217static void __init print_fixed_last(void)
218{
219 if (!last_fixed_end)
220 return;
221
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530222 pr_debug(" %05X-%05X %s\n", last_fixed_start,
223 last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
Yinghai Lud4c90e32009-03-13 14:08:49 -0700224
225 last_fixed_end = 0;
226}
227
228static void __init update_fixed_last(unsigned base, unsigned end,
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530229 mtrr_type type)
Yinghai Lud4c90e32009-03-13 14:08:49 -0700230{
231 last_fixed_start = base;
232 last_fixed_end = end;
233 last_fixed_type = type;
234}
235
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530236static void __init
237print_fixed(unsigned base, unsigned step, const mtrr_type *types)
Jan Beulich365bff82006-12-07 02:14:09 +0100238{
239 unsigned i;
240
Yinghai Lud4c90e32009-03-13 14:08:49 -0700241 for (i = 0; i < 8; ++i, ++types, base += step) {
242 if (last_fixed_end == 0) {
243 update_fixed_last(base, base + step, *types);
244 continue;
245 }
246 if (last_fixed_end == base && last_fixed_type == *types) {
247 last_fixed_end = base + step;
248 continue;
249 }
250 /* new segments: gap or different type */
251 print_fixed_last();
252 update_fixed_last(base, base + step, *types);
253 }
Jan Beulich365bff82006-12-07 02:14:09 +0100254}
255
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700256static void prepare_set(void);
257static void post_set(void);
258
Yinghai Lu8ad97902009-03-12 18:43:54 -0700259static void __init print_mtrr_state(void)
260{
261 unsigned int i;
262 int high_width;
263
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530264 pr_debug("MTRR default type: %s\n",
265 mtrr_attrib_to_str(mtrr_state.def_type));
Yinghai Lu8ad97902009-03-12 18:43:54 -0700266 if (mtrr_state.have_fixed) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530267 pr_debug("MTRR fixed ranges %sabled:\n",
268 mtrr_state.enabled & 1 ? "en" : "dis");
Yinghai Lu8ad97902009-03-12 18:43:54 -0700269 print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
270 for (i = 0; i < 2; ++i)
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530271 print_fixed(0x80000 + i * 0x20000, 0x04000,
272 mtrr_state.fixed_ranges + (i + 1) * 8);
Yinghai Lu8ad97902009-03-12 18:43:54 -0700273 for (i = 0; i < 8; ++i)
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530274 print_fixed(0xC0000 + i * 0x08000, 0x01000,
275 mtrr_state.fixed_ranges + (i + 3) * 8);
Yinghai Lud4c90e32009-03-13 14:08:49 -0700276
277 /* tail */
278 print_fixed_last();
Yinghai Lu8ad97902009-03-12 18:43:54 -0700279 }
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530280 pr_debug("MTRR variable ranges %sabled:\n",
281 mtrr_state.enabled & 2 ? "en" : "dis");
Yinghai Lu917a0152009-05-06 21:36:16 -0700282 if (size_or_mask & 0xffffffffUL)
283 high_width = ffs(size_or_mask & 0xffffffffUL) - 1;
284 else
285 high_width = ffs(size_or_mask>>32) + 32 - 1;
286 high_width = (high_width - (32 - PAGE_SHIFT) + 3) / 4;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530287
Yinghai Lu8ad97902009-03-12 18:43:54 -0700288 for (i = 0; i < num_var_ranges; ++i) {
289 if (mtrr_state.var_ranges[i].mask_lo & (1 << 11))
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530290 pr_debug(" %u base %0*X%05X000 mask %0*X%05X000 %s\n",
291 i,
292 high_width,
293 mtrr_state.var_ranges[i].base_hi,
294 mtrr_state.var_ranges[i].base_lo >> 12,
295 high_width,
296 mtrr_state.var_ranges[i].mask_hi,
297 mtrr_state.var_ranges[i].mask_lo >> 12,
298 mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff));
Yinghai Lu8ad97902009-03-12 18:43:54 -0700299 else
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530300 pr_debug(" %u disabled\n", i);
Yinghai Lu8ad97902009-03-12 18:43:54 -0700301 }
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530302 if (mtrr_tom2)
303 pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
Yinghai Lu8ad97902009-03-12 18:43:54 -0700304}
305
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530306/* Grab all of the MTRR state for this CPU into *state */
Sam Ravnborg9ef231a2007-07-21 17:10:39 +0200307void __init get_mtrr_state(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct mtrr_var_range *vrs;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700310 unsigned long flags;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530311 unsigned lo, dummy;
312 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 vrs = mtrr_state.var_ranges;
315
Jaswinder Singh Rajputd9bcc012009-05-14 12:06:12 +0530316 rdmsr(MSR_MTRRcap, lo, dummy);
Jan Beulich365bff82006-12-07 02:14:09 +0100317 mtrr_state.have_fixed = (lo >> 8) & 1;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 for (i = 0; i < num_var_ranges; i++)
320 get_mtrr_var_range(i, &vrs[i]);
Jan Beulich365bff82006-12-07 02:14:09 +0100321 if (mtrr_state.have_fixed)
322 get_fixed_ranges(mtrr_state.fixed_ranges);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Jaswinder Singh Rajput52650252009-05-14 12:35:46 +0530324 rdmsr(MSR_MTRRdefType, lo, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 mtrr_state.def_type = (lo & 0xff);
326 mtrr_state.enabled = (lo & 0xc00) >> 10;
Jan Beulich365bff82006-12-07 02:14:09 +0100327
Yinghai Lu35605a12008-03-24 16:02:01 -0700328 if (amd_special_default_mtrr()) {
Thomas Gleixner0da72a42008-04-30 20:11:51 +0200329 unsigned low, high;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530330
Yinghai Lu35605a12008-03-24 16:02:01 -0700331 /* TOP_MEM2 */
Thomas Gleixner0da72a42008-04-30 20:11:51 +0200332 rdmsr(MSR_K8_TOP_MEM2, low, high);
Yinghai Lu95ffa242008-04-29 03:52:33 -0700333 mtrr_tom2 = high;
334 mtrr_tom2 <<= 32;
335 mtrr_tom2 |= low;
Yinghai Lu8004dd92008-05-12 17:40:39 -0700336 mtrr_tom2 &= 0xffffff800000ULL;
Yinghai Lu35605a12008-03-24 16:02:01 -0700337 }
Jan Beulich365bff82006-12-07 02:14:09 +0100338
Yinghai Lu8ad97902009-03-12 18:43:54 -0700339 print_mtrr_state();
340
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700341 mtrr_state_set = 1;
342
343 /* PAT setup for BP. We need to go through sync steps here */
344 local_irq_save(flags);
345 prepare_set();
346
347 pat_init();
348
349 post_set();
350 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530353/* Some BIOS's are messed up and don't set all MTRRs the same! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354void __init mtrr_state_warn(void)
355{
356 unsigned long mask = smp_changes_mask;
357
358 if (!mask)
359 return;
360 if (mask & MTRR_CHANGE_MASK_FIXED)
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530361 pr_warning("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (mask & MTRR_CHANGE_MASK_VARIABLE)
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530363 pr_warning("mtrr: your CPUs had inconsistent variable MTRR settings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (mask & MTRR_CHANGE_MASK_DEFTYPE)
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530365 pr_warning("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 printk(KERN_INFO "mtrr: probably your BIOS does not setup all CPUs.\n");
368 printk(KERN_INFO "mtrr: corrected configuration.\n");
369}
370
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530371/*
372 * Doesn't attempt to pass an error out to MTRR users
373 * because it's quite complicated in some cases and probably not
374 * worth it because the best error handling is to ignore it.
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
377{
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530378 if (wrmsr_safe(msr, a, b) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 printk(KERN_ERR
380 "MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
381 smp_processor_id(), msr, a, b);
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200385/**
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530386 * set_fixed_range - checks & updates a fixed-range MTRR if it
387 * differs from the value it should have
Randy Dunlap1d3381e2008-03-13 16:59:12 -0700388 * @msr: MSR address of the MTTR which should be checked and updated
389 * @changed: pointer which indicates whether the MTRR needed to be changed
390 * @msrwords: pointer to the MSR values which the MSR should have
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200391 */
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100392static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200393{
394 unsigned lo, hi;
395
396 rdmsr(msr, lo, hi);
397
398 if (lo != msrwords[0] || hi != msrwords[1]) {
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200399 mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100400 *changed = true;
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200401 }
402}
403
Randy Dunlap1d3381e2008-03-13 16:59:12 -0700404/**
405 * generic_get_free_region - Get a free MTRR.
406 * @base: The starting (base) address of the region.
407 * @size: The size (in bytes) of the region.
408 * @replace_reg: mtrr index to be replaced; set to invalid value if none.
409 *
410 * Returns: The index of the region on success, else negative on error.
411 */
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530412int
413generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Jan Beulich365bff82006-12-07 02:14:09 +0100415 unsigned long lbase, lsize;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530416 mtrr_type ltype;
417 int i, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 max = num_var_ranges;
Jan Beulich365bff82006-12-07 02:14:09 +0100420 if (replace_reg >= 0 && replace_reg < max)
421 return replace_reg;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 for (i = 0; i < max; ++i) {
424 mtrr_if->get(i, &lbase, &lsize, &ltype);
425 if (lsize == 0)
426 return i;
427 }
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -ENOSPC;
430}
431
Adrian Bunk408b6642005-05-01 08:59:29 -0700432static void generic_get_mtrr(unsigned int reg, unsigned long *base,
Jan Beulich365bff82006-12-07 02:14:09 +0100433 unsigned long *size, mtrr_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 unsigned int mask_lo, mask_hi, base_lo, base_hi;
Yinghai Lu38cc1c32008-08-21 20:24:24 -0700436 unsigned int tmp, hi;
Yinghai Lu63516ef2009-03-13 12:46:07 -0700437 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Yinghai Lu8ad97902009-03-12 18:43:54 -0700439 /*
440 * get_mtrr doesn't need to update mtrr_state, also it could be called
441 * from any cpu, so try to print it out directly.
442 */
Yinghai Lu63516ef2009-03-13 12:46:07 -0700443 cpu = get_cpu();
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
Yinghai Lu8ad97902009-03-12 18:43:54 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if ((mask_lo & 0x800) == 0) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530448 /* Invalid (i.e. free) range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *base = 0;
450 *size = 0;
451 *type = 0;
Yinghai Lu63516ef2009-03-13 12:46:07 -0700452 goto out_put_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
456
Yinghai Lu63516ef2009-03-13 12:46:07 -0700457 /* Work out the shifted address mask: */
Yinghai Lu38cc1c32008-08-21 20:24:24 -0700458 tmp = mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT;
459 mask_lo = size_or_mask | tmp;
Yinghai Lu63516ef2009-03-13 12:46:07 -0700460
461 /* Expand tmp with high bits to all 1s: */
Yinghai Lu38cc1c32008-08-21 20:24:24 -0700462 hi = fls(tmp);
463 if (hi > 0) {
464 tmp |= ~((1<<(hi - 1)) - 1);
465
466 if (tmp != mask_lo) {
Alan Cox942fa3b2010-02-08 10:03:17 +0000467 printk(KERN_WARNING "mtrr: your BIOS has configured an incorrect mask, fixing it.\n");
Yinghai Lu38cc1c32008-08-21 20:24:24 -0700468 mask_lo = tmp;
469 }
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Yinghai Lu63516ef2009-03-13 12:46:07 -0700472 /*
473 * This works correctly if size is a power of two, i.e. a
474 * contiguous range:
475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *size = -mask_lo;
477 *base = base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
478 *type = base_lo & 0xff;
Yinghai Lu8ad97902009-03-12 18:43:54 -0700479
Yinghai Lu63516ef2009-03-13 12:46:07 -0700480out_put_cpu:
481 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200484/**
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530485 * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
486 * differ from the saved set
Randy Dunlap1d3381e2008-03-13 16:59:12 -0700487 * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
Bernhard Kaindlde938c52007-05-02 19:27:17 +0200488 */
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530489static int set_fixed_ranges(mtrr_type *frs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530491 unsigned long long *saved = (unsigned long long *)frs;
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100492 bool changed = false;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530493 int block = -1, range;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Andreas Herrmann3ff42da2009-03-12 17:39:37 +0100495 k8_check_syscfg_dram_mod_en();
496
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530497 while (fixed_range_blocks[++block].ranges) {
498 for (range = 0; range < fixed_range_blocks[block].ranges; range++)
499 set_fixed_range(fixed_range_blocks[block].base_msr + range,
500 &changed, (unsigned int *)saved++);
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return changed;
504}
505
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530506/*
507 * Set the MSR pair relating to a var range.
508 * Returns true if changes are made.
509 */
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100510static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 unsigned int lo, hi;
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100513 bool changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 rdmsr(MTRRphysBase_MSR(index), lo, hi);
516 if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL)
Siddha, Suresh Bcf94b622005-04-16 15:25:11 -0700517 || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
518 (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100521 changed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 rdmsr(MTRRphysMask_MSR(index), lo, hi);
525
526 if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL)
Siddha, Suresh Bcf94b622005-04-16 15:25:11 -0700527 || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
528 (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100530 changed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 return changed;
533}
534
Jan Beulich365bff82006-12-07 02:14:09 +0100535static u32 deftype_lo, deftype_hi;
536
Randy Dunlap1d3381e2008-03-13 16:59:12 -0700537/**
538 * set_mtrr_state - Set the MTRR state for this CPU.
539 *
540 * NOTE: The CPU must already be in a safe state for MTRR changes.
541 * RETURNS: 0 if no changes made, else a mask indicating what was changed.
542 */
Jan Beulich365bff82006-12-07 02:14:09 +0100543static unsigned long set_mtrr_state(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned long change_mask = 0;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530546 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530548 for (i = 0; i < num_var_ranges; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
550 change_mask |= MTRR_CHANGE_MASK_VARIABLE;
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jan Beulich365bff82006-12-07 02:14:09 +0100553 if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 change_mask |= MTRR_CHANGE_MASK_FIXED;
555
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530556 /*
557 * Set_mtrr_restore restores the old value of MTRRdefType,
558 * so to set it we fiddle with the saved value:
559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if ((deftype_lo & 0xff) != mtrr_state.def_type
561 || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530562
563 deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type |
564 (mtrr_state.enabled << 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
566 }
567
568 return change_mask;
569}
570
571
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530572static unsigned long cr4;
Thomas Gleixner40d67532009-07-25 18:33:11 +0200573static DEFINE_RAW_SPINLOCK(set_atomicity_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575/*
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530576 * Since we are disabling the cache don't allow any interrupts,
577 * they would run extremely slow and would only increase the pain.
578 *
579 * The caller must ensure that local interrupts are disabled and
580 * are reenabled after post_set() has been called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Josh Triplett182daa52006-09-25 23:32:36 -0700582static void prepare_set(void) __acquires(set_atomicity_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 unsigned long cr0;
585
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530586 /*
587 * Note that this is not ideal
588 * since the cache is only flushed/disabled for this CPU while the
589 * MTRRs are changed, but changing this requires more invasive
590 * changes to the way the kernel boots
591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Thomas Gleixner40d67532009-07-25 18:33:11 +0200593 raw_spin_lock(&set_atomicity_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530595 /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
Dave Jones7ebad702008-01-30 13:30:39 +0100596 cr0 = read_cr0() | X86_CR0_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 write_cr0(cr0);
598 wbinvd();
599
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530600 /* Save value of CR4 and clear Page Global Enable (bit 7) */
601 if (cpu_has_pge) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 cr4 = read_cr4();
603 write_cr4(cr4 & ~X86_CR4_PGE);
604 }
605
606 /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */
607 __flush_tlb();
608
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530609 /* Save MTRR state */
Jaswinder Singh Rajput52650252009-05-14 12:35:46 +0530610 rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530612 /* Disable MTRRs, and set the default type to uncached */
Jaswinder Singh Rajput52650252009-05-14 12:35:46 +0530613 mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Josh Triplett182daa52006-09-25 23:32:36 -0700616static void post_set(void) __releases(set_atomicity_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530618 /* Flush TLBs (no need to flush caches - they are disabled) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 __flush_tlb();
620
621 /* Intel (P6) standard MTRRs */
Jaswinder Singh Rajput52650252009-05-14 12:35:46 +0530622 mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530623
624 /* Enable caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 write_cr0(read_cr0() & 0xbfffffff);
626
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530627 /* Restore value of CR4 */
628 if (cpu_has_pge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 write_cr4(cr4);
Thomas Gleixner40d67532009-07-25 18:33:11 +0200630 raw_spin_unlock(&set_atomicity_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static void generic_set_all(void)
634{
635 unsigned long mask, count;
636 unsigned long flags;
637
638 local_irq_save(flags);
639 prepare_set();
640
641 /* Actually set the state */
Jan Beulich365bff82006-12-07 02:14:09 +0100642 mask = set_mtrr_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700644 /* also set PAT */
645 pat_init();
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 post_set();
648 local_irq_restore(flags);
649
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530650 /* Use the atomic bitops to update the global mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 for (count = 0; count < sizeof mask * 8; ++count) {
652 if (mask & 0x01)
653 set_bit(count, &smp_changes_mask);
654 mask >>= 1;
655 }
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530659/**
660 * generic_set_mtrr - set variable MTRR register on the local CPU.
661 *
662 * @reg: The register to set.
663 * @base: The base address of the region.
664 * @size: The size of the region. If this is 0 the region is disabled.
665 * @type: The type of the region.
666 *
667 * Returns nothing.
668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static void generic_set_mtrr(unsigned int reg, unsigned long base,
670 unsigned long size, mtrr_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 unsigned long flags;
Shaohua Li3b520b22005-07-07 17:56:38 -0700673 struct mtrr_var_range *vr;
674
675 vr = &mtrr_state.var_ranges[reg];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 local_irq_save(flags);
678 prepare_set();
679
680 if (size == 0) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530681 /*
682 * The invalid bit is kept in the mask, so we simply
683 * clear the relevant mask register to disable a range.
684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
Shaohua Li3b520b22005-07-07 17:56:38 -0700686 memset(vr, 0, sizeof(struct mtrr_var_range));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else {
Shaohua Li3b520b22005-07-07 17:56:38 -0700688 vr->base_lo = base << PAGE_SHIFT | type;
689 vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT);
690 vr->mask_lo = -size << PAGE_SHIFT | 0x800;
691 vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT);
692
693 mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
694 mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 post_set();
698 local_irq_restore(flags);
699}
700
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530701int generic_validate_add_page(unsigned long base, unsigned long size,
702 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 unsigned long lbase, last;
705
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530706 /*
707 * For Intel PPro stepping <= 7
708 * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 &&
711 boot_cpu_data.x86_model == 1 &&
712 boot_cpu_data.x86_mask <= 7) {
713 if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530714 pr_warning("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
716 }
Andreas Mohr9b483412006-12-07 02:14:00 +0100717 if (!(base + size < 0x70000 || base > 0x7003F) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 (type == MTRR_TYPE_WRCOMB
719 || type == MTRR_TYPE_WRBACK)) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530720 pr_warning("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EINVAL;
722 }
723 }
724
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530725 /*
726 * Check upper bits of base and last are equal and lower bits are 0
727 * for base and 1 for last
728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 last = base + size - 1;
730 for (lbase = base; !(lbase & 1) && (last & 1);
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530731 lbase = lbase >> 1, last = last >> 1)
732 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (lbase != last) {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530734 pr_warning("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EINVAL;
736 }
737 return 0;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int generic_have_wrcomb(void)
741{
742 unsigned long config, dummy;
Jaswinder Singh Rajputd9bcc012009-05-14 12:06:12 +0530743 rdmsr(MSR_MTRRcap, config, dummy);
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530744 return config & (1 << 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747int positive_have_wrcomb(void)
748{
749 return 1;
750}
751
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530752/*
753 * Generic structure...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
Emese Revfy3b9cfc02010-01-31 20:16:34 +0100755const struct mtrr_ops generic_mtrr_ops = {
Jaswinder Singh Rajputa1a499a2009-07-04 07:53:00 +0530756 .use_intel_if = 1,
757 .set_all = generic_set_all,
758 .get = generic_get_mtrr,
759 .get_free_region = generic_get_free_region,
760 .set = generic_set_mtrr,
761 .validate_add_page = generic_validate_add_page,
762 .have_wrcomb = generic_have_wrcomb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763};