overlay.c 14 KB

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