2 * Copyright (C) 2005 Intel Corporation
3 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
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
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>
18 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
19 ACPI_MODULE_NAME("processor_core");
21 static int map_lapic_id(struct acpi_subtable_header *entry,
22 u32 acpi_id, int *apic_id)
24 struct acpi_madt_local_apic *lapic =
25 (struct acpi_madt_local_apic *)entry;
27 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
30 if (lapic->processor_id != acpi_id)
37 static int map_x2apic_id(struct acpi_subtable_header *entry,
38 int device_declaration, u32 acpi_id, int *apic_id)
40 struct acpi_madt_local_x2apic *apic =
41 (struct acpi_madt_local_x2apic *)entry;
43 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
46 if (device_declaration && (apic->uid == acpi_id)) {
47 *apic_id = apic->local_apic_id;
54 static int map_lsapic_id(struct acpi_subtable_header *entry,
55 int device_declaration, u32 acpi_id, int *apic_id)
57 struct acpi_madt_local_sapic *lsapic =
58 (struct acpi_madt_local_sapic *)entry;
60 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
63 if (device_declaration) {
64 if ((entry->length < 16) || (lsapic->uid != acpi_id))
66 } else if (lsapic->processor_id != acpi_id)
69 *apic_id = (lsapic->id << 8) | lsapic->eid;
73 static int map_gic_id(struct acpi_subtable_header *entry,
74 int device_declaration, u32 acpi_id, int *apic_id)
76 struct acpi_madt_generic_interrupt *gic =
77 (struct acpi_madt_generic_interrupt *)entry;
79 if (!(gic->flags & ACPI_MADT_ENABLED))
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
87 if (device_declaration && (gic->uid == acpi_id)) {
88 *apic_id = gic->gic_id;
95 static int map_madt_entry(int type, u32 acpi_id)
97 unsigned long madt_end, entry;
98 static struct acpi_table_madt *madt;
103 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
104 (struct acpi_table_header **)&madt)))
112 entry = (unsigned long)madt;
113 madt_end = entry + madt->header.length;
115 /* Parse all entries looking for a match. */
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))
124 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
125 if (!map_x2apic_id(header, type, acpi_id, &apic_id))
127 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
128 if (!map_lsapic_id(header, type, acpi_id, &apic_id))
130 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
131 if (!map_gic_id(header, type, acpi_id, &apic_id))
134 entry += header->length;
139 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
141 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
142 union acpi_object *obj;
143 struct acpi_subtable_header *header;
146 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
149 if (!buffer.length || !buffer.pointer)
152 obj = buffer.pointer;
153 if (obj->type != ACPI_TYPE_BUFFER ||
154 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
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);
168 kfree(buffer.pointer);
172 int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
176 apic_id = map_mat_entry(handle, type, acpi_id);
178 apic_id = map_madt_entry(type, acpi_id);
183 int acpi_map_cpuid(int apic_id, u32 acpi_id)
191 * On UP processor, there is no _MAT or MADT table.
192 * So above apic_id is always set to -1.
194 * BIOS may define multiple CPU handles even for UP processor.
199 * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
200 * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
201 * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
202 * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
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.
210 if (nr_cpu_ids <= 1 && acpi_id == 0)
217 for_each_possible_cpu(i) {
218 if (cpu_physical_id(i) == apic_id)
222 /* In UP kernel, only processor 0 is valid */
229 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
233 apic_id = acpi_get_apicid(handle, type, acpi_id);
235 return acpi_map_cpuid(apic_id, acpi_id);
237 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
239 static bool __init processor_physically_present(acpi_handle handle)
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 };
249 status = acpi_get_type(handle, &acpi_type);
250 if (ACPI_FAILURE(status))
254 case ACPI_TYPE_PROCESSOR:
255 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
256 if (ACPI_FAILURE(status))
258 acpi_id = object.processor.proc_id;
260 case ACPI_TYPE_DEVICE:
261 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
262 if (ACPI_FAILURE(status))
270 type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
271 cpuid = acpi_get_cpuid(handle, type, acpi_id);
279 static void acpi_set_pdc_bits(u32 *buf)
281 buf[0] = ACPI_PDC_REVISION_ID;
284 /* Enable coordination with firmware's _TSD info */
285 buf[2] = ACPI_PDC_SMP_T_SWCOORD;
287 /* Twiddle arch-specific bits needed for _PDC */
288 arch_acpi_set_pdc_bits(buf);
291 static struct acpi_object_list *acpi_processor_alloc_pdc(void)
293 struct acpi_object_list *obj_list;
294 union acpi_object *obj;
297 /* allocate and initialize pdc. It will be used later. */
298 obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
300 printk(KERN_ERR "Memory allocation error\n");
304 obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
306 printk(KERN_ERR "Memory allocation error\n");
311 buf = kmalloc(12, GFP_KERNEL);
313 printk(KERN_ERR "Memory allocation error\n");
319 acpi_set_pdc_bits(buf);
321 obj->type = ACPI_TYPE_BUFFER;
322 obj->buffer.length = 12;
323 obj->buffer.pointer = (u8 *) buf;
325 obj_list->pointer = obj;
331 * _PDC is required for a BIOS-OS handshake for most of the newer
332 * ACPI processor features.
335 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
337 acpi_status status = AE_OK;
339 if (boot_option_idle_override == IDLE_NOMWAIT) {
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.
345 union acpi_object *obj;
348 obj = pdc_in->pointer;
349 buffer = (u32 *)(obj->buffer.pointer);
350 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
353 status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
355 if (ACPI_FAILURE(status))
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
357 "Could not evaluate _PDC, using legacy perf. control.\n"));
362 void acpi_processor_set_pdc(acpi_handle handle)
364 struct acpi_object_list *obj_list;
366 if (arch_has_acpi_pdc() == false)
369 obj_list = acpi_processor_alloc_pdc();
373 acpi_processor_eval_pdc(handle, obj_list);
375 kfree(obj_list->pointer->buffer.pointer);
376 kfree(obj_list->pointer);
380 static acpi_status __init
381 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
383 if (processor_physically_present(handle) == false)
386 acpi_processor_set_pdc(handle);
390 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
391 static int __init set_no_mwait(const struct dmi_system_id *id)
393 pr_notice(PREFIX "%s detected - disabling mwait for CPU C-states\n",
395 boot_option_idle_override = IDLE_NOMWAIT;
399 static struct dmi_system_id processor_idle_dmi_table[] __initdata = {
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},
409 static void __init processor_dmi_check(void)
412 * Check whether the system is DMI table. If yes, OSPM
413 * should not use mwait for CPU-states.
415 dmi_check_system(processor_idle_dmi_table);
418 static inline void processor_dmi_check(void) {}
421 void __init acpi_early_processor_set_pdc(void)
423 processor_dmi_check();
425 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
427 early_init_pdc, NULL, NULL, NULL);
428 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);