GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / acpi / property.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ACPI device specific properties support.
4  *
5  * Copyright (C) 2014, Intel Corporation
6  * All rights reserved.
7  *
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  */
12
13 #include <linux/acpi.h>
14 #include <linux/device.h>
15 #include <linux/export.h>
16
17 #include "internal.h"
18
19 static int acpi_data_get_property_array(const struct acpi_device_data *data,
20                                         const char *name,
21                                         acpi_object_type type,
22                                         const union acpi_object **obj);
23
24 /*
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.
31  */
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),
51 };
52
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);
57
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);
62
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);
70
71 static bool acpi_nondev_subnode_extract(union acpi_object *desc,
72                                         acpi_handle handle,
73                                         const union acpi_object *link,
74                                         struct list_head *list,
75                                         struct fwnode_handle *parent)
76 {
77         struct acpi_data_node *dn;
78         bool result;
79
80         dn = kzalloc(sizeof(*dn), GFP_KERNEL);
81         if (!dn)
82                 return false;
83
84         dn->name = link->package.elements[0].string.pointer;
85         fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops);
86         dn->parent = parent;
87         INIT_LIST_HEAD(&dn->data.properties);
88         INIT_LIST_HEAD(&dn->data.subnodes);
89
90         result = acpi_extract_properties(handle, desc, &dn->data);
91
92         if (handle) {
93                 acpi_handle scope;
94                 acpi_status status;
95
96                 /*
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
100                  * object's parent.
101                  */
102                 status = acpi_get_parent(handle, &scope);
103                 if (ACPI_SUCCESS(status)
104                     && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
105                                                       &dn->fwnode))
106                         result = true;
107         } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
108                                                   &dn->fwnode)) {
109                 result = true;
110         }
111
112         if (result) {
113                 dn->handle = handle;
114                 dn->data.pointer = desc;
115                 list_add_tail(&dn->sibling, list);
116                 return true;
117         }
118
119         kfree(dn);
120         acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
121         return false;
122 }
123
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)
128 {
129         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
130         acpi_status status;
131
132         status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
133                                             ACPI_TYPE_PACKAGE);
134         if (ACPI_FAILURE(status))
135                 return false;
136
137         if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
138                                         parent))
139                 return true;
140
141         ACPI_FREE(buf.pointer);
142         return false;
143 }
144
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)
149 {
150         acpi_handle handle;
151         acpi_status status;
152
153         if (!scope)
154                 return false;
155
156         status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
157                                  &handle);
158         if (ACPI_FAILURE(status))
159                 return false;
160
161         return acpi_nondev_subnode_data_ok(handle, link, list, parent);
162 }
163
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)
168 {
169         bool ret = false;
170         int i;
171
172         for (i = 0; i < links->package.count; i++) {
173                 union acpi_object *link, *desc;
174                 acpi_handle handle;
175                 bool result;
176
177                 link = &links->package.elements[i];
178                 /* Only two elements allowed. */
179                 if (link->package.count != 2)
180                         continue;
181
182                 /* The first one must be a string. */
183                 if (link->package.elements[0].type != ACPI_TYPE_STRING)
184                         continue;
185
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,
190                                                          parent);
191                         break;
192                 case ACPI_TYPE_LOCAL_REFERENCE:
193                         handle = link->package.elements[1].reference.handle;
194                         result = acpi_nondev_subnode_data_ok(handle, link, list,
195                                                              parent);
196                         break;
197                 case ACPI_TYPE_PACKAGE:
198                         desc = &link->package.elements[1];
199                         result = acpi_nondev_subnode_extract(desc, NULL, link,
200                                                              list, parent);
201                         break;
202                 default:
203                         result = false;
204                         break;
205                 }
206                 ret = ret || result;
207         }
208
209         return ret;
210 }
211
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)
216 {
217         int i;
218
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;
223
224                 guid = &desc->package.elements[i];
225                 links = &desc->package.elements[i + 1];
226
227                 /*
228                  * The first element must be a GUID and the second one must be
229                  * a package.
230                  */
231                 if (guid->type != ACPI_TYPE_BUFFER ||
232                     guid->buffer.length != 16 ||
233                     links->type != ACPI_TYPE_PACKAGE)
234                         break;
235
236                 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
237                         continue;
238
239                 return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
240                                                 parent);
241         }
242
243         return false;
244 }
245
246 static bool acpi_property_value_ok(const union acpi_object *value)
247 {
248         int j;
249
250         /*
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.
253          */
254         switch (value->type) {
255         case ACPI_TYPE_INTEGER:
256         case ACPI_TYPE_STRING:
257         case ACPI_TYPE_LOCAL_REFERENCE:
258                 return true;
259
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:
266                                 continue;
267
268                         default:
269                                 return false;
270                         }
271
272                 return true;
273         }
274         return false;
275 }
276
277 static bool acpi_properties_format_valid(const union acpi_object *properties)
278 {
279         int i;
280
281         for (i = 0; i < properties->package.count; i++) {
282                 const union acpi_object *property;
283
284                 property = &properties->package.elements[i];
285                 /*
286                  * Only two elements allowed, the first one must be a string and
287                  * the second one has to satisfy certain conditions.
288                  */
289                 if (property->package.count != 2
290                     || property->package.elements[0].type != ACPI_TYPE_STRING
291                     || !acpi_property_value_ok(&property->package.elements[1]))
292                         return false;
293         }
294         return true;
295 }
296
297 static void acpi_init_of_compatible(struct acpi_device *adev)
298 {
299         const union acpi_object *of_compatible;
300         int ret;
301
302         ret = acpi_data_get_property_array(&adev->data, "compatible",
303                                            ACPI_TYPE_STRING, &of_compatible);
304         if (ret) {
305                 ret = acpi_dev_get_property(adev, "compatible",
306                                             ACPI_TYPE_STRING, &of_compatible);
307                 if (ret) {
308                         struct acpi_device *parent;
309
310                         parent = acpi_dev_parent(adev);
311                         if (parent && parent->flags.of_compatible_ok)
312                                 goto out;
313
314                         return;
315                 }
316         }
317         adev->data.of_compatible = of_compatible;
318
319  out:
320         adev->flags.of_compatible_ok = 1;
321 }
322
323 static bool acpi_is_property_guid(const guid_t *guid)
324 {
325         int i;
326
327         for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
328                 if (guid_equal(guid, &prp_guids[i]))
329                         return true;
330         }
331
332         return false;
333 }
334
335 struct acpi_device_properties *
336 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
337                     union acpi_object *properties)
338 {
339         struct acpi_device_properties *props;
340
341         props = kzalloc(sizeof(*props), GFP_KERNEL);
342         if (props) {
343                 INIT_LIST_HEAD(&props->list);
344                 props->guid = guid;
345                 props->properties = properties;
346                 list_add_tail(&props->list, &data->properties);
347         }
348
349         return props;
350 }
351
352 static void acpi_nondev_subnode_tag(acpi_handle handle, void *context)
353 {
354 }
355
356 static void acpi_untie_nondev_subnodes(struct acpi_device_data *data)
357 {
358         struct acpi_data_node *dn;
359
360         list_for_each_entry(dn, &data->subnodes, sibling) {
361                 acpi_detach_data(dn->handle, acpi_nondev_subnode_tag);
362
363                 acpi_untie_nondev_subnodes(&dn->data);
364         }
365 }
366
367 static bool acpi_tie_nondev_subnodes(struct acpi_device_data *data)
368 {
369         struct acpi_data_node *dn;
370
371         list_for_each_entry(dn, &data->subnodes, sibling) {
372                 acpi_status status;
373                 bool ret;
374
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");
378                         return false;
379                 }
380
381                 ret = acpi_tie_nondev_subnodes(&dn->data);
382                 if (!ret)
383                         return ret;
384         }
385
386         return true;
387 }
388
389 static void acpi_data_add_buffer_props(acpi_handle handle,
390                                        struct acpi_device_data *data,
391                                        union acpi_object *properties)
392 {
393         struct acpi_device_properties *props;
394         union acpi_object *package;
395         size_t alloc_size;
396         unsigned int i;
397         u32 *count;
398
399         if (check_mul_overflow((size_t)properties->package.count,
400                                sizeof(*package) + sizeof(void *),
401                                &alloc_size) ||
402             check_add_overflow(sizeof(*props) + sizeof(*package), alloc_size,
403                                &alloc_size)) {
404                 acpi_handle_warn(handle,
405                                  "can't allocate memory for %u buffer props",
406                                  properties->package.count);
407                 return;
408         }
409
410         props = kvzalloc(alloc_size, GFP_KERNEL);
411         if (!props)
412                 return;
413
414         props->guid = &buffer_prop_guid;
415         props->bufs = (void *)(props + 1);
416         props->properties = (void *)(props->bufs + properties->package.count);
417
418         /* Outer package */
419         package = props->properties;
420         package->type = ACPI_TYPE_PACKAGE;
421         package->package.elements = package + 1;
422         count = &package->package.count;
423         *count = 0;
424
425         /* Inner packages */
426         package++;
427
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;
432                 acpi_status status;
433
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);
439                         continue;
440                 }
441
442                 prop = &property->package.elements[0];
443                 obj = &property->package.elements[1];
444
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);
450                         continue;
451                 }
452
453                 status = acpi_evaluate_object_typed(handle, obj->string.pointer,
454                                                     NULL, &buf,
455                                                     ACPI_TYPE_BUFFER);
456                 if (ACPI_FAILURE(status)) {
457                         acpi_handle_warn(handle,
458                                          "can't evaluate \"%*pE\" as buffer\n",
459                                          obj->string.length,
460                                          obj->string.pointer);
461                         continue;
462                 }
463
464                 package->type = ACPI_TYPE_PACKAGE;
465                 package->package.elements = prop;
466                 package->package.count = 2;
467
468                 buf_obj = buf.pointer;
469
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;
474
475                 props->bufs[i] = buf.pointer;
476                 package++;
477                 (*count)++;
478         }
479
480         if (*count)
481                 list_add(&props->list, &data->properties);
482         else
483                 kvfree(props);
484 }
485
486 static bool acpi_extract_properties(acpi_handle scope, union acpi_object *desc,
487                                     struct acpi_device_data *data)
488 {
489         int i;
490
491         if (desc->package.count % 2)
492                 return false;
493
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;
498
499                 guid = &desc->package.elements[i];
500                 properties = &desc->package.elements[i + 1];
501
502                 /*
503                  * The first element must be a GUID and the second one must be
504                  * a package.
505                  */
506                 if (guid->type != ACPI_TYPE_BUFFER ||
507                     guid->buffer.length != 16 ||
508                     properties->type != ACPI_TYPE_PACKAGE)
509                         break;
510
511                 if (guid_equal((guid_t *)guid->buffer.pointer,
512                                &buffer_prop_guid)) {
513                         acpi_data_add_buffer_props(scope, data, properties);
514                         continue;
515                 }
516
517                 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
518                         continue;
519
520                 /*
521                  * We found the matching GUID. Now validate the format of the
522                  * package immediately following it.
523                  */
524                 if (!acpi_properties_format_valid(properties))
525                         continue;
526
527                 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
528                                     properties);
529         }
530
531         return !list_empty(&data->properties);
532 }
533
534 void acpi_init_properties(struct acpi_device *adev)
535 {
536         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
537         struct acpi_hardware_id *hwid;
538         acpi_status status;
539         bool acpi_of = false;
540
541         INIT_LIST_HEAD(&adev->data.properties);
542         INIT_LIST_HEAD(&adev->data.subnodes);
543
544         if (!adev->handle)
545                 return;
546
547         /*
548          * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
549          * Device Tree compatible properties for this device.
550          */
551         list_for_each_entry(hwid, &adev->pnp.ids, list) {
552                 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
553                         acpi_of = true;
554                         break;
555                 }
556         }
557
558         status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
559                                             ACPI_TYPE_PACKAGE);
560         if (ACPI_FAILURE(status))
561                 goto out;
562
563         if (acpi_extract_properties(adev->handle, buf.pointer, &adev->data)) {
564                 adev->data.pointer = buf.pointer;
565                 if (acpi_of)
566                         acpi_init_of_compatible(adev);
567         }
568         if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
569                                         &adev->data, acpi_fwnode_handle(adev)))
570                 adev->data.pointer = buf.pointer;
571
572         if (!adev->data.pointer) {
573                 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
574                 ACPI_FREE(buf.pointer);
575         } else {
576                 if (!acpi_tie_nondev_subnodes(&adev->data))
577                         acpi_untie_nondev_subnodes(&adev->data);
578         }
579
580  out:
581         if (acpi_of && !adev->flags.of_compatible_ok)
582                 acpi_handle_info(adev->handle,
583                          ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
584
585         if (!adev->data.pointer)
586                 acpi_extract_apple_properties(adev);
587 }
588
589 static void acpi_free_device_properties(struct list_head *list)
590 {
591         struct acpi_device_properties *props, *tmp;
592
593         list_for_each_entry_safe(props, tmp, list, list) {
594                 u32 i;
595
596                 list_del(&props->list);
597                 /* Buffer data properties were separately allocated */
598                 if (props->bufs)
599                         for (i = 0; i < props->properties->package.count; i++)
600                                 ACPI_FREE(props->bufs[i]);
601                 kvfree(props);
602         }
603 }
604
605 static void acpi_destroy_nondev_subnodes(struct list_head *list)
606 {
607         struct acpi_data_node *dn, *next;
608
609         if (list_empty(list))
610                 return;
611
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);
618                 kfree(dn);
619         }
620 }
621
622 void acpi_free_properties(struct acpi_device *adev)
623 {
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);
630 }
631
632 /**
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)
638  *
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.
641  *
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.
644  *
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.
649  */
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)
653 {
654         const struct acpi_device_properties *props;
655
656         if (!data || !name)
657                 return -EINVAL;
658
659         if (!data->pointer || list_empty(&data->properties))
660                 return -EINVAL;
661
662         list_for_each_entry(props, &data->properties, list) {
663                 const union acpi_object *properties;
664                 unsigned int i;
665
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;
670
671                         property = &properties->package.elements[i];
672
673                         propname = &property->package.elements[0];
674                         propvalue = &property->package.elements[1];
675
676                         if (!strcmp(name, propname->string.pointer)) {
677                                 if (type != ACPI_TYPE_ANY &&
678                                     propvalue->type != type)
679                                         return -EPROTO;
680                                 if (obj)
681                                         *obj = propvalue;
682
683                                 return 0;
684                         }
685                 }
686         }
687         return -EINVAL;
688 }
689
690 /**
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).
696  */
697 int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
698                           acpi_object_type type, const union acpi_object **obj)
699 {
700         return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
701 }
702 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
703
704 static const struct acpi_device_data *
705 acpi_device_data_of_node(const struct fwnode_handle *fwnode)
706 {
707         if (is_acpi_device_node(fwnode)) {
708                 const struct acpi_device *adev = to_acpi_device_node(fwnode);
709                 return &adev->data;
710         }
711         if (is_acpi_data_node(fwnode)) {
712                 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
713                 return &dn->data;
714         }
715         return NULL;
716 }
717
718 /**
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).
723  */
724 int acpi_node_prop_get(const struct fwnode_handle *fwnode,
725                        const char *propname, void **valptr)
726 {
727         return acpi_data_get_property(acpi_device_data_of_node(fwnode),
728                                       propname, ACPI_TYPE_ANY,
729                                       (const union acpi_object **)valptr);
730 }
731
732 /**
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)
738  *
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.
741  *
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.
744  *
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.
750  */
751 static int acpi_data_get_property_array(const struct acpi_device_data *data,
752                                         const char *name,
753                                         acpi_object_type type,
754                                         const union acpi_object **obj)
755 {
756         const union acpi_object *prop;
757         int ret, i;
758
759         ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
760         if (ret)
761                 return ret;
762
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)
767                                 return -EPROTO;
768         }
769         if (obj)
770                 *obj = prop;
771
772         return 0;
773 }
774
775 static struct fwnode_handle *
776 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
777                                  const char *childname)
778 {
779         struct fwnode_handle *child;
780
781         fwnode_for_each_child_node(fwnode, child) {
782                 if (is_acpi_data_node(child)) {
783                         if (acpi_data_node_match(child, childname))
784                                 return child;
785                         continue;
786                 }
787
788                 if (!strncmp(acpi_device_bid(to_acpi_device_node(child)),
789                              childname, ACPI_NAMESEG_SIZE))
790                         return child;
791         }
792
793         return NULL;
794 }
795
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)
800 {
801         u32 nargs = 0, i;
802
803         /*
804          * Find the referred data extension node under the
805          * referred device node.
806          */
807         for (; *element < end && (*element)->type == ACPI_TYPE_STRING;
808              (*element)++) {
809                 const char *child_name = (*element)->string.pointer;
810
811                 ref_fwnode = acpi_fwnode_get_named_child_node(ref_fwnode, child_name);
812                 if (!ref_fwnode)
813                         return -EINVAL;
814         }
815
816         /*
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.
820          */
821         for (i = 0; (*element) + i < end && i < num_args; i++) {
822                 acpi_object_type type = (*element)[i].type;
823
824                 if (type == ACPI_TYPE_LOCAL_REFERENCE)
825                         break;
826
827                 if (type == ACPI_TYPE_INTEGER)
828                         nargs++;
829                 else
830                         return -EINVAL;
831         }
832
833         if (nargs > NR_FWNODE_REFERENCE_ARGS)
834                 return -EINVAL;
835
836         if (args) {
837                 args->fwnode = ref_fwnode;
838                 args->nargs = nargs;
839                 for (i = 0; i < nargs; i++)
840                         args->args[i] = (*element)[i].integer.value;
841         }
842
843         (*element) += nargs;
844
845         return 0;
846 }
847
848 /**
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
855  *        (may be NULL)
856  *
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.
861  *
862  * If there's more than one reference in the property value package, @index is
863  * used to select the one to return.
864  *
865  * It is possible to leave holes in the property value set like in the
866  * example below:
867  *
868  * Package () {
869  *     "cs-gpios",
870  *     Package () {
871  *        ^GPIO, 19, 0, 0,
872  *        ^GPIO, 20, 0, 0,
873  *        0,
874  *        ^GPIO, 21, 0, 0,
875  *     }
876  * }
877  *
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.
881  *
882  * Return: %0 on success, negative error code on failure.
883  */
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)
887 {
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;
892         int ret, idx = 0;
893
894         data = acpi_device_data_of_node(fwnode);
895         if (!data)
896                 return -ENOENT;
897
898         ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
899         if (ret)
900                 return ret == -EINVAL ? -ENOENT : -EINVAL;
901
902         switch (obj->type) {
903         case ACPI_TYPE_LOCAL_REFERENCE:
904                 /* Plain single reference without arguments. */
905                 if (index)
906                         return -ENOENT;
907
908                 device = acpi_fetch_acpi_dev(obj->reference.handle);
909                 if (!device)
910                         return -EINVAL;
911
912                 if (!args)
913                         return 0;
914
915                 args->fwnode = acpi_fwnode_handle(device);
916                 args->nargs = 0;
917                 return 0;
918         case ACPI_TYPE_PACKAGE:
919                 /*
920                  * If it is not a single reference, then it is a package of
921                  * references followed by number of ints as follows:
922                  *
923                  *  Package () { REF, INT, REF, INT, INT }
924                  *
925                  * The index argument is then used to determine which reference
926                  * the caller wants (along with the arguments).
927                  */
928                 break;
929         default:
930                 return -EINVAL;
931         }
932
933         if (index >= obj->package.count)
934                 return -ENOENT;
935
936         element = obj->package.elements;
937         end = element + obj->package.count;
938
939         while (element < end) {
940                 switch (element->type) {
941                 case ACPI_TYPE_LOCAL_REFERENCE:
942                         device = acpi_fetch_acpi_dev(element->reference.handle);
943                         if (!device)
944                                 return -EINVAL;
945
946                         element++;
947
948                         ret = acpi_get_ref_args(idx == index ? args : NULL,
949                                                 acpi_fwnode_handle(device),
950                                                 &element, end, num_args);
951                         if (ret < 0)
952                                 return ret;
953
954                         if (idx == index)
955                                 return 0;
956
957                         break;
958                 case ACPI_TYPE_INTEGER:
959                         if (idx == index)
960                                 return -ENOENT;
961                         element++;
962                         break;
963                 default:
964                         return -EINVAL;
965                 }
966
967                 idx++;
968         }
969
970         return -ENOENT;
971 }
972 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
973
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)
977 {
978         const union acpi_object *obj;
979         int ret = 0;
980
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);
985         if (ret)
986                 return ret;
987
988         switch (proptype) {
989         case DEV_PROP_U8:
990                 if (obj->integer.value > U8_MAX)
991                         return -EOVERFLOW;
992                 if (val)
993                         *(u8 *)val = obj->integer.value;
994                 break;
995         case DEV_PROP_U16:
996                 if (obj->integer.value > U16_MAX)
997                         return -EOVERFLOW;
998                 if (val)
999                         *(u16 *)val = obj->integer.value;
1000                 break;
1001         case DEV_PROP_U32:
1002                 if (obj->integer.value > U32_MAX)
1003                         return -EOVERFLOW;
1004                 if (val)
1005                         *(u32 *)val = obj->integer.value;
1006                 break;
1007         case DEV_PROP_U64:
1008                 if (val)
1009                         *(u64 *)val = obj->integer.value;
1010                 break;
1011         case DEV_PROP_STRING:
1012                 if (val)
1013                         *(char **)val = obj->string.pointer;
1014                 return 1;
1015         default:
1016                 return -EINVAL;
1017         }
1018
1019         /* When no storage provided return number of available values */
1020         return val ? 0 : 1;
1021 }
1022
1023 #define acpi_copy_property_array_uint(items, val, nval)                 \
1024         ({                                                              \
1025                 typeof(items) __items = items;                          \
1026                 typeof(val) __val = val;                                \
1027                 typeof(nval) __nval = nval;                             \
1028                 size_t i;                                               \
1029                 int ret = 0;                                            \
1030                                                                         \
1031                 for (i = 0; i < __nval; i++) {                          \
1032                         if (__items->type == ACPI_TYPE_BUFFER) {        \
1033                                 __val[i] = __items->buffer.pointer[i];  \
1034                                 continue;                               \
1035                         }                                               \
1036                         if (__items[i].type != ACPI_TYPE_INTEGER) {     \
1037                                 ret = -EPROTO;                          \
1038                                 break;                                  \
1039                         }                                               \
1040                         if (__items[i].integer.value > _Generic(__val,  \
1041                                                                 u8 *: U8_MAX, \
1042                                                                 u16 *: U16_MAX, \
1043                                                                 u32 *: U32_MAX, \
1044                                                                 u64 *: U64_MAX)) { \
1045                                 ret = -EOVERFLOW;                       \
1046                                 break;                                  \
1047                         }                                               \
1048                                                                         \
1049                         __val[i] = __items[i].integer.value;            \
1050                 }                                                       \
1051                 ret;                                                    \
1052         })
1053
1054 static int acpi_copy_property_array_string(const union acpi_object *items,
1055                                            char **val, size_t nval)
1056 {
1057         int i;
1058
1059         for (i = 0; i < nval; i++) {
1060                 if (items[i].type != ACPI_TYPE_STRING)
1061                         return -EPROTO;
1062
1063                 val[i] = items[i].string.pointer;
1064         }
1065         return nval;
1066 }
1067
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)
1072 {
1073         const union acpi_object *obj;
1074         const union acpi_object *items;
1075         int ret;
1076
1077         if (nval == 1 || !val) {
1078                 ret = acpi_data_prop_read_single(data, propname, proptype, val);
1079                 /*
1080                  * The overflow error means that the property is there and it is
1081                  * single-value, but its type does not match, so return.
1082                  */
1083                 if (ret >= 0 || ret == -EOVERFLOW)
1084                         return ret;
1085
1086                 /*
1087                  * Reading this property as a single-value one failed, but its
1088                  * value may still be represented as one-element array, so
1089                  * continue.
1090                  */
1091         }
1092
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,
1096                                              &obj);
1097         if (ret)
1098                 return ret;
1099
1100         if (!val) {
1101                 if (obj->type == ACPI_TYPE_BUFFER)
1102                         return obj->buffer.length;
1103
1104                 return obj->package.count;
1105         }
1106
1107         switch (proptype) {
1108         case DEV_PROP_STRING:
1109                 break;
1110         default:
1111                 if (obj->type == ACPI_TYPE_BUFFER) {
1112                         if (nval > obj->buffer.length)
1113                                 return -EOVERFLOW;
1114                 } else {
1115                         if (nval > obj->package.count)
1116                                 return -EOVERFLOW;
1117                 }
1118                 break;
1119         }
1120         if (nval == 0)
1121                 return -EINVAL;
1122
1123         if (obj->type == ACPI_TYPE_BUFFER) {
1124                 if (proptype != DEV_PROP_U8)
1125                         return -EPROTO;
1126                 items = obj;
1127         } else {
1128                 items = obj->package.elements;
1129         }
1130
1131         switch (proptype) {
1132         case DEV_PROP_U8:
1133                 ret = acpi_copy_property_array_uint(items, (u8 *)val, nval);
1134                 break;
1135         case DEV_PROP_U16:
1136                 ret = acpi_copy_property_array_uint(items, (u16 *)val, nval);
1137                 break;
1138         case DEV_PROP_U32:
1139                 ret = acpi_copy_property_array_uint(items, (u32 *)val, nval);
1140                 break;
1141         case DEV_PROP_U64:
1142                 ret = acpi_copy_property_array_uint(items, (u64 *)val, nval);
1143                 break;
1144         case DEV_PROP_STRING:
1145                 ret = acpi_copy_property_array_string(
1146                         items, (char **)val,
1147                         min_t(u32, nval, obj->package.count));
1148                 break;
1149         default:
1150                 ret = -EINVAL;
1151                 break;
1152         }
1153         return ret;
1154 }
1155
1156 /**
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.
1163  *
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.
1167  */
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)
1171 {
1172         return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
1173                                    propname, proptype, val, nval);
1174 }
1175
1176 static int stop_on_next(struct acpi_device *adev, void *data)
1177 {
1178         struct acpi_device **ret_p = data;
1179
1180         if (!*ret_p) {
1181                 *ret_p = adev;
1182                 return 1;
1183         }
1184
1185         /* Skip until the "previous" object is found. */
1186         if (*ret_p == adev)
1187                 *ret_p = NULL;
1188
1189         return 0;
1190 }
1191
1192 /**
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.
1196  */
1197 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
1198                                             struct fwnode_handle *child)
1199 {
1200         struct acpi_device *adev = to_acpi_device_node(fwnode);
1201
1202         if ((!child || is_acpi_device_node(child)) && adev) {
1203                 struct acpi_device *child_adev = to_acpi_device_node(child);
1204
1205                 acpi_dev_for_each_child(adev, stop_on_next, &child_adev);
1206                 if (child_adev)
1207                         return acpi_fwnode_handle(child_adev);
1208
1209                 child = NULL;
1210         }
1211
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;
1217
1218                 /*
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.
1224                  */
1225                 adev = to_acpi_device_node(fwnode);
1226                 if (adev)
1227                         head = &adev->data.subnodes;
1228                 else if (data)
1229                         head = &data->data.subnodes;
1230                 else
1231                         return NULL;
1232
1233                 if (list_empty(head))
1234                         return NULL;
1235
1236                 if (child) {
1237                         dn = to_acpi_data_node(child);
1238                         next = dn->sibling.next;
1239                         if (next == head)
1240                                 return NULL;
1241
1242                         dn = list_entry(next, struct acpi_data_node, sibling);
1243                 } else {
1244                         dn = list_first_entry(head, struct acpi_data_node, sibling);
1245                 }
1246                 return &dn->fwnode;
1247         }
1248         return NULL;
1249 }
1250
1251 /**
1252  * acpi_node_get_parent - Return parent fwnode of this fwnode
1253  * @fwnode: Firmware node whose parent to get
1254  *
1255  * Returns parent node of an ACPI device or data firmware node or %NULL if
1256  * not available.
1257  */
1258 static struct fwnode_handle *
1259 acpi_node_get_parent(const struct fwnode_handle *fwnode)
1260 {
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;
1264         }
1265         if (is_acpi_device_node(fwnode)) {
1266                 struct acpi_device *parent;
1267
1268                 parent = acpi_dev_parent(to_acpi_device_node(fwnode));
1269                 if (parent)
1270                         return acpi_fwnode_handle(parent);
1271         }
1272
1273         return NULL;
1274 }
1275
1276 /*
1277  * Return true if the node is an ACPI graph node. Called on either ports
1278  * or endpoints.
1279  */
1280 static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1281                                const char *str)
1282 {
1283         unsigned int len = strlen(str);
1284         const char *name;
1285
1286         if (!len || !is_acpi_data_node(fwnode))
1287                 return false;
1288
1289         name = to_acpi_data_node(fwnode)->name;
1290
1291         return (fwnode_property_present(fwnode, "reg") &&
1292                 !strncmp(name, str, len) && name[len] == '@') ||
1293                 fwnode_property_present(fwnode, str);
1294 }
1295
1296 /**
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
1300  *
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.
1304  */
1305 static struct fwnode_handle *acpi_graph_get_next_endpoint(
1306         const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1307 {
1308         struct fwnode_handle *port = NULL;
1309         struct fwnode_handle *endpoint;
1310
1311         if (!prev) {
1312                 do {
1313                         port = fwnode_get_next_child_node(fwnode, port);
1314                         /*
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.
1320                          */
1321                         if (is_acpi_graph_node(port, "port"))
1322                                 break;
1323                 } while (port);
1324         } else {
1325                 port = fwnode_get_parent(prev);
1326         }
1327
1328         if (!port)
1329                 return NULL;
1330
1331         endpoint = fwnode_get_next_child_node(port, prev);
1332         while (!endpoint) {
1333                 port = fwnode_get_next_child_node(fwnode, port);
1334                 if (!port)
1335                         break;
1336                 if (is_acpi_graph_node(port, "port"))
1337                         endpoint = fwnode_get_next_child_node(port, NULL);
1338         }
1339
1340         /*
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.
1346          */
1347         if (!is_acpi_graph_node(endpoint, "endpoint"))
1348                 return NULL;
1349
1350         return endpoint;
1351 }
1352
1353 /**
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
1358  *
1359  * Return the port node corresponding to a given port number. Returns
1360  * the child node on success, NULL otherwise.
1361  */
1362 static struct fwnode_handle *acpi_graph_get_child_prop_value(
1363         const struct fwnode_handle *fwnode, const char *prop_name,
1364         unsigned int val)
1365 {
1366         struct fwnode_handle *child;
1367
1368         fwnode_for_each_child_node(fwnode, child) {
1369                 u32 nr;
1370
1371                 if (fwnode_property_read_u32(child, prop_name, &nr))
1372                         continue;
1373
1374                 if (val == nr)
1375                         return child;
1376         }
1377
1378         return NULL;
1379 }
1380
1381
1382 /**
1383  * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint
1384  * @__fwnode: Endpoint firmware node pointing to a remote device
1385  *
1386  * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1387  */
1388 static struct fwnode_handle *
1389 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1390 {
1391         struct fwnode_handle *fwnode;
1392         unsigned int port_nr, endpoint_nr;
1393         struct fwnode_reference_args args;
1394         int ret;
1395
1396         memset(&args, 0, sizeof(args));
1397         ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1398                                                &args);
1399         if (ret)
1400                 return NULL;
1401
1402         /* Direct endpoint reference? */
1403         if (!is_acpi_device_node(args.fwnode))
1404                 return args.nargs ? NULL : args.fwnode;
1405
1406         /*
1407          * Always require two arguments with the reference: port and
1408          * endpoint indices.
1409          */
1410         if (args.nargs != 2)
1411                 return NULL;
1412
1413         fwnode = args.fwnode;
1414         port_nr = args.args[0];
1415         endpoint_nr = args.args[1];
1416
1417         fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1418
1419         return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1420 }
1421
1422 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1423 {
1424         if (!is_acpi_device_node(fwnode))
1425                 return false;
1426
1427         return acpi_device_is_present(to_acpi_device_node(fwnode));
1428 }
1429
1430 static const void *
1431 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1432                                   const struct device *dev)
1433 {
1434         return acpi_device_get_match_data(dev);
1435 }
1436
1437 static bool acpi_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
1438 {
1439         return acpi_dma_supported(to_acpi_device_node(fwnode));
1440 }
1441
1442 static enum dev_dma_attr
1443 acpi_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
1444 {
1445         return acpi_get_dma_attr(to_acpi_device_node(fwnode));
1446 }
1447
1448 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1449                                          const char *propname)
1450 {
1451         return !acpi_node_prop_get(fwnode, propname, NULL);
1452 }
1453
1454 static int
1455 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1456                                     const char *propname,
1457                                     unsigned int elem_size, void *val,
1458                                     size_t nval)
1459 {
1460         enum dev_prop_type type;
1461
1462         switch (elem_size) {
1463         case sizeof(u8):
1464                 type = DEV_PROP_U8;
1465                 break;
1466         case sizeof(u16):
1467                 type = DEV_PROP_U16;
1468                 break;
1469         case sizeof(u32):
1470                 type = DEV_PROP_U32;
1471                 break;
1472         case sizeof(u64):
1473                 type = DEV_PROP_U64;
1474                 break;
1475         default:
1476                 return -ENXIO;
1477         }
1478
1479         return acpi_node_prop_read(fwnode, propname, type, val, nval);
1480 }
1481
1482 static int
1483 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1484                                        const char *propname, const char **val,
1485                                        size_t nval)
1486 {
1487         return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1488                                    val, nval);
1489 }
1490
1491 static int
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)
1496 {
1497         return __acpi_node_get_property_reference(fwnode, prop, index,
1498                                                   args_count, args);
1499 }
1500
1501 static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
1502 {
1503         const struct acpi_device *adev;
1504         struct fwnode_handle *parent;
1505
1506         /* Is this the root node? */
1507         parent = fwnode_get_parent(fwnode);
1508         if (!parent)
1509                 return "\\";
1510
1511         fwnode_handle_put(parent);
1512
1513         if (is_acpi_data_node(fwnode)) {
1514                 const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
1515
1516                 return dn->name;
1517         }
1518
1519         adev = to_acpi_device_node(fwnode);
1520         if (WARN_ON(!adev))
1521                 return NULL;
1522
1523         return acpi_device_bid(adev);
1524 }
1525
1526 static const char *
1527 acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1528 {
1529         struct fwnode_handle *parent;
1530
1531         /* Is this the root node? */
1532         parent = fwnode_get_parent(fwnode);
1533         if (!parent)
1534                 return "";
1535
1536         /* Is this 2nd node from the root? */
1537         parent = fwnode_get_next_parent(parent);
1538         if (!parent)
1539                 return "";
1540
1541         fwnode_handle_put(parent);
1542
1543         /* ACPI device or data node. */
1544         return ".";
1545 }
1546
1547 static struct fwnode_handle *
1548 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1549 {
1550         return acpi_node_get_parent(fwnode);
1551 }
1552
1553 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1554                                             struct fwnode_endpoint *endpoint)
1555 {
1556         struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1557
1558         endpoint->local_fwnode = fwnode;
1559
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);
1564
1565         return 0;
1566 }
1567
1568 static int acpi_fwnode_irq_get(const struct fwnode_handle *fwnode,
1569                                unsigned int index)
1570 {
1571         struct resource res;
1572         int ret;
1573
1574         ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1575         if (ret)
1576                 return ret;
1577
1578         return res.start;
1579 }
1580
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,                         \
1606         };                                                              \
1607         EXPORT_SYMBOL_GPL(ops)
1608
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;
1612
1613 bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1614 {
1615         return !IS_ERR_OR_NULL(fwnode) &&
1616                 fwnode->ops == &acpi_device_fwnode_ops;
1617 }
1618 EXPORT_SYMBOL(is_acpi_device_node);
1619
1620 bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1621 {
1622         return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1623 }
1624 EXPORT_SYMBOL(is_acpi_data_node);