ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <asm/atomic.h>
26 #include <asm/io.h>
27 #include <asm/mtrr.h>
28 #include <asm/bitops.h>
29 #include <asm/pgtable.h>
30 #include <asm/proto.h>
31 #include <asm/cacheflush.h>
32 #include <asm/kdebug.h>
33 #include <asm/proto.h>
34
35 #ifdef CONFIG_PREEMPT
36 #define preempt_atomic() in_atomic()
37 #else
38 #define preempt_atomic() 1
39 #endif
40
41 dma_addr_t bad_dma_address;
42
43 unsigned long iommu_bus_base;   /* GART remapping area (physical) */
44 static unsigned long iommu_size;        /* size of remapping area bytes */
45 static unsigned long iommu_pages;       /* .. and in pages */
46
47 u32 *iommu_gatt_base;           /* Remapping table */
48
49 int no_iommu; 
50 static int no_agp; 
51 #ifdef CONFIG_IOMMU_DEBUG
52 int panic_on_overflow = 1; 
53 int force_iommu = 1;
54 #else
55 int panic_on_overflow = 0;
56 int force_iommu = 0;
57 #endif
58 int iommu_merge = 0; 
59 int iommu_sac_force = 0; 
60
61 /* If this is disabled the IOMMU will use an optimized flushing strategy
62    of only flushing when an mapping is reused. With it true the GART is flushed 
63    for every mapping. Problem is that doing the lazy flush seems to trigger
64    bugs with some popular PCI cards, in particular 3ware (but has been also
65    also seen with Qlogic at least). */
66 int iommu_fullflush = 1;
67
68 #define MAX_NB 8
69
70 /* Allocation bitmap for the remapping area */ 
71 static spinlock_t iommu_bitmap_lock = SPIN_LOCK_UNLOCKED;
72 static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */
73
74 #define GPTE_VALID    1
75 #define GPTE_COHERENT 2
76 #define GPTE_ENCODE(x) \
77         (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
78 #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
79
80 #define to_pages(addr,size) \
81         (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
82
83 #define for_all_nb(dev) \
84         dev = NULL;     \
85         while ((dev = pci_find_device(PCI_VENDOR_ID_AMD, 0x1103, dev))!=NULL)\
86              if (dev->bus->number == 0 &&                                    \
87                     (PCI_SLOT(dev->devfn) >= 24) && (PCI_SLOT(dev->devfn) <= 31))
88
89 static struct pci_dev *northbridges[MAX_NB];
90 static u32 northbridge_flush_word[MAX_NB];
91
92 #define EMERGENCY_PAGES 32 /* = 128KB */ 
93
94 #ifdef CONFIG_AGP
95 #define AGPEXTERN extern
96 #else
97 #define AGPEXTERN
98 #endif
99
100 /* backdoor interface to AGP driver */
101 AGPEXTERN int agp_memory_reserved;
102 AGPEXTERN __u32 *agp_gatt_table;
103
104 static unsigned long next_bit;  /* protected by iommu_bitmap_lock */
105 static int need_flush;          /* global flush state. set for each gart wrap */
106 static dma_addr_t pci_map_area(struct pci_dev *dev, unsigned long phys_mem, 
107                                size_t size, int dir);
108
109 static unsigned long alloc_iommu(int size) 
110 {       
111         unsigned long offset, flags;
112
113         spin_lock_irqsave(&iommu_bitmap_lock, flags);   
114         offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size);
115         if (offset == -1) {
116                 need_flush = 1;
117                 offset = find_next_zero_string(iommu_gart_bitmap,0,next_bit,size);
118         }
119         if (offset != -1) { 
120                 set_bit_string(iommu_gart_bitmap, offset, size); 
121                 next_bit = offset+size; 
122                 if (next_bit >= iommu_pages) { 
123                         next_bit = 0;
124                         need_flush = 1;
125                 } 
126         } 
127         if (iommu_fullflush)
128                 need_flush = 1;
129         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);      
130         return offset;
131
132
133 static void free_iommu(unsigned long offset, int size)
134
135         unsigned long flags;
136         if (size == 1) { 
137                 clear_bit(offset, iommu_gart_bitmap); 
138                 return;
139         }
140         spin_lock_irqsave(&iommu_bitmap_lock, flags);
141         __clear_bit_string(iommu_gart_bitmap, offset, size);
142         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
143
144
145 /* 
146  * Use global flush state to avoid races with multiple flushers.
147  */
148 static void flush_gart(struct pci_dev *dev)
149
150         unsigned long flags;
151         int bus = dev ? dev->bus->number : -1;
152         cpumask_const_t bus_cpumask = pcibus_to_cpumask(bus);
153         int flushed = 0;
154         int i;
155
156         spin_lock_irqsave(&iommu_bitmap_lock, flags);
157         if (need_flush) { 
158                 for (i = 0; i < MAX_NB; i++) {
159                         u32 w;
160                         if (!northbridges[i]) 
161                                 continue;
162                         if (bus >= 0 && !(cpu_isset_const(i, bus_cpumask)))
163                                 continue;
164                         pci_write_config_dword(northbridges[i], 0x9c, 
165                                                northbridge_flush_word[i] | 1); 
166                         /* Make sure the hardware actually executed the flush. */
167                         do { 
168                                 pci_read_config_dword(northbridges[i], 0x9c, &w);
169                         } while (w & 1);
170                         flushed++;
171                 } 
172                 if (!flushed) 
173                         printk("nothing to flush? %d\n", bus);
174                 need_flush = 0;
175         } 
176         spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
177
178
179 /* 
180  * Allocate memory for a consistent mapping.
181  */
182 void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
183                            dma_addr_t *dma_handle)
184 {
185         void *memory;
186         int gfp = preempt_atomic() ? GFP_ATOMIC : GFP_KERNEL; 
187         unsigned long dma_mask = 0;
188         u64 bus;
189
190         if (hwdev) 
191                 dma_mask = hwdev->dev.coherent_dma_mask;
192         if (dma_mask == 0) 
193                 dma_mask = 0xffffffff; 
194
195         /* Kludge to make it bug-to-bug compatible with i386. i386
196            uses the normal dma_mask for alloc_consistent. */
197         if (hwdev)
198         dma_mask &= hwdev->dma_mask;
199
200  again:
201         memory = (void *)__get_free_pages(gfp, get_order(size));
202         if (memory == NULL)
203                 return NULL; 
204
205         {
206                 int high, mmu;
207                 bus = virt_to_bus(memory);
208                 high = (bus + size) >= dma_mask;
209                 mmu = high;
210                 if (force_iommu && !(gfp & GFP_DMA)) 
211                         mmu = 1;
212                 if (no_iommu || dma_mask < 0xffffffffUL) { 
213                         if (high) {
214                                 if (!(gfp & GFP_DMA)) { 
215                                         gfp |= GFP_DMA; 
216                                         goto again;
217                                 }
218                                 goto free;
219                         }
220                         mmu = 0; 
221                 }       
222                 memset(memory, 0, size); 
223                 if (!mmu) { 
224                         *dma_handle = virt_to_bus(memory);
225                         return memory;
226                 }
227         } 
228
229         *dma_handle = pci_map_area(hwdev, bus, size, PCI_DMA_BIDIRECTIONAL);
230         if (*dma_handle == bad_dma_address)
231                 goto error; 
232         flush_gart(hwdev);      
233         return memory; 
234         
235 error:
236         if (panic_on_overflow)
237                 panic("pci_alloc_consistent: overflow %lu bytes\n", size); 
238 free:
239         free_pages((unsigned long)memory, get_order(size)); 
240         return NULL; 
241 }
242
243 /* 
244  * Unmap consistent memory.
245  * The caller must ensure that the device has finished accessing the mapping.
246  */
247 void pci_free_consistent(struct pci_dev *hwdev, size_t size,
248                          void *vaddr, dma_addr_t bus)
249 {
250         pci_unmap_single(hwdev, bus, size, 0);
251         free_pages((unsigned long)vaddr, get_order(size));              
252 }
253
254 #ifdef CONFIG_IOMMU_LEAK
255
256 #define SET_LEAK(x) if (iommu_leak_tab) \
257                         iommu_leak_tab[x] = __builtin_return_address(0);
258 #define CLEAR_LEAK(x) if (iommu_leak_tab) \
259                         iommu_leak_tab[x] = 0;
260
261 /* Debugging aid for drivers that don't free their IOMMU tables */
262 static void **iommu_leak_tab; 
263 static int leak_trace;
264 int iommu_leak_pages = 20; 
265 void dump_leak(void)
266 {
267         int i;
268         static int dump; 
269         if (dump || !iommu_leak_tab) return;
270         dump = 1;
271         show_stack(NULL,NULL);
272         /* Very crude. dump some from the end of the table too */ 
273         printk("Dumping %d pages from end of IOMMU:\n", iommu_leak_pages); 
274         for (i = 0; i < iommu_leak_pages; i+=2) {
275                 printk("%lu: ", iommu_pages-i);
276                 printk_address((unsigned long) iommu_leak_tab[iommu_pages-i]);
277                 printk("%c", (i+1)%2 == 0 ? '\n' : ' '); 
278         } 
279         printk("\n");
280 }
281 #else
282 #define SET_LEAK(x)
283 #define CLEAR_LEAK(x)
284 #endif
285
286 static void iommu_full(struct pci_dev *dev, size_t size, int dir)
287 {
288         /* 
289          * Ran out of IOMMU space for this operation. This is very bad.
290          * Unfortunately the drivers cannot handle this operation properly.
291          * Return some non mapped prereserved space in the aperture and 
292          * let the Northbridge deal with it. This will result in garbage
293          * in the IO operation. When the size exceeds the prereserved space
294          * memory corruption will occur or random memory will be DMAed 
295          * out. Hopefully no network devices use single mappings that big.
296          */ 
297         
298         printk(KERN_ERR 
299   "PCI-DMA: Out of IOMMU space for %lu bytes at device %s[%s]\n",
300                size, dev ? pci_pretty_name(dev) : "", dev ? dev->slot_name : "?");
301
302         if (size > PAGE_SIZE*EMERGENCY_PAGES) {
303                 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
304                         panic("PCI-DMA: Memory will be corrupted\n");
305                 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL) 
306                         panic("PCI-DMA: Random memory will be DMAed\n"); 
307         } 
308
309 #ifdef CONFIG_IOMMU_LEAK
310         dump_leak(); 
311 #endif
312
313
314 static inline int need_iommu(struct pci_dev *dev, unsigned long addr, size_t size)
315
316         u64 mask = dev ? dev->dma_mask : 0xffffffff;
317         int high = addr + size >= mask;
318         int mmu = high;
319         if (force_iommu) 
320                 mmu = 1; 
321         if (no_iommu) { 
322                 if (high) 
323                         panic("PCI-DMA: high address but no IOMMU.\n"); 
324                 mmu = 0; 
325         }       
326         return mmu; 
327 }
328
329 static inline int nonforced_iommu(struct pci_dev *dev, unsigned long addr, size_t size)
330
331         u64 mask = dev ? dev->dma_mask : 0xffffffff;
332         int high = addr + size >= mask;
333         int mmu = high;
334         if (no_iommu) { 
335                 if (high) 
336                         panic("PCI-DMA: high address but no IOMMU.\n"); 
337                 mmu = 0; 
338         }       
339         return mmu; 
340 }
341
342 /* Map a single continuous physical area into the IOMMU.
343  * Caller needs to check if the iommu is needed and flush.
344  */
345 static dma_addr_t pci_map_area(struct pci_dev *dev, unsigned long phys_mem, 
346                                 size_t size, int dir)
347
348         unsigned long npages = to_pages(phys_mem, size);
349         unsigned long iommu_page = alloc_iommu(npages);
350         int i;
351         if (iommu_page == -1) {
352                 if (!nonforced_iommu(dev, phys_mem, size))
353                         return phys_mem; 
354                 if (panic_on_overflow)
355                         panic("pci_map_area overflow %lu bytes\n", size);
356                 iommu_full(dev, size, dir);
357                 return bad_dma_address;
358         }
359
360         for (i = 0; i < npages; i++) {
361                 iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
362                 SET_LEAK(iommu_page + i);
363                 phys_mem += PAGE_SIZE;
364         }
365         return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
366 }
367
368 /* Map a single area into the IOMMU */
369 dma_addr_t pci_map_single(struct pci_dev *dev, void *addr, size_t size, int dir)
370
371         unsigned long phys_mem, bus;
372
373         BUG_ON(dir == PCI_DMA_NONE);
374
375 #ifdef CONFIG_SWIOTLB
376         if (swiotlb)
377                 return swiotlb_map_single(&dev->dev,addr,size,dir);
378 #endif
379
380         phys_mem = virt_to_phys(addr); 
381         if (!need_iommu(dev, phys_mem, size))
382                 return phys_mem; 
383
384         bus = pci_map_area(dev, phys_mem, size, dir);
385         flush_gart(dev); 
386         return bus; 
387
388
389 /* Fallback for pci_map_sg in case of overflow */ 
390 static int pci_map_sg_nonforce(struct pci_dev *dev, struct scatterlist *sg,
391                                int nents, int dir)
392 {
393         int i;
394
395 #ifdef CONFIG_IOMMU_DEBUG
396         printk(KERN_DEBUG "pci_map_sg overflow\n");
397 #endif
398
399         for (i = 0; i < nents; i++ ) {
400                 struct scatterlist *s = &sg[i];
401                 unsigned long addr = page_to_phys(s->page) + s->offset; 
402                 if (nonforced_iommu(dev, addr, s->length)) { 
403                         addr = pci_map_area(dev, addr, s->length, dir); 
404                         if (addr == bad_dma_address) { 
405                                 if (i > 0) 
406                                         pci_unmap_sg(dev, sg, i, dir); 
407                                 nents = 0; 
408                                 sg[0].dma_length = 0;
409                                 break;
410                         }
411                 }
412                 s->dma_address = addr;
413                 s->dma_length = s->length;
414         }
415         flush_gart(dev);
416         return nents;
417 }
418
419 /* Map multiple scatterlist entries continuous into the first. */
420 static int __pci_map_cont(struct scatterlist *sg, int start, int stopat, 
421                       struct scatterlist *sout, unsigned long pages)
422 {
423         unsigned long iommu_start = alloc_iommu(pages);
424         unsigned long iommu_page = iommu_start; 
425         int i;
426
427         if (iommu_start == -1)
428                 return -1;
429         
430         for (i = start; i < stopat; i++) {
431                 struct scatterlist *s = &sg[i];
432                 unsigned long pages, addr;
433                 unsigned long phys_addr = s->dma_address;
434                 
435                 BUG_ON(i > start && s->offset);
436                 if (i == start) {
437                         *sout = *s; 
438                         sout->dma_address = iommu_bus_base;
439                         sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
440                         sout->dma_length = s->length;
441                 } else { 
442                         sout->dma_length += s->length; 
443                 }
444
445                 addr = phys_addr;
446                 pages = to_pages(s->offset, s->length); 
447                 while (pages--) { 
448                         iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr); 
449                         SET_LEAK(iommu_page);
450                         addr += PAGE_SIZE;
451                         iommu_page++;
452         } 
453         } 
454         BUG_ON(iommu_page - iommu_start != pages);      
455         return 0;
456 }
457
458 static inline int pci_map_cont(struct scatterlist *sg, int start, int stopat, 
459                       struct scatterlist *sout,
460                       unsigned long pages, int need)
461 {
462         if (!need) { 
463                 BUG_ON(stopat - start != 1);
464                 *sout = sg[start]; 
465                 sout->dma_length = sg[start].length; 
466                 return 0;
467         } 
468         return __pci_map_cont(sg, start, stopat, sout, pages);
469 }
470                 
471 /*
472  * DMA map all entries in a scatterlist.
473  * Merge chunks that have page aligned sizes into a continuous mapping. 
474                  */
475 int pci_map_sg(struct pci_dev *dev, struct scatterlist *sg, int nents, int dir)
476 {
477         int i;
478         int out;
479         int start;
480         unsigned long pages = 0;
481         int need = 0, nextneed;
482
483         BUG_ON(dir == PCI_DMA_NONE);
484         if (nents == 0) 
485                 return 0;
486
487 #ifdef CONFIG_SWIOTLB
488         if (swiotlb)
489                 return swiotlb_map_sg(&dev->dev,sg,nents,dir);
490 #endif
491
492         out = 0;
493         start = 0;
494         for (i = 0; i < nents; i++) {
495                 struct scatterlist *s = &sg[i];
496                 dma_addr_t addr = page_to_phys(s->page) + s->offset;
497                 s->dma_address = addr;
498                 BUG_ON(s->length == 0); 
499
500                 nextneed = need_iommu(dev, addr, s->length); 
501
502                 /* Handle the previous not yet processed entries */
503                 if (i > start) {
504                         struct scatterlist *ps = &sg[i-1];
505                         /* Can only merge when the last chunk ends on a page 
506                            boundary and the new one doesn't have an offset. */
507                         if (!iommu_merge || !nextneed || !need || s->offset ||
508                             (ps->offset + ps->length) % PAGE_SIZE) { 
509                                 if (pci_map_cont(sg, start, i, sg+out, pages, 
510                                                  need) < 0)
511                                         goto error;
512                                 out++;
513                                 pages = 0;
514                                 start = i;      
515                         }
516         }
517
518                 need = nextneed;
519                 pages += to_pages(s->offset, s->length);
520         }
521         if (pci_map_cont(sg, start, i, sg+out, pages, need) < 0)
522                 goto error;
523         out++;
524         flush_gart(dev);
525         if (out < nents) 
526                 sg[out].dma_length = 0; 
527         return out;
528
529 error:
530         flush_gart(NULL);
531         pci_unmap_sg(dev, sg, nents, dir);
532         /* When it was forced try again unforced */
533         if (force_iommu) 
534                 return pci_map_sg_nonforce(dev, sg, nents, dir);
535         if (panic_on_overflow)
536                 panic("pci_map_sg: overflow on %lu pages\n", pages); 
537         iommu_full(dev, pages << PAGE_SHIFT, dir);
538         for (i = 0; i < nents; i++)
539                 sg[i].dma_address = bad_dma_address;
540         return 0;
541
542
543 /*
544  * Free a PCI mapping.
545  */ 
546 void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_addr,
547                       size_t size, int direction)
548 {
549         unsigned long iommu_page; 
550         int npages;
551         int i;
552
553 #ifdef CONFIG_SWIOTLB
554         if (swiotlb) {
555                 swiotlb_unmap_single(&hwdev->dev,dma_addr,size,direction);
556                 return;
557         }
558 #endif
559
560         if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE || 
561             dma_addr >= iommu_bus_base + iommu_size)
562                 return;
563         iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;   
564         npages = to_pages(dma_addr, size);
565         for (i = 0; i < npages; i++) { 
566                 iommu_gatt_base[iommu_page + i] = 0; 
567                 CLEAR_LEAK(iommu_page + i);
568         }
569         free_iommu(iommu_page, npages);
570 }
571
572 /* 
573  * Wrapper for pci_unmap_single working with scatterlists.
574  */ 
575 void pci_unmap_sg(struct pci_dev *dev, struct scatterlist *sg, int nents, 
576                   int dir)
577 {
578         int i;
579         for (i = 0; i < nents; i++) { 
580                 struct scatterlist *s = &sg[i];
581                 if (!s->dma_length || !s->length) 
582                         break;
583                 pci_unmap_single(dev, s->dma_address, s->dma_length, dir);
584         }
585 }
586
587 int pci_dma_supported(struct pci_dev *dev, u64 mask)
588 {
589         /* Copied from i386. Doesn't make much sense, because it will 
590            only work for pci_alloc_consistent. 
591            The caller just has to use GFP_DMA in this case. */
592         if (mask < 0x00ffffff)
593                 return 0;
594
595         /* Tell the device to use SAC when IOMMU force is on. 
596            This allows the driver to use cheaper accesses in some cases.
597
598            Problem with this is that if we overflow the IOMMU area
599            and return DAC as fallback address the device may not handle it correctly.
600            
601            As a special case some controllers have a 39bit address mode 
602            that is as efficient as 32bit (aic79xx). Don't force SAC for these.
603            Assume all masks <= 40 bits are of this type. Normally this doesn't
604            make any difference, but gives more gentle handling of IOMMU overflow. */
605         if (iommu_sac_force && (mask >= 0xffffffffffULL)) { 
606                 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->slot_name,mask);
607                 return 0; 
608         }
609
610         return 1;
611
612
613 EXPORT_SYMBOL(pci_unmap_sg);
614 EXPORT_SYMBOL(pci_map_sg);
615 EXPORT_SYMBOL(pci_map_single);
616 EXPORT_SYMBOL(pci_unmap_single);
617 EXPORT_SYMBOL(pci_dma_supported);
618 EXPORT_SYMBOL(no_iommu);
619 EXPORT_SYMBOL(force_iommu); 
620 EXPORT_SYMBOL(bad_dma_address);
621 EXPORT_SYMBOL(iommu_merge);
622
623 static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
624
625         unsigned long a; 
626         if (!iommu_size) { 
627                 iommu_size = aper_size; 
628                 if (!no_agp) 
629                         iommu_size /= 2; 
630         } 
631
632         a = aper + iommu_size; 
633         iommu_size -= round_up(a, LARGE_PAGE_SIZE) - a;
634
635         if (iommu_size < 64*1024*1024) 
636                 printk(KERN_WARNING
637   "PCI-DMA: Warning: Small IOMMU %luMB. Consider increasing the AGP aperture in BIOS\n",iommu_size>>20); 
638         
639         return iommu_size;
640
641
642 static __init unsigned read_aperture(struct pci_dev *dev, u32 *size) 
643
644         unsigned aper_size = 0, aper_base_32;
645         u64 aper_base;
646         unsigned aper_order;
647
648         pci_read_config_dword(dev, 0x94, &aper_base_32); 
649         pci_read_config_dword(dev, 0x90, &aper_order);
650         aper_order = (aper_order >> 1) & 7;     
651
652         aper_base = aper_base_32 & 0x7fff; 
653         aper_base <<= 25;
654
655         aper_size = (32 * 1024 * 1024) << aper_order; 
656         if (aper_base + aper_size >= 0xffffffff || !aper_size)
657                 aper_base = 0;
658
659         *size = aper_size;
660         return aper_base;
661
662
663 /* 
664  * Private Northbridge GATT initialization in case we cannot use the
665  * AGP driver for some reason.  
666  */
667 static __init int init_k8_gatt(struct agp_kern_info *info)
668
669         struct pci_dev *dev;
670         void *gatt;
671         unsigned aper_base, new_aper_base;
672         unsigned aper_size, gatt_size, new_aper_size;
673         
674         aper_size = aper_base = info->aper_size = 0;
675         for_all_nb(dev) { 
676                 new_aper_base = read_aperture(dev, &new_aper_size); 
677                 if (!new_aper_base) 
678                         goto nommu; 
679                 
680                 if (!aper_base) { 
681                         aper_size = new_aper_size;
682                         aper_base = new_aper_base;
683                 } 
684                 if (aper_size != new_aper_size || aper_base != new_aper_base) 
685                         goto nommu;
686         }
687         if (!aper_base)
688                 goto nommu; 
689         info->aper_base = aper_base;
690         info->aper_size = aper_size>>20; 
691
692         gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32); 
693         gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size)); 
694         if (!gatt) 
695                 panic("Cannot allocate GATT table"); 
696         memset(gatt, 0, gatt_size); 
697         agp_gatt_table = gatt;
698         
699         for_all_nb(dev) { 
700                 u32 ctl; 
701                 u32 gatt_reg; 
702
703                 gatt_reg = __pa(gatt) >> 12; 
704                 gatt_reg <<= 4; 
705                 pci_write_config_dword(dev, 0x98, gatt_reg);
706                 pci_read_config_dword(dev, 0x90, &ctl); 
707
708                 ctl |= 1;
709                 ctl &= ~((1<<4) | (1<<5));
710
711                 pci_write_config_dword(dev, 0x90, ctl); 
712         }
713         flush_gart(NULL); 
714         
715         printk("PCI-DMA: aperture base @ %x size %u KB\n",aper_base, aper_size>>10); 
716         return 0;
717
718  nommu:
719         /* Should not happen anymore */
720         printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
721                KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction."); 
722         return -1; 
723
724
725 extern int agp_amd64_init(void);
726
727 static int __init pci_iommu_init(void)
728
729         struct agp_kern_info info;
730         unsigned long aper_size;
731         unsigned long iommu_start;
732         struct pci_dev *dev;
733                 
734
735 #ifndef CONFIG_AGP_AMD64
736         no_agp = 1; 
737 #else
738         /* Makefile puts PCI initialization via subsys_initcall first. */
739         /* Add other K8 AGP bridge drivers here */
740         no_agp = no_agp || 
741                 (agp_amd64_init() < 0) || 
742                 (agp_copy_info(&info) < 0); 
743 #endif  
744
745         if (swiotlb) { 
746                 no_iommu = 1;
747                 printk(KERN_INFO "PCI-DMA: Using software bounce buffering for  IO (SWIOTLB)\n"); 
748                 return -1; 
749         } 
750         
751         if (no_iommu || (!force_iommu && end_pfn < 0xffffffff>>PAGE_SHIFT) || 
752             !iommu_aperture) {
753                 printk(KERN_INFO "PCI-DMA: Disabling IOMMU.\n"); 
754                 no_iommu = 1;
755                 return -1;
756         }
757
758         if (no_agp) { 
759                 int err = -1;
760                 printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
761                 no_agp = 1;
762                 if (force_iommu || end_pfn >= 0xffffffff>>PAGE_SHIFT)
763                         err = init_k8_gatt(&info);
764                 if (err < 0) { 
765                         printk(KERN_INFO "PCI-DMA: Disabling IOMMU.\n"); 
766                         no_iommu = 1;
767                         return -1;
768                 }
769         } 
770         
771         aper_size = info.aper_size * 1024 * 1024;       
772         iommu_size = check_iommu_size(info.aper_base, aper_size); 
773         iommu_pages = iommu_size >> PAGE_SHIFT; 
774
775         iommu_gart_bitmap = (void*)__get_free_pages(GFP_KERNEL, 
776                                                     get_order(iommu_pages/8)); 
777         if (!iommu_gart_bitmap) 
778                 panic("Cannot allocate iommu bitmap\n"); 
779         memset(iommu_gart_bitmap, 0, iommu_pages/8);
780
781 #ifdef CONFIG_IOMMU_LEAK
782         if (leak_trace) { 
783                 iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL, 
784                                   get_order(iommu_pages*sizeof(void *)));
785                 if (iommu_leak_tab) 
786                         memset(iommu_leak_tab, 0, iommu_pages * 8); 
787                 else
788                         printk("PCI-DMA: Cannot allocate leak trace area\n"); 
789         } 
790 #endif
791
792         /* 
793          * Out of IOMMU space handling.
794          * Reserve some invalid pages at the beginning of the GART. 
795          */ 
796         set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES); 
797
798         agp_memory_reserved = iommu_size;       
799         printk(KERN_INFO
800                "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
801                iommu_size>>20); 
802
803         iommu_start = aper_size - iommu_size;   
804         iommu_bus_base = info.aper_base + iommu_start; 
805         bad_dma_address = iommu_bus_base;
806         iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
807
808         /* 
809          * Unmap the IOMMU part of the GART. The alias of the page is
810          * always mapped with cache enabled and there is no full cache
811          * coherency across the GART remapping. The unmapping avoids
812          * automatic prefetches from the CPU allocating cache lines in
813          * there. All CPU accesses are done via the direct mapping to
814          * the backing memory. The GART address is only used by PCI
815          * devices. 
816          */
817         clear_kernel_mapping((unsigned long)__va(iommu_bus_base), iommu_size);
818
819         for_all_nb(dev) {
820                 u32 flag; 
821                 int cpu = PCI_SLOT(dev->devfn) - 24;
822                 if (cpu >= MAX_NB)
823                         continue;
824                 northbridges[cpu] = dev;
825                 pci_read_config_dword(dev, 0x9c, &flag); /* cache flush word */
826                 northbridge_flush_word[cpu] = flag; 
827         }
828                      
829         flush_gart(NULL);
830
831         return 0;
832
833
834 /* Must execute after PCI subsystem */
835 fs_initcall(pci_iommu_init);
836
837 /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
838          [,forcesac][,fullflush][,nomerge]
839    size  set size of iommu (in bytes) 
840    noagp don't initialize the AGP driver and use full aperture.
841    off   don't use the IOMMU
842    leak  turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
843    memaper[=order] allocate an own aperture over RAM with size 32MB^order.  
844    noforce don't force IOMMU usage. Default.
845    force  Force IOMMU.
846    merge  Do SG merging. Implies force (experimental)  
847    nomerge Don't do SG merging.
848    forcesac For SAC mode for masks <40bits  (experimental)
849    fullflush Flush IOMMU on each allocation (default) 
850    nofullflush Don't use IOMMU fullflush
851    allowed  overwrite iommu off workarounds for specific chipsets.
852    soft  Use software bounce buffering (default for Intel machines)
853 */
854 __init int iommu_setup(char *opt) 
855
856     int arg;
857     char *p = opt;
858     
859     for (;;) { 
860             if (!memcmp(p,"noagp", 5))
861                     no_agp = 1;
862             if (!memcmp(p,"off", 3))
863                     no_iommu = 1;
864             if (!memcmp(p,"force", 5)) {
865                     force_iommu = 1;
866                     iommu_aperture_allowed = 1;
867             }
868             if (!memcmp(p,"allowed",7))
869                     iommu_aperture_allowed = 1;
870             if (!memcmp(p,"noforce", 7)) { 
871                     iommu_merge = 0;
872                     force_iommu = 0;
873             }
874             if (!memcmp(p, "memaper", 7)) { 
875                     fallback_aper_force = 1; 
876                     p += 7; 
877                     if (*p == '=' && get_option(&p, &arg))
878                             fallback_aper_order = arg;
879             } 
880             if (!memcmp(p, "panic", 5))
881                     panic_on_overflow = 1;
882             if (!memcmp(p, "nopanic", 7))
883                     panic_on_overflow = 0;          
884             if (!memcmp(p, "merge", 5)) { 
885                     iommu_merge = 1;
886                     force_iommu = 1; 
887             }
888             if (!memcmp(p, "nomerge", 7))
889                     iommu_merge = 0;
890             if (!memcmp(p, "forcesac", 8))
891                     iommu_sac_force = 1;
892             if (!memcmp(p, "fullflush", 9))
893                     iommu_fullflush = 1;
894             if (!memcmp(p, "nofullflush", 11))
895                     iommu_fullflush = 0;
896             if (!memcmp(p, "soft", 4))
897                     swiotlb = 1;
898 #ifdef CONFIG_IOMMU_LEAK
899             if (!memcmp(p,"leak", 4)) { 
900                     leak_trace = 1;
901                     p += 4; 
902                     if (*p == '=') ++p;
903                     if (isdigit(*p) && get_option(&p, &arg))
904                             iommu_leak_pages = arg;
905             } else
906 #endif
907             if (isdigit(*p) && get_option(&p, &arg)) 
908                     iommu_size = arg;
909             do {
910                     if (*p == ' ' || *p == 0) 
911                             return 0; 
912             } while (*p++ != ','); 
913     }
914     return 1;
915