[SPARC64]: Don't be picky about virtual-dma values on sun4v.
[safe/jmp/linux-2.6] / arch / sparc64 / kernel / pci_sun4v.c
index b8846b2..62e4d04 100644 (file)
@@ -1,6 +1,6 @@
 /* pci_sun4v.c: SUN4V specific PCI controller support.
  *
- * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
+ * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
  */
 
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
 #include <linux/percpu.h>
+#include <linux/irq.h>
+#include <linux/msi.h>
+#include <linux/log2.h>
 
-#include <asm/pbm.h>
 #include <asm/iommu.h>
 #include <asm/irq.h>
 #include <asm/upa.h>
 #include <asm/pstate.h>
 #include <asm/oplib.h>
 #include <asm/hypervisor.h>
+#include <asm/prom.h>
 
 #include "pci_impl.h"
 #include "iommu_common.h"
 
 #include "pci_sun4v.h"
 
-#define PGLIST_NENTS   2048
+#define PGLIST_NENTS   (PAGE_SIZE / sizeof(u64))
 
-struct sun4v_pglist {
-       u64     pglist[PGLIST_NENTS];
+struct iommu_batch {
+       struct pci_dev  *pdev;          /* Device mapping is for.       */
+       unsigned long   prot;           /* IOMMU page protections       */
+       unsigned long   entry;          /* Index into IOTSB.            */
+       u64             *pglist;        /* List of physical pages       */
+       unsigned long   npages;         /* Number of pages in list.     */
 };
 
-static DEFINE_PER_CPU(struct sun4v_pglist, iommu_pglists);
+static DEFINE_PER_CPU(struct iommu_batch, pci_iommu_batch);
 
-static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages)
+/* Interrupts must be disabled.  */
+static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
+{
+       struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
+
+       p->pdev         = pdev;
+       p->prot         = prot;
+       p->entry        = entry;
+       p->npages       = 0;
+}
+
+/* Interrupts must be disabled.  */
+static long pci_iommu_batch_flush(struct iommu_batch *p)
+{
+       struct pci_pbm_info *pbm = p->pdev->dev.archdata.host_controller;
+       unsigned long devhandle = pbm->devhandle;
+       unsigned long prot = p->prot;
+       unsigned long entry = p->entry;
+       u64 *pglist = p->pglist;
+       unsigned long npages = p->npages;
+
+       while (npages != 0) {
+               long num;
+
+               num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
+                                         npages, prot, __pa(pglist));
+               if (unlikely(num < 0)) {
+                       if (printk_ratelimit())
+                               printk("pci_iommu_batch_flush: IOMMU map of "
+                                      "[%08lx:%08lx:%lx:%lx:%lx] failed with "
+                                      "status %ld\n",
+                                      devhandle, HV_PCI_TSBID(0, entry),
+                                      npages, prot, __pa(pglist), num);
+                       return -1;
+               }
+
+               entry += num;
+               npages -= num;
+               pglist += num;
+       }
+
+       p->entry = entry;
+       p->npages = 0;
+
+       return 0;
+}
+
+/* Interrupts must be disabled.  */
+static inline long pci_iommu_batch_add(u64 phys_page)
+{
+       struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
+
+       BUG_ON(p->npages >= PGLIST_NENTS);
+
+       p->pglist[p->npages++] = phys_page;
+       if (p->npages == PGLIST_NENTS)
+               return pci_iommu_batch_flush(p);
+
+       return 0;
+}
+
+/* Interrupts must be disabled.  */
+static inline long pci_iommu_batch_end(void)
+{
+       struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
+
+       BUG_ON(p->npages >= PGLIST_NENTS);
+
+       return pci_iommu_batch_flush(p);
+}
+
+static long pci_arena_alloc(struct iommu_arena *arena, unsigned long npages)
 {
        unsigned long n, i, start, end, limit;
        int pass;
@@ -71,7 +149,7 @@ again:
        return n;
 }
 
-static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
+static void pci_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
 {
        unsigned long i;
 
@@ -79,82 +157,82 @@ static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, un
                __clear_bit(i, arena->map);
 }
 
-static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
+static void *pci_4v_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;
-       unsigned long devhandle, flags, order, first_page, npages, n;
+       struct iommu *iommu;
+       unsigned long flags, order, first_page, npages, n;
        void *ret;
        long entry;
-       u64 *pglist;
-       int cpu;
 
        size = IO_PAGE_ALIGN(size);
        order = get_order(size);
-       if (order >= MAX_ORDER)
+       if (unlikely(order >= MAX_ORDER))
                return NULL;
 
        npages = size >> IO_PAGE_SHIFT;
-       if (npages > PGLIST_NENTS)
-               return NULL;
 
-       first_page = __get_free_pages(GFP_ATOMIC, order);
-       if (first_page == 0UL)
+       first_page = __get_free_pages(gfp, order);
+       if (unlikely(first_page == 0UL))
                return NULL;
+
        memset((char *)first_page, 0, PAGE_SIZE << order);
 
-       pcp = pdev->sysdata;
-       devhandle = pcp->pbm->devhandle;
-       iommu = pcp->pbm->iommu;
+       iommu = pdev->dev.archdata.iommu;
 
        spin_lock_irqsave(&iommu->lock, flags);
        entry = pci_arena_alloc(&iommu->arena, npages);
        spin_unlock_irqrestore(&iommu->lock, flags);
 
-       if (unlikely(entry < 0L)) {
-               free_pages(first_page, order);
-               return NULL;
-       }
+       if (unlikely(entry < 0L))
+               goto arena_alloc_fail;
 
        *dma_addrp = (iommu->page_table_map_base +
                      (entry << IO_PAGE_SHIFT));
        ret = (void *) first_page;
        first_page = __pa(first_page);
 
-       cpu = get_cpu();
+       local_irq_save(flags);
 
-       pglist = &__get_cpu_var(iommu_pglists).pglist[0];
-       for (n = 0; n < npages; n++)
-               pglist[n] = first_page + (n * PAGE_SIZE);
+       pci_iommu_batch_start(pdev,
+                             (HV_PCI_MAP_ATTR_READ |
+                              HV_PCI_MAP_ATTR_WRITE),
+                             entry);
 
-       do {
-               unsigned long num;
+       for (n = 0; n < npages; n++) {
+               long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE));
+               if (unlikely(err < 0L))
+                       goto iommu_map_fail;
+       }
 
