xref: /minix3/minix/drivers/power/acpi/events/evgpe.c (revision 29492bb71c7148a089a5afafa0c99409161218df)
1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Module Name: evgpe - General Purpose Event handling and dispatch
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  *****************************************************************************/
6433d6423SLionel Sambuc 
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek  * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc  * All rights reserved.
10433d6423SLionel Sambuc  *
11*29492bb7SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek  * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek  * are met:
14*29492bb7SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek  *    notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek  *    without modification.
17*29492bb7SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek  *    substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek  *    ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek  *    including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek  *    binary redistribution.
22*29492bb7SDavid van Moolenbroek  * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek  *    of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek  *    from this software without specific prior written permission.
25433d6423SLionel Sambuc  *
26*29492bb7SDavid van Moolenbroek  * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek  * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek  * Software Foundation.
29433d6423SLionel Sambuc  *
30*29492bb7SDavid van Moolenbroek  * NO WARRANTY
31*29492bb7SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek  * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek  */
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acevents.h"
47433d6423SLionel Sambuc #include "acnamesp.h"
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc #define _COMPONENT          ACPI_EVENTS
50433d6423SLionel Sambuc         ACPI_MODULE_NAME    ("evgpe")
51433d6423SLionel Sambuc 
52*29492bb7SDavid van Moolenbroek #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
53*29492bb7SDavid van Moolenbroek 
54433d6423SLionel Sambuc /* Local prototypes */
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc static void ACPI_SYSTEM_XFACE
57433d6423SLionel Sambuc AcpiEvAsynchExecuteGpeMethod (
58433d6423SLionel Sambuc     void                    *Context);
59433d6423SLionel Sambuc 
60433d6423SLionel Sambuc static void ACPI_SYSTEM_XFACE
61433d6423SLionel Sambuc AcpiEvAsynchEnableGpe (
62433d6423SLionel Sambuc     void                    *Context);
63433d6423SLionel Sambuc 
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc /*******************************************************************************
66433d6423SLionel Sambuc  *
67433d6423SLionel Sambuc  * FUNCTION:    AcpiEvUpdateGpeEnableMask
68433d6423SLionel Sambuc  *
69433d6423SLionel Sambuc  * PARAMETERS:  GpeEventInfo            - GPE to update
70433d6423SLionel Sambuc  *
71433d6423SLionel Sambuc  * RETURN:      Status
72433d6423SLionel Sambuc  *
73433d6423SLionel Sambuc  * DESCRIPTION: Updates GPE register enable mask based upon whether there are
74433d6423SLionel Sambuc  *              runtime references to this GPE
75433d6423SLionel Sambuc  *
76433d6423SLionel Sambuc  ******************************************************************************/
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc ACPI_STATUS
AcpiEvUpdateGpeEnableMask(ACPI_GPE_EVENT_INFO * GpeEventInfo)79433d6423SLionel Sambuc AcpiEvUpdateGpeEnableMask (
80433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
81433d6423SLionel Sambuc {
82433d6423SLionel Sambuc     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
83433d6423SLionel Sambuc     UINT32                  RegisterBit;
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc 
86433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (EvUpdateGpeEnableMask);
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc     GpeRegisterInfo = GpeEventInfo->RegisterInfo;
90433d6423SLionel Sambuc     if (!GpeRegisterInfo)
91433d6423SLionel Sambuc     {
92433d6423SLionel Sambuc         return_ACPI_STATUS (AE_NOT_EXIST);
93433d6423SLionel Sambuc     }
94433d6423SLionel Sambuc 
95*29492bb7SDavid van Moolenbroek     RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo);
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc     /* Clear the run bit up front */
98433d6423SLionel Sambuc 
99433d6423SLionel Sambuc     ACPI_CLEAR_BIT (GpeRegisterInfo->EnableForRun, RegisterBit);
100433d6423SLionel Sambuc 
101433d6423SLionel Sambuc     /* Set the mask bit only if there are references to this GPE */
102433d6423SLionel Sambuc 
103433d6423SLionel Sambuc     if (GpeEventInfo->RuntimeCount)
104433d6423SLionel Sambuc     {
105433d6423SLionel Sambuc         ACPI_SET_BIT (GpeRegisterInfo->EnableForRun, (UINT8) RegisterBit);
106433d6423SLionel Sambuc     }
107433d6423SLionel Sambuc 
108433d6423SLionel Sambuc     return_ACPI_STATUS (AE_OK);
109433d6423SLionel Sambuc }
110433d6423SLionel Sambuc 
111433d6423SLionel Sambuc 
112433d6423SLionel Sambuc /*******************************************************************************
113433d6423SLionel Sambuc  *
114433d6423SLionel Sambuc  * FUNCTION:    AcpiEvEnableGpe
115433d6423SLionel Sambuc  *
116433d6423SLionel Sambuc  * PARAMETERS:  GpeEventInfo            - GPE to enable
117433d6423SLionel Sambuc  *
118433d6423SLionel Sambuc  * RETURN:      Status
119433d6423SLionel Sambuc  *
120433d6423SLionel Sambuc  * DESCRIPTION: Clear a GPE of stale events and enable it.
121433d6423SLionel Sambuc  *
122433d6423SLionel Sambuc  ******************************************************************************/
123433d6423SLionel Sambuc 
124433d6423SLionel Sambuc ACPI_STATUS
AcpiEvEnableGpe(ACPI_GPE_EVENT_INFO * GpeEventInfo)125433d6423SLionel Sambuc AcpiEvEnableGpe (
126433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
127433d6423SLionel Sambuc {
128433d6423SLionel Sambuc     ACPI_STATUS             Status;
129433d6423SLionel Sambuc 
130433d6423SLionel Sambuc 
131433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (EvEnableGpe);
132433d6423SLionel Sambuc 
133433d6423SLionel Sambuc 
134433d6423SLionel Sambuc     /*
135*29492bb7SDavid van Moolenbroek      * We will only allow a GPE to be enabled if it has either an associated
136*29492bb7SDavid van Moolenbroek      * method (_Lxx/_Exx) or a handler, or is using the implicit notify
137*29492bb7SDavid van Moolenbroek      * feature. Otherwise, the GPE will be immediately disabled by
138*29492bb7SDavid van Moolenbroek      * AcpiEvGpeDispatch the first time it fires.
139433d6423SLionel Sambuc      */
140*29492bb7SDavid van Moolenbroek     if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
141*29492bb7SDavid van Moolenbroek         ACPI_GPE_DISPATCH_NONE)
142433d6423SLionel Sambuc     {
143433d6423SLionel Sambuc         return_ACPI_STATUS (AE_NO_HANDLER);
144433d6423SLionel Sambuc     }
145433d6423SLionel Sambuc 
146433d6423SLionel Sambuc     /* Clear the GPE (of stale events) */
147433d6423SLionel Sambuc 
148433d6423SLionel Sambuc     Status = AcpiHwClearGpe (GpeEventInfo);
149433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
150433d6423SLionel Sambuc     {
151433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
152433d6423SLionel Sambuc     }
153433d6423SLionel Sambuc 
154433d6423SLionel Sambuc     /* Enable the requested GPE */
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc     Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_ENABLE);
157433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
158433d6423SLionel Sambuc }
159433d6423SLionel Sambuc 
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc /*******************************************************************************
162433d6423SLionel Sambuc  *
163*29492bb7SDavid van Moolenbroek  * FUNCTION:    AcpiEvAddGpeReference
164*29492bb7SDavid van Moolenbroek  *
165*29492bb7SDavid van Moolenbroek  * PARAMETERS:  GpeEventInfo            - Add a reference to this GPE
166*29492bb7SDavid van Moolenbroek  *
167*29492bb7SDavid van Moolenbroek  * RETURN:      Status
168*29492bb7SDavid van Moolenbroek  *
169*29492bb7SDavid van Moolenbroek  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
170*29492bb7SDavid van Moolenbroek  *              hardware-enabled.
171*29492bb7SDavid van Moolenbroek  *
172*29492bb7SDavid van Moolenbroek  ******************************************************************************/
173*29492bb7SDavid van Moolenbroek 
174*29492bb7SDavid van Moolenbroek ACPI_STATUS
AcpiEvAddGpeReference(ACPI_GPE_EVENT_INFO * GpeEventInfo)175*29492bb7SDavid van Moolenbroek AcpiEvAddGpeReference (
176*29492bb7SDavid van Moolenbroek     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
177*29492bb7SDavid van Moolenbroek {
178*29492bb7SDavid van Moolenbroek     ACPI_STATUS             Status = AE_OK;
179*29492bb7SDavid van Moolenbroek 
180*29492bb7SDavid van Moolenbroek 
181*29492bb7SDavid van Moolenbroek     ACPI_FUNCTION_TRACE (EvAddGpeReference);
182*29492bb7SDavid van Moolenbroek 
183*29492bb7SDavid van Moolenbroek 
184*29492bb7SDavid van Moolenbroek     if (GpeEventInfo->RuntimeCount == ACPI_UINT8_MAX)
185*29492bb7SDavid van Moolenbroek     {
186*29492bb7SDavid van Moolenbroek         return_ACPI_STATUS (AE_LIMIT);
187*29492bb7SDavid van Moolenbroek     }
188*29492bb7SDavid van Moolenbroek 
189*29492bb7SDavid van Moolenbroek     GpeEventInfo->RuntimeCount++;
190*29492bb7SDavid van Moolenbroek     if (GpeEventInfo->RuntimeCount == 1)
191*29492bb7SDavid van Moolenbroek     {
192*29492bb7SDavid van Moolenbroek         /* Enable on first reference */
193*29492bb7SDavid van Moolenbroek 
194*29492bb7SDavid van Moolenbroek         Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
195*29492bb7SDavid van Moolenbroek         if (ACPI_SUCCESS (Status))
196*29492bb7SDavid van Moolenbroek         {
197*29492bb7SDavid van Moolenbroek             Status = AcpiEvEnableGpe (GpeEventInfo);
198*29492bb7SDavid van Moolenbroek         }
199*29492bb7SDavid van Moolenbroek 
200*29492bb7SDavid van Moolenbroek         if (ACPI_FAILURE (Status))
201*29492bb7SDavid van Moolenbroek         {
202*29492bb7SDavid van Moolenbroek             GpeEventInfo->RuntimeCount--;
203*29492bb7SDavid van Moolenbroek         }
204*29492bb7SDavid van Moolenbroek     }
205*29492bb7SDavid van Moolenbroek 
206*29492bb7SDavid van Moolenbroek     return_ACPI_STATUS (Status);
207*29492bb7SDavid van Moolenbroek }
208*29492bb7SDavid van Moolenbroek 
209*29492bb7SDavid van Moolenbroek 
210*29492bb7SDavid van Moolenbroek /*******************************************************************************
211*29492bb7SDavid van Moolenbroek  *
212*29492bb7SDavid van Moolenbroek  * FUNCTION:    AcpiEvRemoveGpeReference
213*29492bb7SDavid van Moolenbroek  *
214*29492bb7SDavid van Moolenbroek  * PARAMETERS:  GpeEventInfo            - Remove a reference to this GPE
215*29492bb7SDavid van Moolenbroek  *
216*29492bb7SDavid van Moolenbroek  * RETURN:      Status
217*29492bb7SDavid van Moolenbroek  *
218*29492bb7SDavid van Moolenbroek  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
219*29492bb7SDavid van Moolenbroek  *              removed, the GPE is hardware-disabled.
220*29492bb7SDavid van Moolenbroek  *
221*29492bb7SDavid van Moolenbroek  ******************************************************************************/
222*29492bb7SDavid van Moolenbroek 
223*29492bb7SDavid van Moolenbroek ACPI_STATUS
AcpiEvRemoveGpeReference(ACPI_GPE_EVENT_INFO * GpeEventInfo)224*29492bb7SDavid van Moolenbroek AcpiEvRemoveGpeReference (
225*29492bb7SDavid van Moolenbroek     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
226*29492bb7SDavid van Moolenbroek {
227*29492bb7SDavid van Moolenbroek     ACPI_STATUS             Status = AE_OK;
228*29492bb7SDavid van Moolenbroek 
229*29492bb7SDavid van Moolenbroek 
230*29492bb7SDavid van Moolenbroek     ACPI_FUNCTION_TRACE (EvRemoveGpeReference);
231*29492bb7SDavid van Moolenbroek 
232*29492bb7SDavid van Moolenbroek 
233*29492bb7SDavid van Moolenbroek     if (!GpeEventInfo->RuntimeCount)
234*29492bb7SDavid van Moolenbroek     {
235*29492bb7SDavid van Moolenbroek         return_ACPI_STATUS (AE_LIMIT);
236*29492bb7SDavid van Moolenbroek     }
237*29492bb7SDavid van Moolenbroek 
238*29492bb7SDavid van Moolenbroek     GpeEventInfo->RuntimeCount--;
239*29492bb7SDavid van Moolenbroek     if (!GpeEventInfo->RuntimeCount)
240*29492bb7SDavid van Moolenbroek     {
241*29492bb7SDavid van Moolenbroek         /* Disable on last reference */
242*29492bb7SDavid van Moolenbroek 
243*29492bb7SDavid van Moolenbroek         Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
244*29492bb7SDavid van Moolenbroek         if (ACPI_SUCCESS (Status))
245*29492bb7SDavid van Moolenbroek         {
246*29492bb7SDavid van Moolenbroek             Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
247*29492bb7SDavid van Moolenbroek         }
248*29492bb7SDavid van Moolenbroek 
249*29492bb7SDavid van Moolenbroek         if (ACPI_FAILURE (Status))
250*29492bb7SDavid van Moolenbroek         {
251*29492bb7SDavid van Moolenbroek             GpeEventInfo->RuntimeCount++;
252*29492bb7SDavid van Moolenbroek         }
253*29492bb7SDavid van Moolenbroek     }
254*29492bb7SDavid van Moolenbroek 
255*29492bb7SDavid van Moolenbroek     return_ACPI_STATUS (Status);
256*29492bb7SDavid van Moolenbroek }
257*29492bb7SDavid van Moolenbroek 
258*29492bb7SDavid van Moolenbroek 
259*29492bb7SDavid van Moolenbroek /*******************************************************************************
260*29492bb7SDavid van Moolenbroek  *
261433d6423SLionel Sambuc  * FUNCTION:    AcpiEvLowGetGpeInfo
262433d6423SLionel Sambuc  *
263433d6423SLionel Sambuc  * PARAMETERS:  GpeNumber           - Raw GPE number
264433d6423SLionel Sambuc  *              GpeBlock            - A GPE info block
265433d6423SLionel Sambuc  *
266433d6423SLionel Sambuc  * RETURN:      A GPE EventInfo struct. NULL if not a valid GPE (The GpeNumber
267433d6423SLionel Sambuc  *              is not within the specified GPE block)
268433d6423SLionel Sambuc  *
269433d6423SLionel Sambuc  * DESCRIPTION: Returns the EventInfo struct associated with this GPE. This is
270433d6423SLionel Sambuc  *              the low-level implementation of EvGetGpeEventInfo.
271433d6423SLionel Sambuc  *
272433d6423SLionel Sambuc  ******************************************************************************/
273433d6423SLionel Sambuc 
274433d6423SLionel Sambuc ACPI_GPE_EVENT_INFO *
AcpiEvLowGetGpeInfo(UINT32 GpeNumber,ACPI_GPE_BLOCK_INFO * GpeBlock)275433d6423SLionel Sambuc AcpiEvLowGetGpeInfo (
276433d6423SLionel Sambuc     UINT32                  GpeNumber,
277433d6423SLionel Sambuc     ACPI_GPE_BLOCK_INFO     *GpeBlock)
278433d6423SLionel Sambuc {
279433d6423SLionel Sambuc     UINT32                  GpeIndex;
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc 
282433d6423SLionel Sambuc     /*
283433d6423SLionel Sambuc      * Validate that the GpeNumber is within the specified GpeBlock.
284433d6423SLionel Sambuc      * (Two steps)
285433d6423SLionel Sambuc      */
286433d6423SLionel Sambuc     if (!GpeBlock ||
287433d6423SLionel Sambuc         (GpeNumber < GpeBlock->BlockBaseNumber))
288433d6423SLionel Sambuc     {
289433d6423SLionel Sambuc         return (NULL);
290433d6423SLionel Sambuc     }
291433d6423SLionel Sambuc 
292433d6423SLionel Sambuc     GpeIndex = GpeNumber - GpeBlock->BlockBaseNumber;
293433d6423SLionel Sambuc     if (GpeIndex >= GpeBlock->GpeCount)
294433d6423SLionel Sambuc     {
295433d6423SLionel Sambuc         return (NULL);
296433d6423SLionel Sambuc     }
297433d6423SLionel Sambuc 
298433d6423SLionel Sambuc     return (&GpeBlock->EventInfo[GpeIndex]);
299433d6423SLionel Sambuc }
300433d6423SLionel Sambuc 
301433d6423SLionel Sambuc 
302433d6423SLionel Sambuc /*******************************************************************************
303433d6423SLionel Sambuc  *
304433d6423SLionel Sambuc  * FUNCTION:    AcpiEvGetGpeEventInfo
305433d6423SLionel Sambuc  *
306433d6423SLionel Sambuc  * PARAMETERS:  GpeDevice           - Device node. NULL for GPE0/GPE1
307433d6423SLionel Sambuc  *              GpeNumber           - Raw GPE number
308433d6423SLionel Sambuc  *
309433d6423SLionel Sambuc  * RETURN:      A GPE EventInfo struct. NULL if not a valid GPE
310433d6423SLionel Sambuc  *
311433d6423SLionel Sambuc  * DESCRIPTION: Returns the EventInfo struct associated with this GPE.
312433d6423SLionel Sambuc  *              Validates the GpeBlock and the GpeNumber
313433d6423SLionel Sambuc  *
314433d6423SLionel Sambuc  *              Should be called only when the GPE lists are semaphore locked
315433d6423SLionel Sambuc  *              and not subject to change.
316433d6423SLionel Sambuc  *
317433d6423SLionel Sambuc  ******************************************************************************/
318433d6423SLionel Sambuc 
319433d6423SLionel Sambuc ACPI_GPE_EVENT_INFO *
AcpiEvGetGpeEventInfo(ACPI_HANDLE GpeDevice,UINT32 GpeNumber)320433d6423SLionel Sambuc AcpiEvGetGpeEventInfo (
321433d6423SLionel Sambuc     ACPI_HANDLE             GpeDevice,
322433d6423SLionel Sambuc     UINT32                  GpeNumber)
323433d6423SLionel Sambuc {
324433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *ObjDesc;
325433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *GpeInfo;
326433d6423SLionel Sambuc     UINT32                  i;
327433d6423SLionel Sambuc 
328433d6423SLionel Sambuc 
329433d6423SLionel Sambuc     ACPI_FUNCTION_ENTRY ();
330433d6423SLionel Sambuc 
331433d6423SLionel Sambuc 
332433d6423SLionel Sambuc     /* A NULL GpeDevice means use the FADT-defined GPE block(s) */
333433d6423SLionel Sambuc 
334433d6423SLionel Sambuc     if (!GpeDevice)
335433d6423SLionel Sambuc     {
336433d6423SLionel Sambuc         /* Examine GPE Block 0 and 1 (These blocks are permanent) */
337433d6423SLionel Sambuc 
338433d6423SLionel Sambuc         for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++)
339433d6423SLionel Sambuc         {
340433d6423SLionel Sambuc             GpeInfo = AcpiEvLowGetGpeInfo (GpeNumber,
341433d6423SLionel Sambuc                         AcpiGbl_GpeFadtBlocks[i]);
342433d6423SLionel Sambuc             if (GpeInfo)
343433d6423SLionel Sambuc             {
344433d6423SLionel Sambuc                 return (GpeInfo);
345433d6423SLionel Sambuc             }
346433d6423SLionel Sambuc         }
347433d6423SLionel Sambuc 
348433d6423SLionel Sambuc         /* The GpeNumber was not in the range of either FADT GPE block */
349433d6423SLionel Sambuc 
350433d6423SLionel Sambuc         return (NULL);
351433d6423SLionel Sambuc     }
352433d6423SLionel Sambuc 
353433d6423SLionel Sambuc     /* A Non-NULL GpeDevice means this is a GPE Block Device */
354433d6423SLionel Sambuc 
355433d6423SLionel Sambuc     ObjDesc = AcpiNsGetAttachedObject ((ACPI_NAMESPACE_NODE *) GpeDevice);
356433d6423SLionel Sambuc     if (!ObjDesc ||
357433d6423SLionel Sambuc         !ObjDesc->Device.GpeBlock)
358433d6423SLionel Sambuc     {
359433d6423SLionel Sambuc         return (NULL);
360433d6423SLionel Sambuc     }
361433d6423SLionel Sambuc 
362433d6423SLionel Sambuc     return (AcpiEvLowGetGpeInfo (GpeNumber, ObjDesc->Device.GpeBlock));
363433d6423SLionel Sambuc }
364433d6423SLionel Sambuc 
365433d6423SLionel Sambuc 
366433d6423SLionel Sambuc /*******************************************************************************
367433d6423SLionel Sambuc  *
368433d6423SLionel Sambuc  * FUNCTION:    AcpiEvGpeDetect
369433d6423SLionel Sambuc  *
370433d6423SLionel Sambuc  * PARAMETERS:  GpeXruptList        - Interrupt block for this interrupt.
371433d6423SLionel Sambuc  *                                    Can have multiple GPE blocks attached.
372433d6423SLionel Sambuc  *
373433d6423SLionel Sambuc  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
374433d6423SLionel Sambuc  *
375433d6423SLionel Sambuc  * DESCRIPTION: Detect if any GP events have occurred. This function is
376433d6423SLionel Sambuc  *              executed at interrupt level.
377433d6423SLionel Sambuc  *
378433d6423SLionel Sambuc  ******************************************************************************/
379433d6423SLionel Sambuc 
380433d6423SLionel Sambuc UINT32
AcpiEvGpeDetect(ACPI_GPE_XRUPT_INFO * GpeXruptList)381433d6423SLionel Sambuc AcpiEvGpeDetect (
382433d6423SLionel Sambuc     ACPI_GPE_XRUPT_INFO     *GpeXruptList)
383433d6423SLionel Sambuc {
384433d6423SLionel Sambuc     ACPI_STATUS             Status;
385433d6423SLionel Sambuc     ACPI_GPE_BLOCK_INFO     *GpeBlock;
386433d6423SLionel Sambuc     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
387433d6423SLionel Sambuc     UINT32                  IntStatus = ACPI_INTERRUPT_NOT_HANDLED;
388433d6423SLionel Sambuc     UINT8                   EnabledStatusByte;
389433d6423SLionel Sambuc     UINT32                  StatusReg;
390433d6423SLionel Sambuc     UINT32                  EnableReg;
391433d6423SLionel Sambuc     ACPI_CPU_FLAGS          Flags;
392433d6423SLionel Sambuc     UINT32                  i;
393433d6423SLionel Sambuc     UINT32                  j;
394433d6423SLionel Sambuc 
395433d6423SLionel Sambuc 
396433d6423SLionel Sambuc     ACPI_FUNCTION_NAME (EvGpeDetect);
397433d6423SLionel Sambuc 
398433d6423SLionel Sambuc     /* Check for the case where there are no GPEs */
399433d6423SLionel Sambuc 
400433d6423SLionel Sambuc     if (!GpeXruptList)
401433d6423SLionel Sambuc     {
402433d6423SLionel Sambuc         return (IntStatus);
403433d6423SLionel Sambuc     }
404433d6423SLionel Sambuc 
405433d6423SLionel Sambuc     /*
406433d6423SLionel Sambuc      * We need to obtain the GPE lock for both the data structs and registers
407433d6423SLionel Sambuc      * Note: Not necessary to obtain the hardware lock, since the GPE
408433d6423SLionel Sambuc      * registers are owned by the GpeLock.
409433d6423SLionel Sambuc      */
410433d6423SLionel Sambuc     Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
411433d6423SLionel Sambuc 
412433d6423SLionel Sambuc     /* Examine all GPE blocks attached to this interrupt level */
413433d6423SLionel Sambuc 
414433d6423SLionel Sambuc     GpeBlock = GpeXruptList->GpeBlockListHead;
415433d6423SLionel Sambuc     while (GpeBlock)
416433d6423SLionel Sambuc     {
417433d6423SLionel Sambuc         /*
418433d6423SLionel Sambuc          * Read all of the 8-bit GPE status and enable registers in this GPE
419433d6423SLionel Sambuc          * block, saving all of them. Find all currently active GP events.
420433d6423SLionel Sambuc          */
421433d6423SLionel Sambuc         for (i = 0; i < GpeBlock->RegisterCount; i++)
422433d6423SLionel Sambuc         {
423433d6423SLionel Sambuc             /* Get the next status/enable pair */
424433d6423SLionel Sambuc 
425433d6423SLionel Sambuc             GpeRegisterInfo = &GpeBlock->RegisterInfo[i];
426433d6423SLionel Sambuc 
427*29492bb7SDavid van Moolenbroek             /*
428*29492bb7SDavid van Moolenbroek              * Optimization: If there are no GPEs enabled within this
429*29492bb7SDavid van Moolenbroek              * register, we can safely ignore the entire register.
430*29492bb7SDavid van Moolenbroek              */
431*29492bb7SDavid van Moolenbroek             if (!(GpeRegisterInfo->EnableForRun |
432*29492bb7SDavid van Moolenbroek                   GpeRegisterInfo->EnableForWake))
433*29492bb7SDavid van Moolenbroek             {
434*29492bb7SDavid van Moolenbroek                 ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
435*29492bb7SDavid van Moolenbroek                     "Ignore disabled registers for GPE %02X-%02X: "
436*29492bb7SDavid van Moolenbroek                     "RunEnable=%02X, WakeEnable=%02X\n",
437*29492bb7SDavid van Moolenbroek                     GpeRegisterInfo->BaseGpeNumber,
438*29492bb7SDavid van Moolenbroek                     GpeRegisterInfo->BaseGpeNumber + (ACPI_GPE_REGISTER_WIDTH - 1),
439*29492bb7SDavid van Moolenbroek                     GpeRegisterInfo->EnableForRun,
440*29492bb7SDavid van Moolenbroek                     GpeRegisterInfo->EnableForWake));
441*29492bb7SDavid van Moolenbroek                 continue;
442*29492bb7SDavid van Moolenbroek             }
443*29492bb7SDavid van Moolenbroek 
444433d6423SLionel Sambuc             /* Read the Status Register */
445433d6423SLionel Sambuc 
446433d6423SLionel Sambuc             Status = AcpiHwRead (&StatusReg, &GpeRegisterInfo->StatusAddress);
447433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
448433d6423SLionel Sambuc             {
449433d6423SLionel Sambuc                 goto UnlockAndExit;
450433d6423SLionel Sambuc             }
451433d6423SLionel Sambuc 
452433d6423SLionel Sambuc             /* Read the Enable Register */
453433d6423SLionel Sambuc 
454433d6423SLionel Sambuc             Status = AcpiHwRead (&EnableReg, &GpeRegisterInfo->EnableAddress);
455433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
456433d6423SLionel Sambuc             {
457433d6423SLionel Sambuc                 goto UnlockAndExit;
458433d6423SLionel Sambuc             }
459433d6423SLionel Sambuc 
460433d6423SLionel Sambuc             ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
461*29492bb7SDavid van Moolenbroek                 "Read registers for GPE %02X-%02X: Status=%02X, Enable=%02X, "
462*29492bb7SDavid van Moolenbroek                 "RunEnable=%02X, WakeEnable=%02X\n",
463*29492bb7SDavid van Moolenbroek                 GpeRegisterInfo->BaseGpeNumber,
464*29492bb7SDavid van Moolenbroek                 GpeRegisterInfo->BaseGpeNumber + (ACPI_GPE_REGISTER_WIDTH - 1),
465*29492bb7SDavid van Moolenbroek                 StatusReg, EnableReg,
466*29492bb7SDavid van Moolenbroek                 GpeRegisterInfo->EnableForRun,
467*29492bb7SDavid van Moolenbroek                 GpeRegisterInfo->EnableForWake));
468433d6423SLionel Sambuc 
469433d6423SLionel Sambuc             /* Check if there is anything active at all in this register */
470433d6423SLionel Sambuc 
471433d6423SLionel Sambuc             EnabledStatusByte = (UINT8) (StatusReg & EnableReg);
472433d6423SLionel Sambuc             if (!EnabledStatusByte)
473433d6423SLionel Sambuc             {
474433d6423SLionel Sambuc                 /* No active GPEs in this register, move on */
475433d6423SLionel Sambuc 
476433d6423SLionel Sambuc                 continue;
477433d6423SLionel Sambuc             }
478433d6423SLionel Sambuc 
479433d6423SLionel Sambuc             /* Now look at the individual GPEs in this byte register */
480433d6423SLionel Sambuc 
481433d6423SLionel Sambuc             for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
482433d6423SLionel Sambuc             {
483433d6423SLionel Sambuc                 /* Examine one GPE bit */
484433d6423SLionel Sambuc 
485433d6423SLionel Sambuc                 if (EnabledStatusByte & (1 << j))
486433d6423SLionel Sambuc                 {
487433d6423SLionel Sambuc                     /*
488433d6423SLionel Sambuc                      * Found an active GPE. Dispatch the event to a handler
489433d6423SLionel Sambuc                      * or method.
490433d6423SLionel Sambuc                      */
491*29492bb7SDavid van Moolenbroek                     IntStatus |= AcpiEvGpeDispatch (GpeBlock->Node,
492433d6423SLionel Sambuc                         &GpeBlock->EventInfo[((ACPI_SIZE) i *
493433d6423SLionel Sambuc                             ACPI_GPE_REGISTER_WIDTH) + j],
494433d6423SLionel Sambuc                         j + GpeRegisterInfo->BaseGpeNumber);
495433d6423SLionel Sambuc                 }
496433d6423SLionel Sambuc             }
497433d6423SLionel Sambuc         }
498433d6423SLionel Sambuc 
499433d6423SLionel Sambuc         GpeBlock = GpeBlock->Next;
500433d6423SLionel Sambuc     }
501433d6423SLionel Sambuc 
502433d6423SLionel Sambuc UnlockAndExit:
503433d6423SLionel Sambuc 
504433d6423SLionel Sambuc     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
505433d6423SLionel Sambuc     return (IntStatus);
506433d6423SLionel Sambuc }
507433d6423SLionel Sambuc 
508433d6423SLionel Sambuc 
509433d6423SLionel Sambuc /*******************************************************************************
510433d6423SLionel Sambuc  *
511433d6423SLionel Sambuc  * FUNCTION:    AcpiEvAsynchExecuteGpeMethod
512433d6423SLionel Sambuc  *
513433d6423SLionel Sambuc  * PARAMETERS:  Context (GpeEventInfo) - Info for this GPE
514433d6423SLionel Sambuc  *
515433d6423SLionel Sambuc  * RETURN:      None
516433d6423SLionel Sambuc  *
517433d6423SLionel Sambuc  * DESCRIPTION: Perform the actual execution of a GPE control method. This
518433d6423SLionel Sambuc  *              function is called from an invocation of AcpiOsExecute and
519433d6423SLionel Sambuc  *              therefore does NOT execute at interrupt level - so that
520433d6423SLionel Sambuc  *              the control method itself is not executed in the context of
521433d6423SLionel Sambuc  *              an interrupt handler.
522433d6423SLionel Sambuc  *
523433d6423SLionel Sambuc  ******************************************************************************/
524433d6423SLionel Sambuc 
525433d6423SLionel Sambuc static void ACPI_SYSTEM_XFACE
AcpiEvAsynchExecuteGpeMethod(void * Context)526433d6423SLionel Sambuc AcpiEvAsynchExecuteGpeMethod (
527433d6423SLionel Sambuc     void                    *Context)
528433d6423SLionel Sambuc {
529433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *GpeEventInfo = Context;
530433d6423SLionel Sambuc     ACPI_STATUS             Status;
531433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *LocalGpeEventInfo;
532433d6423SLionel Sambuc     ACPI_EVALUATE_INFO      *Info;
533*29492bb7SDavid van Moolenbroek     ACPI_GPE_NOTIFY_INFO    *Notify;
534433d6423SLionel Sambuc 
535433d6423SLionel Sambuc 
536433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (EvAsynchExecuteGpeMethod);
537433d6423SLionel Sambuc 
538433d6423SLionel Sambuc 
539433d6423SLionel Sambuc     /* Allocate a local GPE block */
540433d6423SLionel Sambuc 
541433d6423SLionel Sambuc     LocalGpeEventInfo = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_EVENT_INFO));
542433d6423SLionel Sambuc     if (!LocalGpeEventInfo)
543433d6423SLionel Sambuc     {
544433d6423SLionel Sambuc         ACPI_EXCEPTION ((AE_INFO, AE_NO_MEMORY,
545433d6423SLionel Sambuc             "while handling a GPE"));
546433d6423SLionel Sambuc         return_VOID;
547433d6423SLionel Sambuc     }
548433d6423SLionel Sambuc 
549433d6423SLionel Sambuc     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
550433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
551433d6423SLionel Sambuc     {
552*29492bb7SDavid van Moolenbroek         ACPI_FREE (LocalGpeEventInfo);
553433d6423SLionel Sambuc         return_VOID;
554433d6423SLionel Sambuc     }
555433d6423SLionel Sambuc 
556433d6423SLionel Sambuc     /* Must revalidate the GpeNumber/GpeBlock */
557433d6423SLionel Sambuc 
558433d6423SLionel Sambuc     if (!AcpiEvValidGpeEvent (GpeEventInfo))
559433d6423SLionel Sambuc     {
560433d6423SLionel Sambuc         Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
561*29492bb7SDavid van Moolenbroek         ACPI_FREE (LocalGpeEventInfo);
562433d6423SLionel Sambuc         return_VOID;
563433d6423SLionel Sambuc     }
564433d6423SLionel Sambuc 
565433d6423SLionel Sambuc     /*
566433d6423SLionel Sambuc      * Take a snapshot of the GPE info for this level - we copy the info to
567433d6423SLionel Sambuc      * prevent a race condition with RemoveHandler/RemoveBlock.
568433d6423SLionel Sambuc      */
569433d6423SLionel Sambuc     ACPI_MEMCPY (LocalGpeEventInfo, GpeEventInfo,
570433d6423SLionel Sambuc         sizeof (ACPI_GPE_EVENT_INFO));
571433d6423SLionel Sambuc 
572433d6423SLionel Sambuc     Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
573433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
574433d6423SLionel Sambuc     {
575*29492bb7SDavid van Moolenbroek         ACPI_FREE (LocalGpeEventInfo);
576433d6423SLionel Sambuc         return_VOID;
577433d6423SLionel Sambuc     }
578433d6423SLionel Sambuc 
579*29492bb7SDavid van Moolenbroek     /* Do the correct dispatch - normal method or implicit notify */
580*29492bb7SDavid van Moolenbroek 
581*29492bb7SDavid van Moolenbroek     switch (LocalGpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK)
582433d6423SLionel Sambuc     {
583*29492bb7SDavid van Moolenbroek     case ACPI_GPE_DISPATCH_NOTIFY:
584*29492bb7SDavid van Moolenbroek         /*
585*29492bb7SDavid van Moolenbroek          * Implicit notify.
586*29492bb7SDavid van Moolenbroek          * Dispatch a DEVICE_WAKE notify to the appropriate handler.
587*29492bb7SDavid van Moolenbroek          * NOTE: the request is queued for execution after this method
588*29492bb7SDavid van Moolenbroek          * completes. The notify handlers are NOT invoked synchronously
589*29492bb7SDavid van Moolenbroek          * from this thread -- because handlers may in turn run other
590*29492bb7SDavid van Moolenbroek          * control methods.
591*29492bb7SDavid van Moolenbroek          *
592*29492bb7SDavid van Moolenbroek          * June 2012: Expand implicit notify mechanism to support
593*29492bb7SDavid van Moolenbroek          * notifies on multiple device objects.
594*29492bb7SDavid van Moolenbroek          */
595*29492bb7SDavid van Moolenbroek         Notify = LocalGpeEventInfo->Dispatch.NotifyList;
596*29492bb7SDavid van Moolenbroek         while (ACPI_SUCCESS (Status) && Notify)
597*29492bb7SDavid van Moolenbroek         {
598*29492bb7SDavid van Moolenbroek             Status = AcpiEvQueueNotifyRequest (Notify->DeviceNode,
599*29492bb7SDavid van Moolenbroek                         ACPI_NOTIFY_DEVICE_WAKE);
600*29492bb7SDavid van Moolenbroek 
601*29492bb7SDavid van Moolenbroek             Notify = Notify->Next;
602*29492bb7SDavid van Moolenbroek         }
603*29492bb7SDavid van Moolenbroek         break;
604*29492bb7SDavid van Moolenbroek 
605*29492bb7SDavid van Moolenbroek     case ACPI_GPE_DISPATCH_METHOD:
606*29492bb7SDavid van Moolenbroek 
607433d6423SLionel Sambuc         /* Allocate the evaluation information block */
608433d6423SLionel Sambuc 
609433d6423SLionel Sambuc         Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
610433d6423SLionel Sambuc         if (!Info)
611433d6423SLionel Sambuc         {
612433d6423SLionel Sambuc             Status = AE_NO_MEMORY;
613433d6423SLionel Sambuc         }
614433d6423SLionel Sambuc         else
615433d6423SLionel Sambuc         {
616433d6423SLionel Sambuc             /*
617*29492bb7SDavid van Moolenbroek              * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
618*29492bb7SDavid van Moolenbroek              * _Lxx/_Exx control method that corresponds to this GPE
619433d6423SLionel Sambuc              */
620433d6423SLionel Sambuc             Info->PrefixNode = LocalGpeEventInfo->Dispatch.MethodNode;
621433d6423SLionel Sambuc             Info->Flags = ACPI_IGNORE_RETURN_VALUE;
622433d6423SLionel Sambuc 
623433d6423SLionel Sambuc             Status = AcpiNsEvaluate (Info);
624433d6423SLionel Sambuc             ACPI_FREE (Info);
625433d6423SLionel Sambuc         }
626433d6423SLionel Sambuc 
627433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
628433d6423SLionel Sambuc         {
629433d6423SLionel Sambuc             ACPI_EXCEPTION ((AE_INFO, Status,
630433d6423SLionel Sambuc                 "while evaluating GPE method [%4.4s]",
631433d6423SLionel Sambuc                 AcpiUtGetNodeName (LocalGpeEventInfo->Dispatch.MethodNode)));
632433d6423SLionel Sambuc         }
633*29492bb7SDavid van Moolenbroek         break;
634*29492bb7SDavid van Moolenbroek 
635*29492bb7SDavid van Moolenbroek     default:
636*29492bb7SDavid van Moolenbroek 
637*29492bb7SDavid van Moolenbroek         return_VOID; /* Should never happen */
638433d6423SLionel Sambuc     }
639433d6423SLionel Sambuc 
640433d6423SLionel Sambuc     /* Defer enabling of GPE until all notify handlers are done */
641433d6423SLionel Sambuc 
642433d6423SLionel Sambuc     Status = AcpiOsExecute (OSL_NOTIFY_HANDLER,
643433d6423SLionel Sambuc                 AcpiEvAsynchEnableGpe, LocalGpeEventInfo);
644433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
645433d6423SLionel Sambuc     {
646433d6423SLionel Sambuc         ACPI_FREE (LocalGpeEventInfo);
647433d6423SLionel Sambuc     }
648433d6423SLionel Sambuc     return_VOID;
649433d6423SLionel Sambuc }
650433d6423SLionel Sambuc 
651433d6423SLionel Sambuc 
652433d6423SLionel Sambuc /*******************************************************************************
653433d6423SLionel Sambuc  *
654433d6423SLionel Sambuc  * FUNCTION:    AcpiEvAsynchEnableGpe
655433d6423SLionel Sambuc  *
656433d6423SLionel Sambuc  * PARAMETERS:  Context (GpeEventInfo) - Info for this GPE
657*29492bb7SDavid van Moolenbroek  *              Callback from AcpiOsExecute
658433d6423SLionel Sambuc  *
659433d6423SLionel Sambuc  * RETURN:      None
660433d6423SLionel Sambuc  *
661433d6423SLionel Sambuc  * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
662433d6423SLionel Sambuc  *              complete (i.e., finish execution of Notify)
663433d6423SLionel Sambuc  *
664433d6423SLionel Sambuc  ******************************************************************************/
665433d6423SLionel Sambuc 
666433d6423SLionel Sambuc static void ACPI_SYSTEM_XFACE
AcpiEvAsynchEnableGpe(void * Context)667433d6423SLionel Sambuc AcpiEvAsynchEnableGpe (
668433d6423SLionel Sambuc     void                    *Context)
669433d6423SLionel Sambuc {
670433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *GpeEventInfo = Context;
671433d6423SLionel Sambuc 
672433d6423SLionel Sambuc 
673*29492bb7SDavid van Moolenbroek     (void) AcpiEvFinishGpe (GpeEventInfo);
674433d6423SLionel Sambuc 
675433d6423SLionel Sambuc     ACPI_FREE (GpeEventInfo);
676433d6423SLionel Sambuc     return;
677433d6423SLionel Sambuc }
678433d6423SLionel Sambuc 
679433d6423SLionel Sambuc 
680433d6423SLionel Sambuc /*******************************************************************************
681433d6423SLionel Sambuc  *
682*29492bb7SDavid van Moolenbroek  * FUNCTION:    AcpiEvFinishGpe
683433d6423SLionel Sambuc  *
684433d6423SLionel Sambuc  * PARAMETERS:  GpeEventInfo        - Info for this GPE
685*29492bb7SDavid van Moolenbroek  *
686*29492bb7SDavid van Moolenbroek  * RETURN:      Status
687*29492bb7SDavid van Moolenbroek  *
688*29492bb7SDavid van Moolenbroek  * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
689*29492bb7SDavid van Moolenbroek  *              of a GPE method or a synchronous or asynchronous GPE handler.
690*29492bb7SDavid van Moolenbroek  *
691*29492bb7SDavid van Moolenbroek  ******************************************************************************/
692*29492bb7SDavid van Moolenbroek 
693*29492bb7SDavid van Moolenbroek ACPI_STATUS
AcpiEvFinishGpe(ACPI_GPE_EVENT_INFO * GpeEventInfo)694*29492bb7SDavid van Moolenbroek AcpiEvFinishGpe (
695*29492bb7SDavid van Moolenbroek     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
696*29492bb7SDavid van Moolenbroek {
697*29492bb7SDavid van Moolenbroek     ACPI_STATUS             Status;
698*29492bb7SDavid van Moolenbroek 
699*29492bb7SDavid van Moolenbroek 
700*29492bb7SDavid van Moolenbroek     if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
701*29492bb7SDavid van Moolenbroek             ACPI_GPE_LEVEL_TRIGGERED)
702*29492bb7SDavid van Moolenbroek     {
703*29492bb7SDavid van Moolenbroek         /*
704*29492bb7SDavid van Moolenbroek          * GPE is level-triggered, we clear the GPE status bit after
705*29492bb7SDavid van Moolenbroek          * handling the event.
706*29492bb7SDavid van Moolenbroek          */
707*29492bb7SDavid van Moolenbroek         Status = AcpiHwClearGpe (GpeEventInfo);
708*29492bb7SDavid van Moolenbroek         if (ACPI_FAILURE (Status))
709*29492bb7SDavid van Moolenbroek         {
710*29492bb7SDavid van Moolenbroek             return (Status);
711*29492bb7SDavid van Moolenbroek         }
712*29492bb7SDavid van Moolenbroek     }
713*29492bb7SDavid van Moolenbroek 
714*29492bb7SDavid van Moolenbroek     /*
715*29492bb7SDavid van Moolenbroek      * Enable this GPE, conditionally. This means that the GPE will
716*29492bb7SDavid van Moolenbroek      * only be physically enabled if the EnableForRun bit is set
717*29492bb7SDavid van Moolenbroek      * in the EventInfo.
718*29492bb7SDavid van Moolenbroek      */
719*29492bb7SDavid van Moolenbroek     (void) AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_CONDITIONAL_ENABLE);
720*29492bb7SDavid van Moolenbroek     return (AE_OK);
721*29492bb7SDavid van Moolenbroek }
722*29492bb7SDavid van Moolenbroek 
723*29492bb7SDavid van Moolenbroek 
724*29492bb7SDavid van Moolenbroek /*******************************************************************************
725*29492bb7SDavid van Moolenbroek  *
726*29492bb7SDavid van Moolenbroek  * FUNCTION:    AcpiEvGpeDispatch
727*29492bb7SDavid van Moolenbroek  *
728*29492bb7SDavid van Moolenbroek  * PARAMETERS:  GpeDevice           - Device node. NULL for GPE0/GPE1
729*29492bb7SDavid van Moolenbroek  *              GpeEventInfo        - Info for this GPE
730433d6423SLionel Sambuc  *              GpeNumber           - Number relative to the parent GPE block
731433d6423SLionel Sambuc  *
732433d6423SLionel Sambuc  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
733433d6423SLionel Sambuc  *
734433d6423SLionel Sambuc  * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
735433d6423SLionel Sambuc  *              or method (e.g. _Lxx/_Exx) handler.
736433d6423SLionel Sambuc  *
737433d6423SLionel Sambuc  *              This function executes at interrupt level.
738433d6423SLionel Sambuc  *
739433d6423SLionel Sambuc  ******************************************************************************/
740433d6423SLionel Sambuc 
741433d6423SLionel Sambuc UINT32
AcpiEvGpeDispatch(ACPI_NAMESPACE_NODE * GpeDevice,ACPI_GPE_EVENT_INFO * GpeEventInfo,UINT32 GpeNumber)742433d6423SLionel Sambuc AcpiEvGpeDispatch (
743*29492bb7SDavid van Moolenbroek     ACPI_NAMESPACE_NODE     *GpeDevice,
744433d6423SLionel Sambuc     ACPI_GPE_EVENT_INFO     *GpeEventInfo,
745433d6423SLionel Sambuc     UINT32                  GpeNumber)
746433d6423SLionel Sambuc {
747433d6423SLionel Sambuc     ACPI_STATUS             Status;
748*29492bb7SDavid van Moolenbroek     UINT32                  ReturnValue;
749433d6423SLionel Sambuc 
750433d6423SLionel Sambuc 
751433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (EvGpeDispatch);
752433d6423SLionel Sambuc 
753433d6423SLionel Sambuc 
754*29492bb7SDavid van Moolenbroek     /* Invoke global event handler if present */
755*29492bb7SDavid van Moolenbroek 
756433d6423SLionel Sambuc     AcpiGpeCount++;
757*29492bb7SDavid van Moolenbroek     if (AcpiGbl_GlobalEventHandler)
758*29492bb7SDavid van Moolenbroek     {
759*29492bb7SDavid van Moolenbroek         AcpiGbl_GlobalEventHandler (ACPI_EVENT_TYPE_GPE, GpeDevice,
760*29492bb7SDavid van Moolenbroek              GpeNumber, AcpiGbl_GlobalEventHandlerContext);
761*29492bb7SDavid van Moolenbroek     }
762*29492bb7SDavid van Moolenbroek 
763*29492bb7SDavid van Moolenbroek     /*
764*29492bb7SDavid van Moolenbroek      * Always disable the GPE so that it does not keep firing before
765*29492bb7SDavid van Moolenbroek      * any asynchronous activity completes (either from the execution
766*29492bb7SDavid van Moolenbroek      * of a GPE method or an asynchronous GPE handler.)
767*29492bb7SDavid van Moolenbroek      *
768*29492bb7SDavid van Moolenbroek      * If there is no handler or method to run, just disable the
769*29492bb7SDavid van Moolenbroek      * GPE and leave it disabled permanently to prevent further such
770*29492bb7SDavid van Moolenbroek      * pointless events from firing.
771*29492bb7SDavid van Moolenbroek      */
772*29492bb7SDavid van Moolenbroek     Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
773*29492bb7SDavid van Moolenbroek     if (ACPI_FAILURE (Status))
774*29492bb7SDavid van Moolenbroek     {
775*29492bb7SDavid van Moolenbroek         ACPI_EXCEPTION ((AE_INFO, Status,
776*29492bb7SDavid van Moolenbroek             "Unable to disable GPE %02X", GpeNumber));
777*29492bb7SDavid van Moolenbroek         return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
778*29492bb7SDavid van Moolenbroek     }
779433d6423SLionel Sambuc 
780433d6423SLionel Sambuc     /*
781433d6423SLionel Sambuc      * If edge-triggered, clear the GPE status bit now. Note that
782433d6423SLionel Sambuc      * level-triggered events are cleared after the GPE is serviced.
783433d6423SLionel Sambuc      */
784433d6423SLionel Sambuc     if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
785433d6423SLionel Sambuc             ACPI_GPE_EDGE_TRIGGERED)
786433d6423SLionel Sambuc     {
787433d6423SLionel Sambuc         Status = AcpiHwClearGpe (GpeEventInfo);
788433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
789433d6423SLionel Sambuc         {
790433d6423SLionel Sambuc             ACPI_EXCEPTION ((AE_INFO, Status,
791*29492bb7SDavid van Moolenbroek                 "Unable to clear GPE %02X", GpeNumber));
792*29492bb7SDavid van Moolenbroek             (void) AcpiHwLowSetGpe (GpeEventInfo,
793*29492bb7SDavid van Moolenbroek                     ACPI_GPE_CONDITIONAL_ENABLE);
794433d6423SLionel Sambuc             return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
795433d6423SLionel Sambuc         }
796433d6423SLionel Sambuc     }
797433d6423SLionel Sambuc 
798433d6423SLionel Sambuc     /*
799*29492bb7SDavid van Moolenbroek      * Dispatch the GPE to either an installed handler or the control
800*29492bb7SDavid van Moolenbroek      * method associated with this GPE (_Lxx or _Exx). If a handler
801*29492bb7SDavid van Moolenbroek      * exists, we invoke it and do not attempt to run the method.
802*29492bb7SDavid van Moolenbroek      * If there is neither a handler nor a method, leave the GPE
803*29492bb7SDavid van Moolenbroek      * disabled.
804433d6423SLionel Sambuc      */
805433d6423SLionel Sambuc     switch (GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK)
806433d6423SLionel Sambuc     {
807433d6423SLionel Sambuc     case ACPI_GPE_DISPATCH_HANDLER:
808433d6423SLionel Sambuc 
809*29492bb7SDavid van Moolenbroek         /* Invoke the installed handler (at interrupt level) */
810*29492bb7SDavid van Moolenbroek 
811*29492bb7SDavid van Moolenbroek         ReturnValue = GpeEventInfo->Dispatch.Handler->Address (
812*29492bb7SDavid van Moolenbroek             GpeDevice, GpeNumber,
813433d6423SLionel Sambuc             GpeEventInfo->Dispatch.Handler->Context);
814433d6423SLionel Sambuc 
815*29492bb7SDavid van Moolenbroek         /* If requested, clear (if level-triggered) and reenable the GPE */
816433d6423SLionel Sambuc 
817*29492bb7SDavid van Moolenbroek         if (ReturnValue & ACPI_REENABLE_GPE)
818433d6423SLionel Sambuc         {
819*29492bb7SDavid van Moolenbroek             (void) AcpiEvFinishGpe (GpeEventInfo);
820433d6423SLionel Sambuc         }
821433d6423SLionel Sambuc         break;
822433d6423SLionel Sambuc 
823433d6423SLionel Sambuc     case ACPI_GPE_DISPATCH_METHOD:
824*29492bb7SDavid van Moolenbroek     case ACPI_GPE_DISPATCH_NOTIFY:
825433d6423SLionel Sambuc         /*
826433d6423SLionel Sambuc          * Execute the method associated with the GPE
827433d6423SLionel Sambuc          * NOTE: Level-triggered GPEs are cleared after the method completes.
828433d6423SLionel Sambuc          */
829433d6423SLionel Sambuc         Status = AcpiOsExecute (OSL_GPE_HANDLER,
830433d6423SLionel Sambuc                     AcpiEvAsynchExecuteGpeMethod, GpeEventInfo);
831433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
832433d6423SLionel Sambuc         {
833433d6423SLionel Sambuc             ACPI_EXCEPTION ((AE_INFO, Status,
834*29492bb7SDavid van Moolenbroek                 "Unable to queue handler for GPE %02X - event disabled",
835433d6423SLionel Sambuc                 GpeNumber));
836433d6423SLionel Sambuc         }
837433d6423SLionel Sambuc         break;
838433d6423SLionel Sambuc 
839433d6423SLionel Sambuc     default:
840433d6423SLionel Sambuc         /*
841433d6423SLionel Sambuc          * No handler or method to run!
842433d6423SLionel Sambuc          * 03/2010: This case should no longer be possible. We will not allow
843433d6423SLionel Sambuc          * a GPE to be enabled if it has no handler or method.
844433d6423SLionel Sambuc          */
845433d6423SLionel Sambuc         ACPI_ERROR ((AE_INFO,
846*29492bb7SDavid van Moolenbroek             "No handler or method for GPE %02X, disabling event",
847433d6423SLionel Sambuc             GpeNumber));
848433d6423SLionel Sambuc         break;
849433d6423SLionel Sambuc     }
850433d6423SLionel Sambuc 
851433d6423SLionel Sambuc     return_UINT32 (ACPI_INTERRUPT_HANDLED);
852433d6423SLionel Sambuc }
853433d6423SLionel Sambuc 
854*29492bb7SDavid van Moolenbroek #endif /* !ACPI_REDUCED_HARDWARE */
855