]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/cpu/mtrr/generic.c
mtrr, mm, x86: enhance MTRR checks for KVA huge page mapping
[karo-tx-linux.git] / arch / x86 / kernel / cpu / mtrr / generic.c
1 /*
2  * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong
3  * because MTRRs can span up to 40 bits (36bits on most modern x86)
4  */
5 #define DEBUG
6
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/io.h>
10 #include <linux/mm.h>
11
12 #include <asm/processor-flags.h>
13 #include <asm/cpufeature.h>
14 #include <asm/tlbflush.h>
15 #include <asm/mtrr.h>
16 #include <asm/msr.h>
17 #include <asm/pat.h>
18
19 #include "mtrr.h"
20
21 struct fixed_range_block {
22         int base_msr;           /* start address of an MTRR block */
23         int ranges;             /* number of MTRRs in this block  */
24 };
25
26 static struct fixed_range_block fixed_range_blocks[] = {
27         { MSR_MTRRfix64K_00000, 1 }, /* one   64k MTRR  */
28         { MSR_MTRRfix16K_80000, 2 }, /* two   16k MTRRs */
29         { MSR_MTRRfix4K_C0000,  8 }, /* eight  4k MTRRs */
30         {}
31 };
32
33 static unsigned long smp_changes_mask;
34 static int mtrr_state_set;
35 u64 mtrr_tom2;
36
37 struct mtrr_state_type mtrr_state;
38 EXPORT_SYMBOL_GPL(mtrr_state);
39
40 /*
41  * BIOS is expected to clear MtrrFixDramModEn bit, see for example
42  * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
43  * Opteron Processors" (26094 Rev. 3.30 February 2006), section
44  * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
45  * to 1 during BIOS initalization of the fixed MTRRs, then cleared to
46  * 0 for operation."
47  */
48 static inline void k8_check_syscfg_dram_mod_en(void)
49 {
50         u32 lo, hi;
51
52         if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
53               (boot_cpu_data.x86 >= 0x0f)))
54                 return;
55
56         rdmsr(MSR_K8_SYSCFG, lo, hi);
57         if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
58                 printk(KERN_ERR FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
59                        " not cleared by BIOS, clearing this bit\n",
60                        smp_processor_id());
61                 lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
62                 mtrr_wrmsr(MSR_K8_SYSCFG, lo, hi);
63         }
64 }
65
66 /* Get the size of contiguous MTRR range */
67 static u64 get_mtrr_size(u64 mask)
68 {
69         u64 size;
70
71         mask >>= PAGE_SHIFT;
72         mask |= size_or_mask;
73         size = -mask;
74         size <<= PAGE_SHIFT;
75         return size;
76 }
77
78 /*
79  * Check and return the effective type for MTRR-MTRR type overlap.
80  * Returns 1 if the effective type is UNCACHEABLE, else returns 0
81  */
82 static int check_type_overlap(u8 *prev, u8 *curr)
83 {
84         if (*prev == MTRR_TYPE_UNCACHABLE || *curr == MTRR_TYPE_UNCACHABLE) {
85                 *prev = MTRR_TYPE_UNCACHABLE;
86                 *curr = MTRR_TYPE_UNCACHABLE;
87                 return 1;
88         }
89
90         if ((*prev == MTRR_TYPE_WRBACK && *curr == MTRR_TYPE_WRTHROUGH) ||
91             (*prev == MTRR_TYPE_WRTHROUGH && *curr == MTRR_TYPE_WRBACK)) {
92                 *prev = MTRR_TYPE_WRTHROUGH;
93                 *curr = MTRR_TYPE_WRTHROUGH;
94         }
95
96         if (*prev != *curr) {
97                 *prev = MTRR_TYPE_UNCACHABLE;
98                 *curr = MTRR_TYPE_UNCACHABLE;
99                 return 1;
100         }
101
102         return 0;
103 }
104
105 /**
106  * mtrr_type_lookup_fixed - look up memory type in MTRR fixed entries
107  *
108  * MTRR fixed entries are divided into the following ways:
109  *  0x00000 - 0x7FFFF : This range is divided into eight 64KB sub-ranges
110  *  0x80000 - 0xBFFFF : This range is divided into sixteen 16KB sub-ranges
111  *  0xC0000 - 0xFFFFF : This range is divided into sixty-four 4KB sub-ranges
112  *
113  * Return Values:
114  * MTRR_TYPE_(type)  - Matched memory type
115  * MTRR_TYPE_INVALID - Unmatched or fixed entries are disabled
116  */
117 static u8 mtrr_type_lookup_fixed(u64 start, u64 end)
118 {
119         int idx;
120
121         if (start >= 0x100000)
122                 return MTRR_TYPE_INVALID;
123
124         if (!(mtrr_state.have_fixed) ||
125             !(mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED))
126                 return MTRR_TYPE_INVALID;
127
128         if (start < 0x80000) {          /* 0x0 - 0x7FFFF */
129                 idx = 0;
130                 idx += (start >> 16);
131                 return mtrr_state.fixed_ranges[idx];
132
133         } else if (start < 0xC0000) {   /* 0x80000 - 0xBFFFF */
134                 idx = 1 * 8;
135                 idx += ((start - 0x80000) >> 14);
136                 return mtrr_state.fixed_ranges[idx];
137         }
138
139         /* 0xC0000 - 0xFFFFF */
140         idx = 3 * 8;
141         idx += ((start - 0xC0000) >> 12);
142         return mtrr_state.fixed_ranges[idx];
143 }
144
145 /**
146  * mtrr_type_lookup_variable - look up memory type in MTRR variable entries
147  *
148  * Return Value:
149  * MTRR_TYPE_(type) - Matched memory type or default memory type (unmatched)
150  *
151  * Output Arguments:
152  * repeat - Set to 1 when [start:end] spanned across MTRR range and type
153  *          returned corresponds only to [start:*partial_end].  Caller has
154  *          to lookup again for [*partial_end:end].
155  * uniform - Set to 1 when MTRR covers the region uniformly, i.e. the region
156  *           is fully covered by a single MTRR entry or the default type.
157  */
158 static u8 mtrr_type_lookup_variable(u64 start, u64 end, u64 *partial_end,
159                                     int *repeat, u8 *uniform)
160 {
161         int i;
162         u64 base, mask;
163         u8 prev_match, curr_match;
164
165         *repeat = 0;
166         *uniform = 1;
167
168         /* Make end inclusive end, instead of exclusive */
169         end--;
170
171         prev_match = MTRR_TYPE_INVALID;
172         for (i = 0; i < num_var_ranges; ++i) {
173                 unsigned short start_state, end_state, inclusive;
174
175                 if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11)))
176                         continue;
177
178                 base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) +
179                        (mtrr_state.var_ranges[i].base_lo & PAGE_MASK);
180                 mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) +
181                        (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK);
182
183                 start_state = ((start & mask) == (base & mask));
184                 end_state = ((end & mask) == (base & mask));
185                 inclusive = ((start < base) && (end > base));
186
187                 if ((start_state != end_state) || inclusive) {
188                         /*
189                          * We have start:end spanning across an MTRR.
190                          * We split the region into either
191                          * - start_state:1
192                          *     (start:mtrr_end) (mtrr_end:end)
193                          * - end_state:1 or inclusive:1
194                          *     (start:mtrr_start) (mtrr_start:end)
195                          * depending on kind of overlap.
196                          * Return the type for first region and a pointer to
197                          * the start of second region so that caller will
198                          * lookup again on the second region.
199                          * Note: This way we handle overlaps with multiple
200                          * entries and the default type properly.
201                          */
202                         if (start_state)
203                                 *partial_end = base + get_mtrr_size(mask);
204                         else
205                                 *partial_end = base;
206
207                         if (unlikely(*partial_end <= start)) {
208                                 WARN_ON(1);
209                                 *partial_end = start + PAGE_SIZE;
210                         }
211
212                         end = *partial_end - 1; /* end is inclusive */
213                         *repeat = 1;
214                         *uniform = 0;
215                 }
216
217                 if (!start_state)
218                         continue;
219
220                 curr_match = mtrr_state.var_ranges[i].base_lo & 0xff;
221                 if (prev_match == MTRR_TYPE_INVALID) {
222                         prev_match = curr_match;
223                         continue;
224                 }
225
226                 *uniform = 0;
227                 if (check_type_overlap(&prev_match, &curr_match))
228                         return curr_match;
229         }
230
231         if (prev_match != MTRR_TYPE_INVALID)
232                 return prev_match;
233
234         return mtrr_state.def_type;
235 }
236
237 /**
238  * mtrr_type_lookup - look up memory type in MTRR
239  *
240  * Return Values:
241  * MTRR_TYPE_(type)  - The effective MTRR type for the region
242  * MTRR_TYPE_INVALID - MTRR is disabled
243  *
244  * Output Argument:
245  * uniform - Set to 1 when MTRR covers the region uniformly, i.e. the region
246  *           is fully covered by a single MTRR entry or the default type.
247  */
248 u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
249 {
250         u8 type, prev_type, is_uniform, dummy;
251         int repeat;
252         u64 partial_end;
253
254         *uniform = 1;
255
256         if (!mtrr_state_set)
257                 return MTRR_TYPE_INVALID;
258
259         if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED))
260                 return MTRR_TYPE_INVALID;
261
262         /*
263          * Look up the fixed ranges first, which take priority over
264          * the variable ranges.
265          */
266         type = mtrr_type_lookup_fixed(start, end);
267         if (type != MTRR_TYPE_INVALID) {
268                 *uniform = 0;
269                 return type;
270         }
271
272         /*
273          * Look up the variable ranges.  Look of multiple ranges matching
274          * this address and pick type as per MTRR precedence.
275          */
276         type = mtrr_type_lookup_variable(start, end, &partial_end,
277                                          &repeat, &is_uniform);
278
279         /*
280          * Common path is with repeat = 0.
281          * However, we can have cases where [start:end] spans across some
282          * MTRR ranges and/or the default type.  Do repeated lookups for
283          * that case here.
284          */
285         while (repeat) {
286                 prev_type = type;
287                 start = partial_end;
288                 is_uniform = 0;
289
290                 type = mtrr_type_lookup_variable(start, end, &partial_end,
291                                                  &repeat, &dummy);
292
293                 if (check_type_overlap(&prev_type, &type)) {
294                         *uniform = 0;
295                         return type;
296                 }
297         }
298
299         if (mtrr_tom2 && (start >= (1ULL<<32)) && (end < mtrr_tom2))
300                 return MTRR_TYPE_WRBACK;
301
302         *uniform = is_uniform;
303         return type;
304 }
305
306 /* Get the MSR pair relating to a var range */
307 static void
308 get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
309 {
310         rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
311         rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
312 }
313
314 /* Fill the MSR pair relating to a var range */
315 void fill_mtrr_var_range(unsigned int index,
316                 u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
317 {
318         struct mtrr_var_range *vr;
319
320         vr = mtrr_state.var_ranges;
321
322         vr[index].base_lo = base_lo;
323         vr[index].base_hi = base_hi;
324         vr[index].mask_lo = mask_lo;
325         vr[index].mask_hi = mask_hi;
326 }
327
328 static void get_fixed_ranges(mtrr_type *frs)
329 {
330         unsigned int *p = (unsigned int *)frs;
331         int i;
332
333         k8_check_syscfg_dram_mod_en();
334
335         rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
336
337         for (i = 0; i < 2; i++)
338                 rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
339         for (i = 0; i < 8; i++)
340                 rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
341 }
342
343 void mtrr_save_fixed_ranges(void *info)
344 {
345         if (cpu_has_mtrr)
346                 get_fixed_ranges(mtrr_state.fixed_ranges);
347 }
348
349 static unsigned __initdata last_fixed_start;
350 static unsigned __initdata last_fixed_end;
351 static mtrr_type __initdata last_fixed_type;
352
353 static void __init print_fixed_last(void)
354 {
355         if (!last_fixed_end)
356                 return;
357
358         pr_debug("  %05X-%05X %s\n", last_fixed_start,
359                  last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
360
361         last_fixed_end = 0;
362 }
363
364 static void __init update_fixed_last(unsigned base, unsigned end,
365                                      mtrr_type type)
366 {
367         last_fixed_start = base;
368         last_fixed_end = end;
369         last_fixed_type = type;
370 }
371
372 static void __init
373 print_fixed(unsigned base, unsigned step, const mtrr_type *types)
374 {
375         unsigned i;
376
377         for (i = 0; i < 8; ++i, ++types, base += step) {
378                 if (last_fixed_end == 0) {
379                         update_fixed_last(base, base + step, *types);
380                         continue;
381                 }
382                 if (last_fixed_end == base && last_fixed_type == *types) {
383                         last_fixed_end = base + step;
384                         continue;
385                 }
386                 /* new segments: gap or different type */
387                 print_fixed_last();
388                 update_fixed_last(base, base + step, *types);
389         }
390 }
391
392 static void prepare_set(void);
393 static void post_set(void);
394
395 static void __init print_mtrr_state(void)
396 {
397         unsigned int i;
398         int high_width;
399
400         pr_debug("MTRR default type: %s\n",
401                  mtrr_attrib_to_str(mtrr_state.def_type));
402         if (mtrr_state.have_fixed) {
403                 pr_debug("MTRR fixed ranges %sabled:\n",
404                         ((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) &&
405                          (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ?
406                          "en" : "dis");
407                 print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
408                 for (i = 0; i < 2; ++i)
409                         print_fixed(0x80000 + i * 0x20000, 0x04000,
410                                     mtrr_state.fixed_ranges + (i + 1) * 8);
411                 for (i = 0; i < 8; ++i)
412                         print_fixed(0xC0000 + i * 0x08000, 0x01000,
413                                     mtrr_state.fixed_ranges + (i + 3) * 8);
414
415                 /* tail */
416                 print_fixed_last();
417         }
418         pr_debug("MTRR variable ranges %sabled:\n",
419                  mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis");
420         high_width = (__ffs64(size_or_mask) - (32 - PAGE_SHIFT) + 3) / 4;
421
422         for (i = 0; i < num_var_ranges; ++i) {
423                 if (mtrr_state.var_ranges[i].mask_lo & (1 << 11))
424                         pr_debug("  %u base %0*X%05X000 mask %0*X%05X000 %s\n",
425                                  i,
426                                  high_width,
427                                  mtrr_state.var_ranges[i].base_hi,
428                                  mtrr_state.var_ranges[i].base_lo >> 12,
429                                  high_width,
430                                  mtrr_state.var_ranges[i].mask_hi,
431                                  mtrr_state.var_ranges[i].mask_lo >> 12,
432                                  mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff));
433                 else
434                         pr_debug("  %u disabled\n", i);
435         }
436         if (mtrr_tom2)
437                 pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
438 }
439
440 /* Grab all of the MTRR state for this CPU into *state */
441 void __init get_mtrr_state(void)
442 {
443         struct mtrr_var_range *vrs;
444         unsigned long flags;
445         unsigned lo, dummy;
446         unsigned int i;
447
448         vrs = mtrr_state.var_ranges;
449
450         rdmsr(MSR_MTRRcap, lo, dummy);
451         mtrr_state.have_fixed = (lo >> 8) & 1;
452
453         for (i = 0; i < num_var_ranges; i++)
454                 get_mtrr_var_range(i, &vrs[i]);
455         if (mtrr_state.have_fixed)
456                 get_fixed_ranges(mtrr_state.fixed_ranges);
457
458         rdmsr(MSR_MTRRdefType, lo, dummy);
459         mtrr_state.def_type = (lo & 0xff);
460         mtrr_state.enabled = (lo & 0xc00) >> 10;
461
462         if (amd_special_default_mtrr()) {
463                 unsigned low, high;
464
465                 /* TOP_MEM2 */
466                 rdmsr(MSR_K8_TOP_MEM2, low, high);
467                 mtrr_tom2 = high;
468                 mtrr_tom2 <<= 32;
469                 mtrr_tom2 |= low;
470                 mtrr_tom2 &= 0xffffff800000ULL;
471         }
472
473         print_mtrr_state();
474
475         mtrr_state_set = 1;
476
477         /* PAT setup for BP. We need to go through sync steps here */
478         local_irq_save(flags);
479         prepare_set();
480
481         pat_init();
482
483         post_set();
484         local_irq_restore(flags);
485 }
486
487 /* Some BIOS's are messed up and don't set all MTRRs the same! */
488 void __init mtrr_state_warn(void)
489 {
490         unsigned long mask = smp_changes_mask;
491
492         if (!mask)
493                 return;
494         if (mask & MTRR_CHANGE_MASK_FIXED)
495                 pr_warning("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
496         if (mask & MTRR_CHANGE_MASK_VARIABLE)
497                 pr_warning("mtrr: your CPUs had inconsistent variable MTRR settings\n");
498         if (mask & MTRR_CHANGE_MASK_DEFTYPE)
499                 pr_warning("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
500
501         printk(KERN_INFO "mtrr: probably your BIOS does not setup all CPUs.\n");
502         printk(KERN_INFO "mtrr: corrected configuration.\n");
503 }
504
505 /*
506  * Doesn't attempt to pass an error out to MTRR users
507  * because it's quite complicated in some cases and probably not
508  * worth it because the best error handling is to ignore it.
509  */
510 void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
511 {
512         if (wrmsr_safe(msr, a, b) < 0) {
513                 printk(KERN_ERR
514                         "MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
515                         smp_processor_id(), msr, a, b);
516         }
517 }
518
519 /**
520  * set_fixed_range - checks & updates a fixed-range MTRR if it
521  *                   differs from the value it should have
522  * @msr: MSR address of the MTTR which should be checked and updated
523  * @changed: pointer which indicates whether the MTRR needed to be changed
524  * @msrwords: pointer to the MSR values which the MSR should have
525  */
526 static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
527 {
528         unsigned lo, hi;
529
530         rdmsr(msr, lo, hi);
531
532         if (lo != msrwords[0] || hi != msrwords[1]) {
533                 mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
534                 *changed = true;
535         }
536 }
537
538 /**
539  * generic_get_free_region - Get a free MTRR.
540  * @base: The starting (base) address of the region.
541  * @size: The size (in bytes) of the region.
542  * @replace_reg: mtrr index to be replaced; set to invalid value if none.
543  *
544  * Returns: The index of the region on success, else negative on error.
545  */
546 int
547 generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
548 {
549         unsigned long lbase, lsize;
550         mtrr_type ltype;
551         int i, max;
552
553         max = num_var_ranges;
554         if (replace_reg >= 0 && replace_reg < max)
555                 return replace_reg;
556
557         for (i = 0; i < max; ++i) {
558                 mtrr_if->get(i, &lbase, &lsize, &ltype);
559                 if (lsize == 0)
560                         return i;
561         }
562
563         return -ENOSPC;
564 }
565
566 static void generic_get_mtrr(unsigned int reg, unsigned long *base,
567                              unsigned long *size, mtrr_type *type)
568 {
569         u32 mask_lo, mask_hi, base_lo, base_hi;
570         unsigned int hi;
571         u64 tmp, mask;
572
573         /*
574          * get_mtrr doesn't need to update mtrr_state, also it could be called
575          * from any cpu, so try to print it out directly.
576          */
577         get_cpu();
578
579         rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
580
581         if ((mask_lo & 0x800) == 0) {
582                 /*  Invalid (i.e. free) range */
583                 *base = 0;
584                 *size = 0;
585                 *type = 0;
586                 goto out_put_cpu;
587         }
588
589         rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
590
591         /* Work out the shifted address mask: */
592         tmp = (u64)mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT;
593         mask = size_or_mask | tmp;
594
595         /* Expand tmp with high bits to all 1s: */
596         hi = fls64(tmp);
597         if (hi > 0) {
598                 tmp |= ~((1ULL<<(hi - 1)) - 1);
599
600                 if (tmp != mask) {
601                         printk(KERN_WARNING "mtrr: your BIOS has configured an incorrect mask, fixing it.\n");
602                         add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
603                         mask = tmp;
604                 }
605         }
606
607         /*
608          * This works correctly if size is a power of two, i.e. a
609          * contiguous range:
610          */
611         *size = -mask;
612         *base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
613         *type = base_lo & 0xff;
614
615 out_put_cpu:
616         put_cpu();
617 }
618
619 /**
620  * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
621  *                    differ from the saved set
622  * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
623  */
624 static int set_fixed_ranges(mtrr_type *frs)
625 {
626         unsigned long long *saved = (unsigned long long *)frs;
627         bool changed = false;
628         int block = -1, range;
629
630         k8_check_syscfg_dram_mod_en();
631
632         while (fixed_range_blocks[++block].ranges) {
633                 for (range = 0; range < fixed_range_blocks[block].ranges; range++)
634                         set_fixed_range(fixed_range_blocks[block].base_msr + range,
635                                         &changed, (unsigned int *)saved++);
636         }
637
638         return changed;
639 }
640
641 /*
642  * Set the MSR pair relating to a var range.
643  * Returns true if changes are made.
644  */
645 static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
646 {
647         unsigned int lo, hi;
648         bool changed = false;
649
650         rdmsr(MTRRphysBase_MSR(index), lo, hi);
651         if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL)
652             || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
653                 (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
654
655                 mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
656                 changed = true;
657         }
658
659         rdmsr(MTRRphysMask_MSR(index), lo, hi);
660
661         if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL)
662             || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
663                 (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
664                 mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
665                 changed = true;
666         }
667         return changed;
668 }
669
670 static u32 deftype_lo, deftype_hi;
671
672 /**
673  * set_mtrr_state - Set the MTRR state for this CPU.
674  *
675  * NOTE: The CPU must already be in a safe state for MTRR changes.
676  * RETURNS: 0 if no changes made, else a mask indicating what was changed.
677  */
678 static unsigned long set_mtrr_state(void)
679 {
680         unsigned long change_mask = 0;
681         unsigned int i;
682
683         for (i = 0; i < num_var_ranges; i++) {
684                 if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
685                         change_mask |= MTRR_CHANGE_MASK_VARIABLE;
686         }
687
688         if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
689                 change_mask |= MTRR_CHANGE_MASK_FIXED;
690
691         /*
692          * Set_mtrr_restore restores the old value of MTRRdefType,
693          * so to set it we fiddle with the saved value:
694          */
695         if ((deftype_lo & 0xff) != mtrr_state.def_type
696             || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) {
697
698                 deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type |
699                              (mtrr_state.enabled << 10);
700                 change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
701         }
702
703         return change_mask;
704 }
705
706
707 static unsigned long cr4;
708 static DEFINE_RAW_SPINLOCK(set_atomicity_lock);
709
710 /*
711  * Since we are disabling the cache don't allow any interrupts,
712  * they would run extremely slow and would only increase the pain.
713  *
714  * The caller must ensure that local interrupts are disabled and
715  * are reenabled after post_set() has been called.
716  */
717 static void prepare_set(void) __acquires(set_atomicity_lock)
718 {
719         unsigned long cr0;
720
721         /*
722          * Note that this is not ideal
723          * since the cache is only flushed/disabled for this CPU while the
724          * MTRRs are changed, but changing this requires more invasive
725          * changes to the way the kernel boots
726          */
727
728         raw_spin_lock(&set_atomicity_lock);
729
730         /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
731         cr0 = read_cr0() | X86_CR0_CD;
732         write_cr0(cr0);
733         wbinvd();
734
735         /* Save value of CR4 and clear Page Global Enable (bit 7) */
736         if (cpu_has_pge) {
737                 cr4 = __read_cr4();
738                 __write_cr4(cr4 & ~X86_CR4_PGE);
739         }
740
741         /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */
742         count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
743         __flush_tlb();
744
745         /* Save MTRR state */
746         rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
747
748         /* Disable MTRRs, and set the default type to uncached */
749         mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
750         wbinvd();
751 }
752
753 static void post_set(void) __releases(set_atomicity_lock)
754 {
755         /* Flush TLBs (no need to flush caches - they are disabled) */
756         count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
757         __flush_tlb();
758
759         /* Intel (P6) standard MTRRs */
760         mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
761
762         /* Enable caches */
763         write_cr0(read_cr0() & ~X86_CR0_CD);
764
765         /* Restore value of CR4 */
766         if (cpu_has_pge)
767                 __write_cr4(cr4);
768         raw_spin_unlock(&set_atomicity_lock);
769 }
770
771 static void generic_set_all(void)
772 {
773         unsigned long mask, count;
774         unsigned long flags;
775
776         local_irq_save(flags);
777         prepare_set();
778
779         /* Actually set the state */
780         mask = set_mtrr_state();
781
782         /* also set PAT */
783         pat_init();
784
785         post_set();
786         local_irq_restore(flags);
787
788         /* Use the atomic bitops to update the global mask */
789         for (count = 0; count < sizeof mask * 8; ++count) {
790                 if (mask & 0x01)
791                         set_bit(count, &smp_changes_mask);
792                 mask >>= 1;
793         }
794
795 }
796
797 /**
798  * generic_set_mtrr - set variable MTRR register on the local CPU.
799  *
800  * @reg: The register to set.
801  * @base: The base address of the region.
802  * @size: The size of the region. If this is 0 the region is disabled.
803  * @type: The type of the region.
804  *
805  * Returns nothing.
806  */
807 static void generic_set_mtrr(unsigned int reg, unsigned long base,
808                              unsigned long size, mtrr_type type)
809 {
810         unsigned long flags;
811         struct mtrr_var_range *vr;
812
813         vr = &mtrr_state.var_ranges[reg];
814
815         local_irq_save(flags);
816         prepare_set();
817
818         if (size == 0) {
819                 /*
820                  * The invalid bit is kept in the mask, so we simply
821                  * clear the relevant mask register to disable a range.
822                  */
823                 mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
824                 memset(vr, 0, sizeof(struct mtrr_var_range));
825         } else {
826                 vr->base_lo = base << PAGE_SHIFT | type;
827                 vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT);
828                 vr->mask_lo = -size << PAGE_SHIFT | 0x800;
829                 vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT);
830
831                 mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
832                 mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
833         }
834
835         post_set();
836         local_irq_restore(flags);
837 }
838
839 int generic_validate_add_page(unsigned long base, unsigned long size,
840                               unsigned int type)
841 {
842         unsigned long lbase, last;
843
844         /*
845          * For Intel PPro stepping <= 7
846          * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
847          */
848         if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 &&
849             boot_cpu_data.x86_model == 1 &&
850             boot_cpu_data.x86_mask <= 7) {
851                 if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
852                         pr_warning("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
853                         return -EINVAL;
854                 }
855                 if (!(base + size < 0x70000 || base > 0x7003F) &&
856                     (type == MTRR_TYPE_WRCOMB
857                      || type == MTRR_TYPE_WRBACK)) {
858                         pr_warning("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
859                         return -EINVAL;
860                 }
861         }
862
863         /*
864          * Check upper bits of base and last are equal and lower bits are 0
865          * for base and 1 for last
866          */
867         last = base + size - 1;
868         for (lbase = base; !(lbase & 1) && (last & 1);
869              lbase = lbase >> 1, last = last >> 1)
870                 ;
871         if (lbase != last) {
872                 pr_warning("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
873                 return -EINVAL;
874         }
875         return 0;
876 }
877
878 static int generic_have_wrcomb(void)
879 {
880         unsigned long config, dummy;
881         rdmsr(MSR_MTRRcap, config, dummy);
882         return config & (1 << 10);
883 }
884
885 int positive_have_wrcomb(void)
886 {
887         return 1;
888 }
889
890 /*
891  * Generic structure...
892  */
893 const struct mtrr_ops generic_mtrr_ops = {
894         .use_intel_if           = 1,
895         .set_all                = generic_set_all,
896         .get                    = generic_get_mtrr,
897         .get_free_region        = generic_get_free_region,
898         .set                    = generic_set_mtrr,
899         .validate_add_page      = generic_validate_add_page,
900         .have_wrcomb            = generic_have_wrcomb,
901 };