2 * Copyright (C) 2005 Intel Corporation
3 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 * Alex Chiang <achiang@hp.com>
6 * - Unified x86/ia64 implementations
8 #include <linux/export.h>
9 #include <linux/acpi.h>
10 #include <acpi/processor.h>
12 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
13 ACPI_MODULE_NAME("processor_core");
15 static int map_lapic_id(struct acpi_subtable_header *entry,
16 u32 acpi_id, int *apic_id)
18 struct acpi_madt_local_apic *lapic =
19 container_of(entry, struct acpi_madt_local_apic, header);
21 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
24 if (lapic->processor_id != acpi_id)
31 static int map_x2apic_id(struct acpi_subtable_header *entry,
32 int device_declaration, u32 acpi_id, int *apic_id)
34 struct acpi_madt_local_x2apic *apic =
35 container_of(entry, struct acpi_madt_local_x2apic, header);
37 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
40 if (device_declaration && (apic->uid == acpi_id)) {
41 *apic_id = apic->local_apic_id;
48 static int map_lsapic_id(struct acpi_subtable_header *entry,
49 int device_declaration, u32 acpi_id, int *apic_id)
51 struct acpi_madt_local_sapic *lsapic =
52 container_of(entry, struct acpi_madt_local_sapic, header);
54 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
57 if (device_declaration) {
58 if ((entry->length < 16) || (lsapic->uid != acpi_id))
60 } else if (lsapic->processor_id != acpi_id)
63 *apic_id = (lsapic->id << 8) | lsapic->eid;
67 static int map_madt_entry(int type, u32 acpi_id)
69 unsigned long madt_end, entry;
70 static struct acpi_table_madt *madt;
75 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
76 (struct acpi_table_header **)&madt)))
84 entry = (unsigned long)madt;
85 madt_end = entry + madt->header.length;
87 /* Parse all entries looking for a match. */
89 entry += sizeof(struct acpi_table_madt);
90 while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
91 struct acpi_subtable_header *header =
92 (struct acpi_subtable_header *)entry;
93 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
94 if (!map_lapic_id(header, acpi_id, &apic_id))
96 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
97 if (!map_x2apic_id(header, type, acpi_id, &apic_id))
99 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
100 if (!map_lsapic_id(header, type, acpi_id, &apic_id))
103 entry += header->length;
108 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
110 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
111 union acpi_object *obj;
112 struct acpi_subtable_header *header;
115 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
118 if (!buffer.length || !buffer.pointer)
121 obj = buffer.pointer;
122 if (obj->type != ACPI_TYPE_BUFFER ||
123 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
127 header = (struct acpi_subtable_header *)obj->buffer.pointer;
128 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
129 map_lapic_id(header, acpi_id, &apic_id);
130 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
131 map_lsapic_id(header, type, acpi_id, &apic_id);
132 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
133 map_x2apic_id(header, type, acpi_id, &apic_id);
137 kfree(buffer.pointer);
141 int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
145 apic_id = map_mat_entry(handle, type, acpi_id);
147 apic_id = map_madt_entry(type, acpi_id);
152 int acpi_map_cpuid(int apic_id, u32 acpi_id)
160 * On UP processor, there is no _MAT or MADT table.
161 * So above apic_id is always set to -1.
163 * BIOS may define multiple CPU handles even for UP processor.
168 * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
169 * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
170 * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
171 * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
174 * Ignores apic_id and always returns 0 for the processor
175 * handle with acpi id 0 if nr_cpu_ids is 1.
176 * This should be the case if SMP tables are not found.
177 * Return -1 for other CPU's handle.
179 if (nr_cpu_ids <= 1 && acpi_id == 0)
186 for_each_possible_cpu(i) {
187 if (cpu_physical_id(i) == apic_id)
191 /* In UP kernel, only processor 0 is valid */
198 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
202 apic_id = acpi_get_apicid(handle, type, acpi_id);
204 return acpi_map_cpuid(apic_id, acpi_id);
206 EXPORT_SYMBOL_GPL(acpi_get_cpuid);