]>
Commit | Line | Data |
---|---|---|
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> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/cpumask.h> | |
7d715a6c | 12 | #include <linux/pci-aspm.h> |
bc56b9e0 | 13 | #include "pci.h" |
1da177e4 LT |
14 | |
15 | #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */ | |
16 | #define CARDBUS_RESERVE_BUSNR 3 | |
1da177e4 LT |
17 | |
18 | /* Ugh. Need to stop exporting this to modules. */ | |
19 | LIST_HEAD(pci_root_buses); | |
20 | EXPORT_SYMBOL(pci_root_buses); | |
21 | ||
70308923 GKH |
22 | static int find_anything(struct device *dev, void *data) |
23 | { | |
24 | return 1; | |
25 | } | |
1da177e4 | 26 | |
ed4aaadb ZY |
27 | /* |
28 | * Some device drivers need know if pci is initiated. | |
29 | * Basically, we think pci is not initiated when there | |
70308923 | 30 | * is no device to be found on the pci_bus_type. |
ed4aaadb ZY |
31 | */ |
32 | int no_pci_devices(void) | |
33 | { | |
70308923 GKH |
34 | struct device *dev; |
35 | int no_devices; | |
ed4aaadb | 36 | |
70308923 GKH |
37 | dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything); |
38 | no_devices = (dev == NULL); | |
39 | put_device(dev); | |
40 | return no_devices; | |
41 | } | |
ed4aaadb ZY |
42 | EXPORT_SYMBOL(no_pci_devices); |
43 | ||
1da177e4 LT |
44 | /* |
45 | * PCI Bus Class | |
46 | */ | |
fd7d1ced | 47 | static void release_pcibus_dev(struct device *dev) |
1da177e4 | 48 | { |
fd7d1ced | 49 | struct pci_bus *pci_bus = to_pci_bus(dev); |
1da177e4 LT |
50 | |
51 | if (pci_bus->bridge) | |
52 | put_device(pci_bus->bridge); | |
2fe2abf8 | 53 | pci_bus_remove_resources(pci_bus); |
98d9f30c | 54 | pci_release_bus_of_node(pci_bus); |
1da177e4 LT |
55 | kfree(pci_bus); |
56 | } | |
57 | ||
58 | static struct class pcibus_class = { | |
59 | .name = "pci_bus", | |
fd7d1ced | 60 | .dev_release = &release_pcibus_dev, |
b9d320fc | 61 | .dev_attrs = pcibus_dev_attrs, |
1da177e4 LT |
62 | }; |
63 | ||
64 | static int __init pcibus_class_init(void) | |
65 | { | |
66 | return class_register(&pcibus_class); | |
67 | } | |
68 | postcore_initcall(pcibus_class_init); | |
69 | ||
6ac665c6 | 70 | static u64 pci_size(u64 base, u64 maxbase, u64 mask) |
1da177e4 | 71 | { |
6ac665c6 | 72 | u64 size = mask & maxbase; /* Find the significant bits */ |
1da177e4 LT |
73 | if (!size) |
74 | return 0; | |
75 | ||
76 | /* Get the lowest of them to find the decode size, and | |
77 | from that the extent. */ | |
78 | size = (size & ~(size-1)) - 1; | |
79 | ||
80 | /* base == maxbase can be valid only if the BAR has | |
81 | already been programmed with all 1s. */ | |
82 | if (base == maxbase && ((base | size) & mask) != mask) | |
83 | return 0; | |
84 | ||
85 | return size; | |
86 | } | |
87 | ||
28c6821a | 88 | static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar) |
6ac665c6 | 89 | { |
8d6a6a47 | 90 | u32 mem_type; |
28c6821a | 91 | unsigned long flags; |
8d6a6a47 | 92 | |
6ac665c6 | 93 | if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) { |
28c6821a BH |
94 | flags = bar & ~PCI_BASE_ADDRESS_IO_MASK; |
95 | flags |= IORESOURCE_IO; | |
96 | return flags; | |
6ac665c6 | 97 | } |
07eddf3d | 98 | |
28c6821a BH |
99 | flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK; |
100 | flags |= IORESOURCE_MEM; | |
101 | if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH) | |
102 | flags |= IORESOURCE_PREFETCH; | |
07eddf3d | 103 | |
8d6a6a47 BH |
104 | mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK; |
105 | switch (mem_type) { | |
106 | case PCI_BASE_ADDRESS_MEM_TYPE_32: | |
107 | break; | |
108 | case PCI_BASE_ADDRESS_MEM_TYPE_1M: | |
109 | dev_info(&dev->dev, "1M mem BAR treated as 32-bit BAR\n"); | |
110 | break; | |
111 | case PCI_BASE_ADDRESS_MEM_TYPE_64: | |
28c6821a BH |
112 | flags |= IORESOURCE_MEM_64; |
113 | break; | |
8d6a6a47 BH |
114 | default: |
115 | dev_warn(&dev->dev, | |
116 | "mem unknown type %x treated as 32-bit BAR\n", | |
117 | mem_type); | |
118 | break; | |
119 | } | |
28c6821a | 120 | return flags; |
07eddf3d YL |
121 | } |
122 | ||
0b400c7e YZ |
123 | /** |
124 | * pci_read_base - read a PCI BAR | |
125 | * @dev: the PCI device | |
126 | * @type: type of the BAR | |
127 | * @res: resource buffer to be filled in | |
128 | * @pos: BAR position in the config space | |
129 | * | |
130 | * Returns 1 if the BAR is 64-bit, or 0 if 32-bit. | |
6ac665c6 | 131 | */ |
0b400c7e | 132 | int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, |
6ac665c6 | 133 | struct resource *res, unsigned int pos) |
07eddf3d | 134 | { |
6ac665c6 | 135 | u32 l, sz, mask; |
253d2e54 | 136 | u16 orig_cmd; |
5bfa14ed | 137 | struct pci_bus_region region; |
6ac665c6 | 138 | |
1ed67439 | 139 | mask = type ? PCI_ROM_ADDRESS_MASK : ~0; |
6ac665c6 | 140 | |
253d2e54 JP |
141 | if (!dev->mmio_always_on) { |
142 | pci_read_config_word(dev, PCI_COMMAND, &orig_cmd); | |
143 | pci_write_config_word(dev, PCI_COMMAND, | |
144 | orig_cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO)); | |
145 | } | |
146 | ||
6ac665c6 MW |
147 | res->name = pci_name(dev); |
148 | ||
149 | pci_read_config_dword(dev, pos, &l); | |
1ed67439 | 150 | pci_write_config_dword(dev, pos, l | mask); |
6ac665c6 MW |
151 | pci_read_config_dword(dev, pos, &sz); |
152 | pci_write_config_dword(dev, pos, l); | |
153 | ||
253d2e54 JP |
154 | if (!dev->mmio_always_on) |
155 | pci_write_config_word(dev, PCI_COMMAND, orig_cmd); | |
156 | ||
6ac665c6 MW |
157 | /* |
158 | * All bits set in sz means the device isn't working properly. | |
45aa23b4 BH |
159 | * If the BAR isn't implemented, all bits must be 0. If it's a |
160 | * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit | |
161 | * 1 must be clear. | |
6ac665c6 | 162 | */ |
45aa23b4 | 163 | if (!sz || sz == 0xffffffff) |
6ac665c6 MW |
164 | goto fail; |
165 | ||
166 | /* | |
167 | * I don't know how l can have all bits set. Copied from old code. | |
168 | * Maybe it fixes a bug on some ancient platform. | |
169 | */ | |
170 | if (l == 0xffffffff) | |
171 | l = 0; | |
172 | ||
173 | if (type == pci_bar_unknown) { | |
28c6821a BH |
174 | res->flags = decode_bar(dev, l); |
175 | res->flags |= IORESOURCE_SIZEALIGN; | |
176 | if (res->flags & IORESOURCE_IO) { | |
6ac665c6 | 177 | l &= PCI_BASE_ADDRESS_IO_MASK; |
5aceca9d | 178 | mask = PCI_BASE_ADDRESS_IO_MASK & (u32) IO_SPACE_LIMIT; |
6ac665c6 MW |
179 | } else { |
180 | l &= PCI_BASE_ADDRESS_MEM_MASK; | |
181 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; | |
182 | } | |
183 | } else { | |
184 | res->flags |= (l & IORESOURCE_ROM_ENABLE); | |
185 | l &= PCI_ROM_ADDRESS_MASK; | |
186 | mask = (u32)PCI_ROM_ADDRESS_MASK; | |
187 | } | |
188 | ||
28c6821a | 189 | if (res->flags & IORESOURCE_MEM_64) { |
6ac665c6 MW |
190 | u64 l64 = l; |
191 | u64 sz64 = sz; | |
192 | u64 mask64 = mask | (u64)~0 << 32; | |
193 | ||
194 | pci_read_config_dword(dev, pos + 4, &l); | |
195 | pci_write_config_dword(dev, pos + 4, ~0); | |
196 | pci_read_config_dword(dev, pos + 4, &sz); | |
197 | pci_write_config_dword(dev, pos + 4, l); | |
198 | ||
199 | l64 |= ((u64)l << 32); | |
200 | sz64 |= ((u64)sz << 32); | |
201 | ||
202 | sz64 = pci_size(l64, sz64, mask64); | |
203 | ||
204 | if (!sz64) | |
205 | goto fail; | |
206 | ||
cc5499c3 | 207 | if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) { |
865df576 BH |
208 | dev_err(&dev->dev, "reg %x: can't handle 64-bit BAR\n", |
209 | pos); | |
6ac665c6 | 210 | goto fail; |
c7dabef8 BH |
211 | } |
212 | ||
c7dabef8 | 213 | if ((sizeof(resource_size_t) < 8) && l) { |
6ac665c6 MW |
214 | /* Address above 32-bit boundary; disable the BAR */ |
215 | pci_write_config_dword(dev, pos, 0); | |
216 | pci_write_config_dword(dev, pos + 4, 0); | |
5bfa14ed BH |
217 | region.start = 0; |
218 | region.end = sz64; | |
fb127cb9 | 219 | pcibios_bus_to_resource(dev, res, ®ion); |
6ac665c6 | 220 | } else { |
5bfa14ed BH |
221 | region.start = l64; |
222 | region.end = l64 + sz64; | |
fb127cb9 | 223 | pcibios_bus_to_resource(dev, res, ®ion); |
c7dabef8 | 224 | dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", |
a369c791 | 225 | pos, res); |
6ac665c6 MW |
226 | } |
227 | } else { | |
45aa23b4 | 228 | sz = pci_size(l, sz, mask); |
6ac665c6 | 229 | |
45aa23b4 | 230 | if (!sz) |
6ac665c6 MW |
231 | goto fail; |
232 | ||
5bfa14ed BH |
233 | region.start = l; |
234 | region.end = l + sz; | |
fb127cb9 | 235 | pcibios_bus_to_resource(dev, res, ®ion); |
f393d9b1 | 236 | |
c7dabef8 | 237 | dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos, res); |
6ac665c6 MW |
238 | } |
239 | ||
240 | out: | |
28c6821a | 241 | return (res->flags & IORESOURCE_MEM_64) ? 1 : 0; |
6ac665c6 MW |
242 | fail: |
243 | res->flags = 0; | |
244 | goto out; | |
07eddf3d YL |
245 | } |
246 | ||
1da177e4 LT |
247 | static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) |
248 | { | |
6ac665c6 | 249 | unsigned int pos, reg; |
07eddf3d | 250 | |
6ac665c6 MW |
251 | for (pos = 0; pos < howmany; pos++) { |
252 | struct resource *res = &dev->resource[pos]; | |
1da177e4 | 253 | reg = PCI_BASE_ADDRESS_0 + (pos << 2); |
6ac665c6 | 254 | pos += __pci_read_base(dev, pci_bar_unknown, res, reg); |
1da177e4 | 255 | } |
6ac665c6 | 256 | |
1da177e4 | 257 | if (rom) { |
6ac665c6 | 258 | struct resource *res = &dev->resource[PCI_ROM_RESOURCE]; |
1da177e4 | 259 | dev->rom_base_reg = rom; |
6ac665c6 MW |
260 | res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | |
261 | IORESOURCE_READONLY | IORESOURCE_CACHEABLE | | |
262 | IORESOURCE_SIZEALIGN; | |
263 | __pci_read_base(dev, pci_bar_mem32, res, rom); | |
1da177e4 LT |
264 | } |
265 | } | |
266 | ||
fa27b2d1 | 267 | static void __devinit pci_read_bridge_io(struct pci_bus *child) |
1da177e4 LT |
268 | { |
269 | struct pci_dev *dev = child->self; | |
270 | u8 io_base_lo, io_limit_lo; | |
1da177e4 | 271 | unsigned long base, limit; |
5bfa14ed BH |
272 | struct pci_bus_region region; |
273 | struct resource *res, res2; | |
1da177e4 | 274 | |
1da177e4 LT |
275 | res = child->resource[0]; |
276 | pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); | |
277 | pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo); | |
278 | base = (io_base_lo & PCI_IO_RANGE_MASK) << 8; | |
279 | limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8; | |
280 | ||
281 | if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { | |
282 | u16 io_base_hi, io_limit_hi; | |
283 | pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi); | |
284 | pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi); | |
285 | base |= (io_base_hi << 16); | |
286 | limit |= (io_limit_hi << 16); | |
287 | } | |
288 | ||
cd81e1ea | 289 | if (base && base <= limit) { |
1da177e4 | 290 | res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; |
cf48fb6a | 291 | res2.flags = res->flags; |
5bfa14ed BH |
292 | region.start = base; |
293 | region.end = limit + 0xfff; | |
fb127cb9 | 294 | pcibios_bus_to_resource(dev, &res2, ®ion); |
9d265124 | 295 | if (!res->start) |
5bfa14ed | 296 | res->start = res2.start; |
9d265124 | 297 | if (!res->end) |
5bfa14ed | 298 | res->end = res2.end; |
c7dabef8 | 299 | dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res); |
1da177e4 | 300 | } |
fa27b2d1 BH |
301 | } |
302 | ||
303 | static void __devinit pci_read_bridge_mmio(struct pci_bus *child) | |
304 | { | |
305 | struct pci_dev *dev = child->self; | |
306 | u16 mem_base_lo, mem_limit_lo; | |
307 | unsigned long base, limit; | |
5bfa14ed | 308 | struct pci_bus_region region; |
fa27b2d1 | 309 | struct resource *res; |
1da177e4 LT |
310 | |
311 | res = child->resource[1]; | |
312 | pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo); | |
313 | pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo); | |
314 | base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16; | |
315 | limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; | |
cd81e1ea | 316 | if (base && base <= limit) { |
1da177e4 | 317 | res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM; |
5bfa14ed BH |
318 | region.start = base; |
319 | region.end = limit + 0xfffff; | |
fb127cb9 | 320 | pcibios_bus_to_resource(dev, res, ®ion); |
c7dabef8 | 321 | dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res); |
1da177e4 | 322 | } |
fa27b2d1 BH |
323 | } |
324 | ||
325 | static void __devinit pci_read_bridge_mmio_pref(struct pci_bus *child) | |
326 | { | |
327 | struct pci_dev *dev = child->self; | |
328 | u16 mem_base_lo, mem_limit_lo; | |
329 | unsigned long base, limit; | |
5bfa14ed | 330 | struct pci_bus_region region; |
fa27b2d1 | 331 | struct resource *res; |
1da177e4 LT |
332 | |
333 | res = child->resource[2]; | |
334 | pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo); | |
335 | pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo); | |
336 | base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16; | |
337 | limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16; | |
338 | ||
339 | if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { | |
340 | u32 mem_base_hi, mem_limit_hi; | |
341 | pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi); | |
342 | pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi); | |
343 | ||
344 | /* | |
345 | * Some bridges set the base > limit by default, and some | |
346 | * (broken) BIOSes do not initialize them. If we find | |
347 | * this, just assume they are not being used. | |
348 | */ | |
349 | if (mem_base_hi <= mem_limit_hi) { | |
350 | #if BITS_PER_LONG == 64 | |
351 | base |= ((long) mem_base_hi) << 32; | |
352 | limit |= ((long) mem_limit_hi) << 32; | |
353 | #else | |
354 | if (mem_base_hi || mem_limit_hi) { | |
80ccba11 BH |
355 | dev_err(&dev->dev, "can't handle 64-bit " |
356 | "address space for bridge\n"); | |
1da177e4 LT |
357 | return; |
358 | } | |
359 | #endif | |
360 | } | |
361 | } | |
cd81e1ea | 362 | if (base && base <= limit) { |
1f82de10 YL |
363 | res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) | |
364 | IORESOURCE_MEM | IORESOURCE_PREFETCH; | |
365 | if (res->flags & PCI_PREF_RANGE_TYPE_64) | |
366 | res->flags |= IORESOURCE_MEM_64; | |
5bfa14ed BH |
367 | region.start = base; |
368 | region.end = limit + 0xfffff; | |
fb127cb9 | 369 | pcibios_bus_to_resource(dev, res, ®ion); |
c7dabef8 | 370 | dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res); |
1da177e4 LT |
371 | } |
372 | } | |
373 | ||
fa27b2d1 BH |
374 | void __devinit pci_read_bridge_bases(struct pci_bus *child) |
375 | { | |
376 | struct pci_dev *dev = child->self; | |
2fe2abf8 | 377 | struct resource *res; |
fa27b2d1 BH |
378 | int i; |
379 | ||
380 | if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */ | |
381 | return; | |
382 | ||
383 | dev_info(&dev->dev, "PCI bridge to [bus %02x-%02x]%s\n", | |
384 | child->secondary, child->subordinate, | |
385 | dev->transparent ? " (subtractive decode)" : ""); | |
386 | ||
2fe2abf8 BH |
387 | pci_bus_remove_resources(child); |
388 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) | |
389 | child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i]; | |
390 | ||
fa27b2d1 BH |
391 | pci_read_bridge_io(child); |
392 | pci_read_bridge_mmio(child); | |
393 | pci_read_bridge_mmio_pref(child); | |
2adf7516 BH |
394 | |
395 | if (dev->transparent) { | |
2fe2abf8 BH |
396 | pci_bus_for_each_resource(child->parent, res, i) { |
397 | if (res) { | |
398 | pci_bus_add_resource(child, res, | |
399 | PCI_SUBTRACTIVE_DECODE); | |
2adf7516 BH |
400 | dev_printk(KERN_DEBUG, &dev->dev, |
401 | " bridge window %pR (subtractive decode)\n", | |
2fe2abf8 BH |
402 | res); |
403 | } | |
2adf7516 BH |
404 | } |
405 | } | |
fa27b2d1 BH |
406 | } |
407 | ||
96bde06a | 408 | static struct pci_bus * pci_alloc_bus(void) |
1da177e4 LT |
409 | { |
410 | struct pci_bus *b; | |
411 | ||
f5afe806 | 412 | b = kzalloc(sizeof(*b), GFP_KERNEL); |
1da177e4 | 413 | if (b) { |
1da177e4 LT |
414 | INIT_LIST_HEAD(&b->node); |
415 | INIT_LIST_HEAD(&b->children); | |
416 | INIT_LIST_HEAD(&b->devices); | |
f46753c5 | 417 | INIT_LIST_HEAD(&b->slots); |
2fe2abf8 | 418 | INIT_LIST_HEAD(&b->resources); |
3749c51a MW |
419 | b->max_bus_speed = PCI_SPEED_UNKNOWN; |
420 | b->cur_bus_speed = PCI_SPEED_UNKNOWN; | |
1da177e4 LT |
421 | } |
422 | return b; | |
423 | } | |
424 | ||
7b543663 YL |
425 | static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b) |
426 | { | |
427 | struct pci_host_bridge *bridge; | |
428 | ||
429 | bridge = kzalloc(sizeof(*bridge), GFP_KERNEL); | |
430 | if (bridge) { | |
431 | INIT_LIST_HEAD(&bridge->windows); | |
432 | bridge->bus = b; | |
433 | } | |
434 | ||
435 | return bridge; | |
436 | } | |
437 | ||
9be60ca0 MW |
438 | static unsigned char pcix_bus_speed[] = { |
439 | PCI_SPEED_UNKNOWN, /* 0 */ | |
440 | PCI_SPEED_66MHz_PCIX, /* 1 */ | |
441 | PCI_SPEED_100MHz_PCIX, /* 2 */ | |
442 | PCI_SPEED_133MHz_PCIX, /* 3 */ | |
443 | PCI_SPEED_UNKNOWN, /* 4 */ | |
444 | PCI_SPEED_66MHz_PCIX_ECC, /* 5 */ | |
445 | PCI_SPEED_100MHz_PCIX_ECC, /* 6 */ | |
446 | PCI_SPEED_133MHz_PCIX_ECC, /* 7 */ | |
447 | PCI_SPEED_UNKNOWN, /* 8 */ | |
448 | PCI_SPEED_66MHz_PCIX_266, /* 9 */ | |
449 | PCI_SPEED_100MHz_PCIX_266, /* A */ | |
450 | PCI_SPEED_133MHz_PCIX_266, /* B */ | |
451 | PCI_SPEED_UNKNOWN, /* C */ | |
452 | PCI_SPEED_66MHz_PCIX_533, /* D */ | |
453 | PCI_SPEED_100MHz_PCIX_533, /* E */ | |
454 | PCI_SPEED_133MHz_PCIX_533 /* F */ | |
455 | }; | |
456 | ||
3749c51a MW |
457 | static unsigned char pcie_link_speed[] = { |
458 | PCI_SPEED_UNKNOWN, /* 0 */ | |
459 | PCIE_SPEED_2_5GT, /* 1 */ | |
460 | PCIE_SPEED_5_0GT, /* 2 */ | |
9dfd97fe | 461 | PCIE_SPEED_8_0GT, /* 3 */ |
3749c51a MW |
462 | PCI_SPEED_UNKNOWN, /* 4 */ |
463 | PCI_SPEED_UNKNOWN, /* 5 */ | |
464 | PCI_SPEED_UNKNOWN, /* 6 */ | |
465 | PCI_SPEED_UNKNOWN, /* 7 */ | |
466 | PCI_SPEED_UNKNOWN, /* 8 */ | |
467 | PCI_SPEED_UNKNOWN, /* 9 */ | |
468 | PCI_SPEED_UNKNOWN, /* A */ | |
469 | PCI_SPEED_UNKNOWN, /* B */ | |
470 | PCI_SPEED_UNKNOWN, /* C */ | |
471 | PCI_SPEED_UNKNOWN, /* D */ | |
472 | PCI_SPEED_UNKNOWN, /* E */ | |
473 | PCI_SPEED_UNKNOWN /* F */ | |
474 | }; | |
475 | ||
476 | void pcie_update_link_speed(struct pci_bus *bus, u16 linksta) | |
477 | { | |
478 | bus->cur_bus_speed = pcie_link_speed[linksta & 0xf]; | |
479 | } | |
480 | EXPORT_SYMBOL_GPL(pcie_update_link_speed); | |
481 | ||
45b4cdd5 MW |
482 | static unsigned char agp_speeds[] = { |
483 | AGP_UNKNOWN, | |
484 | AGP_1X, | |
485 | AGP_2X, | |
486 | AGP_4X, | |
487 | AGP_8X | |
488 | }; | |
489 | ||
490 | static enum pci_bus_speed agp_speed(int agp3, int agpstat) | |
491 | { | |
492 | int index = 0; | |
493 | ||
494 | if (agpstat & 4) | |
495 | index = 3; | |
496 | else if (agpstat & 2) | |
497 | index = 2; | |
498 | else if (agpstat & 1) | |
499 | index = 1; | |
500 | else | |
501 | goto out; | |
502 | ||
503 | if (agp3) { | |
504 | index += 2; | |
505 | if (index == 5) | |
506 | index = 0; | |
507 | } | |
508 | ||
509 | out: | |
510 | return agp_speeds[index]; | |
511 | } | |
512 | ||
513 | ||
9be60ca0 MW |
514 | static void pci_set_bus_speed(struct pci_bus *bus) |
515 | { | |
516 | struct pci_dev *bridge = bus->self; | |
517 | int pos; | |
518 | ||
45b4cdd5 MW |
519 | pos = pci_find_capability(bridge, PCI_CAP_ID_AGP); |
520 | if (!pos) | |
521 | pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3); | |
522 | if (pos) { | |
523 | u32 agpstat, agpcmd; | |
524 | ||
525 | pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat); | |
526 | bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7); | |
527 | ||
528 | pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd); | |
529 | bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7); | |
530 | } | |
531 | ||
9be60ca0 MW |
532 | pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX); |
533 | if (pos) { | |
534 | u16 status; | |
535 | enum pci_bus_speed max; | |
536 | pci_read_config_word(bridge, pos + 2, &status); | |
537 | ||
538 | if (status & 0x8000) { | |
539 | max = PCI_SPEED_133MHz_PCIX_533; | |
540 | } else if (status & 0x4000) { | |
541 | max = PCI_SPEED_133MHz_PCIX_266; | |
542 | } else if (status & 0x0002) { | |
543 | if (((status >> 12) & 0x3) == 2) { | |
544 | max = PCI_SPEED_133MHz_PCIX_ECC; | |
545 | } else { | |
546 | max = PCI_SPEED_133MHz_PCIX; | |
547 | } | |
548 | } else { | |
549 | max = PCI_SPEED_66MHz_PCIX; | |
550 | } | |
551 | ||
552 | bus->max_bus_speed = max; | |
553 | bus->cur_bus_speed = pcix_bus_speed[(status >> 6) & 0xf]; | |
554 | ||
555 | return; | |
556 | } | |
557 | ||
558 | pos = pci_find_capability(bridge, PCI_CAP_ID_EXP); | |
559 | if (pos) { | |
560 | u32 linkcap; | |
561 | u16 linksta; | |
562 | ||
563 | pci_read_config_dword(bridge, pos + PCI_EXP_LNKCAP, &linkcap); | |
564 | bus->max_bus_speed = pcie_link_speed[linkcap & 0xf]; | |
565 | ||
566 | pci_read_config_word(bridge, pos + PCI_EXP_LNKSTA, &linksta); | |
567 | pcie_update_link_speed(bus, linksta); | |
568 | } | |
569 | } | |
570 | ||
571 | ||
cbd4e055 AB |
572 | static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, |
573 | struct pci_dev *bridge, int busnr) | |
1da177e4 LT |
574 | { |
575 | struct pci_bus *child; | |
576 | int i; | |
577 | ||
578 | /* | |
579 | * Allocate a new bus, and inherit stuff from the parent.. | |
580 | */ | |
581 | child = pci_alloc_bus(); | |
582 | if (!child) | |
583 | return NULL; | |
584 | ||
1da177e4 LT |
585 | child->parent = parent; |
586 | child->ops = parent->ops; | |
587 | child->sysdata = parent->sysdata; | |
6e325a62 | 588 | child->bus_flags = parent->bus_flags; |
1da177e4 | 589 | |
fd7d1ced GKH |
590 | /* initialize some portions of the bus device, but don't register it |
591 | * now as the parent is not properly set up yet. This device will get | |
592 | * registered later in pci_bus_add_devices() | |
593 | */ | |
594 | child->dev.class = &pcibus_class; | |
1a927133 | 595 | dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr); |
1da177e4 LT |
596 | |
597 | /* | |
598 | * Set up the primary, secondary and subordinate | |
599 | * bus numbers. | |
600 | */ | |
601 | child->number = child->secondary = busnr; | |
602 | child->primary = parent->secondary; | |
603 | child->subordinate = 0xff; | |
604 | ||
3789fa8a YZ |
605 | if (!bridge) |
606 | return child; | |
607 | ||
608 | child->self = bridge; | |
609 | child->bridge = get_device(&bridge->dev); | |
98d9f30c | 610 | pci_set_bus_of_node(child); |
9be60ca0 MW |
611 | pci_set_bus_speed(child); |
612 | ||
1da177e4 | 613 | /* Set up default resource pointers and names.. */ |
fde09c6d | 614 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { |
1da177e4 LT |
615 | child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i]; |
616 | child->resource[i]->name = child->name; | |
617 | } | |
618 | bridge->subordinate = child; | |
619 | ||
620 | return child; | |
621 | } | |
622 | ||
451124a7 | 623 | struct pci_bus *__ref pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr) |
1da177e4 LT |
624 | { |
625 | struct pci_bus *child; | |
626 | ||
627 | child = pci_alloc_child_bus(parent, dev, busnr); | |
e4ea9bb7 | 628 | if (child) { |
d71374da | 629 | down_write(&pci_bus_sem); |
1da177e4 | 630 | list_add_tail(&child->node, &parent->children); |
d71374da | 631 | up_write(&pci_bus_sem); |
e4ea9bb7 | 632 | } |
1da177e4 LT |
633 | return child; |
634 | } | |
635 | ||
96bde06a | 636 | static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max) |
26f674ae GKH |
637 | { |
638 | struct pci_bus *parent = child->parent; | |
12f44f46 IK |
639 | |
640 | /* Attempts to fix that up are really dangerous unless | |
641 | we're going to re-assign all bus numbers. */ | |
642 | if (!pcibios_assign_all_busses()) | |
643 | return; | |
644 | ||
26f674ae GKH |
645 | while (parent->parent && parent->subordinate < max) { |
646 | parent->subordinate = max; | |
647 | pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max); | |
648 | parent = parent->parent; | |
649 | } | |
650 | } | |
651 | ||
1da177e4 LT |
652 | /* |
653 | * If it's a bridge, configure it and scan the bus behind it. | |
654 | * For CardBus bridges, we don't scan behind as the devices will | |
655 | * be handled by the bridge driver itself. | |
656 | * | |
657 | * We need to process bridges in two passes -- first we scan those | |
658 | * already configured by the BIOS and after we are done with all of | |
659 | * them, we proceed to assigning numbers to the remaining buses in | |
660 | * order to avoid overlaps between old and new bus numbers. | |
661 | */ | |
0ab2b57f | 662 | int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass) |
1da177e4 LT |
663 | { |
664 | struct pci_bus *child; | |
665 | int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS); | |
49887941 | 666 | u32 buses, i, j = 0; |
1da177e4 | 667 | u16 bctl; |
99ddd552 | 668 | u8 primary, secondary, subordinate; |
a1c19894 | 669 | int broken = 0; |
1da177e4 LT |
670 | |
671 | pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses); | |
99ddd552 BH |
672 | primary = buses & 0xFF; |
673 | secondary = (buses >> 8) & 0xFF; | |
674 | subordinate = (buses >> 16) & 0xFF; | |
1da177e4 | 675 | |
99ddd552 BH |
676 | dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n", |
677 | secondary, subordinate, pass); | |
1da177e4 | 678 | |
71f6bd4a YL |
679 | if (!primary && (primary != bus->number) && secondary && subordinate) { |
680 | dev_warn(&dev->dev, "Primary bus is hard wired to 0\n"); | |
681 | primary = bus->number; | |
682 | } | |
683 | ||
a1c19894 BH |
684 | /* Check if setup is sensible at all */ |
685 | if (!pass && | |
99ddd552 | 686 | (primary != bus->number || secondary <= bus->number)) { |
a1c19894 BH |
687 | dev_dbg(&dev->dev, "bus configuration invalid, reconfiguring\n"); |
688 | broken = 1; | |
689 | } | |
690 | ||
1da177e4 LT |
691 | /* Disable MasterAbortMode during probing to avoid reporting |
692 | of bus errors (in some architectures) */ | |
693 | pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl); | |
694 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, | |
695 | bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT); | |
696 | ||
99ddd552 BH |
697 | if ((secondary || subordinate) && !pcibios_assign_all_busses() && |
698 | !is_cardbus && !broken) { | |
699 | unsigned int cmax; | |
1da177e4 LT |
700 | /* |
701 | * Bus already configured by firmware, process it in the first | |
702 | * pass and just note the configuration. | |
703 | */ | |
704 | if (pass) | |
bbe8f9a3 | 705 | goto out; |
1da177e4 LT |
706 | |
707 | /* | |
708 | * If we already got to this bus through a different bridge, | |
74710ded AC |
709 | * don't re-add it. This can happen with the i450NX chipset. |
710 | * | |
711 | * However, we continue to descend down the hierarchy and | |
712 | * scan remaining child buses. | |
1da177e4 | 713 | */ |
99ddd552 | 714 | child = pci_find_bus(pci_domain_nr(bus), secondary); |
74710ded | 715 | if (!child) { |
99ddd552 | 716 | child = pci_add_new_bus(bus, dev, secondary); |
74710ded AC |
717 | if (!child) |
718 | goto out; | |
99ddd552 BH |
719 | child->primary = primary; |
720 | child->subordinate = subordinate; | |
74710ded | 721 | child->bridge_ctl = bctl; |
1da177e4 LT |
722 | } |
723 | ||
1da177e4 LT |
724 | cmax = pci_scan_child_bus(child); |
725 | if (cmax > max) | |
726 | max = cmax; | |
727 | if (child->subordinate > max) | |
728 | max = child->subordinate; | |
729 | } else { | |
730 | /* | |
731 | * We need to assign a number to this bus which we always | |
732 | * do in the second pass. | |
733 | */ | |
12f44f46 | 734 | if (!pass) { |
a1c19894 | 735 | if (pcibios_assign_all_busses() || broken) |
12f44f46 IK |
736 | /* Temporarily disable forwarding of the |
737 | configuration cycles on all bridges in | |
738 | this bus segment to avoid possible | |
739 | conflicts in the second pass between two | |
740 | bridges programmed with overlapping | |
741 | bus ranges. */ | |
742 | pci_write_config_dword(dev, PCI_PRIMARY_BUS, | |
743 | buses & ~0xffffff); | |
bbe8f9a3 | 744 | goto out; |
12f44f46 | 745 | } |
1da177e4 LT |
746 | |
747 | /* Clear errors */ | |
748 | pci_write_config_word(dev, PCI_STATUS, 0xffff); | |
749 | ||
cc57450f | 750 | /* Prevent assigning a bus number that already exists. |
b1a98b69 TC |
751 | * This can happen when a bridge is hot-plugged, so in |
752 | * this case we only re-scan this bus. */ | |
753 | child = pci_find_bus(pci_domain_nr(bus), max+1); | |
754 | if (!child) { | |
755 | child = pci_add_new_bus(bus, dev, ++max); | |
756 | if (!child) | |
757 | goto out; | |
758 | } | |
1da177e4 LT |
759 | buses = (buses & 0xff000000) |
760 | | ((unsigned int)(child->primary) << 0) | |
761 | | ((unsigned int)(child->secondary) << 8) | |
762 | | ((unsigned int)(child->subordinate) << 16); | |
763 | ||
764 | /* | |
765 | * yenta.c forces a secondary latency timer of 176. | |
766 | * Copy that behaviour here. | |
767 | */ | |
768 | if (is_cardbus) { | |
769 | buses &= ~0xff000000; | |
770 | buses |= CARDBUS_LATENCY_TIMER << 24; | |
771 | } | |
7c867c88 | 772 | |
1da177e4 LT |
773 | /* |
774 | * We need to blast all three values with a single write. | |
775 | */ | |
776 | pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses); | |
777 | ||
778 | if (!is_cardbus) { | |
11949255 | 779 | child->bridge_ctl = bctl; |
26f674ae GKH |
780 | /* |
781 | * Adjust subordinate busnr in parent buses. | |
782 | * We do this before scanning for children because | |
783 | * some devices may not be detected if the bios | |
784 | * was lazy. | |
785 | */ | |
786 | pci_fixup_parent_subordinate_busnr(child, max); | |
1da177e4 LT |
787 | /* Now we can scan all subordinate buses... */ |
788 | max = pci_scan_child_bus(child); | |
e3ac86d8 KA |
789 | /* |
790 | * now fix it up again since we have found | |
791 | * the real value of max. | |
792 | */ | |
793 | pci_fixup_parent_subordinate_busnr(child, max); | |
1da177e4 LT |
794 | } else { |
795 | /* | |
796 | * For CardBus bridges, we leave 4 bus numbers | |
797 | * as cards with a PCI-to-PCI bridge can be | |
798 | * inserted later. | |
799 | */ | |
49887941 DB |
800 | for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) { |
801 | struct pci_bus *parent = bus; | |
cc57450f RS |
802 | if (pci_find_bus(pci_domain_nr(bus), |
803 | max+i+1)) | |
804 | break; | |
49887941 DB |
805 | while (parent->parent) { |
806 | if ((!pcibios_assign_all_busses()) && | |
807 | (parent->subordinate > max) && | |
808 | (parent->subordinate <= max+i)) { | |
809 | j = 1; | |
810 | } | |
811 | parent = parent->parent; | |
812 | } | |
813 | if (j) { | |
814 | /* | |
815 | * Often, there are two cardbus bridges | |
816 | * -- try to leave one valid bus number | |
817 | * for each one. | |
818 | */ | |
819 | i /= 2; | |
820 | break; | |
821 | } | |
822 | } | |
cc57450f | 823 | max += i; |
26f674ae | 824 | pci_fixup_parent_subordinate_busnr(child, max); |
1da177e4 LT |
825 | } |
826 | /* | |
827 | * Set the subordinate bus number to its real value. | |
828 | */ | |
829 | child->subordinate = max; | |
830 | pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max); | |
831 | } | |
832 | ||
cb3576fa GH |
833 | sprintf(child->name, |
834 | (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"), | |
835 | pci_domain_nr(bus), child->number); | |
1da177e4 | 836 | |
d55bef51 | 837 | /* Has only triggered on CardBus, fixup is in yenta_socket */ |
49887941 DB |
838 | while (bus->parent) { |
839 | if ((child->subordinate > bus->subordinate) || | |
840 | (child->number > bus->subordinate) || | |
841 | (child->number < bus->number) || | |
842 | (child->subordinate < bus->number)) { | |
865df576 BH |
843 | dev_info(&child->dev, "[bus %02x-%02x] %s " |
844 | "hidden behind%s bridge %s [bus %02x-%02x]\n", | |
d55bef51 BK |
845 | child->number, child->subordinate, |
846 | (bus->number > child->subordinate && | |
847 | bus->subordinate < child->number) ? | |
a6f29a98 JP |
848 | "wholly" : "partially", |
849 | bus->self->transparent ? " transparent" : "", | |
865df576 | 850 | dev_name(&bus->dev), |
d55bef51 | 851 | bus->number, bus->subordinate); |
49887941 DB |
852 | } |
853 | bus = bus->parent; | |
854 | } | |
855 | ||
bbe8f9a3 RB |
856 | out: |
857 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl); | |
858 | ||
1da177e4 LT |
859 | return max; |
860 | } | |
861 | ||
862 | /* | |
863 | * Read interrupt line and base address registers. | |
864 | * The architecture-dependent code can tweak these, of course. | |
865 | */ | |
866 | static void pci_read_irq(struct pci_dev *dev) | |
867 | { | |
868 | unsigned char irq; | |
869 | ||
870 | pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq); | |
ffeff788 | 871 | dev->pin = irq; |
1da177e4 LT |
872 | if (irq) |
873 | pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq); | |
874 | dev->irq = irq; | |
875 | } | |
876 | ||
bb209c82 | 877 | void set_pcie_port_type(struct pci_dev *pdev) |
480b93b7 YZ |
878 | { |
879 | int pos; | |
880 | u16 reg16; | |
881 | ||
882 | pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); | |
883 | if (!pos) | |
884 | return; | |
885 | pdev->is_pcie = 1; | |
0efea000 | 886 | pdev->pcie_cap = pos; |
480b93b7 YZ |
887 | pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16); |
888 | pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4; | |
b03e7495 JM |
889 | pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, ®16); |
890 | pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD; | |
480b93b7 YZ |
891 | } |
892 | ||
bb209c82 | 893 | void set_pcie_hotplug_bridge(struct pci_dev *pdev) |
28760489 EB |
894 | { |
895 | int pos; | |
896 | u16 reg16; | |
897 | u32 reg32; | |
898 | ||
06a1cbaf | 899 | pos = pci_pcie_cap(pdev); |
28760489 EB |
900 | if (!pos) |
901 | return; | |
902 | pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16); | |
903 | if (!(reg16 & PCI_EXP_FLAGS_SLOT)) | |
904 | return; | |
905 | pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, ®32); | |
906 | if (reg32 & PCI_EXP_SLTCAP_HPC) | |
907 | pdev->is_hotplug_bridge = 1; | |
908 | } | |
909 | ||
01abc2aa | 910 | #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED) |
76e6a1d6 | 911 | |
1da177e4 LT |
912 | /** |
913 | * pci_setup_device - fill in class and map information of a device | |
914 | * @dev: the device structure to fill | |
915 | * | |
916 | * Initialize the device structure with information about the device's | |
917 | * vendor,class,memory and IO-space addresses,IRQ lines etc. | |
918 | * Called at initialisation of the PCI subsystem and by CardBus services. | |
480b93b7 YZ |
919 | * Returns 0 on success and negative if unknown type of device (not normal, |
920 | * bridge or CardBus). | |
1da177e4 | 921 | */ |
480b93b7 | 922 | int pci_setup_device(struct pci_dev *dev) |
1da177e4 LT |
923 | { |
924 | u32 class; | |
480b93b7 YZ |
925 | u8 hdr_type; |
926 | struct pci_slot *slot; | |
bc577d2b | 927 | int pos = 0; |
5bfa14ed BH |
928 | struct pci_bus_region region; |
929 | struct resource *res; | |
480b93b7 YZ |
930 | |
931 | if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type)) | |
932 | return -EIO; | |
933 | ||
934 | dev->sysdata = dev->bus->sysdata; | |
935 | dev->dev.parent = dev->bus->bridge; | |
936 | dev->dev.bus = &pci_bus_type; | |
937 | dev->hdr_type = hdr_type & 0x7f; | |
938 | dev->multifunction = !!(hdr_type & 0x80); | |
480b93b7 YZ |
939 | dev->error_state = pci_channel_io_normal; |
940 | set_pcie_port_type(dev); | |
941 | ||
942 | list_for_each_entry(slot, &dev->bus->slots, list) | |
943 | if (PCI_SLOT(dev->devfn) == slot->number) | |
944 | dev->slot = slot; | |
945 | ||
946 | /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer) | |
947 | set this higher, assuming the system even supports it. */ | |
948 | dev->dma_mask = 0xffffffff; | |
1da177e4 | 949 | |
eebfcfb5 GKH |
950 | dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus), |
951 | dev->bus->number, PCI_SLOT(dev->devfn), | |
952 | PCI_FUNC(dev->devfn)); | |
1da177e4 LT |
953 | |
954 | pci_read_config_dword(dev, PCI_CLASS_REVISION, &class); | |
b8a3a521 | 955 | dev->revision = class & 0xff; |
2dd8ba92 | 956 | dev->class = class >> 8; /* upper 3 bytes */ |
1da177e4 | 957 | |
2dd8ba92 YL |
958 | dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n", |
959 | dev->vendor, dev->device, dev->hdr_type, dev->class); | |
1da177e4 | 960 | |
853346e4 YZ |
961 | /* need to have dev->class ready */ |
962 | dev->cfg_size = pci_cfg_space_size(dev); | |
963 | ||
1da177e4 | 964 | /* "Unknown power state" */ |
3fe9d19f | 965 | dev->current_state = PCI_UNKNOWN; |
1da177e4 LT |
966 | |
967 | /* Early fixups, before probing the BARs */ | |
968 | pci_fixup_device(pci_fixup_early, dev); | |
f79b1b14 YZ |
969 | /* device class may be changed after fixup */ |
970 | class = dev->class >> 8; | |
1da177e4 LT |
971 | |
972 | switch (dev->hdr_type) { /* header type */ | |
973 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ | |
974 | if (class == PCI_CLASS_BRIDGE_PCI) | |
975 | goto bad; | |
976 | pci_read_irq(dev); | |
977 | pci_read_bases(dev, 6, PCI_ROM_ADDRESS); | |
978 | pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor); | |
979 | pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device); | |
368c73d4 AC |
980 | |
981 | /* | |
982 | * Do the ugly legacy mode stuff here rather than broken chip | |
983 | * quirk code. Legacy mode ATA controllers have fixed | |
984 | * addresses. These are not always echoed in BAR0-3, and | |
985 | * BAR0-3 in a few cases contain junk! | |
986 | */ | |
987 | if (class == PCI_CLASS_STORAGE_IDE) { | |
988 | u8 progif; | |
989 | pci_read_config_byte(dev, PCI_CLASS_PROG, &progif); | |
990 | if ((progif & 1) == 0) { | |
5bfa14ed BH |
991 | region.start = 0x1F0; |
992 | region.end = 0x1F7; | |
993 | res = &dev->resource[0]; | |
994 | res->flags = LEGACY_IO_RESOURCE; | |
fb127cb9 | 995 | pcibios_bus_to_resource(dev, res, ®ion); |
5bfa14ed BH |
996 | region.start = 0x3F6; |
997 | region.end = 0x3F6; | |
998 | res = &dev->resource[1]; | |
999 | res->flags = LEGACY_IO_RESOURCE; | |
fb127cb9 | 1000 | pcibios_bus_to_resource(dev, res, ®ion); |
368c73d4 AC |
1001 | } |
1002 | if ((progif & 4) == 0) { | |
5bfa14ed BH |
1003 | region.start = 0x170; |
1004 | region.end = 0x177; | |
1005 | res = &dev->resource[2]; | |
1006 | res->flags = LEGACY_IO_RESOURCE; | |
fb127cb9 | 1007 | pcibios_bus_to_resource(dev, res, ®ion); |
5bfa14ed BH |
1008 | region.start = 0x376; |
1009 | region.end = 0x376; | |
1010 | res = &dev->resource[3]; | |
1011 | res->flags = LEGACY_IO_RESOURCE; | |
fb127cb9 | 1012 | pcibios_bus_to_resource(dev, res, ®ion); |
368c73d4 AC |
1013 | } |
1014 | } | |
1da177e4 LT |
1015 | break; |
1016 | ||
1017 | case PCI_HEADER_TYPE_BRIDGE: /* bridge header */ | |
1018 | if (class != PCI_CLASS_BRIDGE_PCI) | |
1019 | goto bad; | |
1020 | /* The PCI-to-PCI bridge spec requires that subtractive | |
1021 | decoding (i.e. transparent) bridge must have programming | |
1022 | interface code of 0x01. */ | |
3efd273b | 1023 | pci_read_irq(dev); |
1da177e4 LT |
1024 | dev->transparent = ((dev->class & 0xff) == 1); |
1025 | pci_read_bases(dev, 2, PCI_ROM_ADDRESS1); | |
28760489 | 1026 | set_pcie_hotplug_bridge(dev); |
bc577d2b GB |
1027 | pos = pci_find_capability(dev, PCI_CAP_ID_SSVID); |
1028 | if (pos) { | |
1029 | pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor); | |
1030 | pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device); | |
1031 | } | |
1da177e4 LT |
1032 | break; |
1033 | ||
1034 | case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */ | |
1035 | if (class != PCI_CLASS_BRIDGE_CARDBUS) | |
1036 | goto bad; | |
1037 | pci_read_irq(dev); | |
1038 | pci_read_bases(dev, 1, 0); | |
1039 | pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor); | |
1040 | pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device); | |
1041 | break; | |
1042 | ||
1043 | default: /* unknown header */ | |
80ccba11 BH |
1044 | dev_err(&dev->dev, "unknown header type %02x, " |
1045 | "ignoring device\n", dev->hdr_type); | |
480b93b7 | 1046 | return -EIO; |
1da177e4 LT |
1047 | |
1048 | bad: | |
2dd8ba92 YL |
1049 | dev_err(&dev->dev, "ignoring class %#08x (doesn't match header " |
1050 | "type %02x)\n", dev->class, dev->hdr_type); | |
1da177e4 LT |
1051 | dev->class = PCI_CLASS_NOT_DEFINED; |
1052 | } | |
1053 | ||
1054 | /* We found a fine healthy device, go go go... */ | |
1055 | return 0; | |
1056 | } | |
1057 | ||
201de56e ZY |
1058 | static void pci_release_capabilities(struct pci_dev *dev) |
1059 | { | |
1060 | pci_vpd_release(dev); | |
d1b054da | 1061 | pci_iov_release(dev); |
f796841e | 1062 | pci_free_cap_save_buffers(dev); |
201de56e ZY |
1063 | } |
1064 | ||
1da177e4 LT |
1065 | /** |
1066 | * pci_release_dev - free a pci device structure when all users of it are finished. | |
1067 | * @dev: device that's been disconnected | |
1068 | * | |
1069 | * Will be called only by the device core when all users of this pci device are | |
1070 | * done. | |
1071 | */ | |
1072 | static void pci_release_dev(struct device *dev) | |
1073 | { | |
1074 | struct pci_dev *pci_dev; | |
1075 | ||
1076 | pci_dev = to_pci_dev(dev); | |
201de56e | 1077 | pci_release_capabilities(pci_dev); |
98d9f30c | 1078 | pci_release_of_node(pci_dev); |
1da177e4 LT |
1079 | kfree(pci_dev); |
1080 | } | |
1081 | ||
1082 | /** | |
1083 | * pci_cfg_space_size - get the configuration space size of the PCI device. | |
8f7020d3 | 1084 | * @dev: PCI device |
1da177e4 LT |
1085 | * |
1086 | * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices | |
1087 | * have 4096 bytes. Even if the device is capable, that doesn't mean we can | |
1088 | * access it. Maybe we don't have a way to generate extended config space | |
1089 | * accesses, or the device is behind a reverse Express bridge. So we try | |
1090 | * reading the dword at 0x100 which must either be 0 or a valid extended | |
1091 | * capability header. | |
1092 | */ | |
70b9f7dc | 1093 | int pci_cfg_space_size_ext(struct pci_dev *dev) |
1da177e4 | 1094 | { |
1da177e4 | 1095 | u32 status; |
557848c3 | 1096 | int pos = PCI_CFG_SPACE_SIZE; |
1da177e4 | 1097 | |
557848c3 | 1098 | if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL) |
70b9f7dc YL |
1099 | goto fail; |
1100 | if (status == 0xffffffff) | |
1101 | goto fail; | |
1102 | ||
1103 | return PCI_CFG_SPACE_EXP_SIZE; | |
1104 | ||
1105 | fail: | |
1106 | return PCI_CFG_SPACE_SIZE; | |
1107 | } | |
1108 | ||
1109 | int pci_cfg_space_size(struct pci_dev *dev) | |
1110 | { | |
1111 | int pos; | |
1112 | u32 status; | |
dfadd9ed YL |
1113 | u16 class; |
1114 | ||
1115 | class = dev->class >> 8; | |
1116 | if (class == PCI_CLASS_BRIDGE_HOST) | |
1117 | return pci_cfg_space_size_ext(dev); | |
57741a77 | 1118 | |
06a1cbaf | 1119 | pos = pci_pcie_cap(dev); |
1da177e4 LT |
1120 | if (!pos) { |
1121 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); | |
1122 | if (!pos) | |
1123 | goto fail; | |
1124 | ||
1125 | pci_read_config_dword(dev, pos + PCI_X_STATUS, &status); | |
1126 | if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))) | |
1127 | goto fail; | |
1128 | } | |
1129 | ||
70b9f7dc | 1130 | return pci_cfg_space_size_ext(dev); |
1da177e4 LT |
1131 | |
1132 | fail: | |
1133 | return PCI_CFG_SPACE_SIZE; | |
1134 | } | |
1135 | ||
1136 | static void pci_release_bus_bridge_dev(struct device *dev) | |
1137 | { | |
7b543663 YL |
1138 | struct pci_host_bridge *bridge = to_pci_host_bridge(dev); |
1139 | ||
1140 | /* TODO: need to free window->res */ | |
1141 | ||
1142 | pci_free_resource_list(&bridge->windows); | |
1143 | ||
1144 | kfree(bridge); | |
1da177e4 LT |
1145 | } |
1146 | ||
65891215 ME |
1147 | struct pci_dev *alloc_pci_dev(void) |
1148 | { | |
1149 | struct pci_dev *dev; | |
1150 | ||
1151 | dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL); | |
1152 | if (!dev) | |
1153 | return NULL; | |
1154 | ||
65891215 ME |
1155 | INIT_LIST_HEAD(&dev->bus_list); |
1156 | ||
1157 | return dev; | |
1158 | } | |
1159 | EXPORT_SYMBOL(alloc_pci_dev); | |
1160 | ||
efdc87da YL |
1161 | bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l, |
1162 | int crs_timeout) | |
1da177e4 | 1163 | { |
1da177e4 LT |
1164 | int delay = 1; |
1165 | ||
efdc87da YL |
1166 | if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l)) |
1167 | return false; | |
1da177e4 LT |
1168 | |
1169 | /* some broken boards return 0 or ~0 if a slot is empty: */ | |
efdc87da YL |
1170 | if (*l == 0xffffffff || *l == 0x00000000 || |
1171 | *l == 0x0000ffff || *l == 0xffff0000) | |
1172 | return false; | |
1da177e4 LT |
1173 | |
1174 | /* Configuration request Retry Status */ | |
efdc87da YL |
1175 | while (*l == 0xffff0001) { |
1176 | if (!crs_timeout) | |
1177 | return false; | |
1178 | ||
1da177e4 LT |
1179 | msleep(delay); |
1180 | delay *= 2; | |
efdc87da YL |
1181 | if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l)) |
1182 | return false; | |
1da177e4 | 1183 | /* Card hasn't responded in 60 seconds? Must be stuck. */ |
efdc87da | 1184 | if (delay > crs_timeout) { |
80ccba11 | 1185 | printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not " |
1da177e4 LT |
1186 | "responding\n", pci_domain_nr(bus), |
1187 | bus->number, PCI_SLOT(devfn), | |
1188 | PCI_FUNC(devfn)); | |
efdc87da | 1189 | return false; |
1da177e4 LT |
1190 | } |
1191 | } | |
1192 | ||
efdc87da YL |
1193 | return true; |
1194 | } | |
1195 | EXPORT_SYMBOL(pci_bus_read_dev_vendor_id); | |
1196 | ||
1197 | /* | |
1198 | * Read the config data for a PCI device, sanity-check it | |
1199 | * and fill in the dev structure... | |
1200 | */ | |
1201 | static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn) | |
1202 | { | |
1203 | struct pci_dev *dev; | |
1204 | u32 l; | |
1205 | ||
1206 | if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000)) | |
1207 | return NULL; | |
1208 | ||
bab41e9b | 1209 | dev = alloc_pci_dev(); |
1da177e4 LT |
1210 | if (!dev) |
1211 | return NULL; | |
1212 | ||
1da177e4 | 1213 | dev->bus = bus; |
1da177e4 | 1214 | dev->devfn = devfn; |
1da177e4 LT |
1215 | dev->vendor = l & 0xffff; |
1216 | dev->device = (l >> 16) & 0xffff; | |
cef354db | 1217 | |
98d9f30c BH |
1218 | pci_set_of_node(dev); |
1219 | ||
480b93b7 | 1220 | if (pci_setup_device(dev)) { |
1da177e4 LT |
1221 | kfree(dev); |
1222 | return NULL; | |
1223 | } | |
1da177e4 LT |
1224 | |
1225 | return dev; | |
1226 | } | |
1227 | ||
201de56e ZY |
1228 | static void pci_init_capabilities(struct pci_dev *dev) |
1229 | { | |
1230 | /* MSI/MSI-X list */ | |
1231 | pci_msi_init_pci_dev(dev); | |
1232 | ||
63f4898a RW |
1233 | /* Buffers for saving PCIe and PCI-X capabilities */ |
1234 | pci_allocate_cap_save_buffers(dev); | |
1235 | ||
201de56e ZY |
1236 | /* Power Management */ |
1237 | pci_pm_init(dev); | |
eb9c39d0 | 1238 | platform_pci_wakeup_init(dev); |
201de56e ZY |
1239 | |
1240 | /* Vital Product Data */ | |
1241 | pci_vpd_pci22_init(dev); | |
58c3a727 YZ |
1242 | |
1243 | /* Alternative Routing-ID Forwarding */ | |
1244 | pci_enable_ari(dev); | |
d1b054da YZ |
1245 | |
1246 | /* Single Root I/O Virtualization */ | |
1247 | pci_iov_init(dev); | |
ae21ee65 AK |
1248 | |
1249 | /* Enable ACS P2P upstream forwarding */ | |
5d990b62 | 1250 | pci_enable_acs(dev); |
201de56e ZY |
1251 | } |
1252 | ||
96bde06a | 1253 | void pci_device_add(struct pci_dev *dev, struct pci_bus *bus) |
1da177e4 | 1254 | { |
cdb9b9f7 PM |
1255 | device_initialize(&dev->dev); |
1256 | dev->dev.release = pci_release_dev; | |
1257 | pci_dev_get(dev); | |
1da177e4 | 1258 | |
cdb9b9f7 | 1259 | dev->dev.dma_mask = &dev->dma_mask; |
4d57cdfa | 1260 | dev->dev.dma_parms = &dev->dma_parms; |
cdb9b9f7 | 1261 | dev->dev.coherent_dma_mask = 0xffffffffull; |
1da177e4 | 1262 | |
4d57cdfa | 1263 | pci_set_dma_max_seg_size(dev, 65536); |
59fc67de | 1264 | pci_set_dma_seg_boundary(dev, 0xffffffff); |
4d57cdfa | 1265 | |
1da177e4 LT |
1266 | /* Fix up broken headers */ |
1267 | pci_fixup_device(pci_fixup_header, dev); | |
1268 | ||
2069ecfb YL |
1269 | /* moved out from quirk header fixup code */ |
1270 | pci_reassigndev_resource_alignment(dev); | |
1271 | ||
4b77b0a2 RW |
1272 | /* Clear the state_saved flag. */ |
1273 | dev->state_saved = false; | |
1274 | ||
201de56e ZY |
1275 | /* Initialize various capabilities */ |
1276 | pci_init_capabilities(dev); | |
eb9d0fe4 | 1277 | |
1da177e4 LT |
1278 | /* |
1279 | * Add the device to our list of discovered devices | |
1280 | * and the bus list for fixup functions, etc. | |
1281 | */ | |
d71374da | 1282 | down_write(&pci_bus_sem); |
1da177e4 | 1283 | list_add_tail(&dev->bus_list, &bus->devices); |
d71374da | 1284 | up_write(&pci_bus_sem); |
cdb9b9f7 PM |
1285 | } |
1286 | ||
451124a7 | 1287 | struct pci_dev *__ref pci_scan_single_device(struct pci_bus *bus, int devfn) |
cdb9b9f7 PM |
1288 | { |
1289 | struct pci_dev *dev; | |
1290 | ||
90bdb311 TP |
1291 | dev = pci_get_slot(bus, devfn); |
1292 | if (dev) { | |
1293 | pci_dev_put(dev); | |
1294 | return dev; | |
1295 | } | |
1296 | ||
cdb9b9f7 PM |
1297 | dev = pci_scan_device(bus, devfn); |
1298 | if (!dev) | |
1299 | return NULL; | |
1300 | ||
1301 | pci_device_add(dev, bus); | |
1da177e4 LT |
1302 | |
1303 | return dev; | |
1304 | } | |
b73e9687 | 1305 | EXPORT_SYMBOL(pci_scan_single_device); |
1da177e4 | 1306 | |
f07852d6 MW |
1307 | static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn) |
1308 | { | |
1309 | u16 cap; | |
4fb88c1a MW |
1310 | unsigned pos, next_fn; |
1311 | ||
1312 | if (!dev) | |
1313 | return 0; | |
1314 | ||
1315 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI); | |
f07852d6 MW |
1316 | if (!pos) |
1317 | return 0; | |
1318 | pci_read_config_word(dev, pos + 4, &cap); | |
4fb88c1a MW |
1319 | next_fn = cap >> 8; |
1320 | if (next_fn <= fn) | |
1321 | return 0; | |
1322 | return next_fn; | |
f07852d6 MW |
1323 | } |
1324 | ||
1325 | static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn) | |
1326 | { | |
1327 | return (fn + 1) % 8; | |
1328 | } | |
1329 | ||
1330 | static unsigned no_next_fn(struct pci_dev *dev, unsigned fn) | |
1331 | { | |
1332 | return 0; | |
1333 | } | |
1334 | ||
1335 | static int only_one_child(struct pci_bus *bus) | |
1336 | { | |
1337 | struct pci_dev *parent = bus->self; | |
1338 | if (!parent || !pci_is_pcie(parent)) | |
1339 | return 0; | |
1340 | if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT || | |
1341 | parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) | |
1342 | return 1; | |
1343 | return 0; | |
1344 | } | |
1345 | ||
1da177e4 LT |
1346 | /** |
1347 | * pci_scan_slot - scan a PCI slot on a bus for devices. | |
1348 | * @bus: PCI bus to scan | |
1349 | * @devfn: slot number to scan (must have zero function.) | |
1350 | * | |
1351 | * Scan a PCI slot on the specified PCI bus for devices, adding | |
1352 | * discovered devices to the @bus->devices list. New devices | |
8a1bc901 | 1353 | * will not have is_added set. |
1b69dfc6 TP |
1354 | * |
1355 | * Returns the number of new devices found. | |
1da177e4 | 1356 | */ |
96bde06a | 1357 | int pci_scan_slot(struct pci_bus *bus, int devfn) |
1da177e4 | 1358 | { |
f07852d6 | 1359 | unsigned fn, nr = 0; |
1b69dfc6 | 1360 | struct pci_dev *dev; |
f07852d6 MW |
1361 | unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn; |
1362 | ||
1363 | if (only_one_child(bus) && (devfn > 0)) | |
1364 | return 0; /* Already scanned the entire slot */ | |
1da177e4 | 1365 | |
1b69dfc6 | 1366 | dev = pci_scan_single_device(bus, devfn); |
4fb88c1a MW |
1367 | if (!dev) |
1368 | return 0; | |
1369 | if (!dev->is_added) | |
1b69dfc6 TP |
1370 | nr++; |
1371 | ||
f07852d6 MW |
1372 | if (pci_ari_enabled(bus)) |
1373 | next_fn = next_ari_fn; | |
4fb88c1a | 1374 | else if (dev->multifunction) |
f07852d6 MW |
1375 | next_fn = next_trad_fn; |
1376 | ||
1377 | for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) { | |
1378 | dev = pci_scan_single_device(bus, devfn + fn); | |
1379 | if (dev) { | |
1380 | if (!dev->is_added) | |
1381 | nr++; | |
1382 | dev->multifunction = 1; | |
1da177e4 LT |
1383 | } |
1384 | } | |
7d715a6c | 1385 | |
149e1637 SL |
1386 | /* only one slot has pcie device */ |
1387 | if (bus->self && nr) | |
7d715a6c SL |
1388 | pcie_aspm_init_link_state(bus->self); |
1389 | ||
1da177e4 LT |
1390 | return nr; |
1391 | } | |
1392 | ||
b03e7495 JM |
1393 | static int pcie_find_smpss(struct pci_dev *dev, void *data) |
1394 | { | |
1395 | u8 *smpss = data; | |
1396 | ||
1397 | if (!pci_is_pcie(dev)) | |
1398 | return 0; | |
1399 | ||
1400 | /* For PCIE hotplug enabled slots not connected directly to a | |
1401 | * PCI-E root port, there can be problems when hotplugging | |
1402 | * devices. This is due to the possibility of hotplugging a | |
1403 | * device into the fabric with a smaller MPS that the devices | |
1404 | * currently running have configured. Modifying the MPS on the | |
1405 | * running devices could cause a fatal bus error due to an | |
1406 | * incoming frame being larger than the newly configured MPS. | |
1407 | * To work around this, the MPS for the entire fabric must be | |
1408 | * set to the minimum size. Any devices hotplugged into this | |
1409 | * fabric will have the minimum MPS set. If the PCI hotplug | |
1410 | * slot is directly connected to the root port and there are not | |
1411 | * other devices on the fabric (which seems to be the most | |
1412 | * common case), then this is not an issue and MPS discovery | |
1413 | * will occur as normal. | |
1414 | */ | |
1415 | if (dev->is_hotplug_bridge && (!list_is_singular(&dev->bus->devices) || | |
1a4b1a41 BH |
1416 | (dev->bus->self && |
1417 | dev->bus->self->pcie_type != PCI_EXP_TYPE_ROOT_PORT))) | |
b03e7495 JM |
1418 | *smpss = 0; |
1419 | ||
1420 | if (*smpss > dev->pcie_mpss) | |
1421 | *smpss = dev->pcie_mpss; | |
1422 | ||
1423 | return 0; | |
1424 | } | |
1425 | ||
1426 | static void pcie_write_mps(struct pci_dev *dev, int mps) | |
1427 | { | |
62f392ea | 1428 | int rc; |
b03e7495 JM |
1429 | |
1430 | if (pcie_bus_config == PCIE_BUS_PERFORMANCE) { | |
62f392ea | 1431 | mps = 128 << dev->pcie_mpss; |
b03e7495 | 1432 | |
62f392ea JM |
1433 | if (dev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && dev->bus->self) |
1434 | /* For "Performance", the assumption is made that | |
b03e7495 JM |
1435 | * downstream communication will never be larger than |
1436 | * the MRRS. So, the MPS only needs to be configured | |
1437 | * for the upstream communication. This being the case, | |
1438 | * walk from the top down and set the MPS of the child | |
1439 | * to that of the parent bus. | |
62f392ea JM |
1440 | * |
1441 | * Configure the device MPS with the smaller of the | |
1442 | * device MPSS or the bridge MPS (which is assumed to be | |
1443 | * properly configured at this point to the largest | |
1444 | * allowable MPS based on its parent bus). | |
b03e7495 | 1445 | */ |
62f392ea | 1446 | mps = min(mps, pcie_get_mps(dev->bus->self)); |
b03e7495 JM |
1447 | } |
1448 | ||
1449 | rc = pcie_set_mps(dev, mps); | |
1450 | if (rc) | |
1451 | dev_err(&dev->dev, "Failed attempting to set the MPS\n"); | |
1452 | } | |
1453 | ||
62f392ea | 1454 | static void pcie_write_mrrs(struct pci_dev *dev) |
b03e7495 | 1455 | { |
62f392ea | 1456 | int rc, mrrs; |
b03e7495 | 1457 | |
ed2888e9 JM |
1458 | /* In the "safe" case, do not configure the MRRS. There appear to be |
1459 | * issues with setting MRRS to 0 on a number of devices. | |
1460 | */ | |
ed2888e9 JM |
1461 | if (pcie_bus_config != PCIE_BUS_PERFORMANCE) |
1462 | return; | |
1463 | ||
ed2888e9 JM |
1464 | /* For Max performance, the MRRS must be set to the largest supported |
1465 | * value. However, it cannot be configured larger than the MPS the | |
62f392ea JM |
1466 | * device or the bus can support. This should already be properly |
1467 | * configured by a prior call to pcie_write_mps. | |
ed2888e9 | 1468 | */ |
62f392ea | 1469 | mrrs = pcie_get_mps(dev); |
b03e7495 JM |
1470 | |
1471 | /* MRRS is a R/W register. Invalid values can be written, but a | |
ed2888e9 | 1472 | * subsequent read will verify if the value is acceptable or not. |
b03e7495 JM |
1473 | * If the MRRS value provided is not acceptable (e.g., too large), |
1474 | * shrink the value until it is acceptable to the HW. | |
1475 | */ | |
1476 | while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) { | |
1477 | rc = pcie_set_readrq(dev, mrrs); | |
62f392ea JM |
1478 | if (!rc) |
1479 | break; | |
b03e7495 | 1480 | |
62f392ea | 1481 | dev_warn(&dev->dev, "Failed attempting to set the MRRS\n"); |
b03e7495 JM |
1482 | mrrs /= 2; |
1483 | } | |
62f392ea JM |
1484 | |
1485 | if (mrrs < 128) | |
1486 | dev_err(&dev->dev, "MRRS was unable to be configured with a " | |
1487 | "safe value. If problems are experienced, try running " | |
1488 | "with pci=pcie_bus_safe.\n"); | |
b03e7495 JM |
1489 | } |
1490 | ||
1491 | static int pcie_bus_configure_set(struct pci_dev *dev, void *data) | |
1492 | { | |
a513a99a | 1493 | int mps, orig_mps; |
b03e7495 JM |
1494 | |
1495 | if (!pci_is_pcie(dev)) | |
1496 | return 0; | |
1497 | ||
a513a99a JM |
1498 | mps = 128 << *(u8 *)data; |
1499 | orig_mps = pcie_get_mps(dev); | |
b03e7495 JM |
1500 | |
1501 | pcie_write_mps(dev, mps); | |
62f392ea | 1502 | pcie_write_mrrs(dev); |
b03e7495 | 1503 | |
a513a99a JM |
1504 | dev_info(&dev->dev, "PCI-E Max Payload Size set to %4d/%4d (was %4d), " |
1505 | "Max Read Rq %4d\n", pcie_get_mps(dev), 128 << dev->pcie_mpss, | |
1506 | orig_mps, pcie_get_readrq(dev)); | |
b03e7495 JM |
1507 | |
1508 | return 0; | |
1509 | } | |
1510 | ||
a513a99a | 1511 | /* pcie_bus_configure_settings requires that pci_walk_bus work in a top-down, |
b03e7495 JM |
1512 | * parents then children fashion. If this changes, then this code will not |
1513 | * work as designed. | |
1514 | */ | |
1515 | void pcie_bus_configure_settings(struct pci_bus *bus, u8 mpss) | |
1516 | { | |
5f39e670 | 1517 | u8 smpss; |
b03e7495 | 1518 | |
b03e7495 JM |
1519 | if (!pci_is_pcie(bus->self)) |
1520 | return; | |
1521 | ||
5f39e670 JM |
1522 | if (pcie_bus_config == PCIE_BUS_TUNE_OFF) |
1523 | return; | |
1524 | ||
1525 | /* FIXME - Peer to peer DMA is possible, though the endpoint would need | |
1526 | * to be aware to the MPS of the destination. To work around this, | |
1527 | * simply force the MPS of the entire system to the smallest possible. | |
1528 | */ | |
1529 | if (pcie_bus_config == PCIE_BUS_PEER2PEER) | |
1530 | smpss = 0; | |
1531 | ||
b03e7495 | 1532 | if (pcie_bus_config == PCIE_BUS_SAFE) { |
5f39e670 JM |
1533 | smpss = mpss; |
1534 | ||
b03e7495 JM |
1535 | pcie_find_smpss(bus->self, &smpss); |
1536 | pci_walk_bus(bus, pcie_find_smpss, &smpss); | |
1537 | } | |
1538 | ||
1539 | pcie_bus_configure_set(bus->self, &smpss); | |
1540 | pci_walk_bus(bus, pcie_bus_configure_set, &smpss); | |
1541 | } | |
debc3b77 | 1542 | EXPORT_SYMBOL_GPL(pcie_bus_configure_settings); |
b03e7495 | 1543 | |
0ab2b57f | 1544 | unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus) |
1da177e4 LT |
1545 | { |
1546 | unsigned int devfn, pass, max = bus->secondary; | |
1547 | struct pci_dev *dev; | |
1548 | ||
0207c356 | 1549 | dev_dbg(&bus->dev, "scanning bus\n"); |
1da177e4 LT |
1550 | |
1551 | /* Go find them, Rover! */ | |
1552 | for (devfn = 0; devfn < 0x100; devfn += 8) | |
1553 | pci_scan_slot(bus, devfn); | |
1554 | ||
a28724b0 YZ |
1555 | /* Reserve buses for SR-IOV capability. */ |
1556 | max += pci_iov_bus_range(bus); | |
1557 | ||
1da177e4 LT |
1558 | /* |
1559 | * After performing arch-dependent fixup of the bus, look behind | |
1560 | * all PCI-to-PCI bridges on this bus. | |
1561 | */ | |
74710ded | 1562 | if (!bus->is_added) { |
0207c356 | 1563 | dev_dbg(&bus->dev, "fixups for bus\n"); |
74710ded AC |
1564 | pcibios_fixup_bus(bus); |
1565 | if (pci_is_root_bus(bus)) | |
1566 | bus->is_added = 1; | |
1567 | } | |
1568 | ||
1da177e4 LT |
1569 | for (pass=0; pass < 2; pass++) |
1570 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
1571 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || | |
1572 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) | |
1573 | max = pci_scan_bridge(bus, dev, max, pass); | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * We've scanned the bus and so we know all about what's on | |
1578 | * the other side of any bridges that may be on this bus plus | |
1579 | * any devices. | |
1580 | * | |
1581 | * Return how far we've got finding sub-buses. | |
1582 | */ | |
0207c356 | 1583 | dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max); |
1da177e4 LT |
1584 | return max; |
1585 | } | |
1586 | ||
166c6370 BH |
1587 | struct pci_bus *pci_create_root_bus(struct device *parent, int bus, |
1588 | struct pci_ops *ops, void *sysdata, struct list_head *resources) | |
1da177e4 | 1589 | { |
0efd5aab | 1590 | int error; |
5a21d70d | 1591 | struct pci_host_bridge *bridge; |
0207c356 | 1592 | struct pci_bus *b, *b2; |
0efd5aab | 1593 | struct pci_host_bridge_window *window, *n; |
a9d9f527 | 1594 | struct resource *res; |
0efd5aab BH |
1595 | resource_size_t offset; |
1596 | char bus_addr[64]; | |
1597 | char *fmt; | |
1da177e4 | 1598 | |
5a21d70d | 1599 | |
1da177e4 LT |
1600 | b = pci_alloc_bus(); |
1601 | if (!b) | |
7b543663 | 1602 | return NULL; |
1da177e4 LT |
1603 | |
1604 | b->sysdata = sysdata; | |
1605 | b->ops = ops; | |
0207c356 BH |
1606 | b2 = pci_find_bus(pci_domain_nr(b), bus); |
1607 | if (b2) { | |
1da177e4 | 1608 | /* If we already got to this bus through a different bridge, ignore it */ |
0207c356 | 1609 | dev_dbg(&b2->dev, "bus already known\n"); |
1da177e4 LT |
1610 | goto err_out; |
1611 | } | |
d71374da | 1612 | |
7b543663 YL |
1613 | bridge = pci_alloc_host_bridge(b); |
1614 | if (!bridge) | |
1615 | goto err_out; | |
1616 | ||
1617 | bridge->dev.parent = parent; | |
1618 | bridge->dev.release = pci_release_bus_bridge_dev; | |
1619 | dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(b), bus); | |
1620 | error = device_register(&bridge->dev); | |
1da177e4 | 1621 | if (error) |
7b543663 YL |
1622 | goto bridge_dev_reg_err; |
1623 | b->bridge = get_device(&bridge->dev); | |
a1e4d72c | 1624 | device_enable_async_suspend(b->bridge); |
98d9f30c | 1625 | pci_set_bus_of_node(b); |
1da177e4 | 1626 | |
0d358f22 YL |
1627 | if (!parent) |
1628 | set_dev_node(b->bridge, pcibus_to_node(b)); | |
1629 | ||
fd7d1ced GKH |
1630 | b->dev.class = &pcibus_class; |
1631 | b->dev.parent = b->bridge; | |
1a927133 | 1632 | dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus); |
fd7d1ced | 1633 | error = device_register(&b->dev); |
1da177e4 LT |
1634 | if (error) |
1635 | goto class_dev_reg_err; | |
1da177e4 LT |
1636 | |
1637 | /* Create legacy_io and legacy_mem files for this bus */ | |
1638 | pci_create_legacy_files(b); | |
1639 | ||
1da177e4 | 1640 | b->number = b->secondary = bus; |
166c6370 | 1641 | |
a9d9f527 BH |
1642 | if (parent) |
1643 | dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev)); | |
1644 | else | |
1645 | printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev)); | |
1646 | ||
0efd5aab BH |
1647 | /* Add initial resources to the bus */ |
1648 | list_for_each_entry_safe(window, n, resources, list) { | |
1649 | list_move_tail(&window->list, &bridge->windows); | |
1650 | res = window->res; | |
1651 | offset = window->offset; | |
1652 | pci_bus_add_resource(b, res, 0); | |
1653 | if (offset) { | |
1654 | if (resource_type(res) == IORESOURCE_IO) | |
1655 | fmt = " (bus address [%#06llx-%#06llx])"; | |
1656 | else | |
1657 | fmt = " (bus address [%#010llx-%#010llx])"; | |
1658 | snprintf(bus_addr, sizeof(bus_addr), fmt, | |
1659 | (unsigned long long) (res->start - offset), | |
1660 | (unsigned long long) (res->end - offset)); | |
1661 | } else | |
1662 | bus_addr[0] = '\0'; | |
1663 | dev_info(&b->dev, "root bus resource %pR%s\n", res, bus_addr); | |
a9d9f527 BH |
1664 | } |
1665 | ||
a5390aa6 BH |
1666 | down_write(&pci_bus_sem); |
1667 | list_add_tail(&b->node, &pci_root_buses); | |
1668 | up_write(&pci_bus_sem); | |
1669 | ||
1da177e4 LT |
1670 | return b; |
1671 | ||
1da177e4 | 1672 | class_dev_reg_err: |
7b543663 YL |
1673 | put_device(&bridge->dev); |
1674 | device_unregister(&bridge->dev); | |
1675 | bridge_dev_reg_err: | |
1676 | kfree(bridge); | |
1da177e4 | 1677 | err_out: |
1da177e4 LT |
1678 | kfree(b); |
1679 | return NULL; | |
1680 | } | |
cdb9b9f7 | 1681 | |
a2ebb827 BH |
1682 | struct pci_bus * __devinit pci_scan_root_bus(struct device *parent, int bus, |
1683 | struct pci_ops *ops, void *sysdata, struct list_head *resources) | |
1684 | { | |
1685 | struct pci_bus *b; | |
1686 | ||
1687 | b = pci_create_root_bus(parent, bus, ops, sysdata, resources); | |
1688 | if (!b) | |
1689 | return NULL; | |
1690 | ||
1691 | b->subordinate = pci_scan_child_bus(b); | |
1692 | pci_bus_add_devices(b); | |
1693 | return b; | |
1694 | } | |
1695 | EXPORT_SYMBOL(pci_scan_root_bus); | |
1696 | ||
7e00fe2e | 1697 | /* Deprecated; use pci_scan_root_bus() instead */ |
0ab2b57f | 1698 | struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent, |
cdb9b9f7 PM |
1699 | int bus, struct pci_ops *ops, void *sysdata) |
1700 | { | |
1e39ae9f | 1701 | LIST_HEAD(resources); |
cdb9b9f7 PM |
1702 | struct pci_bus *b; |
1703 | ||
1e39ae9f BH |
1704 | pci_add_resource(&resources, &ioport_resource); |
1705 | pci_add_resource(&resources, &iomem_resource); | |
1706 | b = pci_create_root_bus(parent, bus, ops, sysdata, &resources); | |
cdb9b9f7 PM |
1707 | if (b) |
1708 | b->subordinate = pci_scan_child_bus(b); | |
1e39ae9f BH |
1709 | else |
1710 | pci_free_resource_list(&resources); | |
cdb9b9f7 PM |
1711 | return b; |
1712 | } | |
1da177e4 LT |
1713 | EXPORT_SYMBOL(pci_scan_bus_parented); |
1714 | ||
de4b2f76 BH |
1715 | struct pci_bus * __devinit pci_scan_bus(int bus, struct pci_ops *ops, |
1716 | void *sysdata) | |
1717 | { | |
1718 | LIST_HEAD(resources); | |
1719 | struct pci_bus *b; | |
1720 | ||
1721 | pci_add_resource(&resources, &ioport_resource); | |
1722 | pci_add_resource(&resources, &iomem_resource); | |
1723 | b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources); | |
1724 | if (b) { | |
1725 | b->subordinate = pci_scan_child_bus(b); | |
1726 | pci_bus_add_devices(b); | |
1727 | } else { | |
1728 | pci_free_resource_list(&resources); | |
1729 | } | |
1730 | return b; | |
1731 | } | |
1732 | EXPORT_SYMBOL(pci_scan_bus); | |
1733 | ||
1da177e4 | 1734 | #ifdef CONFIG_HOTPLUG |
2f320521 YL |
1735 | /** |
1736 | * pci_rescan_bus_bridge_resize - scan a PCI bus for devices. | |
1737 | * @bridge: PCI bridge for the bus to scan | |
1738 | * | |
1739 | * Scan a PCI bus and child buses for new devices, add them, | |
1740 | * and enable them, resizing bridge mmio/io resource if necessary | |
1741 | * and possible. The caller must ensure the child devices are already | |
1742 | * removed for resizing to occur. | |
1743 | * | |
1744 | * Returns the max number of subordinate bus discovered. | |
1745 | */ | |
1746 | unsigned int __ref pci_rescan_bus_bridge_resize(struct pci_dev *bridge) | |
1747 | { | |
1748 | unsigned int max; | |
1749 | struct pci_bus *bus = bridge->subordinate; | |
1750 | ||
1751 | max = pci_scan_child_bus(bus); | |
1752 | ||
1753 | pci_assign_unassigned_bridge_resources(bridge); | |
1754 | ||
1755 | pci_bus_add_devices(bus); | |
1756 | ||
1757 | return max; | |
1758 | } | |
1759 | ||
1da177e4 | 1760 | EXPORT_SYMBOL(pci_add_new_bus); |
1da177e4 LT |
1761 | EXPORT_SYMBOL(pci_scan_slot); |
1762 | EXPORT_SYMBOL(pci_scan_bridge); | |
1da177e4 LT |
1763 | EXPORT_SYMBOL_GPL(pci_scan_child_bus); |
1764 | #endif | |
6b4b78fe | 1765 | |
99178b03 | 1766 | static int __init pci_sort_bf_cmp(const struct device *d_a, const struct device *d_b) |
6b4b78fe | 1767 | { |
99178b03 GKH |
1768 | const struct pci_dev *a = to_pci_dev(d_a); |
1769 | const struct pci_dev *b = to_pci_dev(d_b); | |
1770 | ||
6b4b78fe MD |
1771 | if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1; |
1772 | else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1; | |
1773 | ||
1774 | if (a->bus->number < b->bus->number) return -1; | |
1775 | else if (a->bus->number > b->bus->number) return 1; | |
1776 | ||
1777 | if (a->devfn < b->devfn) return -1; | |
1778 | else if (a->devfn > b->devfn) return 1; | |
1779 | ||
1780 | return 0; | |
1781 | } | |
1782 | ||
5ff580c1 | 1783 | void __init pci_sort_breadthfirst(void) |
6b4b78fe | 1784 | { |
99178b03 | 1785 | bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp); |
6b4b78fe | 1786 | } |