x86/quirks: Apply nvidia_bugs quirk only on root bus
[pandora-kernel.git] / arch / x86 / kernel / early-quirks.c
1 /* Various workarounds for chipset bugs.
2    This code runs very early and can't use the regular PCI subsystem
3    The entries are keyed to PCI bridges which usually identify chipsets
4    uniquely.
5    This is only for whole classes of chipsets with specific problems which
6    need early invasive action (e.g. before the timers are initialized).
7    Most PCI device specific workarounds can be done later and should be
8    in standard PCI quirks
9    Mainboard specific bugs should be handled by DMI entries.
10    CPU specific bugs in setup.c */
11
12 #include <linux/pci.h>
13 #include <linux/acpi.h>
14 #include <linux/pci_ids.h>
15 #include <asm/pci-direct.h>
16 #include <asm/dma.h>
17 #include <asm/io_apic.h>
18 #include <asm/apic.h>
19 #include <asm/iommu.h>
20 #include <asm/gart.h>
21
22 static void __init fix_hypertransport_config(int num, int slot, int func)
23 {
24         u32 htcfg;
25         /*
26          * we found a hypertransport bus
27          * make sure that we are broadcasting
28          * interrupts to all cpus on the ht bus
29          * if we're using extended apic ids
30          */
31         htcfg = read_pci_config(num, slot, func, 0x68);
32         if (htcfg & (1 << 18)) {
33                 printk(KERN_INFO "Detected use of extended apic ids "
34                                  "on hypertransport bus\n");
35                 if ((htcfg & (1 << 17)) == 0) {
36                         printk(KERN_INFO "Enabling hypertransport extended "
37                                          "apic interrupt broadcast\n");
38                         printk(KERN_INFO "Note this is a bios bug, "
39                                          "please contact your hw vendor\n");
40                         htcfg |= (1 << 17);
41                         write_pci_config(num, slot, func, 0x68, htcfg);
42                 }
43         }
44
45
46 }
47
48 static void __init via_bugs(int  num, int slot, int func)
49 {
50 #ifdef CONFIG_GART_IOMMU
51         if ((max_pfn > MAX_DMA32_PFN ||  force_iommu) &&
52             !gart_iommu_aperture_allowed) {
53                 printk(KERN_INFO
54                        "Looks like a VIA chipset. Disabling IOMMU."
55                        " Override with iommu=allowed\n");
56                 gart_iommu_aperture_disabled = 1;
57         }
58 #endif
59 }
60
61 #ifdef CONFIG_ACPI
62 #ifdef CONFIG_X86_IO_APIC
63
64 static int __init nvidia_hpet_check(struct acpi_table_header *header)
65 {
66         return 0;
67 }
68 #endif /* CONFIG_X86_IO_APIC */
69 #endif /* CONFIG_ACPI */
70
71 static void __init nvidia_bugs(int num, int slot, int func)
72 {
73 #ifdef CONFIG_ACPI
74 #ifdef CONFIG_X86_IO_APIC
75         /*
76          * Only applies to Nvidia root ports (bus 0) and not to
77          * Nvidia graphics cards with PCI ports on secondary buses.
78          */
79         if (num)
80                 return;
81
82         /*
83          * All timer overrides on Nvidia are
84          * wrong unless HPET is enabled.
85          * Unfortunately that's not true on many Asus boards.
86          * We don't know yet how to detect this automatically, but
87          * at least allow a command line override.
88          */
89         if (acpi_use_timer_override)
90                 return;
91
92         if (acpi_table_parse(ACPI_SIG_HPET, nvidia_hpet_check)) {
93                 acpi_skip_timer_override = 1;
94                 printk(KERN_INFO "Nvidia board "
95                        "detected. Ignoring ACPI "
96                        "timer override.\n");
97                 printk(KERN_INFO "If you got timer trouble "
98                         "try acpi_use_timer_override\n");
99         }
100 #endif
101 #endif
102         /* RED-PEN skip them on mptables too? */
103
104 }
105
106 #if defined(CONFIG_ACPI) && defined(CONFIG_X86_IO_APIC)
107 static u32 __init ati_ixp4x0_rev(int num, int slot, int func)
108 {
109         u32 d;
110         u8  b;
111
112         b = read_pci_config_byte(num, slot, func, 0xac);
113         b &= ~(1<<5);
114         write_pci_config_byte(num, slot, func, 0xac, b);
115
116         d = read_pci_config(num, slot, func, 0x70);
117         d |= 1<<8;
118         write_pci_config(num, slot, func, 0x70, d);
119
120         d = read_pci_config(num, slot, func, 0x8);
121         d &= 0xff;
122         return d;
123 }
124
125 static void __init ati_bugs(int num, int slot, int func)
126 {
127         u32 d;
128         u8  b;
129
130         if (acpi_use_timer_override)
131                 return;
132
133         d = ati_ixp4x0_rev(num, slot, func);
134         if (d  < 0x82)
135                 acpi_skip_timer_override = 1;
136         else {
137                 /* check for IRQ0 interrupt swap */
138                 outb(0x72, 0xcd6); b = inb(0xcd7);
139                 if (!(b & 0x2))
140                         acpi_skip_timer_override = 1;
141         }
142
143         if (acpi_skip_timer_override) {
144                 printk(KERN_INFO "SB4X0 revision 0x%x\n", d);
145                 printk(KERN_INFO "Ignoring ACPI timer override.\n");
146                 printk(KERN_INFO "If you got timer trouble "
147                        "try acpi_use_timer_override\n");
148         }
149 }
150
151 static u32 __init ati_sbx00_rev(int num, int slot, int func)
152 {
153         u32 d;
154
155         d = read_pci_config(num, slot, func, 0x8);
156         d &= 0xff;
157
158         return d;
159 }
160
161 static void __init ati_bugs_contd(int num, int slot, int func)
162 {
163         u32 d, rev;
164
165         rev = ati_sbx00_rev(num, slot, func);
166         if (rev >= 0x40)
167                 acpi_fix_pin2_polarity = 1;
168
169         /*
170          * SB600: revisions 0x11, 0x12, 0x13, 0x14, ...
171          * SB700: revisions 0x39, 0x3a, ...
172          * SB800: revisions 0x40, 0x41, ...
173          */
174         if (rev >= 0x39)
175                 return;
176
177         if (acpi_use_timer_override)
178                 return;
179
180         /* check for IRQ0 interrupt swap */
181         d = read_pci_config(num, slot, func, 0x64);
182         if (!(d & (1<<14)))
183                 acpi_skip_timer_override = 1;
184
185         if (acpi_skip_timer_override) {
186                 printk(KERN_INFO "SB600 revision 0x%x\n", rev);
187                 printk(KERN_INFO "Ignoring ACPI timer override.\n");
188                 printk(KERN_INFO "If you got timer trouble "
189                        "try acpi_use_timer_override\n");
190         }
191 }
192 #else
193 static void __init ati_bugs(int num, int slot, int func)
194 {
195 }
196
197 static void __init ati_bugs_contd(int num, int slot, int func)
198 {
199 }
200 #endif
201
202 #define QFLAG_APPLY_ONCE        0x1
203 #define QFLAG_APPLIED           0x2
204 #define QFLAG_DONE              (QFLAG_APPLY_ONCE|QFLAG_APPLIED)
205 struct chipset {
206         u32 vendor;
207         u32 device;
208         u32 class;
209         u32 class_mask;
210         u32 flags;
211         void (*f)(int num, int slot, int func);
212 };
213
214 /*
215  * Only works for devices on the root bus. If you add any devices
216  * not on bus 0 readd another loop level in early_quirks(). But
217  * be careful because at least the Nvidia quirk here relies on
218  * only matching on bus 0.
219  */
220 static struct chipset early_qrk[] __initdata = {
221         { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
222           PCI_CLASS_BRIDGE_PCI, PCI_ANY_ID, QFLAG_APPLY_ONCE, nvidia_bugs },
223         { PCI_VENDOR_ID_VIA, PCI_ANY_ID,
224           PCI_CLASS_BRIDGE_PCI, PCI_ANY_ID, QFLAG_APPLY_ONCE, via_bugs },
225         { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB,
226           PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, fix_hypertransport_config },
227         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS,
228           PCI_CLASS_SERIAL_SMBUS, PCI_ANY_ID, 0, ati_bugs },
229         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS,
230           PCI_CLASS_SERIAL_SMBUS, PCI_ANY_ID, 0, ati_bugs_contd },
231         {}
232 };
233
234 /**
235  * check_dev_quirk - apply early quirks to a given PCI device
236  * @num: bus number
237  * @slot: slot number
238  * @func: PCI function
239  *
240  * Check the vendor & device ID against the early quirks table.
241  *
242  * If the device is single function, let early_quirks() know so we don't
243  * poke at this device again.
244  */
245 static int __init check_dev_quirk(int num, int slot, int func)
246 {
247         u16 class;
248         u16 vendor;
249         u16 device;
250         u8 type;
251         int i;
252
253         class = read_pci_config_16(num, slot, func, PCI_CLASS_DEVICE);
254
255         if (class == 0xffff)
256                 return -1; /* no class, treat as single function */
257
258         vendor = read_pci_config_16(num, slot, func, PCI_VENDOR_ID);
259
260         device = read_pci_config_16(num, slot, func, PCI_DEVICE_ID);
261
262         for (i = 0; early_qrk[i].f != NULL; i++) {
263                 if (((early_qrk[i].vendor == PCI_ANY_ID) ||
264                         (early_qrk[i].vendor == vendor)) &&
265                         ((early_qrk[i].device == PCI_ANY_ID) ||
266                         (early_qrk[i].device == device)) &&
267                         (!((early_qrk[i].class ^ class) &
268                             early_qrk[i].class_mask))) {
269                                 if ((early_qrk[i].flags &
270                                      QFLAG_DONE) != QFLAG_DONE)
271                                         early_qrk[i].f(num, slot, func);
272                                 early_qrk[i].flags |= QFLAG_APPLIED;
273                         }
274         }
275
276         type = read_pci_config_byte(num, slot, func,
277                                     PCI_HEADER_TYPE);
278         if (!(type & 0x80))
279                 return -1;
280
281         return 0;
282 }
283
284 void __init early_quirks(void)
285 {
286         int slot, func;
287
288         if (!early_pci_allowed())
289                 return;
290
291         /* Poor man's PCI discovery */
292         /* Only scan the root bus */
293         for (slot = 0; slot < 32; slot++)
294                 for (func = 0; func < 8; func++) {
295                         /* Only probe function 0 on single fn devices */
296                         if (check_dev_quirk(0, slot, func))
297                                 break;
298                 }
299 }