Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / sparc64 / kernel / pci_iommu.c
index 94a5298..2e7f142 100644 (file)
@@ -8,6 +8,7 @@
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/mm.h>
+#include <linux/delay.h>
 
 #include <asm/pbm.h>
 
@@ -48,12 +49,6 @@ static void __iommu_flushall(struct pci_iommu *iommu)
 
        /* Ensure completion of previous PIO writes. */
        (void) pci_iommu_read(iommu->write_complete_reg);
-
-       /* Now update everyone's flush point. */
-       for (entry = 0; entry < PBM_NCLUSTERS; entry++) {
-               iommu->alloc_info[entry].flush =
-                       iommu->alloc_info[entry].next;
-       }
 }
 
 #define IOPTE_CONSISTENT(CTX) \
@@ -79,132 +74,156 @@ static void inline iopte_make_dummy(struct pci_iommu *iommu, iopte_t *iopte)
        iopte_val(*iopte) = val;
 }
 
-void pci_iommu_table_init(struct pci_iommu *iommu, int tsbsize)
+/* Based largely upon the ppc64 iommu allocator.  */
+static long pci_arena_alloc(struct pci_iommu *iommu, unsigned long npages)
 {
-       int i;
+       struct pci_iommu_arena *arena = &iommu->arena;
+       unsigned long n, i, start, end, limit;
+       int pass;
+
+       limit = arena->limit;
+       start = arena->hint;
+       pass = 0;
+
+again:
+       n = find_next_zero_bit(arena->map, limit, start);
+       end = n + npages;
+       if (unlikely(end >= limit)) {
+               if (likely(pass < 1)) {
+                       limit = start;
+                       start = 0;
+                       __iommu_flushall(iommu);
+                       pass++;
+                       goto again;
+               } else {
+                       /* Scanned the whole thing, give up. */
+                       return -1;
+               }
+       }
 
-       tsbsize /= sizeof(iopte_t);
+       for (i = n; i < end; i++) {
+               if (test_bit(i, arena->map)) {
+                       start = i + 1;
+                       goto again;
+               }
+       }
 
-       for (i = 0; i < tsbsize; i++)
-               iopte_make_dummy(iommu, &iommu->page_table[i]);
+       for (i = n; i < end; i++)
+               __set_bit(i, arena->map);
+
+       arena->hint = end;
+
+       return n;
 }
 
