rscalc.c 18 KB

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