blob: 3ea2d610d607ebb9c2f879e4436473a38a13614c [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>
Jake Oshins788858e2016-02-16 21:56:22 +000019#include <linux/irqdomain.h>
Mika Westerbergd963f652016-06-02 11:17:13 +030020#include <linux/pm_runtime.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090021#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
24#define CARDBUS_RESERVE_BUSNR 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Stephen Hemminger0b950f02014-01-10 17:14:48 -070026static struct resource busn_resource = {
Yinghai Lu67cdc822012-05-17 18:51:12 -070027 .name = "PCI busn",
28 .start = 0,
29 .end = 255,
30 .flags = IORESOURCE_BUS,
31};
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* Ugh. Need to stop exporting this to modules. */
34LIST_HEAD(pci_root_buses);
35EXPORT_SYMBOL(pci_root_buses);
36
Yinghai Lu5cc62c22012-05-17 18:51:11 -070037static LIST_HEAD(pci_domain_busn_res_list);
38
39struct pci_domain_busn_res {
40 struct list_head list;
41 struct resource res;
42 int domain_nr;
43};
44
45static struct resource *get_pci_domain_busn_res(int domain_nr)
46{
47 struct pci_domain_busn_res *r;
48
49 list_for_each_entry(r, &pci_domain_busn_res_list, list)
50 if (r->domain_nr == domain_nr)
51 return &r->res;
52
53 r = kzalloc(sizeof(*r), GFP_KERNEL);
54 if (!r)
55 return NULL;
56
57 r->domain_nr = domain_nr;
58 r->res.start = 0;
59 r->res.end = 0xff;
60 r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
61
62 list_add_tail(&r->list, &pci_domain_busn_res_list);
63
64 return &r->res;
65}
66
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080067static int find_anything(struct device *dev, void *data)
68{
69 return 1;
70}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070072/*
73 * Some device drivers need know if pci is initiated.
74 * Basically, we think pci is not initiated when there
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080075 * is no device to be found on the pci_bus_type.
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070076 */
77int no_pci_devices(void)
78{
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080079 struct device *dev;
80 int no_devices;
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070081
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080082 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
83 no_devices = (dev == NULL);
84 put_device(dev);
85 return no_devices;
86}
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070087EXPORT_SYMBOL(no_pci_devices);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * PCI Bus Class
91 */
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040092static void release_pcibus_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040094 struct pci_bus *pci_bus = to_pci_bus(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Markus Elfringff0387c2014-11-10 21:02:17 -070096 put_device(pci_bus->bridge);
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070097 pci_bus_remove_resources(pci_bus);
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100098 pci_release_bus_of_node(pci_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kfree(pci_bus);
100}
101
102static struct class pcibus_class = {
103 .name = "pci_bus",
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400104 .dev_release = &release_pcibus_dev,
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700105 .dev_groups = pcibus_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108static int __init pcibus_class_init(void)
109{
110 return class_register(&pcibus_class);
111}
112postcore_initcall(pcibus_class_init);
113
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400114static u64 pci_size(u64 base, u64 maxbase, u64 mask)
Yinghai Lu07eddf32006-11-29 13:53:10 -0800115{
116 u64 size = mask & maxbase; /* Find the significant bits */
117 if (!size)
118 return 0;
119
120 /* Get the lowest of them to find the decode size, and
121 from that the extent. */
122 size = (size & ~(size-1)) - 1;
123
124 /* base == maxbase can be valid only if the BAR has
125 already been programmed with all 1s. */
126 if (base == maxbase && ((base | size) & mask) != mask)
127 return 0;
128
129 return size;
130}
131
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600132static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
Yinghai Lu07eddf32006-11-29 13:53:10 -0800133{
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600134 u32 mem_type;
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600135 unsigned long flags;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600136
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400137 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600138 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
139 flags |= IORESOURCE_IO;
140 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400141 }
142
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600143 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
144 flags |= IORESOURCE_MEM;
145 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
146 flags |= IORESOURCE_PREFETCH;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400147
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600148 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
149 switch (mem_type) {
150 case PCI_BASE_ADDRESS_MEM_TYPE_32:
151 break;
152 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600153 /* 1M mem BAR treated as 32-bit BAR */
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600154 break;
155 case PCI_BASE_ADDRESS_MEM_TYPE_64:
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600156 flags |= IORESOURCE_MEM_64;
157 break;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600158 default:
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600159 /* mem unknown type treated as 32-bit BAR */
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600160 break;
161 }
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600162 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400163}
164
Zoltan Kiss808e34e2013-08-22 23:19:18 +0100165#define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
166
Yu Zhao0b400c72008-11-22 02:40:40 +0800167/**
168 * pci_read_base - read a PCI BAR
169 * @dev: the PCI device
170 * @type: type of the BAR
171 * @res: resource buffer to be filled in
172 * @pos: BAR position in the config space
173 *
174 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400175 */
Yu Zhao0b400c72008-11-22 02:40:40 +0800176int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400177 struct resource *res, unsigned int pos)
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400178{
Marc Gonzalezdc5205e2017-04-10 19:46:54 +0200179 u32 l = 0, sz = 0, mask;
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600180 u64 l64, sz64, mask64;
Jacob Pan253d2e52010-07-16 10:19:22 -0700181 u16 orig_cmd;
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800182 struct pci_bus_region region, inverted_region;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400183
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200184 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400185
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600186 /* No printks while decoding is disabled! */
Jacob Pan253d2e52010-07-16 10:19:22 -0700187 if (!dev->mmio_always_on) {
188 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
Zoltan Kiss808e34e2013-08-22 23:19:18 +0100189 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
190 pci_write_config_word(dev, PCI_COMMAND,
191 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
192 }
Jacob Pan253d2e52010-07-16 10:19:22 -0700193 }
194
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400195 res->name = pci_name(dev);
196
197 pci_read_config_dword(dev, pos, &l);
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200198 pci_write_config_dword(dev, pos, l | mask);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400199 pci_read_config_dword(dev, pos, &sz);
200 pci_write_config_dword(dev, pos, l);
201
202 /*
203 * All bits set in sz means the device isn't working properly.
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600204 * If the BAR isn't implemented, all bits must be 0. If it's a
205 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
206 * 1 must be clear.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400207 */
Myron Stowef795d862014-10-30 11:54:43 -0600208 if (sz == 0xffffffff)
209 sz = 0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400210
211 /*
212 * I don't know how l can have all bits set. Copied from old code.
213 * Maybe it fixes a bug on some ancient platform.
214 */
215 if (l == 0xffffffff)
216 l = 0;
217
218 if (type == pci_bar_unknown) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600219 res->flags = decode_bar(dev, l);
220 res->flags |= IORESOURCE_SIZEALIGN;
221 if (res->flags & IORESOURCE_IO) {
Myron Stowef795d862014-10-30 11:54:43 -0600222 l64 = l & PCI_BASE_ADDRESS_IO_MASK;
223 sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
224 mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400225 } else {
Myron Stowef795d862014-10-30 11:54:43 -0600226 l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
227 sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
228 mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400229 }
230 } else {
Bjorn Helgaas7a6d3122016-11-28 17:21:02 -0600231 if (l & PCI_ROM_ADDRESS_ENABLE)
232 res->flags |= IORESOURCE_ROM_ENABLE;
Myron Stowef795d862014-10-30 11:54:43 -0600233 l64 = l & PCI_ROM_ADDRESS_MASK;
234 sz64 = sz & PCI_ROM_ADDRESS_MASK;
Matthias Kaehlcke76dc52682017-04-14 13:38:02 -0700235 mask64 = PCI_ROM_ADDRESS_MASK;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400236 }
237
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600238 if (res->flags & IORESOURCE_MEM_64) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400239 pci_read_config_dword(dev, pos + 4, &l);
240 pci_write_config_dword(dev, pos + 4, ~0);
241 pci_read_config_dword(dev, pos + 4, &sz);
242 pci_write_config_dword(dev, pos + 4, l);
243
244 l64 |= ((u64)l << 32);
245 sz64 |= ((u64)sz << 32);
Myron Stowef795d862014-10-30 11:54:43 -0600246 mask64 |= ((u64)~0 << 32);
247 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400248
Myron Stowef795d862014-10-30 11:54:43 -0600249 if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
250 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400251
Myron Stowef795d862014-10-30 11:54:43 -0600252 if (!sz64)
253 goto fail;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400254
Myron Stowef795d862014-10-30 11:54:43 -0600255 sz64 = pci_size(l64, sz64, mask64);
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600256 if (!sz64) {
257 dev_info(&dev->dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
258 pos);
Myron Stowef795d862014-10-30 11:54:43 -0600259 goto fail;
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600260 }
Myron Stowef795d862014-10-30 11:54:43 -0600261
262 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700263 if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
264 && sz64 > 0x100000000ULL) {
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600265 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
266 res->start = 0;
267 res->end = 0;
Myron Stowef795d862014-10-30 11:54:43 -0600268 dev_err(&dev->dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
269 pos, (unsigned long long)sz64);
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600270 goto out;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600271 }
272
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700273 if ((sizeof(pci_bus_addr_t) < 8) && l) {
Bjorn Helgaas31e9dd22014-04-29 18:37:47 -0600274 /* Above 32-bit boundary; try to reallocate */
Bjorn Helgaasc83bd902014-02-26 11:26:00 -0700275 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaas72dc5602014-04-29 18:42:49 -0600276 res->start = 0;
277 res->end = sz64;
Myron Stowef795d862014-10-30 11:54:43 -0600278 dev_info(&dev->dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
279 pos, (unsigned long long)l64);
Bjorn Helgaas72dc5602014-04-29 18:42:49 -0600280 goto out;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400281 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400282 }
283
Myron Stowef795d862014-10-30 11:54:43 -0600284 region.start = l64;
285 region.end = l64 + sz64;
286
Yinghai Lufc279852013-12-09 22:54:40 -0800287 pcibios_bus_to_resource(dev->bus, res, &region);
288 pcibios_resource_to_bus(dev->bus, &inverted_region, res);
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800289
290 /*
291 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
292 * the corresponding resource address (the physical address used by
293 * the CPU. Converting that resource address back to a bus address
294 * should yield the original BAR value:
295 *
296 * resource_to_bus(bus_to_resource(A)) == A
297 *
298 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
299 * be claimed by the device.
300 */
301 if (inverted_region.start != region.start) {
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800302 res->flags |= IORESOURCE_UNSET;
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800303 res->start = 0;
Bjorn Helgaas26370fc2014-04-14 15:26:50 -0600304 res->end = region.end - region.start;
Myron Stowef795d862014-10-30 11:54:43 -0600305 dev_info(&dev->dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
306 pos, (unsigned long long)region.start);
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800307 }
Kevin Hao96ddef22013-05-25 19:36:26 +0800308
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600309 goto out;
310
311
312fail:
313 res->flags = 0;
314out:
Bjorn Helgaas31e9dd22014-04-29 18:37:47 -0600315 if (res->flags)
Kevin Hao33963e302013-05-25 19:36:25 +0800316 dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res);
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600317
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600318 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
Yinghai Lu07eddf32006-11-29 13:53:10 -0800319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
322{
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400323 unsigned int pos, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Prarit Bhargavaad67b432016-05-11 12:27:16 -0400325 if (dev->non_compliant_bars)
326 return;
327
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400328 for (pos = 0; pos < howmany; pos++) {
329 struct resource *res = &dev->resource[pos];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400331 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (rom) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400335 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dev->rom_base_reg = rom;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400337 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
Dan Williams92b19ff2015-08-10 23:07:06 -0400338 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400339 __pci_read_base(dev, pci_bar_mem32, res, rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341}
342
Bill Pemberton15856ad2012-11-21 15:35:00 -0500343static void pci_read_bridge_io(struct pci_bus *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct pci_dev *dev = child->self;
346 u8 io_base_lo, io_limit_lo;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600347 unsigned long io_mask, io_granularity, base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700348 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600349 struct resource *res;
350
351 io_mask = PCI_IO_RANGE_MASK;
352 io_granularity = 0x1000;
353 if (dev->io_window_1k) {
354 /* Support 1K I/O space granularity */
355 io_mask = PCI_IO_1K_RANGE_MASK;
356 io_granularity = 0x400;
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 res = child->resource[0];
360 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
361 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600362 base = (io_base_lo & io_mask) << 8;
363 limit = (io_limit_lo & io_mask) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
366 u16 io_base_hi, io_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
369 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600370 base |= ((unsigned long) io_base_hi << 16);
371 limit |= ((unsigned long) io_limit_hi << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600374 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700376 region.start = base;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600377 region.end = limit + io_granularity - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800378 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600379 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700381}
382
Bill Pemberton15856ad2012-11-21 15:35:00 -0500383static void pci_read_bridge_mmio(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700384{
385 struct pci_dev *dev = child->self;
386 u16 mem_base_lo, mem_limit_lo;
387 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700388 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700389 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 res = child->resource[1];
392 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
393 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600394 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
395 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600396 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700398 region.start = base;
399 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800400 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600401 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700403}
404
Bill Pemberton15856ad2012-11-21 15:35:00 -0500405static void pci_read_bridge_mmio_pref(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700406{
407 struct pci_dev *dev = child->self;
408 u16 mem_base_lo, mem_limit_lo;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700409 u64 base64, limit64;
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700410 pci_bus_addr_t base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700411 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700412 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 res = child->resource[2];
415 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
416 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700417 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
418 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
421 u32 mem_base_hi, mem_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
424 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
425
426 /*
427 * Some bridges set the base > limit by default, and some
428 * (broken) BIOSes do not initialize them. If we find
429 * this, just assume they are not being used.
430 */
431 if (mem_base_hi <= mem_limit_hi) {
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700432 base64 |= (u64) mem_base_hi << 32;
433 limit64 |= (u64) mem_limit_hi << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 }
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700436
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700437 base = (pci_bus_addr_t) base64;
438 limit = (pci_bus_addr_t) limit64;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700439
440 if (base != base64) {
441 dev_err(&dev->dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
442 (unsigned long long) base64);
443 return;
444 }
445
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600446 if (base <= limit) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700447 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
448 IORESOURCE_MEM | IORESOURCE_PREFETCH;
449 if (res->flags & PCI_PREF_RANGE_TYPE_64)
450 res->flags |= IORESOURCE_MEM_64;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700451 region.start = base;
452 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800453 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600454 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456}
457
Bill Pemberton15856ad2012-11-21 15:35:00 -0500458void pci_read_bridge_bases(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700459{
460 struct pci_dev *dev = child->self;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700461 struct resource *res;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700462 int i;
463
464 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
465 return;
466
Yinghai Lub918c622012-05-17 18:51:11 -0700467 dev_info(&dev->dev, "PCI bridge to %pR%s\n",
468 &child->busn_res,
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700469 dev->transparent ? " (subtractive decode)" : "");
470
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700471 pci_bus_remove_resources(child);
472 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
473 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
474
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700475 pci_read_bridge_io(child);
476 pci_read_bridge_mmio(child);
477 pci_read_bridge_mmio_pref(child);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700478
479 if (dev->transparent) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700480 pci_bus_for_each_resource(child->parent, res, i) {
Bjorn Helgaasd739a092014-04-14 16:10:54 -0600481 if (res && res->flags) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700482 pci_bus_add_resource(child, res,
483 PCI_SUBTRACTIVE_DECODE);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700484 dev_printk(KERN_DEBUG, &dev->dev,
485 " bridge window %pR (subtractive decode)\n",
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700486 res);
487 }
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700488 }
489 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700490}
491
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100492static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct pci_bus *b;
495
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100496 b = kzalloc(sizeof(*b), GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600497 if (!b)
498 return NULL;
499
500 INIT_LIST_HEAD(&b->node);
501 INIT_LIST_HEAD(&b->children);
502 INIT_LIST_HEAD(&b->devices);
503 INIT_LIST_HEAD(&b->slots);
504 INIT_LIST_HEAD(&b->resources);
505 b->max_bus_speed = PCI_SPEED_UNKNOWN;
506 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100507#ifdef CONFIG_PCI_DOMAINS_GENERIC
508 if (parent)
509 b->domain_nr = parent->domain_nr;
510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return b;
512}
513
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500514static void devm_pci_release_host_bridge_dev(struct device *dev)
Jiang Liu70efde22013-06-07 16:16:51 -0600515{
516 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
517
518 if (bridge->release_fn)
519 bridge->release_fn(bridge);
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500520}
Jiang Liu70efde22013-06-07 16:16:51 -0600521
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500522static void pci_release_host_bridge_dev(struct device *dev)
523{
524 devm_pci_release_host_bridge_dev(dev);
525 pci_free_host_bridge(to_pci_host_bridge(dev));
Jiang Liu70efde22013-06-07 16:16:51 -0600526}
527
Thierry Redinga52d1442016-11-25 11:57:11 +0100528struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
Yinghai Lu7b543662012-04-02 18:31:53 -0700529{
530 struct pci_host_bridge *bridge;
531
Thierry Reding59094062016-11-25 11:57:10 +0100532 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600533 if (!bridge)
534 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -0700535
Bjorn Helgaas05013482013-06-05 14:22:11 -0600536 INIT_LIST_HEAD(&bridge->windows);
Lorenzo Pieralisia1c00502017-06-28 15:13:52 -0500537 bridge->dev.release = pci_release_host_bridge_dev;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100538
Yinghai Lu7b543662012-04-02 18:31:53 -0700539 return bridge;
540}
Thierry Redinga52d1442016-11-25 11:57:11 +0100541EXPORT_SYMBOL(pci_alloc_host_bridge);
Yinghai Lu7b543662012-04-02 18:31:53 -0700542
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500543struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
544 size_t priv)
545{
546 struct pci_host_bridge *bridge;
547
548 bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
549 if (!bridge)
550 return NULL;
551
552 INIT_LIST_HEAD(&bridge->windows);
553 bridge->dev.release = devm_pci_release_host_bridge_dev;
554
555 return bridge;
556}
557EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
558
Lorenzo Pieralisidff79b92017-06-28 15:13:52 -0500559void pci_free_host_bridge(struct pci_host_bridge *bridge)
560{
561 pci_free_resource_list(&bridge->windows);
562
563 kfree(bridge);
564}
565EXPORT_SYMBOL(pci_free_host_bridge);
566
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700567static const unsigned char pcix_bus_speed[] = {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500568 PCI_SPEED_UNKNOWN, /* 0 */
569 PCI_SPEED_66MHz_PCIX, /* 1 */
570 PCI_SPEED_100MHz_PCIX, /* 2 */
571 PCI_SPEED_133MHz_PCIX, /* 3 */
572 PCI_SPEED_UNKNOWN, /* 4 */
573 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
574 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
575 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
576 PCI_SPEED_UNKNOWN, /* 8 */
577 PCI_SPEED_66MHz_PCIX_266, /* 9 */
578 PCI_SPEED_100MHz_PCIX_266, /* A */
579 PCI_SPEED_133MHz_PCIX_266, /* B */
580 PCI_SPEED_UNKNOWN, /* C */
581 PCI_SPEED_66MHz_PCIX_533, /* D */
582 PCI_SPEED_100MHz_PCIX_533, /* E */
583 PCI_SPEED_133MHz_PCIX_533 /* F */
584};
585
Jacob Keller343e51a2013-07-31 06:53:16 +0000586const unsigned char pcie_link_speed[] = {
Matthew Wilcox3749c512009-12-13 08:11:32 -0500587 PCI_SPEED_UNKNOWN, /* 0 */
588 PCIE_SPEED_2_5GT, /* 1 */
589 PCIE_SPEED_5_0GT, /* 2 */
Matthew Wilcox9dfd97f2009-12-13 08:11:35 -0500590 PCIE_SPEED_8_0GT, /* 3 */
Matthew Wilcox3749c512009-12-13 08:11:32 -0500591 PCI_SPEED_UNKNOWN, /* 4 */
592 PCI_SPEED_UNKNOWN, /* 5 */
593 PCI_SPEED_UNKNOWN, /* 6 */
594 PCI_SPEED_UNKNOWN, /* 7 */
595 PCI_SPEED_UNKNOWN, /* 8 */
596 PCI_SPEED_UNKNOWN, /* 9 */
597 PCI_SPEED_UNKNOWN, /* A */
598 PCI_SPEED_UNKNOWN, /* B */
599 PCI_SPEED_UNKNOWN, /* C */
600 PCI_SPEED_UNKNOWN, /* D */
601 PCI_SPEED_UNKNOWN, /* E */
602 PCI_SPEED_UNKNOWN /* F */
603};
604
605void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
606{
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700607 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
Matthew Wilcox3749c512009-12-13 08:11:32 -0500608}
609EXPORT_SYMBOL_GPL(pcie_update_link_speed);
610
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500611static unsigned char agp_speeds[] = {
612 AGP_UNKNOWN,
613 AGP_1X,
614 AGP_2X,
615 AGP_4X,
616 AGP_8X
617};
618
619static enum pci_bus_speed agp_speed(int agp3, int agpstat)
620{
621 int index = 0;
622
623 if (agpstat & 4)
624 index = 3;
625 else if (agpstat & 2)
626 index = 2;
627 else if (agpstat & 1)
628 index = 1;
629 else
630 goto out;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700631
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500632 if (agp3) {
633 index += 2;
634 if (index == 5)
635 index = 0;
636 }
637
638 out:
639 return agp_speeds[index];
640}
641
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500642static void pci_set_bus_speed(struct pci_bus *bus)
643{
644 struct pci_dev *bridge = bus->self;
645 int pos;
646
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500647 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
648 if (!pos)
649 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
650 if (pos) {
651 u32 agpstat, agpcmd;
652
653 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
654 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
655
656 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
657 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
658 }
659
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500660 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
661 if (pos) {
662 u16 status;
663 enum pci_bus_speed max;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500664
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700665 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
666 &status);
667
668 if (status & PCI_X_SSTATUS_533MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500669 max = PCI_SPEED_133MHz_PCIX_533;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700670 } else if (status & PCI_X_SSTATUS_266MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500671 max = PCI_SPEED_133MHz_PCIX_266;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700672 } else if (status & PCI_X_SSTATUS_133MHZ) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400673 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500674 max = PCI_SPEED_133MHz_PCIX_ECC;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400675 else
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500676 max = PCI_SPEED_133MHz_PCIX;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500677 } else {
678 max = PCI_SPEED_66MHz_PCIX;
679 }
680
681 bus->max_bus_speed = max;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700682 bus->cur_bus_speed = pcix_bus_speed[
683 (status & PCI_X_SSTATUS_FREQ) >> 6];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500684
685 return;
686 }
687
Yijing Wangfdfe1512013-09-05 15:55:29 +0800688 if (pci_is_pcie(bridge)) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500689 u32 linkcap;
690 u16 linksta;
691
Jiang Liu59875ae2012-07-24 17:20:06 +0800692 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700693 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500694
Jiang Liu59875ae2012-07-24 17:20:06 +0800695 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500696 pcie_update_link_speed(bus, linksta);
697 }
698}
699
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100700static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
701{
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100702 struct irq_domain *d;
703
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100704 /*
705 * Any firmware interface that can resolve the msi_domain
706 * should be called from here.
707 */
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100708 d = pci_host_bridge_of_msi_domain(bus);
Suravee Suthikulpanit471036b2015-12-10 08:55:27 -0800709 if (!d)
710 d = pci_host_bridge_acpi_msi_domain(bus);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100711
Jake Oshins788858e2016-02-16 21:56:22 +0000712#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
713 /*
714 * If no IRQ domain was found via the OF tree, try looking it up
715 * directly through the fwnode_handle.
716 */
717 if (!d) {
718 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
719
720 if (fwnode)
721 d = irq_find_matching_fwnode(fwnode,
722 DOMAIN_BUS_PCI_MSI);
723 }
724#endif
725
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100726 return d;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100727}
728
729static void pci_set_bus_msi_domain(struct pci_bus *bus)
730{
731 struct irq_domain *d;
Alex Williamson38ea72b2015-09-18 15:08:54 -0600732 struct pci_bus *b;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100733
734 /*
Alex Williamson38ea72b2015-09-18 15:08:54 -0600735 * The bus can be a root bus, a subordinate bus, or a virtual bus
736 * created by an SR-IOV device. Walk up to the first bridge device
737 * found or derive the domain from the host bridge.
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100738 */
Alex Williamson38ea72b2015-09-18 15:08:54 -0600739 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
740 if (b->self)
741 d = dev_get_msi_domain(&b->self->dev);
742 }
743
744 if (!d)
745 d = pci_host_bridge_msi_domain(b);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100746
747 dev_set_msi_domain(&bus->dev, d);
748}
749
Lorenzo Pieralisicea9bc02017-06-28 15:13:55 -0500750static int pci_register_host_bridge(struct pci_host_bridge *bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100751{
752 struct device *parent = bridge->dev.parent;
753 struct resource_entry *window, *n;
754 struct pci_bus *bus, *b;
755 resource_size_t offset;
756 LIST_HEAD(resources);
757 struct resource *res;
758 char addr[64], *fmt;
759 const char *name;
760 int err;
761
762 bus = pci_alloc_bus(NULL);
763 if (!bus)
764 return -ENOMEM;
765
766 bridge->bus = bus;
767
768 /* temporarily move resources off the list */
769 list_splice_init(&bridge->windows, &resources);
770 bus->sysdata = bridge->sysdata;
771 bus->msi = bridge->msi;
772 bus->ops = bridge->ops;
773 bus->number = bus->busn_res.start = bridge->busnr;
774#ifdef CONFIG_PCI_DOMAINS_GENERIC
775 bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
776#endif
777
778 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
779 if (b) {
780 /* If we already got to this bus through a different bridge, ignore it */
781 dev_dbg(&b->dev, "bus already known\n");
782 err = -EEXIST;
783 goto free;
784 }
785
786 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
787 bridge->busnr);
788
789 err = pcibios_root_bridge_prepare(bridge);
790 if (err)
791 goto free;
792
793 err = device_register(&bridge->dev);
794 if (err)
795 put_device(&bridge->dev);
796
797 bus->bridge = get_device(&bridge->dev);
798 device_enable_async_suspend(bus->bridge);
799 pci_set_bus_of_node(bus);
800 pci_set_bus_msi_domain(bus);
801
802 if (!parent)
803 set_dev_node(bus->bridge, pcibus_to_node(bus));
804
805 bus->dev.class = &pcibus_class;
806 bus->dev.parent = bus->bridge;
807
808 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
809 name = dev_name(&bus->dev);
810
811 err = device_register(&bus->dev);
812 if (err)
813 goto unregister;
814
815 pcibios_add_bus(bus);
816
817 /* Create legacy_io and legacy_mem files for this bus */
818 pci_create_legacy_files(bus);
819
820 if (parent)
821 dev_info(parent, "PCI host bridge to bus %s\n", name);
822 else
823 pr_info("PCI host bridge to bus %s\n", name);
824
825 /* Add initial resources to the bus */
826 resource_list_for_each_entry_safe(window, n, &resources) {
827 list_move_tail(&window->node, &bridge->windows);
828 offset = window->offset;
829 res = window->res;
830
831 if (res->flags & IORESOURCE_BUS)
832 pci_bus_insert_busn_res(bus, bus->number, res->end);
833 else
834 pci_bus_add_resource(bus, res, 0);
835
836 if (offset) {
837 if (resource_type(res) == IORESOURCE_IO)
838 fmt = " (bus address [%#06llx-%#06llx])";
839 else
840 fmt = " (bus address [%#010llx-%#010llx])";
841
842 snprintf(addr, sizeof(addr), fmt,
843 (unsigned long long)(res->start - offset),
844 (unsigned long long)(res->end - offset));
845 } else
846 addr[0] = '\0';
847
848 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
849 }
850
851 down_write(&pci_bus_sem);
852 list_add_tail(&bus->node, &pci_root_buses);
853 up_write(&pci_bus_sem);
854
855 return 0;
856
857unregister:
858 put_device(&bridge->dev);
859 device_unregister(&bridge->dev);
860
861free:
862 kfree(bus);
863 return err;
864}
865
Adrian Bunkcbd4e052008-04-18 13:53:55 -0700866static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
867 struct pci_dev *bridge, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct pci_bus *child;
870 int i;
Yinghai Lu4f535092013-01-21 13:20:52 -0800871 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /*
874 * Allocate a new bus, and inherit stuff from the parent..
875 */
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100876 child = pci_alloc_bus(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!child)
878 return NULL;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 child->parent = parent;
881 child->ops = parent->ops;
Thierry Reding0cbdcfc2013-08-09 22:27:08 +0200882 child->msi = parent->msi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 child->sysdata = parent->sysdata;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200884 child->bus_flags = parent->bus_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400886 /* initialize some portions of the bus device, but don't register it
Yinghai Lu4f535092013-01-21 13:20:52 -0800887 * now as the parent is not properly set up yet.
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400888 */
889 child->dev.class = &pcibus_class;
Kay Sievers1a927132008-10-30 02:17:49 +0100890 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /*
893 * Set up the primary, secondary and subordinate
894 * bus numbers.
895 */
Yinghai Lub918c622012-05-17 18:51:11 -0700896 child->number = child->busn_res.start = busnr;
897 child->primary = parent->busn_res.start;
898 child->busn_res.end = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Yinghai Lu4f535092013-01-21 13:20:52 -0800900 if (!bridge) {
901 child->dev.parent = parent->bridge;
902 goto add_dev;
903 }
Yu Zhao3789fa82008-11-22 02:41:07 +0800904
905 child->self = bridge;
906 child->bridge = get_device(&bridge->dev);
Yinghai Lu4f535092013-01-21 13:20:52 -0800907 child->dev.parent = child->bridge;
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000908 pci_set_bus_of_node(child);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500909 pci_set_bus_speed(child);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* Set up default resource pointers and names.. */
Yu Zhaofde09c62008-11-22 02:39:32 +0800912 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
914 child->resource[i]->name = child->name;
915 }
916 bridge->subordinate = child;
917
Yinghai Lu4f535092013-01-21 13:20:52 -0800918add_dev:
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100919 pci_set_bus_msi_domain(child);
Yinghai Lu4f535092013-01-21 13:20:52 -0800920 ret = device_register(&child->dev);
921 WARN_ON(ret < 0);
922
Jiang Liu10a95742013-04-12 05:44:20 +0000923 pcibios_add_bus(child);
924
Thierry Reding057bd2e2016-02-09 15:30:47 +0100925 if (child->ops->add_bus) {
926 ret = child->ops->add_bus(child);
927 if (WARN_ON(ret < 0))
928 dev_err(&child->dev, "failed to add bus: %d\n", ret);
929 }
930
Yinghai Lu4f535092013-01-21 13:20:52 -0800931 /* Create legacy_io and legacy_mem files for this bus */
932 pci_create_legacy_files(child);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return child;
935}
936
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400937struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
938 int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct pci_bus *child;
941
942 child = pci_alloc_child_bus(parent, dev, busnr);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700943 if (child) {
Zhang Yanmind71374d2006-06-02 12:35:43 +0800944 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 list_add_tail(&child->node, &parent->children);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800946 up_write(&pci_bus_sem);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return child;
949}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600950EXPORT_SYMBOL(pci_add_new_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Rajat Jainf3dbd802014-09-02 16:26:00 -0700952static void pci_enable_crs(struct pci_dev *pdev)
953{
954 u16 root_cap = 0;
955
956 /* Enable CRS Software Visibility if supported */
957 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
958 if (root_cap & PCI_EXP_RTCAP_CRSVIS)
959 pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
960 PCI_EXP_RTCTL_CRSSVE);
961}
962
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300963static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
964 unsigned int available_buses);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/*
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300967 * pci_scan_bridge_extend() - Scan buses behind a bridge
968 * @bus: Parent bus the bridge is on
969 * @dev: Bridge itself
970 * @max: Starting subordinate number of buses behind this bridge
971 * @available_buses: Total number of buses available for this bridge and
972 * the devices below. After the minimal bus space has
973 * been allocated the remaining buses will be
974 * distributed equally between hotplug-capable bridges.
975 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
976 * that need to be reconfigured.
977 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * If it's a bridge, configure it and scan the bus behind it.
979 * For CardBus bridges, we don't scan behind as the devices will
980 * be handled by the bridge driver itself.
981 *
982 * We need to process bridges in two passes -- first we scan those
983 * already configured by the BIOS and after we are done with all of
984 * them, we proceed to assigning numbers to the remaining buses in
985 * order to avoid overlaps between old and new bus numbers.
986 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300987static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
988 int max, unsigned int available_buses,
989 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct pci_bus *child;
992 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
Dominik Brodowski49887942005-12-08 16:53:12 +0100993 u32 buses, i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 u16 bctl;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600995 u8 primary, secondary, subordinate;
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100996 int broken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Mika Westerbergd963f652016-06-02 11:17:13 +0300998 /*
999 * Make sure the bridge is powered on to be able to access config
1000 * space of devices below it.
1001 */
1002 pm_runtime_get_sync(&dev->dev);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001005 primary = buses & 0xFF;
1006 secondary = (buses >> 8) & 0xFF;
1007 subordinate = (buses >> 16) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001009 dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
1010 secondary, subordinate, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001012 if (!primary && (primary != bus->number) && secondary && subordinate) {
1013 dev_warn(&dev->dev, "Primary bus is hard wired to 0\n");
1014 primary = bus->number;
1015 }
1016
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001017 /* Check if setup is sensible at all */
1018 if (!pass &&
Yinghai Lu1965f662012-09-10 17:19:33 -07001019 (primary != bus->number || secondary <= bus->number ||
Bjorn Helgaas12d87062014-09-19 11:08:40 -06001020 secondary > subordinate)) {
Yinghai Lu1965f662012-09-10 17:19:33 -07001021 dev_info(&dev->dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
1022 secondary, subordinate);
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001023 broken = 1;
1024 }
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* Disable MasterAbortMode during probing to avoid reporting
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001027 of bus errors (in some architectures) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1029 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1030 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1031
Rajat Jainf3dbd802014-09-02 16:26:00 -07001032 pci_enable_crs(dev);
1033
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001034 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1035 !is_cardbus && !broken) {
1036 unsigned int cmax;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * Bus already configured by firmware, process it in the first
1039 * pass and just note the configuration.
1040 */
1041 if (pass)
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /*
Andreas Noever2ed85822014-01-23 21:59:22 +01001045 * The bus might already exist for two reasons: Either we are
1046 * rescanning the bus or the bus is reachable through more than
1047 * one bridge. The second case can happen with the i450NX
1048 * chipset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001050 child = pci_find_bus(pci_domain_nr(bus), secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001051 if (!child) {
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001052 child = pci_add_new_bus(bus, dev, secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001053 if (!child)
1054 goto out;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001055 child->primary = primary;
Yinghai Lubc76b732012-05-17 18:51:13 -07001056 pci_bus_insert_busn_res(child, secondary, subordinate);
Alex Chiang74710de2009-03-20 14:56:10 -06001057 child->bridge_ctl = bctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 cmax = pci_scan_child_bus(child);
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001061 if (cmax > subordinate)
1062 dev_warn(&dev->dev, "bridge has subordinate %02x but max busn %02x\n",
1063 subordinate, cmax);
1064 /* subordinate should equal child->busn_res.end */
1065 if (subordinate > max)
1066 max = subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else {
1068 /*
1069 * We need to assign a number to this bus which we always
1070 * do in the second pass.
1071 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001072 if (!pass) {
Andreas Noever619c8c32014-01-23 21:59:23 +01001073 if (pcibios_assign_all_busses() || broken || is_cardbus)
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001074 /* Temporarily disable forwarding of the
1075 configuration cycles on all bridges in
1076 this bus segment to avoid possible
1077 conflicts in the second pass between two
1078 bridges programmed with overlapping
1079 bus ranges. */
1080 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1081 buses & ~0xffffff);
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001082 goto out;
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Clear errors */
1086 pci_write_config_word(dev, PCI_STATUS, 0xffff);
1087
Bjorn Helgaas7a0b33d2014-09-19 10:56:06 -06001088 /* Prevent assigning a bus number that already exists.
1089 * This can happen when a bridge is hot-plugged, so in
1090 * this case we only re-scan this bus. */
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001091 child = pci_find_bus(pci_domain_nr(bus), max+1);
1092 if (!child) {
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001093 child = pci_add_new_bus(bus, dev, max+1);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001094 if (!child)
1095 goto out;
Mika Westerberga20c7f32017-10-13 21:35:43 +03001096 pci_bus_insert_busn_res(child, max+1,
1097 bus->busn_res.end);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001098 }
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001099 max++;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001100 if (available_buses)
1101 available_buses--;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 buses = (buses & 0xff000000)
1104 | ((unsigned int)(child->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -07001105 | ((unsigned int)(child->busn_res.start) << 8)
1106 | ((unsigned int)(child->busn_res.end) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /*
1109 * yenta.c forces a secondary latency timer of 176.
1110 * Copy that behaviour here.
1111 */
1112 if (is_cardbus) {
1113 buses &= ~0xff000000;
1114 buses |= CARDBUS_LATENCY_TIMER << 24;
1115 }
Jesper Juhl7c867c82011-01-24 21:14:33 +01001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /*
1118 * We need to blast all three values with a single write.
1119 */
1120 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1121
1122 if (!is_cardbus) {
Gary Hade11949252007-10-08 16:24:16 -07001123 child->bridge_ctl = bctl;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001124 max = pci_scan_child_bus_extend(child, available_buses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 } else {
1126 /*
1127 * For CardBus bridges, we leave 4 bus numbers
1128 * as cards with a PCI-to-PCI bridge can be
1129 * inserted later.
1130 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001131 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001132 struct pci_bus *parent = bus;
Rajesh Shahcc574502005-04-28 00:25:47 -07001133 if (pci_find_bus(pci_domain_nr(bus),
1134 max+i+1))
1135 break;
Dominik Brodowski49887942005-12-08 16:53:12 +01001136 while (parent->parent) {
1137 if ((!pcibios_assign_all_busses()) &&
Yinghai Lub918c622012-05-17 18:51:11 -07001138 (parent->busn_res.end > max) &&
1139 (parent->busn_res.end <= max+i)) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001140 j = 1;
1141 }
1142 parent = parent->parent;
1143 }
1144 if (j) {
1145 /*
1146 * Often, there are two cardbus bridges
1147 * -- try to leave one valid bus number
1148 * for each one.
1149 */
1150 i /= 2;
1151 break;
1152 }
1153 }
Rajesh Shahcc574502005-04-28 00:25:47 -07001154 max += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 /*
1157 * Set the subordinate bus number to its real value.
1158 */
Yinghai Lubc76b732012-05-17 18:51:13 -07001159 pci_bus_update_busn_res_end(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1161 }
1162
Gary Hadecb3576f2008-02-08 14:00:52 -08001163 sprintf(child->name,
1164 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1165 pci_domain_nr(bus), child->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Bernhard Kaindld55bef512007-07-30 20:35:13 +02001167 /* Has only triggered on CardBus, fixup is in yenta_socket */
Dominik Brodowski49887942005-12-08 16:53:12 +01001168 while (bus->parent) {
Yinghai Lub918c622012-05-17 18:51:11 -07001169 if ((child->busn_res.end > bus->busn_res.end) ||
1170 (child->number > bus->busn_res.end) ||
Dominik Brodowski49887942005-12-08 16:53:12 +01001171 (child->number < bus->number) ||
Yinghai Lub918c622012-05-17 18:51:11 -07001172 (child->busn_res.end < bus->number)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001173 dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
Yinghai Lub918c622012-05-17 18:51:11 -07001174 &child->busn_res,
1175 (bus->number > child->busn_res.end &&
1176 bus->busn_res.end < child->number) ?
Joe Perchesa6f29a92007-11-19 17:48:29 -08001177 "wholly" : "partially",
1178 bus->self->transparent ? " transparent" : "",
Bjorn Helgaas865df572009-11-04 10:32:57 -07001179 dev_name(&bus->dev),
Yinghai Lub918c622012-05-17 18:51:11 -07001180 &bus->busn_res);
Dominik Brodowski49887942005-12-08 16:53:12 +01001181 }
1182 bus = bus->parent;
1183 }
1184
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001185out:
1186 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1187
Mika Westerbergd963f652016-06-02 11:17:13 +03001188 pm_runtime_put(&dev->dev);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return max;
1191}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001192
1193/*
1194 * pci_scan_bridge() - Scan buses behind a bridge
1195 * @bus: Parent bus the bridge is on
1196 * @dev: Bridge itself
1197 * @max: Starting subordinate number of buses behind this bridge
1198 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1199 * that need to be reconfigured.
1200 *
1201 * If it's a bridge, configure it and scan the bus behind it.
1202 * For CardBus bridges, we don't scan behind as the devices will
1203 * be handled by the bridge driver itself.
1204 *
1205 * We need to process bridges in two passes -- first we scan those
1206 * already configured by the BIOS and after we are done with all of
1207 * them, we proceed to assigning numbers to the remaining buses in
1208 * order to avoid overlaps between old and new bus numbers.
1209 */
1210int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1211{
1212 return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1213}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001214EXPORT_SYMBOL(pci_scan_bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216/*
1217 * Read interrupt line and base address registers.
1218 * The architecture-dependent code can tweak these, of course.
1219 */
1220static void pci_read_irq(struct pci_dev *dev)
1221{
1222 unsigned char irq;
1223
1224 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
Kristen Accardiffeff782005-11-02 16:24:32 -08001225 dev->pin = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (irq)
1227 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1228 dev->irq = irq;
1229}
1230
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001231void set_pcie_port_type(struct pci_dev *pdev)
Yu Zhao480b93b2009-03-20 11:25:14 +08001232{
1233 int pos;
1234 u16 reg16;
Yijing Wangd0751b92015-05-21 15:05:02 +08001235 int type;
1236 struct pci_dev *parent;
Yu Zhao480b93b2009-03-20 11:25:14 +08001237
1238 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1239 if (!pos)
1240 return;
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001241
Kenji Kaneshige0efea002009-11-05 12:05:11 +09001242 pdev->pcie_cap = pos;
Yu Zhao480b93b2009-03-20 11:25:14 +08001243 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
Yijing Wang786e2282012-07-24 17:20:02 +08001244 pdev->pcie_flags_reg = reg16;
Jon Masonb03e7492011-07-20 15:20:54 -05001245 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
1246 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
Yijing Wangd0751b92015-05-21 15:05:02 +08001247
1248 /*
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001249 * A Root Port or a PCI-to-PCIe bridge is always the upstream end
1250 * of a Link. No PCIe component has two Links. Two Links are
1251 * connected by a Switch that has a Port on each Link and internal
1252 * logic to connect the two Ports.
Yijing Wangd0751b92015-05-21 15:05:02 +08001253 */
1254 type = pci_pcie_type(pdev);
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001255 if (type == PCI_EXP_TYPE_ROOT_PORT ||
1256 type == PCI_EXP_TYPE_PCIE_BRIDGE)
Yijing Wangd0751b92015-05-21 15:05:02 +08001257 pdev->has_secondary_link = 1;
1258 else if (type == PCI_EXP_TYPE_UPSTREAM ||
1259 type == PCI_EXP_TYPE_DOWNSTREAM) {
1260 parent = pci_upstream_bridge(pdev);
Yijing Wangb35b1df2015-08-17 18:47:58 +08001261
1262 /*
1263 * Usually there's an upstream device (Root Port or Switch
1264 * Downstream Port), but we can't assume one exists.
1265 */
1266 if (parent && !parent->has_secondary_link)
Yijing Wangd0751b92015-05-21 15:05:02 +08001267 pdev->has_secondary_link = 1;
1268 }
Yu Zhao480b93b2009-03-20 11:25:14 +08001269}
1270
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001271void set_pcie_hotplug_bridge(struct pci_dev *pdev)
Eric W. Biederman28760482009-09-09 14:09:24 -07001272{
Eric W. Biederman28760482009-09-09 14:09:24 -07001273 u32 reg32;
1274
Jiang Liu59875ae2012-07-24 17:20:06 +08001275 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
Eric W. Biederman28760482009-09-09 14:09:24 -07001276 if (reg32 & PCI_EXP_SLTCAP_HPC)
1277 pdev->is_hotplug_bridge = 1;
1278}
1279
Lukas Wunner8531e282017-03-10 21:23:45 +01001280static void set_pcie_thunderbolt(struct pci_dev *dev)
1281{
1282 int vsec = 0;
1283 u32 header;
1284
1285 while ((vsec = pci_find_next_ext_capability(dev, vsec,
1286 PCI_EXT_CAP_ID_VNDR))) {
1287 pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
1288
1289 /* Is the device part of a Thunderbolt controller? */
1290 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
1291 PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
1292 dev->is_thunderbolt = 1;
1293 return;
1294 }
1295 }
1296}
1297
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001298/**
Alex Williamson78916b02014-05-05 14:20:51 -06001299 * pci_ext_cfg_is_aliased - is ext config space just an alias of std config?
1300 * @dev: PCI device
1301 *
1302 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1303 * when forwarding a type1 configuration request the bridge must check that
1304 * the extended register address field is zero. The bridge is not permitted
1305 * to forward the transactions and must handle it as an Unsupported Request.
1306 * Some bridges do not follow this rule and simply drop the extended register
1307 * bits, resulting in the standard config space being aliased, every 256
1308 * bytes across the entire configuration space. Test for this condition by
1309 * comparing the first dword of each potential alias to the vendor/device ID.
1310 * Known offenders:
1311 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1312 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1313 */
1314static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1315{
1316#ifdef CONFIG_PCI_QUIRKS
1317 int pos;
1318 u32 header, tmp;
1319
1320 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1321
1322 for (pos = PCI_CFG_SPACE_SIZE;
1323 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1324 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1325 || header != tmp)
1326 return false;
1327 }
1328
1329 return true;
1330#else
1331 return false;
1332#endif
1333}
1334
1335/**
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001336 * pci_cfg_space_size - get the configuration space size of the PCI device.
1337 * @dev: PCI device
1338 *
1339 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1340 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1341 * access it. Maybe we don't have a way to generate extended config space
1342 * accesses, or the device is behind a reverse Express bridge. So we try
1343 * reading the dword at 0x100 which must either be 0 or a valid extended
1344 * capability header.
1345 */
1346static int pci_cfg_space_size_ext(struct pci_dev *dev)
1347{
1348 u32 status;
1349 int pos = PCI_CFG_SPACE_SIZE;
1350
1351 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001352 return PCI_CFG_SPACE_SIZE;
Alex Williamson78916b02014-05-05 14:20:51 -06001353 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001354 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001355
1356 return PCI_CFG_SPACE_EXP_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001357}
1358
1359int pci_cfg_space_size(struct pci_dev *dev)
1360{
1361 int pos;
1362 u32 status;
1363 u16 class;
1364
1365 class = dev->class >> 8;
1366 if (class == PCI_CLASS_BRIDGE_HOST)
1367 return pci_cfg_space_size_ext(dev);
1368
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001369 if (pci_is_pcie(dev))
1370 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001371
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001372 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1373 if (!pos)
1374 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001375
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001376 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1377 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1378 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001379
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001380 return PCI_CFG_SPACE_SIZE;
1381}
1382
Bartlomiej Zolnierkiewicz01abc2a2007-04-23 23:19:36 +02001383#define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
Randy Dunlap76e6a1d2006-12-29 16:47:29 -08001384
Guilherme G. Piccolie80e7edc2015-10-21 12:17:35 -02001385static void pci_msi_setup_pci_dev(struct pci_dev *dev)
Michael S. Tsirkin18516172015-05-07 09:52:21 -05001386{
1387 /*
1388 * Disable the MSI hardware to avoid screaming interrupts
1389 * during boot. This is the power on reset default so
1390 * usually this should be a noop.
1391 */
1392 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1393 if (dev->msi_cap)
1394 pci_msi_set_enable(dev, 0);
1395
1396 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1397 if (dev->msix_cap)
1398 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401/**
Piotr Gregor99b3c582017-05-26 22:02:25 +01001402 * pci_intx_mask_broken - test PCI_COMMAND_INTX_DISABLE writability
1403 * @dev: PCI device
1404 *
1405 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
1406 * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1407 */
1408static int pci_intx_mask_broken(struct pci_dev *dev)
1409{
1410 u16 orig, toggle, new;
1411
1412 pci_read_config_word(dev, PCI_COMMAND, &orig);
1413 toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1414 pci_write_config_word(dev, PCI_COMMAND, toggle);
1415 pci_read_config_word(dev, PCI_COMMAND, &new);
1416
1417 pci_write_config_word(dev, PCI_COMMAND, orig);
1418
1419 /*
1420 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1421 * r2.3, so strictly speaking, a device is not *broken* if it's not
1422 * writable. But we'll live with the misnomer for now.
1423 */
1424 if (new != toggle)
1425 return 1;
1426 return 0;
1427}
1428
1429/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 * pci_setup_device - fill in class and map information of a device
1431 * @dev: the device structure to fill
1432 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001433 * Initialize the device structure with information about the device's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 * vendor,class,memory and IO-space addresses,IRQ lines etc.
1435 * Called at initialisation of the PCI subsystem and by CardBus services.
Yu Zhao480b93b2009-03-20 11:25:14 +08001436 * Returns 0 on success and negative if unknown type of device (not normal,
1437 * bridge or CardBus).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001439int pci_setup_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 u32 class;
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001442 u16 cmd;
Yu Zhao480b93b2009-03-20 11:25:14 +08001443 u8 hdr_type;
Gabe Blackbc577d22009-10-06 10:45:19 -05001444 int pos = 0;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001445 struct pci_bus_region region;
1446 struct resource *res;
Yu Zhao480b93b2009-03-20 11:25:14 +08001447
1448 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
1449 return -EIO;
1450
1451 dev->sysdata = dev->bus->sysdata;
1452 dev->dev.parent = dev->bus->bridge;
1453 dev->dev.bus = &pci_bus_type;
1454 dev->hdr_type = hdr_type & 0x7f;
1455 dev->multifunction = !!(hdr_type & 0x80);
Yu Zhao480b93b2009-03-20 11:25:14 +08001456 dev->error_state = pci_channel_io_normal;
1457 set_pcie_port_type(dev);
1458
Yijing Wang017ffe62015-07-17 17:16:32 +08001459 pci_dev_assign_slot(dev);
Yu Zhao480b93b2009-03-20 11:25:14 +08001460 /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1461 set this higher, assuming the system even supports it. */
1462 dev->dma_mask = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Greg Kroah-Hartmaneebfcfb2008-07-02 13:24:49 -07001464 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1465 dev->bus->number, PCI_SLOT(dev->devfn),
1466 PCI_FUNC(dev->devfn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
Auke Kokb8a3a522007-06-08 15:46:30 -07001469 dev->revision = class & 0xff;
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001470 dev->class = class >> 8; /* upper 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001472 dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n",
1473 dev->vendor, dev->device, dev->hdr_type, dev->class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Yu Zhao853346e2009-03-21 22:05:11 +08001475 /* need to have dev->class ready */
1476 dev->cfg_size = pci_cfg_space_size(dev);
1477
Lukas Wunner8531e282017-03-10 21:23:45 +01001478 /* need to have dev->cfg_size ready */
1479 set_pcie_thunderbolt(dev);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* "Unknown power state" */
Daniel Ritz3fe9d192005-08-17 15:32:19 -07001482 dev->current_state = PCI_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /* Early fixups, before probing the BARs */
1485 pci_fixup_device(pci_fixup_early, dev);
Yu Zhaof79b1b12009-05-28 00:25:05 +08001486 /* device class may be changed after fixup */
1487 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001489 if (dev->non_compliant_bars) {
1490 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1491 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
1492 dev_info(&dev->dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
1493 cmd &= ~PCI_COMMAND_IO;
1494 cmd &= ~PCI_COMMAND_MEMORY;
1495 pci_write_config_word(dev, PCI_COMMAND, cmd);
1496 }
1497 }
1498
Piotr Gregor99b3c582017-05-26 22:02:25 +01001499 dev->broken_intx_masking = pci_intx_mask_broken(dev);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 switch (dev->hdr_type) { /* header type */
1502 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1503 if (class == PCI_CLASS_BRIDGE_PCI)
1504 goto bad;
1505 pci_read_irq(dev);
1506 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
1507 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1508 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
Alan Cox368c73d2006-10-04 00:41:26 +01001509
1510 /*
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001511 * Do the ugly legacy mode stuff here rather than broken chip
1512 * quirk code. Legacy mode ATA controllers have fixed
1513 * addresses. These are not always echoed in BAR0-3, and
1514 * BAR0-3 in a few cases contain junk!
Alan Cox368c73d2006-10-04 00:41:26 +01001515 */
1516 if (class == PCI_CLASS_STORAGE_IDE) {
1517 u8 progif;
1518 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1519 if ((progif & 1) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001520 region.start = 0x1F0;
1521 region.end = 0x1F7;
1522 res = &dev->resource[0];
1523 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001524 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001525 dev_info(&dev->dev, "legacy IDE quirk: reg 0x10: %pR\n",
1526 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001527 region.start = 0x3F6;
1528 region.end = 0x3F6;
1529 res = &dev->resource[1];
1530 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001531 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001532 dev_info(&dev->dev, "legacy IDE quirk: reg 0x14: %pR\n",
1533 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001534 }
1535 if ((progif & 4) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001536 region.start = 0x170;
1537 region.end = 0x177;
1538 res = &dev->resource[2];
1539 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001540 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001541 dev_info(&dev->dev, "legacy IDE quirk: reg 0x18: %pR\n",
1542 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001543 region.start = 0x376;
1544 region.end = 0x376;
1545 res = &dev->resource[3];
1546 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001547 pcibios_bus_to_resource(dev->bus, res, &region);
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001548 dev_info(&dev->dev, "legacy IDE quirk: reg 0x1c: %pR\n",
1549 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001550 }
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break;
1553
1554 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1555 if (class != PCI_CLASS_BRIDGE_PCI)
1556 goto bad;
1557 /* The PCI-to-PCI bridge spec requires that subtractive
1558 decoding (i.e. transparent) bridge must have programming
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001559 interface code of 0x01. */
Kristen Accardi3efd2732005-11-02 16:55:49 -08001560 pci_read_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 dev->transparent = ((dev->class & 0xff) == 1);
1562 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
Eric W. Biederman28760482009-09-09 14:09:24 -07001563 set_pcie_hotplug_bridge(dev);
Gabe Blackbc577d22009-10-06 10:45:19 -05001564 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1565 if (pos) {
1566 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1567 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 break;
1570
1571 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1572 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1573 goto bad;
1574 pci_read_irq(dev);
1575 pci_read_bases(dev, 1, 0);
1576 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1577 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1578 break;
1579
1580 default: /* unknown header */
Ryan Desfosses227f0642014-04-18 20:13:50 -04001581 dev_err(&dev->dev, "unknown header type %02x, ignoring device\n",
1582 dev->hdr_type);
Yu Zhao480b93b2009-03-20 11:25:14 +08001583 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 bad:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001586 dev_err(&dev->dev, "ignoring class %#08x (doesn't match header type %02x)\n",
1587 dev->class, dev->hdr_type);
Bjorn Helgaas2b4aed12015-06-19 16:20:58 -05001588 dev->class = PCI_CLASS_NOT_DEFINED << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590
1591 /* We found a fine healthy device, go go go... */
1592 return 0;
1593}
1594
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001595static void pci_configure_mps(struct pci_dev *dev)
1596{
1597 struct pci_dev *bridge = pci_upstream_bridge(dev);
Keith Busch27d868b2015-08-24 08:48:16 -05001598 int mps, p_mps, rc;
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001599
1600 if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
1601 return;
1602
1603 mps = pcie_get_mps(dev);
1604 p_mps = pcie_get_mps(bridge);
1605
1606 if (mps == p_mps)
1607 return;
1608
1609 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
1610 dev_warn(&dev->dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1611 mps, pci_name(bridge), p_mps);
1612 return;
1613 }
Keith Busch27d868b2015-08-24 08:48:16 -05001614
1615 /*
1616 * Fancier MPS configuration is done later by
1617 * pcie_bus_configure_settings()
1618 */
1619 if (pcie_bus_config != PCIE_BUS_DEFAULT)
1620 return;
1621
1622 rc = pcie_set_mps(dev, p_mps);
1623 if (rc) {
1624 dev_warn(&dev->dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1625 p_mps);
1626 return;
1627 }
1628
1629 dev_info(&dev->dev, "Max Payload Size set to %d (was %d, max %d)\n",
1630 p_mps, mps, 128 << dev->pcie_mpss);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001631}
1632
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001633static struct hpp_type0 pci_default_type0 = {
1634 .revision = 1,
1635 .cache_line_size = 8,
1636 .latency_timer = 0x40,
1637 .enable_serr = 0,
1638 .enable_perr = 0,
1639};
1640
1641static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1642{
1643 u16 pci_cmd, pci_bctl;
1644
Bjorn Helgaasc6285fc2014-08-29 18:10:19 -06001645 if (!hpp)
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001646 hpp = &pci_default_type0;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001647
1648 if (hpp->revision > 1) {
1649 dev_warn(&dev->dev,
1650 "PCI settings rev %d not supported; using defaults\n",
1651 hpp->revision);
1652 hpp = &pci_default_type0;
1653 }
1654
1655 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1656 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1657 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1658 if (hpp->enable_serr)
1659 pci_cmd |= PCI_COMMAND_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001660 if (hpp->enable_perr)
1661 pci_cmd |= PCI_COMMAND_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001662 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1663
1664 /* Program bridge control value */
1665 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1666 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1667 hpp->latency_timer);
1668 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1669 if (hpp->enable_serr)
1670 pci_bctl |= PCI_BRIDGE_CTL_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001671 if (hpp->enable_perr)
1672 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001673 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1674 }
1675}
1676
1677static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1678{
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001679 int pos;
1680
1681 if (!hpp)
1682 return;
1683
1684 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1685 if (!pos)
1686 return;
1687
1688 dev_warn(&dev->dev, "PCI-X settings not supported\n");
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001689}
1690
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001691static bool pcie_root_rcb_set(struct pci_dev *dev)
1692{
1693 struct pci_dev *rp = pcie_find_root_port(dev);
1694 u16 lnkctl;
1695
1696 if (!rp)
1697 return false;
1698
1699 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
1700 if (lnkctl & PCI_EXP_LNKCTL_RCB)
1701 return true;
1702
1703 return false;
1704}
1705
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001706static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1707{
1708 int pos;
1709 u32 reg32;
1710
1711 if (!hpp)
1712 return;
1713
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001714 if (!pci_is_pcie(dev))
1715 return;
1716
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001717 if (hpp->revision > 1) {
1718 dev_warn(&dev->dev, "PCIe settings rev %d not supported\n",
1719 hpp->revision);
1720 return;
1721 }
1722
Bjorn Helgaas302328c2014-09-03 13:26:29 -06001723 /*
1724 * Don't allow _HPX to change MPS or MRRS settings. We manage
1725 * those to make sure they're consistent with the rest of the
1726 * platform.
1727 */
1728 hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
1729 PCI_EXP_DEVCTL_READRQ;
1730 hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
1731 PCI_EXP_DEVCTL_READRQ);
1732
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001733 /* Initialize Device Control Register */
1734 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1735 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1736
1737 /* Initialize Link Control Register */
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001738 if (pcie_cap_has_lnkctl(dev)) {
1739
1740 /*
1741 * If the Root Port supports Read Completion Boundary of
1742 * 128, set RCB to 128. Otherwise, clear it.
1743 */
1744 hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
1745 hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
1746 if (pcie_root_rcb_set(dev))
1747 hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
1748
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001749 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1750 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001751 }
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001752
1753 /* Find Advanced Error Reporting Enhanced Capability */
1754 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1755 if (!pos)
1756 return;
1757
1758 /* Initialize Uncorrectable Error Mask Register */
1759 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1760 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1761 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1762
1763 /* Initialize Uncorrectable Error Severity Register */
1764 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1765 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1766 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1767
1768 /* Initialize Correctable Error Mask Register */
1769 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1770 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1771 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1772
1773 /* Initialize Advanced Error Capabilities and Control Register */
1774 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1775 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
Bjorn Helgaas675734b2017-03-21 13:01:30 -05001776 /* Don't enable ECRC generation or checking if unsupported */
1777 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
1778 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
1779 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
1780 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001781 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1782
1783 /*
1784 * FIXME: The following two registers are not supported yet.
1785 *
1786 * o Secondary Uncorrectable Error Severity Register
1787 * o Secondary Uncorrectable Error Mask Register
1788 */
1789}
1790
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001791int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
Sinan Kaya60db3a42017-01-20 09:16:51 -05001792{
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001793 struct pci_host_bridge *host;
1794 u32 cap;
1795 u16 ctl;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001796 int ret;
1797
1798 if (!pci_is_pcie(dev))
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001799 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001800
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001801 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
Sinan Kaya60db3a42017-01-20 09:16:51 -05001802 if (ret)
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001803 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001804
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001805 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
1806 return 0;
1807
1808 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
1809 if (ret)
1810 return 0;
1811
1812 host = pci_find_host_bridge(dev->bus);
1813 if (!host)
1814 return 0;
1815
1816 /*
1817 * If some device in the hierarchy doesn't handle Extended Tags
1818 * correctly, make sure they're disabled.
1819 */
1820 if (host->no_ext_tags) {
1821 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
1822 dev_info(&dev->dev, "disabling Extended Tags\n");
1823 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1824 PCI_EXP_DEVCTL_EXT_TAG);
1825 }
1826 return 0;
1827 }
1828
1829 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
1830 dev_info(&dev->dev, "enabling Extended Tags\n");
Sinan Kaya60db3a42017-01-20 09:16:51 -05001831 pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
1832 PCI_EXP_DEVCTL_EXT_TAG);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001833 }
1834 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001835}
1836
dingtianhonga99b6462017-08-15 11:23:23 +08001837/**
1838 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
1839 * @dev: PCI device to query
1840 *
1841 * Returns true if the device has enabled relaxed ordering attribute.
1842 */
1843bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
1844{
1845 u16 v;
1846
1847 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
1848
1849 return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
1850}
1851EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
1852
1853static void pci_configure_relaxed_ordering(struct pci_dev *dev)
1854{
1855 struct pci_dev *root;
1856
1857 /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
1858 if (dev->is_virtfn)
1859 return;
1860
1861 if (!pcie_relaxed_ordering_enabled(dev))
1862 return;
1863
1864 /*
1865 * For now, we only deal with Relaxed Ordering issues with Root
1866 * Ports. Peer-to-Peer DMA is another can of worms.
1867 */
1868 root = pci_find_pcie_root_port(dev);
1869 if (!root)
1870 return;
1871
1872 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
1873 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1874 PCI_EXP_DEVCTL_RELAX_EN);
1875 dev_info(&dev->dev, "Disable Relaxed Ordering because the Root Port didn't support it\n");
1876 }
1877}
1878
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001879static void pci_configure_device(struct pci_dev *dev)
1880{
1881 struct hotplug_params hpp;
1882 int ret;
1883
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001884 pci_configure_mps(dev);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001885 pci_configure_extended_tags(dev, NULL);
dingtianhonga99b6462017-08-15 11:23:23 +08001886 pci_configure_relaxed_ordering(dev);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001887
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001888 memset(&hpp, 0, sizeof(hpp));
1889 ret = pci_get_hp_params(dev, &hpp);
1890 if (ret)
1891 return;
1892
1893 program_hpp_type2(dev, hpp.t2);
1894 program_hpp_type1(dev, hpp.t1);
1895 program_hpp_type0(dev, hpp.t0);
1896}
1897
Zhao, Yu201de562008-10-13 19:49:55 +08001898static void pci_release_capabilities(struct pci_dev *dev)
1899{
1900 pci_vpd_release(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08001901 pci_iov_release(dev);
Yinghai Luf7968412012-02-11 00:18:30 -08001902 pci_free_cap_save_buffers(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001903}
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905/**
1906 * pci_release_dev - free a pci device structure when all users of it are finished.
1907 * @dev: device that's been disconnected
1908 *
1909 * Will be called only by the device core when all users of this pci device are
1910 * done.
1911 */
1912static void pci_release_dev(struct device *dev)
1913{
Rafael J. Wysocki04480092014-02-01 15:38:29 +01001914 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Rafael J. Wysocki04480092014-02-01 15:38:29 +01001916 pci_dev = to_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001917 pci_release_capabilities(pci_dev);
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10001918 pci_release_of_node(pci_dev);
Sebastian Ott6ae32c52013-06-04 19:18:14 +02001919 pcibios_release_device(pci_dev);
Gu Zheng8b1fce02013-05-25 21:48:31 +08001920 pci_bus_put(pci_dev->bus);
Alex Williamson782a9852014-05-20 08:53:21 -06001921 kfree(pci_dev->driver_override);
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001922 kfree(pci_dev->dma_alias_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 kfree(pci_dev);
1924}
1925
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001926struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
Michael Ellerman65891212007-04-05 17:19:08 +10001927{
1928 struct pci_dev *dev;
1929
1930 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
1931 if (!dev)
1932 return NULL;
1933
Michael Ellerman65891212007-04-05 17:19:08 +10001934 INIT_LIST_HEAD(&dev->bus_list);
Brian King88e7b162013-04-08 03:05:07 +00001935 dev->dev.type = &pci_dev_type;
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001936 dev->bus = pci_bus_get(bus);
Michael Ellerman65891212007-04-05 17:19:08 +10001937
1938 return dev;
1939}
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001940EXPORT_SYMBOL(pci_alloc_dev);
1941
Sinan Kaya62bc6a62017-08-29 14:45:44 -05001942static bool pci_bus_crs_vendor_id(u32 l)
1943{
1944 return (l & 0xffff) == 0x0001;
1945}
1946
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001947static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
1948 int timeout)
Yinghai Luefdc87d2012-01-27 10:55:10 -08001949{
1950 int delay = 1;
1951
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001952 if (!pci_bus_crs_vendor_id(*l))
1953 return true; /* not a CRS completion */
Yinghai Luefdc87d2012-01-27 10:55:10 -08001954
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001955 if (!timeout)
1956 return false; /* CRS, but caller doesn't want to wait */
Yinghai Luefdc87d2012-01-27 10:55:10 -08001957
Rajat Jain89665a6a2014-09-08 14:19:49 -07001958 /*
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001959 * We got the reserved Vendor ID that indicates a completion with
1960 * Configuration Request Retry Status (CRS). Retry until we get a
1961 * valid Vendor ID or we time out.
Rajat Jain89665a6a2014-09-08 14:19:49 -07001962 */
Sinan Kaya62bc6a62017-08-29 14:45:44 -05001963 while (pci_bus_crs_vendor_id(*l)) {
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001964 if (delay > timeout) {
Sinan Kayae78e6612017-08-29 14:45:45 -05001965 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
1966 pci_domain_nr(bus), bus->number,
1967 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
1968
Yinghai Luefdc87d2012-01-27 10:55:10 -08001969 return false;
1970 }
Sinan Kayae78e6612017-08-29 14:45:45 -05001971 if (delay >= 1000)
1972 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
1973 pci_domain_nr(bus), bus->number,
1974 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
Bjorn Helgaas9f982752017-08-29 14:45:43 -05001975
1976 msleep(delay);
1977 delay *= 2;
1978
1979 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1980 return false;
Yinghai Luefdc87d2012-01-27 10:55:10 -08001981 }
1982
Sinan Kayae78e6612017-08-29 14:45:45 -05001983 if (delay >= 1000)
1984 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
1985 pci_domain_nr(bus), bus->number,
1986 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
1987
Yinghai Luefdc87d2012-01-27 10:55:10 -08001988 return true;
1989}
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001990
1991bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
1992 int timeout)
1993{
Yinghai Luefdc87d2012-01-27 10:55:10 -08001994 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1995 return false;
1996
1997 /* some broken boards return 0 or ~0 if a slot is empty: */
1998 if (*l == 0xffffffff || *l == 0x00000000 ||
1999 *l == 0x0000ffff || *l == 0xffff0000)
2000 return false;
2001
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002002 if (pci_bus_crs_vendor_id(*l))
2003 return pci_bus_wait_crs(bus, devfn, l, timeout);
Yinghai Luefdc87d2012-01-27 10:55:10 -08002004
2005 return true;
2006}
2007EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009/*
2010 * Read the config data for a PCI device, sanity-check it
2011 * and fill in the dev structure...
2012 */
Adrian Bunk7f7b5de2008-04-18 13:53:55 -07002013static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct pci_dev *dev;
2016 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Yinghai Luefdc87d2012-01-27 10:55:10 -08002018 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return NULL;
2020
Gu Zheng8b1fce02013-05-25 21:48:31 +08002021 dev = pci_alloc_dev(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (!dev)
2023 return NULL;
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 dev->devfn = devfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 dev->vendor = l & 0xffff;
2027 dev->device = (l >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10002029 pci_set_of_node(dev);
2030
Yu Zhao480b93b2009-03-20 11:25:14 +08002031 if (pci_setup_device(dev)) {
Gu Zheng8b1fce02013-05-25 21:48:31 +08002032 pci_bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 kfree(dev);
2034 return NULL;
2035 }
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002036
2037 return dev;
2038}
2039
Zhao, Yu201de562008-10-13 19:49:55 +08002040static void pci_init_capabilities(struct pci_dev *dev)
2041{
Sean O. Stalley938174e2015-10-29 17:35:39 -05002042 /* Enhanced Allocation */
2043 pci_ea_init(dev);
2044
Guilherme G. Piccolie80e7edc2015-10-21 12:17:35 -02002045 /* Setup MSI caps & disable MSI/MSI-X interrupts */
2046 pci_msi_setup_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002047
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002048 /* Buffers for saving PCIe and PCI-X capabilities */
2049 pci_allocate_cap_save_buffers(dev);
2050
Zhao, Yu201de562008-10-13 19:49:55 +08002051 /* Power Management */
2052 pci_pm_init(dev);
2053
2054 /* Vital Product Data */
Bjorn Helgaasf1cd93f2016-02-22 13:58:37 -06002055 pci_vpd_init(dev);
Yu Zhao58c3a722008-10-14 14:02:53 +08002056
2057 /* Alternative Routing-ID Forwarding */
Yijing Wang31ab2472013-01-15 11:12:17 +08002058 pci_configure_ari(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08002059
2060 /* Single Root I/O Virtualization */
2061 pci_iov_init(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07002062
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -05002063 /* Address Translation Services */
2064 pci_ats_init(dev);
2065
Allen Kayae21ee62009-10-07 10:27:17 -07002066 /* Enable ACS P2P upstream forwarding */
Chris Wright5d990b62009-12-04 12:15:21 -08002067 pci_enable_acs(dev);
Taku Izumib07461a2015-09-17 10:09:37 -05002068
Jonathan Yong9bb04a02016-06-11 14:13:38 -05002069 /* Precision Time Measurement */
2070 pci_ptm_init(dev);
Bjorn Helgaas4dc2db02016-10-03 09:42:57 -05002071
Keith Busch66b80802016-09-27 16:23:34 -04002072 /* Advanced Error Reporting */
2073 pci_aer_init(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002074}
2075
Marc Zyngier098259e2015-10-02 10:19:32 +01002076/*
2077 * This is the equivalent of pci_host_bridge_msi_domain that acts on
2078 * devices. Firmware interfaces that can select the MSI domain on a
2079 * per-device basis should be called from here.
2080 */
2081static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2082{
2083 struct irq_domain *d;
2084
2085 /*
2086 * If a domain has been set through the pcibios_add_device
2087 * callback, then this is the one (platform code knows best).
2088 */
2089 d = dev_get_msi_domain(&dev->dev);
2090 if (d)
2091 return d;
2092
Marc Zyngier54fa97e2015-10-02 14:43:06 +01002093 /*
2094 * Let's see if we have a firmware interface able to provide
2095 * the domain.
2096 */
2097 d = pci_msi_get_device_domain(dev);
2098 if (d)
2099 return d;
2100
Marc Zyngier098259e2015-10-02 10:19:32 +01002101 return NULL;
2102}
2103
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002104static void pci_set_msi_domain(struct pci_dev *dev)
2105{
Marc Zyngier098259e2015-10-02 10:19:32 +01002106 struct irq_domain *d;
2107
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002108 /*
Marc Zyngier098259e2015-10-02 10:19:32 +01002109 * If the platform or firmware interfaces cannot supply a
2110 * device-specific MSI domain, then inherit the default domain
2111 * from the host bridge itself.
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002112 */
Marc Zyngier098259e2015-10-02 10:19:32 +01002113 d = pci_dev_msi_domain(dev);
2114 if (!d)
2115 d = dev_get_msi_domain(&dev->bus->dev);
2116
2117 dev_set_msi_domain(&dev->dev, d);
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002118}
2119
Sam Ravnborg96bde062007-03-26 21:53:30 -08002120void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002121{
Yinghai Lu4f535092013-01-21 13:20:52 -08002122 int ret;
2123
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06002124 pci_configure_device(dev);
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 device_initialize(&dev->dev);
2127 dev->dev.release = pci_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Yinghai Lu7629d192013-01-21 13:20:44 -08002129 set_dev_node(&dev->dev, pcibus_to_node(bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 dev->dev.dma_mask = &dev->dma_mask;
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002131 dev->dev.dma_parms = &dev->dma_parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 dev->dev.coherent_dma_mask = 0xffffffffull;
2133
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002134 pci_set_dma_max_seg_size(dev, 65536);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08002135 pci_set_dma_seg_boundary(dev, 0xffffffff);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Fix up broken headers */
2138 pci_fixup_device(pci_fixup_header, dev);
2139
Yinghai Lu2069ecf2012-02-15 21:40:31 -08002140 /* moved out from quirk header fixup code */
2141 pci_reassigndev_resource_alignment(dev);
2142
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02002143 /* Clear the state_saved flag. */
2144 dev->state_saved = false;
2145
Zhao, Yu201de562008-10-13 19:49:55 +08002146 /* Initialize various capabilities */
2147 pci_init_capabilities(dev);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /*
2150 * Add the device to our list of discovered devices
2151 * and the bus list for fixup functions, etc.
2152 */
Zhang Yanmind71374d2006-06-02 12:35:43 +08002153 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 list_add_tail(&dev->bus_list, &bus->devices);
Zhang Yanmind71374d2006-06-02 12:35:43 +08002155 up_write(&pci_bus_sem);
Yinghai Lu4f535092013-01-21 13:20:52 -08002156
Yinghai Lu4f535092013-01-21 13:20:52 -08002157 ret = pcibios_add_device(dev);
2158 WARN_ON(ret < 0);
2159
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002160 /* Setup MSI irq domain */
2161 pci_set_msi_domain(dev);
2162
Yinghai Lu4f535092013-01-21 13:20:52 -08002163 /* Notifier could use PCI capabilities */
2164 dev->match_driver = false;
2165 ret = device_add(&dev->dev);
2166 WARN_ON(ret < 0);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002167}
2168
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002169struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002170{
2171 struct pci_dev *dev;
2172
Trent Piepho90bdb312009-03-20 14:56:00 -06002173 dev = pci_get_slot(bus, devfn);
2174 if (dev) {
2175 pci_dev_put(dev);
2176 return dev;
2177 }
2178
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002179 dev = pci_scan_device(bus, devfn);
2180 if (!dev)
2181 return NULL;
2182
2183 pci_device_add(dev, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 return dev;
2186}
Adrian Bunkb73e9682007-11-21 15:07:11 -08002187EXPORT_SYMBOL(pci_scan_single_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002189static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002190{
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002191 int pos;
2192 u16 cap = 0;
2193 unsigned next_fn;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002194
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002195 if (pci_ari_enabled(bus)) {
2196 if (!dev)
2197 return 0;
2198 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2199 if (!pos)
2200 return 0;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002201
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002202 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2203 next_fn = PCI_ARI_CAP_NFN(cap);
2204 if (next_fn <= fn)
2205 return 0; /* protect against malformed list */
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002206
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002207 return next_fn;
2208 }
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002209
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002210 /* dev may be NULL for non-contiguous multifunction devices */
2211 if (!dev || dev->multifunction)
2212 return (fn + 1) % 8;
2213
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002214 return 0;
2215}
2216
2217static int only_one_child(struct pci_bus *bus)
2218{
2219 struct pci_dev *parent = bus->self;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06002220
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002221 if (!parent || !pci_is_pcie(parent))
2222 return 0;
Yijing Wang62f87c02012-07-24 17:20:03 +08002223 if (pci_pcie_type(parent) == PCI_EXP_TYPE_ROOT_PORT)
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06002224 return 1;
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002225
2226 /*
2227 * PCIe downstream ports are bridges that normally lead to only a
2228 * device 0, but if PCI_SCAN_ALL_PCIE_DEVS is set, scan all
2229 * possible devices, not just device 0. See PCIe spec r3.0,
2230 * sec 7.3.1.
2231 */
Yijing Wang777e61e2015-05-21 15:05:04 +08002232 if (parent->has_secondary_link &&
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06002233 !pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002234 return 1;
2235 return 0;
2236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/**
2239 * pci_scan_slot - scan a PCI slot on a bus for devices.
2240 * @bus: PCI bus to scan
2241 * @devfn: slot number to scan (must have zero function.)
2242 *
2243 * Scan a PCI slot on the specified PCI bus for devices, adding
2244 * discovered devices to the @bus->devices list. New devices
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -08002245 * will not have is_added set.
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002246 *
2247 * Returns the number of new devices found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 */
Sam Ravnborg96bde062007-03-26 21:53:30 -08002249int pci_scan_slot(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002251 unsigned fn, nr = 0;
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002252 struct pci_dev *dev;
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002253
2254 if (only_one_child(bus) && (devfn > 0))
2255 return 0; /* Already scanned the entire slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002257 dev = pci_scan_single_device(bus, devfn);
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002258 if (!dev)
2259 return 0;
2260 if (!dev->is_added)
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002261 nr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002263 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002264 dev = pci_scan_single_device(bus, devfn + fn);
2265 if (dev) {
2266 if (!dev->is_added)
2267 nr++;
2268 dev->multifunction = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 }
Shaohua Li7d715a62008-02-25 09:46:41 +08002271
Shaohua Li149e1632008-07-23 10:32:31 +08002272 /* only one slot has pcie device */
2273 if (bus->self && nr)
Shaohua Li7d715a62008-02-25 09:46:41 +08002274 pcie_aspm_init_link_state(bus->self);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return nr;
2277}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002278EXPORT_SYMBOL(pci_scan_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Jon Masonb03e7492011-07-20 15:20:54 -05002280static int pcie_find_smpss(struct pci_dev *dev, void *data)
2281{
2282 u8 *smpss = data;
2283
2284 if (!pci_is_pcie(dev))
2285 return 0;
2286
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002287 /*
2288 * We don't have a way to change MPS settings on devices that have
2289 * drivers attached. A hot-added device might support only the minimum
2290 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
2291 * where devices may be hot-added, we limit the fabric MPS to 128 so
2292 * hot-added devices will work correctly.
2293 *
2294 * However, if we hot-add a device to a slot directly below a Root
2295 * Port, it's impossible for there to be other existing devices below
2296 * the port. We don't limit the MPS in this case because we can
2297 * reconfigure MPS on both the Root Port and the hot-added device,
2298 * and there are no other devices involved.
2299 *
2300 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
Jon Masonb03e7492011-07-20 15:20:54 -05002301 */
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002302 if (dev->is_hotplug_bridge &&
2303 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
Jon Masonb03e7492011-07-20 15:20:54 -05002304 *smpss = 0;
2305
2306 if (*smpss > dev->pcie_mpss)
2307 *smpss = dev->pcie_mpss;
2308
2309 return 0;
2310}
2311
2312static void pcie_write_mps(struct pci_dev *dev, int mps)
2313{
Jon Mason62f392e2011-10-14 14:56:14 -05002314 int rc;
Jon Masonb03e7492011-07-20 15:20:54 -05002315
2316 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
Jon Mason62f392e2011-10-14 14:56:14 -05002317 mps = 128 << dev->pcie_mpss;
Jon Masonb03e7492011-07-20 15:20:54 -05002318
Yijing Wang62f87c02012-07-24 17:20:03 +08002319 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2320 dev->bus->self)
Jon Mason62f392e2011-10-14 14:56:14 -05002321 /* For "Performance", the assumption is made that
Jon Masonb03e7492011-07-20 15:20:54 -05002322 * downstream communication will never be larger than
2323 * the MRRS. So, the MPS only needs to be configured
2324 * for the upstream communication. This being the case,
2325 * walk from the top down and set the MPS of the child
2326 * to that of the parent bus.
Jon Mason62f392e2011-10-14 14:56:14 -05002327 *
2328 * Configure the device MPS with the smaller of the
2329 * device MPSS or the bridge MPS (which is assumed to be
2330 * properly configured at this point to the largest
2331 * allowable MPS based on its parent bus).
Jon Masonb03e7492011-07-20 15:20:54 -05002332 */
Jon Mason62f392e2011-10-14 14:56:14 -05002333 mps = min(mps, pcie_get_mps(dev->bus->self));
Jon Masonb03e7492011-07-20 15:20:54 -05002334 }
2335
2336 rc = pcie_set_mps(dev, mps);
2337 if (rc)
2338 dev_err(&dev->dev, "Failed attempting to set the MPS\n");
2339}
2340
Jon Mason62f392e2011-10-14 14:56:14 -05002341static void pcie_write_mrrs(struct pci_dev *dev)
Jon Masonb03e7492011-07-20 15:20:54 -05002342{
Jon Mason62f392e2011-10-14 14:56:14 -05002343 int rc, mrrs;
Jon Masonb03e7492011-07-20 15:20:54 -05002344
Jon Masoned2888e2011-09-08 16:41:18 -05002345 /* In the "safe" case, do not configure the MRRS. There appear to be
2346 * issues with setting MRRS to 0 on a number of devices.
2347 */
Jon Masoned2888e2011-09-08 16:41:18 -05002348 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2349 return;
Jon Masonb03e7492011-07-20 15:20:54 -05002350
Jon Masoned2888e2011-09-08 16:41:18 -05002351 /* For Max performance, the MRRS must be set to the largest supported
2352 * value. However, it cannot be configured larger than the MPS the
Jon Mason62f392e2011-10-14 14:56:14 -05002353 * device or the bus can support. This should already be properly
2354 * configured by a prior call to pcie_write_mps.
Jon Masoned2888e2011-09-08 16:41:18 -05002355 */
Jon Mason62f392e2011-10-14 14:56:14 -05002356 mrrs = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002357
2358 /* MRRS is a R/W register. Invalid values can be written, but a
Jon Masoned2888e2011-09-08 16:41:18 -05002359 * subsequent read will verify if the value is acceptable or not.
Jon Masonb03e7492011-07-20 15:20:54 -05002360 * If the MRRS value provided is not acceptable (e.g., too large),
2361 * shrink the value until it is acceptable to the HW.
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002362 */
Jon Masonb03e7492011-07-20 15:20:54 -05002363 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2364 rc = pcie_set_readrq(dev, mrrs);
Jon Mason62f392e2011-10-14 14:56:14 -05002365 if (!rc)
2366 break;
Jon Masonb03e7492011-07-20 15:20:54 -05002367
Jon Mason62f392e2011-10-14 14:56:14 -05002368 dev_warn(&dev->dev, "Failed attempting to set the MRRS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002369 mrrs /= 2;
2370 }
Jon Mason62f392e2011-10-14 14:56:14 -05002371
2372 if (mrrs < 128)
Ryan Desfosses227f0642014-04-18 20:13:50 -04002373 dev_err(&dev->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 -05002374}
2375
2376static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2377{
Jon Masona513a99a72011-10-14 14:56:16 -05002378 int mps, orig_mps;
Jon Masonb03e7492011-07-20 15:20:54 -05002379
2380 if (!pci_is_pcie(dev))
2381 return 0;
2382
Keith Busch27d868b2015-08-24 08:48:16 -05002383 if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2384 pcie_bus_config == PCIE_BUS_DEFAULT)
Yijing Wang5895af72013-08-26 16:33:06 +08002385 return 0;
Yijing Wang5895af72013-08-26 16:33:06 +08002386
Jon Masona513a99a72011-10-14 14:56:16 -05002387 mps = 128 << *(u8 *)data;
2388 orig_mps = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002389
2390 pcie_write_mps(dev, mps);
Jon Mason62f392e2011-10-14 14:56:14 -05002391 pcie_write_mrrs(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002392
Ryan Desfosses227f0642014-04-18 20:13:50 -04002393 dev_info(&dev->dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
2394 pcie_get_mps(dev), 128 << dev->pcie_mpss,
Jon Masona513a99a72011-10-14 14:56:16 -05002395 orig_mps, pcie_get_readrq(dev));
Jon Masonb03e7492011-07-20 15:20:54 -05002396
2397 return 0;
2398}
2399
Jon Masona513a99a72011-10-14 14:56:16 -05002400/* pcie_bus_configure_settings requires that pci_walk_bus work in a top-down,
Jon Masonb03e7492011-07-20 15:20:54 -05002401 * parents then children fashion. If this changes, then this code will not
2402 * work as designed.
2403 */
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002404void pcie_bus_configure_settings(struct pci_bus *bus)
Jon Masonb03e7492011-07-20 15:20:54 -05002405{
Bjorn Helgaas1e358f92014-04-29 12:51:55 -06002406 u8 smpss = 0;
Jon Masonb03e7492011-07-20 15:20:54 -05002407
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002408 if (!bus->self)
2409 return;
2410
Jon Masonb03e7492011-07-20 15:20:54 -05002411 if (!pci_is_pcie(bus->self))
2412 return;
2413
Jon Mason5f39e672011-10-03 09:50:20 -05002414 /* FIXME - Peer to peer DMA is possible, though the endpoint would need
Jon Mason33154722013-08-26 16:33:05 +08002415 * to be aware of the MPS of the destination. To work around this,
Jon Mason5f39e672011-10-03 09:50:20 -05002416 * simply force the MPS of the entire system to the smallest possible.
2417 */
2418 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2419 smpss = 0;
2420
Jon Masonb03e7492011-07-20 15:20:54 -05002421 if (pcie_bus_config == PCIE_BUS_SAFE) {
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002422 smpss = bus->self->pcie_mpss;
Jon Mason5f39e672011-10-03 09:50:20 -05002423
Jon Masonb03e7492011-07-20 15:20:54 -05002424 pcie_find_smpss(bus->self, &smpss);
2425 pci_walk_bus(bus, pcie_find_smpss, &smpss);
2426 }
2427
2428 pcie_bus_configure_set(bus->self, &smpss);
2429 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2430}
Jon Masondebc3b72011-08-02 00:01:18 -05002431EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
Jon Masonb03e7492011-07-20 15:20:54 -05002432
Palmer Dabbeltbccf90d2017-06-23 18:50:42 -07002433/*
2434 * Called after each bus is probed, but before its children are examined. This
2435 * is marked as __weak because multiple architectures define it.
2436 */
2437void __weak pcibios_fixup_bus(struct pci_bus *bus)
2438{
2439 /* nothing to do, expected to be removed in the future */
2440}
2441
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002442/**
2443 * pci_scan_child_bus_extend() - Scan devices below a bus
2444 * @bus: Bus to scan for devices
2445 * @available_buses: Total number of buses available (%0 does not try to
2446 * extend beyond the minimal)
2447 *
2448 * Scans devices below @bus including subordinate buses. Returns new
2449 * subordinate number including all the found devices. Passing
2450 * @available_buses causes the remaining bus space to be distributed
2451 * equally between hotplug-capable bridges to allow future extension of the
2452 * hierarchy.
2453 */
2454static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2455 unsigned int available_buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002457 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2458 unsigned int start = bus->busn_res.start;
2459 unsigned int devfn, cmax, max = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 struct pci_dev *dev;
2461
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002462 dev_dbg(&bus->dev, "scanning bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* Go find them, Rover! */
2465 for (devfn = 0; devfn < 0x100; devfn += 8)
2466 pci_scan_slot(bus, devfn);
2467
Yu Zhaoa28724b2009-03-20 11:25:13 +08002468 /* Reserve buses for SR-IOV capability. */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002469 used_buses = pci_iov_bus_range(bus);
2470 max += used_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +08002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * After performing arch-dependent fixup of the bus, look behind
2474 * all PCI-to-PCI bridges on this bus.
2475 */
Alex Chiang74710de2009-03-20 14:56:10 -06002476 if (!bus->is_added) {
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002477 dev_dbg(&bus->dev, "fixups for bus\n");
Alex Chiang74710de2009-03-20 14:56:10 -06002478 pcibios_fixup_bus(bus);
Jiang Liu981cf9e2013-04-12 05:44:16 +00002479 bus->is_added = 1;
Alex Chiang74710de2009-03-20 14:56:10 -06002480 }
2481
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002482 /*
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002483 * Calculate how many hotplug bridges and normal bridges there
2484 * are on this bus. We will distribute the additional available
2485 * buses between hotplug bridges.
2486 */
2487 for_each_pci_bridge(dev, bus) {
2488 if (dev->is_hotplug_bridge)
2489 hotplug_bridges++;
2490 else
2491 normal_bridges++;
2492 }
2493
2494 /*
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002495 * Scan bridges that are already configured. We don't touch them
2496 * unless they are misconfigured (which will be done in the second
2497 * scan below).
2498 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002499 for_each_pci_bridge(dev, bus) {
2500 cmax = max;
2501 max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
2502 used_buses += cmax - max;
2503 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002504
2505 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002506 for_each_pci_bridge(dev, bus) {
2507 unsigned int buses = 0;
2508
2509 if (!hotplug_bridges && normal_bridges == 1) {
2510 /*
2511 * There is only one bridge on the bus (upstream
2512 * port) so it gets all available buses which it
2513 * can then distribute to the possible hotplug
2514 * bridges below.
2515 */
2516 buses = available_buses;
2517 } else if (dev->is_hotplug_bridge) {
2518 /*
2519 * Distribute the extra buses between hotplug
2520 * bridges if any.
2521 */
2522 buses = available_buses / hotplug_bridges;
2523 buses = min(buses, available_buses - used_buses);
2524 }
2525
2526 cmax = max;
2527 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
2528 used_buses += max - cmax;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 /*
Keith Busche16b4662016-07-21 21:40:28 -06002532 * Make sure a hotplug bridge has at least the minimum requested
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002533 * number of buses but allow it to grow up to the maximum available
2534 * bus number of there is room.
Keith Busche16b4662016-07-21 21:40:28 -06002535 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002536 if (bus->self && bus->self->is_hotplug_bridge) {
2537 used_buses = max_t(unsigned int, available_buses,
2538 pci_hotplug_bus_size - 1);
2539 if (max - start < used_buses) {
2540 max = start + used_buses;
Mika Westerberga20c7f32017-10-13 21:35:43 +03002541
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002542 /* Do not allocate more buses than we have room left */
2543 if (max > bus->busn_res.end)
2544 max = bus->busn_res.end;
2545
2546 dev_dbg(&bus->dev, "%pR extended by %#02x\n",
2547 &bus->busn_res, max - start);
2548 }
Keith Busche16b4662016-07-21 21:40:28 -06002549 }
2550
2551 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 * We've scanned the bus and so we know all about what's on
2553 * the other side of any bridges that may be on this bus plus
2554 * any devices.
2555 *
2556 * Return how far we've got finding sub-buses.
2557 */
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002558 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return max;
2560}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002561
2562/**
2563 * pci_scan_child_bus() - Scan devices below a bus
2564 * @bus: Bus to scan for devices
2565 *
2566 * Scans devices below @bus including subordinate buses. Returns new
2567 * subordinate number including all the found devices.
2568 */
2569unsigned int pci_scan_child_bus(struct pci_bus *bus)
2570{
2571 return pci_scan_child_bus_extend(bus, 0);
2572}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002573EXPORT_SYMBOL_GPL(pci_scan_child_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002575/**
2576 * pcibios_root_bridge_prepare - Platform-specific host bridge setup.
2577 * @bridge: Host bridge to set up.
2578 *
2579 * Default empty implementation. Replace with an architecture-specific setup
2580 * routine, if necessary.
2581 */
2582int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
2583{
2584 return 0;
2585}
2586
Jiang Liu10a95742013-04-12 05:44:20 +00002587void __weak pcibios_add_bus(struct pci_bus *bus)
2588{
2589}
2590
2591void __weak pcibios_remove_bus(struct pci_bus *bus)
2592{
2593}
2594
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002595struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
2596 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07002598 int error;
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07002599 struct pci_host_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Thierry Reding59094062016-11-25 11:57:10 +01002601 bridge = pci_alloc_host_bridge(0);
Yinghai Lu7b543662012-04-02 18:31:53 -07002602 if (!bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002603 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -07002604
2605 bridge->dev.parent = parent;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002606
2607 list_splice_init(resources, &bridge->windows);
2608 bridge->sysdata = sysdata;
2609 bridge->busnr = bus;
2610 bridge->ops = ops;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002611
2612 error = pci_register_host_bridge(bridge);
2613 if (error < 0)
Jiang Liu343df772013-06-07 01:10:08 +08002614 goto err_out;
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002615
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002616 return bridge->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Yinghai Lu7b543662012-04-02 18:31:53 -07002618err_out:
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002619 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return NULL;
2621}
Ray Juie6b29de2015-04-08 11:21:33 -07002622EXPORT_SYMBOL_GPL(pci_create_root_bus);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002623
Yinghai Lu98a35832012-05-18 11:35:50 -06002624int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2625{
2626 struct resource *res = &b->busn_res;
2627 struct resource *parent_res, *conflict;
2628
2629 res->start = bus;
2630 res->end = bus_max;
2631 res->flags = IORESOURCE_BUS;
2632
2633 if (!pci_is_root_bus(b))
2634 parent_res = &b->parent->busn_res;
2635 else {
2636 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
2637 res->flags |= IORESOURCE_PCI_FIXED;
2638 }
2639
Andreas Noeverced04d12014-01-23 21:59:24 +01002640 conflict = request_resource_conflict(parent_res, res);
Yinghai Lu98a35832012-05-18 11:35:50 -06002641
2642 if (conflict)
2643 dev_printk(KERN_DEBUG, &b->dev,
2644 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
2645 res, pci_is_root_bus(b) ? "domain " : "",
2646 parent_res, conflict->name, conflict);
Yinghai Lu98a35832012-05-18 11:35:50 -06002647
2648 return conflict == NULL;
2649}
2650
2651int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
2652{
2653 struct resource *res = &b->busn_res;
2654 struct resource old_res = *res;
2655 resource_size_t size;
2656 int ret;
2657
2658 if (res->start > bus_max)
2659 return -EINVAL;
2660
2661 size = bus_max - res->start + 1;
2662 ret = adjust_resource(res, res->start, size);
2663 dev_printk(KERN_DEBUG, &b->dev,
2664 "busn_res: %pR end %s updated to %02x\n",
2665 &old_res, ret ? "can not be" : "is", bus_max);
2666
2667 if (!ret && !res->parent)
2668 pci_bus_insert_busn_res(b, res->start, res->end);
2669
2670 return ret;
2671}
2672
2673void pci_bus_release_busn_res(struct pci_bus *b)
2674{
2675 struct resource *res = &b->busn_res;
2676 int ret;
2677
2678 if (!res->flags || !res->parent)
2679 return;
2680
2681 ret = release_resource(res);
2682 dev_printk(KERN_DEBUG, &b->dev,
2683 "busn_res: %pR %s released\n",
2684 res, ret ? "can not be" : "is");
2685}
2686
Lorenzo Pieralisi1228c4b2017-06-28 15:13:55 -05002687int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
2688{
2689 struct resource_entry *window;
2690 bool found = false;
2691 struct pci_bus *b;
2692 int max, bus, ret;
2693
2694 if (!bridge)
2695 return -EINVAL;
2696
2697 resource_list_for_each_entry(window, &bridge->windows)
2698 if (window->res->flags & IORESOURCE_BUS) {
2699 found = true;
2700 break;
2701 }
2702
2703 ret = pci_register_host_bridge(bridge);
2704 if (ret < 0)
2705 return ret;
2706
2707 b = bridge->bus;
2708 bus = bridge->busnr;
2709
2710 if (!found) {
2711 dev_info(&b->dev,
2712 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2713 bus);
2714 pci_bus_insert_busn_res(b, bus, 255);
2715 }
2716
2717 max = pci_scan_child_bus(b);
2718
2719 if (!found)
2720 pci_bus_update_busn_res_end(b, max);
2721
2722 return 0;
2723}
2724EXPORT_SYMBOL(pci_scan_root_bus_bridge);
2725
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002726struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
2727 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002728{
Jiang Liu14d76b62015-02-05 13:44:44 +08002729 struct resource_entry *window;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002730 bool found = false;
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002731 struct pci_bus *b;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002732 int max;
2733
Jiang Liu14d76b62015-02-05 13:44:44 +08002734 resource_list_for_each_entry(window, resources)
Yinghai Lu4d99f522012-05-17 18:51:12 -07002735 if (window->res->flags & IORESOURCE_BUS) {
2736 found = true;
2737 break;
2738 }
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002739
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002740 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002741 if (!b)
2742 return NULL;
2743
Yinghai Lu4d99f522012-05-17 18:51:12 -07002744 if (!found) {
2745 dev_info(&b->dev,
2746 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2747 bus);
2748 pci_bus_insert_busn_res(b, bus, 255);
2749 }
2750
2751 max = pci_scan_child_bus(b);
2752
2753 if (!found)
2754 pci_bus_update_busn_res_end(b, max);
2755
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002756 return b;
2757}
2758EXPORT_SYMBOL(pci_scan_root_bus);
2759
Bill Pemberton15856ad2012-11-21 15:35:00 -05002760struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002761 void *sysdata)
2762{
2763 LIST_HEAD(resources);
2764 struct pci_bus *b;
2765
2766 pci_add_resource(&resources, &ioport_resource);
2767 pci_add_resource(&resources, &iomem_resource);
Yinghai Lu857c3b62012-05-17 18:51:12 -07002768 pci_add_resource(&resources, &busn_resource);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002769 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
2770 if (b) {
Yinghai Lu857c3b62012-05-17 18:51:12 -07002771 pci_scan_child_bus(b);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002772 } else {
2773 pci_free_resource_list(&resources);
2774 }
2775 return b;
2776}
2777EXPORT_SYMBOL(pci_scan_bus);
2778
Alex Chiang3ed4fd92009-03-20 14:56:25 -06002779/**
Yinghai Lu2f320522012-01-21 02:08:22 -08002780 * pci_rescan_bus_bridge_resize - scan a PCI bus for devices.
2781 * @bridge: PCI bridge for the bus to scan
2782 *
2783 * Scan a PCI bus and child buses for new devices, add them,
2784 * and enable them, resizing bridge mmio/io resource if necessary
2785 * and possible. The caller must ensure the child devices are already
2786 * removed for resizing to occur.
2787 *
2788 * Returns the max number of subordinate bus discovered.
2789 */
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002790unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
Yinghai Lu2f320522012-01-21 02:08:22 -08002791{
2792 unsigned int max;
2793 struct pci_bus *bus = bridge->subordinate;
2794
2795 max = pci_scan_child_bus(bus);
2796
2797 pci_assign_unassigned_bridge_resources(bridge);
2798
2799 pci_bus_add_devices(bus);
2800
2801 return max;
2802}
2803
Yinghai Lua5213a32012-10-30 14:31:21 -06002804/**
2805 * pci_rescan_bus - scan a PCI bus for devices.
2806 * @bus: PCI bus to scan
2807 *
2808 * Scan a PCI bus and child buses for new devices, adds them,
2809 * and enables them.
2810 *
2811 * Returns the max number of subordinate bus discovered.
2812 */
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002813unsigned int pci_rescan_bus(struct pci_bus *bus)
Yinghai Lua5213a32012-10-30 14:31:21 -06002814{
2815 unsigned int max;
2816
2817 max = pci_scan_child_bus(bus);
2818 pci_assign_unassigned_bus_resources(bus);
2819 pci_bus_add_devices(bus);
2820
2821 return max;
2822}
2823EXPORT_SYMBOL_GPL(pci_rescan_bus);
2824
Rafael J. Wysocki9d169472014-01-10 15:22:18 +01002825/*
2826 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
2827 * routines should always be executed under this mutex.
2828 */
2829static DEFINE_MUTEX(pci_rescan_remove_lock);
2830
2831void pci_lock_rescan_remove(void)
2832{
2833 mutex_lock(&pci_rescan_remove_lock);
2834}
2835EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
2836
2837void pci_unlock_rescan_remove(void)
2838{
2839 mutex_unlock(&pci_rescan_remove_lock);
2840}
2841EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
2842
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002843static int __init pci_sort_bf_cmp(const struct device *d_a,
2844 const struct device *d_b)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002845{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002846 const struct pci_dev *a = to_pci_dev(d_a);
2847 const struct pci_dev *b = to_pci_dev(d_b);
2848
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002849 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
2850 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
2851
2852 if (a->bus->number < b->bus->number) return -1;
2853 else if (a->bus->number > b->bus->number) return 1;
2854
2855 if (a->devfn < b->devfn) return -1;
2856 else if (a->devfn > b->devfn) return 1;
2857
2858 return 0;
2859}
2860
Greg Kroah-Hartman5ff580c2008-02-14 14:56:56 -08002861void __init pci_sort_breadthfirst(void)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002862{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002863 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002864}
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002865
2866int pci_hp_add_bridge(struct pci_dev *dev)
2867{
2868 struct pci_bus *parent = dev->bus;
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002869 int busnr, start = parent->busn_res.start;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002870 unsigned int available_buses = 0;
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002871 int end = parent->busn_res.end;
2872
2873 for (busnr = start; busnr <= end; busnr++) {
2874 if (!pci_find_bus(pci_domain_nr(parent), busnr))
2875 break;
2876 }
2877 if (busnr-- > end) {
2878 dev_err(&dev->dev, "No bus number available for hot-added bridge\n");
2879 return -1;
2880 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002881
2882 /* Scan bridges that are already configured */
2883 busnr = pci_scan_bridge(parent, dev, busnr, 0);
2884
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002885 /*
2886 * Distribute the available bus numbers between hotplug-capable
2887 * bridges to make extending the chain later possible.
2888 */
2889 available_buses = end - busnr;
2890
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002891 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002892 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002893
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002894 if (!dev->subordinate)
2895 return -1;
2896
2897 return 0;
2898}
2899EXPORT_SYMBOL_GPL(pci_hp_add_bridge);