-static iopte_t *alloc_streaming_cluster(struct pci_iommu *iommu, unsigned long npages)
+static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
 {
-       iopte_t *iopte, *limit, *first;
-       unsigned long cnum, ent, flush_point;
-
-       cnum = 0;
-       while ((1UL << cnum) < npages)
-               cnum++;
-       iopte  = (iommu->page_table +
-                 (cnum << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
-
-       if (cnum == 0)
-               limit = (iommu->page_table +
-                        iommu->lowest_consistent_map);
-       else
-               limit = (iopte +
-                        (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
+       unsigned long i;
 
-       iopte += ((ent = iommu->alloc_info[cnum].next) << cnum);
-       flush_point = iommu->alloc_info[cnum].flush;
-       
-       first = iopte;
-       for (;;) {
-               if (IOPTE_IS_DUMMY(iommu, iopte)) {
-                       if ((iopte + (1 << cnum)) >= limit)
-                               ent = 0;
-                       else
-                               ent = ent + 1;
-                       iommu->alloc_info[cnum].next = ent;
-                       if (ent == flush_point)
-                               __iommu_flushall(iommu);
-                       break;
-               }
-               iopte += (1 << cnum);
-               ent++;
-               if (iopte >= limit) {
-                       iopte = (iommu->page_table +
-                                (cnum <<
-                                 (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
-                       ent = 0;
-               }
-               if (ent == flush_point)
-                       __iommu_flushall(iommu);
-               if (iopte == first)
-                       goto bad;
+       for (i = base; i < (base + npages); i++)
+               __clear_bit(i, arena->map);
+}
+
+void pci_iommu_table_init(struct pci_iommu *iommu, int tsbsize, u32 dma_offset, u32 dma_addr_mask)
+{
+       unsigned long i, tsbbase, order, sz, num_tsb_entries;
+
+       num_tsb_entries = tsbsize / sizeof(iopte_t);
+
+       /* Setup initial software IOMMU state. */
+       spin_lock_init(&iommu->lock);
+       iommu->ctx_lowest_free = 1;
+       iommu->page_table_map_base = dma_offset;
+       iommu->dma_addr_mask = dma_addr_mask;
+
+       /* Allocate and initialize the free area map.  */
+       sz = num_tsb_entries / 8;
+       sz = (sz + 7UL) & ~7UL;
+       iommu->arena.map = kzalloc(sz, GFP_KERNEL);
+       if (!iommu->arena.map) {
+               prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
+               prom_halt();
        }
+       iommu->arena.limit = num_tsb_entries;
 
-       /* I've got your streaming cluster right here buddy boy... */
-       return iopte;
+       /* Allocate and initialize the dummy page which we
+        * set inactive IO PTEs to point to.
+        */
+       iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
+       if (!iommu->dummy_page) {
+               prom_printf("PCI_IOMMU: Error, gfp(dummy_page) failed.\n");
+               prom_halt();
+       }
+       memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
+       iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
+
+       /* Now allocate and setup the IOMMU page table itself.  */
+       order = get_order(tsbsize);
+       tsbbase = __get_free_pages(GFP_KERNEL, order);
+       if (!tsbbase) {
+               prom_printf("PCI_IOMMU: Error, gfp(tsb) failed.\n");
+               prom_halt();
+       }
+       iommu->page_table = (iopte_t *)tsbbase;
 
-bad:
-       printk(KERN_EMERG "pci_iommu: alloc_streaming_cluster of npages(%ld) failed!\n",
-              npages);
-       return NULL;
+       for (i = 0; i < num_tsb_entries; i++)
+               iopte_make_dummy(iommu, &iommu->page_table[i]);
 }
 
-static void free_streaming_cluster(struct pci_iommu *iommu, dma_addr_t base,
-                                  unsigned long npages, unsigned long ctx)
+static inline iopte_t *alloc_npages(struct pci_iommu *iommu, unsigned long npages)
 {
-       unsigned long cnum, ent;
-
-       cnum = 0;
-       while ((1UL << cnum) < npages)
-               cnum++;
+       long entry;
 
-       ent = (base << (32 - IO_PAGE_SHIFT + PBM_LOGCLUSTERS - iommu->page_table_sz_bits))
-               >> (32 + PBM_LOGCLUSTERS + cnum - iommu->page_table_sz_bits);
+       entry = pci_arena_alloc(iommu, npages);
+       if (unlikely(entry < 0))
+               return NULL;
 
-       /* If the global flush might not have caught this entry,
-        * adjust the flush point such that we will flush before
-        * ever trying to reuse it.
-        */
-#define between(X,Y,Z) (((Z) - (Y)) >= ((X) - (Y)))
-       if (between(ent, iommu->alloc_info[cnum].next, iommu->alloc_info[cnum].flush))
-               iommu->alloc_info[cnum].flush = ent;
-#undef between
+       return iommu->page_table + entry;
 }
 
-/* We allocate consistent mappings from the end of cluster zero. */
-static iopte_t *alloc_consistent_cluster(struct pci_iommu *iommu, unsigned long npages)
+static inline void free_npages(struct pci_iommu *iommu, dma_addr_t base, unsigned long npages)
 {
-       iopte_t *iopte;
+       pci_arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
+}
 
-       iopte = iommu->page_table + (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS));
-       while (iopte > iommu->page_table) {
-               iopte--;
-               if (IOPTE_IS_DUMMY(iommu, iopte)) {
-                       unsigned long tmp = npages;
+static int iommu_alloc_ctx(struct pci_iommu *iommu)
+{
+       int lowest = iommu->ctx_lowest_free;
+       int sz = IOMMU_NUM_CTXS - lowest;
+       int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
+
+       if (unlikely(n == sz)) {
+               n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
+               if (unlikely(n == lowest)) {
+                       printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
+                       n = 0;
+               }
+       }
+       if (n)
+               __set_bit(n, iommu->ctx_bitmap);
 
-                       while (--tmp) {
-                               iopte--;
-                               if (!IOPTE_IS_DUMMY(iommu, iopte))
-                                       break;
-                       }
-                       if (tmp == 0) {
-                               u32 entry = (iopte - iommu->page_table);
+       return n;
+}
 
-                               if (entry < iommu->lowest_consistent_map)
-                                       iommu->lowest_consistent_map = entry;
-                               return iopte;
-                       }
-               }
+static inline void iommu_free_ctx(struct pci_iommu *iommu, int ctx)
+{
+       if (likely(ctx)) {
+               __clear_bit(ctx, iommu->ctx_bitmap);
+               if (ctx < iommu->ctx_lowest_free)
+                       iommu->ctx_lowest_free = ctx;
        }
-       return NULL;
 }
 
 /* Allocate and map kernel buffer of size SIZE using consistent mode
  * DMA for PCI device PDEV.  Return non-NULL cpu-side address if
  * successful and set *DMA_ADDRP to the PCI side dma address.
  */
-void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
+static void *pci_4u_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
        iopte_t *iopte;
-       unsigned long flags, order, first_page, ctx;
+       unsigned long flags, order, first_page;
        void *ret;
        int npages;
 
@@ -213,7 +232,7 @@ void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_ad
        if (order >= 10)
                return NULL;
 
-       first_page = __get_free_pages(GFP_ATOMIC, order);
+       first_page = __get_free_pages(gfp, order);
        if (first_page == 0UL)
                return NULL;
        memset((char *)first_page, 0, PAGE_SIZE << order);
@@ -222,9 +241,10 @@ void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_ad
        iommu = pcp->pbm->iommu;
 
        spin_lock_irqsave(&iommu->lock, flags);
-       iopte = alloc_consistent_cluster(iommu, size >> IO_PAGE_SHIFT);
-       if (iopte == NULL) {
-               spin_unlock_irqrestore(&iommu->lock, flags);
+       iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
+       spin_unlock_irqrestore(&iommu->lock, flags);
+
+       if (unlikely(iopte == NULL)) {
                free_pages(first_page, order);
                return NULL;
        }
@@ -233,41 +253,25 @@ void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_ad
                      ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
        ret = (void *) first_page;
        npages = size >> IO_PAGE_SHIFT;
-       ctx = 0;
-       if (iommu->iommu_ctxflush)
-               ctx = iommu->iommu_cur_ctx++;
        first_page = __pa(first_page);
        while (npages--) {
-               iopte_val(*iopte) = (IOPTE_CONSISTENT(ctx) |
+               iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
                                     IOPTE_WRITE |
                                     (first_page & IOPTE_PAGE));
                iopte++;
                first_page += IO_PAGE_SIZE;
        }
 
-       {
-               int i;
-               u32 daddr = *dma_addrp;
-
-               npages = size >> IO_PAGE_SHIFT;
-               for (i = 0; i < npages; i++) {
-                       pci_iommu_write(iommu->iommu_flush, daddr);
-                       daddr += IO_PAGE_SIZE;
-               }
-       }
-
-       spin_unlock_irqrestore(&iommu->lock, flags);
-
        return ret;
 }
 
 /* Free and unmap a consistent DMA translation. */
-void pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
+static void pci_4u_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
        iopte_t *iopte;
-       unsigned long flags, order, npages, i, ctx;
+       unsigned long flags, order, npages;
 
        npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
        pcp = pdev->sysdata;
@@ -277,44 +281,7 @@ void pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_
 
        spin_lock_irqsave(&iommu->lock, flags);
 
-       if ((iopte - iommu->page_table) ==
-           iommu->lowest_consistent_map) {
-               iopte_t *walk = iopte + npages;
-               iopte_t *limit;
-
-               limit = (iommu->page_table +
-                        (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
-               while (walk < limit) {
-                       if (!IOPTE_IS_DUMMY(iommu, walk))
-                               break;
-                       walk++;
-               }
-               iommu->lowest_consistent_map =
-                       (walk - iommu->page_table);
-       }
-
-       /* Data for consistent mappings cannot enter the streaming
-        * buffers, so we only need to update the TSB.  We flush
-        * the IOMMU here as well to prevent conflicts with the
-        * streaming mapping deferred tlb flush scheme.
-        */
-
-       ctx = 0;
-       if (iommu->iommu_ctxflush)
-               ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
-
-       for (i = 0; i < npages; i++, iopte++)
-               iopte_make_dummy(iommu, iopte);
-
-       if (iommu->iommu_ctxflush) {
-               pci_iommu_write(iommu->iommu_ctxflush, ctx);
-       } else {
-               for (i = 0; i < npages; i++) {
-                       u32 daddr = dvma + (i << IO_PAGE_SHIFT);
-
-                       pci_iommu_write(iommu->iommu_flush, daddr);
-               }
-       }
+       free_npages(iommu, dvma - iommu->page_table_map_base, npages);
 
        spin_unlock_irqrestore(&iommu->lock, flags);
 
@@ -326,7 +293,7 @@ void pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_
 /* Map a single buffer at PTR of SZ bytes for PCI DMA
  * in streaming mode.
  */
-dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
+static dma_addr_t pci_4u_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
@@ -341,25 +308,27 @@ dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direct
        iommu = pcp->pbm->iommu;
        strbuf = &pcp->pbm->stc;
 
-       if (direction == PCI_DMA_NONE)
-               BUG();
+       if (unlikely(direction == PCI_DMA_NONE))
+               goto bad_no_ctx;
 
        oaddr = (unsigned long)ptr;
        npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
        npages >>= IO_PAGE_SHIFT;
 
        spin_lock_irqsave(&iommu->lock, flags);
+       base = alloc_npages(iommu, npages);
+       ctx = 0;
+       if (iommu->iommu_ctxflush)
+               ctx = iommu_alloc_ctx(iommu);
+       spin_unlock_irqrestore(&iommu->lock, flags);
 
-       base = alloc_streaming_cluster(iommu, npages);
-       if (base == NULL)
+       if (unlikely(!base))
                goto bad;
+
        bus_addr = (iommu->page_table_map_base +
                    ((base - iommu->page_table) << IO_PAGE_SHIFT));
        ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
        base_paddr = __pa(oaddr & IO_PAGE_MASK);
-       ctx = 0;
-       if (iommu->iommu_ctxflush)
-               ctx = iommu->iommu_cur_ctx++;
        if (strbuf->strbuf_enabled)
                iopte_protection = IOPTE_STREAMING(ctx);
        else
@@ -370,26 +339,94 @@ dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direct
        for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
                iopte_val(*base) = iopte_protection | base_paddr;
 
-       spin_unlock_irqrestore(&iommu->lock, flags);
-
        return ret;
 
 bad:
-       spin_unlock_irqrestore(&iommu->lock, flags);
+       iommu_free_ctx(iommu, ctx);
+bad_no_ctx:
+       if (printk_ratelimit())
+               WARN_ON(1);
        return PCI_DMA_ERROR_CODE;
 }
 
+static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction)
+{
+       int limit;
+
+       if (strbuf->strbuf_ctxflush &&
+           iommu->iommu_ctxflush) {
+               unsigned long matchreg, flushreg;
+               u64 val;
+
+               flushreg = strbuf->strbuf_ctxflush;
+               matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
+
+               pci_iommu_write(flushreg, ctx);
+               val = pci_iommu_read(matchreg);
+               val &= 0xffff;
+               if (!val)
+                       goto do_flush_sync;
+
+               while (val) {
+                       if (val & 0x1)
+                               pci_iommu_write(flushreg, ctx);
+                       val >>= 1;
+               }
+               val = pci_iommu_read(matchreg);
+               if (unlikely(val)) {
+                       printk(KERN_WARNING "pci_strbuf_flush: ctx flush "
+                              "timeout matchreg[%lx] ctx[%lx]\n",
+                              val, ctx);
+                       goto do_page_flush;
+               }
+       } else {
+               unsigned long i;
+
+       do_page_flush:
+               for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
+                       pci_iommu_write(strbuf->strbuf_pflush, vaddr);
+       }
+
+do_flush_sync:
+       /* If the device could not have possibly put dirty data into
+        * the streaming cache, no flush-flag synchronization needs
+        * to be performed.
+        */
+       if (direction == PCI_DMA_TODEVICE)
+               return;
+
+       PCI_STC_FLUSHFLAG_INIT(strbuf);
+       pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
+       (void) pci_iommu_read(iommu->write_complete_reg);
+
+       limit = 100000;
+       while (!PCI_STC_FLUSHFLAG_SET(strbuf)) {
+               limit--;
+               if (!limit)
+                       break;
+               udelay(1);
+               rmb();
+       }
+       if (!limit)
+               printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout "
+                      "vaddr[%08x] ctx[%lx] npages[%ld]\n",
+                      vaddr, ctx, npages);
+}
+
 /* Unmap a single streaming mode DMA translation. */
-void pci_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
+static void pci_4u_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
        struct pci_strbuf *strbuf;
        iopte_t *base;
-       unsigned long flags, npages, i, ctx;
+       unsigned long flags, npages, ctx, i;
 
-       if (direction == PCI_DMA_NONE)
-               BUG();
+       if (unlikely(direction == PCI_DMA_NONE)) {
+               if (printk_ratelimit())
+                       WARN_ON(1);
+               return;
+       }
 
        pcp = pdev->sysdata;
        iommu = pcp->pbm->iommu;
@@ -414,35 +451,17 @@ void pci_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int
                ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
 
        /* Step 1: Kick data out of streaming buffers if necessary. */
-       if (strbuf->strbuf_enabled) {
-               u32 vaddr = bus_addr;
-
-               PCI_STC_FLUSHFLAG_INIT(strbuf);
-               if (strbuf->strbuf_ctxflush &&
-                   iommu->iommu_ctxflush) {
-                       unsigned long matchreg, flushreg;
-
-                       flushreg = strbuf->strbuf_ctxflush;
-                       matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
-                       do {
-                               pci_iommu_write(flushreg, ctx);
-                       } while(((long)pci_iommu_read(matchreg)) < 0L);
-               } else {
-                       for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
-                               pci_iommu_write(strbuf->strbuf_pflush, vaddr);
-               }
+       if (strbuf->strbuf_enabled)
+               pci_strbuf_flush(strbuf, iommu, bus_addr, ctx,
+                                npages, direction);
 
-               pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
-               (void) pci_iommu_read(iommu->write_complete_reg);
-               while (!PCI_STC_FLUSHFLAG_SET(strbuf))
-                       membar("#LoadLoad");
-       }
+       /* Step 2: Clear out TSB entries. */
+       for (i = 0; i < npages; i++)
+               iopte_make_dummy(iommu, base + i);
 
-       /* Step 2: Clear out first TSB entry. */
-       iopte_make_dummy(iommu, base);
+       free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
 
-       free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base,
-                              npages, ctx);
+       iommu_free_ctx(iommu, ctx);
 
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
@@ -528,7 +547,7 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
  * When making changes here, inspect the assembly output. I was having
  * hard time to kepp this routine out of using stack slots for holding variables.
  */
-int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
+static int pci_4u_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
@@ -542,9 +561,11 @@ int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int
        /* Fast path single entry scatterlists. */
        if (nelems == 1) {
                sglist->dma_address =
-                       pci_map_single(pdev,
-                                      (page_address(sglist->page) + sglist->offset),
-                                      sglist->length, direction);
+                       pci_4u_map_single(pdev,
+                                         (page_address(sglist->page) + sglist->offset),
+                                         sglist->length, direction);
+               if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
+                       return 0;
                sglist->dma_length = sglist->length;
                return 1;
        }
@@ -553,21 +574,29 @@ int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int
        iommu = pcp->pbm->iommu;
        strbuf = &pcp->pbm->stc;
        
-       if (direction == PCI_DMA_NONE)
-               BUG();
+       if (unlikely(direction == PCI_DMA_NONE))
+               goto bad_no_ctx;
 
        /* Step 1: Prepare scatter list. */
 
        npages = prepare_sg(sglist, nelems);
 
-       /* Step 2: Allocate a cluster. */
+       /* Step 2: Allocate a cluster and context, if necessary. */
 
        spin_lock_irqsave(&iommu->lock, flags);
 
-       base = alloc_streaming_cluster(iommu, npages);
+       base = alloc_npages(iommu, npages);
+       ctx = 0;
+       if (iommu->iommu_ctxflush)
+               ctx = iommu_alloc_ctx(iommu);
+
+       spin_unlock_irqrestore(&iommu->lock, flags);
+
        if (base == NULL)
                goto bad;
-       dma_base = iommu->page_table_map_base + ((base - iommu->page_table) << IO_PAGE_SHIFT);
+
+       dma_base = iommu->page_table_map_base +
+               ((base - iommu->page_table) << IO_PAGE_SHIFT);
 
        /* Step 3: Normalize DMA addresses. */
        used = nelems;
@@ -580,34 +609,32 @@ int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int
        }
        used = nelems - used;
 
-       /* Step 4: Choose a context if necessary. */
-       ctx = 0;
-       if (iommu->iommu_ctxflush)
-               ctx = iommu->iommu_cur_ctx++;
-
-       /* Step 5: Create the mappings. */
+       /* Step 4: Create the mappings. */
        if (strbuf->strbuf_enabled)
                iopte_protection = IOPTE_STREAMING(ctx);
        else
                iopte_protection = IOPTE_CONSISTENT(ctx);
        if (direction != PCI_DMA_TODEVICE)
                iopte_protection |= IOPTE_WRITE;
-       fill_sg (base, sglist, used, nelems, iopte_protection);
+
+       fill_sg(base, sglist, used, nelems, iopte_protection);
+
 #ifdef VERIFY_SG
        verify_sglist(sglist, nelems, base, npages);
 #endif
 
-       spin_unlock_irqrestore(&iommu->lock, flags);
-
        return used;
 
 bad:
-       spin_unlock_irqrestore(&iommu->lock, flags);
-       return PCI_DMA_ERROR_CODE;
+       iommu_free_ctx(iommu, ctx);
+bad_no_ctx:
+       if (printk_ratelimit())
+               WARN_ON(1);
+       return 0;
 }
 
 /* Unmap a set of streaming mode DMA translations. */
-void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
+static void pci_4u_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
@@ -616,8 +643,10 @@ void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems,
        unsigned long flags, ctx, i, npages;
        u32 bus_addr;
 
-       if (direction == PCI_DMA_NONE)
-               BUG();
+       if (unlikely(direction == PCI_DMA_NONE)) {
+               if (printk_ratelimit())
+                       WARN_ON(1);
+       }
 
        pcp = pdev->sysdata;
        iommu = pcp->pbm->iommu;
@@ -629,7 +658,8 @@ void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems,
                if (sglist[i].dma_length == 0)
                        break;
        i--;
-       npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - bus_addr) >> IO_PAGE_SHIFT;
+       npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
+                 bus_addr) >> IO_PAGE_SHIFT;
 
        base = iommu->page_table +
                ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
@@ -647,35 +677,16 @@ void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems,
                ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
 
        /* Step 1: Kick data out of streaming buffers if necessary. */
-       if (strbuf->strbuf_enabled) {
-               u32 vaddr = (u32) bus_addr;
-
-               PCI_STC_FLUSHFLAG_INIT(strbuf);
-               if (strbuf->strbuf_ctxflush &&
-                   iommu->iommu_ctxflush) {
-                       unsigned long matchreg, flushreg;
-
-                       flushreg = strbuf->strbuf_ctxflush;
-                       matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
-                       do {
-                               pci_iommu_write(flushreg, ctx);
-                       } while(((long)pci_iommu_read(matchreg)) < 0L);
-               } else {
-                       for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
-                               pci_iommu_write(strbuf->strbuf_pflush, vaddr);
-               }
+       if (strbuf->strbuf_enabled)
+               pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
 
-               pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
-               (void) pci_iommu_read(iommu->write_complete_reg);
-               while (!PCI_STC_FLUSHFLAG_SET(strbuf))
-                       membar("#LoadLoad");
-       }
+       /* Step 2: Clear out the TSB entries. */
+       for (i = 0; i < npages; i++)
+               iopte_make_dummy(iommu, base + i);
 
-       /* Step 2: Clear out first TSB entry. */
-       iopte_make_dummy(iommu, base);
+       free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
 
-       free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base,
-                              npages, ctx);
+       iommu_free_ctx(iommu, ctx);
 
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
@@ -683,7 +694,7 @@ void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems,
 /* Make physical memory consistent for a single
  * streaming mode DMA translation after a transfer.
  */
-void pci_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
+static void pci_4u_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
@@ -715,28 +726,7 @@ void pci_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size
        }
 
        /* Step 2: Kick data out of streaming buffers. */
