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_masks
59 * PARAMETERS: gpe_event_info - GPE to update
63 * DESCRIPTION: Updates GPE register enable masks based on the GPE type
65 ******************************************************************************/
68 acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info
*gpe_event_info
)
70 struct acpi_gpe_register_info
*gpe_register_info
;
73 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks
);
75 gpe_register_info
= gpe_event_info
->register_info
;
76 if (!gpe_register_info
) {
77 return_ACPI_STATUS(AE_NOT_EXIST
);
80 register_bit
= acpi_hw_gpe_register_bit(gpe_event_info
,
83 ACPI_CLEAR_BIT(gpe_register_info
->enable_for_wake
, register_bit
);
84 ACPI_CLEAR_BIT(gpe_register_info
->enable_for_run
, register_bit
);
86 if (gpe_event_info
->runtime_count
)
87 ACPI_SET_BIT(gpe_register_info
->enable_for_run
, register_bit
);
89 if (gpe_event_info
->wakeup_count
)
90 ACPI_SET_BIT(gpe_register_info
->enable_for_wake
, register_bit
);
92 return_ACPI_STATUS(AE_OK
);
95 /*******************************************************************************
97 * FUNCTION: acpi_ev_get_gpe_event_info
99 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
100 * gpe_number - Raw GPE number
102 * RETURN: A GPE event_info struct. NULL if not a valid GPE
104 * DESCRIPTION: Returns the event_info struct associated with this GPE.
105 * Validates the gpe_block and the gpe_number
107 * Should be called only when the GPE lists are semaphore locked
108 * and not subject to change.
110 ******************************************************************************/
112 struct acpi_gpe_event_info
*acpi_ev_get_gpe_event_info(acpi_handle gpe_device
,
115 union acpi_operand_object
*obj_desc
;
116 struct acpi_gpe_block_info
*gpe_block
;
119 ACPI_FUNCTION_ENTRY();
121 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
125 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
127 for (i
= 0; i
< ACPI_MAX_GPE_BLOCKS
; i
++) {
128 gpe_block
= acpi_gbl_gpe_fadt_blocks
[i
];
130 if ((gpe_number
>= gpe_block
->block_base_number
)
132 gpe_block
->block_base_number
+
133 (gpe_block
->register_count
* 8))) {
135 event_info
[gpe_number
-
142 /* The gpe_number was not in the range of either FADT GPE block */
147 /* A Non-NULL gpe_device means this is a GPE Block Device */
149 obj_desc
= acpi_ns_get_attached_object((struct acpi_namespace_node
*)
151 if (!obj_desc
|| !obj_desc
->device
.gpe_block
) {
155 gpe_block
= obj_desc
->device
.gpe_block
;
157 if ((gpe_number
>= gpe_block
->block_base_number
) &&
159 gpe_block
->block_base_number
+ (gpe_block
->register_count
* 8))) {
161 event_info
[gpe_number
- gpe_block
->block_base_number
]);
167 /*******************************************************************************
169 * FUNCTION: acpi_ev_gpe_detect
171 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
172 * Can have multiple GPE blocks attached.
174 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
176 * DESCRIPTION: Detect if any GP events have occurred. This function is
177 * executed at interrupt level.
179 ******************************************************************************/
181 u32
acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info
* gpe_xrupt_list
)
184 struct acpi_gpe_block_info
*gpe_block
;
185 struct acpi_gpe_register_info
*gpe_register_info
;
186 u32 int_status
= ACPI_INTERRUPT_NOT_HANDLED
;
187 u8 enabled_status_byte
;
190 acpi_cpu_flags flags
;
194 ACPI_FUNCTION_NAME(ev_gpe_detect
);
196 /* Check for the case where there are no GPEs */
198 if (!gpe_xrupt_list
) {
203 * We need to obtain the GPE lock for both the data structs and registers
204 * Note: Not necessary to obtain the hardware lock, since the GPE
205 * registers are owned by the gpe_lock.
207 flags
= acpi_os_acquire_lock(acpi_gbl_gpe_lock
);
209 /* Examine all GPE blocks attached to this interrupt level */
211 gpe_block
= gpe_xrupt_list
->gpe_block_list_head
;
214 * Read all of the 8-bit GPE status and enable registers in this GPE
215 * block, saving all of them. Find all currently active GP events.
217 for (i
= 0; i
< gpe_block
->register_count
; i
++) {
219 /* Get the next status/enable pair */
221 gpe_register_info
= &gpe_block
->register_info
[i
];
223 /* Read the Status Register */
226 acpi_hw_read(&status_reg
,
227 &gpe_register_info
->status_address
);
228 if (ACPI_FAILURE(status
)) {
229 goto unlock_and_exit
;
232 /* Read the Enable Register */
235 acpi_hw_read(&enable_reg
,
236 &gpe_register_info
->enable_address
);
237 if (ACPI_FAILURE(status
)) {
238 goto unlock_and_exit
;
241 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS
,
242 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
243 gpe_register_info
->base_gpe_number
,
244 status_reg
, enable_reg
));
246 /* Check if there is anything active at all in this register */
248 enabled_status_byte
= (u8
) (status_reg
& enable_reg
);
249 if (!enabled_status_byte
) {
251 /* No active GPEs in this register, move on */
256 /* Now look at the individual GPEs in this byte register */
258 for (j
= 0; j
< ACPI_GPE_REGISTER_WIDTH
; j
++) {
260 /* Examine one GPE bit */
262 if (enabled_status_byte
& (1 << j
)) {
264 * Found an active GPE. Dispatch the event to a handler
268 acpi_ev_gpe_dispatch(&gpe_block
->
269 event_info
[((acpi_size
) i
* ACPI_GPE_REGISTER_WIDTH
) + j
], j
+ gpe_register_info
->base_gpe_number
);
274 gpe_block
= gpe_block
->next
;
279 acpi_os_release_lock(acpi_gbl_gpe_lock
, flags
);
283 /*******************************************************************************
285 * FUNCTION: acpi_ev_asynch_execute_gpe_method
287 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
291 * DESCRIPTION: Perform the actual execution of a GPE control method. This
292 * function is called from an invocation of acpi_os_execute and
293 * therefore does NOT execute at interrupt level - so that
294 * the control method itself is not executed in the context of
295 * an interrupt handler.
297 ******************************************************************************/
298 static void acpi_ev_asynch_enable_gpe(void *context
);
300 static void ACPI_SYSTEM_XFACE
acpi_ev_asynch_execute_gpe_method(void *context
)
302 struct acpi_gpe_event_info
*gpe_event_info
= (void *)context
;
304 struct acpi_gpe_event_info local_gpe_event_info
;
305 struct acpi_evaluate_info
*info
;
307 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method
);
309 status
= acpi_ut_acquire_mutex(ACPI_MTX_EVENTS
);
310 if (ACPI_FAILURE(status
)) {
314 /* Must revalidate the gpe_number/gpe_block */
316 if (!acpi_ev_valid_gpe_event(gpe_event_info
)) {
317 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
322 * Take a snapshot of the GPE info for this level - we copy the info to
323 * prevent a race condition with remove_handler/remove_block.
325 ACPI_MEMCPY(&local_gpe_event_info
, gpe_event_info
,
326 sizeof(struct acpi_gpe_event_info
));
328 status
= acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
329 if (ACPI_FAILURE(status
)) {
334 * Must check for control method type dispatch one more time to avoid a
335 * race with ev_gpe_install_handler
337 if ((local_gpe_event_info
.flags
& ACPI_GPE_DISPATCH_MASK
) ==
338 ACPI_GPE_DISPATCH_METHOD
) {
340 /* Allocate the evaluation information block */
342 info
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info
));
344 status
= AE_NO_MEMORY
;
347 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
348 * control method that corresponds to this GPE
351 local_gpe_event_info
.dispatch
.method_node
;
352 info
->flags
= ACPI_IGNORE_RETURN_VALUE
;
354 status
= acpi_ns_evaluate(info
);
358 if (ACPI_FAILURE(status
)) {
359 ACPI_EXCEPTION((AE_INFO
, status
,
360 "while evaluating GPE method [%4.4s]",
361 acpi_ut_get_node_name
362 (local_gpe_event_info
.dispatch
.
366 /* Defer enabling of GPE until all notify handlers are done */
367 acpi_os_execute(OSL_NOTIFY_HANDLER
, acpi_ev_asynch_enable_gpe
,
372 static void acpi_ev_asynch_enable_gpe(void *context
)
374 struct acpi_gpe_event_info
*gpe_event_info
= context
;
376 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
377 ACPI_GPE_LEVEL_TRIGGERED
) {
379 * GPE is level-triggered, we clear the GPE status bit after handling
382 status
= acpi_hw_clear_gpe(gpe_event_info
);
383 if (ACPI_FAILURE(status
)) {
388 /* Enable this GPE */
389 (void)acpi_hw_write_gpe_enable_reg(gpe_event_info
);
393 /*******************************************************************************
395 * FUNCTION: acpi_ev_gpe_dispatch
397 * PARAMETERS: gpe_event_info - Info for this GPE
398 * gpe_number - Number relative to the parent GPE block
400 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
402 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
403 * or method (e.g. _Lxx/_Exx) handler.
405 * This function executes at interrupt level.
407 ******************************************************************************/
410 acpi_ev_gpe_dispatch(struct acpi_gpe_event_info
*gpe_event_info
, u32 gpe_number
)
414 ACPI_FUNCTION_TRACE(ev_gpe_dispatch
);
416 acpi_os_gpe_count(gpe_number
);
419 * If edge-triggered, clear the GPE status bit now. Note that
420 * level-triggered events are cleared after the GPE is serviced.
422 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
423 ACPI_GPE_EDGE_TRIGGERED
) {
424 status
= acpi_hw_clear_gpe(gpe_event_info
);
425 if (ACPI_FAILURE(status
)) {
426 ACPI_EXCEPTION((AE_INFO
, status
,
427 "Unable to clear GPE[%2X]",
429 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
434 * Dispatch the GPE to either an installed handler, or the control method
435 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
436 * it and do not attempt to run the method. If there is neither a handler
437 * nor a method, we disable this GPE to prevent further such pointless
438 * events from firing.
440 switch (gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) {
441 case ACPI_GPE_DISPATCH_HANDLER
:
444 * Invoke the installed handler (at interrupt level)
445 * Ignore return status for now.
446 * TBD: leave GPE disabled on error?
448 (void)gpe_event_info
->dispatch
.handler
->address(gpe_event_info
->
453 /* It is now safe to clear level-triggered events. */
455 if ((gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
) ==
456 ACPI_GPE_LEVEL_TRIGGERED
) {
457 status
= acpi_hw_clear_gpe(gpe_event_info
);
458 if (ACPI_FAILURE(status
)) {
459 ACPI_EXCEPTION((AE_INFO
, status
,
460 "Unable to clear GPE[%2X]",
462 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
467 case ACPI_GPE_DISPATCH_METHOD
:
470 * Disable the GPE, so it doesn't keep firing before the method has a
471 * chance to run (it runs asynchronously with interrupts enabled).
473 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
474 if (ACPI_FAILURE(status
)) {
475 ACPI_EXCEPTION((AE_INFO
, status
,
476 "Unable to disable GPE[%2X]",
478 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
482 * Execute the method associated with the GPE
483 * NOTE: Level-triggered GPEs are cleared after the method completes.
485 status
= acpi_os_execute(OSL_GPE_HANDLER
,
486 acpi_ev_asynch_execute_gpe_method
,
488 if (ACPI_FAILURE(status
)) {
489 ACPI_EXCEPTION((AE_INFO
, status
,
490 "Unable to queue handler for GPE[%2X] - event disabled",
497 /* No handler or method to run! */
500 "No handler or method for GPE[%2X], disabling event",
504 * Disable the GPE. The GPE will remain disabled until the ACPICA
505 * Core Subsystem is restarted, or a handler is installed.
507 status
= acpi_hw_low_set_gpe(gpe_event_info
, ACPI_GPE_DISABLE
);
508 if (ACPI_FAILURE(status
)) {
509 ACPI_EXCEPTION((AE_INFO
, status
,
510 "Unable to disable GPE[%2X]",
512 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED
);
517 return_UINT32(ACPI_INTERRUPT_HANDLED
);