Merge branch 'remotes/lorenzo/pci/mobiveil'
[linux-2.6-microblaze.git] / drivers / pci / controller / dwc / pcie-designware-host.c
index b626cc7..674f32d 100644 (file)
 #include "pcie-designware.h"
 
 static struct pci_ops dw_pcie_ops;
-
-static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
-                              u32 *val)
-{
-       struct dw_pcie *pci;
-
-       if (pp->ops->rd_own_conf)
-               return pp->ops->rd_own_conf(pp, where, size, val);
-
-       pci = to_dw_pcie_from_pp(pp);
-       return dw_pcie_read(pci->dbi_base + where, size, val);
-}
-
-static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
-                              u32 val)
-{
-       struct dw_pcie *pci;
-
-       if (pp->ops->wr_own_conf)
-               return pp->ops->wr_own_conf(pp, where, size, val);
-
-       pci = to_dw_pcie_from_pp(pp);
-       return dw_pcie_write(pci->dbi_base + where, size, val);
-}
+static struct pci_ops dw_child_pcie_ops;
 
 static void dw_msi_ack_irq(struct irq_data *d)
 {
@@ -289,26 +266,22 @@ void dw_pcie_free_msi(struct pcie_port *pp)
        irq_domain_remove(pp->msi_domain);
        irq_domain_remove(pp->irq_domain);
 
-       if (pp->msi_page)
-               __free_page(pp->msi_page);
+       if (pp->msi_data) {
+               struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+               struct device *dev = pci->dev;
+
+               dma_unmap_single_attrs(dev, pp->msi_data, sizeof(pp->msi_msg),
+                                      DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
+       }
 }
 
 void dw_pcie_msi_init(struct pcie_port *pp)
 {
        struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
-       struct device *dev = pci->dev;
-       u64 msi_target;
+       u64 msi_target = (u64)pp->msi_data;
 
-       pp->msi_page = alloc_page(GFP_KERNEL);
-       pp->msi_data = dma_map_page(dev, pp->msi_page, 0, PAGE_SIZE,
-                                   DMA_FROM_DEVICE);
-       if (dma_mapping_error(dev, pp->msi_data)) {
-               dev_err(dev, "Failed to map MSI data\n");
-               __free_page(pp->msi_page);
-               pp->msi_page = NULL;
+       if (!IS_ENABLED(CONFIG_PCI_MSI))
                return;
-       }
-       msi_target = (u64)pp->msi_data;
 
        /* Program the msi_data */
        dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target));
@@ -323,7 +296,6 @@ int dw_pcie_host_init(struct pcie_port *pp)
        struct device_node *np = dev->of_node;
        struct platform_device *pdev = to_platform_device(dev);
        struct resource_entry *win;
-       struct pci_bus *child;
        struct pci_host_bridge *bridge;
        struct resource *cfg_res;
        int ret;
@@ -332,10 +304,8 @@ int dw_pcie_host_init(struct pcie_port *pp)
 
        cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
        if (cfg_res) {
-               pp->cfg0_size = resource_size(cfg_res) >> 1;
-               pp->cfg1_size = resource_size(cfg_res) >> 1;
+               pp->cfg0_size = resource_size(cfg_res);
                pp->cfg0_base = cfg_res->start;
-               pp->cfg1_base = cfg_res->start + pp->cfg0_size;
        } else if (!pp->va_cfg0_base) {
                dev_err(dev, "Missing *config* reg space\n");
        }
