// SPDX-License-Identifier: GPL-2.0/* * PCI detection and setup code */ #include <linux/array_size.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/pci.h>#include <linux/msi.h>#include <linux/of_pci.h>#include <linux/of_platform.h>#include <linux/platform_device.h>#include <linux/pci_hotplug.h>#include <linux/slab.h>#include <linux/sprintf.h>#include <linux/module.h>#include <linux/cpumask.h>#include <linux/aer.h>#include <linux/acpi.h>#include <linux/hypervisor.h>#include <linux/irqdomain.h>#include <linux/pm_runtime.h>#include <linux/bitfield.h>#include <trace/events/pci.h>#include "pci.h" static struct resource busn_resource = { .name = "PCI busn", .start = 0, .end = 255, .flags = IORESOURCE_BUS,}; /* Ugh. Need to stop exporting this to modules. */LIST_HEAD(pci_root_buses);EXPORT_SYMBOL(pci_root_buses); static LIST_HEAD(pci_domain_busn_res_list); struct pci_domain_busn_res { struct list_head list; struct resource res; int domain_nr;}; static struct resource *get_pci_domain_busn_res(int domain_nr){ struct pci_domain_busn_res *r; list_for_each_entry(r, &pci_domain_busn_res_list, list) if (r->domain_nr == domain_nr) return &r->res; r = kzalloc_obj(*r); if (!r) return NULL; r->domain_nr = domain_nr; r->res.start = 0; r->res.end = 0xff; r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED; list_add_tail(&r->list, &pci_domain_busn_res_list); return &r->res;} /* * PCI Bus Class */static void release_pcibus_dev(struct device *dev){ struct pci_bus *pci_bus = to_pci_bus(dev); put_device(pci_bus->bridge); pci_bus_remove_resources(pci_bus); pci_release_bus_of_node(pci_bus); kfree(pci_bus);} static const struct class pcibus_class = { .name = "pci_bus", .dev_release = &release_pcibus_dev, .dev_groups = pcibus_groups,}; static int __init pcibus_class_init(void){ return class_register(&pcibus_class);}postcore_initcall(pcibus_class_init); static u64 pci_size(u64 base, u64 maxbase, u64 mask){ u64 size = mask & maxbase; /* Find the significant bits */ if (!size) return 0; /* * Get the lowest of them to find the decode size, and from that * the extent. */ size = size & ~(size-1); /* * base == maxbase can be valid only if the BAR has already been * programmed with all 1s. */ if (base == maxbase && ((base | (size - 1)) & mask) != mask) return 0; return size;} static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar){ u32 mem_type; unsigned long flags; if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) { flags = bar & ~PCI_BASE_ADDRESS_IO_MASK; flags |= IORESOURCE_IO; return flags; } flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK; flags |= IORESOURCE_MEM; if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH) flags |= IORESOURCE_PREFETCH; mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK; switch (mem_type) { case PCI_BASE_ADDRESS_MEM_TYPE_32: break; case PCI_BASE_ADDRESS_MEM_TYPE_1M: /* 1M mem BAR treated as 32-bit BAR */ break; case PCI_BASE_ADDRESS_MEM_TYPE_64: flags |= IORESOURCE_MEM_64; break; default: /* mem unknown type treated as 32-bit BAR */ break; } return flags;} #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO) /** * __pci_size_bars - Read the raw BAR mask for a range of PCI BARs * @dev: the PCI device * @count: number of BARs to size * @pos: starting config space position * @sizes: array to store mask values * @rom: indicate whether to use ROM mask, which avoids enabling ROM BARs * * Provided @sizes array must be sufficiently sized to store results for * @count u32 BARs. Caller is responsible for disabling decode to specified * BAR range around calling this function. This function is intended to avoid * disabling decode around sizing each BAR individually, which can result in * non-trivial overhead in virtualized environments with very large PCI BARs. */static void __pci_size_bars(struct pci_dev *dev, int count, unsigned int pos, u32 *sizes, bool rom){ u32 orig, mask = rom ? PCI_ROM_ADDRESS_MASK : ~0; int i; for (i = 0; i < count; i++, pos += 4, sizes++) { pci_read_config_dword(dev, pos, &orig); pci_write_config_dword(dev, pos, mask); pci_read_config_dword(dev, pos, sizes); pci_write_config_dword(dev, pos, orig); }} void __pci_size_stdbars(struct pci_dev *dev, int count, unsigned int pos, u32 *sizes){ __pci_size_bars(dev, count, pos, sizes, false);} static void __pci_size_rom(struct pci_dev *dev, unsigned int pos, u32 *sizes){ __pci_size_bars(dev, 1, pos, sizes, true);} /** * __pci_read_base - Read a PCI BAR * @dev: the PCI device * @type: type of the BAR * @res: resource buffer to be filled in * @pos: BAR position in the config space * @sizes: array of one or more pre-read BAR masks * * Returns 1 if the BAR is 64-bit, or 0 if 32-bit. */int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, struct resource *res, unsigned int pos, u32 *sizes){ u32 l = 0, sz; u64 l64, sz64, mask64; struct pci_bus_region region, inverted_region; const char *res_name = pci_resource_name(dev, res - dev->resource); res->name = pci_name(dev); pci_read_config_dword(dev, pos, &l); sz = sizes[0]; /* * All bits set in sz means the device isn't working properly. * If the BAR isn't implemented, all bits must be 0. If it's a * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit * 1 must be clear. */ if (PCI_POSSIBLE_ERROR(sz)) sz = 0; /* * I don't know how l can have all bits set. Copied from old code. * Maybe it fixes a bug on some ancient platform. */ if (PCI_POSSIBLE_ERROR(l)) l = 0; if (type == pci_bar_unknown) { res->flags = decode_bar(dev, l); res->flags |= IORESOURCE_SIZEALIGN; if (res->flags & IORESOURCE_IO) { l64 = l & PCI_BASE_ADDRESS_IO_MASK; sz64 = sz & PCI_BASE_ADDRESS_IO_MASK; mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT; } else { l64 = l & PCI_BASE_ADDRESS_MEM_MASK; sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK; mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK; } } else { if (l & PCI_ROM_ADDRESS_ENABLE) res->flags |= IORESOURCE_ROM_ENABLE; l64 = l & PCI_ROM_ADDRESS_MASK; sz64 = sz & PCI_ROM_ADDRESS_MASK; mask64 = PCI_ROM_ADDRESS_MASK; } if (res->flags & IORESOURCE_MEM_64) { pci_read_config_dword(dev, pos + 4, &l); sz = sizes[1]; l64 |= ((u64)l << 32); sz64 |= ((u64)sz << 32); mask64 |= ((u64)~0 << 32); } if (!sz64) goto fail; sz64 = pci_size(l64, sz64, mask64); if (!sz64) { pci_info(dev, FW_BUG "%s: invalid; can't size\n", res_name); goto fail; } if (res->flags & IORESOURCE_MEM_64) { if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8) && sz64 > 0x100000000ULL) { res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; resource_set_range(res, 0, 0); pci_err(dev, "%s: can't handle BAR larger than 4GB (size %#010llx)\n", res_name, (unsigned long long)sz64); goto out; } if ((sizeof(pci_bus_addr_t) < 8) && l) { /* Above 32-bit boundary; try to reallocate */ res->flags |= IORESOURCE_UNSET; resource_set_range(res, 0, sz64); pci_info(dev, "%s: can't handle BAR above 4GB (bus address %#010llx)\n", res_name, (unsigned long long)l64); goto out; } } region.start = l64; region.end = l64 + sz64 - 1; pcibios_bus_to_resource(dev->bus, res, ®ion); pcibios_resource_to_bus(dev->bus, &inverted_region, res); /* * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is * the corresponding resource address (the physical address used by * the CPU. Converting that resource address back to a bus address * should yield the original BAR value: * * resource_to_bus(bus_to_resource(A)) == A * * If it doesn't, CPU accesses to "bus_to_resource(A)" will not * be claimed by the device. */ if (inverted_region.start != region.start) { res->flags |= IORESOURCE_UNSET; res->start = 0; res->end = region.end - region.start; pci_info(dev, "%s: initial BAR value %#010llx invalid\n", res_name, (unsigned long long)region.start); } goto out; fail: res->flags = 0;out: if (res->flags) pci_info(dev, "%s %pR\n", res_name, res); return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;} static __always_inline void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom){ u32 rombar, stdbars[PCI_STD_NUM_BARS]; unsigned int pos, reg; u16 orig_cmd; BUILD_BUG_ON(statically_true(howmany > PCI_STD_NUM_BARS)); if (dev->non_compliant_bars) return; /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */ if (dev->is_virtfn) return; /* No printks while decoding is disabled! */ if (!dev->mmio_always_on) { pci_read_config_word(dev, PCI_COMMAND, &orig_cmd); if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) { pci_write_config_word(dev, PCI_COMMAND, orig_cmd & ~PCI_COMMAND_DECODE_ENABLE); } } __pci_size_stdbars(dev, howmany, PCI_BASE_ADDRESS_0, stdbars); if (rom) __pci_size_rom(dev, rom, &rombar); if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE)) pci_write_config_word(dev, PCI_COMMAND, orig_cmd); 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, &stdbars[pos]); } if (rom) { struct resource *res = &dev->resource[PCI_ROM_RESOURCE]; dev->rom_base_reg = rom; res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_READONLY | IORESOURCE_SIZEALIGN; __pci_read_base(dev, pci_bar_mem32, res, rom, &rombar); }} static void pci_read_bridge_io(struct pci_dev *dev, struct resource *res, bool log){ u8 io_base_lo, io_limit_lo; unsigned long io_mask, io_granularity, base, limit; struct pci_bus_region region; if (!dev->io_window) return; io_mask = PCI_IO_RANGE_MASK; io_granularity = 0x1000; if (dev->io_window_1k) { /* Support 1K I/O space granularity */ io_mask = PCI_IO_1K_RANGE_MASK; io_granularity = 0x400; } pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo); base = (io_base_lo & io_mask) << 8; limit = (io_limit_lo & io_mask) << 8; if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { u16 io_base_hi, io_limit_hi; pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi); pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi); base |= ((unsigned long) io_base_hi << 16); limit |= ((unsigned long) io_limit_hi << 16); } res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; if (base <= limit) { region.start = base; region.end = limit + io_granularity - 1; pcibios_bus_to_resource(dev->bus, res, ®ion); if (log) pci_info(dev, " bridge window %pR\n", res); } else { resource_set_range(res, 0, 0); res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; }} static void pci_read_bridge_mmio(struct pci_dev *dev, struct resource *res, bool log){ u16 mem_base_lo, mem_limit_lo; unsigned long base, limit; struct pci_bus_region region; pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo); pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo); base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16; limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM; if (base <= limit) { region.start = base; region.end = limit + 0xfffff; pcibios_bus_to_resource(dev->bus, res, ®ion); if (log) pci_info(dev, " bridge window %pR\n", res); } else { resource_set_range(res, 0, 0); res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; }} static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res, bool log){ u16 mem_base_lo, mem_limit_lo; u64 base64, limit64; pci_bus_addr_t base, limit; struct pci_bus_region region; if (!dev->pref_window) return; pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo); pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo); base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16; limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16; if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { u32 mem_base_hi, mem_limit_hi; pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi); pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi); /* * Some bridges set the base > limit by default, and some * (broken) BIOSes do not initialize them. If we find * this, just assume they are not being used. */ if (mem_base_hi <= mem_limit_hi) { base64 |= (u64) mem_base_hi << 32; limit64 |= (u64) mem_limit_hi << 32; } } base = (pci_bus_addr_t) base64; limit = (pci_bus_addr_t) limit64; if (base != base64) { pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n", (unsigned long long) base64); return; } res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH; if (res->flags & PCI_PREF_RANGE_TYPE_64) res->flags |= IORESOURCE_MEM_64; if (base <= limit) { region.start = base; region.end = limit + 0xfffff; pcibios_bus_to_resource(dev->bus, res, ®ion); if (log) pci_info(dev, " bridge window %pR\n", res); } else { resource_set_range(res, 0, 0);Lines 1–500 of 3582. Load more
// SPDX-License-Identifier: GPL-2.0 /* * PCI detection and setup code */ #include <linux/array_size.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/pci.h> #include <linux/msi.h> #include <linux/of_pci.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/pci_hotplug.h> #include <linux/slab.h> #include <linux/sprintf.h> #include <linux/module.h> #include <linux/cpumask.h> #include <linux/aer.h> #include <linux/acpi.h> #include <linux/hypervisor.h> #include <linux/irqdomain.h> #include <linux/pm_runtime.h> #include <linux/bitfield.h> #include <trace/events/pci.h> #include "pci.h" static struct resource busn_resource = { .name = "PCI busn", .start = 0, .end = 255, .flags = IORESOURCE_BUS, }; /* Ugh. Need to stop exporting this to modules. */ LIST_HEAD(pci_root_buses); EXPORT_SYMBOL(pci_root_buses); static LIST_HEAD(pci_domain_busn_res_list); struct pci_domain_busn_res { struct list_head list; struct resource res; int domain_nr; }; static struct resource *get_pci_domain_busn_res(int domain_nr) { struct pci_domain_busn_res *r; list_for_each_entry(r, &pci_domain_busn_res_list, list) if (r->domain_nr == domain_nr) return &r->res; r = kzalloc_obj(*r); if (!r) return NULL; r->domain_nr = domain_nr; r->res.start = 0; r->res.end = 0xff; r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED; list_add_tail(&r->list, &pci_domain_busn_res_list); return &r->res; } /* * PCI Bus Class */ static void release_pcibus_dev(struct device *dev) { struct pci_bus *pci_bus = to_pci_bus(dev); put_device(pci_bus->bridge); pci_bus_remove_resources(pci_bus); pci_release_bus_of_node(pci_bus); kfree(pci_bus); } static const struct class pcibus_class = { .name = "pci_bus", .dev_release = &release_pcibus_dev, .dev_groups = pcibus_groups, }; static int __init pcibus_class_init(void) { return class_register(&pcibus_class); } postcore_initcall(pcibus_class_init); static u64 pci_size(u64 base, u64 maxbase, u64 mask) { u64 size = mask & maxbase; /* Find the significant bits */ if (!size) return 0; /* * Get the lowest of them to find the decode size, and from that * the extent. */ size = size & ~(size-1); /* * base == maxbase can be valid only if the BAR has already been * programmed with all 1s. */ if (base == maxbase && ((base | (size - 1)) & mask) != mask) return 0; return size; } static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar) { u32 mem_type; unsigned long flags; if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) { flags = bar & ~PCI_BASE_ADDRESS_IO_MASK; flags |= IORESOURCE_IO; return flags; } flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK; flags |= IORESOURCE_MEM; if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH) flags |= IORESOURCE_PREFETCH; mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK; switch (mem_type) { case PCI_BASE_ADDRESS_MEM_TYPE_32: break; case PCI_BASE_ADDRESS_MEM_TYPE_1M: /* 1M mem BAR treated as 32-bit BAR */ break; case PCI_BASE_ADDRESS_MEM_TYPE_64: flags |= IORESOURCE_MEM_64; break; default: /* mem unknown type treated as 32-bit BAR */ break; } return flags; } #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO) /** * __pci_size_bars - Read the raw BAR mask for a range of PCI BARs * @dev: the PCI device * @count: number of BARs to size * @pos: starting config space position * @sizes: array to store mask values * @rom: indicate whether to use ROM mask, which avoids enabling ROM BARs * * Provided @sizes array must be sufficiently sized to store results for * @count u32 BARs. Caller is responsible for disabling decode to specified * BAR range around calling this function. This function is intended to avoid * disabling decode around sizing each BAR individually, which can result in * non-trivial overhead in virtualized environments with very large PCI BARs. */ static void __pci_size_bars(struct pci_dev *dev, int count, unsigned int pos, u32 *sizes, bool rom) { u32 orig, mask = rom ? PCI_ROM_ADDRESS_MASK : ~0; int i; for (i = 0; i < count; i++, pos += 4, sizes++) { pci_read_config_dword(dev, pos, &orig); pci_write_config_dword(dev, pos, mask); pci_read_config_dword(dev, pos, sizes); pci_write_config_dword(dev, pos, orig); } } void __pci_size_stdbars(struct pci_dev *dev, int count, unsigned int pos, u32 *sizes) { __pci_size_bars(dev, count, pos, sizes, false); } static void __pci_size_rom(struct pci_dev *dev, unsigned int pos, u32 *sizes) { __pci_size_bars(dev, 1, pos, sizes, true); } /** * __pci_read_base - Read a PCI BAR * @dev: the PCI device * @type: type of the BAR * @res: resource buffer to be filled in * @pos: BAR position in the config space * @sizes: array of one or more pre-read BAR masks * * Returns 1 if the BAR is 64-bit, or 0 if 32-bit. */ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, struct resource *res, unsigned int pos, u32 *sizes) { u32 l = 0, sz; u64 l64, sz64, mask64; struct pci_bus_region region, inverted_region; const char *res_name = pci_resource_name(dev, res - dev->resource); res->name = pci_name(dev); pci_read_config_dword(dev, pos, &l); sz = sizes[0]; /* * All bits set in sz means the device isn't working properly. * If the BAR isn't implemented, all bits must be 0. If it's a * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit * 1 must be clear. */ if (PCI_POSSIBLE_ERROR(sz)) sz = 0; /* * I don't know how l can have all bits set. Copied from old code. * Maybe it fixes a bug on some ancient platform. */ if (PCI_POSSIBLE_ERROR(l)) l = 0; if (type == pci_bar_unknown) { res->flags = decode_bar(dev, l); res->flags |= IORESOURCE_SIZEALIGN; if (res->flags & IORESOURCE_IO) { l64 = l & PCI_BASE_ADDRESS_IO_MASK; sz64 = sz & PCI_BASE_ADDRESS_IO_MASK; mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT; } else { l64 = l & PCI_BASE_ADDRESS_MEM_MASK; sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK; mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK; } } else { if (l & PCI_ROM_ADDRESS_ENABLE) res->flags |= IORESOURCE_ROM_ENABLE; l64 = l & PCI_ROM_ADDRESS_MASK; sz64 = sz & PCI_ROM_ADDRESS_MASK; mask64 = PCI_ROM_ADDRESS_MASK; } if (res->flags & IORESOURCE_MEM_64) { pci_read_config_dword(dev, pos + 4, &l); sz = sizes[1]; l64 |= ((u64)l << 32); sz64 |= ((u64)sz << 32); mask64 |= ((u64)~0 << 32); } if (!sz64) goto fail; sz64 = pci_size(l64, sz64, mask64); if (!sz64) { pci_info(dev, FW_BUG "%s: invalid; can't size\n", res_name); goto fail; } if (res->flags & IORESOURCE_MEM_64) { if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8) && sz64 > 0x100000000ULL) { res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; resource_set_range(res, 0, 0); pci_err(dev, "%s: can't handle BAR larger than 4GB (size %#010llx)\n", res_name, (unsigned long long)sz64); goto out; } if ((sizeof(pci_bus_addr_t) < 8) && l) { /* Above 32-bit boundary; try to reallocate */ res->flags |= IORESOURCE_UNSET; resource_set_range(res, 0, sz64); pci_info(dev, "%s: can't handle BAR above 4GB (bus address %#010llx)\n", res_name, (unsigned long long)l64); goto out; } } region.start = l64; region.end = l64 + sz64 - 1; pcibios_bus_to_resource(dev->bus, res, ®ion); pcibios_resource_to_bus(dev->bus, &inverted_region, res); /* * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is * the corresponding resource address (the physical address used by * the CPU. Converting that resource address back to a bus address * should yield the original BAR value: * * resource_to_bus(bus_to_resource(A)) == A * * If it doesn't, CPU accesses to "bus_to_resource(A)" will not * be claimed by the device. */ if (inverted_region.start != region.start) { res->flags |= IORESOURCE_UNSET; res->start = 0; res->end = region.end - region.start; pci_info(dev, "%s: initial BAR value %#010llx invalid\n", res_name, (unsigned long long)region.start); } goto out; fail: res->flags = 0; out: if (res->flags) pci_info(dev, "%s %pR\n", res_name, res); return (res->flags & IORESOURCE_MEM_64) ? 1 : 0; } static __always_inline void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) { u32 rombar, stdbars[PCI_STD_NUM_BARS]; unsigned int pos, reg; u16 orig_cmd; BUILD_BUG_ON(statically_true(howmany > PCI_STD_NUM_BARS)); if (dev->non_compliant_bars) return; /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */ if (dev->is_virtfn) return; /* No printks while decoding is disabled! */ if (!dev->mmio_always_on) { pci_read_config_word(dev, PCI_COMMAND, &orig_cmd); if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) { pci_write_config_word(dev, PCI_COMMAND, orig_cmd & ~PCI_COMMAND_DECODE_ENABLE); } } __pci_size_stdbars(dev, howmany, PCI_BASE_ADDRESS_0, stdbars); if (rom) __pci_size_rom(dev, rom, &rombar); if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE)) pci_write_config_word(dev, PCI_COMMAND, orig_cmd); 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, &stdbars[pos]); } if (rom) { struct resource *res = &dev->resource[PCI_ROM_RESOURCE]; dev->rom_base_reg = rom; res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_READONLY | IORESOURCE_SIZEALIGN; __pci_read_base(dev, pci_bar_mem32, res, rom, &rombar); } } static void pci_read_bridge_io(struct pci_dev *dev, struct resource *res, bool log) { u8 io_base_lo, io_limit_lo; unsigned long io_mask, io_granularity, base, limit; struct pci_bus_region region; if (!dev->io_window) return; io_mask = PCI_IO_RANGE_MASK; io_granularity = 0x1000; if (dev->io_window_1k) { /* Support 1K I/O space granularity */ io_mask = PCI_IO_1K_RANGE_MASK; io_granularity = 0x400; } pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo); base = (io_base_lo & io_mask) << 8; limit = (io_limit_lo & io_mask) << 8; if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { u16 io_base_hi, io_limit_hi; pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi); pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi); base |= ((unsigned long) io_base_hi << 16); limit |= ((unsigned long) io_limit_hi << 16); } res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; if (base <= limit) { region.start = base; region.end = limit + io_granularity - 1; pcibios_bus_to_resource(dev->bus, res, ®ion); if (log) pci_info(dev, " bridge window %pR\n", res); } else { resource_set_range(res, 0, 0); res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; } } static void pci_read_bridge_mmio(struct pci_dev *dev, struct resource *res, bool log) { u16 mem_base_lo, mem_limit_lo; unsigned long base, limit; struct pci_bus_region region; pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo); pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo); base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16; limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM; if (base <= limit) { region.start = base; region.end = limit + 0xfffff; pcibios_bus_to_resource(dev->bus, res, ®ion); if (log) pci_info(dev, " bridge window %pR\n", res); } else { resource_set_range(res, 0, 0); res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; } } static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res, bool log) { u16 mem_base_lo, mem_limit_lo; u64 base64, limit64; pci_bus_addr_t base, limit; struct pci_bus_region region; if (!dev->pref_window) return; pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo); pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo); base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16; limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16; if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { u32 mem_base_hi, mem_limit_hi; pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi); pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi); /* * Some bridges set the base > limit by default, and some * (broken) BIOSes do not initialize them. If we find * this, just assume they are not being used. */ if (mem_base_hi <= mem_limit_hi) { base64 |= (u64) mem_base_hi << 32; limit64 |= (u64) mem_limit_hi << 32; } } base = (pci_bus_addr_t) base64; limit = (pci_bus_addr_t) limit64; if (base != base64) { pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n", (unsigned long long) base64); return; } res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH; if (res->flags & PCI_PREF_RANGE_TYPE_64) res->flags |= IORESOURCE_MEM_64; if (base <= limit) { region.start = base; region.end = limit + 0xfffff; pcibios_bus_to_resource(dev->bus, res, ®ion); if (log) pci_info(dev, " bridge window %pR\n", res); } else { resource_set_range(res, 0, 0);