utresrc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*******************************************************************************
  2. *
  3. * Module Name: utresrc - Resource managment 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/amlresrc.h>
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utmisc")
  46. #if defined(ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
  47. /*
  48. * Strings used to decode resource descriptors.
  49. * Used by both the disasssembler and the debugger resource dump routines
  50. */
  51. const char *acpi_gbl_BMdecode[2] = {
  52. "not_bus_master",
  53. "bus_master"
  54. };
  55. const char *acpi_gbl_config_decode[4] = {
  56. "0 - Good Configuration",
  57. "1 - Acceptable Configuration",
  58. "2 - Suboptimal Configuration",
  59. "3 - ***Invalid Configuration***",
  60. };
  61. const char *acpi_gbl_consume_decode[2] = {
  62. "resource_producer",
  63. "resource_consumer"
  64. };
  65. const char *acpi_gbl_DECdecode[2] = {
  66. "pos_decode",
  67. "sub_decode"
  68. };
  69. const char *acpi_gbl_HEdecode[2] = {
  70. "Level",
  71. "Edge"
  72. };
  73. const char *acpi_gbl_io_decode[2] = {
  74. "Decode10",
  75. "Decode16"
  76. };
  77. const char *acpi_gbl_LLdecode[2] = {
  78. "active_high",
  79. "active_low"
  80. };
  81. const char *acpi_gbl_max_decode[2] = {
  82. "max_not_fixed",
  83. "max_fixed"
  84. };
  85. const char *acpi_gbl_MEMdecode[4] = {
  86. "non_cacheable",
  87. "Cacheable",
  88. "write_combining",
  89. "Prefetchable"
  90. };
  91. const char *acpi_gbl_min_decode[2] = {
  92. "min_not_fixed",
  93. "min_fixed"
  94. };
  95. const char *acpi_gbl_MTPdecode[4] = {
  96. "address_range_memory",
  97. "address_range_reserved",
  98. "address_range_aCPI",
  99. "address_range_nVS"
  100. };
  101. const char *acpi_gbl_RNGdecode[4] = {
  102. "invalid_ranges",
  103. "non_iSAonly_ranges",
  104. "ISAonly_ranges",
  105. "entire_range"
  106. };
  107. const char *acpi_gbl_RWdecode[2] = {
  108. "read_only",
  109. "read_write"
  110. };
  111. const char *acpi_gbl_SHRdecode[2] = {
  112. "Exclusive",
  113. "Shared"
  114. };
  115. const char *acpi_gbl_SIZdecode[4] = {
  116. "Transfer8",
  117. "Transfer8_16",
  118. "Transfer16",
  119. "invalid_size"
  120. };
  121. const char *acpi_gbl_TRSdecode[2] = {
  122. "dense_translation",
  123. "sparse_translation"
  124. };
  125. const char *acpi_gbl_TTPdecode[2] = {
  126. "type_static",
  127. "type_translation"
  128. };
  129. const char *acpi_gbl_TYPdecode[4] = {
  130. "Compatibility",
  131. "type_a",
  132. "type_b",
  133. "type_f"
  134. };
  135. #endif
  136. /*
  137. * Base sizes of the raw AML resource descriptors, indexed by resource type.
  138. * Zero indicates a reserved (and therefore invalid) resource type.
  139. */
  140. const u8 acpi_gbl_resource_aml_sizes[] = {
  141. /* Small descriptors */
  142. 0,
  143. 0,
  144. 0,
  145. 0,
  146. ACPI_AML_SIZE_SMALL(struct aml_resource_irq),
  147. ACPI_AML_SIZE_SMALL(struct aml_resource_dma),
  148. ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent),
  149. ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent),
  150. ACPI_AML_SIZE_SMALL(struct aml_resource_io),
  151. ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io),
  152. 0,
  153. 0,
  154. 0,
  155. 0,
  156. ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
  157. ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
  158. /* Large descriptors */
  159. 0,
  160. ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
  161. ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
  162. 0,
  163. ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large),
  164. ACPI_AML_SIZE_LARGE(struct aml_resource_memory32),
  165. ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32),
  166. ACPI_AML_SIZE_LARGE(struct aml_resource_address32),
  167. ACPI_AML_SIZE_LARGE(struct aml_resource_address16),
  168. ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq),
  169. ACPI_AML_SIZE_LARGE(struct aml_resource_address64),
  170. ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64)
  171. };
  172. /*
  173. * Resource types, used to validate the resource length field.
  174. * The length of fixed-length types must match exactly, variable
  175. * lengths must meet the minimum required length, etc.
  176. * Zero indicates a reserved (and therefore invalid) resource type.
  177. */
  178. static const u8 acpi_gbl_resource_types[] = {
  179. /* Small descriptors */
  180. 0,
  181. 0,
  182. 0,
  183. 0,
  184. ACPI_SMALL_VARIABLE_LENGTH,
  185. ACPI_FIXED_LENGTH,
  186. ACPI_SMALL_VARIABLE_LENGTH,
  187. ACPI_FIXED_LENGTH,
  188. ACPI_FIXED_LENGTH,
  189. ACPI_FIXED_LENGTH,
  190. 0,
  191. 0,
  192. 0,
  193. 0,
  194. ACPI_VARIABLE_LENGTH,
  195. ACPI_FIXED_LENGTH,
  196. /* Large descriptors */
  197. 0,
  198. ACPI_FIXED_LENGTH,
  199. ACPI_FIXED_LENGTH,
  200. 0,
  201. ACPI_VARIABLE_LENGTH,
  202. ACPI_FIXED_LENGTH,
  203. ACPI_FIXED_LENGTH,
  204. ACPI_VARIABLE_LENGTH,
  205. ACPI_VARIABLE_LENGTH,
  206. ACPI_VARIABLE_LENGTH,
  207. ACPI_VARIABLE_LENGTH,
  208. ACPI_FIXED_LENGTH
  209. };
  210. /*******************************************************************************
  211. *
  212. * FUNCTION: acpi_ut_validate_resource
  213. *
  214. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  215. * return_index - Where the resource index is returned. NULL
  216. * if the index is not required.
  217. *
  218. * RETURN: Status, and optionally the Index into the global resource tables
  219. *
  220. * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
  221. * Type and Resource Length. Returns an index into the global
  222. * resource information/dispatch tables for later use.
  223. *
  224. ******************************************************************************/
  225. acpi_status acpi_ut_validate_resource(void *aml, u8 * return_index)
  226. {
  227. u8 resource_type;
  228. u8 resource_index;
  229. acpi_rs_length resource_length;
  230. acpi_rs_length minimum_resource_length;
  231. ACPI_FUNCTION_ENTRY();
  232. /*
  233. * 1) Validate the resource_type field (Byte 0)
  234. */
  235. resource_type = ACPI_GET8(aml);
  236. /*
  237. * Byte 0 contains the descriptor name (Resource Type)
  238. * Examine the large/small bit in the resource header
  239. */
  240. if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
  241. /* Verify the large resource type (name) against the max */
  242. if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
  243. return (AE_AML_INVALID_RESOURCE_TYPE);
  244. }
  245. /*
  246. * Large Resource Type -- bits 6:0 contain the name
  247. * Translate range 0x80-0x8B to index range 0x10-0x1B
  248. */
  249. resource_index = (u8) (resource_type - 0x70);
  250. } else {
  251. /*
  252. * Small Resource Type -- bits 6:3 contain the name
  253. * Shift range to index range 0x00-0x0F
  254. */
  255. resource_index = (u8)
  256. ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
  257. }
  258. /* Check validity of the resource type, zero indicates name is invalid */
  259. if (!acpi_gbl_resource_types[resource_index]) {
  260. return (AE_AML_INVALID_RESOURCE_TYPE);
  261. }
  262. /*
  263. * 2) Validate the resource_length field. This ensures that the length
  264. * is at least reasonable, and guarantees that it is non-zero.
  265. */
  266. resource_length = acpi_ut_get_resource_length(aml);
  267. minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
  268. /* Validate based upon the type of resource - fixed length or variable */
  269. switch (acpi_gbl_resource_types[resource_index]) {
  270. case ACPI_FIXED_LENGTH:
  271. /* Fixed length resource, length must match exactly */
  272. if (resource_length != minimum_resource_length) {
  273. return (AE_AML_BAD_RESOURCE_LENGTH);
  274. }
  275. break;
  276. case ACPI_VARIABLE_LENGTH:
  277. /* Variable length resource, length must be at least the minimum */
  278. if (resource_length < minimum_resource_length) {
  279. return (AE_AML_BAD_RESOURCE_LENGTH);
  280. }
  281. break;
  282. case ACPI_SMALL_VARIABLE_LENGTH:
  283. /* Small variable length resource, length can be (Min) or (Min-1) */
  284. if ((resource_length > minimum_resource_length) ||
  285. (resource_length < (minimum_resource_length - 1))) {
  286. return (AE_AML_BAD_RESOURCE_LENGTH);
  287. }
  288. break;
  289. default:
  290. /* Shouldn't happen (because of validation earlier), but be sure */
  291. return (AE_AML_INVALID_RESOURCE_TYPE);
  292. }
  293. /* Optionally return the resource table index */
  294. if (return_index) {
  295. *return_index = resource_index;
  296. }
  297. return (AE_OK);
  298. }
  299. /*******************************************************************************
  300. *
  301. * FUNCTION: acpi_ut_get_resource_type
  302. *
  303. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  304. *
  305. * RETURN: The Resource Type with no extraneous bits (except the
  306. * Large/Small descriptor bit -- this is left alone)
  307. *
  308. * DESCRIPTION: Extract the Resource Type/Name from the first byte of
  309. * a resource descriptor.
  310. *
  311. ******************************************************************************/
  312. u8 acpi_ut_get_resource_type(void *aml)
  313. {
  314. ACPI_FUNCTION_ENTRY();
  315. /*
  316. * Byte 0 contains the descriptor name (Resource Type)
  317. * Examine the large/small bit in the resource header
  318. */
  319. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  320. /* Large Resource Type -- bits 6:0 contain the name */
  321. return (ACPI_GET8(aml));
  322. } else {
  323. /* Small Resource Type -- bits 6:3 contain the name */
  324. return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
  325. }
  326. }
  327. /*******************************************************************************
  328. *
  329. * FUNCTION: acpi_ut_get_resource_length
  330. *
  331. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  332. *
  333. * RETURN: Byte Length
  334. *
  335. * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
  336. * definition, this does not include the size of the descriptor
  337. * header or the length field itself.
  338. *
  339. ******************************************************************************/
  340. u16 acpi_ut_get_resource_length(void *aml)
  341. {
  342. acpi_rs_length resource_length;
  343. ACPI_FUNCTION_ENTRY();
  344. /*
  345. * Byte 0 contains the descriptor name (Resource Type)
  346. * Examine the large/small bit in the resource header
  347. */
  348. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  349. /* Large Resource type -- bytes 1-2 contain the 16-bit length */
  350. ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
  351. } else {
  352. /* Small Resource type -- bits 2:0 of byte 0 contain the length */
  353. resource_length = (u16) (ACPI_GET8(aml) &
  354. ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
  355. }
  356. return (resource_length);
  357. }
  358. /*******************************************************************************
  359. *
  360. * FUNCTION: acpi_ut_get_resource_header_length
  361. *
  362. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  363. *
  364. * RETURN: Length of the AML header (depends on large/small descriptor)
  365. *
  366. * DESCRIPTION: Get the length of the header for this resource.
  367. *
  368. ******************************************************************************/
  369. u8 acpi_ut_get_resource_header_length(void *aml)
  370. {
  371. ACPI_FUNCTION_ENTRY();
  372. /* Examine the large/small bit in the resource header */
  373. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  374. return (sizeof(struct aml_resource_large_header));
  375. } else {
  376. return (sizeof(struct aml_resource_small_header));
  377. }
  378. }
  379. /*******************************************************************************
  380. *
  381. * FUNCTION: acpi_ut_get_descriptor_length
  382. *
  383. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  384. *
  385. * RETURN: Byte length
  386. *
  387. * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
  388. * length of the descriptor header and the length field itself.
  389. * Used to walk descriptor lists.
  390. *
  391. ******************************************************************************/
  392. u32 acpi_ut_get_descriptor_length(void *aml)
  393. {
  394. ACPI_FUNCTION_ENTRY();
  395. /*
  396. * Get the Resource Length (does not include header length) and add
  397. * the header length (depends on if this is a small or large resource)
  398. */
  399. return (acpi_ut_get_resource_length(aml) +
  400. acpi_ut_get_resource_header_length(aml));
  401. }
  402. /*******************************************************************************
  403. *
  404. * FUNCTION: acpi_ut_get_resource_end_tag
  405. *
  406. * PARAMETERS: obj_desc - The resource template buffer object
  407. * end_tag - Where the pointer to the end_tag is returned
  408. *
  409. * RETURN: Status, pointer to the end tag
  410. *
  411. * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
  412. * Note: allows a buffer length of zero.
  413. *
  414. ******************************************************************************/
  415. acpi_status
  416. acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc,
  417. u8 ** end_tag)
  418. {
  419. acpi_status status;
  420. u8 *aml;
  421. u8 *end_aml;
  422. ACPI_FUNCTION_TRACE("ut_get_resource_end_tag");
  423. /* Get start and end pointers */
  424. aml = obj_desc->buffer.pointer;
  425. end_aml = aml + obj_desc->buffer.length;
  426. /* Allow a buffer length of zero */
  427. if (!obj_desc->buffer.length) {
  428. *end_tag = aml;
  429. return_ACPI_STATUS(AE_OK);
  430. }
  431. /* Walk the resource template, one descriptor per iteration */
  432. while (aml < end_aml) {
  433. /* Validate the Resource Type and Resource Length */
  434. status = acpi_ut_validate_resource(aml, NULL);
  435. if (ACPI_FAILURE(status)) {
  436. return_ACPI_STATUS(status);
  437. }
  438. /* end_tag resource indicates the end of the resource template */
  439. if (acpi_ut_get_resource_type(aml) ==
  440. ACPI_RESOURCE_NAME_END_TAG) {
  441. /*
  442. * There must be at least one more byte in the buffer for
  443. * the 2nd byte of the end_tag
  444. */
  445. if ((aml + 1) >= end_aml) {
  446. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  447. }
  448. /* Return the pointer to the end_tag */
  449. *end_tag = aml;
  450. return_ACPI_STATUS(AE_OK);
  451. }
  452. /*
  453. * Point to the next resource descriptor in the AML buffer. The
  454. * descriptor length is guaranteed to be non-zero by resource
  455. * validation above.
  456. */
  457. aml += acpi_ut_get_descriptor_length(aml);
  458. }
  459. /* Did not find an end_tag resource descriptor */
  460. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  461. }