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.
44 #include <acpi/acpi.h>
48 #define _COMPONENT ACPI_TABLES
49 ACPI_MODULE_NAME("tbutils")
51 /* Local prototypes */
52 static void acpi_tb_fix_string(char *string
, acpi_size length
);
55 acpi_tb_cleanup_table_header(struct acpi_table_header
*out_header
,
56 struct acpi_table_header
*header
);
58 static acpi_physical_address
59 acpi_tb_get_root_table_entry(u8
*table_entry
, u32 table_entry_size
);
61 /*******************************************************************************
63 * FUNCTION: acpi_tb_check_xsdt
65 * PARAMETERS: address - Pointer to the XSDT
68 * AE_OK - XSDT is okay
69 * AE_NO_MEMORY - can't map XSDT
70 * AE_INVALID_TABLE_LENGTH - invalid table length
71 * AE_NULL_ENTRY - XSDT has NULL entry
73 * DESCRIPTION: validate XSDT
74 ******************************************************************************/
77 acpi_tb_check_xsdt(acpi_physical_address address
)
79 struct acpi_table_header
*table
;
81 u64 xsdt_entry_address
;
86 table
= acpi_os_map_memory(address
, sizeof(struct acpi_table_header
));
90 length
= table
->length
;
91 acpi_os_unmap_memory(table
, sizeof(struct acpi_table_header
));
92 if (length
< sizeof(struct acpi_table_header
))
93 return AE_INVALID_TABLE_LENGTH
;
95 table
= acpi_os_map_memory(address
, length
);
99 /* Calculate the number of tables described in XSDT */
101 (u32
) ((table
->length
-
102 sizeof(struct acpi_table_header
)) / sizeof(u64
));
104 ACPI_CAST_PTR(u8
, table
) + sizeof(struct acpi_table_header
);
105 for (i
= 0; i
< table_count
; i
++) {
106 ACPI_MOVE_64_TO_64(&xsdt_entry_address
, table_entry
);
107 if (!xsdt_entry_address
) {
108 /* XSDT has NULL entry */
111 table_entry
+= sizeof(u64
);
113 acpi_os_unmap_memory(table
, length
);
116 return AE_NULL_ENTRY
;
121 #if (!ACPI_REDUCED_HARDWARE)
122 /*******************************************************************************
124 * FUNCTION: acpi_tb_initialize_facs
130 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
131 * for accessing the Global Lock and Firmware Waking Vector
133 ******************************************************************************/
135 acpi_status
acpi_tb_initialize_facs(void)
139 /* If Hardware Reduced flag is set, there is no FACS */
141 if (acpi_gbl_reduced_hardware
) {
142 acpi_gbl_FACS
= NULL
;
146 status
= acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS
,
147 ACPI_CAST_INDIRECT_PTR(struct
152 #endif /* !ACPI_REDUCED_HARDWARE */
154 /*******************************************************************************
156 * FUNCTION: acpi_tb_tables_loaded
160 * RETURN: TRUE if required ACPI tables are loaded
162 * DESCRIPTION: Determine if the minimum required ACPI tables are present
165 ******************************************************************************/
167 u8
acpi_tb_tables_loaded(void)
170 if (acpi_gbl_root_table_list
.current_table_count
>= 3) {
177 /*******************************************************************************
179 * FUNCTION: acpi_tb_fix_string
181 * PARAMETERS: string - String to be repaired
182 * length - Maximum length
186 * DESCRIPTION: Replace every non-printable or non-ascii byte in the string
187 * with a question mark '?'.
189 ******************************************************************************/
191 static void acpi_tb_fix_string(char *string
, acpi_size length
)
194 while (length
&& *string
) {
195 if (!ACPI_IS_PRINT(*string
)) {
203 /*******************************************************************************
205 * FUNCTION: acpi_tb_cleanup_table_header
207 * PARAMETERS: out_header - Where the cleaned header is returned
208 * header - Input ACPI table header
210 * RETURN: Returns the cleaned header in out_header
212 * DESCRIPTION: Copy the table header and ensure that all "string" fields in
213 * the header consist of printable characters.
215 ******************************************************************************/
218 acpi_tb_cleanup_table_header(struct acpi_table_header
*out_header
,
219 struct acpi_table_header
*header
)
222 ACPI_MEMCPY(out_header
, header
, sizeof(struct acpi_table_header
));
224 acpi_tb_fix_string(out_header
->signature
, ACPI_NAME_SIZE
);
225 acpi_tb_fix_string(out_header
->oem_id
, ACPI_OEM_ID_SIZE
);
226 acpi_tb_fix_string(out_header
->oem_table_id
, ACPI_OEM_TABLE_ID_SIZE
);
227 acpi_tb_fix_string(out_header
->asl_compiler_id
, ACPI_NAME_SIZE
);
230 /*******************************************************************************
232 * FUNCTION: acpi_tb_print_table_header
234 * PARAMETERS: address - Table physical address
235 * header - Table header
239 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
241 ******************************************************************************/
244 acpi_tb_print_table_header(acpi_physical_address address
,
245 struct acpi_table_header
*header
)
247 struct acpi_table_header local_header
;
250 * The reason that the Address is cast to a void pointer is so that we
251 * can use %p which will work properly on both 32-bit and 64-bit hosts.
253 if (ACPI_COMPARE_NAME(header
->signature
, ACPI_SIG_FACS
)) {
255 /* FACS only has signature and length fields */
257 ACPI_INFO((AE_INFO
, "%4.4s %p %05X",
258 header
->signature
, ACPI_CAST_PTR(void, address
),
260 } else if (ACPI_COMPARE_NAME(header
->signature
, ACPI_SIG_RSDP
)) {
262 /* RSDP has no common fields */
264 ACPI_MEMCPY(local_header
.oem_id
,
265 ACPI_CAST_PTR(struct acpi_table_rsdp
,
266 header
)->oem_id
, ACPI_OEM_ID_SIZE
);
267 acpi_tb_fix_string(local_header
.oem_id
, ACPI_OEM_ID_SIZE
);
269 ACPI_INFO((AE_INFO
, "RSDP %p %05X (v%.2d %6.6s)",
270 ACPI_CAST_PTR (void, address
),
271 (ACPI_CAST_PTR(struct acpi_table_rsdp
, header
)->
273 0) ? ACPI_CAST_PTR(struct acpi_table_rsdp
,
274 header
)->length
: 20,
275 ACPI_CAST_PTR(struct acpi_table_rsdp
,
277 local_header
.oem_id
));
279 /* Standard ACPI table with full common header */
281 acpi_tb_cleanup_table_header(&local_header
, header
);
284 "%4.4s %p %05X (v%.2d %6.6s %8.8s %08X %4.4s %08X)",
285 local_header
.signature
, ACPI_CAST_PTR(void, address
),
286 local_header
.length
, local_header
.revision
,
287 local_header
.oem_id
, local_header
.oem_table_id
,
288 local_header
.oem_revision
,
289 local_header
.asl_compiler_id
,
290 local_header
.asl_compiler_revision
));
295 /*******************************************************************************
297 * FUNCTION: acpi_tb_validate_checksum
299 * PARAMETERS: table - ACPI table to verify
300 * length - Length of entire table
304 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
305 * exception on bad checksum.
307 ******************************************************************************/
309 acpi_status
acpi_tb_verify_checksum(struct acpi_table_header
*table
, u32 length
)
313 /* Compute the checksum on the table */
315 checksum
= acpi_tb_checksum(ACPI_CAST_PTR(u8
, table
), length
);
317 /* Checksum ok? (should be zero) */
320 ACPI_BIOS_WARNING((AE_INFO
,
321 "Incorrect checksum in table [%4.4s] - 0x%2.2X, "
323 table
->signature
, table
->checksum
,
324 (u8
)(table
->checksum
- checksum
)));
326 #if (ACPI_CHECKSUM_ABORT)
328 return (AE_BAD_CHECKSUM
);
335 /*******************************************************************************
337 * FUNCTION: acpi_tb_checksum
339 * PARAMETERS: buffer - Pointer to memory region to be checked
340 * length - Length of this memory region
342 * RETURN: Checksum (u8)
344 * DESCRIPTION: Calculates circular checksum of memory region.
346 ******************************************************************************/
348 u8
acpi_tb_checksum(u8
*buffer
, u32 length
)
351 u8
*end
= buffer
+ length
;
353 while (buffer
< end
) {
354 sum
= (u8
) (sum
+ *(buffer
++));
360 /*******************************************************************************
362 * FUNCTION: acpi_tb_check_dsdt_header
368 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
369 * if the DSDT has been replaced from outside the OS and/or if
370 * the DSDT header has been corrupted.
372 ******************************************************************************/
374 void acpi_tb_check_dsdt_header(void)
377 /* Compare original length and checksum to current values */
379 if (acpi_gbl_original_dsdt_header
.length
!= acpi_gbl_DSDT
->length
||
380 acpi_gbl_original_dsdt_header
.checksum
!= acpi_gbl_DSDT
->checksum
) {
381 ACPI_BIOS_ERROR((AE_INFO
,
382 "The DSDT has been corrupted or replaced - "
383 "old, new headers below"));
384 acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header
);
385 acpi_tb_print_table_header(0, acpi_gbl_DSDT
);
388 "Please send DMI info to linux-acpi@vger.kernel.org\n"
389 "If system does not work as expected, please boot with acpi=copy_dsdt"));
391 /* Disable further error messages */
393 acpi_gbl_original_dsdt_header
.length
= acpi_gbl_DSDT
->length
;
394 acpi_gbl_original_dsdt_header
.checksum
=
395 acpi_gbl_DSDT
->checksum
;
399 /*******************************************************************************
401 * FUNCTION: acpi_tb_copy_dsdt
403 * PARAMETERS: table_desc - Installed table to copy
407 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
408 * Some very bad BIOSs are known to either corrupt the DSDT or
409 * install a new, bad DSDT. This copy works around the problem.
411 ******************************************************************************/
413 struct acpi_table_header
*acpi_tb_copy_dsdt(u32 table_index
)
415 struct acpi_table_header
*new_table
;
416 struct acpi_table_desc
*table_desc
;
418 table_desc
= &acpi_gbl_root_table_list
.tables
[table_index
];
420 new_table
= ACPI_ALLOCATE(table_desc
->length
);
422 ACPI_ERROR((AE_INFO
, "Could not copy DSDT of length 0x%X",
423 table_desc
->length
));
427 ACPI_MEMCPY(new_table
, table_desc
->pointer
, table_desc
->length
);
428 acpi_tb_delete_table(table_desc
);
429 table_desc
->pointer
= new_table
;
430 table_desc
->flags
= ACPI_TABLE_ORIGIN_ALLOCATED
;
433 "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
439 /*******************************************************************************
441 * FUNCTION: acpi_tb_install_table
443 * PARAMETERS: address - Physical address of DSDT or FACS
444 * signature - Table signature, NULL if no need to
446 * table_index - Index into root table array
450 * DESCRIPTION: Install an ACPI table into the global data structure. The
451 * table override mechanism is called to allow the host
452 * OS to replace any table before it is installed in the root
455 ******************************************************************************/
458 acpi_tb_install_table(acpi_physical_address address
,
459 char *signature
, u32 table_index
)
461 struct acpi_table_header
*table
;
462 struct acpi_table_header
*final_table
;
463 struct acpi_table_desc
*table_desc
;
467 "Null physical address for ACPI table [%s]",
472 /* Map just the table header */
474 table
= acpi_os_map_memory(address
, sizeof(struct acpi_table_header
));
477 "Could not map memory for table [%s] at %p",
478 signature
, ACPI_CAST_PTR(void, address
)));
482 /* If a particular signature is expected (DSDT/FACS), it must match */
484 if (signature
&& !ACPI_COMPARE_NAME(table
->signature
, signature
)) {
485 ACPI_BIOS_ERROR((AE_INFO
,
486 "Invalid signature 0x%X for ACPI table, expected [%s]",
487 *ACPI_CAST_PTR(u32
, table
->signature
),
493 * Initialize the table entry. Set the pointer to NULL, since the
494 * table is not fully mapped at this time.
496 table_desc
= &acpi_gbl_root_table_list
.tables
[table_index
];
498 table_desc
->address
= address
;
499 table_desc
->pointer
= NULL
;
500 table_desc
->length
= table
->length
;
501 table_desc
->flags
= ACPI_TABLE_ORIGIN_MAPPED
;
502 ACPI_MOVE_32_TO_32(table_desc
->signature
.ascii
, table
->signature
);
505 * ACPI Table Override:
507 * Before we install the table, let the host OS override it with a new
508 * one if desired. Any table within the RSDT/XSDT can be replaced,
509 * including the DSDT which is pointed to by the FADT.
511 * NOTE: If the table is overridden, then final_table will contain a
512 * mapped pointer to the full new table. If the table is not overridden,
513 * or if there has been a physical override, then the table will be
514 * fully mapped later (in verify table). In any case, we must
515 * unmap the header that was mapped above.
517 final_table
= acpi_tb_table_override(table
, table_desc
);
519 final_table
= table
; /* There was no override */
522 acpi_tb_print_table_header(table_desc
->address
, final_table
);
524 /* Set the global integer width (based upon revision of the DSDT) */
526 if (table_index
== ACPI_TABLE_INDEX_DSDT
) {
527 acpi_ut_set_integer_width(final_table
->revision
);
531 * If we have a physical override during this early loading of the ACPI
532 * tables, unmap the table for now. It will be mapped again later when
533 * it is actually used. This supports very early loading of ACPI tables,
534 * before virtual memory is fully initialized and running within the
535 * host OS. Note: A logical override has the ACPI_TABLE_ORIGIN_OVERRIDE
536 * flag set and will not be deleted below.
538 if (final_table
!= table
) {
539 acpi_tb_delete_table(table_desc
);
544 /* Always unmap the table header that we mapped above */
546 acpi_os_unmap_memory(table
, sizeof(struct acpi_table_header
));
549 /*******************************************************************************
551 * FUNCTION: acpi_tb_get_root_table_entry
553 * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
554 * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
556 * RETURN: Physical address extracted from the root table
558 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
559 * both 32-bit and 64-bit platforms
561 * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
564 ******************************************************************************/
566 static acpi_physical_address
567 acpi_tb_get_root_table_entry(u8
*table_entry
, u32 table_entry_size
)
572 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
573 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
575 if (table_entry_size
== sizeof(u32
)) {
577 * 32-bit platform, RSDT: Return 32-bit table entry
578 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
580 return ((acpi_physical_address
)
581 (*ACPI_CAST_PTR(u32
, table_entry
)));
584 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
585 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
588 ACPI_MOVE_64_TO_64(&address64
, table_entry
);
590 #if ACPI_MACHINE_WIDTH == 32
591 if (address64
> ACPI_UINT32_MAX
) {
593 /* Will truncate 64-bit address to 32 bits, issue warning */
595 ACPI_BIOS_WARNING((AE_INFO
,
596 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
598 ACPI_FORMAT_UINT64(address64
)));
601 return ((acpi_physical_address
) (address64
));
605 /*******************************************************************************
607 * FUNCTION: acpi_tb_parse_root_table
609 * PARAMETERS: rsdp - Pointer to the RSDP
613 * DESCRIPTION: This function is called to parse the Root System Description
614 * Table (RSDT or XSDT)
616 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
617 * be mapped and cannot be copied because it contains the actual
618 * memory location of the ACPI Global Lock.
620 ******************************************************************************/
623 acpi_tb_parse_root_table(acpi_physical_address rsdp_address
)
625 struct acpi_table_rsdp
*rsdp
;
626 u32 table_entry_size
;
629 struct acpi_table_header
*table
;
630 acpi_physical_address address
;
631 acpi_physical_address
uninitialized_var(rsdt_address
);
636 ACPI_FUNCTION_TRACE(tb_parse_root_table
);
639 * Map the entire RSDP and extract the address of the RSDT or XSDT
641 rsdp
= acpi_os_map_memory(rsdp_address
, sizeof(struct acpi_table_rsdp
));
643 return_ACPI_STATUS(AE_NO_MEMORY
);
646 acpi_tb_print_table_header(rsdp_address
,
647 ACPI_CAST_PTR(struct acpi_table_header
,
650 /* Differentiate between RSDT and XSDT root tables */
652 if (rsdp
->revision
> 1 && rsdp
->xsdt_physical_address
653 && !acpi_rsdt_forced
) {
655 * Root table is an XSDT (64-bit physical addresses). We must use the
656 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
657 * the ACPI specification.
659 address
= (acpi_physical_address
) rsdp
->xsdt_physical_address
;
660 table_entry_size
= sizeof(u64
);
661 rsdt_address
= (acpi_physical_address
)
662 rsdp
->rsdt_physical_address
;
664 /* Root table is an RSDT (32-bit physical addresses) */
666 address
= (acpi_physical_address
) rsdp
->rsdt_physical_address
;
667 table_entry_size
= sizeof(u32
);
671 * It is not possible to map more than one entry in some environments,
672 * so unmap the RSDP here before mapping other tables
674 acpi_os_unmap_memory(rsdp
, sizeof(struct acpi_table_rsdp
));
676 if (table_entry_size
== sizeof(u64
)) {
677 if (acpi_tb_check_xsdt(address
) == AE_NULL_ENTRY
) {
678 /* XSDT has NULL entry, RSDT is used */
679 address
= rsdt_address
;
680 table_entry_size
= sizeof(u32
);
681 ACPI_WARNING((AE_INFO
, "BIOS XSDT has NULL entry, "
685 /* Map the RSDT/XSDT table header to get the full table length */
687 table
= acpi_os_map_memory(address
, sizeof(struct acpi_table_header
));
689 return_ACPI_STATUS(AE_NO_MEMORY
);
692 acpi_tb_print_table_header(address
, table
);
694 /* Get the length of the full table, verify length and map entire table */
696 length
= table
->length
;
697 acpi_os_unmap_memory(table
, sizeof(struct acpi_table_header
));
699 if (length
< sizeof(struct acpi_table_header
)) {
700 ACPI_BIOS_ERROR((AE_INFO
,
701 "Invalid table length 0x%X in RSDT/XSDT",
703 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH
);
706 table
= acpi_os_map_memory(address
, length
);
708 return_ACPI_STATUS(AE_NO_MEMORY
);
711 /* Validate the root table checksum */
713 status
= acpi_tb_verify_checksum(table
, length
);
714 if (ACPI_FAILURE(status
)) {
715 acpi_os_unmap_memory(table
, length
);
716 return_ACPI_STATUS(status
);
719 /* Calculate the number of tables described in the root table */
721 table_count
= (u32
)((table
->length
- sizeof(struct acpi_table_header
)) /
724 * First two entries in the table array are reserved for the DSDT
725 * and FACS, which are not actually present in the RSDT/XSDT - they
729 ACPI_CAST_PTR(u8
, table
) + sizeof(struct acpi_table_header
);
730 acpi_gbl_root_table_list
.current_table_count
= 2;
733 * Initialize the root table array from the RSDT/XSDT
735 for (i
= 0; i
< table_count
; i
++) {
736 if (acpi_gbl_root_table_list
.current_table_count
>=
737 acpi_gbl_root_table_list
.max_table_count
) {
739 /* There is no more room in the root table array, attempt resize */
741 status
= acpi_tb_resize_root_table_list();
742 if (ACPI_FAILURE(status
)) {
743 ACPI_WARNING((AE_INFO
,
744 "Truncating %u table entries!",
745 (unsigned) (table_count
-
746 (acpi_gbl_root_table_list
.
747 current_table_count
-
753 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
755 acpi_gbl_root_table_list
.tables
[acpi_gbl_root_table_list
.
756 current_table_count
].address
=
757 acpi_tb_get_root_table_entry(table_entry
, table_entry_size
);
759 table_entry
+= table_entry_size
;
760 acpi_gbl_root_table_list
.current_table_count
++;
764 * It is not possible to map more than one entry in some environments,
765 * so unmap the root table here before mapping other tables
767 acpi_os_unmap_memory(table
, length
);
770 * Complete the initialization of the root table array by examining
771 * the header of each table
773 for (i
= 2; i
< acpi_gbl_root_table_list
.current_table_count
; i
++) {
774 acpi_tb_install_table(acpi_gbl_root_table_list
.tables
[i
].
777 /* Special case for FADT - get the DSDT and FACS */
779 if (ACPI_COMPARE_NAME
780 (&acpi_gbl_root_table_list
.tables
[i
].signature
,
782 acpi_tb_parse_fadt(i
);
786 return_ACPI_STATUS(AE_OK
);