Linux 3.12.39
[linux/fpc-iii.git] / drivers / acpi / bus.c
blob17c12ac42b5b6ab35019997f588402f07d9912a4
1 /*
2 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
4 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/sched.h>
31 #include <linux/pm.h>
32 #include <linux/device.h>
33 #include <linux/proc_fs.h>
34 #include <linux/acpi.h>
35 #include <linux/slab.h>
36 #include <linux/regulator/machine.h>
37 #ifdef CONFIG_X86
38 #include <asm/mpspec.h>
39 #endif
40 #include <linux/pci.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/acpi_drivers.h>
43 #include <acpi/apei.h>
44 #include <linux/dmi.h>
45 #include <linux/suspend.h>
47 #include "internal.h"
49 #define _COMPONENT ACPI_BUS_COMPONENT
50 ACPI_MODULE_NAME("bus");
52 struct acpi_device *acpi_root;
53 struct proc_dir_entry *acpi_root_dir;
54 EXPORT_SYMBOL(acpi_root_dir);
56 #define STRUCT_TO_INT(s) (*((int*)&s))
59 #ifdef CONFIG_X86
60 #ifdef CONFIG_ACPI_CUSTOM_DSDT
61 static inline int set_copy_dsdt(const struct dmi_system_id *id)
63 return 0;
65 #else
66 static int set_copy_dsdt(const struct dmi_system_id *id)
68 printk(KERN_NOTICE "%s detected - "
69 "force copy of DSDT to local memory\n", id->ident);
70 acpi_gbl_copy_dsdt_locally = 1;
71 return 0;
73 #endif
75 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
77 * Invoke DSDT corruption work-around on all Toshiba Satellite.
78 * https://bugzilla.kernel.org/show_bug.cgi?id=14679
81 .callback = set_copy_dsdt,
82 .ident = "TOSHIBA Satellite",
83 .matches = {
84 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
85 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
90 #else
91 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
94 #endif
96 /* --------------------------------------------------------------------------
97 Device Management
98 -------------------------------------------------------------------------- */
100 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
101 unsigned long long *sta)
103 acpi_status status;
105 status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
106 if (ACPI_SUCCESS(status))
107 return AE_OK;
109 if (status == AE_NOT_FOUND) {
110 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
111 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
112 return AE_OK;
114 return status;
117 int acpi_bus_get_status(struct acpi_device *device)
119 acpi_status status;
120 unsigned long long sta;
122 status = acpi_bus_get_status_handle(device->handle, &sta);
123 if (ACPI_FAILURE(status))
124 return -ENODEV;
126 STRUCT_TO_INT(device->status) = (int) sta;
128 if (device->status.functional && !device->status.present) {
129 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
130 "functional but not present;\n",
131 device->pnp.bus_id,
132 (u32) STRUCT_TO_INT(device->status)));
135 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
136 device->pnp.bus_id,
137 (u32) STRUCT_TO_INT(device->status)));
138 return 0;
140 EXPORT_SYMBOL(acpi_bus_get_status);
142 void acpi_bus_private_data_handler(acpi_handle handle,
143 void *context)
145 return;
147 EXPORT_SYMBOL(acpi_bus_private_data_handler);
149 int acpi_bus_get_private_data(acpi_handle handle, void **data)
151 acpi_status status;
153 if (!*data)
154 return -EINVAL;
156 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
157 if (ACPI_FAILURE(status) || !*data) {
158 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
159 handle));
160 return -ENODEV;
163 return 0;
165 EXPORT_SYMBOL(acpi_bus_get_private_data);
167 void acpi_bus_no_hotplug(acpi_handle handle)
169 struct acpi_device *adev = NULL;
171 acpi_bus_get_device(handle, &adev);
172 if (adev)
173 adev->flags.no_hotplug = true;
175 EXPORT_SYMBOL_GPL(acpi_bus_no_hotplug);
177 static void acpi_print_osc_error(acpi_handle handle,
178 struct acpi_osc_context *context, char *error)
180 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
181 int i;
183 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
184 printk(KERN_DEBUG "%s\n", error);
185 else {
186 printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
187 kfree(buffer.pointer);
189 printk(KERN_DEBUG"_OSC request data:");
190 for (i = 0; i < context->cap.length; i += sizeof(u32))
191 printk("%x ", *((u32 *)(context->cap.pointer + i)));
192 printk("\n");
195 static acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
197 int i;
198 static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
199 24, 26, 28, 30, 32, 34};
201 if (strlen(str) != 36)
202 return AE_BAD_PARAMETER;
203 for (i = 0; i < 36; i++) {
204 if (i == 8 || i == 13 || i == 18 || i == 23) {
205 if (str[i] != '-')
206 return AE_BAD_PARAMETER;
207 } else if (!isxdigit(str[i]))
208 return AE_BAD_PARAMETER;
210 for (i = 0; i < 16; i++) {
211 uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
212 uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
214 return AE_OK;
217 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
219 acpi_status status;
220 struct acpi_object_list input;
221 union acpi_object in_params[4];
222 union acpi_object *out_obj;
223 u8 uuid[16];
224 u32 errors;
225 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
227 if (!context)
228 return AE_ERROR;
229 if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
230 return AE_ERROR;
231 context->ret.length = ACPI_ALLOCATE_BUFFER;
232 context->ret.pointer = NULL;
234 /* Setting up input parameters */
235 input.count = 4;
236 input.pointer = in_params;
237 in_params[0].type = ACPI_TYPE_BUFFER;
238 in_params[0].buffer.length = 16;
239 in_params[0].buffer.pointer = uuid;
240 in_params[1].type = ACPI_TYPE_INTEGER;
241 in_params[1].integer.value = context->rev;
242 in_params[2].type = ACPI_TYPE_INTEGER;
243 in_params[2].integer.value = context->cap.length/sizeof(u32);
244 in_params[3].type = ACPI_TYPE_BUFFER;
245 in_params[3].buffer.length = context->cap.length;
246 in_params[3].buffer.pointer = context->cap.pointer;
248 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
249 if (ACPI_FAILURE(status))
250 return status;
252 if (!output.length)
253 return AE_NULL_OBJECT;
255 out_obj = output.pointer;
256 if (out_obj->type != ACPI_TYPE_BUFFER
257 || out_obj->buffer.length != context->cap.length) {
258 acpi_print_osc_error(handle, context,
259 "_OSC evaluation returned wrong type");
260 status = AE_TYPE;
261 goto out_kfree;
263 /* Need to ignore the bit0 in result code */
264 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
265 if (errors) {
266 if (errors & OSC_REQUEST_ERROR)
267 acpi_print_osc_error(handle, context,
268 "_OSC request failed");
269 if (errors & OSC_INVALID_UUID_ERROR)
270 acpi_print_osc_error(handle, context,
271 "_OSC invalid UUID");
272 if (errors & OSC_INVALID_REVISION_ERROR)
273 acpi_print_osc_error(handle, context,
274 "_OSC invalid revision");
275 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
276 if (((u32 *)context->cap.pointer)[OSC_QUERY_TYPE]
277 & OSC_QUERY_ENABLE)
278 goto out_success;
279 status = AE_SUPPORT;
280 goto out_kfree;
282 status = AE_ERROR;
283 goto out_kfree;
285 out_success:
286 context->ret.length = out_obj->buffer.length;
287 context->ret.pointer = kmemdup(out_obj->buffer.pointer,
288 context->ret.length, GFP_KERNEL);
289 if (!context->ret.pointer) {
290 status = AE_NO_MEMORY;
291 goto out_kfree;
293 status = AE_OK;
295 out_kfree:
296 kfree(output.pointer);
297 if (status != AE_OK)
298 context->ret.pointer = NULL;
299 return status;
301 EXPORT_SYMBOL(acpi_run_osc);
303 bool osc_sb_apei_support_acked;
304 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
305 static void acpi_bus_osc_support(void)
307 u32 capbuf[2];
308 struct acpi_osc_context context = {
309 .uuid_str = sb_uuid_str,
310 .rev = 1,
311 .cap.length = 8,
312 .cap.pointer = capbuf,
314 acpi_handle handle;
316 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
317 capbuf[OSC_SUPPORT_TYPE] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
318 #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
319 defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
320 capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PAD_SUPPORT;
321 #endif
323 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
324 capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PPC_OST_SUPPORT;
325 #endif
327 #ifdef ACPI_HOTPLUG_OST
328 capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_HOTPLUG_OST_SUPPORT;
329 #endif
331 if (!ghes_disable)
332 capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_APEI_SUPPORT;
333 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
334 return;
335 if (ACPI_SUCCESS(acpi_run_osc(handle, &context))) {
336 u32 *capbuf_ret = context.ret.pointer;
337 if (context.ret.length > OSC_SUPPORT_TYPE)
338 osc_sb_apei_support_acked =
339 capbuf_ret[OSC_SUPPORT_TYPE] & OSC_SB_APEI_SUPPORT;
340 kfree(context.ret.pointer);
342 /* do we need to check other returned cap? Sounds no */
345 /* --------------------------------------------------------------------------
346 Notification Handling
347 -------------------------------------------------------------------------- */
349 static void acpi_bus_check_device(acpi_handle handle)
351 struct acpi_device *device;
352 acpi_status status;
353 struct acpi_device_status old_status;
355 if (acpi_bus_get_device(handle, &device))
356 return;
357 if (!device)
358 return;
360 old_status = device->status;
363 * Make sure this device's parent is present before we go about
364 * messing with the device.
366 if (device->parent && !device->parent->status.present) {
367 device->status = device->parent->status;
368 return;
371 status = acpi_bus_get_status(device);
372 if (ACPI_FAILURE(status))
373 return;
375 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
376 return;
379 * Device Insertion/Removal
381 if ((device->status.present) && !(old_status.present)) {
382 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
383 /* TBD: Handle device insertion */
384 } else if (!(device->status.present) && (old_status.present)) {
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
386 /* TBD: Handle device removal */
390 static void acpi_bus_check_scope(acpi_handle handle)
392 /* Status Change? */
393 acpi_bus_check_device(handle);
396 * TBD: Enumerate child devices within this device's scope and
397 * run acpi_bus_check_device()'s on them.
402 * acpi_bus_notify
403 * ---------------
404 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
406 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
408 struct acpi_device *device = NULL;
409 struct acpi_driver *driver;
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
412 type, handle));
414 switch (type) {
416 case ACPI_NOTIFY_BUS_CHECK:
417 acpi_bus_check_scope(handle);
419 * TBD: We'll need to outsource certain events to non-ACPI
420 * drivers via the device manager (device.c).
422 break;
424 case ACPI_NOTIFY_DEVICE_CHECK:
425 acpi_bus_check_device(handle);
427 * TBD: We'll need to outsource certain events to non-ACPI
428 * drivers via the device manager (device.c).
430 break;
432 case ACPI_NOTIFY_DEVICE_WAKE:
433 /* TBD */
434 break;
436 case ACPI_NOTIFY_EJECT_REQUEST:
437 /* TBD */
438 break;
440 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
441 /* TBD: Exactly what does 'light' mean? */
442 break;
444 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
445 /* TBD */
446 break;
448 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
449 /* TBD */
450 break;
452 case ACPI_NOTIFY_POWER_FAULT:
453 /* TBD */
454 break;
456 default:
457 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
458 "Received unknown/unsupported notification [%08x]\n",
459 type));
460 break;
463 acpi_bus_get_device(handle, &device);
464 if (device) {
465 driver = device->driver;
466 if (driver && driver->ops.notify &&
467 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
468 driver->ops.notify(device, type);
472 /* --------------------------------------------------------------------------
473 Initialization/Cleanup
474 -------------------------------------------------------------------------- */
476 static int __init acpi_bus_init_irq(void)
478 acpi_status status;
479 char *message = NULL;
483 * Let the system know what interrupt model we are using by
484 * evaluating the \_PIC object, if exists.
487 switch (acpi_irq_model) {
488 case ACPI_IRQ_MODEL_PIC:
489 message = "PIC";
490 break;
491 case ACPI_IRQ_MODEL_IOAPIC:
492 message = "IOAPIC";
493 break;
494 case ACPI_IRQ_MODEL_IOSAPIC:
495 message = "IOSAPIC";
496 break;
497 case ACPI_IRQ_MODEL_PLATFORM:
498 message = "platform specific model";
499 break;
500 default:
501 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
502 return -ENODEV;
505 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
507 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
508 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
509 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
510 return -ENODEV;
513 return 0;
516 u8 acpi_gbl_permanent_mmap;
519 void __init acpi_early_init(void)
521 acpi_status status;
523 if (acpi_disabled)
524 return;
526 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
528 /* enable workarounds, unless strict ACPI spec. compliance */
529 if (!acpi_strict)
530 acpi_gbl_enable_interpreter_slack = TRUE;
532 acpi_gbl_permanent_mmap = 1;
535 * If the machine falls into the DMI check table,
536 * DSDT will be copied to memory
538 dmi_check_system(dsdt_dmi_table);
540 status = acpi_reallocate_root_table();
541 if (ACPI_FAILURE(status)) {
542 printk(KERN_ERR PREFIX
543 "Unable to reallocate ACPI tables\n");
544 goto error0;
547 status = acpi_initialize_subsystem();
548 if (ACPI_FAILURE(status)) {
549 printk(KERN_ERR PREFIX
550 "Unable to initialize the ACPI Interpreter\n");
551 goto error0;
554 status = acpi_load_tables();
555 if (ACPI_FAILURE(status)) {
556 printk(KERN_ERR PREFIX
557 "Unable to load the System Description Tables\n");
558 goto error0;
561 #ifdef CONFIG_X86
562 if (!acpi_ioapic) {
563 /* compatible (0) means level (3) */
564 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
565 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
566 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
568 /* Set PIC-mode SCI trigger type */
569 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
570 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
571 } else {
573 * now that acpi_gbl_FADT is initialized,
574 * update it with result from INT_SRC_OVR parsing
576 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
578 #endif
580 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
581 if (ACPI_FAILURE(status)) {
582 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
583 goto error0;
587 * If the system is using ACPI then we can be reasonably
588 * confident that any regulators are managed by the firmware
589 * so tell the regulator core it has everything it needs to
590 * know.
592 regulator_has_full_constraints();
594 return;
596 error0:
597 disable_acpi();
598 return;
601 static int __init acpi_bus_init(void)
603 int result;
604 acpi_status status;
606 acpi_os_initialize1();
608 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
609 if (ACPI_FAILURE(status)) {
610 printk(KERN_ERR PREFIX
611 "Unable to start the ACPI Interpreter\n");
612 goto error1;
616 * ACPI 2.0 requires the EC driver to be loaded and work before
617 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
618 * is called).
620 * This is accomplished by looking for the ECDT table, and getting
621 * the EC parameters out of that.
623 status = acpi_ec_ecdt_probe();
624 /* Ignore result. Not having an ECDT is not fatal. */
626 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
627 if (ACPI_FAILURE(status)) {
628 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
629 goto error1;
633 * _OSC method may exist in module level code,
634 * so it must be run after ACPI_FULL_INITIALIZATION
636 acpi_bus_osc_support();
639 * _PDC control method may load dynamic SSDT tables,
640 * and we need to install the table handler before that.
642 acpi_sysfs_init();
644 acpi_early_processor_set_pdc();
647 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
648 * is necessary to enable it as early as possible.
650 acpi_boot_ec_enable();
652 printk(KERN_INFO PREFIX "Interpreter enabled\n");
654 /* Initialize sleep structures */
655 acpi_sleep_init();
658 * Get the system interrupt model and evaluate \_PIC.
660 result = acpi_bus_init_irq();
661 if (result)
662 goto error1;
665 * Register the for all standard device notifications.
667 status =
668 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
669 &acpi_bus_notify, NULL);
670 if (ACPI_FAILURE(status)) {
671 printk(KERN_ERR PREFIX
672 "Unable to register for device notifications\n");
673 goto error1;
677 * Create the top ACPI proc directory
679 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
681 return 0;
683 /* Mimic structured exception handling */
684 error1:
685 acpi_terminate();
686 return -ENODEV;
689 struct kobject *acpi_kobj;
690 EXPORT_SYMBOL_GPL(acpi_kobj);
692 static int __init acpi_init(void)
694 int result;
696 if (acpi_disabled) {
697 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
698 return -ENODEV;
701 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
702 if (!acpi_kobj) {
703 printk(KERN_WARNING "%s: kset create error\n", __func__);
704 acpi_kobj = NULL;
707 init_acpi_device_notify();
708 result = acpi_bus_init();
709 if (result) {
710 disable_acpi();
711 return result;
714 pci_mmcfg_late_init();
715 acpi_scan_init();
716 acpi_ec_init();
717 acpi_debugfs_init();
718 acpi_sleep_proc_init();
719 acpi_wakeup_device_init();
720 return 0;
723 subsys_initcall(acpi_init);