tbutils.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - ACPI Table utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, 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. #if (!ACPI_REDUCED_HARDWARE)
  104. /*******************************************************************************
  105. *
  106. * FUNCTION: acpi_tb_initialize_facs
  107. *
  108. * PARAMETERS: None
  109. *
  110. * RETURN: Status
  111. *
  112. * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
  113. * for accessing the Global Lock and Firmware Waking Vector
  114. *
  115. ******************************************************************************/
  116. acpi_status acpi_tb_initialize_facs(void)
  117. {
  118. acpi_status status;
  119. /* If Hardware Reduced flag is set, there is no FACS */
  120. if (acpi_gbl_reduced_hardware) {
  121. acpi_gbl_FACS = NULL;
  122. return (AE_OK);
  123. }
  124. status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
  125. ACPI_CAST_INDIRECT_PTR(struct
  126. acpi_table_header,
  127. &acpi_gbl_FACS));
  128. return (status);
  129. }
  130. #endif /* !ACPI_REDUCED_HARDWARE */
  131. /*******************************************************************************
  132. *
  133. * FUNCTION: acpi_tb_tables_loaded
  134. *
  135. * PARAMETERS: None
  136. *
  137. * RETURN: TRUE if required ACPI tables are loaded
  138. *
  139. * DESCRIPTION: Determine if the minimum required ACPI tables are present
  140. * (FADT, FACS, DSDT)
  141. *
  142. ******************************************************************************/
  143. u8 acpi_tb_tables_loaded(void)
  144. {
  145. if (acpi_gbl_root_table_list.current_table_count >= 3) {
  146. return (TRUE);
  147. }
  148. return (FALSE);
  149. }
  150. /*******************************************************************************
  151. *
  152. * FUNCTION: acpi_tb_check_dsdt_header
  153. *
  154. * PARAMETERS: None
  155. *
  156. * RETURN: None
  157. *
  158. * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
  159. * if the DSDT has been replaced from outside the OS and/or if
  160. * the DSDT header has been corrupted.
  161. *
  162. ******************************************************************************/
  163. void acpi_tb_check_dsdt_header(void)
  164. {
  165. /* Compare original length and checksum to current values */
  166. if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
  167. acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
  168. ACPI_BIOS_ERROR((AE_INFO,
  169. "The DSDT has been corrupted or replaced - "
  170. "old, new headers below"));
  171. acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
  172. acpi_tb_print_table_header(0, acpi_gbl_DSDT);
  173. ACPI_ERROR((AE_INFO,
  174. "Please send DMI info to linux-acpi@vger.kernel.org\n"
  175. "If system does not work as expected, please boot with acpi=copy_dsdt"));
  176. /* Disable further error messages */
  177. acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
  178. acpi_gbl_original_dsdt_header.checksum =
  179. acpi_gbl_DSDT->checksum;
  180. }
  181. }
  182. /*******************************************************************************
  183. *
  184. * FUNCTION: acpi_tb_copy_dsdt
  185. *
  186. * PARAMETERS: table_desc - Installed table to copy
  187. *
  188. * RETURN: None
  189. *
  190. * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
  191. * Some very bad BIOSs are known to either corrupt the DSDT or
  192. * install a new, bad DSDT. This copy works around the problem.
  193. *
  194. ******************************************************************************/
  195. struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  196. {
  197. struct acpi_table_header *new_table;
  198. struct acpi_table_desc *table_desc;
  199. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  200. new_table = ACPI_ALLOCATE(table_desc->length);
  201. if (!new_table) {
  202. ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
  203. table_desc->length));
  204. return (NULL);
  205. }
  206. ACPI_MEMCPY(new_table, table_desc->pointer, table_desc->length);
  207. acpi_tb_delete_table(table_desc);
  208. table_desc->pointer = new_table;
  209. table_desc->flags = ACPI_TABLE_ORIGIN_ALLOCATED;
  210. ACPI_INFO((AE_INFO,
  211. "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
  212. new_table->length));
  213. return (new_table);
  214. }
  215. /*******************************************************************************
  216. *
  217. * FUNCTION: acpi_tb_install_table
  218. *
  219. * PARAMETERS: address - Physical address of DSDT or FACS
  220. * signature - Table signature, NULL if no need to
  221. * match
  222. * table_index - Index into root table array
  223. *
  224. * RETURN: None
  225. *
  226. * DESCRIPTION: Install an ACPI table into the global data structure. The
  227. * table override mechanism is called to allow the host
  228. * OS to replace any table before it is installed in the root
  229. * table array.
  230. *
  231. ******************************************************************************/
  232. void
  233. acpi_tb_install_table(acpi_physical_address address,
  234. char *signature, u32 table_index)
  235. {
  236. struct acpi_table_header *table;
  237. struct acpi_table_header *final_table;
  238. struct acpi_table_desc *table_desc;
  239. if (!address) {
  240. ACPI_ERROR((AE_INFO,
  241. "Null physical address for ACPI table [%s]",
  242. signature));
  243. return;
  244. }
  245. /* Map just the table header */
  246. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  247. if (!table) {
  248. ACPI_ERROR((AE_INFO,
  249. "Could not map memory for table [%s] at %p",
  250. signature, ACPI_CAST_PTR(void, address)));
  251. return;
  252. }
  253. /* If a particular signature is expected (DSDT/FACS), it must match */
  254. if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
  255. ACPI_BIOS_ERROR((AE_INFO,
  256. "Invalid signature 0x%X for ACPI table, expected [%s]",
  257. *ACPI_CAST_PTR(u32, table->signature),
  258. signature));
  259. goto unmap_and_exit;
  260. }
  261. /*
  262. * Initialize the table entry. Set the pointer to NULL, since the
  263. * table is not fully mapped at this time.
  264. */
  265. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  266. table_desc->address = address;
  267. table_desc->pointer = NULL;
  268. table_desc->length = table->length;
  269. table_desc->flags = ACPI_TABLE_ORIGIN_MAPPED;
  270. ACPI_MOVE_32_TO_32(table_desc->signature.ascii, table->signature);
  271. /*
  272. * ACPI Table Override:
  273. *
  274. * Before we install the table, let the host OS override it with a new
  275. * one if desired. Any table within the RSDT/XSDT can be replaced,
  276. * including the DSDT which is pointed to by the FADT.
  277. *
  278. * NOTE: If the table is overridden, then final_table will contain a
  279. * mapped pointer to the full new table. If the table is not overridden,
  280. * or if there has been a physical override, then the table will be
  281. * fully mapped later (in verify table). In any case, we must
  282. * unmap the header that was mapped above.
  283. */
  284. final_table = acpi_tb_table_override(table, table_desc);
  285. if (!final_table) {
  286. final_table = table; /* There was no override */
  287. }
  288. acpi_tb_print_table_header(table_desc->address, final_table);
  289. /* Set the global integer width (based upon revision of the DSDT) */
  290. if (table_index == ACPI_TABLE_INDEX_DSDT) {
  291. acpi_ut_set_integer_width(final_table->revision);
  292. }
  293. /*
  294. * If we have a physical override during this early loading of the ACPI
  295. * tables, unmap the table for now. It will be mapped again later when
  296. * it is actually used. This supports very early loading of ACPI tables,
  297. * before virtual memory is fully initialized and running within the
  298. * host OS. Note: A logical override has the ACPI_TABLE_ORIGIN_OVERRIDE
  299. * flag set and will not be deleted below.
  300. */
  301. if (final_table != table) {
  302. acpi_tb_delete_table(table_desc);
  303. }
  304. unmap_and_exit:
  305. /* Always unmap the table header that we mapped above */
  306. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  307. }
  308. /*******************************************************************************
  309. *
  310. * FUNCTION: acpi_tb_get_root_table_entry
  311. *
  312. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  313. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  314. *
  315. * RETURN: Physical address extracted from the root table
  316. *
  317. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  318. * both 32-bit and 64-bit platforms
  319. *
  320. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  321. * 64-bit platforms.
  322. *
  323. ******************************************************************************/
  324. static acpi_physical_address
  325. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
  326. {
  327. u64 address64;
  328. /*
  329. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  330. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  331. */
  332. if (table_entry_size == sizeof(u32)) {
  333. /*
  334. * 32-bit platform, RSDT: Return 32-bit table entry
  335. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  336. */
  337. return ((acpi_physical_address)
  338. (*ACPI_CAST_PTR(u32, table_entry)));
  339. } else {
  340. /*
  341. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  342. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
  343. * return 64-bit
  344. */
  345. ACPI_MOVE_64_TO_64(&address64, table_entry);
  346. #if ACPI_MACHINE_WIDTH == 32
  347. if (address64 > ACPI_UINT32_MAX) {
  348. /* Will truncate 64-bit address to 32 bits, issue warning */
  349. ACPI_BIOS_WARNING((AE_INFO,
  350. "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
  351. " truncating",
  352. ACPI_FORMAT_UINT64(address64)));
  353. }
  354. #endif
  355. return ((acpi_physical_address) (address64));
  356. }
  357. }
  358. /*******************************************************************************
  359. *
  360. * FUNCTION: acpi_tb_parse_root_table
  361. *
  362. * PARAMETERS: rsdp - Pointer to the RSDP
  363. *
  364. * RETURN: Status
  365. *
  366. * DESCRIPTION: This function is called to parse the Root System Description
  367. * Table (RSDT or XSDT)
  368. *
  369. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  370. * be mapped and cannot be copied because it contains the actual
  371. * memory location of the ACPI Global Lock.
  372. *
  373. ******************************************************************************/
  374. acpi_status __init
  375. acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  376. {
  377. struct acpi_table_rsdp *rsdp;
  378. u32 table_entry_size;
  379. u32 i;
  380. u32 table_count;
  381. struct acpi_table_header *table;
  382. acpi_physical_address address;
  383. acpi_physical_address uninitialized_var(rsdt_address);
  384. u32 length;
  385. u8 *table_entry;
  386. acpi_status status;
  387. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  388. /*
  389. * Map the entire RSDP and extract the address of the RSDT or XSDT
  390. */
  391. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  392. if (!rsdp) {
  393. return_ACPI_STATUS(AE_NO_MEMORY);
  394. }
  395. acpi_tb_print_table_header(rsdp_address,
  396. ACPI_CAST_PTR(struct acpi_table_header,
  397. rsdp));
  398. /* Differentiate between RSDT and XSDT root tables */
  399. if (rsdp->revision > 1 && rsdp->xsdt_physical_address
  400. && !acpi_rsdt_forced) {
  401. /*
  402. * Root table is an XSDT (64-bit physical addresses). We must use the
  403. * XSDT if the revision is > 1 and the XSDT pointer is present, as per
  404. * the ACPI specification.
  405. */
  406. address = (acpi_physical_address) rsdp->xsdt_physical_address;
  407. table_entry_size = sizeof(u64);
  408. rsdt_address = (acpi_physical_address)
  409. rsdp->rsdt_physical_address;
  410. } else {
  411. /* Root table is an RSDT (32-bit physical addresses) */
  412. address = (acpi_physical_address) rsdp->rsdt_physical_address;
  413. table_entry_size = sizeof(u32);
  414. }
  415. /*
  416. * It is not possible to map more than one entry in some environments,
  417. * so unmap the RSDP here before mapping other tables
  418. */
  419. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  420. if (table_entry_size == sizeof(u64)) {
  421. if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
  422. /* XSDT has NULL entry, RSDT is used */
  423. address = rsdt_address;
  424. table_entry_size = sizeof(u32);
  425. ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
  426. "using RSDT"));
  427. }
  428. }
  429. /* Map the RSDT/XSDT table header to get the full table length */
  430. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  431. if (!table) {
  432. return_ACPI_STATUS(AE_NO_MEMORY);
  433. }
  434. acpi_tb_print_table_header(address, table);
  435. /* Get the length of the full table, verify length and map entire table */
  436. length = table->length;
  437. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  438. if (length < sizeof(struct acpi_table_header)) {
  439. ACPI_BIOS_ERROR((AE_INFO,
  440. "Invalid table length 0x%X in RSDT/XSDT",
  441. length));
  442. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  443. }
  444. table = acpi_os_map_memory(address, length);
  445. if (!table) {
  446. return_ACPI_STATUS(AE_NO_MEMORY);
  447. }
  448. /* Validate the root table checksum */
  449. status = acpi_tb_verify_checksum(table, length);
  450. if (ACPI_FAILURE(status)) {
  451. acpi_os_unmap_memory(table, length);
  452. return_ACPI_STATUS(status);
  453. }
  454. /* Calculate the number of tables described in the root table */
  455. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  456. table_entry_size);
  457. /*
  458. * First two entries in the table array are reserved for the DSDT
  459. * and FACS, which are not actually present in the RSDT/XSDT - they
  460. * come from the FADT
  461. */
  462. table_entry =
  463. ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
  464. acpi_gbl_root_table_list.current_table_count = 2;
  465. /*
  466. * Initialize the root table array from the RSDT/XSDT
  467. */
  468. for (i = 0; i < table_count; i++) {
  469. if (acpi_gbl_root_table_list.current_table_count >=
  470. acpi_gbl_root_table_list.max_table_count) {
  471. /* There is no more room in the root table array, attempt resize */
  472. status = acpi_tb_resize_root_table_list();
  473. if (ACPI_FAILURE(status)) {
  474. ACPI_WARNING((AE_INFO,
  475. "Truncating %u table entries!",
  476. (unsigned) (table_count -
  477. (acpi_gbl_root_table_list.
  478. current_table_count -
  479. 2))));
  480. break;
  481. }
  482. }
  483. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  484. acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.
  485. current_table_count].address =
  486. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  487. table_entry += table_entry_size;
  488. acpi_gbl_root_table_list.current_table_count++;
  489. }
  490. /*
  491. * It is not possible to map more than one entry in some environments,
  492. * so unmap the root table here before mapping other tables
  493. */
  494. acpi_os_unmap_memory(table, length);
  495. /*
  496. * Complete the initialization of the root table array by examining
  497. * the header of each table
  498. */
  499. for (i = 2; i < acpi_gbl_root_table_list.current_table_count; i++) {
  500. acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
  501. address, NULL, i);
  502. /* Special case for FADT - get the DSDT and FACS */
  503. if (ACPI_COMPARE_NAME
  504. (&acpi_gbl_root_table_list.tables[i].signature,
  505. ACPI_SIG_FADT)) {
  506. acpi_tb_parse_fadt(i);
  507. }
  508. }
  509. return_ACPI_STATUS(AE_OK);
  510. }