blob: ef5377438a1e65df31790a61472e34e708a835ab [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
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400332 for (pos = 0; pos < howmany; pos++) {
333 struct resource *res = &dev->resource[pos];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400335 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (rom) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400339 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dev->rom_base_reg = rom;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400341 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
Dan Williams92b19ff2015-08-10 23:07:06 -0400342 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400343 __pci_read_base(dev, pci_bar_mem32, res, rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345}
346
Bill Pemberton15856ad2012-11-21 15:35:00 -0500347static void pci_read_bridge_io(struct pci_bus *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct pci_dev *dev = child->self;
350 u8 io_base_lo, io_limit_lo;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600351 unsigned long io_mask, io_granularity, base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700352 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600353 struct resource *res;
354
355 io_mask = PCI_IO_RANGE_MASK;
356 io_granularity = 0x1000;
357 if (dev->io_window_1k) {
358 /* Support 1K I/O space granularity */
359 io_mask = PCI_IO_1K_RANGE_MASK;
360 io_granularity = 0x400;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 res = child->resource[0];
364 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
365 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600366 base = (io_base_lo & io_mask) << 8;
367 limit = (io_limit_lo & io_mask) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
370 u16 io_base_hi, io_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
373 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600374 base |= ((unsigned long) io_base_hi << 16);
375 limit |= ((unsigned long) io_limit_hi << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600378 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700380 region.start = base;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600381 region.end = limit + io_granularity - 1;
Yinghai Lufc279852013-12-09 22:54:40 -0800382 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600383 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700385}
386
Bill Pemberton15856ad2012-11-21 15:35:00 -0500387static void pci_read_bridge_mmio(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700388{
389 struct pci_dev *dev = child->self;
390 u16 mem_base_lo, mem_limit_lo;
391 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700392 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700393 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 res = child->resource[1];
396 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
397 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600398 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
399 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600400 if (base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700402 region.start = base;
403 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800404 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600405 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700407}
408
Bill Pemberton15856ad2012-11-21 15:35:00 -0500409static void pci_read_bridge_mmio_pref(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700410{
411 struct pci_dev *dev = child->self;
412 u16 mem_base_lo, mem_limit_lo;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700413 u64 base64, limit64;
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700414 pci_bus_addr_t base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700415 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700416 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 res = child->resource[2];
419 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
420 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700421 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
422 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
425 u32 mem_base_hi, mem_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
428 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
429
430 /*
431 * Some bridges set the base > limit by default, and some
432 * (broken) BIOSes do not initialize them. If we find
433 * this, just assume they are not being used.
434 */
435 if (mem_base_hi <= mem_limit_hi) {
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700436 base64 |= (u64) mem_base_hi << 32;
437 limit64 |= (u64) mem_limit_hi << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 }
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700440
Yinghai Lu3a9ad0b2015-05-27 17:23:51 -0700441 base = (pci_bus_addr_t) base64;
442 limit = (pci_bus_addr_t) limit64;
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700443
444 if (base != base64) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600445 pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
Yinghai Lu7fc986d2014-11-19 14:30:32 -0700446 (unsigned long long) base64);
447 return;
448 }
449
Bjorn Helgaas5dde3832012-07-09 13:38:41 -0600450 if (base <= limit) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700451 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
452 IORESOURCE_MEM | IORESOURCE_PREFETCH;
453 if (res->flags & PCI_PREF_RANGE_TYPE_64)
454 res->flags |= IORESOURCE_MEM_64;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700455 region.start = base;
456 region.end = limit + 0xfffff;
Yinghai Lufc279852013-12-09 22:54:40 -0800457 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600458 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460}
461
Bill Pemberton15856ad2012-11-21 15:35:00 -0500462void pci_read_bridge_bases(struct pci_bus *child)
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700463{
464 struct pci_dev *dev = child->self;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700465 struct resource *res;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700466 int i;
467
468 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
469 return;
470
Frederick Lawler7506dc72018-01-18 12:55:24 -0600471 pci_info(dev, "PCI bridge to %pR%s\n",
Yinghai Lub918c622012-05-17 18:51:11 -0700472 &child->busn_res,
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700473 dev->transparent ? " (subtractive decode)" : "");
474
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700475 pci_bus_remove_resources(child);
476 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
477 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
478
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700479 pci_read_bridge_io(child);
480 pci_read_bridge_mmio(child);
481 pci_read_bridge_mmio_pref(child);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700482
483 if (dev->transparent) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700484 pci_bus_for_each_resource(child->parent, res, i) {
Bjorn Helgaasd739a092014-04-14 16:10:54 -0600485 if (res && res->flags) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700486 pci_bus_add_resource(child, res,
487 PCI_SUBTRACTIVE_DECODE);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600488 pci_printk(KERN_DEBUG, dev,
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700489 " bridge window %pR (subtractive decode)\n",
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700490 res);
491 }
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700492 }
493 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700494}
495
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100496static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct pci_bus *b;
499
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100500 b = kzalloc(sizeof(*b), GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600501 if (!b)
502 return NULL;
503
504 INIT_LIST_HEAD(&b->node);
505 INIT_LIST_HEAD(&b->children);
506 INIT_LIST_HEAD(&b->devices);
507 INIT_LIST_HEAD(&b->slots);
508 INIT_LIST_HEAD(&b->resources);
509 b->max_bus_speed = PCI_SPEED_UNKNOWN;
510 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100511#ifdef CONFIG_PCI_DOMAINS_GENERIC
512 if (parent)
513 b->domain_nr = parent->domain_nr;
514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return b;
516}
517
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500518static void devm_pci_release_host_bridge_dev(struct device *dev)
Jiang Liu70efde22013-06-07 16:16:51 -0600519{
520 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
521
522 if (bridge->release_fn)
523 bridge->release_fn(bridge);
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500524}
Jiang Liu70efde22013-06-07 16:16:51 -0600525
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500526static void pci_release_host_bridge_dev(struct device *dev)
527{
528 devm_pci_release_host_bridge_dev(dev);
529 pci_free_host_bridge(to_pci_host_bridge(dev));
Jiang Liu70efde22013-06-07 16:16:51 -0600530}
531
Thierry Redinga52d1442016-11-25 11:57:11 +0100532struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
Yinghai Lu7b543662012-04-02 18:31:53 -0700533{
534 struct pci_host_bridge *bridge;
535
Thierry Reding59094062016-11-25 11:57:10 +0100536 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
Bjorn Helgaas05013482013-06-05 14:22:11 -0600537 if (!bridge)
538 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -0700539
Bjorn Helgaas05013482013-06-05 14:22:11 -0600540 INIT_LIST_HEAD(&bridge->windows);
Lorenzo Pieralisia1c00502017-06-28 15:13:52 -0500541 bridge->dev.release = pci_release_host_bridge_dev;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100542
Yinghai Lu7b543662012-04-02 18:31:53 -0700543 return bridge;
544}
Thierry Redinga52d1442016-11-25 11:57:11 +0100545EXPORT_SYMBOL(pci_alloc_host_bridge);
Yinghai Lu7b543662012-04-02 18:31:53 -0700546
Lorenzo Pieralisi5c3f18c2017-06-28 15:13:53 -0500547struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
548 size_t priv)
549{
550 struct pci_host_bridge *bridge;
551
552 bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
553 if (!bridge)
554 return NULL;
555
556 INIT_LIST_HEAD(&bridge->windows);
557 bridge->dev.release = devm_pci_release_host_bridge_dev;
558
559 return bridge;
560}
561EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
562
Lorenzo Pieralisidff79b92017-06-28 15:13:52 -0500563void pci_free_host_bridge(struct pci_host_bridge *bridge)
564{
565 pci_free_resource_list(&bridge->windows);
566
567 kfree(bridge);
568}
569EXPORT_SYMBOL(pci_free_host_bridge);
570
Stephen Hemminger0b950f02014-01-10 17:14:48 -0700571static const unsigned char pcix_bus_speed[] = {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500572 PCI_SPEED_UNKNOWN, /* 0 */
573 PCI_SPEED_66MHz_PCIX, /* 1 */
574 PCI_SPEED_100MHz_PCIX, /* 2 */
575 PCI_SPEED_133MHz_PCIX, /* 3 */
576 PCI_SPEED_UNKNOWN, /* 4 */
577 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
578 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
579 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
580 PCI_SPEED_UNKNOWN, /* 8 */
581 PCI_SPEED_66MHz_PCIX_266, /* 9 */
582 PCI_SPEED_100MHz_PCIX_266, /* A */
583 PCI_SPEED_133MHz_PCIX_266, /* B */
584 PCI_SPEED_UNKNOWN, /* C */
585 PCI_SPEED_66MHz_PCIX_533, /* D */
586 PCI_SPEED_100MHz_PCIX_533, /* E */
587 PCI_SPEED_133MHz_PCIX_533 /* F */
588};
589
Jacob Keller343e51a2013-07-31 06:53:16 +0000590const unsigned char pcie_link_speed[] = {
Matthew Wilcox3749c512009-12-13 08:11:32 -0500591 PCI_SPEED_UNKNOWN, /* 0 */
592 PCIE_SPEED_2_5GT, /* 1 */
593 PCIE_SPEED_5_0GT, /* 2 */
Matthew Wilcox9dfd97f2009-12-13 08:11:35 -0500594 PCIE_SPEED_8_0GT, /* 3 */
Matthew Wilcox3749c512009-12-13 08:11:32 -0500595 PCI_SPEED_UNKNOWN, /* 4 */
596 PCI_SPEED_UNKNOWN, /* 5 */
597 PCI_SPEED_UNKNOWN, /* 6 */
598 PCI_SPEED_UNKNOWN, /* 7 */
599 PCI_SPEED_UNKNOWN, /* 8 */
600 PCI_SPEED_UNKNOWN, /* 9 */
601 PCI_SPEED_UNKNOWN, /* A */
602 PCI_SPEED_UNKNOWN, /* B */
603 PCI_SPEED_UNKNOWN, /* C */
604 PCI_SPEED_UNKNOWN, /* D */
605 PCI_SPEED_UNKNOWN, /* E */
606 PCI_SPEED_UNKNOWN /* F */
607};
608
609void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
610{
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700611 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
Matthew Wilcox3749c512009-12-13 08:11:32 -0500612}
613EXPORT_SYMBOL_GPL(pcie_update_link_speed);
614
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500615static unsigned char agp_speeds[] = {
616 AGP_UNKNOWN,
617 AGP_1X,
618 AGP_2X,
619 AGP_4X,
620 AGP_8X
621};
622
623static enum pci_bus_speed agp_speed(int agp3, int agpstat)
624{
625 int index = 0;
626
627 if (agpstat & 4)
628 index = 3;
629 else if (agpstat & 2)
630 index = 2;
631 else if (agpstat & 1)
632 index = 1;
633 else
634 goto out;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700635
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500636 if (agp3) {
637 index += 2;
638 if (index == 5)
639 index = 0;
640 }
641
642 out:
643 return agp_speeds[index];
644}
645
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500646static void pci_set_bus_speed(struct pci_bus *bus)
647{
648 struct pci_dev *bridge = bus->self;
649 int pos;
650
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500651 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
652 if (!pos)
653 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
654 if (pos) {
655 u32 agpstat, agpcmd;
656
657 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
658 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
659
660 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
661 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
662 }
663
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500664 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
665 if (pos) {
666 u16 status;
667 enum pci_bus_speed max;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500668
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700669 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
670 &status);
671
672 if (status & PCI_X_SSTATUS_533MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500673 max = PCI_SPEED_133MHz_PCIX_533;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700674 } else if (status & PCI_X_SSTATUS_266MHZ) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500675 max = PCI_SPEED_133MHz_PCIX_266;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700676 } else if (status & PCI_X_SSTATUS_133MHZ) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400677 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500678 max = PCI_SPEED_133MHz_PCIX_ECC;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400679 else
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500680 max = PCI_SPEED_133MHz_PCIX;
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500681 } else {
682 max = PCI_SPEED_66MHz_PCIX;
683 }
684
685 bus->max_bus_speed = max;
Bjorn Helgaas7793eea2012-12-05 13:51:17 -0700686 bus->cur_bus_speed = pcix_bus_speed[
687 (status & PCI_X_SSTATUS_FREQ) >> 6];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500688
689 return;
690 }
691
Yijing Wangfdfe1512013-09-05 15:55:29 +0800692 if (pci_is_pcie(bridge)) {
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500693 u32 linkcap;
694 u16 linksta;
695
Jiang Liu59875ae2012-07-24 17:20:06 +0800696 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
Bjorn Helgaas231afea2012-12-05 13:51:18 -0700697 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500698
Jiang Liu59875ae2012-07-24 17:20:06 +0800699 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500700 pcie_update_link_speed(bus, linksta);
701 }
702}
703
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100704static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
705{
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100706 struct irq_domain *d;
707
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100708 /*
709 * Any firmware interface that can resolve the msi_domain
710 * should be called from here.
711 */
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100712 d = pci_host_bridge_of_msi_domain(bus);
Suravee Suthikulpanit471036b2015-12-10 08:55:27 -0800713 if (!d)
714 d = pci_host_bridge_acpi_msi_domain(bus);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100715
Jake Oshins788858e2016-02-16 21:56:22 +0000716#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
717 /*
718 * If no IRQ domain was found via the OF tree, try looking it up
719 * directly through the fwnode_handle.
720 */
721 if (!d) {
722 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
723
724 if (fwnode)
725 d = irq_find_matching_fwnode(fwnode,
726 DOMAIN_BUS_PCI_MSI);
727 }
728#endif
729
Marc Zyngierb165e2b2015-07-28 14:46:12 +0100730 return d;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100731}
732
733static void pci_set_bus_msi_domain(struct pci_bus *bus)
734{
735 struct irq_domain *d;
Alex Williamson38ea72b2015-09-18 15:08:54 -0600736 struct pci_bus *b;
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100737
738 /*
Alex Williamson38ea72b2015-09-18 15:08:54 -0600739 * The bus can be a root bus, a subordinate bus, or a virtual bus
740 * created by an SR-IOV device. Walk up to the first bridge device
741 * found or derive the domain from the host bridge.
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100742 */
Alex Williamson38ea72b2015-09-18 15:08:54 -0600743 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
744 if (b->self)
745 d = dev_get_msi_domain(&b->self->dev);
746 }
747
748 if (!d)
749 d = pci_host_bridge_msi_domain(b);
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100750
751 dev_set_msi_domain(&bus->dev, d);
752}
753
Lorenzo Pieralisicea9bc02017-06-28 15:13:55 -0500754static int pci_register_host_bridge(struct pci_host_bridge *bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100755{
756 struct device *parent = bridge->dev.parent;
757 struct resource_entry *window, *n;
758 struct pci_bus *bus, *b;
759 resource_size_t offset;
760 LIST_HEAD(resources);
761 struct resource *res;
762 char addr[64], *fmt;
763 const char *name;
764 int err;
765
766 bus = pci_alloc_bus(NULL);
767 if (!bus)
768 return -ENOMEM;
769
770 bridge->bus = bus;
771
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600772 /* Temporarily move resources off the list */
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100773 list_splice_init(&bridge->windows, &resources);
774 bus->sysdata = bridge->sysdata;
775 bus->msi = bridge->msi;
776 bus->ops = bridge->ops;
777 bus->number = bus->busn_res.start = bridge->busnr;
778#ifdef CONFIG_PCI_DOMAINS_GENERIC
779 bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
780#endif
781
782 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
783 if (b) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600784 /* Ignore it if we already got here via a different bridge */
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +0100785 dev_dbg(&b->dev, "bus already known\n");
786 err = -EEXIST;
787 goto free;
788 }
789
790 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
791 bridge->busnr);
792
793 err = pcibios_root_bridge_prepare(bridge);
794 if (err)
795 goto free;
796
797 err = device_register(&bridge->dev);
798 if (err)
799 put_device(&bridge->dev);
800
801 bus->bridge = get_device(&bridge->dev);
802 device_enable_async_suspend(bus->bridge);
803 pci_set_bus_of_node(bus);
804 pci_set_bus_msi_domain(bus);
805
806 if (!parent)
807 set_dev_node(bus->bridge, pcibus_to_node(bus));
808
809 bus->dev.class = &pcibus_class;
810 bus->dev.parent = bus->bridge;
811
812 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
813 name = dev_name(&bus->dev);
814
815 err = device_register(&bus->dev);
816 if (err)
817 goto unregister;
818
819 pcibios_add_bus(bus);
820
821 /* Create legacy_io and legacy_mem files for this bus */
822 pci_create_legacy_files(bus);
823
824 if (parent)
825 dev_info(parent, "PCI host bridge to bus %s\n", name);
826 else
827 pr_info("PCI host bridge to bus %s\n", name);
828
829 /* Add initial resources to the bus */
830 resource_list_for_each_entry_safe(window, n, &resources) {
831 list_move_tail(&window->node, &bridge->windows);
832 offset = window->offset;
833 res = window->res;
834
835 if (res->flags & IORESOURCE_BUS)
836 pci_bus_insert_busn_res(bus, bus->number, res->end);
837 else
838 pci_bus_add_resource(bus, res, 0);
839
840 if (offset) {
841 if (resource_type(res) == IORESOURCE_IO)
842 fmt = " (bus address [%#06llx-%#06llx])";
843 else
844 fmt = " (bus address [%#010llx-%#010llx])";
845
846 snprintf(addr, sizeof(addr), fmt,
847 (unsigned long long)(res->start - offset),
848 (unsigned long long)(res->end - offset));
849 } else
850 addr[0] = '\0';
851
852 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
853 }
854
855 down_write(&pci_bus_sem);
856 list_add_tail(&bus->node, &pci_root_buses);
857 up_write(&pci_bus_sem);
858
859 return 0;
860
861unregister:
862 put_device(&bridge->dev);
863 device_unregister(&bridge->dev);
864
865free:
866 kfree(bus);
867 return err;
868}
869
Adrian Bunkcbd4e052008-04-18 13:53:55 -0700870static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
871 struct pci_dev *bridge, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct pci_bus *child;
874 int i;
Yinghai Lu4f535092013-01-21 13:20:52 -0800875 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600877 /* Allocate a new bus and inherit stuff from the parent */
Catalin Marinas670ba0c2014-09-29 15:29:26 +0100878 child = pci_alloc_bus(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!child)
880 return NULL;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 child->parent = parent;
883 child->ops = parent->ops;
Thierry Reding0cbdcfc2013-08-09 22:27:08 +0200884 child->msi = parent->msi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 child->sysdata = parent->sysdata;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200886 child->bus_flags = parent->bus_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600888 /*
889 * Initialize some portions of the bus device, but don't register
890 * it now as the parent is not properly set up yet.
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400891 */
892 child->dev.class = &pcibus_class;
Kay Sievers1a927132008-10-30 02:17:49 +0100893 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600895 /* Set up the primary, secondary and subordinate bus numbers */
Yinghai Lub918c622012-05-17 18:51:11 -0700896 child->number = child->busn_res.start = busnr;
897 child->primary = parent->busn_res.start;
898 child->busn_res.end = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Yinghai Lu4f535092013-01-21 13:20:52 -0800900 if (!bridge) {
901 child->dev.parent = parent->bridge;
902 goto add_dev;
903 }
Yu Zhao3789fa82008-11-22 02:41:07 +0800904
905 child->self = bridge;
906 child->bridge = get_device(&bridge->dev);
Yinghai Lu4f535092013-01-21 13:20:52 -0800907 child->dev.parent = child->bridge;
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +1000908 pci_set_bus_of_node(child);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500909 pci_set_bus_speed(child);
910
Bjorn Helgaas3e466e22017-11-30 10:58:14 -0600911 /* Set up default resource pointers and names */
Yu Zhaofde09c62008-11-22 02:39:32 +0800912 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
914 child->resource[i]->name = child->name;
915 }
916 bridge->subordinate = child;
917
Yinghai Lu4f535092013-01-21 13:20:52 -0800918add_dev:
Marc Zyngier44aa0c62015-07-28 14:46:11 +0100919 pci_set_bus_msi_domain(child);
Yinghai Lu4f535092013-01-21 13:20:52 -0800920 ret = device_register(&child->dev);
921 WARN_ON(ret < 0);
922
Jiang Liu10a95742013-04-12 05:44:20 +0000923 pcibios_add_bus(child);
924
Thierry Reding057bd2e2016-02-09 15:30:47 +0100925 if (child->ops->add_bus) {
926 ret = child->ops->add_bus(child);
927 if (WARN_ON(ret < 0))
928 dev_err(&child->dev, "failed to add bus: %d\n", ret);
929 }
930
Yinghai Lu4f535092013-01-21 13:20:52 -0800931 /* Create legacy_io and legacy_mem files for this bus */
932 pci_create_legacy_files(child);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return child;
935}
936
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400937struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
938 int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct pci_bus *child;
941
942 child = pci_alloc_child_bus(parent, dev, busnr);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700943 if (child) {
Zhang Yanmind71374d2006-06-02 12:35:43 +0800944 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 list_add_tail(&child->node, &parent->children);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800946 up_write(&pci_bus_sem);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return child;
949}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600950EXPORT_SYMBOL(pci_add_new_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Rajat Jainf3dbd802014-09-02 16:26:00 -0700952static void pci_enable_crs(struct pci_dev *pdev)
953{
954 u16 root_cap = 0;
955
956 /* Enable CRS Software Visibility if supported */
957 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
958 if (root_cap & PCI_EXP_RTCAP_CRSVIS)
959 pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
960 PCI_EXP_RTCTL_CRSSVE);
961}
962
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300963static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
964 unsigned int available_buses);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/*
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300967 * pci_scan_bridge_extend() - Scan buses behind a bridge
968 * @bus: Parent bus the bridge is on
969 * @dev: Bridge itself
970 * @max: Starting subordinate number of buses behind this bridge
971 * @available_buses: Total number of buses available for this bridge and
972 * the devices below. After the minimal bus space has
973 * been allocated the remaining buses will be
974 * distributed equally between hotplug-capable bridges.
975 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
976 * that need to be reconfigured.
977 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * If it's a bridge, configure it and scan the bus behind it.
979 * For CardBus bridges, we don't scan behind as the devices will
980 * be handled by the bridge driver itself.
981 *
982 * We need to process bridges in two passes -- first we scan those
983 * already configured by the BIOS and after we are done with all of
984 * them, we proceed to assigning numbers to the remaining buses in
985 * order to avoid overlaps between old and new bus numbers.
986 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +0300987static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
988 int max, unsigned int available_buses,
989 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct pci_bus *child;
992 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
Dominik Brodowski49887942005-12-08 16:53:12 +0100993 u32 buses, i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 u16 bctl;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600995 u8 primary, secondary, subordinate;
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100996 int broken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Mika Westerbergd963f652016-06-02 11:17:13 +0300998 /*
999 * Make sure the bridge is powered on to be able to access config
1000 * space of devices below it.
1001 */
1002 pm_runtime_get_sync(&dev->dev);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001005 primary = buses & 0xFF;
1006 secondary = (buses >> 8) & 0xFF;
1007 subordinate = (buses >> 16) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Frederick Lawler7506dc72018-01-18 12:55:24 -06001009 pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001010 secondary, subordinate, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001012 if (!primary && (primary != bus->number) && secondary && subordinate) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001013 pci_warn(dev, "Primary bus is hard wired to 0\n");
Yinghai Lu71f6bd42012-01-30 12:25:24 +01001014 primary = bus->number;
1015 }
1016
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001017 /* Check if setup is sensible at all */
1018 if (!pass &&
Yinghai Lu1965f662012-09-10 17:19:33 -07001019 (primary != bus->number || secondary <= bus->number ||
Bjorn Helgaas12d87062014-09-19 11:08:40 -06001020 secondary > subordinate)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001021 pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
Yinghai Lu1965f662012-09-10 17:19:33 -07001022 secondary, subordinate);
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +11001023 broken = 1;
1024 }
1025
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001026 /*
1027 * Disable Master-Abort Mode during probing to avoid reporting of
1028 * bus errors in some architectures.
1029 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1031 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1032 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1033
Rajat Jainf3dbd802014-09-02 16:26:00 -07001034 pci_enable_crs(dev);
1035
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001036 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1037 !is_cardbus && !broken) {
1038 unsigned int cmax;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001041 * Bus already configured by firmware, process it in the
1042 * first pass and just note the configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044 if (pass)
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001048 * The bus might already exist for two reasons: Either we
1049 * are rescanning the bus or the bus is reachable through
1050 * more than one bridge. The second case can happen with
1051 * the i450NX chipset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001053 child = pci_find_bus(pci_domain_nr(bus), secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001054 if (!child) {
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001055 child = pci_add_new_bus(bus, dev, secondary);
Alex Chiang74710de2009-03-20 14:56:10 -06001056 if (!child)
1057 goto out;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -06001058 child->primary = primary;
Yinghai Lubc76b732012-05-17 18:51:13 -07001059 pci_bus_insert_busn_res(child, secondary, subordinate);
Alex Chiang74710de2009-03-20 14:56:10 -06001060 child->bridge_ctl = bctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 cmax = pci_scan_child_bus(child);
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001064 if (cmax > subordinate)
Frederick Lawler7506dc72018-01-18 12:55:24 -06001065 pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001066 subordinate, cmax);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001067
1068 /* Subordinate should equal child->busn_res.end */
Andreas Noeverc95b0bd2014-01-23 21:59:27 +01001069 if (subordinate > max)
1070 max = subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 } else {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * We need to assign a number to this bus which we always
1075 * do in the second pass.
1076 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001077 if (!pass) {
Andreas Noever619c8c32014-01-23 21:59:23 +01001078 if (pcibios_assign_all_busses() || broken || is_cardbus)
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001079
1080 /*
1081 * Temporarily disable forwarding of the
1082 * configuration cycles on all bridges in
1083 * this bus segment to avoid possible
1084 * conflicts in the second pass between two
1085 * bridges programmed with overlapping bus
1086 * ranges.
1087 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001088 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1089 buses & ~0xffffff);
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001090 goto out;
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -07001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /* Clear errors */
1094 pci_write_config_word(dev, PCI_STATUS, 0xffff);
1095
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001096 /*
1097 * Prevent assigning a bus number that already exists.
1098 * This can happen when a bridge is hot-plugged, so in this
1099 * case we only re-scan this bus.
1100 */
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001101 child = pci_find_bus(pci_domain_nr(bus), max+1);
1102 if (!child) {
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001103 child = pci_add_new_bus(bus, dev, max+1);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001104 if (!child)
1105 goto out;
Mika Westerberga20c7f32017-10-13 21:35:43 +03001106 pci_bus_insert_busn_res(child, max+1,
1107 bus->busn_res.end);
Tiejun Chenb1a98b62011-06-02 11:02:50 +08001108 }
Andreas Noever9a4d7d82014-01-23 21:59:21 +01001109 max++;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001110 if (available_buses)
1111 available_buses--;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 buses = (buses & 0xff000000)
1114 | ((unsigned int)(child->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -07001115 | ((unsigned int)(child->busn_res.start) << 8)
1116 | ((unsigned int)(child->busn_res.end) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /*
1119 * yenta.c forces a secondary latency timer of 176.
1120 * Copy that behaviour here.
1121 */
1122 if (is_cardbus) {
1123 buses &= ~0xff000000;
1124 buses |= CARDBUS_LATENCY_TIMER << 24;
1125 }
Jesper Juhl7c867c82011-01-24 21:14:33 +01001126
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001127 /* We need to blast all three values with a single write */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1129
1130 if (!is_cardbus) {
Gary Hade11949252007-10-08 16:24:16 -07001131 child->bridge_ctl = bctl;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001132 max = pci_scan_child_bus_extend(child, available_buses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 } else {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001136 * For CardBus bridges, we leave 4 bus numbers as
1137 * cards with a PCI-to-PCI bridge can be inserted
1138 * later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001140 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001141 struct pci_bus *parent = bus;
Rajesh Shahcc574502005-04-28 00:25:47 -07001142 if (pci_find_bus(pci_domain_nr(bus),
1143 max+i+1))
1144 break;
Dominik Brodowski49887942005-12-08 16:53:12 +01001145 while (parent->parent) {
1146 if ((!pcibios_assign_all_busses()) &&
Yinghai Lub918c622012-05-17 18:51:11 -07001147 (parent->busn_res.end > max) &&
1148 (parent->busn_res.end <= max+i)) {
Dominik Brodowski49887942005-12-08 16:53:12 +01001149 j = 1;
1150 }
1151 parent = parent->parent;
1152 }
1153 if (j) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001154
Dominik Brodowski49887942005-12-08 16:53:12 +01001155 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001156 * Often, there are two CardBus
1157 * bridges -- try to leave one
1158 * valid bus number for each one.
Dominik Brodowski49887942005-12-08 16:53:12 +01001159 */
1160 i /= 2;
1161 break;
1162 }
1163 }
Rajesh Shahcc574502005-04-28 00:25:47 -07001164 max += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001166
1167 /* Set subordinate bus number to its real value */
Yinghai Lubc76b732012-05-17 18:51:13 -07001168 pci_bus_update_busn_res_end(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1170 }
1171
Gary Hadecb3576f2008-02-08 14:00:52 -08001172 sprintf(child->name,
1173 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1174 pci_domain_nr(bus), child->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Bernhard Kaindld55bef512007-07-30 20:35:13 +02001176 /* Has only triggered on CardBus, fixup is in yenta_socket */
Dominik Brodowski49887942005-12-08 16:53:12 +01001177 while (bus->parent) {
Yinghai Lub918c622012-05-17 18:51:11 -07001178 if ((child->busn_res.end > bus->busn_res.end) ||
1179 (child->number > bus->busn_res.end) ||
Dominik Brodowski49887942005-12-08 16:53:12 +01001180 (child->number < bus->number) ||
Yinghai Lub918c622012-05-17 18:51:11 -07001181 (child->busn_res.end < bus->number)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001182 dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
Yinghai Lub918c622012-05-17 18:51:11 -07001183 &child->busn_res,
1184 (bus->number > child->busn_res.end &&
1185 bus->busn_res.end < child->number) ?
Joe Perchesa6f29a92007-11-19 17:48:29 -08001186 "wholly" : "partially",
1187 bus->self->transparent ? " transparent" : "",
Bjorn Helgaas865df572009-11-04 10:32:57 -07001188 dev_name(&bus->dev),
Yinghai Lub918c622012-05-17 18:51:11 -07001189 &bus->busn_res);
Dominik Brodowski49887942005-12-08 16:53:12 +01001190 }
1191 bus = bus->parent;
1192 }
1193
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +00001194out:
1195 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1196
Mika Westerbergd963f652016-06-02 11:17:13 +03001197 pm_runtime_put(&dev->dev);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return max;
1200}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03001201
1202/*
1203 * pci_scan_bridge() - Scan buses behind a bridge
1204 * @bus: Parent bus the bridge is on
1205 * @dev: Bridge itself
1206 * @max: Starting subordinate number of buses behind this bridge
1207 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1208 * that need to be reconfigured.
1209 *
1210 * If it's a bridge, configure it and scan the bus behind it.
1211 * For CardBus bridges, we don't scan behind as the devices will
1212 * be handled by the bridge driver itself.
1213 *
1214 * We need to process bridges in two passes -- first we scan those
1215 * already configured by the BIOS and after we are done with all of
1216 * them, we proceed to assigning numbers to the remaining buses in
1217 * order to avoid overlaps between old and new bus numbers.
1218 */
1219int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1220{
1221 return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1222}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06001223EXPORT_SYMBOL(pci_scan_bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225/*
1226 * Read interrupt line and base address registers.
1227 * The architecture-dependent code can tweak these, of course.
1228 */
1229static void pci_read_irq(struct pci_dev *dev)
1230{
1231 unsigned char irq;
1232
1233 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
Kristen Accardiffeff782005-11-02 16:24:32 -08001234 dev->pin = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (irq)
1236 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1237 dev->irq = irq;
1238}
1239
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001240void set_pcie_port_type(struct pci_dev *pdev)
Yu Zhao480b93b2009-03-20 11:25:14 +08001241{
1242 int pos;
1243 u16 reg16;
Yijing Wangd0751b92015-05-21 15:05:02 +08001244 int type;
1245 struct pci_dev *parent;
Yu Zhao480b93b2009-03-20 11:25:14 +08001246
1247 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1248 if (!pos)
1249 return;
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001250
Kenji Kaneshige0efea002009-11-05 12:05:11 +09001251 pdev->pcie_cap = pos;
Yu Zhao480b93b2009-03-20 11:25:14 +08001252 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
Yijing Wang786e2282012-07-24 17:20:02 +08001253 pdev->pcie_flags_reg = reg16;
Jon Masonb03e7492011-07-20 15:20:54 -05001254 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
1255 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
Yijing Wangd0751b92015-05-21 15:05:02 +08001256
1257 /*
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001258 * A Root Port or a PCI-to-PCIe bridge is always the upstream end
1259 * of a Link. No PCIe component has two Links. Two Links are
1260 * connected by a Switch that has a Port on each Link and internal
1261 * logic to connect the two Ports.
Yijing Wangd0751b92015-05-21 15:05:02 +08001262 */
1263 type = pci_pcie_type(pdev);
Bjorn Helgaas51ebfc92017-01-11 09:11:53 -06001264 if (type == PCI_EXP_TYPE_ROOT_PORT ||
1265 type == PCI_EXP_TYPE_PCIE_BRIDGE)
Yijing Wangd0751b92015-05-21 15:05:02 +08001266 pdev->has_secondary_link = 1;
1267 else if (type == PCI_EXP_TYPE_UPSTREAM ||
1268 type == PCI_EXP_TYPE_DOWNSTREAM) {
1269 parent = pci_upstream_bridge(pdev);
Yijing Wangb35b1df2015-08-17 18:47:58 +08001270
1271 /*
1272 * Usually there's an upstream device (Root Port or Switch
1273 * Downstream Port), but we can't assume one exists.
1274 */
1275 if (parent && !parent->has_secondary_link)
Yijing Wangd0751b92015-05-21 15:05:02 +08001276 pdev->has_secondary_link = 1;
1277 }
Yu Zhao480b93b2009-03-20 11:25:14 +08001278}
1279
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +00001280void set_pcie_hotplug_bridge(struct pci_dev *pdev)
Eric W. Biederman28760482009-09-09 14:09:24 -07001281{
Eric W. Biederman28760482009-09-09 14:09:24 -07001282 u32 reg32;
1283
Jiang Liu59875ae2012-07-24 17:20:06 +08001284 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
Eric W. Biederman28760482009-09-09 14:09:24 -07001285 if (reg32 & PCI_EXP_SLTCAP_HPC)
1286 pdev->is_hotplug_bridge = 1;
1287}
1288
Lukas Wunner8531e282017-03-10 21:23:45 +01001289static void set_pcie_thunderbolt(struct pci_dev *dev)
1290{
1291 int vsec = 0;
1292 u32 header;
1293
1294 while ((vsec = pci_find_next_ext_capability(dev, vsec,
1295 PCI_EXT_CAP_ID_VNDR))) {
1296 pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
1297
1298 /* Is the device part of a Thunderbolt controller? */
1299 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
1300 PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
1301 dev->is_thunderbolt = 1;
1302 return;
1303 }
1304 }
1305}
1306
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001307/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001308 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
Alex Williamson78916b02014-05-05 14:20:51 -06001309 * @dev: PCI device
1310 *
1311 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1312 * when forwarding a type1 configuration request the bridge must check that
1313 * the extended register address field is zero. The bridge is not permitted
1314 * to forward the transactions and must handle it as an Unsupported Request.
1315 * Some bridges do not follow this rule and simply drop the extended register
1316 * bits, resulting in the standard config space being aliased, every 256
1317 * bytes across the entire configuration space. Test for this condition by
1318 * comparing the first dword of each potential alias to the vendor/device ID.
1319 * Known offenders:
1320 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1321 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1322 */
1323static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1324{
1325#ifdef CONFIG_PCI_QUIRKS
1326 int pos;
1327 u32 header, tmp;
1328
1329 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1330
1331 for (pos = PCI_CFG_SPACE_SIZE;
1332 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1333 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1334 || header != tmp)
1335 return false;
1336 }
1337
1338 return true;
1339#else
1340 return false;
1341#endif
1342}
1343
1344/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001345 * pci_cfg_space_size - Get the configuration space size of the PCI device
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001346 * @dev: PCI device
1347 *
1348 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1349 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1350 * access it. Maybe we don't have a way to generate extended config space
1351 * accesses, or the device is behind a reverse Express bridge. So we try
1352 * reading the dword at 0x100 which must either be 0 or a valid extended
1353 * capability header.
1354 */
1355static int pci_cfg_space_size_ext(struct pci_dev *dev)
1356{
1357 u32 status;
1358 int pos = PCI_CFG_SPACE_SIZE;
1359
1360 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001361 return PCI_CFG_SPACE_SIZE;
Alex Williamson78916b02014-05-05 14:20:51 -06001362 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001363 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001364
1365 return PCI_CFG_SPACE_EXP_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001366}
1367
1368int pci_cfg_space_size(struct pci_dev *dev)
1369{
1370 int pos;
1371 u32 status;
1372 u16 class;
1373
1374 class = dev->class >> 8;
1375 if (class == PCI_CLASS_BRIDGE_HOST)
1376 return pci_cfg_space_size_ext(dev);
1377
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001378 if (pci_is_pcie(dev))
1379 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001380
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001381 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1382 if (!pos)
1383 return PCI_CFG_SPACE_SIZE;
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001384
Bjorn Helgaas8e5a3952015-12-07 18:21:10 -06001385 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1386 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1387 return pci_cfg_space_size_ext(dev);
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001388
Stephen Hemminger0b950f02014-01-10 17:14:48 -07001389 return PCI_CFG_SPACE_SIZE;
1390}
1391
Bartlomiej Zolnierkiewicz01abc2a2007-04-23 23:19:36 +02001392#define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
Randy Dunlap76e6a1d2006-12-29 16:47:29 -08001393
Guilherme G. Piccolie80e7ed2015-10-21 12:17:35 -02001394static void pci_msi_setup_pci_dev(struct pci_dev *dev)
Michael S. Tsirkin18516172015-05-07 09:52:21 -05001395{
1396 /*
1397 * Disable the MSI hardware to avoid screaming interrupts
1398 * during boot. This is the power on reset default so
1399 * usually this should be a noop.
1400 */
1401 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1402 if (dev->msi_cap)
1403 pci_msi_set_enable(dev, 0);
1404
1405 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1406 if (dev->msix_cap)
1407 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001411 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
Piotr Gregor99b3c582017-05-26 22:02:25 +01001412 * @dev: PCI device
1413 *
1414 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
1415 * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1416 */
1417static int pci_intx_mask_broken(struct pci_dev *dev)
1418{
1419 u16 orig, toggle, new;
1420
1421 pci_read_config_word(dev, PCI_COMMAND, &orig);
1422 toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1423 pci_write_config_word(dev, PCI_COMMAND, toggle);
1424 pci_read_config_word(dev, PCI_COMMAND, &new);
1425
1426 pci_write_config_word(dev, PCI_COMMAND, orig);
1427
1428 /*
1429 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1430 * r2.3, so strictly speaking, a device is not *broken* if it's not
1431 * writable. But we'll live with the misnomer for now.
1432 */
1433 if (new != toggle)
1434 return 1;
1435 return 0;
1436}
1437
1438/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001439 * pci_setup_device - Fill in class and map information of a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * @dev: the device structure to fill
1441 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001442 * Initialize the device structure with information about the device's
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001443 * vendor,class,memory and IO-space addresses, IRQ lines etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * Called at initialisation of the PCI subsystem and by CardBus services.
Yu Zhao480b93b2009-03-20 11:25:14 +08001445 * Returns 0 on success and negative if unknown type of device (not normal,
1446 * bridge or CardBus).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001448int pci_setup_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 u32 class;
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001451 u16 cmd;
Yu Zhao480b93b2009-03-20 11:25:14 +08001452 u8 hdr_type;
Gabe Blackbc577d22009-10-06 10:45:19 -05001453 int pos = 0;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001454 struct pci_bus_region region;
1455 struct resource *res;
Yu Zhao480b93b2009-03-20 11:25:14 +08001456
1457 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
1458 return -EIO;
1459
1460 dev->sysdata = dev->bus->sysdata;
1461 dev->dev.parent = dev->bus->bridge;
1462 dev->dev.bus = &pci_bus_type;
1463 dev->hdr_type = hdr_type & 0x7f;
1464 dev->multifunction = !!(hdr_type & 0x80);
Yu Zhao480b93b2009-03-20 11:25:14 +08001465 dev->error_state = pci_channel_io_normal;
1466 set_pcie_port_type(dev);
1467
Yijing Wang017ffe62015-07-17 17:16:32 +08001468 pci_dev_assign_slot(dev);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001469
1470 /*
1471 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1472 * set this higher, assuming the system even supports it.
1473 */
Yu Zhao480b93b2009-03-20 11:25:14 +08001474 dev->dma_mask = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Greg Kroah-Hartmaneebfcfb2008-07-02 13:24:49 -07001476 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1477 dev->bus->number, PCI_SLOT(dev->devfn),
1478 PCI_FUNC(dev->devfn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
Auke Kokb8a3a522007-06-08 15:46:30 -07001481 dev->revision = class & 0xff;
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001482 dev->class = class >> 8; /* upper 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Frederick Lawler7506dc72018-01-18 12:55:24 -06001484 pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n",
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001485 dev->vendor, dev->device, dev->hdr_type, dev->class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001487 /* Need to have dev->class ready */
Yu Zhao853346e2009-03-21 22:05:11 +08001488 dev->cfg_size = pci_cfg_space_size(dev);
1489
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001490 /* Need to have dev->cfg_size ready */
Lukas Wunner8531e282017-03-10 21:23:45 +01001491 set_pcie_thunderbolt(dev);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /* "Unknown power state" */
Daniel Ritz3fe9d192005-08-17 15:32:19 -07001494 dev->current_state = PCI_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /* Early fixups, before probing the BARs */
1497 pci_fixup_device(pci_fixup_early, dev);
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001498
1499 /* Device class may be changed after fixup */
Yu Zhaof79b1b12009-05-28 00:25:05 +08001500 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001502 if (dev->non_compliant_bars) {
1503 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1504 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001505 pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
Bjorn Helgaasb84106b2016-02-25 14:35:57 -06001506 cmd &= ~PCI_COMMAND_IO;
1507 cmd &= ~PCI_COMMAND_MEMORY;
1508 pci_write_config_word(dev, PCI_COMMAND, cmd);
1509 }
1510 }
1511
Piotr Gregor99b3c582017-05-26 22:02:25 +01001512 dev->broken_intx_masking = pci_intx_mask_broken(dev);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 switch (dev->hdr_type) { /* header type */
1515 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1516 if (class == PCI_CLASS_BRIDGE_PCI)
1517 goto bad;
1518 pci_read_irq(dev);
1519 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
1520 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1521 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
Alan Cox368c73d2006-10-04 00:41:26 +01001522
1523 /*
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001524 * Do the ugly legacy mode stuff here rather than broken chip
1525 * quirk code. Legacy mode ATA controllers have fixed
1526 * addresses. These are not always echoed in BAR0-3, and
1527 * BAR0-3 in a few cases contain junk!
Alan Cox368c73d2006-10-04 00:41:26 +01001528 */
1529 if (class == PCI_CLASS_STORAGE_IDE) {
1530 u8 progif;
1531 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1532 if ((progif & 1) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001533 region.start = 0x1F0;
1534 region.end = 0x1F7;
1535 res = &dev->resource[0];
1536 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001537 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001538 pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001539 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001540 region.start = 0x3F6;
1541 region.end = 0x3F6;
1542 res = &dev->resource[1];
1543 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001544 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001545 pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001546 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001547 }
1548 if ((progif & 4) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001549 region.start = 0x170;
1550 region.end = 0x177;
1551 res = &dev->resource[2];
1552 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001553 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001554 pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001555 res);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001556 region.start = 0x376;
1557 region.end = 0x376;
1558 res = &dev->resource[3];
1559 res->flags = LEGACY_IO_RESOURCE;
Yinghai Lufc279852013-12-09 22:54:40 -08001560 pcibios_bus_to_resource(dev->bus, res, &region);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001561 pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n",
Bjorn Helgaas075eb9e2014-03-05 14:07:03 -07001562 res);
Alan Cox368c73d2006-10-04 00:41:26 +01001563 }
1564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566
1567 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1568 if (class != PCI_CLASS_BRIDGE_PCI)
1569 goto bad;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001570
1571 /*
1572 * The PCI-to-PCI bridge spec requires that subtractive
1573 * decoding (i.e. transparent) bridge must have programming
1574 * interface code of 0x01.
1575 */
Kristen Accardi3efd2732005-11-02 16:55:49 -08001576 pci_read_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 dev->transparent = ((dev->class & 0xff) == 1);
1578 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
Eric W. Biederman28760482009-09-09 14:09:24 -07001579 set_pcie_hotplug_bridge(dev);
Gabe Blackbc577d22009-10-06 10:45:19 -05001580 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1581 if (pos) {
1582 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1583 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 break;
1586
1587 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1588 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1589 goto bad;
1590 pci_read_irq(dev);
1591 pci_read_bases(dev, 1, 0);
1592 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1593 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1594 break;
1595
1596 default: /* unknown header */
Frederick Lawler7506dc72018-01-18 12:55:24 -06001597 pci_err(dev, "unknown header type %02x, ignoring device\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001598 dev->hdr_type);
Yu Zhao480b93b2009-03-20 11:25:14 +08001599 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 bad:
Frederick Lawler7506dc72018-01-18 12:55:24 -06001602 pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001603 dev->class, dev->hdr_type);
Bjorn Helgaas2b4aed12015-06-19 16:20:58 -05001604 dev->class = PCI_CLASS_NOT_DEFINED << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
1607 /* We found a fine healthy device, go go go... */
1608 return 0;
1609}
1610
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001611static void pci_configure_mps(struct pci_dev *dev)
1612{
1613 struct pci_dev *bridge = pci_upstream_bridge(dev);
Keith Busch27d868b2015-08-24 08:48:16 -05001614 int mps, p_mps, rc;
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001615
1616 if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
1617 return;
1618
1619 mps = pcie_get_mps(dev);
1620 p_mps = pcie_get_mps(bridge);
1621
1622 if (mps == p_mps)
1623 return;
1624
1625 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001626 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 -05001627 mps, pci_name(bridge), p_mps);
1628 return;
1629 }
Keith Busch27d868b2015-08-24 08:48:16 -05001630
1631 /*
1632 * Fancier MPS configuration is done later by
1633 * pcie_bus_configure_settings()
1634 */
1635 if (pcie_bus_config != PCIE_BUS_DEFAULT)
1636 return;
1637
1638 rc = pcie_set_mps(dev, p_mps);
1639 if (rc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001640 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 -05001641 p_mps);
1642 return;
1643 }
1644
Frederick Lawler7506dc72018-01-18 12:55:24 -06001645 pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
Keith Busch27d868b2015-08-24 08:48:16 -05001646 p_mps, mps, 128 << dev->pcie_mpss);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001647}
1648
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001649static struct hpp_type0 pci_default_type0 = {
1650 .revision = 1,
1651 .cache_line_size = 8,
1652 .latency_timer = 0x40,
1653 .enable_serr = 0,
1654 .enable_perr = 0,
1655};
1656
1657static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1658{
1659 u16 pci_cmd, pci_bctl;
1660
Bjorn Helgaasc6285fc2014-08-29 18:10:19 -06001661 if (!hpp)
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001662 hpp = &pci_default_type0;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001663
1664 if (hpp->revision > 1) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001665 pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001666 hpp->revision);
1667 hpp = &pci_default_type0;
1668 }
1669
1670 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1671 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1672 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1673 if (hpp->enable_serr)
1674 pci_cmd |= PCI_COMMAND_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001675 if (hpp->enable_perr)
1676 pci_cmd |= PCI_COMMAND_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001677 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1678
1679 /* Program bridge control value */
1680 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1681 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1682 hpp->latency_timer);
1683 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1684 if (hpp->enable_serr)
1685 pci_bctl |= PCI_BRIDGE_CTL_SERR;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001686 if (hpp->enable_perr)
1687 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001688 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1689 }
1690}
1691
1692static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1693{
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001694 int pos;
1695
1696 if (!hpp)
1697 return;
1698
1699 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1700 if (!pos)
1701 return;
1702
Frederick Lawler7506dc72018-01-18 12:55:24 -06001703 pci_warn(dev, "PCI-X settings not supported\n");
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001704}
1705
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001706static bool pcie_root_rcb_set(struct pci_dev *dev)
1707{
1708 struct pci_dev *rp = pcie_find_root_port(dev);
1709 u16 lnkctl;
1710
1711 if (!rp)
1712 return false;
1713
1714 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
1715 if (lnkctl & PCI_EXP_LNKCTL_RCB)
1716 return true;
1717
1718 return false;
1719}
1720
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001721static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1722{
1723 int pos;
1724 u32 reg32;
1725
1726 if (!hpp)
1727 return;
1728
Bjorn Helgaas977509f2017-01-02 14:04:24 -06001729 if (!pci_is_pcie(dev))
1730 return;
1731
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001732 if (hpp->revision > 1) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001733 pci_warn(dev, "PCIe settings rev %d not supported\n",
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001734 hpp->revision);
1735 return;
1736 }
1737
Bjorn Helgaas302328c2014-09-03 13:26:29 -06001738 /*
1739 * Don't allow _HPX to change MPS or MRRS settings. We manage
1740 * those to make sure they're consistent with the rest of the
1741 * platform.
1742 */
1743 hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
1744 PCI_EXP_DEVCTL_READRQ;
1745 hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
1746 PCI_EXP_DEVCTL_READRQ);
1747
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001748 /* Initialize Device Control Register */
1749 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1750 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1751
1752 /* Initialize Link Control Register */
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001753 if (pcie_cap_has_lnkctl(dev)) {
1754
1755 /*
1756 * If the Root Port supports Read Completion Boundary of
1757 * 128, set RCB to 128. Otherwise, clear it.
1758 */
1759 hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
1760 hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
1761 if (pcie_root_rcb_set(dev))
1762 hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
1763
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001764 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1765 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
Johannes Thumshirne42010d2016-11-23 10:56:28 -06001766 }
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001767
1768 /* Find Advanced Error Reporting Enhanced Capability */
1769 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1770 if (!pos)
1771 return;
1772
1773 /* Initialize Uncorrectable Error Mask Register */
1774 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1775 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1776 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1777
1778 /* Initialize Uncorrectable Error Severity Register */
1779 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1780 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1781 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1782
1783 /* Initialize Correctable Error Mask Register */
1784 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1785 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1786 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1787
1788 /* Initialize Advanced Error Capabilities and Control Register */
1789 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1790 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001791
Bjorn Helgaas675734b2017-03-21 13:01:30 -05001792 /* Don't enable ECRC generation or checking if unsupported */
1793 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
1794 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
1795 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
1796 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
Bjorn Helgaas589fcc22014-09-12 20:02:00 -06001797 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1798
1799 /*
1800 * FIXME: The following two registers are not supported yet.
1801 *
1802 * o Secondary Uncorrectable Error Severity Register
1803 * o Secondary Uncorrectable Error Mask Register
1804 */
1805}
1806
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001807int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
Sinan Kaya60db3a42017-01-20 09:16:51 -05001808{
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001809 struct pci_host_bridge *host;
1810 u32 cap;
1811 u16 ctl;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001812 int ret;
1813
1814 if (!pci_is_pcie(dev))
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001815 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001816
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001817 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
Sinan Kaya60db3a42017-01-20 09:16:51 -05001818 if (ret)
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001819 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001820
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001821 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
1822 return 0;
1823
1824 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
1825 if (ret)
1826 return 0;
1827
1828 host = pci_find_host_bridge(dev->bus);
1829 if (!host)
1830 return 0;
1831
1832 /*
1833 * If some device in the hierarchy doesn't handle Extended Tags
1834 * correctly, make sure they're disabled.
1835 */
1836 if (host->no_ext_tags) {
1837 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001838 pci_info(dev, "disabling Extended Tags\n");
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001839 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1840 PCI_EXP_DEVCTL_EXT_TAG);
1841 }
1842 return 0;
1843 }
1844
1845 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001846 pci_info(dev, "enabling Extended Tags\n");
Sinan Kaya60db3a42017-01-20 09:16:51 -05001847 pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
1848 PCI_EXP_DEVCTL_EXT_TAG);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001849 }
1850 return 0;
Sinan Kaya60db3a42017-01-20 09:16:51 -05001851}
1852
dingtianhonga99b6462017-08-15 11:23:23 +08001853/**
1854 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
1855 * @dev: PCI device to query
1856 *
1857 * Returns true if the device has enabled relaxed ordering attribute.
1858 */
1859bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
1860{
1861 u16 v;
1862
1863 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
1864
1865 return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
1866}
1867EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
1868
1869static void pci_configure_relaxed_ordering(struct pci_dev *dev)
1870{
1871 struct pci_dev *root;
1872
1873 /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
1874 if (dev->is_virtfn)
1875 return;
1876
1877 if (!pcie_relaxed_ordering_enabled(dev))
1878 return;
1879
1880 /*
1881 * For now, we only deal with Relaxed Ordering issues with Root
1882 * Ports. Peer-to-Peer DMA is another can of worms.
1883 */
1884 root = pci_find_pcie_root_port(dev);
1885 if (!root)
1886 return;
1887
1888 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
1889 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1890 PCI_EXP_DEVCTL_RELAX_EN);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001891 pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
dingtianhonga99b6462017-08-15 11:23:23 +08001892 }
1893}
1894
Bjorn Helgaasc46fd352017-11-28 16:43:50 -06001895static void pci_configure_ltr(struct pci_dev *dev)
1896{
1897#ifdef CONFIG_PCIEASPM
1898 u32 cap;
1899 struct pci_dev *bridge;
1900
1901 if (!pci_is_pcie(dev))
1902 return;
1903
1904 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
1905 if (!(cap & PCI_EXP_DEVCAP2_LTR))
1906 return;
1907
1908 /*
1909 * Software must not enable LTR in an Endpoint unless the Root
1910 * Complex and all intermediate Switches indicate support for LTR.
1911 * PCIe r3.1, sec 6.18.
1912 */
1913 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
1914 dev->ltr_path = 1;
1915 else {
1916 bridge = pci_upstream_bridge(dev);
1917 if (bridge && bridge->ltr_path)
1918 dev->ltr_path = 1;
1919 }
1920
1921 if (dev->ltr_path)
1922 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
1923 PCI_EXP_DEVCTL2_LTR_EN);
1924#endif
1925}
1926
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001927static void pci_configure_device(struct pci_dev *dev)
1928{
1929 struct hotplug_params hpp;
1930 int ret;
1931
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001932 pci_configure_mps(dev);
Sinan Kaya62ce94a2017-07-12 00:04:14 -04001933 pci_configure_extended_tags(dev, NULL);
dingtianhonga99b6462017-08-15 11:23:23 +08001934 pci_configure_relaxed_ordering(dev);
Bjorn Helgaasc46fd352017-11-28 16:43:50 -06001935 pci_configure_ltr(dev);
Bjorn Helgaas9dae3a92015-08-20 16:08:27 -05001936
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06001937 memset(&hpp, 0, sizeof(hpp));
1938 ret = pci_get_hp_params(dev, &hpp);
1939 if (ret)
1940 return;
1941
1942 program_hpp_type2(dev, hpp.t2);
1943 program_hpp_type1(dev, hpp.t1);
1944 program_hpp_type0(dev, hpp.t0);
1945}
1946
Zhao, Yu201de562008-10-13 19:49:55 +08001947static void pci_release_capabilities(struct pci_dev *dev)
1948{
1949 pci_vpd_release(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08001950 pci_iov_release(dev);
Yinghai Luf7968412012-02-11 00:18:30 -08001951 pci_free_cap_save_buffers(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001955 * pci_release_dev - Free a PCI device structure when all users of it are
1956 * finished
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * @dev: device that's been disconnected
1958 *
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06001959 * Will be called only by the device core when all users of this PCI device are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * done.
1961 */
1962static void pci_release_dev(struct device *dev)
1963{
Rafael J. Wysocki04480092014-02-01 15:38:29 +01001964 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Rafael J. Wysocki04480092014-02-01 15:38:29 +01001966 pci_dev = to_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001967 pci_release_capabilities(pci_dev);
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10001968 pci_release_of_node(pci_dev);
Sebastian Ott6ae32c52013-06-04 19:18:14 +02001969 pcibios_release_device(pci_dev);
Gu Zheng8b1fce02013-05-25 21:48:31 +08001970 pci_bus_put(pci_dev->bus);
Alex Williamson782a9852014-05-20 08:53:21 -06001971 kfree(pci_dev->driver_override);
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001972 kfree(pci_dev->dma_alias_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 kfree(pci_dev);
1974}
1975
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001976struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
Michael Ellerman65891212007-04-05 17:19:08 +10001977{
1978 struct pci_dev *dev;
1979
1980 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
1981 if (!dev)
1982 return NULL;
1983
Michael Ellerman65891212007-04-05 17:19:08 +10001984 INIT_LIST_HEAD(&dev->bus_list);
Brian King88e7b162013-04-08 03:05:07 +00001985 dev->dev.type = &pci_dev_type;
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001986 dev->bus = pci_bus_get(bus);
Michael Ellerman65891212007-04-05 17:19:08 +10001987
1988 return dev;
1989}
Gu Zheng3c6e6ae2013-05-25 21:48:30 +08001990EXPORT_SYMBOL(pci_alloc_dev);
1991
Sinan Kaya62bc6a62017-08-29 14:45:44 -05001992static bool pci_bus_crs_vendor_id(u32 l)
1993{
1994 return (l & 0xffff) == 0x0001;
1995}
1996
Sinan Kaya6a802ef2017-08-29 14:45:44 -05001997static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
1998 int timeout)
Yinghai Luefdc87d2012-01-27 10:55:10 -08001999{
2000 int delay = 1;
2001
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002002 if (!pci_bus_crs_vendor_id(*l))
2003 return true; /* not a CRS completion */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002004
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002005 if (!timeout)
2006 return false; /* CRS, but caller doesn't want to wait */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002007
Rajat Jain89665a62014-09-08 14:19:49 -07002008 /*
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002009 * We got the reserved Vendor ID that indicates a completion with
2010 * Configuration Request Retry Status (CRS). Retry until we get a
2011 * valid Vendor ID or we time out.
Rajat Jain89665a62014-09-08 14:19:49 -07002012 */
Sinan Kaya62bc6a62017-08-29 14:45:44 -05002013 while (pci_bus_crs_vendor_id(*l)) {
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002014 if (delay > timeout) {
Sinan Kayae78e6612017-08-29 14:45:45 -05002015 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
2016 pci_domain_nr(bus), bus->number,
2017 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2018
Yinghai Luefdc87d2012-01-27 10:55:10 -08002019 return false;
2020 }
Sinan Kayae78e6612017-08-29 14:45:45 -05002021 if (delay >= 1000)
2022 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
2023 pci_domain_nr(bus), bus->number,
2024 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
Bjorn Helgaas9f982752017-08-29 14:45:43 -05002025
2026 msleep(delay);
2027 delay *= 2;
2028
2029 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2030 return false;
Yinghai Luefdc87d2012-01-27 10:55:10 -08002031 }
2032
Sinan Kayae78e6612017-08-29 14:45:45 -05002033 if (delay >= 1000)
2034 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
2035 pci_domain_nr(bus), bus->number,
2036 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2037
Yinghai Luefdc87d2012-01-27 10:55:10 -08002038 return true;
2039}
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002040
2041bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2042 int timeout)
2043{
Yinghai Luefdc87d2012-01-27 10:55:10 -08002044 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2045 return false;
2046
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002047 /* Some broken boards return 0 or ~0 if a slot is empty: */
Yinghai Luefdc87d2012-01-27 10:55:10 -08002048 if (*l == 0xffffffff || *l == 0x00000000 ||
2049 *l == 0x0000ffff || *l == 0xffff0000)
2050 return false;
2051
Sinan Kaya6a802ef2017-08-29 14:45:44 -05002052 if (pci_bus_crs_vendor_id(*l))
2053 return pci_bus_wait_crs(bus, devfn, l, timeout);
Yinghai Luefdc87d2012-01-27 10:55:10 -08002054
2055 return true;
2056}
2057EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002060 * Read the config data for a PCI device, sanity-check it,
2061 * and fill in the dev structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
Adrian Bunk7f7b5de2008-04-18 13:53:55 -07002063static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 struct pci_dev *dev;
2066 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Yinghai Luefdc87d2012-01-27 10:55:10 -08002068 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return NULL;
2070
Gu Zheng8b1fce02013-05-25 21:48:31 +08002071 dev = pci_alloc_dev(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (!dev)
2073 return NULL;
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 dev->devfn = devfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 dev->vendor = l & 0xffff;
2077 dev->device = (l >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10002079 pci_set_of_node(dev);
2080
Yu Zhao480b93b2009-03-20 11:25:14 +08002081 if (pci_setup_device(dev)) {
Gu Zheng8b1fce02013-05-25 21:48:31 +08002082 pci_bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 kfree(dev);
2084 return NULL;
2085 }
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002086
2087 return dev;
2088}
2089
Zhao, Yu201de562008-10-13 19:49:55 +08002090static void pci_init_capabilities(struct pci_dev *dev)
2091{
Sean O. Stalley938174e2015-10-29 17:35:39 -05002092 /* Enhanced Allocation */
2093 pci_ea_init(dev);
2094
Guilherme G. Piccolie80e7ed2015-10-21 12:17:35 -02002095 /* Setup MSI caps & disable MSI/MSI-X interrupts */
2096 pci_msi_setup_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002097
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01002098 /* Buffers for saving PCIe and PCI-X capabilities */
2099 pci_allocate_cap_save_buffers(dev);
2100
Zhao, Yu201de562008-10-13 19:49:55 +08002101 /* Power Management */
2102 pci_pm_init(dev);
2103
2104 /* Vital Product Data */
Bjorn Helgaasf1cd93f2016-02-22 13:58:37 -06002105 pci_vpd_init(dev);
Yu Zhao58c3a722008-10-14 14:02:53 +08002106
2107 /* Alternative Routing-ID Forwarding */
Yijing Wang31ab2472013-01-15 11:12:17 +08002108 pci_configure_ari(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08002109
2110 /* Single Root I/O Virtualization */
2111 pci_iov_init(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07002112
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -05002113 /* Address Translation Services */
2114 pci_ats_init(dev);
2115
Allen Kayae21ee62009-10-07 10:27:17 -07002116 /* Enable ACS P2P upstream forwarding */
Chris Wright5d990b62009-12-04 12:15:21 -08002117 pci_enable_acs(dev);
Taku Izumib07461a2015-09-17 10:09:37 -05002118
Jonathan Yong9bb04a02016-06-11 14:13:38 -05002119 /* Precision Time Measurement */
2120 pci_ptm_init(dev);
Bjorn Helgaas4dc2db02016-10-03 09:42:57 -05002121
Keith Busch66b80802016-09-27 16:23:34 -04002122 /* Advanced Error Reporting */
2123 pci_aer_init(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08002124}
2125
Marc Zyngier098259e2015-10-02 10:19:32 +01002126/*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002127 * This is the equivalent of pci_host_bridge_msi_domain() that acts on
Marc Zyngier098259e2015-10-02 10:19:32 +01002128 * devices. Firmware interfaces that can select the MSI domain on a
2129 * per-device basis should be called from here.
2130 */
2131static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2132{
2133 struct irq_domain *d;
2134
2135 /*
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002136 * If a domain has been set through the pcibios_add_device()
Marc Zyngier098259e2015-10-02 10:19:32 +01002137 * callback, then this is the one (platform code knows best).
2138 */
2139 d = dev_get_msi_domain(&dev->dev);
2140 if (d)
2141 return d;
2142
Marc Zyngier54fa97e2015-10-02 14:43:06 +01002143 /*
2144 * Let's see if we have a firmware interface able to provide
2145 * the domain.
2146 */
2147 d = pci_msi_get_device_domain(dev);
2148 if (d)
2149 return d;
2150
Marc Zyngier098259e2015-10-02 10:19:32 +01002151 return NULL;
2152}
2153
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002154static void pci_set_msi_domain(struct pci_dev *dev)
2155{
Marc Zyngier098259e2015-10-02 10:19:32 +01002156 struct irq_domain *d;
2157
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002158 /*
Marc Zyngier098259e2015-10-02 10:19:32 +01002159 * If the platform or firmware interfaces cannot supply a
2160 * device-specific MSI domain, then inherit the default domain
2161 * from the host bridge itself.
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002162 */
Marc Zyngier098259e2015-10-02 10:19:32 +01002163 d = pci_dev_msi_domain(dev);
2164 if (!d)
2165 d = dev_get_msi_domain(&dev->bus->dev);
2166
2167 dev_set_msi_domain(&dev->dev, d);
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002168}
2169
Sam Ravnborg96bde062007-03-26 21:53:30 -08002170void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002171{
Yinghai Lu4f535092013-01-21 13:20:52 -08002172 int ret;
2173
Bjorn Helgaas6cd33642014-08-27 14:29:47 -06002174 pci_configure_device(dev);
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 device_initialize(&dev->dev);
2177 dev->dev.release = pci_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Yinghai Lu7629d192013-01-21 13:20:44 -08002179 set_dev_node(&dev->dev, pcibus_to_node(bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 dev->dev.dma_mask = &dev->dma_mask;
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002181 dev->dev.dma_parms = &dev->dma_parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 dev->dev.coherent_dma_mask = 0xffffffffull;
2183
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002184 pci_set_dma_max_seg_size(dev, 65536);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08002185 pci_set_dma_seg_boundary(dev, 0xffffffff);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /* Fix up broken headers */
2188 pci_fixup_device(pci_fixup_header, dev);
2189
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002190 /* Moved out from quirk header fixup code */
Yinghai Lu2069ecf2012-02-15 21:40:31 -08002191 pci_reassigndev_resource_alignment(dev);
2192
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002193 /* Clear the state_saved flag */
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02002194 dev->state_saved = false;
2195
Zhao, Yu201de562008-10-13 19:49:55 +08002196 /* Initialize various capabilities */
2197 pci_init_capabilities(dev);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /*
2200 * Add the device to our list of discovered devices
2201 * and the bus list for fixup functions, etc.
2202 */
Zhang Yanmind71374d2006-06-02 12:35:43 +08002203 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 list_add_tail(&dev->bus_list, &bus->devices);
Zhang Yanmind71374d2006-06-02 12:35:43 +08002205 up_write(&pci_bus_sem);
Yinghai Lu4f535092013-01-21 13:20:52 -08002206
Yinghai Lu4f535092013-01-21 13:20:52 -08002207 ret = pcibios_add_device(dev);
2208 WARN_ON(ret < 0);
2209
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002210 /* Set up MSI IRQ domain */
Marc Zyngier44aa0c62015-07-28 14:46:11 +01002211 pci_set_msi_domain(dev);
2212
Yinghai Lu4f535092013-01-21 13:20:52 -08002213 /* Notifier could use PCI capabilities */
2214 dev->match_driver = false;
2215 ret = device_add(&dev->dev);
2216 WARN_ON(ret < 0);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002217}
2218
Bjorn Helgaas10874f52014-04-14 16:11:40 -06002219struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002220{
2221 struct pci_dev *dev;
2222
Trent Piepho90bdb312009-03-20 14:56:00 -06002223 dev = pci_get_slot(bus, devfn);
2224 if (dev) {
2225 pci_dev_put(dev);
2226 return dev;
2227 }
2228
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002229 dev = pci_scan_device(bus, devfn);
2230 if (!dev)
2231 return NULL;
2232
2233 pci_device_add(dev, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 return dev;
2236}
Adrian Bunkb73e9682007-11-21 15:07:11 -08002237EXPORT_SYMBOL(pci_scan_single_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002239static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002240{
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002241 int pos;
2242 u16 cap = 0;
2243 unsigned next_fn;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002244
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002245 if (pci_ari_enabled(bus)) {
2246 if (!dev)
2247 return 0;
2248 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2249 if (!pos)
2250 return 0;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002251
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002252 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2253 next_fn = PCI_ARI_CAP_NFN(cap);
2254 if (next_fn <= fn)
2255 return 0; /* protect against malformed list */
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002256
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002257 return next_fn;
2258 }
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002259
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002260 /* dev may be NULL for non-contiguous multifunction devices */
2261 if (!dev || dev->multifunction)
2262 return (fn + 1) % 8;
2263
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002264 return 0;
2265}
2266
2267static int only_one_child(struct pci_bus *bus)
2268{
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002269 struct pci_dev *bridge = bus->self;
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002270
2271 /*
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002272 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
2273 * we scan for all possible devices, not just Device 0.
Bjorn Helgaas5bbe0292016-02-05 14:57:47 -06002274 */
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002275 if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
2276 return 0;
2277
2278 /*
2279 * A PCIe Downstream Port normally leads to a Link with only Device
2280 * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan
2281 * only for Device 0 in that situation.
2282 *
2283 * Checking has_secondary_link is a hack to identify Downstream
2284 * Ports because sometimes Switches are configured such that the
2285 * PCIe Port Type labels are backwards.
2286 */
2287 if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002288 return 1;
Bjorn Helgaasd57f0b82017-11-30 15:22:39 -06002289
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002290 return 0;
2291}
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002294 * pci_scan_slot - Scan a PCI slot on a bus for devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 * @bus: PCI bus to scan
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002296 * @devfn: slot number to scan (must have zero function)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 *
2298 * Scan a PCI slot on the specified PCI bus for devices, adding
2299 * discovered devices to the @bus->devices list. New devices
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -08002300 * will not have is_added set.
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002301 *
2302 * Returns the number of new devices found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 */
Sam Ravnborg96bde062007-03-26 21:53:30 -08002304int pci_scan_slot(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002306 unsigned fn, nr = 0;
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002307 struct pci_dev *dev;
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002308
2309 if (only_one_child(bus) && (devfn > 0))
2310 return 0; /* Already scanned the entire slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002312 dev = pci_scan_single_device(bus, devfn);
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07002313 if (!dev)
2314 return 0;
2315 if (!dev->is_added)
Trent Piepho1b69dfc2009-03-20 14:56:05 -06002316 nr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Yijing Wangb1bd58e2013-01-25 09:12:31 -07002318 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05002319 dev = pci_scan_single_device(bus, devfn + fn);
2320 if (dev) {
2321 if (!dev->is_added)
2322 nr++;
2323 dev->multifunction = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 }
Shaohua Li7d715a62008-02-25 09:46:41 +08002326
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002327 /* Only one slot has PCIe device */
Shaohua Li149e1632008-07-23 10:32:31 +08002328 if (bus->self && nr)
Shaohua Li7d715a62008-02-25 09:46:41 +08002329 pcie_aspm_init_link_state(bus->self);
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return nr;
2332}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002333EXPORT_SYMBOL(pci_scan_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Jon Masonb03e7492011-07-20 15:20:54 -05002335static int pcie_find_smpss(struct pci_dev *dev, void *data)
2336{
2337 u8 *smpss = data;
2338
2339 if (!pci_is_pcie(dev))
2340 return 0;
2341
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002342 /*
2343 * We don't have a way to change MPS settings on devices that have
2344 * drivers attached. A hot-added device might support only the minimum
2345 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
2346 * where devices may be hot-added, we limit the fabric MPS to 128 so
2347 * hot-added devices will work correctly.
2348 *
2349 * However, if we hot-add a device to a slot directly below a Root
2350 * Port, it's impossible for there to be other existing devices below
2351 * the port. We don't limit the MPS in this case because we can
2352 * reconfigure MPS on both the Root Port and the hot-added device,
2353 * and there are no other devices involved.
2354 *
2355 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
Jon Masonb03e7492011-07-20 15:20:54 -05002356 */
Yijing Wangd4aa68f2013-08-22 11:24:47 +08002357 if (dev->is_hotplug_bridge &&
2358 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
Jon Masonb03e7492011-07-20 15:20:54 -05002359 *smpss = 0;
2360
2361 if (*smpss > dev->pcie_mpss)
2362 *smpss = dev->pcie_mpss;
2363
2364 return 0;
2365}
2366
2367static void pcie_write_mps(struct pci_dev *dev, int mps)
2368{
Jon Mason62f392e2011-10-14 14:56:14 -05002369 int rc;
Jon Masonb03e7492011-07-20 15:20:54 -05002370
2371 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
Jon Mason62f392e2011-10-14 14:56:14 -05002372 mps = 128 << dev->pcie_mpss;
Jon Masonb03e7492011-07-20 15:20:54 -05002373
Yijing Wang62f87c02012-07-24 17:20:03 +08002374 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2375 dev->bus->self)
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002376
2377 /*
2378 * For "Performance", the assumption is made that
Jon Masonb03e7492011-07-20 15:20:54 -05002379 * downstream communication will never be larger than
2380 * the MRRS. So, the MPS only needs to be configured
2381 * for the upstream communication. This being the case,
2382 * walk from the top down and set the MPS of the child
2383 * to that of the parent bus.
Jon Mason62f392e2011-10-14 14:56:14 -05002384 *
2385 * Configure the device MPS with the smaller of the
2386 * device MPSS or the bridge MPS (which is assumed to be
2387 * properly configured at this point to the largest
2388 * allowable MPS based on its parent bus).
Jon Masonb03e7492011-07-20 15:20:54 -05002389 */
Jon Mason62f392e2011-10-14 14:56:14 -05002390 mps = min(mps, pcie_get_mps(dev->bus->self));
Jon Masonb03e7492011-07-20 15:20:54 -05002391 }
2392
2393 rc = pcie_set_mps(dev, mps);
2394 if (rc)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002395 pci_err(dev, "Failed attempting to set the MPS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002396}
2397
Jon Mason62f392e2011-10-14 14:56:14 -05002398static void pcie_write_mrrs(struct pci_dev *dev)
Jon Masonb03e7492011-07-20 15:20:54 -05002399{
Jon Mason62f392e2011-10-14 14:56:14 -05002400 int rc, mrrs;
Jon Masonb03e7492011-07-20 15:20:54 -05002401
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002402 /*
2403 * In the "safe" case, do not configure the MRRS. There appear to be
Jon Masoned2888e2011-09-08 16:41:18 -05002404 * issues with setting MRRS to 0 on a number of devices.
2405 */
Jon Masoned2888e2011-09-08 16:41:18 -05002406 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2407 return;
Jon Masonb03e7492011-07-20 15:20:54 -05002408
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002409 /*
2410 * For max performance, the MRRS must be set to the largest supported
Jon Masoned2888e2011-09-08 16:41:18 -05002411 * value. However, it cannot be configured larger than the MPS the
Jon Mason62f392e2011-10-14 14:56:14 -05002412 * device or the bus can support. This should already be properly
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002413 * configured by a prior call to pcie_write_mps().
Jon Masoned2888e2011-09-08 16:41:18 -05002414 */
Jon Mason62f392e2011-10-14 14:56:14 -05002415 mrrs = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002416
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002417 /*
2418 * MRRS is a R/W register. Invalid values can be written, but a
Jon Masoned2888e2011-09-08 16:41:18 -05002419 * subsequent read will verify if the value is acceptable or not.
Jon Masonb03e7492011-07-20 15:20:54 -05002420 * If the MRRS value provided is not acceptable (e.g., too large),
2421 * shrink the value until it is acceptable to the HW.
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002422 */
Jon Masonb03e7492011-07-20 15:20:54 -05002423 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2424 rc = pcie_set_readrq(dev, mrrs);
Jon Mason62f392e2011-10-14 14:56:14 -05002425 if (!rc)
2426 break;
Jon Masonb03e7492011-07-20 15:20:54 -05002427
Frederick Lawler7506dc72018-01-18 12:55:24 -06002428 pci_warn(dev, "Failed attempting to set the MRRS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05002429 mrrs /= 2;
2430 }
Jon Mason62f392e2011-10-14 14:56:14 -05002431
2432 if (mrrs < 128)
Frederick Lawler7506dc72018-01-18 12:55:24 -06002433 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 -05002434}
2435
2436static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2437{
Jon Masona513a992011-10-14 14:56:16 -05002438 int mps, orig_mps;
Jon Masonb03e7492011-07-20 15:20:54 -05002439
2440 if (!pci_is_pcie(dev))
2441 return 0;
2442
Keith Busch27d868b2015-08-24 08:48:16 -05002443 if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2444 pcie_bus_config == PCIE_BUS_DEFAULT)
Yijing Wang5895af72013-08-26 16:33:06 +08002445 return 0;
Yijing Wang5895af72013-08-26 16:33:06 +08002446
Jon Masona513a992011-10-14 14:56:16 -05002447 mps = 128 << *(u8 *)data;
2448 orig_mps = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002449
2450 pcie_write_mps(dev, mps);
Jon Mason62f392e2011-10-14 14:56:14 -05002451 pcie_write_mrrs(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05002452
Frederick Lawler7506dc72018-01-18 12:55:24 -06002453 pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04002454 pcie_get_mps(dev), 128 << dev->pcie_mpss,
Jon Masona513a992011-10-14 14:56:16 -05002455 orig_mps, pcie_get_readrq(dev));
Jon Masonb03e7492011-07-20 15:20:54 -05002456
2457 return 0;
2458}
2459
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002460/*
2461 * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
Jon Masonb03e7492011-07-20 15:20:54 -05002462 * parents then children fashion. If this changes, then this code will not
2463 * work as designed.
2464 */
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002465void pcie_bus_configure_settings(struct pci_bus *bus)
Jon Masonb03e7492011-07-20 15:20:54 -05002466{
Bjorn Helgaas1e358f92014-04-29 12:51:55 -06002467 u8 smpss = 0;
Jon Masonb03e7492011-07-20 15:20:54 -05002468
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002469 if (!bus->self)
2470 return;
2471
Jon Masonb03e7492011-07-20 15:20:54 -05002472 if (!pci_is_pcie(bus->self))
2473 return;
2474
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002475 /*
2476 * FIXME - Peer to peer DMA is possible, though the endpoint would need
Jon Mason33154722013-08-26 16:33:05 +08002477 * to be aware of the MPS of the destination. To work around this,
Jon Mason5f39e672011-10-03 09:50:20 -05002478 * simply force the MPS of the entire system to the smallest possible.
2479 */
2480 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2481 smpss = 0;
2482
Jon Masonb03e7492011-07-20 15:20:54 -05002483 if (pcie_bus_config == PCIE_BUS_SAFE) {
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08002484 smpss = bus->self->pcie_mpss;
Jon Mason5f39e672011-10-03 09:50:20 -05002485
Jon Masonb03e7492011-07-20 15:20:54 -05002486 pcie_find_smpss(bus->self, &smpss);
2487 pci_walk_bus(bus, pcie_find_smpss, &smpss);
2488 }
2489
2490 pcie_bus_configure_set(bus->self, &smpss);
2491 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2492}
Jon Masondebc3b72011-08-02 00:01:18 -05002493EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
Jon Masonb03e7492011-07-20 15:20:54 -05002494
Palmer Dabbeltbccf90d2017-06-23 18:50:42 -07002495/*
2496 * Called after each bus is probed, but before its children are examined. This
2497 * is marked as __weak because multiple architectures define it.
2498 */
2499void __weak pcibios_fixup_bus(struct pci_bus *bus)
2500{
2501 /* nothing to do, expected to be removed in the future */
2502}
2503
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002504/**
2505 * pci_scan_child_bus_extend() - Scan devices below a bus
2506 * @bus: Bus to scan for devices
2507 * @available_buses: Total number of buses available (%0 does not try to
2508 * extend beyond the minimal)
2509 *
2510 * Scans devices below @bus including subordinate buses. Returns new
2511 * subordinate number including all the found devices. Passing
2512 * @available_buses causes the remaining bus space to be distributed
2513 * equally between hotplug-capable bridges to allow future extension of the
2514 * hierarchy.
2515 */
2516static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2517 unsigned int available_buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002519 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2520 unsigned int start = bus->busn_res.start;
2521 unsigned int devfn, cmax, max = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 struct pci_dev *dev;
2523
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002524 dev_dbg(&bus->dev, "scanning bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* Go find them, Rover! */
2527 for (devfn = 0; devfn < 0x100; devfn += 8)
2528 pci_scan_slot(bus, devfn);
2529
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002530 /* Reserve buses for SR-IOV capability */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002531 used_buses = pci_iov_bus_range(bus);
2532 max += used_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +08002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * After performing arch-dependent fixup of the bus, look behind
2536 * all PCI-to-PCI bridges on this bus.
2537 */
Alex Chiang74710de2009-03-20 14:56:10 -06002538 if (!bus->is_added) {
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002539 dev_dbg(&bus->dev, "fixups for bus\n");
Alex Chiang74710de2009-03-20 14:56:10 -06002540 pcibios_fixup_bus(bus);
Jiang Liu981cf9e2013-04-12 05:44:16 +00002541 bus->is_added = 1;
Alex Chiang74710de2009-03-20 14:56:10 -06002542 }
2543
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002544 /*
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002545 * Calculate how many hotplug bridges and normal bridges there
2546 * are on this bus. We will distribute the additional available
2547 * buses between hotplug bridges.
2548 */
2549 for_each_pci_bridge(dev, bus) {
2550 if (dev->is_hotplug_bridge)
2551 hotplug_bridges++;
2552 else
2553 normal_bridges++;
2554 }
2555
2556 /*
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002557 * Scan bridges that are already configured. We don't touch them
2558 * unless they are misconfigured (which will be done in the second
2559 * scan below).
2560 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002561 for_each_pci_bridge(dev, bus) {
2562 cmax = max;
2563 max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
2564 used_buses += cmax - max;
2565 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002566
2567 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002568 for_each_pci_bridge(dev, bus) {
2569 unsigned int buses = 0;
2570
2571 if (!hotplug_bridges && normal_bridges == 1) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002572
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002573 /*
2574 * There is only one bridge on the bus (upstream
2575 * port) so it gets all available buses which it
2576 * can then distribute to the possible hotplug
2577 * bridges below.
2578 */
2579 buses = available_buses;
2580 } else if (dev->is_hotplug_bridge) {
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002581
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002582 /*
2583 * Distribute the extra buses between hotplug
2584 * bridges if any.
2585 */
2586 buses = available_buses / hotplug_bridges;
2587 buses = min(buses, available_buses - used_buses);
2588 }
2589
2590 cmax = max;
2591 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
2592 used_buses += max - cmax;
2593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /*
Keith Busche16b4662016-07-21 21:40:28 -06002596 * Make sure a hotplug bridge has at least the minimum requested
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002597 * number of buses but allow it to grow up to the maximum available
2598 * bus number of there is room.
Keith Busche16b4662016-07-21 21:40:28 -06002599 */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002600 if (bus->self && bus->self->is_hotplug_bridge) {
2601 used_buses = max_t(unsigned int, available_buses,
2602 pci_hotplug_bus_size - 1);
2603 if (max - start < used_buses) {
2604 max = start + used_buses;
Mika Westerberga20c7f32017-10-13 21:35:43 +03002605
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002606 /* Do not allocate more buses than we have room left */
2607 if (max > bus->busn_res.end)
2608 max = bus->busn_res.end;
2609
2610 dev_dbg(&bus->dev, "%pR extended by %#02x\n",
2611 &bus->busn_res, max - start);
2612 }
Keith Busche16b4662016-07-21 21:40:28 -06002613 }
2614
2615 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 * We've scanned the bus and so we know all about what's on
2617 * the other side of any bridges that may be on this bus plus
2618 * any devices.
2619 *
2620 * Return how far we've got finding sub-buses.
2621 */
Bjorn Helgaas0207c352009-11-04 10:32:52 -07002622 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return max;
2624}
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002625
2626/**
2627 * pci_scan_child_bus() - Scan devices below a bus
2628 * @bus: Bus to scan for devices
2629 *
2630 * Scans devices below @bus including subordinate buses. Returns new
2631 * subordinate number including all the found devices.
2632 */
2633unsigned int pci_scan_child_bus(struct pci_bus *bus)
2634{
2635 return pci_scan_child_bus_extend(bus, 0);
2636}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -06002637EXPORT_SYMBOL_GPL(pci_scan_child_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002639/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002640 * pcibios_root_bridge_prepare - Platform-specific host bridge setup
2641 * @bridge: Host bridge to set up
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002642 *
2643 * Default empty implementation. Replace with an architecture-specific setup
2644 * routine, if necessary.
2645 */
2646int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
2647{
2648 return 0;
2649}
2650
Jiang Liu10a95742013-04-12 05:44:20 +00002651void __weak pcibios_add_bus(struct pci_bus *bus)
2652{
2653}
2654
2655void __weak pcibios_remove_bus(struct pci_bus *bus)
2656{
2657}
2658
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002659struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
2660 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07002662 int error;
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07002663 struct pci_host_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Thierry Reding59094062016-11-25 11:57:10 +01002665 bridge = pci_alloc_host_bridge(0);
Yinghai Lu7b543662012-04-02 18:31:53 -07002666 if (!bridge)
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002667 return NULL;
Yinghai Lu7b543662012-04-02 18:31:53 -07002668
2669 bridge->dev.parent = parent;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002670
2671 list_splice_init(resources, &bridge->windows);
2672 bridge->sysdata = sysdata;
2673 bridge->busnr = bus;
2674 bridge->ops = ops;
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002675
2676 error = pci_register_host_bridge(bridge);
2677 if (error < 0)
Jiang Liu343df772013-06-07 01:10:08 +08002678 goto err_out;
Rafael J. Wysocki6c0cc952013-01-09 22:33:37 +01002679
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002680 return bridge->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Yinghai Lu7b543662012-04-02 18:31:53 -07002682err_out:
Arnd Bergmann37d6a0a2016-11-25 11:57:09 +01002683 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return NULL;
2685}
Ray Juie6b29de2015-04-08 11:21:33 -07002686EXPORT_SYMBOL_GPL(pci_create_root_bus);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10002687
Cyrille Pitchen49b8e3f2018-01-30 21:56:52 +01002688int pci_host_probe(struct pci_host_bridge *bridge)
2689{
2690 struct pci_bus *bus, *child;
2691 int ret;
2692
2693 ret = pci_scan_root_bus_bridge(bridge);
2694 if (ret < 0) {
2695 dev_err(bridge->dev.parent, "Scanning root bridge failed");
2696 return ret;
2697 }
2698
2699 bus = bridge->bus;
2700
2701 /*
2702 * We insert PCI resources into the iomem_resource and
2703 * ioport_resource trees in either pci_bus_claim_resources()
2704 * or pci_bus_assign_resources().
2705 */
2706 if (pci_has_flag(PCI_PROBE_ONLY)) {
2707 pci_bus_claim_resources(bus);
2708 } else {
2709 pci_bus_size_bridges(bus);
2710 pci_bus_assign_resources(bus);
2711
2712 list_for_each_entry(child, &bus->children, node)
2713 pcie_bus_configure_settings(child);
2714 }
2715
2716 pci_bus_add_devices(bus);
2717 return 0;
2718}
2719EXPORT_SYMBOL_GPL(pci_host_probe);
2720
Yinghai Lu98a35832012-05-18 11:35:50 -06002721int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2722{
2723 struct resource *res = &b->busn_res;
2724 struct resource *parent_res, *conflict;
2725
2726 res->start = bus;
2727 res->end = bus_max;
2728 res->flags = IORESOURCE_BUS;
2729
2730 if (!pci_is_root_bus(b))
2731 parent_res = &b->parent->busn_res;
2732 else {
2733 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
2734 res->flags |= IORESOURCE_PCI_FIXED;
2735 }
2736
Andreas Noeverced04d12014-01-23 21:59:24 +01002737 conflict = request_resource_conflict(parent_res, res);
Yinghai Lu98a35832012-05-18 11:35:50 -06002738
2739 if (conflict)
2740 dev_printk(KERN_DEBUG, &b->dev,
2741 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
2742 res, pci_is_root_bus(b) ? "domain " : "",
2743 parent_res, conflict->name, conflict);
Yinghai Lu98a35832012-05-18 11:35:50 -06002744
2745 return conflict == NULL;
2746}
2747
2748int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
2749{
2750 struct resource *res = &b->busn_res;
2751 struct resource old_res = *res;
2752 resource_size_t size;
2753 int ret;
2754
2755 if (res->start > bus_max)
2756 return -EINVAL;
2757
2758 size = bus_max - res->start + 1;
2759 ret = adjust_resource(res, res->start, size);
2760 dev_printk(KERN_DEBUG, &b->dev,
2761 "busn_res: %pR end %s updated to %02x\n",
2762 &old_res, ret ? "can not be" : "is", bus_max);
2763
2764 if (!ret && !res->parent)
2765 pci_bus_insert_busn_res(b, res->start, res->end);
2766
2767 return ret;
2768}
2769
2770void pci_bus_release_busn_res(struct pci_bus *b)
2771{
2772 struct resource *res = &b->busn_res;
2773 int ret;
2774
2775 if (!res->flags || !res->parent)
2776 return;
2777
2778 ret = release_resource(res);
2779 dev_printk(KERN_DEBUG, &b->dev,
2780 "busn_res: %pR %s released\n",
2781 res, ret ? "can not be" : "is");
2782}
2783
Lorenzo Pieralisi1228c4b2017-06-28 15:13:55 -05002784int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
2785{
2786 struct resource_entry *window;
2787 bool found = false;
2788 struct pci_bus *b;
2789 int max, bus, ret;
2790
2791 if (!bridge)
2792 return -EINVAL;
2793
2794 resource_list_for_each_entry(window, &bridge->windows)
2795 if (window->res->flags & IORESOURCE_BUS) {
2796 found = true;
2797 break;
2798 }
2799
2800 ret = pci_register_host_bridge(bridge);
2801 if (ret < 0)
2802 return ret;
2803
2804 b = bridge->bus;
2805 bus = bridge->busnr;
2806
2807 if (!found) {
2808 dev_info(&b->dev,
2809 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2810 bus);
2811 pci_bus_insert_busn_res(b, bus, 255);
2812 }
2813
2814 max = pci_scan_child_bus(b);
2815
2816 if (!found)
2817 pci_bus_update_busn_res_end(b, max);
2818
2819 return 0;
2820}
2821EXPORT_SYMBOL(pci_scan_root_bus_bridge);
2822
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002823struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
2824 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Bjorn Helgaasa2ebb822011-10-28 16:25:50 -06002825{
Jiang Liu14d76b62015-02-05 13:44:44 +08002826 struct resource_entry *window;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002827 bool found = false;
Bjorn Helgaasa2ebb822011-10-28 16:25:50 -06002828 struct pci_bus *b;
Yinghai Lu4d99f522012-05-17 18:51:12 -07002829 int max;
2830
Jiang Liu14d76b62015-02-05 13:44:44 +08002831 resource_list_for_each_entry(window, resources)
Yinghai Lu4d99f522012-05-17 18:51:12 -07002832 if (window->res->flags & IORESOURCE_BUS) {
2833 found = true;
2834 break;
2835 }
Bjorn Helgaasa2ebb822011-10-28 16:25:50 -06002836
Lorenzo Pieralisi9ee8a1c2017-06-28 15:14:01 -05002837 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
Bjorn Helgaasa2ebb822011-10-28 16:25:50 -06002838 if (!b)
2839 return NULL;
2840
Yinghai Lu4d99f522012-05-17 18:51:12 -07002841 if (!found) {
2842 dev_info(&b->dev,
2843 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2844 bus);
2845 pci_bus_insert_busn_res(b, bus, 255);
2846 }
2847
2848 max = pci_scan_child_bus(b);
2849
2850 if (!found)
2851 pci_bus_update_busn_res_end(b, max);
2852
Bjorn Helgaasa2ebb822011-10-28 16:25:50 -06002853 return b;
2854}
2855EXPORT_SYMBOL(pci_scan_root_bus);
2856
Bill Pemberton15856ad2012-11-21 15:35:00 -05002857struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002858 void *sysdata)
2859{
2860 LIST_HEAD(resources);
2861 struct pci_bus *b;
2862
2863 pci_add_resource(&resources, &ioport_resource);
2864 pci_add_resource(&resources, &iomem_resource);
Yinghai Lu857c3b62012-05-17 18:51:12 -07002865 pci_add_resource(&resources, &busn_resource);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002866 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
2867 if (b) {
Yinghai Lu857c3b62012-05-17 18:51:12 -07002868 pci_scan_child_bus(b);
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06002869 } else {
2870 pci_free_resource_list(&resources);
2871 }
2872 return b;
2873}
2874EXPORT_SYMBOL(pci_scan_bus);
2875
Alex Chiang3ed4fd92009-03-20 14:56:25 -06002876/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002877 * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
Yinghai Lu2f320522012-01-21 02:08:22 -08002878 * @bridge: PCI bridge for the bus to scan
2879 *
2880 * Scan a PCI bus and child buses for new devices, add them,
2881 * and enable them, resizing bridge mmio/io resource if necessary
2882 * and possible. The caller must ensure the child devices are already
2883 * removed for resizing to occur.
2884 *
2885 * Returns the max number of subordinate bus discovered.
2886 */
Bjorn Helgaas10874f52014-04-14 16:11:40 -06002887unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
Yinghai Lu2f320522012-01-21 02:08:22 -08002888{
2889 unsigned int max;
2890 struct pci_bus *bus = bridge->subordinate;
2891
2892 max = pci_scan_child_bus(bus);
2893
2894 pci_assign_unassigned_bridge_resources(bridge);
2895
2896 pci_bus_add_devices(bus);
2897
2898 return max;
2899}
2900
Yinghai Lua5213a32012-10-30 14:31:21 -06002901/**
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002902 * pci_rescan_bus - Scan a PCI bus for devices
Yinghai Lua5213a32012-10-30 14:31:21 -06002903 * @bus: PCI bus to scan
2904 *
Bjorn Helgaas3e466e22017-11-30 10:58:14 -06002905 * Scan a PCI bus and child buses for new devices, add them,
2906 * and enable them.
Yinghai Lua5213a32012-10-30 14:31:21 -06002907 *
2908 * Returns the max number of subordinate bus discovered.
2909 */
Bjorn Helgaas10874f52014-04-14 16:11:40 -06002910unsigned int pci_rescan_bus(struct pci_bus *bus)
Yinghai Lua5213a32012-10-30 14:31:21 -06002911{
2912 unsigned int max;
2913
2914 max = pci_scan_child_bus(bus);
2915 pci_assign_unassigned_bus_resources(bus);
2916 pci_bus_add_devices(bus);
2917
2918 return max;
2919}
2920EXPORT_SYMBOL_GPL(pci_rescan_bus);
2921
Rafael J. Wysocki9d169472014-01-10 15:22:18 +01002922/*
2923 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
2924 * routines should always be executed under this mutex.
2925 */
2926static DEFINE_MUTEX(pci_rescan_remove_lock);
2927
2928void pci_lock_rescan_remove(void)
2929{
2930 mutex_lock(&pci_rescan_remove_lock);
2931}
2932EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
2933
2934void pci_unlock_rescan_remove(void)
2935{
2936 mutex_unlock(&pci_rescan_remove_lock);
2937}
2938EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
2939
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002940static int __init pci_sort_bf_cmp(const struct device *d_a,
2941 const struct device *d_b)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002942{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002943 const struct pci_dev *a = to_pci_dev(d_a);
2944 const struct pci_dev *b = to_pci_dev(d_b);
2945
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002946 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
2947 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
2948
2949 if (a->bus->number < b->bus->number) return -1;
2950 else if (a->bus->number > b->bus->number) return 1;
2951
2952 if (a->devfn < b->devfn) return -1;
2953 else if (a->devfn > b->devfn) return 1;
2954
2955 return 0;
2956}
2957
Greg Kroah-Hartman5ff580c2008-02-14 14:56:56 -08002958void __init pci_sort_breadthfirst(void)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002959{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05002960 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
Matt Domsch6b4b78f2006-09-29 15:23:23 -05002961}
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002962
2963int pci_hp_add_bridge(struct pci_dev *dev)
2964{
2965 struct pci_bus *parent = dev->bus;
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002966 int busnr, start = parent->busn_res.start;
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002967 unsigned int available_buses = 0;
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002968 int end = parent->busn_res.end;
2969
2970 for (busnr = start; busnr <= end; busnr++) {
2971 if (!pci_find_bus(pci_domain_nr(parent), busnr))
2972 break;
2973 }
2974 if (busnr-- > end) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06002975 pci_err(dev, "No bus number available for hot-added bridge\n");
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002976 return -1;
2977 }
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002978
2979 /* Scan bridges that are already configured */
2980 busnr = pci_scan_bridge(parent, dev, busnr, 0);
2981
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002982 /*
2983 * Distribute the available bus numbers between hotplug-capable
2984 * bridges to make extending the chain later possible.
2985 */
2986 available_buses = end - busnr;
2987
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002988 /* Scan bridges that need to be reconfigured */
Mika Westerberg1c02ea82017-10-13 21:35:44 +03002989 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
Mika Westerberg4147c2f2017-10-13 21:35:42 +03002990
Mika Westerberg95e3ba92017-10-13 21:35:41 +03002991 if (!dev->subordinate)
2992 return -1;
2993
2994 return 0;
2995}
2996EXPORT_SYMBOL_GPL(pci_hp_add_bridge);