-       PCI_STC_FLUSHFLAG_INIT(strbuf);
-       if (iommu->iommu_ctxflush &&
-           strbuf->strbuf_ctxflush) {
-               unsigned long matchreg, flushreg;
-
-               flushreg = strbuf->strbuf_ctxflush;
-               matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
-               do {
-                       pci_iommu_write(flushreg, ctx);
-               } while(((long)pci_iommu_read(matchreg)) < 0L);
-       } else {
-               unsigned long i;
-
-               for (i = 0; i < npages; i++, bus_addr += IO_PAGE_SIZE)
-                       pci_iommu_write(strbuf->strbuf_pflush, bus_addr);
-       }
-
-       /* Step 3: Perform flush synchronization sequence. */
-       pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
-       (void) pci_iommu_read(iommu->write_complete_reg);
-       while (!PCI_STC_FLUSHFLAG_SET(strbuf))
-               membar("#LoadLoad");
+       pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
 
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
@@ -744,12 +734,13 @@ void pci_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size
 /* Make physical memory consistent for a set of streaming
  * mode DMA translations after a transfer.
  */
-void pci_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
+static void pci_4u_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
 {
        struct pcidev_cookie *pcp;
        struct pci_iommu *iommu;
        struct pci_strbuf *strbuf;
-       unsigned long flags, ctx;
+       unsigned long flags, ctx, npages, i;
+       u32 bus_addr;
 
        pcp = pdev->sysdata;
        iommu = pcp->pbm->iommu;
@@ -772,40 +763,29 @@ void pci_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, i
        }
 
        /* Step 2: Kick data out of streaming buffers. */
