2 * acpi.c - Architecture-Specific Low-Level ACPI Support
4 * Copyright (C) 1999 VA Linux Systems
5 * Copyright (C) 1999,2000 Walt Drummond <drummond@valinux.com>
6 * Copyright (C) 2000, 2002-2003 Hewlett-Packard Co.
7 * David Mosberger-Tang <davidm@hpl.hp.com>
8 * Copyright (C) 2000 Intel Corp.
9 * Copyright (C) 2000,2001 J.I. Lee <jung-ik.lee@intel.com>
10 * Copyright (C) 2001 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
11 * Copyright (C) 2001 Jenna Hall <jenna.s.hall@intel.com>
12 * Copyright (C) 2001 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
13 * Copyright (C) 2002 Erich Focht <efocht@ess.nec.de>
15 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/smp.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
42 #include <linux/irq.h>
43 #include <linux/acpi.h>
44 #include <linux/efi.h>
45 #include <linux/mmzone.h>
47 #include <asm/iosapic.h>
48 #include <asm/machvec.h>
50 #include <asm/system.h>
54 #define PREFIX "ACPI: "
56 void (*pm_idle) (void);
57 EXPORT_SYMBOL(pm_idle);
58 void (*pm_power_off) (void);
60 unsigned char acpi_kbd_controller_present = 1;
61 unsigned char acpi_legacy_devices;
63 int acpi_disabled; /* XXX this shouldn't be needed---we can't boot without ACPI! */
66 acpi_get_sysname (void)
68 #ifdef CONFIG_IA64_GENERIC
69 unsigned long rsdp_phys;
70 struct acpi20_table_rsdp *rsdp;
71 struct acpi_table_xsdt *xsdt;
72 struct acpi_table_header *hdr;
74 rsdp_phys = acpi_find_rsdp();
76 printk(KERN_ERR "ACPI 2.0 RSDP not found, default to \"dig\"\n");
80 rsdp = (struct acpi20_table_rsdp *) __va(rsdp_phys);
81 if (strncmp(rsdp->signature, RSDP_SIG, sizeof(RSDP_SIG) - 1)) {
82 printk(KERN_ERR "ACPI 2.0 RSDP signature incorrect, default to \"dig\"\n");
86 xsdt = (struct acpi_table_xsdt *) __va(rsdp->xsdt_address);
88 if (strncmp(hdr->signature, XSDT_SIG, sizeof(XSDT_SIG) - 1)) {
89 printk(KERN_ERR "ACPI 2.0 XSDT signature incorrect, default to \"dig\"\n");
93 if (!strcmp(hdr->oem_id, "HP")) {
96 else if (!strcmp(hdr->oem_id, "SGI")) {
102 # if defined (CONFIG_IA64_HP_SIM)
104 # elif defined (CONFIG_IA64_HP_ZX1)
106 # elif defined (CONFIG_IA64_SGI_SN2)
108 # elif defined (CONFIG_IA64_DIG)
111 # error Unknown platform. Fix acpi.c.
116 #ifdef CONFIG_ACPI_BOOT
118 #define ACPI_MAX_PLATFORM_INTERRUPTS 256
120 /* Array to record platform interrupt vectors for generic interrupt routing. */
121 int platform_intr_list[ACPI_MAX_PLATFORM_INTERRUPTS] = {
122 [0 ... ACPI_MAX_PLATFORM_INTERRUPTS - 1] = -1
125 enum acpi_irq_model_id acpi_irq_model = ACPI_IRQ_MODEL_IOSAPIC;
128 * Interrupt routing API for device drivers. Provides interrupt vector for
129 * a generic platform event. Currently only CPEI is implemented.
132 acpi_request_vector (u32 int_type)
136 if (int_type < ACPI_MAX_PLATFORM_INTERRUPTS) {
137 /* corrected platform error interrupt */
138 vector = platform_intr_list[int_type];
140 printk(KERN_ERR "acpi_request_vector(): invalid interrupt type\n");
145 __acpi_map_table (unsigned long phys_addr, unsigned long size)
147 return __va(phys_addr);
150 /* --------------------------------------------------------------------------
151 Boot-time Table Parsing
152 -------------------------------------------------------------------------- */
154 static int total_cpus __initdata;
155 static int available_cpus __initdata;
156 struct acpi_table_madt * acpi_madt __initdata;
161 acpi_parse_lapic_addr_ovr (acpi_table_entry_header *header)
163 struct acpi_table_lapic_addr_ovr *lapic;
165 lapic = (struct acpi_table_lapic_addr_ovr *) header;
169 acpi_table_print_madt_entry(header);
171 if (lapic->address) {
172 iounmap((void *) ipi_base_addr);
173 ipi_base_addr = (unsigned long) ioremap(lapic->address, 0);
180 acpi_parse_lsapic (acpi_table_entry_header *header)
182 struct acpi_table_lsapic *lsapic;
184 lsapic = (struct acpi_table_lsapic *) header;
188 acpi_table_print_madt_entry(header);
190 printk(KERN_INFO "CPU %d (0x%04x)", total_cpus, (lsapic->id << 8) | lsapic->eid);
192 if (!lsapic->flags.enabled)
197 smp_boot_data.cpu_phys_id[available_cpus] = (lsapic->id << 8) | lsapic->eid;
198 if (hard_smp_processor_id()
199 == (unsigned int) smp_boot_data.cpu_phys_id[available_cpus])
213 acpi_parse_lapic_nmi (acpi_table_entry_header *header)
215 struct acpi_table_lapic_nmi *lacpi_nmi;
217 lacpi_nmi = (struct acpi_table_lapic_nmi*) header;
221 acpi_table_print_madt_entry(header);
223 /* TBD: Support lapic_nmi entries */
229 acpi_parse_iosapic (acpi_table_entry_header *header)
231 struct acpi_table_iosapic *iosapic;
233 iosapic = (struct acpi_table_iosapic *) header;
237 acpi_table_print_madt_entry(header);
239 iosapic_init(iosapic->address, iosapic->global_irq_base);
246 acpi_parse_plat_int_src (acpi_table_entry_header *header)
248 struct acpi_table_plat_int_src *plintsrc;
251 plintsrc = (struct acpi_table_plat_int_src *) header;
255 acpi_table_print_madt_entry(header);
258 * Get vector assignment for this interrupt, set attributes,
259 * and program the IOSAPIC routing table.
261 vector = iosapic_register_platform_intr(plintsrc->type,
262 plintsrc->global_irq,
263 plintsrc->iosapic_vector,
266 (plintsrc->flags.polarity == 1) ? IOSAPIC_POL_HIGH : IOSAPIC_POL_LOW,
267 (plintsrc->flags.trigger == 1) ? IOSAPIC_EDGE : IOSAPIC_LEVEL);
269 platform_intr_list[plintsrc->type] = vector;
275 acpi_parse_int_src_ovr (acpi_table_entry_header *header)
277 struct acpi_table_int_src_ovr *p;
279 p = (struct acpi_table_int_src_ovr *) header;
283 acpi_table_print_madt_entry(header);
285 iosapic_override_isa_irq(p->bus_irq, p->global_irq,
286 (p->flags.polarity == 1) ? IOSAPIC_POL_HIGH : IOSAPIC_POL_LOW,
287 (p->flags.trigger == 1) ? IOSAPIC_EDGE : IOSAPIC_LEVEL);
293 acpi_parse_nmi_src (acpi_table_entry_header *header)
295 struct acpi_table_nmi_src *nmi_src;
297 nmi_src = (struct acpi_table_nmi_src*) header;
301 acpi_table_print_madt_entry(header);
303 /* TBD: Support nimsrc entries */
309 acpi_parse_madt (unsigned long phys_addr, unsigned long size)
311 if (!phys_addr || !size)
314 acpi_madt = (struct acpi_table_madt *) __va(phys_addr);
316 /* remember the value for reference after free_initmem() */
317 #ifdef CONFIG_ITANIUM
318 has_8259 = 1; /* Firmware on old Itanium systems is broken */
320 has_8259 = acpi_madt->flags.pcat_compat;
322 iosapic_system_init(has_8259);
324 /* Get base address of IPI Message Block */
326 if (acpi_madt->lapic_address)
327 ipi_base_addr = (unsigned long) ioremap(acpi_madt->lapic_address, 0);
329 printk(KERN_INFO PREFIX "Local APIC address 0x%lx\n", ipi_base_addr);
334 #ifdef CONFIG_ACPI_NUMA
338 #define PXM_FLAG_LEN ((MAX_PXM_DOMAINS + 1)/32)
339 #define PXM_MAGIC (255)
341 static int __initdata srat_num_cpus; /* number of cpus */
342 static u32 __initdata pxm_flag[PXM_FLAG_LEN];
343 static u32 __initdata mpxm_flag[PXM_FLAG_LEN];
344 #define pxm_bit_set(bit) (set_bit(bit,(void *)pxm_flag))
345 #define pxm_bit_clear(bit) (clear_bit(bit, (void *)pxm_flag))
346 #define pxm_bit_test(bit) (test_bit(bit,(void *)pxm_flag))
347 #define mpxm_bit_set(bit) (set_bit(bit, (void *) mpxm_flag))
348 #define mpxm_bit_test(bit) (test_bit(bit, (void *) mpxm_flag))
349 /* maps to convert between proximity domain and logical node ID */
350 int __initdata pxm_to_nid_map[MAX_PXM_DOMAINS];
351 int __initdata nid_to_pxm_map[MAX_NUMNODES];
352 static struct acpi_table_slit __initdata *slit_table;
355 * ACPI 2.0 SLIT (System Locality Information Table)
356 * http://devresource.hp.com/devresource/Docs/TechPapers/IA64/slit.pdf
359 acpi_numa_slit_init (struct acpi_table_slit *slit)
363 len = sizeof(struct acpi_table_header) + 8
364 + slit->localities * slit->localities;
365 if (slit->header.length != len) {
366 printk(KERN_ERR "ACPI 2.0 SLIT: size mismatch: %d expected, %d actual\n",
367 len, slit->header.length);
368 memset(numa_slit, 10, sizeof(numa_slit));
375 acpi_numa_processor_affinity_init (struct acpi_table_processor_affinity *pa)
377 /* record this node in proximity bitmap */
378 pxm_bit_set(pa->proximity_domain);
380 node_cpuid[srat_num_cpus].phys_id = (pa->apic_id << 8) | (pa->lsapic_eid);
381 /* nid should be overridden as logical node id later */
382 node_cpuid[srat_num_cpus].nid = pa->proximity_domain;
387 acpi_numa_memory_affinity_init (struct acpi_table_memory_affinity *ma)
389 unsigned long paddr, size;
391 struct node_memblk_s *p, *q, *pend;
393 pxm = ma->proximity_domain;
395 /* fill node memory chunk structure */
396 paddr = ma->base_addr_hi;
397 paddr = (paddr << 32) | ma->base_addr_lo;
398 size = ma->length_hi;
399 size = (size << 32) | ma->length_lo;
401 /* Ignore disabled entries */
402 if (!ma->flags.enabled)
405 /* record this node in proximity bitmap */
408 /* Insertion sort based on base address */
409 pend = &node_memblk[num_node_memblks];
410 for (p = &node_memblk[0]; p < pend; p++) {
411 if (paddr < p->start_paddr)
415 for (q = pend - 1; q >= p; q--)
418 p->start_paddr = paddr;
425 acpi_pxm_magic_slit_fix(void)
429 #define SLIT_IDENTITY 10
432 if (!pxm_bit_test(PXM_MAGIC) || slit_table->localities >= PXM_MAGIC)
435 nid = pxm_to_nid_map[PXM_MAGIC];
437 for (distance = SLIT_IDENTITY*2, i = 0; i < slit_table->localities; i++) {
438 if (!pxm_bit_test(i))
440 for (j = 0; j < slit_table->localities; j++) {
441 if (!pxm_bit_test(j) || (i == j))
444 x = (slit_table->entry[i*slit_table->localities + j] + SLIT_IDENTITY) / 2;
445 distance = min(x, distance);
450 * Fill in distances for PXM magic.
453 for (i = 0; i < numnodes; i++)
454 node_distance(i, nid) = distance;
456 for (i = 0; i < (numnodes - 1); i++)
457 node_distance(nid, i) = distance;
459 node_distance(nid, nid) = SLIT_IDENTITY;
466 acpi_pxm_magic_fix(void)
468 struct node_memblk_s *p;
469 int i, nnode, nid, cpu, pxm;
473 * If every nid has memory then we are done.
476 for (nnode = 0, p = &node_memblk[0]; p < &node_memblk[num_node_memblks]; p++)
477 if (!mpxm_bit_test(p->nid)) {
478 mpxm_bit_set(p->nid);
483 * All nids with memory.
486 if (nnode == numnodes)
490 * Change logical node id for nids without memory.
491 * If we are removing a nid without memory, then
492 * move that nid's cpus to nnode-1 which will become
493 * the magic PXM's logical node id. The node_cpu[X].nid
494 * is the PXM but will change later to logical node
498 for (nid = 0, i = 0; i < numnodes; i++)
499 if (mpxm_bit_test(i)) {
505 for (p = &node_memblk[0]; p < &node_memblk[num_node_memblks]; p++)
509 pxm = nid_to_pxm_map[i];
510 pxm_to_nid_map[pxm] = nid;
511 nid_to_pxm_map[nid] = pxm;
515 for (cpu = 0; cpu < srat_num_cpus; cpu++)
516 if (node_cpuid[cpu].nid == nid_to_pxm_map[i])
517 node_cpuid[cpu].nid = PXM_MAGIC;
519 pxm_to_nid_map[i] = nnode - 1;
520 pxm_bit_clear(nid_to_pxm_map[i]);
529 acpi_numa_arch_fixup (void)
531 int i, j, node_from, node_to;
533 /* If there's no SRAT, fix the phys_id */
534 if (srat_num_cpus == 0) {
535 node_cpuid[0].phys_id = hard_smp_processor_id();
539 /* calculate total number of nodes in system from PXM bitmap */
540 numnodes = 0; /* init total nodes in system */
542 memset(pxm_to_nid_map, -1, sizeof(pxm_to_nid_map));
543 memset(nid_to_pxm_map, -1, sizeof(nid_to_pxm_map));
544 for (i = 0; i < MAX_PXM_DOMAINS; i++) {
545 if (pxm_bit_test(i)) {
546 pxm_to_nid_map[i] = numnodes;
547 nid_to_pxm_map[numnodes++] = i;
551 /* set logical node id in memory chunk structure */
552 for (i = 0; i < num_node_memblks; i++)
553 node_memblk[i].nid = pxm_to_nid_map[node_memblk[i].nid];
555 acpi_pxm_magic_fix();
557 /* assign memory bank numbers for each chunk on each node */
558 for (i = 0; i < numnodes; i++) {
562 for (j = 0; j < num_node_memblks; j++)
563 if (node_memblk[j].nid == i)
564 node_memblk[j].bank = bank++;
567 /* set logical node id in cpu structure */
568 for (i = 0; i < srat_num_cpus; i++)
569 node_cpuid[i].nid = pxm_to_nid_map[node_cpuid[i].nid];
571 printk(KERN_INFO "Number of logical nodes in system = %d\n", numnodes);
572 printk(KERN_INFO "Number of memory chunks in system = %d\n", num_node_memblks);
577 memset(numa_slit, -1, sizeof(numa_slit));
579 acpi_pxm_magic_slit_fix();
581 for (i=0; i<slit_table->localities; i++) {
582 if (!pxm_bit_test(i))
584 node_from = pxm_to_nid_map[i];
585 for (j=0; j<slit_table->localities; j++) {
586 if (!pxm_bit_test(j))
588 node_to = pxm_to_nid_map[j];
589 node_distance(node_from, node_to) =
590 slit_table->entry[i*slit_table->localities + j];
595 printk("ACPI 2.0 SLIT locality table:\n");
596 for (i = 0; i < numnodes; i++) {
597 for (j = 0; j < numnodes; j++)
598 printk("%03d ", node_distance(i,j));
603 #endif /* CONFIG_ACPI_NUMA */
606 acpi_parse_fadt (unsigned long phys_addr, unsigned long size)
608 struct acpi_table_header *fadt_header;
609 struct fadt_descriptor_rev2 *fadt;
611 if (!phys_addr || !size)
614 fadt_header = (struct acpi_table_header *) __va(phys_addr);
615 if (fadt_header->revision != 3)
616 return -ENODEV; /* Only deal with ACPI 2.0 FADT */
618 fadt = (struct fadt_descriptor_rev2 *) fadt_header;
620 if (!(fadt->iapc_boot_arch & BAF_8042_KEYBOARD_CONTROLLER))
621 acpi_kbd_controller_present = 0;
623 if (fadt->iapc_boot_arch & BAF_LEGACY_DEVICES)
624 acpi_legacy_devices = 1;
626 acpi_register_irq(fadt->sci_int, ACPI_ACTIVE_LOW, ACPI_LEVEL_SENSITIVE);
632 acpi_find_rsdp (void)
634 unsigned long rsdp_phys = 0;
637 rsdp_phys = __pa(efi.acpi20);
639 printk(KERN_WARNING PREFIX "v1.0/r0.71 tables no longer supported\n");
645 acpi_boot_init (void)
651 * Parse the Multiple APIC Description Table (MADT), if exists.
652 * Note that this table provides platform SMP configuration
653 * information -- the successor to MPS tables.
656 if (acpi_table_parse(ACPI_APIC, acpi_parse_madt) < 1) {
657 printk(KERN_ERR PREFIX "Can't find MADT\n");
663 if (acpi_table_parse_madt(ACPI_MADT_LAPIC_ADDR_OVR, acpi_parse_lapic_addr_ovr, 0) < 0)
664 printk(KERN_ERR PREFIX "Error parsing LAPIC address override entry\n");
666 if (acpi_table_parse_madt(ACPI_MADT_LSAPIC, acpi_parse_lsapic, NR_CPUS) < 1)
667 printk(KERN_ERR PREFIX "Error parsing MADT - no LAPIC entries\n");
669 if (acpi_table_parse_madt(ACPI_MADT_LAPIC_NMI, acpi_parse_lapic_nmi, 0) < 0)
670 printk(KERN_ERR PREFIX "Error parsing LAPIC NMI entry\n");
674 if (acpi_table_parse_madt(ACPI_MADT_IOSAPIC, acpi_parse_iosapic, NR_IOSAPICS) < 1)
675 printk(KERN_ERR PREFIX "Error parsing MADT - no IOSAPIC entries\n");
677 /* System-Level Interrupt Routing */
679 if (acpi_table_parse_madt(ACPI_MADT_PLAT_INT_SRC, acpi_parse_plat_int_src, ACPI_MAX_PLATFORM_INTERRUPTS) < 0)
680 printk(KERN_ERR PREFIX "Error parsing platform interrupt source entry\n");
682 if (acpi_table_parse_madt(ACPI_MADT_INT_SRC_OVR, acpi_parse_int_src_ovr, 0) < 0)
683 printk(KERN_ERR PREFIX "Error parsing interrupt source overrides entry\n");
685 if (acpi_table_parse_madt(ACPI_MADT_NMI_SRC, acpi_parse_nmi_src, 0) < 0)
686 printk(KERN_ERR PREFIX "Error parsing NMI SRC entry\n");
690 * FADT says whether a legacy keyboard controller is present.
691 * The FADT also contains an SCI_INT line, by which the system
692 * gets interrupts such as power and sleep buttons. If it's not
693 * on a Legacy interrupt, it needs to be setup.
695 if (acpi_table_parse(ACPI_FADT, acpi_parse_fadt) < 1)
696 printk(KERN_ERR PREFIX "Can't find FADT\n");
699 if (available_cpus == 0) {
700 printk(KERN_INFO "ACPI: Found 0 CPUS; assuming 1\n");
701 printk(KERN_INFO "CPU 0 (0x%04x)", hard_smp_processor_id());
702 smp_boot_data.cpu_phys_id[available_cpus] = hard_smp_processor_id();
703 available_cpus = 1; /* We've got at least one of these, no? */
705 smp_boot_data.cpu_count = available_cpus;
708 # ifdef CONFIG_ACPI_NUMA
709 if (srat_num_cpus == 0) {
711 for (cpu = 0; cpu < smp_boot_data.cpu_count; cpu++)
712 if (smp_boot_data.cpu_phys_id[cpu] != hard_smp_processor_id())
713 node_cpuid[i++].phys_id = smp_boot_data.cpu_phys_id[cpu];
715 build_cpu_to_node_map();
718 /* Make boot-up look pretty */
719 printk(KERN_INFO "%d CPUs available, %d CPUs total\n", available_cpus, total_cpus);
724 acpi_irq_to_vector (u32 gsi)
726 if (has_8259 && gsi < 16)
727 return isa_irq_to_vector(gsi);
729 return gsi_to_vector(gsi);
733 acpi_register_irq (u32 gsi, u32 polarity, u32 trigger)
735 if (has_8259 && gsi < 16)
736 return isa_irq_to_vector(gsi);
738 return iosapic_register_intr(gsi,
739 (polarity == ACPI_ACTIVE_HIGH) ? IOSAPIC_POL_HIGH : IOSAPIC_POL_LOW,
740 (trigger == ACPI_EDGE_SENSITIVE) ? IOSAPIC_EDGE : IOSAPIC_LEVEL);
742 EXPORT_SYMBOL(acpi_register_irq);
744 #endif /* CONFIG_ACPI_BOOT */