Merge tag 'drm-misc-next-fixes-2023-09-01' of git://anongit.freedesktop.org/drm/drm...
[platform/kernel/linux-rpi.git] / arch / x86 / kernel / acpi / boot.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  boot.c - Architecture-Specific Low-Level ACPI Boot Support
4  *
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2001 Jun Nakajima <jun.nakajima@intel.com>
7  */
8 #define pr_fmt(fmt) "ACPI: " fmt
9
10 #include <linux/init.h>
11 #include <linux/acpi.h>
12 #include <linux/acpi_pmtmr.h>
13 #include <linux/efi.h>
14 #include <linux/cpumask.h>
15 #include <linux/export.h>
16 #include <linux/dmi.h>
17 #include <linux/irq.h>
18 #include <linux/slab.h>
19 #include <linux/memblock.h>
20 #include <linux/ioport.h>
21 #include <linux/pci.h>
22 #include <linux/efi-bgrt.h>
23 #include <linux/serial_core.h>
24 #include <linux/pgtable.h>
25
26 #include <asm/e820/api.h>
27 #include <asm/irqdomain.h>
28 #include <asm/pci_x86.h>
29 #include <asm/io_apic.h>
30 #include <asm/apic.h>
31 #include <asm/io.h>
32 #include <asm/mpspec.h>
33 #include <asm/smp.h>
34 #include <asm/i8259.h>
35 #include <asm/setup.h>
36
37 #include "sleep.h" /* To include x86_acpi_suspend_lowlevel */
38 static int __initdata acpi_force = 0;
39 int acpi_disabled;
40 EXPORT_SYMBOL(acpi_disabled);
41
42 #ifdef  CONFIG_X86_64
43 # include <asm/proto.h>
44 #endif                          /* X86 */
45
46 int acpi_noirq;                         /* skip ACPI IRQ initialization */
47 static int acpi_nobgrt;                 /* skip ACPI BGRT */
48 int acpi_pci_disabled;          /* skip ACPI PCI scan and IRQ initialization */
49 EXPORT_SYMBOL(acpi_pci_disabled);
50
51 int acpi_lapic;
52 int acpi_ioapic;
53 int acpi_strict;
54 int acpi_disable_cmcff;
55 bool acpi_int_src_ovr[NR_IRQS_LEGACY];
56
57 /* ACPI SCI override configuration */
58 u8 acpi_sci_flags __initdata;
59 u32 acpi_sci_override_gsi __initdata = INVALID_ACPI_IRQ;
60 int acpi_skip_timer_override __initdata;
61 int acpi_use_timer_override __initdata;
62 int acpi_fix_pin2_polarity __initdata;
63
64 #ifdef CONFIG_X86_LOCAL_APIC
65 static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
66 static bool acpi_support_online_capable;
67 #endif
68
69 #ifdef CONFIG_X86_64
70 /* Physical address of the Multiprocessor Wakeup Structure mailbox */
71 static u64 acpi_mp_wake_mailbox_paddr;
72 /* Virtual address of the Multiprocessor Wakeup Structure mailbox */
73 static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox;
74 #endif
75
76 #ifdef CONFIG_X86_IO_APIC
77 /*
78  * Locks related to IOAPIC hotplug
79  * Hotplug side:
80  *      ->device_hotplug_lock
81  *              ->acpi_ioapic_lock
82  *                      ->ioapic_lock
83  * Interrupt mapping side:
84  *      ->acpi_ioapic_lock
85  *              ->ioapic_mutex
86  *                      ->ioapic_lock
87  */
88 static DEFINE_MUTEX(acpi_ioapic_lock);
89 #endif
90
91 /* --------------------------------------------------------------------------
92                               Boot-time Configuration
93    -------------------------------------------------------------------------- */
94
95 /*
96  * The default interrupt routing model is PIC (8259).  This gets
97  * overridden if IOAPICs are enumerated (below).
98  */
99 enum acpi_irq_model_id acpi_irq_model = ACPI_IRQ_MODEL_PIC;
100
101
102 /*
103  * ISA irqs by default are the first 16 gsis but can be
104  * any gsi as specified by an interrupt source override.
105  */
106 static u32 isa_irq_to_gsi[NR_IRQS_LEGACY] __read_mostly = {
107         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
108 };
109
110 /*
111  * This is just a simple wrapper around early_memremap(),
112  * with sanity checks for phys == 0 and size == 0.
113  */
114 void __init __iomem *__acpi_map_table(unsigned long phys, unsigned long size)
115 {
116
117         if (!phys || !size)
118                 return NULL;
119
120         return early_memremap(phys, size);
121 }
122
123 void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
124 {
125         if (!map || !size)
126                 return;
127
128         early_memunmap(map, size);
129 }
130
131 #ifdef CONFIG_X86_LOCAL_APIC
132 static int __init acpi_parse_madt(struct acpi_table_header *table)
133 {
134         struct acpi_table_madt *madt = NULL;
135
136         if (!boot_cpu_has(X86_FEATURE_APIC))
137                 return -EINVAL;
138
139         madt = (struct acpi_table_madt *)table;
140         if (!madt) {
141                 pr_warn("Unable to map MADT\n");
142                 return -ENODEV;
143         }
144
145         if (madt->address) {
146                 acpi_lapic_addr = (u64) madt->address;
147
148                 pr_debug("Local APIC address 0x%08x\n", madt->address);
149         }
150
151         /* ACPI 6.3 and newer support the online capable bit. */
152         if (acpi_gbl_FADT.header.revision > 6 ||
153             (acpi_gbl_FADT.header.revision == 6 &&
154              acpi_gbl_FADT.minor_revision >= 3))
155                 acpi_support_online_capable = true;
156
157         default_acpi_madt_oem_check(madt->header.oem_id,
158                                     madt->header.oem_table_id);
159
160         return 0;
161 }
162
163 /**
164  * acpi_register_lapic - register a local apic and generates a logic cpu number
165  * @id: local apic id to register
166  * @acpiid: ACPI id to register
167  * @enabled: this cpu is enabled or not
168  *
169  * Returns the logic cpu number which maps to the local apic
170  */
171 static int acpi_register_lapic(int id, u32 acpiid, u8 enabled)
172 {
173         unsigned int ver = 0;
174         int cpu;
175
176         if (id >= MAX_LOCAL_APIC) {
177                 pr_info("skipped apicid that is too big\n");
178                 return -EINVAL;
179         }
180
181         if (!enabled) {
182                 ++disabled_cpus;
183                 return -EINVAL;
184         }
185
186         if (boot_cpu_physical_apicid != -1U)
187                 ver = boot_cpu_apic_version;
188
189         cpu = generic_processor_info(id, ver);
190         if (cpu >= 0)
191                 early_per_cpu(x86_cpu_to_acpiid, cpu) = acpiid;
192
193         return cpu;
194 }
195
196 static bool __init acpi_is_processor_usable(u32 lapic_flags)
197 {
198         if (lapic_flags & ACPI_MADT_ENABLED)
199                 return true;
200
201         if (!acpi_support_online_capable ||
202             (lapic_flags & ACPI_MADT_ONLINE_CAPABLE))
203                 return true;
204
205         return false;
206 }
207
208 static int __init
209 acpi_parse_x2apic(union acpi_subtable_headers *header, const unsigned long end)
210 {
211         struct acpi_madt_local_x2apic *processor = NULL;
212 #ifdef CONFIG_X86_X2APIC
213         u32 apic_id;
214         u8 enabled;
215 #endif
216
217         processor = (struct acpi_madt_local_x2apic *)header;
218
219         if (BAD_MADT_ENTRY(processor, end))
220                 return -EINVAL;
221
222         acpi_table_print_madt_entry(&header->common);
223
224 #ifdef CONFIG_X86_X2APIC
225         apic_id = processor->local_apic_id;
226         enabled = processor->lapic_flags & ACPI_MADT_ENABLED;
227
228         /* Ignore invalid ID */
229         if (apic_id == 0xffffffff)
230                 return 0;
231
232         /* don't register processors that cannot be onlined */
233         if (!acpi_is_processor_usable(processor->lapic_flags))
234                 return 0;
235
236         /*
237          * We need to register disabled CPU as well to permit
238          * counting disabled CPUs. This allows us to size
239          * cpus_possible_map more accurately, to permit
240          * to not preallocating memory for all NR_CPUS
241          * when we use CPU hotplug.
242          */
243         if (!apic->apic_id_valid(apic_id)) {
244                 if (enabled)
245                         pr_warn("x2apic entry ignored\n");
246                 return 0;
247         }
248
249         acpi_register_lapic(apic_id, processor->uid, enabled);
250 #else
251         pr_warn("x2apic entry ignored\n");
252 #endif
253
254         return 0;
255 }
256
257 static int __init
258 acpi_parse_lapic(union acpi_subtable_headers * header, const unsigned long end)
259 {
260         struct acpi_madt_local_apic *processor = NULL;
261
262         processor = (struct acpi_madt_local_apic *)header;
263
264         if (BAD_MADT_ENTRY(processor, end))
265                 return -EINVAL;
266
267         acpi_table_print_madt_entry(&header->common);
268
269         /* Ignore invalid ID */
270         if (processor->id == 0xff)
271                 return 0;
272
273         /* don't register processors that can not be onlined */
274         if (!acpi_is_processor_usable(processor->lapic_flags))
275                 return 0;
276
277         /*
278          * We need to register disabled CPU as well to permit
279          * counting disabled CPUs. This allows us to size
280          * cpus_possible_map more accurately, to permit
281          * to not preallocating memory for all NR_CPUS
282          * when we use CPU hotplug.
283          */
284         acpi_register_lapic(processor->id,      /* APIC ID */
285                             processor->processor_id, /* ACPI ID */
286                             processor->lapic_flags & ACPI_MADT_ENABLED);
287
288         return 0;
289 }
290
291 static int __init
292 acpi_parse_sapic(union acpi_subtable_headers *header, const unsigned long end)
293 {
294         struct acpi_madt_local_sapic *processor = NULL;
295
296         processor = (struct acpi_madt_local_sapic *)header;
297
298         if (BAD_MADT_ENTRY(processor, end))
299                 return -EINVAL;
300
301         acpi_table_print_madt_entry(&header->common);
302
303         acpi_register_lapic((processor->id << 8) | processor->eid,/* APIC ID */
304                             processor->processor_id, /* ACPI ID */
305                             processor->lapic_flags & ACPI_MADT_ENABLED);
306
307         return 0;
308 }
309
310 static int __init
311 acpi_parse_lapic_addr_ovr(union acpi_subtable_headers * header,
312                           const unsigned long end)
313 {
314         struct acpi_madt_local_apic_override *lapic_addr_ovr = NULL;
315
316         lapic_addr_ovr = (struct acpi_madt_local_apic_override *)header;
317
318         if (BAD_MADT_ENTRY(lapic_addr_ovr, end))
319                 return -EINVAL;
320
321         acpi_table_print_madt_entry(&header->common);
322
323         acpi_lapic_addr = lapic_addr_ovr->address;
324
325         return 0;
326 }
327
328 static int __init
329 acpi_parse_x2apic_nmi(union acpi_subtable_headers *header,
330                       const unsigned long end)
331 {
332         struct acpi_madt_local_x2apic_nmi *x2apic_nmi = NULL;
333
334         x2apic_nmi = (struct acpi_madt_local_x2apic_nmi *)header;
335
336         if (BAD_MADT_ENTRY(x2apic_nmi, end))
337                 return -EINVAL;
338
339         acpi_table_print_madt_entry(&header->common);
340
341         if (x2apic_nmi->lint != 1)
342                 pr_warn("NMI not connected to LINT 1!\n");
343
344         return 0;
345 }
346
347 static int __init
348 acpi_parse_lapic_nmi(union acpi_subtable_headers * header, const unsigned long end)
349 {
350         struct acpi_madt_local_apic_nmi *lapic_nmi = NULL;
351
352         lapic_nmi = (struct acpi_madt_local_apic_nmi *)header;
353
354         if (BAD_MADT_ENTRY(lapic_nmi, end))
355                 return -EINVAL;
356
357         acpi_table_print_madt_entry(&header->common);
358
359         if (lapic_nmi->lint != 1)
360                 pr_warn("NMI not connected to LINT 1!\n");
361
362         return 0;
363 }
364
365 #ifdef CONFIG_X86_64
366 static int acpi_wakeup_cpu(int apicid, unsigned long start_ip)
367 {
368         /*
369          * Remap mailbox memory only for the first call to acpi_wakeup_cpu().
370          *
371          * Wakeup of secondary CPUs is fully serialized in the core code.
372          * No need to protect acpi_mp_wake_mailbox from concurrent accesses.
373          */
374         if (!acpi_mp_wake_mailbox) {
375                 acpi_mp_wake_mailbox = memremap(acpi_mp_wake_mailbox_paddr,
376                                                 sizeof(*acpi_mp_wake_mailbox),
377                                                 MEMREMAP_WB);
378         }
379
380         /*
381          * Mailbox memory is shared between the firmware and OS. Firmware will
382          * listen on mailbox command address, and once it receives the wakeup
383          * command, the CPU associated with the given apicid will be booted.
384          *
385          * The value of 'apic_id' and 'wakeup_vector' must be visible to the
386          * firmware before the wakeup command is visible.  smp_store_release()
387          * ensures ordering and visibility.
388          */
389         acpi_mp_wake_mailbox->apic_id       = apicid;
390         acpi_mp_wake_mailbox->wakeup_vector = start_ip;
391         smp_store_release(&acpi_mp_wake_mailbox->command,
392                           ACPI_MP_WAKE_COMMAND_WAKEUP);
393
394         /*
395          * Wait for the CPU to wake up.
396          *
397          * The CPU being woken up is essentially in a spin loop waiting to be
398          * woken up. It should not take long for it wake up and acknowledge by
399          * zeroing out ->command.
400          *
401          * ACPI specification doesn't provide any guidance on how long kernel
402          * has to wait for a wake up acknowledgement. It also doesn't provide
403          * a way to cancel a wake up request if it takes too long.
404          *
405          * In TDX environment, the VMM has control over how long it takes to
406          * wake up secondary. It can postpone scheduling secondary vCPU
407          * indefinitely. Giving up on wake up request and reporting error opens
408          * possible attack vector for VMM: it can wake up a secondary CPU when
409          * kernel doesn't expect it. Wait until positive result of the wake up
410          * request.
411          */
412         while (READ_ONCE(acpi_mp_wake_mailbox->command))
413                 cpu_relax();
414
415         return 0;
416 }
417 #endif /* CONFIG_X86_64 */
418 #endif /* CONFIG_X86_LOCAL_APIC */
419
420 #ifdef CONFIG_X86_IO_APIC
421 #define MP_ISA_BUS              0
422
423 static int __init mp_register_ioapic_irq(u8 bus_irq, u8 polarity,
424                                                 u8 trigger, u32 gsi);
425
426 static void __init mp_override_legacy_irq(u8 bus_irq, u8 polarity, u8 trigger,
427                                           u32 gsi)
428 {
429         /*
430          * Check bus_irq boundary.
431          */
432         if (bus_irq >= NR_IRQS_LEGACY) {
433                 pr_warn("Invalid bus_irq %u for legacy override\n", bus_irq);
434                 return;
435         }
436
437         /*
438          * TBD: This check is for faulty timer entries, where the override
439          *      erroneously sets the trigger to level, resulting in a HUGE
440          *      increase of timer interrupts!
441          */
442         if ((bus_irq == 0) && (trigger == 3))
443                 trigger = 1;
444
445         if (mp_register_ioapic_irq(bus_irq, polarity, trigger, gsi) < 0)
446                 return;
447         /*
448          * Reset default identity mapping if gsi is also an legacy IRQ,
449          * otherwise there will be more than one entry with the same GSI
450          * and acpi_isa_irq_to_gsi() may give wrong result.
451          */
452         if (gsi < nr_legacy_irqs() && isa_irq_to_gsi[gsi] == gsi)
453                 isa_irq_to_gsi[gsi] = INVALID_ACPI_IRQ;
454         isa_irq_to_gsi[bus_irq] = gsi;
455 }
456
457 static void mp_config_acpi_gsi(struct device *dev, u32 gsi, int trigger,
458                         int polarity)
459 {
460 #ifdef CONFIG_X86_MPPARSE
461         struct mpc_intsrc mp_irq;
462         struct pci_dev *pdev;
463         unsigned char number;
464         unsigned int devfn;
465         int ioapic;
466         u8 pin;
467
468         if (!acpi_ioapic)
469                 return;
470         if (!dev || !dev_is_pci(dev))
471                 return;
472
473         pdev = to_pci_dev(dev);
474         number = pdev->bus->number;
475         devfn = pdev->devfn;
476         pin = pdev->pin;
477         /* print the entry should happen on mptable identically */
478         mp_irq.type = MP_INTSRC;
479         mp_irq.irqtype = mp_INT;
480         mp_irq.irqflag = (trigger == ACPI_EDGE_SENSITIVE ? 4 : 0x0c) |
481                                 (polarity == ACPI_ACTIVE_HIGH ? 1 : 3);
482         mp_irq.srcbus = number;
483         mp_irq.srcbusirq = (((devfn >> 3) & 0x1f) << 2) | ((pin - 1) & 3);
484         ioapic = mp_find_ioapic(gsi);
485         mp_irq.dstapic = mpc_ioapic_id(ioapic);
486         mp_irq.dstirq = mp_find_ioapic_pin(ioapic, gsi);
487
488         mp_save_irq(&mp_irq);
489 #endif
490 }
491
492 static int __init mp_register_ioapic_irq(u8 bus_irq, u8 polarity,
493                                                 u8 trigger, u32 gsi)
494 {
495         struct mpc_intsrc mp_irq;
496         int ioapic, pin;
497
498         /* Convert 'gsi' to 'ioapic.pin'(INTIN#) */
499         ioapic = mp_find_ioapic(gsi);
500         if (ioapic < 0) {
501                 pr_warn("Failed to find ioapic for gsi : %u\n", gsi);
502                 return ioapic;
503         }
504
505         pin = mp_find_ioapic_pin(ioapic, gsi);
506
507         mp_irq.type = MP_INTSRC;
508         mp_irq.irqtype = mp_INT;
509         mp_irq.irqflag = (trigger << 2) | polarity;
510         mp_irq.srcbus = MP_ISA_BUS;
511         mp_irq.srcbusirq = bus_irq;
512         mp_irq.dstapic = mpc_ioapic_id(ioapic);
513         mp_irq.dstirq = pin;
514
515         mp_save_irq(&mp_irq);
516
517         return 0;
518 }
519
520 static int __init
521 acpi_parse_ioapic(union acpi_subtable_headers * header, const unsigned long end)
522 {
523         struct acpi_madt_io_apic *ioapic = NULL;
524         struct ioapic_domain_cfg cfg = {
525                 .type = IOAPIC_DOMAIN_DYNAMIC,
526                 .ops = &mp_ioapic_irqdomain_ops,
527         };
528
529         ioapic = (struct acpi_madt_io_apic *)header;
530
531         if (BAD_MADT_ENTRY(ioapic, end))
532                 return -EINVAL;
533
534         acpi_table_print_madt_entry(&header->common);
535
536         /* Statically assign IRQ numbers for IOAPICs hosting legacy IRQs */
537         if (ioapic->global_irq_base < nr_legacy_irqs())
538                 cfg.type = IOAPIC_DOMAIN_LEGACY;
539
540         mp_register_ioapic(ioapic->id, ioapic->address, ioapic->global_irq_base,
541                            &cfg);
542
543         return 0;
544 }
545
546 /*
547  * Parse Interrupt Source Override for the ACPI SCI
548  */
549 static void __init acpi_sci_ioapic_setup(u8 bus_irq, u16 polarity, u16 trigger, u32 gsi)
550 {
551         if (trigger == 0)       /* compatible SCI trigger is level */
552                 trigger = 3;
553
554         if (polarity == 0)      /* compatible SCI polarity is low */
555                 polarity = 3;
556
557         /* Command-line over-ride via acpi_sci= */
558         if (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)
559                 trigger = (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
560
561         if (acpi_sci_flags & ACPI_MADT_POLARITY_MASK)
562                 polarity = acpi_sci_flags & ACPI_MADT_POLARITY_MASK;
563
564         if (bus_irq < NR_IRQS_LEGACY)
565                 mp_override_legacy_irq(bus_irq, polarity, trigger, gsi);
566         else
567                 mp_register_ioapic_irq(bus_irq, polarity, trigger, gsi);
568
569         acpi_penalize_sci_irq(bus_irq, trigger, polarity);
570
571         /*
572          * stash over-ride to indicate we've been here
573          * and for later update of acpi_gbl_FADT
574          */
575         acpi_sci_override_gsi = gsi;
576         return;
577 }
578
579 static int __init
580 acpi_parse_int_src_ovr(union acpi_subtable_headers * header,
581                        const unsigned long end)
582 {
583         struct acpi_madt_interrupt_override *intsrc = NULL;
584
585         intsrc = (struct acpi_madt_interrupt_override *)header;
586
587         if (BAD_MADT_ENTRY(intsrc, end))
588                 return -EINVAL;
589
590         acpi_table_print_madt_entry(&header->common);
591
592         if (intsrc->source_irq < NR_IRQS_LEGACY)
593                 acpi_int_src_ovr[intsrc->source_irq] = true;
594
595         if (intsrc->source_irq == acpi_gbl_FADT.sci_interrupt) {
596                 acpi_sci_ioapic_setup(intsrc->source_irq,
597                                       intsrc->inti_flags & ACPI_MADT_POLARITY_MASK,
598                                       (intsrc->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2,
599                                       intsrc->global_irq);
600                 return 0;
601         }
602
603         if (intsrc->source_irq == 0) {
604                 if (acpi_skip_timer_override) {
605                         pr_warn("BIOS IRQ0 override ignored.\n");
606                         return 0;
607                 }
608
609                 if ((intsrc->global_irq == 2) && acpi_fix_pin2_polarity
610                         && (intsrc->inti_flags & ACPI_MADT_POLARITY_MASK)) {
611                         intsrc->inti_flags &= ~ACPI_MADT_POLARITY_MASK;
612                         pr_warn("BIOS IRQ0 pin2 override: forcing polarity to high active.\n");
613                 }
614         }
615
616         mp_override_legacy_irq(intsrc->source_irq,
617                                 intsrc->inti_flags & ACPI_MADT_POLARITY_MASK,
618                                 (intsrc->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2,
619                                 intsrc->global_irq);
620
621         return 0;
622 }
623
624 static int __init
625 acpi_parse_nmi_src(union acpi_subtable_headers * header, const unsigned long end)
626 {
627         struct acpi_madt_nmi_source *nmi_src = NULL;
628
629         nmi_src = (struct acpi_madt_nmi_source *)header;
630
631         if (BAD_MADT_ENTRY(nmi_src, end))
632                 return -EINVAL;
633
634         acpi_table_print_madt_entry(&header->common);
635
636         /* TBD: Support nimsrc entries? */
637
638         return 0;
639 }
640
641 #endif                          /* CONFIG_X86_IO_APIC */
642
643 /*
644  * acpi_pic_sci_set_trigger()
645  *
646  * use ELCR to set PIC-mode trigger type for SCI
647  *
648  * If a PIC-mode SCI is not recognized or gives spurious IRQ7's
649  * it may require Edge Trigger -- use "acpi_sci=edge"
650  *
651  * Port 0x4d0-4d1 are ELCR1 and ELCR2, the Edge/Level Control Registers
652  * for the 8259 PIC.  bit[n] = 1 means irq[n] is Level, otherwise Edge.
653  * ELCR1 is IRQs 0-7 (IRQ 0, 1, 2 must be 0)
654  * ELCR2 is IRQs 8-15 (IRQ 8, 13 must be 0)
655  */
656
657 void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger)
658 {
659         unsigned int mask = 1 << irq;
660         unsigned int old, new;
661
662         /* Real old ELCR mask */
663         old = inb(PIC_ELCR1) | (inb(PIC_ELCR2) << 8);
664
665         /*
666          * If we use ACPI to set PCI IRQs, then we should clear ELCR
667          * since we will set it correctly as we enable the PCI irq
668          * routing.
669          */
670         new = acpi_noirq ? old : 0;
671
672         /*
673          * Update SCI information in the ELCR, it isn't in the PCI
674          * routing tables..
675          */
676         switch (trigger) {
677         case 1:         /* Edge - clear */
678                 new &= ~mask;
679                 break;
680         case 3:         /* Level - set */
681                 new |= mask;
682                 break;
683         }
684
685         if (old == new)
686                 return;
687
688         pr_warn("setting ELCR to %04x (from %04x)\n", new, old);
689         outb(new, PIC_ELCR1);
690         outb(new >> 8, PIC_ELCR2);
691 }
692
693 int acpi_gsi_to_irq(u32 gsi, unsigned int *irqp)
694 {
695         int rc, irq, trigger, polarity;
696
697         if (acpi_irq_model == ACPI_IRQ_MODEL_PIC) {
698                 *irqp = gsi;
699                 return 0;
700         }
701
702         rc = acpi_get_override_irq(gsi, &trigger, &polarity);
703         if (rc)
704                 return rc;
705
706         trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
707         polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
708         irq = acpi_register_gsi(NULL, gsi, trigger, polarity);
709         if (irq < 0)
710                 return irq;
711
712         *irqp = irq;
713         return 0;
714 }
715 EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
716
717 int acpi_isa_irq_to_gsi(unsigned isa_irq, u32 *gsi)
718 {
719         if (isa_irq < nr_legacy_irqs() &&
720             isa_irq_to_gsi[isa_irq] != INVALID_ACPI_IRQ) {
721                 *gsi = isa_irq_to_gsi[isa_irq];
722                 return 0;
723         }
724
725         return -1;
726 }
727
728 static int acpi_register_gsi_pic(struct device *dev, u32 gsi,
729                                  int trigger, int polarity)
730 {
731 #ifdef CONFIG_PCI
732         /*
733          * Make sure all (legacy) PCI IRQs are set as level-triggered.
734          */
735         if (trigger == ACPI_LEVEL_SENSITIVE)
736                 elcr_set_level_irq(gsi);
737 #endif
738
739         return gsi;
740 }
741
742 #ifdef CONFIG_X86_LOCAL_APIC
743 static int acpi_register_gsi_ioapic(struct device *dev, u32 gsi,
744                                     int trigger, int polarity)
745 {
746         int irq = gsi;
747 #ifdef CONFIG_X86_IO_APIC
748         int node;
749         struct irq_alloc_info info;
750
751         node = dev ? dev_to_node(dev) : NUMA_NO_NODE;
752         trigger = trigger == ACPI_EDGE_SENSITIVE ? 0 : 1;
753         polarity = polarity == ACPI_ACTIVE_HIGH ? 0 : 1;
754         ioapic_set_alloc_attr(&info, node, trigger, polarity);
755
756         mutex_lock(&acpi_ioapic_lock);
757         irq = mp_map_gsi_to_irq(gsi, IOAPIC_MAP_ALLOC, &info);
758         /* Don't set up the ACPI SCI because it's already set up */
759         if (irq >= 0 && enable_update_mptable && gsi != acpi_gbl_FADT.sci_interrupt)
760                 mp_config_acpi_gsi(dev, gsi, trigger, polarity);
761         mutex_unlock(&acpi_ioapic_lock);
762 #endif
763
764         return irq;
765 }
766
767 static void acpi_unregister_gsi_ioapic(u32 gsi)
768 {
769 #ifdef CONFIG_X86_IO_APIC
770         int irq;
771
772         mutex_lock(&acpi_ioapic_lock);
773         irq = mp_map_gsi_to_irq(gsi, 0, NULL);
774         if (irq > 0)
775                 mp_unmap_irq(irq);
776         mutex_unlock(&acpi_ioapic_lock);
777 #endif
778 }
779 #endif
780
781 int (*__acpi_register_gsi)(struct device *dev, u32 gsi,
782                            int trigger, int polarity) = acpi_register_gsi_pic;
783 void (*__acpi_unregister_gsi)(u32 gsi) = NULL;
784
785 #ifdef CONFIG_ACPI_SLEEP
786 int (*acpi_suspend_lowlevel)(void) = x86_acpi_suspend_lowlevel;
787 #else
788 int (*acpi_suspend_lowlevel)(void);
789 #endif
790
791 /*
792  * success: return IRQ number (>=0)
793  * failure: return < 0
794  */
795 int acpi_register_gsi(struct device *dev, u32 gsi, int trigger, int polarity)
796 {
797         return __acpi_register_gsi(dev, gsi, trigger, polarity);
798 }
799 EXPORT_SYMBOL_GPL(acpi_register_gsi);
800
801 void acpi_unregister_gsi(u32 gsi)
802 {
803         if (__acpi_unregister_gsi)
804                 __acpi_unregister_gsi(gsi);
805 }
806 EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
807
808 #ifdef CONFIG_X86_LOCAL_APIC
809 static void __init acpi_set_irq_model_ioapic(void)
810 {
811         acpi_irq_model = ACPI_IRQ_MODEL_IOAPIC;
812         __acpi_register_gsi = acpi_register_gsi_ioapic;
813         __acpi_unregister_gsi = acpi_unregister_gsi_ioapic;
814         acpi_ioapic = 1;
815 }
816 #endif
817
818 /*
819  *  ACPI based hotplug support for CPU
820  */
821 #ifdef CONFIG_ACPI_HOTPLUG_CPU
822 #include <acpi/processor.h>
823
824 static int acpi_map_cpu2node(acpi_handle handle, int cpu, int physid)
825 {
826 #ifdef CONFIG_ACPI_NUMA
827         int nid;
828
829         nid = acpi_get_node(handle);
830         if (nid != NUMA_NO_NODE) {
831                 set_apicid_to_node(physid, nid);
832                 numa_set_node(cpu, nid);
833         }
834 #endif
835         return 0;
836 }
837
838 int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id,
839                  int *pcpu)
840 {
841         int cpu;
842
843         cpu = acpi_register_lapic(physid, acpi_id, ACPI_MADT_ENABLED);
844         if (cpu < 0) {
845                 pr_info("Unable to map lapic to logical cpu number\n");
846                 return cpu;
847         }
848
849         acpi_processor_set_pdc(handle);
850         acpi_map_cpu2node(handle, cpu, physid);
851
852         *pcpu = cpu;
853         return 0;
854 }
855 EXPORT_SYMBOL(acpi_map_cpu);
856
857 int acpi_unmap_cpu(int cpu)
858 {
859 #ifdef CONFIG_ACPI_NUMA
860         set_apicid_to_node(per_cpu(x86_cpu_to_apicid, cpu), NUMA_NO_NODE);
861 #endif
862
863         per_cpu(x86_cpu_to_apicid, cpu) = -1;
864         set_cpu_present(cpu, false);
865         num_processors--;
866
867         return (0);
868 }
869 EXPORT_SYMBOL(acpi_unmap_cpu);
870 #endif                          /* CONFIG_ACPI_HOTPLUG_CPU */
871
872 int acpi_register_ioapic(acpi_handle handle, u64 phys_addr, u32 gsi_base)
873 {
874         int ret = -ENOSYS;
875 #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
876         int ioapic_id;
877         u64 addr;
878         struct ioapic_domain_cfg cfg = {
879                 .type = IOAPIC_DOMAIN_DYNAMIC,
880                 .ops = &mp_ioapic_irqdomain_ops,
881         };
882
883         ioapic_id = acpi_get_ioapic_id(handle, gsi_base, &addr);
884         if (ioapic_id < 0) {
885                 unsigned long long uid;
886                 acpi_status status;
887
888                 status = acpi_evaluate_integer(handle, METHOD_NAME__UID,
889                                                NULL, &uid);
890                 if (ACPI_FAILURE(status)) {
891                         acpi_handle_warn(handle, "failed to get IOAPIC ID.\n");
892                         return -EINVAL;
893                 }
894                 ioapic_id = (int)uid;
895         }
896
897         mutex_lock(&acpi_ioapic_lock);
898         ret  = mp_register_ioapic(ioapic_id, phys_addr, gsi_base, &cfg);
899         mutex_unlock(&acpi_ioapic_lock);
900 #endif
901
902         return ret;
903 }
904 EXPORT_SYMBOL(acpi_register_ioapic);
905
906 int acpi_unregister_ioapic(acpi_handle handle, u32 gsi_base)
907 {
908         int ret = -ENOSYS;
909
910 #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
911         mutex_lock(&acpi_ioapic_lock);
912         ret  = mp_unregister_ioapic(gsi_base);
913         mutex_unlock(&acpi_ioapic_lock);
914 #endif
915
916         return ret;
917 }
918 EXPORT_SYMBOL(acpi_unregister_ioapic);
919
920 /**
921  * acpi_ioapic_registered - Check whether IOAPIC associated with @gsi_base
922  *                          has been registered
923  * @handle:     ACPI handle of the IOAPIC device
924  * @gsi_base:   GSI base associated with the IOAPIC
925  *
926  * Assume caller holds some type of lock to serialize acpi_ioapic_registered()
927  * with acpi_register_ioapic()/acpi_unregister_ioapic().
928  */
929 int acpi_ioapic_registered(acpi_handle handle, u32 gsi_base)
930 {
931         int ret = 0;
932
933 #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
934         mutex_lock(&acpi_ioapic_lock);
935         ret  = mp_ioapic_registered(gsi_base);
936         mutex_unlock(&acpi_ioapic_lock);
937 #endif
938
939         return ret;
940 }
941
942 static int __init acpi_parse_sbf(struct acpi_table_header *table)
943 {
944         struct acpi_table_boot *sb = (struct acpi_table_boot *)table;
945
946         sbf_port = sb->cmos_index;      /* Save CMOS port */
947
948         return 0;
949 }
950
951 #ifdef CONFIG_HPET_TIMER
952 #include <asm/hpet.h>
953
954 static struct resource *hpet_res __initdata;
955
956 static int __init acpi_parse_hpet(struct acpi_table_header *table)
957 {
958         struct acpi_table_hpet *hpet_tbl = (struct acpi_table_hpet *)table;
959
960         if (hpet_tbl->address.space_id != ACPI_SPACE_MEM) {
961                 pr_warn("HPET timers must be located in memory.\n");
962                 return -1;
963         }
964
965         hpet_address = hpet_tbl->address.address;
966         hpet_blockid = hpet_tbl->sequence;
967
968         /*
969          * Some broken BIOSes advertise HPET at 0x0. We really do not
970          * want to allocate a resource there.
971          */
972         if (!hpet_address) {
973                 pr_warn("HPET id: %#x base: %#lx is invalid\n", hpet_tbl->id, hpet_address);
974                 return 0;
975         }
976 #ifdef CONFIG_X86_64
977         /*
978          * Some even more broken BIOSes advertise HPET at
979          * 0xfed0000000000000 instead of 0xfed00000. Fix it up and add
980          * some noise:
981          */
982         if (hpet_address == 0xfed0000000000000UL) {
983                 if (!hpet_force_user) {
984                         pr_warn("HPET id: %#x base: 0xfed0000000000000 is bogus, try hpet=force on the kernel command line to fix it up to 0xfed00000.\n",
985                                 hpet_tbl->id);
986                         hpet_address = 0;
987                         return 0;
988                 }
989                 pr_warn("HPET id: %#x base: 0xfed0000000000000 fixed up to 0xfed00000.\n",
990                         hpet_tbl->id);
991                 hpet_address >>= 32;
992         }
993 #endif
994         pr_info("HPET id: %#x base: %#lx\n", hpet_tbl->id, hpet_address);
995
996         /*
997          * Allocate and initialize the HPET firmware resource for adding into
998          * the resource tree during the lateinit timeframe.
999          */
1000 #define HPET_RESOURCE_NAME_SIZE 9
1001         hpet_res = memblock_alloc(sizeof(*hpet_res) + HPET_RESOURCE_NAME_SIZE,
1002                                   SMP_CACHE_BYTES);
1003         if (!hpet_res)
1004                 panic("%s: Failed to allocate %zu bytes\n", __func__,
1005                       sizeof(*hpet_res) + HPET_RESOURCE_NAME_SIZE);
1006
1007         hpet_res->name = (void *)&hpet_res[1];
1008         hpet_res->flags = IORESOURCE_MEM;
1009         snprintf((char *)hpet_res->name, HPET_RESOURCE_NAME_SIZE, "HPET %u",
1010                  hpet_tbl->sequence);
1011
1012         hpet_res->start = hpet_address;
1013         hpet_res->end = hpet_address + (1 * 1024) - 1;
1014
1015         return 0;
1016 }
1017
1018 /*
1019  * hpet_insert_resource inserts the HPET resources used into the resource
1020  * tree.
1021  */
1022 static __init int hpet_insert_resource(void)
1023 {
1024         if (!hpet_res)
1025                 return 1;
1026
1027         return insert_resource(&iomem_resource, hpet_res);
1028 }
1029
1030 late_initcall(hpet_insert_resource);
1031
1032 #else
1033 #define acpi_parse_hpet NULL
1034 #endif
1035
1036 static int __init acpi_parse_fadt(struct acpi_table_header *table)
1037 {
1038         if (!(acpi_gbl_FADT.boot_flags & ACPI_FADT_LEGACY_DEVICES)) {
1039                 pr_debug("no legacy devices present\n");
1040                 x86_platform.legacy.devices.pnpbios = 0;
1041         }
1042
1043         if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
1044             !(acpi_gbl_FADT.boot_flags & ACPI_FADT_8042) &&
1045             x86_platform.legacy.i8042 != X86_LEGACY_I8042_PLATFORM_ABSENT) {
1046                 pr_debug("i8042 controller is absent\n");
1047                 x86_platform.legacy.i8042 = X86_LEGACY_I8042_FIRMWARE_ABSENT;
1048         }
1049
1050         if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC) {
1051                 pr_debug("not registering RTC platform device\n");
1052                 x86_platform.legacy.rtc = 0;
1053         }
1054
1055         if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_VGA) {
1056                 pr_debug("probing for VGA not safe\n");
1057                 x86_platform.legacy.no_vga = 1;
1058         }
1059
1060 #ifdef CONFIG_X86_PM_TIMER
1061         /* detect the location of the ACPI PM Timer */
1062         if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) {
1063                 /* FADT rev. 2 */
1064                 if (acpi_gbl_FADT.xpm_timer_block.space_id !=
1065                     ACPI_ADR_SPACE_SYSTEM_IO)
1066                         return 0;
1067
1068                 pmtmr_ioport = acpi_gbl_FADT.xpm_timer_block.address;
1069                 /*
1070                  * "X" fields are optional extensions to the original V1.0
1071                  * fields, so we must selectively expand V1.0 fields if the
1072                  * corresponding X field is zero.
1073                  */
1074                 if (!pmtmr_ioport)
1075                         pmtmr_ioport = acpi_gbl_FADT.pm_timer_block;
1076         } else {
1077                 /* FADT rev. 1 */
1078                 pmtmr_ioport = acpi_gbl_FADT.pm_timer_block;
1079         }
1080         if (pmtmr_ioport)
1081                 pr_info("PM-Timer IO Port: %#x\n", pmtmr_ioport);
1082 #endif
1083         return 0;
1084 }
1085
1086 #ifdef  CONFIG_X86_LOCAL_APIC
1087 /*
1088  * Parse LAPIC entries in MADT
1089  * returns 0 on success, < 0 on error
1090  */
1091
1092 static int __init early_acpi_parse_madt_lapic_addr_ovr(void)
1093 {
1094         int count;
1095
1096         if (!boot_cpu_has(X86_FEATURE_APIC))
1097                 return -ENODEV;
1098
1099         /*
1100          * Note that the LAPIC address is obtained from the MADT (32-bit value)
1101          * and (optionally) overridden by a LAPIC_ADDR_OVR entry (64-bit value).
1102          */
1103
1104         count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE,
1105                                       acpi_parse_lapic_addr_ovr, 0);
1106         if (count < 0) {
1107                 pr_err("Error parsing LAPIC address override entry\n");
1108                 return count;
1109         }
1110
1111         register_lapic_address(acpi_lapic_addr);
1112
1113         return count;
1114 }
1115
1116 static int __init acpi_parse_madt_lapic_entries(void)
1117 {
1118         int count;
1119         int x2count = 0;
1120         int ret;
1121         struct acpi_subtable_proc madt_proc[2];
1122
1123         if (!boot_cpu_has(X86_FEATURE_APIC))
1124                 return -ENODEV;
1125
1126         count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_SAPIC,
1127                                       acpi_parse_sapic, MAX_LOCAL_APIC);
1128
1129         if (!count) {
1130                 memset(madt_proc, 0, sizeof(madt_proc));
1131                 madt_proc[0].id = ACPI_MADT_TYPE_LOCAL_APIC;
1132                 madt_proc[0].handler = acpi_parse_lapic;
1133                 madt_proc[1].id = ACPI_MADT_TYPE_LOCAL_X2APIC;
1134                 madt_proc[1].handler = acpi_parse_x2apic;
1135                 ret = acpi_table_parse_entries_array(ACPI_SIG_MADT,
1136                                 sizeof(struct acpi_table_madt),
1137                                 madt_proc, ARRAY_SIZE(madt_proc), MAX_LOCAL_APIC);
1138                 if (ret < 0) {
1139                         pr_err("Error parsing LAPIC/X2APIC entries\n");
1140                         return ret;
1141                 }
1142
1143                 count = madt_proc[0].count;
1144                 x2count = madt_proc[1].count;
1145         }
1146         if (!count && !x2count) {
1147                 pr_err("No LAPIC entries present\n");
1148                 /* TBD: Cleanup to allow fallback to MPS */
1149                 return -ENODEV;
1150         } else if (count < 0 || x2count < 0) {
1151                 pr_err("Error parsing LAPIC entry\n");
1152                 /* TBD: Cleanup to allow fallback to MPS */
1153                 return count;
1154         }
1155
1156         x2count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_X2APIC_NMI,
1157                                         acpi_parse_x2apic_nmi, 0);
1158         count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_NMI,
1159                                       acpi_parse_lapic_nmi, 0);
1160         if (count < 0 || x2count < 0) {
1161                 pr_err("Error parsing LAPIC NMI entry\n");
1162                 /* TBD: Cleanup to allow fallback to MPS */
1163                 return count;
1164         }
1165         return 0;
1166 }
1167
1168 #ifdef CONFIG_X86_64
1169 static int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,
1170                                      const unsigned long end)
1171 {
1172         struct acpi_madt_multiproc_wakeup *mp_wake;
1173
1174         if (!IS_ENABLED(CONFIG_SMP))
1175                 return -ENODEV;
1176
1177         mp_wake = (struct acpi_madt_multiproc_wakeup *)header;
1178         if (BAD_MADT_ENTRY(mp_wake, end))
1179                 return -EINVAL;
1180
1181         acpi_table_print_madt_entry(&header->common);
1182
1183         acpi_mp_wake_mailbox_paddr = mp_wake->base_address;
1184
1185         acpi_wake_cpu_handler_update(acpi_wakeup_cpu);
1186
1187         return 0;
1188 }
1189 #endif                          /* CONFIG_X86_64 */
1190 #endif                          /* CONFIG_X86_LOCAL_APIC */
1191
1192 #ifdef  CONFIG_X86_IO_APIC
1193 static void __init mp_config_acpi_legacy_irqs(void)
1194 {
1195         int i;
1196         struct mpc_intsrc mp_irq;
1197
1198 #ifdef CONFIG_EISA
1199         /*
1200          * Fabricate the legacy ISA bus (bus #31).
1201          */
1202         mp_bus_id_to_type[MP_ISA_BUS] = MP_BUS_ISA;
1203 #endif
1204         set_bit(MP_ISA_BUS, mp_bus_not_pci);
1205         pr_debug("Bus #%d is ISA (nIRQs: %d)\n", MP_ISA_BUS, nr_legacy_irqs());
1206
1207         /*
1208          * Use the default configuration for the IRQs 0-15.  Unless
1209          * overridden by (MADT) interrupt source override entries.
1210          */
1211         for (i = 0; i < nr_legacy_irqs(); i++) {
1212                 int ioapic, pin;
1213                 unsigned int dstapic;
1214                 int idx;
1215                 u32 gsi;
1216
1217                 /* Locate the gsi that irq i maps to. */
1218                 if (acpi_isa_irq_to_gsi(i, &gsi))
1219                         continue;
1220
1221                 /*
1222                  * Locate the IOAPIC that manages the ISA IRQ.
1223                  */
1224                 ioapic = mp_find_ioapic(gsi);
1225                 if (ioapic < 0)
1226                         continue;
1227                 pin = mp_find_ioapic_pin(ioapic, gsi);
1228                 dstapic = mpc_ioapic_id(ioapic);
1229
1230                 for (idx = 0; idx < mp_irq_entries; idx++) {
1231                         struct mpc_intsrc *irq = mp_irqs + idx;
1232
1233                         /* Do we already have a mapping for this ISA IRQ? */
1234                         if (irq->srcbus == MP_ISA_BUS && irq->srcbusirq == i)
1235                                 break;
1236
1237                         /* Do we already have a mapping for this IOAPIC pin */
1238                         if (irq->dstapic == dstapic && irq->dstirq == pin)
1239                                 break;
1240                 }
1241
1242                 if (idx != mp_irq_entries) {
1243                         pr_debug("ACPI: IRQ%d used by override.\n", i);
1244                         continue;       /* IRQ already used */
1245                 }
1246
1247                 mp_irq.type = MP_INTSRC;
1248                 mp_irq.irqflag = 0;     /* Conforming */
1249                 mp_irq.srcbus = MP_ISA_BUS;
1250                 mp_irq.dstapic = dstapic;
1251                 mp_irq.irqtype = mp_INT;
1252                 mp_irq.srcbusirq = i; /* Identity mapped */
1253                 mp_irq.dstirq = pin;
1254
1255                 mp_save_irq(&mp_irq);
1256         }
1257 }
1258
1259 /*
1260  * Parse IOAPIC related entries in MADT
1261  * returns 0 on success, < 0 on error
1262  */
1263 static int __init acpi_parse_madt_ioapic_entries(void)
1264 {
1265         int count;
1266
1267         /*
1268          * ACPI interpreter is required to complete interrupt setup,
1269          * so if it is off, don't enumerate the io-apics with ACPI.
1270          * If MPS is present, it will handle them,
1271          * otherwise the system will stay in PIC mode
1272          */
1273         if (acpi_disabled || acpi_noirq)
1274                 return -ENODEV;
1275
1276         if (!boot_cpu_has(X86_FEATURE_APIC))
1277                 return -ENODEV;
1278
1279         /*
1280          * if "noapic" boot option, don't look for IO-APICs
1281          */
1282         if (skip_ioapic_setup) {
1283                 pr_info("Skipping IOAPIC probe due to 'noapic' option.\n");
1284                 return -ENODEV;
1285         }
1286
1287         count = acpi_table_parse_madt(ACPI_MADT_TYPE_IO_APIC, acpi_parse_ioapic,
1288                                       MAX_IO_APICS);
1289         if (!count) {
1290                 pr_err("No IOAPIC entries present\n");
1291                 return -ENODEV;
1292         } else if (count < 0) {
1293                 pr_err("Error parsing IOAPIC entry\n");
1294                 return count;
1295         }
1296
1297         count = acpi_table_parse_madt(ACPI_MADT_TYPE_INTERRUPT_OVERRIDE,
1298                                       acpi_parse_int_src_ovr, nr_irqs);
1299         if (count < 0) {
1300                 pr_err("Error parsing interrupt source overrides entry\n");
1301                 /* TBD: Cleanup to allow fallback to MPS */
1302                 return count;
1303         }
1304
1305         /*
1306          * If BIOS did not supply an INT_SRC_OVR for the SCI
1307          * pretend we got one so we can set the SCI flags.
1308          * But ignore setting up SCI on hardware reduced platforms.
1309          */
1310         if (acpi_sci_override_gsi == INVALID_ACPI_IRQ && !acpi_gbl_reduced_hardware)
1311                 acpi_sci_ioapic_setup(acpi_gbl_FADT.sci_interrupt, 0, 0,
1312                                       acpi_gbl_FADT.sci_interrupt);
1313
1314         /* Fill in identity legacy mappings where no override */
1315         mp_config_acpi_legacy_irqs();
1316
1317         count = acpi_table_parse_madt(ACPI_MADT_TYPE_NMI_SOURCE,
1318                                       acpi_parse_nmi_src, nr_irqs);
1319         if (count < 0) {
1320                 pr_err("Error parsing NMI SRC entry\n");
1321                 /* TBD: Cleanup to allow fallback to MPS */
1322                 return count;
1323         }
1324
1325         return 0;
1326 }
1327 #else
1328 static inline int acpi_parse_madt_ioapic_entries(void)
1329 {
1330         return -1;
1331 }
1332 #endif  /* !CONFIG_X86_IO_APIC */
1333
1334 static void __init early_acpi_process_madt(void)
1335 {
1336 #ifdef CONFIG_X86_LOCAL_APIC
1337         int error;
1338
1339         if (!acpi_table_parse(ACPI_SIG_MADT, acpi_parse_madt)) {
1340
1341                 /*
1342                  * Parse MADT LAPIC entries
1343                  */
1344                 error = early_acpi_parse_madt_lapic_addr_ovr();
1345                 if (!error) {
1346                         acpi_lapic = 1;
1347                         smp_found_config = 1;
1348                 }
1349                 if (error == -EINVAL) {
1350                         /*
1351                          * Dell Precision Workstation 410, 610 come here.
1352                          */
1353                         pr_err("Invalid BIOS MADT, disabling ACPI\n");
1354                         disable_acpi();
1355                 }
1356         }
1357 #endif
1358 }
1359
1360 static void __init acpi_process_madt(void)
1361 {
1362 #ifdef CONFIG_X86_LOCAL_APIC
1363         int error;
1364
1365         if (!acpi_table_parse(ACPI_SIG_MADT, acpi_parse_madt)) {
1366
1367                 /*
1368                  * Parse MADT LAPIC entries
1369                  */
1370                 error = acpi_parse_madt_lapic_entries();
1371                 if (!error) {
1372                         acpi_lapic = 1;
1373
1374                         /*
1375                          * Parse MADT IO-APIC entries
1376                          */
1377                         mutex_lock(&acpi_ioapic_lock);
1378                         error = acpi_parse_madt_ioapic_entries();
1379                         mutex_unlock(&acpi_ioapic_lock);
1380                         if (!error) {
1381                                 acpi_set_irq_model_ioapic();
1382
1383                                 smp_found_config = 1;
1384                         }
1385
1386 #ifdef CONFIG_X86_64
1387                         /*
1388                          * Parse MADT MP Wake entry.
1389                          */
1390                         acpi_table_parse_madt(ACPI_MADT_TYPE_MULTIPROC_WAKEUP,
1391                                               acpi_parse_mp_wake, 1);
1392 #endif
1393                 }
1394                 if (error == -EINVAL) {
1395                         /*
1396                          * Dell Precision Workstation 410, 610 come here.
1397                          */
1398                         pr_err("Invalid BIOS MADT, disabling ACPI\n");
1399                         disable_acpi();
1400                 }
1401         } else {
1402                 /*
1403                  * ACPI found no MADT, and so ACPI wants UP PIC mode.
1404                  * In the event an MPS table was found, forget it.
1405                  * Boot with "acpi=off" to use MPS on such a system.
1406                  */
1407                 if (smp_found_config) {
1408                         pr_warn("No APIC-table, disabling MPS\n");
1409                         smp_found_config = 0;
1410                 }
1411         }
1412
1413         /*
1414          * ACPI supports both logical (e.g. Hyper-Threading) and physical
1415          * processors, where MPS only supports physical.
1416          */
1417         if (acpi_lapic && acpi_ioapic)
1418                 pr_info("Using ACPI (MADT) for SMP configuration information\n");
1419         else if (acpi_lapic)
1420                 pr_info("Using ACPI for processor (LAPIC) configuration information\n");
1421 #endif
1422         return;
1423 }
1424
1425 static int __init disable_acpi_irq(const struct dmi_system_id *d)
1426 {
1427         if (!acpi_force) {
1428                 pr_notice("%s detected: force use of acpi=noirq\n", d->ident);
1429                 acpi_noirq_set();
1430         }
1431         return 0;
1432 }
1433
1434 static int __init disable_acpi_pci(const struct dmi_system_id *d)
1435 {
1436         if (!acpi_force) {
1437                 pr_notice("%s detected: force use of pci=noacpi\n", d->ident);
1438                 acpi_disable_pci();
1439         }
1440         return 0;
1441 }
1442
1443 static int __init disable_acpi_xsdt(const struct dmi_system_id *d)
1444 {
1445         if (!acpi_force) {
1446                 pr_notice("%s detected: force use of acpi=rsdt\n", d->ident);
1447                 acpi_gbl_do_not_use_xsdt = TRUE;
1448         } else {
1449                 pr_notice("Warning: DMI blacklist says broken, but acpi XSDT forced\n");
1450         }
1451         return 0;
1452 }
1453
1454 static int __init dmi_disable_acpi(const struct dmi_system_id *d)
1455 {
1456         if (!acpi_force) {
1457                 pr_notice("%s detected: acpi off\n", d->ident);
1458                 disable_acpi();
1459         } else {
1460                 pr_notice("Warning: DMI blacklist says broken, but acpi forced\n");
1461         }
1462         return 0;
1463 }
1464
1465 /*
1466  * Force ignoring BIOS IRQ0 override
1467  */
1468 static int __init dmi_ignore_irq0_timer_override(const struct dmi_system_id *d)
1469 {
1470         if (!acpi_skip_timer_override) {
1471                 pr_notice("%s detected: Ignoring BIOS IRQ0 override\n",
1472                         d->ident);
1473                 acpi_skip_timer_override = 1;
1474         }
1475         return 0;
1476 }
1477
1478 /*
1479  * ACPI offers an alternative platform interface model that removes
1480  * ACPI hardware requirements for platforms that do not implement
1481  * the PC Architecture.
1482  *
1483  * We initialize the Hardware-reduced ACPI model here:
1484  */
1485 void __init acpi_generic_reduced_hw_init(void)
1486 {
1487         /*
1488          * Override x86_init functions and bypass legacy PIC in
1489          * hardware reduced ACPI mode.
1490          */
1491         x86_init.timers.timer_init      = x86_init_noop;
1492         x86_init.irqs.pre_vector_init   = x86_init_noop;
1493         legacy_pic                      = &null_legacy_pic;
1494 }
1495
1496 static void __init acpi_reduced_hw_init(void)
1497 {
1498         if (acpi_gbl_reduced_hardware)
1499                 x86_init.acpi.reduced_hw_early_init();
1500 }
1501
1502 /*
1503  * If your system is blacklisted here, but you find that acpi=force
1504  * works for you, please contact linux-acpi@vger.kernel.org
1505  */
1506 static const struct dmi_system_id acpi_dmi_table[] __initconst = {
1507         /*
1508          * Boxes that need ACPI disabled
1509          */
1510         {
1511          .callback = dmi_disable_acpi,
1512          .ident = "IBM Thinkpad",
1513          .matches = {
1514                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1515                      DMI_MATCH(DMI_BOARD_NAME, "2629H1G"),
1516                      },
1517          },
1518
1519         /*
1520          * Boxes that need ACPI PCI IRQ routing disabled
1521          */
1522         {
1523          .callback = disable_acpi_irq,
1524          .ident = "ASUS A7V",
1525          .matches = {
1526                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC"),
1527                      DMI_MATCH(DMI_BOARD_NAME, "<A7V>"),
1528                      /* newer BIOS, Revision 1011, does work */
1529                      DMI_MATCH(DMI_BIOS_VERSION,
1530                                "ASUS A7V ACPI BIOS Revision 1007"),
1531                      },
1532          },
1533         {
1534                 /*
1535                  * Latest BIOS for IBM 600E (1.16) has bad pcinum
1536                  * for LPC bridge, which is needed for the PCI
1537                  * interrupt links to work. DSDT fix is in bug 5966.
1538                  * 2645, 2646 model numbers are shared with 600/600E/600X
1539                  */
1540          .callback = disable_acpi_irq,
1541          .ident = "IBM Thinkpad 600 Series 2645",
1542          .matches = {
1543                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1544                      DMI_MATCH(DMI_BOARD_NAME, "2645"),
1545                      },
1546          },
1547         {
1548          .callback = disable_acpi_irq,
1549          .ident = "IBM Thinkpad 600 Series 2646",
1550          .matches = {
1551                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1552                      DMI_MATCH(DMI_BOARD_NAME, "2646"),
1553                      },
1554          },
1555         /*
1556          * Boxes that need ACPI PCI IRQ routing and PCI scan disabled
1557          */
1558         {                       /* _BBN 0 bug */
1559          .callback = disable_acpi_pci,
1560          .ident = "ASUS PR-DLS",
1561          .matches = {
1562                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1563                      DMI_MATCH(DMI_BOARD_NAME, "PR-DLS"),
1564                      DMI_MATCH(DMI_BIOS_VERSION,
1565                                "ASUS PR-DLS ACPI BIOS Revision 1010"),
1566                      DMI_MATCH(DMI_BIOS_DATE, "03/21/2003")
1567                      },
1568          },
1569         {
1570          .callback = disable_acpi_pci,
1571          .ident = "Acer TravelMate 36x Laptop",
1572          .matches = {
1573                      DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1574                      DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 360"),
1575                      },
1576          },
1577         /*
1578          * Boxes that need ACPI XSDT use disabled due to corrupted tables
1579          */
1580         {
1581          .callback = disable_acpi_xsdt,
1582          .ident = "Advantech DAC-BJ01",
1583          .matches = {
1584                      DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1585                      DMI_MATCH(DMI_PRODUCT_NAME, "Bearlake CRB Board"),
1586                      DMI_MATCH(DMI_BIOS_VERSION, "V1.12"),
1587                      DMI_MATCH(DMI_BIOS_DATE, "02/01/2011"),
1588                      },
1589          },
1590         {}
1591 };
1592
1593 /* second table for DMI checks that should run after early-quirks */
1594 static const struct dmi_system_id acpi_dmi_table_late[] __initconst = {
1595         /*
1596          * HP laptops which use a DSDT reporting as HP/SB400/10000,
1597          * which includes some code which overrides all temperature
1598          * trip points to 16C if the INTIN2 input of the I/O APIC
1599          * is enabled.  This input is incorrectly designated the
1600          * ISA IRQ 0 via an interrupt source override even though
1601          * it is wired to the output of the master 8259A and INTIN0
1602          * is not connected at all.  Force ignoring BIOS IRQ0
1603          * override in that cases.
1604          */
1605         {
1606          .callback = dmi_ignore_irq0_timer_override,
1607          .ident = "HP nx6115 laptop",
1608          .matches = {
1609                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1610                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6115"),
1611                      },
1612          },
1613         {
1614          .callback = dmi_ignore_irq0_timer_override,
1615          .ident = "HP NX6125 laptop",
1616          .matches = {
1617                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1618                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6125"),
1619                      },
1620          },
1621         {
1622          .callback = dmi_ignore_irq0_timer_override,
1623          .ident = "HP NX6325 laptop",
1624          .matches = {
1625                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1626                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6325"),
1627                      },
1628          },
1629         {
1630          .callback = dmi_ignore_irq0_timer_override,
1631          .ident = "HP 6715b laptop",
1632          .matches = {
1633                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1634                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq 6715b"),
1635                      },
1636          },
1637         {
1638          .callback = dmi_ignore_irq0_timer_override,
1639          .ident = "FUJITSU SIEMENS",
1640          .matches = {
1641                      DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
1642                      DMI_MATCH(DMI_PRODUCT_NAME, "AMILO PRO V2030"),
1643                      },
1644          },
1645         {}
1646 };
1647
1648 /*
1649  * acpi_boot_table_init() and acpi_boot_init()
1650  *  called from setup_arch(), always.
1651  *      1. checksums all tables
1652  *      2. enumerates lapics
1653  *      3. enumerates io-apics
1654  *
1655  * acpi_table_init() is separate to allow reading SRAT without
1656  * other side effects.
1657  *
1658  * side effects of acpi_boot_init:
1659  *      acpi_lapic = 1 if LAPIC found
1660  *      acpi_ioapic = 1 if IOAPIC found
1661  *      if (acpi_lapic && acpi_ioapic) smp_found_config = 1;
1662  *      if acpi_blacklisted() acpi_disabled = 1;
1663  *      acpi_irq_model=...
1664  *      ...
1665  */
1666
1667 void __init acpi_boot_table_init(void)
1668 {
1669         dmi_check_system(acpi_dmi_table);
1670
1671         /*
1672          * If acpi_disabled, bail out
1673          */
1674         if (acpi_disabled)
1675                 return;
1676
1677         /*
1678          * Initialize the ACPI boot-time table parser.
1679          */
1680         if (acpi_locate_initial_tables())
1681                 disable_acpi();
1682         else
1683                 acpi_reserve_initial_tables();
1684 }
1685
1686 int __init early_acpi_boot_init(void)
1687 {
1688         if (acpi_disabled)
1689                 return 1;
1690
1691         acpi_table_init_complete();
1692
1693         acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
1694
1695         /*
1696          * blacklist may disable ACPI entirely
1697          */
1698         if (acpi_blacklisted()) {
1699                 if (acpi_force) {
1700                         pr_warn("acpi=force override\n");
1701                 } else {
1702                         pr_warn("Disabling ACPI support\n");
1703                         disable_acpi();
1704                         return 1;
1705                 }
1706         }
1707
1708         /*
1709          * Process the Multiple APIC Description Table (MADT), if present
1710          */
1711         early_acpi_process_madt();
1712
1713         /*
1714          * Hardware-reduced ACPI mode initialization:
1715          */
1716         acpi_reduced_hw_init();
1717
1718         return 0;
1719 }
1720
1721 int __init acpi_boot_init(void)
1722 {
1723         /* those are executed after early-quirks are executed */
1724         dmi_check_system(acpi_dmi_table_late);
1725
1726         /*
1727          * If acpi_disabled, bail out
1728          */
1729         if (acpi_disabled)
1730                 return 1;
1731
1732         acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
1733
1734         /*
1735          * set sci_int and PM timer address
1736          */
1737         acpi_table_parse(ACPI_SIG_FADT, acpi_parse_fadt);
1738
1739         /*
1740          * Process the Multiple APIC Description Table (MADT), if present
1741          */
1742         acpi_process_madt();
1743
1744         acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
1745         if (IS_ENABLED(CONFIG_ACPI_BGRT) && !acpi_nobgrt)
1746                 acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);
1747
1748         if (!acpi_noirq)
1749                 x86_init.pci.init = pci_acpi_init;
1750
1751         /* Do not enable ACPI SPCR console by default */
1752         acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
1753         return 0;
1754 }
1755
1756 static int __init parse_acpi(char *arg)
1757 {
1758         if (!arg)
1759                 return -EINVAL;
1760
1761         /* "acpi=off" disables both ACPI table parsing and interpreter */
1762         if (strcmp(arg, "off") == 0) {
1763                 disable_acpi();
1764         }
1765         /* acpi=force to over-ride black-list */
1766         else if (strcmp(arg, "force") == 0) {
1767                 acpi_force = 1;
1768                 acpi_disabled = 0;
1769         }
1770         /* acpi=strict disables out-of-spec workarounds */
1771         else if (strcmp(arg, "strict") == 0) {
1772                 acpi_strict = 1;
1773         }
1774         /* acpi=rsdt use RSDT instead of XSDT */
1775         else if (strcmp(arg, "rsdt") == 0) {
1776                 acpi_gbl_do_not_use_xsdt = TRUE;
1777         }
1778         /* "acpi=noirq" disables ACPI interrupt routing */
1779         else if (strcmp(arg, "noirq") == 0) {
1780                 acpi_noirq_set();
1781         }
1782         /* "acpi=copy_dsdt" copies DSDT */
1783         else if (strcmp(arg, "copy_dsdt") == 0) {
1784                 acpi_gbl_copy_dsdt_locally = 1;
1785         }
1786         /* "acpi=nocmcff" disables FF mode for corrected errors */
1787         else if (strcmp(arg, "nocmcff") == 0) {
1788                 acpi_disable_cmcff = 1;
1789         } else {
1790                 /* Core will printk when we return error. */
1791                 return -EINVAL;
1792         }
1793         return 0;
1794 }
1795 early_param("acpi", parse_acpi);
1796
1797 static int __init parse_acpi_bgrt(char *arg)
1798 {
1799         acpi_nobgrt = true;
1800         return 0;
1801 }
1802 early_param("bgrt_disable", parse_acpi_bgrt);
1803
1804 /* FIXME: Using pci= for an ACPI parameter is a travesty. */
1805 static int __init parse_pci(char *arg)
1806 {
1807         if (arg && strcmp(arg, "noacpi") == 0)
1808                 acpi_disable_pci();
1809         return 0;
1810 }
1811 early_param("pci", parse_pci);
1812
1813 int __init acpi_mps_check(void)
1814 {
1815 #if defined(CONFIG_X86_LOCAL_APIC) && !defined(CONFIG_X86_MPPARSE)
1816 /* mptable code is not built-in*/
1817         if (acpi_disabled || acpi_noirq) {
1818                 pr_warn("MPS support code is not built-in, using acpi=off or acpi=noirq or pci=noacpi may have problem\n");
1819                 return 1;
1820         }
1821 #endif
1822         return 0;
1823 }
1824
1825 #ifdef CONFIG_X86_IO_APIC
1826 static int __init parse_acpi_skip_timer_override(char *arg)
1827 {
1828         acpi_skip_timer_override = 1;
1829         return 0;
1830 }
1831 early_param("acpi_skip_timer_override", parse_acpi_skip_timer_override);
1832
1833 static int __init parse_acpi_use_timer_override(char *arg)
1834 {
1835         acpi_use_timer_override = 1;
1836         return 0;
1837 }
1838 early_param("acpi_use_timer_override", parse_acpi_use_timer_override);
1839 #endif /* CONFIG_X86_IO_APIC */
1840
1841 static int __init setup_acpi_sci(char *s)
1842 {
1843         if (!s)
1844                 return -EINVAL;
1845         if (!strcmp(s, "edge"))
1846                 acpi_sci_flags =  ACPI_MADT_TRIGGER_EDGE |
1847                         (acpi_sci_flags & ~ACPI_MADT_TRIGGER_MASK);
1848         else if (!strcmp(s, "level"))
1849                 acpi_sci_flags = ACPI_MADT_TRIGGER_LEVEL |
1850                         (acpi_sci_flags & ~ACPI_MADT_TRIGGER_MASK);
1851         else if (!strcmp(s, "high"))
1852                 acpi_sci_flags = ACPI_MADT_POLARITY_ACTIVE_HIGH |
1853                         (acpi_sci_flags & ~ACPI_MADT_POLARITY_MASK);
1854         else if (!strcmp(s, "low"))
1855                 acpi_sci_flags = ACPI_MADT_POLARITY_ACTIVE_LOW |
1856                         (acpi_sci_flags & ~ACPI_MADT_POLARITY_MASK);
1857         else
1858                 return -EINVAL;
1859         return 0;
1860 }
1861 early_param("acpi_sci", setup_acpi_sci);
1862
1863 int __acpi_acquire_global_lock(unsigned int *lock)
1864 {
1865         unsigned int old, new, val;
1866
1867         old = READ_ONCE(*lock);
1868         do {
1869                 val = (old >> 1) & 0x1;
1870                 new = (old & ~0x3) + 2 + val;
1871         } while (!try_cmpxchg(lock, &old, new));
1872
1873         if (val)
1874                 return 0;
1875
1876         return -1;
1877 }
1878
1879 int __acpi_release_global_lock(unsigned int *lock)
1880 {
1881         unsigned int old, new;
1882
1883         old = READ_ONCE(*lock);
1884         do {
1885                 new = old & ~0x3;
1886         } while (!try_cmpxchg(lock, &old, new));
1887         return old & 0x1;
1888 }
1889
1890 void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
1891 {
1892         e820__range_add(addr, size, E820_TYPE_NVS);
1893         e820__update_table_print();
1894 }
1895
1896 void x86_default_set_root_pointer(u64 addr)
1897 {
1898         boot_params.acpi_rsdp_addr = addr;
1899 }
1900
1901 u64 x86_default_get_root_pointer(void)
1902 {
1903         return boot_params.acpi_rsdp_addr;
1904 }