utresrc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*******************************************************************************
  2. *
  3. * Module Name: utresrc - Resource management utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, 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 <acpi/amlresrc.h>
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utresrc")
  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_bm_decode[] = {
  52. "NotBusMaster",
  53. "BusMaster"
  54. };
  55. const char *acpi_gbl_config_decode[] = {
  56. "0 - Good Configuration",
  57. "1 - Acceptable Configuration",
  58. "2 - Suboptimal Configuration",
  59. "3 - ***Invalid Configuration***",
  60. };
  61. const char *acpi_gbl_consume_decode[] = {
  62. "ResourceProducer",
  63. "ResourceConsumer"
  64. };
  65. const char *acpi_gbl_dec_decode[] = {
  66. "PosDecode",
  67. "SubDecode"
  68. };
  69. const char *acpi_gbl_he_decode[] = {
  70. "Level",
  71. "Edge"
  72. };
  73. const char *acpi_gbl_io_decode[] = {
  74. "Decode10",
  75. "Decode16"
  76. };
  77. const char *acpi_gbl_ll_decode[] = {
  78. "ActiveHigh",
  79. "ActiveLow"
  80. };
  81. const char *acpi_gbl_max_decode[] = {
  82. "MaxNotFixed",
  83. "MaxFixed"
  84. };
  85. const char *acpi_gbl_mem_decode[] = {
  86. "NonCacheable",
  87. "Cacheable",
  88. "WriteCombining",
  89. "Prefetchable"
  90. };
  91. const char *acpi_gbl_min_decode[] = {
  92. "MinNotFixed",
  93. "MinFixed"
  94. };
  95. const char *acpi_gbl_mtp_decode[] = {
  96. "AddressRangeMemory",
  97. "AddressRangeReserved",
  98. "AddressRangeACPI",
  99. "AddressRangeNVS"
  100. };
  101. const char *acpi_gbl_rng_decode[] = {
  102. "InvalidRanges",
  103. "NonISAOnlyRanges",
  104. "ISAOnlyRanges",
  105. "EntireRange"
  106. };
  107. const char *acpi_gbl_rw_decode[] = {
  108. "ReadOnly",
  109. "ReadWrite"
  110. };
  111. const char *acpi_gbl_shr_decode[] = {
  112. "Exclusive",
  113. "Shared"
  114. };
  115. const char *acpi_gbl_siz_decode[] = {
  116. "Transfer8",
  117. "Transfer8_16",
  118. "Transfer16",
  119. "InvalidSize"
  120. };
  121. const char *acpi_gbl_trs_decode[] = {
  122. "DenseTranslation",
  123. "SparseTranslation"
  124. };
  125. const char *acpi_gbl_ttp_decode[] = {
  126. "TypeStatic",
  127. "TypeTranslation"
  128. };
  129. const char *acpi_gbl_typ_decode[] = {
  130. "Compatibility",
  131. "TypeA",
  132. "TypeB",
  133. "TypeF"
  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_walk_aml_resources
  213. *
  214. * PARAMETERS: Aml - Pointer to the raw AML resource template
  215. * aml_length - Length of the entire template
  216. * user_function - Called once for each descriptor found. If
  217. * NULL, a pointer to the end_tag is returned
  218. * Context - Passed to user_function
  219. *
  220. * RETURN: Status
  221. *
  222. * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
  223. * once for each resource found.
  224. *
  225. ******************************************************************************/
  226. acpi_status
  227. acpi_ut_walk_aml_resources(u8 * aml,
  228. acpi_size aml_length,
  229. acpi_walk_aml_callback user_function, void **context)
  230. {
  231. acpi_status status;
  232. u8 *end_aml;
  233. u8 resource_index;
  234. u32 length;
  235. u32 offset = 0;
  236. ACPI_FUNCTION_TRACE(ut_walk_aml_resources);
  237. /* The absolute minimum resource template is one end_tag descriptor */
  238. if (aml_length < sizeof(struct aml_resource_end_tag)) {
  239. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  240. }
  241. /* Point to the end of the resource template buffer */
  242. end_aml = aml + aml_length;
  243. /* Walk the byte list, abort on any invalid descriptor type or length */
  244. while (aml < end_aml) {
  245. /* Validate the Resource Type and Resource Length */
  246. status = acpi_ut_validate_resource(aml, &resource_index);
  247. if (ACPI_FAILURE(status)) {
  248. return_ACPI_STATUS(status);
  249. }
  250. /* Get the length of this descriptor */
  251. length = acpi_ut_get_descriptor_length(aml);
  252. /* Invoke the user function */
  253. if (user_function) {
  254. status =
  255. user_function(aml, length, offset, resource_index,
  256. context);
  257. if (ACPI_FAILURE(status)) {
  258. return (status);
  259. }
  260. }
  261. /* An end_tag descriptor terminates this resource template */
  262. if (acpi_ut_get_resource_type(aml) ==
  263. ACPI_RESOURCE_NAME_END_TAG) {
  264. /*
  265. * There must be at least one more byte in the buffer for
  266. * the 2nd byte of the end_tag
  267. */
  268. if ((aml + 1) >= end_aml) {
  269. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  270. }
  271. /* Return the pointer to the end_tag if requested */
  272. if (!user_function) {
  273. *context = aml;
  274. }
  275. /* Normal exit */
  276. return_ACPI_STATUS(AE_OK);
  277. }
  278. aml += length;
  279. offset += length;
  280. }
  281. /* Did not find an end_tag descriptor */
  282. return (AE_AML_NO_RESOURCE_END_TAG);
  283. }
  284. /*******************************************************************************
  285. *
  286. * FUNCTION: acpi_ut_validate_resource
  287. *
  288. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  289. * return_index - Where the resource index is returned. NULL
  290. * if the index is not required.
  291. *
  292. * RETURN: Status, and optionally the Index into the global resource tables
  293. *
  294. * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
  295. * Type and Resource Length. Returns an index into the global
  296. * resource information/dispatch tables for later use.
  297. *
  298. ******************************************************************************/
  299. acpi_status acpi_ut_validate_resource(void *aml, u8 * return_index)
  300. {
  301. u8 resource_type;
  302. u8 resource_index;
  303. acpi_rs_length resource_length;
  304. acpi_rs_length minimum_resource_length;
  305. ACPI_FUNCTION_ENTRY();
  306. /*
  307. * 1) Validate the resource_type field (Byte 0)
  308. */
  309. resource_type = ACPI_GET8(aml);
  310. /*
  311. * Byte 0 contains the descriptor name (Resource Type)
  312. * Examine the large/small bit in the resource header
  313. */
  314. if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
  315. /* Verify the large resource type (name) against the max */
  316. if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
  317. return (AE_AML_INVALID_RESOURCE_TYPE);
  318. }
  319. /*
  320. * Large Resource Type -- bits 6:0 contain the name
  321. * Translate range 0x80-0x8B to index range 0x10-0x1B
  322. */
  323. resource_index = (u8) (resource_type - 0x70);
  324. } else {
  325. /*
  326. * Small Resource Type -- bits 6:3 contain the name
  327. * Shift range to index range 0x00-0x0F
  328. */
  329. resource_index = (u8)
  330. ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
  331. }
  332. /* Check validity of the resource type, zero indicates name is invalid */
  333. if (!acpi_gbl_resource_types[resource_index]) {
  334. return (AE_AML_INVALID_RESOURCE_TYPE);
  335. }
  336. /*
  337. * 2) Validate the resource_length field. This ensures that the length
  338. * is at least reasonable, and guarantees that it is non-zero.
  339. */
  340. resource_length = acpi_ut_get_resource_length(aml);
  341. minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
  342. /* Validate based upon the type of resource - fixed length or variable */
  343. switch (acpi_gbl_resource_types[resource_index]) {
  344. case ACPI_FIXED_LENGTH:
  345. /* Fixed length resource, length must match exactly */
  346. if (resource_length != minimum_resource_length) {
  347. return (AE_AML_BAD_RESOURCE_LENGTH);
  348. }
  349. break;
  350. case ACPI_VARIABLE_LENGTH:
  351. /* Variable length resource, length must be at least the minimum */
  352. if (resource_length < minimum_resource_length) {
  353. return (AE_AML_BAD_RESOURCE_LENGTH);
  354. }
  355. break;
  356. case ACPI_SMALL_VARIABLE_LENGTH:
  357. /* Small variable length resource, length can be (Min) or (Min-1) */
  358. if ((resource_length > minimum_resource_length) ||
  359. (resource_length < (minimum_resource_length - 1))) {
  360. return (AE_AML_BAD_RESOURCE_LENGTH);
  361. }
  362. break;
  363. default:
  364. /* Shouldn't happen (because of validation earlier), but be sure */
  365. return (AE_AML_INVALID_RESOURCE_TYPE);
  366. }
  367. /* Optionally return the resource table index */
  368. if (return_index) {
  369. *return_index = resource_index;
  370. }
  371. return (AE_OK);
  372. }
  373. /*******************************************************************************
  374. *
  375. * FUNCTION: acpi_ut_get_resource_type
  376. *
  377. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  378. *
  379. * RETURN: The Resource Type with no extraneous bits (except the
  380. * Large/Small descriptor bit -- this is left alone)
  381. *
  382. * DESCRIPTION: Extract the Resource Type/Name from the first byte of
  383. * a resource descriptor.
  384. *
  385. ******************************************************************************/
  386. u8 acpi_ut_get_resource_type(void *aml)
  387. {
  388. ACPI_FUNCTION_ENTRY();
  389. /*
  390. * Byte 0 contains the descriptor name (Resource Type)
  391. * Examine the large/small bit in the resource header
  392. */
  393. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  394. /* Large Resource Type -- bits 6:0 contain the name */
  395. return (ACPI_GET8(aml));
  396. } else {
  397. /* Small Resource Type -- bits 6:3 contain the name */
  398. return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
  399. }
  400. }
  401. /*******************************************************************************
  402. *
  403. * FUNCTION: acpi_ut_get_resource_length
  404. *
  405. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  406. *
  407. * RETURN: Byte Length
  408. *
  409. * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
  410. * definition, this does not include the size of the descriptor
  411. * header or the length field itself.
  412. *
  413. ******************************************************************************/
  414. u16 acpi_ut_get_resource_length(void *aml)
  415. {
  416. acpi_rs_length resource_length;
  417. ACPI_FUNCTION_ENTRY();
  418. /*
  419. * Byte 0 contains the descriptor name (Resource Type)
  420. * Examine the large/small bit in the resource header
  421. */
  422. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  423. /* Large Resource type -- bytes 1-2 contain the 16-bit length */
  424. ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
  425. } else {
  426. /* Small Resource type -- bits 2:0 of byte 0 contain the length */
  427. resource_length = (u16) (ACPI_GET8(aml) &
  428. ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
  429. }
  430. return (resource_length);
  431. }
  432. /*******************************************************************************
  433. *
  434. * FUNCTION: acpi_ut_get_resource_header_length
  435. *
  436. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  437. *
  438. * RETURN: Length of the AML header (depends on large/small descriptor)
  439. *
  440. * DESCRIPTION: Get the length of the header for this resource.
  441. *
  442. ******************************************************************************/
  443. u8 acpi_ut_get_resource_header_length(void *aml)
  444. {
  445. ACPI_FUNCTION_ENTRY();
  446. /* Examine the large/small bit in the resource header */
  447. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  448. return (sizeof(struct aml_resource_large_header));
  449. } else {
  450. return (sizeof(struct aml_resource_small_header));
  451. }
  452. }
  453. /*******************************************************************************
  454. *
  455. * FUNCTION: acpi_ut_get_descriptor_length
  456. *
  457. * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
  458. *
  459. * RETURN: Byte length
  460. *
  461. * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
  462. * length of the descriptor header and the length field itself.
  463. * Used to walk descriptor lists.
  464. *
  465. ******************************************************************************/
  466. u32 acpi_ut_get_descriptor_length(void *aml)
  467. {
  468. ACPI_FUNCTION_ENTRY();
  469. /*
  470. * Get the Resource Length (does not include header length) and add
  471. * the header length (depends on if this is a small or large resource)
  472. */
  473. return (acpi_ut_get_resource_length(aml) +
  474. acpi_ut_get_resource_header_length(aml));
  475. }
  476. /*******************************************************************************
  477. *
  478. * FUNCTION: acpi_ut_get_resource_end_tag
  479. *
  480. * PARAMETERS: obj_desc - The resource template buffer object
  481. * end_tag - Where the pointer to the end_tag is returned
  482. *
  483. * RETURN: Status, pointer to the end tag
  484. *
  485. * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
  486. * Note: allows a buffer length of zero.
  487. *
  488. ******************************************************************************/
  489. acpi_status
  490. acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc,
  491. u8 ** end_tag)
  492. {
  493. acpi_status status;
  494. ACPI_FUNCTION_TRACE(ut_get_resource_end_tag);
  495. /* Allow a buffer length of zero */
  496. if (!obj_desc->buffer.length) {
  497. *end_tag = obj_desc->buffer.pointer;
  498. return_ACPI_STATUS(AE_OK);
  499. }
  500. /* Validate the template and get a pointer to the end_tag */
  501. status = acpi_ut_walk_aml_resources(obj_desc->buffer.pointer,
  502. obj_desc->buffer.length, NULL,
  503. (void **)end_tag);
  504. return_ACPI_STATUS(status);
  505. }