ACPICA: Correctly cleanup after a ACPI table load failure
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / acpica / tbxfload.c
1 /******************************************************************************
2  *
3  * Module Name: tbxfload - Table load/unload external interfaces
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #define EXPORT_ACPI_INTERFACES
45
46 #include <acpi/acpi.h>
47 #include "accommon.h"
48 #include "acnamesp.h"
49 #include "actables.h"
50
51 #define _COMPONENT          ACPI_TABLES
52 ACPI_MODULE_NAME("tbxfload")
53
54 /* Local prototypes */
55 static acpi_status acpi_tb_load_namespace(void);
56
57 /*******************************************************************************
58  *
59  * FUNCTION:    acpi_load_tables
60  *
61  * PARAMETERS:  None
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Load the ACPI tables from the RSDT/XSDT
66  *
67  ******************************************************************************/
68
69 acpi_status __init acpi_load_tables(void)
70 {
71         acpi_status status;
72
73         ACPI_FUNCTION_TRACE(acpi_load_tables);
74
75         /* Load the namespace from the tables */
76
77         status = acpi_tb_load_namespace();
78         if (ACPI_FAILURE(status)) {
79                 ACPI_EXCEPTION((AE_INFO, status,
80                                 "While loading namespace from ACPI tables"));
81         }
82
83         return_ACPI_STATUS(status);
84 }
85
86 ACPI_EXPORT_SYMBOL_INIT(acpi_load_tables)
87
88 /*******************************************************************************
89  *
90  * FUNCTION:    acpi_tb_load_namespace
91  *
92  * PARAMETERS:  None
93  *
94  * RETURN:      Status
95  *
96  * DESCRIPTION: Load the namespace from the DSDT and all SSDTs/PSDTs found in
97  *              the RSDT/XSDT.
98  *
99  ******************************************************************************/
100 static acpi_status acpi_tb_load_namespace(void)
101 {
102         acpi_status status;
103         u32 i;
104         struct acpi_table_header *new_dsdt;
105         u32 tables_loaded = 0;
106         u32 tables_failed = 0;
107
108         ACPI_FUNCTION_TRACE(tb_load_namespace);
109
110         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
111
112         /*
113          * Load the namespace. The DSDT is required, but any SSDT and
114          * PSDT tables are optional. Verify the DSDT.
115          */
116         if (!acpi_gbl_root_table_list.current_table_count ||
117             !ACPI_COMPARE_NAME(&
118                                (acpi_gbl_root_table_list.
119                                 tables[ACPI_TABLE_INDEX_DSDT].signature),
120                                ACPI_SIG_DSDT)
121             ||
122             ACPI_FAILURE(acpi_tb_validate_table
123                          (&acpi_gbl_root_table_list.
124                           tables[ACPI_TABLE_INDEX_DSDT]))) {
125                 status = AE_NO_ACPI_TABLES;
126                 goto unlock_and_exit;
127         }
128
129         /*
130          * Save the DSDT pointer for simple access. This is the mapped memory
131          * address. We must take care here because the address of the .Tables
132          * array can change dynamically as tables are loaded at run-time. Note:
133          * .Pointer field is not validated until after call to acpi_tb_validate_table.
134          */
135         acpi_gbl_DSDT =
136             acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_DSDT].pointer;
137
138         /*
139          * Optionally copy the entire DSDT to local memory (instead of simply
140          * mapping it.) There are some BIOSs that corrupt or replace the original
141          * DSDT, creating the need for this option. Default is FALSE, do not copy
142          * the DSDT.
143          */
144         if (acpi_gbl_copy_dsdt_locally) {
145                 new_dsdt = acpi_tb_copy_dsdt(ACPI_TABLE_INDEX_DSDT);
146                 if (new_dsdt) {
147                         acpi_gbl_DSDT = new_dsdt;
148                 }
149         }
150
151         /*
152          * Save the original DSDT header for detection of table corruption
153          * and/or replacement of the DSDT from outside the OS.
154          */
155         memcpy(&acpi_gbl_original_dsdt_header, acpi_gbl_DSDT,
156                sizeof(struct acpi_table_header));
157
158         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
159
160         /* Load and parse tables */
161
162         status = acpi_ns_load_table(ACPI_TABLE_INDEX_DSDT, acpi_gbl_root_node);
163         if (ACPI_FAILURE(status)) {
164                 ACPI_EXCEPTION((AE_INFO, status, "[DSDT] table load failed"));
165                 tables_failed++;
166         } else {
167                 tables_loaded++;
168         }
169
170         /* Load any SSDT or PSDT tables. Note: Loop leaves tables locked */
171
172         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
173         for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
174                 if (!acpi_gbl_root_table_list.tables[i].address ||
175                     (!ACPI_COMPARE_NAME
176                      (&(acpi_gbl_root_table_list.tables[i].signature),
177                       ACPI_SIG_SSDT)
178                      &&
179                      !ACPI_COMPARE_NAME(&
180                                         (acpi_gbl_root_table_list.tables[i].
181                                          signature), ACPI_SIG_PSDT)
182                      &&
183                      !ACPI_COMPARE_NAME(&
184                                         (acpi_gbl_root_table_list.tables[i].
185                                          signature), ACPI_SIG_OSDT))
186                     ||
187                     ACPI_FAILURE(acpi_tb_validate_table
188                                  (&acpi_gbl_root_table_list.tables[i]))) {
189                         continue;
190                 }
191
192                 /* Ignore errors while loading tables, get as many as possible */
193
194                 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
195                 status = acpi_ns_load_table(i, acpi_gbl_root_node);
196                 if (ACPI_FAILURE(status)) {
197                         ACPI_EXCEPTION((AE_INFO, status,
198                                         "[%4.4s] table load failed",
199                                         &acpi_gbl_root_table_list.tables[i].
200                                         signature.ascii[0]));
201                         tables_failed++;
202                 } else {
203                         tables_loaded++;
204                 }
205
206                 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
207         }
208
209         if (!tables_failed) {
210                 ACPI_INFO((AE_INFO,
211                            "All (%u) ACPI AML tables successfully loaded",
212                            tables_loaded));
213         } else {
214                 ACPI_ERROR((AE_INFO,
215                             "%u ACPI AML tables loaded, %u failed",
216                             tables_loaded, tables_failed));
217         }
218
219 unlock_and_exit:
220         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
221         return_ACPI_STATUS(status);
222 }
223
224 /*******************************************************************************
225  *
226  * FUNCTION:    acpi_install_table
227  *
228  * PARAMETERS:  address             - Address of the ACPI table to be installed.
229  *              physical            - Whether the address is a physical table
230  *                                    address or not
231  *
232  * RETURN:      Status
233  *
234  * DESCRIPTION: Dynamically install an ACPI table.
235  *              Note: This function should only be invoked after
236  *                    acpi_initialize_tables() and before acpi_load_tables().
237  *
238  ******************************************************************************/
239
240 acpi_status __init
241 acpi_install_table(acpi_physical_address address, u8 physical)
242 {
243         acpi_status status;
244         u8 flags;
245         u32 table_index;
246
247         ACPI_FUNCTION_TRACE(acpi_install_table);
248
249         if (physical) {
250                 flags = ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL;
251         } else {
252                 flags = ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL;
253         }
254
255         status = acpi_tb_install_standard_table(address, flags,
256                                                 FALSE, FALSE, &table_index);
257
258         return_ACPI_STATUS(status);
259 }
260
261 ACPI_EXPORT_SYMBOL_INIT(acpi_install_table)
262
263 /*******************************************************************************
264  *
265  * FUNCTION:    acpi_load_table
266  *
267  * PARAMETERS:  table               - Pointer to a buffer containing the ACPI
268  *                                    table to be loaded.
269  *
270  * RETURN:      Status
271  *
272  * DESCRIPTION: Dynamically load an ACPI table from the caller's buffer. Must
273  *              be a valid ACPI table with a valid ACPI table header.
274  *              Note1: Mainly intended to support hotplug addition of SSDTs.
275  *              Note2: Does not copy the incoming table. User is responsible
276  *              to ensure that the table is not deleted or unmapped.
277  *
278  ******************************************************************************/
279 acpi_status acpi_load_table(struct acpi_table_header *table)
280 {
281         acpi_status status;
282         u32 table_index;
283
284         ACPI_FUNCTION_TRACE(acpi_load_table);
285
286         /* Parameter validation */
287
288         if (!table) {
289                 return_ACPI_STATUS(AE_BAD_PARAMETER);
290         }
291
292         /* Must acquire the interpreter lock during this operation */
293
294         status = acpi_ut_acquire_mutex(ACPI_MTX_INTERPRETER);
295         if (ACPI_FAILURE(status)) {
296                 return_ACPI_STATUS(status);
297         }
298
299         /* Install the table and load it into the namespace */
300
301         ACPI_INFO((AE_INFO, "Host-directed Dynamic ACPI Table Load:"));
302         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
303
304         status = acpi_tb_install_standard_table(ACPI_PTR_TO_PHYSADDR(table),
305                                                 ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL,
306                                                 TRUE, FALSE, &table_index);
307
308         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
309         if (ACPI_FAILURE(status)) {
310                 goto unlock_and_exit;
311         }
312
313         /*
314          * Note: Now table is "INSTALLED", it must be validated before
315          * using.
316          */
317         status =
318             acpi_tb_validate_table(&acpi_gbl_root_table_list.
319                                    tables[table_index]);
320         if (ACPI_FAILURE(status)) {
321                 goto unlock_and_exit;
322         }
323
324         status = acpi_ns_load_table(table_index, acpi_gbl_root_node);
325
326         /* Invoke table handler if present */
327
328         if (acpi_gbl_table_handler) {
329                 (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_LOAD, table,
330                                              acpi_gbl_table_handler_context);
331         }
332
333 unlock_and_exit:
334         (void)acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
335         return_ACPI_STATUS(status);
336 }
337
338 ACPI_EXPORT_SYMBOL(acpi_load_table)
339
340 /*******************************************************************************
341  *
342  * FUNCTION:    acpi_unload_parent_table
343  *
344  * PARAMETERS:  object              - Handle to any namespace object owned by
345  *                                    the table to be unloaded
346  *
347  * RETURN:      Status
348  *
349  * DESCRIPTION: Via any namespace object within an SSDT or OEMx table, unloads
350  *              the table and deletes all namespace objects associated with
351  *              that table. Unloading of the DSDT is not allowed.
352  *              Note: Mainly intended to support hotplug removal of SSDTs.
353  *
354  ******************************************************************************/
355 acpi_status acpi_unload_parent_table(acpi_handle object)
356 {
357         struct acpi_namespace_node *node =
358             ACPI_CAST_PTR(struct acpi_namespace_node, object);
359         acpi_status status = AE_NOT_EXIST;
360         acpi_owner_id owner_id;
361         u32 i;
362
363         ACPI_FUNCTION_TRACE(acpi_unload_parent_table);
364
365         /* Parameter validation */
366
367         if (!object) {
368                 return_ACPI_STATUS(AE_BAD_PARAMETER);
369         }
370
371         /*
372          * The node owner_id is currently the same as the parent table ID.
373          * However, this could change in the future.
374          */
375         owner_id = node->owner_id;
376         if (!owner_id) {
377
378                 /* owner_id==0 means DSDT is the owner. DSDT cannot be unloaded */
379
380                 return_ACPI_STATUS(AE_TYPE);
381         }
382
383         /* Must acquire the interpreter lock during this operation */
384
385         status = acpi_ut_acquire_mutex(ACPI_MTX_INTERPRETER);
386         if (ACPI_FAILURE(status)) {
387                 return_ACPI_STATUS(status);
388         }
389
390         /* Find the table in the global table list */
391
392         for (i = 0; i < acpi_gbl_root_table_list.current_table_count; i++) {
393                 if (owner_id != acpi_gbl_root_table_list.tables[i].owner_id) {
394                         continue;
395                 }
396
397                 /*
398                  * Allow unload of SSDT and OEMx tables only. Do not allow unload
399                  * of the DSDT. No other types of tables should get here, since
400                  * only these types can contain AML and thus are the only types
401                  * that can create namespace objects.
402                  */
403                 if (ACPI_COMPARE_NAME
404                     (acpi_gbl_root_table_list.tables[i].signature.ascii,
405                      ACPI_SIG_DSDT)) {
406                         status = AE_TYPE;
407                         break;
408                 }
409
410                 /* Ensure the table is actually loaded */
411
412                 if (!acpi_tb_is_table_loaded(i)) {
413                         status = AE_NOT_EXIST;
414                         break;
415                 }
416
417                 /* Invoke table handler if present */
418
419                 if (acpi_gbl_table_handler) {
420                         (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_UNLOAD,
421                                                      acpi_gbl_root_table_list.
422                                                      tables[i].pointer,
423                                                      acpi_gbl_table_handler_context);
424                 }
425
426                 /*
427                  * Delete all namespace objects owned by this table. Note that
428                  * these objects can appear anywhere in the namespace by virtue
429                  * of the AML "Scope" operator. Thus, we need to track ownership
430                  * by an ID, not simply a position within the hierarchy.
431                  */
432                 status = acpi_tb_delete_namespace_by_owner(i);
433                 if (ACPI_FAILURE(status)) {
434                         break;
435                 }
436
437                 status = acpi_tb_release_owner_id(i);
438                 acpi_tb_set_table_loaded_flag(i, FALSE);
439                 break;
440         }
441
442         (void)acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
443         return_ACPI_STATUS(status);
444 }
445
446 ACPI_EXPORT_SYMBOL(acpi_unload_parent_table)