Merge branch 'x86-nuke-platforms-for-linus' of git://git.kernel.org/pub/scm/linux...
[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
7 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
8 * - Added _PDC for platforms with Intel CPUs
9 */
214f2c90 10#include <linux/export.h>
78f16996 11#include <linux/dmi.h>
5a0e3ad6 12#include <linux/slab.h>
8b48463f 13#include <linux/acpi.h>
78f16996
AC
14#include <acpi/processor.h>
15
16#include "internal.h"
17
78f16996 18#define _COMPONENT ACPI_PROCESSOR_COMPONENT
4d5d4cd8 19ACPI_MODULE_NAME("processor_core");
78f16996 20
78ed8bd2
AC
21static int map_lapic_id(struct acpi_subtable_header *entry,
22 u32 acpi_id, int *apic_id)
23{
24 struct acpi_madt_local_apic *lapic =
25 (struct acpi_madt_local_apic *)entry;
11130736
AC
26
27 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
038d7b59 28 return -ENODEV;
11130736
AC
29
30 if (lapic->processor_id != acpi_id)
038d7b59 31 return -EINVAL;
11130736
AC
32
33 *apic_id = lapic->id;
038d7b59 34 return 0;
78ed8bd2
AC
35}
36
37static int map_x2apic_id(struct acpi_subtable_header *entry,
38 int device_declaration, u32 acpi_id, int *apic_id)
39{
40 struct acpi_madt_local_x2apic *apic =
41 (struct acpi_madt_local_x2apic *)entry;
78ed8bd2 42
78ed8bd2 43 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
038d7b59 44 return -ENODEV;
78ed8bd2 45
d6742095
AC
46 if (device_declaration && (apic->uid == acpi_id)) {
47 *apic_id = apic->local_apic_id;
038d7b59 48 return 0;
78ed8bd2
AC
49 }
50
038d7b59 51 return -EINVAL;
78ed8bd2
AC
52}
53
54static int map_lsapic_id(struct acpi_subtable_header *entry,
55 int device_declaration, u32 acpi_id, int *apic_id)
56{
57 struct acpi_madt_local_sapic *lsapic =
58 (struct acpi_madt_local_sapic *)entry;
78ed8bd2 59
78ed8bd2 60 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
038d7b59 61 return -ENODEV;
78ed8bd2 62
78ed8bd2 63 if (device_declaration) {
eae701ce 64 if ((entry->length < 16) || (lsapic->uid != acpi_id))
038d7b59 65 return -EINVAL;
eae701ce 66 } else if (lsapic->processor_id != acpi_id)
038d7b59 67 return -EINVAL;
78ed8bd2 68
eae701ce 69 *apic_id = (lsapic->id << 8) | lsapic->eid;
038d7b59 70 return 0;
78ed8bd2
AC
71}
72
df86f5df
HG
73static int map_gic_id(struct acpi_subtable_header *entry,
74 int device_declaration, u32 acpi_id, int *apic_id)
75{
76 struct acpi_madt_generic_interrupt *gic =
77 (struct acpi_madt_generic_interrupt *)entry;
78
79 if (!(gic->flags & ACPI_MADT_ENABLED))
80 return -ENODEV;
81
82 /*
83 * In the GIC interrupt model, logical processors are
84 * required to have a Processor Device object in the DSDT,
85 * so we should check device_declaration here
86 */
87 if (device_declaration && (gic->uid == acpi_id)) {
88 *apic_id = gic->gic_id;
89 return 0;
90 }
91
92 return -EINVAL;
93}
94
78ed8bd2
AC
95static int map_madt_entry(int type, u32 acpi_id)
96{
97 unsigned long madt_end, entry;
149fe9c2
AC
98 static struct acpi_table_madt *madt;
99 static int read_madt;
78ed8bd2
AC
100 int apic_id = -1;
101
149fe9c2
AC
102 if (!read_madt) {
103 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
104 (struct acpi_table_header **)&madt)))
105 madt = NULL;
106 read_madt++;
107 }
108
78ed8bd2
AC
109 if (!madt)
110 return apic_id;
111
112 entry = (unsigned long)madt;
113 madt_end = entry + madt->header.length;
114
115 /* Parse all entries looking for a match. */
116
117 entry += sizeof(struct acpi_table_madt);
118 while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
119 struct acpi_subtable_header *header =
120 (struct acpi_subtable_header *)entry;
121 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
038d7b59 122 if (!map_lapic_id(header, acpi_id, &apic_id))
78ed8bd2
AC
123 break;
124 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
038d7b59 125 if (!map_x2apic_id(header, type, acpi_id, &apic_id))
78ed8bd2
AC
126 break;
127 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
038d7b59 128 if (!map_lsapic_id(header, type, acpi_id, &apic_id))
78ed8bd2 129 break;
df86f5df
HG
130 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
131 if (!map_gic_id(header, type, acpi_id, &apic_id))
132 break;
78ed8bd2
AC
133 }
134 entry += header->length;
135 }
136 return apic_id;
137}
138
139static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
140{
141 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
142 union acpi_object *obj;
143 struct acpi_subtable_header *header;
144 int apic_id = -1;
145
146 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
147 goto exit;
148
149 if (!buffer.length || !buffer.pointer)
150 goto exit;
151
152 obj = buffer.pointer;
153 if (obj->type != ACPI_TYPE_BUFFER ||
154 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
155 goto exit;
156 }
157
158 header = (struct acpi_subtable_header *)obj->buffer.pointer;
159 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
160 map_lapic_id(header, acpi_id, &apic_id);
161 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
162 map_lsapic_id(header, type, acpi_id, &apic_id);
df86f5df
HG
163 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
164 map_gic_id(header, type, acpi_id, &apic_id);
78ed8bd2
AC
165 }
166
167exit:
5273a258 168 kfree(buffer.pointer);
78ed8bd2
AC
169 return apic_id;
170}
171
ca9f62ac 172int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
78ed8bd2 173{
ca9f62ac 174 int apic_id;
78ed8bd2
AC
175
176 apic_id = map_mat_entry(handle, type, acpi_id);
177 if (apic_id == -1)
178 apic_id = map_madt_entry(type, acpi_id);
ca9f62ac
JL
179
180 return apic_id;
181}
182
183int acpi_map_cpuid(int apic_id, u32 acpi_id)
184{
185#ifdef CONFIG_SMP
186 int i;
187#endif
188
d640113f
LM
189 if (apic_id == -1) {
190 /*
191 * On UP processor, there is no _MAT or MADT table.
192 * So above apic_id is always set to -1.
193 *
194 * BIOS may define multiple CPU handles even for UP processor.
195 * For example,
196 *
197 * Scope (_PR)
198 * {
199 * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
200 * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
201 * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
202 * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
203 * }
204 *
c4686c71
TR
205 * Ignores apic_id and always returns 0 for the processor
206 * handle with acpi id 0 if nr_cpu_ids is 1.
207 * This should be the case if SMP tables are not found.
d640113f
LM
208 * Return -1 for other CPU's handle.
209 */
c4686c71 210 if (nr_cpu_ids <= 1 && acpi_id == 0)
d640113f
LM
211 return acpi_id;
212 else
213 return apic_id;
214 }
78ed8bd2 215
932df741 216#ifdef CONFIG_SMP
78ed8bd2
AC
217 for_each_possible_cpu(i) {
218 if (cpu_physical_id(i) == apic_id)
219 return i;
220 }
932df741
LM
221#else
222 /* In UP kernel, only processor 0 is valid */
223 if (apic_id == 0)
224 return apic_id;
225#endif
78ed8bd2
AC
226 return -1;
227}
ca9f62ac
JL
228
229int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
230{
231 int apic_id;
232
233 apic_id = acpi_get_apicid(handle, type, acpi_id);
234
235 return acpi_map_cpuid(apic_id, acpi_id);
236}
78ed8bd2 237EXPORT_SYMBOL_GPL(acpi_get_cpuid);
78ed8bd2 238
6430c9c1 239static bool __init processor_physically_present(acpi_handle handle)
5d554a7b
AC
240{
241 int cpuid, type;
242 u32 acpi_id;
243 acpi_status status;
244 acpi_object_type acpi_type;
245 unsigned long long tmp;
246 union acpi_object object = { 0 };
247 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
248
249 status = acpi_get_type(handle, &acpi_type);
250 if (ACPI_FAILURE(status))
251 return false;
252
253 switch (acpi_type) {
254 case ACPI_TYPE_PROCESSOR:
255 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
256 if (ACPI_FAILURE(status))
257 return false;
258 acpi_id = object.processor.proc_id;
259 break;
260 case ACPI_TYPE_DEVICE:
261 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
262 if (ACPI_FAILURE(status))
263 return false;
264 acpi_id = tmp;
265 break;
266 default:
267 return false;
268 }
269
270 type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
271 cpuid = acpi_get_cpuid(handle, type, acpi_id);
272
932df741 273 if (cpuid == -1)
5d554a7b
AC
274 return false;
275
276 return true;
277}
78ed8bd2 278
fe7bf106 279static void acpi_set_pdc_bits(u32 *buf)
08ea48a3
AC
280{
281 buf[0] = ACPI_PDC_REVISION_ID;
282 buf[1] = 1;
283
284 /* Enable coordination with firmware's _TSD info */
285 buf[2] = ACPI_PDC_SMP_T_SWCOORD;
6c5807d7
AC
286
287 /* Twiddle arch-specific bits needed for _PDC */
288 arch_acpi_set_pdc_bits(buf);
08ea48a3
AC
289}
290
fe7bf106 291static struct acpi_object_list *acpi_processor_alloc_pdc(void)
407cd87c
AC
292{
293 struct acpi_object_list *obj_list;
294 union acpi_object *obj;
295 u32 *buf;
296
407cd87c
AC
297 /* allocate and initialize pdc. It will be used later. */
298 obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
299 if (!obj_list) {
300 printk(KERN_ERR "Memory allocation error\n");
3b407aef 301 return NULL;
407cd87c
AC
302 }
303
304 obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
305 if (!obj) {
306 printk(KERN_ERR "Memory allocation error\n");
307 kfree(obj_list);
3b407aef 308 return NULL;
407cd87c
AC
309 }
310
311 buf = kmalloc(12, GFP_KERNEL);
312 if (!buf) {
313 printk(KERN_ERR "Memory allocation error\n");
314 kfree(obj);
315 kfree(obj_list);
3b407aef 316 return NULL;
407cd87c
AC
317 }
318
08ea48a3
AC
319 acpi_set_pdc_bits(buf);
320
407cd87c
AC
321 obj->type = ACPI_TYPE_BUFFER;
322 obj->buffer.length = 12;
323 obj->buffer.pointer = (u8 *) buf;
324 obj_list->count = 1;
325 obj_list->pointer = obj;
407cd87c 326
3b407aef 327 return obj_list;
407cd87c
AC
328}
329
78f16996
AC
330/*
331 * _PDC is required for a BIOS-OS handshake for most of the newer
332 * ACPI processor features.
333 */
1606484e 334static acpi_status
fa118564 335acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
78f16996 336{
78f16996
AC
337 acpi_status status = AE_OK;
338
d1896049 339 if (boot_option_idle_override == IDLE_NOMWAIT) {
78f16996
AC
340 /*
341 * If mwait is disabled for CPU C-states, the C2C3_FFH access
342 * mode will be disabled in the parameter of _PDC object.
343 * Of course C1_FFH access mode will also be disabled.
344 */
345 union acpi_object *obj;
346 u32 *buffer = NULL;
347
348 obj = pdc_in->pointer;
349 buffer = (u32 *)(obj->buffer.pointer);
350 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
351
352 }
fa118564 353 status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
78f16996
AC
354
355 if (ACPI_FAILURE(status))
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
357 "Could not evaluate _PDC, using legacy perf. control.\n"));
358
359 return status;
360}
361
fe7bf106 362void acpi_processor_set_pdc(acpi_handle handle)
78f16996 363{
3b407aef
AC
364 struct acpi_object_list *obj_list;
365
1d9cb470
AC
366 if (arch_has_acpi_pdc() == false)
367 return;
368
3b407aef
AC
369 obj_list = acpi_processor_alloc_pdc();
370 if (!obj_list)
371 return;
372
43bab25c 373 acpi_processor_eval_pdc(handle, obj_list);
b9c2db78
AC
374
375 kfree(obj_list->pointer->buffer.pointer);
376 kfree(obj_list->pointer);
377 kfree(obj_list);
78f16996 378}
78f16996 379
6430c9c1 380static acpi_status __init
78f16996
AC
381early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
382{
5d554a7b
AC
383 if (processor_physically_present(handle) == false)
384 return AE_OK;
385
43bab25c 386 acpi_processor_set_pdc(handle);
78f16996
AC
387 return AE_OK;
388}
389
2c4fa003
HG
390#if defined(CONFIG_X86) || defined(CONFIG_IA64)
391static int __init set_no_mwait(const struct dmi_system_id *id)
392{
393 pr_notice(PREFIX "%s detected - disabling mwait for CPU C-states\n",
394 id->ident);
395 boot_option_idle_override = IDLE_NOMWAIT;
396 return 0;
397}
398
399static struct dmi_system_id processor_idle_dmi_table[] __initdata = {
400 {
401 set_no_mwait, "Extensa 5220", {
402 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
403 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
404 DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
405 DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
406 {},
407};
408
409static void __init processor_dmi_check(void)
78f16996
AC
410{
411 /*
412 * Check whether the system is DMI table. If yes, OSPM
413 * should not use mwait for CPU-states.
414 */
415 dmi_check_system(processor_idle_dmi_table);
2c4fa003
HG
416}
417#else
418static inline void processor_dmi_check(void) {}
419#endif
420
421void __init acpi_early_processor_set_pdc(void)
422{
423 processor_dmi_check();
78f16996
AC
424
425 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
426 ACPI_UINT32_MAX,
427 early_init_pdc, NULL, NULL, NULL);
52056925 428 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);
78f16996 429}