overlay.c 13 KB

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