overlay.c 16 KB

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