xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpica/utalloc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Module Name: utalloc - local memory allocation routines
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *****************************************************************************/
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acdebug.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define _COMPONENT          ACPI_UTILITIES
15*4882a593Smuzhiyun ACPI_MODULE_NAME("utalloc")
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #if !defined (USE_NATIVE_ALLOCATE_ZEROED)
18*4882a593Smuzhiyun /*******************************************************************************
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * FUNCTION:    acpi_os_allocate_zeroed
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * PARAMETERS:  size                - Size of the allocation
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * RETURN:      Address of the allocated memory on success, NULL on failure.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
27*4882a593Smuzhiyun  *              This is the default implementation. Can be overridden via the
28*4882a593Smuzhiyun  *              USE_NATIVE_ALLOCATE_ZEROED flag.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  ******************************************************************************/
acpi_os_allocate_zeroed(acpi_size size)31*4882a593Smuzhiyun void *acpi_os_allocate_zeroed(acpi_size size)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	void *allocation;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	ACPI_FUNCTION_ENTRY();
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	allocation = acpi_os_allocate(size);
38*4882a593Smuzhiyun 	if (allocation) {
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 		/* Clear the memory block */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 		memset(allocation, 0, size);
43*4882a593Smuzhiyun 	}
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	return (allocation);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #endif				/* !USE_NATIVE_ALLOCATE_ZEROED */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*******************************************************************************
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_create_caches
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * PARAMETERS:  None
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * RETURN:      Status
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * DESCRIPTION: Create all local caches
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  ******************************************************************************/
61*4882a593Smuzhiyun 
acpi_ut_create_caches(void)62*4882a593Smuzhiyun acpi_status acpi_ut_create_caches(void)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	acpi_status status;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/* Object Caches, for frequently used objects */
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	status =
69*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-Namespace",
70*4882a593Smuzhiyun 				 sizeof(struct acpi_namespace_node),
71*4882a593Smuzhiyun 				 ACPI_MAX_NAMESPACE_CACHE_DEPTH,
72*4882a593Smuzhiyun 				 &acpi_gbl_namespace_cache);
73*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
74*4882a593Smuzhiyun 		return (status);
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	status =
78*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-State", sizeof(union acpi_generic_state),
79*4882a593Smuzhiyun 				 ACPI_MAX_STATE_CACHE_DEPTH,
80*4882a593Smuzhiyun 				 &acpi_gbl_state_cache);
81*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
82*4882a593Smuzhiyun 		return (status);
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	status =
86*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-Parse",
87*4882a593Smuzhiyun 				 sizeof(struct acpi_parse_obj_common),
88*4882a593Smuzhiyun 				 ACPI_MAX_PARSE_CACHE_DEPTH,
89*4882a593Smuzhiyun 				 &acpi_gbl_ps_node_cache);
90*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
91*4882a593Smuzhiyun 		return (status);
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	status =
95*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-ParseExt",
96*4882a593Smuzhiyun 				 sizeof(struct acpi_parse_obj_named),
97*4882a593Smuzhiyun 				 ACPI_MAX_EXTPARSE_CACHE_DEPTH,
98*4882a593Smuzhiyun 				 &acpi_gbl_ps_node_ext_cache);
99*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
100*4882a593Smuzhiyun 		return (status);
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	status =
104*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-Operand",
105*4882a593Smuzhiyun 				 sizeof(union acpi_operand_object),
106*4882a593Smuzhiyun 				 ACPI_MAX_OBJECT_CACHE_DEPTH,
107*4882a593Smuzhiyun 				 &acpi_gbl_operand_cache);
108*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
109*4882a593Smuzhiyun 		return (status);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun #ifdef ACPI_ASL_COMPILER
112*4882a593Smuzhiyun 	/*
113*4882a593Smuzhiyun 	 * For use with the ASL-/ASL+ option. This cache keeps track of regular
114*4882a593Smuzhiyun 	 * 0xA9 0x01 comments.
115*4882a593Smuzhiyun 	 */
116*4882a593Smuzhiyun 	status =
117*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-Comment",
118*4882a593Smuzhiyun 				 sizeof(struct acpi_comment_node),
119*4882a593Smuzhiyun 				 ACPI_MAX_COMMENT_CACHE_DEPTH,
120*4882a593Smuzhiyun 				 &acpi_gbl_reg_comment_cache);
121*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
122*4882a593Smuzhiyun 		return (status);
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/*
126*4882a593Smuzhiyun 	 * This cache keeps track of the starting addresses of where the comments
127*4882a593Smuzhiyun 	 * lie. This helps prevent duplication of comments.
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	status =
130*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-Comment-Addr",
131*4882a593Smuzhiyun 				 sizeof(struct acpi_comment_addr_node),
132*4882a593Smuzhiyun 				 ACPI_MAX_COMMENT_CACHE_DEPTH,
133*4882a593Smuzhiyun 				 &acpi_gbl_comment_addr_cache);
134*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
135*4882a593Smuzhiyun 		return (status);
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/*
139*4882a593Smuzhiyun 	 * This cache will be used for nodes that represent files.
140*4882a593Smuzhiyun 	 */
141*4882a593Smuzhiyun 	status =
142*4882a593Smuzhiyun 	    acpi_os_create_cache("Acpi-File", sizeof(struct acpi_file_node),
143*4882a593Smuzhiyun 				 ACPI_MAX_COMMENT_CACHE_DEPTH,
144*4882a593Smuzhiyun 				 &acpi_gbl_file_cache);
145*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
146*4882a593Smuzhiyun 		return (status);
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun #ifdef ACPI_DBG_TRACK_ALLOCATIONS
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	/* Memory allocation lists */
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
155*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
156*4882a593Smuzhiyun 		return (status);
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	status =
160*4882a593Smuzhiyun 	    acpi_ut_create_list("Acpi-Namespace",
161*4882a593Smuzhiyun 				sizeof(struct acpi_namespace_node),
162*4882a593Smuzhiyun 				&acpi_gbl_ns_node_list);
163*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
164*4882a593Smuzhiyun 		return (status);
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun #endif
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	return (AE_OK);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /*******************************************************************************
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_delete_caches
174*4882a593Smuzhiyun  *
175*4882a593Smuzhiyun  * PARAMETERS:  None
176*4882a593Smuzhiyun  *
177*4882a593Smuzhiyun  * RETURN:      Status
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * DESCRIPTION: Purge and delete all local caches
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  ******************************************************************************/
182*4882a593Smuzhiyun 
acpi_ut_delete_caches(void)183*4882a593Smuzhiyun acpi_status acpi_ut_delete_caches(void)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun #ifdef ACPI_DBG_TRACK_ALLOCATIONS
186*4882a593Smuzhiyun 	char buffer[7];
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (acpi_gbl_display_final_mem_stats) {
189*4882a593Smuzhiyun 		strcpy(buffer, "MEMORY");
190*4882a593Smuzhiyun 		(void)acpi_db_display_statistics(buffer);
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun #endif
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_namespace_cache);
195*4882a593Smuzhiyun 	acpi_gbl_namespace_cache = NULL;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_state_cache);
198*4882a593Smuzhiyun 	acpi_gbl_state_cache = NULL;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_operand_cache);
201*4882a593Smuzhiyun 	acpi_gbl_operand_cache = NULL;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
204*4882a593Smuzhiyun 	acpi_gbl_ps_node_cache = NULL;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
207*4882a593Smuzhiyun 	acpi_gbl_ps_node_ext_cache = NULL;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun #ifdef ACPI_ASL_COMPILER
210*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_reg_comment_cache);
211*4882a593Smuzhiyun 	acpi_gbl_reg_comment_cache = NULL;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_comment_addr_cache);
214*4882a593Smuzhiyun 	acpi_gbl_comment_addr_cache = NULL;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	(void)acpi_os_delete_cache(acpi_gbl_file_cache);
217*4882a593Smuzhiyun 	acpi_gbl_file_cache = NULL;
218*4882a593Smuzhiyun #endif
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun #ifdef ACPI_DBG_TRACK_ALLOCATIONS
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* Debug only - display leftover memory allocation, if any */
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/* Free memory lists */
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	acpi_os_free(acpi_gbl_global_list);
229*4882a593Smuzhiyun 	acpi_gbl_global_list = NULL;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	acpi_os_free(acpi_gbl_ns_node_list);
232*4882a593Smuzhiyun 	acpi_gbl_ns_node_list = NULL;
233*4882a593Smuzhiyun #endif
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	return (AE_OK);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /*******************************************************************************
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_validate_buffer
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  * PARAMETERS:  buffer              - Buffer descriptor to be validated
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * RETURN:      Status
245*4882a593Smuzhiyun  *
246*4882a593Smuzhiyun  * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
247*4882a593Smuzhiyun  *
248*4882a593Smuzhiyun  ******************************************************************************/
249*4882a593Smuzhiyun 
acpi_ut_validate_buffer(struct acpi_buffer * buffer)250*4882a593Smuzhiyun acpi_status acpi_ut_validate_buffer(struct acpi_buffer *buffer)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/* Obviously, the structure pointer must be valid */
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (!buffer) {
256*4882a593Smuzhiyun 		return (AE_BAD_PARAMETER);
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	/* Special semantics for the length */
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	if ((buffer->length == ACPI_NO_BUFFER) ||
262*4882a593Smuzhiyun 	    (buffer->length == ACPI_ALLOCATE_BUFFER) ||
263*4882a593Smuzhiyun 	    (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
264*4882a593Smuzhiyun 		return (AE_OK);
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/* Length is valid, the buffer pointer must be also */
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (!buffer->pointer) {
270*4882a593Smuzhiyun 		return (AE_BAD_PARAMETER);
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return (AE_OK);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /*******************************************************************************
277*4882a593Smuzhiyun  *
278*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_initialize_buffer
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  * PARAMETERS:  buffer              - Buffer to be validated
281*4882a593Smuzhiyun  *              required_length     - Length needed
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * RETURN:      Status
284*4882a593Smuzhiyun  *
285*4882a593Smuzhiyun  * DESCRIPTION: Validate that the buffer is of the required length or
286*4882a593Smuzhiyun  *              allocate a new buffer. Returned buffer is always zeroed.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  ******************************************************************************/
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun acpi_status
acpi_ut_initialize_buffer(struct acpi_buffer * buffer,acpi_size required_length)291*4882a593Smuzhiyun acpi_ut_initialize_buffer(struct acpi_buffer *buffer, acpi_size required_length)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	acpi_size input_buffer_length;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Parameter validation */
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (!buffer || !required_length) {
298*4882a593Smuzhiyun 		return (AE_BAD_PARAMETER);
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/*
302*4882a593Smuzhiyun 	 * Buffer->Length is used as both an input and output parameter. Get the
303*4882a593Smuzhiyun 	 * input actual length and set the output required buffer length.
304*4882a593Smuzhiyun 	 */
305*4882a593Smuzhiyun 	input_buffer_length = buffer->length;
306*4882a593Smuzhiyun 	buffer->length = required_length;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/*
309*4882a593Smuzhiyun 	 * The input buffer length contains the actual buffer length, or the type
310*4882a593Smuzhiyun 	 * of buffer to be allocated by this routine.
311*4882a593Smuzhiyun 	 */
312*4882a593Smuzhiyun 	switch (input_buffer_length) {
313*4882a593Smuzhiyun 	case ACPI_NO_BUFFER:
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 		/* Return the exception (and the required buffer length) */
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 		return (AE_BUFFER_OVERFLOW);
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	case ACPI_ALLOCATE_BUFFER:
320*4882a593Smuzhiyun 		/*
321*4882a593Smuzhiyun 		 * Allocate a new buffer. We directectly call acpi_os_allocate here to
322*4882a593Smuzhiyun 		 * purposefully bypass the (optionally enabled) internal allocation
323*4882a593Smuzhiyun 		 * tracking mechanism since we only want to track internal
324*4882a593Smuzhiyun 		 * allocations. Note: The caller should use acpi_os_free to free this
325*4882a593Smuzhiyun 		 * buffer created via ACPI_ALLOCATE_BUFFER.
326*4882a593Smuzhiyun 		 */
327*4882a593Smuzhiyun 		buffer->pointer = acpi_os_allocate(required_length);
328*4882a593Smuzhiyun 		break;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	case ACPI_ALLOCATE_LOCAL_BUFFER:
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 		/* Allocate a new buffer with local interface to allow tracking */
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		buffer->pointer = ACPI_ALLOCATE(required_length);
335*4882a593Smuzhiyun 		break;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	default:
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 		/* Existing buffer: Validate the size of the buffer */
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 		if (input_buffer_length < required_length) {
342*4882a593Smuzhiyun 			return (AE_BUFFER_OVERFLOW);
343*4882a593Smuzhiyun 		}
344*4882a593Smuzhiyun 		break;
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* Validate allocation from above or input buffer pointer */
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	if (!buffer->pointer) {
350*4882a593Smuzhiyun 		return (AE_NO_MEMORY);
351*4882a593Smuzhiyun 	}
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	/* Have a valid buffer, clear it */
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	memset(buffer->pointer, 0, required_length);
356*4882a593Smuzhiyun 	return (AE_OK);
357*4882a593Smuzhiyun }
358