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