Pull thermal into release branch
[linux-drm-fsl-dcu.git] / arch / powerpc / kernel / pci_32.c
index d32cd500d8b8086e84bceae626bb26421b556e94..e66064b5093a2496ad2e03e9ebd1212ba98ad72b 100644 (file)
@@ -100,7 +100,7 @@ pcibios_fixup_resources(struct pci_dev *dev)
                        continue;
                if (res->end == 0xffffffff) {
                        DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
-                           pci_name(dev), i, res->start, res->end);
+                           pci_name(dev), i, (u64)res->start, (u64)res->end);
                        res->end -= res->start;
                        res->start = 0;
                        res->flags |= IORESOURCE_UNSET;
@@ -116,11 +116,9 @@ pcibios_fixup_resources(struct pci_dev *dev)
                if (offset != 0) {
                        res->start += offset;
                        res->end += offset;
-#ifdef DEBUG
-                       printk("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
-                              i, res->flags, pci_name(dev),
-                              res->start - offset, res->start);
-#endif
+                       DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
+                           i, res->flags, pci_name(dev),
+                           (u64)res->start - offset, (u64)res->start);
                }
        }
 
@@ -256,7 +254,7 @@ pcibios_allocate_bus_resources(struct list_head *bus_list)
                        }
 
                        DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
-                               res->start, res->end, res->flags, pr);
+                           (u64)res->start, (u64)res->end, res->flags, pr);
                        if (pr) {
                                if (request_resource(pr, res) == 0)
                                        continue;
@@ -307,7 +305,7 @@ reparent_resources(struct resource *parent, struct resource *res)
        for (p = res->child; p != NULL; p = p->sibling) {
                p->parent = res;
                DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
-                   p->name, p->start, p->end, res->name);
+                   p->name, (u64)p->start, (u64)p->end, res->name);
        }
        return 0;
 }
@@ -363,7 +361,7 @@ pci_relocate_bridge_resource(struct pci_bus *bus, int i)
        }
        if (request_resource(pr, res)) {
                DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
-                   res->start, res->end);
+                   (u64)res->start, (u64)res->end);
                return -1;              /* "can't happen" */
        }
        update_bridge_base(bus, i);
@@ -481,14 +479,14 @@ static inline void alloc_resource(struct pci_dev *dev, int idx)
        struct resource *pr, *r = &dev->resource[idx];
 
        DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
-           pci_name(dev), idx, r->start, r->end, r->flags);
+           pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
        pr = pci_find_parent_resource(dev, r);
        if (!pr || request_resource(pr, r) < 0) {
                printk(KERN_ERR "PCI: Cannot allocate resource region %d"
                       " of device %s\n", idx, pci_name(dev));
                if (pr)
                        DBG("PCI:  parent is %p: %016llx-%016llx (f=%lx)\n",
-                           pr, pr->start, pr->end, pr->flags);
+                           pr, (u64)pr->start, (u64)pr->end, pr->flags);
                /* We'll assign a new address later */
                r->flags |= IORESOURCE_UNSET;
                r->end -= r->start;
@@ -639,7 +637,7 @@ make_one_node_map(struct device_node* node, u8 pci_bus)
 
        if (pci_bus >= pci_bus_count)
                return;
-       bus_range = get_property(node, "bus-range", &len);
+       bus_range = of_get_property(node, "bus-range", &len);
        if (bus_range == NULL || len < 2 * sizeof(int)) {
                printk(KERN_WARNING "Can't get bus-range for %s, "
                       "assuming it starts at 0\n", node->full_name);
@@ -651,17 +649,20 @@ make_one_node_map(struct device_node* node, u8 pci_bus)
                struct pci_dev* dev;
                const unsigned int *class_code, *reg;
        
-               class_code = get_property(node, "class-code", NULL);
+               class_code = of_get_property(node, "class-code", NULL);
                if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
                        (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
                        continue;
-               reg = get_property(node, "reg", NULL);
+               reg = of_get_property(node, "reg", NULL);
                if (!reg)
                        continue;
-               dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
-               if (!dev || !dev->subordinate)
+               dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
+               if (!dev || !dev->subordinate) {
+                       pci_dev_put(dev);
                        continue;
+               }
                make_one_node_map(node, dev->subordinate->number);
+               pci_dev_put(dev);
        }
 }
        
