2 * acpi.c - Architecture-Specific Low-Level ACPI Support
4 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
5 * Copyright (C) 2001 Jun Nakajima <jun.nakajima@intel.com>
6 * Copyright (C) 2001 Patrick Mochel <mochel@osdl.org>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/stddef.h>
32 #include <linux/slab.h>
33 #include <linux/pci.h>
34 #include <linux/bootmem.h>
35 #include <linux/irq.h>
36 #include <linux/acpi.h>
37 #include <asm/mpspec.h>
40 #include <asm/apicdef.h>
42 #include <asm/pgtable.h>
43 #include <asm/pgalloc.h>
44 #include <asm/io_apic.h>
45 #include <asm/proto.h>
48 #define PREFIX "ACPI: "
54 acpi_interrupt_flags acpi_sci_flags __initdata;
55 int acpi_sci_override_gsi __initdata;
56 int acpi_skip_timer_override __initdata;
57 /* --------------------------------------------------------------------------
58 Boot-time Configuration
59 -------------------------------------------------------------------------- */
60 #ifdef CONFIG_ACPI_PCI
61 int acpi_noirq __initdata; /* skip ACPI IRQ initialization */
62 int acpi_pci_disabled __initdata; /* skip ACPI PCI scan and IRQ initialization */
64 int acpi_ht __initdata = 1; /* enable HT */
66 enum acpi_irq_model_id acpi_irq_model;
70 * Use reserved fixmap pages for physical-to-virtual mappings of ACPI tables.
71 * Note that the same range is used for each table, so tables that need to
72 * persist should be memcpy'd.
75 extern unsigned long end_pfn;
79 unsigned long phys_addr,
83 unsigned long base = 0;
84 unsigned long mapped_phys = phys_addr;
85 unsigned long offset = phys_addr & (PAGE_SIZE - 1);
86 unsigned long mapped_size = PAGE_SIZE - offset;
87 unsigned long avail_size = mapped_size + (PAGE_SIZE * FIX_ACPI_PAGES);
88 int idx = FIX_ACPI_BEGIN;
91 //printk("__acpi_map_table at %lx size %lu\n", phys_addr, size);
93 if (!phys_addr || !size)
96 if (phys_addr < (end_pfn_map << PAGE_SHIFT))
97 return __va(phys_addr);
99 panic("acpi mapping beyond end_pfn: %lx > %lx\n", phys_addr, end_pfn_map<<PAGE_SHIFT);
103 base = fix_to_virt(FIX_ACPI_BEGIN);
105 set_fixmap(idx, mapped_phys);
107 if (size > avail_size)
110 /* If the table doesn't map completely into the fist page... */
111 if (size > mapped_size) {
113 /* Make sure we don't go past our range */
114 if (idx++ == FIX_ACPI_END)
116 mapped_phys = mapped_phys + PAGE_SIZE;
117 set_fixmap(idx, mapped_phys);
118 mapped_size = mapped_size + PAGE_SIZE;
119 } while (mapped_size < size);
122 return ((unsigned char *) base + offset);
126 #ifdef CONFIG_ACPI_MMCONFIG
128 u32 pci_mmcfg_base_addr;
131 acpi_parse_mcfg(unsigned long phys_addr,
134 struct acpi_table_mcfg *mcfg = NULL;
136 if (!phys_addr || !size)
139 mcfg = (struct acpi_table_mcfg *) __acpi_map_table(phys_addr, size);
141 printk(KERN_WARNING PREFIX "Unable to map MCFG\n");
145 if (mcfg->base_reserved) {
146 printk(KERN_ERR PREFIX "MMCONFIG not in low 4GB of memory\n");
150 pci_mmcfg_base_addr = mcfg->base_address;
154 #endif /* CONFIG_ACPI_MMCONFIG */
156 #ifdef CONFIG_X86_LOCAL_APIC
158 static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
162 unsigned long phys_addr,
165 struct acpi_table_madt *madt = NULL;
167 if (!phys_addr || !size)
170 madt = (struct acpi_table_madt *) __acpi_map_table(phys_addr, size);
172 printk(KERN_WARNING PREFIX "Unable to map MADT\n");
176 if (madt->lapic_address)
177 acpi_lapic_addr = (u64) madt->lapic_address;
179 printk(KERN_INFO PREFIX "Local APIC address 0x%08x\n",
180 madt->lapic_address);
188 acpi_table_entry_header *header)
190 struct acpi_table_lapic *processor = NULL;
192 processor = (struct acpi_table_lapic*) header;
196 acpi_table_print_madt_entry(header);
199 processor->id, /* APIC ID */
200 processor->flags.enabled); /* Enabled? */
207 acpi_parse_lapic_addr_ovr (
208 acpi_table_entry_header *header)
210 struct acpi_table_lapic_addr_ovr *lapic_addr_ovr = NULL;
212 lapic_addr_ovr = (struct acpi_table_lapic_addr_ovr*) header;
216 acpi_lapic_addr = lapic_addr_ovr->address;
223 acpi_parse_lapic_nmi (
224 acpi_table_entry_header *header)
226 struct acpi_table_lapic_nmi *lapic_nmi = NULL;
228 lapic_nmi = (struct acpi_table_lapic_nmi*) header;
232 acpi_table_print_madt_entry(header);
234 if (lapic_nmi->lint != 1)
235 printk(KERN_WARNING PREFIX "NMI not connected to LINT 1!\n");
240 #endif /*CONFIG_X86_LOCAL_APIC*/
242 #if defined(CONFIG_X86_IO_APIC) && defined(CONFIG_ACPI_INTERPRETER)
247 acpi_table_entry_header *header)
249 struct acpi_table_ioapic *ioapic = NULL;
251 ioapic = (struct acpi_table_ioapic*) header;
255 acpi_table_print_madt_entry(header);
260 ioapic->global_irq_base);
266 * Parse Interrupt Source Override for the ACPI SCI
269 acpi_sci_ioapic_setup(u32 gsi, u16 polarity, u16 trigger)
271 if (trigger == 0) /* compatible SCI trigger is level */
274 if (polarity == 0) /* compatible SCI polarity is low */
277 /* Command-line over-ride via acpi_sci= */
278 if (acpi_sci_flags.trigger)
279 trigger = acpi_sci_flags.trigger;
281 if (acpi_sci_flags.polarity)
282 polarity = acpi_sci_flags.polarity;
285 * mp_config_acpi_legacy_irqs() already setup IRQs < 16
286 * If GSI is < 16, this will update its flags,
287 * else it will create a new mp_irqs[] entry.
289 mp_override_legacy_irq(gsi, polarity, trigger, gsi);
292 * stash over-ride to indicate we've been here
293 * and for later update of acpi_fadt
295 acpi_sci_override_gsi = gsi;
300 acpi_parse_fadt(unsigned long phys, unsigned long size)
302 struct fadt_descriptor_rev2 *fadt =0;
304 fadt = (struct fadt_descriptor_rev2*) __acpi_map_table(phys,size);
306 printk(KERN_WARNING PREFIX "Unable to map FADT\n");
310 #ifdef CONFIG_ACPI_INTERPRETER
311 /* initialize sci_int early for INT_SRC_OVR MADT parsing */
312 acpi_fadt.sci_int = fadt->sci_int;
320 acpi_parse_int_src_ovr (
321 acpi_table_entry_header *header)
323 struct acpi_table_int_src_ovr *intsrc = NULL;
325 intsrc = (struct acpi_table_int_src_ovr*) header;
329 acpi_table_print_madt_entry(header);
331 if (intsrc->bus_irq == acpi_fadt.sci_int) {
332 acpi_sci_ioapic_setup(intsrc->global_irq,
333 intsrc->flags.polarity, intsrc->flags.trigger);
337 if (acpi_skip_timer_override &&
338 intsrc->bus_irq == 0 && intsrc->global_irq == 2) {
339 printk(PREFIX "BIOS IRQ0 pin2 override ignored.\n");
343 mp_override_legacy_irq (
345 intsrc->flags.polarity,
346 intsrc->flags.trigger,
355 acpi_table_entry_header *header)
357 struct acpi_table_nmi_src *nmi_src = NULL;
359 nmi_src = (struct acpi_table_nmi_src*) header;
363 acpi_table_print_madt_entry(header);
365 /* TBD: Support nimsrc entries? */
370 #endif /*CONFIG_X86_IO_APIC && CONFIG_ACPI_INTERPRETER*/
372 #ifdef CONFIG_HPET_TIMER
375 unsigned long phys_addr,
378 struct acpi_table_hpet *hpet_tbl;
380 hpet_tbl = __va(phys_addr);
382 if (hpet_tbl->addr.space_id != ACPI_SPACE_MEM) {
383 printk(KERN_WARNING "acpi: HPET timers must be located in memory.\n");
387 hpet_address = hpet_tbl->addr.addrl | ((long) hpet_tbl->addr.addrh << 32);
389 printk(KERN_INFO "acpi: HPET id: %#x base: %#lx\n", hpet_tbl->id, hpet_address);
393 #endif /* CONFIG_HPET_TIMER */
395 #ifdef CONFIG_ACPI_BUS
397 * acpi_pic_sci_set_trigger()
399 * use ELCR to set PIC-mode trigger type for SCI
401 * If a PIC-mode SCI is not recognized or gives spurious IRQ7's
402 * it may require Edge Trigger -- use "acpi_sci=edge"
404 * Port 0x4d0-4d1 are ECLR1 and ECLR2, the Edge/Level Control Registers
405 * for the 8259 PIC. bit[n] = 1 means irq[n] is Level, otherwise Edge.
406 * ECLR1 is IRQ's 0-7 (IRQ 0, 1, 2 must be 0)
407 * ECLR2 is IRQ's 8-15 (IRQ 8, 13 must be 0)
411 acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger)
413 unsigned char mask = 1 << (irq & 7);
414 unsigned int port = 0x4d0 + (irq >> 3);
415 unsigned char val = inb(port);
418 printk(PREFIX "IRQ%d SCI:", irq);
423 printk(" set to Level");
424 outb(val | mask, port);
430 printk(" set to Edge");
431 outb(val & ~mask, port);
434 printk(" Trigger.\n");
436 #endif /* CONFIG_ACPI_BUS */
438 static unsigned long __init
441 unsigned long length)
443 unsigned long offset = 0;
444 unsigned long sig_len = sizeof("RSD PTR ") - 1;
447 * Scan all 16-byte boundaries of the physical memory region for the
450 for (offset = 0; offset < length; offset += 16) {
451 if (strncmp((char *) (start + offset), "RSD PTR ", sig_len))
453 return (start + offset);
461 acpi_find_rsdp (void)
463 unsigned long rsdp_phys = 0;
466 * Scan memory looking for the RSDP signature. First search EBDA (low
467 * memory) paragraphs and then search upper memory (E0000-FFFFF).
469 rsdp_phys = acpi_scan_rsdp (0, 0x400);
471 rsdp_phys = acpi_scan_rsdp (0xE0000, 0xFFFFF);
478 acpi_boot_init (void)
486 * The default interrupt routing model is PIC (8259). This gets
487 * overriden if IOAPICs are enumerated (below).
489 acpi_irq_model = ACPI_IRQ_MODEL_PIC;
492 * Initialize the ACPI boot-time table parser.
494 result = acpi_table_init();
500 #ifdef CONFIG_ACPI_MMCONFIG
501 result = acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
503 printk(KERN_ERR PREFIX "Error %d parsing MCFG\n", result);
504 } else if (result > 1) {
505 printk(KERN_WARNING PREFIX "Multiple MCFG tables exist\n");
509 #ifdef CONFIG_X86_LOCAL_APIC
514 * Parse the Multiple APIC Description Table (MADT), if exists.
515 * Note that this table provides platform SMP configuration
516 * information -- the successor to MPS tables.
519 result = acpi_table_parse(ACPI_APIC, acpi_parse_madt);
521 printk(KERN_WARNING PREFIX "MADT not present\n");
524 else if (result < 0) {
525 printk(KERN_ERR PREFIX "Error parsing MADT\n");
529 printk(KERN_WARNING PREFIX "Multiple MADT tables exist\n");
534 * Note that the LAPIC address is obtained from the MADT (32-bit value)
535 * and (optionally) overriden by a LAPIC_ADDR_OVR entry (64-bit value).
538 result = acpi_table_parse_madt(ACPI_MADT_LAPIC_ADDR_OVR, acpi_parse_lapic_addr_ovr);
540 printk(KERN_ERR PREFIX "Error parsing LAPIC address override entry\n");
544 mp_register_lapic_address(acpi_lapic_addr);
546 result = acpi_table_parse_madt(ACPI_MADT_LAPIC, acpi_parse_lapic);
548 printk(KERN_ERR PREFIX "No LAPIC entries present\n");
549 /* TBD: Cleanup to allow fallback to MPS */
552 else if (result < 0) {
553 printk(KERN_ERR PREFIX "Error parsing LAPIC entry\n");
554 /* TBD: Cleanup to allow fallback to MPS */
558 result = acpi_table_parse_madt(ACPI_MADT_LAPIC_NMI, acpi_parse_lapic_nmi);
560 printk(KERN_ERR PREFIX "Error parsing LAPIC NMI entry\n");
561 /* TBD: Cleanup to allow fallback to MPS */
567 #endif /*CONFIG_X86_LOCAL_APIC*/
569 #if defined(CONFIG_X86_IO_APIC) && defined(CONFIG_ACPI_INTERPRETER)
573 * if "noapic" boot option, don't look for IO-APICs
575 if (ioapic_setup_disabled()) {
576 printk(KERN_INFO PREFIX "Skipping IOAPIC probe "
577 "due to 'noapic' option.\n");
587 * ACPI interpreter is required to complete interrupt setup,
588 * so if it is off, don't enumerate the io-apics with ACPI.
589 * If MPS is present, it will handle them,
590 * otherwise the system will stay in PIC mode
592 if (acpi_disabled || acpi_noirq) {
597 * if "noapic" boot option, don't look for IO-APICs
599 if (ioapic_setup_disabled()) {
600 printk(KERN_INFO PREFIX "Skipping IOAPIC probe "
601 "due to 'noapic' option.\n");
605 result = acpi_table_parse_madt(ACPI_MADT_IOAPIC, acpi_parse_ioapic);
607 printk(KERN_ERR PREFIX "No IOAPIC entries present\n");
610 else if (result < 0) {
611 printk(KERN_ERR PREFIX "Error parsing IOAPIC entry\n");
615 /* Record sci_int for use when looking for MADT sci_int override */
616 acpi_table_parse(ACPI_FADT, acpi_parse_fadt);
618 result = acpi_table_parse_madt(ACPI_MADT_INT_SRC_OVR, acpi_parse_int_src_ovr);
620 printk(KERN_ERR PREFIX "Error parsing interrupt source overrides entry\n");
621 /* TBD: Cleanup to allow fallback to MPS */
626 * If BIOS did not supply an INT_SRC_OVR for the SCI
627 * pretend we got one so we can set the SCI flags.
629 if (!acpi_sci_override_gsi)
630 acpi_sci_ioapic_setup(acpi_fadt.sci_int, 0, 0);
632 /* Fill in identity legacy mapings where no override */
633 mp_config_acpi_legacy_irqs();
635 result = acpi_table_parse_madt(ACPI_MADT_NMI_SRC, acpi_parse_nmi_src);
637 printk(KERN_ERR PREFIX "Error parsing NMI SRC entry\n");
638 /* TBD: Cleanup to allow fallback to MPS */
642 acpi_irq_model = ACPI_IRQ_MODEL_IOAPIC;
644 acpi_irq_balance_set(NULL);
648 if (acpi_lapic && acpi_ioapic)
649 smp_found_config = 1;
651 #ifdef CONFIG_HPET_TIMER
652 result = acpi_table_parse(ACPI_HPET, acpi_parse_hpet);
654 printk("ACPI: no HPET table found (%d).\n", result);
657 #endif /*CONFIG_X86_IO_APIC && CONFIG_ACPI_INTERPRETER*/
663 /* --------------------------------------------------------------------------
664 Low-Level Sleep Support
665 -------------------------------------------------------------------------- */
667 #ifdef CONFIG_ACPI_SLEEP
669 /* address in low memory of the wakeup routine. */
670 unsigned long acpi_wakeup_address = 0;
671 extern char wakeup_start, wakeup_end;
673 extern unsigned long FASTCALL(acpi_copy_wakeup_routine(unsigned long));
675 static void init_low_mapping(void)
677 cpu_pda[0].level4_pgt[0] = cpu_pda[0].level4_pgt[pml4_index(PAGE_OFFSET)];
682 * acpi_save_state_mem - save kernel state
684 * Create an identity mapped page table and copy the wakeup routine to
687 int acpi_save_state_mem (void)
691 memcpy((void *) acpi_wakeup_address, &wakeup_start, &wakeup_end - &wakeup_start);
692 acpi_copy_wakeup_routine(acpi_wakeup_address);
698 * acpi_save_state_disk - save kernel state to disk
701 int acpi_save_state_disk (void)
709 void acpi_restore_state_mem (void)
711 cpu_pda[0].level4_pgt[0] = 0;
716 * acpi_reserve_bootmem - do _very_ early ACPI initialisation
718 * We allocate a page in low memory for the wakeup
719 * routine for when we come back from a sleep state. The
720 * runtime allocator allows specification of <16M pages, but not
723 void __init acpi_reserve_bootmem(void)
725 acpi_wakeup_address = (unsigned long)alloc_bootmem_low(PAGE_SIZE);
726 if ((&wakeup_end - &wakeup_start) > PAGE_SIZE)
727 printk(KERN_CRIT "ACPI: Wakeup code way too big, will crash on attempt to suspend\n");
728 printk(KERN_DEBUG "ACPI: have wakeup address 0x%8.8lx\n", acpi_wakeup_address);
731 #endif /*CONFIG_ACPI_SLEEP*/