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 /*******************************************************************************
57 * FUNCTION: acpi_ev_update_gpe_enable_mask
59 * PARAMETERS: gpe_event_info - GPE to update
63 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
64 * runtime references to this GPE
66 ******************************************************************************/
69 acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info
*gpe_event_info
)
71 struct acpi_gpe_register_info
*gpe_register_info
;
74 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask
);
76 gpe_register_info
= gpe_event_info
->register_info
;
77 if (!gpe_register_info
) {
78 return_ACPI_STATUS(AE_NOT_EXIST
);
81 register_bit
= acpi_hw_get_gpe_register_bit(gpe_event_info
,
84 /* Clear the run bit up front */
86 ACPI_CLEAR_BIT(gpe_register_info
->enable_for_run
, register_bit
);
88 /* Set the mask bit only if there are references to this GPE */
90 if (gpe_event_info
->runtime_count
) {
91 ACPI_SET_BIT(gpe_register_info
->enable_for_run
, (u8
)register_bit
);
94 return_ACPI_STATUS(AE_OK
);
97 /*******************************************************************************
99 * FUNCTION: acpi_ev_enable_gpe
101 * PARAMETERS: gpe_event_info - GPE to enable
105 * DESCRIPTION: Clear the given GPE from stale events and enable it.
107 ******************************************************************************/
109 acpi_ev_enable_gpe(struct acpi_gpe_event_info
*gpe_event_info
)
113 ACPI_FUNCTION_TRACE(ev_enable_gpe
);
116 * We will only allow a GPE to be enabled if it has either an
117 * associated method (_Lxx/_Exx) or a handler. Otherwise, the
118 * GPE will be immediately disabled by acpi_ev_gpe_dispatch the
119 * first time it fires.
121 if (!(gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
)) {
122 return_ACPI_STATUS(AE_NO_HANDLER
);
125 /* Clear the GPE (of stale events) */
126 status
= acpi_hw_clear_gpe(gpe_event_info
);
127 if (ACPI_FAILURE(status
)) {
128 return_ACPI_STATUS(status
);
131 /* Enable the requested GPE */
132 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_ENABLE
);
134 return_ACPI_STATUS(status
);
138 /*******************************************************************************
140 * FUNCTION: acpi_ev_add_gpe_reference
142 * PARAMETERS: gpe_event_info - GPE to enable
146 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
149 ******************************************************************************/
151 acpi_status
acpi_ev_add_gpe_reference(struct acpi_gpe_event_info
*gpe_event_info
)
153 acpi_status status
= AE_OK
;
155 ACPI_FUNCTION_TRACE(ev_add_gpe_reference
);
157 if (gpe_event_info
->runtime_count
== ACPI_UINT8_MAX
) {
158 return_ACPI_STATUS(AE_LIMIT
);
161 gpe_event_info
->runtime_count
++;
162 if (gpe_event_info
->runtime_count
== 1) {
163 status
= acpi_ev_update_gpe_enable_mask(gpe_event_info
);
164 if (ACPI_SUCCESS(status
)) {
165 status
= acpi_ev_enable_gpe(gpe_event_info
);
168 if (ACPI_FAILURE(status
)) {
169 gpe_event_info
->runtime_count
--;
173 return_ACPI_STATUS(status
);
176 /*******************************************************************************
178 * FUNCTION: acpi_ev_remove_gpe_reference
180 * PARAMETERS: gpe_event_info - GPE to disable
184 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
185 * removed, the GPE is hardware-disabled.
187 ******************************************************************************/
189 acpi_status
acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info
*gpe_event_info
)
191 acpi_status status
= AE_OK
;
193 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference
);
195 if (!gpe_event_info
->runtime_count
) {
196 return_ACPI_STATUS(AE_LIMIT
);
199 gpe_event_info
->runtime_count
--;
200 if (!gpe_event_info
->runtime_count
) {
201 status
= acpi_ev_update_gpe_enable_mask(gpe_event_info
);
202 if (ACPI_SUCCESS(status
)) {
203 status
= acpi_hw_low_set_gpe(gpe_event_info
,
207 if (ACPI_FAILURE(status
)) {
208 gpe_event_info
->runtime_count
++;
212 return_ACPI_STATUS(status
);
215 /*******************************************************************************
217 * FUNCTION: acpi_ev_low_get_gpe_info
219 * PARAMETERS: gpe_number - Raw GPE number
220 * gpe_block - A GPE info block
222 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
223 * is not within the specified GPE block)
225 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
226 * the low-level implementation of ev_get_gpe_event_info.
228 ******************************************************************************/
230 struct acpi_gpe_event_info
*acpi_ev_low_get_gpe_info(u32 gpe_number
,
231 struct acpi_gpe_block_info
237 * Validate that the gpe_number is within the specified gpe_block.
240 if (!gpe_block
|| (gpe_number
< gpe_block
->block_base_number
)) {
244 gpe_index
= gpe_number
- gpe_block
->block_base_number
;
245 if (gpe_index
>= gpe_block
->gpe_count
) {
249 return (&gpe_block
->event_info
[gpe_index
]);
253 /*******************************************************************************
255 * FUNCTION: acpi_ev_get_gpe_event_info
257 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
258 * gpe_number - Raw GPE number
260 * RETURN: A GPE event_info struct. NULL if not a valid GPE
262 * DESCRIPTION: Returns the event_info struct associated with this GPE.
263 * Validates the gpe_block and the gpe_number
265 * Should be called only when the GPE lists are semaphore locked
266 * and not subject to change.
268 ******************************************************************************/
270 struct acpi_gpe_event_info
*acpi_ev_get_gpe_event_info(acpi_handle gpe_device
,
273 union acpi_operand_object
*obj_desc
;
274 struct acpi_gpe_event_info
*gpe_info
;
277 ACPI_FUNCTION_ENTRY();
279 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
283 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
285 for (i
= 0; i
< ACPI_MAX_GPE_BLOCKS
; i
++) {
286 gpe_info
= acpi_ev_low_get_gpe_info(gpe_number
,
287 acpi_gbl_gpe_fadt_blocks
294 /* The gpe_number was not in the range of either FADT GPE block */
299 /* A Non-NULL gpe_device means this is a GPE Block Device */
301 obj_desc
= acpi_ns_get_attached_object((struct acpi_namespace_node
*)
303 if (!obj_desc
|| !obj_desc
->device
.gpe_block
) {
307 return (acpi_ev_low_get_gpe_info
308 (gpe_number
, obj_desc
->device
.gpe_block
));
311 /*******************************************************************************
313 * FUNCTION: acpi_ev_gpe_detect
315 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
316 * Can have multiple GPE blocks attached.
318 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
320 * DESCRIPTION: Detect if any GP events have occurred. This function is
321 * executed at interrupt level.
323 ******************************************************************************/
325 u32
acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info
* gpe_xrupt_list
)
328 struct acpi_gpe_block_info
*gpe_block
;
329 struct acpi_gpe_register_info
*gpe_register_info
;
330 u32 int_status
= ACPI_INTERRUPT_NOT_HANDLED
;
331 u8 enabled_status_byte
;
334 acpi_cpu_flags flags
;
338 ACPI_FUNCTION_NAME(ev_gpe_detect
);
340 /* Check for the case where there are no GPEs */
342 if (!gpe_xrupt_list
) {
347 * We need to obtain the GPE lock for both the data structs and registers
348 * Note: Not necessary to obtain the hardware lock, since the GPE
349 * registers are owned by the gpe_lock.
351 flags
= acpi_os_acquire_lock(acpi_gbl_gpe_lock
);
353 /* Examine all GPE blocks attached to this interrupt level */
355 gpe_block
= gpe_xrupt_list
->gpe_block_list_head
;
358 * Read all of the 8-bit GPE status and enable registers in this GPE
359 * block, saving all of them. Find all currently active GP events.
361 for (i
= 0; i
< gpe_block
->register_count
; i
++) {
363 /* Get the next status/enable pair */
365 gpe_register_info
= &gpe_block
->register_info
[i
];
367 /* Read the Status Register */
370 acpi_hw_read(&status_reg
,
371 &gpe_register_info
->status_address
);
372 if (ACPI_FAILURE(status
)) {
373 goto unlock_and_exit
;
376 /* Read the Enable Register */
379 acpi_hw_read(&enable_reg
,
380 &gpe_register_info
->enable_address
);
381 if (ACPI_FAILURE(status
)) {
382 goto unlock_and_exit
;
385 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS
,
386 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
387 gpe_register_info
->base_gpe_number
,
388 status_reg
, enable_reg
));
390 /* Check if there is anything active at all in this register */
392 enabled_status_byte
= (u8
) (status_reg
& enable_reg
);
393 if (!enabled_status_byte
) {
395 /* No active GPEs in this register, move on */
400 /* Now look at the individual GPEs in this byte register */
402 for (j
= 0; j
< ACPI_GPE_REGISTER_WIDTH
; j
++) {
404 /* Examine one GPE bit */
406 if (enabled_status_byte
& (1 << j
)) {
408 * Found an active GPE. Dispatch the event to a handler
412 acpi_ev_gpe_dispatch(&gpe_block
->
413 event_info
[((acpi_size
) i
* ACPI_GPE_REGISTER_WIDTH
) + j
], j
+ gpe_register_info
->base_gpe_number
);
418 gpe_block
= gpe_block
->next
;
423 acpi_os_release_lock(acpi_gbl_gpe_lock
, flags
);
427 /*******************************************************************************
429 * FUNCTION: acpi_ev_asynch_execute_gpe_method
431 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
435 * DESCRIPTION: Perform the actual execution of a GPE control method. This
436 * function is called from an invocation of acpi_os_execute and
437 * therefore does NOT execute at interrupt level - so that
438 * the control method itself is not executed in the context of
439 * an interrupt handler.
441 ******************************************************************************/
442 static void acpi_ev_asynch_enable_gpe(void *context
);
444 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_execute_gpe_method(void *context
)
446 struct acpi_gpe_event_info
*gpe_event_info
= (void *)context
;
448 struct acpi_gpe_event_info local_gpe_event_info
;
449 struct acpi_evaluate_info
*info
;
451 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method
);
453 status
= acpi_ut_acquire_mutex(ACPI_MTX_EVENTS
);
454 if (ACPI_FAILURE(status
)) {
458 /* Must revalidate the gpe_number/gpe_block */
460 if (!acpi_ev_valid_gpe_event(gpe_event_info
)) {
461 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
466 * Take a snapshot of the GPE info for this level - we copy the info to
467 * prevent a race condition with remove_handler/remove_block.
469 ACPI_MEMCPY(&local_gpe_event_info
, gpe_event_info
,
470 sizeof(struct acpi_gpe_event_info
));
472 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
473 if (ACPI_FAILURE(status
)) {
478 * Must check for control method type dispatch one more time to avoid a
479 * race with ev_gpe_install_handler
481 if ((local_gpe_event_info
.flags
& ACPI_GPE_DISPATCH_MASK
) ==
482 ACPI_GPE_DISPATCH_METHOD
) {
484 /* Allocate the evaluation information block */
486 info
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info
));
488 status
= AE_NO_MEMORY
;
491 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
492 * control method that corresponds to this GPE
495 local_gpe_event_info
.dispatch
.method_node
;
496 info
->flags
= ACPI_IGNORE_RETURN_VALUE
;
498 status
= acpi_ns_evaluate(info
);
502 if (ACPI_FAILURE(status
)) {
503 ACPI_EXCEPTION((AE_INFO
, status
,
504 "while evaluating GPE method [%4.4s]",
505 acpi_ut_get_node_name
506 (local_gpe_event_info
.dispatch
.
510 /* Defer enabling of GPE until all notify handlers are done */
511 acpi_os_execute(OSL_NOTIFY_HANDLER
, acpi_ev_asynch_enable_gpe
,
516 static void acpi_ev_asynch_enable_gpe(void *context
)
518 struct acpi_gpe_event_info
*gpe_event_info
= context
;
520 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
521 ACPI_GPE_LEVEL_TRIGGERED
) {
523 * GPE is level-triggered, we clear the GPE status bit after handling
526 status
= acpi_hw_clear_gpe(gpe_event_info
);
527 if (ACPI_FAILURE(status
)) {
533 * Enable this GPE, conditionally. This means that the GPE will only be
534 * physically enabled if the enable_for_run bit is set in the event_info
536 (void)acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_CONDITIONAL_ENABLE
);
541 /*******************************************************************************
543 * FUNCTION: acpi_ev_gpe_dispatch
545 * PARAMETERS: gpe_event_info - Info for this GPE
546 * gpe_number - Number relative to the parent GPE block
548 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
550 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
551 * or method (e.g. _Lxx/_Exx) handler.
553 * This function executes at interrupt level.
555 ******************************************************************************/
558 acpi_ev_gpe_dispatch(struct acpi_gpe_event_info
*gpe_event_info
, u32 gpe_number
)
562 ACPI_FUNCTION_TRACE(ev_gpe_dispatch
);
564 acpi_os_gpe_count(gpe_number
);
567 * If edge-triggered, clear the GPE status bit now. Note that
568 * level-triggered events are cleared after the GPE is serviced.
570 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
571 ACPI_GPE_EDGE_TRIGGERED
) {
572 status
= acpi_hw_clear_gpe(gpe_event_info
);
573 if (ACPI_FAILURE(status
)) {
574 ACPI_EXCEPTION((AE_INFO
, status
,
575 "Unable to clear GPE[0x%2X]",
577 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
582 * Dispatch the GPE to either an installed handler, or the control method
583 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
584 * it and do not attempt to run the method. If there is neither a handler
585 * nor a method, we disable this GPE to prevent further such pointless
586 * events from firing.
588 switch (gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) {
589 case ACPI_GPE_DISPATCH_HANDLER
:
592 * Invoke the installed handler (at interrupt level)
593 * Ignore return status for now.
594 * TBD: leave GPE disabled on error?
596 (void)gpe_event_info
->dispatch
.handler
->address(gpe_event_info
->
601 /* It is now safe to clear level-triggered events. */
603 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
604 ACPI_GPE_LEVEL_TRIGGERED
) {
605 status
= acpi_hw_clear_gpe(gpe_event_info
);
606 if (ACPI_FAILURE(status
)) {
607 ACPI_EXCEPTION((AE_INFO
, status
,
608 "Unable to clear GPE[0x%2X]",
610 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
615 case ACPI_GPE_DISPATCH_METHOD
:
618 * Disable the GPE, so it doesn't keep firing before the method has a
619 * chance to run (it runs asynchronously with interrupts enabled).
621 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
622 if (ACPI_FAILURE(status
)) {
623 ACPI_EXCEPTION((AE_INFO
, status
,
624 "Unable to disable GPE[0x%2X]",
626 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
630 * Execute the method associated with the GPE
631 * NOTE: Level-triggered GPEs are cleared after the method completes.
633 status
= acpi_os_execute(OSL_GPE_HANDLER
,
634 acpi_ev_asynch_execute_gpe_method
,
636 if (ACPI_FAILURE(status
)) {
637 ACPI_EXCEPTION((AE_INFO
, status
,
638 "Unable to queue handler for GPE[0x%2X] - event disabled",
646 * No handler or method to run!
647 * 03/2010: This case should no longer be possible. We will not allow
648 * a GPE to be enabled if it has no handler or method.
651 "No handler or method for GPE[0x%2X], disabling event",
655 * Disable the GPE. The GPE will remain disabled a handler
656 * is installed or ACPICA is restarted.
658 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
659 if (ACPI_FAILURE(status
)) {
660 ACPI_EXCEPTION((AE_INFO
, status
,
661 "Unable to disable GPE[0x%2X]",
663 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
668 return_UINT32(ACPI_INTERRUPT_HANDLED
);