@@ -350,43 +320,27 @@ int dw_pcie_host_init(struct pcie_port *pp)
        resource_list_for_each_entry(win, &bridge->windows) {
                switch (resource_type(win->res)) {
                case IORESOURCE_IO:
-                       pp->io = win->res;
-                       pp->io->name = "I/O";
-                       pp->io_size = resource_size(pp->io);
-                       pp->io_bus_addr = pp->io->start - win->offset;
-                       pp->io_base = pci_pio_to_address(pp->io->start);
-                       break;
-               case IORESOURCE_MEM:
-                       pp->mem = win->res;
-                       pp->mem->name = "MEM";
-                       pp->mem_size = resource_size(pp->mem);
-                       pp->mem_bus_addr = pp->mem->start - win->offset;
+                       pp->io_size = resource_size(win->res);
+                       pp->io_bus_addr = win->res->start - win->offset;
+                       pp->io_base = pci_pio_to_address(win->res->start);
                        break;
                case 0:
-                       pp->cfg = win->res;
-                       pp->cfg0_size = resource_size(pp->cfg) >> 1;
-                       pp->cfg1_size = resource_size(pp->cfg) >> 1;
-                       pp->cfg0_base = pp->cfg->start;
-                       pp->cfg1_base = pp->cfg->start + pp->cfg0_size;
-                       break;
-               case IORESOURCE_BUS:
-                       pp->busn = win->res;
+                       dev_err(dev, "Missing *config* reg space\n");
+                       pp->cfg0_size = resource_size(win->res);
+                       pp->cfg0_base = win->res->start;
+                       if (!pci->dbi_base) {
+                               pci->dbi_base = devm_pci_remap_cfgspace(dev,
+                                                               pp->cfg0_base,
+                                                               pp->cfg0_size);
+                               if (!pci->dbi_base) {
+                                       dev_err(dev, "Error with ioremap\n");
+                                       return -ENOMEM;
+                               }
+                       }
                        break;
                }
        }
 
-       if (!pci->dbi_base) {
-               pci->dbi_base = devm_pci_remap_cfgspace(dev,
-                                               pp->cfg->start,
-                                               resource_size(pp->cfg));
-               if (!pci->dbi_base) {
-                       dev_err(dev, "Error with ioremap\n");
-                       return -ENOMEM;
-               }
-       }
-
-       pp->mem_base = pp->mem->start;
-
        if (!pp->va_cfg0_base) {
                pp->va_cfg0_base = devm_pci_remap_cfgspace(dev,
                                        pp->cfg0_base, pp->cfg0_size);
@@ -396,20 +350,13 @@ int dw_pcie_host_init(struct pcie_port *pp)
                }
        }
 
-       if (!pp->va_cfg1_base) {
-               pp->va_cfg1_base = devm_pci_remap_cfgspace(dev,
-                                               pp->cfg1_base,
-                                               pp->cfg1_size);
-               if (!pp->va_cfg1_base) {
-                       dev_err(dev, "Error with ioremap\n");
-                       return -ENOMEM;
-               }
-       }
-
        ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport);
        if (ret)
                pci->num_viewport = 2;
 
