tbutils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - ACPI Table utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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. struct acpi_table_facs *facs;
  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. } else if (acpi_gbl_FADT.Xfacs &&
  71. (!acpi_gbl_FADT.facs
  72. || !acpi_gbl_use32_bit_facs_addresses)) {
  73. (void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
  74. ACPI_CAST_INDIRECT_PTR(struct
  75. acpi_table_header,
  76. &facs));
  77. acpi_gbl_FACS = facs;
  78. } else if (acpi_gbl_FADT.facs) {
  79. (void)acpi_get_table_by_index(acpi_gbl_facs_index,
  80. ACPI_CAST_INDIRECT_PTR(struct
  81. acpi_table_header,
  82. &facs));
  83. acpi_gbl_FACS = facs;
  84. }
  85. /* If there is no FACS, just continue. There was already an error msg */
  86. return (AE_OK);
  87. }
  88. #endif /* !ACPI_REDUCED_HARDWARE */
  89. /*******************************************************************************
  90. *
  91. * FUNCTION: acpi_tb_check_dsdt_header
  92. *
  93. * PARAMETERS: None
  94. *
  95. * RETURN: None
  96. *
  97. * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
  98. * if the DSDT has been replaced from outside the OS and/or if
  99. * the DSDT header has been corrupted.
  100. *
  101. ******************************************************************************/
  102. void acpi_tb_check_dsdt_header(void)
  103. {
  104. /* Compare original length and checksum to current values */
  105. if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
  106. acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
  107. ACPI_BIOS_ERROR((AE_INFO,
  108. "The DSDT has been corrupted or replaced - "
  109. "old, new headers below"));
  110. acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
  111. acpi_tb_print_table_header(0, acpi_gbl_DSDT);
  112. ACPI_ERROR((AE_INFO,
  113. "Please send DMI info to linux-acpi@vger.kernel.org\n"
  114. "If system does not work as expected, please boot with acpi=copy_dsdt"));
  115. /* Disable further error messages */
  116. acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
  117. acpi_gbl_original_dsdt_header.checksum =
  118. acpi_gbl_DSDT->checksum;
  119. }
  120. }
  121. /*******************************************************************************
  122. *
  123. * FUNCTION: acpi_tb_copy_dsdt
  124. *
  125. * PARAMETERS: table_desc - Installed table to copy
  126. *
  127. * RETURN: None
  128. *
  129. * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
  130. * Some very bad BIOSs are known to either corrupt the DSDT or
  131. * install a new, bad DSDT. This copy works around the problem.
  132. *
  133. ******************************************************************************/
  134. struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  135. {
  136. struct acpi_table_header *new_table;
  137. struct acpi_table_desc *table_desc;
  138. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  139. new_table = ACPI_ALLOCATE(table_desc->length);
  140. if (!new_table) {
  141. ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
  142. table_desc->length));
  143. return (NULL);
  144. }
  145. memcpy(new_table, table_desc->pointer, table_desc->length);
  146. acpi_tb_uninstall_table(table_desc);
  147. acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
  148. tables[acpi_gbl_dsdt_index],
  149. ACPI_PTR_TO_PHYSADDR(new_table),
  150. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  151. new_table);
  152. ACPI_INFO(("Forced DSDT copy: length 0x%05X copied locally, original unmapped", new_table->length));
  153. return (new_table);
  154. }
  155. /*******************************************************************************
  156. *
  157. * FUNCTION: acpi_tb_get_root_table_entry
  158. *
  159. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  160. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  161. *
  162. * RETURN: Physical address extracted from the root table
  163. *
  164. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  165. * both 32-bit and 64-bit platforms
  166. *
  167. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  168. * 64-bit platforms.
  169. *
  170. ******************************************************************************/
  171. static acpi_physical_address
  172. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
  173. {
  174. u64 address64;
  175. /*
  176. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  177. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  178. */
  179. if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
  180. /*
  181. * 32-bit platform, RSDT: Return 32-bit table entry
  182. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  183. */
  184. return ((acpi_physical_address)
  185. (*ACPI_CAST_PTR(u32, table_entry)));
  186. } else {
  187. /*
  188. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  189. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
  190. * return 64-bit
  191. */
  192. ACPI_MOVE_64_TO_64(&address64, table_entry);
  193. #if ACPI_MACHINE_WIDTH == 32
  194. if (address64 > ACPI_UINT32_MAX) {
  195. /* Will truncate 64-bit address to 32 bits, issue warning */
  196. ACPI_BIOS_WARNING((AE_INFO,
  197. "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
  198. " truncating",
  199. ACPI_FORMAT_UINT64(address64)));
  200. }
  201. #endif
  202. return ((acpi_physical_address)(address64));
  203. }
  204. }
  205. /*******************************************************************************
  206. *
  207. * FUNCTION: acpi_tb_parse_root_table
  208. *
  209. * PARAMETERS: rsdp - Pointer to the RSDP
  210. *
  211. * RETURN: Status
  212. *
  213. * DESCRIPTION: This function is called to parse the Root System Description
  214. * Table (RSDT or XSDT)
  215. *
  216. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  217. * be mapped and cannot be copied because it contains the actual
  218. * memory location of the ACPI Global Lock.
  219. *
  220. ******************************************************************************/
  221. acpi_status ACPI_INIT_FUNCTION
  222. acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  223. {
  224. struct acpi_table_rsdp *rsdp;
  225. u32 table_entry_size;
  226. u32 i;
  227. u32 table_count;
  228. struct acpi_table_header *table;
  229. acpi_physical_address address;
  230. u32 length;
  231. u8 *table_entry;
  232. acpi_status status;
  233. u32 table_index;
  234. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  235. /* Map the entire RSDP and extract the address of the RSDT or XSDT */
  236. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  237. if (!rsdp) {
  238. return_ACPI_STATUS(AE_NO_MEMORY);
  239. }
  240. acpi_tb_print_table_header(rsdp_address,
  241. ACPI_CAST_PTR(struct acpi_table_header,
  242. rsdp));
  243. /* Use XSDT if present and not overridden. Otherwise, use RSDT */
  244. if ((rsdp->revision > 1) &&
  245. rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
  246. /*
  247. * RSDP contains an XSDT (64-bit physical addresses). We must use
  248. * the XSDT if the revision is > 1 and the XSDT pointer is present,
  249. * as per the ACPI specification.
  250. */
  251. address = (acpi_physical_address)rsdp->xsdt_physical_address;
  252. table_entry_size = ACPI_XSDT_ENTRY_SIZE;
  253. } else {
  254. /* Root table is an RSDT (32-bit physical addresses) */
  255. address = (acpi_physical_address)rsdp->rsdt_physical_address;
  256. table_entry_size = ACPI_RSDT_ENTRY_SIZE;
  257. }
  258. /*
  259. * It is not possible to map more than one entry in some environments,
  260. * so unmap the RSDP here before mapping other tables
  261. */
  262. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  263. /* Map the RSDT/XSDT table header to get the full table length */
  264. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  265. if (!table) {
  266. return_ACPI_STATUS(AE_NO_MEMORY);
  267. }
  268. acpi_tb_print_table_header(address, table);
  269. /*
  270. * Validate length of the table, and map entire table.
  271. * Minimum length table must contain at least one entry.
  272. */
  273. length = table->length;
  274. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  275. if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
  276. ACPI_BIOS_ERROR((AE_INFO,
  277. "Invalid table length 0x%X in RSDT/XSDT",
  278. length));
  279. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  280. }
  281. table = acpi_os_map_memory(address, length);
  282. if (!table) {
  283. return_ACPI_STATUS(AE_NO_MEMORY);
  284. }
  285. /* Validate the root table checksum */
  286. status = acpi_tb_verify_checksum(table, length);
  287. if (ACPI_FAILURE(status)) {
  288. acpi_os_unmap_memory(table, length);
  289. return_ACPI_STATUS(status);
  290. }
  291. /* Get the number of entries and pointer to first entry */
  292. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  293. table_entry_size);
  294. table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
  295. /* Initialize the root table array from the RSDT/XSDT */
  296. for (i = 0; i < table_count; i++) {
  297. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  298. address =
  299. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  300. /* Skip NULL entries in RSDT/XSDT */
  301. if (!address) {
  302. goto next_table;
  303. }
  304. status = acpi_tb_install_standard_table(address,
  305. ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
  306. FALSE, TRUE,
  307. &table_index);
  308. if (ACPI_SUCCESS(status) &&
  309. ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
  310. tables[table_index].signature,
  311. ACPI_SIG_FADT)) {
  312. acpi_gbl_fadt_index = table_index;
  313. acpi_tb_parse_fadt();
  314. }
  315. next_table:
  316. table_entry += table_entry_size;
  317. }
  318. acpi_os_unmap_memory(table, length);
  319. return_ACPI_STATUS(AE_OK);
  320. }