x86, mtrr, pat: Fix one cpu getting out of sync during resume
[linux-flexiantxendom0-natty.git] / arch / x86 / kernel / cpu / mtrr / main.c
1 /*  Generic MTRR (Memory Type Range Register) driver.
2
3     Copyright (C) 1997-2000  Richard Gooch
4     Copyright (c) 2002       Patrick Mochel
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Library General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Library General Public License for more details.
15
16     You should have received a copy of the GNU Library General Public
17     License along with this library; if not, write to the Free
18     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
21     The postal address is:
22       Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
23
24     Source: "Pentium Pro Family Developer's Manual, Volume 3:
25     Operating System Writer's Guide" (Intel document number 242692),
26     section 11.11.7
27
28     This was cleaned and made readable by Patrick Mochel <mochel@osdl.org>
29     on 6-7 March 2002.
30     Source: Intel Architecture Software Developers Manual, Volume 3:
31     System Programming Guide; Section 9.11. (1997 edition - PPro).
32 */
33
34 #define DEBUG
35
36 #include <linux/types.h> /* FIXME: kvm_para.h needs this */
37
38 #include <linux/stop_machine.h>
39 #include <linux/kvm_para.h>
40 #include <linux/uaccess.h>
41 #include <linux/module.h>
42 #include <linux/mutex.h>
43 #include <linux/init.h>
44 #include <linux/sort.h>
45 #include <linux/cpu.h>
46 #include <linux/pci.h>
47 #include <linux/smp.h>
48
49 #include <asm/processor.h>
50 #include <asm/e820.h>
51 #include <asm/mtrr.h>
52 #include <asm/msr.h>
53
54 #include "mtrr.h"
55
56 u32 num_var_ranges;
57
58 unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
59 static DEFINE_MUTEX(mtrr_mutex);
60
61 u64 size_or_mask, size_and_mask;
62 static bool mtrr_aps_delayed_init;
63
64 static const struct mtrr_ops *mtrr_ops[X86_VENDOR_NUM];
65
66 const struct mtrr_ops *mtrr_if;
67
68 static void set_mtrr(unsigned int reg, unsigned long base,
69                      unsigned long size, mtrr_type type);
70
71 void set_mtrr_ops(const struct mtrr_ops *ops)
72 {
73         if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
74                 mtrr_ops[ops->vendor] = ops;
75 }
76
77 /*  Returns non-zero if we have the write-combining memory type  */
78 static int have_wrcomb(void)
79 {
80         struct pci_dev *dev;
81         u8 rev;
82
83         dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL);
84         if (dev != NULL) {
85                 /*
86                  * ServerWorks LE chipsets < rev 6 have problems with
87                  * write-combining. Don't allow it and leave room for other
88                  * chipsets to be tagged
89                  */
90                 if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
91                     dev->device == PCI_DEVICE_ID_SERVERWORKS_LE) {
92                         pci_read_config_byte(dev, PCI_CLASS_REVISION, &rev);
93                         if (rev <= 5) {
94                                 pr_info("mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
95                                 pci_dev_put(dev);
96                                 return 0;
97                         }
98                 }
99                 /*
100                  * Intel 450NX errata # 23. Non ascending cacheline evictions to
101                  * write combining memory may resulting in data corruption
102                  */
103                 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
104                     dev->device == PCI_DEVICE_ID_INTEL_82451NX) {
105                         pr_info("mtrr: Intel 450NX MMC detected. Write-combining disabled.\n");
106                         pci_dev_put(dev);
107                         return 0;
108                 }
109                 pci_dev_put(dev);
110         }
111         return mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0;
112 }
113
114 /*  This function returns the number of variable MTRRs  */
115 static void __init set_num_var_ranges(void)
116 {
117         unsigned long config = 0, dummy;
118
119         if (use_intel())
120                 rdmsr(MSR_MTRRcap, config, dummy);
121         else if (is_cpu(AMD))
122                 config = 2;
123         else if (is_cpu(CYRIX) || is_cpu(CENTAUR))
124                 config = 8;
125
126         num_var_ranges = config & 0xff;
127 }
128
129 static void __init init_table(void)
130 {
131         int i, max;
132
133         max = num_var_ranges;
134         for (i = 0; i < max; i++)
135                 mtrr_usage_table[i] = 1;
136 }
137
138 struct set_mtrr_data {
139         atomic_t        count;
140         atomic_t        gate;
141         unsigned long   smp_base;
142         unsigned long   smp_size;
143         unsigned int    smp_reg;
144         mtrr_type       smp_type;
145 };
146
147 static DEFINE_PER_CPU(struct cpu_stop_work, mtrr_work);
148
149 /**
150  * mtrr_work_handler - Synchronisation handler. Executed by "other" CPUs.
151  * @info: pointer to mtrr configuration data
152  *
153  * Returns nothing.
154  */
155 static int mtrr_work_handler(void *info)
156 {
157 #ifdef CONFIG_SMP
158         struct set_mtrr_data *data = info;
159         unsigned long flags;
160
161         atomic_dec(&data->count);
162         while (!atomic_read(&data->gate))
163                 cpu_relax();
164
165         local_irq_save(flags);
166
167         atomic_dec(&data->count);
168         while (atomic_read(&data->gate))
169                 cpu_relax();
170
171         /*  The master has cleared me to execute  */
172         if (data->smp_reg != ~0U) {
173                 mtrr_if->set(data->smp_reg, data->smp_base,
174                              data->smp_size, data->smp_type);
175         } else if (mtrr_aps_delayed_init) {
176                 /*
177                  * Initialize the MTRRs inaddition to the synchronisation.
178                  */
179                 mtrr_if->set_all();
180         }
181
182         atomic_dec(&data->count);
183         while (!atomic_read(&data->gate))
184                 cpu_relax();
185
186         atomic_dec(&data->count);
187         local_irq_restore(flags);
188 #endif
189         return 0;
190 }
191
192 static inline int types_compatible(mtrr_type type1, mtrr_type type2)
193 {
194         return type1 == MTRR_TYPE_UNCACHABLE ||
195                type2 == MTRR_TYPE_UNCACHABLE ||
196                (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
197                (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
198 }
199
200 /**
201  * set_mtrr - update mtrrs on all processors
202  * @reg:        mtrr in question
203  * @base:       mtrr base
204  * @size:       mtrr size
205  * @type:       mtrr type
206  *
207  * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
208  *
209  * 1. Queue work to do the following on all processors:
210  * 2. Disable Interrupts
211  * 3. Wait for all procs to do so
212  * 4. Enter no-fill cache mode
213  * 5. Flush caches
214  * 6. Clear PGE bit
215  * 7. Flush all TLBs
216  * 8. Disable all range registers
217  * 9. Update the MTRRs
218  * 10. Enable all range registers
219  * 11. Flush all TLBs and caches again
220  * 12. Enter normal cache mode and reenable caching
221  * 13. Set PGE
222  * 14. Wait for buddies to catch up
223  * 15. Enable interrupts.
224  *
225  * What does that mean for us? Well, first we set data.count to the number
226  * of CPUs. As each CPU announces that it started the rendezvous handler by
227  * decrementing the count, We reset data.count and set the data.gate flag
228  * allowing all the cpu's to proceed with the work. As each cpu disables
229  * interrupts, it'll decrement data.count once. We wait until it hits 0 and
230  * proceed. We clear the data.gate flag and reset data.count. Meanwhile, they
231  * are waiting for that flag to be cleared. Once it's cleared, each
232  * CPU goes through the transition of updating MTRRs.
233  * The CPU vendors may each do it differently,
234  * so we call mtrr_if->set() callback and let them take care of it.
235  * When they're done, they again decrement data->count and wait for data.gate
236  * to be set.
237  * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag
238  * Everyone then enables interrupts and we all continue on.
239  *
240  * Note that the mechanism is the same for UP systems, too; all the SMP stuff
241  * becomes nops.
242  */
243 static void
244 set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type type)
245 {
246         struct set_mtrr_data data;
247         unsigned long flags;
248         int cpu;
249
250         preempt_disable();
251
252         data.smp_reg = reg;
253         data.smp_base = base;
254         data.smp_size = size;
255         data.smp_type = type;
256         atomic_set(&data.count, num_booting_cpus() - 1);
257
258         /* Make sure data.count is visible before unleashing other CPUs */
259         smp_wmb();
260         atomic_set(&data.gate, 0);
261
262         /* Start the ball rolling on other CPUs */
263         for_each_online_cpu(cpu) {
264                 struct cpu_stop_work *work = &per_cpu(mtrr_work, cpu);
265
266                 if (cpu == smp_processor_id())
267                         continue;
268
269                 stop_one_cpu_nowait(cpu, mtrr_work_handler, &data, work);
270         }
271
272
273         while (atomic_read(&data.count))
274                 cpu_relax();
275
276         /* Ok, reset count and toggle gate */
277         atomic_set(&data.count, num_booting_cpus() - 1);
278         smp_wmb();
279         atomic_set(&data.gate, 1);
280
281         local_irq_save(flags);
282
283         while (atomic_read(&data.count))
284                 cpu_relax();
285
286         /* Ok, reset count and toggle gate */
287         atomic_set(&data.count, num_booting_cpus() - 1);
288         smp_wmb();
289         atomic_set(&data.gate, 0);
290
291         /* Do our MTRR business */
292
293         /*
294          * HACK!
295          *
296          * We use this same function to initialize the mtrrs during boot,
297          * resume, runtime cpu online and on an explicit request to set a
298          * specific MTRR.
299          *
300          * During boot or suspend, the state of the boot cpu's mtrrs has been
301          * saved, and we want to replicate that across all the cpus that come
302          * online (either at the end of boot or resume or during a runtime cpu
303          * online). If we're doing that, @reg is set to something special and on
304          * this cpu we still do mtrr_if->set_all(). During boot/resume, this
305          * is unnecessary if at this point we are still on the cpu that started
306          * the boot/resume sequence. But there is no guarantee that we are still
307          * on the same cpu. So we do mtrr_if->set_all() on this cpu aswell to be
308          * sure that we are in sync with everyone else.
309          */
310         if (reg != ~0U)
311                 mtrr_if->set(reg, base, size, type);
312         else
313                 mtrr_if->set_all();
314
315         /* Wait for the others */
316         while (atomic_read(&data.count))
317                 cpu_relax();
318
319         atomic_set(&data.count, num_booting_cpus() - 1);
320         smp_wmb();
321         atomic_set(&data.gate, 1);
322
323         /*
324          * Wait here for everyone to have seen the gate change
325          * So we're the last ones to touch 'data'
326          */
327         while (atomic_read(&data.count))
328                 cpu_relax();
329
330         local_irq_restore(flags);
331         preempt_enable();
332 }
333
334 /**
335  * mtrr_add_page - Add a memory type region
336  * @base: Physical base address of region in pages (in units of 4 kB!)
337  * @size: Physical size of region in pages (4 kB)
338  * @type: Type of MTRR desired
339  * @increment: If this is true do usage counting on the region
340  *
341  * Memory type region registers control the caching on newer Intel and
342  * non Intel processors. This function allows drivers to request an
343  * MTRR is added. The details and hardware specifics of each processor's
344  * implementation are hidden from the caller, but nevertheless the
345  * caller should expect to need to provide a power of two size on an
346  * equivalent power of two boundary.
347  *
348  * If the region cannot be added either because all regions are in use
349  * or the CPU cannot support it a negative value is returned. On success
350  * the register number for this entry is returned, but should be treated
351  * as a cookie only.
352  *
353  * On a multiprocessor machine the changes are made to all processors.
354  * This is required on x86 by the Intel processors.
355  *
356  * The available types are
357  *
358  * %MTRR_TYPE_UNCACHABLE - No caching
359  *
360  * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
361  *
362  * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
363  *
364  * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
365  *
366  * BUGS: Needs a quiet flag for the cases where drivers do not mind
367  * failures and do not wish system log messages to be sent.
368  */
369 int mtrr_add_page(unsigned long base, unsigned long size,
370                   unsigned int type, bool increment)
371 {
372         unsigned long lbase, lsize;
373         int i, replace, error;
374         mtrr_type ltype;
375
376         if (!mtrr_if)
377                 return -ENXIO;
378
379         error = mtrr_if->validate_add_page(base, size, type);
380         if (error)
381                 return error;
382
383         if (type >= MTRR_NUM_TYPES) {
384                 pr_warning("mtrr: type: %u invalid\n", type);
385                 return -EINVAL;
386         }
387
388         /* If the type is WC, check that this processor supports it */
389         if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
390                 pr_warning("mtrr: your processor doesn't support write-combining\n");
391                 return -ENOSYS;
392         }
393
394         if (!size) {
395                 pr_warning("mtrr: zero sized request\n");
396                 return -EINVAL;
397         }
398
399         if (base & size_or_mask || size & size_or_mask) {
400                 pr_warning("mtrr: base or size exceeds the MTRR width\n");
401                 return -EINVAL;
402         }
403
404         error = -EINVAL;
405         replace = -1;
406
407         /* No CPU hotplug when we change MTRR entries */
408         get_online_cpus();
409
410         /* Search for existing MTRR  */
411         mutex_lock(&mtrr_mutex);
412         for (i = 0; i < num_var_ranges; ++i) {
413                 mtrr_if->get(i, &lbase, &lsize, &ltype);
414                 if (!lsize || base > lbase + lsize - 1 ||
415                     base + size - 1 < lbase)
416                         continue;
417                 /*
418                  * At this point we know there is some kind of
419                  * overlap/enclosure
420                  */
421                 if (base < lbase || base + size - 1 > lbase + lsize - 1) {
422                         if (base <= lbase &&
423                             base + size - 1 >= lbase + lsize - 1) {
424                                 /*  New region encloses an existing region  */
425                                 if (type == ltype) {
426                                         replace = replace == -1 ? i : -2;
427                                         continue;
428                                 } else if (types_compatible(type, ltype))
429                                         continue;
430                         }
431                         pr_warning("mtrr: 0x%lx000,0x%lx000 overlaps existing"
432                                 " 0x%lx000,0x%lx000\n", base, size, lbase,
433                                 lsize);
434                         goto out;
435                 }
436                 /* New region is enclosed by an existing region */
437                 if (ltype != type) {
438                         if (types_compatible(type, ltype))
439                                 continue;
440                         pr_warning("mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
441                                 base, size, mtrr_attrib_to_str(ltype),
442                                 mtrr_attrib_to_str(type));
443                         goto out;
444                 }
445                 if (increment)
446                         ++mtrr_usage_table[i];
447                 error = i;
448                 goto out;
449         }
450         /* Search for an empty MTRR */
451         i = mtrr_if->get_free_region(base, size, replace);
452         if (i >= 0) {
453                 set_mtrr(i, base, size, type);
454                 if (likely(replace < 0)) {
455                         mtrr_usage_table[i] = 1;
456                 } else {
457                         mtrr_usage_table[i] = mtrr_usage_table[replace];
458                         if (increment)
459                                 mtrr_usage_table[i]++;
460                         if (unlikely(replace != i)) {
461                                 set_mtrr(replace, 0, 0, 0);
462                                 mtrr_usage_table[replace] = 0;
463                         }
464                 }
465         } else {
466                 pr_info("mtrr: no more MTRRs available\n");
467         }
468         error = i;
469  out:
470         mutex_unlock(&mtrr_mutex);
471         put_online_cpus();
472         return error;
473 }
474
475 static int mtrr_check(unsigned long base, unsigned long size)
476 {
477         if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
478                 pr_warning("mtrr: size and base must be multiples of 4 kiB\n");
479                 pr_debug("mtrr: size: 0x%lx  base: 0x%lx\n", size, base);
480                 dump_stack();
481                 return -1;
482         }
483         return 0;
484 }
485
486 /**
487  * mtrr_add - Add a memory type region
488  * @base: Physical base address of region
489  * @size: Physical size of region
490  * @type: Type of MTRR desired
491  * @increment: If this is true do usage counting on the region
492  *
493  * Memory type region registers control the caching on newer Intel and
494  * non Intel processors. This function allows drivers to request an
495  * MTRR is added. The details and hardware specifics of each processor's
496  * implementation are hidden from the caller, but nevertheless the
497  * caller should expect to need to provide a power of two size on an
498  * equivalent power of two boundary.
499  *
500  * If the region cannot be added either because all regions are in use
501  * or the CPU cannot support it a negative value is returned. On success
502  * the register number for this entry is returned, but should be treated
503  * as a cookie only.
504  *
505  * On a multiprocessor machine the changes are made to all processors.
506  * This is required on x86 by the Intel processors.
507  *
508  * The available types are
509  *
510  * %MTRR_TYPE_UNCACHABLE - No caching
511  *
512  * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
513  *
514  * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
515  *
516  * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
517  *
518  * BUGS: Needs a quiet flag for the cases where drivers do not mind
519  * failures and do not wish system log messages to be sent.
520  */
521 int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
522              bool increment)
523 {
524         if (mtrr_check(base, size))
525                 return -EINVAL;
526         return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
527                              increment);
528 }
529 EXPORT_SYMBOL(mtrr_add);
530
531 /**
532  * mtrr_del_page - delete a memory type region
533  * @reg: Register returned by mtrr_add
534  * @base: Physical base address
535  * @size: Size of region
536  *
537  * If register is supplied then base and size are ignored. This is
538  * how drivers should call it.
539  *
540  * Releases an MTRR region. If the usage count drops to zero the
541  * register is freed and the region returns to default state.
542  * On success the register is returned, on failure a negative error
543  * code.
544  */
545 int mtrr_del_page(int reg, unsigned long base, unsigned long size)
546 {
547         int i, max;
548         mtrr_type ltype;
549         unsigned long lbase, lsize;
550         int error = -EINVAL;
551
552         if (!mtrr_if)
553                 return -ENXIO;
554
555         max = num_var_ranges;
556         /* No CPU hotplug when we change MTRR entries */
557         get_online_cpus();
558         mutex_lock(&mtrr_mutex);
559         if (reg < 0) {
560                 /*  Search for existing MTRR  */
561                 for (i = 0; i < max; ++i) {
562                         mtrr_if->get(i, &lbase, &lsize, &ltype);
563                         if (lbase == base && lsize == size) {
564                                 reg = i;
565                                 break;
566                         }
567                 }
568                 if (reg < 0) {
569                         pr_debug("mtrr: no MTRR for %lx000,%lx000 found\n",
570                                  base, size);
571                         goto out;
572                 }
573         }
574         if (reg >= max) {
575                 pr_warning("mtrr: register: %d too big\n", reg);
576                 goto out;
577         }
578         mtrr_if->get(reg, &lbase, &lsize, &ltype);
579         if (lsize < 1) {
580                 pr_warning("mtrr: MTRR %d not used\n", reg);
581                 goto out;
582         }
583         if (mtrr_usage_table[reg] < 1) {
584                 pr_warning("mtrr: reg: %d has count=0\n", reg);
585                 goto out;
586         }
587         if (--mtrr_usage_table[reg] < 1)
588                 set_mtrr(reg, 0, 0, 0);
589         error = reg;
590  out:
591         mutex_unlock(&mtrr_mutex);
592         put_online_cpus();
593         return error;
594 }
595
596 /**
597  * mtrr_del - delete a memory type region
598  * @reg: Register returned by mtrr_add
599  * @base: Physical base address
600  * @size: Size of region
601  *
602  * If register is supplied then base and size are ignored. This is
603  * how drivers should call it.
604  *
605  * Releases an MTRR region. If the usage count drops to zero the
606  * register is freed and the region returns to default state.
607  * On success the register is returned, on failure a negative error
608  * code.
609  */
610 int mtrr_del(int reg, unsigned long base, unsigned long size)
611 {
612         if (mtrr_check(base, size))
613                 return -EINVAL;
614         return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
615 }
616 EXPORT_SYMBOL(mtrr_del);
617
618 /*
619  * HACK ALERT!
620  * These should be called implicitly, but we can't yet until all the initcall
621  * stuff is done...
622  */
623 static void __init init_ifs(void)
624 {
625 #ifndef CONFIG_X86_64
626         amd_init_mtrr();
627         cyrix_init_mtrr();
628         centaur_init_mtrr();
629 #endif
630 }
631
632 /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
633  * MTRR driver doesn't require this
634  */
635 struct mtrr_value {
636         mtrr_type       ltype;
637         unsigned long   lbase;
638         unsigned long   lsize;
639 };
640
641 static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
642
643 static int mtrr_save(struct sys_device *sysdev, pm_message_t state)
644 {
645         int i;
646
647         for (i = 0; i < num_var_ranges; i++) {
648                 mtrr_if->get(i, &mtrr_value[i].lbase,
649                                 &mtrr_value[i].lsize,
650                                 &mtrr_value[i].ltype);
651         }
652         return 0;
653 }
654
655 static int mtrr_restore(struct sys_device *sysdev)
656 {
657         int i;
658
659         for (i = 0; i < num_var_ranges; i++) {
660                 if (mtrr_value[i].lsize) {
661                         set_mtrr(i, mtrr_value[i].lbase,
662                                     mtrr_value[i].lsize,
663                                     mtrr_value[i].ltype);
664                 }
665         }
666         return 0;
667 }
668
669
670
671 static struct sysdev_driver mtrr_sysdev_driver = {
672         .suspend        = mtrr_save,
673         .resume         = mtrr_restore,
674 };
675
676 int __initdata changed_by_mtrr_cleanup;
677
678 /**
679  * mtrr_bp_init - initialize mtrrs on the boot CPU
680  *
681  * This needs to be called early; before any of the other CPUs are
682  * initialized (i.e. before smp_init()).
683  *
684  */
685 void __init mtrr_bp_init(void)
686 {
687         u32 phys_addr;
688
689         init_ifs();
690
691         phys_addr = 32;
692
693         if (cpu_has_mtrr) {
694                 mtrr_if = &generic_mtrr_ops;
695                 size_or_mask = 0xff000000;                      /* 36 bits */
696                 size_and_mask = 0x00f00000;
697                 phys_addr = 36;
698
699                 /*
700                  * This is an AMD specific MSR, but we assume(hope?) that
701                  * Intel will implement it to when they extend the address
702                  * bus of the Xeon.
703                  */
704                 if (cpuid_eax(0x80000000) >= 0x80000008) {
705                         phys_addr = cpuid_eax(0x80000008) & 0xff;
706                         /* CPUID workaround for Intel 0F33/0F34 CPU */
707                         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
708                             boot_cpu_data.x86 == 0xF &&
709                             boot_cpu_data.x86_model == 0x3 &&
710                             (boot_cpu_data.x86_mask == 0x3 ||
711                              boot_cpu_data.x86_mask == 0x4))
712                                 phys_addr = 36;
713
714                         size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
715                         size_and_mask = ~size_or_mask & 0xfffff00000ULL;
716                 } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
717                            boot_cpu_data.x86 == 6) {
718                         /*
719                          * VIA C* family have Intel style MTRRs,
720                          * but don't support PAE
721                          */
722                         size_or_mask = 0xfff00000;              /* 32 bits */
723                         size_and_mask = 0;
724                         phys_addr = 32;
725                 }
726         } else {
727                 switch (boot_cpu_data.x86_vendor) {
728                 case X86_VENDOR_AMD:
729                         if (cpu_has_k6_mtrr) {
730                                 /* Pre-Athlon (K6) AMD CPU MTRRs */
731                                 mtrr_if = mtrr_ops[X86_VENDOR_AMD];
732                                 size_or_mask = 0xfff00000;      /* 32 bits */
733                                 size_and_mask = 0;
734                         }
735                         break;
736                 case X86_VENDOR_CENTAUR:
737                         if (cpu_has_centaur_mcr) {
738                                 mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
739                                 size_or_mask = 0xfff00000;      /* 32 bits */
740                                 size_and_mask = 0;
741                         }
742                         break;
743                 case X86_VENDOR_CYRIX:
744                         if (cpu_has_cyrix_arr) {
745                                 mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
746                                 size_or_mask = 0xfff00000;      /* 32 bits */
747                                 size_and_mask = 0;
748                         }
749                         break;
750                 default:
751                         break;
752                 }
753         }
754
755         if (mtrr_if) {
756                 set_num_var_ranges();
757                 init_table();
758                 if (use_intel()) {
759                         get_mtrr_state();
760
761                         if (mtrr_cleanup(phys_addr)) {
762                                 changed_by_mtrr_cleanup = 1;
763                                 mtrr_if->set_all();
764                         }
765                 }
766         }
767 }
768
769 void mtrr_ap_init(void)
770 {
771         if (!use_intel() || mtrr_aps_delayed_init)
772                 return;
773         /*
774          * Ideally we should hold mtrr_mutex here to avoid mtrr entries
775          * changed, but this routine will be called in cpu boot time,
776          * holding the lock breaks it.
777          *
778          * This routine is called in two cases:
779          *
780          *   1. very earily time of software resume, when there absolutely
781          *      isn't mtrr entry changes;
782          *
783          *   2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
784          *      lock to prevent mtrr entry changes
785          */
786         set_mtrr(~0U, 0, 0, 0);
787 }
788
789 /**
790  * Save current fixed-range MTRR state of the BSP
791  */
792 void mtrr_save_state(void)
793 {
794         smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1);
795 }
796
797 void set_mtrr_aps_delayed_init(void)
798 {
799         if (!use_intel())
800                 return;
801
802         mtrr_aps_delayed_init = true;
803 }
804
805 /*
806  * Delayed MTRR initialization for all AP's
807  */
808 void mtrr_aps_init(void)
809 {
810         if (!use_intel())
811                 return;
812
813         /*
814          * Check if someone has requested the delay of AP MTRR initialization,
815          * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
816          * then we are done.
817          */
818         if (!mtrr_aps_delayed_init)
819                 return;
820
821         set_mtrr(~0U, 0, 0, 0);
822         mtrr_aps_delayed_init = false;
823 }
824
825 void mtrr_bp_restore(void)
826 {
827         if (!use_intel())
828                 return;
829
830         mtrr_if->set_all();
831 }
832
833 static int __init mtrr_init_finialize(void)
834 {
835         if (!mtrr_if)
836                 return 0;
837
838         if (use_intel()) {
839                 if (!changed_by_mtrr_cleanup)
840                         mtrr_state_warn();
841                 return 0;
842         }
843
844         /*
845          * The CPU has no MTRR and seems to not support SMP. They have
846          * specific drivers, we use a tricky method to support
847          * suspend/resume for them.
848          *
849          * TBD: is there any system with such CPU which supports
850          * suspend/resume? If no, we should remove the code.
851          */
852         sysdev_driver_register(&cpu_sysdev_class, &mtrr_sysdev_driver);
853
854         return 0;
855 }
856 subsys_initcall(mtrr_init_finialize);