1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2008, 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);
82 (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
84 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
85 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
87 if (gpe_event_info->runtime_count)
88 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
90 if (gpe_event_info->wakeup_count)
91 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
93 return_ACPI_STATUS(AE_OK);
96 /*******************************************************************************
98 * FUNCTION: acpi_ev_enable_gpe
100 * PARAMETERS: gpe_event_info - GPE to enable
101 * write_to_hardware - Enable now, or just mark data structs
102 * (WAKE GPEs should be deferred)
106 * DESCRIPTION: Enable a GPE based on the GPE type
108 ******************************************************************************/
111 acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
112 u8 write_to_hardware)
116 ACPI_FUNCTION_TRACE(ev_enable_gpe);
118 /* Make sure HW enable masks are updated */
120 status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
121 if (ACPI_FAILURE(status))
122 return_ACPI_STATUS(status);
124 /* Mark wake-enabled or HW enable, or both */
126 if (gpe_event_info->runtime_count && write_to_hardware) {
127 /* Clear the GPE (of stale events), then enable it */
128 status = acpi_hw_clear_gpe(gpe_event_info);
129 if (ACPI_FAILURE(status))
130 return_ACPI_STATUS(status);
132 /* Enable the requested runtime GPE */
133 status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
136 return_ACPI_STATUS(AE_OK);
139 /*******************************************************************************
141 * FUNCTION: acpi_ev_disable_gpe
143 * PARAMETERS: gpe_event_info - GPE to disable
147 * DESCRIPTION: Disable a GPE based on the GPE type
149 ******************************************************************************/
151 acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
155 ACPI_FUNCTION_TRACE(ev_disable_gpe);
157 /* Make sure HW enable masks are updated */
159 status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
160 if (ACPI_FAILURE(status))
161 return_ACPI_STATUS(status);
164 * Even if we don't know the GPE type, make sure that we always
165 * disable it. low_disable_gpe will just clear the enable bit for this
166 * GPE and write it. It will not write out the current GPE enable mask,
167 * since this may inadvertently enable GPEs too early, if a rogue GPE has
168 * come in during ACPICA initialization - possibly as a result of AML or
169 * other code that has enabled the GPE.
171 status = acpi_hw_low_disable_gpe(gpe_event_info);
172 return_ACPI_STATUS(status);
175 /*******************************************************************************
177 * FUNCTION: acpi_ev_get_gpe_event_info
179 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
180 * gpe_number - Raw GPE number
182 * RETURN: A GPE event_info struct. NULL if not a valid GPE
184 * DESCRIPTION: Returns the event_info struct associated with this GPE.
185 * Validates the gpe_block and the gpe_number
187 * Should be called only when the GPE lists are semaphore locked
188 * and not subject to change.
190 ******************************************************************************/
192 struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
195 union acpi_operand_object *obj_desc;
196 struct acpi_gpe_block_info *gpe_block;
199 ACPI_FUNCTION_ENTRY();
201 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
205 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
207 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
208 gpe_block = acpi_gbl_gpe_fadt_blocks[i];
210 if ((gpe_number >= gpe_block->block_base_number)
212 gpe_block->block_base_number +
213 (gpe_block->register_count * 8))) {
215 event_info[gpe_number -
222 /* The gpe_number was not in the range of either FADT GPE block */
227 /* A Non-NULL gpe_device means this is a GPE Block Device */
229 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
231 if (!obj_desc || !obj_desc->device.gpe_block) {
235 gpe_block = obj_desc->device.gpe_block;
237 if ((gpe_number >= gpe_block->block_base_number) &&
239 gpe_block->block_base_number + (gpe_block->register_count * 8))) {
241 event_info[gpe_number - gpe_block->block_base_number]);
247 /*******************************************************************************
249 * FUNCTION: acpi_ev_gpe_detect
251 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
252 * Can have multiple GPE blocks attached.
254 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
256 * DESCRIPTION: Detect if any GP events have occurred. This function is
257 * executed at interrupt level.
259 ******************************************************************************/
261 u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
264 struct acpi_gpe_block_info *gpe_block;
265 struct acpi_gpe_register_info *gpe_register_info;
266 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
267 u8 enabled_status_byte;
270 acpi_cpu_flags flags;
274 ACPI_FUNCTION_NAME(ev_gpe_detect);
276 /* Check for the case where there are no GPEs */
278 if (!gpe_xrupt_list) {
283 * We need to obtain the GPE lock for both the data structs and registers
284 * Note: Not necessary to obtain the hardware lock, since the GPE
285 * registers are owned by the gpe_lock.
287 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
289 /* Examine all GPE blocks attached to this interrupt level */
291 gpe_block = gpe_xrupt_list->gpe_block_list_head;
294 * Read all of the 8-bit GPE status and enable registers in this GPE
295 * block, saving all of them. Find all currently active GP events.
297 for (i = 0; i < gpe_block->register_count; i++) {
299 /* Get the next status/enable pair */
301 gpe_register_info = &gpe_block->register_info[i];
303 /* Read the Status Register */
306 acpi_hw_read(&status_reg,
307 &gpe_register_info->status_address);
308 if (ACPI_FAILURE(status)) {
309 goto unlock_and_exit;
312 /* Read the Enable Register */
315 acpi_hw_read(&enable_reg,
316 &gpe_register_info->enable_address);
317 if (ACPI_FAILURE(status)) {
318 goto unlock_and_exit;
321 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
322 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
323 gpe_register_info->base_gpe_number,
324 status_reg, enable_reg));
326 /* Check if there is anything active at all in this register */
328 enabled_status_byte = (u8) (status_reg & enable_reg);
329 if (!enabled_status_byte) {
331 /* No active GPEs in this register, move on */
336 /* Now look at the individual GPEs in this byte register */
338 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
340 /* Examine one GPE bit */
342 if (enabled_status_byte & (1 << j)) {
344 * Found an active GPE. Dispatch the event to a handler
348 acpi_ev_gpe_dispatch(&gpe_block->
349 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
354 gpe_block = gpe_block->next;
359 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
363 /*******************************************************************************
365 * FUNCTION: acpi_ev_asynch_execute_gpe_method
367 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
371 * DESCRIPTION: Perform the actual execution of a GPE control method. This
372 * function is called from an invocation of acpi_os_execute and
373 * therefore does NOT execute at interrupt level - so that
374 * the control method itself is not executed in the context of
375 * an interrupt handler.
377 ******************************************************************************/
378 static void acpi_ev_asynch_enable_gpe(void *context);
380 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
382 struct acpi_gpe_event_info *gpe_event_info = (void *)context;
384 struct acpi_gpe_event_info local_gpe_event_info;
385 struct acpi_evaluate_info *info;
387 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
389 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
390 if (ACPI_FAILURE(status)) {
394 /* Must revalidate the gpe_number/gpe_block */
396 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
397 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
401 /* Set the GPE flags for return to enabled state */
403 (void)acpi_ev_enable_gpe(gpe_event_info, FALSE);
406 * Take a snapshot of the GPE info for this level - we copy the info to
407 * prevent a race condition with remove_handler/remove_block.
409 ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
410 sizeof(struct acpi_gpe_event_info));
412 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
413 if (ACPI_FAILURE(status)) {
418 * Must check for control method type dispatch one more time to avoid a
419 * race with ev_gpe_install_handler
421 if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
422 ACPI_GPE_DISPATCH_METHOD) {
424 /* Allocate the evaluation information block */
426 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
428 status = AE_NO_MEMORY;
431 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
432 * control method that corresponds to this GPE
435 local_gpe_event_info.dispatch.method_node;
436 info->flags = ACPI_IGNORE_RETURN_VALUE;
438 status = acpi_ns_evaluate(info);
442 if (ACPI_FAILURE(status)) {
443 ACPI_EXCEPTION((AE_INFO, status,
444 "while evaluating GPE method [%4.4s]",
445 acpi_ut_get_node_name
446 (local_gpe_event_info.dispatch.
450 /* Defer enabling of GPE until all notify handlers are done */
451 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
456 static void acpi_ev_asynch_enable_gpe(void *context)
458 struct acpi_gpe_event_info *gpe_event_info = context;
460 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
461 ACPI_GPE_LEVEL_TRIGGERED) {
463 * GPE is level-triggered, we clear the GPE status bit after handling
466 status = acpi_hw_clear_gpe(gpe_event_info);
467 if (ACPI_FAILURE(status)) {
472 /* Enable this GPE */
473 (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
477 /*******************************************************************************
479 * FUNCTION: acpi_ev_gpe_dispatch
481 * PARAMETERS: gpe_event_info - Info for this GPE
482 * gpe_number - Number relative to the parent GPE block
484 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
486 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
487 * or method (e.g. _Lxx/_Exx) handler.
489 * This function executes at interrupt level.
491 ******************************************************************************/
494 acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
498 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
500 acpi_os_gpe_count(gpe_number);
503 * If edge-triggered, clear the GPE status bit now. Note that
504 * level-triggered events are cleared after the GPE is serviced.
506 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
507 ACPI_GPE_EDGE_TRIGGERED) {
508 status = acpi_hw_clear_gpe(gpe_event_info);
509 if (ACPI_FAILURE(status)) {
510 ACPI_EXCEPTION((AE_INFO, status,
511 "Unable to clear GPE[%2X]",
513 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
518 * Dispatch the GPE to either an installed handler, or the control method
519 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
520 * it and do not attempt to run the method. If there is neither a handler
521 * nor a method, we disable this GPE to prevent further such pointless
522 * events from firing.
524 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
525 case ACPI_GPE_DISPATCH_HANDLER:
528 * Invoke the installed handler (at interrupt level)
529 * Ignore return status for now.
530 * TBD: leave GPE disabled on error?
532 (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
537 /* It is now safe to clear level-triggered events. */
539 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
540 ACPI_GPE_LEVEL_TRIGGERED) {
541 status = acpi_hw_clear_gpe(gpe_event_info);
542 if (ACPI_FAILURE(status)) {
543 ACPI_EXCEPTION((AE_INFO, status,
544 "Unable to clear GPE[%2X]",
546 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
551 case ACPI_GPE_DISPATCH_METHOD:
554 * Disable the GPE, so it doesn't keep firing before the method has a
555 * chance to run (it runs asynchronously with interrupts enabled).
557 status = acpi_ev_disable_gpe(gpe_event_info);
558 if (ACPI_FAILURE(status)) {
559 ACPI_EXCEPTION((AE_INFO, status,
560 "Unable to disable GPE[%2X]",
562 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
566 * Execute the method associated with the GPE
567 * NOTE: Level-triggered GPEs are cleared after the method completes.
569 status = acpi_os_execute(OSL_GPE_HANDLER,
570 acpi_ev_asynch_execute_gpe_method,
572 if (ACPI_FAILURE(status)) {
573 ACPI_EXCEPTION((AE_INFO, status,
574 "Unable to queue handler for GPE[%2X] - event disabled",
581 /* No handler or method to run! */
584 "No handler or method for GPE[%2X], disabling event",
588 * Disable the GPE. The GPE will remain disabled until the ACPICA
589 * Core Subsystem is restarted, or a handler is installed.
591 status = acpi_ev_disable_gpe(gpe_event_info);
592 if (ACPI_FAILURE(status)) {
593 ACPI_EXCEPTION((AE_INFO, status,
594 "Unable to disable GPE[%2X]",
596 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
601 return_UINT32(ACPI_INTERRUPT_HANDLED);