xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpica/exconfig.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Module Name: exconfig - Namespace reconfiguration (Load/Unload opcodes)
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 "acinterp.h"
13*4882a593Smuzhiyun #include "acnamesp.h"
14*4882a593Smuzhiyun #include "actables.h"
15*4882a593Smuzhiyun #include "acdispat.h"
16*4882a593Smuzhiyun #include "acevents.h"
17*4882a593Smuzhiyun #include "amlcode.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define _COMPONENT          ACPI_EXECUTER
20*4882a593Smuzhiyun ACPI_MODULE_NAME("exconfig")
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* Local prototypes */
23*4882a593Smuzhiyun static acpi_status
24*4882a593Smuzhiyun acpi_ex_add_table(u32 table_index, union acpi_operand_object **ddb_handle);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static acpi_status
27*4882a593Smuzhiyun acpi_ex_region_read(union acpi_operand_object *obj_desc,
28*4882a593Smuzhiyun 		    u32 length, u8 *buffer);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*******************************************************************************
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_add_table
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * PARAMETERS:  table               - Pointer to raw table
35*4882a593Smuzhiyun  *              parent_node         - Where to load the table (scope)
36*4882a593Smuzhiyun  *              ddb_handle          - Where to return the table handle.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * RETURN:      Status
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * DESCRIPTION: Common function to Install and Load an ACPI table with a
41*4882a593Smuzhiyun  *              returned table handle.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  ******************************************************************************/
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static acpi_status
acpi_ex_add_table(u32 table_index,union acpi_operand_object ** ddb_handle)46*4882a593Smuzhiyun acpi_ex_add_table(u32 table_index, union acpi_operand_object **ddb_handle)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	union acpi_operand_object *obj_desc;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ex_add_table);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	/* Create an object to be the table handle */
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
55*4882a593Smuzhiyun 	if (!obj_desc) {
56*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_NO_MEMORY);
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	/* Init the table handle */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	obj_desc->common.flags |= AOPOBJ_DATA_VALID;
62*4882a593Smuzhiyun 	obj_desc->reference.class = ACPI_REFCLASS_TABLE;
63*4882a593Smuzhiyun 	obj_desc->reference.value = table_index;
64*4882a593Smuzhiyun 	*ddb_handle = obj_desc;
65*4882a593Smuzhiyun 	return_ACPI_STATUS(AE_OK);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /*******************************************************************************
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_load_table_op
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * PARAMETERS:  walk_state          - Current state with operands
73*4882a593Smuzhiyun  *              return_desc         - Where to store the return object
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * RETURN:      Status
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * DESCRIPTION: Load an ACPI table from the RSDT/XSDT
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  ******************************************************************************/
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun acpi_status
acpi_ex_load_table_op(struct acpi_walk_state * walk_state,union acpi_operand_object ** return_desc)82*4882a593Smuzhiyun acpi_ex_load_table_op(struct acpi_walk_state *walk_state,
83*4882a593Smuzhiyun 		      union acpi_operand_object **return_desc)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	acpi_status status;
86*4882a593Smuzhiyun 	union acpi_operand_object **operand = &walk_state->operands[0];
87*4882a593Smuzhiyun 	struct acpi_namespace_node *parent_node;
88*4882a593Smuzhiyun 	struct acpi_namespace_node *start_node;
89*4882a593Smuzhiyun 	struct acpi_namespace_node *parameter_node = NULL;
90*4882a593Smuzhiyun 	union acpi_operand_object *ddb_handle;
91*4882a593Smuzhiyun 	u32 table_index;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ex_load_table_op);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* Find the ACPI table in the RSDT/XSDT */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	acpi_ex_exit_interpreter();
98*4882a593Smuzhiyun 	status = acpi_tb_find_table(operand[0]->string.pointer,
99*4882a593Smuzhiyun 				    operand[1]->string.pointer,
100*4882a593Smuzhiyun 				    operand[2]->string.pointer, &table_index);
101*4882a593Smuzhiyun 	acpi_ex_enter_interpreter();
102*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
103*4882a593Smuzhiyun 		if (status != AE_NOT_FOUND) {
104*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
105*4882a593Smuzhiyun 		}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 		/* Table not found, return an Integer=0 and AE_OK */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		ddb_handle = acpi_ut_create_integer_object((u64) 0);
110*4882a593Smuzhiyun 		if (!ddb_handle) {
111*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_NO_MEMORY);
112*4882a593Smuzhiyun 		}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 		*return_desc = ddb_handle;
115*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_OK);
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* Default nodes */
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	start_node = walk_state->scope_info->scope.node;
121*4882a593Smuzhiyun 	parent_node = acpi_gbl_root_node;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* root_path (optional parameter) */
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (operand[3]->string.length > 0) {
126*4882a593Smuzhiyun 		/*
127*4882a593Smuzhiyun 		 * Find the node referenced by the root_path_string. This is the
128*4882a593Smuzhiyun 		 * location within the namespace where the table will be loaded.
129*4882a593Smuzhiyun 		 */
130*4882a593Smuzhiyun 		status = acpi_ns_get_node_unlocked(start_node,
131*4882a593Smuzhiyun 						   operand[3]->string.pointer,
132*4882a593Smuzhiyun 						   ACPI_NS_SEARCH_PARENT,
133*4882a593Smuzhiyun 						   &parent_node);
134*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
135*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
136*4882a593Smuzhiyun 		}
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* parameter_path (optional parameter) */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	if (operand[4]->string.length > 0) {
142*4882a593Smuzhiyun 		if ((operand[4]->string.pointer[0] != AML_ROOT_PREFIX) &&
143*4882a593Smuzhiyun 		    (operand[4]->string.pointer[0] != AML_PARENT_PREFIX)) {
144*4882a593Smuzhiyun 			/*
145*4882a593Smuzhiyun 			 * Path is not absolute, so it will be relative to the node
146*4882a593Smuzhiyun 			 * referenced by the root_path_string (or the NS root if omitted)
147*4882a593Smuzhiyun 			 */
148*4882a593Smuzhiyun 			start_node = parent_node;
149*4882a593Smuzhiyun 		}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 		/* Find the node referenced by the parameter_path_string */
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		status = acpi_ns_get_node_unlocked(start_node,
154*4882a593Smuzhiyun 						   operand[4]->string.pointer,
155*4882a593Smuzhiyun 						   ACPI_NS_SEARCH_PARENT,
156*4882a593Smuzhiyun 						   &parameter_node);
157*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
158*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
159*4882a593Smuzhiyun 		}
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	/* Load the table into the namespace */
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	ACPI_INFO(("Dynamic OEM Table Load:"));
165*4882a593Smuzhiyun 	acpi_ex_exit_interpreter();
166*4882a593Smuzhiyun 	status = acpi_tb_load_table(table_index, parent_node);
167*4882a593Smuzhiyun 	acpi_ex_enter_interpreter();
168*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
169*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	status = acpi_ex_add_table(table_index, &ddb_handle);
173*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
174*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* Complete the initialization/resolution of new objects */
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	acpi_ex_exit_interpreter();
180*4882a593Smuzhiyun 	acpi_ns_initialize_objects();
181*4882a593Smuzhiyun 	acpi_ex_enter_interpreter();
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	/* Parameter Data (optional) */
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (parameter_node) {
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 		/* Store the parameter data into the optional parameter object */
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		status = acpi_ex_store(operand[5],
190*4882a593Smuzhiyun 				       ACPI_CAST_PTR(union acpi_operand_object,
191*4882a593Smuzhiyun 						     parameter_node),
192*4882a593Smuzhiyun 				       walk_state);
193*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
194*4882a593Smuzhiyun 			(void)acpi_ex_unload_table(ddb_handle);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 			acpi_ut_remove_reference(ddb_handle);
197*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
198*4882a593Smuzhiyun 		}
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	*return_desc = ddb_handle;
202*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /*******************************************************************************
206*4882a593Smuzhiyun  *
207*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_region_read
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  * PARAMETERS:  obj_desc        - Region descriptor
210*4882a593Smuzhiyun  *              length          - Number of bytes to read
211*4882a593Smuzhiyun  *              buffer          - Pointer to where to put the data
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * RETURN:      Status
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * DESCRIPTION: Read data from an operation region. The read starts from the
216*4882a593Smuzhiyun  *              beginning of the region.
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  ******************************************************************************/
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun static acpi_status
acpi_ex_region_read(union acpi_operand_object * obj_desc,u32 length,u8 * buffer)221*4882a593Smuzhiyun acpi_ex_region_read(union acpi_operand_object *obj_desc, u32 length, u8 *buffer)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	acpi_status status;
224*4882a593Smuzhiyun 	u64 value;
225*4882a593Smuzhiyun 	u32 region_offset = 0;
226*4882a593Smuzhiyun 	u32 i;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	/* Bytewise reads */
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	for (i = 0; i < length; i++) {
231*4882a593Smuzhiyun 		status =
232*4882a593Smuzhiyun 		    acpi_ev_address_space_dispatch(obj_desc, NULL, ACPI_READ,
233*4882a593Smuzhiyun 						   region_offset, 8, &value);
234*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
235*4882a593Smuzhiyun 			return (status);
236*4882a593Smuzhiyun 		}
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		*buffer = (u8)value;
239*4882a593Smuzhiyun 		buffer++;
240*4882a593Smuzhiyun 		region_offset++;
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	return (AE_OK);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /*******************************************************************************
247*4882a593Smuzhiyun  *
248*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_load_op
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * PARAMETERS:  obj_desc        - Region or Buffer/Field where the table will be
251*4882a593Smuzhiyun  *                                obtained
252*4882a593Smuzhiyun  *              target          - Where a handle to the table will be stored
253*4882a593Smuzhiyun  *              walk_state      - Current state
254*4882a593Smuzhiyun  *
255*4882a593Smuzhiyun  * RETURN:      Status
256*4882a593Smuzhiyun  *
257*4882a593Smuzhiyun  * DESCRIPTION: Load an ACPI table from a field or operation region
258*4882a593Smuzhiyun  *
259*4882a593Smuzhiyun  * NOTE: Region Fields (Field, bank_field, index_fields) are resolved to buffer
260*4882a593Smuzhiyun  *       objects before this code is reached.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  *       If source is an operation region, it must refer to system_memory, as
263*4882a593Smuzhiyun  *       per the ACPI specification.
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  ******************************************************************************/
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun acpi_status
acpi_ex_load_op(union acpi_operand_object * obj_desc,union acpi_operand_object * target,struct acpi_walk_state * walk_state)268*4882a593Smuzhiyun acpi_ex_load_op(union acpi_operand_object *obj_desc,
269*4882a593Smuzhiyun 		union acpi_operand_object *target,
270*4882a593Smuzhiyun 		struct acpi_walk_state *walk_state)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	union acpi_operand_object *ddb_handle;
273*4882a593Smuzhiyun 	struct acpi_table_header *table_header;
274*4882a593Smuzhiyun 	struct acpi_table_header *table;
275*4882a593Smuzhiyun 	u32 table_index;
276*4882a593Smuzhiyun 	acpi_status status;
277*4882a593Smuzhiyun 	u32 length;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ex_load_op);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* Source Object can be either an op_region or a Buffer/Field */
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	switch (obj_desc->common.type) {
284*4882a593Smuzhiyun 	case ACPI_TYPE_REGION:
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
287*4882a593Smuzhiyun 				  "Load table from Region %p\n", obj_desc));
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		/* Region must be system_memory (from ACPI spec) */
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 		if (obj_desc->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
292*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
293*4882a593Smuzhiyun 		}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 		/*
296*4882a593Smuzhiyun 		 * If the Region Address and Length have not been previously
297*4882a593Smuzhiyun 		 * evaluated, evaluate them now and save the results.
298*4882a593Smuzhiyun 		 */
299*4882a593Smuzhiyun 		if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
300*4882a593Smuzhiyun 			status = acpi_ds_get_region_arguments(obj_desc);
301*4882a593Smuzhiyun 			if (ACPI_FAILURE(status)) {
302*4882a593Smuzhiyun 				return_ACPI_STATUS(status);
303*4882a593Smuzhiyun 			}
304*4882a593Smuzhiyun 		}
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		/* Get the table header first so we can get the table length */
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		table_header = ACPI_ALLOCATE(sizeof(struct acpi_table_header));
309*4882a593Smuzhiyun 		if (!table_header) {
310*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_NO_MEMORY);
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 		status =
314*4882a593Smuzhiyun 		    acpi_ex_region_read(obj_desc,
315*4882a593Smuzhiyun 					sizeof(struct acpi_table_header),
316*4882a593Smuzhiyun 					ACPI_CAST_PTR(u8, table_header));
317*4882a593Smuzhiyun 		length = table_header->length;
318*4882a593Smuzhiyun 		ACPI_FREE(table_header);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
321*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
322*4882a593Smuzhiyun 		}
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 		/* Must have at least an ACPI table header */
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		if (length < sizeof(struct acpi_table_header)) {
327*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
328*4882a593Smuzhiyun 		}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 		/*
331*4882a593Smuzhiyun 		 * The original implementation simply mapped the table, with no copy.
332*4882a593Smuzhiyun 		 * However, the memory region is not guaranteed to remain stable and
333*4882a593Smuzhiyun 		 * we must copy the table to a local buffer. For example, the memory
334*4882a593Smuzhiyun 		 * region is corrupted after suspend on some machines. Dynamically
335*4882a593Smuzhiyun 		 * loaded tables are usually small, so this overhead is minimal.
336*4882a593Smuzhiyun 		 *
337*4882a593Smuzhiyun 		 * The latest implementation (5/2009) does not use a mapping at all.
338*4882a593Smuzhiyun 		 * We use the low-level operation region interface to read the table
339*4882a593Smuzhiyun 		 * instead of the obvious optimization of using a direct mapping.
340*4882a593Smuzhiyun 		 * This maintains a consistent use of operation regions across the
341*4882a593Smuzhiyun 		 * entire subsystem. This is important if additional processing must
342*4882a593Smuzhiyun 		 * be performed in the (possibly user-installed) operation region
343*4882a593Smuzhiyun 		 * handler. For example, acpi_exec and ASLTS depend on this.
344*4882a593Smuzhiyun 		 */
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		/* Allocate a buffer for the table */
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 		table = ACPI_ALLOCATE(length);
349*4882a593Smuzhiyun 		if (!table) {
350*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_NO_MEMORY);
351*4882a593Smuzhiyun 		}
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 		/* Read the entire table */
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 		status = acpi_ex_region_read(obj_desc, length,
356*4882a593Smuzhiyun 					     ACPI_CAST_PTR(u8, table));
357*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
358*4882a593Smuzhiyun 			ACPI_FREE(table);
359*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
360*4882a593Smuzhiyun 		}
361*4882a593Smuzhiyun 		break;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	case ACPI_TYPE_BUFFER:	/* Buffer or resolved region_field */
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
366*4882a593Smuzhiyun 				  "Load table from Buffer or Field %p\n",
367*4882a593Smuzhiyun 				  obj_desc));
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 		/* Must have at least an ACPI table header */
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 		if (obj_desc->buffer.length < sizeof(struct acpi_table_header)) {
372*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
373*4882a593Smuzhiyun 		}
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 		/* Get the actual table length from the table header */
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		table_header =
378*4882a593Smuzhiyun 		    ACPI_CAST_PTR(struct acpi_table_header,
379*4882a593Smuzhiyun 				  obj_desc->buffer.pointer);
380*4882a593Smuzhiyun 		length = table_header->length;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		/* Table cannot extend beyond the buffer */
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 		if (length > obj_desc->buffer.length) {
385*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
386*4882a593Smuzhiyun 		}
387*4882a593Smuzhiyun 		if (length < sizeof(struct acpi_table_header)) {
388*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
389*4882a593Smuzhiyun 		}
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		/*
392*4882a593Smuzhiyun 		 * Copy the table from the buffer because the buffer could be
393*4882a593Smuzhiyun 		 * modified or even deleted in the future
394*4882a593Smuzhiyun 		 */
395*4882a593Smuzhiyun 		table = ACPI_ALLOCATE(length);
396*4882a593Smuzhiyun 		if (!table) {
397*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_NO_MEMORY);
398*4882a593Smuzhiyun 		}
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 		memcpy(table, table_header, length);
401*4882a593Smuzhiyun 		break;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	default:
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/* Install the new table into the local data structures */
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	ACPI_INFO(("Dynamic OEM Table Load:"));
411*4882a593Smuzhiyun 	acpi_ex_exit_interpreter();
412*4882a593Smuzhiyun 	status = acpi_tb_install_and_load_table(ACPI_PTR_TO_PHYSADDR(table),
413*4882a593Smuzhiyun 						ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
414*4882a593Smuzhiyun 						TRUE, &table_index);
415*4882a593Smuzhiyun 	acpi_ex_enter_interpreter();
416*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 		/* Delete allocated table buffer */
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 		ACPI_FREE(table);
421*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
422*4882a593Smuzhiyun 	}
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	/*
425*4882a593Smuzhiyun 	 * Add the table to the namespace.
426*4882a593Smuzhiyun 	 *
427*4882a593Smuzhiyun 	 * Note: Load the table objects relative to the root of the namespace.
428*4882a593Smuzhiyun 	 * This appears to go against the ACPI specification, but we do it for
429*4882a593Smuzhiyun 	 * compatibility with other ACPI implementations.
430*4882a593Smuzhiyun 	 */
431*4882a593Smuzhiyun 	status = acpi_ex_add_table(table_index, &ddb_handle);
432*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 		/* On error, table_ptr was deallocated above */
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
437*4882a593Smuzhiyun 	}
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	/* Complete the initialization/resolution of new objects */
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	acpi_ex_exit_interpreter();
442*4882a593Smuzhiyun 	acpi_ns_initialize_objects();
443*4882a593Smuzhiyun 	acpi_ex_enter_interpreter();
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	/* Store the ddb_handle into the Target operand */
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	status = acpi_ex_store(ddb_handle, target, walk_state);
448*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
449*4882a593Smuzhiyun 		(void)acpi_ex_unload_table(ddb_handle);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 		/* table_ptr was deallocated above */
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 		acpi_ut_remove_reference(ddb_handle);
454*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/* Remove the reference by added by acpi_ex_store above */
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	acpi_ut_remove_reference(ddb_handle);
460*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun /*******************************************************************************
464*4882a593Smuzhiyun  *
465*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_unload_table
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  * PARAMETERS:  ddb_handle          - Handle to a previously loaded table
468*4882a593Smuzhiyun  *
469*4882a593Smuzhiyun  * RETURN:      Status
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  * DESCRIPTION: Unload an ACPI table
472*4882a593Smuzhiyun  *
473*4882a593Smuzhiyun  ******************************************************************************/
474*4882a593Smuzhiyun 
acpi_ex_unload_table(union acpi_operand_object * ddb_handle)475*4882a593Smuzhiyun acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun 	acpi_status status = AE_OK;
478*4882a593Smuzhiyun 	union acpi_operand_object *table_desc = ddb_handle;
479*4882a593Smuzhiyun 	u32 table_index;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ex_unload_table);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/*
484*4882a593Smuzhiyun 	 * Temporarily emit a warning so that the ASL for the machine can be
485*4882a593Smuzhiyun 	 * hopefully obtained. This is to say that the Unload() operator is
486*4882a593Smuzhiyun 	 * extremely rare if not completely unused.
487*4882a593Smuzhiyun 	 */
488*4882a593Smuzhiyun 	ACPI_WARNING((AE_INFO, "Received request to unload an ACPI table"));
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	/*
491*4882a593Smuzhiyun 	 * May 2018: Unload is no longer supported for the following reasons:
492*4882a593Smuzhiyun 	 * 1) A correct implementation on some hosts may not be possible.
493*4882a593Smuzhiyun 	 * 2) Other ACPI implementations do not correctly/fully support it.
494*4882a593Smuzhiyun 	 * 3) It requires host device driver support which does not exist.
495*4882a593Smuzhiyun 	 *    (To properly support namespace unload out from underneath.)
496*4882a593Smuzhiyun 	 * 4) This AML operator has never been seen in the field.
497*4882a593Smuzhiyun 	 */
498*4882a593Smuzhiyun 	ACPI_EXCEPTION((AE_INFO, AE_NOT_IMPLEMENTED,
499*4882a593Smuzhiyun 			"AML Unload operator is not supported"));
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	/*
502*4882a593Smuzhiyun 	 * Validate the handle
503*4882a593Smuzhiyun 	 * Although the handle is partially validated in acpi_ex_reconfiguration()
504*4882a593Smuzhiyun 	 * when it calls acpi_ex_resolve_operands(), the handle is more completely
505*4882a593Smuzhiyun 	 * validated here.
506*4882a593Smuzhiyun 	 *
507*4882a593Smuzhiyun 	 * Handle must be a valid operand object of type reference. Also, the
508*4882a593Smuzhiyun 	 * ddb_handle must still be marked valid (table has not been previously
509*4882a593Smuzhiyun 	 * unloaded)
510*4882a593Smuzhiyun 	 */
511*4882a593Smuzhiyun 	if ((!ddb_handle) ||
512*4882a593Smuzhiyun 	    (ACPI_GET_DESCRIPTOR_TYPE(ddb_handle) != ACPI_DESC_TYPE_OPERAND) ||
513*4882a593Smuzhiyun 	    (ddb_handle->common.type != ACPI_TYPE_LOCAL_REFERENCE) ||
514*4882a593Smuzhiyun 	    (!(ddb_handle->common.flags & AOPOBJ_DATA_VALID))) {
515*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* Get the table index from the ddb_handle */
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	table_index = table_desc->reference.value;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	/*
523*4882a593Smuzhiyun 	 * Release the interpreter lock so that the table lock won't have
524*4882a593Smuzhiyun 	 * strict order requirement against it.
525*4882a593Smuzhiyun 	 */
526*4882a593Smuzhiyun 	acpi_ex_exit_interpreter();
527*4882a593Smuzhiyun 	status = acpi_tb_unload_table(table_index);
528*4882a593Smuzhiyun 	acpi_ex_enter_interpreter();
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/*
531*4882a593Smuzhiyun 	 * Invalidate the handle. We do this because the handle may be stored
532*4882a593Smuzhiyun 	 * in a named object and may not be actually deleted until much later.
533*4882a593Smuzhiyun 	 */
534*4882a593Smuzhiyun 	if (ACPI_SUCCESS(status)) {
535*4882a593Smuzhiyun 		ddb_handle->common.flags &= ~AOPOBJ_DATA_VALID;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
538*4882a593Smuzhiyun }
539