rscreate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*******************************************************************************
  2. *
  3. * Module Name: rscreate - Create resource lists/tables
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, Intel Corp.
  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/acnamesp.h>
  45. #define _COMPONENT ACPI_RESOURCES
  46. ACPI_MODULE_NAME("rscreate")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_rs_create_resource_list
  50. *
  51. * PARAMETERS: aml_buffer - Pointer to the resource byte stream
  52. * output_buffer - Pointer to the user's buffer
  53. *
  54. * RETURN: Status: AE_OK if okay, else a valid acpi_status code
  55. * If output_buffer is not large enough, output_buffer_length
  56. * indicates how large output_buffer should be, else it
  57. * indicates how may u8 elements of output_buffer are valid.
  58. *
  59. * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
  60. * execution and parses the stream to create a linked list
  61. * of device resources.
  62. *
  63. ******************************************************************************/
  64. acpi_status
  65. acpi_rs_create_resource_list(union acpi_operand_object *aml_buffer,
  66. struct acpi_buffer *output_buffer)
  67. {
  68. acpi_status status;
  69. u8 *aml_start;
  70. acpi_size list_size_needed = 0;
  71. u32 aml_buffer_length;
  72. void *resource;
  73. ACPI_FUNCTION_TRACE(rs_create_resource_list);
  74. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "AmlBuffer = %p\n", aml_buffer));
  75. /* Params already validated, so we don't re-validate here */
  76. aml_buffer_length = aml_buffer->buffer.length;
  77. aml_start = aml_buffer->buffer.pointer;
  78. /*
  79. * Pass the aml_buffer into a module that can calculate
  80. * the buffer size needed for the linked list
  81. */
  82. status = acpi_rs_get_list_length(aml_start, aml_buffer_length,
  83. &list_size_needed);
  84. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
  85. status, (u32) list_size_needed));
  86. if (ACPI_FAILURE(status)) {
  87. return_ACPI_STATUS(status);
  88. }
  89. /* Validate/Allocate/Clear caller buffer */
  90. status = acpi_ut_initialize_buffer(output_buffer, list_size_needed);
  91. if (ACPI_FAILURE(status)) {
  92. return_ACPI_STATUS(status);
  93. }
  94. /* Do the conversion */
  95. resource = output_buffer->pointer;
  96. status = acpi_ut_walk_aml_resources(aml_start, aml_buffer_length,
  97. acpi_rs_convert_aml_to_resources,
  98. &resource);
  99. if (ACPI_FAILURE(status)) {
  100. return_ACPI_STATUS(status);
  101. }
  102. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %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(union acpi_operand_object *package_object,
  128. struct acpi_buffer *output_buffer)
  129. {
  130. u8 *buffer;
  131. union acpi_operand_object **top_object_list;
  132. union acpi_operand_object **sub_object_list;
  133. union acpi_operand_object *obj_desc;
  134. acpi_size buffer_size_needed = 0;
  135. u32 number_of_elements;
  136. u32 index;
  137. struct acpi_pci_routing_table *user_prt;
  138. struct acpi_namespace_node *node;
  139. acpi_status status;
  140. struct acpi_buffer path_buffer;
  141. ACPI_FUNCTION_TRACE(rs_create_pci_routing_table);
  142. /* Params already validated, so we don't re-validate here */
  143. /* Get the required buffer length */
  144. status = acpi_rs_get_pci_routing_table_length(package_object,
  145. &buffer_size_needed);
  146. if (ACPI_FAILURE(status)) {
  147. return_ACPI_STATUS(status);
  148. }
  149. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
  150. (u32) buffer_size_needed));
  151. /* Validate/Allocate/Clear caller buffer */
  152. status = acpi_ut_initialize_buffer(output_buffer, buffer_size_needed);
  153. if (ACPI_FAILURE(status)) {
  154. return_ACPI_STATUS(status);
  155. }
  156. /*
  157. * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
  158. * package that in turn contains an acpi_integer Address, a u8 Pin,
  159. * a Name, and a u8 source_index.
  160. */
  161. top_object_list = package_object->package.elements;
  162. number_of_elements = package_object->package.count;
  163. buffer = output_buffer->pointer;
  164. user_prt = ACPI_CAST_PTR(struct acpi_pci_routing_table, buffer);
  165. for (index = 0; index < number_of_elements; index++) {
  166. int source_name_index = 2;
  167. int source_index_index = 3;
  168. /*
  169. * Point user_prt past this current structure
  170. *
  171. * NOTE: On the first iteration, user_prt->Length will
  172. * be zero because we cleared the return buffer earlier
  173. */
  174. buffer += user_prt->length;
  175. user_prt = ACPI_CAST_PTR(struct acpi_pci_routing_table, buffer);
  176. /*
  177. * Fill in the Length field with the information we have at this point.
  178. * The minus four is to subtract the size of the u8 Source[4] member
  179. * because it is added below.
  180. */
  181. user_prt->length = (sizeof(struct acpi_pci_routing_table) - 4);
  182. /* Each element of the top-level package must also be a package */
  183. if (ACPI_GET_OBJECT_TYPE(*top_object_list) != ACPI_TYPE_PACKAGE) {
  184. ACPI_ERROR((AE_INFO,
  185. "(PRT[%X]) Need sub-package, found %s",
  186. index,
  187. acpi_ut_get_object_type_name
  188. (*top_object_list)));
  189. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  190. }
  191. /* Each sub-package must be of length 4 */
  192. if ((*top_object_list)->package.count != 4) {
  193. ACPI_ERROR((AE_INFO,
  194. "(PRT[%X]) Need package of length 4, found length %d",
  195. index, (*top_object_list)->package.count));
  196. return_ACPI_STATUS(AE_AML_PACKAGE_LIMIT);
  197. }
  198. /*
  199. * Dereference the sub-package.
  200. * The sub_object_list will now point to an array of the four IRQ
  201. * elements: [Address, Pin, Source, source_index]
  202. */
  203. sub_object_list = (*top_object_list)->package.elements;
  204. /* 1) First subobject: Dereference the PRT.Address */
  205. obj_desc = sub_object_list[0];
  206. if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER) {
  207. ACPI_ERROR((AE_INFO,
  208. "(PRT[%X].Address) Need Integer, found %s",
  209. index,
  210. acpi_ut_get_object_type_name(obj_desc)));
  211. return_ACPI_STATUS(AE_BAD_DATA);
  212. }
  213. user_prt->address = obj_desc->integer.value;
  214. /* 2) Second subobject: Dereference the PRT.Pin */
  215. obj_desc = sub_object_list[1];
  216. if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER) {
  217. ACPI_ERROR((AE_INFO,
  218. "(PRT[%X].Pin) Need Integer, found %s",
  219. index,
  220. acpi_ut_get_object_type_name(obj_desc)));
  221. return_ACPI_STATUS(AE_BAD_DATA);
  222. }
  223. /*
  224. * If BIOS erroneously reversed the _PRT source_name and source_index,
  225. * then reverse them back.
  226. */
  227. if (ACPI_GET_OBJECT_TYPE(sub_object_list[3]) !=
  228. ACPI_TYPE_INTEGER) {
  229. if (acpi_gbl_enable_interpreter_slack) {
  230. source_name_index = 3;
  231. source_index_index = 2;
  232. printk(KERN_WARNING
  233. "ACPI: Handling Garbled _PRT entry\n");
  234. } else {
  235. ACPI_ERROR((AE_INFO,
  236. "(PRT[%X].source_index) Need Integer, found %s",
  237. index,
  238. acpi_ut_get_object_type_name
  239. (sub_object_list[3])));
  240. return_ACPI_STATUS(AE_BAD_DATA);
  241. }
  242. }
  243. user_prt->pin = (u32) obj_desc->integer.value;
  244. /*
  245. * If the BIOS has erroneously reversed the _PRT source_name (index 2)
  246. * and the source_index (index 3), fix it. _PRT is important enough to
  247. * workaround this BIOS error. This also provides compatibility with
  248. * other ACPI implementations.
  249. */
  250. obj_desc = sub_object_list[3];
  251. if (!obj_desc
  252. || (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER)) {
  253. sub_object_list[3] = sub_object_list[2];
  254. sub_object_list[2] = obj_desc;
  255. ACPI_WARNING((AE_INFO,
  256. "(PRT[%X].Source) SourceName and SourceIndex are reversed, fixed",
  257. index));
  258. }
  259. /*
  260. * 3) Third subobject: Dereference the PRT.source_name
  261. * The name may be unresolved (slack mode), so allow a null object
  262. */
  263. obj_desc = sub_object_list[source_name_index];
  264. if (obj_desc) {
  265. switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
  266. case ACPI_TYPE_LOCAL_REFERENCE:
  267. if (obj_desc->reference.class !=
  268. ACPI_REFCLASS_NAME) {
  269. ACPI_ERROR((AE_INFO,
  270. "(PRT[%X].Source) Need name, found Reference Class %X",
  271. index,
  272. obj_desc->reference.class));
  273. return_ACPI_STATUS(AE_BAD_DATA);
  274. }
  275. node = obj_desc->reference.node;
  276. /* Use *remaining* length of the buffer as max for pathname */
  277. path_buffer.length = output_buffer->length -
  278. (u32) ((u8 *) user_prt->source -
  279. (u8 *) output_buffer->pointer);
  280. path_buffer.pointer = user_prt->source;
  281. status =
  282. acpi_ns_handle_to_pathname((acpi_handle)
  283. node,
  284. &path_buffer);
  285. /* +1 to include null terminator */
  286. user_prt->length +=
  287. (u32) ACPI_STRLEN(user_prt->source) + 1;
  288. break;
  289. case ACPI_TYPE_STRING:
  290. ACPI_STRCPY(user_prt->source,
  291. obj_desc->string.pointer);
  292. /*
  293. * Add to the Length field the length of the string
  294. * (add 1 for terminator)
  295. */
  296. user_prt->length += obj_desc->string.length + 1;
  297. break;
  298. case ACPI_TYPE_INTEGER:
  299. /*
  300. * If this is a number, then the Source Name is NULL, since the
  301. * entire buffer was zeroed out, we can leave this alone.
  302. *
  303. * Add to the Length field the length of the u32 NULL
  304. */
  305. user_prt->length += sizeof(u32);
  306. break;
  307. default:
  308. ACPI_ERROR((AE_INFO,
  309. "(PRT[%X].Source) Need Ref/String/Integer, found %s",
  310. index,
  311. acpi_ut_get_object_type_name
  312. (obj_desc)));
  313. return_ACPI_STATUS(AE_BAD_DATA);
  314. }
  315. }
  316. /* Now align the current length */
  317. user_prt->length =
  318. (u32) ACPI_ROUND_UP_TO_64BIT(user_prt->length);
  319. /* 4) Fourth subobject: Dereference the PRT.source_index */
  320. obj_desc = sub_object_list[source_index_index];
  321. if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER) {
  322. ACPI_ERROR((AE_INFO,
  323. "(PRT[%X].SourceIndex) Need Integer, found %s",
  324. index,
  325. acpi_ut_get_object_type_name(obj_desc)));
  326. return_ACPI_STATUS(AE_BAD_DATA);
  327. }
  328. user_prt->source_index = (u32) obj_desc->integer.value;
  329. /* Point to the next union acpi_operand_object in the top level package */
  330. top_object_list++;
  331. }
  332. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
  333. output_buffer->pointer, (u32) output_buffer->length));
  334. return_ACPI_STATUS(AE_OK);
  335. }
  336. /*******************************************************************************
  337. *
  338. * FUNCTION: acpi_rs_create_aml_resources
  339. *
  340. * PARAMETERS: linked_list_buffer - Pointer to the resource linked list
  341. * output_buffer - Pointer to the user's buffer
  342. *
  343. * RETURN: Status AE_OK if okay, else a valid acpi_status code.
  344. * If the output_buffer is too small, the error will be
  345. * AE_BUFFER_OVERFLOW and output_buffer->Length will point
  346. * to the size buffer needed.
  347. *
  348. * DESCRIPTION: Takes the linked list of device resources and
  349. * creates a bytestream to be used as input for the
  350. * _SRS control method.
  351. *
  352. ******************************************************************************/
  353. acpi_status
  354. acpi_rs_create_aml_resources(struct acpi_resource *linked_list_buffer,
  355. struct acpi_buffer *output_buffer)
  356. {
  357. acpi_status status;
  358. acpi_size aml_size_needed = 0;
  359. ACPI_FUNCTION_TRACE(rs_create_aml_resources);
  360. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "LinkedListBuffer = %p\n",
  361. linked_list_buffer));
  362. /*
  363. * Params already validated, so we don't re-validate here
  364. *
  365. * Pass the linked_list_buffer into a module that calculates
  366. * the buffer size needed for the byte stream.
  367. */
  368. status = acpi_rs_get_aml_length(linked_list_buffer, &aml_size_needed);
  369. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
  370. (u32) aml_size_needed,
  371. acpi_format_exception(status)));
  372. if (ACPI_FAILURE(status)) {
  373. return_ACPI_STATUS(status);
  374. }
  375. /* Validate/Allocate/Clear caller buffer */
  376. status = acpi_ut_initialize_buffer(output_buffer, aml_size_needed);
  377. if (ACPI_FAILURE(status)) {
  378. return_ACPI_STATUS(status);
  379. }
  380. /* Do the conversion */
  381. status =
  382. acpi_rs_convert_resources_to_aml(linked_list_buffer,
  383. aml_size_needed,
  384. output_buffer->pointer);
  385. if (ACPI_FAILURE(status)) {
  386. return_ACPI_STATUS(status);
  387. }
  388. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
  389. output_buffer->pointer, (u32) output_buffer->length));
  390. return_ACPI_STATUS(AE_OK);
  391. }