x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / arch / x86 / kernel / pci-gart_64.c
blobe6a0d402f1714dec4aff2e160b0eff255ea5307b
1 /*
2 * Dynamic DMA mapping support for AMD Hammer.
4 * Use the integrated AGP GART in the Hammer northbridge as an IOMMU for PCI.
5 * This allows to use PCI devices that only support 32bit addresses on systems
6 * with more than 4GB.
8 * See Documentation/PCI/PCI-DMA-mapping.txt for the interface specification.
10 * Copyright 2002 Andi Kleen, SuSE Labs.
11 * Subject to the GNU General Public License v2 only.
14 #include <linux/types.h>
15 #include <linux/ctype.h>
16 #include <linux/agp_backend.h>
17 #include <linux/init.h>
18 #include <linux/mm.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/spinlock.h>
22 #include <linux/pci.h>
23 #include <linux/module.h>
24 #include <linux/topology.h>
25 #include <linux/interrupt.h>
26 #include <linux/bitops.h>
27 #include <linux/kdebug.h>
28 #include <linux/scatterlist.h>
29 #include <linux/iommu-helper.h>
30 #include <linux/sysdev.h>
31 #include <linux/io.h>
32 #include <asm/atomic.h>
33 #include <asm/mtrr.h>
34 #include <asm/pgtable.h>
35 #include <asm/proto.h>
36 #include <asm/iommu.h>
37 #include <asm/gart.h>
38 #include <asm/cacheflush.h>
39 #include <asm/swiotlb.h>
40 #include <asm/dma.h>
41 #include <asm/k8.h>
42 #include <asm/x86_init.h>
44 static unsigned long iommu_bus_base; /* GART remapping area (physical) */
45 static unsigned long iommu_size; /* size of remapping area bytes */
46 static unsigned long iommu_pages; /* .. and in pages */
48 static u32 *iommu_gatt_base; /* Remapping table */
50 static dma_addr_t bad_dma_addr;
53 * If this is disabled the IOMMU will use an optimized flushing strategy
54 * of only flushing when an mapping is reused. With it true the GART is
55 * flushed for every mapping. Problem is that doing the lazy flush seems
56 * to trigger bugs with some popular PCI cards, in particular 3ware (but
57 * has been also also seen with Qlogic at least).
59 static int iommu_fullflush = 1;
61 /* Allocation bitmap for the remapping area: */
62 static DEFINE_SPINLOCK(iommu_bitmap_lock);
63 /* Guarded by iommu_bitmap_lock: */
64 static unsigned long *iommu_gart_bitmap;
66 static u32 gart_unmapped_entry;
68 #define GPTE_VALID 1
69 #define GPTE_COHERENT 2
70 #define GPTE_ENCODE(x) \
71 (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
72 #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
74 #define EMERGENCY_PAGES 32 /* = 128KB */
76 #ifdef CONFIG_AGP
77 #define AGPEXTERN extern
78 #else
79 #define AGPEXTERN
80 #endif
82 /* backdoor interface to AGP driver */
83 AGPEXTERN int agp_memory_reserved;
84 AGPEXTERN __u32 *agp_gatt_table;
86 static unsigned long next_bit; /* protected by iommu_bitmap_lock */
87 static bool need_flush; /* global flush state. set for each gart wrap */
89 static unsigned long alloc_iommu(struct device *dev, int size,
90 unsigned long align_mask)
92 unsigned long offset, flags;
93 unsigned long boundary_size;
94 unsigned long base_index;
96 base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
97 PAGE_SIZE) >> PAGE_SHIFT;
98 boundary_size = ALIGN((u64)dma_get_seg_boundary(dev) + 1,
99 PAGE_SIZE) >> PAGE_SHIFT;
101 spin_lock_irqsave(&iommu_bitmap_lock, flags);
102 offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit,
103 size, base_index, boundary_size, align_mask);
104 if (offset == -1) {
105 need_flush = true;
106 offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0,
107 size, base_index, boundary_size,
108 align_mask);
110 if (offset != -1) {
111 next_bit = offset+size;
112 if (next_bit >= iommu_pages) {
113 next_bit = 0;
114 need_flush = true;
117 if (iommu_fullflush)
118 need_flush = true;
119 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
121 return offset;
124 static void free_iommu(unsigned long offset, int size)
126 unsigned long flags;
128 spin_lock_irqsave(&iommu_bitmap_lock, flags);
129 iommu_area_free(iommu_gart_bitmap, offset, size);
130 if (offset >= next_bit)
131 next_bit = offset + size;
132 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
136 * Use global flush state to avoid races with multiple flushers.
138 static void flush_gart(void)
140 unsigned long flags;
142 spin_lock_irqsave(&iommu_bitmap_lock, flags);
143 if (need_flush) {
144 k8_flush_garts();
145 need_flush = false;
147 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
150 #ifdef CONFIG_IOMMU_LEAK
151 /* Debugging aid for drivers that don't free their IOMMU tables */
152 static int leak_trace;
153 static int iommu_leak_pages = 20;
155 static void dump_leak(void)
157 static int dump;
159 if (dump)
160 return;
161 dump = 1;
163 show_stack(NULL, NULL);
164 debug_dma_dump_mappings(NULL);
166 #endif
168 static void iommu_full(struct device *dev, size_t size, int dir)
171 * Ran out of IOMMU space for this operation. This is very bad.
172 * Unfortunately the drivers cannot handle this operation properly.
173 * Return some non mapped prereserved space in the aperture and
174 * let the Northbridge deal with it. This will result in garbage
175 * in the IO operation. When the size exceeds the prereserved space
176 * memory corruption will occur or random memory will be DMAed
177 * out. Hopefully no network devices use single mappings that big.
180 dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
182 if (size > PAGE_SIZE*EMERGENCY_PAGES) {
183 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
184 panic("PCI-DMA: Memory would be corrupted\n");
185 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
186 panic(KERN_ERR
187 "PCI-DMA: Random memory would be DMAed\n");
189 #ifdef CONFIG_IOMMU_LEAK
190 dump_leak();
191 #endif
194 static inline int
195 need_iommu(struct device *dev, unsigned long addr, size_t size)
197 return force_iommu || !dma_capable(dev, addr, size);
200 static inline int
201 nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
203 return !dma_capable(dev, addr, size);
206 /* Map a single continuous physical area into the IOMMU.
207 * Caller needs to check if the iommu is needed and flush.
209 static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
210 size_t size, int dir, unsigned long align_mask)
212 unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE);
213 unsigned long iommu_page = alloc_iommu(dev, npages, align_mask);
214 int i;
216 if (iommu_page == -1) {
217 if (!nonforced_iommu(dev, phys_mem, size))
218 return phys_mem;
219 if (panic_on_overflow)
220 panic("dma_map_area overflow %lu bytes\n", size);
221 iommu_full(dev, size, dir);
222 return bad_dma_addr;
225 for (i = 0; i < npages; i++) {
226 iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
227 phys_mem += PAGE_SIZE;
229 return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
232 /* Map a single area into the IOMMU */
233 static dma_addr_t gart_map_page(struct device *dev, struct page *page,
234 unsigned long offset, size_t size,
235 enum dma_data_direction dir,
236 struct dma_attrs *attrs)
238 unsigned long bus;
239 phys_addr_t paddr = page_to_phys(page) + offset;
241 if (!dev)
242 dev = &x86_dma_fallback_dev;
244 if (!need_iommu(dev, paddr, size))
245 return paddr;
247 bus = dma_map_area(dev, paddr, size, dir, 0);
248 flush_gart();
250 return bus;
254 * Free a DMA mapping.
256 static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr,
257 size_t size, enum dma_data_direction dir,
258 struct dma_attrs *attrs)
260 unsigned long iommu_page;
261 int npages;
262 int i;
264 if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
265 dma_addr >= iommu_bus_base + iommu_size)
266 return;
268 iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
269 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
270 for (i = 0; i < npages; i++) {
271 iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
273 free_iommu(iommu_page, npages);
277 * Wrapper for pci_unmap_single working with scatterlists.
279 static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
280 enum dma_data_direction dir, struct dma_attrs *attrs)
282 struct scatterlist *s;
283 int i;
285 for_each_sg(sg, s, nents, i) {
286 if (!s->dma_length || !s->length)
287 break;
288 gart_unmap_page(dev, s->dma_address, s->dma_length, dir, NULL);
292 /* Fallback for dma_map_sg in case of overflow */
293 static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
294 int nents, int dir)
296 struct scatterlist *s;
297 int i;
299 #ifdef CONFIG_IOMMU_DEBUG
300 pr_debug("dma_map_sg overflow\n");
301 #endif
303 for_each_sg(sg, s, nents, i) {
304 unsigned long addr = sg_phys(s);
306 if (nonforced_iommu(dev, addr, s->length)) {
307 addr = dma_map_area(dev, addr, s->length, dir, 0);
308 if (addr == bad_dma_addr) {
309 if (i > 0)
310 gart_unmap_sg(dev, sg, i, dir, NULL);
311 nents = 0;
312 sg[0].dma_length = 0;
313 break;
316 s->dma_address = addr;
317 s->dma_length = s->length;
319 flush_gart();
321 return nents;
324 /* Map multiple scatterlist entries continuous into the first. */
325 static int __dma_map_cont(struct device *dev, struct scatterlist *start,
326 int nelems, struct scatterlist *sout,
327 unsigned long pages)
329 unsigned long iommu_start = alloc_iommu(dev, pages, 0);
330 unsigned long iommu_page = iommu_start;
331 struct scatterlist *s;
332 int i;
334 if (iommu_start == -1)
335 return -1;
337 for_each_sg(start, s, nelems, i) {
338 unsigned long pages, addr;
339 unsigned long phys_addr = s->dma_address;
341 BUG_ON(s != start && s->offset);
342 if (s == start) {
343 sout->dma_address = iommu_bus_base;
344 sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
345 sout->dma_length = s->length;
346 } else {
347 sout->dma_length += s->length;
350 addr = phys_addr;
351 pages = iommu_num_pages(s->offset, s->length, PAGE_SIZE);
352 while (pages--) {
353 iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
354 addr += PAGE_SIZE;
355 iommu_page++;
358 BUG_ON(iommu_page - iommu_start != pages);
360 return 0;
363 static inline int
364 dma_map_cont(struct device *dev, struct scatterlist *start, int nelems,
365 struct scatterlist *sout, unsigned long pages, int need)
367 if (!need) {
368 BUG_ON(nelems != 1);
369 sout->dma_address = start->dma_address;
370 sout->dma_length = start->length;
371 return 0;
373 return __dma_map_cont(dev, start, nelems, sout, pages);
377 * DMA map all entries in a scatterlist.
378 * Merge chunks that have page aligned sizes into a continuous mapping.
380 static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
381 enum dma_data_direction dir, struct dma_attrs *attrs)
383 struct scatterlist *s, *ps, *start_sg, *sgmap;
384 int need = 0, nextneed, i, out, start;
385 unsigned long pages = 0;
386 unsigned int seg_size;
387 unsigned int max_seg_size;
389 if (nents == 0)
390 return 0;
392 if (!dev)
393 dev = &x86_dma_fallback_dev;
395 out = 0;
396 start = 0;
397 start_sg = sg;
398 sgmap = sg;
399 seg_size = 0;
400 max_seg_size = dma_get_max_seg_size(dev);
401 ps = NULL; /* shut up gcc */
403 for_each_sg(sg, s, nents, i) {
404 dma_addr_t addr = sg_phys(s);
406 s->dma_address = addr;
407 BUG_ON(s->length == 0);
409 nextneed = need_iommu(dev, addr, s->length);
411 /* Handle the previous not yet processed entries */
412 if (i > start) {
414 * Can only merge when the last chunk ends on a
415 * page boundary and the new one doesn't have an
416 * offset.
418 if (!iommu_merge || !nextneed || !need || s->offset ||
419 (s->length + seg_size > max_seg_size) ||
420 (ps->offset + ps->length) % PAGE_SIZE) {
421 if (dma_map_cont(dev, start_sg, i - start,
422 sgmap, pages, need) < 0)
423 goto error;
424 out++;
426 seg_size = 0;
427 sgmap = sg_next(sgmap);
428 pages = 0;
429 start = i;
430 start_sg = s;
434 seg_size += s->length;
435 need = nextneed;
436 pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE);
437 ps = s;
439 if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
440 goto error;
441 out++;
442 flush_gart();
443 if (out < nents) {
444 sgmap = sg_next(sgmap);
445 sgmap->dma_length = 0;
447 return out;
449 error:
450 flush_gart();
451 gart_unmap_sg(dev, sg, out, dir, NULL);
453 /* When it was forced or merged try again in a dumb way */
454 if (force_iommu || iommu_merge) {
455 out = dma_map_sg_nonforce(dev, sg, nents, dir);
456 if (out > 0)
457 return out;
459 if (panic_on_overflow)
460 panic("dma_map_sg: overflow on %lu pages\n", pages);
462 iommu_full(dev, pages << PAGE_SHIFT, dir);
463 for_each_sg(sg, s, nents, i)
464 s->dma_address = bad_dma_addr;
465 return 0;
468 /* allocate and map a coherent mapping */
469 static void *
470 gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,
471 gfp_t flag)
473 dma_addr_t paddr;
474 unsigned long align_mask;
475 struct page *page;
477 if (force_iommu && !(flag & GFP_DMA)) {
478 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
479 page = alloc_pages(flag | __GFP_ZERO, get_order(size));
480 if (!page)
481 return NULL;
483 align_mask = (1UL << get_order(size)) - 1;
484 paddr = dma_map_area(dev, page_to_phys(page), size,
485 DMA_BIDIRECTIONAL, align_mask);
487 flush_gart();
488 if (paddr != bad_dma_addr) {
489 *dma_addr = paddr;
490 return page_address(page);
492 __free_pages(page, get_order(size));
493 } else
494 return dma_generic_alloc_coherent(dev, size, dma_addr, flag);
496 return NULL;
499 /* free a coherent mapping */
500 static void
501 gart_free_coherent(struct device *dev, size_t size, void *vaddr,
502 dma_addr_t dma_addr)
504 gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, NULL);
505 free_pages((unsigned long)vaddr, get_order(size));
508 static int gart_mapping_error(struct device *dev, dma_addr_t dma_addr)
510 return (dma_addr == bad_dma_addr);
513 static int no_agp;
515 static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
517 unsigned long a;
519 if (!iommu_size) {
520 iommu_size = aper_size;
521 if (!no_agp)
522 iommu_size /= 2;
525 a = aper + iommu_size;
526 iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
528 if (iommu_size < 64*1024*1024) {
529 pr_warning(
530 "PCI-DMA: Warning: Small IOMMU %luMB."
531 " Consider increasing the AGP aperture in BIOS\n",
532 iommu_size >> 20);
535 return iommu_size;
538 static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
540 unsigned aper_size = 0, aper_base_32, aper_order;
541 u64 aper_base;
543 pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32);
544 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order);
545 aper_order = (aper_order >> 1) & 7;
547 aper_base = aper_base_32 & 0x7fff;
548 aper_base <<= 25;
550 aper_size = (32 * 1024 * 1024) << aper_order;
551 if (aper_base + aper_size > 0x100000000UL || !aper_size)
552 aper_base = 0;
554 *size = aper_size;
555 return aper_base;
558 static void enable_gart_translations(void)
560 int i;
562 for (i = 0; i < num_k8_northbridges; i++) {
563 struct pci_dev *dev = k8_northbridges[i];
565 enable_gart_translation(dev, __pa(agp_gatt_table));
570 * If fix_up_north_bridges is set, the north bridges have to be fixed up on
571 * resume in the same way as they are handled in gart_iommu_hole_init().
573 static bool fix_up_north_bridges;
574 static u32 aperture_order;
575 static u32 aperture_alloc;
577 void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
579 fix_up_north_bridges = true;
580 aperture_order = aper_order;
581 aperture_alloc = aper_alloc;
584 static void gart_fixup_northbridges(struct sys_device *dev)
586 int i;
588 if (!fix_up_north_bridges)
589 return;
591 pr_info("PCI-DMA: Restoring GART aperture settings\n");
593 for (i = 0; i < num_k8_northbridges; i++) {
594 struct pci_dev *dev = k8_northbridges[i];
597 * Don't enable translations just yet. That is the next
598 * step. Restore the pre-suspend aperture settings.
600 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, aperture_order << 1);
601 pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, aperture_alloc >> 25);
605 static int gart_resume(struct sys_device *dev)
607 pr_info("PCI-DMA: Resuming GART IOMMU\n");
609 gart_fixup_northbridges(dev);
611 enable_gart_translations();
613 return 0;
616 static int gart_suspend(struct sys_device *dev, pm_message_t state)
618 return 0;
621 static struct sysdev_class gart_sysdev_class = {
622 .name = "gart",
623 .suspend = gart_suspend,
624 .resume = gart_resume,
628 static struct sys_device device_gart = {
629 .cls = &gart_sysdev_class,
633 * Private Northbridge GATT initialization in case we cannot use the
634 * AGP driver for some reason.
636 static __init int init_k8_gatt(struct agp_kern_info *info)
638 unsigned aper_size, gatt_size, new_aper_size;
639 unsigned aper_base, new_aper_base;
640 struct pci_dev *dev;
641 void *gatt;
642 int i, error;
644 pr_info("PCI-DMA: Disabling AGP.\n");
646 aper_size = aper_base = info->aper_size = 0;
647 dev = NULL;
648 for (i = 0; i < num_k8_northbridges; i++) {
649 dev = k8_northbridges[i];
650 new_aper_base = read_aperture(dev, &new_aper_size);
651 if (!new_aper_base)
652 goto nommu;
654 if (!aper_base) {
655 aper_size = new_aper_size;
656 aper_base = new_aper_base;
658 if (aper_size != new_aper_size || aper_base != new_aper_base)
659 goto nommu;
661 if (!aper_base)
662 goto nommu;
664 info->aper_base = aper_base;
665 info->aper_size = aper_size >> 20;
667 gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
668 gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
669 get_order(gatt_size));
670 if (!gatt)
671 panic("Cannot allocate GATT table");
672 if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
673 panic("Could not set GART PTEs to uncacheable pages");
675 agp_gatt_table = gatt;
677 error = sysdev_class_register(&gart_sysdev_class);
678 if (!error)
679 error = sysdev_register(&device_gart);
680 if (error)
681 panic("Could not register gart_sysdev -- "
682 "would corrupt data on next suspend");
684 flush_gart();
686 pr_info("PCI-DMA: aperture base @ %x size %u KB\n",
687 aper_base, aper_size>>10);
689 return 0;
691 nommu:
692 /* Should not happen anymore */
693 pr_warning("PCI-DMA: More than 4GB of RAM and no IOMMU\n"
694 "falling back to iommu=soft.\n");
695 return -1;
698 static struct dma_map_ops gart_dma_ops = {
699 .map_sg = gart_map_sg,
700 .unmap_sg = gart_unmap_sg,
701 .map_page = gart_map_page,
702 .unmap_page = gart_unmap_page,
703 .alloc_coherent = gart_alloc_coherent,
704 .free_coherent = gart_free_coherent,
705 .mapping_error = gart_mapping_error,
708 static void gart_iommu_shutdown(void)
710 struct pci_dev *dev;
711 int i;
713 if (no_agp)
714 return;
716 for (i = 0; i < num_k8_northbridges; i++) {
717 u32 ctl;
719 dev = k8_northbridges[i];
720 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
722 ctl &= ~GARTEN;
724 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
728 int __init gart_iommu_init(void)
730 struct agp_kern_info info;
731 unsigned long iommu_start;
732 unsigned long aper_base, aper_size;
733 unsigned long start_pfn, end_pfn;
734 unsigned long scratch;
735 long i;
737 if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0)
738 return 0;
740 #ifndef CONFIG_AGP_AMD64
741 no_agp = 1;
742 #else
743 /* Makefile puts PCI initialization via subsys_initcall first. */
744 /* Add other K8 AGP bridge drivers here */
745 no_agp = no_agp ||
746 (agp_amd64_init() < 0) ||
747 (agp_copy_info(agp_bridge, &info) < 0);
748 #endif
750 if (no_iommu ||
751 (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
752 !gart_iommu_aperture ||
753 (no_agp && init_k8_gatt(&info) < 0)) {
754 if (max_pfn > MAX_DMA32_PFN) {
755 pr_warning("More than 4GB of memory but GART IOMMU not available.\n");
756 pr_warning("falling back to iommu=soft.\n");
758 return 0;
761 /* need to map that range */
762 aper_size = info.aper_size << 20;
763 aper_base = info.aper_base;
764 end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
766 if (end_pfn > max_low_pfn_mapped) {
767 start_pfn = (aper_base>>PAGE_SHIFT);
768 init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
771 pr_info("PCI-DMA: using GART IOMMU.\n");
772 iommu_size = check_iommu_size(info.aper_base, aper_size);
773 iommu_pages = iommu_size >> PAGE_SHIFT;
775 iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
776 get_order(iommu_pages/8));
777 if (!iommu_gart_bitmap)
778 panic("Cannot allocate iommu bitmap\n");
780 #ifdef CONFIG_IOMMU_LEAK
781 if (leak_trace) {
782 int ret;
784 ret = dma_debug_resize_entries(iommu_pages);
785 if (ret)
786 pr_debug("PCI-DMA: Cannot trace all the entries\n");
788 #endif
791 * Out of IOMMU space handling.
792 * Reserve some invalid pages at the beginning of the GART.
794 iommu_area_reserve(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
796 pr_info("PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
797 iommu_size >> 20);
799 agp_memory_reserved = iommu_size;
800 iommu_start = aper_size - iommu_size;
801 iommu_bus_base = info.aper_base + iommu_start;
802 bad_dma_addr = iommu_bus_base;
803 iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
806 * Unmap the IOMMU part of the GART. The alias of the page is
807 * always mapped with cache enabled and there is no full cache
808 * coherency across the GART remapping. The unmapping avoids
809 * automatic prefetches from the CPU allocating cache lines in
810 * there. All CPU accesses are done via the direct mapping to
811 * the backing memory. The GART address is only used by PCI
812 * devices.
814 set_memory_np((unsigned long)__va(iommu_bus_base),
815 iommu_size >> PAGE_SHIFT);
817 * Tricky. The GART table remaps the physical memory range,
818 * so the CPU wont notice potential aliases and if the memory
819 * is remapped to UC later on, we might surprise the PCI devices
820 * with a stray writeout of a cacheline. So play it sure and
821 * do an explicit, full-scale wbinvd() _after_ having marked all
822 * the pages as Not-Present:
824 wbinvd();
827 * Now all caches are flushed and we can safely enable
828 * GART hardware. Doing it early leaves the possibility
829 * of stale cache entries that can lead to GART PTE
830 * errors.
832 enable_gart_translations();
835 * Try to workaround a bug (thanks to BenH):
836 * Set unmapped entries to a scratch page instead of 0.
837 * Any prefetches that hit unmapped entries won't get an bus abort
838 * then. (P2P bridge may be prefetching on DMA reads).
840 scratch = get_zeroed_page(GFP_KERNEL);
841 if (!scratch)
842 panic("Cannot allocate iommu scratch page");
843 gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
844 for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
845 iommu_gatt_base[i] = gart_unmapped_entry;
847 flush_gart();
848 dma_ops = &gart_dma_ops;
849 x86_platform.iommu_shutdown = gart_iommu_shutdown;
850 swiotlb = 0;
852 return 0;
855 void __init gart_parse_options(char *p)
857 int arg;
859 #ifdef CONFIG_IOMMU_LEAK
860 if (!strncmp(p, "leak", 4)) {
861 leak_trace = 1;
862 p += 4;
863 if (*p == '=')
864 ++p;
865 if (isdigit(*p) && get_option(&p, &arg))
866 iommu_leak_pages = arg;
868 #endif
869 if (isdigit(*p) && get_option(&p, &arg))
870 iommu_size = arg;
871 if (!strncmp(p, "fullflush", 9))
872 iommu_fullflush = 1;
873 if (!strncmp(p, "nofullflush", 11))
874 iommu_fullflush = 0;
875 if (!strncmp(p, "noagp", 5))
876 no_agp = 1;
877 if (!strncmp(p, "noaperture", 10))
878 fix_aperture = 0;
879 /* duplicated from pci-dma.c */
880 if (!strncmp(p, "force", 5))
881 gart_iommu_aperture_allowed = 1;
882 if (!strncmp(p, "allowed", 7))
883 gart_iommu_aperture_allowed = 1;
884 if (!strncmp(p, "memaper", 7)) {
885 fallback_aper_force = 1;
886 p += 7;
887 if (*p == '=') {
888 ++p;
889 if (get_option(&p, &arg))
890 fallback_aper_order = arg;