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