utresrc.c 16 KB

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