rscreate.c 15 KB

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