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 upon whether there are
64 * references (either wake or run) to this GPE
66 ******************************************************************************/
69 acpi_ev_update_gpe_enable_masks(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_masks);
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_gpe_register_bit(gpe_event_info,
84 /* Clear the wake/run bits up front */
86 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
87 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
89 /* Set the mask bits 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, register_bit);
95 if (gpe_event_info->wakeup_count) {
96 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
99 return_ACPI_STATUS(AE_OK);
102 /*******************************************************************************
104 * FUNCTION: acpi_ev_enable_gpe
106 * PARAMETERS: gpe_event_info - GPE to enable
110 * DESCRIPTION: Hardware-enable a GPE. Always enables the GPE, regardless
111 * of type or number of references.
113 * Note: The GPE lock should be already acquired when this function is called.
115 ******************************************************************************/
117 acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
122 ACPI_FUNCTION_TRACE(ev_enable_gpe);
126 * We will only allow a GPE to be enabled if it has either an
127 * associated method (_Lxx/_Exx) or a handler. Otherwise, the
128 * GPE will be immediately disabled by acpi_ev_gpe_dispatch the
129 * first time it fires.
131 if (!(gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)) {
132 return_ACPI_STATUS(AE_NO_HANDLER);
135 /* Ensure the HW enable masks are current */
137 status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
138 if (ACPI_FAILURE(status)) {
139 return_ACPI_STATUS(status);
142 /* Clear the GPE (of stale events) */
144 status = acpi_hw_clear_gpe(gpe_event_info);
145 if (ACPI_FAILURE(status)) {
146 return_ACPI_STATUS(status);
149 /* Enable the requested GPE */
151 status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
152 return_ACPI_STATUS(status);
155 /*******************************************************************************
157 * FUNCTION: acpi_ev_disable_gpe
159 * PARAMETERS: gpe_event_info - GPE to disable
163 * DESCRIPTION: Hardware-disable a GPE. Always disables the requested GPE,
164 * regardless of the type or number of references.
166 * Note: The GPE lock should be already acquired when this function is called.
168 ******************************************************************************/
170 acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
174 ACPI_FUNCTION_TRACE(ev_disable_gpe);
178 * Note: Always disable the GPE, even if we think that that it is already
179 * disabled. It is possible that the AML or some other code has enabled
180 * the GPE behind our back.
183 /* Ensure the HW enable masks are current */
185 status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
186 if (ACPI_FAILURE(status)) {
187 return_ACPI_STATUS(status);
191 * Always H/W disable this GPE, even if we don't know the GPE type.
192 * Simply clear the enable bit for this particular GPE, but do not
193 * write out the current GPE enable mask since this may inadvertently
194 * enable GPEs too early. An example is a rogue GPE that has arrived
195 * during ACPICA initialization - possibly because AML or other code
196 * has enabled the GPE.
198 status = acpi_hw_low_disable_gpe(gpe_event_info);
199 return_ACPI_STATUS(status);
203 /*******************************************************************************
205 * FUNCTION: acpi_ev_low_get_gpe_info
207 * PARAMETERS: gpe_number - Raw GPE number
208 * gpe_block - A GPE info block
210 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
211 * is not within the specified GPE block)
213 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
214 * the low-level implementation of ev_get_gpe_event_info.
216 ******************************************************************************/
218 struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
219 struct acpi_gpe_block_info
225 * Validate that the gpe_number is within the specified gpe_block.
228 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
232 gpe_index = gpe_number - gpe_block->block_base_number;
233 if (gpe_index >= gpe_block->gpe_count) {
237 return (&gpe_block->event_info[gpe_index]);
241 /*******************************************************************************
243 * FUNCTION: acpi_ev_get_gpe_event_info
245 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
246 * gpe_number - Raw GPE number
248 * RETURN: A GPE event_info struct. NULL if not a valid GPE
250 * DESCRIPTION: Returns the event_info struct associated with this GPE.
251 * Validates the gpe_block and the gpe_number
253 * Should be called only when the GPE lists are semaphore locked
254 * and not subject to change.
256 ******************************************************************************/
258 struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
261 union acpi_operand_object *obj_desc;
262 struct acpi_gpe_event_info *gpe_info;
265 ACPI_FUNCTION_ENTRY();
267 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
271 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
273 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
274 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
275 acpi_gbl_gpe_fadt_blocks
282 /* The gpe_number was not in the range of either FADT GPE block */
287 /* A Non-NULL gpe_device means this is a GPE Block Device */
289 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
291 if (!obj_desc || !obj_desc->device.gpe_block) {
295 return (acpi_ev_low_get_gpe_info
296 (gpe_number, obj_desc->device.gpe_block));
299 /*******************************************************************************
301 * FUNCTION: acpi_ev_gpe_detect
303 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
304 * Can have multiple GPE blocks attached.
306 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
308 * DESCRIPTION: Detect if any GP events have occurred. This function is
309 * executed at interrupt level.
311 ******************************************************************************/
313 u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
316 struct acpi_gpe_block_info *gpe_block;
317 struct acpi_gpe_register_info *gpe_register_info;
318 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
319 u8 enabled_status_byte;
322 acpi_cpu_flags flags;
326 ACPI_FUNCTION_NAME(ev_gpe_detect);
328 /* Check for the case where there are no GPEs */
330 if (!gpe_xrupt_list) {
335 * We need to obtain the GPE lock for both the data structs and registers
336 * Note: Not necessary to obtain the hardware lock, since the GPE
337 * registers are owned by the gpe_lock.
339 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
341 /* Examine all GPE blocks attached to this interrupt level */
343 gpe_block = gpe_xrupt_list->gpe_block_list_head;
346 * Read all of the 8-bit GPE status and enable registers in this GPE
347 * block, saving all of them. Find all currently active GP events.
349 for (i = 0; i < gpe_block->register_count; i++) {
351 /* Get the next status/enable pair */
353 gpe_register_info = &gpe_block->register_info[i];
355 /* Read the Status Register */
358 acpi_hw_read(&status_reg,
359 &gpe_register_info->status_address);
360 if (ACPI_FAILURE(status)) {
361 goto unlock_and_exit;
364 /* Read the Enable Register */
367 acpi_hw_read(&enable_reg,
368 &gpe_register_info->enable_address);
369 if (ACPI_FAILURE(status)) {
370 goto unlock_and_exit;
373 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
374 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
375 gpe_register_info->base_gpe_number,
376 status_reg, enable_reg));
378 /* Check if there is anything active at all in this register */
380 enabled_status_byte = (u8) (status_reg & enable_reg);
381 if (!enabled_status_byte) {
383 /* No active GPEs in this register, move on */
388 /* Now look at the individual GPEs in this byte register */
390 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
392 /* Examine one GPE bit */
394 if (enabled_status_byte & (1 << j)) {
396 * Found an active GPE. Dispatch the event to a handler
400 acpi_ev_gpe_dispatch(&gpe_block->
401 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
406 gpe_block = gpe_block->next;
411 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
415 /*******************************************************************************
417 * FUNCTION: acpi_ev_asynch_execute_gpe_method
419 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
423 * DESCRIPTION: Perform the actual execution of a GPE control method. This
424 * function is called from an invocation of acpi_os_execute and
425 * therefore does NOT execute at interrupt level - so that
426 * the control method itself is not executed in the context of
427 * an interrupt handler.
429 ******************************************************************************/
430 static void acpi_ev_asynch_enable_gpe(void *context);
432 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
434 struct acpi_gpe_event_info *gpe_event_info = (void *)context;
436 struct acpi_gpe_event_info local_gpe_event_info;
437 struct acpi_evaluate_info *info;
439 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
441 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
442 if (ACPI_FAILURE(status)) {
446 /* Must revalidate the gpe_number/gpe_block */
448 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
449 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
453 /* Update the GPE register masks for return to enabled state */
455 (void)acpi_ev_update_gpe_enable_masks(gpe_event_info);
458 * Take a snapshot of the GPE info for this level - we copy the info to
459 * prevent a race condition with remove_handler/remove_block.
461 ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
462 sizeof(struct acpi_gpe_event_info));
464 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
465 if (ACPI_FAILURE(status)) {
470 * Must check for control method type dispatch one more time to avoid a
471 * race with ev_gpe_install_handler
473 if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
474 ACPI_GPE_DISPATCH_METHOD) {
476 /* Allocate the evaluation information block */
478 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
480 status = AE_NO_MEMORY;
483 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
484 * control method that corresponds to this GPE
487 local_gpe_event_info.dispatch.method_node;
488 info->flags = ACPI_IGNORE_RETURN_VALUE;
490 status = acpi_ns_evaluate(info);
494 if (ACPI_FAILURE(status)) {
495 ACPI_EXCEPTION((AE_INFO, status,
496 "while evaluating GPE method [%4.4s]",
497 acpi_ut_get_node_name
498 (local_gpe_event_info.dispatch.
502 /* Defer enabling of GPE until all notify handlers are done */
503 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
508 static void acpi_ev_asynch_enable_gpe(void *context)
510 struct acpi_gpe_event_info *gpe_event_info = context;
512 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
513 ACPI_GPE_LEVEL_TRIGGERED) {
515 * GPE is level-triggered, we clear the GPE status bit after handling
518 status = acpi_hw_clear_gpe(gpe_event_info);
519 if (ACPI_FAILURE(status)) {
524 /* Enable this GPE */
525 (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
529 /*******************************************************************************
531 * FUNCTION: acpi_ev_gpe_dispatch
533 * PARAMETERS: gpe_event_info - Info for this GPE
534 * gpe_number - Number relative to the parent GPE block
536 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
538 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
539 * or method (e.g. _Lxx/_Exx) handler.
541 * This function executes at interrupt level.
543 ******************************************************************************/
546 acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
550 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
552 acpi_os_gpe_count(gpe_number);
555 * If edge-triggered, clear the GPE status bit now. Note that
556 * level-triggered events are cleared after the GPE is serviced.
558 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
559 ACPI_GPE_EDGE_TRIGGERED) {
560 status = acpi_hw_clear_gpe(gpe_event_info);
561 if (ACPI_FAILURE(status)) {
562 ACPI_EXCEPTION((AE_INFO, status,
563 "Unable to clear GPE[0x%2X]",
565 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
570 * Dispatch the GPE to either an installed handler, or the control method
571 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
572 * it and do not attempt to run the method. If there is neither a handler
573 * nor a method, we disable this GPE to prevent further such pointless
574 * events from firing.
576 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
577 case ACPI_GPE_DISPATCH_HANDLER:
580 * Invoke the installed handler (at interrupt level)
581 * Ignore return status for now.
582 * TBD: leave GPE disabled on error?
584 (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
589 /* It is now safe to clear level-triggered events. */
591 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
592 ACPI_GPE_LEVEL_TRIGGERED) {
593 status = acpi_hw_clear_gpe(gpe_event_info);
594 if (ACPI_FAILURE(status)) {
595 ACPI_EXCEPTION((AE_INFO, status,
596 "Unable to clear GPE[0x%2X]",
598 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
603 case ACPI_GPE_DISPATCH_METHOD:
606 * Disable the GPE, so it doesn't keep firing before the method has a
607 * chance to run (it runs asynchronously with interrupts enabled).
609 status = acpi_ev_disable_gpe(gpe_event_info);
610 if (ACPI_FAILURE(status)) {
611 ACPI_EXCEPTION((AE_INFO, status,
612 "Unable to disable GPE[0x%2X]",
614 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
618 * Execute the method associated with the GPE
619 * NOTE: Level-triggered GPEs are cleared after the method completes.
621 status = acpi_os_execute(OSL_GPE_HANDLER,
622 acpi_ev_asynch_execute_gpe_method,
624 if (ACPI_FAILURE(status)) {
625 ACPI_EXCEPTION((AE_INFO, status,
626 "Unable to queue handler for GPE[0x%2X] - event disabled",
634 * No handler or method to run!
635 * 03/2010: This case should no longer be possible. We will not allow
636 * a GPE to be enabled if it has no handler or method.
639 "No handler or method for GPE[0x%2X], disabling event",
643 * Disable the GPE. The GPE will remain disabled a handler
644 * is installed or ACPICA is restarted.
646 status = acpi_ev_disable_gpe(gpe_event_info);
647 if (ACPI_FAILURE(status)) {
648 ACPI_EXCEPTION((AE_INFO, status,
649 "Unable to disable GPE[0x%2X]",
651 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
656 return_UINT32(ACPI_INTERRUPT_HANDLED);