Commit 6ac665c6 authored by Matthew Wilcox's avatar Matthew Wilcox Committed by Jesse Barnes

PCI: rewrite PCI BAR reading code

Factor out the code to read one BAR from the loop in pci_read_bases into
a new function, __pci_read_base.  The new code is slightly more
readable, better commented and removes the ifdef.
Signed-off-by: default avatarMatthew Wilcox <willy@linux.intel.com>
Signed-off-by: default avatarJesse Barnes <jbarnes@virtuousgeek.org>
parent 37139074
...@@ -163,12 +163,9 @@ static inline unsigned int pci_calc_resource_flags(unsigned int flags) ...@@ -163,12 +163,9 @@ static inline unsigned int pci_calc_resource_flags(unsigned int flags)
return IORESOURCE_MEM; return IORESOURCE_MEM;
} }
/* static u64 pci_size(u64 base, u64 maxbase, u64 mask)
* Find the extent of a PCI decode..
*/
static u32 pci_size(u32 base, u32 maxbase, u32 mask)
{ {
u32 size = mask & maxbase; /* Find the significant bits */ u64 size = mask & maxbase; /* Find the significant bits */
if (!size) if (!size)
return 0; return 0;
...@@ -184,135 +181,142 @@ static u32 pci_size(u32 base, u32 maxbase, u32 mask) ...@@ -184,135 +181,142 @@ static u32 pci_size(u32 base, u32 maxbase, u32 mask)
return size; return size;
} }
static u64 pci_size64(u64 base, u64 maxbase, u64 mask) enum pci_bar_type {
{ pci_bar_unknown, /* Standard PCI BAR probe */
u64 size = mask & maxbase; /* Find the significant bits */ pci_bar_io, /* An io port BAR */
if (!size) pci_bar_mem32, /* A 32-bit memory BAR */
return 0; pci_bar_mem64, /* A 64-bit memory BAR */
};
/* Get the lowest of them to find the decode size, and static inline enum pci_bar_type decode_bar(struct resource *res, u32 bar)
from that the extent. */ {
size = (size & ~(size-1)) - 1; if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
return pci_bar_io;
}
/* base == maxbase can be valid only if the BAR has res->flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
already been programmed with all 1s. */
if (base == maxbase && ((base | size) & mask) != mask)
return 0;
return size; if (res->flags == PCI_BASE_ADDRESS_MEM_TYPE_64)
return pci_bar_mem64;
return pci_bar_mem32;
} }
static inline int is_64bit_memory(u32 mask) /*
* If the type is not unknown, we assume that the lowest bit is 'enable'.
* Returns 1 if the BAR was 64-bit and 0 if it was 32-bit.
*/
static int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
struct resource *res, unsigned int pos)
{ {
if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == u32 l, sz, mask;
(PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))
return 1;
return 0;
}
static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) mask = type ? ~PCI_ROM_ADDRESS_ENABLE : ~0;
{
unsigned int pos, reg, next;
u32 l, sz;
struct resource *res;
for(pos=0; pos<howmany; pos = next) { res->name = pci_name(dev);
u64 l64;
u64 sz64;
u32 raw_sz;
next = pos+1; pci_read_config_dword(dev, pos, &l);
res = &dev->resource[pos]; pci_write_config_dword(dev, pos, mask);
res->name = pci_name(dev); pci_read_config_dword(dev, pos, &sz);
reg = PCI_BASE_ADDRESS_0 + (pos << 2); pci_write_config_dword(dev, pos, l);
pci_read_config_dword(dev, reg, &l);
pci_write_config_dword(dev, reg, ~0); /*
pci_read_config_dword(dev, reg, &sz); * All bits set in sz means the device isn't working properly.
pci_write_config_dword(dev, reg, l); * If the BAR isn't implemented, all bits must be 0. If it's a
if (!sz || sz == 0xffffffff) * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
continue; * 1 must be clear.
if (l == 0xffffffff) */
l = 0; if (!sz || sz == 0xffffffff)
raw_sz = sz; goto fail;
if ((l & PCI_BASE_ADDRESS_SPACE) ==
PCI_BASE_ADDRESS_SPACE_MEMORY) { /*
sz = pci_size(l, sz, (u32)PCI_BASE_ADDRESS_MEM_MASK); * I don't know how l can have all bits set. Copied from old code.
/* * Maybe it fixes a bug on some ancient platform.
* For 64bit prefetchable memory sz could be 0, if the */
* real size is bigger than 4G, so we need to check if (l == 0xffffffff)
* szhi for that. l = 0;
*/
if (!is_64bit_memory(l) && !sz) if (type == pci_bar_unknown) {
continue; type = decode_bar(res, l);
res->start = l & PCI_BASE_ADDRESS_MEM_MASK; res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK; if (type == pci_bar_io) {
l &= PCI_BASE_ADDRESS_IO_MASK;
mask = PCI_BASE_ADDRESS_IO_MASK & 0xffff;
} else { } else {
sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff); l &= PCI_BASE_ADDRESS_MEM_MASK;
if (!sz) mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
continue;
res->start = l & PCI_BASE_ADDRESS_IO_MASK;
res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
} }
res->end = res->start + (unsigned long) sz; } else {
res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN; res->flags |= (l & IORESOURCE_ROM_ENABLE);
if (is_64bit_memory(l)) { l &= PCI_ROM_ADDRESS_MASK;
u32 szhi, lhi; mask = (u32)PCI_ROM_ADDRESS_MASK;
}
pci_read_config_dword(dev, reg+4, &lhi);
pci_write_config_dword(dev, reg+4, ~0); if (type == pci_bar_mem64) {
pci_read_config_dword(dev, reg+4, &szhi); u64 l64 = l;
pci_write_config_dword(dev, reg+4, lhi); u64 sz64 = sz;
sz64 = ((u64)szhi << 32) | raw_sz; u64 mask64 = mask | (u64)~0 << 32;
l64 = ((u64)lhi << 32) | l;
sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK); pci_read_config_dword(dev, pos + 4, &l);
next++; pci_write_config_dword(dev, pos + 4, ~0);
#if BITS_PER_LONG == 64 pci_read_config_dword(dev, pos + 4, &sz);
if (!sz64) { pci_write_config_dword(dev, pos + 4, l);
res->start = 0;
res->end = 0; l64 |= ((u64)l << 32);
res->flags = 0; sz64 |= ((u64)sz << 32);
continue;
} sz64 = pci_size(l64, sz64, mask64);
res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;
res->end = res->start + sz64; if (!sz64)
#else goto fail;
if (sz64 > 0x100000000ULL) {
dev_err(&dev->dev, "BAR %d: can't handle 64-bit" if ((BITS_PER_LONG < 64) && (sz64 > 0x100000000ULL)) {
" BAR\n", pos); dev_err(&dev->dev, "can't handle 64-bit BAR\n");
res->start = 0; goto fail;
res->flags = 0; } else if ((BITS_PER_LONG < 64) && l) {
} else if (lhi) { /* Address above 32-bit boundary; disable the BAR */
/* 64-bit wide address, treat as disabled */ pci_write_config_dword(dev, pos, 0);
pci_write_config_dword(dev, reg, pci_write_config_dword(dev, pos + 4, 0);
l & ~(u32)PCI_BASE_ADDRESS_MEM_MASK); res->start = 0;
pci_write_config_dword(dev, reg+4, 0); res->end = sz64;
res->start = 0; } else {
res->end = sz; res->start = l64;
} res->end = l64 + sz64;
#endif
} }
} else {
sz = pci_size(l, sz, mask);
if (!sz)
goto fail;
res->start = l;
res->end = l + sz;
} }
out:
return (type == pci_bar_mem64) ? 1 : 0;
fail:
res->flags = 0;
goto out;
}
static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
{
unsigned int pos, reg;
for (pos = 0; pos < howmany; pos++) {
struct resource *res = &dev->resource[pos];
reg = PCI_BASE_ADDRESS_0 + (pos << 2);
pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
}
if (rom) { if (rom) {
struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
dev->rom_base_reg = rom; dev->rom_base_reg = rom;
res = &dev->resource[PCI_ROM_RESOURCE]; res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
res->name = pci_name(dev); IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
pci_read_config_dword(dev, rom, &l); IORESOURCE_SIZEALIGN;
pci_write_config_dword(dev, rom, ~PCI_ROM_ADDRESS_ENABLE); __pci_read_base(dev, pci_bar_mem32, res, rom);
pci_read_config_dword(dev, rom, &sz);
pci_write_config_dword(dev, rom, l);
if (l == 0xffffffff)
l = 0;
if (sz && sz != 0xffffffff) {
sz = pci_size(l, sz, (u32)PCI_ROM_ADDRESS_MASK);
if (sz) {
res->flags = (l & IORESOURCE_ROM_ENABLE) |
IORESOURCE_MEM | IORESOURCE_PREFETCH |
IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
IORESOURCE_SIZEALIGN;
res->start = l & PCI_ROM_ADDRESS_MASK;
res->end = res->start + (unsigned long) sz;
}
}
} }
} }
......
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