overlay-notes.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ========================
  3. Devicetree 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 Devicetree'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. 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. A notifier callback for OF_OVERLAY_PRE_APPLY, OF_OVERLAY_POST_APPLY, or
  100. OF_OVERLAY_PRE_REMOVE may store pointers to a device tree node in the overlay
  101. or its content but these pointers must not persist past the notifier callback
  102. for OF_OVERLAY_POST_REMOVE. The memory containing the overlay will be
  103. kfree()ed after OF_OVERLAY_POST_REMOVE notifiers are called. Note that the
  104. memory will be kfree()ed even if the notifier for OF_OVERLAY_POST_REMOVE
  105. returns an error.
  106. The changeset notifiers in drivers/of/dynamic.c are a second type of notifier
  107. that could be triggered by applying or removing an overlay. These notifiers
  108. are not allowed to store pointers to a device tree node in the overlay
  109. or its content. The overlay code does not protect against such pointers
  110. remaining active when the memory containing the overlay is freed as a result
  111. of removing the overlay.
  112. Any other code that retains a pointer to the overlay nodes or data is
  113. considered to be a bug because after removing the overlay the pointer
  114. will refer to freed memory.
  115. Users of overlays must be especially aware of the overall operations that
  116. occur on the system to ensure that other kernel code does not retain any
  117. pointers to the overlay nodes or data. Any example of an inadvertent use
  118. of such pointers is if a driver or subsystem module is loaded after an
  119. overlay has been applied, and the driver or subsystem scans the entire
  120. devicetree or a large portion of it, including the overlay nodes.