Device Tree Linux
Device Tree Linux
org
Contents
Device Tree Framework Source Code
Linux Man pages
Linux Gotchas
disabled nodes
node name case sensitivity
phandles
phandle source property
phandle access in kernel (by drivers)
https://git.kernel.org/cgit/utils/dtc/dtc.git
git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
Linux Gotchas
disabled nodes
Linux has widespread use of the "status" property to indicate that a node does not exist. This is used to create a generic
.dtsi file that defines all of the potential components of a device which might contain many different functions. When
the device is used in a system, many of the functions might not be routed to connectors or might not be usable due to
needing pins that are multiplexed with other devices that are active. The convention is to set the "status" property of
such functions to "disabled" in the .dtsi file, then the system .dts file that includes the .dtsi will change the "status"
property of functions that should be enabled to "okay".
Some kernel code does not check the node "status" property before trying to configure, probe, or enable it.
Expect efforts to fix the kernel code to respect the "status" property.
Expect this to change so that the generic node name compare function is case sensitive, and only a few select sub-
architectures will not be case sensitive.
The move to a case sensitive compare began in v4.19 with commit f42b0e18f2e5cf34f73ef1b6327b49040b307a33 ("of:
add node name compare helper functions"), which creates of_node_name_eq() and of_node_name_prefix(). Use of
these functions is added in later commits.
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 2/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
Note that dtc has had node name case sensitivity for a long time (or maybe even from the beginning).
phandles
ibm,phandle
linux,phandle
phandle
Property "linux,phandle" is obsolete. If it exists, its value should be equivalent to the value of property "phandle".
Property "ibm,phandle" may be used in the IBM pSeries dynamic device tree.
ibm,phandle
linux,phandle
phandle
of_find_node_by_phandle()
of_for_each_phandle()
of_parse_phandle_with_args()
of_parse_phandle_with_fixed_args()
The directory structure for the .dts and .dtsi files varies by architecture. Some architectures place all .dts files at a
single level, others use two or level levels of directories.
===== arch/arc/boot/
dts
===== arch/arm/boot/
dts
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 3/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
===== arch/arm64/boot/
dts
dts/actions
dts/realtek
dts/altera
dts/lg
dts/rockchip
dts/broadcom
dts/broadcom/stingray
dts/broadcom/northstar2
dts/broadcom/bcm4908
dts/socionext
dts/xilinx
dts/bitmain
dts/cavium
dts/allwinner
dts/sprd
dts/intel
dts/amlogic
dts/qcom
dts/toshiba
dts/microchip
dts/arm
dts/ti
dts/marvell
dts/nvidia
dts/freescale
dts/amazon
dts/hisilicon
dts/amd
dts/mediatek
dts/apm
dts/synaptics
dts/zte
dts/renesas
dts/exynos
===== arch/c6x/boot/
dts
===== arch/csky/boot/
dts
===== arch/h8300/boot/
dts
===== arch/microblaze/boot/
dts
===== arch/mips/boot/
dts
dts/netlogic
dts/mti
dts/ingenic
dts/img
dts/mscc
dts/xilfpga
dts/ralink
dts/ni
dts/loongson
dts/cavium-octeon
dts/lantiq
dts/brcm
dts/pic32
dts/qca
===== arch/nds32/boot/
dts
===== arch/nios2/boot/
dts
===== arch/openrisc/boot/
dts
===== arch/powerpc/boot/
dts
dts/fsl
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 4/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
===== arch/riscv/boot/
dts
dts/sifive
dts/kendryte
===== arch/sh/boot/
dts
===== arch/xtensa/boot/
dts
discussion
A discussion of the directory layout for .dts files was triggered by the announcement of a script to move the dts files
and update the makefiles [1 (https://lore.kernel.org/linux-arm-kernel/20181204183649.GA5716@bogus/)].
Linux conventions
hex constants are lower case
node names
should begin with a character in the range 'a' to 'z', 'A' to 'Z'
unit-address does not have a leading "0x" (the number is assumed to be hexadecimal)
unit-address does not have leading zeros
use dash "-" instead of underscore "_"
label names
should begin with a character in the range 'a' to 'z', 'A' to 'Z'
should be lowercase
use underscore "_" instead of dash "-"
property names
property ordering within a node (this describes bindings, but .dts should be consistent with bindings)
compatible
reg
common properties (alphabetical order)
vendor specific properties (alphabetical order)
tuple data
property data that is a tuple is more readable when grouped into tuples
preferred style:
instead of:
#define FDT_MAX_DEPTH 64
Linux Preferences
Items in this section may be less firm or certain than items in "Linux Conventions".
If you can document or explain why any item should move to "Linux Conventions" please email frowand.list at
gmail.com".
cpp
The Linux kernel build system preprocesses dts files with cpp before passing them to dtc. The dtc compileralso has an
"/include/" directive.
Files that are included via the "/include/" directive can not contain any cpp directives. This is because such files
are not visible to cpp.
Although cpp can do conditional inclusion with features such as "#if" and "#ifdef" this is frowned upon.
Common use of cpp is for "#include" and simple "#defines" for symbolic names.
Bindings related include files are in include/dt-bindings/
More complex macros do exist, but are rare.
Complex example 1
include/dt-bindings/pinctrl/omap.h:
#define OMAP_IOPAD_OFFSET(pa, offset) (((pa) & 0xffff) - (offset))
Complex example 2
include/dt-bindings/input/input.h:
interrupts vs interrupts-extended
Should I use 1) the "interrupts" property in association with the "interrupt-parent" property or 2) the "interrupts-
extended" property?
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 6/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
The main purpose of "interrupts-extended" is to allow one device to have multiple interrupts that are handled by
different controllers, without introducing a (more complex) "interrupt-map" property in the parent.
--- ePAPR
--- Linux
#address-cells defaults to 2
#size-cells defaults to 1
dtc may warn that the default is being used.
aliases node
--- ePAPR
--- Linux
chosen node
--- ePAPR
The chosen node is not required. If it was required, sections 3.1 and 3.5
would explicitly state such (for example, section 3.6 CPUS Node Properties
states 'A cpus node is required for all device trees.').
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 7/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
3.1 Base Device Node Types
'The sections that follow specify the requirements for the base set of device nodes required in an
ePAPR-compliant device tree.
All device trees shall have a root node and the following nodes shall be present at the root of all
device trees:
• One cpus node
• At least one memory node'
--- Linux
device_type property
--- ePAPR
2.3.11 device_type
'The device_type property was used in IEEE 1275 to describe the device’s FCode
programming model. Because ePAPR does not have FCode, new use of the property is
deprecated, and it should be included only on cpu and memory nodes for compatibility with
IEEE 1275–derived device trees.'
--- Linux
Many Linux bindings have a device_type property. Many of these bindings are not
well documented.
8042
CBEA-Internal-Interrupt-Controller
PowerPC-External-Interrupt-Presentation
adb
be
cache
cpm
cpu
display
fcu
fdc
interrupt-controller
ipic
isa
memory
memory-controller
mic-tm
mpc8xx-pic
nvram
open-pic
pci
pcie-endpoint
pervasive
pic-router
qe
qeic
serial
smu
soc
spe
tsi-bridge
labels
--- ePAPR
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 8/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
Node and property definitions
'Device tree nodes are defined with a node name and unit address with braces marking the start and
end of the node definition. They may be preceded by a label.'
--- Linux
names
Node name
--- ePAPR
node-name@unit-address
--- Linux
Discouraged:
#?.+*_
--- ePAPR
--- Linux
Property name
--- ePAPR
--- Linux
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 9/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
A-Z
,._+-?#
*
Discouraged:
?.+*_
# is only recommended as the first character of a property name
Label names
--- ePAPR
--- Linux
--- ePAPR
--- Linux
The validation tools use a YAML format. YAML format does not
allow a property name to match the name of the node containing
the property.
Some Apple OpenFirmware contains this sort of node name / property name
collision.
Linux does not have any node name / property name collisions. Adding
any such collision is not allowed. This was discussed in the email
thread rooted at https://www.spinics.net/lists/devicetree-spec/msg00978.html
NOTE: the dtc compiler allows a property name to match the name of
the node containing the property. There will not be an error when
a devicetree containing a node name / property name collision is compiled.
A patch to dtc to trigger a warning on node name / property name has been
accepted, but is not yet in a released version.
status property
--- ePAPR
2.3.4 status
'Valid values for status are: "okay", "disabled", "fail", "fail-sss".
"Refer to the device binding for details on what disabled means for
a given device.'
--- Linux
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 10/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
status == "disabled"
Not all code checks whether a node is disabled. These cases are bugs.
"ABI" stability
Are device tree bindings stable?
Well.... Let me put this open jar full of gasoline on the floor and step away for a little while. Oh, can I loan you a lighter
or a book of matches?
See slides 8 and following from the Grant Likely presentation at the Kernel Summit 2013 discussion: It's Broken!
Fixing the DT binding process (PDF)
"The Device Tree as a Stable ABI: A Fairy Tale?", ELC 2015 by Thomas Petazzoni (PDF)
The slides that Grant Likely presented at the Kernel Summit discussion were in an email (https://lists.linuxfoundatio
n.org/pipermail/ksummit-2013-discuss/2013-October/001907.html) attachment that the mail list stripped. Here are
the slides: It's Broken! Fixing the DT binding process (PDF)
Slides 18 and 19 of [PDF] "Device Tree, the Disaster so Far", ELC Europe 2013 by Mark Rutland.
This list is not an endorsement of any particular technique. It is instead a (partial) list of some existing code in the
Linux kernel.
The examples are not meant to capture each method entirely; they are instead meant to illustrate the basic concept.
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 11/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
The hardware version is used throughout the driver to choose alternate actions.
drivers/iommu/arm-smmu.c:
...
drivers/iio/adc/xilinx-xadc-core.c:
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 12/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
This struct pointed to by struct of_device_id.data in this example includes a function call table in addition to the
hardware description fields.
drivers/iio/adc/twl6030-gpadc.c:
The Linux kernel build system has a rule to automagically place an FDT in the kernel image as a binary data object. To
invoke the rule, add entries to the makefile of the form XXX.dtb.o to create a data object for devicetree source file
XXX.dts.
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 13/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
obj-y += my_system.dtb.o
obj-$(CONFIG_XXX) += XXX.dtb.o
PRECIOUS was also required in the makefile until removed by commit 54a702f70589 ("kbuild: mark $(targets) as
.SECONDARY and remove .PRECIOUS markers"), as of Linux kernel 4.17.
.PRECIOUS: \
$(obj)/%.dtb.S \
$(obj)/%.dtb
The data object can be accessed in the kernel through extern declarations of pointers to the beginning and just past the
end of the data:
This fixup is scheduled to be added in Linux version 4.16, 4.4.122, 4.9.88, 4.14.27, 4.15.10
Contrary to the standards, the Linux implementation provides both default values and inheritance.
of_n_addr_cells() will search up through the devicetree until a node with the #address-cells property is found. If none
is found the default value of OF_ROOT_NODE_ADDR_CELLS_DEFAULT is returned.
of_n_size_cells() will search up through the devicetree until a node with the #size-cells property is found. If none is
found the default value of OF_ROOT_NODE_SIZE_CELLS_DEFAULT is returned.
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 14/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
The Linux kernel supports a default for #address-cells and #size-cells to avoid breaking old OF based platforms which
did not set them.
... the default that Linux implements does not match the one defined by
the IEE1275 spec, in order to accomodate certain broken device trees on
certain machines (mostly Apple, I think). The IEEE1275 default is
simply 2, ... These properties were never supposed to be inherited,
but apparently some old firmwares didn't get the memo.
reference:
Date: Thu, 19 Jul 2018 15:37:09 +1000
From: David Gibson <[email protected]>
Subject: Re: [PATCH] libfdt: fdt_address_cells() and fdt_size_cells()
Message-ID: <[email protected]>
List-ID: <devicetree-compiler.vger.kernel.org>
The dtc devicetree compiler will warn if #address-cells or #size-cells is missing. New devicetree source is expected to
properly provide the values for #address-cells and #size-cells.
The /firmware node is used to describe the interface that the Linux kernel can use for run-time communication with
the firmware. Examples of run-time communication include secure calls, a mailbox, IPC, and hypervisor calls.
An exception to using the /firmware node for run-time communication with the firmware is if the firmware is accessed
through a register based interface, in which case the firmware would be under simple-bus. [1]
The specific bindings for the /firmware nodes on various platforms are documented in the same manner as other
Linux bindings.
[1] Rob Herring; aug 24, 2018; Re: Recognize "/firmware" node usage (https://www.spinics.net/lists/devicetree-spec/msg
00767.html)
Documentation/core-api/printk-formats.rst (https://git.kernel.org/?p=/linux/kernel/git/torvalds/linux.git;a=blob_p
lain;f=Documentation/core-api/printk-formats.rst) contains a description of the format specifiers. Check printk-
formats.rst for the current specification of the format specifiers.
%pOF[fnpPcCF]
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 15/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
- F - device node flags
- c - major compatible string
- C - full compatible string
Examples::
Notice that multiple arguments can follow '%pOF'. This has the unfortunate side effect that the current
implementation of decoding the format specifier with eat the character following the final argument if that character is
not detected as a boundary between specifiers, such as a space or a '%'. For example, in this snprintf(), the 'T' is meant
to be printed as a literal value, but is instead suppressed:
For a node with name of 'name' and a null pointer for type,
desired result: of:NnameT<NULL>
actual result: of:Nname<NULL>
boolean property
The devicetree standards do not define a boolean type, but the Linux kernel API includes a function to determine
whether a property is true or false:
/**
* of_property_read_bool - Findfrom a property
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
*
* Search for a property in a device node.
* Returns true if the property exists false otherwise.
*/
static inline bool of_property_read_bool(const struct device_node *np,
const char *propname)
{
The return value of of_property_read_bool() is based on whether the property is present or not. The return value is
not based on the value of the property. Thus the following confusing result:
a_node {
flag="false";
}
Yeah, no
(emphatic, colloquial) Used sarcastically to answer "no" to a question where the negative answer should have been
obvious. [1]
[1] https://en.wiktionary.org/wiki/yeah,_no#:~:text=
(emphatic%2C%20colloquial)%20Used%20sarcastically,answer%20should%20have%20been%20obvious.
Answer: there's already support for DT on x86. But, we should not mix DT and ACPI.
A controversial topic is ACPI overlays (putting DT in ACPI or ACPI in DT).
Don't want to have drivers that get part of their info from ACPI and part from DT. That's nuts.
Question: Can you have ACPI and DT at the same time on x86?
Answer: No. Some ARM64 systems have support for both ACPI and DT, but the system selects one to use at
runtime. They are not used at the same time.
You can run the DT unit tests on x86.
CONFIG_ options
Additional CONFIG_ options are unlikely to be accepted into the devicetree subsystem:
https://lore.kernel.org/all/[email protected]/
...
-Frank
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 17/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
For context, Rob and Frank are devicetree maintainers. Frank's final comment is an implicit agreement with Rob's
reasoning.
Object Lifetime
Devicetree Unittests
The devicetree unittests, drivers/of/unittest.c, uses FDT, EDT, and Changesets. In many cases the usage is as
described in the FDT, EDT, and Changesets general descriptions below. However there are exceptions to the general
descriptions due to test requirements.
No attempt will be made here to describe the exceptions; the unittest code must be read to understand the exceptions.
An FDT may be provided to the Linux kernel by the bootloader, or may be linked into a special section of the kernel
image.
Once overlay suppport is sufficiently complete and robust a way to load an overlay FDT from userspace might be
implemented. There has been an out of mainline tree implementation for many years.
Early in the kernel boot process, before normal memory allocation is available, the FDT passed by the bootloader is
copied from initial_boot_params into properly aligned memory, allocated by early_init_dt_alloc_memory_arch().
initial_boot_params is changed to point to the newly allocated memory and the memory containing the FDT
passed from the bootloader is not freed.
1. The Extended Device Tree (EDT) created from the FDT will contain pointers into the FDT.
2. If a new kernel image is booted via kexec (see drivers/of/kexec.c) then the FDT at initial_boot_params is copied
and modified to be passed to the newly booted kernel
overlay FDT
The API to apply an overlay FDT to the System Devicetree is of_overlay_fdt_apply(). The FDT passed to
of_overlay_fdt_apply() is copied to an overlay subsystem private copy, and thus may be freed by the caller.
The overlay subsystem private copy of the overlay FDT will not be freed after use because:
1. The OF_DETACHED Extended Device Tree (EDT) created from the FDT will contain pointers into the FDT.
2. The System Devicetree that the FDT is applied to will contain pointers into the FDT.
3. Even if the overlay is removed, the devicetree APIs to access the System Devicetree can return pointers into the
FDT and there is no way to guarantee that those pointers are no longer in use
add_changeset_property()
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 18/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
...
__of_prop_dup()
new->name = kstrdup()
new->value = kmemdup()
return new
...
Some of the APIs to access nodes in the system devicetree will default to starting to scan the tree at of_root if the caller
specifies a starting pointer with the value of NULL.
When a new dynamic EDT is created from an FDT, the EDT is marked as OF_DETACHED. If dynamic content is
added to the system devicetree, new objects are created in the system devicetree; the system devicetree will not
contain pointers into an OF_DETACHED EDT. However, the newly created nodes in the system devicetree may
contain pointers into the FDT that was expanded into the OF_DETACHED EDT.
CONFIG_OF_DYNAMIC
System devicetree internal data structure object lifetime is determined by CONFIG_OF_DYNAMIC.
Some additional portions of the concepts should apply as far back as v3.15-rc,
where commit 0829f6d1f69e "of: device_node kobject lifecycle fixes" fixed some
issues with struct device_node objects.
CONFIG_MPC885ADS
CONFIG_PPC_PSERIES
CONFIG_I2C_DEMUX_PINCTRL
CONFIG_HOTPLUG_PCI_POWERNV
CONFIG_OF_OVERLAY
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 19/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
CONFIG_OF_DYNAMIC=n
There is no concept of live devicetree object lifetime.
CONFIG_OF_DYNAMIC=y
Live devicetree object lifetime will be one of:
Objects that are created via a dynamic device tree API will exist as long as the associated devicetree node
reference count is positive.
The pseudo code does not show the entire contents of the functions.
of_node_init[node]
#if defined(CONFIG_OF_KOBJ)
kobject_init(&node->kobj, &of_node_ktype)
kobject_init[kobj, ktype]
kobj->ktype = ktype
of_node_put[node]
kobject_put(&node->kobj)
kobject_put[kobj]
kref_put(&kobj->kref, kobject_release)
kref_put[kref, release]
if (refcount_dec_and_test(&kref->refcount))
release(kref)
// release() is kobject_release()
kobj = container_of(kref, struct kobject, kref)
kobject_cleanup(kobj)
t = get_ktype(kobj)
return kobj->ktype
t->release(kobj)
// t->release() is of_node_release()
of_node_release[kobj]
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 20/21
8/27/23, 12:41 PM Device Tree Linux - eLinux.org
// kobject_cleanup() might continue up the tree:
// parent will either be NULL or kobj->parent
// parent should be NULL for devicetree
kobject_put(parent)
The memory associated with any property from an overlay that was added to a
devicetree node that was created from the boot time Flattened Device Tree (FDT)
will never be freed because the refcount of that node should never become zero.
Even if the refcount of the node becomes zero, of_node_put() will return without
freeing anything because the node is not detached, not dynamic, and not an
overlay node.
of_node_release(kobj)
// objects associated with kobj are not freed if
// any of the following is true:
// kobj is not detached
// kobj is not a dynamic node
// kobj if not an overlay node
if (of_node_check_flag(node, OF_OVERLAY))
// sanity check that a changeset free of kobj is in progress
if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET))
pr_err("ERROR: memory leak ...
// sanity check for unexpected properties in node
if (node->properties)
r_err("ERROR: %s(), unexpected properties in ...
property_list_free(node->properties)
property_list_free[prop_list]
// for each property in prop_list
kfree(prop->name)
kfree(prop->value)
kfree(prop)
property_list_free(node->deadprops)
property_list_free[prop_list]
// for each property in prop_list
kfree(prop->name)
kfree(prop->value)
kfree(prop)
fwnode_links_purge(of_fwnode_handle(node))
kfree(node->full_name)
kfree(node->data)
kfree(node)
Changesets
Each dynamic (run-time) change to the System Devicetree is described by a list of changeset entries (struct
of_changeset_entry). Overlay apply and remove code adds another layer of data structure, the struct
overlay_changeset, to describe how the overlay modifies the System Devicetree.
TODO: describe how changeset related data objects are allocated and freed.
Content is available under a Creative Commons Attribution-ShareAlike 3.0 Unported License unless otherwise noted.
https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1 21/21