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