[PATCH] i386: fix buggy MTRR address checks
[linux-2.6/mini2440.git] / arch / i386 / kernel / cpu / mtrr / main.c
blob2b8b0b361ccb410564aae2c5a9340a708abf2da7
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),
26 section 11.11.7
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).
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>
41 #include <asm/mtrr.h>
43 #include <asm/uaccess.h>
44 #include <asm/processor.h>
45 #include <asm/msr.h>
46 #include "mtrr.h"
48 u32 num_var_ranges = 0;
50 unsigned int *usage_table;
51 static DEFINE_MUTEX(mtrr_mutex);
53 u32 size_or_mask, size_and_mask;
55 static struct mtrr_ops * mtrr_ops[X86_VENDOR_NUM] = {};
57 struct mtrr_ops * mtrr_if = NULL;
59 static void set_mtrr(unsigned int reg, unsigned long base,
60 unsigned long size, mtrr_type type);
62 extern int arr3_protected;
64 void set_mtrr_ops(struct mtrr_ops * ops)
66 if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
67 mtrr_ops[ops->vendor] = ops;
70 /* Returns non-zero if we have the write-combining memory type */
71 static int have_wrcomb(void)
73 struct pci_dev *dev;
74 u8 rev;
76 if ((dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) != NULL) {
77 /* ServerWorks LE chipsets < rev 6 have problems with write-combining
78 Don't allow it and leave room for other chipsets to be tagged */
79 if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
80 dev->device == PCI_DEVICE_ID_SERVERWORKS_LE) {
81 pci_read_config_byte(dev, PCI_CLASS_REVISION, &rev);
82 if (rev <= 5) {
83 printk(KERN_INFO "mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
84 pci_dev_put(dev);
85 return 0;
88 /* Intel 450NX errata # 23. Non ascending cacheline evictions to
89 write combining memory may resulting in data corruption */
90 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
91 dev->device == PCI_DEVICE_ID_INTEL_82451NX) {
92 printk(KERN_INFO "mtrr: Intel 450NX MMC detected. Write-combining disabled.\n");
93 pci_dev_put(dev);
94 return 0;
96 pci_dev_put(dev);
98 return (mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0);
101 /* This function returns the number of variable MTRRs */
102 static void __init set_num_var_ranges(void)
104 unsigned long config = 0, dummy;
106 if (use_intel()) {
107 rdmsr(MTRRcap_MSR, config, dummy);
108 } else if (is_cpu(AMD))
109 config = 2;
110 else if (is_cpu(CYRIX) || is_cpu(CENTAUR))
111 config = 8;
112 num_var_ranges = config & 0xff;
115 static void __init init_table(void)
117 int i, max;
119 max = num_var_ranges;
120 if ((usage_table = kmalloc(max * sizeof *usage_table, GFP_KERNEL))
121 == NULL) {
122 printk(KERN_ERR "mtrr: could not allocate\n");
123 return;
125 for (i = 0; i < max; i++)
126 usage_table[i] = 1;
129 struct set_mtrr_data {
130 atomic_t count;
131 atomic_t gate;
132 unsigned long smp_base;
133 unsigned long smp_size;
134 unsigned int smp_reg;
135 mtrr_type smp_type;
138 #ifdef CONFIG_SMP
140 static void ipi_handler(void *info)
141 /* [SUMMARY] Synchronisation handler. Executed by "other" CPUs.
142 [RETURNS] Nothing.
145 struct set_mtrr_data *data = info;
146 unsigned long flags;
148 local_irq_save(flags);
150 atomic_dec(&data->count);
151 while(!atomic_read(&data->gate))
152 cpu_relax();
154 /* The master has cleared me to execute */
155 if (data->smp_reg != ~0U)
156 mtrr_if->set(data->smp_reg, data->smp_base,
157 data->smp_size, data->smp_type);
158 else
159 mtrr_if->set_all();
161 atomic_dec(&data->count);
162 while(atomic_read(&data->gate))
163 cpu_relax();
165 atomic_dec(&data->count);
166 local_irq_restore(flags);
169 #endif
172 * set_mtrr - update mtrrs on all processors
173 * @reg: mtrr in question
174 * @base: mtrr base
175 * @size: mtrr size
176 * @type: mtrr type
178 * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
180 * 1. Send IPI to do the following:
181 * 2. Disable Interrupts
182 * 3. Wait for all procs to do so
183 * 4. Enter no-fill cache mode
184 * 5. Flush caches
185 * 6. Clear PGE bit
186 * 7. Flush all TLBs
187 * 8. Disable all range registers
188 * 9. Update the MTRRs
189 * 10. Enable all range registers
190 * 11. Flush all TLBs and caches again
191 * 12. Enter normal cache mode and reenable caching
192 * 13. Set PGE
193 * 14. Wait for buddies to catch up
194 * 15. Enable interrupts.
196 * What does that mean for us? Well, first we set data.count to the number
197 * of CPUs. As each CPU disables interrupts, it'll decrement it once. We wait
198 * until it hits 0 and proceed. We set the data.gate flag and reset data.count.
199 * Meanwhile, they are waiting for that flag to be set. Once it's set, each
200 * CPU goes through the transition of updating MTRRs. The CPU vendors may each do it
201 * differently, so we call mtrr_if->set() callback and let them take care of it.
202 * When they're done, they again decrement data->count and wait for data.gate to
203 * be reset.
204 * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag.
205 * Everyone then enables interrupts and we all continue on.
207 * Note that the mechanism is the same for UP systems, too; all the SMP stuff
208 * becomes nops.
210 static void set_mtrr(unsigned int reg, unsigned long base,
211 unsigned long size, mtrr_type type)
213 struct set_mtrr_data data;
214 unsigned long flags;
216 data.smp_reg = reg;
217 data.smp_base = base;
218 data.smp_size = size;
219 data.smp_type = type;
220 atomic_set(&data.count, num_booting_cpus() - 1);
221 atomic_set(&data.gate,0);
223 /* Start the ball rolling on other CPUs */
224 if (smp_call_function(ipi_handler, &data, 1, 0) != 0)
225 panic("mtrr: timed out waiting for other CPUs\n");
227 local_irq_save(flags);
229 while(atomic_read(&data.count))
230 cpu_relax();
232 /* ok, reset count and toggle gate */
233 atomic_set(&data.count, num_booting_cpus() - 1);
234 atomic_set(&data.gate,1);
236 /* do our MTRR business */
238 /* HACK!
239 * We use this same function to initialize the mtrrs on boot.
240 * The state of the boot cpu's mtrrs has been saved, and we want
241 * to replicate across all the APs.
242 * If we're doing that @reg is set to something special...
244 if (reg != ~0U)
245 mtrr_if->set(reg,base,size,type);
247 /* wait for the others */
248 while(atomic_read(&data.count))
249 cpu_relax();
251 atomic_set(&data.count, num_booting_cpus() - 1);
252 atomic_set(&data.gate,0);
255 * Wait here for everyone to have seen the gate change
256 * So we're the last ones to touch 'data'
258 while(atomic_read(&data.count))
259 cpu_relax();
261 local_irq_restore(flags);
265 * mtrr_add_page - Add a memory type region
266 * @base: Physical base address of region in pages (in units of 4 kB!)
267 * @size: Physical size of region in pages (4 kB)
268 * @type: Type of MTRR desired
269 * @increment: If this is true do usage counting on the region
271 * Memory type region registers control the caching on newer Intel and
272 * non Intel processors. This function allows drivers to request an
273 * MTRR is added. The details and hardware specifics of each processor's
274 * implementation are hidden from the caller, but nevertheless the
275 * caller should expect to need to provide a power of two size on an
276 * equivalent power of two boundary.
278 * If the region cannot be added either because all regions are in use
279 * or the CPU cannot support it a negative value is returned. On success
280 * the register number for this entry is returned, but should be treated
281 * as a cookie only.
283 * On a multiprocessor machine the changes are made to all processors.
284 * This is required on x86 by the Intel processors.
286 * The available types are
288 * %MTRR_TYPE_UNCACHABLE - No caching
290 * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
292 * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
294 * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
296 * BUGS: Needs a quiet flag for the cases where drivers do not mind
297 * failures and do not wish system log messages to be sent.
300 int mtrr_add_page(unsigned long base, unsigned long size,
301 unsigned int type, char increment)
303 int i;
304 mtrr_type ltype;
305 unsigned long lbase;
306 unsigned int lsize;
307 int error;
309 if (!mtrr_if)
310 return -ENXIO;
312 if ((error = mtrr_if->validate_add_page(base,size,type)))
313 return error;
315 if (type >= MTRR_NUM_TYPES) {
316 printk(KERN_WARNING "mtrr: type: %u invalid\n", type);
317 return -EINVAL;
320 /* If the type is WC, check that this processor supports it */
321 if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
322 printk(KERN_WARNING
323 "mtrr: your processor doesn't support write-combining\n");
324 return -ENOSYS;
327 if (base & size_or_mask || size & size_or_mask) {
328 printk(KERN_WARNING "mtrr: base or size exceeds the MTRR width\n");
329 return -EINVAL;
332 error = -EINVAL;
334 /* No CPU hotplug when we change MTRR entries */
335 lock_cpu_hotplug();
336 /* Search for existing MTRR */
337 mutex_lock(&mtrr_mutex);
338 for (i = 0; i < num_var_ranges; ++i) {
339 mtrr_if->get(i, &lbase, &lsize, &ltype);
340 if (base >= lbase + lsize)
341 continue;
342 if ((base < lbase) && (base + size <= lbase))
343 continue;
344 /* At this point we know there is some kind of overlap/enclosure */
345 if ((base < lbase) || (base + size > lbase + lsize)) {
346 printk(KERN_WARNING
347 "mtrr: 0x%lx000,0x%lx000 overlaps existing"
348 " 0x%lx000,0x%x000\n", base, size, lbase,
349 lsize);
350 goto out;
352 /* New region is enclosed by an existing region */
353 if (ltype != type) {
354 if (type == MTRR_TYPE_UNCACHABLE)
355 continue;
356 printk (KERN_WARNING "mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
357 base, size, mtrr_attrib_to_str(ltype),
358 mtrr_attrib_to_str(type));
359 goto out;
361 if (increment)
362 ++usage_table[i];
363 error = i;
364 goto out;
366 /* Search for an empty MTRR */
367 i = mtrr_if->get_free_region(base, size);
368 if (i >= 0) {
369 set_mtrr(i, base, size, type);
370 usage_table[i] = 1;
371 } else
372 printk(KERN_INFO "mtrr: no more MTRRs available\n");
373 error = i;
374 out:
375 mutex_unlock(&mtrr_mutex);
376 unlock_cpu_hotplug();
377 return error;
380 static int mtrr_check(unsigned long base, unsigned long size)
382 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
383 printk(KERN_WARNING
384 "mtrr: size and base must be multiples of 4 kiB\n");
385 printk(KERN_DEBUG
386 "mtrr: size: 0x%lx base: 0x%lx\n", size, base);
387 dump_stack();
388 return -1;
390 return 0;
394 * mtrr_add - Add a memory type region
395 * @base: Physical base address of region
396 * @size: Physical size of region
397 * @type: Type of MTRR desired
398 * @increment: If this is true do usage counting on the region
400 * Memory type region registers control the caching on newer Intel and
401 * non Intel processors. This function allows drivers to request an
402 * MTRR is added. The details and hardware specifics of each processor's
403 * implementation are hidden from the caller, but nevertheless the
404 * caller should expect to need to provide a power of two size on an
405 * equivalent power of two boundary.
407 * If the region cannot be added either because all regions are in use
408 * or the CPU cannot support it a negative value is returned. On success
409 * the register number for this entry is returned, but should be treated
410 * as a cookie only.
412 * On a multiprocessor machine the changes are made to all processors.
413 * This is required on x86 by the Intel processors.
415 * The available types are
417 * %MTRR_TYPE_UNCACHABLE - No caching
419 * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
421 * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
423 * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
425 * BUGS: Needs a quiet flag for the cases where drivers do not mind
426 * failures and do not wish system log messages to be sent.
430 mtrr_add(unsigned long base, unsigned long size, unsigned int type,
431 char increment)
433 if (mtrr_check(base, size))
434 return -EINVAL;
435 return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
436 increment);
440 * mtrr_del_page - delete a memory type region
441 * @reg: Register returned by mtrr_add
442 * @base: Physical base address
443 * @size: Size of region
445 * If register is supplied then base and size are ignored. This is
446 * how drivers should call it.
448 * Releases an MTRR region. If the usage count drops to zero the
449 * register is freed and the region returns to default state.
450 * On success the register is returned, on failure a negative error
451 * code.
454 int mtrr_del_page(int reg, unsigned long base, unsigned long size)
456 int i, max;
457 mtrr_type ltype;
458 unsigned long lbase;
459 unsigned int lsize;
460 int error = -EINVAL;
462 if (!mtrr_if)
463 return -ENXIO;
465 max = num_var_ranges;
466 /* No CPU hotplug when we change MTRR entries */
467 lock_cpu_hotplug();
468 mutex_lock(&mtrr_mutex);
469 if (reg < 0) {
470 /* Search for existing MTRR */
471 for (i = 0; i < max; ++i) {
472 mtrr_if->get(i, &lbase, &lsize, &ltype);
473 if (lbase == base && lsize == size) {
474 reg = i;
475 break;
478 if (reg < 0) {
479 printk(KERN_DEBUG "mtrr: no MTRR for %lx000,%lx000 found\n", base,
480 size);
481 goto out;
484 if (reg >= max) {
485 printk(KERN_WARNING "mtrr: register: %d too big\n", reg);
486 goto out;
488 if (is_cpu(CYRIX) && !use_intel()) {
489 if ((reg == 3) && arr3_protected) {
490 printk(KERN_WARNING "mtrr: ARR3 cannot be changed\n");
491 goto out;
494 mtrr_if->get(reg, &lbase, &lsize, &ltype);
495 if (lsize < 1) {
496 printk(KERN_WARNING "mtrr: MTRR %d not used\n", reg);
497 goto out;
499 if (usage_table[reg] < 1) {
500 printk(KERN_WARNING "mtrr: reg: %d has count=0\n", reg);
501 goto out;
503 if (--usage_table[reg] < 1)
504 set_mtrr(reg, 0, 0, 0);
505 error = reg;
506 out:
507 mutex_unlock(&mtrr_mutex);
508 unlock_cpu_hotplug();
509 return error;
512 * mtrr_del - delete a memory type region
513 * @reg: Register returned by mtrr_add
514 * @base: Physical base address
515 * @size: Size of region
517 * If register is supplied then base and size are ignored. This is
518 * how drivers should call it.
520 * Releases an MTRR region. If the usage count drops to zero the
521 * register is freed and the region returns to default state.
522 * On success the register is returned, on failure a negative error
523 * code.
527 mtrr_del(int reg, unsigned long base, unsigned long size)
529 if (mtrr_check(base, size))
530 return -EINVAL;
531 return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
534 EXPORT_SYMBOL(mtrr_add);
535 EXPORT_SYMBOL(mtrr_del);
537 /* HACK ALERT!
538 * These should be called implicitly, but we can't yet until all the initcall
539 * stuff is done...
541 extern void amd_init_mtrr(void);
542 extern void cyrix_init_mtrr(void);
543 extern void centaur_init_mtrr(void);
545 static void __init init_ifs(void)
547 amd_init_mtrr();
548 cyrix_init_mtrr();
549 centaur_init_mtrr();
552 /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
553 * MTRR driver doesn't require this
555 struct mtrr_value {
556 mtrr_type ltype;
557 unsigned long lbase;
558 unsigned int lsize;
561 static struct mtrr_value * mtrr_state;
563 static int mtrr_save(struct sys_device * sysdev, pm_message_t state)
565 int i;
566 int size = num_var_ranges * sizeof(struct mtrr_value);
568 mtrr_state = kmalloc(size,GFP_ATOMIC);
569 if (mtrr_state)
570 memset(mtrr_state,0,size);
571 else
572 return -ENOMEM;
574 for (i = 0; i < num_var_ranges; i++) {
575 mtrr_if->get(i,
576 &mtrr_state[i].lbase,
577 &mtrr_state[i].lsize,
578 &mtrr_state[i].ltype);
580 return 0;
583 static int mtrr_restore(struct sys_device * sysdev)
585 int i;
587 for (i = 0; i < num_var_ranges; i++) {
588 if (mtrr_state[i].lsize)
589 set_mtrr(i,
590 mtrr_state[i].lbase,
591 mtrr_state[i].lsize,
592 mtrr_state[i].ltype);
594 kfree(mtrr_state);
595 return 0;
600 static struct sysdev_driver mtrr_sysdev_driver = {
601 .suspend = mtrr_save,
602 .resume = mtrr_restore,
607 * mtrr_bp_init - initialize mtrrs on the boot CPU
609 * This needs to be called early; before any of the other CPUs are
610 * initialized (i.e. before smp_init()).
613 void __init mtrr_bp_init(void)
615 init_ifs();
617 if (cpu_has_mtrr) {
618 mtrr_if = &generic_mtrr_ops;
619 size_or_mask = 0xff000000; /* 36 bits */
620 size_and_mask = 0x00f00000;
622 /* This is an AMD specific MSR, but we assume(hope?) that
623 Intel will implement it to when they extend the address
624 bus of the Xeon. */
625 if (cpuid_eax(0x80000000) >= 0x80000008) {
626 u32 phys_addr;
627 phys_addr = cpuid_eax(0x80000008) & 0xff;
628 /* CPUID workaround for Intel 0F33/0F34 CPU */
629 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
630 boot_cpu_data.x86 == 0xF &&
631 boot_cpu_data.x86_model == 0x3 &&
632 (boot_cpu_data.x86_mask == 0x3 ||
633 boot_cpu_data.x86_mask == 0x4))
634 phys_addr = 36;
636 size_or_mask = ~((1 << (phys_addr - PAGE_SHIFT)) - 1);
637 size_and_mask = ~size_or_mask & 0xfff00000;
638 } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
639 boot_cpu_data.x86 == 6) {
640 /* VIA C* family have Intel style MTRRs, but
641 don't support PAE */
642 size_or_mask = 0xfff00000; /* 32 bits */
643 size_and_mask = 0;
645 } else {
646 switch (boot_cpu_data.x86_vendor) {
647 case X86_VENDOR_AMD:
648 if (cpu_has_k6_mtrr) {
649 /* Pre-Athlon (K6) AMD CPU MTRRs */
650 mtrr_if = mtrr_ops[X86_VENDOR_AMD];
651 size_or_mask = 0xfff00000; /* 32 bits */
652 size_and_mask = 0;
654 break;
655 case X86_VENDOR_CENTAUR:
656 if (cpu_has_centaur_mcr) {
657 mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
658 size_or_mask = 0xfff00000; /* 32 bits */
659 size_and_mask = 0;
661 break;
662 case X86_VENDOR_CYRIX:
663 if (cpu_has_cyrix_arr) {
664 mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
665 size_or_mask = 0xfff00000; /* 32 bits */
666 size_and_mask = 0;
668 break;
669 default:
670 break;
674 if (mtrr_if) {
675 set_num_var_ranges();
676 init_table();
677 if (use_intel())
678 get_mtrr_state();
682 void mtrr_ap_init(void)
684 unsigned long flags;
686 if (!mtrr_if || !use_intel())
687 return;
689 * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
690 * but this routine will be called in cpu boot time, holding the lock
691 * breaks it. This routine is called in two cases: 1.very earily time
692 * of software resume, when there absolutely isn't mtrr entry changes;
693 * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
694 * prevent mtrr entry changes
696 local_irq_save(flags);
698 mtrr_if->set_all();
700 local_irq_restore(flags);
703 static int __init mtrr_init_finialize(void)
705 if (!mtrr_if)
706 return 0;
707 if (use_intel())
708 mtrr_state_warn();
709 else {
710 /* The CPUs haven't MTRR and seemes not support SMP. They have
711 * specific drivers, we use a tricky method to support
712 * suspend/resume for them.
713 * TBD: is there any system with such CPU which supports
714 * suspend/resume? if no, we should remove the code.
716 sysdev_driver_register(&cpu_sysdev_class,
717 &mtrr_sysdev_driver);
719 return 0;
721 subsys_initcall(mtrr_init_finialize);