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