Merge from vendor branch AWK:
[dragonfly.git] / sys / contrib / dev / acpica-unix-20031203 / events / evgpeblk.c
1 /******************************************************************************
2  *
3  * Module Name: evgpeblk - GPE block creation and initialization.
4  *              $Revision: 26 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116
117 #include "acpi.h"
118 #include "acevents.h"
119 #include "acnamesp.h"
120
121 #define _COMPONENT          ACPI_EVENTS
122         ACPI_MODULE_NAME    ("evgpeblk")
123
124
125 /*******************************************************************************
126  *
127  * FUNCTION:    AcpiEvValidGpeEvent
128  *
129  * PARAMETERS:  GpeEventInfo - Info for this GPE
130  *
131  * RETURN:      TRUE if the GpeEvent is valid
132  *
133  * DESCRIPTION: Validate a GPE event.  DO NOT CALL FROM INTERRUPT LEVEL.
134  *              Should be called only when the GPE lists are semaphore locked
135  *              and not subject to change.
136  *
137  ******************************************************************************/
138
139 BOOLEAN
140 AcpiEvValidGpeEvent (
141     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
142 {
143     ACPI_GPE_XRUPT_INFO     *GpeXruptBlock;
144     ACPI_GPE_BLOCK_INFO     *GpeBlock;
145
146
147     ACPI_FUNCTION_ENTRY ();
148
149
150     /* No need for spin lock since we are not changing any list elements */
151
152     /* Walk the GPE interrupt levels */
153
154     GpeXruptBlock = AcpiGbl_GpeXruptListHead;
155     while (GpeXruptBlock)
156     {
157         GpeBlock = GpeXruptBlock->GpeBlockListHead;
158
159         /* Walk the GPE blocks on this interrupt level */
160
161         while (GpeBlock)
162         {
163             if ((&GpeBlock->EventInfo[0] <= GpeEventInfo) &&
164                 (&GpeBlock->EventInfo[((ACPI_SIZE) GpeBlock->RegisterCount) * 8] > GpeEventInfo))
165             {
166                 return (TRUE);
167             }
168
169             GpeBlock = GpeBlock->Next;
170         }
171
172         GpeXruptBlock = GpeXruptBlock->Next;
173     }
174
175     return (FALSE);
176 }
177
178
179 /*******************************************************************************
180  *
181  * FUNCTION:    AcpiEvWalkGpeList
182  *
183  * PARAMETERS:  GpeWalkCallback     - Routine called for each GPE block
184  *
185  * RETURN:      Status
186  *
187  * DESCRIPTION: Walk the GPE lists.
188  *              FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
189  *
190  ******************************************************************************/
191
192 ACPI_STATUS
193 AcpiEvWalkGpeList (
194     ACPI_GPE_CALLBACK       GpeWalkCallback)
195 {
196     ACPI_GPE_BLOCK_INFO     *GpeBlock;
197     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo;
198     ACPI_STATUS             Status = AE_OK;
199
200
201     ACPI_FUNCTION_TRACE ("EvWalkGpeList");
202
203
204     AcpiOsAcquireLock (AcpiGbl_GpeLock, ACPI_ISR);
205
206     /* Walk the interrupt level descriptor list */
207
208     GpeXruptInfo = AcpiGbl_GpeXruptListHead;
209     while (GpeXruptInfo)
210     {
211         /* Walk all Gpe Blocks attached to this interrupt level */
212
213         GpeBlock = GpeXruptInfo->GpeBlockListHead;
214         while (GpeBlock)
215         {
216             /* One callback per GPE block */
217
218             Status = GpeWalkCallback (GpeXruptInfo, GpeBlock);
219             if (ACPI_FAILURE (Status))
220             {
221                 goto UnlockAndExit;
222             }
223
224             GpeBlock = GpeBlock->Next;
225         }
226
227         GpeXruptInfo = GpeXruptInfo->Next;
228     }
229
230 UnlockAndExit:
231     AcpiOsReleaseLock (AcpiGbl_GpeLock, ACPI_ISR);
232     return_ACPI_STATUS (Status);
233 }
234
235
236 /*******************************************************************************
237  *
238  * FUNCTION:    AcpiEvSaveMethodInfo
239  *
240  * PARAMETERS:  Callback from WalkNamespace
241  *
242  * RETURN:      Status
243  *
244  * DESCRIPTION: Called from AcpiWalkNamespace.  Expects each object to be a
245  *              control method under the _GPE portion of the namespace.
246  *              Extract the name and GPE type from the object, saving this
247  *              information for quick lookup during GPE dispatch
248  *
249  *              The name of each GPE control method is of the form:
250  *                  "_Lnn" or "_Enn"
251  *                  Where:
252  *                      L      - means that the GPE is level triggered
253  *                      E      - means that the GPE is edge triggered
254  *                      nn     - is the GPE number [in HEX]
255  *
256  ******************************************************************************/
257
258 static ACPI_STATUS
259 AcpiEvSaveMethodInfo (
260     ACPI_HANDLE             ObjHandle,
261     UINT32                  Level,
262     void                    *ObjDesc,
263     void                    **ReturnValue)
264 {
265     ACPI_GPE_BLOCK_INFO     *GpeBlock = (void *) ObjDesc;
266     ACPI_GPE_EVENT_INFO     *GpeEventInfo;
267     UINT32                  GpeNumber;
268     char                    Name[ACPI_NAME_SIZE + 1];
269     UINT8                   Type;
270     ACPI_STATUS             Status;
271
272
273     ACPI_FUNCTION_TRACE ("EvSaveMethodInfo");
274
275
276     /* Extract the name from the object and convert to a string */
277
278     ACPI_MOVE_32_TO_32 (Name,
279                         &((ACPI_NAMESPACE_NODE *) ObjHandle)->Name.Integer);
280     Name[ACPI_NAME_SIZE] = 0;
281
282     /*
283      * Edge/Level determination is based on the 2nd character
284      * of the method name
285      */
286     switch (Name[1])
287     {
288     case 'L':
289         Type = ACPI_EVENT_LEVEL_TRIGGERED;
290         break;
291
292     case 'E':
293         Type = ACPI_EVENT_EDGE_TRIGGERED;
294         break;
295
296     default:
297         /* Unknown method type, just ignore it! */
298
299         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
300             "Unknown GPE method type: %s (name not of form _Lnn or _Enn)\n",
301             Name));
302         return_ACPI_STATUS (AE_OK);
303     }
304
305     /* Convert the last two characters of the name to the GPE Number */
306
307     GpeNumber = ACPI_STRTOUL (&Name[2], NULL, 16);
308     if (GpeNumber == ACPI_UINT32_MAX)
309     {
310         /* Conversion failed; invalid method, just ignore it */
311
312         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
313             "Could not extract GPE number from name: %s (name is not of form _Lnn or _Enn)\n",
314             Name));
315         return_ACPI_STATUS (AE_OK);
316     }
317
318     /* Ensure that we have a valid GPE number for this GPE block */
319
320     if ((GpeNumber < GpeBlock->BlockBaseNumber) ||
321         (GpeNumber >= (GpeBlock->BlockBaseNumber + (GpeBlock->RegisterCount * 8))))
322     {
323         /*
324          * Not valid for this GPE block, just ignore it
325          * However, it may be valid for a different GPE block, since GPE0 and GPE1
326          * methods both appear under \_GPE.
327          */
328         return_ACPI_STATUS (AE_OK);
329     }
330
331     /*
332      * Now we can add this information to the GpeEventInfo block
333      * for use during dispatch of this GPE.
334      */
335     GpeEventInfo = &GpeBlock->EventInfo[GpeNumber - GpeBlock->BlockBaseNumber];
336
337     GpeEventInfo->Flags      = Type;
338     GpeEventInfo->MethodNode = (ACPI_NAMESPACE_NODE *) ObjHandle;
339
340     /* Enable the GPE (SCIs should be disabled at this point) */
341
342     Status = AcpiHwEnableGpe (GpeEventInfo);
343     if (ACPI_FAILURE (Status))
344     {
345         return_ACPI_STATUS (Status);
346     }
347
348     ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
349         "Registered GPE method %s as GPE number 0x%.2X\n",
350         Name, GpeNumber));
351     return_ACPI_STATUS (AE_OK);
352 }
353
354
355 /*******************************************************************************
356  *
357  * FUNCTION:    AcpiEvGetGpeXruptBlock
358  *
359  * PARAMETERS:  InterruptLevel      - Interrupt for a GPE block
360  *
361  * RETURN:      A GPE interrupt block
362  *
363  * DESCRIPTION: Get or Create a GPE interrupt block.  There is one interrupt
364  *              block per unique interrupt level used for GPEs.
365  *              Should be called only when the GPE lists are semaphore locked
366  *              and not subject to change.
367  *
368  ******************************************************************************/
369
370 static ACPI_GPE_XRUPT_INFO *
371 AcpiEvGetGpeXruptBlock (
372     UINT32                  InterruptLevel)
373 {
374     ACPI_GPE_XRUPT_INFO     *NextGpeXrupt;
375     ACPI_GPE_XRUPT_INFO     *GpeXrupt;
376     ACPI_STATUS             Status;
377
378
379     ACPI_FUNCTION_TRACE ("EvGetGpeXruptBlock");
380
381
382     /* No need for spin lock since we are not changing any list elements here */
383
384     NextGpeXrupt = AcpiGbl_GpeXruptListHead;
385     while (NextGpeXrupt)
386     {
387         if (NextGpeXrupt->InterruptLevel == InterruptLevel)
388         {
389             return_PTR (NextGpeXrupt);
390         }
391
392         NextGpeXrupt = NextGpeXrupt->Next;
393     }
394
395     /* Not found, must allocate a new xrupt descriptor */
396
397     GpeXrupt = ACPI_MEM_CALLOCATE (sizeof (ACPI_GPE_XRUPT_INFO));
398     if (!GpeXrupt)
399     {
400         return_PTR (NULL);
401     }
402
403     GpeXrupt->InterruptLevel = InterruptLevel;
404
405     /* Install new interrupt descriptor with spin lock */
406
407     AcpiOsAcquireLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
408     if (AcpiGbl_GpeXruptListHead)
409     {
410         NextGpeXrupt = AcpiGbl_GpeXruptListHead;
411         while (NextGpeXrupt->Next)
412         {
413             NextGpeXrupt = NextGpeXrupt->Next;
414         }
415
416         NextGpeXrupt->Next = GpeXrupt;
417         GpeXrupt->Previous = NextGpeXrupt;
418     }
419     else
420     {
421         AcpiGbl_GpeXruptListHead = GpeXrupt;
422     }
423     AcpiOsReleaseLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
424
425     /* Install new interrupt handler if not SCI_INT */
426
427     if (InterruptLevel != AcpiGbl_FADT->SciInt)
428     {
429         Status = AcpiOsInstallInterruptHandler (InterruptLevel,
430                     AcpiEvGpeXruptHandler, GpeXrupt);
431         if (ACPI_FAILURE (Status))
432         {
433             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
434                 "Could not install GPE interrupt handler at level 0x%X\n",
435                 InterruptLevel));
436             return_PTR (NULL);
437         }
438     }
439
440     return_PTR (GpeXrupt);
441 }
442
443
444 /*******************************************************************************
445  *
446  * FUNCTION:    AcpiEvDeleteGpeXrupt
447  *
448  * PARAMETERS:  GpeXrupt        - A GPE interrupt info block
449  *
450  * RETURN:      Status
451  *
452  * DESCRIPTION: Remove and free a GpeXrupt block.  Remove an associated
453  *              interrupt handler if not the SCI interrupt.
454  *
455  ******************************************************************************/
456
457 static ACPI_STATUS
458 AcpiEvDeleteGpeXrupt (
459     ACPI_GPE_XRUPT_INFO     *GpeXrupt)
460 {
461     ACPI_STATUS             Status;
462
463
464     ACPI_FUNCTION_TRACE ("EvDeleteGpeXrupt");
465
466
467     /* We never want to remove the SCI interrupt handler */
468
469     if (GpeXrupt->InterruptLevel == AcpiGbl_FADT->SciInt)
470     {
471         GpeXrupt->GpeBlockListHead = NULL;
472         return_ACPI_STATUS (AE_OK);
473     }
474
475     /* Disable this interrupt */
476
477     Status = AcpiOsRemoveInterruptHandler (GpeXrupt->InterruptLevel,
478                                     AcpiEvGpeXruptHandler);
479     if (ACPI_FAILURE (Status))
480     {
481         return_ACPI_STATUS (Status);
482     }
483
484     /* Unlink the interrupt block with lock */
485
486     AcpiOsAcquireLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
487     if (GpeXrupt->Previous)
488     {
489         GpeXrupt->Previous->Next = GpeXrupt->Next;
490     }
491
492     if (GpeXrupt->Next)
493     {
494         GpeXrupt->Next->Previous = GpeXrupt->Previous;
495     }
496     AcpiOsReleaseLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
497
498     /* Free the block */
499
500     ACPI_MEM_FREE (GpeXrupt);
501     return_ACPI_STATUS (AE_OK);
502 }
503
504
505 /*******************************************************************************
506  *
507  * FUNCTION:    AcpiEvInstallGpeBlock
508  *
509  * PARAMETERS:  GpeBlock        - New GPE block
510  *              InterruptLevel  - Level to be associated with this GPE block
511  *
512  * RETURN:      Status
513  *
514  * DESCRIPTION: Install new GPE block with mutex support
515  *
516  ******************************************************************************/
517
518 static ACPI_STATUS
519 AcpiEvInstallGpeBlock (
520     ACPI_GPE_BLOCK_INFO     *GpeBlock,
521     UINT32                  InterruptLevel)
522 {
523     ACPI_GPE_BLOCK_INFO     *NextGpeBlock;
524     ACPI_GPE_XRUPT_INFO     *GpeXruptBlock;
525     ACPI_STATUS             Status;
526
527
528     ACPI_FUNCTION_TRACE ("EvInstallGpeBlock");
529
530
531     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
532     if (ACPI_FAILURE (Status))
533     {
534         return_ACPI_STATUS (Status);
535     }
536
537     GpeXruptBlock = AcpiEvGetGpeXruptBlock (InterruptLevel);
538     if (!GpeXruptBlock)
539     {
540         Status = AE_NO_MEMORY;
541         goto UnlockAndExit;
542     }
543
544     /* Install the new block at the end of the list for this interrupt with lock */
545
546     AcpiOsAcquireLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
547     if (GpeXruptBlock->GpeBlockListHead)
548     {
549         NextGpeBlock = GpeXruptBlock->GpeBlockListHead;
550         while (NextGpeBlock->Next)
551         {
552             NextGpeBlock = NextGpeBlock->Next;
553         }
554
555         NextGpeBlock->Next = GpeBlock;
556         GpeBlock->Previous = NextGpeBlock;
557     }
558     else
559     {
560         GpeXruptBlock->GpeBlockListHead = GpeBlock;
561     }
562
563     GpeBlock->XruptBlock = GpeXruptBlock;
564     AcpiOsReleaseLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
565
566 UnlockAndExit:
567     Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
568     return_ACPI_STATUS (Status);
569 }
570
571
572 /*******************************************************************************
573  *
574  * FUNCTION:    AcpiEvDeleteGpeBlock
575  *
576  * PARAMETERS:  GpeBlock        - Existing GPE block
577  *
578  * RETURN:      Status
579  *
580  * DESCRIPTION: Remove a GPE block
581  *
582  ******************************************************************************/
583
584 ACPI_STATUS
585 AcpiEvDeleteGpeBlock (
586     ACPI_GPE_BLOCK_INFO     *GpeBlock)
587 {
588     ACPI_STATUS             Status;
589
590
591     ACPI_FUNCTION_TRACE ("EvInstallGpeBlock");
592
593
594     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
595     if (ACPI_FAILURE (Status))
596     {
597         return_ACPI_STATUS (Status);
598     }
599
600     /* Disable all GPEs in this block */
601
602     Status = AcpiHwDisableGpeBlock (GpeBlock->XruptBlock, GpeBlock);
603
604     if (!GpeBlock->Previous && !GpeBlock->Next)
605     {
606         /* This is the last GpeBlock on this interrupt */
607
608         Status = AcpiEvDeleteGpeXrupt (GpeBlock->XruptBlock);
609         if (ACPI_FAILURE (Status))
610         {
611             goto UnlockAndExit;
612         }
613     }
614     else
615     {
616         /* Remove the block on this interrupt with lock */
617
618         AcpiOsAcquireLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
619         if (GpeBlock->Previous)
620         {
621             GpeBlock->Previous->Next = GpeBlock->Next;
622         }
623         else
624         {
625             GpeBlock->XruptBlock->GpeBlockListHead = GpeBlock->Next;
626         }
627
628         if (GpeBlock->Next)
629         {
630             GpeBlock->Next->Previous = GpeBlock->Previous;
631         }
632         AcpiOsReleaseLock (AcpiGbl_GpeLock, ACPI_NOT_ISR);
633     }
634
635     /* Free the GpeBlock */
636
637     ACPI_MEM_FREE (GpeBlock->RegisterInfo);
638     ACPI_MEM_FREE (GpeBlock->EventInfo);
639     ACPI_MEM_FREE (GpeBlock);
640
641 UnlockAndExit:
642     Status = AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
643     return_ACPI_STATUS (Status);
644 }
645
646
647 /*******************************************************************************
648  *
649  * FUNCTION:    AcpiEvCreateGpeInfoBlocks
650  *
651  * PARAMETERS:  GpeBlock    - New GPE block
652  *
653  * RETURN:      Status
654  *
655  * DESCRIPTION: Create the RegisterInfo and EventInfo blocks for this GPE block
656  *
657  ******************************************************************************/
658
659 static ACPI_STATUS
660 AcpiEvCreateGpeInfoBlocks (
661     ACPI_GPE_BLOCK_INFO     *GpeBlock)
662 {
663     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo = NULL;
664     ACPI_GPE_EVENT_INFO     *GpeEventInfo = NULL;
665     ACPI_GPE_EVENT_INFO     *ThisEvent;
666     ACPI_GPE_REGISTER_INFO  *ThisRegister;
667     ACPI_NATIVE_UINT        i;
668     ACPI_NATIVE_UINT        j;
669     ACPI_STATUS             Status;
670
671
672     ACPI_FUNCTION_TRACE ("EvCreateGpeInfoBlocks");
673
674
675     /* Allocate the GPE register information block */
676
677     GpeRegisterInfo = ACPI_MEM_CALLOCATE (
678                                 (ACPI_SIZE) GpeBlock->RegisterCount *
679                                 sizeof (ACPI_GPE_REGISTER_INFO));
680     if (!GpeRegisterInfo)
681     {
682         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
683             "Could not allocate the GpeRegisterInfo table\n"));
684         return_ACPI_STATUS (AE_NO_MEMORY);
685     }
686
687     /*
688      * Allocate the GPE EventInfo block.  There are eight distinct GPEs
689      * per register.  Initialization to zeros is sufficient.
690      */
691     GpeEventInfo = ACPI_MEM_CALLOCATE (
692                         ((ACPI_SIZE) GpeBlock->RegisterCount * ACPI_GPE_REGISTER_WIDTH) *
693                         sizeof (ACPI_GPE_EVENT_INFO));
694     if (!GpeEventInfo)
695     {
696         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not allocate the GpeEventInfo table\n"));
697         Status = AE_NO_MEMORY;
698         goto ErrorExit;
699     }
700
701     /* Save the new Info arrays in the GPE block */
702
703     GpeBlock->RegisterInfo = GpeRegisterInfo;
704     GpeBlock->EventInfo    = GpeEventInfo;
705
706     /*
707      * Initialize the GPE Register and Event structures.  A goal of these
708      * tables is to hide the fact that there are two separate GPE register sets
709      * in a given gpe hardware block, the status registers occupy the first half,
710      * and the enable registers occupy the second half.
711      */
712     ThisRegister = GpeRegisterInfo;
713     ThisEvent    = GpeEventInfo;
714
715     for (i = 0; i < GpeBlock->RegisterCount; i++)
716     {
717         /* Init the RegisterInfo for this GPE register (8 GPEs) */
718
719         ThisRegister->BaseGpeNumber = (UINT8) (GpeBlock->BlockBaseNumber +
720                                                  (i * ACPI_GPE_REGISTER_WIDTH));
721
722         ACPI_STORE_ADDRESS (ThisRegister->StatusAddress.Address,
723                                 (ACPI_GET_ADDRESS (GpeBlock->BlockAddress.Address)
724                                 + i));
725
726         ACPI_STORE_ADDRESS (ThisRegister->EnableAddress.Address,
727                                 (ACPI_GET_ADDRESS (GpeBlock->BlockAddress.Address)
728                                 + i
729                                 + GpeBlock->RegisterCount));
730
731         ThisRegister->StatusAddress.AddressSpaceId    = GpeBlock->BlockAddress.AddressSpaceId;
732         ThisRegister->EnableAddress.AddressSpaceId    = GpeBlock->BlockAddress.AddressSpaceId;
733         ThisRegister->StatusAddress.RegisterBitWidth  = ACPI_GPE_REGISTER_WIDTH;
734         ThisRegister->EnableAddress.RegisterBitWidth  = ACPI_GPE_REGISTER_WIDTH;
735         ThisRegister->StatusAddress.RegisterBitOffset = ACPI_GPE_REGISTER_WIDTH;
736         ThisRegister->EnableAddress.RegisterBitOffset = ACPI_GPE_REGISTER_WIDTH;
737
738         /* Init the EventInfo for each GPE within this register */
739
740         for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)
741         {
742             ThisEvent->BitMask = AcpiGbl_DecodeTo8bit[j];
743             ThisEvent->RegisterInfo = ThisRegister;
744             ThisEvent++;
745         }
746
747         /*
748          * Clear the status/enable registers.  Note that status registers
749          * are cleared by writing a '1', while enable registers are cleared
750          * by writing a '0'.
751          */
752         Status = AcpiHwLowLevelWrite (ACPI_GPE_REGISTER_WIDTH, 0x00,
753                     &ThisRegister->EnableAddress);
754         if (ACPI_FAILURE (Status))
755         {
756             goto ErrorExit;
757         }
758
759         Status = AcpiHwLowLevelWrite (ACPI_GPE_REGISTER_WIDTH, 0xFF,
760                     &ThisRegister->StatusAddress);
761         if (ACPI_FAILURE (Status))
762         {
763             goto ErrorExit;
764         }
765
766         ThisRegister++;
767     }
768
769     return_ACPI_STATUS (AE_OK);
770
771
772 ErrorExit:
773     if (GpeRegisterInfo)
774     {
775         ACPI_MEM_FREE (GpeRegisterInfo);
776     }
777     if (GpeEventInfo)
778     {
779         ACPI_MEM_FREE (GpeEventInfo);
780     }
781
782     return_ACPI_STATUS (Status);
783 }
784
785
786 /*******************************************************************************
787  *
788  * FUNCTION:    AcpiEvCreateGpeBlock
789  *
790  * PARAMETERS:  GpeDevice           - Handle to the parent GPE block
791  *              GpeBlockAddress     - Address and SpaceID
792  *              RegisterCount       - Number of GPE register pairs in the block
793  *              GpeBlockBaseNumber  - Starting GPE number for the block
794  *              InterruptLevel      - H/W interrupt for the block
795  *              ReturnGpeBlock      - Where the new block descriptor is returned
796  *
797  * RETURN:      Status
798  *
799  * DESCRIPTION: Create and Install a block of GPE registers
800  *
801  ******************************************************************************/
802
803 ACPI_STATUS
804 AcpiEvCreateGpeBlock (
805     ACPI_NAMESPACE_NODE     *GpeDevice,
806     ACPI_GENERIC_ADDRESS    *GpeBlockAddress,
807     UINT32                  RegisterCount,
808     UINT8                   GpeBlockBaseNumber,
809     UINT32                  InterruptLevel,
810     ACPI_GPE_BLOCK_INFO     **ReturnGpeBlock)
811 {
812     ACPI_GPE_BLOCK_INFO     *GpeBlock;
813     ACPI_STATUS             Status;
814
815
816     ACPI_FUNCTION_TRACE ("EvCreateGpeBlock");
817
818
819     if (!RegisterCount)
820     {
821         return_ACPI_STATUS (AE_OK);
822     }
823
824     /* Allocate a new GPE block */
825
826     GpeBlock = ACPI_MEM_CALLOCATE (sizeof (ACPI_GPE_BLOCK_INFO));
827     if (!GpeBlock)
828     {
829         return_ACPI_STATUS (AE_NO_MEMORY);
830     }
831
832     /* Initialize the new GPE block */
833
834     GpeBlock->RegisterCount   = RegisterCount;
835     GpeBlock->BlockBaseNumber = GpeBlockBaseNumber;
836
837     ACPI_MEMCPY (&GpeBlock->BlockAddress, GpeBlockAddress, sizeof (ACPI_GENERIC_ADDRESS));
838
839     /* Create the RegisterInfo and EventInfo sub-structures */
840
841     Status = AcpiEvCreateGpeInfoBlocks (GpeBlock);
842     if (ACPI_FAILURE (Status))
843     {
844         ACPI_MEM_FREE (GpeBlock);
845         return_ACPI_STATUS (Status);
846     }
847
848     /* Install the new block in the global list(s) */
849
850     Status = AcpiEvInstallGpeBlock (GpeBlock, InterruptLevel);
851     if (ACPI_FAILURE (Status))
852     {
853         ACPI_MEM_FREE (GpeBlock);
854         return_ACPI_STATUS (Status);
855     }
856
857     /* Dump info about this GPE block */
858
859     ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "GPE %02d to %02d [%4.4s] %d regs at %8.8X%8.8X on int %d\n",
860         GpeBlock->BlockBaseNumber,
861         (UINT32) (GpeBlock->BlockBaseNumber +
862                 ((GpeBlock->RegisterCount * ACPI_GPE_REGISTER_WIDTH) -1)),
863         GpeDevice->Name.Ascii,
864         GpeBlock->RegisterCount,
865         ACPI_FORMAT_UINT64 (ACPI_GET_ADDRESS (GpeBlock->BlockAddress.Address)),
866         InterruptLevel));
867
868     /* Find all GPE methods (_Lxx, _Exx) for this block */
869
870     Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD, GpeDevice,
871                                 ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK, AcpiEvSaveMethodInfo,
872                                 GpeBlock, NULL);
873
874     /* Return the new block */
875
876     if (ReturnGpeBlock)
877     {
878         (*ReturnGpeBlock) = GpeBlock;
879     }
880
881     return_ACPI_STATUS (AE_OK);
882 }
883
884
885 /*******************************************************************************
886  *
887  * FUNCTION:    AcpiEvGpeInitialize
888  *
889  * PARAMETERS:  None
890  *
891  * RETURN:      Status
892  *
893  * DESCRIPTION: Initialize the GPE data structures
894  *
895  ******************************************************************************/
896
897 ACPI_STATUS
898 AcpiEvGpeInitialize (void)
899 {
900     UINT32                  RegisterCount0 = 0;
901     UINT32                  RegisterCount1 = 0;
902     UINT32                  GpeNumberMax = 0;
903     ACPI_HANDLE             GpeDevice;
904     ACPI_STATUS             Status;
905
906
907     ACPI_FUNCTION_TRACE ("EvGpeInitialize");
908
909
910     /* Get a handle to the predefined _GPE object */
911
912     Status = AcpiGetHandle (NULL, "\\_GPE", &GpeDevice);
913     if (ACPI_FAILURE (Status))
914     {
915         return_ACPI_STATUS (Status);
916     }
917
918     /*
919      * Initialize the GPE Blocks defined in the FADT
920      *
921      * Why the GPE register block lengths are divided by 2:  From the ACPI Spec,
922      * section "General-Purpose Event Registers", we have:
923      *
924      * "Each register block contains two registers of equal length
925      *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
926      *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
927      *  The length of the GPE1_STS and GPE1_EN registers is equal to
928      *  half the GPE1_LEN. If a generic register block is not supported
929      *  then its respective block pointer and block length values in the
930      *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
931      *  to be the same size."
932      */
933
934     /*
935      * Determine the maximum GPE number for this machine.
936      *
937      * Note: both GPE0 and GPE1 are optional, and either can exist without
938      * the other.
939      *
940      * If EITHER the register length OR the block address are zero, then that
941      * particular block is not supported.
942      */
943     if (AcpiGbl_FADT->Gpe0BlkLen &&
944         ACPI_GET_ADDRESS (AcpiGbl_FADT->XGpe0Blk.Address))
945     {
946         /* GPE block 0 exists (has both length and address > 0) */
947
948         RegisterCount0 = (UINT16) (AcpiGbl_FADT->Gpe0BlkLen / 2);
949
950         GpeNumberMax = (RegisterCount0 * ACPI_GPE_REGISTER_WIDTH) - 1;
951
952         /* Install GPE Block 0 */
953
954         Status = AcpiEvCreateGpeBlock (GpeDevice, &AcpiGbl_FADT->XGpe0Blk,
955                     RegisterCount0, 0, AcpiGbl_FADT->SciInt, &AcpiGbl_GpeFadtBlocks[0]);
956         if (ACPI_FAILURE (Status))
957         {
958             ACPI_REPORT_ERROR ((
959                 "Could not create GPE Block 0, %s\n",
960                 AcpiFormatException (Status)));
961         }
962     }
963
964     if (AcpiGbl_FADT->Gpe1BlkLen &&
965         ACPI_GET_ADDRESS (AcpiGbl_FADT->XGpe1Blk.Address))
966     {
967         /* GPE block 1 exists (has both length and address > 0) */
968
969         RegisterCount1 = (UINT16) (AcpiGbl_FADT->Gpe1BlkLen / 2);
970
971         /* Check for GPE0/GPE1 overlap (if both banks exist) */
972
973         if ((RegisterCount0) &&
974             (GpeNumberMax >= AcpiGbl_FADT->Gpe1Base))
975         {
976             ACPI_REPORT_ERROR ((
977                 "GPE0 block (GPE 0 to %d) overlaps the GPE1 block (GPE %d to %d) - Ignoring GPE1\n",
978                 GpeNumberMax, AcpiGbl_FADT->Gpe1Base,
979                 AcpiGbl_FADT->Gpe1Base +
980                 ((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1)));
981
982             /* Ignore GPE1 block by setting the register count to zero */
983
984             RegisterCount1 = 0;
985         }
986         else
987         {
988             /* Install GPE Block 1 */
989
990             Status = AcpiEvCreateGpeBlock (GpeDevice, &AcpiGbl_FADT->XGpe1Blk,
991                         RegisterCount1, AcpiGbl_FADT->Gpe1Base,
992                         AcpiGbl_FADT->SciInt, &AcpiGbl_GpeFadtBlocks[1]);
993             if (ACPI_FAILURE (Status))
994             {
995                 ACPI_REPORT_ERROR ((
996                     "Could not create GPE Block 1, %s\n",
997                     AcpiFormatException (Status)));
998             }
999
1000             /*
1001              * GPE0 and GPE1 do not have to be contiguous in the GPE number
1002              * space. However, GPE0 always starts at GPE number zero.
1003              */
1004             GpeNumberMax = AcpiGbl_FADT->Gpe1Base +
1005                                 ((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1);
1006         }
1007     }
1008
1009     /* Exit if there are no GPE registers */
1010
1011     if ((RegisterCount0 + RegisterCount1) == 0)
1012     {
1013         /* GPEs are not required by ACPI, this is OK */
1014
1015         ACPI_REPORT_INFO (("There are no GPE blocks defined in the FADT\n"));
1016         return_ACPI_STATUS (AE_OK);
1017     }
1018
1019     /* Check for Max GPE number out-of-range */
1020
1021     if (GpeNumberMax > ACPI_GPE_MAX)
1022     {
1023         ACPI_REPORT_ERROR (("Maximum GPE number from FADT is too large: 0x%X\n",
1024             GpeNumberMax));
1025         return_ACPI_STATUS (AE_BAD_VALUE);
1026     }
1027
1028     return_ACPI_STATUS (AE_OK);
1029 }
1030
1031