1 // SPDX-License-Identifier: GPL-2.0-only
3 * ACPI device specific properties support.
5 * Copyright (C) 2014, Intel Corporation
8 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
9 * Darren Hart <dvhart@linux.intel.com>
10 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
13 #include <linux/acpi.h>
14 #include <linux/device.h>
15 #include <linux/export.h>
19 static int acpi_data_get_property_array(const struct acpi_device_data *data,
21 acpi_object_type type,
22 const union acpi_object **obj);
25 * The GUIDs here are made equivalent to each other in order to avoid extra
26 * complexity in the properties handling code, with the caveat that the
27 * kernel will accept certain combinations of GUID and properties that are
28 * not defined without a warning. For instance if any of the properties
29 * from different GUID appear in a property list of another, it will be
30 * accepted by the kernel. Firmware validation tools should catch these.
32 static const guid_t prp_guids[] = {
33 /* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
34 GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
35 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
36 /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
37 GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
38 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
39 /* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */
40 GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3,
41 0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89),
42 /* Thunderbolt GUID for IMR_VALID: c44d002f-69f9-4e7d-a904-a7baabdf43f7 */
43 GUID_INIT(0xc44d002f, 0x69f9, 0x4e7d,
44 0xa9, 0x04, 0xa7, 0xba, 0xab, 0xdf, 0x43, 0xf7),
45 /* Thunderbolt GUID for WAKE_SUPPORTED: 6c501103-c189-4296-ba72-9bf5a26ebe5d */
46 GUID_INIT(0x6c501103, 0xc189, 0x4296,
47 0xba, 0x72, 0x9b, 0xf5, 0xa2, 0x6e, 0xbe, 0x5d),
48 /* Storage device needs D3 GUID: 5025030f-842f-4ab4-a561-99a5189762d0 */
49 GUID_INIT(0x5025030f, 0x842f, 0x4ab4,
50 0xa5, 0x61, 0x99, 0xa5, 0x18, 0x97, 0x62, 0xd0),
53 /* ACPI _DSD data subnodes GUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
54 static const guid_t ads_guid =
55 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
56 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
58 /* ACPI _DSD data buffer GUID: edb12dd0-363d-4085-a3d2-49522ca160c4 */
59 static const guid_t buffer_prop_guid =
60 GUID_INIT(0xedb12dd0, 0x363d, 0x4085,
61 0xa3, 0xd2, 0x49, 0x52, 0x2c, 0xa1, 0x60, 0xc4);
63 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
64 union acpi_object *desc,
65 struct acpi_device_data *data,
66 struct fwnode_handle *parent);
67 static bool acpi_extract_properties(acpi_handle handle,
68 union acpi_object *desc,
69 struct acpi_device_data *data);
71 static bool acpi_nondev_subnode_extract(union acpi_object *desc,
73 const union acpi_object *link,
74 struct list_head *list,
75 struct fwnode_handle *parent)
77 struct acpi_data_node *dn;
80 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
84 dn->name = link->package.elements[0].string.pointer;
85 fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops);
87 INIT_LIST_HEAD(&dn->data.properties);
88 INIT_LIST_HEAD(&dn->data.subnodes);
90 result = acpi_extract_properties(handle, desc, &dn->data);
97 * The scope for the subnode object lookup is the one of the
98 * namespace node (device) containing the object that has
99 * returned the package. That is, it's the scope of that
102 status = acpi_get_parent(handle, &scope);
103 if (ACPI_SUCCESS(status)
104 && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
107 } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
114 dn->data.pointer = desc;
115 list_add_tail(&dn->sibling, list);
120 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
124 static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
125 const union acpi_object *link,
126 struct list_head *list,
127 struct fwnode_handle *parent)
129 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
132 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
134 if (ACPI_FAILURE(status))
137 if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
141 ACPI_FREE(buf.pointer);
145 static bool acpi_nondev_subnode_ok(acpi_handle scope,
146 const union acpi_object *link,
147 struct list_head *list,
148 struct fwnode_handle *parent)
156 status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
158 if (ACPI_FAILURE(status))
161 return acpi_nondev_subnode_data_ok(handle, link, list, parent);
164 static bool acpi_add_nondev_subnodes(acpi_handle scope,
165 union acpi_object *links,
166 struct list_head *list,
167 struct fwnode_handle *parent)
172 for (i = 0; i < links->package.count; i++) {
173 union acpi_object *link, *desc;
177 link = &links->package.elements[i];
178 /* Only two elements allowed. */
179 if (link->package.count != 2)
182 /* The first one must be a string. */
183 if (link->package.elements[0].type != ACPI_TYPE_STRING)
186 /* The second one may be a string, a reference or a package. */
187 switch (link->package.elements[1].type) {
188 case ACPI_TYPE_STRING:
189 result = acpi_nondev_subnode_ok(scope, link, list,
192 case ACPI_TYPE_LOCAL_REFERENCE:
193 handle = link->package.elements[1].reference.handle;
194 result = acpi_nondev_subnode_data_ok(handle, link, list,
197 case ACPI_TYPE_PACKAGE:
198 desc = &link->package.elements[1];
199 result = acpi_nondev_subnode_extract(desc, NULL, link,
212 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
213 union acpi_object *desc,
214 struct acpi_device_data *data,
215 struct fwnode_handle *parent)
219 /* Look for the ACPI data subnodes GUID. */
220 for (i = 0; i < desc->package.count; i += 2) {
221 const union acpi_object *guid;
222 union acpi_object *links;
224 guid = &desc->package.elements[i];
225 links = &desc->package.elements[i + 1];
228 * The first element must be a GUID and the second one must be
231 if (guid->type != ACPI_TYPE_BUFFER ||
232 guid->buffer.length != 16 ||
233 links->type != ACPI_TYPE_PACKAGE)
236 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
239 return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
246 static bool acpi_property_value_ok(const union acpi_object *value)
251 * The value must be an integer, a string, a reference, or a package
252 * whose every element must be an integer, a string, or a reference.
254 switch (value->type) {
255 case ACPI_TYPE_INTEGER:
256 case ACPI_TYPE_STRING:
257 case ACPI_TYPE_LOCAL_REFERENCE:
260 case ACPI_TYPE_PACKAGE:
261 for (j = 0; j < value->package.count; j++)
262 switch (value->package.elements[j].type) {
263 case ACPI_TYPE_INTEGER:
264 case ACPI_TYPE_STRING:
265 case ACPI_TYPE_LOCAL_REFERENCE:
277 static bool acpi_properties_format_valid(const union acpi_object *properties)
281 for (i = 0; i < properties->package.count; i++) {
282 const union acpi_object *property;
284 property = &properties->package.elements[i];
286 * Only two elements allowed, the first one must be a string and
287 * the second one has to satisfy certain conditions.
289 if (property->package.count != 2
290 || property->package.elements[0].type != ACPI_TYPE_STRING
291 || !acpi_property_value_ok(&property->package.elements[1]))
297 static void acpi_init_of_compatible(struct acpi_device *adev)
299 const union acpi_object *of_compatible;
302 ret = acpi_data_get_property_array(&adev->data, "compatible",
303 ACPI_TYPE_STRING, &of_compatible);
305 ret = acpi_dev_get_property(adev, "compatible",
306 ACPI_TYPE_STRING, &of_compatible);
308 struct acpi_device *parent;
310 parent = acpi_dev_parent(adev);
311 if (parent && parent->flags.of_compatible_ok)
317 adev->data.of_compatible = of_compatible;
320 adev->flags.of_compatible_ok = 1;
323 static bool acpi_is_property_guid(const guid_t *guid)
327 for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
328 if (guid_equal(guid, &prp_guids[i]))
335 struct acpi_device_properties *
336 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
337 union acpi_object *properties)
339 struct acpi_device_properties *props;
341 props = kzalloc(sizeof(*props), GFP_KERNEL);
343 INIT_LIST_HEAD(&props->list);
345 props->properties = properties;
346 list_add_tail(&props->list, &data->properties);
352 static void acpi_nondev_subnode_tag(acpi_handle handle, void *context)
356 static void acpi_untie_nondev_subnodes(struct acpi_device_data *data)
358 struct acpi_data_node *dn;
360 list_for_each_entry(dn, &data->subnodes, sibling) {
361 acpi_detach_data(dn->handle, acpi_nondev_subnode_tag);
363 acpi_untie_nondev_subnodes(&dn->data);
367 static bool acpi_tie_nondev_subnodes(struct acpi_device_data *data)
369 struct acpi_data_node *dn;
371 list_for_each_entry(dn, &data->subnodes, sibling) {
375 status = acpi_attach_data(dn->handle, acpi_nondev_subnode_tag, dn);
376 if (ACPI_FAILURE(status) && status != AE_ALREADY_EXISTS) {
377 acpi_handle_err(dn->handle, "Can't tag data node\n");
381 ret = acpi_tie_nondev_subnodes(&dn->data);
389 static void acpi_data_add_buffer_props(acpi_handle handle,
390 struct acpi_device_data *data,
391 union acpi_object *properties)
393 struct acpi_device_properties *props;
394 union acpi_object *package;
399 if (check_mul_overflow((size_t)properties->package.count,
400 sizeof(*package) + sizeof(void *),
402 check_add_overflow(sizeof(*props) + sizeof(*package), alloc_size,
404 acpi_handle_warn(handle,
405 "can't allocate memory for %u buffer props",
406 properties->package.count);
410 props = kvzalloc(alloc_size, GFP_KERNEL);
414 props->guid = &buffer_prop_guid;
415 props->bufs = (void *)(props + 1);
416 props->properties = (void *)(props->bufs + properties->package.count);
419 package = props->properties;
420 package->type = ACPI_TYPE_PACKAGE;
421 package->package.elements = package + 1;
422 count = &package->package.count;
428 for (i = 0; i < properties->package.count; i++) {
429 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
430 union acpi_object *property = &properties->package.elements[i];
431 union acpi_object *prop, *obj, *buf_obj;
434 if (property->type != ACPI_TYPE_PACKAGE ||
435 property->package.count != 2) {
436 acpi_handle_warn(handle,
437 "buffer property %u has %u entries\n",
438 i, property->package.count);
442 prop = &property->package.elements[0];
443 obj = &property->package.elements[1];
445 if (prop->type != ACPI_TYPE_STRING ||
446 obj->type != ACPI_TYPE_STRING) {
447 acpi_handle_warn(handle,
448 "wrong object types %u and %u\n",
449 prop->type, obj->type);
453 status = acpi_evaluate_object_typed(handle, obj->string.pointer,
456 if (ACPI_FAILURE(status)) {
457 acpi_handle_warn(handle,
458 "can't evaluate \"%*pE\" as buffer\n",
460 obj->string.pointer);
464 package->type = ACPI_TYPE_PACKAGE;
465 package->package.elements = prop;
466 package->package.count = 2;
468 buf_obj = buf.pointer;
470 /* Replace the string object with a buffer object */
471 obj->type = ACPI_TYPE_BUFFER;
472 obj->buffer.length = buf_obj->buffer.length;
473 obj->buffer.pointer = buf_obj->buffer.pointer;
475 props->bufs[i] = buf.pointer;
481 list_add(&props->list, &data->properties);
486 static bool acpi_extract_properties(acpi_handle scope, union acpi_object *desc,
487 struct acpi_device_data *data)
491 if (desc->package.count % 2)
494 /* Look for the device properties GUID. */
495 for (i = 0; i < desc->package.count; i += 2) {
496 const union acpi_object *guid;
497 union acpi_object *properties;
499 guid = &desc->package.elements[i];
500 properties = &desc->package.elements[i + 1];
503 * The first element must be a GUID and the second one must be
506 if (guid->type != ACPI_TYPE_BUFFER ||
507 guid->buffer.length != 16 ||
508 properties->type != ACPI_TYPE_PACKAGE)
511 if (guid_equal((guid_t *)guid->buffer.pointer,
512 &buffer_prop_guid)) {
513 acpi_data_add_buffer_props(scope, data, properties);
517 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
521 * We found the matching GUID. Now validate the format of the
522 * package immediately following it.
524 if (!acpi_properties_format_valid(properties))
527 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
531 return !list_empty(&data->properties);
534 void acpi_init_properties(struct acpi_device *adev)
536 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
537 struct acpi_hardware_id *hwid;
539 bool acpi_of = false;
541 INIT_LIST_HEAD(&adev->data.properties);
542 INIT_LIST_HEAD(&adev->data.subnodes);
548 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
549 * Device Tree compatible properties for this device.
551 list_for_each_entry(hwid, &adev->pnp.ids, list) {
552 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
558 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
560 if (ACPI_FAILURE(status))
563 if (acpi_extract_properties(adev->handle, buf.pointer, &adev->data)) {
564 adev->data.pointer = buf.pointer;
566 acpi_init_of_compatible(adev);
568 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
569 &adev->data, acpi_fwnode_handle(adev)))
570 adev->data.pointer = buf.pointer;
572 if (!adev->data.pointer) {
573 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
574 ACPI_FREE(buf.pointer);
576 if (!acpi_tie_nondev_subnodes(&adev->data))
577 acpi_untie_nondev_subnodes(&adev->data);
581 if (acpi_of && !adev->flags.of_compatible_ok)
582 acpi_handle_info(adev->handle,
583 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
585 if (!adev->data.pointer)
586 acpi_extract_apple_properties(adev);
589 static void acpi_free_device_properties(struct list_head *list)
591 struct acpi_device_properties *props, *tmp;
593 list_for_each_entry_safe(props, tmp, list, list) {
596 list_del(&props->list);
597 /* Buffer data properties were separately allocated */
599 for (i = 0; i < props->properties->package.count; i++)
600 ACPI_FREE(props->bufs[i]);
605 static void acpi_destroy_nondev_subnodes(struct list_head *list)
607 struct acpi_data_node *dn, *next;
609 if (list_empty(list))
612 list_for_each_entry_safe_reverse(dn, next, list, sibling) {
613 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
614 wait_for_completion(&dn->kobj_done);
615 list_del(&dn->sibling);
616 ACPI_FREE((void *)dn->data.pointer);
617 acpi_free_device_properties(&dn->data.properties);
622 void acpi_free_properties(struct acpi_device *adev)
624 acpi_untie_nondev_subnodes(&adev->data);
625 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
626 ACPI_FREE((void *)adev->data.pointer);
627 adev->data.of_compatible = NULL;
628 adev->data.pointer = NULL;
629 acpi_free_device_properties(&adev->data.properties);
633 * acpi_data_get_property - return an ACPI property with given name
634 * @data: ACPI device deta object to get the property from
635 * @name: Name of the property
636 * @type: Expected property type
637 * @obj: Location to store the property value (if not %NULL)
639 * Look up a property with @name and store a pointer to the resulting ACPI
640 * object at the location pointed to by @obj if found.
642 * Callers must not attempt to free the returned objects. These objects will be
643 * freed by the ACPI core automatically during the removal of @data.
645 * Return: %0 if property with @name has been found (success),
646 * %-EINVAL if the arguments are invalid,
647 * %-EINVAL if the property doesn't exist,
648 * %-EPROTO if the property value type doesn't match @type.
650 static int acpi_data_get_property(const struct acpi_device_data *data,
651 const char *name, acpi_object_type type,
652 const union acpi_object **obj)
654 const struct acpi_device_properties *props;
659 if (!data->pointer || list_empty(&data->properties))
662 list_for_each_entry(props, &data->properties, list) {
663 const union acpi_object *properties;
666 properties = props->properties;
667 for (i = 0; i < properties->package.count; i++) {
668 const union acpi_object *propname, *propvalue;
669 const union acpi_object *property;
671 property = &properties->package.elements[i];
673 propname = &property->package.elements[0];
674 propvalue = &property->package.elements[1];
676 if (!strcmp(name, propname->string.pointer)) {
677 if (type != ACPI_TYPE_ANY &&
678 propvalue->type != type)
691 * acpi_dev_get_property - return an ACPI property with given name.
692 * @adev: ACPI device to get the property from.
693 * @name: Name of the property.
694 * @type: Expected property type.
695 * @obj: Location to store the property value (if not %NULL).
697 int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
698 acpi_object_type type, const union acpi_object **obj)
700 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
702 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
704 static const struct acpi_device_data *
705 acpi_device_data_of_node(const struct fwnode_handle *fwnode)
707 if (is_acpi_device_node(fwnode)) {
708 const struct acpi_device *adev = to_acpi_device_node(fwnode);
711 if (is_acpi_data_node(fwnode)) {
712 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
719 * acpi_node_prop_get - return an ACPI property with given name.
720 * @fwnode: Firmware node to get the property from.
721 * @propname: Name of the property.
722 * @valptr: Location to store a pointer to the property value (if not %NULL).
724 int acpi_node_prop_get(const struct fwnode_handle *fwnode,
725 const char *propname, void **valptr)
727 return acpi_data_get_property(acpi_device_data_of_node(fwnode),
728 propname, ACPI_TYPE_ANY,
729 (const union acpi_object **)valptr);
733 * acpi_data_get_property_array - return an ACPI array property with given name
734 * @data: ACPI data object to get the property from
735 * @name: Name of the property
736 * @type: Expected type of array elements
737 * @obj: Location to store a pointer to the property value (if not NULL)
739 * Look up an array property with @name and store a pointer to the resulting
740 * ACPI object at the location pointed to by @obj if found.
742 * Callers must not attempt to free the returned objects. Those objects will be
743 * freed by the ACPI core automatically during the removal of @data.
745 * Return: %0 if array property (package) with @name has been found (success),
746 * %-EINVAL if the arguments are invalid,
747 * %-EINVAL if the property doesn't exist,
748 * %-EPROTO if the property is not a package or the type of its elements
749 * doesn't match @type.
751 static int acpi_data_get_property_array(const struct acpi_device_data *data,
753 acpi_object_type type,
754 const union acpi_object **obj)
756 const union acpi_object *prop;
759 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
763 if (type != ACPI_TYPE_ANY) {
764 /* Check that all elements are of correct type. */
765 for (i = 0; i < prop->package.count; i++)
766 if (prop->package.elements[i].type != type)
775 static struct fwnode_handle *
776 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
777 const char *childname)
779 struct fwnode_handle *child;
781 fwnode_for_each_child_node(fwnode, child) {
782 if (is_acpi_data_node(child)) {
783 if (acpi_data_node_match(child, childname))
788 if (!strncmp(acpi_device_bid(to_acpi_device_node(child)),
789 childname, ACPI_NAMESEG_SIZE))
796 static int acpi_get_ref_args(struct fwnode_reference_args *args,
797 struct fwnode_handle *ref_fwnode,
798 const union acpi_object **element,
799 const union acpi_object *end, size_t num_args)
804 * Find the referred data extension node under the
805 * referred device node.
807 for (; *element < end && (*element)->type == ACPI_TYPE_STRING;
809 const char *child_name = (*element)->string.pointer;
811 ref_fwnode = acpi_fwnode_get_named_child_node(ref_fwnode, child_name);
817 * Assume the following integer elements are all args. Stop counting on
818 * the first reference or end of the package arguments. In case of
819 * neither reference, nor integer, return an error, we can't parse it.
821 for (i = 0; (*element) + i < end && i < num_args; i++) {
822 acpi_object_type type = (*element)[i].type;
824 if (type == ACPI_TYPE_LOCAL_REFERENCE)
827 if (type == ACPI_TYPE_INTEGER)
833 if (nargs > NR_FWNODE_REFERENCE_ARGS)
837 args->fwnode = ref_fwnode;
839 for (i = 0; i < nargs; i++)
840 args->args[i] = (*element)[i].integer.value;
849 * __acpi_node_get_property_reference - returns handle to the referenced object
850 * @fwnode: Firmware node to get the property from
851 * @propname: Name of the property
852 * @index: Index of the reference to return
853 * @num_args: Maximum number of arguments after each reference
854 * @args: Location to store the returned reference with optional arguments
857 * Find property with @name, verifify that it is a package containing at least
858 * one object reference and if so, store the ACPI device object pointer to the
859 * target object in @args->adev. If the reference includes arguments, store
860 * them in the @args->args[] array.
862 * If there's more than one reference in the property value package, @index is
863 * used to select the one to return.
865 * It is possible to leave holes in the property value set like in the
878 * Calling this function with index %2 or index %3 return %-ENOENT. If the
879 * property does not contain any more values %-ENOENT is returned. The NULL
880 * entry must be single integer and preferably contain value %0.
882 * Return: %0 on success, negative error code on failure.
884 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
885 const char *propname, size_t index, size_t num_args,
886 struct fwnode_reference_args *args)
888 const union acpi_object *element, *end;
889 const union acpi_object *obj;
890 const struct acpi_device_data *data;
891 struct acpi_device *device;
894 data = acpi_device_data_of_node(fwnode);
898 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
900 return ret == -EINVAL ? -ENOENT : -EINVAL;
903 case ACPI_TYPE_LOCAL_REFERENCE:
904 /* Plain single reference without arguments. */
908 device = acpi_fetch_acpi_dev(obj->reference.handle);
915 args->fwnode = acpi_fwnode_handle(device);
918 case ACPI_TYPE_PACKAGE:
920 * If it is not a single reference, then it is a package of
921 * references followed by number of ints as follows:
923 * Package () { REF, INT, REF, INT, INT }
925 * The index argument is then used to determine which reference
926 * the caller wants (along with the arguments).
933 if (index >= obj->package.count)
936 element = obj->package.elements;
937 end = element + obj->package.count;
939 while (element < end) {
940 switch (element->type) {
941 case ACPI_TYPE_LOCAL_REFERENCE:
942 device = acpi_fetch_acpi_dev(element->reference.handle);
948 ret = acpi_get_ref_args(idx == index ? args : NULL,
949 acpi_fwnode_handle(device),
950 &element, end, num_args);
958 case ACPI_TYPE_INTEGER:
972 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
974 static int acpi_data_prop_read_single(const struct acpi_device_data *data,
975 const char *propname,
976 enum dev_prop_type proptype, void *val)
978 const union acpi_object *obj;
981 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
982 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
983 else if (proptype == DEV_PROP_STRING)
984 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
990 if (obj->integer.value > U8_MAX)
993 *(u8 *)val = obj->integer.value;
996 if (obj->integer.value > U16_MAX)
999 *(u16 *)val = obj->integer.value;
1002 if (obj->integer.value > U32_MAX)
1005 *(u32 *)val = obj->integer.value;
1009 *(u64 *)val = obj->integer.value;
1011 case DEV_PROP_STRING:
1013 *(char **)val = obj->string.pointer;
1019 /* When no storage provided return number of available values */
1023 #define acpi_copy_property_array_uint(items, val, nval) \
1025 typeof(items) __items = items; \
1026 typeof(val) __val = val; \
1027 typeof(nval) __nval = nval; \
1031 for (i = 0; i < __nval; i++) { \
1032 if (__items->type == ACPI_TYPE_BUFFER) { \
1033 __val[i] = __items->buffer.pointer[i]; \
1036 if (__items[i].type != ACPI_TYPE_INTEGER) { \
1040 if (__items[i].integer.value > _Generic(__val, \
1044 u64 *: U64_MAX)) { \
1049 __val[i] = __items[i].integer.value; \
1054 static int acpi_copy_property_array_string(const union acpi_object *items,
1055 char **val, size_t nval)
1059 for (i = 0; i < nval; i++) {
1060 if (items[i].type != ACPI_TYPE_STRING)
1063 val[i] = items[i].string.pointer;
1068 static int acpi_data_prop_read(const struct acpi_device_data *data,
1069 const char *propname,
1070 enum dev_prop_type proptype,
1071 void *val, size_t nval)
1073 const union acpi_object *obj;
1074 const union acpi_object *items;
1077 if (nval == 1 || !val) {
1078 ret = acpi_data_prop_read_single(data, propname, proptype, val);
1080 * The overflow error means that the property is there and it is
1081 * single-value, but its type does not match, so return.
1083 if (ret >= 0 || ret == -EOVERFLOW)
1087 * Reading this property as a single-value one failed, but its
1088 * value may still be represented as one-element array, so
1093 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
1094 if (ret && proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
1095 ret = acpi_data_get_property(data, propname, ACPI_TYPE_BUFFER,
1101 if (obj->type == ACPI_TYPE_BUFFER)
1102 return obj->buffer.length;
1104 return obj->package.count;
1108 case DEV_PROP_STRING:
1111 if (obj->type == ACPI_TYPE_BUFFER) {
1112 if (nval > obj->buffer.length)
1115 if (nval > obj->package.count)
1123 if (obj->type == ACPI_TYPE_BUFFER) {
1124 if (proptype != DEV_PROP_U8)
1128 items = obj->package.elements;
1133 ret = acpi_copy_property_array_uint(items, (u8 *)val, nval);
1136 ret = acpi_copy_property_array_uint(items, (u16 *)val, nval);
1139 ret = acpi_copy_property_array_uint(items, (u32 *)val, nval);
1142 ret = acpi_copy_property_array_uint(items, (u64 *)val, nval);
1144 case DEV_PROP_STRING:
1145 ret = acpi_copy_property_array_string(
1146 items, (char **)val,
1147 min_t(u32, nval, obj->package.count));
1157 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
1158 * @fwnode: Firmware node to get the property from.
1159 * @propname: Name of the property.
1160 * @proptype: Expected property type.
1161 * @val: Location to store the property value (if not %NULL).
1162 * @nval: Size of the array pointed to by @val.
1164 * If @val is %NULL, return the number of array elements comprising the value
1165 * of the property. Otherwise, read at most @nval values to the array at the
1166 * location pointed to by @val.
1168 static int acpi_node_prop_read(const struct fwnode_handle *fwnode,
1169 const char *propname, enum dev_prop_type proptype,
1170 void *val, size_t nval)
1172 return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
1173 propname, proptype, val, nval);
1176 static int stop_on_next(struct acpi_device *adev, void *data)
1178 struct acpi_device **ret_p = data;
1185 /* Skip until the "previous" object is found. */
1193 * acpi_get_next_subnode - Return the next child node handle for a fwnode
1194 * @fwnode: Firmware node to find the next child node for.
1195 * @child: Handle to one of the device's child nodes or a null handle.
1197 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
1198 struct fwnode_handle *child)
1200 struct acpi_device *adev = to_acpi_device_node(fwnode);
1202 if ((!child || is_acpi_device_node(child)) && adev) {
1203 struct acpi_device *child_adev = to_acpi_device_node(child);
1205 acpi_dev_for_each_child(adev, stop_on_next, &child_adev);
1207 return acpi_fwnode_handle(child_adev);
1212 if (!child || is_acpi_data_node(child)) {
1213 const struct acpi_data_node *data = to_acpi_data_node(fwnode);
1214 const struct list_head *head;
1215 struct list_head *next;
1216 struct acpi_data_node *dn;
1219 * We can have a combination of device and data nodes, e.g. with
1220 * hierarchical _DSD properties. Make sure the adev pointer is
1221 * restored before going through data nodes, otherwise we will
1222 * be looking for data_nodes below the last device found instead
1223 * of the common fwnode shared by device_nodes and data_nodes.
1225 adev = to_acpi_device_node(fwnode);
1227 head = &adev->data.subnodes;
1229 head = &data->data.subnodes;
1233 if (list_empty(head))
1237 dn = to_acpi_data_node(child);
1238 next = dn->sibling.next;
1242 dn = list_entry(next, struct acpi_data_node, sibling);
1244 dn = list_first_entry(head, struct acpi_data_node, sibling);
1252 * acpi_node_get_parent - Return parent fwnode of this fwnode
1253 * @fwnode: Firmware node whose parent to get
1255 * Returns parent node of an ACPI device or data firmware node or %NULL if
1258 static struct fwnode_handle *
1259 acpi_node_get_parent(const struct fwnode_handle *fwnode)
1261 if (is_acpi_data_node(fwnode)) {
1262 /* All data nodes have parent pointer so just return that */
1263 return to_acpi_data_node(fwnode)->parent;
1265 if (is_acpi_device_node(fwnode)) {
1266 struct acpi_device *parent;
1268 parent = acpi_dev_parent(to_acpi_device_node(fwnode));
1270 return acpi_fwnode_handle(parent);
1277 * Return true if the node is an ACPI graph node. Called on either ports
1280 static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1283 unsigned int len = strlen(str);
1286 if (!len || !is_acpi_data_node(fwnode))
1289 name = to_acpi_data_node(fwnode)->name;
1291 return (fwnode_property_present(fwnode, "reg") &&
1292 !strncmp(name, str, len) && name[len] == '@') ||
1293 fwnode_property_present(fwnode, str);
1297 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1298 * @fwnode: Pointer to the parent firmware node
1299 * @prev: Previous endpoint node or %NULL to get the first
1301 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1302 * %NULL if there is no next endpoint or in case of error. In case of success
1303 * the next endpoint is returned.
1305 static struct fwnode_handle *acpi_graph_get_next_endpoint(
1306 const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1308 struct fwnode_handle *port = NULL;
1309 struct fwnode_handle *endpoint;
1313 port = fwnode_get_next_child_node(fwnode, port);
1315 * The names of the port nodes begin with "port@"
1316 * followed by the number of the port node and they also
1317 * have a "reg" property that also has the number of the
1318 * port node. For compatibility reasons a node is also
1319 * recognised as a port node from the "port" property.
1321 if (is_acpi_graph_node(port, "port"))
1325 port = fwnode_get_parent(prev);
1331 endpoint = fwnode_get_next_child_node(port, prev);
1333 port = fwnode_get_next_child_node(fwnode, port);
1336 if (is_acpi_graph_node(port, "port"))
1337 endpoint = fwnode_get_next_child_node(port, NULL);
1341 * The names of the endpoint nodes begin with "endpoint@" followed by
1342 * the number of the endpoint node and they also have a "reg" property
1343 * that also has the number of the endpoint node. For compatibility
1344 * reasons a node is also recognised as an endpoint node from the
1345 * "endpoint" property.
1347 if (!is_acpi_graph_node(endpoint, "endpoint"))
1354 * acpi_graph_get_child_prop_value - Return a child with a given property value
1355 * @fwnode: device fwnode
1356 * @prop_name: The name of the property to look for
1357 * @val: the desired property value
1359 * Return the port node corresponding to a given port number. Returns
1360 * the child node on success, NULL otherwise.
1362 static struct fwnode_handle *acpi_graph_get_child_prop_value(
1363 const struct fwnode_handle *fwnode, const char *prop_name,
1366 struct fwnode_handle *child;
1368 fwnode_for_each_child_node(fwnode, child) {
1371 if (fwnode_property_read_u32(child, prop_name, &nr))
1383 * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint
1384 * @__fwnode: Endpoint firmware node pointing to a remote device
1386 * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1388 static struct fwnode_handle *
1389 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1391 struct fwnode_handle *fwnode;
1392 unsigned int port_nr, endpoint_nr;
1393 struct fwnode_reference_args args;
1396 memset(&args, 0, sizeof(args));
1397 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1402 /* Direct endpoint reference? */
1403 if (!is_acpi_device_node(args.fwnode))
1404 return args.nargs ? NULL : args.fwnode;
1407 * Always require two arguments with the reference: port and
1410 if (args.nargs != 2)
1413 fwnode = args.fwnode;
1414 port_nr = args.args[0];
1415 endpoint_nr = args.args[1];
1417 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1419 return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1422 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1424 if (!is_acpi_device_node(fwnode))
1427 return acpi_device_is_present(to_acpi_device_node(fwnode));
1431 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1432 const struct device *dev)
1434 return acpi_device_get_match_data(dev);
1437 static bool acpi_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
1439 return acpi_dma_supported(to_acpi_device_node(fwnode));
1442 static enum dev_dma_attr
1443 acpi_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
1445 return acpi_get_dma_attr(to_acpi_device_node(fwnode));
1448 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1449 const char *propname)
1451 return !acpi_node_prop_get(fwnode, propname, NULL);
1455 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1456 const char *propname,
1457 unsigned int elem_size, void *val,
1460 enum dev_prop_type type;
1462 switch (elem_size) {
1467 type = DEV_PROP_U16;
1470 type = DEV_PROP_U32;
1473 type = DEV_PROP_U64;
1479 return acpi_node_prop_read(fwnode, propname, type, val, nval);
1483 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1484 const char *propname, const char **val,
1487 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1492 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1493 const char *prop, const char *nargs_prop,
1494 unsigned int args_count, unsigned int index,
1495 struct fwnode_reference_args *args)
1497 return __acpi_node_get_property_reference(fwnode, prop, index,
1501 static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
1503 const struct acpi_device *adev;
1504 struct fwnode_handle *parent;
1506 /* Is this the root node? */
1507 parent = fwnode_get_parent(fwnode);
1511 fwnode_handle_put(parent);
1513 if (is_acpi_data_node(fwnode)) {
1514 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
1519 adev = to_acpi_device_node(fwnode);
1523 return acpi_device_bid(adev);
1527 acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1529 struct fwnode_handle *parent;
1531 /* Is this the root node? */
1532 parent = fwnode_get_parent(fwnode);
1536 /* Is this 2nd node from the root? */
1537 parent = fwnode_get_next_parent(parent);
1541 fwnode_handle_put(parent);
1543 /* ACPI device or data node. */
1547 static struct fwnode_handle *
1548 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1550 return acpi_node_get_parent(fwnode);
1553 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1554 struct fwnode_endpoint *endpoint)
1556 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1558 endpoint->local_fwnode = fwnode;
1560 if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
1561 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1562 if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
1563 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1568 static int acpi_fwnode_irq_get(const struct fwnode_handle *fwnode,
1571 struct resource res;
1574 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1581 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1582 const struct fwnode_operations ops = { \
1583 .device_is_available = acpi_fwnode_device_is_available, \
1584 .device_get_match_data = acpi_fwnode_device_get_match_data, \
1585 .device_dma_supported = \
1586 acpi_fwnode_device_dma_supported, \
1587 .device_get_dma_attr = acpi_fwnode_device_get_dma_attr, \
1588 .property_present = acpi_fwnode_property_present, \
1589 .property_read_int_array = \
1590 acpi_fwnode_property_read_int_array, \
1591 .property_read_string_array = \
1592 acpi_fwnode_property_read_string_array, \
1593 .get_parent = acpi_node_get_parent, \
1594 .get_next_child_node = acpi_get_next_subnode, \
1595 .get_named_child_node = acpi_fwnode_get_named_child_node, \
1596 .get_name = acpi_fwnode_get_name, \
1597 .get_name_prefix = acpi_fwnode_get_name_prefix, \
1598 .get_reference_args = acpi_fwnode_get_reference_args, \
1599 .graph_get_next_endpoint = \
1600 acpi_graph_get_next_endpoint, \
1601 .graph_get_remote_endpoint = \
1602 acpi_graph_get_remote_endpoint, \
1603 .graph_get_port_parent = acpi_fwnode_get_parent, \
1604 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1605 .irq_get = acpi_fwnode_irq_get, \
1607 EXPORT_SYMBOL_GPL(ops)
1609 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1610 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1611 const struct fwnode_operations acpi_static_fwnode_ops;
1613 bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1615 return !IS_ERR_OR_NULL(fwnode) &&
1616 fwnode->ops == &acpi_device_fwnode_ops;
1618 EXPORT_SYMBOL(is_acpi_device_node);
1620 bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1622 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1624 EXPORT_SYMBOL(is_acpi_data_node);