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 PREFIX "ACPI: "
19 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
20 ACPI_MODULE_NAME("processor_core");
22 static int map_lapic_id(struct acpi_subtable_header *entry,
23 u32 acpi_id, int *apic_id)
25 struct acpi_madt_local_apic *lapic =
26 (struct acpi_madt_local_apic *)entry;
28 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
31 if (lapic->processor_id != acpi_id)
38 static int map_x2apic_id(struct acpi_subtable_header *entry,
39 int device_declaration, u32 acpi_id, int *apic_id)
41 struct acpi_madt_local_x2apic *apic =
42 (struct acpi_madt_local_x2apic *)entry;
44 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
47 if (device_declaration && (apic->uid == acpi_id)) {
48 *apic_id = apic->local_apic_id;
55 static int map_lsapic_id(struct acpi_subtable_header *entry,
56 int device_declaration, u32 acpi_id, int *apic_id)
58 struct acpi_madt_local_sapic *lsapic =
59 (struct acpi_madt_local_sapic *)entry;
61 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
64 if (device_declaration) {
65 if ((entry->length < 16) || (lsapic->uid != acpi_id))
67 } else if (lsapic->processor_id != acpi_id)
70 *apic_id = (lsapic->id << 8) | lsapic->eid;
74 static int map_gic_id(struct acpi_subtable_header *entry,
75 int device_declaration, u32 acpi_id, int *apic_id)
77 struct acpi_madt_generic_interrupt *gic =
78 (struct acpi_madt_generic_interrupt *)entry;
80 if (!(gic->flags & ACPI_MADT_ENABLED))
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
88 if (device_declaration && (gic->uid == acpi_id)) {
89 *apic_id = gic->gic_id;
96 static int map_madt_entry(int type, u32 acpi_id)
98 unsigned long madt_end, entry;
99 static struct acpi_table_madt *madt;
100 static int read_madt;
104 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
105 (struct acpi_table_header **)&madt)))
113 entry = (unsigned long)madt;
114 madt_end = entry + madt->header.length;
116 /* Parse all entries looking for a match. */
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))
125 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
126 if (!map_x2apic_id(header, type, acpi_id, &apic_id))
128 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
129 if (!map_lsapic_id(header, type, acpi_id, &apic_id))
131 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
132 if (!map_gic_id(header, type, acpi_id, &apic_id))
135 entry += header->length;
140 static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
142 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
143 union acpi_object *obj;
144 struct acpi_subtable_header *header;
147 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
150 if (!buffer.length || !buffer.pointer)
153 obj = buffer.pointer;
154 if (obj->type != ACPI_TYPE_BUFFER ||
155 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
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);
169 kfree(buffer.pointer);
173 int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
177 apic_id = map_mat_entry(handle, type, acpi_id);
179 apic_id = map_madt_entry(type, acpi_id);
184 int acpi_map_cpuid(int apic_id, u32 acpi_id)
192 * On UP processor, there is no _MAT or MADT table.
193 * So above apic_id is always set to -1.
195 * BIOS may define multiple CPU handles even for UP processor.
200 * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
201 * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
202 * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
203 * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
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.
211 if (nr_cpu_ids <= 1 && acpi_id == 0)
218 for_each_possible_cpu(i) {
219 if (cpu_physical_id(i) == apic_id)
223 /* In UP kernel, only processor 0 is valid */
230 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
234 apic_id = acpi_get_apicid(handle, type, acpi_id);
236 return acpi_map_cpuid(apic_id, acpi_id);
238 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
240 static bool __init processor_physically_present(acpi_handle handle)
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 };
250 status = acpi_get_type(handle, &acpi_type);
251 if (ACPI_FAILURE(status))
255 case ACPI_TYPE_PROCESSOR:
256 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
257 if (ACPI_FAILURE(status))
259 acpi_id = object.processor.proc_id;
261 case ACPI_TYPE_DEVICE:
262 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
263 if (ACPI_FAILURE(status))
271 type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
272 cpuid = acpi_get_cpuid(handle, type, acpi_id);
280 static void 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 *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;
340 if (boot_option_idle_override == IDLE_NOMWAIT) {
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.
346 union acpi_object *obj;
349 obj = pdc_in->pointer;
350 buffer = (u32 *)(obj->buffer.pointer);
351 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
354 status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
356 if (ACPI_FAILURE(status))
357 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
358 "Could not evaluate _PDC, using legacy perf. control.\n"));
363 void acpi_processor_set_pdc(acpi_handle handle)
365 struct acpi_object_list *obj_list;
367 if (arch_has_acpi_pdc() == false)
370 obj_list = acpi_processor_alloc_pdc();
374 acpi_processor_eval_pdc(handle, obj_list);
376 kfree(obj_list->pointer->buffer.pointer);
377 kfree(obj_list->pointer);
381 static acpi_status __init
382 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
384 if (processor_physically_present(handle) == false)
387 acpi_processor_set_pdc(handle);
391 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
392 static int __init set_no_mwait(const struct dmi_system_id *id)
394 pr_notice(PREFIX "%s detected - disabling mwait for CPU C-states\n",
396 boot_option_idle_override = IDLE_NOMWAIT;
400 static struct dmi_system_id processor_idle_dmi_table[] __initdata = {
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},
410 static void __init processor_dmi_check(void)
413 * Check whether the system is DMI table. If yes, OSPM
414 * should not use mwait for CPU-states.
416 dmi_check_system(processor_idle_dmi_table);
419 static inline void processor_dmi_check(void) {}
422 void __init acpi_early_processor_set_pdc(void)
424 processor_dmi_check();
426 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
428 early_init_pdc, NULL, NULL, NULL);
429 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);