1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, 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 <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acevents.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME ("evgpe")
52 /* Local prototypes */
54 static void ACPI_SYSTEM_XFACE
55 AcpiEvAsynchExecuteGpeMethod (
58 static void ACPI_SYSTEM_XFACE
59 AcpiEvAsynchEnableGpe (
63 /*******************************************************************************
65 * FUNCTION: AcpiEvUpdateGpeEnableMask
67 * PARAMETERS: GpeEventInfo - GPE to update
71 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
72 * runtime references to this GPE
74 ******************************************************************************/
77 AcpiEvUpdateGpeEnableMask (
78 ACPI_GPE_EVENT_INFO *GpeEventInfo)
80 ACPI_GPE_REGISTER_INFO *GpeRegisterInfo;
84 ACPI_FUNCTION_TRACE (EvUpdateGpeEnableMask);
87 GpeRegisterInfo = GpeEventInfo->RegisterInfo;
90 return_ACPI_STATUS (AE_NOT_EXIST);
93 RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo, GpeRegisterInfo);
95 /* Clear the run bit up front */
97 ACPI_CLEAR_BIT (GpeRegisterInfo->EnableForRun, RegisterBit);
99 /* Set the mask bit only if there are references to this GPE */
101 if (GpeEventInfo->RuntimeCount)
103 ACPI_SET_BIT (GpeRegisterInfo->EnableForRun, (UINT8) RegisterBit);
106 return_ACPI_STATUS (AE_OK);
110 /*******************************************************************************
112 * FUNCTION: AcpiEvEnableGpe
114 * PARAMETERS: GpeEventInfo - GPE to enable
118 * DESCRIPTION: Clear a GPE of stale events and enable it.
120 ******************************************************************************/
124 ACPI_GPE_EVENT_INFO *GpeEventInfo)
129 ACPI_FUNCTION_TRACE (EvEnableGpe);
133 * We will only allow a GPE to be enabled if it has either an associated
134 * method (_Lxx/_Exx) or a handler, or is using the implicit notify
135 * feature. Otherwise, the GPE will be immediately disabled by
136 * AcpiEvGpeDispatch the first time it fires.
138 if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
139 ACPI_GPE_DISPATCH_NONE)
141 return_ACPI_STATUS (AE_NO_HANDLER);
144 /* Clear the GPE (of stale events) */
146 Status = AcpiHwClearGpe (GpeEventInfo);
147 if (ACPI_FAILURE (Status))
149 return_ACPI_STATUS (Status);
152 /* Enable the requested GPE */
154 Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_ENABLE);
155 return_ACPI_STATUS (Status);
159 /*******************************************************************************
161 * FUNCTION: AcpiEvAddGpeReference
163 * PARAMETERS: GpeEventInfo - Add a reference to this GPE
167 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
170 ******************************************************************************/
173 AcpiEvAddGpeReference (
174 ACPI_GPE_EVENT_INFO *GpeEventInfo)
176 ACPI_STATUS Status = AE_OK;
179 ACPI_FUNCTION_TRACE (EvAddGpeReference);
182 if (GpeEventInfo->RuntimeCount == ACPI_UINT8_MAX)
184 return_ACPI_STATUS (AE_LIMIT);
187 GpeEventInfo->RuntimeCount++;
188 if (GpeEventInfo->RuntimeCount == 1)
190 /* Enable on first reference */
192 Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
193 if (ACPI_SUCCESS (Status))
195 Status = AcpiEvEnableGpe (GpeEventInfo);
198 if (ACPI_FAILURE (Status))
200 GpeEventInfo->RuntimeCount--;
204 return_ACPI_STATUS (Status);
208 /*******************************************************************************
210 * FUNCTION: AcpiEvRemoveGpeReference
212 * PARAMETERS: GpeEventInfo - Remove a reference to this GPE
216 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
217 * removed, the GPE is hardware-disabled.
219 ******************************************************************************/
222 AcpiEvRemoveGpeReference (
223 ACPI_GPE_EVENT_INFO *GpeEventInfo)
225 ACPI_STATUS Status = AE_OK;
228 ACPI_FUNCTION_TRACE (EvRemoveGpeReference);
231 if (!GpeEventInfo->RuntimeCount)
233 return_ACPI_STATUS (AE_LIMIT);
236 GpeEventInfo->RuntimeCount--;
237 if (!GpeEventInfo->RuntimeCount)
239 /* Disable on last reference */
241 Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
242 if (ACPI_SUCCESS (Status))
244 Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
247 if (ACPI_FAILURE (Status))
249 GpeEventInfo->RuntimeCount++;
253 return_ACPI_STATUS (Status);
257 /*******************************************************************************
259 * FUNCTION: AcpiEvLowGetGpeInfo
261 * PARAMETERS: GpeNumber - Raw GPE number
262 * GpeBlock - A GPE info block
264 * RETURN: A GPE EventInfo struct. NULL if not a valid GPE (The GpeNumber
265 * is not within the specified GPE block)
267 * DESCRIPTION: Returns the EventInfo struct associated with this GPE. This is
268 * the low-level implementation of EvGetGpeEventInfo.
270 ******************************************************************************/
272 ACPI_GPE_EVENT_INFO *
273 AcpiEvLowGetGpeInfo (
275 ACPI_GPE_BLOCK_INFO *GpeBlock)
281 * Validate that the GpeNumber is within the specified GpeBlock.
285 (GpeNumber < GpeBlock->BlockBaseNumber))
290 GpeIndex = GpeNumber - GpeBlock->BlockBaseNumber;
291 if (GpeIndex >= GpeBlock->GpeCount)
296 return (&GpeBlock->EventInfo[GpeIndex]);
300 /*******************************************************************************
302 * FUNCTION: AcpiEvGetGpeEventInfo
304 * PARAMETERS: GpeDevice - Device node. NULL for GPE0/GPE1
305 * GpeNumber - Raw GPE number
307 * RETURN: A GPE EventInfo struct. NULL if not a valid GPE
309 * DESCRIPTION: Returns the EventInfo struct associated with this GPE.
310 * Validates the GpeBlock and the GpeNumber
312 * Should be called only when the GPE lists are semaphore locked
313 * and not subject to change.
315 ******************************************************************************/
317 ACPI_GPE_EVENT_INFO *
318 AcpiEvGetGpeEventInfo (
319 ACPI_HANDLE GpeDevice,
322 ACPI_OPERAND_OBJECT *ObjDesc;
323 ACPI_GPE_EVENT_INFO *GpeInfo;
327 ACPI_FUNCTION_ENTRY ();
330 /* A NULL GpeDevice means use the FADT-defined GPE block(s) */
334 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
336 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++)
338 GpeInfo = AcpiEvLowGetGpeInfo (GpeNumber,
339 AcpiGbl_GpeFadtBlocks[i]);
346 /* The GpeNumber was not in the range of either FADT GPE block */
351 /* A Non-NULL GpeDevice means this is a GPE Block Device */
353 ObjDesc = AcpiNsGetAttachedObject ((ACPI_NAMESPACE_NODE *) GpeDevice);
355 !ObjDesc->Device.GpeBlock)
360 return (AcpiEvLowGetGpeInfo (GpeNumber, ObjDesc->Device.GpeBlock));
364 /*******************************************************************************
366 * FUNCTION: AcpiEvGpeDetect
368 * PARAMETERS: GpeXruptList - Interrupt block for this interrupt.
369 * Can have multiple GPE blocks attached.
371 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
373 * DESCRIPTION: Detect if any GP events have occurred. This function is
374 * executed at interrupt level.
376 ******************************************************************************/
380 ACPI_GPE_XRUPT_INFO *GpeXruptList)
383 ACPI_GPE_BLOCK_INFO *GpeBlock;
384 ACPI_GPE_REGISTER_INFO *GpeRegisterInfo;
385 UINT32 IntStatus = ACPI_INTERRUPT_NOT_HANDLED;
386 UINT8 EnabledStatusByte;
389 ACPI_CPU_FLAGS Flags;
394 ACPI_FUNCTION_NAME (EvGpeDetect);
396 /* Check for the case where there are no GPEs */
404 * We need to obtain the GPE lock for both the data structs and registers
405 * Note: Not necessary to obtain the hardware lock, since the GPE
406 * registers are owned by the GpeLock.
408 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
410 /* Examine all GPE blocks attached to this interrupt level */
412 GpeBlock = GpeXruptList->GpeBlockListHead;
416 * Read all of the 8-bit GPE status and enable registers in this GPE
417 * block, saving all of them. Find all currently active GP events.
419 for (i = 0; i < GpeBlock->RegisterCount; i++)
421 /* Get the next status/enable pair */
423 GpeRegisterInfo = &GpeBlock->RegisterInfo[i];
426 * Optimization: If there are no GPEs enabled within this
427 * register, we can safely ignore the entire register.
429 if (!(GpeRegisterInfo->EnableForRun |
430 GpeRegisterInfo->EnableForWake))
435 /* Read the Status Register */
437 Status = AcpiHwRead (&StatusReg, &GpeRegisterInfo->StatusAddress);
438 if (ACPI_FAILURE (Status))
443 /* Read the Enable Register */
445 Status = AcpiHwRead (&EnableReg, &GpeRegisterInfo->EnableAddress);
446 if (ACPI_FAILURE (Status))
451 ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
452 "Read GPE Register at GPE%02X: Status=%02X, Enable=%02X\n",
453 GpeRegisterInfo->BaseGpeNumber, StatusReg, EnableReg));
455 /* Check if there is anything active at all in this register */
457 EnabledStatusByte = (UINT8) (StatusReg & EnableReg);
458 if (!EnabledStatusByte)
460 /* No active GPEs in this register, move on */
465 /* Now look at the individual GPEs in this byte register */
467 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
469 /* Examine one GPE bit */
471 if (EnabledStatusByte & (1 << j))
474 * Found an active GPE. Dispatch the event to a handler
477 IntStatus |= AcpiEvGpeDispatch (GpeBlock->Node,
478 &GpeBlock->EventInfo[((ACPI_SIZE) i *
479 ACPI_GPE_REGISTER_WIDTH) + j],
480 j + GpeRegisterInfo->BaseGpeNumber);
485 GpeBlock = GpeBlock->Next;
490 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
495 /*******************************************************************************
497 * FUNCTION: AcpiEvAsynchExecuteGpeMethod
499 * PARAMETERS: Context (GpeEventInfo) - Info for this GPE
503 * DESCRIPTION: Perform the actual execution of a GPE control method. This
504 * function is called from an invocation of AcpiOsExecute and
505 * therefore does NOT execute at interrupt level - so that
506 * the control method itself is not executed in the context of
507 * an interrupt handler.
509 ******************************************************************************/
511 static void ACPI_SYSTEM_XFACE
512 AcpiEvAsynchExecuteGpeMethod (
515 ACPI_GPE_EVENT_INFO *GpeEventInfo = Context;
517 ACPI_GPE_EVENT_INFO *LocalGpeEventInfo;
518 ACPI_EVALUATE_INFO *Info;
521 ACPI_FUNCTION_TRACE (EvAsynchExecuteGpeMethod);
524 /* Allocate a local GPE block */
526 LocalGpeEventInfo = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_EVENT_INFO));
527 if (!LocalGpeEventInfo)
529 ACPI_EXCEPTION ((AE_INFO, AE_NO_MEMORY,
530 "while handling a GPE"));
534 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
535 if (ACPI_FAILURE (Status))
540 /* Must revalidate the GpeNumber/GpeBlock */
542 if (!AcpiEvValidGpeEvent (GpeEventInfo))
544 Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
549 * Take a snapshot of the GPE info for this level - we copy the info to
550 * prevent a race condition with RemoveHandler/RemoveBlock.
552 ACPI_MEMCPY (LocalGpeEventInfo, GpeEventInfo,
553 sizeof (ACPI_GPE_EVENT_INFO));
555 Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
556 if (ACPI_FAILURE (Status))
561 /* Do the correct dispatch - normal method or implicit notify */
563 switch (LocalGpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK)
565 case ACPI_GPE_DISPATCH_NOTIFY:
569 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
570 * NOTE: the request is queued for execution after this method
571 * completes. The notify handlers are NOT invoked synchronously
572 * from this thread -- because handlers may in turn run other
575 Status = AcpiEvQueueNotifyRequest (
576 LocalGpeEventInfo->Dispatch.DeviceNode,
577 ACPI_NOTIFY_DEVICE_WAKE);
580 case ACPI_GPE_DISPATCH_METHOD:
582 /* Allocate the evaluation information block */
584 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
587 Status = AE_NO_MEMORY;
592 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
593 * _Lxx/_Exx control method that corresponds to this GPE
595 Info->PrefixNode = LocalGpeEventInfo->Dispatch.MethodNode;
596 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
598 Status = AcpiNsEvaluate (Info);
602 if (ACPI_FAILURE (Status))
604 ACPI_EXCEPTION ((AE_INFO, Status,
605 "while evaluating GPE method [%4.4s]",
606 AcpiUtGetNodeName (LocalGpeEventInfo->Dispatch.MethodNode)));
612 return_VOID; /* Should never happen */
615 /* Defer enabling of GPE until all notify handlers are done */
617 Status = AcpiOsExecute (OSL_NOTIFY_HANDLER,
618 AcpiEvAsynchEnableGpe, LocalGpeEventInfo);
619 if (ACPI_FAILURE (Status))
621 ACPI_FREE (LocalGpeEventInfo);
627 /*******************************************************************************
629 * FUNCTION: AcpiEvAsynchEnableGpe
631 * PARAMETERS: Context (GpeEventInfo) - Info for this GPE
632 * Callback from AcpiOsExecute
636 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
637 * complete (i.e., finish execution of Notify)
639 ******************************************************************************/
641 static void ACPI_SYSTEM_XFACE
642 AcpiEvAsynchEnableGpe (
645 ACPI_GPE_EVENT_INFO *GpeEventInfo = Context;
648 (void) AcpiEvFinishGpe (GpeEventInfo);
650 ACPI_FREE (GpeEventInfo);
655 /*******************************************************************************
657 * FUNCTION: AcpiEvFinishGpe
659 * PARAMETERS: GpeEventInfo - Info for this GPE
663 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
664 * of a GPE method or a synchronous or asynchronous GPE handler.
666 ******************************************************************************/
670 ACPI_GPE_EVENT_INFO *GpeEventInfo)
675 if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
676 ACPI_GPE_LEVEL_TRIGGERED)
679 * GPE is level-triggered, we clear the GPE status bit after
680 * handling the event.
682 Status = AcpiHwClearGpe (GpeEventInfo);
683 if (ACPI_FAILURE (Status))
690 * Enable this GPE, conditionally. This means that the GPE will
691 * only be physically enabled if the EnableForRun bit is set
694 (void) AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_CONDITIONAL_ENABLE);
699 /*******************************************************************************
701 * FUNCTION: AcpiEvGpeDispatch
703 * PARAMETERS: GpeDevice - Device node. NULL for GPE0/GPE1
704 * GpeEventInfo - Info for this GPE
705 * GpeNumber - Number relative to the parent GPE block
707 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
709 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
710 * or method (e.g. _Lxx/_Exx) handler.
712 * This function executes at interrupt level.
714 ******************************************************************************/
718 ACPI_NAMESPACE_NODE *GpeDevice,
719 ACPI_GPE_EVENT_INFO *GpeEventInfo,
726 ACPI_FUNCTION_TRACE (EvGpeDispatch);
729 /* Invoke global event handler if present */
732 if (AcpiGbl_GlobalEventHandler)
734 AcpiGbl_GlobalEventHandler (ACPI_EVENT_TYPE_GPE, GpeDevice,
735 GpeNumber, AcpiGbl_GlobalEventHandlerContext);
739 * If edge-triggered, clear the GPE status bit now. Note that
740 * level-triggered events are cleared after the GPE is serviced.
742 if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
743 ACPI_GPE_EDGE_TRIGGERED)
745 Status = AcpiHwClearGpe (GpeEventInfo);
746 if (ACPI_FAILURE (Status))
748 ACPI_EXCEPTION ((AE_INFO, Status,
749 "Unable to clear GPE%02X", GpeNumber));
750 return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
755 * Always disable the GPE so that it does not keep firing before
756 * any asynchronous activity completes (either from the execution
757 * of a GPE method or an asynchronous GPE handler.)
759 * If there is no handler or method to run, just disable the
760 * GPE and leave it disabled permanently to prevent further such
761 * pointless events from firing.
763 Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
764 if (ACPI_FAILURE (Status))
766 ACPI_EXCEPTION ((AE_INFO, Status,
767 "Unable to disable GPE%02X", GpeNumber));
768 return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
772 * Dispatch the GPE to either an installed handler or the control
773 * method associated with this GPE (_Lxx or _Exx). If a handler
774 * exists, we invoke it and do not attempt to run the method.
775 * If there is neither a handler nor a method, leave the GPE
778 switch (GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK)
780 case ACPI_GPE_DISPATCH_HANDLER:
782 /* Invoke the installed handler (at interrupt level) */
784 ReturnValue = GpeEventInfo->Dispatch.Handler->Address (
785 GpeDevice, GpeNumber,
786 GpeEventInfo->Dispatch.Handler->Context);
788 /* If requested, clear (if level-triggered) and reenable the GPE */
790 if (ReturnValue & ACPI_REENABLE_GPE)
792 (void) AcpiEvFinishGpe (GpeEventInfo);
796 case ACPI_GPE_DISPATCH_METHOD:
797 case ACPI_GPE_DISPATCH_NOTIFY:
800 * Execute the method associated with the GPE
801 * NOTE: Level-triggered GPEs are cleared after the method completes.
803 Status = AcpiOsExecute (OSL_GPE_HANDLER,
804 AcpiEvAsynchExecuteGpeMethod, GpeEventInfo);
805 if (ACPI_FAILURE (Status))
807 ACPI_EXCEPTION ((AE_INFO, Status,
808 "Unable to queue handler for GPE%02X - event disabled",
816 * No handler or method to run!
817 * 03/2010: This case should no longer be possible. We will not allow
818 * a GPE to be enabled if it has no handler or method.
820 ACPI_ERROR ((AE_INFO,
821 "No handler or method for GPE%02X, disabling event",
826 return_UINT32 (ACPI_INTERRUPT_HANDLED);