of: Make sure attached nodes don't carry along extra children
[firefly-linux-kernel-4.4.55.git] / drivers / of / dynamic.c
1 /*
2  * Support for dynamic device trees.
3  *
4  * On some platforms, the device tree can be manipulated at runtime.
5  * The routines in this section support adding, removing and changing
6  * device tree nodes.
7  */
8
9 #include <linux/of.h>
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
13 #include <linux/proc_fs.h>
14
15 #include "of_private.h"
16
17 /**
18  * of_node_get() - Increment refcount of a node
19  * @node:       Node to inc refcount, NULL is supported to simplify writing of
20  *              callers
21  *
22  * Returns node.
23  */
24 struct device_node *of_node_get(struct device_node *node)
25 {
26         if (node)
27                 kobject_get(&node->kobj);
28         return node;
29 }
30 EXPORT_SYMBOL(of_node_get);
31
32 /**
33  * of_node_put() - Decrement refcount of a node
34  * @node:       Node to dec refcount, NULL is supported to simplify writing of
35  *              callers
36  */
37 void of_node_put(struct device_node *node)
38 {
39         if (node)
40                 kobject_put(&node->kobj);
41 }
42 EXPORT_SYMBOL(of_node_put);
43
44 void __of_detach_node_sysfs(struct device_node *np)
45 {
46         struct property *pp;
47
48         BUG_ON(!of_node_is_initialized(np));
49         if (!of_kset)
50                 return;
51
52         /* only remove properties if on sysfs */
53         if (of_node_is_attached(np)) {
54                 for_each_property_of_node(np, pp)
55                         sysfs_remove_bin_file(&np->kobj, &pp->attr);
56                 kobject_del(&np->kobj);
57         }
58
59         /* finally remove the kobj_init ref */
60         of_node_put(np);
61 }
62
63 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
64
65 int of_reconfig_notifier_register(struct notifier_block *nb)
66 {
67         return blocking_notifier_chain_register(&of_reconfig_chain, nb);
68 }
69 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
70
71 int of_reconfig_notifier_unregister(struct notifier_block *nb)
72 {
73         return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
74 }
75 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
76
77 int of_reconfig_notify(unsigned long action, void *p)
78 {
79         int rc;
80
81         rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
82         return notifier_to_errno(rc);
83 }
84
85 int of_property_notify(int action, struct device_node *np,
86                        struct property *prop)
87 {
88         struct of_prop_reconfig pr;
89
90         /* only call notifiers if the node is attached */
91         if (!of_node_is_attached(np))
92                 return 0;
93
94         pr.dn = np;
95         pr.prop = prop;
96         return of_reconfig_notify(action, &pr);
97 }
98
99 void __of_attach_node(struct device_node *np)
100 {
101         np->child = NULL;
102         np->sibling = np->parent->child;
103         np->allnext = np->parent->allnext;
104         np->parent->allnext = np;
105         np->parent->child = np;
106         of_node_clear_flag(np, OF_DETACHED);
107 }
108
109 /**
110  * of_attach_node() - Plug a device node into the tree and global list.
111  */
112 int of_attach_node(struct device_node *np)
113 {
114         unsigned long flags;
115         int rc;
116
117         rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
118         if (rc)
119                 return rc;
120
121         mutex_lock(&of_mutex);
122         raw_spin_lock_irqsave(&devtree_lock, flags);
123         __of_attach_node(np);
124         raw_spin_unlock_irqrestore(&devtree_lock, flags);
125
126         __of_attach_node_sysfs(np);
127         mutex_unlock(&of_mutex);
128         return 0;
129 }
130
131 void __of_detach_node(struct device_node *np)
132 {
133         struct device_node *parent;
134
135         if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
136                 return;
137
138         parent = np->parent;
139         if (WARN_ON(!parent))
140                 return;
141
142         if (of_allnodes == np)
143                 of_allnodes = np->allnext;
144         else {
145                 struct device_node *prev;
146                 for (prev = of_allnodes;
147                      prev->allnext != np;
148                      prev = prev->allnext)
149                         ;
150                 prev->allnext = np->allnext;
151         }
152
153         if (parent->child == np)
154                 parent->child = np->sibling;
155         else {
156                 struct device_node *prevsib;
157                 for (prevsib = np->parent->child;
158                      prevsib->sibling != np;
159                      prevsib = prevsib->sibling)
160                         ;
161                 prevsib->sibling = np->sibling;
162         }
163
164         of_node_set_flag(np, OF_DETACHED);
165 }
166
167 /**
168  * of_detach_node() - "Unplug" a node from the device tree.
169  *
170  * The caller must hold a reference to the node.  The memory associated with
171  * the node is not freed until its refcount goes to zero.
172  */
173 int of_detach_node(struct device_node *np)
174 {
175         unsigned long flags;
176         int rc = 0;
177
178         rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
179         if (rc)
180                 return rc;
181
182         mutex_lock(&of_mutex);
183         raw_spin_lock_irqsave(&devtree_lock, flags);
184         __of_detach_node(np);
185         raw_spin_unlock_irqrestore(&devtree_lock, flags);
186
187         __of_detach_node_sysfs(np);
188         mutex_unlock(&of_mutex);
189         return rc;
190 }
191
192 /**
193  * of_node_release() - release a dynamically allocated node
194  * @kref: kref element of the node to be released
195  *
196  * In of_node_put() this function is passed to kref_put() as the destructor.
197  */
198 void of_node_release(struct kobject *kobj)
199 {
200         struct device_node *node = kobj_to_device_node(kobj);
201         struct property *prop = node->properties;
202
203         /* We should never be releasing nodes that haven't been detached. */
204         if (!of_node_check_flag(node, OF_DETACHED)) {
205                 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
206                 dump_stack();
207                 return;
208         }
209
210         if (!of_node_check_flag(node, OF_DYNAMIC))
211                 return;
212
213         while (prop) {
214                 struct property *next = prop->next;
215                 kfree(prop->name);
216                 kfree(prop->value);
217                 kfree(prop);
218                 prop = next;
219
220                 if (!prop) {
221                         prop = node->deadprops;
222                         node->deadprops = NULL;
223                 }
224         }
225         kfree(node->full_name);
226         kfree(node->data);
227         kfree(node);
228 }
229
230 /**
231  * __of_prop_dup - Copy a property dynamically.
232  * @prop:       Property to copy
233  * @allocflags: Allocation flags (typically pass GFP_KERNEL)
234  *
235  * Copy a property by dynamically allocating the memory of both the
236  * property stucture and the property name & contents. The property's
237  * flags have the OF_DYNAMIC bit set so that we can differentiate between
238  * dynamically allocated properties and not.
239  * Returns the newly allocated property or NULL on out of memory error.
240  */
241 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
242 {
243         struct property *new;
244
245         new = kzalloc(sizeof(*new), allocflags);
246         if (!new)
247                 return NULL;
248
249         /*
250          * NOTE: There is no check for zero length value.
251          * In case of a boolean property This will allocate a value
252          * of zero bytes. We do this to work around the use
253          * of of_get_property() calls on boolean values.
254          */
255         new->name = kstrdup(prop->name, allocflags);
256         new->value = kmemdup(prop->value, prop->length, allocflags);
257         new->length = prop->length;
258         if (!new->name || !new->value)
259                 goto err_free;
260
261         /* mark the property as dynamic */
262         of_property_set_flag(new, OF_DYNAMIC);
263
264         return new;
265
266  err_free:
267         kfree(new->name);
268         kfree(new->value);
269         kfree(new);
270         return NULL;
271 }
272
273 /**
274  * __of_node_alloc() - Create an empty device node dynamically.
275  * @full_name:  Full name of the new device node
276  * @allocflags: Allocation flags (typically pass GFP_KERNEL)
277  *
278  * Create an empty device tree node, suitable for further modification.
279  * The node data are dynamically allocated and all the node flags
280  * have the OF_DYNAMIC & OF_DETACHED bits set.
281  * Returns the newly allocated node or NULL on out of memory error.
282  */
283 struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
284 {
285         struct device_node *node;
286
287         node = kzalloc(sizeof(*node), allocflags);
288         if (!node)
289                 return NULL;
290
291         node->full_name = kstrdup(full_name, allocflags);
292         of_node_set_flag(node, OF_DYNAMIC);
293         of_node_set_flag(node, OF_DETACHED);
294         if (!node->full_name)
295                 goto err_free;
296
297         of_node_init(node);
298
299         return node;
300
301  err_free:
302         kfree(node->full_name);
303         kfree(node);
304         return NULL;
305 }