]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/aeexec.c
This commit was generated by cvs2svn to compensate for changes in r154180,
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / aeexec.c
1 /******************************************************************************
2  *
3  * Module Name: aeexec - Support routines for AcpiExec utility
4  *              $Revision: 1.88 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2005, 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 <contrib/dev/acpica/aecommon.h>
118
119 #define _COMPONENT          ACPI_TOOLS
120         ACPI_MODULE_NAME    ("aeexec")
121
122
123 ACPI_PARSE_OBJECT           *AcpiGbl_ParsedNamespaceRoot;
124 ACPI_PARSE_OBJECT           *root;
125 UINT8                       *AmlStart;
126 UINT32                      AmlLength;
127 UINT8                       *DsdtPtr;
128 UINT32                      AcpiDsdtLength;
129
130 DEBUG_REGIONS               AeRegions;
131 RSDP_DESCRIPTOR             LocalRsdp;
132
133 /*
134  * Misc ACPI tables to be installed
135  */
136 unsigned char Ssdt1Code[] =
137 {
138     0x53,0x53,0x44,0x54,0x30,0x00,0x00,0x00,  /* 00000000    "SSDT0..." */
139     0x01,0xB8,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
140     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
141     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
142     0x24,0x04,0x03,0x20,0x14,0x0B,0x5F,0x54,  /* 00000020    "$.. .._T" */
143     0x39,0x38,0x00,0x70,0x0A,0x04,0x60,0xA4,  /* 00000028    "98.p..`." */
144 };
145
146 unsigned char Ssdt2Code[] =
147 {
148     0x53,0x53,0x44,0x54,0x30,0x00,0x00,0x00,  /* 00000000    "SSDT0..." */
149     0x01,0xB7,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
150     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
151     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
152     0x24,0x04,0x03,0x20,0x14,0x0B,0x5F,0x54,  /* 00000020    "$.. .._T" */
153     0x39,0x39,0x00,0x70,0x0A,0x04,0x60,0xA4,  /* 00000028    "99.p..`." */
154 };
155
156 unsigned char Oem1Code[] =
157 {
158     0x4F,0x45,0x4D,0x31,0x38,0x00,0x00,0x00,  /* 00000000    "OEM18..." */
159     0x01,0x4B,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    ".KIntel." */
160     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
161     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
162     0x18,0x09,0x03,0x20,0x08,0x5F,0x58,0x54,  /* 00000020    "... ._XT" */
163     0x32,0x0A,0x04,0x14,0x0C,0x5F,0x58,0x54,  /* 00000028    "2...._XT" */
164     0x31,0x00,0x70,0x01,0x5F,0x58,0x54,0x32,  /* 00000030    "1.p._XT2" */
165
166 };
167
168 /*
169  * We need a local FADT so that the hardware subcomponent will function,
170  * even though the underlying OSD HW access functions don't do
171  * anything.
172  */
173 RSDP_DESCRIPTOR             LocalRSDP;
174 FADT_DESCRIPTOR_REV1        LocalFADT;
175 FACS_DESCRIPTOR_REV1        LocalFACS;
176 ACPI_TABLE_HEADER           LocalDSDT;
177 ACPI_TABLE_HEADER           LocalTEST;
178 ACPI_TABLE_HEADER           LocalBADTABLE;
179
180 RSDT_DESCRIPTOR_REV1        *LocalRSDT;
181
182 #define RSDT_TABLES         7
183 #define RSDT_SIZE           (sizeof (RSDT_DESCRIPTOR_REV1) + ((RSDT_TABLES -1) * sizeof (UINT32)))
184
185
186 /******************************************************************************
187  *
188  * FUNCTION:    AeCtrlCHandler
189  *
190  * PARAMETERS:  Sig
191  *
192  * RETURN:      none
193  *
194  * DESCRIPTION: Control-C handler.  Abort running control method if any.
195  *
196  *****************************************************************************/
197
198 void
199 AeCtrlCHandler (
200     int                     Sig)
201 {
202
203     signal (SIGINT, SIG_IGN);
204
205     AcpiOsPrintf ("Caught a ctrl-c\n\n");
206
207     if (AcpiGbl_MethodExecuting)
208     {
209         AcpiGbl_AbortMethod = TRUE;
210         signal (SIGINT, AeCtrlCHandler);
211     }
212     else
213     {
214         exit (0);
215     }
216 }
217
218
219 /******************************************************************************
220  *
221  * FUNCTION:    AeBuildLocalTables
222  *
223  * PARAMETERS:
224  *
225  * RETURN:      Status
226  *
227  * DESCRIPTION:
228  *
229  *****************************************************************************/
230
231 ACPI_STATUS
232 AeBuildLocalTables (
233     ACPI_TABLE_HEADER       *UserTable)
234 {
235
236
237     /* Build an RSDT */
238
239     LocalRSDT = AcpiOsAllocate (RSDT_SIZE);
240     if (!LocalRSDT)
241     {
242         return AE_NO_MEMORY;
243     }
244
245     ACPI_MEMSET (LocalRSDT, 0, RSDT_SIZE);
246     ACPI_STRNCPY (LocalRSDT->Signature, RSDT_SIG, 4);
247     LocalRSDT->Length = RSDT_SIZE;
248
249     LocalRSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);
250     LocalRSDT->TableOffsetEntry[1] = ACPI_PTR_TO_PHYSADDR (&LocalBADTABLE);
251     LocalRSDT->TableOffsetEntry[2] = ACPI_PTR_TO_PHYSADDR (&LocalFADT);
252     LocalRSDT->TableOffsetEntry[3] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);  /* Just a placeholder for a user SSDT */
253
254     /* Install two SSDTs to test multiple table support */
255
256     LocalRSDT->TableOffsetEntry[4] = ACPI_PTR_TO_PHYSADDR (&Ssdt1Code);
257     LocalRSDT->TableOffsetEntry[5] = ACPI_PTR_TO_PHYSADDR (&Ssdt2Code);
258
259     /* Install the OEM1 table to test LoadTable */
260
261     LocalRSDT->TableOffsetEntry[6] = ACPI_PTR_TO_PHYSADDR (&Oem1Code);
262
263     /* Build an RSDP */
264
265     ACPI_MEMSET (&LocalRSDP, 0, sizeof (RSDP_DESCRIPTOR));
266     ACPI_STRNCPY (LocalRSDP.Signature, RSDP_SIG, 8);
267     LocalRSDP.Revision            = 1;
268     LocalRSDP.RsdtPhysicalAddress = ACPI_PTR_TO_PHYSADDR (LocalRSDT);
269
270     AcpiGbl_RSDP = &LocalRSDP;
271
272     /*
273      * Examine the incoming user table.  At this point, it has been verified
274      * to be either a DSDT, SSDT, or a PSDT, but they must be handled differently
275      */
276     if (!ACPI_STRNCMP ((char *) UserTable->Signature, DSDT_SIG, 4))
277     {
278         /* User DSDT is installed directly into the FADT */
279
280         AcpiGbl_DSDT = UserTable;
281     }
282     else
283     {
284         /* Build a local DSDT because incoming table is an SSDT or PSDT */
285
286         ACPI_MEMSET (&LocalDSDT, 0, sizeof (ACPI_TABLE_HEADER));
287         ACPI_STRNCPY (LocalDSDT.Signature, DSDT_SIG, 4);
288         LocalDSDT.Revision   = 1;
289         LocalDSDT.Length     = sizeof (ACPI_TABLE_HEADER);
290         LocalDSDT.Checksum   = (UINT8) (0 - AcpiTbGenerateChecksum (&LocalDSDT, LocalDSDT.Length));
291
292         AcpiGbl_DSDT = &LocalDSDT;
293
294         /* Install incoming table (SSDT or PSDT) directly into the RSDT */
295
296         LocalRSDT->TableOffsetEntry[3] = ACPI_PTR_TO_PHYSADDR (UserTable);
297     }
298
299     /* Set checksums for both RSDT and RSDP */
300
301     LocalRSDT->Checksum = (UINT8) (0 - AcpiTbGenerateChecksum (LocalRSDT, LocalRSDT->Length));
302     LocalRSDP.Checksum  = (UINT8) (0 - AcpiTbGenerateChecksum (&LocalRSDP, ACPI_RSDP_CHECKSUM_LENGTH));
303
304     /* Build a FADT so we can test the hardware/event init */
305
306     ACPI_MEMSET (&LocalFADT, 0, sizeof (FADT_DESCRIPTOR_REV1));
307     ACPI_STRNCPY (LocalFADT.Signature, FADT_SIG, 4);
308
309     LocalFADT.FirmwareCtrl      = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
310     LocalFADT.Dsdt              = ACPI_PTR_TO_PHYSADDR (AcpiGbl_DSDT);
311     LocalFADT.Revision          = 1;
312     LocalFADT.Length            = sizeof (FADT_DESCRIPTOR_REV1);
313     LocalFADT.Gpe0BlkLen        = 16;
314     LocalFADT.Gpe1BlkLen        = 6;
315     LocalFADT.Gpe1Base          = 96;
316
317     LocalFADT.Pm1EvtLen         = 4;
318     LocalFADT.Pm1CntLen         = 4;
319     LocalFADT.PmTmLen           = 8;
320
321     LocalFADT.Gpe0Blk           = 0x12340000;
322     LocalFADT.Gpe1Blk           = 0x56780000;
323
324     LocalFADT.Pm1aEvtBlk        = 0x1aaa0000;
325     LocalFADT.Pm1bEvtBlk        = 0;
326     LocalFADT.PmTmrBlk          = 0xA0;
327     LocalFADT.Pm1aCntBlk        = 0xB0;
328
329     /* Complete the FADT with the checksum */
330
331     LocalFADT.Checksum = (UINT8) (0 - AcpiTbGenerateChecksum (&LocalFADT, LocalFADT.Length));
332
333     /* Build a FACS */
334
335     ACPI_MEMSET (&LocalFACS, 0, sizeof (FACS_DESCRIPTOR_REV1));
336     ACPI_STRNCPY (LocalFACS.Signature, FACS_SIG, 4);
337     LocalFACS.Length = sizeof (FACS_DESCRIPTOR_REV1);
338     LocalFACS.GlobalLock = 0x11AA0011;
339
340     /* Build a fake table so that we make sure that the CA core ignores it */
341
342     ACPI_MEMSET (&LocalTEST, 0, sizeof (ACPI_TABLE_HEADER));
343     ACPI_STRNCPY (LocalTEST.Signature, "TEST", 4);
344
345     LocalTEST.Revision   = 1;
346     LocalTEST.Length     = sizeof (ACPI_TABLE_HEADER);
347
348     /* Build a fake table with a bad signature so that we make sure that the CA core ignores it */
349
350     ACPI_MEMSET (&LocalBADTABLE, 0, sizeof (ACPI_TABLE_HEADER));
351     ACPI_STRNCPY (LocalBADTABLE.Signature, "BAD!", 4);
352
353     LocalBADTABLE.Revision   = 1;
354     LocalBADTABLE.Length     = sizeof (ACPI_TABLE_HEADER);
355
356     return (AE_OK);
357 }
358
359
360 /******************************************************************************
361  *
362  * FUNCTION:    AeInstallTables
363  *
364  * PARAMETERS:  None
365  *
366  * RETURN:      Status
367  *
368  * DESCRIPTION: Install the various ACPI tables
369  *
370  *****************************************************************************/
371
372 ACPI_STATUS
373 AeInstallTables (void)
374 {
375     ACPI_STATUS             Status;
376
377
378     Status = AcpiLoadTables ();
379
380     /* Test the code that ignores multiple loads of same SSDT */
381
382     (void) AcpiLoadTable ((ACPI_TABLE_HEADER *) Ssdt1Code);
383
384 #if 0
385     Status = AcpiLoadTable ((ACPI_TABLE_HEADER *) &LocalFADT);
386     if (ACPI_FAILURE (Status))
387     {
388         printf ("**** Could not load local FADT, %s\n", AcpiFormatException (Status));
389         return (Status);
390     }
391
392     Status = AcpiLoadTable ((ACPI_TABLE_HEADER *) &LocalFACS);
393     if (ACPI_FAILURE (Status))
394     {
395         printf ("**** Could not load local FACS, %s\n", AcpiFormatException (Status));
396         return (Status);
397     }
398 #endif
399
400     return (Status);
401 }
402
403
404 /******************************************************************************
405  *
406  * FUNCTION:    AeLocalGetRootPointer
407  *
408  * PARAMETERS:  Flags       - not used
409  *              Address     - Where the root pointer is returned
410  *
411  * RETURN:      Status
412  *
413  * DESCRIPTION: Return a local RSDP, used to dynamically load tables via the
414  *              standard ACPI mechanism.
415  *
416  *****************************************************************************/
417
418 ACPI_STATUS
419 AeLocalGetRootPointer (
420     UINT32                  Flags,
421     ACPI_POINTER            *Address)
422 {
423
424     Address->PointerType     = ACPI_LOGICAL_POINTER;
425     Address->Pointer.Logical = &LocalRSDP;
426     return (AE_OK);
427 }
428
429
430 /******************************************************************************
431  *
432  * FUNCTION:    AeRegionHandler
433  *
434  * PARAMETERS:  Standard region handler parameters
435  *
436  * RETURN:      Status
437  *
438  * DESCRIPTION: Test handler - Handles some dummy regions via memory that can
439  *              be manipulated in Ring 3.
440  *
441  *****************************************************************************/
442
443 ACPI_STATUS
444 AeRegionHandler (
445     UINT32                  Function,
446     ACPI_PHYSICAL_ADDRESS   Address,
447     UINT32                  BitWidth,
448     ACPI_INTEGER            *Value,
449     void                    *HandlerContext,
450     void                    *RegionContext)
451 {
452
453     ACPI_OPERAND_OBJECT     *RegionObject = (ACPI_OPERAND_OBJECT*) RegionContext;
454     ACPI_PHYSICAL_ADDRESS   BaseAddress;
455     ACPI_SIZE               Length;
456     BOOLEAN                 BufferExists;
457     REGION                  *RegionElement;
458     void                    *BufferValue;
459     UINT32                  ByteWidth;
460     UINT32                  i;
461
462
463     ACPI_FUNCTION_NAME ("AeRegionHandler");
464
465     /*
466      * If the object is not a region, simply return
467      */
468     if (RegionObject->Region.Type != ACPI_TYPE_REGION)
469     {
470         return AE_OK;
471     }
472
473     /*
474      * Find the region's address space and length before searching
475      * the linked list.
476      */
477     BaseAddress = RegionObject->Region.Address;
478     Length = (ACPI_SIZE) RegionObject->Region.Length;
479
480     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION, "Operation Region request on %s at 0x%X\n",
481             AcpiUtGetRegionName (RegionObject->Region.SpaceId),
482             (UINT32) Address));
483
484     if (RegionObject->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
485     {
486         Length = 0;
487
488         switch (Function & ACPI_IO_MASK)
489         {
490         case ACPI_READ:
491             switch (Function >> 16)
492             {
493             case AML_FIELD_ATTRIB_SMB_QUICK:
494             case AML_FIELD_ATTRIB_SMB_SEND_RCV:
495             case AML_FIELD_ATTRIB_SMB_BYTE:
496                 Length = 1;
497                 break;
498
499             case AML_FIELD_ATTRIB_SMB_WORD:
500             case AML_FIELD_ATTRIB_SMB_WORD_CALL:
501                 Length = 2;
502                 break;
503
504             case AML_FIELD_ATTRIB_SMB_BLOCK:
505             case AML_FIELD_ATTRIB_SMB_BLOCK_CALL:
506                 Length = 32;
507                 break;
508
509             default:
510                 break;
511             }
512             break;
513
514         case ACPI_WRITE:
515             switch (Function >> 16)
516             {
517             case AML_FIELD_ATTRIB_SMB_QUICK:
518             case AML_FIELD_ATTRIB_SMB_SEND_RCV:
519             case AML_FIELD_ATTRIB_SMB_BYTE:
520             case AML_FIELD_ATTRIB_SMB_WORD:
521             case AML_FIELD_ATTRIB_SMB_BLOCK:
522                 Length = 0;
523                 break;
524
525             case AML_FIELD_ATTRIB_SMB_WORD_CALL:
526                 Length = 2;
527                 break;
528
529             case AML_FIELD_ATTRIB_SMB_BLOCK_CALL:
530                 Length = 32;
531                 break;
532
533             default:
534                 break;
535             }
536             break;
537
538         default:
539             break;
540         }
541
542         for (i = 0; i < Length; i++)
543         {
544             ((UINT8 *) Value)[i+2] = (UINT8) (0xA0 + i);
545         }
546
547         ((UINT8 *) Value)[0] = 0x7A;
548         ((UINT8 *) Value)[1] = (UINT8) Length;
549
550         return AE_OK;
551     }
552
553     /*
554      * Search through the linked list for this region's buffer
555      */
556     BufferExists = FALSE;
557     RegionElement = AeRegions.RegionList;
558
559     if (AeRegions.NumberOfRegions)
560     {
561         while (!BufferExists && RegionElement)
562         {
563             if (RegionElement->Address == BaseAddress &&
564                 RegionElement->Length == Length)
565             {
566                 BufferExists = TRUE;
567             }
568             else
569             {
570                 RegionElement = RegionElement->NextRegion;
571             }
572         }
573     }
574
575     /*
576      * If the Region buffer does not exist, create it now
577      */
578     if (!BufferExists)
579     {
580         /*
581          * Do the memory allocations first
582          */
583         RegionElement = AcpiOsAllocate (sizeof (REGION));
584         if (!RegionElement)
585         {
586             return AE_NO_MEMORY;
587         }
588
589         RegionElement->Buffer = AcpiOsAllocate (Length);
590         if (!RegionElement->Buffer)
591         {
592             AcpiOsFree (RegionElement);
593             return AE_NO_MEMORY;
594         }
595
596         ACPI_MEMSET (RegionElement->Buffer, 0, Length);
597         RegionElement->Address      = BaseAddress;
598         RegionElement->Length       = Length;
599         RegionElement->NextRegion   = NULL;
600
601         /*
602          * Increment the number of regions and put this one
603          *  at the head of the list as it will probably get accessed
604          *  more often anyway.
605          */
606         AeRegions.NumberOfRegions += 1;
607
608         if (NULL != AeRegions.RegionList)
609         {
610             RegionElement->NextRegion = AeRegions.RegionList->NextRegion;
611         }
612
613         AeRegions.RegionList = RegionElement;
614     }
615
616     /*
617      * Calculate the size of the memory copy
618      */
619     ByteWidth = (BitWidth / 8);
620
621     if (BitWidth % 8)
622     {
623         ByteWidth += 1;
624     }
625
626     /*
627      * The buffer exists and is pointed to by RegionElement.
628      * We now need to verify the request is valid and perform the operation.
629      *
630      * NOTE: RegionElement->Length is in bytes, therefore it we compare against
631      * ByteWidth (see above)
632      */
633     if (((ACPI_INTEGER) Address + ByteWidth) >
634         ((ACPI_INTEGER)(RegionElement->Address) + RegionElement->Length))
635     {
636         ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
637             "Request on [%4.4s] is beyond region limit Req-%X+%X, Base=%X, Len-%X\n",
638             (RegionObject->Region.Node)->Name.Ascii, (UINT32) Address,
639             ByteWidth, (UINT32)(RegionElement->Address),
640             RegionElement->Length));
641
642         return AE_AML_REGION_LIMIT;
643     }
644
645     /*
646      * Get BufferValue to point to the "address" in the buffer
647      */
648     BufferValue = ((UINT8 *) RegionElement->Buffer +
649                     ((ACPI_INTEGER) Address - (ACPI_INTEGER) RegionElement->Address));
650
651     /*
652      * Perform a read or write to the buffer space
653      */
654     switch (Function)
655     {
656     case ACPI_READ:
657         /*
658          * Set the pointer Value to whatever is in the buffer
659          */
660         ACPI_MEMCPY (Value, BufferValue, ByteWidth);
661         break;
662
663     case ACPI_WRITE:
664         /*
665          * Write the contents of Value to the buffer
666          */
667         ACPI_MEMCPY (BufferValue, Value, ByteWidth);
668         break;
669
670     default:
671         return AE_BAD_PARAMETER;
672     }
673     return AE_OK;
674 }
675
676
677 /******************************************************************************
678  *
679  * FUNCTION:    AeRegionInit
680  *
681  * PARAMETERS:  None
682  *
683  * RETURN:      Status
684  *
685  * DESCRIPTION: Opregion init function.
686  *
687  *****************************************************************************/
688
689 ACPI_STATUS
690 AeRegionInit (
691     ACPI_HANDLE                 RegionHandle,
692     UINT32                      Function,
693     void                        *HandlerContext,
694     void                        **RegionContext)
695 {
696     /*
697      * Real simple, set the RegionContext to the RegionHandle
698      */
699     *RegionContext = RegionHandle;
700
701     return AE_OK;
702 }
703
704
705 /******************************************************************************
706  *
707  * FUNCTION:    AeNotifyHandler
708  *
709  * PARAMETERS:  Standard notify handler parameters
710  *
711  * RETURN:      Status
712  *
713  * DESCRIPTION: System notify handler for AcpiExec utility.  Used by the ASL
714  *              test suite(s) to communicate errors and other information to
715  *              this utility via the Notify() operator.
716  *
717  *****************************************************************************/
718
719 void
720 AeNotifyHandler (
721     ACPI_HANDLE                 Device,
722     UINT32                      Value,
723     void                        *Context)
724 {
725
726     switch (Value)
727     {
728 #if 0
729     case 0:
730         printf ("**** Method Error 0x%X: Results not equal\n", Value);
731         if (AcpiGbl_DebugFile)
732         {
733             AcpiOsPrintf ("**** Method Error: Results not equal\n");
734         }
735         break;
736
737
738     case 1:
739         printf ("**** Method Error: Incorrect numeric result\n");
740         if (AcpiGbl_DebugFile)
741         {
742             AcpiOsPrintf ("**** Method Error: Incorrect numeric result\n");
743         }
744         break;
745
746
747     case 2:
748         printf ("**** Method Error: An operand was overwritten\n");
749         if (AcpiGbl_DebugFile)
750         {
751             AcpiOsPrintf ("**** Method Error: An operand was overwritten\n");
752         }
753         break;
754
755 #endif
756
757     default:
758         printf ("**** Received a Notify on Device [%4.4s] %p value 0x%X\n",
759             AcpiUtGetNodeName (Device), Device, Value);
760         if (AcpiGbl_DebugFile)
761         {
762             AcpiOsPrintf ("**** Received a notify, value 0x%X\n", Value);
763         }
764         break;
765     }
766
767 }
768
769
770 /******************************************************************************
771  *
772  * FUNCTION:    AeExceptionHandler
773  *
774  * PARAMETERS:  Standard exception handler parameters
775  *
776  * RETURN:      Status
777  *
778  * DESCRIPTION: System exception handler for AcpiExec utility.
779  *
780  *****************************************************************************/
781
782 ACPI_STATUS
783 AeExceptionHandler (
784     ACPI_STATUS             AmlStatus,
785     ACPI_NAME               Name,
786     UINT16                  Opcode,
787     UINT32                  AmlOffset,
788     void                    *Context)
789 {
790     ACPI_STATUS             Status;
791     ACPI_BUFFER             ReturnObj;
792     ACPI_OBJECT_LIST        ArgList;
793     ACPI_OBJECT             Arg[2];
794     const char              *Exception;
795
796
797     Exception = AcpiFormatException (AmlStatus);
798     AcpiOsPrintf (
799         "**** AcpiExec Exception: %s during execution of method [%4.4s] Opcode [%s] @%X\n",
800         Exception, (char *) &Name, 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         AmlStatus = (ACPI_STATUS)
829             ((ACPI_OBJECT *) ReturnObj.Pointer)->Integer.Value;
830
831         AcpiOsFree (ReturnObj.Pointer);
832     }
833     return (AmlStatus);
834 }
835
836
837 /******************************************************************************
838  *
839  * FUNCTION:    AeInstallHandlers
840  *
841  * PARAMETERS:  None
842  *
843  * RETURN:      Status
844  *
845  * DESCRIPTION: Install handlers for the AcpiExec utility.
846  *              NOTE: Currently only a system notify handler is installed.
847  *
848  *****************************************************************************/
849
850 ACPI_ADR_SPACE_TYPE         SpaceId[] = {0, 1, 2, 3, 4, 0x80};
851 #define AEXEC_NUM_REGIONS   6
852
853 ACPI_STATUS
854 AeInstallHandlers (void)
855 {
856     ACPI_STATUS             Status;
857     UINT32                  i;
858     ACPI_HANDLE             Handle;
859
860
861     ACPI_FUNCTION_NAME ("AeInstallHandlers");
862
863
864     Status = AcpiInstallExceptionHandler (AeExceptionHandler);
865     if (ACPI_FAILURE (Status))
866     {
867         printf ("Could not install exception handler, %s\n",
868             AcpiFormatException (Status));
869     }
870
871     Status = AcpiInstallNotifyHandler (ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
872                                         AeNotifyHandler, NULL);
873     if (ACPI_FAILURE (Status))
874     {
875         printf ("Could not install a global notify handler, %s\n",
876             AcpiFormatException (Status));
877     }
878
879     Status = AcpiGetHandle (NULL, "\\_SB_", &Handle);
880     if (ACPI_SUCCESS (Status))
881     {
882         Status = AcpiInstallNotifyHandler (Handle, ACPI_SYSTEM_NOTIFY,
883                                             AeNotifyHandler, NULL);
884         if (ACPI_FAILURE (Status))
885         {
886             printf ("Could not install a notify handler, %s\n",
887                 AcpiFormatException (Status));
888         }
889
890         Status = AcpiRemoveNotifyHandler (Handle, ACPI_SYSTEM_NOTIFY,
891                                             AeNotifyHandler);
892         if (ACPI_FAILURE (Status))
893         {
894             printf ("Could not remove a notify handler, %s\n",
895                 AcpiFormatException (Status));
896         }
897
898         Status = AcpiInstallNotifyHandler (Handle, ACPI_ALL_NOTIFY,
899                                             AeNotifyHandler, NULL);
900         Status = AcpiRemoveNotifyHandler (Handle, ACPI_ALL_NOTIFY,
901                                             AeNotifyHandler);
902         Status = AcpiInstallNotifyHandler (Handle, ACPI_ALL_NOTIFY,
903                                             AeNotifyHandler, NULL);
904         if (ACPI_FAILURE (Status))
905         {
906             printf ("Could not install a notify handler, %s\n",
907                 AcpiFormatException (Status));
908         }
909
910     }
911
912     for (i = 0; i < AEXEC_NUM_REGIONS; i++)
913     {
914         if (i == 2)
915         {
916             continue;
917         }
918
919         Status = AcpiRemoveAddressSpaceHandler (AcpiGbl_RootNode,
920                         SpaceId[i], AeRegionHandler);
921
922         /* Install handler at the root object.
923          * TBD: all default handlers should be installed here!
924          */
925         Status = AcpiInstallAddressSpaceHandler (AcpiGbl_RootNode,
926                         SpaceId[i], AeRegionHandler, AeRegionInit, NULL);
927         if (ACPI_FAILURE (Status))
928         {
929             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
930                 "Could not install an OpRegion handler for %s space(%d), %s\n",
931                 AcpiUtGetRegionName((UINT8) SpaceId[i]), SpaceId[i],
932                 AcpiFormatException (Status)));
933             return (Status);
934         }
935     }
936
937     /*
938      * Initialize the global Region Handler space
939      * MCW 3/23/00
940      */
941     AeRegions.NumberOfRegions = 0;
942     AeRegions.RegionList = NULL;
943
944     return Status;
945 }
946
947