1 /******************************************************************************
3 * Module Name: tbutils - table utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/actables.h>
50 #define _COMPONENT ACPI_TABLES
51 ACPI_MODULE_NAME ("tbutils")
53 /* Local prototypes */
61 AcpiTbCleanupTableHeader (
62 ACPI_TABLE_HEADER *OutHeader,
63 ACPI_TABLE_HEADER *Header);
65 static ACPI_PHYSICAL_ADDRESS
66 AcpiTbGetRootTableEntry (
68 UINT32 TableEntrySize);
71 /*******************************************************************************
73 * FUNCTION: AcpiTbInitializeFacs
79 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
80 * for accessing the Global Lock and Firmware Waking Vector
82 ******************************************************************************/
85 AcpiTbInitializeFacs (
91 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
92 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &AcpiGbl_FACS));
97 /*******************************************************************************
99 * FUNCTION: AcpiTbTablesLoaded
103 * RETURN: TRUE if required ACPI tables are loaded
105 * DESCRIPTION: Determine if the minimum required ACPI tables are present
108 ******************************************************************************/
115 if (AcpiGbl_RootTableList.CurrentTableCount >= 3)
124 /*******************************************************************************
126 * FUNCTION: AcpiTbFixString
128 * PARAMETERS: String - String to be repaired
129 * Length - Maximum length
133 * DESCRIPTION: Replace every non-printable or non-ascii byte in the string
134 * with a question mark '?'.
136 ******************************************************************************/
144 while (Length && *String)
146 if (!ACPI_IS_PRINT (*String))
156 /*******************************************************************************
158 * FUNCTION: AcpiTbCleanupTableHeader
160 * PARAMETERS: OutHeader - Where the cleaned header is returned
161 * Header - Input ACPI table header
163 * RETURN: Returns the cleaned header in OutHeader
165 * DESCRIPTION: Copy the table header and ensure that all "string" fields in
166 * the header consist of printable characters.
168 ******************************************************************************/
171 AcpiTbCleanupTableHeader (
172 ACPI_TABLE_HEADER *OutHeader,
173 ACPI_TABLE_HEADER *Header)
176 ACPI_MEMCPY (OutHeader, Header, sizeof (ACPI_TABLE_HEADER));
178 AcpiTbFixString (OutHeader->Signature, ACPI_NAME_SIZE);
179 AcpiTbFixString (OutHeader->OemId, ACPI_OEM_ID_SIZE);
180 AcpiTbFixString (OutHeader->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
181 AcpiTbFixString (OutHeader->AslCompilerId, ACPI_NAME_SIZE);
185 /*******************************************************************************
187 * FUNCTION: AcpiTbPrintTableHeader
189 * PARAMETERS: Address - Table physical address
190 * Header - Table header
194 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
196 ******************************************************************************/
199 AcpiTbPrintTableHeader (
200 ACPI_PHYSICAL_ADDRESS Address,
201 ACPI_TABLE_HEADER *Header)
203 ACPI_TABLE_HEADER LocalHeader;
207 * The reason that the Address is cast to a void pointer is so that we
208 * can use %p which will work properly on both 32-bit and 64-bit hosts.
210 if (ACPI_COMPARE_NAME (Header->Signature, ACPI_SIG_FACS))
212 /* FACS only has signature and length fields */
214 ACPI_INFO ((AE_INFO, "%4.4s %p %05X",
215 Header->Signature, ACPI_CAST_PTR (void, Address),
218 else if (ACPI_COMPARE_NAME (Header->Signature, ACPI_SIG_RSDP))
220 /* RSDP has no common fields */
222 ACPI_MEMCPY (LocalHeader.OemId,
223 ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->OemId, ACPI_OEM_ID_SIZE);
224 AcpiTbFixString (LocalHeader.OemId, ACPI_OEM_ID_SIZE);
226 ACPI_INFO ((AE_INFO, "RSDP %p %05X (v%.2d %6.6s)",
227 ACPI_CAST_PTR (void, Address),
228 (ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->Revision > 0) ?
229 ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->Length : 20,
230 ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->Revision,
235 /* Standard ACPI table with full common header */
237 AcpiTbCleanupTableHeader (&LocalHeader, Header);
240 "%4.4s %p %05X (v%.2d %6.6s %8.8s %08X %4.4s %08X)",
241 LocalHeader.Signature, ACPI_CAST_PTR (void, Address),
242 LocalHeader.Length, LocalHeader.Revision, LocalHeader.OemId,
243 LocalHeader.OemTableId, LocalHeader.OemRevision,
244 LocalHeader.AslCompilerId, LocalHeader.AslCompilerRevision));
249 /*******************************************************************************
251 * FUNCTION: AcpiTbValidateChecksum
253 * PARAMETERS: Table - ACPI table to verify
254 * Length - Length of entire table
258 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
259 * exception on bad checksum.
261 ******************************************************************************/
264 AcpiTbVerifyChecksum (
265 ACPI_TABLE_HEADER *Table,
271 /* Compute the checksum on the table */
273 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Length);
275 /* Checksum ok? (should be zero) */
279 ACPI_WARNING ((AE_INFO,
280 "Incorrect checksum in table [%4.4s] - 0x%2.2X, should be 0x%2.2X",
281 Table->Signature, Table->Checksum,
282 (UINT8) (Table->Checksum - Checksum)));
284 #if (ACPI_CHECKSUM_ABORT)
285 return (AE_BAD_CHECKSUM);
293 /*******************************************************************************
295 * FUNCTION: AcpiTbChecksum
297 * PARAMETERS: Buffer - Pointer to memory region to be checked
298 * Length - Length of this memory region
300 * RETURN: Checksum (UINT8)
302 * DESCRIPTION: Calculates circular checksum of memory region.
304 ******************************************************************************/
312 UINT8 *End = Buffer + Length;
317 Sum = (UINT8) (Sum + *(Buffer++));
324 /*******************************************************************************
326 * FUNCTION: AcpiTbCheckDsdtHeader
332 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
333 * if the DSDT has been replaced from outside the OS and/or if
334 * the DSDT header has been corrupted.
336 ******************************************************************************/
339 AcpiTbCheckDsdtHeader (
343 /* Compare original length and checksum to current values */
345 if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length ||
346 AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum)
348 ACPI_ERROR ((AE_INFO,
349 "The DSDT has been corrupted or replaced - old, new headers below"));
350 AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader);
351 AcpiTbPrintTableHeader (0, AcpiGbl_DSDT);
353 /* Disable further error messages */
355 AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length;
356 AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum;
361 /*******************************************************************************
363 * FUNCTION: AcpiTbCopyDsdt
365 * PARAMETERS: TableDesc - Installed table to copy
369 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
370 * Some very bad BIOSs are known to either corrupt the DSDT or
371 * install a new, bad DSDT. This copy works around the problem.
373 ******************************************************************************/
379 ACPI_TABLE_HEADER *NewTable;
380 ACPI_TABLE_DESC *TableDesc;
383 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];
385 NewTable = ACPI_ALLOCATE (TableDesc->Length);
388 ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X",
393 ACPI_MEMCPY (NewTable, TableDesc->Pointer, TableDesc->Length);
394 AcpiTbDeleteTable (TableDesc);
395 TableDesc->Pointer = NewTable;
396 TableDesc->Flags = ACPI_TABLE_ORIGIN_ALLOCATED;
399 "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
406 /*******************************************************************************
408 * FUNCTION: AcpiTbInstallTable
410 * PARAMETERS: Address - Physical address of DSDT or FACS
411 * Signature - Table signature, NULL if no need to
413 * TableIndex - Index into root table array
417 * DESCRIPTION: Install an ACPI table into the global data structure. The
418 * table override mechanism is implemented here to allow the host
419 * OS to replace any table before it is installed in the root
422 ******************************************************************************/
426 ACPI_PHYSICAL_ADDRESS Address,
432 ACPI_TABLE_HEADER *TableToInstall;
433 ACPI_TABLE_HEADER *MappedTable;
434 ACPI_TABLE_HEADER *OverrideTable = NULL;
439 ACPI_ERROR ((AE_INFO, "Null physical address for ACPI table [%s]",
444 /* Map just the table header */
446 MappedTable = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
452 /* Skip SSDT when DSDT is overriden */
454 if (ACPI_COMPARE_NAME (MappedTable->Signature, ACPI_SIG_SSDT) &&
455 (AcpiGbl_RootTableList.Tables[ACPI_TABLE_INDEX_DSDT].Flags &
456 ACPI_TABLE_ORIGIN_OVERRIDE))
459 "%4.4s @ 0x%p Table override, replaced with:", ACPI_SIG_SSDT,
460 ACPI_CAST_PTR (void, Address)));
461 AcpiTbPrintTableHeader (
462 AcpiGbl_RootTableList.Tables[ACPI_TABLE_INDEX_DSDT].Address,
463 AcpiGbl_RootTableList.Tables[ACPI_TABLE_INDEX_DSDT].Pointer);
467 /* If a particular signature is expected (DSDT/FACS), it must match */
470 !ACPI_COMPARE_NAME (MappedTable->Signature, Signature))
472 ACPI_ERROR ((AE_INFO,
473 "Invalid signature 0x%X for ACPI table, expected [%s]",
474 *ACPI_CAST_PTR (UINT32, MappedTable->Signature), Signature));
479 * ACPI Table Override:
481 * Before we install the table, let the host OS override it with a new
482 * one if desired. Any table within the RSDT/XSDT can be replaced,
483 * including the DSDT which is pointed to by the FADT.
485 Status = AcpiOsTableOverride (MappedTable, &OverrideTable);
486 if (ACPI_SUCCESS (Status) && OverrideTable)
489 "%4.4s @ 0x%p Table override, replaced with:",
490 MappedTable->Signature, ACPI_CAST_PTR (void, Address)));
492 AcpiGbl_RootTableList.Tables[TableIndex].Pointer = OverrideTable;
493 Address = ACPI_PTR_TO_PHYSADDR (OverrideTable);
495 TableToInstall = OverrideTable;
496 Flags = ACPI_TABLE_ORIGIN_OVERRIDE;
500 TableToInstall = MappedTable;
501 Flags = ACPI_TABLE_ORIGIN_MAPPED;
504 /* Initialize the table entry */
506 AcpiGbl_RootTableList.Tables[TableIndex].Address = Address;
507 AcpiGbl_RootTableList.Tables[TableIndex].Length = TableToInstall->Length;
508 AcpiGbl_RootTableList.Tables[TableIndex].Flags = Flags;
511 &(AcpiGbl_RootTableList.Tables[TableIndex].Signature),
512 TableToInstall->Signature);
514 AcpiTbPrintTableHeader (Address, TableToInstall);
516 if (TableIndex == ACPI_TABLE_INDEX_DSDT)
518 /* Global integer width is based upon revision of the DSDT */
520 AcpiUtSetIntegerWidth (TableToInstall->Revision);
524 AcpiOsUnmapMemory (MappedTable, sizeof (ACPI_TABLE_HEADER));
528 /*******************************************************************************
530 * FUNCTION: AcpiTbGetRootTableEntry
532 * PARAMETERS: TableEntry - Pointer to the RSDT/XSDT table entry
533 * TableEntrySize - sizeof 32 or 64 (RSDT or XSDT)
535 * RETURN: Physical address extracted from the root table
537 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
538 * both 32-bit and 64-bit platforms
540 * NOTE: ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on
543 ******************************************************************************/
545 static ACPI_PHYSICAL_ADDRESS
546 AcpiTbGetRootTableEntry (
548 UINT32 TableEntrySize)
554 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
555 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
557 if (TableEntrySize == sizeof (UINT32))
560 * 32-bit platform, RSDT: Return 32-bit table entry
561 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
563 return ((ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST_PTR (UINT32, TableEntry)));
568 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
569 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
572 ACPI_MOVE_64_TO_64 (&Address64, TableEntry);
574 #if ACPI_MACHINE_WIDTH == 32
575 if (Address64 > ACPI_UINT32_MAX)
577 /* Will truncate 64-bit address to 32 bits, issue warning */
579 ACPI_WARNING ((AE_INFO,
580 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
582 ACPI_FORMAT_UINT64 (Address64)));
585 return ((ACPI_PHYSICAL_ADDRESS) (Address64));
590 /*******************************************************************************
592 * FUNCTION: AcpiTbParseRootTable
594 * PARAMETERS: Rsdp - Pointer to the RSDP
598 * DESCRIPTION: This function is called to parse the Root System Description
599 * Table (RSDT or XSDT)
601 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
602 * be mapped and cannot be copied because it contains the actual
603 * memory location of the ACPI Global Lock.
605 ******************************************************************************/
608 AcpiTbParseRootTable (
609 ACPI_PHYSICAL_ADDRESS RsdpAddress)
611 ACPI_TABLE_RSDP *Rsdp;
612 UINT32 TableEntrySize;
615 ACPI_TABLE_HEADER *Table;
616 ACPI_PHYSICAL_ADDRESS Address;
622 ACPI_FUNCTION_TRACE (TbParseRootTable);
626 * Map the entire RSDP and extract the address of the RSDT or XSDT
628 Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP));
631 return_ACPI_STATUS (AE_NO_MEMORY);
634 AcpiTbPrintTableHeader (RsdpAddress,
635 ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp));
637 /* Differentiate between RSDT and XSDT root tables */
639 if (Rsdp->Revision > 1 && Rsdp->XsdtPhysicalAddress)
642 * Root table is an XSDT (64-bit physical addresses). We must use the
643 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
644 * the ACPI specification.
646 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress;
647 TableEntrySize = sizeof (UINT64);
651 /* Root table is an RSDT (32-bit physical addresses) */
653 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;
654 TableEntrySize = sizeof (UINT32);
658 * It is not possible to map more than one entry in some environments,
659 * so unmap the RSDP here before mapping other tables
661 AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP));
664 /* Map the RSDT/XSDT table header to get the full table length */
666 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
669 return_ACPI_STATUS (AE_NO_MEMORY);
672 AcpiTbPrintTableHeader (Address, Table);
674 /* Get the length of the full table, verify length and map entire table */
676 Length = Table->Length;
677 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
679 if (Length < sizeof (ACPI_TABLE_HEADER))
681 ACPI_ERROR ((AE_INFO, "Invalid length 0x%X in RSDT/XSDT", Length));
682 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
685 Table = AcpiOsMapMemory (Address, Length);
688 return_ACPI_STATUS (AE_NO_MEMORY);
691 /* Validate the root table checksum */
693 Status = AcpiTbVerifyChecksum (Table, Length);
694 if (ACPI_FAILURE (Status))
696 AcpiOsUnmapMemory (Table, Length);
697 return_ACPI_STATUS (Status);
700 /* Calculate the number of tables described in the root table */
702 TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /
706 * First two entries in the table array are reserved for the DSDT
707 * and FACS, which are not actually present in the RSDT/XSDT - they
710 TableEntry = ACPI_CAST_PTR (UINT8, Table) + sizeof (ACPI_TABLE_HEADER);
711 AcpiGbl_RootTableList.CurrentTableCount = 2;
714 * Initialize the root table array from the RSDT/XSDT
716 for (i = 0; i < TableCount; i++)
718 if (AcpiGbl_RootTableList.CurrentTableCount >=
719 AcpiGbl_RootTableList.MaxTableCount)
721 /* There is no more room in the root table array, attempt resize */
723 Status = AcpiTbResizeRootTableList ();
724 if (ACPI_FAILURE (Status))
726 ACPI_WARNING ((AE_INFO, "Truncating %u table entries!",
727 (unsigned) (TableCount -
728 (AcpiGbl_RootTableList.CurrentTableCount - 2))));
733 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
735 AcpiGbl_RootTableList.Tables[AcpiGbl_RootTableList.CurrentTableCount].Address =
736 AcpiTbGetRootTableEntry (TableEntry, TableEntrySize);
738 TableEntry += TableEntrySize;
739 AcpiGbl_RootTableList.CurrentTableCount++;
743 * It is not possible to map more than one entry in some environments,
744 * so unmap the root table here before mapping other tables
746 AcpiOsUnmapMemory (Table, Length);
749 * Complete the initialization of the root table array by examining
750 * the header of each table
752 for (i = 2; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
754 AcpiTbInstallTable (AcpiGbl_RootTableList.Tables[i].Address,
757 /* Special case for FADT - get the DSDT and FACS */
759 if (ACPI_COMPARE_NAME (
760 &AcpiGbl_RootTableList.Tables[i].Signature, ACPI_SIG_FADT))
766 return_ACPI_STATUS (AE_OK);