rscreate.c 13 KB

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