1 /******************************************************************************
3 * Module Name: tbutils - table utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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.
50 #define _COMPONENT ACPI_TABLES
51 ACPI_MODULE_NAME ("tbutils")
54 /* Local prototypes */
62 AcpiTbCleanupTableHeader (
63 ACPI_TABLE_HEADER *OutHeader,
64 ACPI_TABLE_HEADER *Header);
66 static ACPI_PHYSICAL_ADDRESS
67 AcpiTbGetRootTableEntry (
69 UINT32 TableEntrySize);
72 #if (!ACPI_REDUCED_HARDWARE)
73 /*******************************************************************************
75 * FUNCTION: AcpiTbInitializeFacs
81 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
82 * for accessing the Global Lock and Firmware Waking Vector
84 ******************************************************************************/
87 AcpiTbInitializeFacs (
93 /* If Hardware Reduced flag is set, there is no FACS */
95 if (AcpiGbl_ReducedHardware)
101 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
102 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &AcpiGbl_FACS));
105 #endif /* !ACPI_REDUCED_HARDWARE */
108 /*******************************************************************************
110 * FUNCTION: AcpiTbTablesLoaded
114 * RETURN: TRUE if required ACPI tables are loaded
116 * DESCRIPTION: Determine if the minimum required ACPI tables are present
119 ******************************************************************************/
126 if (AcpiGbl_RootTableList.CurrentTableCount >= 3)
135 /*******************************************************************************
137 * FUNCTION: AcpiTbFixString
139 * PARAMETERS: String - String to be repaired
140 * Length - Maximum length
144 * DESCRIPTION: Replace every non-printable or non-ascii byte in the string
145 * with a question mark '?'.
147 ******************************************************************************/
155 while (Length && *String)
157 if (!ACPI_IS_PRINT (*String))
167 /*******************************************************************************
169 * FUNCTION: AcpiTbCleanupTableHeader
171 * PARAMETERS: OutHeader - Where the cleaned header is returned
172 * Header - Input ACPI table header
174 * RETURN: Returns the cleaned header in OutHeader
176 * DESCRIPTION: Copy the table header and ensure that all "string" fields in
177 * the header consist of printable characters.
179 ******************************************************************************/
182 AcpiTbCleanupTableHeader (
183 ACPI_TABLE_HEADER *OutHeader,
184 ACPI_TABLE_HEADER *Header)
187 ACPI_MEMCPY (OutHeader, Header, sizeof (ACPI_TABLE_HEADER));
189 AcpiTbFixString (OutHeader->Signature, ACPI_NAME_SIZE);
190 AcpiTbFixString (OutHeader->OemId, ACPI_OEM_ID_SIZE);
191 AcpiTbFixString (OutHeader->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
192 AcpiTbFixString (OutHeader->AslCompilerId, ACPI_NAME_SIZE);
196 /*******************************************************************************
198 * FUNCTION: AcpiTbPrintTableHeader
200 * PARAMETERS: Address - Table physical address
201 * Header - Table header
205 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
207 ******************************************************************************/
210 AcpiTbPrintTableHeader (
211 ACPI_PHYSICAL_ADDRESS Address,
212 ACPI_TABLE_HEADER *Header)
214 ACPI_TABLE_HEADER LocalHeader;
218 * The reason that the Address is cast to a void pointer is so that we
219 * can use %p which will work properly on both 32-bit and 64-bit hosts.
221 if (ACPI_COMPARE_NAME (Header->Signature, ACPI_SIG_FACS))
223 /* FACS only has signature and length fields */
225 ACPI_INFO ((AE_INFO, "%4.4s %p %05X",
226 Header->Signature, ACPI_CAST_PTR (void, Address),
229 else if (ACPI_COMPARE_NAME (Header->Signature, ACPI_SIG_RSDP))
231 /* RSDP has no common fields */
233 ACPI_MEMCPY (LocalHeader.OemId,
234 ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->OemId, ACPI_OEM_ID_SIZE);
235 AcpiTbFixString (LocalHeader.OemId, ACPI_OEM_ID_SIZE);
237 ACPI_INFO ((AE_INFO, "RSDP %p %05X (v%.2d %6.6s)",
238 ACPI_CAST_PTR (void, Address),
239 (ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->Revision > 0) ?
240 ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->Length : 20,
241 ACPI_CAST_PTR (ACPI_TABLE_RSDP, Header)->Revision,
246 /* Standard ACPI table with full common header */
248 AcpiTbCleanupTableHeader (&LocalHeader, Header);
251 "%4.4s %p %05X (v%.2d %6.6s %8.8s %08X %4.4s %08X)",
252 LocalHeader.Signature, ACPI_CAST_PTR (void, Address),
253 LocalHeader.Length, LocalHeader.Revision, LocalHeader.OemId,
254 LocalHeader.OemTableId, LocalHeader.OemRevision,
255 LocalHeader.AslCompilerId, LocalHeader.AslCompilerRevision));
260 /*******************************************************************************
262 * FUNCTION: AcpiTbValidateChecksum
264 * PARAMETERS: Table - ACPI table to verify
265 * Length - Length of entire table
269 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
270 * exception on bad checksum.
272 ******************************************************************************/
275 AcpiTbVerifyChecksum (
276 ACPI_TABLE_HEADER *Table,
282 /* Compute the checksum on the table */
284 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Length);
286 /* Checksum ok? (should be zero) */
290 ACPI_BIOS_WARNING ((AE_INFO,
291 "Incorrect checksum in table [%4.4s] - 0x%2.2X, "
293 Table->Signature, Table->Checksum,
294 (UINT8) (Table->Checksum - Checksum)));
296 #if (ACPI_CHECKSUM_ABORT)
297 return (AE_BAD_CHECKSUM);
305 /*******************************************************************************
307 * FUNCTION: AcpiTbChecksum
309 * PARAMETERS: Buffer - Pointer to memory region to be checked
310 * Length - Length of this memory region
312 * RETURN: Checksum (UINT8)
314 * DESCRIPTION: Calculates circular checksum of memory region.
316 ******************************************************************************/
324 UINT8 *End = Buffer + Length;
329 Sum = (UINT8) (Sum + *(Buffer++));
336 /*******************************************************************************
338 * FUNCTION: AcpiTbCheckDsdtHeader
344 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
345 * if the DSDT has been replaced from outside the OS and/or if
346 * the DSDT header has been corrupted.
348 ******************************************************************************/
351 AcpiTbCheckDsdtHeader (
355 /* Compare original length and checksum to current values */
357 if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length ||
358 AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum)
360 ACPI_BIOS_ERROR ((AE_INFO,
361 "The DSDT has been corrupted or replaced - "
362 "old, new headers below"));
363 AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader);
364 AcpiTbPrintTableHeader (0, AcpiGbl_DSDT);
366 /* Disable further error messages */
368 AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length;
369 AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum;
374 /*******************************************************************************
376 * FUNCTION: AcpiTbCopyDsdt
378 * PARAMETERS: TableDesc - Installed table to copy
382 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
383 * Some very bad BIOSs are known to either corrupt the DSDT or
384 * install a new, bad DSDT. This copy works around the problem.
386 ******************************************************************************/
392 ACPI_TABLE_HEADER *NewTable;
393 ACPI_TABLE_DESC *TableDesc;
396 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];
398 NewTable = ACPI_ALLOCATE (TableDesc->Length);
401 ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X",
406 ACPI_MEMCPY (NewTable, TableDesc->Pointer, TableDesc->Length);
407 AcpiTbDeleteTable (TableDesc);
408 TableDesc->Pointer = NewTable;
409 TableDesc->Flags = ACPI_TABLE_ORIGIN_ALLOCATED;
412 "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
419 /*******************************************************************************
421 * FUNCTION: AcpiTbInstallTable
423 * PARAMETERS: Address - Physical address of DSDT or FACS
424 * Signature - Table signature, NULL if no need to
426 * TableIndex - Index into root table array
430 * DESCRIPTION: Install an ACPI table into the global data structure. The
431 * table override mechanism is called to allow the host
432 * OS to replace any table before it is installed in the root
435 ******************************************************************************/
439 ACPI_PHYSICAL_ADDRESS Address,
443 ACPI_TABLE_HEADER *Table;
444 ACPI_TABLE_HEADER *FinalTable;
445 ACPI_TABLE_DESC *TableDesc;
450 ACPI_ERROR ((AE_INFO, "Null physical address for ACPI table [%s]",
455 /* Map just the table header */
457 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
460 ACPI_ERROR ((AE_INFO, "Could not map memory for table [%s] at %p",
461 Signature, ACPI_CAST_PTR (void, Address)));
465 /* If a particular signature is expected (DSDT/FACS), it must match */
468 !ACPI_COMPARE_NAME (Table->Signature, Signature))
470 ACPI_BIOS_ERROR ((AE_INFO,
471 "Invalid signature 0x%X for ACPI table, expected [%s]",
472 *ACPI_CAST_PTR (UINT32, Table->Signature), Signature));
477 * Initialize the table entry. Set the pointer to NULL, since the
478 * table is not fully mapped at this time.
480 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];
482 TableDesc->Address = Address;
483 TableDesc->Pointer = NULL;
484 TableDesc->Length = Table->Length;
485 TableDesc->Flags = ACPI_TABLE_ORIGIN_MAPPED;
486 ACPI_MOVE_32_TO_32 (TableDesc->Signature.Ascii, Table->Signature);
489 * ACPI Table Override:
491 * Before we install the table, let the host OS override it with a new
492 * one if desired. Any table within the RSDT/XSDT can be replaced,
493 * including the DSDT which is pointed to by the FADT.
495 * NOTE: If the table is overridden, then FinalTable will contain a
496 * mapped pointer to the full new table. If the table is not overridden,
497 * or if there has been a physical override, then the table will be
498 * fully mapped later (in verify table). In any case, we must
499 * unmap the header that was mapped above.
501 FinalTable = AcpiTbTableOverride (Table, TableDesc);
504 FinalTable = Table; /* There was no override */
507 AcpiTbPrintTableHeader (TableDesc->Address, FinalTable);
509 /* Set the global integer width (based upon revision of the DSDT) */
511 if (TableIndex == ACPI_TABLE_INDEX_DSDT)
513 AcpiUtSetIntegerWidth (FinalTable->Revision);
517 * If we have a physical override during this early loading of the ACPI
518 * tables, unmap the table for now. It will be mapped again later when
519 * it is actually used. This supports very early loading of ACPI tables,
520 * before virtual memory is fully initialized and running within the
521 * host OS. Note: A logical override has the ACPI_TABLE_ORIGIN_OVERRIDE
522 * flag set and will not be deleted below.
524 if (FinalTable != Table)
526 AcpiTbDeleteTable (TableDesc);
532 /* Always unmap the table header that we mapped above */
534 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
538 /*******************************************************************************
540 * FUNCTION: AcpiTbGetRootTableEntry
542 * PARAMETERS: TableEntry - Pointer to the RSDT/XSDT table entry
543 * TableEntrySize - sizeof 32 or 64 (RSDT or XSDT)
545 * RETURN: Physical address extracted from the root table
547 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
548 * both 32-bit and 64-bit platforms
550 * NOTE: ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on
553 ******************************************************************************/
555 static ACPI_PHYSICAL_ADDRESS
556 AcpiTbGetRootTableEntry (
558 UINT32 TableEntrySize)
564 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
565 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
567 if (TableEntrySize == sizeof (UINT32))
570 * 32-bit platform, RSDT: Return 32-bit table entry
571 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
573 return ((ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST_PTR (UINT32, TableEntry)));
578 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
579 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
582 ACPI_MOVE_64_TO_64 (&Address64, TableEntry);
584 #if ACPI_MACHINE_WIDTH == 32
585 if (Address64 > ACPI_UINT32_MAX)
587 /* Will truncate 64-bit address to 32 bits, issue warning */
589 ACPI_BIOS_WARNING ((AE_INFO,
590 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
592 ACPI_FORMAT_UINT64 (Address64)));
595 return ((ACPI_PHYSICAL_ADDRESS) (Address64));
600 /*******************************************************************************
602 * FUNCTION: AcpiTbParseRootTable
604 * PARAMETERS: Rsdp - Pointer to the RSDP
608 * DESCRIPTION: This function is called to parse the Root System Description
609 * Table (RSDT or XSDT)
611 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
612 * be mapped and cannot be copied because it contains the actual
613 * memory location of the ACPI Global Lock.
615 ******************************************************************************/
618 AcpiTbParseRootTable (
619 ACPI_PHYSICAL_ADDRESS RsdpAddress)
621 ACPI_TABLE_RSDP *Rsdp;
622 UINT32 TableEntrySize;
625 ACPI_TABLE_HEADER *Table;
626 ACPI_PHYSICAL_ADDRESS Address;
632 ACPI_FUNCTION_TRACE (TbParseRootTable);
636 * Map the entire RSDP and extract the address of the RSDT or XSDT
638 Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP));
641 return_ACPI_STATUS (AE_NO_MEMORY);
644 AcpiTbPrintTableHeader (RsdpAddress,
645 ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp));
647 /* Differentiate between RSDT and XSDT root tables */
649 if (Rsdp->Revision > 1 && Rsdp->XsdtPhysicalAddress)
652 * Root table is an XSDT (64-bit physical addresses). We must use the
653 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
654 * the ACPI specification.
656 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress;
657 TableEntrySize = sizeof (UINT64);
661 /* Root table is an RSDT (32-bit physical addresses) */
663 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;
664 TableEntrySize = sizeof (UINT32);
668 * It is not possible to map more than one entry in some environments,
669 * so unmap the RSDP here before mapping other tables
671 AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP));
674 /* Map the RSDT/XSDT table header to get the full table length */
676 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
679 return_ACPI_STATUS (AE_NO_MEMORY);
682 AcpiTbPrintTableHeader (Address, Table);
684 /* Get the length of the full table, verify length and map entire table */
686 Length = Table->Length;
687 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
689 if (Length < sizeof (ACPI_TABLE_HEADER))
691 ACPI_BIOS_ERROR ((AE_INFO,
692 "Invalid table length 0x%X in RSDT/XSDT", Length));
693 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
696 Table = AcpiOsMapMemory (Address, Length);
699 return_ACPI_STATUS (AE_NO_MEMORY);
702 /* Validate the root table checksum */
704 Status = AcpiTbVerifyChecksum (Table, Length);
705 if (ACPI_FAILURE (Status))
707 AcpiOsUnmapMemory (Table, Length);
708 return_ACPI_STATUS (Status);
711 /* Calculate the number of tables described in the root table */
713 TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /
717 * First two entries in the table array are reserved for the DSDT
718 * and FACS, which are not actually present in the RSDT/XSDT - they
721 TableEntry = ACPI_CAST_PTR (UINT8, Table) + sizeof (ACPI_TABLE_HEADER);
722 AcpiGbl_RootTableList.CurrentTableCount = 2;
725 * Initialize the root table array from the RSDT/XSDT
727 for (i = 0; i < TableCount; i++)
729 if (AcpiGbl_RootTableList.CurrentTableCount >=
730 AcpiGbl_RootTableList.MaxTableCount)
732 /* There is no more room in the root table array, attempt resize */
734 Status = AcpiTbResizeRootTableList ();
735 if (ACPI_FAILURE (Status))
737 ACPI_WARNING ((AE_INFO, "Truncating %u table entries!",
738 (unsigned) (TableCount -
739 (AcpiGbl_RootTableList.CurrentTableCount - 2))));
744 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
746 AcpiGbl_RootTableList.Tables[AcpiGbl_RootTableList.CurrentTableCount].Address =
747 AcpiTbGetRootTableEntry (TableEntry, TableEntrySize);
749 TableEntry += TableEntrySize;
750 AcpiGbl_RootTableList.CurrentTableCount++;
754 * It is not possible to map more than one entry in some environments,
755 * so unmap the root table here before mapping other tables
757 AcpiOsUnmapMemory (Table, Length);
760 * Complete the initialization of the root table array by examining
761 * the header of each table
763 for (i = 2; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
765 AcpiTbInstallTable (AcpiGbl_RootTableList.Tables[i].Address,
768 /* Special case for FADT - get the DSDT and FACS */
770 if (ACPI_COMPARE_NAME (
771 &AcpiGbl_RootTableList.Tables[i].Signature, ACPI_SIG_FADT))
777 return_ACPI_STATUS (AE_OK);