1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: nsobject - Utilities for objects attached to namespace
7 ******************************************************************************/
13 #define _COMPONENT ACPI_NAMESPACE
14 ACPI_MODULE_NAME("nsobject")
16 /*******************************************************************************
18 * FUNCTION: acpi_ns_attach_object
20 * PARAMETERS: node - Parent Node
21 * object - Object to be attached
22 * type - Type of object, or ACPI_TYPE_ANY if not
27 * DESCRIPTION: Record the given object as the value associated with the
28 * name whose acpi_handle is passed. If Object is NULL
29 * and Type is ACPI_TYPE_ANY, set the name as having no value.
30 * Note: Future may require that the Node->Flags field be passed
33 * MUTEX: Assumes namespace is locked
35 ******************************************************************************/
37 acpi_ns_attach_object(struct acpi_namespace_node *node,
38 union acpi_operand_object *object, acpi_object_type type)
40 union acpi_operand_object *obj_desc;
41 union acpi_operand_object *last_obj_desc;
42 acpi_object_type object_type = ACPI_TYPE_ANY;
44 ACPI_FUNCTION_TRACE(ns_attach_object);
47 * Parameter validation
53 ACPI_ERROR((AE_INFO, "Null NamedObj handle"));
54 return_ACPI_STATUS(AE_BAD_PARAMETER);
57 if (!object && (ACPI_TYPE_ANY != type)) {
62 "Null object, but type not ACPI_TYPE_ANY"));
63 return_ACPI_STATUS(AE_BAD_PARAMETER);
66 if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
68 /* Not a name handle */
70 ACPI_ERROR((AE_INFO, "Invalid handle %p [%s]",
71 node, acpi_ut_get_descriptor_name(node)));
72 return_ACPI_STATUS(AE_BAD_PARAMETER);
75 /* Check if this object is already attached */
77 if (node->object == object) {
78 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
79 "Obj %p already installed in NameObj %p\n",
82 return_ACPI_STATUS(AE_OK);
85 /* If null object, we will just install it */
89 object_type = ACPI_TYPE_ANY;
93 * If the source object is a namespace Node with an attached object,
94 * we will use that (attached) object
96 else if ((ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED) &&
97 ((struct acpi_namespace_node *)object)->object) {
99 * Value passed is a name handle and that name has a
100 * non-null value. Use that name's value and type.
102 obj_desc = ((struct acpi_namespace_node *)object)->object;
103 object_type = ((struct acpi_namespace_node *)object)->type;
107 * Otherwise, we will use the parameter object, but we must type
111 obj_desc = (union acpi_operand_object *)object;
113 /* Use the given type */
118 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
119 obj_desc, node, acpi_ut_get_node_name(node)));
121 /* Detach an existing attached object if present */
124 acpi_ns_detach_object(node);
129 * Must increment the new value's reference count
130 * (if it is an internal object)
132 acpi_ut_add_reference(obj_desc);
135 * Handle objects with multiple descriptors - walk
136 * to the end of the descriptor list
138 last_obj_desc = obj_desc;
139 while (last_obj_desc->common.next_object) {
140 last_obj_desc = last_obj_desc->common.next_object;
143 /* Install the object at the front of the object list */
145 last_obj_desc->common.next_object = node->object;
148 node->type = (u8) object_type;
149 node->object = obj_desc;
151 return_ACPI_STATUS(AE_OK);
154 /*******************************************************************************
156 * FUNCTION: acpi_ns_detach_object
158 * PARAMETERS: node - A Namespace node whose object will be detached
162 * DESCRIPTION: Detach/delete an object associated with a namespace node.
163 * if the object is an allocated object, it is freed.
164 * Otherwise, the field is simply cleared.
166 ******************************************************************************/
168 void acpi_ns_detach_object(struct acpi_namespace_node *node)
170 union acpi_operand_object *obj_desc;
172 ACPI_FUNCTION_TRACE(ns_detach_object);
174 obj_desc = node->object;
176 if (!obj_desc || (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA)) {
180 if (node->flags & ANOBJ_ALLOCATED_BUFFER) {
182 /* Free the dynamic aml buffer */
184 if (obj_desc->common.type == ACPI_TYPE_METHOD) {
185 ACPI_FREE(obj_desc->method.aml_start);
189 if (obj_desc->common.type == ACPI_TYPE_REGION) {
190 acpi_ut_remove_address_range(obj_desc->region.space_id, node);
193 /* Clear the Node entry in all cases */
196 if (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) == ACPI_DESC_TYPE_OPERAND) {
198 /* Unlink object from front of possible object list */
200 node->object = obj_desc->common.next_object;
202 /* Handle possible 2-descriptor object */
205 (node->object->common.type != ACPI_TYPE_LOCAL_DATA)) {
206 node->object = node->object->common.next_object;
210 * Detach the object from any data objects (which are still held by
211 * the namespace node)
213 if (obj_desc->common.next_object &&
214 ((obj_desc->common.next_object)->common.type ==
215 ACPI_TYPE_LOCAL_DATA)) {
216 obj_desc->common.next_object = NULL;
220 /* Reset the node type to untyped */
222 node->type = ACPI_TYPE_ANY;
224 ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
225 node, acpi_ut_get_node_name(node), obj_desc));
227 /* Remove one reference on the object (and all subobjects) */
229 acpi_ut_remove_reference(obj_desc);
233 /*******************************************************************************
235 * FUNCTION: acpi_ns_get_attached_object
237 * PARAMETERS: node - Namespace node
239 * RETURN: Current value of the object field from the Node whose
242 * DESCRIPTION: Obtain the object attached to a namespace node.
244 ******************************************************************************/
246 union acpi_operand_object *acpi_ns_get_attached_object(struct
250 ACPI_FUNCTION_TRACE_PTR(ns_get_attached_object, node);
253 ACPI_WARNING((AE_INFO, "Null Node ptr"));
258 ((ACPI_GET_DESCRIPTOR_TYPE(node->object) != ACPI_DESC_TYPE_OPERAND)
259 && (ACPI_GET_DESCRIPTOR_TYPE(node->object) !=
260 ACPI_DESC_TYPE_NAMED))
261 || ((node->object)->common.type == ACPI_TYPE_LOCAL_DATA)) {
265 return_PTR(node->object);
268 /*******************************************************************************
270 * FUNCTION: acpi_ns_get_secondary_object
272 * PARAMETERS: node - Namespace node
274 * RETURN: Current value of the object field from the Node whose
277 * DESCRIPTION: Obtain a secondary object associated with a namespace node.
279 ******************************************************************************/
281 union acpi_operand_object *acpi_ns_get_secondary_object(union
285 ACPI_FUNCTION_TRACE_PTR(ns_get_secondary_object, obj_desc);
288 (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) ||
289 (!obj_desc->common.next_object) ||
290 ((obj_desc->common.next_object)->common.type ==
291 ACPI_TYPE_LOCAL_DATA)) {
295 return_PTR(obj_desc->common.next_object);
298 /*******************************************************************************
300 * FUNCTION: acpi_ns_attach_data
302 * PARAMETERS: node - Namespace node
303 * handler - Handler to be associated with the data
304 * data - Data to be attached
308 * DESCRIPTION: Low-level attach data. Create and attach a Data object.
310 ******************************************************************************/
313 acpi_ns_attach_data(struct acpi_namespace_node *node,
314 acpi_object_handler handler, void *data)
316 union acpi_operand_object *prev_obj_desc;
317 union acpi_operand_object *obj_desc;
318 union acpi_operand_object *data_desc;
320 /* We only allow one attachment per handler */
322 prev_obj_desc = NULL;
323 obj_desc = node->object;
325 if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
326 (obj_desc->data.handler == handler)) {
327 return (AE_ALREADY_EXISTS);
330 prev_obj_desc = obj_desc;
331 obj_desc = obj_desc->common.next_object;
334 /* Create an internal object for the data */
336 data_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_DATA);
338 return (AE_NO_MEMORY);
341 data_desc->data.handler = handler;
342 data_desc->data.pointer = data;
344 /* Install the data object */
347 prev_obj_desc->common.next_object = data_desc;
349 node->object = data_desc;
355 /*******************************************************************************
357 * FUNCTION: acpi_ns_detach_data
359 * PARAMETERS: node - Namespace node
360 * handler - Handler associated with the data
364 * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
365 * is responsible for the actual data.
367 ******************************************************************************/
370 acpi_ns_detach_data(struct acpi_namespace_node *node,
371 acpi_object_handler handler)
373 union acpi_operand_object *obj_desc;
374 union acpi_operand_object *prev_obj_desc;
376 prev_obj_desc = NULL;
377 obj_desc = node->object;
379 if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
380 (obj_desc->data.handler == handler)) {
382 prev_obj_desc->common.next_object =
383 obj_desc->common.next_object;
385 node->object = obj_desc->common.next_object;
388 acpi_ut_remove_reference(obj_desc);
392 prev_obj_desc = obj_desc;
393 obj_desc = obj_desc->common.next_object;
396 return (AE_NOT_FOUND);
399 /*******************************************************************************
401 * FUNCTION: acpi_ns_get_attached_data
403 * PARAMETERS: node - Namespace node
404 * handler - Handler associated with the data
405 * data - Where the data is returned
409 * DESCRIPTION: Low level interface to obtain data previously associated with
412 ******************************************************************************/
415 acpi_ns_get_attached_data(struct acpi_namespace_node *node,
416 acpi_object_handler handler, void **data)
418 union acpi_operand_object *obj_desc;
420 obj_desc = node->object;
422 if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
423 (obj_desc->data.handler == handler)) {
424 *data = obj_desc->data.pointer;
428 obj_desc = obj_desc->common.next_object;
431 return (AE_NOT_FOUND);