+       if (pci->link_gen < 1)
+               pci->link_gen = of_pci_get_max_link_speed(np);
+
        if (pci_msi_enabled()) {
                /*
                 * If a specific SoC driver needs to change the
@@ -440,6 +387,16 @@ int dw_pcie_host_init(struct pcie_port *pp)
                                irq_set_chained_handler_and_data(pp->msi_irq,
                                                            dw_chained_msi_isr,
                                                            pp);
+
+                       pp->msi_data = dma_map_single_attrs(pci->dev, &pp->msi_msg,
+                                                     sizeof(pp->msi_msg),
+                                                     DMA_FROM_DEVICE,
+                                                     DMA_ATTR_SKIP_CPU_SYNC);
+                       if (dma_mapping_error(pci->dev, pp->msi_data)) {
+                               dev_err(pci->dev, "Failed to map MSI data\n");
+                               pp->msi_data = 0;
+                               goto err_free_msi;
+                       }
                } else {
                        ret = pp->ops->msi_host_init(pp);
                        if (ret < 0)
@@ -449,7 +406,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 
        /* Set default bus ops */
        bridge->ops = &dw_pcie_ops;
-       bridge->child_ops = &dw_pcie_ops;
+       bridge->child_ops = &dw_child_pcie_ops;
 
        if (pp->ops->host_init) {
                ret = pp->ops->host_init(pp);
@@ -459,23 +416,9 @@ int dw_pcie_host_init(struct pcie_port *pp)
 
        bridge->sysdata = pp;
 
-       ret = pci_scan_root_bus_bridge(bridge);
-       if (ret)
-               goto err_free_msi;
-
-       pp->root_bus = bridge->bus;
-
-       if (pp->ops->scan_bus)
-               pp->ops->scan_bus(pp);
-
-       pci_bus_size_bridges(pp->root_bus);
-       pci_bus_assign_resources(pp->root_bus);
-
-       list_for_each_entry(child, &pp->root_bus->children, node)
-               pcie_bus_configure_settings(child);
-
-       pci_bus_add_devices(pp->root_bus);
-       return 0;
+       ret = pci_host_probe(bridge);
+       if (!ret)
+               return 0;
 
 err_free_msi:
        if (pci_msi_enabled() && !pp->ops->msi_host_init)
@@ -486,125 +429,104 @@ EXPORT_SYMBOL_GPL(dw_pcie_host_init);
 
 void dw_pcie_host_deinit(struct pcie_port *pp)
 {
-       pci_stop_root_bus(pp->root_bus);
-       pci_remove_root_bus(pp->root_bus);
+       pci_stop_root_bus(pp->bridge->bus);
+       pci_remove_root_bus(pp->bridge->bus);
        if (pci_msi_enabled() && !pp->ops->msi_host_init)
                dw_pcie_free_msi(pp);
 }
 EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
 
-static int dw_pcie_access_other_conf(struct pcie_port *pp, struct pci_bus *bus,
-                                    u32 devfn, int where, int size, u32 *val,
-                                    bool write)
+static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus,
+                                               unsigned int devfn, int where)
 {
-       int ret, type;
-       u32 busdev, cfg_size;
-       u64 cpu_addr;
-       void __iomem *va_cfg_base;
+       int type;
+       u32 busdev;
+       struct pcie_port *pp = bus->sysdata;
        struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 
+       /*
+        * Checking whether the link is up here is a last line of defense
+        * against platforms that forward errors on the system bus as
+        * SError upon PCI configuration transactions issued when the link
+        * is down. This check is racy by definition and does not stop
+        * the system from triggering an SError if the link goes down
+        * after this check is performed.
+        */
+       if (!dw_pcie_link_up(pci))
+               return NULL;
+
        busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
                 PCIE_ATU_FUNC(PCI_FUNC(devfn));
 
-       if (pci_is_root_bus(bus->parent)) {
+       if (pci_is_root_bus(bus->parent))
                type = PCIE_ATU_TYPE_CFG0;
-               cpu_addr = pp->cfg0_base;
-               cfg_size = pp->cfg0_size;
-               va_cfg_base = pp->va_cfg0_base;
-       } else {
-               type = PCIE_ATU_TYPE_CFG1;
-               cpu_addr = pp->cfg1_base;
-               cfg_size = pp->cfg1_size;
-               va_cfg_base = pp->va_cfg1_base;
-       }
-
-       dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
-                                 type, cpu_addr,
-                                 busdev, cfg_size);
-       if (write)
-               ret = dw_pcie_write(va_cfg_base + where, size, *val);
        else
-               ret = dw_pcie_read(va_cfg_base + where, size, val);
-
-       if (pci->num_viewport <= 2)
-               dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
-                                         PCIE_ATU_TYPE_IO, pp->io_base,
-                                         pp->io_bus_addr, pp->io_size);
-
-       return ret;
-}
-
-static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
-                                u32 devfn, int where, int size, u32 *val)
-{
-       if (pp->ops->rd_other_conf)
-               return pp->ops->rd_other_conf(pp, bus, devfn, where,
-                                             size, val);
+               type = PCIE_ATU_TYPE_CFG1;
 
-       return dw_pcie_access_other_conf(pp, bus, devfn, where, size, val,
-                                        false);
-}
 
-static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
-                                u32 devfn, int where, int size, u32 val)
-{
-       if (pp->ops->wr_other_conf)
-               return pp->ops->wr_other_conf(pp, bus, devfn, where,
-                                             size, val);
+       dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
+                                 type, pp->cfg0_base,
+                                 busdev, pp->cfg0_size);
 
