xref: /openbmc/linux/drivers/acpi/acpica/utobject.c (revision 2359ccdd)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: utobject - ACPI object create/delete/size/cache routines
5  *
6  * Copyright (C) 2000 - 2018, Intel Corp.
7  *
8  *****************************************************************************/
9 
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acnamesp.h"
13 
14 #define _COMPONENT          ACPI_UTILITIES
15 ACPI_MODULE_NAME("utobject")
16 
17 /* Local prototypes */
18 static acpi_status
19 acpi_ut_get_simple_object_size(union acpi_operand_object *obj,
20 			       acpi_size *obj_length);
21 
22 static acpi_status
23 acpi_ut_get_package_object_size(union acpi_operand_object *obj,
24 				acpi_size *obj_length);
25 
26 static acpi_status
27 acpi_ut_get_element_length(u8 object_type,
28 			   union acpi_operand_object *source_object,
29 			   union acpi_generic_state *state, void *context);
30 
31 /*******************************************************************************
32  *
33  * FUNCTION:    acpi_ut_create_internal_object_dbg
34  *
35  * PARAMETERS:  module_name         - Source file name of caller
36  *              line_number         - Line number of caller
37  *              component_id        - Component type of caller
38  *              type                - ACPI Type of the new object
39  *
40  * RETURN:      A new internal object, null on failure
41  *
42  * DESCRIPTION: Create and initialize a new internal object.
43  *
44  * NOTE:        We always allocate the worst-case object descriptor because
45  *              these objects are cached, and we want them to be
46  *              one-size-satisifies-any-request. This in itself may not be
47  *              the most memory efficient, but the efficiency of the object
48  *              cache should more than make up for this!
49  *
50  ******************************************************************************/
51 
52 union acpi_operand_object *acpi_ut_create_internal_object_dbg(const char
53 							      *module_name,
54 							      u32 line_number,
55 							      u32 component_id,
56 							      acpi_object_type
57 							      type)
58 {
59 	union acpi_operand_object *object;
60 	union acpi_operand_object *second_object;
61 
62 	ACPI_FUNCTION_TRACE_STR(ut_create_internal_object_dbg,
63 				acpi_ut_get_type_name(type));
64 
65 	/* Allocate the raw object descriptor */
66 
67 	object =
68 	    acpi_ut_allocate_object_desc_dbg(module_name, line_number,
69 					     component_id);
70 	if (!object) {
71 		return_PTR(NULL);
72 	}
73 
74 	switch (type) {
75 	case ACPI_TYPE_REGION:
76 	case ACPI_TYPE_BUFFER_FIELD:
77 	case ACPI_TYPE_LOCAL_BANK_FIELD:
78 
79 		/* These types require a secondary object */
80 
81 		second_object =
82 		    acpi_ut_allocate_object_desc_dbg(module_name, line_number,
83 						     component_id);
84 		if (!second_object) {
85 			acpi_ut_delete_object_desc(object);
86 			return_PTR(NULL);
87 		}
88 
89 		second_object->common.type = ACPI_TYPE_LOCAL_EXTRA;
90 		second_object->common.reference_count = 1;
91 
92 		/* Link the second object to the first */
93 
94 		object->common.next_object = second_object;
95 		break;
96 
97 	default:
98 
99 		/* All others have no secondary object */
100 		break;
101 	}
102 
103 	/* Save the object type in the object descriptor */
104 
105 	object->common.type = (u8) type;
106 
107 	/* Init the reference count */
108 
109 	object->common.reference_count = 1;
110 
111 	/* Any per-type initialization should go here */
112 
113 	return_PTR(object);
114 }
115 
116 /*******************************************************************************
117  *
118  * FUNCTION:    acpi_ut_create_package_object
119  *
120  * PARAMETERS:  count               - Number of package elements
121  *
122  * RETURN:      Pointer to a new Package object, null on failure
123  *
124  * DESCRIPTION: Create a fully initialized package object
125  *
126  ******************************************************************************/
127 
128 union acpi_operand_object *acpi_ut_create_package_object(u32 count)
129 {
130 	union acpi_operand_object *package_desc;
131 	union acpi_operand_object **package_elements;
132 
133 	ACPI_FUNCTION_TRACE_U32(ut_create_package_object, count);
134 
135 	/* Create a new Package object */
136 
137 	package_desc = acpi_ut_create_internal_object(ACPI_TYPE_PACKAGE);
138 	if (!package_desc) {
139 		return_PTR(NULL);
140 	}
141 
142 	/*
143 	 * Create the element array. Count+1 allows the array to be null
144 	 * terminated.
145 	 */
146 	package_elements = ACPI_ALLOCATE_ZEROED(((acpi_size)count +
147 						 1) * sizeof(void *));
148 	if (!package_elements) {
149 		ACPI_FREE(package_desc);
150 		return_PTR(NULL);
151 	}
152 
153 	package_desc->package.count = count;
154 	package_desc->package.elements = package_elements;
155 	return_PTR(package_desc);
156 }
157 
158 /*******************************************************************************
159  *
160  * FUNCTION:    acpi_ut_create_integer_object
161  *
162  * PARAMETERS:  initial_value       - Initial value for the integer
163  *
164  * RETURN:      Pointer to a new Integer object, null on failure
165  *
166  * DESCRIPTION: Create an initialized integer object
167  *
168  ******************************************************************************/
169 
170 union acpi_operand_object *acpi_ut_create_integer_object(u64 initial_value)
171 {
172 	union acpi_operand_object *integer_desc;
173 
174 	ACPI_FUNCTION_TRACE(ut_create_integer_object);
175 
176 	/* Create and initialize a new integer object */
177 
178 	integer_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
179 	if (!integer_desc) {
180 		return_PTR(NULL);
181 	}
182 
183 	integer_desc->integer.value = initial_value;
184 	return_PTR(integer_desc);
185 }
186 
187 /*******************************************************************************
188  *
189  * FUNCTION:    acpi_ut_create_buffer_object
190  *
191  * PARAMETERS:  buffer_size            - Size of buffer to be created
192  *
193  * RETURN:      Pointer to a new Buffer object, null on failure
194  *
195  * DESCRIPTION: Create a fully initialized buffer object
196  *
197  ******************************************************************************/
198 
199 union acpi_operand_object *acpi_ut_create_buffer_object(acpi_size buffer_size)
200 {
201 	union acpi_operand_object *buffer_desc;
202 	u8 *buffer = NULL;
203 
204 	ACPI_FUNCTION_TRACE_U32(ut_create_buffer_object, buffer_size);
205 
206 	/* Create a new Buffer object */
207 
208 	buffer_desc = acpi_ut_create_internal_object(ACPI_TYPE_BUFFER);
209 	if (!buffer_desc) {
210 		return_PTR(NULL);
211 	}
212 
213 	/* Create an actual buffer only if size > 0 */
214 
215 	if (buffer_size > 0) {
216 
217 		/* Allocate the actual buffer */
218 
219 		buffer = ACPI_ALLOCATE_ZEROED(buffer_size);
220 		if (!buffer) {
221 			ACPI_ERROR((AE_INFO, "Could not allocate size %u",
222 				    (u32)buffer_size));
223 
224 			acpi_ut_remove_reference(buffer_desc);
225 			return_PTR(NULL);
226 		}
227 	}
228 
229 	/* Complete buffer object initialization */
230 
231 	buffer_desc->buffer.flags |= AOPOBJ_DATA_VALID;
232 	buffer_desc->buffer.pointer = buffer;
233 	buffer_desc->buffer.length = (u32) buffer_size;
234 
235 	/* Return the new buffer descriptor */
236 
237 	return_PTR(buffer_desc);
238 }
239 
240 /*******************************************************************************
241  *
242  * FUNCTION:    acpi_ut_create_string_object
243  *
244  * PARAMETERS:  string_size         - Size of string to be created. Does not
245  *                                    include NULL terminator, this is added
246  *                                    automatically.
247  *
248  * RETURN:      Pointer to a new String object
249  *
250  * DESCRIPTION: Create a fully initialized string object
251  *
252  ******************************************************************************/
253 
254 union acpi_operand_object *acpi_ut_create_string_object(acpi_size string_size)
255 {
256 	union acpi_operand_object *string_desc;
257 	char *string;
258 
259 	ACPI_FUNCTION_TRACE_U32(ut_create_string_object, string_size);
260 
261 	/* Create a new String object */
262 
263 	string_desc = acpi_ut_create_internal_object(ACPI_TYPE_STRING);
264 	if (!string_desc) {
265 		return_PTR(NULL);
266 	}
267 
268 	/*
269 	 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
270 	 * NOTE: Zero-length strings are NULL terminated
271 	 */
272 	string = ACPI_ALLOCATE_ZEROED(string_size + 1);
273 	if (!string) {
274 		ACPI_ERROR((AE_INFO, "Could not allocate size %u",
275 			    (u32)string_size));
276 
277 		acpi_ut_remove_reference(string_desc);
278 		return_PTR(NULL);
279 	}
280 
281 	/* Complete string object initialization */
282 
283 	string_desc->string.pointer = string;
284 	string_desc->string.length = (u32) string_size;
285 
286 	/* Return the new string descriptor */
287 
288 	return_PTR(string_desc);
289 }
290 
291 /*******************************************************************************
292  *
293  * FUNCTION:    acpi_ut_valid_internal_object
294  *
295  * PARAMETERS:  object              - Object to be validated
296  *
297  * RETURN:      TRUE if object is valid, FALSE otherwise
298  *
299  * DESCRIPTION: Validate a pointer to be of type union acpi_operand_object
300  *
301  ******************************************************************************/
302 
303 u8 acpi_ut_valid_internal_object(void *object)
304 {
305 
306 	ACPI_FUNCTION_NAME(ut_valid_internal_object);
307 
308 	/* Check for a null pointer */
309 
310 	if (!object) {
311 		ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "**** Null Object Ptr\n"));
312 		return (FALSE);
313 	}
314 
315 	/* Check the descriptor type field */
316 
317 	switch (ACPI_GET_DESCRIPTOR_TYPE(object)) {
318 	case ACPI_DESC_TYPE_OPERAND:
319 
320 		/* The object appears to be a valid union acpi_operand_object */
321 
322 		return (TRUE);
323 
324 	default:
325 
326 		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
327 				  "%p is not an ACPI operand obj [%s]\n",
328 				  object, acpi_ut_get_descriptor_name(object)));
329 		break;
330 	}
331 
332 	return (FALSE);
333 }
334 
335 /*******************************************************************************
336  *
337  * FUNCTION:    acpi_ut_allocate_object_desc_dbg
338  *
339  * PARAMETERS:  module_name         - Caller's module name (for error output)
340  *              line_number         - Caller's line number (for error output)
341  *              component_id        - Caller's component ID (for error output)
342  *
343  * RETURN:      Pointer to newly allocated object descriptor. Null on error
344  *
345  * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
346  *              error conditions.
347  *
348  ******************************************************************************/
349 
350 void *acpi_ut_allocate_object_desc_dbg(const char *module_name,
351 				       u32 line_number, u32 component_id)
352 {
353 	union acpi_operand_object *object;
354 
355 	ACPI_FUNCTION_TRACE(ut_allocate_object_desc_dbg);
356 
357 	object = acpi_os_acquire_object(acpi_gbl_operand_cache);
358 	if (!object) {
359 		ACPI_ERROR((module_name, line_number,
360 			    "Could not allocate an object descriptor"));
361 
362 		return_PTR(NULL);
363 	}
364 
365 	/* Mark the descriptor type */
366 
367 	ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_OPERAND);
368 
369 	ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
370 			  object, (u32) sizeof(union acpi_operand_object)));
371 
372 	return_PTR(object);
373 }
374 
375 /*******************************************************************************
376  *
377  * FUNCTION:    acpi_ut_delete_object_desc
378  *
379  * PARAMETERS:  object          - An Acpi internal object to be deleted
380  *
381  * RETURN:      None.
382  *
383  * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
384  *
385  ******************************************************************************/
386 
387 void acpi_ut_delete_object_desc(union acpi_operand_object *object)
388 {
389 	ACPI_FUNCTION_TRACE_PTR(ut_delete_object_desc, object);
390 
391 	/* Object must be of type union acpi_operand_object */
392 
393 	if (ACPI_GET_DESCRIPTOR_TYPE(object) != ACPI_DESC_TYPE_OPERAND) {
394 		ACPI_ERROR((AE_INFO,
395 			    "%p is not an ACPI Operand object [%s]", object,
396 			    acpi_ut_get_descriptor_name(object)));
397 		return_VOID;
398 	}
399 
400 	(void)acpi_os_release_object(acpi_gbl_operand_cache, object);
401 	return_VOID;
402 }
403 
404 /*******************************************************************************
405  *
406  * FUNCTION:    acpi_ut_get_simple_object_size
407  *
408  * PARAMETERS:  internal_object    - An ACPI operand object
409  *              obj_length         - Where the length is returned
410  *
411  * RETURN:      Status
412  *
413  * DESCRIPTION: This function is called to determine the space required to
414  *              contain a simple object for return to an external user.
415  *
416  *              The length includes the object structure plus any additional
417  *              needed space.
418  *
419  ******************************************************************************/
420 
421 static acpi_status
422 acpi_ut_get_simple_object_size(union acpi_operand_object *internal_object,
423 			       acpi_size *obj_length)
424 {
425 	acpi_size length;
426 	acpi_size size;
427 	acpi_status status = AE_OK;
428 
429 	ACPI_FUNCTION_TRACE_PTR(ut_get_simple_object_size, internal_object);
430 
431 	/* Start with the length of the (external) Acpi object */
432 
433 	length = sizeof(union acpi_object);
434 
435 	/* A NULL object is allowed, can be a legal uninitialized package element */
436 
437 	if (!internal_object) {
438 	/*
439 		 * Object is NULL, just return the length of union acpi_object
440 		 * (A NULL union acpi_object is an object of all zeroes.)
441 	 */
442 		*obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD(length);
443 		return_ACPI_STATUS(AE_OK);
444 	}
445 
446 	/* A Namespace Node should never appear here */
447 
448 	if (ACPI_GET_DESCRIPTOR_TYPE(internal_object) == ACPI_DESC_TYPE_NAMED) {
449 
450 		/* A namespace node should never get here */
451 
452 		ACPI_ERROR((AE_INFO,
453 			    "Received a namespace node [%4.4s] "
454 			    "where an operand object is required",
455 			    ACPI_CAST_PTR(struct acpi_namespace_node,
456 					  internal_object)->name.ascii));
457 		return_ACPI_STATUS(AE_AML_INTERNAL);
458 	}
459 
460 	/*
461 	 * The final length depends on the object type
462 	 * Strings and Buffers are packed right up against the parent object and
463 	 * must be accessed bytewise or there may be alignment problems on
464 	 * certain processors
465 	 */
466 	switch (internal_object->common.type) {
467 	case ACPI_TYPE_STRING:
468 
469 		length += (acpi_size)internal_object->string.length + 1;
470 		break;
471 
472 	case ACPI_TYPE_BUFFER:
473 
474 		length += (acpi_size)internal_object->buffer.length;
475 		break;
476 
477 	case ACPI_TYPE_INTEGER:
478 	case ACPI_TYPE_PROCESSOR:
479 	case ACPI_TYPE_POWER:
480 
481 		/* No extra data for these types */
482 
483 		break;
484 
485 	case ACPI_TYPE_LOCAL_REFERENCE:
486 
487 		switch (internal_object->reference.class) {
488 		case ACPI_REFCLASS_NAME:
489 			/*
490 			 * Get the actual length of the full pathname to this object.
491 			 * The reference will be converted to the pathname to the object
492 			 */
493 			size =
494 			    acpi_ns_get_pathname_length(internal_object->
495 							reference.node);
496 			if (!size) {
497 				return_ACPI_STATUS(AE_BAD_PARAMETER);
498 			}
499 
500 			length += ACPI_ROUND_UP_TO_NATIVE_WORD(size);
501 			break;
502 
503 		default:
504 			/*
505 			 * No other reference opcodes are supported.
506 			 * Notably, Locals and Args are not supported, but this may be
507 			 * required eventually.
508 			 */
509 			ACPI_ERROR((AE_INFO,
510 				    "Cannot convert to external object - "
511 				    "unsupported Reference Class [%s] 0x%X in object %p",
512 				    acpi_ut_get_reference_name(internal_object),
513 				    internal_object->reference.class,
514 				    internal_object));
515 			status = AE_TYPE;
516 			break;
517 		}
518 		break;
519 
520 	default:
521 
522 		ACPI_ERROR((AE_INFO, "Cannot convert to external object - "
523 			    "unsupported type [%s] 0x%X in object %p",
524 			    acpi_ut_get_object_type_name(internal_object),
525 			    internal_object->common.type, internal_object));
526 		status = AE_TYPE;
527 		break;
528 	}
529 
530 	/*
531 	 * Account for the space required by the object rounded up to the next
532 	 * multiple of the machine word size. This keeps each object aligned
533 	 * on a machine word boundary. (preventing alignment faults on some
534 	 * machines.)
535 	 */
536 	*obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD(length);
537 	return_ACPI_STATUS(status);
538 }
539 
540 /*******************************************************************************
541  *
542  * FUNCTION:    acpi_ut_get_element_length
543  *
544  * PARAMETERS:  acpi_pkg_callback
545  *
546  * RETURN:      Status
547  *
548  * DESCRIPTION: Get the length of one package element.
549  *
550  ******************************************************************************/
551 
552 static acpi_status
553 acpi_ut_get_element_length(u8 object_type,
554 			   union acpi_operand_object *source_object,
555 			   union acpi_generic_state *state, void *context)
556 {
557 	acpi_status status = AE_OK;
558 	struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
559 	acpi_size object_space;
560 
561 	switch (object_type) {
562 	case ACPI_COPY_TYPE_SIMPLE:
563 		/*
564 		 * Simple object - just get the size (Null object/entry is handled
565 		 * here also) and sum it into the running package length
566 		 */
567 		status =
568 		    acpi_ut_get_simple_object_size(source_object,
569 						   &object_space);
570 		if (ACPI_FAILURE(status)) {
571 			return (status);
572 		}
573 
574 		info->length += object_space;
575 		break;
576 
577 	case ACPI_COPY_TYPE_PACKAGE:
578 
579 		/* Package object - nothing much to do here, let the walk handle it */
580 
581 		info->num_packages++;
582 		state->pkg.this_target_obj = NULL;
583 		break;
584 
585 	default:
586 
587 		/* No other types allowed */
588 
589 		return (AE_BAD_PARAMETER);
590 	}
591 
592 	return (status);
593 }
594 
595 /*******************************************************************************
596  *
597  * FUNCTION:    acpi_ut_get_package_object_size
598  *
599  * PARAMETERS:  internal_object     - An ACPI internal object
600  *              obj_length          - Where the length is returned
601  *
602  * RETURN:      Status
603  *
604  * DESCRIPTION: This function is called to determine the space required to
605  *              contain a package object for return to an external user.
606  *
607  *              This is moderately complex since a package contains other
608  *              objects including packages.
609  *
610  ******************************************************************************/
611 
612 static acpi_status
613 acpi_ut_get_package_object_size(union acpi_operand_object *internal_object,
614 				acpi_size *obj_length)
615 {
616 	acpi_status status;
617 	struct acpi_pkg_info info;
618 
619 	ACPI_FUNCTION_TRACE_PTR(ut_get_package_object_size, internal_object);
620 
621 	info.length = 0;
622 	info.object_space = 0;
623 	info.num_packages = 1;
624 
625 	status =
626 	    acpi_ut_walk_package_tree(internal_object, NULL,
627 				      acpi_ut_get_element_length, &info);
628 	if (ACPI_FAILURE(status)) {
629 		return_ACPI_STATUS(status);
630 	}
631 
632 	/*
633 	 * We have handled all of the objects in all levels of the package.
634 	 * just add the length of the package objects themselves.
635 	 * Round up to the next machine word.
636 	 */
637 	info.length +=
638 	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object)) *
639 	    (acpi_size)info.num_packages;
640 
641 	/* Return the total package length */
642 
643 	*obj_length = info.length;
644 	return_ACPI_STATUS(status);
645 }
646 
647 /*******************************************************************************
648  *
649  * FUNCTION:    acpi_ut_get_object_size
650  *
651  * PARAMETERS:  internal_object     - An ACPI internal object
652  *              obj_length          - Where the length will be returned
653  *
654  * RETURN:      Status
655  *
656  * DESCRIPTION: This function is called to determine the space required to
657  *              contain an object for return to an API user.
658  *
659  ******************************************************************************/
660 
661 acpi_status
662 acpi_ut_get_object_size(union acpi_operand_object *internal_object,
663 			acpi_size *obj_length)
664 {
665 	acpi_status status;
666 
667 	ACPI_FUNCTION_ENTRY();
668 
669 	if ((ACPI_GET_DESCRIPTOR_TYPE(internal_object) ==
670 	     ACPI_DESC_TYPE_OPERAND) &&
671 	    (internal_object->common.type == ACPI_TYPE_PACKAGE)) {
672 		status =
673 		    acpi_ut_get_package_object_size(internal_object,
674 						    obj_length);
675 	} else {
676 		status =
677 		    acpi_ut_get_simple_object_size(internal_object, obj_length);
678 	}
679 
680 	return (status);
681 }
682