Linux 4.16.11
[linux/fpc-iii.git] / drivers / pci / bus.c
blob737d1c52f002d605fafa97f11828ef09e931c1aa
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/pci/bus.c
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>
19 #include "pci.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);
27 if (!entry) {
28 printk(KERN_ERR "PCI: can't add host bridge window %pR\n", res);
29 return;
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,
50 unsigned int flags)
52 struct pci_bus_resource *bus_res;
54 bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
55 if (!bus_res) {
56 dev_err(&bus->dev, "can't add %pR resource\n", res);
57 return;
60 bus_res->res = 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) {
74 if (n-- == 0)
75 return bus_res->res;
77 return NULL;
79 EXPORT_SYMBOL_GPL(pci_bus_resource_n);
81 void pci_bus_remove_resources(struct pci_bus *bus)
83 int i;
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);
91 kfree(bus_res);
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;
100 int err;
102 resource_list_for_each_entry(win, resources) {
103 res = win->res;
104 switch (resource_type(res)) {
105 case IORESOURCE_IO:
106 parent = &ioport_resource;
107 break;
108 case IORESOURCE_MEM:
109 parent = &iomem_resource;
110 break;
111 default:
112 continue;
115 err = devm_request_resource(dev, parent, res);
116 if (err)
117 return err;
120 return 0;
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};
130 #endif
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)
148 r.end = region->end;
150 if (r.end < r.start)
151 res->end = res->start - 1;
152 else
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 *,
161 resource_size_t,
162 resource_size_t),
163 void *alignf_data,
164 struct pci_bus_region *region)
166 int i, ret;
167 struct resource *r, avail;
168 resource_size_t max;
170 type_mask |= IORESOURCE_TYPE_BITS;
172 pci_bus_for_each_resource(bus, r, i) {
173 resource_size_t min_used = min;
175 if (!r)
176 continue;
178 /* type_mask must match */
179 if ((res->flags ^ r->flags) & type_mask)
180 continue;
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))
186 continue;
188 avail = *r;
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
195 * overrides "min".
197 if (avail.start)
198 min_used = avail.start;
200 max = avail.end;
202 /* Ok, try it out.. */
203 ret = allocate_resource(r, res, size, min_used, max,
204 align, alignf, alignf_data);
205 if (ret == 0)
206 return 0;
208 return -ENOMEM;
212 * pci_bus_alloc_resource - allocate a resource from a parent bus
213 * @bus: PCI 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 *,
231 resource_size_t,
232 resource_size_t),
233 void *alignf_data)
235 #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
236 int rc;
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,
241 &pci_high);
242 if (rc == 0)
243 return 0;
245 return pci_bus_alloc_from_region(bus, res, size, align, min,
246 type_mask, alignf, alignf_data,
247 &pci_64_bit);
249 #endif
251 return pci_bus_alloc_from_region(bus, res, size, align, min,
252 type_mask, alignf, alignf_data,
253 &pci_32_bit);
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;
268 struct resource *r;
269 int i;
271 pci_bus_for_each_resource(bus, r, i) {
272 resource_size_t start, end;
274 if (!r)
275 continue;
277 if (resource_type(res) != resource_type(r))
278 continue;
280 start = max(r->start, res->start);
281 end = min(r->end, res->end);
283 if (start > end)
284 continue; /* no overlap */
286 if (res->start == start && res->end == end)
287 return false; /* no change */
289 res->start = start;
290 res->end = end;
291 res->flags &= ~IORESOURCE_UNSET;
292 orig_res.flags &= ~IORESOURCE_UNSET;
293 pci_printk(KERN_DEBUG, dev, "%pR clipped to %pR\n",
294 &orig_res, res);
296 return true;
299 return false;
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)
314 int retval;
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);
332 return;
335 dev->is_added = 1;
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)
347 struct pci_dev *dev;
348 struct pci_bus *child;
350 list_for_each_entry(dev, &bus->devices, bus_list) {
351 /* Skip already-added devices */
352 if (dev->is_added)
353 continue;
354 pci_bus_add_device(dev);
357 list_for_each_entry(dev, &bus->devices, bus_list) {
358 /* Skip if device attach failed */
359 if (!dev->is_added)
360 continue;
361 child = dev->subordinate;
362 if (child)
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 *),
382 void *userdata)
384 struct pci_dev *dev;
385 struct pci_bus *bus;
386 struct list_head *next;
387 int retval;
389 bus = top;
390 down_read(&pci_bus_sem);
391 next = top->devices.next;
392 for (;;) {
393 if (next == &bus->devices) {
394 /* end of this bus, go up or finish */
395 if (bus == top)
396 break;
397 next = bus->self->bus_list.next;
398 bus = bus->self->bus;
399 continue;
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;
406 } else
407 next = dev->bus_list.next;
409 retval = cb(dev, userdata);
410 if (retval)
411 break;
413 up_read(&pci_bus_sem);
415 EXPORT_SYMBOL_GPL(pci_walk_bus);
417 struct pci_bus *pci_bus_get(struct pci_bus *bus)
419 if (bus)
420 get_device(&bus->dev);
421 return bus;
423 EXPORT_SYMBOL(pci_bus_get);
425 void pci_bus_put(struct pci_bus *bus)
427 if (bus)
428 put_device(&bus->dev);
430 EXPORT_SYMBOL(pci_bus_put);