-               num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
-                                         npages,
-                                         (HV_PCI_MAP_ATTR_READ |
-                                          HV_PCI_MAP_ATTR_WRITE),
-                                         __pa(pglist));
-               entry += num;
-               npages -= num;
-               pglist += num;
-       } while (npages != 0);
+       if (unlikely(pci_iommu_batch_end() < 0L))
+               goto iommu_map_fail;
 
-       put_cpu();
+       local_irq_restore(flags);
 
        return ret;
+
+iommu_map_fail:
+       /* Interrupts are disabled.  */
+       spin_lock(&iommu->lock);
+       pci_arena_free(&iommu->arena, entry, npages);
+       spin_unlock_irqrestore(&iommu->lock, flags);
+
+arena_alloc_fail:
+       free_pages(first_page, order);
+       return NULL;
 }
 
 static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
 {
-       struct pcidev_cookie *pcp;
-       struct pci_iommu *iommu;
-       unsigned long flags, order, npages, entry, devhandle;
+       struct pci_pbm_info *pbm;
+       struct iommu *iommu;
+       unsigned long flags, order, npages, entry;
+       u32 devhandle;
 
        npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
-       pcp = pdev->sysdata;
-       iommu = pcp->pbm->iommu;
-       devhandle = pcp->pbm->devhandle;
+       iommu = pdev->dev.archdata.iommu;
+       pbm = pdev->dev.archdata.host_controller;
+       devhandle = pbm->devhandle;
        entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
 
        spin_lock_irqsave(&iommu->lock, flags);
@@ -179,19 +257,14 @@ static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu,
 
 static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
 {
-       struct pcidev_cookie *pcp;
-       struct pci_iommu *iommu;
+       struct iommu *iommu;
        unsigned long flags, npages, oaddr;
-       unsigned long i, base_paddr, devhandle;
+       unsigned long i, base_paddr;
        u32 bus_addr, ret;
        unsigned long prot;
        long entry;
-       u64 *pglist;
-       int cpu;
 
-       pcp = pdev->sysdata;
-       iommu = pcp->pbm->iommu;
-       devhandle = pcp->pbm->devhandle;
+       iommu = pdev->dev.archdata.iommu;
 
        if (unlikely(direction == PCI_DMA_NONE))
                goto bad;
@@ -199,8 +272,6 @@ static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz,
        oaddr = (unsigned long)ptr;
        npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
        npages >>= IO_PAGE_SHIFT;
-       if (unlikely(npages > PGLIST_NENTS))
-               goto bad;
 
        spin_lock_irqsave(&iommu->lock, flags);
        entry = pci_arena_alloc(&iommu->arena, npages);
@@ -217,24 +288,19 @@ static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz,
        if (direction != PCI_DMA_TODEVICE)
                prot |= HV_PCI_MAP_ATTR_WRITE;
 
-       cpu = get_cpu();
-
-       pglist = &__get_cpu_var(iommu_pglists).pglist[0];
-       for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE)
-               pglist[i] = base_paddr;
+       local_irq_save(flags);
 
-       do {
-               unsigned long num;
+       pci_iommu_batch_start(pdev, prot, entry);
 
-               num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
-                                         npages, prot,
-                                         __pa(pglist));
-               entry += num;
-               npages -= num;
-               pglist += num;
-       } while (npages != 0);
+       for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
+               long err = pci_iommu_batch_add(base_paddr);
+               if (unlikely(err < 0L))
+                       goto iommu_map_fail;
+       }
+       if (unlikely(pci_iommu_batch_end() < 0L))
+               goto iommu_map_fail;
 
-       put_cpu();
+       local_irq_restore(flags);
 
        return ret;
 
@@ -242,14 +308,23 @@ bad:
        if (printk_ratelimit())
                WARN_ON(1);
        return PCI_DMA_ERROR_CODE;
