x86/spectre: Add boot time option to select Spectre v2 mitigation
[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/delay.h>
15 #include <linux/dmi.h>
16 #include <linux/pci_ids.h>
17 #include <linux/bcma/bcma.h>
18 #include <linux/bcma/bcma_regs.h>
19 #include <asm/pci-direct.h>
20 #include <asm/dma.h>
21 #include <asm/io_apic.h>
22 #include <asm/apic.h>
23 #include <asm/iommu.h>
24 #include <asm/gart.h>
25 #include <asm/io.h>
26
27 #define dev_err(msg)  pr_err("pci 0000:%02x:%02x.%d: %s", bus, slot, func, msg)
28
29 static void __init fix_hypertransport_config(int num, int slot, int func)
30 {
31         u32 htcfg;
32         /*
33          * we found a hypertransport bus
34          * make sure that we are broadcasting
35          * interrupts to all cpus on the ht bus
36          * if we're using extended apic ids
37          */
38         htcfg = read_pci_config(num, slot, func, 0x68);
39         if (htcfg & (1 << 18)) {
40                 printk(KERN_INFO "Detected use of extended apic ids "
41                                  "on hypertransport bus\n");
42                 if ((htcfg & (1 << 17)) == 0) {
43                         printk(KERN_INFO "Enabling hypertransport extended "
44                                          "apic interrupt broadcast\n");
45                         printk(KERN_INFO "Note this is a bios bug, "
46                                          "please contact your hw vendor\n");
47                         htcfg |= (1 << 17);
48                         write_pci_config(num, slot, func, 0x68, htcfg);
49                 }
50         }
51
52
53 }
54
55 static void __init via_bugs(int  num, int slot, int func)
56 {
57 #ifdef CONFIG_GART_IOMMU
58         if ((max_pfn > MAX_DMA32_PFN ||  force_iommu) &&
59             !gart_iommu_aperture_allowed) {
60                 printk(KERN_INFO
61                        "Looks like a VIA chipset. Disabling IOMMU."
62                        " Override with iommu=allowed\n");
63                 gart_iommu_aperture_disabled = 1;
64         }
65 #endif
66 }
67
68 #ifdef CONFIG_ACPI
69 #ifdef CONFIG_X86_IO_APIC
70
71 static int __init nvidia_hpet_check(struct acpi_table_header *header)
72 {
73         return 0;
74 }
75 #endif /* CONFIG_X86_IO_APIC */
76 #endif /* CONFIG_ACPI */
77
78 static void __init nvidia_bugs(int num, int slot, int func)
79 {
80 #ifdef CONFIG_ACPI
81 #ifdef CONFIG_X86_IO_APIC
82         /*
83          * Only applies to Nvidia root ports (bus 0) and not to
84          * Nvidia graphics cards with PCI ports on secondary buses.
85          */
86         if (num)
87                 return;
88
89         /*
90          * All timer overrides on Nvidia are
91          * wrong unless HPET is enabled.
92          * Unfortunately that's not true on many Asus boards.
93          * We don't know yet how to detect this automatically, but
94          * at least allow a command line override.
95          */
96         if (acpi_use_timer_override)
97                 return;
98
99         if (acpi_table_parse(ACPI_SIG_HPET, nvidia_hpet_check)) {
100                 acpi_skip_timer_override = 1;
101                 printk(KERN_INFO "Nvidia board "
102                        "detected. Ignoring ACPI "
103                        "timer override.\n");
104                 printk(KERN_INFO "If you got timer trouble "
105                         "try acpi_use_timer_override\n");
106         }
107 #endif
108 #endif
109         /* RED-PEN skip them on mptables too? */
110
111 }
112
113 #if defined(CONFIG_ACPI) && defined(CONFIG_X86_IO_APIC)
114 static u32 __init ati_ixp4x0_rev(int num, int slot, int func)
115 {
116         u32 d;
117         u8  b;
118
119         b = read_pci_config_byte(num, slot, func, 0xac);
120         b &= ~(1<<5);
121         write_pci_config_byte(num, slot, func, 0xac, b);
122
123         d = read_pci_config(num, slot, func, 0x70);
124         d |= 1<<8;
125         write_pci_config(num, slot, func, 0x70, d);
126
127         d = read_pci_config(num, slot, func, 0x8);
128         d &= 0xff;
129         return d;
130 }
131
132 static void __init ati_bugs(int num, int slot, int func)
133 {
134         u32 d;
135         u8  b;
136
137         if (acpi_use_timer_override)
138                 return;
139
140         d = ati_ixp4x0_rev(num, slot, func);
141         if (d  < 0x82)
142                 acpi_skip_timer_override = 1;
143         else {
144                 /* check for IRQ0 interrupt swap */
145                 outb(0x72, 0xcd6); b = inb(0xcd7);
146                 if (!(b & 0x2))
147                         acpi_skip_timer_override = 1;
148         }
149
150         if (acpi_skip_timer_override) {
151                 printk(KERN_INFO "SB4X0 revision 0x%x\n", d);
152                 printk(KERN_INFO "Ignoring ACPI timer override.\n");
153                 printk(KERN_INFO "If you got timer trouble "
154                        "try acpi_use_timer_override\n");
155         }
156 }
157
158 static u32 __init ati_sbx00_rev(int num, int slot, int func)
159 {
160         u32 d;
161
162         d = read_pci_config(num, slot, func, 0x8);
163         d &= 0xff;
164
165         return d;
166 }
167
168 static void __init ati_bugs_contd(int num, int slot, int func)
169 {
170         u32 d, rev;
171
172         rev = ati_sbx00_rev(num, slot, func);
173         if (rev >= 0x40)
174                 acpi_fix_pin2_polarity = 1;
175
176         /*
177          * SB600: revisions 0x11, 0x12, 0x13, 0x14, ...
178          * SB700: revisions 0x39, 0x3a, ...
179          * SB800: revisions 0x40, 0x41, ...
180          */
181         if (rev >= 0x39)
182                 return;
183
184         if (acpi_use_timer_override)
185                 return;
186
187         /* check for IRQ0 interrupt swap */
188         d = read_pci_config(num, slot, func, 0x64);
189         if (!(d & (1<<14)))
190                 acpi_skip_timer_override = 1;
191
192         if (acpi_skip_timer_override) {
193                 printk(KERN_INFO "SB600 revision 0x%x\n", rev);
194                 printk(KERN_INFO "Ignoring ACPI timer override.\n");
195                 printk(KERN_INFO "If you got timer trouble "
196                        "try acpi_use_timer_override\n");
197         }
198 }
199 #else
200 static void __init ati_bugs(int num, int slot, int func)
201 {
202 }
203
204 static void __init ati_bugs_contd(int num, int slot, int func)
205 {
206 }
207 #endif
208
209 #define BCM4331_MMIO_SIZE       16384
210 #define BCM4331_PM_CAP          0x40
211 #define bcma_aread32(reg)       ioread32(mmio + 1 * BCMA_CORE_SIZE + reg)
212 #define bcma_awrite32(reg, val) iowrite32(val, mmio + 1 * BCMA_CORE_SIZE + reg)
213
214 static void __init apple_airport_reset(int bus, int slot, int func)
215 {
216         void __iomem *mmio;
217         u16 pmcsr;
218         u64 addr;
219         int i;
220
221         if (!dmi_match(DMI_SYS_VENDOR, "Apple Inc."))
222                 return;
223
224         /* Card may have been put into PCI_D3hot by grub quirk */
225         pmcsr = read_pci_config_16(bus, slot, func, BCM4331_PM_CAP + PCI_PM_CTRL);
226
227         if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != PCI_D0) {
228                 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
229                 write_pci_config_16(bus, slot, func, BCM4331_PM_CAP + PCI_PM_CTRL, pmcsr);
230                 mdelay(10);
231
232                 pmcsr = read_pci_config_16(bus, slot, func, BCM4331_PM_CAP + PCI_PM_CTRL);
233                 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != PCI_D0) {
234                         dev_err("Cannot power up Apple AirPort card\n");
235                         return;
236                 }
237         }
238
239         addr  =      read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
240         addr |= (u64)read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_1) << 32;
241         addr &= PCI_BASE_ADDRESS_MEM_MASK;
242
243         mmio = early_ioremap(addr, BCM4331_MMIO_SIZE);
244         if (!mmio) {
245                 dev_err("Cannot iomap Apple AirPort card\n");
246                 return;
247         }
248
249         pr_info("Resetting Apple AirPort card (left enabled by EFI)\n");
250
251         for (i = 0; bcma_aread32(BCMA_RESET_ST) && i < 30; i++)
252                 udelay(10);
253
254         bcma_awrite32(BCMA_RESET_CTL, BCMA_RESET_CTL_RESET);
255         bcma_aread32(BCMA_RESET_CTL);
256         udelay(1);
257
258         bcma_awrite32(BCMA_RESET_CTL, 0);
259         bcma_aread32(BCMA_RESET_CTL);
260         udelay(10);
261
262         early_iounmap(mmio, BCM4331_MMIO_SIZE);
263 }
264
265 #define QFLAG_APPLY_ONCE        0x1
266 #define QFLAG_APPLIED           0x2
267 #define QFLAG_DONE              (QFLAG_APPLY_ONCE|QFLAG_APPLIED)
268 struct chipset {
269         u32 vendor;
270         u32 device;
271         u32 class;
272         u32 class_mask;
273         u32 flags;
274         void (*f)(int num, int slot, int func);
275 };
276
277 static struct chipset early_qrk[] __initdata = {
278         { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
279           PCI_CLASS_BRIDGE_PCI, PCI_ANY_ID, QFLAG_APPLY_ONCE, nvidia_bugs },
280         { PCI_VENDOR_ID_VIA, PCI_ANY_ID,
281           PCI_CLASS_BRIDGE_PCI, PCI_ANY_ID, QFLAG_APPLY_ONCE, via_bugs },
282         { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB,
283           PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, fix_hypertransport_config },
284         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS,
285           PCI_CLASS_SERIAL_SMBUS, PCI_ANY_ID, 0, ati_bugs },
286         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS,
287           PCI_CLASS_SERIAL_SMBUS, PCI_ANY_ID, 0, ati_bugs_contd },
288         { PCI_VENDOR_ID_BROADCOM, 0x4331,
289           PCI_CLASS_NETWORK_OTHER, PCI_ANY_ID, 0, apple_airport_reset},
290         {}
291 };
292
293 static void __init early_pci_scan_bus(int bus);
294
295 /**
296  * check_dev_quirk - apply early quirks to a given PCI device
297  * @num: bus number
298  * @slot: slot number
299  * @func: PCI function
300  *
301  * Check the vendor & device ID against the early quirks table.
302  *
303  * If the device is single function, let early_pci_scan_bus() know so we don't
304  * poke at this device again.
305  */
306 static int __init check_dev_quirk(int num, int slot, int func)
307 {
308         u16 class;
309         u16 vendor;
310         u16 device;
311         u8 type;
312         u8 sec;
313         int i;
314
315         class = read_pci_config_16(num, slot, func, PCI_CLASS_DEVICE);
316
317         if (class == 0xffff)
318                 return -1; /* no class, treat as single function */
319
320         vendor = read_pci_config_16(num, slot, func, PCI_VENDOR_ID);
321
322         device = read_pci_config_16(num, slot, func, PCI_DEVICE_ID);
323
324         for (i = 0; early_qrk[i].f != NULL; i++) {
325                 if (((early_qrk[i].vendor == PCI_ANY_ID) ||
326                         (early_qrk[i].vendor == vendor)) &&
327                         ((early_qrk[i].device == PCI_ANY_ID) ||
328                         (early_qrk[i].device == device)) &&
329                         (!((early_qrk[i].class ^ class) &
330                             early_qrk[i].class_mask))) {
331                                 if ((early_qrk[i].flags &
332                                      QFLAG_DONE) != QFLAG_DONE)
333                                         early_qrk[i].f(num, slot, func);
334                                 early_qrk[i].flags |= QFLAG_APPLIED;
335                         }
336         }
337
338         type = read_pci_config_byte(num, slot, func,
339                                     PCI_HEADER_TYPE);
340
341         if ((type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
342                 sec = read_pci_config_byte(num, slot, func, PCI_SECONDARY_BUS);
343                 if (sec > num)
344                         early_pci_scan_bus(sec);
345         }
346
347         if (!(type & 0x80))
348                 return -1;
349
350         return 0;
351 }
352
353 static void __init early_pci_scan_bus(int bus)
354 {
355         int slot, func;
356
357         /* Poor man's PCI discovery */
358         for (slot = 0; slot < 32; slot++)
359                 for (func = 0; func < 8; func++) {
360                         /* Only probe function 0 on single fn devices */
361                         if (check_dev_quirk(bus, slot, func))
362                                 break;
363                 }
364 }
365
366 void __init early_quirks(void)
367 {
368         if (!early_pci_allowed())
369                 return;
370
371         early_pci_scan_bus(0);
372 }