[SPARC64] PCI: Use root list of pbm's instead of pci_controller_info's
[powerpc.git] / arch / sparc64 / kernel / pci.c
index 246b800..c17723f 100644 (file)
@@ -14,7 +14,6 @@
 #include <linux/sched.h>
 #include <linux/capability.h>
 #include <linux/errno.h>
-#include <linux/smp_lock.h>
 #include <linux/msi.h>
 #include <linux/irq.h>
 #include <linux/init.h>
@@ -26,6 +25,9 @@
 #include <asm/ebus.h>
 #include <asm/isa.h>
 #include <asm/prom.h>
+#include <asm/apb.h>
+
+#include "pci_impl.h"
 
 unsigned long pci_memspace_mask = 0xffffffffUL;
 
@@ -46,7 +48,7 @@ asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
 #else
 
 /* List of all PCI controllers found in the system. */
-struct pci_controller_info *pci_controller_root = NULL;
+struct pci_pbm_info *pci_pbm_root = NULL;
 
 /* Each PCI controller found gets a unique index. */
 int pci_num_controllers = 0;
@@ -187,6 +189,7 @@ extern void schizo_init(struct device_node *, const char *);
 extern void schizo_plus_init(struct device_node *, const char *);
 extern void tomatillo_init(struct device_node *, const char *);
 extern void sun4v_pci_init(struct device_node *, const char *);
+extern void fire_pci_init(struct device_node *, const char *);
 
 static struct {
        char *model_name;
@@ -204,6 +207,7 @@ static struct {
        { "SUNW,tomatillo", tomatillo_init },
        { "pci108e,a801", tomatillo_init },
        { "SUNW,sun4v-pci", sun4v_pci_init },
+       { "pciex108e,80f0", fire_pci_init },
 };
 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
                                  sizeof(pci_controller_table[0]))
@@ -279,15 +283,15 @@ int __init pcic_present(void)
        return pci_controller_scan(pci_is_controller);
 }
 
-struct pci_iommu_ops *pci_iommu_ops;
+const struct pci_iommu_ops *pci_iommu_ops;
 EXPORT_SYMBOL(pci_iommu_ops);
 
-extern struct pci_iommu_ops pci_sun4u_iommu_ops,
+extern const struct pci_iommu_ops pci_sun4u_iommu_ops,
        pci_sun4v_iommu_ops;
 
 /* Find each controller in the system, attach and initialize
  * software state structure for each and link into the
- * pci_controller_root.  Setup the controller enough such
+ * pci_pbm_root.  Setup the controller enough such
  * that bus scanning can be done.
  */
 static void __init pci_controller_probe(void)
