Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/wireless...
[linux-2.6-block.git] / drivers / acpi / processor_core.c
CommitLineData
47817254
AC
1/*
2 * Copyright (C) 2005 Intel Corporation
3 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
4 *
5 * Alex Chiang <achiang@hp.com>
6 * - Unified x86/ia64 implementations
47817254 7 */
214f2c90 8#include <linux/export.h>
8b48463f 9#include <linux/acpi.h>
78f16996
AC
10#include <acpi/processor.h>
11
78f16996 12#define _COMPONENT ACPI_PROCESSOR_COMPONENT
4d5d4cd8 13ACPI_MODULE_NAME("processor_core");
78f16996 14
78ed8bd2
AC
15static int map_lapic_id(struct acpi_subtable_header *entry,
16 u32 acpi_id, int *apic_id)
17{
18 struct acpi_madt_local_apic *lapic =
ef86c3f4 19 container_of(entry, struct acpi_madt_local_apic, header);
11130736
AC
20
21 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
038d7b59 22 return -ENODEV;
11130736
AC
23
24 if (lapic->processor_id != acpi_id)
038d7b59 25 return -EINVAL;
11130736
AC
26
27 *apic_id = lapic->id;
038d7b59 28 return 0;
78ed8bd2
AC
29}
30
31static int map_x2apic_id(struct acpi_subtable_header *entry,
32 int device_declaration, u32 acpi_id, int *apic_id)
33{
34 struct acpi_madt_local_x2apic *apic =
ef86c3f4 35 container_of(entry, struct acpi_madt_local_x2apic, header);
78ed8bd2 36
78ed8bd2 37 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
038d7b59 38 return -ENODEV;
78ed8bd2 39
d6742095
AC
40 if (device_declaration && (apic->uid == acpi_id)) {
41 *apic_id = apic->local_apic_id;
038d7b59 42 return 0;
78ed8bd2
AC
43 }
44
038d7b59 45 return -EINVAL;
78ed8bd2
AC
46}
47
48static int map_lsapic_id(struct acpi_subtable_header *entry,
49 int device_declaration, u32 acpi_id, int *apic_id)
50{
51 struct acpi_madt_local_sapic *lsapic =
ef86c3f4 52 container_of(entry, struct acpi_madt_local_sapic, header);
78ed8bd2 53
78ed8bd2 54 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
038d7b59 55 return -ENODEV;
78ed8bd2 56
78ed8bd2 57 if (device_declaration) {
eae701ce 58 if ((entry->length < 16) || (lsapic->uid != acpi_id))
038d7b59 59 return -EINVAL;
eae701ce 60 } else if (lsapic->processor_id != acpi_id)
038d7b59 61 return -EINVAL;
78ed8bd2 62
eae701ce 63 *apic_id = (lsapic->id << 8) | lsapic->eid;
038d7b59 64 return 0;
78ed8bd2
AC
65}
66
67static int map_madt_entry(int type, u32 acpi_id)
68{
69 unsigned long madt_end, entry;
149fe9c2
AC
70 static struct acpi_table_madt *madt;
71 static int read_madt;
af8f3f51 72 int phys_id = -1; /* CPU hardware ID */
78ed8bd2 73
149fe9c2
AC
74 if (!read_madt) {
75 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
76 (struct acpi_table_header **)&madt)))
77 madt = NULL;
78 read_madt++;
79 }
80
78ed8bd2 81 if (!madt)
af8f3f51 82 return phys_id;
78ed8bd2
AC
83
84 entry = (unsigned long)madt;
85 madt_end = entry + madt->header.length;
86
87 /* Parse all entries looking for a match. */
88
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) {
af8f3f51 94 if (!map_lapic_id(header, acpi_id, &phys_id))
78ed8bd2
AC
95 break;
96 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
af8f3f51 97 if (!map_x2apic_id(header, type, acpi_id, &phys_id))
78ed8bd2
AC
98 break;
99 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
af8f3f51 100 if (!map_lsapic_id(header, type, acpi_id, &phys_id))
78ed8bd2
AC
101 break;
102 }
103 entry += header->length;
104 }
af8f3f51 105 return phys_id;
78ed8bd2
AC
106}
107
108static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
109{
110 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
111 union acpi_object *obj;
112 struct acpi_subtable_header *header;
af8f3f51 113 int phys_id = -1;
78ed8bd2
AC
114
115 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
116 goto exit;
117
118 if (!buffer.length || !buffer.pointer)
119 goto exit;
120
121 obj = buffer.pointer;
122 if (obj->type != ACPI_TYPE_BUFFER ||
123 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
124 goto exit;
125 }
126
127 header = (struct acpi_subtable_header *)obj->buffer.pointer;
13ca62b2 128 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
af8f3f51 129 map_lapic_id(header, acpi_id, &phys_id);
13ca62b2 130 else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
af8f3f51 131 map_lsapic_id(header, type, acpi_id, &phys_id);
13ca62b2 132 else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
af8f3f51 133 map_x2apic_id(header, type, acpi_id, &phys_id);
78ed8bd2
AC
134
135exit:
5273a258 136 kfree(buffer.pointer);
af8f3f51 137 return phys_id;
78ed8bd2
AC
138}
139
af8f3f51 140int acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
78ed8bd2 141{
af8f3f51 142 int phys_id;
78ed8bd2 143
af8f3f51
HG
144 phys_id = map_mat_entry(handle, type, acpi_id);
145 if (phys_id == -1)
146 phys_id = map_madt_entry(type, acpi_id);
ca9f62ac 147
af8f3f51 148 return phys_id;
ca9f62ac
JL
149}
150
af8f3f51 151int acpi_map_cpuid(int phys_id, u32 acpi_id)
ca9f62ac
JL
152{
153#ifdef CONFIG_SMP
154 int i;
155#endif
156
af8f3f51 157 if (phys_id == -1) {
d640113f
LM
158 /*
159 * On UP processor, there is no _MAT or MADT table.
af8f3f51 160 * So above phys_id is always set to -1.
d640113f
LM
161 *
162 * BIOS may define multiple CPU handles even for UP processor.
163 * For example,
164 *
165 * Scope (_PR)
13ca62b2 166 * {
d640113f
LM
167 * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
168 * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
169 * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
170 * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
171 * }
172 *
af8f3f51 173 * Ignores phys_id and always returns 0 for the processor
c4686c71
TR
174 * handle with acpi id 0 if nr_cpu_ids is 1.
175 * This should be the case if SMP tables are not found.
d640113f
LM
176 * Return -1 for other CPU's handle.
177 */
c4686c71 178 if (nr_cpu_ids <= 1 && acpi_id == 0)
d640113f
LM
179 return acpi_id;
180 else
af8f3f51 181 return phys_id;
d640113f 182 }
78ed8bd2 183
932df741 184#ifdef CONFIG_SMP
78ed8bd2 185 for_each_possible_cpu(i) {
af8f3f51 186 if (cpu_physical_id(i) == phys_id)
78ed8bd2
AC
187 return i;
188 }
932df741
LM
189#else
190 /* In UP kernel, only processor 0 is valid */
af8f3f51
HG
191 if (phys_id == 0)
192 return phys_id;
932df741 193#endif
78ed8bd2
AC
194 return -1;
195}
ca9f62ac
JL
196
197int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
198{
af8f3f51 199 int phys_id;
ca9f62ac 200
af8f3f51 201 phys_id = acpi_get_phys_id(handle, type, acpi_id);
ca9f62ac 202
af8f3f51 203 return acpi_map_cpuid(phys_id, acpi_id);
ca9f62ac 204}
78ed8bd2 205EXPORT_SYMBOL_GPL(acpi_get_cpuid);