+
+iommu_map_fail:
+       /* Interrupts are disabled.  */
+       spin_lock(&iommu->lock);
+       pci_arena_free(&iommu->arena, entry, npages);
+       spin_unlock_irqrestore(&iommu->lock, flags);
+
+       return PCI_DMA_ERROR_CODE;
 }
 
 static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
 {
-       struct pcidev_cookie *pcp;
-       struct pci_iommu *iommu;
-       unsigned long flags, npages, devhandle;
+       struct pci_pbm_info *pbm;
+       struct iommu *iommu;
+       unsigned long flags, npages;
        long entry;
+       u32 devhandle;
 
        if (unlikely(direction == PCI_DMA_NONE)) {
                if (printk_ratelimit())
@@ -257,9 +332,9 @@ static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_
                return;
        }
 
-       pcp = pdev->sysdata;
-       iommu = pcp->pbm->iommu;
-       devhandle = pcp->pbm->devhandle;
+       iommu = pdev->dev.archdata.iommu;
+       pbm = pdev->dev.archdata.host_controller;
+       devhandle = pbm->devhandle;
 
        npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
        npages >>= IO_PAGE_SHIFT;
@@ -285,18 +360,19 @@ static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_
 #define SG_ENT_PHYS_ADDRESS(SG)        \
        (__pa(page_address((SG)->page)) + (SG)->offset)
 
-static inline void fill_sg(long entry, unsigned long devhandle,
+static inline long fill_sg(long entry, struct pci_dev *pdev,
                           struct scatterlist *sg,
                           int nused, int nelems, unsigned long prot)
 {
        struct scatterlist *dma_sg = sg;
        struct scatterlist *sg_end = sg + nelems;
-       int i, cpu, pglist_ent;
-       u64 *pglist;
+       unsigned long flags;
+       int i;
+
+       local_irq_save(flags);
+
+       pci_iommu_batch_start(pdev, prot, entry);
 
-       cpu = get_cpu();
-       pglist = &__get_cpu_var(iommu_pglists).pglist[0];
-       pglist_ent = 0;
        for (i = 0; i < nused; i++) {
                unsigned long pteval = ~0UL;
                u32 dma_npages;
@@ -334,7 +410,12 @@ static inline void fill_sg(long entry, unsigned long devhandle,
 
                        pteval = (pteval & IOPTE_PAGE);
                        while (len > 0) {
-                               pglist[pglist_ent++] = pteval;
+                               long err;
+
+                               err = pci_iommu_batch_add(pteval);
+                               if (unlikely(err < 0L))
+                                       goto iommu_map_failed;
+
                                pteval += IO_PAGE_SIZE;
                                len -= (IO_PAGE_SIZE - offset);
                                offset = 0;
@@ -362,28 +443,24 @@ static inline void fill_sg(long entry, unsigned long devhandle,
                dma_sg++;
        }
 
-       BUG_ON(pglist_ent == 0);
-
-       do {
-               unsigned long num;
+       if (unlikely(pci_iommu_batch_end() < 0L))
+               goto iommu_map_failed;
 
-               num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
-                                           pglist_ent);
-               entry += num;
-               pglist_ent -= num;
-       } while (pglist_ent != 0);
+       local_irq_restore(flags);
+       return 0;
 
-       put_cpu();
+iommu_map_failed:
+       local_irq_restore(flags);
+       return -1L;
 }
 
 static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
 {
-       struct pcidev_cookie *pcp;
-       struct pci_iommu *iommu;
-       unsigned long flags, npages, prot, devhandle;
+       struct iommu *iommu;
+       unsigned long flags, npages, prot;
        u32 dma_base;
        struct scatterlist *sgtmp;
-       long entry;
+       long entry, err;
        int used;
 
        /* Fast path single entry scatterlists. */
@@ -398,17 +475,13 @@ static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int n
                return 1;
        }
 
-       pcp = pdev->sysdata;
-       iommu = pcp->pbm->iommu;
-       devhandle = pcp->pbm->devhandle;
+       iommu = pdev->dev.archdata.iommu;
        
        if (unlikely(direction == PCI_DMA_NONE))
                goto bad;
 
        /* Step 1: Prepare scatter list. */
        npages = prepare_sg(sglist, nelems);
-       if (unlikely(npages > PGLIST_NENTS))
-               goto bad;
 
        /* Step 2: Allocate a cluster and context, if necessary. */
        spin_lock_irqsave(&iommu->lock, flags);
@@ -437,7 +510,9 @@ static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int n
        if (direction != PCI_DMA_TODEVICE)
                prot |= HV_PCI_MAP_ATTR_WRITE;
 
-       fill_sg(entry, devhandle, sglist, used, nelems, prot);
+       err = fill_sg(entry, pdev, sglist, used, nelems, prot);
+       if (unlikely(err < 0L))
+               goto iommu_map_failed;
 
        return used;
 
@@ -445,24 +520,31 @@ bad:
        if (printk_ratelimit())
                WARN_ON(1);
        return 0;
+
+iommu_map_failed:
+       spin_lock_irqsave(&iommu->lock, flags);
+       pci_arena_free(&iommu->arena, entry, npages);
+       spin_unlock_irqrestore(&iommu->lock, flags);
+
+       return 0;
 }
 
 static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
 {
-       struct pcidev_cookie *pcp;
-       struct pci_iommu *iommu;
-       unsigned long flags, i, npages, devhandle;
+       struct pci_pbm_info *pbm;
+       struct iommu *iommu;
+       unsigned long flags, i, npages;
        long entry;
-       u32 bus_addr;
+       u32 devhandle, bus_addr;
 
        if (unlikely(direction == PCI_DMA_NONE)) {
                if (printk_ratelimit())
                        WARN_ON(1);
        }
 
-       pcp = pdev->sysdata;
-       iommu = pcp->pbm->iommu;
-       devhandle = pcp->pbm->devhandle;
+       iommu = pdev->dev.archdata.iommu;
+       pbm = pdev->dev.archdata.host_controller;
+       devhandle = pbm->devhandle;
        
        bus_addr = sglist->dma_address & IO_PAGE_MASK;
 
@@ -501,7 +583,7 @@ static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist
        /* Nothing to do... */
 }
 
-struct pci_iommu_ops pci_sun4v_iommu_ops = {
+const struct pci_iommu_ops pci_sun4v_iommu_ops = {
        .alloc_consistent               = pci_4v_alloc_consistent,
        .free_consistent                = pci_4v_free_consistent,
        .map_single                     = pci_4v_map_single,
@@ -512,532 +594,628 @@ struct pci_iommu_ops pci_sun4v_iommu_ops = {
        .dma_sync_sg_for_cpu            = pci_4v_dma_sync_sg_for_cpu,
 };
 
-/* SUN4V PCI configuration space accessors. */
-
-static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus)
+static void pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
 {
-       if (bus < pbm->pci_first_busno ||
-           bus > pbm->pci_last_busno)
-               return 1;
-       return 0;
+       struct property *prop;
+       struct device_node *dp;
+
+       dp = pbm->prom_node;
+       prop = of_find_property(dp, "66mhz-capable", NULL);
+       pbm->is_66mhz_capable = (prop != NULL);
+       pbm->pci_bus = pci_scan_one_pbm(pbm);
+
+       /* XXX register error interrupt handlers XXX */
 }
 
-static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
-                                 int where, int size, u32 *value)
+static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
+                                           struct iommu *iommu)
 {
-       struct pci_pbm_info *pbm = bus_dev->sysdata;
-       u32 devhandle = pbm->devhandle;
-       unsigned int bus = bus_dev->number;
-       unsigned int device = PCI_SLOT(devfn);
-       unsigned int func = PCI_FUNC(devfn);
-       unsigned long ret;
-
-       if (pci_sun4v_out_of_range(pbm, bus)) {
-               ret = ~0UL;
-       } else {
-               ret = pci_sun4v_config_get(devhandle,
-                               HV_PCI_DEVICE_BUILD(bus, device, func),
-                               where, size);
-#if 0
-               printk("read_pci_cfg: devh[%x] device[%08x] where[%x] sz[%d] "
-                      "== [%016lx]\n",
-                      devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
-                      where, size, ret);
-#endif
-       }
-       switch (size) {
-       case 1:
-               *value = ret & 0xff;
-               break;
-       case 2:
-               *value = ret & 0xffff;
-               break;
-       case 4:
-               *value = ret & 0xffffffff;
-               break;
-       };
+       struct iommu_arena *arena = &iommu->arena;
+       unsigned long i, cnt = 0;
+       u32 devhandle;
+
+       devhandle = pbm->devhandle;
+       for (i = 0; i < arena->limit; i++) {
+               unsigned long ret, io_attrs, ra;
 
+               ret = pci_sun4v_iommu_getmap(devhandle,
+                                            HV_PCI_TSBID(0, i),
+                                            &io_attrs, &ra);
+               if (ret == HV_EOK) {
+                       if (page_in_phys_avail(ra)) {
+                               pci_sun4v_iommu_demap(devhandle,
+                                                     HV_PCI_TSBID(0, i), 1);
+                       } else {
+                               cnt++;
+                               __set_bit(i, arena->map);
+                       }
+               }
+       }
 
-       return PCIBIOS_SUCCESSFUL;
+       return cnt;
 }
 
-static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
-                                  int where, int size, u32 value)
+static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
 {
-       struct pci_pbm_info *pbm = bus_dev->sysdata;
-       u32 devhandle = pbm->devhandle;
-       unsigned int bus = bus_dev->number;
-       unsigned int device = PCI_SLOT(devfn);
-       unsigned int func = PCI_FUNC(devfn);
-       unsigned long ret;
-
-       if (pci_sun4v_out_of_range(pbm, bus)) {
-               /* Do nothing. */
+       struct iommu *iommu = pbm->iommu;
+       struct property *prop;
+       unsigned long num_tsb_entries, sz, tsbsize;
+       u32 vdma[2], dma_mask, dma_offset;
+
+       prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
+       if (prop) {
+               u32 *val = prop->value;
+
+               vdma[0] = val[0];
+               vdma[1] = val[1];
        } else {
-               ret = pci_sun4v_config_put(devhandle,
-                               HV_PCI_DEVICE_BUILD(bus, device, func),
-                               where, size, value);
-#if 0
-               printk("write_pci_cfg: devh[%x] device[%08x] where[%x] sz[%d] "
-                      "val[%08x] == [%016lx]\n",
-                      devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
-                      where, size, value, ret);
-#endif
+               /* No property, use default values. */
+               vdma[0] = 0x80000000;
+               vdma[1] = 0x80000000;
        }
-       return PCIBIOS_SUCCESSFUL;
-}
 
-static struct pci_ops pci_sun4v_ops = {
-       .read =         pci_sun4v_read_pci_cfg,
-       .write =        pci_sun4v_write_pci_cfg,
-};
+       if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
+               prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
+                           vdma[0], vdma[1]);
+               prom_halt();
+       };
 