@@ -365,11 +369,13 @@ static void pci_parse_of_addrs(struct of_device *op,
 
 struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
                                  struct device_node *node,
-                                 struct pci_bus *bus, int devfn)
+                                 struct pci_bus *bus, int devfn,
+                                 int host_controller)
 {
        struct dev_archdata *sd;
        struct pci_dev *dev;
        const char *type;
+       u32 class;
 
        dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
        if (!dev)
@@ -387,7 +393,8 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
        if (type == NULL)
                type = "";
 
-       printk("    create device, devfn: %x, type: %s\n", devfn, type);
+       printk("    create device, devfn: %x, type: %s hostcontroller(%d)\n",
+              devfn, type, host_controller);
 
        dev->bus = bus;
        dev->sysdata = node;
@@ -396,39 +403,70 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
        dev->devfn = devfn;
        dev->multifunction = 0;         /* maybe a lie? */
 
-       dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
-       dev->device = of_getintprop_default(node, "device-id", 0xffff);
-       dev->subsystem_vendor =
-               of_getintprop_default(node, "subsystem-vendor-id", 0);
-       dev->subsystem_device =
-               of_getintprop_default(node, "subsystem-id", 0);
-
-       dev->cfg_size = pci_cfg_space_size(dev);
+       if (host_controller) {
+               dev->vendor = 0x108e;
+               dev->device = 0x8000;
+               dev->subsystem_vendor = 0x0000;
+               dev->subsystem_device = 0x0000;
+               dev->cfg_size = 256;
+               dev->class = PCI_CLASS_BRIDGE_HOST << 8;
+               sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
+                       0x00, PCI_SLOT(devfn), PCI_FUNC(devfn));
+       } else {
+               dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
+               dev->device = of_getintprop_default(node, "device-id", 0xffff);
+               dev->subsystem_vendor =
+                       of_getintprop_default(node, "subsystem-vendor-id", 0);
+               dev->subsystem_device =
+                       of_getintprop_default(node, "subsystem-id", 0);
+
+               dev->cfg_size = pci_cfg_space_size(dev);
+
+               /* We can't actually use the firmware value, we have
+                * to read what is in the register right now.  One
+                * reason is that in the case of IDE interfaces the
+                * firmware can sample the value before the the IDE
+                * interface is programmed into native mode.
+                */
+               pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
+               dev->class = class >> 8;
 
-       sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
-               dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
-       dev->class = of_getintprop_default(node, "class-code", 0);
+               sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
+                       dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
+       }
+       printk("    class: 0x%x device name: %s\n",
+              dev->class, pci_name(dev));
 
-       printk("    class: 0x%x\n", dev->class);
+       /* I have seen IDE devices which will not respond to
+        * the bmdma simplex check reads if bus mastering is
+        * disabled.
+        */
+       if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
+               pci_set_master(dev);
 
        dev->current_state = 4;         /* unknown power state */
        dev->error_state = pci_channel_io_normal;
 
-       if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
-               /* a PCI-PCI bridge */
+       if (host_controller) {
                dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
                dev->rom_base_reg = PCI_ROM_ADDRESS1;
-       } else if (!strcmp(type, "cardbus")) {
-               dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
+               dev->irq = PCI_IRQ_NONE;
        } else {
-               dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
-               dev->rom_base_reg = PCI_ROM_ADDRESS;
+               if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
+                       /* a PCI-PCI bridge */
+                       dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
+                       dev->rom_base_reg = PCI_ROM_ADDRESS1;
+               } else if (!strcmp(type, "cardbus")) {
+                       dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
+               } else {
+                       dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
+                       dev->rom_base_reg = PCI_ROM_ADDRESS;
 
-               dev->irq = sd->op->irqs[0];
-               if (dev->irq == 0xffffffff)
-                       dev->irq = PCI_IRQ_NONE;
+                       dev->irq = sd->op->irqs[0];
+                       if (dev->irq == 0xffffffff)
+                               dev->irq = PCI_IRQ_NONE;
+               }
        }
-
        pci_parse_of_addrs(sd->op, node, dev);
 
        printk("    adding to system ...\n");
@@ -438,19 +476,73 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
        return dev;
 }
 
-static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
-                                  struct device_node *node,
-                                  struct pci_bus *bus);
+static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
+{
+       u32 idx, first, last;
+
+       first = 8;
+       last = 0;
+       for (idx = 0; idx < 8; idx++) {
+               if ((map & (1 << idx)) != 0) {
+                       if (first > idx)
+                               first = idx;
+                       if (last < idx)
+                               last = idx;
+               }
+       }
+
+       *first_p = first;
+       *last_p = last;
+}
+
+static void __init pci_resource_adjust(struct resource *res,
+                                      struct resource *root)
+{
+       res->start += root->start;
+       res->end += root->start;
+}
+
+/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
+ * a proper 'ranges' property.
+ */
+static void __devinit apb_fake_ranges(struct pci_dev *dev,
+                                     struct pci_bus *bus,
+                                     struct pci_pbm_info *pbm)
+{
+       struct resource *res;
+       u32 first, last;
+       u8 map;
+
+       pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
+       apb_calc_first_last(map, &first, &last);
+       res = bus->resource[0];
+       res->start = (first << 21);
+       res->end = (last << 21) + ((1 << 21) - 1);
+       res->flags = IORESOURCE_IO;
+       pci_resource_adjust(res, &pbm->io_space);
+
+       pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
+       apb_calc_first_last(map, &first, &last);
+       res = bus->resource[1];
+       res->start = (first << 21);
+       res->end = (last << 21) + ((1 << 21) - 1);
+       res->flags = IORESOURCE_MEM;
+       pci_resource_adjust(res, &pbm->mem_space);
+}
+
+static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
+                                     struct device_node *node,
+                                     struct pci_bus *bus);
 
 #define GET_64BIT(prop, i)     ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
 
