[ACPICA]
[reactos.git] / reactos / drivers / bus / acpi / acpica / events / evgpe.c
1 /******************************************************************************
2 *
3 * Module Name: evgpe - General Purpose Event handling and dispatch
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116 #include "acpi.h"
117 #include "accommon.h"
118 #include "acevents.h"
119 #include "acnamesp.h"
120
121 #define _COMPONENT ACPI_EVENTS
122 ACPI_MODULE_NAME ("evgpe")
123
124 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
125
126 /* Local prototypes */
127
128 static void ACPI_SYSTEM_XFACE
129 AcpiEvAsynchExecuteGpeMethod (
130 void *Context);
131
132 static void ACPI_SYSTEM_XFACE
133 AcpiEvAsynchEnableGpe (
134 void *Context);
135
136
137 /*******************************************************************************
138 *
139 * FUNCTION: AcpiEvUpdateGpeEnableMask
140 *
141 * PARAMETERS: GpeEventInfo - GPE to update
142 *
143 * RETURN: Status
144 *
145 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
146 * runtime references to this GPE
147 *
148 ******************************************************************************/
149
150 ACPI_STATUS
151 AcpiEvUpdateGpeEnableMask (
152 ACPI_GPE_EVENT_INFO *GpeEventInfo)
153 {
154 ACPI_GPE_REGISTER_INFO *GpeRegisterInfo;
155 UINT32 RegisterBit;
156
157
158 ACPI_FUNCTION_TRACE (EvUpdateGpeEnableMask);
159
160
161 GpeRegisterInfo = GpeEventInfo->RegisterInfo;
162 if (!GpeRegisterInfo)
163 {
164 return_ACPI_STATUS (AE_NOT_EXIST);
165 }
166
167 RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo);
168
169 /* Clear the run bit up front */
170
171 ACPI_CLEAR_BIT (GpeRegisterInfo->EnableForRun, RegisterBit);
172
173 /* Set the mask bit only if there are references to this GPE */
174
175 if (GpeEventInfo->RuntimeCount)
176 {
177 ACPI_SET_BIT (GpeRegisterInfo->EnableForRun, (UINT8) RegisterBit);
178 }
179 GpeRegisterInfo->EnableMask = GpeRegisterInfo->EnableForRun;
180
181 return_ACPI_STATUS (AE_OK);
182 }
183
184
185 /*******************************************************************************
186 *
187 * FUNCTION: AcpiEvEnableGpe
188 *
189 * PARAMETERS: GpeEventInfo - GPE to enable
190 *
191 * RETURN: Status
192 *
193 * DESCRIPTION: Clear a GPE of stale events and enable it.
194 *
195 ******************************************************************************/
196
197 ACPI_STATUS
198 AcpiEvEnableGpe (
199 ACPI_GPE_EVENT_INFO *GpeEventInfo)
200 {
201 ACPI_STATUS Status;
202
203
204 ACPI_FUNCTION_TRACE (EvEnableGpe);
205
206
207 /* Clear the GPE (of stale events) */
208
209 Status = AcpiHwClearGpe (GpeEventInfo);
210 if (ACPI_FAILURE (Status))
211 {
212 return_ACPI_STATUS (Status);
213 }
214
215 /* Enable the requested GPE */
216
217 Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_ENABLE);
218 return_ACPI_STATUS (Status);
219 }
220
221
222 /*******************************************************************************
223 *
224 * FUNCTION: AcpiEvAddGpeReference
225 *
226 * PARAMETERS: GpeEventInfo - Add a reference to this GPE
227 *
228 * RETURN: Status
229 *
230 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
231 * hardware-enabled.
232 *
233 ******************************************************************************/
234
235 ACPI_STATUS
236 AcpiEvAddGpeReference (
237 ACPI_GPE_EVENT_INFO *GpeEventInfo)
238 {
239 ACPI_STATUS Status = AE_OK;
240
241
242 ACPI_FUNCTION_TRACE (EvAddGpeReference);
243
244
245 if (GpeEventInfo->RuntimeCount == ACPI_UINT8_MAX)
246 {
247 return_ACPI_STATUS (AE_LIMIT);
248 }
249
250 GpeEventInfo->RuntimeCount++;
251 if (GpeEventInfo->RuntimeCount == 1)
252 {
253 /* Enable on first reference */
254
255 Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
256 if (ACPI_SUCCESS (Status))
257 {
258 Status = AcpiEvEnableGpe (GpeEventInfo);
259 }
260
261 if (ACPI_FAILURE (Status))
262 {
263 GpeEventInfo->RuntimeCount--;
264 }
265 }
266
267 return_ACPI_STATUS (Status);
268 }
269
270
271 /*******************************************************************************
272 *
273 * FUNCTION: AcpiEvRemoveGpeReference
274 *
275 * PARAMETERS: GpeEventInfo - Remove a reference to this GPE
276 *
277 * RETURN: Status
278 *
279 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
280 * removed, the GPE is hardware-disabled.
281 *
282 ******************************************************************************/
283
284 ACPI_STATUS
285 AcpiEvRemoveGpeReference (
286 ACPI_GPE_EVENT_INFO *GpeEventInfo)
287 {
288 ACPI_STATUS Status = AE_OK;
289
290
291 ACPI_FUNCTION_TRACE (EvRemoveGpeReference);
292
293
294 if (!GpeEventInfo->RuntimeCount)
295 {
296 return_ACPI_STATUS (AE_LIMIT);
297 }
298
299 GpeEventInfo->RuntimeCount--;
300 if (!GpeEventInfo->RuntimeCount)
301 {
302 /* Disable on last reference */
303
304 Status = AcpiEvUpdateGpeEnableMask (GpeEventInfo);
305 if (ACPI_SUCCESS (Status))
306 {
307 Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
308 }
309
310 if (ACPI_FAILURE (Status))
311 {
312 GpeEventInfo->RuntimeCount++;
313 }
314 }
315
316 return_ACPI_STATUS (Status);
317 }
318
319
320 /*******************************************************************************
321 *
322 * FUNCTION: AcpiEvLowGetGpeInfo
323 *
324 * PARAMETERS: GpeNumber - Raw GPE number
325 * GpeBlock - A GPE info block
326 *
327 * RETURN: A GPE EventInfo struct. NULL if not a valid GPE (The GpeNumber
328 * is not within the specified GPE block)
329 *
330 * DESCRIPTION: Returns the EventInfo struct associated with this GPE. This is
331 * the low-level implementation of EvGetGpeEventInfo.
332 *
333 ******************************************************************************/
334
335 ACPI_GPE_EVENT_INFO *
336 AcpiEvLowGetGpeInfo (
337 UINT32 GpeNumber,
338 ACPI_GPE_BLOCK_INFO *GpeBlock)
339 {
340 UINT32 GpeIndex;
341
342
343 /*
344 * Validate that the GpeNumber is within the specified GpeBlock.
345 * (Two steps)
346 */
347 if (!GpeBlock ||
348 (GpeNumber < GpeBlock->BlockBaseNumber))
349 {
350 return (NULL);
351 }
352
353 GpeIndex = GpeNumber - GpeBlock->BlockBaseNumber;
354 if (GpeIndex >= GpeBlock->GpeCount)
355 {
356 return (NULL);
357 }
358
359 return (&GpeBlock->EventInfo[GpeIndex]);
360 }
361
362
363 /*******************************************************************************
364 *
365 * FUNCTION: AcpiEvGetGpeEventInfo
366 *
367 * PARAMETERS: GpeDevice - Device node. NULL for GPE0/GPE1
368 * GpeNumber - Raw GPE number
369 *
370 * RETURN: A GPE EventInfo struct. NULL if not a valid GPE
371 *
372 * DESCRIPTION: Returns the EventInfo struct associated with this GPE.
373 * Validates the GpeBlock and the GpeNumber
374 *
375 * Should be called only when the GPE lists are semaphore locked
376 * and not subject to change.
377 *
378 ******************************************************************************/
379
380 ACPI_GPE_EVENT_INFO *
381 AcpiEvGetGpeEventInfo (
382 ACPI_HANDLE GpeDevice,
383 UINT32 GpeNumber)
384 {
385 ACPI_OPERAND_OBJECT *ObjDesc;
386 ACPI_GPE_EVENT_INFO *GpeInfo;
387 UINT32 i;
388
389
390 ACPI_FUNCTION_ENTRY ();
391
392
393 /* A NULL GpeDevice means use the FADT-defined GPE block(s) */
394
395 if (!GpeDevice)
396 {
397 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
398
399 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++)
400 {
401 GpeInfo = AcpiEvLowGetGpeInfo (GpeNumber,
402 AcpiGbl_GpeFadtBlocks[i]);
403 if (GpeInfo)
404 {
405 return (GpeInfo);
406 }
407 }
408
409 /* The GpeNumber was not in the range of either FADT GPE block */
410
411 return (NULL);
412 }
413
414 /* A Non-NULL GpeDevice means this is a GPE Block Device */
415
416 ObjDesc = AcpiNsGetAttachedObject ((ACPI_NAMESPACE_NODE *) GpeDevice);
417 if (!ObjDesc ||
418 !ObjDesc->Device.GpeBlock)
419 {
420 return (NULL);
421 }
422
423 return (AcpiEvLowGetGpeInfo (GpeNumber, ObjDesc->Device.GpeBlock));
424 }
425
426
427 /*******************************************************************************
428 *
429 * FUNCTION: AcpiEvGpeDetect
430 *
431 * PARAMETERS: GpeXruptList - Interrupt block for this interrupt.
432 * Can have multiple GPE blocks attached.
433 *
434 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
435 *
436 * DESCRIPTION: Detect if any GP events have occurred. This function is
437 * executed at interrupt level.
438 *
439 ******************************************************************************/
440
441 UINT32
442 AcpiEvGpeDetect (
443 ACPI_GPE_XRUPT_INFO *GpeXruptList)
444 {
445 ACPI_STATUS Status;
446 ACPI_GPE_BLOCK_INFO *GpeBlock;
447 ACPI_NAMESPACE_NODE *GpeDevice;
448 ACPI_GPE_REGISTER_INFO *GpeRegisterInfo;
449 ACPI_GPE_EVENT_INFO *GpeEventInfo;
450 UINT32 GpeNumber;
451 ACPI_GPE_HANDLER_INFO *GpeHandlerInfo;
452 UINT32 IntStatus = ACPI_INTERRUPT_NOT_HANDLED;
453 UINT8 EnabledStatusByte;
454 UINT32 StatusReg;
455 UINT32 EnableReg;
456 ACPI_CPU_FLAGS Flags;
457 UINT32 i;
458 UINT32 j;
459
460
461 ACPI_FUNCTION_NAME (EvGpeDetect);
462
463 /* Check for the case where there are no GPEs */
464
465 if (!GpeXruptList)
466 {
467 return (IntStatus);
468 }
469
470 /*
471 * We need to obtain the GPE lock for both the data structs and registers
472 * Note: Not necessary to obtain the hardware lock, since the GPE
473 * registers are owned by the GpeLock.
474 */
475 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
476
477 /* Examine all GPE blocks attached to this interrupt level */
478
479 GpeBlock = GpeXruptList->GpeBlockListHead;
480 while (GpeBlock)
481 {
482 GpeDevice = GpeBlock->Node;
483
484 /*
485 * Read all of the 8-bit GPE status and enable registers in this GPE
486 * block, saving all of them. Find all currently active GP events.
487 */
488 for (i = 0; i < GpeBlock->RegisterCount; i++)
489 {
490 /* Get the next status/enable pair */
491
492 GpeRegisterInfo = &GpeBlock->RegisterInfo[i];
493
494 /*
495 * Optimization: If there are no GPEs enabled within this
496 * register, we can safely ignore the entire register.
497 */
498 if (!(GpeRegisterInfo->EnableForRun |
499 GpeRegisterInfo->EnableForWake))
500 {
501 ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
502 "Ignore disabled registers for GPE %02X-%02X: "
503 "RunEnable=%02X, WakeEnable=%02X\n",
504 GpeRegisterInfo->BaseGpeNumber,
505 GpeRegisterInfo->BaseGpeNumber + (ACPI_GPE_REGISTER_WIDTH - 1),
506 GpeRegisterInfo->EnableForRun,
507 GpeRegisterInfo->EnableForWake));
508 continue;
509 }
510
511 /* Read the Status Register */
512
513 Status = AcpiHwRead (&StatusReg, &GpeRegisterInfo->StatusAddress);
514 if (ACPI_FAILURE (Status))
515 {
516 goto UnlockAndExit;
517 }
518
519 /* Read the Enable Register */
520
521 Status = AcpiHwRead (&EnableReg, &GpeRegisterInfo->EnableAddress);
522 if (ACPI_FAILURE (Status))
523 {
524 goto UnlockAndExit;
525 }
526
527 ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
528 "Read registers for GPE %02X-%02X: Status=%02X, Enable=%02X, "
529 "RunEnable=%02X, WakeEnable=%02X\n",
530 GpeRegisterInfo->BaseGpeNumber,
531 GpeRegisterInfo->BaseGpeNumber + (ACPI_GPE_REGISTER_WIDTH - 1),
532 StatusReg, EnableReg,
533 GpeRegisterInfo->EnableForRun,
534 GpeRegisterInfo->EnableForWake));
535
536 /* Check if there is anything active at all in this register */
537
538 EnabledStatusByte = (UINT8) (StatusReg & EnableReg);
539 if (!EnabledStatusByte)
540 {
541 /* No active GPEs in this register, move on */
542
543 continue;
544 }
545
546 /* Now look at the individual GPEs in this byte register */
547
548 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
549 {
550 /* Examine one GPE bit */
551
552 GpeEventInfo = &GpeBlock->EventInfo[((ACPI_SIZE) i *
553 ACPI_GPE_REGISTER_WIDTH) + j];
554 GpeNumber = j + GpeRegisterInfo->BaseGpeNumber;
555
556 if (EnabledStatusByte & (1 << j))
557 {
558 /* Invoke global event handler if present */
559
560 AcpiGpeCount++;
561 if (AcpiGbl_GlobalEventHandler)
562 {
563 AcpiGbl_GlobalEventHandler (ACPI_EVENT_TYPE_GPE,
564 GpeDevice, GpeNumber,
565 AcpiGbl_GlobalEventHandlerContext);
566 }
567
568 /* Found an active GPE */
569
570 if (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==
571 ACPI_GPE_DISPATCH_RAW_HANDLER)
572 {
573 /* Dispatch the event to a raw handler */
574
575 GpeHandlerInfo = GpeEventInfo->Dispatch.Handler;
576
577 /*
578 * There is no protection around the namespace node
579 * and the GPE handler to ensure a safe destruction
580 * because:
581 * 1. The namespace node is expected to always
582 * exist after loading a table.
583 * 2. The GPE handler is expected to be flushed by
584 * AcpiOsWaitEventsComplete() before the
585 * destruction.
586 */
587 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
588 IntStatus |= GpeHandlerInfo->Address (
589 GpeDevice, GpeNumber, GpeHandlerInfo->Context);
590 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
591 }
592 else
593 {
594 /*
595 * Dispatch the event to a standard handler or
596 * method.
597 */
598 IntStatus |= AcpiEvGpeDispatch (GpeDevice,
599 GpeEventInfo, GpeNumber);
600 }
601 }
602 }
603 }
604
605 GpeBlock = GpeBlock->Next;
606 }
607
608 UnlockAndExit:
609
610 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
611 return (IntStatus);
612 }
613
614
615 /*******************************************************************************
616 *
617 * FUNCTION: AcpiEvAsynchExecuteGpeMethod
618 *
619 * PARAMETERS: Context (GpeEventInfo) - Info for this GPE
620 *
621 * RETURN: None
622 *
623 * DESCRIPTION: Perform the actual execution of a GPE control method. This
624 * function is called from an invocation of AcpiOsExecute and
625 * therefore does NOT execute at interrupt level - so that
626 * the control method itself is not executed in the context of
627 * an interrupt handler.
628 *
629 ******************************************************************************/
630
631 static void ACPI_SYSTEM_XFACE
632 AcpiEvAsynchExecuteGpeMethod (
633 void *Context)
634 {
635 ACPI_GPE_EVENT_INFO *GpeEventInfo = Context;
636 ACPI_STATUS Status = AE_OK;
637 ACPI_EVALUATE_INFO *Info;
638 ACPI_GPE_NOTIFY_INFO *Notify;
639
640
641 ACPI_FUNCTION_TRACE (EvAsynchExecuteGpeMethod);
642
643
644 /* Do the correct dispatch - normal method or implicit notify */
645
646 switch (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags))
647 {
648 case ACPI_GPE_DISPATCH_NOTIFY:
649 /*
650 * Implicit notify.
651 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
652 * NOTE: the request is queued for execution after this method
653 * completes. The notify handlers are NOT invoked synchronously
654 * from this thread -- because handlers may in turn run other
655 * control methods.
656 *
657 * June 2012: Expand implicit notify mechanism to support
658 * notifies on multiple device objects.
659 */
660 Notify = GpeEventInfo->Dispatch.NotifyList;
661 while (ACPI_SUCCESS (Status) && Notify)
662 {
663 Status = AcpiEvQueueNotifyRequest (Notify->DeviceNode,
664 ACPI_NOTIFY_DEVICE_WAKE);
665
666 Notify = Notify->Next;
667 }
668 break;
669
670 case ACPI_GPE_DISPATCH_METHOD:
671
672 /* Allocate the evaluation information block */
673
674 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
675 if (!Info)
676 {
677 Status = AE_NO_MEMORY;
678 }
679 else
680 {
681 /*
682 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
683 * _Lxx/_Exx control method that corresponds to this GPE
684 */
685 Info->PrefixNode = GpeEventInfo->Dispatch.MethodNode;
686 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
687
688 Status = AcpiNsEvaluate (Info);
689 ACPI_FREE (Info);
690 }
691
692 if (ACPI_FAILURE (Status))
693 {
694 ACPI_EXCEPTION ((AE_INFO, Status,
695 "while evaluating GPE method [%4.4s]",
696 AcpiUtGetNodeName (GpeEventInfo->Dispatch.MethodNode)));
697 }
698 break;
699
700 default:
701
702 goto ErrorExit; /* Should never happen */
703 }
704
705 /* Defer enabling of GPE until all notify handlers are done */
706
707 Status = AcpiOsExecute (OSL_NOTIFY_HANDLER,
708 AcpiEvAsynchEnableGpe, GpeEventInfo);
709 if (ACPI_SUCCESS (Status))
710 {
711 return_VOID;
712 }
713
714 ErrorExit:
715 AcpiEvAsynchEnableGpe (GpeEventInfo);
716 return_VOID;
717 }
718
719
720 /*******************************************************************************
721 *
722 * FUNCTION: AcpiEvAsynchEnableGpe
723 *
724 * PARAMETERS: Context (GpeEventInfo) - Info for this GPE
725 * Callback from AcpiOsExecute
726 *
727 * RETURN: None
728 *
729 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
730 * complete (i.e., finish execution of Notify)
731 *
732 ******************************************************************************/
733
734 static void ACPI_SYSTEM_XFACE
735 AcpiEvAsynchEnableGpe (
736 void *Context)
737 {
738 ACPI_GPE_EVENT_INFO *GpeEventInfo = Context;
739 ACPI_CPU_FLAGS Flags;
740
741
742 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
743 (void) AcpiEvFinishGpe (GpeEventInfo);
744 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
745
746 return;
747 }
748
749
750 /*******************************************************************************
751 *
752 * FUNCTION: AcpiEvFinishGpe
753 *
754 * PARAMETERS: GpeEventInfo - Info for this GPE
755 *
756 * RETURN: Status
757 *
758 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
759 * of a GPE method or a synchronous or asynchronous GPE handler.
760 *
761 ******************************************************************************/
762
763 ACPI_STATUS
764 AcpiEvFinishGpe (
765 ACPI_GPE_EVENT_INFO *GpeEventInfo)
766 {
767 ACPI_STATUS Status;
768
769
770 if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
771 ACPI_GPE_LEVEL_TRIGGERED)
772 {
773 /*
774 * GPE is level-triggered, we clear the GPE status bit after
775 * handling the event.
776 */
777 Status = AcpiHwClearGpe (GpeEventInfo);
778 if (ACPI_FAILURE (Status))
779 {
780 return (Status);
781 }
782 }
783
784 /*
785 * Enable this GPE, conditionally. This means that the GPE will
786 * only be physically enabled if the EnableMask bit is set
787 * in the EventInfo.
788 */
789 (void) AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_CONDITIONAL_ENABLE);
790 return (AE_OK);
791 }
792
793
794 /*******************************************************************************
795 *
796 * FUNCTION: AcpiEvGpeDispatch
797 *
798 * PARAMETERS: GpeDevice - Device node. NULL for GPE0/GPE1
799 * GpeEventInfo - Info for this GPE
800 * GpeNumber - Number relative to the parent GPE block
801 *
802 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
803 *
804 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
805 * or method (e.g. _Lxx/_Exx) handler.
806 *
807 * This function executes at interrupt level.
808 *
809 ******************************************************************************/
810
811 UINT32
812 AcpiEvGpeDispatch (
813 ACPI_NAMESPACE_NODE *GpeDevice,
814 ACPI_GPE_EVENT_INFO *GpeEventInfo,
815 UINT32 GpeNumber)
816 {
817 ACPI_STATUS Status;
818 UINT32 ReturnValue;
819
820
821 ACPI_FUNCTION_TRACE (EvGpeDispatch);
822
823
824 /*
825 * Always disable the GPE so that it does not keep firing before
826 * any asynchronous activity completes (either from the execution
827 * of a GPE method or an asynchronous GPE handler.)
828 *
829 * If there is no handler or method to run, just disable the
830 * GPE and leave it disabled permanently to prevent further such
831 * pointless events from firing.
832 */
833 Status = AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);
834 if (ACPI_FAILURE (Status))
835 {
836 ACPI_EXCEPTION ((AE_INFO, Status,
837 "Unable to disable GPE %02X", GpeNumber));
838 return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
839 }
840
841 /*
842 * If edge-triggered, clear the GPE status bit now. Note that
843 * level-triggered events are cleared after the GPE is serviced.
844 */
845 if ((GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
846 ACPI_GPE_EDGE_TRIGGERED)
847 {
848 Status = AcpiHwClearGpe (GpeEventInfo);
849 if (ACPI_FAILURE (Status))
850 {
851 ACPI_EXCEPTION ((AE_INFO, Status,
852 "Unable to clear GPE %02X", GpeNumber));
853 (void) AcpiHwLowSetGpe (GpeEventInfo,
854 ACPI_GPE_CONDITIONAL_ENABLE);
855 return_UINT32 (ACPI_INTERRUPT_NOT_HANDLED);
856 }
857 }
858
859 /*
860 * Dispatch the GPE to either an installed handler or the control
861 * method associated with this GPE (_Lxx or _Exx). If a handler
862 * exists, we invoke it and do not attempt to run the method.
863 * If there is neither a handler nor a method, leave the GPE
864 * disabled.
865 */
866 switch (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags))
867 {
868 case ACPI_GPE_DISPATCH_HANDLER:
869
870 /* Invoke the installed handler (at interrupt level) */
871
872 ReturnValue = GpeEventInfo->Dispatch.Handler->Address (
873 GpeDevice, GpeNumber,
874 GpeEventInfo->Dispatch.Handler->Context);
875
876 /* If requested, clear (if level-triggered) and reenable the GPE */
877
878 if (ReturnValue & ACPI_REENABLE_GPE)
879 {
880 (void) AcpiEvFinishGpe (GpeEventInfo);
881 }
882 break;
883
884 case ACPI_GPE_DISPATCH_METHOD:
885 case ACPI_GPE_DISPATCH_NOTIFY:
886 /*
887 * Execute the method associated with the GPE
888 * NOTE: Level-triggered GPEs are cleared after the method completes.
889 */
890 Status = AcpiOsExecute (OSL_GPE_HANDLER,
891 AcpiEvAsynchExecuteGpeMethod, GpeEventInfo);
892 if (ACPI_FAILURE (Status))
893 {
894 ACPI_EXCEPTION ((AE_INFO, Status,
895 "Unable to queue handler for GPE %02X - event disabled",
896 GpeNumber));
897 }
898 break;
899
900 default:
901 /*
902 * No handler or method to run!
903 * 03/2010: This case should no longer be possible. We will not allow
904 * a GPE to be enabled if it has no handler or method.
905 */
906 ACPI_ERROR ((AE_INFO,
907 "No handler or method for GPE %02X, disabling event",
908 GpeNumber));
909 break;
910 }
911
912 return_UINT32 (ACPI_INTERRUPT_HANDLED);
913 }
914
915 #endif /* !ACPI_REDUCED_HARDWARE */