1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCI detection and setup code
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/pci.h>
10 #include <linux/msi.h>
11 #include <linux/of_pci.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/pci_hotplug.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/cpumask.h>
18 #include <linux/aer.h>
19 #include <linux/acpi.h>
20 #include <linux/hypervisor.h>
21 #include <linux/irqdomain.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/bitfield.h>
24 #include "pci.h"
25 
26 #define CARDBUS_LATENCY_TIMER	176	/* secondary latency timer */
27 #define CARDBUS_RESERVE_BUSNR	3
28 
29 static struct resource busn_resource = {
30 	.name	= "PCI busn",
31 	.start	= 0,
32 	.end	= 255,
33 	.flags	= IORESOURCE_BUS,
34 };
35 
36 /* Ugh.  Need to stop exporting this to modules. */
37 LIST_HEAD(pci_root_buses);
38 EXPORT_SYMBOL(pci_root_buses);
39 
40 static LIST_HEAD(pci_domain_busn_res_list);
41 
42 struct pci_domain_busn_res {
43 	struct list_head list;
44 	struct resource res;
45 	int domain_nr;
46 };
47 
get_pci_domain_busn_res(int domain_nr)48 static struct resource *get_pci_domain_busn_res(int domain_nr)
49 {
50 	struct pci_domain_busn_res *r;
51 
52 	list_for_each_entry(r, &pci_domain_busn_res_list, list)
53 		if (r->domain_nr == domain_nr)
54 			return &r->res;
55 
56 	r = kzalloc(sizeof(*r), GFP_KERNEL);
57 	if (!r)
58 		return NULL;
59 
60 	r->domain_nr = domain_nr;
61 	r->res.start = 0;
62 	r->res.end = 0xff;
63 	r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
64 
65 	list_add_tail(&r->list, &pci_domain_busn_res_list);
66 
67 	return &r->res;
68 }
69 
70 /*
71  * Some device drivers need know if PCI is initiated.
72  * Basically, we think PCI is not initiated when there
73  * is no device to be found on the pci_bus_type.
74  */
no_pci_devices(void)75 int no_pci_devices(void)
76 {
77 	struct device *dev;
78 	int no_devices;
79 
80 	dev = bus_find_next_device(&pci_bus_type, NULL);
81 	no_devices = (dev == NULL);
82 	put_device(dev);
83 	return no_devices;
84 }
85 EXPORT_SYMBOL(no_pci_devices);
86 
87 /*
88  * PCI Bus Class
89  */
release_pcibus_dev(struct device * dev)90 static void release_pcibus_dev(struct device *dev)
91 {
92 	struct pci_bus *pci_bus = to_pci_bus(dev);
93 
94 	put_device(pci_bus->bridge);
95 	pci_bus_remove_resources(pci_bus);
96 	pci_release_bus_of_node(pci_bus);
97 	kfree(pci_bus);
98 }
99 
100 static const struct class pcibus_class = {
101 	.name		= "pci_bus",
102 	.dev_release	= &release_pcibus_dev,
103 	.dev_groups	= pcibus_groups,
104 };
105 
pcibus_class_init(void)106 static int __init pcibus_class_init(void)
107 {
108 	return class_register(&pcibus_class);
109 }
110 postcore_initcall(pcibus_class_init);
111 
pci_size(u64 base,u64 maxbase,u64 mask)112 static u64 pci_size(u64 base, u64 maxbase, u64 mask)
113 {
114 	u64 size = mask & maxbase;	/* Find the significant bits */
115 	if (!size)
116 		return 0;
117 
118 	/*
119 	 * Get the lowest of them to find the decode size, and from that
120 	 * the extent.
121 	 */
122 	size = size & ~(size-1);
123 
124 	/*
125 	 * base == maxbase can be valid only if the BAR has already been
126 	 * programmed with all 1s.
127 	 */
128 	if (base == maxbase && ((base | (size - 1)) & mask) != mask)
129 		return 0;
130 
131 	return size;
132 }
133 
decode_bar(struct pci_dev * dev,u32 bar)134 static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
135 {
136 	u32 mem_type;
137 	unsigned long flags;
138 
139 	if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
140 		flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
141 		flags |= IORESOURCE_IO;
142 		return flags;
143 	}
144 
145 	flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
146 	flags |= IORESOURCE_MEM;
147 	if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
148 		flags |= IORESOURCE_PREFETCH;
149 
150 	mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
151 	switch (mem_type) {
152 	case PCI_BASE_ADDRESS_MEM_TYPE_32:
153 		break;
154 	case PCI_BASE_ADDRESS_MEM_TYPE_1M:
155 		/* 1M mem BAR treated as 32-bit BAR */
156 		break;
157 	case PCI_BASE_ADDRESS_MEM_TYPE_64:
158 		flags |= IORESOURCE_MEM_64;
159 		break;
160 	default:
161 		/* mem unknown type treated as 32-bit BAR */
162 		break;
163 	}
164 	return flags;
165 }
166 
167 #define PCI_COMMAND_DECODE_ENABLE	(PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
168 
169 /**
170  * __pci_read_base - Read a PCI BAR
171  * @dev: the PCI device
172  * @type: type of the BAR
173  * @res: resource buffer to be filled in
174  * @pos: BAR position in the config space
175  *
176  * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
177  */
__pci_read_base(struct pci_dev * dev,enum pci_bar_type type,struct resource * res,unsigned int pos)178 int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
179 		    struct resource *res, unsigned int pos)
180 {
181 	u32 l = 0, sz = 0, mask;
182 	u64 l64, sz64, mask64;
183 	u16 orig_cmd;
184 	struct pci_bus_region region, inverted_region;
185 	const char *res_name = pci_resource_name(dev, res - dev->resource);
186 
187 	mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
188 
189 	/* No printks while decoding is disabled! */
190 	if (!dev->mmio_always_on) {
191 		pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
192 		if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
193 			pci_write_config_word(dev, PCI_COMMAND,
194 				orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
195 		}
196 	}
197 
198 	res->name = pci_name(dev);
199 
200 	pci_read_config_dword(dev, pos, &l);
201 	pci_write_config_dword(dev, pos, l | mask);
202 	pci_read_config_dword(dev, pos, &sz);
203 	pci_write_config_dword(dev, pos, l);
204 
205 	/*
206 	 * All bits set in sz means the device isn't working properly.
207 	 * If the BAR isn't implemented, all bits must be 0.  If it's a
208 	 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
209 	 * 1 must be clear.
210 	 */
211 	if (PCI_POSSIBLE_ERROR(sz))
212 		sz = 0;
213 
214 	/*
215 	 * I don't know how l can have all bits set.  Copied from old code.
216 	 * Maybe it fixes a bug on some ancient platform.
217 	 */
218 	if (PCI_POSSIBLE_ERROR(l))
219 		l = 0;
220 
221 	if (type == pci_bar_unknown) {
222 		res->flags = decode_bar(dev, l);
223 		res->flags |= IORESOURCE_SIZEALIGN;
224 		if (res->flags & IORESOURCE_IO) {
225 			l64 = l & PCI_BASE_ADDRESS_IO_MASK;
226 			sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
227 			mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
228 		} else {
229 			l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
230 			sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
231 			mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
232 		}
233 	} else {
234 		if (l & PCI_ROM_ADDRESS_ENABLE)
235 			res->flags |= IORESOURCE_ROM_ENABLE;
236 		l64 = l & PCI_ROM_ADDRESS_MASK;
237 		sz64 = sz & PCI_ROM_ADDRESS_MASK;
238 		mask64 = PCI_ROM_ADDRESS_MASK;
239 	}
240 
241 	if (res->flags & IORESOURCE_MEM_64) {
242 		pci_read_config_dword(dev, pos + 4, &l);
243 		pci_write_config_dword(dev, pos + 4, ~0);
244 		pci_read_config_dword(dev, pos + 4, &sz);
245 		pci_write_config_dword(dev, pos + 4, l);
246 
247 		l64 |= ((u64)l << 32);
248 		sz64 |= ((u64)sz << 32);
249 		mask64 |= ((u64)~0 << 32);
250 	}
251 
252 	if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
253 		pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
254 
255 	if (!sz64)
256 		goto fail;
257 
258 	sz64 = pci_size(l64, sz64, mask64);
259 	if (!sz64) {
260 		pci_info(dev, FW_BUG "%s: invalid; can't size\n", res_name);
261 		goto fail;
262 	}
263 
264 	if (res->flags & IORESOURCE_MEM_64) {
265 		if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
266 		    && sz64 > 0x100000000ULL) {
267 			res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
268 			res->start = 0;
269 			res->end = 0;
270 			pci_err(dev, "%s: can't handle BAR larger than 4GB (size %#010llx)\n",
271 				res_name, (unsigned long long)sz64);
272 			goto out;
273 		}
274 
275 		if ((sizeof(pci_bus_addr_t) < 8) && l) {
276 			/* Above 32-bit boundary; try to reallocate */
277 			res->flags |= IORESOURCE_UNSET;
278 			res->start = 0;
279 			res->end = sz64 - 1;
280 			pci_info(dev, "%s: can't handle BAR above 4GB (bus address %#010llx)\n",
281 				 res_name, (unsigned long long)l64);
282 			goto out;
283 		}
284 	}
285 
286 	region.start = l64;
287 	region.end = l64 + sz64 - 1;
288 
289 	pcibios_bus_to_resource(dev->bus, res, ®ion);
290 	pcibios_resource_to_bus(dev->bus, &inverted_region, res);
291 
292 	/*
293 	 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
294 	 * the corresponding resource address (the physical address used by
295 	 * the CPU.  Converting that resource address back to a bus address
296 	 * should yield the original BAR value:
297 	 *
298 	 *     resource_to_bus(bus_to_resource(A)) == A
299 	 *
300 	 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
301 	 * be claimed by the device.
302 	 */
303 	if (inverted_region.start != region.start) {
304 		res->flags |= IORESOURCE_UNSET;
305 		res->start = 0;
306 		res->end = region.end - region.start;
307 		pci_info(dev, "%s: initial BAR value %#010llx invalid\n",
308 			 res_name, (unsigned long long)region.start);
309 	}
310 
311 	goto out;
312 
313 
314 fail:
315 	res->flags = 0;
316 out:
317 	if (res->flags)
318 		pci_info(dev, "%s %pR\n", res_name, res);
319 
320 	return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
321 }
322 
pci_read_bases(struct pci_dev * dev,unsigned int howmany,int rom)323 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
324 {
325 	unsigned int pos, reg;
326 
327 	if (dev->non_compliant_bars)
328 		return;
329 
330 	/* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */
331 	if (dev->is_virtfn)
332 		return;
333 
334 	for (pos = 0; pos < howmany; pos++) {
335 		struct resource *res = &dev->resource[pos];
336 		reg = PCI_BASE_ADDRESS_0 + (pos << 2);
337 		pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
338 	}
339 
340 	if (rom) {
341 		struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
342 		dev->rom_base_reg = rom;
343 		res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
344 				IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
345 		__pci_read_base(dev, pci_bar_mem32, res, rom);
346 	}
347 }
348 
pci_read_bridge_io(struct pci_dev * dev,struct resource * res,bool log)349 static void pci_read_bridge_io(struct pci_dev *dev, struct resource *res,
350 			       bool log)
351 {
352 	u8 io_base_lo, io_limit_lo;
353 	unsigned long io_mask, io_granularity, base, limit;
354 	struct pci_bus_region region;
355 
356 	io_mask = PCI_IO_RANGE_MASK;
357 	io_granularity = 0x1000;
358 	if (dev->io_window_1k) {
359 		/* Support 1K I/O space granularity */
360 		io_mask = PCI_IO_1K_RANGE_MASK;
361 		io_granularity = 0x400;
362 	}
363 
364 	pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
365 	pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
366 	base = (io_base_lo & io_mask) << 8;
367 	limit = (io_limit_lo & io_mask) << 8;
368 
369 	if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
370 		u16 io_base_hi, io_limit_hi;
371 
372 		pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
373 		pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
374 		base |= ((unsigned long) io_base_hi << 16);
375 		limit |= ((unsigned long) io_limit_hi << 16);
376 	}
377 
378 	if (base <= limit) {
379 		res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
380 		region.start = base;
381 		region.end = limit + io_granularity - 1;
382 		pcibios_bus_to_resource(dev->bus, res, ®ion);
383 		if (log)
384 			pci_info(dev, "  bridge window %pR\n", res);
385 	}
386 }
387 
pci_read_bridge_mmio(struct pci_dev * dev,struct resource * res,bool log)388 static void pci_read_bridge_mmio(struct pci_dev *dev, struct resource *res,
389 				 bool log)
390 {
391 	u16 mem_base_lo, mem_limit_lo;
392 	unsigned long base, limit;
393 	struct pci_bus_region region;
394 
395 	pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
396 	pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
397 	base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
398 	limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
399 	if (base <= limit) {
400 		res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
401 		region.start = base;
402 		region.end = limit + 0xfffff;
403 		pcibios_bus_to_resource(dev->bus, res, ®ion);
404 		if (log)
405 			pci_info(dev, "  bridge window %pR\n", res);
406 	}
407 }
408 
pci_read_bridge_mmio_pref(struct pci_dev * dev,struct resource * res,bool log)409 static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res,
410 				      bool log)
411 {
412 	u16 mem_base_lo, mem_limit_lo;
413 	u64 base64, limit64;
414 	pci_bus_addr_t base, limit;
415 	struct pci_bus_region region;
416 
417 	pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
418 	pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
419 	base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
420 	limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
421 
422 	if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
423 		u32 mem_base_hi, mem_limit_hi;
424 
425 		pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
426 		pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
427 
428 		/*
429 		 * Some bridges set the base > limit by default, and some
430 		 * (broken) BIOSes do not initialize them.  If we find
431 		 * this, just assume they are not being used.
432 		 */
433 		if (mem_base_hi <= mem_limit_hi) {
434 			base64 |= (u64) mem_base_hi << 32;
435 			limit64 |= (u64) mem_limit_hi << 32;
436 		}
437 	}
438 
439 	base = (pci_bus_addr_t) base64;
440 	limit = (pci_bus_addr_t) limit64;
441 
442 	if (base != base64) {
443 		pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
444 			(unsigned long long) base64);
445 		return;
446 	}
447 
448 	if (base <= limit) {
449 		res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
450 					 IORESOURCE_MEM | IORESOURCE_PREFETCH;
451 		if (res->flags & PCI_PREF_RANGE_TYPE_64)
452 			res->flags |= IORESOURCE_MEM_64;
453 		region.start = base;
454 		region.end = limit + 0xfffff;
455 		pcibios_bus_to_resource(dev->bus, res, ®ion);
456 		if (log)
457 			pci_info(dev, "  bridge window %pR\n", res);
458 	}
459 }
460 
pci_read_bridge_windows(struct pci_dev * bridge)461 static void pci_read_bridge_windows(struct pci_dev *bridge)
462 {
463 	u32 buses;
464 	u16 io;
465 	u32 pmem, tmp;
466 	struct resource res;
467 
468 	pci_read_config_dword(bridge, PCI_PRIMARY_BUS, &buses);
469 	res.flags = IORESOURCE_BUS;
470 	res.start = (buses >> 8) & 0xff;
471 	res.end = (buses >> 16) & 0xff;
472 	pci_info(bridge, "PCI bridge to %pR%s\n", &res,
473 		 bridge->transparent ? " (subtractive decode)" : "");
474 
475 	pci_read_config_word(bridge, PCI_IO_BASE, &io);
476 	if (!io) {
477 		pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
478 		pci_read_config_word(bridge, PCI_IO_BASE, &io);
479 		pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
480 	}
481 	if (io) {
482 		bridge->io_window = 1;
483 		pci_read_bridge_io(bridge, &res, true);
484 	}
485 
486 	pci_read_bridge_mmio(bridge, &res, true);
487 
488 	/*
489 	 * DECchip 21050 pass 2 errata: the bridge may miss an address
490 	 * disconnect boundary by one PCI data phase.  Workaround: do not
491 	 * use prefetching on this device.
492 	 */
493 	if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
494 		return;
495 
496 	pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
497 	if (!pmem) {
498 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
499 					       0xffe0fff0);
500 		pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
501 		pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
502 	}
503 	if (!pmem)
504 		return;
505 
506 	bridge->pref_window = 1;
507 
508 	if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
509 
510 		/*
511 		 * Bridge claims to have a 64-bit prefetchable memory
512 		 * window; verify that the upper bits are actually
513 		 * writable.
514 		 */
515 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &pmem);
516 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
517 				       0xffffffff);
518 		pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
519 		pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, pmem);
520 		if (tmp)
521 			bridge->pref_64_window = 1;
522 	}
523 
524 	pci_read_bridge_mmio_pref(bridge, &res, true);
525 }
526 
pci_read_bridge_bases(struct pci_bus * child)527 void pci_read_bridge_bases(struct pci_bus *child)
528 {
529 	struct pci_dev *dev = child->self;
530 	struct resource *res;
531 	int i;
532 
533 	if (pci_is_root_bus(child))	/* It's a host bus, nothing to read */
534 		return;
535 
536 	pci_info(dev, "PCI bridge to %pR%s\n",
537 		 &child->busn_res,
538 		 dev->transparent ? " (subtractive decode)" : "");
539 
540 	pci_bus_remove_resources(child);
541 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
542 		child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
543 
544 	pci_read_bridge_io(child->self, child->resource[0], false);
545 	pci_read_bridge_mmio(child->self, child->resource[1], false);
546 	pci_read_bridge_mmio_pref(child->self, child->resource[2], false);
547 
548 	if (dev->transparent) {
549 		pci_bus_for_each_resource(child->parent, res) {
550 			if (res && res->flags) {
551 				pci_bus_add_resource(child, res,
552 						     PCI_SUBTRACTIVE_DECODE);
553 				pci_info(dev, "  bridge window %pR (subtractive decode)\n",
554 					   res);
555 			}
556 		}
557 	}
558 }
559 
pci_alloc_bus(struct pci_bus * parent)560 static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
561 {
562 	struct pci_bus *b;
563 
564 	b = kzalloc(sizeof(*b), GFP_KERNEL);
565 	if (!b)
566 		return NULL;
567 
568 	INIT_LIST_HEAD(&b->node);
569 	INIT_LIST_HEAD(&b->children);
570 	INIT_LIST_HEAD(&b->devices);
571 	INIT_LIST_HEAD(&b->slots);
572 	INIT_LIST_HEAD(&b->resources);
573 	b->max_bus_speed = PCI_SPEED_UNKNOWN;
574 	b->cur_bus_speed = PCI_SPEED_UNKNOWN;
575 #ifdef CONFIG_PCI_DOMAINS_GENERIC
576 	if (parent)
577 		b->domain_nr = parent->domain_nr;
578 #endif
579 	return b;
580 }
581 
pci_release_host_bridge_dev(struct device * dev)582 static void pci_release_host_bridge_dev(struct device *dev)
583 {
584 	struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
585 
586 	if (bridge->release_fn)
587 		bridge->release_fn(bridge);
588 
589 	pci_free_resource_list(&bridge->windows);
590 	pci_free_resource_list(&bridge->dma_ranges);
591 	kfree(bridge);
592 }
593 
pci_init_host_bridge(struct pci_host_bridge * bridge)594 static void pci_init_host_bridge(struct pci_host_bridge *bridge)
595 {
596 	INIT_LIST_HEAD(&bridge->windows);
597 	INIT_LIST_HEAD(&bridge->dma_ranges);
598 
599 	/*
600 	 * We assume we can manage these PCIe features.  Some systems may
601 	 * reserve these for use by the platform itself, e.g., an ACPI BIOS
602 	 * may implement its own AER handling and use _OSC to prevent the
603 	 * OS from interfering.
604 	 */
605 	bridge->native_aer = 1;
606 	bridge->native_pcie_hotplug = 1;
607 	bridge->native_shpc_hotplug = 1;
608 	bridge->native_pme = 1;
609 	bridge->native_ltr = 1;
610 	bridge->native_dpc = 1;
611 	bridge->domain_nr = PCI_DOMAIN_NR_NOT_SET;
612 	bridge->native_cxl_error = 1;
613 
614 	device_initialize(&bridge->dev);
615 }
616 
pci_alloc_host_bridge(size_t priv)617 struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
618 {
619 	struct pci_host_bridge *bridge;
620 
621 	bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
622 	if (!bridge)
623 		return NULL;
624 
625 	pci_init_host_bridge(bridge);
626 	bridge->dev.release = pci_release_host_bridge_dev;
627 
628 	return bridge;
629 }
630 EXPORT_SYMBOL(pci_alloc_host_bridge);
631 
devm_pci_alloc_host_bridge_release(void * data)632 static void devm_pci_alloc_host_bridge_release(void *data)
633 {
634 	pci_free_host_bridge(data);
635 }
636 
devm_pci_alloc_host_bridge(struct device * dev,size_t priv)637 struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
638 						   size_t priv)
639 {
640 	int ret;
641 	struct pci_host_bridge *bridge;
642 
643 	bridge = pci_alloc_host_bridge(priv);
644 	if (!bridge)
645 		return NULL;
646 
647 	bridge->dev.parent = dev;
648 
649 	ret = devm_add_action_or_reset(dev, devm_pci_alloc_host_bridge_release,
650 				       bridge);
651 	if (ret)
652 		return NULL;
653 
654 	ret = devm_of_pci_bridge_init(dev, bridge);
655 	if (ret)
656 		return NULL;
657 
658 	return bridge;
659 }
660 EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
661 
pci_free_host_bridge(struct pci_host_bridge * bridge)662 void pci_free_host_bridge(struct pci_host_bridge *bridge)
663 {
664 	put_device(&bridge->dev);
665 }
666 EXPORT_SYMBOL(pci_free_host_bridge);
667 
668 /* Indexed by PCI_X_SSTATUS_FREQ (secondary bus mode and frequency) */
669 static const unsigned char pcix_bus_speed[] = {
670 	PCI_SPEED_UNKNOWN,		/* 0 */
671 	PCI_SPEED_66MHz_PCIX,		/* 1 */
672 	PCI_SPEED_100MHz_PCIX,		/* 2 */
673 	PCI_SPEED_133MHz_PCIX,		/* 3 */
674 	PCI_SPEED_UNKNOWN,		/* 4 */
675 	PCI_SPEED_66MHz_PCIX_ECC,	/* 5 */
676 	PCI_SPEED_100MHz_PCIX_ECC,	/* 6 */
677 	PCI_SPEED_133MHz_PCIX_ECC,	/* 7 */
678 	PCI_SPEED_UNKNOWN,		/* 8 */
679 	PCI_SPEED_66MHz_PCIX_266,	/* 9 */
680 	PCI_SPEED_100MHz_PCIX_266,	/* A */
681 	PCI_SPEED_133MHz_PCIX_266,	/* B */
682 	PCI_SPEED_UNKNOWN,		/* C */
683 	PCI_SPEED_66MHz_PCIX_533,	/* D */
684 	PCI_SPEED_100MHz_PCIX_533,	/* E */
685 	PCI_SPEED_133MHz_PCIX_533	/* F */
686 };
687 
688 /* Indexed by PCI_EXP_LNKCAP_SLS, PCI_EXP_LNKSTA_CLS */
689 const unsigned char pcie_link_speed[] = {
690 	PCI_SPEED_UNKNOWN,		/* 0 */
691 	PCIE_SPEED_2_5GT,		/* 1 */
692 	PCIE_SPEED_5_0GT,		/* 2 */
693 	PCIE_SPEED_8_0GT,		/* 3 */
694 	PCIE_SPEED_16_0GT,		/* 4 */
695 	PCIE_SPEED_32_0GT,		/* 5 */
696 	PCIE_SPEED_64_0GT,		/* 6 */
697 	PCI_SPEED_UNKNOWN,		/* 7 */
698 	PCI_SPEED_UNKNOWN,		/* 8 */
699 	PCI_SPEED_UNKNOWN,		/* 9 */
700 	PCI_SPEED_UNKNOWN,		/* A */
701 	PCI_SPEED_UNKNOWN,		/* B */
702 	PCI_SPEED_UNKNOWN,		/* C */
703 	PCI_SPEED_UNKNOWN,		/* D */
704 	PCI_SPEED_UNKNOWN,		/* E */
705 	PCI_SPEED_UNKNOWN		/* F */
706 };
707 EXPORT_SYMBOL_GPL(pcie_link_speed);
708 
pci_speed_string(enum pci_bus_speed speed)709 const char *pci_speed_string(enum pci_bus_speed speed)
710 {
711 	/* Indexed by the pci_bus_speed enum */
712 	static const char *speed_strings[] = {
713 	    "33 MHz PCI",		/* 0x00 */
714 	    "66 MHz PCI",		/* 0x01 */
715 	    "66 MHz PCI-X",		/* 0x02 */
716 	    "100 MHz PCI-X",		/* 0x03 */
717 	    "133 MHz PCI-X",		/* 0x04 */
718 	    NULL,			/* 0x05 */
719 	    NULL,			/* 0x06 */
720 	    NULL,			/* 0x07 */
721 	    NULL,			/* 0x08 */
722 	    "66 MHz PCI-X 266",		/* 0x09 */
723 	    "100 MHz PCI-X 266",	/* 0x0a */
724 	    "133 MHz PCI-X 266",	/* 0x0b */
725 	    "Unknown AGP",		/* 0x0c */
726 	    "1x AGP",			/* 0x0d */
727 	    "2x AGP",			/* 0x0e */
728 	    "4x AGP",			/* 0x0f */
729 	    "8x AGP",			/* 0x10 */
730 	    "66 MHz PCI-X 533",		/* 0x11 */
731 	    "100 MHz PCI-X 533",	/* 0x12 */
732 	    "133 MHz PCI-X 533",	/* 0x13 */
733 	    "2.5 GT/s PCIe",		/* 0x14 */
734 	    "5.0 GT/s PCIe",		/* 0x15 */
735 	    "8.0 GT/s PCIe",		/* 0x16 */
736 	    "16.0 GT/s PCIe",		/* 0x17 */
737 	    "32.0 GT/s PCIe",		/* 0x18 */
738 	    "64.0 GT/s PCIe",		/* 0x19 */
739 	};
740 
741 	if (speed < ARRAY_SIZE(speed_strings))
742 		return speed_strings[speed];
743 	return "Unknown";
744 }
745 EXPORT_SYMBOL_GPL(pci_speed_string);
746 
pcie_update_link_speed(struct pci_bus * bus,u16 linksta)747 void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
748 {
749 	bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
750 }
751 EXPORT_SYMBOL_GPL(pcie_update_link_speed);
752 
753 static unsigned char agp_speeds[] = {
754 	AGP_UNKNOWN,
755 	AGP_1X,
756 	AGP_2X,
757 	AGP_4X,
758 	AGP_8X
759 };
760 
agp_speed(int agp3,int agpstat)761 static enum pci_bus_speed agp_speed(int agp3, int agpstat)
762 {
763 	int index = 0;
764 
765 	if (agpstat & 4)
766 		index = 3;
767 	else if (agpstat & 2)
768 		index = 2;
769 	else if (agpstat & 1)
770 		index = 1;
771 	else
772 		goto out;
773 
774 	if (agp3) {
775 		index += 2;
776 		if (index == 5)
777 			index = 0;
778 	}
779 
780  out:
781 	return agp_speeds[index];
782 }
783 
pci_set_bus_speed(struct pci_bus * bus)784 static void pci_set_bus_speed(struct pci_bus *bus)
785 {
786 	struct pci_dev *bridge = bus->self;
787 	int pos;
788 
789 	pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
790 	if (!pos)
791 		pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
792 	if (pos) {
793 		u32 agpstat, agpcmd;
794 
795 		pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
796 		bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
797 
798 		pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
799 		bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
800 	}
801 
802 	pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
803 	if (pos) {
804 		u16 status;
805 		enum pci_bus_speed max;
806 
807 		pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
808 				     &status);
809 
810 		if (status & PCI_X_SSTATUS_533MHZ) {
811 			max = PCI_SPEED_133MHz_PCIX_533;
812 		} else if (status & PCI_X_SSTATUS_266MHZ) {
813 			max = PCI_SPEED_133MHz_PCIX_266;
814 		} else if (status & PCI_X_SSTATUS_133MHZ) {
815 			if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
816 				max = PCI_SPEED_133MHz_PCIX_ECC;
817 			else
818 				max = PCI_SPEED_133MHz_PCIX;
819 		} else {
820 			max = PCI_SPEED_66MHz_PCIX;
821 		}
822 
823 		bus->max_bus_speed = max;
824 		bus->cur_bus_speed =
825 			pcix_bus_speed[FIELD_GET(PCI_X_SSTATUS_FREQ, status)];
826 
827 		return;
828 	}
829 
830 	if (pci_is_pcie(bridge)) {
831 		u32 linkcap;
832 		u16 linksta;
833 
834 		pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
835 		bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
836 
837 		pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
838 		pcie_update_link_speed(bus, linksta);
839 	}
840 }
841 
pci_host_bridge_msi_domain(struct pci_bus * bus)842 static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
843 {
844 	struct irq_domain *d;
845 
846 	/* If the host bridge driver sets a MSI domain of the bridge, use it */
847 	d = dev_get_msi_domain(bus->bridge);
848 
849 	/*
850 	 * Any firmware interface that can resolve the msi_domain
851 	 * should be called from here.
852 	 */
853 	if (!d)
854 		d = pci_host_bridge_of_msi_domain(bus);
855 	if (!d)
856 		d = pci_host_bridge_acpi_msi_domain(bus);
857 
858 	/*
859 	 * If no IRQ domain was found via the OF tree, try looking it up
860 	 * directly through the fwnode_handle.
861 	 */
862 	if (!d) {
863 		struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
864 
865 		if (fwnode)
866 			d = irq_find_matching_fwnode(fwnode,
867 						     DOMAIN_BUS_PCI_MSI);
868 	}
869 
870 	return d;
871 }
872 
pci_set_bus_msi_domain(struct pci_bus * bus)873 static void pci_set_bus_msi_domain(struct pci_bus *bus)
874 {
875 	struct irq_domain *d;
876 	struct pci_bus *b;
877 
878 	/*
879 	 * The bus can be a root bus, a subordinate bus, or a virtual bus
880 	 * created by an SR-IOV device.  Walk up to the first bridge device
881 	 * found or derive the domain from the host bridge.
882 	 */
883 	for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
884 		if (b->self)
885 			d = dev_get_msi_domain(&b->self->dev);
886 	}
887 
888 	if (!d)
889 		d = pci_host_bridge_msi_domain(b);
890 
891 	dev_set_msi_domain(&bus->dev, d);
892 }
893 
pci_preserve_config(struct pci_host_bridge * host_bridge)894 static bool pci_preserve_config(struct pci_host_bridge *host_bridge)
895 {
896 	if (pci_acpi_preserve_config(host_bridge))
897 		return true;
898 
899 	if (host_bridge->dev.parent && host_bridge->dev.parent->of_node)
900 		return of_pci_preserve_config(host_bridge->dev.parent->of_node);
901 
902 	return false;
903 }
904 
pci_register_host_bridge(struct pci_host_bridge * bridge)905 static int pci_register_host_bridge(struct pci_host_bridge *bridge)
906 {
907 	struct device *parent = bridge->dev.parent;
908 	struct resource_entry *window, *next, *n;
909 	struct pci_bus *bus, *b;
910 	resource_size_t offset, next_offset;
911 	LIST_HEAD(resources);
912 	struct resource *res, *next_res;
913 	bool bus_registered = false;
914 	char addr[64], *fmt;
915 	const char *name;
916 	int err;
917 
918 	bus = pci_alloc_bus(NULL);
919 	if (!bus)
920 		return -ENOMEM;
921 
922 	bridge->bus = bus;
923 
924 	bus->sysdata = bridge->sysdata;
925 	bus->ops = bridge->ops;
926 	bus->number = bus->busn_res.start = bridge->busnr;
927 #ifdef CONFIG_PCI_DOMAINS_GENERIC
928 	if (bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET)
929 		bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
930 	else
931 		bus->domain_nr = bridge->domain_nr;
932 	if (bus->domain_nr < 0) {
933 		err = bus->domain_nr;
934 		goto free;
935 	}
936 #endif
937 
938 	b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
939 	if (b) {
940 		/* Ignore it if we already got here via a different bridge */
941 		dev_dbg(&b->dev, "bus already known\n");
942 		err = -EEXIST;
943 		goto free;
944 	}
945 
946 	dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
947 		     bridge->busnr);
948 
949 	err = pcibios_root_bridge_prepare(bridge);
950 	if (err)
951 		goto free;
952 
953 	/* Temporarily move resources off the list */
954 	list_splice_init(&bridge->windows, &resources);
955 	err = device_add(&bridge->dev);
956 	if (err)
957 		goto free;
958 
959 	bus->bridge = get_device(&bridge->dev);
960 	device_enable_async_suspend(bus->bridge);
961 	pci_set_bus_of_node(bus);
962 	pci_set_bus_msi_domain(bus);
963 	if (bridge->msi_domain && !dev_get_msi_domain(&bus->dev) &&
964 	    !pci_host_of_has_msi_map(parent))
965 		bus->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
966 
967 	if (!parent)
968 		set_dev_node(bus->bridge, pcibus_to_node(bus));
969 
970 	bus->dev.class = &pcibus_class;
971 	bus->dev.parent = bus->bridge;
972 
973 	dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
974 	name = dev_name(&bus->dev);
975 
976 	err = device_register(&bus->dev);
977 	bus_registered = true;
978 	if (err)
979 		goto unregister;
980 
981 	pcibios_add_bus(bus);
982 
983 	if (bus->ops->add_bus) {
984 		err = bus->ops->add_bus(bus);
985 		if (WARN_ON(err < 0))
986 			dev_err(&bus->dev, "failed to add bus: %d\n", err);
987 	}
988 
989 	/* Create legacy_io and legacy_mem files for this bus */
990 	pci_create_legacy_files(bus);
991 
992 	if (parent)
993 		dev_info(parent, "PCI host bridge to bus %s\n", name);
994 	else
995 		pr_info("PCI host bridge to bus %s\n", name);
996 
997 	if (nr_node_ids > 1 && pcibus_to_node(bus) == NUMA_NO_NODE)
998 		dev_warn(&bus->dev, "Unknown NUMA node; performance will be reduced\n");
999 
1000 	/* Check if the boot configuration by FW needs to be preserved */
1001 	bridge->preserve_config = pci_preserve_config(bridge);
1002 
1003 	/* Coalesce contiguous windows */
1004 	resource_list_for_each_entry_safe(window, n, &resources) {
1005 		if (list_is_last(&window->node, &resources))
1006 			break;
1007 
1008 		next = list_next_entry(window, node);
1009 		offset = window->offset;
1010 		res = window->res;
1011 		next_offset = next->offset;
1012 		next_res = next->res;
1013 
1014 		if (res->flags != next_res->flags || offset != next_offset)
1015 			continue;
1016 
1017 		if (res->end + 1 == next_res->start) {
1018 			next_res->start = res->start;
1019 			res->flags = res->start = res->end = 0;
1020 		}
1021 	}
1022 
1023 	/* Add initial resources to the bus */
1024 	resource_list_for_each_entry_safe(window, n, &resources) {
1025 		offset = window->offset;
1026 		res = window->res;
1027 		if (!res->flags && !res->start && !res->end) {
1028 			release_resource(res);
1029 			resource_list_destroy_entry(window);
1030 			continue;
1031 		}
1032 
1033 		list_move_tail(&window->node, &bridge->windows);
1034 
1035 		if (res->flags & IORESOURCE_BUS)
1036 			pci_bus_insert_busn_res(bus, bus->number, res->end);
1037 		else
1038 			pci_bus_add_resource(bus, res, 0);
1039 
1040 		if (offset) {
1041 			if (resource_type(res) == IORESOURCE_IO)
1042 				fmt = " (bus address [%#06llx-%#06llx])";
1043 			else
1044 				fmt = " (bus address [%#010llx-%#010llx])";
1045 
1046 			snprintf(addr, sizeof(addr), fmt,
1047 				 (unsigned long long)(res->start - offset),
1048 				 (unsigned long long)(res->end - offset));
1049 		} else
1050 			addr[0] = '\0';
1051 
1052 		dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
1053 	}
1054 
1055 	down_write(&pci_bus_sem);
1056 	list_add_tail(&bus->node, &pci_root_buses);
1057 	up_write(&pci_bus_sem);
1058 
1059 	return 0;
1060 
1061 unregister:
1062 	put_device(&bridge->dev);
1063 	device_del(&bridge->dev);
1064 free:
1065 #ifdef CONFIG_PCI_DOMAINS_GENERIC
1066 	pci_bus_release_domain_nr(parent, bus->domain_nr);
1067 #endif
1068 	if (bus_registered)
1069 		put_device(&bus->dev);
1070 	else
1071 		kfree(bus);
1072 
1073 	return err;
1074 }
1075 
pci_bridge_child_ext_cfg_accessible(struct pci_dev * bridge)1076 static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev *bridge)
1077 {
1078 	int pos;
1079 	u32 status;
1080 
1081 	/*
1082 	 * If extended config space isn't accessible on a bridge's primary
1083 	 * bus, we certainly can't access it on the secondary bus.
1084 	 */
1085 	if (bridge->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
1086 		return false;
1087 
1088 	/*
1089 	 * PCIe Root Ports and switch ports are PCIe on both sides, so if
1090 	 * extended config space is accessible on the primary, it's also
1091 	 * accessible on the secondary.
1092 	 */
1093 	if (pci_is_pcie(bridge) &&
1094 	    (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT ||
1095 	     pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM ||
1096 	     pci_pcie_type(bridge) == PCI_EXP_TYPE_DOWNSTREAM))
1097 		return true;
1098 
1099 	/*
1100 	 * For the other bridge types:
1101 	 *   - PCI-to-PCI bridges
1102 	 *   - PCIe-to-PCI/PCI-X forward bridges
1103 	 *   - PCI/PCI-X-to-PCIe reverse bridges
1104 	 * extended config space on the secondary side is only accessible
1105 	 * if the bridge supports PCI-X Mode 2.
1106 	 */
1107 	pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
1108 	if (!pos)
1109 		return false;
1110 
1111 	pci_read_config_dword(bridge, pos + PCI_X_STATUS, &status);
1112 	return status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ);
1113 }
1114 
pci_alloc_child_bus(struct pci_bus * parent,struct pci_dev * bridge,int busnr)1115 static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
1116 					   struct pci_dev *bridge, int busnr)
1117 {
1118 	struct pci_bus *child;
1119 	struct pci_host_bridge *host;
1120 	int i;
1121 	int ret;
1122 
1123 	/* Allocate a new bus and inherit stuff from the parent */
1124 	child = pci_alloc_bus(parent);
1125 	if (!child)
1126 		return NULL;
1127 
1128 	child->parent = parent;
1129 	child->sysdata = parent->sysdata;
1130 	child->bus_flags = parent->bus_flags;
1131 
1132 	host = pci_find_host_bridge(parent);
1133 	if (host->child_ops)
1134 		child->ops = host->child_ops;
1135 	else
1136 		child->ops = parent->ops;
1137 
1138 	/*
1139 	 * Initialize some portions of the bus device, but don't register
1140 	 * it now as the parent is not properly set up yet.
1141 	 */
1142 	child->dev.class = &pcibus_class;
1143 	dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
1144 
1145 	/* Set up the primary, secondary and subordinate bus numbers */
1146 	child->number = child->busn_res.start = busnr;
1147 	child->primary = parent->busn_res.start;
1148 	child->busn_res.end = 0xff;
1149 
1150 	if (!bridge) {
1151 		child->dev.parent = parent->bridge;
1152 		goto add_dev;
1153 	}
1154 
1155 	child->self = bridge;
1156 	child->bridge = get_device(&bridge->dev);
1157 	child->dev.parent = child->bridge;
1158 	pci_set_bus_of_node(child);
1159 	pci_set_bus_speed(child);
1160 
1161 	/*
1162 	 * Check whether extended config space is accessible on the child
1163 	 * bus.  Note that we currently assume it is always accessible on
1164 	 * the root bus.
1165 	 */
1166 	if (!pci_bridge_child_ext_cfg_accessible(bridge)) {
1167 		child->bus_flags |= PCI_BUS_FLAGS_NO_EXTCFG;
1168 		pci_info(child, "extended config space not accessible\n");
1169 	}
1170 
1171 	/* Set up default resource pointers and names */
1172 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
1173 		child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
1174 		child->resource[i]->name = child->name;
1175 	}
1176 	bridge->subordinate = child;
1177 
1178 add_dev:
1179 	pci_set_bus_msi_domain(child);
1180 	ret = device_register(&child->dev);
1181 	if (WARN_ON(ret < 0)) {
1182 		put_device(&child->dev);
1183 		return NULL;
1184 	}
1185 
1186 	pcibios_add_bus(child);
1187 
1188 	if (child->ops->add_bus) {
1189 		ret = child->ops->add_bus(child);
1190 		if (WARN_ON(ret < 0))
1191 			dev_err(&child->dev, "failed to add bus: %d\n", ret);
1192 	}
1193 
1194 	/* Create legacy_io and legacy_mem files for this bus */
1195 	pci_create_legacy_files(child);
1196 
1197 	return child;
1198 }
1199 
pci_add_new_bus(struct pci_bus * parent,struct pci_dev * dev,int busnr)1200 struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
1201 				int busnr)
1202 {
1203 	struct pci_bus *child;
1204 
1205 	child = pci_alloc_child_bus(parent, dev, busnr);
1206 	if (child) {
1207 		down_write(&pci_bus_sem);
1208 		list_add_tail(&child->node, &parent->children);
1209 		up_write(&pci_bus_sem);
1210 	}
1211 	return child;
1212 }
1213 EXPORT_SYMBOL(pci_add_new_bus);
1214 
pci_enable_rrs_sv(struct pci_dev * pdev)1215 static void pci_enable_rrs_sv(struct pci_dev *pdev)
1216 {
1217 	u16 root_cap = 0;
1218 
1219 	/* Enable Configuration RRS Software Visibility if supported */
1220 	pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
1221 	if (root_cap & PCI_EXP_RTCAP_RRS_SV) {
1222 		pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
1223 					 PCI_EXP_RTCTL_RRS_SVE);
1224 		pdev->config_rrs_sv = 1;
1225 	}
1226 }
1227 
1228 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
1229 					      unsigned int available_buses);
1230 /**
1231  * pci_ea_fixed_busnrs() - Read fixed Secondary and Subordinate bus
1232  * numbers from EA capability.
1233  * @dev: Bridge
1234  * @sec: updated with secondary bus number from EA
1235  * @sub: updated with subordinate bus number from EA
1236  *
1237  * If @dev is a bridge with EA capability that specifies valid secondary
1238  * and subordinate bus numbers, return true with the bus numbers in @sec
1239  * and @sub.  Otherwise return false.
1240  */
pci_ea_fixed_busnrs(struct pci_dev * dev,u8 * sec,u8 * sub)1241 static bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub)
1242 {
1243 	int ea, offset;
1244 	u32 dw;
1245 	u8 ea_sec, ea_sub;
1246 
1247 	if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE)
1248 		return false;
1249 
1250 	/* find PCI EA capability in list */
1251 	ea = pci_find_capability(dev, PCI_CAP_ID_EA);
1252 	if (!ea)
1253 		return false;
1254 
1255 	offset = ea + PCI_EA_FIRST_ENT;
1256 	pci_read_config_dword(dev, offset, &dw);
1257 	ea_sec = FIELD_GET(PCI_EA_SEC_BUS_MASK, dw);
1258 	ea_sub = FIELD_GET(PCI_EA_SUB_BUS_MASK, dw);
1259 	if (ea_sec  == 0 || ea_sub < ea_sec)
1260 		return false;
1261 
1262 	*sec = ea_sec;
1263 	*sub = ea_sub;
1264 	return true;
1265 }
1266 
1267 /*
1268  * pci_scan_bridge_extend() - Scan buses behind a bridge
1269  * @bus: Parent bus the bridge is on
1270  * @dev: Bridge itself
1271  * @max: Starting subordinate number of buses behind this bridge
1272  * @available_buses: Total number of buses available for this bridge and
1273  *		     the devices below. After the minimal bus space has
1274  *		     been allocated the remaining buses will be
1275  *		     distributed equally between hotplug-capable bridges.
1276  * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1277  *        that need to be reconfigured.
1278  *
1279  * If it's a bridge, configure it and scan the bus behind it.
1280  * For CardBus bridges, we don't scan behind as the devices will
1281  * be handled by the bridge driver itself.
1282  *
1283  * We need to process bridges in two passes -- first we scan those
1284  * already configured by the BIOS and after we are done with all of
1285  * them, we proceed to assigning numbers to the remaining buses in
1286  * order to avoid overlaps between old and new bus numbers.
1287  *
1288  * Return: New subordinate number covering all buses behind this bridge.
1289  */
pci_scan_bridge_extend(struct pci_bus * bus,struct pci_dev * dev,int max,unsigned int available_buses,int pass)1290 static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
1291 				  int max, unsigned int available_buses,
1292 				  int pass)
1293 {
1294 	struct pci_bus *child;
1295 	int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
1296 	u32 buses, i, j = 0;
1297 	u16 bctl;
1298 	u8 primary, secondary, subordinate;
1299 	int broken = 0;
1300 	bool fixed_buses;
1301 	u8 fixed_sec, fixed_sub;
1302 	int next_busnr;
1303 
1304 	/*
1305 	 * Make sure the bridge is powered on to be able to access config
1306 	 * space of devices below it.
1307 	 */
1308 	pm_runtime_get_sync(&dev->dev);
1309 
1310 	pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
1311 	primary = buses & 0xFF;
1312 	secondary = (buses >> 8) & 0xFF;
1313 	subordinate = (buses >> 16) & 0xFF;
1314 
1315 	pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
1316 		secondary, subordinate, pass);
1317 
1318 	if (!primary && (primary != bus->number) && secondary && subordinate) {
1319 		pci_warn(dev, "Primary bus is hard wired to 0\n");
1320 		primary = bus->number;
1321 	}
1322 
1323 	/* Check if setup is sensible at all */
1324 	if (!pass &&
1325 	    (primary != bus->number || secondary <= bus->number ||
1326 	     secondary > subordinate)) {
1327 		pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
1328 			 secondary, subordinate);
1329 		broken = 1;
1330 	}
1331 
1332 	/*
1333 	 * Disable Master-Abort Mode during probing to avoid reporting of
1334 	 * bus errors in some architectures.
1335 	 */
1336 	pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1337 	pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1338 			      bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1339 
1340 	if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1341 	    !is_cardbus && !broken) {
1342 		unsigned int cmax, buses;
1343 
1344 		/*
1345 		 * Bus already configured by firmware, process it in the
1346 		 * first pass and just note the configuration.
1347 		 */
1348 		if (pass)
1349 			goto out;
1350 
1351 		/*
1352 		 * The bus might already exist for two reasons: Either we
1353 		 * are rescanning the bus or the bus is reachable through
1354 		 * more than one bridge. The second case can happen with
1355 		 * the i450NX chipset.
1356 		 */
1357 		child = pci_find_bus(pci_domain_nr(bus), secondary);
1358 		if (!child) {
1359 			child = pci_add_new_bus(bus, dev, secondary);
1360 			if (!child)
1361 				goto out;
1362 			child->primary = primary;
1363 			pci_bus_insert_busn_res(child, secondary, subordinate);
1364 			child->bridge_ctl = bctl;
1365 		}
1366 
1367 		buses = subordinate - secondary;
1368 		cmax = pci_scan_child_bus_extend(child, buses);
1369 		if (cmax > subordinate)
1370 			pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
1371 				 subordinate, cmax);
1372 
1373 		/* Subordinate should equal child->busn_res.end */
1374 		if (subordinate > max)
1375 			max = subordinate;
1376 	} else {
1377 
1378 		/*
1379 		 * We need to assign a number to this bus which we always
1380 		 * do in the second pass.
1381 		 */
1382 		if (!pass) {
1383 			if (pcibios_assign_all_busses() || broken || is_cardbus)
1384 
1385 				/*
1386 				 * Temporarily disable forwarding of the
1387 				 * configuration cycles on all bridges in
1388 				 * this bus segment to avoid possible
1389 				 * conflicts in the second pass between two
1390 				 * bridges programmed with overlapping bus
1391 				 * ranges.
1392 				 */
1393 				pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1394 						       buses & ~0xffffff);
1395 			goto out;
1396 		}
1397 
1398 		/* Clear errors */
1399 		pci_write_config_word(dev, PCI_STATUS, 0xffff);
1400 
1401 		/* Read bus numbers from EA Capability (if present) */
1402 		fixed_buses = pci_ea_fixed_busnrs(dev, &fixed_sec, &fixed_sub);
1403 		if (fixed_buses)
1404 			next_busnr = fixed_sec;
1405 		else
1406 			next_busnr = max + 1;
1407 
1408 		/*
1409 		 * Prevent assigning a bus number that already exists.
1410 		 * This can happen when a bridge is hot-plugged, so in this
1411 		 * case we only re-scan this bus.
1412 		 */
1413 		child = pci_find_bus(pci_domain_nr(bus), next_busnr);
1414 		if (!child) {
1415 			child = pci_add_new_bus(bus, dev, next_busnr);
1416 			if (!child)
1417 				goto out;
1418 			pci_bus_insert_busn_res(child, next_busnr,
1419 						bus->busn_res.end);
1420 		}
1421 		max++;
1422 		if (available_buses)
1423 			available_buses--;
1424 
1425 		buses = (buses & 0xff000000)
1426 		      | ((unsigned int)(child->primary)     <<  0)
1427 		      | ((unsigned int)(child->busn_res.start)   <<  8)
1428 		      | ((unsigned int)(child->busn_res.end) << 16);
1429 
1430 		/*
1431 		 * yenta.c forces a secondary latency timer of 176.
1432 		 * Copy that behaviour here.
1433 		 */
1434 		if (is_cardbus) {
1435 			buses &= ~0xff000000;
1436 			buses |= CARDBUS_LATENCY_TIMER << 24;
1437 		}
1438 
1439 		/* We need to blast all three values with a single write */
1440 		pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1441 
1442 		if (!is_cardbus) {
1443 			child->bridge_ctl = bctl;
1444 			max = pci_scan_child_bus_extend(child, available_buses);
1445 		} else {
1446 
1447 			/*
1448 			 * For CardBus bridges, we leave 4 bus numbers as
1449 			 * cards with a PCI-to-PCI bridge can be inserted
1450 			 * later.
1451 			 */
1452 			for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
1453 				struct pci_bus *parent = bus;
1454 				if (pci_find_bus(pci_domain_nr(bus),
1455 							max+i+1))
1456 					break;
1457 				while (parent->parent) {
1458 					if ((!pcibios_assign_all_busses()) &&
1459 					    (parent->busn_res.end > max) &&
1460 					    (parent->busn_res.end <= max+i)) {
1461 						j = 1;
1462 					}
1463 					parent = parent->parent;
1464 				}
1465 				if (j) {
1466 
1467 					/*
1468 					 * Often, there are two CardBus
1469 					 * bridges -- try to leave one
1470 					 * valid bus number for each one.
1471 					 */
1472 					i /= 2;
1473 					break;
1474 				}
1475 			}
1476 			max += i;
1477 		}
1478 
1479 		/*
1480 		 * Set subordinate bus number to its real value.
1481 		 * If fixed subordinate bus number exists from EA
1482 		 * capability then use it.
1483 		 */
1484 		if (fixed_buses)
1485 			max = fixed_sub;
1486 		pci_bus_update_busn_res_end(child, max);
1487 		pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1488 	}
1489 
1490 	sprintf(child->name,
1491 		(is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1492 		pci_domain_nr(bus), child->number);
1493 
1494 	/* Check that all devices are accessible */
1495 	while (bus->parent) {
1496 		if ((child->busn_res.end > bus->busn_res.end) ||
1497 		    (child->number > bus->busn_res.end) ||
1498 		    (child->number < bus->number) ||
1499 		    (child->busn_res.end < bus->number)) {
1500 			dev_info(&dev->dev, "devices behind bridge are unusable because %pR cannot be assigned for them\n",
1501 				 &child->busn_res);
1502 			break;
1503 		}
1504 		bus = bus->parent;
1505 	}
1506 
1507 out:
1508 	/* Clear errors in the Secondary Status Register */
1509 	pci_write_config_word(dev, PCI_SEC_STATUS, 0xffff);
1510 
1511 	pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1512 
1513 	pm_runtime_put(&dev->dev);
1514 
1515 	return max;
1516 }
1517 
1518 /*
1519  * pci_scan_bridge() - Scan buses behind a bridge
1520  * @bus: Parent bus the bridge is on
1521  * @dev: Bridge itself
1522  * @max: Starting subordinate number of buses behind this bridge
1523  * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1524  *        that need to be reconfigured.
1525  *
1526  * If it's a bridge, configure it and scan the bus behind it.
1527  * For CardBus bridges, we don't scan behind as the devices will
1528  * be handled by the bridge driver itself.
1529  *
1530  * We need to process bridges in two passes -- first we scan those
1531  * already configured by the BIOS and after we are done with all of
1532  * them, we proceed to assigning numbers to the remaining buses in
1533  * order to avoid overlaps between old and new bus numbers.
1534  *
1535  * Return: New subordinate number covering all buses behind this bridge.
1536  */
pci_scan_bridge(struct pci_bus * bus,struct pci_dev * dev,int max,int pass)1537 int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1538 {
1539 	return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1540 }
1541 EXPORT_SYMBOL(pci_scan_bridge);
1542 
1543 /*
1544  * Read interrupt line and base address registers.
1545  * The architecture-dependent code can tweak these, of course.
1546  */
pci_read_irq(struct pci_dev * dev)1547 static void pci_read_irq(struct pci_dev *dev)
1548 {
1549 	unsigned char irq;
1550 
1551 	/* VFs are not allowed to use INTx, so skip the config reads */
1552 	if (dev->is_virtfn) {
1553 		dev->pin = 0;
1554 		dev->irq = 0;
1555 		return;
1556 	}
1557 
1558 	pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
1559 	dev->pin = irq;
1560 	if (irq)
1561 		pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1562 	dev->irq = irq;
1563 }
1564 
set_pcie_port_type(struct pci_dev * pdev)1565 void set_pcie_port_type(struct pci_dev *pdev)
1566 {
1567 	int pos;
1568 	u16 reg16;
1569 	u32 reg32;
1570 	int type;
1571 	struct pci_dev *parent;
1572 
1573 	pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1574 	if (!pos)
1575 		return;
1576 
1577 	pdev->pcie_cap = pos;
1578 	pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
1579 	pdev->pcie_flags_reg = reg16;
1580 
1581 	type = pci_pcie_type(pdev);
1582 	if (type == PCI_EXP_TYPE_ROOT_PORT)
1583 		pci_enable_rrs_sv(pdev);
1584 
1585 	pci_read_config_dword(pdev, pos + PCI_EXP_DEVCAP, &pdev->devcap);
1586 	pdev->pcie_mpss = FIELD_GET(PCI_EXP_DEVCAP_PAYLOAD, pdev->devcap);
1587 
1588 	pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, ®32);
1589 	if (reg32 & PCI_EXP_LNKCAP_DLLLARC)
1590 		pdev->link_active_reporting = 1;
1591 
1592 	parent = pci_upstream_bridge(pdev);
1593 	if (!parent)
1594 		return;
1595 
1596 	/*
1597 	 * Some systems do not identify their upstream/downstream ports
1598 	 * correctly so detect impossible configurations here and correct
1599 	 * the port type accordingly.
1600 	 */
1601 	if (type == PCI_EXP_TYPE_DOWNSTREAM) {
1602 		/*
1603 		 * If pdev claims to be downstream port but the parent
1604 		 * device is also downstream port assume pdev is actually
1605 		 * upstream port.
1606 		 */
1607 		if (pcie_downstream_port(parent)) {
1608 			pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n");
1609 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
1610 			pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM;
1611 		}
1612 	} else if (type == PCI_EXP_TYPE_UPSTREAM) {
1613 		/*
1614 		 * If pdev claims to be upstream port but the parent
1615 		 * device is also upstream port assume pdev is actually
1616 		 * downstream port.
1617 		 */
1618 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) {
1619 			pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n");
1620 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
1621 			pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM;
1622 		}
1623 	}
1624 }
1625 
set_pcie_hotplug_bridge(struct pci_dev * pdev)1626 void set_pcie_hotplug_bridge(struct pci_dev *pdev)
1627 {
1628 	u32 reg32;
1629 
1630 	pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32);
1631 	if (reg32 & PCI_EXP_SLTCAP_HPC)
1632 		pdev->is_hotplug_bridge = 1;
1633 }
1634 
set_pcie_thunderbolt(struct pci_dev * dev)1635 static void set_pcie_thunderbolt(struct pci_dev *dev)
1636 {
1637 	u16 vsec;
1638 
1639 	/* Is the device part of a Thunderbolt controller? */
1640 	vsec = pci_find_vsec_capability(dev, PCI_VENDOR_ID_INTEL, PCI_VSEC_ID_INTEL_TBT);
1641 	if (vsec)
1642 		dev->is_thunderbolt = 1;
1643 }
1644 
pci_set_requires_dma_protection(struct pci_dev * dev)1645 static void pci_set_requires_dma_protection(struct pci_dev *dev)
1646 {
1647 	struct pci_dev *parent = pci_upstream_bridge(dev);
1648 
1649 	if (!parent)
1650 		return;
1651 	/*
1652 	 * If the upstream bridge is untrusted we treat this device as
1653 	 * untrusted as well.
1654 	 */
1655 	if (parent->requires_dma_protection) {
1656 		dev->requires_dma_protection = true;
1657 		return;
1658 	}
1659 
1660 	if (arch_pci_dev_is_removable(dev)) {
1661 		pci_dbg(dev, "marking as untrusted\n");
1662 		dev->requires_dma_protection = true;
1663 	}
1664 }
1665 
pci_set_removable(struct pci_dev * dev)1666 static void pci_set_removable(struct pci_dev *dev)
1667 {
1668 	struct pci_dev *parent = pci_upstream_bridge(dev);
1669 
1670 	if (!parent)
1671 		return;
1672 	/*
1673 	 * We (only) consider everything tunneled below an external_facing
1674 	 * device to be removable by the user. We're mainly concerned with
1675 	 * consumer platforms with user accessible thunderbolt ports that are
1676 	 * vulnerable to DMA attacks, and we expect those ports to be marked by
1677 	 * the firmware as external_facing. Devices in traditional hotplug
1678 	 * slots can technically be removed, but the expectation is that unless
1679 	 * the port is marked with external_facing, such devices are less
1680 	 * accessible to user / may not be removed by end user, and thus not
1681 	 * exposed as "removable" to userspace.
1682 	 */
1683 	if (dev_is_removable(&parent->dev)) {
1684 		dev_set_removable(&dev->dev, DEVICE_REMOVABLE);
1685 		return;
1686 	}
1687 
1688 	if (arch_pci_dev_is_removable(dev)) {
1689 		pci_dbg(dev, "marking as removable\n");
1690 		dev_set_removable(&dev->dev, DEVICE_REMOVABLE);
1691 	}
1692 }
1693 
1694 /**
1695  * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
1696  * @dev: PCI device
1697  *
1698  * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1699  * when forwarding a type1 configuration request the bridge must check that
1700  * the extended register address field is zero.  The bridge is not permitted
1701  * to forward the transactions and must handle it as an Unsupported Request.
1702  * Some bridges do not follow this rule and simply drop the extended register
1703  * bits, resulting in the standard config space being aliased, every 256
1704  * bytes across the entire configuration space.  Test for this condition by
1705  * comparing the first dword of each potential alias to the vendor/device ID.
1706  * Known offenders:
1707  *   ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1708  *   AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1709  */
pci_ext_cfg_is_aliased(struct pci_dev * dev)1710 static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1711 {
1712 #ifdef CONFIG_PCI_QUIRKS
1713 	int pos, ret;
1714 	u32 header, tmp;
1715 
1716 	pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1717 
1718 	for (pos = PCI_CFG_SPACE_SIZE;
1719 	     pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1720 		ret = pci_read_config_dword(dev, pos, &tmp);
1721 		if ((ret != PCIBIOS_SUCCESSFUL) || (header != tmp))
1722 			return false;
1723 	}
1724 
1725 	return true;
1726 #else
1727 	return false;
1728 #endif
1729 }
1730 
1731 /**
1732  * pci_cfg_space_size_ext - Get the configuration space size of the PCI device
1733  * @dev: PCI device
1734  *
1735  * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1736  * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
1737  * access it.  Maybe we don't have a way to generate extended config space
1738  * accesses, or the device is behind a reverse Express bridge.  So we try
1739  * reading the dword at 0x100 which must either be 0 or a valid extended
1740  * capability header.
1741  */
pci_cfg_space_size_ext(struct pci_dev * dev)1742 static int pci_cfg_space_size_ext(struct pci_dev *dev)
1743 {
1744 	u32 status;
1745 	int pos = PCI_CFG_SPACE_SIZE;
1746 
1747 	if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1748 		return PCI_CFG_SPACE_SIZE;
1749 	if (PCI_POSSIBLE_ERROR(status) || pci_ext_cfg_is_aliased(dev))
1750 		return PCI_CFG_SPACE_SIZE;
1751 
1752 	return PCI_CFG_SPACE_EXP_SIZE;
1753 }
1754 
pci_cfg_space_size(struct pci_dev * dev)1755 int pci_cfg_space_size(struct pci_dev *dev)
1756 {
1757 	int pos;
1758 	u32 status;
1759 	u16 class;
1760 
1761 #ifdef CONFIG_PCI_IOV
1762 	/*
1763 	 * Per the SR-IOV specification (rev 1.1, sec 3.5), VFs are required to
1764 	 * implement a PCIe capability and therefore must implement extended
1765 	 * config space.  We can skip the NO_EXTCFG test below and the
1766 	 * reachability/aliasing test in pci_cfg_space_size_ext() by virtue of
1767 	 * the fact that the SR-IOV capability on the PF resides in extended
1768 	 * config space and must be accessible and non-aliased to have enabled
1769 	 * support for this VF.  This is a micro performance optimization for
1770 	 * systems supporting many VFs.
1771 	 */
1772 	if (dev->is_virtfn)
1773 		return PCI_CFG_SPACE_EXP_SIZE;
1774 #endif
1775 
1776 	if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
1777 		return PCI_CFG_SPACE_SIZE;
1778 
1779 	class = dev->class >> 8;
1780 	if (class == PCI_CLASS_BRIDGE_HOST)
1781 		return pci_cfg_space_size_ext(dev);
1782 
1783 	if (pci_is_pcie(dev))
1784 		return pci_cfg_space_size_ext(dev);
1785 
1786 	pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1787 	if (!pos)
1788 		return PCI_CFG_SPACE_SIZE;
1789 
1790 	pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1791 	if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1792 		return pci_cfg_space_size_ext(dev);
1793 
1794 	return PCI_CFG_SPACE_SIZE;
1795 }
1796 
pci_class(struct pci_dev * dev)1797 static u32 pci_class(struct pci_dev *dev)
1798 {
1799 	u32 class;
1800 
1801 #ifdef CONFIG_PCI_IOV
1802 	if (dev->is_virtfn)
1803 		return dev->physfn->sriov->class;
1804 #endif
1805 	pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
1806 	return class;
1807 }
1808 
pci_subsystem_ids(struct pci_dev * dev,u16 * vendor,u16 * device)1809 static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device)
1810 {
1811 #ifdef CONFIG_PCI_IOV
1812 	if (dev->is_virtfn) {
1813 		*vendor = dev->physfn->sriov->subsystem_vendor;
1814 		*device = dev->physfn->sriov->subsystem_device;
1815 		return;
1816 	}
1817 #endif
1818 	pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor);
1819 	pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device);
1820 }
1821 
pci_hdr_type(struct pci_dev * dev)1822 static u8 pci_hdr_type(struct pci_dev *dev)
1823 {
1824 	u8 hdr_type;
1825 
1826 #ifdef CONFIG_PCI_IOV
1827 	if (dev->is_virtfn)
1828 		return dev->physfn->sriov->hdr_type;
1829 #endif
1830 	pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
1831 	return hdr_type;
1832 }
1833 
1834 #define LEGACY_IO_RESOURCE	(IORESOURCE_IO | IORESOURCE_PCI_FIXED)
1835 
1836 /**
1837  * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
1838  * @dev: PCI device
1839  *
1840  * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev.  Check this
1841  * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1842  */
pci_intx_mask_broken(struct pci_dev * dev)1843 static int pci_intx_mask_broken(struct pci_dev *dev)
1844 {
1845 	u16 orig, toggle, new;
1846 
1847 	pci_read_config_word(dev, PCI_COMMAND, &orig);
1848 	toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1849 	pci_write_config_word(dev, PCI_COMMAND, toggle);
1850 	pci_read_config_word(dev, PCI_COMMAND, &new);
1851 
1852 	pci_write_config_word(dev, PCI_COMMAND, orig);
1853 
1854 	/*
1855 	 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1856 	 * r2.3, so strictly speaking, a device is not *broken* if it's not
1857 	 * writable.  But we'll live with the misnomer for now.
1858 	 */
1859 	if (new != toggle)
1860 		return 1;
1861 	return 0;
1862 }
1863 
early_dump_pci_device(struct pci_dev * pdev)1864 static void early_dump_pci_device(struct pci_dev *pdev)
1865 {
1866 	u32 value[256 / 4];
1867 	int i;
1868 
1869 	pci_info(pdev, "config space:\n");
1870 
1871 	for (i = 0; i < 256; i += 4)
1872 		pci_read_config_dword(pdev, i, &value[i / 4]);
1873 
1874 	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1875 		       value, 256, false);
1876 }
1877 
pci_type_str(struct pci_dev * dev)1878 static const char *pci_type_str(struct pci_dev *dev)
1879 {
1880 	static const char * const str[] = {
1881 		"PCIe Endpoint",
1882 		"PCIe Legacy Endpoint",
1883 		"PCIe unknown",
1884 		"PCIe unknown",
1885 		"PCIe Root Port",
1886 		"PCIe Switch Upstream Port",
1887 		"PCIe Switch Downstream Port",
1888 		"PCIe to PCI/PCI-X bridge",
1889 		"PCI/PCI-X to PCIe bridge",
1890 		"PCIe Root Complex Integrated Endpoint",
1891 		"PCIe Root Complex Event Collector",
1892 	};
1893 	int type;
1894 
1895 	if (pci_is_pcie(dev)) {
1896 		type = pci_pcie_type(dev);
1897 		if (type < ARRAY_SIZE(str))
1898 			return str[type];
1899 
1900 		return "PCIe unknown";
1901 	}
1902 
1903 	switch (dev->hdr_type) {
1904 	case PCI_HEADER_TYPE_NORMAL:
1905 		return "conventional PCI endpoint";
1906 	case PCI_HEADER_TYPE_BRIDGE:
1907 		return "conventional PCI bridge";
1908 	case PCI_HEADER_TYPE_CARDBUS:
1909 		return "CardBus bridge";
1910 	default:
1911 		return "conventional PCI";
1912 	}
1913 }
1914 
1915 /**
1916  * pci_setup_device - Fill in class and map information of a device
1917  * @dev: the device structure to fill
1918  *
1919  * Initialize the device structure with information about the device's
1920  * vendor,class,memory and IO-space addresses, IRQ lines etc.
1921  * Called at initialisation of the PCI subsystem and by CardBus services.
1922  * Returns 0 on success and negative if unknown type of device (not normal,
1923  * bridge or CardBus).
1924  */
pci_setup_device(struct pci_dev * dev)1925 int pci_setup_device(struct pci_dev *dev)
1926 {
1927 	u32 class;
1928 	u16 cmd;
1929 	u8 hdr_type;
1930 	int err, pos = 0;
1931 	struct pci_bus_region region;
1932 	struct resource *res;
1933 
1934 	hdr_type = pci_hdr_type(dev);
1935 
1936 	dev->sysdata = dev->bus->sysdata;
1937 	dev->dev.parent = dev->bus->bridge;
1938 	dev->dev.bus = &pci_bus_type;
1939 	dev->hdr_type = hdr_type & 0x7f;
1940 	dev->multifunction = !!(hdr_type & 0x80);
1941 	dev->error_state = pci_channel_io_normal;
1942 	set_pcie_port_type(dev);
1943 
1944 	err = pci_set_of_node(dev);
1945 	if (err)
1946 		return err;
1947 	pci_set_acpi_fwnode(dev);
1948 
1949 	pci_dev_assign_slot(dev);
1950 
1951 	/*
1952 	 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1953 	 * set this higher, assuming the system even supports it.
1954 	 */
1955 	dev->dma_mask = 0xffffffff;
1956 
1957 	dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1958 		     dev->bus->number, PCI_SLOT(dev->devfn),
1959 		     PCI_FUNC(dev->devfn));
1960 
1961 	class = pci_class(dev);
1962 
1963 	dev->revision = class & 0xff;
1964 	dev->class = class >> 8;		    /* upper 3 bytes */
1965 
1966 	if (pci_early_dump)
1967 		early_dump_pci_device(dev);
1968 
1969 	/* Need to have dev->class ready */
1970 	dev->cfg_size = pci_cfg_space_size(dev);
1971 
1972 	/* Need to have dev->cfg_size ready */
1973 	set_pcie_thunderbolt(dev);
1974 
1975 	pci_set_requires_dma_protection(dev);
1976 
1977 	/* "Unknown power state" */
1978 	dev->current_state = PCI_UNKNOWN;
1979 
1980 	/* Early fixups, before probing the BARs */
1981 	pci_fixup_device(pci_fixup_early, dev);
1982 
1983 	pci_set_removable(dev);
1984 
1985 	pci_info(dev, "[%04x:%04x] type %02x class %#08x %s\n",
1986 		 dev->vendor, dev->device, dev->hdr_type, dev->class,
1987 		 pci_type_str(dev));
1988 
1989 	/* Device class may be changed after fixup */
1990 	class = dev->class >> 8;
1991 
1992 	if (dev->non_compliant_bars && !dev->mmio_always_on) {
1993 		pci_read_config_word(dev, PCI_COMMAND, &cmd);
1994 		if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
1995 			pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
1996 			cmd &= ~PCI_COMMAND_IO;
1997 			cmd &= ~PCI_COMMAND_MEMORY;
1998 			pci_write_config_word(dev, PCI_COMMAND, cmd);
1999 		}
2000 	}
2001 
2002 	dev->broken_intx_masking = pci_intx_mask_broken(dev);
2003 
2004 	switch (dev->hdr_type) {		    /* header type */
2005 	case PCI_HEADER_TYPE_NORMAL:		    /* standard header */
2006 		if (class == PCI_CLASS_BRIDGE_PCI)
2007 			goto bad;
2008 		pci_read_irq(dev);
2009 		pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
2010 
2011 		pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device);
2012 
2013 		/*
2014 		 * Do the ugly legacy mode stuff here rather than broken chip
2015 		 * quirk code. Legacy mode ATA controllers have fixed
2016 		 * addresses. These are not always echoed in BAR0-3, and
2017 		 * BAR0-3 in a few cases contain junk!
2018 		 */
2019 		if (class == PCI_CLASS_STORAGE_IDE) {
2020 			u8 progif;
2021 			pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
2022 			if ((progif & 1) == 0) {
2023 				region.start = 0x1F0;
2024 				region.end = 0x1F7;
2025 				res = &dev->resource[0];
2026 				res->flags = LEGACY_IO_RESOURCE;
2027 				pcibios_bus_to_resource(dev->bus, res, ®ion);
2028 				pci_info(dev, "BAR 0 %pR: legacy IDE quirk\n",
2029 					 res);
2030 				region.start = 0x3F6;
2031 				region.end = 0x3F6;
2032 				res = &dev->resource[1];
2033 				res->flags = LEGACY_IO_RESOURCE;
2034 				pcibios_bus_to_resource(dev->bus, res, ®ion);
2035 				pci_info(dev, "BAR 1 %pR: legacy IDE quirk\n",
2036 					 res);
2037 			}
2038 			if ((progif & 4) == 0) {
2039 				region.start = 0x170;
2040 				region.end = 0x177;
2041 				res = &dev->resource[2];
2042 				res->flags = LEGACY_IO_RESOURCE;
2043 				pcibios_bus_to_resource(dev->bus, res, ®ion);
2044 				pci_info(dev, "BAR 2 %pR: legacy IDE quirk\n",
2045 					 res);
2046 				region.start = 0x376;
2047 				region.end = 0x376;
2048 				res = &dev->resource[3];
2049 				res->flags = LEGACY_IO_RESOURCE;
2050 				pcibios_bus_to_resource(dev->bus, res, ®ion);
2051 				pci_info(dev, "BAR 3 %pR: legacy IDE quirk\n",
2052 					 res);
2053 			}
2054 		}
2055 		break;
2056 
2057 	case PCI_HEADER_TYPE_BRIDGE:		    /* bridge header */
2058 		/*
2059 		 * The PCI-to-PCI bridge spec requires that subtractive
2060 		 * decoding (i.e. transparent) bridge must have programming
2061 		 * interface code of 0x01.
2062 		 */
2063 		pci_read_irq(dev);
2064 		dev->transparent = ((dev->class & 0xff) == 1);
2065 		pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
2066 		pci_read_bridge_windows(dev);
2067 		set_pcie_hotplug_bridge(dev);
2068 		pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
2069 		if (pos) {
2070 			pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
2071 			pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
2072 		}
2073 		break;
2074 
2075 	case PCI_HEADER_TYPE_CARDBUS:		    /* CardBus bridge header */
2076 		if (class != PCI_CLASS_BRIDGE_CARDBUS)
2077 			goto bad;
2078 		pci_read_irq(dev);
2079 		pci_read_bases(dev, 1, 0);
2080 		pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
2081 		pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
2082 		break;
2083 
2084 	default:				    /* unknown header */
2085 		pci_err(dev, "unknown header type %02x, ignoring device\n",
2086 			dev->hdr_type);
2087 		pci_release_of_node(dev);
2088 		return -EIO;
2089 
2090 	bad:
2091 		pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
2092 			dev->class, dev->hdr_type);
2093 		dev->class = PCI_CLASS_NOT_DEFINED << 8;
2094 	}
2095 
2096 	/* We found a fine healthy device, go go go... */
2097 	return 0;
2098 }
2099 
pci_configure_mps(struct pci_dev * dev)2100 static void pci_configure_mps(struct pci_dev *dev)
2101 {
2102 	struct pci_dev *bridge = pci_upstream_bridge(dev);
2103 	int mps, mpss, p_mps, rc;
2104 
2105 	if (!pci_is_pcie(dev))
2106 		return;
2107 
2108 	/* MPS and MRRS fields are of type 'RsvdP' for VFs, short-circuit out */
2109 	if (dev->is_virtfn)
2110 		return;
2111 
2112 	/*
2113 	 * For Root Complex Integrated Endpoints, program the maximum
2114 	 * supported value unless limited by the PCIE_BUS_PEER2PEER case.
2115 	 */
2116 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) {
2117 		if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2118 			mps = 128;
2119 		else
2120 			mps = 128 << dev->pcie_mpss;
2121 		rc = pcie_set_mps(dev, mps);
2122 		if (rc) {
2123 			pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
2124 				 mps);
2125 		}
2126 		return;
2127 	}
2128 
2129 	if (!bridge || !pci_is_pcie(bridge))
2130 		return;
2131 
2132 	mps = pcie_get_mps(dev);
2133 	p_mps = pcie_get_mps(bridge);
2134 
2135 	if (mps == p_mps)
2136 		return;
2137 
2138 	if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
2139 		pci_warn(dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
2140 			 mps, pci_name(bridge), p_mps);
2141 		return;
2142 	}
2143 
2144 	/*
2145 	 * Fancier MPS configuration is done later by
2146 	 * pcie_bus_configure_settings()
2147 	 */
2148 	if (pcie_bus_config != PCIE_BUS_DEFAULT)
2149 		return;
2150 
2151 	mpss = 128 << dev->pcie_mpss;
2152 	if (mpss < p_mps && pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT) {
2153 		pcie_set_mps(bridge, mpss);
2154 		pci_info(dev, "Upstream bridge's Max Payload Size set to %d (was %d, max %d)\n",
2155 			 mpss, p_mps, 128 << bridge->pcie_mpss);
2156 		p_mps = pcie_get_mps(bridge);
2157 	}
2158 
2159 	rc = pcie_set_mps(dev, p_mps);
2160 	if (rc) {
2161 		pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
2162 			 p_mps);
2163 		return;
2164 	}
2165 
2166 	pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
2167 		 p_mps, mps, mpss);
2168 }
2169 
pci_configure_extended_tags(struct pci_dev * dev,void * ign)2170 int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
2171 {
2172 	struct pci_host_bridge *host;
2173 	u32 cap;
2174 	u16 ctl;
2175 	int ret;
2176 
2177 	if (!pci_is_pcie(dev))
2178 		return 0;
2179 
2180 	ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
2181 	if (ret)
2182 		return 0;
2183 
2184 	if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
2185 		return 0;
2186 
2187 	ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
2188 	if (ret)
2189 		return 0;
2190 
2191 	host = pci_find_host_bridge(dev->bus);
2192 	if (!host)
2193 		return 0;
2194 
2195 	/*
2196 	 * If some device in the hierarchy doesn't handle Extended Tags
2197 	 * correctly, make sure they're disabled.
2198 	 */
2199 	if (host->no_ext_tags) {
2200 		if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
2201 			pci_info(dev, "disabling Extended Tags\n");
2202 			pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
2203 						   PCI_EXP_DEVCTL_EXT_TAG);
2204 		}
2205 		return 0;
2206 	}
2207 
2208 	if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
2209 		pci_info(dev, "enabling Extended Tags\n");
2210 		pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
2211 					 PCI_EXP_DEVCTL_EXT_TAG);
2212 	}
2213 	return 0;
2214 }
2215 
2216 /**
2217  * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
2218  * @dev: PCI device to query
2219  *
2220  * Returns true if the device has enabled relaxed ordering attribute.
2221  */
pcie_relaxed_ordering_enabled(struct pci_dev * dev)2222 bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
2223 {
2224 	u16 v;
2225 
2226 	pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
2227 
2228 	return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
2229 }
2230 EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
2231 
pci_configure_relaxed_ordering(struct pci_dev * dev)2232 static void pci_configure_relaxed_ordering(struct pci_dev *dev)
2233 {
2234 	struct pci_dev *root;
2235 
2236 	/* PCI_EXP_DEVCTL_RELAX_EN is RsvdP in VFs */
2237 	if (dev->is_virtfn)
2238 		return;
2239 
2240 	if (!pcie_relaxed_ordering_enabled(dev))
2241 		return;
2242 
2243 	/*
2244 	 * For now, we only deal with Relaxed Ordering issues with Root
2245 	 * Ports. Peer-to-Peer DMA is another can of worms.
2246 	 */
2247 	root = pcie_find_root_port(dev);
2248 	if (!root)
2249 		return;
2250 
2251 	if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
2252 		pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
2253 					   PCI_EXP_DEVCTL_RELAX_EN);
2254 		pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
2255 	}
2256 }
2257 
pci_configure_eetlp_prefix(struct pci_dev * dev)2258 static void pci_configure_eetlp_prefix(struct pci_dev *dev)
2259 {
2260 #ifdef CONFIG_PCI_PASID
2261 	struct pci_dev *bridge;
2262 	int pcie_type;
2263 	u32 cap;
2264 
2265 	if (!pci_is_pcie(dev))
2266 		return;
2267 
2268 	pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
2269 	if (!(cap & PCI_EXP_DEVCAP2_EE_PREFIX))
2270 		return;
2271 
2272 	pcie_type = pci_pcie_type(dev);
2273 	if (pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
2274 	    pcie_type == PCI_EXP_TYPE_RC_END)
2275 		dev->eetlp_prefix_path = 1;
2276 	else {
2277 		bridge = pci_upstream_bridge(dev);
2278 		if (bridge && bridge->eetlp_prefix_path)
2279 			dev->eetlp_prefix_path = 1;
2280 	}
2281 #endif
2282 }
2283 
pci_configure_serr(struct pci_dev * dev)2284 static void pci_configure_serr(struct pci_dev *dev)
2285 {
2286 	u16 control;
2287 
2288 	if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
2289 
2290 		/*
2291 		 * A bridge will not forward ERR_ messages coming from an
2292 		 * endpoint unless SERR# forwarding is enabled.
2293 		 */
2294 		pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &control);
2295 		if (!(control & PCI_BRIDGE_CTL_SERR)) {
2296 			control |= PCI_BRIDGE_CTL_SERR;
2297 			pci_write_config_word(dev, PCI_BRIDGE_CONTROL, control);
2298 		}
2299 	}
2300 }
2301 
pci_configure_device(struct pci_dev * dev)2302 static void pci_configure_device(struct pci_dev *dev)
2303 {
2304 	pci_configure_mps(dev);
2305 	pci_configure_extended_tags(dev, NULL);
2306 	pci_configure_relaxed_ordering(dev);
2307 	pci_configure_ltr(dev);
2308 	pci_configure_aspm_l1ss(dev);
2309 	pci_configure_eetlp_prefix(dev);
2310 	pci_configure_serr(dev);
2311 
2312 	pci_acpi_program_hp_params(dev);
2313 }
2314 
pci_release_capabilities(struct pci_dev * dev)2315 static void pci_release_capabilities(struct pci_dev *dev)
2316 {
2317 	pci_aer_exit(dev);
2318 	pci_rcec_exit(dev);
2319 	pci_iov_release(dev);
2320 	pci_free_cap_save_buffers(dev);
2321 }
2322 
2323 /**
2324  * pci_release_dev - Free a PCI device structure when all users of it are
2325  *		     finished
2326  * @dev: device that's been disconnected
2327  *
2328  * Will be called only by the device core when all users of this PCI device are
2329  * done.
2330  */
pci_release_dev(struct device * dev)2331 static void pci_release_dev(struct device *dev)
2332 {
2333 	struct pci_dev *pci_dev;
2334 
2335 	pci_dev = to_pci_dev(dev);
2336 	pci_release_capabilities(pci_dev);
2337 	pci_release_of_node(pci_dev);
2338 	pcibios_release_device(pci_dev);
2339 	pci_bus_put(pci_dev->bus);
2340 	kfree(pci_dev->driver_override);
2341 	bitmap_free(pci_dev->dma_alias_mask);
2342 	dev_dbg(dev, "device released\n");
2343 	kfree(pci_dev);
2344 }
2345 
2346 static const struct device_type pci_dev_type = {
2347 	.groups = pci_dev_attr_groups,
2348 };
2349 
pci_alloc_dev(struct pci_bus * bus)2350 struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
2351 {
2352 	struct pci_dev *dev;
2353 
2354 	dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
2355 	if (!dev)
2356 		return NULL;
2357 
2358 	INIT_LIST_HEAD(&dev->bus_list);
2359 	dev->dev.type = &pci_dev_type;
2360 	dev->bus = pci_bus_get(bus);
2361 	dev->driver_exclusive_resource = (struct resource) {
2362 		.name = "PCI Exclusive",
2363 		.start = 0,
2364 		.end = -1,
2365 	};
2366 
2367 	spin_lock_init(&dev->pcie_cap_lock);
2368 #ifdef CONFIG_PCI_MSI
2369 	raw_spin_lock_init(&dev->msi_lock);
2370 #endif
2371 	return dev;
2372 }
2373 EXPORT_SYMBOL(pci_alloc_dev);
2374 
pci_bus_wait_rrs(struct pci_bus * bus,int devfn,u32 * l,int timeout)2375 static bool pci_bus_wait_rrs(struct pci_bus *bus, int devfn, u32 *l,
2376 			     int timeout)
2377 {
2378 	int delay = 1;
2379 
2380 	if (!pci_bus_rrs_vendor_id(*l))
2381 		return true;	/* not a Configuration RRS completion */
2382 
2383 	if (!timeout)
2384 		return false;	/* RRS, but caller doesn't want to wait */
2385 
2386 	/*
2387 	 * We got the reserved Vendor ID that indicates a completion with
2388 	 * Configuration Request Retry Status (RRS).  Retry until we get a
2389 	 * valid Vendor ID or we time out.
2390 	 */
2391 	while (pci_bus_rrs_vendor_id(*l)) {
2392 		if (delay > timeout) {
2393 			pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
2394 				pci_domain_nr(bus), bus->number,
2395 				PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2396 
2397 			return false;
2398 		}
2399 		if (delay >= 1000)
2400 			pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
2401 				pci_domain_nr(bus), bus->number,
2402 				PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2403 
2404 		msleep(delay);
2405 		delay *= 2;
2406 
2407 		if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2408 			return false;
2409 	}
2410 
2411 	if (delay >= 1000)
2412 		pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
2413 			pci_domain_nr(bus), bus->number,
2414 			PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2415 
2416 	return true;
2417 }
2418 
pci_bus_generic_read_dev_vendor_id(struct pci_bus * bus,int devfn,u32 * l,int timeout)2419 bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2420 					int timeout)
2421 {
2422 	if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2423 		return false;
2424 
2425 	/* Some broken boards return 0 or ~0 (PCI_ERROR_RESPONSE) if a slot is empty: */
2426 	if (PCI_POSSIBLE_ERROR(*l) || *l == 0x00000000 ||
2427 	    *l == 0x0000ffff || *l == 0xffff0000)
2428 		return false;
2429 
2430 	if (pci_bus_rrs_vendor_id(*l))
2431 		return pci_bus_wait_rrs(bus, devfn, l, timeout);
2432 
2433 	return true;
2434 }
2435 
pci_bus_read_dev_vendor_id(struct pci_bus * bus,int devfn,u32 * l,int timeout)2436 bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2437 				int timeout)
2438 {
2439 #ifdef CONFIG_PCI_QUIRKS
2440 	struct pci_dev *bridge = bus->self;
2441 
2442 	/*
2443 	 * Certain IDT switches have an issue where they improperly trigger
2444 	 * ACS Source Validation errors on completions for config reads.
2445 	 */
2446 	if (bridge && bridge->vendor == PCI_VENDOR_ID_IDT &&
2447 	    bridge->device == 0x80b5)
2448 		return pci_idt_bus_quirk(bus, devfn, l, timeout);
2449 #endif
2450 
2451 	return pci_bus_generic_read_dev_vendor_id(bus, devfn, l, timeout);
2452 }
2453 EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2454 
pci_pwrctrl_create_device(struct pci_bus * bus,int devfn)2455 static struct platform_device *pci_pwrctrl_create_device(struct pci_bus *bus, int devfn)
2456 {
2457 	struct pci_host_bridge *host = pci_find_host_bridge(bus);
2458 	struct platform_device *pdev;
2459 	struct device_node *np;
2460 
2461 	np = of_pci_find_child_device(dev_of_node(&bus->dev), devfn);
2462 	if (!np || of_find_device_by_node(np))
2463 		return NULL;
2464 
2465 	/*
2466 	 * First check whether the pwrctrl device really needs to be created or
2467 	 * not. This is decided based on at least one of the power supplies
2468 	 * being defined in the devicetree node of the device.
2469 	 */
2470 	if (!of_pci_supply_present(np)) {
2471 		pr_debug("PCI/pwrctrl: Skipping OF node: %s\n", np->name);
2472 		return NULL;
2473 	}
2474 
2475 	/* Now create the pwrctrl device */
2476 	pdev = of_platform_device_create(np, NULL, &host->dev);
2477 	if (!pdev) {
2478 		pr_err("PCI/pwrctrl: Failed to create pwrctrl device for node: %s\n", np->name);
2479 		return NULL;
2480 	}
2481 
2482 	return pdev;
2483 }
2484 
2485 /*
2486  * Read the config data for a PCI device, sanity-check it,
2487  * and fill in the dev structure.
2488  */
pci_scan_device(struct pci_bus * bus,int devfn)2489 static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
2490 {
2491 	struct pci_dev *dev;
2492 	u32 l;
2493 
2494 	/*
2495 	 * Create pwrctrl device (if required) for the PCI device to handle the
2496 	 * power state. If the pwrctrl device is created, then skip scanning
2497 	 * further as the pwrctrl core will rescan the bus after powering on
2498 	 * the device.
2499 	 */
2500 	if (pci_pwrctrl_create_device(bus, devfn))
2501 		return NULL;
2502 
2503 	if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
2504 		return NULL;
2505 
2506 	dev = pci_alloc_dev(bus);
2507 	if (!dev)
2508 		return NULL;
2509 
2510 	dev->devfn = devfn;
2511 	dev->vendor = l & 0xffff;
2512 	dev->device = (l >> 16) & 0xffff;
2513 
2514 	if (pci_setup_device(dev)) {
2515 		pci_bus_put(dev->bus);
2516 		kfree(dev);
2517 		return NULL;
2518 	}
2519 
2520 	return dev;
2521 }
2522 
pcie_report_downtraining(struct pci_dev * dev)2523 void pcie_report_downtraining(struct pci_dev *dev)
2524 {
2525 	if (!pci_is_pcie(dev))
2526 		return;
2527 
2528 	/* Look from the device up to avoid downstream ports with no devices */
2529 	if ((pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT) &&
2530 	    (pci_pcie_type(dev) != PCI_EXP_TYPE_LEG_END) &&
2531 	    (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM))
2532 		return;
2533 
2534 	/* Multi-function PCIe devices share the same link/status */
2535 	if (PCI_FUNC(dev->devfn) != 0 || dev->is_virtfn)
2536 		return;
2537 
2538 	/* Print link status only if the device is constrained by the fabric */
2539 	__pcie_print_link_status(dev, false);
2540 }
2541 
pci_init_capabilities(struct pci_dev * dev)2542 static void pci_init_capabilities(struct pci_dev *dev)
2543 {
2544 	pci_ea_init(dev);		/* Enhanced Allocation */
2545 	pci_msi_init(dev);		/* Disable MSI */
2546 	pci_msix_init(dev);		/* Disable MSI-X */
2547 
2548 	/* Buffers for saving PCIe and PCI-X capabilities */
2549 	pci_allocate_cap_save_buffers(dev);
2550 
2551 	pci_pm_init(dev);		/* Power Management */
2552 	pci_vpd_init(dev);		/* Vital Product Data */
2553 	pci_configure_ari(dev);		/* Alternative Routing-ID Forwarding */
2554 	pci_iov_init(dev);		/* Single Root I/O Virtualization */
2555 	pci_ats_init(dev);		/* Address Translation Services */
2556 	pci_pri_init(dev);		/* Page Request Interface */
2557 	pci_pasid_init(dev);		/* Process Address Space ID */
2558 	pci_acs_init(dev);		/* Access Control Services */
2559 	pci_ptm_init(dev);		/* Precision Time Measurement */
2560 	pci_aer_init(dev);		/* Advanced Error Reporting */
2561 	pci_dpc_init(dev);		/* Downstream Port Containment */
2562 	pci_rcec_init(dev);		/* Root Complex Event Collector */
2563 	pci_doe_init(dev);		/* Data Object Exchange */
2564 
2565 	pcie_report_downtraining(dev);
2566 	pci_init_reset_methods(dev);
2567 }
2568 
2569 /*
2570  * This is the equivalent of pci_host_bridge_msi_domain() that acts on
2571  * devices. Firmware interfaces that can select the MSI domain on a
2572  * per-device basis should be called from here.
2573  */
pci_dev_msi_domain(struct pci_dev * dev)2574 static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2575 {
2576 	struct irq_domain *d;
2577 
2578 	/*
2579 	 * If a domain has been set through the pcibios_device_add()
2580 	 * callback, then this is the one (platform code knows best).
2581 	 */
2582 	d = dev_get_msi_domain(&dev->dev);
2583 	if (d)
2584 		return d;
2585 
2586 	/*
2587 	 * Let's see if we have a firmware interface able to provide
2588 	 * the domain.
2589 	 */
2590 	d = pci_msi_get_device_domain(dev);
2591 	if (d)
2592 		return d;
2593 
2594 	return NULL;
2595 }
2596 
pci_set_msi_domain(struct pci_dev * dev)2597 static void pci_set_msi_domain(struct pci_dev *dev)
2598 {
2599 	struct irq_domain *d;
2600 
2601 	/*
2602 	 * If the platform or firmware interfaces cannot supply a
2603 	 * device-specific MSI domain, then inherit the default domain
2604 	 * from the host bridge itself.
2605 	 */
2606 	d = pci_dev_msi_domain(dev);
2607 	if (!d)
2608 		d = dev_get_msi_domain(&dev->bus->dev);
2609 
2610 	dev_set_msi_domain(&dev->dev, d);
2611 }
2612 
pci_device_add(struct pci_dev * dev,struct pci_bus * bus)2613 void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
2614 {
2615 	int ret;
2616 
2617 	pci_configure_device(dev);
2618 
2619 	device_initialize(&dev->dev);
2620 	dev->dev.release = pci_release_dev;
2621 
2622 	set_dev_node(&dev->dev, pcibus_to_node(bus));
2623 	dev->dev.dma_mask = &dev->dma_mask;
2624 	dev->dev.dma_parms = &dev->dma_parms;
2625 	dev->dev.coherent_dma_mask = 0xffffffffull;
2626 
2627 	dma_set_max_seg_size(&dev->dev, 65536);
2628 	dma_set_seg_boundary(&dev->dev, 0xffffffff);
2629 
2630 	pcie_failed_link_retrain(dev);
2631 
2632 	/* Fix up broken headers */
2633 	pci_fixup_device(pci_fixup_header, dev);
2634 
2635 	pci_reassigndev_resource_alignment(dev);
2636 
2637 	dev->state_saved = false;
2638 
2639 	pci_init_capabilities(dev);
2640 
2641 	/*
2642 	 * Add the device to our list of discovered devices
2643 	 * and the bus list for fixup functions, etc.
2644 	 */
2645 	down_write(&pci_bus_sem);
2646 	list_add_tail(&dev->bus_list, &bus->devices);
2647 	up_write(&pci_bus_sem);
2648 
2649 	ret = pcibios_device_add(dev);
2650 	WARN_ON(ret < 0);
2651 
2652 	/* Set up MSI IRQ domain */
2653 	pci_set_msi_domain(dev);
2654 
2655 	/* Notifier could use PCI capabilities */
2656 	dev->match_driver = false;
2657 	ret = device_add(&dev->dev);
2658 	WARN_ON(ret < 0);
2659 
2660 	pci_npem_create(dev);
2661 }
2662 
pci_scan_single_device(struct pci_bus * bus,int devfn)2663 struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
2664 {
2665 	struct pci_dev *dev;
2666 
2667 	dev = pci_get_slot(bus, devfn);
2668 	if (dev) {
2669 		pci_dev_put(dev);
2670 		return dev;
2671 	}
2672 
2673 	dev = pci_scan_device(bus, devfn);
2674 	if (!dev)
2675 		return NULL;
2676 
2677 	pci_device_add(dev, bus);
2678 
2679 	return dev;
2680 }
2681 EXPORT_SYMBOL(pci_scan_single_device);
2682 
next_ari_fn(struct pci_bus * bus,struct pci_dev * dev,int fn)2683 static int next_ari_fn(struct pci_bus *bus, struct pci_dev *dev, int fn)
2684 {
2685 	int pos;
2686 	u16 cap = 0;
2687 	unsigned int next_fn;
2688 
2689 	if (!dev)
2690 		return -ENODEV;
2691 
2692 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2693 	if (!pos)
2694 		return -ENODEV;
2695 
2696 	pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2697 	next_fn = PCI_ARI_CAP_NFN(cap);
2698 	if (next_fn <= fn)
2699 		return -ENODEV;	/* protect against malformed list */
2700 
2701 	return next_fn;
2702 }
2703 
next_fn(struct pci_bus * bus,struct pci_dev * dev,int fn)2704 static int next_fn(struct pci_bus *bus, struct pci_dev *dev, int fn)
2705 {
2706 	if (pci_ari_enabled(bus))
2707 		return next_ari_fn(bus, dev, fn);
2708 
2709 	if (fn >= 7)
2710 		return -ENODEV;
2711 	/* only multifunction devices may have more functions */
2712 	if (dev && !dev->multifunction)
2713 		return -ENODEV;
2714 
2715 	return fn + 1;
2716 }
2717 
only_one_child(struct pci_bus * bus)2718 static int only_one_child(struct pci_bus *bus)
2719 {
2720 	struct pci_dev *bridge = bus->self;
2721 
2722 	/*
2723 	 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
2724 	 * we scan for all possible devices, not just Device 0.
2725 	 */
2726 	if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
2727 		return 0;
2728 
2729 	/*
2730 	 * A PCIe Downstream Port normally leads to a Link with only Device
2731 	 * 0 on it (PCIe spec r3.1, sec 7.3.1).  As an optimization, scan
2732 	 * only for Device 0 in that situation.
2733 	 */
2734 	if (bridge && pci_is_pcie(bridge) && pcie_downstream_port(bridge))
2735 		return 1;
2736 
2737 	return 0;
2738 }
2739 
2740 /**
2741  * pci_scan_slot - Scan a PCI slot on a bus for devices
2742  * @bus: PCI bus to scan
2743  * @devfn: slot number to scan (must have zero function)
2744  *
2745  * Scan a PCI slot on the specified PCI bus for devices, adding
2746  * discovered devices to the @bus->devices list.  New devices
2747  * will not have is_added set.
2748  *
2749  * Returns the number of new devices found.
2750  */
pci_scan_slot(struct pci_bus * bus,int devfn)2751 int pci_scan_slot(struct pci_bus *bus, int devfn)
2752 {
2753 	struct pci_dev *dev;
2754 	int fn = 0, nr = 0;
2755 
2756 	if (only_one_child(bus) && (devfn > 0))
2757 		return 0; /* Already scanned the entire slot */
2758 
2759 	do {
2760 		dev = pci_scan_single_device(bus, devfn + fn);
2761 		if (dev) {
2762 			if (!pci_dev_is_added(dev))
2763 				nr++;
2764 			if (fn > 0)
2765 				dev->multifunction = 1;
2766 		} else if (fn == 0) {
2767 			/*
2768 			 * Function 0 is required unless we are running on
2769 			 * a hypervisor that passes through individual PCI
2770 			 * functions.
2771 			 */
2772 			if (!hypervisor_isolated_pci_functions())
2773 				break;
2774 		}
2775 		fn = next_fn(bus, dev, fn);
2776 	} while (fn >= 0);
2777 
2778 	/* Only one slot has PCIe device */
2779 	if (bus->self && nr)
2780 		pcie_aspm_init_link_state(bus->self);
2781 
2782 	return nr;
2783 }
2784 EXPORT_SYMBOL(pci_scan_slot);
2785 
pcie_find_smpss(struct pci_dev * dev,void * data)2786 static int pcie_find_smpss(struct pci_dev *dev, void *data)
2787 {
2788 	u8 *smpss = data;
2789 
2790 	if (!pci_is_pcie(dev))
2791 		return 0;
2792 
2793 	/*
2794 	 * We don't have a way to change MPS settings on devices that have
2795 	 * drivers attached.  A hot-added device might support only the minimum
2796 	 * MPS setting (MPS=128).  Therefore, if the fabric contains a bridge
2797 	 * where devices may be hot-added, we limit the fabric MPS to 128 so
2798 	 * hot-added devices will work correctly.
2799 	 *
2800 	 * However, if we hot-add a device to a slot directly below a Root
2801 	 * Port, it's impossible for there to be other existing devices below
2802 	 * the port.  We don't limit the MPS in this case because we can
2803 	 * reconfigure MPS on both the Root Port and the hot-added device,
2804 	 * and there are no other devices involved.
2805 	 *
2806 	 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
2807 	 */
2808 	if (dev->is_hotplug_bridge &&
2809 	    pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
2810 		*smpss = 0;
2811 
2812 	if (*smpss > dev->pcie_mpss)
2813 		*smpss = dev->pcie_mpss;
2814 
2815 	return 0;
2816 }
2817 
pcie_write_mps(struct pci_dev * dev,int mps)2818 static void pcie_write_mps(struct pci_dev *dev, int mps)
2819 {
2820 	int rc;
2821 
2822 	if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
2823 		mps = 128 << dev->pcie_mpss;
2824 
2825 		if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2826 		    dev->bus->self)
2827 
2828 			/*
2829 			 * For "Performance", the assumption is made that
2830 			 * downstream communication will never be larger than
2831 			 * the MRRS.  So, the MPS only needs to be configured
2832 			 * for the upstream communication.  This being the case,
2833 			 * walk from the top down and set the MPS of the child
2834 			 * to that of the parent bus.
2835 			 *
2836 			 * Configure the device MPS with the smaller of the
2837 			 * device MPSS or the bridge MPS (which is assumed to be
2838 			 * properly configured at this point to the largest
2839 			 * allowable MPS based on its parent bus).
2840 			 */
2841 			mps = min(mps, pcie_get_mps(dev->bus->self));
2842 	}
2843 
2844 	rc = pcie_set_mps(dev, mps);
2845 	if (rc)
2846 		pci_err(dev, "Failed attempting to set the MPS\n");
2847 }
2848 
pcie_write_mrrs(struct pci_dev * dev)2849 static void pcie_write_mrrs(struct pci_dev *dev)
2850 {
2851 	int rc, mrrs;
2852 
2853 	/*
2854 	 * In the "safe" case, do not configure the MRRS.  There appear to be
2855 	 * issues with setting MRRS to 0 on a number of devices.
2856 	 */
2857 	if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2858 		return;
2859 
2860 	/*
2861 	 * For max performance, the MRRS must be set to the largest supported
2862 	 * value.  However, it cannot be configured larger than the MPS the
2863 	 * device or the bus can support.  This should already be properly
2864 	 * configured by a prior call to pcie_write_mps().
2865 	 */
2866 	mrrs = pcie_get_mps(dev);
2867 
2868 	/*
2869 	 * MRRS is a R/W register.  Invalid values can be written, but a
2870 	 * subsequent read will verify if the value is acceptable or not.
2871 	 * If the MRRS value provided is not acceptable (e.g., too large),
2872 	 * shrink the value until it is acceptable to the HW.
2873 	 */
2874 	while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2875 		rc = pcie_set_readrq(dev, mrrs);
2876 		if (!rc)
2877 			break;
2878 
2879 		pci_warn(dev, "Failed attempting to set the MRRS\n");
2880 		mrrs /= 2;
2881 	}
2882 
2883 	if (mrrs < 128)
2884 		pci_err(dev, "MRRS was unable to be configured with a safe value.  If problems are experienced, try running with pci=pcie_bus_safe\n");
2885 }
2886 
pcie_bus_configure_set(struct pci_dev * dev,void * data)2887 static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2888 {
2889 	int mps, orig_mps;
2890 
2891 	if (!pci_is_pcie(dev))
2892 		return 0;
2893 
2894 	if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2895 	    pcie_bus_config == PCIE_BUS_DEFAULT)
2896 		return 0;
2897 
2898 	mps = 128 << *(u8 *)data;
2899 	orig_mps = pcie_get_mps(dev);
2900 
2901 	pcie_write_mps(dev, mps);
2902 	pcie_write_mrrs(dev);
2903 
2904 	pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
2905 		 pcie_get_mps(dev), 128 << dev->pcie_mpss,
2906 		 orig_mps, pcie_get_readrq(dev));
2907 
2908 	return 0;
2909 }
2910 
2911 /*
2912  * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
2913  * parents then children fashion.  If this changes, then this code will not
2914  * work as designed.
2915  */
pcie_bus_configure_settings(struct pci_bus * bus)2916 void pcie_bus_configure_settings(struct pci_bus *bus)
2917 {
2918 	u8 smpss = 0;
2919 
2920 	if (!bus->self)
2921 		return;
2922 
2923 	if (!pci_is_pcie(bus->self))
2924 		return;
2925 
2926 	/*
2927 	 * FIXME - Peer to peer DMA is possible, though the endpoint would need
2928 	 * to be aware of the MPS of the destination.  To work around this,
2929 	 * simply force the MPS of the entire system to the smallest possible.
2930 	 */
2931 	if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2932 		smpss = 0;
2933 
2934 	if (pcie_bus_config == PCIE_BUS_SAFE) {
2935 		smpss = bus->self->pcie_mpss;
2936 
2937 		pcie_find_smpss(bus->self, &smpss);
2938 		pci_walk_bus(bus, pcie_find_smpss, &smpss);
2939 	}
2940 
2941 	pcie_bus_configure_set(bus->self, &smpss);
2942 	pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2943 }
2944 EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
2945 
2946 /*
2947  * Called after each bus is probed, but before its children are examined.  This
2948  * is marked as __weak because multiple architectures define it.
2949  */
pcibios_fixup_bus(struct pci_bus * bus)2950 void __weak pcibios_fixup_bus(struct pci_bus *bus)
2951 {
2952        /* nothing to do, expected to be removed in the future */
2953 }
2954 
2955 /**
2956  * pci_scan_child_bus_extend() - Scan devices below a bus
2957  * @bus: Bus to scan for devices
2958  * @available_buses: Total number of buses available (%0 does not try to
2959  *		     extend beyond the minimal)
2960  *
2961  * Scans devices below @bus including subordinate buses. Returns new
2962  * subordinate number including all the found devices. Passing
2963  * @available_buses causes the remaining bus space to be distributed
2964  * equally between hotplug-capable bridges to allow future extension of the
2965  * hierarchy.
2966  */
pci_scan_child_bus_extend(struct pci_bus * bus,unsigned int available_buses)2967 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2968 					      unsigned int available_buses)
2969 {
2970 	unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2971 	unsigned int start = bus->busn_res.start;
2972 	unsigned int devfn, cmax, max = start;
2973 	struct pci_dev *dev;
2974 
2975 	dev_dbg(&bus->dev, "scanning bus\n");
2976 
2977 	/* Go find them, Rover! */
2978 	for (devfn = 0; devfn < 256; devfn += 8)
2979 		pci_scan_slot(bus, devfn);
2980 
2981 	/* Reserve buses for SR-IOV capability */
2982 	used_buses = pci_iov_bus_range(bus);
2983 	max += used_buses;
2984 
2985 	/*
2986 	 * After performing arch-dependent fixup of the bus, look behind
2987 	 * all PCI-to-PCI bridges on this bus.
2988 	 */
2989 	if (!bus->is_added) {
2990 		dev_dbg(&bus->dev, "fixups for bus\n");
2991 		pcibios_fixup_bus(bus);
2992 		bus->is_added = 1;
2993 	}
2994 
2995 	/*
2996 	 * Calculate how many hotplug bridges and normal bridges there
2997 	 * are on this bus. We will distribute the additional available
2998 	 * buses between hotplug bridges.
2999 	 */
3000 	for_each_pci_bridge(dev, bus) {
3001 		if (dev->is_hotplug_bridge)
3002 			hotplug_bridges++;
3003 		else
3004 			normal_bridges++;
3005 	}
3006 
3007 	/*
3008 	 * Scan bridges that are already configured. We don't touch them
3009 	 * unless they are misconfigured (which will be done in the second
3010 	 * scan below).
3011 	 */
3012 	for_each_pci_bridge(dev, bus) {
3013 		cmax = max;
3014 		max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
3015 
3016 		/*
3017 		 * Reserve one bus for each bridge now to avoid extending
3018 		 * hotplug bridges too much during the second scan below.
3019 		 */
3020 		used_buses++;
3021 		if (max - cmax > 1)
3022 			used_buses += max - cmax - 1;
3023 	}
3024 
3025 	/* Scan bridges that need to be reconfigured */
3026 	for_each_pci_bridge(dev, bus) {
3027 		unsigned int buses = 0;
3028 
3029 		if (!hotplug_bridges && normal_bridges == 1) {
3030 			/*
3031 			 * There is only one bridge on the bus (upstream
3032 			 * port) so it gets all available buses which it
3033 			 * can then distribute to the possible hotplug
3034 			 * bridges below.
3035 			 */
3036 			buses = available_buses;
3037 		} else if (dev->is_hotplug_bridge) {
3038 			/*
3039 			 * Distribute the extra buses between hotplug
3040 			 * bridges if any.
3041 			 */
3042 			buses = available_buses / hotplug_bridges;
3043 			buses = min(buses, available_buses - used_buses + 1);
3044 		}
3045 
3046 		cmax = max;
3047 		max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
3048 		/* One bus is already accounted so don't add it again */
3049 		if (max - cmax > 1)
3050 			used_buses += max - cmax - 1;
3051 	}
3052 
3053 	/*
3054 	 * Make sure a hotplug bridge has at least the minimum requested
3055 	 * number of buses but allow it to grow up to the maximum available
3056 	 * bus number if there is room.
3057 	 */
3058 	if (bus->self && bus->self->is_hotplug_bridge) {
3059 		used_buses = max_t(unsigned int, available_buses,
3060 				   pci_hotplug_bus_size - 1);
3061 		if (max - start < used_buses) {
3062 			max = start + used_buses;
3063 
3064 			/* Do not allocate more buses than we have room left */
3065 			if (max > bus->busn_res.end)
3066 				max = bus->busn_res.end;
3067 
3068 			dev_dbg(&bus->dev, "%pR extended by %#02x\n",
3069 				&bus->busn_res, max - start);
3070 		}
3071 	}
3072 
3073 	/*
3074 	 * We've scanned the bus and so we know all about what's on
3075 	 * the other side of any bridges that may be on this bus plus
3076 	 * any devices.
3077 	 *
3078 	 * Return how far we've got finding sub-buses.
3079 	 */
3080 	dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
3081 	return max;
3082 }
3083 
3084 /**
3085  * pci_scan_child_bus() - Scan devices below a bus
3086  * @bus: Bus to scan for devices
3087  *
3088  * Scans devices below @bus including subordinate buses. Returns new
3089  * subordinate number including all the found devices.
3090  */
pci_scan_child_bus(struct pci_bus * bus)3091 unsigned int pci_scan_child_bus(struct pci_bus *bus)
3092 {
3093 	return pci_scan_child_bus_extend(bus, 0);
3094 }
3095 EXPORT_SYMBOL_GPL(pci_scan_child_bus);
3096 
3097 /**
3098  * pcibios_root_bridge_prepare - Platform-specific host bridge setup
3099  * @bridge: Host bridge to set up
3100  *
3101  * Default empty implementation.  Replace with an architecture-specific setup
3102  * routine, if necessary.
3103  */
pcibios_root_bridge_prepare(struct pci_host_bridge * bridge)3104 int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
3105 {
3106 	return 0;
3107 }
3108 
pcibios_add_bus(struct pci_bus * bus)3109 void __weak pcibios_add_bus(struct pci_bus *bus)
3110 {
3111 }
3112 
pcibios_remove_bus(struct pci_bus * bus)3113 void __weak pcibios_remove_bus(struct pci_bus *bus)
3114 {
3115 }
3116 
pci_create_root_bus(struct device * parent,int bus,struct pci_ops * ops,void * sysdata,struct list_head * resources)3117 struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
3118 		struct pci_ops *ops, void *sysdata, struct list_head *resources)
3119 {
3120 	int error;
3121 	struct pci_host_bridge *bridge;
3122 
3123 	bridge = pci_alloc_host_bridge(0);
3124 	if (!bridge)
3125 		return NULL;
3126 
3127 	bridge->dev.parent = parent;
3128 
3129 	list_splice_init(resources, &bridge->windows);
3130 	bridge->sysdata = sysdata;
3131 	bridge->busnr = bus;
3132 	bridge->ops = ops;
3133 
3134 	error = pci_register_host_bridge(bridge);
3135 	if (error < 0)
3136 		goto err_out;
3137 
3138 	return bridge->bus;
3139 
3140 err_out:
3141 	put_device(&bridge->dev);
3142 	return NULL;
3143 }
3144 EXPORT_SYMBOL_GPL(pci_create_root_bus);
3145 
pci_host_probe(struct pci_host_bridge * bridge)3146 int pci_host_probe(struct pci_host_bridge *bridge)
3147 {
3148 	struct pci_bus *bus, *child;
3149 	int ret;
3150 
3151 	pci_lock_rescan_remove();
3152 	ret = pci_scan_root_bus_bridge(bridge);
3153 	pci_unlock_rescan_remove();
3154 	if (ret < 0) {
3155 		dev_err(bridge->dev.parent, "Scanning root bridge failed");
3156 		return ret;
3157 	}
3158 
3159 	bus = bridge->bus;
3160 
3161 	/* If we must preserve the resource configuration, claim now */
3162 	if (bridge->preserve_config)
3163 		pci_bus_claim_resources(bus);
3164 
3165 	/*
3166 	 * Assign whatever was left unassigned. If we didn't claim above,
3167 	 * this will reassign everything.
3168 	 */
3169 	pci_assign_unassigned_root_bus_resources(bus);
3170 
3171 	list_for_each_entry(child, &bus->children, node)
3172 		pcie_bus_configure_settings(child);
3173 
3174 	pci_lock_rescan_remove();
3175 	pci_bus_add_devices(bus);
3176 	pci_unlock_rescan_remove();
3177 
3178 	/*
3179 	 * Ensure pm_runtime_enable() is called for the controller drivers
3180 	 * before calling pci_host_probe(). The PM framework expects that
3181 	 * if the parent device supports runtime PM, it will be enabled
3182 	 * before child runtime PM is enabled.
3183 	 */
3184 	pm_runtime_set_active(&bridge->dev);
3185 	pm_runtime_no_callbacks(&bridge->dev);
3186 	devm_pm_runtime_enable(&bridge->dev);
3187 
3188 	return 0;
3189 }
3190 EXPORT_SYMBOL_GPL(pci_host_probe);
3191 
pci_bus_insert_busn_res(struct pci_bus * b,int bus,int bus_max)3192 int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
3193 {
3194 	struct resource *res = &b->busn_res;
3195 	struct resource *parent_res, *conflict;
3196 
3197 	res->start = bus;
3198 	res->end = bus_max;
3199 	res->flags = IORESOURCE_BUS;
3200 
3201 	if (!pci_is_root_bus(b))
3202 		parent_res = &b->parent->busn_res;
3203 	else {
3204 		parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
3205 		res->flags |= IORESOURCE_PCI_FIXED;
3206 	}
3207 
3208 	conflict = request_resource_conflict(parent_res, res);
3209 
3210 	if (conflict)
3211 		dev_info(&b->dev,
3212 			   "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
3213 			    res, pci_is_root_bus(b) ? "domain " : "",
3214 			    parent_res, conflict->name, conflict);
3215 
3216 	return conflict == NULL;
3217 }
3218 
pci_bus_update_busn_res_end(struct pci_bus * b,int bus_max)3219 int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
3220 {
3221 	struct resource *res = &b->busn_res;
3222 	struct resource old_res = *res;
3223 	resource_size_t size;
3224 	int ret;
3225 
3226 	if (res->start > bus_max)
3227 		return -EINVAL;
3228 
3229 	size = bus_max - res->start + 1;
3230 	ret = adjust_resource(res, res->start, size);
3231 	dev_info(&b->dev, "busn_res: %pR end %s updated to %02x\n",
3232 			&old_res, ret ? "can not be" : "is", bus_max);
3233 
3234 	if (!ret && !res->parent)
3235 		pci_bus_insert_busn_res(b, res->start, res->end);
3236 
3237 	return ret;
3238 }
3239 
pci_bus_release_busn_res(struct pci_bus * b)3240 void pci_bus_release_busn_res(struct pci_bus *b)
3241 {
3242 	struct resource *res = &b->busn_res;
3243 	int ret;
3244 
3245 	if (!res->flags || !res->parent)
3246 		return;
3247 
3248 	ret = release_resource(res);
3249 	dev_info(&b->dev, "busn_res: %pR %s released\n",
3250 			res, ret ? "can not be" : "is");
3251 }
3252 
pci_scan_root_bus_bridge(struct pci_host_bridge * bridge)3253 int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
3254 {
3255 	struct resource_entry *window;
3256 	bool found = false;
3257 	struct pci_bus *b;
3258 	int max, bus, ret;
3259 
3260 	if (!bridge)
3261 		return -EINVAL;
3262 
3263 	resource_list_for_each_entry(window, &bridge->windows)
3264 		if (window->res->flags & IORESOURCE_BUS) {
3265 			bridge->busnr = window->res->start;
3266 			found = true;
3267 			break;
3268 		}
3269 
3270 	ret = pci_register_host_bridge(bridge);
3271 	if (ret < 0)
3272 		return ret;
3273 
3274 	b = bridge->bus;
3275 	bus = bridge->busnr;
3276 
3277 	if (!found) {
3278 		dev_info(&b->dev,
3279 		 "No busn resource found for root bus, will use [bus %02x-ff]\n",
3280 			bus);
3281 		pci_bus_insert_busn_res(b, bus, 255);
3282 	}
3283 
3284 	max = pci_scan_child_bus(b);
3285 
3286 	if (!found)
3287 		pci_bus_update_busn_res_end(b, max);
3288 
3289 	return 0;
3290 }
3291 EXPORT_SYMBOL(pci_scan_root_bus_bridge);
3292 
pci_scan_root_bus(struct device * parent,int bus,struct pci_ops * ops,void * sysdata,struct list_head * resources)3293 struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
3294 		struct pci_ops *ops, void *sysdata, struct list_head *resources)
3295 {
3296 	struct resource_entry *window;
3297 	bool found = false;
3298 	struct pci_bus *b;
3299 	int max;
3300 
3301 	resource_list_for_each_entry(window, resources)
3302 		if (window->res->flags & IORESOURCE_BUS) {
3303 			found = true;
3304 			break;
3305 		}
3306 
3307 	b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
3308 	if (!b)
3309 		return NULL;
3310 
3311 	if (!found) {
3312 		dev_info(&b->dev,
3313 		 "No busn resource found for root bus, will use [bus %02x-ff]\n",
3314 			bus);
3315 		pci_bus_insert_busn_res(b, bus, 255);
3316 	}
3317 
3318 	max = pci_scan_child_bus(b);
3319 
3320 	if (!found)
3321 		pci_bus_update_busn_res_end(b, max);
3322 
3323 	return b;
3324 }
3325 EXPORT_SYMBOL(pci_scan_root_bus);
3326 
pci_scan_bus(int bus,struct pci_ops * ops,void * sysdata)3327 struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
3328 					void *sysdata)
3329 {
3330 	LIST_HEAD(resources);
3331 	struct pci_bus *b;
3332 
3333 	pci_add_resource(&resources, &ioport_resource);
3334 	pci_add_resource(&resources, &iomem_resource);
3335 	pci_add_resource(&resources, &busn_resource);
3336 	b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
3337 	if (b) {
3338 		pci_scan_child_bus(b);
3339 	} else {
3340 		pci_free_resource_list(&resources);
3341 	}
3342 	return b;
3343 }
3344 EXPORT_SYMBOL(pci_scan_bus);
3345 
3346 /**
3347  * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
3348  * @bridge: PCI bridge for the bus to scan
3349  *
3350  * Scan a PCI bus and child buses for new devices, add them,
3351  * and enable them, resizing bridge mmio/io resource if necessary
3352  * and possible.  The caller must ensure the child devices are already
3353  * removed for resizing to occur.
3354  *
3355  * Returns the max number of subordinate bus discovered.
3356  */
pci_rescan_bus_bridge_resize(struct pci_dev * bridge)3357 unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
3358 {
3359 	unsigned int max;
3360 	struct pci_bus *bus = bridge->subordinate;
3361 
3362 	max = pci_scan_child_bus(bus);
3363 
3364 	pci_assign_unassigned_bridge_resources(bridge);
3365 
3366 	pci_bus_add_devices(bus);
3367 
3368 	return max;
3369 }
3370 
3371 /**
3372  * pci_rescan_bus - Scan a PCI bus for devices
3373  * @bus: PCI bus to scan
3374  *
3375  * Scan a PCI bus and child buses for new devices, add them,
3376  * and enable them.
3377  *
3378  * Returns the max number of subordinate bus discovered.
3379  */
pci_rescan_bus(struct pci_bus * bus)3380 unsigned int pci_rescan_bus(struct pci_bus *bus)
3381 {
3382 	unsigned int max;
3383 
3384 	max = pci_scan_child_bus(bus);
3385 	pci_assign_unassigned_bus_resources(bus);
3386 	pci_bus_add_devices(bus);
3387 
3388 	return max;
3389 }
3390 EXPORT_SYMBOL_GPL(pci_rescan_bus);
3391 
3392 /*
3393  * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
3394  * routines should always be executed under this mutex.
3395  */
3396 static DEFINE_MUTEX(pci_rescan_remove_lock);
3397 
pci_lock_rescan_remove(void)3398 void pci_lock_rescan_remove(void)
3399 {
3400 	mutex_lock(&pci_rescan_remove_lock);
3401 }
3402 EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
3403 
pci_unlock_rescan_remove(void)3404 void pci_unlock_rescan_remove(void)
3405 {
3406 	mutex_unlock(&pci_rescan_remove_lock);
3407 }
3408 EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
3409 
pci_sort_bf_cmp(const struct device * d_a,const struct device * d_b)3410 static int __init pci_sort_bf_cmp(const struct device *d_a,
3411 				  const struct device *d_b)
3412 {
3413 	const struct pci_dev *a = to_pci_dev(d_a);
3414 	const struct pci_dev *b = to_pci_dev(d_b);
3415 
3416 	if      (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
3417 	else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return  1;
3418 
3419 	if      (a->bus->number < b->bus->number) return -1;
3420 	else if (a->bus->number > b->bus->number) return  1;
3421 
3422 	if      (a->devfn < b->devfn) return -1;
3423 	else if (a->devfn > b->devfn) return  1;
3424 
3425 	return 0;
3426 }
3427 
pci_sort_breadthfirst(void)3428 void __init pci_sort_breadthfirst(void)
3429 {
3430 	bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
3431 }
3432 
pci_hp_add_bridge(struct pci_dev * dev)3433 int pci_hp_add_bridge(struct pci_dev *dev)
3434 {
3435 	struct pci_bus *parent = dev->bus;
3436 	int busnr, start = parent->busn_res.start;
3437 	unsigned int available_buses = 0;
3438 	int end = parent->busn_res.end;
3439 
3440 	for (busnr = start; busnr <= end; busnr++) {
3441 		if (!pci_find_bus(pci_domain_nr(parent), busnr))
3442 			break;
3443 	}
3444 	if (busnr-- > end) {
3445 		pci_err(dev, "No bus number available for hot-added bridge\n");
3446 		return -1;
3447 	}
3448 
3449 	/* Scan bridges that are already configured */
3450 	busnr = pci_scan_bridge(parent, dev, busnr, 0);
3451 
3452 	/*
3453 	 * Distribute the available bus numbers between hotplug-capable
3454 	 * bridges to make extending the chain later possible.
3455 	 */
3456 	available_buses = end - busnr;
3457 
3458 	/* Scan bridges that need to be reconfigured */
3459 	pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
3460 
3461 	if (!dev->subordinate)
3462 		return -1;
3463 
3464 	return 0;
3465 }
3466 EXPORT_SYMBOL_GPL(pci_hp_add_bridge);
3467