+       dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
+       num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
+       tsbsize = num_tsb_entries * sizeof(iopte_t);
 
-static void pbm_scan_bus(struct pci_controller_info *p,
-                        struct pci_pbm_info *pbm)
-{
-       struct pcidev_cookie *cookie = kmalloc(sizeof(*cookie), GFP_KERNEL);
+       dma_offset = vdma[0];
+
+       /* 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_mask;
 
-       if (!cookie) {
-               prom_printf("%s: Critical allocation failure.\n", pbm->name);
+       /* Allocate and initialize the free area map.  */
+       sz = (num_tsb_entries + 7) / 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;
 
-       /* All we care about is the PBM. */
-       memset(cookie, 0, sizeof(*cookie));
-       cookie->pbm = pbm;
-
-       pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno,
-                                   p->pci_ops,
-                                   pbm);
-#if 0
-       pci_fixup_host_bridge_self(pbm->pci_bus);
-       pbm->pci_bus->self->sysdata = cookie;
-#endif
-
-       pci_fill_in_pbm_cookies(pbm->pci_bus, pbm,
-                               prom_getchild(pbm->prom_node));
-       pci_record_assignments(pbm, pbm->pci_bus);
-       pci_assign_unassigned(pbm, pbm->pci_bus);
-       pci_fixup_irq(pbm, pbm->pci_bus);
-       pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
-       pci_setup_busmastering(pbm, pbm->pci_bus);
+       sz = probe_existing_entries(pbm, iommu);
+       if (sz)
+               printk("%s: Imported %lu TSB entries from OBP\n",
+                      pbm->name, sz);
 }
 
-static void pci_sun4v_scan_bus(struct pci_controller_info *p)
-{
-       if (p->pbm_A.prom_node) {
-               p->pbm_A.is_66mhz_capable =
-                       prom_getbool(p->pbm_A.prom_node, "66mhz-capable");
+#ifdef CONFIG_PCI_MSI
+struct pci_sun4v_msiq_entry {
+       u64             version_type;
+#define MSIQ_VERSION_MASK              0xffffffff00000000UL
+#define MSIQ_VERSION_SHIFT             32
+#define MSIQ_TYPE_MASK                 0x00000000000000ffUL
+#define MSIQ_TYPE_SHIFT                        0
+#define MSIQ_TYPE_NONE                 0x00
+#define MSIQ_TYPE_MSG                  0x01
+#define MSIQ_TYPE_MSI32                        0x02
+#define MSIQ_TYPE_MSI64                        0x03
+#define MSIQ_TYPE_INTX                 0x08
+#define MSIQ_TYPE_NONE2                        0xff
+
+       u64             intx_sysino;
+       u64             reserved1;
+       u64             stick;
+       u64             req_id;  /* bus/device/func */
+#define MSIQ_REQID_BUS_MASK            0xff00UL
+#define MSIQ_REQID_BUS_SHIFT           8
+#define MSIQ_REQID_DEVICE_MASK         0x00f8UL
+#define MSIQ_REQID_DEVICE_SHIFT                3
+#define MSIQ_REQID_FUNC_MASK           0x0007UL
+#define MSIQ_REQID_FUNC_SHIFT          0
+
+       u64             msi_address;
+
+       /* The format of this value is message type dependent.
+        * For MSI bits 15:0 are the data from the MSI packet.
+        * For MSI-X bits 31:0 are the data from the MSI packet.
+        * For MSG, the message code and message routing code where:
+        *      bits 39:32 is the bus/device/fn of the msg target-id
+        *      bits 18:16 is the message routing code
+        *      bits 7:0 is the message code
+        * For INTx the low order 2-bits are:
+        *      00 - INTA
+        *      01 - INTB
+        *      10 - INTC
+        *      11 - INTD
+        */
+       u64             msi_data;
+
+       u64             reserved2;
+};
 
-               pbm_scan_bus(p, &p->pbm_A);
+/* For now this just runs as a pre-handler for the real interrupt handler.
+ * So we just walk through the queue and ACK all the entries, update the
+ * head pointer, and return.
+ *
+ * In the longer term it would be nice to do something more integrated
+ * wherein we can pass in some of this MSI info to the drivers.  This
+ * would be most useful for PCIe fabric error messages, although we could
+ * invoke those directly from the loop here in order to pass the info around.
+ */
+static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
+{
+       struct pci_pbm_info *pbm = data1;
+       struct pci_sun4v_msiq_entry *base, *ep;
+       unsigned long msiqid, orig_head, head, type, err;
+
+       msiqid = (unsigned long) data2;
+
+       head = 0xdeadbeef;
+       err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
+       if (unlikely(err))
+               goto hv_error_get;
+
+       if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
+               goto bad_offset;
+
+       head /= sizeof(struct pci_sun4v_msiq_entry);
+       orig_head = head;
+       base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
+                                  (pbm->msiq_ent_count *
+                                   sizeof(struct pci_sun4v_msiq_entry))));
+       ep = &base[head];
+       while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
+               type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
+               if (unlikely(type != MSIQ_TYPE_MSI32 &&
+                            type != MSIQ_TYPE_MSI64))
+                       goto bad_type;
+
+               pci_sun4v_msi_setstate(pbm->devhandle,
+                                      ep->msi_data /* msi_num */,
+                                      HV_MSISTATE_IDLE);
+
+               /* Clear the entry.  */
+               ep->version_type &= ~MSIQ_TYPE_MASK;
+
+               /* Go to next entry in ring.  */
+               head++;
+               if (head >= pbm->msiq_ent_count)
+                       head = 0;
+               ep = &base[head];
        }
