1 /* 2 * Functions for working with device tree overlays 3 * 4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com> 5 * Copyright (C) 2012 Texas Instruments Inc. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * version 2 as published by the Free Software Foundation. 10 */ 11 12 #define pr_fmt(fmt) "OF: overlay: " fmt 13 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_device.h> 18 #include <linux/string.h> 19 #include <linux/ctype.h> 20 #include <linux/errno.h> 21 #include <linux/slab.h> 22 #include <linux/err.h> 23 #include <linux/idr.h> 24 25 #include "of_private.h" 26 27 /** 28 * struct of_overlay_info - Holds a single overlay info 29 * @target: target of the overlay operation 30 * @overlay: pointer to the overlay contents node 31 * 32 * Holds a single overlay state, including all the overlay logs & 33 * records. 34 */ 35 struct of_overlay_info { 36 struct device_node *target; 37 struct device_node *overlay; 38 bool is_symbols_node; 39 }; 40 41 /** 42 * struct of_overlay - Holds a complete overlay transaction 43 * @node: List on which we are located 44 * @count: Count of ovinfo structures 45 * @ovinfo_tab: Overlay info table (count sized) 46 * @cset: Changeset to be used 47 * 48 * Holds a complete overlay transaction 49 */ 50 struct of_overlay { 51 int id; 52 struct list_head node; 53 int count; 54 struct of_overlay_info *ovinfo_tab; 55 struct of_changeset cset; 56 }; 57 58 static int of_overlay_apply_one(struct of_overlay *ov, 59 struct device_node *target, const struct device_node *overlay, 60 bool is_symbols_node); 61 62 static BLOCKING_NOTIFIER_HEAD(of_overlay_chain); 63 64 int of_overlay_notifier_register(struct notifier_block *nb) 65 { 66 return blocking_notifier_chain_register(&of_overlay_chain, nb); 67 } 68 EXPORT_SYMBOL_GPL(of_overlay_notifier_register); 69 70 int of_overlay_notifier_unregister(struct notifier_block *nb) 71 { 72 return blocking_notifier_chain_unregister(&of_overlay_chain, nb); 73 } 74 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister); 75 76 static int of_overlay_notify(struct of_overlay *ov, 77 enum of_overlay_notify_action action) 78 { 79 struct of_overlay_notify_data nd; 80 int i, ret; 81 82 for (i = 0; i < ov->count; i++) { 83 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i]; 84 85 nd.target = ovinfo->target; 86 nd.overlay = ovinfo->overlay; 87 88 ret = blocking_notifier_call_chain(&of_overlay_chain, 89 action, &nd); 90 if (ret) 91 return notifier_to_errno(ret); 92 } 93 94 return 0; 95 } 96 97 static struct property *dup_and_fixup_symbol_prop(struct of_overlay *ov, 98 const struct property *prop) 99 { 100 struct of_overlay_info *ovinfo; 101 struct property *new; 102 const char *overlay_name; 103 char *label_path; 104 char *symbol_path; 105 const char *target_path; 106 int k; 107 int label_path_len; 108 int overlay_name_len; 109 int target_path_len; 110 111 if (!prop->value) 112 return NULL; 113 symbol_path = prop->value; 114 115 new = kzalloc(sizeof(*new), GFP_KERNEL); 116 if (!new) 117 return NULL; 118 119 for (k = 0; k < ov->count; k++) { 120 ovinfo = &ov->ovinfo_tab[k]; 121 overlay_name = ovinfo->overlay->full_name; 122 overlay_name_len = strlen(overlay_name); 123 if (!strncasecmp(symbol_path, overlay_name, overlay_name_len)) 124 break; 125 } 126 127 if (k >= ov->count) 128 goto err_free; 129 130 target_path = ovinfo->target->full_name; 131 target_path_len = strlen(target_path); 132 133 label_path = symbol_path + overlay_name_len; 134 label_path_len = strlen(label_path); 135 136 new->name = kstrdup(prop->name, GFP_KERNEL); 137 new->length = target_path_len + label_path_len + 1; 138 new->value = kzalloc(new->length, GFP_KERNEL); 139 140 if (!new->name || !new->value) 141 goto err_free; 142 143 strcpy(new->value, target_path); 144 strcpy(new->value + target_path_len, label_path); 145 146 /* mark the property as dynamic */ 147 of_property_set_flag(new, OF_DYNAMIC); 148 149 return new; 150 151 err_free: 152 kfree(new->name); 153 kfree(new->value); 154 kfree(new); 155 return NULL; 156 157 158 } 159 160 static int of_overlay_apply_single_property(struct of_overlay *ov, 161 struct device_node *target, struct property *prop, 162 bool is_symbols_node) 163 { 164 struct property *propn = NULL, *tprop; 165 166 /* NOTE: Multiple changes of single properties not supported */ 167 tprop = of_find_property(target, prop->name, NULL); 168 169 /* special properties are not meant to be updated (silent NOP) */ 170 if (of_prop_cmp(prop->name, "name") == 0 || 171 of_prop_cmp(prop->name, "phandle") == 0 || 172 of_prop_cmp(prop->name, "linux,phandle") == 0) 173 return 0; 174 175 if (is_symbols_node) { 176 /* changing a property in __symbols__ node not allowed */ 177 if (tprop) 178 return -EINVAL; 179 propn = dup_and_fixup_symbol_prop(ov, prop); 180 } else { 181 propn = __of_prop_dup(prop, GFP_KERNEL); 182 } 183 184 if (propn == NULL) 185 return -ENOMEM; 186 187 /* not found? add */ 188 if (tprop == NULL) 189 return of_changeset_add_property(&ov->cset, target, propn); 190 191 /* found? update */ 192 return of_changeset_update_property(&ov->cset, target, propn); 193 } 194 195 static int of_overlay_apply_single_device_node(struct of_overlay *ov, 196 struct device_node *target, struct device_node *child) 197 { 198 const char *cname; 199 struct device_node *tchild; 200 int ret = 0; 201 202 cname = kbasename(child->full_name); 203 if (cname == NULL) 204 return -ENOMEM; 205 206 /* NOTE: Multiple mods of created nodes not supported */ 207 for_each_child_of_node(target, tchild) 208 if (!of_node_cmp(cname, kbasename(tchild->full_name))) 209 break; 210 211 if (tchild != NULL) { 212 /* new overlay phandle value conflicts with existing value */ 213 if (child->phandle) 214 return -EINVAL; 215 216 /* apply overlay recursively */ 217 ret = of_overlay_apply_one(ov, tchild, child, 0); 218 of_node_put(tchild); 219 } else { 220 /* create empty tree as a target */ 221 tchild = __of_node_dup(child, "%pOF/%s", target, cname); 222 if (!tchild) 223 return -ENOMEM; 224 225 /* point to parent */ 226 tchild->parent = target; 227 228 ret = of_changeset_attach_node(&ov->cset, tchild); 229 if (ret) 230 return ret; 231 232 ret = of_overlay_apply_one(ov, tchild, child, 0); 233 if (ret) 234 return ret; 235 } 236 237 return ret; 238 } 239 240 /* 241 * Apply a single overlay node recursively. 242 * 243 * Note that the in case of an error the target node is left 244 * in a inconsistent state. Error recovery should be performed 245 * by using the changeset. 246 */ 247 static int of_overlay_apply_one(struct of_overlay *ov, 248 struct device_node *target, const struct device_node *overlay, 249 bool is_symbols_node) 250 { 251 struct device_node *child; 252 struct property *prop; 253 int ret; 254 255 for_each_property_of_node(overlay, prop) { 256 ret = of_overlay_apply_single_property(ov, target, prop, 257 is_symbols_node); 258 if (ret) { 259 pr_err("Failed to apply prop @%pOF/%s\n", 260 target, prop->name); 261 return ret; 262 } 263 } 264 265 /* do not allow symbols node to have any children */ 266 if (is_symbols_node) 267 return 0; 268 269 for_each_child_of_node(overlay, child) { 270 ret = of_overlay_apply_single_device_node(ov, target, child); 271 if (ret != 0) { 272 pr_err("Failed to apply single node @%pOF/%s\n", 273 target, child->name); 274 of_node_put(child); 275 return ret; 276 } 277 } 278 279 return 0; 280 } 281 282 /** 283 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab 284 * @ov: Overlay to apply 285 * 286 * Applies the overlays given, while handling all error conditions 287 * appropriately. Either the operation succeeds, or if it fails the 288 * live tree is reverted to the state before the attempt. 289 * Returns 0, or an error if the overlay attempt failed. 290 */ 291 static int of_overlay_apply(struct of_overlay *ov) 292 { 293 int i, err; 294 295 /* first we apply the overlays atomically */ 296 for (i = 0; i < ov->count; i++) { 297 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i]; 298 299 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay, 300 ovinfo->is_symbols_node); 301 if (err != 0) { 302 pr_err("apply failed '%pOF'\n", ovinfo->target); 303 return err; 304 } 305 } 306 307 return 0; 308 } 309 310 /* 311 * Find the target node using a number of different strategies 312 * in order of preference 313 * 314 * "target" property containing the phandle of the target 315 * "target-path" property containing the path of the target 316 */ 317 static struct device_node *find_target_node(struct device_node *info_node) 318 { 319 const char *path; 320 u32 val; 321 int ret; 322 323 /* first try to go by using the target as a phandle */ 324 ret = of_property_read_u32(info_node, "target", &val); 325 if (ret == 0) 326 return of_find_node_by_phandle(val); 327 328 /* now try to locate by path */ 329 ret = of_property_read_string(info_node, "target-path", &path); 330 if (ret == 0) 331 return of_find_node_by_path(path); 332 333 pr_err("Failed to find target for node %p (%s)\n", 334 info_node, info_node->name); 335 336 return NULL; 337 } 338 339 /** 340 * of_fill_overlay_info() - Fill an overlay info structure 341 * @ov Overlay to fill 342 * @info_node: Device node containing the overlay 343 * @ovinfo: Pointer to the overlay info structure to fill 344 * 345 * Fills an overlay info structure with the overlay information 346 * from a device node. This device node must have a target property 347 * which contains a phandle of the overlay target node, and an 348 * __overlay__ child node which has the overlay contents. 349 * Both ovinfo->target & ovinfo->overlay have their references taken. 350 * 351 * Returns 0 on success, or a negative error value. 352 */ 353 static int of_fill_overlay_info(struct of_overlay *ov, 354 struct device_node *info_node, struct of_overlay_info *ovinfo) 355 { 356 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__"); 357 if (ovinfo->overlay == NULL) 358 goto err_fail; 359 360 ovinfo->target = find_target_node(info_node); 361 if (ovinfo->target == NULL) 362 goto err_fail; 363 364 return 0; 365 366 err_fail: 367 of_node_put(ovinfo->target); 368 of_node_put(ovinfo->overlay); 369 370 memset(ovinfo, 0, sizeof(*ovinfo)); 371 return -EINVAL; 372 } 373 374 /** 375 * of_build_overlay_info() - Build an overlay info array 376 * @ov Overlay to build 377 * @tree: Device node containing all the overlays 378 * 379 * Helper function that given a tree containing overlay information, 380 * allocates and builds an overlay info array containing it, ready 381 * for use using of_overlay_apply. 382 * 383 * Returns 0 on success with the @cntp @ovinfop pointers valid, 384 * while on error a negative error value is returned. 385 */ 386 static int of_build_overlay_info(struct of_overlay *ov, 387 struct device_node *tree) 388 { 389 struct device_node *node; 390 struct of_overlay_info *ovinfo; 391 int cnt, err; 392 393 /* worst case; every child is a node */ 394 cnt = 0; 395 for_each_child_of_node(tree, node) 396 cnt++; 397 398 if (of_get_child_by_name(tree, "__symbols__")) 399 cnt++; 400 401 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL); 402 if (ovinfo == NULL) 403 return -ENOMEM; 404 405 cnt = 0; 406 for_each_child_of_node(tree, node) { 407 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]); 408 if (err == 0) 409 cnt++; 410 } 411 412 node = of_get_child_by_name(tree, "__symbols__"); 413 if (node) { 414 ovinfo[cnt].overlay = node; 415 ovinfo[cnt].target = of_find_node_by_path("/__symbols__"); 416 ovinfo[cnt].is_symbols_node = 1; 417 418 if (!ovinfo[cnt].target) { 419 pr_err("no symbols in root of device tree.\n"); 420 return -EINVAL; 421 } 422 423 cnt++; 424 } 425 426 /* if nothing filled, return error */ 427 if (cnt == 0) { 428 kfree(ovinfo); 429 return -ENODEV; 430 } 431 432 ov->count = cnt; 433 ov->ovinfo_tab = ovinfo; 434 435 return 0; 436 } 437 438 /** 439 * of_free_overlay_info() - Free an overlay info array 440 * @ov Overlay to free the overlay info from 441 * @ovinfo_tab: Array of overlay_info's to free 442 * 443 * Releases the memory of a previously allocated ovinfo array 444 * by of_build_overlay_info. 445 * Returns 0, or an error if the arguments are bogus. 446 */ 447 static int of_free_overlay_info(struct of_overlay *ov) 448 { 449 struct of_overlay_info *ovinfo; 450 int i; 451 452 /* do it in reverse */ 453 for (i = ov->count - 1; i >= 0; i--) { 454 ovinfo = &ov->ovinfo_tab[i]; 455 456 of_node_put(ovinfo->target); 457 of_node_put(ovinfo->overlay); 458 } 459 kfree(ov->ovinfo_tab); 460 461 return 0; 462 } 463 464 static LIST_HEAD(ov_list); 465 static DEFINE_IDR(ov_idr); 466 467 /** 468 * of_overlay_create() - Create and apply an overlay 469 * @tree: Device node containing all the overlays 470 * 471 * Creates and applies an overlay while also keeping track 472 * of the overlay in a list. This list can be used to prevent 473 * illegal overlay removals. 474 * 475 * Returns the id of the created overlay, or a negative error number 476 */ 477 int of_overlay_create(struct device_node *tree) 478 { 479 struct of_overlay *ov; 480 int err, id; 481 482 /* allocate the overlay structure */ 483 ov = kzalloc(sizeof(*ov), GFP_KERNEL); 484 if (ov == NULL) 485 return -ENOMEM; 486 ov->id = -1; 487 488 INIT_LIST_HEAD(&ov->node); 489 490 of_changeset_init(&ov->cset); 491 492 mutex_lock(&of_mutex); 493 494 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL); 495 if (id < 0) { 496 err = id; 497 goto err_destroy_trans; 498 } 499 ov->id = id; 500 501 /* build the overlay info structures */ 502 err = of_build_overlay_info(ov, tree); 503 if (err) { 504 pr_err("of_build_overlay_info() failed for tree@%pOF\n", 505 tree); 506 goto err_free_idr; 507 } 508 509 err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY); 510 if (err < 0) { 511 pr_err("%s: Pre-apply notifier failed (err=%d)\n", 512 __func__, err); 513 goto err_free_idr; 514 } 515 516 /* apply the overlay */ 517 err = of_overlay_apply(ov); 518 if (err) 519 goto err_abort_trans; 520 521 /* apply the changeset */ 522 err = __of_changeset_apply(&ov->cset); 523 if (err) 524 goto err_revert_overlay; 525 526 527 /* add to the tail of the overlay list */ 528 list_add_tail(&ov->node, &ov_list); 529 530 of_overlay_notify(ov, OF_OVERLAY_POST_APPLY); 531 532 mutex_unlock(&of_mutex); 533 534 return id; 535 536 err_revert_overlay: 537 err_abort_trans: 538 of_free_overlay_info(ov); 539 err_free_idr: 540 idr_remove(&ov_idr, ov->id); 541 err_destroy_trans: 542 of_changeset_destroy(&ov->cset); 543 kfree(ov); 544 mutex_unlock(&of_mutex); 545 546 return err; 547 } 548 EXPORT_SYMBOL_GPL(of_overlay_create); 549 550 /* check whether the given node, lies under the given tree */ 551 static int overlay_subtree_check(struct device_node *tree, 552 struct device_node *dn) 553 { 554 struct device_node *child; 555 556 /* match? */ 557 if (tree == dn) 558 return 1; 559 560 for_each_child_of_node(tree, child) { 561 if (overlay_subtree_check(child, dn)) { 562 of_node_put(child); 563 return 1; 564 } 565 } 566 567 return 0; 568 } 569 570 /* check whether this overlay is the topmost */ 571 static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn) 572 { 573 struct of_overlay *ovt; 574 struct of_changeset_entry *ce; 575 576 list_for_each_entry_reverse(ovt, &ov_list, node) { 577 /* if we hit ourselves, we're done */ 578 if (ovt == ov) 579 break; 580 581 /* check against each subtree affected by this overlay */ 582 list_for_each_entry(ce, &ovt->cset.entries, node) { 583 if (overlay_subtree_check(ce->np, dn)) { 584 pr_err("%s: #%d clashes #%d @%pOF\n", 585 __func__, ov->id, ovt->id, dn); 586 return 0; 587 } 588 } 589 } 590 591 /* overlay is topmost */ 592 return 1; 593 } 594 595 /* 596 * We can safely remove the overlay only if it's the top-most one. 597 * Newly applied overlays are inserted at the tail of the overlay list, 598 * so a top most overlay is the one that is closest to the tail. 599 * 600 * The topmost check is done by exploiting this property. For each 601 * affected device node in the log list we check if this overlay is 602 * the one closest to the tail. If another overlay has affected this 603 * device node and is closest to the tail, then removal is not permited. 604 */ 605 static int overlay_removal_is_ok(struct of_overlay *ov) 606 { 607 struct of_changeset_entry *ce; 608 609 list_for_each_entry(ce, &ov->cset.entries, node) { 610 if (!overlay_is_topmost(ov, ce->np)) { 611 pr_err("overlay #%d is not topmost\n", ov->id); 612 return 0; 613 } 614 } 615 616 return 1; 617 } 618 619 /** 620 * of_overlay_destroy() - Removes an overlay 621 * @id: Overlay id number returned by a previous call to of_overlay_create 622 * 623 * Removes an overlay if it is permissible. 624 * 625 * Returns 0 on success, or a negative error number 626 */ 627 int of_overlay_destroy(int id) 628 { 629 struct of_overlay *ov; 630 int err; 631 632 mutex_lock(&of_mutex); 633 634 ov = idr_find(&ov_idr, id); 635 if (ov == NULL) { 636 err = -ENODEV; 637 pr_err("destroy: Could not find overlay #%d\n", id); 638 goto out; 639 } 640 641 /* check whether the overlay is safe to remove */ 642 if (!overlay_removal_is_ok(ov)) { 643 err = -EBUSY; 644 goto out; 645 } 646 647 of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE); 648 list_del(&ov->node); 649 __of_changeset_revert(&ov->cset); 650 of_overlay_notify(ov, OF_OVERLAY_POST_REMOVE); 651 of_free_overlay_info(ov); 652 idr_remove(&ov_idr, id); 653 of_changeset_destroy(&ov->cset); 654 kfree(ov); 655 656 err = 0; 657 658 out: 659 mutex_unlock(&of_mutex); 660 661 return err; 662 } 663 EXPORT_SYMBOL_GPL(of_overlay_destroy); 664 665 /** 666 * of_overlay_destroy_all() - Removes all overlays from the system 667 * 668 * Removes all overlays from the system in the correct order. 669 * 670 * Returns 0 on success, or a negative error number 671 */ 672 int of_overlay_destroy_all(void) 673 { 674 struct of_overlay *ov, *ovn; 675 676 mutex_lock(&of_mutex); 677 678 /* the tail of list is guaranteed to be safe to remove */ 679 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) { 680 list_del(&ov->node); 681 __of_changeset_revert(&ov->cset); 682 of_free_overlay_info(ov); 683 idr_remove(&ov_idr, ov->id); 684 kfree(ov); 685 } 686 687 mutex_unlock(&of_mutex); 688 689 return 0; 690 } 691 EXPORT_SYMBOL_GPL(of_overlay_destroy_all); 692