]>
Commit | Line | Data |
---|---|---|
af6074fc | 1 | // SPDX-License-Identifier: GPL-2.0 |
6afc0dc3 GL |
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 | ||
606ad42a RH |
10 | #define pr_fmt(fmt) "OF: " fmt |
11 | ||
6afc0dc3 GL |
12 | #include <linux/of.h> |
13 | #include <linux/spinlock.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/string.h> | |
16 | #include <linux/proc_fs.h> | |
17 | ||
18 | #include "of_private.h" | |
19 | ||
4c2bb574 RH |
20 | static struct device_node *kobj_to_device_node(struct kobject *kobj) |
21 | { | |
22 | return container_of(kobj, struct device_node, kobj); | |
23 | } | |
24 | ||
6afc0dc3 GL |
25 | /** |
26 | * of_node_get() - Increment refcount of a node | |
27 | * @node: Node to inc refcount, NULL is supported to simplify writing of | |
28 | * callers | |
29 | * | |
30 | * Returns node. | |
31 | */ | |
32 | struct device_node *of_node_get(struct device_node *node) | |
33 | { | |
34 | if (node) | |
35 | kobject_get(&node->kobj); | |
36 | return node; | |
37 | } | |
38 | EXPORT_SYMBOL(of_node_get); | |
39 | ||
40 | /** | |
41 | * of_node_put() - Decrement refcount of a node | |
42 | * @node: Node to dec refcount, NULL is supported to simplify writing of | |
43 | * callers | |
44 | */ | |
45 | void of_node_put(struct device_node *node) | |
46 | { | |
47 | if (node) | |
48 | kobject_put(&node->kobj); | |
49 | } | |
50 | EXPORT_SYMBOL(of_node_put); | |
51 | ||
6afc0dc3 GL |
52 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); |
53 | ||
54 | int of_reconfig_notifier_register(struct notifier_block *nb) | |
55 | { | |
56 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); | |
57 | } | |
58 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); | |
59 | ||
60 | int of_reconfig_notifier_unregister(struct notifier_block *nb) | |
61 | { | |
62 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); | |
63 | } | |
64 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); | |
65 | ||
00aa3720 GL |
66 | #ifdef DEBUG |
67 | const char *action_names[] = { | |
68 | [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE", | |
69 | [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE", | |
70 | [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY", | |
71 | [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY", | |
72 | [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY", | |
73 | }; | |
74 | #endif | |
75 | ||
f5242e5a | 76 | int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p) |
6afc0dc3 GL |
77 | { |
78 | int rc; | |
00aa3720 | 79 | #ifdef DEBUG |
f5242e5a | 80 | struct of_reconfig_data *pr = p; |
00aa3720 GL |
81 | |
82 | switch (action) { | |
83 | case OF_RECONFIG_ATTACH_NODE: | |
84 | case OF_RECONFIG_DETACH_NODE: | |
0d638a07 RH |
85 | pr_debug("notify %-15s %pOF\n", action_names[action], |
86 | pr->dn); | |
00aa3720 GL |
87 | break; |
88 | case OF_RECONFIG_ADD_PROPERTY: | |
89 | case OF_RECONFIG_REMOVE_PROPERTY: | |
90 | case OF_RECONFIG_UPDATE_PROPERTY: | |
0d638a07 RH |
91 | pr_debug("notify %-15s %pOF:%s\n", action_names[action], |
92 | pr->dn, pr->prop->name); | |
00aa3720 | 93 | break; |
6afc0dc3 | 94 | |
00aa3720 GL |
95 | } |
96 | #endif | |
6afc0dc3 GL |
97 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); |
98 | return notifier_to_errno(rc); | |
99 | } | |
100 | ||
b53a2340 PA |
101 | /* |
102 | * of_reconfig_get_state_change() - Returns new state of device | |
103 | * @action - action of the of notifier | |
104 | * @arg - argument of the of notifier | |
105 | * | |
106 | * Returns the new state of a device based on the notifier used. | |
107 | * Returns 0 on device going from enabled to disabled, 1 on device | |
108 | * going from disabled to enabled and -1 on no change. | |
109 | */ | |
f5242e5a | 110 | int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr) |
b53a2340 | 111 | { |
f5242e5a | 112 | struct property *prop, *old_prop = NULL; |
b53a2340 PA |
113 | int is_status, status_state, old_status_state, prev_state, new_state; |
114 | ||
115 | /* figure out if a device should be created or destroyed */ | |
b53a2340 PA |
116 | switch (action) { |
117 | case OF_RECONFIG_ATTACH_NODE: | |
118 | case OF_RECONFIG_DETACH_NODE: | |
f5242e5a | 119 | prop = of_find_property(pr->dn, "status", NULL); |
b53a2340 PA |
120 | break; |
121 | case OF_RECONFIG_ADD_PROPERTY: | |
122 | case OF_RECONFIG_REMOVE_PROPERTY: | |
b53a2340 PA |
123 | prop = pr->prop; |
124 | break; | |
125 | case OF_RECONFIG_UPDATE_PROPERTY: | |
b53a2340 PA |
126 | prop = pr->prop; |
127 | old_prop = pr->old_prop; | |
128 | break; | |
129 | default: | |
130 | return OF_RECONFIG_NO_CHANGE; | |
131 | } | |
132 | ||
133 | is_status = 0; | |
134 | status_state = -1; | |
135 | old_status_state = -1; | |
136 | prev_state = -1; | |
137 | new_state = -1; | |
138 | ||
139 | if (prop && !strcmp(prop->name, "status")) { | |
140 | is_status = 1; | |
141 | status_state = !strcmp(prop->value, "okay") || | |
142 | !strcmp(prop->value, "ok"); | |
143 | if (old_prop) | |
144 | old_status_state = !strcmp(old_prop->value, "okay") || | |
145 | !strcmp(old_prop->value, "ok"); | |
146 | } | |
147 | ||
148 | switch (action) { | |
149 | case OF_RECONFIG_ATTACH_NODE: | |
150 | prev_state = 0; | |
151 | /* -1 & 0 status either missing or okay */ | |
152 | new_state = status_state != 0; | |
153 | break; | |
154 | case OF_RECONFIG_DETACH_NODE: | |
155 | /* -1 & 0 status either missing or okay */ | |
156 | prev_state = status_state != 0; | |
157 | new_state = 0; | |
158 | break; | |
159 | case OF_RECONFIG_ADD_PROPERTY: | |
160 | if (is_status) { | |
161 | /* no status property -> enabled (legacy) */ | |
162 | prev_state = 1; | |
163 | new_state = status_state; | |
164 | } | |
165 | break; | |
166 | case OF_RECONFIG_REMOVE_PROPERTY: | |
167 | if (is_status) { | |
168 | prev_state = status_state; | |
169 | /* no status property -> enabled (legacy) */ | |
170 | new_state = 1; | |
171 | } | |
172 | break; | |
173 | case OF_RECONFIG_UPDATE_PROPERTY: | |
174 | if (is_status) { | |
175 | prev_state = old_status_state != 0; | |
176 | new_state = status_state != 0; | |
177 | } | |
178 | break; | |
179 | } | |
180 | ||
181 | if (prev_state == new_state) | |
182 | return OF_RECONFIG_NO_CHANGE; | |
183 | ||
184 | return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE; | |
185 | } | |
186 | EXPORT_SYMBOL_GPL(of_reconfig_get_state_change); | |
187 | ||
6afc0dc3 | 188 | int of_property_notify(int action, struct device_node *np, |
259092a3 | 189 | struct property *prop, struct property *oldprop) |
6afc0dc3 | 190 | { |
f5242e5a | 191 | struct of_reconfig_data pr; |
6afc0dc3 GL |
192 | |
193 | /* only call notifiers if the node is attached */ | |
194 | if (!of_node_is_attached(np)) | |
195 | return 0; | |
196 | ||
197 | pr.dn = np; | |
198 | pr.prop = prop; | |
259092a3 | 199 | pr.old_prop = oldprop; |
6afc0dc3 GL |
200 | return of_reconfig_notify(action, &pr); |
201 | } | |
202 | ||
24996951 | 203 | static void __of_attach_node(struct device_node *np) |
d8c50088 | 204 | { |
a25095d4 GL |
205 | const __be32 *phandle; |
206 | int sz; | |
207 | ||
208 | np->name = __of_get_property(np, "name", NULL) ? : "<NULL>"; | |
209 | np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>"; | |
210 | ||
211 | phandle = __of_get_property(np, "phandle", &sz); | |
212 | if (!phandle) | |
213 | phandle = __of_get_property(np, "linux,phandle", &sz); | |
f76502aa | 214 | if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle) |
a25095d4 GL |
215 | phandle = __of_get_property(np, "ibm,phandle", &sz); |
216 | np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0; | |
217 | ||
6162dbe4 | 218 | np->child = NULL; |
d8c50088 | 219 | np->sibling = np->parent->child; |
d8c50088 PA |
220 | np->parent->child = np; |
221 | of_node_clear_flag(np, OF_DETACHED); | |
222 | } | |
223 | ||
6afc0dc3 GL |
224 | /** |
225 | * of_attach_node() - Plug a device node into the tree and global list. | |
226 | */ | |
227 | int of_attach_node(struct device_node *np) | |
228 | { | |
f5242e5a | 229 | struct of_reconfig_data rd; |
6afc0dc3 | 230 | unsigned long flags; |
6afc0dc3 | 231 | |
f5242e5a GL |
232 | memset(&rd, 0, sizeof(rd)); |
233 | rd.dn = np; | |
234 | ||
8a2b22a2 | 235 | mutex_lock(&of_mutex); |
6afc0dc3 | 236 | raw_spin_lock_irqsave(&devtree_lock, flags); |
d8c50088 | 237 | __of_attach_node(np); |
6afc0dc3 GL |
238 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
239 | ||
8a2b22a2 GL |
240 | __of_attach_node_sysfs(np); |
241 | mutex_unlock(&of_mutex); | |
259092a3 | 242 | |
f5242e5a | 243 | of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd); |
259092a3 | 244 | |
6afc0dc3 GL |
245 | return 0; |
246 | } | |
247 | ||
d8c50088 | 248 | void __of_detach_node(struct device_node *np) |
6afc0dc3 GL |
249 | { |
250 | struct device_node *parent; | |
6afc0dc3 | 251 | |
d8c50088 PA |
252 | if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) |
253 | return; | |
6afc0dc3 GL |
254 | |
255 | parent = np->parent; | |
d8c50088 PA |
256 | if (WARN_ON(!parent)) |
257 | return; | |
6afc0dc3 | 258 | |
6afc0dc3 GL |
259 | if (parent->child == np) |
260 | parent->child = np->sibling; | |
261 | else { | |
262 | struct device_node *prevsib; | |
263 | for (prevsib = np->parent->child; | |
264 | prevsib->sibling != np; | |
265 | prevsib = prevsib->sibling) | |
266 | ; | |
267 | prevsib->sibling = np->sibling; | |
268 | } | |
269 | ||
270 | of_node_set_flag(np, OF_DETACHED); | |
d8c50088 PA |
271 | } |
272 | ||
273 | /** | |
274 | * of_detach_node() - "Unplug" a node from the device tree. | |
d8c50088 PA |
275 | */ |
276 | int of_detach_node(struct device_node *np) | |
277 | { | |
f5242e5a | 278 | struct of_reconfig_data rd; |
d8c50088 PA |
279 | unsigned long flags; |
280 | int rc = 0; | |
281 | ||
f5242e5a GL |
282 | memset(&rd, 0, sizeof(rd)); |
283 | rd.dn = np; | |
284 | ||
8a2b22a2 | 285 | mutex_lock(&of_mutex); |
d8c50088 PA |
286 | raw_spin_lock_irqsave(&devtree_lock, flags); |
287 | __of_detach_node(np); | |
6afc0dc3 GL |
288 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
289 | ||
8a2b22a2 GL |
290 | __of_detach_node_sysfs(np); |
291 | mutex_unlock(&of_mutex); | |
259092a3 | 292 | |
f5242e5a | 293 | of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd); |
259092a3 | 294 | |
6afc0dc3 GL |
295 | return rc; |
296 | } | |
bb91f923 | 297 | EXPORT_SYMBOL_GPL(of_detach_node); |
6afc0dc3 | 298 | |
070ea018 LW |
299 | static void property_list_free(struct property *prop_list) |
300 | { | |
301 | struct property *prop, *next; | |
302 | ||
303 | for (prop = prop_list; prop != NULL; prop = next) { | |
304 | next = prop->next; | |
305 | kfree(prop->name); | |
306 | kfree(prop->value); | |
307 | kfree(prop); | |
308 | } | |
309 | } | |
310 | ||
6afc0dc3 GL |
311 | /** |
312 | * of_node_release() - release a dynamically allocated node | |
313 | * @kref: kref element of the node to be released | |
314 | * | |
315 | * In of_node_put() this function is passed to kref_put() as the destructor. | |
316 | */ | |
317 | void of_node_release(struct kobject *kobj) | |
318 | { | |
319 | struct device_node *node = kobj_to_device_node(kobj); | |
6afc0dc3 GL |
320 | |
321 | /* We should never be releasing nodes that haven't been detached. */ | |
322 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
0d638a07 | 323 | pr_err("ERROR: Bad of_node_put() on %pOF\n", node); |
6afc0dc3 GL |
324 | dump_stack(); |
325 | return; | |
326 | } | |
6afc0dc3 GL |
327 | if (!of_node_check_flag(node, OF_DYNAMIC)) |
328 | return; | |
329 | ||
144552c7 FR |
330 | if (of_node_check_flag(node, OF_OVERLAY)) { |
331 | ||
332 | if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET)) { | |
333 | /* premature refcount of zero, do not free memory */ | |
334 | pr_err("ERROR: memory leak before free overlay changeset, %pOF\n", | |
335 | node); | |
336 | return; | |
337 | } | |
338 | ||
339 | /* | |
340 | * If node->properties non-empty then properties were added | |
341 | * to this node either by different overlay that has not | |
342 | * yet been removed, or by a non-overlay mechanism. | |
343 | */ | |
344 | if (node->properties) | |
345 | pr_err("ERROR: %s(), unexpected properties in %pOF\n", | |
346 | __func__, node); | |
347 | } | |
348 | ||
070ea018 LW |
349 | property_list_free(node->properties); |
350 | property_list_free(node->deadprops); | |
6afc0dc3 | 351 | |
6afc0dc3 GL |
352 | kfree(node->full_name); |
353 | kfree(node->data); | |
354 | kfree(node); | |
355 | } | |
69843396 PA |
356 | |
357 | /** | |
358 | * __of_prop_dup - Copy a property dynamically. | |
359 | * @prop: Property to copy | |
360 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
361 | * | |
362 | * Copy a property by dynamically allocating the memory of both the | |
27b3383a | 363 | * property structure and the property name & contents. The property's |
69843396 PA |
364 | * flags have the OF_DYNAMIC bit set so that we can differentiate between |
365 | * dynamically allocated properties and not. | |
366 | * Returns the newly allocated property or NULL on out of memory error. | |
367 | */ | |
368 | struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) | |
369 | { | |
370 | struct property *new; | |
371 | ||
372 | new = kzalloc(sizeof(*new), allocflags); | |
373 | if (!new) | |
374 | return NULL; | |
375 | ||
376 | /* | |
377 | * NOTE: There is no check for zero length value. | |
b6ae5dc5 | 378 | * In case of a boolean property, this will allocate a value |
69843396 PA |
379 | * of zero bytes. We do this to work around the use |
380 | * of of_get_property() calls on boolean values. | |
381 | */ | |
382 | new->name = kstrdup(prop->name, allocflags); | |
383 | new->value = kmemdup(prop->value, prop->length, allocflags); | |
384 | new->length = prop->length; | |
385 | if (!new->name || !new->value) | |
386 | goto err_free; | |
387 | ||
388 | /* mark the property as dynamic */ | |
389 | of_property_set_flag(new, OF_DYNAMIC); | |
390 | ||
391 | return new; | |
392 | ||
393 | err_free: | |
394 | kfree(new->name); | |
395 | kfree(new->value); | |
396 | kfree(new); | |
397 | return NULL; | |
398 | } | |
399 | ||
400 | /** | |
e5179581 | 401 | * __of_node_dup() - Duplicate or create an empty device node dynamically. |
b89dae18 FR |
402 | * @np: if not NULL, contains properties to be duplicated in new node |
403 | * @full_name: string value to be duplicated into new node's full_name field | |
69843396 | 404 | * |
b89dae18 FR |
405 | * Create a device tree node, optionally duplicating the properties of |
406 | * another node. The node data are dynamically allocated and all the node | |
407 | * flags have the OF_DYNAMIC & OF_DETACHED bits set. | |
408 | * | |
409 | * Returns the newly allocated node or NULL on out of memory error. | |
69843396 | 410 | */ |
b89dae18 FR |
411 | struct device_node *__of_node_dup(const struct device_node *np, |
412 | const char *full_name) | |
69843396 PA |
413 | { |
414 | struct device_node *node; | |
415 | ||
ef8bbd73 | 416 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
69843396 PA |
417 | if (!node) |
418 | return NULL; | |
b89dae18 | 419 | node->full_name = kstrdup(full_name, GFP_KERNEL); |
e5179581 GL |
420 | if (!node->full_name) { |
421 | kfree(node); | |
422 | return NULL; | |
423 | } | |
69843396 | 424 | |
ef8bbd73 GL |
425 | of_node_set_flag(node, OF_DYNAMIC); |
426 | of_node_set_flag(node, OF_DETACHED); | |
69843396 PA |
427 | of_node_init(node); |
428 | ||
e5179581 GL |
429 | /* Iterate over and duplicate all properties */ |
430 | if (np) { | |
431 | struct property *pp, *new_pp; | |
432 | for_each_property_of_node(np, pp) { | |
433 | new_pp = __of_prop_dup(pp, GFP_KERNEL); | |
434 | if (!new_pp) | |
435 | goto err_prop; | |
436 | if (__of_add_property(node, new_pp)) { | |
437 | kfree(new_pp->name); | |
438 | kfree(new_pp->value); | |
439 | kfree(new_pp); | |
440 | goto err_prop; | |
441 | } | |
442 | } | |
443 | } | |
69843396 PA |
444 | return node; |
445 | ||
e5179581 GL |
446 | err_prop: |
447 | of_node_put(node); /* Frees the node and properties */ | |
69843396 PA |
448 | return NULL; |
449 | } | |
201c910b PA |
450 | |
451 | static void __of_changeset_entry_destroy(struct of_changeset_entry *ce) | |
452 | { | |
144552c7 FR |
453 | if (ce->action == OF_RECONFIG_ATTACH_NODE && |
454 | of_node_check_flag(ce->np, OF_OVERLAY)) { | |
455 | if (kref_read(&ce->np->kobj.kref) > 1) { | |
456 | 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", | |
457 | kref_read(&ce->np->kobj.kref), ce->np); | |
458 | } else { | |
459 | of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET); | |
460 | } | |
461 | } | |
462 | ||
201c910b PA |
463 | of_node_put(ce->np); |
464 | list_del(&ce->node); | |
465 | kfree(ce); | |
466 | } | |
467 | ||
468 | #ifdef DEBUG | |
469 | static void __of_changeset_entry_dump(struct of_changeset_entry *ce) | |
470 | { | |
471 | switch (ce->action) { | |
472 | case OF_RECONFIG_ADD_PROPERTY: | |
201c910b | 473 | case OF_RECONFIG_REMOVE_PROPERTY: |
201c910b | 474 | case OF_RECONFIG_UPDATE_PROPERTY: |
0d638a07 RH |
475 | pr_debug("cset<%p> %-15s %pOF/%s\n", ce, action_names[ce->action], |
476 | ce->np, ce->prop->name); | |
201c910b PA |
477 | break; |
478 | case OF_RECONFIG_ATTACH_NODE: | |
201c910b | 479 | case OF_RECONFIG_DETACH_NODE: |
0d638a07 RH |
480 | pr_debug("cset<%p> %-15s %pOF\n", ce, action_names[ce->action], |
481 | ce->np); | |
201c910b PA |
482 | break; |
483 | } | |
484 | } | |
485 | #else | |
486 | static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce) | |
487 | { | |
488 | /* empty */ | |
489 | } | |
490 | #endif | |
491 | ||
492 | static void __of_changeset_entry_invert(struct of_changeset_entry *ce, | |
493 | struct of_changeset_entry *rce) | |
494 | { | |
495 | memcpy(rce, ce, sizeof(*rce)); | |
496 | ||
497 | switch (ce->action) { | |
498 | case OF_RECONFIG_ATTACH_NODE: | |
499 | rce->action = OF_RECONFIG_DETACH_NODE; | |
500 | break; | |
501 | case OF_RECONFIG_DETACH_NODE: | |
502 | rce->action = OF_RECONFIG_ATTACH_NODE; | |
503 | break; | |
504 | case OF_RECONFIG_ADD_PROPERTY: | |
505 | rce->action = OF_RECONFIG_REMOVE_PROPERTY; | |
506 | break; | |
507 | case OF_RECONFIG_REMOVE_PROPERTY: | |
508 | rce->action = OF_RECONFIG_ADD_PROPERTY; | |
509 | break; | |
510 | case OF_RECONFIG_UPDATE_PROPERTY: | |
511 | rce->old_prop = ce->prop; | |
512 | rce->prop = ce->old_prop; | |
b9c43856 PA |
513 | /* update was used but original property did not exist */ |
514 | if (!rce->prop) { | |
515 | rce->action = OF_RECONFIG_REMOVE_PROPERTY; | |
516 | rce->prop = ce->prop; | |
517 | } | |
201c910b PA |
518 | break; |
519 | } | |
520 | } | |
521 | ||
24789c5c FR |
522 | static int __of_changeset_entry_notify(struct of_changeset_entry *ce, |
523 | bool revert) | |
201c910b | 524 | { |
f5242e5a | 525 | struct of_reconfig_data rd; |
201c910b | 526 | struct of_changeset_entry ce_inverted; |
24789c5c | 527 | int ret = 0; |
201c910b PA |
528 | |
529 | if (revert) { | |
530 | __of_changeset_entry_invert(ce, &ce_inverted); | |
531 | ce = &ce_inverted; | |
532 | } | |
533 | ||
534 | switch (ce->action) { | |
535 | case OF_RECONFIG_ATTACH_NODE: | |
536 | case OF_RECONFIG_DETACH_NODE: | |
f5242e5a GL |
537 | memset(&rd, 0, sizeof(rd)); |
538 | rd.dn = ce->np; | |
539 | ret = of_reconfig_notify(ce->action, &rd); | |
201c910b PA |
540 | break; |
541 | case OF_RECONFIG_ADD_PROPERTY: | |
542 | case OF_RECONFIG_REMOVE_PROPERTY: | |
543 | case OF_RECONFIG_UPDATE_PROPERTY: | |
544 | ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop); | |
545 | break; | |
546 | default: | |
606ad42a | 547 | pr_err("invalid devicetree changeset action: %i\n", |
201c910b | 548 | (int)ce->action); |
24789c5c | 549 | ret = -EINVAL; |
201c910b PA |
550 | } |
551 | ||
552 | if (ret) | |
0d638a07 | 553 | pr_err("changeset notifier error @%pOF\n", ce->np); |
24789c5c | 554 | return ret; |
201c910b PA |
555 | } |
556 | ||
557 | static int __of_changeset_entry_apply(struct of_changeset_entry *ce) | |
558 | { | |
559 | struct property *old_prop, **propp; | |
560 | unsigned long flags; | |
561 | int ret = 0; | |
562 | ||
563 | __of_changeset_entry_dump(ce); | |
564 | ||
565 | raw_spin_lock_irqsave(&devtree_lock, flags); | |
566 | switch (ce->action) { | |
567 | case OF_RECONFIG_ATTACH_NODE: | |
568 | __of_attach_node(ce->np); | |
569 | break; | |
570 | case OF_RECONFIG_DETACH_NODE: | |
571 | __of_detach_node(ce->np); | |
572 | break; | |
573 | case OF_RECONFIG_ADD_PROPERTY: | |
574 | /* If the property is in deadprops then it must be removed */ | |
575 | for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { | |
576 | if (*propp == ce->prop) { | |
577 | *propp = ce->prop->next; | |
578 | ce->prop->next = NULL; | |
579 | break; | |
580 | } | |
581 | } | |
582 | ||
583 | ret = __of_add_property(ce->np, ce->prop); | |
584 | if (ret) { | |
0d638a07 RH |
585 | pr_err("changeset: add_property failed @%pOF/%s\n", |
586 | ce->np, | |
201c910b PA |
587 | ce->prop->name); |
588 | break; | |
589 | } | |
590 | break; | |
591 | case OF_RECONFIG_REMOVE_PROPERTY: | |
592 | ret = __of_remove_property(ce->np, ce->prop); | |
593 | if (ret) { | |
0d638a07 RH |
594 | pr_err("changeset: remove_property failed @%pOF/%s\n", |
595 | ce->np, | |
201c910b PA |
596 | ce->prop->name); |
597 | break; | |
598 | } | |
599 | break; | |
600 | ||
601 | case OF_RECONFIG_UPDATE_PROPERTY: | |
602 | /* If the property is in deadprops then it must be removed */ | |
603 | for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { | |
604 | if (*propp == ce->prop) { | |
605 | *propp = ce->prop->next; | |
606 | ce->prop->next = NULL; | |
607 | break; | |
608 | } | |
609 | } | |
610 | ||
611 | ret = __of_update_property(ce->np, ce->prop, &old_prop); | |
612 | if (ret) { | |
0d638a07 RH |
613 | pr_err("changeset: update_property failed @%pOF/%s\n", |
614 | ce->np, | |
201c910b PA |
615 | ce->prop->name); |
616 | break; | |
617 | } | |
618 | break; | |
619 | default: | |
620 | ret = -EINVAL; | |
621 | } | |
622 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | |
623 | ||
624 | if (ret) | |
625 | return ret; | |
626 | ||
627 | switch (ce->action) { | |
628 | case OF_RECONFIG_ATTACH_NODE: | |
629 | __of_attach_node_sysfs(ce->np); | |
630 | break; | |
631 | case OF_RECONFIG_DETACH_NODE: | |
632 | __of_detach_node_sysfs(ce->np); | |
633 | break; | |
634 | case OF_RECONFIG_ADD_PROPERTY: | |
635 | /* ignore duplicate names */ | |
636 | __of_add_property_sysfs(ce->np, ce->prop); | |
637 | break; | |
638 | case OF_RECONFIG_REMOVE_PROPERTY: | |
639 | __of_remove_property_sysfs(ce->np, ce->prop); | |
640 | break; | |
641 | case OF_RECONFIG_UPDATE_PROPERTY: | |
642 | __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop); | |
643 | break; | |
644 | } | |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
649 | static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce) | |
650 | { | |
651 | struct of_changeset_entry ce_inverted; | |
652 | ||
653 | __of_changeset_entry_invert(ce, &ce_inverted); | |
654 | return __of_changeset_entry_apply(&ce_inverted); | |
655 | } | |
656 | ||
657 | /** | |
658 | * of_changeset_init - Initialize a changeset for use | |
659 | * | |
660 | * @ocs: changeset pointer | |
661 | * | |
662 | * Initialize a changeset structure | |
663 | */ | |
664 | void of_changeset_init(struct of_changeset *ocs) | |
665 | { | |
666 | memset(ocs, 0, sizeof(*ocs)); | |
667 | INIT_LIST_HEAD(&ocs->entries); | |
668 | } | |
18322377 | 669 | EXPORT_SYMBOL_GPL(of_changeset_init); |
201c910b PA |
670 | |
671 | /** | |
672 | * of_changeset_destroy - Destroy a changeset | |
673 | * | |
674 | * @ocs: changeset pointer | |
675 | * | |
676 | * Destroys a changeset. Note that if a changeset is applied, | |
677 | * its changes to the tree cannot be reverted. | |
678 | */ | |
679 | void of_changeset_destroy(struct of_changeset *ocs) | |
680 | { | |
681 | struct of_changeset_entry *ce, *cen; | |
682 | ||
683 | list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node) | |
684 | __of_changeset_entry_destroy(ce); | |
685 | } | |
18322377 | 686 | EXPORT_SYMBOL_GPL(of_changeset_destroy); |
201c910b | 687 | |
24789c5c FR |
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) | |
201c910b PA |
699 | { |
700 | struct of_changeset_entry *ce; | |
24789c5c | 701 | int ret, ret_tmp; |
201c910b | 702 | |
606ad42a | 703 | pr_debug("changeset: applying...\n"); |
201c910b PA |
704 | list_for_each_entry(ce, &ocs->entries, node) { |
705 | ret = __of_changeset_entry_apply(ce); | |
706 | if (ret) { | |
606ad42a | 707 | pr_err("Error applying changeset (%d)\n", ret); |
24789c5c FR |
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 | } | |
201c910b PA |
714 | return ret; |
715 | } | |
716 | } | |
24789c5c FR |
717 | |
718 | return 0; | |
719 | } | |
720 | ||
721 | /* | |
722 | * Returns 0 on success, a negative error value in case of an error. | |
723 | * | |
e9d92e40 | 724 | * If multiple changeset entry notification errors occur then only the |
24789c5c FR |
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"); | |
201c910b PA |
733 | |
734 | /* drop the global lock while emitting notifiers */ | |
735 | mutex_unlock(&of_mutex); | |
24789c5c FR |
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 | } | |
201c910b | 741 | mutex_lock(&of_mutex); |
606ad42a | 742 | pr_debug("changeset: notifiers sent.\n"); |
201c910b | 743 | |
24789c5c FR |
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; | |
201c910b PA |
764 | } |
765 | ||
766 | /** | |
18322377 | 767 | * of_changeset_apply - Applies a changeset |
201c910b PA |
768 | * |
769 | * @ocs: changeset pointer | |
770 | * | |
18322377 GS |
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. | |
201c910b | 775 | * Returns 0 on success, a negative error value in case of an error. |
18322377 | 776 | * On error the partially applied effects are reverted. |
201c910b | 777 | */ |
18322377 GS |
778 | int of_changeset_apply(struct of_changeset *ocs) |
779 | { | |
780 | int ret; | |
781 | ||
782 | mutex_lock(&of_mutex); | |
783 | ret = __of_changeset_apply(ocs); | |
784 | mutex_unlock(&of_mutex); | |
785 | ||
786 | return ret; | |
787 | } | |
788 | EXPORT_SYMBOL_GPL(of_changeset_apply); | |
789 | ||
24789c5c FR |
790 | /* |
791 | * Revert the changeset entries in @ocs. | |
792 | * If revert fails, an attempt is made to re-apply the entries that were | |
793 | * successfully removed. | |
794 | * | |
795 | * If multiple re-apply errors occur then only the final apply error is | |
796 | * reported. | |
797 | * | |
798 | * Returns 0 on success, a negative error value in case of an error. | |
799 | * If an apply error occurs, it is returned in *ret_apply. | |
800 | */ | |
801 | int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply) | |
201c910b PA |
802 | { |
803 | struct of_changeset_entry *ce; | |
24789c5c | 804 | int ret, ret_tmp; |
201c910b | 805 | |
606ad42a | 806 | pr_debug("changeset: reverting...\n"); |
201c910b PA |
807 | list_for_each_entry_reverse(ce, &ocs->entries, node) { |
808 | ret = __of_changeset_entry_revert(ce); | |
809 | if (ret) { | |
606ad42a | 810 | pr_err("Error reverting changeset (%d)\n", ret); |
24789c5c FR |
811 | list_for_each_entry_continue(ce, &ocs->entries, node) { |
812 | ret_tmp = __of_changeset_entry_apply(ce); | |
813 | if (ret_tmp) | |
814 | *ret_apply = ret_tmp; | |
815 | } | |
201c910b PA |
816 | return ret; |
817 | } | |
818 | } | |
24789c5c FR |
819 | |
820 | return 0; | |
821 | } | |
822 | ||
823 | /* | |
e9d92e40 | 824 | * If multiple changeset entry notification errors occur then only the |
24789c5c FR |
825 | * final notification error is reported. |
826 | */ | |
827 | int __of_changeset_revert_notify(struct of_changeset *ocs) | |
828 | { | |
829 | struct of_changeset_entry *ce; | |
830 | int ret = 0, ret_tmp; | |
831 | ||
832 | pr_debug("changeset: emitting notifiers.\n"); | |
201c910b PA |
833 | |
834 | /* drop the global lock while emitting notifiers */ | |
835 | mutex_unlock(&of_mutex); | |
24789c5c FR |
836 | list_for_each_entry_reverse(ce, &ocs->entries, node) { |
837 | ret_tmp = __of_changeset_entry_notify(ce, 1); | |
838 | if (ret_tmp) | |
839 | ret = ret_tmp; | |
840 | } | |
201c910b | 841 | mutex_lock(&of_mutex); |
606ad42a | 842 | pr_debug("changeset: notifiers sent.\n"); |
201c910b | 843 | |
24789c5c FR |
844 | return ret; |
845 | } | |
846 | ||
847 | static int __of_changeset_revert(struct of_changeset *ocs) | |
848 | { | |
849 | int ret, ret_reply; | |
850 | ||
851 | ret_reply = 0; | |
852 | ret = __of_changeset_revert_entries(ocs, &ret_reply); | |
853 | ||
854 | if (!ret) | |
855 | ret = __of_changeset_revert_notify(ocs); | |
856 | ||
857 | return ret; | |
201c910b PA |
858 | } |
859 | ||
18322377 GS |
860 | /** |
861 | * of_changeset_revert - Reverts an applied changeset | |
862 | * | |
863 | * @ocs: changeset pointer | |
864 | * | |
865 | * Reverts a changeset returning the state of the tree to what it | |
866 | * was before the application. | |
867 | * Any side-effects like creation/destruction of devices and | |
868 | * removal of sysfs properties and directories are applied. | |
869 | * Returns 0 on success, a negative error value in case of an error. | |
870 | */ | |
871 | int of_changeset_revert(struct of_changeset *ocs) | |
872 | { | |
873 | int ret; | |
874 | ||
875 | mutex_lock(&of_mutex); | |
876 | ret = __of_changeset_revert(ocs); | |
877 | mutex_unlock(&of_mutex); | |
878 | ||
879 | return ret; | |
880 | } | |
881 | EXPORT_SYMBOL_GPL(of_changeset_revert); | |
882 | ||
201c910b | 883 | /** |
0290c4ca | 884 | * of_changeset_action - Add an action to the tail of the changeset list |
201c910b PA |
885 | * |
886 | * @ocs: changeset pointer | |
887 | * @action: action to perform | |
888 | * @np: Pointer to device node | |
889 | * @prop: Pointer to property | |
890 | * | |
891 | * On action being one of: | |
892 | * + OF_RECONFIG_ATTACH_NODE | |
893 | * + OF_RECONFIG_DETACH_NODE, | |
894 | * + OF_RECONFIG_ADD_PROPERTY | |
895 | * + OF_RECONFIG_REMOVE_PROPERTY, | |
896 | * + OF_RECONFIG_UPDATE_PROPERTY | |
897 | * Returns 0 on success, a negative error value in case of an error. | |
898 | */ | |
899 | int of_changeset_action(struct of_changeset *ocs, unsigned long action, | |
900 | struct device_node *np, struct property *prop) | |
901 | { | |
902 | struct of_changeset_entry *ce; | |
903 | ||
904 | ce = kzalloc(sizeof(*ce), GFP_KERNEL); | |
606ad42a | 905 | if (!ce) |
201c910b | 906 | return -ENOMEM; |
606ad42a | 907 | |
201c910b PA |
908 | /* get a reference to the node */ |
909 | ce->action = action; | |
910 | ce->np = of_node_get(np); | |
911 | ce->prop = prop; | |
912 | ||
913 | if (action == OF_RECONFIG_UPDATE_PROPERTY && prop) | |
914 | ce->old_prop = of_find_property(np, prop->name, NULL); | |
915 | ||
916 | /* add it to the list */ | |
917 | list_add_tail(&ce->node, &ocs->entries); | |
918 | return 0; | |
919 | } | |
18322377 | 920 | EXPORT_SYMBOL_GPL(of_changeset_action); |