]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/early-quirks.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / early-quirks.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
dfa4698c
AK
2/* Various workarounds for chipset bugs.
3 This code runs very early and can't use the regular PCI subsystem
4 The entries are keyed to PCI bridges which usually identify chipsets
5 uniquely.
6 This is only for whole classes of chipsets with specific problems which
7 need early invasive action (e.g. before the timers are initialized).
8 Most PCI device specific workarounds can be done later and should be
9 in standard PCI quirks
10 Mainboard specific bugs should be handled by DMI entries.
11 CPU specific bugs in setup.c */
12
13#include <linux/pci.h>
14#include <linux/acpi.h>
abb2bafd 15#include <linux/delay.h>
dfa4698c 16#include <linux/pci_ids.h>
abb2bafd
LW
17#include <linux/bcma/bcma.h>
18#include <linux/bcma/bcma_regs.h>
630b3aff 19#include <linux/platform_data/x86/apple.h>
814c5f1f 20#include <drm/i915_drm.h>
dfa4698c 21#include <asm/pci-direct.h>
dfa4698c 22#include <asm/dma.h>
54ef3400
AK
23#include <asm/io_apic.h>
24#include <asm/apic.h>
62187910 25#include <asm/hpet.h>
46a7fa27 26#include <asm/iommu.h>
1d9b16d1 27#include <asm/gart.h>
03bbcb2e 28#include <asm/irq_remapping.h>
abb2bafd
LW
29#include <asm/early_ioremap.h>
30
7fc875e6
GP
31static void __init early_pci_clear_msi(int bus, int slot, int func)
32{
33 int pos;
34 u16 ctrl;
35
36 if (likely(!pci_early_clear_msi))
37 return;
38
39 pr_info_once("Clearing MSI/MSI-X enable bits early in boot (quirk)\n");
40
41 pos = pci_early_find_cap(bus, slot, func, PCI_CAP_ID_MSI);
42 if (pos) {
43 ctrl = read_pci_config_16(bus, slot, func, pos + PCI_MSI_FLAGS);
44 ctrl &= ~PCI_MSI_FLAGS_ENABLE;
45 write_pci_config_16(bus, slot, func, pos + PCI_MSI_FLAGS, ctrl);
46
47 /* Read again to flush previous write */
48 ctrl = read_pci_config_16(bus, slot, func, pos + PCI_MSI_FLAGS);
49 }
50
51 pos = pci_early_find_cap(bus, slot, func, PCI_CAP_ID_MSIX);
52 if (pos) {
53 ctrl = read_pci_config_16(bus, slot, func, pos + PCI_MSIX_FLAGS);
54 ctrl &= ~PCI_MSIX_FLAGS_ENABLE;
55 write_pci_config_16(bus, slot, func, pos + PCI_MSIX_FLAGS, ctrl);
56
57 /* Read again to flush previous write */
58 ctrl = read_pci_config_16(bus, slot, func, pos + PCI_MSIX_FLAGS);
59 }
60}
61
abb2bafd 62#define dev_err(msg) pr_err("pci 0000:%02x:%02x.%d: %s", bus, slot, func, msg)
dfa4698c 63
c6b48324
NH
64static void __init fix_hypertransport_config(int num, int slot, int func)
65{
66 u32 htcfg;
67 /*
68 * we found a hypertransport bus
69 * make sure that we are broadcasting
70 * interrupts to all cpus on the ht bus
71 * if we're using extended apic ids
72 */
73 htcfg = read_pci_config(num, slot, func, 0x68);
74 if (htcfg & (1 << 18)) {
7bcbc78d
NH
75 printk(KERN_INFO "Detected use of extended apic ids "
76 "on hypertransport bus\n");
c6b48324 77 if ((htcfg & (1 << 17)) == 0) {
7bcbc78d
NH
78 printk(KERN_INFO "Enabling hypertransport extended "
79 "apic interrupt broadcast\n");
80 printk(KERN_INFO "Note this is a bios bug, "
81 "please contact your hw vendor\n");
c6b48324
NH
82 htcfg |= (1 << 17);
83 write_pci_config(num, slot, func, 0x68, htcfg);
84 }
85 }
86
87
88}
89
90static void __init via_bugs(int num, int slot, int func)
dfa4698c 91{
966396d3 92#ifdef CONFIG_GART_IOMMU
c987d12f 93 if ((max_pfn > MAX_DMA32_PFN || force_iommu) &&
0440d4c0 94 !gart_iommu_aperture_allowed) {
dfa4698c 95 printk(KERN_INFO
54ef3400
AK
96 "Looks like a VIA chipset. Disabling IOMMU."
97 " Override with iommu=allowed\n");
0440d4c0 98 gart_iommu_aperture_disabled = 1;
dfa4698c
AK
99 }
100#endif
101}
102
103#ifdef CONFIG_ACPI
03d0d20e 104#ifdef CONFIG_X86_IO_APIC
dfa4698c 105
15a58ed1 106static int __init nvidia_hpet_check(struct acpi_table_header *header)
dfa4698c 107{
dfa4698c
AK
108 return 0;
109}
03d0d20e
JG
110#endif /* CONFIG_X86_IO_APIC */
111#endif /* CONFIG_ACPI */
dfa4698c 112
c6b48324 113static void __init nvidia_bugs(int num, int slot, int func)
dfa4698c
AK
114{
115#ifdef CONFIG_ACPI
54ef3400 116#ifdef CONFIG_X86_IO_APIC
447d29d1
LW
117 /*
118 * Only applies to Nvidia root ports (bus 0) and not to
119 * Nvidia graphics cards with PCI ports on secondary buses.
120 */
121 if (num)
122 return;
123
dfa4698c
AK
124 /*
125 * All timer overrides on Nvidia are
126 * wrong unless HPET is enabled.
fa18f477
AK
127 * Unfortunately that's not true on many Asus boards.
128 * We don't know yet how to detect this automatically, but
129 * at least allow a command line override.
dfa4698c 130 */
fa18f477
AK
131 if (acpi_use_timer_override)
132 return;
133
fe699336 134 if (acpi_table_parse(ACPI_SIG_HPET, nvidia_hpet_check)) {
dfa4698c
AK
135 acpi_skip_timer_override = 1;
136 printk(KERN_INFO "Nvidia board "
137 "detected. Ignoring ACPI "
138 "timer override.\n");
fa18f477
AK
139 printk(KERN_INFO "If you got timer trouble "
140 "try acpi_use_timer_override\n");
dfa4698c 141 }
54ef3400 142#endif
dfa4698c
AK
143#endif
144 /* RED-PEN skip them on mptables too? */
145
146}
147
26adcfbf
AH
148#if defined(CONFIG_ACPI) && defined(CONFIG_X86_IO_APIC)
149static u32 __init ati_ixp4x0_rev(int num, int slot, int func)
33fb0e4e
AH
150{
151 u32 d;
152 u8 b;
153
154 b = read_pci_config_byte(num, slot, func, 0xac);
155 b &= ~(1<<5);
156 write_pci_config_byte(num, slot, func, 0xac, b);
157
158 d = read_pci_config(num, slot, func, 0x70);
159 d |= 1<<8;
160 write_pci_config(num, slot, func, 0x70, d);
161
162 d = read_pci_config(num, slot, func, 0x8);
163 d &= 0xff;
164 return d;
165}
166
167static void __init ati_bugs(int num, int slot, int func)
168{
33fb0e4e
AH
169 u32 d;
170 u8 b;
171
172 if (acpi_use_timer_override)
173 return;
174
175 d = ati_ixp4x0_rev(num, slot, func);
176 if (d < 0x82)
177 acpi_skip_timer_override = 1;
178 else {
179 /* check for IRQ0 interrupt swap */
180 outb(0x72, 0xcd6); b = inb(0xcd7);
181 if (!(b & 0x2))
182 acpi_skip_timer_override = 1;
183 }
184
185 if (acpi_skip_timer_override) {
186 printk(KERN_INFO "SB4X0 revision 0x%x\n", d);
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 }
33fb0e4e
AH
191}
192
26adcfbf
AH
193static u32 __init ati_sbx00_rev(int num, int slot, int func)
194{
7f74f8f2 195 u32 d;
26adcfbf 196
26adcfbf
AH
197 d = read_pci_config(num, slot, func, 0x8);
198 d &= 0xff;
26adcfbf
AH
199
200 return d;
201}
202
203static void __init ati_bugs_contd(int num, int slot, int func)
204{
205 u32 d, rev;
206
26adcfbf 207 rev = ati_sbx00_rev(num, slot, func);
7f74f8f2
AH
208 if (rev >= 0x40)
209 acpi_fix_pin2_polarity = 1;
210
1d3e09a3
AH
211 /*
212 * SB600: revisions 0x11, 0x12, 0x13, 0x14, ...
213 * SB700: revisions 0x39, 0x3a, ...
214 * SB800: revisions 0x40, 0x41, ...
215 */
216 if (rev >= 0x39)
26adcfbf
AH
217 return;
218
7f74f8f2
AH
219 if (acpi_use_timer_override)
220 return;
221
26adcfbf
AH
222 /* check for IRQ0 interrupt swap */
223 d = read_pci_config(num, slot, func, 0x64);
224 if (!(d & (1<<14)))
225 acpi_skip_timer_override = 1;
226
227 if (acpi_skip_timer_override) {
228 printk(KERN_INFO "SB600 revision 0x%x\n", rev);
229 printk(KERN_INFO "Ignoring ACPI timer override.\n");
230 printk(KERN_INFO "If you got timer trouble "
231 "try acpi_use_timer_override\n");
232 }
233}
234#else
235static void __init ati_bugs(int num, int slot, int func)
236{
237}
238
239static void __init ati_bugs_contd(int num, int slot, int func)
240{
241}
242#endif
243
03bbcb2e
NH
244static void __init intel_remapping_check(int num, int slot, int func)
245{
246 u8 revision;
803075db 247 u16 device;
03bbcb2e 248
803075db 249 device = read_pci_config_16(num, slot, func, PCI_DEVICE_ID);
03bbcb2e
NH
250 revision = read_pci_config_byte(num, slot, func, PCI_REVISION_ID);
251
252 /*
6f8a1b33
NH
253 * Revision <= 13 of all triggering devices id in this quirk
254 * have a problem draining interrupts when irq remapping is
255 * enabled, and should be flagged as broken. Additionally
256 * revision 0x22 of device id 0x3405 has this problem.
03bbcb2e 257 */
6f8a1b33 258 if (revision <= 0x13)
03bbcb2e 259 set_irq_remapping_broken();
6f8a1b33 260 else if (device == 0x3405 && revision == 0x22)
803075db 261 set_irq_remapping_broken();
03bbcb2e
NH
262}
263
814c5f1f
JB
264/*
265 * Systems with Intel graphics controllers set aside memory exclusively
266 * for gfx driver use. This memory is not marked in the E820 as reserved
267 * or as RAM, and so is subject to overlap from E820 manipulation later
268 * in the boot process. On some systems, MMIO space is allocated on top,
269 * despite the efforts of the "RAM buffer" approach, which simply rounds
270 * memory boundaries up to 64M to try to catch space that may decode
271 * as RAM and so is not suitable for MMIO.
814c5f1f 272 */
814c5f1f 273
86e58762 274#define KB(x) ((x) * 1024UL)
814c5f1f 275#define MB(x) (KB (KB (x)))
814c5f1f 276
a4dff769
VS
277static size_t __init i830_tseg_size(void)
278{
c0dd3460 279 u8 esmramc = read_pci_config_byte(0, 0, 0, I830_ESMRAMC);
a4dff769 280
c0dd3460 281 if (!(esmramc & TSEG_ENABLE))
a4dff769
VS
282 return 0;
283
c0dd3460 284 if (esmramc & I830_TSEG_SIZE_1M)
a4dff769
VS
285 return MB(1);
286 else
287 return KB(512);
288}
289
290static size_t __init i845_tseg_size(void)
291{
c0dd3460
JL
292 u8 esmramc = read_pci_config_byte(0, 0, 0, I845_ESMRAMC);
293 u8 tseg_size = esmramc & I845_TSEG_SIZE_MASK;
a4dff769 294
c0dd3460 295 if (!(esmramc & TSEG_ENABLE))
a4dff769
VS
296 return 0;
297
c0dd3460
JL
298 switch (tseg_size) {
299 case I845_TSEG_SIZE_512K: return KB(512);
300 case I845_TSEG_SIZE_1M: return MB(1);
a4dff769 301 default:
c0dd3460 302 WARN(1, "Unknown ESMRAMC value: %x!\n", esmramc);
a4dff769 303 }
c0dd3460 304 return 0;
a4dff769
VS
305}
306
307static size_t __init i85x_tseg_size(void)
308{
c0dd3460 309 u8 esmramc = read_pci_config_byte(0, 0, 0, I85X_ESMRAMC);
a4dff769 310
c0dd3460 311 if (!(esmramc & TSEG_ENABLE))
a4dff769
VS
312 return 0;
313
314 return MB(1);
315}
316
317static size_t __init i830_mem_size(void)
318{
319 return read_pci_config_byte(0, 0, 0, I830_DRB3) * MB(32);
320}
321
322static size_t __init i85x_mem_size(void)
323{
324 return read_pci_config_byte(0, 0, 1, I85X_DRB3) * MB(32);
325}
326
327/*
328 * On 830/845/85x the stolen memory base isn't available in any
329 * register. We need to calculate it as TOM-TSEG_SIZE-stolen_size.
330 */
c0dd3460
JL
331static phys_addr_t __init i830_stolen_base(int num, int slot, int func,
332 size_t stolen_size)
a4dff769 333{
c0dd3460 334 return (phys_addr_t)i830_mem_size() - i830_tseg_size() - stolen_size;
a4dff769
VS
335}
336
c0dd3460
JL
337static phys_addr_t __init i845_stolen_base(int num, int slot, int func,
338 size_t stolen_size)
a4dff769 339{
c0dd3460 340 return (phys_addr_t)i830_mem_size() - i845_tseg_size() - stolen_size;
a4dff769
VS
341}
342
c0dd3460
JL
343static phys_addr_t __init i85x_stolen_base(int num, int slot, int func,
344 size_t stolen_size)
a4dff769 345{
c0dd3460 346 return (phys_addr_t)i85x_mem_size() - i85x_tseg_size() - stolen_size;
a4dff769
VS
347}
348
c0dd3460
JL
349static phys_addr_t __init i865_stolen_base(int num, int slot, int func,
350 size_t stolen_size)
a4dff769 351{
d721b02f 352 u16 toud = 0;
c0dd3460 353
c0dd3460
JL
354 toud = read_pci_config_16(0, 0, 0, I865_TOUD);
355
d721b02f 356 return (phys_addr_t)(toud << 16) + i845_tseg_size();
c0dd3460
JL
357}
358
359static phys_addr_t __init gen3_stolen_base(int num, int slot, int func,
360 size_t stolen_size)
361{
362 u32 bsm;
363
364 /* Almost universally we can find the Graphics Base of Stolen Memory
365 * at register BSM (0x5c) in the igfx configuration space. On a few
366 * (desktop) machines this is also mirrored in the bridge device at
367 * different locations, or in the MCHBAR.
368 */
369 bsm = read_pci_config(num, slot, func, INTEL_BSM);
370
371 return (phys_addr_t)bsm & INTEL_BSM_MASK;
a4dff769
VS
372}
373
374static size_t __init i830_stolen_size(int num, int slot, int func)
375{
a4dff769 376 u16 gmch_ctrl;
c0dd3460 377 u16 gms;
a4dff769
VS
378
379 gmch_ctrl = read_pci_config_16(0, 0, 0, I830_GMCH_CTRL);
c0dd3460
JL
380 gms = gmch_ctrl & I830_GMCH_GMS_MASK;
381
382 switch (gms) {
383 case I830_GMCH_GMS_STOLEN_512: return KB(512);
384 case I830_GMCH_GMS_STOLEN_1024: return MB(1);
385 case I830_GMCH_GMS_STOLEN_8192: return MB(8);
386 /* local memory isn't part of the normal address space */
387 case I830_GMCH_GMS_LOCAL: return 0;
a4dff769 388 default:
c0dd3460 389 WARN(1, "Unknown GMCH_CTRL value: %x!\n", gmch_ctrl);
a4dff769
VS
390 }
391
c0dd3460 392 return 0;
a4dff769
VS
393}
394
814c5f1f
JB
395static size_t __init gen3_stolen_size(int num, int slot, int func)
396{
814c5f1f 397 u16 gmch_ctrl;
c0dd3460 398 u16 gms;
814c5f1f
JB
399
400 gmch_ctrl = read_pci_config_16(0, 0, 0, I830_GMCH_CTRL);
c0dd3460
JL
401 gms = gmch_ctrl & I855_GMCH_GMS_MASK;
402
403 switch (gms) {
404 case I855_GMCH_GMS_STOLEN_1M: return MB(1);
405 case I855_GMCH_GMS_STOLEN_4M: return MB(4);
406 case I855_GMCH_GMS_STOLEN_8M: return MB(8);
407 case I855_GMCH_GMS_STOLEN_16M: return MB(16);
408 case I855_GMCH_GMS_STOLEN_32M: return MB(32);
409 case I915_GMCH_GMS_STOLEN_48M: return MB(48);
410 case I915_GMCH_GMS_STOLEN_64M: return MB(64);
411 case G33_GMCH_GMS_STOLEN_128M: return MB(128);
412 case G33_GMCH_GMS_STOLEN_256M: return MB(256);
413 case INTEL_GMCH_GMS_STOLEN_96M: return MB(96);
414 case INTEL_GMCH_GMS_STOLEN_160M:return MB(160);
415 case INTEL_GMCH_GMS_STOLEN_224M:return MB(224);
416 case INTEL_GMCH_GMS_STOLEN_352M:return MB(352);
814c5f1f 417 default:
c0dd3460 418 WARN(1, "Unknown GMCH_CTRL value: %x!\n", gmch_ctrl);
814c5f1f
JB
419 }
420
c0dd3460 421 return 0;
814c5f1f
JB
422}
423
424static size_t __init gen6_stolen_size(int num, int slot, int func)
425{
426 u16 gmch_ctrl;
c0dd3460 427 u16 gms;
814c5f1f
JB
428
429 gmch_ctrl = read_pci_config_16(num, slot, func, SNB_GMCH_CTRL);
c0dd3460 430 gms = (gmch_ctrl >> SNB_GMCH_GMS_SHIFT) & SNB_GMCH_GMS_MASK;
814c5f1f 431
c0dd3460 432 return (size_t)gms * MB(32);
814c5f1f
JB
433}
434
36dfcea4 435static size_t __init gen8_stolen_size(int num, int slot, int func)
9459d252
BW
436{
437 u16 gmch_ctrl;
c0dd3460 438 u16 gms;
9459d252
BW
439
440 gmch_ctrl = read_pci_config_16(num, slot, func, SNB_GMCH_CTRL);
c0dd3460
JL
441 gms = (gmch_ctrl >> BDW_GMCH_GMS_SHIFT) & BDW_GMCH_GMS_MASK;
442
443 return (size_t)gms * MB(32);
9459d252
BW
444}
445
3e3b2c39
DL
446static size_t __init chv_stolen_size(int num, int slot, int func)
447{
448 u16 gmch_ctrl;
c0dd3460 449 u16 gms;
3e3b2c39
DL
450
451 gmch_ctrl = read_pci_config_16(num, slot, func, SNB_GMCH_CTRL);
c0dd3460 452 gms = (gmch_ctrl >> SNB_GMCH_GMS_SHIFT) & SNB_GMCH_GMS_MASK;
3e3b2c39
DL
453
454 /*
455 * 0x0 to 0x10: 32MB increments starting at 0MB
456 * 0x11 to 0x16: 4MB increments starting at 8MB
457 * 0x17 to 0x1d: 4MB increments start at 36MB
458 */
c0dd3460
JL
459 if (gms < 0x11)
460 return (size_t)gms * MB(32);
461 else if (gms < 0x17)
462 return (size_t)(gms - 0x11 + 2) * MB(4);
3e3b2c39 463 else
c0dd3460 464 return (size_t)(gms - 0x17 + 9) * MB(4);
3e3b2c39 465}
52ca7045 466
66375014
DL
467static size_t __init gen9_stolen_size(int num, int slot, int func)
468{
469 u16 gmch_ctrl;
c0dd3460 470 u16 gms;
66375014
DL
471
472 gmch_ctrl = read_pci_config_16(num, slot, func, SNB_GMCH_CTRL);
c0dd3460 473 gms = (gmch_ctrl >> BDW_GMCH_GMS_SHIFT) & BDW_GMCH_GMS_MASK;
66375014 474
c0dd3460
JL
475 /* 0x0 to 0xef: 32MB increments starting at 0MB */
476 /* 0xf0 to 0xfe: 4MB increments starting at 4MB */
477 if (gms < 0xf0)
478 return (size_t)gms * MB(32);
66375014 479 else
c0dd3460 480 return (size_t)(gms - 0xf0 + 1) * MB(4);
66375014
DL
481}
482
ee0629cf
JL
483struct intel_early_ops {
484 size_t (*stolen_size)(int num, int slot, int func);
485 phys_addr_t (*stolen_base)(int num, int slot, int func, size_t size);
486};
66375014 487
ee0629cf
JL
488static const struct intel_early_ops i830_early_ops __initconst = {
489 .stolen_base = i830_stolen_base,
490 .stolen_size = i830_stolen_size,
a4dff769
VS
491};
492
ee0629cf
JL
493static const struct intel_early_ops i845_early_ops __initconst = {
494 .stolen_base = i845_stolen_base,
495 .stolen_size = i830_stolen_size,
a4dff769
VS
496};
497
ee0629cf
JL
498static const struct intel_early_ops i85x_early_ops __initconst = {
499 .stolen_base = i85x_stolen_base,
500 .stolen_size = gen3_stolen_size,
a4dff769
VS
501};
502
ee0629cf
JL
503static const struct intel_early_ops i865_early_ops __initconst = {
504 .stolen_base = i865_stolen_base,
505 .stolen_size = gen3_stolen_size,
a4dff769
VS
506};
507
ee0629cf
JL
508static const struct intel_early_ops gen3_early_ops __initconst = {
509 .stolen_base = gen3_stolen_base,
510 .stolen_size = gen3_stolen_size,
52ca7045
VS
511};
512
ee0629cf
JL
513static const struct intel_early_ops gen6_early_ops __initconst = {
514 .stolen_base = gen3_stolen_base,
515 .stolen_size = gen6_stolen_size,
52ca7045
VS
516};
517
ee0629cf
JL
518static const struct intel_early_ops gen8_early_ops __initconst = {
519 .stolen_base = gen3_stolen_base,
520 .stolen_size = gen8_stolen_size,
52ca7045 521};
814c5f1f 522
ee0629cf
JL
523static const struct intel_early_ops gen9_early_ops __initconst = {
524 .stolen_base = gen3_stolen_base,
525 .stolen_size = gen9_stolen_size,
66375014
DL
526};
527
ee0629cf
JL
528static const struct intel_early_ops chv_early_ops __initconst = {
529 .stolen_base = gen3_stolen_base,
530 .stolen_size = chv_stolen_size,
3e3b2c39
DL
531};
532
ee0629cf
JL
533static const struct pci_device_id intel_early_ids[] __initconst = {
534 INTEL_I830_IDS(&i830_early_ops),
535 INTEL_I845G_IDS(&i845_early_ops),
536 INTEL_I85X_IDS(&i85x_early_ops),
537 INTEL_I865G_IDS(&i865_early_ops),
538 INTEL_I915G_IDS(&gen3_early_ops),
539 INTEL_I915GM_IDS(&gen3_early_ops),
540 INTEL_I945G_IDS(&gen3_early_ops),
541 INTEL_I945GM_IDS(&gen3_early_ops),
8d9c20e1 542 INTEL_VLV_IDS(&gen6_early_ops),
ee0629cf
JL
543 INTEL_PINEVIEW_IDS(&gen3_early_ops),
544 INTEL_I965G_IDS(&gen3_early_ops),
545 INTEL_G33_IDS(&gen3_early_ops),
546 INTEL_I965GM_IDS(&gen3_early_ops),
547 INTEL_GM45_IDS(&gen3_early_ops),
548 INTEL_G45_IDS(&gen3_early_ops),
549 INTEL_IRONLAKE_D_IDS(&gen3_early_ops),
550 INTEL_IRONLAKE_M_IDS(&gen3_early_ops),
551 INTEL_SNB_D_IDS(&gen6_early_ops),
552 INTEL_SNB_M_IDS(&gen6_early_ops),
553 INTEL_IVB_M_IDS(&gen6_early_ops),
554 INTEL_IVB_D_IDS(&gen6_early_ops),
8d9c20e1
CS
555 INTEL_HSW_IDS(&gen6_early_ops),
556 INTEL_BDW_IDS(&gen8_early_ops),
ee0629cf
JL
557 INTEL_CHV_IDS(&chv_early_ops),
558 INTEL_SKL_IDS(&gen9_early_ops),
559 INTEL_BXT_IDS(&gen9_early_ops),
560 INTEL_KBL_IDS(&gen9_early_ops),
3b95301d 561 INTEL_CFL_IDS(&gen9_early_ops),
bc384c77 562 INTEL_GLK_IDS(&gen9_early_ops),
2e1e9d48 563 INTEL_CNL_IDS(&gen9_early_ops),
814c5f1f
JB
564};
565
ee0629cf
JL
566static void __init
567intel_graphics_stolen(int num, int slot, int func,
568 const struct intel_early_ops *early_ops)
814c5f1f 569{
01e5d3b4 570 phys_addr_t base, end;
814c5f1f 571 size_t size;
ee0629cf
JL
572
573 size = early_ops->stolen_size(num, slot, func);
574 base = early_ops->stolen_base(num, slot, func, size);
575
576 if (!size || !base)
577 return;
578
01e5d3b4
CW
579 end = base + size - 1;
580 printk(KERN_INFO "Reserving Intel graphics memory at %pa-%pa\n",
581 &base, &end);
ee0629cf
JL
582
583 /* Mark this space as reserved */
09821ff1 584 e820__range_add(base, size, E820_TYPE_RESERVED);
f9748fa0 585 e820__update_table(e820_table);
ee0629cf
JL
586}
587
588static void __init intel_graphics_quirks(int num, int slot, int func)
589{
590 const struct intel_early_ops *early_ops;
591 u16 device;
814c5f1f 592 int i;
814c5f1f
JB
593
594 device = read_pci_config_16(num, slot, func, PCI_DEVICE_ID);
ee0629cf
JL
595
596 for (i = 0; i < ARRAY_SIZE(intel_early_ids); i++) {
597 kernel_ulong_t driver_data = intel_early_ids[i].driver_data;
598
599 if (intel_early_ids[i].device != device)
600 continue;
601
602 early_ops = (typeof(early_ops))driver_data;
603
604 intel_graphics_stolen(num, slot, func, early_ops);
605
606 return;
814c5f1f
JB
607 }
608}
609
62187910
FT
610static void __init force_disable_hpet(int num, int slot, int func)
611{
612#ifdef CONFIG_HPET_TIMER
3d45ac4b 613 boot_hpet_disable = true;
62187910
FT
614 pr_info("x86/hpet: Will disable the HPET for this platform because it's not reliable\n");
615#endif
616}
617
abb2bafd
LW
618#define BCM4331_MMIO_SIZE 16384
619#define BCM4331_PM_CAP 0x40
620#define bcma_aread32(reg) ioread32(mmio + 1 * BCMA_CORE_SIZE + reg)
621#define bcma_awrite32(reg, val) iowrite32(val, mmio + 1 * BCMA_CORE_SIZE + reg)
622
623static void __init apple_airport_reset(int bus, int slot, int func)
624{
625 void __iomem *mmio;
626 u16 pmcsr;
627 u64 addr;
628 int i;
629
630b3aff 630 if (!x86_apple_machine)
abb2bafd
LW
631 return;
632
633 /* Card may have been put into PCI_D3hot by grub quirk */
634 pmcsr = read_pci_config_16(bus, slot, func, BCM4331_PM_CAP + PCI_PM_CTRL);
635
636 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != PCI_D0) {
637 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
638 write_pci_config_16(bus, slot, func, BCM4331_PM_CAP + PCI_PM_CTRL, pmcsr);
639 mdelay(10);
640
641 pmcsr = read_pci_config_16(bus, slot, func, BCM4331_PM_CAP + PCI_PM_CTRL);
642 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != PCI_D0) {
643 dev_err("Cannot power up Apple AirPort card\n");
644 return;
645 }
646 }
647
648 addr = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
649 addr |= (u64)read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_1) << 32;
650 addr &= PCI_BASE_ADDRESS_MEM_MASK;
651
652 mmio = early_ioremap(addr, BCM4331_MMIO_SIZE);
653 if (!mmio) {
654 dev_err("Cannot iomap Apple AirPort card\n");
655 return;
656 }
657
658 pr_info("Resetting Apple AirPort card (left enabled by EFI)\n");
659
660 for (i = 0; bcma_aread32(BCMA_RESET_ST) && i < 30; i++)
661 udelay(10);
662
663 bcma_awrite32(BCMA_RESET_CTL, BCMA_RESET_CTL_RESET);
664 bcma_aread32(BCMA_RESET_CTL);
665 udelay(1);
666
667 bcma_awrite32(BCMA_RESET_CTL, 0);
668 bcma_aread32(BCMA_RESET_CTL);
669 udelay(10);
670
671 early_iounmap(mmio, BCM4331_MMIO_SIZE);
672}
62187910 673
c6b48324
NH
674#define QFLAG_APPLY_ONCE 0x1
675#define QFLAG_APPLIED 0x2
676#define QFLAG_DONE (QFLAG_APPLY_ONCE|QFLAG_APPLIED)
dfa4698c 677struct chipset {
c6b48324
NH
678 u32 vendor;
679 u32 device;
680 u32 class;
681 u32 class_mask;
682 u32 flags;
683 void (*f)(int num, int slot, int func);
dfa4698c
AK
684};
685
c993c735 686static struct chipset early_qrk[] __initdata = {
c6b48324
NH
687 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
688 PCI_CLASS_BRIDGE_PCI, PCI_ANY_ID, QFLAG_APPLY_ONCE, nvidia_bugs },
689 { PCI_VENDOR_ID_VIA, PCI_ANY_ID,
690 PCI_CLASS_BRIDGE_PCI, PCI_ANY_ID, QFLAG_APPLY_ONCE, via_bugs },
c6b48324
NH
691 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB,
692 PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, fix_hypertransport_config },
33fb0e4e
AH
693 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS,
694 PCI_CLASS_SERIAL_SMBUS, PCI_ANY_ID, 0, ati_bugs },
26adcfbf
AH
695 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS,
696 PCI_CLASS_SERIAL_SMBUS, PCI_ANY_ID, 0, ati_bugs_contd },
03bbcb2e
NH
697 { PCI_VENDOR_ID_INTEL, 0x3403, PCI_CLASS_BRIDGE_HOST,
698 PCI_BASE_CLASS_BRIDGE, 0, intel_remapping_check },
803075db
NH
699 { PCI_VENDOR_ID_INTEL, 0x3405, PCI_CLASS_BRIDGE_HOST,
700 PCI_BASE_CLASS_BRIDGE, 0, intel_remapping_check },
03bbcb2e
NH
701 { PCI_VENDOR_ID_INTEL, 0x3406, PCI_CLASS_BRIDGE_HOST,
702 PCI_BASE_CLASS_BRIDGE, 0, intel_remapping_check },
814c5f1f 703 { PCI_VENDOR_ID_INTEL, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, PCI_ANY_ID,
ee0629cf 704 QFLAG_APPLY_ONCE, intel_graphics_quirks },
62187910 705 /*
b58d9307
FT
706 * HPET on the current version of the Baytrail platform has accuracy
707 * problems: it will halt in deep idle state - so we disable it.
708 *
709 * More details can be found in section 18.10.1.3 of the datasheet:
710 *
711 * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/atom-z8000-datasheet-vol-1.pdf
62187910
FT
712 */
713 { PCI_VENDOR_ID_INTEL, 0x0f00,
714 PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, force_disable_hpet},
ecc71c9a
KHF
715 { PCI_VENDOR_ID_INTEL, 0x3e20,
716 PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, force_disable_hpet},
389723e9
KHF
717 { PCI_VENDOR_ID_INTEL, 0x3ec4,
718 PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, force_disable_hpet},
b69cbdd3
KHF
719 { PCI_VENDOR_ID_INTEL, 0x8a12,
720 PCI_CLASS_BRIDGE_HOST, PCI_ANY_ID, 0, force_disable_hpet},
abb2bafd
LW
721 { PCI_VENDOR_ID_BROADCOM, 0x4331,
722 PCI_CLASS_NETWORK_OTHER, PCI_ANY_ID, 0, apple_airport_reset},
7fc875e6 723 { PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, early_pci_clear_msi},
dfa4698c
AK
724 {}
725};
726
850c3210
LW
727static void __init early_pci_scan_bus(int bus);
728
15650a2f
JB
729/**
730 * check_dev_quirk - apply early quirks to a given PCI device
731 * @num: bus number
732 * @slot: slot number
733 * @func: PCI function
734 *
735 * Check the vendor & device ID against the early quirks table.
736 *
850c3210 737 * If the device is single function, let early_pci_scan_bus() know so we don't
15650a2f
JB
738 * poke at this device again.
739 */
740static int __init check_dev_quirk(int num, int slot, int func)
7bcbc78d
NH
741{
742 u16 class;
743 u16 vendor;
744 u16 device;
745 u8 type;
850c3210 746 u8 sec;
7bcbc78d
NH
747 int i;
748
749 class = read_pci_config_16(num, slot, func, PCI_CLASS_DEVICE);
750
751 if (class == 0xffff)
15650a2f 752 return -1; /* no class, treat as single function */
7bcbc78d
NH
753
754 vendor = read_pci_config_16(num, slot, func, PCI_VENDOR_ID);
755
756 device = read_pci_config_16(num, slot, func, PCI_DEVICE_ID);
757
758 for (i = 0; early_qrk[i].f != NULL; i++) {
759 if (((early_qrk[i].vendor == PCI_ANY_ID) ||
760 (early_qrk[i].vendor == vendor)) &&
761 ((early_qrk[i].device == PCI_ANY_ID) ||
762 (early_qrk[i].device == device)) &&
763 (!((early_qrk[i].class ^ class) &
764 early_qrk[i].class_mask))) {
765 if ((early_qrk[i].flags &
766 QFLAG_DONE) != QFLAG_DONE)
767 early_qrk[i].f(num, slot, func);
768 early_qrk[i].flags |= QFLAG_APPLIED;
769 }
770 }
771
772 type = read_pci_config_byte(num, slot, func,
773 PCI_HEADER_TYPE);
850c3210
LW
774
775 if ((type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
71b0bf77
GP
776 /* pci_early_clear_msi scans the buses differently. */
777 if (pci_early_clear_msi)
778 return -1;
779
850c3210
LW
780 sec = read_pci_config_byte(num, slot, func, PCI_SECONDARY_BUS);
781 if (sec > num)
782 early_pci_scan_bus(sec);
783 }
784
7bcbc78d 785 if (!(type & 0x80))
15650a2f
JB
786 return -1;
787
788 return 0;
7bcbc78d
NH
789}
790
850c3210 791static void __init early_pci_scan_bus(int bus)
dfa4698c 792{
8659c406 793 int slot, func;
0637a70a 794
dfa4698c 795 /* Poor man's PCI discovery */
8659c406
AK
796 for (slot = 0; slot < 32; slot++)
797 for (func = 0; func < 8; func++) {
798 /* Only probe function 0 on single fn devices */
850c3210 799 if (check_dev_quirk(bus, slot, func))
8659c406
AK
800 break;
801 }
dfa4698c 802}
850c3210
LW
803
804void __init early_quirks(void)
805{
71b0bf77
GP
806 int bus;
807
850c3210
LW
808 if (!early_pci_allowed())
809 return;
810
811 early_pci_scan_bus(0);
71b0bf77
GP
812 /* pci_early_clear_msi scans more buses. */
813 for (bus = 1; pci_early_clear_msi && bus < 256; bus++)
814 early_pci_scan_bus(bus);
850c3210 815}