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>
44 #define _COMPONENT ACPI_BUS_COMPONENT
45 ACPI_MODULE_NAME("bus");
47 struct acpi_device
*acpi_root
;
48 struct proc_dir_entry
*acpi_root_dir
;
49 EXPORT_SYMBOL(acpi_root_dir
);
51 #define STRUCT_TO_INT(s) (*((int*)&s))
53 static int set_power_nocheck(const struct dmi_system_id
*id
)
55 printk(KERN_NOTICE PREFIX
"%s detected - "
56 "disable power check in power transistion\n", id
->ident
);
57 acpi_power_nocheck
= 1;
60 static struct dmi_system_id __cpuinitdata power_nocheck_dmi_table
[] = {
62 set_power_nocheck
, "HP Pavilion 05", {
63 DMI_MATCH(DMI_BIOS_VENDOR
, "Phoenix Technologies LTD"),
64 DMI_MATCH(DMI_SYS_VENDOR
, "HP Pavilion 05"),
65 DMI_MATCH(DMI_PRODUCT_VERSION
, "2001211RE101GLEND") }, NULL
},
70 /* --------------------------------------------------------------------------
72 -------------------------------------------------------------------------- */
74 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
76 acpi_status status
= AE_OK
;
82 /* TBD: Support fixed-feature devices */
84 status
= acpi_get_data(handle
, acpi_bus_data_handler
, (void **)device
);
85 if (ACPI_FAILURE(status
) || !*device
) {
86 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
94 EXPORT_SYMBOL(acpi_bus_get_device
);
96 int acpi_bus_get_status(struct acpi_device
*device
)
98 acpi_status status
= AE_OK
;
99 unsigned long long sta
= 0;
106 * Evaluate _STA if present.
108 if (device
->flags
.dynamic_status
) {
110 acpi_evaluate_integer(device
->handle
, "_STA", NULL
, &sta
);
111 if (ACPI_FAILURE(status
))
113 STRUCT_TO_INT(device
->status
) = (int)sta
;
117 * According to ACPI spec some device can be present and functional
118 * even if the parent is not present but functional.
119 * In such conditions the child device should not inherit the status
123 STRUCT_TO_INT(device
->status
) =
124 ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
125 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
127 if (device
->status
.functional
&& !device
->status
.present
) {
128 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]: "
129 "functional but not present;\n",
131 (u32
) STRUCT_TO_INT(device
->status
)));
134 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]\n",
136 (u32
) STRUCT_TO_INT(device
->status
)));
141 EXPORT_SYMBOL(acpi_bus_get_status
);
143 void acpi_bus_private_data_handler(acpi_handle handle
,
144 u32 function
, void *context
)
148 EXPORT_SYMBOL(acpi_bus_private_data_handler
);
150 int acpi_bus_get_private_data(acpi_handle handle
, void **data
)
152 acpi_status status
= AE_OK
;
157 status
= acpi_get_data(handle
, acpi_bus_private_data_handler
, data
);
158 if (ACPI_FAILURE(status
) || !*data
) {
159 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
166 EXPORT_SYMBOL(acpi_bus_get_private_data
);
168 /* --------------------------------------------------------------------------
170 -------------------------------------------------------------------------- */
172 int acpi_bus_get_power(acpi_handle handle
, int *state
)
175 acpi_status status
= 0;
176 struct acpi_device
*device
= NULL
;
177 unsigned long long psc
= 0;
180 result
= acpi_bus_get_device(handle
, &device
);
184 *state
= ACPI_STATE_UNKNOWN
;
186 if (!device
->flags
.power_manageable
) {
187 /* TBD: Non-recursive algorithm for walking up hierarchy */
189 *state
= device
->parent
->power
.state
;
191 *state
= ACPI_STATE_D0
;
194 * Get the device's power state either directly (via _PSC) or
195 * indirectly (via power resources).
197 if (device
->power
.flags
.explicit_get
) {
198 status
= acpi_evaluate_integer(device
->handle
, "_PSC",
200 if (ACPI_FAILURE(status
))
202 device
->power
.state
= (int)psc
;
203 } else if (device
->power
.flags
.power_resources
) {
204 result
= acpi_power_get_inferred_state(device
);
209 *state
= device
->power
.state
;
212 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] power state is D%d\n",
213 device
->pnp
.bus_id
, device
->power
.state
));
218 EXPORT_SYMBOL(acpi_bus_get_power
);
220 int acpi_bus_set_power(acpi_handle handle
, int state
)
223 acpi_status status
= AE_OK
;
224 struct acpi_device
*device
= NULL
;
225 char object_name
[5] = { '_', 'P', 'S', '0' + state
, '\0' };
228 result
= acpi_bus_get_device(handle
, &device
);
232 if ((state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
235 /* Make sure this is a valid target state */
237 if (!device
->flags
.power_manageable
) {
238 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device `[%s]' is not power manageable\n",
239 kobject_name(&device
->dev
.kobj
)));
243 * Get device's current power state
245 if (!acpi_power_nocheck
) {
247 * Maybe the incorrect power state is returned on the bogus
248 * bios, which is different with the real power state.
249 * For example: the bios returns D0 state and the real power
250 * state is D3. OS expects to set the device to D0 state. In
251 * such case if OS uses the power state returned by the BIOS,
252 * the device can't be transisted to the correct power state.
253 * So if the acpi_power_nocheck is set, it is unnecessary to
254 * get the power state by calling acpi_bus_get_power.
256 acpi_bus_get_power(device
->handle
, &device
->power
.state
);
258 if ((state
== device
->power
.state
) && !device
->flags
.force_power_state
) {
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device is already at D%d\n",
264 if (!device
->power
.states
[state
].flags
.valid
) {
265 printk(KERN_WARNING PREFIX
"Device does not support D%d\n", state
);
268 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
269 printk(KERN_WARNING PREFIX
270 "Cannot set device to a higher-powered"
271 " state than parent\n");
278 * On transitions to a high-powered state we first apply power (via
279 * power resources) then evalute _PSx. Conversly for transitions to
280 * a lower-powered state.
282 if (state
< device
->power
.state
) {
283 if (device
->power
.flags
.power_resources
) {
284 result
= acpi_power_transition(device
, state
);
288 if (device
->power
.states
[state
].flags
.explicit_set
) {
289 status
= acpi_evaluate_object(device
->handle
,
290 object_name
, NULL
, NULL
);
291 if (ACPI_FAILURE(status
)) {
297 if (device
->power
.states
[state
].flags
.explicit_set
) {
298 status
= acpi_evaluate_object(device
->handle
,
299 object_name
, NULL
, NULL
);
300 if (ACPI_FAILURE(status
)) {
305 if (device
->power
.flags
.power_resources
) {
306 result
= acpi_power_transition(device
, state
);
314 printk(KERN_WARNING PREFIX
315 "Device [%s] failed to transition to D%d\n",
316 device
->pnp
.bus_id
, state
);
318 device
->power
.state
= state
;
319 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
320 "Device [%s] transitioned to D%d\n",
321 device
->pnp
.bus_id
, state
));
327 EXPORT_SYMBOL(acpi_bus_set_power
);
329 bool acpi_bus_power_manageable(acpi_handle handle
)
331 struct acpi_device
*device
;
334 result
= acpi_bus_get_device(handle
, &device
);
335 return result
? false : device
->flags
.power_manageable
;
338 EXPORT_SYMBOL(acpi_bus_power_manageable
);
340 bool acpi_bus_can_wakeup(acpi_handle handle
)
342 struct acpi_device
*device
;
345 result
= acpi_bus_get_device(handle
, &device
);
346 return result
? false : device
->wakeup
.flags
.valid
;
349 EXPORT_SYMBOL(acpi_bus_can_wakeup
);
351 /* --------------------------------------------------------------------------
353 -------------------------------------------------------------------------- */
355 #ifdef CONFIG_ACPI_PROC_EVENT
356 static DEFINE_SPINLOCK(acpi_bus_event_lock
);
358 LIST_HEAD(acpi_bus_event_list
);
359 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue
);
361 extern int event_is_open
;
363 int acpi_bus_generate_proc_event4(const char *device_class
, const char *bus_id
, u8 type
, int data
)
365 struct acpi_bus_event
*event
;
366 unsigned long flags
= 0;
368 /* drop event on the floor if no one's listening */
372 event
= kmalloc(sizeof(struct acpi_bus_event
), GFP_ATOMIC
);
376 strcpy(event
->device_class
, device_class
);
377 strcpy(event
->bus_id
, bus_id
);
381 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
382 list_add_tail(&event
->node
, &acpi_bus_event_list
);
383 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
385 wake_up_interruptible(&acpi_bus_event_queue
);
391 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4
);
393 int acpi_bus_generate_proc_event(struct acpi_device
*device
, u8 type
, int data
)
397 return acpi_bus_generate_proc_event4(device
->pnp
.device_class
,
398 device
->pnp
.bus_id
, type
, data
);
401 EXPORT_SYMBOL(acpi_bus_generate_proc_event
);
403 int acpi_bus_receive_event(struct acpi_bus_event
*event
)
405 unsigned long flags
= 0;
406 struct acpi_bus_event
*entry
= NULL
;
408 DECLARE_WAITQUEUE(wait
, current
);
414 if (list_empty(&acpi_bus_event_list
)) {
416 set_current_state(TASK_INTERRUPTIBLE
);
417 add_wait_queue(&acpi_bus_event_queue
, &wait
);
419 if (list_empty(&acpi_bus_event_list
))
422 remove_wait_queue(&acpi_bus_event_queue
, &wait
);
423 set_current_state(TASK_RUNNING
);
425 if (signal_pending(current
))
429 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
430 if (!list_empty(&acpi_bus_event_list
)) {
431 entry
= list_entry(acpi_bus_event_list
.next
,
432 struct acpi_bus_event
, node
);
433 list_del(&entry
->node
);
435 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
440 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
447 #endif /* CONFIG_ACPI_PROC_EVENT */
449 /* --------------------------------------------------------------------------
450 Notification Handling
451 -------------------------------------------------------------------------- */
454 acpi_bus_check_device(struct acpi_device
*device
, int *status_changed
)
456 acpi_status status
= 0;
457 struct acpi_device_status old_status
;
466 old_status
= device
->status
;
469 * Make sure this device's parent is present before we go about
470 * messing with the device.
472 if (device
->parent
&& !device
->parent
->status
.present
) {
473 device
->status
= device
->parent
->status
;
474 if (STRUCT_TO_INT(old_status
) != STRUCT_TO_INT(device
->status
)) {
481 status
= acpi_bus_get_status(device
);
482 if (ACPI_FAILURE(status
))
485 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
492 * Device Insertion/Removal
494 if ((device
->status
.present
) && !(old_status
.present
)) {
495 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device insertion detected\n"));
496 /* TBD: Handle device insertion */
497 } else if (!(device
->status
.present
) && (old_status
.present
)) {
498 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device removal detected\n"));
499 /* TBD: Handle device removal */
505 static int acpi_bus_check_scope(struct acpi_device
*device
)
508 int status_changed
= 0;
515 result
= acpi_bus_check_device(device
, &status_changed
);
523 * TBD: Enumerate child devices within this device's scope and
524 * run acpi_bus_check_device()'s on them.
530 static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list
);
531 int register_acpi_bus_notifier(struct notifier_block
*nb
)
533 return blocking_notifier_chain_register(&acpi_bus_notify_list
, nb
);
535 EXPORT_SYMBOL_GPL(register_acpi_bus_notifier
);
537 void unregister_acpi_bus_notifier(struct notifier_block
*nb
)
539 blocking_notifier_chain_unregister(&acpi_bus_notify_list
, nb
);
541 EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier
);
546 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
548 static void acpi_bus_notify(acpi_handle handle
, u32 type
, void *data
)
551 struct acpi_device
*device
= NULL
;
553 blocking_notifier_call_chain(&acpi_bus_notify_list
,
554 type
, (void *)handle
);
556 if (acpi_bus_get_device(handle
, &device
))
561 case ACPI_NOTIFY_BUS_CHECK
:
562 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
563 "Received BUS CHECK notification for device [%s]\n",
564 device
->pnp
.bus_id
));
565 result
= acpi_bus_check_scope(device
);
567 * TBD: We'll need to outsource certain events to non-ACPI
568 * drivers via the device manager (device.c).
572 case ACPI_NOTIFY_DEVICE_CHECK
:
573 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
574 "Received DEVICE CHECK notification for device [%s]\n",
575 device
->pnp
.bus_id
));
576 result
= acpi_bus_check_device(device
, NULL
);
578 * TBD: We'll need to outsource certain events to non-ACPI
579 * drivers via the device manager (device.c).
583 case ACPI_NOTIFY_DEVICE_WAKE
:
584 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
585 "Received DEVICE WAKE notification for device [%s]\n",
586 device
->pnp
.bus_id
));
590 case ACPI_NOTIFY_EJECT_REQUEST
:
591 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
592 "Received EJECT REQUEST notification for device [%s]\n",
593 device
->pnp
.bus_id
));
597 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
598 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
599 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
600 device
->pnp
.bus_id
));
601 /* TBD: Exactly what does 'light' mean? */
604 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
605 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
606 "Received FREQUENCY MISMATCH notification for device [%s]\n",
607 device
->pnp
.bus_id
));
611 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
612 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
613 "Received BUS MODE MISMATCH notification for device [%s]\n",
614 device
->pnp
.bus_id
));
618 case ACPI_NOTIFY_POWER_FAULT
:
619 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
620 "Received POWER FAULT notification for device [%s]\n",
621 device
->pnp
.bus_id
));
626 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
627 "Received unknown/unsupported notification [%08x]\n",
635 /* --------------------------------------------------------------------------
636 Initialization/Cleanup
637 -------------------------------------------------------------------------- */
639 static int __init
acpi_bus_init_irq(void)
641 acpi_status status
= AE_OK
;
642 union acpi_object arg
= { ACPI_TYPE_INTEGER
};
643 struct acpi_object_list arg_list
= { 1, &arg
};
644 char *message
= NULL
;
648 * Let the system know what interrupt model we are using by
649 * evaluating the \_PIC object, if exists.
652 switch (acpi_irq_model
) {
653 case ACPI_IRQ_MODEL_PIC
:
656 case ACPI_IRQ_MODEL_IOAPIC
:
659 case ACPI_IRQ_MODEL_IOSAPIC
:
662 case ACPI_IRQ_MODEL_PLATFORM
:
663 message
= "platform specific model";
666 printk(KERN_WARNING PREFIX
"Unknown interrupt routing model\n");
670 printk(KERN_INFO PREFIX
"Using %s for interrupt routing\n", message
);
672 arg
.integer
.value
= acpi_irq_model
;
674 status
= acpi_evaluate_object(NULL
, "\\_PIC", &arg_list
, NULL
);
675 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
676 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PIC"));
683 u8 acpi_gbl_permanent_mmap
;
686 void __init
acpi_early_init(void)
688 acpi_status status
= AE_OK
;
693 printk(KERN_INFO PREFIX
"Core revision %08x\n", ACPI_CA_VERSION
);
695 /* enable workarounds, unless strict ACPI spec. compliance */
697 acpi_gbl_enable_interpreter_slack
= TRUE
;
699 acpi_gbl_permanent_mmap
= 1;
701 status
= acpi_reallocate_root_table();
702 if (ACPI_FAILURE(status
)) {
703 printk(KERN_ERR PREFIX
704 "Unable to reallocate ACPI tables\n");
708 status
= acpi_initialize_subsystem();
709 if (ACPI_FAILURE(status
)) {
710 printk(KERN_ERR PREFIX
711 "Unable to initialize the ACPI Interpreter\n");
715 status
= acpi_load_tables();
716 if (ACPI_FAILURE(status
)) {
717 printk(KERN_ERR PREFIX
718 "Unable to load the System Description Tables\n");
724 /* compatible (0) means level (3) */
725 if (!(acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
)) {
726 acpi_sci_flags
&= ~ACPI_MADT_TRIGGER_MASK
;
727 acpi_sci_flags
|= ACPI_MADT_TRIGGER_LEVEL
;
729 /* Set PIC-mode SCI trigger type */
730 acpi_pic_sci_set_trigger(acpi_gbl_FADT
.sci_interrupt
,
731 (acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
) >> 2);
734 * now that acpi_gbl_FADT is initialized,
735 * update it with result from INT_SRC_OVR parsing
737 acpi_gbl_FADT
.sci_interrupt
= acpi_sci_override_gsi
;
742 acpi_enable_subsystem(~
743 (ACPI_NO_HARDWARE_INIT
|
744 ACPI_NO_ACPI_ENABLE
));
745 if (ACPI_FAILURE(status
)) {
746 printk(KERN_ERR PREFIX
"Unable to enable ACPI\n");
757 static int __init
acpi_bus_init(void)
760 acpi_status status
= AE_OK
;
761 extern acpi_status
acpi_os_initialize1(void);
763 acpi_os_initialize1();
766 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT
| ACPI_NO_ACPI_ENABLE
);
767 if (ACPI_FAILURE(status
)) {
768 printk(KERN_ERR PREFIX
769 "Unable to start the ACPI Interpreter\n");
774 * ACPI 2.0 requires the EC driver to be loaded and work before
775 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
778 * This is accomplished by looking for the ECDT table, and getting
779 * the EC parameters out of that.
781 status
= acpi_ec_ecdt_probe();
782 /* Ignore result. Not having an ECDT is not fatal. */
784 status
= acpi_initialize_objects(ACPI_FULL_INITIALIZATION
);
785 if (ACPI_FAILURE(status
)) {
786 printk(KERN_ERR PREFIX
"Unable to initialize ACPI objects\n");
791 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
792 * is necessary to enable it as early as possible.
794 acpi_boot_ec_enable();
796 printk(KERN_INFO PREFIX
"Interpreter enabled\n");
798 /* Initialize sleep structures */
802 * Get the system interrupt model and evaluate \_PIC.
804 result
= acpi_bus_init_irq();
809 * Register the for all standard device notifications.
812 acpi_install_notify_handler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
,
813 &acpi_bus_notify
, NULL
);
814 if (ACPI_FAILURE(status
)) {
815 printk(KERN_ERR PREFIX
816 "Unable to register for device notifications\n");
821 * Create the top ACPI proc directory
823 acpi_root_dir
= proc_mkdir(ACPI_BUS_FILE_ROOT
, NULL
);
827 /* Mimic structured exception handling */
833 struct kobject
*acpi_kobj
;
835 static int __init
acpi_init(void)
841 printk(KERN_INFO PREFIX
"Interpreter disabled.\n");
845 acpi_kobj
= kobject_create_and_add("acpi", firmware_kobj
);
847 printk(KERN_WARNING
"%s: kset create error\n", __func__
);
851 init_acpi_device_notify();
852 result
= acpi_bus_init();
855 pci_mmcfg_late_init();
856 if (!(pm_flags
& PM_APM
))
859 printk(KERN_INFO PREFIX
860 "APM is already active, exiting\n");
871 * If the laptop falls into the DMI check table, the power state check
872 * will be disabled in the course of device power transistion.
874 dmi_check_system(power_nocheck_dmi_table
);
881 acpi_sleep_proc_init();
882 acpi_wakeup_device_init();
886 subsys_initcall(acpi_init
);