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