tbutils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: tbutils - ACPI Table utilities
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "actables.h"
  12. #define _COMPONENT ACPI_TABLES
  13. ACPI_MODULE_NAME("tbutils")
  14. /* Local prototypes */
  15. static acpi_physical_address
  16. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
  17. #if (!ACPI_REDUCED_HARDWARE)
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: acpi_tb_initialize_facs
  21. *
  22. * PARAMETERS: None
  23. *
  24. * RETURN: Status
  25. *
  26. * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
  27. * for accessing the Global Lock and Firmware Waking Vector
  28. *
  29. ******************************************************************************/
  30. acpi_status acpi_tb_initialize_facs(void)
  31. {
  32. struct acpi_table_facs *facs;
  33. /* If Hardware Reduced flag is set, there is no FACS */
  34. if (acpi_gbl_reduced_hardware) {
  35. acpi_gbl_FACS = NULL;
  36. return (AE_OK);
  37. } else if (acpi_gbl_FADT.Xfacs &&
  38. (!acpi_gbl_FADT.facs
  39. || !acpi_gbl_use32_bit_facs_addresses)) {
  40. (void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
  41. ACPI_CAST_INDIRECT_PTR(struct
  42. acpi_table_header,
  43. &facs));
  44. acpi_gbl_FACS = facs;
  45. } else if (acpi_gbl_FADT.facs) {
  46. (void)acpi_get_table_by_index(acpi_gbl_facs_index,
  47. ACPI_CAST_INDIRECT_PTR(struct
  48. acpi_table_header,
  49. &facs));
  50. acpi_gbl_FACS = facs;
  51. }
  52. /* If there is no FACS, just continue. There was already an error msg */
  53. return (AE_OK);
  54. }
  55. #endif /* !ACPI_REDUCED_HARDWARE */
  56. /*******************************************************************************
  57. *
  58. * FUNCTION: acpi_tb_check_dsdt_header
  59. *
  60. * PARAMETERS: None
  61. *
  62. * RETURN: None
  63. *
  64. * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
  65. * if the DSDT has been replaced from outside the OS and/or if
  66. * the DSDT header has been corrupted.
  67. *
  68. ******************************************************************************/
  69. void acpi_tb_check_dsdt_header(void)
  70. {
  71. /* Compare original length and checksum to current values */
  72. if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
  73. acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
  74. ACPI_BIOS_ERROR((AE_INFO,
  75. "The DSDT has been corrupted or replaced - "
  76. "old, new headers below"));
  77. acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
  78. acpi_tb_print_table_header(0, acpi_gbl_DSDT);
  79. ACPI_ERROR((AE_INFO,
  80. "Please send DMI info to [email protected]\n"
  81. "If system does not work as expected, please boot with acpi=copy_dsdt"));
  82. /* Disable further error messages */
  83. acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
  84. acpi_gbl_original_dsdt_header.checksum =
  85. acpi_gbl_DSDT->checksum;
  86. }
  87. }
  88. /*******************************************************************************
  89. *
  90. * FUNCTION: acpi_tb_copy_dsdt
  91. *
  92. * PARAMETERS: table_index - Index of installed table to copy
  93. *
  94. * RETURN: The copied DSDT
  95. *
  96. * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
  97. * Some very bad BIOSs are known to either corrupt the DSDT or
  98. * install a new, bad DSDT. This copy works around the problem.
  99. *
  100. ******************************************************************************/
  101. struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  102. {
  103. struct acpi_table_header *new_table;
  104. struct acpi_table_desc *table_desc;
  105. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  106. new_table = ACPI_ALLOCATE(table_desc->length);
  107. if (!new_table) {
  108. ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
  109. table_desc->length));
  110. return (NULL);
  111. }
  112. memcpy(new_table, table_desc->pointer, table_desc->length);
  113. acpi_tb_uninstall_table(table_desc);
  114. acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
  115. tables[acpi_gbl_dsdt_index],
  116. ACPI_PTR_TO_PHYSADDR(new_table),
  117. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  118. new_table);
  119. ACPI_INFO(("Forced DSDT copy: length 0x%05X copied locally, original unmapped", new_table->length));
  120. return (new_table);
  121. }
  122. /*******************************************************************************
  123. *
  124. * FUNCTION: acpi_tb_get_root_table_entry
  125. *
  126. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  127. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  128. *
  129. * RETURN: Physical address extracted from the root table
  130. *
  131. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  132. * both 32-bit and 64-bit platforms
  133. *
  134. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  135. * 64-bit platforms.
  136. *
  137. ******************************************************************************/
  138. static acpi_physical_address
  139. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
  140. {
  141. u64 address64;
  142. /*
  143. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  144. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  145. */
  146. if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
  147. /*
  148. * 32-bit platform, RSDT: Return 32-bit table entry
  149. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  150. */
  151. return ((acpi_physical_address)
  152. (*ACPI_CAST_PTR(u32, table_entry)));
  153. } else {
  154. /*
  155. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  156. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
  157. * return 64-bit
  158. */
  159. ACPI_MOVE_64_TO_64(&address64, table_entry);
  160. #if ACPI_MACHINE_WIDTH == 32
  161. if (address64 > ACPI_UINT32_MAX) {
  162. /* Will truncate 64-bit address to 32 bits, issue warning */
  163. ACPI_BIOS_WARNING((AE_INFO,
  164. "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
  165. " truncating",
  166. ACPI_FORMAT_UINT64(address64)));
  167. }
  168. #endif
  169. return ((acpi_physical_address)(address64));
  170. }
  171. }
  172. /*******************************************************************************
  173. *
  174. * FUNCTION: acpi_tb_parse_root_table
  175. *
  176. * PARAMETERS: rsdp_address - Pointer to the RSDP
  177. *
  178. * RETURN: Status
  179. *
  180. * DESCRIPTION: This function is called to parse the Root System Description
  181. * Table (RSDT or XSDT)
  182. *
  183. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  184. * be mapped and cannot be copied because it contains the actual
  185. * memory location of the ACPI Global Lock.
  186. *
  187. ******************************************************************************/
  188. acpi_status ACPI_INIT_FUNCTION
  189. acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  190. {
  191. struct acpi_table_rsdp *rsdp;
  192. u32 table_entry_size;
  193. u32 i;
  194. u32 table_count;
  195. struct acpi_table_header *table;
  196. acpi_physical_address address;
  197. u32 length;
  198. u8 *table_entry;
  199. acpi_status status;
  200. u32 table_index;
  201. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  202. /* Map the entire RSDP and extract the address of the RSDT or XSDT */
  203. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  204. if (!rsdp) {
  205. return_ACPI_STATUS(AE_NO_MEMORY);
  206. }
  207. acpi_tb_print_table_header(rsdp_address,
  208. ACPI_CAST_PTR(struct acpi_table_header,
  209. rsdp));
  210. /* Use XSDT if present and not overridden. Otherwise, use RSDT */
  211. if ((rsdp->revision > 1) &&
  212. rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
  213. /*
  214. * RSDP contains an XSDT (64-bit physical addresses). We must use
  215. * the XSDT if the revision is > 1 and the XSDT pointer is present,
  216. * as per the ACPI specification.
  217. */
  218. address = (acpi_physical_address)rsdp->xsdt_physical_address;
  219. table_entry_size = ACPI_XSDT_ENTRY_SIZE;
  220. } else {
  221. /* Root table is an RSDT (32-bit physical addresses) */
  222. address = (acpi_physical_address)rsdp->rsdt_physical_address;
  223. table_entry_size = ACPI_RSDT_ENTRY_SIZE;
  224. }
  225. /*
  226. * It is not possible to map more than one entry in some environments,
  227. * so unmap the RSDP here before mapping other tables
  228. */
  229. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  230. /* Map the RSDT/XSDT table header to get the full table length */
  231. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  232. if (!table) {
  233. return_ACPI_STATUS(AE_NO_MEMORY);
  234. }
  235. acpi_tb_print_table_header(address, table);
  236. /*
  237. * Validate length of the table, and map entire table.
  238. * Minimum length table must contain at least one entry.
  239. */
  240. length = table->length;
  241. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  242. if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
  243. ACPI_BIOS_ERROR((AE_INFO,
  244. "Invalid table length 0x%X in RSDT/XSDT",
  245. length));
  246. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  247. }
  248. table = acpi_os_map_memory(address, length);
  249. if (!table) {
  250. return_ACPI_STATUS(AE_NO_MEMORY);
  251. }
  252. /* Validate the root table checksum */
  253. status = acpi_tb_verify_checksum(table, length);
  254. if (ACPI_FAILURE(status)) {
  255. acpi_os_unmap_memory(table, length);
  256. return_ACPI_STATUS(status);
  257. }
  258. /* Get the number of entries and pointer to first entry */
  259. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  260. table_entry_size);
  261. table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
  262. /* Initialize the root table array from the RSDT/XSDT */
  263. for (i = 0; i < table_count; i++) {
  264. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  265. address =
  266. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  267. /* Skip NULL entries in RSDT/XSDT */
  268. if (!address) {
  269. goto next_table;
  270. }
  271. status = acpi_tb_install_standard_table(address,
  272. ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
  273. NULL, FALSE, TRUE,
  274. &table_index);
  275. if (ACPI_SUCCESS(status) &&
  276. ACPI_COMPARE_NAMESEG(&acpi_gbl_root_table_list.
  277. tables[table_index].signature,
  278. ACPI_SIG_FADT)) {
  279. acpi_gbl_fadt_index = table_index;
  280. acpi_tb_parse_fadt();
  281. }
  282. next_table:
  283. table_entry += table_entry_size;
  284. }
  285. acpi_os_unmap_memory(table, length);
  286. return_ACPI_STATUS(AE_OK);
  287. }
  288. /*******************************************************************************
  289. *
  290. * FUNCTION: acpi_tb_get_table
  291. *
  292. * PARAMETERS: table_desc - Table descriptor
  293. * out_table - Where the pointer to the table is returned
  294. *
  295. * RETURN: Status and pointer to the requested table
  296. *
  297. * DESCRIPTION: Increase a reference to a table descriptor and return the
  298. * validated table pointer.
  299. * If the table descriptor is an entry of the root table list,
  300. * this API must be invoked with ACPI_MTX_TABLES acquired.
  301. *
  302. ******************************************************************************/
  303. acpi_status
  304. acpi_tb_get_table(struct acpi_table_desc *table_desc,
  305. struct acpi_table_header **out_table)
  306. {
  307. acpi_status status;
  308. ACPI_FUNCTION_TRACE(acpi_tb_get_table);
  309. if (table_desc->validation_count == 0) {
  310. /* Table need to be "VALIDATED" */
  311. status = acpi_tb_validate_table(table_desc);
  312. if (ACPI_FAILURE(status)) {
  313. return_ACPI_STATUS(status);
  314. }
  315. }
  316. if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
  317. table_desc->validation_count++;
  318. /*
  319. * Detect validation_count overflows to ensure that the warning
  320. * message will only be printed once.
  321. */
  322. if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
  323. ACPI_WARNING((AE_INFO,
  324. "Table %p, Validation count overflows\n",
  325. table_desc));
  326. }
  327. }
  328. *out_table = table_desc->pointer;
  329. return_ACPI_STATUS(AE_OK);
  330. }
  331. /*******************************************************************************
  332. *
  333. * FUNCTION: acpi_tb_put_table
  334. *
  335. * PARAMETERS: table_desc - Table descriptor
  336. *
  337. * RETURN: None
  338. *
  339. * DESCRIPTION: Decrease a reference to a table descriptor and release the
  340. * validated table pointer if no references.
  341. * If the table descriptor is an entry of the root table list,
  342. * this API must be invoked with ACPI_MTX_TABLES acquired.
  343. *
  344. ******************************************************************************/
  345. void acpi_tb_put_table(struct acpi_table_desc *table_desc)
  346. {
  347. ACPI_FUNCTION_TRACE(acpi_tb_put_table);
  348. if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
  349. table_desc->validation_count--;
  350. /*
  351. * Detect validation_count underflows to ensure that the warning
  352. * message will only be printed once.
  353. */
  354. if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
  355. ACPI_WARNING((AE_INFO,
  356. "Table %p, Validation count underflows\n",
  357. table_desc));
  358. return_VOID;
  359. }
  360. }
  361. if (table_desc->validation_count == 0) {
  362. /* Table need to be "INVALIDATED" */
  363. acpi_tb_invalidate_table(table_desc);
  364. }
  365. return_VOID;
  366. }