overlay.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 "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. };
  35. /**
  36. * struct of_overlay - Holds a complete overlay transaction
  37. * @node: List on which we are located
  38. * @count: Count of ovinfo structures
  39. * @ovinfo_tab: Overlay info table (count sized)
  40. * @cset: Changeset to be used
  41. *
  42. * Holds a complete overlay transaction
  43. */
  44. struct of_overlay {
  45. int id;
  46. struct list_head node;
  47. int count;
  48. struct of_overlay_info *ovinfo_tab;
  49. struct of_changeset cset;
  50. };
  51. static int of_overlay_apply_one(struct of_overlay *ov,
  52. struct device_node *target, const struct device_node *overlay);
  53. static int of_overlay_apply_single_property(struct of_overlay *ov,
  54. struct device_node *target, struct property *prop)
  55. {
  56. struct property *propn, *tprop;
  57. /* NOTE: Multiple changes of single properties not supported */
  58. tprop = of_find_property(target, prop->name, NULL);
  59. /* special properties are not meant to be updated (silent NOP) */
  60. if (of_prop_cmp(prop->name, "name") == 0 ||
  61. of_prop_cmp(prop->name, "phandle") == 0 ||
  62. of_prop_cmp(prop->name, "linux,phandle") == 0)
  63. return 0;
  64. propn = __of_prop_dup(prop, GFP_KERNEL);
  65. if (propn == NULL)
  66. return -ENOMEM;
  67. /* not found? add */
  68. if (tprop == NULL)
  69. return of_changeset_add_property(&ov->cset, target, propn);
  70. /* found? update */
  71. return of_changeset_update_property(&ov->cset, target, propn);
  72. }
  73. static int of_overlay_apply_single_device_node(struct of_overlay *ov,
  74. struct device_node *target, struct device_node *child)
  75. {
  76. const char *cname;
  77. struct device_node *tchild, *grandchild;
  78. int ret = 0;
  79. cname = kbasename(child->full_name);
  80. if (cname == NULL)
  81. return -ENOMEM;
  82. /* NOTE: Multiple mods of created nodes not supported */
  83. tchild = of_get_child_by_name(target, cname);
  84. if (tchild != NULL) {
  85. /* apply overlay recursively */
  86. ret = of_overlay_apply_one(ov, tchild, child);
  87. of_node_put(tchild);
  88. } else {
  89. /* create empty tree as a target */
  90. tchild = __of_node_dup(child, "%s/%s", target->full_name, cname);
  91. if (!tchild)
  92. return -ENOMEM;
  93. /* point to parent */
  94. tchild->parent = target;
  95. ret = of_changeset_attach_node(&ov->cset, tchild);
  96. if (ret)
  97. return ret;
  98. ret = of_overlay_apply_one(ov, tchild, child);
  99. if (ret)
  100. return ret;
  101. }
  102. return ret;
  103. }
  104. /*
  105. * Apply a single overlay node recursively.
  106. *
  107. * Note that the in case of an error the target node is left
  108. * in a inconsistent state. Error recovery should be performed
  109. * by using the changeset.
  110. */
  111. static int of_overlay_apply_one(struct of_overlay *ov,
  112. struct device_node *target, const struct device_node *overlay)
  113. {
  114. struct device_node *child;
  115. struct property *prop;
  116. int ret;
  117. for_each_property_of_node(overlay, prop) {
  118. ret = of_overlay_apply_single_property(ov, target, prop);
  119. if (ret) {
  120. pr_err("%s: Failed to apply prop @%s/%s\n",
  121. __func__, target->full_name, prop->name);
  122. return ret;
  123. }
  124. }
  125. for_each_child_of_node(overlay, child) {
  126. ret = of_overlay_apply_single_device_node(ov, target, child);
  127. if (ret != 0) {
  128. pr_err("%s: Failed to apply single node @%s/%s\n",
  129. __func__, target->full_name,
  130. child->name);
  131. return ret;
  132. }
  133. }
  134. return 0;
  135. }
  136. /**
  137. * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
  138. * @ov: Overlay to apply
  139. *
  140. * Applies the overlays given, while handling all error conditions
  141. * appropriately. Either the operation succeeds, or if it fails the
  142. * live tree is reverted to the state before the attempt.
  143. * Returns 0, or an error if the overlay attempt failed.
  144. */
  145. static int of_overlay_apply(struct of_overlay *ov)
  146. {
  147. int i, err;
  148. /* first we apply the overlays atomically */
  149. for (i = 0; i < ov->count; i++) {
  150. struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
  151. err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
  152. if (err != 0) {
  153. pr_err("%s: overlay failed '%s'\n",
  154. __func__, 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("%s: Failed to find target for node %p (%s)\n", __func__,
  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 an 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. pr_err("%s: idr_alloc() failed for tree@%s\n",
  304. __func__, tree->full_name);
  305. err = id;
  306. goto err_destroy_trans;
  307. }
  308. ov->id = id;
  309. /* build the overlay info structures */
  310. err = of_build_overlay_info(ov, tree);
  311. if (err) {
  312. pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
  313. __func__, tree->full_name);
  314. goto err_free_idr;
  315. }
  316. /* apply the overlay */
  317. err = of_overlay_apply(ov);
  318. if (err) {
  319. pr_err("%s: of_overlay_apply() failed for tree@%s\n",
  320. __func__, tree->full_name);
  321. goto err_abort_trans;
  322. }
  323. /* apply the changeset */
  324. err = of_changeset_apply(&ov->cset);
  325. if (err) {
  326. pr_err("%s: of_changeset_apply() failed for tree@%s\n",
  327. __func__, tree->full_name);
  328. goto err_revert_overlay;
  329. }
  330. /* add to the tail of the overlay list */
  331. list_add_tail(&ov->node, &ov_list);
  332. mutex_unlock(&of_mutex);
  333. return id;
  334. err_revert_overlay:
  335. err_abort_trans:
  336. of_free_overlay_info(ov);
  337. err_free_idr:
  338. idr_remove(&ov_idr, ov->id);
  339. err_destroy_trans:
  340. of_changeset_destroy(&ov->cset);
  341. kfree(ov);
  342. mutex_unlock(&of_mutex);
  343. return err;
  344. }
  345. EXPORT_SYMBOL_GPL(of_overlay_create);
  346. /* check whether the given node, lies under the given tree */
  347. static int overlay_subtree_check(struct device_node *tree,
  348. struct device_node *dn)
  349. {
  350. struct device_node *child;
  351. /* match? */
  352. if (tree == dn)
  353. return 1;
  354. for_each_child_of_node(tree, child) {
  355. if (overlay_subtree_check(child, dn))
  356. return 1;
  357. }
  358. return 0;
  359. }
  360. /* check whether this overlay is the topmost */
  361. static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
  362. {
  363. struct of_overlay *ovt;
  364. struct of_changeset_entry *ce;
  365. list_for_each_entry_reverse(ovt, &ov_list, node) {
  366. /* if we hit ourselves, we're done */
  367. if (ovt == ov)
  368. break;
  369. /* check against each subtree affected by this overlay */
  370. list_for_each_entry(ce, &ovt->cset.entries, node) {
  371. if (overlay_subtree_check(ce->np, dn)) {
  372. pr_err("%s: #%d clashes #%d @%s\n",
  373. __func__, ov->id, ovt->id,
  374. dn->full_name);
  375. return 0;
  376. }
  377. }
  378. }
  379. /* overlay is topmost */
  380. return 1;
  381. }
  382. /*
  383. * We can safely remove the overlay only if it's the top-most one.
  384. * Newly applied overlays are inserted at the tail of the overlay list,
  385. * so a top most overlay is the one that is closest to the tail.
  386. *
  387. * The topmost check is done by exploiting this property. For each
  388. * affected device node in the log list we check if this overlay is
  389. * the one closest to the tail. If another overlay has affected this
  390. * device node and is closest to the tail, then removal is not permited.
  391. */
  392. static int overlay_removal_is_ok(struct of_overlay *ov)
  393. {
  394. struct of_changeset_entry *ce;
  395. list_for_each_entry(ce, &ov->cset.entries, node) {
  396. if (!overlay_is_topmost(ov, ce->np)) {
  397. pr_err("%s: overlay #%d is not topmost\n",
  398. __func__, ov->id);
  399. return 0;
  400. }
  401. }
  402. return 1;
  403. }
  404. /**
  405. * of_overlay_destroy() - Removes an overlay
  406. * @id: Overlay id number returned by a previous call to of_overlay_create
  407. *
  408. * Removes an overlay if it is permissible.
  409. *
  410. * Returns 0 on success, or an negative error number
  411. */
  412. int of_overlay_destroy(int id)
  413. {
  414. struct of_overlay *ov;
  415. int err;
  416. mutex_lock(&of_mutex);
  417. ov = idr_find(&ov_idr, id);
  418. if (ov == NULL) {
  419. err = -ENODEV;
  420. pr_err("%s: Could not find overlay #%d\n",
  421. __func__, id);
  422. goto out;
  423. }
  424. /* check whether the overlay is safe to remove */
  425. if (!overlay_removal_is_ok(ov)) {
  426. err = -EBUSY;
  427. pr_err("%s: removal check failed for overlay #%d\n",
  428. __func__, id);
  429. goto out;
  430. }
  431. list_del(&ov->node);
  432. of_changeset_revert(&ov->cset);
  433. of_free_overlay_info(ov);
  434. idr_remove(&ov_idr, id);
  435. of_changeset_destroy(&ov->cset);
  436. kfree(ov);
  437. err = 0;
  438. out:
  439. mutex_unlock(&of_mutex);
  440. return err;
  441. }
  442. EXPORT_SYMBOL_GPL(of_overlay_destroy);
  443. /**
  444. * of_overlay_destroy_all() - Removes all overlays from the system
  445. *
  446. * Removes all overlays from the system in the correct order.
  447. *
  448. * Returns 0 on success, or an negative error number
  449. */
  450. int of_overlay_destroy_all(void)
  451. {
  452. struct of_overlay *ov, *ovn;
  453. mutex_lock(&of_mutex);
  454. /* the tail of list is guaranteed to be safe to remove */
  455. list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
  456. list_del(&ov->node);
  457. of_changeset_revert(&ov->cset);
  458. of_free_overlay_info(ov);
  459. idr_remove(&ov_idr, ov->id);
  460. kfree(ov);
  461. }
  462. mutex_unlock(&of_mutex);
  463. return 0;
  464. }
  465. EXPORT_SYMBOL_GPL(of_overlay_destroy_all);