Commit f75b99d5 authored by Yinghai Lu's avatar Yinghai Lu Committed by Bjorn Helgaas

PCI: Enforce bus address limits in resource allocation

When allocating space for 32-bit BARs, we previously limited RESOURCE
addresses so they would fit in 32 bits.  However, the BUS address need not
be the same as the resource address, and it's the bus address that must fit
in the 32-bit BAR.

This patch adds:

  - pci_clip_resource_to_region(), which clips a resource so it contains
    only the range that maps to the specified bus address region, e.g., to
    clip a resource to 32-bit bus addresses, and

  - pci_bus_alloc_from_region(), which allocates space for a resource from
    the specified bus address region,

and changes pci_bus_alloc_resource() to allocate space for 64-bit BARs from
the entire bus address region, and space for 32-bit BARs from only the bus
address region below 4GB.

If we had this window:

  pci_root HWP0002:0a: host bridge window [mem 0xf0180000000-0xf01fedfffff] (bus address [0x80000000-0xfedfffff])

we previously could not put a 32-bit BAR there, because the CPU addresses
don't fit in 32 bits.  This patch fixes this, so we can use this space for
32-bit BARs.

It's also possible (though unlikely) to have resources with 32-bit CPU
addresses but bus addresses above 4GB.  In this case the previous code
would allocate space that a 32-bit BAR could not map.

Remove PCIBIOS_MAX_MEM_32, which is no longer used.

[bhelgaas: reworked starting from http://lkml.kernel.org/r/1386658484-15774-3-git-send-email-yinghai@kernel.org]
Signed-off-by: default avatarYinghai Lu <yinghai@kernel.org>
Signed-off-by: default avatarBjorn Helgaas <bhelgaas@google.com>
parent 36e097a8
...@@ -125,7 +125,6 @@ int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, ...@@ -125,7 +125,6 @@ int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc,
/* generic pci stuff */ /* generic pci stuff */
#include <asm-generic/pci.h> #include <asm-generic/pci.h>
#define PCIBIOS_MAX_MEM_32 0xffffffff
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
/* Returns the node based on pci bus */ /* Returns the node based on pci bus */
......
...@@ -98,41 +98,52 @@ void pci_bus_remove_resources(struct pci_bus *bus) ...@@ -98,41 +98,52 @@ void pci_bus_remove_resources(struct pci_bus *bus)
} }
} }
/** static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
* pci_bus_alloc_resource - allocate a resource from a parent bus #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
* @bus: PCI bus static struct pci_bus_region pci_64_bit = {0,
* @res: resource to allocate (dma_addr_t) 0xffffffffffffffffULL};
* @size: size of resource to allocate #endif
* @align: alignment of resource to allocate
* @min: minimum /proc/iomem address to allocate /*
* @type_mask: IORESOURCE_* type flags * @res contains CPU addresses. Clip it so the corresponding bus addresses
* @alignf: resource alignment function * on @bus are entirely within @region. This is used to control the bus
* @alignf_data: data argument for resource alignment function * addresses of resources we allocate, e.g., we may need a resource that
* * can be mapped by a 32-bit BAR.
* Given the PCI bus a device resides on, the size, minimum address,
* alignment and type, try to find an acceptable resource allocation
* for a specific device resource.
*/ */
int static void pci_clip_resource_to_region(struct pci_bus *bus,
pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, struct resource *res,
struct pci_bus_region *region)
{
struct pci_bus_region r;
pcibios_resource_to_bus(bus, &r, res);
if (r.start < region->start)
r.start = region->start;
if (r.end > region->end)
r.end = region->end;
if (r.end < r.start)
res->end = res->start - 1;
else
pcibios_bus_to_resource(bus, res, &r);
}
static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
resource_size_t size, resource_size_t align, resource_size_t size, resource_size_t align,
resource_size_t min, unsigned int type_mask, resource_size_t min, unsigned int type_mask,
resource_size_t (*alignf)(void *, resource_size_t (*alignf)(void *,
const struct resource *, const struct resource *,
resource_size_t, resource_size_t,
resource_size_t), resource_size_t),
void *alignf_data) void *alignf_data,
struct pci_bus_region *region)
{ {
int i, ret = -ENOMEM; int i, ret;
struct resource *r; struct resource *r, avail;
resource_size_t max = -1; resource_size_t max;
type_mask |= IORESOURCE_IO | IORESOURCE_MEM; type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
/* don't allocate too high if the pref mem doesn't support 64bit*/
if (!(res->flags & IORESOURCE_MEM_64))
max = PCIBIOS_MAX_MEM_32;
pci_bus_for_each_resource(bus, r, i) { pci_bus_for_each_resource(bus, r, i) {
if (!r) if (!r)
continue; continue;
...@@ -147,22 +158,66 @@ pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, ...@@ -147,22 +158,66 @@ pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
!(res->flags & IORESOURCE_PREFETCH)) !(res->flags & IORESOURCE_PREFETCH))
continue; continue;
avail = *r;
pci_clip_resource_to_region(bus, &avail, region);
if (!resource_size(&avail))
continue;
/* /*
* "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
* protect badly documented motherboard resources, but if * protect badly documented motherboard resources, but if
* this is an already-configured bridge window, its start * this is an already-configured bridge window, its start
* overrides "min". * overrides "min".
*/ */
if (r->start) if (avail.start)
min = r->start; min = avail.start;
max = avail.end;
/* Ok, try it out.. */ /* Ok, try it out.. */
ret = allocate_resource(r, res, size, min, max, ret = allocate_resource(r, res, size, min, max,
align, alignf, alignf_data); align, alignf, alignf_data);
if (ret == 0) if (ret == 0)
break; return 0;
} }
return ret; return -ENOMEM;
}
/**
* pci_bus_alloc_resource - allocate a resource from a parent bus
* @bus: PCI bus
* @res: resource to allocate
* @size: size of resource to allocate
* @align: alignment of resource to allocate
* @min: minimum /proc/iomem address to allocate
* @type_mask: IORESOURCE_* type flags
* @alignf: resource alignment function
* @alignf_data: data argument for resource alignment function
*
* Given the PCI bus a device resides on, the size, minimum address,
* alignment and type, try to find an acceptable resource allocation
* for a specific device resource.
*/
int
pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
resource_size_t size, resource_size_t align,
resource_size_t min, unsigned int type_mask,
resource_size_t (*alignf)(void *,
const struct resource *,
resource_size_t,
resource_size_t),
void *alignf_data)
{
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
if (res->flags & IORESOURCE_MEM_64)
return pci_bus_alloc_from_region(bus, res, size, align, min,
type_mask, alignf, alignf_data,
&pci_64_bit);
#endif
return pci_bus_alloc_from_region(bus, res, size, align, min,
type_mask, alignf, alignf_data,
&pci_32_bit);
} }
void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { } void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
......
...@@ -1493,10 +1493,6 @@ static inline struct pci_dev *pci_dev_get(struct pci_dev *dev) ...@@ -1493,10 +1493,6 @@ static inline struct pci_dev *pci_dev_get(struct pci_dev *dev)
#include <asm/pci.h> #include <asm/pci.h>
#ifndef PCIBIOS_MAX_MEM_32
#define PCIBIOS_MAX_MEM_32 (-1)
#endif
/* 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_start(dev, bar) ((dev)->resource[(bar)].start)
......
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