tbutils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - ACPI Table utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, 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_status acpi_tb_validate_xsdt(acpi_physical_address address);
  49. static acpi_physical_address
  50. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
  51. #if (!ACPI_REDUCED_HARDWARE)
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_tb_initialize_facs
  55. *
  56. * PARAMETERS: None
  57. *
  58. * RETURN: Status
  59. *
  60. * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
  61. * for accessing the Global Lock and Firmware Waking Vector
  62. *
  63. ******************************************************************************/
  64. acpi_status acpi_tb_initialize_facs(void)
  65. {
  66. acpi_status status;
  67. /* If Hardware Reduced flag is set, there is no FACS */
  68. if (acpi_gbl_reduced_hardware) {
  69. acpi_gbl_FACS = NULL;
  70. return (AE_OK);
  71. }
  72. status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
  73. ACPI_CAST_INDIRECT_PTR(struct
  74. acpi_table_header,
  75. &acpi_gbl_FACS));
  76. return (status);
  77. }
  78. #endif /* !ACPI_REDUCED_HARDWARE */
  79. /*******************************************************************************
  80. *
  81. * FUNCTION: acpi_tb_tables_loaded
  82. *
  83. * PARAMETERS: None
  84. *
  85. * RETURN: TRUE if required ACPI tables are loaded
  86. *
  87. * DESCRIPTION: Determine if the minimum required ACPI tables are present
  88. * (FADT, FACS, DSDT)
  89. *
  90. ******************************************************************************/
  91. u8 acpi_tb_tables_loaded(void)
  92. {
  93. if (acpi_gbl_root_table_list.current_table_count >= 3) {
  94. return (TRUE);
  95. }
  96. return (FALSE);
  97. }
  98. /*******************************************************************************
  99. *
  100. * FUNCTION: acpi_tb_check_dsdt_header
  101. *
  102. * PARAMETERS: None
  103. *
  104. * RETURN: None
  105. *
  106. * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
  107. * if the DSDT has been replaced from outside the OS and/or if
  108. * the DSDT header has been corrupted.
  109. *
  110. ******************************************************************************/
  111. void acpi_tb_check_dsdt_header(void)
  112. {
  113. /* Compare original length and checksum to current values */
  114. if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
  115. acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
  116. ACPI_BIOS_ERROR((AE_INFO,
  117. "The DSDT has been corrupted or replaced - "
  118. "old, new headers below"));
  119. acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
  120. acpi_tb_print_table_header(0, acpi_gbl_DSDT);
  121. ACPI_ERROR((AE_INFO,
  122. "Please send DMI info to linux-acpi@vger.kernel.org\n"
  123. "If system does not work as expected, please boot with acpi=copy_dsdt"));
  124. /* Disable further error messages */
  125. acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
  126. acpi_gbl_original_dsdt_header.checksum =
  127. acpi_gbl_DSDT->checksum;
  128. }
  129. }
  130. /*******************************************************************************
  131. *
  132. * FUNCTION: acpi_tb_copy_dsdt
  133. *
  134. * PARAMETERS: table_desc - Installed table to copy
  135. *
  136. * RETURN: None
  137. *
  138. * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
  139. * Some very bad BIOSs are known to either corrupt the DSDT or
  140. * install a new, bad DSDT. This copy works around the problem.
  141. *
  142. ******************************************************************************/
  143. struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  144. {
  145. struct acpi_table_header *new_table;
  146. struct acpi_table_desc *table_desc;
  147. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  148. new_table = ACPI_ALLOCATE(table_desc->length);
  149. if (!new_table) {
  150. ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
  151. table_desc->length));
  152. return (NULL);
  153. }
  154. ACPI_MEMCPY(new_table, table_desc->pointer, table_desc->length);
  155. acpi_tb_uninstall_table(table_desc);
  156. acpi_tb_install_table(&acpi_gbl_root_table_list.
  157. tables[ACPI_TABLE_INDEX_DSDT],
  158. ACPI_PTR_TO_PHYSADDR(new_table),
  159. ACPI_TABLE_ORIGIN_INTERN_VIRTUAL, new_table);
  160. ACPI_INFO((AE_INFO,
  161. "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
  162. new_table->length));
  163. return (new_table);
  164. }
  165. /*******************************************************************************
  166. *
  167. * FUNCTION: acpi_tb_get_root_table_entry
  168. *
  169. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  170. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  171. *
  172. * RETURN: Physical address extracted from the root table
  173. *
  174. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  175. * both 32-bit and 64-bit platforms
  176. *
  177. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  178. * 64-bit platforms.
  179. *
  180. ******************************************************************************/
  181. static acpi_physical_address
  182. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
  183. {
  184. u64 address64;
  185. /*
  186. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  187. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  188. */
  189. if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
  190. /*
  191. * 32-bit platform, RSDT: Return 32-bit table entry
  192. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  193. */
  194. return ((acpi_physical_address)
  195. (*ACPI_CAST_PTR(u32, table_entry)));
  196. } else {
  197. /*
  198. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  199. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
  200. * return 64-bit
  201. */
  202. ACPI_MOVE_64_TO_64(&address64, table_entry);
  203. #if ACPI_MACHINE_WIDTH == 32
  204. if (address64 > ACPI_UINT32_MAX) {
  205. /* Will truncate 64-bit address to 32 bits, issue warning */
  206. ACPI_BIOS_WARNING((AE_INFO,
  207. "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
  208. " truncating",
  209. ACPI_FORMAT_UINT64(address64)));
  210. }
  211. #endif
  212. return ((acpi_physical_address) (address64));
  213. }
  214. }
  215. /*******************************************************************************
  216. *
  217. * FUNCTION: acpi_tb_validate_xsdt
  218. *
  219. * PARAMETERS: address - Physical address of the XSDT (from RSDP)
  220. *
  221. * RETURN: Status. AE_OK if the table appears to be valid.
  222. *
  223. * DESCRIPTION: Validate an XSDT to ensure that it is of minimum size and does
  224. * not contain any NULL entries. A problem that is seen in the
  225. * field is that the XSDT exists, but is actually useless because
  226. * of one or more (or all) NULL entries.
  227. *
  228. ******************************************************************************/
  229. static acpi_status acpi_tb_validate_xsdt(acpi_physical_address xsdt_address)
  230. {
  231. struct acpi_table_header *table;
  232. u8 *next_entry;
  233. acpi_physical_address address;
  234. u32 length;
  235. u32 entry_count;
  236. acpi_status status;
  237. u32 i;
  238. /* Get the XSDT length */
  239. table =
  240. acpi_os_map_memory(xsdt_address, sizeof(struct acpi_table_header));
  241. if (!table) {
  242. return (AE_NO_MEMORY);
  243. }
  244. length = table->length;
  245. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  246. /*
  247. * Minimum XSDT length is the size of the standard ACPI header
  248. * plus one physical address entry
  249. */
  250. if (length < (sizeof(struct acpi_table_header) + ACPI_XSDT_ENTRY_SIZE)) {
  251. return (AE_INVALID_TABLE_LENGTH);
  252. }
  253. /* Map the entire XSDT */
  254. table = acpi_os_map_memory(xsdt_address, length);
  255. if (!table) {
  256. return (AE_NO_MEMORY);
  257. }
  258. /* Get the number of entries and pointer to first entry */
  259. status = AE_OK;
  260. next_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
  261. entry_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  262. ACPI_XSDT_ENTRY_SIZE);
  263. /* Validate each entry (physical address) within the XSDT */
  264. for (i = 0; i < entry_count; i++) {
  265. address =
  266. acpi_tb_get_root_table_entry(next_entry,
  267. ACPI_XSDT_ENTRY_SIZE);
  268. if (!address) {
  269. /* Detected a NULL entry, XSDT is invalid */
  270. status = AE_NULL_ENTRY;
  271. break;
  272. }
  273. next_entry += ACPI_XSDT_ENTRY_SIZE;
  274. }
  275. /* Unmap table */
  276. acpi_os_unmap_memory(table, length);
  277. return (status);
  278. }
  279. /*******************************************************************************
  280. *
  281. * FUNCTION: acpi_tb_parse_root_table
  282. *
  283. * PARAMETERS: rsdp - Pointer to the RSDP
  284. *
  285. * RETURN: Status
  286. *
  287. * DESCRIPTION: This function is called to parse the Root System Description
  288. * Table (RSDT or XSDT)
  289. *
  290. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  291. * be mapped and cannot be copied because it contains the actual
  292. * memory location of the ACPI Global Lock.
  293. *
  294. ******************************************************************************/
  295. acpi_status __init acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  296. {
  297. struct acpi_table_rsdp *rsdp;
  298. u32 table_entry_size;
  299. u32 i;
  300. u32 table_count;
  301. struct acpi_table_header *table;
  302. acpi_physical_address address;
  303. u32 length;
  304. u8 *table_entry;
  305. acpi_status status;
  306. u32 table_index;
  307. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  308. /* Map the entire RSDP and extract the address of the RSDT or XSDT */
  309. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  310. if (!rsdp) {
  311. return_ACPI_STATUS(AE_NO_MEMORY);
  312. }
  313. acpi_tb_print_table_header(rsdp_address,
  314. ACPI_CAST_PTR(struct acpi_table_header,
  315. rsdp));
  316. /* Use XSDT if present and not overridden. Otherwise, use RSDT */
  317. if ((rsdp->revision > 1) &&
  318. rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
  319. /*
  320. * RSDP contains an XSDT (64-bit physical addresses). We must use
  321. * the XSDT if the revision is > 1 and the XSDT pointer is present,
  322. * as per the ACPI specification.
  323. */
  324. address = (acpi_physical_address) rsdp->xsdt_physical_address;
  325. table_entry_size = ACPI_XSDT_ENTRY_SIZE;
  326. } else {
  327. /* Root table is an RSDT (32-bit physical addresses) */
  328. address = (acpi_physical_address) rsdp->rsdt_physical_address;
  329. table_entry_size = ACPI_RSDT_ENTRY_SIZE;
  330. }
  331. /*
  332. * It is not possible to map more than one entry in some environments,
  333. * so unmap the RSDP here before mapping other tables
  334. */
  335. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  336. /*
  337. * If it is present and used, validate the XSDT for access/size
  338. * and ensure that all table entries are at least non-NULL
  339. */
  340. if (table_entry_size == ACPI_XSDT_ENTRY_SIZE) {
  341. status = acpi_tb_validate_xsdt(address);
  342. if (ACPI_FAILURE(status)) {
  343. ACPI_BIOS_WARNING((AE_INFO,
  344. "XSDT is invalid (%s), using RSDT",
  345. acpi_format_exception(status)));
  346. /* Fall back to the RSDT */
  347. address =
  348. (acpi_physical_address) rsdp->rsdt_physical_address;
  349. table_entry_size = ACPI_RSDT_ENTRY_SIZE;
  350. }
  351. }
  352. /* Map the RSDT/XSDT table header to get the full table length */
  353. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  354. if (!table) {
  355. return_ACPI_STATUS(AE_NO_MEMORY);
  356. }
  357. acpi_tb_print_table_header(address, table);
  358. /*
  359. * Validate length of the table, and map entire table.
  360. * Minimum length table must contain at least one entry.
  361. */
  362. length = table->length;
  363. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  364. if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
  365. ACPI_BIOS_ERROR((AE_INFO,
  366. "Invalid table length 0x%X in RSDT/XSDT",
  367. length));
  368. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  369. }
  370. table = acpi_os_map_memory(address, length);
  371. if (!table) {
  372. return_ACPI_STATUS(AE_NO_MEMORY);
  373. }
  374. /* Validate the root table checksum */
  375. status = acpi_tb_verify_checksum(table, length);
  376. if (ACPI_FAILURE(status)) {
  377. acpi_os_unmap_memory(table, length);
  378. return_ACPI_STATUS(status);
  379. }
  380. /* Get the number of entries and pointer to first entry */
  381. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  382. table_entry_size);
  383. table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
  384. /*
  385. * First two entries in the table array are reserved for the DSDT
  386. * and FACS, which are not actually present in the RSDT/XSDT - they
  387. * come from the FADT
  388. */
  389. acpi_gbl_root_table_list.current_table_count = 2;
  390. /* Initialize the root table array from the RSDT/XSDT */
  391. for (i = 0; i < table_count; i++) {
  392. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  393. status =
  394. acpi_tb_install_non_fixed_table(acpi_tb_get_root_table_entry
  395. (table_entry,
  396. table_entry_size),
  397. ACPI_TABLE_ORIGIN_INTERN_PHYSICAL,
  398. FALSE, TRUE, &table_index);
  399. if (ACPI_SUCCESS(status) &&
  400. ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
  401. tables[table_index].signature,
  402. ACPI_SIG_FADT)) {
  403. acpi_tb_parse_fadt(table_index);
  404. }
  405. table_entry += table_entry_size;
  406. }
  407. /*
  408. * It is not possible to map more than one entry in some environments,
  409. * so unmap the root table here before mapping other tables
  410. */
  411. acpi_os_unmap_memory(table, length);
  412. return_ACPI_STATUS(AE_OK);
  413. }