rscreate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*******************************************************************************
  2. *
  3. * Module Name: rscreate - Create resource lists/tables
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2007, 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: aml_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 *aml_buffer,
  67. struct acpi_buffer *output_buffer)
  68. {
  69. acpi_status status;
  70. u8 *aml_start;
  71. acpi_size list_size_needed = 0;
  72. u32 aml_buffer_length;
  73. void *resource;
  74. ACPI_FUNCTION_TRACE(rs_create_resource_list);
  75. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "AmlBuffer = %p\n", aml_buffer));
  76. /* Params already validated, so we don't re-validate here */
  77. aml_buffer_length = aml_buffer->buffer.length;
  78. aml_start = aml_buffer->buffer.pointer;
  79. /*
  80. * Pass the aml_buffer into a module that can calculate
  81. * the buffer size needed for the linked list
  82. */
  83. status = acpi_rs_get_list_length(aml_start, aml_buffer_length,
  84. &list_size_needed);
  85. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
  86. status, (u32) list_size_needed));
  87. if (ACPI_FAILURE(status)) {
  88. return_ACPI_STATUS(status);
  89. }
  90. /* Validate/Allocate/Clear caller buffer */
  91. status = acpi_ut_initialize_buffer(output_buffer, list_size_needed);
  92. if (ACPI_FAILURE(status)) {
  93. return_ACPI_STATUS(status);
  94. }
  95. /* Do the conversion */
  96. resource = output_buffer->pointer;
  97. status = acpi_ut_walk_aml_resources(aml_start, aml_buffer_length,
  98. acpi_rs_convert_aml_to_resources,
  99. &resource);
  100. if (ACPI_FAILURE(status)) {
  101. return_ACPI_STATUS(status);
  102. }
  103. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
  104. output_buffer->pointer, (u32) output_buffer->length));
  105. return_ACPI_STATUS(AE_OK);
  106. }
  107. /*******************************************************************************
  108. *
  109. * FUNCTION: acpi_rs_create_pci_routing_table
  110. *
  111. * PARAMETERS: package_object - Pointer to an union acpi_operand_object
  112. * package
  113. * output_buffer - Pointer to the user's buffer
  114. *
  115. * RETURN: Status AE_OK if okay, else a valid acpi_status code.
  116. * If the output_buffer is too small, the error will be
  117. * AE_BUFFER_OVERFLOW and output_buffer->Length will point
  118. * to the size buffer needed.
  119. *
  120. * DESCRIPTION: Takes the union acpi_operand_object package and creates a
  121. * linked list of PCI interrupt descriptions
  122. *
  123. * NOTE: It is the caller's responsibility to ensure that the start of the
  124. * output buffer is aligned properly (if necessary).
  125. *
  126. ******************************************************************************/
  127. acpi_status
  128. acpi_rs_create_pci_routing_table(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, "BufferSizeNeeded = %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. int source_name_index = 2;
  168. int source_index_index = 3;
  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_ERROR((AE_INFO,
  186. "(PRT[%X]) Need sub-package, found %s",
  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_ERROR((AE_INFO,
  195. "(PRT[%X]) Need package of length 4, found length %d",
  196. index, (*top_object_list)->package.count));
  197. return_ACPI_STATUS(AE_AML_PACKAGE_LIMIT);
  198. }
  199. /*
  200. * Dereference the sub-package.
  201. * The sub_object_list will now point to an array of the four IRQ
  202. * elements: [Address, Pin, Source, source_index]
  203. */
  204. sub_object_list = (*top_object_list)->package.elements;
  205. /* 1) First subobject: Dereference the PRT.Address */
  206. obj_desc = sub_object_list[0];
  207. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) {
  208. user_prt->address = obj_desc->integer.value;
  209. } else {
  210. ACPI_ERROR((AE_INFO,
  211. "(PRT[%X].Address) Need Integer, found %s",
  212. index,
  213. acpi_ut_get_object_type_name(obj_desc)));
  214. return_ACPI_STATUS(AE_BAD_DATA);
  215. }
  216. /* 2) Second subobject: Dereference the PRT.Pin */
  217. obj_desc = sub_object_list[1];
  218. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) {
  219. user_prt->pin = (u32) obj_desc->integer.value;
  220. } else {
  221. ACPI_ERROR((AE_INFO,
  222. "(PRT[%X].Pin) Need Integer, found %s",
  223. index,
  224. acpi_ut_get_object_type_name(obj_desc)));
  225. return_ACPI_STATUS(AE_BAD_DATA);
  226. }
  227. /*
  228. * If BIOS erroneously reversed the _PRT source_name and source_index,
  229. * then reverse them back.
  230. */
  231. if (ACPI_GET_OBJECT_TYPE(sub_object_list[3]) !=
  232. ACPI_TYPE_INTEGER) {
  233. if (acpi_gbl_enable_interpreter_slack) {
  234. source_name_index = 3;
  235. source_index_index = 2;
  236. printk(KERN_WARNING
  237. "ACPI: Handling Garbled _PRT entry\n");
  238. } else {
  239. ACPI_ERROR((AE_INFO,
  240. "(PRT[%X].source_index) Need Integer, found %s",
  241. index,
  242. acpi_ut_get_object_type_name
  243. (sub_object_list[3])));
  244. return_ACPI_STATUS(AE_BAD_DATA);
  245. }
  246. }
  247. /*
  248. * 3) Third subobject: Dereference the PRT.source_name
  249. * The name may be unresolved (slack mode), so allow a null object
  250. */
  251. obj_desc = sub_object_list[source_name_index];
  252. if (obj_desc) {
  253. switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
  254. case ACPI_TYPE_LOCAL_REFERENCE:
  255. if (obj_desc->reference.opcode !=
  256. AML_INT_NAMEPATH_OP) {
  257. ACPI_ERROR((AE_INFO,
  258. "(PRT[%X].Source) Need name, found reference op %X",
  259. index,
  260. obj_desc->reference.
  261. opcode));
  262. return_ACPI_STATUS(AE_BAD_DATA);
  263. }
  264. node = obj_desc->reference.node;
  265. /* Use *remaining* length of the buffer as max for pathname */
  266. path_buffer.length = output_buffer->length -
  267. (u32) ((u8 *) user_prt->source -
  268. (u8 *) output_buffer->pointer);
  269. path_buffer.pointer = user_prt->source;
  270. status =
  271. acpi_ns_handle_to_pathname((acpi_handle)
  272. node,
  273. &path_buffer);
  274. /* +1 to include null terminator */
  275. user_prt->length +=
  276. (u32) ACPI_STRLEN(user_prt->source) + 1;
  277. break;
  278. case ACPI_TYPE_STRING:
  279. ACPI_STRCPY(user_prt->source,
  280. obj_desc->string.pointer);
  281. /*
  282. * Add to the Length field the length of the string
  283. * (add 1 for terminator)
  284. */
  285. user_prt->length += obj_desc->string.length + 1;
  286. break;
  287. case ACPI_TYPE_INTEGER:
  288. /*
  289. * If this is a number, then the Source Name is NULL, since the
  290. * entire buffer was zeroed out, we can leave this alone.
  291. *
  292. * Add to the Length field the length of the u32 NULL
  293. */
  294. user_prt->length += sizeof(u32);
  295. break;
  296. default:
  297. ACPI_ERROR((AE_INFO,
  298. "(PRT[%X].Source) Need Ref/String/Integer, found %s",
  299. index,
  300. acpi_ut_get_object_type_name
  301. (obj_desc)));
  302. return_ACPI_STATUS(AE_BAD_DATA);
  303. }
  304. }
  305. /* Now align the current length */
  306. user_prt->length =
  307. (u32) ACPI_ROUND_UP_TO_64BIT(user_prt->length);
  308. /* 4) Fourth subobject: Dereference the PRT.source_index */
  309. obj_desc = sub_object_list[source_index_index];
  310. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) {
  311. user_prt->source_index = (u32) obj_desc->integer.value;
  312. } else {
  313. ACPI_ERROR((AE_INFO,
  314. "(PRT[%X].SourceIndex) Need Integer, found %s",
  315. index,
  316. acpi_ut_get_object_type_name(obj_desc)));
  317. return_ACPI_STATUS(AE_BAD_DATA);
  318. }
  319. /* Point to the next union acpi_operand_object in the top level package */
  320. top_object_list++;
  321. }
  322. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
  323. output_buffer->pointer, (u32) output_buffer->length));
  324. return_ACPI_STATUS(AE_OK);
  325. }
  326. /*******************************************************************************
  327. *
  328. * FUNCTION: acpi_rs_create_aml_resources
  329. *
  330. * PARAMETERS: linked_list_buffer - Pointer to the resource linked list
  331. * output_buffer - Pointer to the user's buffer
  332. *
  333. * RETURN: Status AE_OK if okay, else a valid acpi_status code.
  334. * If the output_buffer is too small, the error will be
  335. * AE_BUFFER_OVERFLOW and output_buffer->Length will point
  336. * to the size buffer needed.
  337. *
  338. * DESCRIPTION: Takes the linked list of device resources and
  339. * creates a bytestream to be used as input for the
  340. * _SRS control method.
  341. *
  342. ******************************************************************************/
  343. acpi_status
  344. acpi_rs_create_aml_resources(struct acpi_resource *linked_list_buffer,
  345. struct acpi_buffer *output_buffer)
  346. {
  347. acpi_status status;
  348. acpi_size aml_size_needed = 0;
  349. ACPI_FUNCTION_TRACE(rs_create_aml_resources);
  350. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "LinkedListBuffer = %p\n",
  351. linked_list_buffer));
  352. /*
  353. * Params already validated, so we don't re-validate here
  354. *
  355. * Pass the linked_list_buffer into a module that calculates
  356. * the buffer size needed for the byte stream.
  357. */
  358. status = acpi_rs_get_aml_length(linked_list_buffer, &aml_size_needed);
  359. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
  360. (u32) aml_size_needed,
  361. acpi_format_exception(status)));
  362. if (ACPI_FAILURE(status)) {
  363. return_ACPI_STATUS(status);
  364. }
  365. /* Validate/Allocate/Clear caller buffer */
  366. status = acpi_ut_initialize_buffer(output_buffer, aml_size_needed);
  367. if (ACPI_FAILURE(status)) {
  368. return_ACPI_STATUS(status);
  369. }
  370. /* Do the conversion */
  371. status =
  372. acpi_rs_convert_resources_to_aml(linked_list_buffer,
  373. aml_size_needed,
  374. output_buffer->pointer);
  375. if (ACPI_FAILURE(status)) {
  376. return_ACPI_STATUS(status);
  377. }
  378. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
  379. output_buffer->pointer, (u32) output_buffer->length));
  380. return_ACPI_STATUS(AE_OK);
  381. }