overlay.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/idr.h>
  23. #include "of_private.h"
  24. /**
  25. * struct of_overlay_info - Holds a single overlay info
  26. * @target: target of the overlay operation
  27. * @overlay: pointer to the overlay contents node
  28. *
  29. * Holds a single overlay state, including all the overlay logs &
  30. * records.
  31. */
  32. struct of_overlay_info {
  33. struct device_node *target;
  34. struct device_node *overlay;
  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. static int of_overlay_apply_single_property(struct of_overlay *ov,
  55. struct device_node *target, struct property *prop)
  56. {
  57. struct property *propn, *tprop;
  58. /* NOTE: Multiple changes of single properties not supported */
  59. tprop = of_find_property(target, prop->name, NULL);
  60. /* special properties are not meant to be updated (silent NOP) */
  61. if (of_prop_cmp(prop->name, "name") == 0 ||
  62. of_prop_cmp(prop->name, "phandle") == 0 ||
  63. of_prop_cmp(prop->name, "linux,phandle") == 0)
  64. return 0;
  65. propn = __of_prop_dup(prop, GFP_KERNEL);
  66. if (propn == NULL)
  67. return -ENOMEM;
  68. /* not found? add */
  69. if (tprop == NULL)
  70. return of_changeset_add_property(&ov->cset, target, propn);
  71. /* found? update */
  72. return of_changeset_update_property(&ov->cset, target, propn);
  73. }
  74. static int of_overlay_apply_single_device_node(struct of_overlay *ov,
  75. struct device_node *target, struct device_node *child)
  76. {
  77. const char *cname;
  78. struct device_node *tchild;
  79. int ret = 0;
  80. cname = kbasename(child->full_name);
  81. if (cname == NULL)
  82. return -ENOMEM;
  83. /* NOTE: Multiple mods of created nodes not supported */
  84. tchild = of_get_child_by_name(target, cname);
  85. if (tchild != NULL) {
  86. /* apply overlay recursively */
  87. ret = of_overlay_apply_one(ov, tchild, child);
  88. of_node_put(tchild);
  89. } else {
  90. /* create empty tree as a target */
  91. tchild = __of_node_dup(child, "%s/%s", target->full_name, cname);
  92. if (!tchild)
  93. return -ENOMEM;
  94. /* point to parent */
  95. tchild->parent = target;
  96. ret = of_changeset_attach_node(&ov->cset, tchild);
  97. if (ret)
  98. return ret;
  99. ret = of_overlay_apply_one(ov, tchild, child);
  100. if (ret)
  101. return ret;
  102. }
  103. return ret;
  104. }
  105. /*
  106. * Apply a single overlay node recursively.
  107. *
  108. * Note that the in case of an error the target node is left
  109. * in a inconsistent state. Error recovery should be performed
  110. * by using the changeset.
  111. */
  112. static int of_overlay_apply_one(struct of_overlay *ov,
  113. struct device_node *target, const struct device_node *overlay)
  114. {
  115. struct device_node *child;
  116. struct property *prop;
  117. int ret;
  118. for_each_property_of_node(overlay, prop) {
  119. ret = of_overlay_apply_single_property(ov, target, prop);
  120. if (ret) {
  121. pr_err("Failed to apply prop @%s/%s\n",
  122. target->full_name, prop->name);
  123. return ret;
  124. }
  125. }
  126. for_each_child_of_node(overlay, child) {
  127. ret = of_overlay_apply_single_device_node(ov, target, child);
  128. if (ret != 0) {
  129. pr_err("Failed to apply single node @%s/%s\n",
  130. target->full_name, child->name);
  131. of_node_put(child);
  132. return ret;
  133. }
  134. }
  135. return 0;
  136. }
  137. /**
  138. * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
  139. * @ov: Overlay to apply
  140. *
  141. * Applies the overlays given, while handling all error conditions
  142. * appropriately. Either the operation succeeds, or if it fails the
  143. * live tree is reverted to the state before the attempt.
  144. * Returns 0, or an error if the overlay attempt failed.
  145. */
  146. static int of_overlay_apply(struct of_overlay *ov)
  147. {
  148. int i, err;
  149. /* first we apply the overlays atomically */
  150. for (i = 0; i < ov->count; i++) {
  151. struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
  152. err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
  153. if (err != 0) {
  154. pr_err("apply failed '%s'\n", ovinfo->target->full_name);
  155. return err;
  156. }
  157. }
  158. return 0;
  159. }
  160. /*
  161. * Find the target node using a number of different strategies
  162. * in order of preference
  163. *
  164. * "target" property containing the phandle of the target
  165. * "target-path" property containing the path of the target
  166. */
  167. static struct device_node *find_target_node(struct device_node *info_node)
  168. {
  169. const char *path;
  170. u32 val;
  171. int ret;
  172. /* first try to go by using the target as a phandle */
  173. ret = of_property_read_u32(info_node, "target", &val);
  174. if (ret == 0)
  175. return of_find_node_by_phandle(val);
  176. /* now try to locate by path */
  177. ret = of_property_read_string(info_node, "target-path", &path);
  178. if (ret == 0)
  179. return of_find_node_by_path(path);
  180. pr_err("Failed to find target for node %p (%s)\n",
  181. info_node, info_node->name);
  182. return NULL;
  183. }
  184. /**
  185. * of_fill_overlay_info() - Fill an overlay info structure
  186. * @ov Overlay to fill
  187. * @info_node: Device node containing the overlay
  188. * @ovinfo: Pointer to the overlay info structure to fill
  189. *
  190. * Fills an overlay info structure with the overlay information
  191. * from a device node. This device node must have a target property
  192. * which contains a phandle of the overlay target node, and an
  193. * __overlay__ child node which has the overlay contents.
  194. * Both ovinfo->target & ovinfo->overlay have their references taken.
  195. *
  196. * Returns 0 on success, or a negative error value.
  197. */
  198. static int of_fill_overlay_info(struct of_overlay *ov,
  199. struct device_node *info_node, struct of_overlay_info *ovinfo)
  200. {
  201. ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
  202. if (ovinfo->overlay == NULL)
  203. goto err_fail;
  204. ovinfo->target = find_target_node(info_node);
  205. if (ovinfo->target == NULL)
  206. goto err_fail;
  207. return 0;
  208. err_fail:
  209. of_node_put(ovinfo->target);
  210. of_node_put(ovinfo->overlay);
  211. memset(ovinfo, 0, sizeof(*ovinfo));
  212. return -EINVAL;
  213. }
  214. /**
  215. * of_build_overlay_info() - Build an overlay info array
  216. * @ov Overlay to build
  217. * @tree: Device node containing all the overlays
  218. *
  219. * Helper function that given a tree containing overlay information,
  220. * allocates and builds an overlay info array containing it, ready
  221. * for use using of_overlay_apply.
  222. *
  223. * Returns 0 on success with the @cntp @ovinfop pointers valid,
  224. * while on error a negative error value is returned.
  225. */
  226. static int of_build_overlay_info(struct of_overlay *ov,
  227. struct device_node *tree)
  228. {
  229. struct device_node *node;
  230. struct of_overlay_info *ovinfo;
  231. int cnt, err;
  232. /* worst case; every child is a node */
  233. cnt = 0;
  234. for_each_child_of_node(tree, node)
  235. cnt++;
  236. ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
  237. if (ovinfo == NULL)
  238. return -ENOMEM;
  239. cnt = 0;
  240. for_each_child_of_node(tree, node) {
  241. memset(&ovinfo[cnt], 0, sizeof(*ovinfo));
  242. err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
  243. if (err == 0)
  244. cnt++;
  245. }
  246. /* if nothing filled, return error */
  247. if (cnt == 0) {
  248. kfree(ovinfo);
  249. return -ENODEV;
  250. }
  251. ov->count = cnt;
  252. ov->ovinfo_tab = ovinfo;
  253. return 0;
  254. }
  255. /**
  256. * of_free_overlay_info() - Free an overlay info array
  257. * @ov Overlay to free the overlay info from
  258. * @ovinfo_tab: Array of overlay_info's to free
  259. *
  260. * Releases the memory of a previously allocated ovinfo array
  261. * by of_build_overlay_info.
  262. * Returns 0, or an error if the arguments are bogus.
  263. */
  264. static int of_free_overlay_info(struct of_overlay *ov)
  265. {
  266. struct of_overlay_info *ovinfo;
  267. int i;
  268. /* do it in reverse */
  269. for (i = ov->count - 1; i >= 0; i--) {
  270. ovinfo = &ov->ovinfo_tab[i];
  271. of_node_put(ovinfo->target);
  272. of_node_put(ovinfo->overlay);
  273. }
  274. kfree(ov->ovinfo_tab);
  275. return 0;
  276. }
  277. static LIST_HEAD(ov_list);
  278. static DEFINE_IDR(ov_idr);
  279. /**
  280. * of_overlay_create() - Create and apply an overlay
  281. * @tree: Device node containing all the overlays
  282. *
  283. * Creates and applies an overlay while also keeping track
  284. * of the overlay in a list. This list can be used to prevent
  285. * illegal overlay removals.
  286. *
  287. * Returns the id of the created overlay, or a negative error number
  288. */
  289. int of_overlay_create(struct device_node *tree)
  290. {
  291. struct of_overlay *ov;
  292. int err, id;
  293. /* allocate the overlay structure */
  294. ov = kzalloc(sizeof(*ov), GFP_KERNEL);
  295. if (ov == NULL)
  296. return -ENOMEM;
  297. ov->id = -1;
  298. INIT_LIST_HEAD(&ov->node);
  299. of_changeset_init(&ov->cset);
  300. mutex_lock(&of_mutex);
  301. id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
  302. if (id < 0) {
  303. err = id;
  304. goto err_destroy_trans;
  305. }
  306. ov->id = id;
  307. /* build the overlay info structures */
  308. err = of_build_overlay_info(ov, tree);
  309. if (err) {
  310. pr_err("of_build_overlay_info() failed for tree@%s\n",
  311. tree->full_name);
  312. goto err_free_idr;
  313. }
  314. /* apply the overlay */
  315. err = of_overlay_apply(ov);
  316. if (err)
  317. goto err_abort_trans;
  318. /* apply the changeset */
  319. err = __of_changeset_apply(&ov->cset);
  320. if (err)
  321. goto err_revert_overlay;
  322. /* add to the tail of the overlay list */
  323. list_add_tail(&ov->node, &ov_list);
  324. mutex_unlock(&of_mutex);
  325. return id;
  326. err_revert_overlay:
  327. err_abort_trans:
  328. of_free_overlay_info(ov);
  329. err_free_idr:
  330. idr_remove(&ov_idr, ov->id);
  331. err_destroy_trans:
  332. of_changeset_destroy(&ov->cset);
  333. kfree(ov);
  334. mutex_unlock(&of_mutex);
  335. return err;
  336. }
  337. EXPORT_SYMBOL_GPL(of_overlay_create);
  338. /* check whether the given node, lies under the given tree */
  339. static int overlay_subtree_check(struct device_node *tree,
  340. struct device_node *dn)
  341. {
  342. struct device_node *child;
  343. /* match? */
  344. if (tree == dn)
  345. return 1;
  346. for_each_child_of_node(tree, child) {
  347. if (overlay_subtree_check(child, dn)) {
  348. of_node_put(child);
  349. return 1;
  350. }
  351. }
  352. return 0;
  353. }
  354. /* check whether this overlay is the topmost */
  355. static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
  356. {
  357. struct of_overlay *ovt;
  358. struct of_changeset_entry *ce;
  359. list_for_each_entry_reverse(ovt, &ov_list, node) {
  360. /* if we hit ourselves, we're done */
  361. if (ovt == ov)
  362. break;
  363. /* check against each subtree affected by this overlay */
  364. list_for_each_entry(ce, &ovt->cset.entries, node) {
  365. if (overlay_subtree_check(ce->np, dn)) {
  366. pr_err("%s: #%d clashes #%d @%s\n",
  367. __func__, ov->id, ovt->id,
  368. dn->full_name);
  369. return 0;
  370. }
  371. }
  372. }
  373. /* overlay is topmost */
  374. return 1;
  375. }
  376. /*
  377. * We can safely remove the overlay only if it's the top-most one.
  378. * Newly applied overlays are inserted at the tail of the overlay list,
  379. * so a top most overlay is the one that is closest to the tail.
  380. *
  381. * The topmost check is done by exploiting this property. For each
  382. * affected device node in the log list we check if this overlay is
  383. * the one closest to the tail. If another overlay has affected this
  384. * device node and is closest to the tail, then removal is not permited.
  385. */
  386. static int overlay_removal_is_ok(struct of_overlay *ov)
  387. {
  388. struct of_changeset_entry *ce;
  389. list_for_each_entry(ce, &ov->cset.entries, node) {
  390. if (!overlay_is_topmost(ov, ce->np)) {
  391. pr_err("overlay #%d is not topmost\n", ov->id);
  392. return 0;
  393. }
  394. }
  395. return 1;
  396. }
  397. /**
  398. * of_overlay_destroy() - Removes an overlay
  399. * @id: Overlay id number returned by a previous call to of_overlay_create
  400. *
  401. * Removes an overlay if it is permissible.
  402. *
  403. * Returns 0 on success, or a negative error number
  404. */
  405. int of_overlay_destroy(int id)
  406. {
  407. struct of_overlay *ov;
  408. int err;
  409. mutex_lock(&of_mutex);
  410. ov = idr_find(&ov_idr, id);
  411. if (ov == NULL) {
  412. err = -ENODEV;
  413. pr_err("destroy: Could not find overlay #%d\n", id);
  414. goto out;
  415. }
  416. /* check whether the overlay is safe to remove */
  417. if (!overlay_removal_is_ok(ov)) {
  418. err = -EBUSY;
  419. goto out;
  420. }
  421. list_del(&ov->node);
  422. __of_changeset_revert(&ov->cset);
  423. of_free_overlay_info(ov);
  424. idr_remove(&ov_idr, id);
  425. of_changeset_destroy(&ov->cset);
  426. kfree(ov);
  427. err = 0;
  428. out:
  429. mutex_unlock(&of_mutex);
  430. return err;
  431. }
  432. EXPORT_SYMBOL_GPL(of_overlay_destroy);
  433. /**
  434. * of_overlay_destroy_all() - Removes all overlays from the system
  435. *
  436. * Removes all overlays from the system in the correct order.
  437. *
  438. * Returns 0 on success, or a negative error number
  439. */
  440. int of_overlay_destroy_all(void)
  441. {
  442. struct of_overlay *ov, *ovn;
  443. mutex_lock(&of_mutex);
  444. /* the tail of list is guaranteed to be safe to remove */
  445. list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
  446. list_del(&ov->node);
  447. __of_changeset_revert(&ov->cset);
  448. of_free_overlay_info(ov);
  449. idr_remove(&ov_idr, ov->id);
  450. kfree(ov);
  451. }
  452. mutex_unlock(&of_mutex);
  453. return 0;
  454. }
  455. EXPORT_SYMBOL_GPL(of_overlay_destroy_all);