-       PCI_STC_FLUSHFLAG_INIT(strbuf);
-       if (iommu->iommu_ctxflush &&
-           strbuf->strbuf_ctxflush) {
-               unsigned long matchreg, flushreg;
-
-               flushreg = strbuf->strbuf_ctxflush;
-               matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
-               do {
-                       pci_iommu_write(flushreg, ctx);
-               } while (((long)pci_iommu_read(matchreg)) < 0L);
-       } else {
-               unsigned long i, npages;
-               u32 bus_addr;
-
-               bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
-
-               for(i = 1; i < nelems; i++)
-                       if (!sglist[i].dma_length)
-                               break;
-               i--;
-               npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - bus_addr) >> IO_PAGE_SHIFT;
-               for (i = 0; i < npages; i++, bus_addr += IO_PAGE_SIZE)
-                       pci_iommu_write(strbuf->strbuf_pflush, bus_addr);
-       }
-
-       /* Step 3: Perform flush synchronization sequence. */
-       pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
-       (void) pci_iommu_read(iommu->write_complete_reg);
-       while (!PCI_STC_FLUSHFLAG_SET(strbuf))
-               membar("#LoadLoad");
+       bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
+       for(i = 1; i < nelems; i++)
+               if (!sglist[i].dma_length)
+                       break;
+       i--;
+       npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length)
+                 - bus_addr) >> IO_PAGE_SHIFT;
+       pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
 
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
 
