Loading...
1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2/******************************************************************************
3 *
4 * Module Name: uteval - Object evaluation
5 *
6 * Copyright (C) 2000 - 2023, Intel Corp.
7 *
8 *****************************************************************************/
9
10#include <acpi/acpi.h>
11#include "accommon.h"
12#include "acnamesp.h"
13
14#define _COMPONENT ACPI_UTILITIES
15ACPI_MODULE_NAME("uteval")
16
17/*******************************************************************************
18 *
19 * FUNCTION: acpi_ut_evaluate_object
20 *
21 * PARAMETERS: prefix_node - Starting node
22 * path - Path to object from starting node
23 * expected_return_types - Bitmap of allowed return types
24 * return_desc - Where a return value is stored
25 *
26 * RETURN: Status
27 *
28 * DESCRIPTION: Evaluates a namespace object and verifies the type of the
29 * return object. Common code that simplifies accessing objects
30 * that have required return objects of fixed types.
31 *
32 * NOTE: Internal function, no parameter validation
33 *
34 ******************************************************************************/
35
36acpi_status
37acpi_ut_evaluate_object(struct acpi_namespace_node *prefix_node,
38 const char *path,
39 u32 expected_return_btypes,
40 union acpi_operand_object **return_desc)
41{
42 struct acpi_evaluate_info *info;
43 acpi_status status;
44 u32 return_btype;
45
46 ACPI_FUNCTION_TRACE(ut_evaluate_object);
47
48 /* Allocate the evaluation information block */
49
50 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
51 if (!info) {
52 return_ACPI_STATUS(AE_NO_MEMORY);
53 }
54
55 info->prefix_node = prefix_node;
56 info->relative_pathname = path;
57
58 /* Evaluate the object/method */
59
60 status = acpi_ns_evaluate(info);
61 if (ACPI_FAILURE(status)) {
62 if (status == AE_NOT_FOUND) {
63 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
64 "[%4.4s.%s] was not found\n",
65 acpi_ut_get_node_name(prefix_node),
66 path));
67 } else {
68 ACPI_ERROR_METHOD("Method execution failed",
69 prefix_node, path, status);
70 }
71
72 goto cleanup;
73 }
74
75 /* Did we get a return object? */
76
77 if (!info->return_object) {
78 if (expected_return_btypes) {
79 ACPI_ERROR_METHOD("No object was returned from",
80 prefix_node, path, AE_NOT_EXIST);
81
82 status = AE_NOT_EXIST;
83 }
84
85 goto cleanup;
86 }
87
88 /* Map the return object type to the bitmapped type */
89
90 switch ((info->return_object)->common.type) {
91 case ACPI_TYPE_INTEGER:
92
93 return_btype = ACPI_BTYPE_INTEGER;
94 break;
95
96 case ACPI_TYPE_BUFFER:
97
98 return_btype = ACPI_BTYPE_BUFFER;
99 break;
100
101 case ACPI_TYPE_STRING:
102
103 return_btype = ACPI_BTYPE_STRING;
104 break;
105
106 case ACPI_TYPE_PACKAGE:
107
108 return_btype = ACPI_BTYPE_PACKAGE;
109 break;
110
111 default:
112
113 return_btype = 0;
114 break;
115 }
116
117 if ((acpi_gbl_enable_interpreter_slack) && (!expected_return_btypes)) {
118 /*
119 * We received a return object, but one was not expected. This can
120 * happen frequently if the "implicit return" feature is enabled.
121 * Just delete the return object and return AE_OK.
122 */
123 acpi_ut_remove_reference(info->return_object);
124 goto cleanup;
125 }
126
127 /* Is the return object one of the expected types? */
128
129 if (!(expected_return_btypes & return_btype)) {
130 ACPI_ERROR_METHOD("Return object type is incorrect",
131 prefix_node, path, AE_TYPE);
132
133 ACPI_ERROR((AE_INFO,
134 "Type returned from %s was incorrect: %s, expected Btypes: 0x%X",
135 path,
136 acpi_ut_get_object_type_name(info->return_object),
137 expected_return_btypes));
138
139 /* On error exit, we must delete the return object */
140
141 acpi_ut_remove_reference(info->return_object);
142 status = AE_TYPE;
143 goto cleanup;
144 }
145
146 /* Object type is OK, return it */
147
148 *return_desc = info->return_object;
149
150cleanup:
151 ACPI_FREE(info);
152 return_ACPI_STATUS(status);
153}
154
155/*******************************************************************************
156 *
157 * FUNCTION: acpi_ut_evaluate_numeric_object
158 *
159 * PARAMETERS: object_name - Object name to be evaluated
160 * device_node - Node for the device
161 * value - Where the value is returned
162 *
163 * RETURN: Status
164 *
165 * DESCRIPTION: Evaluates a numeric namespace object for a selected device
166 * and stores result in *Value.
167 *
168 * NOTE: Internal function, no parameter validation
169 *
170 ******************************************************************************/
171
172acpi_status
173acpi_ut_evaluate_numeric_object(const char *object_name,
174 struct acpi_namespace_node *device_node,
175 u64 *value)
176{
177 union acpi_operand_object *obj_desc;
178 acpi_status status;
179
180 ACPI_FUNCTION_TRACE(ut_evaluate_numeric_object);
181
182 status = acpi_ut_evaluate_object(device_node, object_name,
183 ACPI_BTYPE_INTEGER, &obj_desc);
184 if (ACPI_FAILURE(status)) {
185 return_ACPI_STATUS(status);
186 }
187
188 /* Get the returned Integer */
189
190 *value = obj_desc->integer.value;
191
192 /* On exit, we must delete the return object */
193
194 acpi_ut_remove_reference(obj_desc);
195 return_ACPI_STATUS(status);
196}
197
198/*******************************************************************************
199 *
200 * FUNCTION: acpi_ut_execute_STA
201 *
202 * PARAMETERS: device_node - Node for the device
203 * flags - Where the status flags are returned
204 *
205 * RETURN: Status
206 *
207 * DESCRIPTION: Executes _STA for selected device and stores results in
208 * *Flags. If _STA does not exist, then the device is assumed
209 * to be present/functional/enabled (as per the ACPI spec).
210 *
211 * NOTE: Internal function, no parameter validation
212 *
213 ******************************************************************************/
214
215acpi_status
216acpi_ut_execute_STA(struct acpi_namespace_node *device_node, u32 * flags)
217{
218 union acpi_operand_object *obj_desc;
219 acpi_status status;
220
221 ACPI_FUNCTION_TRACE(ut_execute_STA);
222
223 status = acpi_ut_evaluate_object(device_node, METHOD_NAME__STA,
224 ACPI_BTYPE_INTEGER, &obj_desc);
225 if (ACPI_FAILURE(status)) {
226 if (AE_NOT_FOUND == status) {
227 /*
228 * if _STA does not exist, then (as per the ACPI specification),
229 * the returned flags will indicate that the device is present,
230 * functional, and enabled.
231 */
232 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
233 "_STA on %4.4s was not found, assuming device is present\n",
234 acpi_ut_get_node_name(device_node)));
235
236 *flags = ACPI_UINT32_MAX;
237 status = AE_OK;
238 }
239
240 return_ACPI_STATUS(status);
241 }
242
243 /* Extract the status flags */
244
245 *flags = (u32) obj_desc->integer.value;
246
247 /* On exit, we must delete the return object */
248
249 acpi_ut_remove_reference(obj_desc);
250 return_ACPI_STATUS(status);
251}
252
253/*******************************************************************************
254 *
255 * FUNCTION: acpi_ut_execute_power_methods
256 *
257 * PARAMETERS: device_node - Node for the device
258 * method_names - Array of power method names
259 * method_count - Number of methods to execute
260 * out_values - Where the power method values are returned
261 *
262 * RETURN: Status, out_values
263 *
264 * DESCRIPTION: Executes the specified power methods for the device and returns
265 * the result(s).
266 *
267 * NOTE: Internal function, no parameter validation
268 *
269******************************************************************************/
270
271acpi_status
272acpi_ut_execute_power_methods(struct acpi_namespace_node *device_node,
273 const char **method_names,
274 u8 method_count, u8 *out_values)
275{
276 union acpi_operand_object *obj_desc;
277 acpi_status status;
278 acpi_status final_status = AE_NOT_FOUND;
279 u32 i;
280
281 ACPI_FUNCTION_TRACE(ut_execute_power_methods);
282
283 for (i = 0; i < method_count; i++) {
284 /*
285 * Execute the power method (_sx_d or _sx_w). The only allowable
286 * return type is an Integer.
287 */
288 status = acpi_ut_evaluate_object(device_node,
289 ACPI_CAST_PTR(char,
290 method_names[i]),
291 ACPI_BTYPE_INTEGER, &obj_desc);
292 if (ACPI_SUCCESS(status)) {
293 out_values[i] = (u8)obj_desc->integer.value;
294
295 /* Delete the return object */
296
297 acpi_ut_remove_reference(obj_desc);
298 final_status = AE_OK; /* At least one value is valid */
299 continue;
300 }
301
302 out_values[i] = ACPI_UINT8_MAX;
303 if (status == AE_NOT_FOUND) {
304 continue; /* Ignore if not found */
305 }
306
307 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
308 "Failed %s on Device %4.4s, %s\n",
309 ACPI_CAST_PTR(char, method_names[i]),
310 acpi_ut_get_node_name(device_node),
311 acpi_format_exception(status)));
312 }
313
314 return_ACPI_STATUS(final_status);
315}
1/******************************************************************************
2 *
3 * Module Name: uteval - Object evaluation
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2012, 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
48#define _COMPONENT ACPI_UTILITIES
49ACPI_MODULE_NAME("uteval")
50
51/*******************************************************************************
52 *
53 * FUNCTION: acpi_ut_evaluate_object
54 *
55 * PARAMETERS: prefix_node - Starting node
56 * Path - Path to object from starting node
57 * expected_return_types - Bitmap of allowed return types
58 * return_desc - Where a return value is stored
59 *
60 * RETURN: Status
61 *
62 * DESCRIPTION: Evaluates a namespace object and verifies the type of the
63 * return object. Common code that simplifies accessing objects
64 * that have required return objects of fixed types.
65 *
66 * NOTE: Internal function, no parameter validation
67 *
68 ******************************************************************************/
69
70acpi_status
71acpi_ut_evaluate_object(struct acpi_namespace_node *prefix_node,
72 char *path,
73 u32 expected_return_btypes,
74 union acpi_operand_object **return_desc)
75{
76 struct acpi_evaluate_info *info;
77 acpi_status status;
78 u32 return_btype;
79
80 ACPI_FUNCTION_TRACE(ut_evaluate_object);
81
82 /* Allocate the evaluation information block */
83
84 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
85 if (!info) {
86 return_ACPI_STATUS(AE_NO_MEMORY);
87 }
88
89 info->prefix_node = prefix_node;
90 info->pathname = path;
91
92 /* Evaluate the object/method */
93
94 status = acpi_ns_evaluate(info);
95 if (ACPI_FAILURE(status)) {
96 if (status == AE_NOT_FOUND) {
97 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
98 "[%4.4s.%s] was not found\n",
99 acpi_ut_get_node_name(prefix_node),
100 path));
101 } else {
102 ACPI_ERROR_METHOD("Method execution failed",
103 prefix_node, path, status);
104 }
105
106 goto cleanup;
107 }
108
109 /* Did we get a return object? */
110
111 if (!info->return_object) {
112 if (expected_return_btypes) {
113 ACPI_ERROR_METHOD("No object was returned from",
114 prefix_node, path, AE_NOT_EXIST);
115
116 status = AE_NOT_EXIST;
117 }
118
119 goto cleanup;
120 }
121
122 /* Map the return object type to the bitmapped type */
123
124 switch ((info->return_object)->common.type) {
125 case ACPI_TYPE_INTEGER:
126 return_btype = ACPI_BTYPE_INTEGER;
127 break;
128
129 case ACPI_TYPE_BUFFER:
130 return_btype = ACPI_BTYPE_BUFFER;
131 break;
132
133 case ACPI_TYPE_STRING:
134 return_btype = ACPI_BTYPE_STRING;
135 break;
136
137 case ACPI_TYPE_PACKAGE:
138 return_btype = ACPI_BTYPE_PACKAGE;
139 break;
140
141 default:
142 return_btype = 0;
143 break;
144 }
145
146 if ((acpi_gbl_enable_interpreter_slack) && (!expected_return_btypes)) {
147 /*
148 * We received a return object, but one was not expected. This can
149 * happen frequently if the "implicit return" feature is enabled.
150 * Just delete the return object and return AE_OK.
151 */
152 acpi_ut_remove_reference(info->return_object);
153 goto cleanup;
154 }
155
156 /* Is the return object one of the expected types? */
157
158 if (!(expected_return_btypes & return_btype)) {
159 ACPI_ERROR_METHOD("Return object type is incorrect",
160 prefix_node, path, AE_TYPE);
161
162 ACPI_ERROR((AE_INFO,
163 "Type returned from %s was incorrect: %s, expected Btypes: 0x%X",
164 path,
165 acpi_ut_get_object_type_name(info->return_object),
166 expected_return_btypes));
167
168 /* On error exit, we must delete the return object */
169
170 acpi_ut_remove_reference(info->return_object);
171 status = AE_TYPE;
172 goto cleanup;
173 }
174
175 /* Object type is OK, return it */
176
177 *return_desc = info->return_object;
178
179 cleanup:
180 ACPI_FREE(info);
181 return_ACPI_STATUS(status);
182}
183
184/*******************************************************************************
185 *
186 * FUNCTION: acpi_ut_evaluate_numeric_object
187 *
188 * PARAMETERS: object_name - Object name to be evaluated
189 * device_node - Node for the device
190 * Value - Where the value is returned
191 *
192 * RETURN: Status
193 *
194 * DESCRIPTION: Evaluates a numeric namespace object for a selected device
195 * and stores result in *Value.
196 *
197 * NOTE: Internal function, no parameter validation
198 *
199 ******************************************************************************/
200
201acpi_status
202acpi_ut_evaluate_numeric_object(char *object_name,
203 struct acpi_namespace_node *device_node,
204 u64 *value)
205{
206 union acpi_operand_object *obj_desc;
207 acpi_status status;
208
209 ACPI_FUNCTION_TRACE(ut_evaluate_numeric_object);
210
211 status = acpi_ut_evaluate_object(device_node, object_name,
212 ACPI_BTYPE_INTEGER, &obj_desc);
213 if (ACPI_FAILURE(status)) {
214 return_ACPI_STATUS(status);
215 }
216
217 /* Get the returned Integer */
218
219 *value = obj_desc->integer.value;
220
221 /* On exit, we must delete the return object */
222
223 acpi_ut_remove_reference(obj_desc);
224 return_ACPI_STATUS(status);
225}
226
227/*******************************************************************************
228 *
229 * FUNCTION: acpi_ut_execute_STA
230 *
231 * PARAMETERS: device_node - Node for the device
232 * Flags - Where the status flags are returned
233 *
234 * RETURN: Status
235 *
236 * DESCRIPTION: Executes _STA for selected device and stores results in
237 * *Flags.
238 *
239 * NOTE: Internal function, no parameter validation
240 *
241 ******************************************************************************/
242
243acpi_status
244acpi_ut_execute_STA(struct acpi_namespace_node *device_node, u32 * flags)
245{
246 union acpi_operand_object *obj_desc;
247 acpi_status status;
248
249 ACPI_FUNCTION_TRACE(ut_execute_STA);
250
251 status = acpi_ut_evaluate_object(device_node, METHOD_NAME__STA,
252 ACPI_BTYPE_INTEGER, &obj_desc);
253 if (ACPI_FAILURE(status)) {
254 if (AE_NOT_FOUND == status) {
255 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
256 "_STA on %4.4s was not found, assuming device is present\n",
257 acpi_ut_get_node_name(device_node)));
258
259 *flags = ACPI_UINT32_MAX;
260 status = AE_OK;
261 }
262
263 return_ACPI_STATUS(status);
264 }
265
266 /* Extract the status flags */
267
268 *flags = (u32) obj_desc->integer.value;
269
270 /* On exit, we must delete the return object */
271
272 acpi_ut_remove_reference(obj_desc);
273 return_ACPI_STATUS(status);
274}
275
276/*******************************************************************************
277 *
278 * FUNCTION: acpi_ut_execute_power_methods
279 *
280 * PARAMETERS: device_node - Node for the device
281 * method_names - Array of power method names
282 * method_count - Number of methods to execute
283 * out_values - Where the power method values are returned
284 *
285 * RETURN: Status, out_values
286 *
287 * DESCRIPTION: Executes the specified power methods for the device and returns
288 * the result(s).
289 *
290 * NOTE: Internal function, no parameter validation
291 *
292******************************************************************************/
293
294acpi_status
295acpi_ut_execute_power_methods(struct acpi_namespace_node *device_node,
296 const char **method_names,
297 u8 method_count, u8 *out_values)
298{
299 union acpi_operand_object *obj_desc;
300 acpi_status status;
301 acpi_status final_status = AE_NOT_FOUND;
302 u32 i;
303
304 ACPI_FUNCTION_TRACE(ut_execute_power_methods);
305
306 for (i = 0; i < method_count; i++) {
307 /*
308 * Execute the power method (_sx_d or _sx_w). The only allowable
309 * return type is an Integer.
310 */
311 status = acpi_ut_evaluate_object(device_node,
312 ACPI_CAST_PTR(char,
313 method_names[i]),
314 ACPI_BTYPE_INTEGER, &obj_desc);
315 if (ACPI_SUCCESS(status)) {
316 out_values[i] = (u8)obj_desc->integer.value;
317
318 /* Delete the return object */
319
320 acpi_ut_remove_reference(obj_desc);
321 final_status = AE_OK; /* At least one value is valid */
322 continue;
323 }
324
325 out_values[i] = ACPI_UINT8_MAX;
326 if (status == AE_NOT_FOUND) {
327 continue; /* Ignore if not found */
328 }
329
330 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
331 "Failed %s on Device %4.4s, %s\n",
332 ACPI_CAST_PTR(char, method_names[i]),
333 acpi_ut_get_node_name(device_node),
334 acpi_format_exception(status)));
335 }
336
337 return_ACPI_STATUS(final_status);
338}