1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2010, 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")
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
,
86 /* Clear the run bit up front */
88 ACPI_CLEAR_BIT(gpe_register_info
->enable_for_run
, register_bit
);
90 /* Set the mask bit only if there are references to this GPE */
92 if (gpe_event_info
->runtime_count
) {
93 ACPI_SET_BIT(gpe_register_info
->enable_for_run
, (u8
)register_bit
);
96 return_ACPI_STATUS(AE_OK
);
99 /*******************************************************************************
101 * FUNCTION: acpi_ev_enable_gpe
103 * PARAMETERS: gpe_event_info - GPE to enable
107 * DESCRIPTION: Clear the given GPE from stale events and enable it.
109 ******************************************************************************/
111 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
119 * associated method (_Lxx/_Exx) or a handler. Otherwise, the
120 * GPE will be immediately disabled by acpi_ev_gpe_dispatch the
121 * first time it fires.
123 if (!(gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
)) {
124 return_ACPI_STATUS(AE_NO_HANDLER
);
127 /* Clear the GPE (of stale events) */
128 status
= acpi_hw_clear_gpe(gpe_event_info
);
129 if (ACPI_FAILURE(status
)) {
130 return_ACPI_STATUS(status
);
133 /* Enable the requested GPE */
134 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_ENABLE
);
136 return_ACPI_STATUS(status
);
140 /*******************************************************************************
142 * FUNCTION: acpi_ev_add_gpe_reference
144 * PARAMETERS: gpe_event_info - GPE to enable
148 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
151 ******************************************************************************/
153 acpi_status
acpi_ev_add_gpe_reference(struct acpi_gpe_event_info
*gpe_event_info
)
155 acpi_status status
= AE_OK
;
157 ACPI_FUNCTION_TRACE(ev_add_gpe_reference
);
159 if (gpe_event_info
->runtime_count
== ACPI_UINT8_MAX
) {
160 return_ACPI_STATUS(AE_LIMIT
);
163 gpe_event_info
->runtime_count
++;
164 if (gpe_event_info
->runtime_count
== 1) {
165 status
= acpi_ev_update_gpe_enable_mask(gpe_event_info
);
166 if (ACPI_SUCCESS(status
)) {
167 status
= acpi_ev_enable_gpe(gpe_event_info
);
170 if (ACPI_FAILURE(status
)) {
171 gpe_event_info
->runtime_count
--;
175 return_ACPI_STATUS(status
);
178 /*******************************************************************************
180 * FUNCTION: acpi_ev_remove_gpe_reference
182 * PARAMETERS: gpe_event_info - GPE to disable
186 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
187 * removed, the GPE is hardware-disabled.
189 ******************************************************************************/
191 acpi_status
acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info
*gpe_event_info
)
193 acpi_status status
= AE_OK
;
195 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference
);
197 if (!gpe_event_info
->runtime_count
) {
198 return_ACPI_STATUS(AE_LIMIT
);
201 gpe_event_info
->runtime_count
--;
202 if (!gpe_event_info
->runtime_count
) {
203 status
= acpi_ev_update_gpe_enable_mask(gpe_event_info
);
204 if (ACPI_SUCCESS(status
)) {
205 status
= acpi_hw_low_set_gpe(gpe_event_info
,
209 if (ACPI_FAILURE(status
)) {
210 gpe_event_info
->runtime_count
++;
214 return_ACPI_STATUS(status
);
217 /*******************************************************************************
219 * FUNCTION: acpi_ev_low_get_gpe_info
221 * PARAMETERS: gpe_number - Raw GPE number
222 * gpe_block - A GPE info block
224 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
225 * is not within the specified GPE block)
227 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
228 * the low-level implementation of ev_get_gpe_event_info.
230 ******************************************************************************/
232 struct acpi_gpe_event_info
*acpi_ev_low_get_gpe_info(u32 gpe_number
,
233 struct acpi_gpe_block_info
239 * Validate that the gpe_number is within the specified gpe_block.
242 if (!gpe_block
|| (gpe_number
< gpe_block
->block_base_number
)) {
246 gpe_index
= gpe_number
- gpe_block
->block_base_number
;
247 if (gpe_index
>= gpe_block
->gpe_count
) {
251 return (&gpe_block
->event_info
[gpe_index
]);
255 /*******************************************************************************
257 * FUNCTION: acpi_ev_get_gpe_event_info
259 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
260 * gpe_number - Raw GPE number
262 * RETURN: A GPE event_info struct. NULL if not a valid GPE
264 * DESCRIPTION: Returns the event_info struct associated with this GPE.
265 * Validates the gpe_block and the gpe_number
267 * Should be called only when the GPE lists are semaphore locked
268 * and not subject to change.
270 ******************************************************************************/
272 struct acpi_gpe_event_info
*acpi_ev_get_gpe_event_info(acpi_handle gpe_device
,
275 union acpi_operand_object
*obj_desc
;
276 struct acpi_gpe_event_info
*gpe_info
;
279 ACPI_FUNCTION_ENTRY();
281 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
285 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
287 for (i
= 0; i
< ACPI_MAX_GPE_BLOCKS
; i
++) {
288 gpe_info
= acpi_ev_low_get_gpe_info(gpe_number
,
289 acpi_gbl_gpe_fadt_blocks
296 /* The gpe_number was not in the range of either FADT GPE block */
301 /* A Non-NULL gpe_device means this is a GPE Block Device */
303 obj_desc
= acpi_ns_get_attached_object((struct acpi_namespace_node
*)
305 if (!obj_desc
|| !obj_desc
->device
.gpe_block
) {
309 return (acpi_ev_low_get_gpe_info
310 (gpe_number
, obj_desc
->device
.gpe_block
));
313 /*******************************************************************************
315 * FUNCTION: acpi_ev_gpe_detect
317 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
318 * Can have multiple GPE blocks attached.
320 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
322 * DESCRIPTION: Detect if any GP events have occurred. This function is
323 * executed at interrupt level.
325 ******************************************************************************/
327 u32
acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info
* gpe_xrupt_list
)
330 struct acpi_gpe_block_info
*gpe_block
;
331 struct acpi_gpe_register_info
*gpe_register_info
;
332 u32 int_status
= ACPI_INTERRUPT_NOT_HANDLED
;
333 u8 enabled_status_byte
;
336 acpi_cpu_flags flags
;
340 ACPI_FUNCTION_NAME(ev_gpe_detect
);
342 /* Check for the case where there are no GPEs */
344 if (!gpe_xrupt_list
) {
349 * We need to obtain the GPE lock for both the data structs and registers
350 * Note: Not necessary to obtain the hardware lock, since the GPE
351 * registers are owned by the gpe_lock.
353 flags
= acpi_os_acquire_lock(acpi_gbl_gpe_lock
);
355 /* Examine all GPE blocks attached to this interrupt level */
357 gpe_block
= gpe_xrupt_list
->gpe_block_list_head
;
360 * Read all of the 8-bit GPE status and enable registers in this GPE
361 * block, saving all of them. Find all currently active GP events.
363 for (i
= 0; i
< gpe_block
->register_count
; i
++) {
365 /* Get the next status/enable pair */
367 gpe_register_info
= &gpe_block
->register_info
[i
];
369 /* Read the Status Register */
372 acpi_hw_read(&status_reg
,
373 &gpe_register_info
->status_address
);
374 if (ACPI_FAILURE(status
)) {
375 goto unlock_and_exit
;
378 /* Read the Enable Register */
381 acpi_hw_read(&enable_reg
,
382 &gpe_register_info
->enable_address
);
383 if (ACPI_FAILURE(status
)) {
384 goto unlock_and_exit
;
387 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS
,
388 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
389 gpe_register_info
->base_gpe_number
,
390 status_reg
, enable_reg
));
392 /* Check if there is anything active at all in this register */
394 enabled_status_byte
= (u8
) (status_reg
& enable_reg
);
395 if (!enabled_status_byte
) {
397 /* No active GPEs in this register, move on */
402 /* Now look at the individual GPEs in this byte register */
404 for (j
= 0; j
< ACPI_GPE_REGISTER_WIDTH
; j
++) {
406 /* Examine one GPE bit */
408 if (enabled_status_byte
& (1 << j
)) {
410 * Found an active GPE. Dispatch the event to a handler
414 acpi_ev_gpe_dispatch(gpe_block
->
417 event_info
[((acpi_size
) i
* ACPI_GPE_REGISTER_WIDTH
) + j
], j
+ gpe_register_info
->base_gpe_number
);
422 gpe_block
= gpe_block
->next
;
427 acpi_os_release_lock(acpi_gbl_gpe_lock
, flags
);
431 /*******************************************************************************
433 * FUNCTION: acpi_ev_asynch_execute_gpe_method
435 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
439 * DESCRIPTION: Perform the actual execution of a GPE control method. This
440 * function is called from an invocation of acpi_os_execute and
441 * therefore does NOT execute at interrupt level - so that
442 * the control method itself is not executed in the context of
443 * an interrupt handler.
445 ******************************************************************************/
447 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_execute_gpe_method(void *context
)
449 struct acpi_gpe_event_info
*gpe_event_info
= context
;
451 struct acpi_gpe_event_info
*local_gpe_event_info
;
452 struct acpi_evaluate_info
*info
;
454 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method
);
456 /* Allocate a local GPE block */
458 local_gpe_event_info
=
459 ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info
));
460 if (!local_gpe_event_info
) {
461 ACPI_EXCEPTION((AE_INFO
, AE_NO_MEMORY
, "while handling a GPE"));
465 status
= acpi_ut_acquire_mutex(ACPI_MTX_EVENTS
);
466 if (ACPI_FAILURE(status
)) {
470 /* Must revalidate the gpe_number/gpe_block */
472 if (!acpi_ev_valid_gpe_event(gpe_event_info
)) {
473 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
478 * Take a snapshot of the GPE info for this level - we copy the info to
479 * prevent a race condition with remove_handler/remove_block.
481 ACPI_MEMCPY(local_gpe_event_info
, gpe_event_info
,
482 sizeof(struct acpi_gpe_event_info
));
484 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
485 if (ACPI_FAILURE(status
)) {
490 * Must check for control method type dispatch one more time to avoid a
491 * race with ev_gpe_install_handler
493 if ((local_gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) ==
494 ACPI_GPE_DISPATCH_METHOD
) {
496 /* Allocate the evaluation information block */
498 info
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info
));
500 status
= AE_NO_MEMORY
;
503 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
504 * control method that corresponds to this GPE
507 local_gpe_event_info
->dispatch
.method_node
;
508 info
->flags
= ACPI_IGNORE_RETURN_VALUE
;
510 status
= acpi_ns_evaluate(info
);
514 if (ACPI_FAILURE(status
)) {
515 ACPI_EXCEPTION((AE_INFO
, status
,
516 "while evaluating GPE method [%4.4s]",
517 acpi_ut_get_node_name
518 (local_gpe_event_info
->dispatch
.
523 /* Defer enabling of GPE until all notify handlers are done */
525 status
= acpi_os_execute(OSL_NOTIFY_HANDLER
,
526 acpi_ev_asynch_enable_gpe
,
527 local_gpe_event_info
);
528 if (ACPI_FAILURE(status
)) {
529 ACPI_FREE(local_gpe_event_info
);
534 /*******************************************************************************
536 * FUNCTION: acpi_ev_asynch_enable_gpe
538 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
539 * Callback from acpi_os_execute
543 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
546 ******************************************************************************/
548 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_enable_gpe(void *context
)
550 struct acpi_gpe_event_info
*gpe_event_info
= context
;
553 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
554 ACPI_GPE_LEVEL_TRIGGERED
) {
556 * GPE is level-triggered, we clear the GPE status bit after handling
559 status
= acpi_hw_clear_gpe(gpe_event_info
);
560 if (ACPI_FAILURE(status
)) {
566 * Enable this GPE, conditionally. This means that the GPE will only be
567 * physically enabled if the enable_for_run bit is set in the event_info
569 (void)acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_CONDITIONAL_ENABLE
);
572 ACPI_FREE(gpe_event_info
);
576 /*******************************************************************************
578 * FUNCTION: acpi_ev_gpe_dispatch
580 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
581 * gpe_event_info - Info for this GPE
582 * gpe_number - Number relative to the parent GPE block
584 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
586 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
587 * or method (e.g. _Lxx/_Exx) handler.
589 * This function executes at interrupt level.
591 ******************************************************************************/
594 acpi_ev_gpe_dispatch(struct acpi_namespace_node
*gpe_device
,
595 struct acpi_gpe_event_info
*gpe_event_info
, u32 gpe_number
)
599 ACPI_FUNCTION_TRACE(ev_gpe_dispatch
);
601 acpi_os_gpe_count(gpe_number
);
604 * If edge-triggered, clear the GPE status bit now. Note that
605 * level-triggered events are cleared after the GPE is serviced.
607 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
608 ACPI_GPE_EDGE_TRIGGERED
) {
609 status
= acpi_hw_clear_gpe(gpe_event_info
);
610 if (ACPI_FAILURE(status
)) {
611 ACPI_EXCEPTION((AE_INFO
, status
,
612 "Unable to clear GPE[0x%2X]",
614 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
619 * Dispatch the GPE to either an installed handler, or the control method
620 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
621 * it and do not attempt to run the method. If there is neither a handler
622 * nor a method, we disable this GPE to prevent further such pointless
623 * events from firing.
625 switch (gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) {
626 case ACPI_GPE_DISPATCH_HANDLER
:
629 * Invoke the installed handler (at interrupt level)
630 * Ignore return status for now.
631 * TBD: leave GPE disabled on error?
633 (void)gpe_event_info
->dispatch
.handler
->address(gpe_device
,
640 /* It is now safe to clear level-triggered events. */
642 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
643 ACPI_GPE_LEVEL_TRIGGERED
) {
644 status
= acpi_hw_clear_gpe(gpe_event_info
);
645 if (ACPI_FAILURE(status
)) {
646 ACPI_EXCEPTION((AE_INFO
, status
,
647 "Unable to clear GPE[0x%2X]",
649 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
654 case ACPI_GPE_DISPATCH_METHOD
:
657 * Disable the GPE, so it doesn't keep firing before the method has a
658 * chance to run (it runs asynchronously with interrupts enabled).
660 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
661 if (ACPI_FAILURE(status
)) {
662 ACPI_EXCEPTION((AE_INFO
, status
,
663 "Unable to disable GPE[0x%2X]",
665 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
669 * Execute the method associated with the GPE
670 * NOTE: Level-triggered GPEs are cleared after the method completes.
672 status
= acpi_os_execute(OSL_GPE_HANDLER
,
673 acpi_ev_asynch_execute_gpe_method
,
675 if (ACPI_FAILURE(status
)) {
676 ACPI_EXCEPTION((AE_INFO
, status
,
677 "Unable to queue handler for GPE[0x%2X] - event disabled",
685 * No handler or method to run!
686 * 03/2010: This case should no longer be possible. We will not allow
687 * a GPE to be enabled if it has no handler or method.
690 "No handler or method for GPE[0x%2X], disabling event",
694 * Disable the GPE. The GPE will remain disabled a handler
695 * is installed or ACPICA is restarted.
697 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
698 if (ACPI_FAILURE(status
)) {
699 ACPI_EXCEPTION((AE_INFO
, status
,
700 "Unable to disable GPE[0x%2X]",
702 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
707 return_UINT32(ACPI_INTERRUPT_HANDLED
);