1 /* Generic MTRR (Memory Type Range Register) driver.
3 Copyright (C) 1997-2000 Richard Gooch
4 Copyright (c) 2002 Patrick Mochel
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.
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.
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.
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.
24 Source: "Pentium Pro Family Developer's Manual, Volume 3:
25 Operating System Writer's Guide" (Intel document number 242692),
28 This was cleaned and made readable by Patrick Mochel <mochel@osdl.org>
30 Source: Intel Architecture Software Developers Manual, Volume 3:
31 System Programming Guide; Section 9.11. (1997 edition - PPro).
36 #include <linux/types.h> /* FIXME: kvm_para.h needs this */
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>
50 #include <asm/processor.h>
58 /* arch_phys_wc_add returns an MTRR register index plus this offset. */
59 #define MTRR_TO_PHYS_WC_OFFSET 1000
63 unsigned int mtrr_usage_table
[MTRR_MAX_VAR_RANGES
];
64 static DEFINE_MUTEX(mtrr_mutex
);
66 u64 size_or_mask
, size_and_mask
;
67 static bool mtrr_aps_delayed_init
;
69 static const struct mtrr_ops
*mtrr_ops
[X86_VENDOR_NUM
];
71 const struct mtrr_ops
*mtrr_if
;
73 static void set_mtrr(unsigned int reg
, unsigned long base
,
74 unsigned long size
, mtrr_type type
);
76 void set_mtrr_ops(const struct mtrr_ops
*ops
)
78 if (ops
->vendor
&& ops
->vendor
< X86_VENDOR_NUM
)
79 mtrr_ops
[ops
->vendor
] = ops
;
82 /* Returns non-zero if we have the write-combining memory type */
83 static int have_wrcomb(void)
87 dev
= pci_get_class(PCI_CLASS_BRIDGE_HOST
<< 8, NULL
);
90 * ServerWorks LE chipsets < rev 6 have problems with
91 * write-combining. Don't allow it and leave room for other
92 * chipsets to be tagged
94 if (dev
->vendor
== PCI_VENDOR_ID_SERVERWORKS
&&
95 dev
->device
== PCI_DEVICE_ID_SERVERWORKS_LE
&&
97 pr_info("mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
102 * Intel 450NX errata # 23. Non ascending cacheline evictions to
103 * write combining memory may resulting in data corruption
105 if (dev
->vendor
== PCI_VENDOR_ID_INTEL
&&
106 dev
->device
== PCI_DEVICE_ID_INTEL_82451NX
) {
107 pr_info("mtrr: Intel 450NX MMC detected. Write-combining disabled.\n");
113 return mtrr_if
->have_wrcomb
? mtrr_if
->have_wrcomb() : 0;
116 /* This function returns the number of variable MTRRs */
117 static void __init
set_num_var_ranges(void)
119 unsigned long config
= 0, dummy
;
122 rdmsr(MSR_MTRRcap
, config
, dummy
);
123 else if (is_cpu(AMD
))
125 else if (is_cpu(CYRIX
) || is_cpu(CENTAUR
))
128 num_var_ranges
= config
& 0xff;
131 static void __init
init_table(void)
135 max
= num_var_ranges
;
136 for (i
= 0; i
< max
; i
++)
137 mtrr_usage_table
[i
] = 1;
140 struct set_mtrr_data
{
141 unsigned long smp_base
;
142 unsigned long smp_size
;
143 unsigned int smp_reg
;
148 * mtrr_rendezvous_handler - Work done in the synchronization handler. Executed
150 * @info: pointer to mtrr configuration data
154 static int mtrr_rendezvous_handler(void *info
)
156 struct set_mtrr_data
*data
= info
;
159 * We use this same function to initialize the mtrrs during boot,
160 * resume, runtime cpu online and on an explicit request to set a
163 * During boot or suspend, the state of the boot cpu's mtrrs has been
164 * saved, and we want to replicate that across all the cpus that come
165 * online (either at the end of boot or resume or during a runtime cpu
166 * online). If we're doing that, @reg is set to something special and on
167 * all the cpu's we do mtrr_if->set_all() (On the logical cpu that
168 * started the boot/resume sequence, this might be a duplicate
171 if (data
->smp_reg
!= ~0U) {
172 mtrr_if
->set(data
->smp_reg
, data
->smp_base
,
173 data
->smp_size
, data
->smp_type
);
174 } else if (mtrr_aps_delayed_init
|| !cpu_online(smp_processor_id())) {
180 static inline int types_compatible(mtrr_type type1
, mtrr_type type2
)
182 return type1
== MTRR_TYPE_UNCACHABLE
||
183 type2
== MTRR_TYPE_UNCACHABLE
||
184 (type1
== MTRR_TYPE_WRTHROUGH
&& type2
== MTRR_TYPE_WRBACK
) ||
185 (type1
== MTRR_TYPE_WRBACK
&& type2
== MTRR_TYPE_WRTHROUGH
);
189 * set_mtrr - update mtrrs on all processors
190 * @reg: mtrr in question
195 * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
197 * 1. Queue work to do the following on all processors:
198 * 2. Disable Interrupts
199 * 3. Wait for all procs to do so
200 * 4. Enter no-fill cache mode
204 * 8. Disable all range registers
205 * 9. Update the MTRRs
206 * 10. Enable all range registers
207 * 11. Flush all TLBs and caches again
208 * 12. Enter normal cache mode and reenable caching
210 * 14. Wait for buddies to catch up
211 * 15. Enable interrupts.
213 * What does that mean for us? Well, stop_machine() will ensure that
214 * the rendezvous handler is started on each CPU. And in lockstep they
215 * do the state transition of disabling interrupts, updating MTRR's
216 * (the CPU vendors may each do it differently, so we call mtrr_if->set()
217 * callback and let them take care of it.) and enabling interrupts.
219 * Note that the mechanism is the same for UP systems, too; all the SMP stuff
223 set_mtrr(unsigned int reg
, unsigned long base
, unsigned long size
, mtrr_type type
)
225 struct set_mtrr_data data
= { .smp_reg
= reg
,
231 stop_machine(mtrr_rendezvous_handler
, &data
, cpu_online_mask
);
234 static void set_mtrr_from_inactive_cpu(unsigned int reg
, unsigned long base
,
235 unsigned long size
, mtrr_type type
)
237 struct set_mtrr_data data
= { .smp_reg
= reg
,
243 stop_machine_from_inactive_cpu(mtrr_rendezvous_handler
, &data
,
248 * mtrr_add_page - Add a memory type region
249 * @base: Physical base address of region in pages (in units of 4 kB!)
250 * @size: Physical size of region in pages (4 kB)
251 * @type: Type of MTRR desired
252 * @increment: If this is true do usage counting on the region
254 * Memory type region registers control the caching on newer Intel and
255 * non Intel processors. This function allows drivers to request an
256 * MTRR is added. The details and hardware specifics of each processor's
257 * implementation are hidden from the caller, but nevertheless the
258 * caller should expect to need to provide a power of two size on an
259 * equivalent power of two boundary.
261 * If the region cannot be added either because all regions are in use
262 * or the CPU cannot support it a negative value is returned. On success
263 * the register number for this entry is returned, but should be treated
266 * On a multiprocessor machine the changes are made to all processors.
267 * This is required on x86 by the Intel processors.
269 * The available types are
271 * %MTRR_TYPE_UNCACHABLE - No caching
273 * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
275 * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
277 * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
279 * BUGS: Needs a quiet flag for the cases where drivers do not mind
280 * failures and do not wish system log messages to be sent.
282 int mtrr_add_page(unsigned long base
, unsigned long size
,
283 unsigned int type
, bool increment
)
285 unsigned long lbase
, lsize
;
286 int i
, replace
, error
;
292 error
= mtrr_if
->validate_add_page(base
, size
, type
);
296 if (type
>= MTRR_NUM_TYPES
) {
297 pr_warning("mtrr: type: %u invalid\n", type
);
301 /* If the type is WC, check that this processor supports it */
302 if ((type
== MTRR_TYPE_WRCOMB
) && !have_wrcomb()) {
303 pr_warning("mtrr: your processor doesn't support write-combining\n");
308 pr_warning("mtrr: zero sized request\n");
312 if ((base
| (base
+ size
- 1)) >>
313 (boot_cpu_data
.x86_phys_bits
- PAGE_SHIFT
)) {
314 pr_warning("mtrr: base or size exceeds the MTRR width\n");
321 /* No CPU hotplug when we change MTRR entries */
324 /* Search for existing MTRR */
325 mutex_lock(&mtrr_mutex
);
326 for (i
= 0; i
< num_var_ranges
; ++i
) {
327 mtrr_if
->get(i
, &lbase
, &lsize
, <ype
);
328 if (!lsize
|| base
> lbase
+ lsize
- 1 ||
329 base
+ size
- 1 < lbase
)
332 * At this point we know there is some kind of
335 if (base
< lbase
|| base
+ size
- 1 > lbase
+ lsize
- 1) {
337 base
+ size
- 1 >= lbase
+ lsize
- 1) {
338 /* New region encloses an existing region */
340 replace
= replace
== -1 ? i
: -2;
342 } else if (types_compatible(type
, ltype
))
345 pr_warning("mtrr: 0x%lx000,0x%lx000 overlaps existing"
346 " 0x%lx000,0x%lx000\n", base
, size
, lbase
,
350 /* New region is enclosed by an existing region */
352 if (types_compatible(type
, ltype
))
354 pr_warning("mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
355 base
, size
, mtrr_attrib_to_str(ltype
),
356 mtrr_attrib_to_str(type
));
360 ++mtrr_usage_table
[i
];
364 /* Search for an empty MTRR */
365 i
= mtrr_if
->get_free_region(base
, size
, replace
);
367 set_mtrr(i
, base
, size
, type
);
368 if (likely(replace
< 0)) {
369 mtrr_usage_table
[i
] = 1;
371 mtrr_usage_table
[i
] = mtrr_usage_table
[replace
];
373 mtrr_usage_table
[i
]++;
374 if (unlikely(replace
!= i
)) {
375 set_mtrr(replace
, 0, 0, 0);
376 mtrr_usage_table
[replace
] = 0;
380 pr_info("mtrr: no more MTRRs available\n");
384 mutex_unlock(&mtrr_mutex
);
389 static int mtrr_check(unsigned long base
, unsigned long size
)
391 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1))) {
392 pr_warning("mtrr: size and base must be multiples of 4 kiB\n");
393 pr_debug("mtrr: size: 0x%lx base: 0x%lx\n", size
, base
);
401 * mtrr_add - Add a memory type region
402 * @base: Physical base address of region
403 * @size: Physical size of region
404 * @type: Type of MTRR desired
405 * @increment: If this is true do usage counting on the region
407 * Memory type region registers control the caching on newer Intel and
408 * non Intel processors. This function allows drivers to request an
409 * MTRR is added. The details and hardware specifics of each processor's
410 * implementation are hidden from the caller, but nevertheless the
411 * caller should expect to need to provide a power of two size on an
412 * equivalent power of two boundary.
414 * If the region cannot be added either because all regions are in use
415 * or the CPU cannot support it a negative value is returned. On success
416 * the register number for this entry is returned, but should be treated
419 * On a multiprocessor machine the changes are made to all processors.
420 * This is required on x86 by the Intel processors.
422 * The available types are
424 * %MTRR_TYPE_UNCACHABLE - No caching
426 * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
428 * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
430 * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
432 * BUGS: Needs a quiet flag for the cases where drivers do not mind
433 * failures and do not wish system log messages to be sent.
435 int mtrr_add(unsigned long base
, unsigned long size
, unsigned int type
,
438 if (mtrr_check(base
, size
))
440 return mtrr_add_page(base
>> PAGE_SHIFT
, size
>> PAGE_SHIFT
, type
,
443 EXPORT_SYMBOL(mtrr_add
);
446 * mtrr_del_page - delete a memory type region
447 * @reg: Register returned by mtrr_add
448 * @base: Physical base address
449 * @size: Size of region
451 * If register is supplied then base and size are ignored. This is
452 * how drivers should call it.
454 * Releases an MTRR region. If the usage count drops to zero the
455 * register is freed and the region returns to default state.
456 * On success the register is returned, on failure a negative error
459 int mtrr_del_page(int reg
, unsigned long base
, unsigned long size
)
463 unsigned long lbase
, lsize
;
469 max
= num_var_ranges
;
470 /* No CPU hotplug when we change MTRR entries */
472 mutex_lock(&mtrr_mutex
);
474 /* Search for existing MTRR */
475 for (i
= 0; i
< max
; ++i
) {
476 mtrr_if
->get(i
, &lbase
, &lsize
, <ype
);
477 if (lbase
== base
&& lsize
== size
) {
483 pr_debug("mtrr: no MTRR for %lx000,%lx000 found\n",
489 pr_warning("mtrr: register: %d too big\n", reg
);
492 mtrr_if
->get(reg
, &lbase
, &lsize
, <ype
);
494 pr_warning("mtrr: MTRR %d not used\n", reg
);
497 if (mtrr_usage_table
[reg
] < 1) {
498 pr_warning("mtrr: reg: %d has count=0\n", reg
);
501 if (--mtrr_usage_table
[reg
] < 1)
502 set_mtrr(reg
, 0, 0, 0);
505 mutex_unlock(&mtrr_mutex
);
511 * mtrr_del - delete a memory type region
512 * @reg: Register returned by mtrr_add
513 * @base: Physical base address
514 * @size: Size of region
516 * If register is supplied then base and size are ignored. This is
517 * how drivers should call it.
519 * Releases an MTRR region. If the usage count drops to zero the
520 * register is freed and the region returns to default state.
521 * On success the register is returned, on failure a negative error
524 int mtrr_del(int reg
, unsigned long base
, unsigned long size
)
526 if (mtrr_check(base
, size
))
528 return mtrr_del_page(reg
, base
>> PAGE_SHIFT
, size
>> PAGE_SHIFT
);
530 EXPORT_SYMBOL(mtrr_del
);
533 * arch_phys_wc_add - add a WC MTRR and handle errors if PAT is unavailable
534 * @base: Physical base address
535 * @size: Size of region
537 * If PAT is available, this does nothing. If PAT is unavailable, it
538 * attempts to add a WC MTRR covering size bytes starting at base and
539 * logs an error if this fails.
541 * Drivers must store the return value to pass to mtrr_del_wc_if_needed,
542 * but drivers should not try to interpret that return value.
544 int arch_phys_wc_add(unsigned long base
, unsigned long size
)
549 return 0; /* Success! (We don't need to do anything.) */
551 ret
= mtrr_add(base
, size
, MTRR_TYPE_WRCOMB
, true);
553 pr_warn("Failed to add WC MTRR for [%p-%p]; performance may suffer.",
554 (void *)base
, (void *)(base
+ size
- 1));
557 return ret
+ MTRR_TO_PHYS_WC_OFFSET
;
559 EXPORT_SYMBOL(arch_phys_wc_add
);
562 * arch_phys_wc_del - undoes arch_phys_wc_add
563 * @handle: Return value from arch_phys_wc_add
565 * This cleans up after mtrr_add_wc_if_needed.
567 * The API guarantees that mtrr_del_wc_if_needed(error code) and
568 * mtrr_del_wc_if_needed(0) do nothing.
570 void arch_phys_wc_del(int handle
)
573 WARN_ON(handle
< MTRR_TO_PHYS_WC_OFFSET
);
574 mtrr_del(handle
- MTRR_TO_PHYS_WC_OFFSET
, 0, 0);
577 EXPORT_SYMBOL(arch_phys_wc_del
);
580 * phys_wc_to_mtrr_index - translates arch_phys_wc_add's return value
581 * @handle: Return value from arch_phys_wc_add
583 * This will turn the return value from arch_phys_wc_add into an mtrr
584 * index suitable for debugging.
586 * Note: There is no legitimate use for this function, except possibly
587 * in printk line. Alas there is an illegitimate use in some ancient
590 int phys_wc_to_mtrr_index(int handle
)
592 if (handle
< MTRR_TO_PHYS_WC_OFFSET
)
595 return handle
- MTRR_TO_PHYS_WC_OFFSET
;
597 EXPORT_SYMBOL_GPL(phys_wc_to_mtrr_index
);
601 * These should be called implicitly, but we can't yet until all the initcall
604 static void __init
init_ifs(void)
606 #ifndef CONFIG_X86_64
613 /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
614 * MTRR driver doesn't require this
622 static struct mtrr_value mtrr_value
[MTRR_MAX_VAR_RANGES
];
624 static int mtrr_save(void)
628 for (i
= 0; i
< num_var_ranges
; i
++) {
629 mtrr_if
->get(i
, &mtrr_value
[i
].lbase
,
630 &mtrr_value
[i
].lsize
,
631 &mtrr_value
[i
].ltype
);
636 static void mtrr_restore(void)
640 for (i
= 0; i
< num_var_ranges
; i
++) {
641 if (mtrr_value
[i
].lsize
) {
642 set_mtrr(i
, mtrr_value
[i
].lbase
,
644 mtrr_value
[i
].ltype
);
651 static struct syscore_ops mtrr_syscore_ops
= {
652 .suspend
= mtrr_save
,
653 .resume
= mtrr_restore
,
656 int __initdata changed_by_mtrr_cleanup
;
658 #define SIZE_OR_MASK_BITS(n) (~((1ULL << ((n) - PAGE_SHIFT)) - 1))
660 * mtrr_bp_init - initialize mtrrs on the boot CPU
662 * This needs to be called early; before any of the other CPUs are
663 * initialized (i.e. before smp_init()).
666 void __init
mtrr_bp_init(void)
675 mtrr_if
= &generic_mtrr_ops
;
676 size_or_mask
= SIZE_OR_MASK_BITS(36);
677 size_and_mask
= 0x00f00000;
681 * This is an AMD specific MSR, but we assume(hope?) that
682 * Intel will implement it too when they extend the address
685 if (cpuid_eax(0x80000000) >= 0x80000008) {
686 phys_addr
= cpuid_eax(0x80000008) & 0xff;
687 /* CPUID workaround for Intel 0F33/0F34 CPU */
688 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
&&
689 boot_cpu_data
.x86
== 0xF &&
690 boot_cpu_data
.x86_model
== 0x3 &&
691 (boot_cpu_data
.x86_mask
== 0x3 ||
692 boot_cpu_data
.x86_mask
== 0x4))
695 size_or_mask
= SIZE_OR_MASK_BITS(phys_addr
);
696 size_and_mask
= ~size_or_mask
& 0xfffff00000ULL
;
697 } else if (boot_cpu_data
.x86_vendor
== X86_VENDOR_CENTAUR
&&
698 boot_cpu_data
.x86
== 6) {
700 * VIA C* family have Intel style MTRRs,
701 * but don't support PAE
703 size_or_mask
= SIZE_OR_MASK_BITS(32);
708 switch (boot_cpu_data
.x86_vendor
) {
710 if (cpu_feature_enabled(X86_FEATURE_K6_MTRR
)) {
711 /* Pre-Athlon (K6) AMD CPU MTRRs */
712 mtrr_if
= mtrr_ops
[X86_VENDOR_AMD
];
713 size_or_mask
= SIZE_OR_MASK_BITS(32);
717 case X86_VENDOR_CENTAUR
:
718 if (cpu_feature_enabled(X86_FEATURE_CENTAUR_MCR
)) {
719 mtrr_if
= mtrr_ops
[X86_VENDOR_CENTAUR
];
720 size_or_mask
= SIZE_OR_MASK_BITS(32);
724 case X86_VENDOR_CYRIX
:
725 if (cpu_feature_enabled(X86_FEATURE_CYRIX_ARR
)) {
726 mtrr_if
= mtrr_ops
[X86_VENDOR_CYRIX
];
727 size_or_mask
= SIZE_OR_MASK_BITS(32);
737 set_num_var_ranges();
742 if (mtrr_cleanup(phys_addr
)) {
743 changed_by_mtrr_cleanup
= 1;
750 void mtrr_ap_init(void)
752 if (!use_intel() || mtrr_aps_delayed_init
)
755 * Ideally we should hold mtrr_mutex here to avoid mtrr entries
756 * changed, but this routine will be called in cpu boot time,
757 * holding the lock breaks it.
759 * This routine is called in two cases:
761 * 1. very earily time of software resume, when there absolutely
762 * isn't mtrr entry changes;
764 * 2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
765 * lock to prevent mtrr entry changes
767 set_mtrr_from_inactive_cpu(~0U, 0, 0, 0);
771 * Save current fixed-range MTRR state of the first cpu in cpu_online_mask.
773 void mtrr_save_state(void)
778 first_cpu
= cpumask_first(cpu_online_mask
);
779 smp_call_function_single(first_cpu
, mtrr_save_fixed_ranges
, NULL
, 1);
783 void set_mtrr_aps_delayed_init(void)
788 mtrr_aps_delayed_init
= true;
792 * Delayed MTRR initialization for all AP's
794 void mtrr_aps_init(void)
800 * Check if someone has requested the delay of AP MTRR initialization,
801 * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
804 if (!mtrr_aps_delayed_init
)
807 set_mtrr(~0U, 0, 0, 0);
808 mtrr_aps_delayed_init
= false;
811 void mtrr_bp_restore(void)
819 static int __init
mtrr_init_finialize(void)
825 if (!changed_by_mtrr_cleanup
)
831 * The CPU has no MTRR and seems to not support SMP. They have
832 * specific drivers, we use a tricky method to support
833 * suspend/resume for them.
835 * TBD: is there any system with such CPU which supports
836 * suspend/resume? If no, we should remove the code.
838 register_syscore_ops(&mtrr_syscore_ops
);
842 subsys_initcall(mtrr_init_finialize
);