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