From: ddugger@redhat.com <ddugger@redhat.com> Date: Thu, 2 Apr 2009 08:46:13 -0600 Subject: [pci] add a new function to map BAR offsets Message-id: 200904021446.n32EkDK3017140@sobek.n0ano.com O-Subject: [RHEL5.4 PATCH 9/17] BZ493152: Backport: PCI: add a new function to map BAR offsets Bugzilla: 493152 RH-Acked-by: Rik van Riel <riel@redhat.com> RH-Acked-by: Chris Wright <chrisw@redhat.com> Upstream status: commit 613e7ed6f72b1a115f7ece8ce1b66cf095de1348 Author: Yu Zhao <yu.zhao@intel.com> Date: Sat Nov 22 02:41:27 2008 +0800 PCI: add a new function to map BAR offsets Add a function to map a given resource number to a corresponding register so drivers can get the offset and type of device specific BARs. Signed-off-by: Yu Zhao <yu.zhao@intel.com> Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org> Signed-off-by: Gerd Hoffman <kraxel@redhat.com> Signed-off-by: Don Dugger <donald.d.dugger@intel.com> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index a2bf736..fe089ff 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -1505,6 +1505,28 @@ int pci_select_bars(struct pci_dev *dev, unsigned long flags) return bars; } +/** + * pci_resource_bar - get position of the BAR associated with a resource + * @dev: the PCI device + * @resno: the resource number + * @type: the BAR type to be filled in + * + * Returns BAR position in config space, or 0 if the BAR is invalid. + */ +int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type) +{ + if (resno < PCI_ROM_RESOURCE) { + *type = pci_bar_unknown; + return PCI_BASE_ADDRESS_0 + 4 * resno; + } else if (resno == PCI_ROM_RESOURCE) { + *type = pci_bar_mem32; + return dev->rom_base_reg; + } + + dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno); + return 0; +} + static int __devinit pci_init(void) { struct pci_dev *dev = NULL; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 2f41a9d..8f51881 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -101,6 +101,8 @@ enum pci_bar_type { extern int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, struct resource *res, unsigned int reg); +extern int pci_resource_bar(struct pci_dev *dev, int resno, + enum pci_bar_type *type); extern void pci_enable_ari(struct pci_dev *dev); /** * pci_ari_enabled - query ARI forwarding status diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c index c8b4aef..aaac923 100644 --- a/drivers/pci/setup-res.c +++ b/drivers/pci/setup-res.c @@ -32,6 +32,7 @@ pci_update_resource(struct pci_dev *dev, struct resource *res, int resno) struct pci_bus_region region; u32 new, check, mask; int reg; + enum pci_bar_type type; /* Ignore resources for unimplemented BARs and unused resource slots for 64 bit BARs. */ @@ -51,17 +52,13 @@ pci_update_resource(struct pci_dev *dev, struct resource *res, int resno) else mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; - if (resno < 6) { - reg = PCI_BASE_ADDRESS_0 + 4 * resno; - } else if (resno == PCI_ROM_RESOURCE) { + reg = pci_resource_bar(dev, resno, &type); + if (!reg) + return; + if (type != pci_bar_unknown) { if (!(res->flags & IORESOURCE_ROM_ENABLE)) return; new |= PCI_ROM_ADDRESS_ENABLE; - reg = dev->rom_base_reg; - } else { - /* Hmm, non-standard resource. */ - - return; /* kill uninitialised var warning */ } pci_write_config_dword(dev, reg, new);