PCI: Move pci_configure_slot() to drivers/pci/probe.c
[deliverable/linux.git] / drivers / pci / probe.c
CommitLineData
1da177e4
LT
1/*
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>
589fcc23 9#include <linux/pci_hotplug.h>
1da177e4
LT
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/cpumask.h>
7d715a6c 13#include <linux/pci-aspm.h>
284f5f9d 14#include <asm-generic/pci-bridge.h>
bc56b9e0 15#include "pci.h"
1da177e4
LT
16
17#define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
18#define CARDBUS_RESERVE_BUSNR 3
1da177e4 19
0b950f0f 20static struct resource busn_resource = {
67cdc827
YL
21 .name = "PCI busn",
22 .start = 0,
23 .end = 255,
24 .flags = IORESOURCE_BUS,
25};
26
1da177e4
LT
27/* Ugh. Need to stop exporting this to modules. */
28LIST_HEAD(pci_root_buses);
29EXPORT_SYMBOL(pci_root_buses);
30
5cc62c20
YL
31static LIST_HEAD(pci_domain_busn_res_list);
32
33struct pci_domain_busn_res {
34 struct list_head list;
35 struct resource res;
36 int domain_nr;
37};
38
39static struct resource *get_pci_domain_busn_res(int domain_nr)
40{
41 struct pci_domain_busn_res *r;
42
43 list_for_each_entry(r, &pci_domain_busn_res_list, list)
44 if (r->domain_nr == domain_nr)
45 return &r->res;
46
47 r = kzalloc(sizeof(*r), GFP_KERNEL);
48 if (!r)
49 return NULL;
50
51 r->domain_nr = domain_nr;
52 r->res.start = 0;
53 r->res.end = 0xff;
54 r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
55
56 list_add_tail(&r->list, &pci_domain_busn_res_list);
57
58 return &r->res;
59}
60
70308923
GKH
61static int find_anything(struct device *dev, void *data)
62{
63 return 1;
64}
1da177e4 65
ed4aaadb
ZY
66/*
67 * Some device drivers need know if pci is initiated.
68 * Basically, we think pci is not initiated when there
70308923 69 * is no device to be found on the pci_bus_type.
ed4aaadb
ZY
70 */
71int no_pci_devices(void)
72{
70308923
GKH
73 struct device *dev;
74 int no_devices;
ed4aaadb 75
70308923
GKH
76 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
77 no_devices = (dev == NULL);
78 put_device(dev);
79 return no_devices;
80}
ed4aaadb
ZY
81EXPORT_SYMBOL(no_pci_devices);
82
1da177e4
LT
83/*
84 * PCI Bus Class
85 */
fd7d1ced 86static void release_pcibus_dev(struct device *dev)
1da177e4 87{
fd7d1ced 88 struct pci_bus *pci_bus = to_pci_bus(dev);
1da177e4
LT
89
90 if (pci_bus->bridge)
91 put_device(pci_bus->bridge);
2fe2abf8 92 pci_bus_remove_resources(pci_bus);
98d9f30c 93 pci_release_bus_of_node(pci_bus);
1da177e4
LT
94 kfree(pci_bus);
95}
96
97static struct class pcibus_class = {
98 .name = "pci_bus",
fd7d1ced 99 .dev_release = &release_pcibus_dev,
56039e65 100 .dev_groups = pcibus_groups,
1da177e4
LT
101};
102
103static int __init pcibus_class_init(void)
104{
105 return class_register(&pcibus_class);
106}
107postcore_initcall(pcibus_class_init);
108
6ac665c6 109static u64 pci_size(u64 base, u64 maxbase, u64 mask)
1da177e4 110{
6ac665c6 111 u64 size = mask & maxbase; /* Find the significant bits */
1da177e4
LT
112 if (!size)
113 return 0;
114
115 /* Get the lowest of them to find the decode size, and
116 from that the extent. */
117 size = (size & ~(size-1)) - 1;
118
119 /* base == maxbase can be valid only if the BAR has
120 already been programmed with all 1s. */
121 if (base == maxbase && ((base | size) & mask) != mask)
122 return 0;
123
124 return size;
125}
126
28c6821a 127static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
6ac665c6 128{
8d6a6a47 129 u32 mem_type;
28c6821a 130 unsigned long flags;
8d6a6a47 131
6ac665c6 132 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
28c6821a
BH
133 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
134 flags |= IORESOURCE_IO;
135 return flags;
6ac665c6 136 }
07eddf3d 137
28c6821a
BH
138 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
139 flags |= IORESOURCE_MEM;
140 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
141 flags |= IORESOURCE_PREFETCH;
07eddf3d 142
8d6a6a47
BH
143 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
144 switch (mem_type) {
145 case PCI_BASE_ADDRESS_MEM_TYPE_32:
146 break;
147 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
0ff9514b 148 /* 1M mem BAR treated as 32-bit BAR */
8d6a6a47
BH
149 break;
150 case PCI_BASE_ADDRESS_MEM_TYPE_64:
28c6821a
BH
151 flags |= IORESOURCE_MEM_64;
152 break;
8d6a6a47 153 default:
0ff9514b 154 /* mem unknown type treated as 32-bit BAR */
8d6a6a47
BH
155 break;
156 }
28c6821a 157 return flags;
07eddf3d
YL
158}
159
808e34e2
ZK
160#define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
161
0b400c7e
YZ
162/**
163 * pci_read_base - read a PCI BAR
164 * @dev: the PCI device
165 * @type: type of the BAR
166 * @res: resource buffer to be filled in
167 * @pos: BAR position in the config space
168 *
169 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
6ac665c6 170 */
0b400c7e 171int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
3c78bc61 172 struct resource *res, unsigned int pos)
07eddf3d 173{
6ac665c6 174 u32 l, sz, mask;
23b13bc7 175 u64 l64, sz64, mask64;
253d2e54 176 u16 orig_cmd;
cf4d1cf5 177 struct pci_bus_region region, inverted_region;
26370fc6 178 bool bar_too_big = false, bar_too_high = false, bar_invalid = false;
6ac665c6 179
1ed67439 180 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
6ac665c6 181
0ff9514b 182 /* No printks while decoding is disabled! */
253d2e54
JP
183 if (!dev->mmio_always_on) {
184 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
808e34e2
ZK
185 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
186 pci_write_config_word(dev, PCI_COMMAND,
187 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
188 }
253d2e54
JP
189 }
190
6ac665c6
MW
191 res->name = pci_name(dev);
192
193 pci_read_config_dword(dev, pos, &l);
1ed67439 194 pci_write_config_dword(dev, pos, l | mask);
6ac665c6
MW
195 pci_read_config_dword(dev, pos, &sz);
196 pci_write_config_dword(dev, pos, l);
197
198 /*
199 * All bits set in sz means the device isn't working properly.
45aa23b4
BH
200 * If the BAR isn't implemented, all bits must be 0. If it's a
201 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
202 * 1 must be clear.
6ac665c6 203 */
45aa23b4 204 if (!sz || sz == 0xffffffff)
6ac665c6
MW
205 goto fail;
206
207 /*
208 * I don't know how l can have all bits set. Copied from old code.
209 * Maybe it fixes a bug on some ancient platform.
210 */
211 if (l == 0xffffffff)
212 l = 0;
213
214 if (type == pci_bar_unknown) {
28c6821a
BH
215 res->flags = decode_bar(dev, l);
216 res->flags |= IORESOURCE_SIZEALIGN;
217 if (res->flags & IORESOURCE_IO) {
6ac665c6 218 l &= PCI_BASE_ADDRESS_IO_MASK;
5aceca9d 219 mask = PCI_BASE_ADDRESS_IO_MASK & (u32) IO_SPACE_LIMIT;
6ac665c6
MW
220 } else {
221 l &= PCI_BASE_ADDRESS_MEM_MASK;
222 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
223 }
224 } else {
225 res->flags |= (l & IORESOURCE_ROM_ENABLE);
226 l &= PCI_ROM_ADDRESS_MASK;
227 mask = (u32)PCI_ROM_ADDRESS_MASK;
228 }
229
28c6821a 230 if (res->flags & IORESOURCE_MEM_64) {
23b13bc7
BH
231 l64 = l;
232 sz64 = sz;
233 mask64 = mask | (u64)~0 << 32;
6ac665c6
MW
234
235 pci_read_config_dword(dev, pos + 4, &l);
236 pci_write_config_dword(dev, pos + 4, ~0);
237 pci_read_config_dword(dev, pos + 4, &sz);
238 pci_write_config_dword(dev, pos + 4, l);
239
240 l64 |= ((u64)l << 32);
241 sz64 |= ((u64)sz << 32);
242
243 sz64 = pci_size(l64, sz64, mask64);
244
245 if (!sz64)
246 goto fail;
247
23b13bc7
BH
248 if ((sizeof(dma_addr_t) < 8 || sizeof(resource_size_t) < 8) &&
249 sz64 > 0x100000000ULL) {
250 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
251 res->start = 0;
252 res->end = 0;
0ff9514b 253 bar_too_big = true;
23b13bc7 254 goto out;
c7dabef8
BH
255 }
256
d1a313e4 257 if ((sizeof(dma_addr_t) < 8) && l) {
31e9dd25 258 /* Above 32-bit boundary; try to reallocate */
c83bd900 259 res->flags |= IORESOURCE_UNSET;
72dc5601
BH
260 res->start = 0;
261 res->end = sz64;
31e9dd25 262 bar_too_high = true;
72dc5601 263 goto out;
6ac665c6 264 } else {
5bfa14ed
BH
265 region.start = l64;
266 region.end = l64 + sz64;
6ac665c6
MW
267 }
268 } else {
45aa23b4 269 sz = pci_size(l, sz, mask);
6ac665c6 270
45aa23b4 271 if (!sz)
6ac665c6
MW
272 goto fail;
273
5bfa14ed
BH
274 region.start = l;
275 region.end = l + sz;
6ac665c6
MW
276 }
277
fc279850
YL
278 pcibios_bus_to_resource(dev->bus, res, &region);
279 pcibios_resource_to_bus(dev->bus, &inverted_region, res);
cf4d1cf5
KH
280
281 /*
282 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
283 * the corresponding resource address (the physical address used by
284 * the CPU. Converting that resource address back to a bus address
285 * should yield the original BAR value:
286 *
287 * resource_to_bus(bus_to_resource(A)) == A
288 *
289 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
290 * be claimed by the device.
291 */
292 if (inverted_region.start != region.start) {
cf4d1cf5 293 res->flags |= IORESOURCE_UNSET;
cf4d1cf5 294 res->start = 0;
26370fc6
BH
295 res->end = region.end - region.start;
296 bar_invalid = true;
cf4d1cf5 297 }
96ddef25 298
0ff9514b
BH
299 goto out;
300
301
302fail:
303 res->flags = 0;
304out:
808e34e2
ZK
305 if (!dev->mmio_always_on &&
306 (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
bbffe435
BH
307 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
308
0ff9514b 309 if (bar_too_big)
23b13bc7
BH
310 dev_err(&dev->dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
311 pos, (unsigned long long) sz64);
31e9dd25
BH
312 if (bar_too_high)
313 dev_info(&dev->dev, "reg 0x%x: can't handle BAR above 4G (bus address %#010llx)\n",
314 pos, (unsigned long long) l64);
26370fc6
BH
315 if (bar_invalid)
316 dev_info(&dev->dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
317 pos, (unsigned long long) region.start);
31e9dd25 318 if (res->flags)
33963e30 319 dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res);
0ff9514b 320
28c6821a 321 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
07eddf3d
YL
322}
323
1da177e4
LT
324static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
325{
6ac665c6 326 unsigned int pos, reg;
07eddf3d 327
6ac665c6
MW
328 for (pos = 0; pos < howmany; pos++) {
329 struct resource *res = &dev->resource[pos];
1da177e4 330 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
6ac665c6 331 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
1da177e4 332 }
6ac665c6 333
1da177e4 334 if (rom) {
6ac665c6 335 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
1da177e4 336 dev->rom_base_reg = rom;
6ac665c6
MW
337 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
338 IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
339 IORESOURCE_SIZEALIGN;
340 __pci_read_base(dev, pci_bar_mem32, res, rom);
1da177e4
LT
341 }
342}
343
15856ad5 344static void pci_read_bridge_io(struct pci_bus *child)
1da177e4
LT
345{
346 struct pci_dev *dev = child->self;
347 u8 io_base_lo, io_limit_lo;
2b28ae19 348 unsigned long io_mask, io_granularity, base, limit;
5bfa14ed 349 struct pci_bus_region region;
2b28ae19
BH
350 struct resource *res;
351
352 io_mask = PCI_IO_RANGE_MASK;
353 io_granularity = 0x1000;
354 if (dev->io_window_1k) {
355 /* Support 1K I/O space granularity */
356 io_mask = PCI_IO_1K_RANGE_MASK;
357 io_granularity = 0x400;
358 }
1da177e4 359
1da177e4
LT
360 res = child->resource[0];
361 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
362 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
2b28ae19
BH
363 base = (io_base_lo & io_mask) << 8;
364 limit = (io_limit_lo & io_mask) << 8;
1da177e4
LT
365
366 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
367 u16 io_base_hi, io_limit_hi;
8f38eaca 368
1da177e4
LT
369 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
370 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
8f38eaca
BH
371 base |= ((unsigned long) io_base_hi << 16);
372 limit |= ((unsigned long) io_limit_hi << 16);
1da177e4
LT
373 }
374
5dde383e 375 if (base <= limit) {
1da177e4 376 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
5bfa14ed 377 region.start = base;
2b28ae19 378 region.end = limit + io_granularity - 1;
fc279850 379 pcibios_bus_to_resource(dev->bus, res, &region);
c7dabef8 380 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
1da177e4 381 }
fa27b2d1
BH
382}
383
15856ad5 384static void pci_read_bridge_mmio(struct pci_bus *child)
fa27b2d1
BH
385{
386 struct pci_dev *dev = child->self;
387 u16 mem_base_lo, mem_limit_lo;
388 unsigned long base, limit;
5bfa14ed 389 struct pci_bus_region region;
fa27b2d1 390 struct resource *res;
1da177e4
LT
391
392 res = child->resource[1];
393 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
394 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
8f38eaca
BH
395 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
396 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
5dde383e 397 if (base <= limit) {
1da177e4 398 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
5bfa14ed
BH
399 region.start = base;
400 region.end = limit + 0xfffff;
fc279850 401 pcibios_bus_to_resource(dev->bus, res, &region);
c7dabef8 402 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
1da177e4 403 }
fa27b2d1
BH
404}
405
15856ad5 406static void pci_read_bridge_mmio_pref(struct pci_bus *child)
fa27b2d1
BH
407{
408 struct pci_dev *dev = child->self;
409 u16 mem_base_lo, mem_limit_lo;
410 unsigned long base, limit;
5bfa14ed 411 struct pci_bus_region region;
fa27b2d1 412 struct resource *res;
1da177e4
LT
413
414 res = child->resource[2];
415 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
416 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
8f38eaca
BH
417 base = ((unsigned long) mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
418 limit = ((unsigned long) mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
1da177e4
LT
419
420 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
421 u32 mem_base_hi, mem_limit_hi;
8f38eaca 422
1da177e4
LT
423 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
424 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
425
426 /*
427 * Some bridges set the base > limit by default, and some
428 * (broken) BIOSes do not initialize them. If we find
429 * this, just assume they are not being used.
430 */
431 if (mem_base_hi <= mem_limit_hi) {
432#if BITS_PER_LONG == 64
8f38eaca
BH
433 base |= ((unsigned long) mem_base_hi) << 32;
434 limit |= ((unsigned long) mem_limit_hi) << 32;
1da177e4
LT
435#else
436 if (mem_base_hi || mem_limit_hi) {
227f0647 437 dev_err(&dev->dev, "can't handle 64-bit address space for bridge\n");
1da177e4
LT
438 return;
439 }
440#endif
441 }
442 }
5dde383e 443 if (base <= limit) {
1f82de10
YL
444 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
445 IORESOURCE_MEM | IORESOURCE_PREFETCH;
446 if (res->flags & PCI_PREF_RANGE_TYPE_64)
447 res->flags |= IORESOURCE_MEM_64;
5bfa14ed
BH
448 region.start = base;
449 region.end = limit + 0xfffff;
fc279850 450 pcibios_bus_to_resource(dev->bus, res, &region);
c7dabef8 451 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
1da177e4
LT
452 }
453}
454
15856ad5 455void pci_read_bridge_bases(struct pci_bus *child)
fa27b2d1
BH
456{
457 struct pci_dev *dev = child->self;
2fe2abf8 458 struct resource *res;
fa27b2d1
BH
459 int i;
460
461 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
462 return;
463
b918c62e
YL
464 dev_info(&dev->dev, "PCI bridge to %pR%s\n",
465 &child->busn_res,
fa27b2d1
BH
466 dev->transparent ? " (subtractive decode)" : "");
467
2fe2abf8
BH
468 pci_bus_remove_resources(child);
469 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
470 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
471
fa27b2d1
BH
472 pci_read_bridge_io(child);
473 pci_read_bridge_mmio(child);
474 pci_read_bridge_mmio_pref(child);
2adf7516
BH
475
476 if (dev->transparent) {
2fe2abf8 477 pci_bus_for_each_resource(child->parent, res, i) {
d739a099 478 if (res && res->flags) {
2fe2abf8
BH
479 pci_bus_add_resource(child, res,
480 PCI_SUBTRACTIVE_DECODE);
2adf7516
BH
481 dev_printk(KERN_DEBUG, &dev->dev,
482 " bridge window %pR (subtractive decode)\n",
2fe2abf8
BH
483 res);
484 }
2adf7516
BH
485 }
486 }
fa27b2d1
BH
487}
488
05013486 489static struct pci_bus *pci_alloc_bus(void)
1da177e4
LT
490{
491 struct pci_bus *b;
492
f5afe806 493 b = kzalloc(sizeof(*b), GFP_KERNEL);
05013486
BH
494 if (!b)
495 return NULL;
496
497 INIT_LIST_HEAD(&b->node);
498 INIT_LIST_HEAD(&b->children);
499 INIT_LIST_HEAD(&b->devices);
500 INIT_LIST_HEAD(&b->slots);
501 INIT_LIST_HEAD(&b->resources);
502 b->max_bus_speed = PCI_SPEED_UNKNOWN;
503 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
1da177e4
LT
504 return b;
505}
506
70efde2a
JL
507static void pci_release_host_bridge_dev(struct device *dev)
508{
509 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
510
511 if (bridge->release_fn)
512 bridge->release_fn(bridge);
513
514 pci_free_resource_list(&bridge->windows);
515
516 kfree(bridge);
517}
518
7b543663
YL
519static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b)
520{
521 struct pci_host_bridge *bridge;
522
523 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
05013486
BH
524 if (!bridge)
525 return NULL;
7b543663 526
05013486
BH
527 INIT_LIST_HEAD(&bridge->windows);
528 bridge->bus = b;
7b543663
YL
529 return bridge;
530}
531
0b950f0f 532static const unsigned char pcix_bus_speed[] = {
9be60ca0
MW
533 PCI_SPEED_UNKNOWN, /* 0 */
534 PCI_SPEED_66MHz_PCIX, /* 1 */
535 PCI_SPEED_100MHz_PCIX, /* 2 */
536 PCI_SPEED_133MHz_PCIX, /* 3 */
537 PCI_SPEED_UNKNOWN, /* 4 */
538 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
539 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
540 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
541 PCI_SPEED_UNKNOWN, /* 8 */
542 PCI_SPEED_66MHz_PCIX_266, /* 9 */
543 PCI_SPEED_100MHz_PCIX_266, /* A */
544 PCI_SPEED_133MHz_PCIX_266, /* B */
545 PCI_SPEED_UNKNOWN, /* C */
546 PCI_SPEED_66MHz_PCIX_533, /* D */
547 PCI_SPEED_100MHz_PCIX_533, /* E */
548 PCI_SPEED_133MHz_PCIX_533 /* F */
549};
550
343e51ae 551const unsigned char pcie_link_speed[] = {
3749c51a
MW
552 PCI_SPEED_UNKNOWN, /* 0 */
553 PCIE_SPEED_2_5GT, /* 1 */
554 PCIE_SPEED_5_0GT, /* 2 */
9dfd97fe 555 PCIE_SPEED_8_0GT, /* 3 */
3749c51a
MW
556 PCI_SPEED_UNKNOWN, /* 4 */
557 PCI_SPEED_UNKNOWN, /* 5 */
558 PCI_SPEED_UNKNOWN, /* 6 */
559 PCI_SPEED_UNKNOWN, /* 7 */
560 PCI_SPEED_UNKNOWN, /* 8 */
561 PCI_SPEED_UNKNOWN, /* 9 */
562 PCI_SPEED_UNKNOWN, /* A */
563 PCI_SPEED_UNKNOWN, /* B */
564 PCI_SPEED_UNKNOWN, /* C */
565 PCI_SPEED_UNKNOWN, /* D */
566 PCI_SPEED_UNKNOWN, /* E */
567 PCI_SPEED_UNKNOWN /* F */
568};
569
570void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
571{
231afea1 572 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
3749c51a
MW
573}
574EXPORT_SYMBOL_GPL(pcie_update_link_speed);
575
45b4cdd5
MW
576static unsigned char agp_speeds[] = {
577 AGP_UNKNOWN,
578 AGP_1X,
579 AGP_2X,
580 AGP_4X,
581 AGP_8X
582};
583
584static enum pci_bus_speed agp_speed(int agp3, int agpstat)
585{
586 int index = 0;
587
588 if (agpstat & 4)
589 index = 3;
590 else if (agpstat & 2)
591 index = 2;
592 else if (agpstat & 1)
593 index = 1;
594 else
595 goto out;
f7625980 596
45b4cdd5
MW
597 if (agp3) {
598 index += 2;
599 if (index == 5)
600 index = 0;
601 }
602
603 out:
604 return agp_speeds[index];
605}
606
9be60ca0
MW
607static void pci_set_bus_speed(struct pci_bus *bus)
608{
609 struct pci_dev *bridge = bus->self;
610 int pos;
611
45b4cdd5
MW
612 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
613 if (!pos)
614 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
615 if (pos) {
616 u32 agpstat, agpcmd;
617
618 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
619 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
620
621 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
622 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
623 }
624
9be60ca0
MW
625 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
626 if (pos) {
627 u16 status;
628 enum pci_bus_speed max;
9be60ca0 629
7793eeab
BH
630 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
631 &status);
632
633 if (status & PCI_X_SSTATUS_533MHZ) {
9be60ca0 634 max = PCI_SPEED_133MHz_PCIX_533;
7793eeab 635 } else if (status & PCI_X_SSTATUS_266MHZ) {
9be60ca0 636 max = PCI_SPEED_133MHz_PCIX_266;
7793eeab 637 } else if (status & PCI_X_SSTATUS_133MHZ) {
3c78bc61 638 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
9be60ca0 639 max = PCI_SPEED_133MHz_PCIX_ECC;
3c78bc61 640 else
9be60ca0 641 max = PCI_SPEED_133MHz_PCIX;
9be60ca0
MW
642 } else {
643 max = PCI_SPEED_66MHz_PCIX;
644 }
645
646 bus->max_bus_speed = max;
7793eeab
BH
647 bus->cur_bus_speed = pcix_bus_speed[
648 (status & PCI_X_SSTATUS_FREQ) >> 6];
9be60ca0
MW
649
650 return;
651 }
652
fdfe1511 653 if (pci_is_pcie(bridge)) {
9be60ca0
MW
654 u32 linkcap;
655 u16 linksta;
656
59875ae4 657 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
231afea1 658 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
9be60ca0 659
59875ae4 660 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
9be60ca0
MW
661 pcie_update_link_speed(bus, linksta);
662 }
663}
664
cbd4e055
AB
665static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
666 struct pci_dev *bridge, int busnr)
1da177e4
LT
667{
668 struct pci_bus *child;
669 int i;
4f535093 670 int ret;
1da177e4
LT
671
672 /*
673 * Allocate a new bus, and inherit stuff from the parent..
674 */
675 child = pci_alloc_bus();
676 if (!child)
677 return NULL;
678
1da177e4
LT
679 child->parent = parent;
680 child->ops = parent->ops;
0cbdcfcf 681 child->msi = parent->msi;
1da177e4 682 child->sysdata = parent->sysdata;
6e325a62 683 child->bus_flags = parent->bus_flags;
1da177e4 684
fd7d1ced 685 /* initialize some portions of the bus device, but don't register it
4f535093 686 * now as the parent is not properly set up yet.
fd7d1ced
GKH
687 */
688 child->dev.class = &pcibus_class;
1a927133 689 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
1da177e4
LT
690
691 /*
692 * Set up the primary, secondary and subordinate
693 * bus numbers.
694 */
b918c62e
YL
695 child->number = child->busn_res.start = busnr;
696 child->primary = parent->busn_res.start;
697 child->busn_res.end = 0xff;
1da177e4 698
4f535093
YL
699 if (!bridge) {
700 child->dev.parent = parent->bridge;
701 goto add_dev;
702 }
3789fa8a
YZ
703
704 child->self = bridge;
705 child->bridge = get_device(&bridge->dev);
4f535093 706 child->dev.parent = child->bridge;
98d9f30c 707 pci_set_bus_of_node(child);
9be60ca0
MW
708 pci_set_bus_speed(child);
709
1da177e4 710 /* Set up default resource pointers and names.. */
fde09c6d 711 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
1da177e4
LT
712 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
713 child->resource[i]->name = child->name;
714 }
715 bridge->subordinate = child;
716
4f535093
YL
717add_dev:
718 ret = device_register(&child->dev);
719 WARN_ON(ret < 0);
720
10a95747
JL
721 pcibios_add_bus(child);
722
4f535093
YL
723 /* Create legacy_io and legacy_mem files for this bus */
724 pci_create_legacy_files(child);
725
1da177e4
LT
726 return child;
727}
728
3c78bc61
RD
729struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
730 int busnr)
1da177e4
LT
731{
732 struct pci_bus *child;
733
734 child = pci_alloc_child_bus(parent, dev, busnr);
e4ea9bb7 735 if (child) {
d71374da 736 down_write(&pci_bus_sem);
1da177e4 737 list_add_tail(&child->node, &parent->children);
d71374da 738 up_write(&pci_bus_sem);
e4ea9bb7 739 }
1da177e4
LT
740 return child;
741}
b7fe9434 742EXPORT_SYMBOL(pci_add_new_bus);
1da177e4 743
1da177e4
LT
744/*
745 * If it's a bridge, configure it and scan the bus behind it.
746 * For CardBus bridges, we don't scan behind as the devices will
747 * be handled by the bridge driver itself.
748 *
749 * We need to process bridges in two passes -- first we scan those
750 * already configured by the BIOS and after we are done with all of
751 * them, we proceed to assigning numbers to the remaining buses in
752 * order to avoid overlaps between old and new bus numbers.
753 */
15856ad5 754int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1da177e4
LT
755{
756 struct pci_bus *child;
757 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
49887941 758 u32 buses, i, j = 0;
1da177e4 759 u16 bctl;
99ddd552 760 u8 primary, secondary, subordinate;
a1c19894 761 int broken = 0;
1da177e4
LT
762
763 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
99ddd552
BH
764 primary = buses & 0xFF;
765 secondary = (buses >> 8) & 0xFF;
766 subordinate = (buses >> 16) & 0xFF;
1da177e4 767
99ddd552
BH
768 dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
769 secondary, subordinate, pass);
1da177e4 770
71f6bd4a
YL
771 if (!primary && (primary != bus->number) && secondary && subordinate) {
772 dev_warn(&dev->dev, "Primary bus is hard wired to 0\n");
773 primary = bus->number;
774 }
775
a1c19894
BH
776 /* Check if setup is sensible at all */
777 if (!pass &&
1965f66e 778 (primary != bus->number || secondary <= bus->number ||
1820ffdc 779 secondary > subordinate || subordinate > bus->busn_res.end)) {
1965f66e
YL
780 dev_info(&dev->dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
781 secondary, subordinate);
a1c19894
BH
782 broken = 1;
783 }
784
1da177e4 785 /* Disable MasterAbortMode during probing to avoid reporting
f7625980 786 of bus errors (in some architectures) */
1da177e4
LT
787 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
788 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
789 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
790
99ddd552
BH
791 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
792 !is_cardbus && !broken) {
793 unsigned int cmax;
1da177e4
LT
794 /*
795 * Bus already configured by firmware, process it in the first
796 * pass and just note the configuration.
797 */
798 if (pass)
bbe8f9a3 799 goto out;
1da177e4
LT
800
801 /*
2ed85823
AN
802 * The bus might already exist for two reasons: Either we are
803 * rescanning the bus or the bus is reachable through more than
804 * one bridge. The second case can happen with the i450NX
805 * chipset.
1da177e4 806 */
99ddd552 807 child = pci_find_bus(pci_domain_nr(bus), secondary);
74710ded 808 if (!child) {
99ddd552 809 child = pci_add_new_bus(bus, dev, secondary);
74710ded
AC
810 if (!child)
811 goto out;
99ddd552 812 child->primary = primary;
bc76b731 813 pci_bus_insert_busn_res(child, secondary, subordinate);
74710ded 814 child->bridge_ctl = bctl;
1da177e4
LT
815 }
816
1da177e4 817 cmax = pci_scan_child_bus(child);
c95b0bd6
AN
818 if (cmax > subordinate)
819 dev_warn(&dev->dev, "bridge has subordinate %02x but max busn %02x\n",
820 subordinate, cmax);
821 /* subordinate should equal child->busn_res.end */
822 if (subordinate > max)
823 max = subordinate;
1da177e4
LT
824 } else {
825 /*
826 * We need to assign a number to this bus which we always
827 * do in the second pass.
828 */
12f44f46 829 if (!pass) {
619c8c31 830 if (pcibios_assign_all_busses() || broken || is_cardbus)
12f44f46
IK
831 /* Temporarily disable forwarding of the
832 configuration cycles on all bridges in
833 this bus segment to avoid possible
834 conflicts in the second pass between two
835 bridges programmed with overlapping
836 bus ranges. */
837 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
838 buses & ~0xffffff);
bbe8f9a3 839 goto out;
12f44f46 840 }
1da177e4 841
fc1b2531
AN
842 if (max >= bus->busn_res.end) {
843 dev_warn(&dev->dev, "can't allocate child bus %02x from %pR\n",
844 max, &bus->busn_res);
845 goto out;
846 }
847
1da177e4
LT
848 /* Clear errors */
849 pci_write_config_word(dev, PCI_STATUS, 0xffff);
850
fc1b2531 851 /* The bus will already exist if we are rescanning */
b1a98b69
TC
852 child = pci_find_bus(pci_domain_nr(bus), max+1);
853 if (!child) {
9a4d7d87 854 child = pci_add_new_bus(bus, dev, max+1);
b1a98b69
TC
855 if (!child)
856 goto out;
1820ffdc
AN
857 pci_bus_insert_busn_res(child, max+1,
858 bus->busn_res.end);
b1a98b69 859 }
9a4d7d87 860 max++;
1da177e4
LT
861 buses = (buses & 0xff000000)
862 | ((unsigned int)(child->primary) << 0)
b918c62e
YL
863 | ((unsigned int)(child->busn_res.start) << 8)
864 | ((unsigned int)(child->busn_res.end) << 16);
1da177e4
LT
865
866 /*
867 * yenta.c forces a secondary latency timer of 176.
868 * Copy that behaviour here.
869 */
870 if (is_cardbus) {
871 buses &= ~0xff000000;
872 buses |= CARDBUS_LATENCY_TIMER << 24;
873 }
7c867c88 874
1da177e4
LT
875 /*
876 * We need to blast all three values with a single write.
877 */
878 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
879
880 if (!is_cardbus) {
11949255 881 child->bridge_ctl = bctl;
1da177e4
LT
882 max = pci_scan_child_bus(child);
883 } else {
884 /*
885 * For CardBus bridges, we leave 4 bus numbers
886 * as cards with a PCI-to-PCI bridge can be
887 * inserted later.
888 */
3c78bc61 889 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
49887941 890 struct pci_bus *parent = bus;
cc57450f
RS
891 if (pci_find_bus(pci_domain_nr(bus),
892 max+i+1))
893 break;
49887941
DB
894 while (parent->parent) {
895 if ((!pcibios_assign_all_busses()) &&
b918c62e
YL
896 (parent->busn_res.end > max) &&
897 (parent->busn_res.end <= max+i)) {
49887941
DB
898 j = 1;
899 }
900 parent = parent->parent;
901 }
902 if (j) {
903 /*
904 * Often, there are two cardbus bridges
905 * -- try to leave one valid bus number
906 * for each one.
907 */
908 i /= 2;
909 break;
910 }
911 }
cc57450f 912 max += i;
1da177e4
LT
913 }
914 /*
915 * Set the subordinate bus number to its real value.
916 */
1820ffdc
AN
917 if (max > bus->busn_res.end) {
918 dev_warn(&dev->dev, "max busn %02x is outside %pR\n",
919 max, &bus->busn_res);
920 max = bus->busn_res.end;
921 }
bc76b731 922 pci_bus_update_busn_res_end(child, max);
1da177e4
LT
923 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
924 }
925
cb3576fa
GH
926 sprintf(child->name,
927 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
928 pci_domain_nr(bus), child->number);
1da177e4 929
d55bef51 930 /* Has only triggered on CardBus, fixup is in yenta_socket */
49887941 931 while (bus->parent) {
b918c62e
YL
932 if ((child->busn_res.end > bus->busn_res.end) ||
933 (child->number > bus->busn_res.end) ||
49887941 934 (child->number < bus->number) ||
b918c62e 935 (child->busn_res.end < bus->number)) {
227f0647 936 dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
b918c62e
YL
937 &child->busn_res,
938 (bus->number > child->busn_res.end &&
939 bus->busn_res.end < child->number) ?
a6f29a98
JP
940 "wholly" : "partially",
941 bus->self->transparent ? " transparent" : "",
865df576 942 dev_name(&bus->dev),
b918c62e 943 &bus->busn_res);
49887941
DB
944 }
945 bus = bus->parent;
946 }
947
bbe8f9a3
RB
948out:
949 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
950
1da177e4
LT
951 return max;
952}
b7fe9434 953EXPORT_SYMBOL(pci_scan_bridge);
1da177e4
LT
954
955/*
956 * Read interrupt line and base address registers.
957 * The architecture-dependent code can tweak these, of course.
958 */
959static void pci_read_irq(struct pci_dev *dev)
960{
961 unsigned char irq;
962
963 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
ffeff788 964 dev->pin = irq;
1da177e4
LT
965 if (irq)
966 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
967 dev->irq = irq;
968}
969
bb209c82 970void set_pcie_port_type(struct pci_dev *pdev)
480b93b7
YZ
971{
972 int pos;
973 u16 reg16;
974
975 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
976 if (!pos)
977 return;
0efea000 978 pdev->pcie_cap = pos;
480b93b7 979 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
786e2288 980 pdev->pcie_flags_reg = reg16;
b03e7495
JM
981 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
982 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
480b93b7
YZ
983}
984
bb209c82 985void set_pcie_hotplug_bridge(struct pci_dev *pdev)
28760489 986{
28760489
EB
987 u32 reg32;
988
59875ae4 989 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
28760489
EB
990 if (reg32 & PCI_EXP_SLTCAP_HPC)
991 pdev->is_hotplug_bridge = 1;
992}
993
78916b00
AW
994/**
995 * pci_ext_cfg_is_aliased - is ext config space just an alias of std config?
996 * @dev: PCI device
997 *
998 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
999 * when forwarding a type1 configuration request the bridge must check that
1000 * the extended register address field is zero. The bridge is not permitted
1001 * to forward the transactions and must handle it as an Unsupported Request.
1002 * Some bridges do not follow this rule and simply drop the extended register
1003 * bits, resulting in the standard config space being aliased, every 256
1004 * bytes across the entire configuration space. Test for this condition by
1005 * comparing the first dword of each potential alias to the vendor/device ID.
1006 * Known offenders:
1007 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1008 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1009 */
1010static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1011{
1012#ifdef CONFIG_PCI_QUIRKS
1013 int pos;
1014 u32 header, tmp;
1015
1016 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1017
1018 for (pos = PCI_CFG_SPACE_SIZE;
1019 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1020 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1021 || header != tmp)
1022 return false;
1023 }
1024
1025 return true;
1026#else
1027 return false;
1028#endif
1029}
1030
0b950f0f
SH
1031/**
1032 * pci_cfg_space_size - get the configuration space size of the PCI device.
1033 * @dev: PCI device
1034 *
1035 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1036 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1037 * access it. Maybe we don't have a way to generate extended config space
1038 * accesses, or the device is behind a reverse Express bridge. So we try
1039 * reading the dword at 0x100 which must either be 0 or a valid extended
1040 * capability header.
1041 */
1042static int pci_cfg_space_size_ext(struct pci_dev *dev)
1043{
1044 u32 status;
1045 int pos = PCI_CFG_SPACE_SIZE;
1046
1047 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1048 goto fail;
78916b00 1049 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
0b950f0f
SH
1050 goto fail;
1051
1052 return PCI_CFG_SPACE_EXP_SIZE;
1053
1054 fail:
1055 return PCI_CFG_SPACE_SIZE;
1056}
1057
1058int pci_cfg_space_size(struct pci_dev *dev)
1059{
1060 int pos;
1061 u32 status;
1062 u16 class;
1063
1064 class = dev->class >> 8;
1065 if (class == PCI_CLASS_BRIDGE_HOST)
1066 return pci_cfg_space_size_ext(dev);
1067
1068 if (!pci_is_pcie(dev)) {
1069 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1070 if (!pos)
1071 goto fail;
1072
1073 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1074 if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
1075 goto fail;
1076 }
1077
1078 return pci_cfg_space_size_ext(dev);
1079
1080 fail:
1081 return PCI_CFG_SPACE_SIZE;
1082}
1083
01abc2aa 1084#define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
76e6a1d6 1085
1da177e4
LT
1086/**
1087 * pci_setup_device - fill in class and map information of a device
1088 * @dev: the device structure to fill
1089 *
f7625980 1090 * Initialize the device structure with information about the device's
1da177e4
LT
1091 * vendor,class,memory and IO-space addresses,IRQ lines etc.
1092 * Called at initialisation of the PCI subsystem and by CardBus services.
480b93b7
YZ
1093 * Returns 0 on success and negative if unknown type of device (not normal,
1094 * bridge or CardBus).
1da177e4 1095 */
480b93b7 1096int pci_setup_device(struct pci_dev *dev)
1da177e4
LT
1097{
1098 u32 class;
480b93b7
YZ
1099 u8 hdr_type;
1100 struct pci_slot *slot;
bc577d2b 1101 int pos = 0;
5bfa14ed
BH
1102 struct pci_bus_region region;
1103 struct resource *res;
480b93b7
YZ
1104
1105 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
1106 return -EIO;
1107
1108 dev->sysdata = dev->bus->sysdata;
1109 dev->dev.parent = dev->bus->bridge;
1110 dev->dev.bus = &pci_bus_type;
1111 dev->hdr_type = hdr_type & 0x7f;
1112 dev->multifunction = !!(hdr_type & 0x80);
480b93b7
YZ
1113 dev->error_state = pci_channel_io_normal;
1114 set_pcie_port_type(dev);
1115
1116 list_for_each_entry(slot, &dev->bus->slots, list)
1117 if (PCI_SLOT(dev->devfn) == slot->number)
1118 dev->slot = slot;
1119
1120 /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1121 set this higher, assuming the system even supports it. */
1122 dev->dma_mask = 0xffffffff;
1da177e4 1123
eebfcfb5
GKH
1124 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1125 dev->bus->number, PCI_SLOT(dev->devfn),
1126 PCI_FUNC(dev->devfn));
1da177e4
LT
1127
1128 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
b8a3a521 1129 dev->revision = class & 0xff;
2dd8ba92 1130 dev->class = class >> 8; /* upper 3 bytes */
1da177e4 1131
2dd8ba92
YL
1132 dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n",
1133 dev->vendor, dev->device, dev->hdr_type, dev->class);
1da177e4 1134
853346e4
YZ
1135 /* need to have dev->class ready */
1136 dev->cfg_size = pci_cfg_space_size(dev);
1137
1da177e4 1138 /* "Unknown power state" */
3fe9d19f 1139 dev->current_state = PCI_UNKNOWN;
1da177e4
LT
1140
1141 /* Early fixups, before probing the BARs */
1142 pci_fixup_device(pci_fixup_early, dev);
f79b1b14
YZ
1143 /* device class may be changed after fixup */
1144 class = dev->class >> 8;
1da177e4
LT
1145
1146 switch (dev->hdr_type) { /* header type */
1147 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1148 if (class == PCI_CLASS_BRIDGE_PCI)
1149 goto bad;
1150 pci_read_irq(dev);
1151 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
1152 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1153 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
368c73d4
AC
1154
1155 /*
075eb9e3
BH
1156 * Do the ugly legacy mode stuff here rather than broken chip
1157 * quirk code. Legacy mode ATA controllers have fixed
1158 * addresses. These are not always echoed in BAR0-3, and
1159 * BAR0-3 in a few cases contain junk!
368c73d4
AC
1160 */
1161 if (class == PCI_CLASS_STORAGE_IDE) {
1162 u8 progif;
1163 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1164 if ((progif & 1) == 0) {
5bfa14ed
BH
1165 region.start = 0x1F0;
1166 region.end = 0x1F7;
1167 res = &dev->resource[0];
1168 res->flags = LEGACY_IO_RESOURCE;
fc279850 1169 pcibios_bus_to_resource(dev->bus, res, &region);
075eb9e3
BH
1170 dev_info(&dev->dev, "legacy IDE quirk: reg 0x10: %pR\n",
1171 res);
5bfa14ed
BH
1172 region.start = 0x3F6;
1173 region.end = 0x3F6;
1174 res = &dev->resource[1];
1175 res->flags = LEGACY_IO_RESOURCE;
fc279850 1176 pcibios_bus_to_resource(dev->bus, res, &region);
075eb9e3
BH
1177 dev_info(&dev->dev, "legacy IDE quirk: reg 0x14: %pR\n",
1178 res);
368c73d4
AC
1179 }
1180 if ((progif & 4) == 0) {
5bfa14ed
BH
1181 region.start = 0x170;
1182 region.end = 0x177;
1183 res = &dev->resource[2];
1184 res->flags = LEGACY_IO_RESOURCE;
fc279850 1185 pcibios_bus_to_resource(dev->bus, res, &region);
075eb9e3
BH
1186 dev_info(&dev->dev, "legacy IDE quirk: reg 0x18: %pR\n",
1187 res);
5bfa14ed
BH
1188 region.start = 0x376;
1189 region.end = 0x376;
1190 res = &dev->resource[3];
1191 res->flags = LEGACY_IO_RESOURCE;
fc279850 1192 pcibios_bus_to_resource(dev->bus, res, &region);
075eb9e3
BH
1193 dev_info(&dev->dev, "legacy IDE quirk: reg 0x1c: %pR\n",
1194 res);
368c73d4
AC
1195 }
1196 }
1da177e4
LT
1197 break;
1198
1199 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1200 if (class != PCI_CLASS_BRIDGE_PCI)
1201 goto bad;
1202 /* The PCI-to-PCI bridge spec requires that subtractive
1203 decoding (i.e. transparent) bridge must have programming
f7625980 1204 interface code of 0x01. */
3efd273b 1205 pci_read_irq(dev);
1da177e4
LT
1206 dev->transparent = ((dev->class & 0xff) == 1);
1207 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
28760489 1208 set_pcie_hotplug_bridge(dev);
bc577d2b
GB
1209 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1210 if (pos) {
1211 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1212 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1213 }
1da177e4
LT
1214 break;
1215
1216 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1217 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1218 goto bad;
1219 pci_read_irq(dev);
1220 pci_read_bases(dev, 1, 0);
1221 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1222 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1223 break;
1224
1225 default: /* unknown header */
227f0647
RD
1226 dev_err(&dev->dev, "unknown header type %02x, ignoring device\n",
1227 dev->hdr_type);
480b93b7 1228 return -EIO;
1da177e4
LT
1229
1230 bad:
227f0647
RD
1231 dev_err(&dev->dev, "ignoring class %#08x (doesn't match header type %02x)\n",
1232 dev->class, dev->hdr_type);
1da177e4
LT
1233 dev->class = PCI_CLASS_NOT_DEFINED;
1234 }
1235
1236 /* We found a fine healthy device, go go go... */
1237 return 0;
1238}
1239
589fcc23
BH
1240static struct hpp_type0 pci_default_type0 = {
1241 .revision = 1,
1242 .cache_line_size = 8,
1243 .latency_timer = 0x40,
1244 .enable_serr = 0,
1245 .enable_perr = 0,
1246};
1247
1248static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1249{
1250 u16 pci_cmd, pci_bctl;
1251
1252 if (!hpp) {
1253 /*
1254 * Perhaps we *should* use default settings for PCIe, but
1255 * pciehp didn't, so we won't either.
1256 */
1257 if (pci_is_pcie(dev))
1258 return;
1259 hpp = &pci_default_type0;
1260 }
1261
1262 if (hpp->revision > 1) {
1263 dev_warn(&dev->dev,
1264 "PCI settings rev %d not supported; using defaults\n",
1265 hpp->revision);
1266 hpp = &pci_default_type0;
1267 }
1268
1269 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1270 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1271 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1272 if (hpp->enable_serr)
1273 pci_cmd |= PCI_COMMAND_SERR;
1274 else
1275 pci_cmd &= ~PCI_COMMAND_SERR;
1276 if (hpp->enable_perr)
1277 pci_cmd |= PCI_COMMAND_PARITY;
1278 else
1279 pci_cmd &= ~PCI_COMMAND_PARITY;
1280 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1281
1282 /* Program bridge control value */
1283 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1284 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1285 hpp->latency_timer);
1286 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1287 if (hpp->enable_serr)
1288 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1289 else
1290 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1291 if (hpp->enable_perr)
1292 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1293 else
1294 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1295 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1296 }
1297}
1298
1299static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1300{
1301 if (hpp)
1302 dev_warn(&dev->dev, "PCI-X settings not supported\n");
1303}
1304
1305static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1306{
1307 int pos;
1308 u32 reg32;
1309
1310 if (!hpp)
1311 return;
1312
1313 if (hpp->revision > 1) {
1314 dev_warn(&dev->dev, "PCIe settings rev %d not supported\n",
1315 hpp->revision);
1316 return;
1317 }
1318
1319 /* Initialize Device Control Register */
1320 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1321 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1322
1323 /* Initialize Link Control Register */
1324 if (dev->subordinate)
1325 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1326 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
1327
1328 /* Find Advanced Error Reporting Enhanced Capability */
1329 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1330 if (!pos)
1331 return;
1332
1333 /* Initialize Uncorrectable Error Mask Register */
1334 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1335 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1336 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1337
1338 /* Initialize Uncorrectable Error Severity Register */
1339 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1340 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1341 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1342
1343 /* Initialize Correctable Error Mask Register */
1344 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1345 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1346 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1347
1348 /* Initialize Advanced Error Capabilities and Control Register */
1349 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1350 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
1351 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1352
1353 /*
1354 * FIXME: The following two registers are not supported yet.
1355 *
1356 * o Secondary Uncorrectable Error Severity Register
1357 * o Secondary Uncorrectable Error Mask Register
1358 */
1359}
1360
1361void pci_configure_slot(struct pci_dev *dev)
1362{
1363 struct pci_dev *cdev;
1364 struct hotplug_params hpp;
1365 int ret;
1366
1367 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1368 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1369 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1370 return;
1371
1372 pcie_bus_configure_settings(dev->bus);
1373
1374 memset(&hpp, 0, sizeof(hpp));
1375 ret = pci_get_hp_params(dev, &hpp);
1376
1377 program_hpp_type2(dev, hpp.t2);
1378 program_hpp_type1(dev, hpp.t1);
1379 program_hpp_type0(dev, hpp.t0);
1380
1381 if (dev->subordinate) {
1382 list_for_each_entry(cdev, &dev->subordinate->devices,
1383 bus_list)
1384 pci_configure_slot(cdev);
1385 }
1386}
1387EXPORT_SYMBOL_GPL(pci_configure_slot);
1388
201de56e
ZY
1389static void pci_release_capabilities(struct pci_dev *dev)
1390{
1391 pci_vpd_release(dev);
d1b054da 1392 pci_iov_release(dev);
f796841e 1393 pci_free_cap_save_buffers(dev);
201de56e
ZY
1394}
1395
1da177e4
LT
1396/**
1397 * pci_release_dev - free a pci device structure when all users of it are finished.
1398 * @dev: device that's been disconnected
1399 *
1400 * Will be called only by the device core when all users of this pci device are
1401 * done.
1402 */
1403static void pci_release_dev(struct device *dev)
1404{
04480094 1405 struct pci_dev *pci_dev;
1da177e4 1406
04480094 1407 pci_dev = to_pci_dev(dev);
201de56e 1408 pci_release_capabilities(pci_dev);
98d9f30c 1409 pci_release_of_node(pci_dev);
6ae32c53 1410 pcibios_release_device(pci_dev);
8b1fce04 1411 pci_bus_put(pci_dev->bus);
782a985d 1412 kfree(pci_dev->driver_override);
1da177e4
LT
1413 kfree(pci_dev);
1414}
1415
3c6e6ae7 1416struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
65891215
ME
1417{
1418 struct pci_dev *dev;
1419
1420 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
1421 if (!dev)
1422 return NULL;
1423
65891215 1424 INIT_LIST_HEAD(&dev->bus_list);
88e7b167 1425 dev->dev.type = &pci_dev_type;
3c6e6ae7 1426 dev->bus = pci_bus_get(bus);
65891215
ME
1427
1428 return dev;
1429}
3c6e6ae7
GZ
1430EXPORT_SYMBOL(pci_alloc_dev);
1431
efdc87da 1432bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
3c78bc61 1433 int crs_timeout)
1da177e4 1434{
1da177e4
LT
1435 int delay = 1;
1436
efdc87da
YL
1437 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1438 return false;
1da177e4
LT
1439
1440 /* some broken boards return 0 or ~0 if a slot is empty: */
efdc87da
YL
1441 if (*l == 0xffffffff || *l == 0x00000000 ||
1442 *l == 0x0000ffff || *l == 0xffff0000)
1443 return false;
1da177e4
LT
1444
1445 /* Configuration request Retry Status */
efdc87da
YL
1446 while (*l == 0xffff0001) {
1447 if (!crs_timeout)
1448 return false;
1449
1da177e4
LT
1450 msleep(delay);
1451 delay *= 2;
efdc87da
YL
1452 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1453 return false;
1da177e4 1454 /* Card hasn't responded in 60 seconds? Must be stuck. */
efdc87da 1455 if (delay > crs_timeout) {
227f0647
RD
1456 printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not responding\n",
1457 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
1458 PCI_FUNC(devfn));
efdc87da 1459 return false;
1da177e4
LT
1460 }
1461 }
1462
efdc87da
YL
1463 return true;
1464}
1465EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
1466
1467/*
1468 * Read the config data for a PCI device, sanity-check it
1469 * and fill in the dev structure...
1470 */
1471static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
1472{
1473 struct pci_dev *dev;
1474 u32 l;
1475
1476 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
1477 return NULL;
1478
8b1fce04 1479 dev = pci_alloc_dev(bus);
1da177e4
LT
1480 if (!dev)
1481 return NULL;
1482
1da177e4 1483 dev->devfn = devfn;
1da177e4
LT
1484 dev->vendor = l & 0xffff;
1485 dev->device = (l >> 16) & 0xffff;
cef354db 1486
98d9f30c
BH
1487 pci_set_of_node(dev);
1488
480b93b7 1489 if (pci_setup_device(dev)) {
8b1fce04 1490 pci_bus_put(dev->bus);
1da177e4
LT
1491 kfree(dev);
1492 return NULL;
1493 }
1da177e4
LT
1494
1495 return dev;
1496}
1497
201de56e
ZY
1498static void pci_init_capabilities(struct pci_dev *dev)
1499{
1500 /* MSI/MSI-X list */
1501 pci_msi_init_pci_dev(dev);
1502
63f4898a
RW
1503 /* Buffers for saving PCIe and PCI-X capabilities */
1504 pci_allocate_cap_save_buffers(dev);
1505
201de56e
ZY
1506 /* Power Management */
1507 pci_pm_init(dev);
1508
1509 /* Vital Product Data */
1510 pci_vpd_pci22_init(dev);
58c3a727
YZ
1511
1512 /* Alternative Routing-ID Forwarding */
31ab2476 1513 pci_configure_ari(dev);
d1b054da
YZ
1514
1515 /* Single Root I/O Virtualization */
1516 pci_iov_init(dev);
ae21ee65
AK
1517
1518 /* Enable ACS P2P upstream forwarding */
5d990b62 1519 pci_enable_acs(dev);
201de56e
ZY
1520}
1521
96bde06a 1522void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
1da177e4 1523{
4f535093
YL
1524 int ret;
1525
cdb9b9f7
PM
1526 device_initialize(&dev->dev);
1527 dev->dev.release = pci_release_dev;
1da177e4 1528
7629d19a 1529 set_dev_node(&dev->dev, pcibus_to_node(bus));
cdb9b9f7 1530 dev->dev.dma_mask = &dev->dma_mask;
4d57cdfa 1531 dev->dev.dma_parms = &dev->dma_parms;
cdb9b9f7 1532 dev->dev.coherent_dma_mask = 0xffffffffull;
1da177e4 1533
4d57cdfa 1534 pci_set_dma_max_seg_size(dev, 65536);
59fc67de 1535 pci_set_dma_seg_boundary(dev, 0xffffffff);
4d57cdfa 1536
1da177e4
LT
1537 /* Fix up broken headers */
1538 pci_fixup_device(pci_fixup_header, dev);
1539
2069ecfb
YL
1540 /* moved out from quirk header fixup code */
1541 pci_reassigndev_resource_alignment(dev);
1542
4b77b0a2
RW
1543 /* Clear the state_saved flag. */
1544 dev->state_saved = false;
1545
201de56e
ZY
1546 /* Initialize various capabilities */
1547 pci_init_capabilities(dev);
eb9d0fe4 1548
1da177e4
LT
1549 /*
1550 * Add the device to our list of discovered devices
1551 * and the bus list for fixup functions, etc.
1552 */
d71374da 1553 down_write(&pci_bus_sem);
1da177e4 1554 list_add_tail(&dev->bus_list, &bus->devices);
d71374da 1555 up_write(&pci_bus_sem);
4f535093 1556
4f535093
YL
1557 ret = pcibios_add_device(dev);
1558 WARN_ON(ret < 0);
1559
1560 /* Notifier could use PCI capabilities */
1561 dev->match_driver = false;
1562 ret = device_add(&dev->dev);
1563 WARN_ON(ret < 0);
cdb9b9f7
PM
1564}
1565
10874f5a 1566struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
cdb9b9f7
PM
1567{
1568 struct pci_dev *dev;
1569
90bdb311
TP
1570 dev = pci_get_slot(bus, devfn);
1571 if (dev) {
1572 pci_dev_put(dev);
1573 return dev;
1574 }
1575
cdb9b9f7
PM
1576 dev = pci_scan_device(bus, devfn);
1577 if (!dev)
1578 return NULL;
1579
1580 pci_device_add(dev, bus);
1da177e4
LT
1581
1582 return dev;
1583}
b73e9687 1584EXPORT_SYMBOL(pci_scan_single_device);
1da177e4 1585
b1bd58e4 1586static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
f07852d6 1587{
b1bd58e4
YW
1588 int pos;
1589 u16 cap = 0;
1590 unsigned next_fn;
4fb88c1a 1591
b1bd58e4
YW
1592 if (pci_ari_enabled(bus)) {
1593 if (!dev)
1594 return 0;
1595 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
1596 if (!pos)
1597 return 0;
4fb88c1a 1598
b1bd58e4
YW
1599 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
1600 next_fn = PCI_ARI_CAP_NFN(cap);
1601 if (next_fn <= fn)
1602 return 0; /* protect against malformed list */
f07852d6 1603
b1bd58e4
YW
1604 return next_fn;
1605 }
1606
1607 /* dev may be NULL for non-contiguous multifunction devices */
1608 if (!dev || dev->multifunction)
1609 return (fn + 1) % 8;
f07852d6 1610
f07852d6
MW
1611 return 0;
1612}
1613
1614static int only_one_child(struct pci_bus *bus)
1615{
1616 struct pci_dev *parent = bus->self;
284f5f9d 1617
f07852d6
MW
1618 if (!parent || !pci_is_pcie(parent))
1619 return 0;
62f87c0e 1620 if (pci_pcie_type(parent) == PCI_EXP_TYPE_ROOT_PORT)
284f5f9d 1621 return 1;
62f87c0e 1622 if (pci_pcie_type(parent) == PCI_EXP_TYPE_DOWNSTREAM &&
284f5f9d 1623 !pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
f07852d6
MW
1624 return 1;
1625 return 0;
1626}
1627
1da177e4
LT
1628/**
1629 * pci_scan_slot - scan a PCI slot on a bus for devices.
1630 * @bus: PCI bus to scan
1631 * @devfn: slot number to scan (must have zero function.)
1632 *
1633 * Scan a PCI slot on the specified PCI bus for devices, adding
1634 * discovered devices to the @bus->devices list. New devices
8a1bc901 1635 * will not have is_added set.
1b69dfc6
TP
1636 *
1637 * Returns the number of new devices found.
1da177e4 1638 */
96bde06a 1639int pci_scan_slot(struct pci_bus *bus, int devfn)
1da177e4 1640{
f07852d6 1641 unsigned fn, nr = 0;
1b69dfc6 1642 struct pci_dev *dev;
f07852d6
MW
1643
1644 if (only_one_child(bus) && (devfn > 0))
1645 return 0; /* Already scanned the entire slot */
1da177e4 1646
1b69dfc6 1647 dev = pci_scan_single_device(bus, devfn);
4fb88c1a
MW
1648 if (!dev)
1649 return 0;
1650 if (!dev->is_added)
1b69dfc6
TP
1651 nr++;
1652
b1bd58e4 1653 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
f07852d6
MW
1654 dev = pci_scan_single_device(bus, devfn + fn);
1655 if (dev) {
1656 if (!dev->is_added)
1657 nr++;
1658 dev->multifunction = 1;
1da177e4
LT
1659 }
1660 }
7d715a6c 1661
149e1637
SL
1662 /* only one slot has pcie device */
1663 if (bus->self && nr)
7d715a6c
SL
1664 pcie_aspm_init_link_state(bus->self);
1665
1da177e4
LT
1666 return nr;
1667}
b7fe9434 1668EXPORT_SYMBOL(pci_scan_slot);
1da177e4 1669
b03e7495
JM
1670static int pcie_find_smpss(struct pci_dev *dev, void *data)
1671{
1672 u8 *smpss = data;
1673
1674 if (!pci_is_pcie(dev))
1675 return 0;
1676
d4aa68f6
YW
1677 /*
1678 * We don't have a way to change MPS settings on devices that have
1679 * drivers attached. A hot-added device might support only the minimum
1680 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
1681 * where devices may be hot-added, we limit the fabric MPS to 128 so
1682 * hot-added devices will work correctly.
1683 *
1684 * However, if we hot-add a device to a slot directly below a Root
1685 * Port, it's impossible for there to be other existing devices below
1686 * the port. We don't limit the MPS in this case because we can
1687 * reconfigure MPS on both the Root Port and the hot-added device,
1688 * and there are no other devices involved.
1689 *
1690 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
b03e7495 1691 */
d4aa68f6
YW
1692 if (dev->is_hotplug_bridge &&
1693 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
b03e7495
JM
1694 *smpss = 0;
1695
1696 if (*smpss > dev->pcie_mpss)
1697 *smpss = dev->pcie_mpss;
1698
1699 return 0;
1700}
1701
1702static void pcie_write_mps(struct pci_dev *dev, int mps)
1703{
62f392ea 1704 int rc;
b03e7495
JM
1705
1706 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
62f392ea 1707 mps = 128 << dev->pcie_mpss;
b03e7495 1708
62f87c0e
YW
1709 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
1710 dev->bus->self)
62f392ea 1711 /* For "Performance", the assumption is made that
b03e7495
JM
1712 * downstream communication will never be larger than
1713 * the MRRS. So, the MPS only needs to be configured
1714 * for the upstream communication. This being the case,
1715 * walk from the top down and set the MPS of the child
1716 * to that of the parent bus.
62f392ea
JM
1717 *
1718 * Configure the device MPS with the smaller of the
1719 * device MPSS or the bridge MPS (which is assumed to be
1720 * properly configured at this point to the largest
1721 * allowable MPS based on its parent bus).
b03e7495 1722 */
62f392ea 1723 mps = min(mps, pcie_get_mps(dev->bus->self));
b03e7495
JM
1724 }
1725
1726 rc = pcie_set_mps(dev, mps);
1727 if (rc)
1728 dev_err(&dev->dev, "Failed attempting to set the MPS\n");
1729}
1730
62f392ea 1731static void pcie_write_mrrs(struct pci_dev *dev)
b03e7495 1732{
62f392ea 1733 int rc, mrrs;
b03e7495 1734
ed2888e9
JM
1735 /* In the "safe" case, do not configure the MRRS. There appear to be
1736 * issues with setting MRRS to 0 on a number of devices.
1737 */
ed2888e9
JM
1738 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
1739 return;
1740
ed2888e9
JM
1741 /* For Max performance, the MRRS must be set to the largest supported
1742 * value. However, it cannot be configured larger than the MPS the
62f392ea
JM
1743 * device or the bus can support. This should already be properly
1744 * configured by a prior call to pcie_write_mps.
ed2888e9 1745 */
62f392ea 1746 mrrs = pcie_get_mps(dev);
b03e7495
JM
1747
1748 /* MRRS is a R/W register. Invalid values can be written, but a
ed2888e9 1749 * subsequent read will verify if the value is acceptable or not.
b03e7495
JM
1750 * If the MRRS value provided is not acceptable (e.g., too large),
1751 * shrink the value until it is acceptable to the HW.
f7625980 1752 */
b03e7495
JM
1753 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
1754 rc = pcie_set_readrq(dev, mrrs);
62f392ea
JM
1755 if (!rc)
1756 break;
b03e7495 1757
62f392ea 1758 dev_warn(&dev->dev, "Failed attempting to set the MRRS\n");
b03e7495
JM
1759 mrrs /= 2;
1760 }
62f392ea
JM
1761
1762 if (mrrs < 128)
227f0647 1763 dev_err(&dev->dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n");
b03e7495
JM
1764}
1765
5895af79
YW
1766static void pcie_bus_detect_mps(struct pci_dev *dev)
1767{
1768 struct pci_dev *bridge = dev->bus->self;
1769 int mps, p_mps;
1770
1771 if (!bridge)
1772 return;
1773
1774 mps = pcie_get_mps(dev);
1775 p_mps = pcie_get_mps(bridge);
1776
1777 if (mps != p_mps)
1778 dev_warn(&dev->dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1779 mps, pci_name(bridge), p_mps);
1780}
1781
b03e7495
JM
1782static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
1783{
a513a99a 1784 int mps, orig_mps;
b03e7495
JM
1785
1786 if (!pci_is_pcie(dev))
1787 return 0;
1788
5895af79
YW
1789 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
1790 pcie_bus_detect_mps(dev);
1791 return 0;
1792 }
1793
a513a99a
JM
1794 mps = 128 << *(u8 *)data;
1795 orig_mps = pcie_get_mps(dev);
b03e7495
JM
1796
1797 pcie_write_mps(dev, mps);
62f392ea 1798 pcie_write_mrrs(dev);
b03e7495 1799
227f0647
RD
1800 dev_info(&dev->dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
1801 pcie_get_mps(dev), 128 << dev->pcie_mpss,
a513a99a 1802 orig_mps, pcie_get_readrq(dev));
b03e7495
JM
1803
1804 return 0;
1805}
1806
a513a99a 1807/* pcie_bus_configure_settings requires that pci_walk_bus work in a top-down,
b03e7495
JM
1808 * parents then children fashion. If this changes, then this code will not
1809 * work as designed.
1810 */
a58674ff 1811void pcie_bus_configure_settings(struct pci_bus *bus)
b03e7495 1812{
1e358f94 1813 u8 smpss = 0;
b03e7495 1814
a58674ff 1815 if (!bus->self)
b03e7495
JM
1816 return;
1817
b03e7495 1818 if (!pci_is_pcie(bus->self))
5f39e670
JM
1819 return;
1820
1821 /* FIXME - Peer to peer DMA is possible, though the endpoint would need
3315472c 1822 * to be aware of the MPS of the destination. To work around this,
5f39e670
JM
1823 * simply force the MPS of the entire system to the smallest possible.
1824 */
1825 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
1826 smpss = 0;
1827
b03e7495 1828 if (pcie_bus_config == PCIE_BUS_SAFE) {
a58674ff 1829 smpss = bus->self->pcie_mpss;
5f39e670 1830
b03e7495
JM
1831 pcie_find_smpss(bus->self, &smpss);
1832 pci_walk_bus(bus, pcie_find_smpss, &smpss);
1833 }
1834
1835 pcie_bus_configure_set(bus->self, &smpss);
1836 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
1837}
debc3b77 1838EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
b03e7495 1839
15856ad5 1840unsigned int pci_scan_child_bus(struct pci_bus *bus)
1da177e4 1841{
b918c62e 1842 unsigned int devfn, pass, max = bus->busn_res.start;
1da177e4
LT
1843 struct pci_dev *dev;
1844
0207c356 1845 dev_dbg(&bus->dev, "scanning bus\n");
1da177e4
LT
1846
1847 /* Go find them, Rover! */
1848 for (devfn = 0; devfn < 0x100; devfn += 8)
1849 pci_scan_slot(bus, devfn);
1850
a28724b0
YZ
1851 /* Reserve buses for SR-IOV capability. */
1852 max += pci_iov_bus_range(bus);
1853
1da177e4
LT
1854 /*
1855 * After performing arch-dependent fixup of the bus, look behind
1856 * all PCI-to-PCI bridges on this bus.
1857 */
74710ded 1858 if (!bus->is_added) {
0207c356 1859 dev_dbg(&bus->dev, "fixups for bus\n");
74710ded 1860 pcibios_fixup_bus(bus);
981cf9ea 1861 bus->is_added = 1;
74710ded
AC
1862 }
1863
3c78bc61 1864 for (pass = 0; pass < 2; pass++)
1da177e4 1865 list_for_each_entry(dev, &bus->devices, bus_list) {
6788a51f 1866 if (pci_is_bridge(dev))
1da177e4
LT
1867 max = pci_scan_bridge(bus, dev, max, pass);
1868 }
1869
1870 /*
1871 * We've scanned the bus and so we know all about what's on
1872 * the other side of any bridges that may be on this bus plus
1873 * any devices.
1874 *
1875 * Return how far we've got finding sub-buses.
1876 */
0207c356 1877 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
1da177e4
LT
1878 return max;
1879}
b7fe9434 1880EXPORT_SYMBOL_GPL(pci_scan_child_bus);
1da177e4 1881
6c0cc950
RW
1882/**
1883 * pcibios_root_bridge_prepare - Platform-specific host bridge setup.
1884 * @bridge: Host bridge to set up.
1885 *
1886 * Default empty implementation. Replace with an architecture-specific setup
1887 * routine, if necessary.
1888 */
1889int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
1890{
1891 return 0;
1892}
1893
10a95747
JL
1894void __weak pcibios_add_bus(struct pci_bus *bus)
1895{
1896}
1897
1898void __weak pcibios_remove_bus(struct pci_bus *bus)
1899{
1900}
1901
166c6370
BH
1902struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
1903 struct pci_ops *ops, void *sysdata, struct list_head *resources)
1da177e4 1904{
0efd5aab 1905 int error;
5a21d70d 1906 struct pci_host_bridge *bridge;
0207c356 1907 struct pci_bus *b, *b2;
0efd5aab 1908 struct pci_host_bridge_window *window, *n;
a9d9f527 1909 struct resource *res;
0efd5aab
BH
1910 resource_size_t offset;
1911 char bus_addr[64];
1912 char *fmt;
1da177e4
LT
1913
1914 b = pci_alloc_bus();
1915 if (!b)
7b543663 1916 return NULL;
1da177e4
LT
1917
1918 b->sysdata = sysdata;
1919 b->ops = ops;
4f535093 1920 b->number = b->busn_res.start = bus;
0207c356
BH
1921 b2 = pci_find_bus(pci_domain_nr(b), bus);
1922 if (b2) {
1da177e4 1923 /* If we already got to this bus through a different bridge, ignore it */
0207c356 1924 dev_dbg(&b2->dev, "bus already known\n");
1da177e4
LT
1925 goto err_out;
1926 }
d71374da 1927
7b543663
YL
1928 bridge = pci_alloc_host_bridge(b);
1929 if (!bridge)
1930 goto err_out;
1931
1932 bridge->dev.parent = parent;
70efde2a 1933 bridge->dev.release = pci_release_host_bridge_dev;
7b543663 1934 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(b), bus);
6c0cc950 1935 error = pcibios_root_bridge_prepare(bridge);
343df771
JL
1936 if (error) {
1937 kfree(bridge);
1938 goto err_out;
1939 }
6c0cc950 1940
7b543663 1941 error = device_register(&bridge->dev);
343df771
JL
1942 if (error) {
1943 put_device(&bridge->dev);
1944 goto err_out;
1945 }
7b543663 1946 b->bridge = get_device(&bridge->dev);
a1e4d72c 1947 device_enable_async_suspend(b->bridge);
98d9f30c 1948 pci_set_bus_of_node(b);
1da177e4 1949
0d358f22
YL
1950 if (!parent)
1951 set_dev_node(b->bridge, pcibus_to_node(b));
1952
fd7d1ced
GKH
1953 b->dev.class = &pcibus_class;
1954 b->dev.parent = b->bridge;
1a927133 1955 dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus);
fd7d1ced 1956 error = device_register(&b->dev);
1da177e4
LT
1957 if (error)
1958 goto class_dev_reg_err;
1da177e4 1959
10a95747
JL
1960 pcibios_add_bus(b);
1961
1da177e4
LT
1962 /* Create legacy_io and legacy_mem files for this bus */
1963 pci_create_legacy_files(b);
1964
a9d9f527
BH
1965 if (parent)
1966 dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev));
1967 else
1968 printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev));
1969
0efd5aab
BH
1970 /* Add initial resources to the bus */
1971 list_for_each_entry_safe(window, n, resources, list) {
1972 list_move_tail(&window->list, &bridge->windows);
1973 res = window->res;
1974 offset = window->offset;
f848ffb1
YL
1975 if (res->flags & IORESOURCE_BUS)
1976 pci_bus_insert_busn_res(b, bus, res->end);
1977 else
1978 pci_bus_add_resource(b, res, 0);
0efd5aab
BH
1979 if (offset) {
1980 if (resource_type(res) == IORESOURCE_IO)
1981 fmt = " (bus address [%#06llx-%#06llx])";
1982 else
1983 fmt = " (bus address [%#010llx-%#010llx])";
1984 snprintf(bus_addr, sizeof(bus_addr), fmt,
1985 (unsigned long long) (res->start - offset),
1986 (unsigned long long) (res->end - offset));
1987 } else
1988 bus_addr[0] = '\0';
1989 dev_info(&b->dev, "root bus resource %pR%s\n", res, bus_addr);
a9d9f527
BH
1990 }
1991
a5390aa6
BH
1992 down_write(&pci_bus_sem);
1993 list_add_tail(&b->node, &pci_root_buses);
1994 up_write(&pci_bus_sem);
1995
1da177e4
LT
1996 return b;
1997
1da177e4 1998class_dev_reg_err:
7b543663
YL
1999 put_device(&bridge->dev);
2000 device_unregister(&bridge->dev);
1da177e4 2001err_out:
1da177e4
LT
2002 kfree(b);
2003 return NULL;
2004}
cdb9b9f7 2005
98a35831
YL
2006int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2007{
2008 struct resource *res = &b->busn_res;
2009 struct resource *parent_res, *conflict;
2010
2011 res->start = bus;
2012 res->end = bus_max;
2013 res->flags = IORESOURCE_BUS;
2014
2015 if (!pci_is_root_bus(b))
2016 parent_res = &b->parent->busn_res;
2017 else {
2018 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
2019 res->flags |= IORESOURCE_PCI_FIXED;
2020 }
2021
ced04d15 2022 conflict = request_resource_conflict(parent_res, res);
98a35831
YL
2023
2024 if (conflict)
2025 dev_printk(KERN_DEBUG, &b->dev,
2026 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
2027 res, pci_is_root_bus(b) ? "domain " : "",
2028 parent_res, conflict->name, conflict);
98a35831
YL
2029
2030 return conflict == NULL;
2031}
2032
2033int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
2034{
2035 struct resource *res = &b->busn_res;
2036 struct resource old_res = *res;
2037 resource_size_t size;
2038 int ret;
2039
2040 if (res->start > bus_max)
2041 return -EINVAL;
2042
2043 size = bus_max - res->start + 1;
2044 ret = adjust_resource(res, res->start, size);
2045 dev_printk(KERN_DEBUG, &b->dev,
2046 "busn_res: %pR end %s updated to %02x\n",
2047 &old_res, ret ? "can not be" : "is", bus_max);
2048
2049 if (!ret && !res->parent)
2050 pci_bus_insert_busn_res(b, res->start, res->end);
2051
2052 return ret;
2053}
2054
2055void pci_bus_release_busn_res(struct pci_bus *b)
2056{
2057 struct resource *res = &b->busn_res;
2058 int ret;
2059
2060 if (!res->flags || !res->parent)
2061 return;
2062
2063 ret = release_resource(res);
2064 dev_printk(KERN_DEBUG, &b->dev,
2065 "busn_res: %pR %s released\n",
2066 res, ret ? "can not be" : "is");
2067}
2068
15856ad5 2069struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
a2ebb827
BH
2070 struct pci_ops *ops, void *sysdata, struct list_head *resources)
2071{
4d99f524
YL
2072 struct pci_host_bridge_window *window;
2073 bool found = false;
a2ebb827 2074 struct pci_bus *b;
4d99f524
YL
2075 int max;
2076
2077 list_for_each_entry(window, resources, list)
2078 if (window->res->flags & IORESOURCE_BUS) {
2079 found = true;
2080 break;
2081 }
a2ebb827
BH
2082
2083 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
2084 if (!b)
2085 return NULL;
2086
4d99f524
YL
2087 if (!found) {
2088 dev_info(&b->dev,
2089 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2090 bus);
2091 pci_bus_insert_busn_res(b, bus, 255);
2092 }
2093
2094 max = pci_scan_child_bus(b);
2095
2096 if (!found)
2097 pci_bus_update_busn_res_end(b, max);
2098
a2ebb827
BH
2099 pci_bus_add_devices(b);
2100 return b;
2101}
2102EXPORT_SYMBOL(pci_scan_root_bus);
2103
7e00fe2e 2104/* Deprecated; use pci_scan_root_bus() instead */
15856ad5 2105struct pci_bus *pci_scan_bus_parented(struct device *parent,
cdb9b9f7
PM
2106 int bus, struct pci_ops *ops, void *sysdata)
2107{
1e39ae9f 2108 LIST_HEAD(resources);
cdb9b9f7
PM
2109 struct pci_bus *b;
2110
1e39ae9f
BH
2111 pci_add_resource(&resources, &ioport_resource);
2112 pci_add_resource(&resources, &iomem_resource);
857c3b66 2113 pci_add_resource(&resources, &busn_resource);
1e39ae9f 2114 b = pci_create_root_bus(parent, bus, ops, sysdata, &resources);
cdb9b9f7 2115 if (b)
857c3b66 2116 pci_scan_child_bus(b);
1e39ae9f
BH
2117 else
2118 pci_free_resource_list(&resources);
cdb9b9f7
PM
2119 return b;
2120}
1da177e4
LT
2121EXPORT_SYMBOL(pci_scan_bus_parented);
2122
15856ad5 2123struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
de4b2f76
BH
2124 void *sysdata)
2125{
2126 LIST_HEAD(resources);
2127 struct pci_bus *b;
2128
2129 pci_add_resource(&resources, &ioport_resource);
2130 pci_add_resource(&resources, &iomem_resource);
857c3b66 2131 pci_add_resource(&resources, &busn_resource);
de4b2f76
BH
2132 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
2133 if (b) {
857c3b66 2134 pci_scan_child_bus(b);
de4b2f76
BH
2135 pci_bus_add_devices(b);
2136 } else {
2137 pci_free_resource_list(&resources);
2138 }
2139 return b;
2140}
2141EXPORT_SYMBOL(pci_scan_bus);
2142
2f320521
YL
2143/**
2144 * pci_rescan_bus_bridge_resize - scan a PCI bus for devices.
2145 * @bridge: PCI bridge for the bus to scan
2146 *
2147 * Scan a PCI bus and child buses for new devices, add them,
2148 * and enable them, resizing bridge mmio/io resource if necessary
2149 * and possible. The caller must ensure the child devices are already
2150 * removed for resizing to occur.
2151 *
2152 * Returns the max number of subordinate bus discovered.
2153 */
10874f5a 2154unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
2f320521
YL
2155{
2156 unsigned int max;
2157 struct pci_bus *bus = bridge->subordinate;
2158
2159 max = pci_scan_child_bus(bus);
2160
2161 pci_assign_unassigned_bridge_resources(bridge);
2162
2163 pci_bus_add_devices(bus);
2164
2165 return max;
2166}
2167
a5213a31
YL
2168/**
2169 * pci_rescan_bus - scan a PCI bus for devices.
2170 * @bus: PCI bus to scan
2171 *
2172 * Scan a PCI bus and child buses for new devices, adds them,
2173 * and enables them.
2174 *
2175 * Returns the max number of subordinate bus discovered.
2176 */
10874f5a 2177unsigned int pci_rescan_bus(struct pci_bus *bus)
a5213a31
YL
2178{
2179 unsigned int max;
2180
2181 max = pci_scan_child_bus(bus);
2182 pci_assign_unassigned_bus_resources(bus);
2183 pci_bus_add_devices(bus);
2184
2185 return max;
2186}
2187EXPORT_SYMBOL_GPL(pci_rescan_bus);
2188
9d16947b
RW
2189/*
2190 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
2191 * routines should always be executed under this mutex.
2192 */
2193static DEFINE_MUTEX(pci_rescan_remove_lock);
2194
2195void pci_lock_rescan_remove(void)
2196{
2197 mutex_lock(&pci_rescan_remove_lock);
2198}
2199EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
2200
2201void pci_unlock_rescan_remove(void)
2202{
2203 mutex_unlock(&pci_rescan_remove_lock);
2204}
2205EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
2206
3c78bc61
RD
2207static int __init pci_sort_bf_cmp(const struct device *d_a,
2208 const struct device *d_b)
6b4b78fe 2209{
99178b03
GKH
2210 const struct pci_dev *a = to_pci_dev(d_a);
2211 const struct pci_dev *b = to_pci_dev(d_b);
2212
6b4b78fe
MD
2213 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
2214 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
2215
2216 if (a->bus->number < b->bus->number) return -1;
2217 else if (a->bus->number > b->bus->number) return 1;
2218
2219 if (a->devfn < b->devfn) return -1;
2220 else if (a->devfn > b->devfn) return 1;
2221
2222 return 0;
2223}
2224
5ff580c1 2225void __init pci_sort_breadthfirst(void)
6b4b78fe 2226{
99178b03 2227 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
6b4b78fe 2228}
This page took 0.993416 seconds and 5 git commands to generate.