xref: /openbmc/linux/drivers/acpi/acpica/nsxfname.c (revision 160b8e75)
1 /******************************************************************************
2  *
3  * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4  *                         ACPI Namespace oriented interfaces
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2018, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #define EXPORT_ACPI_INTERFACES
46 
47 #include <acpi/acpi.h>
48 #include "accommon.h"
49 #include "acnamesp.h"
50 #include "acparser.h"
51 #include "amlcode.h"
52 
53 #define _COMPONENT          ACPI_NAMESPACE
54 ACPI_MODULE_NAME("nsxfname")
55 
56 /* Local prototypes */
57 static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
58 				    struct acpi_pnp_device_id *source,
59 				    char *string_area);
60 
61 /******************************************************************************
62  *
63  * FUNCTION:    acpi_get_handle
64  *
65  * PARAMETERS:  parent          - Object to search under (search scope).
66  *              pathname        - Pointer to an asciiz string containing the
67  *                                name
68  *              ret_handle      - Where the return handle is returned
69  *
70  * RETURN:      Status
71  *
72  * DESCRIPTION: This routine will search for a caller specified name in the
73  *              name space. The caller can restrict the search region by
74  *              specifying a non NULL parent. The parent value is itself a
75  *              namespace handle.
76  *
77  ******************************************************************************/
78 
79 acpi_status
80 acpi_get_handle(acpi_handle parent,
81 		acpi_string pathname, acpi_handle *ret_handle)
82 {
83 	acpi_status status;
84 	struct acpi_namespace_node *node = NULL;
85 	struct acpi_namespace_node *prefix_node = NULL;
86 
87 	ACPI_FUNCTION_ENTRY();
88 
89 	/* Parameter Validation */
90 
91 	if (!ret_handle || !pathname) {
92 		return (AE_BAD_PARAMETER);
93 	}
94 
95 	/* Convert a parent handle to a prefix node */
96 
97 	if (parent) {
98 		prefix_node = acpi_ns_validate_handle(parent);
99 		if (!prefix_node) {
100 			return (AE_BAD_PARAMETER);
101 		}
102 	}
103 
104 	/*
105 	 * Valid cases are:
106 	 * 1) Fully qualified pathname
107 	 * 2) Parent + Relative pathname
108 	 *
109 	 * Error for <null Parent + relative path>
110 	 */
111 	if (ACPI_IS_ROOT_PREFIX(pathname[0])) {
112 
113 		/* Pathname is fully qualified (starts with '\') */
114 
115 		/* Special case for root-only, since we can't search for it */
116 
117 		if (!strcmp(pathname, ACPI_NS_ROOT_PATH)) {
118 			*ret_handle =
119 			    ACPI_CAST_PTR(acpi_handle, acpi_gbl_root_node);
120 			return (AE_OK);
121 		}
122 	} else if (!prefix_node) {
123 
124 		/* Relative path with null prefix is disallowed */
125 
126 		return (AE_BAD_PARAMETER);
127 	}
128 
129 	/* Find the Node and convert to a handle */
130 
131 	status =
132 	    acpi_ns_get_node(prefix_node, pathname, ACPI_NS_NO_UPSEARCH, &node);
133 	if (ACPI_SUCCESS(status)) {
134 		*ret_handle = ACPI_CAST_PTR(acpi_handle, node);
135 	}
136 
137 	return (status);
138 }
139 
140 ACPI_EXPORT_SYMBOL(acpi_get_handle)
141 
142 /******************************************************************************
143  *
144  * FUNCTION:    acpi_get_name
145  *
146  * PARAMETERS:  handle          - Handle to be converted to a pathname
147  *              name_type       - Full pathname or single segment
148  *              buffer          - Buffer for returned path
149  *
150  * RETURN:      Pointer to a string containing the fully qualified Name.
151  *
152  * DESCRIPTION: This routine returns the fully qualified name associated with
153  *              the Handle parameter. This and the acpi_pathname_to_handle are
154  *              complementary functions.
155  *
156  ******************************************************************************/
157 acpi_status
158 acpi_get_name(acpi_handle handle, u32 name_type, struct acpi_buffer *buffer)
159 {
160 	acpi_status status;
161 
162 	/* Parameter validation */
163 
164 	if (name_type > ACPI_NAME_TYPE_MAX) {
165 		return (AE_BAD_PARAMETER);
166 	}
167 
168 	status = acpi_ut_validate_buffer(buffer);
169 	if (ACPI_FAILURE(status)) {
170 		return (status);
171 	}
172 
173 	/*
174 	 * Wants the single segment ACPI name.
175 	 * Validate handle and convert to a namespace Node
176 	 */
177 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
178 	if (ACPI_FAILURE(status)) {
179 		return (status);
180 	}
181 
182 	if (name_type == ACPI_FULL_PATHNAME ||
183 	    name_type == ACPI_FULL_PATHNAME_NO_TRAILING) {
184 
185 		/* Get the full pathname (From the namespace root) */
186 
187 		status = acpi_ns_handle_to_pathname(handle, buffer,
188 						    name_type ==
189 						    ACPI_FULL_PATHNAME ? FALSE :
190 						    TRUE);
191 	} else {
192 		/* Get the single name */
193 
194 		status = acpi_ns_handle_to_name(handle, buffer);
195 	}
196 
197 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
198 	return (status);
199 }
200 
201 ACPI_EXPORT_SYMBOL(acpi_get_name)
202 
203 /******************************************************************************
204  *
205  * FUNCTION:    acpi_ns_copy_device_id
206  *
207  * PARAMETERS:  dest                - Pointer to the destination PNP_DEVICE_ID
208  *              source              - Pointer to the source PNP_DEVICE_ID
209  *              string_area         - Pointer to where to copy the dest string
210  *
211  * RETURN:      Pointer to the next string area
212  *
213  * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
214  *
215  ******************************************************************************/
216 static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
217 				    struct acpi_pnp_device_id *source,
218 				    char *string_area)
219 {
220 	/* Create the destination PNP_DEVICE_ID */
221 
222 	dest->string = string_area;
223 	dest->length = source->length;
224 
225 	/* Copy actual string and return a pointer to the next string area */
226 
227 	memcpy(string_area, source->string, source->length);
228 	return (string_area + source->length);
229 }
230 
231 /******************************************************************************
232  *
233  * FUNCTION:    acpi_get_object_info
234  *
235  * PARAMETERS:  handle              - Object Handle
236  *              return_buffer       - Where the info is returned
237  *
238  * RETURN:      Status
239  *
240  * DESCRIPTION: Returns information about an object as gleaned from the
241  *              namespace node and possibly by running several standard
242  *              control methods (Such as in the case of a device.)
243  *
244  * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
245  * _CLS, _ADR, _sx_w, and _sx_d methods.
246  *
247  * Note: Allocates the return buffer, must be freed by the caller.
248  *
249  * Note: This interface is intended to be used during the initial device
250  * discovery namespace traversal. Therefore, no complex methods can be
251  * executed, especially those that access operation regions. Therefore, do
252  * not add any additional methods that could cause problems in this area.
253  * this was the fate of the _SUB method which was found to cause such
254  * problems and was removed (11/2015).
255  *
256  ******************************************************************************/
257 
258 acpi_status
259 acpi_get_object_info(acpi_handle handle,
260 		     struct acpi_device_info **return_buffer)
261 {
262 	struct acpi_namespace_node *node;
263 	struct acpi_device_info *info;
264 	struct acpi_pnp_device_id_list *cid_list = NULL;
265 	struct acpi_pnp_device_id *hid = NULL;
266 	struct acpi_pnp_device_id *uid = NULL;
267 	struct acpi_pnp_device_id *cls = NULL;
268 	char *next_id_string;
269 	acpi_object_type type;
270 	acpi_name name;
271 	u8 param_count = 0;
272 	u16 valid = 0;
273 	u32 info_size;
274 	u32 i;
275 	acpi_status status;
276 
277 	/* Parameter validation */
278 
279 	if (!handle || !return_buffer) {
280 		return (AE_BAD_PARAMETER);
281 	}
282 
283 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
284 	if (ACPI_FAILURE(status)) {
285 		return (status);
286 	}
287 
288 	node = acpi_ns_validate_handle(handle);
289 	if (!node) {
290 		(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
291 		return (AE_BAD_PARAMETER);
292 	}
293 
294 	/* Get the namespace node data while the namespace is locked */
295 
296 	info_size = sizeof(struct acpi_device_info);
297 	type = node->type;
298 	name = node->name.integer;
299 
300 	if (node->type == ACPI_TYPE_METHOD) {
301 		param_count = node->object->method.param_count;
302 	}
303 
304 	status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
305 	if (ACPI_FAILURE(status)) {
306 		return (status);
307 	}
308 
309 	if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
310 		/*
311 		 * Get extra info for ACPI Device/Processor objects only:
312 		 * Run the Device _HID, _UID, _CLS, and _CID methods.
313 		 *
314 		 * Note: none of these methods are required, so they may or may
315 		 * not be present for this device. The Info->Valid bitfield is used
316 		 * to indicate which methods were found and run successfully.
317 		 */
318 
319 		/* Execute the Device._HID method */
320 
321 		status = acpi_ut_execute_HID(node, &hid);
322 		if (ACPI_SUCCESS(status)) {
323 			info_size += hid->length;
324 			valid |= ACPI_VALID_HID;
325 		}
326 
327 		/* Execute the Device._UID method */
328 
329 		status = acpi_ut_execute_UID(node, &uid);
330 		if (ACPI_SUCCESS(status)) {
331 			info_size += uid->length;
332 			valid |= ACPI_VALID_UID;
333 		}
334 
335 		/* Execute the Device._CID method */
336 
337 		status = acpi_ut_execute_CID(node, &cid_list);
338 		if (ACPI_SUCCESS(status)) {
339 
340 			/* Add size of CID strings and CID pointer array */
341 
342 			info_size +=
343 			    (cid_list->list_size -
344 			     sizeof(struct acpi_pnp_device_id_list));
345 			valid |= ACPI_VALID_CID;
346 		}
347 
348 		/* Execute the Device._CLS method */
349 
350 		status = acpi_ut_execute_CLS(node, &cls);
351 		if (ACPI_SUCCESS(status)) {
352 			info_size += cls->length;
353 			valid |= ACPI_VALID_CLS;
354 		}
355 	}
356 
357 	/*
358 	 * Now that we have the variable-length data, we can allocate the
359 	 * return buffer
360 	 */
361 	info = ACPI_ALLOCATE_ZEROED(info_size);
362 	if (!info) {
363 		status = AE_NO_MEMORY;
364 		goto cleanup;
365 	}
366 
367 	/* Get the fixed-length data */
368 
369 	if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
370 		/*
371 		 * Get extra info for ACPI Device/Processor objects only:
372 		 * Run the _STA, _ADR and, sx_w, and _sx_d methods.
373 		 *
374 		 * Notes: none of these methods are required, so they may or may
375 		 * not be present for this device. The Info->Valid bitfield is used
376 		 * to indicate which methods were found and run successfully.
377 		 *
378 		 * For _STA, if the method does not exist, then (as per the ACPI
379 		 * specification), the returned current_status flags will indicate
380 		 * that the device is present/functional/enabled. Otherwise, the
381 		 * current_status flags reflect the value returned from _STA.
382 		 */
383 
384 		/* Execute the Device._STA method */
385 
386 		status = acpi_ut_execute_STA(node, &info->current_status);
387 		if (ACPI_SUCCESS(status)) {
388 			valid |= ACPI_VALID_STA;
389 		}
390 
391 		/* Execute the Device._ADR method */
392 
393 		status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, node,
394 							 &info->address);
395 		if (ACPI_SUCCESS(status)) {
396 			valid |= ACPI_VALID_ADR;
397 		}
398 
399 		/* Execute the Device._sx_w methods */
400 
401 		status = acpi_ut_execute_power_methods(node,
402 						       acpi_gbl_lowest_dstate_names,
403 						       ACPI_NUM_sx_w_METHODS,
404 						       info->lowest_dstates);
405 		if (ACPI_SUCCESS(status)) {
406 			valid |= ACPI_VALID_SXWS;
407 		}
408 
409 		/* Execute the Device._sx_d methods */
410 
411 		status = acpi_ut_execute_power_methods(node,
412 						       acpi_gbl_highest_dstate_names,
413 						       ACPI_NUM_sx_d_METHODS,
414 						       info->highest_dstates);
415 		if (ACPI_SUCCESS(status)) {
416 			valid |= ACPI_VALID_SXDS;
417 		}
418 	}
419 
420 	/*
421 	 * Create a pointer to the string area of the return buffer.
422 	 * Point to the end of the base struct acpi_device_info structure.
423 	 */
424 	next_id_string = ACPI_CAST_PTR(char, info->compatible_id_list.ids);
425 	if (cid_list) {
426 
427 		/* Point past the CID PNP_DEVICE_ID array */
428 
429 		next_id_string +=
430 		    ((acpi_size)cid_list->count *
431 		     sizeof(struct acpi_pnp_device_id));
432 	}
433 
434 	/*
435 	 * Copy the HID, UID, and CIDs to the return buffer. The variable-length
436 	 * strings are copied to the reserved area at the end of the buffer.
437 	 *
438 	 * For HID and CID, check if the ID is a PCI Root Bridge.
439 	 */
440 	if (hid) {
441 		next_id_string = acpi_ns_copy_device_id(&info->hardware_id,
442 							hid, next_id_string);
443 
444 		if (acpi_ut_is_pci_root_bridge(hid->string)) {
445 			info->flags |= ACPI_PCI_ROOT_BRIDGE;
446 		}
447 	}
448 
449 	if (uid) {
450 		next_id_string = acpi_ns_copy_device_id(&info->unique_id,
451 							uid, next_id_string);
452 	}
453 
454 	if (cid_list) {
455 		info->compatible_id_list.count = cid_list->count;
456 		info->compatible_id_list.list_size = cid_list->list_size;
457 
458 		/* Copy each CID */
459 
460 		for (i = 0; i < cid_list->count; i++) {
461 			next_id_string =
462 			    acpi_ns_copy_device_id(&info->compatible_id_list.
463 						   ids[i], &cid_list->ids[i],
464 						   next_id_string);
465 
466 			if (acpi_ut_is_pci_root_bridge(cid_list->ids[i].string)) {
467 				info->flags |= ACPI_PCI_ROOT_BRIDGE;
468 			}
469 		}
470 	}
471 
472 	if (cls) {
473 		next_id_string = acpi_ns_copy_device_id(&info->class_code,
474 							cls, next_id_string);
475 	}
476 
477 	/* Copy the fixed-length data */
478 
479 	info->info_size = info_size;
480 	info->type = type;
481 	info->name = name;
482 	info->param_count = param_count;
483 	info->valid = valid;
484 
485 	*return_buffer = info;
486 	status = AE_OK;
487 
488 cleanup:
489 	if (hid) {
490 		ACPI_FREE(hid);
491 	}
492 	if (uid) {
493 		ACPI_FREE(uid);
494 	}
495 	if (cid_list) {
496 		ACPI_FREE(cid_list);
497 	}
498 	if (cls) {
499 		ACPI_FREE(cls);
500 	}
501 	return (status);
502 }
503 
504 ACPI_EXPORT_SYMBOL(acpi_get_object_info)
505 
506 /******************************************************************************
507  *
508  * FUNCTION:    acpi_install_method
509  *
510  * PARAMETERS:  buffer         - An ACPI table containing one control method
511  *
512  * RETURN:      Status
513  *
514  * DESCRIPTION: Install a control method into the namespace. If the method
515  *              name already exists in the namespace, it is overwritten. The
516  *              input buffer must contain a valid DSDT or SSDT containing a
517  *              single control method.
518  *
519  ******************************************************************************/
520 acpi_status acpi_install_method(u8 *buffer)
521 {
522 	struct acpi_table_header *table =
523 	    ACPI_CAST_PTR(struct acpi_table_header, buffer);
524 	u8 *aml_buffer;
525 	u8 *aml_start;
526 	char *path;
527 	struct acpi_namespace_node *node;
528 	union acpi_operand_object *method_obj;
529 	struct acpi_parse_state parser_state;
530 	u32 aml_length;
531 	u16 opcode;
532 	u8 method_flags;
533 	acpi_status status;
534 
535 	/* Parameter validation */
536 
537 	if (!buffer) {
538 		return (AE_BAD_PARAMETER);
539 	}
540 
541 	/* Table must be a DSDT or SSDT */
542 
543 	if (!ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) &&
544 	    !ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
545 		return (AE_BAD_HEADER);
546 	}
547 
548 	/* First AML opcode in the table must be a control method */
549 
550 	parser_state.aml = buffer + sizeof(struct acpi_table_header);
551 	opcode = acpi_ps_peek_opcode(&parser_state);
552 	if (opcode != AML_METHOD_OP) {
553 		return (AE_BAD_PARAMETER);
554 	}
555 
556 	/* Extract method information from the raw AML */
557 
558 	parser_state.aml += acpi_ps_get_opcode_size(opcode);
559 	parser_state.pkg_end = acpi_ps_get_next_package_end(&parser_state);
560 	path = acpi_ps_get_next_namestring(&parser_state);
561 
562 	method_flags = *parser_state.aml++;
563 	aml_start = parser_state.aml;
564 	aml_length = ACPI_PTR_DIFF(parser_state.pkg_end, aml_start);
565 
566 	/*
567 	 * Allocate resources up-front. We don't want to have to delete a new
568 	 * node from the namespace if we cannot allocate memory.
569 	 */
570 	aml_buffer = ACPI_ALLOCATE(aml_length);
571 	if (!aml_buffer) {
572 		return (AE_NO_MEMORY);
573 	}
574 
575 	method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
576 	if (!method_obj) {
577 		ACPI_FREE(aml_buffer);
578 		return (AE_NO_MEMORY);
579 	}
580 
581 	/* Lock namespace for acpi_ns_lookup, we may be creating a new node */
582 
583 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
584 	if (ACPI_FAILURE(status)) {
585 		goto error_exit;
586 	}
587 
588 	/* The lookup either returns an existing node or creates a new one */
589 
590 	status =
591 	    acpi_ns_lookup(NULL, path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
592 			   ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND,
593 			   NULL, &node);
594 
595 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
596 
597 	if (ACPI_FAILURE(status)) {	/* ns_lookup */
598 		if (status != AE_ALREADY_EXISTS) {
599 			goto error_exit;
600 		}
601 
602 		/* Node existed previously, make sure it is a method node */
603 
604 		if (node->type != ACPI_TYPE_METHOD) {
605 			status = AE_TYPE;
606 			goto error_exit;
607 		}
608 	}
609 
610 	/* Copy the method AML to the local buffer */
611 
612 	memcpy(aml_buffer, aml_start, aml_length);
613 
614 	/* Initialize the method object with the new method's information */
615 
616 	method_obj->method.aml_start = aml_buffer;
617 	method_obj->method.aml_length = aml_length;
618 
619 	method_obj->method.param_count = (u8)
620 	    (method_flags & AML_METHOD_ARG_COUNT);
621 
622 	if (method_flags & AML_METHOD_SERIALIZED) {
623 		method_obj->method.info_flags = ACPI_METHOD_SERIALIZED;
624 
625 		method_obj->method.sync_level = (u8)
626 		    ((method_flags & AML_METHOD_SYNC_LEVEL) >> 4);
627 	}
628 
629 	/*
630 	 * Now that it is complete, we can attach the new method object to
631 	 * the method Node (detaches/deletes any existing object)
632 	 */
633 	status = acpi_ns_attach_object(node, method_obj, ACPI_TYPE_METHOD);
634 
635 	/*
636 	 * Flag indicates AML buffer is dynamic, must be deleted later.
637 	 * Must be set only after attach above.
638 	 */
639 	node->flags |= ANOBJ_ALLOCATED_BUFFER;
640 
641 	/* Remove local reference to the method object */
642 
643 	acpi_ut_remove_reference(method_obj);
644 	return (status);
645 
646 error_exit:
647 
648 	ACPI_FREE(aml_buffer);
649 	ACPI_FREE(method_obj);
650 	return (status);
651 }
652 ACPI_EXPORT_SYMBOL(acpi_install_method)
653