tbutils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_physical_address
  49. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
  50. #if (!ACPI_REDUCED_HARDWARE)
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_tb_initialize_facs
  54. *
  55. * PARAMETERS: None
  56. *
  57. * RETURN: Status
  58. *
  59. * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
  60. * for accessing the Global Lock and Firmware Waking Vector
  61. *
  62. ******************************************************************************/
  63. acpi_status acpi_tb_initialize_facs(void)
  64. {
  65. acpi_status status;
  66. /* If Hardware Reduced flag is set, there is no FACS */
  67. if (acpi_gbl_reduced_hardware) {
  68. acpi_gbl_FACS = NULL;
  69. return (AE_OK);
  70. }
  71. status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
  72. ACPI_CAST_INDIRECT_PTR(struct
  73. acpi_table_header,
  74. &acpi_gbl_FACS));
  75. return (status);
  76. }
  77. #endif /* !ACPI_REDUCED_HARDWARE */
  78. /*******************************************************************************
  79. *
  80. * FUNCTION: acpi_tb_tables_loaded
  81. *
  82. * PARAMETERS: None
  83. *
  84. * RETURN: TRUE if required ACPI tables are loaded
  85. *
  86. * DESCRIPTION: Determine if the minimum required ACPI tables are present
  87. * (FADT, FACS, DSDT)
  88. *
  89. ******************************************************************************/
  90. u8 acpi_tb_tables_loaded(void)
  91. {
  92. if (acpi_gbl_root_table_list.current_table_count >= 3) {
  93. return (TRUE);
  94. }
  95. return (FALSE);
  96. }
  97. /*******************************************************************************
  98. *
  99. * FUNCTION: acpi_tb_check_dsdt_header
  100. *
  101. * PARAMETERS: None
  102. *
  103. * RETURN: None
  104. *
  105. * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
  106. * if the DSDT has been replaced from outside the OS and/or if
  107. * the DSDT header has been corrupted.
  108. *
  109. ******************************************************************************/
  110. void acpi_tb_check_dsdt_header(void)
  111. {
  112. /* Compare original length and checksum to current values */
  113. if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
  114. acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
  115. ACPI_BIOS_ERROR((AE_INFO,
  116. "The DSDT has been corrupted or replaced - "
  117. "old, new headers below"));
  118. acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
  119. acpi_tb_print_table_header(0, acpi_gbl_DSDT);
  120. ACPI_ERROR((AE_INFO,
  121. "Please send DMI info to linux-acpi@vger.kernel.org\n"
  122. "If system does not work as expected, please boot with acpi=copy_dsdt"));
  123. /* Disable further error messages */
  124. acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
  125. acpi_gbl_original_dsdt_header.checksum =
  126. acpi_gbl_DSDT->checksum;
  127. }
  128. }
  129. /*******************************************************************************
  130. *
  131. * FUNCTION: acpi_tb_copy_dsdt
  132. *
  133. * PARAMETERS: table_desc - Installed table to copy
  134. *
  135. * RETURN: None
  136. *
  137. * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
  138. * Some very bad BIOSs are known to either corrupt the DSDT or
  139. * install a new, bad DSDT. This copy works around the problem.
  140. *
  141. ******************************************************************************/
  142. struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  143. {
  144. struct acpi_table_header *new_table;
  145. struct acpi_table_desc *table_desc;
  146. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  147. new_table = ACPI_ALLOCATE(table_desc->length);
  148. if (!new_table) {
  149. ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
  150. table_desc->length));
  151. return (NULL);
  152. }
  153. ACPI_MEMCPY(new_table, table_desc->pointer, table_desc->length);
  154. acpi_tb_uninstall_table(table_desc);
  155. acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
  156. tables[ACPI_TABLE_INDEX_DSDT],
  157. ACPI_PTR_TO_PHYSADDR(new_table),
  158. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  159. 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_parse_root_table
  218. *
  219. * PARAMETERS: rsdp - Pointer to the RSDP
  220. *
  221. * RETURN: Status
  222. *
  223. * DESCRIPTION: This function is called to parse the Root System Description
  224. * Table (RSDT or XSDT)
  225. *
  226. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  227. * be mapped and cannot be copied because it contains the actual
  228. * memory location of the ACPI Global Lock.
  229. *
  230. ******************************************************************************/
  231. acpi_status __init acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  232. {
  233. struct acpi_table_rsdp *rsdp;
  234. u32 table_entry_size;
  235. u32 i;
  236. u32 table_count;
  237. struct acpi_table_header *table;
  238. acpi_physical_address address;
  239. u32 length;
  240. u8 *table_entry;
  241. acpi_status status;
  242. u32 table_index;
  243. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  244. /* Map the entire RSDP and extract the address of the RSDT or XSDT */
  245. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  246. if (!rsdp) {
  247. return_ACPI_STATUS(AE_NO_MEMORY);
  248. }
  249. acpi_tb_print_table_header(rsdp_address,
  250. ACPI_CAST_PTR(struct acpi_table_header,
  251. rsdp));
  252. /* Use XSDT if present and not overridden. Otherwise, use RSDT */
  253. if ((rsdp->revision > 1) &&
  254. rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
  255. /*
  256. * RSDP contains an XSDT (64-bit physical addresses). We must use
  257. * the XSDT if the revision is > 1 and the XSDT pointer is present,
  258. * as per the ACPI specification.
  259. */
  260. address = (acpi_physical_address) rsdp->xsdt_physical_address;
  261. table_entry_size = ACPI_XSDT_ENTRY_SIZE;
  262. } else {
  263. /* Root table is an RSDT (32-bit physical addresses) */
  264. address = (acpi_physical_address) rsdp->rsdt_physical_address;
  265. table_entry_size = ACPI_RSDT_ENTRY_SIZE;
  266. }
  267. /*
  268. * It is not possible to map more than one entry in some environments,
  269. * so unmap the RSDP here before mapping other tables
  270. */
  271. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  272. /* Map the RSDT/XSDT table header to get the full table length */
  273. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  274. if (!table) {
  275. return_ACPI_STATUS(AE_NO_MEMORY);
  276. }
  277. acpi_tb_print_table_header(address, table);
  278. /*
  279. * Validate length of the table, and map entire table.
  280. * Minimum length table must contain at least one entry.
  281. */
  282. length = table->length;
  283. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  284. if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
  285. ACPI_BIOS_ERROR((AE_INFO,
  286. "Invalid table length 0x%X in RSDT/XSDT",
  287. length));
  288. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  289. }
  290. table = acpi_os_map_memory(address, length);
  291. if (!table) {
  292. return_ACPI_STATUS(AE_NO_MEMORY);
  293. }
  294. /* Validate the root table checksum */
  295. status = acpi_tb_verify_checksum(table, length);
  296. if (ACPI_FAILURE(status)) {
  297. acpi_os_unmap_memory(table, length);
  298. return_ACPI_STATUS(status);
  299. }
  300. /* Get the number of entries and pointer to first entry */
  301. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  302. table_entry_size);
  303. table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
  304. /*
  305. * First two entries in the table array are reserved for the DSDT
  306. * and FACS, which are not actually present in the RSDT/XSDT - they
  307. * come from the FADT
  308. */
  309. acpi_gbl_root_table_list.current_table_count = 2;
  310. /* Initialize the root table array from the RSDT/XSDT */
  311. for (i = 0; i < table_count; i++) {
  312. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  313. address =
  314. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  315. /* Skip NULL entries in RSDT/XSDT */
  316. if (!address) {
  317. goto next_table;
  318. }
  319. status = acpi_tb_install_standard_table(address,
  320. ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
  321. FALSE, TRUE,
  322. &table_index);
  323. if (ACPI_SUCCESS(status) &&
  324. ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
  325. tables[table_index].signature,
  326. ACPI_SIG_FADT)) {
  327. acpi_tb_parse_fadt(table_index);
  328. }
  329. next_table:
  330. table_entry += table_entry_size;
  331. }
  332. acpi_os_unmap_memory(table, length);
  333. return_ACPI_STATUS(AE_OK);
  334. }