-       return dw_pcie_access_other_conf(pp, bus, devfn, where, size, &val,
-                                        true);
+       return pp->va_cfg0_base + where;
 }
 
-static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
-                               int dev)
+static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn,
+                                int where, int size, u32 *val)
 {
+       int ret;
+       struct pcie_port *pp = bus->sysdata;
        struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 
-       /* If there is no link, then there is no device */
-       if (!pci_is_root_bus(bus)) {
-               if (!dw_pcie_link_up(pci))
-                       return 0;
-       } else if (dev > 0)
-               /* Access only one slot on each root port */
-               return 0;
+       ret = pci_generic_config_read(bus, devfn, where, size, val);
+
+       if (!ret && pci->num_viewport <= 2)
+               dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
+                                         PCIE_ATU_TYPE_IO, pp->io_base,
+                                         pp->io_bus_addr, pp->io_size);
 
-       return 1;
+       return ret;
 }
 
-static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
-                          int size, u32 *val)
+static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn,
+                                int where, int size, u32 val)
 {
+       int ret;
        struct pcie_port *pp = bus->sysdata;
+       struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 
-       if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
-               *val = 0xffffffff;
-               return PCIBIOS_DEVICE_NOT_FOUND;
-       }
+       ret = pci_generic_config_write(bus, devfn, where, size, val);
 
-       if (pci_is_root_bus(bus))
-               return dw_pcie_rd_own_conf(pp, where, size, val);
+       if (!ret && pci->num_viewport <= 2)
+               dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
+                                         PCIE_ATU_TYPE_IO, pp->io_base,
+                                         pp->io_bus_addr, pp->io_size);
 
-       return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val);
+       return ret;
 }
 
-static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
-                          int where, int size, u32 val)
+static struct pci_ops dw_child_pcie_ops = {
+       .map_bus = dw_pcie_other_conf_map_bus,
+       .read = dw_pcie_rd_other_conf,
+       .write = dw_pcie_wr_other_conf,
+};
+
+void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
 {
        struct pcie_port *pp = bus->sysdata;
+       struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 
-       if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
-               return PCIBIOS_DEVICE_NOT_FOUND;
-
-       if (pci_is_root_bus(bus))
-               return dw_pcie_wr_own_conf(pp, where, size, val);
+       if (PCI_SLOT(devfn) > 0)
+               return NULL;
 
-       return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val);
+       return pci->dbi_base + where;
 }
+EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus);
 
 static struct pci_ops dw_pcie_ops = {
-       .read = dw_pcie_rd_conf,
-       .write = dw_pcie_wr_conf,
+       .map_bus = dw_pcie_own_conf_map_bus,
+       .read = pci_generic_config_read,
+       .write = pci_generic_config_write,
 };
 
 void dw_pcie_setup_rc(struct pcie_port *pp)
@@ -620,7 +542,7 @@ void dw_pcie_setup_rc(struct pcie_port *pp)
 
        dw_pcie_setup(pci);
 
-       if (!pp->ops->msi_host_init) {
+       if (pci_msi_enabled() && !pp->ops->msi_host_init) {
                num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
 
                /* Initialize IRQ Status array */
@@ -663,10 +585,14 @@ void dw_pcie_setup_rc(struct pcie_port *pp)
         * the platform uses its own address translation component rather than
         * ATU, so we should not program the ATU here.
         */
-       if (pp->bridge->child_ops == &dw_pcie_ops && !pp->ops->rd_other_conf) {
+       if (pp->bridge->child_ops == &dw_child_pcie_ops) {
+               struct resource_entry *entry =
+                       resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM);
+
                dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0,
-                                         PCIE_ATU_TYPE_MEM, pp->mem_base,
-                                         pp->mem_bus_addr, pp->mem_size);
+                                         PCIE_ATU_TYPE_MEM, entry->res->start,
+                                         entry->res->start - entry->offset,
+                                         resource_size(entry->res));
                if (pci->num_viewport > 2)
                        dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2,
                                                  PCIE_ATU_TYPE_IO, pp->io_base,