1 /* This only handles 32bit MTRR on 32bit hosts. This is strictly wrong
2 because MTRRs can span upto 40 bits (36bits on most modern x86) */
3 #include <linux/init.h>
4 #include <linux/slab.h>
9 #include <asm/system.h>
10 #include <asm/cpufeature.h>
11 #include <asm/tlbflush.h>
15 struct mtrr_var_range *var_ranges;
16 mtrr_type fixed_ranges[NUM_FIXED_RANGES];
17 unsigned char enabled;
21 static unsigned long smp_changes_mask;
22 static struct mtrr_state mtrr_state = {};
24 /* Get the MSR pair relating to a var range */
26 get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
28 rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
29 rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
33 get_fixed_ranges(mtrr_type * frs)
35 unsigned int *p = (unsigned int *) frs;
38 rdmsr(MTRRfix64K_00000_MSR, p[0], p[1]);
40 for (i = 0; i < 2; i++)
41 rdmsr(MTRRfix16K_80000_MSR + i, p[2 + i * 2], p[3 + i * 2]);
42 for (i = 0; i < 8; i++)
43 rdmsr(MTRRfix4K_C0000_MSR + i, p[6 + i * 2], p[7 + i * 2]);
46 /* Grab all of the MTRR state for this CPU into *state */
47 void __init get_mtrr_state(void)
50 struct mtrr_var_range *vrs;
53 if (!mtrr_state.var_ranges) {
54 mtrr_state.var_ranges = kmalloc(num_var_ranges * sizeof (struct mtrr_var_range),
56 if (!mtrr_state.var_ranges)
59 vrs = mtrr_state.var_ranges;
61 for (i = 0; i < num_var_ranges; i++)
62 get_mtrr_var_range(i, &vrs[i]);
63 get_fixed_ranges(mtrr_state.fixed_ranges);
65 rdmsr(MTRRdefType_MSR, lo, dummy);
66 mtrr_state.def_type = (lo & 0xff);
67 mtrr_state.enabled = (lo & 0xc00) >> 10;
70 /* Free resources associated with a struct mtrr_state */
71 void __init finalize_mtrr_state(void)
73 kfree(mtrr_state.var_ranges);
74 mtrr_state.var_ranges = NULL;
77 /* Some BIOS's are fucked and don't set all MTRRs the same! */
78 void __init mtrr_state_warn(void)
80 unsigned long mask = smp_changes_mask;
84 if (mask & MTRR_CHANGE_MASK_FIXED)
85 printk(KERN_WARNING "mtrr: your CPUs had inconsistent fixed MTRR settings\n");
86 if (mask & MTRR_CHANGE_MASK_VARIABLE)
87 printk(KERN_WARNING "mtrr: your CPUs had inconsistent variable MTRR settings\n");
88 if (mask & MTRR_CHANGE_MASK_DEFTYPE)
89 printk(KERN_WARNING "mtrr: your CPUs had inconsistent MTRRdefType settings\n");
90 printk(KERN_INFO "mtrr: probably your BIOS does not setup all CPUs.\n");
91 printk(KERN_INFO "mtrr: corrected configuration.\n");
94 /* Doesn't attempt to pass an error out to MTRR users
95 because it's quite complicated in some cases and probably not
96 worth it because the best error handling is to ignore it. */
97 void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
99 if (wrmsr_safe(msr, a, b) < 0)
101 "MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
102 smp_processor_id(), msr, a, b);
105 int generic_get_free_region(unsigned long base, unsigned long size)
106 /* [SUMMARY] Get a free MTRR.
107 <base> The starting (base) address of the region.
108 <size> The size (in bytes) of the region.
109 [RETURNS] The index of the region on success, else -1 on error.
117 max = num_var_ranges;
118 for (i = 0; i < max; ++i) {
119 mtrr_if->get(i, &lbase, &lsize, <ype);
126 static void generic_get_mtrr(unsigned int reg, unsigned long *base,
127 unsigned int *size, mtrr_type * type)
129 unsigned int mask_lo, mask_hi, base_lo, base_hi;
131 rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
132 if ((mask_lo & 0x800) == 0) {
133 /* Invalid (i.e. free) range */
140 rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
142 /* Work out the shifted address mask. */
143 mask_lo = size_or_mask | mask_hi << (32 - PAGE_SHIFT)
144 | mask_lo >> PAGE_SHIFT;
146 /* This works correctly if size is a power of two, i.e. a
149 *base = base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
150 *type = base_lo & 0xff;
153 static int set_fixed_ranges(mtrr_type * frs)
155 unsigned int *p = (unsigned int *) frs;
160 rdmsr(MTRRfix64K_00000_MSR, lo, hi);
161 if (p[0] != lo || p[1] != hi) {
162 mtrr_wrmsr(MTRRfix64K_00000_MSR, p[0], p[1]);
166 for (i = 0; i < 2; i++) {
167 rdmsr(MTRRfix16K_80000_MSR + i, lo, hi);
168 if (p[2 + i * 2] != lo || p[3 + i * 2] != hi) {
169 mtrr_wrmsr(MTRRfix16K_80000_MSR + i, p[2 + i * 2],
175 for (i = 0; i < 8; i++) {
176 rdmsr(MTRRfix4K_C0000_MSR + i, lo, hi);
177 if (p[6 + i * 2] != lo || p[7 + i * 2] != hi) {
178 mtrr_wrmsr(MTRRfix4K_C0000_MSR + i, p[6 + i * 2],
186 /* Set the MSR pair relating to a var range. Returns TRUE if
188 static int set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
193 rdmsr(MTRRphysBase_MSR(index), lo, hi);
194 if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL)
195 || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
196 (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
197 mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
201 rdmsr(MTRRphysMask_MSR(index), lo, hi);
203 if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL)
204 || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
205 (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
206 mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
212 static unsigned long set_mtrr_state(u32 deftype_lo, u32 deftype_hi)
213 /* [SUMMARY] Set the MTRR state for this CPU.
214 <state> The MTRR state information to read.
215 <ctxt> Some relevant CPU context.
216 [NOTE] The CPU must already be in a safe state for MTRR changes.
217 [RETURNS] 0 if no changes made, else a mask indication what was changed.
221 unsigned long change_mask = 0;
223 for (i = 0; i < num_var_ranges; i++)
224 if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
225 change_mask |= MTRR_CHANGE_MASK_VARIABLE;
227 if (set_fixed_ranges(mtrr_state.fixed_ranges))
228 change_mask |= MTRR_CHANGE_MASK_FIXED;
230 /* Set_mtrr_restore restores the old value of MTRRdefType,
231 so to set it we fiddle with the saved value */
232 if ((deftype_lo & 0xff) != mtrr_state.def_type
233 || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) {
234 deftype_lo |= (mtrr_state.def_type | mtrr_state.enabled << 10);
235 change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
242 static unsigned long cr4 = 0;
243 static u32 deftype_lo, deftype_hi;
244 static DEFINE_SPINLOCK(set_atomicity_lock);
247 * Since we are disabling the cache don't allow any interrupts - they
248 * would run extremely slow and would only increase the pain. The caller must
249 * ensure that local interrupts are disabled and are reenabled after post_set()
253 static void prepare_set(void)
257 /* Note that this is not ideal, since the cache is only flushed/disabled
258 for this CPU while the MTRRs are changed, but changing this requires
259 more invasive changes to the way the kernel boots */
261 spin_lock(&set_atomicity_lock);
263 /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
264 cr0 = read_cr0() | 0x40000000; /* set CD flag */
268 /* Save value of CR4 and clear Page Global Enable (bit 7) */
271 write_cr4(cr4 & ~X86_CR4_PGE);
274 /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */
277 /* Save MTRR state */
278 rdmsr(MTRRdefType_MSR, deftype_lo, deftype_hi);
280 /* Disable MTRRs, and set the default type to uncached */
281 mtrr_wrmsr(MTRRdefType_MSR, deftype_lo & 0xf300UL, deftype_hi);
284 static void post_set(void)
286 /* Flush TLBs (no need to flush caches - they are disabled) */
289 /* Intel (P6) standard MTRRs */
290 mtrr_wrmsr(MTRRdefType_MSR, deftype_lo, deftype_hi);
293 write_cr0(read_cr0() & 0xbfffffff);
295 /* Restore value of CR4 */
298 spin_unlock(&set_atomicity_lock);
301 static void generic_set_all(void)
303 unsigned long mask, count;
306 local_irq_save(flags);
309 /* Actually set the state */
310 mask = set_mtrr_state(deftype_lo,deftype_hi);
313 local_irq_restore(flags);
315 /* Use the atomic bitops to update the global mask */
316 for (count = 0; count < sizeof mask * 8; ++count) {
318 set_bit(count, &smp_changes_mask);
324 static void generic_set_mtrr(unsigned int reg, unsigned long base,
325 unsigned long size, mtrr_type type)
326 /* [SUMMARY] Set variable MTRR register on the local CPU.
327 <reg> The register to set.
328 <base> The base address of the region.
329 <size> The size of the region. If this is 0 the region is disabled.
330 <type> The type of the region.
331 <do_safe> If TRUE, do the change safely. If FALSE, safety measures should
338 local_irq_save(flags);
342 /* The invalid bit is kept in the mask, so we simply clear the
343 relevant mask register to disable a range. */
344 mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
346 mtrr_wrmsr(MTRRphysBase_MSR(reg), base << PAGE_SHIFT | type,
347 (base & size_and_mask) >> (32 - PAGE_SHIFT));
348 mtrr_wrmsr(MTRRphysMask_MSR(reg), -size << PAGE_SHIFT | 0x800,
349 (-size & size_and_mask) >> (32 - PAGE_SHIFT));
353 local_irq_restore(flags);
356 int generic_validate_add_page(unsigned long base, unsigned long size, unsigned int type)
358 unsigned long lbase, last;
360 /* For Intel PPro stepping <= 7, must be 4 MiB aligned
361 and not touch 0x70000000->0x7003FFFF */
362 if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 &&
363 boot_cpu_data.x86_model == 1 &&
364 boot_cpu_data.x86_mask <= 7) {
365 if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
366 printk(KERN_WARNING "mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
369 if (!(base + size < 0x70000000 || base > 0x7003FFFF) &&
370 (type == MTRR_TYPE_WRCOMB
371 || type == MTRR_TYPE_WRBACK)) {
372 printk(KERN_WARNING "mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
377 if (base + size < 0x100) {
378 printk(KERN_WARNING "mtrr: cannot set region below 1 MiB (0x%lx000,0x%lx000)\n",
382 /* Check upper bits of base and last are equal and lower bits are 0
383 for base and 1 for last */
384 last = base + size - 1;
385 for (lbase = base; !(lbase & 1) && (last & 1);
386 lbase = lbase >> 1, last = last >> 1) ;
388 printk(KERN_WARNING "mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n",
396 static int generic_have_wrcomb(void)
398 unsigned long config, dummy;
399 rdmsr(MTRRcap_MSR, config, dummy);
400 return (config & (1 << 10));
403 int positive_have_wrcomb(void)
408 /* generic structure...
410 struct mtrr_ops generic_mtrr_ops = {
412 .set_all = generic_set_all,
413 .get = generic_get_mtrr,
414 .get_free_region = generic_get_free_region,
415 .set = generic_set_mtrr,
416 .validate_add_page = generic_validate_add_page,
417 .have_wrcomb = generic_have_wrcomb,