tbutils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - table utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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 <acpi/actables.h>
  44. #define _COMPONENT ACPI_TABLES
  45. ACPI_MODULE_NAME("tbutils")
  46. /* Local prototypes */
  47. static acpi_physical_address
  48. acpi_tb_get_root_table_entry(u8 * table_entry,
  49. acpi_native_uint table_entry_size);
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_tb_print_table_header
  53. *
  54. * PARAMETERS: Address - Table physical address
  55. * Header - Table header
  56. *
  57. * RETURN: None
  58. *
  59. * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
  60. *
  61. ******************************************************************************/
  62. void
  63. acpi_tb_print_table_header(acpi_physical_address address,
  64. struct acpi_table_header *header)
  65. {
  66. if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
  67. /* FACS only has signature and length fields of common table header */
  68. ACPI_INFO((AE_INFO, "%4.4s @ 0x%p/0x%04X",
  69. header->signature, ACPI_CAST_PTR(void, address),
  70. header->length));
  71. } else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
  72. /* RSDP has no common fields */
  73. ACPI_INFO((AE_INFO, "RSDP @ 0x%p/0x%04X (v%3.3d %6.6s)",
  74. ACPI_CAST_PTR(void, address),
  75. (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
  76. revision >
  77. 0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
  78. header)->length : 20,
  79. ACPI_CAST_PTR(struct acpi_table_rsdp,
  80. header)->revision,
  81. ACPI_CAST_PTR(struct acpi_table_rsdp,
  82. header)->oem_id));
  83. } else {
  84. /* Standard ACPI table with full common header */
  85. ACPI_INFO((AE_INFO,
  86. "%4.4s @ 0x%p/0x%04X (v%3.3d %6.6s %8.8s 0x%08X %4.4s 0x%08X)",
  87. header->signature, ACPI_CAST_PTR(void, address),
  88. header->length, header->revision, header->oem_id,
  89. header->oem_table_id, header->oem_revision,
  90. header->asl_compiler_id,
  91. header->asl_compiler_revision));
  92. }
  93. }
  94. /*******************************************************************************
  95. *
  96. * FUNCTION: acpi_tb_validate_checksum
  97. *
  98. * PARAMETERS: Table - ACPI table to verify
  99. * Length - Length of entire table
  100. *
  101. * RETURN: Status
  102. *
  103. * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
  104. * exception on bad checksum.
  105. *
  106. ******************************************************************************/
  107. acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
  108. {
  109. u8 checksum;
  110. /* Compute the checksum on the table */
  111. checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
  112. /* Checksum ok? (should be zero) */
  113. if (checksum) {
  114. ACPI_WARNING((AE_INFO,
  115. "Incorrect checksum in table [%4.4s] - %2.2X, should be %2.2X",
  116. table->signature, table->checksum,
  117. (u8) (table->checksum - checksum)));
  118. #if (ACPI_CHECKSUM_ABORT)
  119. return (AE_BAD_CHECKSUM);
  120. #endif
  121. }
  122. return (AE_OK);
  123. }
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_tb_checksum
  127. *
  128. * PARAMETERS: Buffer - Pointer to memory region to be checked
  129. * Length - Length of this memory region
  130. *
  131. * RETURN: Checksum (u8)
  132. *
  133. * DESCRIPTION: Calculates circular checksum of memory region.
  134. *
  135. ******************************************************************************/
  136. u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
  137. {
  138. u8 sum = 0;
  139. u8 *end = buffer + length;
  140. while (buffer < end) {
  141. sum = (u8) (sum + *(buffer++));
  142. }
  143. return sum;
  144. }
  145. /*******************************************************************************
  146. *
  147. * FUNCTION: acpi_tb_install_table
  148. *
  149. * PARAMETERS: Address - Physical address of DSDT or FACS
  150. * Flags - Flags
  151. * Signature - Table signature, NULL if no need to
  152. * match
  153. * table_index - Index into root table array
  154. *
  155. * RETURN: None
  156. *
  157. * DESCRIPTION: Install an ACPI table into the global data structure.
  158. *
  159. ******************************************************************************/
  160. void
  161. acpi_tb_install_table(acpi_physical_address address,
  162. u8 flags, char *signature, acpi_native_uint table_index)
  163. {
  164. struct acpi_table_header *table;
  165. if (!address) {
  166. ACPI_ERROR((AE_INFO,
  167. "Null physical address for ACPI table [%s]",
  168. signature));
  169. return;
  170. }
  171. /* Map just the table header */
  172. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  173. if (!table) {
  174. return;
  175. }
  176. /* If a particular signature is expected, signature must match */
  177. if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
  178. ACPI_ERROR((AE_INFO,
  179. "Invalid signature 0x%X for ACPI table [%s]",
  180. *ACPI_CAST_PTR(u32, table->signature), signature));
  181. goto unmap_and_exit;
  182. }
  183. /* Initialize the table entry */
  184. acpi_gbl_root_table_list.tables[table_index].address = address;
  185. acpi_gbl_root_table_list.tables[table_index].length = table->length;
  186. acpi_gbl_root_table_list.tables[table_index].flags = flags;
  187. ACPI_MOVE_32_TO_32(&
  188. (acpi_gbl_root_table_list.tables[table_index].
  189. signature), table->signature);
  190. acpi_tb_print_table_header(address, table);
  191. if (table_index == ACPI_TABLE_INDEX_DSDT) {
  192. /* Global integer width is based upon revision of the DSDT */
  193. acpi_ut_set_integer_width(table->revision);
  194. }
  195. unmap_and_exit:
  196. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  197. }
  198. /*******************************************************************************
  199. *
  200. * FUNCTION: acpi_tb_get_root_table_entry
  201. *
  202. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  203. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  204. *
  205. * RETURN: Physical address extracted from the root table
  206. *
  207. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  208. * both 32-bit and 64-bit platforms
  209. *
  210. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  211. * 64-bit platforms.
  212. *
  213. ******************************************************************************/
  214. static acpi_physical_address
  215. acpi_tb_get_root_table_entry(u8 * table_entry,
  216. acpi_native_uint table_entry_size)
  217. {
  218. u64 address64;
  219. /*
  220. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  221. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  222. */
  223. if (table_entry_size == sizeof(u32)) {
  224. /*
  225. * 32-bit platform, RSDT: Return 32-bit table entry
  226. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  227. */
  228. return ((acpi_physical_address)
  229. (*ACPI_CAST_PTR(u32, table_entry)));
  230. } else {
  231. /*
  232. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  233. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local, return 64-bit
  234. */
  235. ACPI_MOVE_64_TO_64(&address64, table_entry);
  236. #if ACPI_MACHINE_WIDTH == 32
  237. if (address64 > ACPI_UINT32_MAX) {
  238. /* Will truncate 64-bit address to 32 bits */
  239. ACPI_WARNING((AE_INFO,
  240. "64-bit Physical Address in XSDT is too large (%8.8X%8.8X), truncating",
  241. ACPI_FORMAT_UINT64(address64)));
  242. }
  243. #endif
  244. return ((acpi_physical_address) (address64));
  245. }
  246. }
  247. /*******************************************************************************
  248. *
  249. * FUNCTION: acpi_tb_parse_root_table
  250. *
  251. * PARAMETERS: Rsdp - Pointer to the RSDP
  252. * Flags - Flags
  253. *
  254. * RETURN: Status
  255. *
  256. * DESCRIPTION: This function is called to parse the Root System Description
  257. * Table (RSDT or XSDT)
  258. *
  259. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  260. * be mapped and cannot be copied because it contains the actual
  261. * memory location of the ACPI Global Lock.
  262. *
  263. ******************************************************************************/
  264. acpi_status __init
  265. acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags)
  266. {
  267. struct acpi_table_rsdp *rsdp;
  268. acpi_native_uint table_entry_size;
  269. acpi_native_uint i;
  270. u32 table_count;
  271. struct acpi_table_header *table;
  272. acpi_physical_address address;
  273. u32 length;
  274. u8 *table_entry;
  275. acpi_status status;
  276. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  277. /*
  278. * Map the entire RSDP and extract the address of the RSDT or XSDT
  279. */
  280. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  281. if (!rsdp) {
  282. return_ACPI_STATUS(AE_NO_MEMORY);
  283. }
  284. acpi_tb_print_table_header(rsdp_address,
  285. ACPI_CAST_PTR(struct acpi_table_header,
  286. rsdp));
  287. /* Differentiate between RSDT and XSDT root tables */
  288. if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
  289. /*
  290. * Root table is an XSDT (64-bit physical addresses). We must use the
  291. * XSDT if the revision is > 1 and the XSDT pointer is present, as per
  292. * the ACPI specification.
  293. */
  294. address = (acpi_physical_address) rsdp->xsdt_physical_address;
  295. table_entry_size = sizeof(u64);
  296. } else {
  297. /* Root table is an RSDT (32-bit physical addresses) */
  298. address = (acpi_physical_address) rsdp->rsdt_physical_address;
  299. table_entry_size = sizeof(u32);
  300. }
  301. /*
  302. * It is not possible to map more than one entry in some environments,
  303. * so unmap the RSDP here before mapping other tables
  304. */
  305. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  306. /* Map the RSDT/XSDT table header to get the full table length */
  307. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  308. if (!table) {
  309. return_ACPI_STATUS(AE_NO_MEMORY);
  310. }
  311. acpi_tb_print_table_header(address, table);
  312. /* Get the length of the full table, verify length and map entire table */
  313. length = table->length;
  314. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  315. if (length < sizeof(struct acpi_table_header)) {
  316. ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
  317. length));
  318. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  319. }
  320. table = acpi_os_map_memory(address, length);
  321. if (!table) {
  322. return_ACPI_STATUS(AE_NO_MEMORY);
  323. }
  324. /* Validate the root table checksum */
  325. status = acpi_tb_verify_checksum(table, length);
  326. if (ACPI_FAILURE(status)) {
  327. acpi_os_unmap_memory(table, length);
  328. return_ACPI_STATUS(status);
  329. }
  330. /* Calculate the number of tables described in the root table */
  331. table_count =
  332. (u32) ((table->length -
  333. sizeof(struct acpi_table_header)) / table_entry_size);
  334. /*
  335. * First two entries in the table array are reserved for the DSDT and FACS,
  336. * which are not actually present in the RSDT/XSDT - they come from the FADT
  337. */
  338. table_entry =
  339. ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
  340. acpi_gbl_root_table_list.count = 2;
  341. /*
  342. * Initialize the root table array from the RSDT/XSDT
  343. */
  344. for (i = 0; i < table_count; i++) {
  345. if (acpi_gbl_root_table_list.count >=
  346. acpi_gbl_root_table_list.size) {
  347. /* There is no more room in the root table array, attempt resize */
  348. status = acpi_tb_resize_root_table_list();
  349. if (ACPI_FAILURE(status)) {
  350. ACPI_WARNING((AE_INFO,
  351. "Truncating %u table entries!",
  352. (unsigned)
  353. (acpi_gbl_root_table_list.size -
  354. acpi_gbl_root_table_list.
  355. count)));
  356. break;
  357. }
  358. }
  359. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  360. acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
  361. address =
  362. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  363. table_entry += table_entry_size;
  364. acpi_gbl_root_table_list.count++;
  365. }
  366. /*
  367. * It is not possible to map more than one entry in some environments,
  368. * so unmap the root table here before mapping other tables
  369. */
  370. acpi_os_unmap_memory(table, length);
  371. /*
  372. * Complete the initialization of the root table array by examining
  373. * the header of each table
  374. */
  375. for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
  376. acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
  377. address, flags, NULL, i);
  378. /* Special case for FADT - get the DSDT and FACS */
  379. if (ACPI_COMPARE_NAME
  380. (&acpi_gbl_root_table_list.tables[i].signature,
  381. ACPI_SIG_FADT)) {
  382. acpi_tb_parse_fadt(i, flags);
  383. }
  384. }
  385. return_ACPI_STATUS(AE_OK);
  386. }
  387. /******************************************************************************
  388. *
  389. * FUNCTION: acpi_tb_map
  390. *
  391. * PARAMETERS: Address - Address to be mapped
  392. * Length - Length to be mapped
  393. * Flags - Logical or physical addressing mode
  394. *
  395. * RETURN: Pointer to mapped region
  396. *
  397. * DESCRIPTION: Maps memory according to flag
  398. *
  399. *****************************************************************************/
  400. void *acpi_tb_map(acpi_physical_address address, u32 length, u32 flags)
  401. {
  402. if (flags == ACPI_TABLE_ORIGIN_MAPPED) {
  403. return (acpi_os_map_memory(address, length));
  404. } else {
  405. return (ACPI_CAST_PTR(void, address));
  406. }
  407. }
  408. /******************************************************************************
  409. *
  410. * FUNCTION: acpi_tb_unmap
  411. *
  412. * PARAMETERS: Pointer - To mapped region
  413. * Length - Length to be unmapped
  414. * Flags - Logical or physical addressing mode
  415. *
  416. * RETURN: None
  417. *
  418. * DESCRIPTION: Unmaps memory according to flag
  419. *
  420. *****************************************************************************/
  421. void acpi_tb_unmap(void *pointer, u32 length, u32 flags)
  422. {
  423. if (flags == ACPI_TABLE_ORIGIN_MAPPED) {
  424. acpi_os_unmap_memory(pointer, length);
  425. }
  426. }