overlay-notes.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========================
  3. Device Tree Overlay Notes
  4. =========================
  5. This document describes the implementation of the in-kernel
  6. device tree overlay functionality residing in drivers/of/overlay.c and is a
  7. companion document to Documentation/devicetree/dynamic-resolution-notes.rst[1]
  8. How overlays work
  9. -----------------
  10. A Device Tree's overlay purpose is to modify the kernel's live tree, and
  11. have the modification affecting the state of the kernel in a way that
  12. is reflecting the changes.
  13. Since the kernel mainly deals with devices, any new device node that result
  14. in an active device should have it created while if the device node is either
  15. disabled or removed all together, the affected device should be deregistered.
  16. Lets take an example where we have a foo board with the following base tree::
  17. ---- foo.dts ---------------------------------------------------------------
  18. /* FOO platform */
  19. /dts-v1/;
  20. / {
  21. compatible = "corp,foo";
  22. /* shared resources */
  23. res: res {
  24. };
  25. /* On chip peripherals */
  26. ocp: ocp {
  27. /* peripherals that are always instantiated */
  28. peripheral1 { ... };
  29. };
  30. };
  31. ---- foo.dts ---------------------------------------------------------------
  32. The overlay bar.dts,
  33. ::
  34. ---- bar.dts - overlay target location by label ----------------------------
  35. /dts-v1/;
  36. /plugin/;
  37. &ocp {
  38. /* bar peripheral */
  39. bar {
  40. compatible = "corp,bar";
  41. ... /* various properties and child nodes */
  42. };
  43. };
  44. ---- bar.dts ---------------------------------------------------------------
  45. when loaded (and resolved as described in [1]) should result in foo+bar.dts::
  46. ---- foo+bar.dts -----------------------------------------------------------
  47. /* FOO platform + bar peripheral */
  48. / {
  49. compatible = "corp,foo";
  50. /* shared resources */
  51. res: res {
  52. };
  53. /* On chip peripherals */
  54. ocp: ocp {
  55. /* peripherals that are always instantiated */
  56. peripheral1 { ... };
  57. /* bar peripheral */
  58. bar {
  59. compatible = "corp,bar";
  60. ... /* various properties and child nodes */
  61. };
  62. };
  63. };
  64. ---- foo+bar.dts -----------------------------------------------------------
  65. As a result of the overlay, a new device node (bar) has been created
  66. so a bar platform device will be registered and if a matching device driver
  67. is loaded the device will be created as expected.
  68. If the base DT was not compiled with the -@ option then the "&ocp" label
  69. will not be available to resolve the overlay node(s) to the proper location
  70. in the base DT. In this case, the target path can be provided. The target
  71. location by label syntax is preferred because the overlay can be applied to
  72. any base DT containing the label, no matter where the label occurs in the DT.
  73. The above bar.dts example modified to use target path syntax is::
  74. ---- bar.dts - overlay target location by explicit path --------------------
  75. /dts-v1/;
  76. /plugin/;
  77. &{/ocp} {
  78. /* bar peripheral */
  79. bar {
  80. compatible = "corp,bar";
  81. ... /* various properties and child nodes */
  82. }
  83. };
  84. ---- bar.dts ---------------------------------------------------------------
  85. Overlay in-kernel API
  86. --------------------------------
  87. The API is quite easy to use.
  88. 1) Call of_overlay_fdt_apply() to create and apply an overlay changeset. The
  89. return value is an error or a cookie identifying this overlay.
  90. 2) Call of_overlay_remove() to remove and cleanup the overlay changeset
  91. previously created via the call to of_overlay_fdt_apply(). Removal of an
  92. overlay changeset that is stacked by another will not be permitted.
  93. Finally, if you need to remove all overlays in one-go, just call
  94. of_overlay_remove_all() which will remove every single one in the correct
  95. order.
  96. In addition, there is the option to register notifiers that get called on
  97. overlay operations. See of_overlay_notifier_register/unregister and
  98. enum of_overlay_notify_action for details.
  99. Note that a notifier callback is not supposed to store pointers to a device
  100. tree node or its content beyond OF_OVERLAY_POST_REMOVE corresponding to the
  101. respective node it received.