blob: d4ec117c1142e0f155be3f3a17f4f675d64f4135 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Low-Level PCI Support for PC
3 *
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
5 */
6
7#include <linux/sched.h>
8#include <linux/pci.h>
Jiang Liu89016502013-04-12 05:44:23 +00009#include <linux/pci-acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/ioport.h>
11#include <linux/init.h>
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -080012#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/acpi.h>
16#include <asm/segment.h>
17#include <asm/io.h>
18#include <asm/smp.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053019#include <asm/pci_x86.h>
Matthew Garrettf9a37be02012-12-05 14:33:27 -070020#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
23 PCI_PROBE_MMCONF;
24
Adrian Bunk2b290da2006-11-16 13:16:23 +010025static int pci_bf_sort;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026int pci_routeirq;
Stefan Assmanna9322f62008-06-11 16:35:14 +020027int noioapicquirk;
Stefan Assmann41b9eb22008-07-15 13:48:55 +020028#ifdef CONFIG_X86_REROUTE_FOR_BROKEN_BOOT_IRQS
29int noioapicreroute = 0;
30#else
Stefan Assmann91979792008-06-11 16:35:15 +020031int noioapicreroute = 1;
Stefan Assmann41b9eb22008-07-15 13:48:55 +020032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033int pcibios_last_bus = -1;
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080034unsigned long pirq_table_addr;
Jan Beulich72da0b02011-09-15 08:58:51 +010035const struct pci_raw_ops *__read_mostly raw_pci_ops;
36const struct pci_raw_ops *__read_mostly raw_pci_ext_ops;
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050037
38int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
39 int reg, int len, u32 *val)
40{
Matthew Wilcoxbeef3122008-07-11 15:21:17 -060041 if (domain == 0 && reg < 256 && raw_pci_ops)
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050042 return raw_pci_ops->read(domain, bus, devfn, reg, len, val);
43 if (raw_pci_ext_ops)
44 return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
45 return -EINVAL;
46}
47
48int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
49 int reg, int len, u32 val)
50{
Matthew Wilcoxbeef3122008-07-11 15:21:17 -060051 if (domain == 0 && reg < 256 && raw_pci_ops)
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050052 return raw_pci_ops->write(domain, bus, devfn, reg, len, val);
53 if (raw_pci_ext_ops)
54 return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
55 return -EINVAL;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
59{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050060 return raw_pci_read(pci_domain_nr(bus), bus->number,
Jeff Garzika79e4192007-10-11 16:58:30 -040061 devfn, where, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
65{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050066 return raw_pci_write(pci_domain_nr(bus), bus->number,
Jeff Garzika79e4192007-10-11 16:58:30 -040067 devfn, where, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70struct pci_ops pci_root_ops = {
71 .read = pci_read,
72 .write = pci_write,
73};
74
75/*
Thomas Gleixnerdf65c1b2017-03-16 22:50:07 +010076 * This interrupt-safe spinlock protects all accesses to PCI configuration
77 * space, except for the mmconfig (ECAM) based operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +000079DEFINE_RAW_SPINLOCK(pci_config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Mathias Krause4ac9cbf2014-08-25 23:26:35 +020081static int __init can_skip_ioresource_align(const struct dmi_system_id *d)
Yinghai Lu13a6ddb2008-03-27 01:31:18 -070082{
83 pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
84 printk(KERN_INFO "PCI: %s detected, can skip ISA alignment\n", d->ident);
85 return 0;
86}
87
Mathias Krause4ac9cbf2014-08-25 23:26:35 +020088static const struct dmi_system_id can_skip_pciprobe_dmi_table[] __initconst = {
Yinghai Lu13a6ddb2008-03-27 01:31:18 -070089/*
90 * Systems where PCI IO resource ISA alignment can be skipped
91 * when the ISA enable bit in the bridge control is not set
92 */
93 {
94 .callback = can_skip_ioresource_align,
95 .ident = "IBM System x3800",
96 .matches = {
97 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
98 DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
99 },
100 },
101 {
102 .callback = can_skip_ioresource_align,
103 .ident = "IBM System x3850",
104 .matches = {
105 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
106 DMI_MATCH(DMI_PRODUCT_NAME, "x3850"),
107 },
108 },
109 {
110 .callback = can_skip_ioresource_align,
111 .ident = "IBM System x3950",
112 .matches = {
113 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
114 DMI_MATCH(DMI_PRODUCT_NAME, "x3950"),
115 },
116 },
117 {}
118};
119
120void __init dmi_check_skip_isa_align(void)
121{
122 dmi_check_system(can_skip_pciprobe_dmi_table);
123}
124
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800125static void pcibios_fixup_device_resources(struct pci_dev *dev)
Gary Hadebb71ad82008-05-12 13:57:46 -0700126{
127 struct resource *rom_r = &dev->resource[PCI_ROM_RESOURCE];
Mike Habeck7bd1c362010-05-12 11:14:32 -0700128 struct resource *bar_r;
129 int bar;
130
131 if (pci_probe & PCI_NOASSIGN_BARS) {
132 /*
133 * If the BIOS did not assign the BAR, zero out the
Andrea Gelmini386ed2ab2016-06-10 19:05:09 -0500134 * resource so the kernel doesn't attempt to assign
Mike Habeck7bd1c362010-05-12 11:14:32 -0700135 * it later on in pci_assign_unassigned_resources
136 */
137 for (bar = 0; bar <= PCI_STD_RESOURCE_END; bar++) {
138 bar_r = &dev->resource[bar];
139 if (bar_r->start == 0 && bar_r->end != 0) {
140 bar_r->flags = 0;
141 bar_r->end = 0;
142 }
143 }
144 }
Gary Hadebb71ad82008-05-12 13:57:46 -0700145
146 if (pci_probe & PCI_NOASSIGN_ROMS) {
147 if (rom_r->parent)
148 return;
149 if (rom_r->start) {
150 /* we deal with BIOS assigned ROM later */
151 return;
152 }
153 rom_r->start = rom_r->end = rom_r->flags = 0;
154 }
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * Called after each bus is probed, but before its children
159 * are examined.
160 */
161
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800162void pcibios_fixup_bus(struct pci_bus *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Gary Hadebb71ad82008-05-12 13:57:46 -0700164 struct pci_dev *dev;
165
Bjorn Helgaas237865f2015-09-15 13:18:04 -0500166 pci_read_bridge_bases(b);
Gary Hadebb71ad82008-05-12 13:57:46 -0700167 list_for_each_entry(dev, &b->devices, bus_list)
168 pcibios_fixup_device_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jiang Liu89016502013-04-12 05:44:23 +0000171void pcibios_add_bus(struct pci_bus *bus)
172{
173 acpi_pci_add_bus(bus);
174}
175
176void pcibios_remove_bus(struct pci_bus *bus)
177{
178 acpi_pci_remove_bus(bus);
179}
180
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800181/*
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500182 * Only use DMI information to set this if nothing was passed
183 * on the kernel command line (which was parsed earlier).
184 */
185
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200186static int __init set_bf_sort(const struct dmi_system_id *d)
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500187{
188 if (pci_bf_sort == pci_bf_sort_default) {
189 pci_bf_sort = pci_dmi_bf;
190 printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
191 }
192 return 0;
193}
194
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200195static void __init read_dmi_type_b1(const struct dmi_header *dm,
196 void *private_data)
Narendra_K@Dell.com6e8af082010-12-14 09:57:12 -0800197{
Jean Delvaref5ab3b72017-06-02 16:13:11 +0200198 u8 *data = (u8 *)dm + 4;
Narendra_K@Dell.com6e8af082010-12-14 09:57:12 -0800199
200 if (dm->type != 0xB1)
201 return;
Jean Delvaref5ab3b72017-06-02 16:13:11 +0200202 if ((((*(u32 *)data) >> 9) & 0x03) == 0x01)
203 set_bf_sort((const struct dmi_system_id *)private_data);
Narendra_K@Dell.com6e8af082010-12-14 09:57:12 -0800204}
205
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200206static int __init find_sort_method(const struct dmi_system_id *d)
Narendra_K@Dell.com6e8af082010-12-14 09:57:12 -0800207{
Jean Delvaref5ab3b72017-06-02 16:13:11 +0200208 dmi_walk(read_dmi_type_b1, (void *)d);
209 return 0;
Narendra_K@Dell.com6e8af082010-12-14 09:57:12 -0800210}
211
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500212/*
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800213 * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
214 */
215#ifdef __i386__
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200216static int __init assign_all_busses(const struct dmi_system_id *d)
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800217{
218 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
219 printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
220 " (pci=assign-busses)\n", d->ident);
221 return 0;
222}
223#endif
224
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200225static int __init set_scan_all(const struct dmi_system_id *d)
Bjorn Helgaas284f5f92012-04-30 15:21:02 -0600226{
227 printk(KERN_INFO "PCI: %s detected, enabling pci=pcie_scan_all\n",
228 d->ident);
229 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
230 return 0;
231}
232
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200233static const struct dmi_system_id pciprobe_dmi_table[] __initconst = {
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500234#ifdef __i386__
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800235/*
236 * Laptops which need pci=assign-busses to see Cardbus cards
237 */
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800238 {
239 .callback = assign_all_busses,
240 .ident = "Samsung X20 Laptop",
241 .matches = {
242 DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
243 DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
244 },
245 },
246#endif /* __i386__ */
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500247 {
248 .callback = set_bf_sort,
249 .ident = "Dell PowerEdge 1950",
250 .matches = {
251 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
252 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
253 },
254 },
255 {
256 .callback = set_bf_sort,
257 .ident = "Dell PowerEdge 1955",
258 .matches = {
259 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
260 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
261 },
262 },
263 {
264 .callback = set_bf_sort,
265 .ident = "Dell PowerEdge 2900",
266 .matches = {
267 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
268 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
269 },
270 },
271 {
272 .callback = set_bf_sort,
273 .ident = "Dell PowerEdge 2950",
274 .matches = {
275 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
276 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
277 },
278 },
Andy Gospodarekf52383d2007-02-05 16:36:10 -0800279 {
280 .callback = set_bf_sort,
Matt Domschf7a9dae2007-03-23 23:58:07 -0500281 .ident = "Dell PowerEdge R900",
282 .matches = {
283 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
284 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
285 },
286 },
287 {
Narendra_K@Dell.com9b373ed2011-03-18 10:22:14 -0700288 .callback = find_sort_method,
289 .ident = "Dell System",
290 .matches = {
291 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
292 },
293 },
294 {
Matt Domschf7a9dae2007-03-23 23:58:07 -0500295 .callback = set_bf_sort,
Andy Gospodarekf52383d2007-02-05 16:36:10 -0800296 .ident = "HP ProLiant BL20p G3",
297 .matches = {
298 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
299 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
300 },
301 },
302 {
303 .callback = set_bf_sort,
304 .ident = "HP ProLiant BL20p G4",
305 .matches = {
306 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
307 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
308 },
309 },
310 {
311 .callback = set_bf_sort,
312 .ident = "HP ProLiant BL30p G1",
313 .matches = {
314 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
315 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
316 },
317 },
318 {
319 .callback = set_bf_sort,
320 .ident = "HP ProLiant BL25p G1",
321 .matches = {
322 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
323 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
324 },
325 },
326 {
327 .callback = set_bf_sort,
328 .ident = "HP ProLiant BL35p G1",
329 .matches = {
330 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
331 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
332 },
333 },
334 {
335 .callback = set_bf_sort,
336 .ident = "HP ProLiant BL45p G1",
337 .matches = {
338 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
339 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
340 },
341 },
342 {
343 .callback = set_bf_sort,
344 .ident = "HP ProLiant BL45p G2",
345 .matches = {
346 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
347 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
348 },
349 },
350 {
351 .callback = set_bf_sort,
352 .ident = "HP ProLiant BL460c G1",
353 .matches = {
354 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
355 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
356 },
357 },
358 {
359 .callback = set_bf_sort,
360 .ident = "HP ProLiant BL465c G1",
361 .matches = {
362 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
363 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
364 },
365 },
366 {
367 .callback = set_bf_sort,
368 .ident = "HP ProLiant BL480c G1",
369 .matches = {
370 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
371 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
372 },
373 },
374 {
375 .callback = set_bf_sort,
376 .ident = "HP ProLiant BL685c G1",
377 .matches = {
378 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
379 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
380 },
381 },
Michal Schmidt8f8ae1a2007-10-17 18:04:35 +0200382 {
383 .callback = set_bf_sort,
Tony Camuso8d64c7812008-05-15 14:40:14 -0400384 .ident = "HP ProLiant DL360",
Michal Schmidt8f8ae1a2007-10-17 18:04:35 +0200385 .matches = {
386 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
Tony Camuso8d64c7812008-05-15 14:40:14 -0400387 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL360"),
Michal Schmidt8f8ae1a2007-10-17 18:04:35 +0200388 },
389 },
390 {
391 .callback = set_bf_sort,
Tony Camuso8d64c7812008-05-15 14:40:14 -0400392 .ident = "HP ProLiant DL380",
Michal Schmidt8f8ae1a2007-10-17 18:04:35 +0200393 .matches = {
394 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
Tony Camuso8d64c7812008-05-15 14:40:14 -0400395 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL380"),
Michal Schmidt8f8ae1a2007-10-17 18:04:35 +0200396 },
397 },
Juha Laiho5b1ea822007-09-13 21:21:34 +0300398#ifdef __i386__
399 {
400 .callback = assign_all_busses,
401 .ident = "Compaq EVO N800c",
402 .matches = {
403 DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
404 DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
405 },
406 },
407#endif
Michal Schmidtc82bc5a2007-11-26 20:42:19 +0100408 {
409 .callback = set_bf_sort,
Jesse Barnes739db072008-07-07 09:55:26 -0700410 .ident = "HP ProLiant DL385 G2",
Michal Schmidtc82bc5a2007-11-26 20:42:19 +0100411 .matches = {
412 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
Jesse Barnes739db072008-07-07 09:55:26 -0700413 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
Michal Schmidtc82bc5a2007-11-26 20:42:19 +0100414 },
415 },
416 {
417 .callback = set_bf_sort,
Jesse Barnes739db072008-07-07 09:55:26 -0700418 .ident = "HP ProLiant DL585 G2",
Michal Schmidtc82bc5a2007-11-26 20:42:19 +0100419 .matches = {
420 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
Jesse Barnes739db072008-07-07 09:55:26 -0700421 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
Michal Schmidtc82bc5a2007-11-26 20:42:19 +0100422 },
423 },
Bjorn Helgaas284f5f92012-04-30 15:21:02 -0600424 {
425 .callback = set_scan_all,
426 .ident = "Stratus/NEC ftServer",
427 .matches = {
Myron Stowe12789982012-12-26 10:39:23 -0700428 DMI_MATCH(DMI_SYS_VENDOR, "Stratus"),
429 DMI_MATCH(DMI_PRODUCT_NAME, "ftServer"),
Bjorn Helgaas284f5f92012-04-30 15:21:02 -0600430 },
431 },
Charlotte Richardson51ac3d22015-02-02 09:36:23 -0600432 {
433 .callback = set_scan_all,
434 .ident = "Stratus/NEC ftServer",
435 .matches = {
436 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
437 DMI_MATCH(DMI_PRODUCT_NAME, "Express5800/R32"),
438 },
439 },
440 {
441 .callback = set_scan_all,
442 .ident = "Stratus/NEC ftServer",
443 .matches = {
444 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
445 DMI_MATCH(DMI_PRODUCT_NAME, "Express5800/R31"),
446 },
447 },
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800448 {}
449};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Yinghai Lu0df18ff2008-04-14 15:40:37 -0700451void __init dmi_check_pciprobe(void)
452{
453 dmi_check_system(pciprobe_dmi_table);
454}
455
Bjorn Helgaas49886cf2014-01-28 16:40:36 -0700456void pcibios_scan_root(int busnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Bjorn Helgaas289a24a2014-01-24 11:52:25 -0700458 struct pci_bus *bus;
459 struct pci_sysdata *sd;
460 LIST_HEAD(resources);
461
462 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
463 if (!sd) {
464 printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busnum);
Bjorn Helgaas49886cf2014-01-28 16:40:36 -0700465 return;
Bjorn Helgaas289a24a2014-01-24 11:52:25 -0700466 }
Bjorn Helgaas6616dbd2014-01-24 11:54:51 -0700467 sd->node = x86_pci_root_bus_node(busnum);
Bjorn Helgaas289a24a2014-01-24 11:52:25 -0700468 x86_pci_root_bus_resources(busnum, &resources);
469 printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
470 bus = pci_scan_root_bus(NULL, busnum, &pci_root_ops, sd, &resources);
471 if (!bus) {
472 pci_free_resource_list(&resources);
473 kfree(sd);
Yijing Wangb97ea282015-03-16 11:18:56 +0800474 return;
Bjorn Helgaas289a24a2014-01-24 11:52:25 -0700475 }
Yijing Wangb97ea282015-03-16 11:18:56 +0800476 pci_bus_add_devices(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
Yinghai Luc57ca652012-04-02 18:31:54 -0700478
Alex Nixon44de3392010-03-18 14:28:12 -0400479void __init pcibios_set_cache_line_size(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct cpuinfo_x86 *c = &boot_cpu_data;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
Dave Jones76b1a872009-10-14 16:31:39 -0400484 * Set PCI cacheline size to that of the CPU if the CPU has reported it.
485 * (For older CPUs that don't support cpuid, we se it to 32 bytes
486 * It's also good for 386/486s (which actually have 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * as quite a few PCI devices do not support smaller values.
488 */
Dave Jones76b1a872009-10-14 16:31:39 -0400489 if (c->x86_clflush_size > 0) {
490 pci_dfl_cache_line_size = c->x86_clflush_size >> 2;
491 printk(KERN_DEBUG "PCI: pci_cache_line_size set to %d bytes\n",
492 pci_dfl_cache_line_size << 2);
493 } else {
494 pci_dfl_cache_line_size = 32 >> 2;
495 printk(KERN_DEBUG "PCI: Unknown cacheline size. Setting to 32 bytes\n");
496 }
Alex Nixon44de3392010-03-18 14:28:12 -0400497}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Alex Nixon44de3392010-03-18 14:28:12 -0400499int __init pcibios_init(void)
500{
Adrian-Ken Rueegsegger35a6ae02016-03-23 11:34:29 +0100501 if (!raw_pci_ops && !raw_pci_ext_ops) {
Alex Nixon44de3392010-03-18 14:28:12 -0400502 printk(KERN_WARNING "PCI: System does not support PCI\n");
503 return 0;
504 }
505
506 pcibios_set_cache_line_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 pcibios_resource_survey();
508
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500509 if (pci_bf_sort >= pci_force_bf)
510 pci_sort_breadthfirst();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
512}
513
Mathias Krause4ac9cbf2014-08-25 23:26:35 +0200514char *__init pcibios_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 if (!strcmp(str, "off")) {
517 pci_probe = 0;
518 return NULL;
Matt Domsch6b4b78fe2006-09-29 15:23:23 -0500519 } else if (!strcmp(str, "bfsort")) {
520 pci_bf_sort = pci_force_bf;
521 return NULL;
522 } else if (!strcmp(str, "nobfsort")) {
523 pci_bf_sort = pci_force_nobf;
524 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526#ifdef CONFIG_PCI_BIOS
527 else if (!strcmp(str, "bios")) {
528 pci_probe = PCI_PROBE_BIOS;
529 return NULL;
530 } else if (!strcmp(str, "nobios")) {
531 pci_probe &= ~PCI_PROBE_BIOS;
532 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else if (!strcmp(str, "biosirq")) {
534 pci_probe |= PCI_BIOS_IRQ_SCAN;
535 return NULL;
jayalk@intworks.biz120bb422005-03-21 20:20:42 -0800536 } else if (!strncmp(str, "pirqaddr=", 9)) {
537 pirq_table_addr = simple_strtoul(str+9, NULL, 0);
538 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540#endif
541#ifdef CONFIG_PCI_DIRECT
542 else if (!strcmp(str, "conf1")) {
543 pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
544 return NULL;
545 }
546 else if (!strcmp(str, "conf2")) {
547 pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
548 return NULL;
549 }
550#endif
551#ifdef CONFIG_PCI_MMCONFIG
552 else if (!strcmp(str, "nommconf")) {
553 pci_probe &= ~PCI_PROBE_MMCONF;
554 return NULL;
555 }
Yinghai Lu5f0b2972008-04-14 16:08:25 -0700556 else if (!strcmp(str, "check_enable_amd_mmconf")) {
557 pci_probe |= PCI_CHECK_ENABLE_AMD_MMCONF;
558 return NULL;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#endif
561 else if (!strcmp(str, "noacpi")) {
562 acpi_noirq_set();
563 return NULL;
564 }
Andi Kleen0637a702006-09-26 10:52:41 +0200565 else if (!strcmp(str, "noearly")) {
566 pci_probe |= PCI_PROBE_NOEARLY;
567 return NULL;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 else if (!strcmp(str, "usepirqmask")) {
570 pci_probe |= PCI_USE_PIRQ_MASK;
571 return NULL;
572 } else if (!strncmp(str, "irqmask=", 8)) {
573 pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
574 return NULL;
575 } else if (!strncmp(str, "lastbus=", 8)) {
576 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
577 return NULL;
H. Peter Anvinc5f9ee32014-02-25 12:05:34 -0800578 } else if (!strcmp(str, "rom")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 pci_probe |= PCI_ASSIGN_ROMS;
580 return NULL;
Gary Hadebb71ad82008-05-12 13:57:46 -0700581 } else if (!strcmp(str, "norom")) {
582 pci_probe |= PCI_NOASSIGN_ROMS;
583 return NULL;
Mike Habeck7bd1c362010-05-12 11:14:32 -0700584 } else if (!strcmp(str, "nobar")) {
585 pci_probe |= PCI_NOASSIGN_BARS;
586 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } else if (!strcmp(str, "assign-busses")) {
588 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
589 return NULL;
Linus Torvalds236e9462009-06-24 16:23:03 -0700590 } else if (!strcmp(str, "use_crs")) {
591 pci_probe |= PCI_USE__CRS;
Gary Hade62f420f2007-10-03 15:56:51 -0700592 return NULL;
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700593 } else if (!strcmp(str, "nocrs")) {
594 pci_probe |= PCI_ROOT_NO_CRS;
595 return NULL;
=?UTF-8?q?Christian=20K=C3=B6nig?=f32ab752018-01-11 14:23:29 +0100596#ifdef CONFIG_PHYS_ADDR_T_64BIT
597 } else if (!strcmp(str, "big_root_window")) {
598 pci_probe |= PCI_BIG_ROOT_WINDOW;
599 return NULL;
600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else if (!strcmp(str, "routeirq")) {
602 pci_routeirq = 1;
603 return NULL;
Yinghai Lu13a6ddb2008-03-27 01:31:18 -0700604 } else if (!strcmp(str, "skip_isa_align")) {
605 pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
606 return NULL;
Stefan Assmanna9322f62008-06-11 16:35:14 +0200607 } else if (!strcmp(str, "noioapicquirk")) {
608 noioapicquirk = 1;
609 return NULL;
Stefan Assmann91979792008-06-11 16:35:15 +0200610 } else if (!strcmp(str, "ioapicreroute")) {
611 if (noioapicreroute != -1)
612 noioapicreroute = 0;
613 return NULL;
Stefan Assmann41b9eb22008-07-15 13:48:55 +0200614 } else if (!strcmp(str, "noioapicreroute")) {
615 if (noioapicreroute != -1)
616 noioapicreroute = 1;
617 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 return str;
620}
621
622unsigned int pcibios_assign_all_busses(void)
623{
624 return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
625}
626
Keith Buschd9c3d6f2016-01-12 13:18:08 -0700627#if defined(CONFIG_X86_DEV_DMA_OPS) && defined(CONFIG_PCI_DOMAINS)
628static LIST_HEAD(dma_domain_list);
629static DEFINE_SPINLOCK(dma_domain_list_lock);
630
631void add_dma_domain(struct dma_domain *domain)
632{
633 spin_lock(&dma_domain_list_lock);
634 list_add(&domain->node, &dma_domain_list);
635 spin_unlock(&dma_domain_list_lock);
636}
637EXPORT_SYMBOL_GPL(add_dma_domain);
638
639void del_dma_domain(struct dma_domain *domain)
640{
641 spin_lock(&dma_domain_list_lock);
642 list_del(&domain->node);
643 spin_unlock(&dma_domain_list_lock);
644}
645EXPORT_SYMBOL_GPL(del_dma_domain);
646
647static void set_dma_domain_ops(struct pci_dev *pdev)
648{
649 struct dma_domain *domain;
650
651 spin_lock(&dma_domain_list_lock);
652 list_for_each_entry(domain, &dma_domain_list, node) {
653 if (pci_domain_nr(pdev->bus) == domain->domain_nr) {
Bart Van Assche56579332017-01-20 13:04:02 -0800654 pdev->dev.dma_ops = domain->dma_ops;
Keith Buschd9c3d6f2016-01-12 13:18:08 -0700655 break;
656 }
657 }
658 spin_unlock(&dma_domain_list_lock);
659}
660#else
661static void set_dma_domain_ops(struct pci_dev *pdev) {}
662#endif
663
Keith Busch31618322016-09-13 09:05:40 -0600664static void set_dev_domain_options(struct pci_dev *pdev)
665{
666 if (is_vmd(pdev->bus))
667 pdev->hotplug_user_indicators = 1;
668}
669
Matthew Garrettf9a37be02012-12-05 14:33:27 -0700670int pcibios_add_device(struct pci_dev *dev)
671{
672 struct setup_data *data;
673 struct pci_setup_rom *rom;
674 u64 pa_data;
675
676 pa_data = boot_params.hdr.setup_data;
677 while (pa_data) {
Tom Lendackyf7750a72017-07-17 16:10:00 -0500678 data = memremap(pa_data, sizeof(*rom), MEMREMAP_WB);
Matt Fleming65694c52013-06-05 15:15:41 +0100679 if (!data)
680 return -ENOMEM;
Matthew Garrettf9a37be02012-12-05 14:33:27 -0700681
682 if (data->type == SETUP_PCI) {
683 rom = (struct pci_setup_rom *)data;
684
685 if ((pci_domain_nr(dev->bus) == rom->segment) &&
686 (dev->bus->number == rom->bus) &&
687 (PCI_SLOT(dev->devfn) == rom->device) &&
688 (PCI_FUNC(dev->devfn) == rom->function) &&
689 (dev->vendor == rom->vendor) &&
690 (dev->device == rom->devid)) {
Bjorn Helgaasdbd3fc32012-12-10 11:24:42 -0700691 dev->rom = pa_data +
692 offsetof(struct pci_setup_rom, romdata);
Matthew Garrettf9a37be02012-12-05 14:33:27 -0700693 dev->romlen = rom->pcilen;
694 }
695 }
696 pa_data = data->next;
Tom Lendackyf7750a72017-07-17 16:10:00 -0500697 memunmap(data);
Matthew Garrettf9a37be02012-12-05 14:33:27 -0700698 }
Keith Buschd9c3d6f2016-01-12 13:18:08 -0700699 set_dma_domain_ops(dev);
Keith Busch31618322016-09-13 09:05:40 -0600700 set_dev_domain_options(dev);
Matthew Garrettf9a37be02012-12-05 14:33:27 -0700701 return 0;
702}
703
Jiang Liu991de2e2015-06-10 16:54:59 +0800704int pcibios_enable_device(struct pci_dev *dev, int mask)
705{
Bjorn Helgaas6c777e82016-02-17 12:26:42 -0600706 int err;
707
708 if ((err = pci_enable_resources(dev, mask)) < 0)
709 return err;
710
711 if (!pci_dev_msi_enabled(dev))
712 return pcibios_enable_irq(dev);
713 return 0;
714}
715
716void pcibios_disable_device (struct pci_dev *dev)
717{
718 if (!pci_dev_msi_enabled(dev) && pcibios_disable_irq)
719 pcibios_disable_irq(dev);
Jiang Liu991de2e2015-06-10 16:54:59 +0800720}
721
Rui Wang153654d2017-02-28 21:34:28 +0800722#ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
723void pcibios_release_device(struct pci_dev *dev)
724{
725 if (atomic_dec_return(&dev->enable_cnt) >= 0)
726 pcibios_disable_device(dev);
727
728}
729#endif
730
Taku Izumi642c92d2012-10-30 15:26:18 +0900731int pci_ext_cfg_avail(void)
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700732{
733 if (raw_pci_ext_ops)
734 return 1;
735 else
736 return 0;
737}