blob: e51012b90118756500c9d0ee0b225bb7e4ea5b4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: nsparse - namespace interface to AML parser
4 *
5 *****************************************************************************/
6
7/*
Bob Moorec8100dc2016-01-15 08:17:03 +08008 * Copyright (C) 2000 - 2016, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acnamesp.h"
47#include "acparser.h"
48#include "acdispat.h"
49#include "actables.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040052ACPI_MODULE_NAME("nsparse")
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*******************************************************************************
55 *
Lv Zhengde56ba92016-09-07 14:06:54 +080056 * FUNCTION: ns_execute_table
57 *
58 * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
59 * start_node - Where to enter the table into the namespace
60 *
61 * RETURN: Status
62 *
63 * DESCRIPTION: Load ACPI/AML table by executing the entire table as a
64 * term_list.
65 *
66 ******************************************************************************/
67acpi_status
68acpi_ns_execute_table(u32 table_index, struct acpi_namespace_node *start_node)
69{
70 acpi_status status;
71 struct acpi_table_header *table;
72 acpi_owner_id owner_id;
73 struct acpi_evaluate_info *info = NULL;
74 u32 aml_length;
75 u8 *aml_start;
76 union acpi_operand_object *method_obj = NULL;
77
78 ACPI_FUNCTION_TRACE(ns_execute_table);
79
80 status = acpi_get_table_by_index(table_index, &table);
81 if (ACPI_FAILURE(status)) {
82 return_ACPI_STATUS(status);
83 }
84
85 /* Table must consist of at least a complete header */
86
87 if (table->length < sizeof(struct acpi_table_header)) {
88 return_ACPI_STATUS(AE_BAD_HEADER);
89 }
90
91 aml_start = (u8 *)table + sizeof(struct acpi_table_header);
92 aml_length = table->length - sizeof(struct acpi_table_header);
93
94 status = acpi_tb_get_owner_id(table_index, &owner_id);
95 if (ACPI_FAILURE(status)) {
96 return_ACPI_STATUS(status);
97 }
98
99 /* Create, initialize, and link a new temporary method object */
100
101 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
102 if (!method_obj) {
103 return_ACPI_STATUS(AE_NO_MEMORY);
104 }
105
106 /* Allocate the evaluation information block */
107
108 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
109 if (!info) {
110 status = AE_NO_MEMORY;
111 goto cleanup;
112 }
113
114 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
115 "Create table code block: %p\n", method_obj));
116
117 method_obj->method.aml_start = aml_start;
118 method_obj->method.aml_length = aml_length;
119 method_obj->method.owner_id = owner_id;
120 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
121
122 info->pass_number = ACPI_IMODE_EXECUTE;
123 info->node = start_node;
124 info->obj_desc = method_obj;
125 info->node_flags = info->node->flags;
126 info->full_pathname = acpi_ns_get_normalized_pathname(info->node, TRUE);
127 if (!info->full_pathname) {
128 status = AE_NO_MEMORY;
129 goto cleanup;
130 }
131
132 status = acpi_ps_execute_table(info);
133
134cleanup:
135 if (info) {
136 ACPI_FREE(info->full_pathname);
137 info->full_pathname = NULL;
138 }
139 ACPI_FREE(info);
140 acpi_ut_remove_reference(method_obj);
141 return_ACPI_STATUS(status);
142}
143
144/*******************************************************************************
145 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * FUNCTION: ns_one_complete_parse
147 *
148 * PARAMETERS: pass_number - 1 or 2
149 * table_desc - The table to be parsed.
150 *
151 * RETURN: Status
152 *
153 * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
154 *
155 ******************************************************************************/
Lv Zhengde56ba92016-09-07 14:06:54 +0800156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157acpi_status
Bob Moore67a119f2008-06-10 13:42:13 +0800158acpi_ns_one_complete_parse(u32 pass_number,
159 u32 table_index,
160 struct acpi_namespace_node *start_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Len Brown4be44fc2005-08-05 00:44:28 -0400162 union acpi_parse_object *parse_root;
163 acpi_status status;
Lv Zhengeb87a052015-07-23 12:52:05 +0800164 u32 aml_length;
Bob Mooref3d2e782007-02-02 19:48:18 +0300165 u8 *aml_start;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 struct acpi_walk_state *walk_state;
Bob Mooref3d2e782007-02-02 19:48:18 +0300167 struct acpi_table_header *table;
168 acpi_owner_id owner_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Bob Mooreb229cf92006-04-21 17:15:00 -0400170 ACPI_FUNCTION_TRACE(ns_one_complete_parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Lv Zheng62eb9352015-07-23 12:52:24 +0800172 status = acpi_get_table_by_index(table_index, &table);
173 if (ACPI_FAILURE(status)) {
174 return_ACPI_STATUS(status);
175 }
176
177 /* Table must consist of at least a complete header */
178
179 if (table->length < sizeof(struct acpi_table_header)) {
180 return_ACPI_STATUS(AE_BAD_HEADER);
181 }
182
183 aml_start = (u8 *)table + sizeof(struct acpi_table_header);
184 aml_length = table->length - sizeof(struct acpi_table_header);
185
Bob Mooref3d2e782007-02-02 19:48:18 +0300186 status = acpi_tb_get_owner_id(table_index, &owner_id);
187 if (ACPI_FAILURE(status)) {
188 return_ACPI_STATUS(status);
189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Create and init a Root Node */
192
Lv Zheng62eb9352015-07-23 12:52:24 +0800193 parse_root = acpi_ps_create_scope_op(aml_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (!parse_root) {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 /* Create and initialize a new walk state */
199
Bob Mooref3d2e782007-02-02 19:48:18 +0300200 walk_state = acpi_ds_create_walk_state(owner_id, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!walk_state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 acpi_ps_free_op(parse_root);
203 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
Lv Zheng62eb9352015-07-23 12:52:24 +0800206 status = acpi_ds_init_aml_walk(walk_state, parse_root, NULL,
207 aml_start, aml_length, NULL,
208 (u8)pass_number);
Len Brown4be44fc2005-08-05 00:44:28 -0400209 if (ACPI_FAILURE(status)) {
210 acpi_ds_delete_walk_state(walk_state);
Lv Zheng62eb9352015-07-23 12:52:24 +0800211 goto cleanup;
Bob Mooref3d2e782007-02-02 19:48:18 +0300212 }
213
Bob Moorefe536992015-07-01 14:44:23 +0800214 /* Found OSDT table, enable the namespace override feature */
215
216 if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_OSDT) &&
217 pass_number == ACPI_IMODE_LOAD_PASS1) {
218 walk_state->namespace_override = TRUE;
219 }
220
Bob Moore7f4ac9f2008-04-10 19:06:39 +0400221 /* start_node is the default location to load the table */
222
223 if (start_node && start_node != acpi_gbl_root_node) {
Bob Moore1d18c052008-04-10 19:06:40 +0400224 status =
225 acpi_ds_scope_stack_push(start_node, ACPI_TYPE_METHOD,
226 walk_state);
227 if (ACPI_FAILURE(status)) {
228 acpi_ds_delete_walk_state(walk_state);
229 goto cleanup;
230 }
Bob Moore7f4ac9f2008-04-10 19:06:39 +0400231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Parse the AML */
234
Bob Moore1fad8732015-12-29 13:54:36 +0800235 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
236 "*PARSE* pass %u parse\n", pass_number));
Len Brown4be44fc2005-08-05 00:44:28 -0400237 status = acpi_ps_parse_aml(walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Lv Zheng10622bf2013-10-29 09:30:02 +0800239cleanup:
Len Brown4be44fc2005-08-05 00:44:28 -0400240 acpi_ps_delete_parse_tree(parse_root);
241 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*******************************************************************************
245 *
246 * FUNCTION: acpi_ns_parse_table
247 *
248 * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
249 * start_node - Where to enter the table into the namespace
250 *
251 * RETURN: Status
252 *
253 * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
254 *
255 ******************************************************************************/
256
257acpi_status
Bob Moore67a119f2008-06-10 13:42:13 +0800258acpi_ns_parse_table(u32 table_index, struct acpi_namespace_node *start_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Len Brown4be44fc2005-08-05 00:44:28 -0400260 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Bob Mooreb229cf92006-04-21 17:15:00 -0400262 ACPI_FUNCTION_TRACE(ns_parse_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Lv Zhengde56ba92016-09-07 14:06:54 +0800264 if (acpi_gbl_parse_table_as_term_list) {
265 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start load pass\n"));
Bob Moore1fad8732015-12-29 13:54:36 +0800266
Lv Zhengde56ba92016-09-07 14:06:54 +0800267 status = acpi_ns_execute_table(table_index, start_node);
268 if (ACPI_FAILURE(status)) {
269 return_ACPI_STATUS(status);
270 }
271 } else {
272 /*
273 * AML Parse, pass 1
274 *
275 * In this pass, we load most of the namespace. Control methods
276 * are not parsed until later. A parse tree is not created.
277 * Instead, each Parser Op subtree is deleted when it is finished.
278 * This saves a great deal of memory, and allows a small cache of
279 * parse objects to service the entire parse. The second pass of
280 * the parse then performs another complete parse of the AML.
281 */
282 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start pass 1\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Lv Zhengde56ba92016-09-07 14:06:54 +0800284 status = acpi_ns_one_complete_parse(ACPI_IMODE_LOAD_PASS1,
285 table_index, start_node);
286 if (ACPI_FAILURE(status)) {
287 return_ACPI_STATUS(status);
288 }
289
290 /*
291 * AML Parse, pass 2
292 *
293 * In this pass, we resolve forward references and other things
294 * that could not be completed during the first pass.
295 * Another complete parse of the AML is performed, but the
296 * overhead of this is compensated for by the fact that the
297 * parse objects are all cached.
298 */
299 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start pass 2\n"));
300 status = acpi_ns_one_complete_parse(ACPI_IMODE_LOAD_PASS2,
301 table_index, start_node);
302 if (ACPI_FAILURE(status)) {
303 return_ACPI_STATUS(status);
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}