-void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
-                                 struct device_node *node,
-                                 struct pci_dev *dev)
+static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
+                                        struct device_node *node,
+                                        struct pci_dev *dev)
 {
        struct pci_bus *bus;
        const u32 *busrange, *ranges;
-       int len, i;
+       int len, i, simba;
        struct resource *res;
        unsigned int flags;
        u64 size;
@@ -465,10 +557,16 @@ void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
                return;
        }
        ranges = of_get_property(node, "ranges", &len);
+       simba = 0;
        if (ranges == NULL) {
-               printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
-                      node->full_name);
-               return;
+               const char *model = of_get_property(node, "model", NULL);
+               if (model && !strcmp(model, "SUNW,simba")) {
+                       simba = 1;
+               } else {
+                       printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
+                              node->full_name);
+                       return;
+               }
        }
 
        bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
@@ -482,7 +580,7 @@ void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
        bus->subordinate = busrange[1];
        bus->bridge_ctl = 0;
 
-       /* parse ranges property */
+       /* parse ranges property, or cook one up by hand for Simba */
        /* PCI #address-cells == 3 and #size-cells == 2 always */
        res = &dev->resource[PCI_BRIDGE_RESOURCES];
        for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
@@ -490,6 +588,10 @@ void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
                bus->resource[i] = res;
                ++res;
        }
+       if (simba) {
+               apb_fake_ranges(dev, bus, pbm);
+               goto simba_cont;
+       }
        i = 1;
        for (; len >= 32; len -= 32, ranges += 8) {
                struct resource *root;
@@ -525,8 +627,9 @@ void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
                 * layer routine that can calculate a resource for a given
                 * range property value in a PCI device.
                 */
-               pbm->parent->resource_adjust(dev, res, root);
+               pci_resource_adjust(res, root);
        }
+simba_cont:
        sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
                bus->number);
        printk("    bus name: %s\n", bus->name);
@@ -534,9 +637,9 @@ void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
        pci_of_scan_bus(pbm, node, bus);
 }
 
