PCI quirks: call boot IRQ quirks at end of device init and during resume
[sfrench/cifs-2.6.git] / arch / x86 / pci / common.c
1 /*
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>
9 #include <linux/ioport.h>
10 #include <linux/init.h>
11 #include <linux/dmi.h>
12
13 #include <asm/acpi.h>
14 #include <asm/segment.h>
15 #include <asm/io.h>
16 #include <asm/smp.h>
17
18 #include "pci.h"
19
20 unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
21                                 PCI_PROBE_MMCONF;
22
23 static int pci_bf_sort;
24 int pci_routeirq;
25 int noioapicquirk;
26 int noioapicreroute = 1;
27 int pcibios_last_bus = -1;
28 unsigned long pirq_table_addr;
29 struct pci_bus *pci_root_bus;
30 struct pci_raw_ops *raw_pci_ops;
31 struct pci_raw_ops *raw_pci_ext_ops;
32
33 int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
34                                                 int reg, int len, u32 *val)
35 {
36         if (reg < 256 && raw_pci_ops)
37                 return raw_pci_ops->read(domain, bus, devfn, reg, len, val);
38         if (raw_pci_ext_ops)
39                 return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
40         return -EINVAL;
41 }
42
43 int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
44                                                 int reg, int len, u32 val)
45 {
46         if (reg < 256 && raw_pci_ops)
47                 return raw_pci_ops->write(domain, bus, devfn, reg, len, val);
48         if (raw_pci_ext_ops)
49                 return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
50         return -EINVAL;
51 }
52
53 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
54 {
55         return raw_pci_read(pci_domain_nr(bus), bus->number,
56                                  devfn, where, size, value);
57 }
58
59 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
60 {
61         return raw_pci_write(pci_domain_nr(bus), bus->number,
62                                   devfn, where, size, value);
63 }
64
65 struct pci_ops pci_root_ops = {
66         .read = pci_read,
67         .write = pci_write,
68 };
69
70 /*
71  * legacy, numa, and acpi all want to call pcibios_scan_root
72  * from their initcalls. This flag prevents that.
73  */
74 int pcibios_scanned;
75
76 /*
77  * This interrupt-safe spinlock protects all accesses to PCI
78  * configuration space.
79  */
80 DEFINE_SPINLOCK(pci_config_lock);
81
82 static int __devinit can_skip_ioresource_align(const struct dmi_system_id *d)
83 {
84         pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
85         printk(KERN_INFO "PCI: %s detected, can skip ISA alignment\n", d->ident);
86         return 0;
87 }
88
89 static struct dmi_system_id can_skip_pciprobe_dmi_table[] __devinitdata = {
90 /*
91  * Systems where PCI IO resource ISA alignment can be skipped
92  * when the ISA enable bit in the bridge control is not set
93  */
94         {
95                 .callback = can_skip_ioresource_align,
96                 .ident = "IBM System x3800",
97                 .matches = {
98                         DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
99                         DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
100                 },
101         },
102         {
103                 .callback = can_skip_ioresource_align,
104                 .ident = "IBM System x3850",
105                 .matches = {
106                         DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
107                         DMI_MATCH(DMI_PRODUCT_NAME, "x3850"),
108                 },
109         },
110         {
111                 .callback = can_skip_ioresource_align,
112                 .ident = "IBM System x3950",
113                 .matches = {
114                         DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
115                         DMI_MATCH(DMI_PRODUCT_NAME, "x3950"),
116                 },
117         },
118         {}
119 };
120
121 void __init dmi_check_skip_isa_align(void)
122 {
123         dmi_check_system(can_skip_pciprobe_dmi_table);
124 }
125
126 /*
127  *  Called after each bus is probed, but before its children
128  *  are examined.
129  */
130
131 void __devinit  pcibios_fixup_bus(struct pci_bus *b)
132 {
133         pci_read_bridge_bases(b);
134 }
135
136 /*
137  * Only use DMI information to set this if nothing was passed
138  * on the kernel command line (which was parsed earlier).
139  */
140
141 static int __devinit set_bf_sort(const struct dmi_system_id *d)
142 {
143         if (pci_bf_sort == pci_bf_sort_default) {
144                 pci_bf_sort = pci_dmi_bf;
145                 printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
146         }
147         return 0;
148 }
149
150 /*
151  * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
152  */
153 #ifdef __i386__
154 static int __devinit assign_all_busses(const struct dmi_system_id *d)
155 {
156         pci_probe |= PCI_ASSIGN_ALL_BUSSES;
157         printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
158                         " (pci=assign-busses)\n", d->ident);
159         return 0;
160 }
161 #endif
162
163 static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
164 #ifdef __i386__
165 /*
166  * Laptops which need pci=assign-busses to see Cardbus cards
167  */
168         {
169                 .callback = assign_all_busses,
170                 .ident = "Samsung X20 Laptop",
171                 .matches = {
172                         DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
173                         DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
174                 },
175         },
176 #endif          /* __i386__ */
177         {
178                 .callback = set_bf_sort,
179                 .ident = "Dell PowerEdge 1950",
180                 .matches = {
181                         DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
182                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
183                 },
184         },
185         {
186                 .callback = set_bf_sort,
187                 .ident = "Dell PowerEdge 1955",
188                 .matches = {
189                         DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
190                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
191                 },
192         },
193         {
194                 .callback = set_bf_sort,
195                 .ident = "Dell PowerEdge 2900",
196                 .matches = {
197                         DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
198                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
199                 },
200         },
201         {
202                 .callback = set_bf_sort,
203                 .ident = "Dell PowerEdge 2950",
204                 .matches = {
205                         DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
206                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
207                 },
208         },
209         {
210                 .callback = set_bf_sort,
211                 .ident = "Dell PowerEdge R900",
212                 .matches = {
213                         DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
214                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
215                 },
216         },
217         {
218                 .callback = set_bf_sort,
219                 .ident = "HP ProLiant BL20p G3",
220                 .matches = {
221                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
222                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
223                 },
224         },
225         {
226                 .callback = set_bf_sort,
227                 .ident = "HP ProLiant BL20p G4",
228                 .matches = {
229                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
230                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
231                 },
232         },
233         {
234                 .callback = set_bf_sort,
235                 .ident = "HP ProLiant BL30p G1",
236                 .matches = {
237                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
238                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
239                 },
240         },
241         {
242                 .callback = set_bf_sort,
243                 .ident = "HP ProLiant BL25p G1",
244                 .matches = {
245                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
246                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
247                 },
248         },
249         {
250                 .callback = set_bf_sort,
251                 .ident = "HP ProLiant BL35p G1",
252                 .matches = {
253                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
254                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
255                 },
256         },
257         {
258                 .callback = set_bf_sort,
259                 .ident = "HP ProLiant BL45p G1",
260                 .matches = {
261                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
262                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
263                 },
264         },
265         {
266                 .callback = set_bf_sort,
267                 .ident = "HP ProLiant BL45p G2",
268                 .matches = {
269                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
270                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
271                 },
272         },
273         {
274                 .callback = set_bf_sort,
275                 .ident = "HP ProLiant BL460c G1",
276                 .matches = {
277                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
278                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
279                 },
280         },
281         {
282                 .callback = set_bf_sort,
283                 .ident = "HP ProLiant BL465c G1",
284                 .matches = {
285                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
286                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
287                 },
288         },
289         {
290                 .callback = set_bf_sort,
291                 .ident = "HP ProLiant BL480c G1",
292                 .matches = {
293                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
294                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
295                 },
296         },
297         {
298                 .callback = set_bf_sort,
299                 .ident = "HP ProLiant BL685c G1",
300                 .matches = {
301                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
302                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
303                 },
304         },
305         {
306                 .callback = set_bf_sort,
307                 .ident = "HP ProLiant DL360",
308                 .matches = {
309                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
310                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL360"),
311                 },
312         },
313         {
314                 .callback = set_bf_sort,
315                 .ident = "HP ProLiant DL380",
316                 .matches = {
317                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
318                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL380"),
319                 },
320         },
321 #ifdef __i386__
322         {
323                 .callback = assign_all_busses,
324                 .ident = "Compaq EVO N800c",
325                 .matches = {
326                         DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
327                         DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
328                 },
329         },
330 #endif
331         {
332                 .callback = set_bf_sort,
333                 .ident = "HP ProLiant DL385 G2",
334                 .matches = {
335                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
336                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
337                 },
338         },
339         {
340                 .callback = set_bf_sort,
341                 .ident = "HP ProLiant DL585 G2",
342                 .matches = {
343                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
344                         DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
345                 },
346         },
347         {}
348 };
349
350 void __init dmi_check_pciprobe(void)
351 {
352         dmi_check_system(pciprobe_dmi_table);
353 }
354
355 struct pci_bus * __devinit pcibios_scan_root(int busnum)
356 {
357         struct pci_bus *bus = NULL;
358         struct pci_sysdata *sd;
359
360         while ((bus = pci_find_next_bus(bus)) != NULL) {
361                 if (bus->number == busnum) {
362                         /* Already scanned */
363                         return bus;
364                 }
365         }
366
367         /* Allocate per-root-bus (not per bus) arch-specific data.
368          * TODO: leak; this memory is never freed.
369          * It's arguable whether it's worth the trouble to care.
370          */
371         sd = kzalloc(sizeof(*sd), GFP_KERNEL);
372         if (!sd) {
373                 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
374                 return NULL;
375         }
376
377         sd->node = get_mp_bus_to_node(busnum);
378
379         printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
380         bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
381         if (!bus)
382                 kfree(sd);
383
384         return bus;
385 }
386
387 extern u8 pci_cache_line_size;
388
389 static int __init pcibios_init(void)
390 {
391         struct cpuinfo_x86 *c = &boot_cpu_data;
392
393         if (!raw_pci_ops) {
394                 printk(KERN_WARNING "PCI: System does not support PCI\n");
395                 return 0;
396         }
397
398         /*
399          * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
400          * and P4. It's also good for 386/486s (which actually have 16)
401          * as quite a few PCI devices do not support smaller values.
402          */
403         pci_cache_line_size = 32 >> 2;
404         if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
405                 pci_cache_line_size = 64 >> 2;  /* K7 & K8 */
406         else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
407                 pci_cache_line_size = 128 >> 2; /* P4 */
408
409         pcibios_resource_survey();
410
411         if (pci_bf_sort >= pci_force_bf)
412                 pci_sort_breadthfirst();
413         return 0;
414 }
415
416 subsys_initcall(pcibios_init);
417
418 char * __devinit  pcibios_setup(char *str)
419 {
420         if (!strcmp(str, "off")) {
421                 pci_probe = 0;
422                 return NULL;
423         } else if (!strcmp(str, "bfsort")) {
424                 pci_bf_sort = pci_force_bf;
425                 return NULL;
426         } else if (!strcmp(str, "nobfsort")) {
427                 pci_bf_sort = pci_force_nobf;
428                 return NULL;
429         }
430 #ifdef CONFIG_PCI_BIOS
431         else if (!strcmp(str, "bios")) {
432                 pci_probe = PCI_PROBE_BIOS;
433                 return NULL;
434         } else if (!strcmp(str, "nobios")) {
435                 pci_probe &= ~PCI_PROBE_BIOS;
436                 return NULL;
437         } else if (!strcmp(str, "biosirq")) {
438                 pci_probe |= PCI_BIOS_IRQ_SCAN;
439                 return NULL;
440         } else if (!strncmp(str, "pirqaddr=", 9)) {
441                 pirq_table_addr = simple_strtoul(str+9, NULL, 0);
442                 return NULL;
443         }
444 #endif
445 #ifdef CONFIG_PCI_DIRECT
446         else if (!strcmp(str, "conf1")) {
447                 pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
448                 return NULL;
449         }
450         else if (!strcmp(str, "conf2")) {
451                 pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
452                 return NULL;
453         }
454 #endif
455 #ifdef CONFIG_PCI_MMCONFIG
456         else if (!strcmp(str, "nommconf")) {
457                 pci_probe &= ~PCI_PROBE_MMCONF;
458                 return NULL;
459         }
460         else if (!strcmp(str, "check_enable_amd_mmconf")) {
461                 pci_probe |= PCI_CHECK_ENABLE_AMD_MMCONF;
462                 return NULL;
463         }
464 #endif
465         else if (!strcmp(str, "noacpi")) {
466                 acpi_noirq_set();
467                 return NULL;
468         }
469         else if (!strcmp(str, "noearly")) {
470                 pci_probe |= PCI_PROBE_NOEARLY;
471                 return NULL;
472         }
473 #ifndef CONFIG_X86_VISWS
474         else if (!strcmp(str, "usepirqmask")) {
475                 pci_probe |= PCI_USE_PIRQ_MASK;
476                 return NULL;
477         } else if (!strncmp(str, "irqmask=", 8)) {
478                 pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
479                 return NULL;
480         } else if (!strncmp(str, "lastbus=", 8)) {
481                 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
482                 return NULL;
483         }
484 #endif
485         else if (!strcmp(str, "rom")) {
486                 pci_probe |= PCI_ASSIGN_ROMS;
487                 return NULL;
488         } else if (!strcmp(str, "assign-busses")) {
489                 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
490                 return NULL;
491         } else if (!strcmp(str, "use_crs")) {
492                 pci_probe |= PCI_USE__CRS;
493                 return NULL;
494         } else if (!strcmp(str, "routeirq")) {
495                 pci_routeirq = 1;
496                 return NULL;
497         } else if (!strcmp(str, "skip_isa_align")) {
498                 pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
499                 return NULL;
500         } else if (!strcmp(str, "noioapicquirk")) {
501                 noioapicquirk = 1;
502                 return NULL;
503         } else if (!strcmp(str, "ioapicreroute")) {
504                 if (noioapicreroute != -1)
505                         noioapicreroute = 0;
506                 return NULL;
507         }
508         return str;
509 }
510
511 unsigned int pcibios_assign_all_busses(void)
512 {
513         return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
514 }
515
516 int pcibios_enable_device(struct pci_dev *dev, int mask)
517 {
518         int err;
519
520         if ((err = pci_enable_resources(dev, mask)) < 0)
521                 return err;
522
523         if (!dev->msi_enabled)
524                 return pcibios_enable_irq(dev);
525         return 0;
526 }
527
528 void pcibios_disable_device (struct pci_dev *dev)
529 {
530         if (!dev->msi_enabled && pcibios_disable_irq)
531                 pcibios_disable_irq(dev);
532 }
533
534 struct pci_bus * __devinit pci_scan_bus_on_node(int busno, struct pci_ops *ops, int node)
535 {
536         struct pci_bus *bus = NULL;
537         struct pci_sysdata *sd;
538
539         /*
540          * Allocate per-root-bus (not per bus) arch-specific data.
541          * TODO: leak; this memory is never freed.
542          * It's arguable whether it's worth the trouble to care.
543          */
544         sd = kzalloc(sizeof(*sd), GFP_KERNEL);
545         if (!sd) {
546                 printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
547                 return NULL;
548         }
549         sd->node = node;
550         bus = pci_scan_bus(busno, ops, sd);
551         if (!bus)
552                 kfree(sd);
553
554         return bus;
555 }
556
557 struct pci_bus * __devinit pci_scan_bus_with_sysdata(int busno)
558 {
559         return pci_scan_bus_on_node(busno, &pci_root_ops, -1);
560 }