-       if (p->pbm_B.prom_node) {
-               p->pbm_B.is_66mhz_capable =
-                       prom_getbool(p->pbm_B.prom_node, "66mhz-capable");
 
-               pbm_scan_bus(p, &p->pbm_B);
+       if (likely(head != orig_head)) {
+               /* ACK entries by updating head pointer.  */
+               head *= sizeof(struct pci_sun4v_msiq_entry);
+               err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
+               if (unlikely(err))
+                       goto hv_error_set;
        }
+       return;
 
-       /* XXX register error interrupt handlers XXX */
+hv_error_set:
+       printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
+       goto hv_error_cont;
+
+hv_error_get:
+       printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
+
+hv_error_cont:
+       printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
+              pbm->devhandle, msiqid, head);
+       return;
+
+bad_offset:
+       printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
+              head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
+       return;
+
+bad_type:
+       printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
+       return;
 }
 
-static unsigned int pci_sun4v_irq_build(struct pci_pbm_info *pbm,
-                                       struct pci_dev *pdev,
-                                       unsigned int devino)
+static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
 {
-       u32 devhandle = pbm->devhandle;
-       int pil;
-
-       pil = 4;
-       if (pdev) {
-               switch ((pdev->class >> 16) & 0xff) {
-               case PCI_BASE_CLASS_STORAGE:
-                       pil = 4;
-                       break;
+       unsigned long size, bits_per_ulong;
 
-               case PCI_BASE_CLASS_NETWORK:
-                       pil = 6;
-                       break;
-
-               case PCI_BASE_CLASS_DISPLAY:
-                       pil = 9;
-                       break;
+       bits_per_ulong = sizeof(unsigned long) * 8;
+       size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
+       size /= 8;
+       BUG_ON(size % sizeof(unsigned long));
 
-               case PCI_BASE_CLASS_MULTIMEDIA:
-               case PCI_BASE_CLASS_MEMORY:
-               case PCI_BASE_CLASS_BRIDGE:
-               case PCI_BASE_CLASS_SERIAL:
-                       pil = 10;
-                       break;
+       pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
+       if (!pbm->msi_bitmap)
+               return -ENOMEM;
 
-               default:
-                       pil = 4;
-                       break;
-               };
-       }
-       BUG_ON(PIL_RESERVED(pil));
+       return 0;
+}
 
-       return sun4v_build_irq(devhandle, devino, pil, IBF_PCI);
+static void msi_bitmap_free(struct pci_pbm_info *pbm)
+{
+       kfree(pbm->msi_bitmap);
+       pbm->msi_bitmap = NULL;
 }
 
-static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
+static int msi_queue_alloc(struct pci_pbm_info *pbm)
 {
-       struct pcidev_cookie *pcp = pdev->sysdata;
-       struct pci_pbm_info *pbm = pcp->pbm;
-       struct resource *res, *root;
-       u32 reg;
-       int where, size, is_64bit;
-
-       res = &pdev->resource[resource];
-       if (resource < 6) {
-               where = PCI_BASE_ADDRESS_0 + (resource * 4);
-       } else if (resource == PCI_ROM_RESOURCE) {
-               where = pdev->rom_base_reg;
-       } else {
-               /* Somebody might have asked allocation of a non-standard resource */
-               return;
+       unsigned long q_size, alloc_size, pages, order;
+       int i;
+
+       q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
+       alloc_size = (pbm->msiq_num * q_size);
+       order = get_order(alloc_size);
+       pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
+       if (pages == 0UL) {
+               printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
+                      order);
+               return -ENOMEM;
        }
+       memset((char *)pages, 0, PAGE_SIZE << order);
+       pbm->msi_queues = (void *) pages;
+
+       for (i = 0; i < pbm->msiq_num; i++) {
+               unsigned long err, base = __pa(pages + (i * q_size));
+               unsigned long ret1, ret2;
+
+               err = pci_sun4v_msiq_conf(pbm->devhandle,
+                                         pbm->msiq_first + i,
+                                         base, pbm->msiq_ent_count);
+               if (err) {
+                       printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
+                              err);
+                       goto h_error;
+               }
 
-       /* XXX 64-bit MEM handling is not %100 correct... XXX */
-       is_64bit = 0;
-       if (res->flags & IORESOURCE_IO)
-               root = &pbm->io_space;
-       else {
-               root = &pbm->mem_space;
-               if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
-                   == PCI_BASE_ADDRESS_MEM_TYPE_64)
-                       is_64bit = 1;
+               err = pci_sun4v_msiq_info(pbm->devhandle,
+                                         pbm->msiq_first + i,
+                                         &ret1, &ret2);
+               if (err) {
+                       printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
+                              err);
+                       goto h_error;
+               }
+               if (ret1 != base || ret2 != pbm->msiq_ent_count) {
+                       printk(KERN_ERR "MSI: Bogus qconf "
+                              "expected[%lx:%x] got[%lx:%lx]\n",
+                              base, pbm->msiq_ent_count,
+                              ret1, ret2);
+                       goto h_error;
+               }
        }
 
