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