rscreate.c 15 KB

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