b9dbe3ce967c4ad91702c22831a11b86e964fdcf
[linux-2.6.git] / arch / x86_64 / kernel / pci-gart.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/DMA-mapping.txt for the interface specification.
9  * 
10  * Copyright 2002 Andi Kleen, SuSE Labs.
11  */
12
13 #include <linux/config.h>
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/string.h>
20 #include <linux/spinlock.h>
21 #include <linux/pci.h>
22 #include <linux/module.h>
23 #include <linux/topology.h>
24 #include <linux/interrupt.h>
25 #include <linux/bitops.h>
26 #include <asm/atomic.h>
27 #include <asm/io.h>
28 #include <asm/mtrr.h>
29 #include <asm/pgtable.h>
30 #include <asm/proto.h>
31 #include <asm/cacheflush.h>
32 #include <asm/kdebug.h>
33 #include <asm/swiotlb.h>
34 #include <asm/dma.h>
35
36 unsigned long iommu_bus_base;   /* GART remapping area (physical) */
37 static unsigned long iommu_size;        /* size of remapping area bytes */
38 static unsigned long iommu_pages;       /* .. and in pages */
39
40 u32 *iommu_gatt_base;           /* Remapping table */
41
42 /* If this is disabled the IOMMU will use an optimized flushing strategy
43    of only flushing when an mapping is reused. With it true the GART is flushed 
44    for every mapping. Problem is that doing the lazy flush seems to trigger
45    bugs with some popular PCI cards, in particular 3ware (but has been also
46    also seen with Qlogic at least). */
47 int iommu_fullflush = 1;
48
49 #define MAX_NB 8
50
51 /* Allocation bitmap for the remapping area */ 
52 static DEFINE_SPINLOCK(iommu_bitmap_lock);
53 static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */
54
55 static u32 gart_unmapped_entry; 
56
57 #define GPTE_VALID    1
58 #define GPTE_COHERENT 2
59 #define GPTE_ENCODE(x) \
60         (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
61 #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
62
63 #define to_pages(addr,size) \
64         (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
65
66 #define for_all_nb(dev) \
67         dev = NULL;     \
68         while ((dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1103, dev))!=NULL)\
69              if (dev->bus->number == 0 &&                                    \
70                     (PCI_SLOT(dev->devfn) >= 24) && (PCI_SLOT(dev->devfn) <= 31))
71
72 static struct pci_dev *northbridges[MAX_NB];
73 static u32 northbridge_flush_word[MAX_NB];
74
75 #define EMERGENCY_PAGES 32 /* = 128KB */ 
76
77 #ifdef CONFIG_AGP
78 #define AGPEXTERN extern
79 #else
80 #define AGPEXTERN
81 #endif
82
83 /* backdoor interface to AGP driver */
84 AGPEXTERN int agp_memory_reserved;
85 AGPEXTERN __u32 *agp_gatt_table;
86
87 static unsigned long next_bit;  /* protected by iommu_bitmap_lock */
88 static int need_flush;          /* global flush state. set for each gart wrap */
89
90 static unsigned long alloc_iommu(int size) 
91 {       
92         unsigned long offset, flags;
93
94         spin_lock_irqsave(&iommu_bitmap_lock, flags);   
95         offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size);
96         if (offset == -1) {
97                 need_flush = 1;
98                 offset = find_next_zero_string(iommu_gart_bitmap,0,next_bit,size);
99         }
100         if (offset != -1) { 
101                 set_bit_string(iommu_gart_bitmap, offset, size); 
102                 next_bit = offset+size; 
103                 if (next_bit >= iommu_pages) { 
104                         next_bit = 0;
105                         need_flush = 1;
106                 } 
107         } 
108         if (iommu_fullflush)
109                 need_flush = 1;
110         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);      
111         return offset;
112
113
114 static void free_iommu(unsigned long offset, int size)
115
116         unsigned long flags;
117         spin_lock_irqsave(&iommu_bitmap_lock, flags);
118         __clear_bit_string(iommu_gart_bitmap, offset, size);
119         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
120
121
122 /* 
123  * Use global flush state to avoid races with multiple flushers.
124  */
125 static void flush_gart(struct device *dev)
126
127         unsigned long flags;
128         int flushed = 0;
129         int i, max;
130
131         spin_lock_irqsave(&iommu_bitmap_lock, flags);
132         if (need_flush) { 
133                 max = 0;
134                 for (i = 0; i < MAX_NB; i++) {
135                         if (!northbridges[i]) 
136                                 continue;
137                         pci_write_config_dword(northbridges[i], 0x9c, 
138                                                northbridge_flush_word[i] | 1); 
139                         flushed++;
140                         max = i;
141                 }
142                 for (i = 0; i <= max; i++) {
143                         u32 w;
144                         if (!northbridges[i])
145                                 continue;
146                         /* Make sure the hardware actually executed the flush. */
147                         do { 
148                                 pci_read_config_dword(northbridges[i], 0x9c, &w);
149                         } while (w & 1);
150                 } 
151                 if (!flushed) 
152                         printk("nothing to flush?\n");
153                 need_flush = 0;
154         } 
155         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
156
157
158
159
160 #ifdef CONFIG_IOMMU_LEAK
161
162 #define SET_LEAK(x) if (iommu_leak_tab) \
163                         iommu_leak_tab[x] = __builtin_return_address(0);
164 #define CLEAR_LEAK(x) if (iommu_leak_tab) \
165                         iommu_leak_tab[x] = NULL;
166
167 /* Debugging aid for drivers that don't free their IOMMU tables */
168 static void **iommu_leak_tab; 
169 static int leak_trace;
170 int iommu_leak_pages = 20; 
171 void dump_leak(void)
172 {
173         int i;
174         static int dump; 
175         if (dump || !iommu_leak_tab) return;
176         dump = 1;
177         show_stack(NULL,NULL);
178         /* Very crude. dump some from the end of the table too */ 
179         printk("Dumping %d pages from end of IOMMU:\n", iommu_leak_pages); 
180         for (i = 0; i < iommu_leak_pages; i+=2) {
181                 printk("%lu: ", iommu_pages-i);
182                 printk_address((unsigned long) iommu_leak_tab[iommu_pages-i]);
183                 printk("%c", (i+1)%2 == 0 ? '\n' : ' '); 
184         } 
185         printk("\n");
186 }
187 #else
188 #define SET_LEAK(x)
189 #define CLEAR_LEAK(x)
190 #endif
191
192 static void iommu_full(struct device *dev, size_t size, int dir)
193 {
194         /* 
195          * Ran out of IOMMU space for this operation. This is very bad.
196          * Unfortunately the drivers cannot handle this operation properly.
197          * Return some non mapped prereserved space in the aperture and 
198          * let the Northbridge deal with it. This will result in garbage
199          * in the IO operation. When the size exceeds the prereserved space
200          * memory corruption will occur or random memory will be DMAed 
201          * out. Hopefully no network devices use single mappings that big.
202          */ 
203         
204         printk(KERN_ERR 
205   "PCI-DMA: Out of IOMMU space for %lu bytes at device %s\n",
206                size, dev->bus_id);
207
208         if (size > PAGE_SIZE*EMERGENCY_PAGES) {
209                 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
210                         panic("PCI-DMA: Memory would be corrupted\n");
211                 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL) 
212                         panic(KERN_ERR "PCI-DMA: Random memory would be DMAed\n");
213         } 
214
215 #ifdef CONFIG_IOMMU_LEAK
216         dump_leak(); 
217 #endif
218
219
220 static inline int need_iommu(struct device *dev, unsigned long addr, size_t size)
221
222         u64 mask = *dev->dma_mask;
223         int high = addr + size >= mask;
224         int mmu = high;
225         if (force_iommu) 
226                 mmu = 1; 
227         return mmu; 
228 }
229
230 static inline int nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
231
232         u64 mask = *dev->dma_mask;
233         int high = addr + size >= mask;
234         int mmu = high;
235         return mmu; 
236 }
237
238 /* Map a single continuous physical area into the IOMMU.
239  * Caller needs to check if the iommu is needed and flush.
240  */
241 static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
242                                 size_t size, int dir)
243
244         unsigned long npages = to_pages(phys_mem, size);
245         unsigned long iommu_page = alloc_iommu(npages);
246         int i;
247         if (iommu_page == -1) {
248                 if (!nonforced_iommu(dev, phys_mem, size))
249                         return phys_mem; 
250                 if (panic_on_overflow)
251                         panic("dma_map_area overflow %lu bytes\n", size);
252                 iommu_full(dev, size, dir);
253                 return bad_dma_address;
254         }
255
256         for (i = 0; i < npages; i++) {
257                 iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
258                 SET_LEAK(iommu_page + i);
259                 phys_mem += PAGE_SIZE;
260         }
261         return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
262 }
263
264 static dma_addr_t gart_map_simple(struct device *dev, char *buf,
265                                  size_t size, int dir)
266 {
267         dma_addr_t map = dma_map_area(dev, virt_to_bus(buf), size, dir);
268         flush_gart(dev);
269         return map;
270 }
271
272 /* Map a single area into the IOMMU */
273 dma_addr_t gart_map_single(struct device *dev, void *addr, size_t size, int dir)
274 {
275         unsigned long phys_mem, bus;
276
277         BUG_ON(dir == DMA_NONE);
278
279         if (!dev)
280                 dev = &fallback_dev;
281
282         phys_mem = virt_to_phys(addr); 
283         if (!need_iommu(dev, phys_mem, size))
284                 return phys_mem; 
285
286         bus = gart_map_simple(dev, addr, size, dir);
287         return bus; 
288 }
289
290 /*
291  * Wrapper for pci_unmap_single working with scatterlists.
292  */
293 void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
294 {
295         int i;
296
297         for (i = 0; i < nents; i++) {
298                 struct scatterlist *s = &sg[i];
299                 if (!s->dma_length || !s->length)
300                         break;
301                 dma_unmap_single(dev, s->dma_address, s->dma_length, dir);
302         }
303 }
304
305 /* Fallback for dma_map_sg in case of overflow */
306 static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
307                                int nents, int dir)
308 {
309         int i;
310
311 #ifdef CONFIG_IOMMU_DEBUG
312         printk(KERN_DEBUG "dma_map_sg overflow\n");
313 #endif
314
315         for (i = 0; i < nents; i++ ) {
316                 struct scatterlist *s = &sg[i];
317                 unsigned long addr = page_to_phys(s->page) + s->offset; 
318                 if (nonforced_iommu(dev, addr, s->length)) { 
319                         addr = dma_map_area(dev, addr, s->length, dir);
320                         if (addr == bad_dma_address) { 
321                                 if (i > 0) 
322                                         gart_unmap_sg(dev, sg, i, dir);
323                                 nents = 0; 
324                                 sg[0].dma_length = 0;
325                                 break;
326                         }
327                 }
328                 s->dma_address = addr;
329                 s->dma_length = s->length;
330         }
331         flush_gart(dev);
332         return nents;
333 }
334
335 /* Map multiple scatterlist entries continuous into the first. */
336 static int __dma_map_cont(struct scatterlist *sg, int start, int stopat,
337                       struct scatterlist *sout, unsigned long pages)
338 {
339         unsigned long iommu_start = alloc_iommu(pages);
340         unsigned long iommu_page = iommu_start; 
341         int i;
342
343         if (iommu_start == -1)
344                 return -1;
345         
346         for (i = start; i < stopat; i++) {
347                 struct scatterlist *s = &sg[i];
348                 unsigned long pages, addr;
349                 unsigned long phys_addr = s->dma_address;
350                 
351                 BUG_ON(i > start && s->offset);
352                 if (i == start) {
353                         *sout = *s; 
354                         sout->dma_address = iommu_bus_base;
355                         sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
356                         sout->dma_length = s->length;
357                 } else { 
358                         sout->dma_length += s->length; 
359                 }
360
361                 addr = phys_addr;
362                 pages = to_pages(s->offset, s->length); 
363                 while (pages--) { 
364                         iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr); 
365                         SET_LEAK(iommu_page);
366                         addr += PAGE_SIZE;
367                         iommu_page++;
368                 }
369         } 
370         BUG_ON(iommu_page - iommu_start != pages);      
371         return 0;
372 }
373
374 static inline int dma_map_cont(struct scatterlist *sg, int start, int stopat,
375                       struct scatterlist *sout,
376                       unsigned long pages, int need)
377 {
378         if (!need) { 
379                 BUG_ON(stopat - start != 1);
380                 *sout = sg[start]; 
381                 sout->dma_length = sg[start].length; 
382                 return 0;
383         } 
384         return __dma_map_cont(sg, start, stopat, sout, pages);
385 }
386                 
387 /*
388  * DMA map all entries in a scatterlist.
389  * Merge chunks that have page aligned sizes into a continuous mapping. 
390  */
391 int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
392 {
393         int i;
394         int out;
395         int start;
396         unsigned long pages = 0;
397         int need = 0, nextneed;
398
399         BUG_ON(dir == DMA_NONE);
400         if (nents == 0) 
401                 return 0;
402
403         if (!dev)
404                 dev = &fallback_dev;
405
406         out = 0;
407         start = 0;
408         for (i = 0; i < nents; i++) {
409                 struct scatterlist *s = &sg[i];
410                 dma_addr_t addr = page_to_phys(s->page) + s->offset;
411                 s->dma_address = addr;
412                 BUG_ON(s->length == 0); 
413
414                 nextneed = need_iommu(dev, addr, s->length); 
415
416                 /* Handle the previous not yet processed entries */
417                 if (i > start) {
418                         struct scatterlist *ps = &sg[i-1];
419                         /* Can only merge when the last chunk ends on a page 
420                            boundary and the new one doesn't have an offset. */
421                         if (!iommu_merge || !nextneed || !need || s->offset ||
422                             (ps->offset + ps->length) % PAGE_SIZE) { 
423                                 if (dma_map_cont(sg, start, i, sg+out, pages,
424                                                  need) < 0)
425                                         goto error;
426                                 out++;
427                                 pages = 0;
428                                 start = i;      
429                         }
430                 }
431
432                 need = nextneed;
433                 pages += to_pages(s->offset, s->length);
434         }
435         if (dma_map_cont(sg, start, i, sg+out, pages, need) < 0)
436                 goto error;
437         out++;
438         flush_gart(dev);
439         if (out < nents) 
440                 sg[out].dma_length = 0; 
441         return out;
442
443 error:
444         flush_gart(NULL);
445         gart_unmap_sg(dev, sg, nents, dir);
446         /* When it was forced or merged try again in a dumb way */
447         if (force_iommu || iommu_merge) {
448                 out = dma_map_sg_nonforce(dev, sg, nents, dir);
449                 if (out > 0)
450                         return out;
451         }
452         if (panic_on_overflow)
453                 panic("dma_map_sg: overflow on %lu pages\n", pages);
454         iommu_full(dev, pages << PAGE_SHIFT, dir);
455         for (i = 0; i < nents; i++)
456                 sg[i].dma_address = bad_dma_address;
457         return 0;
458
459
460 /*
461  * Free a DMA mapping.
462  */ 
463 void gart_unmap_single(struct device *dev, dma_addr_t dma_addr,
464                       size_t size, int direction)
465 {
466         unsigned long iommu_page; 
467         int npages;
468         int i;
469
470         if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE || 
471             dma_addr >= iommu_bus_base + iommu_size)
472                 return;
473         iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;   
474         npages = to_pages(dma_addr, size);
475         for (i = 0; i < npages; i++) { 
476                 iommu_gatt_base[iommu_page + i] = gart_unmapped_entry; 
477                 CLEAR_LEAK(iommu_page + i);
478         }
479         free_iommu(iommu_page, npages);
480 }
481
482 static int no_agp;
483
484 static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
485
486         unsigned long a; 
487         if (!iommu_size) { 
488                 iommu_size = aper_size; 
489                 if (!no_agp) 
490                         iommu_size /= 2; 
491         } 
492
493         a = aper + iommu_size; 
494         iommu_size -= round_up(a, LARGE_PAGE_SIZE) - a;
495
496         if (iommu_size < 64*1024*1024) 
497                 printk(KERN_WARNING
498   "PCI-DMA: Warning: Small IOMMU %luMB. Consider increasing the AGP aperture in BIOS\n",iommu_size>>20); 
499         
500         return iommu_size;
501
502
503 static __init unsigned read_aperture(struct pci_dev *dev, u32 *size) 
504
505         unsigned aper_size = 0, aper_base_32;
506         u64 aper_base;
507         unsigned aper_order;
508
509         pci_read_config_dword(dev, 0x94, &aper_base_32); 
510         pci_read_config_dword(dev, 0x90, &aper_order);
511         aper_order = (aper_order >> 1) & 7;     
512
513         aper_base = aper_base_32 & 0x7fff; 
514         aper_base <<= 25;
515
516         aper_size = (32 * 1024 * 1024) << aper_order; 
517         if (aper_base + aper_size >= 0xffffffff || !aper_size)
518                 aper_base = 0;
519
520         *size = aper_size;
521         return aper_base;
522
523
524 /* 
525  * Private Northbridge GATT initialization in case we cannot use the
526  * AGP driver for some reason.  
527  */
528 static __init int init_k8_gatt(struct agp_kern_info *info)
529
530         struct pci_dev *dev;
531         void *gatt;
532         unsigned aper_base, new_aper_base;
533         unsigned aper_size, gatt_size, new_aper_size;
534         
535         printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
536         aper_size = aper_base = info->aper_size = 0;
537         for_all_nb(dev) { 
538                 new_aper_base = read_aperture(dev, &new_aper_size); 
539                 if (!new_aper_base) 
540                         goto nommu; 
541                 
542                 if (!aper_base) { 
543                         aper_size = new_aper_size;
544                         aper_base = new_aper_base;
545                 } 
546                 if (aper_size != new_aper_size || aper_base != new_aper_base) 
547                         goto nommu;
548         }
549         if (!aper_base)
550                 goto nommu; 
551         info->aper_base = aper_base;
552         info->aper_size = aper_size>>20; 
553
554         gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32); 
555         gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size)); 
556         if (!gatt) 
557                 panic("Cannot allocate GATT table"); 
558         memset(gatt, 0, gatt_size); 
559         agp_gatt_table = gatt;
560         
561         for_all_nb(dev) { 
562                 u32 ctl; 
563                 u32 gatt_reg; 
564
565                 gatt_reg = __pa(gatt) >> 12; 
566                 gatt_reg <<= 4; 
567                 pci_write_config_dword(dev, 0x98, gatt_reg);
568                 pci_read_config_dword(dev, 0x90, &ctl); 
569
570                 ctl |= 1;
571                 ctl &= ~((1<<4) | (1<<5));
572
573                 pci_write_config_dword(dev, 0x90, ctl); 
574         }
575         flush_gart(NULL); 
576         
577         printk("PCI-DMA: aperture base @ %x size %u KB\n",aper_base, aper_size>>10); 
578         return 0;
579
580  nommu:
581         /* Should not happen anymore */
582         printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
583                KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction.\n");
584         return -1; 
585
586
587 extern int agp_amd64_init(void);
588
589 static struct dma_mapping_ops gart_dma_ops = {
590         .mapping_error = NULL,
591         .map_single = gart_map_single,
592         .map_simple = gart_map_simple,
593         .unmap_single = gart_unmap_single,
594         .sync_single_for_cpu = NULL,
595         .sync_single_for_device = NULL,
596         .sync_single_range_for_cpu = NULL,
597         .sync_single_range_for_device = NULL,
598         .sync_sg_for_cpu = NULL,
599         .sync_sg_for_device = NULL,
600         .map_sg = gart_map_sg,
601         .unmap_sg = gart_unmap_sg,
602 };
603
604 static int __init pci_iommu_init(void)
605
606         struct agp_kern_info info;
607         unsigned long aper_size;
608         unsigned long iommu_start;
609         struct pci_dev *dev;
610         unsigned long scratch;
611         long i;
612
613 #ifndef CONFIG_AGP_AMD64
614         no_agp = 1; 
615 #else
616         /* Makefile puts PCI initialization via subsys_initcall first. */
617         /* Add other K8 AGP bridge drivers here */
618         no_agp = no_agp || 
619                 (agp_amd64_init() < 0) || 
620                 (agp_copy_info(agp_bridge, &info) < 0);
621 #endif  
622
623         if (swiotlb)
624                 return -1; 
625
626         if (no_iommu ||
627             (!force_iommu && end_pfn <= MAX_DMA32_PFN) ||
628             !iommu_aperture ||
629             (no_agp && init_k8_gatt(&info) < 0)) {
630                 printk(KERN_INFO "PCI-DMA: Disabling IOMMU.\n");
631                 if (end_pfn > MAX_DMA32_PFN) {
632                         printk(KERN_ERR "WARNING more than 4GB of memory "
633                                         "but IOMMU not compiled in.\n"
634                                KERN_ERR "WARNING 32bit PCI may malfunction.\n"
635                                KERN_ERR "You might want to enable "
636                                         "CONFIG_GART_IOMMU\n");
637                 }
638                 return -1;
639         }
640
641         printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n");
642         aper_size = info.aper_size * 1024 * 1024;       
643         iommu_size = check_iommu_size(info.aper_base, aper_size); 
644         iommu_pages = iommu_size >> PAGE_SHIFT; 
645
646         iommu_gart_bitmap = (void*)__get_free_pages(GFP_KERNEL, 
647                                                     get_order(iommu_pages/8)); 
648         if (!iommu_gart_bitmap) 
649                 panic("Cannot allocate iommu bitmap\n"); 
650         memset(iommu_gart_bitmap, 0, iommu_pages/8);
651
652 #ifdef CONFIG_IOMMU_LEAK
653         if (leak_trace) { 
654                 iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL, 
655                                   get_order(iommu_pages*sizeof(void *)));
656                 if (iommu_leak_tab) 
657                         memset(iommu_leak_tab, 0, iommu_pages * 8); 
658                 else
659                         printk("PCI-DMA: Cannot allocate leak trace area\n"); 
660         } 
661 #endif
662
663         /* 
664          * Out of IOMMU space handling.
665          * Reserve some invalid pages at the beginning of the GART. 
666          */ 
667         set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES); 
668
669         agp_memory_reserved = iommu_size;       
670         printk(KERN_INFO
671                "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
672                iommu_size>>20); 
673
674         iommu_start = aper_size - iommu_size;   
675         iommu_bus_base = info.aper_base + iommu_start; 
676         bad_dma_address = iommu_bus_base;
677         iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
678
679         /* 
680          * Unmap the IOMMU part of the GART. The alias of the page is
681          * always mapped with cache enabled and there is no full cache
682          * coherency across the GART remapping. The unmapping avoids
683          * automatic prefetches from the CPU allocating cache lines in
684          * there. All CPU accesses are done via the direct mapping to
685          * the backing memory. The GART address is only used by PCI
686          * devices. 
687          */
688         clear_kernel_mapping((unsigned long)__va(iommu_bus_base), iommu_size);
689
690         /* 
691          * Try to workaround a bug (thanks to BenH) 
692          * Set unmapped entries to a scratch page instead of 0. 
693          * Any prefetches that hit unmapped entries won't get an bus abort
694          * then.
695          */
696         scratch = get_zeroed_page(GFP_KERNEL); 
697         if (!scratch) 
698                 panic("Cannot allocate iommu scratch page");
699         gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
700         for (i = EMERGENCY_PAGES; i < iommu_pages; i++) 
701                 iommu_gatt_base[i] = gart_unmapped_entry;
702
703         for_all_nb(dev) {
704                 u32 flag; 
705                 int cpu = PCI_SLOT(dev->devfn) - 24;
706                 if (cpu >= MAX_NB)
707                         continue;
708                 northbridges[cpu] = dev;
709                 pci_read_config_dword(dev, 0x9c, &flag); /* cache flush word */
710                 northbridge_flush_word[cpu] = flag; 
711         }
712                      
713         flush_gart(NULL);
714
715         dma_ops = &gart_dma_ops;
716
717         return 0;
718
719
720 /* Must execute after PCI subsystem */
721 fs_initcall(pci_iommu_init);
722
723 void gart_parse_options(char *p)
724 {
725         int arg;
726
727 #ifdef CONFIG_IOMMU_LEAK
728         if (!strncmp(p,"leak",4)) {
729                 leak_trace = 1;
730                 p += 4;
731                 if (*p == '=') ++p;
732                 if (isdigit(*p) && get_option(&p, &arg))
733                         iommu_leak_pages = arg;
734         }
735 #endif
736         if (isdigit(*p) && get_option(&p, &arg))
737                 iommu_size = arg;
738         if (!strncmp(p, "fullflush",8))
739                 iommu_fullflush = 1;
740         if (!strncmp(p, "nofullflush",11))
741                 iommu_fullflush = 0;
742         if (!strncmp(p,"noagp",5))
743                 no_agp = 1;
744         if (!strncmp(p, "noaperture",10))
745                 fix_aperture = 0;
746         /* duplicated from pci-dma.c */
747         if (!strncmp(p,"force",5))
748                 iommu_aperture_allowed = 1;
749         if (!strncmp(p,"allowed",7))
750                 iommu_aperture_allowed = 1;
751         if (!strncmp(p, "memaper", 7)) {
752                 fallback_aper_force = 1;
753                 p += 7;
754                 if (*p == '=') {
755                         ++p;
756                         if (get_option(&p, &arg))
757                                 fallback_aper_order = arg;
758                 }
759         }
760 }