nsobject.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: nsobject - Utilities for objects attached to namespace
  5. * table entries
  6. *
  7. ******************************************************************************/
  8. #include <acpi/acpi.h>
  9. #include "accommon.h"
  10. #include "acnamesp.h"
  11. #define _COMPONENT ACPI_NAMESPACE
  12. ACPI_MODULE_NAME("nsobject")
  13. /*******************************************************************************
  14. *
  15. * FUNCTION: acpi_ns_attach_object
  16. *
  17. * PARAMETERS: node - Parent Node
  18. * object - Object to be attached
  19. * type - Type of object, or ACPI_TYPE_ANY if not
  20. * known
  21. *
  22. * RETURN: Status
  23. *
  24. * DESCRIPTION: Record the given object as the value associated with the
  25. * name whose acpi_handle is passed. If Object is NULL
  26. * and Type is ACPI_TYPE_ANY, set the name as having no value.
  27. * Note: Future may require that the Node->Flags field be passed
  28. * as a parameter.
  29. *
  30. * MUTEX: Assumes namespace is locked
  31. *
  32. ******************************************************************************/
  33. acpi_status
  34. acpi_ns_attach_object(struct acpi_namespace_node *node,
  35. union acpi_operand_object *object, acpi_object_type type)
  36. {
  37. union acpi_operand_object *obj_desc;
  38. union acpi_operand_object *last_obj_desc;
  39. acpi_object_type object_type = ACPI_TYPE_ANY;
  40. ACPI_FUNCTION_TRACE(ns_attach_object);
  41. /*
  42. * Parameter validation
  43. */
  44. if (!node) {
  45. /* Invalid handle */
  46. ACPI_ERROR((AE_INFO, "Null NamedObj handle"));
  47. return_ACPI_STATUS(AE_BAD_PARAMETER);
  48. }
  49. if (!object && (ACPI_TYPE_ANY != type)) {
  50. /* Null object */
  51. ACPI_ERROR((AE_INFO,
  52. "Null object, but type not ACPI_TYPE_ANY"));
  53. return_ACPI_STATUS(AE_BAD_PARAMETER);
  54. }
  55. if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
  56. /* Not a name handle */
  57. ACPI_ERROR((AE_INFO, "Invalid handle %p [%s]",
  58. node, acpi_ut_get_descriptor_name(node)));
  59. return_ACPI_STATUS(AE_BAD_PARAMETER);
  60. }
  61. /* Check if this object is already attached */
  62. if (node->object == object) {
  63. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  64. "Obj %p already installed in NameObj %p\n",
  65. object, node));
  66. return_ACPI_STATUS(AE_OK);
  67. }
  68. /* If null object, we will just install it */
  69. if (!object) {
  70. obj_desc = NULL;
  71. object_type = ACPI_TYPE_ANY;
  72. }
  73. /*
  74. * If the source object is a namespace Node with an attached object,
  75. * we will use that (attached) object
  76. */
  77. else if ((ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED) &&
  78. ((struct acpi_namespace_node *)object)->object) {
  79. /*
  80. * Value passed is a name handle and that name has a
  81. * non-null value. Use that name's value and type.
  82. */
  83. obj_desc = ((struct acpi_namespace_node *)object)->object;
  84. object_type = ((struct acpi_namespace_node *)object)->type;
  85. }
  86. /*
  87. * Otherwise, we will use the parameter object, but we must type
  88. * it first
  89. */
  90. else {
  91. obj_desc = (union acpi_operand_object *)object;
  92. /* Use the given type */
  93. object_type = type;
  94. }
  95. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
  96. obj_desc, node, acpi_ut_get_node_name(node)));
  97. /* Detach an existing attached object if present */
  98. if (node->object) {
  99. acpi_ns_detach_object(node);
  100. }
  101. if (obj_desc) {
  102. /*
  103. * Must increment the new value's reference count
  104. * (if it is an internal object)
  105. */
  106. acpi_ut_add_reference(obj_desc);
  107. /*
  108. * Handle objects with multiple descriptors - walk
  109. * to the end of the descriptor list
  110. */
  111. last_obj_desc = obj_desc;
  112. while (last_obj_desc->common.next_object) {
  113. last_obj_desc = last_obj_desc->common.next_object;
  114. }
  115. /* Install the object at the front of the object list */
  116. last_obj_desc->common.next_object = node->object;
  117. }
  118. node->type = (u8) object_type;
  119. node->object = obj_desc;
  120. return_ACPI_STATUS(AE_OK);
  121. }
  122. /*******************************************************************************
  123. *
  124. * FUNCTION: acpi_ns_detach_object
  125. *
  126. * PARAMETERS: node - A Namespace node whose object will be detached
  127. *
  128. * RETURN: None.
  129. *
  130. * DESCRIPTION: Detach/delete an object associated with a namespace node.
  131. * if the object is an allocated object, it is freed.
  132. * Otherwise, the field is simply cleared.
  133. *
  134. ******************************************************************************/
  135. void acpi_ns_detach_object(struct acpi_namespace_node *node)
  136. {
  137. union acpi_operand_object *obj_desc;
  138. ACPI_FUNCTION_TRACE(ns_detach_object);
  139. obj_desc = node->object;
  140. if (!obj_desc || (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA)) {
  141. return_VOID;
  142. }
  143. if (node->flags & ANOBJ_ALLOCATED_BUFFER) {
  144. /* Free the dynamic aml buffer */
  145. if (obj_desc->common.type == ACPI_TYPE_METHOD) {
  146. ACPI_FREE(obj_desc->method.aml_start);
  147. }
  148. }
  149. /* Clear the Node entry in all cases */
  150. node->object = NULL;
  151. if (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) == ACPI_DESC_TYPE_OPERAND) {
  152. /* Unlink object from front of possible object list */
  153. node->object = obj_desc->common.next_object;
  154. /* Handle possible 2-descriptor object */
  155. if (node->object &&
  156. (node->object->common.type != ACPI_TYPE_LOCAL_DATA)) {
  157. node->object = node->object->common.next_object;
  158. }
  159. /*
  160. * Detach the object from any data objects (which are still held by
  161. * the namespace node)
  162. */
  163. if (obj_desc->common.next_object &&
  164. ((obj_desc->common.next_object)->common.type ==
  165. ACPI_TYPE_LOCAL_DATA)) {
  166. obj_desc->common.next_object = NULL;
  167. }
  168. }
  169. /* Reset the node type to untyped */
  170. node->type = ACPI_TYPE_ANY;
  171. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
  172. node, acpi_ut_get_node_name(node), obj_desc));
  173. /* Remove one reference on the object (and all subobjects) */
  174. acpi_ut_remove_reference(obj_desc);
  175. return_VOID;
  176. }
  177. /*******************************************************************************
  178. *
  179. * FUNCTION: acpi_ns_get_attached_object
  180. *
  181. * PARAMETERS: node - Namespace node
  182. *
  183. * RETURN: Current value of the object field from the Node whose
  184. * handle is passed
  185. *
  186. * DESCRIPTION: Obtain the object attached to a namespace node.
  187. *
  188. ******************************************************************************/
  189. union acpi_operand_object *acpi_ns_get_attached_object(struct
  190. acpi_namespace_node
  191. *node)
  192. {
  193. ACPI_FUNCTION_TRACE_PTR(ns_get_attached_object, node);
  194. if (!node) {
  195. ACPI_WARNING((AE_INFO, "Null Node ptr"));
  196. return_PTR(NULL);
  197. }
  198. if (!node->object ||
  199. ((ACPI_GET_DESCRIPTOR_TYPE(node->object) != ACPI_DESC_TYPE_OPERAND)
  200. && (ACPI_GET_DESCRIPTOR_TYPE(node->object) !=
  201. ACPI_DESC_TYPE_NAMED))
  202. || ((node->object)->common.type == ACPI_TYPE_LOCAL_DATA)) {
  203. return_PTR(NULL);
  204. }
  205. return_PTR(node->object);
  206. }
  207. /*******************************************************************************
  208. *
  209. * FUNCTION: acpi_ns_get_secondary_object
  210. *
  211. * PARAMETERS: node - Namespace node
  212. *
  213. * RETURN: Current value of the object field from the Node whose
  214. * handle is passed.
  215. *
  216. * DESCRIPTION: Obtain a secondary object associated with a namespace node.
  217. *
  218. ******************************************************************************/
  219. union acpi_operand_object *acpi_ns_get_secondary_object(union
  220. acpi_operand_object
  221. *obj_desc)
  222. {
  223. ACPI_FUNCTION_TRACE_PTR(ns_get_secondary_object, obj_desc);
  224. if ((!obj_desc) ||
  225. (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) ||
  226. (!obj_desc->common.next_object) ||
  227. ((obj_desc->common.next_object)->common.type ==
  228. ACPI_TYPE_LOCAL_DATA)) {
  229. return_PTR(NULL);
  230. }
  231. return_PTR(obj_desc->common.next_object);
  232. }
  233. /*******************************************************************************
  234. *
  235. * FUNCTION: acpi_ns_attach_data
  236. *
  237. * PARAMETERS: node - Namespace node
  238. * handler - Handler to be associated with the data
  239. * data - Data to be attached
  240. *
  241. * RETURN: Status
  242. *
  243. * DESCRIPTION: Low-level attach data. Create and attach a Data object.
  244. *
  245. ******************************************************************************/
  246. acpi_status
  247. acpi_ns_attach_data(struct acpi_namespace_node *node,
  248. acpi_object_handler handler, void *data)
  249. {
  250. union acpi_operand_object *prev_obj_desc;
  251. union acpi_operand_object *obj_desc;
  252. union acpi_operand_object *data_desc;
  253. /* We only allow one attachment per handler */
  254. prev_obj_desc = NULL;
  255. obj_desc = node->object;
  256. while (obj_desc) {
  257. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
  258. (obj_desc->data.handler == handler)) {
  259. return (AE_ALREADY_EXISTS);
  260. }
  261. prev_obj_desc = obj_desc;
  262. obj_desc = obj_desc->common.next_object;
  263. }
  264. /* Create an internal object for the data */
  265. data_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_DATA);
  266. if (!data_desc) {
  267. return (AE_NO_MEMORY);
  268. }
  269. data_desc->data.handler = handler;
  270. data_desc->data.pointer = data;
  271. /* Install the data object */
  272. if (prev_obj_desc) {
  273. prev_obj_desc->common.next_object = data_desc;
  274. } else {
  275. node->object = data_desc;
  276. }
  277. return (AE_OK);
  278. }
  279. /*******************************************************************************
  280. *
  281. * FUNCTION: acpi_ns_detach_data
  282. *
  283. * PARAMETERS: node - Namespace node
  284. * handler - Handler associated with the data
  285. *
  286. * RETURN: Status
  287. *
  288. * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
  289. * is responsible for the actual data.
  290. *
  291. ******************************************************************************/
  292. acpi_status
  293. acpi_ns_detach_data(struct acpi_namespace_node *node,
  294. acpi_object_handler handler)
  295. {
  296. union acpi_operand_object *obj_desc;
  297. union acpi_operand_object *prev_obj_desc;
  298. prev_obj_desc = NULL;
  299. obj_desc = node->object;
  300. while (obj_desc) {
  301. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
  302. (obj_desc->data.handler == handler)) {
  303. if (prev_obj_desc) {
  304. prev_obj_desc->common.next_object =
  305. obj_desc->common.next_object;
  306. } else {
  307. node->object = obj_desc->common.next_object;
  308. }
  309. acpi_ut_remove_reference(obj_desc);
  310. return (AE_OK);
  311. }
  312. prev_obj_desc = obj_desc;
  313. obj_desc = obj_desc->common.next_object;
  314. }
  315. return (AE_NOT_FOUND);
  316. }
  317. /*******************************************************************************
  318. *
  319. * FUNCTION: acpi_ns_get_attached_data
  320. *
  321. * PARAMETERS: node - Namespace node
  322. * handler - Handler associated with the data
  323. * data - Where the data is returned
  324. *
  325. * RETURN: Status
  326. *
  327. * DESCRIPTION: Low level interface to obtain data previously associated with
  328. * a namespace node.
  329. *
  330. ******************************************************************************/
  331. acpi_status
  332. acpi_ns_get_attached_data(struct acpi_namespace_node *node,
  333. acpi_object_handler handler, void **data)
  334. {
  335. union acpi_operand_object *obj_desc;
  336. obj_desc = node->object;
  337. while (obj_desc) {
  338. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
  339. (obj_desc->data.handler == handler)) {
  340. *data = obj_desc->data.pointer;
  341. return (AE_OK);
  342. }
  343. obj_desc = obj_desc->common.next_object;
  344. }
  345. return (AE_NOT_FOUND);
  346. }