overlay.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #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. #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("%s: Failed to apply prop @%s/%s\n",
  122. __func__, 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("%s: Failed to apply single node @%s/%s\n",
  130. __func__, target->full_name,
  131. child->name);
  132. of_node_put(child);
  133. return ret;
  134. }
  135. }
  136. return 0;
  137. }
  138. /**
  139. * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
  140. * @ov: Overlay to apply
  141. *
  142. * Applies the overlays given, while handling all error conditions
  143. * appropriately. Either the operation succeeds, or if it fails the
  144. * live tree is reverted to the state before the attempt.
  145. * Returns 0, or an error if the overlay attempt failed.
  146. */
  147. static int of_overlay_apply(struct of_overlay *ov)
  148. {
  149. int i, err;
  150. /* first we apply the overlays atomically */
  151. for (i = 0; i < ov->count; i++) {
  152. struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
  153. err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
  154. if (err != 0) {
  155. pr_err("%s: overlay failed '%s'\n",
  156. __func__, ovinfo->target->full_name);
  157. return err;
  158. }
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Find the target node using a number of different strategies
  164. * in order of preference
  165. *
  166. * "target" property containing the phandle of the target
  167. * "target-path" property containing the path of the target
  168. */
  169. static struct device_node *find_target_node(struct device_node *info_node)
  170. {
  171. const char *path;
  172. u32 val;
  173. int ret;
  174. /* first try to go by using the target as a phandle */
  175. ret = of_property_read_u32(info_node, "target", &val);
  176. if (ret == 0)
  177. return of_find_node_by_phandle(val);
  178. /* now try to locate by path */
  179. ret = of_property_read_string(info_node, "target-path", &path);
  180. if (ret == 0)
  181. return of_find_node_by_path(path);
  182. pr_err("%s: Failed to find target for node %p (%s)\n", __func__,
  183. info_node, info_node->name);
  184. return NULL;
  185. }
  186. /**
  187. * of_fill_overlay_info() - Fill an overlay info structure
  188. * @ov Overlay to fill
  189. * @info_node: Device node containing the overlay
  190. * @ovinfo: Pointer to the overlay info structure to fill
  191. *
  192. * Fills an overlay info structure with the overlay information
  193. * from a device node. This device node must have a target property
  194. * which contains a phandle of the overlay target node, and an
  195. * __overlay__ child node which has the overlay contents.
  196. * Both ovinfo->target & ovinfo->overlay have their references taken.
  197. *
  198. * Returns 0 on success, or a negative error value.
  199. */
  200. static int of_fill_overlay_info(struct of_overlay *ov,
  201. struct device_node *info_node, struct of_overlay_info *ovinfo)
  202. {
  203. ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
  204. if (ovinfo->overlay == NULL)
  205. goto err_fail;
  206. ovinfo->target = find_target_node(info_node);
  207. if (ovinfo->target == NULL)
  208. goto err_fail;
  209. return 0;
  210. err_fail:
  211. of_node_put(ovinfo->target);
  212. of_node_put(ovinfo->overlay);
  213. memset(ovinfo, 0, sizeof(*ovinfo));
  214. return -EINVAL;
  215. }
  216. /**
  217. * of_build_overlay_info() - Build an overlay info array
  218. * @ov Overlay to build
  219. * @tree: Device node containing all the overlays
  220. *
  221. * Helper function that given a tree containing overlay information,
  222. * allocates and builds an overlay info array containing it, ready
  223. * for use using of_overlay_apply.
  224. *
  225. * Returns 0 on success with the @cntp @ovinfop pointers valid,
  226. * while on error a negative error value is returned.
  227. */
  228. static int of_build_overlay_info(struct of_overlay *ov,
  229. struct device_node *tree)
  230. {
  231. struct device_node *node;
  232. struct of_overlay_info *ovinfo;
  233. int cnt, err;
  234. /* worst case; every child is a node */
  235. cnt = 0;
  236. for_each_child_of_node(tree, node)
  237. cnt++;
  238. ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
  239. if (ovinfo == NULL)
  240. return -ENOMEM;
  241. cnt = 0;
  242. for_each_child_of_node(tree, node) {
  243. memset(&ovinfo[cnt], 0, sizeof(*ovinfo));
  244. err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
  245. if (err == 0)
  246. cnt++;
  247. }
  248. /* if nothing filled, return error */
  249. if (cnt == 0) {
  250. kfree(ovinfo);
  251. return -ENODEV;
  252. }
  253. ov->count = cnt;
  254. ov->ovinfo_tab = ovinfo;
  255. return 0;
  256. }
  257. /**
  258. * of_free_overlay_info() - Free an overlay info array
  259. * @ov Overlay to free the overlay info from
  260. * @ovinfo_tab: Array of overlay_info's to free
  261. *
  262. * Releases the memory of a previously allocated ovinfo array
  263. * by of_build_overlay_info.
  264. * Returns 0, or an error if the arguments are bogus.
  265. */
  266. static int of_free_overlay_info(struct of_overlay *ov)
  267. {
  268. struct of_overlay_info *ovinfo;
  269. int i;
  270. /* do it in reverse */
  271. for (i = ov->count - 1; i >= 0; i--) {
  272. ovinfo = &ov->ovinfo_tab[i];
  273. of_node_put(ovinfo->target);
  274. of_node_put(ovinfo->overlay);
  275. }
  276. kfree(ov->ovinfo_tab);
  277. return 0;
  278. }
  279. static LIST_HEAD(ov_list);
  280. static DEFINE_IDR(ov_idr);
  281. /**
  282. * of_overlay_create() - Create and apply an overlay
  283. * @tree: Device node containing all the overlays
  284. *
  285. * Creates and applies an overlay while also keeping track
  286. * of the overlay in a list. This list can be used to prevent
  287. * illegal overlay removals.
  288. *
  289. * Returns the id of the created overlay, or a negative error number
  290. */
  291. int of_overlay_create(struct device_node *tree)
  292. {
  293. struct of_overlay *ov;
  294. int err, id;
  295. /* allocate the overlay structure */
  296. ov = kzalloc(sizeof(*ov), GFP_KERNEL);
  297. if (ov == NULL)
  298. return -ENOMEM;
  299. ov->id = -1;
  300. INIT_LIST_HEAD(&ov->node);
  301. of_changeset_init(&ov->cset);
  302. mutex_lock(&of_mutex);
  303. id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
  304. if (id < 0) {
  305. pr_err("%s: idr_alloc() failed for tree@%s\n",
  306. __func__, tree->full_name);
  307. err = id;
  308. goto err_destroy_trans;
  309. }
  310. ov->id = id;
  311. /* build the overlay info structures */
  312. err = of_build_overlay_info(ov, tree);
  313. if (err) {
  314. pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
  315. __func__, tree->full_name);
  316. goto err_free_idr;
  317. }
  318. /* apply the overlay */
  319. err = of_overlay_apply(ov);
  320. if (err) {
  321. pr_err("%s: of_overlay_apply() failed for tree@%s\n",
  322. __func__, tree->full_name);
  323. goto err_abort_trans;
  324. }
  325. /* apply the changeset */
  326. err = of_changeset_apply(&ov->cset);
  327. if (err) {
  328. pr_err("%s: of_changeset_apply() failed for tree@%s\n",
  329. __func__, tree->full_name);
  330. goto err_revert_overlay;
  331. }
  332. /* add to the tail of the overlay list */
  333. list_add_tail(&ov->node, &ov_list);
  334. mutex_unlock(&of_mutex);
  335. return id;
  336. err_revert_overlay:
  337. err_abort_trans:
  338. of_free_overlay_info(ov);
  339. err_free_idr:
  340. idr_remove(&ov_idr, ov->id);
  341. err_destroy_trans:
  342. of_changeset_destroy(&ov->cset);
  343. kfree(ov);
  344. mutex_unlock(&of_mutex);
  345. return err;
  346. }
  347. EXPORT_SYMBOL_GPL(of_overlay_create);
  348. /* check whether the given node, lies under the given tree */
  349. static int overlay_subtree_check(struct device_node *tree,
  350. struct device_node *dn)
  351. {
  352. struct device_node *child;
  353. /* match? */
  354. if (tree == dn)
  355. return 1;
  356. for_each_child_of_node(tree, child) {
  357. if (overlay_subtree_check(child, dn)) {
  358. of_node_put(child);
  359. return 1;
  360. }
  361. }
  362. return 0;
  363. }
  364. /* check whether this overlay is the topmost */
  365. static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
  366. {
  367. struct of_overlay *ovt;
  368. struct of_changeset_entry *ce;
  369. list_for_each_entry_reverse(ovt, &ov_list, node) {
  370. /* if we hit ourselves, we're done */
  371. if (ovt == ov)
  372. break;
  373. /* check against each subtree affected by this overlay */
  374. list_for_each_entry(ce, &ovt->cset.entries, node) {
  375. if (overlay_subtree_check(ce->np, dn)) {
  376. pr_err("%s: #%d clashes #%d @%s\n",
  377. __func__, ov->id, ovt->id,
  378. dn->full_name);
  379. return 0;
  380. }
  381. }
  382. }
  383. /* overlay is topmost */
  384. return 1;
  385. }
  386. /*
  387. * We can safely remove the overlay only if it's the top-most one.
  388. * Newly applied overlays are inserted at the tail of the overlay list,
  389. * so a top most overlay is the one that is closest to the tail.
  390. *
  391. * The topmost check is done by exploiting this property. For each
  392. * affected device node in the log list we check if this overlay is
  393. * the one closest to the tail. If another overlay has affected this
  394. * device node and is closest to the tail, then removal is not permited.
  395. */
  396. static int overlay_removal_is_ok(struct of_overlay *ov)
  397. {
  398. struct of_changeset_entry *ce;
  399. list_for_each_entry(ce, &ov->cset.entries, node) {
  400. if (!overlay_is_topmost(ov, ce->np)) {
  401. pr_err("%s: overlay #%d is not topmost\n",
  402. __func__, ov->id);
  403. return 0;
  404. }
  405. }
  406. return 1;
  407. }
  408. /**
  409. * of_overlay_destroy() - Removes an overlay
  410. * @id: Overlay id number returned by a previous call to of_overlay_create
  411. *
  412. * Removes an overlay if it is permissible.
  413. *
  414. * Returns 0 on success, or a negative error number
  415. */
  416. int of_overlay_destroy(int id)
  417. {
  418. struct of_overlay *ov;
  419. int err;
  420. mutex_lock(&of_mutex);
  421. ov = idr_find(&ov_idr, id);
  422. if (ov == NULL) {
  423. err = -ENODEV;
  424. pr_err("%s: Could not find overlay #%d\n",
  425. __func__, id);
  426. goto out;
  427. }
  428. /* check whether the overlay is safe to remove */
  429. if (!overlay_removal_is_ok(ov)) {
  430. err = -EBUSY;
  431. pr_err("%s: removal check failed for overlay #%d\n",
  432. __func__, id);
  433. goto out;
  434. }
  435. list_del(&ov->node);
  436. of_changeset_revert(&ov->cset);
  437. of_free_overlay_info(ov);
  438. idr_remove(&ov_idr, id);
  439. of_changeset_destroy(&ov->cset);
  440. kfree(ov);
  441. err = 0;
  442. out:
  443. mutex_unlock(&of_mutex);
  444. return err;
  445. }
  446. EXPORT_SYMBOL_GPL(of_overlay_destroy);
  447. /**
  448. * of_overlay_destroy_all() - Removes all overlays from the system
  449. *
  450. * Removes all overlays from the system in the correct order.
  451. *
  452. * Returns 0 on success, or a negative error number
  453. */
  454. int of_overlay_destroy_all(void)
  455. {
  456. struct of_overlay *ov, *ovn;
  457. mutex_lock(&of_mutex);
  458. /* the tail of list is guaranteed to be safe to remove */
  459. list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
  460. list_del(&ov->node);
  461. of_changeset_revert(&ov->cset);
  462. of_free_overlay_info(ov);
  463. idr_remove(&ov_idr, ov->id);
  464. kfree(ov);
  465. }
  466. mutex_unlock(&of_mutex);
  467. return 0;
  468. }
  469. EXPORT_SYMBOL_GPL(of_overlay_destroy_all);