rscreate.c 14 KB

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