of: Move dynamic node fixups out of powerpc and into common code
[deliverable/linux.git] / drivers / of / dynamic.c
CommitLineData
6afc0dc3
GL
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 */
24struct device_node *of_node_get(struct device_node *node)
25{
26 if (node)
27 kobject_get(&node->kobj);
28 return node;
29}
30EXPORT_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 */
37void of_node_put(struct device_node *node)
38{
39 if (node)
40 kobject_put(&node->kobj);
41}
42EXPORT_SYMBOL(of_node_put);
43
8a2b22a2 44void __of_detach_node_sysfs(struct device_node *np)
6afc0dc3
GL
45{
46 struct property *pp;
47
48 BUG_ON(!of_node_is_initialized(np));
8a2b22a2
GL
49 if (!of_kset)
50 return;
6afc0dc3
GL
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
63static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
64
65int of_reconfig_notifier_register(struct notifier_block *nb)
66{
67 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
68}
69EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
70
71int of_reconfig_notifier_unregister(struct notifier_block *nb)
72{
73 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
74}
75EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
76
77int 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
85int 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
d8c50088
PA
99void __of_attach_node(struct device_node *np)
100{
a25095d4
GL
101 const __be32 *phandle;
102 int sz;
103
104 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
105 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
106
107 phandle = __of_get_property(np, "phandle", &sz);
108 if (!phandle)
109 phandle = __of_get_property(np, "linux,phandle", &sz);
110 if (IS_ENABLED(PPC_PSERIES) && !phandle)
111 phandle = __of_get_property(np, "ibm,phandle", &sz);
112 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
113
6162dbe4 114 np->child = NULL;
d8c50088
PA
115 np->sibling = np->parent->child;
116 np->allnext = np->parent->allnext;
117 np->parent->allnext = np;
118 np->parent->child = np;
119 of_node_clear_flag(np, OF_DETACHED);
120}
121
6afc0dc3
GL
122/**
123 * of_attach_node() - Plug a device node into the tree and global list.
124 */
125int of_attach_node(struct device_node *np)
126{
127 unsigned long flags;
128 int rc;
129
130 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
131 if (rc)
132 return rc;
133
8a2b22a2 134 mutex_lock(&of_mutex);
6afc0dc3 135 raw_spin_lock_irqsave(&devtree_lock, flags);
d8c50088 136 __of_attach_node(np);
6afc0dc3
GL
137 raw_spin_unlock_irqrestore(&devtree_lock, flags);
138
8a2b22a2
GL
139 __of_attach_node_sysfs(np);
140 mutex_unlock(&of_mutex);
6afc0dc3
GL
141 return 0;
142}
143
d8c50088 144void __of_detach_node(struct device_node *np)
6afc0dc3
GL
145{
146 struct device_node *parent;
6afc0dc3 147
d8c50088
PA
148 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
149 return;
6afc0dc3
GL
150
151 parent = np->parent;
d8c50088
PA
152 if (WARN_ON(!parent))
153 return;
6afc0dc3
GL
154
155 if (of_allnodes == np)
156 of_allnodes = np->allnext;
157 else {
158 struct device_node *prev;
159 for (prev = of_allnodes;
160 prev->allnext != np;
161 prev = prev->allnext)
162 ;
163 prev->allnext = np->allnext;
164 }
165
166 if (parent->child == np)
167 parent->child = np->sibling;
168 else {
169 struct device_node *prevsib;
170 for (prevsib = np->parent->child;
171 prevsib->sibling != np;
172 prevsib = prevsib->sibling)
173 ;
174 prevsib->sibling = np->sibling;
175 }
176
177 of_node_set_flag(np, OF_DETACHED);
d8c50088
PA
178}
179
180/**
181 * of_detach_node() - "Unplug" a node from the device tree.
182 *
183 * The caller must hold a reference to the node. The memory associated with
184 * the node is not freed until its refcount goes to zero.
185 */
186int of_detach_node(struct device_node *np)
187{
188 unsigned long flags;
189 int rc = 0;
190
191 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
192 if (rc)
193 return rc;
194
8a2b22a2 195 mutex_lock(&of_mutex);
d8c50088
PA
196 raw_spin_lock_irqsave(&devtree_lock, flags);
197 __of_detach_node(np);
6afc0dc3
GL
198 raw_spin_unlock_irqrestore(&devtree_lock, flags);
199
8a2b22a2
GL
200 __of_detach_node_sysfs(np);
201 mutex_unlock(&of_mutex);
6afc0dc3
GL
202 return rc;
203}
204
205/**
206 * of_node_release() - release a dynamically allocated node
207 * @kref: kref element of the node to be released
208 *
209 * In of_node_put() this function is passed to kref_put() as the destructor.
210 */
211void of_node_release(struct kobject *kobj)
212{
213 struct device_node *node = kobj_to_device_node(kobj);
214 struct property *prop = node->properties;
215
216 /* We should never be releasing nodes that haven't been detached. */
217 if (!of_node_check_flag(node, OF_DETACHED)) {
218 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
219 dump_stack();
220 return;
221 }
222
223 if (!of_node_check_flag(node, OF_DYNAMIC))
224 return;
225
226 while (prop) {
227 struct property *next = prop->next;
228 kfree(prop->name);
229 kfree(prop->value);
230 kfree(prop);
231 prop = next;
232
233 if (!prop) {
234 prop = node->deadprops;
235 node->deadprops = NULL;
236 }
237 }
238 kfree(node->full_name);
239 kfree(node->data);
240 kfree(node);
241}
69843396
PA
242
243/**
244 * __of_prop_dup - Copy a property dynamically.
245 * @prop: Property to copy
246 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
247 *
248 * Copy a property by dynamically allocating the memory of both the
249 * property stucture and the property name & contents. The property's
250 * flags have the OF_DYNAMIC bit set so that we can differentiate between
251 * dynamically allocated properties and not.
252 * Returns the newly allocated property or NULL on out of memory error.
253 */
254struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
255{
256 struct property *new;
257
258 new = kzalloc(sizeof(*new), allocflags);
259 if (!new)
260 return NULL;
261
262 /*
263 * NOTE: There is no check for zero length value.
264 * In case of a boolean property This will allocate a value
265 * of zero bytes. We do this to work around the use
266 * of of_get_property() calls on boolean values.
267 */
268 new->name = kstrdup(prop->name, allocflags);
269 new->value = kmemdup(prop->value, prop->length, allocflags);
270 new->length = prop->length;
271 if (!new->name || !new->value)
272 goto err_free;
273
274 /* mark the property as dynamic */
275 of_property_set_flag(new, OF_DYNAMIC);
276
277 return new;
278
279 err_free:
280 kfree(new->name);
281 kfree(new->value);
282 kfree(new);
283 return NULL;
284}
285
286/**
287 * __of_node_alloc() - Create an empty device node dynamically.
288 * @full_name: Full name of the new device node
289 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
290 *
291 * Create an empty device tree node, suitable for further modification.
292 * The node data are dynamically allocated and all the node flags
293 * have the OF_DYNAMIC & OF_DETACHED bits set.
294 * Returns the newly allocated node or NULL on out of memory error.
295 */
296struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
297{
298 struct device_node *node;
299
300 node = kzalloc(sizeof(*node), allocflags);
301 if (!node)
302 return NULL;
303
304 node->full_name = kstrdup(full_name, allocflags);
305 of_node_set_flag(node, OF_DYNAMIC);
306 of_node_set_flag(node, OF_DETACHED);
307 if (!node->full_name)
308 goto err_free;
309
310 of_node_init(node);
311
312 return node;
313
314 err_free:
315 kfree(node->full_name);
316 kfree(node);
317 return NULL;
318}
This page took 0.036704 seconds and 5 git commands to generate.