1 /****************************************************************************** 2 * 3 * Module Name: tbutils - ACPI Table utilities 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2013, 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 <contrib/dev/acpica/include/acpi.h> 47 #include <contrib/dev/acpica/include/accommon.h> 48 #include <contrib/dev/acpica/include/actables.h> 49 50 #define _COMPONENT ACPI_TABLES 51 ACPI_MODULE_NAME ("tbutils") 52 53 54 /* Local prototypes */ 55 56 static ACPI_PHYSICAL_ADDRESS 57 AcpiTbGetRootTableEntry ( 58 UINT8 *TableEntry, 59 UINT32 TableEntrySize); 60 61 62 #if (!ACPI_REDUCED_HARDWARE) 63 /******************************************************************************* 64 * 65 * FUNCTION: AcpiTbInitializeFacs 66 * 67 * PARAMETERS: None 68 * 69 * RETURN: Status 70 * 71 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global 72 * for accessing the Global Lock and Firmware Waking Vector 73 * 74 ******************************************************************************/ 75 76 ACPI_STATUS 77 AcpiTbInitializeFacs ( 78 void) 79 { 80 ACPI_STATUS Status; 81 82 83 /* If Hardware Reduced flag is set, there is no FACS */ 84 85 if (AcpiGbl_ReducedHardware) 86 { 87 AcpiGbl_FACS = NULL; 88 return (AE_OK); 89 } 90 91 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS, 92 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &AcpiGbl_FACS)); 93 return (Status); 94 } 95 #endif /* !ACPI_REDUCED_HARDWARE */ 96 97 98 /******************************************************************************* 99 * 100 * FUNCTION: AcpiTbTablesLoaded 101 * 102 * PARAMETERS: None 103 * 104 * RETURN: TRUE if required ACPI tables are loaded 105 * 106 * DESCRIPTION: Determine if the minimum required ACPI tables are present 107 * (FADT, FACS, DSDT) 108 * 109 ******************************************************************************/ 110 111 BOOLEAN 112 AcpiTbTablesLoaded ( 113 void) 114 { 115 116 if (AcpiGbl_RootTableList.CurrentTableCount >= 3) 117 { 118 return (TRUE); 119 } 120 121 return (FALSE); 122 } 123 124 125 /******************************************************************************* 126 * 127 * FUNCTION: AcpiTbCheckDsdtHeader 128 * 129 * PARAMETERS: None 130 * 131 * RETURN: None 132 * 133 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect 134 * if the DSDT has been replaced from outside the OS and/or if 135 * the DSDT header has been corrupted. 136 * 137 ******************************************************************************/ 138 139 void 140 AcpiTbCheckDsdtHeader ( 141 void) 142 { 143 144 /* Compare original length and checksum to current values */ 145 146 if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length || 147 AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum) 148 { 149 ACPI_BIOS_ERROR ((AE_INFO, 150 "The DSDT has been corrupted or replaced - " 151 "old, new headers below")); 152 AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader); 153 AcpiTbPrintTableHeader (0, AcpiGbl_DSDT); 154 155 /* Disable further error messages */ 156 157 AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length; 158 AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum; 159 } 160 } 161 162 163 /******************************************************************************* 164 * 165 * FUNCTION: AcpiTbCopyDsdt 166 * 167 * PARAMETERS: TableDesc - Installed table to copy 168 * 169 * RETURN: None 170 * 171 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory. 172 * Some very bad BIOSs are known to either corrupt the DSDT or 173 * install a new, bad DSDT. This copy works around the problem. 174 * 175 ******************************************************************************/ 176 177 ACPI_TABLE_HEADER * 178 AcpiTbCopyDsdt ( 179 UINT32 TableIndex) 180 { 181 ACPI_TABLE_HEADER *NewTable; 182 ACPI_TABLE_DESC *TableDesc; 183 184 185 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex]; 186 187 NewTable = ACPI_ALLOCATE (TableDesc->Length); 188 if (!NewTable) 189 { 190 ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X", 191 TableDesc->Length)); 192 return (NULL); 193 } 194 195 ACPI_MEMCPY (NewTable, TableDesc->Pointer, TableDesc->Length); 196 AcpiTbDeleteTable (TableDesc); 197 TableDesc->Pointer = NewTable; 198 TableDesc->Flags = ACPI_TABLE_ORIGIN_ALLOCATED; 199 200 ACPI_INFO ((AE_INFO, 201 "Forced DSDT copy: length 0x%05X copied locally, original unmapped", 202 NewTable->Length)); 203 204 return (NewTable); 205 } 206 207 208 /******************************************************************************* 209 * 210 * FUNCTION: AcpiTbInstallTable 211 * 212 * PARAMETERS: Address - Physical address of DSDT or FACS 213 * Signature - Table signature, NULL if no need to 214 * match 215 * TableIndex - Index into root table array 216 * 217 * RETURN: None 218 * 219 * DESCRIPTION: Install an ACPI table into the global data structure. The 220 * table override mechanism is called to allow the host 221 * OS to replace any table before it is installed in the root 222 * table array. 223 * 224 ******************************************************************************/ 225 226 void 227 AcpiTbInstallTable ( 228 ACPI_PHYSICAL_ADDRESS Address, 229 char *Signature, 230 UINT32 TableIndex) 231 { 232 ACPI_TABLE_HEADER *Table; 233 ACPI_TABLE_HEADER *FinalTable; 234 ACPI_TABLE_DESC *TableDesc; 235 236 237 if (!Address) 238 { 239 ACPI_ERROR ((AE_INFO, "Null physical address for ACPI table [%s]", 240 Signature)); 241 return; 242 } 243 244 /* Map just the table header */ 245 246 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER)); 247 if (!Table) 248 { 249 ACPI_ERROR ((AE_INFO, "Could not map memory for table [%s] at %p", 250 Signature, ACPI_CAST_PTR (void, Address))); 251 return; 252 } 253 254 /* If a particular signature is expected (DSDT/FACS), it must match */ 255 256 if (Signature && 257 !ACPI_COMPARE_NAME (Table->Signature, Signature)) 258 { 259 ACPI_BIOS_ERROR ((AE_INFO, 260 "Invalid signature 0x%X for ACPI table, expected [%s]", 261 *ACPI_CAST_PTR (UINT32, Table->Signature), Signature)); 262 goto UnmapAndExit; 263 } 264 265 /* 266 * Initialize the table entry. Set the pointer to NULL, since the 267 * table is not fully mapped at this time. 268 */ 269 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex]; 270 271 TableDesc->Address = Address; 272 TableDesc->Pointer = NULL; 273 TableDesc->Length = Table->Length; 274 TableDesc->Flags = ACPI_TABLE_ORIGIN_MAPPED; 275 ACPI_MOVE_32_TO_32 (TableDesc->Signature.Ascii, Table->Signature); 276 277 /* 278 * ACPI Table Override: 279 * 280 * Before we install the table, let the host OS override it with a new 281 * one if desired. Any table within the RSDT/XSDT can be replaced, 282 * including the DSDT which is pointed to by the FADT. 283 * 284 * NOTE: If the table is overridden, then FinalTable will contain a 285 * mapped pointer to the full new table. If the table is not overridden, 286 * or if there has been a physical override, then the table will be 287 * fully mapped later (in verify table). In any case, we must 288 * unmap the header that was mapped above. 289 */ 290 FinalTable = AcpiTbTableOverride (Table, TableDesc); 291 if (!FinalTable) 292 { 293 FinalTable = Table; /* There was no override */ 294 } 295 296 AcpiTbPrintTableHeader (TableDesc->Address, FinalTable); 297 298 /* Set the global integer width (based upon revision of the DSDT) */ 299 300 if (TableIndex == ACPI_TABLE_INDEX_DSDT) 301 { 302 AcpiUtSetIntegerWidth (FinalTable->Revision); 303 } 304 305 /* 306 * If we have a physical override during this early loading of the ACPI 307 * tables, unmap the table for now. It will be mapped again later when 308 * it is actually used. This supports very early loading of ACPI tables, 309 * before virtual memory is fully initialized and running within the 310 * host OS. Note: A logical override has the ACPI_TABLE_ORIGIN_OVERRIDE 311 * flag set and will not be deleted below. 312 */ 313 if (FinalTable != Table) 314 { 315 AcpiTbDeleteTable (TableDesc); 316 } 317 318 319 UnmapAndExit: 320 321 /* Always unmap the table header that we mapped above */ 322 323 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER)); 324 } 325 326 327 /******************************************************************************* 328 * 329 * FUNCTION: AcpiTbGetRootTableEntry 330 * 331 * PARAMETERS: TableEntry - Pointer to the RSDT/XSDT table entry 332 * TableEntrySize - sizeof 32 or 64 (RSDT or XSDT) 333 * 334 * RETURN: Physical address extracted from the root table 335 * 336 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on 337 * both 32-bit and 64-bit platforms 338 * 339 * NOTE: ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on 340 * 64-bit platforms. 341 * 342 ******************************************************************************/ 343 344 static ACPI_PHYSICAL_ADDRESS 345 AcpiTbGetRootTableEntry ( 346 UINT8 *TableEntry, 347 UINT32 TableEntrySize) 348 { 349 UINT64 Address64; 350 351 352 /* 353 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT): 354 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT 355 */ 356 if (TableEntrySize == sizeof (UINT32)) 357 { 358 /* 359 * 32-bit platform, RSDT: Return 32-bit table entry 360 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return 361 */ 362 return ((ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST_PTR (UINT32, TableEntry))); 363 } 364 else 365 { 366 /* 367 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return 368 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local, 369 * return 64-bit 370 */ 371 ACPI_MOVE_64_TO_64 (&Address64, TableEntry); 372 373 #if ACPI_MACHINE_WIDTH == 32 374 if (Address64 > ACPI_UINT32_MAX) 375 { 376 /* Will truncate 64-bit address to 32 bits, issue warning */ 377 378 ACPI_BIOS_WARNING ((AE_INFO, 379 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X)," 380 " truncating", 381 ACPI_FORMAT_UINT64 (Address64))); 382 } 383 #endif 384 return ((ACPI_PHYSICAL_ADDRESS) (Address64)); 385 } 386 } 387 388 389 /******************************************************************************* 390 * 391 * FUNCTION: AcpiTbParseRootTable 392 * 393 * PARAMETERS: Rsdp - Pointer to the RSDP 394 * 395 * RETURN: Status 396 * 397 * DESCRIPTION: This function is called to parse the Root System Description 398 * Table (RSDT or XSDT) 399 * 400 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must 401 * be mapped and cannot be copied because it contains the actual 402 * memory location of the ACPI Global Lock. 403 * 404 ******************************************************************************/ 405 406 ACPI_STATUS 407 AcpiTbParseRootTable ( 408 ACPI_PHYSICAL_ADDRESS RsdpAddress) 409 { 410 ACPI_TABLE_RSDP *Rsdp; 411 UINT32 TableEntrySize; 412 UINT32 i; 413 UINT32 TableCount; 414 ACPI_TABLE_HEADER *Table; 415 ACPI_PHYSICAL_ADDRESS Address; 416 UINT32 Length; 417 UINT8 *TableEntry; 418 ACPI_STATUS Status; 419 420 421 ACPI_FUNCTION_TRACE (TbParseRootTable); 422 423 424 /* 425 * Map the entire RSDP and extract the address of the RSDT or XSDT 426 */ 427 Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP)); 428 if (!Rsdp) 429 { 430 return_ACPI_STATUS (AE_NO_MEMORY); 431 } 432 433 AcpiTbPrintTableHeader (RsdpAddress, 434 ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp)); 435 436 /* Differentiate between RSDT and XSDT root tables */ 437 438 if (Rsdp->Revision > 1 && Rsdp->XsdtPhysicalAddress) 439 { 440 /* 441 * Root table is an XSDT (64-bit physical addresses). We must use the 442 * XSDT if the revision is > 1 and the XSDT pointer is present, as per 443 * the ACPI specification. 444 */ 445 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress; 446 TableEntrySize = sizeof (UINT64); 447 } 448 else 449 { 450 /* Root table is an RSDT (32-bit physical addresses) */ 451 452 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress; 453 TableEntrySize = sizeof (UINT32); 454 } 455 456 /* 457 * It is not possible to map more than one entry in some environments, 458 * so unmap the RSDP here before mapping other tables 459 */ 460 AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP)); 461 462 463 /* Map the RSDT/XSDT table header to get the full table length */ 464 465 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER)); 466 if (!Table) 467 { 468 return_ACPI_STATUS (AE_NO_MEMORY); 469 } 470 471 AcpiTbPrintTableHeader (Address, Table); 472 473 /* Get the length of the full table, verify length and map entire table */ 474 475 Length = Table->Length; 476 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER)); 477 478 if (Length < sizeof (ACPI_TABLE_HEADER)) 479 { 480 ACPI_BIOS_ERROR ((AE_INFO, 481 "Invalid table length 0x%X in RSDT/XSDT", Length)); 482 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH); 483 } 484 485 Table = AcpiOsMapMemory (Address, Length); 486 if (!Table) 487 { 488 return_ACPI_STATUS (AE_NO_MEMORY); 489 } 490 491 /* Validate the root table checksum */ 492 493 Status = AcpiTbVerifyChecksum (Table, Length); 494 if (ACPI_FAILURE (Status)) 495 { 496 AcpiOsUnmapMemory (Table, Length); 497 return_ACPI_STATUS (Status); 498 } 499 500 /* Calculate the number of tables described in the root table */ 501 502 TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) / 503 TableEntrySize); 504 505 /* 506 * First two entries in the table array are reserved for the DSDT 507 * and FACS, which are not actually present in the RSDT/XSDT - they 508 * come from the FADT 509 */ 510 TableEntry = ACPI_CAST_PTR (UINT8, Table) + sizeof (ACPI_TABLE_HEADER); 511 AcpiGbl_RootTableList.CurrentTableCount = 2; 512 513 /* 514 * Initialize the root table array from the RSDT/XSDT 515 */ 516 for (i = 0; i < TableCount; i++) 517 { 518 if (AcpiGbl_RootTableList.CurrentTableCount >= 519 AcpiGbl_RootTableList.MaxTableCount) 520 { 521 /* There is no more room in the root table array, attempt resize */ 522 523 Status = AcpiTbResizeRootTableList (); 524 if (ACPI_FAILURE (Status)) 525 { 526 ACPI_WARNING ((AE_INFO, "Truncating %u table entries!", 527 (unsigned) (TableCount - 528 (AcpiGbl_RootTableList.CurrentTableCount - 2)))); 529 break; 530 } 531 } 532 533 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */ 534 535 AcpiGbl_RootTableList.Tables[AcpiGbl_RootTableList.CurrentTableCount].Address = 536 AcpiTbGetRootTableEntry (TableEntry, TableEntrySize); 537 538 TableEntry += TableEntrySize; 539 AcpiGbl_RootTableList.CurrentTableCount++; 540 } 541 542 /* 543 * It is not possible to map more than one entry in some environments, 544 * so unmap the root table here before mapping other tables 545 */ 546 AcpiOsUnmapMemory (Table, Length); 547 548 /* 549 * Complete the initialization of the root table array by examining 550 * the header of each table 551 */ 552 for (i = 2; i < AcpiGbl_RootTableList.CurrentTableCount; i++) 553 { 554 AcpiTbInstallTable (AcpiGbl_RootTableList.Tables[i].Address, 555 NULL, i); 556 557 /* Special case for FADT - get the DSDT and FACS */ 558 559 if (ACPI_COMPARE_NAME ( 560 &AcpiGbl_RootTableList.Tables[i].Signature, ACPI_SIG_FADT)) 561 { 562 AcpiTbParseFadt (i); 563 } 564 } 565 566 return_ACPI_STATUS (AE_OK); 567 } 568