rscalc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*******************************************************************************
  2. *
  3. * Module Name: rscalc - Calculate stream and list lengths
  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/acresrc.h>
  44. #include <acpi/acnamesp.h>
  45. #define _COMPONENT ACPI_RESOURCES
  46. ACPI_MODULE_NAME("rscalc")
  47. /* Local prototypes */
  48. static u8 acpi_rs_count_set_bits(u16 bit_field);
  49. static acpi_rs_length
  50. acpi_rs_struct_option_length(struct acpi_resource_source *resource_source);
  51. static u32
  52. acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length);
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_rs_count_set_bits
  56. *
  57. * PARAMETERS: bit_field - Field in which to count bits
  58. *
  59. * RETURN: Number of bits set within the field
  60. *
  61. * DESCRIPTION: Count the number of bits set in a resource field. Used for
  62. * (Short descriptor) interrupt and DMA lists.
  63. *
  64. ******************************************************************************/
  65. static u8 acpi_rs_count_set_bits(u16 bit_field)
  66. {
  67. u8 bits_set;
  68. ACPI_FUNCTION_ENTRY();
  69. for (bits_set = 0; bit_field; bits_set++) {
  70. /* Zero the least significant bit that is set */
  71. bit_field &= (u16) (bit_field - 1);
  72. }
  73. return bits_set;
  74. }
  75. /*******************************************************************************
  76. *
  77. * FUNCTION: acpi_rs_struct_option_length
  78. *
  79. * PARAMETERS: resource_source - Pointer to optional descriptor field
  80. *
  81. * RETURN: Status
  82. *
  83. * DESCRIPTION: Common code to handle optional resource_source_index and
  84. * resource_source fields in some Large descriptors. Used during
  85. * list-to-stream conversion
  86. *
  87. ******************************************************************************/
  88. static acpi_rs_length
  89. acpi_rs_struct_option_length(struct acpi_resource_source *resource_source)
  90. {
  91. ACPI_FUNCTION_ENTRY();
  92. /*
  93. * If the resource_source string is valid, return the size of the string
  94. * (string_length includes the NULL terminator) plus the size of the
  95. * resource_source_index (1).
  96. */
  97. if (resource_source->string_ptr) {
  98. return ((acpi_rs_length) (resource_source->string_length + 1));
  99. }
  100. return (0);
  101. }
  102. /*******************************************************************************
  103. *
  104. * FUNCTION: acpi_rs_stream_option_length
  105. *
  106. * PARAMETERS: resource_length - Length from the resource header
  107. * minimum_total_length - Minimum length of this resource, before
  108. * any optional fields. Includes header size
  109. *
  110. * RETURN: Length of optional string (0 if no string present)
  111. *
  112. * DESCRIPTION: Common code to handle optional resource_source_index and
  113. * resource_source fields in some Large descriptors. Used during
  114. * stream-to-list conversion
  115. *
  116. ******************************************************************************/
  117. static u32
  118. acpi_rs_stream_option_length(u32 resource_length,
  119. u32 minimum_aml_resource_length)
  120. {
  121. u32 string_length = 0;
  122. ACPI_FUNCTION_ENTRY();
  123. /*
  124. * The resource_source_index and resource_source are optional elements of some
  125. * Large-type resource descriptors.
  126. */
  127. /*
  128. * If the length of the actual resource descriptor is greater than the ACPI
  129. * spec-defined minimum length, it means that a resource_source_index exists
  130. * and is followed by a (required) null terminated string. The string length
  131. * (including the null terminator) is the resource length minus the minimum
  132. * length, minus one byte for the resource_source_index itself.
  133. */
  134. if (resource_length > minimum_aml_resource_length) {
  135. /* Compute the length of the optional string */
  136. string_length =
  137. resource_length - minimum_aml_resource_length - 1;
  138. }
  139. /*
  140. * Round the length up to a multiple of the native word in order to
  141. * guarantee that the entire resource descriptor is native word aligned
  142. */
  143. return ((u32) ACPI_ROUND_UP_TO_NATIVE_WORD(string_length));
  144. }
  145. /*******************************************************************************
  146. *
  147. * FUNCTION: acpi_rs_get_aml_length
  148. *
  149. * PARAMETERS: Resource - Pointer to the resource linked list
  150. * size_needed - Where the required size is returned
  151. *
  152. * RETURN: Status
  153. *
  154. * DESCRIPTION: Takes a linked list of internal resource descriptors and
  155. * calculates the size buffer needed to hold the corresponding
  156. * external resource byte stream.
  157. *
  158. ******************************************************************************/
  159. acpi_status
  160. acpi_rs_get_aml_length(struct acpi_resource * resource, acpi_size * size_needed)
  161. {
  162. acpi_size aml_size_needed = 0;
  163. acpi_rs_length total_size;
  164. ACPI_FUNCTION_TRACE(rs_get_aml_length);
  165. /* Traverse entire list of internal resource descriptors */
  166. while (resource) {
  167. /* Validate the descriptor type */
  168. if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
  169. return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
  170. }
  171. /* Get the base size of the (external stream) resource descriptor */
  172. total_size = acpi_gbl_aml_resource_sizes[resource->type];
  173. /*
  174. * Augment the base size for descriptors with optional and/or
  175. * variable-length fields
  176. */
  177. switch (resource->type) {
  178. case ACPI_RESOURCE_TYPE_IRQ:
  179. /* Length can be 3 or 2 */
  180. if (resource->data.irq.descriptor_length == 2) {
  181. total_size--;
  182. }
  183. break;
  184. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  185. /* Length can be 1 or 0 */
  186. if (resource->data.irq.descriptor_length == 0) {
  187. total_size--;
  188. }
  189. break;
  190. case ACPI_RESOURCE_TYPE_VENDOR:
  191. /*
  192. * Vendor Defined Resource:
  193. * For a Vendor Specific resource, if the Length is between 1 and 7
  194. * it will be created as a Small Resource data type, otherwise it
  195. * is a Large Resource data type.
  196. */
  197. if (resource->data.vendor.byte_length > 7) {
  198. /* Base size of a Large resource descriptor */
  199. total_size =
  200. sizeof(struct aml_resource_large_header);
  201. }
  202. /* Add the size of the vendor-specific data */
  203. total_size = (acpi_rs_length)
  204. (total_size + resource->data.vendor.byte_length);
  205. break;
  206. case ACPI_RESOURCE_TYPE_END_TAG:
  207. /*
  208. * End Tag:
  209. * We are done -- return the accumulated total size.
  210. */
  211. *size_needed = aml_size_needed + total_size;
  212. /* Normal exit */
  213. return_ACPI_STATUS(AE_OK);
  214. case ACPI_RESOURCE_TYPE_ADDRESS16:
  215. /*
  216. * 16-Bit Address Resource:
  217. * Add the size of the optional resource_source info
  218. */
  219. total_size = (acpi_rs_length)
  220. (total_size +
  221. acpi_rs_struct_option_length(&resource->data.
  222. address16.
  223. resource_source));
  224. break;
  225. case ACPI_RESOURCE_TYPE_ADDRESS32:
  226. /*
  227. * 32-Bit Address Resource:
  228. * Add the size of the optional resource_source info
  229. */
  230. total_size = (acpi_rs_length)
  231. (total_size +
  232. acpi_rs_struct_option_length(&resource->data.
  233. address32.
  234. resource_source));
  235. break;
  236. case ACPI_RESOURCE_TYPE_ADDRESS64:
  237. /*
  238. * 64-Bit Address Resource:
  239. * Add the size of the optional resource_source info
  240. */
  241. total_size = (acpi_rs_length)
  242. (total_size +
  243. acpi_rs_struct_option_length(&resource->data.
  244. address64.
  245. resource_source));
  246. break;
  247. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  248. /*
  249. * Extended IRQ Resource:
  250. * Add the size of each additional optional interrupt beyond the
  251. * required 1 (4 bytes for each u32 interrupt number)
  252. */
  253. total_size = (acpi_rs_length)
  254. (total_size +
  255. ((resource->data.extended_irq.interrupt_count -
  256. 1) * 4) +
  257. /* Add the size of the optional resource_source info */
  258. acpi_rs_struct_option_length(&resource->data.
  259. extended_irq.
  260. resource_source));
  261. break;
  262. default:
  263. break;
  264. }
  265. /* Update the total */
  266. aml_size_needed += total_size;
  267. /* Point to the next object */
  268. resource =
  269. ACPI_ADD_PTR(struct acpi_resource, resource,
  270. resource->length);
  271. }
  272. /* Did not find an end_tag resource descriptor */
  273. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  274. }
  275. /*******************************************************************************
  276. *
  277. * FUNCTION: acpi_rs_get_list_length
  278. *
  279. * PARAMETERS: aml_buffer - Pointer to the resource byte stream
  280. * aml_buffer_length - Size of aml_buffer
  281. * size_needed - Where the size needed is returned
  282. *
  283. * RETURN: Status
  284. *
  285. * DESCRIPTION: Takes an external resource byte stream and calculates the size
  286. * buffer needed to hold the corresponding internal resource
  287. * descriptor linked list.
  288. *
  289. ******************************************************************************/
  290. acpi_status
  291. acpi_rs_get_list_length(u8 * aml_buffer,
  292. u32 aml_buffer_length, acpi_size * size_needed)
  293. {
  294. acpi_status status;
  295. u8 *end_aml;
  296. u8 *buffer;
  297. u32 buffer_size;
  298. u16 temp16;
  299. u16 resource_length;
  300. u32 extra_struct_bytes;
  301. u8 resource_index;
  302. u8 minimum_aml_resource_length;
  303. ACPI_FUNCTION_TRACE(rs_get_list_length);
  304. *size_needed = 0;
  305. end_aml = aml_buffer + aml_buffer_length;
  306. /* Walk the list of AML resource descriptors */
  307. while (aml_buffer < end_aml) {
  308. /* Validate the Resource Type and Resource Length */
  309. status = acpi_ut_validate_resource(aml_buffer, &resource_index);
  310. if (ACPI_FAILURE(status)) {
  311. return_ACPI_STATUS(status);
  312. }
  313. /* Get the resource length and base (minimum) AML size */
  314. resource_length = acpi_ut_get_resource_length(aml_buffer);
  315. minimum_aml_resource_length =
  316. acpi_gbl_resource_aml_sizes[resource_index];
  317. /*
  318. * Augment the size for descriptors with optional
  319. * and/or variable length fields
  320. */
  321. extra_struct_bytes = 0;
  322. buffer =
  323. aml_buffer + acpi_ut_get_resource_header_length(aml_buffer);
  324. switch (acpi_ut_get_resource_type(aml_buffer)) {
  325. case ACPI_RESOURCE_NAME_IRQ:
  326. /*
  327. * IRQ Resource:
  328. * Get the number of bits set in the 16-bit IRQ mask
  329. */
  330. ACPI_MOVE_16_TO_16(&temp16, buffer);
  331. extra_struct_bytes = acpi_rs_count_set_bits(temp16);
  332. break;
  333. case ACPI_RESOURCE_NAME_DMA:
  334. /*
  335. * DMA Resource:
  336. * Get the number of bits set in the 8-bit DMA mask
  337. */
  338. extra_struct_bytes = acpi_rs_count_set_bits(*buffer);
  339. break;
  340. case ACPI_RESOURCE_NAME_VENDOR_SMALL:
  341. case ACPI_RESOURCE_NAME_VENDOR_LARGE:
  342. /*
  343. * Vendor Resource:
  344. * Get the number of vendor data bytes
  345. */
  346. extra_struct_bytes = resource_length;
  347. break;
  348. case ACPI_RESOURCE_NAME_END_TAG:
  349. /*
  350. * End Tag:
  351. * This is the normal exit, add size of end_tag
  352. */
  353. *size_needed += ACPI_RS_SIZE_MIN;
  354. return_ACPI_STATUS(AE_OK);
  355. case ACPI_RESOURCE_NAME_ADDRESS32:
  356. case ACPI_RESOURCE_NAME_ADDRESS16:
  357. case ACPI_RESOURCE_NAME_ADDRESS64:
  358. /*
  359. * Address Resource:
  360. * Add the size of the optional resource_source
  361. */
  362. extra_struct_bytes =
  363. acpi_rs_stream_option_length(resource_length,
  364. minimum_aml_resource_length);
  365. break;
  366. case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
  367. /*
  368. * Extended IRQ Resource:
  369. * Using the interrupt_table_length, add 4 bytes for each additional
  370. * interrupt. Note: at least one interrupt is required and is
  371. * included in the minimum descriptor size (reason for the -1)
  372. */
  373. extra_struct_bytes = (buffer[1] - 1) * sizeof(u32);
  374. /* Add the size of the optional resource_source */
  375. extra_struct_bytes +=
  376. acpi_rs_stream_option_length(resource_length -
  377. extra_struct_bytes,
  378. minimum_aml_resource_length);
  379. break;
  380. default:
  381. break;
  382. }
  383. /*
  384. * Update the required buffer size for the internal descriptor structs
  385. *
  386. * Important: Round the size up for the appropriate alignment. This
  387. * is a requirement on IA64.
  388. */
  389. buffer_size = acpi_gbl_resource_struct_sizes[resource_index] +
  390. extra_struct_bytes;
  391. buffer_size = (u32) ACPI_ROUND_UP_TO_NATIVE_WORD(buffer_size);
  392. *size_needed += buffer_size;
  393. ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
  394. "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
  395. acpi_ut_get_resource_type(aml_buffer),
  396. acpi_ut_get_descriptor_length(aml_buffer),
  397. buffer_size));
  398. /*
  399. * Point to the next resource within the AML stream using the length
  400. * contained in the resource descriptor header
  401. */
  402. aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
  403. }
  404. /* Did not find an end_tag resource descriptor */
  405. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  406. }
  407. /*******************************************************************************
  408. *
  409. * FUNCTION: acpi_rs_get_pci_routing_table_length
  410. *
  411. * PARAMETERS: package_object - Pointer to the package object
  412. * buffer_size_needed - u32 pointer of the size buffer
  413. * needed to properly return the
  414. * parsed data
  415. *
  416. * RETURN: Status
  417. *
  418. * DESCRIPTION: Given a package representing a PCI routing table, this
  419. * calculates the size of the corresponding linked list of
  420. * descriptions.
  421. *
  422. ******************************************************************************/
  423. acpi_status
  424. acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
  425. acpi_size * buffer_size_needed)
  426. {
  427. u32 number_of_elements;
  428. acpi_size temp_size_needed = 0;
  429. union acpi_operand_object **top_object_list;
  430. u32 index;
  431. union acpi_operand_object *package_element;
  432. union acpi_operand_object **sub_object_list;
  433. u8 name_found;
  434. u32 table_index;
  435. ACPI_FUNCTION_TRACE(rs_get_pci_routing_table_length);
  436. number_of_elements = package_object->package.count;
  437. /*
  438. * Calculate the size of the return buffer.
  439. * The base size is the number of elements * the sizes of the
  440. * structures. Additional space for the strings is added below.
  441. * The minus one is to subtract the size of the u8 Source[1]
  442. * member because it is added below.
  443. *
  444. * But each PRT_ENTRY structure has a pointer to a string and
  445. * the size of that string must be found.
  446. */
  447. top_object_list = package_object->package.elements;
  448. for (index = 0; index < number_of_elements; index++) {
  449. /* Dereference the sub-package */
  450. package_element = *top_object_list;
  451. /*
  452. * The sub_object_list will now point to an array of the
  453. * four IRQ elements: Address, Pin, Source and source_index
  454. */
  455. sub_object_list = package_element->package.elements;
  456. /* Scan the irq_table_elements for the Source Name String */
  457. name_found = FALSE;
  458. for (table_index = 0; table_index < 4 && !name_found;
  459. table_index++) {
  460. if (*sub_object_list && /* Null object allowed */
  461. ((ACPI_TYPE_STRING ==
  462. ACPI_GET_OBJECT_TYPE(*sub_object_list)) ||
  463. ((ACPI_TYPE_LOCAL_REFERENCE ==
  464. ACPI_GET_OBJECT_TYPE(*sub_object_list)) &&
  465. ((*sub_object_list)->reference.class ==
  466. ACPI_REFCLASS_NAME)))) {
  467. name_found = TRUE;
  468. } else {
  469. /* Look at the next element */
  470. sub_object_list++;
  471. }
  472. }
  473. temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
  474. /* Was a String type found? */
  475. if (name_found) {
  476. if (ACPI_GET_OBJECT_TYPE(*sub_object_list) ==
  477. ACPI_TYPE_STRING) {
  478. /*
  479. * The length String.Length field does not include the
  480. * terminating NULL, add 1
  481. */
  482. temp_size_needed += ((acpi_size)
  483. (*sub_object_list)->string.
  484. length + 1);
  485. } else {
  486. temp_size_needed +=
  487. acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
  488. if (!temp_size_needed) {
  489. return_ACPI_STATUS(AE_BAD_PARAMETER);
  490. }
  491. }
  492. } else {
  493. /*
  494. * If no name was found, then this is a NULL, which is
  495. * translated as a u32 zero.
  496. */
  497. temp_size_needed += sizeof(u32);
  498. }
  499. /* Round up the size since each element must be aligned */
  500. temp_size_needed = ACPI_ROUND_UP_TO_64BIT(temp_size_needed);
  501. /* Point to the next union acpi_operand_object */
  502. top_object_list++;
  503. }
  504. /*
  505. * Add an extra element to the end of the list, essentially a
  506. * NULL terminator
  507. */
  508. *buffer_size_needed =
  509. temp_size_needed + sizeof(struct acpi_pci_routing_table);
  510. return_ACPI_STATUS(AE_OK);
  511. }