-       size = res->end - res->start;
-       pci_read_config_dword(pdev, where, &reg);
-       reg = ((reg & size) |
-              (((u32)(res->start - root->start)) & ~size));
-       if (resource == PCI_ROM_RESOURCE) {
-               reg |= PCI_ROM_ADDRESS_ENABLE;
-               res->flags |= IORESOURCE_ROM_ENABLE;
+       return 0;
+
+h_error:
+       free_pages(pages, order);
+       return -EINVAL;
+}
+
+
+static int alloc_msi(struct pci_pbm_info *pbm)
+{
+       int i;
+
+       for (i = 0; i < pbm->msi_num; i++) {
+               if (!test_and_set_bit(i, pbm->msi_bitmap))
+                       return i + pbm->msi_first;
        }
-       pci_write_config_dword(pdev, where, reg);
 
-       /* This knows that the upper 32-bits of the address
-        * must be zero.  Our PCI common layer enforces this.
-        */
-       if (is_64bit)
-               pci_write_config_dword(pdev, where + 4, 0);
+       return -ENOENT;
 }
 
-static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
-                                     struct resource *res,
-                                     struct resource *root)
+static void free_msi(struct pci_pbm_info *pbm, int msi_num)
 {
-       res->start += root->start;
-       res->end += root->start;
+       msi_num -= pbm->msi_first;
+       clear_bit(msi_num, pbm->msi_bitmap);
 }
 
-/* Use ranges property to determine where PCI MEM, I/O, and Config
- * space are for this PCI bus module.
- */
-static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
+static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
+                                  struct pci_dev *pdev,
+                                  struct msi_desc *entry)
 {
-       int i, saw_mem, saw_io;
-
-       saw_mem = saw_io = 0;
-       for (i = 0; i < pbm->num_pbm_ranges; i++) {
-               struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
-               unsigned long a;
-               int type;
-
-               type = (pr->child_phys_hi >> 24) & 0x3;
-               a = (((unsigned long)pr->parent_phys_hi << 32UL) |
-                    ((unsigned long)pr->parent_phys_lo  <<  0UL));
-
-               switch (type) {
-               case 1:
-                       /* 16-bit IO space, 16MB */
-                       pbm->io_space.start = a;
-                       pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
-                       pbm->io_space.flags = IORESOURCE_IO;
-                       saw_io = 1;
-                       break;
+       struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
+       unsigned long devino, msiqid;
+       struct msi_msg msg;
+       int msi_num, err;
 
-               case 2:
-                       /* 32-bit MEM space, 2GB */
-                       pbm->mem_space.start = a;
-                       pbm->mem_space.end = a + (0x80000000UL - 1UL);
-                       pbm->mem_space.flags = IORESOURCE_MEM;
-                       saw_mem = 1;
-                       break;
+       *virt_irq_p = 0;
 
-               case 3:
-                       /* XXX 64-bit MEM handling XXX */
+       msi_num = alloc_msi(pbm);
+       if (msi_num < 0)
+               return msi_num;
 
-               default:
-                       break;
-               };
-       }
+       devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
+                                pbm->msiq_first_devino,
+                                (pbm->msiq_first_devino +
+                                 pbm->msiq_num));
+       err = -ENOMEM;
+       if (!devino)
+               goto out_err;
 
-       if (!saw_io || !saw_mem) {
-               prom_printf("%s: Fatal error, missing %s PBM range.\n",
-                           pbm->name,
-                           (!saw_io ? "IO" : "MEM"));
-               prom_halt();
-       }
+       msiqid = ((devino - pbm->msiq_first_devino) +
+                 pbm->msiq_first);
 
-       printk("%s: PCI IO[%lx] MEM[%lx]\n",
-              pbm->name,
-              pbm->io_space.start,
-              pbm->mem_space.start);
-}
+       err = -EINVAL;
+       if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
+       if (err)
+               goto out_err;
 
