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