xref: /linux/drivers/of/dynamic.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for dynamic device trees.
4  *
5  * On some platforms, the device tree can be manipulated at runtime.
6  * The routines in this section support adding, removing and changing
7  * device tree nodes.
8  */
9 
10 #define pr_fmt(fmt)	"OF: " fmt
11 
12 #include <linux/cleanup.h>
13 #include <linux/device.h>
14 #include <linux/of.h>
15 #include <linux/spinlock.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/proc_fs.h>
19 
20 #include "of_private.h"
21 
22 static struct device_node *kobj_to_device_node(struct kobject *kobj)
23 {
24 	return container_of(kobj, struct device_node, kobj);
25 }
26 
27 /**
28  * of_node_get() - Increment refcount of a node
29  * @node:	Node to inc refcount, NULL is supported to simplify writing of
30  *		callers
31  *
32  * Return: The node with refcount incremented.
33  */
34 struct device_node *of_node_get(struct device_node *node)
35 {
36 	if (node)
37 		kobject_get(&node->kobj);
38 	return node;
39 }
40 EXPORT_SYMBOL(of_node_get);
41 
42 /**
43  * of_node_put() - Decrement refcount of a node
44  * @node:	Node to dec refcount, NULL is supported to simplify writing of
45  *		callers
46  */
47 void of_node_put(struct device_node *node)
48 {
49 	if (node)
50 		kobject_put(&node->kobj);
51 }
52 EXPORT_SYMBOL(of_node_put);
53 
54 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
55 
56 int of_reconfig_notifier_register(struct notifier_block *nb)
57 {
58 	return blocking_notifier_chain_register(&of_reconfig_chain, nb);
59 }
60 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
61 
62 int of_reconfig_notifier_unregister(struct notifier_block *nb)
63 {
64 	return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
65 }
66 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
67 
68 static const char *action_names[] = {
69 	[0] = "INVALID",
70 	[OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
71 	[OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
72 	[OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
73 	[OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
74 	[OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
75 };
76 
77 #define _do_print(func, prefix, action, node, prop, ...) ({	\
78 	func("changeset: " prefix "%-15s %pOF%s%s\n",		\
79 	     ##__VA_ARGS__, action_names[action], node,		\
80 	     prop ? ":" : "", prop ? prop->name : "");		\
81 })
82 #define of_changeset_action_err(...) _do_print(pr_err, __VA_ARGS__)
83 #define of_changeset_action_debug(...) _do_print(pr_debug, __VA_ARGS__)
84 
85 int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
86 {
87 	int rc;
88 	struct of_reconfig_data *pr = p;
89 
90 	of_changeset_action_debug("notify: ", action, pr->dn, pr->prop);
91 
92 	rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
93 	return notifier_to_errno(rc);
94 }
95 
96 /*
97  * of_reconfig_get_state_change()	- Returns new state of device
98  * @action	- action of the of notifier
99  * @arg		- argument of the of notifier
100  *
101  * Returns the new state of a device based on the notifier used.
102  *
103  * Return: OF_RECONFIG_CHANGE_REMOVE on device going from enabled to
104  * disabled, OF_RECONFIG_CHANGE_ADD on device going from disabled to
105  * enabled and OF_RECONFIG_NO_CHANGE on no change.
106  */
107 int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
108 {
109 	struct property *prop, *old_prop = NULL;
110 	int is_status, status_state, old_status_state, prev_state, new_state;
111 
112 	/* figure out if a device should be created or destroyed */
113 	switch (action) {
114 	case OF_RECONFIG_ATTACH_NODE:
115 	case OF_RECONFIG_DETACH_NODE:
116 		prop = of_find_property(pr->dn, "status", NULL);
117 		break;
118 	case OF_RECONFIG_ADD_PROPERTY:
119 	case OF_RECONFIG_REMOVE_PROPERTY:
120 		prop = pr->prop;
121 		break;
122 	case OF_RECONFIG_UPDATE_PROPERTY:
123 		prop = pr->prop;
124 		old_prop = pr->old_prop;
125 		break;
126 	default:
127 		return OF_RECONFIG_NO_CHANGE;
128 	}
129 
130 	is_status = 0;
131 	status_state = -1;
132 	old_status_state = -1;
133 	prev_state = -1;
134 	new_state = -1;
135 
136 	if (prop && !strcmp(prop->name, "status")) {
137 		is_status = 1;
138 		status_state = !strcmp(prop->value, "okay") ||
139 			       !strcmp(prop->value, "ok");
140 		if (old_prop)
141 			old_status_state = !strcmp(old_prop->value, "okay") ||
142 					   !strcmp(old_prop->value, "ok");
143 	}
144 
145 	switch (action) {
146 	case OF_RECONFIG_ATTACH_NODE:
147 		prev_state = 0;
148 		/* -1 & 0 status either missing or okay */
149 		new_state = status_state != 0;
150 		break;
151 	case OF_RECONFIG_DETACH_NODE:
152 		/* -1 & 0 status either missing or okay */
153 		prev_state = status_state != 0;
154 		new_state = 0;
155 		break;
156 	case OF_RECONFIG_ADD_PROPERTY:
157 		if (is_status) {
158 			/* no status property -> enabled (legacy) */
159 			prev_state = 1;
160 			new_state = status_state;
161 		}
162 		break;
163 	case OF_RECONFIG_REMOVE_PROPERTY:
164 		if (is_status) {
165 			prev_state = status_state;
166 			/* no status property -> enabled (legacy) */
167 			new_state = 1;
168 		}
169 		break;
170 	case OF_RECONFIG_UPDATE_PROPERTY:
171 		if (is_status) {
172 			prev_state = old_status_state != 0;
173 			new_state = status_state != 0;
174 		}
175 		break;
176 	}
177 
178 	if (prev_state == new_state)
179 		return OF_RECONFIG_NO_CHANGE;
180 
181 	return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
182 }
183 EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
184 
185 int of_property_notify(int action, struct device_node *np,
186 		       struct property *prop, struct property *oldprop)
187 {
188 	struct of_reconfig_data pr;
189 
190 	/* only call notifiers if the node is attached */
191 	if (!of_node_is_attached(np))
192 		return 0;
193 
194 	pr.dn = np;
195 	pr.prop = prop;
196 	pr.old_prop = oldprop;
197 	return of_reconfig_notify(action, &pr);
198 }
199 
200 static void __of_attach_node(struct device_node *np)
201 {
202 	const __be32 *phandle;
203 	int sz;
204 	unsigned long flags;
205 
206 	raw_spin_lock_irqsave(&devtree_lock, flags);
207 
208 	if (!of_node_check_flag(np, OF_OVERLAY)) {
209 		np->name = __of_get_property(np, "name", NULL);
210 		if (!np->name)
211 			np->name = "<NULL>";
212 
213 		phandle = __of_get_property(np, "phandle", &sz);
214 		if (!phandle)
215 			phandle = __of_get_property(np, "linux,phandle", &sz);
216 		if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
217 			phandle = __of_get_property(np, "ibm,phandle", &sz);
218 		if (phandle && (sz >= 4))
219 			np->phandle = be32_to_cpup(phandle);
220 		else
221 			np->phandle = 0;
222 	}
223 
224 	np->child = NULL;
225 	np->sibling = np->parent->child;
226 	np->parent->child = np;
227 	of_node_clear_flag(np, OF_DETACHED);
228 
229 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
230 
231 	__of_attach_node_sysfs(np);
232 }
233 
234 /**
235  * of_attach_node() - Plug a device node into the tree and global list.
236  * @np:		Pointer to the caller's Device Node
237  */
238 int of_attach_node(struct device_node *np)
239 {
240 	struct of_reconfig_data rd;
241 
242 	memset(&rd, 0, sizeof(rd));
243 	rd.dn = np;
244 
245 	mutex_lock(&of_mutex);
246 	__of_attach_node(np);
247 	mutex_unlock(&of_mutex);
248 
249 	of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
250 
251 	return 0;
252 }
253 
254 void __of_detach_node(struct device_node *np)
255 {
256 	struct device_node *parent;
257 	unsigned long flags;
258 
259 	raw_spin_lock_irqsave(&devtree_lock, flags);
260 
261 	parent = np->parent;
262 	if (WARN_ON(of_node_check_flag(np, OF_DETACHED) || !parent)) {
263 		raw_spin_unlock_irqrestore(&devtree_lock, flags);
264 		return;
265 	}
266 
267 	if (parent->child == np)
268 		parent->child = np->sibling;
269 	else {
270 		struct device_node *prevsib;
271 		for (prevsib = np->parent->child;
272 		     prevsib->sibling != np;
273 		     prevsib = prevsib->sibling)
274 			;
275 		prevsib->sibling = np->sibling;
276 	}
277 
278 	of_node_set_flag(np, OF_DETACHED);
279 
280 	/* race with of_find_node_by_phandle() prevented by devtree_lock */
281 	__of_phandle_cache_inv_entry(np->phandle);
282 
283 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
284 
285 	__of_detach_node_sysfs(np);
286 }
287 
288 /**
289  * of_detach_node() - "Unplug" a node from the device tree.
290  * @np:		Pointer to the caller's Device Node
291  */
292 int of_detach_node(struct device_node *np)
293 {
294 	struct of_reconfig_data rd;
295 
296 	memset(&rd, 0, sizeof(rd));
297 	rd.dn = np;
298 
299 	mutex_lock(&of_mutex);
300 	__of_detach_node(np);
301 	mutex_unlock(&of_mutex);
302 
303 	of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
304 
305 	return 0;
306 }
307 EXPORT_SYMBOL_GPL(of_detach_node);
308 
309 void __of_prop_free(struct property *prop)
310 {
311 	kfree(prop->name);
312 	kfree(prop->value);
313 	kfree(prop);
314 }
315 
316 static void property_list_free(struct property *prop_list)
317 {
318 	struct property *prop, *next;
319 
320 	for (prop = prop_list; prop != NULL; prop = next) {
321 		next = prop->next;
322 		__of_prop_free(prop);
323 	}
324 }
325 
326 /**
327  * of_node_release() - release a dynamically allocated node
328  * @kobj: kernel object of the node to be released
329  *
330  * In of_node_put() this function is passed to kref_put() as the destructor.
331  */
332 void of_node_release(struct kobject *kobj)
333 {
334 	struct device_node *node = kobj_to_device_node(kobj);
335 
336 	/*
337 	 * can not use '"%pOF", node' in pr_err() calls from this function
338 	 * because an of_node_get(node) when refcount is already zero
339 	 * will result in an error and a stack dump
340 	 */
341 
342 	/* We should never be releasing nodes that haven't been detached. */
343 	if (!of_node_check_flag(node, OF_DETACHED)) {
344 
345 		pr_err("ERROR: %s() detected bad of_node_put() on %pOF/%s\n",
346 			__func__, node->parent, node->full_name);
347 
348 		/*
349 		 * of unittests will test this path.  Do not print the stack
350 		 * trace when the error is caused by unittest so that we do
351 		 * not display what a normal developer might reasonably
352 		 * consider a real bug.
353 		 */
354 		if (!IS_ENABLED(CONFIG_OF_UNITTEST) ||
355 		    strcmp(node->parent->full_name, "testcase-data")) {
356 			dump_stack();
357 			pr_err("ERROR: next of_node_put() on this node will result in a kobject warning 'refcount_t: underflow; use-after-free.'\n");
358 		}
359 
360 		return;
361 	}
362 	if (!of_node_check_flag(node, OF_DYNAMIC))
363 		return;
364 
365 	if (of_node_check_flag(node, OF_OVERLAY)) {
366 
367 		if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET)) {
368 			/* premature refcount of zero, do not free memory */
369 			pr_err("ERROR: memory leak before free overlay changeset,  %pOF\n",
370 			       node);
371 			return;
372 		}
373 
374 		/*
375 		 * If node->properties non-empty then properties were added
376 		 * to this node either by different overlay that has not
377 		 * yet been removed, or by a non-overlay mechanism.
378 		 */
379 		if (node->properties)
380 			pr_err("ERROR: %s(), unexpected properties in %pOF\n",
381 			       __func__, node);
382 	}
383 
384 	if (node->child)
385 		pr_err("ERROR: %s() unexpected children for %pOF/%s\n",
386 			__func__, node->parent, node->full_name);
387 
388 	property_list_free(node->properties);
389 	property_list_free(node->deadprops);
390 	fwnode_links_purge(of_fwnode_handle(node));
391 
392 	kfree(node->full_name);
393 	kfree(node->data);
394 	kfree(node);
395 }
396 
397 /**
398  * __of_prop_dup - Copy a property dynamically.
399  * @prop:	Property to copy
400  * @allocflags:	Allocation flags (typically pass GFP_KERNEL)
401  *
402  * Copy a property by dynamically allocating the memory of both the
403  * property structure and the property name & contents. The property's
404  * flags have the OF_DYNAMIC bit set so that we can differentiate between
405  * dynamically allocated properties and not.
406  *
407  * Return: The newly allocated property or NULL on out of memory error.
408  */
409 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
410 {
411 	struct property *new;
412 
413 	new = kzalloc_obj(*new, allocflags);
414 	if (!new)
415 		return NULL;
416 
417 	/*
418 	 * NOTE: There is no check for zero length value.
419 	 * In case of a boolean property, this will allocate a value
420 	 * of zero bytes. We do this to work around the use
421 	 * of of_get_property() calls on boolean values.
422 	 */
423 	new->name = kstrdup(prop->name, allocflags);
424 	new->value = kmemdup(prop->value, prop->length, allocflags);
425 	new->length = prop->length;
426 	if (!new->name || !new->value)
427 		goto err_free;
428 
429 	/* mark the property as dynamic */
430 	of_property_set_flag(new, OF_DYNAMIC);
431 
432 	return new;
433 
434  err_free:
435 	__of_prop_free(new);
436 	return NULL;
437 }
438 
439 /**
440  * __of_node_dup() - Duplicate or create an empty device node dynamically.
441  * @np:		if not NULL, contains properties to be duplicated in new node
442  * @full_name:	string value to be duplicated into new node's full_name field
443  *
444  * Create a device tree node, optionally duplicating the properties of
445  * another node.  The node data are dynamically allocated and all the node
446  * flags have the OF_DYNAMIC & OF_DETACHED bits set.
447  *
448  * Return: The newly allocated node or NULL on out of memory error.  Use
449  * of_node_put() on it when done to free the memory allocated for it.
450  */
451 struct device_node *__of_node_dup(const struct device_node *np,
452 				  const char *full_name)
453 {
454 	struct device_node *node;
455 
456 	node = kzalloc_obj(*node);
457 	if (!node)
458 		return NULL;
459 	node->full_name = kstrdup(full_name, GFP_KERNEL);
460 	if (!node->full_name) {
461 		kfree(node);
462 		return NULL;
463 	}
464 
465 	of_node_set_flag(node, OF_DYNAMIC);
466 	of_node_set_flag(node, OF_DETACHED);
467 	of_node_init(node);
468 
469 	/* Iterate over and duplicate all properties */
470 	if (np) {
471 		struct property *pp, *new_pp;
472 		for_each_property_of_node(np, pp) {
473 			new_pp = __of_prop_dup(pp, GFP_KERNEL);
474 			if (!new_pp)
475 				goto err_prop;
476 			if (__of_add_property(node, new_pp)) {
477 				__of_prop_free(new_pp);
478 				goto err_prop;
479 			}
480 		}
481 	}
482 	return node;
483 
484  err_prop:
485 	of_node_put(node); /* Frees the node and properties */
486 	return NULL;
487 }
488 
489 /**
490  * of_changeset_create_node - Dynamically create a device node and attach to
491  * a given changeset.
492  *
493  * @ocs: Pointer to changeset
494  * @parent: Pointer to parent device node
495  * @full_name: Node full name
496  *
497  * Return: Pointer to the created device node or NULL in case of an error.
498  */
499 struct device_node *of_changeset_create_node(struct of_changeset *ocs,
500 					     struct device_node *parent,
501 					     const char *full_name)
502 {
503 	struct device_node *np;
504 	int ret;
505 
506 	np = __of_node_dup(NULL, full_name);
507 	if (!np)
508 		return NULL;
509 	np->parent = parent;
510 
511 	ret = of_changeset_attach_node(ocs, np);
512 	if (ret) {
513 		of_node_put(np);
514 		return NULL;
515 	}
516 
517 	return np;
518 }
519 EXPORT_SYMBOL(of_changeset_create_node);
520 
521 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
522 {
523 	if (ce->action == OF_RECONFIG_ATTACH_NODE &&
524 	    of_node_check_flag(ce->np, OF_OVERLAY)) {
525 		if (kref_read(&ce->np->kobj.kref) > 1) {
526 			pr_err("ERROR: memory leak, expected refcount 1 instead of %d, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node %pOF\n",
527 			       kref_read(&ce->np->kobj.kref), ce->np);
528 		} else {
529 			of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET);
530 		}
531 	}
532 
533 	of_node_put(ce->np);
534 	list_del(&ce->node);
535 	kfree(ce);
536 }
537 
538 static void __of_changeset_entry_invert(const struct of_changeset_entry *ce,
539 					  struct of_changeset_entry *rce)
540 {
541 	memcpy(rce, ce, sizeof(*rce));
542 
543 	switch (ce->action) {
544 	case OF_RECONFIG_ATTACH_NODE:
545 		rce->action = OF_RECONFIG_DETACH_NODE;
546 		break;
547 	case OF_RECONFIG_DETACH_NODE:
548 		rce->action = OF_RECONFIG_ATTACH_NODE;
549 		break;
550 	case OF_RECONFIG_ADD_PROPERTY:
551 		rce->action = OF_RECONFIG_REMOVE_PROPERTY;
552 		break;
553 	case OF_RECONFIG_REMOVE_PROPERTY:
554 		rce->action = OF_RECONFIG_ADD_PROPERTY;
555 		break;
556 	case OF_RECONFIG_UPDATE_PROPERTY:
557 		rce->old_prop = ce->prop;
558 		rce->prop = ce->old_prop;
559 		/* update was used but original property did not exist */
560 		if (!rce->prop) {
561 			rce->action = OF_RECONFIG_REMOVE_PROPERTY;
562 			rce->prop = ce->prop;
563 		}
564 		break;
565 	}
566 }
567 
568 static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
569 		bool revert)
570 {
571 	struct of_reconfig_data rd;
572 	struct of_changeset_entry ce_inverted;
573 	int ret = 0;
574 
575 	if (revert) {
576 		__of_changeset_entry_invert(ce, &ce_inverted);
577 		ce = &ce_inverted;
578 	}
579 
580 	switch (ce->action) {
581 	case OF_RECONFIG_ATTACH_NODE:
582 	case OF_RECONFIG_DETACH_NODE:
583 		memset(&rd, 0, sizeof(rd));
584 		rd.dn = ce->np;
585 		ret = of_reconfig_notify(ce->action, &rd);
586 		break;
587 	case OF_RECONFIG_ADD_PROPERTY:
588 	case OF_RECONFIG_REMOVE_PROPERTY:
589 	case OF_RECONFIG_UPDATE_PROPERTY:
590 		ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
591 		break;
592 	default:
593 		pr_err("invalid devicetree changeset action: %i\n",
594 			(int)ce->action);
595 		ret = -EINVAL;
596 	}
597 
598 	if (ret)
599 		pr_err("changeset notifier error @%pOF\n", ce->np);
600 	return ret;
601 }
602 
603 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
604 {
605 	int ret = 0;
606 
607 	of_changeset_action_debug("apply: ", ce->action, ce->np, ce->prop);
608 
609 	switch (ce->action) {
610 	case OF_RECONFIG_ATTACH_NODE:
611 		__of_attach_node(ce->np);
612 		break;
613 	case OF_RECONFIG_DETACH_NODE:
614 		__of_detach_node(ce->np);
615 		break;
616 	case OF_RECONFIG_ADD_PROPERTY:
617 		ret = __of_add_property(ce->np, ce->prop);
618 		break;
619 	case OF_RECONFIG_REMOVE_PROPERTY:
620 		ret = __of_remove_property(ce->np, ce->prop);
621 		break;
622 
623 	case OF_RECONFIG_UPDATE_PROPERTY:
624 		ret = __of_update_property(ce->np, ce->prop, &ce->old_prop);
625 		break;
626 	default:
627 		ret = -EINVAL;
628 	}
629 
630 	if (ret) {
631 		of_changeset_action_err("apply failed: ", ce->action, ce->np, ce->prop);
632 		return ret;
633 	}
634 
635 	return 0;
636 }
637 
638 static inline int __of_changeset_entry_revert(const struct of_changeset_entry *ce)
639 {
640 	struct of_changeset_entry ce_inverted;
641 
642 	__of_changeset_entry_invert(ce, &ce_inverted);
643 	return __of_changeset_entry_apply(&ce_inverted);
644 }
645 
646 /**
647  * of_changeset_init - Initialize a changeset for use
648  *
649  * @ocs:	changeset pointer
650  *
651  * Initialize a changeset structure
652  */
653 void of_changeset_init(struct of_changeset *ocs)
654 {
655 	memset(ocs, 0, sizeof(*ocs));
656 	INIT_LIST_HEAD(&ocs->entries);
657 }
658 EXPORT_SYMBOL_GPL(of_changeset_init);
659 
660 /**
661  * of_changeset_destroy - Destroy a changeset
662  *
663  * @ocs:	changeset pointer
664  *
665  * Destroys a changeset. Note that if a changeset is applied,
666  * its changes to the tree cannot be reverted.
667  */
668 void of_changeset_destroy(struct of_changeset *ocs)
669 {
670 	struct of_changeset_entry *ce, *cen;
671 
672 	/*
673 	 * When a device is deleted, the device links to/from it are also queued
674 	 * for deletion. Until these device links are freed, the devices
675 	 * themselves aren't freed. If the device being deleted is due to an
676 	 * overlay change, this device might be holding a reference to a device
677 	 * node that will be freed. So, wait until all already pending device
678 	 * links are deleted before freeing a device node. This ensures we don't
679 	 * free any device node that has a non-zero reference count.
680 	 */
681 	device_link_wait_removal();
682 
683 	list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
684 		__of_changeset_entry_destroy(ce);
685 }
686 EXPORT_SYMBOL_GPL(of_changeset_destroy);
687 
688 /*
689  * Apply the changeset entries in @ocs.
690  * If apply fails, an attempt is made to revert the entries that were
691  * successfully applied.
692  *
693  * If multiple revert errors occur then only the final revert error is reported.
694  *
695  * Returns 0 on success, a negative error value in case of an error.
696  * If a revert error occurs, it is returned in *ret_revert.
697  */
698 int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
699 {
700 	struct of_changeset_entry *ce;
701 	int ret, ret_tmp;
702 
703 	pr_debug("changeset: applying...\n");
704 	list_for_each_entry(ce, &ocs->entries, node) {
705 		ret = __of_changeset_entry_apply(ce);
706 		if (ret) {
707 			pr_err("Error applying changeset (%d)\n", ret);
708 			list_for_each_entry_continue_reverse(ce, &ocs->entries,
709 							     node) {
710 				ret_tmp = __of_changeset_entry_revert(ce);
711 				if (ret_tmp)
712 					*ret_revert = ret_tmp;
713 			}
714 			return ret;
715 		}
716 	}
717 
718 	return 0;
719 }
720 
721 /*
722  * Returns 0 on success, a negative error value in case of an error.
723  *
724  * If multiple changeset entry notification errors occur then only the
725  * final notification error is reported.
726  */
727 int __of_changeset_apply_notify(struct of_changeset *ocs)
728 {
729 	struct of_changeset_entry *ce;
730 	int ret = 0, ret_tmp;
731 
732 	pr_debug("changeset: emitting notifiers.\n");
733 
734 	/* drop the global lock while emitting notifiers */
735 	mutex_unlock(&of_mutex);
736 	list_for_each_entry(ce, &ocs->entries, node) {
737 		ret_tmp = __of_changeset_entry_notify(ce, 0);
738 		if (ret_tmp)
739 			ret = ret_tmp;
740 	}
741 	mutex_lock(&of_mutex);
742 	pr_debug("changeset: notifiers sent.\n");
743 
744 	return ret;
745 }
746 
747 /*
748  * Returns 0 on success, a negative error value in case of an error.
749  *
750  * If a changeset entry apply fails, an attempt is made to revert any
751  * previous entries in the changeset.  If any of the reverts fails,
752  * that failure is not reported.  Thus the state of the device tree
753  * is unknown if an apply error occurs.
754  */
755 static int __of_changeset_apply(struct of_changeset *ocs)
756 {
757 	int ret, ret_revert = 0;
758 
759 	ret = __of_changeset_apply_entries(ocs, &ret_revert);
760 	if (!ret)
761 		ret = __of_changeset_apply_notify(ocs);
762 
763 	return ret;
764 }
765 
766 /**
767  * of_changeset_apply - Applies a changeset
768  *
769  * @ocs:	changeset pointer
770  *
771  * Applies a changeset to the live tree.
772  * Any side-effects of live tree state changes are applied here on
773  * success, like creation/destruction of devices and side-effects
774  * like creation of sysfs properties and directories.
775  *
776  * Return: 0 on success, a negative error value in case of an error.
777  * On error the partially applied effects are reverted.
778  */
779 int of_changeset_apply(struct of_changeset *ocs)
780 {
781 	int ret;
782 
783 	mutex_lock(&of_mutex);
784 	ret = __of_changeset_apply(ocs);
785 	mutex_unlock(&of_mutex);
786 
787 	return ret;
788 }
789 EXPORT_SYMBOL_GPL(of_changeset_apply);
790 
791 /*
792  * Revert the changeset entries in @ocs.
793  * If revert fails, an attempt is made to re-apply the entries that were
794  * successfully removed.
795  *
796  * If multiple re-apply errors occur then only the final apply error is
797  * reported.
798  *
799  * Returns 0 on success, a negative error value in case of an error.
800  * If an apply error occurs, it is returned in *ret_apply.
801  */
802 int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
803 {
804 	struct of_changeset_entry *ce;
805 	int ret, ret_tmp;
806 
807 	pr_debug("changeset: reverting...\n");
808 	list_for_each_entry_reverse(ce, &ocs->entries, node) {
809 		ret = __of_changeset_entry_revert(ce);
810 		if (ret) {
811 			pr_err("Error reverting changeset (%d)\n", ret);
812 			list_for_each_entry_continue(ce, &ocs->entries, node) {
813 				ret_tmp = __of_changeset_entry_apply(ce);
814 				if (ret_tmp)
815 					*ret_apply = ret_tmp;
816 			}
817 			return ret;
818 		}
819 	}
820 
821 	return 0;
822 }
823 
824 /*
825  * If multiple changeset entry notification errors occur then only the
826  * final notification error is reported.
827  */
828 int __of_changeset_revert_notify(struct of_changeset *ocs)
829 {
830 	struct of_changeset_entry *ce;
831 	int ret = 0, ret_tmp;
832 
833 	pr_debug("changeset: emitting notifiers.\n");
834 
835 	/* drop the global lock while emitting notifiers */
836 	mutex_unlock(&of_mutex);
837 	list_for_each_entry_reverse(ce, &ocs->entries, node) {
838 		ret_tmp = __of_changeset_entry_notify(ce, 1);
839 		if (ret_tmp)
840 			ret = ret_tmp;
841 	}
842 	mutex_lock(&of_mutex);
843 	pr_debug("changeset: notifiers sent.\n");
844 
845 	return ret;
846 }
847 
848 static int __of_changeset_revert(struct of_changeset *ocs)
849 {
850 	int ret, ret_reply;
851 
852 	ret_reply = 0;
853 	ret = __of_changeset_revert_entries(ocs, &ret_reply);
854 
855 	if (!ret)
856 		ret = __of_changeset_revert_notify(ocs);
857 
858 	return ret;
859 }
860 
861 /**
862  * of_changeset_revert - Reverts an applied changeset
863  *
864  * @ocs:	changeset pointer
865  *
866  * Reverts a changeset returning the state of the tree to what it
867  * was before the application.
868  * Any side-effects like creation/destruction of devices and
869  * removal of sysfs properties and directories are applied.
870  *
871  * Return: 0 on success, a negative error value in case of an error.
872  */
873 int of_changeset_revert(struct of_changeset *ocs)
874 {
875 	int ret;
876 
877 	mutex_lock(&of_mutex);
878 	ret = __of_changeset_revert(ocs);
879 	mutex_unlock(&of_mutex);
880 
881 	return ret;
882 }
883 EXPORT_SYMBOL_GPL(of_changeset_revert);
884 
885 /**
886  * of_changeset_action - Add an action to the tail of the changeset list
887  *
888  * @ocs:	changeset pointer
889  * @action:	action to perform
890  * @np:		Pointer to device node
891  * @prop:	Pointer to property
892  *
893  * On action being one of:
894  * + OF_RECONFIG_ATTACH_NODE
895  * + OF_RECONFIG_DETACH_NODE,
896  * + OF_RECONFIG_ADD_PROPERTY
897  * + OF_RECONFIG_REMOVE_PROPERTY,
898  * + OF_RECONFIG_UPDATE_PROPERTY
899  *
900  * Return: 0 on success, a negative error value in case of an error.
901  */
902 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
903 		struct device_node *np, struct property *prop)
904 {
905 	struct of_changeset_entry *ce;
906 
907 	if (WARN_ON(action >= ARRAY_SIZE(action_names)))
908 		return -EINVAL;
909 
910 	ce = kzalloc_obj(*ce);
911 	if (!ce)
912 		return -ENOMEM;
913 
914 	/* get a reference to the node */
915 	ce->action = action;
916 	ce->np = of_node_get(np);
917 	ce->prop = prop;
918 
919 	/* add it to the list */
920 	list_add_tail(&ce->node, &ocs->entries);
921 	return 0;
922 }
923 EXPORT_SYMBOL_GPL(of_changeset_action);
924 
925 static int of_changeset_add_prop_helper(struct of_changeset *ocs,
926 					struct device_node *np,
927 					const struct property *pp)
928 {
929 	struct property *new_pp;
930 	int ret;
931 
932 	new_pp = __of_prop_dup(pp, GFP_KERNEL);
933 	if (!new_pp)
934 		return -ENOMEM;
935 
936 	ret = of_changeset_add_property(ocs, np, new_pp);
937 	if (ret) {
938 		__of_prop_free(new_pp);
939 		return ret;
940 	}
941 
942 	new_pp->next = np->deadprops;
943 	np->deadprops = new_pp;
944 
945 	return 0;
946 }
947 
948 /**
949  * of_changeset_add_prop_string - Add a string property to a changeset
950  *
951  * @ocs:	changeset pointer
952  * @np:		device node pointer
953  * @prop_name:	name of the property to be added
954  * @str:	pointer to null terminated string
955  *
956  * Create a string property and add it to a changeset.
957  *
958  * Return: 0 on success, a negative error value in case of an error.
959  */
960 int of_changeset_add_prop_string(struct of_changeset *ocs,
961 				 struct device_node *np,
962 				 const char *prop_name, const char *str)
963 {
964 	struct property prop;
965 
966 	prop.name = (char *)prop_name;
967 	prop.length = strlen(str) + 1;
968 	prop.value = (void *)str;
969 
970 	return of_changeset_add_prop_helper(ocs, np, &prop);
971 }
972 EXPORT_SYMBOL_GPL(of_changeset_add_prop_string);
973 
974 /**
975  * of_changeset_add_prop_string_array - Add a string list property to
976  * a changeset
977  *
978  * @ocs:	changeset pointer
979  * @np:		device node pointer
980  * @prop_name:	name of the property to be added
981  * @str_array:	pointer to an array of null terminated strings
982  * @sz:		number of string array elements
983  *
984  * Create a string list property and add it to a changeset.
985  *
986  * Return: 0 on success, a negative error value in case of an error.
987  */
988 int of_changeset_add_prop_string_array(struct of_changeset *ocs,
989 				       struct device_node *np,
990 				       const char *prop_name,
991 				       const char * const *str_array, size_t sz)
992 {
993 	struct property prop;
994 	int i, ret;
995 	char *vp;
996 
997 	prop.name = (char *)prop_name;
998 
999 	prop.length = 0;
1000 	for (i = 0; i < sz; i++)
1001 		prop.length += strlen(str_array[i]) + 1;
1002 
1003 	prop.value = kmalloc(prop.length, GFP_KERNEL);
1004 	if (!prop.value)
1005 		return -ENOMEM;
1006 
1007 	vp = prop.value;
1008 	for (i = 0; i < sz; i++) {
1009 		vp += snprintf(vp, (char *)prop.value + prop.length - vp, "%s",
1010 			       str_array[i]) + 1;
1011 	}
1012 	ret = of_changeset_add_prop_helper(ocs, np, &prop);
1013 	kfree(prop.value);
1014 
1015 	return ret;
1016 }
1017 EXPORT_SYMBOL_GPL(of_changeset_add_prop_string_array);
1018 
1019 /**
1020  * of_changeset_add_prop_u32_array - Add a property of 32 bit integers
1021  * property to a changeset
1022  *
1023  * @ocs:	changeset pointer
1024  * @np:		device node pointer
1025  * @prop_name:	name of the property to be added
1026  * @array:	pointer to an array of 32 bit integers
1027  * @sz:		number of array elements
1028  *
1029  * Create a property of 32 bit integers and add it to a changeset.
1030  *
1031  * Return: 0 on success, a negative error value in case of an error.
1032  */
1033 int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
1034 				    struct device_node *np,
1035 				    const char *prop_name,
1036 				    const u32 *array, size_t sz)
1037 {
1038 	struct property prop;
1039 	__be32 *val __free(kfree) = kcalloc(sz, sizeof(__be32), GFP_KERNEL);
1040 	int i;
1041 
1042 	if (!val)
1043 		return -ENOMEM;
1044 
1045 	for (i = 0; i < sz; i++)
1046 		val[i] = cpu_to_be32(array[i]);
1047 	prop.name = (char *)prop_name;
1048 	prop.length = sizeof(u32) * sz;
1049 	prop.value = (void *)val;
1050 
1051 	return of_changeset_add_prop_helper(ocs, np, &prop);
1052 }
1053 EXPORT_SYMBOL_GPL(of_changeset_add_prop_u32_array);
1054 
1055 /**
1056  * of_changeset_add_prop_bool - Add a boolean property (i.e. a property without
1057  * any values) to a changeset.
1058  *
1059  * @ocs:	changeset pointer
1060  * @np:		device node pointer
1061  * @prop_name:	name of the property to be added
1062  *
1063  * Create a boolean property and add it to a changeset.
1064  *
1065  * Return: 0 on success, a negative error value in case of an error.
1066  */
1067 int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
1068 			       const char *prop_name)
1069 {
1070 	struct property prop;
1071 
1072 	prop.name = (char *)prop_name;
1073 	prop.length = 0;
1074 	prop.value = NULL;
1075 
1076 	return of_changeset_add_prop_helper(ocs, np, &prop);
1077 }
1078 EXPORT_SYMBOL_GPL(of_changeset_add_prop_bool);
1079 
1080 static int of_changeset_update_prop_helper(struct of_changeset *ocs,
1081 					   struct device_node *np,
1082 					   const struct property *pp)
1083 {
1084 	struct property *new_pp;
1085 	int ret;
1086 
1087 	new_pp = __of_prop_dup(pp, GFP_KERNEL);
1088 	if (!new_pp)
1089 		return -ENOMEM;
1090 
1091 	ret = of_changeset_update_property(ocs, np, new_pp);
1092 	if (ret)
1093 		__of_prop_free(new_pp);
1094 
1095 	return ret;
1096 }
1097 
1098 /**
1099  * of_changeset_update_prop_string - Add a string property update to a changeset
1100  *
1101  * @ocs:	changeset pointer
1102  * @np:		device node pointer
1103  * @prop_name:	name of the property to be updated
1104  * @str:	pointer to null terminated string
1105  *
1106  * Create a string property to be updated and add it to a changeset.
1107  *
1108  * Return: 0 on success, a negative error value in case of an error.
1109  */
1110 int of_changeset_update_prop_string(struct of_changeset *ocs,
1111 				    struct device_node *np,
1112 				    const char *prop_name, const char *str)
1113 {
1114 	struct property prop = {
1115 		.name = (char *)prop_name,
1116 		.length = strlen(str) + 1,
1117 		.value = (void *)str,
1118 	};
1119 
1120 	return of_changeset_update_prop_helper(ocs, np, &prop);
1121 }
1122 EXPORT_SYMBOL_GPL(of_changeset_update_prop_string);
1123