-static void pbm_register_toplevel_resources(struct pci_controller_info *p,
-                                           struct pci_pbm_info *pbm)
-{
-       pbm->io_space.name = pbm->mem_space.name = pbm->name;
+       if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
+               goto out_err;
 
-       request_resource(&ioport_resource, &pbm->io_space);
-       request_resource(&iomem_resource, &pbm->mem_space);
-       pci_register_legacy_regions(&pbm->io_space,
-                                   &pbm->mem_space);
-}
+       if (pci_sun4v_msi_setmsiq(pbm->devhandle,
+                                 msi_num, msiqid,
+                                 (entry->msi_attrib.is_64 ?
+                                  HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
+               goto out_err;
 
-static void probe_existing_entries(struct pci_pbm_info *pbm,
-                                  struct pci_iommu *iommu)
-{
-       struct pci_iommu_arena *arena = &iommu->arena;
-       unsigned long i, devhandle;
+       if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
+               goto out_err;
 
-       devhandle = pbm->devhandle;
-       for (i = 0; i < arena->limit; i++) {
-               unsigned long ret, io_attrs, ra;
-
-               ret = pci_sun4v_iommu_getmap(devhandle,
-                                            HV_PCI_TSBID(0, i),
-                                            &io_attrs, &ra);
-               if (ret == HV_EOK)
-                       __set_bit(i, arena->map);
-       }
-}
+       if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
+               goto out_err;
 
-static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
-{
-       struct pci_iommu *iommu = pbm->iommu;
-       unsigned long num_tsb_entries, sz;
-       u32 vdma[2], dma_mask, dma_offset;
-       int err, tsbsize;
+       pdev->dev.archdata.msi_num = msi_num;
 
-       err = prom_getproperty(pbm->prom_node, "virtual-dma",
-                              (char *)&vdma[0], sizeof(vdma));
-       if (err == 0 || err == -1) {
-               /* No property, use default values. */
-               vdma[0] = 0x80000000;
-               vdma[1] = 0x80000000;
+       if (entry->msi_attrib.is_64) {
+               msg.address_hi = pbm->msi64_start >> 32;
+               msg.address_lo = pbm->msi64_start & 0xffffffff;
+       } else {
+               msg.address_hi = 0;
+               msg.address_lo = pbm->msi32_start;
        }
+       msg.data = msi_num;
 
-       dma_mask = vdma[0];
-       switch (vdma[1]) {
-               case 0x20000000:
-                       dma_mask |= 0x1fffffff;
-                       tsbsize = 64;
-                       break;
+       set_irq_msi(*virt_irq_p, entry);
+       write_msi_msg(*virt_irq_p, &msg);
 
-               case 0x40000000:
-                       dma_mask |= 0x3fffffff;
-                       tsbsize = 128;
-                       break;
+       irq_install_pre_handler(*virt_irq_p,
+                               pci_sun4v_msi_prehandler,
+                               pbm, (void *) msiqid);
 
-               case 0x80000000:
-                       dma_mask |= 0x7fffffff;
-                       tsbsize = 128;
-                       break;
+       return 0;
 
-               default:
-                       prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
-                       prom_halt();
-       };
+out_err:
+       free_msi(pbm, msi_num);
+       sun4v_destroy_msi(*virt_irq_p);
+       *virt_irq_p = 0;
+       return err;
 
-       num_tsb_entries = tsbsize / sizeof(iopte_t);
+}
 
-       dma_offset = vdma[0];
+static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
+                                      struct pci_dev *pdev)
+{
+       struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
+       unsigned long msiqid, err;
+       unsigned int msi_num;
+
+       msi_num = pdev->dev.archdata.msi_num;
+       err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
+       if (err) {
+               printk(KERN_ERR "%s: getmsiq gives error %lu\n",
+                      pbm->name, err);
+               return;
+       }
 
-       /* 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_mask;
+       pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
+       pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
 
-       /* Allocate and initialize the free area map.  */
-       sz = num_tsb_entries / 8;
-       sz = (sz + 7UL) & ~7UL;
-       iommu->arena.map = kmalloc(sz, GFP_KERNEL);
-       if (!iommu->arena.map) {
-               prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
-               prom_halt();
-       }
-       memset(iommu->arena.map, 0, sz);
-       iommu->arena.limit = num_tsb_entries;
+       free_msi(pbm, msi_num);
 
-       probe_existing_entries(pbm, iommu);
+       /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
+        * allocation.
+        */
+       sun4v_destroy_msi(virt_irq);
 }
 
-/* Don't get this from the root nexus, get it from the "pci@0" node below.  */
-static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
+static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
 {
-       unsigned int busrange[2];
-       int prom_node = pbm->prom_node;
-       int err;
-
-       prom_node = prom_getchild(prom_node);
-       if (prom_node == 0) {
-               prom_printf("%s: Fatal error, no child OBP node.\n", pbm->name);
-               prom_halt();
-       }
+       const u32 *val;
+       int len;
+
+       val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
+       if (!val || len != 4)
+               goto no_msi;
+       pbm->msiq_num = *val;
+       if (pbm->msiq_num) {
+               const struct msiq_prop {
+                       u32 first_msiq;
+                       u32 num_msiq;
+                       u32 first_devino;
+               } *mqp;
+               const struct msi_range_prop {
+                       u32 first_msi;
+                       u32 num_msi;
+               } *mrng;
+               const struct addr_range_prop {
+                       u32 msi32_high;
+                       u32 msi32_low;
+                       u32 msi32_len;
+                       u32 msi64_high;
+                       u32 msi64_low;
+                       u32 msi64_len;
+               } *arng;
+
+               val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
+               if (!val || len != 4)
+                       goto no_msi;
+
+               pbm->msiq_ent_count = *val;
+
+               mqp = of_get_property(pbm->prom_node,
+                                     "msi-eq-to-devino", &len);
+               if (!mqp || len != sizeof(struct msiq_prop))
+                       goto no_msi;
+
+               pbm->msiq_first = mqp->first_msiq;
+               pbm->msiq_first_devino = mqp->first_devino;
+
+               val = of_get_property(pbm->prom_node, "#msi", &len);
+               if (!val || len != 4)
+                       goto no_msi;
+               pbm->msi_num = *val;
+
+               mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
+               if (!mrng || len != sizeof(struct msi_range_prop))
+                       goto no_msi;
+               pbm->msi_first = mrng->first_msi;
+
+               val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
+               if (!val || len != 4)
+                       goto no_msi;
+               pbm->msi_data_mask = *val;
+
+               val = of_get_property(pbm->prom_node, "msix-data-width", &len);
+               if (!val || len != 4)
+                       goto no_msi;
+               pbm->msix_data_width = *val;
+
+               arng = of_get_property(pbm->prom_node, "msi-address-ranges",
+                                      &len);
+               if (!arng || len != sizeof(struct addr_range_prop))
+                       goto no_msi;
+               pbm->msi32_start = ((u64)arng->msi32_high << 32) |
+                       (u64) arng->msi32_low;
+               pbm->msi64_start = ((u64)arng->msi64_high << 32) |
+                       (u64) arng->msi64_low;
+               pbm->msi32_len = arng->msi32_len;
+               pbm->msi64_len = arng->msi64_len;
+
+               if (msi_bitmap_alloc(pbm))
+                       goto no_msi;
+
+               if (msi_queue_alloc(pbm)) {
+                       msi_bitmap_free(pbm);
+                       goto no_msi;
+               }
 
-       err = prom_getproperty(prom_node, "bus-range",
-                              (char *)&busrange[0],
-                              sizeof(busrange));
-       if (err == 0 || err == -1) {
-               prom_printf("%s: Fatal error, no bus-range.\n", pbm->name);
-               prom_halt();
+               printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
+                      "devino[0x%x]\n",
+                      pbm->name,
+                      pbm->msiq_first, pbm->msiq_num,
+                      pbm->msiq_ent_count,
+                      pbm->msiq_first_devino);
+               printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
+                      "width[%u]\n",
+                      pbm->name,
+                      pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
+                      pbm->msix_data_width);
+               printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
+                      "addr64[0x%lx:0x%x]\n",
+                      pbm->name,
+                      pbm->msi32_start, pbm->msi32_len,
+                      pbm->msi64_start, pbm->msi64_len);
+               printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
+                      pbm->name,
+                      pbm->msi_queues);
        }
+       pbm->setup_msi_irq = pci_sun4v_setup_msi_irq;
+       pbm->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
 
-       pbm->pci_first_busno = busrange[0];
-       pbm->pci_last_busno = busrange[1];
+       return;
 
+no_msi:
+       pbm->msiq_num = 0;
+       printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
 }
+#else /* CONFIG_PCI_MSI */
+static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
+{
+}
+#endif /* !(CONFIG_PCI_MSI) */
 