-static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
-                                  struct device_node *node,
-                                  struct pci_bus *bus)
+static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
+                                     struct device_node *node,
+                                     struct pci_bus *bus)
 {
        struct device_node *child;
        const u32 *reg;
@@ -555,7 +658,7 @@ static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
                devfn = (reg[0] >> 8) & 0xff;
 
                /* create a new pci_dev for this device */
-               dev = of_create_pci_dev(pbm, child, bus, devfn);
+               dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
                if (!dev)
                        continue;
                printk("PCI: dev header type: %x\n", dev->hdr_type);
@@ -583,6 +686,7 @@ static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, N
 static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
 {
        struct pci_dev *dev;
+       struct pci_bus *child_bus;
        int err;
 
        list_for_each_entry(dev, &bus->devices, bus_list) {
@@ -595,12 +699,53 @@ static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
                 */
                err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
        }
+       list_for_each_entry(child_bus, &bus->children, node)
+               pci_bus_register_of_sysfs(child_bus);
+}
+
+int pci_host_bridge_read_pci_cfg(struct pci_bus *bus_dev,
+                                unsigned int devfn,
+                                int where, int size,
+                                u32 *value)
+{
+       static u8 fake_pci_config[] = {
+               0x8e, 0x10, /* Vendor: 0x108e (Sun) */
+               0x00, 0x80, /* Device: 0x8000 (PBM) */
+               0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
+               0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
+               0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
+               0x00, /* Cacheline: 0x00 */
+               0x40, /* Latency: 0x40 */
+               0x00, /* Header-Type: 0x00 normal */
+       };
+
+       *value = 0;
+       if (where >= 0 && where < sizeof(fake_pci_config) &&
+           (where + size) >= 0 &&
+           (where + size) < sizeof(fake_pci_config) &&
+           size <= sizeof(u32)) {
+               while (size--) {
+                       *value <<= 8;
+                       *value |= fake_pci_config[where + size];
+               }
+       }
+
+       return PCIBIOS_SUCCESSFUL;
+}
+
+int pci_host_bridge_write_pci_cfg(struct pci_bus *bus_dev,
+                                 unsigned int devfn,
+                                 int where, int size,
+                                 u32 value)
+{
+       return PCIBIOS_SUCCESSFUL;
 }
 
-struct pci_bus * __init pci_scan_one_pbm(struct pci_pbm_info *pbm)
+struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
 {
        struct pci_controller_info *p = pbm->parent;
        struct device_node *node = pbm->prom_node;
+       struct pci_dev *host_pdev;
        struct pci_bus *bus;
 
        printk("PCI: Scanning PBM %s\n", node->full_name);
@@ -618,6 +763,10 @@ struct pci_bus * __init pci_scan_one_pbm(struct pci_pbm_info *pbm)
        bus->resource[0] = &pbm->io_space;
        bus->resource[1] = &pbm->mem_space;
 
+       /* Create the dummy host bridge and link it in.  */
+       host_pdev = of_create_pci_dev(pbm, node, bus, 0x00, 1);
+       bus->self = host_pdev;
+
        pci_of_scan_bus(pbm, node, bus);
        pci_bus_add_devices(bus);
        pci_bus_register_of_sysfs(bus);
@@ -627,10 +776,10 @@ struct pci_bus * __init pci_scan_one_pbm(struct pci_pbm_info *pbm)
 
 static void __init pci_scan_each_controller_bus(void)
 {
-       struct pci_controller_info *p;
+       struct pci_pbm_info *pbm;
 
-       for (p = pci_controller_root; p; p = p->next)
-               p->scan_bus(p);
+       for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
+               pbm->scan_bus(pbm);
 }
 
 extern void power_init(void);
@@ -638,7 +787,7 @@ extern void power_init(void);
 static int __init pcibios_init(void)
 {
        pci_controller_probe();
-       if (pci_controller_root == NULL)
+       if (pci_pbm_root == NULL)
                return 0;
 
        pci_scan_each_controller_bus();
@@ -730,7 +879,7 @@ void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region
        else
                root = &pbm->mem_space;
 
-       pbm->parent->resource_adjust(pdev, &zero_res, root);
+       pci_resource_adjust(&zero_res, root);
 
        region->start = res->start - zero_res.start;
        region->end = res->end - zero_res.start;
@@ -751,7 +900,7 @@ void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
        else
                root = &pbm->mem_space;
 
-       pbm->parent->resource_adjust(pdev, res, root);
+       pci_resource_adjust(res, root);
 }
 EXPORT_SYMBOL(pcibios_bus_to_resource);
 
@@ -777,43 +926,12 @@ static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struc
        unsigned long space_size, user_offset, user_size;
 
        p = pbm->parent;
-       if (p->pbms_same_domain) {
-               unsigned long lowest, highest;
-
-               lowest = ~0UL; highest = 0UL;
-               if (mmap_state == pci_mmap_io) {
-                       if (p->pbm_A.io_space.flags) {
-                               lowest = p->pbm_A.io_space.start;
-                               highest = p->pbm_A.io_space.end + 1;
-                       }
-                       if (p->pbm_B.io_space.flags) {
-                               if (lowest > p->pbm_B.io_space.start)
-                                       lowest = p->pbm_B.io_space.start;
-                               if (highest < p->pbm_B.io_space.end + 1)
-                                       highest = p->pbm_B.io_space.end + 1;
-                       }
-                       space_size = highest - lowest;
-               } else {
-                       if (p->pbm_A.mem_space.flags) {
-                               lowest = p->pbm_A.mem_space.start;
-                               highest = p->pbm_A.mem_space.end + 1;
-                       }
-                       if (p->pbm_B.mem_space.flags) {
-                               if (lowest > p->pbm_B.mem_space.start)
-                                       lowest = p->pbm_B.mem_space.start;
-                               if (highest < p->pbm_B.mem_space.end + 1)
-                                       highest = p->pbm_B.mem_space.end + 1;
-                       }
-                       space_size = highest - lowest;
-               }
+       if (mmap_state == pci_mmap_io) {
+               space_size = (pbm->io_space.end -
+                             pbm->io_space.start) + 1;
        } else {
-               if (mmap_state == pci_mmap_io) {
-                       space_size = (pbm->io_space.end -
-                                     pbm->io_space.start) + 1;
-               } else {
-                       space_size = (pbm->mem_space.end -
-                                     pbm->mem_space.start) + 1;
-               }
+               space_size = (pbm->mem_space.end -
+                             pbm->mem_space.start) + 1;
        }
 
        /* Make sure the request is in range. */
@@ -824,31 +942,12 @@ static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struc
            (user_offset + user_size) > space_size)
                return -EINVAL;
 
-       if (p->pbms_same_domain) {
-               unsigned long lowest = ~0UL;
-
-               if (mmap_state == pci_mmap_io) {
-                       if (p->pbm_A.io_space.flags)
-                               lowest = p->pbm_A.io_space.start;
-                       if (p->pbm_B.io_space.flags &&
-                           lowest > p->pbm_B.io_space.start)
-                               lowest = p->pbm_B.io_space.start;
-               } else {
-                       if (p->pbm_A.mem_space.flags)
-                               lowest = p->pbm_A.mem_space.start;
-                       if (p->pbm_B.mem_space.flags &&
-                           lowest > p->pbm_B.mem_space.start)
-                               lowest = p->pbm_B.mem_space.start;
-               }
-               vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
+       if (mmap_state == pci_mmap_io) {
+               vma->vm_pgoff = (pbm->io_space.start +
+                                user_offset) >> PAGE_SHIFT;
        } else {
-               if (mmap_state == pci_mmap_io) {
-                       vma->vm_pgoff = (pbm->io_space.start +
-                                        user_offset) >> PAGE_SHIFT;
-               } else {
-                       vma->vm_pgoff = (pbm->mem_space.start +
-                                        user_offset) >> PAGE_SHIFT;
-               }
+               vma->vm_pgoff = (pbm->mem_space.start +
+                                user_offset) >> PAGE_SHIFT;
        }
 
        return 0;
@@ -982,9 +1081,8 @@ int pci_domain_nr(struct pci_bus *pbus)
                struct pci_controller_info *p = pbm->parent;
 
                ret = p->index;
-               if (p->pbms_same_domain == 0)
-                       ret = ((ret << 1) +
-                              ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
+               ret = ((ret << 1) +
+                      ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
        }
 
        return ret;
@@ -1002,10 +1100,10 @@ int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
                return -EINVAL;
 
        err = p->setup_msi_irq(&virt_irq, pdev, desc);
-       if (err < 0)
+       if (err)
                return err;
 
-       return virt_irq;
+       return 0;
 }
 
 void arch_teardown_msi_irq(unsigned int virt_irq)