overlay.c 14 KB

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