aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/base/dma-coherent.c10
-rw-r--r--drivers/base/dma-mapping.c6
-rw-r--r--drivers/block/nvme-core.c11
-rw-r--r--drivers/edac/i82875p_edac.c8
-rw-r--r--drivers/iommu/exynos-iommu.c14
-rw-r--r--drivers/misc/genwqe/card_utils.c2
-rw-r--r--drivers/pci/access.c12
-rw-r--r--drivers/pci/bus.c11
-rw-r--r--drivers/pci/host-bridge.c1
-rw-r--r--drivers/pci/host/Kconfig13
-rw-r--r--drivers/pci/host/Makefile2
-rw-r--r--drivers/pci/host/pci-exynos.c11
-rw-r--r--drivers/pci/host/pci-host-generic.c388
-rw-r--r--drivers/pci/host/pci-imx6.c147
-rw-r--r--drivers/pci/host/pci-rcar-gen2.c31
-rw-r--r--drivers/pci/host/pcie-designware.c6
-rw-r--r--drivers/pci/host/pcie-designware.h2
-rw-r--r--drivers/pci/host/pcie-rcar.c1008
-rw-r--r--drivers/pci/hotplug-pci.c2
-rw-r--r--drivers/pci/hotplug/acpiphp_glue.c6
-rw-r--r--drivers/pci/hotplug/cpci_hotplug_pci.c5
-rw-r--r--drivers/pci/hotplug/cpqphp_ctrl.c3
-rw-r--r--drivers/pci/hotplug/cpqphp_nvram.c1
-rw-r--r--drivers/pci/hotplug/pciehp.h2
-rw-r--r--drivers/pci/hotplug/pciehp_hpc.c2
-rw-r--r--drivers/pci/hotplug/pciehp_pci.c3
-rw-r--r--drivers/pci/hotplug/pcihp_slot.c3
-rw-r--r--drivers/pci/hotplug/rpadlpar_core.c3
-rw-r--r--drivers/pci/hotplug/rpaphp_core.c15
-rw-r--r--drivers/pci/hotplug/s390_pci_hpc.c1
-rw-r--r--drivers/pci/hotplug/shpchp_pci.c5
-rw-r--r--drivers/pci/iov.c2
-rw-r--r--drivers/pci/msi.c96
-rw-r--r--drivers/pci/pci-acpi.c8
-rw-r--r--drivers/pci/pci-driver.c58
-rw-r--r--drivers/pci/pci-sysfs.c68
-rw-r--r--drivers/pci/pci.c34
-rw-r--r--drivers/pci/pci.h10
-rw-r--r--drivers/pci/pcie/portdrv_core.c9
-rw-r--r--drivers/pci/probe.c101
-rw-r--r--drivers/pci/quirks.c11
-rw-r--r--drivers/pci/search.c1
-rw-r--r--drivers/pci/setup-bus.c251
-rw-r--r--drivers/pci/setup-irq.c1
-rw-r--r--drivers/pci/setup-res.c42
-rw-r--r--drivers/pcmcia/cardbus.c3
-rw-r--r--drivers/platform/x86/asus-wmi.c3
-rw-r--r--drivers/platform/x86/eeepc-laptop.c3
48 files changed, 2041 insertions, 394 deletions
diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c
index bc256b641027..7d6e84a51424 100644
--- a/drivers/base/dma-coherent.c
+++ b/drivers/base/dma-coherent.c
@@ -10,13 +10,13 @@
10struct dma_coherent_mem { 10struct dma_coherent_mem {
11 void *virt_base; 11 void *virt_base;
12 dma_addr_t device_base; 12 dma_addr_t device_base;
13 phys_addr_t pfn_base; 13 unsigned long pfn_base;
14 int size; 14 int size;
15 int flags; 15 int flags;
16 unsigned long *bitmap; 16 unsigned long *bitmap;
17}; 17};
18 18
19int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr, 19int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
20 dma_addr_t device_addr, size_t size, int flags) 20 dma_addr_t device_addr, size_t size, int flags)
21{ 21{
22 void __iomem *mem_base = NULL; 22 void __iomem *mem_base = NULL;
@@ -32,7 +32,7 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
32 32
33 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */ 33 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
34 34
35 mem_base = ioremap(bus_addr, size); 35 mem_base = ioremap(phys_addr, size);
36 if (!mem_base) 36 if (!mem_base)
37 goto out; 37 goto out;
38 38
@@ -45,7 +45,7 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
45 45
46 dev->dma_mem->virt_base = mem_base; 46 dev->dma_mem->virt_base = mem_base;
47 dev->dma_mem->device_base = device_addr; 47 dev->dma_mem->device_base = device_addr;
48 dev->dma_mem->pfn_base = PFN_DOWN(bus_addr); 48 dev->dma_mem->pfn_base = PFN_DOWN(phys_addr);
49 dev->dma_mem->size = pages; 49 dev->dma_mem->size = pages;
50 dev->dma_mem->flags = flags; 50 dev->dma_mem->flags = flags;
51 51
@@ -208,7 +208,7 @@ int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
208 208
209 *ret = -ENXIO; 209 *ret = -ENXIO;
210 if (off < count && user_count <= count - off) { 210 if (off < count && user_count <= count - off) {
211 unsigned pfn = mem->pfn_base + start + off; 211 unsigned long pfn = mem->pfn_base + start + off;
212 *ret = remap_pfn_range(vma, vma->vm_start, pfn, 212 *ret = remap_pfn_range(vma, vma->vm_start, pfn,
213 user_count << PAGE_SHIFT, 213 user_count << PAGE_SHIFT,
214 vma->vm_page_prot); 214 vma->vm_page_prot);
diff --git a/drivers/base/dma-mapping.c b/drivers/base/dma-mapping.c
index 0ce39a33b3c2..6cd08e145bfa 100644
--- a/drivers/base/dma-mapping.c
+++ b/drivers/base/dma-mapping.c
@@ -175,7 +175,7 @@ static void dmam_coherent_decl_release(struct device *dev, void *res)
175/** 175/**
176 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory() 176 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
177 * @dev: Device to declare coherent memory for 177 * @dev: Device to declare coherent memory for
178 * @bus_addr: Bus address of coherent memory to be declared 178 * @phys_addr: Physical address of coherent memory to be declared
179 * @device_addr: Device address of coherent memory to be declared 179 * @device_addr: Device address of coherent memory to be declared
180 * @size: Size of coherent memory to be declared 180 * @size: Size of coherent memory to be declared
181 * @flags: Flags 181 * @flags: Flags
@@ -185,7 +185,7 @@ static void dmam_coherent_decl_release(struct device *dev, void *res)
185 * RETURNS: 185 * RETURNS:
186 * 0 on success, -errno on failure. 186 * 0 on success, -errno on failure.
187 */ 187 */
188int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr, 188int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
189 dma_addr_t device_addr, size_t size, int flags) 189 dma_addr_t device_addr, size_t size, int flags)
190{ 190{
191 void *res; 191 void *res;
@@ -195,7 +195,7 @@ int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
195 if (!res) 195 if (!res)
196 return -ENOMEM; 196 return -ENOMEM;
197 197
198 rc = dma_declare_coherent_memory(dev, bus_addr, device_addr, size, 198 rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
199 flags); 199 flags);
200 if (rc == 0) 200 if (rc == 0)
201 devres_add(dev, res); 201 devres_add(dev, res);
diff --git a/drivers/block/nvme-core.c b/drivers/block/nvme-core.c
index 7c64fa756cce..a842c71dcc21 100644
--- a/drivers/block/nvme-core.c
+++ b/drivers/block/nvme-core.c
@@ -2775,6 +2775,16 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2775 return result; 2775 return result;
2776} 2776}
2777 2777
2778static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2779{
2780 struct nvme_dev *dev = pci_get_drvdata(pdev);
2781
2782 if (prepare)
2783 nvme_dev_shutdown(dev);
2784 else
2785 nvme_dev_resume(dev);
2786}
2787
2778static void nvme_shutdown(struct pci_dev *pdev) 2788static void nvme_shutdown(struct pci_dev *pdev)
2779{ 2789{
2780 struct nvme_dev *dev = pci_get_drvdata(pdev); 2790 struct nvme_dev *dev = pci_get_drvdata(pdev);
@@ -2839,6 +2849,7 @@ static const struct pci_error_handlers nvme_err_handler = {
2839 .link_reset = nvme_link_reset, 2849 .link_reset = nvme_link_reset,
2840 .slot_reset = nvme_slot_reset, 2850 .slot_reset = nvme_slot_reset,
2841 .resume = nvme_error_resume, 2851 .resume = nvme_error_resume,
2852 .reset_notify = nvme_reset_notify,
2842}; 2853};
2843 2854
2844/* Move to pci_ids.h later */ 2855/* Move to pci_ids.h later */
diff --git a/drivers/edac/i82875p_edac.c b/drivers/edac/i82875p_edac.c
index 8d0450b9b9af..64b68320249f 100644
--- a/drivers/edac/i82875p_edac.c
+++ b/drivers/edac/i82875p_edac.c
@@ -275,7 +275,6 @@ static int i82875p_setup_overfl_dev(struct pci_dev *pdev,
275{ 275{
276 struct pci_dev *dev; 276 struct pci_dev *dev;
277 void __iomem *window; 277 void __iomem *window;
278 int err;
279 278
280 *ovrfl_pdev = NULL; 279 *ovrfl_pdev = NULL;
281 *ovrfl_window = NULL; 280 *ovrfl_window = NULL;
@@ -293,13 +292,8 @@ static int i82875p_setup_overfl_dev(struct pci_dev *pdev,
293 if (dev == NULL) 292 if (dev == NULL)
294 return 1; 293 return 1;
295 294
296 err = pci_bus_add_device(dev);
297 if (err) {
298 i82875p_printk(KERN_ERR,
299 "%s(): pci_bus_add_device() Failed\n",
300 __func__);
301 }
302 pci_bus_assign_resources(dev->bus); 295 pci_bus_assign_resources(dev->bus);
296 pci_bus_add_device(dev);
303 } 297 }
304 298
305 *ovrfl_pdev = dev; 299 *ovrfl_pdev = dev;
diff --git a/drivers/iommu/exynos-iommu.c b/drivers/iommu/exynos-iommu.c
index 074018979cdf..2ca0744b0a45 100644
--- a/drivers/iommu/exynos-iommu.c
+++ b/drivers/iommu/exynos-iommu.c
@@ -1011,13 +1011,13 @@ static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *domain,
1011} 1011}
1012 1012
1013static struct iommu_ops exynos_iommu_ops = { 1013static struct iommu_ops exynos_iommu_ops = {
1014 .domain_init = &exynos_iommu_domain_init, 1014 .domain_init = exynos_iommu_domain_init,
1015 .domain_destroy = &exynos_iommu_domain_destroy, 1015 .domain_destroy = exynos_iommu_domain_destroy,
1016 .attach_dev = &exynos_iommu_attach_device, 1016 .attach_dev = exynos_iommu_attach_device,
1017 .detach_dev = &exynos_iommu_detach_device, 1017 .detach_dev = exynos_iommu_detach_device,
1018 .map = &exynos_iommu_map, 1018 .map = exynos_iommu_map,
1019 .unmap = &exynos_iommu_unmap, 1019 .unmap = exynos_iommu_unmap,
1020 .iova_to_phys = &exynos_iommu_iova_to_phys, 1020 .iova_to_phys = exynos_iommu_iova_to_phys,
1021 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE, 1021 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1022}; 1022};
1023 1023
diff --git a/drivers/misc/genwqe/card_utils.c b/drivers/misc/genwqe/card_utils.c
index d049d271699c..c00adfaa6279 100644
--- a/drivers/misc/genwqe/card_utils.c
+++ b/drivers/misc/genwqe/card_utils.c
@@ -718,7 +718,7 @@ int genwqe_set_interrupt_capability(struct genwqe_dev *cd, int count)
718 int rc; 718 int rc;
719 struct pci_dev *pci_dev = cd->pci_dev; 719 struct pci_dev *pci_dev = cd->pci_dev;
720 720
721 rc = pci_enable_msi_block(pci_dev, count); 721 rc = pci_enable_msi_exact(pci_dev, count);
722 if (rc == 0) 722 if (rc == 0)
723 cd->flags |= GENWQE_FLAG_MSI_ENABLED; 723 cd->flags |= GENWQE_FLAG_MSI_ENABLED;
724 return rc; 724 return rc;
diff --git a/drivers/pci/access.c b/drivers/pci/access.c
index 7f8b78c08879..8c148f39e8d7 100644
--- a/drivers/pci/access.c
+++ b/drivers/pci/access.c
@@ -148,7 +148,7 @@ static noinline void pci_wait_cfg(struct pci_dev *dev)
148int pci_user_read_config_##size \ 148int pci_user_read_config_##size \
149 (struct pci_dev *dev, int pos, type *val) \ 149 (struct pci_dev *dev, int pos, type *val) \
150{ \ 150{ \
151 int ret = 0; \ 151 int ret = PCIBIOS_SUCCESSFUL; \
152 u32 data = -1; \ 152 u32 data = -1; \
153 if (PCI_##size##_BAD) \ 153 if (PCI_##size##_BAD) \
154 return -EINVAL; \ 154 return -EINVAL; \
@@ -159,9 +159,7 @@ int pci_user_read_config_##size \
159 pos, sizeof(type), &data); \ 159 pos, sizeof(type), &data); \
160 raw_spin_unlock_irq(&pci_lock); \ 160 raw_spin_unlock_irq(&pci_lock); \
161 *val = (type)data; \ 161 *val = (type)data; \
162 if (ret > 0) \ 162 return pcibios_err_to_errno(ret); \
163 ret = -EINVAL; \
164 return ret; \
165} \ 163} \
166EXPORT_SYMBOL_GPL(pci_user_read_config_##size); 164EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
167 165
@@ -170,7 +168,7 @@ EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
170int pci_user_write_config_##size \ 168int pci_user_write_config_##size \
171 (struct pci_dev *dev, int pos, type val) \ 169 (struct pci_dev *dev, int pos, type val) \
172{ \ 170{ \
173 int ret = -EIO; \ 171 int ret = PCIBIOS_SUCCESSFUL; \
174 if (PCI_##size##_BAD) \ 172 if (PCI_##size##_BAD) \
175 return -EINVAL; \ 173 return -EINVAL; \
176 raw_spin_lock_irq(&pci_lock); \ 174 raw_spin_lock_irq(&pci_lock); \
@@ -179,9 +177,7 @@ int pci_user_write_config_##size \
179 ret = dev->bus->ops->write(dev->bus, dev->devfn, \ 177 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
180 pos, sizeof(type), val); \ 178 pos, sizeof(type), val); \
181 raw_spin_unlock_irq(&pci_lock); \ 179 raw_spin_unlock_irq(&pci_lock); \
182 if (ret > 0) \ 180 return pcibios_err_to_errno(ret); \
183 ret = -EINVAL; \
184 return ret; \
185} \ 181} \
186EXPORT_SYMBOL_GPL(pci_user_write_config_##size); 182EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
187 183
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index fb8aed307c28..447d393725e1 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -13,7 +13,6 @@
13#include <linux/errno.h> 13#include <linux/errno.h>
14#include <linux/ioport.h> 14#include <linux/ioport.h>
15#include <linux/proc_fs.h> 15#include <linux/proc_fs.h>
16#include <linux/init.h>
17#include <linux/slab.h> 16#include <linux/slab.h>
18 17
19#include "pci.h" 18#include "pci.h"
@@ -236,7 +235,7 @@ void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
236 * 235 *
237 * This adds add sysfs entries and start device drivers 236 * This adds add sysfs entries and start device drivers
238 */ 237 */
239int pci_bus_add_device(struct pci_dev *dev) 238void pci_bus_add_device(struct pci_dev *dev)
240{ 239{
241 int retval; 240 int retval;
242 241
@@ -253,8 +252,6 @@ int pci_bus_add_device(struct pci_dev *dev)
253 WARN_ON(retval < 0); 252 WARN_ON(retval < 0);
254 253
255 dev->is_added = 1; 254 dev->is_added = 1;
256
257 return 0;
258} 255}
259 256
260/** 257/**
@@ -267,16 +264,12 @@ void pci_bus_add_devices(const struct pci_bus *bus)
267{ 264{
268 struct pci_dev *dev; 265 struct pci_dev *dev;
269 struct pci_bus *child; 266 struct pci_bus *child;
270 int retval;
271 267
272 list_for_each_entry(dev, &bus->devices, bus_list) { 268 list_for_each_entry(dev, &bus->devices, bus_list) {
273 /* Skip already-added devices */ 269 /* Skip already-added devices */
274 if (dev->is_added) 270 if (dev->is_added)
275 continue; 271 continue;
276 retval = pci_bus_add_device(dev); 272 pci_bus_add_device(dev);
277 if (retval)
278 dev_err(&dev->dev, "Error adding device (%d)\n",
279 retval);
280 } 273 }
281 274
282 list_for_each_entry(dev, &bus->devices, bus_list) { 275 list_for_each_entry(dev, &bus->devices, bus_list) {
diff --git a/drivers/pci/host-bridge.c b/drivers/pci/host-bridge.c
index 47aaf22d814e..0e5f3c95af5b 100644
--- a/drivers/pci/host-bridge.c
+++ b/drivers/pci/host-bridge.c
@@ -3,7 +3,6 @@
3 */ 3 */
4 4
5#include <linux/kernel.h> 5#include <linux/kernel.h>
6#include <linux/init.h>
7#include <linux/pci.h> 6#include <linux/pci.h>
8#include <linux/module.h> 7#include <linux/module.h>
9 8
diff --git a/drivers/pci/host/Kconfig b/drivers/pci/host/Kconfig
index a6f67ec8882f..21df477be0c8 100644
--- a/drivers/pci/host/Kconfig
+++ b/drivers/pci/host/Kconfig
@@ -33,4 +33,17 @@ config PCI_RCAR_GEN2
33 There are 3 internal PCI controllers available with a single 33 There are 3 internal PCI controllers available with a single
34 built-in EHCI/OHCI host controller present on each one. 34 built-in EHCI/OHCI host controller present on each one.
35 35
36config PCI_RCAR_GEN2_PCIE
37 bool "Renesas R-Car PCIe controller"
38 depends on ARCH_SHMOBILE || (ARM && COMPILE_TEST)
39 help
40 Say Y here if you want PCIe controller support on R-Car Gen2 SoCs.
41
42config PCI_HOST_GENERIC
43 bool "Generic PCI host controller"
44 depends on ARM && OF
45 help
46 Say Y here if you want to support a simple generic PCI host
47 controller, such as the one emulated by kvmtool.
48
36endmenu 49endmenu
diff --git a/drivers/pci/host/Makefile b/drivers/pci/host/Makefile
index 13fb3333aa05..611ba4b48c94 100644
--- a/drivers/pci/host/Makefile
+++ b/drivers/pci/host/Makefile
@@ -4,3 +4,5 @@ obj-$(CONFIG_PCI_IMX6) += pci-imx6.o
4obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o 4obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o
5obj-$(CONFIG_PCI_TEGRA) += pci-tegra.o 5obj-$(CONFIG_PCI_TEGRA) += pci-tegra.o
6obj-$(CONFIG_PCI_RCAR_GEN2) += pci-rcar-gen2.o 6obj-$(CONFIG_PCI_RCAR_GEN2) += pci-rcar-gen2.o
7obj-$(CONFIG_PCI_RCAR_GEN2_PCIE) += pcie-rcar.o
8obj-$(CONFIG_PCI_HOST_GENERIC) += pci-host-generic.o
diff --git a/drivers/pci/host/pci-exynos.c b/drivers/pci/host/pci-exynos.c
index 3de6bfbbe8e9..1632661c5b7f 100644
--- a/drivers/pci/host/pci-exynos.c
+++ b/drivers/pci/host/pci-exynos.c
@@ -415,9 +415,7 @@ static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
415{ 415{
416 struct pcie_port *pp = arg; 416 struct pcie_port *pp = arg;
417 417
418 dw_handle_msi_irq(pp); 418 return dw_handle_msi_irq(pp);
419
420 return IRQ_HANDLED;
421} 419}
422 420
423static void exynos_pcie_msi_init(struct pcie_port *pp) 421static void exynos_pcie_msi_init(struct pcie_port *pp)
@@ -511,7 +509,8 @@ static struct pcie_host_ops exynos_pcie_host_ops = {
511 .host_init = exynos_pcie_host_init, 509 .host_init = exynos_pcie_host_init,
512}; 510};
513 511
514static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev) 512static int __init add_pcie_port(struct pcie_port *pp,
513 struct platform_device *pdev)
515{ 514{
516 int ret; 515 int ret;
517 516
@@ -568,10 +567,8 @@ static int __init exynos_pcie_probe(struct platform_device *pdev)
568 567
569 exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie), 568 exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie),
570 GFP_KERNEL); 569 GFP_KERNEL);
571 if (!exynos_pcie) { 570 if (!exynos_pcie)
572 dev_err(&pdev->dev, "no memory for exynos pcie\n");
573 return -ENOMEM; 571 return -ENOMEM;
574 }
575 572
576 pp = &exynos_pcie->pp; 573 pp = &exynos_pcie->pp;
577 574
diff --git a/drivers/pci/host/pci-host-generic.c b/drivers/pci/host/pci-host-generic.c
new file mode 100644
index 000000000000..44fe6aa6a43f
--- /dev/null
+++ b/drivers/pci/host/pci-host-generic.c
@@ -0,0 +1,388 @@
1/*
2 * Simple, generic PCI host controller driver targetting firmware-initialised
3 * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright (C) 2014 ARM Limited
18 *
19 * Author: Will Deacon <will.deacon@arm.com>
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of_address.h>
25#include <linux/of_pci.h>
26#include <linux/platform_device.h>
27
28struct gen_pci_cfg_bus_ops {
29 u32 bus_shift;
30 void __iomem *(*map_bus)(struct pci_bus *, unsigned int, int);
31};
32
33struct gen_pci_cfg_windows {
34 struct resource res;
35 struct resource bus_range;
36 void __iomem **win;
37
38 const struct gen_pci_cfg_bus_ops *ops;
39};
40
41struct gen_pci {
42 struct pci_host_bridge host;
43 struct gen_pci_cfg_windows cfg;
44 struct list_head resources;
45};
46
47static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
48 unsigned int devfn,
49 int where)
50{
51 struct pci_sys_data *sys = bus->sysdata;
52 struct gen_pci *pci = sys->private_data;
53 resource_size_t idx = bus->number - pci->cfg.bus_range.start;
54
55 return pci->cfg.win[idx] + ((devfn << 8) | where);
56}
57
58static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
59 .bus_shift = 16,
60 .map_bus = gen_pci_map_cfg_bus_cam,
61};
62
63static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
64 unsigned int devfn,
65 int where)
66{
67 struct pci_sys_data *sys = bus->sysdata;
68 struct gen_pci *pci = sys->private_data;
69 resource_size_t idx = bus->number - pci->cfg.bus_range.start;
70
71 return pci->cfg.win[idx] + ((devfn << 12) | where);
72}
73
74static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
75 .bus_shift = 20,
76 .map_bus = gen_pci_map_cfg_bus_ecam,
77};
78
79static int gen_pci_config_read(struct pci_bus *bus, unsigned int devfn,
80 int where, int size, u32 *val)
81{
82 void __iomem *addr;
83 struct pci_sys_data *sys = bus->sysdata;
84 struct gen_pci *pci = sys->private_data;
85
86 addr = pci->cfg.ops->map_bus(bus, devfn, where);
87
88 switch (size) {
89 case 1:
90 *val = readb(addr);
91 break;
92 case 2:
93 *val = readw(addr);
94 break;
95 default:
96 *val = readl(addr);
97 }
98
99 return PCIBIOS_SUCCESSFUL;
100}
101
102static int gen_pci_config_write(struct pci_bus *bus, unsigned int devfn,
103 int where, int size, u32 val)
104{
105 void __iomem *addr;
106 struct pci_sys_data *sys = bus->sysdata;
107 struct gen_pci *pci = sys->private_data;
108
109 addr = pci->cfg.ops->map_bus(bus, devfn, where);
110
111 switch (size) {
112 case 1:
113 writeb(val, addr);
114 break;
115 case 2:
116 writew(val, addr);
117 break;
118 default:
119 writel(val, addr);
120 }
121
122 return PCIBIOS_SUCCESSFUL;
123}
124
125static struct pci_ops gen_pci_ops = {
126 .read = gen_pci_config_read,
127 .write = gen_pci_config_write,
128};
129
130static const struct of_device_id gen_pci_of_match[] = {
131 { .compatible = "pci-host-cam-generic",
132 .data = &gen_pci_cfg_cam_bus_ops },
133
134 { .compatible = "pci-host-ecam-generic",
135 .data = &gen_pci_cfg_ecam_bus_ops },
136
137 { },
138};
139MODULE_DEVICE_TABLE(of, gen_pci_of_match);
140
141static int gen_pci_calc_io_offset(struct device *dev,
142 struct of_pci_range *range,
143 struct resource *res,
144 resource_size_t *offset)
145{
146 static atomic_t wins = ATOMIC_INIT(0);
147 int err, idx, max_win;
148 unsigned int window;
149
150 if (!PAGE_ALIGNED(range->cpu_addr))
151 return -EINVAL;
152
153 max_win = (IO_SPACE_LIMIT + 1) / SZ_64K;
154 idx = atomic_inc_return(&wins);
155 if (idx > max_win)
156 return -ENOSPC;
157
158 window = (idx - 1) * SZ_64K;
159 err = pci_ioremap_io(window, range->cpu_addr);
160 if (err)
161 return err;
162
163 of_pci_range_to_resource(range, dev->of_node, res);
164 res->start = window;
165 res->end = res->start + range->size - 1;
166 *offset = window - range->pci_addr;
167 return 0;
168}
169
170static int gen_pci_calc_mem_offset(struct device *dev,
171 struct of_pci_range *range,
172 struct resource *res,
173 resource_size_t *offset)
174{
175 of_pci_range_to_resource(range, dev->of_node, res);
176 *offset = range->cpu_addr - range->pci_addr;
177 return 0;
178}
179
180static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
181{
182 struct pci_host_bridge_window *win;
183
184 list_for_each_entry(win, &pci->resources, list)
185 release_resource(win->res);
186
187 pci_free_resource_list(&pci->resources);
188}
189
190static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
191{
192 struct of_pci_range range;
193 struct of_pci_range_parser parser;
194 int err, res_valid = 0;
195 struct device *dev = pci->host.dev.parent;
196 struct device_node *np = dev->of_node;
197
198 if (of_pci_range_parser_init(&parser, np)) {
199 dev_err(dev, "missing \"ranges\" property\n");
200 return -EINVAL;
201 }
202
203 for_each_of_pci_range(&parser, &range) {
204 struct resource *parent, *res;
205 resource_size_t offset;
206 u32 restype = range.flags & IORESOURCE_TYPE_BITS;
207
208 res = devm_kmalloc(dev, sizeof(*res), GFP_KERNEL);
209 if (!res) {
210 err = -ENOMEM;
211 goto out_release_res;
212 }
213
214 switch (restype) {
215 case IORESOURCE_IO:
216 parent = &ioport_resource;
217 err = gen_pci_calc_io_offset(dev, &range, res, &offset);
218 break;
219 case IORESOURCE_MEM:
220 parent = &iomem_resource;
221 err = gen_pci_calc_mem_offset(dev, &range, res, &offset);
222 res_valid |= !(res->flags & IORESOURCE_PREFETCH || err);
223 break;
224 default:
225 err = -EINVAL;
226 continue;
227 }
228
229 if (err) {
230 dev_warn(dev,
231 "error %d: failed to add resource [type 0x%x, %lld bytes]\n",
232 err, restype, range.size);
233 continue;
234 }
235
236 err = request_resource(parent, res);
237 if (err)
238 goto out_release_res;
239
240 pci_add_resource_offset(&pci->resources, res, offset);
241 }
242
243 if (!res_valid) {
244 dev_err(dev, "non-prefetchable memory resource required\n");
245 err = -EINVAL;
246 goto out_release_res;
247 }
248
249 return 0;
250
251out_release_res:
252 gen_pci_release_of_pci_ranges(pci);
253 return err;
254}
255
256static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
257{
258 int err;
259 u8 bus_max;
260 resource_size_t busn;
261 struct resource *bus_range;
262 struct device *dev = pci->host.dev.parent;
263 struct device_node *np = dev->of_node;
264
265 if (of_pci_parse_bus_range(np, &pci->cfg.bus_range))
266 pci->cfg.bus_range = (struct resource) {
267 .name = np->name,
268 .start = 0,
269 .end = 0xff,
270 .flags = IORESOURCE_BUS,
271 };
272
273 err = of_address_to_resource(np, 0, &pci->cfg.res);
274 if (err) {
275 dev_err(dev, "missing \"reg\" property\n");
276 return err;
277 }
278
279 pci->cfg.win = devm_kcalloc(dev, resource_size(&pci->cfg.bus_range),
280 sizeof(*pci->cfg.win), GFP_KERNEL);
281 if (!pci->cfg.win)
282 return -ENOMEM;
283
284 /* Limit the bus-range to fit within reg */
285 bus_max = pci->cfg.bus_range.start +
286 (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
287 pci->cfg.bus_range.end = min_t(resource_size_t, pci->cfg.bus_range.end,
288 bus_max);
289
290 /* Map our Configuration Space windows */
291 if (!devm_request_mem_region(dev, pci->cfg.res.start,
292 resource_size(&pci->cfg.res),
293 "Configuration Space"))
294 return -ENOMEM;
295
296 bus_range = &pci->cfg.bus_range;
297 for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
298 u32 idx = busn - bus_range->start;
299 u32 sz = 1 << pci->cfg.ops->bus_shift;
300
301 pci->cfg.win[idx] = devm_ioremap(dev,
302 pci->cfg.res.start + busn * sz,
303 sz);
304 if (!pci->cfg.win[idx])
305 return -ENOMEM;
306 }
307
308 /* Register bus resource */
309 pci_add_resource(&pci->resources, bus_range);
310 return 0;
311}
312
313static int gen_pci_setup(int nr, struct pci_sys_data *sys)
314{
315 struct gen_pci *pci = sys->private_data;
316 list_splice_init(&pci->resources, &sys->resources);
317 return 1;
318}
319
320static int gen_pci_probe(struct platform_device *pdev)
321{
322 int err;
323 const char *type;
324 const struct of_device_id *of_id;
325 const int *prop;
326 struct device *dev = &pdev->dev;
327 struct device_node *np = dev->of_node;
328 struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
329 struct hw_pci hw = {
330 .nr_controllers = 1,
331 .private_data = (void **)&pci,
332 .setup = gen_pci_setup,
333 .map_irq = of_irq_parse_and_map_pci,
334 .ops = &gen_pci_ops,
335 };
336
337 if (!pci)
338 return -ENOMEM;
339
340 type = of_get_property(np, "device_type", NULL);
341 if (!type || strcmp(type, "pci")) {
342 dev_err(dev, "invalid \"device_type\" %s\n", type);
343 return -EINVAL;
344 }
345
346 prop = of_get_property(of_chosen, "linux,pci-probe-only", NULL);
347 if (prop) {
348 if (*prop)
349 pci_add_flags(PCI_PROBE_ONLY);
350 else
351 pci_clear_flags(PCI_PROBE_ONLY);
352 }
353
354 of_id = of_match_node(gen_pci_of_match, np);
355 pci->cfg.ops = of_id->data;
356 pci->host.dev.parent = dev;
357 INIT_LIST_HEAD(&pci->host.windows);
358 INIT_LIST_HEAD(&pci->resources);
359
360 /* Parse our PCI ranges and request their resources */
361 err = gen_pci_parse_request_of_pci_ranges(pci);
362 if (err)
363 return err;
364
365 /* Parse and map our Configuration Space windows */
366 err = gen_pci_parse_map_cfg_windows(pci);
367 if (err) {
368 gen_pci_release_of_pci_ranges(pci);
369 return err;
370 }
371
372 pci_common_init_dev(dev, &hw);
373 return 0;
374}
375
376static struct platform_driver gen_pci_driver = {
377 .driver = {
378 .name = "pci-host-generic",
379 .owner = THIS_MODULE,
380 .of_match_table = gen_pci_of_match,
381 },
382 .probe = gen_pci_probe,
383};
384module_platform_driver(gen_pci_driver);
385
386MODULE_DESCRIPTION("Generic PCI host driver");
387MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
388MODULE_LICENSE("GPLv2");
diff --git a/drivers/pci/host/pci-imx6.c b/drivers/pci/host/pci-imx6.c
index ee082509b0ba..a5645ae4aef0 100644
--- a/drivers/pci/host/pci-imx6.c
+++ b/drivers/pci/host/pci-imx6.c
@@ -25,6 +25,7 @@
25#include <linux/resource.h> 25#include <linux/resource.h>
26#include <linux/signal.h> 26#include <linux/signal.h>
27#include <linux/types.h> 27#include <linux/types.h>
28#include <linux/interrupt.h>
28 29
29#include "pcie-designware.h" 30#include "pcie-designware.h"
30 31
@@ -32,13 +33,9 @@
32 33
33struct imx6_pcie { 34struct imx6_pcie {
34 int reset_gpio; 35 int reset_gpio;
35 int power_on_gpio; 36 struct clk *pcie_bus;
36 int wake_up_gpio; 37 struct clk *pcie_phy;
37 int disable_gpio; 38 struct clk *pcie;
38 struct clk *lvds_gate;
39 struct clk *sata_ref_100m;
40 struct clk *pcie_ref_125m;
41 struct clk *pcie_axi;
42 struct pcie_port pp; 39 struct pcie_port pp;
43 struct regmap *iomuxc_gpr; 40 struct regmap *iomuxc_gpr;
44 void __iomem *mem_base; 41 void __iomem *mem_base;
@@ -231,36 +228,27 @@ static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
231 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp); 228 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
232 int ret; 229 int ret;
233 230
234 if (gpio_is_valid(imx6_pcie->power_on_gpio))
235 gpio_set_value(imx6_pcie->power_on_gpio, 1);
236
237 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, 231 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
238 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18); 232 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
239 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, 233 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
240 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16); 234 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
241 235
242 ret = clk_prepare_enable(imx6_pcie->sata_ref_100m); 236 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
243 if (ret) {
244 dev_err(pp->dev, "unable to enable sata_ref_100m\n");
245 goto err_sata_ref;
246 }
247
248 ret = clk_prepare_enable(imx6_pcie->pcie_ref_125m);
249 if (ret) { 237 if (ret) {
250 dev_err(pp->dev, "unable to enable pcie_ref_125m\n"); 238 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
251 goto err_pcie_ref; 239 goto err_pcie_phy;
252 } 240 }
253 241
254 ret = clk_prepare_enable(imx6_pcie->lvds_gate); 242 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
255 if (ret) { 243 if (ret) {
256 dev_err(pp->dev, "unable to enable lvds_gate\n"); 244 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
257 goto err_lvds_gate; 245 goto err_pcie_bus;
258 } 246 }
259 247
260 ret = clk_prepare_enable(imx6_pcie->pcie_axi); 248 ret = clk_prepare_enable(imx6_pcie->pcie);
261 if (ret) { 249 if (ret) {
262 dev_err(pp->dev, "unable to enable pcie_axi\n"); 250 dev_err(pp->dev, "unable to enable pcie clock\n");
263 goto err_pcie_axi; 251 goto err_pcie;
264 } 252 }
265 253
266 /* allow the clocks to stabilize */ 254 /* allow the clocks to stabilize */
@@ -274,13 +262,11 @@ static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
274 } 262 }
275 return 0; 263 return 0;
276 264
277err_pcie_axi: 265err_pcie:
278 clk_disable_unprepare(imx6_pcie->lvds_gate); 266 clk_disable_unprepare(imx6_pcie->pcie_bus);
279err_lvds_gate: 267err_pcie_bus:
280 clk_disable_unprepare(imx6_pcie->pcie_ref_125m); 268 clk_disable_unprepare(imx6_pcie->pcie_phy);
281err_pcie_ref: 269err_pcie_phy:
282 clk_disable_unprepare(imx6_pcie->sata_ref_100m);
283err_sata_ref:
284 return ret; 270 return ret;
285 271
286} 272}
@@ -329,6 +315,13 @@ static int imx6_pcie_wait_for_link(struct pcie_port *pp)
329 return 0; 315 return 0;
330} 316}
331 317
318static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
319{
320 struct pcie_port *pp = arg;
321
322 return dw_handle_msi_irq(pp);
323}
324
332static int imx6_pcie_start_link(struct pcie_port *pp) 325static int imx6_pcie_start_link(struct pcie_port *pp)
333{ 326{
334 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp); 327 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
@@ -403,6 +396,9 @@ static void imx6_pcie_host_init(struct pcie_port *pp)
403 dw_pcie_setup_rc(pp); 396 dw_pcie_setup_rc(pp);
404 397
405 imx6_pcie_start_link(pp); 398 imx6_pcie_start_link(pp);
399
400 if (IS_ENABLED(CONFIG_PCI_MSI))
401 dw_pcie_msi_init(pp);
406} 402}
407 403
408static void imx6_pcie_reset_phy(struct pcie_port *pp) 404static void imx6_pcie_reset_phy(struct pcie_port *pp)
@@ -487,15 +483,25 @@ static struct pcie_host_ops imx6_pcie_host_ops = {
487 .host_init = imx6_pcie_host_init, 483 .host_init = imx6_pcie_host_init,
488}; 484};
489 485
490static int imx6_add_pcie_port(struct pcie_port *pp, 486static int __init imx6_add_pcie_port(struct pcie_port *pp,
491 struct platform_device *pdev) 487 struct platform_device *pdev)
492{ 488{
493 int ret; 489 int ret;
494 490
495 pp->irq = platform_get_irq(pdev, 0); 491 if (IS_ENABLED(CONFIG_PCI_MSI)) {
496 if (!pp->irq) { 492 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
497 dev_err(&pdev->dev, "failed to get irq\n"); 493 if (pp->msi_irq <= 0) {
498 return -ENODEV; 494 dev_err(&pdev->dev, "failed to get MSI irq\n");
495 return -ENODEV;
496 }
497
498 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
499 imx6_pcie_msi_handler,
500 IRQF_SHARED, "mx6-pcie-msi", pp);
501 if (ret) {
502 dev_err(&pdev->dev, "failed to request MSI irq\n");
503 return -ENODEV;
504 }
499 } 505 }
500 506
501 pp->root_bus_nr = -1; 507 pp->root_bus_nr = -1;
@@ -546,69 +552,26 @@ static int __init imx6_pcie_probe(struct platform_device *pdev)
546 } 552 }
547 } 553 }
548 554
549 imx6_pcie->power_on_gpio = of_get_named_gpio(np, "power-on-gpio", 0);
550 if (gpio_is_valid(imx6_pcie->power_on_gpio)) {
551 ret = devm_gpio_request_one(&pdev->dev,
552 imx6_pcie->power_on_gpio,
553 GPIOF_OUT_INIT_LOW,
554 "PCIe power enable");
555 if (ret) {
556 dev_err(&pdev->dev, "unable to get power-on gpio\n");
557 return ret;
558 }
559 }
560
561 imx6_pcie->wake_up_gpio = of_get_named_gpio(np, "wake-up-gpio", 0);
562 if (gpio_is_valid(imx6_pcie->wake_up_gpio)) {
563 ret = devm_gpio_request_one(&pdev->dev,
564 imx6_pcie->wake_up_gpio,
565 GPIOF_IN,
566 "PCIe wake up");
567 if (ret) {
568 dev_err(&pdev->dev, "unable to get wake-up gpio\n");
569 return ret;
570 }
571 }
572
573 imx6_pcie->disable_gpio = of_get_named_gpio(np, "disable-gpio", 0);
574 if (gpio_is_valid(imx6_pcie->disable_gpio)) {
575 ret = devm_gpio_request_one(&pdev->dev,
576 imx6_pcie->disable_gpio,
577 GPIOF_OUT_INIT_HIGH,
578 "PCIe disable endpoint");
579 if (ret) {
580 dev_err(&pdev->dev, "unable to get disable-ep gpio\n");
581 return ret;
582 }
583 }
584
585 /* Fetch clocks */ 555 /* Fetch clocks */
586 imx6_pcie->lvds_gate = devm_clk_get(&pdev->dev, "lvds_gate"); 556 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
587 if (IS_ERR(imx6_pcie->lvds_gate)) { 557 if (IS_ERR(imx6_pcie->pcie_phy)) {
588 dev_err(&pdev->dev,
589 "lvds_gate clock select missing or invalid\n");
590 return PTR_ERR(imx6_pcie->lvds_gate);
591 }
592
593 imx6_pcie->sata_ref_100m = devm_clk_get(&pdev->dev, "sata_ref_100m");
594 if (IS_ERR(imx6_pcie->sata_ref_100m)) {
595 dev_err(&pdev->dev, 558 dev_err(&pdev->dev,
596 "sata_ref_100m clock source missing or invalid\n"); 559 "pcie_phy clock source missing or invalid\n");
597 return PTR_ERR(imx6_pcie->sata_ref_100m); 560 return PTR_ERR(imx6_pcie->pcie_phy);
598 } 561 }
599 562
600 imx6_pcie->pcie_ref_125m = devm_clk_get(&pdev->dev, "pcie_ref_125m"); 563 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
601 if (IS_ERR(imx6_pcie->pcie_ref_125m)) { 564 if (IS_ERR(imx6_pcie->pcie_bus)) {
602 dev_err(&pdev->dev, 565 dev_err(&pdev->dev,
603 "pcie_ref_125m clock source missing or invalid\n"); 566 "pcie_bus clock source missing or invalid\n");
604 return PTR_ERR(imx6_pcie->pcie_ref_125m); 567 return PTR_ERR(imx6_pcie->pcie_bus);
605 } 568 }
606 569
607 imx6_pcie->pcie_axi = devm_clk_get(&pdev->dev, "pcie_axi"); 570 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
608 if (IS_ERR(imx6_pcie->pcie_axi)) { 571 if (IS_ERR(imx6_pcie->pcie)) {
609 dev_err(&pdev->dev, 572 dev_err(&pdev->dev,
610 "pcie_axi clock source missing or invalid\n"); 573 "pcie clock source missing or invalid\n");
611 return PTR_ERR(imx6_pcie->pcie_axi); 574 return PTR_ERR(imx6_pcie->pcie);
612 } 575 }
613 576
614 /* Grab GPR config register range */ 577 /* Grab GPR config register range */
diff --git a/drivers/pci/host/pci-rcar-gen2.c b/drivers/pci/host/pci-rcar-gen2.c
index 4fe349dcaf59..3ef854f5a5b5 100644
--- a/drivers/pci/host/pci-rcar-gen2.c
+++ b/drivers/pci/host/pci-rcar-gen2.c
@@ -99,6 +99,7 @@ struct rcar_pci_priv {
99 struct resource io_res; 99 struct resource io_res;
100 struct resource mem_res; 100 struct resource mem_res;
101 struct resource *cfg_res; 101 struct resource *cfg_res;
102 unsigned busnr;
102 int irq; 103 int irq;
103 unsigned long window_size; 104 unsigned long window_size;
104}; 105};
@@ -318,8 +319,8 @@ static int rcar_pci_setup(int nr, struct pci_sys_data *sys)
318 pci_add_resource(&sys->resources, &priv->io_res); 319 pci_add_resource(&sys->resources, &priv->io_res);
319 pci_add_resource(&sys->resources, &priv->mem_res); 320 pci_add_resource(&sys->resources, &priv->mem_res);
320 321
321 /* Setup bus number based on platform device id */ 322 /* Setup bus number based on platform device id / of bus-range */
322 sys->busnr = to_platform_device(priv->dev)->id; 323 sys->busnr = priv->busnr;
323 return 1; 324 return 1;
324} 325}
325 326
@@ -372,6 +373,23 @@ static int rcar_pci_probe(struct platform_device *pdev)
372 373
373 priv->window_size = SZ_1G; 374 priv->window_size = SZ_1G;
374 375
376 if (pdev->dev.of_node) {
377 struct resource busnr;
378 int ret;
379
380 ret = of_pci_parse_bus_range(pdev->dev.of_node, &busnr);
381 if (ret < 0) {
382 dev_err(&pdev->dev, "failed to parse bus-range\n");
383 return ret;
384 }
385
386 priv->busnr = busnr.start;
387 if (busnr.end != busnr.start)
388 dev_warn(&pdev->dev, "only one bus number supported\n");
389 } else {
390 priv->busnr = pdev->id;
391 }
392
375 hw_private[0] = priv; 393 hw_private[0] = priv;
376 memset(&hw, 0, sizeof(hw)); 394 memset(&hw, 0, sizeof(hw));
377 hw.nr_controllers = ARRAY_SIZE(hw_private); 395 hw.nr_controllers = ARRAY_SIZE(hw_private);
@@ -383,11 +401,20 @@ static int rcar_pci_probe(struct platform_device *pdev)
383 return 0; 401 return 0;
384} 402}
385 403
404static struct of_device_id rcar_pci_of_match[] = {
405 { .compatible = "renesas,pci-r8a7790", },
406 { .compatible = "renesas,pci-r8a7791", },
407 { },
408};
409
410MODULE_DEVICE_TABLE(of, rcar_pci_of_match);
411
386static struct platform_driver rcar_pci_driver = { 412static struct platform_driver rcar_pci_driver = {
387 .driver = { 413 .driver = {
388 .name = "pci-rcar-gen2", 414 .name = "pci-rcar-gen2",
389 .owner = THIS_MODULE, 415 .owner = THIS_MODULE,
390 .suppress_bind_attrs = true, 416 .suppress_bind_attrs = true,
417 .of_match_table = rcar_pci_of_match,
391 }, 418 },
392 .probe = rcar_pci_probe, 419 .probe = rcar_pci_probe,
393}; 420};
diff --git a/drivers/pci/host/pcie-designware.c b/drivers/pci/host/pcie-designware.c
index c4e373294476..e3bf9e6d5d9a 100644
--- a/drivers/pci/host/pcie-designware.c
+++ b/drivers/pci/host/pcie-designware.c
@@ -156,15 +156,17 @@ static struct irq_chip dw_msi_irq_chip = {
156}; 156};
157 157
158/* MSI int handler */ 158/* MSI int handler */
159void dw_handle_msi_irq(struct pcie_port *pp) 159irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
160{ 160{
161 unsigned long val; 161 unsigned long val;
162 int i, pos, irq; 162 int i, pos, irq;
163 irqreturn_t ret = IRQ_NONE;
163 164
164 for (i = 0; i < MAX_MSI_CTRLS; i++) { 165 for (i = 0; i < MAX_MSI_CTRLS; i++) {
165 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4, 166 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
166 (u32 *)&val); 167 (u32 *)&val);
167 if (val) { 168 if (val) {
169 ret = IRQ_HANDLED;
168 pos = 0; 170 pos = 0;
169 while ((pos = find_next_bit(&val, 32, pos)) != 32) { 171 while ((pos = find_next_bit(&val, 32, pos)) != 32) {
170 irq = irq_find_mapping(pp->irq_domain, 172 irq = irq_find_mapping(pp->irq_domain,
@@ -177,6 +179,8 @@ void dw_handle_msi_irq(struct pcie_port *pp)
177 } 179 }
178 } 180 }
179 } 181 }
182
183 return ret;
180} 184}
181 185
182void dw_pcie_msi_init(struct pcie_port *pp) 186void dw_pcie_msi_init(struct pcie_port *pp)
diff --git a/drivers/pci/host/pcie-designware.h b/drivers/pci/host/pcie-designware.h
index 3063b3594d88..a169d22d517e 100644
--- a/drivers/pci/host/pcie-designware.h
+++ b/drivers/pci/host/pcie-designware.h
@@ -68,7 +68,7 @@ struct pcie_host_ops {
68 68
69int dw_pcie_cfg_read(void __iomem *addr, int where, int size, u32 *val); 69int dw_pcie_cfg_read(void __iomem *addr, int where, int size, u32 *val);
70int dw_pcie_cfg_write(void __iomem *addr, int where, int size, u32 val); 70int dw_pcie_cfg_write(void __iomem *addr, int where, int size, u32 val);
71void dw_handle_msi_irq(struct pcie_port *pp); 71irqreturn_t dw_handle_msi_irq(struct pcie_port *pp);
72void dw_pcie_msi_init(struct pcie_port *pp); 72void dw_pcie_msi_init(struct pcie_port *pp);
73int dw_pcie_link_up(struct pcie_port *pp); 73int dw_pcie_link_up(struct pcie_port *pp);
74void dw_pcie_setup_rc(struct pcie_port *pp); 74void dw_pcie_setup_rc(struct pcie_port *pp);
diff --git a/drivers/pci/host/pcie-rcar.c b/drivers/pci/host/pcie-rcar.c
new file mode 100644
index 000000000000..8e06124aa80f
--- /dev/null
+++ b/drivers/pci/host/pcie-rcar.c
@@ -0,0 +1,1008 @@
1/*
2 * PCIe driver for Renesas R-Car SoCs
3 * Copyright (C) 2014 Renesas Electronics Europe Ltd
4 *
5 * Based on:
6 * arch/sh/drivers/pci/pcie-sh7786.c
7 * arch/sh/drivers/pci/ops-sh7786.c
8 * Copyright (C) 2009 - 2011 Paul Mundt
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14
15#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/irqdomain.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/msi.h>
23#include <linux/of_address.h>
24#include <linux/of_irq.h>
25#include <linux/of_pci.h>
26#include <linux/of_platform.h>
27#include <linux/pci.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30
31#define DRV_NAME "rcar-pcie"
32
33#define PCIECAR 0x000010
34#define PCIECCTLR 0x000018
35#define CONFIG_SEND_ENABLE (1 << 31)
36#define TYPE0 (0 << 8)
37#define TYPE1 (1 << 8)
38#define PCIECDR 0x000020
39#define PCIEMSR 0x000028
40#define PCIEINTXR 0x000400
41#define PCIEMSITXR 0x000840
42
43/* Transfer control */
44#define PCIETCTLR 0x02000
45#define CFINIT 1
46#define PCIETSTR 0x02004
47#define DATA_LINK_ACTIVE 1
48#define PCIEERRFR 0x02020
49#define UNSUPPORTED_REQUEST (1 << 4)
50#define PCIEMSIFR 0x02044
51#define PCIEMSIALR 0x02048
52#define MSIFE 1
53#define PCIEMSIAUR 0x0204c
54#define PCIEMSIIER 0x02050
55
56/* root port address */
57#define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
58
59/* local address reg & mask */
60#define PCIELAR(x) (0x02200 + ((x) * 0x20))
61#define PCIELAMR(x) (0x02208 + ((x) * 0x20))
62#define LAM_PREFETCH (1 << 3)
63#define LAM_64BIT (1 << 2)
64#define LAR_ENABLE (1 << 1)
65
66/* PCIe address reg & mask */
67#define PCIEPARL(x) (0x03400 + ((x) * 0x20))
68#define PCIEPARH(x) (0x03404 + ((x) * 0x20))
69#define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
70#define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
71#define PAR_ENABLE (1 << 31)
72#define IO_SPACE (1 << 8)
73
74/* Configuration */
75#define PCICONF(x) (0x010000 + ((x) * 0x4))
76#define PMCAP(x) (0x010040 + ((x) * 0x4))
77#define EXPCAP(x) (0x010070 + ((x) * 0x4))
78#define VCCAP(x) (0x010100 + ((x) * 0x4))
79
80/* link layer */
81#define IDSETR1 0x011004
82#define TLCTLR 0x011048
83#define MACSR 0x011054
84#define MACCTLR 0x011058
85#define SCRAMBLE_DISABLE (1 << 27)
86
87/* R-Car H1 PHY */
88#define H1_PCIEPHYADRR 0x04000c
89#define WRITE_CMD (1 << 16)
90#define PHY_ACK (1 << 24)
91#define RATE_POS 12
92#define LANE_POS 8
93#define ADR_POS 0
94#define H1_PCIEPHYDOUTR 0x040014
95#define H1_PCIEPHYSR 0x040018
96
97#define INT_PCI_MSI_NR 32
98
99#define RCONF(x) (PCICONF(0)+(x))
100#define RPMCAP(x) (PMCAP(0)+(x))
101#define REXPCAP(x) (EXPCAP(0)+(x))
102#define RVCCAP(x) (VCCAP(0)+(x))
103
104#define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
105#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
106#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
107
108#define PCI_MAX_RESOURCES 4
109#define MAX_NR_INBOUND_MAPS 6
110
111struct rcar_msi {
112 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
113 struct irq_domain *domain;
114 struct msi_chip chip;
115 unsigned long pages;
116 struct mutex lock;
117 int irq1;
118 int irq2;
119};
120
121static inline struct rcar_msi *to_rcar_msi(struct msi_chip *chip)
122{
123 return container_of(chip, struct rcar_msi, chip);
124}
125
126/* Structure representing the PCIe interface */
127struct rcar_pcie {
128 struct device *dev;
129 void __iomem *base;
130 struct resource res[PCI_MAX_RESOURCES];
131 struct resource busn;
132 int root_bus_nr;
133 struct clk *clk;
134 struct clk *bus_clk;
135 struct rcar_msi msi;
136};
137
138static inline struct rcar_pcie *sys_to_pcie(struct pci_sys_data *sys)
139{
140 return sys->private_data;
141}
142
143static void pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
144 unsigned long reg)
145{
146 writel(val, pcie->base + reg);
147}
148
149static unsigned long pci_read_reg(struct rcar_pcie *pcie, unsigned long reg)
150{
151 return readl(pcie->base + reg);
152}
153
154enum {
155 PCI_ACCESS_READ,
156 PCI_ACCESS_WRITE,
157};
158
159static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
160{
161 int shift = 8 * (where & 3);
162 u32 val = pci_read_reg(pcie, where & ~3);
163
164 val &= ~(mask << shift);
165 val |= data << shift;
166 pci_write_reg(pcie, val, where & ~3);
167}
168
169static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
170{
171 int shift = 8 * (where & 3);
172 u32 val = pci_read_reg(pcie, where & ~3);
173
174 return val >> shift;
175}
176
177/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
178static int rcar_pcie_config_access(struct rcar_pcie *pcie,
179 unsigned char access_type, struct pci_bus *bus,
180 unsigned int devfn, int where, u32 *data)
181{
182 int dev, func, reg, index;
183
184 dev = PCI_SLOT(devfn);
185 func = PCI_FUNC(devfn);
186 reg = where & ~3;
187 index = reg / 4;
188
189 /*
190 * While each channel has its own memory-mapped extended config
191 * space, it's generally only accessible when in endpoint mode.
192 * When in root complex mode, the controller is unable to target
193 * itself with either type 0 or type 1 accesses, and indeed, any
194 * controller initiated target transfer to its own config space
195 * result in a completer abort.
196 *
197 * Each channel effectively only supports a single device, but as
198 * the same channel <-> device access works for any PCI_SLOT()
199 * value, we cheat a bit here and bind the controller's config
200 * space to devfn 0 in order to enable self-enumeration. In this
201 * case the regular ECAR/ECDR path is sidelined and the mangled
202 * config access itself is initiated as an internal bus transaction.
203 */
204 if (pci_is_root_bus(bus)) {
205 if (dev != 0)
206 return PCIBIOS_DEVICE_NOT_FOUND;
207
208 if (access_type == PCI_ACCESS_READ) {
209 *data = pci_read_reg(pcie, PCICONF(index));
210 } else {
211 /* Keep an eye out for changes to the root bus number */
212 if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
213 pcie->root_bus_nr = *data & 0xff;
214
215 pci_write_reg(pcie, *data, PCICONF(index));
216 }
217
218 return PCIBIOS_SUCCESSFUL;
219 }
220
221 if (pcie->root_bus_nr < 0)
222 return PCIBIOS_DEVICE_NOT_FOUND;
223
224 /* Clear errors */
225 pci_write_reg(pcie, pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
226
227 /* Set the PIO address */
228 pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) | PCIE_CONF_DEV(dev) |
229 PCIE_CONF_FUNC(func) | reg, PCIECAR);
230
231 /* Enable the configuration access */
232 if (bus->parent->number == pcie->root_bus_nr)
233 pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
234 else
235 pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
236
237 /* Check for errors */
238 if (pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
239 return PCIBIOS_DEVICE_NOT_FOUND;
240
241 /* Check for master and target aborts */
242 if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
243 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
244 return PCIBIOS_DEVICE_NOT_FOUND;
245
246 if (access_type == PCI_ACCESS_READ)
247 *data = pci_read_reg(pcie, PCIECDR);
248 else
249 pci_write_reg(pcie, *data, PCIECDR);
250
251 /* Disable the configuration access */
252 pci_write_reg(pcie, 0, PCIECCTLR);
253
254 return PCIBIOS_SUCCESSFUL;
255}
256
257static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
258 int where, int size, u32 *val)
259{
260 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
261 int ret;
262
263 if ((size == 2) && (where & 1))
264 return PCIBIOS_BAD_REGISTER_NUMBER;
265 else if ((size == 4) && (where & 3))
266 return PCIBIOS_BAD_REGISTER_NUMBER;
267
268 ret = rcar_pcie_config_access(pcie, PCI_ACCESS_READ,
269 bus, devfn, where, val);
270 if (ret != PCIBIOS_SUCCESSFUL) {
271 *val = 0xffffffff;
272 return ret;
273 }
274
275 if (size == 1)
276 *val = (*val >> (8 * (where & 3))) & 0xff;
277 else if (size == 2)
278 *val = (*val >> (8 * (where & 2))) & 0xffff;
279
280 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
281 "where=0x%04x size=%d val=0x%08lx\n", bus->number,
282 devfn, where, size, (unsigned long)*val);
283
284 return ret;
285}
286
287/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
288static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
289 int where, int size, u32 val)
290{
291 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
292 int shift, ret;
293 u32 data;
294
295 if ((size == 2) && (where & 1))
296 return PCIBIOS_BAD_REGISTER_NUMBER;
297 else if ((size == 4) && (where & 3))
298 return PCIBIOS_BAD_REGISTER_NUMBER;
299
300 ret = rcar_pcie_config_access(pcie, PCI_ACCESS_READ,
301 bus, devfn, where, &data);
302 if (ret != PCIBIOS_SUCCESSFUL)
303 return ret;
304
305 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
306 "where=0x%04x size=%d val=0x%08lx\n", bus->number,
307 devfn, where, size, (unsigned long)val);
308
309 if (size == 1) {
310 shift = 8 * (where & 3);
311 data &= ~(0xff << shift);
312 data |= ((val & 0xff) << shift);
313 } else if (size == 2) {
314 shift = 8 * (where & 2);
315 data &= ~(0xffff << shift);
316 data |= ((val & 0xffff) << shift);
317 } else
318 data = val;
319
320 ret = rcar_pcie_config_access(pcie, PCI_ACCESS_WRITE,
321 bus, devfn, where, &data);
322
323 return ret;
324}
325
326static struct pci_ops rcar_pcie_ops = {
327 .read = rcar_pcie_read_conf,
328 .write = rcar_pcie_write_conf,
329};
330
331static void rcar_pcie_setup_window(int win, struct resource *res,
332 struct rcar_pcie *pcie)
333{
334 /* Setup PCIe address space mappings for each resource */
335 resource_size_t size;
336 u32 mask;
337
338 pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
339
340 /*
341 * The PAMR mask is calculated in units of 128Bytes, which
342 * keeps things pretty simple.
343 */
344 size = resource_size(res);
345 mask = (roundup_pow_of_two(size) / SZ_128) - 1;
346 pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
347
348 pci_write_reg(pcie, upper_32_bits(res->start), PCIEPARH(win));
349 pci_write_reg(pcie, lower_32_bits(res->start), PCIEPARL(win));
350
351 /* First resource is for IO */
352 mask = PAR_ENABLE;
353 if (res->flags & IORESOURCE_IO)
354 mask |= IO_SPACE;
355
356 pci_write_reg(pcie, mask, PCIEPTCTLR(win));
357}
358
359static int rcar_pcie_setup(int nr, struct pci_sys_data *sys)
360{
361 struct rcar_pcie *pcie = sys_to_pcie(sys);
362 struct resource *res;
363 int i;
364
365 pcie->root_bus_nr = -1;
366
367 /* Setup PCI resources */
368 for (i = 0; i < PCI_MAX_RESOURCES; i++) {
369
370 res = &pcie->res[i];
371 if (!res->flags)
372 continue;
373
374 rcar_pcie_setup_window(i, res, pcie);
375
376 if (res->flags & IORESOURCE_IO)
377 pci_ioremap_io(nr * SZ_64K, res->start);
378 else
379 pci_add_resource(&sys->resources, res);
380 }
381 pci_add_resource(&sys->resources, &pcie->busn);
382
383 return 1;
384}
385
386static void rcar_pcie_add_bus(struct pci_bus *bus)
387{
388 if (IS_ENABLED(CONFIG_PCI_MSI)) {
389 struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
390
391 bus->msi = &pcie->msi.chip;
392 }
393}
394
395struct hw_pci rcar_pci = {
396 .setup = rcar_pcie_setup,
397 .map_irq = of_irq_parse_and_map_pci,
398 .ops = &rcar_pcie_ops,
399 .add_bus = rcar_pcie_add_bus,
400};
401
402static void rcar_pcie_enable(struct rcar_pcie *pcie)
403{
404 struct platform_device *pdev = to_platform_device(pcie->dev);
405
406 rcar_pci.nr_controllers = 1;
407 rcar_pci.private_data = (void **)&pcie;
408
409 pci_common_init_dev(&pdev->dev, &rcar_pci);
410#ifdef CONFIG_PCI_DOMAINS
411 rcar_pci.domain++;
412#endif
413}
414
415static int phy_wait_for_ack(struct rcar_pcie *pcie)
416{
417 unsigned int timeout = 100;
418
419 while (timeout--) {
420 if (pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
421 return 0;
422
423 udelay(100);
424 }
425
426 dev_err(pcie->dev, "Access to PCIe phy timed out\n");
427
428 return -ETIMEDOUT;
429}
430
431static void phy_write_reg(struct rcar_pcie *pcie,
432 unsigned int rate, unsigned int addr,
433 unsigned int lane, unsigned int data)
434{
435 unsigned long phyaddr;
436
437 phyaddr = WRITE_CMD |
438 ((rate & 1) << RATE_POS) |
439 ((lane & 0xf) << LANE_POS) |
440 ((addr & 0xff) << ADR_POS);
441
442 /* Set write data */
443 pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
444 pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
445
446 /* Ignore errors as they will be dealt with if the data link is down */
447 phy_wait_for_ack(pcie);
448
449 /* Clear command */
450 pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
451 pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
452
453 /* Ignore errors as they will be dealt with if the data link is down */
454 phy_wait_for_ack(pcie);
455}
456
457static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
458{
459 unsigned int timeout = 10;
460
461 while (timeout--) {
462 if ((pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
463 return 0;
464
465 msleep(5);
466 }
467
468 return -ETIMEDOUT;
469}
470
471static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
472{
473 int err;
474
475 /* Begin initialization */
476 pci_write_reg(pcie, 0, PCIETCTLR);
477
478 /* Set mode */
479 pci_write_reg(pcie, 1, PCIEMSR);
480
481 /*
482 * Initial header for port config space is type 1, set the device
483 * class to match. Hardware takes care of propagating the IDSETR
484 * settings, so there is no need to bother with a quirk.
485 */
486 pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
487
488 /*
489 * Setup Secondary Bus Number & Subordinate Bus Number, even though
490 * they aren't used, to avoid bridge being detected as broken.
491 */
492 rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
493 rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
494
495 /* Initialize default capabilities. */
496 rcar_rmw32(pcie, REXPCAP(0), 0, PCI_CAP_ID_EXP);
497 rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
498 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
499 rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
500 PCI_HEADER_TYPE_BRIDGE);
501
502 /* Enable data link layer active state reporting */
503 rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), 0, PCI_EXP_LNKCAP_DLLLARC);
504
505 /* Write out the physical slot number = 0 */
506 rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
507
508 /* Set the completion timer timeout to the maximum 50ms. */
509 rcar_rmw32(pcie, TLCTLR+1, 0x3f, 50);
510
511 /* Terminate list of capabilities (Next Capability Offset=0) */
512 rcar_rmw32(pcie, RVCCAP(0), 0xfff0, 0);
513
514 /* Enable MAC data scrambling. */
515 rcar_rmw32(pcie, MACCTLR, SCRAMBLE_DISABLE, 0);
516
517 /* Enable MSI */
518 if (IS_ENABLED(CONFIG_PCI_MSI))
519 pci_write_reg(pcie, 0x101f0000, PCIEMSITXR);
520
521 /* Finish initialization - establish a PCI Express link */
522 pci_write_reg(pcie, CFINIT, PCIETCTLR);
523
524 /* This will timeout if we don't have a link. */
525 err = rcar_pcie_wait_for_dl(pcie);
526 if (err)
527 return err;
528
529 /* Enable INTx interrupts */
530 rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
531
532 /* Enable slave Bus Mastering */
533 rcar_rmw32(pcie, RCONF(PCI_STATUS), PCI_STATUS_DEVSEL_MASK,
534 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
535 PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST);
536
537 wmb();
538
539 return 0;
540}
541
542static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
543{
544 unsigned int timeout = 10;
545
546 /* Initialize the phy */
547 phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
548 phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
549 phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
550 phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
551 phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
552 phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
553 phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
554 phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
555 phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
556 phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
557 phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
558 phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
559
560 phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
561 phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
562 phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
563
564 while (timeout--) {
565 if (pci_read_reg(pcie, H1_PCIEPHYSR))
566 return rcar_pcie_hw_init(pcie);
567
568 msleep(5);
569 }
570
571 return -ETIMEDOUT;
572}
573
574static int rcar_msi_alloc(struct rcar_msi *chip)
575{
576 int msi;
577
578 mutex_lock(&chip->lock);
579
580 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
581 if (msi < INT_PCI_MSI_NR)
582 set_bit(msi, chip->used);
583 else
584 msi = -ENOSPC;
585
586 mutex_unlock(&chip->lock);
587
588 return msi;
589}
590
591static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
592{
593 mutex_lock(&chip->lock);
594 clear_bit(irq, chip->used);
595 mutex_unlock(&chip->lock);
596}
597
598static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
599{
600 struct rcar_pcie *pcie = data;
601 struct rcar_msi *msi = &pcie->msi;
602 unsigned long reg;
603
604 reg = pci_read_reg(pcie, PCIEMSIFR);
605
606 /* MSI & INTx share an interrupt - we only handle MSI here */
607 if (!reg)
608 return IRQ_NONE;
609
610 while (reg) {
611 unsigned int index = find_first_bit(&reg, 32);
612 unsigned int irq;
613
614 /* clear the interrupt */
615 pci_write_reg(pcie, 1 << index, PCIEMSIFR);
616
617 irq = irq_find_mapping(msi->domain, index);
618 if (irq) {
619 if (test_bit(index, msi->used))
620 generic_handle_irq(irq);
621 else
622 dev_info(pcie->dev, "unhandled MSI\n");
623 } else {
624 /* Unknown MSI, just clear it */
625 dev_dbg(pcie->dev, "unexpected MSI\n");
626 }
627
628 /* see if there's any more pending in this vector */
629 reg = pci_read_reg(pcie, PCIEMSIFR);
630 }
631
632 return IRQ_HANDLED;
633}
634
635static int rcar_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
636 struct msi_desc *desc)
637{
638 struct rcar_msi *msi = to_rcar_msi(chip);
639 struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
640 struct msi_msg msg;
641 unsigned int irq;
642 int hwirq;
643
644 hwirq = rcar_msi_alloc(msi);
645 if (hwirq < 0)
646 return hwirq;
647
648 irq = irq_create_mapping(msi->domain, hwirq);
649 if (!irq) {
650 rcar_msi_free(msi, hwirq);
651 return -EINVAL;
652 }
653
654 irq_set_msi_desc(irq, desc);
655
656 msg.address_lo = pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
657 msg.address_hi = pci_read_reg(pcie, PCIEMSIAUR);
658 msg.data = hwirq;
659
660 write_msi_msg(irq, &msg);
661
662 return 0;
663}
664
665static void rcar_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
666{
667 struct rcar_msi *msi = to_rcar_msi(chip);
668 struct irq_data *d = irq_get_irq_data(irq);
669
670 rcar_msi_free(msi, d->hwirq);
671}
672
673static struct irq_chip rcar_msi_irq_chip = {
674 .name = "R-Car PCIe MSI",
675 .irq_enable = unmask_msi_irq,
676 .irq_disable = mask_msi_irq,
677 .irq_mask = mask_msi_irq,
678 .irq_unmask = unmask_msi_irq,
679};
680
681static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
682 irq_hw_number_t hwirq)
683{
684 irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
685 irq_set_chip_data(irq, domain->host_data);
686 set_irq_flags(irq, IRQF_VALID);
687
688 return 0;
689}
690
691static const struct irq_domain_ops msi_domain_ops = {
692 .map = rcar_msi_map,
693};
694
695static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
696{
697 struct platform_device *pdev = to_platform_device(pcie->dev);
698 struct rcar_msi *msi = &pcie->msi;
699 unsigned long base;
700 int err;
701
702 mutex_init(&msi->lock);
703
704 msi->chip.dev = pcie->dev;
705 msi->chip.setup_irq = rcar_msi_setup_irq;
706 msi->chip.teardown_irq = rcar_msi_teardown_irq;
707
708 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
709 &msi_domain_ops, &msi->chip);
710 if (!msi->domain) {
711 dev_err(&pdev->dev, "failed to create IRQ domain\n");
712 return -ENOMEM;
713 }
714
715 /* Two irqs are for MSI, but they are also used for non-MSI irqs */
716 err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq,
717 IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
718 if (err < 0) {
719 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
720 goto err;
721 }
722
723 err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq,
724 IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
725 if (err < 0) {
726 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
727 goto err;
728 }
729
730 /* setup MSI data target */
731 msi->pages = __get_free_pages(GFP_KERNEL, 0);
732 base = virt_to_phys((void *)msi->pages);
733
734 pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
735 pci_write_reg(pcie, 0, PCIEMSIAUR);
736
737 /* enable all MSI interrupts */
738 pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
739
740 return 0;
741
742err:
743 irq_domain_remove(msi->domain);
744 return err;
745}
746
747static int rcar_pcie_get_resources(struct platform_device *pdev,
748 struct rcar_pcie *pcie)
749{
750 struct resource res;
751 int err, i;
752
753 err = of_address_to_resource(pdev->dev.of_node, 0, &res);
754 if (err)
755 return err;
756
757 pcie->clk = devm_clk_get(&pdev->dev, "pcie");
758 if (IS_ERR(pcie->clk)) {
759 dev_err(pcie->dev, "cannot get platform clock\n");
760 return PTR_ERR(pcie->clk);
761 }
762 err = clk_prepare_enable(pcie->clk);
763 if (err)
764 goto fail_clk;
765
766 pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
767 if (IS_ERR(pcie->bus_clk)) {
768 dev_err(pcie->dev, "cannot get pcie bus clock\n");
769 err = PTR_ERR(pcie->bus_clk);
770 goto fail_clk;
771 }
772 err = clk_prepare_enable(pcie->bus_clk);
773 if (err)
774 goto err_map_reg;
775
776 i = irq_of_parse_and_map(pdev->dev.of_node, 0);
777 if (i < 0) {
778 dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
779 err = -ENOENT;
780 goto err_map_reg;
781 }
782 pcie->msi.irq1 = i;
783
784 i = irq_of_parse_and_map(pdev->dev.of_node, 1);
785 if (i < 0) {
786 dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
787 err = -ENOENT;
788 goto err_map_reg;
789 }
790 pcie->msi.irq2 = i;
791
792 pcie->base = devm_ioremap_resource(&pdev->dev, &res);
793 if (IS_ERR(pcie->base)) {
794 err = PTR_ERR(pcie->base);
795 goto err_map_reg;
796 }
797
798 return 0;
799
800err_map_reg:
801 clk_disable_unprepare(pcie->bus_clk);
802fail_clk:
803 clk_disable_unprepare(pcie->clk);
804
805 return err;
806}
807
808static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
809 struct of_pci_range *range,
810 int *index)
811{
812 u64 restype = range->flags;
813 u64 cpu_addr = range->cpu_addr;
814 u64 cpu_end = range->cpu_addr + range->size;
815 u64 pci_addr = range->pci_addr;
816 u32 flags = LAM_64BIT | LAR_ENABLE;
817 u64 mask;
818 u64 size;
819 int idx = *index;
820
821 if (restype & IORESOURCE_PREFETCH)
822 flags |= LAM_PREFETCH;
823
824 /*
825 * If the size of the range is larger than the alignment of the start
826 * address, we have to use multiple entries to perform the mapping.
827 */
828 if (cpu_addr > 0) {
829 unsigned long nr_zeros = __ffs64(cpu_addr);
830 u64 alignment = 1ULL << nr_zeros;
831 size = min(range->size, alignment);
832 } else {
833 size = range->size;
834 }
835 /* Hardware supports max 4GiB inbound region */
836 size = min(size, 1ULL << 32);
837
838 mask = roundup_pow_of_two(size) - 1;
839 mask &= ~0xf;
840
841 while (cpu_addr < cpu_end) {
842 /*
843 * Set up 64-bit inbound regions as the range parser doesn't
844 * distinguish between 32 and 64-bit types.
845 */
846 pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx));
847 pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
848 pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx));
849
850 pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1));
851 pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1));
852 pci_write_reg(pcie, 0, PCIELAMR(idx+1));
853
854 pci_addr += size;
855 cpu_addr += size;
856 idx += 2;
857
858 if (idx > MAX_NR_INBOUND_MAPS) {
859 dev_err(pcie->dev, "Failed to map inbound regions!\n");
860 return -EINVAL;
861 }
862 }
863 *index = idx;
864
865 return 0;
866}
867
868static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
869 struct device_node *node)
870{
871 const int na = 3, ns = 2;
872 int rlen;
873
874 parser->node = node;
875 parser->pna = of_n_addr_cells(node);
876 parser->np = parser->pna + na + ns;
877
878 parser->range = of_get_property(node, "dma-ranges", &rlen);
879 if (!parser->range)
880 return -ENOENT;
881
882 parser->end = parser->range + rlen / sizeof(__be32);
883 return 0;
884}
885
886static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
887 struct device_node *np)
888{
889 struct of_pci_range range;
890 struct of_pci_range_parser parser;
891 int index = 0;
892 int err;
893
894 if (pci_dma_range_parser_init(&parser, np))
895 return -EINVAL;
896
897 /* Get the dma-ranges from DT */
898 for_each_of_pci_range(&parser, &range) {
899 u64 end = range.cpu_addr + range.size - 1;
900 dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
901 range.flags, range.cpu_addr, end, range.pci_addr);
902
903 err = rcar_pcie_inbound_ranges(pcie, &range, &index);
904 if (err)
905 return err;
906 }
907
908 return 0;
909}
910
911static const struct of_device_id rcar_pcie_of_match[] = {
912 { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
913 { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init },
914 { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init },
915 {},
916};
917MODULE_DEVICE_TABLE(of, rcar_pcie_of_match);
918
919static int rcar_pcie_probe(struct platform_device *pdev)
920{
921 struct rcar_pcie *pcie;
922 unsigned int data;
923 struct of_pci_range range;
924 struct of_pci_range_parser parser;
925 const struct of_device_id *of_id;
926 int err, win = 0;
927 int (*hw_init_fn)(struct rcar_pcie *);
928
929 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
930 if (!pcie)
931 return -ENOMEM;
932
933 pcie->dev = &pdev->dev;
934 platform_set_drvdata(pdev, pcie);
935
936 /* Get the bus range */
937 if (of_pci_parse_bus_range(pdev->dev.of_node, &pcie->busn)) {
938 dev_err(&pdev->dev, "failed to parse bus-range property\n");
939 return -EINVAL;
940 }
941
942 if (of_pci_range_parser_init(&parser, pdev->dev.of_node)) {
943 dev_err(&pdev->dev, "missing ranges property\n");
944 return -EINVAL;
945 }
946
947 err = rcar_pcie_get_resources(pdev, pcie);
948 if (err < 0) {
949 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
950 return err;
951 }
952
953 for_each_of_pci_range(&parser, &range) {
954 of_pci_range_to_resource(&range, pdev->dev.of_node,
955 &pcie->res[win++]);
956
957 if (win > PCI_MAX_RESOURCES)
958 break;
959 }
960
961 err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node);
962 if (err)
963 return err;
964
965 if (IS_ENABLED(CONFIG_PCI_MSI)) {
966 err = rcar_pcie_enable_msi(pcie);
967 if (err < 0) {
968 dev_err(&pdev->dev,
969 "failed to enable MSI support: %d\n",
970 err);
971 return err;
972 }
973 }
974
975 of_id = of_match_device(rcar_pcie_of_match, pcie->dev);
976 if (!of_id || !of_id->data)
977 return -EINVAL;
978 hw_init_fn = of_id->data;
979
980 /* Failure to get a link might just be that no cards are inserted */
981 err = hw_init_fn(pcie);
982 if (err) {
983 dev_info(&pdev->dev, "PCIe link down\n");
984 return 0;
985 }
986
987 data = pci_read_reg(pcie, MACSR);
988 dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
989
990 rcar_pcie_enable(pcie);
991
992 return 0;
993}
994
995static struct platform_driver rcar_pcie_driver = {
996 .driver = {
997 .name = DRV_NAME,
998 .owner = THIS_MODULE,
999 .of_match_table = rcar_pcie_of_match,
1000 .suppress_bind_attrs = true,
1001 },
1002 .probe = rcar_pcie_probe,
1003};
1004module_platform_driver(rcar_pcie_driver);
1005
1006MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
1007MODULE_DESCRIPTION("Renesas R-Car PCIe driver");
1008MODULE_LICENSE("GPLv2");
diff --git a/drivers/pci/hotplug-pci.c b/drivers/pci/hotplug-pci.c
index 6258dc260d9f..c68366cee6b7 100644
--- a/drivers/pci/hotplug-pci.c
+++ b/drivers/pci/hotplug-pci.c
@@ -4,7 +4,7 @@
4#include <linux/export.h> 4#include <linux/export.h>
5#include "pci.h" 5#include "pci.h"
6 6
7int __ref pci_hp_add_bridge(struct pci_dev *dev) 7int pci_hp_add_bridge(struct pci_dev *dev)
8{ 8{
9 struct pci_bus *parent = dev->bus; 9 struct pci_bus *parent = dev->bus;
10 int pass, busnr, start = parent->busn_res.start; 10 int pass, busnr, start = parent->busn_res.start;
diff --git a/drivers/pci/hotplug/acpiphp_glue.c b/drivers/pci/hotplug/acpiphp_glue.c
index bccc27ee1030..75e178330215 100644
--- a/drivers/pci/hotplug/acpiphp_glue.c
+++ b/drivers/pci/hotplug/acpiphp_glue.c
@@ -41,7 +41,6 @@
41 41
42#define pr_fmt(fmt) "acpiphp_glue: " fmt 42#define pr_fmt(fmt) "acpiphp_glue: " fmt
43 43
44#include <linux/init.h>
45#include <linux/module.h> 44#include <linux/module.h>
46 45
47#include <linux/kernel.h> 46#include <linux/kernel.h>
@@ -501,7 +500,7 @@ static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
501 * This function should be called per *physical slot*, 500 * This function should be called per *physical slot*,
502 * not per each slot object in ACPI namespace. 501 * not per each slot object in ACPI namespace.
503 */ 502 */
504static void __ref enable_slot(struct acpiphp_slot *slot) 503static void enable_slot(struct acpiphp_slot *slot)
505{ 504{
506 struct pci_dev *dev; 505 struct pci_dev *dev;
507 struct pci_bus *bus = slot->bus; 506 struct pci_bus *bus = slot->bus;
@@ -516,8 +515,7 @@ static void __ref enable_slot(struct acpiphp_slot *slot)
516 if (PCI_SLOT(dev->devfn) != slot->device) 515 if (PCI_SLOT(dev->devfn) != slot->device)
517 continue; 516 continue;
518 517
519 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 518 if (pci_is_bridge(dev)) {
520 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
521 max = pci_scan_bridge(bus, dev, max, pass); 519 max = pci_scan_bridge(bus, dev, max, pass);
522 if (pass && dev->subordinate) { 520 if (pass && dev->subordinate) {
523 check_hotplug_bridge(slot, dev); 521 check_hotplug_bridge(slot, dev);
diff --git a/drivers/pci/hotplug/cpci_hotplug_pci.c b/drivers/pci/hotplug/cpci_hotplug_pci.c
index 8c1464851768..f6ef64c2ccb5 100644
--- a/drivers/pci/hotplug/cpci_hotplug_pci.c
+++ b/drivers/pci/hotplug/cpci_hotplug_pci.c
@@ -250,7 +250,7 @@ int cpci_led_off(struct slot* slot)
250 * Device configuration functions 250 * Device configuration functions
251 */ 251 */
252 252
253int __ref cpci_configure_slot(struct slot *slot) 253int cpci_configure_slot(struct slot *slot)
254{ 254{
255 struct pci_dev *dev; 255 struct pci_dev *dev;
256 struct pci_bus *parent; 256 struct pci_bus *parent;
@@ -289,8 +289,7 @@ int __ref cpci_configure_slot(struct slot *slot)
289 list_for_each_entry(dev, &parent->devices, bus_list) 289 list_for_each_entry(dev, &parent->devices, bus_list)
290 if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn)) 290 if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn))
291 continue; 291 continue;
292 if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) || 292 if (pci_is_bridge(dev))
293 (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS))
294 pci_hp_add_bridge(dev); 293 pci_hp_add_bridge(dev);
295 294
296 295
diff --git a/drivers/pci/hotplug/cpqphp_ctrl.c b/drivers/pci/hotplug/cpqphp_ctrl.c
index 11845b796799..f593585f2784 100644
--- a/drivers/pci/hotplug/cpqphp_ctrl.c
+++ b/drivers/pci/hotplug/cpqphp_ctrl.c
@@ -709,7 +709,8 @@ static struct pci_resource *get_max_resource(struct pci_resource **head, u32 siz
709 temp = temp->next; 709 temp = temp->next;
710 } 710 }
711 711
712 temp->next = max->next; 712 if (temp)
713 temp->next = max->next;
713 } 714 }
714 715
715 max->next = NULL; 716 max->next = NULL;
diff --git a/drivers/pci/hotplug/cpqphp_nvram.c b/drivers/pci/hotplug/cpqphp_nvram.c
index 76ba8a1c774d..9600a392eaae 100644
--- a/drivers/pci/hotplug/cpqphp_nvram.c
+++ b/drivers/pci/hotplug/cpqphp_nvram.c
@@ -34,7 +34,6 @@
34#include <linux/workqueue.h> 34#include <linux/workqueue.h>
35#include <linux/pci.h> 35#include <linux/pci.h>
36#include <linux/pci_hotplug.h> 36#include <linux/pci_hotplug.h>
37#include <linux/init.h>
38#include <asm/uaccess.h> 37#include <asm/uaccess.h>
39#include "cpqphp.h" 38#include "cpqphp.h"
40#include "cpqphp_nvram.h" 39#include "cpqphp_nvram.h"
diff --git a/drivers/pci/hotplug/pciehp.h b/drivers/pci/hotplug/pciehp.h
index 8a66866b8cf1..8e9012dca450 100644
--- a/drivers/pci/hotplug/pciehp.h
+++ b/drivers/pci/hotplug/pciehp.h
@@ -127,7 +127,7 @@ struct controller {
127#define HP_SUPR_RM(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_HPS) 127#define HP_SUPR_RM(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_HPS)
128#define EMI(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_EIP) 128#define EMI(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_EIP)
129#define NO_CMD_CMPL(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_NCCS) 129#define NO_CMD_CMPL(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_NCCS)
130#define PSN(ctrl) ((ctrl)->slot_cap >> 19) 130#define PSN(ctrl) (((ctrl)->slot_cap & PCI_EXP_SLTCAP_PSN) >> 19)
131 131
132int pciehp_sysfs_enable_slot(struct slot *slot); 132int pciehp_sysfs_enable_slot(struct slot *slot);
133int pciehp_sysfs_disable_slot(struct slot *slot); 133int pciehp_sysfs_disable_slot(struct slot *slot);
diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index d7d058fa19a4..1463412cf7f8 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -159,6 +159,8 @@ static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
159 159
160 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status); 160 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
161 if (slot_status & PCI_EXP_SLTSTA_CC) { 161 if (slot_status & PCI_EXP_SLTSTA_CC) {
162 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
163 PCI_EXP_SLTSTA_CC);
162 if (!ctrl->no_cmd_complete) { 164 if (!ctrl->no_cmd_complete) {
163 /* 165 /*
164 * After 1 sec and CMD_COMPLETED still not set, just 166 * After 1 sec and CMD_COMPLETED still not set, just
diff --git a/drivers/pci/hotplug/pciehp_pci.c b/drivers/pci/hotplug/pciehp_pci.c
index 1b533060ce65..b6cb1df67097 100644
--- a/drivers/pci/hotplug/pciehp_pci.c
+++ b/drivers/pci/hotplug/pciehp_pci.c
@@ -62,8 +62,7 @@ int pciehp_configure_device(struct slot *p_slot)
62 } 62 }
63 63
64 list_for_each_entry(dev, &parent->devices, bus_list) 64 list_for_each_entry(dev, &parent->devices, bus_list)
65 if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) || 65 if (pci_is_bridge(dev))
66 (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS))
67 pci_hp_add_bridge(dev); 66 pci_hp_add_bridge(dev);
68 67
69 pci_assign_unassigned_bridge_resources(bridge); 68 pci_assign_unassigned_bridge_resources(bridge);
diff --git a/drivers/pci/hotplug/pcihp_slot.c b/drivers/pci/hotplug/pcihp_slot.c
index 16f920352317..e246a10a0d2c 100644
--- a/drivers/pci/hotplug/pcihp_slot.c
+++ b/drivers/pci/hotplug/pcihp_slot.c
@@ -160,8 +160,7 @@ void pci_configure_slot(struct pci_dev *dev)
160 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI))) 160 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
161 return; 161 return;
162 162
163 if (dev->bus) 163 pcie_bus_configure_settings(dev->bus);
164 pcie_bus_configure_settings(dev->bus);
165 164
166 memset(&hpp, 0, sizeof(hpp)); 165 memset(&hpp, 0, sizeof(hpp));
167 ret = pci_get_hp_params(dev, &hpp); 166 ret = pci_get_hp_params(dev, &hpp);
diff --git a/drivers/pci/hotplug/rpadlpar_core.c b/drivers/pci/hotplug/rpadlpar_core.c
index 4fcdeedda31b..7660232ef460 100644
--- a/drivers/pci/hotplug/rpadlpar_core.c
+++ b/drivers/pci/hotplug/rpadlpar_core.c
@@ -157,8 +157,7 @@ static void dlpar_pci_add_bus(struct device_node *dn)
157 } 157 }
158 158
159 /* Scan below the new bridge */ 159 /* Scan below the new bridge */
160 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 160 if (pci_is_bridge(dev))
161 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
162 of_scan_pci_bridge(dev); 161 of_scan_pci_bridge(dev);
163 162
164 /* Map IO space for child bus, which may or may not succeed */ 163 /* Map IO space for child bus, which may or may not succeed */
diff --git a/drivers/pci/hotplug/rpaphp_core.c b/drivers/pci/hotplug/rpaphp_core.c
index 4796c15fba94..984d708552f6 100644
--- a/drivers/pci/hotplug/rpaphp_core.c
+++ b/drivers/pci/hotplug/rpaphp_core.c
@@ -223,16 +223,16 @@ int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
223 type_tmp = (char *) &types[1]; 223 type_tmp = (char *) &types[1];
224 224
225 /* Iterate through parent properties, looking for my-drc-index */ 225 /* Iterate through parent properties, looking for my-drc-index */
226 for (i = 0; i < indexes[0]; i++) { 226 for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
227 if ((unsigned int) indexes[i + 1] == *my_index) { 227 if ((unsigned int) indexes[i + 1] == *my_index) {
228 if (drc_name) 228 if (drc_name)
229 *drc_name = name_tmp; 229 *drc_name = name_tmp;
230 if (drc_type) 230 if (drc_type)
231 *drc_type = type_tmp; 231 *drc_type = type_tmp;
232 if (drc_index) 232 if (drc_index)
233 *drc_index = *my_index; 233 *drc_index = be32_to_cpu(*my_index);
234 if (drc_power_domain) 234 if (drc_power_domain)
235 *drc_power_domain = domains[i+1]; 235 *drc_power_domain = be32_to_cpu(domains[i+1]);
236 return 0; 236 return 0;
237 } 237 }
238 name_tmp += (strlen(name_tmp) + 1); 238 name_tmp += (strlen(name_tmp) + 1);
@@ -321,16 +321,19 @@ int rpaphp_add_slot(struct device_node *dn)
321 /* register PCI devices */ 321 /* register PCI devices */
322 name = (char *) &names[1]; 322 name = (char *) &names[1];
323 type = (char *) &types[1]; 323 type = (char *) &types[1];
324 for (i = 0; i < indexes[0]; i++) { 324 for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
325 int index;
325 326
326 slot = alloc_slot_struct(dn, indexes[i + 1], name, power_domains[i + 1]); 327 index = be32_to_cpu(indexes[i + 1]);
328 slot = alloc_slot_struct(dn, index, name,
329 be32_to_cpu(power_domains[i + 1]));
327 if (!slot) 330 if (!slot)
328 return -ENOMEM; 331 return -ENOMEM;
329 332
330 slot->type = simple_strtoul(type, NULL, 10); 333 slot->type = simple_strtoul(type, NULL, 10);
331 334
332 dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n", 335 dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
333 indexes[i + 1], name, type); 336 index, name, type);
334 337
335 retval = rpaphp_enable_slot(slot); 338 retval = rpaphp_enable_slot(slot);
336 if (!retval) 339 if (!retval)
diff --git a/drivers/pci/hotplug/s390_pci_hpc.c b/drivers/pci/hotplug/s390_pci_hpc.c
index 8d2ce22151eb..d1332d2f8730 100644
--- a/drivers/pci/hotplug/s390_pci_hpc.c
+++ b/drivers/pci/hotplug/s390_pci_hpc.c
@@ -15,7 +15,6 @@
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/pci.h> 16#include <linux/pci.h>
17#include <linux/pci_hotplug.h> 17#include <linux/pci_hotplug.h>
18#include <linux/init.h>
19#include <asm/pci_debug.h> 18#include <asm/pci_debug.h>
20#include <asm/sclp.h> 19#include <asm/sclp.h>
21 20
diff --git a/drivers/pci/hotplug/shpchp_pci.c b/drivers/pci/hotplug/shpchp_pci.c
index 2bf69fe1926c..9202d133485c 100644
--- a/drivers/pci/hotplug/shpchp_pci.c
+++ b/drivers/pci/hotplug/shpchp_pci.c
@@ -34,7 +34,7 @@
34#include "../pci.h" 34#include "../pci.h"
35#include "shpchp.h" 35#include "shpchp.h"
36 36
37int __ref shpchp_configure_device(struct slot *p_slot) 37int shpchp_configure_device(struct slot *p_slot)
38{ 38{
39 struct pci_dev *dev; 39 struct pci_dev *dev;
40 struct controller *ctrl = p_slot->ctrl; 40 struct controller *ctrl = p_slot->ctrl;
@@ -64,8 +64,7 @@ int __ref shpchp_configure_device(struct slot *p_slot)
64 list_for_each_entry(dev, &parent->devices, bus_list) { 64 list_for_each_entry(dev, &parent->devices, bus_list) {
65 if (PCI_SLOT(dev->devfn) != p_slot->device) 65 if (PCI_SLOT(dev->devfn) != p_slot->device)
66 continue; 66 continue;
67 if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) || 67 if (pci_is_bridge(dev))
68 (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS))
69 pci_hp_add_bridge(dev); 68 pci_hp_add_bridge(dev);
70 } 69 }
71 70
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index de7a74782f92..cb6f24740ee3 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -106,7 +106,7 @@ static int virtfn_add(struct pci_dev *dev, int id, int reset)
106 pci_device_add(virtfn, virtfn->bus); 106 pci_device_add(virtfn, virtfn->bus);
107 mutex_unlock(&iov->dev->sriov->lock); 107 mutex_unlock(&iov->dev->sriov->lock);
108 108
109 rc = pci_bus_add_device(virtfn); 109 pci_bus_add_device(virtfn);
110 sprintf(buf, "virtfn%u", id); 110 sprintf(buf, "virtfn%u", id);
111 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf); 111 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
112 if (rc) 112 if (rc)
diff --git a/drivers/pci/msi.c b/drivers/pci/msi.c
index 955ab7990c5b..27a7e67ddfe4 100644
--- a/drivers/pci/msi.c
+++ b/drivers/pci/msi.c
@@ -10,7 +10,6 @@
10#include <linux/mm.h> 10#include <linux/mm.h>
11#include <linux/irq.h> 11#include <linux/irq.h>
12#include <linux/interrupt.h> 12#include <linux/interrupt.h>
13#include <linux/init.h>
14#include <linux/export.h> 13#include <linux/export.h>
15#include <linux/ioport.h> 14#include <linux/ioport.h>
16#include <linux/pci.h> 15#include <linux/pci.h>
@@ -544,22 +543,18 @@ static int populate_msi_sysfs(struct pci_dev *pdev)
544 if (!msi_attrs) 543 if (!msi_attrs)
545 return -ENOMEM; 544 return -ENOMEM;
546 list_for_each_entry(entry, &pdev->msi_list, list) { 545 list_for_each_entry(entry, &pdev->msi_list, list) {
547 char *name = kmalloc(20, GFP_KERNEL);
548 if (!name)
549 goto error_attrs;
550
551 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); 546 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
552 if (!msi_dev_attr) { 547 if (!msi_dev_attr)
553 kfree(name);
554 goto error_attrs; 548 goto error_attrs;
555 } 549 msi_attrs[count] = &msi_dev_attr->attr;
556 550
557 sprintf(name, "%d", entry->irq);
558 sysfs_attr_init(&msi_dev_attr->attr); 551 sysfs_attr_init(&msi_dev_attr->attr);
559 msi_dev_attr->attr.name = name; 552 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
553 entry->irq);
554 if (!msi_dev_attr->attr.name)
555 goto error_attrs;
560 msi_dev_attr->attr.mode = S_IRUGO; 556 msi_dev_attr->attr.mode = S_IRUGO;
561 msi_dev_attr->show = msi_mode_show; 557 msi_dev_attr->show = msi_mode_show;
562 msi_attrs[count] = &msi_dev_attr->attr;
563 ++count; 558 ++count;
564 } 559 }
565 560
@@ -883,50 +878,6 @@ int pci_msi_vec_count(struct pci_dev *dev)
883} 878}
884EXPORT_SYMBOL(pci_msi_vec_count); 879EXPORT_SYMBOL(pci_msi_vec_count);
885 880
886/**
887 * pci_enable_msi_block - configure device's MSI capability structure
888 * @dev: device to configure
889 * @nvec: number of interrupts to configure
890 *
891 * Allocate IRQs for a device with the MSI capability.
892 * This function returns a negative errno if an error occurs. If it
893 * is unable to allocate the number of interrupts requested, it returns
894 * the number of interrupts it might be able to allocate. If it successfully
895 * allocates at least the number of interrupts requested, it returns 0 and
896 * updates the @dev's irq member to the lowest new interrupt number; the
897 * other interrupt numbers allocated to this device are consecutive.
898 */
899int pci_enable_msi_block(struct pci_dev *dev, int nvec)
900{
901 int status, maxvec;
902
903 if (dev->current_state != PCI_D0)
904 return -EINVAL;
905
906 maxvec = pci_msi_vec_count(dev);
907 if (maxvec < 0)
908 return maxvec;
909 if (nvec > maxvec)
910 return maxvec;
911
912 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
913 if (status)
914 return status;
915
916 WARN_ON(!!dev->msi_enabled);
917
918 /* Check whether driver already requested MSI-X irqs */
919 if (dev->msix_enabled) {
920 dev_info(&dev->dev, "can't enable MSI "
921 "(MSI-X already enabled)\n");
922 return -EINVAL;
923 }
924
925 status = msi_capability_init(dev, nvec);
926 return status;
927}
928EXPORT_SYMBOL(pci_enable_msi_block);
929
930void pci_msi_shutdown(struct pci_dev *dev) 881void pci_msi_shutdown(struct pci_dev *dev)
931{ 882{
932 struct msi_desc *desc; 883 struct msi_desc *desc;
@@ -1132,14 +1083,45 @@ void pci_msi_init_pci_dev(struct pci_dev *dev)
1132 **/ 1083 **/
1133int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec) 1084int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
1134{ 1085{
1135 int nvec = maxvec; 1086 int nvec;
1136 int rc; 1087 int rc;
1137 1088
1089 if (dev->current_state != PCI_D0)
1090 return -EINVAL;
1091
1092 WARN_ON(!!dev->msi_enabled);
1093
1094 /* Check whether driver already requested MSI-X irqs */
1095 if (dev->msix_enabled) {
1096 dev_info(&dev->dev,
1097 "can't enable MSI (MSI-X already enabled)\n");
1098 return -EINVAL;
1099 }
1100
1138 if (maxvec < minvec) 1101 if (maxvec < minvec)
1139 return -ERANGE; 1102 return -ERANGE;
1140 1103
1104 nvec = pci_msi_vec_count(dev);
1105 if (nvec < 0)
1106 return nvec;
1107 else if (nvec < minvec)
1108 return -EINVAL;
1109 else if (nvec > maxvec)
1110 nvec = maxvec;
1111
1112 do {
1113 rc = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
1114 if (rc < 0) {
1115 return rc;
1116 } else if (rc > 0) {
1117 if (rc < minvec)
1118 return -ENOSPC;
1119 nvec = rc;
1120 }
1121 } while (rc);
1122
1141 do { 1123 do {
1142 rc = pci_enable_msi_block(dev, nvec); 1124 rc = msi_capability_init(dev, nvec);
1143 if (rc < 0) { 1125 if (rc < 0) {
1144 return rc; 1126 return rc;
1145 } else if (rc > 0) { 1127 } else if (rc > 0) {
diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c
index f49abef88485..ca4927ba8433 100644
--- a/drivers/pci/pci-acpi.c
+++ b/drivers/pci/pci-acpi.c
@@ -309,13 +309,7 @@ static struct acpi_device *acpi_pci_find_companion(struct device *dev)
309 bool check_children; 309 bool check_children;
310 u64 addr; 310 u64 addr;
311 311
312 /* 312 check_children = pci_is_bridge(pci_dev);
313 * pci_is_bridge() is not suitable here, because pci_dev->subordinate
314 * is set only after acpi_pci_find_device() has been called for the
315 * given device.
316 */
317 check_children = pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE
318 || pci_dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
319 /* Please ref to ACPI spec for the syntax of _ADR */ 313 /* Please ref to ACPI spec for the syntax of _ADR */
320 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn); 314 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
321 return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr, 315 return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index d911e0c1f359..837d71f5390b 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -107,7 +107,7 @@ store_new_id(struct device_driver *driver, const char *buf, size_t count)
107 subdevice=PCI_ANY_ID, class=0, class_mask=0; 107 subdevice=PCI_ANY_ID, class=0, class_mask=0;
108 unsigned long driver_data=0; 108 unsigned long driver_data=0;
109 int fields=0; 109 int fields=0;
110 int retval; 110 int retval = 0;
111 111
112 fields = sscanf(buf, "%x %x %x %x %x %x %lx", 112 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
113 &vendor, &device, &subvendor, &subdevice, 113 &vendor, &device, &subvendor, &subdevice,
@@ -115,6 +115,26 @@ store_new_id(struct device_driver *driver, const char *buf, size_t count)
115 if (fields < 2) 115 if (fields < 2)
116 return -EINVAL; 116 return -EINVAL;
117 117
118 if (fields != 7) {
119 struct pci_dev *pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
120 if (!pdev)
121 return -ENOMEM;
122
123 pdev->vendor = vendor;
124 pdev->device = device;
125 pdev->subsystem_vendor = subvendor;
126 pdev->subsystem_device = subdevice;
127 pdev->class = class;
128
129 if (pci_match_id(pdrv->id_table, pdev))
130 retval = -EEXIST;
131
132 kfree(pdev);
133
134 if (retval)
135 return retval;
136 }
137
118 /* Only accept driver_data values that match an existing id_table 138 /* Only accept driver_data values that match an existing id_table
119 entry */ 139 entry */
120 if (ids) { 140 if (ids) {
@@ -216,6 +236,13 @@ const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
216 return NULL; 236 return NULL;
217} 237}
218 238
239static const struct pci_device_id pci_device_id_any = {
240 .vendor = PCI_ANY_ID,
241 .device = PCI_ANY_ID,
242 .subvendor = PCI_ANY_ID,
243 .subdevice = PCI_ANY_ID,
244};
245
219/** 246/**
220 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure 247 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
221 * @drv: the PCI driver to match against 248 * @drv: the PCI driver to match against
@@ -229,18 +256,30 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
229 struct pci_dev *dev) 256 struct pci_dev *dev)
230{ 257{
231 struct pci_dynid *dynid; 258 struct pci_dynid *dynid;
259 const struct pci_device_id *found_id = NULL;
260
261 /* When driver_override is set, only bind to the matching driver */
262 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
263 return NULL;
232 264
233 /* Look at the dynamic ids first, before the static ones */ 265 /* Look at the dynamic ids first, before the static ones */
234 spin_lock(&drv->dynids.lock); 266 spin_lock(&drv->dynids.lock);
235 list_for_each_entry(dynid, &drv->dynids.list, node) { 267 list_for_each_entry(dynid, &drv->dynids.list, node) {
236 if (pci_match_one_device(&dynid->id, dev)) { 268 if (pci_match_one_device(&dynid->id, dev)) {
237 spin_unlock(&drv->dynids.lock); 269 found_id = &dynid->id;
238 return &dynid->id; 270 break;
239 } 271 }
240 } 272 }
241 spin_unlock(&drv->dynids.lock); 273 spin_unlock(&drv->dynids.lock);
242 274
243 return pci_match_id(drv->id_table, dev); 275 if (!found_id)
276 found_id = pci_match_id(drv->id_table, dev);
277
278 /* driver_override will always match, send a dummy id */
279 if (!found_id && dev->driver_override)
280 found_id = &pci_device_id_any;
281
282 return found_id;
244} 283}
245 284
246struct drv_dev_and_id { 285struct drv_dev_and_id {
@@ -580,14 +619,14 @@ static void pci_pm_default_resume(struct pci_dev *pci_dev)
580{ 619{
581 pci_fixup_device(pci_fixup_resume, pci_dev); 620 pci_fixup_device(pci_fixup_resume, pci_dev);
582 621
583 if (!pci_is_bridge(pci_dev)) 622 if (!pci_has_subordinate(pci_dev))
584 pci_enable_wake(pci_dev, PCI_D0, false); 623 pci_enable_wake(pci_dev, PCI_D0, false);
585} 624}
586 625
587static void pci_pm_default_suspend(struct pci_dev *pci_dev) 626static void pci_pm_default_suspend(struct pci_dev *pci_dev)
588{ 627{
589 /* Disable non-bridge devices without PM support */ 628 /* Disable non-bridge devices without PM support */
590 if (!pci_is_bridge(pci_dev)) 629 if (!pci_has_subordinate(pci_dev))
591 pci_disable_enabled_device(pci_dev); 630 pci_disable_enabled_device(pci_dev);
592} 631}
593 632
@@ -717,7 +756,7 @@ static int pci_pm_suspend_noirq(struct device *dev)
717 756
718 if (!pci_dev->state_saved) { 757 if (!pci_dev->state_saved) {
719 pci_save_state(pci_dev); 758 pci_save_state(pci_dev);
720 if (!pci_is_bridge(pci_dev)) 759 if (!pci_has_subordinate(pci_dev))
721 pci_prepare_to_sleep(pci_dev); 760 pci_prepare_to_sleep(pci_dev);
722 } 761 }
723 762
@@ -971,7 +1010,7 @@ static int pci_pm_poweroff_noirq(struct device *dev)
971 return error; 1010 return error;
972 } 1011 }
973 1012
974 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev)) 1013 if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev))
975 pci_prepare_to_sleep(pci_dev); 1014 pci_prepare_to_sleep(pci_dev);
976 1015
977 /* 1016 /*
@@ -1325,8 +1364,6 @@ static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1325 return -ENODEV; 1364 return -ENODEV;
1326 1365
1327 pdev = to_pci_dev(dev); 1366 pdev = to_pci_dev(dev);
1328 if (!pdev)
1329 return -ENODEV;
1330 1367
1331 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class)) 1368 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
1332 return -ENOMEM; 1369 return -ENOMEM;
@@ -1347,6 +1384,7 @@ static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1347 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8), 1384 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
1348 (u8)(pdev->class))) 1385 (u8)(pdev->class)))
1349 return -ENOMEM; 1386 return -ENOMEM;
1387
1350 return 0; 1388 return 0;
1351} 1389}
1352 1390
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 4e0acefb7565..84c350994b06 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -29,6 +29,7 @@
29#include <linux/slab.h> 29#include <linux/slab.h>
30#include <linux/vgaarb.h> 30#include <linux/vgaarb.h>
31#include <linux/pm_runtime.h> 31#include <linux/pm_runtime.h>
32#include <linux/of.h>
32#include "pci.h" 33#include "pci.h"
33 34
34static int sysfs_initialized; /* = 0 */ 35static int sysfs_initialized; /* = 0 */
@@ -416,6 +417,20 @@ static ssize_t d3cold_allowed_show(struct device *dev,
416static DEVICE_ATTR_RW(d3cold_allowed); 417static DEVICE_ATTR_RW(d3cold_allowed);
417#endif 418#endif
418 419
420#ifdef CONFIG_OF
421static ssize_t devspec_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 struct pci_dev *pdev = to_pci_dev(dev);
425 struct device_node *np = pci_device_to_OF_node(pdev);
426
427 if (np == NULL || np->full_name == NULL)
428 return 0;
429 return sprintf(buf, "%s", np->full_name);
430}
431static DEVICE_ATTR_RO(devspec);
432#endif
433
419#ifdef CONFIG_PCI_IOV 434#ifdef CONFIG_PCI_IOV
420static ssize_t sriov_totalvfs_show(struct device *dev, 435static ssize_t sriov_totalvfs_show(struct device *dev,
421 struct device_attribute *attr, 436 struct device_attribute *attr,
@@ -499,6 +514,45 @@ static struct device_attribute sriov_numvfs_attr =
499 sriov_numvfs_show, sriov_numvfs_store); 514 sriov_numvfs_show, sriov_numvfs_store);
500#endif /* CONFIG_PCI_IOV */ 515#endif /* CONFIG_PCI_IOV */
501 516
517static ssize_t driver_override_store(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t count)
520{
521 struct pci_dev *pdev = to_pci_dev(dev);
522 char *driver_override, *old = pdev->driver_override, *cp;
523
524 if (count > PATH_MAX)
525 return -EINVAL;
526
527 driver_override = kstrndup(buf, count, GFP_KERNEL);
528 if (!driver_override)
529 return -ENOMEM;
530
531 cp = strchr(driver_override, '\n');
532 if (cp)
533 *cp = '\0';
534
535 if (strlen(driver_override)) {
536 pdev->driver_override = driver_override;
537 } else {
538 kfree(driver_override);
539 pdev->driver_override = NULL;
540 }
541
542 kfree(old);
543
544 return count;
545}
546
547static ssize_t driver_override_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
549{
550 struct pci_dev *pdev = to_pci_dev(dev);
551
552 return sprintf(buf, "%s\n", pdev->driver_override);
553}
554static DEVICE_ATTR_RW(driver_override);
555
502static struct attribute *pci_dev_attrs[] = { 556static struct attribute *pci_dev_attrs[] = {
503 &dev_attr_resource.attr, 557 &dev_attr_resource.attr,
504 &dev_attr_vendor.attr, 558 &dev_attr_vendor.attr,
@@ -521,6 +575,10 @@ static struct attribute *pci_dev_attrs[] = {
521#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI) 575#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
522 &dev_attr_d3cold_allowed.attr, 576 &dev_attr_d3cold_allowed.attr,
523#endif 577#endif
578#ifdef CONFIG_OF
579 &dev_attr_devspec.attr,
580#endif
581 &dev_attr_driver_override.attr,
524 NULL, 582 NULL,
525}; 583};
526 584
@@ -1255,11 +1313,6 @@ static struct bin_attribute pcie_config_attr = {
1255 .write = pci_write_config, 1313 .write = pci_write_config,
1256}; 1314};
1257 1315
1258int __weak pcibios_add_platform_entries(struct pci_dev *dev)
1259{
1260 return 0;
1261}
1262
1263static ssize_t reset_store(struct device *dev, 1316static ssize_t reset_store(struct device *dev,
1264 struct device_attribute *attr, const char *buf, 1317 struct device_attribute *attr, const char *buf,
1265 size_t count) 1318 size_t count)
@@ -1375,11 +1428,6 @@ int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1375 pdev->rom_attr = attr; 1428 pdev->rom_attr = attr;
1376 } 1429 }
1377 1430
1378 /* add platform-specific attributes */
1379 retval = pcibios_add_platform_entries(pdev);
1380 if (retval)
1381 goto err_rom_file;
1382
1383 /* add sysfs entries for various capabilities */ 1431 /* add sysfs entries for various capabilities */
1384 retval = pci_create_capabilities_sysfs(pdev); 1432 retval = pci_create_capabilities_sysfs(pdev);
1385 if (retval) 1433 if (retval)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 759475ef6ff3..7ae7aa0166b6 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -1468,6 +1468,17 @@ void __weak pcibios_release_device(struct pci_dev *dev) {}
1468 */ 1468 */
1469void __weak pcibios_disable_device (struct pci_dev *dev) {} 1469void __weak pcibios_disable_device (struct pci_dev *dev) {}
1470 1470
1471/**
1472 * pcibios_penalize_isa_irq - penalize an ISA IRQ
1473 * @irq: ISA IRQ to penalize
1474 * @active: IRQ active or not
1475 *
1476 * Permits the platform to provide architecture-specific functionality when
1477 * penalizing ISA IRQs. This is the default implementation. Architecture
1478 * implementations can override this.
1479 */
1480void __weak pcibios_penalize_isa_irq(int irq, int active) {}
1481
1471static void do_pci_disable_device(struct pci_dev *dev) 1482static void do_pci_disable_device(struct pci_dev *dev)
1472{ 1483{
1473 u16 pci_command; 1484 u16 pci_command;
@@ -3306,8 +3317,27 @@ static void pci_dev_unlock(struct pci_dev *dev)
3306 pci_cfg_access_unlock(dev); 3317 pci_cfg_access_unlock(dev);
3307} 3318}
3308 3319
3320/**
3321 * pci_reset_notify - notify device driver of reset
3322 * @dev: device to be notified of reset
3323 * @prepare: 'true' if device is about to be reset; 'false' if reset attempt
3324 * completed
3325 *
3326 * Must be called prior to device access being disabled and after device
3327 * access is restored.
3328 */
3329static void pci_reset_notify(struct pci_dev *dev, bool prepare)
3330{
3331 const struct pci_error_handlers *err_handler =
3332 dev->driver ? dev->driver->err_handler : NULL;
3333 if (err_handler && err_handler->reset_notify)
3334 err_handler->reset_notify(dev, prepare);
3335}
3336
3309static void pci_dev_save_and_disable(struct pci_dev *dev) 3337static void pci_dev_save_and_disable(struct pci_dev *dev)
3310{ 3338{
3339 pci_reset_notify(dev, true);
3340
3311 /* 3341 /*
3312 * Wake-up device prior to save. PM registers default to D0 after 3342 * Wake-up device prior to save. PM registers default to D0 after
3313 * reset and a simple register restore doesn't reliably return 3343 * reset and a simple register restore doesn't reliably return
@@ -3329,6 +3359,7 @@ static void pci_dev_save_and_disable(struct pci_dev *dev)
3329static void pci_dev_restore(struct pci_dev *dev) 3359static void pci_dev_restore(struct pci_dev *dev)
3330{ 3360{
3331 pci_restore_state(dev); 3361 pci_restore_state(dev);
3362 pci_reset_notify(dev, false);
3332} 3363}
3333 3364
3334static int pci_dev_reset(struct pci_dev *dev, int probe) 3365static int pci_dev_reset(struct pci_dev *dev, int probe)
@@ -3345,6 +3376,7 @@ static int pci_dev_reset(struct pci_dev *dev, int probe)
3345 3376
3346 return rc; 3377 return rc;
3347} 3378}
3379
3348/** 3380/**
3349 * __pci_reset_function - reset a PCI device function 3381 * __pci_reset_function - reset a PCI device function
3350 * @dev: PCI device to reset 3382 * @dev: PCI device to reset
@@ -4126,7 +4158,7 @@ int pci_set_vga_state(struct pci_dev *dev, bool decode,
4126 u16 cmd; 4158 u16 cmd;
4127 int rc; 4159 int rc;
4128 4160
4129 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) & (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY))); 4161 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
4130 4162
4131 /* ARCH specific VGA enables */ 4163 /* ARCH specific VGA enables */
4132 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags); 4164 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 6bd082299e31..0601890db22d 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -77,7 +77,7 @@ static inline void pci_wakeup_event(struct pci_dev *dev)
77 pm_wakeup_event(&dev->dev, 100); 77 pm_wakeup_event(&dev->dev, 100);
78} 78}
79 79
80static inline bool pci_is_bridge(struct pci_dev *pci_dev) 80static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
81{ 81{
82 return !!(pci_dev->subordinate); 82 return !!(pci_dev->subordinate);
83} 83}
@@ -201,11 +201,11 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
201 struct resource *res, unsigned int reg); 201 struct resource *res, unsigned int reg);
202int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type); 202int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type);
203void pci_configure_ari(struct pci_dev *dev); 203void pci_configure_ari(struct pci_dev *dev);
204void __ref __pci_bus_size_bridges(struct pci_bus *bus, 204void __pci_bus_size_bridges(struct pci_bus *bus,
205 struct list_head *realloc_head); 205 struct list_head *realloc_head);
206void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 206void __pci_bus_assign_resources(const struct pci_bus *bus,
207 struct list_head *realloc_head, 207 struct list_head *realloc_head,
208 struct list_head *fail_head); 208 struct list_head *fail_head);
209 209
210/** 210/**
211 * pci_ari_enabled - query ARI forwarding status 211 * pci_ari_enabled - query ARI forwarding status
diff --git a/drivers/pci/pcie/portdrv_core.c b/drivers/pci/pcie/portdrv_core.c
index 986f8eadfd39..2f0ce668a775 100644
--- a/drivers/pci/pcie/portdrv_core.c
+++ b/drivers/pci/pcie/portdrv_core.c
@@ -99,7 +99,7 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
99 for (i = 0; i < nr_entries; i++) 99 for (i = 0; i < nr_entries; i++)
100 msix_entries[i].entry = i; 100 msix_entries[i].entry = i;
101 101
102 status = pci_enable_msix(dev, msix_entries, nr_entries); 102 status = pci_enable_msix_exact(dev, msix_entries, nr_entries);
103 if (status) 103 if (status)
104 goto Exit; 104 goto Exit;
105 105
@@ -171,7 +171,7 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
171 pci_disable_msix(dev); 171 pci_disable_msix(dev);
172 172
173 /* Now allocate the MSI-X vectors for real */ 173 /* Now allocate the MSI-X vectors for real */
174 status = pci_enable_msix(dev, msix_entries, nvec); 174 status = pci_enable_msix_exact(dev, msix_entries, nvec);
175 if (status) 175 if (status)
176 goto Exit; 176 goto Exit;
177 } 177 }
@@ -379,10 +379,13 @@ int pcie_port_device_register(struct pci_dev *dev)
379 /* 379 /*
380 * Initialize service irqs. Don't use service devices that 380 * Initialize service irqs. Don't use service devices that
381 * require interrupts if there is no way to generate them. 381 * require interrupts if there is no way to generate them.
382 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
383 * that can be used in the absence of irqs. Allow them to determine
384 * if that is to be used.
382 */ 385 */
383 status = init_service_irqs(dev, irqs, capabilities); 386 status = init_service_irqs(dev, irqs, capabilities);
384 if (status) { 387 if (status) {
385 capabilities &= PCIE_PORT_SERVICE_VC; 388 capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
386 if (!capabilities) 389 if (!capabilities)
387 goto error_disable; 390 goto error_disable;
388 } 391 }
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index ef09f5f2fe6c..2bbf5221afb3 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -171,9 +171,10 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
171 struct resource *res, unsigned int pos) 171 struct resource *res, unsigned int pos)
172{ 172{
173 u32 l, sz, mask; 173 u32 l, sz, mask;
174 u64 l64, sz64, mask64;
174 u16 orig_cmd; 175 u16 orig_cmd;
175 struct pci_bus_region region, inverted_region; 176 struct pci_bus_region region, inverted_region;
176 bool bar_too_big = false, bar_disabled = false; 177 bool bar_too_big = false, bar_too_high = false, bar_invalid = false;
177 178
178 mask = type ? PCI_ROM_ADDRESS_MASK : ~0; 179 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
179 180
@@ -226,9 +227,9 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
226 } 227 }
227 228
228 if (res->flags & IORESOURCE_MEM_64) { 229 if (res->flags & IORESOURCE_MEM_64) {
229 u64 l64 = l; 230 l64 = l;
230 u64 sz64 = sz; 231 sz64 = sz;
231 u64 mask64 = mask | (u64)~0 << 32; 232 mask64 = mask | (u64)~0 << 32;
232 233
233 pci_read_config_dword(dev, pos + 4, &l); 234 pci_read_config_dword(dev, pos + 4, &l);
234 pci_write_config_dword(dev, pos + 4, ~0); 235 pci_write_config_dword(dev, pos + 4, ~0);
@@ -243,19 +244,22 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
243 if (!sz64) 244 if (!sz64)
244 goto fail; 245 goto fail;
245 246
246 if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) { 247 if ((sizeof(dma_addr_t) < 8 || sizeof(resource_size_t) < 8) &&
248 sz64 > 0x100000000ULL) {
249 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
250 res->start = 0;
251 res->end = 0;
247 bar_too_big = true; 252 bar_too_big = true;
248 goto fail; 253 goto out;
249 } 254 }
250 255
251 if ((sizeof(resource_size_t) < 8) && l) { 256 if ((sizeof(dma_addr_t) < 8) && l) {
252 /* Address above 32-bit boundary; disable the BAR */ 257 /* Above 32-bit boundary; try to reallocate */
253 pci_write_config_dword(dev, pos, 0);
254 pci_write_config_dword(dev, pos + 4, 0);
255 res->flags |= IORESOURCE_UNSET; 258 res->flags |= IORESOURCE_UNSET;
256 region.start = 0; 259 res->start = 0;
257 region.end = sz64; 260 res->end = sz64;
258 bar_disabled = true; 261 bar_too_high = true;
262 goto out;
259 } else { 263 } else {
260 region.start = l64; 264 region.start = l64;
261 region.end = l64 + sz64; 265 region.end = l64 + sz64;
@@ -285,11 +289,10 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
285 * be claimed by the device. 289 * be claimed by the device.
286 */ 290 */
287 if (inverted_region.start != region.start) { 291 if (inverted_region.start != region.start) {
288 dev_info(&dev->dev, "reg 0x%x: initial BAR value %pa invalid; forcing reassignment\n",
289 pos, &region.start);
290 res->flags |= IORESOURCE_UNSET; 292 res->flags |= IORESOURCE_UNSET;
291 res->end -= res->start;
292 res->start = 0; 293 res->start = 0;
294 res->end = region.end - region.start;
295 bar_invalid = true;
293 } 296 }
294 297
295 goto out; 298 goto out;
@@ -303,8 +306,15 @@ out:
303 pci_write_config_word(dev, PCI_COMMAND, orig_cmd); 306 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
304 307
305 if (bar_too_big) 308 if (bar_too_big)
306 dev_err(&dev->dev, "reg 0x%x: can't handle 64-bit BAR\n", pos); 309 dev_err(&dev->dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
307 if (res->flags && !bar_disabled) 310 pos, (unsigned long long) sz64);
311 if (bar_too_high)
312 dev_info(&dev->dev, "reg 0x%x: can't handle BAR above 4G (bus address %#010llx)\n",
313 pos, (unsigned long long) l64);
314 if (bar_invalid)
315 dev_info(&dev->dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
316 pos, (unsigned long long) region.start);
317 if (res->flags)
308 dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res); 318 dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res);
309 319
310 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0; 320 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
@@ -465,7 +475,7 @@ void pci_read_bridge_bases(struct pci_bus *child)
465 475
466 if (dev->transparent) { 476 if (dev->transparent) {
467 pci_bus_for_each_resource(child->parent, res, i) { 477 pci_bus_for_each_resource(child->parent, res, i) {
468 if (res) { 478 if (res && res->flags) {
469 pci_bus_add_resource(child, res, 479 pci_bus_add_resource(child, res,
470 PCI_SUBTRACTIVE_DECODE); 480 PCI_SUBTRACTIVE_DECODE);
471 dev_printk(KERN_DEBUG, &dev->dev, 481 dev_printk(KERN_DEBUG, &dev->dev,
@@ -719,7 +729,7 @@ add_dev:
719 return child; 729 return child;
720} 730}
721 731
722struct pci_bus *__ref pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr) 732struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
723{ 733{
724 struct pci_bus *child; 734 struct pci_bus *child;
725 735
@@ -984,6 +994,43 @@ void set_pcie_hotplug_bridge(struct pci_dev *pdev)
984 994
985 995
986/** 996/**
997 * pci_ext_cfg_is_aliased - is ext config space just an alias of std config?
998 * @dev: PCI device
999 *
1000 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1001 * when forwarding a type1 configuration request the bridge must check that
1002 * the extended register address field is zero. The bridge is not permitted
1003 * to forward the transactions and must handle it as an Unsupported Request.
1004 * Some bridges do not follow this rule and simply drop the extended register
1005 * bits, resulting in the standard config space being aliased, every 256
1006 * bytes across the entire configuration space. Test for this condition by
1007 * comparing the first dword of each potential alias to the vendor/device ID.
1008 * Known offenders:
1009 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1010 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1011 */
1012static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1013{
1014#ifdef CONFIG_PCI_QUIRKS
1015 int pos;
1016 u32 header, tmp;
1017
1018 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1019
1020 for (pos = PCI_CFG_SPACE_SIZE;
1021 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1022 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1023 || header != tmp)
1024 return false;
1025 }
1026
1027 return true;
1028#else
1029 return false;
1030#endif
1031}
1032
1033/**
987 * pci_cfg_space_size - get the configuration space size of the PCI device. 1034 * pci_cfg_space_size - get the configuration space size of the PCI device.
988 * @dev: PCI device 1035 * @dev: PCI device
989 * 1036 *
@@ -1001,7 +1048,7 @@ static int pci_cfg_space_size_ext(struct pci_dev *dev)
1001 1048
1002 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL) 1049 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1003 goto fail; 1050 goto fail;
1004 if (status == 0xffffffff) 1051 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
1005 goto fail; 1052 goto fail;
1006 1053
1007 return PCI_CFG_SPACE_EXP_SIZE; 1054 return PCI_CFG_SPACE_EXP_SIZE;
@@ -1215,6 +1262,7 @@ static void pci_release_dev(struct device *dev)
1215 pci_release_of_node(pci_dev); 1262 pci_release_of_node(pci_dev);
1216 pcibios_release_device(pci_dev); 1263 pcibios_release_device(pci_dev);
1217 pci_bus_put(pci_dev->bus); 1264 pci_bus_put(pci_dev->bus);
1265 kfree(pci_dev->driver_override);
1218 kfree(pci_dev); 1266 kfree(pci_dev);
1219} 1267}
1220 1268
@@ -1369,7 +1417,7 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
1369 WARN_ON(ret < 0); 1417 WARN_ON(ret < 0);
1370} 1418}
1371 1419
1372struct pci_dev *__ref pci_scan_single_device(struct pci_bus *bus, int devfn) 1420struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
1373{ 1421{
1374 struct pci_dev *dev; 1422 struct pci_dev *dev;
1375 1423
@@ -1617,7 +1665,7 @@ static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
1617 */ 1665 */
1618void pcie_bus_configure_settings(struct pci_bus *bus) 1666void pcie_bus_configure_settings(struct pci_bus *bus)
1619{ 1667{
1620 u8 smpss; 1668 u8 smpss = 0;
1621 1669
1622 if (!bus->self) 1670 if (!bus->self)
1623 return; 1671 return;
@@ -1670,8 +1718,7 @@ unsigned int pci_scan_child_bus(struct pci_bus *bus)
1670 1718
1671 for (pass=0; pass < 2; pass++) 1719 for (pass=0; pass < 2; pass++)
1672 list_for_each_entry(dev, &bus->devices, bus_list) { 1720 list_for_each_entry(dev, &bus->devices, bus_list) {
1673 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 1721 if (pci_is_bridge(dev))
1674 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1675 max = pci_scan_bridge(bus, dev, max, pass); 1722 max = pci_scan_bridge(bus, dev, max, pass);
1676 } 1723 }
1677 1724
@@ -1958,7 +2005,7 @@ EXPORT_SYMBOL(pci_scan_bus);
1958 * 2005 *
1959 * Returns the max number of subordinate bus discovered. 2006 * Returns the max number of subordinate bus discovered.
1960 */ 2007 */
1961unsigned int __ref pci_rescan_bus_bridge_resize(struct pci_dev *bridge) 2008unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
1962{ 2009{
1963 unsigned int max; 2010 unsigned int max;
1964 struct pci_bus *bus = bridge->subordinate; 2011 struct pci_bus *bus = bridge->subordinate;
@@ -1981,7 +2028,7 @@ unsigned int __ref pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
1981 * 2028 *
1982 * Returns the max number of subordinate bus discovered. 2029 * Returns the max number of subordinate bus discovered.
1983 */ 2030 */
1984unsigned int __ref pci_rescan_bus(struct pci_bus *bus) 2031unsigned int pci_rescan_bus(struct pci_bus *bus)
1985{ 2032{
1986 unsigned int max; 2033 unsigned int max;
1987 2034
diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index e7292065a1b1..92e68c7747f7 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -2954,6 +2954,7 @@ static void disable_igfx_irq(struct pci_dev *dev)
2954} 2954}
2955DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0102, disable_igfx_irq); 2955DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0102, disable_igfx_irq);
2956DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x010a, disable_igfx_irq); 2956DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x010a, disable_igfx_irq);
2957DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0152, disable_igfx_irq);
2957 2958
2958/* 2959/*
2959 * PCI devices which are on Intel chips can skip the 10ms delay 2960 * PCI devices which are on Intel chips can skip the 10ms delay
@@ -2991,6 +2992,14 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CHELSIO, 0x0030,
2991 quirk_broken_intx_masking); 2992 quirk_broken_intx_masking);
2992DECLARE_PCI_FIXUP_HEADER(0x1814, 0x0601, /* Ralink RT2800 802.11n PCI */ 2993DECLARE_PCI_FIXUP_HEADER(0x1814, 0x0601, /* Ralink RT2800 802.11n PCI */
2993 quirk_broken_intx_masking); 2994 quirk_broken_intx_masking);
2995/*
2996 * Realtek RTL8169 PCI Gigabit Ethernet Controller (rev 10)
2997 * Subsystem: Realtek RTL8169/8110 Family PCI Gigabit Ethernet NIC
2998 *
2999 * RTL8110SC - Fails under PCI device assignment using DisINTx masking.
3000 */
3001DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_REALTEK, 0x8169,
3002 quirk_broken_intx_masking);
2994 3003
2995static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f, 3004static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f,
2996 struct pci_fixup *end) 3005 struct pci_fixup *end)
@@ -3453,6 +3462,8 @@ static const u16 pci_quirk_intel_pch_acs_ids[] = {
3453 /* Wildcat PCH */ 3462 /* Wildcat PCH */
3454 0x9c90, 0x9c91, 0x9c92, 0x9c93, 0x9c94, 0x9c95, 0x9c96, 0x9c97, 3463 0x9c90, 0x9c91, 0x9c92, 0x9c93, 0x9c94, 0x9c95, 0x9c96, 0x9c97,
3455 0x9c98, 0x9c99, 0x9c9a, 0x9c9b, 3464 0x9c98, 0x9c99, 0x9c9a, 0x9c9b,
3465 /* Patsburg (X79) PCH */
3466 0x1d10, 0x1d12, 0x1d14, 0x1d16, 0x1d18, 0x1d1a, 0x1d1c, 0x1d1e,
3456}; 3467};
3457 3468
3458static bool pci_quirk_intel_pch_acs_match(struct pci_dev *dev) 3469static bool pci_quirk_intel_pch_acs_match(struct pci_dev *dev)
diff --git a/drivers/pci/search.c b/drivers/pci/search.c
index 4a1b972efe7f..8e495bda678f 100644
--- a/drivers/pci/search.c
+++ b/drivers/pci/search.c
@@ -7,7 +7,6 @@
7 * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <greg@kroah.com> 7 * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <greg@kroah.com>
8 */ 8 */
9 9
10#include <linux/init.h>
11#include <linux/pci.h> 10#include <linux/pci.h>
12#include <linux/slab.h> 11#include <linux/slab.h>
13#include <linux/module.h> 12#include <linux/module.h>
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 138bdd6393be..fd9b545c3cf5 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -713,12 +713,11 @@ static void pci_bridge_check_ranges(struct pci_bus *bus)
713 bus resource of a given type. Note: we intentionally skip 713 bus resource of a given type. Note: we intentionally skip
714 the bus resources which have already been assigned (that is, 714 the bus resources which have already been assigned (that is,
715 have non-NULL parent resource). */ 715 have non-NULL parent resource). */
716static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 716static struct resource *find_free_bus_resource(struct pci_bus *bus,
717 unsigned long type_mask, unsigned long type)
717{ 718{
718 int i; 719 int i;
719 struct resource *r; 720 struct resource *r;
720 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
721 IORESOURCE_PREFETCH;
722 721
723 pci_bus_for_each_resource(bus, r, i) { 722 pci_bus_for_each_resource(bus, r, i) {
724 if (r == &ioport_resource || r == &iomem_resource) 723 if (r == &ioport_resource || r == &iomem_resource)
@@ -815,7 +814,8 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
815 resource_size_t add_size, struct list_head *realloc_head) 814 resource_size_t add_size, struct list_head *realloc_head)
816{ 815{
817 struct pci_dev *dev; 816 struct pci_dev *dev;
818 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 817 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
818 IORESOURCE_IO);
819 resource_size_t size = 0, size0 = 0, size1 = 0; 819 resource_size_t size = 0, size0 = 0, size1 = 0;
820 resource_size_t children_add_size = 0; 820 resource_size_t children_add_size = 0;
821 resource_size_t min_align, align; 821 resource_size_t min_align, align;
@@ -907,36 +907,40 @@ static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
907 * @bus : the bus 907 * @bus : the bus
908 * @mask: mask the resource flag, then compare it with type 908 * @mask: mask the resource flag, then compare it with type
909 * @type: the type of free resource from bridge 909 * @type: the type of free resource from bridge
910 * @type2: second match type
911 * @type3: third match type
910 * @min_size : the minimum memory window that must to be allocated 912 * @min_size : the minimum memory window that must to be allocated
911 * @add_size : additional optional memory window 913 * @add_size : additional optional memory window
912 * @realloc_head : track the additional memory window on this list 914 * @realloc_head : track the additional memory window on this list
913 * 915 *
914 * Calculate the size of the bus and minimal alignment which 916 * Calculate the size of the bus and minimal alignment which
915 * guarantees that all child resources fit in this size. 917 * guarantees that all child resources fit in this size.
918 *
919 * Returns -ENOSPC if there's no available bus resource of the desired type.
920 * Otherwise, sets the bus resource start/end to indicate the required
921 * size, adds things to realloc_head (if supplied), and returns 0.
916 */ 922 */
917static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 923static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
918 unsigned long type, resource_size_t min_size, 924 unsigned long type, unsigned long type2,
919 resource_size_t add_size, 925 unsigned long type3,
920 struct list_head *realloc_head) 926 resource_size_t min_size, resource_size_t add_size,
927 struct list_head *realloc_head)
921{ 928{
922 struct pci_dev *dev; 929 struct pci_dev *dev;
923 resource_size_t min_align, align, size, size0, size1; 930 resource_size_t min_align, align, size, size0, size1;
924 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 931 resource_size_t aligns[14]; /* Alignments from 1Mb to 8Gb */
925 int order, max_order; 932 int order, max_order;
926 struct resource *b_res = find_free_bus_resource(bus, type); 933 struct resource *b_res = find_free_bus_resource(bus,
927 unsigned int mem64_mask = 0; 934 mask | IORESOURCE_PREFETCH, type);
928 resource_size_t children_add_size = 0; 935 resource_size_t children_add_size = 0;
929 936
930 if (!b_res) 937 if (!b_res)
931 return 0; 938 return -ENOSPC;
932 939
933 memset(aligns, 0, sizeof(aligns)); 940 memset(aligns, 0, sizeof(aligns));
934 max_order = 0; 941 max_order = 0;
935 size = 0; 942 size = 0;
936 943
937 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
938 b_res->flags &= ~IORESOURCE_MEM_64;
939
940 list_for_each_entry(dev, &bus->devices, bus_list) { 944 list_for_each_entry(dev, &bus->devices, bus_list) {
941 int i; 945 int i;
942 946
@@ -944,7 +948,9 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
944 struct resource *r = &dev->resource[i]; 948 struct resource *r = &dev->resource[i];
945 resource_size_t r_size; 949 resource_size_t r_size;
946 950
947 if (r->parent || (r->flags & mask) != type) 951 if (r->parent || ((r->flags & mask) != type &&
952 (r->flags & mask) != type2 &&
953 (r->flags & mask) != type3))
948 continue; 954 continue;
949 r_size = resource_size(r); 955 r_size = resource_size(r);
950#ifdef CONFIG_PCI_IOV 956#ifdef CONFIG_PCI_IOV
@@ -957,10 +963,17 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
957 continue; 963 continue;
958 } 964 }
959#endif 965#endif
960 /* For bridges size != alignment */ 966 /*
967 * aligns[0] is for 1MB (since bridge memory
968 * windows are always at least 1MB aligned), so
969 * keep "order" from being negative for smaller
970 * resources.
971 */
961 align = pci_resource_alignment(dev, r); 972 align = pci_resource_alignment(dev, r);
962 order = __ffs(align) - 20; 973 order = __ffs(align) - 20;
963 if (order > 11) { 974 if (order < 0)
975 order = 0;
976 if (order >= ARRAY_SIZE(aligns)) {
964 dev_warn(&dev->dev, "disabling BAR %d: %pR " 977 dev_warn(&dev->dev, "disabling BAR %d: %pR "
965 "(bad alignment %#llx)\n", i, r, 978 "(bad alignment %#llx)\n", i, r,
966 (unsigned long long) align); 979 (unsigned long long) align);
@@ -968,15 +981,12 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
968 continue; 981 continue;
969 } 982 }
970 size += r_size; 983 size += r_size;
971 if (order < 0)
972 order = 0;
973 /* Exclude ranges with size > align from 984 /* Exclude ranges with size > align from
974 calculation of the alignment. */ 985 calculation of the alignment. */
975 if (r_size == align) 986 if (r_size == align)
976 aligns[order] += align; 987 aligns[order] += align;
977 if (order > max_order) 988 if (order > max_order)
978 max_order = order; 989 max_order = order;
979 mem64_mask &= r->flags & IORESOURCE_MEM_64;
980 990
981 if (realloc_head) 991 if (realloc_head)
982 children_add_size += get_res_add_size(realloc_head, r); 992 children_add_size += get_res_add_size(realloc_head, r);
@@ -997,18 +1007,18 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
997 "%pR to %pR (unused)\n", b_res, 1007 "%pR to %pR (unused)\n", b_res,
998 &bus->busn_res); 1008 &bus->busn_res);
999 b_res->flags = 0; 1009 b_res->flags = 0;
1000 return 1; 1010 return 0;
1001 } 1011 }
1002 b_res->start = min_align; 1012 b_res->start = min_align;
1003 b_res->end = size0 + min_align - 1; 1013 b_res->end = size0 + min_align - 1;
1004 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; 1014 b_res->flags |= IORESOURCE_STARTALIGN;
1005 if (size1 > size0 && realloc_head) { 1015 if (size1 > size0 && realloc_head) {
1006 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 1016 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
1007 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 1017 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
1008 "%pR to %pR add_size %llx\n", b_res, 1018 "%pR to %pR add_size %llx\n", b_res,
1009 &bus->busn_res, (unsigned long long)size1-size0); 1019 &bus->busn_res, (unsigned long long)size1-size0);
1010 } 1020 }
1011 return 1; 1021 return 0;
1012} 1022}
1013 1023
1014unsigned long pci_cardbus_resource_alignment(struct resource *res) 1024unsigned long pci_cardbus_resource_alignment(struct resource *res)
@@ -1113,12 +1123,13 @@ handle_done:
1113 ; 1123 ;
1114} 1124}
1115 1125
1116void __ref __pci_bus_size_bridges(struct pci_bus *bus, 1126void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
1117 struct list_head *realloc_head)
1118{ 1127{
1119 struct pci_dev *dev; 1128 struct pci_dev *dev;
1120 unsigned long mask, prefmask; 1129 unsigned long mask, prefmask, type2 = 0, type3 = 0;
1121 resource_size_t additional_mem_size = 0, additional_io_size = 0; 1130 resource_size_t additional_mem_size = 0, additional_io_size = 0;
1131 struct resource *b_res;
1132 int ret;
1122 1133
1123 list_for_each_entry(dev, &bus->devices, bus_list) { 1134 list_for_each_entry(dev, &bus->devices, bus_list) {
1124 struct pci_bus *b = dev->subordinate; 1135 struct pci_bus *b = dev->subordinate;
@@ -1152,41 +1163,93 @@ void __ref __pci_bus_size_bridges(struct pci_bus *bus,
1152 additional_io_size = pci_hotplug_io_size; 1163 additional_io_size = pci_hotplug_io_size;
1153 additional_mem_size = pci_hotplug_mem_size; 1164 additional_mem_size = pci_hotplug_mem_size;
1154 } 1165 }
1155 /* 1166 /* Fall through */
1156 * Follow thru
1157 */
1158 default: 1167 default:
1159 pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 1168 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1160 additional_io_size, realloc_head); 1169 additional_io_size, realloc_head);
1161 /* If the bridge supports prefetchable range, size it 1170
1162 separately. If it doesn't, or its prefetchable window 1171 /*
1163 has already been allocated by arch code, try 1172 * If there's a 64-bit prefetchable MMIO window, compute
1164 non-prefetchable range for both types of PCI memory 1173 * the size required to put all 64-bit prefetchable
1165 resources. */ 1174 * resources in it.
1175 */
1176 b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
1166 mask = IORESOURCE_MEM; 1177 mask = IORESOURCE_MEM;
1167 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 1178 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
1168 if (pbus_size_mem(bus, prefmask, prefmask, 1179 if (b_res[2].flags & IORESOURCE_MEM_64) {
1180 prefmask |= IORESOURCE_MEM_64;
1181 ret = pbus_size_mem(bus, prefmask, prefmask,
1182 prefmask, prefmask,
1169 realloc_head ? 0 : additional_mem_size, 1183 realloc_head ? 0 : additional_mem_size,
1170 additional_mem_size, realloc_head)) 1184 additional_mem_size, realloc_head);
1171 mask = prefmask; /* Success, size non-prefetch only. */ 1185
1172 else 1186 /*
1173 additional_mem_size += additional_mem_size; 1187 * If successful, all non-prefetchable resources
1174 pbus_size_mem(bus, mask, IORESOURCE_MEM, 1188 * and any 32-bit prefetchable resources will go in
1189 * the non-prefetchable window.
1190 */
1191 if (ret == 0) {
1192 mask = prefmask;
1193 type2 = prefmask & ~IORESOURCE_MEM_64;
1194 type3 = prefmask & ~IORESOURCE_PREFETCH;
1195 }
1196 }
1197
1198 /*
1199 * If there is no 64-bit prefetchable window, compute the
1200 * size required to put all prefetchable resources in the
1201 * 32-bit prefetchable window (if there is one).
1202 */
1203 if (!type2) {
1204 prefmask &= ~IORESOURCE_MEM_64;
1205 ret = pbus_size_mem(bus, prefmask, prefmask,
1206 prefmask, prefmask,
1207 realloc_head ? 0 : additional_mem_size,
1208 additional_mem_size, realloc_head);
1209
1210 /*
1211 * If successful, only non-prefetchable resources
1212 * will go in the non-prefetchable window.
1213 */
1214 if (ret == 0)
1215 mask = prefmask;
1216 else
1217 additional_mem_size += additional_mem_size;
1218
1219 type2 = type3 = IORESOURCE_MEM;
1220 }
1221
1222 /*
1223 * Compute the size required to put everything else in the
1224 * non-prefetchable window. This includes:
1225 *
1226 * - all non-prefetchable resources
1227 * - 32-bit prefetchable resources if there's a 64-bit
1228 * prefetchable window or no prefetchable window at all
1229 * - 64-bit prefetchable resources if there's no
1230 * prefetchable window at all
1231 *
1232 * Note that the strategy in __pci_assign_resource() must
1233 * match that used here. Specifically, we cannot put a
1234 * 32-bit prefetchable resource in a 64-bit prefetchable
1235 * window.
1236 */
1237 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
1175 realloc_head ? 0 : additional_mem_size, 1238 realloc_head ? 0 : additional_mem_size,
1176 additional_mem_size, realloc_head); 1239 additional_mem_size, realloc_head);
1177 break; 1240 break;
1178 } 1241 }
1179} 1242}
1180 1243
1181void __ref pci_bus_size_bridges(struct pci_bus *bus) 1244void pci_bus_size_bridges(struct pci_bus *bus)
1182{ 1245{
1183 __pci_bus_size_bridges(bus, NULL); 1246 __pci_bus_size_bridges(bus, NULL);
1184} 1247}
1185EXPORT_SYMBOL(pci_bus_size_bridges); 1248EXPORT_SYMBOL(pci_bus_size_bridges);
1186 1249
1187void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 1250void __pci_bus_assign_resources(const struct pci_bus *bus,
1188 struct list_head *realloc_head, 1251 struct list_head *realloc_head,
1189 struct list_head *fail_head) 1252 struct list_head *fail_head)
1190{ 1253{
1191 struct pci_bus *b; 1254 struct pci_bus *b;
1192 struct pci_dev *dev; 1255 struct pci_dev *dev;
@@ -1218,15 +1281,15 @@ void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
1218 } 1281 }
1219} 1282}
1220 1283
1221void __ref pci_bus_assign_resources(const struct pci_bus *bus) 1284void pci_bus_assign_resources(const struct pci_bus *bus)
1222{ 1285{
1223 __pci_bus_assign_resources(bus, NULL, NULL); 1286 __pci_bus_assign_resources(bus, NULL, NULL);
1224} 1287}
1225EXPORT_SYMBOL(pci_bus_assign_resources); 1288EXPORT_SYMBOL(pci_bus_assign_resources);
1226 1289
1227static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 1290static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1228 struct list_head *add_head, 1291 struct list_head *add_head,
1229 struct list_head *fail_head) 1292 struct list_head *fail_head)
1230{ 1293{
1231 struct pci_bus *b; 1294 struct pci_bus *b;
1232 1295
@@ -1257,42 +1320,66 @@ static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
1257static void pci_bridge_release_resources(struct pci_bus *bus, 1320static void pci_bridge_release_resources(struct pci_bus *bus,
1258 unsigned long type) 1321 unsigned long type)
1259{ 1322{
1260 int idx; 1323 struct pci_dev *dev = bus->self;
1261 bool changed = false;
1262 struct pci_dev *dev;
1263 struct resource *r; 1324 struct resource *r;
1264 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1325 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1265 IORESOURCE_PREFETCH; 1326 IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
1327 unsigned old_flags = 0;
1328 struct resource *b_res;
1329 int idx = 1;
1266 1330
1267 dev = bus->self; 1331 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
1268 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; 1332
1269 idx++) { 1333 /*
1270 r = &dev->resource[idx]; 1334 * 1. if there is io port assign fail, will release bridge
1271 if ((r->flags & type_mask) != type) 1335 * io port.
1272 continue; 1336 * 2. if there is non pref mmio assign fail, release bridge
1273 if (!r->parent) 1337 * nonpref mmio.
1274 continue; 1338 * 3. if there is 64bit pref mmio assign fail, and bridge pref
1275 /* 1339 * is 64bit, release bridge pref mmio.
1276 * if there are children under that, we should release them 1340 * 4. if there is pref mmio assign fail, and bridge pref is
1277 * all 1341 * 32bit mmio, release bridge pref mmio
1278 */ 1342 * 5. if there is pref mmio assign fail, and bridge pref is not
1279 release_child_resources(r); 1343 * assigned, release bridge nonpref mmio.
1280 if (!release_resource(r)) { 1344 */
1281 dev_printk(KERN_DEBUG, &dev->dev, 1345 if (type & IORESOURCE_IO)
1282 "resource %d %pR released\n", idx, r); 1346 idx = 0;
1283 /* keep the old size */ 1347 else if (!(type & IORESOURCE_PREFETCH))
1284 r->end = resource_size(r) - 1; 1348 idx = 1;
1285 r->start = 0; 1349 else if ((type & IORESOURCE_MEM_64) &&
1286 r->flags = 0; 1350 (b_res[2].flags & IORESOURCE_MEM_64))
1287 changed = true; 1351 idx = 2;
1288 } 1352 else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1289 } 1353 (b_res[2].flags & IORESOURCE_PREFETCH))
1354 idx = 2;
1355 else
1356 idx = 1;
1357
1358 r = &b_res[idx];
1359
1360 if (!r->parent)
1361 return;
1362
1363 /*
1364 * if there are children under that, we should release them
1365 * all
1366 */
1367 release_child_resources(r);
1368 if (!release_resource(r)) {
1369 type = old_flags = r->flags & type_mask;
1370 dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
1371 PCI_BRIDGE_RESOURCES + idx, r);
1372 /* keep the old size */
1373 r->end = resource_size(r) - 1;
1374 r->start = 0;
1375 r->flags = 0;
1290 1376
1291 if (changed) {
1292 /* avoiding touch the one without PREF */ 1377 /* avoiding touch the one without PREF */
1293 if (type & IORESOURCE_PREFETCH) 1378 if (type & IORESOURCE_PREFETCH)
1294 type = IORESOURCE_PREFETCH; 1379 type = IORESOURCE_PREFETCH;
1295 __pci_setup_bridge(bus, type); 1380 __pci_setup_bridge(bus, type);
1381 /* for next child res under same bridge */
1382 r->flags = old_flags;
1296 } 1383 }
1297} 1384}
1298 1385
@@ -1304,9 +1391,9 @@ enum release_type {
1304 * try to release pci bridge resources that is from leaf bridge, 1391 * try to release pci bridge resources that is from leaf bridge,
1305 * so we can allocate big new one later 1392 * so we can allocate big new one later
1306 */ 1393 */
1307static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, 1394static void pci_bus_release_bridge_resources(struct pci_bus *bus,
1308 unsigned long type, 1395 unsigned long type,
1309 enum release_type rel_type) 1396 enum release_type rel_type)
1310{ 1397{
1311 struct pci_dev *dev; 1398 struct pci_dev *dev;
1312 bool is_leaf_bridge = true; 1399 bool is_leaf_bridge = true;
@@ -1471,7 +1558,7 @@ void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
1471 LIST_HEAD(fail_head); 1558 LIST_HEAD(fail_head);
1472 struct pci_dev_resource *fail_res; 1559 struct pci_dev_resource *fail_res;
1473 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1560 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1474 IORESOURCE_PREFETCH; 1561 IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
1475 int pci_try_num = 1; 1562 int pci_try_num = 1;
1476 enum enable_type enable_local; 1563 enum enable_type enable_local;
1477 1564
@@ -1629,9 +1716,7 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
1629 1716
1630 down_read(&pci_bus_sem); 1717 down_read(&pci_bus_sem);
1631 list_for_each_entry(dev, &bus->devices, bus_list) 1718 list_for_each_entry(dev, &bus->devices, bus_list)
1632 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 1719 if (pci_is_bridge(dev) && pci_has_subordinate(dev))
1633 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1634 if (dev->subordinate)
1635 __pci_bus_size_bridges(dev->subordinate, 1720 __pci_bus_size_bridges(dev->subordinate,
1636 &add_list); 1721 &add_list);
1637 up_read(&pci_bus_sem); 1722 up_read(&pci_bus_sem);
diff --git a/drivers/pci/setup-irq.c b/drivers/pci/setup-irq.c
index 9bd6864ec5d3..dbc4ffcf42de 100644
--- a/drivers/pci/setup-irq.c
+++ b/drivers/pci/setup-irq.c
@@ -10,7 +10,6 @@
10 */ 10 */
11 11
12 12
13#include <linux/init.h>
14#include <linux/kernel.h> 13#include <linux/kernel.h>
15#include <linux/pci.h> 14#include <linux/pci.h>
16#include <linux/errno.h> 15#include <linux/errno.h>
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index 7eed671d5586..33f9e32d94d0 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -16,7 +16,6 @@
16 * Resource sorting 16 * Resource sorting
17 */ 17 */
18 18
19#include <linux/init.h>
20#include <linux/kernel.h> 19#include <linux/kernel.h>
21#include <linux/export.h> 20#include <linux/export.h>
22#include <linux/pci.h> 21#include <linux/pci.h>
@@ -209,21 +208,42 @@ static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
209 208
210 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; 209 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
211 210
212 /* First, try exact prefetching match.. */ 211 /*
212 * First, try exact prefetching match. Even if a 64-bit
213 * prefetchable bridge window is below 4GB, we can't put a 32-bit
214 * prefetchable resource in it because pbus_size_mem() assumes a
215 * 64-bit window will contain no 32-bit resources. If we assign
216 * things differently than they were sized, not everything will fit.
217 */
213 ret = pci_bus_alloc_resource(bus, res, size, align, min, 218 ret = pci_bus_alloc_resource(bus, res, size, align, min,
214 IORESOURCE_PREFETCH, 219 IORESOURCE_PREFETCH | IORESOURCE_MEM_64,
215 pcibios_align_resource, dev); 220 pcibios_align_resource, dev);
221 if (ret == 0)
222 return 0;
216 223
217 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { 224 /*
218 /* 225 * If the prefetchable window is only 32 bits wide, we can put
219 * That failed. 226 * 64-bit prefetchable resources in it.
220 * 227 */
221 * But a prefetching area can handle a non-prefetching 228 if ((res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) ==
222 * window (it will just not perform as well). 229 (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) {
223 */ 230 ret = pci_bus_alloc_resource(bus, res, size, align, min,
224 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, 231 IORESOURCE_PREFETCH,
225 pcibios_align_resource, dev); 232 pcibios_align_resource, dev);
233 if (ret == 0)
234 return 0;
226 } 235 }
236
237 /*
238 * If we didn't find a better match, we can put any memory resource
239 * in a non-prefetchable window. If this resource is 32 bits and
240 * non-prefetchable, the first call already tried the only possibility
241 * so we don't need to try again.
242 */
243 if (res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64))
244 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
245 pcibios_align_resource, dev);
246
227 return ret; 247 return ret;
228} 248}
229 249
diff --git a/drivers/pcmcia/cardbus.c b/drivers/pcmcia/cardbus.c
index 8bde61952d20..4fe4cc4ae19a 100644
--- a/drivers/pcmcia/cardbus.c
+++ b/drivers/pcmcia/cardbus.c
@@ -78,8 +78,7 @@ int __ref cb_alloc(struct pcmcia_socket *s)
78 max = bus->busn_res.start; 78 max = bus->busn_res.start;
79 for (pass = 0; pass < 2; pass++) 79 for (pass = 0; pass < 2; pass++)
80 list_for_each_entry(dev, &bus->devices, bus_list) 80 list_for_each_entry(dev, &bus->devices, bus_list)
81 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 81 if (pci_is_bridge(dev))
82 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
83 max = pci_scan_bridge(bus, dev, max, pass); 82 max = pci_scan_bridge(bus, dev, max, pass);
84 83
85 /* 84 /*
diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
index c5e082fb82fa..91ef69a52263 100644
--- a/drivers/platform/x86/asus-wmi.c
+++ b/drivers/platform/x86/asus-wmi.c
@@ -642,8 +642,7 @@ static void asus_rfkill_hotplug(struct asus_wmi *asus)
642 dev = pci_scan_single_device(bus, 0); 642 dev = pci_scan_single_device(bus, 0);
643 if (dev) { 643 if (dev) {
644 pci_bus_assign_resources(bus); 644 pci_bus_assign_resources(bus);
645 if (pci_bus_add_device(dev)) 645 pci_bus_add_device(dev);
646 pr_err("Unable to hotplug wifi\n");
647 } 646 }
648 } else { 647 } else {
649 dev = pci_get_slot(bus, 0); 648 dev = pci_get_slot(bus, 0);
diff --git a/drivers/platform/x86/eeepc-laptop.c b/drivers/platform/x86/eeepc-laptop.c
index 399e8c562192..9b0c57cd1d4a 100644
--- a/drivers/platform/x86/eeepc-laptop.c
+++ b/drivers/platform/x86/eeepc-laptop.c
@@ -633,8 +633,7 @@ static void eeepc_rfkill_hotplug(struct eeepc_laptop *eeepc, acpi_handle handle)
633 dev = pci_scan_single_device(bus, 0); 633 dev = pci_scan_single_device(bus, 0);
634 if (dev) { 634 if (dev) {
635 pci_bus_assign_resources(bus); 635 pci_bus_assign_resources(bus);
636 if (pci_bus_add_device(dev)) 636 pci_bus_add_device(dev);
637 pr_err("Unable to hotplug wifi\n");
638 } 637 }
639 } else { 638 } else {
640 dev = pci_get_slot(bus, 0); 639 dev = pci_get_slot(bus, 0);