@@ -671,8 +672,9 @@ pcibios_make_OF_bus_map(void)
        int i;
        struct pci_controller* hose;
        struct property *map_prop;
+       struct device_node *dn;
 
-       pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
+       pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
        if (!pci_to_OF_bus_map) {
                printk(KERN_ERR "Can't allocate OF bus map !\n");
                return;
@@ -692,12 +694,13 @@ pcibios_make_OF_bus_map(void)
                        continue;
                make_one_node_map(node, hose->first_busno);
        }
-       map_prop = of_find_property(find_path_device("/"),
-                       "pci-OF-bus-map", NULL);
+       dn = of_find_node_by_path("/");
+       map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
        if (map_prop) {
                BUG_ON(pci_bus_count > map_prop->length);
                memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
        }
+       of_node_put(dn);
 #ifdef DEBUG
        printk("PCI->OF bus map:\n");
        for (i=0; i<pci_bus_count; i++) {
@@ -726,7 +729,7 @@ scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void*
                 * a fake root for all functions of a multi-function device,
                 * we go down them as well.
                 */
-               class_code = get_property(node, "class-code", NULL);
+               class_code = of_get_property(node, "class-code", NULL);
                if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
                        (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
                        strcmp(node->name, "multifunc-device"))
@@ -738,25 +741,46 @@ scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void*
        return NULL;
 }
 
-static int
-scan_OF_pci_childs_iterator(struct device_node* node, void* data)
+static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
+                                              unsigned int devfn)
 {
-       const unsigned int *reg;
-       u8* fdata = (u8*)data;
-       
-       reg = get_property(node, "reg", NULL);
-       if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
-               && ((reg[0] >> 16) & 0xff) == fdata[0])
-               return 1;
-       return 0;
+       struct device_node *np = NULL;
+       const u32 *reg;
+       unsigned int psize;
+
+       while ((np = of_get_next_child(parent, np)) != NULL) {
+               reg = of_get_property(np, "reg", &psize);
+               if (reg == NULL || psize < 4)
+                       continue;
+               if (((reg[0] >> 8) & 0xff) == devfn)
+                       return np;
+       }
+       return NULL;
 }
 
-static struct device_node*
-scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
+
+static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
 {
-       u8 filter_data[2] = {bus, dev_fn};
+       struct device_node *parent, *np;
+
+       /* Are we a root bus ? */
+       if (bus->self == NULL || bus->parent == NULL) {
+               struct pci_controller *hose = pci_bus_to_hose(bus->number);
+               if (hose == NULL)
+                       return NULL;
+               return of_node_get(hose->arch_data);
+       }
+
+       /* not a root bus, we need to get our parent */
+       parent = scan_OF_for_pci_bus(bus->parent);
+       if (parent == NULL)
+               return NULL;
+
+       /* now iterate for children for a match */
+       np = scan_OF_for_pci_dev(parent, bus->self->devfn);
+       of_node_put(parent);
 
-       return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
+       return np;
 }
 
 /*
@@ -765,43 +789,25 @@ scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
 struct device_node *
 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
 {
-       struct pci_controller *hose;
-       struct device_node *node;
-       int busnr;
+       struct device_node *parent, *np;
 
        if (!have_of)
                return NULL;
-       
-       /* Lookup the hose */
-       busnr = bus->number;
-       hose = pci_bus_to_hose(busnr);
-       if (!hose)
-               return NULL;
 
-       /* Check it has an OF node associated */
-       node = (struct device_node *) hose->arch_data;
-       if (!node)
+       DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
+       parent = scan_OF_for_pci_bus(bus);
+       if (parent == NULL)
                return NULL;
-
-       /* Fixup bus number according to what OF think it is. */
-#ifdef CONFIG_PPC_PMAC
-       /* The G5 need a special case here. Basically, we don't remap all
-        * busses on it so we don't create the pci-OF-map. However, we do
-        * remap the AGP bus and so have to deal with it. A future better
-        * fix has to be done by making the remapping per-host and always
-        * filling the pci_to_OF map. --BenH
+       DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
+       np = scan_OF_for_pci_dev(parent, devfn);
+       of_node_put(parent);
+       DBG(" result is %s\n", np ? np->full_name : "<NULL>");
+
+       /* XXX most callers don't release the returned node
+        * mostly because ppc64 doesn't increase the refcount,
+        * we need to fix that.
         */
