rslist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*******************************************************************************
  2. *
  3. * Module Name: rslist - Linked list utilities
  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. #define _COMPONENT ACPI_RESOURCES
  45. ACPI_MODULE_NAME("rslist")
  46. /* Dispatch table for convert-to-stream functions */
  47. typedef
  48. acpi_status(*ACPI_STREAM_HANDLER) (struct acpi_resource * resource,
  49. u8 ** output_buffer,
  50. acpi_size * bytes_consumed);
  51. static ACPI_STREAM_HANDLER acpi_gbl_stream_dispatch[] = {
  52. acpi_rs_irq_stream, /* ACPI_RSTYPE_IRQ */
  53. acpi_rs_dma_stream, /* ACPI_RSTYPE_DMA */
  54. acpi_rs_start_depend_fns_stream, /* ACPI_RSTYPE_START_DPF */
  55. acpi_rs_end_depend_fns_stream, /* ACPI_RSTYPE_END_DPF */
  56. acpi_rs_io_stream, /* ACPI_RSTYPE_IO */
  57. acpi_rs_fixed_io_stream, /* ACPI_RSTYPE_FIXED_IO */
  58. acpi_rs_vendor_stream, /* ACPI_RSTYPE_VENDOR */
  59. acpi_rs_end_tag_stream, /* ACPI_RSTYPE_END_TAG */
  60. acpi_rs_memory24_stream, /* ACPI_RSTYPE_MEM24 */
  61. acpi_rs_memory32_range_stream, /* ACPI_RSTYPE_MEM32 */
  62. acpi_rs_fixed_memory32_stream, /* ACPI_RSTYPE_FIXED_MEM32 */
  63. acpi_rs_address16_stream, /* ACPI_RSTYPE_ADDRESS16 */
  64. acpi_rs_address32_stream, /* ACPI_RSTYPE_ADDRESS32 */
  65. acpi_rs_address64_stream, /* ACPI_RSTYPE_ADDRESS64 */
  66. acpi_rs_extended_irq_stream, /* ACPI_RSTYPE_EXT_IRQ */
  67. acpi_rs_generic_register_stream /* ACPI_RSTYPE_GENERIC_REG */
  68. };
  69. /* Dispatch tables for convert-to-resource functions */
  70. typedef
  71. acpi_status(*ACPI_RESOURCE_HANDLER) (u8 * byte_stream_buffer,
  72. acpi_size * bytes_consumed,
  73. u8 ** output_buffer,
  74. acpi_size * structure_size);
  75. static ACPI_RESOURCE_HANDLER acpi_gbl_sm_resource_dispatch[] = {
  76. NULL, /* 0x00, Reserved */
  77. NULL, /* 0x01, Reserved */
  78. NULL, /* 0x02, Reserved */
  79. NULL, /* 0x03, Reserved */
  80. acpi_rs_irq_resource, /* ACPI_RDESC_TYPE_IRQ_FORMAT */
  81. acpi_rs_dma_resource, /* ACPI_RDESC_TYPE_DMA_FORMAT */
  82. acpi_rs_start_depend_fns_resource, /* ACPI_RDESC_TYPE_START_DEPENDENT */
  83. acpi_rs_end_depend_fns_resource, /* ACPI_RDESC_TYPE_END_DEPENDENT */
  84. acpi_rs_io_resource, /* ACPI_RDESC_TYPE_IO_PORT */
  85. acpi_rs_fixed_io_resource, /* ACPI_RDESC_TYPE_FIXED_IO_PORT */
  86. NULL, /* 0x0A, Reserved */
  87. NULL, /* 0x0B, Reserved */
  88. NULL, /* 0x0C, Reserved */
  89. NULL, /* 0x0D, Reserved */
  90. acpi_rs_vendor_resource, /* ACPI_RDESC_TYPE_SMALL_VENDOR */
  91. acpi_rs_end_tag_resource /* ACPI_RDESC_TYPE_END_TAG */
  92. };
  93. static ACPI_RESOURCE_HANDLER acpi_gbl_lg_resource_dispatch[] = {
  94. NULL, /* 0x00, Reserved */
  95. acpi_rs_memory24_resource, /* ACPI_RDESC_TYPE_MEMORY_24 */
  96. acpi_rs_generic_register_resource, /* ACPI_RDESC_TYPE_GENERIC_REGISTER */
  97. NULL, /* 0x03, Reserved */
  98. acpi_rs_vendor_resource, /* ACPI_RDESC_TYPE_LARGE_VENDOR */
  99. acpi_rs_memory32_range_resource, /* ACPI_RDESC_TYPE_MEMORY_32 */
  100. acpi_rs_fixed_memory32_resource, /* ACPI_RDESC_TYPE_FIXED_MEMORY_32 */
  101. acpi_rs_address32_resource, /* ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE */
  102. acpi_rs_address16_resource, /* ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE */
  103. acpi_rs_extended_irq_resource, /* ACPI_RDESC_TYPE_EXTENDED_XRUPT */
  104. acpi_rs_address64_resource, /* ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE */
  105. acpi_rs_address64_resource /* ACPI_RDESC_TYPE_EXTENDED_ADDRESS_SPACE */
  106. };
  107. /* Local prototypes */
  108. static ACPI_RESOURCE_HANDLER acpi_rs_get_resource_handler(u8 resource_type);
  109. /*******************************************************************************
  110. *
  111. * FUNCTION: acpi_rs_get_resource_type
  112. *
  113. * PARAMETERS: resource_type - Byte 0 of a resource descriptor
  114. *
  115. * RETURN: The Resource Type with no extraneous bits (except the large/
  116. * small bit -- left alone)
  117. *
  118. * DESCRIPTION: Extract the Resource Type/Name from the first byte of
  119. * a resource descriptor.
  120. *
  121. ******************************************************************************/
  122. u8 acpi_rs_get_resource_type(u8 resource_type)
  123. {
  124. ACPI_FUNCTION_ENTRY();
  125. /* Determine if this is a small or large resource */
  126. if (resource_type & ACPI_RDESC_TYPE_LARGE) {
  127. /* Large Resource Type -- bits 6:0 contain the name */
  128. return (resource_type);
  129. } else {
  130. /* Small Resource Type -- bits 6:3 contain the name */
  131. return ((u8) (resource_type & ACPI_RDESC_SMALL_MASK));
  132. }
  133. }
  134. /*******************************************************************************
  135. *
  136. * FUNCTION: acpi_rs_get_resource_handler
  137. *
  138. * PARAMETERS: resource_type - Byte 0 of a resource descriptor
  139. *
  140. * RETURN: Pointer to the resource conversion handler
  141. *
  142. * DESCRIPTION: Extract the Resource Type/Name from the first byte of
  143. * a resource descriptor.
  144. *
  145. ******************************************************************************/
  146. static ACPI_RESOURCE_HANDLER acpi_rs_get_resource_handler(u8 resource_type)
  147. {
  148. ACPI_FUNCTION_ENTRY();
  149. /* Determine if this is a small or large resource */
  150. if (resource_type & ACPI_RDESC_TYPE_LARGE) {
  151. /* Large Resource Type -- bits 6:0 contain the name */
  152. if (resource_type > ACPI_RDESC_LARGE_MAX) {
  153. return (NULL);
  154. }
  155. return (acpi_gbl_lg_resource_dispatch[(resource_type &
  156. ACPI_RDESC_LARGE_MASK)]);
  157. } else {
  158. /* Small Resource Type -- bits 6:3 contain the name */
  159. return (acpi_gbl_sm_resource_dispatch[((resource_type &
  160. ACPI_RDESC_SMALL_MASK)
  161. >> 3)]);
  162. }
  163. }
  164. /*******************************************************************************
  165. *
  166. * FUNCTION: acpi_rs_byte_stream_to_list
  167. *
  168. * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
  169. * byte_stream_buffer_length - Length of byte_stream_buffer
  170. * output_buffer - Pointer to the buffer that will
  171. * contain the output structures
  172. *
  173. * RETURN: Status
  174. *
  175. * DESCRIPTION: Takes the resource byte stream and parses it, creating a
  176. * linked list of resources in the caller's output buffer
  177. *
  178. ******************************************************************************/
  179. acpi_status
  180. acpi_rs_byte_stream_to_list(u8 * byte_stream_buffer,
  181. u32 byte_stream_buffer_length, u8 * output_buffer)
  182. {
  183. u8 *buffer = output_buffer;
  184. acpi_status status;
  185. acpi_size bytes_parsed = 0;
  186. acpi_size bytes_consumed = 0;
  187. acpi_size structure_size = 0;
  188. struct acpi_resource *resource;
  189. ACPI_RESOURCE_HANDLER handler;
  190. ACPI_FUNCTION_TRACE("rs_byte_stream_to_list");
  191. /* Loop until end-of-buffer or an end_tag is found */
  192. while (bytes_parsed < byte_stream_buffer_length) {
  193. /* Get the handler associated with this Descriptor Type */
  194. handler = acpi_rs_get_resource_handler(*byte_stream_buffer);
  195. if (handler) {
  196. /* Convert a byte stream resource to local resource struct */
  197. status = handler(byte_stream_buffer, &bytes_consumed,
  198. &buffer, &structure_size);
  199. } else {
  200. /* Invalid resource type */
  201. status = AE_AML_INVALID_RESOURCE_TYPE;
  202. }
  203. if (ACPI_FAILURE(status)) {
  204. return_ACPI_STATUS(status);
  205. }
  206. /* Set the aligned length of the new resource descriptor */
  207. resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
  208. resource->length =
  209. (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
  210. /* Normal exit on completion of an end_tag resource descriptor */
  211. if (acpi_rs_get_resource_type(*byte_stream_buffer) ==
  212. ACPI_RDESC_TYPE_END_TAG) {
  213. return_ACPI_STATUS(AE_OK);
  214. }
  215. /* Update counter and point to the next input resource */
  216. bytes_parsed += bytes_consumed;
  217. byte_stream_buffer += bytes_consumed;
  218. /* Point to the next structure in the output buffer */
  219. buffer += ACPI_ALIGN_RESOURCE_SIZE(structure_size);
  220. }
  221. /* Completed buffer, but did not find an end_tag resource descriptor */
  222. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  223. }
  224. /*******************************************************************************
  225. *
  226. * FUNCTION: acpi_rs_list_to_byte_stream
  227. *
  228. * PARAMETERS: Resource - Pointer to the resource linked list
  229. * byte_steam_size_needed - Calculated size of the byte stream
  230. * needed from calling
  231. * acpi_rs_get_byte_stream_length()
  232. * The size of the output_buffer is
  233. * guaranteed to be >=
  234. * byte_stream_size_needed
  235. * output_buffer - Pointer to the buffer that will
  236. * contain the byte stream
  237. *
  238. * RETURN: Status
  239. *
  240. * DESCRIPTION: Takes the resource linked list and parses it, creating a
  241. * byte stream of resources in the caller's output buffer
  242. *
  243. ******************************************************************************/
  244. acpi_status
  245. acpi_rs_list_to_byte_stream(struct acpi_resource *resource,
  246. acpi_size byte_stream_size_needed,
  247. u8 * output_buffer)
  248. {
  249. u8 *buffer = output_buffer;
  250. acpi_size bytes_consumed = 0;
  251. acpi_status status;
  252. ACPI_FUNCTION_TRACE("rs_list_to_byte_stream");
  253. /* Convert each resource descriptor in the list */
  254. while (1) {
  255. /* Validate Type before dispatch */
  256. if (resource->type > ACPI_RSTYPE_MAX) {
  257. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  258. "Invalid descriptor type (%X) in resource list\n",
  259. resource->type));
  260. return_ACPI_STATUS(AE_BAD_DATA);
  261. }
  262. /* Perform the conversion, per resource type */
  263. status = acpi_gbl_stream_dispatch[resource->type] (resource,
  264. &buffer,
  265. &bytes_consumed);
  266. if (ACPI_FAILURE(status)) {
  267. return_ACPI_STATUS(status);
  268. }
  269. /* Check for end-of-list */
  270. if (resource->type == ACPI_RSTYPE_END_TAG) {
  271. /* An End Tag indicates the end of the Resource Template */
  272. return_ACPI_STATUS(AE_OK);
  273. }
  274. /* Set the Buffer to point to the next (output) resource descriptor */
  275. buffer += bytes_consumed;
  276. /* Point to the next input resource object */
  277. resource = ACPI_PTR_ADD(struct acpi_resource,
  278. resource, resource->length);
  279. }
  280. }