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/pm_legacy.h>
33 #include <linux/device.h>
34 #include <linux/proc_fs.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 extern void __init
acpi_pic_sci_set_trigger(unsigned int irq
, u16 trigger
);
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 /* --------------------------------------------------------------------------
56 -------------------------------------------------------------------------- */
58 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
60 acpi_status status
= AE_OK
;
66 /* TBD: Support fixed-feature devices */
68 status
= acpi_get_data(handle
, acpi_bus_data_handler
, (void **)device
);
69 if (ACPI_FAILURE(status
) || !*device
) {
70 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
78 EXPORT_SYMBOL(acpi_bus_get_device
);
80 int acpi_bus_get_status(struct acpi_device
*device
)
82 acpi_status status
= AE_OK
;
83 unsigned long sta
= 0;
90 * Evaluate _STA if present.
92 if (device
->flags
.dynamic_status
) {
94 acpi_evaluate_integer(device
->handle
, "_STA", NULL
, &sta
);
95 if (ACPI_FAILURE(status
))
97 STRUCT_TO_INT(device
->status
) = (int)sta
;
101 * Otherwise we assume the status of our parent (unless we don't
102 * have one, in which case status is implied).
104 else if (device
->parent
)
105 device
->status
= device
->parent
->status
;
107 STRUCT_TO_INT(device
->status
) =
108 ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
109 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
111 if (device
->status
.functional
&& !device
->status
.present
) {
112 printk(KERN_WARNING PREFIX
"Device [%s] status [%08x]: "
113 "functional but not present; setting present\n",
114 device
->pnp
.bus_id
, (u32
) STRUCT_TO_INT(device
->status
));
115 device
->status
.present
= 1;
118 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]\n",
120 (u32
) STRUCT_TO_INT(device
->status
)));
125 EXPORT_SYMBOL(acpi_bus_get_status
);
127 /* --------------------------------------------------------------------------
129 -------------------------------------------------------------------------- */
131 int acpi_bus_get_power(acpi_handle handle
, int *state
)
134 acpi_status status
= 0;
135 struct acpi_device
*device
= NULL
;
136 unsigned long psc
= 0;
139 result
= acpi_bus_get_device(handle
, &device
);
143 *state
= ACPI_STATE_UNKNOWN
;
145 if (!device
->flags
.power_manageable
) {
146 /* TBD: Non-recursive algorithm for walking up hierarchy */
148 *state
= device
->parent
->power
.state
;
150 *state
= ACPI_STATE_D0
;
153 * Get the device's power state either directly (via _PSC) or
154 * indirectly (via power resources).
156 if (device
->power
.flags
.explicit_get
) {
157 status
= acpi_evaluate_integer(device
->handle
, "_PSC",
159 if (ACPI_FAILURE(status
))
161 device
->power
.state
= (int)psc
;
162 } else if (device
->power
.flags
.power_resources
) {
163 result
= acpi_power_get_inferred_state(device
);
168 *state
= device
->power
.state
;
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] power state is D%d\n",
172 device
->pnp
.bus_id
, device
->power
.state
));
177 EXPORT_SYMBOL(acpi_bus_get_power
);
179 int acpi_bus_set_power(acpi_handle handle
, int state
)
182 acpi_status status
= AE_OK
;
183 struct acpi_device
*device
= NULL
;
184 char object_name
[5] = { '_', 'P', 'S', '0' + state
, '\0' };
187 result
= acpi_bus_get_device(handle
, &device
);
191 if ((state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
194 /* Make sure this is a valid target state */
196 if (!device
->flags
.power_manageable
) {
197 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device `[%s]' is not power manageable\n",
198 kobject_name(&device
->dev
.kobj
)));
202 * Get device's current power state
204 acpi_bus_get_power(device
->handle
, &device
->power
.state
);
205 if (state
== device
->power
.state
) {
206 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device is already at D%d\n",
211 if (!device
->power
.states
[state
].flags
.valid
) {
212 printk(KERN_WARNING PREFIX
"Device does not support D%d\n", state
);
215 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
216 printk(KERN_WARNING PREFIX
217 "Cannot set device to a higher-powered"
218 " state than parent\n");
225 * On transitions to a high-powered state we first apply power (via
226 * power resources) then evalute _PSx. Conversly for transitions to
227 * a lower-powered state.
229 if (state
< device
->power
.state
) {
230 if (device
->power
.flags
.power_resources
) {
231 result
= acpi_power_transition(device
, state
);
235 if (device
->power
.states
[state
].flags
.explicit_set
) {
236 status
= acpi_evaluate_object(device
->handle
,
237 object_name
, NULL
, NULL
);
238 if (ACPI_FAILURE(status
)) {
244 if (device
->power
.states
[state
].flags
.explicit_set
) {
245 status
= acpi_evaluate_object(device
->handle
,
246 object_name
, NULL
, NULL
);
247 if (ACPI_FAILURE(status
)) {
252 if (device
->power
.flags
.power_resources
) {
253 result
= acpi_power_transition(device
, state
);
261 printk(KERN_WARNING PREFIX
262 "Transitioning device [%s] to D%d\n",
263 device
->pnp
.bus_id
, state
);
265 device
->power
.state
= state
;
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
267 "Device [%s] transitioned to D%d\n",
268 device
->pnp
.bus_id
, state
));
274 EXPORT_SYMBOL(acpi_bus_set_power
);
276 /* --------------------------------------------------------------------------
278 -------------------------------------------------------------------------- */
280 #ifdef CONFIG_ACPI_PROC_EVENT
281 static DEFINE_SPINLOCK(acpi_bus_event_lock
);
283 LIST_HEAD(acpi_bus_event_list
);
284 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue
);
286 extern int event_is_open
;
288 int acpi_bus_generate_proc_event4(const char *device_class
, const char *bus_id
, u8 type
, int data
)
290 struct acpi_bus_event
*event
;
291 unsigned long flags
= 0;
293 /* drop event on the floor if no one's listening */
297 event
= kmalloc(sizeof(struct acpi_bus_event
), GFP_ATOMIC
);
301 strcpy(event
->device_class
, device_class
);
302 strcpy(event
->bus_id
, bus_id
);
306 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
307 list_add_tail(&event
->node
, &acpi_bus_event_list
);
308 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
310 wake_up_interruptible(&acpi_bus_event_queue
);
316 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4
);
318 int acpi_bus_generate_proc_event(struct acpi_device
*device
, u8 type
, int data
)
322 return acpi_bus_generate_proc_event4(device
->pnp
.device_class
,
323 device
->pnp
.bus_id
, type
, data
);
326 EXPORT_SYMBOL(acpi_bus_generate_proc_event
);
328 int acpi_bus_receive_event(struct acpi_bus_event
*event
)
330 unsigned long flags
= 0;
331 struct acpi_bus_event
*entry
= NULL
;
333 DECLARE_WAITQUEUE(wait
, current
);
339 if (list_empty(&acpi_bus_event_list
)) {
341 set_current_state(TASK_INTERRUPTIBLE
);
342 add_wait_queue(&acpi_bus_event_queue
, &wait
);
344 if (list_empty(&acpi_bus_event_list
))
347 remove_wait_queue(&acpi_bus_event_queue
, &wait
);
348 set_current_state(TASK_RUNNING
);
350 if (signal_pending(current
))
354 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
356 list_entry(acpi_bus_event_list
.next
, struct acpi_bus_event
, node
);
358 list_del(&entry
->node
);
359 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
364 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
371 EXPORT_SYMBOL(acpi_bus_receive_event
);
372 #endif /* CONFIG_ACPI_PROC_EVENT */
374 /* --------------------------------------------------------------------------
375 Notification Handling
376 -------------------------------------------------------------------------- */
379 acpi_bus_check_device(struct acpi_device
*device
, int *status_changed
)
381 acpi_status status
= 0;
382 struct acpi_device_status old_status
;
391 old_status
= device
->status
;
394 * Make sure this device's parent is present before we go about
395 * messing with the device.
397 if (device
->parent
&& !device
->parent
->status
.present
) {
398 device
->status
= device
->parent
->status
;
399 if (STRUCT_TO_INT(old_status
) != STRUCT_TO_INT(device
->status
)) {
406 status
= acpi_bus_get_status(device
);
407 if (ACPI_FAILURE(status
))
410 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
417 * Device Insertion/Removal
419 if ((device
->status
.present
) && !(old_status
.present
)) {
420 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device insertion detected\n"));
421 /* TBD: Handle device insertion */
422 } else if (!(device
->status
.present
) && (old_status
.present
)) {
423 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device removal detected\n"));
424 /* TBD: Handle device removal */
430 static int acpi_bus_check_scope(struct acpi_device
*device
)
433 int status_changed
= 0;
440 result
= acpi_bus_check_device(device
, &status_changed
);
448 * TBD: Enumerate child devices within this device's scope and
449 * run acpi_bus_check_device()'s on them.
458 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
460 static void acpi_bus_notify(acpi_handle handle
, u32 type
, void *data
)
463 struct acpi_device
*device
= NULL
;
466 if (acpi_bus_get_device(handle
, &device
))
471 case ACPI_NOTIFY_BUS_CHECK
:
472 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
473 "Received BUS CHECK notification for device [%s]\n",
474 device
->pnp
.bus_id
));
475 result
= acpi_bus_check_scope(device
);
477 * TBD: We'll need to outsource certain events to non-ACPI
478 * drivers via the device manager (device.c).
482 case ACPI_NOTIFY_DEVICE_CHECK
:
483 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
484 "Received DEVICE CHECK notification for device [%s]\n",
485 device
->pnp
.bus_id
));
486 result
= acpi_bus_check_device(device
, NULL
);
488 * TBD: We'll need to outsource certain events to non-ACPI
489 * drivers via the device manager (device.c).
493 case ACPI_NOTIFY_DEVICE_WAKE
:
494 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
495 "Received DEVICE WAKE notification for device [%s]\n",
496 device
->pnp
.bus_id
));
500 case ACPI_NOTIFY_EJECT_REQUEST
:
501 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
502 "Received EJECT REQUEST notification for device [%s]\n",
503 device
->pnp
.bus_id
));
507 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
508 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
509 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
510 device
->pnp
.bus_id
));
511 /* TBD: Exactly what does 'light' mean? */
514 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
515 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
516 "Received FREQUENCY MISMATCH notification for device [%s]\n",
517 device
->pnp
.bus_id
));
521 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
522 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
523 "Received BUS MODE MISMATCH notification for device [%s]\n",
524 device
->pnp
.bus_id
));
528 case ACPI_NOTIFY_POWER_FAULT
:
529 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
530 "Received POWER FAULT notification for device [%s]\n",
531 device
->pnp
.bus_id
));
536 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
537 "Received unknown/unsupported notification [%08x]\n",
545 /* --------------------------------------------------------------------------
546 Initialization/Cleanup
547 -------------------------------------------------------------------------- */
549 static int __init
acpi_bus_init_irq(void)
551 acpi_status status
= AE_OK
;
552 union acpi_object arg
= { ACPI_TYPE_INTEGER
};
553 struct acpi_object_list arg_list
= { 1, &arg
};
554 char *message
= NULL
;
558 * Let the system know what interrupt model we are using by
559 * evaluating the \_PIC object, if exists.
562 switch (acpi_irq_model
) {
563 case ACPI_IRQ_MODEL_PIC
:
566 case ACPI_IRQ_MODEL_IOAPIC
:
569 case ACPI_IRQ_MODEL_IOSAPIC
:
572 case ACPI_IRQ_MODEL_PLATFORM
:
573 message
= "platform specific model";
576 printk(KERN_WARNING PREFIX
"Unknown interrupt routing model\n");
580 printk(KERN_INFO PREFIX
"Using %s for interrupt routing\n", message
);
582 arg
.integer
.value
= acpi_irq_model
;
584 status
= acpi_evaluate_object(NULL
, "\\_PIC", &arg_list
, NULL
);
585 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
586 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PIC"));
593 acpi_native_uint acpi_gbl_permanent_mmap
;
596 void __init
acpi_early_init(void)
598 acpi_status status
= AE_OK
;
603 printk(KERN_INFO PREFIX
"Core revision %08x\n", ACPI_CA_VERSION
);
605 /* enable workarounds, unless strict ACPI spec. compliance */
607 acpi_gbl_enable_interpreter_slack
= TRUE
;
609 acpi_gbl_permanent_mmap
= 1;
611 status
= acpi_reallocate_root_table();
612 if (ACPI_FAILURE(status
)) {
613 printk(KERN_ERR PREFIX
614 "Unable to reallocate ACPI tables\n");
618 status
= acpi_initialize_subsystem();
619 if (ACPI_FAILURE(status
)) {
620 printk(KERN_ERR PREFIX
621 "Unable to initialize the ACPI Interpreter\n");
625 status
= acpi_load_tables();
626 if (ACPI_FAILURE(status
)) {
627 printk(KERN_ERR PREFIX
628 "Unable to load the System Description Tables\n");
634 extern u8 acpi_sci_flags
;
636 /* compatible (0) means level (3) */
637 if (!(acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
)) {
638 acpi_sci_flags
&= ~ACPI_MADT_TRIGGER_MASK
;
639 acpi_sci_flags
|= ACPI_MADT_TRIGGER_LEVEL
;
641 /* Set PIC-mode SCI trigger type */
642 acpi_pic_sci_set_trigger(acpi_gbl_FADT
.sci_interrupt
,
643 (acpi_sci_flags
& ACPI_MADT_TRIGGER_MASK
) >> 2);
645 extern int acpi_sci_override_gsi
;
647 * now that acpi_gbl_FADT is initialized,
648 * update it with result from INT_SRC_OVR parsing
650 acpi_gbl_FADT
.sci_interrupt
= acpi_sci_override_gsi
;
655 acpi_enable_subsystem(~
656 (ACPI_NO_HARDWARE_INIT
|
657 ACPI_NO_ACPI_ENABLE
));
658 if (ACPI_FAILURE(status
)) {
659 printk(KERN_ERR PREFIX
"Unable to enable ACPI\n");
670 static int __init
acpi_bus_init(void)
673 acpi_status status
= AE_OK
;
674 extern acpi_status
acpi_os_initialize1(void);
677 status
= acpi_os_initialize1();
680 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT
| ACPI_NO_ACPI_ENABLE
);
681 if (ACPI_FAILURE(status
)) {
682 printk(KERN_ERR PREFIX
683 "Unable to start the ACPI Interpreter\n");
687 if (ACPI_FAILURE(status
)) {
688 printk(KERN_ERR PREFIX
689 "Unable to initialize ACPI OS objects\n");
692 #ifdef CONFIG_ACPI_EC
694 * ACPI 2.0 requires the EC driver to be loaded and work before
695 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
698 * This is accomplished by looking for the ECDT table, and getting
699 * the EC parameters out of that.
701 status
= acpi_ec_ecdt_probe();
702 /* Ignore result. Not having an ECDT is not fatal. */
705 status
= acpi_initialize_objects(ACPI_FULL_INITIALIZATION
);
706 if (ACPI_FAILURE(status
)) {
707 printk(KERN_ERR PREFIX
"Unable to initialize ACPI objects\n");
711 printk(KERN_INFO PREFIX
"Interpreter enabled\n");
713 /* Initialize sleep structures */
717 * Get the system interrupt model and evaluate \_PIC.
719 result
= acpi_bus_init_irq();
724 * Register the for all standard device notifications.
727 acpi_install_notify_handler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
,
728 &acpi_bus_notify
, NULL
);
729 if (ACPI_FAILURE(status
)) {
730 printk(KERN_ERR PREFIX
731 "Unable to register for device notifications\n");
736 * Create the top ACPI proc directory
738 acpi_root_dir
= proc_mkdir(ACPI_BUS_FILE_ROOT
, NULL
);
742 /* Mimic structured exception handling */
748 struct kset
*acpi_kset
;
750 static int __init
acpi_init(void)
756 printk(KERN_INFO PREFIX
"Interpreter disabled.\n");
760 acpi_kset
= kset_create_and_register("acpi", NULL
, firmware_kobj
,
763 printk(KERN_WARNING
"%s: kset create error\n", __FUNCTION__
);
767 result
= acpi_bus_init();
770 pci_mmcfg_late_init();
771 #ifdef CONFIG_PM_LEGACY
775 printk(KERN_INFO PREFIX
776 "APM is already active, exiting\n");
787 subsys_initcall(acpi_init
);