- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / acpi / processor_core.c
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  */
10 #include <linux/export.h>
11 #include <linux/dmi.h>
12 #include <linux/slab.h>
13
14 #include <acpi/acpi_drivers.h>
15 #include <acpi/processor.h>
16
17 #include "internal.h"
18
19 #define PREFIX                  "ACPI: "
20 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
21 ACPI_MODULE_NAME("processor_core");
22
23 #ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
24 /*
25  * External processor control logic may register with its own set of
26  * ops to get ACPI related notification. One example is like VMM.
27  */
28 const struct processor_extcntl_ops *processor_extcntl_ops;
29 EXPORT_SYMBOL(processor_extcntl_ops);
30 #endif
31
32 static int __init set_no_mwait(const struct dmi_system_id *id)
33 {
34         printk(KERN_NOTICE PREFIX "%s detected - "
35                 "disabling mwait for CPU C-states\n", id->ident);
36         boot_option_idle_override = IDLE_NOMWAIT;
37         return 0;
38 }
39
40 static struct dmi_system_id __initdata processor_idle_dmi_table[] = {
41         {
42         set_no_mwait, "Extensa 5220", {
43         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
44         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
45         DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
46         DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
47         {},
48 };
49
50 static int map_lapic_id(struct acpi_subtable_header *entry,
51                  u32 acpi_id, int *apic_id)
52 {
53         struct acpi_madt_local_apic *lapic =
54                 (struct acpi_madt_local_apic *)entry;
55
56         if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
57                 return 0;
58
59         if (lapic->processor_id != acpi_id)
60                 return 0;
61
62         *apic_id = lapic->id;
63         return 1;
64 }
65
66 static int map_x2apic_id(struct acpi_subtable_header *entry,
67                          int device_declaration, u32 acpi_id, int *apic_id)
68 {
69         struct acpi_madt_local_x2apic *apic =
70                 (struct acpi_madt_local_x2apic *)entry;
71
72         if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
73                 return 0;
74
75         if (device_declaration && (apic->uid == acpi_id)) {
76                 *apic_id = apic->local_apic_id;
77                 return 1;
78         }
79
80         return 0;
81 }
82
83 static int map_lsapic_id(struct acpi_subtable_header *entry,
84                 int device_declaration, u32 acpi_id, int *apic_id)
85 {
86         struct acpi_madt_local_sapic *lsapic =
87                 (struct acpi_madt_local_sapic *)entry;
88
89         if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
90                 return 0;
91
92         if (device_declaration) {
93                 if ((entry->length < 16) || (lsapic->uid != acpi_id))
94                         return 0;
95         } else if (lsapic->processor_id != acpi_id)
96                 return 0;
97
98         *apic_id = (lsapic->id << 8) | lsapic->eid;
99         return 1;
100 }
101
102 static int map_madt_entry(int type, u32 acpi_id)
103 {
104         unsigned long madt_end, entry;
105         static struct acpi_table_madt *madt;
106         static int read_madt;
107         int apic_id = -1;
108
109         if (!read_madt) {
110                 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
111                                         (struct acpi_table_header **)&madt)))
112                         madt = NULL;
113                 read_madt++;
114         }
115
116         if (!madt)
117                 return apic_id;
118
119         entry = (unsigned long)madt;
120         madt_end = entry + madt->header.length;
121
122         /* Parse all entries looking for a match. */
123
124         entry += sizeof(struct acpi_table_madt);
125         while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
126                 struct acpi_subtable_header *header =
127                         (struct acpi_subtable_header *)entry;
128                 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
129                         if (map_lapic_id(header, acpi_id, &apic_id))
130                                 break;
131                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
132                         if (map_x2apic_id(header, type, acpi_id, &apic_id))
133                                 break;
134                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
135                         if (map_lsapic_id(header, type, acpi_id, &apic_id))
136                                 break;
137                 }
138                 entry += header->length;
139         }
140         return apic_id;
141 }
142
143 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
144 {
145         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
146         union acpi_object *obj;
147         struct acpi_subtable_header *header;
148         int apic_id = -1;
149
150         if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
151                 goto exit;
152
153         if (!buffer.length || !buffer.pointer)
154                 goto exit;
155
156         obj = buffer.pointer;
157         if (obj->type != ACPI_TYPE_BUFFER ||
158             obj->buffer.length < sizeof(struct acpi_subtable_header)) {
159                 goto exit;
160         }
161
162         header = (struct acpi_subtable_header *)obj->buffer.pointer;
163         if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
164                 map_lapic_id(header, acpi_id, &apic_id);
165         } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
166                 map_lsapic_id(header, type, acpi_id, &apic_id);
167         }
168
169 exit:
170         if (buffer.pointer)
171                 kfree(buffer.pointer);
172         return apic_id;
173 }
174
175 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
176 {
177         int i = 0, apic_id = -1;
178
179         if (type < 0) {
180                 if (!processor_cntl_external())
181                         return -1;
182                 type = ~type;
183                 i = 1;
184         }
185
186         apic_id = map_mat_entry(handle, type, acpi_id);
187         if (apic_id == -1)
188                 apic_id = map_madt_entry(type, acpi_id);
189         if (apic_id == -1 || i) {
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                  *
205                  * Ignores apic_id and always return 0 for CPU0's handle.
206                  * Return -1 for other CPU's handle.
207                  */
208                 if (acpi_id == 0 && !i)
209                         return acpi_id;
210                 else
211                         return apic_id;
212         }
213
214 #ifdef CONFIG_SMP
215 #ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
216         for_each_possible_cpu(i) {
217                 if (cpu_physical_id(i) == apic_id)
218                         return i;
219         }
220 #else
221         /*
222          * Use of cpu_physical_id() is bogus here. Rather than defining a
223          * stub enforcing a 1:1 mapping, we keep it undefined to catch bad
224          * uses. Return as if there was a 1:1 mapping.
225          */
226         if (apic_id < nr_cpu_ids && cpu_possible(apic_id))
227                 return apic_id;
228 #endif
229 #else
230         /* In UP kernel, only processor 0 is valid */
231         if (apic_id == 0)
232                 return apic_id;
233 #endif
234         return -1;
235 }
236 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
237
238 static bool __init processor_physically_present(acpi_handle handle)
239 {
240         int cpuid, type;
241         u32 acpi_id;
242         acpi_status status;
243         acpi_object_type acpi_type;
244         unsigned long long tmp;
245         union acpi_object object = { 0 };
246         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
247
248         status = acpi_get_type(handle, &acpi_type);
249         if (ACPI_FAILURE(status))
250                 return false;
251
252         switch (acpi_type) {
253         case ACPI_TYPE_PROCESSOR:
254                 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
255                 if (ACPI_FAILURE(status))
256                         return false;
257                 acpi_id = object.processor.proc_id;
258                 break;
259         case ACPI_TYPE_DEVICE:
260                 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
261                 if (ACPI_FAILURE(status))
262                         return false;
263                 acpi_id = tmp;
264                 break;
265         default:
266                 return false;
267         }
268
269         type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
270         if (processor_cntl_external())
271                 type = ~type;
272         cpuid = acpi_get_cpuid(handle, type, acpi_id);
273
274         if (cpuid == -1)
275                 return false;
276
277         return true;
278 }
279
280 static void __cpuinit acpi_set_pdc_bits(u32 *buf)
281 {
282         buf[0] = ACPI_PDC_REVISION_ID;
283         buf[1] = 1;
284
285         /* Enable coordination with firmware's _TSD info */
286         buf[2] = ACPI_PDC_SMP_T_SWCOORD;
287
288         /* Twiddle arch-specific bits needed for _PDC */
289         arch_acpi_set_pdc_bits(buf);
290 }
291
292 static struct acpi_object_list *__cpuinit acpi_processor_alloc_pdc(void)
293 {
294         struct acpi_object_list *obj_list;
295         union acpi_object *obj;
296         u32 *buf;
297
298         /* allocate and initialize pdc. It will be used later. */
299         obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
300         if (!obj_list) {
301                 printk(KERN_ERR "Memory allocation error\n");
302                 return NULL;
303         }
304
305         obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
306         if (!obj) {
307                 printk(KERN_ERR "Memory allocation error\n");
308                 kfree(obj_list);
309                 return NULL;
310         }
311
312         buf = kmalloc(12, GFP_KERNEL);
313         if (!buf) {
314                 printk(KERN_ERR "Memory allocation error\n");
315                 kfree(obj);
316                 kfree(obj_list);
317                 return NULL;
318         }
319
320         acpi_set_pdc_bits(buf);
321
322         obj->type = ACPI_TYPE_BUFFER;
323         obj->buffer.length = 12;
324         obj->buffer.pointer = (u8 *) buf;
325         obj_list->count = 1;
326         obj_list->pointer = obj;
327
328         return obj_list;
329 }
330
331 /*
332  * _PDC is required for a BIOS-OS handshake for most of the newer
333  * ACPI processor features.
334  */
335 static int __cpuinit
336 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
337 {
338         acpi_status status = AE_OK;
339
340 #ifndef CONFIG_XEN
341         if (boot_option_idle_override == IDLE_NOMWAIT) {
342                 /*
343                  * If mwait is disabled for CPU C-states, the C2C3_FFH access
344                  * mode will be disabled in the parameter of _PDC object.
345                  * Of course C1_FFH access mode will also be disabled.
346                  */
347 #else
348         {
349                 struct xen_platform_op op;
350 #endif
351                 union acpi_object *obj;
352                 u32 *buffer = NULL;
353
354                 obj = pdc_in->pointer;
355                 buffer = (u32 *)(obj->buffer.pointer);
356 #ifndef CONFIG_XEN
357                 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
358 #else
359                 op.cmd = XENPF_set_processor_pminfo;
360                 op.u.set_pminfo.id = -1;
361                 op.u.set_pminfo.type = XEN_PM_PDC;
362                 set_xen_guest_handle(op.u.set_pminfo.u.pdc, buffer);
363                 VOID(HYPERVISOR_platform_op(&op));
364 #endif
365         }
366         status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
367
368         if (ACPI_FAILURE(status))
369                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
370                     "Could not evaluate _PDC, using legacy perf. control.\n"));
371
372         return status;
373 }
374
375 void __cpuinit acpi_processor_set_pdc(acpi_handle handle)
376 {
377         struct acpi_object_list *obj_list;
378
379         if (arch_has_acpi_pdc() == false)
380                 return;
381
382         obj_list = acpi_processor_alloc_pdc();
383         if (!obj_list)
384                 return;
385
386         acpi_processor_eval_pdc(handle, obj_list);
387
388         kfree(obj_list->pointer->buffer.pointer);
389         kfree(obj_list->pointer);
390         kfree(obj_list);
391 }
392
393 static acpi_status __init
394 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
395 {
396         if (processor_physically_present(handle) == false)
397                 return AE_OK;
398
399         acpi_processor_set_pdc(handle);
400         return AE_OK;
401 }
402
403 void __init acpi_early_processor_set_pdc(void)
404 {
405         /*
406          * Check whether the system is DMI table. If yes, OSPM
407          * should not use mwait for CPU-states.
408          */
409         dmi_check_system(processor_idle_dmi_table);
410
411         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
412                             ACPI_UINT32_MAX,
413                             early_init_pdc, NULL, NULL, NULL);
414         acpi_get_devices("ACPI0007", early_init_pdc, NULL, NULL);
415 }