blob: 3c365dc996e71e103bf5891749d52bee1af01f26 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * probe.c - 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>
Suthikulpanit, Suravee50230712015-10-28 15:50:53 -070010#include <linux/of_device.h>
Murali Karicheride335bb42015-03-03 12:52:13 -050011#include <linux/of_pci.h>
Bjorn Helgaas589fcc22014-09-12 20:02:00 -060012#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/cpumask.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080016#include <linux/pci-aspm.h>
Taku Izumib07461a2015-09-17 10:09:37 -050017#include <linux/aer.h>
Suthikulpanit, Suravee29dbe1f2015-10-28 15:50:54 -070018#include <linux/acpi.h>
Jan Kiszka690f4302018-03-07 08:39:13 +010019#include <linux/hypervisor.h>
Jake Oshins788858e2016-02-16 21:56:22 +000020#include <linux/irqdomain.h>
Mika Westerbergd963f652016-06-02 11:17:13 +030021#include <linux/pm_runtime.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090022#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
25#define CARDBUS_RESERVE_BUSNR 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Stephen Hemminger0b950f02014-01-10 17:14:48 -070027static struct resource busn_resource = {
Yinghai Lu67cdc822012-05-17 18:51:12 -070028 .name = "PCI busn",
29 .start = 0,
30 .end = 255,
31 .flags = IORESOURCE_BUS,
32};
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Ugh. Need to stop exporting this to modules. */
35LIST_HEAD(pci_root_buses);
36EXPORT_SYMBOL(pci_root_buses);
37
Yinghai Lu5cc62c22012-05-17 18:51:11 -070038static LIST_HEAD(pci_domain_busn_res_list);
39
40struct pci_domain_busn_res {
41 struct list_head list;
42 struct resource res;
43 int domain_nr;
44};
45
46static struct resource *get_pci_domain_busn_res(int domain_nr)
47{
48 struct pci_domain_busn_res *r;
49
50 list_for_each_entry(r, &pci_domain_busn_res_list, list)
51 if (r->domain_nr == domain_nr)
52 return &r->res;
53
54 r = kzalloc(sizeof(*r), GFP_KERNEL);
55 if (!r)
56 return NULL;
57
58 r->domain_nr = domain_nr;
59 r->res.start = 0;
60 r->res.end = 0xff;
61 r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
62
63 list_add_tail(&r->list, &pci_domain_busn_res_list);
64
65 return &r->res;
66}
67
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080068static int find_anything(struct device *dev, void *data)
69{
70 return 1;
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070073/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -060074 * Some device drivers need know if PCI is initiated.
75 * Basically, we think PCI is not initiated when there
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080076 * is no device to be found on the pci_bus_type.
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070077 */
78int no_pci_devices(void)
79{
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080080 struct device *dev;
81 int no_devices;
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070082
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080083 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
84 no_devices = (dev == NULL);
85 put_device(dev);
86 return no_devices;
87}
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070088EXPORT_SYMBOL(no_pci_devices);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * PCI Bus Class
92 */
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040093static void release_pcibus_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040095 struct pci_bus *pci_bus = to_pci_bus(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Markus Elfringff0387c2014-11-10 21:02:17 -070097 put_device(pci_bus->bridge);
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070098 pci_bus_remove_resources(pci_bus);
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100099 pci_release_bus_of_node(pci_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree(pci_bus);
101}
102
103static struct class pcibus_class = {
104 .name = "pci_bus",
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400105 .dev_release = &release_pcibus_dev,
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700106 .dev_groups = pcibus_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109static int __init pcibus_class_init(void)
110{
111 return class_register(&pcibus_class);
112}
113postcore_initcall(pcibus_class_init);
114
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400115static u64 pci_size(u64 base, u64 maxbase, u64 mask)
Yinghai Lu07eddf32006-11-29 13:53:10 -0800116{
117 u64 size = mask & maxbase; /* Find the significant bits */
118 if (!size)
119 return 0;
120
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600121 /*
122 * Get the lowest of them to find the decode size, and from that
123 * the extent.
124 */
Yinghai Lu07eddf32006-11-29 13:53:10 -0800125 size = (size & ~(size-1)) - 1;
126
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600127 /*
128 * base == maxbase can be valid only if the BAR has already been
129 * programmed with all 1s.
130 */
Yinghai Lu07eddf32006-11-29 13:53:10 -0800131 if (base == maxbase && ((base | size) & mask) != mask)
132 return 0;
133
134 return size;
135}
136
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600137static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
Yinghai Lu07eddf32006-11-29 13:53:10 -0800138{
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600139 u32 mem_type;
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600140 unsigned long flags;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600141
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400142 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600143 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
144 flags |= IORESOURCE_IO;
145 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400146 }
147
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600148 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
149 flags |= IORESOURCE_MEM;
150 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
151 flags |= IORESOURCE_PREFETCH;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400152
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600153 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
154 switch (mem_type) {
155 case PCI_BASE_ADDRESS_MEM_TYPE_32:
156 break;
157 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600158 /* 1M mem BAR treated as 32-bit BAR */
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600159 break;
160 case PCI_BASE_ADDRESS_MEM_TYPE_64:
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600161 flags |= IORESOURCE_MEM_64;
162 break;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600163 default:
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600164 /* mem unknown type treated as 32-bit BAR */
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600165 break;
166 }
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600167 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400168}
169
Zoltan Kiss808e34e2013-08-22 23:19:18 +0100170#define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
171
Yu Zhao0b400c72008-11-22 02:40:40 +0800172/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600173 * pci_read_base - Read a PCI BAR
Yu Zhao0b400c72008-11-22 02:40:40 +0800174 * @dev: the PCI device
175 * @type: type of the BAR
176 * @res: resource buffer to be filled in
177 * @pos: BAR position in the config space
178 *
179 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400180 */
Yu Zhao0b400c72008-11-22 02:40:40 +0800181int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400182 struct resource *res, unsigned int pos)
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400183{
Marc Gonzalezdc5205e2017-04-10 19:46:54 +0200184 u32 l = 0, sz = 0, mask;
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600185 u64 l64, sz64, mask64;
Jacob Pan253d2e52010-07-16 10:19:22 -0700186 u16 orig_cmd;
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800187 struct pci_bus_region region, inverted_region;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400188
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200189 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400190
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600191 /* No printks while decoding is disabled! */
Jacob Pan253d2e52010-07-16 10:19:22 -0700192 if (!dev->mmio_always_on) {
193 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
Zoltan Kiss808e34e2013-08-22 23:19:18 +0100194 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
195 pci_write_config_word(dev, PCI_COMMAND,
196 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
197 }
Jacob Pan253d2e52010-07-16 10:19:22 -0700198 }
199
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400200 res->name = pci_name(dev);
201
202 pci_read_config_dword(dev, pos, &l);
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200203 pci_write_config_dword(dev, pos, l | mask);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400204 pci_read_config_dword(dev, pos, &sz);
205 pci_write_config_dword(dev, pos, l);
206
207 /*
208 * All bits set in sz means the device isn't working properly.
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600209 * If the BAR isn't implemented, all bits must be 0. If it's a
210 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
211 * 1 must be clear.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400212 */
Myron Stowef795d862014-10-30 11:54:43 -0600213 if (sz == 0xffffffff)
214 sz = 0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400215
216 /*
217 * I don't know how l can have all bits set. Copied from old code.
218 * Maybe it fixes a bug on some ancient platform.
219 */
220 if (l == 0xffffffff)
221 l = 0;
222
223 if (type == pci_bar_unknown) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600224 res->flags = decode_bar(dev, l);
225 res->flags |= IORESOURCE_SIZEALIGN;
226 if (res->flags & IORESOURCE_IO) {
Myron Stowef795d862014-10-30 11:54:43 -0600227 l64 = l & PCI_BASE_ADDRESS_IO_MASK;
228 sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
229 mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400230 } else {
Myron Stowef795d862014-10-30 11:54:43 -0600231 l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
232 sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
233 mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400234 }
235 } else {
Bjorn Helgaas7a6d3122016-11-28 17:21:02 -0600236 if (l & PCI_ROM_ADDRESS_ENABLE)
237 res->flags |= IORESOURCE_ROM_ENABLE;
Myron Stowef795d862014-10-30 11:54:43 -0600238 l64 = l & PCI_ROM_ADDRESS_MASK;
239 sz64 = sz & PCI_ROM_ADDRESS_MASK;
Matthias Kaehlcke76dc52682017-04-14 13:38:02 -0700240 mask64 = PCI_ROM_ADDRESS_MASK;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400241 }
242
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600243 if (res->flags & IORESOURCE_MEM_64) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400244 pci_read_config_dword(dev, pos + 4, &l);
245 pci_write_config_dword(dev, pos + 4, ~0);
246 pci_read_config_dword(dev, pos + 4, &sz);
247 pci_write_config_dword(dev, pos + 4, l);
248
249 l64 |= ((u64)l << 32);
250 sz64 |= ((u64)sz << 32);
Myron Stowef795d862014-10-30 11:54:43 -0600251 mask64 |= ((u64)~0 << 32);
252 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400253
Myron Stowef795d862014-10-30 11:54:43 -0600254 if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
255 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400256
Myron Stowef795d862014-10-30 11:54:43 -0600257 if (!sz64)
258 goto fail;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400259
Myron Stowef795d862014-10-30 11:54:43 -0600260 sz64 = pci_size(l64, sz64, mask64);
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600261 if (!sz64) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600262 pci_info(dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600263 pos);
Myron Stowef795d862014-10-30 11:54:43 -0600264 goto fail;
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600265 }
Myron Stowef795d862014-10-30 11:54:43 -0600266
267 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700268 if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
269 && sz64 > 0x100000000ULL) {
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600270 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
271 res->start = 0;
272 res->end = 0;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600273 pci_err(dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
Myron Stowef795d862014-10-30 11:54:43 -0600274 pos, (unsigned long long)sz64);
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600275 goto out;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600276 }
277
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700278 if ((sizeof(pci_bus_addr_t) < 8) && l) {
Bjorn Helgaas31e9dd22014-04-29 18:37:47 -0600279 /* Above 32-bit boundary; try to reallocate */
Bjorn Helgaasc83bd902014-02-26 11:26:00 -0700280 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaas72dc5602014-04-29 18:42:49 -0600281 res->start = 0;
282 res->end = sz64;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600283 pci_info(dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
Myron Stowef795d862014-10-30 11:54:43 -0600284 pos, (unsigned long long)l64);
Bjorn Helgaas72dc5602014-04-29 18:42:49 -0600285 goto out;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400286 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400287 }
288
Myron Stowef795d862014-10-30 11:54:43 -0600289 region.start = l64;
290 region.end = l64 + sz64;
291
Yinghai Lufc279852013-12-09 22:54:40 -0800292 pcibios_bus_to_resource(dev->bus, res, &region);
293 pcibios_resource_to_bus(dev->bus, &inverted_region, res);
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800294
295 /*
296 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
297 * the corresponding resource address (the physical address used by
298 * the CPU. Converting that resource address back to a bus address
299 * should yield the original BAR value:
300 *
301 * resource_to_bus(bus_to_resource(A)) == A
302 *
303 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
304 * be claimed by the device.
305 */
306 if (inverted_region.start != region.start) {
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800307 res->flags |= IORESOURCE_UNSET;
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800308 res->start = 0;
Bjorn Helgaas26370fc2014-04-14 15:26:50 -0600309 res->end = region.end - region.start;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600310 pci_info(dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
Myron Stowef795d862014-10-30 11:54:43 -0600311 pos, (unsigned long long)region.start);
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800312 }
Kevin Hao96ddef22013-05-25 19:36:26 +0800313
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600314 goto out;
315
316
317fail:
318 res->flags = 0;
319out:
Bjorn Helgaas31e9dd22014-04-29 18:37:47 -0600320 if (res->flags)
Frederick Lawler7506dc72018-01-18 12:55:24 -0600321 pci_printk(KERN_DEBUG, dev, "reg 0x%x: %pR\n", pos, res);
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600322
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600323 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
Yinghai Lu07eddf32006-11-29 13:53:10 -0800324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
327{
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400328 unsigned int pos, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Prarit Bhargavaad67b432016-05-11 12:27:16 -0400330 if (dev->non_compliant_bars)
331 return;
332
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400333 for (pos = 0; pos < howmany; pos++) {
334 struct resource *res = &dev->resource[pos];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400336 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (rom) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400340 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dev->rom_base_reg = rom;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400342 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
Dan Williams92b19ff2015-08-10 23:07:06 -0400343 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400344 __pci_read_base(dev, pci_bar_mem32, res, rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346}
347
Bill Pemberton15856ad2012-11-21 15:35:00 -0500348static void pci_read_bridge_io(struct pci_bus *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct pci_dev *dev = child->self;
351 u8 io_base_lo, io_limit_lo;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600352 unsigned long io_mask, io_granularity, base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700353 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600354 struct resource *res;
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 res = child->resource[0];
365 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
366 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600367 base = (io_base_lo & io_mask) << 8;
368 limit = (io_limit_lo & io_mask) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
371 u16 io_base_hi, io_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
374 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600375 base |= ((unsigned long) io_base_hi << 16);
376 limit |= ((unsigned long) io_limit_hi << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600379 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700381 region.start = base;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600382 region.end = limit + io_granularity - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800383 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600384 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700386}
387
Bill Pemberton15856ad2012-11-21 15:35:00 -0500388static void pci_read_bridge_mmio(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700389{
390 struct pci_dev *dev = child->self;
391 u16 mem_base_lo, mem_limit_lo;
392 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700393 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700394 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 res = child->resource[1];
397 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
398 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600399 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
400 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600401 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700403 region.start = base;
404 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800405 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600406 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700408}
409
Bill Pemberton15856ad2012-11-21 15:35:00 -0500410static void pci_read_bridge_mmio_pref(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700411{
412 struct pci_dev *dev = child->self;
413 u16 mem_base_lo, mem_limit_lo;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700414 u64 base64, limit64;
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700415 pci_bus_addr_t base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700416 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700417 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 res = child->resource[2];
420 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
421 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700422 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
423 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
426 u32 mem_base_hi, mem_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
429 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
430
431 /*
432 * Some bridges set the base > limit by default, and some
433 * (broken) BIOSes do not initialize them. If we find
434 * this, just assume they are not being used.
435 */
436 if (mem_base_hi <= mem_limit_hi) {
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700437 base64 |= (u64) mem_base_hi << 32;
438 limit64 |= (u64) mem_limit_hi << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 }
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700441
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700442 base = (pci_bus_addr_t) base64;
443 limit = (pci_bus_addr_t) limit64;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700444
445 if (base != base64) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600446 pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700447 (unsigned long long) base64);
448 return;
449 }
450
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600451 if (base <= limit) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700452 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
453 IORESOURCE_MEM | IORESOURCE_PREFETCH;
454 if (res->flags & PCI_PREF_RANGE_TYPE_64)
455 res->flags |= IORESOURCE_MEM_64;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700456 region.start = base;
457 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800458 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600459 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461}
462
Bill Pemberton15856ad2012-11-21 15:35:00 -0500463void pci_read_bridge_bases(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700464{
465 struct pci_dev *dev = child->self;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700466 struct resource *res;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700467 int i;
468
469 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
470 return;
471
Frederick Lawler7506dc72018-01-18 12:55:24 -0600472 pci_info(dev, "PCI bridge to %pR%s\n",
Yinghai Lub918c622012-05-17 18:51:11 -0700473 &child->busn_res,
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700474 dev->transparent ? " (subtractive decode)" : "");
475
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700476 pci_bus_remove_resources(child);
477 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
478 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
479
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700480 pci_read_bridge_io(child);
481 pci_read_bridge_mmio(child);
482 pci_read_bridge_mmio_pref(child);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700483
484 if (dev->transparent) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700485 pci_bus_for_each_resource(child->parent, res, i) {
Bjorn Helgaasd739a092014-04-14 16:10:54 -0600486 if (res && res->flags) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700487 pci_bus_add_resource(child, res,
488 PCI_SUBTRACTIVE_DECODE);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600489 pci_printk(KERN_DEBUG, dev,
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700490 " bridge window %pR (subtractive decode)\n",
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700491 res);
492 }
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700493 }
494 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700495}
496
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100497static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct pci_bus *b;
500
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100501 b = kzalloc(sizeof(*b), GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600502 if (!b)
503 return NULL;
504
505 INIT_LIST_HEAD(&b->node);
506 INIT_LIST_HEAD(&b->children);
507 INIT_LIST_HEAD(&b->devices);
508 INIT_LIST_HEAD(&b->slots);
509 INIT_LIST_HEAD(&b->resources);
510 b->max_bus_speed = PCI_SPEED_UNKNOWN;
511 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100512#ifdef CONFIG_PCI_DOMAINS_GENERIC
513 if (parent)
514 b->domain_nr = parent->domain_nr;
515#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return b;
517}
518
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500519static void devm_pci_release_host_bridge_dev(struct device *dev)
Jiang Liu70efde22013-06-07 16:16:51 -0600520{
521 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
522
523 if (bridge->release_fn)
524 bridge->release_fn(bridge);
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500525}
Jiang Liu70efde22013-06-07 16:16:51 -0600526
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500527static void pci_release_host_bridge_dev(struct device *dev)
528{
529 devm_pci_release_host_bridge_dev(dev);
530 pci_free_host_bridge(to_pci_host_bridge(dev));
Jiang Liu70efde22013-06-07 16:16:51 -0600531}
532
Thierry Redinga52d1442016-11-25 11:57:11 +0100533struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
Yinghai Lu7b543662012-04-02 18:31:53 -0700534{
535 struct pci_host_bridge *bridge;
536
Thierry Reding59094062016-11-25 11:57:10 +0100537 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600538 if (!bridge)
539 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -0700540
Bjorn Helgaas05013482013-06-05 14:22:11 -0600541 INIT_LIST_HEAD(&bridge->windows);
Lorenzo Pieralisia1c00502017-06-28 15:13:52 -0500542 bridge->dev.release = pci_release_host_bridge_dev;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100543
Yinghai Lu7b543662012-04-02 18:31:53 -0700544 return bridge;
545}
Thierry Redinga52d1442016-11-25 11:57:11 +0100546EXPORT_SYMBOL(pci_alloc_host_bridge);
Yinghai Lu7b543662012-04-02 18:31:53 -0700547
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500548struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
549 size_t priv)
550{
551 struct pci_host_bridge *bridge;
552
553 bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
554 if (!bridge)
555 return NULL;
556
557 INIT_LIST_HEAD(&bridge->windows);
558 bridge->dev.release = devm_pci_release_host_bridge_dev;
559
560 return bridge;
561}
562EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
563
Lorenzo Pieralisidff79b92017-06-28 15:13:52 -0500564void pci_free_host_bridge(struct pci_host_bridge *bridge)
565{
566 pci_free_resource_list(&bridge->windows);
567
568 kfree(bridge);
569}
570EXPORT_SYMBOL(pci_free_host_bridge);
571
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700572static const unsigned char pcix_bus_speed[] = {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500573 PCI_SPEED_UNKNOWN, /* 0 */
574 PCI_SPEED_66MHz_PCIX, /* 1 */
575 PCI_SPEED_100MHz_PCIX, /* 2 */
576 PCI_SPEED_133MHz_PCIX, /* 3 */
577 PCI_SPEED_UNKNOWN, /* 4 */
578 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
579 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
580 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
581 PCI_SPEED_UNKNOWN, /* 8 */
582 PCI_SPEED_66MHz_PCIX_266, /* 9 */
583 PCI_SPEED_100MHz_PCIX_266, /* A */
584 PCI_SPEED_133MHz_PCIX_266, /* B */
585 PCI_SPEED_UNKNOWN, /* C */
586 PCI_SPEED_66MHz_PCIX_533, /* D */
587 PCI_SPEED_100MHz_PCIX_533, /* E */
588 PCI_SPEED_133MHz_PCIX_533 /* F */
589};
590
Jacob Keller343e51a2013-07-31 06:53:16 +0000591const unsigned char pcie_link_speed[] = {
Matthew Wilcox3749c512009-12-13 08:11:32 -0500592 PCI_SPEED_UNKNOWN, /* 0 */
593 PCIE_SPEED_2_5GT, /* 1 */
594 PCIE_SPEED_5_0GT, /* 2 */
Matthew Wilcox9dfd97f2009-12-13 08:11:35 -0500595 PCIE_SPEED_8_0GT, /* 3 */
Matthew Wilcox3749c512009-12-13 08:11:32 -0500596 PCI_SPEED_UNKNOWN, /* 4 */
597 PCI_SPEED_UNKNOWN, /* 5 */
598 PCI_SPEED_UNKNOWN, /* 6 */
599 PCI_SPEED_UNKNOWN, /* 7 */
600 PCI_SPEED_UNKNOWN, /* 8 */
601 PCI_SPEED_UNKNOWN, /* 9 */
602 PCI_SPEED_UNKNOWN, /* A */
603 PCI_SPEED_UNKNOWN, /* B */
604 PCI_SPEED_UNKNOWN, /* C */
605 PCI_SPEED_UNKNOWN, /* D */
606 PCI_SPEED_UNKNOWN, /* E */
607 PCI_SPEED_UNKNOWN /* F */
608};
609
610void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
611{
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700612 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
Matthew Wilcox3749c512009-12-13 08:11:32 -0500613}
614EXPORT_SYMBOL_GPL(pcie_update_link_speed);
615
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500616static unsigned char agp_speeds[] = {
617 AGP_UNKNOWN,
618 AGP_1X,
619 AGP_2X,
620 AGP_4X,
621 AGP_8X
622};
623
624static enum pci_bus_speed agp_speed(int agp3, int agpstat)
625{
626 int index = 0;
627
628 if (agpstat & 4)
629 index = 3;
630 else if (agpstat & 2)
631 index = 2;
632 else if (agpstat & 1)
633 index = 1;
634 else
635 goto out;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700636
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500637 if (agp3) {
638 index += 2;
639 if (index == 5)
640 index = 0;
641 }
642
643 out:
644 return agp_speeds[index];
645}
646
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500647static void pci_set_bus_speed(struct pci_bus *bus)
648{
649 struct pci_dev *bridge = bus->self;
650 int pos;
651
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500652 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
653 if (!pos)
654 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
655 if (pos) {
656 u32 agpstat, agpcmd;
657
658 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
659 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
660
661 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
662 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
663 }
664
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500665 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
666 if (pos) {
667 u16 status;
668 enum pci_bus_speed max;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500669
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700670 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
671 &status);
672
673 if (status & PCI_X_SSTATUS_533MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500674 max = PCI_SPEED_133MHz_PCIX_533;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700675 } else if (status & PCI_X_SSTATUS_266MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500676 max = PCI_SPEED_133MHz_PCIX_266;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700677 } else if (status & PCI_X_SSTATUS_133MHZ) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400678 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500679 max = PCI_SPEED_133MHz_PCIX_ECC;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400680 else
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500681 max = PCI_SPEED_133MHz_PCIX;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500682 } else {
683 max = PCI_SPEED_66MHz_PCIX;
684 }
685
686 bus->max_bus_speed = max;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700687 bus->cur_bus_speed = pcix_bus_speed[
688 (status & PCI_X_SSTATUS_FREQ) >> 6];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500689
690 return;
691 }
692
Yijing Wangfdfe1512013-09-05 15:55:29 +0800693 if (pci_is_pcie(bridge)) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500694 u32 linkcap;
695 u16 linksta;
696
Jiang Liu59875ae2012-07-24 17:20:06 +0800697 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700698 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500699
Jiang Liu59875ae2012-07-24 17:20:06 +0800700 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500701 pcie_update_link_speed(bus, linksta);
702 }
703}
704
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100705static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
706{
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100707 struct irq_domain *d;
708
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100709 /*
710 * Any firmware interface that can resolve the msi_domain
711 * should be called from here.
712 */
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100713 d = pci_host_bridge_of_msi_domain(bus);
Suravee Suthikulpanit471036b2015-12-10 08:55:27 -0800714 if (!d)
715 d = pci_host_bridge_acpi_msi_domain(bus);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100716
Jake Oshins788858e2016-02-16 21:56:22 +0000717#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
718 /*
719 * If no IRQ domain was found via the OF tree, try looking it up
720 * directly through the fwnode_handle.
721 */
722 if (!d) {
723 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
724
725 if (fwnode)
726 d = irq_find_matching_fwnode(fwnode,
727 DOMAIN_BUS_PCI_MSI);
728 }
729#endif
730
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100731 return d;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100732}
733
734static void pci_set_bus_msi_domain(struct pci_bus *bus)
735{
736 struct irq_domain *d;
Alex Williamson38ea72b2015-09-18 15:08:54 -0600737 struct pci_bus *b;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100738
739 /*
Alex Williamson38ea72b2015-09-18 15:08:54 -0600740 * The bus can be a root bus, a subordinate bus, or a virtual bus
741 * created by an SR-IOV device. Walk up to the first bridge device
742 * found or derive the domain from the host bridge.
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100743 */
Alex Williamson38ea72b2015-09-18 15:08:54 -0600744 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
745 if (b->self)
746 d = dev_get_msi_domain(&b->self->dev);
747 }
748
749 if (!d)
750 d = pci_host_bridge_msi_domain(b);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100751
752 dev_set_msi_domain(&bus->dev, d);
753}
754
Lorenzo Pieralisicea9bc02017-06-28 15:13:55 -0500755static int pci_register_host_bridge(struct pci_host_bridge *bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100756{
757 struct device *parent = bridge->dev.parent;
758 struct resource_entry *window, *n;
759 struct pci_bus *bus, *b;
760 resource_size_t offset;
761 LIST_HEAD(resources);
762 struct resource *res;
763 char addr[64], *fmt;
764 const char *name;
765 int err;
766
767 bus = pci_alloc_bus(NULL);
768 if (!bus)
769 return -ENOMEM;
770
771 bridge->bus = bus;
772
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600773 /* Temporarily move resources off the list */
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100774 list_splice_init(&bridge->windows, &resources);
775 bus->sysdata = bridge->sysdata;
776 bus->msi = bridge->msi;
777 bus->ops = bridge->ops;
778 bus->number = bus->busn_res.start = bridge->busnr;
779#ifdef CONFIG_PCI_DOMAINS_GENERIC
780 bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
781#endif
782
783 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
784 if (b) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600785 /* Ignore it if we already got here via a different bridge */
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100786 dev_dbg(&b->dev, "bus already known\n");
787 err = -EEXIST;
788 goto free;
789 }
790
791 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
792 bridge->busnr);
793
794 err = pcibios_root_bridge_prepare(bridge);
795 if (err)
796 goto free;
797
798 err = device_register(&bridge->dev);
799 if (err)
800 put_device(&bridge->dev);
801
802 bus->bridge = get_device(&bridge->dev);
803 device_enable_async_suspend(bus->bridge);
804 pci_set_bus_of_node(bus);
805 pci_set_bus_msi_domain(bus);
806
807 if (!parent)
808 set_dev_node(bus->bridge, pcibus_to_node(bus));
809
810 bus->dev.class = &pcibus_class;
811 bus->dev.parent = bus->bridge;
812
813 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
814 name = dev_name(&bus->dev);
815
816 err = device_register(&bus->dev);
817 if (err)
818 goto unregister;
819
820 pcibios_add_bus(bus);
821
822 /* Create legacy_io and legacy_mem files for this bus */
823 pci_create_legacy_files(bus);
824
825 if (parent)
826 dev_info(parent, "PCI host bridge to bus %s\n", name);
827 else
828 pr_info("PCI host bridge to bus %s\n", name);
829
830 /* Add initial resources to the bus */
831 resource_list_for_each_entry_safe(window, n, &resources) {
832 list_move_tail(&window->node, &bridge->windows);
833 offset = window->offset;
834 res = window->res;
835
836 if (res->flags & IORESOURCE_BUS)
837 pci_bus_insert_busn_res(bus, bus->number, res->end);
838 else
839 pci_bus_add_resource(bus, res, 0);
840
841 if (offset) {
842 if (resource_type(res) == IORESOURCE_IO)
843 fmt = " (bus address [%#06llx-%#06llx])";
844 else
845 fmt = " (bus address [%#010llx-%#010llx])";
846
847 snprintf(addr, sizeof(addr), fmt,
848 (unsigned long long)(res->start - offset),
849 (unsigned long long)(res->end - offset));
850 } else
851 addr[0] = '\0';
852
853 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
854 }
855
856 down_write(&pci_bus_sem);
857 list_add_tail(&bus->node, &pci_root_buses);
858 up_write(&pci_bus_sem);
859
860 return 0;
861
862unregister:
863 put_device(&bridge->dev);
864 device_unregister(&bridge->dev);
865
866free:
867 kfree(bus);
868 return err;
869}
870
Adrian Bunkcbd4e052008-04-18 13:53:55 -0700871static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
872 struct pci_dev *bridge, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct pci_bus *child;
875 int i;
Yinghai Lu4f535092013-01-21 13:20:52 -0800876 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600878 /* Allocate a new bus and inherit stuff from the parent */
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100879 child = pci_alloc_bus(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!child)
881 return NULL;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 child->parent = parent;
884 child->ops = parent->ops;
Thierry Reding0cbdcfc2013-08-09 22:27:08 +0200885 child->msi = parent->msi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 child->sysdata = parent->sysdata;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200887 child->bus_flags = parent->bus_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600889 /*
890 * Initialize some portions of the bus device, but don't register
891 * it now as the parent is not properly set up yet.
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400892 */
893 child->dev.class = &pcibus_class;
Kay Sievers1a927132008-10-30 02:17:49 +0100894 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600896 /* Set up the primary, secondary and subordinate bus numbers */
Yinghai Lub918c622012-05-17 18:51:11 -0700897 child->number = child->busn_res.start = busnr;
898 child->primary = parent->busn_res.start;
899 child->busn_res.end = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Yinghai Lu4f535092013-01-21 13:20:52 -0800901 if (!bridge) {
902 child->dev.parent = parent->bridge;
903 goto add_dev;
904 }
Yu Zhao3789fa82008-11-22 02:41:07 +0800905
906 child->self = bridge;
907 child->bridge = get_device(&bridge->dev);
Yinghai Lu4f535092013-01-21 13:20:52 -0800908 child->dev.parent = child->bridge;
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000909 pci_set_bus_of_node(child);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500910 pci_set_bus_speed(child);
911
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600912 /* Set up default resource pointers and names */
Yu Zhaofde09c62008-11-22 02:39:32 +0800913 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
915 child->resource[i]->name = child->name;
916 }
917 bridge->subordinate = child;
918
Yinghai Lu4f535092013-01-21 13:20:52 -0800919add_dev:
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100920 pci_set_bus_msi_domain(child);
Yinghai Lu4f535092013-01-21 13:20:52 -0800921 ret = device_register(&child->dev);
922 WARN_ON(ret < 0);
923
Jiang Liu10a95742013-04-12 05:44:20 +0000924 pcibios_add_bus(child);
925
Thierry Reding057bd2e2016-02-09 15:30:47 +0100926 if (child->ops->add_bus) {
927 ret = child->ops->add_bus(child);
928 if (WARN_ON(ret < 0))
929 dev_err(&child->dev, "failed to add bus: %d\n", ret);
930 }
931
Yinghai Lu4f535092013-01-21 13:20:52 -0800932 /* Create legacy_io and legacy_mem files for this bus */
933 pci_create_legacy_files(child);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return child;
936}
937
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400938struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
939 int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct pci_bus *child;
942
943 child = pci_alloc_child_bus(parent, dev, busnr);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700944 if (child) {
Zhang Yanmind71374d2006-06-02 12:35:43 +0800945 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 list_add_tail(&child->node, &parent->children);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800947 up_write(&pci_bus_sem);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return child;
950}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600951EXPORT_SYMBOL(pci_add_new_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Rajat Jainf3dbd802014-09-02 16:26:00 -0700953static void pci_enable_crs(struct pci_dev *pdev)
954{
955 u16 root_cap = 0;
956
957 /* Enable CRS Software Visibility if supported */
958 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
959 if (root_cap & PCI_EXP_RTCAP_CRSVIS)
960 pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
961 PCI_EXP_RTCTL_CRSSVE);
962}
963
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300964static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
965 unsigned int available_buses);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300968 * pci_scan_bridge_extend() - Scan buses behind a bridge
969 * @bus: Parent bus the bridge is on
970 * @dev: Bridge itself
971 * @max: Starting subordinate number of buses behind this bridge
972 * @available_buses: Total number of buses available for this bridge and
973 * the devices below. After the minimal bus space has
974 * been allocated the remaining buses will be
975 * distributed equally between hotplug-capable bridges.
976 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
977 * that need to be reconfigured.
978 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * If it's a bridge, configure it and scan the bus behind it.
980 * For CardBus bridges, we don't scan behind as the devices will
981 * be handled by the bridge driver itself.
982 *
983 * We need to process bridges in two passes -- first we scan those
984 * already configured by the BIOS and after we are done with all of
985 * them, we proceed to assigning numbers to the remaining buses in
986 * order to avoid overlaps between old and new bus numbers.
987 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300988static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
989 int max, unsigned int available_buses,
990 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct pci_bus *child;
993 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
Dominik Brodowski49887942005-12-08 16:53:12 +0100994 u32 buses, i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 u16 bctl;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600996 u8 primary, secondary, subordinate;
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100997 int broken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Mika Westerbergd963f652016-06-02 11:17:13 +0300999 /*
1000 * Make sure the bridge is powered on to be able to access config
1001 * space of devices below it.
1002 */
1003 pm_runtime_get_sync(&dev->dev);
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001006 primary = buses & 0xFF;
1007 secondary = (buses >> 8) & 0xFF;
1008 subordinate = (buses >> 16) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Frederick Lawler7506dc72018-01-18 12:55:24 -06001010 pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001011 secondary, subordinate, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001013 if (!primary && (primary != bus->number) && secondary && subordinate) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001014 pci_warn(dev, "Primary bus is hard wired to 0\n");
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001015 primary = bus->number;
1016 }
1017
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001018 /* Check if setup is sensible at all */
1019 if (!pass &&
Yinghai Lu1965f662012-09-10 17:19:33 -07001020 (primary != bus->number || secondary <= bus->number ||
Bjorn Helgaas12d87062014-09-19 11:08:40 -06001021 secondary > subordinate)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001022 pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
Yinghai Lu1965f662012-09-10 17:19:33 -07001023 secondary, subordinate);
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001024 broken = 1;
1025 }
1026
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001027 /*
1028 * Disable Master-Abort Mode during probing to avoid reporting of
1029 * bus errors in some architectures.
1030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1032 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1033 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1034
Rajat Jainf3dbd802014-09-02 16:26:00 -07001035 pci_enable_crs(dev);
1036
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001037 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1038 !is_cardbus && !broken) {
1039 unsigned int cmax;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001042 * Bus already configured by firmware, process it in the
1043 * first pass and just note the configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
1045 if (pass)
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001049 * The bus might already exist for two reasons: Either we
1050 * are rescanning the bus or the bus is reachable through
1051 * more than one bridge. The second case can happen with
1052 * the i450NX chipset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001054 child = pci_find_bus(pci_domain_nr(bus), secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001055 if (!child) {
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001056 child = pci_add_new_bus(bus, dev, secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001057 if (!child)
1058 goto out;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001059 child->primary = primary;
Yinghai Lubc76b732012-05-17 18:51:13 -07001060 pci_bus_insert_busn_res(child, secondary, subordinate);
Alex Chiang74710de2009-03-20 14:56:10 -06001061 child->bridge_ctl = bctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 cmax = pci_scan_child_bus(child);
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001065 if (cmax > subordinate)
Frederick Lawler7506dc72018-01-18 12:55:24 -06001066 pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001067 subordinate, cmax);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001068
1069 /* Subordinate should equal child->busn_res.end */
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001070 if (subordinate > max)
1071 max = subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 } else {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * We need to assign a number to this bus which we always
1076 * do in the second pass.
1077 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001078 if (!pass) {
Andreas Noever619c8c32014-01-23 21:59:23 +01001079 if (pcibios_assign_all_busses() || broken || is_cardbus)
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001080
1081 /*
1082 * Temporarily disable forwarding of the
1083 * configuration cycles on all bridges in
1084 * this bus segment to avoid possible
1085 * conflicts in the second pass between two
1086 * bridges programmed with overlapping bus
1087 * ranges.
1088 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001089 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1090 buses & ~0xffffff);
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001091 goto out;
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /* Clear errors */
1095 pci_write_config_word(dev, PCI_STATUS, 0xffff);
1096
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001097 /*
1098 * Prevent assigning a bus number that already exists.
1099 * This can happen when a bridge is hot-plugged, so in this
1100 * case we only re-scan this bus.
1101 */
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001102 child = pci_find_bus(pci_domain_nr(bus), max+1);
1103 if (!child) {
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001104 child = pci_add_new_bus(bus, dev, max+1);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001105 if (!child)
1106 goto out;
Mika Westerberga20c7f32017-10-13 21:35:43 +03001107 pci_bus_insert_busn_res(child, max+1,
1108 bus->busn_res.end);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001109 }
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001110 max++;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001111 if (available_buses)
1112 available_buses--;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 buses = (buses & 0xff000000)
1115 | ((unsigned int)(child->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -07001116 | ((unsigned int)(child->busn_res.start) << 8)
1117 | ((unsigned int)(child->busn_res.end) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /*
1120 * yenta.c forces a secondary latency timer of 176.
1121 * Copy that behaviour here.
1122 */
1123 if (is_cardbus) {
1124 buses &= ~0xff000000;
1125 buses |= CARDBUS_LATENCY_TIMER << 24;
1126 }
Jesper Juhl7c867c82011-01-24 21:14:33 +01001127
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001128 /* We need to blast all three values with a single write */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1130
1131 if (!is_cardbus) {
Gary Hade11949252007-10-08 16:24:16 -07001132 child->bridge_ctl = bctl;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001133 max = pci_scan_child_bus_extend(child, available_buses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 } else {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001137 * For CardBus bridges, we leave 4 bus numbers as
1138 * cards with a PCI-to-PCI bridge can be inserted
1139 * later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001141 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001142 struct pci_bus *parent = bus;
Rajesh Shahcc574502005-04-28 00:25:47 -07001143 if (pci_find_bus(pci_domain_nr(bus),
1144 max+i+1))
1145 break;
Dominik Brodowski49887942005-12-08 16:53:12 +01001146 while (parent->parent) {
1147 if ((!pcibios_assign_all_busses()) &&
Yinghai Lub918c622012-05-17 18:51:11 -07001148 (parent->busn_res.end > max) &&
1149 (parent->busn_res.end <= max+i)) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001150 j = 1;
1151 }
1152 parent = parent->parent;
1153 }
1154 if (j) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001155
Dominik Brodowski49887942005-12-08 16:53:12 +01001156 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001157 * Often, there are two CardBus
1158 * bridges -- try to leave one
1159 * valid bus number for each one.
Dominik Brodowski49887942005-12-08 16:53:12 +01001160 */
1161 i /= 2;
1162 break;
1163 }
1164 }
Rajesh Shahcc574502005-04-28 00:25:47 -07001165 max += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001167
1168 /* Set subordinate bus number to its real value */
Yinghai Lubc76b732012-05-17 18:51:13 -07001169 pci_bus_update_busn_res_end(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1171 }
1172
Gary Hadecb3576f2008-02-08 14:00:52 -08001173 sprintf(child->name,
1174 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1175 pci_domain_nr(bus), child->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Bernhard Kaindld55bef512007-07-30 20:35:13 +02001177 /* Has only triggered on CardBus, fixup is in yenta_socket */
Dominik Brodowski49887942005-12-08 16:53:12 +01001178 while (bus->parent) {
Yinghai Lub918c622012-05-17 18:51:11 -07001179 if ((child->busn_res.end > bus->busn_res.end) ||
1180 (child->number > bus->busn_res.end) ||
Dominik Brodowski49887942005-12-08 16:53:12 +01001181 (child->number < bus->number) ||
Yinghai Lub918c622012-05-17 18:51:11 -07001182 (child->busn_res.end < bus->number)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001183 dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
Yinghai Lub918c622012-05-17 18:51:11 -07001184 &child->busn_res,
1185 (bus->number > child->busn_res.end &&
1186 bus->busn_res.end < child->number) ?
Joe Perchesa6f29a92007-11-19 17:48:29 -08001187 "wholly" : "partially",
1188 bus->self->transparent ? " transparent" : "",
Bjorn Helgaas865df572009-11-04 10:32:57 -07001189 dev_name(&bus->dev),
Yinghai Lub918c622012-05-17 18:51:11 -07001190 &bus->busn_res);
Dominik Brodowski49887942005-12-08 16:53:12 +01001191 }
1192 bus = bus->parent;
1193 }
1194
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001195out:
1196 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1197
Mika Westerbergd963f652016-06-02 11:17:13 +03001198 pm_runtime_put(&dev->dev);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return max;
1201}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001202
1203/*
1204 * pci_scan_bridge() - Scan buses behind a bridge
1205 * @bus: Parent bus the bridge is on
1206 * @dev: Bridge itself
1207 * @max: Starting subordinate number of buses behind this bridge
1208 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1209 * that need to be reconfigured.
1210 *
1211 * If it's a bridge, configure it and scan the bus behind it.
1212 * For CardBus bridges, we don't scan behind as the devices will
1213 * be handled by the bridge driver itself.
1214 *
1215 * We need to process bridges in two passes -- first we scan those
1216 * already configured by the BIOS and after we are done with all of
1217 * them, we proceed to assigning numbers to the remaining buses in
1218 * order to avoid overlaps between old and new bus numbers.
1219 */
1220int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1221{
1222 return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1223}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001224EXPORT_SYMBOL(pci_scan_bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226/*
1227 * Read interrupt line and base address registers.
1228 * The architecture-dependent code can tweak these, of course.
1229 */
1230static void pci_read_irq(struct pci_dev *dev)
1231{
1232 unsigned char irq;
1233
1234 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
Kristen Accardiffeff782005-11-02 16:24:32 -08001235 dev->pin = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (irq)
1237 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1238 dev->irq = irq;
1239}
1240
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001241void set_pcie_port_type(struct pci_dev *pdev)
Yu Zhao480b93b2009-03-20 11:25:14 +08001242{
1243 int pos;
1244 u16 reg16;
Yijing Wangd0751b92015-05-21 15:05:02 +08001245 int type;
1246 struct pci_dev *parent;
Yu Zhao480b93b2009-03-20 11:25:14 +08001247
1248 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1249 if (!pos)
1250 return;
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001251
Kenji Kaneshige0efea002009-11-05 12:05:11 +09001252 pdev->pcie_cap = pos;
Yu Zhao480b93b2009-03-20 11:25:14 +08001253 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
Yijing Wang786e2282012-07-24 17:20:02 +08001254 pdev->pcie_flags_reg = reg16;
Jon Masonb03e7492011-07-20 15:20:54 -05001255 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
1256 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
Yijing Wangd0751b92015-05-21 15:05:02 +08001257
1258 /*
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001259 * A Root Port or a PCI-to-PCIe bridge is always the upstream end
1260 * of a Link. No PCIe component has two Links. Two Links are
1261 * connected by a Switch that has a Port on each Link and internal
1262 * logic to connect the two Ports.
Yijing Wangd0751b92015-05-21 15:05:02 +08001263 */
1264 type = pci_pcie_type(pdev);
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001265 if (type == PCI_EXP_TYPE_ROOT_PORT ||
1266 type == PCI_EXP_TYPE_PCIE_BRIDGE)
Yijing Wangd0751b92015-05-21 15:05:02 +08001267 pdev->has_secondary_link = 1;
1268 else if (type == PCI_EXP_TYPE_UPSTREAM ||
1269 type == PCI_EXP_TYPE_DOWNSTREAM) {
1270 parent = pci_upstream_bridge(pdev);
Yijing Wangb35b1df2015-08-17 18:47:58 +08001271
1272 /*
1273 * Usually there's an upstream device (Root Port or Switch
1274 * Downstream Port), but we can't assume one exists.
1275 */
1276 if (parent && !parent->has_secondary_link)
Yijing Wangd0751b92015-05-21 15:05:02 +08001277 pdev->has_secondary_link = 1;
1278 }
Yu Zhao480b93b2009-03-20 11:25:14 +08001279}
1280
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001281void set_pcie_hotplug_bridge(struct pci_dev *pdev)
Eric W. Biederman28760482009-09-09 14:09:24 -07001282{
Eric W. Biederman28760482009-09-09 14:09:24 -07001283 u32 reg32;
1284
Jiang Liu59875ae2012-07-24 17:20:06 +08001285 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
Eric W. Biederman28760482009-09-09 14:09:24 -07001286 if (reg32 & PCI_EXP_SLTCAP_HPC)
1287 pdev->is_hotplug_bridge = 1;
1288}
1289
Lukas Wunner8531e282017-03-10 21:23:45 +01001290static void set_pcie_thunderbolt(struct pci_dev *dev)
1291{
1292 int vsec = 0;
1293 u32 header;
1294
1295 while ((vsec = pci_find_next_ext_capability(dev, vsec,
1296 PCI_EXT_CAP_ID_VNDR))) {
1297 pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
1298
1299 /* Is the device part of a Thunderbolt controller? */
1300 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
1301 PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
1302 dev->is_thunderbolt = 1;
1303 return;
1304 }
1305 }
1306}
1307
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001308/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001309 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
Alex Williamson78916b02014-05-05 14:20:51 -06001310 * @dev: PCI device
1311 *
1312 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1313 * when forwarding a type1 configuration request the bridge must check that
1314 * the extended register address field is zero. The bridge is not permitted
1315 * to forward the transactions and must handle it as an Unsupported Request.
1316 * Some bridges do not follow this rule and simply drop the extended register
1317 * bits, resulting in the standard config space being aliased, every 256
1318 * bytes across the entire configuration space. Test for this condition by
1319 * comparing the first dword of each potential alias to the vendor/device ID.
1320 * Known offenders:
1321 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1322 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1323 */
1324static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1325{
1326#ifdef CONFIG_PCI_QUIRKS
1327 int pos;
1328 u32 header, tmp;
1329
1330 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1331
1332 for (pos = PCI_CFG_SPACE_SIZE;
1333 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1334 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1335 || header != tmp)
1336 return false;
1337 }
1338
1339 return true;
1340#else
1341 return false;
1342#endif
1343}
1344
1345/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001346 * pci_cfg_space_size - Get the configuration space size of the PCI device
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001347 * @dev: PCI device
1348 *
1349 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1350 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1351 * access it. Maybe we don't have a way to generate extended config space
1352 * accesses, or the device is behind a reverse Express bridge. So we try
1353 * reading the dword at 0x100 which must either be 0 or a valid extended
1354 * capability header.
1355 */
1356static int pci_cfg_space_size_ext(struct pci_dev *dev)
1357{
1358 u32 status;
1359 int pos = PCI_CFG_SPACE_SIZE;
1360
1361 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001362 return PCI_CFG_SPACE_SIZE;
Alex Williamson78916b02014-05-05 14:20:51 -06001363 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001364 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001365
1366 return PCI_CFG_SPACE_EXP_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001367}
1368
1369int pci_cfg_space_size(struct pci_dev *dev)
1370{
1371 int pos;
1372 u32 status;
1373 u16 class;
1374
1375 class = dev->class >> 8;
1376 if (class == PCI_CLASS_BRIDGE_HOST)
1377 return pci_cfg_space_size_ext(dev);
1378
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001379 if (pci_is_pcie(dev))
1380 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001381
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001382 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1383 if (!pos)
1384 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001385
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001386 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1387 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1388 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001389
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001390 return PCI_CFG_SPACE_SIZE;
1391}
1392
Bartlomiej Zolnierkiewicz01abc2a2007-04-23 23:19:36 +02001393#define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
Randy Dunlap76e6a1d2006-12-29 16:47:29 -08001394
Guilherme G. Piccolie80e7edc2015-10-21 12:17:35 -02001395static void pci_msi_setup_pci_dev(struct pci_dev *dev)
Michael S. Tsirkin18516172015-05-07 09:52:21 -05001396{
1397 /*
1398 * Disable the MSI hardware to avoid screaming interrupts
1399 * during boot. This is the power on reset default so
1400 * usually this should be a noop.
1401 */
1402 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1403 if (dev->msi_cap)
1404 pci_msi_set_enable(dev, 0);
1405
1406 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1407 if (dev->msix_cap)
1408 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001412 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
Piotr Gregor99b3c582017-05-26 22:02:25 +01001413 * @dev: PCI device
1414 *
1415 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
1416 * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1417 */
1418static int pci_intx_mask_broken(struct pci_dev *dev)
1419{
1420 u16 orig, toggle, new;
1421
1422 pci_read_config_word(dev, PCI_COMMAND, &orig);
1423 toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1424 pci_write_config_word(dev, PCI_COMMAND, toggle);
1425 pci_read_config_word(dev, PCI_COMMAND, &new);
1426
1427 pci_write_config_word(dev, PCI_COMMAND, orig);
1428
1429 /*
1430 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1431 * r2.3, so strictly speaking, a device is not *broken* if it's not
1432 * writable. But we'll live with the misnomer for now.
1433 */
1434 if (new != toggle)
1435 return 1;
1436 return 0;
1437}
1438
1439/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001440 * pci_setup_device - Fill in class and map information of a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * @dev: the device structure to fill
1442 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001443 * Initialize the device structure with information about the device's
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001444 * vendor,class,memory and IO-space addresses, IRQ lines etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * Called at initialisation of the PCI subsystem and by CardBus services.
Yu Zhao480b93b2009-03-20 11:25:14 +08001446 * Returns 0 on success and negative if unknown type of device (not normal,
1447 * bridge or CardBus).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001449int pci_setup_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 u32 class;
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001452 u16 cmd;
Yu Zhao480b93b2009-03-20 11:25:14 +08001453 u8 hdr_type;
Gabe Blackbc577d22009-10-06 10:45:19 -05001454 int pos = 0;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001455 struct pci_bus_region region;
1456 struct resource *res;
Yu Zhao480b93b2009-03-20 11:25:14 +08001457
1458 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
1459 return -EIO;
1460
1461 dev->sysdata = dev->bus->sysdata;
1462 dev->dev.parent = dev->bus->bridge;
1463 dev->dev.bus = &pci_bus_type;
1464 dev->hdr_type = hdr_type & 0x7f;
1465 dev->multifunction = !!(hdr_type & 0x80);
Yu Zhao480b93b2009-03-20 11:25:14 +08001466 dev->error_state = pci_channel_io_normal;
1467 set_pcie_port_type(dev);
1468
Yijing Wang017ffe62015-07-17 17:16:32 +08001469 pci_dev_assign_slot(dev);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001470
1471 /*
1472 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1473 * set this higher, assuming the system even supports it.
1474 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001475 dev->dma_mask = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Greg Kroah-Hartmaneebfcfb2008-07-02 13:24:49 -07001477 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1478 dev->bus->number, PCI_SLOT(dev->devfn),
1479 PCI_FUNC(dev->devfn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
Auke Kokb8a3a522007-06-08 15:46:30 -07001482 dev->revision = class & 0xff;
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001483 dev->class = class >> 8; /* upper 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Frederick Lawler7506dc72018-01-18 12:55:24 -06001485 pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n",
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001486 dev->vendor, dev->device, dev->hdr_type, dev->class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001488 /* Need to have dev->class ready */
Yu Zhao853346e2009-03-21 22:05:11 +08001489 dev->cfg_size = pci_cfg_space_size(dev);
1490
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001491 /* Need to have dev->cfg_size ready */
Lukas Wunner8531e282017-03-10 21:23:45 +01001492 set_pcie_thunderbolt(dev);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /* "Unknown power state" */
Daniel Ritz3fe9d192005-08-17 15:32:19 -07001495 dev->current_state = PCI_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /* Early fixups, before probing the BARs */
1498 pci_fixup_device(pci_fixup_early, dev);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001499
1500 /* Device class may be changed after fixup */
Yu Zhaof79b1b12009-05-28 00:25:05 +08001501 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001503 if (dev->non_compliant_bars) {
1504 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1505 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001506 pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001507 cmd &= ~PCI_COMMAND_IO;
1508 cmd &= ~PCI_COMMAND_MEMORY;
1509 pci_write_config_word(dev, PCI_COMMAND, cmd);
1510 }
1511 }
1512
Piotr Gregor99b3c582017-05-26 22:02:25 +01001513 dev->broken_intx_masking = pci_intx_mask_broken(dev);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 switch (dev->hdr_type) { /* header type */
1516 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1517 if (class == PCI_CLASS_BRIDGE_PCI)
1518 goto bad;
1519 pci_read_irq(dev);
1520 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
1521 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1522 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
Alan Cox368c73d2006-10-04 00:41:26 +01001523
1524 /*
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001525 * Do the ugly legacy mode stuff here rather than broken chip
1526 * quirk code. Legacy mode ATA controllers have fixed
1527 * addresses. These are not always echoed in BAR0-3, and
1528 * BAR0-3 in a few cases contain junk!
Alan Cox368c73d2006-10-04 00:41:26 +01001529 */
1530 if (class == PCI_CLASS_STORAGE_IDE) {
1531 u8 progif;
1532 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1533 if ((progif & 1) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001534 region.start = 0x1F0;
1535 region.end = 0x1F7;
1536 res = &dev->resource[0];
1537 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001538 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001539 pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001540 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001541 region.start = 0x3F6;
1542 region.end = 0x3F6;
1543 res = &dev->resource[1];
1544 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001545 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001546 pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001547 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001548 }
1549 if ((progif & 4) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001550 region.start = 0x170;
1551 region.end = 0x177;
1552 res = &dev->resource[2];
1553 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001554 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001555 pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001556 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001557 region.start = 0x376;
1558 region.end = 0x376;
1559 res = &dev->resource[3];
1560 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001561 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001562 pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001563 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001564 }
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567
1568 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1569 if (class != PCI_CLASS_BRIDGE_PCI)
1570 goto bad;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001571
1572 /*
1573 * The PCI-to-PCI bridge spec requires that subtractive
1574 * decoding (i.e. transparent) bridge must have programming
1575 * interface code of 0x01.
1576 */
Kristen Accardi3efd2732005-11-02 16:55:49 -08001577 pci_read_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 dev->transparent = ((dev->class & 0xff) == 1);
1579 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
Eric W. Biederman28760482009-09-09 14:09:24 -07001580 set_pcie_hotplug_bridge(dev);
Gabe Blackbc577d22009-10-06 10:45:19 -05001581 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1582 if (pos) {
1583 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1584 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
1587
1588 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1589 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1590 goto bad;
1591 pci_read_irq(dev);
1592 pci_read_bases(dev, 1, 0);
1593 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1594 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1595 break;
1596
1597 default: /* unknown header */
Frederick Lawler7506dc72018-01-18 12:55:24 -06001598 pci_err(dev, "unknown header type %02x, ignoring device\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001599 dev->hdr_type);
Yu Zhao480b93b2009-03-20 11:25:14 +08001600 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 bad:
Frederick Lawler7506dc72018-01-18 12:55:24 -06001603 pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001604 dev->class, dev->hdr_type);
Bjorn Helgaas2b4aed12015-06-19 16:20:58 -05001605 dev->class = PCI_CLASS_NOT_DEFINED << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607
1608 /* We found a fine healthy device, go go go... */
1609 return 0;
1610}
1611
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001612static void pci_configure_mps(struct pci_dev *dev)
1613{
1614 struct pci_dev *bridge = pci_upstream_bridge(dev);
Keith Busch27d868b2015-08-24 08:48:16 -05001615 int mps, p_mps, rc;
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001616
1617 if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
1618 return;
1619
1620 mps = pcie_get_mps(dev);
1621 p_mps = pcie_get_mps(bridge);
1622
1623 if (mps == p_mps)
1624 return;
1625
1626 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001627 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",
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001628 mps, pci_name(bridge), p_mps);
1629 return;
1630 }
Keith Busch27d868b2015-08-24 08:48:16 -05001631
1632 /*
1633 * Fancier MPS configuration is done later by
1634 * pcie_bus_configure_settings()
1635 */
1636 if (pcie_bus_config != PCIE_BUS_DEFAULT)
1637 return;
1638
1639 rc = pcie_set_mps(dev, p_mps);
1640 if (rc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001641 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
Keith Busch27d868b2015-08-24 08:48:16 -05001642 p_mps);
1643 return;
1644 }
1645
Frederick Lawler7506dc72018-01-18 12:55:24 -06001646 pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
Keith Busch27d868b2015-08-24 08:48:16 -05001647 p_mps, mps, 128 << dev->pcie_mpss);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001648}
1649
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001650static struct hpp_type0 pci_default_type0 = {
1651 .revision = 1,
1652 .cache_line_size = 8,
1653 .latency_timer = 0x40,
1654 .enable_serr = 0,
1655 .enable_perr = 0,
1656};
1657
1658static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1659{
1660 u16 pci_cmd, pci_bctl;
1661
Bjorn Helgaasc6285fc2014-08-29 18:10:19 -06001662 if (!hpp)
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001663 hpp = &pci_default_type0;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001664
1665 if (hpp->revision > 1) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001666 pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001667 hpp->revision);
1668 hpp = &pci_default_type0;
1669 }
1670
1671 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1672 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1673 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1674 if (hpp->enable_serr)
1675 pci_cmd |= PCI_COMMAND_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001676 if (hpp->enable_perr)
1677 pci_cmd |= PCI_COMMAND_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001678 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1679
1680 /* Program bridge control value */
1681 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1682 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1683 hpp->latency_timer);
1684 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1685 if (hpp->enable_serr)
1686 pci_bctl |= PCI_BRIDGE_CTL_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001687 if (hpp->enable_perr)
1688 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001689 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1690 }
1691}
1692
1693static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1694{
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001695 int pos;
1696
1697 if (!hpp)
1698 return;
1699
1700 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1701 if (!pos)
1702 return;
1703
Frederick Lawler7506dc72018-01-18 12:55:24 -06001704 pci_warn(dev, "PCI-X settings not supported\n");
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001705}
1706
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001707static bool pcie_root_rcb_set(struct pci_dev *dev)
1708{
1709 struct pci_dev *rp = pcie_find_root_port(dev);
1710 u16 lnkctl;
1711
1712 if (!rp)
1713 return false;
1714
1715 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
1716 if (lnkctl & PCI_EXP_LNKCTL_RCB)
1717 return true;
1718
1719 return false;
1720}
1721
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001722static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1723{
1724 int pos;
1725 u32 reg32;
1726
1727 if (!hpp)
1728 return;
1729
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001730 if (!pci_is_pcie(dev))
1731 return;
1732
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001733 if (hpp->revision > 1) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001734 pci_warn(dev, "PCIe settings rev %d not supported\n",
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001735 hpp->revision);
1736 return;
1737 }
1738
Bjorn Helgaas302328c2014-09-03 13:26:29 -06001739 /*
1740 * Don't allow _HPX to change MPS or MRRS settings. We manage
1741 * those to make sure they're consistent with the rest of the
1742 * platform.
1743 */
1744 hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
1745 PCI_EXP_DEVCTL_READRQ;
1746 hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
1747 PCI_EXP_DEVCTL_READRQ);
1748
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001749 /* Initialize Device Control Register */
1750 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1751 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1752
1753 /* Initialize Link Control Register */
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001754 if (pcie_cap_has_lnkctl(dev)) {
1755
1756 /*
1757 * If the Root Port supports Read Completion Boundary of
1758 * 128, set RCB to 128. Otherwise, clear it.
1759 */
1760 hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
1761 hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
1762 if (pcie_root_rcb_set(dev))
1763 hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
1764
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001765 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1766 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001767 }
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001768
1769 /* Find Advanced Error Reporting Enhanced Capability */
1770 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1771 if (!pos)
1772 return;
1773
1774 /* Initialize Uncorrectable Error Mask Register */
1775 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1776 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1777 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1778
1779 /* Initialize Uncorrectable Error Severity Register */
1780 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1781 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1782 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1783
1784 /* Initialize Correctable Error Mask Register */
1785 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1786 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1787 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1788
1789 /* Initialize Advanced Error Capabilities and Control Register */
1790 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1791 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001792
Bjorn Helgaas675734b2017-03-21 13:01:30 -05001793 /* Don't enable ECRC generation or checking if unsupported */
1794 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
1795 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
1796 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
1797 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001798 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1799
1800 /*
1801 * FIXME: The following two registers are not supported yet.
1802 *
1803 * o Secondary Uncorrectable Error Severity Register
1804 * o Secondary Uncorrectable Error Mask Register
1805 */
1806}
1807
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001808int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
Sinan Kaya60db3a42017-01-20 09:16:51 -05001809{
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001810 struct pci_host_bridge *host;
1811 u32 cap;
1812 u16 ctl;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001813 int ret;
1814
1815 if (!pci_is_pcie(dev))
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001816 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001817
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001818 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
Sinan Kaya60db3a42017-01-20 09:16:51 -05001819 if (ret)
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001820 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001821
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001822 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
1823 return 0;
1824
1825 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
1826 if (ret)
1827 return 0;
1828
1829 host = pci_find_host_bridge(dev->bus);
1830 if (!host)
1831 return 0;
1832
1833 /*
1834 * If some device in the hierarchy doesn't handle Extended Tags
1835 * correctly, make sure they're disabled.
1836 */
1837 if (host->no_ext_tags) {
1838 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001839 pci_info(dev, "disabling Extended Tags\n");
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001840 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1841 PCI_EXP_DEVCTL_EXT_TAG);
1842 }
1843 return 0;
1844 }
1845
1846 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001847 pci_info(dev, "enabling Extended Tags\n");
Sinan Kaya60db3a42017-01-20 09:16:51 -05001848 pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
1849 PCI_EXP_DEVCTL_EXT_TAG);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001850 }
1851 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001852}
1853
dingtianhonga99b6462017-08-15 11:23:23 +08001854/**
1855 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
1856 * @dev: PCI device to query
1857 *
1858 * Returns true if the device has enabled relaxed ordering attribute.
1859 */
1860bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
1861{
1862 u16 v;
1863
1864 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
1865
1866 return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
1867}
1868EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
1869
1870static void pci_configure_relaxed_ordering(struct pci_dev *dev)
1871{
1872 struct pci_dev *root;
1873
1874 /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
1875 if (dev->is_virtfn)
1876 return;
1877
1878 if (!pcie_relaxed_ordering_enabled(dev))
1879 return;
1880
1881 /*
1882 * For now, we only deal with Relaxed Ordering issues with Root
1883 * Ports. Peer-to-Peer DMA is another can of worms.
1884 */
1885 root = pci_find_pcie_root_port(dev);
1886 if (!root)
1887 return;
1888
1889 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
1890 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1891 PCI_EXP_DEVCTL_RELAX_EN);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001892 pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
dingtianhonga99b6462017-08-15 11:23:23 +08001893 }
1894}
1895
Bjorn Helgaasc46fd352017-11-28 16:43:50 -06001896static void pci_configure_ltr(struct pci_dev *dev)
1897{
1898#ifdef CONFIG_PCIEASPM
1899 u32 cap;
1900 struct pci_dev *bridge;
1901
1902 if (!pci_is_pcie(dev))
1903 return;
1904
1905 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
1906 if (!(cap & PCI_EXP_DEVCAP2_LTR))
1907 return;
1908
1909 /*
1910 * Software must not enable LTR in an Endpoint unless the Root
1911 * Complex and all intermediate Switches indicate support for LTR.
1912 * PCIe r3.1, sec 6.18.
1913 */
1914 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
1915 dev->ltr_path = 1;
1916 else {
1917 bridge = pci_upstream_bridge(dev);
1918 if (bridge && bridge->ltr_path)
1919 dev->ltr_path = 1;
1920 }
1921
1922 if (dev->ltr_path)
1923 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
1924 PCI_EXP_DEVCTL2_LTR_EN);
1925#endif
1926}
1927
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001928static void pci_configure_device(struct pci_dev *dev)
1929{
1930 struct hotplug_params hpp;
1931 int ret;
1932
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001933 pci_configure_mps(dev);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001934 pci_configure_extended_tags(dev, NULL);
dingtianhonga99b6462017-08-15 11:23:23 +08001935 pci_configure_relaxed_ordering(dev);
Bjorn Helgaasc46fd352017-11-28 16:43:50 -06001936 pci_configure_ltr(dev);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001937
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001938 memset(&hpp, 0, sizeof(hpp));
1939 ret = pci_get_hp_params(dev, &hpp);
1940 if (ret)
1941 return;
1942
1943 program_hpp_type2(dev, hpp.t2);
1944 program_hpp_type1(dev, hpp.t1);
1945 program_hpp_type0(dev, hpp.t0);
1946}
1947
Zhao, Yu201de562008-10-13 19:49:55 +08001948static void pci_release_capabilities(struct pci_dev *dev)
1949{
1950 pci_vpd_release(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08001951 pci_iov_release(dev);
Yinghai Luf7968412012-02-11 00:18:30 -08001952 pci_free_cap_save_buffers(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001956 * pci_release_dev - Free a PCI device structure when all users of it are
1957 * finished
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * @dev: device that's been disconnected
1959 *
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001960 * Will be called only by the device core when all users of this PCI device are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * done.
1962 */
1963static void pci_release_dev(struct device *dev)
1964{
Rafael J. Wysocki04480092014-02-01 15:38:29 +01001965 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Rafael J. Wysocki04480092014-02-01 15:38:29 +01001967 pci_dev = to_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001968 pci_release_capabilities(pci_dev);
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10001969 pci_release_of_node(pci_dev);
Sebastian Ott6ae32c52013-06-04 19:18:14 +02001970 pcibios_release_device(pci_dev);
Gu Zheng8b1fce02013-05-25 21:48:31 +08001971 pci_bus_put(pci_dev->bus);
Alex Williamson782a9852014-05-20 08:53:21 -06001972 kfree(pci_dev->driver_override);
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001973 kfree(pci_dev->dma_alias_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 kfree(pci_dev);
1975}
1976
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001977struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
Michael Ellerman65891212007-04-05 17:19:08 +10001978{
1979 struct pci_dev *dev;
1980
1981 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
1982 if (!dev)
1983 return NULL;
1984
Michael Ellerman65891212007-04-05 17:19:08 +10001985 INIT_LIST_HEAD(&dev->bus_list);
Brian King88e7b162013-04-08 03:05:07 +00001986 dev->dev.type = &pci_dev_type;
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001987 dev->bus = pci_bus_get(bus);
Michael Ellerman65891212007-04-05 17:19:08 +10001988
1989 return dev;
1990}
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001991EXPORT_SYMBOL(pci_alloc_dev);
1992
Sinan Kaya62bc6a62017-08-29 14:45:44 -05001993static bool pci_bus_crs_vendor_id(u32 l)
1994{
1995 return (l & 0xffff) == 0x0001;
1996}
1997
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001998static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
1999 int timeout)
Yinghai Luefdc87d2012-01-27 10:55:10 -08002000{
2001 int delay = 1;
2002
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002003 if (!pci_bus_crs_vendor_id(*l))
2004 return true; /* not a CRS completion */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002005
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002006 if (!timeout)
2007 return false; /* CRS, but caller doesn't want to wait */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002008
Rajat Jain89665a6a2014-09-08 14:19:49 -07002009 /*
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002010 * We got the reserved Vendor ID that indicates a completion with
2011 * Configuration Request Retry Status (CRS). Retry until we get a
2012 * valid Vendor ID or we time out.
Rajat Jain89665a6a2014-09-08 14:19:49 -07002013 */
Sinan Kaya62bc6a62017-08-29 14:45:44 -05002014 while (pci_bus_crs_vendor_id(*l)) {
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002015 if (delay > timeout) {
Sinan Kayae78e6612017-08-29 14:45:45 -05002016 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
2017 pci_domain_nr(bus), bus->number,
2018 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2019
Yinghai Luefdc87d2012-01-27 10:55:10 -08002020 return false;
2021 }
Sinan Kayae78e6612017-08-29 14:45:45 -05002022 if (delay >= 1000)
2023 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
2024 pci_domain_nr(bus), bus->number,
2025 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
Bjorn Helgaas9f982752017-08-29 14:45:43 -05002026
2027 msleep(delay);
2028 delay *= 2;
2029
2030 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2031 return false;
Yinghai Luefdc87d2012-01-27 10:55:10 -08002032 }
2033
Sinan Kayae78e6612017-08-29 14:45:45 -05002034 if (delay >= 1000)
2035 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
2036 pci_domain_nr(bus), bus->number,
2037 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2038
Yinghai Luefdc87d2012-01-27 10:55:10 -08002039 return true;
2040}
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002041
2042bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2043 int timeout)
2044{
Yinghai Luefdc87d2012-01-27 10:55:10 -08002045 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2046 return false;
2047
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002048 /* Some broken boards return 0 or ~0 if a slot is empty: */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002049 if (*l == 0xffffffff || *l == 0x00000000 ||
2050 *l == 0x0000ffff || *l == 0xffff0000)
2051 return false;
2052
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002053 if (pci_bus_crs_vendor_id(*l))
2054 return pci_bus_wait_crs(bus, devfn, l, timeout);
Yinghai Luefdc87d2012-01-27 10:55:10 -08002055
2056 return true;
2057}
2058EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002061 * Read the config data for a PCI device, sanity-check it,
2062 * and fill in the dev structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Adrian Bunk7f7b5de2008-04-18 13:53:55 -07002064static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct pci_dev *dev;
2067 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Yinghai Luefdc87d2012-01-27 10:55:10 -08002069 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return NULL;
2071
Gu Zheng8b1fce02013-05-25 21:48:31 +08002072 dev = pci_alloc_dev(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (!dev)
2074 return NULL;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 dev->devfn = devfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 dev->vendor = l & 0xffff;
2078 dev->device = (l >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10002080 pci_set_of_node(dev);
2081
Yu Zhao480b93b2009-03-20 11:25:14 +08002082 if (pci_setup_device(dev)) {
Gu Zheng8b1fce02013-05-25 21:48:31 +08002083 pci_bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 kfree(dev);
2085 return NULL;
2086 }
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002087
2088 return dev;
2089}
2090
Zhao, Yu201de562008-10-13 19:49:55 +08002091static void pci_init_capabilities(struct pci_dev *dev)
2092{
Sean O. Stalley938174e2015-10-29 17:35:39 -05002093 /* Enhanced Allocation */
2094 pci_ea_init(dev);
2095
Guilherme G. Piccolie80e7edc2015-10-21 12:17:35 -02002096 /* Setup MSI caps & disable MSI/MSI-X interrupts */
2097 pci_msi_setup_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002098
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002099 /* Buffers for saving PCIe and PCI-X capabilities */
2100 pci_allocate_cap_save_buffers(dev);
2101
Zhao, Yu201de562008-10-13 19:49:55 +08002102 /* Power Management */
2103 pci_pm_init(dev);
2104
2105 /* Vital Product Data */
Bjorn Helgaasf1cd93f2016-02-22 13:58:37 -06002106 pci_vpd_init(dev);
Yu Zhao58c3a722008-10-14 14:02:53 +08002107
2108 /* Alternative Routing-ID Forwarding */
Yijing Wang31ab2472013-01-15 11:12:17 +08002109 pci_configure_ari(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08002110
2111 /* Single Root I/O Virtualization */
2112 pci_iov_init(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07002113
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -05002114 /* Address Translation Services */
2115 pci_ats_init(dev);
2116
Allen Kayae21ee62009-10-07 10:27:17 -07002117 /* Enable ACS P2P upstream forwarding */
Chris Wright5d990b62009-12-04 12:15:21 -08002118 pci_enable_acs(dev);
Taku Izumib07461a2015-09-17 10:09:37 -05002119
Jonathan Yong9bb04a02016-06-11 14:13:38 -05002120 /* Precision Time Measurement */
2121 pci_ptm_init(dev);
Bjorn Helgaas4dc2db02016-10-03 09:42:57 -05002122
Keith Busch66b80802016-09-27 16:23:34 -04002123 /* Advanced Error Reporting */
2124 pci_aer_init(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002125}
2126
Marc Zyngier098259e2015-10-02 10:19:32 +01002127/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002128 * This is the equivalent of pci_host_bridge_msi_domain() that acts on
Marc Zyngier098259e2015-10-02 10:19:32 +01002129 * devices. Firmware interfaces that can select the MSI domain on a
2130 * per-device basis should be called from here.
2131 */
2132static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2133{
2134 struct irq_domain *d;
2135
2136 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002137 * If a domain has been set through the pcibios_add_device()
Marc Zyngier098259e2015-10-02 10:19:32 +01002138 * callback, then this is the one (platform code knows best).
2139 */
2140 d = dev_get_msi_domain(&dev->dev);
2141 if (d)
2142 return d;
2143
Marc Zyngier54fa97e2015-10-02 14:43:06 +01002144 /*
2145 * Let's see if we have a firmware interface able to provide
2146 * the domain.
2147 */
2148 d = pci_msi_get_device_domain(dev);
2149 if (d)
2150 return d;
2151
Marc Zyngier098259e2015-10-02 10:19:32 +01002152 return NULL;
2153}
2154
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002155static void pci_set_msi_domain(struct pci_dev *dev)
2156{
Marc Zyngier098259e2015-10-02 10:19:32 +01002157 struct irq_domain *d;
2158
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002159 /*
Marc Zyngier098259e2015-10-02 10:19:32 +01002160 * If the platform or firmware interfaces cannot supply a
2161 * device-specific MSI domain, then inherit the default domain
2162 * from the host bridge itself.
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002163 */
Marc Zyngier098259e2015-10-02 10:19:32 +01002164 d = pci_dev_msi_domain(dev);
2165 if (!d)
2166 d = dev_get_msi_domain(&dev->bus->dev);
2167
2168 dev_set_msi_domain(&dev->dev, d);
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002169}
2170
Sam Ravnborg96bde062007-03-26 21:53:30 -08002171void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002172{
Yinghai Lu4f535092013-01-21 13:20:52 -08002173 int ret;
2174
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06002175 pci_configure_device(dev);
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 device_initialize(&dev->dev);
2178 dev->dev.release = pci_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Yinghai Lu7629d192013-01-21 13:20:44 -08002180 set_dev_node(&dev->dev, pcibus_to_node(bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 dev->dev.dma_mask = &dev->dma_mask;
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002182 dev->dev.dma_parms = &dev->dma_parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 dev->dev.coherent_dma_mask = 0xffffffffull;
2184
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002185 pci_set_dma_max_seg_size(dev, 65536);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08002186 pci_set_dma_seg_boundary(dev, 0xffffffff);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /* Fix up broken headers */
2189 pci_fixup_device(pci_fixup_header, dev);
2190
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002191 /* Moved out from quirk header fixup code */
Yinghai Lu2069ecf2012-02-15 21:40:31 -08002192 pci_reassigndev_resource_alignment(dev);
2193
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002194 /* Clear the state_saved flag */
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02002195 dev->state_saved = false;
2196
Zhao, Yu201de562008-10-13 19:49:55 +08002197 /* Initialize various capabilities */
2198 pci_init_capabilities(dev);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /*
2201 * Add the device to our list of discovered devices
2202 * and the bus list for fixup functions, etc.
2203 */
Zhang Yanmind71374d2006-06-02 12:35:43 +08002204 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 list_add_tail(&dev->bus_list, &bus->devices);
Zhang Yanmind71374d2006-06-02 12:35:43 +08002206 up_write(&pci_bus_sem);
Yinghai Lu4f535092013-01-21 13:20:52 -08002207
Yinghai Lu4f535092013-01-21 13:20:52 -08002208 ret = pcibios_add_device(dev);
2209 WARN_ON(ret < 0);
2210
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002211 /* Set up MSI IRQ domain */
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002212 pci_set_msi_domain(dev);
2213
Yinghai Lu4f535092013-01-21 13:20:52 -08002214 /* Notifier could use PCI capabilities */
2215 dev->match_driver = false;
2216 ret = device_add(&dev->dev);
2217 WARN_ON(ret < 0);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002218}
2219
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002220struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002221{
2222 struct pci_dev *dev;
2223
Trent Piepho90bdb312009-03-20 14:56:00 -06002224 dev = pci_get_slot(bus, devfn);
2225 if (dev) {
2226 pci_dev_put(dev);
2227 return dev;
2228 }
2229
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002230 dev = pci_scan_device(bus, devfn);
2231 if (!dev)
2232 return NULL;
2233
2234 pci_device_add(dev, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 return dev;
2237}
Adrian Bunkb73e9682007-11-21 15:07:11 -08002238EXPORT_SYMBOL(pci_scan_single_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002240static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002241{
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002242 int pos;
2243 u16 cap = 0;
2244 unsigned next_fn;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002245
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002246 if (pci_ari_enabled(bus)) {
2247 if (!dev)
2248 return 0;
2249 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2250 if (!pos)
2251 return 0;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002252
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002253 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2254 next_fn = PCI_ARI_CAP_NFN(cap);
2255 if (next_fn <= fn)
2256 return 0; /* protect against malformed list */
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002257
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002258 return next_fn;
2259 }
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002260
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002261 /* dev may be NULL for non-contiguous multifunction devices */
2262 if (!dev || dev->multifunction)
2263 return (fn + 1) % 8;
2264
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002265 return 0;
2266}
2267
2268static int only_one_child(struct pci_bus *bus)
2269{
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002270 struct pci_dev *bridge = bus->self;
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002271
2272 /*
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002273 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
2274 * we scan for all possible devices, not just Device 0.
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002275 */
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002276 if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
2277 return 0;
2278
2279 /*
2280 * A PCIe Downstream Port normally leads to a Link with only Device
2281 * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan
2282 * only for Device 0 in that situation.
2283 *
2284 * Checking has_secondary_link is a hack to identify Downstream
2285 * Ports because sometimes Switches are configured such that the
2286 * PCIe Port Type labels are backwards.
2287 */
2288 if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002289 return 1;
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002290
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002291 return 0;
2292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002295 * pci_scan_slot - Scan a PCI slot on a bus for devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 * @bus: PCI bus to scan
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002297 * @devfn: slot number to scan (must have zero function)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 *
2299 * Scan a PCI slot on the specified PCI bus for devices, adding
2300 * discovered devices to the @bus->devices list. New devices
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -08002301 * will not have is_added set.
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002302 *
2303 * Returns the number of new devices found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 */
Sam Ravnborg96bde062007-03-26 21:53:30 -08002305int pci_scan_slot(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002307 unsigned fn, nr = 0;
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002308 struct pci_dev *dev;
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002309
2310 if (only_one_child(bus) && (devfn > 0))
2311 return 0; /* Already scanned the entire slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002313 dev = pci_scan_single_device(bus, devfn);
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002314 if (!dev)
2315 return 0;
2316 if (!dev->is_added)
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002317 nr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002319 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002320 dev = pci_scan_single_device(bus, devfn + fn);
2321 if (dev) {
2322 if (!dev->is_added)
2323 nr++;
2324 dev->multifunction = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 }
Shaohua Li7d715a62008-02-25 09:46:41 +08002327
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002328 /* Only one slot has PCIe device */
Shaohua Li149e1632008-07-23 10:32:31 +08002329 if (bus->self && nr)
Shaohua Li7d715a62008-02-25 09:46:41 +08002330 pcie_aspm_init_link_state(bus->self);
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return nr;
2333}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002334EXPORT_SYMBOL(pci_scan_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Jon Masonb03e7492011-07-20 15:20:54 -05002336static int pcie_find_smpss(struct pci_dev *dev, void *data)
2337{
2338 u8 *smpss = data;
2339
2340 if (!pci_is_pcie(dev))
2341 return 0;
2342
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002343 /*
2344 * We don't have a way to change MPS settings on devices that have
2345 * drivers attached. A hot-added device might support only the minimum
2346 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
2347 * where devices may be hot-added, we limit the fabric MPS to 128 so
2348 * hot-added devices will work correctly.
2349 *
2350 * However, if we hot-add a device to a slot directly below a Root
2351 * Port, it's impossible for there to be other existing devices below
2352 * the port. We don't limit the MPS in this case because we can
2353 * reconfigure MPS on both the Root Port and the hot-added device,
2354 * and there are no other devices involved.
2355 *
2356 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
Jon Masonb03e7492011-07-20 15:20:54 -05002357 */
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002358 if (dev->is_hotplug_bridge &&
2359 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
Jon Masonb03e7492011-07-20 15:20:54 -05002360 *smpss = 0;
2361
2362 if (*smpss > dev->pcie_mpss)
2363 *smpss = dev->pcie_mpss;
2364
2365 return 0;
2366}
2367
2368static void pcie_write_mps(struct pci_dev *dev, int mps)
2369{
Jon Mason62f392e2011-10-14 14:56:14 -05002370 int rc;
Jon Masonb03e7492011-07-20 15:20:54 -05002371
2372 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
Jon Mason62f392e2011-10-14 14:56:14 -05002373 mps = 128 << dev->pcie_mpss;
Jon Masonb03e7492011-07-20 15:20:54 -05002374
Yijing Wang62f87c02012-07-24 17:20:03 +08002375 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2376 dev->bus->self)
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002377
2378 /*
2379 * For "Performance", the assumption is made that
Jon Masonb03e7492011-07-20 15:20:54 -05002380 * downstream communication will never be larger than
2381 * the MRRS. So, the MPS only needs to be configured
2382 * for the upstream communication. This being the case,
2383 * walk from the top down and set the MPS of the child
2384 * to that of the parent bus.
Jon Mason62f392e2011-10-14 14:56:14 -05002385 *
2386 * Configure the device MPS with the smaller of the
2387 * device MPSS or the bridge MPS (which is assumed to be
2388 * properly configured at this point to the largest
2389 * allowable MPS based on its parent bus).
Jon Masonb03e7492011-07-20 15:20:54 -05002390 */
Jon Mason62f392e2011-10-14 14:56:14 -05002391 mps = min(mps, pcie_get_mps(dev->bus->self));
Jon Masonb03e7492011-07-20 15:20:54 -05002392 }
2393
2394 rc = pcie_set_mps(dev, mps);
2395 if (rc)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002396 pci_err(dev, "Failed attempting to set the MPS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002397}
2398
Jon Mason62f392e2011-10-14 14:56:14 -05002399static void pcie_write_mrrs(struct pci_dev *dev)
Jon Masonb03e7492011-07-20 15:20:54 -05002400{
Jon Mason62f392e2011-10-14 14:56:14 -05002401 int rc, mrrs;
Jon Masonb03e7492011-07-20 15:20:54 -05002402
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002403 /*
2404 * In the "safe" case, do not configure the MRRS. There appear to be
Jon Masoned2888e2011-09-08 16:41:18 -05002405 * issues with setting MRRS to 0 on a number of devices.
2406 */
Jon Masoned2888e2011-09-08 16:41:18 -05002407 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2408 return;
Jon Masonb03e7492011-07-20 15:20:54 -05002409
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002410 /*
2411 * For max performance, the MRRS must be set to the largest supported
Jon Masoned2888e2011-09-08 16:41:18 -05002412 * value. However, it cannot be configured larger than the MPS the
Jon Mason62f392e2011-10-14 14:56:14 -05002413 * device or the bus can support. This should already be properly
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002414 * configured by a prior call to pcie_write_mps().
Jon Masoned2888e2011-09-08 16:41:18 -05002415 */
Jon Mason62f392e2011-10-14 14:56:14 -05002416 mrrs = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002417
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002418 /*
2419 * MRRS is a R/W register. Invalid values can be written, but a
Jon Masoned2888e2011-09-08 16:41:18 -05002420 * subsequent read will verify if the value is acceptable or not.
Jon Masonb03e7492011-07-20 15:20:54 -05002421 * If the MRRS value provided is not acceptable (e.g., too large),
2422 * shrink the value until it is acceptable to the HW.
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002423 */
Jon Masonb03e7492011-07-20 15:20:54 -05002424 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2425 rc = pcie_set_readrq(dev, mrrs);
Jon Mason62f392e2011-10-14 14:56:14 -05002426 if (!rc)
2427 break;
Jon Masonb03e7492011-07-20 15:20:54 -05002428
Frederick Lawler7506dc72018-01-18 12:55:24 -06002429 pci_warn(dev, "Failed attempting to set the MRRS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002430 mrrs /= 2;
2431 }
Jon Mason62f392e2011-10-14 14:56:14 -05002432
2433 if (mrrs < 128)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002434 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");
Jon Masonb03e7492011-07-20 15:20:54 -05002435}
2436
2437static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2438{
Jon Masona513a99a72011-10-14 14:56:16 -05002439 int mps, orig_mps;
Jon Masonb03e7492011-07-20 15:20:54 -05002440
2441 if (!pci_is_pcie(dev))
2442 return 0;
2443
Keith Busch27d868b2015-08-24 08:48:16 -05002444 if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2445 pcie_bus_config == PCIE_BUS_DEFAULT)
Yijing Wang5895af72013-08-26 16:33:06 +08002446 return 0;
Yijing Wang5895af72013-08-26 16:33:06 +08002447
Jon Masona513a99a72011-10-14 14:56:16 -05002448 mps = 128 << *(u8 *)data;
2449 orig_mps = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002450
2451 pcie_write_mps(dev, mps);
Jon Mason62f392e2011-10-14 14:56:14 -05002452 pcie_write_mrrs(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002453
Frederick Lawler7506dc72018-01-18 12:55:24 -06002454 pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04002455 pcie_get_mps(dev), 128 << dev->pcie_mpss,
Jon Masona513a99a72011-10-14 14:56:16 -05002456 orig_mps, pcie_get_readrq(dev));
Jon Masonb03e7492011-07-20 15:20:54 -05002457
2458 return 0;
2459}
2460
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002461/*
2462 * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
Jon Masonb03e7492011-07-20 15:20:54 -05002463 * parents then children fashion. If this changes, then this code will not
2464 * work as designed.
2465 */
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002466void pcie_bus_configure_settings(struct pci_bus *bus)
Jon Masonb03e7492011-07-20 15:20:54 -05002467{
Bjorn Helgaas1e358f92014-04-29 12:51:55 -06002468 u8 smpss = 0;
Jon Masonb03e7492011-07-20 15:20:54 -05002469
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002470 if (!bus->self)
2471 return;
2472
Jon Masonb03e7492011-07-20 15:20:54 -05002473 if (!pci_is_pcie(bus->self))
2474 return;
2475
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002476 /*
2477 * FIXME - Peer to peer DMA is possible, though the endpoint would need
Jon Mason33154722013-08-26 16:33:05 +08002478 * to be aware of the MPS of the destination. To work around this,
Jon Mason5f39e672011-10-03 09:50:20 -05002479 * simply force the MPS of the entire system to the smallest possible.
2480 */
2481 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2482 smpss = 0;
2483
Jon Masonb03e7492011-07-20 15:20:54 -05002484 if (pcie_bus_config == PCIE_BUS_SAFE) {
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002485 smpss = bus->self->pcie_mpss;
Jon Mason5f39e672011-10-03 09:50:20 -05002486
Jon Masonb03e7492011-07-20 15:20:54 -05002487 pcie_find_smpss(bus->self, &smpss);
2488 pci_walk_bus(bus, pcie_find_smpss, &smpss);
2489 }
2490
2491 pcie_bus_configure_set(bus->self, &smpss);
2492 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2493}
Jon Masondebc3b72011-08-02 00:01:18 -05002494EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
Jon Masonb03e7492011-07-20 15:20:54 -05002495
Palmer Dabbeltbccf90d2017-06-23 18:50:42 -07002496/*
2497 * Called after each bus is probed, but before its children are examined. This
2498 * is marked as __weak because multiple architectures define it.
2499 */
2500void __weak pcibios_fixup_bus(struct pci_bus *bus)
2501{
2502 /* nothing to do, expected to be removed in the future */
2503}
2504
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002505/**
2506 * pci_scan_child_bus_extend() - Scan devices below a bus
2507 * @bus: Bus to scan for devices
2508 * @available_buses: Total number of buses available (%0 does not try to
2509 * extend beyond the minimal)
2510 *
2511 * Scans devices below @bus including subordinate buses. Returns new
2512 * subordinate number including all the found devices. Passing
2513 * @available_buses causes the remaining bus space to be distributed
2514 * equally between hotplug-capable bridges to allow future extension of the
2515 * hierarchy.
2516 */
2517static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2518 unsigned int available_buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002520 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2521 unsigned int start = bus->busn_res.start;
Jan Kiszka690f4302018-03-07 08:39:13 +01002522 unsigned int devfn, fn, cmax, max = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct pci_dev *dev;
Jan Kiszka690f4302018-03-07 08:39:13 +01002524 int nr_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002526 dev_dbg(&bus->dev, "scanning bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 /* Go find them, Rover! */
Jan Kiszka690f4302018-03-07 08:39:13 +01002529 for (devfn = 0; devfn < 256; devfn += 8) {
2530 nr_devs = pci_scan_slot(bus, devfn);
2531
2532 /*
2533 * The Jailhouse hypervisor may pass individual functions of a
2534 * multi-function device to a guest without passing function 0.
2535 * Look for them as well.
2536 */
2537 if (jailhouse_paravirt() && nr_devs == 0) {
2538 for (fn = 1; fn < 8; fn++) {
2539 dev = pci_scan_single_device(bus, devfn + fn);
2540 if (dev)
2541 dev->multifunction = 1;
2542 }
2543 }
2544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002546 /* Reserve buses for SR-IOV capability */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002547 used_buses = pci_iov_bus_range(bus);
2548 max += used_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +08002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /*
2551 * After performing arch-dependent fixup of the bus, look behind
2552 * all PCI-to-PCI bridges on this bus.
2553 */
Alex Chiang74710de2009-03-20 14:56:10 -06002554 if (!bus->is_added) {
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002555 dev_dbg(&bus->dev, "fixups for bus\n");
Alex Chiang74710de2009-03-20 14:56:10 -06002556 pcibios_fixup_bus(bus);
Jiang Liu981cf9e2013-04-12 05:44:16 +00002557 bus->is_added = 1;
Alex Chiang74710de2009-03-20 14:56:10 -06002558 }
2559
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002560 /*
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002561 * Calculate how many hotplug bridges and normal bridges there
2562 * are on this bus. We will distribute the additional available
2563 * buses between hotplug bridges.
2564 */
2565 for_each_pci_bridge(dev, bus) {
2566 if (dev->is_hotplug_bridge)
2567 hotplug_bridges++;
2568 else
2569 normal_bridges++;
2570 }
2571
2572 /*
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002573 * Scan bridges that are already configured. We don't touch them
2574 * unless they are misconfigured (which will be done in the second
2575 * scan below).
2576 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002577 for_each_pci_bridge(dev, bus) {
2578 cmax = max;
2579 max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
2580 used_buses += cmax - max;
2581 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002582
2583 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002584 for_each_pci_bridge(dev, bus) {
2585 unsigned int buses = 0;
2586
2587 if (!hotplug_bridges && normal_bridges == 1) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002588
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002589 /*
2590 * There is only one bridge on the bus (upstream
2591 * port) so it gets all available buses which it
2592 * can then distribute to the possible hotplug
2593 * bridges below.
2594 */
2595 buses = available_buses;
2596 } else if (dev->is_hotplug_bridge) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002597
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002598 /*
2599 * Distribute the extra buses between hotplug
2600 * bridges if any.
2601 */
2602 buses = available_buses / hotplug_bridges;
2603 buses = min(buses, available_buses - used_buses);
2604 }
2605
2606 cmax = max;
2607 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
2608 used_buses += max - cmax;
2609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611 /*
Keith Busche16b4662016-07-21 21:40:28 -06002612 * Make sure a hotplug bridge has at least the minimum requested
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002613 * number of buses but allow it to grow up to the maximum available
2614 * bus number of there is room.
Keith Busche16b4662016-07-21 21:40:28 -06002615 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002616 if (bus->self && bus->self->is_hotplug_bridge) {
2617 used_buses = max_t(unsigned int, available_buses,
2618 pci_hotplug_bus_size - 1);
2619 if (max - start < used_buses) {
2620 max = start + used_buses;
Mika Westerberga20c7f32017-10-13 21:35:43 +03002621
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002622 /* Do not allocate more buses than we have room left */
2623 if (max > bus->busn_res.end)
2624 max = bus->busn_res.end;
2625
2626 dev_dbg(&bus->dev, "%pR extended by %#02x\n",
2627 &bus->busn_res, max - start);
2628 }
Keith Busche16b4662016-07-21 21:40:28 -06002629 }
2630
2631 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 * We've scanned the bus and so we know all about what's on
2633 * the other side of any bridges that may be on this bus plus
2634 * any devices.
2635 *
2636 * Return how far we've got finding sub-buses.
2637 */
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002638 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return max;
2640}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002641
2642/**
2643 * pci_scan_child_bus() - Scan devices below a bus
2644 * @bus: Bus to scan for devices
2645 *
2646 * Scans devices below @bus including subordinate buses. Returns new
2647 * subordinate number including all the found devices.
2648 */
2649unsigned int pci_scan_child_bus(struct pci_bus *bus)
2650{
2651 return pci_scan_child_bus_extend(bus, 0);
2652}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002653EXPORT_SYMBOL_GPL(pci_scan_child_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002655/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002656 * pcibios_root_bridge_prepare - Platform-specific host bridge setup
2657 * @bridge: Host bridge to set up
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002658 *
2659 * Default empty implementation. Replace with an architecture-specific setup
2660 * routine, if necessary.
2661 */
2662int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
2663{
2664 return 0;
2665}
2666
Jiang Liu10a95742013-04-12 05:44:20 +00002667void __weak pcibios_add_bus(struct pci_bus *bus)
2668{
2669}
2670
2671void __weak pcibios_remove_bus(struct pci_bus *bus)
2672{
2673}
2674
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002675struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
2676 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07002678 int error;
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07002679 struct pci_host_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Thierry Reding59094062016-11-25 11:57:10 +01002681 bridge = pci_alloc_host_bridge(0);
Yinghai Lu7b543662012-04-02 18:31:53 -07002682 if (!bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002683 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -07002684
2685 bridge->dev.parent = parent;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002686
2687 list_splice_init(resources, &bridge->windows);
2688 bridge->sysdata = sysdata;
2689 bridge->busnr = bus;
2690 bridge->ops = ops;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002691
2692 error = pci_register_host_bridge(bridge);
2693 if (error < 0)
Jiang Liu343df772013-06-07 01:10:08 +08002694 goto err_out;
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002695
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002696 return bridge->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Yinghai Lu7b543662012-04-02 18:31:53 -07002698err_out:
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002699 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return NULL;
2701}
Ray Juie6b29de2015-04-08 11:21:33 -07002702EXPORT_SYMBOL_GPL(pci_create_root_bus);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002703
Cyrille Pitchen49b8e3f2018-01-30 21:56:52 +01002704int pci_host_probe(struct pci_host_bridge *bridge)
2705{
2706 struct pci_bus *bus, *child;
2707 int ret;
2708
2709 ret = pci_scan_root_bus_bridge(bridge);
2710 if (ret < 0) {
2711 dev_err(bridge->dev.parent, "Scanning root bridge failed");
2712 return ret;
2713 }
2714
2715 bus = bridge->bus;
2716
2717 /*
2718 * We insert PCI resources into the iomem_resource and
2719 * ioport_resource trees in either pci_bus_claim_resources()
2720 * or pci_bus_assign_resources().
2721 */
2722 if (pci_has_flag(PCI_PROBE_ONLY)) {
2723 pci_bus_claim_resources(bus);
2724 } else {
2725 pci_bus_size_bridges(bus);
2726 pci_bus_assign_resources(bus);
2727
2728 list_for_each_entry(child, &bus->children, node)
2729 pcie_bus_configure_settings(child);
2730 }
2731
2732 pci_bus_add_devices(bus);
2733 return 0;
2734}
2735EXPORT_SYMBOL_GPL(pci_host_probe);
2736
Yinghai Lu98a35832012-05-18 11:35:50 -06002737int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2738{
2739 struct resource *res = &b->busn_res;
2740 struct resource *parent_res, *conflict;
2741
2742 res->start = bus;
2743 res->end = bus_max;
2744 res->flags = IORESOURCE_BUS;
2745
2746 if (!pci_is_root_bus(b))
2747 parent_res = &b->parent->busn_res;
2748 else {
2749 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
2750 res->flags |= IORESOURCE_PCI_FIXED;
2751 }
2752
Andreas Noeverced04d12014-01-23 21:59:24 +01002753 conflict = request_resource_conflict(parent_res, res);
Yinghai Lu98a35832012-05-18 11:35:50 -06002754
2755 if (conflict)
2756 dev_printk(KERN_DEBUG, &b->dev,
2757 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
2758 res, pci_is_root_bus(b) ? "domain " : "",
2759 parent_res, conflict->name, conflict);
Yinghai Lu98a35832012-05-18 11:35:50 -06002760
2761 return conflict == NULL;
2762}
2763
2764int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
2765{
2766 struct resource *res = &b->busn_res;
2767 struct resource old_res = *res;
2768 resource_size_t size;
2769 int ret;
2770
2771 if (res->start > bus_max)
2772 return -EINVAL;
2773
2774 size = bus_max - res->start + 1;
2775 ret = adjust_resource(res, res->start, size);
2776 dev_printk(KERN_DEBUG, &b->dev,
2777 "busn_res: %pR end %s updated to %02x\n",
2778 &old_res, ret ? "can not be" : "is", bus_max);
2779
2780 if (!ret && !res->parent)
2781 pci_bus_insert_busn_res(b, res->start, res->end);
2782
2783 return ret;
2784}
2785
2786void pci_bus_release_busn_res(struct pci_bus *b)
2787{
2788 struct resource *res = &b->busn_res;
2789 int ret;
2790
2791 if (!res->flags || !res->parent)
2792 return;
2793
2794 ret = release_resource(res);
2795 dev_printk(KERN_DEBUG, &b->dev,
2796 "busn_res: %pR %s released\n",
2797 res, ret ? "can not be" : "is");
2798}
2799
Lorenzo Pieralisi1228c4b2017-06-28 15:13:55 -05002800int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
2801{
2802 struct resource_entry *window;
2803 bool found = false;
2804 struct pci_bus *b;
2805 int max, bus, ret;
2806
2807 if (!bridge)
2808 return -EINVAL;
2809
2810 resource_list_for_each_entry(window, &bridge->windows)
2811 if (window->res->flags & IORESOURCE_BUS) {
2812 found = true;
2813 break;
2814 }
2815
2816 ret = pci_register_host_bridge(bridge);
2817 if (ret < 0)
2818 return ret;
2819
2820 b = bridge->bus;
2821 bus = bridge->busnr;
2822
2823 if (!found) {
2824 dev_info(&b->dev,
2825 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2826 bus);
2827 pci_bus_insert_busn_res(b, bus, 255);
2828 }
2829
2830 max = pci_scan_child_bus(b);
2831
2832 if (!found)
2833 pci_bus_update_busn_res_end(b, max);
2834
2835 return 0;
2836}
2837EXPORT_SYMBOL(pci_scan_root_bus_bridge);
2838
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002839struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
2840 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002841{
Jiang Liu14d76b62015-02-05 13:44:44 +08002842 struct resource_entry *window;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002843 bool found = false;
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002844 struct pci_bus *b;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002845 int max;
2846
Jiang Liu14d76b62015-02-05 13:44:44 +08002847 resource_list_for_each_entry(window, resources)
Yinghai Lu4d99f522012-05-17 18:51:12 -07002848 if (window->res->flags & IORESOURCE_BUS) {
2849 found = true;
2850 break;
2851 }
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002852
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002853 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002854 if (!b)
2855 return NULL;
2856
Yinghai Lu4d99f522012-05-17 18:51:12 -07002857 if (!found) {
2858 dev_info(&b->dev,
2859 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2860 bus);
2861 pci_bus_insert_busn_res(b, bus, 255);
2862 }
2863
2864 max = pci_scan_child_bus(b);
2865
2866 if (!found)
2867 pci_bus_update_busn_res_end(b, max);
2868
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002869 return b;
2870}
2871EXPORT_SYMBOL(pci_scan_root_bus);
2872
Bill Pemberton15856ad2012-11-21 15:35:00 -05002873struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002874 void *sysdata)
2875{
2876 LIST_HEAD(resources);
2877 struct pci_bus *b;
2878
2879 pci_add_resource(&resources, &ioport_resource);
2880 pci_add_resource(&resources, &iomem_resource);
Yinghai Lu857c3b62012-05-17 18:51:12 -07002881 pci_add_resource(&resources, &busn_resource);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002882 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
2883 if (b) {
Yinghai Lu857c3b62012-05-17 18:51:12 -07002884 pci_scan_child_bus(b);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002885 } else {
2886 pci_free_resource_list(&resources);
2887 }
2888 return b;
2889}
2890EXPORT_SYMBOL(pci_scan_bus);
2891
Alex Chiang3ed4fd92009-03-20 14:56:25 -06002892/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002893 * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
Yinghai Lu2f320522012-01-21 02:08:22 -08002894 * @bridge: PCI bridge for the bus to scan
2895 *
2896 * Scan a PCI bus and child buses for new devices, add them,
2897 * and enable them, resizing bridge mmio/io resource if necessary
2898 * and possible. The caller must ensure the child devices are already
2899 * removed for resizing to occur.
2900 *
2901 * Returns the max number of subordinate bus discovered.
2902 */
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002903unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
Yinghai Lu2f320522012-01-21 02:08:22 -08002904{
2905 unsigned int max;
2906 struct pci_bus *bus = bridge->subordinate;
2907
2908 max = pci_scan_child_bus(bus);
2909
2910 pci_assign_unassigned_bridge_resources(bridge);
2911
2912 pci_bus_add_devices(bus);
2913
2914 return max;
2915}
2916
Yinghai Lua5213a32012-10-30 14:31:21 -06002917/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002918 * pci_rescan_bus - Scan a PCI bus for devices
Yinghai Lua5213a32012-10-30 14:31:21 -06002919 * @bus: PCI bus to scan
2920 *
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002921 * Scan a PCI bus and child buses for new devices, add them,
2922 * and enable them.
Yinghai Lua5213a32012-10-30 14:31:21 -06002923 *
2924 * Returns the max number of subordinate bus discovered.
2925 */
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002926unsigned int pci_rescan_bus(struct pci_bus *bus)
Yinghai Lua5213a32012-10-30 14:31:21 -06002927{
2928 unsigned int max;
2929
2930 max = pci_scan_child_bus(bus);
2931 pci_assign_unassigned_bus_resources(bus);
2932 pci_bus_add_devices(bus);
2933
2934 return max;
2935}
2936EXPORT_SYMBOL_GPL(pci_rescan_bus);
2937
Rafael J. Wysocki9d169472014-01-10 15:22:18 +01002938/*
2939 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
2940 * routines should always be executed under this mutex.
2941 */
2942static DEFINE_MUTEX(pci_rescan_remove_lock);
2943
2944void pci_lock_rescan_remove(void)
2945{
2946 mutex_lock(&pci_rescan_remove_lock);
2947}
2948EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
2949
2950void pci_unlock_rescan_remove(void)
2951{
2952 mutex_unlock(&pci_rescan_remove_lock);
2953}
2954EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
2955
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002956static int __init pci_sort_bf_cmp(const struct device *d_a,
2957 const struct device *d_b)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002958{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002959 const struct pci_dev *a = to_pci_dev(d_a);
2960 const struct pci_dev *b = to_pci_dev(d_b);
2961
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002962 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
2963 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
2964
2965 if (a->bus->number < b->bus->number) return -1;
2966 else if (a->bus->number > b->bus->number) return 1;
2967
2968 if (a->devfn < b->devfn) return -1;
2969 else if (a->devfn > b->devfn) return 1;
2970
2971 return 0;
2972}
2973
Greg Kroah-Hartman5ff580c2008-02-14 14:56:56 -08002974void __init pci_sort_breadthfirst(void)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002975{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002976 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002977}
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002978
2979int pci_hp_add_bridge(struct pci_dev *dev)
2980{
2981 struct pci_bus *parent = dev->bus;
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002982 int busnr, start = parent->busn_res.start;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002983 unsigned int available_buses = 0;
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002984 int end = parent->busn_res.end;
2985
2986 for (busnr = start; busnr <= end; busnr++) {
2987 if (!pci_find_bus(pci_domain_nr(parent), busnr))
2988 break;
2989 }
2990 if (busnr-- > end) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002991 pci_err(dev, "No bus number available for hot-added bridge\n");
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002992 return -1;
2993 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002994
2995 /* Scan bridges that are already configured */
2996 busnr = pci_scan_bridge(parent, dev, busnr, 0);
2997
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002998 /*
2999 * Distribute the available bus numbers between hotplug-capable
3000 * bridges to make extending the chain later possible.
3001 */
3002 available_buses = end - busnr;
3003
Mika Westerberg4147c2f2017-10-13 21:35:42 +03003004 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03003005 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
Mika Westerberg4147c2f2017-10-13 21:35:42 +03003006
Mika Westerberg95e3ba92017-10-13 21:35:41 +03003007 if (!dev->subordinate)
3008 return -1;
3009
3010 return 0;
3011}
3012EXPORT_SYMBOL_GPL(pci_hp_add_bridge);