blob: 30a4d33038bf6c5e142e68b2f12597d85523f54b [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/pci/bus.c
4 *
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)
10 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "pci.h"
20
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -070021void pci_add_resource_offset(struct list_head *resources, struct resource *res,
22 resource_size_t offset)
Bjorn Helgaas45ca9e92011-10-28 16:25:35 -060023{
Jiang Liu14d76b62015-02-05 13:44:44 +080024 struct resource_entry *entry;
Bjorn Helgaas45ca9e92011-10-28 16:25:35 -060025
Jiang Liu14d76b62015-02-05 13:44:44 +080026 entry = resource_list_create_entry(res, 0);
27 if (!entry) {
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -070028 printk(KERN_ERR "PCI: can't add host bridge window %pR\n", res);
Bjorn Helgaas45ca9e92011-10-28 16:25:35 -060029 return;
30 }
31
Jiang Liu14d76b62015-02-05 13:44:44 +080032 entry->offset = offset;
33 resource_list_add_tail(entry, resources);
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -070034}
35EXPORT_SYMBOL(pci_add_resource_offset);
36
37void pci_add_resource(struct list_head *resources, struct resource *res)
38{
39 pci_add_resource_offset(resources, res, 0);
Bjorn Helgaas45ca9e92011-10-28 16:25:35 -060040}
41EXPORT_SYMBOL(pci_add_resource);
42
43void pci_free_resource_list(struct list_head *resources)
44{
Jiang Liu14d76b62015-02-05 13:44:44 +080045 resource_list_free(resources);
Bjorn Helgaas45ca9e92011-10-28 16:25:35 -060046}
47EXPORT_SYMBOL(pci_free_resource_list);
48
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070049void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
50 unsigned int flags)
51{
52 struct pci_bus_resource *bus_res;
53
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;
58 }
59
60 bus_res->res = res;
61 bus_res->flags = flags;
62 list_add_tail(&bus_res->list, &bus->resources);
63}
64
65struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
66{
67 struct pci_bus_resource *bus_res;
68
69 if (n < PCI_BRIDGE_RESOURCE_NUM)
70 return bus->resource[n];
71
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;
76 }
77 return NULL;
78}
79EXPORT_SYMBOL_GPL(pci_bus_resource_n);
80
81void pci_bus_remove_resources(struct pci_bus *bus)
82{
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070083 int i;
Yinghai Lu817a2682012-09-14 17:48:41 -070084 struct pci_bus_resource *bus_res, *tmp;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070085
86 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
Stephen Hemminger7736a052010-06-01 09:00:16 -070087 bus->resource[i] = NULL;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070088
Yinghai Lu817a2682012-09-14 17:48:41 -070089 list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
90 list_del(&bus_res->list);
91 kfree(bus_res);
92 }
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070093}
94
Bjorn Helgaas950334b2016-05-28 18:09:16 -050095int devm_request_pci_bus_resources(struct device *dev,
96 struct list_head *resources)
97{
98 struct resource_entry *win;
99 struct resource *parent, *res;
100 int err;
101
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;
113 }
114
115 err = devm_request_resource(dev, parent, res);
116 if (err)
117 return err;
118 }
119
120 return 0;
121}
122EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
123
Yinghai Luf75b99d2013-12-20 09:57:37 -0700124static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700125#ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
Yinghai Luf75b99d2013-12-20 09:57:37 -0700126static struct pci_bus_region pci_64_bit = {0,
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700127 (pci_bus_addr_t) 0xffffffffffffffffULL};
128static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
129 (pci_bus_addr_t) 0xffffffffffffffffULL};
Yinghai Luf75b99d2013-12-20 09:57:37 -0700130#endif
131
132/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Yinghai Luf75b99d2013-12-20 09:57:37 -0700138static void pci_clip_resource_to_region(struct pci_bus *bus,
139 struct resource *res,
140 struct pci_bus_region *region)
141{
142 struct pci_bus_region r;
143
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;
149
150 if (r.end < r.start)
151 res->end = res->start - 1;
152 else
153 pcibios_bus_to_resource(bus, res, &r);
154}
155
156static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700157 resource_size_t size, resource_size_t align,
Bjorn Helgaas664c2842014-03-07 13:51:12 -0700158 resource_size_t min, unsigned long type_mask,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100159 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100160 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100161 resource_size_t,
162 resource_size_t),
Yinghai Luf75b99d2013-12-20 09:57:37 -0700163 void *alignf_data,
164 struct pci_bus_region *region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Yinghai Luf75b99d2013-12-20 09:57:37 -0700166 int i, ret;
167 struct resource *r, avail;
168 resource_size_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Bjorn Helgaasaa11fc52014-03-07 13:39:01 -0700170 type_mask |= IORESOURCE_TYPE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Bjorn Helgaas6db45b72010-12-16 10:38:36 -0700172 pci_bus_for_each_resource(bus, r, i) {
Christoph Biedl3460baa2015-12-23 16:51:57 +0100173 resource_size_t min_used = min;
174
Bjorn Helgaas6db45b72010-12-16 10:38:36 -0700175 if (!r)
176 continue;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* type_mask must match */
179 if ((res->flags ^ r->flags) & type_mask)
180 continue;
181
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;
187
Yinghai Luf75b99d2013-12-20 09:57:37 -0700188 avail = *r;
189 pci_clip_resource_to_region(bus, &avail, region);
Yinghai Luf75b99d2013-12-20 09:57:37 -0700190
191 /*
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".
196 */
197 if (avail.start)
Christoph Biedl3460baa2015-12-23 16:51:57 +0100198 min_used = avail.start;
Yinghai Luf75b99d2013-12-20 09:57:37 -0700199
200 max = avail.end;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Ok, try it out.. */
Christoph Biedl3460baa2015-12-23 16:51:57 +0100203 ret = allocate_resource(r, res, size, min_used, max,
Yinghai Luf75b99d2013-12-20 09:57:37 -0700204 align, alignf, alignf_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (ret == 0)
Yinghai Luf75b99d2013-12-20 09:57:37 -0700206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Yinghai Luf75b99d2013-12-20 09:57:37 -0700208 return -ENOMEM;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/**
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
221 *
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.
225 */
Yinghai Lud56dbf52013-12-20 10:55:44 -0700226int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 resource_size_t size, resource_size_t align,
Bjorn Helgaas664c2842014-03-07 13:51:12 -0700228 resource_size_t min, unsigned long type_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 resource_size_t (*alignf)(void *,
230 const struct resource *,
231 resource_size_t,
232 resource_size_t),
233 void *alignf_data)
234{
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700235#ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
Yinghai Lud56dbf52013-12-20 10:55:44 -0700236 int rc;
237
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;
244
Yinghai Luf75b99d2013-12-20 09:57:37 -0700245 return pci_bus_alloc_from_region(bus, res, size, align, min,
246 type_mask, alignf, alignf_data,
247 &pci_64_bit);
Yinghai Lud56dbf52013-12-20 10:55:44 -0700248 }
Yinghai Luf75b99d2013-12-20 09:57:37 -0700249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Yinghai Luf75b99d2013-12-20 09:57:37 -0700251 return pci_bus_alloc_from_region(bus, res, size, align, min,
252 type_mask, alignf, alignf_data,
253 &pci_32_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600255EXPORT_SYMBOL(pci_bus_alloc_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Yinghai Lu0f7e7ae2015-01-15 16:21:49 -0600257/*
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.
262 */
263bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
264{
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;
270
271 pci_bus_for_each_resource(bus, r, i) {
272 resource_size_t start, end;
273
274 if (!r)
275 continue;
276
277 if (resource_type(res) != resource_type(r))
278 continue;
279
280 start = max(r->start, res->start);
281 end = min(r->end, res->end);
282
283 if (start > end)
284 continue; /* no overlap */
285
286 if (res->start == start && res->end == end)
287 return false; /* no change */
288
289 res->start = start;
290 res->end = end;
Bjorn Helgaasb838b392015-09-22 17:03:54 -0500291 res->flags &= ~IORESOURCE_UNSET;
292 orig_res.flags &= ~IORESOURCE_UNSET;
Yinghai Lu0f7e7ae2015-01-15 16:21:49 -0600293 dev_printk(KERN_DEBUG, &dev->dev, "%pR clipped to %pR\n",
294 &orig_res, res);
295
296 return true;
297 }
298
299 return false;
300}
301
Yinghai Lu3c449ed2012-11-03 21:39:31 -0700302void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
303
Wei Yang7b770612016-03-04 10:53:04 +1100304void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/**
Yinghai Lu4f535092013-01-21 13:20:52 -0800307 * pci_bus_add_device - start driver for a single device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * @dev: device to add
309 *
Yinghai Lu4f535092013-01-21 13:20:52 -0800310 * This adds add sysfs entries and start device drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Yijing Wangc893d132014-05-30 11:01:03 +0800312void pci_bus_add_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700314 int retval;
Myron Stowe735bff12012-07-09 15:36:46 -0600315
Yinghai Lu4f535092013-01-21 13:20:52 -0800316 /*
317 * Can not put in pci_device_add yet because resources
318 * are not assigned yet for some devices.
319 */
Wei Yang7b770612016-03-04 10:53:04 +1100320 pcibios_bus_add_device(dev);
Yinghai Lue253aaf2013-05-07 14:35:44 -0600321 pci_fixup_device(pci_fixup_final, dev);
Yinghai Lu4f535092013-01-21 13:20:52 -0800322 pci_create_sysfs_dev_files(dev);
Yinghai Luef377022013-11-30 14:40:28 -0800323 pci_proc_attach_device(dev);
Lukas Wunner1ed276a2016-10-28 10:52:06 +0200324 pci_bridge_d3_update(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Yinghai Lu58d9a382013-01-21 13:20:51 -0800326 dev->match_driver = true;
327 retval = device_attach(&dev->dev);
Lukas Wunner9a2a5a62016-05-02 13:48:31 -0500328 if (retval < 0 && retval != -EPROBE_DEFER) {
Bjorn Helgaasab1a1872016-01-27 07:35:07 -0600329 dev_warn(&dev->dev, "device attach failed (%d)\n", retval);
330 pci_proc_detach_device(dev);
331 pci_remove_sysfs_dev_files(dev);
332 return;
333 }
Yinghai Lu58d9a382013-01-21 13:20:51 -0800334
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -0800335 dev->is_added = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600337EXPORT_SYMBOL_GPL(pci_bus_add_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/**
Yinghai Lu4f535092013-01-21 13:20:52 -0800340 * pci_bus_add_devices - start driver for PCI devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @bus: bus to check for new devices
342 *
Yinghai Lu4f535092013-01-21 13:20:52 -0800343 * Start driver for PCI devices and add some sysfs entries.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
akpm@linux-foundation.orgc48f1672009-02-03 15:45:26 -0800345void pci_bus_add_devices(const struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct pci_dev *dev;
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800348 struct pci_bus *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 list_for_each_entry(dev, &bus->devices, bus_list) {
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -0800351 /* Skip already-added devices */
352 if (dev->is_added)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 continue;
Yijing Wangc893d132014-05-30 11:01:03 +0800354 pci_bus_add_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
357 list_for_each_entry(dev, &bus->devices, bus_list) {
Lukas Wunner1e398ea2016-05-02 13:48:25 -0500358 /* Skip if device attach failed */
359 if (!dev->is_added)
360 continue;
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800361 child = dev->subordinate;
Jiang Liu981cf9e2013-04-12 05:44:16 +0000362 if (child)
363 pci_bus_add_devices(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600366EXPORT_SYMBOL(pci_bus_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Paul Mackerrascecf4862005-08-18 14:33:01 +1000368/** 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.
372 *
373 * Walk the given bus, including any bridged devices
374 * on buses under this bus. Call the provided callback
375 * on each device found.
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800376 *
377 * We check the return of @cb each time. If it returns anything
378 * other than 0, we break out.
379 *
Paul Mackerrascecf4862005-08-18 14:33:01 +1000380 */
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800381void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
Paul Mackerrascecf4862005-08-18 14:33:01 +1000382 void *userdata)
383{
384 struct pci_dev *dev;
385 struct pci_bus *bus;
386 struct list_head *next;
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800387 int retval;
Paul Mackerrascecf4862005-08-18 14:33:01 +1000388
389 bus = top;
Zhang Yanmind71374d2006-06-02 12:35:43 +0800390 down_read(&pci_bus_sem);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000391 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;
400 }
401 dev = list_entry(next, struct pci_dev, bus_list);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000402 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;
Paul Mackerrascecf4862005-08-18 14:33:01 +1000408
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800409 retval = cb(dev, userdata);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800410 if (retval)
411 break;
Paul Mackerrascecf4862005-08-18 14:33:01 +1000412 }
Zhang Yanmind71374d2006-06-02 12:35:43 +0800413 up_read(&pci_bus_sem);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000414}
Konrad Rzeszutek Wilk7c94def2009-12-22 14:49:45 -0500415EXPORT_SYMBOL_GPL(pci_walk_bus);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000416
Jiang Liufe830ef2013-05-25 21:48:29 +0800417struct pci_bus *pci_bus_get(struct pci_bus *bus)
418{
419 if (bus)
420 get_device(&bus->dev);
421 return bus;
422}
423EXPORT_SYMBOL(pci_bus_get);
424
425void pci_bus_put(struct pci_bus *bus)
426{
427 if (bus)
428 put_device(&bus->dev);
429}
430EXPORT_SYMBOL(pci_bus_put);