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