1 // SPDX-License-Identifier: GPL-2.0
3 * From setup-res.c, by:
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <linux/errno.h>
13 #include <linux/ioport.h>
15 #include <linux/of_platform.h>
16 #include <linux/proc_fs.h>
17 #include <linux/slab.h>
21 void pci_add_resource_offset(struct list_head *resources, struct resource *res,
22 resource_size_t offset)
24 struct resource_entry *entry;
26 entry = resource_list_create_entry(res, 0);
28 pr_err("PCI: can't add host bridge window %pR\n", res);
32 entry->offset = offset;
33 resource_list_add_tail(entry, resources);
35 EXPORT_SYMBOL(pci_add_resource_offset);
37 void pci_add_resource(struct list_head *resources, struct resource *res)
39 pci_add_resource_offset(resources, res, 0);
41 EXPORT_SYMBOL(pci_add_resource);
43 void pci_free_resource_list(struct list_head *resources)
45 resource_list_free(resources);
47 EXPORT_SYMBOL(pci_free_resource_list);
49 void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
52 struct pci_bus_resource *bus_res;
54 bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
56 dev_err(&bus->dev, "can't add %pR resource\n", res);
61 bus_res->flags = flags;
62 list_add_tail(&bus_res->list, &bus->resources);
65 struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
67 struct pci_bus_resource *bus_res;
69 if (n < PCI_BRIDGE_RESOURCE_NUM)
70 return bus->resource[n];
72 n -= PCI_BRIDGE_RESOURCE_NUM;
73 list_for_each_entry(bus_res, &bus->resources, list) {
79 EXPORT_SYMBOL_GPL(pci_bus_resource_n);
81 void pci_bus_remove_resource(struct pci_bus *bus, struct resource *res)
83 struct pci_bus_resource *bus_res, *tmp;
86 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
87 if (bus->resource[i] == res) {
88 bus->resource[i] = NULL;
93 list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
94 if (bus_res->res == res) {
95 list_del(&bus_res->list);
102 void pci_bus_remove_resources(struct pci_bus *bus)
105 struct pci_bus_resource *bus_res, *tmp;
107 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
108 bus->resource[i] = NULL;
110 list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
111 list_del(&bus_res->list);
116 int devm_request_pci_bus_resources(struct device *dev,
117 struct list_head *resources)
119 struct resource_entry *win;
120 struct resource *parent, *res;
123 resource_list_for_each_entry(win, resources) {
125 switch (resource_type(res)) {
127 parent = &ioport_resource;
130 parent = &iomem_resource;
136 err = devm_request_resource(dev, parent, res);
143 EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
145 static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
146 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
147 static struct pci_bus_region pci_64_bit = {0,
148 (pci_bus_addr_t) 0xffffffffffffffffULL};
149 static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
150 (pci_bus_addr_t) 0xffffffffffffffffULL};
154 * @res contains CPU addresses. Clip it so the corresponding bus addresses
155 * on @bus are entirely within @region. This is used to control the bus
156 * addresses of resources we allocate, e.g., we may need a resource that
157 * can be mapped by a 32-bit BAR.
159 static void pci_clip_resource_to_region(struct pci_bus *bus,
160 struct resource *res,
161 struct pci_bus_region *region)
163 struct pci_bus_region r;
165 pcibios_resource_to_bus(bus, &r, res);
166 if (r.start < region->start)
167 r.start = region->start;
168 if (r.end > region->end)
172 res->end = res->start - 1;
174 pcibios_bus_to_resource(bus, res, &r);
177 static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
178 resource_size_t size, resource_size_t align,
179 resource_size_t min, unsigned long type_mask,
180 resource_alignf alignf,
182 struct pci_bus_region *region)
184 struct resource *r, avail;
188 type_mask |= IORESOURCE_TYPE_BITS;
190 pci_bus_for_each_resource(bus, r) {
191 resource_size_t min_used = min;
196 /* type_mask must match */
197 if ((res->flags ^ r->flags) & type_mask)
200 /* We cannot allocate a non-prefetching resource
201 from a pre-fetching area */
202 if ((r->flags & IORESOURCE_PREFETCH) &&
203 !(res->flags & IORESOURCE_PREFETCH))
207 pci_clip_resource_to_region(bus, &avail, region);
210 * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
211 * protect badly documented motherboard resources, but if
212 * this is an already-configured bridge window, its start
216 min_used = avail.start;
220 /* Don't bother if available space isn't large enough */
221 if (size > max - min_used + 1)
224 /* Ok, try it out.. */
225 ret = allocate_resource(r, res, size, min_used, max,
226 align, alignf, alignf_data);
234 * pci_bus_alloc_resource - allocate a resource from a parent bus
236 * @res: resource to allocate
237 * @size: size of resource to allocate
238 * @align: alignment of resource to allocate
239 * @min: minimum /proc/iomem address to allocate
240 * @type_mask: IORESOURCE_* type flags
241 * @alignf: resource alignment function
242 * @alignf_data: data argument for resource alignment function
244 * Given the PCI bus a device resides on, the size, minimum address,
245 * alignment and type, try to find an acceptable resource allocation
246 * for a specific device resource.
248 int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
249 resource_size_t size, resource_size_t align,
250 resource_size_t min, unsigned long type_mask,
251 resource_alignf alignf,
254 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
257 if (res->flags & IORESOURCE_MEM_64) {
258 rc = pci_bus_alloc_from_region(bus, res, size, align, min,
259 type_mask, alignf, alignf_data,
264 return pci_bus_alloc_from_region(bus, res, size, align, min,
265 type_mask, alignf, alignf_data,
270 return pci_bus_alloc_from_region(bus, res, size, align, min,
271 type_mask, alignf, alignf_data,
274 EXPORT_SYMBOL(pci_bus_alloc_resource);
277 * The @idx resource of @dev should be a PCI-PCI bridge window. If this
278 * resource fits inside a window of an upstream bridge, do nothing. If it
279 * overlaps an upstream window but extends outside it, clip the resource so
280 * it fits completely inside.
282 bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
284 struct pci_bus *bus = dev->bus;
285 struct resource *res = &dev->resource[idx];
286 struct resource orig_res = *res;
289 pci_bus_for_each_resource(bus, r) {
290 resource_size_t start, end;
295 if (resource_type(res) != resource_type(r))
298 start = max(r->start, res->start);
299 end = min(r->end, res->end);
302 continue; /* no overlap */
304 if (res->start == start && res->end == end)
305 return false; /* no change */
309 res->flags &= ~IORESOURCE_UNSET;
310 orig_res.flags &= ~IORESOURCE_UNSET;
311 pci_info(dev, "%pR clipped to %pR\n", &orig_res, res);
319 void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
321 void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
324 * pci_bus_add_device - start driver for a single device
325 * @dev: device to add
327 * This adds add sysfs entries and start device drivers
329 void pci_bus_add_device(struct pci_dev *dev)
331 struct device_node *dn = dev->dev.of_node;
335 * Can not put in pci_device_add yet because resources
336 * are not assigned yet for some devices.
338 pcibios_bus_add_device(dev);
339 pci_fixup_device(pci_fixup_final, dev);
340 if (pci_is_bridge(dev))
341 of_pci_make_dev_node(dev);
342 pci_create_sysfs_dev_files(dev);
343 pci_proc_attach_device(dev);
344 pci_bridge_d3_update(dev);
346 dev->match_driver = !dn || of_device_is_available(dn);
347 retval = device_attach(&dev->dev);
348 if (retval < 0 && retval != -EPROBE_DEFER)
349 pci_warn(dev, "device attach failed (%d)\n", retval);
351 pci_dev_assign_added(dev, true);
353 if (dev_of_node(&dev->dev) && pci_is_bridge(dev)) {
354 retval = of_platform_populate(dev_of_node(&dev->dev), NULL, NULL,
357 pci_err(dev, "failed to populate child OF nodes (%d)\n",
361 EXPORT_SYMBOL_GPL(pci_bus_add_device);
364 * pci_bus_add_devices - start driver for PCI devices
365 * @bus: bus to check for new devices
367 * Start driver for PCI devices and add some sysfs entries.
369 void pci_bus_add_devices(const struct pci_bus *bus)
372 struct pci_bus *child;
374 list_for_each_entry(dev, &bus->devices, bus_list) {
375 /* Skip already-added devices */
376 if (pci_dev_is_added(dev))
378 pci_bus_add_device(dev);
381 list_for_each_entry(dev, &bus->devices, bus_list) {
382 /* Skip if device attach failed */
383 if (!pci_dev_is_added(dev))
385 child = dev->subordinate;
387 pci_bus_add_devices(child);
390 EXPORT_SYMBOL(pci_bus_add_devices);
392 static void __pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
393 void *userdata, bool locked)
397 struct list_head *next;
402 down_read(&pci_bus_sem);
403 next = top->devices.next;
405 if (next == &bus->devices) {
406 /* end of this bus, go up or finish */
409 next = bus->self->bus_list.next;
410 bus = bus->self->bus;
413 dev = list_entry(next, struct pci_dev, bus_list);
414 if (dev->subordinate) {
415 /* this is a pci-pci bridge, do its devices next */
416 next = dev->subordinate->devices.next;
417 bus = dev->subordinate;
419 next = dev->bus_list.next;
421 retval = cb(dev, userdata);
426 up_read(&pci_bus_sem);
430 * pci_walk_bus - walk devices on/under bus, calling callback.
431 * @top: bus whose devices should be walked
432 * @cb: callback to be called for each device found
433 * @userdata: arbitrary pointer to be passed to callback
435 * Walk the given bus, including any bridged devices
436 * on buses under this bus. Call the provided callback
437 * on each device found.
439 * We check the return of @cb each time. If it returns anything
440 * other than 0, we break out.
442 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata)
444 __pci_walk_bus(top, cb, userdata, false);
446 EXPORT_SYMBOL_GPL(pci_walk_bus);
448 void pci_walk_bus_locked(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata)
450 lockdep_assert_held(&pci_bus_sem);
452 __pci_walk_bus(top, cb, userdata, true);
454 EXPORT_SYMBOL_GPL(pci_walk_bus_locked);
456 struct pci_bus *pci_bus_get(struct pci_bus *bus)
459 get_device(&bus->dev);
463 void pci_bus_put(struct pci_bus *bus)
466 put_device(&bus->dev);