nsxfname.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nsxfname - Public interfaces to the ACPI subsystem
  5. * ACPI Namespace oriented interfaces
  6. *
  7. * Copyright (C) 2000 - 2022, Intel Corp.
  8. *
  9. *****************************************************************************/
  10. #define EXPORT_ACPI_INTERFACES
  11. #include <acpi/acpi.h>
  12. #include "accommon.h"
  13. #include "acnamesp.h"
  14. #include "acparser.h"
  15. #include "amlcode.h"
  16. #define _COMPONENT ACPI_NAMESPACE
  17. ACPI_MODULE_NAME("nsxfname")
  18. /* Local prototypes */
  19. static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
  20. struct acpi_pnp_device_id *source,
  21. char *string_area);
  22. /******************************************************************************
  23. *
  24. * FUNCTION: acpi_get_handle
  25. *
  26. * PARAMETERS: parent - Object to search under (search scope).
  27. * pathname - Pointer to an asciiz string containing the
  28. * name
  29. * ret_handle - Where the return handle is returned
  30. *
  31. * RETURN: Status
  32. *
  33. * DESCRIPTION: This routine will search for a caller specified name in the
  34. * name space. The caller can restrict the search region by
  35. * specifying a non NULL parent. The parent value is itself a
  36. * namespace handle.
  37. *
  38. ******************************************************************************/
  39. acpi_status
  40. acpi_get_handle(acpi_handle parent,
  41. acpi_string pathname, acpi_handle *ret_handle)
  42. {
  43. acpi_status status;
  44. struct acpi_namespace_node *node = NULL;
  45. struct acpi_namespace_node *prefix_node = NULL;
  46. ACPI_FUNCTION_ENTRY();
  47. /* Parameter Validation */
  48. if (!ret_handle || !pathname) {
  49. return (AE_BAD_PARAMETER);
  50. }
  51. /* Convert a parent handle to a prefix node */
  52. if (parent) {
  53. prefix_node = acpi_ns_validate_handle(parent);
  54. if (!prefix_node) {
  55. return (AE_BAD_PARAMETER);
  56. }
  57. }
  58. /*
  59. * Valid cases are:
  60. * 1) Fully qualified pathname
  61. * 2) Parent + Relative pathname
  62. *
  63. * Error for <null Parent + relative path>
  64. */
  65. if (ACPI_IS_ROOT_PREFIX(pathname[0])) {
  66. /* Pathname is fully qualified (starts with '\') */
  67. /* Special case for root-only, since we can't search for it */
  68. if (!strcmp(pathname, ACPI_NS_ROOT_PATH)) {
  69. *ret_handle =
  70. ACPI_CAST_PTR(acpi_handle, acpi_gbl_root_node);
  71. return (AE_OK);
  72. }
  73. } else if (!prefix_node) {
  74. /* Relative path with null prefix is disallowed */
  75. return (AE_BAD_PARAMETER);
  76. }
  77. /* Find the Node and convert to a handle */
  78. status =
  79. acpi_ns_get_node(prefix_node, pathname, ACPI_NS_NO_UPSEARCH, &node);
  80. if (ACPI_SUCCESS(status)) {
  81. *ret_handle = ACPI_CAST_PTR(acpi_handle, node);
  82. }
  83. return (status);
  84. }
  85. ACPI_EXPORT_SYMBOL(acpi_get_handle)
  86. /******************************************************************************
  87. *
  88. * FUNCTION: acpi_get_name
  89. *
  90. * PARAMETERS: handle - Handle to be converted to a pathname
  91. * name_type - Full pathname or single segment
  92. * buffer - Buffer for returned path
  93. *
  94. * RETURN: Pointer to a string containing the fully qualified Name.
  95. *
  96. * DESCRIPTION: This routine returns the fully qualified name associated with
  97. * the Handle parameter. This and the acpi_pathname_to_handle are
  98. * complementary functions.
  99. *
  100. ******************************************************************************/
  101. acpi_status
  102. acpi_get_name(acpi_handle handle, u32 name_type, struct acpi_buffer *buffer)
  103. {
  104. acpi_status status;
  105. /* Parameter validation */
  106. if (name_type > ACPI_NAME_TYPE_MAX) {
  107. return (AE_BAD_PARAMETER);
  108. }
  109. status = acpi_ut_validate_buffer(buffer);
  110. if (ACPI_FAILURE(status)) {
  111. return (status);
  112. }
  113. /*
  114. * Wants the single segment ACPI name.
  115. * Validate handle and convert to a namespace Node
  116. */
  117. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  118. if (ACPI_FAILURE(status)) {
  119. return (status);
  120. }
  121. if (name_type == ACPI_FULL_PATHNAME ||
  122. name_type == ACPI_FULL_PATHNAME_NO_TRAILING) {
  123. /* Get the full pathname (From the namespace root) */
  124. status = acpi_ns_handle_to_pathname(handle, buffer,
  125. name_type ==
  126. ACPI_FULL_PATHNAME ? FALSE :
  127. TRUE);
  128. } else {
  129. /* Get the single name */
  130. status = acpi_ns_handle_to_name(handle, buffer);
  131. }
  132. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  133. return (status);
  134. }
  135. ACPI_EXPORT_SYMBOL(acpi_get_name)
  136. /******************************************************************************
  137. *
  138. * FUNCTION: acpi_ns_copy_device_id
  139. *
  140. * PARAMETERS: dest - Pointer to the destination PNP_DEVICE_ID
  141. * source - Pointer to the source PNP_DEVICE_ID
  142. * string_area - Pointer to where to copy the dest string
  143. *
  144. * RETURN: Pointer to the next string area
  145. *
  146. * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
  147. *
  148. ******************************************************************************/
  149. static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
  150. struct acpi_pnp_device_id *source,
  151. char *string_area)
  152. {
  153. /* Create the destination PNP_DEVICE_ID */
  154. dest->string = string_area;
  155. dest->length = source->length;
  156. /* Copy actual string and return a pointer to the next string area */
  157. memcpy(string_area, source->string, source->length);
  158. return (string_area + source->length);
  159. }
  160. /******************************************************************************
  161. *
  162. * FUNCTION: acpi_get_object_info
  163. *
  164. * PARAMETERS: handle - Object Handle
  165. * return_buffer - Where the info is returned
  166. *
  167. * RETURN: Status
  168. *
  169. * DESCRIPTION: Returns information about an object as gleaned from the
  170. * namespace node and possibly by running several standard
  171. * control methods (Such as in the case of a device.)
  172. *
  173. * For Device and Processor objects, run the Device _HID, _UID, _CID,
  174. * _CLS, _ADR, _sx_w, and _sx_d methods.
  175. *
  176. * Note: Allocates the return buffer, must be freed by the caller.
  177. *
  178. * Note: This interface is intended to be used during the initial device
  179. * discovery namespace traversal. Therefore, no complex methods can be
  180. * executed, especially those that access operation regions. Therefore, do
  181. * not add any additional methods that could cause problems in this area.
  182. * Because of this reason support for the following methods has been removed:
  183. * 1) _SUB method was removed (11/2015)
  184. * 2) _STA method was removed (02/2018)
  185. *
  186. ******************************************************************************/
  187. acpi_status
  188. acpi_get_object_info(acpi_handle handle,
  189. struct acpi_device_info **return_buffer)
  190. {
  191. struct acpi_namespace_node *node;
  192. struct acpi_device_info *info;
  193. struct acpi_pnp_device_id_list *cid_list = NULL;
  194. struct acpi_pnp_device_id *hid = NULL;
  195. struct acpi_pnp_device_id *uid = NULL;
  196. struct acpi_pnp_device_id *cls = NULL;
  197. char *next_id_string;
  198. acpi_object_type type;
  199. acpi_name name;
  200. u8 param_count = 0;
  201. u16 valid = 0;
  202. u32 info_size;
  203. u32 i;
  204. acpi_status status;
  205. /* Parameter validation */
  206. if (!handle || !return_buffer) {
  207. return (AE_BAD_PARAMETER);
  208. }
  209. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  210. if (ACPI_FAILURE(status)) {
  211. return (status);
  212. }
  213. node = acpi_ns_validate_handle(handle);
  214. if (!node) {
  215. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  216. return (AE_BAD_PARAMETER);
  217. }
  218. /* Get the namespace node data while the namespace is locked */
  219. info_size = sizeof(struct acpi_device_info);
  220. type = node->type;
  221. name = node->name.integer;
  222. if (node->type == ACPI_TYPE_METHOD) {
  223. param_count = node->object->method.param_count;
  224. }
  225. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  226. if (ACPI_FAILURE(status)) {
  227. return (status);
  228. }
  229. if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
  230. /*
  231. * Get extra info for ACPI Device/Processor objects only:
  232. * Run the Device _HID, _UID, _CLS, and _CID methods.
  233. *
  234. * Note: none of these methods are required, so they may or may
  235. * not be present for this device. The Info->Valid bitfield is used
  236. * to indicate which methods were found and run successfully.
  237. */
  238. /* Execute the Device._HID method */
  239. status = acpi_ut_execute_HID(node, &hid);
  240. if (ACPI_SUCCESS(status)) {
  241. info_size += hid->length;
  242. valid |= ACPI_VALID_HID;
  243. }
  244. /* Execute the Device._UID method */
  245. status = acpi_ut_execute_UID(node, &uid);
  246. if (ACPI_SUCCESS(status)) {
  247. info_size += uid->length;
  248. valid |= ACPI_VALID_UID;
  249. }
  250. /* Execute the Device._CID method */
  251. status = acpi_ut_execute_CID(node, &cid_list);
  252. if (ACPI_SUCCESS(status)) {
  253. /* Add size of CID strings and CID pointer array */
  254. info_size +=
  255. (cid_list->list_size -
  256. sizeof(struct acpi_pnp_device_id_list));
  257. valid |= ACPI_VALID_CID;
  258. }
  259. /* Execute the Device._CLS method */
  260. status = acpi_ut_execute_CLS(node, &cls);
  261. if (ACPI_SUCCESS(status)) {
  262. info_size += cls->length;
  263. valid |= ACPI_VALID_CLS;
  264. }
  265. }
  266. /*
  267. * Now that we have the variable-length data, we can allocate the
  268. * return buffer
  269. */
  270. info = ACPI_ALLOCATE_ZEROED(info_size);
  271. if (!info) {
  272. status = AE_NO_MEMORY;
  273. goto cleanup;
  274. }
  275. /* Get the fixed-length data */
  276. if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
  277. /*
  278. * Get extra info for ACPI Device/Processor objects only:
  279. * Run the _ADR and, sx_w, and _sx_d methods.
  280. *
  281. * Notes: none of these methods are required, so they may or may
  282. * not be present for this device. The Info->Valid bitfield is used
  283. * to indicate which methods were found and run successfully.
  284. */
  285. /* Execute the Device._ADR method */
  286. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, node,
  287. &info->address);
  288. if (ACPI_SUCCESS(status)) {
  289. valid |= ACPI_VALID_ADR;
  290. }
  291. /* Execute the Device._sx_w methods */
  292. status = acpi_ut_execute_power_methods(node,
  293. acpi_gbl_lowest_dstate_names,
  294. ACPI_NUM_sx_w_METHODS,
  295. info->lowest_dstates);
  296. if (ACPI_SUCCESS(status)) {
  297. valid |= ACPI_VALID_SXWS;
  298. }
  299. /* Execute the Device._sx_d methods */
  300. status = acpi_ut_execute_power_methods(node,
  301. acpi_gbl_highest_dstate_names,
  302. ACPI_NUM_sx_d_METHODS,
  303. info->highest_dstates);
  304. if (ACPI_SUCCESS(status)) {
  305. valid |= ACPI_VALID_SXDS;
  306. }
  307. }
  308. /*
  309. * Create a pointer to the string area of the return buffer.
  310. * Point to the end of the base struct acpi_device_info structure.
  311. */
  312. next_id_string = ACPI_CAST_PTR(char, info->compatible_id_list.ids);
  313. if (cid_list) {
  314. /* Point past the CID PNP_DEVICE_ID array */
  315. next_id_string +=
  316. ((acpi_size)cid_list->count *
  317. sizeof(struct acpi_pnp_device_id));
  318. }
  319. /*
  320. * Copy the HID, UID, and CIDs to the return buffer. The variable-length
  321. * strings are copied to the reserved area at the end of the buffer.
  322. *
  323. * For HID and CID, check if the ID is a PCI Root Bridge.
  324. */
  325. if (hid) {
  326. next_id_string = acpi_ns_copy_device_id(&info->hardware_id,
  327. hid, next_id_string);
  328. if (acpi_ut_is_pci_root_bridge(hid->string)) {
  329. info->flags |= ACPI_PCI_ROOT_BRIDGE;
  330. }
  331. }
  332. if (uid) {
  333. next_id_string = acpi_ns_copy_device_id(&info->unique_id,
  334. uid, next_id_string);
  335. }
  336. if (cid_list) {
  337. info->compatible_id_list.count = cid_list->count;
  338. info->compatible_id_list.list_size = cid_list->list_size;
  339. /* Copy each CID */
  340. for (i = 0; i < cid_list->count; i++) {
  341. next_id_string =
  342. acpi_ns_copy_device_id(&info->compatible_id_list.
  343. ids[i], &cid_list->ids[i],
  344. next_id_string);
  345. if (acpi_ut_is_pci_root_bridge(cid_list->ids[i].string)) {
  346. info->flags |= ACPI_PCI_ROOT_BRIDGE;
  347. }
  348. }
  349. }
  350. if (cls) {
  351. (void)acpi_ns_copy_device_id(&info->class_code,
  352. cls, next_id_string);
  353. }
  354. /* Copy the fixed-length data */
  355. info->info_size = info_size;
  356. info->type = type;
  357. info->name = name;
  358. info->param_count = param_count;
  359. info->valid = valid;
  360. *return_buffer = info;
  361. status = AE_OK;
  362. cleanup:
  363. if (hid) {
  364. ACPI_FREE(hid);
  365. }
  366. if (uid) {
  367. ACPI_FREE(uid);
  368. }
  369. if (cid_list) {
  370. ACPI_FREE(cid_list);
  371. }
  372. if (cls) {
  373. ACPI_FREE(cls);
  374. }
  375. return (status);
  376. }
  377. ACPI_EXPORT_SYMBOL(acpi_get_object_info)
  378. /******************************************************************************
  379. *
  380. * FUNCTION: acpi_install_method
  381. *
  382. * PARAMETERS: buffer - An ACPI table containing one control method
  383. *
  384. * RETURN: Status
  385. *
  386. * DESCRIPTION: Install a control method into the namespace. If the method
  387. * name already exists in the namespace, it is overwritten. The
  388. * input buffer must contain a valid DSDT or SSDT containing a
  389. * single control method.
  390. *
  391. ******************************************************************************/
  392. acpi_status acpi_install_method(u8 *buffer)
  393. {
  394. struct acpi_table_header *table =
  395. ACPI_CAST_PTR(struct acpi_table_header, buffer);
  396. u8 *aml_buffer;
  397. u8 *aml_start;
  398. char *path;
  399. struct acpi_namespace_node *node;
  400. union acpi_operand_object *method_obj;
  401. struct acpi_parse_state parser_state;
  402. u32 aml_length;
  403. u16 opcode;
  404. u8 method_flags;
  405. acpi_status status;
  406. /* Parameter validation */
  407. if (!buffer) {
  408. return (AE_BAD_PARAMETER);
  409. }
  410. /* Table must be a DSDT or SSDT */
  411. if (!ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_DSDT) &&
  412. !ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_SSDT)) {
  413. return (AE_BAD_HEADER);
  414. }
  415. /* First AML opcode in the table must be a control method */
  416. parser_state.aml = buffer + sizeof(struct acpi_table_header);
  417. opcode = acpi_ps_peek_opcode(&parser_state);
  418. if (opcode != AML_METHOD_OP) {
  419. return (AE_BAD_PARAMETER);
  420. }
  421. /* Extract method information from the raw AML */
  422. parser_state.aml += acpi_ps_get_opcode_size(opcode);
  423. parser_state.pkg_end = acpi_ps_get_next_package_end(&parser_state);
  424. path = acpi_ps_get_next_namestring(&parser_state);
  425. method_flags = *parser_state.aml++;
  426. aml_start = parser_state.aml;
  427. aml_length = (u32)ACPI_PTR_DIFF(parser_state.pkg_end, aml_start);
  428. /*
  429. * Allocate resources up-front. We don't want to have to delete a new
  430. * node from the namespace if we cannot allocate memory.
  431. */
  432. aml_buffer = ACPI_ALLOCATE(aml_length);
  433. if (!aml_buffer) {
  434. return (AE_NO_MEMORY);
  435. }
  436. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  437. if (!method_obj) {
  438. ACPI_FREE(aml_buffer);
  439. return (AE_NO_MEMORY);
  440. }
  441. /* Lock namespace for acpi_ns_lookup, we may be creating a new node */
  442. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  443. if (ACPI_FAILURE(status)) {
  444. goto error_exit;
  445. }
  446. /* The lookup either returns an existing node or creates a new one */
  447. status =
  448. acpi_ns_lookup(NULL, path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
  449. ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND,
  450. NULL, &node);
  451. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  452. if (ACPI_FAILURE(status)) { /* ns_lookup */
  453. if (status != AE_ALREADY_EXISTS) {
  454. goto error_exit;
  455. }
  456. /* Node existed previously, make sure it is a method node */
  457. if (node->type != ACPI_TYPE_METHOD) {
  458. status = AE_TYPE;
  459. goto error_exit;
  460. }
  461. }
  462. /* Copy the method AML to the local buffer */
  463. memcpy(aml_buffer, aml_start, aml_length);
  464. /* Initialize the method object with the new method's information */
  465. method_obj->method.aml_start = aml_buffer;
  466. method_obj->method.aml_length = aml_length;
  467. method_obj->method.param_count = (u8)
  468. (method_flags & AML_METHOD_ARG_COUNT);
  469. if (method_flags & AML_METHOD_SERIALIZED) {
  470. method_obj->method.info_flags = ACPI_METHOD_SERIALIZED;
  471. method_obj->method.sync_level = (u8)
  472. ((method_flags & AML_METHOD_SYNC_LEVEL) >> 4);
  473. }
  474. /*
  475. * Now that it is complete, we can attach the new method object to
  476. * the method Node (detaches/deletes any existing object)
  477. */
  478. status = acpi_ns_attach_object(node, method_obj, ACPI_TYPE_METHOD);
  479. /*
  480. * Flag indicates AML buffer is dynamic, must be deleted later.
  481. * Must be set only after attach above.
  482. */
  483. node->flags |= ANOBJ_ALLOCATED_BUFFER;
  484. /* Remove local reference to the method object */
  485. acpi_ut_remove_reference(method_obj);
  486. return (status);
  487. error_exit:
  488. ACPI_FREE(aml_buffer);
  489. ACPI_FREE(method_obj);
  490. return (status);
  491. }
  492. ACPI_EXPORT_SYMBOL(acpi_install_method)