evgpe.c revision 217365
1/******************************************************************************
2 *
3 * Module Name: evgpe - General Purpose Event handling and dispatch
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2011, Intel Corp.
9 * All rights reserved.
10 *
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.
25 *
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.
29 *
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.
42 */
43
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>
48
49#define _COMPONENT          ACPI_EVENTS
50        ACPI_MODULE_NAME    ("evgpe")
51
52/* Local prototypes */
53
54static void ACPI_SYSTEM_XFACE
55AcpiEvAsynchExecuteGpeMethod (
56    void                    *Context);
57
58static void ACPI_SYSTEM_XFACE
59AcpiEvAsynchEnableGpe (
60    void                    *Context);
61
62
63/*******************************************************************************
64 *
65 * FUNCTION:    AcpiEvUpdateGpeEnableMask
66 *
67 * PARAMETERS:  GpeEventInfo            - GPE to update
68 *
69 * RETURN:      Status
70 *
71 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
72 *              runtime references to this GPE
73 *
74 ******************************************************************************/
75
76ACPI_STATUS
77AcpiEvUpdateGpeEnableMask (
78    ACPI_GPE_EVENT_INFO     *GpeEventInfo)
79{
80    ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
81    UINT32                  RegisterBit;
82
83
84    ACPI_FUNCTION_TRACE (EvUpdateGpeEnableMask);
85
86
87    GpeRegisterInfo = GpeEventInfo->RegisterInfo;
88    if (!GpeRegisterInfo)
89    {
90        return_ACPI_STATUS (AE_NOT_EXIST);
91    }
92
93    RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo, GpeRegisterInfo);
94
95    /* Clear the run bit up front */
96
97    ACPI_CLEAR_BIT (GpeRegisterInfo->EnableForRun, RegisterBit);
98
99    /* Set the mask bit only if there are references to this GPE */
100
101    if (GpeEventInfo->RuntimeCount)
102    {
103        ACPI_SET_BIT (GpeRegisterInfo->EnableForRun, (UINT8) RegisterBit);
104    }
105
106    return_ACPI_STATUS (AE_OK);
107}
108
109
110/*******************************************************************************
111 *
112 * FUNCTION:    AcpiEvEnableGpe
113 *
114 * PARAMETERS:  GpeEventInfo            - GPE to enable
115 *
116 * RETURN:      Status
117 *
118 * DESCRIPTION: Clear a GPE of stale events and enable it.
119 *
120 ******************************************************************************/
121
122ACPI_STATUS
123AcpiEvEnableGpe (
124    ACPI_GPE_EVENT_INFO     *GpeEventInfo)
125{
126    ACPI_STATUS             Status;
127
128
129    ACPI_FUNCTION_TRACE (EvEnableGpe);
130
131
132    /*
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.
137     */
138    if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
139        ACPI_GPE_DISPATCH_NONE)
140    {
141        return_ACPI_STATUS (AE_NO_HANDLER);
142    }
143
144    /* Clear the GPE (of stale events) */
145
146    Status = AcpiHwClearGpe (GpeEventInfo);
147    if (ACPI_FAILURE (Status))
148    {
149        return_ACPI_STATUS (Status);
150    }
151
152    /* Enable the requested GPE */
153
154    Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_ENABLE);
155    return_ACPI_STATUS (Status);
156}
157
158
159/*******************************************************************************
160 *
161 * FUNCTION:    AcpiEvAddGpeReference
162 *
163 * PARAMETERS:  GpeEventInfo            - Add a reference to this GPE
164 *
165 * RETURN:      Status
166 *
167 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
168 *              hardware-enabled.
169 *
170 ******************************************************************************/
171
172ACPI_STATUS
173AcpiEvAddGpeReference (
174    ACPI_GPE_EVENT_INFO     *GpeEventInfo)
175{
176    ACPI_STATUS             Status = AE_OK;
177
178
179    ACPI_FUNCTION_TRACE (EvAddGpeReference);
180
181
182    if (GpeEventInfo->RuntimeCount == ACPI_UINT8_MAX)
183    {
184        return_ACPI_STATUS (AE_LIMIT);
185    }
186
187    GpeEventInfo->RuntimeCount++;
188    if (GpeEventInfo->RuntimeCount == 1)
189    {
190        /* Enable on first reference */
191
192        Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
193        if (ACPI_SUCCESS (Status))
194        {
195            Status = AcpiEvEnableGpe (GpeEventInfo);
196        }
197
198        if (ACPI_FAILURE (Status))
199        {
200            GpeEventInfo->RuntimeCount--;
201        }
202    }
203
204    return_ACPI_STATUS (Status);
205}
206
207
208/*******************************************************************************
209 *
210 * FUNCTION:    AcpiEvRemoveGpeReference
211 *
212 * PARAMETERS:  GpeEventInfo            - Remove a reference to this GPE
213 *
214 * RETURN:      Status
215 *
216 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
217 *              removed, the GPE is hardware-disabled.
218 *
219 ******************************************************************************/
220
221ACPI_STATUS
222AcpiEvRemoveGpeReference (
223    ACPI_GPE_EVENT_INFO     *GpeEventInfo)
224{
225    ACPI_STATUS             Status = AE_OK;
226
227
228    ACPI_FUNCTION_TRACE (EvRemoveGpeReference);
229
230
231    if (!GpeEventInfo->RuntimeCount)
232    {
233        return_ACPI_STATUS (AE_LIMIT);
234    }
235
236    GpeEventInfo->RuntimeCount--;
237    if (!GpeEventInfo->RuntimeCount)
238    {
239        /* Disable on last reference */
240
241        Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
242        if (ACPI_SUCCESS (Status))
243        {
244            Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
245        }
246
247        if (ACPI_FAILURE (Status))
248        {
249            GpeEventInfo->RuntimeCount++;
250        }
251    }
252
253    return_ACPI_STATUS (Status);
254}
255
256
257/*******************************************************************************
258 *
259 * FUNCTION:    AcpiEvLowGetGpeInfo
260 *
261 * PARAMETERS:  GpeNumber           - Raw GPE number
262 *              GpeBlock            - A GPE info block
263 *
264 * RETURN:      A GPE EventInfo struct. NULL if not a valid GPE (The GpeNumber
265 *              is not within the specified GPE block)
266 *
267 * DESCRIPTION: Returns the EventInfo struct associated with this GPE. This is
268 *              the low-level implementation of EvGetGpeEventInfo.
269 *
270 ******************************************************************************/
271
272ACPI_GPE_EVENT_INFO *
273AcpiEvLowGetGpeInfo (
274    UINT32                  GpeNumber,
275    ACPI_GPE_BLOCK_INFO     *GpeBlock)
276{
277    UINT32                  GpeIndex;
278
279
280    /*
281     * Validate that the GpeNumber is within the specified GpeBlock.
282     * (Two steps)
283     */
284    if (!GpeBlock ||
285        (GpeNumber < GpeBlock->BlockBaseNumber))
286    {
287        return (NULL);
288    }
289
290    GpeIndex = GpeNumber - GpeBlock->BlockBaseNumber;
291    if (GpeIndex >= GpeBlock->GpeCount)
292    {
293        return (NULL);
294    }
295
296    return (&GpeBlock->EventInfo[GpeIndex]);
297}
298
299
300/*******************************************************************************
301 *
302 * FUNCTION:    AcpiEvGetGpeEventInfo
303 *
304 * PARAMETERS:  GpeDevice           - Device node. NULL for GPE0/GPE1
305 *              GpeNumber           - Raw GPE number
306 *
307 * RETURN:      A GPE EventInfo struct. NULL if not a valid GPE
308 *
309 * DESCRIPTION: Returns the EventInfo struct associated with this GPE.
310 *              Validates the GpeBlock and the GpeNumber
311 *
312 *              Should be called only when the GPE lists are semaphore locked
313 *              and not subject to change.
314 *
315 ******************************************************************************/
316
317ACPI_GPE_EVENT_INFO *
318AcpiEvGetGpeEventInfo (
319    ACPI_HANDLE             GpeDevice,
320    UINT32                  GpeNumber)
321{
322    ACPI_OPERAND_OBJECT     *ObjDesc;
323    ACPI_GPE_EVENT_INFO     *GpeInfo;
324    UINT32                  i;
325
326
327    ACPI_FUNCTION_ENTRY ();
328
329
330    /* A NULL GpeDevice means use the FADT-defined GPE block(s) */
331
332    if (!GpeDevice)
333    {
334        /* Examine GPE Block 0 and 1 (These blocks are permanent) */
335
336        for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++)
337        {
338            GpeInfo = AcpiEvLowGetGpeInfo (GpeNumber,
339                        AcpiGbl_GpeFadtBlocks[i]);
340            if (GpeInfo)
341            {
342                return (GpeInfo);
343            }
344        }
345
346        /* The GpeNumber was not in the range of either FADT GPE block */
347
348        return (NULL);
349    }
350
351    /* A Non-NULL GpeDevice means this is a GPE Block Device */
352
353    ObjDesc = AcpiNsGetAttachedObject ((ACPI_NAMESPACE_NODE *) GpeDevice);
354    if (!ObjDesc ||
355        !ObjDesc->Device.GpeBlock)
356    {
357        return (NULL);
358    }
359
360    return (AcpiEvLowGetGpeInfo (GpeNumber, ObjDesc->Device.GpeBlock));
361}
362
363
364/*******************************************************************************
365 *
366 * FUNCTION:    AcpiEvGpeDetect
367 *
368 * PARAMETERS:  GpeXruptList        - Interrupt block for this interrupt.
369 *                                    Can have multiple GPE blocks attached.
370 *
371 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
372 *
373 * DESCRIPTION: Detect if any GP events have occurred. This function is
374 *              executed at interrupt level.
375 *
376 ******************************************************************************/
377
378UINT32
379AcpiEvGpeDetect (
380    ACPI_GPE_XRUPT_INFO     *GpeXruptList)
381{
382    ACPI_STATUS             Status;
383    ACPI_GPE_BLOCK_INFO     *GpeBlock;
384    ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
385    UINT32                  IntStatus = ACPI_INTERRUPT_NOT_HANDLED;
386    UINT8                   EnabledStatusByte;
387    UINT32                  StatusReg;
388    UINT32                  EnableReg;
389    ACPI_CPU_FLAGS          Flags;
390    UINT32                  i;
391    UINT32                  j;
392
393
394    ACPI_FUNCTION_NAME (EvGpeDetect);
395
396    /* Check for the case where there are no GPEs */
397
398    if (!GpeXruptList)
399    {
400        return (IntStatus);
401    }
402
403    /*
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.
407     */
408    Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
409
410    /* Examine all GPE blocks attached to this interrupt level */
411
412    GpeBlock = GpeXruptList->GpeBlockListHead;
413    while (GpeBlock)
414    {
415        /*
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.
418         */
419        for (i = 0; i < GpeBlock->RegisterCount; i++)
420        {
421            /* Get the next status/enable pair */
422
423            GpeRegisterInfo = &GpeBlock->RegisterInfo[i];
424
425            /* Read the Status Register */
426
427            Status = AcpiHwRead (&StatusReg, &GpeRegisterInfo->StatusAddress);
428            if (ACPI_FAILURE (Status))
429            {
430                goto UnlockAndExit;
431            }
432
433            /* Read the Enable Register */
434
435            Status = AcpiHwRead (&EnableReg, &GpeRegisterInfo->EnableAddress);
436            if (ACPI_FAILURE (Status))
437            {
438                goto UnlockAndExit;
439            }
440
441            ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
442                "Read GPE Register at GPE%02X: Status=%02X, Enable=%02X\n",
443                GpeRegisterInfo->BaseGpeNumber, StatusReg, EnableReg));
444
445            /* Check if there is anything active at all in this register */
446
447            EnabledStatusByte = (UINT8) (StatusReg & EnableReg);
448            if (!EnabledStatusByte)
449            {
450                /* No active GPEs in this register, move on */
451
452                continue;
453            }
454
455            /* Now look at the individual GPEs in this byte register */
456
457            for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
458            {
459                /* Examine one GPE bit */
460
461                if (EnabledStatusByte & (1 << j))
462                {
463                    /*
464                     * Found an active GPE. Dispatch the event to a handler
465                     * or method.
466                     */
467                    IntStatus |= AcpiEvGpeDispatch (GpeBlock->Node,
468                        &GpeBlock->EventInfo[((ACPI_SIZE) i *
469                            ACPI_GPE_REGISTER_WIDTH) + j],
470                        j + GpeRegisterInfo->BaseGpeNumber);
471                }
472            }
473        }
474
475        GpeBlock = GpeBlock->Next;
476    }
477
478UnlockAndExit:
479
480    AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
481    return (IntStatus);
482}
483
484
485/*******************************************************************************
486 *
487 * FUNCTION:    AcpiEvAsynchExecuteGpeMethod
488 *
489 * PARAMETERS:  Context (GpeEventInfo) - Info for this GPE
490 *
491 * RETURN:      None
492 *
493 * DESCRIPTION: Perform the actual execution of a GPE control method. This
494 *              function is called from an invocation of AcpiOsExecute and
495 *              therefore does NOT execute at interrupt level - so that
496 *              the control method itself is not executed in the context of
497 *              an interrupt handler.
498 *
499 ******************************************************************************/
500
501static void ACPI_SYSTEM_XFACE
502AcpiEvAsynchExecuteGpeMethod (
503    void                    *Context)
504{
505    ACPI_GPE_EVENT_INFO     *GpeEventInfo = Context;
506    ACPI_STATUS             Status;
507    ACPI_GPE_EVENT_INFO     *LocalGpeEventInfo;
508    ACPI_EVALUATE_INFO      *Info;
509
510
511    ACPI_FUNCTION_TRACE (EvAsynchExecuteGpeMethod);
512
513
514    /* Allocate a local GPE block */
515
516    LocalGpeEventInfo = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_EVENT_INFO));
517    if (!LocalGpeEventInfo)
518    {
519        ACPI_EXCEPTION ((AE_INFO, AE_NO_MEMORY,
520            "while handling a GPE"));
521        return_VOID;
522    }
523
524    Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
525    if (ACPI_FAILURE (Status))
526    {
527        return_VOID;
528    }
529
530    /* Must revalidate the GpeNumber/GpeBlock */
531
532    if (!AcpiEvValidGpeEvent (GpeEventInfo))
533    {
534        Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
535        return_VOID;
536    }
537
538    /*
539     * Take a snapshot of the GPE info for this level - we copy the info to
540     * prevent a race condition with RemoveHandler/RemoveBlock.
541     */
542    ACPI_MEMCPY (LocalGpeEventInfo, GpeEventInfo,
543        sizeof (ACPI_GPE_EVENT_INFO));
544
545    Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
546    if (ACPI_FAILURE (Status))
547    {
548        return_VOID;
549    }
550
551    /* Do the correct dispatch - normal method or implicit notify */
552
553    switch (LocalGpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK)
554    {
555    case ACPI_GPE_DISPATCH_NOTIFY:
556
557        /*
558         * Implicit notify.
559         * Dispatch a DEVICE_WAKE notify to the appropriate handler.
560         * NOTE: the request is queued for execution after this method
561         * completes. The notify handlers are NOT invoked synchronously
562         * from this thread -- because handlers may in turn run other
563         * control methods.
564         */
565        Status = AcpiEvQueueNotifyRequest (
566                    LocalGpeEventInfo->Dispatch.DeviceNode,
567                    ACPI_NOTIFY_DEVICE_WAKE);
568        break;
569
570    case ACPI_GPE_DISPATCH_METHOD:
571
572        /* Allocate the evaluation information block */
573
574        Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
575        if (!Info)
576        {
577            Status = AE_NO_MEMORY;
578        }
579        else
580        {
581            /*
582             * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
583             * _Lxx/_Exx control method that corresponds to this GPE
584             */
585            Info->PrefixNode = LocalGpeEventInfo->Dispatch.MethodNode;
586            Info->Flags = ACPI_IGNORE_RETURN_VALUE;
587
588            Status = AcpiNsEvaluate (Info);
589            ACPI_FREE (Info);
590        }
591
592        if (ACPI_FAILURE (Status))
593        {
594            ACPI_EXCEPTION ((AE_INFO, Status,
595                "while evaluating GPE method [%4.4s]",
596                AcpiUtGetNodeName (LocalGpeEventInfo->Dispatch.MethodNode)));
597        }
598
599        break;
600
601    default:
602        return_VOID; /* Should never happen */
603    }
604
605    /* Defer enabling of GPE until all notify handlers are done */
606
607    Status = AcpiOsExecute (OSL_NOTIFY_HANDLER,
608                AcpiEvAsynchEnableGpe, LocalGpeEventInfo);
609    if (ACPI_FAILURE (Status))
610    {
611        ACPI_FREE (LocalGpeEventInfo);
612    }
613    return_VOID;
614}
615
616
617/*******************************************************************************
618 *
619 * FUNCTION:    AcpiEvAsynchEnableGpe
620 *
621 * PARAMETERS:  Context (GpeEventInfo) - Info for this GPE
622 *              Callback from AcpiOsExecute
623 *
624 * RETURN:      None
625 *
626 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
627 *              complete (i.e., finish execution of Notify)
628 *
629 ******************************************************************************/
630
631static void ACPI_SYSTEM_XFACE
632AcpiEvAsynchEnableGpe (
633    void                    *Context)
634{
635    ACPI_GPE_EVENT_INFO     *GpeEventInfo = Context;
636
637
638    (void) AcpiEvFinishGpe (GpeEventInfo);
639
640    ACPI_FREE (GpeEventInfo);
641    return;
642}
643
644
645/*******************************************************************************
646 *
647 * FUNCTION:    AcpiEvFinishGpe
648 *
649 * PARAMETERS:  GpeEventInfo        - Info for this GPE
650 *
651 * RETURN:      Status
652 *
653 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
654 *              of a GPE method or a synchronous or asynchronous GPE handler.
655 *
656 ******************************************************************************/
657
658ACPI_STATUS
659AcpiEvFinishGpe (
660    ACPI_GPE_EVENT_INFO     *GpeEventInfo)
661{
662    ACPI_STATUS             Status;
663
664
665    if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
666            ACPI_GPE_LEVEL_TRIGGERED)
667    {
668        /*
669         * GPE is level-triggered, we clear the GPE status bit after
670         * handling the event.
671         */
672        Status = AcpiHwClearGpe (GpeEventInfo);
673        if (ACPI_FAILURE (Status))
674        {
675            return (Status);
676        }
677    }
678
679    /*
680     * Enable this GPE, conditionally. This means that the GPE will
681     * only be physically enabled if the EnableForRun bit is set
682     * in the EventInfo.
683     */
684    (void) AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_CONDITIONAL_ENABLE);
685    return (AE_OK);
686}
687
688
689/*******************************************************************************
690 *
691 * FUNCTION:    AcpiEvGpeDispatch
692 *
693 * PARAMETERS:  GpeDevice           - Device node. NULL for GPE0/GPE1
694 *              GpeEventInfo        - Info for this GPE
695 *              GpeNumber           - Number relative to the parent GPE block
696 *
697 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
698 *
699 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
700 *              or method (e.g. _Lxx/_Exx) handler.
701 *
702 *              This function executes at interrupt level.
703 *
704 ******************************************************************************/
705
706UINT32
707AcpiEvGpeDispatch (
708    ACPI_NAMESPACE_NODE     *GpeDevice,
709    ACPI_GPE_EVENT_INFO     *GpeEventInfo,
710    UINT32                  GpeNumber)
711{
712    ACPI_STATUS             Status;
713    UINT32                  ReturnValue;
714
715
716    ACPI_FUNCTION_TRACE (EvGpeDispatch);
717
718
719    /* Invoke global event handler if present */
720
721    AcpiGpeCount++;
722    if (AcpiGbl_GlobalEventHandler)
723    {
724        AcpiGbl_GlobalEventHandler (ACPI_EVENT_TYPE_GPE, GpeDevice,
725             GpeNumber, AcpiGbl_GlobalEventHandlerContext);
726    }
727
728    /*
729     * If edge-triggered, clear the GPE status bit now. Note that
730     * level-triggered events are cleared after the GPE is serviced.
731     */
732    if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
733            ACPI_GPE_EDGE_TRIGGERED)
734    {
735        Status = AcpiHwClearGpe (GpeEventInfo);
736        if (ACPI_FAILURE (Status))
737        {
738            ACPI_EXCEPTION ((AE_INFO, Status,
739                "Unable to clear GPE%02X", GpeNumber));
740            return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
741        }
742    }
743
744    /*
745     * Always disable the GPE so that it does not keep firing before
746     * any asynchronous activity completes (either from the execution
747     * of a GPE method or an asynchronous GPE handler.)
748     *
749     * If there is no handler or method to run, just disable the
750     * GPE and leave it disabled permanently to prevent further such
751     * pointless events from firing.
752     */
753    Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
754    if (ACPI_FAILURE (Status))
755    {
756        ACPI_EXCEPTION ((AE_INFO, Status,
757            "Unable to disable GPE%02X", GpeNumber));
758        return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
759    }
760
761    /*
762     * Dispatch the GPE to either an installed handler or the control
763     * method associated with this GPE (_Lxx or _Exx). If a handler
764     * exists, we invoke it and do not attempt to run the method.
765     * If there is neither a handler nor a method, leave the GPE
766     * disabled.
767     */
768    switch (GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK)
769    {
770    case ACPI_GPE_DISPATCH_HANDLER:
771
772        /* Invoke the installed handler (at interrupt level) */
773
774        ReturnValue = GpeEventInfo->Dispatch.Handler->Address (
775            GpeDevice, GpeNumber,
776            GpeEventInfo->Dispatch.Handler->Context);
777
778        /* If requested, clear (if level-triggered) and reenable the GPE */
779
780        if (ReturnValue & ACPI_REENABLE_GPE)
781        {
782            (void) AcpiEvFinishGpe (GpeEventInfo);
783        }
784        break;
785
786    case ACPI_GPE_DISPATCH_METHOD:
787    case ACPI_GPE_DISPATCH_NOTIFY:
788
789        /*
790         * Execute the method associated with the GPE
791         * NOTE: Level-triggered GPEs are cleared after the method completes.
792         */
793        Status = AcpiOsExecute (OSL_GPE_HANDLER,
794                    AcpiEvAsynchExecuteGpeMethod, GpeEventInfo);
795        if (ACPI_FAILURE (Status))
796        {
797            ACPI_EXCEPTION ((AE_INFO, Status,
798                "Unable to queue handler for GPE%02X - event disabled",
799                GpeNumber));
800        }
801        break;
802
803    default:
804
805        /*
806         * No handler or method to run!
807         * 03/2010: This case should no longer be possible. We will not allow
808         * a GPE to be enabled if it has no handler or method.
809         */
810        ACPI_ERROR ((AE_INFO,
811            "No handler or method for GPE%02X, disabling event",
812            GpeNumber));
813        break;
814    }
815
816    return_UINT32 (ACPI_INTERRUPT_HANDLED);
817}
818
819