• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ACPI device specific properties support.
4  *
5  * Copyright (C) 2014 - 2023, 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  *          Sakari Ailus <sakari.ailus@linux.intel.com>
12  */
13 
14 #define pr_fmt(fmt) "ACPI: " fmt
15 
16 #include <linux/acpi.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
19 
20 #include "internal.h"
21 
22 static int acpi_data_get_property_array(const struct acpi_device_data *data,
23 					const char *name,
24 					acpi_object_type type,
25 					const union acpi_object **obj);
26 
27 /*
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.
34  *
35  * References:
36  *
37  * [1] UEFI DSD Guide.
38  *     https://github.com/UEFI/DSD-Guide/blob/main/src/dsd-guide.adoc
39  */
40 static const guid_t prp_guids[] = {
41 	/* ACPI _DSD device properties GUID [1]: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
42 	GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
43 		  0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
44 	/* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
45 	GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
46 		  0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
47 	/* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */
48 	GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3,
49 		  0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89),
50 	/* Thunderbolt GUID for IMR_VALID: c44d002f-69f9-4e7d-a904-a7baabdf43f7 */
51 	GUID_INIT(0xc44d002f, 0x69f9, 0x4e7d,
52 		  0xa9, 0x04, 0xa7, 0xba, 0xab, 0xdf, 0x43, 0xf7),
53 	/* Thunderbolt GUID for WAKE_SUPPORTED: 6c501103-c189-4296-ba72-9bf5a26ebe5d */
54 	GUID_INIT(0x6c501103, 0xc189, 0x4296,
55 		  0xba, 0x72, 0x9b, 0xf5, 0xa2, 0x6e, 0xbe, 0x5d),
56 	/* Storage device needs D3 GUID: 5025030f-842f-4ab4-a561-99a5189762d0 */
57 	GUID_INIT(0x5025030f, 0x842f, 0x4ab4,
58 		  0xa5, 0x61, 0x99, 0xa5, 0x18, 0x97, 0x62, 0xd0),
59 	/* DmaProperty for PCI devices GUID: 70d24161-6dd5-4c9e-8070-705531292865 */
60 	GUID_INIT(0x70d24161, 0x6dd5, 0x4c9e,
61 		  0x80, 0x70, 0x70, 0x55, 0x31, 0x29, 0x28, 0x65),
62 };
63 
64 /* ACPI _DSD data subnodes GUID [1]: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
65 static const guid_t ads_guid =
66 	GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
67 		  0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
68 
69 /* ACPI _DSD data buffer GUID [1]: edb12dd0-363d-4085-a3d2-49522ca160c4 */
70 static const guid_t buffer_prop_guid =
71 	GUID_INIT(0xedb12dd0, 0x363d, 0x4085,
72 		  0xa3, 0xd2, 0x49, 0x52, 0x2c, 0xa1, 0x60, 0xc4);
73 
74 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
75 					   union acpi_object *desc,
76 					   struct acpi_device_data *data,
77 					   struct fwnode_handle *parent);
78 static bool acpi_extract_properties(acpi_handle handle,
79 				    union acpi_object *desc,
80 				    struct acpi_device_data *data);
81 
acpi_nondev_subnode_extract(union acpi_object * desc,acpi_handle handle,const union acpi_object * link,struct list_head * list,struct fwnode_handle * parent)82 static bool acpi_nondev_subnode_extract(union acpi_object *desc,
83 					acpi_handle handle,
84 					const union acpi_object *link,
85 					struct list_head *list,
86 					struct fwnode_handle *parent)
87 {
88 	struct acpi_data_node *dn;
89 	bool result;
90 
91 	if (acpi_graph_ignore_port(handle))
92 		return false;
93 
94 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
95 	if (!dn)
96 		return false;
97 
98 	dn->name = link->package.elements[0].string.pointer;
99 	fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops);
100 	dn->parent = parent;
101 	INIT_LIST_HEAD(&dn->data.properties);
102 	INIT_LIST_HEAD(&dn->data.subnodes);
103 
104 	result = acpi_extract_properties(handle, desc, &dn->data);
105 
106 	if (handle) {
107 		acpi_handle scope;
108 		acpi_status status;
109 
110 		/*
111 		 * The scope for the subnode object lookup is the one of the
112 		 * namespace node (device) containing the object that has
113 		 * returned the package.  That is, it's the scope of that
114 		 * object's parent.
115 		 */
116 		status = acpi_get_parent(handle, &scope);
117 		if (ACPI_SUCCESS(status)
118 		    && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
119 						      &dn->fwnode))
120 			result = true;
121 	} else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
122 						  &dn->fwnode)) {
123 		result = true;
124 	}
125 
126 	if (result) {
127 		dn->handle = handle;
128 		dn->data.pointer = desc;
129 		list_add_tail(&dn->sibling, list);
130 		return true;
131 	}
132 
133 	kfree(dn);
134 	acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
135 	return false;
136 }
137 
acpi_nondev_subnode_data_ok(acpi_handle handle,const union acpi_object * link,struct list_head * list,struct fwnode_handle * parent)138 static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
139 					const union acpi_object *link,
140 					struct list_head *list,
141 					struct fwnode_handle *parent)
142 {
143 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
144 	acpi_status status;
145 
146 	status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
147 					    ACPI_TYPE_PACKAGE);
148 	if (ACPI_FAILURE(status))
149 		return false;
150 
151 	if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
152 					parent))
153 		return true;
154 
155 	ACPI_FREE(buf.pointer);
156 	return false;
157 }
158 
acpi_nondev_subnode_ok(acpi_handle scope,const union acpi_object * link,struct list_head * list,struct fwnode_handle * parent)159 static bool acpi_nondev_subnode_ok(acpi_handle scope,
160 				   const union acpi_object *link,
161 				   struct list_head *list,
162 				   struct fwnode_handle *parent)
163 {
164 	acpi_handle handle;
165 	acpi_status status;
166 
167 	if (!scope)
168 		return false;
169 
170 	status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
171 				 &handle);
172 	if (ACPI_FAILURE(status))
173 		return false;
174 
175 	return acpi_nondev_subnode_data_ok(handle, link, list, parent);
176 }
177 
acpi_add_nondev_subnodes(acpi_handle scope,union acpi_object * links,struct list_head * list,struct fwnode_handle * parent)178 static bool acpi_add_nondev_subnodes(acpi_handle scope,
179 				     union acpi_object *links,
180 				     struct list_head *list,
181 				     struct fwnode_handle *parent)
182 {
183 	bool ret = false;
184 	int i;
185 
186 	for (i = 0; i < links->package.count; i++) {
187 		union acpi_object *link, *desc;
188 		acpi_handle handle;
189 		bool result;
190 
191 		link = &links->package.elements[i];
192 		/* Only two elements allowed. */
193 		if (link->package.count != 2)
194 			continue;
195 
196 		/* The first one must be a string. */
197 		if (link->package.elements[0].type != ACPI_TYPE_STRING)
198 			continue;
199 
200 		/* The second one may be a string, a reference or a package. */
201 		switch (link->package.elements[1].type) {
202 		case ACPI_TYPE_STRING:
203 			result = acpi_nondev_subnode_ok(scope, link, list,
204 							 parent);
205 			break;
206 		case ACPI_TYPE_LOCAL_REFERENCE:
207 			handle = link->package.elements[1].reference.handle;
208 			result = acpi_nondev_subnode_data_ok(handle, link, list,
209 							     parent);
210 			break;
211 		case ACPI_TYPE_PACKAGE:
212 			desc = &link->package.elements[1];
213 			result = acpi_nondev_subnode_extract(desc, NULL, link,
214 							     list, parent);
215 			break;
216 		default:
217 			result = false;
218 			break;
219 		}
220 		ret = ret || result;
221 	}
222 
223 	return ret;
224 }
225 
acpi_enumerate_nondev_subnodes(acpi_handle scope,union acpi_object * desc,struct acpi_device_data * data,struct fwnode_handle * parent)226 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
227 					   union acpi_object *desc,
228 					   struct acpi_device_data *data,
229 					   struct fwnode_handle *parent)
230 {
231 	int i;
232 
233 	/* Look for the ACPI data subnodes GUID. */
234 	for (i = 0; i < desc->package.count; i += 2) {
235 		const union acpi_object *guid;
236 		union acpi_object *links;
237 
238 		guid = &desc->package.elements[i];
239 		links = &desc->package.elements[i + 1];
240 
241 		/*
242 		 * The first element must be a GUID and the second one must be
243 		 * a package.
244 		 */
245 		if (guid->type != ACPI_TYPE_BUFFER ||
246 		    guid->buffer.length != 16 ||
247 		    links->type != ACPI_TYPE_PACKAGE)
248 			break;
249 
250 		if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
251 			continue;
252 
253 		return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
254 						parent);
255 	}
256 
257 	return false;
258 }
259 
acpi_property_value_ok(const union acpi_object * value)260 static bool acpi_property_value_ok(const union acpi_object *value)
261 {
262 	int j;
263 
264 	/*
265 	 * The value must be an integer, a string, a reference, or a package
266 	 * whose every element must be an integer, a string, or a reference.
267 	 */
268 	switch (value->type) {
269 	case ACPI_TYPE_INTEGER:
270 	case ACPI_TYPE_STRING:
271 	case ACPI_TYPE_LOCAL_REFERENCE:
272 		return true;
273 
274 	case ACPI_TYPE_PACKAGE:
275 		for (j = 0; j < value->package.count; j++)
276 			switch (value->package.elements[j].type) {
277 			case ACPI_TYPE_INTEGER:
278 			case ACPI_TYPE_STRING:
279 			case ACPI_TYPE_LOCAL_REFERENCE:
280 				continue;
281 
282 			default:
283 				return false;
284 			}
285 
286 		return true;
287 	}
288 	return false;
289 }
290 
acpi_properties_format_valid(const union acpi_object * properties)291 static bool acpi_properties_format_valid(const union acpi_object *properties)
292 {
293 	int i;
294 
295 	for (i = 0; i < properties->package.count; i++) {
296 		const union acpi_object *property;
297 
298 		property = &properties->package.elements[i];
299 		/*
300 		 * Only two elements allowed, the first one must be a string and
301 		 * the second one has to satisfy certain conditions.
302 		 */
303 		if (property->package.count != 2
304 		    || property->package.elements[0].type != ACPI_TYPE_STRING
305 		    || !acpi_property_value_ok(&property->package.elements[1]))
306 			return false;
307 	}
308 	return true;
309 }
310 
acpi_init_of_compatible(struct acpi_device * adev)311 static void acpi_init_of_compatible(struct acpi_device *adev)
312 {
313 	const union acpi_object *of_compatible;
314 	int ret;
315 
316 	ret = acpi_data_get_property_array(&adev->data, "compatible",
317 					   ACPI_TYPE_STRING, &of_compatible);
318 	if (ret) {
319 		ret = acpi_dev_get_property(adev, "compatible",
320 					    ACPI_TYPE_STRING, &of_compatible);
321 		if (ret) {
322 			struct acpi_device *parent;
323 
324 			parent = acpi_dev_parent(adev);
325 			if (parent && parent->flags.of_compatible_ok)
326 				goto out;
327 
328 			return;
329 		}
330 	}
331 	adev->data.of_compatible = of_compatible;
332 
333  out:
334 	adev->flags.of_compatible_ok = 1;
335 }
336 
acpi_is_property_guid(const guid_t * guid)337 static bool acpi_is_property_guid(const guid_t *guid)
338 {
339 	int i;
340 
341 	for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
342 		if (guid_equal(guid, &prp_guids[i]))
343 			return true;
344 	}
345 
346 	return false;
347 }
348 
349 struct acpi_device_properties *
acpi_data_add_props(struct acpi_device_data * data,const guid_t * guid,union acpi_object * properties)350 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
351 		    union acpi_object *properties)
352 {
353 	struct acpi_device_properties *props;
354 
355 	props = kzalloc(sizeof(*props), GFP_KERNEL);
356 	if (props) {
357 		INIT_LIST_HEAD(&props->list);
358 		props->guid = guid;
359 		props->properties = properties;
360 		list_add_tail(&props->list, &data->properties);
361 	}
362 
363 	return props;
364 }
365 
acpi_nondev_subnode_tag(acpi_handle handle,void * context)366 static void acpi_nondev_subnode_tag(acpi_handle handle, void *context)
367 {
368 }
369 
acpi_untie_nondev_subnodes(struct acpi_device_data * data)370 static void acpi_untie_nondev_subnodes(struct acpi_device_data *data)
371 {
372 	struct acpi_data_node *dn;
373 
374 	list_for_each_entry(dn, &data->subnodes, sibling) {
375 		acpi_detach_data(dn->handle, acpi_nondev_subnode_tag);
376 
377 		acpi_untie_nondev_subnodes(&dn->data);
378 	}
379 }
380 
acpi_tie_nondev_subnodes(struct acpi_device_data * data)381 static bool acpi_tie_nondev_subnodes(struct acpi_device_data *data)
382 {
383 	struct acpi_data_node *dn;
384 
385 	list_for_each_entry(dn, &data->subnodes, sibling) {
386 		acpi_status status;
387 		bool ret;
388 
389 		status = acpi_attach_data(dn->handle, acpi_nondev_subnode_tag, dn);
390 		if (ACPI_FAILURE(status) && status != AE_ALREADY_EXISTS) {
391 			acpi_handle_err(dn->handle, "Can't tag data node\n");
392 			return false;
393 		}
394 
395 		ret = acpi_tie_nondev_subnodes(&dn->data);
396 		if (!ret)
397 			return ret;
398 	}
399 
400 	return true;
401 }
402 
acpi_data_add_buffer_props(acpi_handle handle,struct acpi_device_data * data,union acpi_object * properties)403 static void acpi_data_add_buffer_props(acpi_handle handle,
404 				       struct acpi_device_data *data,
405 				       union acpi_object *properties)
406 {
407 	struct acpi_device_properties *props;
408 	union acpi_object *package;
409 	size_t alloc_size;
410 	unsigned int i;
411 	u32 *count;
412 
413 	if (check_mul_overflow((size_t)properties->package.count,
414 			       sizeof(*package) + sizeof(void *),
415 			       &alloc_size) ||
416 	    check_add_overflow(sizeof(*props) + sizeof(*package), alloc_size,
417 			       &alloc_size)) {
418 		acpi_handle_warn(handle,
419 				 "can't allocate memory for %u buffer props",
420 				 properties->package.count);
421 		return;
422 	}
423 
424 	props = kvzalloc(alloc_size, GFP_KERNEL);
425 	if (!props)
426 		return;
427 
428 	props->guid = &buffer_prop_guid;
429 	props->bufs = (void *)(props + 1);
430 	props->properties = (void *)(props->bufs + properties->package.count);
431 
432 	/* Outer package */
433 	package = props->properties;
434 	package->type = ACPI_TYPE_PACKAGE;
435 	package->package.elements = package + 1;
436 	count = &package->package.count;
437 	*count = 0;
438 
439 	/* Inner packages */
440 	package++;
441 
442 	for (i = 0; i < properties->package.count; i++) {
443 		struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
444 		union acpi_object *property = &properties->package.elements[i];
445 		union acpi_object *prop, *obj, *buf_obj;
446 		acpi_status status;
447 
448 		if (property->type != ACPI_TYPE_PACKAGE ||
449 		    property->package.count != 2) {
450 			acpi_handle_warn(handle,
451 					 "buffer property %u has %u entries\n",
452 					 i, property->package.count);
453 			continue;
454 		}
455 
456 		prop = &property->package.elements[0];
457 		obj = &property->package.elements[1];
458 
459 		if (prop->type != ACPI_TYPE_STRING ||
460 		    obj->type != ACPI_TYPE_STRING) {
461 			acpi_handle_warn(handle,
462 					 "wrong object types %u and %u\n",
463 					 prop->type, obj->type);
464 			continue;
465 		}
466 
467 		status = acpi_evaluate_object_typed(handle, obj->string.pointer,
468 						    NULL, &buf,
469 						    ACPI_TYPE_BUFFER);
470 		if (ACPI_FAILURE(status)) {
471 			acpi_handle_warn(handle,
472 					 "can't evaluate \"%*pE\" as buffer\n",
473 					 obj->string.length,
474 					 obj->string.pointer);
475 			continue;
476 		}
477 
478 		package->type = ACPI_TYPE_PACKAGE;
479 		package->package.elements = prop;
480 		package->package.count = 2;
481 
482 		buf_obj = buf.pointer;
483 
484 		/* Replace the string object with a buffer object */
485 		obj->type = ACPI_TYPE_BUFFER;
486 		obj->buffer.length = buf_obj->buffer.length;
487 		obj->buffer.pointer = buf_obj->buffer.pointer;
488 
489 		props->bufs[i] = buf.pointer;
490 		package++;
491 		(*count)++;
492 	}
493 
494 	if (*count)
495 		list_add(&props->list, &data->properties);
496 	else
497 		kvfree(props);
498 }
499 
acpi_extract_properties(acpi_handle scope,union acpi_object * desc,struct acpi_device_data * data)500 static bool acpi_extract_properties(acpi_handle scope, union acpi_object *desc,
501 				    struct acpi_device_data *data)
502 {
503 	int i;
504 
505 	if (desc->package.count % 2)
506 		return false;
507 
508 	/* Look for the device properties GUID. */
509 	for (i = 0; i < desc->package.count; i += 2) {
510 		const union acpi_object *guid;
511 		union acpi_object *properties;
512 
513 		guid = &desc->package.elements[i];
514 		properties = &desc->package.elements[i + 1];
515 
516 		/*
517 		 * The first element must be a GUID and the second one must be
518 		 * a package.
519 		 */
520 		if (guid->type != ACPI_TYPE_BUFFER ||
521 		    guid->buffer.length != 16 ||
522 		    properties->type != ACPI_TYPE_PACKAGE)
523 			break;
524 
525 		if (guid_equal((guid_t *)guid->buffer.pointer,
526 			       &buffer_prop_guid)) {
527 			acpi_data_add_buffer_props(scope, data, properties);
528 			continue;
529 		}
530 
531 		if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
532 			continue;
533 
534 		/*
535 		 * We found the matching GUID. Now validate the format of the
536 		 * package immediately following it.
537 		 */
538 		if (!acpi_properties_format_valid(properties))
539 			continue;
540 
541 		acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
542 				    properties);
543 	}
544 
545 	return !list_empty(&data->properties);
546 }
547 
acpi_init_properties(struct acpi_device * adev)548 void acpi_init_properties(struct acpi_device *adev)
549 {
550 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
551 	struct acpi_hardware_id *hwid;
552 	acpi_status status;
553 	bool acpi_of = false;
554 
555 	INIT_LIST_HEAD(&adev->data.properties);
556 	INIT_LIST_HEAD(&adev->data.subnodes);
557 
558 	if (!adev->handle)
559 		return;
560 
561 	/*
562 	 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
563 	 * Device Tree compatible properties for this device.
564 	 */
565 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
566 		if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
567 			acpi_of = true;
568 			break;
569 		}
570 	}
571 
572 	status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
573 					    ACPI_TYPE_PACKAGE);
574 	if (ACPI_FAILURE(status))
575 		goto out;
576 
577 	if (acpi_extract_properties(adev->handle, buf.pointer, &adev->data)) {
578 		adev->data.pointer = buf.pointer;
579 		if (acpi_of)
580 			acpi_init_of_compatible(adev);
581 	}
582 	if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
583 					&adev->data, acpi_fwnode_handle(adev)))
584 		adev->data.pointer = buf.pointer;
585 
586 	if (!adev->data.pointer) {
587 		acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
588 		ACPI_FREE(buf.pointer);
589 	} else {
590 		if (!acpi_tie_nondev_subnodes(&adev->data))
591 			acpi_untie_nondev_subnodes(&adev->data);
592 	}
593 
594  out:
595 	if (acpi_of && !adev->flags.of_compatible_ok)
596 		acpi_handle_info(adev->handle,
597 			 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
598 
599 	if (!adev->data.pointer)
600 		acpi_extract_apple_properties(adev);
601 }
602 
acpi_free_device_properties(struct list_head * list)603 static void acpi_free_device_properties(struct list_head *list)
604 {
605 	struct acpi_device_properties *props, *tmp;
606 
607 	list_for_each_entry_safe(props, tmp, list, list) {
608 		u32 i;
609 
610 		list_del(&props->list);
611 		/* Buffer data properties were separately allocated */
612 		if (props->bufs)
613 			for (i = 0; i < props->properties->package.count; i++)
614 				ACPI_FREE(props->bufs[i]);
615 		kvfree(props);
616 	}
617 }
618 
acpi_destroy_nondev_subnodes(struct list_head * list)619 static void acpi_destroy_nondev_subnodes(struct list_head *list)
620 {
621 	struct acpi_data_node *dn, *next;
622 
623 	if (list_empty(list))
624 		return;
625 
626 	list_for_each_entry_safe_reverse(dn, next, list, sibling) {
627 		acpi_destroy_nondev_subnodes(&dn->data.subnodes);
628 		wait_for_completion(&dn->kobj_done);
629 		list_del(&dn->sibling);
630 		ACPI_FREE((void *)dn->data.pointer);
631 		acpi_free_device_properties(&dn->data.properties);
632 		kfree(dn);
633 	}
634 }
635 
acpi_free_properties(struct acpi_device * adev)636 void acpi_free_properties(struct acpi_device *adev)
637 {
638 	acpi_untie_nondev_subnodes(&adev->data);
639 	acpi_destroy_nondev_subnodes(&adev->data.subnodes);
640 	ACPI_FREE((void *)adev->data.pointer);
641 	adev->data.of_compatible = NULL;
642 	adev->data.pointer = NULL;
643 	acpi_free_device_properties(&adev->data.properties);
644 }
645 
646 /**
647  * acpi_data_get_property - return an ACPI property with given name
648  * @data: ACPI device deta object to get the property from
649  * @name: Name of the property
650  * @type: Expected property type
651  * @obj: Location to store the property value (if not %NULL)
652  *
653  * Look up a property with @name and store a pointer to the resulting ACPI
654  * object at the location pointed to by @obj if found.
655  *
656  * Callers must not attempt to free the returned objects.  These objects will be
657  * freed by the ACPI core automatically during the removal of @data.
658  *
659  * Return: %0 if property with @name has been found (success),
660  *         %-EINVAL if the arguments are invalid,
661  *         %-EINVAL if the property doesn't exist,
662  *         %-EPROTO if the property value type doesn't match @type.
663  */
acpi_data_get_property(const struct acpi_device_data * data,const char * name,acpi_object_type type,const union acpi_object ** obj)664 static int acpi_data_get_property(const struct acpi_device_data *data,
665 				  const char *name, acpi_object_type type,
666 				  const union acpi_object **obj)
667 {
668 	const struct acpi_device_properties *props;
669 
670 	if (!data || !name)
671 		return -EINVAL;
672 
673 	if (!data->pointer || list_empty(&data->properties))
674 		return -EINVAL;
675 
676 	list_for_each_entry(props, &data->properties, list) {
677 		const union acpi_object *properties;
678 		unsigned int i;
679 
680 		properties = props->properties;
681 		for (i = 0; i < properties->package.count; i++) {
682 			const union acpi_object *propname, *propvalue;
683 			const union acpi_object *property;
684 
685 			property = &properties->package.elements[i];
686 
687 			propname = &property->package.elements[0];
688 			propvalue = &property->package.elements[1];
689 
690 			if (!strcmp(name, propname->string.pointer)) {
691 				if (type != ACPI_TYPE_ANY &&
692 				    propvalue->type != type)
693 					return -EPROTO;
694 				if (obj)
695 					*obj = propvalue;
696 
697 				return 0;
698 			}
699 		}
700 	}
701 	return -EINVAL;
702 }
703 
704 /**
705  * acpi_dev_get_property - return an ACPI property with given name.
706  * @adev: ACPI device to get the property from.
707  * @name: Name of the property.
708  * @type: Expected property type.
709  * @obj: Location to store the property value (if not %NULL).
710  */
acpi_dev_get_property(const struct acpi_device * adev,const char * name,acpi_object_type type,const union acpi_object ** obj)711 int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
712 			  acpi_object_type type, const union acpi_object **obj)
713 {
714 	return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
715 }
716 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
717 
718 static const struct acpi_device_data *
acpi_device_data_of_node(const struct fwnode_handle * fwnode)719 acpi_device_data_of_node(const struct fwnode_handle *fwnode)
720 {
721 	if (is_acpi_device_node(fwnode)) {
722 		const struct acpi_device *adev = to_acpi_device_node(fwnode);
723 		return &adev->data;
724 	}
725 	if (is_acpi_data_node(fwnode)) {
726 		const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
727 		return &dn->data;
728 	}
729 	return NULL;
730 }
731 
732 /**
733  * acpi_node_prop_get - return an ACPI property with given name.
734  * @fwnode: Firmware node to get the property from.
735  * @propname: Name of the property.
736  * @valptr: Location to store a pointer to the property value (if not %NULL).
737  */
acpi_node_prop_get(const struct fwnode_handle * fwnode,const char * propname,void ** valptr)738 int acpi_node_prop_get(const struct fwnode_handle *fwnode,
739 		       const char *propname, void **valptr)
740 {
741 	return acpi_data_get_property(acpi_device_data_of_node(fwnode),
742 				      propname, ACPI_TYPE_ANY,
743 				      (const union acpi_object **)valptr);
744 }
745 
746 /**
747  * acpi_data_get_property_array - return an ACPI array property with given name
748  * @data: ACPI data object to get the property from
749  * @name: Name of the property
750  * @type: Expected type of array elements
751  * @obj: Location to store a pointer to the property value (if not NULL)
752  *
753  * Look up an array property with @name and store a pointer to the resulting
754  * ACPI object at the location pointed to by @obj if found.
755  *
756  * Callers must not attempt to free the returned objects.  Those objects will be
757  * freed by the ACPI core automatically during the removal of @data.
758  *
759  * Return: %0 if array property (package) with @name has been found (success),
760  *         %-EINVAL if the arguments are invalid,
761  *         %-EINVAL if the property doesn't exist,
762  *         %-EPROTO if the property is not a package or the type of its elements
763  *           doesn't match @type.
764  */
acpi_data_get_property_array(const struct acpi_device_data * data,const char * name,acpi_object_type type,const union acpi_object ** obj)765 static int acpi_data_get_property_array(const struct acpi_device_data *data,
766 					const char *name,
767 					acpi_object_type type,
768 					const union acpi_object **obj)
769 {
770 	const union acpi_object *prop;
771 	int ret, i;
772 
773 	ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
774 	if (ret)
775 		return ret;
776 
777 	if (type != ACPI_TYPE_ANY) {
778 		/* Check that all elements are of correct type. */
779 		for (i = 0; i < prop->package.count; i++)
780 			if (prop->package.elements[i].type != type)
781 				return -EPROTO;
782 	}
783 	if (obj)
784 		*obj = prop;
785 
786 	return 0;
787 }
788 
789 static struct fwnode_handle *
acpi_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)790 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
791 				 const char *childname)
792 {
793 	struct fwnode_handle *child;
794 
795 	fwnode_for_each_child_node(fwnode, child) {
796 		if (is_acpi_data_node(child)) {
797 			if (acpi_data_node_match(child, childname))
798 				return child;
799 			continue;
800 		}
801 
802 		if (!strncmp(acpi_device_bid(to_acpi_device_node(child)),
803 			     childname, ACPI_NAMESEG_SIZE))
804 			return child;
805 	}
806 
807 	return NULL;
808 }
809 
acpi_get_ref_args(struct fwnode_reference_args * args,struct fwnode_handle * ref_fwnode,const union acpi_object ** element,const union acpi_object * end,size_t num_args)810 static int acpi_get_ref_args(struct fwnode_reference_args *args,
811 			     struct fwnode_handle *ref_fwnode,
812 			     const union acpi_object **element,
813 			     const union acpi_object *end, size_t num_args)
814 {
815 	u32 nargs = 0, i;
816 
817 	/*
818 	 * Assume the following integer elements are all args. Stop counting on
819 	 * the first reference (possibly represented as a string) or end of the
820 	 * package arguments. In case of neither reference, nor integer, return
821 	 * an error, we can't parse it.
822 	 */
823 	for (i = 0; (*element) + i < end && i < num_args; i++) {
824 		acpi_object_type type = (*element)[i].type;
825 
826 		if (type == ACPI_TYPE_LOCAL_REFERENCE || type == ACPI_TYPE_STRING)
827 			break;
828 
829 		if (type == ACPI_TYPE_INTEGER)
830 			nargs++;
831 		else
832 			return -EINVAL;
833 	}
834 
835 	if (nargs > NR_FWNODE_REFERENCE_ARGS)
836 		return -EINVAL;
837 
838 	if (args) {
839 		args->fwnode = ref_fwnode;
840 		args->nargs = nargs;
841 		for (i = 0; i < nargs; i++)
842 			args->args[i] = (*element)[i].integer.value;
843 	}
844 
845 	(*element) += nargs;
846 
847 	return 0;
848 }
849 
acpi_parse_string_ref(const struct fwnode_handle * fwnode,const char * refstring)850 static struct fwnode_handle *acpi_parse_string_ref(const struct fwnode_handle *fwnode,
851 						   const char *refstring)
852 {
853 	acpi_handle scope, handle;
854 	struct acpi_data_node *dn;
855 	struct acpi_device *device;
856 	acpi_status status;
857 
858 	if (is_acpi_device_node(fwnode)) {
859 		scope = to_acpi_device_node(fwnode)->handle;
860 	} else if (is_acpi_data_node(fwnode)) {
861 		scope = to_acpi_data_node(fwnode)->handle;
862 	} else {
863 		pr_debug("Bad node type for node %pfw\n", fwnode);
864 		return NULL;
865 	}
866 
867 	status = acpi_get_handle(scope, refstring, &handle);
868 	if (ACPI_FAILURE(status)) {
869 		acpi_handle_debug(scope, "Unable to get an ACPI handle for %s\n",
870 				  refstring);
871 		return NULL;
872 	}
873 
874 	device = acpi_fetch_acpi_dev(handle);
875 	if (device)
876 		return acpi_fwnode_handle(device);
877 
878 	status = acpi_get_data_full(handle, acpi_nondev_subnode_tag,
879 				    (void **)&dn, NULL);
880 	if (ACPI_FAILURE(status) || !dn) {
881 		acpi_handle_debug(handle, "Subnode not found\n");
882 		return NULL;
883 	}
884 
885 	return &dn->fwnode;
886 }
887 
888 /**
889  * __acpi_node_get_property_reference - returns handle to the referenced object
890  * @fwnode: Firmware node to get the property from
891  * @propname: Name of the property
892  * @index: Index of the reference to return
893  * @num_args: Maximum number of arguments after each reference
894  * @args: Location to store the returned reference with optional arguments
895  *	  (may be NULL)
896  *
897  * Find property with @name, verifify that it is a package containing at least
898  * one object reference and if so, store the ACPI device object pointer to the
899  * target object in @args->adev.  If the reference includes arguments, store
900  * them in the @args->args[] array.
901  *
902  * If there's more than one reference in the property value package, @index is
903  * used to select the one to return.
904  *
905  * It is possible to leave holes in the property value set like in the
906  * example below:
907  *
908  * Package () {
909  *     "cs-gpios",
910  *     Package () {
911  *        ^GPIO, 19, 0, 0,
912  *        ^GPIO, 20, 0, 0,
913  *        0,
914  *        ^GPIO, 21, 0, 0,
915  *     }
916  * }
917  *
918  * Calling this function with index %2 or index %3 return %-ENOENT. If the
919  * property does not contain any more values %-ENOENT is returned. The NULL
920  * entry must be single integer and preferably contain value %0.
921  *
922  * Return: %0 on success, negative error code on failure.
923  */
__acpi_node_get_property_reference(const struct fwnode_handle * fwnode,const char * propname,size_t index,size_t num_args,struct fwnode_reference_args * args)924 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
925 	const char *propname, size_t index, size_t num_args,
926 	struct fwnode_reference_args *args)
927 {
928 	const union acpi_object *element, *end;
929 	const union acpi_object *obj;
930 	const struct acpi_device_data *data;
931 	struct fwnode_handle *ref_fwnode;
932 	struct acpi_device *device;
933 	int ret, idx = 0;
934 
935 	data = acpi_device_data_of_node(fwnode);
936 	if (!data)
937 		return -ENOENT;
938 
939 	ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
940 	if (ret)
941 		return ret == -EINVAL ? -ENOENT : -EINVAL;
942 
943 	switch (obj->type) {
944 	case ACPI_TYPE_LOCAL_REFERENCE:
945 		/* Plain single reference without arguments. */
946 		if (index)
947 			return -ENOENT;
948 
949 		device = acpi_fetch_acpi_dev(obj->reference.handle);
950 		if (!device)
951 			return -EINVAL;
952 
953 		if (!args)
954 			return 0;
955 
956 		args->fwnode = acpi_fwnode_handle(device);
957 		args->nargs = 0;
958 
959 		return 0;
960 	case ACPI_TYPE_STRING:
961 		if (index)
962 			return -ENOENT;
963 
964 		ref_fwnode = acpi_parse_string_ref(fwnode, obj->string.pointer);
965 		if (!ref_fwnode)
966 			return -EINVAL;
967 
968 		args->fwnode = ref_fwnode;
969 		args->nargs = 0;
970 
971 		return 0;
972 	case ACPI_TYPE_PACKAGE:
973 		/*
974 		 * If it is not a single reference, then it is a package of
975 		 * references, followed by number of ints as follows:
976 		 *
977 		 *  Package () { REF, INT, REF, INT, INT }
978 		 *
979 		 * Here, REF may be either a local reference or a string. The
980 		 * index argument is then used to determine which reference the
981 		 * caller wants (along with the arguments).
982 		 */
983 		break;
984 	default:
985 		return -EINVAL;
986 	}
987 
988 	if (index >= obj->package.count)
989 		return -ENOENT;
990 
991 	element = obj->package.elements;
992 	end = element + obj->package.count;
993 
994 	while (element < end) {
995 		switch (element->type) {
996 		case ACPI_TYPE_LOCAL_REFERENCE:
997 			device = acpi_fetch_acpi_dev(element->reference.handle);
998 			if (!device)
999 				return -EINVAL;
1000 
1001 			element++;
1002 
1003 			ret = acpi_get_ref_args(idx == index ? args : NULL,
1004 						acpi_fwnode_handle(device),
1005 						&element, end, num_args);
1006 			if (ret < 0)
1007 				return ret;
1008 
1009 			if (idx == index)
1010 				return 0;
1011 
1012 			break;
1013 		case ACPI_TYPE_STRING:
1014 			ref_fwnode = acpi_parse_string_ref(fwnode,
1015 							   element->string.pointer);
1016 			if (!ref_fwnode)
1017 				return -EINVAL;
1018 
1019 			element++;
1020 
1021 			ret = acpi_get_ref_args(idx == index ? args : NULL,
1022 						ref_fwnode, &element, end,
1023 						num_args);
1024 			if (ret < 0)
1025 				return ret;
1026 
1027 			if (idx == index)
1028 				return 0;
1029 
1030 			break;
1031 		case ACPI_TYPE_INTEGER:
1032 			if (idx == index)
1033 				return -ENOENT;
1034 			element++;
1035 			break;
1036 		default:
1037 			return -EINVAL;
1038 		}
1039 
1040 		idx++;
1041 	}
1042 
1043 	return -ENOENT;
1044 }
1045 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
1046 
acpi_data_prop_read_single(const struct acpi_device_data * data,const char * propname,enum dev_prop_type proptype,void * val)1047 static int acpi_data_prop_read_single(const struct acpi_device_data *data,
1048 				      const char *propname,
1049 				      enum dev_prop_type proptype, void *val)
1050 {
1051 	const union acpi_object *obj;
1052 	int ret = 0;
1053 
1054 	if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
1055 		ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
1056 	else if (proptype == DEV_PROP_STRING)
1057 		ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
1058 	if (ret)
1059 		return ret;
1060 
1061 	switch (proptype) {
1062 	case DEV_PROP_U8:
1063 		if (obj->integer.value > U8_MAX)
1064 			return -EOVERFLOW;
1065 		if (val)
1066 			*(u8 *)val = obj->integer.value;
1067 		break;
1068 	case DEV_PROP_U16:
1069 		if (obj->integer.value > U16_MAX)
1070 			return -EOVERFLOW;
1071 		if (val)
1072 			*(u16 *)val = obj->integer.value;
1073 		break;
1074 	case DEV_PROP_U32:
1075 		if (obj->integer.value > U32_MAX)
1076 			return -EOVERFLOW;
1077 		if (val)
1078 			*(u32 *)val = obj->integer.value;
1079 		break;
1080 	case DEV_PROP_U64:
1081 		if (val)
1082 			*(u64 *)val = obj->integer.value;
1083 		break;
1084 	case DEV_PROP_STRING:
1085 		if (val)
1086 			*(char **)val = obj->string.pointer;
1087 		return 1;
1088 	default:
1089 		return -EINVAL;
1090 	}
1091 
1092 	/* When no storage provided return number of available values */
1093 	return val ? 0 : 1;
1094 }
1095 
1096 #define acpi_copy_property_array_uint(items, val, nval)			\
1097 	({								\
1098 		typeof(items) __items = items;				\
1099 		typeof(val) __val = val;				\
1100 		typeof(nval) __nval = nval;				\
1101 		size_t i;						\
1102 		int ret = 0;						\
1103 									\
1104 		for (i = 0; i < __nval; i++) {				\
1105 			if (__items->type == ACPI_TYPE_BUFFER) {	\
1106 				__val[i] = __items->buffer.pointer[i];	\
1107 				continue;				\
1108 			}						\
1109 			if (__items[i].type != ACPI_TYPE_INTEGER) {	\
1110 				ret = -EPROTO;				\
1111 				break;					\
1112 			}						\
1113 			if (__items[i].integer.value > _Generic(__val,	\
1114 								u8 *: U8_MAX, \
1115 								u16 *: U16_MAX, \
1116 								u32 *: U32_MAX, \
1117 								u64 *: U64_MAX)) { \
1118 				ret = -EOVERFLOW;			\
1119 				break;					\
1120 			}						\
1121 									\
1122 			__val[i] = __items[i].integer.value;		\
1123 		}							\
1124 		ret;							\
1125 	})
1126 
acpi_copy_property_array_string(const union acpi_object * items,char ** val,size_t nval)1127 static int acpi_copy_property_array_string(const union acpi_object *items,
1128 					   char **val, size_t nval)
1129 {
1130 	int i;
1131 
1132 	for (i = 0; i < nval; i++) {
1133 		if (items[i].type != ACPI_TYPE_STRING)
1134 			return -EPROTO;
1135 
1136 		val[i] = items[i].string.pointer;
1137 	}
1138 	return nval;
1139 }
1140 
acpi_data_prop_read(const struct acpi_device_data * data,const char * propname,enum dev_prop_type proptype,void * val,size_t nval)1141 static int acpi_data_prop_read(const struct acpi_device_data *data,
1142 			       const char *propname,
1143 			       enum dev_prop_type proptype,
1144 			       void *val, size_t nval)
1145 {
1146 	const union acpi_object *obj;
1147 	const union acpi_object *items;
1148 	int ret;
1149 
1150 	if (nval == 1 || !val) {
1151 		ret = acpi_data_prop_read_single(data, propname, proptype, val);
1152 		/*
1153 		 * The overflow error means that the property is there and it is
1154 		 * single-value, but its type does not match, so return.
1155 		 */
1156 		if (ret >= 0 || ret == -EOVERFLOW)
1157 			return ret;
1158 
1159 		/*
1160 		 * Reading this property as a single-value one failed, but its
1161 		 * value may still be represented as one-element array, so
1162 		 * continue.
1163 		 */
1164 	}
1165 
1166 	ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
1167 	if (ret && proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
1168 		ret = acpi_data_get_property(data, propname, ACPI_TYPE_BUFFER,
1169 					     &obj);
1170 	if (ret)
1171 		return ret;
1172 
1173 	if (!val) {
1174 		if (obj->type == ACPI_TYPE_BUFFER)
1175 			return obj->buffer.length;
1176 
1177 		return obj->package.count;
1178 	}
1179 
1180 	switch (proptype) {
1181 	case DEV_PROP_STRING:
1182 		break;
1183 	default:
1184 		if (obj->type == ACPI_TYPE_BUFFER) {
1185 			if (nval > obj->buffer.length)
1186 				return -EOVERFLOW;
1187 		} else {
1188 			if (nval > obj->package.count)
1189 				return -EOVERFLOW;
1190 		}
1191 		break;
1192 	}
1193 
1194 	if (obj->type == ACPI_TYPE_BUFFER) {
1195 		if (proptype != DEV_PROP_U8)
1196 			return -EPROTO;
1197 		items = obj;
1198 	} else {
1199 		items = obj->package.elements;
1200 	}
1201 
1202 	switch (proptype) {
1203 	case DEV_PROP_U8:
1204 		ret = acpi_copy_property_array_uint(items, (u8 *)val, nval);
1205 		break;
1206 	case DEV_PROP_U16:
1207 		ret = acpi_copy_property_array_uint(items, (u16 *)val, nval);
1208 		break;
1209 	case DEV_PROP_U32:
1210 		ret = acpi_copy_property_array_uint(items, (u32 *)val, nval);
1211 		break;
1212 	case DEV_PROP_U64:
1213 		ret = acpi_copy_property_array_uint(items, (u64 *)val, nval);
1214 		break;
1215 	case DEV_PROP_STRING:
1216 		nval = min_t(u32, nval, obj->package.count);
1217 		if (nval == 0)
1218 			return -ENODATA;
1219 
1220 		ret = acpi_copy_property_array_string(items, (char **)val, nval);
1221 		break;
1222 	default:
1223 		ret = -EINVAL;
1224 		break;
1225 	}
1226 	return ret;
1227 }
1228 
1229 /**
1230  * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
1231  * @fwnode: Firmware node to get the property from.
1232  * @propname: Name of the property.
1233  * @proptype: Expected property type.
1234  * @val: Location to store the property value (if not %NULL).
1235  * @nval: Size of the array pointed to by @val.
1236  *
1237  * If @val is %NULL, return the number of array elements comprising the value
1238  * of the property.  Otherwise, read at most @nval values to the array at the
1239  * location pointed to by @val.
1240  */
acpi_node_prop_read(const struct fwnode_handle * fwnode,const char * propname,enum dev_prop_type proptype,void * val,size_t nval)1241 static int acpi_node_prop_read(const struct fwnode_handle *fwnode,
1242 			       const char *propname, enum dev_prop_type proptype,
1243 			       void *val, size_t nval)
1244 {
1245 	return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
1246 				   propname, proptype, val, nval);
1247 }
1248 
stop_on_next(struct acpi_device * adev,void * data)1249 static int stop_on_next(struct acpi_device *adev, void *data)
1250 {
1251 	struct acpi_device **ret_p = data;
1252 
1253 	if (!*ret_p) {
1254 		*ret_p = adev;
1255 		return 1;
1256 	}
1257 
1258 	/* Skip until the "previous" object is found. */
1259 	if (*ret_p == adev)
1260 		*ret_p = NULL;
1261 
1262 	return 0;
1263 }
1264 
1265 /**
1266  * acpi_get_next_subnode - Return the next child node handle for a fwnode
1267  * @fwnode: Firmware node to find the next child node for.
1268  * @child: Handle to one of the device's child nodes or a null handle.
1269  */
acpi_get_next_subnode(const struct fwnode_handle * fwnode,struct fwnode_handle * child)1270 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
1271 					    struct fwnode_handle *child)
1272 {
1273 	struct acpi_device *adev = to_acpi_device_node(fwnode);
1274 
1275 	if ((!child || is_acpi_device_node(child)) && adev) {
1276 		struct acpi_device *child_adev = to_acpi_device_node(child);
1277 
1278 		acpi_dev_for_each_child(adev, stop_on_next, &child_adev);
1279 		if (child_adev)
1280 			return acpi_fwnode_handle(child_adev);
1281 
1282 		child = NULL;
1283 	}
1284 
1285 	if (!child || is_acpi_data_node(child)) {
1286 		const struct acpi_data_node *data = to_acpi_data_node(fwnode);
1287 		const struct list_head *head;
1288 		struct list_head *next;
1289 		struct acpi_data_node *dn;
1290 
1291 		/*
1292 		 * We can have a combination of device and data nodes, e.g. with
1293 		 * hierarchical _DSD properties. Make sure the adev pointer is
1294 		 * restored before going through data nodes, otherwise we will
1295 		 * be looking for data_nodes below the last device found instead
1296 		 * of the common fwnode shared by device_nodes and data_nodes.
1297 		 */
1298 		adev = to_acpi_device_node(fwnode);
1299 		if (adev)
1300 			head = &adev->data.subnodes;
1301 		else if (data)
1302 			head = &data->data.subnodes;
1303 		else
1304 			return NULL;
1305 
1306 		if (list_empty(head))
1307 			return NULL;
1308 
1309 		if (child) {
1310 			dn = to_acpi_data_node(child);
1311 			next = dn->sibling.next;
1312 			if (next == head)
1313 				return NULL;
1314 
1315 			dn = list_entry(next, struct acpi_data_node, sibling);
1316 		} else {
1317 			dn = list_first_entry(head, struct acpi_data_node, sibling);
1318 		}
1319 		return &dn->fwnode;
1320 	}
1321 	return NULL;
1322 }
1323 
1324 /**
1325  * acpi_node_get_parent - Return parent fwnode of this fwnode
1326  * @fwnode: Firmware node whose parent to get
1327  *
1328  * Returns parent node of an ACPI device or data firmware node or %NULL if
1329  * not available.
1330  */
1331 static struct fwnode_handle *
acpi_node_get_parent(const struct fwnode_handle * fwnode)1332 acpi_node_get_parent(const struct fwnode_handle *fwnode)
1333 {
1334 	if (is_acpi_data_node(fwnode)) {
1335 		/* All data nodes have parent pointer so just return that */
1336 		return to_acpi_data_node(fwnode)->parent;
1337 	}
1338 	if (is_acpi_device_node(fwnode)) {
1339 		struct acpi_device *parent;
1340 
1341 		parent = acpi_dev_parent(to_acpi_device_node(fwnode));
1342 		if (parent)
1343 			return acpi_fwnode_handle(parent);
1344 	}
1345 
1346 	return NULL;
1347 }
1348 
1349 /*
1350  * Return true if the node is an ACPI graph node. Called on either ports
1351  * or endpoints.
1352  */
is_acpi_graph_node(struct fwnode_handle * fwnode,const char * str)1353 static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1354 			       const char *str)
1355 {
1356 	unsigned int len = strlen(str);
1357 	const char *name;
1358 
1359 	if (!len || !is_acpi_data_node(fwnode))
1360 		return false;
1361 
1362 	name = to_acpi_data_node(fwnode)->name;
1363 
1364 	return (fwnode_property_present(fwnode, "reg") &&
1365 		!strncmp(name, str, len) && name[len] == '@') ||
1366 		fwnode_property_present(fwnode, str);
1367 }
1368 
1369 /**
1370  * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1371  * @fwnode: Pointer to the parent firmware node
1372  * @prev: Previous endpoint node or %NULL to get the first
1373  *
1374  * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1375  * %NULL if there is no next endpoint or in case of error. In case of success
1376  * the next endpoint is returned.
1377  */
acpi_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)1378 static struct fwnode_handle *acpi_graph_get_next_endpoint(
1379 	const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1380 {
1381 	struct fwnode_handle *port = NULL;
1382 	struct fwnode_handle *endpoint;
1383 
1384 	if (!prev) {
1385 		do {
1386 			port = fwnode_get_next_child_node(fwnode, port);
1387 			/*
1388 			 * The names of the port nodes begin with "port@"
1389 			 * followed by the number of the port node and they also
1390 			 * have a "reg" property that also has the number of the
1391 			 * port node. For compatibility reasons a node is also
1392 			 * recognised as a port node from the "port" property.
1393 			 */
1394 			if (is_acpi_graph_node(port, "port"))
1395 				break;
1396 		} while (port);
1397 	} else {
1398 		port = fwnode_get_parent(prev);
1399 	}
1400 
1401 	if (!port)
1402 		return NULL;
1403 
1404 	endpoint = fwnode_get_next_child_node(port, prev);
1405 	while (!endpoint) {
1406 		port = fwnode_get_next_child_node(fwnode, port);
1407 		if (!port)
1408 			break;
1409 		if (is_acpi_graph_node(port, "port"))
1410 			endpoint = fwnode_get_next_child_node(port, NULL);
1411 	}
1412 
1413 	/*
1414 	 * The names of the endpoint nodes begin with "endpoint@" followed by
1415 	 * the number of the endpoint node and they also have a "reg" property
1416 	 * that also has the number of the endpoint node. For compatibility
1417 	 * reasons a node is also recognised as an endpoint node from the
1418 	 * "endpoint" property.
1419 	 */
1420 	if (!is_acpi_graph_node(endpoint, "endpoint"))
1421 		return NULL;
1422 
1423 	return endpoint;
1424 }
1425 
1426 /**
1427  * acpi_graph_get_child_prop_value - Return a child with a given property value
1428  * @fwnode: device fwnode
1429  * @prop_name: The name of the property to look for
1430  * @val: the desired property value
1431  *
1432  * Return the port node corresponding to a given port number. Returns
1433  * the child node on success, NULL otherwise.
1434  */
acpi_graph_get_child_prop_value(const struct fwnode_handle * fwnode,const char * prop_name,unsigned int val)1435 static struct fwnode_handle *acpi_graph_get_child_prop_value(
1436 	const struct fwnode_handle *fwnode, const char *prop_name,
1437 	unsigned int val)
1438 {
1439 	struct fwnode_handle *child;
1440 
1441 	fwnode_for_each_child_node(fwnode, child) {
1442 		u32 nr;
1443 
1444 		if (fwnode_property_read_u32(child, prop_name, &nr))
1445 			continue;
1446 
1447 		if (val == nr)
1448 			return child;
1449 	}
1450 
1451 	return NULL;
1452 }
1453 
1454 
1455 /**
1456  * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint
1457  * @__fwnode: Endpoint firmware node pointing to a remote device
1458  *
1459  * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1460  */
1461 static struct fwnode_handle *
acpi_graph_get_remote_endpoint(const struct fwnode_handle * __fwnode)1462 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1463 {
1464 	struct fwnode_handle *fwnode;
1465 	unsigned int port_nr, endpoint_nr;
1466 	struct fwnode_reference_args args;
1467 	int ret;
1468 
1469 	memset(&args, 0, sizeof(args));
1470 	ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1471 					       &args);
1472 	if (ret)
1473 		return NULL;
1474 
1475 	/* Direct endpoint reference? */
1476 	if (!is_acpi_device_node(args.fwnode))
1477 		return args.nargs ? NULL : args.fwnode;
1478 
1479 	/*
1480 	 * Always require two arguments with the reference: port and
1481 	 * endpoint indices.
1482 	 */
1483 	if (args.nargs != 2)
1484 		return NULL;
1485 
1486 	fwnode = args.fwnode;
1487 	port_nr = args.args[0];
1488 	endpoint_nr = args.args[1];
1489 
1490 	fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1491 
1492 	return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1493 }
1494 
acpi_fwnode_device_is_available(const struct fwnode_handle * fwnode)1495 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1496 {
1497 	if (!is_acpi_device_node(fwnode))
1498 		return false;
1499 
1500 	return acpi_device_is_present(to_acpi_device_node(fwnode));
1501 }
1502 
1503 static const void *
acpi_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)1504 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1505 				  const struct device *dev)
1506 {
1507 	return acpi_device_get_match_data(dev);
1508 }
1509 
acpi_fwnode_device_dma_supported(const struct fwnode_handle * fwnode)1510 static bool acpi_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
1511 {
1512 	return acpi_dma_supported(to_acpi_device_node(fwnode));
1513 }
1514 
1515 static enum dev_dma_attr
acpi_fwnode_device_get_dma_attr(const struct fwnode_handle * fwnode)1516 acpi_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
1517 {
1518 	return acpi_get_dma_attr(to_acpi_device_node(fwnode));
1519 }
1520 
acpi_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)1521 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1522 					 const char *propname)
1523 {
1524 	return !acpi_node_prop_get(fwnode, propname, NULL);
1525 }
1526 
1527 static int
acpi_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)1528 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1529 				    const char *propname,
1530 				    unsigned int elem_size, void *val,
1531 				    size_t nval)
1532 {
1533 	enum dev_prop_type type;
1534 
1535 	switch (elem_size) {
1536 	case sizeof(u8):
1537 		type = DEV_PROP_U8;
1538 		break;
1539 	case sizeof(u16):
1540 		type = DEV_PROP_U16;
1541 		break;
1542 	case sizeof(u32):
1543 		type = DEV_PROP_U32;
1544 		break;
1545 	case sizeof(u64):
1546 		type = DEV_PROP_U64;
1547 		break;
1548 	default:
1549 		return -ENXIO;
1550 	}
1551 
1552 	return acpi_node_prop_read(fwnode, propname, type, val, nval);
1553 }
1554 
1555 static int
acpi_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)1556 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1557 				       const char *propname, const char **val,
1558 				       size_t nval)
1559 {
1560 	return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1561 				   val, nval);
1562 }
1563 
1564 static int
acpi_fwnode_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int args_count,unsigned int index,struct fwnode_reference_args * args)1565 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1566 			       const char *prop, const char *nargs_prop,
1567 			       unsigned int args_count, unsigned int index,
1568 			       struct fwnode_reference_args *args)
1569 {
1570 	return __acpi_node_get_property_reference(fwnode, prop, index,
1571 						  args_count, args);
1572 }
1573 
acpi_fwnode_get_name(const struct fwnode_handle * fwnode)1574 static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
1575 {
1576 	const struct acpi_device *adev;
1577 	struct fwnode_handle *parent;
1578 
1579 	/* Is this the root node? */
1580 	parent = fwnode_get_parent(fwnode);
1581 	if (!parent)
1582 		return "\\";
1583 
1584 	fwnode_handle_put(parent);
1585 
1586 	if (is_acpi_data_node(fwnode)) {
1587 		const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
1588 
1589 		return dn->name;
1590 	}
1591 
1592 	adev = to_acpi_device_node(fwnode);
1593 	if (WARN_ON(!adev))
1594 		return NULL;
1595 
1596 	return acpi_device_bid(adev);
1597 }
1598 
1599 static const char *
acpi_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)1600 acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1601 {
1602 	struct fwnode_handle *parent;
1603 
1604 	/* Is this the root node? */
1605 	parent = fwnode_get_parent(fwnode);
1606 	if (!parent)
1607 		return "";
1608 
1609 	/* Is this 2nd node from the root? */
1610 	parent = fwnode_get_next_parent(parent);
1611 	if (!parent)
1612 		return "";
1613 
1614 	fwnode_handle_put(parent);
1615 
1616 	/* ACPI device or data node. */
1617 	return ".";
1618 }
1619 
1620 static struct fwnode_handle *
acpi_fwnode_get_parent(struct fwnode_handle * fwnode)1621 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1622 {
1623 	return acpi_node_get_parent(fwnode);
1624 }
1625 
acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1626 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1627 					    struct fwnode_endpoint *endpoint)
1628 {
1629 	struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1630 
1631 	endpoint->local_fwnode = fwnode;
1632 
1633 	if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
1634 		fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1635 	if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
1636 		fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1637 
1638 	return 0;
1639 }
1640 
acpi_fwnode_irq_get(const struct fwnode_handle * fwnode,unsigned int index)1641 static int acpi_fwnode_irq_get(const struct fwnode_handle *fwnode,
1642 			       unsigned int index)
1643 {
1644 	struct resource res;
1645 	int ret;
1646 
1647 	ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1648 	if (ret)
1649 		return ret;
1650 
1651 	return res.start;
1652 }
1653 
1654 #define DECLARE_ACPI_FWNODE_OPS(ops) \
1655 	const struct fwnode_operations ops = {				\
1656 		.device_is_available = acpi_fwnode_device_is_available, \
1657 		.device_get_match_data = acpi_fwnode_device_get_match_data, \
1658 		.device_dma_supported =				\
1659 			acpi_fwnode_device_dma_supported,		\
1660 		.device_get_dma_attr = acpi_fwnode_device_get_dma_attr,	\
1661 		.property_present = acpi_fwnode_property_present,	\
1662 		.property_read_int_array =				\
1663 			acpi_fwnode_property_read_int_array,		\
1664 		.property_read_string_array =				\
1665 			acpi_fwnode_property_read_string_array,		\
1666 		.get_parent = acpi_node_get_parent,			\
1667 		.get_next_child_node = acpi_get_next_subnode,		\
1668 		.get_named_child_node = acpi_fwnode_get_named_child_node, \
1669 		.get_name = acpi_fwnode_get_name,			\
1670 		.get_name_prefix = acpi_fwnode_get_name_prefix,		\
1671 		.get_reference_args = acpi_fwnode_get_reference_args,	\
1672 		.graph_get_next_endpoint =				\
1673 			acpi_graph_get_next_endpoint,			\
1674 		.graph_get_remote_endpoint =				\
1675 			acpi_graph_get_remote_endpoint,			\
1676 		.graph_get_port_parent = acpi_fwnode_get_parent,	\
1677 		.graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1678 		.irq_get = acpi_fwnode_irq_get,				\
1679 	};								\
1680 	EXPORT_SYMBOL_GPL(ops)
1681 
1682 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1683 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1684 const struct fwnode_operations acpi_static_fwnode_ops;
1685 
is_acpi_device_node(const struct fwnode_handle * fwnode)1686 bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1687 {
1688 	return !IS_ERR_OR_NULL(fwnode) &&
1689 		fwnode->ops == &acpi_device_fwnode_ops;
1690 }
1691 EXPORT_SYMBOL(is_acpi_device_node);
1692 
is_acpi_data_node(const struct fwnode_handle * fwnode)1693 bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1694 {
1695 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1696 }
1697 EXPORT_SYMBOL(is_acpi_data_node);
1698