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 / sbus.c
index 549e978..c49a577 100644 (file)
@@ -19,6 +19,7 @@
 #include <asm/cache.h>
 #include <asm/dma.h>
 #include <asm/irq.h>
+#include <asm/prom.h>
 #include <asm/starfire.h>
 
 #include "iommu_common.h"
  *
  * On SYSIO, using an 8K page size we have 1GB of SBUS
  * DMA space mapped.  We divide this space into equally
- * sized clusters.  Currently we allow clusters up to a
- * size of 1MB.  If anything begins to generate DMA
- * mapping requests larger than this we will need to
- * increase things a bit.
+ * sized clusters. We allocate a DMA mapping from the
+ * cluster that matches the order of the allocation, or
+ * if the order is greater than the number of clusters,
+ * we try to allocate from the last cluster.
  */
 
 #define NCLUSTERS      8UL
@@ -117,29 +118,57 @@ static void iommu_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages
 
 #define STRBUF_TAG_VALID       0x02UL
 
-static void strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages)
+static void sbus_strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages, int direction)
 {
-       iommu->strbuf_flushflag = 0UL;
-       while (npages--)
-               upa_writeq(base + (npages << IO_PAGE_SHIFT),
+       unsigned long n;
+       int limit;
+
+       n = npages;
+       while (n--)
+               upa_writeq(base + (n << IO_PAGE_SHIFT),
                           iommu->strbuf_regs + STRBUF_PFLUSH);
 
+       /* If the device could not have possibly put dirty data into
+        * the streaming cache, no flush-flag synchronization needs
+        * to be performed.
+        */
+       if (direction == SBUS_DMA_TODEVICE)
+               return;
+
+       iommu->strbuf_flushflag = 0UL;
+
        /* Whoopee cushion! */
        upa_writeq(__pa(&iommu->strbuf_flushflag),
                   iommu->strbuf_regs + STRBUF_FSYNC);
        upa_readq(iommu->sbus_control_reg);
-       while (iommu->strbuf_flushflag == 0UL)
-               membar("#LoadLoad");
+
+       limit = 100000;
+       while (iommu->strbuf_flushflag == 0UL) {
+               limit--;
+               if (!limit)
+                       break;
+               udelay(1);
+               rmb();
+       }
+       if (!limit)
+               printk(KERN_WARNING "sbus_strbuf_flush: flushflag timeout "
+                      "vaddr[%08x] npages[%ld]\n",
+                      base, npages);
 }
 
 static iopte_t *alloc_streaming_cluster(struct sbus_iommu *iommu, unsigned long npages)
 {
-       iopte_t *iopte, *limit, *first;
-       unsigned long cnum, ent, flush_point;
+       iopte_t *iopte, *limit, *first, *cluster;
+       unsigned long cnum, ent, nent, flush_point, found;
 
        cnum = 0;
+       nent = 1;
        while ((1UL << cnum) < npages)
                cnum++;
+       if(cnum >= NCLUSTERS) {
+               nent = 1UL << (cnum - NCLUSTERS);
+               cnum = NCLUSTERS - 1;
+       }
        iopte  = iommu->page_table + (cnum * CLUSTER_NPAGES);
 
        if (cnum == 0)
@@ -152,22 +181,31 @@ static iopte_t *alloc_streaming_cluster(struct sbus_iommu *iommu, unsigned long
        flush_point = iommu->alloc_info[cnum].flush;
 
        first = iopte;
+       cluster = NULL;
+       found = 0;
        for (;;) {
                if (iopte_val(*iopte) == 0UL) {
-                       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;
+                       found++;
+                       if (!cluster)
+                               cluster = iopte;
+               } else {
+                       /* Used cluster in the way */
+                       cluster = NULL;
+                       found = 0;
                }
+
+               if (found == nent)
+                       break;
+
                iopte += (1 << cnum);
                ent++;
                if (iopte >= limit) {
                        iopte = (iommu->page_table + (cnum * CLUSTER_NPAGES));
                        ent = 0;
+
+                       /* Multiple cluster allocations must not wrap */
+                       cluster = NULL;
+                       found = 0;
                }
                if (ent == flush_point)
                        __iommu_flushall(iommu);
@@ -175,8 +213,19 @@ static iopte_t *alloc_streaming_cluster(struct sbus_iommu *iommu, unsigned long
                        goto bad;
        }
 
+       /* ent/iopte points to the last cluster entry we're going to use,
+        * so save our place for the next allocation.
+        */
+       if ((iopte + (1 << cnum)) >= limit)
+               ent = 0;
+       else
+               ent = ent + 1;
+       iommu->alloc_info[cnum].next = ent;
+       if (ent == flush_point)
+               __iommu_flushall(iommu);
+
        /* I've got your streaming cluster right here buddy boy... */
-       return iopte;
+       return cluster;
 
 bad:
        printk(KERN_EMERG "sbus: alloc_streaming_cluster of npages(%ld) failed!\n",
@@ -186,15 +235,23 @@ bad:
 
 static void free_streaming_cluster(struct sbus_iommu *iommu, u32 base, unsigned long npages)
 {
-       unsigned long cnum, ent;
+       unsigned long cnum, ent, nent;
        iopte_t *iopte;
 
        cnum = 0;
+       nent = 1;
        while ((1UL << cnum) < npages)
                cnum++;
+       if(cnum >= NCLUSTERS) {
+               nent = 1UL << (cnum - NCLUSTERS);
+               cnum = NCLUSTERS - 1;
+       }
        ent = (base & CLUSTER_MASK) >> (IO_PAGE_SHIFT + cnum);
        iopte = iommu->page_table + ((base - MAP_BASE) >> IO_PAGE_SHIFT);
-       iopte_val(*iopte) = 0UL;
+       do {
+               iopte_val(*iopte) = 0UL;
+               iopte += 1 << cnum;
+       } while(--nent);
 
        /* If the global flush might not have caught this entry,
         * adjust the flush point such that we will flush before
@@ -271,7 +328,7 @@ void *sbus_alloc_consistent(struct sbus_dev *sdev, size_t size, dma_addr_t *dvma
        order = get_order(size);
        if (order >= 10)
                return NULL;
-       first_page = __get_free_pages(GFP_KERNEL, order);
+       first_page = __get_free_pages(GFP_KERNEL|__GFP_COMP, order);
        if (first_page == 0UL)
                return NULL;
        memset((char *)first_page, 0, PAGE_SIZE << order);
@@ -373,7 +430,7 @@ void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t dma_addr, size_t size,
 
        spin_lock_irqsave(&iommu->lock, flags);
        free_streaming_cluster(iommu, dma_base, size >> IO_PAGE_SHIFT);
-       strbuf_flush(iommu, dma_base, size >> IO_PAGE_SHIFT);
+       sbus_strbuf_flush(iommu, dma_base, size >> IO_PAGE_SHIFT, direction);
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
 
@@ -536,7 +593,7 @@ void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int
        iommu = sdev->bus->iommu;
        spin_lock_irqsave(&iommu->lock, flags);
        free_streaming_cluster(iommu, dvma_base, size >> IO_PAGE_SHIFT);
-       strbuf_flush(iommu, dvma_base, size >> IO_PAGE_SHIFT);
+       sbus_strbuf_flush(iommu, dvma_base, size >> IO_PAGE_SHIFT, direction);
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
 
@@ -548,7 +605,7 @@ void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t base, size_t
        size = (IO_PAGE_ALIGN(base + size) - (base & IO_PAGE_MASK));
 
        spin_lock_irqsave(&iommu->lock, flags);
-       strbuf_flush(iommu, base & IO_PAGE_MASK, size >> IO_PAGE_SHIFT);
+       sbus_strbuf_flush(iommu, base & IO_PAGE_MASK, size >> IO_PAGE_SHIFT, direction);
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
 
@@ -572,7 +629,7 @@ void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int
        size = IO_PAGE_ALIGN(sg[i].dma_address + sg[i].dma_length) - base;
 
        spin_lock_irqsave(&iommu->lock, flags);
-       strbuf_flush(iommu, base, size >> IO_PAGE_SHIFT);
+       sbus_strbuf_flush(iommu, base, size >> IO_PAGE_SHIFT, direction);
        spin_unlock_irqrestore(&iommu->lock, flags);
 }
 
@@ -635,36 +692,6 @@ void sbus_set_sbus64(struct sbus_dev *sdev, int bursts)
        upa_writeq(val, cfg_reg);
 }
 
-/* SBUS SYSIO INO number to Sparc PIL level. */
-static unsigned char sysio_ino_to_pil[] = {
-       0, 4, 4, 7, 5, 7, 8, 9,         /* SBUS slot 0 */
-       0, 4, 4, 7, 5, 7, 8, 9,         /* SBUS slot 1 */
-       0, 4, 4, 7, 5, 7, 8, 9,         /* SBUS slot 2 */
-       0, 4, 4, 7, 5, 7, 8, 9,         /* SBUS slot 3 */
-       4, /* Onboard SCSI */
-       5, /* Onboard Ethernet */
-/*XXX*/        8, /* Onboard BPP */
-       0, /* Bogon */
-       13, /* Audio */
-/*XXX*/15, /* PowerFail */
-       0, /* Bogon */
-       0, /* Bogon */
-       12, /* Zilog Serial Channels (incl. Keyboard/Mouse lines) */
-       11, /* Floppy */
-       0, /* Spare Hardware (bogon for now) */
-       0, /* Keyboard (bogon for now) */
-       0, /* Mouse (bogon for now) */
-       0, /* Serial (bogon for now) */
-     0, 0, /* Bogon, Bogon */
-       10, /* Timer 0 */
-       11, /* Timer 1 */
-     0, 0, /* Bogon, Bogon */
-       15, /* Uncorrectable SBUS Error */
-       15, /* Correctable SBUS Error */
-       15, /* SBUS Error */
-/*XXX*/ 0, /* Power Management (bogon for now) */
-};
-
 /* INO number to IMAP register offset for SYSIO external IRQ's.
  * This should conform to both Sunfire/Wildfire server and Fusion
  * desktop designs.
@@ -734,7 +761,7 @@ static unsigned long sysio_irq_offsets[] = {
 
 #undef bogon
 
-#define NUM_SYSIO_OFFSETS (sizeof(sysio_irq_offsets) / sizeof(sysio_irq_offsets[0]))
+#define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
 
 /* Convert Interrupt Mapping register pointer to associated
  * Interrupt Clear register pointer, SYSIO specific version.
@@ -756,21 +783,12 @@ unsigned int sbus_build_irq(void *buscookie, unsigned int ino)
        struct sbus_iommu *iommu = sbus->iommu;
        unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL;
        unsigned long imap, iclr;
-       int pil, sbus_level = 0;
-
-       pil = sysio_ino_to_pil[ino];
-       if (!pil) {
-               printk("sbus_irq_build: Bad SYSIO INO[%x]\n", ino);
-               panic("Bad SYSIO IRQ translations...");
-       }
-
-       if (PIL_RESERVED(pil))
-               BUG();
+       int sbus_level = 0;
 
        imap = sysio_irq_offsets[ino];
        if (imap == ((unsigned long)-1)) {
-               prom_printf("get_irq_translations: Bad SYSIO INO[%x] cpu[%d]\n",
-                           ino, pil);
+               prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
+                           ino);
                prom_halt();
        }
        imap += reg_base;
@@ -804,7 +822,7 @@ unsigned int sbus_build_irq(void *buscookie, unsigned int ino)
 
                iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
        }
-       return build_irq(pil, sbus_level, iclr, imap);
+       return build_irq(sbus_level, iclr, imap);
 }
 
 /* Error interrupt handling. */
@@ -1047,7 +1065,7 @@ static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
 
        irq = sbus_build_irq(sbus, SYSIO_UE_INO);
        if (request_irq(irq, sysio_ue_handler,
-                       SA_SHIRQ, "SYSIO UE", sbus) < 0) {
+                       IRQF_SHARED, "SYSIO UE", sbus) < 0) {
                prom_printf("SYSIO[%x]: Cannot register UE interrupt.\n",
                            sbus->portid);
                prom_halt();
@@ -1055,7 +1073,7 @@ static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
 
        irq = sbus_build_irq(sbus, SYSIO_CE_INO);
        if (request_irq(irq, sysio_ce_handler,
-                       SA_SHIRQ, "SYSIO CE", sbus) < 0) {
+                       IRQF_SHARED, "SYSIO CE", sbus) < 0) {
                prom_printf("SYSIO[%x]: Cannot register CE interrupt.\n",
                            sbus->portid);
                prom_halt();
@@ -1063,7 +1081,7 @@ static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
 
        irq = sbus_build_irq(sbus, SYSIO_SBUSERR_INO);
        if (request_irq(irq, sysio_sbus_error_handler,
-                       SA_SHIRQ, "SYSIO SBUS Error", sbus) < 0) {
+                       IRQF_SHARED, "SYSIO SBUS Error", sbus) < 0) {
                prom_printf("SYSIO[%x]: Cannot register SBUS Error interrupt.\n",
                            sbus->portid);
                prom_halt();
@@ -1081,24 +1099,25 @@ static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
 }
 
 /* Boot time initialization. */
-void __init sbus_iommu_init(int prom_node, struct sbus_bus *sbus)
+static void __init sbus_iommu_init(int __node, struct sbus_bus *sbus)
 {
-       struct linux_prom64_registers rprop;
+       struct linux_prom64_registers *pr;
+       struct device_node *dp;
        struct sbus_iommu *iommu;
        unsigned long regs, tsb_base;
        u64 control;
-       int err, i;
+       int i;
+
+       dp = of_find_node_by_phandle(__node);
 
-       sbus->portid = prom_getintdefault(sbus->prom_node,
-                                         "upa-portid", -1);
+       sbus->portid = of_getintprop_default(dp, "upa-portid", -1);
 
-       err = prom_getproperty(prom_node, "reg",
-                              (char *)&rprop, sizeof(rprop));
-       if (err < 0) {
+       pr = of_get_property(dp, "reg", NULL);
+       if (!pr) {
                prom_printf("sbus_iommu_init: Cannot map SYSIO control registers.\n");
                prom_halt();
        }
-       regs = rprop.phys_addr;
+       regs = pr->phys_addr;
 
        iommu = kmalloc(sizeof(*iommu) + SMP_CACHE_BYTES, GFP_ATOMIC);
        if (iommu == NULL) {
@@ -1202,9 +1221,52 @@ void __init sbus_iommu_init(int prom_node, struct sbus_bus *sbus)
 
        /* Now some Xfire specific grot... */
        if (this_is_starfire)
-               sbus->starfire_cookie = starfire_hookup(sbus->portid);
-       else
-               sbus->starfire_cookie = NULL;
+               starfire_hookup(sbus->portid);
 
        sysio_register_error_handlers(sbus);
 }
+
+void sbus_fill_device_irq(struct sbus_dev *sdev)
+{
+       struct device_node *dp = of_find_node_by_phandle(sdev->prom_node);
+       struct linux_prom_irqs *irqs;
+
+       irqs = of_get_property(dp, "interrupts", NULL);
+       if (!irqs) {
+               sdev->irqs[0] = 0;
+               sdev->num_irqs = 0;
+       } else {
+               unsigned int pri = irqs[0].pri;
+
+               sdev->num_irqs = 1;
+               if (pri < 0x20)
+                       pri += sdev->slot * 8;
+
+               sdev->irqs[0] = sbus_build_irq(sdev->bus, pri);
+       }
+}
+
+void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
+{
+}
+
+void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
+{
+       sbus_iommu_init(dp->node, sbus);
+}
+
+void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
+{
+}
+
+int __init sbus_arch_preinit(void)
+{
+       return 0;
+}
+
+void __init sbus_arch_postinit(void)
+{
+       extern void firetruck_init(void);
+
+       firetruck_init();
+}