1 // SPDX-License-Identifier: GPL-2.0-only
3 * ACPI device specific properties support.
5 * Copyright (C) 2014 - 2023, 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>
11 * Sakari Ailus <sakari.ailus@linux.intel.com>
14 #define pr_fmt(fmt) "ACPI: " fmt
16 #include <linux/acpi.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
22 static int acpi_data_get_property_array(const struct acpi_device_data *data,
24 acpi_object_type type,
25 const union acpi_object **obj);
28 * The GUIDs here are made equivalent to each other in order to avoid extra
29 * complexity in the properties handling code, with the caveat that the
30 * kernel will accept certain combinations of GUID and properties that are
31 * not defined without a warning. For instance if any of the properties
32 * from different GUID appear in a property list of another, it will be
33 * accepted by the kernel. Firmware validation tools should catch these.
35 static const guid_t prp_guids[] = {
36 /* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
37 GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
38 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
39 /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
40 GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
41 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
42 /* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */
43 GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3,
44 0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89),
45 /* Thunderbolt GUID for IMR_VALID: c44d002f-69f9-4e7d-a904-a7baabdf43f7 */
46 GUID_INIT(0xc44d002f, 0x69f9, 0x4e7d,
47 0xa9, 0x04, 0xa7, 0xba, 0xab, 0xdf, 0x43, 0xf7),
48 /* Thunderbolt GUID for WAKE_SUPPORTED: 6c501103-c189-4296-ba72-9bf5a26ebe5d */
49 GUID_INIT(0x6c501103, 0xc189, 0x4296,
50 0xba, 0x72, 0x9b, 0xf5, 0xa2, 0x6e, 0xbe, 0x5d),
51 /* Storage device needs D3 GUID: 5025030f-842f-4ab4-a561-99a5189762d0 */
52 GUID_INIT(0x5025030f, 0x842f, 0x4ab4,
53 0xa5, 0x61, 0x99, 0xa5, 0x18, 0x97, 0x62, 0xd0),
56 /* ACPI _DSD data subnodes GUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
57 static const guid_t ads_guid =
58 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
59 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
61 /* ACPI _DSD data buffer GUID: edb12dd0-363d-4085-a3d2-49522ca160c4 */
62 static const guid_t buffer_prop_guid =
63 GUID_INIT(0xedb12dd0, 0x363d, 0x4085,
64 0xa3, 0xd2, 0x49, 0x52, 0x2c, 0xa1, 0x60, 0xc4);
66 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
67 union acpi_object *desc,
68 struct acpi_device_data *data,
69 struct fwnode_handle *parent);
70 static bool acpi_extract_properties(acpi_handle handle,
71 union acpi_object *desc,
72 struct acpi_device_data *data);
74 static bool acpi_nondev_subnode_extract(union acpi_object *desc,
76 const union acpi_object *link,
77 struct list_head *list,
78 struct fwnode_handle *parent)
80 struct acpi_data_node *dn;
83 if (acpi_graph_ignore_port(handle))
86 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
90 dn->name = link->package.elements[0].string.pointer;
91 fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops);
93 INIT_LIST_HEAD(&dn->data.properties);
94 INIT_LIST_HEAD(&dn->data.subnodes);
96 result = acpi_extract_properties(handle, desc, &dn->data);
103 * The scope for the subnode object lookup is the one of the
104 * namespace node (device) containing the object that has
105 * returned the package. That is, it's the scope of that
108 status = acpi_get_parent(handle, &scope);
109 if (ACPI_SUCCESS(status)
110 && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
113 } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
120 dn->data.pointer = desc;
121 list_add_tail(&dn->sibling, list);
126 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
130 static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
131 const union acpi_object *link,
132 struct list_head *list,
133 struct fwnode_handle *parent)
135 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
138 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
140 if (ACPI_FAILURE(status))
143 if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
147 ACPI_FREE(buf.pointer);
151 static bool acpi_nondev_subnode_ok(acpi_handle scope,
152 const union acpi_object *link,
153 struct list_head *list,
154 struct fwnode_handle *parent)
162 status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
164 if (ACPI_FAILURE(status))
167 return acpi_nondev_subnode_data_ok(handle, link, list, parent);
170 static bool acpi_add_nondev_subnodes(acpi_handle scope,
171 union acpi_object *links,
172 struct list_head *list,
173 struct fwnode_handle *parent)
178 for (i = 0; i < links->package.count; i++) {
179 union acpi_object *link, *desc;
183 link = &links->package.elements[i];
184 /* Only two elements allowed. */
185 if (link->package.count != 2)
188 /* The first one must be a string. */
189 if (link->package.elements[0].type != ACPI_TYPE_STRING)
192 /* The second one may be a string, a reference or a package. */
193 switch (link->package.elements[1].type) {
194 case ACPI_TYPE_STRING:
195 result = acpi_nondev_subnode_ok(scope, link, list,
198 case ACPI_TYPE_LOCAL_REFERENCE:
199 handle = link->package.elements[1].reference.handle;
200 result = acpi_nondev_subnode_data_ok(handle, link, list,
203 case ACPI_TYPE_PACKAGE:
204 desc = &link->package.elements[1];
205 result = acpi_nondev_subnode_extract(desc, NULL, link,
218 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
219 union acpi_object *desc,
220 struct acpi_device_data *data,
221 struct fwnode_handle *parent)
225 /* Look for the ACPI data subnodes GUID. */
226 for (i = 0; i < desc->package.count; i += 2) {
227 const union acpi_object *guid;
228 union acpi_object *links;
230 guid = &desc->package.elements[i];
231 links = &desc->package.elements[i + 1];
234 * The first element must be a GUID and the second one must be
237 if (guid->type != ACPI_TYPE_BUFFER ||
238 guid->buffer.length != 16 ||
239 links->type != ACPI_TYPE_PACKAGE)
242 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
245 return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
252 static bool acpi_property_value_ok(const union acpi_object *value)
257 * The value must be an integer, a string, a reference, or a package
258 * whose every element must be an integer, a string, or a reference.
260 switch (value->type) {
261 case ACPI_TYPE_INTEGER:
262 case ACPI_TYPE_STRING:
263 case ACPI_TYPE_LOCAL_REFERENCE:
266 case ACPI_TYPE_PACKAGE:
267 for (j = 0; j < value->package.count; j++)
268 switch (value->package.elements[j].type) {
269 case ACPI_TYPE_INTEGER:
270 case ACPI_TYPE_STRING:
271 case ACPI_TYPE_LOCAL_REFERENCE:
283 static bool acpi_properties_format_valid(const union acpi_object *properties)
287 for (i = 0; i < properties->package.count; i++) {
288 const union acpi_object *property;
290 property = &properties->package.elements[i];
292 * Only two elements allowed, the first one must be a string and
293 * the second one has to satisfy certain conditions.
295 if (property->package.count != 2
296 || property->package.elements[0].type != ACPI_TYPE_STRING
297 || !acpi_property_value_ok(&property->package.elements[1]))
303 static void acpi_init_of_compatible(struct acpi_device *adev)
305 const union acpi_object *of_compatible;
308 ret = acpi_data_get_property_array(&adev->data, "compatible",
309 ACPI_TYPE_STRING, &of_compatible);
311 ret = acpi_dev_get_property(adev, "compatible",
312 ACPI_TYPE_STRING, &of_compatible);
314 struct acpi_device *parent;
316 parent = acpi_dev_parent(adev);
317 if (parent && parent->flags.of_compatible_ok)
323 adev->data.of_compatible = of_compatible;
326 adev->flags.of_compatible_ok = 1;
329 static bool acpi_is_property_guid(const guid_t *guid)
333 for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
334 if (guid_equal(guid, &prp_guids[i]))
341 struct acpi_device_properties *
342 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
343 union acpi_object *properties)
345 struct acpi_device_properties *props;
347 props = kzalloc(sizeof(*props), GFP_KERNEL);
349 INIT_LIST_HEAD(&props->list);
351 props->properties = properties;
352 list_add_tail(&props->list, &data->properties);
358 static void acpi_nondev_subnode_tag(acpi_handle handle, void *context)
362 static void acpi_untie_nondev_subnodes(struct acpi_device_data *data)
364 struct acpi_data_node *dn;
366 list_for_each_entry(dn, &data->subnodes, sibling) {
367 acpi_detach_data(dn->handle, acpi_nondev_subnode_tag);
369 acpi_untie_nondev_subnodes(&dn->data);
373 static bool acpi_tie_nondev_subnodes(struct acpi_device_data *data)
375 struct acpi_data_node *dn;
377 list_for_each_entry(dn, &data->subnodes, sibling) {
381 status = acpi_attach_data(dn->handle, acpi_nondev_subnode_tag, dn);
382 if (ACPI_FAILURE(status) && status != AE_ALREADY_EXISTS) {
383 acpi_handle_err(dn->handle, "Can't tag data node\n");
387 ret = acpi_tie_nondev_subnodes(&dn->data);
395 static void acpi_data_add_buffer_props(acpi_handle handle,
396 struct acpi_device_data *data,
397 union acpi_object *properties)
399 struct acpi_device_properties *props;
400 union acpi_object *package;
405 if (check_mul_overflow((size_t)properties->package.count,
406 sizeof(*package) + sizeof(void *),
408 check_add_overflow(sizeof(*props) + sizeof(*package), alloc_size,
410 acpi_handle_warn(handle,
411 "can't allocate memory for %u buffer props",
412 properties->package.count);
416 props = kvzalloc(alloc_size, GFP_KERNEL);
420 props->guid = &buffer_prop_guid;
421 props->bufs = (void *)(props + 1);
422 props->properties = (void *)(props->bufs + properties->package.count);
425 package = props->properties;
426 package->type = ACPI_TYPE_PACKAGE;
427 package->package.elements = package + 1;
428 count = &package->package.count;
434 for (i = 0; i < properties->package.count; i++) {
435 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
436 union acpi_object *property = &properties->package.elements[i];
437 union acpi_object *prop, *obj, *buf_obj;
440 if (property->type != ACPI_TYPE_PACKAGE ||
441 property->package.count != 2) {
442 acpi_handle_warn(handle,
443 "buffer property %u has %u entries\n",
444 i, property->package.count);
448 prop = &property->package.elements[0];
449 obj = &property->package.elements[1];
451 if (prop->type != ACPI_TYPE_STRING ||
452 obj->type != ACPI_TYPE_STRING) {
453 acpi_handle_warn(handle,
454 "wrong object types %u and %u\n",
455 prop->type, obj->type);
459 status = acpi_evaluate_object_typed(handle, obj->string.pointer,
462 if (ACPI_FAILURE(status)) {
463 acpi_handle_warn(handle,
464 "can't evaluate \"%*pE\" as buffer\n",
466 obj->string.pointer);
470 package->type = ACPI_TYPE_PACKAGE;
471 package->package.elements = prop;
472 package->package.count = 2;
474 buf_obj = buf.pointer;
476 /* Replace the string object with a buffer object */
477 obj->type = ACPI_TYPE_BUFFER;
478 obj->buffer.length = buf_obj->buffer.length;
479 obj->buffer.pointer = buf_obj->buffer.pointer;
481 props->bufs[i] = buf.pointer;
487 list_add(&props->list, &data->properties);
492 static bool acpi_extract_properties(acpi_handle scope, union acpi_object *desc,
493 struct acpi_device_data *data)
497 if (desc->package.count % 2)
500 /* Look for the device properties GUID. */
501 for (i = 0; i < desc->package.count; i += 2) {
502 const union acpi_object *guid;
503 union acpi_object *properties;
505 guid = &desc->package.elements[i];
506 properties = &desc->package.elements[i + 1];
509 * The first element must be a GUID and the second one must be
512 if (guid->type != ACPI_TYPE_BUFFER ||
513 guid->buffer.length != 16 ||
514 properties->type != ACPI_TYPE_PACKAGE)
517 if (guid_equal((guid_t *)guid->buffer.pointer,
518 &buffer_prop_guid)) {
519 acpi_data_add_buffer_props(scope, data, properties);
523 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
527 * We found the matching GUID. Now validate the format of the
528 * package immediately following it.
530 if (!acpi_properties_format_valid(properties))
533 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
537 return !list_empty(&data->properties);
540 void acpi_init_properties(struct acpi_device *adev)
542 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
543 struct acpi_hardware_id *hwid;
545 bool acpi_of = false;
547 INIT_LIST_HEAD(&adev->data.properties);
548 INIT_LIST_HEAD(&adev->data.subnodes);
554 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
555 * Device Tree compatible properties for this device.
557 list_for_each_entry(hwid, &adev->pnp.ids, list) {
558 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
564 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
566 if (ACPI_FAILURE(status))
569 if (acpi_extract_properties(adev->handle, buf.pointer, &adev->data)) {
570 adev->data.pointer = buf.pointer;
572 acpi_init_of_compatible(adev);
574 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
575 &adev->data, acpi_fwnode_handle(adev)))
576 adev->data.pointer = buf.pointer;
578 if (!adev->data.pointer) {
579 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
580 ACPI_FREE(buf.pointer);
582 if (!acpi_tie_nondev_subnodes(&adev->data))
583 acpi_untie_nondev_subnodes(&adev->data);
587 if (acpi_of && !adev->flags.of_compatible_ok)
588 acpi_handle_info(adev->handle,
589 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
591 if (!adev->data.pointer)
592 acpi_extract_apple_properties(adev);
595 static void acpi_free_device_properties(struct list_head *list)
597 struct acpi_device_properties *props, *tmp;
599 list_for_each_entry_safe(props, tmp, list, list) {
602 list_del(&props->list);
603 /* Buffer data properties were separately allocated */
605 for (i = 0; i < props->properties->package.count; i++)
606 ACPI_FREE(props->bufs[i]);
611 static void acpi_destroy_nondev_subnodes(struct list_head *list)
613 struct acpi_data_node *dn, *next;
615 if (list_empty(list))
618 list_for_each_entry_safe_reverse(dn, next, list, sibling) {
619 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
620 wait_for_completion(&dn->kobj_done);
621 list_del(&dn->sibling);
622 ACPI_FREE((void *)dn->data.pointer);
623 acpi_free_device_properties(&dn->data.properties);
628 void acpi_free_properties(struct acpi_device *adev)
630 acpi_untie_nondev_subnodes(&adev->data);
631 acpi_destroy_nondev_subnodes(&adev->data.subnodes);
632 ACPI_FREE((void *)adev->data.pointer);
633 adev->data.of_compatible = NULL;
634 adev->data.pointer = NULL;
635 acpi_free_device_properties(&adev->data.properties);
639 * acpi_data_get_property - return an ACPI property with given name
640 * @data: ACPI device deta object to get the property from
641 * @name: Name of the property
642 * @type: Expected property type
643 * @obj: Location to store the property value (if not %NULL)
645 * Look up a property with @name and store a pointer to the resulting ACPI
646 * object at the location pointed to by @obj if found.
648 * Callers must not attempt to free the returned objects. These objects will be
649 * freed by the ACPI core automatically during the removal of @data.
651 * Return: %0 if property with @name has been found (success),
652 * %-EINVAL if the arguments are invalid,
653 * %-EINVAL if the property doesn't exist,
654 * %-EPROTO if the property value type doesn't match @type.
656 static int acpi_data_get_property(const struct acpi_device_data *data,
657 const char *name, acpi_object_type type,
658 const union acpi_object **obj)
660 const struct acpi_device_properties *props;
665 if (!data->pointer || list_empty(&data->properties))
668 list_for_each_entry(props, &data->properties, list) {
669 const union acpi_object *properties;
672 properties = props->properties;
673 for (i = 0; i < properties->package.count; i++) {
674 const union acpi_object *propname, *propvalue;
675 const union acpi_object *property;
677 property = &properties->package.elements[i];
679 propname = &property->package.elements[0];
680 propvalue = &property->package.elements[1];
682 if (!strcmp(name, propname->string.pointer)) {
683 if (type != ACPI_TYPE_ANY &&
684 propvalue->type != type)
697 * acpi_dev_get_property - return an ACPI property with given name.
698 * @adev: ACPI device to get the property from.
699 * @name: Name of the property.
700 * @type: Expected property type.
701 * @obj: Location to store the property value (if not %NULL).
703 int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
704 acpi_object_type type, const union acpi_object **obj)
706 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
708 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
710 static const struct acpi_device_data *
711 acpi_device_data_of_node(const struct fwnode_handle *fwnode)
713 if (is_acpi_device_node(fwnode)) {
714 const struct acpi_device *adev = to_acpi_device_node(fwnode);
717 if (is_acpi_data_node(fwnode)) {
718 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
725 * acpi_node_prop_get - return an ACPI property with given name.
726 * @fwnode: Firmware node to get the property from.
727 * @propname: Name of the property.
728 * @valptr: Location to store a pointer to the property value (if not %NULL).
730 int acpi_node_prop_get(const struct fwnode_handle *fwnode,
731 const char *propname, void **valptr)
733 return acpi_data_get_property(acpi_device_data_of_node(fwnode),
734 propname, ACPI_TYPE_ANY,
735 (const union acpi_object **)valptr);
739 * acpi_data_get_property_array - return an ACPI array property with given name
740 * @data: ACPI data object to get the property from
741 * @name: Name of the property
742 * @type: Expected type of array elements
743 * @obj: Location to store a pointer to the property value (if not NULL)
745 * Look up an array property with @name and store a pointer to the resulting
746 * ACPI object at the location pointed to by @obj if found.
748 * Callers must not attempt to free the returned objects. Those objects will be
749 * freed by the ACPI core automatically during the removal of @data.
751 * Return: %0 if array property (package) with @name has been found (success),
752 * %-EINVAL if the arguments are invalid,
753 * %-EINVAL if the property doesn't exist,
754 * %-EPROTO if the property is not a package or the type of its elements
755 * doesn't match @type.
757 static int acpi_data_get_property_array(const struct acpi_device_data *data,
759 acpi_object_type type,
760 const union acpi_object **obj)
762 const union acpi_object *prop;
765 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
769 if (type != ACPI_TYPE_ANY) {
770 /* Check that all elements are of correct type. */
771 for (i = 0; i < prop->package.count; i++)
772 if (prop->package.elements[i].type != type)
781 static struct fwnode_handle *
782 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
783 const char *childname)
785 struct fwnode_handle *child;
787 fwnode_for_each_child_node(fwnode, child) {
788 if (is_acpi_data_node(child)) {
789 if (acpi_data_node_match(child, childname))
794 if (!strncmp(acpi_device_bid(to_acpi_device_node(child)),
795 childname, ACPI_NAMESEG_SIZE))
802 static int acpi_get_ref_args(struct fwnode_reference_args *args,
803 struct fwnode_handle *ref_fwnode,
804 const union acpi_object **element,
805 const union acpi_object *end, size_t num_args)
810 * Assume the following integer elements are all args. Stop counting on
811 * the first reference (possibly represented as a string) or end of the
812 * package arguments. In case of neither reference, nor integer, return
813 * an error, we can't parse it.
815 for (i = 0; (*element) + i < end && i < num_args; i++) {
816 acpi_object_type type = (*element)[i].type;
818 if (type == ACPI_TYPE_LOCAL_REFERENCE || type == ACPI_TYPE_STRING)
821 if (type == ACPI_TYPE_INTEGER)
827 if (nargs > NR_FWNODE_REFERENCE_ARGS)
831 args->fwnode = ref_fwnode;
833 for (i = 0; i < nargs; i++)
834 args->args[i] = (*element)[i].integer.value;
842 static struct fwnode_handle *acpi_parse_string_ref(const struct fwnode_handle *fwnode,
843 const char *refstring)
845 acpi_handle scope, handle;
846 struct acpi_data_node *dn;
847 struct acpi_device *device;
850 if (is_acpi_device_node(fwnode)) {
851 scope = to_acpi_device_node(fwnode)->handle;
852 } else if (is_acpi_data_node(fwnode)) {
853 scope = to_acpi_data_node(fwnode)->handle;
855 pr_debug("Bad node type for node %pfw\n", fwnode);
859 status = acpi_get_handle(scope, refstring, &handle);
860 if (ACPI_FAILURE(status)) {
861 acpi_handle_debug(scope, "Unable to get an ACPI handle for %s\n",
866 device = acpi_fetch_acpi_dev(handle);
868 return acpi_fwnode_handle(device);
870 status = acpi_get_data_full(handle, acpi_nondev_subnode_tag,
872 if (ACPI_FAILURE(status) || !dn) {
873 acpi_handle_debug(handle, "Subnode not found\n");
881 * __acpi_node_get_property_reference - returns handle to the referenced object
882 * @fwnode: Firmware node to get the property from
883 * @propname: Name of the property
884 * @index: Index of the reference to return
885 * @num_args: Maximum number of arguments after each reference
886 * @args: Location to store the returned reference with optional arguments
889 * Find property with @name, verifify that it is a package containing at least
890 * one object reference and if so, store the ACPI device object pointer to the
891 * target object in @args->adev. If the reference includes arguments, store
892 * them in the @args->args[] array.
894 * If there's more than one reference in the property value package, @index is
895 * used to select the one to return.
897 * It is possible to leave holes in the property value set like in the
910 * Calling this function with index %2 or index %3 return %-ENOENT. If the
911 * property does not contain any more values %-ENOENT is returned. The NULL
912 * entry must be single integer and preferably contain value %0.
914 * Return: %0 on success, negative error code on failure.
916 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
917 const char *propname, size_t index, size_t num_args,
918 struct fwnode_reference_args *args)
920 const union acpi_object *element, *end;
921 const union acpi_object *obj;
922 const struct acpi_device_data *data;
923 struct fwnode_handle *ref_fwnode;
924 struct acpi_device *device;
927 data = acpi_device_data_of_node(fwnode);
931 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
933 return ret == -EINVAL ? -ENOENT : -EINVAL;
936 case ACPI_TYPE_LOCAL_REFERENCE:
937 /* Plain single reference without arguments. */
941 device = acpi_fetch_acpi_dev(obj->reference.handle);
948 args->fwnode = acpi_fwnode_handle(device);
952 case ACPI_TYPE_STRING:
956 ref_fwnode = acpi_parse_string_ref(fwnode, obj->string.pointer);
960 args->fwnode = ref_fwnode;
964 case ACPI_TYPE_PACKAGE:
966 * If it is not a single reference, then it is a package of
967 * references, followed by number of ints as follows:
969 * Package () { REF, INT, REF, INT, INT }
971 * Here, REF may be either a local reference or a string. The
972 * index argument is then used to determine which reference the
973 * caller wants (along with the arguments).
980 if (index >= obj->package.count)
983 element = obj->package.elements;
984 end = element + obj->package.count;
986 while (element < end) {
987 switch (element->type) {
988 case ACPI_TYPE_LOCAL_REFERENCE:
989 device = acpi_fetch_acpi_dev(element->reference.handle);
995 ret = acpi_get_ref_args(idx == index ? args : NULL,
996 acpi_fwnode_handle(device),
997 &element, end, num_args);
1005 case ACPI_TYPE_STRING:
1006 ref_fwnode = acpi_parse_string_ref(fwnode,
1007 element->string.pointer);
1013 ret = acpi_get_ref_args(idx == index ? args : NULL,
1014 ref_fwnode, &element, end,
1023 case ACPI_TYPE_INTEGER:
1037 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
1039 static int acpi_data_prop_read_single(const struct acpi_device_data *data,
1040 const char *propname,
1041 enum dev_prop_type proptype, void *val)
1043 const union acpi_object *obj;
1046 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
1047 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
1048 else if (proptype == DEV_PROP_STRING)
1049 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
1055 if (obj->integer.value > U8_MAX)
1058 *(u8 *)val = obj->integer.value;
1061 if (obj->integer.value > U16_MAX)
1064 *(u16 *)val = obj->integer.value;
1067 if (obj->integer.value > U32_MAX)
1070 *(u32 *)val = obj->integer.value;
1074 *(u64 *)val = obj->integer.value;
1076 case DEV_PROP_STRING:
1078 *(char **)val = obj->string.pointer;
1084 /* When no storage provided return number of available values */
1088 #define acpi_copy_property_array_uint(items, val, nval) \
1090 typeof(items) __items = items; \
1091 typeof(val) __val = val; \
1092 typeof(nval) __nval = nval; \
1096 for (i = 0; i < __nval; i++) { \
1097 if (__items->type == ACPI_TYPE_BUFFER) { \
1098 __val[i] = __items->buffer.pointer[i]; \
1101 if (__items[i].type != ACPI_TYPE_INTEGER) { \
1105 if (__items[i].integer.value > _Generic(__val, \
1109 u64 *: U64_MAX)) { \
1114 __val[i] = __items[i].integer.value; \
1119 static int acpi_copy_property_array_string(const union acpi_object *items,
1120 char **val, size_t nval)
1124 for (i = 0; i < nval; i++) {
1125 if (items[i].type != ACPI_TYPE_STRING)
1128 val[i] = items[i].string.pointer;
1133 static int acpi_data_prop_read(const struct acpi_device_data *data,
1134 const char *propname,
1135 enum dev_prop_type proptype,
1136 void *val, size_t nval)
1138 const union acpi_object *obj;
1139 const union acpi_object *items;
1142 if (nval == 1 || !val) {
1143 ret = acpi_data_prop_read_single(data, propname, proptype, val);
1145 * The overflow error means that the property is there and it is
1146 * single-value, but its type does not match, so return.
1148 if (ret >= 0 || ret == -EOVERFLOW)
1152 * Reading this property as a single-value one failed, but its
1153 * value may still be represented as one-element array, so
1158 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
1159 if (ret && proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
1160 ret = acpi_data_get_property(data, propname, ACPI_TYPE_BUFFER,
1166 if (obj->type == ACPI_TYPE_BUFFER)
1167 return obj->buffer.length;
1169 return obj->package.count;
1173 case DEV_PROP_STRING:
1176 if (obj->type == ACPI_TYPE_BUFFER) {
1177 if (nval > obj->buffer.length)
1180 if (nval > obj->package.count)
1188 if (obj->type == ACPI_TYPE_BUFFER) {
1189 if (proptype != DEV_PROP_U8)
1193 items = obj->package.elements;
1198 ret = acpi_copy_property_array_uint(items, (u8 *)val, nval);
1201 ret = acpi_copy_property_array_uint(items, (u16 *)val, nval);
1204 ret = acpi_copy_property_array_uint(items, (u32 *)val, nval);
1207 ret = acpi_copy_property_array_uint(items, (u64 *)val, nval);
1209 case DEV_PROP_STRING:
1210 ret = acpi_copy_property_array_string(
1211 items, (char **)val,
1212 min_t(u32, nval, obj->package.count));
1222 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
1223 * @fwnode: Firmware node to get the property from.
1224 * @propname: Name of the property.
1225 * @proptype: Expected property type.
1226 * @val: Location to store the property value (if not %NULL).
1227 * @nval: Size of the array pointed to by @val.
1229 * If @val is %NULL, return the number of array elements comprising the value
1230 * of the property. Otherwise, read at most @nval values to the array at the
1231 * location pointed to by @val.
1233 static int acpi_node_prop_read(const struct fwnode_handle *fwnode,
1234 const char *propname, enum dev_prop_type proptype,
1235 void *val, size_t nval)
1237 return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
1238 propname, proptype, val, nval);
1241 static int stop_on_next(struct acpi_device *adev, void *data)
1243 struct acpi_device **ret_p = data;
1250 /* Skip until the "previous" object is found. */
1258 * acpi_get_next_subnode - Return the next child node handle for a fwnode
1259 * @fwnode: Firmware node to find the next child node for.
1260 * @child: Handle to one of the device's child nodes or a null handle.
1262 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
1263 struct fwnode_handle *child)
1265 struct acpi_device *adev = to_acpi_device_node(fwnode);
1267 if ((!child || is_acpi_device_node(child)) && adev) {
1268 struct acpi_device *child_adev = to_acpi_device_node(child);
1270 acpi_dev_for_each_child(adev, stop_on_next, &child_adev);
1272 return acpi_fwnode_handle(child_adev);
1277 if (!child || is_acpi_data_node(child)) {
1278 const struct acpi_data_node *data = to_acpi_data_node(fwnode);
1279 const struct list_head *head;
1280 struct list_head *next;
1281 struct acpi_data_node *dn;
1284 * We can have a combination of device and data nodes, e.g. with
1285 * hierarchical _DSD properties. Make sure the adev pointer is
1286 * restored before going through data nodes, otherwise we will
1287 * be looking for data_nodes below the last device found instead
1288 * of the common fwnode shared by device_nodes and data_nodes.
1290 adev = to_acpi_device_node(fwnode);
1292 head = &adev->data.subnodes;
1294 head = &data->data.subnodes;
1298 if (list_empty(head))
1302 dn = to_acpi_data_node(child);
1303 next = dn->sibling.next;
1307 dn = list_entry(next, struct acpi_data_node, sibling);
1309 dn = list_first_entry(head, struct acpi_data_node, sibling);
1317 * acpi_node_get_parent - Return parent fwnode of this fwnode
1318 * @fwnode: Firmware node whose parent to get
1320 * Returns parent node of an ACPI device or data firmware node or %NULL if
1323 static struct fwnode_handle *
1324 acpi_node_get_parent(const struct fwnode_handle *fwnode)
1326 if (is_acpi_data_node(fwnode)) {
1327 /* All data nodes have parent pointer so just return that */
1328 return to_acpi_data_node(fwnode)->parent;
1330 if (is_acpi_device_node(fwnode)) {
1331 struct acpi_device *parent;
1333 parent = acpi_dev_parent(to_acpi_device_node(fwnode));
1335 return acpi_fwnode_handle(parent);
1342 * Return true if the node is an ACPI graph node. Called on either ports
1345 static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1348 unsigned int len = strlen(str);
1351 if (!len || !is_acpi_data_node(fwnode))
1354 name = to_acpi_data_node(fwnode)->name;
1356 return (fwnode_property_present(fwnode, "reg") &&
1357 !strncmp(name, str, len) && name[len] == '@') ||
1358 fwnode_property_present(fwnode, str);
1362 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1363 * @fwnode: Pointer to the parent firmware node
1364 * @prev: Previous endpoint node or %NULL to get the first
1366 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1367 * %NULL if there is no next endpoint or in case of error. In case of success
1368 * the next endpoint is returned.
1370 static struct fwnode_handle *acpi_graph_get_next_endpoint(
1371 const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1373 struct fwnode_handle *port = NULL;
1374 struct fwnode_handle *endpoint;
1378 port = fwnode_get_next_child_node(fwnode, port);
1380 * The names of the port nodes begin with "port@"
1381 * followed by the number of the port node and they also
1382 * have a "reg" property that also has the number of the
1383 * port node. For compatibility reasons a node is also
1384 * recognised as a port node from the "port" property.
1386 if (is_acpi_graph_node(port, "port"))
1390 port = fwnode_get_parent(prev);
1396 endpoint = fwnode_get_next_child_node(port, prev);
1398 port = fwnode_get_next_child_node(fwnode, port);
1401 if (is_acpi_graph_node(port, "port"))
1402 endpoint = fwnode_get_next_child_node(port, NULL);
1406 * The names of the endpoint nodes begin with "endpoint@" followed by
1407 * the number of the endpoint node and they also have a "reg" property
1408 * that also has the number of the endpoint node. For compatibility
1409 * reasons a node is also recognised as an endpoint node from the
1410 * "endpoint" property.
1412 if (!is_acpi_graph_node(endpoint, "endpoint"))
1419 * acpi_graph_get_child_prop_value - Return a child with a given property value
1420 * @fwnode: device fwnode
1421 * @prop_name: The name of the property to look for
1422 * @val: the desired property value
1424 * Return the port node corresponding to a given port number. Returns
1425 * the child node on success, NULL otherwise.
1427 static struct fwnode_handle *acpi_graph_get_child_prop_value(
1428 const struct fwnode_handle *fwnode, const char *prop_name,
1431 struct fwnode_handle *child;
1433 fwnode_for_each_child_node(fwnode, child) {
1436 if (fwnode_property_read_u32(child, prop_name, &nr))
1448 * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint
1449 * @__fwnode: Endpoint firmware node pointing to a remote device
1451 * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1453 static struct fwnode_handle *
1454 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1456 struct fwnode_handle *fwnode;
1457 unsigned int port_nr, endpoint_nr;
1458 struct fwnode_reference_args args;
1461 memset(&args, 0, sizeof(args));
1462 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1467 /* Direct endpoint reference? */
1468 if (!is_acpi_device_node(args.fwnode))
1469 return args.nargs ? NULL : args.fwnode;
1472 * Always require two arguments with the reference: port and
1475 if (args.nargs != 2)
1478 fwnode = args.fwnode;
1479 port_nr = args.args[0];
1480 endpoint_nr = args.args[1];
1482 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1484 return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1487 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1489 if (!is_acpi_device_node(fwnode))
1492 return acpi_device_is_present(to_acpi_device_node(fwnode));
1496 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1497 const struct device *dev)
1499 return acpi_device_get_match_data(dev);
1502 static bool acpi_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
1504 return acpi_dma_supported(to_acpi_device_node(fwnode));
1507 static enum dev_dma_attr
1508 acpi_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
1510 return acpi_get_dma_attr(to_acpi_device_node(fwnode));
1513 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1514 const char *propname)
1516 return !acpi_node_prop_get(fwnode, propname, NULL);
1520 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1521 const char *propname,
1522 unsigned int elem_size, void *val,
1525 enum dev_prop_type type;
1527 switch (elem_size) {
1532 type = DEV_PROP_U16;
1535 type = DEV_PROP_U32;
1538 type = DEV_PROP_U64;
1544 return acpi_node_prop_read(fwnode, propname, type, val, nval);
1548 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1549 const char *propname, const char **val,
1552 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1557 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1558 const char *prop, const char *nargs_prop,
1559 unsigned int args_count, unsigned int index,
1560 struct fwnode_reference_args *args)
1562 return __acpi_node_get_property_reference(fwnode, prop, index,
1566 static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
1568 const struct acpi_device *adev;
1569 struct fwnode_handle *parent;
1571 /* Is this the root node? */
1572 parent = fwnode_get_parent(fwnode);
1576 fwnode_handle_put(parent);
1578 if (is_acpi_data_node(fwnode)) {
1579 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
1584 adev = to_acpi_device_node(fwnode);
1588 return acpi_device_bid(adev);
1592 acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1594 struct fwnode_handle *parent;
1596 /* Is this the root node? */
1597 parent = fwnode_get_parent(fwnode);
1601 /* Is this 2nd node from the root? */
1602 parent = fwnode_get_next_parent(parent);
1606 fwnode_handle_put(parent);
1608 /* ACPI device or data node. */
1612 static struct fwnode_handle *
1613 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1615 return acpi_node_get_parent(fwnode);
1618 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1619 struct fwnode_endpoint *endpoint)
1621 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1623 endpoint->local_fwnode = fwnode;
1625 if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
1626 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1627 if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
1628 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1633 static int acpi_fwnode_irq_get(const struct fwnode_handle *fwnode,
1636 struct resource res;
1639 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1646 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1647 const struct fwnode_operations ops = { \
1648 .device_is_available = acpi_fwnode_device_is_available, \
1649 .device_get_match_data = acpi_fwnode_device_get_match_data, \
1650 .device_dma_supported = \
1651 acpi_fwnode_device_dma_supported, \
1652 .device_get_dma_attr = acpi_fwnode_device_get_dma_attr, \
1653 .property_present = acpi_fwnode_property_present, \
1654 .property_read_int_array = \
1655 acpi_fwnode_property_read_int_array, \
1656 .property_read_string_array = \
1657 acpi_fwnode_property_read_string_array, \
1658 .get_parent = acpi_node_get_parent, \
1659 .get_next_child_node = acpi_get_next_subnode, \
1660 .get_named_child_node = acpi_fwnode_get_named_child_node, \
1661 .get_name = acpi_fwnode_get_name, \
1662 .get_name_prefix = acpi_fwnode_get_name_prefix, \
1663 .get_reference_args = acpi_fwnode_get_reference_args, \
1664 .graph_get_next_endpoint = \
1665 acpi_graph_get_next_endpoint, \
1666 .graph_get_remote_endpoint = \
1667 acpi_graph_get_remote_endpoint, \
1668 .graph_get_port_parent = acpi_fwnode_get_parent, \
1669 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1670 .irq_get = acpi_fwnode_irq_get, \
1672 EXPORT_SYMBOL_GPL(ops)
1674 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1675 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1676 const struct fwnode_operations acpi_static_fwnode_ops;
1678 bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1680 return !IS_ERR_OR_NULL(fwnode) &&
1681 fwnode->ops == &acpi_device_fwnode_ops;
1683 EXPORT_SYMBOL(is_acpi_device_node);
1685 bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1687 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1689 EXPORT_SYMBOL(is_acpi_data_node);