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