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