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>
32 #include <linux/device.h>
33 #include <linux/proc_fs.h>
34 #include <linux/acpi.h>
36 #include <asm/mpspec.h>
38 #include <linux/pci.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <linux/dmi.h>
45 #define _COMPONENT ACPI_BUS_COMPONENT
46 ACPI_MODULE_NAME("bus");
48 struct acpi_device *acpi_root;
49 struct proc_dir_entry *acpi_root_dir;
50 EXPORT_SYMBOL(acpi_root_dir);
52 #define STRUCT_TO_INT(s) (*((int*)&s))
54 static int set_power_nocheck(const struct dmi_system_id *id)
56 printk(KERN_NOTICE PREFIX "%s detected - "
57 "disable power check in power transistion\n", id->ident);
58 acpi_power_nocheck = 1;
61 static struct dmi_system_id __cpuinitdata power_nocheck_dmi_table[] = {
63 set_power_nocheck, "HP Pavilion 05", {
64 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
65 DMI_MATCH(DMI_SYS_VENDOR, "HP Pavilion 05"),
66 DMI_MATCH(DMI_PRODUCT_VERSION, "2001211RE101GLEND") }, NULL},
71 /* --------------------------------------------------------------------------
73 -------------------------------------------------------------------------- */
75 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
77 acpi_status status = AE_OK;
83 /* TBD: Support fixed-feature devices */
85 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
86 if (ACPI_FAILURE(status) || !*device) {
87 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
95 EXPORT_SYMBOL(acpi_bus_get_device);
97 int acpi_bus_get_status(struct acpi_device *device)
99 acpi_status status = AE_OK;
100 unsigned long long sta = 0;
107 * Evaluate _STA if present.
109 if (device->flags.dynamic_status) {
111 acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
112 if (ACPI_FAILURE(status))
114 STRUCT_TO_INT(device->status) = (int)sta;
118 * According to ACPI spec some device can be present and functional
119 * even if the parent is not present but functional.
120 * In such conditions the child device should not inherit the status
124 STRUCT_TO_INT(device->status) =
125 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
126 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
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",
132 (u32) STRUCT_TO_INT(device->status)));
135 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
137 (u32) STRUCT_TO_INT(device->status)));
142 EXPORT_SYMBOL(acpi_bus_get_status);
144 void acpi_bus_private_data_handler(acpi_handle handle,
149 EXPORT_SYMBOL(acpi_bus_private_data_handler);
151 int acpi_bus_get_private_data(acpi_handle handle, void **data)
153 acpi_status status = AE_OK;
158 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
159 if (ACPI_FAILURE(status) || !*data) {
160 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
167 EXPORT_SYMBOL(acpi_bus_get_private_data);
169 /* --------------------------------------------------------------------------
171 -------------------------------------------------------------------------- */
173 int acpi_bus_get_power(acpi_handle handle, int *state)
176 acpi_status status = 0;
177 struct acpi_device *device = NULL;
178 unsigned long long psc = 0;
181 result = acpi_bus_get_device(handle, &device);
185 *state = ACPI_STATE_UNKNOWN;
187 if (!device->flags.power_manageable) {
188 /* TBD: Non-recursive algorithm for walking up hierarchy */
190 *state = device->parent->power.state;
192 *state = ACPI_STATE_D0;
195 * Get the device's power state either directly (via _PSC) or
196 * indirectly (via power resources).
198 if (device->power.flags.explicit_get) {
199 status = acpi_evaluate_integer(device->handle, "_PSC",
201 if (ACPI_FAILURE(status))
203 device->power.state = (int)psc;
204 } else if (device->power.flags.power_resources) {
205 result = acpi_power_get_inferred_state(device);
210 *state = device->power.state;
213 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
214 device->pnp.bus_id, device->power.state));
219 EXPORT_SYMBOL(acpi_bus_get_power);
221 int acpi_bus_set_power(acpi_handle handle, int state)
224 acpi_status status = AE_OK;
225 struct acpi_device *device = NULL;
226 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
229 result = acpi_bus_get_device(handle, &device);
233 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
236 /* Make sure this is a valid target state */
238 if (!device->flags.power_manageable) {
239 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
240 kobject_name(&device->dev.kobj)));
244 * Get device's current power state
246 if (!acpi_power_nocheck) {
248 * Maybe the incorrect power state is returned on the bogus
249 * bios, which is different with the real power state.
250 * For example: the bios returns D0 state and the real power
251 * state is D3. OS expects to set the device to D0 state. In
252 * such case if OS uses the power state returned by the BIOS,
253 * the device can't be transisted to the correct power state.
254 * So if the acpi_power_nocheck is set, it is unnecessary to
255 * get the power state by calling acpi_bus_get_power.
257 acpi_bus_get_power(device->handle, &device->power.state);
259 if ((state == device->power.state) && !device->flags.force_power_state) {
260 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
265 if (!device->power.states[state].flags.valid) {
266 printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
269 if (device->parent && (state < device->parent->power.state)) {
270 printk(KERN_WARNING PREFIX
271 "Cannot set device to a higher-powered"
272 " state than parent\n");
279 * On transitions to a high-powered state we first apply power (via
280 * power resources) then evalute _PSx. Conversly for transitions to
281 * a lower-powered state.
283 if (state < device->power.state) {
284 if (device->power.flags.power_resources) {
285 result = acpi_power_transition(device, state);
289 if (device->power.states[state].flags.explicit_set) {
290 status = acpi_evaluate_object(device->handle,
291 object_name, NULL, NULL);
292 if (ACPI_FAILURE(status)) {
298 if (device->power.states[state].flags.explicit_set) {
299 status = acpi_evaluate_object(device->handle,
300 object_name, NULL, NULL);
301 if (ACPI_FAILURE(status)) {
306 if (device->power.flags.power_resources) {
307 result = acpi_power_transition(device, state);
315 printk(KERN_WARNING PREFIX
316 "Device [%s] failed to transition to D%d\n",
317 device->pnp.bus_id, state);
319 device->power.state = state;
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "Device [%s] transitioned to D%d\n",
322 device->pnp.bus_id, state));
328 EXPORT_SYMBOL(acpi_bus_set_power);
330 bool acpi_bus_power_manageable(acpi_handle handle)
332 struct acpi_device *device;
335 result = acpi_bus_get_device(handle, &device);
336 return result ? false : device->flags.power_manageable;
339 EXPORT_SYMBOL(acpi_bus_power_manageable);
341 bool acpi_bus_can_wakeup(acpi_handle handle)
343 struct acpi_device *device;
346 result = acpi_bus_get_device(handle, &device);
347 return result ? false : device->wakeup.flags.valid;
350 EXPORT_SYMBOL(acpi_bus_can_wakeup);
352 /* --------------------------------------------------------------------------
354 -------------------------------------------------------------------------- */
356 #ifdef CONFIG_ACPI_PROC_EVENT
357 static DEFINE_SPINLOCK(acpi_bus_event_lock);
359 LIST_HEAD(acpi_bus_event_list);
360 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
362 extern int event_is_open;
364 int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
366 struct acpi_bus_event *event;
367 unsigned long flags = 0;
369 /* drop event on the floor if no one's listening */
373 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
377 strcpy(event->device_class, device_class);
378 strcpy(event->bus_id, bus_id);
382 spin_lock_irqsave(&acpi_bus_event_lock, flags);
383 list_add_tail(&event->node, &acpi_bus_event_list);
384 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
386 wake_up_interruptible(&acpi_bus_event_queue);
392 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
394 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
398 return acpi_bus_generate_proc_event4(device->pnp.device_class,
399 device->pnp.bus_id, type, data);
402 EXPORT_SYMBOL(acpi_bus_generate_proc_event);
404 int acpi_bus_receive_event(struct acpi_bus_event *event)
406 unsigned long flags = 0;
407 struct acpi_bus_event *entry = NULL;
409 DECLARE_WAITQUEUE(wait, current);
415 if (list_empty(&acpi_bus_event_list)) {
417 set_current_state(TASK_INTERRUPTIBLE);
418 add_wait_queue(&acpi_bus_event_queue, &wait);
420 if (list_empty(&acpi_bus_event_list))
423 remove_wait_queue(&acpi_bus_event_queue, &wait);
424 set_current_state(TASK_RUNNING);
426 if (signal_pending(current))
430 spin_lock_irqsave(&acpi_bus_event_lock, flags);
431 if (!list_empty(&acpi_bus_event_list)) {
432 entry = list_entry(acpi_bus_event_list.next,
433 struct acpi_bus_event, node);
434 list_del(&entry->node);
436 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
441 memcpy(event, entry, sizeof(struct acpi_bus_event));
448 #endif /* CONFIG_ACPI_PROC_EVENT */
450 /* --------------------------------------------------------------------------
451 Notification Handling
452 -------------------------------------------------------------------------- */
454 static void acpi_bus_check_device(acpi_handle handle)
456 struct acpi_device *device;
458 struct acpi_device_status old_status;
460 if (acpi_bus_get_device(handle, &device))
465 old_status = device->status;
468 * Make sure this device's parent is present before we go about
469 * messing with the device.
471 if (device->parent && !device->parent->status.present) {
472 device->status = device->parent->status;
476 status = acpi_bus_get_status(device);
477 if (ACPI_FAILURE(status))
480 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
484 * Device Insertion/Removal
486 if ((device->status.present) && !(old_status.present)) {
487 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
488 /* TBD: Handle device insertion */
489 } else if (!(device->status.present) && (old_status.present)) {
490 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
491 /* TBD: Handle device removal */
495 static void acpi_bus_check_scope(acpi_handle handle)
498 acpi_bus_check_device(handle);
501 * TBD: Enumerate child devices within this device's scope and
502 * run acpi_bus_check_device()'s on them.
506 static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
507 int register_acpi_bus_notifier(struct notifier_block *nb)
509 return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
511 EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
513 void unregister_acpi_bus_notifier(struct notifier_block *nb)
515 blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
517 EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
522 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
524 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
526 struct acpi_device *device = NULL;
527 struct acpi_driver *driver;
529 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
532 blocking_notifier_call_chain(&acpi_bus_notify_list,
533 type, (void *)handle);
537 case ACPI_NOTIFY_BUS_CHECK:
538 acpi_bus_check_scope(handle);
540 * TBD: We'll need to outsource certain events to non-ACPI
541 * drivers via the device manager (device.c).
545 case ACPI_NOTIFY_DEVICE_CHECK:
546 acpi_bus_check_device(handle);
548 * TBD: We'll need to outsource certain events to non-ACPI
549 * drivers via the device manager (device.c).
553 case ACPI_NOTIFY_DEVICE_WAKE:
557 case ACPI_NOTIFY_EJECT_REQUEST:
561 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
562 /* TBD: Exactly what does 'light' mean? */
565 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
569 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
573 case ACPI_NOTIFY_POWER_FAULT:
578 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
579 "Received unknown/unsupported notification [%08x]\n",
584 acpi_bus_get_device(handle, &device);
586 driver = device->driver;
587 if (driver && driver->ops.notify &&
588 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
589 driver->ops.notify(device, type);
593 /* --------------------------------------------------------------------------
594 Initialization/Cleanup
595 -------------------------------------------------------------------------- */
597 static int __init acpi_bus_init_irq(void)
599 acpi_status status = AE_OK;
600 union acpi_object arg = { ACPI_TYPE_INTEGER };
601 struct acpi_object_list arg_list = { 1, &arg };
602 char *message = NULL;
606 * Let the system know what interrupt model we are using by
607 * evaluating the \_PIC object, if exists.
610 switch (acpi_irq_model) {
611 case ACPI_IRQ_MODEL_PIC:
614 case ACPI_IRQ_MODEL_IOAPIC:
617 case ACPI_IRQ_MODEL_IOSAPIC:
620 case ACPI_IRQ_MODEL_PLATFORM:
621 message = "platform specific model";
624 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
628 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
630 arg.integer.value = acpi_irq_model;
632 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
633 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
634 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
641 u8 acpi_gbl_permanent_mmap;
644 void __init acpi_early_init(void)
646 acpi_status status = AE_OK;
651 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
653 /* enable workarounds, unless strict ACPI spec. compliance */
655 acpi_gbl_enable_interpreter_slack = TRUE;
657 acpi_gbl_permanent_mmap = 1;
659 status = acpi_reallocate_root_table();
660 if (ACPI_FAILURE(status)) {
661 printk(KERN_ERR PREFIX
662 "Unable to reallocate ACPI tables\n");
666 status = acpi_initialize_subsystem();
667 if (ACPI_FAILURE(status)) {
668 printk(KERN_ERR PREFIX
669 "Unable to initialize the ACPI Interpreter\n");
673 status = acpi_load_tables();
674 if (ACPI_FAILURE(status)) {
675 printk(KERN_ERR PREFIX
676 "Unable to load the System Description Tables\n");
682 /* compatible (0) means level (3) */
683 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
684 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
685 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
687 /* Set PIC-mode SCI trigger type */
688 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
689 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
692 * now that acpi_gbl_FADT is initialized,
693 * update it with result from INT_SRC_OVR parsing
695 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
700 acpi_enable_subsystem(~
701 (ACPI_NO_HARDWARE_INIT |
702 ACPI_NO_ACPI_ENABLE));
703 if (ACPI_FAILURE(status)) {
704 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
715 static int __init acpi_bus_init(void)
718 acpi_status status = AE_OK;
719 extern acpi_status acpi_os_initialize1(void);
721 acpi_os_initialize1();
724 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
725 if (ACPI_FAILURE(status)) {
726 printk(KERN_ERR PREFIX
727 "Unable to start the ACPI Interpreter\n");
732 * ACPI 2.0 requires the EC driver to be loaded and work before
733 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
736 * This is accomplished by looking for the ECDT table, and getting
737 * the EC parameters out of that.
739 status = acpi_ec_ecdt_probe();
740 /* Ignore result. Not having an ECDT is not fatal. */
742 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
743 if (ACPI_FAILURE(status)) {
744 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
749 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
750 * is necessary to enable it as early as possible.
752 acpi_boot_ec_enable();
754 printk(KERN_INFO PREFIX "Interpreter enabled\n");
756 /* Initialize sleep structures */
760 * Get the system interrupt model and evaluate \_PIC.
762 result = acpi_bus_init_irq();
767 * Register the for all standard device notifications.
770 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
771 &acpi_bus_notify, NULL);
772 if (ACPI_FAILURE(status)) {
773 printk(KERN_ERR PREFIX
774 "Unable to register for device notifications\n");
779 * Create the top ACPI proc directory
781 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
785 /* Mimic structured exception handling */
791 struct kobject *acpi_kobj;
793 static int __init acpi_init(void)
799 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
803 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
805 printk(KERN_WARNING "%s: kset create error\n", __func__);
809 init_acpi_device_notify();
810 result = acpi_bus_init();
813 pci_mmcfg_late_init();
814 if (!(pm_flags & PM_APM))
817 printk(KERN_INFO PREFIX
818 "APM is already active, exiting\n");
829 * If the laptop falls into the DMI check table, the power state check
830 * will be disabled in the course of device power transistion.
832 dmi_check_system(power_nocheck_dmi_table);
839 acpi_sleep_proc_init();
840 acpi_wakeup_device_init();
844 subsys_initcall(acpi_init);