iommu-helper: use bitmap library
[safe/jmp/linux-2.6] / arch / x86 / kernel / pci-gart_64.c
1 /*
2  * Dynamic DMA mapping support for AMD Hammer.
3  *
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.
7  *
8  * See Documentation/PCI/PCI-DMA-mapping.txt for the interface specification.
9  *
10  * Copyright 2002 Andi Kleen, SuSE Labs.
11  * Subject to the GNU General Public License v2 only.
12  */
13
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/bitmap.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>
43
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 */
47
48 static u32 *iommu_gatt_base;            /* Remapping table */
49
50 static dma_addr_t bad_dma_addr;
51
52 /*
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).
58  */
59 static int iommu_fullflush = 1;
60
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;
65
66 static u32 gart_unmapped_entry;
67
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))
73
74 #define EMERGENCY_PAGES 32 /* = 128KB */
75
76 #ifdef CONFIG_AGP
77 #define AGPEXTERN extern
78 #else
79 #define AGPEXTERN
80 #endif
81
82 /* backdoor interface to AGP driver */
83 AGPEXTERN int agp_memory_reserved;
84 AGPEXTERN __u32 *agp_gatt_table;
85
86 static unsigned long next_bit;  /* protected by iommu_bitmap_lock */
87 static bool need_flush;         /* global flush state. set for each gart wrap */
88
89 static unsigned long alloc_iommu(struct device *dev, int size,
90                                  unsigned long align_mask)
91 {
92         unsigned long offset, flags;
93         unsigned long boundary_size;
94         unsigned long base_index;
95
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;
100
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);
109         }
110         if (offset != -1) {
111                 next_bit = offset+size;
112                 if (next_bit >= iommu_pages) {
113                         next_bit = 0;
114                         need_flush = true;
115                 }
116         }
117         if (iommu_fullflush)
118                 need_flush = true;
119         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
120
121         return offset;
122 }
123
124 static void free_iommu(unsigned long offset, int size)
125 {
126         unsigned long flags;
127
128         spin_lock_irqsave(&iommu_bitmap_lock, flags);
129         bitmap_clear(iommu_gart_bitmap, offset, size);
130         if (offset >= next_bit)
131                 next_bit = offset + size;
132         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
133 }
134
135 /*
136  * Use global flush state to avoid races with multiple flushers.
137  */
138 static void flush_gart(void)
139 {
140         unsigned long flags;
141
142         spin_lock_irqsave(&iommu_bitmap_lock, flags);
143         if (need_flush) {
144                 k8_flush_garts();
145                 need_flush = false;
146         }
147         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
148 }
149
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;
154
155 static void dump_leak(void)
156 {
157         static int dump;
158
159         if (dump)
160                 return;
161         dump = 1;
162
163         show_stack(NULL, NULL);
164         debug_dma_dump_mappings(NULL);
165 }
166 #endif
167
168 static void iommu_full(struct device *dev, size_t size, int dir)
169 {
170         /*
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.
178          */
179
180         dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
181
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");
188         }
189 #ifdef CONFIG_IOMMU_LEAK
190         dump_leak();
191 #endif
192 }
193
194 static inline int
195 need_iommu(struct device *dev, unsigned long addr, size_t size)
196 {
197         return force_iommu || !dma_capable(dev, addr, size);
198 }
199
200 static inline int
201 nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
202 {
203         return !dma_capable(dev, addr, size);
204 }
205
206 /* Map a single continuous physical area into the IOMMU.
207  * Caller needs to check if the iommu is needed and flush.
208  */
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)
211 {
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;
215
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;
223         }
224
225         for (i = 0; i < npages; i++) {
226                 iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
227                 phys_mem += PAGE_SIZE;
228         }
229         return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
230 }
231
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)
237 {
238         unsigned long bus;
239         phys_addr_t paddr = page_to_phys(page) + offset;
240
241         if (!dev)
242                 dev = &x86_dma_fallback_dev;
243
244         if (!need_iommu(dev, paddr, size))
245                 return paddr;
246
247         bus = dma_map_area(dev, paddr, size, dir, 0);
248         flush_gart();
249
250         return bus;
251 }
252
253 /*
254  * Free a DMA mapping.
255  */
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)
259 {
260         unsigned long iommu_page;
261         int npages;
262         int i;
263
264         if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
265             dma_addr >= iommu_bus_base + iommu_size)
266                 return;
267
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;
272         }
273         free_iommu(iommu_page, npages);
274 }
275
276 /*
277  * Wrapper for pci_unmap_single working with scatterlists.
278  */
279 static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
280                           enum dma_data_direction dir, struct dma_attrs *attrs)
281 {
282         struct scatterlist *s;
283         int i;
284
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);
289         }
290 }
291
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)
295 {
296         struct scatterlist *s;
297         int i;
298
299 #ifdef CONFIG_IOMMU_DEBUG
300         pr_debug("dma_map_sg overflow\n");
301 #endif
302
303         for_each_sg(sg, s, nents, i) {
304                 unsigned long addr = sg_phys(s);
305
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;
314                         }
315                 }
316                 s->dma_address = addr;
317                 s->dma_length = s->length;
318         }
319         flush_gart();
320
321         return nents;
322 }
323
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)
328 {
329         unsigned long iommu_start = alloc_iommu(dev, pages, 0);
330         unsigned long iommu_page = iommu_start;
331         struct scatterlist *s;
332         int i;
333
334         if (iommu_start == -1)
335                 return -1;
336
337         for_each_sg(start, s, nelems, i) {
338                 unsigned long pages, addr;
339                 unsigned long phys_addr = s->dma_address;
340
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;
348                 }
349
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++;
356                 }
357         }
358         BUG_ON(iommu_page - iommu_start != pages);
359
360         return 0;
361 }
362
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)
366 {
367         if (!need) {
368                 BUG_ON(nelems != 1);
369                 sout->dma_address = start->dma_address;
370                 sout->dma_length = start->length;
371                 return 0;
372         }
373         return __dma_map_cont(dev, start, nelems, sout, pages);
374 }
375
376 /*
377  * DMA map all entries in a scatterlist.
378  * Merge chunks that have page aligned sizes into a continuous mapping.
379  */
380 static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
381                        enum dma_data_direction dir, struct dma_attrs *attrs)
382 {
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;
388
389         if (nents == 0)
390                 return 0;
391
392         if (!dev)
393                 dev = &x86_dma_fallback_dev;
394
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 */
402
403         for_each_sg(sg, s, nents, i) {
404                 dma_addr_t addr = sg_phys(s);
405
406                 s->dma_address = addr;
407                 BUG_ON(s->length == 0);
408
409                 nextneed = need_iommu(dev, addr, s->length);
410
411                 /* Handle the previous not yet processed entries */
412                 if (i > start) {
413                         /*
414                          * Can only merge when the last chunk ends on a
415                          * page boundary and the new one doesn't have an
416                          * offset.
417                          */
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++;
425
426                                 seg_size        = 0;
427                                 sgmap           = sg_next(sgmap);
428                                 pages           = 0;
429                                 start           = i;
430                                 start_sg        = s;
431                         }
432                 }
433
434                 seg_size += s->length;
435                 need = nextneed;
436                 pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE);
437                 ps = s;
438         }
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;
446         }
447         return out;
448
449 error:
450         flush_gart();
451         gart_unmap_sg(dev, sg, out, dir, NULL);
452
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;
458         }
459         if (panic_on_overflow)
460                 panic("dma_map_sg: overflow on %lu pages\n", pages);
461
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;
466 }
467
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)
472 {
473         dma_addr_t paddr;
474         unsigned long align_mask;
475         struct page *page;
476
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;
482
483                 align_mask = (1UL << get_order(size)) - 1;
484                 paddr = dma_map_area(dev, page_to_phys(page), size,
485                                      DMA_BIDIRECTIONAL, align_mask);
486
487                 flush_gart();
488                 if (paddr != bad_dma_addr) {
489                         *dma_addr = paddr;
490                         return page_address(page);
491                 }
492                 __free_pages(page, get_order(size));
493         } else
494                 return dma_generic_alloc_coherent(dev, size, dma_addr, flag);
495
496         return NULL;
497 }
498
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)
503 {
504         gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, NULL);
505         free_pages((unsigned long)vaddr, get_order(size));
506 }
507
508 static int gart_mapping_error(struct device *dev, dma_addr_t dma_addr)
509 {
510         return (dma_addr == bad_dma_addr);
511 }
512
513 static int no_agp;
514
515 static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
516 {
517         unsigned long a;
518
519         if (!iommu_size) {
520                 iommu_size = aper_size;
521                 if (!no_agp)
522                         iommu_size /= 2;
523         }
524
525         a = aper + iommu_size;
526         iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
527
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);
533         }
534
535         return iommu_size;
536 }
537
538 static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
539 {
540         unsigned aper_size = 0, aper_base_32, aper_order;
541         u64 aper_base;
542
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;
546
547         aper_base = aper_base_32 & 0x7fff;
548         aper_base <<= 25;
549
550         aper_size = (32 * 1024 * 1024) << aper_order;
551         if (aper_base + aper_size > 0x100000000UL || !aper_size)
552                 aper_base = 0;
553
554         *size = aper_size;
555         return aper_base;
556 }
557
558 static void enable_gart_translations(void)
559 {
560         int i;
561
562         for (i = 0; i < num_k8_northbridges; i++) {
563                 struct pci_dev *dev = k8_northbridges[i];
564
565                 enable_gart_translation(dev, __pa(agp_gatt_table));
566         }
567 }
568
569 /*
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().
572  */
573 static bool fix_up_north_bridges;
574 static u32 aperture_order;
575 static u32 aperture_alloc;
576
577 void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
578 {
579         fix_up_north_bridges = true;
580         aperture_order = aper_order;
581         aperture_alloc = aper_alloc;
582 }
583
584 static void gart_fixup_northbridges(struct sys_device *dev)
585 {
586         int i;
587
588         if (!fix_up_north_bridges)
589                 return;
590
591         pr_info("PCI-DMA: Restoring GART aperture settings\n");
592
593         for (i = 0; i < num_k8_northbridges; i++) {
594                 struct pci_dev *dev = k8_northbridges[i];
595
596                 /*
597                  * Don't enable translations just yet.  That is the next
598                  * step.  Restore the pre-suspend aperture settings.
599                  */
600                 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, aperture_order << 1);
601                 pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, aperture_alloc >> 25);
602         }
603 }
604
605 static int gart_resume(struct sys_device *dev)
606 {
607         pr_info("PCI-DMA: Resuming GART IOMMU\n");
608
609         gart_fixup_northbridges(dev);
610
611         enable_gart_translations();
612
613         return 0;
614 }
615
616 static int gart_suspend(struct sys_device *dev, pm_message_t state)
617 {
618         return 0;
619 }
620
621 static struct sysdev_class gart_sysdev_class = {
622         .name           = "gart",
623         .suspend        = gart_suspend,
624         .resume         = gart_resume,
625
626 };
627
628 static struct sys_device device_gart = {
629         .cls            = &gart_sysdev_class,
630 };
631
632 /*
633  * Private Northbridge GATT initialization in case we cannot use the
634  * AGP driver for some reason.
635  */
636 static __init int init_k8_gatt(struct agp_kern_info *info)
637 {
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;
643
644         pr_info("PCI-DMA: Disabling AGP.\n");
645
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;
653
654                 if (!aper_base) {
655                         aper_size = new_aper_size;
656                         aper_base = new_aper_base;
657                 }
658                 if (aper_size != new_aper_size || aper_base != new_aper_base)
659                         goto nommu;
660         }
661         if (!aper_base)
662                 goto nommu;
663
664         info->aper_base = aper_base;
665         info->aper_size = aper_size >> 20;
666
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");
674
675         agp_gatt_table = gatt;
676
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");
683
684         flush_gart();
685
686         pr_info("PCI-DMA: aperture base @ %x size %u KB\n",
687                aper_base, aper_size>>10);
688
689         return 0;
690
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;
696 }
697
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,
706 };
707
708 static void gart_iommu_shutdown(void)
709 {
710         struct pci_dev *dev;
711         int i;
712
713         /* don't shutdown it if there is AGP installed */
714         if (!no_agp)
715                 return;
716
717         for (i = 0; i < num_k8_northbridges; i++) {
718                 u32 ctl;
719
720                 dev = k8_northbridges[i];
721                 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
722
723                 ctl &= ~GARTEN;
724
725                 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
726         }
727 }
728
729 int __init gart_iommu_init(void)
730 {
731         struct agp_kern_info info;
732         unsigned long iommu_start;
733         unsigned long aper_base, aper_size;
734         unsigned long start_pfn, end_pfn;
735         unsigned long scratch;
736         long i;
737
738         if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0)
739                 return 0;
740
741 #ifndef CONFIG_AGP_AMD64
742         no_agp = 1;
743 #else
744         /* Makefile puts PCI initialization via subsys_initcall first. */
745         /* Add other K8 AGP bridge drivers here */
746         no_agp = no_agp ||
747                 (agp_amd64_init() < 0) ||
748                 (agp_copy_info(agp_bridge, &info) < 0);
749 #endif
750
751         if (no_iommu ||
752             (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
753             !gart_iommu_aperture ||
754             (no_agp && init_k8_gatt(&info) < 0)) {
755                 if (max_pfn > MAX_DMA32_PFN) {
756                         pr_warning("More than 4GB of memory but GART IOMMU not available.\n");
757                         pr_warning("falling back to iommu=soft.\n");
758                 }
759                 return 0;
760         }
761
762         /* need to map that range */
763         aper_size       = info.aper_size << 20;
764         aper_base       = info.aper_base;
765         end_pfn         = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
766
767         if (end_pfn > max_low_pfn_mapped) {
768                 start_pfn = (aper_base>>PAGE_SHIFT);
769                 init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
770         }
771
772         pr_info("PCI-DMA: using GART IOMMU.\n");
773         iommu_size = check_iommu_size(info.aper_base, aper_size);
774         iommu_pages = iommu_size >> PAGE_SHIFT;
775
776         iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
777                                                       get_order(iommu_pages/8));
778         if (!iommu_gart_bitmap)
779                 panic("Cannot allocate iommu bitmap\n");
780
781 #ifdef CONFIG_IOMMU_LEAK
782         if (leak_trace) {
783                 int ret;
784
785                 ret = dma_debug_resize_entries(iommu_pages);
786                 if (ret)
787                         pr_debug("PCI-DMA: Cannot trace all the entries\n");
788         }
789 #endif
790
791         /*
792          * Out of IOMMU space handling.
793          * Reserve some invalid pages at the beginning of the GART.
794          */
795         bitmap_set(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
796
797         pr_info("PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
798                iommu_size >> 20);
799
800         agp_memory_reserved     = iommu_size;
801         iommu_start             = aper_size - iommu_size;
802         iommu_bus_base          = info.aper_base + iommu_start;
803         bad_dma_addr            = iommu_bus_base;
804         iommu_gatt_base         = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
805
806         /*
807          * Unmap the IOMMU part of the GART. The alias of the page is
808          * always mapped with cache enabled and there is no full cache
809          * coherency across the GART remapping. The unmapping avoids
810          * automatic prefetches from the CPU allocating cache lines in
811          * there. All CPU accesses are done via the direct mapping to
812          * the backing memory. The GART address is only used by PCI
813          * devices.
814          */
815         set_memory_np((unsigned long)__va(iommu_bus_base),
816                                 iommu_size >> PAGE_SHIFT);
817         /*
818          * Tricky. The GART table remaps the physical memory range,
819          * so the CPU wont notice potential aliases and if the memory
820          * is remapped to UC later on, we might surprise the PCI devices
821          * with a stray writeout of a cacheline. So play it sure and
822          * do an explicit, full-scale wbinvd() _after_ having marked all
823          * the pages as Not-Present:
824          */
825         wbinvd();
826
827         /*
828          * Now all caches are flushed and we can safely enable
829          * GART hardware.  Doing it early leaves the possibility
830          * of stale cache entries that can lead to GART PTE
831          * errors.
832          */
833         enable_gart_translations();
834
835         /*
836          * Try to workaround a bug (thanks to BenH):
837          * Set unmapped entries to a scratch page instead of 0.
838          * Any prefetches that hit unmapped entries won't get an bus abort
839          * then. (P2P bridge may be prefetching on DMA reads).
840          */
841         scratch = get_zeroed_page(GFP_KERNEL);
842         if (!scratch)
843                 panic("Cannot allocate iommu scratch page");
844         gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
845         for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
846                 iommu_gatt_base[i] = gart_unmapped_entry;
847
848         flush_gart();
849         dma_ops = &gart_dma_ops;
850         x86_platform.iommu_shutdown = gart_iommu_shutdown;
851         swiotlb = 0;
852
853         return 0;
854 }
855
856 void __init gart_parse_options(char *p)
857 {
858         int arg;
859
860 #ifdef CONFIG_IOMMU_LEAK
861         if (!strncmp(p, "leak", 4)) {
862                 leak_trace = 1;
863                 p += 4;
864                 if (*p == '=')
865                         ++p;
866                 if (isdigit(*p) && get_option(&p, &arg))
867                         iommu_leak_pages = arg;
868         }
869 #endif
870         if (isdigit(*p) && get_option(&p, &arg))
871                 iommu_size = arg;
872         if (!strncmp(p, "fullflush", 9))
873                 iommu_fullflush = 1;
874         if (!strncmp(p, "nofullflush", 11))
875                 iommu_fullflush = 0;
876         if (!strncmp(p, "noagp", 5))
877                 no_agp = 1;
878         if (!strncmp(p, "noaperture", 10))
879                 fix_aperture = 0;
880         /* duplicated from pci-dma.c */
881         if (!strncmp(p, "force", 5))
882                 gart_iommu_aperture_allowed = 1;
883         if (!strncmp(p, "allowed", 7))
884                 gart_iommu_aperture_allowed = 1;
885         if (!strncmp(p, "memaper", 7)) {
886                 fallback_aper_force = 1;
887                 p += 7;
888                 if (*p == '=') {
889                         ++p;
890                         if (get_option(&p, &arg))
891                                 fallback_aper_order = arg;
892                 }
893         }
894 }