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