nsobject.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. if (obj_desc->common.type == ACPI_TYPE_REGION) {
  150. acpi_ut_remove_address_range(obj_desc->region.space_id, node);
  151. }
  152. /* Clear the Node entry in all cases */
  153. node->object = NULL;
  154. if (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) == ACPI_DESC_TYPE_OPERAND) {
  155. /* Unlink object from front of possible object list */
  156. node->object = obj_desc->common.next_object;
  157. /* Handle possible 2-descriptor object */
  158. if (node->object &&
  159. (node->object->common.type != ACPI_TYPE_LOCAL_DATA)) {
  160. node->object = node->object->common.next_object;
  161. }
  162. /*
  163. * Detach the object from any data objects (which are still held by
  164. * the namespace node)
  165. */
  166. if (obj_desc->common.next_object &&
  167. ((obj_desc->common.next_object)->common.type ==
  168. ACPI_TYPE_LOCAL_DATA)) {
  169. obj_desc->common.next_object = NULL;
  170. }
  171. }
  172. /* Reset the node type to untyped */
  173. node->type = ACPI_TYPE_ANY;
  174. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
  175. node, acpi_ut_get_node_name(node), obj_desc));
  176. /* Remove one reference on the object (and all subobjects) */
  177. acpi_ut_remove_reference(obj_desc);
  178. return_VOID;
  179. }
  180. /*******************************************************************************
  181. *
  182. * FUNCTION: acpi_ns_get_attached_object
  183. *
  184. * PARAMETERS: node - Namespace node
  185. *
  186. * RETURN: Current value of the object field from the Node whose
  187. * handle is passed
  188. *
  189. * DESCRIPTION: Obtain the object attached to a namespace node.
  190. *
  191. ******************************************************************************/
  192. union acpi_operand_object *acpi_ns_get_attached_object(struct
  193. acpi_namespace_node
  194. *node)
  195. {
  196. ACPI_FUNCTION_TRACE_PTR(ns_get_attached_object, node);
  197. if (!node) {
  198. ACPI_WARNING((AE_INFO, "Null Node ptr"));
  199. return_PTR(NULL);
  200. }
  201. if (!node->object ||
  202. ((ACPI_GET_DESCRIPTOR_TYPE(node->object) != ACPI_DESC_TYPE_OPERAND)
  203. && (ACPI_GET_DESCRIPTOR_TYPE(node->object) !=
  204. ACPI_DESC_TYPE_NAMED))
  205. || ((node->object)->common.type == ACPI_TYPE_LOCAL_DATA)) {
  206. return_PTR(NULL);
  207. }
  208. return_PTR(node->object);
  209. }
  210. /*******************************************************************************
  211. *
  212. * FUNCTION: acpi_ns_get_secondary_object
  213. *
  214. * PARAMETERS: node - Namespace node
  215. *
  216. * RETURN: Current value of the object field from the Node whose
  217. * handle is passed.
  218. *
  219. * DESCRIPTION: Obtain a secondary object associated with a namespace node.
  220. *
  221. ******************************************************************************/
  222. union acpi_operand_object *acpi_ns_get_secondary_object(union
  223. acpi_operand_object
  224. *obj_desc)
  225. {
  226. ACPI_FUNCTION_TRACE_PTR(ns_get_secondary_object, obj_desc);
  227. if ((!obj_desc) ||
  228. (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) ||
  229. (!obj_desc->common.next_object) ||
  230. ((obj_desc->common.next_object)->common.type ==
  231. ACPI_TYPE_LOCAL_DATA)) {
  232. return_PTR(NULL);
  233. }
  234. return_PTR(obj_desc->common.next_object);
  235. }
  236. /*******************************************************************************
  237. *
  238. * FUNCTION: acpi_ns_attach_data
  239. *
  240. * PARAMETERS: node - Namespace node
  241. * handler - Handler to be associated with the data
  242. * data - Data to be attached
  243. *
  244. * RETURN: Status
  245. *
  246. * DESCRIPTION: Low-level attach data. Create and attach a Data object.
  247. *
  248. ******************************************************************************/
  249. acpi_status
  250. acpi_ns_attach_data(struct acpi_namespace_node *node,
  251. acpi_object_handler handler, void *data)
  252. {
  253. union acpi_operand_object *prev_obj_desc;
  254. union acpi_operand_object *obj_desc;
  255. union acpi_operand_object *data_desc;
  256. /* We only allow one attachment per handler */
  257. prev_obj_desc = NULL;
  258. obj_desc = node->object;
  259. while (obj_desc) {
  260. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
  261. (obj_desc->data.handler == handler)) {
  262. return (AE_ALREADY_EXISTS);
  263. }
  264. prev_obj_desc = obj_desc;
  265. obj_desc = obj_desc->common.next_object;
  266. }
  267. /* Create an internal object for the data */
  268. data_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_DATA);
  269. if (!data_desc) {
  270. return (AE_NO_MEMORY);
  271. }
  272. data_desc->data.handler = handler;
  273. data_desc->data.pointer = data;
  274. /* Install the data object */
  275. if (prev_obj_desc) {
  276. prev_obj_desc->common.next_object = data_desc;
  277. } else {
  278. node->object = data_desc;
  279. }
  280. return (AE_OK);
  281. }
  282. /*******************************************************************************
  283. *
  284. * FUNCTION: acpi_ns_detach_data
  285. *
  286. * PARAMETERS: node - Namespace node
  287. * handler - Handler associated with the data
  288. *
  289. * RETURN: Status
  290. *
  291. * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
  292. * is responsible for the actual data.
  293. *
  294. ******************************************************************************/
  295. acpi_status
  296. acpi_ns_detach_data(struct acpi_namespace_node *node,
  297. acpi_object_handler handler)
  298. {
  299. union acpi_operand_object *obj_desc;
  300. union acpi_operand_object *prev_obj_desc;
  301. prev_obj_desc = NULL;
  302. obj_desc = node->object;
  303. while (obj_desc) {
  304. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
  305. (obj_desc->data.handler == handler)) {
  306. if (prev_obj_desc) {
  307. prev_obj_desc->common.next_object =
  308. obj_desc->common.next_object;
  309. } else {
  310. node->object = obj_desc->common.next_object;
  311. }
  312. acpi_ut_remove_reference(obj_desc);
  313. return (AE_OK);
  314. }
  315. prev_obj_desc = obj_desc;
  316. obj_desc = obj_desc->common.next_object;
  317. }
  318. return (AE_NOT_FOUND);
  319. }
  320. /*******************************************************************************
  321. *
  322. * FUNCTION: acpi_ns_get_attached_data
  323. *
  324. * PARAMETERS: node - Namespace node
  325. * handler - Handler associated with the data
  326. * data - Where the data is returned
  327. *
  328. * RETURN: Status
  329. *
  330. * DESCRIPTION: Low level interface to obtain data previously associated with
  331. * a namespace node.
  332. *
  333. ******************************************************************************/
  334. acpi_status
  335. acpi_ns_get_attached_data(struct acpi_namespace_node *node,
  336. acpi_object_handler handler, void **data)
  337. {
  338. union acpi_operand_object *obj_desc;
  339. obj_desc = node->object;
  340. while (obj_desc) {
  341. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
  342. (obj_desc->data.handler == handler)) {
  343. *data = obj_desc->data.pointer;
  344. return (AE_OK);
  345. }
  346. obj_desc = obj_desc->common.next_object;
  347. }
  348. return (AE_NOT_FOUND);
  349. }