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