1
2
3
4
5
6
7
8
9
10
11#undef DEBUG
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/of_device.h>
16#include <linux/string.h>
17#include <linux/ctype.h>
18#include <linux/errno.h>
19#include <linux/string.h>
20#include <linux/slab.h>
21#include <linux/err.h>
22#include <linux/idr.h>
23
24#include "of_private.h"
25
26
27
28
29
30
31
32
33
34struct of_overlay_info {
35 struct device_node *target;
36 struct device_node *overlay;
37};
38
39
40
41
42
43
44
45
46
47
48struct of_overlay {
49 int id;
50 struct list_head node;
51 int count;
52 struct of_overlay_info *ovinfo_tab;
53 struct of_changeset cset;
54};
55
56static int of_overlay_apply_one(struct of_overlay *ov,
57 struct device_node *target, const struct device_node *overlay);
58
59static int of_overlay_apply_single_property(struct of_overlay *ov,
60 struct device_node *target, struct property *prop)
61{
62 struct property *propn, *tprop;
63
64
65 tprop = of_find_property(target, prop->name, NULL);
66
67
68 if (of_prop_cmp(prop->name, "name") == 0 ||
69 of_prop_cmp(prop->name, "phandle") == 0 ||
70 of_prop_cmp(prop->name, "linux,phandle") == 0)
71 return 0;
72
73 propn = __of_prop_dup(prop, GFP_KERNEL);
74 if (propn == NULL)
75 return -ENOMEM;
76
77
78 if (tprop == NULL)
79 return of_changeset_add_property(&ov->cset, target, propn);
80
81
82 return of_changeset_update_property(&ov->cset, target, propn);
83}
84
85static int of_overlay_apply_single_device_node(struct of_overlay *ov,
86 struct device_node *target, struct device_node *child)
87{
88 const char *cname;
89 struct device_node *tchild;
90 int ret = 0;
91
92 cname = kbasename(child->full_name);
93 if (cname == NULL)
94 return -ENOMEM;
95
96
97 tchild = of_get_child_by_name(target, cname);
98 if (tchild != NULL) {
99
100 ret = of_overlay_apply_one(ov, tchild, child);
101 of_node_put(tchild);
102 } else {
103
104 tchild = __of_node_dup(child, "%s/%s", target->full_name, cname);
105 if (!tchild)
106 return -ENOMEM;
107
108
109 tchild->parent = target;
110
111 ret = of_changeset_attach_node(&ov->cset, tchild);
112 if (ret)
113 return ret;
114
115 ret = of_overlay_apply_one(ov, tchild, child);
116 if (ret)
117 return ret;
118 }
119
120 return ret;
121}
122
123
124
125
126
127
128
129
130static int of_overlay_apply_one(struct of_overlay *ov,
131 struct device_node *target, const struct device_node *overlay)
132{
133 struct device_node *child;
134 struct property *prop;
135 int ret;
136
137 for_each_property_of_node(overlay, prop) {
138 ret = of_overlay_apply_single_property(ov, target, prop);
139 if (ret) {
140 pr_err("%s: Failed to apply prop @%s/%s\n",
141 __func__, target->full_name, prop->name);
142 return ret;
143 }
144 }
145
146 for_each_child_of_node(overlay, child) {
147 ret = of_overlay_apply_single_device_node(ov, target, child);
148 if (ret != 0) {
149 pr_err("%s: Failed to apply single node @%s/%s\n",
150 __func__, target->full_name,
151 child->name);
152 of_node_put(child);
153 return ret;
154 }
155 }
156
157 return 0;
158}
159
160
161
162
163
164
165
166
167
168
169static int of_overlay_apply(struct of_overlay *ov)
170{
171 int i, err;
172
173
174 for (i = 0; i < ov->count; i++) {
175 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
176
177 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
178 if (err != 0) {
179 pr_err("%s: overlay failed '%s'\n",
180 __func__, ovinfo->target->full_name);
181 return err;
182 }
183 }
184
185 return 0;
186}
187
188
189
190
191
192
193
194
195static struct device_node *find_target_node(struct device_node *info_node)
196{
197 const char *path;
198 u32 val;
199 int ret;
200
201
202 ret = of_property_read_u32(info_node, "target", &val);
203 if (ret == 0)
204 return of_find_node_by_phandle(val);
205
206
207 ret = of_property_read_string(info_node, "target-path", &path);
208 if (ret == 0)
209 return of_find_node_by_path(path);
210
211 pr_err("%s: Failed to find target for node %p (%s)\n", __func__,
212 info_node, info_node->name);
213
214 return NULL;
215}
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231static int of_fill_overlay_info(struct of_overlay *ov,
232 struct device_node *info_node, struct of_overlay_info *ovinfo)
233{
234 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
235 if (ovinfo->overlay == NULL)
236 goto err_fail;
237
238 ovinfo->target = find_target_node(info_node);
239 if (ovinfo->target == NULL)
240 goto err_fail;
241
242 return 0;
243
244err_fail:
245 of_node_put(ovinfo->target);
246 of_node_put(ovinfo->overlay);
247
248 memset(ovinfo, 0, sizeof(*ovinfo));
249 return -EINVAL;
250}
251
252
253
254
255
256
257
258
259
260
261
262
263
264static int of_build_overlay_info(struct of_overlay *ov,
265 struct device_node *tree)
266{
267 struct device_node *node;
268 struct of_overlay_info *ovinfo;
269 int cnt, err;
270
271
272 cnt = 0;
273 for_each_child_of_node(tree, node)
274 cnt++;
275
276 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
277 if (ovinfo == NULL)
278 return -ENOMEM;
279
280 cnt = 0;
281 for_each_child_of_node(tree, node) {
282 memset(&ovinfo[cnt], 0, sizeof(*ovinfo));
283 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
284 if (err == 0)
285 cnt++;
286 }
287
288
289 if (cnt == 0) {
290 kfree(ovinfo);
291 return -ENODEV;
292 }
293
294 ov->count = cnt;
295 ov->ovinfo_tab = ovinfo;
296
297 return 0;
298}
299
300
301
302
303
304
305
306
307
308
309static int of_free_overlay_info(struct of_overlay *ov)
310{
311 struct of_overlay_info *ovinfo;
312 int i;
313
314
315 for (i = ov->count - 1; i >= 0; i--) {
316 ovinfo = &ov->ovinfo_tab[i];
317
318 of_node_put(ovinfo->target);
319 of_node_put(ovinfo->overlay);
320 }
321 kfree(ov->ovinfo_tab);
322
323 return 0;
324}
325
326static LIST_HEAD(ov_list);
327static DEFINE_IDR(ov_idr);
328
329
330
331
332
333
334
335
336
337
338
339int of_overlay_create(struct device_node *tree)
340{
341 struct of_overlay *ov;
342 int err, id;
343
344
345 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
346 if (ov == NULL)
347 return -ENOMEM;
348 ov->id = -1;
349
350 INIT_LIST_HEAD(&ov->node);
351
352 of_changeset_init(&ov->cset);
353
354 mutex_lock(&of_mutex);
355
356 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
357 if (id < 0) {
358 pr_err("%s: idr_alloc() failed for tree@%s\n",
359 __func__, tree->full_name);
360 err = id;
361 goto err_destroy_trans;
362 }
363 ov->id = id;
364
365
366 err = of_build_overlay_info(ov, tree);
367 if (err) {
368 pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
369 __func__, tree->full_name);
370 goto err_free_idr;
371 }
372
373
374 err = of_overlay_apply(ov);
375 if (err) {
376 pr_err("%s: of_overlay_apply() failed for tree@%s\n",
377 __func__, tree->full_name);
378 goto err_abort_trans;
379 }
380
381
382 err = __of_changeset_apply(&ov->cset);
383 if (err) {
384 pr_err("%s: __of_changeset_apply() failed for tree@%s\n",
385 __func__, tree->full_name);
386 goto err_revert_overlay;
387 }
388
389
390 list_add_tail(&ov->node, &ov_list);
391
392 mutex_unlock(&of_mutex);
393
394 return id;
395
396err_revert_overlay:
397err_abort_trans:
398 of_free_overlay_info(ov);
399err_free_idr:
400 idr_remove(&ov_idr, ov->id);
401err_destroy_trans:
402 of_changeset_destroy(&ov->cset);
403 kfree(ov);
404 mutex_unlock(&of_mutex);
405
406 return err;
407}
408EXPORT_SYMBOL_GPL(of_overlay_create);
409
410
411static int overlay_subtree_check(struct device_node *tree,
412 struct device_node *dn)
413{
414 struct device_node *child;
415
416
417 if (tree == dn)
418 return 1;
419
420 for_each_child_of_node(tree, child) {
421 if (overlay_subtree_check(child, dn)) {
422 of_node_put(child);
423 return 1;
424 }
425 }
426
427 return 0;
428}
429
430
431static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
432{
433 struct of_overlay *ovt;
434 struct of_changeset_entry *ce;
435
436 list_for_each_entry_reverse(ovt, &ov_list, node) {
437
438 if (ovt == ov)
439 break;
440
441
442 list_for_each_entry(ce, &ovt->cset.entries, node) {
443 if (overlay_subtree_check(ce->np, dn)) {
444 pr_err("%s: #%d clashes #%d @%s\n",
445 __func__, ov->id, ovt->id,
446 dn->full_name);
447 return 0;
448 }
449 }
450 }
451
452
453 return 1;
454}
455
456
457
458
459
460
461
462
463
464
465
466static int overlay_removal_is_ok(struct of_overlay *ov)
467{
468 struct of_changeset_entry *ce;
469
470 list_for_each_entry(ce, &ov->cset.entries, node) {
471 if (!overlay_is_topmost(ov, ce->np)) {
472 pr_err("%s: overlay #%d is not topmost\n",
473 __func__, ov->id);
474 return 0;
475 }
476 }
477
478 return 1;
479}
480
481
482
483
484
485
486
487
488
489int of_overlay_destroy(int id)
490{
491 struct of_overlay *ov;
492 int err;
493
494 mutex_lock(&of_mutex);
495
496 ov = idr_find(&ov_idr, id);
497 if (ov == NULL) {
498 err = -ENODEV;
499 pr_err("%s: Could not find overlay #%d\n",
500 __func__, id);
501 goto out;
502 }
503
504
505 if (!overlay_removal_is_ok(ov)) {
506 err = -EBUSY;
507 pr_err("%s: removal check failed for overlay #%d\n",
508 __func__, id);
509 goto out;
510 }
511
512
513 list_del(&ov->node);
514 __of_changeset_revert(&ov->cset);
515 of_free_overlay_info(ov);
516 idr_remove(&ov_idr, id);
517 of_changeset_destroy(&ov->cset);
518 kfree(ov);
519
520 err = 0;
521
522out:
523 mutex_unlock(&of_mutex);
524
525 return err;
526}
527EXPORT_SYMBOL_GPL(of_overlay_destroy);
528
529
530
531
532
533
534
535
536int of_overlay_destroy_all(void)
537{
538 struct of_overlay *ov, *ovn;
539
540 mutex_lock(&of_mutex);
541
542
543 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
544 list_del(&ov->node);
545 __of_changeset_revert(&ov->cset);
546 of_free_overlay_info(ov);
547 idr_remove(&ov_idr, ov->id);
548 kfree(ov);
549 }
550
551 mutex_unlock(&of_mutex);
552
553 return 0;
554}
555EXPORT_SYMBOL_GPL(of_overlay_destroy_all);
556