^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * From setup-res.c, by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Dave Rusling (david.rusling@reo.mts.dec.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * David Mosberger (davidm@cs.arizona.edu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * David Miller (davem@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Ivan Kokshaysky (ink@jurassic.park.msu.ru)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "pci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void pci_add_resource_offset(struct list_head *resources, struct resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) resource_size_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct resource_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) entry = resource_list_create_entry(res, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) pr_err("PCI: can't add host bridge window %pR\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) entry->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) resource_list_add_tail(entry, resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) EXPORT_SYMBOL(pci_add_resource_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) void pci_add_resource(struct list_head *resources, struct resource *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pci_add_resource_offset(resources, res, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) EXPORT_SYMBOL(pci_add_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void pci_free_resource_list(struct list_head *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) resource_list_free(resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) EXPORT_SYMBOL(pci_free_resource_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct pci_bus_resource *bus_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!bus_res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dev_err(&bus->dev, "can't add %pR resource\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) bus_res->res = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) bus_res->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) list_add_tail(&bus_res->list, &bus->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct pci_bus_resource *bus_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (n < PCI_BRIDGE_RESOURCE_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return bus->resource[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) n -= PCI_BRIDGE_RESOURCE_NUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) list_for_each_entry(bus_res, &bus->resources, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (n-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return bus_res->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) EXPORT_SYMBOL_GPL(pci_bus_resource_n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) void pci_bus_remove_resources(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct pci_bus_resource *bus_res, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) bus->resource[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) list_del(&bus_res->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) kfree(bus_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int devm_request_pci_bus_resources(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct list_head *resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct resource_entry *win;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct resource *parent, *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) resource_list_for_each_entry(win, resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) res = win->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) switch (resource_type(res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) case IORESOURCE_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) parent = &ioport_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case IORESOURCE_MEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) parent = &iomem_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err = devm_request_resource(dev, parent, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static struct pci_bus_region pci_64_bit = {0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) (pci_bus_addr_t) 0xffffffffffffffffULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) (pci_bus_addr_t) 0xffffffffffffffffULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * @res contains CPU addresses. Clip it so the corresponding bus addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * on @bus are entirely within @region. This is used to control the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * addresses of resources we allocate, e.g., we may need a resource that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * can be mapped by a 32-bit BAR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void pci_clip_resource_to_region(struct pci_bus *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct pci_bus_region *region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct pci_bus_region r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) pcibios_resource_to_bus(bus, &r, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (r.start < region->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) r.start = region->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (r.end > region->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) r.end = region->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (r.end < r.start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) res->end = res->start - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pcibios_bus_to_resource(bus, res, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) resource_size_t size, resource_size_t align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) resource_size_t min, unsigned long type_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) resource_size_t (*alignf)(void *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) const struct resource *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) resource_size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) resource_size_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void *alignf_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct pci_bus_region *region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct resource *r, avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) resource_size_t max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) type_mask |= IORESOURCE_TYPE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pci_bus_for_each_resource(bus, r, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) resource_size_t min_used = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* type_mask must match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if ((res->flags ^ r->flags) & type_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* We cannot allocate a non-prefetching resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) from a pre-fetching area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if ((r->flags & IORESOURCE_PREFETCH) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) !(res->flags & IORESOURCE_PREFETCH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) avail = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) pci_clip_resource_to_region(bus, &avail, region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * protect badly documented motherboard resources, but if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * this is an already-configured bridge window, its start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * overrides "min".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (avail.start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) min_used = avail.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) max = avail.end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* Ok, try it out.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = allocate_resource(r, res, size, min_used, max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) align, alignf, alignf_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * pci_bus_alloc_resource - allocate a resource from a parent bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @bus: PCI bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * @res: resource to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @size: size of resource to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * @align: alignment of resource to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * @min: minimum /proc/iomem address to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @type_mask: IORESOURCE_* type flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * @alignf: resource alignment function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * @alignf_data: data argument for resource alignment function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Given the PCI bus a device resides on, the size, minimum address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * alignment and type, try to find an acceptable resource allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * for a specific device resource.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) resource_size_t size, resource_size_t align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) resource_size_t min, unsigned long type_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) resource_size_t (*alignf)(void *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) const struct resource *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) resource_size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) resource_size_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) void *alignf_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (res->flags & IORESOURCE_MEM_64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) rc = pci_bus_alloc_from_region(bus, res, size, align, min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) type_mask, alignf, alignf_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) &pci_high);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return pci_bus_alloc_from_region(bus, res, size, align, min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) type_mask, alignf, alignf_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) &pci_64_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return pci_bus_alloc_from_region(bus, res, size, align, min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) type_mask, alignf, alignf_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) &pci_32_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) EXPORT_SYMBOL(pci_bus_alloc_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * The @idx resource of @dev should be a PCI-PCI bridge window. If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * resource fits inside a window of an upstream bridge, do nothing. If it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * overlaps an upstream window but extends outside it, clip the resource so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * it fits completely inside.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct pci_bus *bus = dev->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct resource *res = &dev->resource[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct resource orig_res = *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct resource *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) pci_bus_for_each_resource(bus, r, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) resource_size_t start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (resource_type(res) != resource_type(r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) start = max(r->start, res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) end = min(r->end, res->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) continue; /* no overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (res->start == start && res->end == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return false; /* no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) res->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) res->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) res->flags &= ~IORESOURCE_UNSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) orig_res.flags &= ~IORESOURCE_UNSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) pci_info(dev, "%pR clipped to %pR\n", &orig_res, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * pci_bus_add_device - start driver for a single device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @dev: device to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * This adds add sysfs entries and start device drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) void pci_bus_add_device(struct pci_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * Can not put in pci_device_add yet because resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * are not assigned yet for some devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) pcibios_bus_add_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) pci_fixup_device(pci_fixup_final, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) pci_create_sysfs_dev_files(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) pci_proc_attach_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) pci_bridge_d3_update(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) dev->match_driver = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) retval = device_attach(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (retval < 0 && retval != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) pci_warn(dev, "device attach failed (%d)\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) pci_dev_assign_added(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) EXPORT_SYMBOL_GPL(pci_bus_add_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * pci_bus_add_devices - start driver for PCI devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * @bus: bus to check for new devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * Start driver for PCI devices and add some sysfs entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) void pci_bus_add_devices(const struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct pci_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct pci_bus *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) list_for_each_entry(dev, &bus->devices, bus_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /* Skip already-added devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (pci_dev_is_added(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) pci_bus_add_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) list_for_each_entry(dev, &bus->devices, bus_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* Skip if device attach failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!pci_dev_is_added(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) child = dev->subordinate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) pci_bus_add_devices(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) EXPORT_SYMBOL(pci_bus_add_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /** pci_walk_bus - walk devices on/under bus, calling callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * @top bus whose devices should be walked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * @cb callback to be called for each device found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * @userdata arbitrary pointer to be passed to callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * Walk the given bus, including any bridged devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * on buses under this bus. Call the provided callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * on each device found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * We check the return of @cb each time. If it returns anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * other than 0, we break out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) void *userdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct pci_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct pci_bus *bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct list_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) bus = top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) down_read(&pci_bus_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) next = top->devices.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (next == &bus->devices) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* end of this bus, go up or finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (bus == top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) next = bus->self->bus_list.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) bus = bus->self->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) dev = list_entry(next, struct pci_dev, bus_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (dev->subordinate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* this is a pci-pci bridge, do its devices next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) next = dev->subordinate->devices.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) bus = dev->subordinate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) next = dev->bus_list.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) retval = cb(dev, userdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) up_read(&pci_bus_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) EXPORT_SYMBOL_GPL(pci_walk_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct pci_bus *pci_bus_get(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) get_device(&bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) void pci_bus_put(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) put_device(&bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }