blob: 708094f720fdd42e9a24047ecc667a95dd5d97a8 [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/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -060073 * 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
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600120 /*
121 * Get the lowest of them to find the decode size, and from that
122 * the extent.
123 */
Yinghai Lu07eddf32006-11-29 13:53:10 -0800124 size = (size & ~(size-1)) - 1;
125
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600126 /*
127 * base == maxbase can be valid only if the BAR has already been
128 * programmed with all 1s.
129 */
Yinghai Lu07eddf32006-11-29 13:53:10 -0800130 if (base == maxbase && ((base | size) & mask) != mask)
131 return 0;
132
133 return size;
134}
135
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600136static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
Yinghai Lu07eddf32006-11-29 13:53:10 -0800137{
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600138 u32 mem_type;
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600139 unsigned long flags;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600140
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400141 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600142 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
143 flags |= IORESOURCE_IO;
144 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400145 }
146
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600147 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
148 flags |= IORESOURCE_MEM;
149 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
150 flags |= IORESOURCE_PREFETCH;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400151
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600152 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
153 switch (mem_type) {
154 case PCI_BASE_ADDRESS_MEM_TYPE_32:
155 break;
156 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600157 /* 1M mem BAR treated as 32-bit BAR */
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600158 break;
159 case PCI_BASE_ADDRESS_MEM_TYPE_64:
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600160 flags |= IORESOURCE_MEM_64;
161 break;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600162 default:
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600163 /* mem unknown type treated as 32-bit BAR */
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600164 break;
165 }
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600166 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400167}
168
Zoltan Kiss808e34e2013-08-22 23:19:18 +0100169#define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
170
Yu Zhao0b400c72008-11-22 02:40:40 +0800171/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600172 * pci_read_base - Read a PCI BAR
Yu Zhao0b400c72008-11-22 02:40:40 +0800173 * @dev: the PCI device
174 * @type: type of the BAR
175 * @res: resource buffer to be filled in
176 * @pos: BAR position in the config space
177 *
178 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400179 */
Yu Zhao0b400c72008-11-22 02:40:40 +0800180int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400181 struct resource *res, unsigned int pos)
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400182{
Marc Gonzalezdc5205e2017-04-10 19:46:54 +0200183 u32 l = 0, sz = 0, mask;
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600184 u64 l64, sz64, mask64;
Jacob Pan253d2e52010-07-16 10:19:22 -0700185 u16 orig_cmd;
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800186 struct pci_bus_region region, inverted_region;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400187
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200188 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400189
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600190 /* No printks while decoding is disabled! */
Jacob Pan253d2e52010-07-16 10:19:22 -0700191 if (!dev->mmio_always_on) {
192 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
Zoltan Kiss808e34e2013-08-22 23:19:18 +0100193 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
194 pci_write_config_word(dev, PCI_COMMAND,
195 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
196 }
Jacob Pan253d2e52010-07-16 10:19:22 -0700197 }
198
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400199 res->name = pci_name(dev);
200
201 pci_read_config_dword(dev, pos, &l);
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200202 pci_write_config_dword(dev, pos, l | mask);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400203 pci_read_config_dword(dev, pos, &sz);
204 pci_write_config_dword(dev, pos, l);
205
206 /*
207 * All bits set in sz means the device isn't working properly.
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600208 * If the BAR isn't implemented, all bits must be 0. If it's a
209 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
210 * 1 must be clear.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400211 */
Myron Stowef795d862014-10-30 11:54:43 -0600212 if (sz == 0xffffffff)
213 sz = 0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400214
215 /*
216 * I don't know how l can have all bits set. Copied from old code.
217 * Maybe it fixes a bug on some ancient platform.
218 */
219 if (l == 0xffffffff)
220 l = 0;
221
222 if (type == pci_bar_unknown) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600223 res->flags = decode_bar(dev, l);
224 res->flags |= IORESOURCE_SIZEALIGN;
225 if (res->flags & IORESOURCE_IO) {
Myron Stowef795d862014-10-30 11:54:43 -0600226 l64 = l & PCI_BASE_ADDRESS_IO_MASK;
227 sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
228 mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400229 } else {
Myron Stowef795d862014-10-30 11:54:43 -0600230 l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
231 sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
232 mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400233 }
234 } else {
Bjorn Helgaas7a6d3122016-11-28 17:21:02 -0600235 if (l & PCI_ROM_ADDRESS_ENABLE)
236 res->flags |= IORESOURCE_ROM_ENABLE;
Myron Stowef795d862014-10-30 11:54:43 -0600237 l64 = l & PCI_ROM_ADDRESS_MASK;
238 sz64 = sz & PCI_ROM_ADDRESS_MASK;
Matthias Kaehlcke76dc52682017-04-14 13:38:02 -0700239 mask64 = PCI_ROM_ADDRESS_MASK;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400240 }
241
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600242 if (res->flags & IORESOURCE_MEM_64) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400243 pci_read_config_dword(dev, pos + 4, &l);
244 pci_write_config_dword(dev, pos + 4, ~0);
245 pci_read_config_dword(dev, pos + 4, &sz);
246 pci_write_config_dword(dev, pos + 4, l);
247
248 l64 |= ((u64)l << 32);
249 sz64 |= ((u64)sz << 32);
Myron Stowef795d862014-10-30 11:54:43 -0600250 mask64 |= ((u64)~0 << 32);
251 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400252
Myron Stowef795d862014-10-30 11:54:43 -0600253 if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
254 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400255
Myron Stowef795d862014-10-30 11:54:43 -0600256 if (!sz64)
257 goto fail;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400258
Myron Stowef795d862014-10-30 11:54:43 -0600259 sz64 = pci_size(l64, sz64, mask64);
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600260 if (!sz64) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600261 pci_info(dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600262 pos);
Myron Stowef795d862014-10-30 11:54:43 -0600263 goto fail;
Myron Stowe7e79c5f2014-10-30 11:54:50 -0600264 }
Myron Stowef795d862014-10-30 11:54:43 -0600265
266 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700267 if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
268 && sz64 > 0x100000000ULL) {
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600269 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
270 res->start = 0;
271 res->end = 0;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600272 pci_err(dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
Myron Stowef795d862014-10-30 11:54:43 -0600273 pos, (unsigned long long)sz64);
Bjorn Helgaas23b13bc2014-04-14 15:25:54 -0600274 goto out;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600275 }
276
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700277 if ((sizeof(pci_bus_addr_t) < 8) && l) {
Bjorn Helgaas31e9dd22014-04-29 18:37:47 -0600278 /* Above 32-bit boundary; try to reallocate */
Bjorn Helgaasc83bd902014-02-26 11:26:00 -0700279 res->flags |= IORESOURCE_UNSET;
Bjorn Helgaas72dc5602014-04-29 18:42:49 -0600280 res->start = 0;
281 res->end = sz64;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600282 pci_info(dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
Myron Stowef795d862014-10-30 11:54:43 -0600283 pos, (unsigned long long)l64);
Bjorn Helgaas72dc5602014-04-29 18:42:49 -0600284 goto out;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400285 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400286 }
287
Myron Stowef795d862014-10-30 11:54:43 -0600288 region.start = l64;
289 region.end = l64 + sz64;
290
Yinghai Lufc279852013-12-09 22:54:40 -0800291 pcibios_bus_to_resource(dev->bus, res, &region);
292 pcibios_resource_to_bus(dev->bus, &inverted_region, res);
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800293
294 /*
295 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
296 * the corresponding resource address (the physical address used by
297 * the CPU. Converting that resource address back to a bus address
298 * should yield the original BAR value:
299 *
300 * resource_to_bus(bus_to_resource(A)) == A
301 *
302 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
303 * be claimed by the device.
304 */
305 if (inverted_region.start != region.start) {
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800306 res->flags |= IORESOURCE_UNSET;
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800307 res->start = 0;
Bjorn Helgaas26370fc2014-04-14 15:26:50 -0600308 res->end = region.end - region.start;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600309 pci_info(dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
Myron Stowef795d862014-10-30 11:54:43 -0600310 pos, (unsigned long long)region.start);
Kevin Haocf4d1cf2013-05-25 19:36:27 +0800311 }
Kevin Hao96ddef22013-05-25 19:36:26 +0800312
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600313 goto out;
314
315
316fail:
317 res->flags = 0;
318out:
Bjorn Helgaas31e9dd22014-04-29 18:37:47 -0600319 if (res->flags)
Frederick Lawler7506dc72018-01-18 12:55:24 -0600320 pci_printk(KERN_DEBUG, dev, "reg 0x%x: %pR\n", pos, res);
Bjorn Helgaas0ff95142012-08-23 10:53:08 -0600321
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600322 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
Yinghai Lu07eddf32006-11-29 13:53:10 -0800323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
326{
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400327 unsigned int pos, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Prarit Bhargavaad67b432016-05-11 12:27:16 -0400329 if (dev->non_compliant_bars)
330 return;
331
KarimAllah Ahmedbf4447f2018-03-03 05:33:10 +0100332 /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */
333 if (dev->is_virtfn)
334 return;
335
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400336 for (pos = 0; pos < howmany; pos++) {
337 struct resource *res = &dev->resource[pos];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400339 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (rom) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400343 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dev->rom_base_reg = rom;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400345 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
Dan Williams92b19ff2015-08-10 23:07:06 -0400346 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400347 __pci_read_base(dev, pci_bar_mem32, res, rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350
Bill Pemberton15856ad2012-11-21 15:35:00 -0500351static void pci_read_bridge_io(struct pci_bus *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct pci_dev *dev = child->self;
354 u8 io_base_lo, io_limit_lo;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600355 unsigned long io_mask, io_granularity, base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700356 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600357 struct resource *res;
358
359 io_mask = PCI_IO_RANGE_MASK;
360 io_granularity = 0x1000;
361 if (dev->io_window_1k) {
362 /* Support 1K I/O space granularity */
363 io_mask = PCI_IO_1K_RANGE_MASK;
364 io_granularity = 0x400;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 res = child->resource[0];
368 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
369 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600370 base = (io_base_lo & io_mask) << 8;
371 limit = (io_limit_lo & io_mask) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
374 u16 io_base_hi, io_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
377 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600378 base |= ((unsigned long) io_base_hi << 16);
379 limit |= ((unsigned long) io_limit_hi << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600382 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700384 region.start = base;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600385 region.end = limit + io_granularity - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800386 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600387 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700389}
390
Bill Pemberton15856ad2012-11-21 15:35:00 -0500391static void pci_read_bridge_mmio(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700392{
393 struct pci_dev *dev = child->self;
394 u16 mem_base_lo, mem_limit_lo;
395 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700396 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700397 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 res = child->resource[1];
400 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
401 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600402 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
403 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600404 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700406 region.start = base;
407 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800408 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600409 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700411}
412
Bill Pemberton15856ad2012-11-21 15:35:00 -0500413static void pci_read_bridge_mmio_pref(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700414{
415 struct pci_dev *dev = child->self;
416 u16 mem_base_lo, mem_limit_lo;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700417 u64 base64, limit64;
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700418 pci_bus_addr_t base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700419 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700420 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 res = child->resource[2];
423 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
424 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700425 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
426 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
429 u32 mem_base_hi, mem_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
432 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
433
434 /*
435 * Some bridges set the base > limit by default, and some
436 * (broken) BIOSes do not initialize them. If we find
437 * this, just assume they are not being used.
438 */
439 if (mem_base_hi <= mem_limit_hi) {
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700440 base64 |= (u64) mem_base_hi << 32;
441 limit64 |= (u64) mem_limit_hi << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700444
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700445 base = (pci_bus_addr_t) base64;
446 limit = (pci_bus_addr_t) limit64;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700447
448 if (base != base64) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600449 pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700450 (unsigned long long) base64);
451 return;
452 }
453
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600454 if (base <= limit) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700455 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
456 IORESOURCE_MEM | IORESOURCE_PREFETCH;
457 if (res->flags & PCI_PREF_RANGE_TYPE_64)
458 res->flags |= IORESOURCE_MEM_64;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700459 region.start = base;
460 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800461 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600462 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
Bill Pemberton15856ad2012-11-21 15:35:00 -0500466void pci_read_bridge_bases(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700467{
468 struct pci_dev *dev = child->self;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700469 struct resource *res;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700470 int i;
471
472 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
473 return;
474
Frederick Lawler7506dc72018-01-18 12:55:24 -0600475 pci_info(dev, "PCI bridge to %pR%s\n",
Yinghai Lub918c622012-05-17 18:51:11 -0700476 &child->busn_res,
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700477 dev->transparent ? " (subtractive decode)" : "");
478
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700479 pci_bus_remove_resources(child);
480 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
481 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
482
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700483 pci_read_bridge_io(child);
484 pci_read_bridge_mmio(child);
485 pci_read_bridge_mmio_pref(child);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700486
487 if (dev->transparent) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700488 pci_bus_for_each_resource(child->parent, res, i) {
Bjorn Helgaasd739a092014-04-14 16:10:54 -0600489 if (res && res->flags) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700490 pci_bus_add_resource(child, res,
491 PCI_SUBTRACTIVE_DECODE);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600492 pci_printk(KERN_DEBUG, dev,
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700493 " bridge window %pR (subtractive decode)\n",
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700494 res);
495 }
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700496 }
497 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700498}
499
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100500static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct pci_bus *b;
503
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100504 b = kzalloc(sizeof(*b), GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600505 if (!b)
506 return NULL;
507
508 INIT_LIST_HEAD(&b->node);
509 INIT_LIST_HEAD(&b->children);
510 INIT_LIST_HEAD(&b->devices);
511 INIT_LIST_HEAD(&b->slots);
512 INIT_LIST_HEAD(&b->resources);
513 b->max_bus_speed = PCI_SPEED_UNKNOWN;
514 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100515#ifdef CONFIG_PCI_DOMAINS_GENERIC
516 if (parent)
517 b->domain_nr = parent->domain_nr;
518#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return b;
520}
521
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500522static void devm_pci_release_host_bridge_dev(struct device *dev)
Jiang Liu70efde22013-06-07 16:16:51 -0600523{
524 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
525
526 if (bridge->release_fn)
527 bridge->release_fn(bridge);
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500528}
Jiang Liu70efde22013-06-07 16:16:51 -0600529
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500530static void pci_release_host_bridge_dev(struct device *dev)
531{
532 devm_pci_release_host_bridge_dev(dev);
533 pci_free_host_bridge(to_pci_host_bridge(dev));
Jiang Liu70efde22013-06-07 16:16:51 -0600534}
535
Thierry Redinga52d1442016-11-25 11:57:11 +0100536struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
Yinghai Lu7b543662012-04-02 18:31:53 -0700537{
538 struct pci_host_bridge *bridge;
539
Thierry Reding59094062016-11-25 11:57:10 +0100540 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600541 if (!bridge)
542 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -0700543
Bjorn Helgaas05013482013-06-05 14:22:11 -0600544 INIT_LIST_HEAD(&bridge->windows);
Lorenzo Pieralisia1c00502017-06-28 15:13:52 -0500545 bridge->dev.release = pci_release_host_bridge_dev;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100546
Yinghai Lu7b543662012-04-02 18:31:53 -0700547 return bridge;
548}
Thierry Redinga52d1442016-11-25 11:57:11 +0100549EXPORT_SYMBOL(pci_alloc_host_bridge);
Yinghai Lu7b543662012-04-02 18:31:53 -0700550
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500551struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
552 size_t priv)
553{
554 struct pci_host_bridge *bridge;
555
556 bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
557 if (!bridge)
558 return NULL;
559
560 INIT_LIST_HEAD(&bridge->windows);
561 bridge->dev.release = devm_pci_release_host_bridge_dev;
562
563 return bridge;
564}
565EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
566
Lorenzo Pieralisidff79b92017-06-28 15:13:52 -0500567void pci_free_host_bridge(struct pci_host_bridge *bridge)
568{
569 pci_free_resource_list(&bridge->windows);
570
571 kfree(bridge);
572}
573EXPORT_SYMBOL(pci_free_host_bridge);
574
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700575static const unsigned char pcix_bus_speed[] = {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500576 PCI_SPEED_UNKNOWN, /* 0 */
577 PCI_SPEED_66MHz_PCIX, /* 1 */
578 PCI_SPEED_100MHz_PCIX, /* 2 */
579 PCI_SPEED_133MHz_PCIX, /* 3 */
580 PCI_SPEED_UNKNOWN, /* 4 */
581 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
582 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
583 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
584 PCI_SPEED_UNKNOWN, /* 8 */
585 PCI_SPEED_66MHz_PCIX_266, /* 9 */
586 PCI_SPEED_100MHz_PCIX_266, /* A */
587 PCI_SPEED_133MHz_PCIX_266, /* B */
588 PCI_SPEED_UNKNOWN, /* C */
589 PCI_SPEED_66MHz_PCIX_533, /* D */
590 PCI_SPEED_100MHz_PCIX_533, /* E */
591 PCI_SPEED_133MHz_PCIX_533 /* F */
592};
593
Jacob Keller343e51a2013-07-31 06:53:16 +0000594const unsigned char pcie_link_speed[] = {
Matthew Wilcox3749c512009-12-13 08:11:32 -0500595 PCI_SPEED_UNKNOWN, /* 0 */
596 PCIE_SPEED_2_5GT, /* 1 */
597 PCIE_SPEED_5_0GT, /* 2 */
Matthew Wilcox9dfd97f2009-12-13 08:11:35 -0500598 PCIE_SPEED_8_0GT, /* 3 */
Matthew Wilcox3749c512009-12-13 08:11:32 -0500599 PCI_SPEED_UNKNOWN, /* 4 */
600 PCI_SPEED_UNKNOWN, /* 5 */
601 PCI_SPEED_UNKNOWN, /* 6 */
602 PCI_SPEED_UNKNOWN, /* 7 */
603 PCI_SPEED_UNKNOWN, /* 8 */
604 PCI_SPEED_UNKNOWN, /* 9 */
605 PCI_SPEED_UNKNOWN, /* A */
606 PCI_SPEED_UNKNOWN, /* B */
607 PCI_SPEED_UNKNOWN, /* C */
608 PCI_SPEED_UNKNOWN, /* D */
609 PCI_SPEED_UNKNOWN, /* E */
610 PCI_SPEED_UNKNOWN /* F */
611};
612
613void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
614{
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700615 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
Matthew Wilcox3749c512009-12-13 08:11:32 -0500616}
617EXPORT_SYMBOL_GPL(pcie_update_link_speed);
618
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500619static unsigned char agp_speeds[] = {
620 AGP_UNKNOWN,
621 AGP_1X,
622 AGP_2X,
623 AGP_4X,
624 AGP_8X
625};
626
627static enum pci_bus_speed agp_speed(int agp3, int agpstat)
628{
629 int index = 0;
630
631 if (agpstat & 4)
632 index = 3;
633 else if (agpstat & 2)
634 index = 2;
635 else if (agpstat & 1)
636 index = 1;
637 else
638 goto out;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700639
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500640 if (agp3) {
641 index += 2;
642 if (index == 5)
643 index = 0;
644 }
645
646 out:
647 return agp_speeds[index];
648}
649
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500650static void pci_set_bus_speed(struct pci_bus *bus)
651{
652 struct pci_dev *bridge = bus->self;
653 int pos;
654
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500655 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
656 if (!pos)
657 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
658 if (pos) {
659 u32 agpstat, agpcmd;
660
661 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
662 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
663
664 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
665 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
666 }
667
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500668 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
669 if (pos) {
670 u16 status;
671 enum pci_bus_speed max;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500672
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700673 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
674 &status);
675
676 if (status & PCI_X_SSTATUS_533MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500677 max = PCI_SPEED_133MHz_PCIX_533;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700678 } else if (status & PCI_X_SSTATUS_266MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500679 max = PCI_SPEED_133MHz_PCIX_266;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700680 } else if (status & PCI_X_SSTATUS_133MHZ) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400681 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500682 max = PCI_SPEED_133MHz_PCIX_ECC;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400683 else
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500684 max = PCI_SPEED_133MHz_PCIX;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500685 } else {
686 max = PCI_SPEED_66MHz_PCIX;
687 }
688
689 bus->max_bus_speed = max;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700690 bus->cur_bus_speed = pcix_bus_speed[
691 (status & PCI_X_SSTATUS_FREQ) >> 6];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500692
693 return;
694 }
695
Yijing Wangfdfe1512013-09-05 15:55:29 +0800696 if (pci_is_pcie(bridge)) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500697 u32 linkcap;
698 u16 linksta;
699
Jiang Liu59875ae2012-07-24 17:20:06 +0800700 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700701 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500702
Jiang Liu59875ae2012-07-24 17:20:06 +0800703 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500704 pcie_update_link_speed(bus, linksta);
705 }
706}
707
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100708static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
709{
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100710 struct irq_domain *d;
711
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100712 /*
713 * Any firmware interface that can resolve the msi_domain
714 * should be called from here.
715 */
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100716 d = pci_host_bridge_of_msi_domain(bus);
Suravee Suthikulpanit471036b2015-12-10 08:55:27 -0800717 if (!d)
718 d = pci_host_bridge_acpi_msi_domain(bus);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100719
Jake Oshins788858e2016-02-16 21:56:22 +0000720#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
721 /*
722 * If no IRQ domain was found via the OF tree, try looking it up
723 * directly through the fwnode_handle.
724 */
725 if (!d) {
726 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
727
728 if (fwnode)
729 d = irq_find_matching_fwnode(fwnode,
730 DOMAIN_BUS_PCI_MSI);
731 }
732#endif
733
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100734 return d;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100735}
736
737static void pci_set_bus_msi_domain(struct pci_bus *bus)
738{
739 struct irq_domain *d;
Alex Williamson38ea72b2015-09-18 15:08:54 -0600740 struct pci_bus *b;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100741
742 /*
Alex Williamson38ea72b2015-09-18 15:08:54 -0600743 * The bus can be a root bus, a subordinate bus, or a virtual bus
744 * created by an SR-IOV device. Walk up to the first bridge device
745 * found or derive the domain from the host bridge.
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100746 */
Alex Williamson38ea72b2015-09-18 15:08:54 -0600747 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
748 if (b->self)
749 d = dev_get_msi_domain(&b->self->dev);
750 }
751
752 if (!d)
753 d = pci_host_bridge_msi_domain(b);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100754
755 dev_set_msi_domain(&bus->dev, d);
756}
757
Lorenzo Pieralisicea9bc02017-06-28 15:13:55 -0500758static int pci_register_host_bridge(struct pci_host_bridge *bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100759{
760 struct device *parent = bridge->dev.parent;
761 struct resource_entry *window, *n;
762 struct pci_bus *bus, *b;
763 resource_size_t offset;
764 LIST_HEAD(resources);
765 struct resource *res;
766 char addr[64], *fmt;
767 const char *name;
768 int err;
769
770 bus = pci_alloc_bus(NULL);
771 if (!bus)
772 return -ENOMEM;
773
774 bridge->bus = bus;
775
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600776 /* Temporarily move resources off the list */
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100777 list_splice_init(&bridge->windows, &resources);
778 bus->sysdata = bridge->sysdata;
779 bus->msi = bridge->msi;
780 bus->ops = bridge->ops;
781 bus->number = bus->busn_res.start = bridge->busnr;
782#ifdef CONFIG_PCI_DOMAINS_GENERIC
783 bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
784#endif
785
786 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
787 if (b) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600788 /* Ignore it if we already got here via a different bridge */
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100789 dev_dbg(&b->dev, "bus already known\n");
790 err = -EEXIST;
791 goto free;
792 }
793
794 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
795 bridge->busnr);
796
797 err = pcibios_root_bridge_prepare(bridge);
798 if (err)
799 goto free;
800
801 err = device_register(&bridge->dev);
802 if (err)
803 put_device(&bridge->dev);
804
805 bus->bridge = get_device(&bridge->dev);
806 device_enable_async_suspend(bus->bridge);
807 pci_set_bus_of_node(bus);
808 pci_set_bus_msi_domain(bus);
809
810 if (!parent)
811 set_dev_node(bus->bridge, pcibus_to_node(bus));
812
813 bus->dev.class = &pcibus_class;
814 bus->dev.parent = bus->bridge;
815
816 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
817 name = dev_name(&bus->dev);
818
819 err = device_register(&bus->dev);
820 if (err)
821 goto unregister;
822
823 pcibios_add_bus(bus);
824
825 /* Create legacy_io and legacy_mem files for this bus */
826 pci_create_legacy_files(bus);
827
828 if (parent)
829 dev_info(parent, "PCI host bridge to bus %s\n", name);
830 else
831 pr_info("PCI host bridge to bus %s\n", name);
832
833 /* Add initial resources to the bus */
834 resource_list_for_each_entry_safe(window, n, &resources) {
835 list_move_tail(&window->node, &bridge->windows);
836 offset = window->offset;
837 res = window->res;
838
839 if (res->flags & IORESOURCE_BUS)
840 pci_bus_insert_busn_res(bus, bus->number, res->end);
841 else
842 pci_bus_add_resource(bus, res, 0);
843
844 if (offset) {
845 if (resource_type(res) == IORESOURCE_IO)
846 fmt = " (bus address [%#06llx-%#06llx])";
847 else
848 fmt = " (bus address [%#010llx-%#010llx])";
849
850 snprintf(addr, sizeof(addr), fmt,
851 (unsigned long long)(res->start - offset),
852 (unsigned long long)(res->end - offset));
853 } else
854 addr[0] = '\0';
855
856 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
857 }
858
859 down_write(&pci_bus_sem);
860 list_add_tail(&bus->node, &pci_root_buses);
861 up_write(&pci_bus_sem);
862
863 return 0;
864
865unregister:
866 put_device(&bridge->dev);
867 device_unregister(&bridge->dev);
868
869free:
870 kfree(bus);
871 return err;
872}
873
Adrian Bunkcbd4e052008-04-18 13:53:55 -0700874static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
875 struct pci_dev *bridge, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 struct pci_bus *child;
878 int i;
Yinghai Lu4f535092013-01-21 13:20:52 -0800879 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600881 /* Allocate a new bus and inherit stuff from the parent */
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100882 child = pci_alloc_bus(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!child)
884 return NULL;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 child->parent = parent;
887 child->ops = parent->ops;
Thierry Reding0cbdcfc2013-08-09 22:27:08 +0200888 child->msi = parent->msi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 child->sysdata = parent->sysdata;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200890 child->bus_flags = parent->bus_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600892 /*
893 * Initialize some portions of the bus device, but don't register
894 * it now as the parent is not properly set up yet.
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400895 */
896 child->dev.class = &pcibus_class;
Kay Sievers1a927132008-10-30 02:17:49 +0100897 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600899 /* Set up the primary, secondary and subordinate bus numbers */
Yinghai Lub918c622012-05-17 18:51:11 -0700900 child->number = child->busn_res.start = busnr;
901 child->primary = parent->busn_res.start;
902 child->busn_res.end = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Yinghai Lu4f535092013-01-21 13:20:52 -0800904 if (!bridge) {
905 child->dev.parent = parent->bridge;
906 goto add_dev;
907 }
Yu Zhao3789fa82008-11-22 02:41:07 +0800908
909 child->self = bridge;
910 child->bridge = get_device(&bridge->dev);
Yinghai Lu4f535092013-01-21 13:20:52 -0800911 child->dev.parent = child->bridge;
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000912 pci_set_bus_of_node(child);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500913 pci_set_bus_speed(child);
914
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600915 /* Set up default resource pointers and names */
Yu Zhaofde09c62008-11-22 02:39:32 +0800916 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
918 child->resource[i]->name = child->name;
919 }
920 bridge->subordinate = child;
921
Yinghai Lu4f535092013-01-21 13:20:52 -0800922add_dev:
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100923 pci_set_bus_msi_domain(child);
Yinghai Lu4f535092013-01-21 13:20:52 -0800924 ret = device_register(&child->dev);
925 WARN_ON(ret < 0);
926
Jiang Liu10a95742013-04-12 05:44:20 +0000927 pcibios_add_bus(child);
928
Thierry Reding057bd2e2016-02-09 15:30:47 +0100929 if (child->ops->add_bus) {
930 ret = child->ops->add_bus(child);
931 if (WARN_ON(ret < 0))
932 dev_err(&child->dev, "failed to add bus: %d\n", ret);
933 }
934
Yinghai Lu4f535092013-01-21 13:20:52 -0800935 /* Create legacy_io and legacy_mem files for this bus */
936 pci_create_legacy_files(child);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return child;
939}
940
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400941struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
942 int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct pci_bus *child;
945
946 child = pci_alloc_child_bus(parent, dev, busnr);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700947 if (child) {
Zhang Yanmind71374d2006-06-02 12:35:43 +0800948 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 list_add_tail(&child->node, &parent->children);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800950 up_write(&pci_bus_sem);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return child;
953}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600954EXPORT_SYMBOL(pci_add_new_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Rajat Jainf3dbd802014-09-02 16:26:00 -0700956static void pci_enable_crs(struct pci_dev *pdev)
957{
958 u16 root_cap = 0;
959
960 /* Enable CRS Software Visibility if supported */
961 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
962 if (root_cap & PCI_EXP_RTCAP_CRSVIS)
963 pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
964 PCI_EXP_RTCTL_CRSSVE);
965}
966
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300967static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
968 unsigned int available_buses);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/*
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300971 * pci_scan_bridge_extend() - Scan buses behind a bridge
972 * @bus: Parent bus the bridge is on
973 * @dev: Bridge itself
974 * @max: Starting subordinate number of buses behind this bridge
975 * @available_buses: Total number of buses available for this bridge and
976 * the devices below. After the minimal bus space has
977 * been allocated the remaining buses will be
978 * distributed equally between hotplug-capable bridges.
979 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
980 * that need to be reconfigured.
981 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * If it's a bridge, configure it and scan the bus behind it.
983 * For CardBus bridges, we don't scan behind as the devices will
984 * be handled by the bridge driver itself.
985 *
986 * We need to process bridges in two passes -- first we scan those
987 * already configured by the BIOS and after we are done with all of
988 * them, we proceed to assigning numbers to the remaining buses in
989 * order to avoid overlaps between old and new bus numbers.
990 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300991static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
992 int max, unsigned int available_buses,
993 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct pci_bus *child;
996 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
Dominik Brodowski49887942005-12-08 16:53:12 +0100997 u32 buses, i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 u16 bctl;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600999 u8 primary, secondary, subordinate;
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001000 int broken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mika Westerbergd963f652016-06-02 11:17:13 +03001002 /*
1003 * Make sure the bridge is powered on to be able to access config
1004 * space of devices below it.
1005 */
1006 pm_runtime_get_sync(&dev->dev);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001009 primary = buses & 0xFF;
1010 secondary = (buses >> 8) & 0xFF;
1011 subordinate = (buses >> 16) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Frederick Lawler7506dc72018-01-18 12:55:24 -06001013 pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001014 secondary, subordinate, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001016 if (!primary && (primary != bus->number) && secondary && subordinate) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001017 pci_warn(dev, "Primary bus is hard wired to 0\n");
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001018 primary = bus->number;
1019 }
1020
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001021 /* Check if setup is sensible at all */
1022 if (!pass &&
Yinghai Lu1965f662012-09-10 17:19:33 -07001023 (primary != bus->number || secondary <= bus->number ||
Bjorn Helgaas12d87062014-09-19 11:08:40 -06001024 secondary > subordinate)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001025 pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
Yinghai Lu1965f662012-09-10 17:19:33 -07001026 secondary, subordinate);
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001027 broken = 1;
1028 }
1029
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001030 /*
1031 * Disable Master-Abort Mode during probing to avoid reporting of
1032 * bus errors in some architectures.
1033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1035 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1036 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1037
Rajat Jainf3dbd802014-09-02 16:26:00 -07001038 pci_enable_crs(dev);
1039
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001040 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1041 !is_cardbus && !broken) {
1042 unsigned int cmax;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001045 * Bus already configured by firmware, process it in the
1046 * first pass and just note the configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
1048 if (pass)
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001052 * The bus might already exist for two reasons: Either we
1053 * are rescanning the bus or the bus is reachable through
1054 * more than one bridge. The second case can happen with
1055 * the i450NX chipset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001057 child = pci_find_bus(pci_domain_nr(bus), secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001058 if (!child) {
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001059 child = pci_add_new_bus(bus, dev, secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001060 if (!child)
1061 goto out;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001062 child->primary = primary;
Yinghai Lubc76b732012-05-17 18:51:13 -07001063 pci_bus_insert_busn_res(child, secondary, subordinate);
Alex Chiang74710de2009-03-20 14:56:10 -06001064 child->bridge_ctl = bctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 cmax = pci_scan_child_bus(child);
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001068 if (cmax > subordinate)
Frederick Lawler7506dc72018-01-18 12:55:24 -06001069 pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001070 subordinate, cmax);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001071
1072 /* Subordinate should equal child->busn_res.end */
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001073 if (subordinate > max)
1074 max = subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 } else {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /*
1078 * We need to assign a number to this bus which we always
1079 * do in the second pass.
1080 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001081 if (!pass) {
Andreas Noever619c8c32014-01-23 21:59:23 +01001082 if (pcibios_assign_all_busses() || broken || is_cardbus)
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001083
1084 /*
1085 * Temporarily disable forwarding of the
1086 * configuration cycles on all bridges in
1087 * this bus segment to avoid possible
1088 * conflicts in the second pass between two
1089 * bridges programmed with overlapping bus
1090 * ranges.
1091 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001092 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1093 buses & ~0xffffff);
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001094 goto out;
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* Clear errors */
1098 pci_write_config_word(dev, PCI_STATUS, 0xffff);
1099
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001100 /*
1101 * Prevent assigning a bus number that already exists.
1102 * This can happen when a bridge is hot-plugged, so in this
1103 * case we only re-scan this bus.
1104 */
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001105 child = pci_find_bus(pci_domain_nr(bus), max+1);
1106 if (!child) {
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001107 child = pci_add_new_bus(bus, dev, max+1);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001108 if (!child)
1109 goto out;
Mika Westerberga20c7f32017-10-13 21:35:43 +03001110 pci_bus_insert_busn_res(child, max+1,
1111 bus->busn_res.end);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001112 }
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001113 max++;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001114 if (available_buses)
1115 available_buses--;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 buses = (buses & 0xff000000)
1118 | ((unsigned int)(child->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -07001119 | ((unsigned int)(child->busn_res.start) << 8)
1120 | ((unsigned int)(child->busn_res.end) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /*
1123 * yenta.c forces a secondary latency timer of 176.
1124 * Copy that behaviour here.
1125 */
1126 if (is_cardbus) {
1127 buses &= ~0xff000000;
1128 buses |= CARDBUS_LATENCY_TIMER << 24;
1129 }
Jesper Juhl7c867c82011-01-24 21:14:33 +01001130
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001131 /* We need to blast all three values with a single write */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1133
1134 if (!is_cardbus) {
Gary Hade11949252007-10-08 16:24:16 -07001135 child->bridge_ctl = bctl;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001136 max = pci_scan_child_bus_extend(child, available_buses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 } else {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001140 * For CardBus bridges, we leave 4 bus numbers as
1141 * cards with a PCI-to-PCI bridge can be inserted
1142 * later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001144 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001145 struct pci_bus *parent = bus;
Rajesh Shahcc574502005-04-28 00:25:47 -07001146 if (pci_find_bus(pci_domain_nr(bus),
1147 max+i+1))
1148 break;
Dominik Brodowski49887942005-12-08 16:53:12 +01001149 while (parent->parent) {
1150 if ((!pcibios_assign_all_busses()) &&
Yinghai Lub918c622012-05-17 18:51:11 -07001151 (parent->busn_res.end > max) &&
1152 (parent->busn_res.end <= max+i)) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001153 j = 1;
1154 }
1155 parent = parent->parent;
1156 }
1157 if (j) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001158
Dominik Brodowski49887942005-12-08 16:53:12 +01001159 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001160 * Often, there are two CardBus
1161 * bridges -- try to leave one
1162 * valid bus number for each one.
Dominik Brodowski49887942005-12-08 16:53:12 +01001163 */
1164 i /= 2;
1165 break;
1166 }
1167 }
Rajesh Shahcc574502005-04-28 00:25:47 -07001168 max += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001170
1171 /* Set subordinate bus number to its real value */
Yinghai Lubc76b732012-05-17 18:51:13 -07001172 pci_bus_update_busn_res_end(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1174 }
1175
Gary Hadecb3576f2008-02-08 14:00:52 -08001176 sprintf(child->name,
1177 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1178 pci_domain_nr(bus), child->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Bernhard Kaindld55bef512007-07-30 20:35:13 +02001180 /* Has only triggered on CardBus, fixup is in yenta_socket */
Dominik Brodowski49887942005-12-08 16:53:12 +01001181 while (bus->parent) {
Yinghai Lub918c622012-05-17 18:51:11 -07001182 if ((child->busn_res.end > bus->busn_res.end) ||
1183 (child->number > bus->busn_res.end) ||
Dominik Brodowski49887942005-12-08 16:53:12 +01001184 (child->number < bus->number) ||
Yinghai Lub918c622012-05-17 18:51:11 -07001185 (child->busn_res.end < bus->number)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001186 dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
Yinghai Lub918c622012-05-17 18:51:11 -07001187 &child->busn_res,
1188 (bus->number > child->busn_res.end &&
1189 bus->busn_res.end < child->number) ?
Joe Perchesa6f29a92007-11-19 17:48:29 -08001190 "wholly" : "partially",
1191 bus->self->transparent ? " transparent" : "",
Bjorn Helgaas865df572009-11-04 10:32:57 -07001192 dev_name(&bus->dev),
Yinghai Lub918c622012-05-17 18:51:11 -07001193 &bus->busn_res);
Dominik Brodowski49887942005-12-08 16:53:12 +01001194 }
1195 bus = bus->parent;
1196 }
1197
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001198out:
1199 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1200
Mika Westerbergd963f652016-06-02 11:17:13 +03001201 pm_runtime_put(&dev->dev);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return max;
1204}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001205
1206/*
1207 * pci_scan_bridge() - Scan buses behind a bridge
1208 * @bus: Parent bus the bridge is on
1209 * @dev: Bridge itself
1210 * @max: Starting subordinate number of buses behind this bridge
1211 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1212 * that need to be reconfigured.
1213 *
1214 * If it's a bridge, configure it and scan the bus behind it.
1215 * For CardBus bridges, we don't scan behind as the devices will
1216 * be handled by the bridge driver itself.
1217 *
1218 * We need to process bridges in two passes -- first we scan those
1219 * already configured by the BIOS and after we are done with all of
1220 * them, we proceed to assigning numbers to the remaining buses in
1221 * order to avoid overlaps between old and new bus numbers.
1222 */
1223int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1224{
1225 return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1226}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001227EXPORT_SYMBOL(pci_scan_bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229/*
1230 * Read interrupt line and base address registers.
1231 * The architecture-dependent code can tweak these, of course.
1232 */
1233static void pci_read_irq(struct pci_dev *dev)
1234{
1235 unsigned char irq;
1236
KarimAllah Ahmedbe20f6b2018-01-17 19:30:29 +01001237 /* VFs are not allowed to use INTx, so skip the config reads */
1238 if (dev->is_virtfn) {
1239 dev->pin = 0;
1240 dev->irq = 0;
1241 return;
1242 }
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
Kristen Accardiffeff782005-11-02 16:24:32 -08001245 dev->pin = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (irq)
1247 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1248 dev->irq = irq;
1249}
1250
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001251void set_pcie_port_type(struct pci_dev *pdev)
Yu Zhao480b93b2009-03-20 11:25:14 +08001252{
1253 int pos;
1254 u16 reg16;
Yijing Wangd0751b92015-05-21 15:05:02 +08001255 int type;
1256 struct pci_dev *parent;
Yu Zhao480b93b2009-03-20 11:25:14 +08001257
1258 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1259 if (!pos)
1260 return;
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001261
Kenji Kaneshige0efea002009-11-05 12:05:11 +09001262 pdev->pcie_cap = pos;
Yu Zhao480b93b2009-03-20 11:25:14 +08001263 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
Yijing Wang786e2282012-07-24 17:20:02 +08001264 pdev->pcie_flags_reg = reg16;
Jon Masonb03e7492011-07-20 15:20:54 -05001265 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
1266 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
Yijing Wangd0751b92015-05-21 15:05:02 +08001267
1268 /*
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001269 * A Root Port or a PCI-to-PCIe bridge is always the upstream end
1270 * of a Link. No PCIe component has two Links. Two Links are
1271 * connected by a Switch that has a Port on each Link and internal
1272 * logic to connect the two Ports.
Yijing Wangd0751b92015-05-21 15:05:02 +08001273 */
1274 type = pci_pcie_type(pdev);
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001275 if (type == PCI_EXP_TYPE_ROOT_PORT ||
1276 type == PCI_EXP_TYPE_PCIE_BRIDGE)
Yijing Wangd0751b92015-05-21 15:05:02 +08001277 pdev->has_secondary_link = 1;
1278 else if (type == PCI_EXP_TYPE_UPSTREAM ||
1279 type == PCI_EXP_TYPE_DOWNSTREAM) {
1280 parent = pci_upstream_bridge(pdev);
Yijing Wangb35b1df2015-08-17 18:47:58 +08001281
1282 /*
1283 * Usually there's an upstream device (Root Port or Switch
1284 * Downstream Port), but we can't assume one exists.
1285 */
1286 if (parent && !parent->has_secondary_link)
Yijing Wangd0751b92015-05-21 15:05:02 +08001287 pdev->has_secondary_link = 1;
1288 }
Yu Zhao480b93b2009-03-20 11:25:14 +08001289}
1290
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001291void set_pcie_hotplug_bridge(struct pci_dev *pdev)
Eric W. Biederman28760482009-09-09 14:09:24 -07001292{
Eric W. Biederman28760482009-09-09 14:09:24 -07001293 u32 reg32;
1294
Jiang Liu59875ae2012-07-24 17:20:06 +08001295 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
Eric W. Biederman28760482009-09-09 14:09:24 -07001296 if (reg32 & PCI_EXP_SLTCAP_HPC)
1297 pdev->is_hotplug_bridge = 1;
1298}
1299
Lukas Wunner8531e282017-03-10 21:23:45 +01001300static void set_pcie_thunderbolt(struct pci_dev *dev)
1301{
1302 int vsec = 0;
1303 u32 header;
1304
1305 while ((vsec = pci_find_next_ext_capability(dev, vsec,
1306 PCI_EXT_CAP_ID_VNDR))) {
1307 pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
1308
1309 /* Is the device part of a Thunderbolt controller? */
1310 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
1311 PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
1312 dev->is_thunderbolt = 1;
1313 return;
1314 }
1315 }
1316}
1317
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001318/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001319 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
Alex Williamson78916b02014-05-05 14:20:51 -06001320 * @dev: PCI device
1321 *
1322 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1323 * when forwarding a type1 configuration request the bridge must check that
1324 * the extended register address field is zero. The bridge is not permitted
1325 * to forward the transactions and must handle it as an Unsupported Request.
1326 * Some bridges do not follow this rule and simply drop the extended register
1327 * bits, resulting in the standard config space being aliased, every 256
1328 * bytes across the entire configuration space. Test for this condition by
1329 * comparing the first dword of each potential alias to the vendor/device ID.
1330 * Known offenders:
1331 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1332 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1333 */
1334static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1335{
1336#ifdef CONFIG_PCI_QUIRKS
1337 int pos;
1338 u32 header, tmp;
1339
1340 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1341
1342 for (pos = PCI_CFG_SPACE_SIZE;
1343 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1344 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1345 || header != tmp)
1346 return false;
1347 }
1348
1349 return true;
1350#else
1351 return false;
1352#endif
1353}
1354
1355/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001356 * pci_cfg_space_size - Get the configuration space size of the PCI device
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001357 * @dev: PCI device
1358 *
1359 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1360 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1361 * access it. Maybe we don't have a way to generate extended config space
1362 * accesses, or the device is behind a reverse Express bridge. So we try
1363 * reading the dword at 0x100 which must either be 0 or a valid extended
1364 * capability header.
1365 */
1366static int pci_cfg_space_size_ext(struct pci_dev *dev)
1367{
1368 u32 status;
1369 int pos = PCI_CFG_SPACE_SIZE;
1370
1371 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001372 return PCI_CFG_SPACE_SIZE;
Alex Williamson78916b02014-05-05 14:20:51 -06001373 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001374 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001375
1376 return PCI_CFG_SPACE_EXP_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001377}
1378
1379int pci_cfg_space_size(struct pci_dev *dev)
1380{
1381 int pos;
1382 u32 status;
1383 u16 class;
1384
1385 class = dev->class >> 8;
1386 if (class == PCI_CLASS_BRIDGE_HOST)
1387 return pci_cfg_space_size_ext(dev);
1388
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001389 if (pci_is_pcie(dev))
1390 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001391
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001392 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1393 if (!pos)
1394 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001395
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001396 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1397 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1398 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001399
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001400 return PCI_CFG_SPACE_SIZE;
1401}
1402
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +01001403static u32 pci_class(struct pci_dev *dev)
1404{
1405 u32 class;
1406
1407#ifdef CONFIG_PCI_IOV
1408 if (dev->is_virtfn)
1409 return dev->physfn->sriov->class;
1410#endif
1411 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
1412 return class;
1413}
1414
1415static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device)
1416{
1417#ifdef CONFIG_PCI_IOV
1418 if (dev->is_virtfn) {
1419 *vendor = dev->physfn->sriov->subsystem_vendor;
1420 *device = dev->physfn->sriov->subsystem_device;
1421 return;
1422 }
1423#endif
1424 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor);
1425 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device);
1426}
1427
1428static u8 pci_hdr_type(struct pci_dev *dev)
1429{
1430 u8 hdr_type;
1431
1432#ifdef CONFIG_PCI_IOV
1433 if (dev->is_virtfn)
1434 return dev->physfn->sriov->hdr_type;
1435#endif
1436 pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
1437 return hdr_type;
1438}
1439
Bartlomiej Zolnierkiewicz01abc2a2007-04-23 23:19:36 +02001440#define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
Randy Dunlap76e6a1d2006-12-29 16:47:29 -08001441
Guilherme G. Piccolie80e7edc2015-10-21 12:17:35 -02001442static void pci_msi_setup_pci_dev(struct pci_dev *dev)
Michael S. Tsirkin18516172015-05-07 09:52:21 -05001443{
1444 /*
1445 * Disable the MSI hardware to avoid screaming interrupts
1446 * during boot. This is the power on reset default so
1447 * usually this should be a noop.
1448 */
1449 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1450 if (dev->msi_cap)
1451 pci_msi_set_enable(dev, 0);
1452
1453 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1454 if (dev->msix_cap)
1455 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001459 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
Piotr Gregor99b3c582017-05-26 22:02:25 +01001460 * @dev: PCI device
1461 *
1462 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
1463 * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1464 */
1465static int pci_intx_mask_broken(struct pci_dev *dev)
1466{
1467 u16 orig, toggle, new;
1468
1469 pci_read_config_word(dev, PCI_COMMAND, &orig);
1470 toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1471 pci_write_config_word(dev, PCI_COMMAND, toggle);
1472 pci_read_config_word(dev, PCI_COMMAND, &new);
1473
1474 pci_write_config_word(dev, PCI_COMMAND, orig);
1475
1476 /*
1477 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1478 * r2.3, so strictly speaking, a device is not *broken* if it's not
1479 * writable. But we'll live with the misnomer for now.
1480 */
1481 if (new != toggle)
1482 return 1;
1483 return 0;
1484}
1485
1486/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001487 * pci_setup_device - Fill in class and map information of a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * @dev: the device structure to fill
1489 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001490 * Initialize the device structure with information about the device's
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001491 * vendor,class,memory and IO-space addresses, IRQ lines etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * Called at initialisation of the PCI subsystem and by CardBus services.
Yu Zhao480b93b2009-03-20 11:25:14 +08001493 * Returns 0 on success and negative if unknown type of device (not normal,
1494 * bridge or CardBus).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001496int pci_setup_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 u32 class;
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001499 u16 cmd;
Yu Zhao480b93b2009-03-20 11:25:14 +08001500 u8 hdr_type;
Gabe Blackbc577d22009-10-06 10:45:19 -05001501 int pos = 0;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001502 struct pci_bus_region region;
1503 struct resource *res;
Yu Zhao480b93b2009-03-20 11:25:14 +08001504
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +01001505 hdr_type = pci_hdr_type(dev);
Yu Zhao480b93b2009-03-20 11:25:14 +08001506
1507 dev->sysdata = dev->bus->sysdata;
1508 dev->dev.parent = dev->bus->bridge;
1509 dev->dev.bus = &pci_bus_type;
1510 dev->hdr_type = hdr_type & 0x7f;
1511 dev->multifunction = !!(hdr_type & 0x80);
Yu Zhao480b93b2009-03-20 11:25:14 +08001512 dev->error_state = pci_channel_io_normal;
1513 set_pcie_port_type(dev);
1514
Yijing Wang017ffe62015-07-17 17:16:32 +08001515 pci_dev_assign_slot(dev);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001516
1517 /*
1518 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1519 * set this higher, assuming the system even supports it.
1520 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001521 dev->dma_mask = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Greg Kroah-Hartmaneebfcfb2008-07-02 13:24:49 -07001523 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1524 dev->bus->number, PCI_SLOT(dev->devfn),
1525 PCI_FUNC(dev->devfn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +01001527 class = pci_class(dev);
1528
Auke Kokb8a3a522007-06-08 15:46:30 -07001529 dev->revision = class & 0xff;
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001530 dev->class = class >> 8; /* upper 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Frederick Lawler7506dc72018-01-18 12:55:24 -06001532 pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n",
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001533 dev->vendor, dev->device, dev->hdr_type, dev->class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001535 /* Need to have dev->class ready */
Yu Zhao853346e2009-03-21 22:05:11 +08001536 dev->cfg_size = pci_cfg_space_size(dev);
1537
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001538 /* Need to have dev->cfg_size ready */
Lukas Wunner8531e282017-03-10 21:23:45 +01001539 set_pcie_thunderbolt(dev);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /* "Unknown power state" */
Daniel Ritz3fe9d192005-08-17 15:32:19 -07001542 dev->current_state = PCI_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* Early fixups, before probing the BARs */
1545 pci_fixup_device(pci_fixup_early, dev);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001546
1547 /* Device class may be changed after fixup */
Yu Zhaof79b1b12009-05-28 00:25:05 +08001548 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001550 if (dev->non_compliant_bars) {
1551 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1552 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001553 pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001554 cmd &= ~PCI_COMMAND_IO;
1555 cmd &= ~PCI_COMMAND_MEMORY;
1556 pci_write_config_word(dev, PCI_COMMAND, cmd);
1557 }
1558 }
1559
Piotr Gregor99b3c582017-05-26 22:02:25 +01001560 dev->broken_intx_masking = pci_intx_mask_broken(dev);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 switch (dev->hdr_type) { /* header type */
1563 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1564 if (class == PCI_CLASS_BRIDGE_PCI)
1565 goto bad;
1566 pci_read_irq(dev);
1567 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +01001568
1569 pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device);
Alan Cox368c73d2006-10-04 00:41:26 +01001570
1571 /*
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001572 * Do the ugly legacy mode stuff here rather than broken chip
1573 * quirk code. Legacy mode ATA controllers have fixed
1574 * addresses. These are not always echoed in BAR0-3, and
1575 * BAR0-3 in a few cases contain junk!
Alan Cox368c73d2006-10-04 00:41:26 +01001576 */
1577 if (class == PCI_CLASS_STORAGE_IDE) {
1578 u8 progif;
1579 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1580 if ((progif & 1) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001581 region.start = 0x1F0;
1582 region.end = 0x1F7;
1583 res = &dev->resource[0];
1584 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001585 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001586 pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001587 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001588 region.start = 0x3F6;
1589 region.end = 0x3F6;
1590 res = &dev->resource[1];
1591 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001592 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001593 pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001594 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001595 }
1596 if ((progif & 4) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001597 region.start = 0x170;
1598 region.end = 0x177;
1599 res = &dev->resource[2];
1600 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001601 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001602 pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001603 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001604 region.start = 0x376;
1605 region.end = 0x376;
1606 res = &dev->resource[3];
1607 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001608 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001609 pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001610 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001611 }
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614
1615 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1616 if (class != PCI_CLASS_BRIDGE_PCI)
1617 goto bad;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001618
1619 /*
1620 * The PCI-to-PCI bridge spec requires that subtractive
1621 * decoding (i.e. transparent) bridge must have programming
1622 * interface code of 0x01.
1623 */
Kristen Accardi3efd2732005-11-02 16:55:49 -08001624 pci_read_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 dev->transparent = ((dev->class & 0xff) == 1);
1626 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
Eric W. Biederman28760482009-09-09 14:09:24 -07001627 set_pcie_hotplug_bridge(dev);
Gabe Blackbc577d22009-10-06 10:45:19 -05001628 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1629 if (pos) {
1630 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1631 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634
1635 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1636 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1637 goto bad;
1638 pci_read_irq(dev);
1639 pci_read_bases(dev, 1, 0);
1640 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1641 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1642 break;
1643
1644 default: /* unknown header */
Frederick Lawler7506dc72018-01-18 12:55:24 -06001645 pci_err(dev, "unknown header type %02x, ignoring device\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001646 dev->hdr_type);
Yu Zhao480b93b2009-03-20 11:25:14 +08001647 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 bad:
Frederick Lawler7506dc72018-01-18 12:55:24 -06001650 pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001651 dev->class, dev->hdr_type);
Bjorn Helgaas2b4aed12015-06-19 16:20:58 -05001652 dev->class = PCI_CLASS_NOT_DEFINED << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
1655 /* We found a fine healthy device, go go go... */
1656 return 0;
1657}
1658
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001659static void pci_configure_mps(struct pci_dev *dev)
1660{
1661 struct pci_dev *bridge = pci_upstream_bridge(dev);
Keith Busch27d868b2015-08-24 08:48:16 -05001662 int mps, p_mps, rc;
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001663
1664 if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
1665 return;
1666
1667 mps = pcie_get_mps(dev);
1668 p_mps = pcie_get_mps(bridge);
1669
1670 if (mps == p_mps)
1671 return;
1672
1673 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001674 pci_warn(dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001675 mps, pci_name(bridge), p_mps);
1676 return;
1677 }
Keith Busch27d868b2015-08-24 08:48:16 -05001678
1679 /*
1680 * Fancier MPS configuration is done later by
1681 * pcie_bus_configure_settings()
1682 */
1683 if (pcie_bus_config != PCIE_BUS_DEFAULT)
1684 return;
1685
1686 rc = pcie_set_mps(dev, p_mps);
1687 if (rc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001688 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
Keith Busch27d868b2015-08-24 08:48:16 -05001689 p_mps);
1690 return;
1691 }
1692
Frederick Lawler7506dc72018-01-18 12:55:24 -06001693 pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
Keith Busch27d868b2015-08-24 08:48:16 -05001694 p_mps, mps, 128 << dev->pcie_mpss);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001695}
1696
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001697static struct hpp_type0 pci_default_type0 = {
1698 .revision = 1,
1699 .cache_line_size = 8,
1700 .latency_timer = 0x40,
1701 .enable_serr = 0,
1702 .enable_perr = 0,
1703};
1704
1705static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1706{
1707 u16 pci_cmd, pci_bctl;
1708
Bjorn Helgaasc6285fc2014-08-29 18:10:19 -06001709 if (!hpp)
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001710 hpp = &pci_default_type0;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001711
1712 if (hpp->revision > 1) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001713 pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001714 hpp->revision);
1715 hpp = &pci_default_type0;
1716 }
1717
1718 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1719 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1720 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1721 if (hpp->enable_serr)
1722 pci_cmd |= PCI_COMMAND_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001723 if (hpp->enable_perr)
1724 pci_cmd |= PCI_COMMAND_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001725 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1726
1727 /* Program bridge control value */
1728 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1729 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1730 hpp->latency_timer);
1731 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1732 if (hpp->enable_serr)
1733 pci_bctl |= PCI_BRIDGE_CTL_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001734 if (hpp->enable_perr)
1735 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001736 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1737 }
1738}
1739
1740static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1741{
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001742 int pos;
1743
1744 if (!hpp)
1745 return;
1746
1747 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1748 if (!pos)
1749 return;
1750
Frederick Lawler7506dc72018-01-18 12:55:24 -06001751 pci_warn(dev, "PCI-X settings not supported\n");
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001752}
1753
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001754static bool pcie_root_rcb_set(struct pci_dev *dev)
1755{
1756 struct pci_dev *rp = pcie_find_root_port(dev);
1757 u16 lnkctl;
1758
1759 if (!rp)
1760 return false;
1761
1762 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
1763 if (lnkctl & PCI_EXP_LNKCTL_RCB)
1764 return true;
1765
1766 return false;
1767}
1768
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001769static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1770{
1771 int pos;
1772 u32 reg32;
1773
1774 if (!hpp)
1775 return;
1776
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001777 if (!pci_is_pcie(dev))
1778 return;
1779
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001780 if (hpp->revision > 1) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001781 pci_warn(dev, "PCIe settings rev %d not supported\n",
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001782 hpp->revision);
1783 return;
1784 }
1785
Bjorn Helgaas302328c2014-09-03 13:26:29 -06001786 /*
1787 * Don't allow _HPX to change MPS or MRRS settings. We manage
1788 * those to make sure they're consistent with the rest of the
1789 * platform.
1790 */
1791 hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
1792 PCI_EXP_DEVCTL_READRQ;
1793 hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
1794 PCI_EXP_DEVCTL_READRQ);
1795
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001796 /* Initialize Device Control Register */
1797 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1798 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1799
1800 /* Initialize Link Control Register */
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001801 if (pcie_cap_has_lnkctl(dev)) {
1802
1803 /*
1804 * If the Root Port supports Read Completion Boundary of
1805 * 128, set RCB to 128. Otherwise, clear it.
1806 */
1807 hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
1808 hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
1809 if (pcie_root_rcb_set(dev))
1810 hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
1811
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001812 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1813 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001814 }
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001815
1816 /* Find Advanced Error Reporting Enhanced Capability */
1817 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1818 if (!pos)
1819 return;
1820
1821 /* Initialize Uncorrectable Error Mask Register */
1822 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1823 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1824 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1825
1826 /* Initialize Uncorrectable Error Severity Register */
1827 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1828 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1829 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1830
1831 /* Initialize Correctable Error Mask Register */
1832 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1833 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1834 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1835
1836 /* Initialize Advanced Error Capabilities and Control Register */
1837 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1838 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001839
Bjorn Helgaas675734b2017-03-21 13:01:30 -05001840 /* Don't enable ECRC generation or checking if unsupported */
1841 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
1842 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
1843 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
1844 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001845 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1846
1847 /*
1848 * FIXME: The following two registers are not supported yet.
1849 *
1850 * o Secondary Uncorrectable Error Severity Register
1851 * o Secondary Uncorrectable Error Mask Register
1852 */
1853}
1854
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001855int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
Sinan Kaya60db3a42017-01-20 09:16:51 -05001856{
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001857 struct pci_host_bridge *host;
1858 u32 cap;
1859 u16 ctl;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001860 int ret;
1861
1862 if (!pci_is_pcie(dev))
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001863 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001864
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001865 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
Sinan Kaya60db3a42017-01-20 09:16:51 -05001866 if (ret)
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001867 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001868
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001869 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
1870 return 0;
1871
1872 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
1873 if (ret)
1874 return 0;
1875
1876 host = pci_find_host_bridge(dev->bus);
1877 if (!host)
1878 return 0;
1879
1880 /*
1881 * If some device in the hierarchy doesn't handle Extended Tags
1882 * correctly, make sure they're disabled.
1883 */
1884 if (host->no_ext_tags) {
1885 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001886 pci_info(dev, "disabling Extended Tags\n");
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001887 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1888 PCI_EXP_DEVCTL_EXT_TAG);
1889 }
1890 return 0;
1891 }
1892
1893 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001894 pci_info(dev, "enabling Extended Tags\n");
Sinan Kaya60db3a42017-01-20 09:16:51 -05001895 pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
1896 PCI_EXP_DEVCTL_EXT_TAG);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001897 }
1898 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001899}
1900
dingtianhonga99b6462017-08-15 11:23:23 +08001901/**
1902 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
1903 * @dev: PCI device to query
1904 *
1905 * Returns true if the device has enabled relaxed ordering attribute.
1906 */
1907bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
1908{
1909 u16 v;
1910
1911 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
1912
1913 return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
1914}
1915EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
1916
1917static void pci_configure_relaxed_ordering(struct pci_dev *dev)
1918{
1919 struct pci_dev *root;
1920
1921 /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
1922 if (dev->is_virtfn)
1923 return;
1924
1925 if (!pcie_relaxed_ordering_enabled(dev))
1926 return;
1927
1928 /*
1929 * For now, we only deal with Relaxed Ordering issues with Root
1930 * Ports. Peer-to-Peer DMA is another can of worms.
1931 */
1932 root = pci_find_pcie_root_port(dev);
1933 if (!root)
1934 return;
1935
1936 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
1937 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1938 PCI_EXP_DEVCTL_RELAX_EN);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001939 pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
dingtianhonga99b6462017-08-15 11:23:23 +08001940 }
1941}
1942
Bjorn Helgaasc46fd352017-11-28 16:43:50 -06001943static void pci_configure_ltr(struct pci_dev *dev)
1944{
1945#ifdef CONFIG_PCIEASPM
1946 u32 cap;
1947 struct pci_dev *bridge;
1948
1949 if (!pci_is_pcie(dev))
1950 return;
1951
1952 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
1953 if (!(cap & PCI_EXP_DEVCAP2_LTR))
1954 return;
1955
1956 /*
1957 * Software must not enable LTR in an Endpoint unless the Root
1958 * Complex and all intermediate Switches indicate support for LTR.
1959 * PCIe r3.1, sec 6.18.
1960 */
1961 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
1962 dev->ltr_path = 1;
1963 else {
1964 bridge = pci_upstream_bridge(dev);
1965 if (bridge && bridge->ltr_path)
1966 dev->ltr_path = 1;
1967 }
1968
1969 if (dev->ltr_path)
1970 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
1971 PCI_EXP_DEVCTL2_LTR_EN);
1972#endif
1973}
1974
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001975static void pci_configure_device(struct pci_dev *dev)
1976{
1977 struct hotplug_params hpp;
1978 int ret;
1979
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001980 pci_configure_mps(dev);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001981 pci_configure_extended_tags(dev, NULL);
dingtianhonga99b6462017-08-15 11:23:23 +08001982 pci_configure_relaxed_ordering(dev);
Bjorn Helgaasc46fd352017-11-28 16:43:50 -06001983 pci_configure_ltr(dev);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001984
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001985 memset(&hpp, 0, sizeof(hpp));
1986 ret = pci_get_hp_params(dev, &hpp);
1987 if (ret)
1988 return;
1989
1990 program_hpp_type2(dev, hpp.t2);
1991 program_hpp_type1(dev, hpp.t1);
1992 program_hpp_type0(dev, hpp.t0);
1993}
1994
Zhao, Yu201de562008-10-13 19:49:55 +08001995static void pci_release_capabilities(struct pci_dev *dev)
1996{
1997 pci_vpd_release(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08001998 pci_iov_release(dev);
Yinghai Luf7968412012-02-11 00:18:30 -08001999 pci_free_cap_save_buffers(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002003 * pci_release_dev - Free a PCI device structure when all users of it are
2004 * finished
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 * @dev: device that's been disconnected
2006 *
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002007 * Will be called only by the device core when all users of this PCI device are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * done.
2009 */
2010static void pci_release_dev(struct device *dev)
2011{
Rafael J. Wysocki04480092014-02-01 15:38:29 +01002012 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Rafael J. Wysocki04480092014-02-01 15:38:29 +01002014 pci_dev = to_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002015 pci_release_capabilities(pci_dev);
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10002016 pci_release_of_node(pci_dev);
Sebastian Ott6ae32c52013-06-04 19:18:14 +02002017 pcibios_release_device(pci_dev);
Gu Zheng8b1fce02013-05-25 21:48:31 +08002018 pci_bus_put(pci_dev->bus);
Alex Williamson782a9852014-05-20 08:53:21 -06002019 kfree(pci_dev->driver_override);
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01002020 kfree(pci_dev->dma_alias_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 kfree(pci_dev);
2022}
2023
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08002024struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
Michael Ellerman65891212007-04-05 17:19:08 +10002025{
2026 struct pci_dev *dev;
2027
2028 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
2029 if (!dev)
2030 return NULL;
2031
Michael Ellerman65891212007-04-05 17:19:08 +10002032 INIT_LIST_HEAD(&dev->bus_list);
Brian King88e7b162013-04-08 03:05:07 +00002033 dev->dev.type = &pci_dev_type;
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08002034 dev->bus = pci_bus_get(bus);
Michael Ellerman65891212007-04-05 17:19:08 +10002035
2036 return dev;
2037}
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08002038EXPORT_SYMBOL(pci_alloc_dev);
2039
Sinan Kaya62bc6a62017-08-29 14:45:44 -05002040static bool pci_bus_crs_vendor_id(u32 l)
2041{
2042 return (l & 0xffff) == 0x0001;
2043}
2044
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002045static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
2046 int timeout)
Yinghai Luefdc87d2012-01-27 10:55:10 -08002047{
2048 int delay = 1;
2049
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002050 if (!pci_bus_crs_vendor_id(*l))
2051 return true; /* not a CRS completion */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002052
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002053 if (!timeout)
2054 return false; /* CRS, but caller doesn't want to wait */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002055
Rajat Jain89665a6a2014-09-08 14:19:49 -07002056 /*
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002057 * We got the reserved Vendor ID that indicates a completion with
2058 * Configuration Request Retry Status (CRS). Retry until we get a
2059 * valid Vendor ID or we time out.
Rajat Jain89665a6a2014-09-08 14:19:49 -07002060 */
Sinan Kaya62bc6a62017-08-29 14:45:44 -05002061 while (pci_bus_crs_vendor_id(*l)) {
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002062 if (delay > timeout) {
Sinan Kayae78e6612017-08-29 14:45:45 -05002063 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
2064 pci_domain_nr(bus), bus->number,
2065 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2066
Yinghai Luefdc87d2012-01-27 10:55:10 -08002067 return false;
2068 }
Sinan Kayae78e6612017-08-29 14:45:45 -05002069 if (delay >= 1000)
2070 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
2071 pci_domain_nr(bus), bus->number,
2072 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
Bjorn Helgaas9f982752017-08-29 14:45:43 -05002073
2074 msleep(delay);
2075 delay *= 2;
2076
2077 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2078 return false;
Yinghai Luefdc87d2012-01-27 10:55:10 -08002079 }
2080
Sinan Kayae78e6612017-08-29 14:45:45 -05002081 if (delay >= 1000)
2082 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
2083 pci_domain_nr(bus), bus->number,
2084 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2085
Yinghai Luefdc87d2012-01-27 10:55:10 -08002086 return true;
2087}
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002088
2089bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2090 int timeout)
2091{
Yinghai Luefdc87d2012-01-27 10:55:10 -08002092 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2093 return false;
2094
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002095 /* Some broken boards return 0 or ~0 if a slot is empty: */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002096 if (*l == 0xffffffff || *l == 0x00000000 ||
2097 *l == 0x0000ffff || *l == 0xffff0000)
2098 return false;
2099
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002100 if (pci_bus_crs_vendor_id(*l))
2101 return pci_bus_wait_crs(bus, devfn, l, timeout);
Yinghai Luefdc87d2012-01-27 10:55:10 -08002102
2103 return true;
2104}
2105EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002108 * Read the config data for a PCI device, sanity-check it,
2109 * and fill in the dev structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
Adrian Bunk7f7b5de2008-04-18 13:53:55 -07002111static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct pci_dev *dev;
2114 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Yinghai Luefdc87d2012-01-27 10:55:10 -08002116 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return NULL;
2118
Gu Zheng8b1fce02013-05-25 21:48:31 +08002119 dev = pci_alloc_dev(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (!dev)
2121 return NULL;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 dev->devfn = devfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 dev->vendor = l & 0xffff;
2125 dev->device = (l >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10002127 pci_set_of_node(dev);
2128
Yu Zhao480b93b2009-03-20 11:25:14 +08002129 if (pci_setup_device(dev)) {
Gu Zheng8b1fce02013-05-25 21:48:31 +08002130 pci_bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 kfree(dev);
2132 return NULL;
2133 }
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002134
2135 return dev;
2136}
2137
Zhao, Yu201de562008-10-13 19:49:55 +08002138static void pci_init_capabilities(struct pci_dev *dev)
2139{
Sean O. Stalley938174e2015-10-29 17:35:39 -05002140 /* Enhanced Allocation */
2141 pci_ea_init(dev);
2142
Guilherme G. Piccolie80e7edc2015-10-21 12:17:35 -02002143 /* Setup MSI caps & disable MSI/MSI-X interrupts */
2144 pci_msi_setup_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002145
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002146 /* Buffers for saving PCIe and PCI-X capabilities */
2147 pci_allocate_cap_save_buffers(dev);
2148
Zhao, Yu201de562008-10-13 19:49:55 +08002149 /* Power Management */
2150 pci_pm_init(dev);
2151
2152 /* Vital Product Data */
Bjorn Helgaasf1cd93f2016-02-22 13:58:37 -06002153 pci_vpd_init(dev);
Yu Zhao58c3a722008-10-14 14:02:53 +08002154
2155 /* Alternative Routing-ID Forwarding */
Yijing Wang31ab2472013-01-15 11:12:17 +08002156 pci_configure_ari(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08002157
2158 /* Single Root I/O Virtualization */
2159 pci_iov_init(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07002160
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -05002161 /* Address Translation Services */
2162 pci_ats_init(dev);
2163
Allen Kayae21ee62009-10-07 10:27:17 -07002164 /* Enable ACS P2P upstream forwarding */
Chris Wright5d990b62009-12-04 12:15:21 -08002165 pci_enable_acs(dev);
Taku Izumib07461a2015-09-17 10:09:37 -05002166
Jonathan Yong9bb04a02016-06-11 14:13:38 -05002167 /* Precision Time Measurement */
2168 pci_ptm_init(dev);
Bjorn Helgaas4dc2db02016-10-03 09:42:57 -05002169
Keith Busch66b80802016-09-27 16:23:34 -04002170 /* Advanced Error Reporting */
2171 pci_aer_init(dev);
Bjorn Helgaas5b0764c2018-02-16 10:55:38 -06002172
2173 if (pci_probe_reset_function(dev) == 0)
2174 dev->reset_fn = 1;
Zhao, Yu201de562008-10-13 19:49:55 +08002175}
2176
Marc Zyngier098259e2015-10-02 10:19:32 +01002177/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002178 * This is the equivalent of pci_host_bridge_msi_domain() that acts on
Marc Zyngier098259e2015-10-02 10:19:32 +01002179 * devices. Firmware interfaces that can select the MSI domain on a
2180 * per-device basis should be called from here.
2181 */
2182static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2183{
2184 struct irq_domain *d;
2185
2186 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002187 * If a domain has been set through the pcibios_add_device()
Marc Zyngier098259e2015-10-02 10:19:32 +01002188 * callback, then this is the one (platform code knows best).
2189 */
2190 d = dev_get_msi_domain(&dev->dev);
2191 if (d)
2192 return d;
2193
Marc Zyngier54fa97e2015-10-02 14:43:06 +01002194 /*
2195 * Let's see if we have a firmware interface able to provide
2196 * the domain.
2197 */
2198 d = pci_msi_get_device_domain(dev);
2199 if (d)
2200 return d;
2201
Marc Zyngier098259e2015-10-02 10:19:32 +01002202 return NULL;
2203}
2204
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002205static void pci_set_msi_domain(struct pci_dev *dev)
2206{
Marc Zyngier098259e2015-10-02 10:19:32 +01002207 struct irq_domain *d;
2208
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002209 /*
Marc Zyngier098259e2015-10-02 10:19:32 +01002210 * If the platform or firmware interfaces cannot supply a
2211 * device-specific MSI domain, then inherit the default domain
2212 * from the host bridge itself.
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002213 */
Marc Zyngier098259e2015-10-02 10:19:32 +01002214 d = pci_dev_msi_domain(dev);
2215 if (!d)
2216 d = dev_get_msi_domain(&dev->bus->dev);
2217
2218 dev_set_msi_domain(&dev->dev, d);
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002219}
2220
Sam Ravnborg96bde062007-03-26 21:53:30 -08002221void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002222{
Yinghai Lu4f535092013-01-21 13:20:52 -08002223 int ret;
2224
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06002225 pci_configure_device(dev);
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 device_initialize(&dev->dev);
2228 dev->dev.release = pci_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Yinghai Lu7629d192013-01-21 13:20:44 -08002230 set_dev_node(&dev->dev, pcibus_to_node(bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 dev->dev.dma_mask = &dev->dma_mask;
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002232 dev->dev.dma_parms = &dev->dma_parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 dev->dev.coherent_dma_mask = 0xffffffffull;
2234
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002235 pci_set_dma_max_seg_size(dev, 65536);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08002236 pci_set_dma_seg_boundary(dev, 0xffffffff);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* Fix up broken headers */
2239 pci_fixup_device(pci_fixup_header, dev);
2240
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002241 /* Moved out from quirk header fixup code */
Yinghai Lu2069ecf2012-02-15 21:40:31 -08002242 pci_reassigndev_resource_alignment(dev);
2243
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002244 /* Clear the state_saved flag */
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02002245 dev->state_saved = false;
2246
Zhao, Yu201de562008-10-13 19:49:55 +08002247 /* Initialize various capabilities */
2248 pci_init_capabilities(dev);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /*
2251 * Add the device to our list of discovered devices
2252 * and the bus list for fixup functions, etc.
2253 */
Zhang Yanmind71374d2006-06-02 12:35:43 +08002254 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 list_add_tail(&dev->bus_list, &bus->devices);
Zhang Yanmind71374d2006-06-02 12:35:43 +08002256 up_write(&pci_bus_sem);
Yinghai Lu4f535092013-01-21 13:20:52 -08002257
Yinghai Lu4f535092013-01-21 13:20:52 -08002258 ret = pcibios_add_device(dev);
2259 WARN_ON(ret < 0);
2260
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002261 /* Set up MSI IRQ domain */
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002262 pci_set_msi_domain(dev);
2263
Yinghai Lu4f535092013-01-21 13:20:52 -08002264 /* Notifier could use PCI capabilities */
2265 dev->match_driver = false;
2266 ret = device_add(&dev->dev);
2267 WARN_ON(ret < 0);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002268}
2269
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002270struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002271{
2272 struct pci_dev *dev;
2273
Trent Piepho90bdb312009-03-20 14:56:00 -06002274 dev = pci_get_slot(bus, devfn);
2275 if (dev) {
2276 pci_dev_put(dev);
2277 return dev;
2278 }
2279
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002280 dev = pci_scan_device(bus, devfn);
2281 if (!dev)
2282 return NULL;
2283
2284 pci_device_add(dev, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 return dev;
2287}
Adrian Bunkb73e9682007-11-21 15:07:11 -08002288EXPORT_SYMBOL(pci_scan_single_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002290static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002291{
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002292 int pos;
2293 u16 cap = 0;
2294 unsigned next_fn;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002295
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002296 if (pci_ari_enabled(bus)) {
2297 if (!dev)
2298 return 0;
2299 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2300 if (!pos)
2301 return 0;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002302
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002303 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2304 next_fn = PCI_ARI_CAP_NFN(cap);
2305 if (next_fn <= fn)
2306 return 0; /* protect against malformed list */
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002307
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002308 return next_fn;
2309 }
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002310
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002311 /* dev may be NULL for non-contiguous multifunction devices */
2312 if (!dev || dev->multifunction)
2313 return (fn + 1) % 8;
2314
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002315 return 0;
2316}
2317
2318static int only_one_child(struct pci_bus *bus)
2319{
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002320 struct pci_dev *bridge = bus->self;
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002321
2322 /*
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002323 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
2324 * we scan for all possible devices, not just Device 0.
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002325 */
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002326 if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
2327 return 0;
2328
2329 /*
2330 * A PCIe Downstream Port normally leads to a Link with only Device
2331 * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan
2332 * only for Device 0 in that situation.
2333 *
2334 * Checking has_secondary_link is a hack to identify Downstream
2335 * Ports because sometimes Switches are configured such that the
2336 * PCIe Port Type labels are backwards.
2337 */
2338 if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002339 return 1;
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002340
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002341 return 0;
2342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002345 * pci_scan_slot - Scan a PCI slot on a bus for devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 * @bus: PCI bus to scan
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002347 * @devfn: slot number to scan (must have zero function)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 *
2349 * Scan a PCI slot on the specified PCI bus for devices, adding
2350 * discovered devices to the @bus->devices list. New devices
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -08002351 * will not have is_added set.
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002352 *
2353 * Returns the number of new devices found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 */
Sam Ravnborg96bde062007-03-26 21:53:30 -08002355int pci_scan_slot(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002357 unsigned fn, nr = 0;
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002358 struct pci_dev *dev;
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002359
2360 if (only_one_child(bus) && (devfn > 0))
2361 return 0; /* Already scanned the entire slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002363 dev = pci_scan_single_device(bus, devfn);
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002364 if (!dev)
2365 return 0;
2366 if (!dev->is_added)
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002367 nr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002369 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002370 dev = pci_scan_single_device(bus, devfn + fn);
2371 if (dev) {
2372 if (!dev->is_added)
2373 nr++;
2374 dev->multifunction = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 }
Shaohua Li7d715a62008-02-25 09:46:41 +08002377
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002378 /* Only one slot has PCIe device */
Shaohua Li149e1632008-07-23 10:32:31 +08002379 if (bus->self && nr)
Shaohua Li7d715a62008-02-25 09:46:41 +08002380 pcie_aspm_init_link_state(bus->self);
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return nr;
2383}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002384EXPORT_SYMBOL(pci_scan_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Jon Masonb03e7492011-07-20 15:20:54 -05002386static int pcie_find_smpss(struct pci_dev *dev, void *data)
2387{
2388 u8 *smpss = data;
2389
2390 if (!pci_is_pcie(dev))
2391 return 0;
2392
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002393 /*
2394 * We don't have a way to change MPS settings on devices that have
2395 * drivers attached. A hot-added device might support only the minimum
2396 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
2397 * where devices may be hot-added, we limit the fabric MPS to 128 so
2398 * hot-added devices will work correctly.
2399 *
2400 * However, if we hot-add a device to a slot directly below a Root
2401 * Port, it's impossible for there to be other existing devices below
2402 * the port. We don't limit the MPS in this case because we can
2403 * reconfigure MPS on both the Root Port and the hot-added device,
2404 * and there are no other devices involved.
2405 *
2406 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
Jon Masonb03e7492011-07-20 15:20:54 -05002407 */
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002408 if (dev->is_hotplug_bridge &&
2409 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
Jon Masonb03e7492011-07-20 15:20:54 -05002410 *smpss = 0;
2411
2412 if (*smpss > dev->pcie_mpss)
2413 *smpss = dev->pcie_mpss;
2414
2415 return 0;
2416}
2417
2418static void pcie_write_mps(struct pci_dev *dev, int mps)
2419{
Jon Mason62f392e2011-10-14 14:56:14 -05002420 int rc;
Jon Masonb03e7492011-07-20 15:20:54 -05002421
2422 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
Jon Mason62f392e2011-10-14 14:56:14 -05002423 mps = 128 << dev->pcie_mpss;
Jon Masonb03e7492011-07-20 15:20:54 -05002424
Yijing Wang62f87c02012-07-24 17:20:03 +08002425 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2426 dev->bus->self)
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002427
2428 /*
2429 * For "Performance", the assumption is made that
Jon Masonb03e7492011-07-20 15:20:54 -05002430 * downstream communication will never be larger than
2431 * the MRRS. So, the MPS only needs to be configured
2432 * for the upstream communication. This being the case,
2433 * walk from the top down and set the MPS of the child
2434 * to that of the parent bus.
Jon Mason62f392e2011-10-14 14:56:14 -05002435 *
2436 * Configure the device MPS with the smaller of the
2437 * device MPSS or the bridge MPS (which is assumed to be
2438 * properly configured at this point to the largest
2439 * allowable MPS based on its parent bus).
Jon Masonb03e7492011-07-20 15:20:54 -05002440 */
Jon Mason62f392e2011-10-14 14:56:14 -05002441 mps = min(mps, pcie_get_mps(dev->bus->self));
Jon Masonb03e7492011-07-20 15:20:54 -05002442 }
2443
2444 rc = pcie_set_mps(dev, mps);
2445 if (rc)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002446 pci_err(dev, "Failed attempting to set the MPS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002447}
2448
Jon Mason62f392e2011-10-14 14:56:14 -05002449static void pcie_write_mrrs(struct pci_dev *dev)
Jon Masonb03e7492011-07-20 15:20:54 -05002450{
Jon Mason62f392e2011-10-14 14:56:14 -05002451 int rc, mrrs;
Jon Masonb03e7492011-07-20 15:20:54 -05002452
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002453 /*
2454 * In the "safe" case, do not configure the MRRS. There appear to be
Jon Masoned2888e2011-09-08 16:41:18 -05002455 * issues with setting MRRS to 0 on a number of devices.
2456 */
Jon Masoned2888e2011-09-08 16:41:18 -05002457 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2458 return;
Jon Masonb03e7492011-07-20 15:20:54 -05002459
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002460 /*
2461 * For max performance, the MRRS must be set to the largest supported
Jon Masoned2888e2011-09-08 16:41:18 -05002462 * value. However, it cannot be configured larger than the MPS the
Jon Mason62f392e2011-10-14 14:56:14 -05002463 * device or the bus can support. This should already be properly
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002464 * configured by a prior call to pcie_write_mps().
Jon Masoned2888e2011-09-08 16:41:18 -05002465 */
Jon Mason62f392e2011-10-14 14:56:14 -05002466 mrrs = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002467
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002468 /*
2469 * MRRS is a R/W register. Invalid values can be written, but a
Jon Masoned2888e2011-09-08 16:41:18 -05002470 * subsequent read will verify if the value is acceptable or not.
Jon Masonb03e7492011-07-20 15:20:54 -05002471 * If the MRRS value provided is not acceptable (e.g., too large),
2472 * shrink the value until it is acceptable to the HW.
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002473 */
Jon Masonb03e7492011-07-20 15:20:54 -05002474 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2475 rc = pcie_set_readrq(dev, mrrs);
Jon Mason62f392e2011-10-14 14:56:14 -05002476 if (!rc)
2477 break;
Jon Masonb03e7492011-07-20 15:20:54 -05002478
Frederick Lawler7506dc72018-01-18 12:55:24 -06002479 pci_warn(dev, "Failed attempting to set the MRRS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002480 mrrs /= 2;
2481 }
Jon Mason62f392e2011-10-14 14:56:14 -05002482
2483 if (mrrs < 128)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002484 pci_err(dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002485}
2486
2487static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2488{
Jon Masona513a99a72011-10-14 14:56:16 -05002489 int mps, orig_mps;
Jon Masonb03e7492011-07-20 15:20:54 -05002490
2491 if (!pci_is_pcie(dev))
2492 return 0;
2493
Keith Busch27d868b2015-08-24 08:48:16 -05002494 if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2495 pcie_bus_config == PCIE_BUS_DEFAULT)
Yijing Wang5895af72013-08-26 16:33:06 +08002496 return 0;
Yijing Wang5895af72013-08-26 16:33:06 +08002497
Jon Masona513a99a72011-10-14 14:56:16 -05002498 mps = 128 << *(u8 *)data;
2499 orig_mps = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002500
2501 pcie_write_mps(dev, mps);
Jon Mason62f392e2011-10-14 14:56:14 -05002502 pcie_write_mrrs(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002503
Frederick Lawler7506dc72018-01-18 12:55:24 -06002504 pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04002505 pcie_get_mps(dev), 128 << dev->pcie_mpss,
Jon Masona513a99a72011-10-14 14:56:16 -05002506 orig_mps, pcie_get_readrq(dev));
Jon Masonb03e7492011-07-20 15:20:54 -05002507
2508 return 0;
2509}
2510
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002511/*
2512 * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
Jon Masonb03e7492011-07-20 15:20:54 -05002513 * parents then children fashion. If this changes, then this code will not
2514 * work as designed.
2515 */
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002516void pcie_bus_configure_settings(struct pci_bus *bus)
Jon Masonb03e7492011-07-20 15:20:54 -05002517{
Bjorn Helgaas1e358f92014-04-29 12:51:55 -06002518 u8 smpss = 0;
Jon Masonb03e7492011-07-20 15:20:54 -05002519
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002520 if (!bus->self)
2521 return;
2522
Jon Masonb03e7492011-07-20 15:20:54 -05002523 if (!pci_is_pcie(bus->self))
2524 return;
2525
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002526 /*
2527 * FIXME - Peer to peer DMA is possible, though the endpoint would need
Jon Mason33154722013-08-26 16:33:05 +08002528 * to be aware of the MPS of the destination. To work around this,
Jon Mason5f39e672011-10-03 09:50:20 -05002529 * simply force the MPS of the entire system to the smallest possible.
2530 */
2531 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2532 smpss = 0;
2533
Jon Masonb03e7492011-07-20 15:20:54 -05002534 if (pcie_bus_config == PCIE_BUS_SAFE) {
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002535 smpss = bus->self->pcie_mpss;
Jon Mason5f39e672011-10-03 09:50:20 -05002536
Jon Masonb03e7492011-07-20 15:20:54 -05002537 pcie_find_smpss(bus->self, &smpss);
2538 pci_walk_bus(bus, pcie_find_smpss, &smpss);
2539 }
2540
2541 pcie_bus_configure_set(bus->self, &smpss);
2542 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2543}
Jon Masondebc3b72011-08-02 00:01:18 -05002544EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
Jon Masonb03e7492011-07-20 15:20:54 -05002545
Palmer Dabbeltbccf90d2017-06-23 18:50:42 -07002546/*
2547 * Called after each bus is probed, but before its children are examined. This
2548 * is marked as __weak because multiple architectures define it.
2549 */
2550void __weak pcibios_fixup_bus(struct pci_bus *bus)
2551{
2552 /* nothing to do, expected to be removed in the future */
2553}
2554
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002555/**
2556 * pci_scan_child_bus_extend() - Scan devices below a bus
2557 * @bus: Bus to scan for devices
2558 * @available_buses: Total number of buses available (%0 does not try to
2559 * extend beyond the minimal)
2560 *
2561 * Scans devices below @bus including subordinate buses. Returns new
2562 * subordinate number including all the found devices. Passing
2563 * @available_buses causes the remaining bus space to be distributed
2564 * equally between hotplug-capable bridges to allow future extension of the
2565 * hierarchy.
2566 */
2567static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2568 unsigned int available_buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002570 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2571 unsigned int start = bus->busn_res.start;
2572 unsigned int devfn, cmax, max = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 struct pci_dev *dev;
2574
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002575 dev_dbg(&bus->dev, "scanning bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 /* Go find them, Rover! */
2578 for (devfn = 0; devfn < 0x100; devfn += 8)
2579 pci_scan_slot(bus, devfn);
2580
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002581 /* Reserve buses for SR-IOV capability */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002582 used_buses = pci_iov_bus_range(bus);
2583 max += used_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +08002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /*
2586 * After performing arch-dependent fixup of the bus, look behind
2587 * all PCI-to-PCI bridges on this bus.
2588 */
Alex Chiang74710de2009-03-20 14:56:10 -06002589 if (!bus->is_added) {
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002590 dev_dbg(&bus->dev, "fixups for bus\n");
Alex Chiang74710de2009-03-20 14:56:10 -06002591 pcibios_fixup_bus(bus);
Jiang Liu981cf9e2013-04-12 05:44:16 +00002592 bus->is_added = 1;
Alex Chiang74710de2009-03-20 14:56:10 -06002593 }
2594
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002595 /*
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002596 * Calculate how many hotplug bridges and normal bridges there
2597 * are on this bus. We will distribute the additional available
2598 * buses between hotplug bridges.
2599 */
2600 for_each_pci_bridge(dev, bus) {
2601 if (dev->is_hotplug_bridge)
2602 hotplug_bridges++;
2603 else
2604 normal_bridges++;
2605 }
2606
2607 /*
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002608 * Scan bridges that are already configured. We don't touch them
2609 * unless they are misconfigured (which will be done in the second
2610 * scan below).
2611 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002612 for_each_pci_bridge(dev, bus) {
2613 cmax = max;
2614 max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
2615 used_buses += cmax - max;
2616 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002617
2618 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002619 for_each_pci_bridge(dev, bus) {
2620 unsigned int buses = 0;
2621
2622 if (!hotplug_bridges && normal_bridges == 1) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002623
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002624 /*
2625 * There is only one bridge on the bus (upstream
2626 * port) so it gets all available buses which it
2627 * can then distribute to the possible hotplug
2628 * bridges below.
2629 */
2630 buses = available_buses;
2631 } else if (dev->is_hotplug_bridge) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002632
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002633 /*
2634 * Distribute the extra buses between hotplug
2635 * bridges if any.
2636 */
2637 buses = available_buses / hotplug_bridges;
2638 buses = min(buses, available_buses - used_buses);
2639 }
2640
2641 cmax = max;
2642 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
2643 used_buses += max - cmax;
2644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 /*
Keith Busche16b4662016-07-21 21:40:28 -06002647 * Make sure a hotplug bridge has at least the minimum requested
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002648 * number of buses but allow it to grow up to the maximum available
2649 * bus number of there is room.
Keith Busche16b4662016-07-21 21:40:28 -06002650 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002651 if (bus->self && bus->self->is_hotplug_bridge) {
2652 used_buses = max_t(unsigned int, available_buses,
2653 pci_hotplug_bus_size - 1);
2654 if (max - start < used_buses) {
2655 max = start + used_buses;
Mika Westerberga20c7f32017-10-13 21:35:43 +03002656
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002657 /* Do not allocate more buses than we have room left */
2658 if (max > bus->busn_res.end)
2659 max = bus->busn_res.end;
2660
2661 dev_dbg(&bus->dev, "%pR extended by %#02x\n",
2662 &bus->busn_res, max - start);
2663 }
Keith Busche16b4662016-07-21 21:40:28 -06002664 }
2665
2666 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 * We've scanned the bus and so we know all about what's on
2668 * the other side of any bridges that may be on this bus plus
2669 * any devices.
2670 *
2671 * Return how far we've got finding sub-buses.
2672 */
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002673 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return max;
2675}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002676
2677/**
2678 * pci_scan_child_bus() - Scan devices below a bus
2679 * @bus: Bus to scan for devices
2680 *
2681 * Scans devices below @bus including subordinate buses. Returns new
2682 * subordinate number including all the found devices.
2683 */
2684unsigned int pci_scan_child_bus(struct pci_bus *bus)
2685{
2686 return pci_scan_child_bus_extend(bus, 0);
2687}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002688EXPORT_SYMBOL_GPL(pci_scan_child_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002690/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002691 * pcibios_root_bridge_prepare - Platform-specific host bridge setup
2692 * @bridge: Host bridge to set up
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002693 *
2694 * Default empty implementation. Replace with an architecture-specific setup
2695 * routine, if necessary.
2696 */
2697int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
2698{
2699 return 0;
2700}
2701
Jiang Liu10a95742013-04-12 05:44:20 +00002702void __weak pcibios_add_bus(struct pci_bus *bus)
2703{
2704}
2705
2706void __weak pcibios_remove_bus(struct pci_bus *bus)
2707{
2708}
2709
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002710struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
2711 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07002713 int error;
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07002714 struct pci_host_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Thierry Reding59094062016-11-25 11:57:10 +01002716 bridge = pci_alloc_host_bridge(0);
Yinghai Lu7b543662012-04-02 18:31:53 -07002717 if (!bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002718 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -07002719
2720 bridge->dev.parent = parent;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002721
2722 list_splice_init(resources, &bridge->windows);
2723 bridge->sysdata = sysdata;
2724 bridge->busnr = bus;
2725 bridge->ops = ops;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002726
2727 error = pci_register_host_bridge(bridge);
2728 if (error < 0)
Jiang Liu343df772013-06-07 01:10:08 +08002729 goto err_out;
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002730
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002731 return bridge->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Yinghai Lu7b543662012-04-02 18:31:53 -07002733err_out:
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002734 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return NULL;
2736}
Ray Juie6b29de2015-04-08 11:21:33 -07002737EXPORT_SYMBOL_GPL(pci_create_root_bus);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002738
Cyrille Pitchen49b8e3f2018-01-30 21:56:52 +01002739int pci_host_probe(struct pci_host_bridge *bridge)
2740{
2741 struct pci_bus *bus, *child;
2742 int ret;
2743
2744 ret = pci_scan_root_bus_bridge(bridge);
2745 if (ret < 0) {
2746 dev_err(bridge->dev.parent, "Scanning root bridge failed");
2747 return ret;
2748 }
2749
2750 bus = bridge->bus;
2751
2752 /*
2753 * We insert PCI resources into the iomem_resource and
2754 * ioport_resource trees in either pci_bus_claim_resources()
2755 * or pci_bus_assign_resources().
2756 */
2757 if (pci_has_flag(PCI_PROBE_ONLY)) {
2758 pci_bus_claim_resources(bus);
2759 } else {
2760 pci_bus_size_bridges(bus);
2761 pci_bus_assign_resources(bus);
2762
2763 list_for_each_entry(child, &bus->children, node)
2764 pcie_bus_configure_settings(child);
2765 }
2766
2767 pci_bus_add_devices(bus);
2768 return 0;
2769}
2770EXPORT_SYMBOL_GPL(pci_host_probe);
2771
Yinghai Lu98a35832012-05-18 11:35:50 -06002772int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2773{
2774 struct resource *res = &b->busn_res;
2775 struct resource *parent_res, *conflict;
2776
2777 res->start = bus;
2778 res->end = bus_max;
2779 res->flags = IORESOURCE_BUS;
2780
2781 if (!pci_is_root_bus(b))
2782 parent_res = &b->parent->busn_res;
2783 else {
2784 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
2785 res->flags |= IORESOURCE_PCI_FIXED;
2786 }
2787
Andreas Noeverced04d12014-01-23 21:59:24 +01002788 conflict = request_resource_conflict(parent_res, res);
Yinghai Lu98a35832012-05-18 11:35:50 -06002789
2790 if (conflict)
2791 dev_printk(KERN_DEBUG, &b->dev,
2792 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
2793 res, pci_is_root_bus(b) ? "domain " : "",
2794 parent_res, conflict->name, conflict);
Yinghai Lu98a35832012-05-18 11:35:50 -06002795
2796 return conflict == NULL;
2797}
2798
2799int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
2800{
2801 struct resource *res = &b->busn_res;
2802 struct resource old_res = *res;
2803 resource_size_t size;
2804 int ret;
2805
2806 if (res->start > bus_max)
2807 return -EINVAL;
2808
2809 size = bus_max - res->start + 1;
2810 ret = adjust_resource(res, res->start, size);
2811 dev_printk(KERN_DEBUG, &b->dev,
2812 "busn_res: %pR end %s updated to %02x\n",
2813 &old_res, ret ? "can not be" : "is", bus_max);
2814
2815 if (!ret && !res->parent)
2816 pci_bus_insert_busn_res(b, res->start, res->end);
2817
2818 return ret;
2819}
2820
2821void pci_bus_release_busn_res(struct pci_bus *b)
2822{
2823 struct resource *res = &b->busn_res;
2824 int ret;
2825
2826 if (!res->flags || !res->parent)
2827 return;
2828
2829 ret = release_resource(res);
2830 dev_printk(KERN_DEBUG, &b->dev,
2831 "busn_res: %pR %s released\n",
2832 res, ret ? "can not be" : "is");
2833}
2834
Lorenzo Pieralisi1228c4b2017-06-28 15:13:55 -05002835int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
2836{
2837 struct resource_entry *window;
2838 bool found = false;
2839 struct pci_bus *b;
2840 int max, bus, ret;
2841
2842 if (!bridge)
2843 return -EINVAL;
2844
2845 resource_list_for_each_entry(window, &bridge->windows)
2846 if (window->res->flags & IORESOURCE_BUS) {
2847 found = true;
2848 break;
2849 }
2850
2851 ret = pci_register_host_bridge(bridge);
2852 if (ret < 0)
2853 return ret;
2854
2855 b = bridge->bus;
2856 bus = bridge->busnr;
2857
2858 if (!found) {
2859 dev_info(&b->dev,
2860 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2861 bus);
2862 pci_bus_insert_busn_res(b, bus, 255);
2863 }
2864
2865 max = pci_scan_child_bus(b);
2866
2867 if (!found)
2868 pci_bus_update_busn_res_end(b, max);
2869
2870 return 0;
2871}
2872EXPORT_SYMBOL(pci_scan_root_bus_bridge);
2873
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002874struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
2875 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002876{
Jiang Liu14d76b62015-02-05 13:44:44 +08002877 struct resource_entry *window;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002878 bool found = false;
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002879 struct pci_bus *b;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002880 int max;
2881
Jiang Liu14d76b62015-02-05 13:44:44 +08002882 resource_list_for_each_entry(window, resources)
Yinghai Lu4d99f522012-05-17 18:51:12 -07002883 if (window->res->flags & IORESOURCE_BUS) {
2884 found = true;
2885 break;
2886 }
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002887
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002888 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002889 if (!b)
2890 return NULL;
2891
Yinghai Lu4d99f522012-05-17 18:51:12 -07002892 if (!found) {
2893 dev_info(&b->dev,
2894 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2895 bus);
2896 pci_bus_insert_busn_res(b, bus, 255);
2897 }
2898
2899 max = pci_scan_child_bus(b);
2900
2901 if (!found)
2902 pci_bus_update_busn_res_end(b, max);
2903
Bjorn Helgaasa2ebb8272011-10-28 16:25:50 -06002904 return b;
2905}
2906EXPORT_SYMBOL(pci_scan_root_bus);
2907
Bill Pemberton15856ad2012-11-21 15:35:00 -05002908struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002909 void *sysdata)
2910{
2911 LIST_HEAD(resources);
2912 struct pci_bus *b;
2913
2914 pci_add_resource(&resources, &ioport_resource);
2915 pci_add_resource(&resources, &iomem_resource);
Yinghai Lu857c3b62012-05-17 18:51:12 -07002916 pci_add_resource(&resources, &busn_resource);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002917 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
2918 if (b) {
Yinghai Lu857c3b62012-05-17 18:51:12 -07002919 pci_scan_child_bus(b);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002920 } else {
2921 pci_free_resource_list(&resources);
2922 }
2923 return b;
2924}
2925EXPORT_SYMBOL(pci_scan_bus);
2926
Alex Chiang3ed4fd92009-03-20 14:56:25 -06002927/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002928 * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
Yinghai Lu2f320522012-01-21 02:08:22 -08002929 * @bridge: PCI bridge for the bus to scan
2930 *
2931 * Scan a PCI bus and child buses for new devices, add them,
2932 * and enable them, resizing bridge mmio/io resource if necessary
2933 * and possible. The caller must ensure the child devices are already
2934 * removed for resizing to occur.
2935 *
2936 * Returns the max number of subordinate bus discovered.
2937 */
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002938unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
Yinghai Lu2f320522012-01-21 02:08:22 -08002939{
2940 unsigned int max;
2941 struct pci_bus *bus = bridge->subordinate;
2942
2943 max = pci_scan_child_bus(bus);
2944
2945 pci_assign_unassigned_bridge_resources(bridge);
2946
2947 pci_bus_add_devices(bus);
2948
2949 return max;
2950}
2951
Yinghai Lua5213a32012-10-30 14:31:21 -06002952/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002953 * pci_rescan_bus - Scan a PCI bus for devices
Yinghai Lua5213a32012-10-30 14:31:21 -06002954 * @bus: PCI bus to scan
2955 *
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002956 * Scan a PCI bus and child buses for new devices, add them,
2957 * and enable them.
Yinghai Lua5213a32012-10-30 14:31:21 -06002958 *
2959 * Returns the max number of subordinate bus discovered.
2960 */
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -06002961unsigned int pci_rescan_bus(struct pci_bus *bus)
Yinghai Lua5213a32012-10-30 14:31:21 -06002962{
2963 unsigned int max;
2964
2965 max = pci_scan_child_bus(bus);
2966 pci_assign_unassigned_bus_resources(bus);
2967 pci_bus_add_devices(bus);
2968
2969 return max;
2970}
2971EXPORT_SYMBOL_GPL(pci_rescan_bus);
2972
Rafael J. Wysocki9d169472014-01-10 15:22:18 +01002973/*
2974 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
2975 * routines should always be executed under this mutex.
2976 */
2977static DEFINE_MUTEX(pci_rescan_remove_lock);
2978
2979void pci_lock_rescan_remove(void)
2980{
2981 mutex_lock(&pci_rescan_remove_lock);
2982}
2983EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
2984
2985void pci_unlock_rescan_remove(void)
2986{
2987 mutex_unlock(&pci_rescan_remove_lock);
2988}
2989EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
2990
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002991static int __init pci_sort_bf_cmp(const struct device *d_a,
2992 const struct device *d_b)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002993{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002994 const struct pci_dev *a = to_pci_dev(d_a);
2995 const struct pci_dev *b = to_pci_dev(d_b);
2996
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002997 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
2998 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
2999
3000 if (a->bus->number < b->bus->number) return -1;
3001 else if (a->bus->number > b->bus->number) return 1;
3002
3003 if (a->devfn < b->devfn) return -1;
3004 else if (a->devfn > b->devfn) return 1;
3005
3006 return 0;
3007}
3008
Greg Kroah-Hartman5ff580c2008-02-14 14:56:56 -08003009void __init pci_sort_breadthfirst(void)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05003010{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05003011 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
Matt Domsch6b4b78f2006-09-29 15:23:23 -05003012}
Mika Westerberg95e3ba92017-10-13 21:35:41 +03003013
3014int pci_hp_add_bridge(struct pci_dev *dev)
3015{
3016 struct pci_bus *parent = dev->bus;
Mika Westerberg4147c2f2017-10-13 21:35:42 +03003017 int busnr, start = parent->busn_res.start;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03003018 unsigned int available_buses = 0;
Mika Westerberg95e3ba92017-10-13 21:35:41 +03003019 int end = parent->busn_res.end;
3020
3021 for (busnr = start; busnr <= end; busnr++) {
3022 if (!pci_find_bus(pci_domain_nr(parent), busnr))
3023 break;
3024 }
3025 if (busnr-- > end) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06003026 pci_err(dev, "No bus number available for hot-added bridge\n");
Mika Westerberg95e3ba92017-10-13 21:35:41 +03003027 return -1;
3028 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03003029
3030 /* Scan bridges that are already configured */
3031 busnr = pci_scan_bridge(parent, dev, busnr, 0);
3032
Mika Westerberg1c02ea82017-10-13 21:35:44 +03003033 /*
3034 * Distribute the available bus numbers between hotplug-capable
3035 * bridges to make extending the chain later possible.
3036 */
3037 available_buses = end - busnr;
3038
Mika Westerberg4147c2f2017-10-13 21:35:42 +03003039 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03003040 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
Mika Westerberg4147c2f2017-10-13 21:35:42 +03003041
Mika Westerberg95e3ba92017-10-13 21:35:41 +03003042 if (!dev->subordinate)
3043 return -1;
3044
3045 return 0;
3046}
3047EXPORT_SYMBOL_GPL(pci_hp_add_bridge);