rscreate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*******************************************************************************
  2. *
  3. * Module Name: rscreate - Create resource lists/tables
  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 ("rscreate")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_rs_create_resource_list
  51. *
  52. * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
  53. * output_buffer - Pointer to the user's buffer
  54. *
  55. * RETURN: Status - AE_OK if okay, else a valid acpi_status code
  56. * If output_buffer is not large enough, output_buffer_length
  57. * indicates how large output_buffer should be, else it
  58. * indicates how may u8 elements of output_buffer are valid.
  59. *
  60. * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
  61. * execution and parses the stream to create a linked list
  62. * of device resources.
  63. *
  64. ******************************************************************************/
  65. acpi_status
  66. acpi_rs_create_resource_list (
  67. union acpi_operand_object *byte_stream_buffer,
  68. struct acpi_buffer *output_buffer)
  69. {
  70. acpi_status status;
  71. u8 *byte_stream_start;
  72. acpi_size list_size_needed = 0;
  73. u32 byte_stream_buffer_length;
  74. ACPI_FUNCTION_TRACE ("rs_create_resource_list");
  75. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "byte_stream_buffer = %p\n",
  76. byte_stream_buffer));
  77. /* Params already validated, so we don't re-validate here */
  78. byte_stream_buffer_length = byte_stream_buffer->buffer.length;
  79. byte_stream_start = byte_stream_buffer->buffer.pointer;
  80. /*
  81. * Pass the byte_stream_buffer into a module that can calculate
  82. * the buffer size needed for the linked list
  83. */
  84. status = acpi_rs_get_list_length (byte_stream_start, byte_stream_buffer_length,
  85. &list_size_needed);
  86. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X list_size_needed=%X\n",
  87. status, (u32) list_size_needed));
  88. if (ACPI_FAILURE (status)) {
  89. return_ACPI_STATUS (status);
  90. }
  91. /* Validate/Allocate/Clear caller buffer */
  92. status = acpi_ut_initialize_buffer (output_buffer, list_size_needed);
  93. if (ACPI_FAILURE (status)) {
  94. return_ACPI_STATUS (status);
  95. }
  96. /* Do the conversion */
  97. status = acpi_rs_byte_stream_to_list (byte_stream_start, byte_stream_buffer_length,
  98. output_buffer->pointer);
  99. if (ACPI_FAILURE (status)) {
  100. return_ACPI_STATUS (status);
  101. }
  102. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "output_buffer %p Length %X\n",
  103. output_buffer->pointer, (u32) output_buffer->length));
  104. return_ACPI_STATUS (AE_OK);
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_rs_create_pci_routing_table
  109. *
  110. * PARAMETERS: package_object - Pointer to an union acpi_operand_object
  111. * package
  112. * output_buffer - Pointer to the user's buffer
  113. *
  114. * RETURN: Status AE_OK if okay, else a valid acpi_status code.
  115. * If the output_buffer is too small, the error will be
  116. * AE_BUFFER_OVERFLOW and output_buffer->Length will point
  117. * to the size buffer needed.
  118. *
  119. * DESCRIPTION: Takes the union acpi_operand_object package and creates a
  120. * linked list of PCI interrupt descriptions
  121. *
  122. * NOTE: It is the caller's responsibility to ensure that the start of the
  123. * output buffer is aligned properly (if necessary).
  124. *
  125. ******************************************************************************/
  126. acpi_status
  127. acpi_rs_create_pci_routing_table (
  128. union acpi_operand_object *package_object,
  129. struct acpi_buffer *output_buffer)
  130. {
  131. u8 *buffer;
  132. union acpi_operand_object **top_object_list;
  133. union acpi_operand_object **sub_object_list;
  134. union acpi_operand_object *obj_desc;
  135. acpi_size buffer_size_needed = 0;
  136. u32 number_of_elements;
  137. u32 index;
  138. struct acpi_pci_routing_table *user_prt;
  139. struct acpi_namespace_node *node;
  140. acpi_status status;
  141. struct acpi_buffer path_buffer;
  142. ACPI_FUNCTION_TRACE ("rs_create_pci_routing_table");
  143. /* Params already validated, so we don't re-validate here */
  144. /* Get the required buffer length */
  145. status = acpi_rs_get_pci_routing_table_length (package_object,
  146. &buffer_size_needed);
  147. if (ACPI_FAILURE (status)) {
  148. return_ACPI_STATUS (status);
  149. }
  150. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "buffer_size_needed = %X\n",
  151. (u32) buffer_size_needed));
  152. /* Validate/Allocate/Clear caller buffer */
  153. status = acpi_ut_initialize_buffer (output_buffer, buffer_size_needed);
  154. if (ACPI_FAILURE (status)) {
  155. return_ACPI_STATUS (status);
  156. }
  157. /*
  158. * Loop through the ACPI_INTERNAL_OBJECTS - Each object
  159. * should be a package that in turn contains an
  160. * acpi_integer Address, a u8 Pin, a Name and a u8 source_index.
  161. */
  162. top_object_list = package_object->package.elements;
  163. number_of_elements = package_object->package.count;
  164. buffer = output_buffer->pointer;
  165. user_prt = ACPI_CAST_PTR (struct acpi_pci_routing_table, buffer);
  166. for (index = 0; index < number_of_elements; index++) {
  167. /*
  168. * Point user_prt past this current structure
  169. *
  170. * NOTE: On the first iteration, user_prt->Length will
  171. * be zero because we cleared the return buffer earlier
  172. */
  173. buffer += user_prt->length;
  174. user_prt = ACPI_CAST_PTR (struct acpi_pci_routing_table, buffer);
  175. /*
  176. * Fill in the Length field with the information we have at this point.
  177. * The minus four is to subtract the size of the u8 Source[4] member
  178. * because it is added below.
  179. */
  180. user_prt->length = (sizeof (struct acpi_pci_routing_table) - 4);
  181. /* Each element of the top-level package must also be a package */
  182. if (ACPI_GET_OBJECT_TYPE (*top_object_list) != ACPI_TYPE_PACKAGE) {
  183. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  184. "(PRT[%X]) Need sub-package, found %s\n",
  185. index, acpi_ut_get_object_type_name (*top_object_list)));
  186. return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
  187. }
  188. /* Each sub-package must be of length 4 */
  189. if ((*top_object_list)->package.count != 4) {
  190. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  191. "(PRT[%X]) Need package of length 4, found length %d\n",
  192. index, (*top_object_list)->package.count));
  193. return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
  194. }
  195. /*
  196. * Dereference the sub-package.
  197. * The sub_object_list will now point to an array of the four IRQ
  198. * elements: [Address, Pin, Source, source_index]
  199. */
  200. sub_object_list = (*top_object_list)->package.elements;
  201. /* 1) First subobject: Dereference the PRT.Address */
  202. obj_desc = sub_object_list[0];
  203. if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
  204. user_prt->address = obj_desc->integer.value;
  205. }
  206. else {
  207. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  208. "(PRT[%X].Address) Need Integer, found %s\n",
  209. index, acpi_ut_get_object_type_name (obj_desc)));
  210. return_ACPI_STATUS (AE_BAD_DATA);
  211. }
  212. /* 2) Second subobject: Dereference the PRT.Pin */
  213. obj_desc = sub_object_list[1];
  214. if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
  215. user_prt->pin = (u32) obj_desc->integer.value;
  216. }
  217. else {
  218. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  219. "(PRT[%X].Pin) Need Integer, found %s\n",
  220. index, acpi_ut_get_object_type_name (obj_desc)));
  221. return_ACPI_STATUS (AE_BAD_DATA);
  222. }
  223. /* 3) Third subobject: Dereference the PRT.source_name */
  224. obj_desc = sub_object_list[2];
  225. switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  226. case ACPI_TYPE_LOCAL_REFERENCE:
  227. if (obj_desc->reference.opcode != AML_INT_NAMEPATH_OP) {
  228. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  229. "(PRT[%X].Source) Need name, found reference op %X\n",
  230. index, obj_desc->reference.opcode));
  231. return_ACPI_STATUS (AE_BAD_DATA);
  232. }
  233. node = obj_desc->reference.node;
  234. /* Use *remaining* length of the buffer as max for pathname */
  235. path_buffer.length = output_buffer->length -
  236. (u32) ((u8 *) user_prt->source -
  237. (u8 *) output_buffer->pointer);
  238. path_buffer.pointer = user_prt->source;
  239. status = acpi_ns_handle_to_pathname ((acpi_handle) node, &path_buffer);
  240. /* +1 to include null terminator */
  241. user_prt->length += (u32) ACPI_STRLEN (user_prt->source) + 1;
  242. break;
  243. case ACPI_TYPE_STRING:
  244. ACPI_STRCPY (user_prt->source, obj_desc->string.pointer);
  245. /*
  246. * Add to the Length field the length of the string
  247. * (add 1 for terminator)
  248. */
  249. user_prt->length += obj_desc->string.length + 1;
  250. break;
  251. case ACPI_TYPE_INTEGER:
  252. /*
  253. * If this is a number, then the Source Name is NULL, since the
  254. * entire buffer was zeroed out, we can leave this alone.
  255. *
  256. * Add to the Length field the length of the u32 NULL
  257. */
  258. user_prt->length += sizeof (u32);
  259. break;
  260. default:
  261. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  262. "(PRT[%X].Source) Need Ref/String/Integer, found %s\n",
  263. index, acpi_ut_get_object_type_name (obj_desc)));
  264. return_ACPI_STATUS (AE_BAD_DATA);
  265. }
  266. /* Now align the current length */
  267. user_prt->length = (u32) ACPI_ROUND_UP_to_64_bITS (user_prt->length);
  268. /* 4) Fourth subobject: Dereference the PRT.source_index */
  269. obj_desc = sub_object_list[3];
  270. if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
  271. user_prt->source_index = (u32) obj_desc->integer.value;
  272. }
  273. else {
  274. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  275. "(PRT[%X].source_index) Need Integer, found %s\n",
  276. index, acpi_ut_get_object_type_name (obj_desc)));
  277. return_ACPI_STATUS (AE_BAD_DATA);
  278. }
  279. /* Point to the next union acpi_operand_object in the top level package */
  280. top_object_list++;
  281. }
  282. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "output_buffer %p Length %X\n",
  283. output_buffer->pointer, (u32) output_buffer->length));
  284. return_ACPI_STATUS (AE_OK);
  285. }
  286. /*******************************************************************************
  287. *
  288. * FUNCTION: acpi_rs_create_byte_stream
  289. *
  290. * PARAMETERS: linked_list_buffer - Pointer to the resource linked list
  291. * output_buffer - Pointer to the user's buffer
  292. *
  293. * RETURN: Status AE_OK if okay, else a valid acpi_status code.
  294. * If the output_buffer is too small, the error will be
  295. * AE_BUFFER_OVERFLOW and output_buffer->Length will point
  296. * to the size buffer needed.
  297. *
  298. * DESCRIPTION: Takes the linked list of device resources and
  299. * creates a bytestream to be used as input for the
  300. * _SRS control method.
  301. *
  302. ******************************************************************************/
  303. acpi_status
  304. acpi_rs_create_byte_stream (
  305. struct acpi_resource *linked_list_buffer,
  306. struct acpi_buffer *output_buffer)
  307. {
  308. acpi_status status;
  309. acpi_size byte_stream_size_needed = 0;
  310. ACPI_FUNCTION_TRACE ("rs_create_byte_stream");
  311. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "linked_list_buffer = %p\n",
  312. linked_list_buffer));
  313. /*
  314. * Params already validated, so we don't re-validate here
  315. *
  316. * Pass the linked_list_buffer into a module that calculates
  317. * the buffer size needed for the byte stream.
  318. */
  319. status = acpi_rs_get_byte_stream_length (linked_list_buffer,
  320. &byte_stream_size_needed);
  321. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "byte_stream_size_needed=%X, %s\n",
  322. (u32) byte_stream_size_needed, acpi_format_exception (status)));
  323. if (ACPI_FAILURE (status)) {
  324. return_ACPI_STATUS (status);
  325. }
  326. /* Validate/Allocate/Clear caller buffer */
  327. status = acpi_ut_initialize_buffer (output_buffer, byte_stream_size_needed);
  328. if (ACPI_FAILURE (status)) {
  329. return_ACPI_STATUS (status);
  330. }
  331. /* Do the conversion */
  332. status = acpi_rs_list_to_byte_stream (linked_list_buffer, byte_stream_size_needed,
  333. output_buffer->pointer);
  334. if (ACPI_FAILURE (status)) {
  335. return_ACPI_STATUS (status);
  336. }
  337. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "output_buffer %p Length %X\n",
  338. output_buffer->pointer, (u32) output_buffer->length));
  339. return_ACPI_STATUS (AE_OK);
  340. }