-static void pci_sun4v_pbm_init(struct pci_controller_info *p, int prom_node, unsigned int devhandle)
+static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
 {
        struct pci_pbm_info *pbm;
-       int err, i;
 
        if (devhandle & 0x40)
                pbm = &p->pbm_B;
        else
                pbm = &p->pbm_A;
 
-       pbm->parent = p;
-       pbm->prom_node = prom_node;
-       pbm->pci_first_slot = 1;
+       pbm->next = pci_pbm_root;
+       pci_pbm_root = pbm;
 
-       pbm->devhandle = devhandle;
+       pbm->scan_bus = pci_sun4v_scan_bus;
+       pbm->pci_ops = &sun4v_pci_ops;
+       pbm->config_space_reg_bits = 12;
 
-       sprintf(pbm->name, "SUN4V-PCI%d PBM%c",
-               p->index, (pbm == &p->pbm_A ? 'A' : 'B'));
+       pbm->index = pci_num_pbms++;
 
-       printk("%s: devhandle[%x]\n", pbm->name, pbm->devhandle);
+       pbm->parent = p;
+       pbm->prom_node = dp;
 
-       prom_getstring(prom_node, "name",
-                      pbm->prom_name, sizeof(pbm->prom_name));
+       pbm->devhandle = devhandle;
 
-       err = prom_getproperty(prom_node, "ranges",
-                              (char *) pbm->pbm_ranges,
-                              sizeof(pbm->pbm_ranges));
-       if (err == 0 || err == -1) {
-               prom_printf("%s: Fatal error, no ranges property.\n",
-                           pbm->name);
-               prom_halt();
-       }
+       pbm->name = dp->full_name;
 
-       pbm->num_pbm_ranges =
-               (err / sizeof(struct linux_prom_pci_ranges));
+       printk("%s: SUN4V PCI Bus Module\n", pbm->name);
 
-       /* Mask out the top 8 bits of the ranges, leaving the real
-        * physical address.
-        */
-       for (i = 0; i < pbm->num_pbm_ranges; i++)
-               pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
-
-       pci_sun4v_determine_mem_io_space(pbm);
-       pbm_register_toplevel_resources(p, pbm);
-
-       err = prom_getproperty(prom_node, "interrupt-map",
-                              (char *)pbm->pbm_intmap,
-                              sizeof(pbm->pbm_intmap));
-       if (err != -1) {
-               pbm->num_pbm_intmap = (err / sizeof(struct linux_prom_pci_intmap));
-               err = prom_getproperty(prom_node, "interrupt-map-mask",
-                                      (char *)&pbm->pbm_intmask,
-                                      sizeof(pbm->pbm_intmask));
-               if (err == -1) {
-                       prom_printf("%s: Fatal error, no "
-                                   "interrupt-map-mask.\n", pbm->name);
-                       prom_halt();
-               }
-       } else {
-               pbm->num_pbm_intmap = 0;
-               memset(&pbm->pbm_intmask, 0, sizeof(pbm->pbm_intmask));
-       }
+       pci_determine_mem_io_space(pbm);
 
-       pci_sun4v_get_bus_range(pbm);
+       pci_get_pbm_props(pbm);
        pci_sun4v_iommu_init(pbm);
+       pci_sun4v_msi_init(pbm);
 }
 
-void sun4v_pci_init(int node, char *model_name)
+void sun4v_pci_init(struct device_node *dp, char *model_name)
 {
        struct pci_controller_info *p;
-       struct pci_iommu *iommu;
-       struct linux_prom64_registers regs;
-       unsigned int devhandle;
-
-       prom_getproperty(node, "reg", (char *)&regs, sizeof(regs));
-       devhandle = (regs.phys_addr >> 32UL) & 0x0fffffff;;
-
-       for (p = pci_controller_root; p; p = p->next) {
-               struct pci_pbm_info *pbm;
+       struct pci_pbm_info *pbm;
+       struct iommu *iommu;
+       struct property *prop;
+       struct linux_prom64_registers *regs;
+       u32 devhandle;
+       int i;
 
-               if (p->pbm_A.prom_node && p->pbm_B.prom_node)
-                       continue;
+       prop = of_find_property(dp, "reg", NULL);
+       regs = prop->value;
 
-               pbm = (p->pbm_A.prom_node ?
-                      &p->pbm_A :
-                      &p->pbm_B);
+       devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
 
+       for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
                if (pbm->devhandle == (devhandle ^ 0x40)) {
-                       pci_sun4v_pbm_init(p, node, devhandle);
+                       pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
                        return;
                }
        }
 
-       p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
-       if (!p) {
-               prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
-               prom_halt();
-       }
-       memset(p, 0, sizeof(*p));
+       for_each_possible_cpu(i) {
+               unsigned long page = get_zeroed_page(GFP_ATOMIC);
 
-       iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
-       if (!iommu) {
-               prom_printf("SCHIZO: Fatal memory allocation error.\n");
-               prom_halt();
-       }
-       memset(iommu, 0, sizeof(*iommu));
-       p->pbm_A.iommu = iommu;
+               if (!page)
+                       goto fatal_memory_error;
 
-       iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
-       if (!iommu) {
-               prom_printf("SCHIZO: Fatal memory allocation error.\n");
-               prom_halt();
+               per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
        }
-       memset(iommu, 0, sizeof(*iommu));
-       p->pbm_B.iommu = iommu;
 
-       p->next = pci_controller_root;
-       pci_controller_root = p;
+       p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
+       if (!p)
+               goto fatal_memory_error;
 
-       p->index = pci_num_controllers++;
-       p->pbms_same_domain = 0;
+       iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
+       if (!iommu)
+               goto fatal_memory_error;
+
+       p->pbm_A.iommu = iommu;
 
-       p->scan_bus = pci_sun4v_scan_bus;
-       p->irq_build = pci_sun4v_irq_build;
-       p->base_address_update = pci_sun4v_base_address_update;
-       p->resource_adjust = pci_sun4v_resource_adjust;
-       p->pci_ops = &pci_sun4v_ops;
+       iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
+       if (!iommu)
+               goto fatal_memory_error;
+
+       p->pbm_B.iommu = iommu;
 
        /* Like PSYCHO and SCHIZO we have a 2GB aligned area
         * for memory space.
         */
        pci_memspace_mask = 0x7fffffffUL;
 
-       pci_sun4v_pbm_init(p, node, devhandle);
+       pci_sun4v_pbm_init(p, dp, devhandle);
+       return;
+
+fatal_memory_error:
+       prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
+       prom_halt();
 }