1 /******************************************************************************
3 * Module Name: tbutils - table utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2006, R. Byron Moore
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>
45 #include <acpi/actables.h>
47 #define _COMPONENT ACPI_TABLES
48 ACPI_MODULE_NAME("tbutils")
50 /* Local prototypes */
51 static void acpi_tb_parse_fadt(acpi_native_uint table_index, u8 flags);
53 static void acpi_tb_convert_fadt(void);
56 acpi_tb_install_table(acpi_physical_address address,
57 u8 flags, char *signature, acpi_native_uint table_index);
60 acpi_tb_init_generic_address(struct acpi_generic_address *new_gas_struct,
61 u8 bit_width, u64 address);
63 static acpi_physical_address
64 acpi_tb_get_root_table_entry(u8 * table_entry,
65 acpi_native_uint table_entry_size);
67 /* Table used for conversion of FADT to common format */
69 typedef struct acpi_fadt_conversion {
74 } acpi_fadt_conversion;
76 static struct acpi_fadt_conversion fadt_conversion_table[] = {
77 {ACPI_FADT_OFFSET(xpm1a_event_block),
78 ACPI_FADT_OFFSET(pm1a_event_block),
79 ACPI_FADT_OFFSET(pm1_event_length)},
80 {ACPI_FADT_OFFSET(xpm1b_event_block),
81 ACPI_FADT_OFFSET(pm1b_event_block),
82 ACPI_FADT_OFFSET(pm1_event_length)},
83 {ACPI_FADT_OFFSET(xpm1a_control_block),
84 ACPI_FADT_OFFSET(pm1a_control_block),
85 ACPI_FADT_OFFSET(pm1_control_length)},
86 {ACPI_FADT_OFFSET(xpm1b_control_block),
87 ACPI_FADT_OFFSET(pm1b_control_block),
88 ACPI_FADT_OFFSET(pm1_control_length)},
89 {ACPI_FADT_OFFSET(xpm2_control_block),
90 ACPI_FADT_OFFSET(pm2_control_block),
91 ACPI_FADT_OFFSET(pm2_control_length)},
92 {ACPI_FADT_OFFSET(xpm_timer_block), ACPI_FADT_OFFSET(pm_timer_block),
93 ACPI_FADT_OFFSET(pm_timer_length)},
94 {ACPI_FADT_OFFSET(xgpe0_block), ACPI_FADT_OFFSET(gpe0_block),
95 ACPI_FADT_OFFSET(gpe0_block_length)},
96 {ACPI_FADT_OFFSET(xgpe1_block), ACPI_FADT_OFFSET(gpe1_block),
97 ACPI_FADT_OFFSET(gpe1_block_length)}
100 #define ACPI_FADT_CONVERSION_ENTRIES (sizeof (fadt_conversion_table) / sizeof (struct acpi_fadt_conversion))
102 /*******************************************************************************
104 * FUNCTION: acpi_tb_print_table_header
106 * PARAMETERS: Address - Table physical address
107 * Header - Table header
111 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
113 ******************************************************************************/
116 acpi_tb_print_table_header(acpi_physical_address address,
117 struct acpi_table_header *header)
120 if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
122 /* FACS only has signature and length fields of common table header */
124 ACPI_INFO((AE_INFO, "%4.4s @ 0x%p/0x%04X",
125 header->signature, ACPI_CAST_PTR(void, address),
127 } else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
129 /* RSDP has no common fields */
131 ACPI_INFO((AE_INFO, "RSDP @ 0x%p/0x%04X (v%3.3d %6.6s)",
132 ACPI_CAST_PTR(void, address),
133 (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
135 0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
136 header)->length : 20,
137 ACPI_CAST_PTR(struct acpi_table_rsdp,
139 ACPI_CAST_PTR(struct acpi_table_rsdp,
142 /* Standard ACPI table with full common header */
145 "%4.4s @ 0x%p/0x%04X (v%3.3d %6.6s %8.8s 0x%08X %4.4s 0x%08X)",
146 header->signature, ACPI_CAST_PTR(void, address),
147 header->length, header->revision, header->oem_id,
148 header->oem_table_id, header->oem_revision,
149 header->asl_compiler_id,
150 header->asl_compiler_revision));
154 /*******************************************************************************
156 * FUNCTION: acpi_tb_init_generic_address
158 * PARAMETERS: new_gas_struct - GAS struct to be initialized
159 * bit_width - Width of this register
160 * Address - Address of the register
164 * DESCRIPTION: Initialize a GAS structure.
166 ******************************************************************************/
169 acpi_tb_init_generic_address(struct acpi_generic_address *new_gas_struct,
170 u8 bit_width, u64 address)
173 ACPI_MOVE_64_TO_64(&new_gas_struct->address, &address);
174 new_gas_struct->space_id = ACPI_ADR_SPACE_SYSTEM_IO;
175 new_gas_struct->bit_width = bit_width;
176 new_gas_struct->bit_offset = 0;
177 new_gas_struct->access_width = 0;
180 /*******************************************************************************
182 * FUNCTION: acpi_tb_validate_checksum
184 * PARAMETERS: Table - ACPI table to verify
185 * Length - Length of entire table
189 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
190 * exception on bad checksum.
192 ******************************************************************************/
194 acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
198 /* Compute the checksum on the table */
200 checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
202 /* Checksum ok? (should be zero) */
205 ACPI_WARNING((AE_INFO,
206 "Incorrect checksum in table [%4.4s] - %2.2X, should be %2.2X",
207 table->signature, table->checksum,
208 (u8) (table->checksum - checksum)));
210 #if (ACPI_CHECKSUM_ABORT)
212 return (AE_BAD_CHECKSUM);
219 /*******************************************************************************
221 * FUNCTION: acpi_tb_checksum
223 * PARAMETERS: Buffer - Pointer to memory region to be checked
224 * Length - Length of this memory region
226 * RETURN: Checksum (u8)
228 * DESCRIPTION: Calculates circular checksum of memory region.
230 ******************************************************************************/
232 u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
235 u8 *end = buffer + length;
237 while (buffer < end) {
238 sum = (u8) (sum + *(buffer++));
244 /*******************************************************************************
246 * FUNCTION: acpi_tb_convert_fadt
248 * PARAMETERS: None, uses acpi_gbl_FADT
252 * DESCRIPTION: Converts all versions of the FADT to a common internal format.
254 * NOTE: acpi_gbl_FADT must be of size (struct acpi_table_fadt), and must contain
255 * a copy of the actual FADT.
257 * ACPICA will use the "X" fields of the FADT for all addresses.
259 * "X" fields are optional extensions to the original V1.0 fields. Even if
260 * they are present in the structure, they can be optionally not used by
261 * setting them to zero. Therefore, we must selectively expand V1.0 fields
262 * if the corresponding X field is zero.
264 * For ACPI 1.0 FADTs, all address fields are expanded to the corresponding
267 * For ACPI 2.0 FADTs, any "X" fields that are NULL are filled in by
268 * expanding the corresponding ACPI 1.0 field.
270 ******************************************************************************/
272 static void acpi_tb_convert_fadt(void)
274 u8 pm1_register_length;
275 struct acpi_generic_address *target;
278 /* Expand the FACS and DSDT addresses as necessary */
280 if (!acpi_gbl_FADT.Xfacs) {
281 acpi_gbl_FADT.Xfacs = (u64) acpi_gbl_FADT.facs;
284 if (!acpi_gbl_FADT.Xdsdt) {
285 acpi_gbl_FADT.Xdsdt = (u64) acpi_gbl_FADT.dsdt;
289 * Expand the 32-bit V1.0 addresses to the 64-bit "X" generic address
290 * structures as necessary.
292 for (i = 0; i < ACPI_FADT_CONVERSION_ENTRIES; i++) {
294 ACPI_ADD_PTR(struct acpi_generic_address, &acpi_gbl_FADT,
295 fadt_conversion_table[i].target);
297 /* Expand only if the X target is null */
299 if (!target->address) {
300 acpi_tb_init_generic_address(target,
303 fadt_conversion_table
305 (u64) * ACPI_ADD_PTR(u32,
307 fadt_conversion_table
314 * Calculate separate GAS structs for the PM1 Enable registers.
315 * These addresses do not appear (directly) in the FADT, so it is
316 * useful to calculate them once, here.
318 * The PM event blocks are split into two register blocks, first is the
319 * PM Status Register block, followed immediately by the PM Enable Register
320 * block. Each is of length (pm1_event_length/2)
322 pm1_register_length = (u8) ACPI_DIV_2(acpi_gbl_FADT.pm1_event_length);
324 /* PM1A is required */
326 acpi_tb_init_generic_address(&acpi_gbl_xpm1a_enable,
328 (acpi_gbl_FADT.xpm1a_event_block.address +
329 pm1_register_length));
331 /* PM1B is optional; leave null if not present */
333 if (acpi_gbl_FADT.xpm1b_event_block.address) {
334 acpi_tb_init_generic_address(&acpi_gbl_xpm1b_enable,
336 (acpi_gbl_FADT.xpm1b_event_block.
337 address + pm1_register_length));
340 /* Global FADT is the new common V2.0 FADT */
342 acpi_gbl_FADT.header.length = sizeof(struct acpi_table_fadt);
345 /*******************************************************************************
347 * FUNCTION: acpi_tb_install_table
349 * PARAMETERS: Address - Physical address of DSDT or FACS
351 * Signature - Table signature, NULL if no need to
353 * table_index - Index into root table array
357 * DESCRIPTION: Install an ACPI table into the global data structure.
359 ******************************************************************************/
362 acpi_tb_install_table(acpi_physical_address address,
363 u8 flags, char *signature, acpi_native_uint table_index)
365 struct acpi_table_header *table;
369 "Null physical address for ACPI table [%s]",
374 /* Map just the table header */
376 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
381 /* If a particular signature is expected, signature must match */
383 if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
385 "Invalid signature 0x%X for ACPI table [%s]",
386 *ACPI_CAST_PTR(u32, table->signature), signature));
390 /* Initialize the table entry */
392 acpi_gbl_root_table_list.tables[table_index].address = address;
393 acpi_gbl_root_table_list.tables[table_index].length = table->length;
394 acpi_gbl_root_table_list.tables[table_index].flags = flags;
397 (acpi_gbl_root_table_list.tables[table_index].
398 signature), table->signature);
400 acpi_tb_print_table_header(address, table);
402 if (table_index == ACPI_TABLE_INDEX_DSDT) {
404 /* Global integer width is based upon revision of the DSDT */
406 acpi_ut_set_integer_width(table->revision);
410 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
413 /*******************************************************************************
415 * FUNCTION: acpi_tb_parse_fadt
417 * PARAMETERS: table_index - Index for the FADT
422 * DESCRIPTION: Initialize the FADT, DSDT and FACS tables
423 * (FADT contains the addresses of the DSDT and FACS)
425 ******************************************************************************/
427 static void acpi_tb_parse_fadt(acpi_native_uint table_index, u8 flags)
430 struct acpi_table_header *table;
433 * Special case for the FADT because of multiple versions and the fact
434 * that it contains pointers to both the DSDT and FACS tables.
436 * Get a local copy of the FADT and convert it to a common format
437 * Map entire FADT, assumed to be smaller than one page.
439 length = acpi_gbl_root_table_list.tables[table_index].length;
442 acpi_os_map_memory(acpi_gbl_root_table_list.tables[table_index].
449 * Validate the FADT checksum before we copy the table. Ignore
450 * checksum error as we want to try to get the DSDT and FACS.
452 (void)acpi_tb_verify_checksum(table, length);
454 /* Copy the entire FADT locally */
456 ACPI_MEMSET(&acpi_gbl_FADT, 0, sizeof(struct acpi_table_fadt));
458 ACPI_MEMCPY(&acpi_gbl_FADT, table,
459 ACPI_MIN(length, sizeof(struct acpi_table_fadt)));
460 acpi_os_unmap_memory(table, length);
462 /* Convert local FADT to the common internal format */
464 acpi_tb_convert_fadt();
466 /* Extract the DSDT and FACS tables from the FADT */
468 acpi_tb_install_table((acpi_physical_address) acpi_gbl_FADT.Xdsdt,
469 flags, ACPI_SIG_DSDT, ACPI_TABLE_INDEX_DSDT);
471 acpi_tb_install_table((acpi_physical_address) acpi_gbl_FADT.Xfacs,
472 flags, ACPI_SIG_FACS, ACPI_TABLE_INDEX_FACS);
475 /*******************************************************************************
477 * FUNCTION: acpi_tb_get_root_table_entry
479 * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
480 * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
482 * RETURN: Physical address extracted from the root table
484 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
485 * both 32-bit and 64-bit platforms
487 * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
490 ******************************************************************************/
492 static acpi_physical_address
493 acpi_tb_get_root_table_entry(u8 * table_entry,
494 acpi_native_uint table_entry_size)
499 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
500 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
502 if (table_entry_size == sizeof(u32)) {
504 * 32-bit platform, RSDT: Return 32-bit table entry
505 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
507 return ((acpi_physical_address)
508 (*ACPI_CAST_PTR(u32, table_entry)));
511 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
512 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local, return 64-bit
514 ACPI_MOVE_64_TO_64(&address64, table_entry);
516 #if ACPI_MACHINE_WIDTH == 32
517 if (address64 > ACPI_UINT32_MAX) {
519 /* Will truncate 64-bit address to 32 bits */
521 ACPI_WARNING((AE_INFO,
522 "64-bit Physical Address in XSDT is too large (%8.8X%8.8X), truncating",
523 ACPI_FORMAT_UINT64(address64)));
526 return ((acpi_physical_address) (address64));
530 /*******************************************************************************
532 * FUNCTION: acpi_tb_parse_root_table
534 * PARAMETERS: Rsdp - Pointer to the RSDP
539 * DESCRIPTION: This function is called to parse the Root System Description
540 * Table (RSDT or XSDT)
542 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
543 * be mapped and cannot be copied because it contains the actual
544 * memory location of the ACPI Global Lock.
546 ******************************************************************************/
549 acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags)
551 struct acpi_table_rsdp *rsdp;
552 acpi_native_uint table_entry_size;
555 struct acpi_table_header *table;
556 acpi_physical_address address;
561 ACPI_FUNCTION_TRACE(tb_parse_root_table);
564 * Map the entire RSDP and extract the address of the RSDT or XSDT
566 rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
568 return_ACPI_STATUS(AE_NO_MEMORY);
571 acpi_tb_print_table_header(rsdp_address,
572 ACPI_CAST_PTR(struct acpi_table_header,
575 /* Differentiate between RSDT and XSDT root tables */
577 if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
579 * Root table is an XSDT (64-bit physical addresses). We must use the
580 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
581 * the ACPI specification.
583 address = (acpi_physical_address) rsdp->xsdt_physical_address;
584 table_entry_size = sizeof(u64);
586 /* Root table is an RSDT (32-bit physical addresses) */
588 address = (acpi_physical_address) rsdp->rsdt_physical_address;
589 table_entry_size = sizeof(u32);
593 * It is not possible to map more than one entry in some environments,
594 * so unmap the RSDP here before mapping other tables
596 acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
598 /* Map the RSDT/XSDT table header to get the full table length */
600 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
602 return_ACPI_STATUS(AE_NO_MEMORY);
605 acpi_tb_print_table_header(address, table);
607 /* Get the length of the full table, verify length and map entire table */
609 length = table->length;
610 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
612 if (length < sizeof(struct acpi_table_header)) {
613 ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
615 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
618 table = acpi_os_map_memory(address, length);
620 return_ACPI_STATUS(AE_NO_MEMORY);
623 /* Validate the root table checksum */
625 status = acpi_tb_verify_checksum(table, length);
626 if (ACPI_FAILURE(status)) {
627 acpi_os_unmap_memory(table, length);
628 return_ACPI_STATUS(status);
631 /* Calculate the number of tables described in the root table */
634 (u32) ((table->length -
635 sizeof(struct acpi_table_header)) / table_entry_size);
638 * First two entries in the table array are reserved for the DSDT and FACS,
639 * which are not actually present in the RSDT/XSDT - they come from the FADT
642 ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
643 acpi_gbl_root_table_list.count = 2;
646 * Initialize the root table array from the RSDT/XSDT
648 for (i = 0; i < table_count; i++) {
649 if (acpi_gbl_root_table_list.count >=
650 acpi_gbl_root_table_list.size) {
652 /* There is no more room in the root table array, attempt resize */
654 status = acpi_tb_resize_root_table_list();
655 if (ACPI_FAILURE(status)) {
656 ACPI_WARNING((AE_INFO,
657 "Truncating %u table entries!",
659 (acpi_gbl_root_table_list.size -
660 acpi_gbl_root_table_list.
666 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
668 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
670 acpi_tb_get_root_table_entry(table_entry, table_entry_size);
672 table_entry += table_entry_size;
673 acpi_gbl_root_table_list.count++;
677 * It is not possible to map more than one entry in some environments,
678 * so unmap the root table here before mapping other tables
680 acpi_os_unmap_memory(table, length);
683 * Complete the initialization of the root table array by examining
684 * the header of each table
686 for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
687 acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
688 address, flags, NULL, i);
690 /* Special case for FADT - get the DSDT and FACS */
692 if (ACPI_COMPARE_NAME
693 (&acpi_gbl_root_table_list.tables[i].signature,
695 acpi_tb_parse_fadt(i, flags);
699 return_ACPI_STATUS(AE_OK);
702 /******************************************************************************
704 * FUNCTION: acpi_tb_map
706 * PARAMETERS: Address - Address to be mapped
707 * Length - Length to be mapped
708 * Flags - Logical or physical addressing mode
710 * RETURN: Pointer to mapped region
712 * DESCRIPTION: Maps memory according to flag
714 *****************************************************************************/
716 void *acpi_tb_map(acpi_physical_address address, u32 length, u32 flags)
719 if (flags == ACPI_TABLE_ORIGIN_MAPPED) {
720 return (acpi_os_map_memory(address, length));
722 return (ACPI_CAST_PTR(void, address));
726 /******************************************************************************
728 * FUNCTION: acpi_tb_unmap
730 * PARAMETERS: Pointer - To mapped region
731 * Length - Length to be unmapped
732 * Flags - Logical or physical addressing mode
736 * DESCRIPTION: Unmaps memory according to flag
738 *****************************************************************************/
740 void acpi_tb_unmap(void *pointer, u32 length, u32 flags)
743 if (flags == ACPI_TABLE_ORIGIN_MAPPED) {
744 acpi_os_unmap_memory(pointer, length);