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>
14 #include <acpi/acpi_drivers.h>
15 #include <acpi/processor.h>
19 #define PREFIX "ACPI: "
20 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
21 ACPI_MODULE_NAME("processor_core");
23 #ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
25 * External processor control logic may register with its own set of
26 * ops to get ACPI related notification. One example is like VMM.
28 const struct processor_extcntl_ops *processor_extcntl_ops;
29 EXPORT_SYMBOL(processor_extcntl_ops);
32 static int __init set_no_mwait(const struct dmi_system_id *id)
34 printk(KERN_NOTICE PREFIX "%s detected - "
35 "disabling mwait for CPU C-states\n", id->ident);
36 boot_option_idle_override = IDLE_NOMWAIT;
40 static struct dmi_system_id __initdata processor_idle_dmi_table[] = {
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},
50 static int map_lapic_id(struct acpi_subtable_header *entry,
51 u32 acpi_id, int *apic_id)
53 struct acpi_madt_local_apic *lapic =
54 (struct acpi_madt_local_apic *)entry;
56 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
59 if (lapic->processor_id != acpi_id)
66 static int map_x2apic_id(struct acpi_subtable_header *entry,
67 int device_declaration, u32 acpi_id, int *apic_id)
69 struct acpi_madt_local_x2apic *apic =
70 (struct acpi_madt_local_x2apic *)entry;
72 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
75 if (device_declaration && (apic->uid == acpi_id)) {
76 *apic_id = apic->local_apic_id;
83 static int map_lsapic_id(struct acpi_subtable_header *entry,
84 int device_declaration, u32 acpi_id, int *apic_id)
86 struct acpi_madt_local_sapic *lsapic =
87 (struct acpi_madt_local_sapic *)entry;
89 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
92 if (device_declaration) {
93 if ((entry->length < 16) || (lsapic->uid != acpi_id))
95 } else if (lsapic->processor_id != acpi_id)
98 *apic_id = (lsapic->id << 8) | lsapic->eid;
102 static int map_madt_entry(int type, u32 acpi_id)
104 unsigned long madt_end, entry;
105 static struct acpi_table_madt *madt;
106 static int read_madt;
110 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
111 (struct acpi_table_header **)&madt)))
119 entry = (unsigned long)madt;
120 madt_end = entry + madt->header.length;
122 /* Parse all entries looking for a match. */
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))
131 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
132 if (map_x2apic_id(header, type, acpi_id, &apic_id))
134 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
135 if (map_lsapic_id(header, type, acpi_id, &apic_id))
138 entry += header->length;
143 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
145 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
146 union acpi_object *obj;
147 struct acpi_subtable_header *header;
150 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
153 if (!buffer.length || !buffer.pointer)
156 obj = buffer.pointer;
157 if (obj->type != ACPI_TYPE_BUFFER ||
158 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
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);
171 kfree(buffer.pointer);
175 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
177 int i = 0, apic_id = -1;
180 if (!processor_cntl_external())
186 apic_id = map_mat_entry(handle, type, acpi_id);
188 apic_id = map_madt_entry(type, acpi_id);
189 if (apic_id == -1 || i) {
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 return 0 for CPU0's handle.
206 * Return -1 for other CPU's handle.
208 if (acpi_id == 0 && !i)
215 #ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
216 for_each_possible_cpu(i) {
217 if (cpu_physical_id(i) == apic_id)
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.
226 if (apic_id < nr_cpu_ids && cpu_possible(apic_id))
230 /* In UP kernel, only processor 0 is valid */
236 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
238 static bool __init processor_physically_present(acpi_handle handle)
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 };
248 status = acpi_get_type(handle, &acpi_type);
249 if (ACPI_FAILURE(status))
253 case ACPI_TYPE_PROCESSOR:
254 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
255 if (ACPI_FAILURE(status))
257 acpi_id = object.processor.proc_id;
259 case ACPI_TYPE_DEVICE:
260 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
261 if (ACPI_FAILURE(status))
269 type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
270 if (processor_cntl_external())
272 cpuid = acpi_get_cpuid(handle, type, acpi_id);
280 static void __cpuinit acpi_set_pdc_bits(u32 *buf)
282 buf[0] = ACPI_PDC_REVISION_ID;
285 /* Enable coordination with firmware's _TSD info */
286 buf[2] = ACPI_PDC_SMP_T_SWCOORD;
288 /* Twiddle arch-specific bits needed for _PDC */
289 arch_acpi_set_pdc_bits(buf);
292 static struct acpi_object_list *__cpuinit acpi_processor_alloc_pdc(void)
294 struct acpi_object_list *obj_list;
295 union acpi_object *obj;
298 /* allocate and initialize pdc. It will be used later. */
299 obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
301 printk(KERN_ERR "Memory allocation error\n");
305 obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
307 printk(KERN_ERR "Memory allocation error\n");
312 buf = kmalloc(12, GFP_KERNEL);
314 printk(KERN_ERR "Memory allocation error\n");
320 acpi_set_pdc_bits(buf);
322 obj->type = ACPI_TYPE_BUFFER;
323 obj->buffer.length = 12;
324 obj->buffer.pointer = (u8 *) buf;
326 obj_list->pointer = obj;
332 * _PDC is required for a BIOS-OS handshake for most of the newer
333 * ACPI processor features.
336 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
338 acpi_status status = AE_OK;
341 if (boot_option_idle_override == IDLE_NOMWAIT) {
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.
349 struct xen_platform_op op;
351 union acpi_object *obj;
354 obj = pdc_in->pointer;
355 buffer = (u32 *)(obj->buffer.pointer);
357 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
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));
366 status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
368 if (ACPI_FAILURE(status))
369 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
370 "Could not evaluate _PDC, using legacy perf. control.\n"));
375 void __cpuinit acpi_processor_set_pdc(acpi_handle handle)
377 struct acpi_object_list *obj_list;
379 if (arch_has_acpi_pdc() == false)
382 obj_list = acpi_processor_alloc_pdc();
386 acpi_processor_eval_pdc(handle, obj_list);
388 kfree(obj_list->pointer->buffer.pointer);
389 kfree(obj_list->pointer);
393 static acpi_status __init
394 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
396 if (processor_physically_present(handle) == false)
399 acpi_processor_set_pdc(handle);
403 void __init acpi_early_processor_set_pdc(void)
406 * Check whether the system is DMI table. If yes, OSPM
407 * should not use mwait for CPU-states.
409 dmi_check_system(processor_idle_dmi_table);
411 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
413 early_init_pdc, NULL, NULL, NULL);
414 acpi_get_devices("ACPI0007", early_init_pdc, NULL, NULL);