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