rscalc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*******************************************************************************
  2. *
  3. * Module Name: rscalc - Calculate stream and list lengths
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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 "acresrc.h"
  45. #include "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_rs_length
  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 &= (u16) (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_rs_length
  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_rs_length)(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
  126. * some Large-type resource descriptors.
  127. */
  128. /*
  129. * If the length of the actual resource descriptor is greater than the
  130. * ACPI spec-defined minimum length, it means that a resource_source_index
  131. * exists and is followed by a (required) null terminated string. The
  132. * string length (including the null terminator) is the resource length
  133. * minus the minimum length, minus one byte for the resource_source_index
  134. * itself.
  135. */
  136. if (resource_length > minimum_aml_resource_length) {
  137. /* Compute the length of the optional string */
  138. string_length =
  139. resource_length - minimum_aml_resource_length - 1;
  140. }
  141. /*
  142. * Round the length up to a multiple of the native word in order to
  143. * guarantee that the entire resource descriptor is native word aligned
  144. */
  145. return ((u32) ACPI_ROUND_UP_TO_NATIVE_WORD(string_length));
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_rs_get_aml_length
  150. *
  151. * PARAMETERS: resource - Pointer to the resource linked list
  152. * resource_list_size - Size of the resource linked list
  153. * size_needed - Where the required size is returned
  154. *
  155. * RETURN: Status
  156. *
  157. * DESCRIPTION: Takes a linked list of internal resource descriptors and
  158. * calculates the size buffer needed to hold the corresponding
  159. * external resource byte stream.
  160. *
  161. ******************************************************************************/
  162. acpi_status
  163. acpi_rs_get_aml_length(struct acpi_resource *resource,
  164. acpi_size resource_list_size, acpi_size *size_needed)
  165. {
  166. acpi_size aml_size_needed = 0;
  167. struct acpi_resource *resource_end;
  168. acpi_rs_length total_size;
  169. ACPI_FUNCTION_TRACE(rs_get_aml_length);
  170. /* Traverse entire list of internal resource descriptors */
  171. resource_end =
  172. ACPI_ADD_PTR(struct acpi_resource, resource, resource_list_size);
  173. while (resource < resource_end) {
  174. /* Validate the descriptor type */
  175. if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
  176. return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
  177. }
  178. /* Sanity check the length. It must not be zero, or we loop forever */
  179. if (!resource->length) {
  180. return_ACPI_STATUS(AE_AML_BAD_RESOURCE_LENGTH);
  181. }
  182. /* Get the base size of the (external stream) resource descriptor */
  183. total_size = acpi_gbl_aml_resource_sizes[resource->type];
  184. /*
  185. * Augment the base size for descriptors with optional and/or
  186. * variable-length fields
  187. */
  188. switch (resource->type) {
  189. case ACPI_RESOURCE_TYPE_IRQ:
  190. /* Length can be 3 or 2 */
  191. if (resource->data.irq.descriptor_length == 2) {
  192. total_size--;
  193. }
  194. break;
  195. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  196. /* Length can be 1 or 0 */
  197. if (resource->data.irq.descriptor_length == 0) {
  198. total_size--;
  199. }
  200. break;
  201. case ACPI_RESOURCE_TYPE_VENDOR:
  202. /*
  203. * Vendor Defined Resource:
  204. * For a Vendor Specific resource, if the Length is between 1 and 7
  205. * it will be created as a Small Resource data type, otherwise it
  206. * is a Large Resource data type.
  207. */
  208. if (resource->data.vendor.byte_length > 7) {
  209. /* Base size of a Large resource descriptor */
  210. total_size =
  211. sizeof(struct aml_resource_large_header);
  212. }
  213. /* Add the size of the vendor-specific data */
  214. total_size = (acpi_rs_length)
  215. (total_size + resource->data.vendor.byte_length);
  216. break;
  217. case ACPI_RESOURCE_TYPE_END_TAG:
  218. /*
  219. * End Tag:
  220. * We are done -- return the accumulated total size.
  221. */
  222. *size_needed = aml_size_needed + total_size;
  223. /* Normal exit */
  224. return_ACPI_STATUS(AE_OK);
  225. case ACPI_RESOURCE_TYPE_ADDRESS16:
  226. /*
  227. * 16-Bit Address Resource:
  228. * Add the size of the optional resource_source info
  229. */
  230. total_size = (acpi_rs_length)(total_size +
  231. acpi_rs_struct_option_length
  232. (&resource->data.
  233. address16.
  234. resource_source));
  235. break;
  236. case ACPI_RESOURCE_TYPE_ADDRESS32:
  237. /*
  238. * 32-Bit Address Resource:
  239. * Add the size of the optional resource_source info
  240. */
  241. total_size = (acpi_rs_length)(total_size +
  242. acpi_rs_struct_option_length
  243. (&resource->data.
  244. address32.
  245. resource_source));
  246. break;
  247. case ACPI_RESOURCE_TYPE_ADDRESS64:
  248. /*
  249. * 64-Bit Address Resource:
  250. * Add the size of the optional resource_source info
  251. */
  252. total_size = (acpi_rs_length)(total_size +
  253. acpi_rs_struct_option_length
  254. (&resource->data.
  255. address64.
  256. resource_source));
  257. break;
  258. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  259. /*
  260. * Extended IRQ Resource:
  261. * Add the size of each additional optional interrupt beyond the
  262. * required 1 (4 bytes for each u32 interrupt number)
  263. */
  264. total_size = (acpi_rs_length)(total_size +
  265. ((resource->data.
  266. extended_irq.
  267. interrupt_count -
  268. 1) * 4) +
  269. /* Add the size of the optional resource_source info */
  270. acpi_rs_struct_option_length
  271. (&resource->data.
  272. extended_irq.
  273. resource_source));
  274. break;
  275. case ACPI_RESOURCE_TYPE_GPIO:
  276. total_size = (acpi_rs_length)(total_size +
  277. (resource->data.gpio.
  278. pin_table_length * 2) +
  279. resource->data.gpio.
  280. resource_source.
  281. string_length +
  282. resource->data.gpio.
  283. vendor_length);
  284. break;
  285. case ACPI_RESOURCE_TYPE_PIN_FUNCTION:
  286. total_size = (acpi_rs_length)(total_size +
  287. (resource->data.
  288. pin_function.
  289. pin_table_length * 2) +
  290. resource->data.
  291. pin_function.
  292. resource_source.
  293. string_length +
  294. resource->data.
  295. pin_function.
  296. vendor_length);
  297. break;
  298. case ACPI_RESOURCE_TYPE_SERIAL_BUS:
  299. total_size =
  300. acpi_gbl_aml_resource_serial_bus_sizes[resource->
  301. data.
  302. common_serial_bus.
  303. type];
  304. total_size = (acpi_rs_length)(total_size +
  305. resource->data.
  306. i2c_serial_bus.
  307. resource_source.
  308. string_length +
  309. resource->data.
  310. i2c_serial_bus.
  311. vendor_length);
  312. break;
  313. case ACPI_RESOURCE_TYPE_PIN_CONFIG:
  314. total_size = (acpi_rs_length)(total_size +
  315. (resource->data.
  316. pin_config.
  317. pin_table_length * 2) +
  318. resource->data.pin_config.
  319. resource_source.
  320. string_length +
  321. resource->data.pin_config.
  322. vendor_length);
  323. break;
  324. case ACPI_RESOURCE_TYPE_PIN_GROUP:
  325. total_size = (acpi_rs_length)(total_size +
  326. (resource->data.pin_group.
  327. pin_table_length * 2) +
  328. resource->data.pin_group.
  329. resource_label.
  330. string_length +
  331. resource->data.pin_group.
  332. vendor_length);
  333. break;
  334. case ACPI_RESOURCE_TYPE_PIN_GROUP_FUNCTION:
  335. total_size = (acpi_rs_length)(total_size +
  336. resource->data.
  337. pin_group_function.
  338. resource_source.
  339. string_length +
  340. resource->data.
  341. pin_group_function.
  342. resource_source_label.
  343. string_length +
  344. resource->data.
  345. pin_group_function.
  346. vendor_length);
  347. break;
  348. case ACPI_RESOURCE_TYPE_PIN_GROUP_CONFIG:
  349. total_size = (acpi_rs_length)(total_size +
  350. resource->data.
  351. pin_group_config.
  352. resource_source.
  353. string_length +
  354. resource->data.
  355. pin_group_config.
  356. resource_source_label.
  357. string_length +
  358. resource->data.
  359. pin_group_config.
  360. vendor_length);
  361. break;
  362. default:
  363. break;
  364. }
  365. /* Update the total */
  366. aml_size_needed += total_size;
  367. /* Point to the next object */
  368. resource =
  369. ACPI_ADD_PTR(struct acpi_resource, resource,
  370. resource->length);
  371. }
  372. /* Did not find an end_tag resource descriptor */
  373. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  374. }
  375. /*******************************************************************************
  376. *
  377. * FUNCTION: acpi_rs_get_list_length
  378. *
  379. * PARAMETERS: aml_buffer - Pointer to the resource byte stream
  380. * aml_buffer_length - Size of aml_buffer
  381. * size_needed - Where the size needed is returned
  382. *
  383. * RETURN: Status
  384. *
  385. * DESCRIPTION: Takes an external resource byte stream and calculates the size
  386. * buffer needed to hold the corresponding internal resource
  387. * descriptor linked list.
  388. *
  389. ******************************************************************************/
  390. acpi_status
  391. acpi_rs_get_list_length(u8 *aml_buffer,
  392. u32 aml_buffer_length, acpi_size *size_needed)
  393. {
  394. acpi_status status;
  395. u8 *end_aml;
  396. u8 *buffer;
  397. u32 buffer_size;
  398. u16 temp16;
  399. u16 resource_length;
  400. u32 extra_struct_bytes;
  401. u8 resource_index;
  402. u8 minimum_aml_resource_length;
  403. union aml_resource *aml_resource;
  404. ACPI_FUNCTION_TRACE(rs_get_list_length);
  405. *size_needed = ACPI_RS_SIZE_MIN; /* Minimum size is one end_tag */
  406. end_aml = aml_buffer + aml_buffer_length;
  407. /* Walk the list of AML resource descriptors */
  408. while (aml_buffer < end_aml) {
  409. /* Validate the Resource Type and Resource Length */
  410. status =
  411. acpi_ut_validate_resource(NULL, aml_buffer,
  412. &resource_index);
  413. if (ACPI_FAILURE(status)) {
  414. /*
  415. * Exit on failure. Cannot continue because the descriptor length
  416. * may be bogus also.
  417. */
  418. return_ACPI_STATUS(status);
  419. }
  420. aml_resource = (void *)aml_buffer;
  421. /* Get the resource length and base (minimum) AML size */
  422. resource_length = acpi_ut_get_resource_length(aml_buffer);
  423. minimum_aml_resource_length =
  424. acpi_gbl_resource_aml_sizes[resource_index];
  425. /*
  426. * Augment the size for descriptors with optional
  427. * and/or variable length fields
  428. */
  429. extra_struct_bytes = 0;
  430. buffer =
  431. aml_buffer + acpi_ut_get_resource_header_length(aml_buffer);
  432. switch (acpi_ut_get_resource_type(aml_buffer)) {
  433. case ACPI_RESOURCE_NAME_IRQ:
  434. /*
  435. * IRQ Resource:
  436. * Get the number of bits set in the 16-bit IRQ mask
  437. */
  438. ACPI_MOVE_16_TO_16(&temp16, buffer);
  439. extra_struct_bytes = acpi_rs_count_set_bits(temp16);
  440. break;
  441. case ACPI_RESOURCE_NAME_DMA:
  442. /*
  443. * DMA Resource:
  444. * Get the number of bits set in the 8-bit DMA mask
  445. */
  446. extra_struct_bytes = acpi_rs_count_set_bits(*buffer);
  447. break;
  448. case ACPI_RESOURCE_NAME_VENDOR_SMALL:
  449. case ACPI_RESOURCE_NAME_VENDOR_LARGE:
  450. /*
  451. * Vendor Resource:
  452. * Get the number of vendor data bytes
  453. */
  454. extra_struct_bytes = resource_length;
  455. /*
  456. * There is already one byte included in the minimum
  457. * descriptor size. If there are extra struct bytes,
  458. * subtract one from the count.
  459. */
  460. if (extra_struct_bytes) {
  461. extra_struct_bytes--;
  462. }
  463. break;
  464. case ACPI_RESOURCE_NAME_END_TAG:
  465. /*
  466. * End Tag: This is the normal exit
  467. */
  468. return_ACPI_STATUS(AE_OK);
  469. case ACPI_RESOURCE_NAME_ADDRESS32:
  470. case ACPI_RESOURCE_NAME_ADDRESS16:
  471. case ACPI_RESOURCE_NAME_ADDRESS64:
  472. /*
  473. * Address Resource:
  474. * Add the size of the optional resource_source
  475. */
  476. extra_struct_bytes =
  477. acpi_rs_stream_option_length(resource_length,
  478. minimum_aml_resource_length);
  479. break;
  480. case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
  481. /*
  482. * Extended IRQ Resource:
  483. * Using the interrupt_table_length, add 4 bytes for each additional
  484. * interrupt. Note: at least one interrupt is required and is
  485. * included in the minimum descriptor size (reason for the -1)
  486. */
  487. extra_struct_bytes = (buffer[1] - 1) * sizeof(u32);
  488. /* Add the size of the optional resource_source */
  489. extra_struct_bytes +=
  490. acpi_rs_stream_option_length(resource_length -
  491. extra_struct_bytes,
  492. minimum_aml_resource_length);
  493. break;
  494. case ACPI_RESOURCE_NAME_GPIO:
  495. /* Vendor data is optional */
  496. if (aml_resource->gpio.vendor_length) {
  497. extra_struct_bytes +=
  498. aml_resource->gpio.vendor_offset -
  499. aml_resource->gpio.pin_table_offset +
  500. aml_resource->gpio.vendor_length;
  501. } else {
  502. extra_struct_bytes +=
  503. aml_resource->large_header.resource_length +
  504. sizeof(struct aml_resource_large_header) -
  505. aml_resource->gpio.pin_table_offset;
  506. }
  507. break;
  508. case ACPI_RESOURCE_NAME_PIN_FUNCTION:
  509. /* Vendor data is optional */
  510. if (aml_resource->pin_function.vendor_length) {
  511. extra_struct_bytes +=
  512. aml_resource->pin_function.vendor_offset -
  513. aml_resource->pin_function.
  514. pin_table_offset +
  515. aml_resource->pin_function.vendor_length;
  516. } else {
  517. extra_struct_bytes +=
  518. aml_resource->large_header.resource_length +
  519. sizeof(struct aml_resource_large_header) -
  520. aml_resource->pin_function.pin_table_offset;
  521. }
  522. break;
  523. case ACPI_RESOURCE_NAME_SERIAL_BUS:
  524. minimum_aml_resource_length =
  525. acpi_gbl_resource_aml_serial_bus_sizes
  526. [aml_resource->common_serial_bus.type];
  527. extra_struct_bytes +=
  528. aml_resource->common_serial_bus.resource_length -
  529. minimum_aml_resource_length;
  530. break;
  531. case ACPI_RESOURCE_NAME_PIN_CONFIG:
  532. /* Vendor data is optional */
  533. if (aml_resource->pin_config.vendor_length) {
  534. extra_struct_bytes +=
  535. aml_resource->pin_config.vendor_offset -
  536. aml_resource->pin_config.pin_table_offset +
  537. aml_resource->pin_config.vendor_length;
  538. } else {
  539. extra_struct_bytes +=
  540. aml_resource->large_header.resource_length +
  541. sizeof(struct aml_resource_large_header) -
  542. aml_resource->pin_config.pin_table_offset;
  543. }
  544. break;
  545. case ACPI_RESOURCE_NAME_PIN_GROUP:
  546. extra_struct_bytes +=
  547. aml_resource->pin_group.vendor_offset -
  548. aml_resource->pin_group.pin_table_offset +
  549. aml_resource->pin_group.vendor_length;
  550. break;
  551. case ACPI_RESOURCE_NAME_PIN_GROUP_FUNCTION:
  552. extra_struct_bytes +=
  553. aml_resource->pin_group_function.vendor_offset -
  554. aml_resource->pin_group_function.res_source_offset +
  555. aml_resource->pin_group_function.vendor_length;
  556. break;
  557. case ACPI_RESOURCE_NAME_PIN_GROUP_CONFIG:
  558. extra_struct_bytes +=
  559. aml_resource->pin_group_config.vendor_offset -
  560. aml_resource->pin_group_config.res_source_offset +
  561. aml_resource->pin_group_config.vendor_length;
  562. break;
  563. default:
  564. break;
  565. }
  566. /*
  567. * Update the required buffer size for the internal descriptor structs
  568. *
  569. * Important: Round the size up for the appropriate alignment. This
  570. * is a requirement on IA64.
  571. */
  572. if (acpi_ut_get_resource_type(aml_buffer) ==
  573. ACPI_RESOURCE_NAME_SERIAL_BUS) {
  574. buffer_size =
  575. acpi_gbl_resource_struct_serial_bus_sizes
  576. [aml_resource->common_serial_bus.type] +
  577. extra_struct_bytes;
  578. } else {
  579. buffer_size =
  580. acpi_gbl_resource_struct_sizes[resource_index] +
  581. extra_struct_bytes;
  582. }
  583. buffer_size = (u32)ACPI_ROUND_UP_TO_NATIVE_WORD(buffer_size);
  584. *size_needed += buffer_size;
  585. ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
  586. "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
  587. acpi_ut_get_resource_type(aml_buffer),
  588. acpi_ut_get_descriptor_length(aml_buffer),
  589. buffer_size));
  590. /*
  591. * Point to the next resource within the AML stream using the length
  592. * contained in the resource descriptor header
  593. */
  594. aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
  595. }
  596. /* Did not find an end_tag resource descriptor */
  597. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  598. }
  599. /*******************************************************************************
  600. *
  601. * FUNCTION: acpi_rs_get_pci_routing_table_length
  602. *
  603. * PARAMETERS: package_object - Pointer to the package object
  604. * buffer_size_needed - u32 pointer of the size buffer
  605. * needed to properly return the
  606. * parsed data
  607. *
  608. * RETURN: Status
  609. *
  610. * DESCRIPTION: Given a package representing a PCI routing table, this
  611. * calculates the size of the corresponding linked list of
  612. * descriptions.
  613. *
  614. ******************************************************************************/
  615. acpi_status
  616. acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
  617. acpi_size *buffer_size_needed)
  618. {
  619. u32 number_of_elements;
  620. acpi_size temp_size_needed = 0;
  621. union acpi_operand_object **top_object_list;
  622. u32 index;
  623. union acpi_operand_object *package_element;
  624. union acpi_operand_object **sub_object_list;
  625. u8 name_found;
  626. u32 table_index;
  627. ACPI_FUNCTION_TRACE(rs_get_pci_routing_table_length);
  628. number_of_elements = package_object->package.count;
  629. /*
  630. * Calculate the size of the return buffer.
  631. * The base size is the number of elements * the sizes of the
  632. * structures. Additional space for the strings is added below.
  633. * The minus one is to subtract the size of the u8 Source[1]
  634. * member because it is added below.
  635. *
  636. * But each PRT_ENTRY structure has a pointer to a string and
  637. * the size of that string must be found.
  638. */
  639. top_object_list = package_object->package.elements;
  640. for (index = 0; index < number_of_elements; index++) {
  641. /* Dereference the subpackage */
  642. package_element = *top_object_list;
  643. /* We must have a valid Package object */
  644. if (!package_element ||
  645. (package_element->common.type != ACPI_TYPE_PACKAGE)) {
  646. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  647. }
  648. /*
  649. * The sub_object_list will now point to an array of the
  650. * four IRQ elements: Address, Pin, Source and source_index
  651. */
  652. sub_object_list = package_element->package.elements;
  653. /* Scan the irq_table_elements for the Source Name String */
  654. name_found = FALSE;
  655. for (table_index = 0;
  656. table_index < package_element->package.count
  657. && !name_found; table_index++) {
  658. if (*sub_object_list && /* Null object allowed */
  659. ((ACPI_TYPE_STRING ==
  660. (*sub_object_list)->common.type) ||
  661. ((ACPI_TYPE_LOCAL_REFERENCE ==
  662. (*sub_object_list)->common.type) &&
  663. ((*sub_object_list)->reference.class ==
  664. ACPI_REFCLASS_NAME)))) {
  665. name_found = TRUE;
  666. } else {
  667. /* Look at the next element */
  668. sub_object_list++;
  669. }
  670. }
  671. temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
  672. /* Was a String type found? */
  673. if (name_found) {
  674. if ((*sub_object_list)->common.type == ACPI_TYPE_STRING) {
  675. /*
  676. * The length String.Length field does not include the
  677. * terminating NULL, add 1
  678. */
  679. temp_size_needed += ((acpi_size)
  680. (*sub_object_list)->string.
  681. length + 1);
  682. } else {
  683. temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
  684. }
  685. } else {
  686. /*
  687. * If no name was found, then this is a NULL, which is
  688. * translated as a u32 zero.
  689. */
  690. temp_size_needed += sizeof(u32);
  691. }
  692. /* Round up the size since each element must be aligned */
  693. temp_size_needed = ACPI_ROUND_UP_TO_64BIT(temp_size_needed);
  694. /* Point to the next union acpi_operand_object */
  695. top_object_list++;
  696. }
  697. /*
  698. * Add an extra element to the end of the list, essentially a
  699. * NULL terminator
  700. */
  701. *buffer_size_needed =
  702. temp_size_needed + sizeof(struct acpi_pci_routing_table);
  703. return_ACPI_STATUS(AE_OK);
  704. }