Sync ACPICA with Intel's version 20141107.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / tables / tbutils.c
1 /******************************************************************************
2  *
3  * Module Name: tbutils - ACPI Table utilities
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 "acpi.h"
45 #include "accommon.h"
46 #include "actables.h"
47
48 #define _COMPONENT          ACPI_TABLES
49         ACPI_MODULE_NAME    ("tbutils")
50
51
52 /* Local prototypes */
53
54 static ACPI_PHYSICAL_ADDRESS
55 AcpiTbGetRootTableEntry (
56     UINT8                   *TableEntry,
57     UINT32                  TableEntrySize);
58
59
60 #if (!ACPI_REDUCED_HARDWARE)
61 /*******************************************************************************
62  *
63  * FUNCTION:    AcpiTbInitializeFacs
64  *
65  * PARAMETERS:  None
66  *
67  * RETURN:      Status
68  *
69  * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
70  *              for accessing the Global Lock and Firmware Waking Vector
71  *
72  ******************************************************************************/
73
74 ACPI_STATUS
75 AcpiTbInitializeFacs (
76     void)
77 {
78     ACPI_STATUS             Status;
79
80
81     /* If Hardware Reduced flag is set, there is no FACS */
82
83     if (AcpiGbl_ReducedHardware)
84     {
85         AcpiGbl_FACS = NULL;
86         return (AE_OK);
87     }
88
89     Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
90                 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &AcpiGbl_FACS));
91     return (Status);
92 }
93 #endif /* !ACPI_REDUCED_HARDWARE */
94
95
96 /*******************************************************************************
97  *
98  * FUNCTION:    AcpiTbTablesLoaded
99  *
100  * PARAMETERS:  None
101  *
102  * RETURN:      TRUE if required ACPI tables are loaded
103  *
104  * DESCRIPTION: Determine if the minimum required ACPI tables are present
105  *              (FADT, FACS, DSDT)
106  *
107  ******************************************************************************/
108
109 BOOLEAN
110 AcpiTbTablesLoaded (
111     void)
112 {
113
114     if (AcpiGbl_RootTableList.CurrentTableCount >= 3)
115     {
116         return (TRUE);
117     }
118
119     return (FALSE);
120 }
121
122
123 /*******************************************************************************
124  *
125  * FUNCTION:    AcpiTbCheckDsdtHeader
126  *
127  * PARAMETERS:  None
128  *
129  * RETURN:      None
130  *
131  * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
132  *              if the DSDT has been replaced from outside the OS and/or if
133  *              the DSDT header has been corrupted.
134  *
135  ******************************************************************************/
136
137 void
138 AcpiTbCheckDsdtHeader (
139     void)
140 {
141
142     /* Compare original length and checksum to current values */
143
144     if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length ||
145         AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum)
146     {
147         ACPI_BIOS_ERROR ((AE_INFO,
148             "The DSDT has been corrupted or replaced - "
149             "old, new headers below"));
150         AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader);
151         AcpiTbPrintTableHeader (0, AcpiGbl_DSDT);
152
153         /* Disable further error messages */
154
155         AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length;
156         AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum;
157     }
158 }
159
160
161 /*******************************************************************************
162  *
163  * FUNCTION:    AcpiTbCopyDsdt
164  *
165  * PARAMETERS:  TableDesc           - Installed table to copy
166  *
167  * RETURN:      None
168  *
169  * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
170  *              Some very bad BIOSs are known to either corrupt the DSDT or
171  *              install a new, bad DSDT. This copy works around the problem.
172  *
173  ******************************************************************************/
174
175 ACPI_TABLE_HEADER *
176 AcpiTbCopyDsdt (
177     UINT32                  TableIndex)
178 {
179     ACPI_TABLE_HEADER       *NewTable;
180     ACPI_TABLE_DESC         *TableDesc;
181
182
183     TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];
184
185     NewTable = ACPI_ALLOCATE (TableDesc->Length);
186     if (!NewTable)
187     {
188         ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X",
189             TableDesc->Length));
190         return (NULL);
191     }
192
193     ACPI_MEMCPY (NewTable, TableDesc->Pointer, TableDesc->Length);
194     AcpiTbUninstallTable (TableDesc);
195
196     AcpiTbInitTableDescriptor (
197         &AcpiGbl_RootTableList.Tables[ACPI_TABLE_INDEX_DSDT],
198         ACPI_PTR_TO_PHYSADDR (NewTable), ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
199         NewTable);
200
201     ACPI_INFO ((AE_INFO,
202         "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
203         NewTable->Length));
204
205     return (NewTable);
206 }
207
208
209 /*******************************************************************************
210  *
211  * FUNCTION:    AcpiTbGetRootTableEntry
212  *
213  * PARAMETERS:  TableEntry          - Pointer to the RSDT/XSDT table entry
214  *              TableEntrySize      - sizeof 32 or 64 (RSDT or XSDT)
215  *
216  * RETURN:      Physical address extracted from the root table
217  *
218  * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
219  *              both 32-bit and 64-bit platforms
220  *
221  * NOTE:        ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on
222  *              64-bit platforms.
223  *
224  ******************************************************************************/
225
226 static ACPI_PHYSICAL_ADDRESS
227 AcpiTbGetRootTableEntry (
228     UINT8                   *TableEntry,
229     UINT32                  TableEntrySize)
230 {
231     UINT64                  Address64;
232
233
234     /*
235      * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
236      * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
237      */
238     if (TableEntrySize == ACPI_RSDT_ENTRY_SIZE)
239     {
240         /*
241          * 32-bit platform, RSDT: Return 32-bit table entry
242          * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
243          */
244         return ((ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST_PTR (UINT32, TableEntry)));
245     }
246     else
247     {
248         /*
249          * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
250          * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
251          *  return 64-bit
252          */
253         ACPI_MOVE_64_TO_64 (&Address64, TableEntry);
254
255 #if ACPI_MACHINE_WIDTH == 32
256         if (Address64 > ACPI_UINT32_MAX)
257         {
258             /* Will truncate 64-bit address to 32 bits, issue warning */
259
260             ACPI_BIOS_WARNING ((AE_INFO,
261                 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
262                 " truncating",
263                 ACPI_FORMAT_UINT64 (Address64)));
264         }
265 #endif
266         return ((ACPI_PHYSICAL_ADDRESS) (Address64));
267     }
268 }
269
270
271 /*******************************************************************************
272  *
273  * FUNCTION:    AcpiTbParseRootTable
274  *
275  * PARAMETERS:  Rsdp                    - Pointer to the RSDP
276  *
277  * RETURN:      Status
278  *
279  * DESCRIPTION: This function is called to parse the Root System Description
280  *              Table (RSDT or XSDT)
281  *
282  * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
283  *              be mapped and cannot be copied because it contains the actual
284  *              memory location of the ACPI Global Lock.
285  *
286  ******************************************************************************/
287
288 ACPI_STATUS
289 AcpiTbParseRootTable (
290     ACPI_PHYSICAL_ADDRESS   RsdpAddress)
291 {
292     ACPI_TABLE_RSDP         *Rsdp;
293     UINT32                  TableEntrySize;
294     UINT32                  i;
295     UINT32                  TableCount;
296     ACPI_TABLE_HEADER       *Table;
297     ACPI_PHYSICAL_ADDRESS   Address;
298     UINT32                  Length;
299     UINT8                   *TableEntry;
300     ACPI_STATUS             Status;
301     UINT32                  TableIndex;
302
303
304     ACPI_FUNCTION_TRACE (TbParseRootTable);
305
306
307     /* Map the entire RSDP and extract the address of the RSDT or XSDT */
308
309     Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP));
310     if (!Rsdp)
311     {
312         return_ACPI_STATUS (AE_NO_MEMORY);
313     }
314
315     AcpiTbPrintTableHeader (RsdpAddress,
316         ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp));
317
318     /* Use XSDT if present and not overridden. Otherwise, use RSDT */
319
320     if ((Rsdp->Revision > 1) &&
321         Rsdp->XsdtPhysicalAddress &&
322         !AcpiGbl_DoNotUseXsdt)
323     {
324         /*
325          * RSDP contains an XSDT (64-bit physical addresses). We must use
326          * the XSDT if the revision is > 1 and the XSDT pointer is present,
327          * as per the ACPI specification.
328          */
329         Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress;
330         TableEntrySize = ACPI_XSDT_ENTRY_SIZE;
331     }
332     else
333     {
334         /* Root table is an RSDT (32-bit physical addresses) */
335
336         Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;
337         TableEntrySize = ACPI_RSDT_ENTRY_SIZE;
338     }
339
340     /*
341      * It is not possible to map more than one entry in some environments,
342      * so unmap the RSDP here before mapping other tables
343      */
344     AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP));
345
346     /* Map the RSDT/XSDT table header to get the full table length */
347
348     Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
349     if (!Table)
350     {
351         return_ACPI_STATUS (AE_NO_MEMORY);
352     }
353
354     AcpiTbPrintTableHeader (Address, Table);
355
356     /*
357      * Validate length of the table, and map entire table.
358      * Minimum length table must contain at least one entry.
359      */
360     Length = Table->Length;
361     AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
362
363     if (Length < (sizeof (ACPI_TABLE_HEADER) + TableEntrySize))
364     {
365         ACPI_BIOS_ERROR ((AE_INFO,
366             "Invalid table length 0x%X in RSDT/XSDT", Length));
367         return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
368     }
369
370     Table = AcpiOsMapMemory (Address, Length);
371     if (!Table)
372     {
373         return_ACPI_STATUS (AE_NO_MEMORY);
374     }
375
376     /* Validate the root table checksum */
377
378     Status = AcpiTbVerifyChecksum (Table, Length);
379     if (ACPI_FAILURE (Status))
380     {
381         AcpiOsUnmapMemory (Table, Length);
382         return_ACPI_STATUS (Status);
383     }
384
385     /* Get the number of entries and pointer to first entry */
386
387     TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /
388         TableEntrySize);
389     TableEntry = ACPI_ADD_PTR (UINT8, Table, sizeof (ACPI_TABLE_HEADER));
390
391     /*
392      * First two entries in the table array are reserved for the DSDT
393      * and FACS, which are not actually present in the RSDT/XSDT - they
394      * come from the FADT
395      */
396     AcpiGbl_RootTableList.CurrentTableCount = 2;
397
398     /* Initialize the root table array from the RSDT/XSDT */
399
400     for (i = 0; i < TableCount; i++)
401     {
402         /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
403
404         Address = AcpiTbGetRootTableEntry (TableEntry, TableEntrySize);
405
406         /* Skip NULL entries in RSDT/XSDT */
407
408         if (!Address)
409         {
410             goto NextTable;
411         }
412
413         Status = AcpiTbInstallStandardTable (Address,
414             ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, FALSE, TRUE, &TableIndex);
415
416         if (ACPI_SUCCESS (Status) &&
417             ACPI_COMPARE_NAME (&AcpiGbl_RootTableList.Tables[TableIndex].Signature,
418                 ACPI_SIG_FADT))
419         {
420             AcpiTbParseFadt (TableIndex);
421         }
422
423 NextTable:
424
425         TableEntry += TableEntrySize;
426     }
427
428     AcpiOsUnmapMemory (Table, Length);
429
430     return_ACPI_STATUS (AE_OK);
431 }