blob: a7a504fc82b991e87c45f870a5fba953face03ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * probe.c - PCI detection and setup code
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/init.h>
8#include <linux/pci.h>
9#include <linux/slab.h>
10#include <linux/module.h>
11#include <linux/cpumask.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080012#include <linux/pci-aspm.h>
Bjorn Helgaas284f5f92012-04-30 15:21:02 -060013#include <asm-generic/pci-bridge.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090014#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
17#define CARDBUS_RESERVE_BUSNR 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/* Ugh. Need to stop exporting this to modules. */
20LIST_HEAD(pci_root_buses);
21EXPORT_SYMBOL(pci_root_buses);
22
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080023static int find_anything(struct device *dev, void *data)
24{
25 return 1;
26}
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070028/*
29 * Some device drivers need know if pci is initiated.
30 * Basically, we think pci is not initiated when there
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080031 * is no device to be found on the pci_bus_type.
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070032 */
33int no_pci_devices(void)
34{
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080035 struct device *dev;
36 int no_devices;
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070037
Greg Kroah-Hartman70308922008-02-13 22:30:39 -080038 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
39 no_devices = (dev == NULL);
40 put_device(dev);
41 return no_devices;
42}
Zhang, Yanmined4aaad2007-07-15 23:39:39 -070043EXPORT_SYMBOL(no_pci_devices);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * PCI Bus Class
47 */
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040048static void release_pcibus_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040050 struct pci_bus *pci_bus = to_pci_bus(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 if (pci_bus->bridge)
53 put_device(pci_bus->bridge);
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -070054 pci_bus_remove_resources(pci_bus);
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +100055 pci_release_bus_of_node(pci_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 kfree(pci_bus);
57}
58
59static struct class pcibus_class = {
60 .name = "pci_bus",
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040061 .dev_release = &release_pcibus_dev,
Yinghai Lub9d320f2011-05-12 17:11:39 -070062 .dev_attrs = pcibus_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65static int __init pcibus_class_init(void)
66{
67 return class_register(&pcibus_class);
68}
69postcore_initcall(pcibus_class_init);
70
Matthew Wilcox6ac665c2008-07-28 13:38:59 -040071static u64 pci_size(u64 base, u64 maxbase, u64 mask)
Yinghai Lu07eddf32006-11-29 13:53:10 -080072{
73 u64 size = mask & maxbase; /* Find the significant bits */
74 if (!size)
75 return 0;
76
77 /* Get the lowest of them to find the decode size, and
78 from that the extent. */
79 size = (size & ~(size-1)) - 1;
80
81 /* base == maxbase can be valid only if the BAR has
82 already been programmed with all 1s. */
83 if (base == maxbase && ((base | size) & mask) != mask)
84 return 0;
85
86 return size;
87}
88
Bjorn Helgaas28c68212011-06-14 13:04:35 -060089static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
Yinghai Lu07eddf32006-11-29 13:53:10 -080090{
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -060091 u32 mem_type;
Bjorn Helgaas28c68212011-06-14 13:04:35 -060092 unsigned long flags;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -060093
Matthew Wilcox6ac665c2008-07-28 13:38:59 -040094 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -060095 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
96 flags |= IORESOURCE_IO;
97 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -040098 }
99
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600100 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
101 flags |= IORESOURCE_MEM;
102 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
103 flags |= IORESOURCE_PREFETCH;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400104
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600105 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
106 switch (mem_type) {
107 case PCI_BASE_ADDRESS_MEM_TYPE_32:
108 break;
109 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
110 dev_info(&dev->dev, "1M mem BAR treated as 32-bit BAR\n");
111 break;
112 case PCI_BASE_ADDRESS_MEM_TYPE_64:
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600113 flags |= IORESOURCE_MEM_64;
114 break;
Bjorn Helgaas8d6a6a42011-06-14 13:04:29 -0600115 default:
116 dev_warn(&dev->dev,
117 "mem unknown type %x treated as 32-bit BAR\n",
118 mem_type);
119 break;
120 }
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600121 return flags;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400122}
123
Yu Zhao0b400c72008-11-22 02:40:40 +0800124/**
125 * pci_read_base - read a PCI BAR
126 * @dev: the PCI device
127 * @type: type of the BAR
128 * @res: resource buffer to be filled in
129 * @pos: BAR position in the config space
130 *
131 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400132 */
Yu Zhao0b400c72008-11-22 02:40:40 +0800133int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400134 struct resource *res, unsigned int pos)
135{
136 u32 l, sz, mask;
Jacob Pan253d2e52010-07-16 10:19:22 -0700137 u16 orig_cmd;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700138 struct pci_bus_region region;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400139
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200140 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400141
Jacob Pan253d2e52010-07-16 10:19:22 -0700142 if (!dev->mmio_always_on) {
143 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
144 pci_write_config_word(dev, PCI_COMMAND,
145 orig_cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
146 }
147
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400148 res->name = pci_name(dev);
149
150 pci_read_config_dword(dev, pos, &l);
Michael S. Tsirkin1ed67432009-10-29 17:24:59 +0200151 pci_write_config_dword(dev, pos, l | mask);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400152 pci_read_config_dword(dev, pos, &sz);
153 pci_write_config_dword(dev, pos, l);
154
Jacob Pan253d2e52010-07-16 10:19:22 -0700155 if (!dev->mmio_always_on)
156 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
157
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400158 /*
159 * All bits set in sz means the device isn't working properly.
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600160 * If the BAR isn't implemented, all bits must be 0. If it's a
161 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
162 * 1 must be clear.
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400163 */
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600164 if (!sz || sz == 0xffffffff)
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400165 goto fail;
166
167 /*
168 * I don't know how l can have all bits set. Copied from old code.
169 * Maybe it fixes a bug on some ancient platform.
170 */
171 if (l == 0xffffffff)
172 l = 0;
173
174 if (type == pci_bar_unknown) {
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600175 res->flags = decode_bar(dev, l);
176 res->flags |= IORESOURCE_SIZEALIGN;
177 if (res->flags & IORESOURCE_IO) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400178 l &= PCI_BASE_ADDRESS_IO_MASK;
David S. Miller5aceca92011-05-23 17:12:22 -0700179 mask = PCI_BASE_ADDRESS_IO_MASK & (u32) IO_SPACE_LIMIT;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400180 } else {
181 l &= PCI_BASE_ADDRESS_MEM_MASK;
182 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
183 }
184 } else {
185 res->flags |= (l & IORESOURCE_ROM_ENABLE);
186 l &= PCI_ROM_ADDRESS_MASK;
187 mask = (u32)PCI_ROM_ADDRESS_MASK;
188 }
189
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600190 if (res->flags & IORESOURCE_MEM_64) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400191 u64 l64 = l;
192 u64 sz64 = sz;
193 u64 mask64 = mask | (u64)~0 << 32;
194
195 pci_read_config_dword(dev, pos + 4, &l);
196 pci_write_config_dword(dev, pos + 4, ~0);
197 pci_read_config_dword(dev, pos + 4, &sz);
198 pci_write_config_dword(dev, pos + 4, l);
199
200 l64 |= ((u64)l << 32);
201 sz64 |= ((u64)sz << 32);
202
203 sz64 = pci_size(l64, sz64, mask64);
204
205 if (!sz64)
206 goto fail;
207
Matthew Wilcoxcc5499c2008-07-28 13:39:00 -0400208 if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700209 dev_err(&dev->dev, "reg %x: can't handle 64-bit BAR\n",
210 pos);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400211 goto fail;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600212 }
213
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600214 if ((sizeof(resource_size_t) < 8) && l) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400215 /* Address above 32-bit boundary; disable the BAR */
216 pci_write_config_dword(dev, pos, 0);
217 pci_write_config_dword(dev, pos + 4, 0);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700218 region.start = 0;
219 region.end = sz64;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700220 pcibios_bus_to_resource(dev, res, &region);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400221 } else {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700222 region.start = l64;
223 region.end = l64 + sz64;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700224 pcibios_bus_to_resource(dev, res, &region);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600225 dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n",
Bjorn Helgaasa369c792009-10-06 15:33:44 -0600226 pos, res);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400227 }
228 } else {
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600229 sz = pci_size(l, sz, mask);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400230
Bjorn Helgaas45aa23b2010-04-22 09:02:43 -0600231 if (!sz)
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400232 goto fail;
233
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700234 region.start = l;
235 region.end = l + sz;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700236 pcibios_bus_to_resource(dev, res, &region);
Vincent Legollf393d9b2008-10-12 12:26:12 +0200237
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600238 dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos, res);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400239 }
240
241 out:
Bjorn Helgaas28c68212011-06-14 13:04:35 -0600242 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400243 fail:
244 res->flags = 0;
245 goto out;
Yinghai Lu07eddf32006-11-29 13:53:10 -0800246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
249{
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400250 unsigned int pos, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400252 for (pos = 0; pos < howmany; pos++) {
253 struct resource *res = &dev->resource[pos];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400255 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (rom) {
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400259 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dev->rom_base_reg = rom;
Matthew Wilcox6ac665c2008-07-28 13:38:59 -0400261 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
262 IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
263 IORESOURCE_SIZEALIGN;
264 __pci_read_base(dev, pci_bar_mem32, res, rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266}
267
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700268static void __devinit pci_read_bridge_io(struct pci_bus *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct pci_dev *dev = child->self;
271 u8 io_base_lo, io_limit_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700273 struct pci_bus_region region;
274 struct resource *res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 res = child->resource[0];
277 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
278 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
279 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
280 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
281
282 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
283 u16 io_base_hi, io_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
286 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600287 base |= ((unsigned long) io_base_hi << 16);
288 limit |= ((unsigned long) io_limit_hi << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Yinghai Lucd81e1e2010-01-22 01:02:22 -0800291 if (base && base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
Bjorn Helgaascf48fb62012-03-16 17:47:59 -0600293 res2.flags = res->flags;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700294 region.start = base;
295 region.end = limit + 0xfff;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700296 pcibios_bus_to_resource(dev, &res2, &region);
Daniel Yeisley9d265122005-12-05 07:06:43 -0500297 if (!res->start)
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700298 res->start = res2.start;
Daniel Yeisley9d265122005-12-05 07:06:43 -0500299 if (!res->end)
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700300 res->end = res2.end;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600301 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700303}
304
305static void __devinit pci_read_bridge_mmio(struct pci_bus *child)
306{
307 struct pci_dev *dev = child->self;
308 u16 mem_base_lo, mem_limit_lo;
309 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700310 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700311 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 res = child->resource[1];
314 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
315 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600316 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
317 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
Yinghai Lucd81e1e2010-01-22 01:02:22 -0800318 if (base && base <= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700320 region.start = base;
321 region.end = limit + 0xfffff;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700322 pcibios_bus_to_resource(dev, res, &region);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600323 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700325}
326
327static void __devinit pci_read_bridge_mmio_pref(struct pci_bus *child)
328{
329 struct pci_dev *dev = child->self;
330 u16 mem_base_lo, mem_limit_lo;
331 unsigned long base, limit;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700332 struct pci_bus_region region;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700333 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 res = child->resource[2];
336 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
337 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600338 base = ((unsigned long) mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
339 limit = ((unsigned long) mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
342 u32 mem_base_hi, mem_limit_hi;
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
345 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
346
347 /*
348 * Some bridges set the base > limit by default, and some
349 * (broken) BIOSes do not initialize them. If we find
350 * this, just assume they are not being used.
351 */
352 if (mem_base_hi <= mem_limit_hi) {
353#if BITS_PER_LONG == 64
Bjorn Helgaas8f38eac2012-06-19 07:45:44 -0600354 base |= ((unsigned long) mem_base_hi) << 32;
355 limit |= ((unsigned long) mem_limit_hi) << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#else
357 if (mem_base_hi || mem_limit_hi) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600358 dev_err(&dev->dev, "can't handle 64-bit "
359 "address space for bridge\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return;
361 }
362#endif
363 }
364 }
Yinghai Lucd81e1e2010-01-22 01:02:22 -0800365 if (base && base <= limit) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700366 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
367 IORESOURCE_MEM | IORESOURCE_PREFETCH;
368 if (res->flags & PCI_PREF_RANGE_TYPE_64)
369 res->flags |= IORESOURCE_MEM_64;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700370 region.start = base;
371 region.end = limit + 0xfffff;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700372 pcibios_bus_to_resource(dev, res, &region);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600373 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375}
376
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700377void __devinit pci_read_bridge_bases(struct pci_bus *child)
378{
379 struct pci_dev *dev = child->self;
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700380 struct resource *res;
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700381 int i;
382
383 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
384 return;
385
386 dev_info(&dev->dev, "PCI bridge to [bus %02x-%02x]%s\n",
387 child->secondary, child->subordinate,
388 dev->transparent ? " (subtractive decode)" : "");
389
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700390 pci_bus_remove_resources(child);
391 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
392 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
393
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700394 pci_read_bridge_io(child);
395 pci_read_bridge_mmio(child);
396 pci_read_bridge_mmio_pref(child);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700397
398 if (dev->transparent) {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700399 pci_bus_for_each_resource(child->parent, res, i) {
400 if (res) {
401 pci_bus_add_resource(child, res,
402 PCI_SUBTRACTIVE_DECODE);
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700403 dev_printk(KERN_DEBUG, &dev->dev,
404 " bridge window %pR (subtractive decode)\n",
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700405 res);
406 }
Bjorn Helgaas2adf7512010-02-23 10:24:26 -0700407 }
408 }
Bjorn Helgaasfa27b2d2010-02-23 10:24:21 -0700409}
410
Sam Ravnborg96bde062007-03-26 21:53:30 -0800411static struct pci_bus * pci_alloc_bus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct pci_bus *b;
414
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100415 b = kzalloc(sizeof(*b), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (b) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 INIT_LIST_HEAD(&b->node);
418 INIT_LIST_HEAD(&b->children);
419 INIT_LIST_HEAD(&b->devices);
Alex Chiangf46753c2008-06-10 15:28:50 -0600420 INIT_LIST_HEAD(&b->slots);
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700421 INIT_LIST_HEAD(&b->resources);
Matthew Wilcox3749c512009-12-13 08:11:32 -0500422 b->max_bus_speed = PCI_SPEED_UNKNOWN;
423 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 return b;
426}
427
Yinghai Lu7b543662012-04-02 18:31:53 -0700428static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b)
429{
430 struct pci_host_bridge *bridge;
431
432 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
433 if (bridge) {
434 INIT_LIST_HEAD(&bridge->windows);
435 bridge->bus = b;
436 }
437
438 return bridge;
439}
440
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500441static unsigned char pcix_bus_speed[] = {
442 PCI_SPEED_UNKNOWN, /* 0 */
443 PCI_SPEED_66MHz_PCIX, /* 1 */
444 PCI_SPEED_100MHz_PCIX, /* 2 */
445 PCI_SPEED_133MHz_PCIX, /* 3 */
446 PCI_SPEED_UNKNOWN, /* 4 */
447 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
448 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
449 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
450 PCI_SPEED_UNKNOWN, /* 8 */
451 PCI_SPEED_66MHz_PCIX_266, /* 9 */
452 PCI_SPEED_100MHz_PCIX_266, /* A */
453 PCI_SPEED_133MHz_PCIX_266, /* B */
454 PCI_SPEED_UNKNOWN, /* C */
455 PCI_SPEED_66MHz_PCIX_533, /* D */
456 PCI_SPEED_100MHz_PCIX_533, /* E */
457 PCI_SPEED_133MHz_PCIX_533 /* F */
458};
459
Matthew Wilcox3749c512009-12-13 08:11:32 -0500460static unsigned char pcie_link_speed[] = {
461 PCI_SPEED_UNKNOWN, /* 0 */
462 PCIE_SPEED_2_5GT, /* 1 */
463 PCIE_SPEED_5_0GT, /* 2 */
Matthew Wilcox9dfd97f2009-12-13 08:11:35 -0500464 PCIE_SPEED_8_0GT, /* 3 */
Matthew Wilcox3749c512009-12-13 08:11:32 -0500465 PCI_SPEED_UNKNOWN, /* 4 */
466 PCI_SPEED_UNKNOWN, /* 5 */
467 PCI_SPEED_UNKNOWN, /* 6 */
468 PCI_SPEED_UNKNOWN, /* 7 */
469 PCI_SPEED_UNKNOWN, /* 8 */
470 PCI_SPEED_UNKNOWN, /* 9 */
471 PCI_SPEED_UNKNOWN, /* A */
472 PCI_SPEED_UNKNOWN, /* B */
473 PCI_SPEED_UNKNOWN, /* C */
474 PCI_SPEED_UNKNOWN, /* D */
475 PCI_SPEED_UNKNOWN, /* E */
476 PCI_SPEED_UNKNOWN /* F */
477};
478
479void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
480{
481 bus->cur_bus_speed = pcie_link_speed[linksta & 0xf];
482}
483EXPORT_SYMBOL_GPL(pcie_update_link_speed);
484
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500485static unsigned char agp_speeds[] = {
486 AGP_UNKNOWN,
487 AGP_1X,
488 AGP_2X,
489 AGP_4X,
490 AGP_8X
491};
492
493static enum pci_bus_speed agp_speed(int agp3, int agpstat)
494{
495 int index = 0;
496
497 if (agpstat & 4)
498 index = 3;
499 else if (agpstat & 2)
500 index = 2;
501 else if (agpstat & 1)
502 index = 1;
503 else
504 goto out;
505
506 if (agp3) {
507 index += 2;
508 if (index == 5)
509 index = 0;
510 }
511
512 out:
513 return agp_speeds[index];
514}
515
516
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500517static void pci_set_bus_speed(struct pci_bus *bus)
518{
519 struct pci_dev *bridge = bus->self;
520 int pos;
521
Matthew Wilcox45b4cdd52009-12-13 08:11:34 -0500522 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
523 if (!pos)
524 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
525 if (pos) {
526 u32 agpstat, agpcmd;
527
528 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
529 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
530
531 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
532 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
533 }
534
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500535 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
536 if (pos) {
537 u16 status;
538 enum pci_bus_speed max;
539 pci_read_config_word(bridge, pos + 2, &status);
540
541 if (status & 0x8000) {
542 max = PCI_SPEED_133MHz_PCIX_533;
543 } else if (status & 0x4000) {
544 max = PCI_SPEED_133MHz_PCIX_266;
545 } else if (status & 0x0002) {
546 if (((status >> 12) & 0x3) == 2) {
547 max = PCI_SPEED_133MHz_PCIX_ECC;
548 } else {
549 max = PCI_SPEED_133MHz_PCIX;
550 }
551 } else {
552 max = PCI_SPEED_66MHz_PCIX;
553 }
554
555 bus->max_bus_speed = max;
556 bus->cur_bus_speed = pcix_bus_speed[(status >> 6) & 0xf];
557
558 return;
559 }
560
561 pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
562 if (pos) {
563 u32 linkcap;
564 u16 linksta;
565
566 pci_read_config_dword(bridge, pos + PCI_EXP_LNKCAP, &linkcap);
567 bus->max_bus_speed = pcie_link_speed[linkcap & 0xf];
568
569 pci_read_config_word(bridge, pos + PCI_EXP_LNKSTA, &linksta);
570 pcie_update_link_speed(bus, linksta);
571 }
572}
573
574
Adrian Bunkcbd4e052008-04-18 13:53:55 -0700575static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
576 struct pci_dev *bridge, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct pci_bus *child;
579 int i;
580
581 /*
582 * Allocate a new bus, and inherit stuff from the parent..
583 */
584 child = pci_alloc_bus();
585 if (!child)
586 return NULL;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 child->parent = parent;
589 child->ops = parent->ops;
590 child->sysdata = parent->sysdata;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200591 child->bus_flags = parent->bus_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400593 /* initialize some portions of the bus device, but don't register it
594 * now as the parent is not properly set up yet. This device will get
595 * registered later in pci_bus_add_devices()
596 */
597 child->dev.class = &pcibus_class;
Kay Sievers1a927132008-10-30 02:17:49 +0100598 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /*
601 * Set up the primary, secondary and subordinate
602 * bus numbers.
603 */
604 child->number = child->secondary = busnr;
605 child->primary = parent->secondary;
606 child->subordinate = 0xff;
607
Yu Zhao3789fa82008-11-22 02:41:07 +0800608 if (!bridge)
609 return child;
610
611 child->self = bridge;
612 child->bridge = get_device(&bridge->dev);
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +1000613 pci_set_bus_of_node(child);
Matthew Wilcox9be60ca2009-12-13 08:11:33 -0500614 pci_set_bus_speed(child);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Set up default resource pointers and names.. */
Yu Zhaofde09c62008-11-22 02:39:32 +0800617 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
619 child->resource[i]->name = child->name;
620 }
621 bridge->subordinate = child;
622
623 return child;
624}
625
Sam Ravnborg451124a2008-02-02 22:33:43 +0100626struct pci_bus *__ref pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct pci_bus *child;
629
630 child = pci_alloc_child_bus(parent, dev, busnr);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700631 if (child) {
Zhang Yanmind71374d2006-06-02 12:35:43 +0800632 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 list_add_tail(&child->node, &parent->children);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800634 up_write(&pci_bus_sem);
Rajesh Shahe4ea9bb2005-04-28 00:25:48 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return child;
637}
638
Sam Ravnborg96bde062007-03-26 21:53:30 -0800639static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
Greg Kroah-Hartman26f674a2005-06-02 15:41:48 -0700640{
641 struct pci_bus *parent = child->parent;
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -0700642
643 /* Attempts to fix that up are really dangerous unless
644 we're going to re-assign all bus numbers. */
645 if (!pcibios_assign_all_busses())
646 return;
647
Greg Kroah-Hartman26f674a2005-06-02 15:41:48 -0700648 while (parent->parent && parent->subordinate < max) {
649 parent->subordinate = max;
650 pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
651 parent = parent->parent;
652 }
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
656 * If it's a bridge, configure it and scan the bus behind it.
657 * For CardBus bridges, we don't scan behind as the devices will
658 * be handled by the bridge driver itself.
659 *
660 * We need to process bridges in two passes -- first we scan those
661 * already configured by the BIOS and after we are done with all of
662 * them, we proceed to assigning numbers to the remaining buses in
663 * order to avoid overlaps between old and new bus numbers.
664 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100665int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct pci_bus *child;
668 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
Dominik Brodowski49887942005-12-08 16:53:12 +0100669 u32 buses, i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 u16 bctl;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600671 u8 primary, secondary, subordinate;
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100672 int broken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600675 primary = buses & 0xFF;
676 secondary = (buses >> 8) & 0xFF;
677 subordinate = (buses >> 16) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600679 dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
680 secondary, subordinate, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Yinghai Lu71f6bd42012-01-30 12:25:24 +0100682 if (!primary && (primary != bus->number) && secondary && subordinate) {
683 dev_warn(&dev->dev, "Primary bus is hard wired to 0\n");
684 primary = bus->number;
685 }
686
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100687 /* Check if setup is sensible at all */
688 if (!pass &&
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600689 (primary != bus->number || secondary <= bus->number)) {
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100690 dev_dbg(&dev->dev, "bus configuration invalid, reconfiguring\n");
691 broken = 1;
692 }
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Disable MasterAbortMode during probing to avoid reporting
695 of bus errors (in some architectures) */
696 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
697 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
698 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
699
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600700 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
701 !is_cardbus && !broken) {
702 unsigned int cmax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /*
704 * Bus already configured by firmware, process it in the first
705 * pass and just note the configuration.
706 */
707 if (pass)
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +0000708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /*
711 * If we already got to this bus through a different bridge,
Alex Chiang74710de2009-03-20 14:56:10 -0600712 * don't re-add it. This can happen with the i450NX chipset.
713 *
714 * However, we continue to descend down the hierarchy and
715 * scan remaining child buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600717 child = pci_find_bus(pci_domain_nr(bus), secondary);
Alex Chiang74710de2009-03-20 14:56:10 -0600718 if (!child) {
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600719 child = pci_add_new_bus(bus, dev, secondary);
Alex Chiang74710de2009-03-20 14:56:10 -0600720 if (!child)
721 goto out;
Bjorn Helgaas99ddd552010-03-16 15:52:58 -0600722 child->primary = primary;
723 child->subordinate = subordinate;
Alex Chiang74710de2009-03-20 14:56:10 -0600724 child->bridge_ctl = bctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 cmax = pci_scan_child_bus(child);
728 if (cmax > max)
729 max = cmax;
730 if (child->subordinate > max)
731 max = child->subordinate;
732 } else {
733 /*
734 * We need to assign a number to this bus which we always
735 * do in the second pass.
736 */
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -0700737 if (!pass) {
Benjamin Herrenschmidta1c19892008-10-21 10:06:29 +1100738 if (pcibios_assign_all_busses() || broken)
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -0700739 /* Temporarily disable forwarding of the
740 configuration cycles on all bridges in
741 this bus segment to avoid possible
742 conflicts in the second pass between two
743 bridges programmed with overlapping
744 bus ranges. */
745 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
746 buses & ~0xffffff);
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +0000747 goto out;
Ivan Kokshaysky12f44f42005-09-22 21:06:31 -0700748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* Clear errors */
751 pci_write_config_word(dev, PCI_STATUS, 0xffff);
752
Rajesh Shahcc574502005-04-28 00:25:47 -0700753 /* Prevent assigning a bus number that already exists.
Tiejun Chenb1a98b62011-06-02 11:02:50 +0800754 * This can happen when a bridge is hot-plugged, so in
755 * this case we only re-scan this bus. */
756 child = pci_find_bus(pci_domain_nr(bus), max+1);
757 if (!child) {
758 child = pci_add_new_bus(bus, dev, ++max);
759 if (!child)
760 goto out;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 buses = (buses & 0xff000000)
763 | ((unsigned int)(child->primary) << 0)
764 | ((unsigned int)(child->secondary) << 8)
765 | ((unsigned int)(child->subordinate) << 16);
766
767 /*
768 * yenta.c forces a secondary latency timer of 176.
769 * Copy that behaviour here.
770 */
771 if (is_cardbus) {
772 buses &= ~0xff000000;
773 buses |= CARDBUS_LATENCY_TIMER << 24;
774 }
Jesper Juhl7c867c82011-01-24 21:14:33 +0100775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * We need to blast all three values with a single write.
778 */
779 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
780
781 if (!is_cardbus) {
Gary Hade11949252007-10-08 16:24:16 -0700782 child->bridge_ctl = bctl;
Greg Kroah-Hartman26f674a2005-06-02 15:41:48 -0700783 /*
784 * Adjust subordinate busnr in parent buses.
785 * We do this before scanning for children because
786 * some devices may not be detected if the bios
787 * was lazy.
788 */
789 pci_fixup_parent_subordinate_busnr(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* Now we can scan all subordinate buses... */
791 max = pci_scan_child_bus(child);
Kristen Accardie3ac86d2006-01-17 16:57:01 -0800792 /*
793 * now fix it up again since we have found
794 * the real value of max.
795 */
796 pci_fixup_parent_subordinate_busnr(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 } else {
798 /*
799 * For CardBus bridges, we leave 4 bus numbers
800 * as cards with a PCI-to-PCI bridge can be
801 * inserted later.
802 */
Dominik Brodowski49887942005-12-08 16:53:12 +0100803 for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {
804 struct pci_bus *parent = bus;
Rajesh Shahcc574502005-04-28 00:25:47 -0700805 if (pci_find_bus(pci_domain_nr(bus),
806 max+i+1))
807 break;
Dominik Brodowski49887942005-12-08 16:53:12 +0100808 while (parent->parent) {
809 if ((!pcibios_assign_all_busses()) &&
810 (parent->subordinate > max) &&
811 (parent->subordinate <= max+i)) {
812 j = 1;
813 }
814 parent = parent->parent;
815 }
816 if (j) {
817 /*
818 * Often, there are two cardbus bridges
819 * -- try to leave one valid bus number
820 * for each one.
821 */
822 i /= 2;
823 break;
824 }
825 }
Rajesh Shahcc574502005-04-28 00:25:47 -0700826 max += i;
Greg Kroah-Hartman26f674a2005-06-02 15:41:48 -0700827 pci_fixup_parent_subordinate_busnr(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 /*
830 * Set the subordinate bus number to its real value.
831 */
832 child->subordinate = max;
833 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
834 }
835
Gary Hadecb3576f2008-02-08 14:00:52 -0800836 sprintf(child->name,
837 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
838 pci_domain_nr(bus), child->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Bernhard Kaindld55bef52007-07-30 20:35:13 +0200840 /* Has only triggered on CardBus, fixup is in yenta_socket */
Dominik Brodowski49887942005-12-08 16:53:12 +0100841 while (bus->parent) {
842 if ((child->subordinate > bus->subordinate) ||
843 (child->number > bus->subordinate) ||
844 (child->number < bus->number) ||
845 (child->subordinate < bus->number)) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700846 dev_info(&child->dev, "[bus %02x-%02x] %s "
847 "hidden behind%s bridge %s [bus %02x-%02x]\n",
Bernhard Kaindld55bef52007-07-30 20:35:13 +0200848 child->number, child->subordinate,
849 (bus->number > child->subordinate &&
850 bus->subordinate < child->number) ?
Joe Perchesa6f29a92007-11-19 17:48:29 -0800851 "wholly" : "partially",
852 bus->self->transparent ? " transparent" : "",
Bjorn Helgaas865df572009-11-04 10:32:57 -0700853 dev_name(&bus->dev),
Bernhard Kaindld55bef52007-07-30 20:35:13 +0200854 bus->number, bus->subordinate);
Dominik Brodowski49887942005-12-08 16:53:12 +0100855 }
856 bus = bus->parent;
857 }
858
Ralf Baechlebbe8f9a2006-02-14 16:23:57 +0000859out:
860 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return max;
863}
864
865/*
866 * Read interrupt line and base address registers.
867 * The architecture-dependent code can tweak these, of course.
868 */
869static void pci_read_irq(struct pci_dev *dev)
870{
871 unsigned char irq;
872
873 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
Kristen Accardiffeff782005-11-02 16:24:32 -0800874 dev->pin = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (irq)
876 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
877 dev->irq = irq;
878}
879
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000880void set_pcie_port_type(struct pci_dev *pdev)
Yu Zhao480b93b2009-03-20 11:25:14 +0800881{
882 int pos;
883 u16 reg16;
884
885 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
886 if (!pos)
887 return;
888 pdev->is_pcie = 1;
Kenji Kaneshige0efea002009-11-05 12:05:11 +0900889 pdev->pcie_cap = pos;
Yu Zhao480b93b2009-03-20 11:25:14 +0800890 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
891 pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
Jon Masonb03e7492011-07-20 15:20:54 -0500892 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
893 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
Yu Zhao480b93b2009-03-20 11:25:14 +0800894}
895
Benjamin Herrenschmidtbb209c82010-01-26 17:10:03 +0000896void set_pcie_hotplug_bridge(struct pci_dev *pdev)
Eric W. Biederman28760482009-09-09 14:09:24 -0700897{
898 int pos;
899 u16 reg16;
900 u32 reg32;
901
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +0900902 pos = pci_pcie_cap(pdev);
Eric W. Biederman28760482009-09-09 14:09:24 -0700903 if (!pos)
904 return;
905 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
906 if (!(reg16 & PCI_EXP_FLAGS_SLOT))
907 return;
908 pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, &reg32);
909 if (reg32 & PCI_EXP_SLTCAP_HPC)
910 pdev->is_hotplug_bridge = 1;
911}
912
Bartlomiej Zolnierkiewicz01abc2a2007-04-23 23:19:36 +0200913#define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
Randy Dunlap76e6a1d2006-12-29 16:47:29 -0800914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/**
916 * pci_setup_device - fill in class and map information of a device
917 * @dev: the device structure to fill
918 *
919 * Initialize the device structure with information about the device's
920 * vendor,class,memory and IO-space addresses,IRQ lines etc.
921 * Called at initialisation of the PCI subsystem and by CardBus services.
Yu Zhao480b93b2009-03-20 11:25:14 +0800922 * Returns 0 on success and negative if unknown type of device (not normal,
923 * bridge or CardBus).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
Yu Zhao480b93b2009-03-20 11:25:14 +0800925int pci_setup_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 u32 class;
Yu Zhao480b93b2009-03-20 11:25:14 +0800928 u8 hdr_type;
929 struct pci_slot *slot;
Gabe Blackbc577d22009-10-06 10:45:19 -0500930 int pos = 0;
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700931 struct pci_bus_region region;
932 struct resource *res;
Yu Zhao480b93b2009-03-20 11:25:14 +0800933
934 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
935 return -EIO;
936
937 dev->sysdata = dev->bus->sysdata;
938 dev->dev.parent = dev->bus->bridge;
939 dev->dev.bus = &pci_bus_type;
940 dev->hdr_type = hdr_type & 0x7f;
941 dev->multifunction = !!(hdr_type & 0x80);
Yu Zhao480b93b2009-03-20 11:25:14 +0800942 dev->error_state = pci_channel_io_normal;
943 set_pcie_port_type(dev);
944
945 list_for_each_entry(slot, &dev->bus->slots, list)
946 if (PCI_SLOT(dev->devfn) == slot->number)
947 dev->slot = slot;
948
949 /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
950 set this higher, assuming the system even supports it. */
951 dev->dma_mask = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Greg Kroah-Hartmaneebfcfb2008-07-02 13:24:49 -0700953 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
954 dev->bus->number, PCI_SLOT(dev->devfn),
955 PCI_FUNC(dev->devfn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
Auke Kokb8a3a522007-06-08 15:46:30 -0700958 dev->revision = class & 0xff;
Yinghai Lu2dd8ba92012-02-19 14:50:12 -0800959 dev->class = class >> 8; /* upper 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Yinghai Lu2dd8ba92012-02-19 14:50:12 -0800961 dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n",
962 dev->vendor, dev->device, dev->hdr_type, dev->class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Yu Zhao853346e2009-03-21 22:05:11 +0800964 /* need to have dev->class ready */
965 dev->cfg_size = pci_cfg_space_size(dev);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* "Unknown power state" */
Daniel Ritz3fe9d192005-08-17 15:32:19 -0700968 dev->current_state = PCI_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* Early fixups, before probing the BARs */
971 pci_fixup_device(pci_fixup_early, dev);
Yu Zhaof79b1b12009-05-28 00:25:05 +0800972 /* device class may be changed after fixup */
973 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 switch (dev->hdr_type) { /* header type */
976 case PCI_HEADER_TYPE_NORMAL: /* standard header */
977 if (class == PCI_CLASS_BRIDGE_PCI)
978 goto bad;
979 pci_read_irq(dev);
980 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
981 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
982 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
Alan Cox368c73d2006-10-04 00:41:26 +0100983
984 /*
985 * Do the ugly legacy mode stuff here rather than broken chip
986 * quirk code. Legacy mode ATA controllers have fixed
987 * addresses. These are not always echoed in BAR0-3, and
988 * BAR0-3 in a few cases contain junk!
989 */
990 if (class == PCI_CLASS_STORAGE_IDE) {
991 u8 progif;
992 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
993 if ((progif & 1) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700994 region.start = 0x1F0;
995 region.end = 0x1F7;
996 res = &dev->resource[0];
997 res->flags = LEGACY_IO_RESOURCE;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -0700998 pcibios_bus_to_resource(dev, res, &region);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -0700999 region.start = 0x3F6;
1000 region.end = 0x3F6;
1001 res = &dev->resource[1];
1002 res->flags = LEGACY_IO_RESOURCE;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -07001003 pcibios_bus_to_resource(dev, res, &region);
Alan Cox368c73d2006-10-04 00:41:26 +01001004 }
1005 if ((progif & 4) == 0) {
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001006 region.start = 0x170;
1007 region.end = 0x177;
1008 res = &dev->resource[2];
1009 res->flags = LEGACY_IO_RESOURCE;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -07001010 pcibios_bus_to_resource(dev, res, &region);
Bjorn Helgaas5bfa14e2012-02-23 20:19:00 -07001011 region.start = 0x376;
1012 region.end = 0x376;
1013 res = &dev->resource[3];
1014 res->flags = LEGACY_IO_RESOURCE;
Bjorn Helgaasfb127cb2012-02-23 20:19:04 -07001015 pcibios_bus_to_resource(dev, res, &region);
Alan Cox368c73d2006-10-04 00:41:26 +01001016 }
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019
1020 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1021 if (class != PCI_CLASS_BRIDGE_PCI)
1022 goto bad;
1023 /* The PCI-to-PCI bridge spec requires that subtractive
1024 decoding (i.e. transparent) bridge must have programming
1025 interface code of 0x01. */
Kristen Accardi3efd2732005-11-02 16:55:49 -08001026 pci_read_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dev->transparent = ((dev->class & 0xff) == 1);
1028 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
Eric W. Biederman28760482009-09-09 14:09:24 -07001029 set_pcie_hotplug_bridge(dev);
Gabe Blackbc577d22009-10-06 10:45:19 -05001030 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1031 if (pos) {
1032 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1033 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
1036
1037 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1038 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1039 goto bad;
1040 pci_read_irq(dev);
1041 pci_read_bases(dev, 1, 0);
1042 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1043 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1044 break;
1045
1046 default: /* unknown header */
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001047 dev_err(&dev->dev, "unknown header type %02x, "
1048 "ignoring device\n", dev->hdr_type);
Yu Zhao480b93b2009-03-20 11:25:14 +08001049 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 bad:
Yinghai Lu2dd8ba92012-02-19 14:50:12 -08001052 dev_err(&dev->dev, "ignoring class %#08x (doesn't match header "
1053 "type %02x)\n", dev->class, dev->hdr_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dev->class = PCI_CLASS_NOT_DEFINED;
1055 }
1056
1057 /* We found a fine healthy device, go go go... */
1058 return 0;
1059}
1060
Zhao, Yu201de562008-10-13 19:49:55 +08001061static void pci_release_capabilities(struct pci_dev *dev)
1062{
1063 pci_vpd_release(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08001064 pci_iov_release(dev);
Yinghai Luf7968412012-02-11 00:18:30 -08001065 pci_free_cap_save_buffers(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/**
1069 * pci_release_dev - free a pci device structure when all users of it are finished.
1070 * @dev: device that's been disconnected
1071 *
1072 * Will be called only by the device core when all users of this pci device are
1073 * done.
1074 */
1075static void pci_release_dev(struct device *dev)
1076{
1077 struct pci_dev *pci_dev;
1078
1079 pci_dev = to_pci_dev(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001080 pci_release_capabilities(pci_dev);
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +10001081 pci_release_of_node(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 kfree(pci_dev);
1083}
1084
1085/**
1086 * pci_cfg_space_size - get the configuration space size of the PCI device.
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001087 * @dev: PCI device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 *
1089 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1090 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1091 * access it. Maybe we don't have a way to generate extended config space
1092 * accesses, or the device is behind a reverse Express bridge. So we try
1093 * reading the dword at 0x100 which must either be 0 or a valid extended
1094 * capability header.
1095 */
Yinghai Lu70b9f7d2008-04-28 16:27:23 -07001096int pci_cfg_space_size_ext(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 u32 status;
Zhao, Yu557848c2008-10-13 19:18:07 +08001099 int pos = PCI_CFG_SPACE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Zhao, Yu557848c2008-10-13 19:18:07 +08001101 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto fail;
1103 if (status == 0xffffffff)
1104 goto fail;
1105
1106 return PCI_CFG_SPACE_EXP_SIZE;
1107
1108 fail:
1109 return PCI_CFG_SPACE_SIZE;
1110}
1111
Yinghai Lu57741a72008-02-15 01:32:50 -08001112int pci_cfg_space_size(struct pci_dev *dev)
1113{
Yinghai Lu70b9f7d2008-04-28 16:27:23 -07001114 int pos;
1115 u32 status;
Yinghai Ludfadd9ed2009-03-08 21:35:37 -07001116 u16 class;
1117
1118 class = dev->class >> 8;
1119 if (class == PCI_CLASS_BRIDGE_HOST)
1120 return pci_cfg_space_size_ext(dev);
Yinghai Lu70b9f7d2008-04-28 16:27:23 -07001121
Kenji Kaneshige06a1cba2009-11-11 14:30:56 +09001122 pos = pci_pcie_cap(dev);
Yinghai Lu70b9f7d2008-04-28 16:27:23 -07001123 if (!pos) {
1124 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1125 if (!pos)
1126 goto fail;
1127
1128 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1129 if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
1130 goto fail;
1131 }
1132
1133 return pci_cfg_space_size_ext(dev);
1134
1135 fail:
1136 return PCI_CFG_SPACE_SIZE;
Yinghai Lu57741a72008-02-15 01:32:50 -08001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static void pci_release_bus_bridge_dev(struct device *dev)
1140{
Yinghai Lu7b543662012-04-02 18:31:53 -07001141 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
1142
Yinghai Lu4fa26492012-04-02 18:31:53 -07001143 if (bridge->release_fn)
1144 bridge->release_fn(bridge);
Yinghai Lu7b543662012-04-02 18:31:53 -07001145
1146 pci_free_resource_list(&bridge->windows);
1147
1148 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Michael Ellerman65891212007-04-05 17:19:08 +10001151struct pci_dev *alloc_pci_dev(void)
1152{
1153 struct pci_dev *dev;
1154
1155 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
1156 if (!dev)
1157 return NULL;
1158
Michael Ellerman65891212007-04-05 17:19:08 +10001159 INIT_LIST_HEAD(&dev->bus_list);
1160
1161 return dev;
1162}
1163EXPORT_SYMBOL(alloc_pci_dev);
1164
Yinghai Luefdc87d2012-01-27 10:55:10 -08001165bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
1166 int crs_timeout)
1167{
1168 int delay = 1;
1169
1170 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1171 return false;
1172
1173 /* some broken boards return 0 or ~0 if a slot is empty: */
1174 if (*l == 0xffffffff || *l == 0x00000000 ||
1175 *l == 0x0000ffff || *l == 0xffff0000)
1176 return false;
1177
1178 /* Configuration request Retry Status */
1179 while (*l == 0xffff0001) {
1180 if (!crs_timeout)
1181 return false;
1182
1183 msleep(delay);
1184 delay *= 2;
1185 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1186 return false;
1187 /* Card hasn't responded in 60 seconds? Must be stuck. */
1188 if (delay > crs_timeout) {
1189 printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
1190 "responding\n", pci_domain_nr(bus),
1191 bus->number, PCI_SLOT(devfn),
1192 PCI_FUNC(devfn));
1193 return false;
1194 }
1195 }
1196
1197 return true;
1198}
1199EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/*
1202 * Read the config data for a PCI device, sanity-check it
1203 * and fill in the dev structure...
1204 */
Adrian Bunk7f7b5de2008-04-18 13:53:55 -07001205static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct pci_dev *dev;
1208 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Yinghai Luefdc87d2012-01-27 10:55:10 -08001210 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return NULL;
1212
Michael Ellermanbab41e92007-04-05 17:19:09 +10001213 dev = alloc_pci_dev();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!dev)
1215 return NULL;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 dev->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 dev->devfn = devfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dev->vendor = l & 0xffff;
1220 dev->device = (l >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +10001222 pci_set_of_node(dev);
1223
Yu Zhao480b93b2009-03-20 11:25:14 +08001224 if (pci_setup_device(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 kfree(dev);
1226 return NULL;
1227 }
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001228
1229 return dev;
1230}
1231
Zhao, Yu201de562008-10-13 19:49:55 +08001232static void pci_init_capabilities(struct pci_dev *dev)
1233{
1234 /* MSI/MSI-X list */
1235 pci_msi_init_pci_dev(dev);
1236
Rafael J. Wysocki63f48982008-12-07 22:02:58 +01001237 /* Buffers for saving PCIe and PCI-X capabilities */
1238 pci_allocate_cap_save_buffers(dev);
1239
Zhao, Yu201de562008-10-13 19:49:55 +08001240 /* Power Management */
1241 pci_pm_init(dev);
Jesse Barneseb9c39d2008-12-17 12:10:05 -08001242 platform_pci_wakeup_init(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001243
1244 /* Vital Product Data */
1245 pci_vpd_pci22_init(dev);
Yu Zhao58c3a722008-10-14 14:02:53 +08001246
1247 /* Alternative Routing-ID Forwarding */
1248 pci_enable_ari(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +08001249
1250 /* Single Root I/O Virtualization */
1251 pci_iov_init(dev);
Allen Kayae21ee62009-10-07 10:27:17 -07001252
1253 /* Enable ACS P2P upstream forwarding */
Chris Wright5d990b62009-12-04 12:15:21 -08001254 pci_enable_acs(dev);
Zhao, Yu201de562008-10-13 19:49:55 +08001255}
1256
Sam Ravnborg96bde062007-03-26 21:53:30 -08001257void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 device_initialize(&dev->dev);
1260 dev->dev.release = pci_release_dev;
1261 pci_dev_get(dev);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 dev->dev.dma_mask = &dev->dma_mask;
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08001264 dev->dev.dma_parms = &dev->dma_parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 dev->dev.coherent_dma_mask = 0xffffffffull;
1266
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08001267 pci_set_dma_max_seg_size(dev, 65536);
FUJITA Tomonori59fc67d2008-02-04 22:28:14 -08001268 pci_set_dma_seg_boundary(dev, 0xffffffff);
FUJITA Tomonori4d57cdf2008-02-04 22:27:55 -08001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* Fix up broken headers */
1271 pci_fixup_device(pci_fixup_header, dev);
1272
Yinghai Lu2069ecf2012-02-15 21:40:31 -08001273 /* moved out from quirk header fixup code */
1274 pci_reassigndev_resource_alignment(dev);
1275
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +02001276 /* Clear the state_saved flag. */
1277 dev->state_saved = false;
1278
Zhao, Yu201de562008-10-13 19:49:55 +08001279 /* Initialize various capabilities */
1280 pci_init_capabilities(dev);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +02001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * Add the device to our list of discovered devices
1284 * and the bus list for fixup functions, etc.
1285 */
Zhang Yanmind71374d2006-06-02 12:35:43 +08001286 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 list_add_tail(&dev->bus_list, &bus->devices);
Zhang Yanmind71374d2006-06-02 12:35:43 +08001288 up_write(&pci_bus_sem);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001289}
1290
Sam Ravnborg451124a2008-02-02 22:33:43 +01001291struct pci_dev *__ref pci_scan_single_device(struct pci_bus *bus, int devfn)
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001292{
1293 struct pci_dev *dev;
1294
Trent Piepho90bdb312009-03-20 14:56:00 -06001295 dev = pci_get_slot(bus, devfn);
1296 if (dev) {
1297 pci_dev_put(dev);
1298 return dev;
1299 }
1300
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001301 dev = pci_scan_device(bus, devfn);
1302 if (!dev)
1303 return NULL;
1304
1305 pci_device_add(dev, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 return dev;
1308}
Adrian Bunkb73e9682007-11-21 15:07:11 -08001309EXPORT_SYMBOL(pci_scan_single_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001311static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn)
1312{
1313 u16 cap;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07001314 unsigned pos, next_fn;
1315
1316 if (!dev)
1317 return 0;
1318
1319 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001320 if (!pos)
1321 return 0;
1322 pci_read_config_word(dev, pos + 4, &cap);
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07001323 next_fn = cap >> 8;
1324 if (next_fn <= fn)
1325 return 0;
1326 return next_fn;
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001327}
1328
1329static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn)
1330{
1331 return (fn + 1) % 8;
1332}
1333
1334static unsigned no_next_fn(struct pci_dev *dev, unsigned fn)
1335{
1336 return 0;
1337}
1338
1339static int only_one_child(struct pci_bus *bus)
1340{
1341 struct pci_dev *parent = bus->self;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06001342
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001343 if (!parent || !pci_is_pcie(parent))
1344 return 0;
Bjorn Helgaas284f5f92012-04-30 15:21:02 -06001345 if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT)
1346 return 1;
1347 if (parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM &&
1348 !pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001349 return 1;
1350 return 0;
1351}
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/**
1354 * pci_scan_slot - scan a PCI slot on a bus for devices.
1355 * @bus: PCI bus to scan
1356 * @devfn: slot number to scan (must have zero function.)
1357 *
1358 * Scan a PCI slot on the specified PCI bus for devices, adding
1359 * discovered devices to the @bus->devices list. New devices
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -08001360 * will not have is_added set.
Trent Piepho1b69dfc2009-03-20 14:56:05 -06001361 *
1362 * Returns the number of new devices found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
Sam Ravnborg96bde062007-03-26 21:53:30 -08001364int pci_scan_slot(struct pci_bus *bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001366 unsigned fn, nr = 0;
Trent Piepho1b69dfc2009-03-20 14:56:05 -06001367 struct pci_dev *dev;
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001368 unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn;
1369
1370 if (only_one_child(bus) && (devfn > 0))
1371 return 0; /* Already scanned the entire slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Trent Piepho1b69dfc2009-03-20 14:56:05 -06001373 dev = pci_scan_single_device(bus, devfn);
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07001374 if (!dev)
1375 return 0;
1376 if (!dev->is_added)
Trent Piepho1b69dfc2009-03-20 14:56:05 -06001377 nr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001379 if (pci_ari_enabled(bus))
1380 next_fn = next_ari_fn;
Matthew Wilcox4fb88c12010-01-17 14:01:41 -07001381 else if (dev->multifunction)
Matthew Wilcoxf07852d2009-12-13 08:10:02 -05001382 next_fn = next_trad_fn;
1383
1384 for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {
1385 dev = pci_scan_single_device(bus, devfn + fn);
1386 if (dev) {
1387 if (!dev->is_added)
1388 nr++;
1389 dev->multifunction = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001392
Shaohua Li149e1632008-07-23 10:32:31 +08001393 /* only one slot has pcie device */
1394 if (bus->self && nr)
Shaohua Li7d715a62008-02-25 09:46:41 +08001395 pcie_aspm_init_link_state(bus->self);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return nr;
1398}
1399
Jon Masonb03e7492011-07-20 15:20:54 -05001400static int pcie_find_smpss(struct pci_dev *dev, void *data)
1401{
1402 u8 *smpss = data;
1403
1404 if (!pci_is_pcie(dev))
1405 return 0;
1406
1407 /* For PCIE hotplug enabled slots not connected directly to a
1408 * PCI-E root port, there can be problems when hotplugging
1409 * devices. This is due to the possibility of hotplugging a
1410 * device into the fabric with a smaller MPS that the devices
1411 * currently running have configured. Modifying the MPS on the
1412 * running devices could cause a fatal bus error due to an
1413 * incoming frame being larger than the newly configured MPS.
1414 * To work around this, the MPS for the entire fabric must be
1415 * set to the minimum size. Any devices hotplugged into this
1416 * fabric will have the minimum MPS set. If the PCI hotplug
1417 * slot is directly connected to the root port and there are not
1418 * other devices on the fabric (which seems to be the most
1419 * common case), then this is not an issue and MPS discovery
1420 * will occur as normal.
1421 */
1422 if (dev->is_hotplug_bridge && (!list_is_singular(&dev->bus->devices) ||
Benjamin Herrenschmidt1a4b1a42011-09-13 15:16:33 -03001423 (dev->bus->self &&
1424 dev->bus->self->pcie_type != PCI_EXP_TYPE_ROOT_PORT)))
Jon Masonb03e7492011-07-20 15:20:54 -05001425 *smpss = 0;
1426
1427 if (*smpss > dev->pcie_mpss)
1428 *smpss = dev->pcie_mpss;
1429
1430 return 0;
1431}
1432
1433static void pcie_write_mps(struct pci_dev *dev, int mps)
1434{
Jon Mason62f392e2011-10-14 14:56:14 -05001435 int rc;
Jon Masonb03e7492011-07-20 15:20:54 -05001436
1437 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
Jon Mason62f392e2011-10-14 14:56:14 -05001438 mps = 128 << dev->pcie_mpss;
Jon Masonb03e7492011-07-20 15:20:54 -05001439
Jon Mason62f392e2011-10-14 14:56:14 -05001440 if (dev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && dev->bus->self)
1441 /* For "Performance", the assumption is made that
Jon Masonb03e7492011-07-20 15:20:54 -05001442 * downstream communication will never be larger than
1443 * the MRRS. So, the MPS only needs to be configured
1444 * for the upstream communication. This being the case,
1445 * walk from the top down and set the MPS of the child
1446 * to that of the parent bus.
Jon Mason62f392e2011-10-14 14:56:14 -05001447 *
1448 * Configure the device MPS with the smaller of the
1449 * device MPSS or the bridge MPS (which is assumed to be
1450 * properly configured at this point to the largest
1451 * allowable MPS based on its parent bus).
Jon Masonb03e7492011-07-20 15:20:54 -05001452 */
Jon Mason62f392e2011-10-14 14:56:14 -05001453 mps = min(mps, pcie_get_mps(dev->bus->self));
Jon Masonb03e7492011-07-20 15:20:54 -05001454 }
1455
1456 rc = pcie_set_mps(dev, mps);
1457 if (rc)
1458 dev_err(&dev->dev, "Failed attempting to set the MPS\n");
1459}
1460
Jon Mason62f392e2011-10-14 14:56:14 -05001461static void pcie_write_mrrs(struct pci_dev *dev)
Jon Masonb03e7492011-07-20 15:20:54 -05001462{
Jon Mason62f392e2011-10-14 14:56:14 -05001463 int rc, mrrs;
Jon Masonb03e7492011-07-20 15:20:54 -05001464
Jon Masoned2888e2011-09-08 16:41:18 -05001465 /* In the "safe" case, do not configure the MRRS. There appear to be
1466 * issues with setting MRRS to 0 on a number of devices.
1467 */
Jon Masoned2888e2011-09-08 16:41:18 -05001468 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
1469 return;
Jon Masonb03e7492011-07-20 15:20:54 -05001470
Jon Masoned2888e2011-09-08 16:41:18 -05001471 /* For Max performance, the MRRS must be set to the largest supported
1472 * value. However, it cannot be configured larger than the MPS the
Jon Mason62f392e2011-10-14 14:56:14 -05001473 * device or the bus can support. This should already be properly
1474 * configured by a prior call to pcie_write_mps.
Jon Masoned2888e2011-09-08 16:41:18 -05001475 */
Jon Mason62f392e2011-10-14 14:56:14 -05001476 mrrs = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05001477
1478 /* MRRS is a R/W register. Invalid values can be written, but a
Jon Masoned2888e2011-09-08 16:41:18 -05001479 * subsequent read will verify if the value is acceptable or not.
Jon Masonb03e7492011-07-20 15:20:54 -05001480 * If the MRRS value provided is not acceptable (e.g., too large),
1481 * shrink the value until it is acceptable to the HW.
1482 */
1483 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
1484 rc = pcie_set_readrq(dev, mrrs);
Jon Mason62f392e2011-10-14 14:56:14 -05001485 if (!rc)
1486 break;
Jon Masonb03e7492011-07-20 15:20:54 -05001487
Jon Mason62f392e2011-10-14 14:56:14 -05001488 dev_warn(&dev->dev, "Failed attempting to set the MRRS\n");
Jon Masonb03e7492011-07-20 15:20:54 -05001489 mrrs /= 2;
1490 }
Jon Mason62f392e2011-10-14 14:56:14 -05001491
1492 if (mrrs < 128)
1493 dev_err(&dev->dev, "MRRS was unable to be configured with a "
1494 "safe value. If problems are experienced, try running "
1495 "with pci=pcie_bus_safe.\n");
Jon Masonb03e7492011-07-20 15:20:54 -05001496}
1497
1498static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
1499{
Jon Masona513a992011-10-14 14:56:16 -05001500 int mps, orig_mps;
Jon Masonb03e7492011-07-20 15:20:54 -05001501
1502 if (!pci_is_pcie(dev))
1503 return 0;
1504
Jon Masona513a992011-10-14 14:56:16 -05001505 mps = 128 << *(u8 *)data;
1506 orig_mps = pcie_get_mps(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05001507
1508 pcie_write_mps(dev, mps);
Jon Mason62f392e2011-10-14 14:56:14 -05001509 pcie_write_mrrs(dev);
Jon Masonb03e7492011-07-20 15:20:54 -05001510
Jon Masona513a992011-10-14 14:56:16 -05001511 dev_info(&dev->dev, "PCI-E Max Payload Size set to %4d/%4d (was %4d), "
1512 "Max Read Rq %4d\n", pcie_get_mps(dev), 128 << dev->pcie_mpss,
1513 orig_mps, pcie_get_readrq(dev));
Jon Masonb03e7492011-07-20 15:20:54 -05001514
1515 return 0;
1516}
1517
Jon Masona513a992011-10-14 14:56:16 -05001518/* pcie_bus_configure_settings requires that pci_walk_bus work in a top-down,
Jon Masonb03e7492011-07-20 15:20:54 -05001519 * parents then children fashion. If this changes, then this code will not
1520 * work as designed.
1521 */
1522void pcie_bus_configure_settings(struct pci_bus *bus, u8 mpss)
1523{
Jon Mason5f39e672011-10-03 09:50:20 -05001524 u8 smpss;
Jon Masonb03e7492011-07-20 15:20:54 -05001525
Jon Masonb03e7492011-07-20 15:20:54 -05001526 if (!pci_is_pcie(bus->self))
1527 return;
1528
Jon Mason5f39e672011-10-03 09:50:20 -05001529 if (pcie_bus_config == PCIE_BUS_TUNE_OFF)
1530 return;
1531
1532 /* FIXME - Peer to peer DMA is possible, though the endpoint would need
1533 * to be aware to the MPS of the destination. To work around this,
1534 * simply force the MPS of the entire system to the smallest possible.
1535 */
1536 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
1537 smpss = 0;
1538
Jon Masonb03e7492011-07-20 15:20:54 -05001539 if (pcie_bus_config == PCIE_BUS_SAFE) {
Jon Mason5f39e672011-10-03 09:50:20 -05001540 smpss = mpss;
1541
Jon Masonb03e7492011-07-20 15:20:54 -05001542 pcie_find_smpss(bus->self, &smpss);
1543 pci_walk_bus(bus, pcie_find_smpss, &smpss);
1544 }
1545
1546 pcie_bus_configure_set(bus->self, &smpss);
1547 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
1548}
Jon Masondebc3b72011-08-02 00:01:18 -05001549EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
Jon Masonb03e7492011-07-20 15:20:54 -05001550
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001551unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 unsigned int devfn, pass, max = bus->secondary;
1554 struct pci_dev *dev;
1555
Bjorn Helgaas0207c352009-11-04 10:32:52 -07001556 dev_dbg(&bus->dev, "scanning bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* Go find them, Rover! */
1559 for (devfn = 0; devfn < 0x100; devfn += 8)
1560 pci_scan_slot(bus, devfn);
1561
Yu Zhaoa28724b2009-03-20 11:25:13 +08001562 /* Reserve buses for SR-IOV capability. */
1563 max += pci_iov_bus_range(bus);
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /*
1566 * After performing arch-dependent fixup of the bus, look behind
1567 * all PCI-to-PCI bridges on this bus.
1568 */
Alex Chiang74710de2009-03-20 14:56:10 -06001569 if (!bus->is_added) {
Bjorn Helgaas0207c352009-11-04 10:32:52 -07001570 dev_dbg(&bus->dev, "fixups for bus\n");
Alex Chiang74710de2009-03-20 14:56:10 -06001571 pcibios_fixup_bus(bus);
1572 if (pci_is_root_bus(bus))
1573 bus->is_added = 1;
1574 }
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 for (pass=0; pass < 2; pass++)
1577 list_for_each_entry(dev, &bus->devices, bus_list) {
1578 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1579 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1580 max = pci_scan_bridge(bus, dev, max, pass);
1581 }
1582
1583 /*
1584 * We've scanned the bus and so we know all about what's on
1585 * the other side of any bridges that may be on this bus plus
1586 * any devices.
1587 *
1588 * Return how far we've got finding sub-buses.
1589 */
Bjorn Helgaas0207c352009-11-04 10:32:52 -07001590 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return max;
1592}
1593
Bjorn Helgaas166c6372011-10-28 16:25:45 -06001594struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
1595 struct pci_ops *ops, void *sysdata, struct list_head *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07001597 int error;
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07001598 struct pci_host_bridge *bridge;
Bjorn Helgaas0207c352009-11-04 10:32:52 -07001599 struct pci_bus *b, *b2;
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07001600 struct pci_host_bridge_window *window, *n;
Bjorn Helgaasa9d9f522011-10-28 16:25:40 -06001601 struct resource *res;
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07001602 resource_size_t offset;
1603 char bus_addr[64];
1604 char *fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07001607 b = pci_alloc_bus();
1608 if (!b)
Yinghai Lu7b543662012-04-02 18:31:53 -07001609 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 b->sysdata = sysdata;
1612 b->ops = ops;
Bjorn Helgaas0207c352009-11-04 10:32:52 -07001613 b2 = pci_find_bus(pci_domain_nr(b), bus);
1614 if (b2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /* If we already got to this bus through a different bridge, ignore it */
Bjorn Helgaas0207c352009-11-04 10:32:52 -07001616 dev_dbg(&b2->dev, "bus already known\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto err_out;
1618 }
Zhang Yanmind71374d2006-06-02 12:35:43 +08001619
Yinghai Lu7b543662012-04-02 18:31:53 -07001620 bridge = pci_alloc_host_bridge(b);
1621 if (!bridge)
1622 goto err_out;
1623
1624 bridge->dev.parent = parent;
1625 bridge->dev.release = pci_release_bus_bridge_dev;
1626 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(b), bus);
1627 error = device_register(&bridge->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (error)
Yinghai Lu7b543662012-04-02 18:31:53 -07001629 goto bridge_dev_reg_err;
1630 b->bridge = get_device(&bridge->dev);
Rafael J. Wysockia1e4d722010-02-08 19:16:33 +01001631 device_enable_async_suspend(b->bridge);
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +10001632 pci_set_bus_of_node(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Yinghai Lu0d358f22008-02-19 03:20:41 -08001634 if (!parent)
1635 set_dev_node(b->bridge, pcibus_to_node(b));
1636
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -04001637 b->dev.class = &pcibus_class;
1638 b->dev.parent = b->bridge;
Kay Sievers1a927132008-10-30 02:17:49 +01001639 dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus);
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -04001640 error = device_register(&b->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (error)
1642 goto class_dev_reg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /* Create legacy_io and legacy_mem files for this bus */
1645 pci_create_legacy_files(b);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 b->number = b->secondary = bus;
Bjorn Helgaas166c6372011-10-28 16:25:45 -06001648
Bjorn Helgaasa9d9f522011-10-28 16:25:40 -06001649 if (parent)
1650 dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev));
1651 else
1652 printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev));
1653
Bjorn Helgaas0efd5aa2012-02-23 20:19:00 -07001654 /* Add initial resources to the bus */
1655 list_for_each_entry_safe(window, n, resources, list) {
1656 list_move_tail(&window->list, &bridge->windows);
1657 res = window->res;
1658 offset = window->offset;
1659 pci_bus_add_resource(b, res, 0);
1660 if (offset) {
1661 if (resource_type(res) == IORESOURCE_IO)
1662 fmt = " (bus address [%#06llx-%#06llx])";
1663 else
1664 fmt = " (bus address [%#010llx-%#010llx])";
1665 snprintf(bus_addr, sizeof(bus_addr), fmt,
1666 (unsigned long long) (res->start - offset),
1667 (unsigned long long) (res->end - offset));
1668 } else
1669 bus_addr[0] = '\0';
1670 dev_info(&b->dev, "root bus resource %pR%s\n", res, bus_addr);
Bjorn Helgaasa9d9f522011-10-28 16:25:40 -06001671 }
1672
Bjorn Helgaasa5390aa2012-02-23 20:18:59 -07001673 down_write(&pci_bus_sem);
1674 list_add_tail(&b->node, &pci_root_buses);
1675 up_write(&pci_bus_sem);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return b;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679class_dev_reg_err:
Yinghai Lu7b543662012-04-02 18:31:53 -07001680 put_device(&bridge->dev);
1681 device_unregister(&bridge->dev);
1682bridge_dev_reg_err:
Bjorn Helgaas5a21d702012-02-23 20:18:59 -07001683 kfree(bridge);
Yinghai Lu7b543662012-04-02 18:31:53 -07001684err_out:
1685 kfree(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return NULL;
1687}
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001688
Bjorn Helgaasa2ebb822011-10-28 16:25:50 -06001689struct pci_bus * __devinit pci_scan_root_bus(struct device *parent, int bus,
1690 struct pci_ops *ops, void *sysdata, struct list_head *resources)
1691{
1692 struct pci_bus *b;
1693
1694 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
1695 if (!b)
1696 return NULL;
1697
1698 b->subordinate = pci_scan_child_bus(b);
1699 pci_bus_add_devices(b);
1700 return b;
1701}
1702EXPORT_SYMBOL(pci_scan_root_bus);
1703
Bjorn Helgaas7e00fe22011-10-28 16:26:05 -06001704/* Deprecated; use pci_scan_root_bus() instead */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001705struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent,
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001706 int bus, struct pci_ops *ops, void *sysdata)
1707{
Bjorn Helgaas1e39ae92011-10-28 16:26:00 -06001708 LIST_HEAD(resources);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001709 struct pci_bus *b;
1710
Bjorn Helgaas1e39ae92011-10-28 16:26:00 -06001711 pci_add_resource(&resources, &ioport_resource);
1712 pci_add_resource(&resources, &iomem_resource);
1713 b = pci_create_root_bus(parent, bus, ops, sysdata, &resources);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001714 if (b)
1715 b->subordinate = pci_scan_child_bus(b);
Bjorn Helgaas1e39ae92011-10-28 16:26:00 -06001716 else
1717 pci_free_resource_list(&resources);
Paul Mackerrascdb9b9f2005-09-06 09:31:03 +10001718 return b;
1719}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720EXPORT_SYMBOL(pci_scan_bus_parented);
1721
Bjorn Helgaasde4b2f72011-10-28 16:25:55 -06001722struct pci_bus * __devinit pci_scan_bus(int bus, struct pci_ops *ops,
1723 void *sysdata)
1724{
1725 LIST_HEAD(resources);
1726 struct pci_bus *b;
1727
1728 pci_add_resource(&resources, &ioport_resource);
1729 pci_add_resource(&resources, &iomem_resource);
1730 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
1731 if (b) {
1732 b->subordinate = pci_scan_child_bus(b);
1733 pci_bus_add_devices(b);
1734 } else {
1735 pci_free_resource_list(&resources);
1736 }
1737 return b;
1738}
1739EXPORT_SYMBOL(pci_scan_bus);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741#ifdef CONFIG_HOTPLUG
Alex Chiang3ed4fd92009-03-20 14:56:25 -06001742/**
Yinghai Lu2f320522012-01-21 02:08:22 -08001743 * pci_rescan_bus_bridge_resize - scan a PCI bus for devices.
1744 * @bridge: PCI bridge for the bus to scan
1745 *
1746 * Scan a PCI bus and child buses for new devices, add them,
1747 * and enable them, resizing bridge mmio/io resource if necessary
1748 * and possible. The caller must ensure the child devices are already
1749 * removed for resizing to occur.
1750 *
1751 * Returns the max number of subordinate bus discovered.
1752 */
1753unsigned int __ref pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
1754{
1755 unsigned int max;
1756 struct pci_bus *bus = bridge->subordinate;
1757
1758 max = pci_scan_child_bus(bus);
1759
1760 pci_assign_unassigned_bridge_resources(bridge);
1761
1762 pci_bus_add_devices(bus);
1763
1764 return max;
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767EXPORT_SYMBOL(pci_add_new_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768EXPORT_SYMBOL(pci_scan_slot);
1769EXPORT_SYMBOL(pci_scan_bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770EXPORT_SYMBOL_GPL(pci_scan_child_bus);
1771#endif
Matt Domsch6b4b78f2006-09-29 15:23:23 -05001772
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05001773static int __init pci_sort_bf_cmp(const struct device *d_a, const struct device *d_b)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05001774{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05001775 const struct pci_dev *a = to_pci_dev(d_a);
1776 const struct pci_dev *b = to_pci_dev(d_b);
1777
Matt Domsch6b4b78f2006-09-29 15:23:23 -05001778 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
1779 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
1780
1781 if (a->bus->number < b->bus->number) return -1;
1782 else if (a->bus->number > b->bus->number) return 1;
1783
1784 if (a->devfn < b->devfn) return -1;
1785 else if (a->devfn > b->devfn) return 1;
1786
1787 return 0;
1788}
1789
Greg Kroah-Hartman5ff580c2008-02-14 14:56:56 -08001790void __init pci_sort_breadthfirst(void)
Matt Domsch6b4b78f2006-09-29 15:23:23 -05001791{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -05001792 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
Matt Domsch6b4b78f2006-09-29 15:23:23 -05001793}