kernel: Sync ACPICA with Intel's version 20140627.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpinames / antables.c
1 /******************************************************************************
2  *
3  * Module Name: antables - ACPI table setup/install for AcpiNames utility
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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 "acpinames.h"
45
46 #define _COMPONENT          ACPI_TOOLS
47         ACPI_MODULE_NAME    ("antables")
48
49 /* Local prototypes */
50
51 /* Non-AML tables that are constructed locally and installed */
52
53 static ACPI_TABLE_RSDP          LocalRSDP;
54 static ACPI_TABLE_FACS          LocalFACS;
55
56 /*
57  * We need a local FADT so that the hardware subcomponent will function,
58  * even though the underlying OSD HW access functions don't do anything.
59  */
60 static ACPI_TABLE_FADT          LocalFADT;
61
62 /*
63  * Use XSDT so that both 32- and 64-bit versions of this utility will
64  * function automatically.
65  */
66 static ACPI_TABLE_XSDT          *LocalXSDT;
67
68 #define BASE_XSDT_TABLES        1
69 #define BASE_XSDT_SIZE          (sizeof (ACPI_TABLE_XSDT) + \
70                                     ((BASE_XSDT_TABLES -1) * sizeof (UINT64)))
71
72 ACPI_TABLE_DESC                 Tables[ACPI_MAX_INIT_TABLES];
73
74
75 /******************************************************************************
76  *
77  * FUNCTION:    AeBuildLocalTables
78  *
79  * PARAMETERS:  TableCount      - Number of tables on the command line
80  *              TableList       - List of actual tables from files
81  *
82  * RETURN:      Status
83  *
84  * DESCRIPTION: Build a complete ACPI table chain, with a local RSDP, XSDT,
85  *              FADT, FACS, and the input DSDT/SSDT.
86  *
87  *****************************************************************************/
88
89 ACPI_STATUS
90 AeBuildLocalTables (
91     UINT32                  TableCount,
92     AE_TABLE_DESC           *TableList)
93 {
94     ACPI_PHYSICAL_ADDRESS   DsdtAddress = 0;
95     UINT32                  XsdtSize;
96     AE_TABLE_DESC           *NextTable;
97     UINT32                  NextIndex;
98     ACPI_TABLE_FADT         *ExternalFadt = NULL;
99
100
101     /*
102      * Update the table count. For DSDT, it is not put into the XSDT. For
103      * FADT, this is already accounted for since we usually install a
104      * local FADT.
105      */
106     NextTable = TableList;
107     while (NextTable)
108     {
109         if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_DSDT) ||
110             ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_FADT))
111         {
112             TableCount--;
113         }
114         NextTable = NextTable->Next;
115     }
116
117     XsdtSize = BASE_XSDT_SIZE + (TableCount * sizeof (UINT64));
118
119     /* Build an XSDT */
120
121     LocalXSDT = AcpiOsAllocate (XsdtSize);
122     if (!LocalXSDT)
123     {
124         return (AE_NO_MEMORY);
125     }
126
127     ACPI_MEMSET (LocalXSDT, 0, XsdtSize);
128     ACPI_MOVE_NAME (LocalXSDT->Header.Signature, ACPI_SIG_XSDT);
129     LocalXSDT->Header.Length = XsdtSize;
130     LocalXSDT->Header.Revision = 1;
131
132     LocalXSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (&LocalFADT);
133
134     /*
135      * Install the user tables. The DSDT must be installed in the FADT.
136      * All other tables are installed directly into the XSDT.
137      */
138     NextIndex = BASE_XSDT_TABLES;
139     NextTable = TableList;
140     while (NextTable)
141     {
142         /*
143          * Incoming DSDT or FADT are special cases. All other tables are
144          * just immediately installed into the XSDT.
145          */
146         if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_DSDT))
147         {
148             if (DsdtAddress)
149             {
150                 printf ("Already found a DSDT, only one allowed\n");
151                 return (AE_ALREADY_EXISTS);
152             }
153
154             /* The incoming user table is a DSDT */
155
156             DsdtAddress = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
157         }
158         else if (ACPI_COMPARE_NAME (NextTable->Table->Signature, ACPI_SIG_FADT))
159         {
160             ExternalFadt = ACPI_CAST_PTR (ACPI_TABLE_FADT, NextTable->Table);
161             LocalXSDT->TableOffsetEntry[2] = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
162         }
163         else
164         {
165             /* Install the table in the XSDT */
166
167             LocalXSDT->TableOffsetEntry[NextIndex] = ACPI_PTR_TO_PHYSADDR (NextTable->Table);
168             NextIndex++;
169         }
170
171         NextTable = NextTable->Next;
172     }
173
174     /* Build an RSDP */
175
176     ACPI_MEMSET (&LocalRSDP, 0, sizeof (ACPI_TABLE_RSDP));
177     ACPI_MAKE_RSDP_SIG (LocalRSDP.Signature);
178     ACPI_MEMCPY (LocalRSDP.OemId, "I_TEST", 6);
179     LocalRSDP.Revision = 2;
180     LocalRSDP.XsdtPhysicalAddress = ACPI_PTR_TO_PHYSADDR (LocalXSDT);
181     LocalRSDP.Length = sizeof (ACPI_TABLE_XSDT);
182
183     /* Set checksums for both XSDT and RSDP */
184
185     LocalXSDT->Header.Checksum = (UINT8) -AcpiTbChecksum (
186         (void *) LocalXSDT, LocalXSDT->Header.Length);
187     LocalRSDP.Checksum = (UINT8) -AcpiTbChecksum (
188         (void *) &LocalRSDP, ACPI_RSDP_CHECKSUM_LENGTH);
189
190     if (!DsdtAddress)
191     {
192         return (AE_SUPPORT);
193     }
194
195     if (ExternalFadt)
196     {
197         /*
198          * Use the external FADT, but we must update the DSDT/FACS addresses
199          * as well as the checksum
200          */
201         ExternalFadt->Dsdt = DsdtAddress;
202         ExternalFadt->Facs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
203
204         if (ExternalFadt->Header.Length > ACPI_PTR_DIFF (&ExternalFadt->XDsdt, ExternalFadt))
205         {
206             ExternalFadt->XDsdt = DsdtAddress;
207             ExternalFadt->XFacs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
208         }
209         /* Complete the FADT with the checksum */
210
211         ExternalFadt->Header.Checksum = 0;
212         ExternalFadt->Header.Checksum = (UINT8) -AcpiTbChecksum (
213             (void *) ExternalFadt, ExternalFadt->Header.Length);
214     }
215     else
216     {
217         /*
218          * Build a local FADT so we can test the hardware/event init
219          */
220         ACPI_MEMSET (&LocalFADT, 0, sizeof (ACPI_TABLE_FADT));
221         ACPI_MOVE_NAME (LocalFADT.Header.Signature, ACPI_SIG_FADT);
222
223         /* Setup FADT header and DSDT/FACS addresses */
224
225         LocalFADT.Dsdt = 0;
226         LocalFADT.Facs = 0;
227
228         LocalFADT.XDsdt = DsdtAddress;
229         LocalFADT.XFacs = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
230
231         LocalFADT.Header.Revision = 3;
232         LocalFADT.Header.Length = sizeof (ACPI_TABLE_FADT);
233
234         /* Miscellaneous FADT fields */
235
236         LocalFADT.Gpe0BlockLength = 16;
237         LocalFADT.Gpe0Block = 0x00001234;
238
239         LocalFADT.Gpe1BlockLength = 6;
240         LocalFADT.Gpe1Block = 0x00005678;
241         LocalFADT.Gpe1Base = 96;
242
243         LocalFADT.Pm1EventLength = 4;
244         LocalFADT.Pm1aEventBlock = 0x00001aaa;
245         LocalFADT.Pm1bEventBlock = 0x00001bbb;
246
247         LocalFADT.Pm1ControlLength = 2;
248         LocalFADT.Pm1aControlBlock = 0xB0;
249
250         LocalFADT.PmTimerLength = 4;
251         LocalFADT.PmTimerBlock = 0xA0;
252
253         LocalFADT.Pm2ControlBlock = 0xC0;
254         LocalFADT.Pm2ControlLength = 1;
255
256         /* Setup one example X-64 field */
257
258         LocalFADT.XPm1bEventBlock.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
259         LocalFADT.XPm1bEventBlock.Address = LocalFADT.Pm1bEventBlock;
260         LocalFADT.XPm1bEventBlock.BitWidth = (UINT8) ACPI_MUL_8 (LocalFADT.Pm1EventLength);
261
262         /* Complete the FADT with the checksum */
263
264         LocalFADT.Header.Checksum = 0;
265         LocalFADT.Header.Checksum = (UINT8) -AcpiTbChecksum (
266             (void *) &LocalFADT, LocalFADT.Header.Length);
267     }
268
269     /* Build a FACS */
270
271     ACPI_MEMSET (&LocalFACS, 0, sizeof (ACPI_TABLE_FACS));
272     ACPI_MOVE_NAME (LocalFACS.Signature, ACPI_SIG_FACS);
273
274     LocalFACS.Length = sizeof (ACPI_TABLE_FACS);
275     LocalFACS.GlobalLock = 0x11AA0011;
276
277     return (AE_OK);
278 }
279
280
281 /******************************************************************************
282  *
283  * FUNCTION:    AcpiOsGetRootPointer
284  *
285  * PARAMETERS:  None
286  *
287  * RETURN:      Address of the RSDP
288  *
289  * DESCRIPTION: Return a local RSDP, used to dynamically load tables via the
290  *              standard ACPI mechanism.
291  *
292  *****************************************************************************/
293
294 ACPI_PHYSICAL_ADDRESS
295 AcpiOsGetRootPointer (
296     void)
297 {
298
299     return ((ACPI_PHYSICAL_ADDRESS) &LocalRSDP);
300 }