Commit 1c03b5bf authored by Bjorn Helgaas's avatar Bjorn Helgaas

Merge branch 'pci/resource'

- Add pci_dev_for_each_resource() and pci_bus_for_each_resource() iterators
  to simplify loops (Andy Shevchenko)

* pci/resource:
  EISA: Drop unused pci_bus_for_each_resource() index argument
  PCI: Make pci_bus_for_each_resource() index optional
  PCI: Document pci_bus_for_each_resource()
  PCI: Introduce pci_dev_for_each_resource()
  PCI: Introduce pci_resource_n()
parents 43ca31e0 e34a6ba5
...@@ -520,6 +520,7 @@ ForEachMacros: ...@@ -520,6 +520,7 @@ ForEachMacros:
- 'of_property_for_each_string' - 'of_property_for_each_string'
- 'of_property_for_each_u32' - 'of_property_for_each_u32'
- 'pci_bus_for_each_resource' - 'pci_bus_for_each_resource'
- 'pci_dev_for_each_resource'
- 'pci_doe_for_each_off' - 'pci_doe_for_each_off'
- 'pcl_for_each_chunk' - 'pcl_for_each_chunk'
- 'pcl_for_each_segment' - 'pcl_for_each_segment'
......
...@@ -288,11 +288,10 @@ pcibios_claim_one_bus(struct pci_bus *b) ...@@ -288,11 +288,10 @@ pcibios_claim_one_bus(struct pci_bus *b)
struct pci_bus *child_bus; struct pci_bus *child_bus;
list_for_each_entry(dev, &b->devices, bus_list) { list_for_each_entry(dev, &b->devices, bus_list) {
struct resource *r;
int i; int i;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
struct resource *r = &dev->resource[i];
if (r->parent || !r->start || !r->flags) if (r->parent || !r->start || !r->flags)
continue; continue;
if (pci_has_flag(PCI_PROBE_ONLY) || if (pci_has_flag(PCI_PROBE_ONLY) ||
......
...@@ -142,15 +142,15 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_89C940F, ...@@ -142,15 +142,15 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_89C940F,
*/ */
static void pci_fixup_dec21285(struct pci_dev *dev) static void pci_fixup_dec21285(struct pci_dev *dev)
{ {
int i;
if (dev->devfn == 0) { if (dev->devfn == 0) {
struct resource *r;
dev->class &= 0xff; dev->class &= 0xff;
dev->class |= PCI_CLASS_BRIDGE_HOST << 8; dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = 0; r->start = 0;
dev->resource[i].end = 0; r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
} }
} }
...@@ -162,13 +162,11 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, pci_fixup_d ...@@ -162,13 +162,11 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, pci_fixup_d
static void pci_fixup_ide_bases(struct pci_dev *dev) static void pci_fixup_ide_bases(struct pci_dev *dev)
{ {
struct resource *r; struct resource *r;
int i;
if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE) if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
return; return;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r) {
r = dev->resource + i;
if ((r->start & ~0x80) == 0x374) { if ((r->start & ~0x80) == 0x374) {
r->start |= 2; r->start |= 2;
r->end = r->start; r->end = r->start;
......
...@@ -142,14 +142,14 @@ static struct pci_ops pcie_ops = { ...@@ -142,14 +142,14 @@ static struct pci_ops pcie_ops = {
static void rc_pci_fixup(struct pci_dev *dev) static void rc_pci_fixup(struct pci_dev *dev)
{ {
if (dev->bus->parent == NULL && dev->devfn == 0) { if (dev->bus->parent == NULL && dev->devfn == 0) {
int i; struct resource *r;
dev->class &= 0xff; dev->class &= 0xff;
dev->class |= PCI_CLASS_BRIDGE_HOST << 8; dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = 0; r->start = 0;
dev->resource[i].end = 0; r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
} }
} }
......
...@@ -186,14 +186,14 @@ static struct pci_ops pcie_ops = { ...@@ -186,14 +186,14 @@ static struct pci_ops pcie_ops = {
static void rc_pci_fixup(struct pci_dev *dev) static void rc_pci_fixup(struct pci_dev *dev)
{ {
if (dev->bus->parent == NULL && dev->devfn == 0) { if (dev->bus->parent == NULL && dev->devfn == 0) {
int i; struct resource *r;
dev->class &= 0xff; dev->class &= 0xff;
dev->class |= PCI_CLASS_BRIDGE_HOST << 8; dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = 0; r->start = 0;
dev->resource[i].end = 0; r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
} }
} }
......
...@@ -522,14 +522,14 @@ static int __init pci_setup(struct pci_sys_data *sys) ...@@ -522,14 +522,14 @@ static int __init pci_setup(struct pci_sys_data *sys)
static void rc_pci_fixup(struct pci_dev *dev) static void rc_pci_fixup(struct pci_dev *dev)
{ {
if (dev->bus->parent == NULL && dev->devfn == 0) { if (dev->bus->parent == NULL && dev->devfn == 0) {
int i; struct resource *r;
dev->class &= 0xff; dev->class &= 0xff;
dev->class |= PCI_CLASS_BRIDGE_HOST << 8; dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = 0; r->start = 0;
dev->resource[i].end = 0; r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
} }
} }
......
...@@ -413,18 +413,18 @@ struct pci_ops bcm63xx_cb_ops = { ...@@ -413,18 +413,18 @@ struct pci_ops bcm63xx_cb_ops = {
static void bcm63xx_fixup(struct pci_dev *dev) static void bcm63xx_fixup(struct pci_dev *dev)
{ {
static int io_window = -1; static int io_window = -1;
int i, found, new_io_window; int found, new_io_window;
struct resource *r;
u32 val; u32 val;
/* look for any io resource */ /* look for any io resource */
found = 0; found = 0;
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
if (pci_resource_flags(dev, i) & IORESOURCE_IO) { if (resource_type(r) == IORESOURCE_IO) {
found = 1; found = 1;
break; break;
} }
} }
if (!found) if (!found)
return; return;
......
...@@ -249,12 +249,11 @@ static int pcibios_enable_resources(struct pci_dev *dev, int mask) ...@@ -249,12 +249,11 @@ static int pcibios_enable_resources(struct pci_dev *dev, int mask)
pci_read_config_word(dev, PCI_COMMAND, &cmd); pci_read_config_word(dev, PCI_COMMAND, &cmd);
old_cmd = cmd; old_cmd = cmd;
for (idx = 0; idx < PCI_NUM_RESOURCES; idx++) { pci_dev_for_each_resource(dev, r, idx) {
/* Only set up the requested stuff */ /* Only set up the requested stuff */
if (!(mask & (1<<idx))) if (!(mask & (1<<idx)))
continue; continue;
r = &dev->resource[idx];
if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
continue; continue;
if ((idx == PCI_ROM_RESOURCE) && if ((idx == PCI_ROM_RESOURCE) &&
......
...@@ -880,6 +880,7 @@ int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge) ...@@ -880,6 +880,7 @@ int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
static void pcibios_fixup_resources(struct pci_dev *dev) static void pcibios_fixup_resources(struct pci_dev *dev)
{ {
struct pci_controller *hose = pci_bus_to_host(dev->bus); struct pci_controller *hose = pci_bus_to_host(dev->bus);
struct resource *res;
int i; int i;
if (!hose) { if (!hose) {
...@@ -891,9 +892,9 @@ static void pcibios_fixup_resources(struct pci_dev *dev) ...@@ -891,9 +892,9 @@ static void pcibios_fixup_resources(struct pci_dev *dev)
if (dev->is_virtfn) if (dev->is_virtfn)
return; return;
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, res, i) {
struct resource *res = dev->resource + i;
struct pci_bus_region reg; struct pci_bus_region reg;
if (!res->flags) if (!res->flags)
continue; continue;
...@@ -1452,11 +1453,10 @@ void pcibios_claim_one_bus(struct pci_bus *bus) ...@@ -1452,11 +1453,10 @@ void pcibios_claim_one_bus(struct pci_bus *bus)
struct pci_bus *child_bus; struct pci_bus *child_bus;
list_for_each_entry(dev, &bus->devices, bus_list) { list_for_each_entry(dev, &bus->devices, bus_list) {
struct resource *r;
int i; int i;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
struct resource *r = &dev->resource[i];
if (r->parent || !r->start || !r->flags) if (r->parent || !r->start || !r->flags)
continue; continue;
...@@ -1705,19 +1705,20 @@ EXPORT_SYMBOL_GPL(pcibios_scan_phb); ...@@ -1705,19 +1705,20 @@ EXPORT_SYMBOL_GPL(pcibios_scan_phb);
static void fixup_hide_host_resource_fsl(struct pci_dev *dev) static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
{ {
int i, class = dev->class >> 8; int class = dev->class >> 8;
/* When configured as agent, programming interface = 1 */ /* When configured as agent, programming interface = 1 */
int prog_if = dev->class & 0xf; int prog_if = dev->class & 0xf;
struct resource *r;
if ((class == PCI_CLASS_PROCESSOR_POWERPC || if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
class == PCI_CLASS_BRIDGE_OTHER) && class == PCI_CLASS_BRIDGE_OTHER) &&
(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) && (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
(prog_if == 0) && (prog_if == 0) &&
(dev->bus->parent == NULL)) { (dev->bus->parent == NULL)) {
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = 0; r->start = 0;
dev->resource[i].end = 0; r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
} }
} }
......
...@@ -57,7 +57,7 @@ static inline int ppc440spe_revA(void) ...@@ -57,7 +57,7 @@ static inline int ppc440spe_revA(void)
static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev) static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
{ {
struct pci_controller *hose; struct pci_controller *hose;
int i; struct resource *r;
if (dev->devfn != 0 || dev->bus->self != NULL) if (dev->devfn != 0 || dev->bus->self != NULL)
return; return;
...@@ -79,9 +79,9 @@ static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev) ...@@ -79,9 +79,9 @@ static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
/* Hide the PCI host BARs from the kernel as their content doesn't /* Hide the PCI host BARs from the kernel as their content doesn't
* fit well in the resource management * fit well in the resource management
*/ */
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = dev->resource[i].end = 0; r->start = r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n", printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n",
......
...@@ -327,14 +327,13 @@ mpc52xx_pci_setup(struct pci_controller *hose, ...@@ -327,14 +327,13 @@ mpc52xx_pci_setup(struct pci_controller *hose,
static void static void
mpc52xx_pci_fixup_resources(struct pci_dev *dev) mpc52xx_pci_fixup_resources(struct pci_dev *dev)
{ {
int i; struct resource *res;
pr_debug("%s() %.4x:%.4x\n", __func__, dev->vendor, dev->device); pr_debug("%s() %.4x:%.4x\n", __func__, dev->vendor, dev->device);
/* We don't rely on boot loader for PCI and resets all /* We don't rely on boot loader for PCI and resets all
devices */ devices */
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, res) {
struct resource *res = &dev->resource[i];
if (res->end > res->start) { /* Only valid resources */ if (res->end > res->start) { /* Only valid resources */
res->end -= res->start; res->end -= res->start;
res->start = 0; res->start = 0;
......
...@@ -240,7 +240,7 @@ void __init pSeries_final_fixup(void) ...@@ -240,7 +240,7 @@ void __init pSeries_final_fixup(void)
*/ */
static void fixup_winbond_82c105(struct pci_dev* dev) static void fixup_winbond_82c105(struct pci_dev* dev)
{ {
int i; struct resource *r;
unsigned int reg; unsigned int reg;
if (!machine_is(pseries)) if (!machine_is(pseries))
...@@ -251,14 +251,14 @@ static void fixup_winbond_82c105(struct pci_dev* dev) ...@@ -251,14 +251,14 @@ static void fixup_winbond_82c105(struct pci_dev* dev)
/* Enable LEGIRQ to use INTC instead of ISA interrupts */ /* Enable LEGIRQ to use INTC instead of ISA interrupts */
pci_write_config_dword(dev, 0x40, reg | (1<<11)); pci_write_config_dword(dev, 0x40, reg | (1<<11));
for (i = 0; i < DEVICE_COUNT_RESOURCE; ++i) { pci_dev_for_each_resource(dev, r) {
/* zap the 2nd function of the winbond chip */ /* zap the 2nd function of the winbond chip */
if (dev->resource[i].flags & IORESOURCE_IO if (dev->bus->number == 0 && dev->devfn == 0x81 &&
&& dev->bus->number == 0 && dev->devfn == 0x81) r->flags & IORESOURCE_IO)
dev->resource[i].flags &= ~IORESOURCE_IO; r->flags &= ~IORESOURCE_IO;
if (dev->resource[i].start == 0 && dev->resource[i].end) { if (r->start == 0 && r->end) {
dev->resource[i].flags = 0; r->flags = 0;
dev->resource[i].end = 0; r->end = 0;
} }
} }
} }
......
...@@ -140,12 +140,12 @@ static void sh7786_pci_fixup(struct pci_dev *dev) ...@@ -140,12 +140,12 @@ static void sh7786_pci_fixup(struct pci_dev *dev)
* Prevent enumeration of root complex resources. * Prevent enumeration of root complex resources.
*/ */
if (pci_is_root_bus(dev->bus) && dev->devfn == 0) { if (pci_is_root_bus(dev->bus) && dev->devfn == 0) {
int i; struct resource *r;
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(dev, r) {
dev->resource[i].start = 0; r->start = 0;
dev->resource[i].end = 0; r->end = 0;
dev->resource[i].flags = 0; r->flags = 0;
} }
} }
} }
......
...@@ -62,15 +62,14 @@ void leon_pci_init(struct platform_device *ofdev, struct leon_pci_info *info) ...@@ -62,15 +62,14 @@ void leon_pci_init(struct platform_device *ofdev, struct leon_pci_info *info)
int pcibios_enable_device(struct pci_dev *dev, int mask) int pcibios_enable_device(struct pci_dev *dev, int mask)
{ {
struct resource *res;
u16 cmd, oldcmd; u16 cmd, oldcmd;
int i; int i;
pci_read_config_word(dev, PCI_COMMAND, &cmd); pci_read_config_word(dev, PCI_COMMAND, &cmd);
oldcmd = cmd; oldcmd = cmd;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, res, i) {
struct resource *res = &dev->resource[i];
/* Only set up the requested stuff */ /* Only set up the requested stuff */
if (!(mask & (1<<i))) if (!(mask & (1<<i)))
continue; continue;
......
...@@ -663,11 +663,10 @@ static void pci_claim_bus_resources(struct pci_bus *bus) ...@@ -663,11 +663,10 @@ static void pci_claim_bus_resources(struct pci_bus *bus)
struct pci_dev *dev; struct pci_dev *dev;
list_for_each_entry(dev, &bus->devices, bus_list) { list_for_each_entry(dev, &bus->devices, bus_list) {
struct resource *r;
int i; int i;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
struct resource *r = &dev->resource[i];
if (r->parent || !r->start || !r->flags) if (r->parent || !r->start || !r->flags)
continue; continue;
...@@ -724,15 +723,14 @@ struct pci_bus *pci_scan_one_pbm(struct pci_pbm_info *pbm, ...@@ -724,15 +723,14 @@ struct pci_bus *pci_scan_one_pbm(struct pci_pbm_info *pbm,
int pcibios_enable_device(struct pci_dev *dev, int mask) int pcibios_enable_device(struct pci_dev *dev, int mask)
{ {
struct resource *res;
u16 cmd, oldcmd; u16 cmd, oldcmd;
int i; int i;
pci_read_config_word(dev, PCI_COMMAND, &cmd); pci_read_config_word(dev, PCI_COMMAND, &cmd);
oldcmd = cmd; oldcmd = cmd;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, res, i) {
struct resource *res = &dev->resource[i];
/* Only set up the requested stuff */ /* Only set up the requested stuff */
if (!(mask & (1<<i))) if (!(mask & (1<<i)))
continue; continue;
......
...@@ -643,15 +643,14 @@ void pcibios_fixup_bus(struct pci_bus *bus) ...@@ -643,15 +643,14 @@ void pcibios_fixup_bus(struct pci_bus *bus)
int pcibios_enable_device(struct pci_dev *dev, int mask) int pcibios_enable_device(struct pci_dev *dev, int mask)
{ {
struct resource *res;
u16 cmd, oldcmd; u16 cmd, oldcmd;
int i; int i;
pci_read_config_word(dev, PCI_COMMAND, &cmd); pci_read_config_word(dev, PCI_COMMAND, &cmd);
oldcmd = cmd; oldcmd = cmd;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, res, i) {
struct resource *res = &dev->resource[i];
/* Only set up the requested stuff */ /* Only set up the requested stuff */
if (!(mask & (1<<i))) if (!(mask & (1<<i)))
continue; continue;
......
...@@ -20,8 +20,8 @@ static struct eisa_root_device pci_eisa_root; ...@@ -20,8 +20,8 @@ static struct eisa_root_device pci_eisa_root;
static int __init pci_eisa_init(struct pci_dev *pdev) static int __init pci_eisa_init(struct pci_dev *pdev)
{ {
int rc, i;
struct resource *res, *bus_res = NULL; struct resource *res, *bus_res = NULL;
int rc;
if ((rc = pci_enable_device (pdev))) { if ((rc = pci_enable_device (pdev))) {
dev_err(&pdev->dev, "Could not enable device\n"); dev_err(&pdev->dev, "Could not enable device\n");
...@@ -38,7 +38,7 @@ static int __init pci_eisa_init(struct pci_dev *pdev) ...@@ -38,7 +38,7 @@ static int __init pci_eisa_init(struct pci_dev *pdev)
* eisa_root_register() can only deal with a single io port resource, * eisa_root_register() can only deal with a single io port resource,
* so we use the first valid io port resource. * so we use the first valid io port resource.
*/ */
pci_bus_for_each_resource(pdev->bus, res, i) pci_bus_for_each_resource(pdev->bus, res)
if (res && (res->flags & IORESOURCE_IO)) { if (res && (res->flags & IORESOURCE_IO)) {
bus_res = res; bus_res = res;
break; break;
......
...@@ -161,13 +161,13 @@ static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res, ...@@ -161,13 +161,13 @@ static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
void *alignf_data, void *alignf_data,
struct pci_bus_region *region) struct pci_bus_region *region)
{ {
int i, ret;
struct resource *r, avail; struct resource *r, avail;
resource_size_t max; resource_size_t max;
int ret;
type_mask |= IORESOURCE_TYPE_BITS; type_mask |= IORESOURCE_TYPE_BITS;
pci_bus_for_each_resource(bus, r, i) { pci_bus_for_each_resource(bus, r) {
resource_size_t min_used = min; resource_size_t min_used = min;
if (!r) if (!r)
...@@ -268,9 +268,8 @@ bool pci_bus_clip_resource(struct pci_dev *dev, int idx) ...@@ -268,9 +268,8 @@ bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
struct resource *res = &dev->resource[idx]; struct resource *res = &dev->resource[idx];
struct resource orig_res = *res; struct resource orig_res = *res;
struct resource *r; struct resource *r;
int i;
pci_bus_for_each_resource(bus, r, i) { pci_bus_for_each_resource(bus, r) {
resource_size_t start, end; resource_size_t start, end;
if (!r) if (!r)
......
...@@ -24,16 +24,16 @@ ...@@ -24,16 +24,16 @@
static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char *buf) static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
{ {
struct pci_dev *pdev; struct pci_dev *pdev;
int index, busnr;
struct resource *res; struct resource *res;
struct pci_bus *bus; struct pci_bus *bus;
size_t len = 0; size_t len = 0;
int busnr;
pdev = to_pci_dev(dev); pdev = to_pci_dev(dev);
bus = pdev->subordinate; bus = pdev->subordinate;
len += sysfs_emit_at(buf, len, "Free resources: memory\n"); len += sysfs_emit_at(buf, len, "Free resources: memory\n");
pci_bus_for_each_resource(bus, res, index) { pci_bus_for_each_resource(bus, res) {
if (res && (res->flags & IORESOURCE_MEM) && if (res && (res->flags & IORESOURCE_MEM) &&
!(res->flags & IORESOURCE_PREFETCH)) { !(res->flags & IORESOURCE_PREFETCH)) {
len += sysfs_emit_at(buf, len, len += sysfs_emit_at(buf, len,
...@@ -43,7 +43,7 @@ static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char ...@@ -43,7 +43,7 @@ static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char
} }
} }
len += sysfs_emit_at(buf, len, "Free resources: prefetchable memory\n"); len += sysfs_emit_at(buf, len, "Free resources: prefetchable memory\n");
pci_bus_for_each_resource(bus, res, index) { pci_bus_for_each_resource(bus, res) {
if (res && (res->flags & IORESOURCE_MEM) && if (res && (res->flags & IORESOURCE_MEM) &&
(res->flags & IORESOURCE_PREFETCH)) { (res->flags & IORESOURCE_PREFETCH)) {
len += sysfs_emit_at(buf, len, len += sysfs_emit_at(buf, len,
...@@ -53,7 +53,7 @@ static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char ...@@ -53,7 +53,7 @@ static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char
} }
} }
len += sysfs_emit_at(buf, len, "Free resources: IO\n"); len += sysfs_emit_at(buf, len, "Free resources: IO\n");
pci_bus_for_each_resource(bus, res, index) { pci_bus_for_each_resource(bus, res) {
if (res && (res->flags & IORESOURCE_IO)) { if (res && (res->flags & IORESOURCE_IO)) {
len += sysfs_emit_at(buf, len, len += sysfs_emit_at(buf, len,
"start = %8.8llx, length = %8.8llx\n", "start = %8.8llx, length = %8.8llx\n",
......
...@@ -787,9 +787,8 @@ struct resource *pci_find_parent_resource(const struct pci_dev *dev, ...@@ -787,9 +787,8 @@ struct resource *pci_find_parent_resource(const struct pci_dev *dev,
{ {
const struct pci_bus *bus = dev->bus; const struct pci_bus *bus = dev->bus;
struct resource *r; struct resource *r;
int i;
pci_bus_for_each_resource(bus, r, i) { pci_bus_for_each_resource(bus, r) {
if (!r) if (!r)
continue; continue;
if (resource_contains(r, res)) { if (resource_contains(r, res)) {
......
...@@ -533,7 +533,7 @@ void pci_read_bridge_bases(struct pci_bus *child) ...@@ -533,7 +533,7 @@ void pci_read_bridge_bases(struct pci_bus *child)
pci_read_bridge_mmio_pref(child); pci_read_bridge_mmio_pref(child);
if (dev->transparent) { if (dev->transparent) {
pci_bus_for_each_resource(child->parent, res, i) { pci_bus_for_each_resource(child->parent, res) {
if (res && res->flags) { if (res && res->flags) {
pci_bus_add_resource(child, res, pci_bus_add_resource(child, res,
PCI_SUBTRACTIVE_DECODE); PCI_SUBTRACTIVE_DECODE);
......
...@@ -5,10 +5,9 @@ ...@@ -5,10 +5,9 @@
static void pci_free_resources(struct pci_dev *dev) static void pci_free_resources(struct pci_dev *dev)
{ {
int i; struct resource *res;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, res) {
struct resource *res = dev->resource + i;
if (res->parent) if (res->parent)
release_resource(res); release_resource(res);
} }
......
...@@ -124,20 +124,17 @@ static resource_size_t get_res_add_align(struct list_head *head, ...@@ -124,20 +124,17 @@ static resource_size_t get_res_add_align(struct list_head *head,
return dev_res ? dev_res->min_align : 0; return dev_res ? dev_res->min_align : 0;
} }
/* Sort resources by alignment */ /* Sort resources by alignment */
static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
{ {
struct resource *r;
int i; int i;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
struct resource *r;
struct pci_dev_resource *dev_res, *tmp; struct pci_dev_resource *dev_res, *tmp;
resource_size_t r_align; resource_size_t r_align;
struct list_head *n; struct list_head *n;
r = &dev->resource[i];
if (r->flags & IORESOURCE_PCI_FIXED) if (r->flags & IORESOURCE_PCI_FIXED)
continue; continue;
...@@ -773,9 +770,8 @@ static struct resource *find_bus_resource_of_type(struct pci_bus *bus, ...@@ -773,9 +770,8 @@ static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
unsigned long type) unsigned long type)
{ {
struct resource *r, *r_assigned = NULL; struct resource *r, *r_assigned = NULL;
int i;
pci_bus_for_each_resource(bus, r, i) { pci_bus_for_each_resource(bus, r) {
if (r == &ioport_resource || r == &iomem_resource) if (r == &ioport_resource || r == &iomem_resource)
continue; continue;
if (r && (r->flags & type_mask) == type && !r->parent) if (r && (r->flags & type_mask) == type && !r->parent)
...@@ -895,10 +891,9 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, ...@@ -895,10 +891,9 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
min_align = window_alignment(bus, IORESOURCE_IO); min_align = window_alignment(bus, IORESOURCE_IO);
list_for_each_entry(dev, &bus->devices, bus_list) { list_for_each_entry(dev, &bus->devices, bus_list) {
int i; struct resource *r;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r) {
struct resource *r = &dev->resource[i];
unsigned long r_size; unsigned long r_size;
if (r->parent || !(r->flags & IORESOURCE_IO)) if (r->parent || !(r->flags & IORESOURCE_IO))
...@@ -1014,10 +1009,10 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, ...@@ -1014,10 +1009,10 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
size = 0; size = 0;
list_for_each_entry(dev, &bus->devices, bus_list) { list_for_each_entry(dev, &bus->devices, bus_list) {
struct resource *r;
int i; int i;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
struct resource *r = &dev->resource[i];
resource_size_t r_size; resource_size_t r_size;
if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) || if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
...@@ -1208,7 +1203,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) ...@@ -1208,7 +1203,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
additional_mmio_pref_size = 0; additional_mmio_pref_size = 0;
struct resource *pref; struct resource *pref;
struct pci_host_bridge *host; struct pci_host_bridge *host;
int hdr_type, i, ret; int hdr_type, ret;
list_for_each_entry(dev, &bus->devices, bus_list) { list_for_each_entry(dev, &bus->devices, bus_list) {
struct pci_bus *b = dev->subordinate; struct pci_bus *b = dev->subordinate;
...@@ -1232,7 +1227,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) ...@@ -1232,7 +1227,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
host = to_pci_host_bridge(bus->bridge); host = to_pci_host_bridge(bus->bridge);
if (!host->size_windows) if (!host->size_windows)
return; return;
pci_bus_for_each_resource(bus, pref, i) pci_bus_for_each_resource(bus, pref)
if (pref && (pref->flags & IORESOURCE_PREFETCH)) if (pref && (pref->flags & IORESOURCE_PREFETCH))
break; break;
hdr_type = -1; /* Intentionally invalid - not a PCI device. */ hdr_type = -1; /* Intentionally invalid - not a PCI device. */
...@@ -1337,12 +1332,11 @@ EXPORT_SYMBOL(pci_bus_size_bridges); ...@@ -1337,12 +1332,11 @@ EXPORT_SYMBOL(pci_bus_size_bridges);
static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
{ {
int i;
struct resource *parent_r; struct resource *parent_r;
unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM | unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
IORESOURCE_PREFETCH; IORESOURCE_PREFETCH;
pci_bus_for_each_resource(b, parent_r, i) { pci_bus_for_each_resource(b, parent_r) {
if (!parent_r) if (!parent_r)
continue; continue;
...@@ -1358,11 +1352,10 @@ static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) ...@@ -1358,11 +1352,10 @@ static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
*/ */
static void pdev_assign_fixed_resources(struct pci_dev *dev) static void pdev_assign_fixed_resources(struct pci_dev *dev)
{ {
int i; struct resource *r;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r) {
struct pci_bus *b; struct pci_bus *b;
struct resource *r = &dev->resource[i];
if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) || if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
...@@ -1795,11 +1788,9 @@ static void remove_dev_resources(struct pci_dev *dev, struct resource *io, ...@@ -1795,11 +1788,9 @@ static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
struct resource *mmio, struct resource *mmio,
struct resource *mmio_pref) struct resource *mmio_pref)
{ {
int i; struct resource *res;
for (i = 0; i < PCI_NUM_RESOURCES; i++) {
struct resource *res = &dev->resource[i];
pci_dev_for_each_resource(dev, res) {
if (resource_type(res) == IORESOURCE_IO) { if (resource_type(res) == IORESOURCE_IO) {
remove_dev_resource(io, dev, res); remove_dev_resource(io, dev, res);
} else if (resource_type(res) == IORESOURCE_MEM) { } else if (resource_type(res) == IORESOURCE_MEM) {
......
...@@ -484,12 +484,10 @@ int pci_enable_resources(struct pci_dev *dev, int mask) ...@@ -484,12 +484,10 @@ int pci_enable_resources(struct pci_dev *dev, int mask)
pci_read_config_word(dev, PCI_COMMAND, &cmd); pci_read_config_word(dev, PCI_COMMAND, &cmd);
old_cmd = cmd; old_cmd = cmd;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
if (!(mask & (1 << i))) if (!(mask & (1 << i)))
continue; continue;
r = &dev->resource[i];
if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
continue; continue;
if ((i == PCI_ROM_RESOURCE) && if ((i == PCI_ROM_RESOURCE) &&
......
...@@ -548,10 +548,8 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) ...@@ -548,10 +548,8 @@ static bool vga_is_firmware_default(struct pci_dev *pdev)
#if defined(CONFIG_X86) || defined(CONFIG_IA64) #if defined(CONFIG_X86) || defined(CONFIG_IA64)
u64 base = screen_info.lfb_base; u64 base = screen_info.lfb_base;
u64 size = screen_info.lfb_size; u64 size = screen_info.lfb_size;
struct resource *r;
u64 limit; u64 limit;
resource_size_t start, end;
unsigned long flags;
int i;
/* Select the device owning the boot framebuffer if there is one */ /* Select the device owning the boot framebuffer if there is one */
...@@ -561,19 +559,14 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) ...@@ -561,19 +559,14 @@ static bool vga_is_firmware_default(struct pci_dev *pdev)
limit = base + size; limit = base + size;
/* Does firmware framebuffer belong to us? */ /* Does firmware framebuffer belong to us? */
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(pdev, r) {
flags = pci_resource_flags(pdev, i); if (resource_type(r) != IORESOURCE_MEM)
if ((flags & IORESOURCE_MEM) == 0)
continue; continue;
start = pci_resource_start(pdev, i); if (!r->start || !r->end)
end = pci_resource_end(pdev, i);
if (!start || !end)
continue; continue;
if (base < start || limit >= end) if (base < r->start || limit >= r->end)
continue; continue;
return true; return true;
......
...@@ -390,9 +390,7 @@ static int pcifront_claim_resource(struct pci_dev *dev, void *data) ...@@ -390,9 +390,7 @@ static int pcifront_claim_resource(struct pci_dev *dev, void *data)
int i; int i;
struct resource *r; struct resource *r;
for (i = 0; i < PCI_NUM_RESOURCES; i++) { pci_dev_for_each_resource(dev, r, i) {
r = &dev->resource[i];
if (!r->parent && r->start && r->flags) { if (!r->parent && r->start && r->flags) {
dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n", dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
pci_name(dev), i); pci_name(dev), i);
......
...@@ -229,8 +229,7 @@ static void quirk_ad1815_mpu_resources(struct pnp_dev *dev) ...@@ -229,8 +229,7 @@ static void quirk_ad1815_mpu_resources(struct pnp_dev *dev)
static void quirk_system_pci_resources(struct pnp_dev *dev) static void quirk_system_pci_resources(struct pnp_dev *dev)
{ {
struct pci_dev *pdev = NULL; struct pci_dev *pdev = NULL;
struct resource *res; struct resource *res, *r;
resource_size_t pnp_start, pnp_end, pci_start, pci_end;
int i, j; int i, j;
/* /*
...@@ -243,32 +242,26 @@ static void quirk_system_pci_resources(struct pnp_dev *dev) ...@@ -243,32 +242,26 @@ static void quirk_system_pci_resources(struct pnp_dev *dev)
* so they won't be claimed by the PNP system driver. * so they won't be claimed by the PNP system driver.
*/ */
for_each_pci_dev(pdev) { for_each_pci_dev(pdev) {
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { pci_dev_for_each_resource(pdev, r, i) {
unsigned long flags, type; unsigned long type = resource_type(r);
flags = pci_resource_flags(pdev, i); if (!(type == IORESOURCE_IO || type == IORESOURCE_MEM) ||
type = flags & (IORESOURCE_IO | IORESOURCE_MEM); resource_size(r) == 0)
if (!type || pci_resource_len(pdev, i) == 0)
continue; continue;
if (flags & IORESOURCE_UNSET) if (r->flags & IORESOURCE_UNSET)
continue; continue;
pci_start = pci_resource_start(pdev, i);
pci_end = pci_resource_end(pdev, i);
for (j = 0; for (j = 0;
(res = pnp_get_resource(dev, type, j)); j++) { (res = pnp_get_resource(dev, type, j)); j++) {
if (res->start == 0 && res->end == 0) if (res->start == 0 && res->end == 0)
continue; continue;
pnp_start = res->start;
pnp_end = res->end;
/* /*
* If the PNP region doesn't overlap the PCI * If the PNP region doesn't overlap the PCI
* region at all, there's no problem. * region at all, there's no problem.
*/ */
if (pnp_end < pci_start || pnp_start > pci_end) if (!resource_overlaps(res, r))
continue; continue;
/* /*
...@@ -278,8 +271,7 @@ static void quirk_system_pci_resources(struct pnp_dev *dev) ...@@ -278,8 +271,7 @@ static void quirk_system_pci_resources(struct pnp_dev *dev)
* PNP device describes a bridge with PCI * PNP device describes a bridge with PCI
* behind it. * behind it.
*/ */
if (pnp_start <= pci_start && if (res->start <= r->start && res->end >= r->end)
pnp_end >= pci_end)
continue; continue;
/* /*
...@@ -288,9 +280,8 @@ static void quirk_system_pci_resources(struct pnp_dev *dev) ...@@ -288,9 +280,8 @@ static void quirk_system_pci_resources(struct pnp_dev *dev)
* driver from requesting its resources. * driver from requesting its resources.
*/ */
dev_warn(&dev->dev, dev_warn(&dev->dev,
"disabling %pR because it overlaps " "disabling %pR because it overlaps %s BAR %d %pR\n",
"%s BAR %d %pR\n", res, res, pci_name(pdev), i, r);
pci_name(pdev), i, &pdev->resource[i]);
res->flags |= IORESOURCE_DISABLED; res->flags |= IORESOURCE_DISABLED;
} }
} }
......
...@@ -1444,10 +1444,44 @@ int devm_request_pci_bus_resources(struct device *dev, ...@@ -1444,10 +1444,44 @@ int devm_request_pci_bus_resources(struct device *dev,
/* Temporary until new and working PCI SBR API in place */ /* Temporary until new and working PCI SBR API in place */
int pci_bridge_secondary_bus_reset(struct pci_dev *dev); int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
#define pci_bus_for_each_resource(bus, res, i) \ #define __pci_bus_for_each_res0(bus, res, ...) \
for (i = 0; \ for (unsigned int __b = 0; \
(res = pci_bus_resource_n(bus, i)) || i < PCI_BRIDGE_RESOURCE_NUM; \ (res = pci_bus_resource_n(bus, __b)) || __b < PCI_BRIDGE_RESOURCE_NUM; \
i++) __b++)
#define __pci_bus_for_each_res1(bus, res, __b) \
for (__b = 0; \
(res = pci_bus_resource_n(bus, __b)) || __b < PCI_BRIDGE_RESOURCE_NUM; \
__b++)
/**
* pci_bus_for_each_resource - iterate over PCI bus resources
* @bus: the PCI bus
* @res: pointer to the current resource
* @...: optional index of the current resource
*
* Iterate over PCI bus resources. The first part is to go over PCI bus
* resource array, which has at most the %PCI_BRIDGE_RESOURCE_NUM entries.
* After that continue with the separate list of the additional resources,
* if not empty. That's why the Logical OR is being used.
*
* Possible usage:
*
* struct pci_bus *bus = ...;
* struct resource *res;
* unsigned int i;
*
* // With optional index
* pci_bus_for_each_resource(bus, res, i)
* pr_info("PCI bus resource[%u]: %pR\n", i, res);
*
* // Without index
* pci_bus_for_each_resource(bus, res)
* _do_something_(res);
*/
#define pci_bus_for_each_resource(bus, res, ...) \
CONCATENATE(__pci_bus_for_each_res, COUNT_ARGS(__VA_ARGS__)) \
(bus, res, __VA_ARGS__)
int __must_check pci_bus_alloc_resource(struct pci_bus *bus, int __must_check pci_bus_alloc_resource(struct pci_bus *bus,
struct resource *res, resource_size_t size, struct resource *res, resource_size_t size,
...@@ -1994,14 +2028,27 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma); ...@@ -1994,14 +2028,27 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma);
* These helpers provide future and backwards compatibility * These helpers provide future and backwards compatibility
* for accessing popular PCI BAR info * for accessing popular PCI BAR info
*/ */
#define pci_resource_start(dev, bar) ((dev)->resource[(bar)].start) #define pci_resource_n(dev, bar) (&(dev)->resource[(bar)])
#define pci_resource_end(dev, bar) ((dev)->resource[(bar)].end) #define pci_resource_start(dev, bar) (pci_resource_n(dev, bar)->start)
#define pci_resource_flags(dev, bar) ((dev)->resource[(bar)].flags) #define pci_resource_end(dev, bar) (pci_resource_n(dev, bar)->end)
#define pci_resource_len(dev,bar) \ #define pci_resource_flags(dev, bar) (pci_resource_n(dev, bar)->flags)
((pci_resource_end((dev), (bar)) == 0) ? 0 : \ #define pci_resource_len(dev,bar) \
\ (pci_resource_end((dev), (bar)) ? \
(pci_resource_end((dev), (bar)) - \ resource_size(pci_resource_n((dev), (bar))) : 0)
pci_resource_start((dev), (bar)) + 1))
#define __pci_dev_for_each_res0(dev, res, ...) \
for (unsigned int __b = 0; \
res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \
__b++)
#define __pci_dev_for_each_res1(dev, res, __b) \
for (__b = 0; \
res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \
__b++)
#define pci_dev_for_each_resource(dev, res, ...) \
CONCATENATE(__pci_dev_for_each_res, COUNT_ARGS(__VA_ARGS__)) \
(dev, res, __VA_ARGS__)
/* /*
* Similar to the helpers above, these manipulate per-pci_dev * Similar to the helpers above, these manipulate per-pci_dev
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment