]>
Commit | Line | Data |
---|---|---|
7328c8f4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
df62ab5e | 3 | * PCI detection and setup code |
1da177e4 LT |
4 | */ |
5 | ||
6 | #include <linux/kernel.h> | |
7 | #include <linux/delay.h> | |
8 | #include <linux/init.h> | |
9 | #include <linux/pci.h> | |
50230713 | 10 | #include <linux/of_device.h> |
de335bb4 | 11 | #include <linux/of_pci.h> |
589fcc23 | 12 | #include <linux/pci_hotplug.h> |
1da177e4 LT |
13 | #include <linux/slab.h> |
14 | #include <linux/module.h> | |
15 | #include <linux/cpumask.h> | |
7d715a6c | 16 | #include <linux/pci-aspm.h> |
b07461a8 | 17 | #include <linux/aer.h> |
29dbe1f0 | 18 | #include <linux/acpi.h> |
690f4304 | 19 | #include <linux/hypervisor.h> |
788858eb | 20 | #include <linux/irqdomain.h> |
d963f651 | 21 | #include <linux/pm_runtime.h> |
bc56b9e0 | 22 | #include "pci.h" |
1da177e4 LT |
23 | |
24 | #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */ | |
25 | #define CARDBUS_RESERVE_BUSNR 3 | |
1da177e4 | 26 | |
0b950f0f | 27 | static struct resource busn_resource = { |
67cdc827 YL |
28 | .name = "PCI busn", |
29 | .start = 0, | |
30 | .end = 255, | |
31 | .flags = IORESOURCE_BUS, | |
32 | }; | |
33 | ||
1da177e4 LT |
34 | /* Ugh. Need to stop exporting this to modules. */ |
35 | LIST_HEAD(pci_root_buses); | |
36 | EXPORT_SYMBOL(pci_root_buses); | |
37 | ||
5cc62c20 YL |
38 | static LIST_HEAD(pci_domain_busn_res_list); |
39 | ||
40 | struct pci_domain_busn_res { | |
41 | struct list_head list; | |
42 | struct resource res; | |
43 | int domain_nr; | |
44 | }; | |
45 | ||
46 | static struct resource *get_pci_domain_busn_res(int domain_nr) | |
47 | { | |
48 | struct pci_domain_busn_res *r; | |
49 | ||
50 | list_for_each_entry(r, &pci_domain_busn_res_list, list) | |
51 | if (r->domain_nr == domain_nr) | |
52 | return &r->res; | |
53 | ||
54 | r = kzalloc(sizeof(*r), GFP_KERNEL); | |
55 | if (!r) | |
56 | return NULL; | |
57 | ||
58 | r->domain_nr = domain_nr; | |
59 | r->res.start = 0; | |
60 | r->res.end = 0xff; | |
61 | r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED; | |
62 | ||
63 | list_add_tail(&r->list, &pci_domain_busn_res_list); | |
64 | ||
65 | return &r->res; | |
66 | } | |
67 | ||
70308923 GKH |
68 | static int find_anything(struct device *dev, void *data) |
69 | { | |
70 | return 1; | |
71 | } | |
1da177e4 | 72 | |
ed4aaadb | 73 | /* |
3e466e2d BH |
74 | * Some device drivers need know if PCI is initiated. |
75 | * Basically, we think PCI is not initiated when there | |
70308923 | 76 | * is no device to be found on the pci_bus_type. |
ed4aaadb ZY |
77 | */ |
78 | int no_pci_devices(void) | |
79 | { | |
70308923 GKH |
80 | struct device *dev; |
81 | int no_devices; | |
ed4aaadb | 82 | |
70308923 GKH |
83 | dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything); |
84 | no_devices = (dev == NULL); | |
85 | put_device(dev); | |
86 | return no_devices; | |
87 | } | |
ed4aaadb ZY |
88 | EXPORT_SYMBOL(no_pci_devices); |
89 | ||
1da177e4 LT |
90 | /* |
91 | * PCI Bus Class | |
92 | */ | |
fd7d1ced | 93 | static void release_pcibus_dev(struct device *dev) |
1da177e4 | 94 | { |
fd7d1ced | 95 | struct pci_bus *pci_bus = to_pci_bus(dev); |
1da177e4 | 96 | |
ff0387c3 | 97 | put_device(pci_bus->bridge); |
2fe2abf8 | 98 | pci_bus_remove_resources(pci_bus); |
98d9f30c | 99 | pci_release_bus_of_node(pci_bus); |
1da177e4 LT |
100 | kfree(pci_bus); |
101 | } | |
102 | ||
103 | static struct class pcibus_class = { | |
104 | .name = "pci_bus", | |
fd7d1ced | 105 | .dev_release = &release_pcibus_dev, |
56039e65 | 106 | .dev_groups = pcibus_groups, |
1da177e4 LT |
107 | }; |
108 | ||
109 | static int __init pcibus_class_init(void) | |
110 | { | |
111 | return class_register(&pcibus_class); | |
112 | } | |
113 | postcore_initcall(pcibus_class_init); | |
114 | ||
6ac665c6 | 115 | static u64 pci_size(u64 base, u64 maxbase, u64 mask) |
1da177e4 | 116 | { |
6ac665c6 | 117 | u64 size = mask & maxbase; /* Find the significant bits */ |
1da177e4 LT |
118 | if (!size) |
119 | return 0; | |
120 | ||
3e466e2d BH |
121 | /* |
122 | * Get the lowest of them to find the decode size, and from that | |
123 | * the extent. | |
124 | */ | |
1da177e4 LT |
125 | size = (size & ~(size-1)) - 1; |
126 | ||
3e466e2d BH |
127 | /* |
128 | * base == maxbase can be valid only if the BAR has already been | |
129 | * programmed with all 1s. | |
130 | */ | |
1da177e4 LT |
131 | if (base == maxbase && ((base | size) & mask) != mask) |
132 | return 0; | |
133 | ||
134 | return size; | |
135 | } | |
136 | ||
28c6821a | 137 | static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar) |
6ac665c6 | 138 | { |
8d6a6a47 | 139 | u32 mem_type; |
28c6821a | 140 | unsigned long flags; |
8d6a6a47 | 141 | |
6ac665c6 | 142 | if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) { |
28c6821a BH |
143 | flags = bar & ~PCI_BASE_ADDRESS_IO_MASK; |
144 | flags |= IORESOURCE_IO; | |
145 | return flags; | |
6ac665c6 | 146 | } |
07eddf3d | 147 | |
28c6821a BH |
148 | flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK; |
149 | flags |= IORESOURCE_MEM; | |
150 | if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH) | |
151 | flags |= IORESOURCE_PREFETCH; | |
07eddf3d | 152 | |
8d6a6a47 BH |
153 | mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK; |
154 | switch (mem_type) { | |
155 | case PCI_BASE_ADDRESS_MEM_TYPE_32: | |
156 | break; | |
157 | case PCI_BASE_ADDRESS_MEM_TYPE_1M: | |
0ff9514b | 158 | /* 1M mem BAR treated as 32-bit BAR */ |
8d6a6a47 BH |
159 | break; |
160 | case PCI_BASE_ADDRESS_MEM_TYPE_64: | |
28c6821a BH |
161 | flags |= IORESOURCE_MEM_64; |
162 | break; | |
8d6a6a47 | 163 | default: |
0ff9514b | 164 | /* mem unknown type treated as 32-bit BAR */ |
8d6a6a47 BH |
165 | break; |
166 | } | |
28c6821a | 167 | return flags; |
07eddf3d YL |
168 | } |
169 | ||
808e34e2 ZK |
170 | #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO) |
171 | ||
0b400c7e | 172 | /** |
3e466e2d | 173 | * pci_read_base - Read a PCI BAR |
0b400c7e YZ |
174 | * @dev: the PCI device |
175 | * @type: type of the BAR | |
176 | * @res: resource buffer to be filled in | |
177 | * @pos: BAR position in the config space | |
178 | * | |
179 | * Returns 1 if the BAR is 64-bit, or 0 if 32-bit. | |
6ac665c6 | 180 | */ |
0b400c7e | 181 | int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, |
3c78bc61 | 182 | struct resource *res, unsigned int pos) |
07eddf3d | 183 | { |
dc5205ef | 184 | u32 l = 0, sz = 0, mask; |
23b13bc7 | 185 | u64 l64, sz64, mask64; |
253d2e54 | 186 | u16 orig_cmd; |
cf4d1cf5 | 187 | struct pci_bus_region region, inverted_region; |
6ac665c6 | 188 | |
1ed67439 | 189 | mask = type ? PCI_ROM_ADDRESS_MASK : ~0; |
6ac665c6 | 190 | |
0ff9514b | 191 | /* No printks while decoding is disabled! */ |
253d2e54 JP |
192 | if (!dev->mmio_always_on) { |
193 | pci_read_config_word(dev, PCI_COMMAND, &orig_cmd); | |
808e34e2 ZK |
194 | if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) { |
195 | pci_write_config_word(dev, PCI_COMMAND, | |
196 | orig_cmd & ~PCI_COMMAND_DECODE_ENABLE); | |
197 | } | |
253d2e54 JP |
198 | } |
199 | ||
6ac665c6 MW |
200 | res->name = pci_name(dev); |
201 | ||
202 | pci_read_config_dword(dev, pos, &l); | |
1ed67439 | 203 | pci_write_config_dword(dev, pos, l | mask); |
6ac665c6 MW |
204 | pci_read_config_dword(dev, pos, &sz); |
205 | pci_write_config_dword(dev, pos, l); | |
206 | ||
207 | /* | |
208 | * All bits set in sz means the device isn't working properly. | |
45aa23b4 BH |
209 | * If the BAR isn't implemented, all bits must be 0. If it's a |
210 | * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit | |
211 | * 1 must be clear. | |
6ac665c6 | 212 | */ |
f795d86a MS |
213 | if (sz == 0xffffffff) |
214 | sz = 0; | |
6ac665c6 MW |
215 | |
216 | /* | |
217 | * I don't know how l can have all bits set. Copied from old code. | |
218 | * Maybe it fixes a bug on some ancient platform. | |
219 | */ | |
220 | if (l == 0xffffffff) | |
221 | l = 0; | |
222 | ||
223 | if (type == pci_bar_unknown) { | |
28c6821a BH |
224 | res->flags = decode_bar(dev, l); |
225 | res->flags |= IORESOURCE_SIZEALIGN; | |
226 | if (res->flags & IORESOURCE_IO) { | |
f795d86a MS |
227 | l64 = l & PCI_BASE_ADDRESS_IO_MASK; |
228 | sz64 = sz & PCI_BASE_ADDRESS_IO_MASK; | |
229 | mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT; | |
6ac665c6 | 230 | } else { |
f795d86a MS |
231 | l64 = l & PCI_BASE_ADDRESS_MEM_MASK; |
232 | sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK; | |
233 | mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK; | |
6ac665c6 MW |
234 | } |
235 | } else { | |
7a6d312b BH |
236 | if (l & PCI_ROM_ADDRESS_ENABLE) |
237 | res->flags |= IORESOURCE_ROM_ENABLE; | |
f795d86a MS |
238 | l64 = l & PCI_ROM_ADDRESS_MASK; |
239 | sz64 = sz & PCI_ROM_ADDRESS_MASK; | |
76dc5268 | 240 | mask64 = PCI_ROM_ADDRESS_MASK; |
6ac665c6 MW |
241 | } |
242 | ||
28c6821a | 243 | if (res->flags & IORESOURCE_MEM_64) { |
6ac665c6 MW |
244 | pci_read_config_dword(dev, pos + 4, &l); |
245 | pci_write_config_dword(dev, pos + 4, ~0); | |
246 | pci_read_config_dword(dev, pos + 4, &sz); | |
247 | pci_write_config_dword(dev, pos + 4, l); | |
248 | ||
249 | l64 |= ((u64)l << 32); | |
250 | sz64 |= ((u64)sz << 32); | |
f795d86a MS |
251 | mask64 |= ((u64)~0 << 32); |
252 | } | |
6ac665c6 | 253 | |
f795d86a MS |
254 | if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE)) |
255 | pci_write_config_word(dev, PCI_COMMAND, orig_cmd); | |
6ac665c6 | 256 | |
f795d86a MS |
257 | if (!sz64) |
258 | goto fail; | |
6ac665c6 | 259 | |
f795d86a | 260 | sz64 = pci_size(l64, sz64, mask64); |
7e79c5f8 | 261 | if (!sz64) { |
7506dc79 | 262 | pci_info(dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n", |
7e79c5f8 | 263 | pos); |
f795d86a | 264 | goto fail; |
7e79c5f8 | 265 | } |
f795d86a MS |
266 | |
267 | if (res->flags & IORESOURCE_MEM_64) { | |
3a9ad0b4 YL |
268 | if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8) |
269 | && sz64 > 0x100000000ULL) { | |
23b13bc7 BH |
270 | res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; |
271 | res->start = 0; | |
272 | res->end = 0; | |
7506dc79 | 273 | pci_err(dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n", |
f795d86a | 274 | pos, (unsigned long long)sz64); |
23b13bc7 | 275 | goto out; |
c7dabef8 BH |
276 | } |
277 | ||
3a9ad0b4 | 278 | if ((sizeof(pci_bus_addr_t) < 8) && l) { |
31e9dd25 | 279 | /* Above 32-bit boundary; try to reallocate */ |
c83bd900 | 280 | res->flags |= IORESOURCE_UNSET; |
72dc5601 BH |
281 | res->start = 0; |
282 | res->end = sz64; | |
7506dc79 | 283 | pci_info(dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n", |
f795d86a | 284 | pos, (unsigned long long)l64); |
72dc5601 | 285 | goto out; |
6ac665c6 | 286 | } |
6ac665c6 MW |
287 | } |
288 | ||
f795d86a MS |
289 | region.start = l64; |
290 | region.end = l64 + sz64; | |
291 | ||
fc279850 YL |
292 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
293 | pcibios_resource_to_bus(dev->bus, &inverted_region, res); | |
cf4d1cf5 KH |
294 | |
295 | /* | |
296 | * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is | |
297 | * the corresponding resource address (the physical address used by | |
298 | * the CPU. Converting that resource address back to a bus address | |
299 | * should yield the original BAR value: | |
300 | * | |
301 | * resource_to_bus(bus_to_resource(A)) == A | |
302 | * | |
303 | * If it doesn't, CPU accesses to "bus_to_resource(A)" will not | |
304 | * be claimed by the device. | |
305 | */ | |
306 | if (inverted_region.start != region.start) { | |
cf4d1cf5 | 307 | res->flags |= IORESOURCE_UNSET; |
cf4d1cf5 | 308 | res->start = 0; |
26370fc6 | 309 | res->end = region.end - region.start; |
7506dc79 | 310 | pci_info(dev, "reg 0x%x: initial BAR value %#010llx invalid\n", |
f795d86a | 311 | pos, (unsigned long long)region.start); |
cf4d1cf5 | 312 | } |
96ddef25 | 313 | |
0ff9514b BH |
314 | goto out; |
315 | ||
316 | ||
317 | fail: | |
318 | res->flags = 0; | |
319 | out: | |
31e9dd25 | 320 | if (res->flags) |
7506dc79 | 321 | pci_printk(KERN_DEBUG, dev, "reg 0x%x: %pR\n", pos, res); |
0ff9514b | 322 | |
28c6821a | 323 | return (res->flags & IORESOURCE_MEM_64) ? 1 : 0; |
07eddf3d YL |
324 | } |
325 | ||
1da177e4 LT |
326 | static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) |
327 | { | |
6ac665c6 | 328 | unsigned int pos, reg; |
07eddf3d | 329 | |
ad67b437 PB |
330 | if (dev->non_compliant_bars) |
331 | return; | |
332 | ||
bf4447fd KA |
333 | /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */ |
334 | if (dev->is_virtfn) | |
335 | return; | |
336 | ||
6ac665c6 MW |
337 | for (pos = 0; pos < howmany; pos++) { |
338 | struct resource *res = &dev->resource[pos]; | |
1da177e4 | 339 | reg = PCI_BASE_ADDRESS_0 + (pos << 2); |
6ac665c6 | 340 | pos += __pci_read_base(dev, pci_bar_unknown, res, reg); |
1da177e4 | 341 | } |
6ac665c6 | 342 | |
1da177e4 | 343 | if (rom) { |
6ac665c6 | 344 | struct resource *res = &dev->resource[PCI_ROM_RESOURCE]; |
1da177e4 | 345 | dev->rom_base_reg = rom; |
6ac665c6 | 346 | res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | |
92b19ff5 | 347 | IORESOURCE_READONLY | IORESOURCE_SIZEALIGN; |
6ac665c6 | 348 | __pci_read_base(dev, pci_bar_mem32, res, rom); |
1da177e4 LT |
349 | } |
350 | } | |
351 | ||
15856ad5 | 352 | static void pci_read_bridge_io(struct pci_bus *child) |
1da177e4 LT |
353 | { |
354 | struct pci_dev *dev = child->self; | |
355 | u8 io_base_lo, io_limit_lo; | |
2b28ae19 | 356 | unsigned long io_mask, io_granularity, base, limit; |
5bfa14ed | 357 | struct pci_bus_region region; |
2b28ae19 BH |
358 | struct resource *res; |
359 | ||
360 | io_mask = PCI_IO_RANGE_MASK; | |
361 | io_granularity = 0x1000; | |
362 | if (dev->io_window_1k) { | |
363 | /* Support 1K I/O space granularity */ | |
364 | io_mask = PCI_IO_1K_RANGE_MASK; | |
365 | io_granularity = 0x400; | |
366 | } | |
1da177e4 | 367 | |
1da177e4 LT |
368 | res = child->resource[0]; |
369 | pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); | |
370 | pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo); | |
2b28ae19 BH |
371 | base = (io_base_lo & io_mask) << 8; |
372 | limit = (io_limit_lo & io_mask) << 8; | |
1da177e4 LT |
373 | |
374 | if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { | |
375 | u16 io_base_hi, io_limit_hi; | |
8f38eaca | 376 | |
1da177e4 LT |
377 | pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi); |
378 | pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi); | |
8f38eaca BH |
379 | base |= ((unsigned long) io_base_hi << 16); |
380 | limit |= ((unsigned long) io_limit_hi << 16); | |
1da177e4 LT |
381 | } |
382 | ||
5dde383e | 383 | if (base <= limit) { |
1da177e4 | 384 | res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; |
5bfa14ed | 385 | region.start = base; |
2b28ae19 | 386 | region.end = limit + io_granularity - 1; |
fc279850 | 387 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 388 | pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res); |
1da177e4 | 389 | } |
fa27b2d1 BH |
390 | } |
391 | ||
15856ad5 | 392 | static void pci_read_bridge_mmio(struct pci_bus *child) |
fa27b2d1 BH |
393 | { |
394 | struct pci_dev *dev = child->self; | |
395 | u16 mem_base_lo, mem_limit_lo; | |
396 | unsigned long base, limit; | |
5bfa14ed | 397 | struct pci_bus_region region; |
fa27b2d1 | 398 | struct resource *res; |
1da177e4 LT |
399 | |
400 | res = child->resource[1]; | |
401 | pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo); | |
402 | pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo); | |
8f38eaca BH |
403 | base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16; |
404 | limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; | |
5dde383e | 405 | if (base <= limit) { |
1da177e4 | 406 | res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM; |
5bfa14ed BH |
407 | region.start = base; |
408 | region.end = limit + 0xfffff; | |
fc279850 | 409 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 410 | pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res); |
1da177e4 | 411 | } |
fa27b2d1 BH |
412 | } |
413 | ||
15856ad5 | 414 | static void pci_read_bridge_mmio_pref(struct pci_bus *child) |
fa27b2d1 BH |
415 | { |
416 | struct pci_dev *dev = child->self; | |
417 | u16 mem_base_lo, mem_limit_lo; | |
7fc986d8 | 418 | u64 base64, limit64; |
3a9ad0b4 | 419 | pci_bus_addr_t base, limit; |
5bfa14ed | 420 | struct pci_bus_region region; |
fa27b2d1 | 421 | struct resource *res; |
1da177e4 LT |
422 | |
423 | res = child->resource[2]; | |
424 | pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo); | |
425 | pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo); | |
7fc986d8 YL |
426 | base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16; |
427 | limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16; | |
1da177e4 LT |
428 | |
429 | if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { | |
430 | u32 mem_base_hi, mem_limit_hi; | |
8f38eaca | 431 | |
1da177e4 LT |
432 | pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi); |
433 | pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi); | |
434 | ||
435 | /* | |
436 | * Some bridges set the base > limit by default, and some | |
437 | * (broken) BIOSes do not initialize them. If we find | |
438 | * this, just assume they are not being used. | |
439 | */ | |
440 | if (mem_base_hi <= mem_limit_hi) { | |
7fc986d8 YL |
441 | base64 |= (u64) mem_base_hi << 32; |
442 | limit64 |= (u64) mem_limit_hi << 32; | |
1da177e4 LT |
443 | } |
444 | } | |
7fc986d8 | 445 | |
3a9ad0b4 YL |
446 | base = (pci_bus_addr_t) base64; |
447 | limit = (pci_bus_addr_t) limit64; | |
7fc986d8 YL |
448 | |
449 | if (base != base64) { | |
7506dc79 | 450 | pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n", |
7fc986d8 YL |
451 | (unsigned long long) base64); |
452 | return; | |
453 | } | |
454 | ||
5dde383e | 455 | if (base <= limit) { |
1f82de10 YL |
456 | res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) | |
457 | IORESOURCE_MEM | IORESOURCE_PREFETCH; | |
458 | if (res->flags & PCI_PREF_RANGE_TYPE_64) | |
459 | res->flags |= IORESOURCE_MEM_64; | |
5bfa14ed BH |
460 | region.start = base; |
461 | region.end = limit + 0xfffff; | |
fc279850 | 462 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 463 | pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res); |
1da177e4 LT |
464 | } |
465 | } | |
466 | ||
15856ad5 | 467 | void pci_read_bridge_bases(struct pci_bus *child) |
fa27b2d1 BH |
468 | { |
469 | struct pci_dev *dev = child->self; | |
2fe2abf8 | 470 | struct resource *res; |
fa27b2d1 BH |
471 | int i; |
472 | ||
473 | if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */ | |
474 | return; | |
475 | ||
7506dc79 | 476 | pci_info(dev, "PCI bridge to %pR%s\n", |
b918c62e | 477 | &child->busn_res, |
fa27b2d1 BH |
478 | dev->transparent ? " (subtractive decode)" : ""); |
479 | ||
2fe2abf8 BH |
480 | pci_bus_remove_resources(child); |
481 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) | |
482 | child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i]; | |
483 | ||
fa27b2d1 BH |
484 | pci_read_bridge_io(child); |
485 | pci_read_bridge_mmio(child); | |
486 | pci_read_bridge_mmio_pref(child); | |
2adf7516 BH |
487 | |
488 | if (dev->transparent) { | |
2fe2abf8 | 489 | pci_bus_for_each_resource(child->parent, res, i) { |
d739a099 | 490 | if (res && res->flags) { |
2fe2abf8 BH |
491 | pci_bus_add_resource(child, res, |
492 | PCI_SUBTRACTIVE_DECODE); | |
7506dc79 | 493 | pci_printk(KERN_DEBUG, dev, |
2adf7516 | 494 | " bridge window %pR (subtractive decode)\n", |
2fe2abf8 BH |
495 | res); |
496 | } | |
2adf7516 BH |
497 | } |
498 | } | |
fa27b2d1 BH |
499 | } |
500 | ||
670ba0c8 | 501 | static struct pci_bus *pci_alloc_bus(struct pci_bus *parent) |
1da177e4 LT |
502 | { |
503 | struct pci_bus *b; | |
504 | ||
f5afe806 | 505 | b = kzalloc(sizeof(*b), GFP_KERNEL); |
05013486 BH |
506 | if (!b) |
507 | return NULL; | |
508 | ||
509 | INIT_LIST_HEAD(&b->node); | |
510 | INIT_LIST_HEAD(&b->children); | |
511 | INIT_LIST_HEAD(&b->devices); | |
512 | INIT_LIST_HEAD(&b->slots); | |
513 | INIT_LIST_HEAD(&b->resources); | |
514 | b->max_bus_speed = PCI_SPEED_UNKNOWN; | |
515 | b->cur_bus_speed = PCI_SPEED_UNKNOWN; | |
670ba0c8 CM |
516 | #ifdef CONFIG_PCI_DOMAINS_GENERIC |
517 | if (parent) | |
518 | b->domain_nr = parent->domain_nr; | |
519 | #endif | |
1da177e4 LT |
520 | return b; |
521 | } | |
522 | ||
5c3f18cc | 523 | static void devm_pci_release_host_bridge_dev(struct device *dev) |
70efde2a JL |
524 | { |
525 | struct pci_host_bridge *bridge = to_pci_host_bridge(dev); | |
526 | ||
527 | if (bridge->release_fn) | |
528 | bridge->release_fn(bridge); | |
3bbce531 JK |
529 | |
530 | pci_free_resource_list(&bridge->windows); | |
5c3f18cc | 531 | } |
70efde2a | 532 | |
5c3f18cc LP |
533 | static void pci_release_host_bridge_dev(struct device *dev) |
534 | { | |
535 | devm_pci_release_host_bridge_dev(dev); | |
3bbce531 | 536 | kfree(to_pci_host_bridge(dev)); |
70efde2a JL |
537 | } |
538 | ||
a52d1443 | 539 | struct pci_host_bridge *pci_alloc_host_bridge(size_t priv) |
7b543663 YL |
540 | { |
541 | struct pci_host_bridge *bridge; | |
542 | ||
59094065 | 543 | bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL); |
05013486 BH |
544 | if (!bridge) |
545 | return NULL; | |
7b543663 | 546 | |
05013486 | 547 | INIT_LIST_HEAD(&bridge->windows); |
a1c0050a | 548 | bridge->dev.release = pci_release_host_bridge_dev; |
37d6a0a6 | 549 | |
02bfeb48 BH |
550 | /* |
551 | * We assume we can manage these PCIe features. Some systems may | |
552 | * reserve these for use by the platform itself, e.g., an ACPI BIOS | |
553 | * may implement its own AER handling and use _OSC to prevent the | |
554 | * OS from interfering. | |
555 | */ | |
556 | bridge->native_aer = 1; | |
9310f0dc | 557 | bridge->native_pcie_hotplug = 1; |
1df81a6d | 558 | bridge->native_shpc_hotplug = 1; |
02bfeb48 | 559 | bridge->native_pme = 1; |
af8bb9f8 | 560 | bridge->native_ltr = 1; |
02bfeb48 | 561 | |
7b543663 YL |
562 | return bridge; |
563 | } | |
a52d1443 | 564 | EXPORT_SYMBOL(pci_alloc_host_bridge); |
7b543663 | 565 | |
5c3f18cc LP |
566 | struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev, |
567 | size_t priv) | |
568 | { | |
569 | struct pci_host_bridge *bridge; | |
570 | ||
571 | bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL); | |
572 | if (!bridge) | |
573 | return NULL; | |
574 | ||
575 | INIT_LIST_HEAD(&bridge->windows); | |
576 | bridge->dev.release = devm_pci_release_host_bridge_dev; | |
577 | ||
578 | return bridge; | |
579 | } | |
580 | EXPORT_SYMBOL(devm_pci_alloc_host_bridge); | |
581 | ||
dff79b91 LP |
582 | void pci_free_host_bridge(struct pci_host_bridge *bridge) |
583 | { | |
584 | pci_free_resource_list(&bridge->windows); | |
585 | ||
586 | kfree(bridge); | |
587 | } | |
588 | EXPORT_SYMBOL(pci_free_host_bridge); | |
589 | ||
0b950f0f | 590 | static const unsigned char pcix_bus_speed[] = { |
9be60ca0 MW |
591 | PCI_SPEED_UNKNOWN, /* 0 */ |
592 | PCI_SPEED_66MHz_PCIX, /* 1 */ | |
593 | PCI_SPEED_100MHz_PCIX, /* 2 */ | |
594 | PCI_SPEED_133MHz_PCIX, /* 3 */ | |
595 | PCI_SPEED_UNKNOWN, /* 4 */ | |
596 | PCI_SPEED_66MHz_PCIX_ECC, /* 5 */ | |
597 | PCI_SPEED_100MHz_PCIX_ECC, /* 6 */ | |
598 | PCI_SPEED_133MHz_PCIX_ECC, /* 7 */ | |
599 | PCI_SPEED_UNKNOWN, /* 8 */ | |
600 | PCI_SPEED_66MHz_PCIX_266, /* 9 */ | |
601 | PCI_SPEED_100MHz_PCIX_266, /* A */ | |
602 | PCI_SPEED_133MHz_PCIX_266, /* B */ | |
603 | PCI_SPEED_UNKNOWN, /* C */ | |
604 | PCI_SPEED_66MHz_PCIX_533, /* D */ | |
605 | PCI_SPEED_100MHz_PCIX_533, /* E */ | |
606 | PCI_SPEED_133MHz_PCIX_533 /* F */ | |
607 | }; | |
608 | ||
343e51ae | 609 | const unsigned char pcie_link_speed[] = { |
3749c51a MW |
610 | PCI_SPEED_UNKNOWN, /* 0 */ |
611 | PCIE_SPEED_2_5GT, /* 1 */ | |
612 | PCIE_SPEED_5_0GT, /* 2 */ | |
9dfd97fe | 613 | PCIE_SPEED_8_0GT, /* 3 */ |
1acfb9b7 | 614 | PCIE_SPEED_16_0GT, /* 4 */ |
3749c51a MW |
615 | PCI_SPEED_UNKNOWN, /* 5 */ |
616 | PCI_SPEED_UNKNOWN, /* 6 */ | |
617 | PCI_SPEED_UNKNOWN, /* 7 */ | |
618 | PCI_SPEED_UNKNOWN, /* 8 */ | |
619 | PCI_SPEED_UNKNOWN, /* 9 */ | |
620 | PCI_SPEED_UNKNOWN, /* A */ | |
621 | PCI_SPEED_UNKNOWN, /* B */ | |
622 | PCI_SPEED_UNKNOWN, /* C */ | |
623 | PCI_SPEED_UNKNOWN, /* D */ | |
624 | PCI_SPEED_UNKNOWN, /* E */ | |
625 | PCI_SPEED_UNKNOWN /* F */ | |
626 | }; | |
627 | ||
628 | void pcie_update_link_speed(struct pci_bus *bus, u16 linksta) | |
629 | { | |
231afea1 | 630 | bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS]; |
3749c51a MW |
631 | } |
632 | EXPORT_SYMBOL_GPL(pcie_update_link_speed); | |
633 | ||
45b4cdd5 MW |
634 | static unsigned char agp_speeds[] = { |
635 | AGP_UNKNOWN, | |
636 | AGP_1X, | |
637 | AGP_2X, | |
638 | AGP_4X, | |
639 | AGP_8X | |
640 | }; | |
641 | ||
642 | static enum pci_bus_speed agp_speed(int agp3, int agpstat) | |
643 | { | |
644 | int index = 0; | |
645 | ||
646 | if (agpstat & 4) | |
647 | index = 3; | |
648 | else if (agpstat & 2) | |
649 | index = 2; | |
650 | else if (agpstat & 1) | |
651 | index = 1; | |
652 | else | |
653 | goto out; | |
f7625980 | 654 | |
45b4cdd5 MW |
655 | if (agp3) { |
656 | index += 2; | |
657 | if (index == 5) | |
658 | index = 0; | |
659 | } | |
660 | ||
661 | out: | |
662 | return agp_speeds[index]; | |
663 | } | |
664 | ||
9be60ca0 MW |
665 | static void pci_set_bus_speed(struct pci_bus *bus) |
666 | { | |
667 | struct pci_dev *bridge = bus->self; | |
668 | int pos; | |
669 | ||
45b4cdd5 MW |
670 | pos = pci_find_capability(bridge, PCI_CAP_ID_AGP); |
671 | if (!pos) | |
672 | pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3); | |
673 | if (pos) { | |
674 | u32 agpstat, agpcmd; | |
675 | ||
676 | pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat); | |
677 | bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7); | |
678 | ||
679 | pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd); | |
680 | bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7); | |
681 | } | |
682 | ||
9be60ca0 MW |
683 | pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX); |
684 | if (pos) { | |
685 | u16 status; | |
686 | enum pci_bus_speed max; | |
9be60ca0 | 687 | |
7793eeab BH |
688 | pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS, |
689 | &status); | |
690 | ||
691 | if (status & PCI_X_SSTATUS_533MHZ) { | |
9be60ca0 | 692 | max = PCI_SPEED_133MHz_PCIX_533; |
7793eeab | 693 | } else if (status & PCI_X_SSTATUS_266MHZ) { |
9be60ca0 | 694 | max = PCI_SPEED_133MHz_PCIX_266; |
7793eeab | 695 | } else if (status & PCI_X_SSTATUS_133MHZ) { |
3c78bc61 | 696 | if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2) |
9be60ca0 | 697 | max = PCI_SPEED_133MHz_PCIX_ECC; |
3c78bc61 | 698 | else |
9be60ca0 | 699 | max = PCI_SPEED_133MHz_PCIX; |
9be60ca0 MW |
700 | } else { |
701 | max = PCI_SPEED_66MHz_PCIX; | |
702 | } | |
703 | ||
704 | bus->max_bus_speed = max; | |
7793eeab BH |
705 | bus->cur_bus_speed = pcix_bus_speed[ |
706 | (status & PCI_X_SSTATUS_FREQ) >> 6]; | |
9be60ca0 MW |
707 | |
708 | return; | |
709 | } | |
710 | ||
fdfe1511 | 711 | if (pci_is_pcie(bridge)) { |
9be60ca0 MW |
712 | u32 linkcap; |
713 | u16 linksta; | |
714 | ||
59875ae4 | 715 | pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap); |
231afea1 | 716 | bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS]; |
9be60ca0 | 717 | |
59875ae4 | 718 | pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta); |
9be60ca0 MW |
719 | pcie_update_link_speed(bus, linksta); |
720 | } | |
721 | } | |
722 | ||
44aa0c65 MZ |
723 | static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus) |
724 | { | |
b165e2b6 MZ |
725 | struct irq_domain *d; |
726 | ||
44aa0c65 MZ |
727 | /* |
728 | * Any firmware interface that can resolve the msi_domain | |
729 | * should be called from here. | |
730 | */ | |
b165e2b6 | 731 | d = pci_host_bridge_of_msi_domain(bus); |
471036b2 SS |
732 | if (!d) |
733 | d = pci_host_bridge_acpi_msi_domain(bus); | |
44aa0c65 | 734 | |
788858eb JO |
735 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
736 | /* | |
737 | * If no IRQ domain was found via the OF tree, try looking it up | |
738 | * directly through the fwnode_handle. | |
739 | */ | |
740 | if (!d) { | |
741 | struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus); | |
742 | ||
743 | if (fwnode) | |
744 | d = irq_find_matching_fwnode(fwnode, | |
745 | DOMAIN_BUS_PCI_MSI); | |
746 | } | |
747 | #endif | |
748 | ||
b165e2b6 | 749 | return d; |
44aa0c65 MZ |
750 | } |
751 | ||
752 | static void pci_set_bus_msi_domain(struct pci_bus *bus) | |
753 | { | |
754 | struct irq_domain *d; | |
38ea72bd | 755 | struct pci_bus *b; |
44aa0c65 MZ |
756 | |
757 | /* | |
38ea72bd AW |
758 | * The bus can be a root bus, a subordinate bus, or a virtual bus |
759 | * created by an SR-IOV device. Walk up to the first bridge device | |
760 | * found or derive the domain from the host bridge. | |
44aa0c65 | 761 | */ |
38ea72bd AW |
762 | for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) { |
763 | if (b->self) | |
764 | d = dev_get_msi_domain(&b->self->dev); | |
765 | } | |
766 | ||
767 | if (!d) | |
768 | d = pci_host_bridge_msi_domain(b); | |
44aa0c65 MZ |
769 | |
770 | dev_set_msi_domain(&bus->dev, d); | |
771 | } | |
772 | ||
cea9bc0b | 773 | static int pci_register_host_bridge(struct pci_host_bridge *bridge) |
37d6a0a6 AB |
774 | { |
775 | struct device *parent = bridge->dev.parent; | |
776 | struct resource_entry *window, *n; | |
777 | struct pci_bus *bus, *b; | |
778 | resource_size_t offset; | |
779 | LIST_HEAD(resources); | |
780 | struct resource *res; | |
781 | char addr[64], *fmt; | |
782 | const char *name; | |
783 | int err; | |
784 | ||
785 | bus = pci_alloc_bus(NULL); | |
786 | if (!bus) | |
787 | return -ENOMEM; | |
788 | ||
789 | bridge->bus = bus; | |
790 | ||
3e466e2d | 791 | /* Temporarily move resources off the list */ |
37d6a0a6 AB |
792 | list_splice_init(&bridge->windows, &resources); |
793 | bus->sysdata = bridge->sysdata; | |
794 | bus->msi = bridge->msi; | |
795 | bus->ops = bridge->ops; | |
796 | bus->number = bus->busn_res.start = bridge->busnr; | |
797 | #ifdef CONFIG_PCI_DOMAINS_GENERIC | |
798 | bus->domain_nr = pci_bus_find_domain_nr(bus, parent); | |
799 | #endif | |
800 | ||
801 | b = pci_find_bus(pci_domain_nr(bus), bridge->busnr); | |
802 | if (b) { | |
3e466e2d | 803 | /* Ignore it if we already got here via a different bridge */ |
37d6a0a6 AB |
804 | dev_dbg(&b->dev, "bus already known\n"); |
805 | err = -EEXIST; | |
806 | goto free; | |
807 | } | |
808 | ||
809 | dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus), | |
810 | bridge->busnr); | |
811 | ||
812 | err = pcibios_root_bridge_prepare(bridge); | |
813 | if (err) | |
814 | goto free; | |
815 | ||
816 | err = device_register(&bridge->dev); | |
817 | if (err) | |
818 | put_device(&bridge->dev); | |
819 | ||
820 | bus->bridge = get_device(&bridge->dev); | |
821 | device_enable_async_suspend(bus->bridge); | |
822 | pci_set_bus_of_node(bus); | |
823 | pci_set_bus_msi_domain(bus); | |
824 | ||
825 | if (!parent) | |
826 | set_dev_node(bus->bridge, pcibus_to_node(bus)); | |
827 | ||
828 | bus->dev.class = &pcibus_class; | |
829 | bus->dev.parent = bus->bridge; | |
830 | ||
831 | dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number); | |
832 | name = dev_name(&bus->dev); | |
833 | ||
834 | err = device_register(&bus->dev); | |
835 | if (err) | |
836 | goto unregister; | |
837 | ||
838 | pcibios_add_bus(bus); | |
839 | ||
840 | /* Create legacy_io and legacy_mem files for this bus */ | |
841 | pci_create_legacy_files(bus); | |
842 | ||
843 | if (parent) | |
844 | dev_info(parent, "PCI host bridge to bus %s\n", name); | |
845 | else | |
846 | pr_info("PCI host bridge to bus %s\n", name); | |
847 | ||
848 | /* Add initial resources to the bus */ | |
849 | resource_list_for_each_entry_safe(window, n, &resources) { | |
850 | list_move_tail(&window->node, &bridge->windows); | |
851 | offset = window->offset; | |
852 | res = window->res; | |
853 | ||
854 | if (res->flags & IORESOURCE_BUS) | |
855 | pci_bus_insert_busn_res(bus, bus->number, res->end); | |
856 | else | |
857 | pci_bus_add_resource(bus, res, 0); | |
858 | ||
859 | if (offset) { | |
860 | if (resource_type(res) == IORESOURCE_IO) | |
861 | fmt = " (bus address [%#06llx-%#06llx])"; | |
862 | else | |
863 | fmt = " (bus address [%#010llx-%#010llx])"; | |
864 | ||
865 | snprintf(addr, sizeof(addr), fmt, | |
866 | (unsigned long long)(res->start - offset), | |
867 | (unsigned long long)(res->end - offset)); | |
868 | } else | |
869 | addr[0] = '\0'; | |
870 | ||
871 | dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr); | |
872 | } | |
873 | ||
874 | down_write(&pci_bus_sem); | |
875 | list_add_tail(&bus->node, &pci_root_buses); | |
876 | up_write(&pci_bus_sem); | |
877 | ||
878 | return 0; | |
879 | ||
880 | unregister: | |
881 | put_device(&bridge->dev); | |
882 | device_unregister(&bridge->dev); | |
883 | ||
884 | free: | |
885 | kfree(bus); | |
886 | return err; | |
887 | } | |
888 | ||
17e8f0d4 GB |
889 | static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev *bridge) |
890 | { | |
891 | int pos; | |
892 | u32 status; | |
893 | ||
894 | /* | |
895 | * If extended config space isn't accessible on a bridge's primary | |
896 | * bus, we certainly can't access it on the secondary bus. | |
897 | */ | |
898 | if (bridge->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG) | |
899 | return false; | |
900 | ||
901 | /* | |
902 | * PCIe Root Ports and switch ports are PCIe on both sides, so if | |
903 | * extended config space is accessible on the primary, it's also | |
904 | * accessible on the secondary. | |
905 | */ | |
906 | if (pci_is_pcie(bridge) && | |
907 | (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT || | |
908 | pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM || | |
909 | pci_pcie_type(bridge) == PCI_EXP_TYPE_DOWNSTREAM)) | |
910 | return true; | |
911 | ||
912 | /* | |
913 | * For the other bridge types: | |
914 | * - PCI-to-PCI bridges | |
915 | * - PCIe-to-PCI/PCI-X forward bridges | |
916 | * - PCI/PCI-X-to-PCIe reverse bridges | |
917 | * extended config space on the secondary side is only accessible | |
918 | * if the bridge supports PCI-X Mode 2. | |
919 | */ | |
920 | pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX); | |
921 | if (!pos) | |
922 | return false; | |
923 | ||
924 | pci_read_config_dword(bridge, pos + PCI_X_STATUS, &status); | |
925 | return status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ); | |
926 | } | |
927 | ||
cbd4e055 AB |
928 | static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, |
929 | struct pci_dev *bridge, int busnr) | |
1da177e4 LT |
930 | { |
931 | struct pci_bus *child; | |
932 | int i; | |
4f535093 | 933 | int ret; |
1da177e4 | 934 | |
3e466e2d | 935 | /* Allocate a new bus and inherit stuff from the parent */ |
670ba0c8 | 936 | child = pci_alloc_bus(parent); |
1da177e4 LT |
937 | if (!child) |
938 | return NULL; | |
939 | ||
1da177e4 LT |
940 | child->parent = parent; |
941 | child->ops = parent->ops; | |
0cbdcfcf | 942 | child->msi = parent->msi; |
1da177e4 | 943 | child->sysdata = parent->sysdata; |
6e325a62 | 944 | child->bus_flags = parent->bus_flags; |
1da177e4 | 945 | |
3e466e2d BH |
946 | /* |
947 | * Initialize some portions of the bus device, but don't register | |
948 | * it now as the parent is not properly set up yet. | |
fd7d1ced GKH |
949 | */ |
950 | child->dev.class = &pcibus_class; | |
1a927133 | 951 | dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr); |
1da177e4 | 952 | |
3e466e2d | 953 | /* Set up the primary, secondary and subordinate bus numbers */ |
b918c62e YL |
954 | child->number = child->busn_res.start = busnr; |
955 | child->primary = parent->busn_res.start; | |
956 | child->busn_res.end = 0xff; | |
1da177e4 | 957 | |
4f535093 YL |
958 | if (!bridge) { |
959 | child->dev.parent = parent->bridge; | |
960 | goto add_dev; | |
961 | } | |
3789fa8a YZ |
962 | |
963 | child->self = bridge; | |
964 | child->bridge = get_device(&bridge->dev); | |
4f535093 | 965 | child->dev.parent = child->bridge; |
98d9f30c | 966 | pci_set_bus_of_node(child); |
9be60ca0 MW |
967 | pci_set_bus_speed(child); |
968 | ||
17e8f0d4 GB |
969 | /* |
970 | * Check whether extended config space is accessible on the child | |
971 | * bus. Note that we currently assume it is always accessible on | |
972 | * the root bus. | |
973 | */ | |
974 | if (!pci_bridge_child_ext_cfg_accessible(bridge)) { | |
975 | child->bus_flags |= PCI_BUS_FLAGS_NO_EXTCFG; | |
976 | pci_info(child, "extended config space not accessible\n"); | |
977 | } | |
978 | ||
3e466e2d | 979 | /* Set up default resource pointers and names */ |
fde09c6d | 980 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { |
1da177e4 LT |
981 | child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i]; |
982 | child->resource[i]->name = child->name; | |
983 | } | |
984 | bridge->subordinate = child; | |
985 | ||
4f535093 | 986 | add_dev: |
44aa0c65 | 987 | pci_set_bus_msi_domain(child); |
4f535093 YL |
988 | ret = device_register(&child->dev); |
989 | WARN_ON(ret < 0); | |
990 | ||
10a95747 JL |
991 | pcibios_add_bus(child); |
992 | ||
057bd2e0 TR |
993 | if (child->ops->add_bus) { |
994 | ret = child->ops->add_bus(child); | |
995 | if (WARN_ON(ret < 0)) | |
996 | dev_err(&child->dev, "failed to add bus: %d\n", ret); | |
997 | } | |
998 | ||
4f535093 YL |
999 | /* Create legacy_io and legacy_mem files for this bus */ |
1000 | pci_create_legacy_files(child); | |
1001 | ||
1da177e4 LT |
1002 | return child; |
1003 | } | |
1004 | ||
3c78bc61 RD |
1005 | struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, |
1006 | int busnr) | |
1da177e4 LT |
1007 | { |
1008 | struct pci_bus *child; | |
1009 | ||
1010 | child = pci_alloc_child_bus(parent, dev, busnr); | |
e4ea9bb7 | 1011 | if (child) { |
d71374da | 1012 | down_write(&pci_bus_sem); |
1da177e4 | 1013 | list_add_tail(&child->node, &parent->children); |
d71374da | 1014 | up_write(&pci_bus_sem); |
e4ea9bb7 | 1015 | } |
1da177e4 LT |
1016 | return child; |
1017 | } | |
b7fe9434 | 1018 | EXPORT_SYMBOL(pci_add_new_bus); |
1da177e4 | 1019 | |
f3dbd802 RJ |
1020 | static void pci_enable_crs(struct pci_dev *pdev) |
1021 | { | |
1022 | u16 root_cap = 0; | |
1023 | ||
1024 | /* Enable CRS Software Visibility if supported */ | |
1025 | pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap); | |
1026 | if (root_cap & PCI_EXP_RTCAP_CRSVIS) | |
1027 | pcie_capability_set_word(pdev, PCI_EXP_RTCTL, | |
1028 | PCI_EXP_RTCTL_CRSSVE); | |
1029 | } | |
1030 | ||
1c02ea81 MW |
1031 | static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus, |
1032 | unsigned int available_buses); | |
1033 | ||
1da177e4 | 1034 | /* |
1c02ea81 MW |
1035 | * pci_scan_bridge_extend() - Scan buses behind a bridge |
1036 | * @bus: Parent bus the bridge is on | |
1037 | * @dev: Bridge itself | |
1038 | * @max: Starting subordinate number of buses behind this bridge | |
1039 | * @available_buses: Total number of buses available for this bridge and | |
1040 | * the devices below. After the minimal bus space has | |
1041 | * been allocated the remaining buses will be | |
1042 | * distributed equally between hotplug-capable bridges. | |
1043 | * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges | |
1044 | * that need to be reconfigured. | |
1045 | * | |
1da177e4 LT |
1046 | * If it's a bridge, configure it and scan the bus behind it. |
1047 | * For CardBus bridges, we don't scan behind as the devices will | |
1048 | * be handled by the bridge driver itself. | |
1049 | * | |
1050 | * We need to process bridges in two passes -- first we scan those | |
1051 | * already configured by the BIOS and after we are done with all of | |
1052 | * them, we proceed to assigning numbers to the remaining buses in | |
1053 | * order to avoid overlaps between old and new bus numbers. | |
70f7880d MW |
1054 | * |
1055 | * Return: New subordinate number covering all buses behind this bridge. | |
1da177e4 | 1056 | */ |
1c02ea81 MW |
1057 | static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev, |
1058 | int max, unsigned int available_buses, | |
1059 | int pass) | |
1da177e4 LT |
1060 | { |
1061 | struct pci_bus *child; | |
1062 | int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS); | |
49887941 | 1063 | u32 buses, i, j = 0; |
1da177e4 | 1064 | u16 bctl; |
99ddd552 | 1065 | u8 primary, secondary, subordinate; |
a1c19894 | 1066 | int broken = 0; |
1da177e4 | 1067 | |
d963f651 MW |
1068 | /* |
1069 | * Make sure the bridge is powered on to be able to access config | |
1070 | * space of devices below it. | |
1071 | */ | |
1072 | pm_runtime_get_sync(&dev->dev); | |
1073 | ||
1da177e4 | 1074 | pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses); |
99ddd552 BH |
1075 | primary = buses & 0xFF; |
1076 | secondary = (buses >> 8) & 0xFF; | |
1077 | subordinate = (buses >> 16) & 0xFF; | |
1da177e4 | 1078 | |
7506dc79 | 1079 | pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n", |
99ddd552 | 1080 | secondary, subordinate, pass); |
1da177e4 | 1081 | |
71f6bd4a | 1082 | if (!primary && (primary != bus->number) && secondary && subordinate) { |
7506dc79 | 1083 | pci_warn(dev, "Primary bus is hard wired to 0\n"); |
71f6bd4a YL |
1084 | primary = bus->number; |
1085 | } | |
1086 | ||
a1c19894 BH |
1087 | /* Check if setup is sensible at all */ |
1088 | if (!pass && | |
1965f66e | 1089 | (primary != bus->number || secondary <= bus->number || |
12d87069 | 1090 | secondary > subordinate)) { |
7506dc79 | 1091 | pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n", |
1965f66e | 1092 | secondary, subordinate); |
a1c19894 BH |
1093 | broken = 1; |
1094 | } | |
1095 | ||
3e466e2d BH |
1096 | /* |
1097 | * Disable Master-Abort Mode during probing to avoid reporting of | |
1098 | * bus errors in some architectures. | |
1099 | */ | |
1da177e4 LT |
1100 | pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl); |
1101 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, | |
1102 | bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT); | |
1103 | ||
f3dbd802 RJ |
1104 | pci_enable_crs(dev); |
1105 | ||
99ddd552 BH |
1106 | if ((secondary || subordinate) && !pcibios_assign_all_busses() && |
1107 | !is_cardbus && !broken) { | |
1108 | unsigned int cmax; | |
3e466e2d | 1109 | |
1da177e4 | 1110 | /* |
3e466e2d BH |
1111 | * Bus already configured by firmware, process it in the |
1112 | * first pass and just note the configuration. | |
1da177e4 LT |
1113 | */ |
1114 | if (pass) | |
bbe8f9a3 | 1115 | goto out; |
1da177e4 LT |
1116 | |
1117 | /* | |
3e466e2d BH |
1118 | * The bus might already exist for two reasons: Either we |
1119 | * are rescanning the bus or the bus is reachable through | |
1120 | * more than one bridge. The second case can happen with | |
1121 | * the i450NX chipset. | |
1da177e4 | 1122 | */ |
99ddd552 | 1123 | child = pci_find_bus(pci_domain_nr(bus), secondary); |
74710ded | 1124 | if (!child) { |
99ddd552 | 1125 | child = pci_add_new_bus(bus, dev, secondary); |
74710ded AC |
1126 | if (!child) |
1127 | goto out; | |
99ddd552 | 1128 | child->primary = primary; |
bc76b731 | 1129 | pci_bus_insert_busn_res(child, secondary, subordinate); |
74710ded | 1130 | child->bridge_ctl = bctl; |
1da177e4 LT |
1131 | } |
1132 | ||
1da177e4 | 1133 | cmax = pci_scan_child_bus(child); |
c95b0bd6 | 1134 | if (cmax > subordinate) |
7506dc79 | 1135 | pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n", |
c95b0bd6 | 1136 | subordinate, cmax); |
3e466e2d BH |
1137 | |
1138 | /* Subordinate should equal child->busn_res.end */ | |
c95b0bd6 AN |
1139 | if (subordinate > max) |
1140 | max = subordinate; | |
1da177e4 | 1141 | } else { |
3e466e2d | 1142 | |
1da177e4 LT |
1143 | /* |
1144 | * We need to assign a number to this bus which we always | |
1145 | * do in the second pass. | |
1146 | */ | |
12f44f46 | 1147 | if (!pass) { |
619c8c31 | 1148 | if (pcibios_assign_all_busses() || broken || is_cardbus) |
3e466e2d BH |
1149 | |
1150 | /* | |
1151 | * Temporarily disable forwarding of the | |
1152 | * configuration cycles on all bridges in | |
1153 | * this bus segment to avoid possible | |
1154 | * conflicts in the second pass between two | |
1155 | * bridges programmed with overlapping bus | |
1156 | * ranges. | |
1157 | */ | |
12f44f46 IK |
1158 | pci_write_config_dword(dev, PCI_PRIMARY_BUS, |
1159 | buses & ~0xffffff); | |
bbe8f9a3 | 1160 | goto out; |
12f44f46 | 1161 | } |
1da177e4 LT |
1162 | |
1163 | /* Clear errors */ | |
1164 | pci_write_config_word(dev, PCI_STATUS, 0xffff); | |
1165 | ||
3e466e2d BH |
1166 | /* |
1167 | * Prevent assigning a bus number that already exists. | |
1168 | * This can happen when a bridge is hot-plugged, so in this | |
1169 | * case we only re-scan this bus. | |
1170 | */ | |
b1a98b69 TC |
1171 | child = pci_find_bus(pci_domain_nr(bus), max+1); |
1172 | if (!child) { | |
9a4d7d87 | 1173 | child = pci_add_new_bus(bus, dev, max+1); |
b1a98b69 TC |
1174 | if (!child) |
1175 | goto out; | |
a20c7f36 MW |
1176 | pci_bus_insert_busn_res(child, max+1, |
1177 | bus->busn_res.end); | |
b1a98b69 | 1178 | } |
9a4d7d87 | 1179 | max++; |
1c02ea81 MW |
1180 | if (available_buses) |
1181 | available_buses--; | |
1182 | ||
1da177e4 LT |
1183 | buses = (buses & 0xff000000) |
1184 | | ((unsigned int)(child->primary) << 0) | |
b918c62e YL |
1185 | | ((unsigned int)(child->busn_res.start) << 8) |
1186 | | ((unsigned int)(child->busn_res.end) << 16); | |
1da177e4 LT |
1187 | |
1188 | /* | |
1189 | * yenta.c forces a secondary latency timer of 176. | |
1190 | * Copy that behaviour here. | |
1191 | */ | |
1192 | if (is_cardbus) { | |
1193 | buses &= ~0xff000000; | |
1194 | buses |= CARDBUS_LATENCY_TIMER << 24; | |
1195 | } | |
7c867c88 | 1196 | |
3e466e2d | 1197 | /* We need to blast all three values with a single write */ |
1da177e4 LT |
1198 | pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses); |
1199 | ||
1200 | if (!is_cardbus) { | |
11949255 | 1201 | child->bridge_ctl = bctl; |
1c02ea81 | 1202 | max = pci_scan_child_bus_extend(child, available_buses); |
1da177e4 | 1203 | } else { |
3e466e2d | 1204 | |
1da177e4 | 1205 | /* |
3e466e2d BH |
1206 | * For CardBus bridges, we leave 4 bus numbers as |
1207 | * cards with a PCI-to-PCI bridge can be inserted | |
1208 | * later. | |
1da177e4 | 1209 | */ |
3c78bc61 | 1210 | for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) { |
49887941 | 1211 | struct pci_bus *parent = bus; |
cc57450f RS |
1212 | if (pci_find_bus(pci_domain_nr(bus), |
1213 | max+i+1)) | |
1214 | break; | |
49887941 DB |
1215 | while (parent->parent) { |
1216 | if ((!pcibios_assign_all_busses()) && | |
b918c62e YL |
1217 | (parent->busn_res.end > max) && |
1218 | (parent->busn_res.end <= max+i)) { | |
49887941 DB |
1219 | j = 1; |
1220 | } | |
1221 | parent = parent->parent; | |
1222 | } | |
1223 | if (j) { | |
3e466e2d | 1224 | |
49887941 | 1225 | /* |
3e466e2d BH |
1226 | * Often, there are two CardBus |
1227 | * bridges -- try to leave one | |
1228 | * valid bus number for each one. | |
49887941 DB |
1229 | */ |
1230 | i /= 2; | |
1231 | break; | |
1232 | } | |
1233 | } | |
cc57450f | 1234 | max += i; |
1da177e4 | 1235 | } |
3e466e2d BH |
1236 | |
1237 | /* Set subordinate bus number to its real value */ | |
bc76b731 | 1238 | pci_bus_update_busn_res_end(child, max); |
1da177e4 LT |
1239 | pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max); |
1240 | } | |
1241 | ||
cb3576fa GH |
1242 | sprintf(child->name, |
1243 | (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"), | |
1244 | pci_domain_nr(bus), child->number); | |
1da177e4 | 1245 | |
e412d63d | 1246 | /* Check that all devices are accessible */ |
49887941 | 1247 | while (bus->parent) { |
b918c62e YL |
1248 | if ((child->busn_res.end > bus->busn_res.end) || |
1249 | (child->number > bus->busn_res.end) || | |
49887941 | 1250 | (child->number < bus->number) || |
b918c62e | 1251 | (child->busn_res.end < bus->number)) { |
e412d63d MW |
1252 | dev_info(&dev->dev, "devices behind bridge are unusable because %pR cannot be assigned for them\n", |
1253 | &child->busn_res); | |
1254 | break; | |
49887941 DB |
1255 | } |
1256 | bus = bus->parent; | |
1257 | } | |
1258 | ||
bbe8f9a3 RB |
1259 | out: |
1260 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl); | |
1261 | ||
d963f651 MW |
1262 | pm_runtime_put(&dev->dev); |
1263 | ||
1da177e4 LT |
1264 | return max; |
1265 | } | |
1c02ea81 MW |
1266 | |
1267 | /* | |
1268 | * pci_scan_bridge() - Scan buses behind a bridge | |
1269 | * @bus: Parent bus the bridge is on | |
1270 | * @dev: Bridge itself | |
1271 | * @max: Starting subordinate number of buses behind this bridge | |
1272 | * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges | |
1273 | * that need to be reconfigured. | |
1274 | * | |
1275 | * If it's a bridge, configure it and scan the bus behind it. | |
1276 | * For CardBus bridges, we don't scan behind as the devices will | |
1277 | * be handled by the bridge driver itself. | |
1278 | * | |
1279 | * We need to process bridges in two passes -- first we scan those | |
1280 | * already configured by the BIOS and after we are done with all of | |
1281 | * them, we proceed to assigning numbers to the remaining buses in | |
1282 | * order to avoid overlaps between old and new bus numbers. | |
70f7880d MW |
1283 | * |
1284 | * Return: New subordinate number covering all buses behind this bridge. | |
1c02ea81 MW |
1285 | */ |
1286 | int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass) | |
1287 | { | |
1288 | return pci_scan_bridge_extend(bus, dev, max, 0, pass); | |
1289 | } | |
b7fe9434 | 1290 | EXPORT_SYMBOL(pci_scan_bridge); |
1da177e4 LT |
1291 | |
1292 | /* | |
1293 | * Read interrupt line and base address registers. | |
1294 | * The architecture-dependent code can tweak these, of course. | |
1295 | */ | |
1296 | static void pci_read_irq(struct pci_dev *dev) | |
1297 | { | |
1298 | unsigned char irq; | |
1299 | ||
be20f6b0 KA |
1300 | /* VFs are not allowed to use INTx, so skip the config reads */ |
1301 | if (dev->is_virtfn) { | |
1302 | dev->pin = 0; | |
1303 | dev->irq = 0; | |
1304 | return; | |
1305 | } | |
1306 | ||
1da177e4 | 1307 | pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq); |
ffeff788 | 1308 | dev->pin = irq; |
1da177e4 LT |
1309 | if (irq) |
1310 | pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq); | |
1311 | dev->irq = irq; | |
1312 | } | |
1313 | ||
bb209c82 | 1314 | void set_pcie_port_type(struct pci_dev *pdev) |
480b93b7 YZ |
1315 | { |
1316 | int pos; | |
1317 | u16 reg16; | |
d0751b98 YW |
1318 | int type; |
1319 | struct pci_dev *parent; | |
480b93b7 YZ |
1320 | |
1321 | pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); | |
1322 | if (!pos) | |
1323 | return; | |
51ebfc92 | 1324 | |
0efea000 | 1325 | pdev->pcie_cap = pos; |
480b93b7 | 1326 | pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16); |
786e2288 | 1327 | pdev->pcie_flags_reg = reg16; |
b03e7495 JM |
1328 | pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, ®16); |
1329 | pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD; | |
d0751b98 YW |
1330 | |
1331 | /* | |
51ebfc92 BH |
1332 | * A Root Port or a PCI-to-PCIe bridge is always the upstream end |
1333 | * of a Link. No PCIe component has two Links. Two Links are | |
1334 | * connected by a Switch that has a Port on each Link and internal | |
1335 | * logic to connect the two Ports. | |
d0751b98 YW |
1336 | */ |
1337 | type = pci_pcie_type(pdev); | |
51ebfc92 BH |
1338 | if (type == PCI_EXP_TYPE_ROOT_PORT || |
1339 | type == PCI_EXP_TYPE_PCIE_BRIDGE) | |
d0751b98 YW |
1340 | pdev->has_secondary_link = 1; |
1341 | else if (type == PCI_EXP_TYPE_UPSTREAM || | |
1342 | type == PCI_EXP_TYPE_DOWNSTREAM) { | |
1343 | parent = pci_upstream_bridge(pdev); | |
b35b1df5 YW |
1344 | |
1345 | /* | |
1346 | * Usually there's an upstream device (Root Port or Switch | |
1347 | * Downstream Port), but we can't assume one exists. | |
1348 | */ | |
1349 | if (parent && !parent->has_secondary_link) | |
d0751b98 YW |
1350 | pdev->has_secondary_link = 1; |
1351 | } | |
480b93b7 YZ |
1352 | } |
1353 | ||
bb209c82 | 1354 | void set_pcie_hotplug_bridge(struct pci_dev *pdev) |
28760489 | 1355 | { |
28760489 EB |
1356 | u32 reg32; |
1357 | ||
59875ae4 | 1358 | pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32); |
28760489 EB |
1359 | if (reg32 & PCI_EXP_SLTCAP_HPC) |
1360 | pdev->is_hotplug_bridge = 1; | |
1361 | } | |
1362 | ||
8531e283 LW |
1363 | static void set_pcie_thunderbolt(struct pci_dev *dev) |
1364 | { | |
1365 | int vsec = 0; | |
1366 | u32 header; | |
1367 | ||
1368 | while ((vsec = pci_find_next_ext_capability(dev, vsec, | |
1369 | PCI_EXT_CAP_ID_VNDR))) { | |
1370 | pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header); | |
1371 | ||
1372 | /* Is the device part of a Thunderbolt controller? */ | |
1373 | if (dev->vendor == PCI_VENDOR_ID_INTEL && | |
1374 | PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) { | |
1375 | dev->is_thunderbolt = 1; | |
1376 | return; | |
1377 | } | |
1378 | } | |
1379 | } | |
1380 | ||
78916b00 | 1381 | /** |
3e466e2d | 1382 | * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config? |
78916b00 AW |
1383 | * @dev: PCI device |
1384 | * | |
1385 | * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that | |
1386 | * when forwarding a type1 configuration request the bridge must check that | |
1387 | * the extended register address field is zero. The bridge is not permitted | |
1388 | * to forward the transactions and must handle it as an Unsupported Request. | |
1389 | * Some bridges do not follow this rule and simply drop the extended register | |
1390 | * bits, resulting in the standard config space being aliased, every 256 | |
1391 | * bytes across the entire configuration space. Test for this condition by | |
1392 | * comparing the first dword of each potential alias to the vendor/device ID. | |
1393 | * Known offenders: | |
1394 | * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03) | |
1395 | * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40) | |
1396 | */ | |
1397 | static bool pci_ext_cfg_is_aliased(struct pci_dev *dev) | |
1398 | { | |
1399 | #ifdef CONFIG_PCI_QUIRKS | |
1400 | int pos; | |
1401 | u32 header, tmp; | |
1402 | ||
1403 | pci_read_config_dword(dev, PCI_VENDOR_ID, &header); | |
1404 | ||
1405 | for (pos = PCI_CFG_SPACE_SIZE; | |
1406 | pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) { | |
1407 | if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL | |
1408 | || header != tmp) | |
1409 | return false; | |
1410 | } | |
1411 | ||
1412 | return true; | |
1413 | #else | |
1414 | return false; | |
1415 | #endif | |
1416 | } | |
1417 | ||
0b950f0f | 1418 | /** |
3e466e2d | 1419 | * pci_cfg_space_size - Get the configuration space size of the PCI device |
0b950f0f SH |
1420 | * @dev: PCI device |
1421 | * | |
1422 | * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices | |
1423 | * have 4096 bytes. Even if the device is capable, that doesn't mean we can | |
1424 | * access it. Maybe we don't have a way to generate extended config space | |
1425 | * accesses, or the device is behind a reverse Express bridge. So we try | |
1426 | * reading the dword at 0x100 which must either be 0 or a valid extended | |
1427 | * capability header. | |
1428 | */ | |
1429 | static int pci_cfg_space_size_ext(struct pci_dev *dev) | |
1430 | { | |
1431 | u32 status; | |
1432 | int pos = PCI_CFG_SPACE_SIZE; | |
1433 | ||
1434 | if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL) | |
8e5a395a | 1435 | return PCI_CFG_SPACE_SIZE; |
78916b00 | 1436 | if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev)) |
8e5a395a | 1437 | return PCI_CFG_SPACE_SIZE; |
0b950f0f SH |
1438 | |
1439 | return PCI_CFG_SPACE_EXP_SIZE; | |
0b950f0f SH |
1440 | } |
1441 | ||
1442 | int pci_cfg_space_size(struct pci_dev *dev) | |
1443 | { | |
1444 | int pos; | |
1445 | u32 status; | |
1446 | u16 class; | |
1447 | ||
17e8f0d4 GB |
1448 | if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG) |
1449 | return PCI_CFG_SPACE_SIZE; | |
1450 | ||
0b950f0f SH |
1451 | class = dev->class >> 8; |
1452 | if (class == PCI_CLASS_BRIDGE_HOST) | |
1453 | return pci_cfg_space_size_ext(dev); | |
1454 | ||
8e5a395a BH |
1455 | if (pci_is_pcie(dev)) |
1456 | return pci_cfg_space_size_ext(dev); | |
0b950f0f | 1457 | |
8e5a395a BH |
1458 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
1459 | if (!pos) | |
1460 | return PCI_CFG_SPACE_SIZE; | |
0b950f0f | 1461 | |
8e5a395a BH |
1462 | pci_read_config_dword(dev, pos + PCI_X_STATUS, &status); |
1463 | if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)) | |
1464 | return pci_cfg_space_size_ext(dev); | |
0b950f0f | 1465 | |
0b950f0f SH |
1466 | return PCI_CFG_SPACE_SIZE; |
1467 | } | |
1468 | ||
cf0921be KA |
1469 | static u32 pci_class(struct pci_dev *dev) |
1470 | { | |
1471 | u32 class; | |
1472 | ||
1473 | #ifdef CONFIG_PCI_IOV | |
1474 | if (dev->is_virtfn) | |
1475 | return dev->physfn->sriov->class; | |
1476 | #endif | |
1477 | pci_read_config_dword(dev, PCI_CLASS_REVISION, &class); | |
1478 | return class; | |
1479 | } | |
1480 | ||
1481 | static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device) | |
1482 | { | |
1483 | #ifdef CONFIG_PCI_IOV | |
1484 | if (dev->is_virtfn) { | |
1485 | *vendor = dev->physfn->sriov->subsystem_vendor; | |
1486 | *device = dev->physfn->sriov->subsystem_device; | |
1487 | return; | |
1488 | } | |
1489 | #endif | |
1490 | pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor); | |
1491 | pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device); | |
1492 | } | |
1493 | ||
1494 | static u8 pci_hdr_type(struct pci_dev *dev) | |
1495 | { | |
1496 | u8 hdr_type; | |
1497 | ||
1498 | #ifdef CONFIG_PCI_IOV | |
1499 | if (dev->is_virtfn) | |
1500 | return dev->physfn->sriov->hdr_type; | |
1501 | #endif | |
1502 | pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type); | |
1503 | return hdr_type; | |
1504 | } | |
1505 | ||
01abc2aa | 1506 | #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED) |
76e6a1d6 | 1507 | |
e80e7edc | 1508 | static void pci_msi_setup_pci_dev(struct pci_dev *dev) |
1851617c MT |
1509 | { |
1510 | /* | |
1511 | * Disable the MSI hardware to avoid screaming interrupts | |
1512 | * during boot. This is the power on reset default so | |
1513 | * usually this should be a noop. | |
1514 | */ | |
1515 | dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI); | |
1516 | if (dev->msi_cap) | |
1517 | pci_msi_set_enable(dev, 0); | |
1518 | ||
1519 | dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX); | |
1520 | if (dev->msix_cap) | |
1521 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); | |
1522 | } | |
1523 | ||
99b3c58f | 1524 | /** |
3e466e2d | 1525 | * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability |
99b3c58f PG |
1526 | * @dev: PCI device |
1527 | * | |
1528 | * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this | |
1529 | * at enumeration-time to avoid modifying PCI_COMMAND at run-time. | |
1530 | */ | |
1531 | static int pci_intx_mask_broken(struct pci_dev *dev) | |
1532 | { | |
1533 | u16 orig, toggle, new; | |
1534 | ||
1535 | pci_read_config_word(dev, PCI_COMMAND, &orig); | |
1536 | toggle = orig ^ PCI_COMMAND_INTX_DISABLE; | |
1537 | pci_write_config_word(dev, PCI_COMMAND, toggle); | |
1538 | pci_read_config_word(dev, PCI_COMMAND, &new); | |
1539 | ||
1540 | pci_write_config_word(dev, PCI_COMMAND, orig); | |
1541 | ||
1542 | /* | |
1543 | * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI | |
1544 | * r2.3, so strictly speaking, a device is not *broken* if it's not | |
1545 | * writable. But we'll live with the misnomer for now. | |
1546 | */ | |
1547 | if (new != toggle) | |
1548 | return 1; | |
1549 | return 0; | |
1550 | } | |
1551 | ||
11eb0e0e SK |
1552 | static void early_dump_pci_device(struct pci_dev *pdev) |
1553 | { | |
1554 | u32 value[256 / 4]; | |
1555 | int i; | |
1556 | ||
1557 | pci_info(pdev, "config space:\n"); | |
1558 | ||
1559 | for (i = 0; i < 256; i += 4) | |
1560 | pci_read_config_dword(pdev, i, &value[i / 4]); | |
1561 | ||
1562 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, | |
1563 | value, 256, false); | |
1564 | } | |
1565 | ||
1da177e4 | 1566 | /** |
3e466e2d | 1567 | * pci_setup_device - Fill in class and map information of a device |
1da177e4 LT |
1568 | * @dev: the device structure to fill |
1569 | * | |
f7625980 | 1570 | * Initialize the device structure with information about the device's |
3e466e2d | 1571 | * vendor,class,memory and IO-space addresses, IRQ lines etc. |
1da177e4 | 1572 | * Called at initialisation of the PCI subsystem and by CardBus services. |
480b93b7 YZ |
1573 | * Returns 0 on success and negative if unknown type of device (not normal, |
1574 | * bridge or CardBus). | |
1da177e4 | 1575 | */ |
480b93b7 | 1576 | int pci_setup_device(struct pci_dev *dev) |
1da177e4 LT |
1577 | { |
1578 | u32 class; | |
b84106b4 | 1579 | u16 cmd; |
480b93b7 | 1580 | u8 hdr_type; |
bc577d2b | 1581 | int pos = 0; |
5bfa14ed BH |
1582 | struct pci_bus_region region; |
1583 | struct resource *res; | |
480b93b7 | 1584 | |
cf0921be | 1585 | hdr_type = pci_hdr_type(dev); |
480b93b7 YZ |
1586 | |
1587 | dev->sysdata = dev->bus->sysdata; | |
1588 | dev->dev.parent = dev->bus->bridge; | |
1589 | dev->dev.bus = &pci_bus_type; | |
1590 | dev->hdr_type = hdr_type & 0x7f; | |
1591 | dev->multifunction = !!(hdr_type & 0x80); | |
480b93b7 YZ |
1592 | dev->error_state = pci_channel_io_normal; |
1593 | set_pcie_port_type(dev); | |
1594 | ||
017ffe64 | 1595 | pci_dev_assign_slot(dev); |
3e466e2d BH |
1596 | |
1597 | /* | |
1598 | * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer) | |
1599 | * set this higher, assuming the system even supports it. | |
1600 | */ | |
480b93b7 | 1601 | dev->dma_mask = 0xffffffff; |
1da177e4 | 1602 | |
eebfcfb5 GKH |
1603 | dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus), |
1604 | dev->bus->number, PCI_SLOT(dev->devfn), | |
1605 | PCI_FUNC(dev->devfn)); | |
1da177e4 | 1606 | |
cf0921be KA |
1607 | class = pci_class(dev); |
1608 | ||
b8a3a521 | 1609 | dev->revision = class & 0xff; |
2dd8ba92 | 1610 | dev->class = class >> 8; /* upper 3 bytes */ |
1da177e4 | 1611 | |
7506dc79 | 1612 | pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n", |
2dd8ba92 | 1613 | dev->vendor, dev->device, dev->hdr_type, dev->class); |
1da177e4 | 1614 | |
11eb0e0e SK |
1615 | if (pci_early_dump) |
1616 | early_dump_pci_device(dev); | |
1617 | ||
3e466e2d | 1618 | /* Need to have dev->class ready */ |
853346e4 YZ |
1619 | dev->cfg_size = pci_cfg_space_size(dev); |
1620 | ||
3e466e2d | 1621 | /* Need to have dev->cfg_size ready */ |
8531e283 LW |
1622 | set_pcie_thunderbolt(dev); |
1623 | ||
1da177e4 | 1624 | /* "Unknown power state" */ |
3fe9d19f | 1625 | dev->current_state = PCI_UNKNOWN; |
1da177e4 LT |
1626 | |
1627 | /* Early fixups, before probing the BARs */ | |
1628 | pci_fixup_device(pci_fixup_early, dev); | |
3e466e2d BH |
1629 | |
1630 | /* Device class may be changed after fixup */ | |
f79b1b14 | 1631 | class = dev->class >> 8; |
1da177e4 | 1632 | |
b84106b4 BH |
1633 | if (dev->non_compliant_bars) { |
1634 | pci_read_config_word(dev, PCI_COMMAND, &cmd); | |
1635 | if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) { | |
7506dc79 | 1636 | pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n"); |
b84106b4 BH |
1637 | cmd &= ~PCI_COMMAND_IO; |
1638 | cmd &= ~PCI_COMMAND_MEMORY; | |
1639 | pci_write_config_word(dev, PCI_COMMAND, cmd); | |
1640 | } | |
1641 | } | |
1642 | ||
99b3c58f PG |
1643 | dev->broken_intx_masking = pci_intx_mask_broken(dev); |
1644 | ||
1da177e4 LT |
1645 | switch (dev->hdr_type) { /* header type */ |
1646 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ | |
1647 | if (class == PCI_CLASS_BRIDGE_PCI) | |
1648 | goto bad; | |
1649 | pci_read_irq(dev); | |
1650 | pci_read_bases(dev, 6, PCI_ROM_ADDRESS); | |
cf0921be KA |
1651 | |
1652 | pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device); | |
368c73d4 AC |
1653 | |
1654 | /* | |
075eb9e3 BH |
1655 | * Do the ugly legacy mode stuff here rather than broken chip |
1656 | * quirk code. Legacy mode ATA controllers have fixed | |
1657 | * addresses. These are not always echoed in BAR0-3, and | |
1658 | * BAR0-3 in a few cases contain junk! | |
368c73d4 AC |
1659 | */ |
1660 | if (class == PCI_CLASS_STORAGE_IDE) { | |
1661 | u8 progif; | |
1662 | pci_read_config_byte(dev, PCI_CLASS_PROG, &progif); | |
1663 | if ((progif & 1) == 0) { | |
5bfa14ed BH |
1664 | region.start = 0x1F0; |
1665 | region.end = 0x1F7; | |
1666 | res = &dev->resource[0]; | |
1667 | res->flags = LEGACY_IO_RESOURCE; | |
fc279850 | 1668 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 1669 | pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n", |
075eb9e3 | 1670 | res); |
5bfa14ed BH |
1671 | region.start = 0x3F6; |
1672 | region.end = 0x3F6; | |
1673 | res = &dev->resource[1]; | |
1674 | res->flags = LEGACY_IO_RESOURCE; | |
fc279850 | 1675 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 1676 | pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n", |
075eb9e3 | 1677 | res); |
368c73d4 AC |
1678 | } |
1679 | if ((progif & 4) == 0) { | |
5bfa14ed BH |
1680 | region.start = 0x170; |
1681 | region.end = 0x177; | |
1682 | res = &dev->resource[2]; | |
1683 | res->flags = LEGACY_IO_RESOURCE; | |
fc279850 | 1684 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 1685 | pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n", |
075eb9e3 | 1686 | res); |
5bfa14ed BH |
1687 | region.start = 0x376; |
1688 | region.end = 0x376; | |
1689 | res = &dev->resource[3]; | |
1690 | res->flags = LEGACY_IO_RESOURCE; | |
fc279850 | 1691 | pcibios_bus_to_resource(dev->bus, res, ®ion); |
7506dc79 | 1692 | pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n", |
075eb9e3 | 1693 | res); |
368c73d4 AC |
1694 | } |
1695 | } | |
1da177e4 LT |
1696 | break; |
1697 | ||
1698 | case PCI_HEADER_TYPE_BRIDGE: /* bridge header */ | |
1699 | if (class != PCI_CLASS_BRIDGE_PCI) | |
1700 | goto bad; | |
3e466e2d BH |
1701 | |
1702 | /* | |
1703 | * The PCI-to-PCI bridge spec requires that subtractive | |
1704 | * decoding (i.e. transparent) bridge must have programming | |
1705 | * interface code of 0x01. | |
1706 | */ | |
3efd273b | 1707 | pci_read_irq(dev); |
1da177e4 LT |
1708 | dev->transparent = ((dev->class & 0xff) == 1); |
1709 | pci_read_bases(dev, 2, PCI_ROM_ADDRESS1); | |
28760489 | 1710 | set_pcie_hotplug_bridge(dev); |
bc577d2b GB |
1711 | pos = pci_find_capability(dev, PCI_CAP_ID_SSVID); |
1712 | if (pos) { | |
1713 | pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor); | |
1714 | pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device); | |
1715 | } | |
1da177e4 LT |
1716 | break; |
1717 | ||
1718 | case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */ | |
1719 | if (class != PCI_CLASS_BRIDGE_CARDBUS) | |
1720 | goto bad; | |
1721 | pci_read_irq(dev); | |
1722 | pci_read_bases(dev, 1, 0); | |
1723 | pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor); | |
1724 | pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device); | |
1725 | break; | |
1726 | ||
1727 | default: /* unknown header */ | |
7506dc79 | 1728 | pci_err(dev, "unknown header type %02x, ignoring device\n", |
227f0647 | 1729 | dev->hdr_type); |
480b93b7 | 1730 | return -EIO; |
1da177e4 LT |
1731 | |
1732 | bad: | |
7506dc79 | 1733 | pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n", |
227f0647 | 1734 | dev->class, dev->hdr_type); |
2b4aed1d | 1735 | dev->class = PCI_CLASS_NOT_DEFINED << 8; |
1da177e4 LT |
1736 | } |
1737 | ||
1738 | /* We found a fine healthy device, go go go... */ | |
1739 | return 0; | |
1740 | } | |
1741 | ||
9dae3a97 BH |
1742 | static void pci_configure_mps(struct pci_dev *dev) |
1743 | { | |
1744 | struct pci_dev *bridge = pci_upstream_bridge(dev); | |
27d868b5 | 1745 | int mps, p_mps, rc; |
9dae3a97 BH |
1746 | |
1747 | if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge)) | |
1748 | return; | |
1749 | ||
1750 | mps = pcie_get_mps(dev); | |
1751 | p_mps = pcie_get_mps(bridge); | |
1752 | ||
1753 | if (mps == p_mps) | |
1754 | return; | |
1755 | ||
1756 | if (pcie_bus_config == PCIE_BUS_TUNE_OFF) { | |
7506dc79 | 1757 | pci_warn(dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n", |
9dae3a97 BH |
1758 | mps, pci_name(bridge), p_mps); |
1759 | return; | |
1760 | } | |
27d868b5 KB |
1761 | |
1762 | /* | |
1763 | * Fancier MPS configuration is done later by | |
1764 | * pcie_bus_configure_settings() | |
1765 | */ | |
1766 | if (pcie_bus_config != PCIE_BUS_DEFAULT) | |
1767 | return; | |
1768 | ||
1769 | rc = pcie_set_mps(dev, p_mps); | |
1770 | if (rc) { | |
7506dc79 | 1771 | pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n", |
27d868b5 KB |
1772 | p_mps); |
1773 | return; | |
1774 | } | |
1775 | ||
7506dc79 | 1776 | pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n", |
27d868b5 | 1777 | p_mps, mps, 128 << dev->pcie_mpss); |
9dae3a97 BH |
1778 | } |
1779 | ||
589fcc23 BH |
1780 | static struct hpp_type0 pci_default_type0 = { |
1781 | .revision = 1, | |
1782 | .cache_line_size = 8, | |
1783 | .latency_timer = 0x40, | |
1784 | .enable_serr = 0, | |
1785 | .enable_perr = 0, | |
1786 | }; | |
1787 | ||
1788 | static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp) | |
1789 | { | |
1790 | u16 pci_cmd, pci_bctl; | |
1791 | ||
c6285fc5 | 1792 | if (!hpp) |
589fcc23 | 1793 | hpp = &pci_default_type0; |
589fcc23 BH |
1794 | |
1795 | if (hpp->revision > 1) { | |
7506dc79 | 1796 | pci_warn(dev, "PCI settings rev %d not supported; using defaults\n", |
589fcc23 BH |
1797 | hpp->revision); |
1798 | hpp = &pci_default_type0; | |
1799 | } | |
1800 | ||
1801 | pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size); | |
1802 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer); | |
1803 | pci_read_config_word(dev, PCI_COMMAND, &pci_cmd); | |
1804 | if (hpp->enable_serr) | |
1805 | pci_cmd |= PCI_COMMAND_SERR; | |
589fcc23 BH |
1806 | if (hpp->enable_perr) |
1807 | pci_cmd |= PCI_COMMAND_PARITY; | |
589fcc23 BH |
1808 | pci_write_config_word(dev, PCI_COMMAND, pci_cmd); |
1809 | ||
1810 | /* Program bridge control value */ | |
1811 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) { | |
1812 | pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, | |
1813 | hpp->latency_timer); | |
1814 | pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl); | |
1815 | if (hpp->enable_serr) | |
1816 | pci_bctl |= PCI_BRIDGE_CTL_SERR; | |
589fcc23 BH |
1817 | if (hpp->enable_perr) |
1818 | pci_bctl |= PCI_BRIDGE_CTL_PARITY; | |
589fcc23 BH |
1819 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl); |
1820 | } | |
1821 | } | |
1822 | ||
1823 | static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp) | |
1824 | { | |
977509f7 BH |
1825 | int pos; |
1826 | ||
1827 | if (!hpp) | |
1828 | return; | |
1829 | ||
1830 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); | |
1831 | if (!pos) | |
1832 | return; | |
1833 | ||
7506dc79 | 1834 | pci_warn(dev, "PCI-X settings not supported\n"); |
589fcc23 BH |
1835 | } |
1836 | ||
e42010d8 JT |
1837 | static bool pcie_root_rcb_set(struct pci_dev *dev) |
1838 | { | |
1839 | struct pci_dev *rp = pcie_find_root_port(dev); | |
1840 | u16 lnkctl; | |
1841 | ||
1842 | if (!rp) | |
1843 | return false; | |
1844 | ||
1845 | pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl); | |
1846 | if (lnkctl & PCI_EXP_LNKCTL_RCB) | |
1847 | return true; | |
1848 | ||
1849 | return false; | |
1850 | } | |
1851 | ||
589fcc23 BH |
1852 | static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp) |
1853 | { | |
1854 | int pos; | |
1855 | u32 reg32; | |
1856 | ||
1857 | if (!hpp) | |
1858 | return; | |
1859 | ||
977509f7 BH |
1860 | if (!pci_is_pcie(dev)) |
1861 | return; | |
1862 | ||
589fcc23 | 1863 | if (hpp->revision > 1) { |
7506dc79 | 1864 | pci_warn(dev, "PCIe settings rev %d not supported\n", |
589fcc23 BH |
1865 | hpp->revision); |
1866 | return; | |
1867 | } | |
1868 | ||
302328c0 BH |
1869 | /* |
1870 | * Don't allow _HPX to change MPS or MRRS settings. We manage | |
1871 | * those to make sure they're consistent with the rest of the | |
1872 | * platform. | |
1873 | */ | |
1874 | hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD | | |
1875 | PCI_EXP_DEVCTL_READRQ; | |
1876 | hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD | | |
1877 | PCI_EXP_DEVCTL_READRQ); | |
1878 | ||
589fcc23 BH |
1879 | /* Initialize Device Control Register */ |
1880 | pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL, | |
1881 | ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or); | |
1882 | ||
1883 | /* Initialize Link Control Register */ | |
e42010d8 JT |
1884 | if (pcie_cap_has_lnkctl(dev)) { |
1885 | ||
1886 | /* | |
1887 | * If the Root Port supports Read Completion Boundary of | |
1888 | * 128, set RCB to 128. Otherwise, clear it. | |
1889 | */ | |
1890 | hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB; | |
1891 | hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB; | |
1892 | if (pcie_root_rcb_set(dev)) | |
1893 | hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB; | |
1894 | ||
589fcc23 BH |
1895 | pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL, |
1896 | ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or); | |
e42010d8 | 1897 | } |
589fcc23 BH |
1898 | |
1899 | /* Find Advanced Error Reporting Enhanced Capability */ | |
1900 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); | |
1901 | if (!pos) | |
1902 | return; | |
1903 | ||
1904 | /* Initialize Uncorrectable Error Mask Register */ | |
1905 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, ®32); | |
1906 | reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or; | |
1907 | pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32); | |
1908 | ||
1909 | /* Initialize Uncorrectable Error Severity Register */ | |
1910 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, ®32); | |
1911 | reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or; | |
1912 | pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32); | |
1913 | ||
1914 | /* Initialize Correctable Error Mask Register */ | |
1915 | pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, ®32); | |
1916 | reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or; | |
1917 | pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32); | |
1918 | ||
1919 | /* Initialize Advanced Error Capabilities and Control Register */ | |
1920 | pci_read_config_dword(dev, pos + PCI_ERR_CAP, ®32); | |
1921 | reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or; | |
3e466e2d | 1922 | |
675734ba BH |
1923 | /* Don't enable ECRC generation or checking if unsupported */ |
1924 | if (!(reg32 & PCI_ERR_CAP_ECRC_GENC)) | |
1925 | reg32 &= ~PCI_ERR_CAP_ECRC_GENE; | |
1926 | if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC)) | |
1927 | reg32 &= ~PCI_ERR_CAP_ECRC_CHKE; | |
589fcc23 BH |
1928 | pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32); |
1929 | ||
1930 | /* | |
1931 | * FIXME: The following two registers are not supported yet. | |
1932 | * | |
1933 | * o Secondary Uncorrectable Error Severity Register | |
1934 | * o Secondary Uncorrectable Error Mask Register | |
1935 | */ | |
1936 | } | |
1937 | ||
62ce94a7 | 1938 | int pci_configure_extended_tags(struct pci_dev *dev, void *ign) |
60db3a4d | 1939 | { |
62ce94a7 SK |
1940 | struct pci_host_bridge *host; |
1941 | u32 cap; | |
1942 | u16 ctl; | |
60db3a4d SK |
1943 | int ret; |
1944 | ||
1945 | if (!pci_is_pcie(dev)) | |
62ce94a7 | 1946 | return 0; |
60db3a4d | 1947 | |
62ce94a7 | 1948 | ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap); |
60db3a4d | 1949 | if (ret) |
62ce94a7 SK |
1950 | return 0; |
1951 | ||
1952 | if (!(cap & PCI_EXP_DEVCAP_EXT_TAG)) | |
1953 | return 0; | |
60db3a4d | 1954 | |
62ce94a7 SK |
1955 | ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl); |
1956 | if (ret) | |
1957 | return 0; | |
1958 | ||
1959 | host = pci_find_host_bridge(dev->bus); | |
1960 | if (!host) | |
1961 | return 0; | |
60db3a4d | 1962 | |
62ce94a7 SK |
1963 | /* |
1964 | * If some device in the hierarchy doesn't handle Extended Tags | |
1965 | * correctly, make sure they're disabled. | |
1966 | */ | |
1967 | if (host->no_ext_tags) { | |
1968 | if (ctl & PCI_EXP_DEVCTL_EXT_TAG) { | |
7506dc79 | 1969 | pci_info(dev, "disabling Extended Tags\n"); |
62ce94a7 SK |
1970 | pcie_capability_clear_word(dev, PCI_EXP_DEVCTL, |
1971 | PCI_EXP_DEVCTL_EXT_TAG); | |
1972 | } | |
1973 | return 0; | |
1974 | } | |
1975 | ||
1976 | if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) { | |
7506dc79 | 1977 | pci_info(dev, "enabling Extended Tags\n"); |
60db3a4d SK |
1978 | pcie_capability_set_word(dev, PCI_EXP_DEVCTL, |
1979 | PCI_EXP_DEVCTL_EXT_TAG); | |
62ce94a7 SK |
1980 | } |
1981 | return 0; | |
60db3a4d SK |
1982 | } |
1983 | ||
a99b646a | 1984 | /** |
1985 | * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable | |
1986 | * @dev: PCI device to query | |
1987 | * | |
1988 | * Returns true if the device has enabled relaxed ordering attribute. | |
1989 | */ | |
1990 | bool pcie_relaxed_ordering_enabled(struct pci_dev *dev) | |
1991 | { | |
1992 | u16 v; | |
1993 | ||
1994 | pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v); | |
1995 | ||
1996 | return !!(v & PCI_EXP_DEVCTL_RELAX_EN); | |
1997 | } | |
1998 | EXPORT_SYMBOL(pcie_relaxed_ordering_enabled); | |
1999 | ||
2000 | static void pci_configure_relaxed_ordering(struct pci_dev *dev) | |
2001 | { | |
2002 | struct pci_dev *root; | |
2003 | ||
2004 | /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */ | |
2005 | if (dev->is_virtfn) | |
2006 | return; | |
2007 | ||
2008 | if (!pcie_relaxed_ordering_enabled(dev)) | |
2009 | return; | |
2010 | ||
2011 | /* | |
2012 | * For now, we only deal with Relaxed Ordering issues with Root | |
2013 | * Ports. Peer-to-Peer DMA is another can of worms. | |
2014 | */ | |
2015 | root = pci_find_pcie_root_port(dev); | |
2016 | if (!root) | |
2017 | return; | |
2018 | ||
2019 | if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) { | |
2020 | pcie_capability_clear_word(dev, PCI_EXP_DEVCTL, | |
2021 | PCI_EXP_DEVCTL_RELAX_EN); | |
7506dc79 | 2022 | pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n"); |
a99b646a | 2023 | } |
2024 | } | |
2025 | ||
c46fd358 BH |
2026 | static void pci_configure_ltr(struct pci_dev *dev) |
2027 | { | |
2028 | #ifdef CONFIG_PCIEASPM | |
af8bb9f8 | 2029 | struct pci_host_bridge *host = pci_find_host_bridge(dev->bus); |
c46fd358 BH |
2030 | u32 cap; |
2031 | struct pci_dev *bridge; | |
2032 | ||
af8bb9f8 BH |
2033 | if (!host->native_ltr) |
2034 | return; | |
2035 | ||
c46fd358 BH |
2036 | if (!pci_is_pcie(dev)) |
2037 | return; | |
2038 | ||
2039 | pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap); | |
2040 | if (!(cap & PCI_EXP_DEVCAP2_LTR)) | |
2041 | return; | |
2042 | ||
2043 | /* | |
2044 | * Software must not enable LTR in an Endpoint unless the Root | |
2045 | * Complex and all intermediate Switches indicate support for LTR. | |
2046 | * PCIe r3.1, sec 6.18. | |
2047 | */ | |
2048 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) | |
2049 | dev->ltr_path = 1; | |
2050 | else { | |
2051 | bridge = pci_upstream_bridge(dev); | |
2052 | if (bridge && bridge->ltr_path) | |
2053 | dev->ltr_path = 1; | |
2054 | } | |
2055 | ||
2056 | if (dev->ltr_path) | |
2057 | pcie_capability_set_word(dev, PCI_EXP_DEVCTL2, | |
2058 | PCI_EXP_DEVCTL2_LTR_EN); | |
2059 | #endif | |
2060 | } | |
2061 | ||
6cd33649 BH |
2062 | static void pci_configure_device(struct pci_dev *dev) |
2063 | { | |
2064 | struct hotplug_params hpp; | |
2065 | int ret; | |
2066 | ||
9dae3a97 | 2067 | pci_configure_mps(dev); |
62ce94a7 | 2068 | pci_configure_extended_tags(dev, NULL); |
a99b646a | 2069 | pci_configure_relaxed_ordering(dev); |
c46fd358 | 2070 | pci_configure_ltr(dev); |
9dae3a97 | 2071 | |
6cd33649 BH |
2072 | memset(&hpp, 0, sizeof(hpp)); |
2073 | ret = pci_get_hp_params(dev, &hpp); | |
2074 | if (ret) | |
2075 | return; | |
2076 | ||
2077 | program_hpp_type2(dev, hpp.t2); | |
2078 | program_hpp_type1(dev, hpp.t1); | |
2079 | program_hpp_type0(dev, hpp.t0); | |
2080 | } | |
2081 | ||
201de56e ZY |
2082 | static void pci_release_capabilities(struct pci_dev *dev) |
2083 | { | |
2084 | pci_vpd_release(dev); | |
d1b054da | 2085 | pci_iov_release(dev); |
f796841e | 2086 | pci_free_cap_save_buffers(dev); |
201de56e ZY |
2087 | } |
2088 | ||
1da177e4 | 2089 | /** |
3e466e2d BH |
2090 | * pci_release_dev - Free a PCI device structure when all users of it are |
2091 | * finished | |
1da177e4 LT |
2092 | * @dev: device that's been disconnected |
2093 | * | |
3e466e2d | 2094 | * Will be called only by the device core when all users of this PCI device are |
1da177e4 LT |
2095 | * done. |
2096 | */ | |
2097 | static void pci_release_dev(struct device *dev) | |
2098 | { | |
04480094 | 2099 | struct pci_dev *pci_dev; |
1da177e4 | 2100 | |
04480094 | 2101 | pci_dev = to_pci_dev(dev); |
201de56e | 2102 | pci_release_capabilities(pci_dev); |
98d9f30c | 2103 | pci_release_of_node(pci_dev); |
6ae32c53 | 2104 | pcibios_release_device(pci_dev); |
8b1fce04 | 2105 | pci_bus_put(pci_dev->bus); |
782a985d | 2106 | kfree(pci_dev->driver_override); |
338c3149 | 2107 | kfree(pci_dev->dma_alias_mask); |
1da177e4 LT |
2108 | kfree(pci_dev); |
2109 | } | |
2110 | ||
3c6e6ae7 | 2111 | struct pci_dev *pci_alloc_dev(struct pci_bus *bus) |
65891215 ME |
2112 | { |
2113 | struct pci_dev *dev; | |
2114 | ||
2115 | dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL); | |
2116 | if (!dev) | |
2117 | return NULL; | |
2118 | ||
65891215 | 2119 | INIT_LIST_HEAD(&dev->bus_list); |
88e7b167 | 2120 | dev->dev.type = &pci_dev_type; |
3c6e6ae7 | 2121 | dev->bus = pci_bus_get(bus); |
65891215 ME |
2122 | |
2123 | return dev; | |
2124 | } | |
3c6e6ae7 GZ |
2125 | EXPORT_SYMBOL(pci_alloc_dev); |
2126 | ||
62bc6a6f SK |
2127 | static bool pci_bus_crs_vendor_id(u32 l) |
2128 | { | |
2129 | return (l & 0xffff) == 0x0001; | |
2130 | } | |
2131 | ||
6a802ef0 SK |
2132 | static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l, |
2133 | int timeout) | |
1da177e4 | 2134 | { |
1da177e4 LT |
2135 | int delay = 1; |
2136 | ||
6a802ef0 SK |
2137 | if (!pci_bus_crs_vendor_id(*l)) |
2138 | return true; /* not a CRS completion */ | |
1da177e4 | 2139 | |
6a802ef0 SK |
2140 | if (!timeout) |
2141 | return false; /* CRS, but caller doesn't want to wait */ | |
1da177e4 | 2142 | |
89665a6a | 2143 | /* |
6a802ef0 SK |
2144 | * We got the reserved Vendor ID that indicates a completion with |
2145 | * Configuration Request Retry Status (CRS). Retry until we get a | |
2146 | * valid Vendor ID or we time out. | |
89665a6a | 2147 | */ |
62bc6a6f | 2148 | while (pci_bus_crs_vendor_id(*l)) { |
6a802ef0 | 2149 | if (delay > timeout) { |
e78e661f SK |
2150 | pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n", |
2151 | pci_domain_nr(bus), bus->number, | |
2152 | PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1); | |
2153 | ||
efdc87da | 2154 | return false; |
1da177e4 | 2155 | } |
e78e661f SK |
2156 | if (delay >= 1000) |
2157 | pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n", | |
2158 | pci_domain_nr(bus), bus->number, | |
2159 | PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1); | |
efdc87da | 2160 | |
1da177e4 LT |
2161 | msleep(delay); |
2162 | delay *= 2; | |
9f982756 | 2163 | |
efdc87da YL |
2164 | if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l)) |
2165 | return false; | |
1da177e4 LT |
2166 | } |
2167 | ||
e78e661f SK |
2168 | if (delay >= 1000) |
2169 | pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n", | |
2170 | pci_domain_nr(bus), bus->number, | |
2171 | PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1); | |
2172 | ||
efdc87da YL |
2173 | return true; |
2174 | } | |
6a802ef0 SK |
2175 | |
2176 | bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l, | |
2177 | int timeout) | |
2178 | { | |
2179 | if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l)) | |
2180 | return false; | |
2181 | ||
3e466e2d | 2182 | /* Some broken boards return 0 or ~0 if a slot is empty: */ |
6a802ef0 SK |
2183 | if (*l == 0xffffffff || *l == 0x00000000 || |
2184 | *l == 0x0000ffff || *l == 0xffff0000) | |
2185 | return false; | |
2186 | ||
2187 | if (pci_bus_crs_vendor_id(*l)) | |
2188 | return pci_bus_wait_crs(bus, devfn, l, timeout); | |
2189 | ||
efdc87da YL |
2190 | return true; |
2191 | } | |
2192 | EXPORT_SYMBOL(pci_bus_read_dev_vendor_id); | |
2193 | ||
2194 | /* | |
3e466e2d BH |
2195 | * Read the config data for a PCI device, sanity-check it, |
2196 | * and fill in the dev structure. | |
efdc87da YL |
2197 | */ |
2198 | static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn) | |
2199 | { | |
2200 | struct pci_dev *dev; | |
2201 | u32 l; | |
2202 | ||
2203 | if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000)) | |
2204 | return NULL; | |
2205 | ||
8b1fce04 | 2206 | dev = pci_alloc_dev(bus); |
1da177e4 LT |
2207 | if (!dev) |
2208 | return NULL; | |
2209 | ||
1da177e4 | 2210 | dev->devfn = devfn; |
1da177e4 LT |
2211 | dev->vendor = l & 0xffff; |
2212 | dev->device = (l >> 16) & 0xffff; | |
cef354db | 2213 | |
98d9f30c BH |
2214 | pci_set_of_node(dev); |
2215 | ||
480b93b7 | 2216 | if (pci_setup_device(dev)) { |
8b1fce04 | 2217 | pci_bus_put(dev->bus); |
1da177e4 LT |
2218 | kfree(dev); |
2219 | return NULL; | |
2220 | } | |
1da177e4 LT |
2221 | |
2222 | return dev; | |
2223 | } | |
2224 | ||
201de56e ZY |
2225 | static void pci_init_capabilities(struct pci_dev *dev) |
2226 | { | |
938174e5 SS |
2227 | /* Enhanced Allocation */ |
2228 | pci_ea_init(dev); | |
2229 | ||
e80e7edc GP |
2230 | /* Setup MSI caps & disable MSI/MSI-X interrupts */ |
2231 | pci_msi_setup_pci_dev(dev); | |
201de56e | 2232 | |
63f4898a RW |
2233 | /* Buffers for saving PCIe and PCI-X capabilities */ |
2234 | pci_allocate_cap_save_buffers(dev); | |
2235 | ||
201de56e ZY |
2236 | /* Power Management */ |
2237 | pci_pm_init(dev); | |
2238 | ||
2239 | /* Vital Product Data */ | |
f1cd93f9 | 2240 | pci_vpd_init(dev); |
58c3a727 YZ |
2241 | |
2242 | /* Alternative Routing-ID Forwarding */ | |
31ab2476 | 2243 | pci_configure_ari(dev); |
d1b054da YZ |
2244 | |
2245 | /* Single Root I/O Virtualization */ | |
2246 | pci_iov_init(dev); | |
ae21ee65 | 2247 | |
edc90fee BH |
2248 | /* Address Translation Services */ |
2249 | pci_ats_init(dev); | |
2250 | ||
ae21ee65 | 2251 | /* Enable ACS P2P upstream forwarding */ |
5d990b62 | 2252 | pci_enable_acs(dev); |
b07461a8 | 2253 | |
9bb04a0c JY |
2254 | /* Precision Time Measurement */ |
2255 | pci_ptm_init(dev); | |
4dc2db09 | 2256 | |
66b80809 KB |
2257 | /* Advanced Error Reporting */ |
2258 | pci_aer_init(dev); | |
5b0764ca BH |
2259 | |
2260 | if (pci_probe_reset_function(dev) == 0) | |
2261 | dev->reset_fn = 1; | |
201de56e ZY |
2262 | } |
2263 | ||
098259eb | 2264 | /* |
3e466e2d | 2265 | * This is the equivalent of pci_host_bridge_msi_domain() that acts on |
098259eb MZ |
2266 | * devices. Firmware interfaces that can select the MSI domain on a |
2267 | * per-device basis should be called from here. | |
2268 | */ | |
2269 | static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev) | |
2270 | { | |
2271 | struct irq_domain *d; | |
2272 | ||
2273 | /* | |
3e466e2d | 2274 | * If a domain has been set through the pcibios_add_device() |
098259eb MZ |
2275 | * callback, then this is the one (platform code knows best). |
2276 | */ | |
2277 | d = dev_get_msi_domain(&dev->dev); | |
2278 | if (d) | |
2279 | return d; | |
2280 | ||
54fa97ee MZ |
2281 | /* |
2282 | * Let's see if we have a firmware interface able to provide | |
2283 | * the domain. | |
2284 | */ | |
2285 | d = pci_msi_get_device_domain(dev); | |
2286 | if (d) | |
2287 | return d; | |
2288 | ||
098259eb MZ |
2289 | return NULL; |
2290 | } | |
2291 | ||
44aa0c65 MZ |
2292 | static void pci_set_msi_domain(struct pci_dev *dev) |
2293 | { | |
098259eb MZ |
2294 | struct irq_domain *d; |
2295 | ||
44aa0c65 | 2296 | /* |
098259eb MZ |
2297 | * If the platform or firmware interfaces cannot supply a |
2298 | * device-specific MSI domain, then inherit the default domain | |
2299 | * from the host bridge itself. | |
44aa0c65 | 2300 | */ |
098259eb MZ |
2301 | d = pci_dev_msi_domain(dev); |
2302 | if (!d) | |
2303 | d = dev_get_msi_domain(&dev->bus->dev); | |
2304 | ||
2305 | dev_set_msi_domain(&dev->dev, d); | |
44aa0c65 MZ |
2306 | } |
2307 | ||
96bde06a | 2308 | void pci_device_add(struct pci_dev *dev, struct pci_bus *bus) |
1da177e4 | 2309 | { |
4f535093 YL |
2310 | int ret; |
2311 | ||
6cd33649 BH |
2312 | pci_configure_device(dev); |
2313 | ||
cdb9b9f7 PM |
2314 | device_initialize(&dev->dev); |
2315 | dev->dev.release = pci_release_dev; | |
1da177e4 | 2316 | |
7629d19a | 2317 | set_dev_node(&dev->dev, pcibus_to_node(bus)); |
cdb9b9f7 | 2318 | dev->dev.dma_mask = &dev->dma_mask; |
4d57cdfa | 2319 | dev->dev.dma_parms = &dev->dma_parms; |
cdb9b9f7 | 2320 | dev->dev.coherent_dma_mask = 0xffffffffull; |
1da177e4 | 2321 | |
4d57cdfa | 2322 | pci_set_dma_max_seg_size(dev, 65536); |
59fc67de | 2323 | pci_set_dma_seg_boundary(dev, 0xffffffff); |
4d57cdfa | 2324 | |
1da177e4 LT |
2325 | /* Fix up broken headers */ |
2326 | pci_fixup_device(pci_fixup_header, dev); | |
2327 | ||
3e466e2d | 2328 | /* Moved out from quirk header fixup code */ |
2069ecfb YL |
2329 | pci_reassigndev_resource_alignment(dev); |
2330 | ||
3e466e2d | 2331 | /* Clear the state_saved flag */ |
4b77b0a2 RW |
2332 | dev->state_saved = false; |
2333 | ||
201de56e ZY |
2334 | /* Initialize various capabilities */ |
2335 | pci_init_capabilities(dev); | |
eb9d0fe4 | 2336 | |
1da177e4 LT |
2337 | /* |
2338 | * Add the device to our list of discovered devices | |
2339 | * and the bus list for fixup functions, etc. | |
2340 | */ | |
d71374da | 2341 | down_write(&pci_bus_sem); |
1da177e4 | 2342 | list_add_tail(&dev->bus_list, &bus->devices); |
d71374da | 2343 | up_write(&pci_bus_sem); |
4f535093 | 2344 | |
4f535093 YL |
2345 | ret = pcibios_add_device(dev); |
2346 | WARN_ON(ret < 0); | |
2347 | ||
3e466e2d | 2348 | /* Set up MSI IRQ domain */ |
44aa0c65 MZ |
2349 | pci_set_msi_domain(dev); |
2350 | ||
4f535093 YL |
2351 | /* Notifier could use PCI capabilities */ |
2352 | dev->match_driver = false; | |
2353 | ret = device_add(&dev->dev); | |
2354 | WARN_ON(ret < 0); | |
cdb9b9f7 PM |
2355 | } |
2356 | ||
10874f5a | 2357 | struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn) |
cdb9b9f7 PM |
2358 | { |
2359 | struct pci_dev *dev; | |
2360 | ||
90bdb311 TP |
2361 | dev = pci_get_slot(bus, devfn); |
2362 | if (dev) { | |
2363 | pci_dev_put(dev); | |
2364 | return dev; | |
2365 | } | |
2366 | ||
cdb9b9f7 PM |
2367 | dev = pci_scan_device(bus, devfn); |
2368 | if (!dev) | |
2369 | return NULL; | |
2370 | ||
2371 | pci_device_add(dev, bus); | |
1da177e4 LT |
2372 | |
2373 | return dev; | |
2374 | } | |
b73e9687 | 2375 | EXPORT_SYMBOL(pci_scan_single_device); |
1da177e4 | 2376 | |
b1bd58e4 | 2377 | static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn) |
f07852d6 | 2378 | { |
b1bd58e4 YW |
2379 | int pos; |
2380 | u16 cap = 0; | |
2381 | unsigned next_fn; | |
4fb88c1a | 2382 | |
b1bd58e4 YW |
2383 | if (pci_ari_enabled(bus)) { |
2384 | if (!dev) | |
2385 | return 0; | |
2386 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI); | |
2387 | if (!pos) | |
2388 | return 0; | |
4fb88c1a | 2389 | |
b1bd58e4 YW |
2390 | pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap); |
2391 | next_fn = PCI_ARI_CAP_NFN(cap); | |
2392 | if (next_fn <= fn) | |
2393 | return 0; /* protect against malformed list */ | |
f07852d6 | 2394 | |
b1bd58e4 YW |
2395 | return next_fn; |
2396 | } | |
2397 | ||
2398 | /* dev may be NULL for non-contiguous multifunction devices */ | |
2399 | if (!dev || dev->multifunction) | |
2400 | return (fn + 1) % 8; | |
f07852d6 | 2401 | |
f07852d6 MW |
2402 | return 0; |
2403 | } | |
2404 | ||
2405 | static int only_one_child(struct pci_bus *bus) | |
2406 | { | |
d57f0b8c | 2407 | struct pci_dev *bridge = bus->self; |
284f5f9d | 2408 | |
d57f0b8c BH |
2409 | /* |
2410 | * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so | |
2411 | * we scan for all possible devices, not just Device 0. | |
2412 | */ | |
2413 | if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS)) | |
f07852d6 | 2414 | return 0; |
5bbe029f BH |
2415 | |
2416 | /* | |
d57f0b8c BH |
2417 | * A PCIe Downstream Port normally leads to a Link with only Device |
2418 | * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan | |
2419 | * only for Device 0 in that situation. | |
2420 | * | |
2421 | * Checking has_secondary_link is a hack to identify Downstream | |
2422 | * Ports because sometimes Switches are configured such that the | |
2423 | * PCIe Port Type labels are backwards. | |
5bbe029f | 2424 | */ |
d57f0b8c | 2425 | if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link) |
f07852d6 | 2426 | return 1; |
d57f0b8c | 2427 | |
f07852d6 MW |
2428 | return 0; |
2429 | } | |
2430 | ||
1da177e4 | 2431 | /** |
3e466e2d | 2432 | * pci_scan_slot - Scan a PCI slot on a bus for devices |
1da177e4 | 2433 | * @bus: PCI bus to scan |
3e466e2d | 2434 | * @devfn: slot number to scan (must have zero function) |
1da177e4 LT |
2435 | * |
2436 | * Scan a PCI slot on the specified PCI bus for devices, adding | |
2437 | * discovered devices to the @bus->devices list. New devices | |
8a1bc901 | 2438 | * will not have is_added set. |
1b69dfc6 TP |
2439 | * |
2440 | * Returns the number of new devices found. | |
1da177e4 | 2441 | */ |
96bde06a | 2442 | int pci_scan_slot(struct pci_bus *bus, int devfn) |
1da177e4 | 2443 | { |
f07852d6 | 2444 | unsigned fn, nr = 0; |
1b69dfc6 | 2445 | struct pci_dev *dev; |
f07852d6 MW |
2446 | |
2447 | if (only_one_child(bus) && (devfn > 0)) | |
2448 | return 0; /* Already scanned the entire slot */ | |
1da177e4 | 2449 | |
1b69dfc6 | 2450 | dev = pci_scan_single_device(bus, devfn); |
4fb88c1a MW |
2451 | if (!dev) |
2452 | return 0; | |
2453 | if (!dev->is_added) | |
1b69dfc6 TP |
2454 | nr++; |
2455 | ||
b1bd58e4 | 2456 | for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) { |
f07852d6 MW |
2457 | dev = pci_scan_single_device(bus, devfn + fn); |
2458 | if (dev) { | |
2459 | if (!dev->is_added) | |
2460 | nr++; | |
2461 | dev->multifunction = 1; | |
1da177e4 LT |
2462 | } |
2463 | } | |
7d715a6c | 2464 | |
3e466e2d | 2465 | /* Only one slot has PCIe device */ |
149e1637 | 2466 | if (bus->self && nr) |
7d715a6c SL |
2467 | pcie_aspm_init_link_state(bus->self); |
2468 | ||
1da177e4 LT |
2469 | return nr; |
2470 | } | |
b7fe9434 | 2471 | EXPORT_SYMBOL(pci_scan_slot); |
1da177e4 | 2472 | |
b03e7495 JM |
2473 | static int pcie_find_smpss(struct pci_dev *dev, void *data) |
2474 | { | |
2475 | u8 *smpss = data; | |
2476 | ||
2477 | if (!pci_is_pcie(dev)) | |
2478 | return 0; | |
2479 | ||
d4aa68f6 YW |
2480 | /* |
2481 | * We don't have a way to change MPS settings on devices that have | |
2482 | * drivers attached. A hot-added device might support only the minimum | |
2483 | * MPS setting (MPS=128). Therefore, if the fabric contains a bridge | |
2484 | * where devices may be hot-added, we limit the fabric MPS to 128 so | |
2485 | * hot-added devices will work correctly. | |
2486 | * | |
2487 | * However, if we hot-add a device to a slot directly below a Root | |
2488 | * Port, it's impossible for there to be other existing devices below | |
2489 | * the port. We don't limit the MPS in this case because we can | |
2490 | * reconfigure MPS on both the Root Port and the hot-added device, | |
2491 | * and there are no other devices involved. | |
2492 | * | |
2493 | * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA. | |
b03e7495 | 2494 | */ |
d4aa68f6 YW |
2495 | if (dev->is_hotplug_bridge && |
2496 | pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT) | |
b03e7495 JM |
2497 | *smpss = 0; |
2498 | ||
2499 | if (*smpss > dev->pcie_mpss) | |
2500 | *smpss = dev->pcie_mpss; | |
2501 | ||
2502 | return 0; | |
2503 | } | |
2504 | ||
2505 | static void pcie_write_mps(struct pci_dev *dev, int mps) | |
2506 | { | |
62f392ea | 2507 | int rc; |
b03e7495 JM |
2508 | |
2509 | if (pcie_bus_config == PCIE_BUS_PERFORMANCE) { | |
62f392ea | 2510 | mps = 128 << dev->pcie_mpss; |
b03e7495 | 2511 | |
62f87c0e YW |
2512 | if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT && |
2513 | dev->bus->self) | |
3e466e2d BH |
2514 | |
2515 | /* | |
2516 | * For "Performance", the assumption is made that | |
b03e7495 JM |
2517 | * downstream communication will never be larger than |
2518 | * the MRRS. So, the MPS only needs to be configured | |
2519 | * for the upstream communication. This being the case, | |
2520 | * walk from the top down and set the MPS of the child | |
2521 | * to that of the parent bus. | |
62f392ea JM |
2522 | * |
2523 | * Configure the device MPS with the smaller of the | |
2524 | * device MPSS or the bridge MPS (which is assumed to be | |
2525 | * properly configured at this point to the largest | |
2526 | * allowable MPS based on its parent bus). | |
b03e7495 | 2527 | */ |
62f392ea | 2528 | mps = min(mps, pcie_get_mps(dev->bus->self)); |
b03e7495 JM |
2529 | } |
2530 | ||
2531 | rc = pcie_set_mps(dev, mps); | |
2532 | if (rc) | |
7506dc79 | 2533 | pci_err(dev, "Failed attempting to set the MPS\n"); |
b03e7495 JM |
2534 | } |
2535 | ||
62f392ea | 2536 | static void pcie_write_mrrs(struct pci_dev *dev) |
b03e7495 | 2537 | { |
62f392ea | 2538 | int rc, mrrs; |
b03e7495 | 2539 | |
3e466e2d BH |
2540 | /* |
2541 | * In the "safe" case, do not configure the MRRS. There appear to be | |
ed2888e9 JM |
2542 | * issues with setting MRRS to 0 on a number of devices. |
2543 | */ | |
ed2888e9 JM |
2544 | if (pcie_bus_config != PCIE_BUS_PERFORMANCE) |
2545 | return; | |
2546 | ||
3e466e2d BH |
2547 | /* |
2548 | * For max performance, the MRRS must be set to the largest supported | |
ed2888e9 | 2549 | * value. However, it cannot be configured larger than the MPS the |
62f392ea | 2550 | * device or the bus can support. This should already be properly |
3e466e2d | 2551 | * configured by a prior call to pcie_write_mps(). |
ed2888e9 | 2552 | */ |
62f392ea | 2553 | mrrs = pcie_get_mps(dev); |
b03e7495 | 2554 | |
3e466e2d BH |
2555 | /* |
2556 | * MRRS is a R/W register. Invalid values can be written, but a | |
ed2888e9 | 2557 | * subsequent read will verify if the value is acceptable or not. |
b03e7495 JM |
2558 | * If the MRRS value provided is not acceptable (e.g., too large), |
2559 | * shrink the value until it is acceptable to the HW. | |
f7625980 | 2560 | */ |
b03e7495 JM |
2561 | while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) { |
2562 | rc = pcie_set_readrq(dev, mrrs); | |
62f392ea JM |
2563 | if (!rc) |
2564 | break; | |
b03e7495 | 2565 | |
7506dc79 | 2566 | pci_warn(dev, "Failed attempting to set the MRRS\n"); |
b03e7495 JM |
2567 | mrrs /= 2; |
2568 | } | |
62f392ea JM |
2569 | |
2570 | if (mrrs < 128) | |
7506dc79 | 2571 | pci_err(dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n"); |
b03e7495 JM |
2572 | } |
2573 | ||
2574 | static int pcie_bus_configure_set(struct pci_dev *dev, void *data) | |
2575 | { | |
a513a99a | 2576 | int mps, orig_mps; |
b03e7495 JM |
2577 | |
2578 | if (!pci_is_pcie(dev)) | |
2579 | return 0; | |
2580 | ||
27d868b5 KB |
2581 | if (pcie_bus_config == PCIE_BUS_TUNE_OFF || |
2582 | pcie_bus_config == PCIE_BUS_DEFAULT) | |
5895af79 | 2583 | return 0; |
5895af79 | 2584 | |
a513a99a JM |
2585 | mps = 128 << *(u8 *)data; |
2586 | orig_mps = pcie_get_mps(dev); | |
b03e7495 JM |
2587 | |
2588 | pcie_write_mps(dev, mps); | |
62f392ea | 2589 | pcie_write_mrrs(dev); |
b03e7495 | 2590 | |
7506dc79 | 2591 | pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n", |
227f0647 | 2592 | pcie_get_mps(dev), 128 << dev->pcie_mpss, |
a513a99a | 2593 | orig_mps, pcie_get_readrq(dev)); |
b03e7495 JM |
2594 | |
2595 | return 0; | |
2596 | } | |
2597 | ||
3e466e2d BH |
2598 | /* |
2599 | * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down, | |
b03e7495 JM |
2600 | * parents then children fashion. If this changes, then this code will not |
2601 | * work as designed. | |
2602 | */ | |
a58674ff | 2603 | void pcie_bus_configure_settings(struct pci_bus *bus) |
b03e7495 | 2604 | { |
1e358f94 | 2605 | u8 smpss = 0; |
b03e7495 | 2606 | |
a58674ff | 2607 | if (!bus->self) |
b03e7495 JM |
2608 | return; |
2609 | ||
b03e7495 | 2610 | if (!pci_is_pcie(bus->self)) |
5f39e670 JM |
2611 | return; |
2612 | ||
3e466e2d BH |
2613 | /* |
2614 | * FIXME - Peer to peer DMA is possible, though the endpoint would need | |
3315472c | 2615 | * to be aware of the MPS of the destination. To work around this, |
5f39e670 JM |
2616 | * simply force the MPS of the entire system to the smallest possible. |
2617 | */ | |
2618 | if (pcie_bus_config == PCIE_BUS_PEER2PEER) | |
2619 | smpss = 0; | |
2620 | ||
b03e7495 | 2621 | if (pcie_bus_config == PCIE_BUS_SAFE) { |
a58674ff | 2622 | smpss = bus->self->pcie_mpss; |
5f39e670 | 2623 | |
b03e7495 JM |
2624 | pcie_find_smpss(bus->self, &smpss); |
2625 | pci_walk_bus(bus, pcie_find_smpss, &smpss); | |
2626 | } | |
2627 | ||
2628 | pcie_bus_configure_set(bus->self, &smpss); | |
2629 | pci_walk_bus(bus, pcie_bus_configure_set, &smpss); | |
2630 | } | |
debc3b77 | 2631 | EXPORT_SYMBOL_GPL(pcie_bus_configure_settings); |
b03e7495 | 2632 | |
bccf90d6 PD |
2633 | /* |
2634 | * Called after each bus is probed, but before its children are examined. This | |
2635 | * is marked as __weak because multiple architectures define it. | |
2636 | */ | |
2637 | void __weak pcibios_fixup_bus(struct pci_bus *bus) | |
2638 | { | |
2639 | /* nothing to do, expected to be removed in the future */ | |
2640 | } | |
2641 | ||
1c02ea81 MW |
2642 | /** |
2643 | * pci_scan_child_bus_extend() - Scan devices below a bus | |
2644 | * @bus: Bus to scan for devices | |
2645 | * @available_buses: Total number of buses available (%0 does not try to | |
2646 | * extend beyond the minimal) | |
2647 | * | |
2648 | * Scans devices below @bus including subordinate buses. Returns new | |
2649 | * subordinate number including all the found devices. Passing | |
2650 | * @available_buses causes the remaining bus space to be distributed | |
2651 | * equally between hotplug-capable bridges to allow future extension of the | |
2652 | * hierarchy. | |
2653 | */ | |
2654 | static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus, | |
2655 | unsigned int available_buses) | |
1da177e4 | 2656 | { |
1c02ea81 MW |
2657 | unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0; |
2658 | unsigned int start = bus->busn_res.start; | |
690f4304 | 2659 | unsigned int devfn, fn, cmax, max = start; |
1da177e4 | 2660 | struct pci_dev *dev; |
690f4304 | 2661 | int nr_devs; |
1da177e4 | 2662 | |
0207c356 | 2663 | dev_dbg(&bus->dev, "scanning bus\n"); |
1da177e4 LT |
2664 | |
2665 | /* Go find them, Rover! */ | |
690f4304 JK |
2666 | for (devfn = 0; devfn < 256; devfn += 8) { |
2667 | nr_devs = pci_scan_slot(bus, devfn); | |
2668 | ||
2669 | /* | |
2670 | * The Jailhouse hypervisor may pass individual functions of a | |
2671 | * multi-function device to a guest without passing function 0. | |
2672 | * Look for them as well. | |
2673 | */ | |
2674 | if (jailhouse_paravirt() && nr_devs == 0) { | |
2675 | for (fn = 1; fn < 8; fn++) { | |
2676 | dev = pci_scan_single_device(bus, devfn + fn); | |
2677 | if (dev) | |
2678 | dev->multifunction = 1; | |
2679 | } | |
2680 | } | |
2681 | } | |
1da177e4 | 2682 | |
3e466e2d | 2683 | /* Reserve buses for SR-IOV capability */ |
1c02ea81 MW |
2684 | used_buses = pci_iov_bus_range(bus); |
2685 | max += used_buses; | |
a28724b0 | 2686 | |
1da177e4 LT |
2687 | /* |
2688 | * After performing arch-dependent fixup of the bus, look behind | |
2689 | * all PCI-to-PCI bridges on this bus. | |
2690 | */ | |
74710ded | 2691 | if (!bus->is_added) { |
0207c356 | 2692 | dev_dbg(&bus->dev, "fixups for bus\n"); |
74710ded | 2693 | pcibios_fixup_bus(bus); |
981cf9ea | 2694 | bus->is_added = 1; |
74710ded AC |
2695 | } |
2696 | ||
1c02ea81 MW |
2697 | /* |
2698 | * Calculate how many hotplug bridges and normal bridges there | |
2699 | * are on this bus. We will distribute the additional available | |
2700 | * buses between hotplug bridges. | |
2701 | */ | |
2702 | for_each_pci_bridge(dev, bus) { | |
2703 | if (dev->is_hotplug_bridge) | |
2704 | hotplug_bridges++; | |
2705 | else | |
2706 | normal_bridges++; | |
2707 | } | |
2708 | ||
4147c2fd MW |
2709 | /* |
2710 | * Scan bridges that are already configured. We don't touch them | |
2711 | * unless they are misconfigured (which will be done in the second | |
2712 | * scan below). | |
2713 | */ | |
1c02ea81 MW |
2714 | for_each_pci_bridge(dev, bus) { |
2715 | cmax = max; | |
2716 | max = pci_scan_bridge_extend(bus, dev, max, 0, 0); | |
3374c545 MW |
2717 | |
2718 | /* | |
2719 | * Reserve one bus for each bridge now to avoid extending | |
2720 | * hotplug bridges too much during the second scan below. | |
2721 | */ | |
2722 | used_buses++; | |
2723 | if (cmax - max > 1) | |
2724 | used_buses += cmax - max - 1; | |
1c02ea81 | 2725 | } |
4147c2fd MW |
2726 | |
2727 | /* Scan bridges that need to be reconfigured */ | |
1c02ea81 MW |
2728 | for_each_pci_bridge(dev, bus) { |
2729 | unsigned int buses = 0; | |
2730 | ||
2731 | if (!hotplug_bridges && normal_bridges == 1) { | |
3e466e2d | 2732 | |
1c02ea81 MW |
2733 | /* |
2734 | * There is only one bridge on the bus (upstream | |
2735 | * port) so it gets all available buses which it | |
2736 | * can then distribute to the possible hotplug | |
2737 | * bridges below. | |
2738 | */ | |
2739 | buses = available_buses; | |
2740 | } else if (dev->is_hotplug_bridge) { | |
3e466e2d | 2741 | |
1c02ea81 MW |
2742 | /* |
2743 | * Distribute the extra buses between hotplug | |
2744 | * bridges if any. | |
2745 | */ | |
2746 | buses = available_buses / hotplug_bridges; | |
3374c545 | 2747 | buses = min(buses, available_buses - used_buses + 1); |
1c02ea81 MW |
2748 | } |
2749 | ||
2750 | cmax = max; | |
2751 | max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1); | |
3374c545 MW |
2752 | /* One bus is already accounted so don't add it again */ |
2753 | if (max - cmax > 1) | |
2754 | used_buses += max - cmax - 1; | |
1c02ea81 | 2755 | } |
1da177e4 | 2756 | |
e16b4660 KB |
2757 | /* |
2758 | * Make sure a hotplug bridge has at least the minimum requested | |
1c02ea81 MW |
2759 | * number of buses but allow it to grow up to the maximum available |
2760 | * bus number of there is room. | |
e16b4660 | 2761 | */ |
1c02ea81 MW |
2762 | if (bus->self && bus->self->is_hotplug_bridge) { |
2763 | used_buses = max_t(unsigned int, available_buses, | |
2764 | pci_hotplug_bus_size - 1); | |
2765 | if (max - start < used_buses) { | |
2766 | max = start + used_buses; | |
2767 | ||
2768 | /* Do not allocate more buses than we have room left */ | |
2769 | if (max > bus->busn_res.end) | |
2770 | max = bus->busn_res.end; | |
2771 | ||
2772 | dev_dbg(&bus->dev, "%pR extended by %#02x\n", | |
2773 | &bus->busn_res, max - start); | |
2774 | } | |
e16b4660 KB |
2775 | } |
2776 | ||
1da177e4 LT |
2777 | /* |
2778 | * We've scanned the bus and so we know all about what's on | |
2779 | * the other side of any bridges that may be on this bus plus | |
2780 | * any devices. | |
2781 | * | |
2782 | * Return how far we've got finding sub-buses. | |
2783 | */ | |
0207c356 | 2784 | dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max); |
1da177e4 LT |
2785 | return max; |
2786 | } | |
1c02ea81 MW |
2787 | |
2788 | /** | |
2789 | * pci_scan_child_bus() - Scan devices below a bus | |
2790 | * @bus: Bus to scan for devices | |
2791 | * | |
2792 | * Scans devices below @bus including subordinate buses. Returns new | |
2793 | * subordinate number including all the found devices. | |
2794 | */ | |
2795 | unsigned int pci_scan_child_bus(struct pci_bus *bus) | |
2796 | { | |
2797 | return pci_scan_child_bus_extend(bus, 0); | |
2798 | } | |
b7fe9434 | 2799 | EXPORT_SYMBOL_GPL(pci_scan_child_bus); |
1da177e4 | 2800 | |
6c0cc950 | 2801 | /** |
3e466e2d BH |
2802 | * pcibios_root_bridge_prepare - Platform-specific host bridge setup |
2803 | * @bridge: Host bridge to set up | |
6c0cc950 RW |
2804 | * |
2805 | * Default empty implementation. Replace with an architecture-specific setup | |
2806 | * routine, if necessary. | |
2807 | */ | |
2808 | int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge) | |
2809 | { | |
2810 | return 0; | |
2811 | } | |
2812 | ||
10a95747 JL |
2813 | void __weak pcibios_add_bus(struct pci_bus *bus) |
2814 | { | |
2815 | } | |
2816 | ||
2817 | void __weak pcibios_remove_bus(struct pci_bus *bus) | |
2818 | { | |
2819 | } | |
2820 | ||
9ee8a1c4 LP |
2821 | struct pci_bus *pci_create_root_bus(struct device *parent, int bus, |
2822 | struct pci_ops *ops, void *sysdata, struct list_head *resources) | |
1da177e4 | 2823 | { |
0efd5aab | 2824 | int error; |
5a21d70d | 2825 | struct pci_host_bridge *bridge; |
1da177e4 | 2826 | |
59094065 | 2827 | bridge = pci_alloc_host_bridge(0); |
7b543663 | 2828 | if (!bridge) |
37d6a0a6 | 2829 | return NULL; |
7b543663 YL |
2830 | |
2831 | bridge->dev.parent = parent; | |
a9d9f527 | 2832 | |
37d6a0a6 AB |
2833 | list_splice_init(resources, &bridge->windows); |
2834 | bridge->sysdata = sysdata; | |
2835 | bridge->busnr = bus; | |
2836 | bridge->ops = ops; | |
a9d9f527 | 2837 | |
37d6a0a6 AB |
2838 | error = pci_register_host_bridge(bridge); |
2839 | if (error < 0) | |
2840 | goto err_out; | |
a5390aa6 | 2841 | |
37d6a0a6 | 2842 | return bridge->bus; |
1da177e4 | 2843 | |
1da177e4 | 2844 | err_out: |
37d6a0a6 | 2845 | kfree(bridge); |
1da177e4 LT |
2846 | return NULL; |
2847 | } | |
e6b29dea | 2848 | EXPORT_SYMBOL_GPL(pci_create_root_bus); |
cdb9b9f7 | 2849 | |
49b8e3f3 CP |
2850 | int pci_host_probe(struct pci_host_bridge *bridge) |
2851 | { | |
2852 | struct pci_bus *bus, *child; | |
2853 | int ret; | |
2854 | ||
2855 | ret = pci_scan_root_bus_bridge(bridge); | |
2856 | if (ret < 0) { | |
2857 | dev_err(bridge->dev.parent, "Scanning root bridge failed"); | |
2858 | return ret; | |
2859 | } | |
2860 | ||
2861 | bus = bridge->bus; | |
2862 | ||
2863 | /* | |
2864 | * We insert PCI resources into the iomem_resource and | |
2865 | * ioport_resource trees in either pci_bus_claim_resources() | |
2866 | * or pci_bus_assign_resources(). | |
2867 | */ | |
2868 | if (pci_has_flag(PCI_PROBE_ONLY)) { | |
2869 | pci_bus_claim_resources(bus); | |
2870 | } else { | |
2871 | pci_bus_size_bridges(bus); | |
2872 | pci_bus_assign_resources(bus); | |
2873 | ||
2874 | list_for_each_entry(child, &bus->children, node) | |
2875 | pcie_bus_configure_settings(child); | |
2876 | } | |
2877 | ||
2878 | pci_bus_add_devices(bus); | |
2879 | return 0; | |
2880 | } | |
2881 | EXPORT_SYMBOL_GPL(pci_host_probe); | |
2882 | ||
98a35831 YL |
2883 | int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max) |
2884 | { | |
2885 | struct resource *res = &b->busn_res; | |
2886 | struct resource *parent_res, *conflict; | |
2887 | ||
2888 | res->start = bus; | |
2889 | res->end = bus_max; | |
2890 | res->flags = IORESOURCE_BUS; | |
2891 | ||
2892 | if (!pci_is_root_bus(b)) | |
2893 | parent_res = &b->parent->busn_res; | |
2894 | else { | |
2895 | parent_res = get_pci_domain_busn_res(pci_domain_nr(b)); | |
2896 | res->flags |= IORESOURCE_PCI_FIXED; | |
2897 | } | |
2898 | ||
ced04d15 | 2899 | conflict = request_resource_conflict(parent_res, res); |
98a35831 YL |
2900 | |
2901 | if (conflict) | |
2902 | dev_printk(KERN_DEBUG, &b->dev, | |
2903 | "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n", | |
2904 | res, pci_is_root_bus(b) ? "domain " : "", | |
2905 | parent_res, conflict->name, conflict); | |
98a35831 YL |
2906 | |
2907 | return conflict == NULL; | |
2908 | } | |
2909 | ||
2910 | int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max) | |
2911 | { | |
2912 | struct resource *res = &b->busn_res; | |
2913 | struct resource old_res = *res; | |
2914 | resource_size_t size; | |
2915 | int ret; | |
2916 | ||
2917 | if (res->start > bus_max) | |
2918 | return -EINVAL; | |
2919 | ||
2920 | size = bus_max - res->start + 1; | |
2921 | ret = adjust_resource(res, res->start, size); | |
2922 | dev_printk(KERN_DEBUG, &b->dev, | |
2923 | "busn_res: %pR end %s updated to %02x\n", | |
2924 | &old_res, ret ? "can not be" : "is", bus_max); | |
2925 | ||
2926 | if (!ret && !res->parent) | |
2927 | pci_bus_insert_busn_res(b, res->start, res->end); | |
2928 | ||
2929 | return ret; | |
2930 | } | |
2931 | ||
2932 | void pci_bus_release_busn_res(struct pci_bus *b) | |
2933 | { | |
2934 | struct resource *res = &b->busn_res; | |
2935 | int ret; | |
2936 | ||
2937 | if (!res->flags || !res->parent) | |
2938 | return; | |
2939 | ||
2940 | ret = release_resource(res); | |
2941 | dev_printk(KERN_DEBUG, &b->dev, | |
2942 | "busn_res: %pR %s released\n", | |
2943 | res, ret ? "can not be" : "is"); | |
2944 | } | |
2945 | ||
1228c4b6 | 2946 | int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge) |
a2ebb827 | 2947 | { |
14d76b68 | 2948 | struct resource_entry *window; |
4d99f524 | 2949 | bool found = false; |
a2ebb827 | 2950 | struct pci_bus *b; |
1228c4b6 | 2951 | int max, bus, ret; |
4d99f524 | 2952 | |
1228c4b6 LP |
2953 | if (!bridge) |
2954 | return -EINVAL; | |
2955 | ||
2956 | resource_list_for_each_entry(window, &bridge->windows) | |
4d99f524 YL |
2957 | if (window->res->flags & IORESOURCE_BUS) { |
2958 | found = true; | |
2959 | break; | |
2960 | } | |
a2ebb827 | 2961 | |
1228c4b6 LP |
2962 | ret = pci_register_host_bridge(bridge); |
2963 | if (ret < 0) | |
2964 | return ret; | |
2965 | ||
2966 | b = bridge->bus; | |
2967 | bus = bridge->busnr; | |
a2ebb827 | 2968 | |
4d99f524 YL |
2969 | if (!found) { |
2970 | dev_info(&b->dev, | |
2971 | "No busn resource found for root bus, will use [bus %02x-ff]\n", | |
2972 | bus); | |
2973 | pci_bus_insert_busn_res(b, bus, 255); | |
2974 | } | |
2975 | ||
2976 | max = pci_scan_child_bus(b); | |
2977 | ||
2978 | if (!found) | |
2979 | pci_bus_update_busn_res_end(b, max); | |
2980 | ||
1228c4b6 | 2981 | return 0; |
a2ebb827 | 2982 | } |
1228c4b6 | 2983 | EXPORT_SYMBOL(pci_scan_root_bus_bridge); |
d2a7926d LP |
2984 | |
2985 | struct pci_bus *pci_scan_root_bus(struct device *parent, int bus, | |
2986 | struct pci_ops *ops, void *sysdata, struct list_head *resources) | |
2987 | { | |
14d76b68 | 2988 | struct resource_entry *window; |
4d99f524 | 2989 | bool found = false; |
a2ebb827 | 2990 | struct pci_bus *b; |
4d99f524 YL |
2991 | int max; |
2992 | ||
14d76b68 | 2993 | resource_list_for_each_entry(window, resources) |
4d99f524 YL |
2994 | if (window->res->flags & IORESOURCE_BUS) { |
2995 | found = true; | |
2996 | break; | |
2997 | } | |
a2ebb827 | 2998 | |
9ee8a1c4 | 2999 | b = pci_create_root_bus(parent, bus, ops, sysdata, resources); |
a2ebb827 BH |
3000 | if (!b) |
3001 | return NULL; | |
3002 | ||
4d99f524 YL |
3003 | if (!found) { |
3004 | dev_info(&b->dev, | |
3005 | "No busn resource found for root bus, will use [bus %02x-ff]\n", | |
3006 | bus); | |
3007 | pci_bus_insert_busn_res(b, bus, 255); | |
3008 | } | |
3009 | ||
3010 | max = pci_scan_child_bus(b); | |
3011 | ||
3012 | if (!found) | |
3013 | pci_bus_update_busn_res_end(b, max); | |
3014 | ||
a2ebb827 | 3015 | return b; |
d2a7926d | 3016 | } |
a2ebb827 BH |
3017 | EXPORT_SYMBOL(pci_scan_root_bus); |
3018 | ||
15856ad5 | 3019 | struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops, |
de4b2f76 BH |
3020 | void *sysdata) |
3021 | { | |
3022 | LIST_HEAD(resources); | |
3023 | struct pci_bus *b; | |
3024 | ||
3025 | pci_add_resource(&resources, &ioport_resource); | |
3026 | pci_add_resource(&resources, &iomem_resource); | |
857c3b66 | 3027 | pci_add_resource(&resources, &busn_resource); |
de4b2f76 BH |
3028 | b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources); |
3029 | if (b) { | |
857c3b66 | 3030 | pci_scan_child_bus(b); |
de4b2f76 BH |
3031 | } else { |
3032 | pci_free_resource_list(&resources); | |
3033 | } | |
3034 | return b; | |
3035 | } | |
3036 | EXPORT_SYMBOL(pci_scan_bus); | |
3037 | ||
2f320521 | 3038 | /** |
3e466e2d | 3039 | * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices |
2f320521 YL |
3040 | * @bridge: PCI bridge for the bus to scan |
3041 | * | |
3042 | * Scan a PCI bus and child buses for new devices, add them, | |
3043 | * and enable them, resizing bridge mmio/io resource if necessary | |
3044 | * and possible. The caller must ensure the child devices are already | |
3045 | * removed for resizing to occur. | |
3046 | * | |
3047 | * Returns the max number of subordinate bus discovered. | |
3048 | */ | |
10874f5a | 3049 | unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge) |
2f320521 YL |
3050 | { |
3051 | unsigned int max; | |
3052 | struct pci_bus *bus = bridge->subordinate; | |
3053 | ||
3054 | max = pci_scan_child_bus(bus); | |
3055 | ||
3056 | pci_assign_unassigned_bridge_resources(bridge); | |
3057 | ||
3058 | pci_bus_add_devices(bus); | |
3059 | ||
3060 | return max; | |
3061 | } | |
3062 | ||
a5213a31 | 3063 | /** |
3e466e2d | 3064 | * pci_rescan_bus - Scan a PCI bus for devices |
a5213a31 YL |
3065 | * @bus: PCI bus to scan |
3066 | * | |
3e466e2d BH |
3067 | * Scan a PCI bus and child buses for new devices, add them, |
3068 | * and enable them. | |
a5213a31 YL |
3069 | * |
3070 | * Returns the max number of subordinate bus discovered. | |
3071 | */ | |
10874f5a | 3072 | unsigned int pci_rescan_bus(struct pci_bus *bus) |
a5213a31 YL |
3073 | { |
3074 | unsigned int max; | |
3075 | ||
3076 | max = pci_scan_child_bus(bus); | |
3077 | pci_assign_unassigned_bus_resources(bus); | |
3078 | pci_bus_add_devices(bus); | |
3079 | ||
3080 | return max; | |
3081 | } | |
3082 | EXPORT_SYMBOL_GPL(pci_rescan_bus); | |
3083 | ||
9d16947b RW |
3084 | /* |
3085 | * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal | |
3086 | * routines should always be executed under this mutex. | |
3087 | */ | |
3088 | static DEFINE_MUTEX(pci_rescan_remove_lock); | |
3089 | ||
3090 | void pci_lock_rescan_remove(void) | |
3091 | { | |
3092 | mutex_lock(&pci_rescan_remove_lock); | |
3093 | } | |
3094 | EXPORT_SYMBOL_GPL(pci_lock_rescan_remove); | |
3095 | ||
3096 | void pci_unlock_rescan_remove(void) | |
3097 | { | |
3098 | mutex_unlock(&pci_rescan_remove_lock); | |
3099 | } | |
3100 | EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove); | |
3101 | ||
3c78bc61 RD |
3102 | static int __init pci_sort_bf_cmp(const struct device *d_a, |
3103 | const struct device *d_b) | |
6b4b78fe | 3104 | { |
99178b03 GKH |
3105 | const struct pci_dev *a = to_pci_dev(d_a); |
3106 | const struct pci_dev *b = to_pci_dev(d_b); | |
3107 | ||
6b4b78fe MD |
3108 | if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1; |
3109 | else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1; | |
3110 | ||
3111 | if (a->bus->number < b->bus->number) return -1; | |
3112 | else if (a->bus->number > b->bus->number) return 1; | |
3113 | ||
3114 | if (a->devfn < b->devfn) return -1; | |
3115 | else if (a->devfn > b->devfn) return 1; | |
3116 | ||
3117 | return 0; | |
3118 | } | |
3119 | ||
5ff580c1 | 3120 | void __init pci_sort_breadthfirst(void) |
6b4b78fe | 3121 | { |
99178b03 | 3122 | bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp); |
6b4b78fe | 3123 | } |
95e3ba97 MW |
3124 | |
3125 | int pci_hp_add_bridge(struct pci_dev *dev) | |
3126 | { | |
3127 | struct pci_bus *parent = dev->bus; | |
4147c2fd | 3128 | int busnr, start = parent->busn_res.start; |
1c02ea81 | 3129 | unsigned int available_buses = 0; |
95e3ba97 MW |
3130 | int end = parent->busn_res.end; |
3131 | ||
3132 | for (busnr = start; busnr <= end; busnr++) { | |
3133 | if (!pci_find_bus(pci_domain_nr(parent), busnr)) | |
3134 | break; | |
3135 | } | |
3136 | if (busnr-- > end) { | |
7506dc79 | 3137 | pci_err(dev, "No bus number available for hot-added bridge\n"); |
95e3ba97 MW |
3138 | return -1; |
3139 | } | |
4147c2fd MW |
3140 | |
3141 | /* Scan bridges that are already configured */ | |
3142 | busnr = pci_scan_bridge(parent, dev, busnr, 0); | |
3143 | ||
1c02ea81 MW |
3144 | /* |
3145 | * Distribute the available bus numbers between hotplug-capable | |
3146 | * bridges to make extending the chain later possible. | |
3147 | */ | |
3148 | available_buses = end - busnr; | |
3149 | ||
4147c2fd | 3150 | /* Scan bridges that need to be reconfigured */ |
1c02ea81 | 3151 | pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1); |
4147c2fd | 3152 | |
95e3ba97 MW |
3153 | if (!dev->subordinate) |
3154 | return -1; | |
3155 | ||
3156 | return 0; | |
3157 | } | |
3158 | EXPORT_SYMBOL_GPL(pci_hp_add_bridge); |