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