xref: /linux/drivers/base/swnode.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Software nodes for the firmware node framework.
4  *
5  * Copyright (C) 2018, Intel Corporation
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  */
8 
9 #include <linux/container_of.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/idr.h>
14 #include <linux/init.h>
15 #include <linux/kobject.h>
16 #include <linux/kstrtox.h>
17 #include <linux/list.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/string.h>
22 #include <linux/sysfs.h>
23 #include <linux/types.h>
24 
25 #include "base.h"
26 
27 struct swnode {
28 	struct kobject kobj;
29 	struct fwnode_handle fwnode;
30 	const struct software_node *node;
31 	int id;
32 
33 	/* hierarchy */
34 	struct ida child_ids;
35 	struct list_head entry;
36 	struct list_head children;
37 	struct swnode *parent;
38 
39 	unsigned int allocated:1;
40 	unsigned int managed:1;
41 };
42 
43 static DEFINE_IDA(swnode_root_ids);
44 static struct kset *swnode_kset;
45 
46 #define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj)
47 
48 static const struct fwnode_operations software_node_ops;
49 
50 bool is_software_node(const struct fwnode_handle *fwnode)
51 {
52 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops;
53 }
54 EXPORT_SYMBOL_GPL(is_software_node);
55 
56 #define to_swnode(__fwnode)						\
57 	({								\
58 		typeof(__fwnode) __to_swnode_fwnode = __fwnode;		\
59 									\
60 		is_software_node(__to_swnode_fwnode) ?			\
61 			container_of(__to_swnode_fwnode,		\
62 				     struct swnode, fwnode) : NULL;	\
63 	})
64 
65 static inline struct swnode *dev_to_swnode(struct device *dev)
66 {
67 	struct fwnode_handle *fwnode = dev_fwnode(dev);
68 
69 	if (!fwnode)
70 		return NULL;
71 
72 	if (!is_software_node(fwnode))
73 		fwnode = fwnode->secondary;
74 
75 	return to_swnode(fwnode);
76 }
77 
78 static struct swnode *
79 software_node_to_swnode(const struct software_node *node)
80 {
81 	struct swnode *swnode = NULL;
82 	struct kobject *k;
83 
84 	if (!node)
85 		return NULL;
86 
87 	spin_lock(&swnode_kset->list_lock);
88 
89 	list_for_each_entry(k, &swnode_kset->list, entry) {
90 		swnode = kobj_to_swnode(k);
91 		if (swnode->node == node)
92 			break;
93 		swnode = NULL;
94 	}
95 
96 	spin_unlock(&swnode_kset->list_lock);
97 
98 	return swnode;
99 }
100 
101 const struct software_node *to_software_node(const struct fwnode_handle *fwnode)
102 {
103 	const struct swnode *swnode = to_swnode(fwnode);
104 
105 	return swnode ? swnode->node : NULL;
106 }
107 EXPORT_SYMBOL_GPL(to_software_node);
108 
109 struct fwnode_handle *software_node_fwnode(const struct software_node *node)
110 {
111 	struct swnode *swnode = software_node_to_swnode(node);
112 
113 	return swnode ? &swnode->fwnode : NULL;
114 }
115 EXPORT_SYMBOL_GPL(software_node_fwnode);
116 
117 /* -------------------------------------------------------------------------- */
118 /* property_entry processing */
119 
120 static const struct property_entry *
121 property_entry_get(const struct property_entry *prop, const char *name)
122 {
123 	if (!prop)
124 		return NULL;
125 
126 	for (; prop->name; prop++)
127 		if (!strcmp(name, prop->name))
128 			return prop;
129 
130 	return NULL;
131 }
132 
133 static const void *property_get_pointer(const struct property_entry *prop)
134 {
135 	if (!prop->length)
136 		return NULL;
137 
138 	return prop->is_inline ? &prop->value : prop->pointer;
139 }
140 
141 static const void *property_entry_find(const struct property_entry *props,
142 				       const char *propname, size_t length)
143 {
144 	const struct property_entry *prop;
145 	const void *pointer;
146 
147 	prop = property_entry_get(props, propname);
148 	if (!prop)
149 		return ERR_PTR(-EINVAL);
150 	pointer = property_get_pointer(prop);
151 	if (!pointer)
152 		return ERR_PTR(-ENODATA);
153 	if (length > prop->length)
154 		return ERR_PTR(-EOVERFLOW);
155 	return pointer;
156 }
157 
158 static int
159 property_entry_count_elems_of_size(const struct property_entry *props,
160 				   const char *propname, size_t length)
161 {
162 	const struct property_entry *prop;
163 
164 	prop = property_entry_get(props, propname);
165 	if (!prop)
166 		return -EINVAL;
167 
168 	return prop->length / length;
169 }
170 
171 static int property_entry_read_int_array(const struct property_entry *props,
172 					 const char *name,
173 					 unsigned int elem_size, void *val,
174 					 size_t nval)
175 {
176 	const void *pointer;
177 	size_t length;
178 
179 	if (!val)
180 		return property_entry_count_elems_of_size(props, name,
181 							  elem_size);
182 
183 	if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
184 		return -ENXIO;
185 
186 	length = nval * elem_size;
187 
188 	pointer = property_entry_find(props, name, length);
189 	if (IS_ERR(pointer))
190 		return PTR_ERR(pointer);
191 
192 	memcpy(val, pointer, length);
193 	return 0;
194 }
195 
196 static int property_entry_read_string_array(const struct property_entry *props,
197 					    const char *propname,
198 					    const char **strings, size_t nval)
199 {
200 	const void *pointer;
201 	size_t length;
202 	int array_len;
203 
204 	/* Find out the array length. */
205 	array_len = property_entry_count_elems_of_size(props, propname,
206 						       sizeof(const char *));
207 	if (array_len < 0)
208 		return array_len;
209 
210 	/* Return how many there are if strings is NULL. */
211 	if (!strings)
212 		return array_len;
213 
214 	array_len = min_t(size_t, nval, array_len);
215 	length = array_len * sizeof(*strings);
216 
217 	pointer = property_entry_find(props, propname, length);
218 	if (IS_ERR(pointer))
219 		return PTR_ERR(pointer);
220 
221 	memcpy(strings, pointer, length);
222 
223 	return array_len;
224 }
225 
226 static void property_entry_free_data(const struct property_entry *p)
227 {
228 	const char * const *src_str;
229 	size_t i, nval;
230 
231 	if (p->type == DEV_PROP_STRING) {
232 		src_str = property_get_pointer(p);
233 		nval = p->length / sizeof(*src_str);
234 		for (i = 0; i < nval; i++)
235 			kfree(src_str[i]);
236 	}
237 
238 	if (!p->is_inline)
239 		kfree(p->pointer);
240 
241 	kfree(p->name);
242 }
243 
244 static bool property_copy_string_array(const char **dst_ptr,
245 				       const char * const *src_ptr,
246 				       size_t nval)
247 {
248 	int i;
249 
250 	for (i = 0; i < nval; i++) {
251 		dst_ptr[i] = kstrdup(src_ptr[i], GFP_KERNEL);
252 		if (!dst_ptr[i] && src_ptr[i]) {
253 			while (--i >= 0)
254 				kfree(dst_ptr[i]);
255 			return false;
256 		}
257 	}
258 
259 	return true;
260 }
261 
262 static int property_entry_copy_data(struct property_entry *dst,
263 				    const struct property_entry *src)
264 {
265 	const void *pointer = property_get_pointer(src);
266 	void *dst_ptr;
267 	size_t nval;
268 
269 	/*
270 	 * Properties with no data should not be marked as stored
271 	 * out of line.
272 	 */
273 	if (!src->is_inline && !src->length)
274 		return -ENODATA;
275 
276 	/*
277 	 * Reference properties are never stored inline as
278 	 * they are too big.
279 	 */
280 	if (src->type == DEV_PROP_REF && src->is_inline)
281 		return -EINVAL;
282 
283 	if (src->length <= sizeof(dst->value)) {
284 		dst_ptr = &dst->value;
285 		dst->is_inline = true;
286 	} else {
287 		dst_ptr = kmalloc(src->length, GFP_KERNEL);
288 		if (!dst_ptr)
289 			return -ENOMEM;
290 		dst->pointer = dst_ptr;
291 	}
292 
293 	if (src->type == DEV_PROP_STRING) {
294 		nval = src->length / sizeof(const char *);
295 		if (!property_copy_string_array(dst_ptr, pointer, nval)) {
296 			if (!dst->is_inline)
297 				kfree(dst->pointer);
298 			return -ENOMEM;
299 		}
300 	} else {
301 		memcpy(dst_ptr, pointer, src->length);
302 	}
303 
304 	dst->length = src->length;
305 	dst->type = src->type;
306 	dst->name = kstrdup(src->name, GFP_KERNEL);
307 	if (!dst->name) {
308 		property_entry_free_data(dst);
309 		return -ENOMEM;
310 	}
311 
312 	return 0;
313 }
314 
315 /**
316  * property_entries_dup - duplicate array of properties
317  * @properties: array of properties to copy
318  *
319  * This function creates a deep copy of the given NULL-terminated array
320  * of property entries.
321  */
322 struct property_entry *
323 property_entries_dup(const struct property_entry *properties)
324 {
325 	struct property_entry *p;
326 	int i, n = 0;
327 	int ret;
328 
329 	if (!properties)
330 		return NULL;
331 
332 	while (properties[n].name)
333 		n++;
334 
335 	p = kzalloc_objs(*p, n + 1);
336 	if (!p)
337 		return ERR_PTR(-ENOMEM);
338 
339 	for (i = 0; i < n; i++) {
340 		ret = property_entry_copy_data(&p[i], &properties[i]);
341 		if (ret) {
342 			while (--i >= 0)
343 				property_entry_free_data(&p[i]);
344 			kfree(p);
345 			return ERR_PTR(ret);
346 		}
347 	}
348 
349 	return p;
350 }
351 EXPORT_SYMBOL_GPL(property_entries_dup);
352 
353 /**
354  * property_entries_free - free previously allocated array of properties
355  * @properties: array of properties to destroy
356  *
357  * This function frees given NULL-terminated array of property entries,
358  * along with their data.
359  */
360 void property_entries_free(const struct property_entry *properties)
361 {
362 	const struct property_entry *p;
363 
364 	if (!properties)
365 		return;
366 
367 	for (p = properties; p->name; p++)
368 		property_entry_free_data(p);
369 
370 	kfree(properties);
371 }
372 EXPORT_SYMBOL_GPL(property_entries_free);
373 
374 /* -------------------------------------------------------------------------- */
375 /* fwnode operations */
376 
377 static struct swnode *swnode_get(struct swnode *swnode)
378 {
379 	kobject_get(&swnode->kobj);
380 
381 	return swnode;
382 }
383 
384 static void swnode_put(struct swnode *swnode)
385 {
386 	kobject_put(&swnode->kobj);
387 }
388 
389 static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
390 {
391 	struct swnode *swnode = swnode_get(to_swnode(fwnode));
392 
393 	return &swnode->fwnode;
394 }
395 
396 static void software_node_put(struct fwnode_handle *fwnode)
397 {
398 	swnode_put(to_swnode(fwnode));
399 }
400 
401 static bool software_node_property_present(const struct fwnode_handle *fwnode,
402 					   const char *propname)
403 {
404 	struct swnode *swnode = to_swnode(fwnode);
405 
406 	return !!property_entry_get(swnode->node->properties, propname);
407 }
408 
409 static int software_node_read_int_array(const struct fwnode_handle *fwnode,
410 					const char *propname,
411 					unsigned int elem_size, void *val,
412 					size_t nval)
413 {
414 	struct swnode *swnode = to_swnode(fwnode);
415 
416 	return property_entry_read_int_array(swnode->node->properties, propname,
417 					     elem_size, val, nval);
418 }
419 
420 static int software_node_read_string_array(const struct fwnode_handle *fwnode,
421 					   const char *propname,
422 					   const char **val, size_t nval)
423 {
424 	struct swnode *swnode = to_swnode(fwnode);
425 
426 	return property_entry_read_string_array(swnode->node->properties,
427 						propname, val, nval);
428 }
429 
430 static const char *
431 software_node_get_name(const struct fwnode_handle *fwnode)
432 {
433 	const struct swnode *swnode = to_swnode(fwnode);
434 
435 	return kobject_name(&swnode->kobj);
436 }
437 
438 static const char *
439 software_node_get_name_prefix(const struct fwnode_handle *fwnode)
440 {
441 	struct fwnode_handle *parent;
442 	const char *prefix;
443 
444 	parent = fwnode_get_parent(fwnode);
445 	if (!parent)
446 		return "";
447 
448 	/* Figure out the prefix from the parents. */
449 	while (is_software_node(parent))
450 		parent = fwnode_get_next_parent(parent);
451 
452 	prefix = fwnode_get_name_prefix(parent);
453 	fwnode_handle_put(parent);
454 
455 	/* Guess something if prefix was NULL. */
456 	return prefix ?: "/";
457 }
458 
459 static struct fwnode_handle *
460 software_node_get_parent(const struct fwnode_handle *fwnode)
461 {
462 	struct swnode *swnode = to_swnode(fwnode);
463 
464 	if (!swnode || !swnode->parent)
465 		return NULL;
466 
467 	return fwnode_handle_get(&swnode->parent->fwnode);
468 }
469 
470 static struct fwnode_handle *
471 software_node_get_next_child(const struct fwnode_handle *fwnode,
472 			     struct fwnode_handle *child)
473 {
474 	struct swnode *p = to_swnode(fwnode);
475 	struct swnode *c = to_swnode(child);
476 
477 	if (!p || list_empty(&p->children) ||
478 	    (c && list_is_last(&c->entry, &p->children))) {
479 		fwnode_handle_put(child);
480 		return NULL;
481 	}
482 
483 	if (c)
484 		c = list_next_entry(c, entry);
485 	else
486 		c = list_first_entry(&p->children, struct swnode, entry);
487 
488 	fwnode_handle_put(child);
489 	return fwnode_handle_get(&c->fwnode);
490 }
491 
492 static struct fwnode_handle *
493 software_node_get_named_child_node(const struct fwnode_handle *fwnode,
494 				   const char *childname)
495 {
496 	struct swnode *swnode = to_swnode(fwnode);
497 	struct swnode *child;
498 
499 	if (!swnode || list_empty(&swnode->children))
500 		return NULL;
501 
502 	list_for_each_entry(child, &swnode->children, entry) {
503 		if (!strcmp(childname, kobject_name(&child->kobj))) {
504 			swnode_get(child);
505 			return &child->fwnode;
506 		}
507 	}
508 	return NULL;
509 }
510 
511 static int
512 software_node_get_reference_args(const struct fwnode_handle *fwnode,
513 				 const char *propname, const char *nargs_prop,
514 				 unsigned int nargs, unsigned int index,
515 				 struct fwnode_reference_args *args)
516 {
517 	struct swnode *swnode = to_swnode(fwnode);
518 	const struct software_node_ref_args *ref_array;
519 	const struct software_node_ref_args *ref;
520 	const struct property_entry *prop;
521 	struct fwnode_handle *refnode;
522 	u32 nargs_prop_val;
523 	int error;
524 	int i;
525 
526 	prop = property_entry_get(swnode->node->properties, propname);
527 	if (!prop)
528 		return -ENOENT;
529 
530 	if (prop->type != DEV_PROP_REF)
531 		return -EINVAL;
532 
533 	/*
534 	 * We expect that references are never stored inline, even
535 	 * single ones, as they are too big.
536 	 */
537 	if (prop->is_inline)
538 		return -EINVAL;
539 
540 	if ((index + 1) * sizeof(*ref) > prop->length)
541 		return -ENOENT;
542 
543 	ref_array = prop->pointer;
544 	ref = &ref_array[index];
545 
546 	/*
547 	 * A software node can reference other software nodes or firmware
548 	 * nodes (which are the abstraction layer sitting on top of them).
549 	 * This is done to ensure we can create references to static software
550 	 * nodes before they're registered with the firmware node framework.
551 	 * At the time the reference is being resolved, we expect the swnodes
552 	 * in question to already have been registered and to be backed by
553 	 * a firmware node. This is why we use the fwnode API below to read the
554 	 * relevant properties and bump the reference count.
555 	 */
556 
557 	if (ref->swnode)
558 		refnode = software_node_fwnode(ref->swnode);
559 	else if (ref->fwnode)
560 		refnode = ref->fwnode;
561 	else
562 		return -EINVAL;
563 
564 	if (!refnode)
565 		return -ENOTCONN;
566 
567 	if (nargs_prop) {
568 		error = fwnode_property_read_u32(refnode, nargs_prop, &nargs_prop_val);
569 		if (error)
570 			return error;
571 
572 		nargs = nargs_prop_val;
573 	}
574 
575 	if (nargs > NR_FWNODE_REFERENCE_ARGS)
576 		return -EINVAL;
577 
578 	if (!args)
579 		return 0;
580 
581 	args->fwnode = fwnode_handle_get(refnode);
582 	args->nargs = nargs;
583 
584 	for (i = 0; i < nargs; i++)
585 		args->args[i] = ref->args[i];
586 
587 	return 0;
588 }
589 
590 static struct fwnode_handle *
591 swnode_graph_find_next_port(const struct fwnode_handle *parent,
592 			    struct fwnode_handle *port)
593 {
594 	struct fwnode_handle *old = port;
595 
596 	while ((port = software_node_get_next_child(parent, old))) {
597 		/*
598 		 * fwnode ports have naming style "port@", so we search for any
599 		 * children that follow that convention.
600 		 */
601 		if (!strncmp(to_swnode(port)->node->name, "port@",
602 			     strlen("port@")))
603 			return port;
604 		old = port;
605 	}
606 
607 	return NULL;
608 }
609 
610 static struct fwnode_handle *
611 software_node_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
612 				      struct fwnode_handle *endpoint)
613 {
614 	struct swnode *swnode = to_swnode(fwnode);
615 	struct fwnode_handle *parent;
616 	struct fwnode_handle *port;
617 
618 	if (!swnode)
619 		return NULL;
620 
621 	if (endpoint) {
622 		port = software_node_get_parent(endpoint);
623 		parent = software_node_get_parent(port);
624 	} else {
625 		parent = software_node_get_named_child_node(fwnode, "ports");
626 		if (!parent)
627 			parent = software_node_get(&swnode->fwnode);
628 
629 		port = swnode_graph_find_next_port(parent, NULL);
630 	}
631 
632 	for (; port; port = swnode_graph_find_next_port(parent, port)) {
633 		endpoint = software_node_get_next_child(port, endpoint);
634 		if (endpoint) {
635 			fwnode_handle_put(port);
636 			break;
637 		}
638 	}
639 
640 	fwnode_handle_put(parent);
641 
642 	return endpoint;
643 }
644 
645 static struct fwnode_handle *
646 software_node_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
647 {
648 	struct swnode *swnode = to_swnode(fwnode);
649 	const struct software_node_ref_args *ref;
650 	const struct property_entry *prop;
651 
652 	if (!swnode)
653 		return NULL;
654 
655 	prop = property_entry_get(swnode->node->properties, "remote-endpoint");
656 	if (!prop || prop->type != DEV_PROP_REF || prop->is_inline)
657 		return NULL;
658 
659 	ref = prop->pointer;
660 
661 	if (!ref->swnode)
662 		return NULL;
663 
664 	return software_node_get(software_node_fwnode(ref->swnode));
665 }
666 
667 static struct fwnode_handle *
668 software_node_graph_get_port_parent(struct fwnode_handle *fwnode)
669 {
670 	struct swnode *swnode = to_swnode(fwnode);
671 
672 	swnode = swnode->parent;
673 	if (swnode && !strcmp(swnode->node->name, "ports"))
674 		swnode = swnode->parent;
675 
676 	return swnode ? software_node_get(&swnode->fwnode) : NULL;
677 }
678 
679 static int
680 software_node_graph_parse_endpoint(const struct fwnode_handle *fwnode,
681 				   struct fwnode_endpoint *endpoint)
682 {
683 	struct swnode *swnode = to_swnode(fwnode);
684 	const char *parent_name = swnode->parent->node->name;
685 	int ret;
686 
687 	if (strlen("port@") >= strlen(parent_name) ||
688 	    strncmp(parent_name, "port@", strlen("port@")))
689 		return -EINVAL;
690 
691 	/* Ports have naming style "port@n", we need to select the n */
692 	ret = kstrtou32(parent_name + strlen("port@"), 10, &endpoint->port);
693 	if (ret)
694 		return ret;
695 
696 	endpoint->id = swnode->id;
697 	endpoint->local_fwnode = fwnode;
698 
699 	return 0;
700 }
701 
702 static int software_node_add_links(struct fwnode_handle *fwnode)
703 {
704 	const struct software_node_ref_args *ref, *ref_array;
705 	struct swnode *swnode = to_swnode(fwnode);
706 	const struct property_entry *prop;
707 	struct fwnode_handle *refnode;
708 	unsigned int count;
709 
710 	if (!swnode || !swnode->node->properties)
711 		return 0;
712 
713 	/*
714 	 * Unlike Device Tree, where phandles appear in many non-supplier
715 	 * contexts and a curated allowlist is required, a software node only
716 	 * carries a DEV_PROP_REF property when the author explicitly describes
717 	 * a reference to another node. Every such reference is therefore an
718 	 * intentional supplier dependency, so we create fwnode links for all
719 	 * of them.
720 	 */
721 	for (prop = swnode->node->properties; prop->name; prop++) {
722 		if (prop->type != DEV_PROP_REF || prop->is_inline)
723 			continue;
724 
725 		/*
726 		 * TODO: Graph "remote-endpoint" references go both ways
727 		 * between endpoint child nodes and would create endpoint
728 		 * cycles. Let's leave it out for now until we have potential
729 		 * users.
730 		 */
731 		if (!strcmp(prop->name, "remote-endpoint"))
732 			continue;
733 
734 		ref_array = prop->pointer;
735 		count = prop->length / sizeof(*ref_array);
736 
737 		for (unsigned int i = 0; i < count; i++) {
738 			ref = &ref_array[i];
739 
740 			if (ref->swnode)
741 				refnode = software_node_fwnode(ref->swnode);
742 			else if (ref->fwnode)
743 				refnode = ref->fwnode;
744 			else
745 				continue;
746 
747 			/* Supplier not registered yet, or self-reference. */
748 			if (!refnode || refnode == &swnode->fwnode)
749 				continue;
750 
751 			fwnode_link_add(&swnode->fwnode, refnode, 0);
752 		}
753 	}
754 
755 	return 0;
756 }
757 
758 static const struct fwnode_operations software_node_ops = {
759 	.get = software_node_get,
760 	.put = software_node_put,
761 	.property_present = software_node_property_present,
762 	.property_read_bool = software_node_property_present,
763 	.property_read_int_array = software_node_read_int_array,
764 	.property_read_string_array = software_node_read_string_array,
765 	.get_name = software_node_get_name,
766 	.get_name_prefix = software_node_get_name_prefix,
767 	.get_parent = software_node_get_parent,
768 	.get_next_child_node = software_node_get_next_child,
769 	.get_named_child_node = software_node_get_named_child_node,
770 	.get_reference_args = software_node_get_reference_args,
771 	.graph_get_next_endpoint = software_node_graph_get_next_endpoint,
772 	.graph_get_remote_endpoint = software_node_graph_get_remote_endpoint,
773 	.graph_get_port_parent = software_node_graph_get_port_parent,
774 	.graph_parse_endpoint = software_node_graph_parse_endpoint,
775 	.add_links = software_node_add_links,
776 };
777 
778 /* -------------------------------------------------------------------------- */
779 
780 /**
781  * software_node_find_by_name - Find software node by name
782  * @parent: Parent of the software node
783  * @name: Name of the software node
784  *
785  * The function will find a node that is child of @parent and that is named
786  * @name. If no node is found, the function returns NULL.
787  *
788  * NOTE: you will need to drop the reference with fwnode_handle_put() after use.
789  */
790 const struct software_node *
791 software_node_find_by_name(const struct software_node *parent, const char *name)
792 {
793 	struct swnode *swnode = NULL;
794 	struct kobject *k;
795 
796 	if (!name)
797 		return NULL;
798 
799 	spin_lock(&swnode_kset->list_lock);
800 
801 	list_for_each_entry(k, &swnode_kset->list, entry) {
802 		swnode = kobj_to_swnode(k);
803 		if (parent == swnode->node->parent && swnode->node->name &&
804 		    !strcmp(name, swnode->node->name)) {
805 			swnode_get(swnode);
806 			break;
807 		}
808 		swnode = NULL;
809 	}
810 
811 	spin_unlock(&swnode_kset->list_lock);
812 
813 	return swnode ? swnode->node : NULL;
814 }
815 EXPORT_SYMBOL_GPL(software_node_find_by_name);
816 
817 static struct software_node *software_node_alloc(const struct property_entry *properties)
818 {
819 	struct property_entry *props;
820 	struct software_node *node;
821 
822 	props = property_entries_dup(properties);
823 	if (IS_ERR(props))
824 		return ERR_CAST(props);
825 
826 	node = kzalloc_obj(*node);
827 	if (!node) {
828 		property_entries_free(props);
829 		return ERR_PTR(-ENOMEM);
830 	}
831 
832 	node->properties = props;
833 
834 	return node;
835 }
836 
837 static void software_node_free(const struct software_node *node)
838 {
839 	property_entries_free(node->properties);
840 	kfree(node);
841 }
842 
843 static void software_node_release(struct kobject *kobj)
844 {
845 	struct swnode *swnode = kobj_to_swnode(kobj);
846 
847 	fwnode_links_purge(&swnode->fwnode);
848 
849 	if (swnode->parent) {
850 		ida_free(&swnode->parent->child_ids, swnode->id);
851 		list_del(&swnode->entry);
852 	} else {
853 		ida_free(&swnode_root_ids, swnode->id);
854 	}
855 
856 	if (swnode->allocated)
857 		software_node_free(swnode->node);
858 
859 	ida_destroy(&swnode->child_ids);
860 	kfree(swnode);
861 }
862 
863 static const struct kobj_type software_node_type = {
864 	.release = software_node_release,
865 	.sysfs_ops = &kobj_sysfs_ops,
866 };
867 
868 static struct fwnode_handle *
869 swnode_register(const struct software_node *node, struct swnode *parent,
870 		unsigned int allocated)
871 {
872 	struct swnode *swnode;
873 	int ret;
874 
875 	swnode = kzalloc_obj(*swnode);
876 	if (!swnode)
877 		return ERR_PTR(-ENOMEM);
878 
879 	ret = ida_alloc(parent ? &parent->child_ids : &swnode_root_ids,
880 			GFP_KERNEL);
881 	if (ret < 0) {
882 		kfree(swnode);
883 		return ERR_PTR(ret);
884 	}
885 
886 	swnode->id = ret;
887 	swnode->node = node;
888 	swnode->parent = parent;
889 	swnode->kobj.kset = swnode_kset;
890 	fwnode_init(&swnode->fwnode, &software_node_ops);
891 
892 	ida_init(&swnode->child_ids);
893 	INIT_LIST_HEAD(&swnode->entry);
894 	INIT_LIST_HEAD(&swnode->children);
895 
896 	if (node->name)
897 		ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
898 					   parent ? &parent->kobj : NULL,
899 					   "%s", node->name);
900 	else
901 		ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
902 					   parent ? &parent->kobj : NULL,
903 					   "node%d", swnode->id);
904 	if (ret) {
905 		swnode_put(swnode);
906 		return ERR_PTR(ret);
907 	}
908 
909 	/*
910 	 * Assign the flag only in the successful case, so
911 	 * the above swnode_put() won't mess up with properties.
912 	 */
913 	swnode->allocated = allocated;
914 
915 	if (parent)
916 		list_add_tail(&swnode->entry, &parent->children);
917 
918 	kobject_uevent(&swnode->kobj, KOBJ_ADD);
919 	return &swnode->fwnode;
920 }
921 
922 /**
923  * software_node_register_node_group - Register a group of software nodes
924  * @node_group: NULL terminated array of software node pointers to be registered
925  *
926  * Register multiple software nodes at once. If any node in the array
927  * has its .parent pointer set (which can only be to another software_node),
928  * then its parent **must** have been registered before it is; either outside
929  * of this function or by ordering the array such that parent comes before
930  * child.
931  */
932 int software_node_register_node_group(const struct software_node * const *node_group)
933 {
934 	unsigned int i;
935 	int ret;
936 
937 	if (!node_group)
938 		return 0;
939 
940 	for (i = 0; node_group[i]; i++) {
941 		ret = software_node_register(node_group[i]);
942 		if (ret) {
943 			software_node_unregister_node_group(node_group);
944 			return ret;
945 		}
946 	}
947 
948 	return 0;
949 }
950 EXPORT_SYMBOL_GPL(software_node_register_node_group);
951 
952 /**
953  * software_node_unregister_node_group - Unregister a group of software nodes
954  * @node_group: NULL terminated array of software node pointers to be unregistered
955  *
956  * Unregister multiple software nodes at once. If parent pointers are set up
957  * in any of the software nodes then the array **must** be ordered such that
958  * parents come before their children.
959  *
960  * NOTE: If you are uncertain whether the array is ordered such that
961  * parents will be unregistered before their children, it is wiser to
962  * remove the nodes individually, in the correct order (child before
963  * parent).
964  */
965 void software_node_unregister_node_group(const struct software_node * const *node_group)
966 {
967 	unsigned int i = 0;
968 
969 	if (!node_group)
970 		return;
971 
972 	while (node_group[i])
973 		i++;
974 
975 	while (i--)
976 		software_node_unregister(node_group[i]);
977 }
978 EXPORT_SYMBOL_GPL(software_node_unregister_node_group);
979 
980 /**
981  * software_node_register - Register static software node
982  * @node: The software node to be registered
983  */
984 int software_node_register(const struct software_node *node)
985 {
986 	struct swnode *parent = software_node_to_swnode(node->parent);
987 
988 	if (software_node_to_swnode(node))
989 		return -EEXIST;
990 
991 	if (node->parent && !parent)
992 		return -EINVAL;
993 
994 	return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0));
995 }
996 EXPORT_SYMBOL_GPL(software_node_register);
997 
998 /**
999  * software_node_unregister - Unregister static software node
1000  * @node: The software node to be unregistered
1001  */
1002 void software_node_unregister(const struct software_node *node)
1003 {
1004 	struct swnode *swnode;
1005 
1006 	swnode = software_node_to_swnode(node);
1007 	if (swnode)
1008 		fwnode_remove_software_node(&swnode->fwnode);
1009 }
1010 EXPORT_SYMBOL_GPL(software_node_unregister);
1011 
1012 struct fwnode_handle *
1013 fwnode_create_software_node(const struct property_entry *properties,
1014 			    const struct fwnode_handle *parent)
1015 {
1016 	struct fwnode_handle *fwnode;
1017 	struct software_node *node;
1018 	struct swnode *p;
1019 
1020 	if (IS_ERR(parent))
1021 		return ERR_CAST(parent);
1022 
1023 	p = to_swnode(parent);
1024 	if (parent && !p)
1025 		return ERR_PTR(-EINVAL);
1026 
1027 	node = software_node_alloc(properties);
1028 	if (IS_ERR(node))
1029 		return ERR_CAST(node);
1030 
1031 	node->parent = p ? p->node : NULL;
1032 
1033 	fwnode = swnode_register(node, p, 1);
1034 	if (IS_ERR(fwnode))
1035 		software_node_free(node);
1036 
1037 	return fwnode;
1038 }
1039 EXPORT_SYMBOL_GPL(fwnode_create_software_node);
1040 
1041 void fwnode_remove_software_node(struct fwnode_handle *fwnode)
1042 {
1043 	struct swnode *swnode = to_swnode(fwnode);
1044 
1045 	if (!swnode)
1046 		return;
1047 
1048 	swnode_put(swnode);
1049 }
1050 EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
1051 
1052 /**
1053  * device_add_software_node - Assign software node to a device
1054  * @dev: The device the software node is meant for.
1055  * @node: The software node.
1056  *
1057  * This function will make @node the secondary firmware node pointer of @dev. If
1058  * @dev has no primary node, then @node will become the primary node. The
1059  * function will register @node automatically if it wasn't already registered.
1060  */
1061 int device_add_software_node(struct device *dev, const struct software_node *node)
1062 {
1063 	struct swnode *swnode;
1064 	int ret;
1065 
1066 	/* Only one software node per device. */
1067 	if (dev_to_swnode(dev))
1068 		return -EBUSY;
1069 
1070 	swnode = software_node_to_swnode(node);
1071 	if (swnode) {
1072 		swnode_get(swnode);
1073 	} else {
1074 		ret = software_node_register(node);
1075 		if (ret)
1076 			return ret;
1077 
1078 		swnode = software_node_to_swnode(node);
1079 	}
1080 
1081 	set_secondary_fwnode(dev, &swnode->fwnode);
1082 
1083 	/*
1084 	 * If the device has been fully registered by the time this function is
1085 	 * called, software_node_notify() must be called separately so that the
1086 	 * symlinks get created and the reference count of the node is kept in
1087 	 * balance.
1088 	 */
1089 	if (device_is_registered(dev))
1090 		software_node_notify(dev);
1091 
1092 	return 0;
1093 }
1094 EXPORT_SYMBOL_GPL(device_add_software_node);
1095 
1096 /**
1097  * device_remove_software_node - Remove device's software node
1098  * @dev: The device with the software node.
1099  *
1100  * This function will unregister the software node of @dev.
1101  */
1102 void device_remove_software_node(struct device *dev)
1103 {
1104 	struct swnode *swnode;
1105 
1106 	swnode = dev_to_swnode(dev);
1107 	if (!swnode)
1108 		return;
1109 
1110 	if (device_is_registered(dev))
1111 		software_node_notify_remove(dev);
1112 
1113 	set_secondary_fwnode(dev, NULL);
1114 	swnode_put(swnode);
1115 }
1116 EXPORT_SYMBOL_GPL(device_remove_software_node);
1117 
1118 /**
1119  * device_create_managed_software_node - Create a software node for a device
1120  * @dev: The device the software node is assigned to.
1121  * @properties: Device properties for the software node.
1122  * @parent: Parent of the software node.
1123  *
1124  * Creates a software node as a managed resource for @dev, which means the
1125  * lifetime of the newly created software node is tied to the lifetime of @dev.
1126  * Software nodes created with this function should not be reused or shared
1127  * because of that. The function takes a deep copy of @properties for the
1128  * software node.
1129  *
1130  * Since the new software node is assigned directly to @dev, and since it should
1131  * not be shared, it is not returned to the caller. The function returns 0 on
1132  * success, and errno in case of an error.
1133  */
1134 int device_create_managed_software_node(struct device *dev,
1135 					const struct property_entry *properties,
1136 					const struct software_node *parent)
1137 {
1138 	struct fwnode_handle *p = software_node_fwnode(parent);
1139 	struct fwnode_handle *fwnode;
1140 
1141 	if (parent && !p)
1142 		return -EINVAL;
1143 
1144 	fwnode = fwnode_create_software_node(properties, p);
1145 	if (IS_ERR(fwnode))
1146 		return PTR_ERR(fwnode);
1147 
1148 	to_swnode(fwnode)->managed = true;
1149 	set_secondary_fwnode(dev, fwnode);
1150 
1151 	if (device_is_registered(dev))
1152 		software_node_notify(dev);
1153 
1154 	return 0;
1155 }
1156 EXPORT_SYMBOL_GPL(device_create_managed_software_node);
1157 
1158 void software_node_notify(struct device *dev)
1159 {
1160 	struct swnode *swnode;
1161 	int ret;
1162 
1163 	swnode = dev_to_swnode(dev);
1164 	if (!swnode)
1165 		return;
1166 
1167 	/*
1168 	 * When the software node is the device's secondary firmware node,
1169 	 * the core only records the owning device on the primary fwnode
1170 	 * (see device_add()). fw_devlink resolves a supplier device through
1171 	 * fwnode->dev, so without this a consumer referencing the software
1172 	 * node could never find the supplier device and would defer forever.
1173 	 * Make fwnode.dev point to its owner in that case.
1174 	 */
1175 	if (!device_match_fwnode(dev, &swnode->fwnode) && !swnode->fwnode.dev)
1176 		swnode->fwnode.dev = dev;
1177 
1178 	swnode_get(swnode);
1179 	ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node");
1180 	if (ret)
1181 		return;
1182 
1183 	ret = sysfs_create_link(&swnode->kobj, &dev->kobj, dev_name(dev));
1184 	if (ret) {
1185 		sysfs_remove_link(&dev->kobj, "software_node");
1186 		return;
1187 	}
1188 }
1189 
1190 void software_node_notify_remove(struct device *dev)
1191 {
1192 	struct swnode *swnode;
1193 
1194 	swnode = dev_to_swnode(dev);
1195 	if (!swnode)
1196 		return;
1197 
1198 	sysfs_remove_link(&swnode->kobj, dev_name(dev));
1199 	sysfs_remove_link(&dev->kobj, "software_node");
1200 
1201 	/*
1202 	 * Drop the device pointer mirrored onto a secondary software node in
1203 	 * software_node_notify(). For a primary software node the core owns
1204 	 * fwnode->dev and clears it in device_del().
1205 	 */
1206 	if (!device_match_fwnode(dev, &swnode->fwnode) && swnode->fwnode.dev == dev)
1207 		swnode->fwnode.dev = NULL;
1208 
1209 	swnode_put(swnode);
1210 
1211 	if (swnode->managed) {
1212 		set_secondary_fwnode(dev, NULL);
1213 		swnode_put(swnode);
1214 	}
1215 }
1216 
1217 void __init software_node_init(void)
1218 {
1219 	swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj);
1220 	if (!swnode_kset)
1221 		pr_err("failed to register software nodes\n");
1222 }
1223