ACPI / processor: Make it possible to get APIC ID via GIC
[linux-block.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 PREFIX                  "ACPI: "
19 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
20 ACPI_MODULE_NAME("processor_core");
21
22 static int map_lapic_id(struct acpi_subtable_header *entry,
23                  u32 acpi_id, int *apic_id)
24 {
25         struct acpi_madt_local_apic *lapic =
26                 (struct acpi_madt_local_apic *)entry;
27
28         if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
29                 return -ENODEV;
30
31         if (lapic->processor_id != acpi_id)
32                 return -EINVAL;
33
34         *apic_id = lapic->id;
35         return 0;
36 }
37
38 static int map_x2apic_id(struct acpi_subtable_header *entry,
39                          int device_declaration, u32 acpi_id, int *apic_id)
40 {
41         struct acpi_madt_local_x2apic *apic =
42                 (struct acpi_madt_local_x2apic *)entry;
43
44         if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
45                 return -ENODEV;
46
47         if (device_declaration && (apic->uid == acpi_id)) {
48                 *apic_id = apic->local_apic_id;
49                 return 0;
50         }
51
52         return -EINVAL;
53 }
54
55 static int map_lsapic_id(struct acpi_subtable_header *entry,
56                 int device_declaration, u32 acpi_id, int *apic_id)
57 {
58         struct acpi_madt_local_sapic *lsapic =
59                 (struct acpi_madt_local_sapic *)entry;
60
61         if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
62                 return -ENODEV;
63
64         if (device_declaration) {
65                 if ((entry->length < 16) || (lsapic->uid != acpi_id))
66                         return -EINVAL;
67         } else if (lsapic->processor_id != acpi_id)
68                 return -EINVAL;
69
70         *apic_id = (lsapic->id << 8) | lsapic->eid;
71         return 0;
72 }
73
74 static int map_gic_id(struct acpi_subtable_header *entry,
75                 int device_declaration, u32 acpi_id, int *apic_id)
76 {
77         struct acpi_madt_generic_interrupt *gic =
78                 (struct acpi_madt_generic_interrupt *)entry;
79
80         if (!(gic->flags & ACPI_MADT_ENABLED))
81                 return -ENODEV;
82
83         /*
84          * In the GIC interrupt model, logical processors are
85          * required to have a Processor Device object in the DSDT,
86          * so we should check device_declaration here
87          */
88         if (device_declaration && (gic->uid == acpi_id)) {
89                 *apic_id = gic->gic_id;
90                 return 0;
91         }
92
93         return -EINVAL;
94 }
95
96 static int map_madt_entry(int type, u32 acpi_id)
97 {
98         unsigned long madt_end, entry;
99         static struct acpi_table_madt *madt;
100         static int read_madt;
101         int apic_id = -1;
102
103         if (!read_madt) {
104                 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
105                                         (struct acpi_table_header **)&madt)))
106                         madt = NULL;
107                 read_madt++;
108         }
109
110         if (!madt)
111                 return apic_id;
112
113         entry = (unsigned long)madt;
114         madt_end = entry + madt->header.length;
115
116         /* Parse all entries looking for a match. */
117
118         entry += sizeof(struct acpi_table_madt);
119         while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
120                 struct acpi_subtable_header *header =
121                         (struct acpi_subtable_header *)entry;
122                 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
123                         if (!map_lapic_id(header, acpi_id, &apic_id))
124                                 break;
125                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
126                         if (!map_x2apic_id(header, type, acpi_id, &apic_id))
127                                 break;
128                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
129                         if (!map_lsapic_id(header, type, acpi_id, &apic_id))
130                                 break;
131                 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
132                         if (!map_gic_id(header, type, acpi_id, &apic_id))
133                                 break;
134                 }
135                 entry += header->length;
136         }
137         return apic_id;
138 }
139
140 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
141 {
142         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
143         union acpi_object *obj;
144         struct acpi_subtable_header *header;
145         int apic_id = -1;
146
147         if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
148                 goto exit;
149
150         if (!buffer.length || !buffer.pointer)
151                 goto exit;
152
153         obj = buffer.pointer;
154         if (obj->type != ACPI_TYPE_BUFFER ||
155             obj->buffer.length < sizeof(struct acpi_subtable_header)) {
156                 goto exit;
157         }
158
159         header = (struct acpi_subtable_header *)obj->buffer.pointer;
160         if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
161                 map_lapic_id(header, acpi_id, &apic_id);
162         } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
163                 map_lsapic_id(header, type, acpi_id, &apic_id);
164         } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
165                 map_gic_id(header, type, acpi_id, &apic_id);
166         }
167
168 exit:
169         kfree(buffer.pointer);
170         return apic_id;
171 }
172
173 int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
174 {
175         int apic_id;
176
177         apic_id = map_mat_entry(handle, type, acpi_id);
178         if (apic_id == -1)
179                 apic_id = map_madt_entry(type, acpi_id);
180
181         return apic_id;
182 }
183
184 int acpi_map_cpuid(int apic_id, u32 acpi_id)
185 {
186 #ifdef CONFIG_SMP
187         int i;
188 #endif
189
190         if (apic_id == -1) {
191                 /*
192                  * On UP processor, there is no _MAT or MADT table.
193                  * So above apic_id is always set to -1.
194                  *
195                  * BIOS may define multiple CPU handles even for UP processor.
196                  * For example,
197                  *
198                  * Scope (_PR)
199                  * {
200                  *     Processor (CPU0, 0x00, 0x00000410, 0x06) {}
201                  *     Processor (CPU1, 0x01, 0x00000410, 0x06) {}
202                  *     Processor (CPU2, 0x02, 0x00000410, 0x06) {}
203                  *     Processor (CPU3, 0x03, 0x00000410, 0x06) {}
204                  * }
205                  *
206                  * Ignores apic_id and always returns 0 for the processor
207                  * handle with acpi id 0 if nr_cpu_ids is 1.
208                  * This should be the case if SMP tables are not found.
209                  * Return -1 for other CPU's handle.
210                  */
211                 if (nr_cpu_ids <= 1 && acpi_id == 0)
212                         return acpi_id;
213                 else
214                         return apic_id;
215         }
216
217 #ifdef CONFIG_SMP
218         for_each_possible_cpu(i) {
219                 if (cpu_physical_id(i) == apic_id)
220                         return i;
221         }
222 #else
223         /* In UP kernel, only processor 0 is valid */
224         if (apic_id == 0)
225                 return apic_id;
226 #endif
227         return -1;
228 }
229
230 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
231 {
232         int apic_id;
233
234         apic_id = acpi_get_apicid(handle, type, acpi_id);
235
236         return acpi_map_cpuid(apic_id, acpi_id);
237 }
238 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
239
240 static bool __init processor_physically_present(acpi_handle handle)
241 {
242         int cpuid, type;
243         u32 acpi_id;
244         acpi_status status;
245         acpi_object_type acpi_type;
246         unsigned long long tmp;
247         union acpi_object object = { 0 };
248         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
249
250         status = acpi_get_type(handle, &acpi_type);
251         if (ACPI_FAILURE(status))
252                 return false;
253
254         switch (acpi_type) {
255         case ACPI_TYPE_PROCESSOR:
256                 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
257                 if (ACPI_FAILURE(status))
258                         return false;
259                 acpi_id = object.processor.proc_id;
260                 break;
261         case ACPI_TYPE_DEVICE:
262                 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
263                 if (ACPI_FAILURE(status))
264                         return false;
265                 acpi_id = tmp;
266                 break;
267         default:
268                 return false;
269         }
270
271         type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
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 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 *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 acpi_status
336 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
337 {
338         acpi_status status = AE_OK;
339
340         if (boot_option_idle_override == IDLE_NOMWAIT) {
341                 /*
342                  * If mwait is disabled for CPU C-states, the C2C3_FFH access
343                  * mode will be disabled in the parameter of _PDC object.
344                  * Of course C1_FFH access mode will also be disabled.
345                  */
346                 union acpi_object *obj;
347                 u32 *buffer = NULL;
348
349                 obj = pdc_in->pointer;
350                 buffer = (u32 *)(obj->buffer.pointer);
351                 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
352
353         }
354         status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
355
356         if (ACPI_FAILURE(status))
357                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
358                     "Could not evaluate _PDC, using legacy perf. control.\n"));
359
360         return status;
361 }
362
363 void acpi_processor_set_pdc(acpi_handle handle)
364 {
365         struct acpi_object_list *obj_list;
366
367         if (arch_has_acpi_pdc() == false)
368                 return;
369
370         obj_list = acpi_processor_alloc_pdc();
371         if (!obj_list)
372                 return;
373
374         acpi_processor_eval_pdc(handle, obj_list);
375
376         kfree(obj_list->pointer->buffer.pointer);
377         kfree(obj_list->pointer);
378         kfree(obj_list);
379 }
380
381 static acpi_status __init
382 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
383 {
384         if (processor_physically_present(handle) == false)
385                 return AE_OK;
386
387         acpi_processor_set_pdc(handle);
388         return AE_OK;
389 }
390
391 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
392 static int __init set_no_mwait(const struct dmi_system_id *id)
393 {
394         pr_notice(PREFIX "%s detected - disabling mwait for CPU C-states\n",
395                   id->ident);
396         boot_option_idle_override = IDLE_NOMWAIT;
397         return 0;
398 }
399
400 static struct dmi_system_id processor_idle_dmi_table[] __initdata = {
401         {
402         set_no_mwait, "Extensa 5220", {
403         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
404         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
405         DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
406         DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
407         {},
408 };
409
410 static void __init processor_dmi_check(void)
411 {
412         /*
413          * Check whether the system is DMI table. If yes, OSPM
414          * should not use mwait for CPU-states.
415          */
416         dmi_check_system(processor_idle_dmi_table);
417 }
418 #else
419 static inline void processor_dmi_check(void) {}
420 #endif
421
422 void __init acpi_early_processor_set_pdc(void)
423 {
424         processor_dmi_check();
425
426         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
427                             ACPI_UINT32_MAX,
428                             early_init_pdc, NULL, NULL, NULL);
429         acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);
430 }