ACPICA: Tables: make default region accessible during the table load
[cascardo/linux.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 - 2016, 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 #include "acevents.h"
51
52 #define _COMPONENT          ACPI_TABLES
53 ACPI_MODULE_NAME("tbxfload")
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_load_tables
58  *
59  * PARAMETERS:  None
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Load the ACPI tables from the RSDT/XSDT
64  *
65  ******************************************************************************/
66 acpi_status __init acpi_load_tables(void)
67 {
68         acpi_status status;
69
70         ACPI_FUNCTION_TRACE(acpi_load_tables);
71
72         /*
73          * Install the default operation region handlers. These are the
74          * handlers that are defined by the ACPI specification to be
75          * "always accessible" -- namely, system_memory, system_IO, and
76          * PCI_Config. This also means that no _REG methods need to be
77          * run for these address spaces. We need to have these handlers
78          * installed before any AML code can be executed, especially any
79          * module-level code (11/2015).
80          * Note that we allow OSPMs to install their own region handlers
81          * between acpi_initialize_subsystem() and acpi_load_tables() to use
82          * their customized default region handlers.
83          */
84         if (acpi_gbl_group_module_level_code) {
85                 status = acpi_ev_install_region_handlers();
86                 if (ACPI_FAILURE(status) && status != AE_ALREADY_EXISTS) {
87                         ACPI_EXCEPTION((AE_INFO, status,
88                                         "During Region initialization"));
89                         return_ACPI_STATUS(status);
90                 }
91         }
92
93         /* Load the namespace from the tables */
94
95         status = acpi_tb_load_namespace();
96
97         /* Don't let single failures abort the load */
98
99         if (status == AE_CTRL_TERMINATE) {
100                 status = AE_OK;
101         }
102
103         if (ACPI_FAILURE(status)) {
104                 ACPI_EXCEPTION((AE_INFO, status,
105                                 "While loading namespace from ACPI tables"));
106         }
107
108         return_ACPI_STATUS(status);
109 }
110
111 ACPI_EXPORT_SYMBOL_INIT(acpi_load_tables)
112
113 /*******************************************************************************
114  *
115  * FUNCTION:    acpi_tb_load_namespace
116  *
117  * PARAMETERS:  None
118  *
119  * RETURN:      Status
120  *
121  * DESCRIPTION: Load the namespace from the DSDT and all SSDTs/PSDTs found in
122  *              the RSDT/XSDT.
123  *
124  ******************************************************************************/
125 acpi_status acpi_tb_load_namespace(void)
126 {
127         acpi_status status;
128         u32 i;
129         struct acpi_table_header *new_dsdt;
130         struct acpi_table_desc *table;
131         u32 tables_loaded = 0;
132         u32 tables_failed = 0;
133
134         ACPI_FUNCTION_TRACE(tb_load_namespace);
135
136         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
137
138         /*
139          * Load the namespace. The DSDT is required, but any SSDT and
140          * PSDT tables are optional. Verify the DSDT.
141          */
142         table = &acpi_gbl_root_table_list.tables[acpi_gbl_dsdt_index];
143
144         if (!acpi_gbl_root_table_list.current_table_count ||
145             !ACPI_COMPARE_NAME(table->signature.ascii, ACPI_SIG_DSDT) ||
146             ACPI_FAILURE(acpi_tb_validate_table(table))) {
147                 status = AE_NO_ACPI_TABLES;
148                 goto unlock_and_exit;
149         }
150
151         /*
152          * Save the DSDT pointer for simple access. This is the mapped memory
153          * address. We must take care here because the address of the .Tables
154          * array can change dynamically as tables are loaded at run-time. Note:
155          * .Pointer field is not validated until after call to acpi_tb_validate_table.
156          */
157         acpi_gbl_DSDT = table->pointer;
158
159         /*
160          * Optionally copy the entire DSDT to local memory (instead of simply
161          * mapping it.) There are some BIOSs that corrupt or replace the original
162          * DSDT, creating the need for this option. Default is FALSE, do not copy
163          * the DSDT.
164          */
165         if (acpi_gbl_copy_dsdt_locally) {
166                 new_dsdt = acpi_tb_copy_dsdt(acpi_gbl_dsdt_index);
167                 if (new_dsdt) {
168                         acpi_gbl_DSDT = new_dsdt;
169                 }
170         }
171
172         /*
173          * Save the original DSDT header for detection of table corruption
174          * and/or replacement of the DSDT from outside the OS.
175          */
176         memcpy(&acpi_gbl_original_dsdt_header, acpi_gbl_DSDT,
177                sizeof(struct acpi_table_header));
178
179         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
180
181         /* Load and parse tables */
182
183         status = acpi_ns_load_table(acpi_gbl_dsdt_index, acpi_gbl_root_node);
184         if (ACPI_FAILURE(status)) {
185                 ACPI_EXCEPTION((AE_INFO, status, "[DSDT] table load failed"));
186                 tables_failed++;
187         } else {
188                 tables_loaded++;
189         }
190
191         /* Load any SSDT or PSDT tables. Note: Loop leaves tables locked */
192
193         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
194         for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
195                 table = &acpi_gbl_root_table_list.tables[i];
196
197                 if (!acpi_gbl_root_table_list.tables[i].address ||
198                     (!ACPI_COMPARE_NAME(table->signature.ascii, ACPI_SIG_SSDT)
199                      && !ACPI_COMPARE_NAME(table->signature.ascii,
200                                            ACPI_SIG_PSDT)
201                      && !ACPI_COMPARE_NAME(table->signature.ascii,
202                                            ACPI_SIG_OSDT))
203                     || ACPI_FAILURE(acpi_tb_validate_table(table))) {
204                         continue;
205                 }
206
207                 /* Ignore errors while loading tables, get as many as possible */
208
209                 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
210                 status = acpi_ns_load_table(i, acpi_gbl_root_node);
211                 if (ACPI_FAILURE(status)) {
212                         ACPI_EXCEPTION((AE_INFO, status,
213                                         "(%4.4s:%8.8s) while loading table",
214                                         table->signature.ascii,
215                                         table->pointer->oem_table_id));
216
217                         tables_failed++;
218
219                         ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
220                                               "Table [%4.4s:%8.8s] (id FF) - Table namespace load failed\n\n",
221                                               table->signature.ascii,
222                                               table->pointer->oem_table_id));
223                 } else {
224                         tables_loaded++;
225                 }
226
227                 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
228         }
229
230         if (!tables_failed) {
231                 ACPI_INFO(("%u ACPI AML tables successfully acquired and loaded\n", tables_loaded));
232         } else {
233                 ACPI_ERROR((AE_INFO,
234                             "%u table load failures, %u successful",
235                             tables_failed, tables_loaded));
236
237                 /* Indicate at least one failure */
238
239                 status = AE_CTRL_TERMINATE;
240         }
241
242 unlock_and_exit:
243         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
244         return_ACPI_STATUS(status);
245 }
246
247 /*******************************************************************************
248  *
249  * FUNCTION:    acpi_install_table
250  *
251  * PARAMETERS:  address             - Address of the ACPI table to be installed.
252  *              physical            - Whether the address is a physical table
253  *                                    address or not
254  *
255  * RETURN:      Status
256  *
257  * DESCRIPTION: Dynamically install an ACPI table.
258  *              Note: This function should only be invoked after
259  *                    acpi_initialize_tables() and before acpi_load_tables().
260  *
261  ******************************************************************************/
262
263 acpi_status __init
264 acpi_install_table(acpi_physical_address address, u8 physical)
265 {
266         acpi_status status;
267         u8 flags;
268         u32 table_index;
269
270         ACPI_FUNCTION_TRACE(acpi_install_table);
271
272         if (physical) {
273                 flags = ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL;
274         } else {
275                 flags = ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL;
276         }
277
278         status = acpi_tb_install_standard_table(address, flags,
279                                                 FALSE, FALSE, &table_index);
280
281         return_ACPI_STATUS(status);
282 }
283
284 ACPI_EXPORT_SYMBOL_INIT(acpi_install_table)
285
286 /*******************************************************************************
287  *
288  * FUNCTION:    acpi_load_table
289  *
290  * PARAMETERS:  table               - Pointer to a buffer containing the ACPI
291  *                                    table to be loaded.
292  *
293  * RETURN:      Status
294  *
295  * DESCRIPTION: Dynamically load an ACPI table from the caller's buffer. Must
296  *              be a valid ACPI table with a valid ACPI table header.
297  *              Note1: Mainly intended to support hotplug addition of SSDTs.
298  *              Note2: Does not copy the incoming table. User is responsible
299  *              to ensure that the table is not deleted or unmapped.
300  *
301  ******************************************************************************/
302 acpi_status acpi_load_table(struct acpi_table_header *table)
303 {
304         acpi_status status;
305         u32 table_index;
306
307         ACPI_FUNCTION_TRACE(acpi_load_table);
308
309         /* Parameter validation */
310
311         if (!table) {
312                 return_ACPI_STATUS(AE_BAD_PARAMETER);
313         }
314
315         /* Must acquire the interpreter lock during this operation */
316
317         status = acpi_ut_acquire_mutex(ACPI_MTX_INTERPRETER);
318         if (ACPI_FAILURE(status)) {
319                 return_ACPI_STATUS(status);
320         }
321
322         /* Install the table and load it into the namespace */
323
324         ACPI_INFO(("Host-directed Dynamic ACPI Table Load:"));
325         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
326
327         status = acpi_tb_install_standard_table(ACPI_PTR_TO_PHYSADDR(table),
328                                                 ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL,
329                                                 TRUE, FALSE, &table_index);
330
331         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
332         if (ACPI_FAILURE(status)) {
333                 goto unlock_and_exit;
334         }
335
336         /*
337          * Note: Now table is "INSTALLED", it must be validated before
338          * using.
339          */
340         status =
341             acpi_tb_validate_table(&acpi_gbl_root_table_list.
342                                    tables[table_index]);
343         if (ACPI_FAILURE(status)) {
344                 goto unlock_and_exit;
345         }
346
347         status = acpi_ns_load_table(table_index, acpi_gbl_root_node);
348
349         /* Invoke table handler if present */
350
351         if (acpi_gbl_table_handler) {
352                 (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_LOAD, table,
353                                              acpi_gbl_table_handler_context);
354         }
355
356 unlock_and_exit:
357         (void)acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
358         return_ACPI_STATUS(status);
359 }
360
361 ACPI_EXPORT_SYMBOL(acpi_load_table)
362
363 /*******************************************************************************
364  *
365  * FUNCTION:    acpi_unload_parent_table
366  *
367  * PARAMETERS:  object              - Handle to any namespace object owned by
368  *                                    the table to be unloaded
369  *
370  * RETURN:      Status
371  *
372  * DESCRIPTION: Via any namespace object within an SSDT or OEMx table, unloads
373  *              the table and deletes all namespace objects associated with
374  *              that table. Unloading of the DSDT is not allowed.
375  *              Note: Mainly intended to support hotplug removal of SSDTs.
376  *
377  ******************************************************************************/
378 acpi_status acpi_unload_parent_table(acpi_handle object)
379 {
380         struct acpi_namespace_node *node =
381             ACPI_CAST_PTR(struct acpi_namespace_node, object);
382         acpi_status status = AE_NOT_EXIST;
383         acpi_owner_id owner_id;
384         u32 i;
385
386         ACPI_FUNCTION_TRACE(acpi_unload_parent_table);
387
388         /* Parameter validation */
389
390         if (!object) {
391                 return_ACPI_STATUS(AE_BAD_PARAMETER);
392         }
393
394         /*
395          * The node owner_id is currently the same as the parent table ID.
396          * However, this could change in the future.
397          */
398         owner_id = node->owner_id;
399         if (!owner_id) {
400
401                 /* owner_id==0 means DSDT is the owner. DSDT cannot be unloaded */
402
403                 return_ACPI_STATUS(AE_TYPE);
404         }
405
406         /* Must acquire the interpreter lock during this operation */
407
408         status = acpi_ut_acquire_mutex(ACPI_MTX_INTERPRETER);
409         if (ACPI_FAILURE(status)) {
410                 return_ACPI_STATUS(status);
411         }
412
413         /* Find the table in the global table list */
414
415         for (i = 0; i < acpi_gbl_root_table_list.current_table_count; i++) {
416                 if (owner_id != acpi_gbl_root_table_list.tables[i].owner_id) {
417                         continue;
418                 }
419
420                 /*
421                  * Allow unload of SSDT and OEMx tables only. Do not allow unload
422                  * of the DSDT. No other types of tables should get here, since
423                  * only these types can contain AML and thus are the only types
424                  * that can create namespace objects.
425                  */
426                 if (ACPI_COMPARE_NAME
427                     (acpi_gbl_root_table_list.tables[i].signature.ascii,
428                      ACPI_SIG_DSDT)) {
429                         status = AE_TYPE;
430                         break;
431                 }
432
433                 /* Ensure the table is actually loaded */
434
435                 if (!acpi_tb_is_table_loaded(i)) {
436                         status = AE_NOT_EXIST;
437                         break;
438                 }
439
440                 /* Invoke table handler if present */
441
442                 if (acpi_gbl_table_handler) {
443                         (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_UNLOAD,
444                                                      acpi_gbl_root_table_list.
445                                                      tables[i].pointer,
446                                                      acpi_gbl_table_handler_context);
447                 }
448
449                 /*
450                  * Delete all namespace objects owned by this table. Note that
451                  * these objects can appear anywhere in the namespace by virtue
452                  * of the AML "Scope" operator. Thus, we need to track ownership
453                  * by an ID, not simply a position within the hierarchy.
454                  */
455                 status = acpi_tb_delete_namespace_by_owner(i);
456                 if (ACPI_FAILURE(status)) {
457                         break;
458                 }
459
460                 status = acpi_tb_release_owner_id(i);
461                 acpi_tb_set_table_loaded_flag(i, FALSE);
462                 break;
463         }
464
465         (void)acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
466         return_ACPI_STATUS(status);
467 }
468
469 ACPI_EXPORT_SYMBOL(acpi_unload_parent_table)