1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * From setup-res.c, by:
4  *	Dave Rusling (david.rusling@reo.mts.dec.com)
5  *	David Mosberger (davidm@cs.arizona.edu)
6  *	David Miller (davem@redhat.com)
7  *	Ivan Kokshaysky (ink@jurassic.park.msu.ru)
8  */
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>
14 #include <linux/of.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/proc_fs.h>
18 #include <linux/slab.h>
19 
20 #include "pci.h"
21 
pci_add_resource_offset(struct list_head * resources,struct resource * res,resource_size_t offset)22 void pci_add_resource_offset(struct list_head *resources, struct resource *res,
23 			     resource_size_t offset)
24 {
25 	struct resource_entry *entry;
26 
27 	entry = resource_list_create_entry(res, 0);
28 	if (!entry) {
29 		pr_err("PCI: can't add host bridge window %pR\n", res);
30 		return;
31 	}
32 
33 	entry->offset = offset;
34 	resource_list_add_tail(entry, resources);
35 }
36 EXPORT_SYMBOL(pci_add_resource_offset);
37 
pci_add_resource(struct list_head * resources,struct resource * res)38 void pci_add_resource(struct list_head *resources, struct resource *res)
39 {
40 	pci_add_resource_offset(resources, res, 0);
41 }
42 EXPORT_SYMBOL(pci_add_resource);
43 
pci_free_resource_list(struct list_head * resources)44 void pci_free_resource_list(struct list_head *resources)
45 {
46 	resource_list_free(resources);
47 }
48 EXPORT_SYMBOL(pci_free_resource_list);
49 
pci_bus_add_resource(struct pci_bus * bus,struct resource * res,unsigned int flags)50 void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
51 			  unsigned int flags)
52 {
53 	struct pci_bus_resource *bus_res;
54 
55 	bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
56 	if (!bus_res) {
57 		dev_err(&bus->dev, "can't add %pR resource\n", res);
58 		return;
59 	}
60 
61 	bus_res->res = res;
62 	bus_res->flags = flags;
63 	list_add_tail(&bus_res->list, &bus->resources);
64 }
65 
pci_bus_resource_n(const struct pci_bus * bus,int n)66 struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
67 {
68 	struct pci_bus_resource *bus_res;
69 
70 	if (n < PCI_BRIDGE_RESOURCE_NUM)
71 		return bus->resource[n];
72 
73 	n -= PCI_BRIDGE_RESOURCE_NUM;
74 	list_for_each_entry(bus_res, &bus->resources, list) {
75 		if (n-- == 0)
76 			return bus_res->res;
77 	}
78 	return NULL;
79 }
80 EXPORT_SYMBOL_GPL(pci_bus_resource_n);
81 
pci_bus_remove_resource(struct pci_bus * bus,struct resource * res)82 void pci_bus_remove_resource(struct pci_bus *bus, struct resource *res)
83 {
84 	struct pci_bus_resource *bus_res, *tmp;
85 	int i;
86 
87 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
88 		if (bus->resource[i] == res) {
89 			bus->resource[i] = NULL;
90 			return;
91 		}
92 	}
93 
94 	list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
95 		if (bus_res->res == res) {
96 			list_del(&bus_res->list);
97 			kfree(bus_res);
98 			return;
99 		}
100 	}
101 }
102 
pci_bus_remove_resources(struct pci_bus * bus)103 void pci_bus_remove_resources(struct pci_bus *bus)
104 {
105 	int i;
106 	struct pci_bus_resource *bus_res, *tmp;
107 
108 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
109 		bus->resource[i] = NULL;
110 
111 	list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
112 		list_del(&bus_res->list);
113 		kfree(bus_res);
114 	}
115 }
116 
devm_request_pci_bus_resources(struct device * dev,struct list_head * resources)117 int devm_request_pci_bus_resources(struct device *dev,
118 				   struct list_head *resources)
119 {
120 	struct resource_entry *win;
121 	struct resource *parent, *res;
122 	int err;
123 
124 	resource_list_for_each_entry(win, resources) {
125 		res = win->res;
126 		switch (resource_type(res)) {
127 		case IORESOURCE_IO:
128 			parent = &ioport_resource;
129 			break;
130 		case IORESOURCE_MEM:
131 			parent = &iomem_resource;
132 			break;
133 		default:
134 			continue;
135 		}
136 
137 		err = devm_request_resource(dev, parent, res);
138 		if (err)
139 			return err;
140 	}
141 
142 	return 0;
143 }
144 EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
145 
146 static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
147 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
148 static struct pci_bus_region pci_64_bit = {0,
149 				(pci_bus_addr_t) 0xffffffffffffffffULL};
150 static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
151 				(pci_bus_addr_t) 0xffffffffffffffffULL};
152 #endif
153 
154 /*
155  * @res contains CPU addresses.  Clip it so the corresponding bus addresses
156  * on @bus are entirely within @region.  This is used to control the bus
157  * addresses of resources we allocate, e.g., we may need a resource that
158  * can be mapped by a 32-bit BAR.
159  */
pci_clip_resource_to_region(struct pci_bus * bus,struct resource * res,struct pci_bus_region * region)160 static void pci_clip_resource_to_region(struct pci_bus *bus,
161 					struct resource *res,
162 					struct pci_bus_region *region)
163 {
164 	struct pci_bus_region r;
165 
166 	pcibios_resource_to_bus(bus, &r, res);
167 	if (r.start < region->start)
168 		r.start = region->start;
169 	if (r.end > region->end)
170 		r.end = region->end;
171 
172 	if (r.end < r.start)
173 		res->end = res->start - 1;
174 	else
175 		pcibios_bus_to_resource(bus, res, &r);
176 }
177 
pci_bus_alloc_from_region(struct pci_bus * bus,struct resource * res,resource_size_t size,resource_size_t align,resource_size_t min,unsigned long type_mask,resource_alignf alignf,void * alignf_data,struct pci_bus_region * region)178 static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
179 		resource_size_t size, resource_size_t align,
180 		resource_size_t min, unsigned long type_mask,
181 		resource_alignf alignf,
182 		void *alignf_data,
183 		struct pci_bus_region *region)
184 {
185 	struct resource *r, avail;
186 	resource_size_t max;
187 	int ret;
188 
189 	type_mask |= IORESOURCE_TYPE_BITS;
190 
191 	pci_bus_for_each_resource(bus, r) {
192 		resource_size_t min_used = min;
193 
194 		if (!r)
195 			continue;
196 
197 		/* type_mask must match */
198 		if ((res->flags ^ r->flags) & type_mask)
199 			continue;
200 
201 		/* We cannot allocate a non-prefetching resource
202 		   from a pre-fetching area */
203 		if ((r->flags & IORESOURCE_PREFETCH) &&
204 		    !(res->flags & IORESOURCE_PREFETCH))
205 			continue;
206 
207 		avail = *r;
208 		pci_clip_resource_to_region(bus, &avail, region);
209 
210 		/*
211 		 * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
212 		 * protect badly documented motherboard resources, but if
213 		 * this is an already-configured bridge window, its start
214 		 * overrides "min".
215 		 */
216 		if (avail.start)
217 			min_used = avail.start;
218 
219 		max = avail.end;
220 
221 		/* Don't bother if available space isn't large enough */
222 		if (size > max - min_used + 1)
223 			continue;
224 
225 		/* Ok, try it out.. */
226 		ret = allocate_resource(r, res, size, min_used, max,
227 					align, alignf, alignf_data);
228 		if (ret == 0)
229 			return 0;
230 	}
231 	return -ENOMEM;
232 }
233 
234 /**
235  * pci_bus_alloc_resource - allocate a resource from a parent bus
236  * @bus: PCI bus
237  * @res: resource to allocate
238  * @size: size of resource to allocate
239  * @align: alignment of resource to allocate
240  * @min: minimum /proc/iomem address to allocate
241  * @type_mask: IORESOURCE_* type flags
242  * @alignf: resource alignment function
243  * @alignf_data: data argument for resource alignment function
244  *
245  * Given the PCI bus a device resides on, the size, minimum address,
246  * alignment and type, try to find an acceptable resource allocation
247  * for a specific device resource.
248  */
pci_bus_alloc_resource(struct pci_bus * bus,struct resource * res,resource_size_t size,resource_size_t align,resource_size_t min,unsigned long type_mask,resource_alignf alignf,void * alignf_data)249 int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
250 		resource_size_t size, resource_size_t align,
251 		resource_size_t min, unsigned long type_mask,
252 		resource_alignf alignf,
253 		void *alignf_data)
254 {
255 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
256 	int rc;
257 
258 	if (res->flags & IORESOURCE_MEM_64) {
259 		rc = pci_bus_alloc_from_region(bus, res, size, align, min,
260 					       type_mask, alignf, alignf_data,
261 					       &pci_high);
262 		if (rc == 0)
263 			return 0;
264 
265 		return pci_bus_alloc_from_region(bus, res, size, align, min,
266 						 type_mask, alignf, alignf_data,
267 						 &pci_64_bit);
268 	}
269 #endif
270 
271 	return pci_bus_alloc_from_region(bus, res, size, align, min,
272 					 type_mask, alignf, alignf_data,
273 					 &pci_32_bit);
274 }
275 EXPORT_SYMBOL(pci_bus_alloc_resource);
276 
277 /*
278  * The @idx resource of @dev should be a PCI-PCI bridge window.  If this
279  * resource fits inside a window of an upstream bridge, do nothing.  If it
280  * overlaps an upstream window but extends outside it, clip the resource so
281  * it fits completely inside.
282  */
pci_bus_clip_resource(struct pci_dev * dev,int idx)283 bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
284 {
285 	struct pci_bus *bus = dev->bus;
286 	struct resource *res = &dev->resource[idx];
287 	struct resource orig_res = *res;
288 	struct resource *r;
289 
290 	pci_bus_for_each_resource(bus, r) {
291 		resource_size_t start, end;
292 
293 		if (!r)
294 			continue;
295 
296 		if (resource_type(res) != resource_type(r))
297 			continue;
298 
299 		start = max(r->start, res->start);
300 		end = min(r->end, res->end);
301 
302 		if (start > end)
303 			continue;	/* no overlap */
304 
305 		if (res->start == start && res->end == end)
306 			return false;	/* no change */
307 
308 		res->start = start;
309 		res->end = end;
310 		res->flags &= ~IORESOURCE_UNSET;
311 		orig_res.flags &= ~IORESOURCE_UNSET;
312 		pci_info(dev, "%pR clipped to %pR\n", &orig_res, res);
313 
314 		return true;
315 	}
316 
317 	return false;
318 }
319 
pcibios_resource_survey_bus(struct pci_bus * bus)320 void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
321 
pcibios_bus_add_device(struct pci_dev * pdev)322 void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
323 
324 /**
325  * pci_bus_add_device - start driver for a single device
326  * @dev: device to add
327  *
328  * This adds add sysfs entries and start device drivers
329  */
pci_bus_add_device(struct pci_dev * dev)330 void pci_bus_add_device(struct pci_dev *dev)
331 {
332 	struct device_node *dn = dev->dev.of_node;
333 	struct platform_device *pdev;
334 	int retval;
335 
336 	/*
337 	 * Can not put in pci_device_add yet because resources
338 	 * are not assigned yet for some devices.
339 	 */
340 	pcibios_bus_add_device(dev);
341 	pci_fixup_device(pci_fixup_final, dev);
342 	if (pci_is_bridge(dev))
343 		of_pci_make_dev_node(dev);
344 	pci_create_sysfs_dev_files(dev);
345 	pci_proc_attach_device(dev);
346 	pci_bridge_d3_update(dev);
347 
348 	/*
349 	 * If the PCI device is associated with a pwrctrl device with a
350 	 * power supply, create a device link between the PCI device and
351 	 * pwrctrl device.  This ensures that pwrctrl drivers are probed
352 	 * before PCI client drivers.
353 	 */
354 	pdev = of_find_device_by_node(dn);
355 	if (pdev && of_pci_supply_present(dn)) {
356 		if (!device_link_add(&dev->dev, &pdev->dev,
357 				     DL_FLAG_AUTOREMOVE_CONSUMER))
358 			pci_err(dev, "failed to add device link to power control device %s\n",
359 				pdev->name);
360 	}
361 
362 	dev->match_driver = !dn || of_device_is_available(dn);
363 	retval = device_attach(&dev->dev);
364 	if (retval < 0 && retval != -EPROBE_DEFER)
365 		pci_warn(dev, "device attach failed (%d)\n", retval);
366 
367 	pci_dev_assign_added(dev, true);
368 }
369 EXPORT_SYMBOL_GPL(pci_bus_add_device);
370 
371 /**
372  * pci_bus_add_devices - start driver for PCI devices
373  * @bus: bus to check for new devices
374  *
375  * Start driver for PCI devices and add some sysfs entries.
376  */
pci_bus_add_devices(const struct pci_bus * bus)377 void pci_bus_add_devices(const struct pci_bus *bus)
378 {
379 	struct pci_dev *dev;
380 	struct pci_bus *child;
381 
382 	list_for_each_entry(dev, &bus->devices, bus_list) {
383 		/* Skip already-added devices */
384 		if (pci_dev_is_added(dev))
385 			continue;
386 		pci_bus_add_device(dev);
387 	}
388 
389 	list_for_each_entry(dev, &bus->devices, bus_list) {
390 		/* Skip if device attach failed */
391 		if (!pci_dev_is_added(dev))
392 			continue;
393 		child = dev->subordinate;
394 		if (child)
395 			pci_bus_add_devices(child);
396 	}
397 }
398 EXPORT_SYMBOL(pci_bus_add_devices);
399 
__pci_walk_bus(struct pci_bus * top,int (* cb)(struct pci_dev *,void *),void * userdata,bool locked)400 static void __pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
401 			   void *userdata, bool locked)
402 {
403 	struct pci_dev *dev;
404 	struct pci_bus *bus;
405 	struct list_head *next;
406 	int retval;
407 
408 	bus = top;
409 	if (!locked)
410 		down_read(&pci_bus_sem);
411 	next = top->devices.next;
412 	for (;;) {
413 		if (next == &bus->devices) {
414 			/* end of this bus, go up or finish */
415 			if (bus == top)
416 				break;
417 			next = bus->self->bus_list.next;
418 			bus = bus->self->bus;
419 			continue;
420 		}
421 		dev = list_entry(next, struct pci_dev, bus_list);
422 		if (dev->subordinate) {
423 			/* this is a pci-pci bridge, do its devices next */
424 			next = dev->subordinate->devices.next;
425 			bus = dev->subordinate;
426 		} else
427 			next = dev->bus_list.next;
428 
429 		retval = cb(dev, userdata);
430 		if (retval)
431 			break;
432 	}
433 	if (!locked)
434 		up_read(&pci_bus_sem);
435 }
436 
437 /**
438  *  pci_walk_bus - walk devices on/under bus, calling callback.
439  *  @top: bus whose devices should be walked
440  *  @cb: callback to be called for each device found
441  *  @userdata: arbitrary pointer to be passed to callback
442  *
443  *  Walk the given bus, including any bridged devices
444  *  on buses under this bus.  Call the provided callback
445  *  on each device found.
446  *
447  *  We check the return of @cb each time. If it returns anything
448  *  other than 0, we break out.
449  */
pci_walk_bus(struct pci_bus * top,int (* cb)(struct pci_dev *,void *),void * userdata)450 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata)
451 {
452 	__pci_walk_bus(top, cb, userdata, false);
453 }
454 EXPORT_SYMBOL_GPL(pci_walk_bus);
455 
pci_walk_bus_locked(struct pci_bus * top,int (* cb)(struct pci_dev *,void *),void * userdata)456 void pci_walk_bus_locked(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata)
457 {
458 	lockdep_assert_held(&pci_bus_sem);
459 
460 	__pci_walk_bus(top, cb, userdata, true);
461 }
462 EXPORT_SYMBOL_GPL(pci_walk_bus_locked);
463 
pci_bus_get(struct pci_bus * bus)464 struct pci_bus *pci_bus_get(struct pci_bus *bus)
465 {
466 	if (bus)
467 		get_device(&bus->dev);
468 	return bus;
469 }
470 
pci_bus_put(struct pci_bus * bus)471 void pci_bus_put(struct pci_bus *bus)
472 {
473 	if (bus)
474 		put_device(&bus->dev);
475 }
476