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