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