1 // SPDX-License-Identifier: GPL-2.0
5 * From setup-res.c, by:
6 * Dave Rusling (david.rusling@reo.mts.dec.com)
7 * David Mosberger (davidm@cs.arizona.edu)
8 * David Miller (davem@redhat.com)
9 * Ivan Kokshaysky (ink@jurassic.park.msu.ru)
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.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 printk(KERN_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_resources(struct pci_bus *bus)
84 struct pci_bus_resource *bus_res, *tmp;
86 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
87 bus->resource[i] = NULL;
89 list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
90 list_del(&bus_res->list);
95 int devm_request_pci_bus_resources(struct device *dev,
96 struct list_head *resources)
98 struct resource_entry *win;
99 struct resource *parent, *res;
102 resource_list_for_each_entry(win, resources) {
104 switch (resource_type(res)) {
106 parent = &ioport_resource;
109 parent = &iomem_resource;
115 err = devm_request_resource(dev, parent, res);
122 EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
124 static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
125 #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
126 static struct pci_bus_region pci_64_bit = {0,
127 (pci_bus_addr_t) 0xffffffffffffffffULL};
128 static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
129 (pci_bus_addr_t) 0xffffffffffffffffULL};
133 * @res contains CPU addresses. Clip it so the corresponding bus addresses
134 * on @bus are entirely within @region. This is used to control the bus
135 * addresses of resources we allocate, e.g., we may need a resource that
136 * can be mapped by a 32-bit BAR.
138 static void pci_clip_resource_to_region(struct pci_bus *bus,
139 struct resource *res,
140 struct pci_bus_region *region)
142 struct pci_bus_region r;
144 pcibios_resource_to_bus(bus, &r, res);
145 if (r.start < region->start)
146 r.start = region->start;
147 if (r.end > region->end)
151 res->end = res->start - 1;
153 pcibios_bus_to_resource(bus, res, &r);
156 static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
157 resource_size_t size, resource_size_t align,
158 resource_size_t min, unsigned long type_mask,
159 resource_size_t (*alignf)(void *,
160 const struct resource *,
164 struct pci_bus_region *region)
167 struct resource *r, avail;
170 type_mask |= IORESOURCE_TYPE_BITS;
172 pci_bus_for_each_resource(bus, r, i) {
173 resource_size_t min_used = min;
178 /* type_mask must match */
179 if ((res->flags ^ r->flags) & type_mask)
182 /* We cannot allocate a non-prefetching resource
183 from a pre-fetching area */
184 if ((r->flags & IORESOURCE_PREFETCH) &&
185 !(res->flags & IORESOURCE_PREFETCH))
189 pci_clip_resource_to_region(bus, &avail, region);
192 * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
193 * protect badly documented motherboard resources, but if
194 * this is an already-configured bridge window, its start
198 min_used = avail.start;
202 /* Ok, try it out.. */
203 ret = allocate_resource(r, res, size, min_used, max,
204 align, alignf, alignf_data);
212 * pci_bus_alloc_resource - allocate a resource from a parent bus
214 * @res: resource to allocate
215 * @size: size of resource to allocate
216 * @align: alignment of resource to allocate
217 * @min: minimum /proc/iomem address to allocate
218 * @type_mask: IORESOURCE_* type flags
219 * @alignf: resource alignment function
220 * @alignf_data: data argument for resource alignment function
222 * Given the PCI bus a device resides on, the size, minimum address,
223 * alignment and type, try to find an acceptable resource allocation
224 * for a specific device resource.
226 int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
227 resource_size_t size, resource_size_t align,
228 resource_size_t min, unsigned long type_mask,
229 resource_size_t (*alignf)(void *,
230 const struct resource *,
235 #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
238 if (res->flags & IORESOURCE_MEM_64) {
239 rc = pci_bus_alloc_from_region(bus, res, size, align, min,
240 type_mask, alignf, alignf_data,
245 return pci_bus_alloc_from_region(bus, res, size, align, min,
246 type_mask, alignf, alignf_data,
251 return pci_bus_alloc_from_region(bus, res, size, align, min,
252 type_mask, alignf, alignf_data,
255 EXPORT_SYMBOL(pci_bus_alloc_resource);
258 * The @idx resource of @dev should be a PCI-PCI bridge window. If this
259 * resource fits inside a window of an upstream bridge, do nothing. If it
260 * overlaps an upstream window but extends outside it, clip the resource so
261 * it fits completely inside.
263 bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
265 struct pci_bus *bus = dev->bus;
266 struct resource *res = &dev->resource[idx];
267 struct resource orig_res = *res;
271 pci_bus_for_each_resource(bus, r, i) {
272 resource_size_t start, end;
277 if (resource_type(res) != resource_type(r))
280 start = max(r->start, res->start);
281 end = min(r->end, res->end);
284 continue; /* no overlap */
286 if (res->start == start && res->end == end)
287 return false; /* no change */
291 res->flags &= ~IORESOURCE_UNSET;
292 orig_res.flags &= ~IORESOURCE_UNSET;
293 pci_printk(KERN_DEBUG, dev, "%pR clipped to %pR\n",
302 void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
304 void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
307 * pci_bus_add_device - start driver for a single device
308 * @dev: device to add
310 * This adds add sysfs entries and start device drivers
312 void pci_bus_add_device(struct pci_dev *dev)
317 * Can not put in pci_device_add yet because resources
318 * are not assigned yet for some devices.
320 pcibios_bus_add_device(dev);
321 pci_fixup_device(pci_fixup_final, dev);
322 pci_create_sysfs_dev_files(dev);
323 pci_proc_attach_device(dev);
324 pci_bridge_d3_update(dev);
326 dev->match_driver = true;
327 retval = device_attach(&dev->dev);
328 if (retval < 0 && retval != -EPROBE_DEFER) {
329 pci_warn(dev, "device attach failed (%d)\n", retval);
330 pci_proc_detach_device(dev);
331 pci_remove_sysfs_dev_files(dev);
337 EXPORT_SYMBOL_GPL(pci_bus_add_device);
340 * pci_bus_add_devices - start driver for PCI devices
341 * @bus: bus to check for new devices
343 * Start driver for PCI devices and add some sysfs entries.
345 void pci_bus_add_devices(const struct pci_bus *bus)
348 struct pci_bus *child;
350 list_for_each_entry(dev, &bus->devices, bus_list) {
351 /* Skip already-added devices */
354 pci_bus_add_device(dev);
357 list_for_each_entry(dev, &bus->devices, bus_list) {
358 /* Skip if device attach failed */
361 child = dev->subordinate;
363 pci_bus_add_devices(child);
366 EXPORT_SYMBOL(pci_bus_add_devices);
368 /** pci_walk_bus - walk devices on/under bus, calling callback.
369 * @top bus whose devices should be walked
370 * @cb callback to be called for each device found
371 * @userdata arbitrary pointer to be passed to callback.
373 * Walk the given bus, including any bridged devices
374 * on buses under this bus. Call the provided callback
375 * on each device found.
377 * We check the return of @cb each time. If it returns anything
378 * other than 0, we break out.
381 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
386 struct list_head *next;
390 down_read(&pci_bus_sem);
391 next = top->devices.next;
393 if (next == &bus->devices) {
394 /* end of this bus, go up or finish */
397 next = bus->self->bus_list.next;
398 bus = bus->self->bus;
401 dev = list_entry(next, struct pci_dev, bus_list);
402 if (dev->subordinate) {
403 /* this is a pci-pci bridge, do its devices next */
404 next = dev->subordinate->devices.next;
405 bus = dev->subordinate;
407 next = dev->bus_list.next;
409 retval = cb(dev, userdata);
413 up_read(&pci_bus_sem);
415 EXPORT_SYMBOL_GPL(pci_walk_bus);
417 struct pci_bus *pci_bus_get(struct pci_bus *bus)
420 get_device(&bus->dev);
423 EXPORT_SYMBOL(pci_bus_get);
425 void pci_bus_put(struct pci_bus *bus)
428 put_device(&bus->dev);
430 EXPORT_SYMBOL(pci_bus_put);