projects/linux·blob master · refs

1134 lines · 28.3 KiB

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