2daa9a093c56510483b8373e72b84196f27dd674
[cascardo/linux.git] / drivers / acpi / acpica / nsload.c
1 /******************************************************************************
2  *
3  * Module Name: nsload - namespace loading/expanding/contracting procedures
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 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acdispat.h"
48 #include "actables.h"
49
50 #define _COMPONENT          ACPI_NAMESPACE
51 ACPI_MODULE_NAME("nsload")
52
53 /* Local prototypes */
54 #ifdef ACPI_FUTURE_IMPLEMENTATION
55 acpi_status acpi_ns_unload_namespace(acpi_handle handle);
56
57 static acpi_status acpi_ns_delete_subtree(acpi_handle start_handle);
58 #endif
59
60 #ifndef ACPI_NO_METHOD_EXECUTION
61 /*******************************************************************************
62  *
63  * FUNCTION:    acpi_ns_load_table
64  *
65  * PARAMETERS:  table_index     - Index for table to be loaded
66  *              node            - Owning NS node
67  *
68  * RETURN:      Status
69  *
70  * DESCRIPTION: Load one ACPI table into the namespace
71  *
72  ******************************************************************************/
73
74 acpi_status
75 acpi_ns_load_table(u32 table_index, struct acpi_namespace_node *node)
76 {
77         acpi_status status;
78
79         ACPI_FUNCTION_TRACE(ns_load_table);
80
81         /*
82          * Parse the table and load the namespace with all named
83          * objects found within. Control methods are NOT parsed
84          * at this time. In fact, the control methods cannot be
85          * parsed until the entire namespace is loaded, because
86          * if a control method makes a forward reference (call)
87          * to another control method, we can't continue parsing
88          * because we don't know how many arguments to parse next!
89          */
90         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
91         if (ACPI_FAILURE(status)) {
92                 return_ACPI_STATUS(status);
93         }
94
95         /* If table already loaded into namespace, just return */
96
97         if (acpi_tb_is_table_loaded(table_index)) {
98                 status = AE_ALREADY_EXISTS;
99                 goto unlock;
100         }
101
102         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
103                           "**** Loading table into namespace ****\n"));
104
105         status = acpi_tb_allocate_owner_id(table_index);
106         if (ACPI_FAILURE(status)) {
107                 goto unlock;
108         }
109
110         status = acpi_ns_parse_table(table_index, node);
111         if (ACPI_SUCCESS(status)) {
112                 acpi_tb_set_table_loaded_flag(table_index, TRUE);
113         } else {
114                 /*
115                  * On error, delete any namespace objects created by this table.
116                  * We cannot initialize these objects, so delete them. There are
117                  * a couple of expecially bad cases:
118                  * AE_ALREADY_EXISTS - namespace collision.
119                  * AE_NOT_FOUND - the target of a Scope operator does not
120                  * exist. This target of Scope must already exist in the
121                  * namespace, as per the ACPI specification.
122                  */
123                 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
124                 acpi_ns_delete_namespace_by_owner(acpi_gbl_root_table_list.
125                                                   tables[table_index].owner_id);
126
127                 acpi_tb_release_owner_id(table_index);
128                 return_ACPI_STATUS(status);
129         }
130
131 unlock:
132         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
133
134         if (ACPI_FAILURE(status)) {
135                 return_ACPI_STATUS(status);
136         }
137
138         /*
139          * Now we can parse the control methods. We always parse
140          * them here for a sanity check, and if configured for
141          * just-in-time parsing, we delete the control method
142          * parse trees.
143          */
144         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
145                           "**** Begin Table Object Initialization\n"));
146
147         status = acpi_ds_initialize_objects(table_index, node);
148
149         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
150                           "**** Completed Table Object Initialization\n"));
151
152         /*
153          * Execute any module-level code that was detected during the table load
154          * phase. Although illegal since ACPI 2.0, there are many machines that
155          * contain this type of code. Each block of detected executable AML code
156          * outside of any control method is wrapped with a temporary control
157          * method object and placed on a global list. The methods on this list
158          * are executed below.
159          *
160          * This case executes the module-level code for each table immediately
161          * after the table has been loaded. This provides compatibility with
162          * other ACPI implementations. Optionally, the execution can be deferred
163          * until later, see acpi_initialize_objects.
164          */
165         if (!acpi_gbl_parse_table_as_term_list
166             && !acpi_gbl_group_module_level_code) {
167                 acpi_ns_exec_module_code_list();
168         }
169
170         return_ACPI_STATUS(status);
171 }
172
173 #ifdef ACPI_OBSOLETE_FUNCTIONS
174 /*******************************************************************************
175  *
176  * FUNCTION:    acpi_load_namespace
177  *
178  * PARAMETERS:  None
179  *
180  * RETURN:      Status
181  *
182  * DESCRIPTION: Load the name space from what ever is pointed to by DSDT.
183  *              (DSDT points to either the BIOS or a buffer.)
184  *
185  ******************************************************************************/
186
187 acpi_status acpi_ns_load_namespace(void)
188 {
189         acpi_status status;
190
191         ACPI_FUNCTION_TRACE(acpi_load_name_space);
192
193         /* There must be at least a DSDT installed */
194
195         if (acpi_gbl_DSDT == NULL) {
196                 ACPI_ERROR((AE_INFO, "DSDT is not in memory"));
197                 return_ACPI_STATUS(AE_NO_ACPI_TABLES);
198         }
199
200         /*
201          * Load the namespace. The DSDT is required,
202          * but the SSDT and PSDT tables are optional.
203          */
204         status = acpi_ns_load_table_by_type(ACPI_TABLE_ID_DSDT);
205         if (ACPI_FAILURE(status)) {
206                 return_ACPI_STATUS(status);
207         }
208
209         /* Ignore exceptions from these */
210
211         (void)acpi_ns_load_table_by_type(ACPI_TABLE_ID_SSDT);
212         (void)acpi_ns_load_table_by_type(ACPI_TABLE_ID_PSDT);
213
214         ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
215                               "ACPI Namespace successfully loaded at root %p\n",
216                               acpi_gbl_root_node));
217
218         return_ACPI_STATUS(status);
219 }
220 #endif
221
222 #ifdef ACPI_FUTURE_IMPLEMENTATION
223 /*******************************************************************************
224  *
225  * FUNCTION:    acpi_ns_delete_subtree
226  *
227  * PARAMETERS:  start_handle        - Handle in namespace where search begins
228  *
229  * RETURNS      Status
230  *
231  * DESCRIPTION: Walks the namespace starting at the given handle and deletes
232  *              all objects, entries, and scopes in the entire subtree.
233  *
234  *              Namespace/Interpreter should be locked or the subsystem should
235  *              be in shutdown before this routine is called.
236  *
237  ******************************************************************************/
238
239 static acpi_status acpi_ns_delete_subtree(acpi_handle start_handle)
240 {
241         acpi_status status;
242         acpi_handle child_handle;
243         acpi_handle parent_handle;
244         acpi_handle next_child_handle;
245         acpi_handle dummy;
246         u32 level;
247
248         ACPI_FUNCTION_TRACE(ns_delete_subtree);
249
250         parent_handle = start_handle;
251         child_handle = NULL;
252         level = 1;
253
254         /*
255          * Traverse the tree of objects until we bubble back up
256          * to where we started.
257          */
258         while (level > 0) {
259
260                 /* Attempt to get the next object in this scope */
261
262                 status = acpi_get_next_object(ACPI_TYPE_ANY, parent_handle,
263                                               child_handle, &next_child_handle);
264
265                 child_handle = next_child_handle;
266
267                 /* Did we get a new object? */
268
269                 if (ACPI_SUCCESS(status)) {
270
271                         /* Check if this object has any children */
272
273                         if (ACPI_SUCCESS
274                             (acpi_get_next_object
275                              (ACPI_TYPE_ANY, child_handle, NULL, &dummy))) {
276                                 /*
277                                  * There is at least one child of this object,
278                                  * visit the object
279                                  */
280                                 level++;
281                                 parent_handle = child_handle;
282                                 child_handle = NULL;
283                         }
284                 } else {
285                         /*
286                          * No more children in this object, go back up to
287                          * the object's parent
288                          */
289                         level--;
290
291                         /* Delete all children now */
292
293                         acpi_ns_delete_children(child_handle);
294
295                         child_handle = parent_handle;
296                         status = acpi_get_parent(parent_handle, &parent_handle);
297                         if (ACPI_FAILURE(status)) {
298                                 return_ACPI_STATUS(status);
299                         }
300                 }
301         }
302
303         /* Now delete the starting object, and we are done */
304
305         acpi_ns_remove_node(child_handle);
306         return_ACPI_STATUS(AE_OK);
307 }
308
309 /*******************************************************************************
310  *
311  *  FUNCTION:       acpi_ns_unload_name_space
312  *
313  *  PARAMETERS:     handle          - Root of namespace subtree to be deleted
314  *
315  *  RETURN:         Status
316  *
317  *  DESCRIPTION:    Shrinks the namespace, typically in response to an undocking
318  *                  event. Deletes an entire subtree starting from (and
319  *                  including) the given handle.
320  *
321  ******************************************************************************/
322
323 acpi_status acpi_ns_unload_namespace(acpi_handle handle)
324 {
325         acpi_status status;
326
327         ACPI_FUNCTION_TRACE(ns_unload_name_space);
328
329         /* Parameter validation */
330
331         if (!acpi_gbl_root_node) {
332                 return_ACPI_STATUS(AE_NO_NAMESPACE);
333         }
334
335         if (!handle) {
336                 return_ACPI_STATUS(AE_BAD_PARAMETER);
337         }
338
339         /* This function does the real work */
340
341         status = acpi_ns_delete_subtree(handle);
342         return_ACPI_STATUS(status);
343 }
344 #endif
345 #endif