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>
42 #define _COMPONENT ACPI_BUS_COMPONENT
43 ACPI_MODULE_NAME("bus");
45 struct acpi_device
*acpi_root
;
46 struct proc_dir_entry
*acpi_root_dir
;
47 EXPORT_SYMBOL(acpi_root_dir
);
49 #define STRUCT_TO_INT(s) (*((int*)&s))
51 static int set_power_nocheck(const struct dmi_system_id
*id
)
53 printk(KERN_NOTICE PREFIX
"%s detected - "
54 "disable power check in power transistion\n", id
->ident
);
55 acpi_power_nocheck
= 1;
58 static struct dmi_system_id __cpuinitdata power_nocheck_dmi_table
[] = {
60 set_power_nocheck
, "HP Pavilion 05", {
61 DMI_MATCH(DMI_BIOS_VENDOR
, "Phoenix Technologies LTD"),
62 DMI_MATCH(DMI_SYS_VENDOR
, "HP Pavilion 05"),
63 DMI_MATCH(DMI_PRODUCT_VERSION
, "2001211RE101GLEND") }, NULL
},
68 /* --------------------------------------------------------------------------
70 -------------------------------------------------------------------------- */
72 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
74 acpi_status status
= AE_OK
;
80 /* TBD: Support fixed-feature devices */
82 status
= acpi_get_data(handle
, acpi_bus_data_handler
, (void **)device
);
83 if (ACPI_FAILURE(status
) || !*device
) {
84 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
92 EXPORT_SYMBOL(acpi_bus_get_device
);
94 int acpi_bus_get_status(struct acpi_device
*device
)
96 acpi_status status
= AE_OK
;
97 unsigned long long sta
= 0;
104 * Evaluate _STA if present.
106 if (device
->flags
.dynamic_status
) {
108 acpi_evaluate_integer(device
->handle
, "_STA", NULL
, &sta
);
109 if (ACPI_FAILURE(status
))
111 STRUCT_TO_INT(device
->status
) = (int)sta
;
115 * According to ACPI spec some device can be present and functional
116 * even if the parent is not present but functional.
117 * In such conditions the child device should not inherit the status
121 STRUCT_TO_INT(device
->status
) =
122 ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
123 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
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
)));
139 EXPORT_SYMBOL(acpi_bus_get_status
);
141 void acpi_bus_private_data_handler(acpi_handle handle
,
142 u32 function
, void *context
)
146 EXPORT_SYMBOL(acpi_bus_private_data_handler
);
148 int acpi_bus_get_private_data(acpi_handle handle
, void **data
)
150 acpi_status status
= AE_OK
;
155 status
= acpi_get_data(handle
, acpi_bus_private_data_handler
, data
);
156 if (ACPI_FAILURE(status
) || !*data
) {
157 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
164 EXPORT_SYMBOL(acpi_bus_get_private_data
);
166 /* --------------------------------------------------------------------------
168 -------------------------------------------------------------------------- */
170 int acpi_bus_get_power(acpi_handle handle
, int *state
)
173 acpi_status status
= 0;
174 struct acpi_device
*device
= NULL
;
175 unsigned long long psc
= 0;
178 result
= acpi_bus_get_device(handle
, &device
);
182 *state
= ACPI_STATE_UNKNOWN
;
184 if (!device
->flags
.power_manageable
) {
185 /* TBD: Non-recursive algorithm for walking up hierarchy */
187 *state
= device
->parent
->power
.state
;
189 *state
= ACPI_STATE_D0
;
192 * Get the device's power state either directly (via _PSC) or
193 * indirectly (via power resources).
195 if (device
->power
.flags
.explicit_get
) {
196 status
= acpi_evaluate_integer(device
->handle
, "_PSC",
198 if (ACPI_FAILURE(status
))
200 device
->power
.state
= (int)psc
;
201 } else if (device
->power
.flags
.power_resources
) {
202 result
= acpi_power_get_inferred_state(device
);
207 *state
= device
->power
.state
;
210 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] power state is D%d\n",
211 device
->pnp
.bus_id
, device
->power
.state
));
216 EXPORT_SYMBOL(acpi_bus_get_power
);
218 int acpi_bus_set_power(acpi_handle handle
, int state
)
221 acpi_status status
= AE_OK
;
222 struct acpi_device
*device
= NULL
;
223 char object_name
[5] = { '_', 'P', 'S', '0' + state
, '\0' };
226 result
= acpi_bus_get_device(handle
, &device
);
230 if ((state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
233 /* Make sure this is a valid target state */
235 if (!device
->flags
.power_manageable
) {
236 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device `[%s]' is not power manageable\n",
237 kobject_name(&device
->dev
.kobj
)));
241 * Get device's current power state
243 if (!acpi_power_nocheck
) {
245 * Maybe the incorrect power state is returned on the bogus
246 * bios, which is different with the real power state.
247 * For example: the bios returns D0 state and the real power
248 * state is D3. OS expects to set the device to D0 state. In
249 * such case if OS uses the power state returned by the BIOS,
250 * the device can't be transisted to the correct power state.
251 * So if the acpi_power_nocheck is set, it is unnecessary to
252 * get the power state by calling acpi_bus_get_power.
254 acpi_bus_get_power(device
->handle
, &device
->power
.state
);
256 if ((state
== device
->power
.state
) && !device
->flags
.force_power_state
) {
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device is already at D%d\n",
262 if (!device
->power
.states
[state
].flags
.valid
) {
263 printk(KERN_WARNING PREFIX
"Device does not support D%d\n", state
);
266 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
267 printk(KERN_WARNING PREFIX
268 "Cannot set device to a higher-powered"
269 " state than parent\n");
276 * On transitions to a high-powered state we first apply power (via
277 * power resources) then evalute _PSx. Conversly for transitions to
278 * a lower-powered state.
280 if (state
< device
->power
.state
) {
281 if (device
->power
.flags
.power_resources
) {
282 result
= acpi_power_transition(device
, state
);
286 if (device
->power
.states
[state
].flags
.explicit_set
) {
287 status
= acpi_evaluate_object(device
->handle
,
288 object_name
, NULL
, NULL
);
289 if (ACPI_FAILURE(status
)) {
295 if (device
->power
.states
[state
].flags
.explicit_set
) {
296 status
= acpi_evaluate_object(device
->handle
,
297 object_name
, NULL
, NULL
);
298 if (ACPI_FAILURE(status
)) {
303 if (device
->power
.flags
.power_resources
) {
304 result
= acpi_power_transition(device
, state
);
312 printk(KERN_WARNING PREFIX
313 "Transitioning device [%s] to D%d\n",
314 device
->pnp
.bus_id
, state
);
316 device
->power
.state
= state
;
317 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
318 "Device [%s] transitioned to D%d\n",
319 device
->pnp
.bus_id
, state
));
325 EXPORT_SYMBOL(acpi_bus_set_power
);
327 bool acpi_bus_power_manageable(acpi_handle handle
)
329 struct acpi_device
*device
;
332 result
= acpi_bus_get_device(handle
, &device
);
333 return result
? false : device
->flags
.power_manageable
;
336 EXPORT_SYMBOL(acpi_bus_power_manageable
);
338 bool acpi_bus_can_wakeup(acpi_handle handle
)
340 struct acpi_device
*device
;
343 result
= acpi_bus_get_device(handle
, &device
);
344 return result
? false : device
->wakeup
.flags
.valid
;
347 EXPORT_SYMBOL(acpi_bus_can_wakeup
);
349 /* --------------------------------------------------------------------------
351 -------------------------------------------------------------------------- */
353 #ifdef CONFIG_ACPI_PROC_EVENT
354 static DEFINE_SPINLOCK(acpi_bus_event_lock
);
356 LIST_HEAD(acpi_bus_event_list
);
357 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue
);
359 extern int event_is_open
;
361 int acpi_bus_generate_proc_event4(const char *device_class
, const char *bus_id
, u8 type
, int data
)
363 struct acpi_bus_event
*event
;
364 unsigned long flags
= 0;
366 /* drop event on the floor if no one's listening */
370 event
= kmalloc(sizeof(struct acpi_bus_event
), GFP_ATOMIC
);
374 strcpy(event
->device_class
, device_class
);
375 strcpy(event
->bus_id
, bus_id
);
379 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
380 list_add_tail(&event
->node
, &acpi_bus_event_list
);
381 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
383 wake_up_interruptible(&acpi_bus_event_queue
);
389 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4
);
391 int acpi_bus_generate_proc_event(struct acpi_device
*device
, u8 type
, int data
)
395 return acpi_bus_generate_proc_event4(device
->pnp
.device_class
,
396 device
->pnp
.bus_id
, type
, data
);
399 EXPORT_SYMBOL(acpi_bus_generate_proc_event
);
401 int acpi_bus_receive_event(struct acpi_bus_event
*event
)
403 unsigned long flags
= 0;
404 struct acpi_bus_event
*entry
= NULL
;
406 DECLARE_WAITQUEUE(wait
, current
);
412 if (list_empty(&acpi_bus_event_list
)) {
414 set_current_state(TASK_INTERRUPTIBLE
);
415 add_wait_queue(&acpi_bus_event_queue
, &wait
);
417 if (list_empty(&acpi_bus_event_list
))
420 remove_wait_queue(&acpi_bus_event_queue
, &wait
);
421 set_current_state(TASK_RUNNING
);
423 if (signal_pending(current
))
427 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
428 if (!list_empty(&acpi_bus_event_list
)) {
429 entry
= list_entry(acpi_bus_event_list
.next
,
430 struct acpi_bus_event
, node
);
431 list_del(&entry
->node
);
433 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
438 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
445 #endif /* CONFIG_ACPI_PROC_EVENT */
447 /* --------------------------------------------------------------------------
448 Notification Handling
449 -------------------------------------------------------------------------- */
452 acpi_bus_check_device(struct acpi_device
*device
, int *status_changed
)
454 acpi_status status
= 0;
455 struct acpi_device_status old_status
;
464 old_status
= device
->status
;
467 * Make sure this device's parent is present before we go about
468 * messing with the device.
470 if (device
->parent
&& !device
->parent
->status
.present
) {
471 device
->status
= device
->parent
->status
;
472 if (STRUCT_TO_INT(old_status
) != STRUCT_TO_INT(device
->status
)) {
479 status
= acpi_bus_get_status(device
);
480 if (ACPI_FAILURE(status
))
483 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
490 * Device Insertion/Removal
492 if ((device
->status
.present
) && !(old_status
.present
)) {
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device insertion detected\n"));
494 /* TBD: Handle device insertion */
495 } else if (!(device
->status
.present
) && (old_status
.present
)) {
496 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device removal detected\n"));
497 /* TBD: Handle device removal */
503 static int acpi_bus_check_scope(struct acpi_device
*device
)
506 int status_changed
= 0;
513 result
= acpi_bus_check_device(device
, &status_changed
);
521 * TBD: Enumerate child devices within this device's scope and
522 * run acpi_bus_check_device()'s on them.
528 static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list
);
529 int register_acpi_bus_notifier(struct notifier_block
*nb
)
531 return blocking_notifier_chain_register(&acpi_bus_notify_list
, nb
);
533 EXPORT_SYMBOL_GPL(register_acpi_bus_notifier
);
535 void unregister_acpi_bus_notifier(struct notifier_block
*nb
)
537 blocking_notifier_chain_unregister(&acpi_bus_notify_list
, nb
);
539 EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier
);
544 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
546 static void acpi_bus_notify(acpi_handle handle
, u32 type
, void *data
)
549 struct acpi_device
*device
= NULL
;
551 blocking_notifier_call_chain(&acpi_bus_notify_list
,
552 type
, (void *)handle
);
554 if (acpi_bus_get_device(handle
, &device
))
559 case ACPI_NOTIFY_BUS_CHECK
:
560 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
561 "Received BUS CHECK notification for device [%s]\n",
562 device
->pnp
.bus_id
));
563 result
= acpi_bus_check_scope(device
);
565 * TBD: We'll need to outsource certain events to non-ACPI
566 * drivers via the device manager (device.c).
570 case ACPI_NOTIFY_DEVICE_CHECK
:
571 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
572 "Received DEVICE CHECK notification for device [%s]\n",
573 device
->pnp
.bus_id
));
574 result
= acpi_bus_check_device(device
, NULL
);
576 * TBD: We'll need to outsource certain events to non-ACPI
577 * drivers via the device manager (device.c).
581 case ACPI_NOTIFY_DEVICE_WAKE
:
582 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
583 "Received DEVICE WAKE notification for device [%s]\n",
584 device
->pnp
.bus_id
));
588 case ACPI_NOTIFY_EJECT_REQUEST
:
589 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
590 "Received EJECT REQUEST notification for device [%s]\n",
591 device
->pnp
.bus_id
));
595 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
596 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
597 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
598 device
->pnp
.bus_id
));
599 /* TBD: Exactly what does 'light' mean? */
602 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
603 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
604 "Received FREQUENCY MISMATCH notification for device [%s]\n",
605 device
->pnp
.bus_id
));
609 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
610 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
611 "Received BUS MODE MISMATCH notification for device [%s]\n",
612 device
->pnp
.bus_id
));
616 case ACPI_NOTIFY_POWER_FAULT
:
617 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
618 "Received POWER FAULT notification for device [%s]\n",
619 device
->pnp
.bus_id
));
624 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
625 "Received unknown/unsupported notification [%08x]\n",
633 /* --------------------------------------------------------------------------
634 Initialization/Cleanup
635 -------------------------------------------------------------------------- */
637 static int __init
acpi_bus_init_irq(void)
639 acpi_status status
= AE_OK
;
640 union acpi_object arg
= { ACPI_TYPE_INTEGER
};
641 struct acpi_object_list arg_list
= { 1, &arg
};
642 char *message
= NULL
;
646 * Let the system know what interrupt model we are using by
647 * evaluating the \_PIC object, if exists.
650 switch (acpi_irq_model
) {
651 case ACPI_IRQ_MODEL_PIC
:
654 case ACPI_IRQ_MODEL_IOAPIC
:
657 case ACPI_IRQ_MODEL_IOSAPIC
:
660 case ACPI_IRQ_MODEL_PLATFORM
:
661 message
= "platform specific model";
664 printk(KERN_WARNING PREFIX
"Unknown interrupt routing model\n");
668 printk(KERN_INFO PREFIX
"Using %s for interrupt routing\n", message
);
670 arg
.integer
.value
= acpi_irq_model
;
672 status
= acpi_evaluate_object(NULL
, "\\_PIC", &arg_list
, NULL
);
673 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
674 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PIC"));
681 u8 acpi_gbl_permanent_mmap
;
684 void __init
acpi_early_init(void)
686 acpi_status status
= AE_OK
;
692 * ACPI CA initializes acpi_dbg_level to non-zero, which means
693 * we get debug output merely by turning on CONFIG_ACPI_DEBUG.
694 * Turn it off so we don't get output unless the user specifies
699 printk(KERN_INFO PREFIX
"Core revision %08x\n", ACPI_CA_VERSION
);
701 /* enable workarounds, unless strict ACPI spec. compliance */
703 acpi_gbl_enable_interpreter_slack
= TRUE
;
705 acpi_gbl_permanent_mmap
= 1;
707 status
= acpi_reallocate_root_table();
708 if (ACPI_FAILURE(status
)) {
709 printk(KERN_ERR PREFIX
710 "Unable to reallocate ACPI tables\n");
714 status
= acpi_initialize_subsystem();
715 if (ACPI_FAILURE(status
)) {
716 printk(KERN_ERR PREFIX
717 "Unable to initialize the ACPI Interpreter\n");
721 status
= acpi_load_tables();
722 if (ACPI_FAILURE(status
)) {
723 printk(KERN_ERR PREFIX
724 "Unable to load the System Description Tables\n");
730 /* compatible (0) means level (3) */
731 if (!(acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
)) {
732 acpi_sci_flags
&= ~ACPI_MADT_TRIGGER_MASK
;
733 acpi_sci_flags
|= ACPI_MADT_TRIGGER_LEVEL
;
735 /* Set PIC-mode SCI trigger type */
736 acpi_pic_sci_set_trigger(acpi_gbl_FADT
.sci_interrupt
,
737 (acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
) >> 2);
740 * now that acpi_gbl_FADT is initialized,
741 * update it with result from INT_SRC_OVR parsing
743 acpi_gbl_FADT
.sci_interrupt
= acpi_sci_override_gsi
;
748 acpi_enable_subsystem(~
749 (ACPI_NO_HARDWARE_INIT
|
750 ACPI_NO_ACPI_ENABLE
));
751 if (ACPI_FAILURE(status
)) {
752 printk(KERN_ERR PREFIX
"Unable to enable ACPI\n");
763 static int __init
acpi_bus_init(void)
766 acpi_status status
= AE_OK
;
767 extern acpi_status
acpi_os_initialize1(void);
770 status
= acpi_os_initialize1();
773 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT
| ACPI_NO_ACPI_ENABLE
);
774 if (ACPI_FAILURE(status
)) {
775 printk(KERN_ERR PREFIX
776 "Unable to start the ACPI Interpreter\n");
780 if (ACPI_FAILURE(status
)) {
781 printk(KERN_ERR PREFIX
782 "Unable to initialize ACPI OS objects\n");
787 * ACPI 2.0 requires the EC driver to be loaded and work before
788 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
791 * This is accomplished by looking for the ECDT table, and getting
792 * the EC parameters out of that.
794 status
= acpi_ec_ecdt_probe();
795 /* Ignore result. Not having an ECDT is not fatal. */
797 status
= acpi_initialize_objects(ACPI_FULL_INITIALIZATION
);
798 if (ACPI_FAILURE(status
)) {
799 printk(KERN_ERR PREFIX
"Unable to initialize ACPI objects\n");
804 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
805 * is necessary to enable it as early as possible.
807 acpi_boot_ec_enable();
809 printk(KERN_INFO PREFIX
"Interpreter enabled\n");
811 /* Initialize sleep structures */
815 * Get the system interrupt model and evaluate \_PIC.
817 result
= acpi_bus_init_irq();
822 * Register the for all standard device notifications.
825 acpi_install_notify_handler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
,
826 &acpi_bus_notify
, NULL
);
827 if (ACPI_FAILURE(status
)) {
828 printk(KERN_ERR PREFIX
829 "Unable to register for device notifications\n");
834 * Create the top ACPI proc directory
836 acpi_root_dir
= proc_mkdir(ACPI_BUS_FILE_ROOT
, NULL
);
840 /* Mimic structured exception handling */
846 struct kobject
*acpi_kobj
;
848 static int __init
acpi_init(void)
854 printk(KERN_INFO PREFIX
"Interpreter disabled.\n");
858 acpi_kobj
= kobject_create_and_add("acpi", firmware_kobj
);
860 printk(KERN_WARNING
"%s: kset create error\n", __func__
);
864 result
= acpi_bus_init();
867 pci_mmcfg_late_init();
868 if (!(pm_flags
& PM_APM
))
871 printk(KERN_INFO PREFIX
872 "APM is already active, exiting\n");
879 * If the laptop falls into the DMI check table, the power state check
880 * will be disabled in the course of device power transistion.
882 dmi_check_system(power_nocheck_dmi_table
);
886 subsys_initcall(acpi_init
);