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;
401 struct acpi_gpe_block_info *gpe_block;
406 ACPI_FUNCTION_NAME ("ev_gpe_detect");
408 /* Check for the case where there are no GPEs */
410 if (!gpe_xrupt_list) {
414 /* Examine all GPE blocks attached to this interrupt level */
416 flags = acpi_os_acquire_lock (acpi_gbl_gpe_lock);
417 gpe_block = gpe_xrupt_list->gpe_block_list_head;
420 * Read all of the 8-bit GPE status and enable registers
421 * in this GPE block, saving all of them.
422 * Find all currently active GP events.
424 for (i = 0; i < gpe_block->register_count; i++) {
425 /* Get the next status/enable pair */
427 gpe_register_info = &gpe_block->register_info[i];
429 /* Read the Status Register */
431 status = acpi_hw_low_level_read (ACPI_GPE_REGISTER_WIDTH, &status_reg,
432 &gpe_register_info->status_address);
433 if (ACPI_FAILURE (status)) {
434 goto unlock_and_exit;
437 /* Read the Enable Register */
439 status = acpi_hw_low_level_read (ACPI_GPE_REGISTER_WIDTH, &enable_reg,
440 &gpe_register_info->enable_address);
441 if (ACPI_FAILURE (status)) {
442 goto unlock_and_exit;
445 ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
446 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
447 gpe_register_info->base_gpe_number, status_reg, enable_reg));
449 /* Check if there is anything active at all in this register */
451 enabled_status_byte = (u8) (status_reg & enable_reg);
452 if (!enabled_status_byte) {
453 /* No active GPEs in this register, move on */
458 /* Now look at the individual GPEs in this byte register */
460 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
461 /* Examine one GPE bit */
463 if (enabled_status_byte & acpi_gbl_decode_to8bit[j]) {
465 * Found an active GPE. Dispatch the event to a handler
468 int_status |= acpi_ev_gpe_dispatch (
469 &gpe_block->event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j],
470 (u32) j + gpe_register_info->base_gpe_number);
475 gpe_block = gpe_block->next;
480 acpi_os_release_lock (acpi_gbl_gpe_lock, flags);
485 /*******************************************************************************
487 * FUNCTION: acpi_ev_asynch_execute_gpe_method
489 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
493 * DESCRIPTION: Perform the actual execution of a GPE control method. This
494 * function is called from an invocation of acpi_os_queue_for_execution
495 * (and therefore does NOT execute at interrupt level) so that
496 * the control method itself is not executed in the context of
497 * an interrupt handler.
499 ******************************************************************************/
501 static void ACPI_SYSTEM_XFACE
502 acpi_ev_asynch_execute_gpe_method (
505 struct acpi_gpe_event_info *gpe_event_info = (void *) context;
508 struct acpi_gpe_event_info local_gpe_event_info;
509 struct acpi_parameter_info info;
512 ACPI_FUNCTION_TRACE ("ev_asynch_execute_gpe_method");
515 status = acpi_ut_acquire_mutex (ACPI_MTX_EVENTS);
516 if (ACPI_FAILURE (status)) {
520 /* Must revalidate the gpe_number/gpe_block */
522 if (!acpi_ev_valid_gpe_event (gpe_event_info)) {
523 status = acpi_ut_release_mutex (ACPI_MTX_EVENTS);
527 /* Set the GPE flags for return to enabled state */
529 (void) acpi_ev_enable_gpe (gpe_event_info, FALSE);
532 * Take a snapshot of the GPE info for this level - we copy the
533 * info to prevent a race condition with remove_handler/remove_block.
535 ACPI_MEMCPY (&local_gpe_event_info, gpe_event_info,
536 sizeof (struct acpi_gpe_event_info));
538 status = acpi_ut_release_mutex (ACPI_MTX_EVENTS);
539 if (ACPI_FAILURE (status)) {
544 * Must check for control method type dispatch one more
545 * time to avoid race with ev_gpe_install_handler
547 if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
548 ACPI_GPE_DISPATCH_METHOD) {
550 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
551 * control method that corresponds to this GPE
553 info.node = local_gpe_event_info.dispatch.method_node;
554 info.parameters = ACPI_CAST_PTR (union acpi_operand_object *, gpe_event_info);
555 info.parameter_type = ACPI_PARAM_GPE;
557 status = acpi_ns_evaluate_by_handle (&info);
558 if (ACPI_FAILURE (status)) {
560 "%s while evaluating method [%4.4s] for GPE[%2X]\n",
561 acpi_format_exception (status),
562 acpi_ut_get_node_name (local_gpe_event_info.dispatch.method_node),
567 if ((local_gpe_event_info.flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
568 ACPI_GPE_LEVEL_TRIGGERED) {
570 * GPE is level-triggered, we clear the GPE status bit after
571 * handling the event.
573 status = acpi_hw_clear_gpe (&local_gpe_event_info);
574 if (ACPI_FAILURE (status)) {
579 /* Enable this GPE */
581 (void) acpi_hw_write_gpe_enable_reg (&local_gpe_event_info);
586 /*******************************************************************************
588 * FUNCTION: acpi_ev_gpe_dispatch
590 * PARAMETERS: gpe_event_info - Info for this GPE
591 * gpe_number - Number relative to the parent GPE block
593 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
595 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
596 * or method (e.g. _Lxx/_Exx) handler.
598 * This function executes at interrupt level.
600 ******************************************************************************/
603 acpi_ev_gpe_dispatch (
604 struct acpi_gpe_event_info *gpe_event_info,
610 ACPI_FUNCTION_TRACE ("ev_gpe_dispatch");
614 * If edge-triggered, clear the GPE status bit now. Note that
615 * level-triggered events are cleared after the GPE is serviced.
617 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
618 ACPI_GPE_EDGE_TRIGGERED) {
619 status = acpi_hw_clear_gpe (gpe_event_info);
620 if (ACPI_FAILURE (status)) {
622 "acpi_ev_gpe_dispatch: %s, Unable to clear GPE[%2X]\n",
623 acpi_format_exception (status), gpe_number));
624 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
628 /* Save current system state */
630 if (acpi_gbl_system_awake_and_running) {
631 ACPI_SET_BIT (gpe_event_info->flags, ACPI_GPE_SYSTEM_RUNNING);
634 ACPI_CLEAR_BIT (gpe_event_info->flags, ACPI_GPE_SYSTEM_RUNNING);
638 * Dispatch the GPE to either an installed handler, or the control
639 * method associated with this GPE (_Lxx or _Exx).
640 * If a handler exists, we invoke it and do not attempt to run the method.
641 * If there is neither a handler nor a method, we disable the level to
642 * prevent further events from coming in here.
644 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
645 case ACPI_GPE_DISPATCH_HANDLER:
648 * Invoke the installed handler (at interrupt level)
649 * Ignore return status for now. TBD: leave GPE disabled on error?
651 (void) gpe_event_info->dispatch.handler->address (
652 gpe_event_info->dispatch.handler->context);
654 /* It is now safe to clear level-triggered events. */
656 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
657 ACPI_GPE_LEVEL_TRIGGERED) {
658 status = acpi_hw_clear_gpe (gpe_event_info);
659 if (ACPI_FAILURE (status)) {
661 "acpi_ev_gpe_dispatch: %s, Unable to clear GPE[%2X]\n",
662 acpi_format_exception (status), gpe_number));
663 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
668 case ACPI_GPE_DISPATCH_METHOD:
671 * Disable GPE, so it doesn't keep firing before the method has a
674 status = acpi_ev_disable_gpe (gpe_event_info);
675 if (ACPI_FAILURE (status)) {
677 "acpi_ev_gpe_dispatch: %s, Unable to disable GPE[%2X]\n",
678 acpi_format_exception (status), gpe_number));
679 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
683 * Execute the method associated with the GPE
684 * NOTE: Level-triggered GPEs are cleared after the method completes.
686 status = acpi_os_queue_for_execution (OSD_PRIORITY_GPE,
687 acpi_ev_asynch_execute_gpe_method, gpe_event_info);
688 if (ACPI_FAILURE (status)) {
690 "acpi_ev_gpe_dispatch: %s, Unable to queue handler for GPE[%2X] - event disabled\n",
691 acpi_format_exception (status), gpe_number));
697 /* No handler or method to run! */
700 "acpi_ev_gpe_dispatch: No handler or method for GPE[%2X], disabling event\n",
704 * Disable the GPE. The GPE will remain disabled until the ACPI
705 * Core Subsystem is restarted, or a handler is installed.
707 status = acpi_ev_disable_gpe (gpe_event_info);
708 if (ACPI_FAILURE (status)) {
710 "acpi_ev_gpe_dispatch: %s, Unable to disable GPE[%2X]\n",
711 acpi_format_exception (status), gpe_number));
712 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
717 return_VALUE (ACPI_INTERRUPT_HANDLED);
721 #ifdef ACPI_GPE_NOTIFY_CHECK
722 /*******************************************************************************
723 * TBD: NOT USED, PROTOTYPE ONLY AND WILL PROBABLY BE REMOVED
725 * FUNCTION: acpi_ev_check_for_wake_only_gpe
727 * PARAMETERS: gpe_event_info - info for this GPE
731 * DESCRIPTION: Determine if a a GPE is "wake-only".
733 * Called from Notify() code in interpreter when a "device_wake"
736 ******************************************************************************/
739 acpi_ev_check_for_wake_only_gpe (
740 struct acpi_gpe_event_info *gpe_event_info)
745 ACPI_FUNCTION_TRACE ("ev_check_for_wake_only_gpe");
748 if ((gpe_event_info) && /* Only >0 for _Lxx/_Exx */
749 ((gpe_event_info->flags & ACPI_GPE_SYSTEM_MASK) == ACPI_GPE_SYSTEM_RUNNING)) /* System state at GPE time */ {
750 /* This must be a wake-only GPE, disable it */
752 status = acpi_ev_disable_gpe (gpe_event_info);
754 /* Set GPE to wake-only. Do not change wake disabled/enabled status */
756 acpi_ev_set_gpe_type (gpe_event_info, ACPI_GPE_TYPE_WAKE);
758 ACPI_REPORT_INFO (("GPE %p was updated from wake/run to wake-only\n",
761 /* This was a wake-only GPE */
763 return_ACPI_STATUS (AE_WAKE_ONLY_GPE);
766 return_ACPI_STATUS (AE_OK);