-       if (machine_is(powermac) && busnr >= 0xf0)
-               busnr -= 0xf0;
-       else
-#endif
-       if (pci_to_OF_bus_map)
-               busnr = pci_to_OF_bus_map[busnr];
-       if (busnr == 0xff)
-               return NULL;
-       
-       /* Now, lookup childs of the hose */
-       return scan_OF_childs_for_device(node->child, busnr, devfn);
+       return np;
 }
 EXPORT_SYMBOL(pci_busdev_to_OF_node);
 
@@ -858,7 +864,7 @@ pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
        if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
                        find_OF_pci_device_filter, (void *)node))
                return -ENODEV;
-       reg = get_property(node, "reg", NULL);
+       reg = of_get_property(node, "reg", NULL);
        if (!reg)
                return -ENODEV;
        *bus = (reg[0] >> 16) & 0xff;
@@ -894,14 +900,14 @@ pci_process_bridge_OF_ranges(struct pci_controller *hose,
        int rlen = 0, orig_rlen;
        int memno = 0;
        struct resource *res;
-       int np, na = prom_n_addr_cells(dev);
+       int np, na = of_n_addr_cells(dev);
        np = na + 5;
 
        /* First we try to merge ranges to fix a problem with some pmacs
         * that can have more than 3 ranges, fortunately using contiguous
         * addresses -- BenH
         */
-       dt_ranges = get_property(dev, "ranges", &rlen);
+       dt_ranges = of_get_property(dev, "ranges", &rlen);
        if (!dt_ranges)
                return;
        /* Sanity check, though hopefully that never happens */
@@ -961,7 +967,7 @@ pci_process_bridge_OF_ranges(struct pci_controller *hose,
                        res->flags = IORESOURCE_IO;
                        res->start = ranges[2];
                        DBG("PCI: IO 0x%llx -> 0x%llx\n",
-                                   res->start, res->start + size - 1);
+                           (u64)res->start, (u64)res->start + size - 1);
                        break;
                case 2:         /* memory space */
                        memno = 0;
@@ -983,7 +989,7 @@ pci_process_bridge_OF_ranges(struct pci_controller *hose,
                                        res->flags |= IORESOURCE_PREFETCH;
                                res->start = ranges[na+2];
                                DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
-                                           res->start, res->start + size - 1);
+                                   (u64)res->start, (u64)res->start + size - 1);
                        }
                        break;
                }
@@ -1005,14 +1011,19 @@ void __init
 pci_create_OF_bus_map(void)
 {
        struct property* of_prop;
-       
+       struct device_node *dn;
+
        of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
-       if (of_prop && find_path_device("/")) {
+       if (!of_prop)
+               return;
+       dn = of_find_node_by_path("/");
+       if (dn) {
                memset(of_prop, -1, sizeof(struct property) + 256);
                of_prop->name = "pci-OF-bus-map";
                of_prop->length = 256;
-               of_prop->value = (unsigned char *)&of_prop[1];
-               prom_add_property(find_path_device("/"), of_prop);
+               of_prop->value = &of_prop[1];
+               prom_add_property(dn, of_prop);
+               of_node_put(dn);
        }
 }
 
@@ -1269,7 +1280,10 @@ pcibios_init(void)
                if (pci_assign_all_buses)
                        hose->first_busno = next_busno;
                hose->last_busno = 0xff;
-               bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
+               bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
+                                           hose->ops, hose);
+               if (bus)
+                       pci_bus_add_devices(bus);
                hose->last_busno = bus->subordinate;
                if (pci_assign_all_buses || next_busno <= hose->last_busno)
                        next_busno = hose->last_busno + pcibios_assign_bus_offset;
@@ -1446,7 +1460,6 @@ int pci_read_irq_line(struct pci_dev *pci_dev)
                return -1;
        }
        pci_dev->irq = virq;
-       pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, virq);
 
        return 0;
 }
@@ -1543,7 +1556,7 @@ pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
 
 
 static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
