xref: /openbmc/linux/drivers/pci/bus.c (revision 82e6fdd6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
17 #include <linux/slab.h>
18 
19 #include "pci.h"
20 
21 void pci_add_resource_offset(struct list_head *resources, struct resource *res,
22 			     resource_size_t offset)
23 {
24 	struct resource_entry *entry;
25 
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;
30 	}
31 
32 	entry->offset = offset;
33 	resource_list_add_tail(entry, resources);
34 }
35 EXPORT_SYMBOL(pci_add_resource_offset);
36 
37 void pci_add_resource(struct list_head *resources, struct resource *res)
38 {
39 	pci_add_resource_offset(resources, res, 0);
40 }
41 EXPORT_SYMBOL(pci_add_resource);
42 
43 void pci_free_resource_list(struct list_head *resources)
44 {
45 	resource_list_free(resources);
46 }
47 EXPORT_SYMBOL(pci_free_resource_list);
48 
49 void 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 
65 struct 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 }
79 EXPORT_SYMBOL_GPL(pci_bus_resource_n);
80 
81 void pci_bus_remove_resources(struct pci_bus *bus)
82 {
83 	int i;
84 	struct pci_bus_resource *bus_res, *tmp;
85 
86 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
87 		bus->resource[i] = NULL;
88 
89 	list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
90 		list_del(&bus_res->list);
91 		kfree(bus_res);
92 	}
93 }
94 
95 int 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 }
122 EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
123 
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
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.
137  */
138 static 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 
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)
165 {
166 	int i, ret;
167 	struct resource *r, avail;
168 	resource_size_t max;
169 
170 	type_mask |= IORESOURCE_TYPE_BITS;
171 
172 	pci_bus_for_each_resource(bus, r, i) {
173 		resource_size_t min_used = min;
174 
175 		if (!r)
176 			continue;
177 
178 		/* 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 
188 		avail = *r;
189 		pci_clip_resource_to_region(bus, &avail, region);
190 
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)
198 			min_used = avail.start;
199 
200 		max = avail.end;
201 
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;
207 	}
208 	return -ENOMEM;
209 }
210 
211 /**
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  */
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)
234 {
235 #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
236 	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 
245 		return pci_bus_alloc_from_region(bus, res, size, align, min,
246 						 type_mask, alignf, alignf_data,
247 						 &pci_64_bit);
248 	}
249 #endif
250 
251 	return pci_bus_alloc_from_region(bus, res, size, align, min,
252 					 type_mask, alignf, alignf_data,
253 					 &pci_32_bit);
254 }
255 EXPORT_SYMBOL(pci_bus_alloc_resource);
256 
257 /*
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  */
263 bool 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;
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);
295 
296 		return true;
297 	}
298 
299 	return false;
300 }
301 
302 void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
303 
304 void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
305 
306 /**
307  * pci_bus_add_device - start driver for a single device
308  * @dev: device to add
309  *
310  * This adds add sysfs entries and start device drivers
311  */
312 void pci_bus_add_device(struct pci_dev *dev)
313 {
314 	int retval;
315 
316 	/*
317 	 * Can not put in pci_device_add yet because resources
318 	 * are not assigned yet for some devices.
319 	 */
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);
325 
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;
333 	}
334 
335 	dev->is_added = 1;
336 }
337 EXPORT_SYMBOL_GPL(pci_bus_add_device);
338 
339 /**
340  * pci_bus_add_devices - start driver for PCI devices
341  * @bus: bus to check for new devices
342  *
343  * Start driver for PCI devices and add some sysfs entries.
344  */
345 void pci_bus_add_devices(const struct pci_bus *bus)
346 {
347 	struct pci_dev *dev;
348 	struct pci_bus *child;
349 
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);
355 	}
356 
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);
364 	}
365 }
366 EXPORT_SYMBOL(pci_bus_add_devices);
367 
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.
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.
376  *
377  *  We check the return of @cb each time. If it returns anything
378  *  other than 0, we break out.
379  *
380  */
381 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
382 		  void *userdata)
383 {
384 	struct pci_dev *dev;
385 	struct pci_bus *bus;
386 	struct list_head *next;
387 	int retval;
388 
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;
400 		}
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;
408 
409 		retval = cb(dev, userdata);
410 		if (retval)
411 			break;
412 	}
413 	up_read(&pci_bus_sem);
414 }
415 EXPORT_SYMBOL_GPL(pci_walk_bus);
416 
417 struct pci_bus *pci_bus_get(struct pci_bus *bus)
418 {
419 	if (bus)
420 		get_device(&bus->dev);
421 	return bus;
422 }
423 EXPORT_SYMBOL(pci_bus_get);
424 
425 void pci_bus_put(struct pci_bus *bus)
426 {
427 	if (bus)
428 		put_device(&bus->dev);
429 }
430 EXPORT_SYMBOL(pci_bus_put);
431