tbutils.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - table utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "actables.h"
  45. #define _COMPONENT ACPI_TABLES
  46. ACPI_MODULE_NAME("tbutils")
  47. /* Local prototypes */
  48. static acpi_physical_address
  49. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_tb_check_xsdt
  53. *
  54. * PARAMETERS: address - Pointer to the XSDT
  55. *
  56. * RETURN: status
  57. * AE_OK - XSDT is okay
  58. * AE_NO_MEMORY - can't map XSDT
  59. * AE_INVALID_TABLE_LENGTH - invalid table length
  60. * AE_NULL_ENTRY - XSDT has NULL entry
  61. *
  62. * DESCRIPTION: validate XSDT
  63. ******************************************************************************/
  64. static acpi_status
  65. acpi_tb_check_xsdt(acpi_physical_address address)
  66. {
  67. struct acpi_table_header *table;
  68. u32 length;
  69. u64 xsdt_entry_address;
  70. u8 *table_entry;
  71. u32 table_count;
  72. int i;
  73. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  74. if (!table)
  75. return AE_NO_MEMORY;
  76. length = table->length;
  77. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  78. if (length < sizeof(struct acpi_table_header))
  79. return AE_INVALID_TABLE_LENGTH;
  80. table = acpi_os_map_memory(address, length);
  81. if (!table)
  82. return AE_NO_MEMORY;
  83. /* Calculate the number of tables described in XSDT */
  84. table_count =
  85. (u32) ((table->length -
  86. sizeof(struct acpi_table_header)) / sizeof(u64));
  87. table_entry =
  88. ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
  89. for (i = 0; i < table_count; i++) {
  90. ACPI_MOVE_64_TO_64(&xsdt_entry_address, table_entry);
  91. if (!xsdt_entry_address) {
  92. /* XSDT has NULL entry */
  93. break;
  94. }
  95. table_entry += sizeof(u64);
  96. }
  97. acpi_os_unmap_memory(table, length);
  98. if (i < table_count)
  99. return AE_NULL_ENTRY;
  100. else
  101. return AE_OK;
  102. }
  103. /*******************************************************************************
  104. *
  105. * FUNCTION: acpi_tb_initialize_facs
  106. *
  107. * PARAMETERS: None
  108. *
  109. * RETURN: Status
  110. *
  111. * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
  112. * for accessing the Global Lock and Firmware Waking Vector
  113. *
  114. ******************************************************************************/
  115. acpi_status acpi_tb_initialize_facs(void)
  116. {
  117. acpi_status status;
  118. status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
  119. ACPI_CAST_INDIRECT_PTR(struct
  120. acpi_table_header,
  121. &acpi_gbl_FACS));
  122. return status;
  123. }
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_tb_tables_loaded
  127. *
  128. * PARAMETERS: None
  129. *
  130. * RETURN: TRUE if required ACPI tables are loaded
  131. *
  132. * DESCRIPTION: Determine if the minimum required ACPI tables are present
  133. * (FADT, FACS, DSDT)
  134. *
  135. ******************************************************************************/
  136. u8 acpi_tb_tables_loaded(void)
  137. {
  138. if (acpi_gbl_root_table_list.count >= 3) {
  139. return (TRUE);
  140. }
  141. return (FALSE);
  142. }
  143. /*******************************************************************************
  144. *
  145. * FUNCTION: acpi_tb_print_table_header
  146. *
  147. * PARAMETERS: Address - Table physical address
  148. * Header - Table header
  149. *
  150. * RETURN: None
  151. *
  152. * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
  153. *
  154. ******************************************************************************/
  155. void
  156. acpi_tb_print_table_header(acpi_physical_address address,
  157. struct acpi_table_header *header)
  158. {
  159. /*
  160. * The reason that the Address is cast to a void pointer is so that we
  161. * can use %p which will work properly on both 32-bit and 64-bit hosts.
  162. */
  163. if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
  164. /* FACS only has signature and length fields */
  165. ACPI_INFO((AE_INFO, "%4.4s %p %05X",
  166. header->signature, ACPI_CAST_PTR(void, address),
  167. header->length));
  168. } else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
  169. /* RSDP has no common fields */
  170. ACPI_INFO((AE_INFO, "RSDP %p %05X (v%.2d %6.6s)",
  171. ACPI_CAST_PTR (void, address),
  172. (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
  173. revision >
  174. 0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
  175. header)->length : 20,
  176. ACPI_CAST_PTR(struct acpi_table_rsdp,
  177. header)->revision,
  178. ACPI_CAST_PTR(struct acpi_table_rsdp,
  179. header)->oem_id));
  180. } else {
  181. /* Standard ACPI table with full common header */
  182. ACPI_INFO((AE_INFO,
  183. "%4.4s %p %05X (v%.2d %6.6s %8.8s %08X %4.4s %08X)",
  184. header->signature, ACPI_CAST_PTR (void, address),
  185. header->length, header->revision, header->oem_id,
  186. header->oem_table_id, header->oem_revision,
  187. header->asl_compiler_id,
  188. header->asl_compiler_revision));
  189. }
  190. }
  191. /*******************************************************************************
  192. *
  193. * FUNCTION: acpi_tb_validate_checksum
  194. *
  195. * PARAMETERS: Table - ACPI table to verify
  196. * Length - Length of entire table
  197. *
  198. * RETURN: Status
  199. *
  200. * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
  201. * exception on bad checksum.
  202. *
  203. ******************************************************************************/
  204. acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
  205. {
  206. u8 checksum;
  207. /* Compute the checksum on the table */
  208. checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
  209. /* Checksum ok? (should be zero) */
  210. if (checksum) {
  211. ACPI_WARNING((AE_INFO,
  212. "Incorrect checksum in table [%4.4s] - %2.2X, should be %2.2X",
  213. table->signature, table->checksum,
  214. (u8) (table->checksum - checksum)));
  215. #if (ACPI_CHECKSUM_ABORT)
  216. return (AE_BAD_CHECKSUM);
  217. #endif
  218. }
  219. return (AE_OK);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: acpi_tb_checksum
  224. *
  225. * PARAMETERS: Buffer - Pointer to memory region to be checked
  226. * Length - Length of this memory region
  227. *
  228. * RETURN: Checksum (u8)
  229. *
  230. * DESCRIPTION: Calculates circular checksum of memory region.
  231. *
  232. ******************************************************************************/
  233. u8 acpi_tb_checksum(u8 *buffer, u32 length)
  234. {
  235. u8 sum = 0;
  236. u8 *end = buffer + length;
  237. while (buffer < end) {
  238. sum = (u8) (sum + *(buffer++));
  239. }
  240. return sum;
  241. }
  242. /*******************************************************************************
  243. *
  244. * FUNCTION: acpi_tb_install_table
  245. *
  246. * PARAMETERS: Address - Physical address of DSDT or FACS
  247. * Signature - Table signature, NULL if no need to
  248. * match
  249. * table_index - Index into root table array
  250. *
  251. * RETURN: None
  252. *
  253. * DESCRIPTION: Install an ACPI table into the global data structure. The
  254. * table override mechanism is implemented here to allow the host
  255. * OS to replace any table before it is installed in the root
  256. * table array.
  257. *
  258. ******************************************************************************/
  259. void
  260. acpi_tb_install_table(acpi_physical_address address,
  261. char *signature, u32 table_index)
  262. {
  263. u8 flags;
  264. acpi_status status;
  265. struct acpi_table_header *table_to_install;
  266. struct acpi_table_header *mapped_table;
  267. struct acpi_table_header *override_table = NULL;
  268. if (!address) {
  269. ACPI_ERROR((AE_INFO,
  270. "Null physical address for ACPI table [%s]",
  271. signature));
  272. return;
  273. }
  274. /* Map just the table header */
  275. mapped_table =
  276. acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  277. if (!mapped_table) {
  278. return;
  279. }
  280. /* If a particular signature is expected (DSDT/FACS), it must match */
  281. if (signature && !ACPI_COMPARE_NAME(mapped_table->signature, signature)) {
  282. ACPI_ERROR((AE_INFO,
  283. "Invalid signature 0x%X for ACPI table, expected [%s]",
  284. *ACPI_CAST_PTR(u32, mapped_table->signature),
  285. signature));
  286. goto unmap_and_exit;
  287. }
  288. /*
  289. * ACPI Table Override:
  290. *
  291. * Before we install the table, let the host OS override it with a new
  292. * one if desired. Any table within the RSDT/XSDT can be replaced,
  293. * including the DSDT which is pointed to by the FADT.
  294. */
  295. status = acpi_os_table_override(mapped_table, &override_table);
  296. if (ACPI_SUCCESS(status) && override_table) {
  297. ACPI_INFO((AE_INFO,
  298. "%4.4s @ 0x%p Table override, replaced with:",
  299. mapped_table->signature, ACPI_CAST_PTR(void,
  300. address)));
  301. acpi_gbl_root_table_list.tables[table_index].pointer =
  302. override_table;
  303. address = ACPI_PTR_TO_PHYSADDR(override_table);
  304. table_to_install = override_table;
  305. flags = ACPI_TABLE_ORIGIN_OVERRIDE;
  306. } else {
  307. table_to_install = mapped_table;
  308. flags = ACPI_TABLE_ORIGIN_MAPPED;
  309. }
  310. /* Initialize the table entry */
  311. acpi_gbl_root_table_list.tables[table_index].address = address;
  312. acpi_gbl_root_table_list.tables[table_index].length =
  313. table_to_install->length;
  314. acpi_gbl_root_table_list.tables[table_index].flags = flags;
  315. ACPI_MOVE_32_TO_32(&
  316. (acpi_gbl_root_table_list.tables[table_index].
  317. signature), table_to_install->signature);
  318. acpi_tb_print_table_header(address, table_to_install);
  319. if (table_index == ACPI_TABLE_INDEX_DSDT) {
  320. /* Global integer width is based upon revision of the DSDT */
  321. acpi_ut_set_integer_width(table_to_install->revision);
  322. }
  323. unmap_and_exit:
  324. acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
  325. }
  326. /*******************************************************************************
  327. *
  328. * FUNCTION: acpi_tb_get_root_table_entry
  329. *
  330. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  331. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  332. *
  333. * RETURN: Physical address extracted from the root table
  334. *
  335. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  336. * both 32-bit and 64-bit platforms
  337. *
  338. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  339. * 64-bit platforms.
  340. *
  341. ******************************************************************************/
  342. static acpi_physical_address
  343. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
  344. {
  345. u64 address64;
  346. /*
  347. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  348. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  349. */
  350. if (table_entry_size == sizeof(u32)) {
  351. /*
  352. * 32-bit platform, RSDT: Return 32-bit table entry
  353. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  354. */
  355. return ((acpi_physical_address)
  356. (*ACPI_CAST_PTR(u32, table_entry)));
  357. } else {
  358. /*
  359. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  360. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
  361. * return 64-bit
  362. */
  363. ACPI_MOVE_64_TO_64(&address64, table_entry);
  364. #if ACPI_MACHINE_WIDTH == 32
  365. if (address64 > ACPI_UINT32_MAX) {
  366. /* Will truncate 64-bit address to 32 bits, issue warning */
  367. ACPI_WARNING((AE_INFO,
  368. "64-bit Physical Address in XSDT is too large (%8.8X%8.8X),"
  369. " truncating",
  370. ACPI_FORMAT_UINT64(address64)));
  371. }
  372. #endif
  373. return ((acpi_physical_address) (address64));
  374. }
  375. }
  376. /*******************************************************************************
  377. *
  378. * FUNCTION: acpi_tb_parse_root_table
  379. *
  380. * PARAMETERS: Rsdp - Pointer to the RSDP
  381. *
  382. * RETURN: Status
  383. *
  384. * DESCRIPTION: This function is called to parse the Root System Description
  385. * Table (RSDT or XSDT)
  386. *
  387. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  388. * be mapped and cannot be copied because it contains the actual
  389. * memory location of the ACPI Global Lock.
  390. *
  391. ******************************************************************************/
  392. acpi_status __init
  393. acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  394. {
  395. struct acpi_table_rsdp *rsdp;
  396. u32 table_entry_size;
  397. u32 i;
  398. u32 table_count;
  399. struct acpi_table_header *table;
  400. acpi_physical_address address;
  401. acpi_physical_address uninitialized_var(rsdt_address);
  402. u32 length;
  403. u8 *table_entry;
  404. acpi_status status;
  405. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  406. /*
  407. * Map the entire RSDP and extract the address of the RSDT or XSDT
  408. */
  409. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  410. if (!rsdp) {
  411. return_ACPI_STATUS(AE_NO_MEMORY);
  412. }
  413. acpi_tb_print_table_header(rsdp_address,
  414. ACPI_CAST_PTR(struct acpi_table_header,
  415. rsdp));
  416. /* Differentiate between RSDT and XSDT root tables */
  417. if (rsdp->revision > 1 && rsdp->xsdt_physical_address
  418. && !acpi_rsdt_forced) {
  419. /*
  420. * Root table is an XSDT (64-bit physical addresses). We must use the
  421. * XSDT if the revision is > 1 and the XSDT pointer is present, as per
  422. * the ACPI specification.
  423. */
  424. address = (acpi_physical_address) rsdp->xsdt_physical_address;
  425. table_entry_size = sizeof(u64);
  426. rsdt_address = (acpi_physical_address)
  427. rsdp->rsdt_physical_address;
  428. } else {
  429. /* Root table is an RSDT (32-bit physical addresses) */
  430. address = (acpi_physical_address) rsdp->rsdt_physical_address;
  431. table_entry_size = sizeof(u32);
  432. }
  433. /*
  434. * It is not possible to map more than one entry in some environments,
  435. * so unmap the RSDP here before mapping other tables
  436. */
  437. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  438. if (table_entry_size == sizeof(u64)) {
  439. if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
  440. /* XSDT has NULL entry, RSDT is used */
  441. address = rsdt_address;
  442. table_entry_size = sizeof(u32);
  443. ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
  444. "using RSDT"));
  445. }
  446. }
  447. /* Map the RSDT/XSDT table header to get the full table length */
  448. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  449. if (!table) {
  450. return_ACPI_STATUS(AE_NO_MEMORY);
  451. }
  452. acpi_tb_print_table_header(address, table);
  453. /* Get the length of the full table, verify length and map entire table */
  454. length = table->length;
  455. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  456. if (length < sizeof(struct acpi_table_header)) {
  457. ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
  458. length));
  459. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  460. }
  461. table = acpi_os_map_memory(address, length);
  462. if (!table) {
  463. return_ACPI_STATUS(AE_NO_MEMORY);
  464. }
  465. /* Validate the root table checksum */
  466. status = acpi_tb_verify_checksum(table, length);
  467. if (ACPI_FAILURE(status)) {
  468. acpi_os_unmap_memory(table, length);
  469. return_ACPI_STATUS(status);
  470. }
  471. /* Calculate the number of tables described in the root table */
  472. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  473. table_entry_size);
  474. /*
  475. * First two entries in the table array are reserved for the DSDT
  476. * and FACS, which are not actually present in the RSDT/XSDT - they
  477. * come from the FADT
  478. */
  479. table_entry =
  480. ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
  481. acpi_gbl_root_table_list.count = 2;
  482. /*
  483. * Initialize the root table array from the RSDT/XSDT
  484. */
  485. for (i = 0; i < table_count; i++) {
  486. if (acpi_gbl_root_table_list.count >=
  487. acpi_gbl_root_table_list.size) {
  488. /* There is no more room in the root table array, attempt resize */
  489. status = acpi_tb_resize_root_table_list();
  490. if (ACPI_FAILURE(status)) {
  491. ACPI_WARNING((AE_INFO,
  492. "Truncating %u table entries!",
  493. (unsigned) (table_count -
  494. (acpi_gbl_root_table_list.
  495. count - 2))));
  496. break;
  497. }
  498. }
  499. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  500. acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
  501. address =
  502. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  503. table_entry += table_entry_size;
  504. acpi_gbl_root_table_list.count++;
  505. }
  506. /*
  507. * It is not possible to map more than one entry in some environments,
  508. * so unmap the root table here before mapping other tables
  509. */
  510. acpi_os_unmap_memory(table, length);
  511. /*
  512. * Complete the initialization of the root table array by examining
  513. * the header of each table
  514. */
  515. for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
  516. acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
  517. address, NULL, i);
  518. /* Special case for FADT - get the DSDT and FACS */
  519. if (ACPI_COMPARE_NAME
  520. (&acpi_gbl_root_table_list.tables[i].signature,
  521. ACPI_SIG_FADT)) {
  522. acpi_tb_parse_fadt(i);
  523. }
  524. }
  525. return_ACPI_STATUS(AE_OK);
  526. }