xref: /openbmc/linux/drivers/acpi/acpica/tbxfload.c (revision 6d99a79c)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: tbxfload - Table load/unload external interfaces
5  *
6  * Copyright (C) 2000 - 2018, Intel Corp.
7  *
8  *****************************************************************************/
9 
10 #define EXPORT_ACPI_INTERFACES
11 
12 #include <acpi/acpi.h>
13 #include "accommon.h"
14 #include "acnamesp.h"
15 #include "actables.h"
16 #include "acevents.h"
17 
18 #define _COMPONENT          ACPI_TABLES
19 ACPI_MODULE_NAME("tbxfload")
20 
21 /*******************************************************************************
22  *
23  * FUNCTION:    acpi_load_tables
24  *
25  * PARAMETERS:  None
26  *
27  * RETURN:      Status
28  *
29  * DESCRIPTION: Load the ACPI tables from the RSDT/XSDT
30  *
31  ******************************************************************************/
32 acpi_status ACPI_INIT_FUNCTION acpi_load_tables(void)
33 {
34 	acpi_status status;
35 
36 	ACPI_FUNCTION_TRACE(acpi_load_tables);
37 
38 	/*
39 	 * Install the default operation region handlers. These are the
40 	 * handlers that are defined by the ACPI specification to be
41 	 * "always accessible" -- namely, system_memory, system_IO, and
42 	 * PCI_Config. This also means that no _REG methods need to be
43 	 * run for these address spaces. We need to have these handlers
44 	 * installed before any AML code can be executed, especially any
45 	 * module-level code (11/2015).
46 	 * Note that we allow OSPMs to install their own region handlers
47 	 * between acpi_initialize_subsystem() and acpi_load_tables() to use
48 	 * their customized default region handlers.
49 	 */
50 	status = acpi_ev_install_region_handlers();
51 	if (ACPI_FAILURE(status)) {
52 		ACPI_EXCEPTION((AE_INFO, status,
53 				"During Region initialization"));
54 		return_ACPI_STATUS(status);
55 	}
56 
57 	/* Load the namespace from the tables */
58 
59 	status = acpi_tb_load_namespace();
60 
61 	/* Don't let single failures abort the load */
62 
63 	if (status == AE_CTRL_TERMINATE) {
64 		status = AE_OK;
65 	}
66 
67 	if (ACPI_FAILURE(status)) {
68 		ACPI_EXCEPTION((AE_INFO, status,
69 				"While loading namespace from ACPI tables"));
70 	}
71 
72 	if (acpi_gbl_execute_tables_as_methods) {
73 		/*
74 		 * If the module-level code support is enabled, initialize the objects
75 		 * in the namespace that remain uninitialized. This runs the executable
76 		 * AML that may be part of the declaration of these name objects:
77 		 *     operation_regions, buffer_fields, Buffers, and Packages.
78 		 *
79 		 * Note: The module-level code is optional at this time, but will
80 		 * become the default in the future.
81 		 */
82 		status = acpi_ns_initialize_objects();
83 		if (ACPI_FAILURE(status)) {
84 			return_ACPI_STATUS(status);
85 		}
86 	}
87 
88 	acpi_gbl_namespace_initialized = TRUE;
89 	return_ACPI_STATUS(status);
90 }
91 
92 ACPI_EXPORT_SYMBOL_INIT(acpi_load_tables)
93 
94 /*******************************************************************************
95  *
96  * FUNCTION:    acpi_tb_load_namespace
97  *
98  * PARAMETERS:  None
99  *
100  * RETURN:      Status
101  *
102  * DESCRIPTION: Load the namespace from the DSDT and all SSDTs/PSDTs found in
103  *              the RSDT/XSDT.
104  *
105  ******************************************************************************/
106 acpi_status acpi_tb_load_namespace(void)
107 {
108 	acpi_status status;
109 	u32 i;
110 	struct acpi_table_header *new_dsdt;
111 	struct acpi_table_desc *table;
112 	u32 tables_loaded = 0;
113 	u32 tables_failed = 0;
114 
115 	ACPI_FUNCTION_TRACE(tb_load_namespace);
116 
117 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
118 
119 	/*
120 	 * Load the namespace. The DSDT is required, but any SSDT and
121 	 * PSDT tables are optional. Verify the DSDT.
122 	 */
123 	table = &acpi_gbl_root_table_list.tables[acpi_gbl_dsdt_index];
124 
125 	if (!acpi_gbl_root_table_list.current_table_count ||
126 	    !ACPI_COMPARE_NAME(table->signature.ascii, ACPI_SIG_DSDT) ||
127 	    ACPI_FAILURE(acpi_tb_validate_table(table))) {
128 		status = AE_NO_ACPI_TABLES;
129 		goto unlock_and_exit;
130 	}
131 
132 	/*
133 	 * Save the DSDT pointer for simple access. This is the mapped memory
134 	 * address. We must take care here because the address of the .Tables
135 	 * array can change dynamically as tables are loaded at run-time. Note:
136 	 * .Pointer field is not validated until after call to acpi_tb_validate_table.
137 	 */
138 	acpi_gbl_DSDT = table->pointer;
139 
140 	/*
141 	 * Optionally copy the entire DSDT to local memory (instead of simply
142 	 * mapping it.) There are some BIOSs that corrupt or replace the original
143 	 * DSDT, creating the need for this option. Default is FALSE, do not copy
144 	 * the DSDT.
145 	 */
146 	if (acpi_gbl_copy_dsdt_locally) {
147 		new_dsdt = acpi_tb_copy_dsdt(acpi_gbl_dsdt_index);
148 		if (new_dsdt) {
149 			acpi_gbl_DSDT = new_dsdt;
150 		}
151 	}
152 
153 	/*
154 	 * Save the original DSDT header for detection of table corruption
155 	 * and/or replacement of the DSDT from outside the OS.
156 	 */
157 	memcpy(&acpi_gbl_original_dsdt_header, acpi_gbl_DSDT,
158 	       sizeof(struct acpi_table_header));
159 
160 	/* Load and parse tables */
161 
162 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
163 	status = acpi_ns_load_table(acpi_gbl_dsdt_index, acpi_gbl_root_node);
164 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
165 	if (ACPI_FAILURE(status)) {
166 		ACPI_EXCEPTION((AE_INFO, status, "[DSDT] table load failed"));
167 		tables_failed++;
168 	} else {
169 		tables_loaded++;
170 	}
171 
172 	/* Load any SSDT or PSDT tables. Note: Loop leaves tables locked */
173 
174 	for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
175 		table = &acpi_gbl_root_table_list.tables[i];
176 
177 		if (!table->address ||
178 		    (!ACPI_COMPARE_NAME(table->signature.ascii, ACPI_SIG_SSDT)
179 		     && !ACPI_COMPARE_NAME(table->signature.ascii,
180 					   ACPI_SIG_PSDT)
181 		     && !ACPI_COMPARE_NAME(table->signature.ascii,
182 					   ACPI_SIG_OSDT))
183 		    || ACPI_FAILURE(acpi_tb_validate_table(table))) {
184 			continue;
185 		}
186 
187 		/* Ignore errors while loading tables, get as many as possible */
188 
189 		(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
190 		status = acpi_ns_load_table(i, acpi_gbl_root_node);
191 		(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
192 		if (ACPI_FAILURE(status)) {
193 			ACPI_EXCEPTION((AE_INFO, status,
194 					"(%4.4s:%8.8s) while loading table",
195 					table->signature.ascii,
196 					table->pointer->oem_table_id));
197 
198 			tables_failed++;
199 
200 			ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
201 					      "Table [%4.4s:%8.8s] (id FF) - Table namespace load failed\n\n",
202 					      table->signature.ascii,
203 					      table->pointer->oem_table_id));
204 		} else {
205 			tables_loaded++;
206 		}
207 	}
208 
209 	if (!tables_failed) {
210 		ACPI_INFO(("%u ACPI AML tables successfully acquired and loaded", tables_loaded));
211 	} else {
212 		ACPI_ERROR((AE_INFO,
213 			    "%u table load failures, %u successful",
214 			    tables_failed, tables_loaded));
215 
216 		/* Indicate at least one failure */
217 
218 		status = AE_CTRL_TERMINATE;
219 	}
220 
221 #ifdef ACPI_APPLICATION
222 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT, "\n"));
223 #endif
224 
225 unlock_and_exit:
226 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
227 	return_ACPI_STATUS(status);
228 }
229 
230 /*******************************************************************************
231  *
232  * FUNCTION:    acpi_install_table
233  *
234  * PARAMETERS:  address             - Address of the ACPI table to be installed.
235  *              physical            - Whether the address is a physical table
236  *                                    address or not
237  *
238  * RETURN:      Status
239  *
240  * DESCRIPTION: Dynamically install an ACPI table.
241  *              Note: This function should only be invoked after
242  *                    acpi_initialize_tables() and before acpi_load_tables().
243  *
244  ******************************************************************************/
245 
246 acpi_status ACPI_INIT_FUNCTION
247 acpi_install_table(acpi_physical_address address, u8 physical)
248 {
249 	acpi_status status;
250 	u8 flags;
251 	u32 table_index;
252 
253 	ACPI_FUNCTION_TRACE(acpi_install_table);
254 
255 	if (physical) {
256 		flags = ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL;
257 	} else {
258 		flags = ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL;
259 	}
260 
261 	status = acpi_tb_install_standard_table(address, flags,
262 						FALSE, FALSE, &table_index);
263 
264 	return_ACPI_STATUS(status);
265 }
266 
267 ACPI_EXPORT_SYMBOL_INIT(acpi_install_table)
268 
269 /*******************************************************************************
270  *
271  * FUNCTION:    acpi_load_table
272  *
273  * PARAMETERS:  table               - Pointer to a buffer containing the ACPI
274  *                                    table to be loaded.
275  *
276  * RETURN:      Status
277  *
278  * DESCRIPTION: Dynamically load an ACPI table from the caller's buffer. Must
279  *              be a valid ACPI table with a valid ACPI table header.
280  *              Note1: Mainly intended to support hotplug addition of SSDTs.
281  *              Note2: Does not copy the incoming table. User is responsible
282  *              to ensure that the table is not deleted or unmapped.
283  *
284  ******************************************************************************/
285 acpi_status acpi_load_table(struct acpi_table_header *table)
286 {
287 	acpi_status status;
288 	u32 table_index;
289 
290 	ACPI_FUNCTION_TRACE(acpi_load_table);
291 
292 	/* Parameter validation */
293 
294 	if (!table) {
295 		return_ACPI_STATUS(AE_BAD_PARAMETER);
296 	}
297 
298 	/* Install the table and load it into the namespace */
299 
300 	ACPI_INFO(("Host-directed Dynamic ACPI Table Load:"));
301 	status = acpi_tb_install_and_load_table(ACPI_PTR_TO_PHYSADDR(table),
302 						ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL,
303 						FALSE, &table_index);
304 	return_ACPI_STATUS(status);
305 }
306 
307 ACPI_EXPORT_SYMBOL(acpi_load_table)
308 
309 /*******************************************************************************
310  *
311  * FUNCTION:    acpi_unload_parent_table
312  *
313  * PARAMETERS:  object              - Handle to any namespace object owned by
314  *                                    the table to be unloaded
315  *
316  * RETURN:      Status
317  *
318  * DESCRIPTION: Via any namespace object within an SSDT or OEMx table, unloads
319  *              the table and deletes all namespace objects associated with
320  *              that table. Unloading of the DSDT is not allowed.
321  *              Note: Mainly intended to support hotplug removal of SSDTs.
322  *
323  ******************************************************************************/
324 acpi_status acpi_unload_parent_table(acpi_handle object)
325 {
326 	struct acpi_namespace_node *node =
327 	    ACPI_CAST_PTR(struct acpi_namespace_node, object);
328 	acpi_status status = AE_NOT_EXIST;
329 	acpi_owner_id owner_id;
330 	u32 i;
331 
332 	ACPI_FUNCTION_TRACE(acpi_unload_parent_table);
333 
334 	/* Parameter validation */
335 
336 	if (!object) {
337 		return_ACPI_STATUS(AE_BAD_PARAMETER);
338 	}
339 
340 	/*
341 	 * The node owner_id is currently the same as the parent table ID.
342 	 * However, this could change in the future.
343 	 */
344 	owner_id = node->owner_id;
345 	if (!owner_id) {
346 
347 		/* owner_id==0 means DSDT is the owner. DSDT cannot be unloaded */
348 
349 		return_ACPI_STATUS(AE_TYPE);
350 	}
351 
352 	/* Must acquire the table lock during this operation */
353 
354 	status = acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
355 	if (ACPI_FAILURE(status)) {
356 		return_ACPI_STATUS(status);
357 	}
358 
359 	/* Find the table in the global table list */
360 
361 	for (i = 0; i < acpi_gbl_root_table_list.current_table_count; i++) {
362 		if (owner_id != acpi_gbl_root_table_list.tables[i].owner_id) {
363 			continue;
364 		}
365 
366 		/*
367 		 * Allow unload of SSDT and OEMx tables only. Do not allow unload
368 		 * of the DSDT. No other types of tables should get here, since
369 		 * only these types can contain AML and thus are the only types
370 		 * that can create namespace objects.
371 		 */
372 		if (ACPI_COMPARE_NAME
373 		    (acpi_gbl_root_table_list.tables[i].signature.ascii,
374 		     ACPI_SIG_DSDT)) {
375 			status = AE_TYPE;
376 			break;
377 		}
378 
379 		(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
380 		status = acpi_tb_unload_table(i);
381 		(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
382 		break;
383 	}
384 
385 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
386 	return_ACPI_STATUS(status);
387 }
388 
389 ACPI_EXPORT_SYMBOL(acpi_unload_parent_table)
390