Merge branch 'pci/vpd'
[muen/linux.git] / drivers / pci / quirks.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file contains work-arounds for many known PCI hardware bugs.
4  * Devices present only on certain architectures (host bridges et cetera)
5  * should be handled in arch-specific code.
6  *
7  * Note: any quirks for hotpluggable devices must _NOT_ be declared __init.
8  *
9  * Copyright (c) 1999 Martin Mares <mj@ucw.cz>
10  *
11  * Init/reset quirks for USB host controllers should be in the USB quirks
12  * file, where their drivers can use them.
13  */
14
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/pci.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/acpi.h>
22 #include <linux/dmi.h>
23 #include <linux/pci-aspm.h>
24 #include <linux/ioport.h>
25 #include <linux/sched.h>
26 #include <linux/ktime.h>
27 #include <linux/mm.h>
28 #include <linux/platform_data/x86/apple.h>
29 #include <asm/dma.h>    /* isa_dma_bridge_buggy */
30 #include "pci.h"
31
32 /*
33  * Decoding should be disabled for a PCI device during BAR sizing to avoid
34  * conflict. But doing so may cause problems on host bridge and perhaps other
35  * key system devices. For devices that need to have mmio decoding always-on,
36  * we need to set the dev->mmio_always_on bit.
37  */
38 static void quirk_mmio_always_on(struct pci_dev *dev)
39 {
40         dev->mmio_always_on = 1;
41 }
42 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_ANY_ID, PCI_ANY_ID,
43                                 PCI_CLASS_BRIDGE_HOST, 8, quirk_mmio_always_on);
44
45 /* The Mellanox Tavor device gives false positive parity errors
46  * Mark this device with a broken_parity_status, to allow
47  * PCI scanning code to "skip" this now blacklisted device.
48  */
49 static void quirk_mellanox_tavor(struct pci_dev *dev)
50 {
51         dev->broken_parity_status = 1;  /* This device gives false positives */
52 }
53 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR, quirk_mellanox_tavor);
54 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR_BRIDGE, quirk_mellanox_tavor);
55
56 /* Deal with broken BIOSes that neglect to enable passive release,
57    which can cause problems in combination with the 82441FX/PPro MTRRs */
58 static void quirk_passive_release(struct pci_dev *dev)
59 {
60         struct pci_dev *d = NULL;
61         unsigned char dlc;
62
63         /* We have to make sure a particular bit is set in the PIIX3
64            ISA bridge, so we have to go out and find it. */
65         while ((d = pci_get_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0, d))) {
66                 pci_read_config_byte(d, 0x82, &dlc);
67                 if (!(dlc & 1<<1)) {
68                         pci_info(d, "PIIX3: Enabling Passive Release\n");
69                         dlc |= 1<<1;
70                         pci_write_config_byte(d, 0x82, dlc);
71                 }
72         }
73 }
74 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82441,      quirk_passive_release);
75 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82441,      quirk_passive_release);
76
77 /*  The VIA VP2/VP3/MVP3 seem to have some 'features'. There may be a workaround
78     but VIA don't answer queries. If you happen to have good contacts at VIA
79     ask them for me please -- Alan
80
81     This appears to be BIOS not version dependent. So presumably there is a
82     chipset level fix */
83
84 static void quirk_isa_dma_hangs(struct pci_dev *dev)
85 {
86         if (!isa_dma_bridge_buggy) {
87                 isa_dma_bridge_buggy = 1;
88                 pci_info(dev, "Activating ISA DMA hang workarounds\n");
89         }
90 }
91         /*
92          * Its not totally clear which chipsets are the problematic ones
93          * We know 82C586 and 82C596 variants are affected.
94          */
95 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_82C586_0,     quirk_isa_dma_hangs);
96 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_82C596,       quirk_isa_dma_hangs);
97 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82371SB_0,  quirk_isa_dma_hangs);
98 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AL,       PCI_DEVICE_ID_AL_M1533,         quirk_isa_dma_hangs);
99 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NEC,      PCI_DEVICE_ID_NEC_CBUS_1,       quirk_isa_dma_hangs);
100 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NEC,      PCI_DEVICE_ID_NEC_CBUS_2,       quirk_isa_dma_hangs);
101 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NEC,      PCI_DEVICE_ID_NEC_CBUS_3,       quirk_isa_dma_hangs);
102
103 /*
104  * Intel NM10 "TigerPoint" LPC PM1a_STS.BM_STS must be clear
105  * for some HT machines to use C4 w/o hanging.
106  */
107 static void quirk_tigerpoint_bm_sts(struct pci_dev *dev)
108 {
109         u32 pmbase;
110         u16 pm1a;
111
112         pci_read_config_dword(dev, 0x40, &pmbase);
113         pmbase = pmbase & 0xff80;
114         pm1a = inw(pmbase);
115
116         if (pm1a & 0x10) {
117                 pci_info(dev, FW_BUG "TigerPoint LPC.BM_STS cleared\n");
118                 outw(0x10, pmbase);
119         }
120 }
121 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_TGP_LPC, quirk_tigerpoint_bm_sts);
122
123 /*
124  *      Chipsets where PCI->PCI transfers vanish or hang
125  */
126 static void quirk_nopcipci(struct pci_dev *dev)
127 {
128         if ((pci_pci_problems & PCIPCI_FAIL) == 0) {
129                 pci_info(dev, "Disabling direct PCI/PCI transfers\n");
130                 pci_pci_problems |= PCIPCI_FAIL;
131         }
132 }
133 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SI,       PCI_DEVICE_ID_SI_5597,          quirk_nopcipci);
134 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SI,       PCI_DEVICE_ID_SI_496,           quirk_nopcipci);
135
136 static void quirk_nopciamd(struct pci_dev *dev)
137 {
138         u8 rev;
139         pci_read_config_byte(dev, 0x08, &rev);
140         if (rev == 0x13) {
141                 /* Erratum 24 */
142                 pci_info(dev, "Chipset erratum: Disabling direct PCI/AGP transfers\n");
143                 pci_pci_problems |= PCIAGP_FAIL;
144         }
145 }
146 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD,      PCI_DEVICE_ID_AMD_8151_0,       quirk_nopciamd);
147
148 /*
149  *      Triton requires workarounds to be used by the drivers
150  */
151 static void quirk_triton(struct pci_dev *dev)
152 {
153         if ((pci_pci_problems&PCIPCI_TRITON) == 0) {
154                 pci_info(dev, "Limiting direct PCI/PCI transfers\n");
155                 pci_pci_problems |= PCIPCI_TRITON;
156         }
157 }
158 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82437,      quirk_triton);
159 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82437VX,    quirk_triton);
160 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82439,      quirk_triton);
161 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82439TX,    quirk_triton);
162
163 /*
164  *      VIA Apollo KT133 needs PCI latency patch
165  *      Made according to a windows driver based patch by George E. Breese
166  *      see PCI Latency Adjust on http://www.viahardware.com/download/viatweak.shtm
167  *      Also see http://www.au-ja.org/review-kt133a-1-en.phtml for
168  *      the info on which Mr Breese based his work.
169  *
170  *      Updated based on further information from the site and also on
171  *      information provided by VIA
172  */
173 static void quirk_vialatency(struct pci_dev *dev)
174 {
175         struct pci_dev *p;
176         u8 busarb;
177         /* Ok we have a potential problem chipset here. Now see if we have
178            a buggy southbridge */
179
180         p = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, NULL);
181         if (p != NULL) {
182                 /* 0x40 - 0x4f == 686B, 0x10 - 0x2f == 686A; thanks Dan Hollis */
183                 /* Check for buggy part revisions */
184                 if (p->revision < 0x40 || p->revision > 0x42)
185                         goto exit;
186         } else {
187                 p = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231, NULL);
188                 if (p == NULL)  /* No problem parts */
189                         goto exit;
190                 /* Check for buggy part revisions */
191                 if (p->revision < 0x10 || p->revision > 0x12)
192                         goto exit;
193         }
194
195         /*
196          *      Ok we have the problem. Now set the PCI master grant to
197          *      occur every master grant. The apparent bug is that under high
198          *      PCI load (quite common in Linux of course) you can get data
199          *      loss when the CPU is held off the bus for 3 bus master requests
200          *      This happens to include the IDE controllers....
201          *
202          *      VIA only apply this fix when an SB Live! is present but under
203          *      both Linux and Windows this isn't enough, and we have seen
204          *      corruption without SB Live! but with things like 3 UDMA IDE
205          *      controllers. So we ignore that bit of the VIA recommendation..
206          */
207
208         pci_read_config_byte(dev, 0x76, &busarb);
209         /* Set bit 4 and bi 5 of byte 76 to 0x01
210            "Master priority rotation on every PCI master grant */
211         busarb &= ~(1<<5);
212         busarb |= (1<<4);
213         pci_write_config_byte(dev, 0x76, busarb);
214         pci_info(dev, "Applying VIA southbridge workaround\n");
215 exit:
216         pci_dev_put(p);
217 }
218 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_8363_0,       quirk_vialatency);
219 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_8371_1,       quirk_vialatency);
220 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_8361,         quirk_vialatency);
221 /* Must restore this on a resume from RAM */
222 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8363_0,       quirk_vialatency);
223 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8371_1,       quirk_vialatency);
224 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8361,         quirk_vialatency);
225
226 /*
227  *      VIA Apollo VP3 needs ETBF on BT848/878
228  */
229 static void quirk_viaetbf(struct pci_dev *dev)
230 {
231         if ((pci_pci_problems&PCIPCI_VIAETBF) == 0) {
232                 pci_info(dev, "Limiting direct PCI/PCI transfers\n");
233                 pci_pci_problems |= PCIPCI_VIAETBF;
234         }
235 }
236 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_82C597_0,     quirk_viaetbf);
237
238 static void quirk_vsfx(struct pci_dev *dev)
239 {
240         if ((pci_pci_problems&PCIPCI_VSFX) == 0) {
241                 pci_info(dev, "Limiting direct PCI/PCI transfers\n");
242                 pci_pci_problems |= PCIPCI_VSFX;
243         }
244 }
245 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_82C576,       quirk_vsfx);
246
247 /*
248  *      Ali Magik requires workarounds to be used by the drivers
249  *      that DMA to AGP space. Latency must be set to 0xA and triton
250  *      workaround applied too
251  *      [Info kindly provided by ALi]
252  */
253 static void quirk_alimagik(struct pci_dev *dev)
254 {
255         if ((pci_pci_problems&PCIPCI_ALIMAGIK) == 0) {
256                 pci_info(dev, "Limiting direct PCI/PCI transfers\n");
257                 pci_pci_problems |= PCIPCI_ALIMAGIK|PCIPCI_TRITON;
258         }
259 }
260 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AL,       PCI_DEVICE_ID_AL_M1647,         quirk_alimagik);
261 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AL,       PCI_DEVICE_ID_AL_M1651,         quirk_alimagik);
262
263 /*
264  *      Natoma has some interesting boundary conditions with Zoran stuff
265  *      at least
266  */
267 static void quirk_natoma(struct pci_dev *dev)
268 {
269         if ((pci_pci_problems&PCIPCI_NATOMA) == 0) {
270                 pci_info(dev, "Limiting direct PCI/PCI transfers\n");
271                 pci_pci_problems |= PCIPCI_NATOMA;
272         }
273 }
274 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82441,      quirk_natoma);
275 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82443LX_0,  quirk_natoma);
276 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82443LX_1,  quirk_natoma);
277 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82443BX_0,  quirk_natoma);
278 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82443BX_1,  quirk_natoma);
279 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82443BX_2,  quirk_natoma);
280
281 /*
282  *  This chip can cause PCI parity errors if config register 0xA0 is read
283  *  while DMAs are occurring.
284  */
285 static void quirk_citrine(struct pci_dev *dev)
286 {
287         dev->cfg_size = 0xA0;
288 }
289 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,     PCI_DEVICE_ID_IBM_CITRINE,      quirk_citrine);
290
291 /*
292  * This chip can cause bus lockups if config addresses above 0x600
293  * are read or written.
294  */
295 static void quirk_nfp6000(struct pci_dev *dev)
296 {
297         dev->cfg_size = 0x600;
298 }
299 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_NETRONOME,       PCI_DEVICE_ID_NETRONOME_NFP4000,        quirk_nfp6000);
300 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_NETRONOME,       PCI_DEVICE_ID_NETRONOME_NFP6000,        quirk_nfp6000);
301 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_NETRONOME,       PCI_DEVICE_ID_NETRONOME_NFP6000_VF,     quirk_nfp6000);
302
303 /*  On IBM Crocodile ipr SAS adapters, expand BAR to system page size */
304 static void quirk_extend_bar_to_page(struct pci_dev *dev)
305 {
306         int i;
307
308         for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
309                 struct resource *r = &dev->resource[i];
310
311                 if (r->flags & IORESOURCE_MEM && resource_size(r) < PAGE_SIZE) {
312                         r->end = PAGE_SIZE - 1;
313                         r->start = 0;
314                         r->flags |= IORESOURCE_UNSET;
315                         pci_info(dev, "expanded BAR %d to page size: %pR\n",
316                                  i, r);
317                 }
318         }
319 }
320 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, 0x034a, quirk_extend_bar_to_page);
321
322 /*
323  *  S3 868 and 968 chips report region size equal to 32M, but they decode 64M.
324  *  If it's needed, re-allocate the region.
325  */
326 static void quirk_s3_64M(struct pci_dev *dev)
327 {
328         struct resource *r = &dev->resource[0];
329
330         if ((r->start & 0x3ffffff) || r->end != r->start + 0x3ffffff) {
331                 r->flags |= IORESOURCE_UNSET;
332                 r->start = 0;
333                 r->end = 0x3ffffff;
334         }
335 }
336 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_S3,      PCI_DEVICE_ID_S3_868,           quirk_s3_64M);
337 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_S3,      PCI_DEVICE_ID_S3_968,           quirk_s3_64M);
338
339 static void quirk_io(struct pci_dev *dev, int pos, unsigned size,
340                      const char *name)
341 {
342         u32 region;
343         struct pci_bus_region bus_region;
344         struct resource *res = dev->resource + pos;
345
346         pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + (pos << 2), &region);
347
348         if (!region)
349                 return;
350
351         res->name = pci_name(dev);
352         res->flags = region & ~PCI_BASE_ADDRESS_IO_MASK;
353         res->flags |=
354                 (IORESOURCE_IO | IORESOURCE_PCI_FIXED | IORESOURCE_SIZEALIGN);
355         region &= ~(size - 1);
356
357         /* Convert from PCI bus to resource space */
358         bus_region.start = region;
359         bus_region.end = region + size - 1;
360         pcibios_bus_to_resource(dev->bus, res, &bus_region);
361
362         pci_info(dev, FW_BUG "%s quirk: reg 0x%x: %pR\n",
363                  name, PCI_BASE_ADDRESS_0 + (pos << 2), res);
364 }
365
366 /*
367  * Some CS5536 BIOSes (for example, the Soekris NET5501 board w/ comBIOS
368  * ver. 1.33  20070103) don't set the correct ISA PCI region header info.
369  * BAR0 should be 8 bytes; instead, it may be set to something like 8k
370  * (which conflicts w/ BAR1's memory range).
371  *
372  * CS553x's ISA PCI BARs may also be read-only (ref:
373  * https://bugzilla.kernel.org/show_bug.cgi?id=85991 - Comment #4 forward).
374  */
375 static void quirk_cs5536_vsa(struct pci_dev *dev)
376 {
377         static char *name = "CS5536 ISA bridge";
378
379         if (pci_resource_len(dev, 0) != 8) {
380                 quirk_io(dev, 0,   8, name);    /* SMB */
381                 quirk_io(dev, 1, 256, name);    /* GPIO */
382                 quirk_io(dev, 2,  64, name);    /* MFGPT */
383                 pci_info(dev, "%s bug detected (incorrect header); workaround applied\n",
384                          name);
385         }
386 }
387 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA, quirk_cs5536_vsa);
388
389 static void quirk_io_region(struct pci_dev *dev, int port,
390                                 unsigned size, int nr, const char *name)
391 {
392         u16 region;
393         struct pci_bus_region bus_region;
394         struct resource *res = dev->resource + nr;
395
396         pci_read_config_word(dev, port, &region);
397         region &= ~(size - 1);
398
399         if (!region)
400                 return;
401
402         res->name = pci_name(dev);
403         res->flags = IORESOURCE_IO;
404
405         /* Convert from PCI bus to resource space */
406         bus_region.start = region;
407         bus_region.end = region + size - 1;
408         pcibios_bus_to_resource(dev->bus, res, &bus_region);
409
410         if (!pci_claim_resource(dev, nr))
411                 pci_info(dev, "quirk: %pR claimed by %s\n", res, name);
412 }
413
414 /*
415  *      ATI Northbridge setups MCE the processor if you even
416  *      read somewhere between 0x3b0->0x3bb or read 0x3d3
417  */
418 static void quirk_ati_exploding_mce(struct pci_dev *dev)
419 {
420         pci_info(dev, "ATI Northbridge, reserving I/O ports 0x3b0 to 0x3bb\n");
421         /* Mae rhaid i ni beidio ag edrych ar y lleoliadiau I/O hyn */
422         request_region(0x3b0, 0x0C, "RadeonIGP");
423         request_region(0x3d3, 0x01, "RadeonIGP");
424 }
425 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI,      PCI_DEVICE_ID_ATI_RS100,   quirk_ati_exploding_mce);
426
427 /*
428  * In the AMD NL platform, this device ([1022:7912]) has a class code of
429  * PCI_CLASS_SERIAL_USB_XHCI (0x0c0330), which means the xhci driver will
430  * claim it.
431  * But the dwc3 driver is a more specific driver for this device, and we'd
432  * prefer to use it instead of xhci. To prevent xhci from claiming the
433  * device, change the class code to 0x0c03fe, which the PCI r3.0 spec
434  * defines as "USB device (not host controller)". The dwc3 driver can then
435  * claim it based on its Vendor and Device ID.
436  */
437 static void quirk_amd_nl_class(struct pci_dev *pdev)
438 {
439         u32 class = pdev->class;
440
441         /* Use "USB Device (not host controller)" class */
442         pdev->class = PCI_CLASS_SERIAL_USB_DEVICE;
443         pci_info(pdev, "PCI class overridden (%#08x -> %#08x) so dwc3 driver can claim this instead of xhci\n",
444                  class, pdev->class);
445 }
446 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_NL_USB,
447                 quirk_amd_nl_class);
448
449 /*
450  * Let's make the southbridge information explicit instead
451  * of having to worry about people probing the ACPI areas,
452  * for example.. (Yes, it happens, and if you read the wrong
453  * ACPI register it will put the machine to sleep with no
454  * way of waking it up again. Bummer).
455  *
456  * ALI M7101: Two IO regions pointed to by words at
457  *      0xE0 (64 bytes of ACPI registers)
458  *      0xE2 (32 bytes of SMB registers)
459  */
460 static void quirk_ali7101_acpi(struct pci_dev *dev)
461 {
462         quirk_io_region(dev, 0xE0, 64, PCI_BRIDGE_RESOURCES, "ali7101 ACPI");
463         quirk_io_region(dev, 0xE2, 32, PCI_BRIDGE_RESOURCES+1, "ali7101 SMB");
464 }
465 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AL,      PCI_DEVICE_ID_AL_M7101,         quirk_ali7101_acpi);
466
467 static void piix4_io_quirk(struct pci_dev *dev, const char *name, unsigned int port, unsigned int enable)
468 {
469         u32 devres;
470         u32 mask, size, base;
471
472         pci_read_config_dword(dev, port, &devres);
473         if ((devres & enable) != enable)
474                 return;
475         mask = (devres >> 16) & 15;
476         base = devres & 0xffff;
477         size = 16;
478         for (;;) {
479                 unsigned bit = size >> 1;
480                 if ((bit & mask) == bit)
481                         break;
482                 size = bit;
483         }
484         /*
485          * For now we only print it out. Eventually we'll want to
486          * reserve it (at least if it's in the 0x1000+ range), but
487          * let's get enough confirmation reports first.
488          */
489         base &= -size;
490         pci_info(dev, "%s PIO at %04x-%04x\n", name, base, base + size - 1);
491 }
492
493 static void piix4_mem_quirk(struct pci_dev *dev, const char *name, unsigned int port, unsigned int enable)
494 {
495         u32 devres;
496         u32 mask, size, base;
497
498         pci_read_config_dword(dev, port, &devres);
499         if ((devres & enable) != enable)
500                 return;
501         base = devres & 0xffff0000;
502         mask = (devres & 0x3f) << 16;
503         size = 128 << 16;
504         for (;;) {
505                 unsigned bit = size >> 1;
506                 if ((bit & mask) == bit)
507                         break;
508                 size = bit;
509         }
510         /*
511          * For now we only print it out. Eventually we'll want to
512          * reserve it, but let's get enough confirmation reports first.
513          */
514         base &= -size;
515         pci_info(dev, "%s MMIO at %04x-%04x\n", name, base, base + size - 1);
516 }
517
518 /*
519  * PIIX4 ACPI: Two IO regions pointed to by longwords at
520  *      0x40 (64 bytes of ACPI registers)
521  *      0x90 (16 bytes of SMB registers)
522  * and a few strange programmable PIIX4 device resources.
523  */
524 static void quirk_piix4_acpi(struct pci_dev *dev)
525 {
526         u32 res_a;
527
528         quirk_io_region(dev, 0x40, 64, PCI_BRIDGE_RESOURCES, "PIIX4 ACPI");
529         quirk_io_region(dev, 0x90, 16, PCI_BRIDGE_RESOURCES+1, "PIIX4 SMB");
530
531         /* Device resource A has enables for some of the other ones */
532         pci_read_config_dword(dev, 0x5c, &res_a);
533
534         piix4_io_quirk(dev, "PIIX4 devres B", 0x60, 3 << 21);
535         piix4_io_quirk(dev, "PIIX4 devres C", 0x64, 3 << 21);
536
537         /* Device resource D is just bitfields for static resources */
538
539         /* Device 12 enabled? */
540         if (res_a & (1 << 29)) {
541                 piix4_io_quirk(dev, "PIIX4 devres E", 0x68, 1 << 20);
542                 piix4_mem_quirk(dev, "PIIX4 devres F", 0x6c, 1 << 7);
543         }
544         /* Device 13 enabled? */
545         if (res_a & (1 << 30)) {
546                 piix4_io_quirk(dev, "PIIX4 devres G", 0x70, 1 << 20);
547                 piix4_mem_quirk(dev, "PIIX4 devres H", 0x74, 1 << 7);
548         }
549         piix4_io_quirk(dev, "PIIX4 devres I", 0x78, 1 << 20);
550         piix4_io_quirk(dev, "PIIX4 devres J", 0x7c, 1 << 20);
551 }
552 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82371AB_3,  quirk_piix4_acpi);
553 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82443MX_3,  quirk_piix4_acpi);
554
555 #define ICH_PMBASE      0x40
556 #define ICH_ACPI_CNTL   0x44
557 #define  ICH4_ACPI_EN   0x10
558 #define  ICH6_ACPI_EN   0x80
559 #define ICH4_GPIOBASE   0x58
560 #define ICH4_GPIO_CNTL  0x5c
561 #define  ICH4_GPIO_EN   0x10
562 #define ICH6_GPIOBASE   0x48
563 #define ICH6_GPIO_CNTL  0x4c
564 #define  ICH6_GPIO_EN   0x10
565
566 /*
567  * ICH4, ICH4-M, ICH5, ICH5-M ACPI: Three IO regions pointed to by longwords at
568  *      0x40 (128 bytes of ACPI, GPIO & TCO registers)
569  *      0x58 (64 bytes of GPIO I/O space)
570  */
571 static void quirk_ich4_lpc_acpi(struct pci_dev *dev)
572 {
573         u8 enable;
574
575         /*
576          * The check for PCIBIOS_MIN_IO is to ensure we won't create a conflict
577          * with low legacy (and fixed) ports. We don't know the decoding
578          * priority and can't tell whether the legacy device or the one created
579          * here is really at that address.  This happens on boards with broken
580          * BIOSes.
581         */
582
583         pci_read_config_byte(dev, ICH_ACPI_CNTL, &enable);
584         if (enable & ICH4_ACPI_EN)
585                 quirk_io_region(dev, ICH_PMBASE, 128, PCI_BRIDGE_RESOURCES,
586                                  "ICH4 ACPI/GPIO/TCO");
587
588         pci_read_config_byte(dev, ICH4_GPIO_CNTL, &enable);
589         if (enable & ICH4_GPIO_EN)
590                 quirk_io_region(dev, ICH4_GPIOBASE, 64, PCI_BRIDGE_RESOURCES+1,
591                                 "ICH4 GPIO");
592 }
593 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801AA_0,         quirk_ich4_lpc_acpi);
594 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801AB_0,         quirk_ich4_lpc_acpi);
595 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801BA_0,         quirk_ich4_lpc_acpi);
596 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801BA_10,        quirk_ich4_lpc_acpi);
597 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801CA_0,         quirk_ich4_lpc_acpi);
598 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801CA_12,        quirk_ich4_lpc_acpi);
599 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801DB_0,         quirk_ich4_lpc_acpi);
600 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801DB_12,        quirk_ich4_lpc_acpi);
601 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82801EB_0,         quirk_ich4_lpc_acpi);
602 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_ESB_1,             quirk_ich4_lpc_acpi);
603
604 static void ich6_lpc_acpi_gpio(struct pci_dev *dev)
605 {
606         u8 enable;
607
608         pci_read_config_byte(dev, ICH_ACPI_CNTL, &enable);
609         if (enable & ICH6_ACPI_EN)
610                 quirk_io_region(dev, ICH_PMBASE, 128, PCI_BRIDGE_RESOURCES,
611                                  "ICH6 ACPI/GPIO/TCO");
612
613         pci_read_config_byte(dev, ICH6_GPIO_CNTL, &enable);
614         if (enable & ICH6_GPIO_EN)
615                 quirk_io_region(dev, ICH6_GPIOBASE, 64, PCI_BRIDGE_RESOURCES+1,
616                                 "ICH6 GPIO");
617 }
618
619 static void ich6_lpc_generic_decode(struct pci_dev *dev, unsigned reg, const char *name, int dynsize)
620 {
621         u32 val;
622         u32 size, base;
623
624         pci_read_config_dword(dev, reg, &val);
625
626         /* Enabled? */
627         if (!(val & 1))
628                 return;
629         base = val & 0xfffc;
630         if (dynsize) {
631                 /*
632                  * This is not correct. It is 16, 32 or 64 bytes depending on
633                  * register D31:F0:ADh bits 5:4.
634                  *
635                  * But this gets us at least _part_ of it.
636                  */
637                 size = 16;
638         } else {
639                 size = 128;
640         }
641         base &= ~(size-1);
642
643         /* Just print it out for now. We should reserve it after more debugging */
644         pci_info(dev, "%s PIO at %04x-%04x\n", name, base, base+size-1);
645 }
646
647 static void quirk_ich6_lpc(struct pci_dev *dev)
648 {
649         /* Shared ACPI/GPIO decode with all ICH6+ */
650         ich6_lpc_acpi_gpio(dev);
651
652         /* ICH6-specific generic IO decode */
653         ich6_lpc_generic_decode(dev, 0x84, "LPC Generic IO decode 1", 0);
654         ich6_lpc_generic_decode(dev, 0x88, "LPC Generic IO decode 2", 1);
655 }
656 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH6_0, quirk_ich6_lpc);
657 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH6_1, quirk_ich6_lpc);
658
659 static void ich7_lpc_generic_decode(struct pci_dev *dev, unsigned reg, const char *name)
660 {
661         u32 val;
662         u32 mask, base;
663
664         pci_read_config_dword(dev, reg, &val);
665
666         /* Enabled? */
667         if (!(val & 1))
668                 return;
669
670         /*
671          * IO base in bits 15:2, mask in bits 23:18, both
672          * are dword-based
673          */
674         base = val & 0xfffc;
675         mask = (val >> 16) & 0xfc;
676         mask |= 3;
677
678         /* Just print it out for now. We should reserve it after more debugging */
679         pci_info(dev, "%s PIO at %04x (mask %04x)\n", name, base, mask);
680 }
681
682 /* ICH7-10 has the same common LPC generic IO decode registers */
683 static void quirk_ich7_lpc(struct pci_dev *dev)
684 {
685         /* We share the common ACPI/GPIO decode with ICH6 */
686         ich6_lpc_acpi_gpio(dev);
687
688         /* And have 4 ICH7+ generic decodes */
689         ich7_lpc_generic_decode(dev, 0x84, "ICH7 LPC Generic IO decode 1");
690         ich7_lpc_generic_decode(dev, 0x88, "ICH7 LPC Generic IO decode 2");
691         ich7_lpc_generic_decode(dev, 0x8c, "ICH7 LPC Generic IO decode 3");
692         ich7_lpc_generic_decode(dev, 0x90, "ICH7 LPC Generic IO decode 4");
693 }
694 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH7_0, quirk_ich7_lpc);
695 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH7_1, quirk_ich7_lpc);
696 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH7_31, quirk_ich7_lpc);
697 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH8_0, quirk_ich7_lpc);
698 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH8_2, quirk_ich7_lpc);
699 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH8_3, quirk_ich7_lpc);
700 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH8_1, quirk_ich7_lpc);
701 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH8_4, quirk_ich7_lpc);
702 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH9_2, quirk_ich7_lpc);
703 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH9_4, quirk_ich7_lpc);
704 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH9_7, quirk_ich7_lpc);
705 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH9_8, quirk_ich7_lpc);
706 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH10_1, quirk_ich7_lpc);
707
708 /*
709  * VIA ACPI: One IO region pointed to by longword at
710  *      0x48 or 0x20 (256 bytes of ACPI registers)
711  */
712 static void quirk_vt82c586_acpi(struct pci_dev *dev)
713 {
714         if (dev->revision & 0x10)
715                 quirk_io_region(dev, 0x48, 256, PCI_BRIDGE_RESOURCES,
716                                 "vt82c586 ACPI");
717 }
718 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_82C586_3,     quirk_vt82c586_acpi);
719
720 /*
721  * VIA VT82C686 ACPI: Three IO region pointed to by (long)words at
722  *      0x48 (256 bytes of ACPI registers)
723  *      0x70 (128 bytes of hardware monitoring register)
724  *      0x90 (16 bytes of SMB registers)
725  */
726 static void quirk_vt82c686_acpi(struct pci_dev *dev)
727 {
728         quirk_vt82c586_acpi(dev);
729
730         quirk_io_region(dev, 0x70, 128, PCI_BRIDGE_RESOURCES+1,
731                                  "vt82c686 HW-mon");
732
733         quirk_io_region(dev, 0x90, 16, PCI_BRIDGE_RESOURCES+2, "vt82c686 SMB");
734 }
735 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_82C686_4,     quirk_vt82c686_acpi);
736
737 /*
738  * VIA VT8235 ISA Bridge: Two IO regions pointed to by words at
739  *      0x88 (128 bytes of power management registers)
740  *      0xd0 (16 bytes of SMB registers)
741  */
742 static void quirk_vt8235_acpi(struct pci_dev *dev)
743 {
744         quirk_io_region(dev, 0x88, 128, PCI_BRIDGE_RESOURCES, "vt8235 PM");
745         quirk_io_region(dev, 0xd0, 16, PCI_BRIDGE_RESOURCES+1, "vt8235 SMB");
746 }
747 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8235, quirk_vt8235_acpi);
748
749 /*
750  * TI XIO2000a PCIe-PCI Bridge erroneously reports it supports fast back-to-back:
751  *      Disable fast back-to-back on the secondary bus segment
752  */
753 static void quirk_xio2000a(struct pci_dev *dev)
754 {
755         struct pci_dev *pdev;
756         u16 command;
757
758         pci_warn(dev, "TI XIO2000a quirk detected; secondary bus fast back-to-back transfers disabled\n");
759         list_for_each_entry(pdev, &dev->subordinate->devices, bus_list) {
760                 pci_read_config_word(pdev, PCI_COMMAND, &command);
761                 if (command & PCI_COMMAND_FAST_BACK)
762                         pci_write_config_word(pdev, PCI_COMMAND, command & ~PCI_COMMAND_FAST_BACK);
763         }
764 }
765 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XIO2000A,
766                         quirk_xio2000a);
767
768 #ifdef CONFIG_X86_IO_APIC
769
770 #include <asm/io_apic.h>
771
772 /*
773  * VIA 686A/B: If an IO-APIC is active, we need to route all on-chip
774  * devices to the external APIC.
775  *
776  * TODO: When we have device-specific interrupt routers,
777  * this code will go away from quirks.
778  */
779 static void quirk_via_ioapic(struct pci_dev *dev)
780 {
781         u8 tmp;
782
783         if (nr_ioapics < 1)
784                 tmp = 0;    /* nothing routed to external APIC */
785         else
786                 tmp = 0x1f; /* all known bits (4-0) routed to external APIC */
787
788         pci_info(dev, "%sbling VIA external APIC routing\n",
789                tmp == 0 ? "Disa" : "Ena");
790
791         /* Offset 0x58: External APIC IRQ output control */
792         pci_write_config_byte(dev, 0x58, tmp);
793 }
794 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_82C686,       quirk_via_ioapic);
795 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_VIA,       PCI_DEVICE_ID_VIA_82C686,       quirk_via_ioapic);
796
797 /*
798  * VIA 8237: Some BIOSes don't set the 'Bypass APIC De-Assert Message' Bit.
799  * This leads to doubled level interrupt rates.
800  * Set this bit to get rid of cycle wastage.
801  * Otherwise uncritical.
802  */
803 static void quirk_via_vt8237_bypass_apic_deassert(struct pci_dev *dev)
804 {
805         u8 misc_control2;
806 #define BYPASS_APIC_DEASSERT 8
807
808         pci_read_config_byte(dev, 0x5B, &misc_control2);
809         if (!(misc_control2 & BYPASS_APIC_DEASSERT)) {
810                 pci_info(dev, "Bypassing VIA 8237 APIC De-Assert Message\n");
811                 pci_write_config_byte(dev, 0x5B, misc_control2|BYPASS_APIC_DEASSERT);
812         }
813 }
814 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA,      PCI_DEVICE_ID_VIA_8237,         quirk_via_vt8237_bypass_apic_deassert);
815 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_VIA,       PCI_DEVICE_ID_VIA_8237,         quirk_via_vt8237_bypass_apic_deassert);
816
817 /*
818  * The AMD io apic can hang the box when an apic irq is masked.
819  * We check all revs >= B0 (yet not in the pre production!) as the bug
820  * is currently marked NoFix
821  *
822  * We have multiple reports of hangs with this chipset that went away with
823  * noapic specified. For the moment we assume it's the erratum. We may be wrong
824  * of course. However the advice is demonstrably good even if so..
825  */
826 static void quirk_amd_ioapic(struct pci_dev *dev)
827 {
828         if (dev->revision >= 0x02) {
829                 pci_warn(dev, "I/O APIC: AMD Erratum #22 may be present. In the event of instability try\n");
830                 pci_warn(dev, "        : booting with the \"noapic\" option\n");
831         }
832 }
833 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD,      PCI_DEVICE_ID_AMD_VIPER_7410,   quirk_amd_ioapic);
834 #endif /* CONFIG_X86_IO_APIC */
835
836 #if defined(CONFIG_ARM64) && defined(CONFIG_PCI_ATS)
837
838 static void quirk_cavium_sriov_rnm_link(struct pci_dev *dev)
839 {
840         /* Fix for improper SRIOV configuration on Cavium cn88xx  RNM device */
841         if (dev->subsystem_device == 0xa118)
842                 dev->sriov->link = dev->devfn;
843 }
844 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CAVIUM, 0xa018, quirk_cavium_sriov_rnm_link);
845 #endif
846
847 /*
848  * Some settings of MMRBC can lead to data corruption so block changes.
849  * See AMD 8131 HyperTransport PCI-X Tunnel Revision Guide
850  */
851 static void quirk_amd_8131_mmrbc(struct pci_dev *dev)
852 {
853         if (dev->subordinate && dev->revision <= 0x12) {
854                 pci_info(dev, "AMD8131 rev %x detected; disabling PCI-X MMRBC\n",
855                          dev->revision);
856                 dev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MMRBC;
857         }
858 }
859 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE, quirk_amd_8131_mmrbc);
860
861 /*
862  * FIXME: it is questionable that quirk_via_acpi
863  * is needed.  It shows up as an ISA bridge, and does not
864  * support the PCI_INTERRUPT_LINE register at all.  Therefore
865  * it seems like setting the pci_dev's 'irq' to the
866  * value of the ACPI SCI interrupt is only done for convenience.
867  *      -jgarzik
868  */
869 static void quirk_via_acpi(struct pci_dev *d)
870 {
871         /*
872          * VIA ACPI device: SCI IRQ line in PCI config byte 0x42
873          */
874         u8 irq;
875         pci_read_config_byte(d, 0x42, &irq);
876         irq &= 0xf;
877         if (irq && (irq != 2))
878                 d->irq = irq;
879 }
880 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_82C586_3,     quirk_via_acpi);
881 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_82C686_4,     quirk_via_acpi);
882
883
884 /*
885  *      VIA bridges which have VLink
886  */
887
888 static int via_vlink_dev_lo = -1, via_vlink_dev_hi = 18;
889
890 static void quirk_via_bridge(struct pci_dev *dev)
891 {
892         /* See what bridge we have and find the device ranges */
893         switch (dev->device) {
894         case PCI_DEVICE_ID_VIA_82C686:
895                 /* The VT82C686 is special, it attaches to PCI and can have
896                    any device number. All its subdevices are functions of
897                    that single device. */
898                 via_vlink_dev_lo = PCI_SLOT(dev->devfn);
899                 via_vlink_dev_hi = PCI_SLOT(dev->devfn);
900                 break;
901         case PCI_DEVICE_ID_VIA_8237:
902         case PCI_DEVICE_ID_VIA_8237A:
903                 via_vlink_dev_lo = 15;
904                 break;
905         case PCI_DEVICE_ID_VIA_8235:
906                 via_vlink_dev_lo = 16;
907                 break;
908         case PCI_DEVICE_ID_VIA_8231:
909         case PCI_DEVICE_ID_VIA_8233_0:
910         case PCI_DEVICE_ID_VIA_8233A:
911         case PCI_DEVICE_ID_VIA_8233C_0:
912                 via_vlink_dev_lo = 17;
913                 break;
914         }
915 }
916 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_82C686,       quirk_via_bridge);
917 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8231,         quirk_via_bridge);
918 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8233_0,       quirk_via_bridge);
919 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8233A,        quirk_via_bridge);
920 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8233C_0,      quirk_via_bridge);
921 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8235,         quirk_via_bridge);
922 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8237,         quirk_via_bridge);
923 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8237A,        quirk_via_bridge);
924
925 /**
926  *      quirk_via_vlink         -       VIA VLink IRQ number update
927  *      @dev: PCI device
928  *
929  *      If the device we are dealing with is on a PIC IRQ we need to
930  *      ensure that the IRQ line register which usually is not relevant
931  *      for PCI cards, is actually written so that interrupts get sent
932  *      to the right place.
933  *      We only do this on systems where a VIA south bridge was detected,
934  *      and only for VIA devices on the motherboard (see quirk_via_bridge
935  *      above).
936  */
937
938 static void quirk_via_vlink(struct pci_dev *dev)
939 {
940         u8 irq, new_irq;
941
942         /* Check if we have VLink at all */
943         if (via_vlink_dev_lo == -1)
944                 return;
945
946         new_irq = dev->irq;
947
948         /* Don't quirk interrupts outside the legacy IRQ range */
949         if (!new_irq || new_irq > 15)
950                 return;
951
952         /* Internal device ? */
953         if (dev->bus->number != 0 || PCI_SLOT(dev->devfn) > via_vlink_dev_hi ||
954             PCI_SLOT(dev->devfn) < via_vlink_dev_lo)
955                 return;
956
957         /* This is an internal VLink device on a PIC interrupt. The BIOS
958            ought to have set this but may not have, so we redo it */
959
960         pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
961         if (new_irq != irq) {
962                 pci_info(dev, "VIA VLink IRQ fixup, from %d to %d\n",
963                         irq, new_irq);
964                 udelay(15);     /* unknown if delay really needed */
965                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, new_irq);
966         }
967 }
968 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_VIA, PCI_ANY_ID, quirk_via_vlink);
969
970 /*
971  * VIA VT82C598 has its device ID settable and many BIOSes
972  * set it to the ID of VT82C597 for backward compatibility.
973  * We need to switch it off to be able to recognize the real
974  * type of the chip.
975  */
976 static void quirk_vt82c598_id(struct pci_dev *dev)
977 {
978         pci_write_config_byte(dev, 0xfc, 0);
979         pci_read_config_word(dev, PCI_DEVICE_ID, &dev->device);
980 }
981 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_82C597_0,     quirk_vt82c598_id);
982
983 /*
984  * CardBus controllers have a legacy base address that enables them
985  * to respond as i82365 pcmcia controllers.  We don't want them to
986  * do this even if the Linux CardBus driver is not loaded, because
987  * the Linux i82365 driver does not (and should not) handle CardBus.
988  */
989 static void quirk_cardbus_legacy(struct pci_dev *dev)
990 {
991         pci_write_config_dword(dev, PCI_CB_LEGACY_MODE_BASE, 0);
992 }
993 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_ANY_ID, PCI_ANY_ID,
994                         PCI_CLASS_BRIDGE_CARDBUS, 8, quirk_cardbus_legacy);
995 DECLARE_PCI_FIXUP_CLASS_RESUME_EARLY(PCI_ANY_ID, PCI_ANY_ID,
996                         PCI_CLASS_BRIDGE_CARDBUS, 8, quirk_cardbus_legacy);
997
998 /*
999  * Following the PCI ordering rules is optional on the AMD762. I'm not
1000  * sure what the designers were smoking but let's not inhale...
1001  *
1002  * To be fair to AMD, it follows the spec by default, its BIOS people
1003  * who turn it off!
1004  */
1005 static void quirk_amd_ordering(struct pci_dev *dev)
1006 {
1007         u32 pcic;
1008         pci_read_config_dword(dev, 0x4C, &pcic);
1009         if ((pcic & 6) != 6) {
1010                 pcic |= 6;
1011                 pci_warn(dev, "BIOS failed to enable PCI standards compliance; fixing this error\n");
1012                 pci_write_config_dword(dev, 0x4C, pcic);
1013                 pci_read_config_dword(dev, 0x84, &pcic);
1014                 pcic |= (1 << 23);      /* Required in this mode */
1015                 pci_write_config_dword(dev, 0x84, pcic);
1016         }
1017 }
1018 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD,      PCI_DEVICE_ID_AMD_FE_GATE_700C, quirk_amd_ordering);
1019 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_AMD,       PCI_DEVICE_ID_AMD_FE_GATE_700C, quirk_amd_ordering);
1020
1021 /*
1022  *      DreamWorks provided workaround for Dunord I-3000 problem
1023  *
1024  *      This card decodes and responds to addresses not apparently
1025  *      assigned to it. We force a larger allocation to ensure that
1026  *      nothing gets put too close to it.
1027  */
1028 static void quirk_dunord(struct pci_dev *dev)
1029 {
1030         struct resource *r = &dev->resource[1];
1031
1032         r->flags |= IORESOURCE_UNSET;
1033         r->start = 0;
1034         r->end = 0xffffff;
1035 }
1036 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DUNORD,  PCI_DEVICE_ID_DUNORD_I3000,     quirk_dunord);
1037
1038 /*
1039  * i82380FB mobile docking controller: its PCI-to-PCI bridge
1040  * is subtractive decoding (transparent), and does indicate this
1041  * in the ProgIf. Unfortunately, the ProgIf value is wrong - 0x80
1042  * instead of 0x01.
1043  */
1044 static void quirk_transparent_bridge(struct pci_dev *dev)
1045 {
1046         dev->transparent = 1;
1047 }
1048 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82380FB,    quirk_transparent_bridge);
1049 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TOSHIBA, 0x605,  quirk_transparent_bridge);
1050
1051 /*
1052  * Common misconfiguration of the MediaGX/Geode PCI master that will
1053  * reduce PCI bandwidth from 70MB/s to 25MB/s.  See the GXM/GXLV/GX1
1054  * datasheets found at http://www.national.com/analog for info on what
1055  * these bits do.  <christer@weinigel.se>
1056  */
1057 static void quirk_mediagx_master(struct pci_dev *dev)
1058 {
1059         u8 reg;
1060
1061         pci_read_config_byte(dev, 0x41, &reg);
1062         if (reg & 2) {
1063                 reg &= ~2;
1064                 pci_info(dev, "Fixup for MediaGX/Geode Slave Disconnect Boundary (0x41=0x%02x)\n",
1065                          reg);
1066                 pci_write_config_byte(dev, 0x41, reg);
1067         }
1068 }
1069 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CYRIX,    PCI_DEVICE_ID_CYRIX_PCI_MASTER, quirk_mediagx_master);
1070 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_CYRIX,   PCI_DEVICE_ID_CYRIX_PCI_MASTER, quirk_mediagx_master);
1071
1072 /*
1073  *      Ensure C0 rev restreaming is off. This is normally done by
1074  *      the BIOS but in the odd case it is not the results are corruption
1075  *      hence the presence of a Linux check
1076  */
1077 static void quirk_disable_pxb(struct pci_dev *pdev)
1078 {
1079         u16 config;
1080
1081         if (pdev->revision != 0x04)             /* Only C0 requires this */
1082                 return;
1083         pci_read_config_word(pdev, 0x40, &config);
1084         if (config & (1<<6)) {
1085                 config &= ~(1<<6);
1086                 pci_write_config_word(pdev, 0x40, config);
1087                 pci_info(pdev, "C0 revision 450NX. Disabling PCI restreaming\n");
1088         }
1089 }
1090 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82454NX,    quirk_disable_pxb);
1091 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82454NX,    quirk_disable_pxb);
1092
1093 static void quirk_amd_ide_mode(struct pci_dev *pdev)
1094 {
1095         /* set SBX00/Hudson-2 SATA in IDE mode to AHCI mode */
1096         u8 tmp;
1097
1098         pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &tmp);
1099         if (tmp == 0x01) {
1100                 pci_read_config_byte(pdev, 0x40, &tmp);
1101                 pci_write_config_byte(pdev, 0x40, tmp|1);
1102                 pci_write_config_byte(pdev, 0x9, 1);
1103                 pci_write_config_byte(pdev, 0xa, 6);
1104                 pci_write_config_byte(pdev, 0x40, tmp);
1105
1106                 pdev->class = PCI_CLASS_STORAGE_SATA_AHCI;
1107                 pci_info(pdev, "set SATA to AHCI mode\n");
1108         }
1109 }
1110 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SATA, quirk_amd_ide_mode);
1111 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SATA, quirk_amd_ide_mode);
1112 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SATA, quirk_amd_ide_mode);
1113 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SATA, quirk_amd_ide_mode);
1114 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SATA_IDE, quirk_amd_ide_mode);
1115 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SATA_IDE, quirk_amd_ide_mode);
1116 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, 0x7900, quirk_amd_ide_mode);
1117 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_AMD, 0x7900, quirk_amd_ide_mode);
1118
1119 /*
1120  *      Serverworks CSB5 IDE does not fully support native mode
1121  */
1122 static void quirk_svwks_csb5ide(struct pci_dev *pdev)
1123 {
1124         u8 prog;
1125         pci_read_config_byte(pdev, PCI_CLASS_PROG, &prog);
1126         if (prog & 5) {
1127                 prog &= ~5;
1128                 pdev->class &= ~5;
1129                 pci_write_config_byte(pdev, PCI_CLASS_PROG, prog);
1130                 /* PCI layer will sort out resources */
1131         }
1132 }
1133 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5IDE, quirk_svwks_csb5ide);
1134
1135 /*
1136  *      Intel 82801CAM ICH3-M datasheet says IDE modes must be the same
1137  */
1138 static void quirk_ide_samemode(struct pci_dev *pdev)
1139 {
1140         u8 prog;
1141
1142         pci_read_config_byte(pdev, PCI_CLASS_PROG, &prog);
1143
1144         if (((prog & 1) && !(prog & 4)) || ((prog & 4) && !(prog & 1))) {
1145                 pci_info(pdev, "IDE mode mismatch; forcing legacy mode\n");
1146                 prog &= ~5;
1147                 pdev->class &= ~5;
1148                 pci_write_config_byte(pdev, PCI_CLASS_PROG, prog);
1149         }
1150 }
1151 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_10, quirk_ide_samemode);
1152
1153 /*
1154  * Some ATA devices break if put into D3
1155  */
1156
1157 static void quirk_no_ata_d3(struct pci_dev *pdev)
1158 {
1159         pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
1160 }
1161 /* Quirk the legacy ATA devices only. The AHCI ones are ok */
1162 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_ANY_ID,
1163                                 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1164 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_ATI, PCI_ANY_ID,
1165                                 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1166 /* ALi loses some register settings that we cannot then restore */
1167 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_AL, PCI_ANY_ID,
1168                                 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1169 /* VIA comes back fine but we need to keep it alive or ACPI GTM failures
1170    occur when mode detecting */
1171 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
1172                                 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1173
1174 /* This was originally an Alpha specific thing, but it really fits here.
1175  * The i82375 PCI/EISA bridge appears as non-classified. Fix that.
1176  */
1177 static void quirk_eisa_bridge(struct pci_dev *dev)
1178 {
1179         dev->class = PCI_CLASS_BRIDGE_EISA << 8;
1180 }
1181 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82375,      quirk_eisa_bridge);
1182
1183
1184 /*
1185  * On ASUS P4B boards, the SMBus PCI Device within the ICH2/4 southbridge
1186  * is not activated. The myth is that Asus said that they do not want the
1187  * users to be irritated by just another PCI Device in the Win98 device
1188  * manager. (see the file prog/hotplug/README.p4b in the lm_sensors
1189  * package 2.7.0 for details)
1190  *
1191  * The SMBus PCI Device can be activated by setting a bit in the ICH LPC
1192  * bridge. Unfortunately, this device has no subvendor/subdevice ID. So it
1193  * becomes necessary to do this tweak in two steps -- the chosen trigger
1194  * is either the Host bridge (preferred) or on-board VGA controller.
1195  *
1196  * Note that we used to unhide the SMBus that way on Toshiba laptops
1197  * (Satellite A40 and Tecra M2) but then found that the thermal management
1198  * was done by SMM code, which could cause unsynchronized concurrent
1199  * accesses to the SMBus registers, with potentially bad effects. Thus you
1200  * should be very careful when adding new entries: if SMM is accessing the
1201  * Intel SMBus, this is a very good reason to leave it hidden.
1202  *
1203  * Likewise, many recent laptops use ACPI for thermal management. If the
1204  * ACPI DSDT code accesses the SMBus, then Linux should not access it
1205  * natively, and keeping the SMBus hidden is the right thing to do. If you
1206  * are about to add an entry in the table below, please first disassemble
1207  * the DSDT and double-check that there is no code accessing the SMBus.
1208  */
1209 static int asus_hides_smbus;
1210
1211 static void asus_hides_smbus_hostbridge(struct pci_dev *dev)
1212 {
1213         if (unlikely(dev->subsystem_vendor == PCI_VENDOR_ID_ASUSTEK)) {
1214                 if (dev->device == PCI_DEVICE_ID_INTEL_82845_HB)
1215                         switch (dev->subsystem_device) {
1216                         case 0x8025: /* P4B-LX */
1217                         case 0x8070: /* P4B */
1218                         case 0x8088: /* P4B533 */
1219                         case 0x1626: /* L3C notebook */
1220                                 asus_hides_smbus = 1;
1221                         }
1222                 else if (dev->device == PCI_DEVICE_ID_INTEL_82845G_HB)
1223                         switch (dev->subsystem_device) {
1224                         case 0x80b1: /* P4GE-V */
1225                         case 0x80b2: /* P4PE */
1226                         case 0x8093: /* P4B533-V */
1227                                 asus_hides_smbus = 1;
1228                         }
1229                 else if (dev->device == PCI_DEVICE_ID_INTEL_82850_HB)
1230                         switch (dev->subsystem_device) {
1231                         case 0x8030: /* P4T533 */
1232                                 asus_hides_smbus = 1;
1233                         }
1234                 else if (dev->device == PCI_DEVICE_ID_INTEL_7205_0)
1235                         switch (dev->subsystem_device) {
1236                         case 0x8070: /* P4G8X Deluxe */
1237                                 asus_hides_smbus = 1;
1238                         }
1239                 else if (dev->device == PCI_DEVICE_ID_INTEL_E7501_MCH)
1240                         switch (dev->subsystem_device) {
1241                         case 0x80c9: /* PU-DLS */
1242                                 asus_hides_smbus = 1;
1243                         }
1244                 else if (dev->device == PCI_DEVICE_ID_INTEL_82855GM_HB)
1245                         switch (dev->subsystem_device) {
1246                         case 0x1751: /* M2N notebook */
1247                         case 0x1821: /* M5N notebook */
1248                         case 0x1897: /* A6L notebook */
1249                                 asus_hides_smbus = 1;
1250                         }
1251                 else if (dev->device == PCI_DEVICE_ID_INTEL_82855PM_HB)
1252                         switch (dev->subsystem_device) {
1253                         case 0x184b: /* W1N notebook */
1254                         case 0x186a: /* M6Ne notebook */
1255                                 asus_hides_smbus = 1;
1256                         }
1257                 else if (dev->device == PCI_DEVICE_ID_INTEL_82865_HB)
1258                         switch (dev->subsystem_device) {
1259                         case 0x80f2: /* P4P800-X */
1260                                 asus_hides_smbus = 1;
1261                         }
1262                 else if (dev->device == PCI_DEVICE_ID_INTEL_82915GM_HB)
1263                         switch (dev->subsystem_device) {
1264                         case 0x1882: /* M6V notebook */
1265                         case 0x1977: /* A6VA notebook */
1266                                 asus_hides_smbus = 1;
1267                         }
1268         } else if (unlikely(dev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
1269                 if (dev->device ==  PCI_DEVICE_ID_INTEL_82855PM_HB)
1270                         switch (dev->subsystem_device) {
1271                         case 0x088C: /* HP Compaq nc8000 */
1272                         case 0x0890: /* HP Compaq nc6000 */
1273                                 asus_hides_smbus = 1;
1274                         }
1275                 else if (dev->device == PCI_DEVICE_ID_INTEL_82865_HB)
1276                         switch (dev->subsystem_device) {
1277                         case 0x12bc: /* HP D330L */
1278                         case 0x12bd: /* HP D530 */
1279                         case 0x006a: /* HP Compaq nx9500 */
1280                                 asus_hides_smbus = 1;
1281                         }
1282                 else if (dev->device == PCI_DEVICE_ID_INTEL_82875_HB)
1283                         switch (dev->subsystem_device) {
1284                         case 0x12bf: /* HP xw4100 */
1285                                 asus_hides_smbus = 1;
1286                         }
1287         } else if (unlikely(dev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG)) {
1288                 if (dev->device ==  PCI_DEVICE_ID_INTEL_82855PM_HB)
1289                         switch (dev->subsystem_device) {
1290                         case 0xC00C: /* Samsung P35 notebook */
1291                                 asus_hides_smbus = 1;
1292                 }
1293         } else if (unlikely(dev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ)) {
1294                 if (dev->device == PCI_DEVICE_ID_INTEL_82855PM_HB)
1295                         switch (dev->subsystem_device) {
1296                         case 0x0058: /* Compaq Evo N620c */
1297                                 asus_hides_smbus = 1;
1298                         }
1299                 else if (dev->device == PCI_DEVICE_ID_INTEL_82810_IG3)
1300                         switch (dev->subsystem_device) {
1301                         case 0xB16C: /* Compaq Deskpro EP 401963-001 (PCA# 010174) */
1302                                 /* Motherboard doesn't have Host bridge
1303                                  * subvendor/subdevice IDs, therefore checking
1304                                  * its on-board VGA controller */
1305                                 asus_hides_smbus = 1;
1306                         }
1307                 else if (dev->device == PCI_DEVICE_ID_INTEL_82801DB_2)
1308                         switch (dev->subsystem_device) {
1309                         case 0x00b8: /* Compaq Evo D510 CMT */
1310                         case 0x00b9: /* Compaq Evo D510 SFF */
1311                         case 0x00ba: /* Compaq Evo D510 USDT */
1312                                 /* Motherboard doesn't have Host bridge
1313                                  * subvendor/subdevice IDs and on-board VGA
1314                                  * controller is disabled if an AGP card is
1315                                  * inserted, therefore checking USB UHCI
1316                                  * Controller #1 */
1317                                 asus_hides_smbus = 1;
1318                         }
1319                 else if (dev->device == PCI_DEVICE_ID_INTEL_82815_CGC)
1320                         switch (dev->subsystem_device) {
1321                         case 0x001A: /* Compaq Deskpro EN SSF P667 815E */
1322                                 /* Motherboard doesn't have host bridge
1323                                  * subvendor/subdevice IDs, therefore checking
1324                                  * its on-board VGA controller */
1325                                 asus_hides_smbus = 1;
1326                         }
1327         }
1328 }
1329 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82845_HB,   asus_hides_smbus_hostbridge);
1330 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82845G_HB,  asus_hides_smbus_hostbridge);
1331 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82850_HB,   asus_hides_smbus_hostbridge);
1332 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82865_HB,   asus_hides_smbus_hostbridge);
1333 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82875_HB,   asus_hides_smbus_hostbridge);
1334 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_7205_0,     asus_hides_smbus_hostbridge);
1335 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_E7501_MCH,  asus_hides_smbus_hostbridge);
1336 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82855PM_HB, asus_hides_smbus_hostbridge);
1337 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82855GM_HB, asus_hides_smbus_hostbridge);
1338 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82915GM_HB, asus_hides_smbus_hostbridge);
1339
1340 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82810_IG3,  asus_hides_smbus_hostbridge);
1341 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801DB_2,  asus_hides_smbus_hostbridge);
1342 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82815_CGC,  asus_hides_smbus_hostbridge);
1343
1344 static void asus_hides_smbus_lpc(struct pci_dev *dev)
1345 {
1346         u16 val;
1347
1348         if (likely(!asus_hides_smbus))
1349                 return;
1350
1351         pci_read_config_word(dev, 0xF2, &val);
1352         if (val & 0x8) {
1353                 pci_write_config_word(dev, 0xF2, val & (~0x8));
1354                 pci_read_config_word(dev, 0xF2, &val);
1355                 if (val & 0x8)
1356                         pci_info(dev, "i801 SMBus device continues to play 'hide and seek'! 0x%x\n",
1357                                  val);
1358                 else
1359                         pci_info(dev, "Enabled i801 SMBus device\n");
1360         }
1361 }
1362 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801AA_0,  asus_hides_smbus_lpc);
1363 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801DB_0,  asus_hides_smbus_lpc);
1364 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801BA_0,  asus_hides_smbus_lpc);
1365 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801CA_0,  asus_hides_smbus_lpc);
1366 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801CA_12, asus_hides_smbus_lpc);
1367 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801DB_12, asus_hides_smbus_lpc);
1368 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_82801EB_0,  asus_hides_smbus_lpc);
1369 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801AA_0,  asus_hides_smbus_lpc);
1370 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801DB_0,  asus_hides_smbus_lpc);
1371 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801BA_0,  asus_hides_smbus_lpc);
1372 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801CA_0,  asus_hides_smbus_lpc);
1373 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801CA_12, asus_hides_smbus_lpc);
1374 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801DB_12, asus_hides_smbus_lpc);
1375 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_82801EB_0,  asus_hides_smbus_lpc);
1376
1377 /* It appears we just have one such device. If not, we have a warning */
1378 static void __iomem *asus_rcba_base;
1379 static void asus_hides_smbus_lpc_ich6_suspend(struct pci_dev *dev)
1380 {
1381         u32 rcba;
1382
1383         if (likely(!asus_hides_smbus))
1384                 return;
1385         WARN_ON(asus_rcba_base);
1386
1387         pci_read_config_dword(dev, 0xF0, &rcba);
1388         /* use bits 31:14, 16 kB aligned */
1389         asus_rcba_base = ioremap_nocache(rcba & 0xFFFFC000, 0x4000);
1390         if (asus_rcba_base == NULL)
1391                 return;
1392 }
1393
1394 static void asus_hides_smbus_lpc_ich6_resume_early(struct pci_dev *dev)
1395 {
1396         u32 val;
1397
1398         if (likely(!asus_hides_smbus || !asus_rcba_base))
1399                 return;
1400         /* read the Function Disable register, dword mode only */
1401         val = readl(asus_rcba_base + 0x3418);
1402         writel(val & 0xFFFFFFF7, asus_rcba_base + 0x3418); /* enable the SMBus device */
1403 }
1404
1405 static void asus_hides_smbus_lpc_ich6_resume(struct pci_dev *dev)
1406 {
1407         if (likely(!asus_hides_smbus || !asus_rcba_base))
1408                 return;
1409         iounmap(asus_rcba_base);
1410         asus_rcba_base = NULL;
1411         pci_info(dev, "Enabled ICH6/i801 SMBus device\n");
1412 }
1413
1414 static void asus_hides_smbus_lpc_ich6(struct pci_dev *dev)
1415 {
1416         asus_hides_smbus_lpc_ich6_suspend(dev);
1417         asus_hides_smbus_lpc_ich6_resume_early(dev);
1418         asus_hides_smbus_lpc_ich6_resume(dev);
1419 }
1420 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH6_1,     asus_hides_smbus_lpc_ich6);
1421 DECLARE_PCI_FIXUP_SUSPEND(PCI_VENDOR_ID_INTEL,  PCI_DEVICE_ID_INTEL_ICH6_1,     asus_hides_smbus_lpc_ich6_suspend);
1422 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ICH6_1,     asus_hides_smbus_lpc_ich6_resume);
1423 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,     PCI_DEVICE_ID_INTEL_ICH6_1,     asus_hides_smbus_lpc_ich6_resume_early);
1424
1425 /*
1426  * SiS 96x south bridge: BIOS typically hides SMBus device...
1427  */
1428 static void quirk_sis_96x_smbus(struct pci_dev *dev)
1429 {
1430         u8 val = 0;
1431         pci_read_config_byte(dev, 0x77, &val);
1432         if (val & 0x10) {
1433                 pci_info(dev, "Enabling SiS 96x SMBus\n");
1434                 pci_write_config_byte(dev, 0x77, val & ~0x10);
1435         }
1436 }
1437 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI,      PCI_DEVICE_ID_SI_961,           quirk_sis_96x_smbus);
1438 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI,      PCI_DEVICE_ID_SI_962,           quirk_sis_96x_smbus);
1439 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI,      PCI_DEVICE_ID_SI_963,           quirk_sis_96x_smbus);
1440 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI,      PCI_DEVICE_ID_SI_LPC,           quirk_sis_96x_smbus);
1441 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI,        PCI_DEVICE_ID_SI_961,           quirk_sis_96x_smbus);
1442 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI,        PCI_DEVICE_ID_SI_962,           quirk_sis_96x_smbus);
1443 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI,        PCI_DEVICE_ID_SI_963,           quirk_sis_96x_smbus);
1444 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI,        PCI_DEVICE_ID_SI_LPC,           quirk_sis_96x_smbus);
1445
1446 /*
1447  * ... This is further complicated by the fact that some SiS96x south
1448  * bridges pretend to be 85C503/5513 instead.  In that case see if we
1449  * spotted a compatible north bridge to make sure.
1450  * (pci_find_device doesn't work yet)
1451  *
1452  * We can also enable the sis96x bit in the discovery register..
1453  */
1454 #define SIS_DETECT_REGISTER 0x40
1455
1456 static void quirk_sis_503(struct pci_dev *dev)
1457 {
1458         u8 reg;
1459         u16 devid;
1460
1461         pci_read_config_byte(dev, SIS_DETECT_REGISTER, &reg);
1462         pci_write_config_byte(dev, SIS_DETECT_REGISTER, reg | (1 << 6));
1463         pci_read_config_word(dev, PCI_DEVICE_ID, &devid);
1464         if (((devid & 0xfff0) != 0x0960) && (devid != 0x0018)) {
1465                 pci_write_config_byte(dev, SIS_DETECT_REGISTER, reg);
1466                 return;
1467         }
1468
1469         /*
1470          * Ok, it now shows up as a 96x.. run the 96x quirk by
1471          * hand in case it has already been processed.
1472          * (depends on link order, which is apparently not guaranteed)
1473          */
1474         dev->device = devid;
1475         quirk_sis_96x_smbus(dev);
1476 }
1477 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI,      PCI_DEVICE_ID_SI_503,           quirk_sis_503);
1478 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI,        PCI_DEVICE_ID_SI_503,           quirk_sis_503);
1479
1480
1481 /*
1482  * On ASUS A8V and A8V Deluxe boards, the onboard AC97 audio controller
1483  * and MC97 modem controller are disabled when a second PCI soundcard is
1484  * present. This patch, tweaking the VT8237 ISA bridge, enables them.
1485  * -- bjd
1486  */
1487 static void asus_hides_ac97_lpc(struct pci_dev *dev)
1488 {
1489         u8 val;
1490         int asus_hides_ac97 = 0;
1491
1492         if (likely(dev->subsystem_vendor == PCI_VENDOR_ID_ASUSTEK)) {
1493                 if (dev->device == PCI_DEVICE_ID_VIA_8237)
1494                         asus_hides_ac97 = 1;
1495         }
1496
1497         if (!asus_hides_ac97)
1498                 return;
1499
1500         pci_read_config_byte(dev, 0x50, &val);
1501         if (val & 0xc0) {
1502                 pci_write_config_byte(dev, 0x50, val & (~0xc0));
1503                 pci_read_config_byte(dev, 0x50, &val);
1504                 if (val & 0xc0)
1505                         pci_info(dev, "Onboard AC97/MC97 devices continue to play 'hide and seek'! 0x%x\n",
1506                                  val);
1507                 else
1508                         pci_info(dev, "Enabled onboard AC97/MC97 devices\n");
1509         }
1510 }
1511 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA,     PCI_DEVICE_ID_VIA_8237, asus_hides_ac97_lpc);
1512 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_VIA,       PCI_DEVICE_ID_VIA_8237, asus_hides_ac97_lpc);
1513
1514 #if defined(CONFIG_ATA) || defined(CONFIG_ATA_MODULE)
1515
1516 /*
1517  *      If we are using libata we can drive this chip properly but must
1518  *      do this early on to make the additional device appear during
1519  *      the PCI scanning.
1520  */
1521 static void quirk_jmicron_ata(struct pci_dev *pdev)
1522 {
1523         u32 conf1, conf5, class;
1524         u8 hdr;
1525
1526         /* Only poke fn 0 */
1527         if (PCI_FUNC(pdev->devfn))
1528                 return;
1529
1530         pci_read_config_dword(pdev, 0x40, &conf1);
1531         pci_read_config_dword(pdev, 0x80, &conf5);
1532
1533         conf1 &= ~0x00CFF302; /* Clear bit 1, 8, 9, 12-19, 22, 23 */
1534         conf5 &= ~(1 << 24);  /* Clear bit 24 */
1535
1536         switch (pdev->device) {
1537         case PCI_DEVICE_ID_JMICRON_JMB360: /* SATA single port */
1538         case PCI_DEVICE_ID_JMICRON_JMB362: /* SATA dual ports */
1539         case PCI_DEVICE_ID_JMICRON_JMB364: /* SATA dual ports */
1540                 /* The controller should be in single function ahci mode */
1541                 conf1 |= 0x0002A100; /* Set 8, 13, 15, 17 */
1542                 break;
1543
1544         case PCI_DEVICE_ID_JMICRON_JMB365:
1545         case PCI_DEVICE_ID_JMICRON_JMB366:
1546                 /* Redirect IDE second PATA port to the right spot */
1547                 conf5 |= (1 << 24);
1548                 /* Fall through */
1549         case PCI_DEVICE_ID_JMICRON_JMB361:
1550         case PCI_DEVICE_ID_JMICRON_JMB363:
1551         case PCI_DEVICE_ID_JMICRON_JMB369:
1552                 /* Enable dual function mode, AHCI on fn 0, IDE fn1 */
1553                 /* Set the class codes correctly and then direct IDE 0 */
1554                 conf1 |= 0x00C2A1B3; /* Set 0, 1, 4, 5, 7, 8, 13, 15, 17, 22, 23 */
1555                 break;
1556
1557         case PCI_DEVICE_ID_JMICRON_JMB368:
1558                 /* The controller should be in single function IDE mode */
1559                 conf1 |= 0x00C00000; /* Set 22, 23 */
1560                 break;
1561         }
1562
1563         pci_write_config_dword(pdev, 0x40, conf1);
1564         pci_write_config_dword(pdev, 0x80, conf5);
1565
1566         /* Update pdev accordingly */
1567         pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr);
1568         pdev->hdr_type = hdr & 0x7f;
1569         pdev->multifunction = !!(hdr & 0x80);
1570
1571         pci_read_config_dword(pdev, PCI_CLASS_REVISION, &class);
1572         pdev->class = class >> 8;
1573 }
1574 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB360, quirk_jmicron_ata);
1575 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361, quirk_jmicron_ata);
1576 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB362, quirk_jmicron_ata);
1577 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363, quirk_jmicron_ata);
1578 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB364, quirk_jmicron_ata);
1579 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365, quirk_jmicron_ata);
1580 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366, quirk_jmicron_ata);
1581 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368, quirk_jmicron_ata);
1582 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB369, quirk_jmicron_ata);
1583 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB360, quirk_jmicron_ata);
1584 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361, quirk_jmicron_ata);
1585 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB362, quirk_jmicron_ata);
1586 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363, quirk_jmicron_ata);
1587 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB364, quirk_jmicron_ata);
1588 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365, quirk_jmicron_ata);
1589 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366, quirk_jmicron_ata);
1590 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368, quirk_jmicron_ata);
1591 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB369, quirk_jmicron_ata);
1592
1593 #endif
1594
1595 static void quirk_jmicron_async_suspend(struct pci_dev *dev)
1596 {
1597         if (dev->multifunction) {
1598                 device_disable_async_suspend(&dev->dev);
1599                 pci_info(dev, "async suspend disabled to avoid multi-function power-on ordering issue\n");
1600         }
1601 }
1602 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE, 8, quirk_jmicron_async_suspend);
1603 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_CLASS_STORAGE_SATA_AHCI, 0, quirk_jmicron_async_suspend);
1604 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_JMICRON, 0x2362, quirk_jmicron_async_suspend);
1605 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_JMICRON, 0x236f, quirk_jmicron_async_suspend);
1606
1607 #ifdef CONFIG_X86_IO_APIC
1608 static void quirk_alder_ioapic(struct pci_dev *pdev)
1609 {
1610         int i;
1611
1612         if ((pdev->class >> 8) != 0xff00)
1613                 return;
1614
1615         /* the first BAR is the location of the IO APIC...we must
1616          * not touch this (and it's already covered by the fixmap), so
1617          * forcibly insert it into the resource tree */
1618         if (pci_resource_start(pdev, 0) && pci_resource_len(pdev, 0))
1619                 insert_resource(&iomem_resource, &pdev->resource[0]);
1620
1621         /* The next five BARs all seem to be rubbish, so just clean
1622          * them out */
1623         for (i = 1; i < 6; i++)
1624                 memset(&pdev->resource[i], 0, sizeof(pdev->resource[i]));
1625 }
1626 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_EESSC,      quirk_alder_ioapic);
1627 #endif
1628
1629 static void quirk_pcie_mch(struct pci_dev *pdev)
1630 {
1631         pdev->no_msi = 1;
1632 }
1633 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_E7520_MCH,  quirk_pcie_mch);
1634 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_E7320_MCH,  quirk_pcie_mch);
1635 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_E7525_MCH,  quirk_pcie_mch);
1636
1637 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_HUAWEI, 0x1610, PCI_CLASS_BRIDGE_PCI, 8, quirk_pcie_mch);
1638
1639 /*
1640  * It's possible for the MSI to get corrupted if shpc and acpi
1641  * are used together on certain PXH-based systems.
1642  */
1643 static void quirk_pcie_pxh(struct pci_dev *dev)
1644 {
1645         dev->no_msi = 1;
1646         pci_warn(dev, "PXH quirk detected; SHPC device MSI disabled\n");
1647 }
1648 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXHD_0,     quirk_pcie_pxh);
1649 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXHD_1,     quirk_pcie_pxh);
1650 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXH_0,      quirk_pcie_pxh);
1651 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXH_1,      quirk_pcie_pxh);
1652 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXHV,       quirk_pcie_pxh);
1653
1654 /*
1655  * Some Intel PCI Express chipsets have trouble with downstream
1656  * device power management.
1657  */
1658 static void quirk_intel_pcie_pm(struct pci_dev *dev)
1659 {
1660         pci_pm_d3_delay = 120;
1661         dev->no_d1d2 = 1;
1662 }
1663
1664 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25e2, quirk_intel_pcie_pm);
1665 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25e3, quirk_intel_pcie_pm);
1666 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25e4, quirk_intel_pcie_pm);
1667 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25e5, quirk_intel_pcie_pm);
1668 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25e6, quirk_intel_pcie_pm);
1669 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25e7, quirk_intel_pcie_pm);
1670 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25f7, quirk_intel_pcie_pm);
1671 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25f8, quirk_intel_pcie_pm);
1672 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25f9, quirk_intel_pcie_pm);
1673 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x25fa, quirk_intel_pcie_pm);
1674 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2601, quirk_intel_pcie_pm);
1675 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2602, quirk_intel_pcie_pm);
1676 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2603, quirk_intel_pcie_pm);
1677 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2604, quirk_intel_pcie_pm);
1678 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2605, quirk_intel_pcie_pm);
1679 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2606, quirk_intel_pcie_pm);
1680 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2607, quirk_intel_pcie_pm);
1681 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2608, quirk_intel_pcie_pm);
1682 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x2609, quirk_intel_pcie_pm);
1683 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x260a, quirk_intel_pcie_pm);
1684 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    0x260b, quirk_intel_pcie_pm);
1685
1686 static void quirk_radeon_pm(struct pci_dev *dev)
1687 {
1688         if (dev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
1689             dev->subsystem_device == 0x00e2) {
1690                 if (dev->d3_delay < 20) {
1691                         dev->d3_delay = 20;
1692                         pci_info(dev, "extending delay after power-on from D3 to %d msec\n",
1693                                  dev->d3_delay);
1694                 }
1695         }
1696 }
1697 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x6741, quirk_radeon_pm);
1698
1699 #ifdef CONFIG_X86_IO_APIC
1700 static int dmi_disable_ioapicreroute(const struct dmi_system_id *d)
1701 {
1702         noioapicreroute = 1;
1703         pr_info("%s detected: disable boot interrupt reroute\n", d->ident);
1704
1705         return 0;
1706 }
1707
1708 static const struct dmi_system_id boot_interrupt_dmi_table[] = {
1709         /*
1710          * Systems to exclude from boot interrupt reroute quirks
1711          */
1712         {
1713                 .callback = dmi_disable_ioapicreroute,
1714                 .ident = "ASUSTek Computer INC. M2N-LR",
1715                 .matches = {
1716                         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer INC."),
1717                         DMI_MATCH(DMI_PRODUCT_NAME, "M2N-LR"),
1718                 },
1719         },
1720         {}
1721 };
1722
1723 /*
1724  * Boot interrupts on some chipsets cannot be turned off. For these chipsets,
1725  * remap the original interrupt in the linux kernel to the boot interrupt, so
1726  * that a PCI device's interrupt handler is installed on the boot interrupt
1727  * line instead.
1728  */
1729 static void quirk_reroute_to_boot_interrupts_intel(struct pci_dev *dev)
1730 {
1731         dmi_check_system(boot_interrupt_dmi_table);
1732         if (noioapicquirk || noioapicreroute)
1733                 return;
1734
1735         dev->irq_reroute_variant = INTEL_IRQ_REROUTE_VARIANT;
1736         pci_info(dev, "rerouting interrupts for [%04x:%04x]\n",
1737                  dev->vendor, dev->device);
1738 }
1739 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_80333_0,    quirk_reroute_to_boot_interrupts_intel);
1740 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_80333_1,    quirk_reroute_to_boot_interrupts_intel);
1741 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_ESB2_0,     quirk_reroute_to_boot_interrupts_intel);
1742 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXH_0,      quirk_reroute_to_boot_interrupts_intel);
1743 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXH_1,      quirk_reroute_to_boot_interrupts_intel);
1744 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_PXHV,       quirk_reroute_to_boot_interrupts_intel);
1745 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_80332_0,    quirk_reroute_to_boot_interrupts_intel);
1746 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_80332_1,    quirk_reroute_to_boot_interrupts_intel);
1747 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_80333_0,    quirk_reroute_to_boot_interrupts_intel);
1748 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_80333_1,    quirk_reroute_to_boot_interrupts_intel);
1749 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ESB2_0,     quirk_reroute_to_boot_interrupts_intel);
1750 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_PXH_0,      quirk_reroute_to_boot_interrupts_intel);
1751 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_PXH_1,      quirk_reroute_to_boot_interrupts_intel);
1752 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_PXHV,       quirk_reroute_to_boot_interrupts_intel);
1753 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_80332_0,    quirk_reroute_to_boot_interrupts_intel);
1754 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_80332_1,    quirk_reroute_to_boot_interrupts_intel);
1755
1756 /*
1757  * On some chipsets we can disable the generation of legacy INTx boot
1758  * interrupts.
1759  */
1760
1761 /*
1762  * IO-APIC1 on 6300ESB generates boot interrupts, see intel order no
1763  * 300641-004US, section 5.7.3.
1764  */
1765 #define INTEL_6300_IOAPIC_ABAR          0x40
1766 #define INTEL_6300_DISABLE_BOOT_IRQ     (1<<14)
1767
1768 static void quirk_disable_intel_boot_interrupt(struct pci_dev *dev)
1769 {
1770         u16 pci_config_word;
1771
1772         if (noioapicquirk)
1773                 return;
1774
1775         pci_read_config_word(dev, INTEL_6300_IOAPIC_ABAR, &pci_config_word);
1776         pci_config_word |= INTEL_6300_DISABLE_BOOT_IRQ;
1777         pci_write_config_word(dev, INTEL_6300_IOAPIC_ABAR, pci_config_word);
1778
1779         pci_info(dev, "disabled boot interrupts on device [%04x:%04x]\n",
1780                  dev->vendor, dev->device);
1781 }
1782 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ESB_10,      quirk_disable_intel_boot_interrupt);
1783 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL,   PCI_DEVICE_ID_INTEL_ESB_10,     quirk_disable_intel_boot_interrupt);
1784
1785 /*
1786  * disable boot interrupts on HT-1000
1787  */
1788 #define BC_HT1000_FEATURE_REG           0x64
1789 #define BC_HT1000_PIC_REGS_ENABLE       (1<<0)
1790 #define BC_HT1000_MAP_IDX               0xC00
1791 #define BC_HT1000_MAP_DATA              0xC01
1792
1793 static void quirk_disable_broadcom_boot_interrupt(struct pci_dev *dev)
1794 {
1795         u32 pci_config_dword;
1796         u8 irq;
1797
1798         if (noioapicquirk)
1799                 return;
1800
1801         pci_read_config_dword(dev, BC_HT1000_FEATURE_REG, &pci_config_dword);
1802         pci_write_config_dword(dev, BC_HT1000_FEATURE_REG, pci_config_dword |
1803                         BC_HT1000_PIC_REGS_ENABLE);
1804
1805         for (irq = 0x10; irq < 0x10 + 32; irq++) {
1806                 outb(irq, BC_HT1000_MAP_IDX);
1807                 outb(0x00, BC_HT1000_MAP_DATA);
1808         }
1809
1810         pci_write_config_dword(dev, BC_HT1000_FEATURE_REG, pci_config_dword);
1811
1812         pci_info(dev, "disabled boot interrupts on device [%04x:%04x]\n",
1813                  dev->vendor, dev->device);
1814 }
1815 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SERVERWORKS,   PCI_DEVICE_ID_SERVERWORKS_HT1000SB,        quirk_disable_broadcom_boot_interrupt);
1816 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_SERVERWORKS,   PCI_DEVICE_ID_SERVERWORKS_HT1000SB,       quirk_disable_broadcom_boot_interrupt);
1817
1818 /*
1819  * disable boot interrupts on AMD and ATI chipsets
1820  */
1821 /*
1822  * NOIOAMODE needs to be disabled to disable "boot interrupts". For AMD 8131
1823  * rev. A0 and B0, NOIOAMODE needs to be disabled anyway to fix IO-APIC mode
1824  * (due to an erratum).
1825  */
1826 #define AMD_813X_MISC                   0x40
1827 #define AMD_813X_NOIOAMODE              (1<<0)
1828 #define AMD_813X_REV_B1                 0x12
1829 #define AMD_813X_REV_B2                 0x13
1830
1831 static void quirk_disable_amd_813x_boot_interrupt(struct pci_dev *dev)
1832 {
1833         u32 pci_config_dword;
1834
1835         if (noioapicquirk)
1836                 return;
1837         if ((dev->revision == AMD_813X_REV_B1) ||
1838             (dev->revision == AMD_813X_REV_B2))
1839                 return;
1840
1841         pci_read_config_dword(dev, AMD_813X_MISC, &pci_config_dword);
1842         pci_config_dword &= ~AMD_813X_NOIOAMODE;
1843         pci_write_config_dword(dev, AMD_813X_MISC, pci_config_dword);
1844
1845         pci_info(dev, "disabled boot interrupts on device [%04x:%04x]\n",
1846                  dev->vendor, dev->device);
1847 }
1848 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD,      PCI_DEVICE_ID_AMD_8131_BRIDGE,  quirk_disable_amd_813x_boot_interrupt);
1849 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_AMD,     PCI_DEVICE_ID_AMD_8131_BRIDGE,  quirk_disable_amd_813x_boot_interrupt);
1850 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD,      PCI_DEVICE_ID_AMD_8132_BRIDGE,  quirk_disable_amd_813x_boot_interrupt);
1851 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_AMD,     PCI_DEVICE_ID_AMD_8132_BRIDGE,  quirk_disable_amd_813x_boot_interrupt);
1852
1853 #define AMD_8111_PCI_IRQ_ROUTING        0x56
1854
1855 static void quirk_disable_amd_8111_boot_interrupt(struct pci_dev *dev)
1856 {
1857         u16 pci_config_word;
1858
1859         if (noioapicquirk)
1860                 return;
1861
1862         pci_read_config_word(dev, AMD_8111_PCI_IRQ_ROUTING, &pci_config_word);
1863         if (!pci_config_word) {
1864                 pci_info(dev, "boot interrupts on device [%04x:%04x] already disabled\n",
1865                          dev->vendor, dev->device);
1866                 return;
1867         }
1868         pci_write_config_word(dev, AMD_8111_PCI_IRQ_ROUTING, 0);
1869         pci_info(dev, "disabled boot interrupts on device [%04x:%04x]\n",
1870                  dev->vendor, dev->device);
1871 }
1872 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD,   PCI_DEVICE_ID_AMD_8111_SMBUS,      quirk_disable_amd_8111_boot_interrupt);
1873 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_AMD,   PCI_DEVICE_ID_AMD_8111_SMBUS,     quirk_disable_amd_8111_boot_interrupt);
1874 #endif /* CONFIG_X86_IO_APIC */
1875
1876 /*
1877  * Toshiba TC86C001 IDE controller reports the standard 8-byte BAR0 size
1878  * but the PIO transfers won't work if BAR0 falls at the odd 8 bytes.
1879  * Re-allocate the region if needed...
1880  */
1881 static void quirk_tc86c001_ide(struct pci_dev *dev)
1882 {
1883         struct resource *r = &dev->resource[0];
1884
1885         if (r->start & 0x8) {
1886                 r->flags |= IORESOURCE_UNSET;
1887                 r->start = 0;
1888                 r->end = 0xf;
1889         }
1890 }
1891 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TOSHIBA_2,
1892                          PCI_DEVICE_ID_TOSHIBA_TC86C001_IDE,
1893                          quirk_tc86c001_ide);
1894
1895 /*
1896  * PLX PCI 9050 PCI Target bridge controller has an errata that prevents the
1897  * local configuration registers accessible via BAR0 (memory) or BAR1 (i/o)
1898  * being read correctly if bit 7 of the base address is set.
1899  * The BAR0 or BAR1 region may be disabled (size 0) or enabled (size 128).
1900  * Re-allocate the regions to a 256-byte boundary if necessary.
1901  */
1902 static void quirk_plx_pci9050(struct pci_dev *dev)
1903 {
1904         unsigned int bar;
1905
1906         /* Fixed in revision 2 (PCI 9052). */
1907         if (dev->revision >= 2)
1908                 return;
1909         for (bar = 0; bar <= 1; bar++)
1910                 if (pci_resource_len(dev, bar) == 0x80 &&
1911                     (pci_resource_start(dev, bar) & 0x80)) {
1912                         struct resource *r = &dev->resource[bar];
1913                         pci_info(dev, "Re-allocating PLX PCI 9050 BAR %u to length 256 to avoid bit 7 bug\n",
1914                                  bar);
1915                         r->flags |= IORESOURCE_UNSET;
1916                         r->start = 0;
1917                         r->end = 0xff;
1918                 }
1919 }
1920 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
1921                          quirk_plx_pci9050);
1922 /*
1923  * The following Meilhaus (vendor ID 0x1402) device IDs (amongst others)
1924  * may be using the PLX PCI 9050: 0x0630, 0x0940, 0x0950, 0x0960, 0x100b,
1925  * 0x1400, 0x140a, 0x140b, 0x14e0, 0x14ea, 0x14eb, 0x1604, 0x1608, 0x160c,
1926  * 0x168f, 0x2000, 0x2600, 0x3000, 0x810a, 0x810b.
1927  *
1928  * Currently, device IDs 0x2000 and 0x2600 are used by the Comedi "me_daq"
1929  * driver.
1930  */
1931 DECLARE_PCI_FIXUP_HEADER(0x1402, 0x2000, quirk_plx_pci9050);
1932 DECLARE_PCI_FIXUP_HEADER(0x1402, 0x2600, quirk_plx_pci9050);
1933
1934 static void quirk_netmos(struct pci_dev *dev)
1935 {
1936         unsigned int num_parallel = (dev->subsystem_device & 0xf0) >> 4;
1937         unsigned int num_serial = dev->subsystem_device & 0xf;
1938
1939         /*
1940          * These Netmos parts are multiport serial devices with optional
1941          * parallel ports.  Even when parallel ports are present, they
1942          * are identified as class SERIAL, which means the serial driver
1943          * will claim them.  To prevent this, mark them as class OTHER.
1944          * These combo devices should be claimed by parport_serial.
1945          *
1946          * The subdevice ID is of the form 0x00PS, where <P> is the number
1947          * of parallel ports and <S> is the number of serial ports.
1948          */
1949         switch (dev->device) {
1950         case PCI_DEVICE_ID_NETMOS_9835:
1951                 /* Well, this rule doesn't hold for the following 9835 device */
1952                 if (dev->subsystem_vendor == PCI_VENDOR_ID_IBM &&
1953                                 dev->subsystem_device == 0x0299)
1954                         return;
1955         case PCI_DEVICE_ID_NETMOS_9735:
1956         case PCI_DEVICE_ID_NETMOS_9745:
1957         case PCI_DEVICE_ID_NETMOS_9845:
1958         case PCI_DEVICE_ID_NETMOS_9855:
1959                 if (num_parallel) {
1960                         pci_info(dev, "Netmos %04x (%u parallel, %u serial); changing class SERIAL to OTHER (use parport_serial)\n",
1961                                 dev->device, num_parallel, num_serial);
1962                         dev->class = (PCI_CLASS_COMMUNICATION_OTHER << 8) |
1963                             (dev->class & 0xff);
1964                 }
1965         }
1966 }
1967 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_NETMOS, PCI_ANY_ID,
1968                          PCI_CLASS_COMMUNICATION_SERIAL, 8, quirk_netmos);
1969
1970 static void quirk_e100_interrupt(struct pci_dev *dev)
1971 {
1972         u16 command, pmcsr;
1973         u8 __iomem *csr;
1974         u8 cmd_hi;
1975
1976         switch (dev->device) {
1977         /* PCI IDs taken from drivers/net/e100.c */
1978         case 0x1029:
1979         case 0x1030 ... 0x1034:
1980         case 0x1038 ... 0x103E:
1981         case 0x1050 ... 0x1057:
1982         case 0x1059:
1983         case 0x1064 ... 0x106B:
1984         case 0x1091 ... 0x1095:
1985         case 0x1209:
1986         case 0x1229:
1987         case 0x2449:
1988         case 0x2459:
1989         case 0x245D:
1990         case 0x27DC:
1991                 break;
1992         default:
1993                 return;
1994         }
1995
1996         /*
1997          * Some firmware hands off the e100 with interrupts enabled,
1998          * which can cause a flood of interrupts if packets are
1999          * received before the driver attaches to the device.  So
2000          * disable all e100 interrupts here.  The driver will
2001          * re-enable them when it's ready.
2002          */
2003         pci_read_config_word(dev, PCI_COMMAND, &command);
2004
2005         if (!(command & PCI_COMMAND_MEMORY) || !pci_resource_start(dev, 0))
2006                 return;
2007
2008         /*
2009          * Check that the device is in the D0 power state. If it's not,
2010          * there is no point to look any further.
2011          */
2012         if (dev->pm_cap) {
2013                 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
2014                 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != PCI_D0)
2015                         return;
2016         }
2017
2018         /* Convert from PCI bus to resource space.  */
2019         csr = ioremap(pci_resource_start(dev, 0), 8);
2020         if (!csr) {
2021                 pci_warn(dev, "Can't map e100 registers\n");
2022                 return;
2023         }
2024
2025         cmd_hi = readb(csr + 3);
2026         if (cmd_hi == 0) {
2027                 pci_warn(dev, "Firmware left e100 interrupts enabled; disabling\n");
2028                 writeb(1, csr + 3);
2029         }
2030
2031         iounmap(csr);
2032 }
2033 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
2034                         PCI_CLASS_NETWORK_ETHERNET, 8, quirk_e100_interrupt);
2035
2036 /*
2037  * The 82575 and 82598 may experience data corruption issues when transitioning
2038  * out of L0S.  To prevent this we need to disable L0S on the PCIe link.
2039  */
2040 static void quirk_disable_aspm_l0s(struct pci_dev *dev)
2041 {
2042         pci_info(dev, "Disabling L0s\n");
2043         pci_disable_link_state(dev, PCIE_LINK_STATE_L0S);
2044 }
2045 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10a7, quirk_disable_aspm_l0s);
2046 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10a9, quirk_disable_aspm_l0s);
2047 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10b6, quirk_disable_aspm_l0s);
2048 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10c6, quirk_disable_aspm_l0s);
2049 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10c7, quirk_disable_aspm_l0s);
2050 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10c8, quirk_disable_aspm_l0s);
2051 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10d6, quirk_disable_aspm_l0s);
2052 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10db, quirk_disable_aspm_l0s);
2053 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10dd, quirk_disable_aspm_l0s);
2054 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10e1, quirk_disable_aspm_l0s);
2055 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10ec, quirk_disable_aspm_l0s);
2056 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10f1, quirk_disable_aspm_l0s);
2057 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x10f4, quirk_disable_aspm_l0s);
2058 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1508, quirk_disable_aspm_l0s);
2059
2060 static void fixup_rev1_53c810(struct pci_dev *dev)
2061 {
2062         u32 class = dev->class;
2063
2064         /*
2065          * rev 1 ncr53c810 chips don't set the class at all which means
2066          * they don't get their resources remapped. Fix that here.
2067          */
2068         if (class)
2069                 return;
2070
2071         dev->class = PCI_CLASS_STORAGE_SCSI << 8;
2072         pci_info(dev, "NCR 53c810 rev 1 PCI class overridden (%#08x -> %#08x)\n",
2073                  class, dev->class);
2074 }
2075 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_NCR, PCI_DEVICE_ID_NCR_53C810, fixup_rev1_53c810);
2076
2077 /* Enable 1k I/O space granularity on the Intel P64H2 */
2078 static void quirk_p64h2_1k_io(struct pci_dev *dev)
2079 {
2080         u16 en1k;
2081
2082         pci_read_config_word(dev, 0x40, &en1k);
2083
2084         if (en1k & 0x200) {
2085                 pci_info(dev, "Enable I/O Space to 1KB granularity\n");
2086                 dev->io_window_1k = 1;
2087         }
2088 }
2089 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL,   0x1460,         quirk_p64h2_1k_io);
2090
2091 /* Under some circumstances, AER is not linked with extended capabilities.
2092  * Force it to be linked by setting the corresponding control bit in the
2093  * config space.
2094  */
2095 static void quirk_nvidia_ck804_pcie_aer_ext_cap(struct pci_dev *dev)
2096 {
2097         uint8_t b;
2098         if (pci_read_config_byte(dev, 0xf41, &b) == 0) {
2099                 if (!(b & 0x20)) {
2100                         pci_write_config_byte(dev, 0xf41, b | 0x20);
2101                         pci_info(dev, "Linking AER extended capability\n");
2102                 }
2103         }
2104 }
2105 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA,  PCI_DEVICE_ID_NVIDIA_CK804_PCIE,
2106                         quirk_nvidia_ck804_pcie_aer_ext_cap);
2107 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_NVIDIA,  PCI_DEVICE_ID_NVIDIA_CK804_PCIE,
2108                         quirk_nvidia_ck804_pcie_aer_ext_cap);
2109
2110 static void quirk_via_cx700_pci_parking_caching(struct pci_dev *dev)
2111 {
2112         /*
2113          * Disable PCI Bus Parking and PCI Master read caching on CX700
2114          * which causes unspecified timing errors with a VT6212L on the PCI
2115          * bus leading to USB2.0 packet loss.
2116          *
2117          * This quirk is only enabled if a second (on the external PCI bus)
2118          * VT6212L is found -- the CX700 core itself also contains a USB
2119          * host controller with the same PCI ID as the VT6212L.
2120          */
2121
2122         /* Count VT6212L instances */
2123         struct pci_dev *p = pci_get_device(PCI_VENDOR_ID_VIA,
2124                 PCI_DEVICE_ID_VIA_8235_USB_2, NULL);
2125         uint8_t b;
2126
2127         /* p should contain the first (internal) VT6212L -- see if we have
2128            an external one by searching again */
2129         p = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235_USB_2, p);
2130         if (!p)
2131                 return;
2132         pci_dev_put(p);
2133
2134         if (pci_read_config_byte(dev, 0x76, &b) == 0) {
2135                 if (b & 0x40) {
2136                         /* Turn off PCI Bus Parking */
2137                         pci_write_config_byte(dev, 0x76, b ^ 0x40);
2138
2139                         pci_info(dev, "Disabling VIA CX700 PCI parking\n");
2140                 }
2141         }
2142
2143         if (pci_read_config_byte(dev, 0x72, &b) == 0) {
2144                 if (b != 0) {
2145                         /* Turn off PCI Master read caching */
2146                         pci_write_config_byte(dev, 0x72, 0x0);
2147
2148                         /* Set PCI Master Bus time-out to "1x16 PCLK" */
2149                         pci_write_config_byte(dev, 0x75, 0x1);
2150
2151                         /* Disable "Read FIFO Timer" */
2152                         pci_write_config_byte(dev, 0x77, 0x0);
2153
2154                         pci_info(dev, "Disabling VIA CX700 PCI caching\n");
2155                 }
2156         }
2157 }
2158 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, 0x324e, quirk_via_cx700_pci_parking_caching);
2159
2160 static void quirk_brcm_5719_limit_mrrs(struct pci_dev *dev)
2161 {
2162         u32 rev;
2163
2164         pci_read_config_dword(dev, 0xf4, &rev);
2165
2166         /* Only CAP the MRRS if the device is a 5719 A0 */
2167         if (rev == 0x05719000) {
2168                 int readrq = pcie_get_readrq(dev);
2169                 if (readrq > 2048)
2170                         pcie_set_readrq(dev, 2048);
2171         }
2172 }
2173
2174 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_BROADCOM,
2175                          PCI_DEVICE_ID_TIGON3_5719,
2176                          quirk_brcm_5719_limit_mrrs);
2177
2178 #ifdef CONFIG_PCIE_IPROC_PLATFORM
2179 static void quirk_paxc_bridge(struct pci_dev *pdev)
2180 {
2181         /* The PCI config space is shared with the PAXC root port and the first
2182          * Ethernet device.  So, we need to workaround this by telling the PCI
2183          * code that the bridge is not an Ethernet device.
2184          */
2185         if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
2186                 pdev->class = PCI_CLASS_BRIDGE_PCI << 8;
2187
2188         /* MPSS is not being set properly (as it is currently 0).  This is
2189          * because that area of the PCI config space is hard coded to zero, and
2190          * is not modifiable by firmware.  Set this to 2 (e.g., 512 byte MPS)
2191          * so that the MPS can be set to the real max value.
2192          */
2193         pdev->pcie_mpss = 2;
2194 }
2195 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16cd, quirk_paxc_bridge);
2196 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0, quirk_paxc_bridge);
2197 #endif
2198
2199 /* Originally in EDAC sources for i82875P:
2200  * Intel tells BIOS developers to hide device 6 which
2201  * configures the overflow device access containing
2202  * the DRBs - this is where we expose device 6.
2203  * http://www.x86-secret.com/articles/tweak/pat/patsecrets-2.htm
2204  */
2205 static void quirk_unhide_mch_dev6(struct pci_dev *dev)
2206 {
2207         u8 reg;
2208
2209         if (pci_read_config_byte(dev, 0xF4, &reg) == 0 && !(reg & 0x02)) {
2210                 pci_info(dev, "Enabling MCH 'Overflow' Device\n");
2211                 pci_write_config_byte(dev, 0xF4, reg | 0x02);
2212         }
2213 }
2214
2215 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82865_HB,
2216                         quirk_unhide_mch_dev6);
2217 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82875_HB,
2218                         quirk_unhide_mch_dev6);
2219
2220 #ifdef CONFIG_TILEPRO
2221 /*
2222  * The Tilera TILEmpower tilepro platform needs to set the link speed
2223  * to 2.5GT(Giga-Transfers)/s (Gen 1). The default link speed
2224  * setting is 5GT/s (Gen 2). 0x98 is the Link Control2 PCIe
2225  * capability register of the PEX8624 PCIe switch. The switch
2226  * supports link speed auto negotiation, but falsely sets
2227  * the link speed to 5GT/s.
2228  */
2229 static void quirk_tile_plx_gen1(struct pci_dev *dev)
2230 {
2231         if (tile_plx_gen1) {
2232                 pci_write_config_dword(dev, 0x98, 0x1);
2233                 mdelay(50);
2234         }
2235 }
2236 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_PLX, 0x8624, quirk_tile_plx_gen1);
2237 #endif /* CONFIG_TILEPRO */
2238
2239 #ifdef CONFIG_PCI_MSI
2240 /* Some chipsets do not support MSI. We cannot easily rely on setting
2241  * PCI_BUS_FLAGS_NO_MSI in its bus flags because there are actually
2242  * some other buses controlled by the chipset even if Linux is not
2243  * aware of it.  Instead of setting the flag on all buses in the
2244  * machine, simply disable MSI globally.
2245  */
2246 static void quirk_disable_all_msi(struct pci_dev *dev)
2247 {
2248         pci_no_msi();
2249         pci_warn(dev, "MSI quirk detected; MSI disabled\n");
2250 }
2251 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_GCNB_LE, quirk_disable_all_msi);
2252 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RS400_200, quirk_disable_all_msi);
2253 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RS480, quirk_disable_all_msi);
2254 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT3336, quirk_disable_all_msi);
2255 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT3351, quirk_disable_all_msi);
2256 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT3364, quirk_disable_all_msi);
2257 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8380_0, quirk_disable_all_msi);
2258 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SI, 0x0761, quirk_disable_all_msi);
2259
2260 /* Disable MSI on chipsets that are known to not support it */
2261 static void quirk_disable_msi(struct pci_dev *dev)
2262 {
2263         if (dev->subordinate) {
2264                 pci_warn(dev, "MSI quirk detected; subordinate MSI disabled\n");
2265                 dev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
2266         }
2267 }
2268 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE, quirk_disable_msi);
2269 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, 0xa238, quirk_disable_msi);
2270 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x5a3f, quirk_disable_msi);
2271
2272 /*
2273  * The APC bridge device in AMD 780 family northbridges has some random
2274  * OEM subsystem ID in its vendor ID register (erratum 18), so instead
2275  * we use the possible vendor/device IDs of the host bridge for the
2276  * declared quirk, and search for the APC bridge by slot number.
2277  */
2278 static void quirk_amd_780_apc_msi(struct pci_dev *host_bridge)
2279 {
2280         struct pci_dev *apc_bridge;
2281
2282         apc_bridge = pci_get_slot(host_bridge->bus, PCI_DEVFN(1, 0));
2283         if (apc_bridge) {
2284                 if (apc_bridge->device == 0x9602)
2285                         quirk_disable_msi(apc_bridge);
2286                 pci_dev_put(apc_bridge);
2287         }
2288 }
2289 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, 0x9600, quirk_amd_780_apc_msi);
2290 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, 0x9601, quirk_amd_780_apc_msi);
2291
2292 /* Go through the list of Hypertransport capabilities and
2293  * return 1 if a HT MSI capability is found and enabled */
2294 static int msi_ht_cap_enabled(struct pci_dev *dev)
2295 {
2296         int pos, ttl = PCI_FIND_CAP_TTL;
2297
2298         pos = pci_find_ht_capability(dev, HT_CAPTYPE_MSI_MAPPING);
2299         while (pos && ttl--) {
2300                 u8 flags;
2301
2302                 if (pci_read_config_byte(dev, pos + HT_MSI_FLAGS,
2303                                          &flags) == 0) {
2304                         pci_info(dev, "Found %s HT MSI Mapping\n",
2305                                 flags & HT_MSI_FLAGS_ENABLE ?
2306                                 "enabled" : "disabled");
2307                         return (flags & HT_MSI_FLAGS_ENABLE) != 0;
2308                 }
2309
2310                 pos = pci_find_next_ht_capability(dev, pos,
2311                                                   HT_CAPTYPE_MSI_MAPPING);
2312         }
2313         return 0;
2314 }
2315
2316 /* Check the hypertransport MSI mapping to know whether MSI is enabled or not */
2317 static void quirk_msi_ht_cap(struct pci_dev *dev)
2318 {
2319         if (dev->subordinate && !msi_ht_cap_enabled(dev)) {
2320                 pci_warn(dev, "MSI quirk detected; subordinate MSI disabled\n");
2321                 dev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
2322         }
2323 }
2324 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_HT2000_PCIE,
2325                         quirk_msi_ht_cap);
2326
2327 /* The nVidia CK804 chipset may have 2 HT MSI mappings.
2328  * MSI are supported if the MSI capability set in any of these mappings.
2329  */
2330 static void quirk_nvidia_ck804_msi_ht_cap(struct pci_dev *dev)
2331 {
2332         struct pci_dev *pdev;
2333
2334         if (!dev->subordinate)
2335                 return;
2336
2337         /* check HT MSI cap on this chipset and the root one.
2338          * a single one having MSI is enough to be sure that MSI are supported.
2339          */
2340         pdev = pci_get_slot(dev->bus, 0);
2341         if (!pdev)
2342                 return;
2343         if (!msi_ht_cap_enabled(dev) && !msi_ht_cap_enabled(pdev)) {
2344                 pci_warn(dev, "MSI quirk detected; subordinate MSI disabled\n");
2345                 dev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
2346         }
2347         pci_dev_put(pdev);
2348 }
2349 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_PCIE,
2350                         quirk_nvidia_ck804_msi_ht_cap);
2351
2352 /* Force enable MSI mapping capability on HT bridges */
2353 static void ht_enable_msi_mapping(struct pci_dev *dev)
2354 {
2355         int pos, ttl = PCI_FIND_CAP_TTL;
2356
2357         pos = pci_find_ht_capability(dev, HT_CAPTYPE_MSI_MAPPING);
2358         while (pos && ttl--) {
2359                 u8 flags;
2360
2361                 if (pci_read_config_byte(dev, pos + HT_MSI_FLAGS,
2362                                          &flags) == 0) {
2363                         pci_info(dev, "Enabling HT MSI Mapping\n");
2364
2365                         pci_write_config_byte(dev, pos + HT_MSI_FLAGS,
2366                                               flags | HT_MSI_FLAGS_ENABLE);
2367                 }
2368                 pos = pci_find_next_ht_capability(dev, pos,
2369                                                   HT_CAPTYPE_MSI_MAPPING);
2370         }
2371 }
2372 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS,
2373                          PCI_DEVICE_ID_SERVERWORKS_HT1000_PXB,
2374                          ht_enable_msi_mapping);
2375
2376 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8132_BRIDGE,
2377                          ht_enable_msi_mapping);
2378
2379 /* The P5N32-SLI motherboards from Asus have a problem with msi
2380  * for the MCP55 NIC. It is not yet determined whether the msi problem
2381  * also affects other devices. As for now, turn off msi for this device.
2382  */
2383 static void nvenet_msi_disable(struct pci_dev *dev)
2384 {
2385         const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
2386
2387         if (board_name &&
2388             (strstr(board_name, "P5N32-SLI PREMIUM") ||
2389              strstr(board_name, "P5N32-E SLI"))) {
2390                 pci_info(dev, "Disabling MSI for MCP55 NIC on P5N32-SLI\n");
2391                 dev->no_msi = 1;
2392         }
2393 }
2394 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA,
2395                         PCI_DEVICE_ID_NVIDIA_NVENET_15,
2396                         nvenet_msi_disable);
2397
2398 /*
2399  * Some versions of the MCP55 bridge from Nvidia have a legacy IRQ routing
2400  * config register.  This register controls the routing of legacy
2401  * interrupts from devices that route through the MCP55.  If this register
2402  * is misprogrammed, interrupts are only sent to the BSP, unlike
2403  * conventional systems where the IRQ is broadcast to all online CPUs.  Not
2404  * having this register set properly prevents kdump from booting up
2405  * properly, so let's make sure that we have it set correctly.
2406  * Note that this is an undocumented register.
2407  */
2408 static void nvbridge_check_legacy_irq_routing(struct pci_dev *dev)
2409 {
2410         u32 cfg;
2411
2412         if (!pci_find_capability(dev, PCI_CAP_ID_HT))
2413                 return;
2414
2415         pci_read_config_dword(dev, 0x74, &cfg);
2416
2417         if (cfg & ((1 << 2) | (1 << 15))) {
2418                 printk(KERN_INFO "Rewriting irq routing register on MCP55\n");
2419                 cfg &= ~((1 << 2) | (1 << 15));
2420                 pci_write_config_dword(dev, 0x74, cfg);
2421         }
2422 }
2423
2424 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA,
2425                         PCI_DEVICE_ID_NVIDIA_MCP55_BRIDGE_V0,
2426                         nvbridge_check_legacy_irq_routing);
2427
2428 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA,
2429                         PCI_DEVICE_ID_NVIDIA_MCP55_BRIDGE_V4,
2430                         nvbridge_check_legacy_irq_routing);
2431
2432 static int ht_check_msi_mapping(struct pci_dev *dev)
2433 {
2434         int pos, ttl = PCI_FIND_CAP_TTL;
2435         int found = 0;
2436
2437         /* check if there is HT MSI cap or enabled on this device */
2438         pos = pci_find_ht_capability(dev, HT_CAPTYPE_MSI_MAPPING);
2439         while (pos && ttl--) {
2440                 u8 flags;
2441
2442                 if (found < 1)
2443                         found = 1;
2444                 if (pci_read_config_byte(dev, pos + HT_MSI_FLAGS,
2445                                          &flags) == 0) {
2446                         if (flags & HT_MSI_FLAGS_ENABLE) {
2447                                 if (found < 2) {
2448                                         found = 2;
2449                                         break;
2450                                 }
2451                         }
2452                 }
2453                 pos = pci_find_next_ht_capability(dev, pos,
2454                                                   HT_CAPTYPE_MSI_MAPPING);
2455         }
2456
2457         return found;
2458 }
2459
2460 static int host_bridge_with_leaf(struct pci_dev *host_bridge)
2461 {
2462         struct pci_dev *dev;
2463         int pos;
2464         int i, dev_no;
2465         int found = 0;
2466
2467         dev_no = host_bridge->devfn >> 3;
2468         for (i = dev_no + 1; i < 0x20; i++) {
2469                 dev = pci_get_slot(host_bridge->bus, PCI_DEVFN(i, 0));
2470                 if (!dev)
2471                         continue;
2472
2473                 /* found next host bridge ?*/
2474                 pos = pci_find_ht_capability(dev, HT_CAPTYPE_SLAVE);
2475                 if (pos != 0) {
2476                         pci_dev_put(dev);
2477                         break;
2478                 }
2479
2480                 if (ht_check_msi_mapping(dev)) {
2481                         found = 1;
2482                         pci_dev_put(dev);
2483                         break;
2484                 }
2485                 pci_dev_put(dev);
2486         }
2487
2488         return found;
2489 }
2490
2491 #define PCI_HT_CAP_SLAVE_CTRL0     4    /* link control */
2492 #define PCI_HT_CAP_SLAVE_CTRL1     8    /* link control to */
2493
2494 static int is_end_of_ht_chain(struct pci_dev *dev)
2495 {
2496         int pos, ctrl_off;
2497         int end = 0;
2498         u16 flags, ctrl;
2499
2500         pos = pci_find_ht_capability(dev, HT_CAPTYPE_SLAVE);
2501
2502         if (!pos)
2503                 goto out;
2504
2505         pci_read_config_word(dev, pos + PCI_CAP_FLAGS, &flags);
2506
2507         ctrl_off = ((flags >> 10) & 1) ?
2508                         PCI_HT_CAP_SLAVE_CTRL0 : PCI_HT_CAP_SLAVE_CTRL1;
2509         pci_read_config_word(dev, pos + ctrl_off, &ctrl);
2510
2511         if (ctrl & (1 << 6))
2512                 end = 1;
2513
2514 out:
2515         return end;
2516 }
2517
2518 static void nv_ht_enable_msi_mapping(struct pci_dev *dev)
2519 {
2520         struct pci_dev *host_bridge;
2521         int pos;
2522         int i, dev_no;
2523         int found = 0;
2524
2525         dev_no = dev->devfn >> 3;
2526         for (i = dev_no; i >= 0; i--) {
2527                 host_bridge = pci_get_slot(dev->bus, PCI_DEVFN(i, 0));
2528                 if (!host_bridge)
2529                         continue;
2530
2531                 pos = pci_find_ht_capability(host_bridge, HT_CAPTYPE_SLAVE);
2532                 if (pos != 0) {
2533                         found = 1;
2534                         break;
2535                 }
2536                 pci_dev_put(host_bridge);
2537         }
2538
2539         if (!found)
2540                 return;
2541
2542         /* don't enable end_device/host_bridge with leaf directly here */
2543         if (host_bridge == dev && is_end_of_ht_chain(host_bridge) &&
2544             host_bridge_with_leaf(host_bridge))
2545                 goto out;
2546
2547         /* root did that ! */
2548         if (msi_ht_cap_enabled(host_bridge))
2549                 goto out;
2550
2551         ht_enable_msi_mapping(dev);
2552
2553 out:
2554         pci_dev_put(host_bridge);
2555 }
2556
2557 static void ht_disable_msi_mapping(struct pci_dev *dev)
2558 {
2559         int pos, ttl = PCI_FIND_CAP_TTL;
2560
2561         pos = pci_find_ht_capability(dev, HT_CAPTYPE_MSI_MAPPING);
2562         while (pos && ttl--) {
2563                 u8 flags;
2564
2565                 if (pci_read_config_byte(dev, pos + HT_MSI_FLAGS,
2566                                          &flags) == 0) {
2567                         pci_info(dev, "Disabling HT MSI Mapping\n");
2568
2569                         pci_write_config_byte(dev, pos + HT_MSI_FLAGS,
2570                                               flags & ~HT_MSI_FLAGS_ENABLE);
2571                 }
2572                 pos = pci_find_next_ht_capability(dev, pos,
2573                                                   HT_CAPTYPE_MSI_MAPPING);
2574         }
2575 }
2576
2577 static void __nv_msi_ht_cap_quirk(struct pci_dev *dev, int all)
2578 {
2579         struct pci_dev *host_bridge;
2580         int pos;
2581         int found;
2582
2583         if (!pci_msi_enabled())
2584                 return;
2585
2586         /* check if there is HT MSI cap or enabled on this device */
2587         found = ht_check_msi_mapping(dev);
2588
2589         /* no HT MSI CAP */
2590         if (found == 0)
2591                 return;
2592
2593         /*
2594          * HT MSI mapping should be disabled on devices that are below
2595          * a non-Hypertransport host bridge. Locate the host bridge...
2596          */
2597         host_bridge = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), 0,
2598                                                   PCI_DEVFN(0, 0));
2599         if (host_bridge == NULL) {
2600                 pci_warn(dev, "nv_msi_ht_cap_quirk didn't locate host bridge\n");
2601                 return;
2602         }
2603
2604         pos = pci_find_ht_capability(host_bridge, HT_CAPTYPE_SLAVE);
2605         if (pos != 0) {
2606                 /* Host bridge is to HT */
2607                 if (found == 1) {
2608                         /* it is not enabled, try to enable it */
2609                         if (all)
2610                                 ht_enable_msi_mapping(dev);
2611                         else
2612                                 nv_ht_enable_msi_mapping(dev);
2613                 }
2614                 goto out;
2615         }
2616
2617         /* HT MSI is not enabled */
2618         if (found == 1)
2619                 goto out;
2620
2621         /* Host bridge is not to HT, disable HT MSI mapping on this device */
2622         ht_disable_msi_mapping(dev);
2623
2624 out:
2625         pci_dev_put(host_bridge);
2626 }
2627
2628 static void nv_msi_ht_cap_quirk_all(struct pci_dev *dev)
2629 {
2630         return __nv_msi_ht_cap_quirk(dev, 1);
2631 }
2632
2633 static void nv_msi_ht_cap_quirk_leaf(struct pci_dev *dev)
2634 {
2635         return __nv_msi_ht_cap_quirk(dev, 0);
2636 }
2637
2638 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, nv_msi_ht_cap_quirk_leaf);
2639 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, nv_msi_ht_cap_quirk_leaf);
2640
2641 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AL, PCI_ANY_ID, nv_msi_ht_cap_quirk_all);
2642 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_AL, PCI_ANY_ID, nv_msi_ht_cap_quirk_all);
2643
2644 static void quirk_msi_intx_disable_bug(struct pci_dev *dev)
2645 {
2646         dev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
2647 }
2648 static void quirk_msi_intx_disable_ati_bug(struct pci_dev *dev)
2649 {
2650         struct pci_dev *p;
2651
2652         /* SB700 MSI issue will be fixed at HW level from revision A21,
2653          * we need check PCI REVISION ID of SMBus controller to get SB700
2654          * revision.
2655          */
2656         p = pci_get_device(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS,
2657                            NULL);
2658         if (!p)
2659                 return;
2660
2661         if ((p->revision < 0x3B) && (p->revision >= 0x30))
2662                 dev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
2663         pci_dev_put(p);
2664 }
2665 static void quirk_msi_intx_disable_qca_bug(struct pci_dev *dev)
2666 {
2667         /* AR816X/AR817X/E210X MSI is fixed at HW level from revision 0x18 */
2668         if (dev->revision < 0x18) {
2669                 pci_info(dev, "set MSI_INTX_DISABLE_BUG flag\n");
2670                 dev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
2671         }
2672 }
2673 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2674                         PCI_DEVICE_ID_TIGON3_5780,
2675                         quirk_msi_intx_disable_bug);
2676 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2677                         PCI_DEVICE_ID_TIGON3_5780S,
2678                         quirk_msi_intx_disable_bug);
2679 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2680                         PCI_DEVICE_ID_TIGON3_5714,
2681                         quirk_msi_intx_disable_bug);
2682 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2683                         PCI_DEVICE_ID_TIGON3_5714S,
2684                         quirk_msi_intx_disable_bug);
2685 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2686                         PCI_DEVICE_ID_TIGON3_5715,
2687                         quirk_msi_intx_disable_bug);
2688 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2689                         PCI_DEVICE_ID_TIGON3_5715S,
2690                         quirk_msi_intx_disable_bug);
2691
2692 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4390,
2693                         quirk_msi_intx_disable_ati_bug);
2694 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4391,
2695                         quirk_msi_intx_disable_ati_bug);
2696 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4392,
2697                         quirk_msi_intx_disable_ati_bug);
2698 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4393,
2699                         quirk_msi_intx_disable_ati_bug);
2700 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4394,
2701                         quirk_msi_intx_disable_ati_bug);
2702
2703 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4373,
2704                         quirk_msi_intx_disable_bug);
2705 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4374,
2706                         quirk_msi_intx_disable_bug);
2707 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x4375,
2708                         quirk_msi_intx_disable_bug);
2709
2710 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x1062,
2711                         quirk_msi_intx_disable_bug);
2712 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x1063,
2713                         quirk_msi_intx_disable_bug);
2714 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x2060,
2715                         quirk_msi_intx_disable_bug);
2716 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x2062,
2717                         quirk_msi_intx_disable_bug);
2718 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x1073,
2719                         quirk_msi_intx_disable_bug);
2720 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x1083,
2721                         quirk_msi_intx_disable_bug);
2722 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x1090,
2723                         quirk_msi_intx_disable_qca_bug);
2724 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x1091,
2725                         quirk_msi_intx_disable_qca_bug);
2726 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x10a0,
2727                         quirk_msi_intx_disable_qca_bug);
2728 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0x10a1,
2729                         quirk_msi_intx_disable_qca_bug);
2730 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, 0xe091,
2731                         quirk_msi_intx_disable_qca_bug);
2732 #endif /* CONFIG_PCI_MSI */
2733
2734 /* Allow manual resource allocation for PCI hotplug bridges
2735  * via pci=hpmemsize=nnM and pci=hpiosize=nnM parameters. For
2736  * some PCI-PCI hotplug bridges, like PLX 6254 (former HINT HB6),
2737  * kernel fails to allocate resources when hotplug device is
2738  * inserted and PCI bus is rescanned.
2739  */
2740 static void quirk_hotplug_bridge(struct pci_dev *dev)
2741 {
2742         dev->is_hotplug_bridge = 1;
2743 }
2744
2745 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_HINT, 0x0020, quirk_hotplug_bridge);
2746
2747 /*
2748  * This is a quirk for the Ricoh MMC controller found as a part of
2749  * some mulifunction chips.
2750
2751  * This is very similar and based on the ricoh_mmc driver written by
2752  * Philip Langdale. Thank you for these magic sequences.
2753  *
2754  * These chips implement the four main memory card controllers (SD, MMC, MS, xD)
2755  * and one or both of cardbus or firewire.
2756  *
2757  * It happens that they implement SD and MMC
2758  * support as separate controllers (and PCI functions). The linux SDHCI
2759  * driver supports MMC cards but the chip detects MMC cards in hardware
2760  * and directs them to the MMC controller - so the SDHCI driver never sees
2761  * them.
2762  *
2763  * To get around this, we must disable the useless MMC controller.
2764  * At that point, the SDHCI controller will start seeing them
2765  * It seems to be the case that the relevant PCI registers to deactivate the
2766  * MMC controller live on PCI function 0, which might be the cardbus controller
2767  * or the firewire controller, depending on the particular chip in question
2768  *
2769  * This has to be done early, because as soon as we disable the MMC controller
2770  * other pci functions shift up one level, e.g. function #2 becomes function
2771  * #1, and this will confuse the pci core.
2772  */
2773
2774 #ifdef CONFIG_MMC_RICOH_MMC
2775 static void ricoh_mmc_fixup_rl5c476(struct pci_dev *dev)
2776 {
2777         /* disable via cardbus interface */
2778         u8 write_enable;
2779         u8 write_target;
2780         u8 disable;
2781
2782         /* disable must be done via function #0 */
2783         if (PCI_FUNC(dev->devfn))
2784                 return;
2785
2786         pci_read_config_byte(dev, 0xB7, &disable);
2787         if (disable & 0x02)
2788                 return;
2789
2790         pci_read_config_byte(dev, 0x8E, &write_enable);
2791         pci_write_config_byte(dev, 0x8E, 0xAA);
2792         pci_read_config_byte(dev, 0x8D, &write_target);
2793         pci_write_config_byte(dev, 0x8D, 0xB7);
2794         pci_write_config_byte(dev, 0xB7, disable | 0x02);
2795         pci_write_config_byte(dev, 0x8E, write_enable);
2796         pci_write_config_byte(dev, 0x8D, write_target);
2797
2798         pci_notice(dev, "proprietary Ricoh MMC controller disabled (via cardbus function)\n");
2799         pci_notice(dev, "MMC cards are now supported by standard SDHCI controller\n");
2800 }
2801 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, ricoh_mmc_fixup_rl5c476);
2802 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, ricoh_mmc_fixup_rl5c476);
2803
2804 static void ricoh_mmc_fixup_r5c832(struct pci_dev *dev)
2805 {
2806         /* disable via firewire interface */
2807         u8 write_enable;
2808         u8 disable;
2809
2810         /* disable must be done via function #0 */
2811         if (PCI_FUNC(dev->devfn))
2812                 return;
2813         /*
2814          * RICOH 0xe822 and 0xe823 SD/MMC card readers fail to recognize
2815          * certain types of SD/MMC cards. Lowering the SD base
2816          * clock frequency from 200Mhz to 50Mhz fixes this issue.
2817          *
2818          * 0x150 - SD2.0 mode enable for changing base clock
2819          *         frequency to 50Mhz
2820          * 0xe1  - Base clock frequency
2821          * 0x32  - 50Mhz new clock frequency
2822          * 0xf9  - Key register for 0x150
2823          * 0xfc  - key register for 0xe1
2824          */
2825         if (dev->device == PCI_DEVICE_ID_RICOH_R5CE822 ||
2826             dev->device == PCI_DEVICE_ID_RICOH_R5CE823) {
2827                 pci_write_config_byte(dev, 0xf9, 0xfc);
2828                 pci_write_config_byte(dev, 0x150, 0x10);
2829                 pci_write_config_byte(dev, 0xf9, 0x00);
2830                 pci_write_config_byte(dev, 0xfc, 0x01);
2831                 pci_write_config_byte(dev, 0xe1, 0x32);
2832                 pci_write_config_byte(dev, 0xfc, 0x00);
2833
2834                 pci_notice(dev, "MMC controller base frequency changed to 50Mhz.\n");
2835         }
2836
2837         pci_read_config_byte(dev, 0xCB, &disable);
2838
2839         if (disable & 0x02)
2840                 return;
2841
2842         pci_read_config_byte(dev, 0xCA, &write_enable);
2843         pci_write_config_byte(dev, 0xCA, 0x57);
2844         pci_write_config_byte(dev, 0xCB, disable | 0x02);
2845         pci_write_config_byte(dev, 0xCA, write_enable);
2846
2847         pci_notice(dev, "proprietary Ricoh MMC controller disabled (via firewire function)\n");
2848         pci_notice(dev, "MMC cards are now supported by standard SDHCI controller\n");
2849
2850 }
2851 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5C832, ricoh_mmc_fixup_r5c832);
2852 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5C832, ricoh_mmc_fixup_r5c832);
2853 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5CE822, ricoh_mmc_fixup_r5c832);
2854 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5CE822, ricoh_mmc_fixup_r5c832);
2855 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5CE823, ricoh_mmc_fixup_r5c832);
2856 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5CE823, ricoh_mmc_fixup_r5c832);
2857 #endif /*CONFIG_MMC_RICOH_MMC*/
2858
2859 #ifdef CONFIG_DMAR_TABLE
2860 #define VTUNCERRMSK_REG 0x1ac
2861 #define VTD_MSK_SPEC_ERRORS     (1 << 31)
2862 /*
2863  * This is a quirk for masking vt-d spec defined errors to platform error
2864  * handling logic. With out this, platforms using Intel 7500, 5500 chipsets
2865  * (and the derivative chipsets like X58 etc) seem to generate NMI/SMI (based
2866  * on the RAS config settings of the platform) when a vt-d fault happens.
2867  * The resulting SMI caused the system to hang.
2868  *
2869  * VT-d spec related errors are already handled by the VT-d OS code, so no
2870  * need to report the same error through other channels.
2871  */
2872 static void vtd_mask_spec_errors(struct pci_dev *dev)
2873 {
2874         u32 word;
2875
2876         pci_read_config_dword(dev, VTUNCERRMSK_REG, &word);
2877         pci_write_config_dword(dev, VTUNCERRMSK_REG, word | VTD_MSK_SPEC_ERRORS);
2878 }
2879 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x342e, vtd_mask_spec_errors);
2880 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x3c28, vtd_mask_spec_errors);
2881 #endif
2882
2883 static void fixup_ti816x_class(struct pci_dev *dev)
2884 {
2885         u32 class = dev->class;
2886
2887         /* TI 816x devices do not have class code set when in PCIe boot mode */
2888         dev->class = PCI_CLASS_MULTIMEDIA_VIDEO << 8;
2889         pci_info(dev, "PCI class overridden (%#08x -> %#08x)\n",
2890                  class, dev->class);
2891 }
2892 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_TI, 0xb800,
2893                               PCI_CLASS_NOT_DEFINED, 8, fixup_ti816x_class);
2894
2895 /* Some PCIe devices do not work reliably with the claimed maximum
2896  * payload size supported.
2897  */
2898 static void fixup_mpss_256(struct pci_dev *dev)
2899 {
2900         dev->pcie_mpss = 1; /* 256 bytes */
2901 }
2902 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SOLARFLARE,
2903                          PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0, fixup_mpss_256);
2904 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SOLARFLARE,
2905                          PCI_DEVICE_ID_SOLARFLARE_SFC4000A_1, fixup_mpss_256);
2906 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SOLARFLARE,
2907                          PCI_DEVICE_ID_SOLARFLARE_SFC4000B, fixup_mpss_256);
2908
2909 /* Intel 5000 and 5100 Memory controllers have an errata with read completion
2910  * coalescing (which is enabled by default on some BIOSes) and MPS of 256B.
2911  * Since there is no way of knowing what the PCIE MPS on each fabric will be
2912  * until all of the devices are discovered and buses walked, read completion
2913  * coalescing must be disabled.  Unfortunately, it cannot be re-enabled because
2914  * it is possible to hotplug a device with MPS of 256B.
2915  */
2916 static void quirk_intel_mc_errata(struct pci_dev *dev)
2917 {
2918         int err;
2919         u16 rcc;
2920
2921         if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2922             pcie_bus_config == PCIE_BUS_DEFAULT)
2923                 return;
2924
2925         /* Intel errata specifies bits to change but does not say what they are.
2926          * Keeping them magical until such time as the registers and values can
2927          * be explained.
2928          */
2929         err = pci_read_config_word(dev, 0x48, &rcc);
2930         if (err) {
2931                 pci_err(dev, "Error attempting to read the read completion coalescing register\n");
2932                 return;
2933         }
2934
2935         if (!(rcc & (1 << 10)))
2936                 return;
2937
2938         rcc &= ~(1 << 10);
2939
2940         err = pci_write_config_word(dev, 0x48, rcc);
2941         if (err) {
2942                 pci_err(dev, "Error attempting to write the read completion coalescing register\n");
2943                 return;
2944         }
2945
2946         pr_info_once("Read completion coalescing disabled due to hardware errata relating to 256B MPS\n");
2947 }
2948 /* Intel 5000 series memory controllers and ports 2-7 */
2949 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25c0, quirk_intel_mc_errata);
2950 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25d0, quirk_intel_mc_errata);
2951 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25d4, quirk_intel_mc_errata);
2952 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25d8, quirk_intel_mc_errata);
2953 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25e2, quirk_intel_mc_errata);
2954 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25e3, quirk_intel_mc_errata);
2955 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25e4, quirk_intel_mc_errata);
2956 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25e5, quirk_intel_mc_errata);
2957 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25e6, quirk_intel_mc_errata);
2958 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25e7, quirk_intel_mc_errata);
2959 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25f7, quirk_intel_mc_errata);
2960 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25f8, quirk_intel_mc_errata);
2961 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25f9, quirk_intel_mc_errata);
2962 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x25fa, quirk_intel_mc_errata);
2963 /* Intel 5100 series memory controllers and ports 2-7 */
2964 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65c0, quirk_intel_mc_errata);
2965 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65e2, quirk_intel_mc_errata);
2966 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65e3, quirk_intel_mc_errata);
2967 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65e4, quirk_intel_mc_errata);
2968 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65e5, quirk_intel_mc_errata);
2969 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65e6, quirk_intel_mc_errata);
2970 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65e7, quirk_intel_mc_errata);
2971 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65f7, quirk_intel_mc_errata);
2972 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65f8, quirk_intel_mc_errata);
2973 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65f9, quirk_intel_mc_errata);
2974 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65fa, quirk_intel_mc_errata);
2975
2976
2977 /*
2978  * Ivytown NTB BAR sizes are misreported by the hardware due to an erratum.  To
2979  * work around this, query the size it should be configured to by the device and
2980  * modify the resource end to correspond to this new size.
2981  */
2982 static void quirk_intel_ntb(struct pci_dev *dev)
2983 {
2984         int rc;
2985         u8 val;
2986
2987         rc = pci_read_config_byte(dev, 0x00D0, &val);
2988         if (rc)
2989                 return;
2990
2991         dev->resource[2].end = dev->resource[2].start + ((u64) 1 << val) - 1;
2992
2993         rc = pci_read_config_byte(dev, 0x00D1, &val);
2994         if (rc)
2995                 return;
2996
2997         dev->resource[4].end = dev->resource[4].start + ((u64) 1 << val) - 1;
2998 }
2999 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0e08, quirk_intel_ntb);
3000 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0e0d, quirk_intel_ntb);
3001
3002 static ktime_t fixup_debug_start(struct pci_dev *dev,
3003                                  void (*fn)(struct pci_dev *dev))
3004 {
3005         if (initcall_debug)
3006                 pci_info(dev, "calling  %pF @ %i\n", fn, task_pid_nr(current));
3007
3008         return ktime_get();
3009 }
3010
3011 static void fixup_debug_report(struct pci_dev *dev, ktime_t calltime,
3012                                void (*fn)(struct pci_dev *dev))
3013 {
3014         ktime_t delta, rettime;
3015         unsigned long long duration;
3016
3017         rettime = ktime_get();
3018         delta = ktime_sub(rettime, calltime);
3019         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3020         if (initcall_debug || duration > 10000)
3021                 pci_info(dev, "%pF took %lld usecs\n", fn, duration);
3022 }
3023
3024 /*
3025  * Some BIOS implementations leave the Intel GPU interrupts enabled,
3026  * even though no one is handling them (f.e. i915 driver is never loaded).
3027  * Additionally the interrupt destination is not set up properly
3028  * and the interrupt ends up -somewhere-.
3029  *
3030  * These spurious interrupts are "sticky" and the kernel disables
3031  * the (shared) interrupt line after 100.000+ generated interrupts.
3032  *
3033  * Fix it by disabling the still enabled interrupts.
3034  * This resolves crashes often seen on monitor unplug.
3035  */
3036 #define I915_DEIER_REG 0x4400c
3037 static void disable_igfx_irq(struct pci_dev *dev)
3038 {
3039         void __iomem *regs = pci_iomap(dev, 0, 0);
3040         if (regs == NULL) {
3041                 pci_warn(dev, "igfx quirk: Can't iomap PCI device\n");
3042                 return;
3043         }
3044
3045         /* Check if any interrupt line is still enabled */
3046         if (readl(regs + I915_DEIER_REG) != 0) {
3047                 pci_warn(dev, "BIOS left Intel GPU interrupts enabled; disabling\n");
3048
3049                 writel(0, regs + I915_DEIER_REG);
3050         }
3051
3052         pci_iounmap(dev, regs);
3053 }
3054 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0102, disable_igfx_irq);
3055 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x010a, disable_igfx_irq);
3056 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0152, disable_igfx_irq);
3057
3058 /*
3059  * PCI devices which are on Intel chips can skip the 10ms delay
3060  * before entering D3 mode.
3061  */
3062 static void quirk_remove_d3_delay(struct pci_dev *dev)
3063 {
3064         dev->d3_delay = 0;
3065 }
3066 /* C600 Series devices do not need 10ms d3_delay */
3067 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0412, quirk_remove_d3_delay);
3068 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0c00, quirk_remove_d3_delay);
3069 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0c0c, quirk_remove_d3_delay);
3070 /* Lynxpoint-H PCH devices do not need 10ms d3_delay */
3071 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c02, quirk_remove_d3_delay);
3072 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c18, quirk_remove_d3_delay);
3073 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c1c, quirk_remove_d3_delay);
3074 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c20, quirk_remove_d3_delay);
3075 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c22, quirk_remove_d3_delay);
3076 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c26, quirk_remove_d3_delay);
3077 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c2d, quirk_remove_d3_delay);
3078 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c31, quirk_remove_d3_delay);
3079 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c3a, quirk_remove_d3_delay);
3080 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c3d, quirk_remove_d3_delay);
3081 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x8c4e, quirk_remove_d3_delay);
3082 /* Intel Cherrytrail devices do not need 10ms d3_delay */
3083 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x2280, quirk_remove_d3_delay);
3084 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x2298, quirk_remove_d3_delay);
3085 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x229c, quirk_remove_d3_delay);
3086 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x22b0, quirk_remove_d3_delay);
3087 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x22b5, quirk_remove_d3_delay);
3088 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x22b7, quirk_remove_d3_delay);
3089 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x22b8, quirk_remove_d3_delay);
3090 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x22d8, quirk_remove_d3_delay);
3091 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x22dc, quirk_remove_d3_delay);
3092
3093 /*
3094  * Some devices may pass our check in pci_intx_mask_supported() if
3095  * PCI_COMMAND_INTX_DISABLE works though they actually do not properly
3096  * support this feature.
3097  */
3098 static void quirk_broken_intx_masking(struct pci_dev *dev)
3099 {
3100         dev->broken_intx_masking = 1;
3101 }
3102 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x0030,
3103                         quirk_broken_intx_masking);
3104 DECLARE_PCI_FIXUP_FINAL(0x1814, 0x0601, /* Ralink RT2800 802.11n PCI */
3105                         quirk_broken_intx_masking);
3106 DECLARE_PCI_FIXUP_FINAL(0x1b7c, 0x0004, /* Ceton InfiniTV4 */
3107                         quirk_broken_intx_masking);
3108
3109 /*
3110  * Realtek RTL8169 PCI Gigabit Ethernet Controller (rev 10)
3111  * Subsystem: Realtek RTL8169/8110 Family PCI Gigabit Ethernet NIC
3112  *
3113  * RTL8110SC - Fails under PCI device assignment using DisINTx masking.
3114  */
3115 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_REALTEK, 0x8169,
3116                         quirk_broken_intx_masking);
3117
3118 /*
3119  * Intel i40e (XL710/X710) 10/20/40GbE NICs all have broken INTx masking,
3120  * DisINTx can be set but the interrupt status bit is non-functional.
3121  */
3122 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1572,
3123                         quirk_broken_intx_masking);
3124 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1574,
3125                         quirk_broken_intx_masking);
3126 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1580,
3127                         quirk_broken_intx_masking);
3128 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1581,
3129                         quirk_broken_intx_masking);
3130 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1583,
3131                         quirk_broken_intx_masking);
3132 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1584,
3133                         quirk_broken_intx_masking);
3134 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1585,
3135                         quirk_broken_intx_masking);
3136 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1586,
3137                         quirk_broken_intx_masking);
3138 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1587,
3139                         quirk_broken_intx_masking);
3140 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1588,
3141                         quirk_broken_intx_masking);
3142 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1589,
3143                         quirk_broken_intx_masking);
3144 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x158a,
3145                         quirk_broken_intx_masking);
3146 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x158b,
3147                         quirk_broken_intx_masking);
3148 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x37d0,
3149                         quirk_broken_intx_masking);
3150 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x37d1,
3151                         quirk_broken_intx_masking);
3152 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x37d2,
3153                         quirk_broken_intx_masking);
3154
3155 static u16 mellanox_broken_intx_devs[] = {
3156         PCI_DEVICE_ID_MELLANOX_HERMON_SDR,
3157         PCI_DEVICE_ID_MELLANOX_HERMON_DDR,
3158         PCI_DEVICE_ID_MELLANOX_HERMON_QDR,
3159         PCI_DEVICE_ID_MELLANOX_HERMON_DDR_GEN2,
3160         PCI_DEVICE_ID_MELLANOX_HERMON_QDR_GEN2,
3161         PCI_DEVICE_ID_MELLANOX_HERMON_EN,
3162         PCI_DEVICE_ID_MELLANOX_HERMON_EN_GEN2,
3163         PCI_DEVICE_ID_MELLANOX_CONNECTX_EN,
3164         PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_T_GEN2,
3165         PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_GEN2,
3166         PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_5_GEN2,
3167         PCI_DEVICE_ID_MELLANOX_CONNECTX2,
3168         PCI_DEVICE_ID_MELLANOX_CONNECTX3,
3169         PCI_DEVICE_ID_MELLANOX_CONNECTX3_PRO,
3170 };
3171
3172 #define CONNECTX_4_CURR_MAX_MINOR 99
3173 #define CONNECTX_4_INTX_SUPPORT_MINOR 14
3174
3175 /*
3176  * Check ConnectX-4/LX FW version to see if it supports legacy interrupts.
3177  * If so, don't mark it as broken.
3178  * FW minor > 99 means older FW version format and no INTx masking support.
3179  * FW minor < 14 means new FW version format and no INTx masking support.
3180  */
3181 static void mellanox_check_broken_intx_masking(struct pci_dev *pdev)
3182 {
3183         __be32 __iomem *fw_ver;
3184         u16 fw_major;
3185         u16 fw_minor;
3186         u16 fw_subminor;
3187         u32 fw_maj_min;
3188         u32 fw_sub_min;
3189         int i;
3190
3191         for (i = 0; i < ARRAY_SIZE(mellanox_broken_intx_devs); i++) {
3192                 if (pdev->device == mellanox_broken_intx_devs[i]) {
3193                         pdev->broken_intx_masking = 1;
3194                         return;
3195                 }
3196         }
3197
3198         /* Getting here means Connect-IB cards and up. Connect-IB has no INTx
3199          * support so shouldn't be checked further
3200          */
3201         if (pdev->device == PCI_DEVICE_ID_MELLANOX_CONNECTIB)
3202                 return;
3203
3204         if (pdev->device != PCI_DEVICE_ID_MELLANOX_CONNECTX4 &&
3205             pdev->device != PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX)
3206                 return;
3207
3208         /* For ConnectX-4 and ConnectX-4LX, need to check FW support */
3209         if (pci_enable_device_mem(pdev)) {
3210                 pci_warn(pdev, "Can't enable device memory\n");
3211                 return;
3212         }
3213
3214         fw_ver = ioremap(pci_resource_start(pdev, 0), 4);
3215         if (!fw_ver) {
3216                 pci_warn(pdev, "Can't map ConnectX-4 initialization segment\n");
3217                 goto out;
3218         }
3219
3220         /* Reading from resource space should be 32b aligned */
3221         fw_maj_min = ioread32be(fw_ver);
3222         fw_sub_min = ioread32be(fw_ver + 1);
3223         fw_major = fw_maj_min & 0xffff;
3224         fw_minor = fw_maj_min >> 16;
3225         fw_subminor = fw_sub_min & 0xffff;
3226         if (fw_minor > CONNECTX_4_CURR_MAX_MINOR ||
3227             fw_minor < CONNECTX_4_INTX_SUPPORT_MINOR) {
3228                 pci_warn(pdev, "ConnectX-4: FW %u.%u.%u doesn't support INTx masking, disabling. Please upgrade FW to %d.14.1100 and up for INTx support\n",
3229                          fw_major, fw_minor, fw_subminor, pdev->device ==
3230                          PCI_DEVICE_ID_MELLANOX_CONNECTX4 ? 12 : 14);
3231                 pdev->broken_intx_masking = 1;
3232         }
3233
3234         iounmap(fw_ver);
3235
3236 out:
3237         pci_disable_device(pdev);
3238 }
3239 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_MELLANOX, PCI_ANY_ID,
3240                         mellanox_check_broken_intx_masking);
3241
3242 static void quirk_no_bus_reset(struct pci_dev *dev)
3243 {
3244         dev->dev_flags |= PCI_DEV_FLAGS_NO_BUS_RESET;
3245 }
3246
3247 /*
3248  * Some Atheros AR9xxx and QCA988x chips do not behave after a bus reset.
3249  * The device will throw a Link Down error on AER-capable systems and
3250  * regardless of AER, config space of the device is never accessible again
3251  * and typically causes the system to hang or reset when access is attempted.
3252  * http://www.spinics.net/lists/linux-pci/msg34797.html
3253  */
3254 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATHEROS, 0x0030, quirk_no_bus_reset);
3255 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATHEROS, 0x0032, quirk_no_bus_reset);
3256 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATHEROS, 0x003c, quirk_no_bus_reset);
3257 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATHEROS, 0x0033, quirk_no_bus_reset);
3258
3259 /*
3260  * Root port on some Cavium CN8xxx chips do not successfully complete a bus
3261  * reset when used with certain child devices.  After the reset, config
3262  * accesses to the child may fail.
3263  */
3264 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CAVIUM, 0xa100, quirk_no_bus_reset);
3265
3266 static void quirk_no_pm_reset(struct pci_dev *dev)
3267 {
3268         /*
3269          * We can't do a bus reset on root bus devices, but an ineffective
3270          * PM reset may be better than nothing.
3271          */
3272         if (!pci_is_root_bus(dev->bus))
3273                 dev->dev_flags |= PCI_DEV_FLAGS_NO_PM_RESET;
3274 }
3275
3276 /*
3277  * Some AMD/ATI GPUS (HD8570 - Oland) report that a D3hot->D0 transition
3278  * causes a reset (i.e., they advertise NoSoftRst-).  This transition seems
3279  * to have no effect on the device: it retains the framebuffer contents and
3280  * monitor sync.  Advertising this support makes other layers, like VFIO,
3281  * assume pci_reset_function() is viable for this device.  Mark it as
3282  * unavailable to skip it when testing reset methods.
3283  */
3284 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_ATI, PCI_ANY_ID,
3285                                PCI_CLASS_DISPLAY_VGA, 8, quirk_no_pm_reset);
3286
3287 /*
3288  * Thunderbolt controllers with broken MSI hotplug signaling:
3289  * Entire 1st generation (Light Ridge, Eagle Ridge, Light Peak) and part
3290  * of the 2nd generation (Cactus Ridge 4C up to revision 1, Port Ridge).
3291  */
3292 static void quirk_thunderbolt_hotplug_msi(struct pci_dev *pdev)
3293 {
3294         if (pdev->is_hotplug_bridge &&
3295             (pdev->device != PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C ||
3296              pdev->revision <= 1))
3297                 pdev->no_msi = 1;
3298 }
3299 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
3300                         quirk_thunderbolt_hotplug_msi);
3301 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EAGLE_RIDGE,
3302                         quirk_thunderbolt_hotplug_msi);
3303 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LIGHT_PEAK,
3304                         quirk_thunderbolt_hotplug_msi);
3305 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
3306                         quirk_thunderbolt_hotplug_msi);
3307 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
3308                         quirk_thunderbolt_hotplug_msi);
3309
3310 #ifdef CONFIG_ACPI
3311 /*
3312  * Apple: Shutdown Cactus Ridge Thunderbolt controller.
3313  *
3314  * On Apple hardware the Cactus Ridge Thunderbolt controller needs to be
3315  * shutdown before suspend. Otherwise the native host interface (NHI) will not
3316  * be present after resume if a device was plugged in before suspend.
3317  *
3318  * The thunderbolt controller consists of a pcie switch with downstream
3319  * bridges leading to the NHI and to the tunnel pci bridges.
3320  *
3321  * This quirk cuts power to the whole chip. Therefore we have to apply it
3322  * during suspend_noirq of the upstream bridge.
3323  *
3324  * Power is automagically restored before resume. No action is needed.
3325  */
3326 static void quirk_apple_poweroff_thunderbolt(struct pci_dev *dev)
3327 {
3328         acpi_handle bridge, SXIO, SXFP, SXLV;
3329
3330         if (!x86_apple_machine)
3331                 return;
3332         if (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM)
3333                 return;
3334         bridge = ACPI_HANDLE(&dev->dev);
3335         if (!bridge)
3336                 return;
3337         /*
3338          * SXIO and SXLV are present only on machines requiring this quirk.
3339          * TB bridges in external devices might have the same device id as those
3340          * on the host, but they will not have the associated ACPI methods. This
3341          * implicitly checks that we are at the right bridge.
3342          */
3343         if (ACPI_FAILURE(acpi_get_handle(bridge, "DSB0.NHI0.SXIO", &SXIO))
3344             || ACPI_FAILURE(acpi_get_handle(bridge, "DSB0.NHI0.SXFP", &SXFP))
3345             || ACPI_FAILURE(acpi_get_handle(bridge, "DSB0.NHI0.SXLV", &SXLV)))
3346                 return;
3347         pci_info(dev, "quirk: cutting power to thunderbolt controller...\n");
3348
3349         /* magic sequence */
3350         acpi_execute_simple_method(SXIO, NULL, 1);
3351         acpi_execute_simple_method(SXFP, NULL, 0);
3352         msleep(300);
3353         acpi_execute_simple_method(SXLV, NULL, 0);
3354         acpi_execute_simple_method(SXIO, NULL, 0);
3355         acpi_execute_simple_method(SXLV, NULL, 0);
3356 }
3357 DECLARE_PCI_FIXUP_SUSPEND_LATE(PCI_VENDOR_ID_INTEL,
3358                                PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
3359                                quirk_apple_poweroff_thunderbolt);
3360
3361 /*
3362  * Apple: Wait for the thunderbolt controller to reestablish pci tunnels.
3363  *
3364  * During suspend the thunderbolt controller is reset and all pci
3365  * tunnels are lost. The NHI driver will try to reestablish all tunnels
3366  * during resume. We have to manually wait for the NHI since there is
3367  * no parent child relationship between the NHI and the tunneled
3368  * bridges.
3369  */
3370 static void quirk_apple_wait_for_thunderbolt(struct pci_dev *dev)
3371 {
3372         struct pci_dev *sibling = NULL;
3373         struct pci_dev *nhi = NULL;
3374
3375         if (!x86_apple_machine)
3376                 return;
3377         if (pci_pcie_type(dev) != PCI_EXP_TYPE_DOWNSTREAM)
3378                 return;
3379         /*
3380          * Find the NHI and confirm that we are a bridge on the tb host
3381          * controller and not on a tb endpoint.
3382          */
3383         sibling = pci_get_slot(dev->bus, 0x0);
3384         if (sibling == dev)
3385                 goto out; /* we are the downstream bridge to the NHI */
3386         if (!sibling || !sibling->subordinate)
3387                 goto out;
3388         nhi = pci_get_slot(sibling->subordinate, 0x0);
3389         if (!nhi)
3390                 goto out;
3391         if (nhi->vendor != PCI_VENDOR_ID_INTEL
3392                     || (nhi->device != PCI_DEVICE_ID_INTEL_LIGHT_RIDGE &&
3393                         nhi->device != PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C &&
3394                         nhi->device != PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI &&
3395                         nhi->device != PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI)
3396                     || nhi->class != PCI_CLASS_SYSTEM_OTHER << 8)
3397                 goto out;
3398         pci_info(dev, "quirk: waiting for thunderbolt to reestablish PCI tunnels...\n");
3399         device_pm_wait_for_dev(&dev->dev, &nhi->dev);
3400 out:
3401         pci_dev_put(nhi);
3402         pci_dev_put(sibling);
3403 }
3404 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,
3405                                PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
3406                                quirk_apple_wait_for_thunderbolt);
3407 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,
3408                                PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
3409                                quirk_apple_wait_for_thunderbolt);
3410 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,
3411                                PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE,
3412                                quirk_apple_wait_for_thunderbolt);
3413 DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL,
3414                                PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE,
3415                                quirk_apple_wait_for_thunderbolt);
3416 #endif
3417
3418 static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f,
3419                           struct pci_fixup *end)
3420 {
3421         ktime_t calltime;
3422
3423         for (; f < end; f++)
3424                 if ((f->class == (u32) (dev->class >> f->class_shift) ||
3425                      f->class == (u32) PCI_ANY_ID) &&
3426                     (f->vendor == dev->vendor ||
3427                      f->vendor == (u16) PCI_ANY_ID) &&
3428                     (f->device == dev->device ||
3429                      f->device == (u16) PCI_ANY_ID)) {
3430                         calltime = fixup_debug_start(dev, f->hook);
3431                         f->hook(dev);
3432                         fixup_debug_report(dev, calltime, f->hook);
3433                 }
3434 }
3435
3436 extern struct pci_fixup __start_pci_fixups_early[];
3437 extern struct pci_fixup __end_pci_fixups_early[];
3438 extern struct pci_fixup __start_pci_fixups_header[];
3439 extern struct pci_fixup __end_pci_fixups_header[];
3440 extern struct pci_fixup __start_pci_fixups_final[];
3441 extern struct pci_fixup __end_pci_fixups_final[];
3442 extern struct pci_fixup __start_pci_fixups_enable[];
3443 extern struct pci_fixup __end_pci_fixups_enable[];
3444 extern struct pci_fixup __start_pci_fixups_resume[];
3445 extern struct pci_fixup __end_pci_fixups_resume[];
3446 extern struct pci_fixup __start_pci_fixups_resume_early[];
3447 extern struct pci_fixup __end_pci_fixups_resume_early[];
3448 extern struct pci_fixup __start_pci_fixups_suspend[];
3449 extern struct pci_fixup __end_pci_fixups_suspend[];
3450 extern struct pci_fixup __start_pci_fixups_suspend_late[];
3451 extern struct pci_fixup __end_pci_fixups_suspend_late[];
3452
3453 static bool pci_apply_fixup_final_quirks;
3454
3455 void pci_fixup_device(enum pci_fixup_pass pass, struct pci_dev *dev)
3456 {
3457         struct pci_fixup *start, *end;
3458