Merge branch 'vendor/GCC50'
[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
91
92 /******************************************************************************
93  *
94  * FUNCTION:    AeTableOverride
95  *
96  * DESCRIPTION: Local implementation of AcpiOsTableOverride.
97  *              Exercise the override mechanism
98  *
99  *****************************************************************************/
100
101 void
102 AeTableOverride (
103     ACPI_TABLE_HEADER       *ExistingTable,
104     ACPI_TABLE_HEADER       **NewTable)
105 {
106
107     if (!AcpiGbl_LoadTestTables)
108     {
109         *NewTable = NULL;
110         return;
111     }
112
113     /* This code exercises the table override mechanism in the core */
114
115     if (ACPI_COMPARE_NAME (ExistingTable->Signature, ACPI_SIG_DSDT))
116     {
117         *NewTable = DsdtToInstallOverride;
118     }
119
120     /* This code tests override of dynamically loaded tables */
121
122     else if (ACPI_COMPARE_NAME (ExistingTable->Signature, "OEM9"))
123     {
124         *NewTable = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Ssdt3Code);
125     }
126 }
127
128
129 /******************************************************************************
130  *
131  * FUNCTION:    AeInitializeTableHeader
132  *
133  * PARAMETERS:  Header          - A valid standard ACPI table header
134  *              Signature       - Signature to insert
135  *              Length          - Length of the table
136  *
137  * RETURN:      None. Header is modified.
138  *
139  * DESCRIPTION: Initialize the table header for a local ACPI table.
140  *
141  *****************************************************************************/
142
143 static void
144 AeInitializeTableHeader (
145     ACPI_TABLE_HEADER       *Header,
146     char                    *Signature,
147     UINT32                  Length)
148 {
149
150     ACPI_MOVE_NAME (Header->Signature, Signature);
151     Header->Length = Length;
152
153     Header->OemRevision = 0x1001;
154     strncpy (Header->OemId, "Intel", ACPI_OEM_ID_SIZE);
155     strncpy (Header->OemTableId, "AcpiExec", ACPI_OEM_TABLE_ID_SIZE);
156     strncpy (Header->AslCompilerId, "INTL", ACPI_NAME_SIZE);
157     Header->AslCompilerRevision = ACPI_CA_VERSION;
158
159     /* Set the checksum, must set to zero first */
160
161     Header->Checksum = 0;
162     Header->Checksum = (UINT8) -AcpiTbChecksum (
163         (void *) Header, Header->Length);
164 }
165
166
167 /******************************************************************************
168  *
169  * FUNCTION:    AeBuildLocalTables
170  *
171  * PARAMETERS:  TableCount      - Number of tables on the command line
172  *              ListHead        - List of actual tables from files
173  *
174  * RETURN:      Status
175  *
176  * DESCRIPTION: Build a complete ACPI table chain, with a local RSDP, XSDT,
177  *              FADT, and several other test tables.
178  *
179  *****************************************************************************/
180
181 ACPI_STATUS
182 AeBuildLocalTables (
183     ACPI_NEW_TABLE_DESC     *ListHead)
184 {
185     UINT32                  TableCount = 1;
186     ACPI_PHYSICAL_ADDRESS   DsdtAddress = 0;
187     UINT32                  XsdtSize;
188     ACPI_NEW_TABLE_DESC     *NextTable;
189     UINT32                  NextIndex;
190     ACPI_TABLE_FADT         *ExternalFadt = NULL;
191
192
193     /*
194      * Update the table count. For the DSDT, it is not put into the XSDT.
195      * For the FADT, this table is already accounted for since we usually
196      * install a local FADT.
197      */
198     NextTable = ListHead;
199     while (NextTable)
200     {
201         if (!ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_DSDT) &&
202             !ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_FADT))
203         {
204             TableCount++;
205         }
206
207         NextTable = NextTable->Next;
208     }
209
210     XsdtSize = (((TableCount + 1) * sizeof (UINT64)) +
211         sizeof (ACPI_TABLE_HEADER));
212     if (AcpiGbl_LoadTestTables)
213     {
214         XsdtSize += BASE_XSDT_SIZE;
215     }
216
217     /* Build an XSDT */
218
219     LocalXSDT = AcpiOsAllocate (XsdtSize);
220     if (!LocalXSDT)
221     {
222         return (AE_NO_MEMORY);
223     }
224
225     memset (LocalXSDT, 0, XsdtSize);
226     LocalXSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (&LocalFADT);
227     NextIndex = 1;
228
229     /*
230      * Install the user tables. The DSDT must be installed in the FADT.
231      * All other tables are installed directly into the XSDT.
232      *
233      * Note: The tables are loaded in reverse order from the incoming
234      * input, which makes it match the command line order.
235      */
236     NextTable = ListHead;
237     while (NextTable)
238     {
239         /*
240          * Incoming DSDT or FADT are special cases. All other tables are
241          * just immediately installed into the XSDT.
242          */
243         if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_DSDT))
244         {
245             if (DsdtAddress)
246             {
247                 printf ("Already found a DSDT, only one allowed\n");
248                 return (AE_ALREADY_EXISTS);
249             }
250
251             /* The incoming user table is a DSDT */
252
253             DsdtAddress = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
254             DsdtToInstallOverride = NextTable->Table;
255         }
256         else if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_FADT))
257         {
258             ExternalFadt = ACPI_CAST_PTR (ACPI_TABLE_FADT, NextTable->Table);
259             LocalXSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
260         }
261         else
262         {
263             /* Install the table in the XSDT */
264
265             LocalXSDT->TableOffsetEntry[TableCount - NextIndex + 1] =
266                 ACPI_PTR_TO_PHYSADDR (NextTable->Table);
267             NextIndex++;
268         }
269
270         NextTable = NextTable->Next;
271     }
272
273     /* Install the optional extra local tables */
274
275     if (AcpiGbl_LoadTestTables)
276     {
277         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);
278         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&LocalBADTABLE);
279
280         /* Install two SSDTs to test multiple table support */
281
282         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Ssdt1Code);
283         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Ssdt2Code);
284
285         /* Install the OEM1 table to test LoadTable */
286
287         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Oem1Code);
288
289         /* Install the OEMx table to test LoadTable */
290
291         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&OemxCode);
292
293          /* Install the ECDT table to test _REG */
294
295         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&EcdtCode);
296
297         /* Install two UEFIs to test multiple table support */
298
299         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Uefi1Code);
300         LocalXSDT->TableOffsetEntry[NextIndex++] = ACPI_PTR_TO_PHYSADDR (&Uefi2Code);
301     }
302
303     /* Build an RSDP. Contains a valid XSDT only, no RSDT */
304
305     memset (&LocalRSDP, 0, sizeof (ACPI_TABLE_RSDP));
306     ACPI_MAKE_RSDP_SIG (LocalRSDP.Signature);
307     memcpy (LocalRSDP.OemId, "Intel", 6);
308
309     LocalRSDP.Revision = 2;
310     LocalRSDP.XsdtPhysicalAddress = ACPI_PTR_TO_PHYSADDR (LocalXSDT);
311     LocalRSDP.Length = sizeof (ACPI_TABLE_RSDP);
312
313     /* Set checksums for both XSDT and RSDP */
314
315     AeInitializeTableHeader ((void *) LocalXSDT, ACPI_SIG_XSDT, XsdtSize);
316
317     LocalRSDP.Checksum = 0;
318     LocalRSDP.Checksum = (UINT8) -AcpiTbChecksum (
319         (void *) &LocalRSDP, ACPI_RSDP_CHECKSUM_LENGTH);
320
321     if (!DsdtAddress)
322     {
323         /* Use the local DSDT because incoming table(s) are all SSDT(s) */
324
325         DsdtAddress = ACPI_PTR_TO_PHYSADDR (LocalDsdtCode);
326         DsdtToInstallOverride = ACPI_CAST_PTR (ACPI_TABLE_HEADER, LocalDsdtCode);
327     }
328
329     /*
330      * Build an FADT. There are three options for the FADT:
331      * 1) Incoming external FADT specified on the command line
332      * 2) A "hardware reduced" local FADT
333      * 3) A fully featured local FADT
334      */
335     memset (&LocalFADT, 0, sizeof (ACPI_TABLE_FADT));
336
337     if (ExternalFadt)
338     {
339         /*
340          * Use the external FADT, but we must update the DSDT/FACS
341          * addresses as well as the checksum
342          */
343         ExternalFadt->Dsdt = (UINT32) DsdtAddress;
344         if (!AcpiGbl_ReducedHardware)
345         {
346             ExternalFadt->Facs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
347         }
348
349         /*
350          * If there room in the FADT for the XDsdt and XFacs 64-bit
351          * pointers, use them.
352          */
353         if (ExternalFadt->Header.Length > ACPI_PTR_DIFF (
354             &ExternalFadt->XDsdt, ExternalFadt))
355         {
356             ExternalFadt->Dsdt = 0;
357             ExternalFadt->Facs = 0;
358
359             ExternalFadt->XDsdt = DsdtAddress;
360             if (!AcpiGbl_ReducedHardware)
361             {
362                 ExternalFadt->XFacs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
363             }
364         }
365
366         /* Complete the external FADT with the checksum */
367
368         ExternalFadt->Header.Checksum = 0;
369         ExternalFadt->Header.Checksum = (UINT8) -AcpiTbChecksum (
370             (void *) ExternalFadt, ExternalFadt->Header.Length);
371     }
372     else if (AcpiGbl_UseHwReducedFadt)
373     {
374         memcpy (&LocalFADT, HwReducedFadtCode, ACPI_FADT_V5_SIZE);
375         LocalFADT.Dsdt = 0;
376         LocalFADT.XDsdt = DsdtAddress;
377     }
378     else
379     {
380         /*
381          * Build a local FADT so we can test the hardware/event init
382          */
383         LocalFADT.Header.Revision = 5;
384
385         /* Setup FADT header and DSDT/FACS addresses */
386
387         LocalFADT.Dsdt = 0;
388         LocalFADT.Facs = 0;
389
390         LocalFADT.XDsdt = DsdtAddress;
391         LocalFADT.XFacs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
392
393         /* Miscellaneous FADT fields */
394
395         LocalFADT.Gpe0BlockLength = 0x08;
396         LocalFADT.Gpe0Block = 0x00001234;
397
398         LocalFADT.Gpe1BlockLength = 0x80;
399         LocalFADT.Gpe1Block = 0x00005678;
400         LocalFADT.Gpe1Base = 100;
401
402         LocalFADT.Pm1EventLength = 4;
403         LocalFADT.Pm1aEventBlock = 0x00001aaa;
404         LocalFADT.Pm1bEventBlock = 0x00001bbb;
405
406         LocalFADT.Pm1ControlLength = 2;
407         LocalFADT.Pm1aControlBlock = 0xB0;
408
409         LocalFADT.PmTimerLength = 4;
410         LocalFADT.PmTimerBlock = 0xA0;
411
412         LocalFADT.Pm2ControlBlock = 0xC0;
413         LocalFADT.Pm2ControlLength = 1;
414
415         /* Setup one example X-64 GAS field */
416
417         LocalFADT.XPm1bEventBlock.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
418         LocalFADT.XPm1bEventBlock.Address = LocalFADT.Pm1bEventBlock;
419         LocalFADT.XPm1bEventBlock.BitWidth = (UINT8)
420             ACPI_MUL_8 (LocalFADT.Pm1EventLength);
421     }
422
423     AeInitializeTableHeader ((void *) &LocalFADT,
424         ACPI_SIG_FADT, sizeof (ACPI_TABLE_FADT));
425
426     /* Build a FACS */
427
428     memset (&LocalFACS, 0, sizeof (ACPI_TABLE_FACS));
429     ACPI_MOVE_NAME (LocalFACS.Signature, ACPI_SIG_FACS);
430
431     LocalFACS.Length = sizeof (ACPI_TABLE_FACS);
432     LocalFACS.GlobalLock = 0x11AA0011;
433
434     /* Build the optional local tables */
435
436     if (AcpiGbl_LoadTestTables)
437     {
438         /*
439          * Build a fake table [TEST] so that we make sure that the
440          * ACPICA core ignores it
441          */
442         memset (&LocalTEST, 0, sizeof (ACPI_TABLE_HEADER));
443         ACPI_MOVE_NAME (LocalTEST.Signature, "TEST");
444
445         LocalTEST.Revision = 1;
446         LocalTEST.Length = sizeof (ACPI_TABLE_HEADER);
447
448         LocalTEST.Checksum = 0;
449         LocalTEST.Checksum = (UINT8) -AcpiTbChecksum (
450             (void *) &LocalTEST, LocalTEST.Length);
451
452         /*
453          * Build a fake table with a bad signature [BAD!] so that we make
454          * sure that the ACPICA core ignores it
455          */
456         memset (&LocalBADTABLE, 0, sizeof (ACPI_TABLE_HEADER));
457         ACPI_MOVE_NAME (LocalBADTABLE.Signature, "BAD!");
458
459         LocalBADTABLE.Revision = 1;
460         LocalBADTABLE.Length = sizeof (ACPI_TABLE_HEADER);
461
462         LocalBADTABLE.Checksum = 0;
463         LocalBADTABLE.Checksum = (UINT8) -AcpiTbChecksum (
464             (void *) &LocalBADTABLE, LocalBADTABLE.Length);
465     }
466
467     return (AE_OK);
468 }
469
470
471 /******************************************************************************
472  *
473  * FUNCTION:    AeInstallTables
474  *
475  * PARAMETERS:  None
476  *
477  * RETURN:      Status
478  *
479  * DESCRIPTION: Install the various ACPI tables
480  *
481  *****************************************************************************/
482
483 ACPI_STATUS
484 AeInstallTables (
485     void)
486 {
487     ACPI_STATUS             Status;
488     ACPI_TABLE_HEADER       Header;
489     ACPI_TABLE_HEADER       *Table;
490     UINT32                  i;
491
492
493     Status = AcpiInitializeTables (NULL, ACPI_MAX_INIT_TABLES, TRUE);
494     ACPI_CHECK_OK (AcpiInitializeTables, Status);
495
496     Status = AcpiLoadTables ();
497     ACPI_CHECK_OK (AcpiLoadTables, Status);
498
499     /*
500      * Test run-time control method installation. Do it twice to test code
501      * for an existing name.
502      */
503     Status = AcpiInstallMethod (MethodCode);
504     if (ACPI_FAILURE (Status))
505     {
506         AcpiOsPrintf ("%s, Could not install method\n",
507             AcpiFormatException (Status));
508     }
509
510     Status = AcpiInstallMethod (MethodCode);
511     if (ACPI_FAILURE (Status))
512     {
513         AcpiOsPrintf ("%s, Could not install method\n",
514             AcpiFormatException (Status));
515     }
516
517     if (AcpiGbl_LoadTestTables)
518     {
519         /* Test multiple table/UEFI support. First, get the headers */
520
521         Status = AcpiGetTableHeader (ACPI_SIG_UEFI, 1, &Header);
522         ACPI_CHECK_OK (AcpiGetTableHeader, Status);
523
524         Status = AcpiGetTableHeader (ACPI_SIG_UEFI, 2, &Header);
525         ACPI_CHECK_OK (AcpiGetTableHeader, Status);
526
527         Status = AcpiGetTableHeader (ACPI_SIG_UEFI, 3, &Header);
528         ACPI_CHECK_STATUS (AcpiGetTableHeader, Status, AE_NOT_FOUND);
529
530         /* Now get the actual tables */
531
532         Status = AcpiGetTable (ACPI_SIG_UEFI, 1, &Table);
533         ACPI_CHECK_OK (AcpiGetTable, Status);
534
535         Status = AcpiGetTable (ACPI_SIG_UEFI, 2, &Table);
536         ACPI_CHECK_OK (AcpiGetTable, Status);
537
538         Status = AcpiGetTable (ACPI_SIG_UEFI, 3, &Table);
539         ACPI_CHECK_STATUS (AcpiGetTable, Status, AE_NOT_FOUND);
540     }
541
542     /* Check that we can get all of the ACPI tables */
543
544     for (i = 0; ; i++)
545     {
546         Status = AcpiGetTableByIndex (i, &Table);
547         if ((Status == AE_BAD_PARAMETER) || !Table)
548         {
549             break;
550         }
551
552         ACPI_CHECK_OK (AcpiGetTableByIndex, Status);
553     }
554
555     return (AE_OK);
556 }
557
558
559 /******************************************************************************
560  *
561  * FUNCTION:    AcpiOsGetRootPointer
562  *
563  * PARAMETERS:  Flags       - not used
564  *              Address     - Where the root pointer is returned
565  *
566  * RETURN:      Status
567  *
568  * DESCRIPTION: Return a local RSDP, used to dynamically load tables via the
569  *              standard ACPI mechanism.
570  *
571  *****************************************************************************/
572
573 ACPI_PHYSICAL_ADDRESS
574 AcpiOsGetRootPointer (
575     void)
576 {
577
578     return (ACPI_PTR_TO_PHYSADDR (&LocalRSDP));
579 }