Merge branch 'liblockdep-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.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 #include <linux/acpi.h>
14 #include <acpi/processor.h>
15
16 #include "internal.h"
17
18 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
19 ACPI_MODULE_NAME("processor_core");
20
21 static 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;
26
27         if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
28                 return -ENODEV;
29
30         if (lapic->processor_id != acpi_id)
31                 return -EINVAL;
32
33         *apic_id = lapic->id;
34         return 0;
35 }
36
37 static 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;
42
43         if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
44                 return -ENODEV;
45
46         if (device_declaration && (apic->uid == acpi_id)) {
47                 *apic_id = apic->local_apic_id;
48                 return 0;
49         }
50
51         return -EINVAL;
52 }
53
54 static 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;
59
60         if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
61                 return -ENODEV;
62
63         if (device_declaration) {
64                 if ((entry->length < 16) || (lsapic->uid != acpi_id))
65                         return -EINVAL;
66         } else if (lsapic->processor_id != acpi_id)
67                 return -EINVAL;
68
69         *apic_id = (lsapic->id << 8) | lsapic->eid;
70         return 0;
71 }
72
73 static 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
95 static int map_madt_entry(int type, u32 acpi_id)
96 {
97         unsigned long madt_end, entry;
98         static struct acpi_table_madt *madt;
99         static int read_madt;
100         int apic_id = -1;
101
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
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) {
122                         if (!map_lapic_id(header, acpi_id, &apic_id))
123                                 break;
124                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
125                         if (!map_x2apic_id(header, type, acpi_id, &apic_id))
126                                 break;
127                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
128                         if (!map_lsapic_id(header, type, acpi_id, &apic_id))
129                                 break;
130                 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
131                         if (!map_gic_id(header, type, acpi_id, &apic_id))
132                                 break;
133                 }
134                 entry += header->length;
135         }
136         return apic_id;
137 }
138
139 static 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);
163         } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
164                 map_gic_id(header, type, acpi_id, &apic_id);
165         }
166
167 exit:
168         kfree(buffer.pointer);
169         return apic_id;
170 }
171
172 int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
173 {
174         int apic_id;
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);
179
180         return apic_id;
181 }
182
183 int acpi_map_cpuid(int apic_id, u32 acpi_id)
184 {
185 #ifdef CONFIG_SMP
186         int i;
187 #endif
188
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                  *
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.
208                  * Return -1 for other CPU's handle.
209                  */
210                 if (nr_cpu_ids <= 1 && acpi_id == 0)
211                         return acpi_id;
212                 else
213                         return apic_id;
214         }
215
216 #ifdef CONFIG_SMP
217         for_each_possible_cpu(i) {
218                 if (cpu_physical_id(i) == apic_id)
219                         return i;
220         }
221 #else
222         /* In UP kernel, only processor 0 is valid */
223         if (apic_id == 0)
224                 return apic_id;
225 #endif
226         return -1;
227 }
228
229 int 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 }
237 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
238
239 static bool __init processor_physically_present(acpi_handle handle)
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
273         if (cpuid == -1)
274                 return false;
275
276         return true;
277 }
278
279 static void acpi_set_pdc_bits(u32 *buf)
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;
286
287         /* Twiddle arch-specific bits needed for _PDC */
288         arch_acpi_set_pdc_bits(buf);
289 }
290
291 static struct acpi_object_list *acpi_processor_alloc_pdc(void)
292 {
293         struct acpi_object_list *obj_list;
294         union acpi_object *obj;
295         u32 *buf;
296
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");
301                 return NULL;
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);
308                 return NULL;
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);
316                 return NULL;
317         }
318
319         acpi_set_pdc_bits(buf);
320
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;
326
327         return obj_list;
328 }
329
330 /*
331  * _PDC is required for a BIOS-OS handshake for most of the newer
332  * ACPI processor features.
333  */
334 static acpi_status
335 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
336 {
337         acpi_status status = AE_OK;
338
339         if (boot_option_idle_override == IDLE_NOMWAIT) {
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         }
353         status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
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
362 void acpi_processor_set_pdc(acpi_handle handle)
363 {
364         struct acpi_object_list *obj_list;
365
366         if (arch_has_acpi_pdc() == false)
367                 return;
368
369         obj_list = acpi_processor_alloc_pdc();
370         if (!obj_list)
371                 return;
372
373         acpi_processor_eval_pdc(handle, obj_list);
374
375         kfree(obj_list->pointer->buffer.pointer);
376         kfree(obj_list->pointer);
377         kfree(obj_list);
378 }
379
380 static acpi_status __init
381 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
382 {
383         if (processor_physically_present(handle) == false)
384                 return AE_OK;
385
386         acpi_processor_set_pdc(handle);
387         return AE_OK;
388 }
389
390 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
391 static 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
399 static 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
409 static void __init processor_dmi_check(void)
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);
416 }
417 #else
418 static inline void processor_dmi_check(void) {}
419 #endif
420
421 void __init acpi_early_processor_set_pdc(void)
422 {
423         processor_dmi_check();
424
425         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
426                             ACPI_UINT32_MAX,
427                             early_init_pdc, NULL, NULL, NULL);
428         acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);
429 }