Devicetree Overlay Notes

This document describes the implementation of the in-kerneldevice tree overlay functionality residing in drivers/of/overlay.c and is acompanion document toDevicetree Dynamic Resolver Notes[1]

How overlays work

A Devicetree’s overlay purpose is to modify the kernel’s live tree, andhave the modification affecting the state of the kernel in a way thatis reflecting the changes.Since the kernel mainly deals with devices, any new device node that resultsin an active device should have it created while if the device node is eitherdisabled or removed all together, the affected device should be deregistered.

Let’s take an example where we have a foo board with the following base tree:

---- foo.dts ---------------------------------------------------------------    /* FOO platform */    /dts-v1/;    / {            compatible = "corp,foo";            /* shared resources */            res: res {            };            /* On chip peripherals */            ocp: ocp {                    /* peripherals that are always instantiated */                    peripheral1 { ... };            };    };---- foo.dts ---------------------------------------------------------------

The overlay bar.dtso,

---- bar.dtso - overlay target location by label ---------------------------    /dts-v1/;    /plugin/;    &ocp {            /* bar peripheral */            bar {                    compatible = "corp,bar";                    ... /* various properties and child nodes */            };    };---- bar.dtso --------------------------------------------------------------

when loaded (and resolved as described in [1]) should result in foo+bar.dts:

---- foo+bar.dts -----------------------------------------------------------    /* FOO platform + bar peripheral */    / {            compatible = "corp,foo";            /* shared resources */            res: res {            };            /* On chip peripherals */            ocp: ocp {                    /* peripherals that are always instantiated */                    peripheral1 { ... };                    /* bar peripheral */                    bar {                            compatible = "corp,bar";                            ... /* various properties and child nodes */                    };            };    };---- foo+bar.dts -----------------------------------------------------------

As a result of the overlay, a new device node (bar) has been createdso a bar platform device will be registered and if a matching device driveris loaded the device will be created as expected.

If the base DT was not compiled with the -@ option then the “&ocp” labelwill not be available to resolve the overlay node(s) to the proper locationin the base DT. In this case, the target path can be provided. The targetlocation by label syntax is preferred because the overlay can be applied toany base DT containing the label, no matter where the label occurs in the DT.

The above bar.dtso example modified to use target path syntax is:

---- bar.dtso - overlay target location by explicit path -------------------    /dts-v1/;    /plugin/;    &{/ocp} {            /* bar peripheral */            bar {                    compatible = "corp,bar";                    ... /* various properties and child nodes */            }    };---- bar.dtso --------------------------------------------------------------

Overlay in-kernel API

The API is quite easy to use.

  1. Callof_overlay_fdt_apply() to create and apply an overlay changeset. Thereturn value is an error or a cookie identifying this overlay.

  2. Callof_overlay_remove() to remove and clean up the overlay changesetpreviously created via the call toof_overlay_fdt_apply(). Removal of anoverlay changeset that is stacked by another will not be permitted.

Finally, if you need to remove all overlays in one-go, just callof_overlay_remove_all() which will remove every single one in the correctorder.

There is the option to register notifiers that get called onoverlay operations. See of_overlay_notifier_register/unregister andenumof_overlay_notify_action for details.

A notifier callback for OF_OVERLAY_PRE_APPLY, OF_OVERLAY_POST_APPLY, orOF_OVERLAY_PRE_REMOVE may store pointers to a device tree node in the overlayor its content but these pointers must not persist past the notifier callbackfor OF_OVERLAY_POST_REMOVE. The memory containing the overlay will bekfree()ed after OF_OVERLAY_POST_REMOVE notifiers are called. Note that thememory will bekfree()ed even if the notifier for OF_OVERLAY_POST_REMOVEreturns an error.

The changeset notifiers in drivers/of/dynamic.c are a second type of notifierthat could be triggered by applying or removing an overlay. These notifiersare not allowed to store pointers to a device tree node in the overlayor its content. The overlay code does not protect against such pointersremaining active when the memory containing the overlay is freed as a resultof removing the overlay.

Any other code that retains a pointer to the overlay nodes or data isconsidered to be a bug because after removing the overlay the pointerwill refer to freed memory.

Users of overlays must be especially aware of the overall operations thatoccur on the system to ensure that other kernel code does not retain anypointers to the overlay nodes or data. Any example of an inadvertent useof such pointers is if a driver or subsystem module is loaded after anoverlay has been applied, and the driver or subsystem scans the entiredevicetree or a large portion of it, including the overlay nodes.