Merge from vendor branch INTEL_ACPICA:
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / tools / acpiexec / aeexec.c
1 /******************************************************************************
2  *
3  * Module Name: aeexec - Support routines for AcpiExec utility
4  *              $Revision: 1.113 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2006, 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 "aecommon.h"
118
119 #define _COMPONENT          ACPI_TOOLS
120         ACPI_MODULE_NAME    ("aeexec")
121
122
123 ACPI_PARSE_OBJECT           *root;
124 UINT8                       *AmlStart;
125 UINT32                      AmlLength;
126 UINT8                       *DsdtPtr;
127 UINT32                      AcpiDsdtLength;
128
129 DEBUG_REGIONS               AeRegions;
130 ACPI_TABLE_RSDP             LocalRsdp;
131
132 /*
133  * Misc ACPI tables to be installed
134  */
135 unsigned char Ssdt1Code[] =
136 {
137     0x53,0x53,0x44,0x54,0x30,0x00,0x00,0x00,  /* 00000000    "SSDT0..." */
138     0x01,0xB8,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
139     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
140     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
141     0x24,0x04,0x03,0x20,0x14,0x0B,0x5F,0x54,  /* 00000020    "$.. .._T" */
142     0x39,0x38,0x00,0x70,0x0A,0x04,0x60,0xA4,  /* 00000028    "98.p..`." */
143 };
144
145 unsigned char Ssdt2Code[] =
146 {
147     0x53,0x53,0x44,0x54,0x30,0x00,0x00,0x00,  /* 00000000    "SSDT0..." */
148     0x01,0xB7,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
149     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
150     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
151     0x24,0x04,0x03,0x20,0x14,0x0B,0x5F,0x54,  /* 00000020    "$.. .._T" */
152     0x39,0x39,0x00,0x70,0x0A,0x04,0x60,0xA4,  /* 00000028    "99.p..`." */
153 };
154
155 unsigned char Oem1Code[] =
156 {
157     0x4F,0x45,0x4D,0x31,0x38,0x00,0x00,0x00,  /* 00000000    "OEM18..." */
158     0x01,0x4B,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    ".KIntel." */
159     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
160     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
161     0x18,0x09,0x03,0x20,0x08,0x5F,0x58,0x54,  /* 00000020    "... ._XT" */
162     0x32,0x0A,0x04,0x14,0x0C,0x5F,0x58,0x54,  /* 00000028    "2...._XT" */
163     0x31,0x00,0x70,0x01,0x5F,0x58,0x54,0x32,  /* 00000030    "1.p._XT2" */
164 };
165
166 /*
167  * We need a local FADT so that the hardware subcomponent will function,
168  * even though the underlying OSD HW access functions don't do
169  * anything.
170  */
171 ACPI_TABLE_RSDP             LocalRSDP;
172 ACPI_TABLE_FADT             LocalFADT;
173 ACPI_TABLE_FACS             LocalFACS;
174 ACPI_TABLE_HEADER           LocalDSDT;
175 ACPI_TABLE_HEADER           LocalTEST;
176 ACPI_TABLE_HEADER           LocalBADTABLE;
177
178 ACPI_TABLE_RSDT             *LocalRSDT;
179
180 #define RSDT_TABLES         7
181 #define RSDT_SIZE           (sizeof (ACPI_TABLE_RSDT) + ((RSDT_TABLES -1) * sizeof (UINT32)))
182
183 #define ACPI_MAX_INIT_TABLES (16)
184 static ACPI_TABLE_DESC      Tables[ACPI_MAX_INIT_TABLES];
185
186 UINT32                      SigintCount = 0;
187
188 /******************************************************************************
189  *
190  * FUNCTION:    AeCtrlCHandler
191  *
192  * PARAMETERS:  Sig
193  *
194  * RETURN:      none
195  *
196  * DESCRIPTION: Control-C handler.  Abort running control method if any.
197  *
198  *****************************************************************************/
199
200 void __cdecl
201 AeCtrlCHandler (
202     int                     Sig)
203 {
204
205     signal (SIGINT, SIG_IGN);
206     SigintCount++;
207
208     AcpiOsPrintf ("Caught a ctrl-c (#%d)\n\n", SigintCount);
209
210     if (AcpiGbl_MethodExecuting)
211     {
212         AcpiGbl_AbortMethod = TRUE;
213         signal (SIGINT, AeCtrlCHandler);
214
215         if (SigintCount < 10)
216         {
217             return;
218         }
219     }
220
221     exit (0);
222 }
223
224
225 /******************************************************************************
226  *
227  * FUNCTION:    AeBuildLocalTables
228  *
229  * PARAMETERS:
230  *
231  * RETURN:      Status
232  *
233  * DESCRIPTION:
234  *
235  *****************************************************************************/
236
237 ACPI_STATUS
238 AeBuildLocalTables (
239     ACPI_TABLE_HEADER       *UserTable)
240 {
241
242     /* Build an RSDT */
243
244     LocalRSDT = AcpiOsAllocate (RSDT_SIZE);
245     if (!LocalRSDT)
246     {
247         return AE_NO_MEMORY;
248     }
249
250     ACPI_MEMSET (LocalRSDT, 0, RSDT_SIZE);
251     ACPI_STRNCPY (LocalRSDT->Header.Signature, ACPI_SIG_RSDT, 4);
252     LocalRSDT->Header.Length = RSDT_SIZE;
253
254     LocalRSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);
255     LocalRSDT->TableOffsetEntry[1] = ACPI_PTR_TO_PHYSADDR (&LocalBADTABLE);
256     LocalRSDT->TableOffsetEntry[2] = ACPI_PTR_TO_PHYSADDR (&LocalFADT);
257     LocalRSDT->TableOffsetEntry[3] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);  /* Just a placeholder for a user SSDT */
258
259     /* Install two SSDTs to test multiple table support */
260
261     LocalRSDT->TableOffsetEntry[4] = ACPI_PTR_TO_PHYSADDR (&Ssdt1Code);
262     LocalRSDT->TableOffsetEntry[5] = ACPI_PTR_TO_PHYSADDR (&Ssdt2Code);
263
264     /* Install the OEM1 table to test LoadTable */
265
266     LocalRSDT->TableOffsetEntry[6] = ACPI_PTR_TO_PHYSADDR (&Oem1Code);
267
268     /* Build an RSDP */
269
270     ACPI_MEMSET (&LocalRSDP, 0, sizeof (ACPI_TABLE_RSDP));
271     ACPI_MEMCPY (LocalRSDP.Signature, ACPI_SIG_RSDP, 8);
272     LocalRSDP.Revision = 1;
273     LocalRSDP.RsdtPhysicalAddress = ACPI_PTR_TO_PHYSADDR (LocalRSDT);
274     LocalRSDP.Length = sizeof (ACPI_TABLE_RSDT);
275     ACPI_MEMCPY (LocalRSDP.OemId, "I_TEST", 6);
276
277     /*
278      * Examine the incoming user table.  At this point, it has been verified
279      * to be either a DSDT, SSDT, or a PSDT, but they must be handled differently
280      */
281     if (!ACPI_COMPARE_NAME (UserTable->Signature, ACPI_SIG_DSDT))
282     {
283         /* Build a local DSDT because incoming table is an SSDT or PSDT */
284
285         ACPI_MEMSET (&LocalDSDT, 0, sizeof (ACPI_TABLE_HEADER));
286         ACPI_STRNCPY (LocalDSDT.Signature, ACPI_SIG_DSDT, 4);
287         LocalDSDT.Revision = 1;
288         LocalDSDT.Length = sizeof (ACPI_TABLE_HEADER);
289         LocalDSDT.Checksum = (UINT8) -AcpiTbChecksum ((void *) &LocalDSDT, LocalDSDT.Length);
290
291         /* Install incoming table (SSDT or PSDT) directly into the RSDT */
292
293         LocalRSDT->TableOffsetEntry[3] = ACPI_PTR_TO_PHYSADDR (UserTable);
294     }
295
296     /* Set checksums for both RSDT and RSDP */
297
298     LocalRSDT->Header.Checksum = (UINT8) -AcpiTbChecksum ((void *) LocalRSDT, LocalRSDT->Header.Length);
299     LocalRSDP.Checksum = (UINT8) -AcpiTbChecksum ((void *) &LocalRSDP, ACPI_RSDP_CHECKSUM_LENGTH);
300
301     /* Build a FADT so we can test the hardware/event init */
302
303     ACPI_MEMSET (&LocalFADT, 0, sizeof (ACPI_TABLE_FADT));
304     ACPI_STRNCPY (LocalFADT.Header.Signature, ACPI_SIG_FADT, 4);
305
306     LocalFADT.Facs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
307     if (!ACPI_COMPARE_NAME (UserTable->Signature, ACPI_SIG_DSDT))
308     {
309         LocalFADT.Dsdt = ACPI_PTR_TO_PHYSADDR (&LocalDSDT);
310     }
311     else
312     {
313         LocalFADT.Dsdt = ACPI_PTR_TO_PHYSADDR (UserTable);
314     }
315
316     /* Short (V1) FADT */
317
318     LocalFADT.Header.Revision = 1;
319     LocalFADT.Header.Length = ACPI_FADT_OFFSET (Flags);
320     LocalFADT.Gpe0BlockLength = 16;
321     LocalFADT.Gpe1BlockLength = 6;
322     LocalFADT.Gpe1Base = 96;
323
324     LocalFADT.Pm1EventLength = 4;
325     LocalFADT.Pm1ControlLength = 4;
326     LocalFADT.PmTimerLength  = 8;
327
328     LocalFADT.Gpe0Block = 0x12340000;
329     LocalFADT.Gpe1Block = 0x56780000;
330
331     LocalFADT.Pm1aEventBlock = 0x1aaa0000;
332     LocalFADT.Pm1bEventBlock = 0;
333     LocalFADT.PmTimerBlock = 0xA0;
334     LocalFADT.Pm1aControlBlock = 0xB0;
335
336     /* Complete the FADT with the checksum */
337
338     LocalFADT.Header.Checksum = 0;
339     LocalFADT.Header.Checksum = (UINT8) -AcpiTbChecksum ((void *) &LocalFADT, LocalFADT.Header.Length);
340
341     /* Build a FACS */
342
343     ACPI_MEMSET (&LocalFACS, 0, sizeof (ACPI_TABLE_FACS));
344     ACPI_STRNCPY (LocalFACS.Signature, ACPI_SIG_FACS, 4);
345     LocalFACS.Length = sizeof (ACPI_TABLE_FACS);
346     LocalFACS.GlobalLock = 0x11AA0011;
347
348     /* Build a fake table [TEST] so that we make sure that the CA core ignores it */
349
350     ACPI_MEMSET (&LocalTEST, 0, sizeof (ACPI_TABLE_HEADER));
351     ACPI_STRNCPY (LocalTEST.Signature, "TEST", 4);
352
353     LocalTEST.Revision = 1;
354     LocalTEST.Length = sizeof (ACPI_TABLE_HEADER);
355     LocalTEST.Checksum = (UINT8) -AcpiTbChecksum ((void *) &LocalTEST, LocalTEST.Length);
356
357     /* Build a fake table with a bad signature [BAD!] so that we make sure that the CA core ignores it */
358
359     ACPI_MEMSET (&LocalBADTABLE, 0, sizeof (ACPI_TABLE_HEADER));
360     ACPI_STRNCPY (LocalBADTABLE.Signature, "BAD!", 4);
361
362     LocalBADTABLE.Revision = 1;
363     LocalBADTABLE.Length = sizeof (ACPI_TABLE_HEADER);
364     LocalBADTABLE.Checksum = (UINT8) -AcpiTbChecksum ((void *) &LocalBADTABLE, LocalBADTABLE.Length);
365
366     return (AE_OK);
367 }
368
369
370 /******************************************************************************
371  *
372  * FUNCTION:    AeInstallTables
373  *
374  * PARAMETERS:  None
375  *
376  * RETURN:      Status
377  *
378  * DESCRIPTION: Install the various ACPI tables
379  *
380  *****************************************************************************/
381
382 ACPI_STATUS
383 AeInstallTables (
384     void)
385 {
386     ACPI_STATUS             Status;
387
388     Status = AcpiInitializeTables (Tables, ACPI_MAX_INIT_TABLES, TRUE);
389     Status = AcpiReallocateRootTable ();
390     Status = AcpiLoadTables ();
391     return (Status);
392 }
393
394
395 /******************************************************************************
396  *
397  * FUNCTION:    AeLocalGetRootPointer
398  *
399  * PARAMETERS:  Flags       - not used
400  *              Address     - Where the root pointer is returned
401  *
402  * RETURN:      Status
403  *
404  * DESCRIPTION: Return a local RSDP, used to dynamically load tables via the
405  *              standard ACPI mechanism.
406  *
407  *****************************************************************************/
408
409 ACPI_PHYSICAL_ADDRESS
410 AeLocalGetRootPointer (
411     void)
412 {
413
414     return ((ACPI_PHYSICAL_ADDRESS) &LocalRSDP);
415 }
416
417
418 /******************************************************************************
419  *
420  * FUNCTION:    AeRegionHandler
421  *
422  * PARAMETERS:  Standard region handler parameters
423  *
424  * RETURN:      Status
425  *
426  * DESCRIPTION: Test handler - Handles some dummy regions via memory that can
427  *              be manipulated in Ring 3.
428  *
429  *****************************************************************************/
430
431 ACPI_STATUS
432 AeRegionHandler (
433     UINT32                  Function,
434     ACPI_PHYSICAL_ADDRESS   Address,
435     UINT32                  BitWidth,
436     ACPI_INTEGER            *Value,
437     void                    *HandlerContext,
438     void                    *RegionContext)
439 {
440
441     ACPI_OPERAND_OBJECT     *RegionObject = (ACPI_OPERAND_OBJECT*) RegionContext;
442     ACPI_PHYSICAL_ADDRESS   BaseAddress;
443     ACPI_SIZE               Length;
444     BOOLEAN                 BufferExists;
445     REGION                  *RegionElement;
446     void                    *BufferValue;
447     UINT32                  ByteWidth;
448     UINT32                  i;
449     UINT8                   SpaceId;
450
451
452     ACPI_FUNCTION_NAME (AeRegionHandler);
453
454     /*
455      * If the object is not a region, simply return
456      */
457     if (RegionObject->Region.Type != ACPI_TYPE_REGION)
458     {
459         return AE_OK;
460     }
461
462     /*
463      * Find the region's address space and length before searching
464      * the linked list.
465      */
466     BaseAddress = RegionObject->Region.Address;
467     Length = (ACPI_SIZE) RegionObject->Region.Length;
468     SpaceId = RegionObject->Region.SpaceId;
469
470     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION, "Operation Region request on %s at 0x%X\n",
471             AcpiUtGetRegionName (RegionObject->Region.SpaceId),
472             (UINT32) Address));
473
474     if (SpaceId == ACPI_ADR_SPACE_SMBUS)
475     {
476         Length = 0;
477
478         switch (Function & ACPI_IO_MASK)
479         {
480         case ACPI_READ:
481             switch (Function >> 16)
482             {
483             case AML_FIELD_ATTRIB_SMB_QUICK:
484             case AML_FIELD_ATTRIB_SMB_SEND_RCV:
485             case AML_FIELD_ATTRIB_SMB_BYTE:
486                 Length = 1;
487                 break;
488
489             case AML_FIELD_ATTRIB_SMB_WORD:
490             case AML_FIELD_ATTRIB_SMB_WORD_CALL:
491                 Length = 2;
492                 break;
493
494             case AML_FIELD_ATTRIB_SMB_BLOCK:
495             case AML_FIELD_ATTRIB_SMB_BLOCK_CALL:
496                 Length = 32;
497                 break;
498
499             default:
500                 break;
501             }
502             break;
503
504         case ACPI_WRITE:
505             switch (Function >> 16)
506             {
507             case AML_FIELD_ATTRIB_SMB_QUICK:
508             case AML_FIELD_ATTRIB_SMB_SEND_RCV:
509             case AML_FIELD_ATTRIB_SMB_BYTE:
510             case AML_FIELD_ATTRIB_SMB_WORD:
511             case AML_FIELD_ATTRIB_SMB_BLOCK:
512                 Length = 0;
513                 break;
514
515             case AML_FIELD_ATTRIB_SMB_WORD_CALL:
516                 Length = 2;
517                 break;
518
519             case AML_FIELD_ATTRIB_SMB_BLOCK_CALL:
520                 Length = 32;
521                 break;
522
523             default:
524                 break;
525             }
526             break;
527
528         default:
529             break;
530         }
531
532         for (i = 0; i < Length; i++)
533         {
534             ((UINT8 *) Value)[i+2] = (UINT8) (0xA0 + i);
535         }
536
537         ((UINT8 *) Value)[0] = 0x7A;
538         ((UINT8 *) Value)[1] = (UINT8) Length;
539
540         return AE_OK;
541     }
542
543     /*
544      * Search through the linked list for this region's buffer
545      */
546     BufferExists = FALSE;
547     RegionElement = AeRegions.RegionList;
548
549     if (AeRegions.NumberOfRegions)
550     {
551         while (!BufferExists && RegionElement)
552         {
553             if (RegionElement->Address == BaseAddress &&
554                 RegionElement->Length == Length &&
555                 RegionElement->SpaceId == SpaceId)
556             {
557                 BufferExists = TRUE;
558             }
559             else
560             {
561                 RegionElement = RegionElement->NextRegion;
562             }
563         }
564     }
565
566     /*
567      * If the Region buffer does not exist, create it now
568      */
569     if (!BufferExists)
570     {
571         /*
572          * Do the memory allocations first
573          */
574         RegionElement = AcpiOsAllocate (sizeof (REGION));
575         if (!RegionElement)
576         {
577             return AE_NO_MEMORY;
578         }
579
580         RegionElement->Buffer = AcpiOsAllocate (Length);
581         if (!RegionElement->Buffer)
582         {
583             AcpiOsFree (RegionElement);
584             return AE_NO_MEMORY;
585         }
586
587         ACPI_MEMSET (RegionElement->Buffer, 0, Length);
588         RegionElement->Address      = BaseAddress;
589         RegionElement->Length       = Length;
590         RegionElement->SpaceId      = SpaceId;
591         RegionElement->NextRegion   = NULL;
592
593         /*
594          * Increment the number of regions and put this one
595          *  at the head of the list as it will probably get accessed
596          *  more often anyway.
597          */
598         AeRegions.NumberOfRegions += 1;
599
600         if (NULL != AeRegions.RegionList)
601         {
602             RegionElement->NextRegion = AeRegions.RegionList->NextRegion;
603         }
604
605         AeRegions.RegionList = RegionElement;
606     }
607
608     /*
609      * Calculate the size of the memory copy
610      */
611     ByteWidth = (BitWidth / 8);
612
613     if (BitWidth % 8)
614     {
615         ByteWidth += 1;
616     }
617
618     /*
619      * The buffer exists and is pointed to by RegionElement.
620      * We now need to verify the request is valid and perform the operation.
621      *
622      * NOTE: RegionElement->Length is in bytes, therefore it we compare against
623      * ByteWidth (see above)
624      */
625     if (((ACPI_INTEGER) Address + ByteWidth) >
626         ((ACPI_INTEGER)(RegionElement->Address) + RegionElement->Length))
627     {
628         ACPI_WARNING ((AE_INFO,
629             "Request on [%4.4s] is beyond region limit Req-%X+%X, Base=%X, Len-%X",
630             (RegionObject->Region.Node)->Name.Ascii, (UINT32) Address,
631             ByteWidth, (UINT32)(RegionElement->Address),
632             RegionElement->Length));
633
634         return AE_AML_REGION_LIMIT;
635     }
636
637     /*
638      * Get BufferValue to point to the "address" in the buffer
639      */
640     BufferValue = ((UINT8 *) RegionElement->Buffer +
641                     ((ACPI_INTEGER) Address - (ACPI_INTEGER) RegionElement->Address));
642
643     /*
644      * Perform a read or write to the buffer space
645      */
646     switch (Function)
647     {
648     case ACPI_READ:
649         /*
650          * Set the pointer Value to whatever is in the buffer
651          */
652         ACPI_MEMCPY (Value, BufferValue, ByteWidth);
653         break;
654
655     case ACPI_WRITE:
656         /*
657          * Write the contents of Value to the buffer
658          */
659         ACPI_MEMCPY (BufferValue, Value, ByteWidth);
660         break;
661
662     default:
663         return AE_BAD_PARAMETER;
664     }
665     return AE_OK;
666 }
667
668
669 /******************************************************************************
670  *
671  * FUNCTION:    AeRegionInit
672  *
673  * PARAMETERS:  None
674  *
675  * RETURN:      Status
676  *
677  * DESCRIPTION: Opregion init function.
678  *
679  *****************************************************************************/
680
681 ACPI_STATUS
682 AeRegionInit (
683     ACPI_HANDLE                 RegionHandle,
684     UINT32                      Function,
685     void                        *HandlerContext,
686     void                        **RegionContext)
687 {
688     /*
689      * Real simple, set the RegionContext to the RegionHandle
690      */
691     *RegionContext = RegionHandle;
692
693     return AE_OK;
694 }
695
696
697 /******************************************************************************
698  *
699  * FUNCTION:    AeNotifyHandler
700  *
701  * PARAMETERS:  Standard notify handler parameters
702  *
703  * RETURN:      Status
704  *
705  * DESCRIPTION: System notify handler for AcpiExec utility.  Used by the ASL
706  *              test suite(s) to communicate errors and other information to
707  *              this utility via the Notify() operator.
708  *
709  *****************************************************************************/
710
711 void
712 AeNotifyHandler (
713     ACPI_HANDLE                 Device,
714     UINT32                      Value,
715     void                        *Context)
716 {
717
718     switch (Value)
719     {
720 #if 0
721     case 0:
722         printf ("**** Method Error 0x%X: Results not equal\n", Value);
723         if (AcpiGbl_DebugFile)
724         {
725             AcpiOsPrintf ("**** Method Error: Results not equal\n");
726         }
727         break;
728
729
730     case 1:
731         printf ("**** Method Error: Incorrect numeric result\n");
732         if (AcpiGbl_DebugFile)
733         {
734             AcpiOsPrintf ("**** Method Error: Incorrect numeric result\n");
735         }
736         break;
737
738
739     case 2:
740         printf ("**** Method Error: An operand was overwritten\n");
741         if (AcpiGbl_DebugFile)
742         {
743             AcpiOsPrintf ("**** Method Error: An operand was overwritten\n");
744         }
745         break;
746
747 #endif
748
749     default:
750         printf ("**** Received a Notify on Device [%4.4s] %p value 0x%X\n",
751             AcpiUtGetNodeName (Device), Device, Value);
752         if (AcpiGbl_DebugFile)
753         {
754             AcpiOsPrintf ("**** Received a notify, value 0x%X\n", Value);
755         }
756         break;
757     }
758
759 }
760
761
762 /******************************************************************************
763  *
764  * FUNCTION:    AeExceptionHandler
765  *
766  * PARAMETERS:  Standard exception handler parameters
767  *
768  * RETURN:      Status
769  *
770  * DESCRIPTION: System exception handler for AcpiExec utility.
771  *
772  *****************************************************************************/
773
774 ACPI_STATUS
775 AeExceptionHandler (
776     ACPI_STATUS             AmlStatus,
777     ACPI_NAME               Name,
778     UINT16                  Opcode,
779     UINT32                  AmlOffset,
780     void                    *Context)
781 {
782     ACPI_STATUS             NewAmlStatus = AmlStatus;
783     ACPI_STATUS             Status;
784     ACPI_BUFFER             ReturnObj;
785     ACPI_OBJECT_LIST        ArgList;
786     ACPI_OBJECT             Arg[2];
787     const char              *Exception;
788
789
790     Exception = AcpiFormatException (AmlStatus);
791     AcpiOsPrintf ("**** AcpiExec: Exception %s during execution ", Exception);
792     if (Name)
793     {
794         AcpiOsPrintf ("of method [%4.4s]", (char *) &Name);
795     }
796     else
797     {
798         AcpiOsPrintf ("at module level (table load)");
799     }
800     AcpiOsPrintf (" Opcode [%s] @%X\n", AcpiPsGetOpcodeName (Opcode), AmlOffset);
801
802     /*
803      * Invoke the _ERR method if present
804      *
805      * Setup parameter object
806      */
807     ArgList.Count = 2;
808     ArgList.Pointer = Arg;
809
810     Arg[0].Type = ACPI_TYPE_INTEGER;
811     Arg[0].Integer.Value = AmlStatus;
812
813     Arg[1].Type = ACPI_TYPE_STRING;
814     Arg[1].String.Pointer = (char *) Exception;
815     Arg[1].String.Length = ACPI_STRLEN (Exception);
816
817     /* Setup return buffer */
818
819     ReturnObj.Pointer = NULL;
820     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
821
822     Status = AcpiEvaluateObject (NULL, "\\_ERR", &ArgList, &ReturnObj);
823     if (ACPI_SUCCESS (Status) &&
824         ReturnObj.Pointer)
825     {
826         /* Override original status */
827
828         NewAmlStatus = (ACPI_STATUS)
829             ((ACPI_OBJECT *) ReturnObj.Pointer)->Integer.Value;
830
831         AcpiOsFree (ReturnObj.Pointer);
832     }
833
834     /* Global override */
835
836     if (AcpiGbl_IgnoreErrors)
837     {
838         NewAmlStatus = AE_OK;
839     }
840
841     if (NewAmlStatus != AmlStatus)
842     {
843         AcpiOsPrintf ("**** AcpiExec: Exception override, new status %s\n",
844             AcpiFormatException (NewAmlStatus));
845     }
846
847     return (NewAmlStatus);
848 }
849
850
851 /******************************************************************************
852  *
853  * FUNCTION:    AeInstallHandlers
854  *
855  * PARAMETERS:  None
856  *
857  * RETURN:      Status
858  *
859  * DESCRIPTION: Install handlers for the AcpiExec utility.
860  *              NOTE: Currently only a system notify handler is installed.
861  *
862  *****************************************************************************/
863
864 ACPI_ADR_SPACE_TYPE         SpaceId[] = {0, 1, 2, 3, 4, 0x80};
865 #define AEXEC_NUM_REGIONS   6
866
867 ACPI_STATUS
868 AeInstallHandlers (void)
869 {
870     ACPI_STATUS             Status;
871     UINT32                  i;
872     ACPI_HANDLE             Handle;
873
874
875     ACPI_FUNCTION_ENTRY ();
876
877
878     Status = AcpiInstallExceptionHandler (AeExceptionHandler);
879     if (ACPI_FAILURE (Status))
880     {
881         printf ("Could not install exception handler, %s\n",
882             AcpiFormatException (Status));
883     }
884
885     Status = AcpiInstallNotifyHandler (ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
886                                         AeNotifyHandler, NULL);
887     if (ACPI_FAILURE (Status))
888     {
889         printf ("Could not install a global notify handler, %s\n",
890             AcpiFormatException (Status));
891     }
892
893     Status = AcpiGetHandle (NULL, "\\_SB", &Handle);
894     if (ACPI_SUCCESS (Status))
895     {
896         Status = AcpiInstallNotifyHandler (Handle, ACPI_SYSTEM_NOTIFY,
897                                             AeNotifyHandler, NULL);
898         if (ACPI_FAILURE (Status))
899         {
900             printf ("Could not install a notify handler, %s\n",
901                 AcpiFormatException (Status));
902         }
903
904         Status = AcpiRemoveNotifyHandler (Handle, ACPI_SYSTEM_NOTIFY,
905                                             AeNotifyHandler);
906         if (ACPI_FAILURE (Status))
907         {
908             printf ("Could not remove a notify handler, %s\n",
909                 AcpiFormatException (Status));
910         }
911
912         Status = AcpiInstallNotifyHandler (Handle, ACPI_ALL_NOTIFY,
913                                             AeNotifyHandler, NULL);
914         Status = AcpiRemoveNotifyHandler (Handle, ACPI_ALL_NOTIFY,
915                                             AeNotifyHandler);
916         Status = AcpiInstallNotifyHandler (Handle, ACPI_ALL_NOTIFY,
917                                             AeNotifyHandler, NULL);
918         if (ACPI_FAILURE (Status))
919         {
920             printf ("Could not install a notify handler, %s\n",
921                 AcpiFormatException (Status));
922         }
923     }
924     else
925     {
926         printf ("No _SB_ found, %s\n", AcpiFormatException (Status));
927     }
928
929     for (i = 0; i < AEXEC_NUM_REGIONS; i++)
930     {
931         if (i == 2)
932         {
933             continue;
934         }
935
936         Status = AcpiRemoveAddressSpaceHandler (AcpiGbl_RootNode,
937                         SpaceId[i], AeRegionHandler);
938
939         /* Install handler at the root object.
940          * TBD: all default handlers should be installed here!
941          */
942         Status = AcpiInstallAddressSpaceHandler (AcpiGbl_RootNode,
943                         SpaceId[i], AeRegionHandler, AeRegionInit, NULL);
944         if (ACPI_FAILURE (Status))
945         {
946             ACPI_EXCEPTION ((AE_INFO, Status,
947                 "Could not install an OpRegion handler for %s space(%d)",
948                 AcpiUtGetRegionName((UINT8) SpaceId[i]), SpaceId[i]));
949             return (Status);
950         }
951     }
952
953     /*
954      * Initialize the global Region Handler space
955      * MCW 3/23/00
956      */
957     AeRegions.NumberOfRegions = 0;
958     AeRegions.RegionList = NULL;
959
960     return Status;
961 }
962
963
964 /******************************************************************************
965  *
966  * FUNCTION:    AeGpeHandler
967  *
968  * DESCRIPTION: GPE handler for acpiexec
969  *
970  *****************************************************************************/
971
972 UINT32
973 AeGpeHandler (
974     void                        *Context)
975 {
976
977
978     AcpiOsPrintf ("Received a GPE at handler\n");
979     return (0);
980 }
981
982
983 /******************************************************************************
984  *
985  * FUNCTION:    AfInstallGpeBlock
986  *
987  * PARAMETERS:  None
988  *
989  * RETURN:      None
990  *
991  * DESCRIPTION: Various GPE initialization
992  *
993  *****************************************************************************/
994
995 void
996 AfInstallGpeBlock (
997     void)
998 {
999     ACPI_STATUS                 Status;
1000     ACPI_HANDLE                 Handle;
1001     ACPI_HANDLE                 Handle2 = NULL;
1002     ACPI_HANDLE                 Handle3 = NULL;
1003     ACPI_GENERIC_ADDRESS        BlockAddress;
1004
1005
1006     Status = AcpiGetHandle (NULL, "\\_GPE", &Handle);
1007     if (ACPI_FAILURE (Status))
1008     {
1009         return;
1010     }
1011
1012     BlockAddress.SpaceId = 0;
1013     ACPI_STORE_ADDRESS (BlockAddress.Address, 0x76540000);
1014
1015 //    Status = AcpiInstallGpeBlock (Handle, &BlockAddress, 4, 8);
1016
1017     /* Above should fail, ignore */
1018
1019     Status = AcpiGetHandle (NULL, "\\GPE2", &Handle2);
1020     if (ACPI_SUCCESS (Status))
1021     {
1022         Status = AcpiInstallGpeBlock (Handle2, &BlockAddress, 8, 8);
1023
1024         AcpiInstallGpeHandler (Handle2, 8, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
1025         AcpiSetGpeType (Handle2, 8, ACPI_GPE_TYPE_WAKE);
1026         AcpiEnableGpe (Handle2, 8, 0);
1027     }
1028
1029     Status = AcpiGetHandle (NULL, "\\GPE3", &Handle3);
1030     if (ACPI_SUCCESS (Status))
1031     {
1032         Status = AcpiInstallGpeBlock (Handle3, &BlockAddress, 8, 11);
1033     }
1034
1035 //    Status = AcpiRemoveGpeBlock (Handle);
1036 //    Status = AcpiRemoveGpeBlock (Handle2);
1037 //    Status = AcpiRemoveGpeBlock (Handle3);
1038
1039 }
1040
1041
1042 void
1043 AeMiscellaneousTests (
1044     void)
1045 {
1046     ACPI_HANDLE             Handle;
1047     ACPI_BUFFER             ReturnBuf;
1048     char                    Buffer[32];
1049     ACPI_VENDOR_UUID        Uuid = {0, {ACPI_INIT_UUID (0,0,0,0,0,0,0,0,0,0,0)}};
1050     ACPI_STATUS             Status;
1051     UINT32                  LockHandle;
1052
1053
1054     ReturnBuf.Length = 32;
1055     ReturnBuf.Pointer = Buffer;
1056
1057     AcpiGetName (AcpiGbl_RootNode, ACPI_FULL_PATHNAME, &ReturnBuf);
1058     AcpiEnableEvent (ACPI_EVENT_GLOBAL, 0);
1059
1060     AcpiInstallGpeHandler (NULL, 0, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
1061     AcpiSetGpeType (NULL, 0, ACPI_GPE_TYPE_WAKE_RUN);
1062     AcpiEnableGpe (NULL, 0, ACPI_NOT_ISR);
1063     AcpiRemoveGpeHandler (NULL, 0, AeGpeHandler);
1064
1065     AcpiInstallGpeHandler (NULL, 0, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
1066     AcpiSetGpeType (NULL, 0, ACPI_GPE_TYPE_WAKE_RUN);
1067     AcpiEnableGpe (NULL, 0, ACPI_NOT_ISR);
1068
1069     AcpiInstallGpeHandler (NULL, 1, ACPI_GPE_EDGE_TRIGGERED, AeGpeHandler, NULL);
1070     AcpiSetGpeType (NULL, 1, ACPI_GPE_TYPE_RUNTIME);
1071     AcpiEnableGpe (NULL, 1, ACPI_NOT_ISR);
1072
1073     AcpiInstallGpeHandler (NULL, 2, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
1074     AcpiSetGpeType (NULL, 2, ACPI_GPE_TYPE_WAKE);
1075     AcpiEnableGpe (NULL, 2, ACPI_NOT_ISR);
1076
1077     AcpiInstallGpeHandler (NULL, 3, ACPI_GPE_EDGE_TRIGGERED, AeGpeHandler, NULL);
1078     AcpiSetGpeType (NULL, 3, ACPI_GPE_TYPE_WAKE_RUN);
1079
1080     AcpiInstallGpeHandler (NULL, 4, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
1081     AcpiSetGpeType (NULL, 4, ACPI_GPE_TYPE_RUNTIME);
1082
1083     AcpiInstallGpeHandler (NULL, 5, ACPI_GPE_EDGE_TRIGGERED, AeGpeHandler, NULL);
1084     AcpiSetGpeType (NULL, 5, ACPI_GPE_TYPE_WAKE);
1085
1086     AcpiInstallGpeHandler (NULL, 0x19, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
1087     AcpiSetGpeType (NULL, 0x19, ACPI_GPE_TYPE_WAKE_RUN);
1088     AcpiEnableGpe (NULL, 0x19, ACPI_NOT_ISR);
1089
1090     AfInstallGpeBlock ();
1091
1092
1093     Status = AcpiGetHandle (NULL, "RSRC", &Handle);
1094     if (ACPI_SUCCESS (Status))
1095     {
1096         ReturnBuf.Length = ACPI_ALLOCATE_BUFFER;
1097
1098         Status = AcpiGetVendorResource (Handle, "_CRS", &Uuid, &ReturnBuf);
1099         if (ACPI_SUCCESS (Status))
1100         {
1101             AcpiOsFree (ReturnBuf.Pointer);
1102         }
1103     }
1104
1105     /* Test global lock */
1106
1107     Status = AcpiAcquireGlobalLock (0xFFFF, &LockHandle);
1108     if (ACPI_FAILURE (Status))
1109     {
1110         AcpiOsPrintf ("Could not get GlobalLock, %X\n", Status);
1111     }
1112
1113     Status = AcpiAcquireGlobalLock (0x5, &LockHandle); /* Should fail */
1114
1115     Status = AcpiReleaseGlobalLock (LockHandle);
1116     if (ACPI_FAILURE (Status))
1117     {
1118         AcpiOsPrintf ("Could not release GlobalLock, %X\n", Status);
1119     }
1120
1121 }
1122