ACPICA: Fix local variable mess in acpi_ev_asynch_execute_gpe_method
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / acpica / evgpe.c
blobc25999546c8c58c897d03f54a563ab1e57aad1b2
1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2010, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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>
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.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
63 * RETURN: Status
65 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
66 * runtime references to this GPE
68 ******************************************************************************/
70 acpi_status
71 acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
73 struct acpi_gpe_register_info *gpe_register_info;
74 u32 register_bit;
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,
84 gpe_register_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
105 * RETURN: Status
107 * DESCRIPTION: Clear the given GPE from stale events and enable it.
109 ******************************************************************************/
110 acpi_status
111 acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
113 acpi_status status;
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
146 * RETURN: Status
148 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
149 * hardware-enabled.
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
184 * RETURN: Status
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,
206 ACPI_GPE_DISABLE);
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
234 *gpe_block)
236 u32 gpe_index;
239 * Validate that the gpe_number is within the specified gpe_block.
240 * (Two steps)
242 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
243 return (NULL);
246 gpe_index = gpe_number - gpe_block->block_base_number;
247 if (gpe_index >= gpe_block->gpe_count) {
248 return (NULL);
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,
273 u32 gpe_number)
275 union acpi_operand_object *obj_desc;
276 struct acpi_gpe_event_info *gpe_info;
277 u32 i;
279 ACPI_FUNCTION_ENTRY();
281 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
283 if (!gpe_device) {
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
290 [i]);
291 if (gpe_info) {
292 return (gpe_info);
296 /* The gpe_number was not in the range of either FADT GPE block */
298 return (NULL);
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 *)
304 gpe_device);
305 if (!obj_desc || !obj_desc->device.gpe_block) {
306 return (NULL);
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)
329 acpi_status status;
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;
334 u32 status_reg;
335 u32 enable_reg;
336 acpi_cpu_flags flags;
337 u32 i;
338 u32 j;
340 ACPI_FUNCTION_NAME(ev_gpe_detect);
342 /* Check for the case where there are no GPEs */
344 if (!gpe_xrupt_list) {
345 return (int_status);
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;
358 while (gpe_block) {
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 */
371 status =
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 */
380 status =
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 */
399 continue;
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
411 * or method.
413 int_status |=
414 acpi_ev_gpe_dispatch(gpe_block->
415 node,
416 &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;
425 unlock_and_exit:
427 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
428 return (int_status);
431 /*******************************************************************************
433 * FUNCTION: acpi_ev_asynch_execute_gpe_method
435 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
437 * RETURN: None
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;
450 acpi_status status;
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"));
462 return_VOID;
465 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
466 if (ACPI_FAILURE(status)) {
467 return_VOID;
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);
474 return_VOID;
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)) {
486 return_VOID;
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));
499 if (!info) {
500 status = AE_NO_MEMORY;
501 } else {
503 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
504 * control method that corresponds to this GPE
506 info->prefix_node =
507 local_gpe_event_info->dispatch.method_node;
508 info->flags = ACPI_IGNORE_RETURN_VALUE;
510 status = acpi_ns_evaluate(info);
511 ACPI_FREE(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.
519 method_node)));
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);
531 return_VOID;
534 /*******************************************************************************
536 * FUNCTION: acpi_ev_asynch_enable_gpe
538 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
539 * Callback from acpi_os_execute
541 * RETURN: None
543 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
544 * complete.
546 ******************************************************************************/
548 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
550 struct acpi_gpe_event_info *gpe_event_info = context;
551 acpi_status status;
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
557 * the event.
559 status = acpi_hw_clear_gpe(gpe_event_info);
560 if (ACPI_FAILURE(status)) {
561 goto exit;
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);
571 exit:
572 ACPI_FREE(gpe_event_info);
573 return;
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)
597 acpi_status status;
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]",
613 gpe_number));
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,
634 gpe_number,
635 gpe_event_info->
636 dispatch.
637 handler->
638 context);
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]",
648 gpe_number));
649 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
652 break;
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]",
664 gpe_number));
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,
674 gpe_event_info);
675 if (ACPI_FAILURE(status)) {
676 ACPI_EXCEPTION((AE_INFO, status,
677 "Unable to queue handler for GPE[0x%2X] - event disabled",
678 gpe_number));
680 break;
682 default:
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.
689 ACPI_ERROR((AE_INFO,
690 "No handler or method for GPE[0x%2X], disabling event",
691 gpe_number));
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]",
701 gpe_number));
702 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
704 break;
707 return_UINT32(ACPI_INTERRUPT_HANDLED);