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