+struct pci_iommu_ops pci_sun4u_iommu_ops = {
+       .alloc_consistent               = pci_4u_alloc_consistent,
+       .free_consistent                = pci_4u_free_consistent,
+       .map_single                     = pci_4u_map_single,
+       .unmap_single                   = pci_4u_unmap_single,
+       .map_sg                         = pci_4u_map_sg,
+       .unmap_sg                       = pci_4u_unmap_sg,
+       .dma_sync_single_for_cpu        = pci_4u_dma_sync_single_for_cpu,
+       .dma_sync_sg_for_cpu            = pci_4u_dma_sync_sg_for_cpu,
+};
+
 static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
 {
        struct pci_dev *ali_isa_bridge;
@@ -814,7 +794,7 @@ static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
        /* ALI sound chips generate 31-bits of DMA, a special register
         * determines what bit 31 is emitted as.
         */
-       ali_isa_bridge = pci_find_device(PCI_VENDOR_ID_AL,
+       ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
                                         PCI_DEVICE_ID_AL_M1533,
                                         NULL);
 
@@ -824,6 +804,7 @@ static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
        else
                val &= ~0x01;
        pci_write_config_byte(ali_isa_bridge, 0x7e, val);
+       pci_dev_put(ali_isa_bridge);
 }
 
 int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)