1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include <acpi/acevents.h>
46 #include <acpi/acnamesp.h>
48 #define _COMPONENT ACPI_EVENTS
49 ACPI_MODULE_NAME ("evgpe")
51 /* Local prototypes */
53 static void ACPI_SYSTEM_XFACE
54 acpi_ev_asynch_execute_gpe_method (
58 /*******************************************************************************
60 * FUNCTION: acpi_ev_set_gpe_type
62 * PARAMETERS: gpe_event_info - GPE to set
67 * DESCRIPTION: Sets the new type for the GPE (wake, run, or wake/run)
69 ******************************************************************************/
72 acpi_ev_set_gpe_type (
73 struct acpi_gpe_event_info
*gpe_event_info
,
79 ACPI_FUNCTION_TRACE ("ev_set_gpe_type");
82 /* Validate type and update register enable masks */
85 case ACPI_GPE_TYPE_WAKE
:
86 case ACPI_GPE_TYPE_RUNTIME
:
87 case ACPI_GPE_TYPE_WAKE_RUN
:
91 return_ACPI_STATUS (AE_BAD_PARAMETER
);
94 /* Disable the GPE if currently enabled */
96 status
= acpi_ev_disable_gpe (gpe_event_info
);
98 /* Type was validated above */
100 gpe_event_info
->flags
&= ~ACPI_GPE_TYPE_MASK
; /* Clear type bits */
101 gpe_event_info
->flags
|= type
; /* Insert type */
102 return_ACPI_STATUS (status
);
106 /*******************************************************************************
108 * FUNCTION: acpi_ev_update_gpe_enable_masks
110 * PARAMETERS: gpe_event_info - GPE to update
111 * Type - What to do: ACPI_GPE_DISABLE or
116 * DESCRIPTION: Updates GPE register enable masks based on the GPE type
118 ******************************************************************************/
121 acpi_ev_update_gpe_enable_masks (
122 struct acpi_gpe_event_info
*gpe_event_info
,
125 struct acpi_gpe_register_info
*gpe_register_info
;
129 ACPI_FUNCTION_TRACE ("ev_update_gpe_enable_masks");
132 gpe_register_info
= gpe_event_info
->register_info
;
133 if (!gpe_register_info
) {
134 return_ACPI_STATUS (AE_NOT_EXIST
);
136 register_bit
= gpe_event_info
->register_bit
;
138 /* 1) Disable case. Simply clear all enable bits */
140 if (type
== ACPI_GPE_DISABLE
) {
141 ACPI_CLEAR_BIT (gpe_register_info
->enable_for_wake
, register_bit
);
142 ACPI_CLEAR_BIT (gpe_register_info
->enable_for_run
, register_bit
);
143 return_ACPI_STATUS (AE_OK
);
146 /* 2) Enable case. Set/Clear the appropriate enable bits */
148 switch (gpe_event_info
->flags
& ACPI_GPE_TYPE_MASK
) {
149 case ACPI_GPE_TYPE_WAKE
:
150 ACPI_SET_BIT (gpe_register_info
->enable_for_wake
, register_bit
);
151 ACPI_CLEAR_BIT (gpe_register_info
->enable_for_run
, register_bit
);
154 case ACPI_GPE_TYPE_RUNTIME
:
155 ACPI_CLEAR_BIT (gpe_register_info
->enable_for_wake
, register_bit
);
156 ACPI_SET_BIT (gpe_register_info
->enable_for_run
, register_bit
);
159 case ACPI_GPE_TYPE_WAKE_RUN
:
160 ACPI_SET_BIT (gpe_register_info
->enable_for_wake
, register_bit
);
161 ACPI_SET_BIT (gpe_register_info
->enable_for_run
, register_bit
);
165 return_ACPI_STATUS (AE_BAD_PARAMETER
);
168 return_ACPI_STATUS (AE_OK
);
172 /*******************************************************************************
174 * FUNCTION: acpi_ev_enable_gpe
176 * PARAMETERS: gpe_event_info - GPE to enable
177 * write_to_hardware - Enable now, or just mark data structs
178 * (WAKE GPEs should be deferred)
182 * DESCRIPTION: Enable a GPE based on the GPE type
184 ******************************************************************************/
188 struct acpi_gpe_event_info
*gpe_event_info
,
189 u8 write_to_hardware
)
194 ACPI_FUNCTION_TRACE ("ev_enable_gpe");
197 /* Make sure HW enable masks are updated */
199 status
= acpi_ev_update_gpe_enable_masks (gpe_event_info
, ACPI_GPE_ENABLE
);
200 if (ACPI_FAILURE (status
)) {
201 return_ACPI_STATUS (status
);
204 /* Mark wake-enabled or HW enable, or both */
206 switch (gpe_event_info
->flags
& ACPI_GPE_TYPE_MASK
) {
207 case ACPI_GPE_TYPE_WAKE
:
209 ACPI_SET_BIT (gpe_event_info
->flags
, ACPI_GPE_WAKE_ENABLED
);
212 case ACPI_GPE_TYPE_WAKE_RUN
:
214 ACPI_SET_BIT (gpe_event_info
->flags
, ACPI_GPE_WAKE_ENABLED
);
216 /*lint -fallthrough */
218 case ACPI_GPE_TYPE_RUNTIME
:
220 ACPI_SET_BIT (gpe_event_info
->flags
, ACPI_GPE_RUN_ENABLED
);
222 if (write_to_hardware
) {
223 /* Clear the GPE (of stale events), then enable it */
225 status
= acpi_hw_clear_gpe (gpe_event_info
);
226 if (ACPI_FAILURE (status
)) {
227 return_ACPI_STATUS (status
);
230 /* Enable the requested runtime GPE */
232 status
= acpi_hw_write_gpe_enable_reg (gpe_event_info
);
237 return_ACPI_STATUS (AE_BAD_PARAMETER
);
240 return_ACPI_STATUS (AE_OK
);
244 /*******************************************************************************
246 * FUNCTION: acpi_ev_disable_gpe
248 * PARAMETERS: gpe_event_info - GPE to disable
252 * DESCRIPTION: Disable a GPE based on the GPE type
254 ******************************************************************************/
257 acpi_ev_disable_gpe (
258 struct acpi_gpe_event_info
*gpe_event_info
)
263 ACPI_FUNCTION_TRACE ("ev_disable_gpe");
266 if (!(gpe_event_info
->flags
& ACPI_GPE_ENABLE_MASK
)) {
267 return_ACPI_STATUS (AE_OK
);
270 /* Make sure HW enable masks are updated */
272 status
= acpi_ev_update_gpe_enable_masks (gpe_event_info
, ACPI_GPE_DISABLE
);
273 if (ACPI_FAILURE (status
)) {
274 return_ACPI_STATUS (status
);
277 /* Mark wake-disabled or HW disable, or both */
279 switch (gpe_event_info
->flags
& ACPI_GPE_TYPE_MASK
) {
280 case ACPI_GPE_TYPE_WAKE
:
281 ACPI_CLEAR_BIT (gpe_event_info
->flags
, ACPI_GPE_WAKE_ENABLED
);
284 case ACPI_GPE_TYPE_WAKE_RUN
:
285 ACPI_CLEAR_BIT (gpe_event_info
->flags
, ACPI_GPE_WAKE_ENABLED
);
287 /*lint -fallthrough */
289 case ACPI_GPE_TYPE_RUNTIME
:
291 /* Disable the requested runtime GPE */
293 ACPI_CLEAR_BIT (gpe_event_info
->flags
, ACPI_GPE_RUN_ENABLED
);
294 status
= acpi_hw_write_gpe_enable_reg (gpe_event_info
);
298 return_ACPI_STATUS (AE_BAD_PARAMETER
);
301 return_ACPI_STATUS (AE_OK
);
305 /*******************************************************************************
307 * FUNCTION: acpi_ev_get_gpe_event_info
309 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
310 * gpe_number - Raw GPE number
312 * RETURN: A GPE event_info struct. NULL if not a valid GPE
314 * DESCRIPTION: Returns the event_info struct associated with this GPE.
315 * Validates the gpe_block and the gpe_number
317 * Should be called only when the GPE lists are semaphore locked
318 * and not subject to change.
320 ******************************************************************************/
322 struct acpi_gpe_event_info
*
323 acpi_ev_get_gpe_event_info (
324 acpi_handle gpe_device
,
327 union acpi_operand_object
*obj_desc
;
328 struct acpi_gpe_block_info
*gpe_block
;
332 ACPI_FUNCTION_ENTRY ();
335 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
338 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
340 for (i
= 0; i
< ACPI_MAX_GPE_BLOCKS
; i
++) {
341 gpe_block
= acpi_gbl_gpe_fadt_blocks
[i
];
343 if ((gpe_number
>= gpe_block
->block_base_number
) &&
344 (gpe_number
< gpe_block
->block_base_number
+
345 (gpe_block
->register_count
* 8))) {
346 return (&gpe_block
->event_info
[gpe_number
-
347 gpe_block
->block_base_number
]);
352 /* The gpe_number was not in the range of either FADT GPE block */
357 /* A Non-NULL gpe_device means this is a GPE Block Device */
359 obj_desc
= acpi_ns_get_attached_object ((struct acpi_namespace_node
*) gpe_device
);
361 !obj_desc
->device
.gpe_block
) {
365 gpe_block
= obj_desc
->device
.gpe_block
;
367 if ((gpe_number
>= gpe_block
->block_base_number
) &&
368 (gpe_number
< gpe_block
->block_base_number
+ (gpe_block
->register_count
* 8))) {
369 return (&gpe_block
->event_info
[gpe_number
- gpe_block
->block_base_number
]);
376 /*******************************************************************************
378 * FUNCTION: acpi_ev_gpe_detect
380 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
381 * Can have multiple GPE blocks attached.
383 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
385 * DESCRIPTION: Detect if any GP events have occurred. This function is
386 * executed at interrupt level.
388 ******************************************************************************/
392 struct acpi_gpe_xrupt_info
*gpe_xrupt_list
)
394 u32 int_status
= ACPI_INTERRUPT_NOT_HANDLED
;
395 u8 enabled_status_byte
;
396 struct acpi_gpe_register_info
*gpe_register_info
;
400 struct acpi_gpe_block_info
*gpe_block
;
405 ACPI_FUNCTION_NAME ("ev_gpe_detect");
407 /* Check for the case where there are no GPEs */
409 if (!gpe_xrupt_list
) {
413 /* Examine all GPE blocks attached to this interrupt level */
415 acpi_os_acquire_lock (acpi_gbl_gpe_lock
, ACPI_ISR
);
416 gpe_block
= gpe_xrupt_list
->gpe_block_list_head
;
419 * Read all of the 8-bit GPE status and enable registers
420 * in this GPE block, saving all of them.
421 * Find all currently active GP events.
423 for (i
= 0; i
< gpe_block
->register_count
; i
++) {
424 /* Get the next status/enable pair */
426 gpe_register_info
= &gpe_block
->register_info
[i
];
428 /* Read the Status Register */
430 status
= acpi_hw_low_level_read (ACPI_GPE_REGISTER_WIDTH
, &status_reg
,
431 &gpe_register_info
->status_address
);
432 if (ACPI_FAILURE (status
)) {
433 goto unlock_and_exit
;
436 /* Read the Enable Register */
438 status
= acpi_hw_low_level_read (ACPI_GPE_REGISTER_WIDTH
, &enable_reg
,
439 &gpe_register_info
->enable_address
);
440 if (ACPI_FAILURE (status
)) {
441 goto unlock_and_exit
;
444 ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS
,
445 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
446 gpe_register_info
->base_gpe_number
, status_reg
, enable_reg
));
448 /* Check if there is anything active at all in this register */
450 enabled_status_byte
= (u8
) (status_reg
& enable_reg
);
451 if (!enabled_status_byte
) {
452 /* No active GPEs in this register, move on */
457 /* Now look at the individual GPEs in this byte register */
459 for (j
= 0; j
< ACPI_GPE_REGISTER_WIDTH
; j
++) {
460 /* Examine one GPE bit */
462 if (enabled_status_byte
& acpi_gbl_decode_to8bit
[j
]) {
464 * Found an active GPE. Dispatch the event to a handler
467 int_status
|= acpi_ev_gpe_dispatch (
468 &gpe_block
->event_info
[(i
* ACPI_GPE_REGISTER_WIDTH
) + j
],
469 (u32
) j
+ gpe_register_info
->base_gpe_number
);
474 gpe_block
= gpe_block
->next
;
479 acpi_os_release_lock (acpi_gbl_gpe_lock
, ACPI_ISR
);
484 /*******************************************************************************
486 * FUNCTION: acpi_ev_asynch_execute_gpe_method
488 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
492 * DESCRIPTION: Perform the actual execution of a GPE control method. This
493 * function is called from an invocation of acpi_os_queue_for_execution
494 * (and therefore does NOT execute at interrupt level) so that
495 * the control method itself is not executed in the context of
496 * an interrupt handler.
498 ******************************************************************************/
500 static void ACPI_SYSTEM_XFACE
501 acpi_ev_asynch_execute_gpe_method (
504 struct acpi_gpe_event_info
*gpe_event_info
= (void *) context
;
507 struct acpi_gpe_event_info local_gpe_event_info
;
508 struct acpi_parameter_info info
;
511 ACPI_FUNCTION_TRACE ("ev_asynch_execute_gpe_method");
514 status
= acpi_ut_acquire_mutex (ACPI_MTX_EVENTS
);
515 if (ACPI_FAILURE (status
)) {
519 /* Must revalidate the gpe_number/gpe_block */
521 if (!acpi_ev_valid_gpe_event (gpe_event_info
)) {
522 status
= acpi_ut_release_mutex (ACPI_MTX_EVENTS
);
526 /* Set the GPE flags for return to enabled state */
528 (void) acpi_ev_enable_gpe (gpe_event_info
, FALSE
);
531 * Take a snapshot of the GPE info for this level - we copy the
532 * info to prevent a race condition with remove_handler/remove_block.
534 ACPI_MEMCPY (&local_gpe_event_info
, gpe_event_info
,
535 sizeof (struct acpi_gpe_event_info
));
537 status
= acpi_ut_release_mutex (ACPI_MTX_EVENTS
);
538 if (ACPI_FAILURE (status
)) {
543 * Must check for control method type dispatch one more
544 * time to avoid race with ev_gpe_install_handler
546 if ((local_gpe_event_info
.flags
& ACPI_GPE_DISPATCH_MASK
) ==
547 ACPI_GPE_DISPATCH_METHOD
) {
549 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
550 * control method that corresponds to this GPE
552 info
.node
= local_gpe_event_info
.dispatch
.method_node
;
553 info
.parameters
= ACPI_CAST_PTR (union acpi_operand_object
*, gpe_event_info
);
554 info
.parameter_type
= ACPI_PARAM_GPE
;
556 status
= acpi_ns_evaluate_by_handle (&info
);
557 if (ACPI_FAILURE (status
)) {
559 "%s while evaluating method [%4.4s] for GPE[%2X]\n",
560 acpi_format_exception (status
),
561 acpi_ut_get_node_name (local_gpe_event_info
.dispatch
.method_node
),
566 if ((local_gpe_event_info
.flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
567 ACPI_GPE_LEVEL_TRIGGERED
) {
569 * GPE is level-triggered, we clear the GPE status bit after
570 * handling the event.
572 status
= acpi_hw_clear_gpe (&local_gpe_event_info
);
573 if (ACPI_FAILURE (status
)) {
578 /* Enable this GPE */
580 (void) acpi_hw_write_gpe_enable_reg (&local_gpe_event_info
);
585 /*******************************************************************************
587 * FUNCTION: acpi_ev_gpe_dispatch
589 * PARAMETERS: gpe_event_info - Info for this GPE
590 * gpe_number - Number relative to the parent GPE block
592 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
594 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
595 * or method (e.g. _Lxx/_Exx) handler.
597 * This function executes at interrupt level.
599 ******************************************************************************/
602 acpi_ev_gpe_dispatch (
603 struct acpi_gpe_event_info
*gpe_event_info
,
609 ACPI_FUNCTION_TRACE ("ev_gpe_dispatch");
613 * If edge-triggered, clear the GPE status bit now. Note that
614 * level-triggered events are cleared after the GPE is serviced.
616 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
617 ACPI_GPE_EDGE_TRIGGERED
) {
618 status
= acpi_hw_clear_gpe (gpe_event_info
);
619 if (ACPI_FAILURE (status
)) {
621 "acpi_ev_gpe_dispatch: %s, Unable to clear GPE[%2X]\n",
622 acpi_format_exception (status
), gpe_number
));
623 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED
);
627 /* Save current system state */
629 if (acpi_gbl_system_awake_and_running
) {
630 ACPI_SET_BIT (gpe_event_info
->flags
, ACPI_GPE_SYSTEM_RUNNING
);
633 ACPI_CLEAR_BIT (gpe_event_info
->flags
, ACPI_GPE_SYSTEM_RUNNING
);
637 * Dispatch the GPE to either an installed handler, or the control
638 * method associated with this GPE (_Lxx or _Exx).
639 * If a handler exists, we invoke it and do not attempt to run the method.
640 * If there is neither a handler nor a method, we disable the level to
641 * prevent further events from coming in here.
643 switch (gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) {
644 case ACPI_GPE_DISPATCH_HANDLER
:
647 * Invoke the installed handler (at interrupt level)
648 * Ignore return status for now. TBD: leave GPE disabled on error?
650 (void) gpe_event_info
->dispatch
.handler
->address (
651 gpe_event_info
->dispatch
.handler
->context
);
653 /* It is now safe to clear level-triggered events. */
655 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
656 ACPI_GPE_LEVEL_TRIGGERED
) {
657 status
= acpi_hw_clear_gpe (gpe_event_info
);
658 if (ACPI_FAILURE (status
)) {
660 "acpi_ev_gpe_dispatch: %s, Unable to clear GPE[%2X]\n",
661 acpi_format_exception (status
), gpe_number
));
662 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED
);
667 case ACPI_GPE_DISPATCH_METHOD
:
670 * Disable GPE, so it doesn't keep firing before the method has a
673 status
= acpi_ev_disable_gpe (gpe_event_info
);
674 if (ACPI_FAILURE (status
)) {
676 "acpi_ev_gpe_dispatch: %s, Unable to disable GPE[%2X]\n",
677 acpi_format_exception (status
), gpe_number
));
678 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED
);
682 * Execute the method associated with the GPE
683 * NOTE: Level-triggered GPEs are cleared after the method completes.
685 status
= acpi_os_queue_for_execution (OSD_PRIORITY_GPE
,
686 acpi_ev_asynch_execute_gpe_method
, gpe_event_info
);
687 if (ACPI_FAILURE (status
)) {
689 "acpi_ev_gpe_dispatch: %s, Unable to queue handler for GPE[%2X] - event disabled\n",
690 acpi_format_exception (status
), gpe_number
));
696 /* No handler or method to run! */
699 "acpi_ev_gpe_dispatch: No handler or method for GPE[%2X], disabling event\n",
703 * Disable the GPE. The GPE will remain disabled until the ACPI
704 * Core Subsystem is restarted, or a handler is installed.
706 status
= acpi_ev_disable_gpe (gpe_event_info
);
707 if (ACPI_FAILURE (status
)) {
709 "acpi_ev_gpe_dispatch: %s, Unable to disable GPE[%2X]\n",
710 acpi_format_exception (status
), gpe_number
));
711 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED
);
716 return_VALUE (ACPI_INTERRUPT_HANDLED
);
720 #ifdef ACPI_GPE_NOTIFY_CHECK
721 /*******************************************************************************
722 * TBD: NOT USED, PROTOTYPE ONLY AND WILL PROBABLY BE REMOVED
724 * FUNCTION: acpi_ev_check_for_wake_only_gpe
726 * PARAMETERS: gpe_event_info - info for this GPE
730 * DESCRIPTION: Determine if a a GPE is "wake-only".
732 * Called from Notify() code in interpreter when a "device_wake"
735 ******************************************************************************/
738 acpi_ev_check_for_wake_only_gpe (
739 struct acpi_gpe_event_info
*gpe_event_info
)
744 ACPI_FUNCTION_TRACE ("ev_check_for_wake_only_gpe");
747 if ((gpe_event_info
) && /* Only >0 for _Lxx/_Exx */
748 ((gpe_event_info
->flags
& ACPI_GPE_SYSTEM_MASK
) == ACPI_GPE_SYSTEM_RUNNING
)) /* System state at GPE time */ {
749 /* This must be a wake-only GPE, disable it */
751 status
= acpi_ev_disable_gpe (gpe_event_info
);
753 /* Set GPE to wake-only. Do not change wake disabled/enabled status */
755 acpi_ev_set_gpe_type (gpe_event_info
, ACPI_GPE_TYPE_WAKE
);
757 ACPI_REPORT_INFO (("GPE %p was updated from wake/run to wake-only\n",
760 /* This was a wake-only GPE */
762 return_ACPI_STATUS (AE_WAKE_ONLY_GPE
);
765 return_ACPI_STATUS (AE_OK
);