-                                              unsigned long *offset,
+                                              resource_size_t *offset,
                                               enum pci_mmap_state mmap_state)
 {
        struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
@@ -1555,10 +1568,12 @@ static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
 
        /* If memory, add on the PCI bridge address offset */
        if (mmap_state == pci_mmap_mem) {
+#if 0 /* See comment in pci_resource_to_user() for why this is disabled */
                *offset += hose->pci_mem_offset;
+#endif
                res_bit = IORESOURCE_MEM;
        } else {
-               io_offset = hose->io_base_virt - ___IO_BASE;
+               io_offset = hose->io_base_virt - (void __iomem *)_IO_BASE;
                *offset += io_offset;
                res_bit = IORESOURCE_IO;
        }
@@ -1623,9 +1638,6 @@ static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
        else
                prot |= _PAGE_GUARDED;
 
-       printk("PCI map for %s:%llx, prot: %lx\n", pci_name(dev),
-               (unsigned long long)rp->start, prot);
-
        return __pgprot(prot);
 }
 
@@ -1646,7 +1658,7 @@ pgprot_t pci_phys_mem_access_prot(struct file *file,
        int i;
 
        if (page_is_ram(pfn))
-               return prot;
+               return __pgprot(prot);
 
        prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
 
@@ -1694,7 +1706,7 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
                        enum pci_mmap_state mmap_state,
                        int write_combine)
 {
-       unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
+       resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
        struct resource *rp;
        int ret;
 
@@ -1807,21 +1819,42 @@ void pci_resource_to_user(const struct pci_dev *dev, int bar,
                          resource_size_t *start, resource_size_t *end)
 {
        struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
-       unsigned long offset = 0;
+       resource_size_t offset = 0;
 
        if (hose == NULL)
                return;
 
        if (rsrc->flags & IORESOURCE_IO)
-               offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
+               offset = (unsigned long)hose->io_base_virt - _IO_BASE;
+
+       /* We pass a fully fixed up address to userland for MMIO instead of
+        * a BAR value because X is lame and expects to be able to use that
+        * to pass to /dev/mem !
+        *
+        * That means that we'll have potentially 64 bits values where some
+        * userland apps only expect 32 (like X itself since it thinks only
+        * Sparc has 64 bits MMIO) but if we don't do that, we break it on
+        * 32 bits CHRPs :-(
+        *
+        * Hopefully, the sysfs insterface is immune to that gunk. Once X
+        * has been fixed (and the fix spread enough), we can re-enable the
+        * 2 lines below and pass down a BAR value to userland. In that case
+        * we'll also have to re-enable the matching code in
+        * __pci_mmap_make_offset().
+        *
+        * BenH.
+        */
+#if 0
+       else if (rsrc->flags & IORESOURCE_MEM)
+               offset = hose->pci_mem_offset;
+#endif
 
-       *start = rsrc->start + offset;
-       *end = rsrc->end + offset;
+       *start = rsrc->start - offset;
+       *end = rsrc->end - offset;
 }
 
-void __init
-pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
-                 int flags, char *name)
+void __init pci_init_resource(struct resource *res, resource_size_t start,
+                             resource_size_t end, int flags, char *name)
 {
        res->start = start;
        res->end = end;
@@ -1832,35 +1865,6 @@ pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
        res->child = NULL;
 }
 
-void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
-{
-       unsigned long start = pci_resource_start(dev, bar);
-       unsigned long len = pci_resource_len(dev, bar);
-       unsigned long flags = pci_resource_flags(dev, bar);
-
-       if (!len)
-               return NULL;
-       if (max && len > max)
-               len = max;
-       if (flags & IORESOURCE_IO)
-               return ioport_map(start, len);
-       if (flags & IORESOURCE_MEM)
-               /* Not checking IORESOURCE_CACHEABLE because PPC does
-                * not currently distinguish between ioremap and
-                * ioremap_nocache.
-                */
-               return ioremap(start, len);
-       /* What? */
-       return NULL;
-}
-
-void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
-{
-       /* Nothing to do */
-}
-EXPORT_SYMBOL(pci_iomap);
-EXPORT_SYMBOL(pci_iounmap);
-
 unsigned long pci_address_to_pio(phys_addr_t address)
 {
        struct pci_controller* hose = hose_head;