ACPI: processor: refactor internal map_lapic_id()
[safe/jmp/linux-2.6] / 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/dmi.h>
11
12 #include <acpi/acpi_drivers.h>
13 #include <acpi/processor.h>
14
15 #include "internal.h"
16
17 #define PREFIX                  "ACPI: "
18 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
19 ACPI_MODULE_NAME("processor_core");
20
21 static int set_no_mwait(const struct dmi_system_id *id)
22 {
23         printk(KERN_NOTICE PREFIX "%s detected - "
24                 "disabling mwait for CPU C-states\n", id->ident);
25         idle_nomwait = 1;
26         return 0;
27 }
28
29 static struct dmi_system_id __cpuinitdata processor_idle_dmi_table[] = {
30         {
31         set_no_mwait, "IFL91 board", {
32         DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
33         DMI_MATCH(DMI_SYS_VENDOR, "ZEPTO"),
34         DMI_MATCH(DMI_PRODUCT_VERSION, "3215W"),
35         DMI_MATCH(DMI_BOARD_NAME, "IFL91") }, NULL},
36         {
37         set_no_mwait, "Extensa 5220", {
38         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
39         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
40         DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
41         DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
42         {},
43 };
44
45 #ifdef CONFIG_SMP
46 static struct acpi_table_madt *madt;
47
48 static int map_lapic_id(struct acpi_subtable_header *entry,
49                  u32 acpi_id, int *apic_id)
50 {
51         struct acpi_madt_local_apic *lapic =
52                 (struct acpi_madt_local_apic *)entry;
53
54         if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
55                 return 0;
56
57         if (lapic->processor_id != acpi_id)
58                 return 0;
59
60         *apic_id = lapic->id;
61         return 1;
62 }
63
64 static int map_x2apic_id(struct acpi_subtable_header *entry,
65                          int device_declaration, u32 acpi_id, int *apic_id)
66 {
67         struct acpi_madt_local_x2apic *apic =
68                 (struct acpi_madt_local_x2apic *)entry;
69         u32 tmp = apic->local_apic_id;
70
71         /* Only check enabled APICs*/
72         if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
73                 return 0;
74
75         /* Device statement declaration type */
76         if (device_declaration) {
77                 if (apic->uid == acpi_id)
78                         goto found;
79         }
80
81         return 0;
82 found:
83         *apic_id = tmp;
84         return 1;
85 }
86
87 static int map_lsapic_id(struct acpi_subtable_header *entry,
88                 int device_declaration, u32 acpi_id, int *apic_id)
89 {
90         struct acpi_madt_local_sapic *lsapic =
91                 (struct acpi_madt_local_sapic *)entry;
92         u32 tmp = (lsapic->id << 8) | lsapic->eid;
93
94         /* Only check enabled APICs*/
95         if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
96                 return 0;
97
98         /* Device statement declaration type */
99         if (device_declaration) {
100                 if (entry->length < 16)
101                         printk(KERN_ERR PREFIX
102                             "Invalid LSAPIC with Device type processor (SAPIC ID %#x)\n",
103                             tmp);
104                 else if (lsapic->uid == acpi_id)
105                         goto found;
106         /* Processor statement declaration type */
107         } else if (lsapic->processor_id == acpi_id)
108                 goto found;
109
110         return 0;
111 found:
112         *apic_id = tmp;
113         return 1;
114 }
115
116 static int map_madt_entry(int type, u32 acpi_id)
117 {
118         unsigned long madt_end, entry;
119         int apic_id = -1;
120
121         if (!madt)
122                 return apic_id;
123
124         entry = (unsigned long)madt;
125         madt_end = entry + madt->header.length;
126
127         /* Parse all entries looking for a match. */
128
129         entry += sizeof(struct acpi_table_madt);
130         while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
131                 struct acpi_subtable_header *header =
132                         (struct acpi_subtable_header *)entry;
133                 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
134                         if (map_lapic_id(header, acpi_id, &apic_id))
135                                 break;
136                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
137                         if (map_x2apic_id(header, type, acpi_id, &apic_id))
138                                 break;
139                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
140                         if (map_lsapic_id(header, type, acpi_id, &apic_id))
141                                 break;
142                 }
143                 entry += header->length;
144         }
145         return apic_id;
146 }
147
148 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
149 {
150         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
151         union acpi_object *obj;
152         struct acpi_subtable_header *header;
153         int apic_id = -1;
154
155         if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
156                 goto exit;
157
158         if (!buffer.length || !buffer.pointer)
159                 goto exit;
160
161         obj = buffer.pointer;
162         if (obj->type != ACPI_TYPE_BUFFER ||
163             obj->buffer.length < sizeof(struct acpi_subtable_header)) {
164                 goto exit;
165         }
166
167         header = (struct acpi_subtable_header *)obj->buffer.pointer;
168         if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
169                 map_lapic_id(header, acpi_id, &apic_id);
170         } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
171                 map_lsapic_id(header, type, acpi_id, &apic_id);
172         }
173
174 exit:
175         if (buffer.pointer)
176                 kfree(buffer.pointer);
177         return apic_id;
178 }
179
180 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
181 {
182         int i;
183         int apic_id = -1;
184
185         apic_id = map_mat_entry(handle, type, acpi_id);
186         if (apic_id == -1)
187                 apic_id = map_madt_entry(type, acpi_id);
188         if (apic_id == -1)
189                 return apic_id;
190
191         for_each_possible_cpu(i) {
192                 if (cpu_physical_id(i) == apic_id)
193                         return i;
194         }
195         return -1;
196 }
197 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
198 #endif
199
200 static bool processor_physically_present(acpi_handle handle)
201 {
202         int cpuid, type;
203         u32 acpi_id;
204         acpi_status status;
205         acpi_object_type acpi_type;
206         unsigned long long tmp;
207         union acpi_object object = { 0 };
208         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
209
210         status = acpi_get_type(handle, &acpi_type);
211         if (ACPI_FAILURE(status))
212                 return false;
213
214         switch (acpi_type) {
215         case ACPI_TYPE_PROCESSOR:
216                 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
217                 if (ACPI_FAILURE(status))
218                         return false;
219                 acpi_id = object.processor.proc_id;
220                 break;
221         case ACPI_TYPE_DEVICE:
222                 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
223                 if (ACPI_FAILURE(status))
224                         return false;
225                 acpi_id = tmp;
226                 break;
227         default:
228                 return false;
229         }
230
231         type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
232         cpuid = acpi_get_cpuid(handle, type, acpi_id);
233
234         if (cpuid == -1)
235                 return false;
236
237         return true;
238 }
239
240 static void acpi_set_pdc_bits(u32 *buf)
241 {
242         buf[0] = ACPI_PDC_REVISION_ID;
243         buf[1] = 1;
244
245         /* Enable coordination with firmware's _TSD info */
246         buf[2] = ACPI_PDC_SMP_T_SWCOORD;
247
248         /* Twiddle arch-specific bits needed for _PDC */
249         arch_acpi_set_pdc_bits(buf);
250 }
251
252 static struct acpi_object_list *acpi_processor_alloc_pdc(void)
253 {
254         struct acpi_object_list *obj_list;
255         union acpi_object *obj;
256         u32 *buf;
257
258         /* allocate and initialize pdc. It will be used later. */
259         obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
260         if (!obj_list) {
261                 printk(KERN_ERR "Memory allocation error\n");
262                 return NULL;
263         }
264
265         obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
266         if (!obj) {
267                 printk(KERN_ERR "Memory allocation error\n");
268                 kfree(obj_list);
269                 return NULL;
270         }
271
272         buf = kmalloc(12, GFP_KERNEL);
273         if (!buf) {
274                 printk(KERN_ERR "Memory allocation error\n");
275                 kfree(obj);
276                 kfree(obj_list);
277                 return NULL;
278         }
279
280         acpi_set_pdc_bits(buf);
281
282         obj->type = ACPI_TYPE_BUFFER;
283         obj->buffer.length = 12;
284         obj->buffer.pointer = (u8 *) buf;
285         obj_list->count = 1;
286         obj_list->pointer = obj;
287
288         return obj_list;
289 }
290
291 /*
292  * _PDC is required for a BIOS-OS handshake for most of the newer
293  * ACPI processor features.
294  */
295 static int
296 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
297 {
298         acpi_status status = AE_OK;
299
300         if (idle_nomwait) {
301                 /*
302                  * If mwait is disabled for CPU C-states, the C2C3_FFH access
303                  * mode will be disabled in the parameter of _PDC object.
304                  * Of course C1_FFH access mode will also be disabled.
305                  */
306                 union acpi_object *obj;
307                 u32 *buffer = NULL;
308
309                 obj = pdc_in->pointer;
310                 buffer = (u32 *)(obj->buffer.pointer);
311                 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
312
313         }
314         status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
315
316         if (ACPI_FAILURE(status))
317                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
318                     "Could not evaluate _PDC, using legacy perf. control.\n"));
319
320         return status;
321 }
322
323 void acpi_processor_set_pdc(acpi_handle handle)
324 {
325         struct acpi_object_list *obj_list;
326
327         if (arch_has_acpi_pdc() == false)
328                 return;
329
330         obj_list = acpi_processor_alloc_pdc();
331         if (!obj_list)
332                 return;
333
334         acpi_processor_eval_pdc(handle, obj_list);
335
336         kfree(obj_list->pointer->buffer.pointer);
337         kfree(obj_list->pointer);
338         kfree(obj_list);
339 }
340 EXPORT_SYMBOL_GPL(acpi_processor_set_pdc);
341
342 static acpi_status
343 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
344 {
345         if (processor_physically_present(handle) == false)
346                 return AE_OK;
347
348         acpi_processor_set_pdc(handle);
349         return AE_OK;
350 }
351
352 void __init acpi_early_processor_set_pdc(void)
353 {
354
355 #ifdef CONFIG_SMP
356         if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
357                                 (struct acpi_table_header **)&madt)))
358                 madt = NULL;
359 #endif
360
361         /*
362          * Check whether the system is DMI table. If yes, OSPM
363          * should not use mwait for CPU-states.
364          */
365         dmi_check_system(processor_idle_dmi_table);
366
367         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
368                             ACPI_UINT32_MAX,
369                             early_init_pdc, NULL, NULL, NULL);
370 }