Sync ACPICA with Intel's version 20150204.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpiexec / aetables.c
1 /******************************************************************************
2  *
3  * Module Name: aetables - ACPI table setup/install for acpiexec utility
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include "aecommon.h"
45 #include "aetables.h"
46
47 #define _COMPONENT          ACPI_TOOLS
48         ACPI_MODULE_NAME    ("aetables")
49
50 /* Local prototypes */
51
52 static void
53 AeInitializeTableHeader (
54     ACPI_TABLE_HEADER       *Header,
55     char                    *Signature,
56     UINT32                  Length);
57
58 void
59 AeTableOverride (
60     ACPI_TABLE_HEADER       *ExistingTable,
61     ACPI_TABLE_HEADER       **NewTable);
62
63 /* User table (DSDT) */
64
65 static ACPI_TABLE_HEADER        *DsdtToInstallOverride;
66
67 /* Non-AML tables that are constructed locally and installed */
68
69 static ACPI_TABLE_RSDP          LocalRSDP;
70 static ACPI_TABLE_FACS          LocalFACS;
71 static ACPI_TABLE_HEADER        LocalTEST;
72 static ACPI_TABLE_HEADER        LocalBADTABLE;
73
74 /*
75  * We need a local FADT so that the hardware subcomponent will function,
76  * even though the underlying OSD HW access functions don't do anything.
77  */
78 static ACPI_TABLE_FADT          LocalFADT;
79
80 /*
81  * Use XSDT so that both 32- and 64-bit versions of this utility will
82  * function automatically.
83  */
84 static ACPI_TABLE_XSDT          *LocalXSDT;
85
86 #define BASE_XSDT_TABLES        9
87 #define BASE_XSDT_SIZE          ((BASE_XSDT_TABLES) * sizeof (UINT64))
88
89 #define ACPI_MAX_INIT_TABLES    (32)
90 static ACPI_TABLE_DESC          Tables[ACPI_MAX_INIT_TABLES];
91
92
93 /******************************************************************************
94  *
95  * FUNCTION:    AeTableOverride
96  *
97  * DESCRIPTION: Local implementation of AcpiOsTableOverride.
98  *              Exercise the override mechanism
99  *
100  *****************************************************************************/
101
102 void
103 AeTableOverride (
104     ACPI_TABLE_HEADER       *ExistingTable,
105     ACPI_TABLE_HEADER       **NewTable)
106 {
107
108     if (!AcpiGbl_LoadTestTables)
109     {
110         *NewTable = NULL;
111         return;
112     }
113
114     /* This code exercises the table override mechanism in the core */
115
116     if (ACPI_COMPARE_NAME (ExistingTable->Signature, ACPI_SIG_DSDT))
117     {
118         *NewTable = DsdtToInstallOverride;
119     }
120
121     /* This code tests override of dynamically loaded tables */
122
123     else if (ACPI_COMPARE_NAME (ExistingTable->Signature, "OEM9"))
124     {
125         *NewTable = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Ssdt3Code);
126     }
127 }
128
129
130 /******************************************************************************
131  *
132  * FUNCTION:    AeInitializeTableHeader
133  *
134  * PARAMETERS:  Header          - A valid standard ACPI table header
135  *              Signature       - Signature to insert
136  *              Length          - Length of the table
137  *
138  * RETURN:      None. Header is modified.
139  *
140  * DESCRIPTION: Initialize the table header for a local ACPI table.
141  *
142  *****************************************************************************/
143
144 static void
145 AeInitializeTableHeader (
146     ACPI_TABLE_HEADER       *Header,
147     char                    *Signature,
148     UINT32                  Length)
149 {
150
151     ACPI_MOVE_NAME (Header->Signature, Signature);
152     Header->Length = Length;
153
154     Header->OemRevision = 0x1001;
155     ACPI_STRNCPY (Header->OemId, "Intel", ACPI_OEM_ID_SIZE);
156     ACPI_STRNCPY (Header->OemTableId, "AcpiExec", ACPI_OEM_TABLE_ID_SIZE);
157     ACPI_STRNCPY (Header->AslCompilerId, "INTL", ACPI_NAME_SIZE);
158     Header->AslCompilerRevision = 0x20131218;
159 }
160
161
162 /******************************************************************************
163  *
164  * FUNCTION:    AeBuildLocalTables
165  *
166  * PARAMETERS:  TableCount      - Number of tables on the command line
167  *              TableList       - List of actual tables from files
168  *
169  * RETURN:      Status
170  *
171  * DESCRIPTION: Build a complete ACPI table chain, with a local RSDP, XSDT,
172  *              FADT, and several other test tables.
173  *
174  *****************************************************************************/
175
176 ACPI_STATUS
177 AeBuildLocalTables (
178     UINT32                  TableCount,
179     AE_TABLE_DESC           *TableList)
180 {
181     ACPI_PHYSICAL_ADDRESS   DsdtAddress = 0;
182     UINT32                  XsdtSize;
183     AE_TABLE_DESC           *NextTable;
184     UINT32                  NextIndex;
185     ACPI_TABLE_FADT         *ExternalFadt = NULL;
186
187
188     /*
189      * Update the table count. For the DSDT, it is not put into the XSDT.
190      * For the FADT, this table is already accounted for since we usually
191      * install a local FADT.
192      */
193     NextTable = TableList;
194     while (NextTable)
195     {
196         if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_DSDT) ||
197             ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_FADT))
198         {
199             TableCount--;
200         }
201         NextTable = NextTable->Next;
202     }
203
204     XsdtSize = (((TableCount + 1) * sizeof (UINT64)) + sizeof (ACPI_TABLE_HEADER));
205     if (AcpiGbl_LoadTestTables)
206     {
207         XsdtSize += BASE_XSDT_SIZE;
208     }
209
210     /* Build an XSDT */
211
212     LocalXSDT = AcpiOsAllocate (XsdtSize);
213     if (!LocalXSDT)
214     {
215         return (AE_NO_MEMORY);
216     }
217
218     ACPI_MEMSET (LocalXSDT, 0, XsdtSize);
219     AeInitializeTableHeader ((void *) LocalXSDT, ACPI_SIG_XSDT, XsdtSize);
220
221     LocalXSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (&LocalFADT);
222     NextIndex = 1;
223
224     /*
225      * Install the user tables. The DSDT must be installed in the FADT.
226      * All other tables are installed directly into the XSDT.
227      *
228      * Note: The tables are loaded in reverse order from the incoming
229      * input, which makes it match the command line order.
230      */
231     NextTable = TableList;
232     while (NextTable)
233     {
234         /*
235          * Incoming DSDT or FADT are special cases. All other tables are
236          * just immediately installed into the XSDT.
237          */
238         if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_DSDT))
239         {
240             if (DsdtAddress)
241             {
242                 printf ("Already found a DSDT, only one allowed\n");
243                 return (AE_ALREADY_EXISTS);
244             }
245
246             /* The incoming user table is a DSDT */
247
248             DsdtAddress = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
249             DsdtToInstallOverride = NextTable->Table;
250         }
251         else if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_FADT))
252         {
253             ExternalFadt = ACPI_CAST_PTR (ACPI_TABLE_FADT, NextTable->Table);
254             LocalXSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
255         }
256         else
257         {
258             /* Install the table in the XSDT */
259
260             LocalXSDT->TableOffsetEntry[TableCount - NextIndex + 1] =
261                 ACPI_PTR_TO_PHYSADDR (NextTable->Table);
262             NextIndex++;
263         }
264
265         NextTable = NextTable->Next;
266     }
267
268     /* Install the optional extra local tables */
269
270     if (AcpiGbl_LoadTestTables)
271     {
272         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);
273         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&LocalBADTABLE);
274
275         /* Install two SSDTs to test multiple table support */
276
277         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Ssdt1Code);
278         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Ssdt2Code);
279
280         /* Install the OEM1 table to test LoadTable */
281
282         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Oem1Code);
283
284         /* Install the OEMx table to test LoadTable */
285
286         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&OemxCode);
287
288          /* Install the ECDT table to test _REG */
289
290         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&EcdtCode);
291
292         /* Install two UEFIs to test multiple table support */
293
294         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Uefi1Code);
295         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Uefi2Code);
296     }
297
298     /* Build an RSDP. Contains a valid XSDT only, no RSDT */
299
300     ACPI_MEMSET (&LocalRSDP, 0, sizeof (ACPI_TABLE_RSDP));
301     ACPI_MAKE_RSDP_SIG (LocalRSDP.Signature);
302     ACPI_MEMCPY (LocalRSDP.OemId, "Intel", 6);
303
304     LocalRSDP.Revision = 2;
305     LocalRSDP.XsdtPhysicalAddress = ACPI_PTR_TO_PHYSADDR (LocalXSDT);
306     LocalRSDP.Length = sizeof (ACPI_TABLE_RSDP);
307
308     /* Set checksums for both XSDT and RSDP */
309
310     LocalXSDT->Header.Checksum = 0;
311     LocalXSDT->Header.Checksum = (UINT8) -AcpiTbChecksum (
312         (void *) LocalXSDT, LocalXSDT->Header.Length);
313
314     LocalRSDP.Checksum = 0;
315     LocalRSDP.Checksum = (UINT8) -AcpiTbChecksum (
316         (void *) &LocalRSDP, ACPI_RSDP_CHECKSUM_LENGTH);
317
318     if (!DsdtAddress)
319     {
320         /* Use the local DSDT because incoming table(s) are all SSDT(s) */
321
322         DsdtAddress = ACPI_PTR_TO_PHYSADDR (LocalDsdtCode);
323         DsdtToInstallOverride = ACPI_CAST_PTR (ACPI_TABLE_HEADER, LocalDsdtCode);
324     }
325
326     /*
327      * Build an FADT. There are three options for the FADT:
328      * 1) Incoming external FADT specified on the command line
329      * 2) A "hardware reduced" local FADT
330      * 3) A fully featured local FADT
331      */
332     if (ExternalFadt)
333     {
334         /*
335          * Use the external FADT, but we must update the DSDT/FACS addresses
336          * as well as the checksum
337          */
338         ExternalFadt->Dsdt = DsdtAddress;
339         if (!AcpiGbl_ReducedHardware)
340         {
341             ExternalFadt->Facs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
342         }
343
344         /* Is there room in the FADT for the XDsdst and XFacs 64-bit pointers? */
345
346         if (ExternalFadt->Header.Length > ACPI_PTR_DIFF (&ExternalFadt->XDsdt, ExternalFadt))
347         {
348             ExternalFadt->XDsdt = DsdtAddress;
349
350             if (!AcpiGbl_ReducedHardware)
351             {
352                 ExternalFadt->XFacs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
353             }
354         }
355
356         /* Complete the FADT with the checksum */
357
358         ExternalFadt->Header.Checksum = 0;
359         ExternalFadt->Header.Checksum = (UINT8) -AcpiTbChecksum (
360             (void *) ExternalFadt, ExternalFadt->Header.Length);
361     }
362     else if (AcpiGbl_UseHwReducedFadt)
363     {
364         ACPI_MEMCPY (&LocalFADT, HwReducedFadtCode, sizeof (ACPI_TABLE_FADT));
365         LocalFADT.Dsdt = DsdtAddress;
366         LocalFADT.XDsdt = DsdtAddress;
367
368         LocalFADT.Header.Checksum = 0;
369         LocalFADT.Header.Checksum = (UINT8) -AcpiTbChecksum (
370             (void *) &LocalFADT, LocalFADT.Header.Length);
371     }
372     else
373     {
374         /*
375          * Build a local FADT so we can test the hardware/event init
376          */
377         ACPI_MEMSET (&LocalFADT, 0, sizeof (ACPI_TABLE_FADT));
378         LocalFADT.Header.Revision = 5;
379         AeInitializeTableHeader ((void *) &LocalFADT, ACPI_SIG_FADT, sizeof (ACPI_TABLE_FADT));
380
381         /* Setup FADT header and DSDT/FACS addresses */
382
383         LocalFADT.Dsdt = 0;
384         LocalFADT.Facs = 0;
385
386         LocalFADT.XDsdt = DsdtAddress;
387         LocalFADT.XFacs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
388
389         /* Miscellaneous FADT fields */
390
391         LocalFADT.Gpe0BlockLength = 0x08;
392         LocalFADT.Gpe0Block = 0x00001234;
393
394         LocalFADT.Gpe1BlockLength = 0x80;
395         LocalFADT.Gpe1Block = 0x00005678;
396         LocalFADT.Gpe1Base = 100;
397
398         LocalFADT.Pm1EventLength = 4;
399         LocalFADT.Pm1aEventBlock = 0x00001aaa;
400         LocalFADT.Pm1bEventBlock = 0x00001bbb;
401
402         LocalFADT.Pm1ControlLength = 2;
403         LocalFADT.Pm1aControlBlock = 0xB0;
404
405         LocalFADT.PmTimerLength = 4;
406         LocalFADT.PmTimerBlock = 0xA0;
407
408         LocalFADT.Pm2ControlBlock = 0xC0;
409         LocalFADT.Pm2ControlLength = 1;
410
411         /* Setup one example X-64 GAS field */
412
413         LocalFADT.XPm1bEventBlock.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
414         LocalFADT.XPm1bEventBlock.Address = LocalFADT.Pm1bEventBlock;
415         LocalFADT.XPm1bEventBlock.BitWidth = (UINT8) ACPI_MUL_8 (LocalFADT.Pm1EventLength);
416
417         /* Complete the FADT with the checksum */
418
419         LocalFADT.Header.Checksum = 0;
420         LocalFADT.Header.Checksum = (UINT8) -AcpiTbChecksum (
421             (void *) &LocalFADT, LocalFADT.Header.Length);
422     }
423
424     /* Build a FACS */
425
426     ACPI_MEMSET (&LocalFACS, 0, sizeof (ACPI_TABLE_FACS));
427     ACPI_MOVE_NAME (LocalFACS.Signature, ACPI_SIG_FACS);
428
429     LocalFACS.Length = sizeof (ACPI_TABLE_FACS);
430     LocalFACS.GlobalLock = 0x11AA0011;
431
432     /* Build the optional local tables */
433
434     if (AcpiGbl_LoadTestTables)
435     {
436         /*
437          * Build a fake table [TEST] so that we make sure that the
438          * ACPICA core ignores it
439          */
440         ACPI_MEMSET (&LocalTEST, 0, sizeof (ACPI_TABLE_HEADER));
441         ACPI_MOVE_NAME (LocalTEST.Signature, "TEST");
442
443         LocalTEST.Revision = 1;
444         LocalTEST.Length = sizeof (ACPI_TABLE_HEADER);
445         LocalTEST.Checksum = (UINT8) -AcpiTbChecksum (
446             (void *) &LocalTEST, LocalTEST.Length);
447
448         /*
449          * Build a fake table with a bad signature [BAD!] so that we make
450          * sure that the ACPICA core ignores it
451          */
452         ACPI_MEMSET (&LocalBADTABLE, 0, sizeof (ACPI_TABLE_HEADER));
453         ACPI_MOVE_NAME (LocalBADTABLE.Signature, "BAD!");
454
455         LocalBADTABLE.Revision = 1;
456         LocalBADTABLE.Length = sizeof (ACPI_TABLE_HEADER);
457         LocalBADTABLE.Checksum = (UINT8) -AcpiTbChecksum (
458             (void *) &LocalBADTABLE, LocalBADTABLE.Length);
459     }
460
461     return (AE_OK);
462 }
463
464
465 /******************************************************************************
466  *
467  * FUNCTION:    AeInstallTables
468  *
469  * PARAMETERS:  None
470  *
471  * RETURN:      Status
472  *
473  * DESCRIPTION: Install the various ACPI tables
474  *
475  *****************************************************************************/
476
477 ACPI_STATUS
478 AeInstallTables (
479     void)
480 {
481     ACPI_STATUS             Status;
482     ACPI_TABLE_HEADER       Header;
483     ACPI_TABLE_HEADER       *Table;
484
485
486     Status = AcpiInitializeTables (Tables, ACPI_MAX_INIT_TABLES, TRUE);
487     AE_CHECK_OK (AcpiInitializeTables, Status);
488
489     Status = AcpiReallocateRootTable ();
490     AE_CHECK_OK (AcpiReallocateRootTable, Status);
491
492     Status = AcpiLoadTables ();
493     AE_CHECK_OK (AcpiLoadTables, Status);
494
495     /*
496      * Test run-time control method installation. Do it twice to test code
497      * for an existing name.
498      */
499     Status = AcpiInstallMethod (MethodCode);
500     if (ACPI_FAILURE (Status))
501     {
502         AcpiOsPrintf ("%s, Could not install method\n",
503             AcpiFormatException (Status));
504     }
505
506     Status = AcpiInstallMethod (MethodCode);
507     if (ACPI_FAILURE (Status))
508     {
509         AcpiOsPrintf ("%s, Could not install method\n",
510             AcpiFormatException (Status));
511     }
512
513     if (AcpiGbl_LoadTestTables)
514     {
515         /* Test multiple table/UEFI support. First, get the headers */
516
517         Status = AcpiGetTableHeader (ACPI_SIG_UEFI, 1, &Header);
518         AE_CHECK_OK (AcpiGetTableHeader, Status);
519
520         Status = AcpiGetTableHeader (ACPI_SIG_UEFI, 2, &Header);
521         AE_CHECK_OK (AcpiGetTableHeader, Status);
522
523         Status = AcpiGetTableHeader (ACPI_SIG_UEFI, 3, &Header);
524         AE_CHECK_STATUS (AcpiGetTableHeader, Status, AE_NOT_FOUND);
525
526         /* Now get the actual tables */
527
528         Status = AcpiGetTable (ACPI_SIG_UEFI, 1, &Table);
529         AE_CHECK_OK (AcpiGetTable, Status);
530
531         Status = AcpiGetTable (ACPI_SIG_UEFI, 2, &Table);
532         AE_CHECK_OK (AcpiGetTable, Status);
533
534         Status = AcpiGetTable (ACPI_SIG_UEFI, 3, &Table);
535         AE_CHECK_STATUS (AcpiGetTable, Status, AE_NOT_FOUND);
536     }
537
538     return (AE_OK);
539 }
540
541
542 /******************************************************************************
543  *
544  * FUNCTION:    AcpiOsGetRootPointer
545  *
546  * PARAMETERS:  Flags       - not used
547  *              Address     - Where the root pointer is returned
548  *
549  * RETURN:      Status
550  *
551  * DESCRIPTION: Return a local RSDP, used to dynamically load tables via the
552  *              standard ACPI mechanism.
553  *
554  *****************************************************************************/
555
556 ACPI_PHYSICAL_ADDRESS
557 AcpiOsGetRootPointer (
558     void)
559 {
560
561     return ((ACPI_PHYSICAL_ADDRESS) &LocalRSDP);
562 }