1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
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>
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME("evgpe")
51 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
52 /* Local prototypes */
53 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_execute_gpe_method(void *context
);
55 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_enable_gpe(void *context
);
57 /*******************************************************************************
59 * FUNCTION: acpi_ev_update_gpe_enable_mask
61 * PARAMETERS: gpe_event_info - GPE to update
65 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
66 * runtime references to this GPE
68 ******************************************************************************/
71 acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info
*gpe_event_info
)
73 struct acpi_gpe_register_info
*gpe_register_info
;
76 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask
);
78 gpe_register_info
= gpe_event_info
->register_info
;
79 if (!gpe_register_info
) {
80 return_ACPI_STATUS(AE_NOT_EXIST
);
83 register_bit
= acpi_hw_get_gpe_register_bit(gpe_event_info
);
85 /* Clear the run bit up front */
87 ACPI_CLEAR_BIT(gpe_register_info
->enable_for_run
, register_bit
);
89 /* Set the mask bit only if there are references to this GPE */
91 if (gpe_event_info
->runtime_count
) {
92 ACPI_SET_BIT(gpe_register_info
->enable_for_run
,
96 return_ACPI_STATUS(AE_OK
);
99 /*******************************************************************************
101 * FUNCTION: acpi_ev_enable_gpe
103 * PARAMETERS: gpe_event_info - GPE to enable
107 * DESCRIPTION: Clear a GPE of stale events and enable it.
109 ******************************************************************************/
111 acpi_status
acpi_ev_enable_gpe(struct acpi_gpe_event_info
*gpe_event_info
)
115 ACPI_FUNCTION_TRACE(ev_enable_gpe
);
118 * We will only allow a GPE to be enabled if it has either an associated
119 * method (_Lxx/_Exx) or a handler, or is using the implicit notify
120 * feature. Otherwise, the GPE will be immediately disabled by
121 * acpi_ev_gpe_dispatch the first time it fires.
123 if ((gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) ==
124 ACPI_GPE_DISPATCH_NONE
) {
125 return_ACPI_STATUS(AE_NO_HANDLER
);
128 /* Clear the GPE (of stale events) */
130 status
= acpi_hw_clear_gpe(gpe_event_info
);
131 if (ACPI_FAILURE(status
)) {
132 return_ACPI_STATUS(status
);
135 /* Enable the requested GPE */
137 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_ENABLE_SAVE
);
138 return_ACPI_STATUS(status
);
141 /*******************************************************************************
143 * FUNCTION: acpi_ev_add_gpe_reference
145 * PARAMETERS: gpe_event_info - Add a reference to this GPE
149 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
152 ******************************************************************************/
155 acpi_ev_add_gpe_reference(struct acpi_gpe_event_info
*gpe_event_info
)
157 acpi_status status
= AE_OK
;
159 ACPI_FUNCTION_TRACE(ev_add_gpe_reference
);
161 if (gpe_event_info
->runtime_count
== ACPI_UINT8_MAX
) {
162 return_ACPI_STATUS(AE_LIMIT
);
165 gpe_event_info
->runtime_count
++;
166 if (gpe_event_info
->runtime_count
== 1) {
168 /* Enable on first reference */
170 status
= acpi_ev_update_gpe_enable_mask(gpe_event_info
);
171 if (ACPI_SUCCESS(status
)) {
172 status
= acpi_ev_enable_gpe(gpe_event_info
);
175 if (ACPI_FAILURE(status
)) {
176 gpe_event_info
->runtime_count
--;
180 return_ACPI_STATUS(status
);
183 /*******************************************************************************
185 * FUNCTION: acpi_ev_remove_gpe_reference
187 * PARAMETERS: gpe_event_info - Remove a reference to this GPE
191 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
192 * removed, the GPE is hardware-disabled.
194 ******************************************************************************/
197 acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info
*gpe_event_info
)
199 acpi_status status
= AE_OK
;
201 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference
);
203 if (!gpe_event_info
->runtime_count
) {
204 return_ACPI_STATUS(AE_LIMIT
);
207 gpe_event_info
->runtime_count
--;
208 if (!gpe_event_info
->runtime_count
) {
210 /* Disable on last reference */
212 status
= acpi_ev_update_gpe_enable_mask(gpe_event_info
);
213 if (ACPI_SUCCESS(status
)) {
215 acpi_hw_low_set_gpe(gpe_event_info
,
216 ACPI_GPE_DISABLE_SAVE
);
219 if (ACPI_FAILURE(status
)) {
220 gpe_event_info
->runtime_count
++;
224 return_ACPI_STATUS(status
);
227 /*******************************************************************************
229 * FUNCTION: acpi_ev_low_get_gpe_info
231 * PARAMETERS: gpe_number - Raw GPE number
232 * gpe_block - A GPE info block
234 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
235 * is not within the specified GPE block)
237 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
238 * the low-level implementation of ev_get_gpe_event_info.
240 ******************************************************************************/
242 struct acpi_gpe_event_info
*acpi_ev_low_get_gpe_info(u32 gpe_number
,
243 struct acpi_gpe_block_info
249 * Validate that the gpe_number is within the specified gpe_block.
252 if (!gpe_block
|| (gpe_number
< gpe_block
->block_base_number
)) {
256 gpe_index
= gpe_number
- gpe_block
->block_base_number
;
257 if (gpe_index
>= gpe_block
->gpe_count
) {
261 return (&gpe_block
->event_info
[gpe_index
]);
265 /*******************************************************************************
267 * FUNCTION: acpi_ev_get_gpe_event_info
269 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
270 * gpe_number - Raw GPE number
272 * RETURN: A GPE event_info struct. NULL if not a valid GPE
274 * DESCRIPTION: Returns the event_info struct associated with this GPE.
275 * Validates the gpe_block and the gpe_number
277 * Should be called only when the GPE lists are semaphore locked
278 * and not subject to change.
280 ******************************************************************************/
282 struct acpi_gpe_event_info
*acpi_ev_get_gpe_event_info(acpi_handle gpe_device
,
285 union acpi_operand_object
*obj_desc
;
286 struct acpi_gpe_event_info
*gpe_info
;
289 ACPI_FUNCTION_ENTRY();
291 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
295 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
297 for (i
= 0; i
< ACPI_MAX_GPE_BLOCKS
; i
++) {
298 gpe_info
= acpi_ev_low_get_gpe_info(gpe_number
,
299 acpi_gbl_gpe_fadt_blocks
306 /* The gpe_number was not in the range of either FADT GPE block */
311 /* A Non-NULL gpe_device means this is a GPE Block Device */
314 acpi_ns_get_attached_object((struct acpi_namespace_node
*)
316 if (!obj_desc
|| !obj_desc
->device
.gpe_block
) {
320 return (acpi_ev_low_get_gpe_info
321 (gpe_number
, obj_desc
->device
.gpe_block
));
324 /*******************************************************************************
326 * FUNCTION: acpi_ev_gpe_detect
328 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
329 * Can have multiple GPE blocks attached.
331 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
333 * DESCRIPTION: Detect if any GP events have occurred. This function is
334 * executed at interrupt level.
336 ******************************************************************************/
338 u32
acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info
*gpe_xrupt_list
)
341 struct acpi_gpe_block_info
*gpe_block
;
342 struct acpi_gpe_register_info
*gpe_register_info
;
343 u32 int_status
= ACPI_INTERRUPT_NOT_HANDLED
;
344 u8 enabled_status_byte
;
347 acpi_cpu_flags flags
;
351 ACPI_FUNCTION_NAME(ev_gpe_detect
);
353 /* Check for the case where there are no GPEs */
355 if (!gpe_xrupt_list
) {
360 * We need to obtain the GPE lock for both the data structs and registers
361 * Note: Not necessary to obtain the hardware lock, since the GPE
362 * registers are owned by the gpe_lock.
364 flags
= acpi_os_acquire_lock(acpi_gbl_gpe_lock
);
366 /* Examine all GPE blocks attached to this interrupt level */
368 gpe_block
= gpe_xrupt_list
->gpe_block_list_head
;
371 * Read all of the 8-bit GPE status and enable registers in this GPE
372 * block, saving all of them. Find all currently active GP events.
374 for (i
= 0; i
< gpe_block
->register_count
; i
++) {
376 /* Get the next status/enable pair */
378 gpe_register_info
= &gpe_block
->register_info
[i
];
381 * Optimization: If there are no GPEs enabled within this
382 * register, we can safely ignore the entire register.
384 if (!(gpe_register_info
->enable_for_run
|
385 gpe_register_info
->enable_for_wake
)) {
386 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS
,
387 "Ignore disabled registers for GPE %02X-%02X: "
388 "RunEnable=%02X, WakeEnable=%02X\n",
393 (ACPI_GPE_REGISTER_WIDTH
- 1),
401 /* Read the Status Register */
404 acpi_hw_read(&status_reg
,
405 &gpe_register_info
->status_address
);
406 if (ACPI_FAILURE(status
)) {
407 goto unlock_and_exit
;
410 /* Read the Enable Register */
413 acpi_hw_read(&enable_reg
,
414 &gpe_register_info
->enable_address
);
415 if (ACPI_FAILURE(status
)) {
416 goto unlock_and_exit
;
419 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS
,
420 "Read registers for GPE %02X-%02X: Status=%02X, Enable=%02X, "
421 "RunEnable=%02X, WakeEnable=%02X\n",
422 gpe_register_info
->base_gpe_number
,
423 gpe_register_info
->base_gpe_number
+
424 (ACPI_GPE_REGISTER_WIDTH
- 1),
425 status_reg
, enable_reg
,
426 gpe_register_info
->enable_for_run
,
427 gpe_register_info
->enable_for_wake
));
429 /* Check if there is anything active at all in this register */
431 enabled_status_byte
= (u8
)(status_reg
& enable_reg
);
432 if (!enabled_status_byte
) {
434 /* No active GPEs in this register, move on */
439 /* Now look at the individual GPEs in this byte register */
441 for (j
= 0; j
< ACPI_GPE_REGISTER_WIDTH
; j
++) {
443 /* Examine one GPE bit */
445 if (enabled_status_byte
& (1 << j
)) {
447 * Found an active GPE. Dispatch the event to a handler
451 acpi_ev_gpe_dispatch(gpe_block
->
454 event_info
[((acpi_size
) i
* ACPI_GPE_REGISTER_WIDTH
) + j
], j
+ gpe_register_info
->base_gpe_number
);
459 gpe_block
= gpe_block
->next
;
464 acpi_os_release_lock(acpi_gbl_gpe_lock
, flags
);
468 /*******************************************************************************
470 * FUNCTION: acpi_ev_asynch_execute_gpe_method
472 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
476 * DESCRIPTION: Perform the actual execution of a GPE control method. This
477 * function is called from an invocation of acpi_os_execute and
478 * therefore does NOT execute at interrupt level - so that
479 * the control method itself is not executed in the context of
480 * an interrupt handler.
482 ******************************************************************************/
484 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_execute_gpe_method(void *context
)
486 struct acpi_gpe_event_info
*gpe_event_info
= context
;
488 struct acpi_gpe_event_info
*local_gpe_event_info
;
489 struct acpi_evaluate_info
*info
;
490 struct acpi_gpe_notify_info
*notify
;
492 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method
);
494 /* Allocate a local GPE block */
496 local_gpe_event_info
=
497 ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info
));
498 if (!local_gpe_event_info
) {
499 ACPI_EXCEPTION((AE_INFO
, AE_NO_MEMORY
, "while handling a GPE"));
503 status
= acpi_ut_acquire_mutex(ACPI_MTX_EVENTS
);
504 if (ACPI_FAILURE(status
)) {
505 ACPI_FREE(local_gpe_event_info
);
509 /* Must revalidate the gpe_number/gpe_block */
511 if (!acpi_ev_valid_gpe_event(gpe_event_info
)) {
512 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
513 ACPI_FREE(local_gpe_event_info
);
518 * Take a snapshot of the GPE info for this level - we copy the info to
519 * prevent a race condition with remove_handler/remove_block.
521 ACPI_MEMCPY(local_gpe_event_info
, gpe_event_info
,
522 sizeof(struct acpi_gpe_event_info
));
524 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
525 if (ACPI_FAILURE(status
)) {
526 ACPI_FREE(local_gpe_event_info
);
530 /* Do the correct dispatch - normal method or implicit notify */
532 switch (local_gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) {
533 case ACPI_GPE_DISPATCH_NOTIFY
:
536 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
537 * NOTE: the request is queued for execution after this method
538 * completes. The notify handlers are NOT invoked synchronously
539 * from this thread -- because handlers may in turn run other
542 * June 2012: Expand implicit notify mechanism to support
543 * notifies on multiple device objects.
545 notify
= local_gpe_event_info
->dispatch
.notify_list
;
546 while (ACPI_SUCCESS(status
) && notify
) {
548 acpi_ev_queue_notify_request(notify
->device_node
,
549 ACPI_NOTIFY_DEVICE_WAKE
);
551 notify
= notify
->next
;
556 case ACPI_GPE_DISPATCH_METHOD
:
558 /* Allocate the evaluation information block */
560 info
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info
));
562 status
= AE_NO_MEMORY
;
565 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
566 * _Lxx/_Exx control method that corresponds to this GPE
569 local_gpe_event_info
->dispatch
.method_node
;
570 info
->flags
= ACPI_IGNORE_RETURN_VALUE
;
572 status
= acpi_ns_evaluate(info
);
576 if (ACPI_FAILURE(status
)) {
577 ACPI_EXCEPTION((AE_INFO
, status
,
578 "while evaluating GPE method [%4.4s]",
579 acpi_ut_get_node_name
580 (local_gpe_event_info
->dispatch
.
587 return_VOID
; /* Should never happen */
590 /* Defer enabling of GPE until all notify handlers are done */
592 status
= acpi_os_execute(OSL_NOTIFY_HANDLER
,
593 acpi_ev_asynch_enable_gpe
,
594 local_gpe_event_info
);
595 if (ACPI_FAILURE(status
)) {
596 ACPI_FREE(local_gpe_event_info
);
602 /*******************************************************************************
604 * FUNCTION: acpi_ev_asynch_enable_gpe
606 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
607 * Callback from acpi_os_execute
611 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
612 * complete (i.e., finish execution of Notify)
614 ******************************************************************************/
616 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_enable_gpe(void *context
)
618 struct acpi_gpe_event_info
*gpe_event_info
= context
;
619 acpi_cpu_flags flags
;
621 flags
= acpi_os_acquire_lock(acpi_gbl_gpe_lock
);
622 (void)acpi_ev_finish_gpe(gpe_event_info
);
623 acpi_os_release_lock(acpi_gbl_gpe_lock
, flags
);
625 ACPI_FREE(gpe_event_info
);
630 /*******************************************************************************
632 * FUNCTION: acpi_ev_finish_gpe
634 * PARAMETERS: gpe_event_info - Info for this GPE
638 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
639 * of a GPE method or a synchronous or asynchronous GPE handler.
641 ******************************************************************************/
643 acpi_status
acpi_ev_finish_gpe(struct acpi_gpe_event_info
* gpe_event_info
)
647 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
648 ACPI_GPE_LEVEL_TRIGGERED
) {
650 * GPE is level-triggered, we clear the GPE status bit after
651 * handling the event.
653 status
= acpi_hw_clear_gpe(gpe_event_info
);
654 if (ACPI_FAILURE(status
)) {
660 * Enable this GPE, conditionally. This means that the GPE will
661 * only be physically enabled if the enable_mask bit is set
664 (void)acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_CONDITIONAL_ENABLE
);
669 /*******************************************************************************
671 * FUNCTION: acpi_ev_gpe_dispatch
673 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
674 * gpe_event_info - Info for this GPE
675 * gpe_number - Number relative to the parent GPE block
677 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
679 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
680 * or method (e.g. _Lxx/_Exx) handler.
682 * This function executes at interrupt level.
684 ******************************************************************************/
687 acpi_ev_gpe_dispatch(struct acpi_namespace_node
*gpe_device
,
688 struct acpi_gpe_event_info
*gpe_event_info
, u32 gpe_number
)
693 ACPI_FUNCTION_TRACE(ev_gpe_dispatch
);
695 /* Invoke global event handler if present */
698 if (acpi_gbl_global_event_handler
) {
699 acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE
, gpe_device
,
701 acpi_gbl_global_event_handler_context
);
705 * Always disable the GPE so that it does not keep firing before
706 * any asynchronous activity completes (either from the execution
707 * of a GPE method or an asynchronous GPE handler.)
709 * If there is no handler or method to run, just disable the
710 * GPE and leave it disabled permanently to prevent further such
711 * pointless events from firing.
713 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
714 if (ACPI_FAILURE(status
)) {
715 ACPI_EXCEPTION((AE_INFO
, status
,
716 "Unable to disable GPE %02X", gpe_number
));
717 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
721 * If edge-triggered, clear the GPE status bit now. Note that
722 * level-triggered events are cleared after the GPE is serviced.
724 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
725 ACPI_GPE_EDGE_TRIGGERED
) {
726 status
= acpi_hw_clear_gpe(gpe_event_info
);
727 if (ACPI_FAILURE(status
)) {
728 ACPI_EXCEPTION((AE_INFO
, status
,
729 "Unable to clear GPE %02X",
731 (void)acpi_hw_low_set_gpe(gpe_event_info
,
732 ACPI_GPE_CONDITIONAL_ENABLE
);
733 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
738 * Dispatch the GPE to either an installed handler or the control
739 * method associated with this GPE (_Lxx or _Exx). If a handler
740 * exists, we invoke it and do not attempt to run the method.
741 * If there is neither a handler nor a method, leave the GPE
744 switch (gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) {
745 case ACPI_GPE_DISPATCH_HANDLER
:
747 /* Invoke the installed handler (at interrupt level) */
750 gpe_event_info
->dispatch
.handler
->address(gpe_device
,
756 /* If requested, clear (if level-triggered) and reenable the GPE */
758 if (return_value
& ACPI_REENABLE_GPE
) {
759 (void)acpi_ev_finish_gpe(gpe_event_info
);
763 case ACPI_GPE_DISPATCH_METHOD
:
764 case ACPI_GPE_DISPATCH_NOTIFY
:
766 * Execute the method associated with the GPE
767 * NOTE: Level-triggered GPEs are cleared after the method completes.
769 status
= acpi_os_execute(OSL_GPE_HANDLER
,
770 acpi_ev_asynch_execute_gpe_method
,
772 if (ACPI_FAILURE(status
)) {
773 ACPI_EXCEPTION((AE_INFO
, status
,
774 "Unable to queue handler for GPE %02X - event disabled",
781 * No handler or method to run!
782 * 03/2010: This case should no longer be possible. We will not allow
783 * a GPE to be enabled if it has no handler or method.
786 "No handler or method for GPE %02X, disabling event",
792 return_UINT32(ACPI_INTERRUPT_HANDLED
);
795 #endif /* !ACPI_REDUCED_HARDWARE */