0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ========================
0004 Devicetree Overlay Notes
0005 ========================
0006
0007 This document describes the implementation of the in-kernel
0008 device tree overlay functionality residing in drivers/of/overlay.c and is a
0009 companion document to Documentation/devicetree/dynamic-resolution-notes.rst[1]
0010
0011 How overlays work
0012 -----------------
0013
0014 A Devicetree's overlay purpose is to modify the kernel's live tree, and
0015 have the modification affecting the state of the kernel in a way that
0016 is reflecting the changes.
0017 Since the kernel mainly deals with devices, any new device node that result
0018 in an active device should have it created while if the device node is either
0019 disabled or removed all together, the affected device should be deregistered.
0020
0021 Lets take an example where we have a foo board with the following base tree::
0022
0023 ---- foo.dts ---------------------------------------------------------------
0024 /* FOO platform */
0025 /dts-v1/;
0026 / {
0027 compatible = "corp,foo";
0028
0029 /* shared resources */
0030 res: res {
0031 };
0032
0033 /* On chip peripherals */
0034 ocp: ocp {
0035 /* peripherals that are always instantiated */
0036 peripheral1 { ... };
0037 };
0038 };
0039 ---- foo.dts ---------------------------------------------------------------
0040
0041 The overlay bar.dts,
0042 ::
0043
0044 ---- bar.dts - overlay target location by label ----------------------------
0045 /dts-v1/;
0046 /plugin/;
0047 &ocp {
0048 /* bar peripheral */
0049 bar {
0050 compatible = "corp,bar";
0051 ... /* various properties and child nodes */
0052 };
0053 };
0054 ---- bar.dts ---------------------------------------------------------------
0055
0056 when loaded (and resolved as described in [1]) should result in foo+bar.dts::
0057
0058 ---- foo+bar.dts -----------------------------------------------------------
0059 /* FOO platform + bar peripheral */
0060 / {
0061 compatible = "corp,foo";
0062
0063 /* shared resources */
0064 res: res {
0065 };
0066
0067 /* On chip peripherals */
0068 ocp: ocp {
0069 /* peripherals that are always instantiated */
0070 peripheral1 { ... };
0071
0072 /* bar peripheral */
0073 bar {
0074 compatible = "corp,bar";
0075 ... /* various properties and child nodes */
0076 };
0077 };
0078 };
0079 ---- foo+bar.dts -----------------------------------------------------------
0080
0081 As a result of the overlay, a new device node (bar) has been created
0082 so a bar platform device will be registered and if a matching device driver
0083 is loaded the device will be created as expected.
0084
0085 If the base DT was not compiled with the -@ option then the "&ocp" label
0086 will not be available to resolve the overlay node(s) to the proper location
0087 in the base DT. In this case, the target path can be provided. The target
0088 location by label syntax is preferred because the overlay can be applied to
0089 any base DT containing the label, no matter where the label occurs in the DT.
0090
0091 The above bar.dts example modified to use target path syntax is::
0092
0093 ---- bar.dts - overlay target location by explicit path --------------------
0094 /dts-v1/;
0095 /plugin/;
0096 &{/ocp} {
0097 /* bar peripheral */
0098 bar {
0099 compatible = "corp,bar";
0100 ... /* various properties and child nodes */
0101 }
0102 };
0103 ---- bar.dts ---------------------------------------------------------------
0104
0105
0106 Overlay in-kernel API
0107 --------------------------------
0108
0109 The API is quite easy to use.
0110
0111 1) Call of_overlay_fdt_apply() to create and apply an overlay changeset. The
0112 return value is an error or a cookie identifying this overlay.
0113
0114 2) Call of_overlay_remove() to remove and cleanup the overlay changeset
0115 previously created via the call to of_overlay_fdt_apply(). Removal of an
0116 overlay changeset that is stacked by another will not be permitted.
0117
0118 Finally, if you need to remove all overlays in one-go, just call
0119 of_overlay_remove_all() which will remove every single one in the correct
0120 order.
0121
0122 There is the option to register notifiers that get called on
0123 overlay operations. See of_overlay_notifier_register/unregister and
0124 enum of_overlay_notify_action for details.
0125
0126 A notifier callback for OF_OVERLAY_PRE_APPLY, OF_OVERLAY_POST_APPLY, or
0127 OF_OVERLAY_PRE_REMOVE may store pointers to a device tree node in the overlay
0128 or its content but these pointers must not persist past the notifier callback
0129 for OF_OVERLAY_POST_REMOVE. The memory containing the overlay will be
0130 kfree()ed after OF_OVERLAY_POST_REMOVE notifiers are called. Note that the
0131 memory will be kfree()ed even if the notifier for OF_OVERLAY_POST_REMOVE
0132 returns an error.
0133
0134 The changeset notifiers in drivers/of/dynamic.c are a second type of notifier
0135 that could be triggered by applying or removing an overlay. These notifiers
0136 are not allowed to store pointers to a device tree node in the overlay
0137 or its content. The overlay code does not protect against such pointers
0138 remaining active when the memory containing the overlay is freed as a result
0139 of removing the overlay.
0140
0141 Any other code that retains a pointer to the overlay nodes or data is
0142 considered to be a bug because after removing the overlay the pointer
0143 will refer to freed memory.
0144
0145 Users of overlays must be especially aware of the overall operations that
0146 occur on the system to ensure that other kernel code does not retain any
0147 pointers to the overlay nodes or data. Any example of an inadvertent use
0148 of such pointers is if a driver or subsystem module is loaded after an
0149 overlay has been applied, and the driver or subsystem scans the entire
0150 devicetree or a large portion of it, including the overlay nodes.