overlay.c 14 KB

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