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 acpi_status
acpi_bus_get_status_handle(acpi_handle handle
,
98 unsigned long long *sta
)
102 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, sta
);
103 if (ACPI_SUCCESS(status
))
106 if (status
== AE_NOT_FOUND
) {
107 *sta
= ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
108 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
114 int acpi_bus_get_status(struct acpi_device
*device
)
117 unsigned long long sta
;
119 status
= acpi_bus_get_status_handle(device
->handle
, &sta
);
120 if (ACPI_FAILURE(status
))
123 STRUCT_TO_INT(device
->status
) = (int) sta
;
125 if (device
->status
.functional
&& !device
->status
.present
) {
126 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]: "
127 "functional but not present;\n",
129 (u32
) STRUCT_TO_INT(device
->status
)));
132 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]\n",
134 (u32
) STRUCT_TO_INT(device
->status
)));
137 EXPORT_SYMBOL(acpi_bus_get_status
);
139 void acpi_bus_private_data_handler(acpi_handle handle
,
144 EXPORT_SYMBOL(acpi_bus_private_data_handler
);
146 int acpi_bus_get_private_data(acpi_handle handle
, void **data
)
148 acpi_status status
= AE_OK
;
153 status
= acpi_get_data(handle
, acpi_bus_private_data_handler
, data
);
154 if (ACPI_FAILURE(status
) || !*data
) {
155 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
162 EXPORT_SYMBOL(acpi_bus_get_private_data
);
164 /* --------------------------------------------------------------------------
166 -------------------------------------------------------------------------- */
168 int acpi_bus_get_power(acpi_handle handle
, int *state
)
171 acpi_status status
= 0;
172 struct acpi_device
*device
= NULL
;
173 unsigned long long psc
= 0;
176 result
= acpi_bus_get_device(handle
, &device
);
180 *state
= ACPI_STATE_UNKNOWN
;
182 if (!device
->flags
.power_manageable
) {
183 /* TBD: Non-recursive algorithm for walking up hierarchy */
185 *state
= device
->parent
->power
.state
;
187 *state
= ACPI_STATE_D0
;
190 * Get the device's power state either directly (via _PSC) or
191 * indirectly (via power resources).
193 if (device
->power
.flags
.explicit_get
) {
194 status
= acpi_evaluate_integer(device
->handle
, "_PSC",
196 if (ACPI_FAILURE(status
))
198 device
->power
.state
= (int)psc
;
199 } else if (device
->power
.flags
.power_resources
) {
200 result
= acpi_power_get_inferred_state(device
);
205 *state
= device
->power
.state
;
208 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] power state is D%d\n",
209 device
->pnp
.bus_id
, device
->power
.state
));
214 EXPORT_SYMBOL(acpi_bus_get_power
);
216 int acpi_bus_set_power(acpi_handle handle
, int state
)
219 acpi_status status
= AE_OK
;
220 struct acpi_device
*device
= NULL
;
221 char object_name
[5] = { '_', 'P', 'S', '0' + state
, '\0' };
224 result
= acpi_bus_get_device(handle
, &device
);
228 if ((state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
231 /* Make sure this is a valid target state */
233 if (!device
->flags
.power_manageable
) {
234 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device `[%s]' is not power manageable\n",
235 kobject_name(&device
->dev
.kobj
)));
239 * Get device's current power state
241 if (!acpi_power_nocheck
) {
243 * Maybe the incorrect power state is returned on the bogus
244 * bios, which is different with the real power state.
245 * For example: the bios returns D0 state and the real power
246 * state is D3. OS expects to set the device to D0 state. In
247 * such case if OS uses the power state returned by the BIOS,
248 * the device can't be transisted to the correct power state.
249 * So if the acpi_power_nocheck is set, it is unnecessary to
250 * get the power state by calling acpi_bus_get_power.
252 acpi_bus_get_power(device
->handle
, &device
->power
.state
);
254 if ((state
== device
->power
.state
) && !device
->flags
.force_power_state
) {
255 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device is already at D%d\n",
260 if (!device
->power
.states
[state
].flags
.valid
) {
261 printk(KERN_WARNING PREFIX
"Device does not support D%d\n", state
);
264 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
265 printk(KERN_WARNING PREFIX
266 "Cannot set device to a higher-powered"
267 " state than parent\n");
274 * On transitions to a high-powered state we first apply power (via
275 * power resources) then evalute _PSx. Conversly for transitions to
276 * a lower-powered state.
278 if (state
< device
->power
.state
) {
279 if (device
->power
.flags
.power_resources
) {
280 result
= acpi_power_transition(device
, state
);
284 if (device
->power
.states
[state
].flags
.explicit_set
) {
285 status
= acpi_evaluate_object(device
->handle
,
286 object_name
, NULL
, NULL
);
287 if (ACPI_FAILURE(status
)) {
293 if (device
->power
.states
[state
].flags
.explicit_set
) {
294 status
= acpi_evaluate_object(device
->handle
,
295 object_name
, NULL
, NULL
);
296 if (ACPI_FAILURE(status
)) {
301 if (device
->power
.flags
.power_resources
) {
302 result
= acpi_power_transition(device
, state
);
310 printk(KERN_WARNING PREFIX
311 "Device [%s] failed to transition to D%d\n",
312 device
->pnp
.bus_id
, state
);
314 device
->power
.state
= state
;
315 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
316 "Device [%s] transitioned to D%d\n",
317 device
->pnp
.bus_id
, state
));
323 EXPORT_SYMBOL(acpi_bus_set_power
);
325 bool acpi_bus_power_manageable(acpi_handle handle
)
327 struct acpi_device
*device
;
330 result
= acpi_bus_get_device(handle
, &device
);
331 return result
? false : device
->flags
.power_manageable
;
334 EXPORT_SYMBOL(acpi_bus_power_manageable
);
336 bool acpi_bus_can_wakeup(acpi_handle handle
)
338 struct acpi_device
*device
;
341 result
= acpi_bus_get_device(handle
, &device
);
342 return result
? false : device
->wakeup
.flags
.valid
;
345 EXPORT_SYMBOL(acpi_bus_can_wakeup
);
347 /* --------------------------------------------------------------------------
349 -------------------------------------------------------------------------- */
351 #ifdef CONFIG_ACPI_PROC_EVENT
352 static DEFINE_SPINLOCK(acpi_bus_event_lock
);
354 LIST_HEAD(acpi_bus_event_list
);
355 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue
);
357 extern int event_is_open
;
359 int acpi_bus_generate_proc_event4(const char *device_class
, const char *bus_id
, u8 type
, int data
)
361 struct acpi_bus_event
*event
;
362 unsigned long flags
= 0;
364 /* drop event on the floor if no one's listening */
368 event
= kmalloc(sizeof(struct acpi_bus_event
), GFP_ATOMIC
);
372 strcpy(event
->device_class
, device_class
);
373 strcpy(event
->bus_id
, bus_id
);
377 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
378 list_add_tail(&event
->node
, &acpi_bus_event_list
);
379 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
381 wake_up_interruptible(&acpi_bus_event_queue
);
387 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4
);
389 int acpi_bus_generate_proc_event(struct acpi_device
*device
, u8 type
, int data
)
393 return acpi_bus_generate_proc_event4(device
->pnp
.device_class
,
394 device
->pnp
.bus_id
, type
, data
);
397 EXPORT_SYMBOL(acpi_bus_generate_proc_event
);
399 int acpi_bus_receive_event(struct acpi_bus_event
*event
)
401 unsigned long flags
= 0;
402 struct acpi_bus_event
*entry
= NULL
;
404 DECLARE_WAITQUEUE(wait
, current
);
410 if (list_empty(&acpi_bus_event_list
)) {
412 set_current_state(TASK_INTERRUPTIBLE
);
413 add_wait_queue(&acpi_bus_event_queue
, &wait
);
415 if (list_empty(&acpi_bus_event_list
))
418 remove_wait_queue(&acpi_bus_event_queue
, &wait
);
419 set_current_state(TASK_RUNNING
);
421 if (signal_pending(current
))
425 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
426 if (!list_empty(&acpi_bus_event_list
)) {
427 entry
= list_entry(acpi_bus_event_list
.next
,
428 struct acpi_bus_event
, node
);
429 list_del(&entry
->node
);
431 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
436 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
443 #endif /* CONFIG_ACPI_PROC_EVENT */
445 /* --------------------------------------------------------------------------
446 Notification Handling
447 -------------------------------------------------------------------------- */
449 static void acpi_bus_check_device(acpi_handle handle
)
451 struct acpi_device
*device
;
453 struct acpi_device_status old_status
;
455 if (acpi_bus_get_device(handle
, &device
))
460 old_status
= device
->status
;
463 * Make sure this device's parent is present before we go about
464 * messing with the device.
466 if (device
->parent
&& !device
->parent
->status
.present
) {
467 device
->status
= device
->parent
->status
;
471 status
= acpi_bus_get_status(device
);
472 if (ACPI_FAILURE(status
))
475 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
479 * Device Insertion/Removal
481 if ((device
->status
.present
) && !(old_status
.present
)) {
482 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device insertion detected\n"));
483 /* TBD: Handle device insertion */
484 } else if (!(device
->status
.present
) && (old_status
.present
)) {
485 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device removal detected\n"));
486 /* TBD: Handle device removal */
490 static void acpi_bus_check_scope(acpi_handle handle
)
493 acpi_bus_check_device(handle
);
496 * TBD: Enumerate child devices within this device's scope and
497 * run acpi_bus_check_device()'s on them.
501 static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list
);
502 int register_acpi_bus_notifier(struct notifier_block
*nb
)
504 return blocking_notifier_chain_register(&acpi_bus_notify_list
, nb
);
506 EXPORT_SYMBOL_GPL(register_acpi_bus_notifier
);
508 void unregister_acpi_bus_notifier(struct notifier_block
*nb
)
510 blocking_notifier_chain_unregister(&acpi_bus_notify_list
, nb
);
512 EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier
);
517 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
519 static void acpi_bus_notify(acpi_handle handle
, u32 type
, void *data
)
521 struct acpi_device
*device
= NULL
;
522 struct acpi_driver
*driver
;
524 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Notification %#02x to handle %p\n",
527 blocking_notifier_call_chain(&acpi_bus_notify_list
,
528 type
, (void *)handle
);
532 case ACPI_NOTIFY_BUS_CHECK
:
533 acpi_bus_check_scope(handle
);
535 * TBD: We'll need to outsource certain events to non-ACPI
536 * drivers via the device manager (device.c).
540 case ACPI_NOTIFY_DEVICE_CHECK
:
541 acpi_bus_check_device(handle
);
543 * TBD: We'll need to outsource certain events to non-ACPI
544 * drivers via the device manager (device.c).
548 case ACPI_NOTIFY_DEVICE_WAKE
:
552 case ACPI_NOTIFY_EJECT_REQUEST
:
556 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
557 /* TBD: Exactly what does 'light' mean? */
560 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
564 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
568 case ACPI_NOTIFY_POWER_FAULT
:
573 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
574 "Received unknown/unsupported notification [%08x]\n",
579 acpi_bus_get_device(handle
, &device
);
581 driver
= device
->driver
;
582 if (driver
&& driver
->ops
.notify
&&
583 (driver
->flags
& ACPI_DRIVER_ALL_NOTIFY_EVENTS
))
584 driver
->ops
.notify(device
, type
);
588 /* --------------------------------------------------------------------------
589 Initialization/Cleanup
590 -------------------------------------------------------------------------- */
592 static int __init
acpi_bus_init_irq(void)
594 acpi_status status
= AE_OK
;
595 union acpi_object arg
= { ACPI_TYPE_INTEGER
};
596 struct acpi_object_list arg_list
= { 1, &arg
};
597 char *message
= NULL
;
601 * Let the system know what interrupt model we are using by
602 * evaluating the \_PIC object, if exists.
605 switch (acpi_irq_model
) {
606 case ACPI_IRQ_MODEL_PIC
:
609 case ACPI_IRQ_MODEL_IOAPIC
:
612 case ACPI_IRQ_MODEL_IOSAPIC
:
615 case ACPI_IRQ_MODEL_PLATFORM
:
616 message
= "platform specific model";
619 printk(KERN_WARNING PREFIX
"Unknown interrupt routing model\n");
623 printk(KERN_INFO PREFIX
"Using %s for interrupt routing\n", message
);
625 arg
.integer
.value
= acpi_irq_model
;
627 status
= acpi_evaluate_object(NULL
, "\\_PIC", &arg_list
, NULL
);
628 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
629 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PIC"));
636 u8 acpi_gbl_permanent_mmap
;
639 void __init
acpi_early_init(void)
641 acpi_status status
= AE_OK
;
646 printk(KERN_INFO PREFIX
"Core revision %08x\n", ACPI_CA_VERSION
);
648 /* enable workarounds, unless strict ACPI spec. compliance */
650 acpi_gbl_enable_interpreter_slack
= TRUE
;
652 acpi_gbl_permanent_mmap
= 1;
654 status
= acpi_reallocate_root_table();
655 if (ACPI_FAILURE(status
)) {
656 printk(KERN_ERR PREFIX
657 "Unable to reallocate ACPI tables\n");
661 status
= acpi_initialize_subsystem();
662 if (ACPI_FAILURE(status
)) {
663 printk(KERN_ERR PREFIX
664 "Unable to initialize the ACPI Interpreter\n");
668 status
= acpi_load_tables();
669 if (ACPI_FAILURE(status
)) {
670 printk(KERN_ERR PREFIX
671 "Unable to load the System Description Tables\n");
677 /* compatible (0) means level (3) */
678 if (!(acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
)) {
679 acpi_sci_flags
&= ~ACPI_MADT_TRIGGER_MASK
;
680 acpi_sci_flags
|= ACPI_MADT_TRIGGER_LEVEL
;
682 /* Set PIC-mode SCI trigger type */
683 acpi_pic_sci_set_trigger(acpi_gbl_FADT
.sci_interrupt
,
684 (acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
) >> 2);
687 * now that acpi_gbl_FADT is initialized,
688 * update it with result from INT_SRC_OVR parsing
690 acpi_gbl_FADT
.sci_interrupt
= acpi_sci_override_gsi
;
695 acpi_enable_subsystem(~
696 (ACPI_NO_HARDWARE_INIT
|
697 ACPI_NO_ACPI_ENABLE
));
698 if (ACPI_FAILURE(status
)) {
699 printk(KERN_ERR PREFIX
"Unable to enable ACPI\n");
710 static int __init
acpi_bus_init(void)
713 acpi_status status
= AE_OK
;
714 extern acpi_status
acpi_os_initialize1(void);
716 acpi_os_initialize1();
719 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT
| ACPI_NO_ACPI_ENABLE
);
720 if (ACPI_FAILURE(status
)) {
721 printk(KERN_ERR PREFIX
722 "Unable to start the ACPI Interpreter\n");
727 * ACPI 2.0 requires the EC driver to be loaded and work before
728 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
731 * This is accomplished by looking for the ECDT table, and getting
732 * the EC parameters out of that.
734 status
= acpi_ec_ecdt_probe();
735 /* Ignore result. Not having an ECDT is not fatal. */
737 status
= acpi_initialize_objects(ACPI_FULL_INITIALIZATION
);
738 if (ACPI_FAILURE(status
)) {
739 printk(KERN_ERR PREFIX
"Unable to initialize ACPI objects\n");
744 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
745 * is necessary to enable it as early as possible.
747 acpi_boot_ec_enable();
749 printk(KERN_INFO PREFIX
"Interpreter enabled\n");
751 /* Initialize sleep structures */
755 * Get the system interrupt model and evaluate \_PIC.
757 result
= acpi_bus_init_irq();
762 * Register the for all standard device notifications.
765 acpi_install_notify_handler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
,
766 &acpi_bus_notify
, NULL
);
767 if (ACPI_FAILURE(status
)) {
768 printk(KERN_ERR PREFIX
769 "Unable to register for device notifications\n");
774 * Create the top ACPI proc directory
776 acpi_root_dir
= proc_mkdir(ACPI_BUS_FILE_ROOT
, NULL
);
780 /* Mimic structured exception handling */
786 struct kobject
*acpi_kobj
;
788 static int __init
acpi_init(void)
794 printk(KERN_INFO PREFIX
"Interpreter disabled.\n");
798 acpi_kobj
= kobject_create_and_add("acpi", firmware_kobj
);
800 printk(KERN_WARNING
"%s: kset create error\n", __func__
);
804 init_acpi_device_notify();
805 result
= acpi_bus_init();
808 pci_mmcfg_late_init();
809 if (!(pm_flags
& PM_APM
))
812 printk(KERN_INFO PREFIX
813 "APM is already active, exiting\n");
824 * If the laptop falls into the DMI check table, the power state check
825 * will be disabled in the course of device power transistion.
827 dmi_check_system(power_nocheck_dmi_table
);
834 acpi_sleep_proc_init();
835 acpi_wakeup_device_init();
839 subsys_initcall(acpi_init
);