Device tree

Also known as: devicetree

On this page 7

A device tree (also written devicetree) is a hierarchical data structure that describes the hardware of a computer, such as its processors, memory, buses and peripherals, so that an operating system kernel can find and configure devices that cannot announce themselves. In the field of drivers & devices, it is the standard way for operating systems such as Linux, FreeBSD and Zephyr to learn the layout of ARM, RISC-V and PowerPC systems without hard-coding it into the kernel.

The device tree separates the description of a board from the code that drives it. One kernel image can then boot on many different boards, because each board ships a small binary blob that tells the kernel which devices exist, where their registers live and how their interrupts are wired.

Why non-discoverable hardware needs a device tree

A device tree is needed because many buses provide no enumeration mechanism. PCI Express and USB devices report vendor and device identifiers when the system probes them, which is how plug and play works on a typical PC. Peripherals on a system on a chip, by contrast, sit at fixed memory-mapped I/O addresses on internal buses, and an I2C sensor or SPI flash chip has no standard way to announce its presence.

Before device trees became common on ARM around 2011, Linux described each board in C source files called board files. Every new board needed kernel code, the ARM tree grew rapidly, and a single kernel could not support many boards. Moving the description into data solved the problem and is the reason the device tree is now mandatory for new ARM boards in mainline Linux.

The following table compares the main ways an operating system learns about hardware.

MethodTypical platformHow devices are foundMain strength
Device treeARM, RISC-V, PowerPC, embeddedStatic description passed by the bootloaderSimple, compact, board-independent kernels
ACPIx86 PCs, ARM serversFirmware tables with executable AML methodsPower management and runtime methods
Bus enumerationPCI Express, USBDevices report identifiers when probedFully dynamic, no description needed
Board filesLegacy ARM LinuxHard-coded C structures in the kernelNone today, deprecated
Hardware description methods compared

Structure of a device tree

A device tree is a tree of nodes with properties. Each node represents a device or bus, and its child nodes represent devices attached to it. The root node describes the board, and typical top-level children are cpus, memory, chosen for boot parameters and the SoC bus node that contains on-chip peripherals.

Properties are name and value pairs. A small set of standard properties carries most of the information drivers need, as the table shows.

PropertyMeaningExample value
compatibleIdentifies the device model for driver matching"vendor,uart-v2"
regRegister address and size on the parent bus<0x10000000 0x1000>
interruptsInterrupt lines used by the device<0 33 4>
statusWhether the device is enabledEnabled or "disabled"
clocksReferences to clock providers<&clk 12>
#address-cellsNumber of cells in child addresses<1> or <2>
Key device tree properties

Nodes refer to each other through phandles, numeric handles written with labels such as &gpio1. This is how a device names its interrupt controller, clocks, resets, regulators and pins. The compatible string is the most important property: the kernel compares it with the match tables of registered drivers and binds the first driver that declares the same string.

From source file to running kernel

A device tree passes through a defined build and boot pipeline. Developers write human-readable source, a compiler turns it into a binary, the bootloader loads it into memory and the kernel parses it early during boot.

FormatExtensionPurpose
Device tree source.dtsBoard description written by developers
Include file.dtsiShared description of an SoC or module
Device tree blob.dtbCompiled flattened binary loaded at boot
Overlay.dtboFragment that modifies a base tree
Device tree file formats

The device tree compiler, dtc, converts source into a flattened device tree (FDT). A bootloader such as U-Boot loads the blob next to the kernel, may patch it with memory size or the boot command line, and passes its address in a register. The kernel then unflattens the tree into in-memory structures, creates platform devices for enabled nodes and lets the driver model match them with drivers. On Linux, the live tree is visible under /proc/device-tree.

Device tree overlays extend this model. An overlay adds or changes nodes at boot or runtime, which is how single-board computers describe add-on boards and expansion hats without editing the base tree.

Bindings and the specification

The format itself is defined by the Devicetree Specification, which grew out of the Open Firmware standard IEEE 1275. The specification fixes the syntax, the blob layout and the generic properties, but it does not say what properties a particular UART or camera needs.

That second layer is defined by bindings, documents that state which properties a given compatible value requires and what they mean. In Linux, bindings are written as YAML schemas and validated with automated tools, so a malformed tree can be caught at build time rather than at boot. A stable binding is treated as an ABI: the kernel should keep accepting older trees, although in practice this guarantee is honored unevenly across vendors.

Common problems and misconceptions

Most device tree problems show up as a driver that never probes or a device that probes with wrong settings. The table lists frequent symptoms and their usual causes.

SymptomLikely causeFix
Driver does not loadMismatched compatible stringMatch the string in the driver’s table
Device absentNode has status disabledEnable the node in the board file
No interrupts arriveWrong interrupt number or trigger typeCheck the SoC manual and interrupt parent
Boot hangs earlyWrong memory node or reg valuesVerify addresses against the hardware
Typical device tree mistakes
  • The device tree is not a driver: it only describes hardware, and a driver must still exist in the kernel.
  • It does not configure plug-and-play buses in detail: devices on PCI Express or USB are still enumerated dynamically.
  • It should describe hardware, not policy: settings such as user preferences belong in user space, not in the tree.

The short version

Some computer parts can introduce themselves when the computer starts, but many small chips on phones and single-board computers cannot. A device tree is like a floor plan handed to the operating system: it lists every part, where it sits and how it is connected.

With this plan, the same operating system can run on many different boards. The map changes from board to board, while the software that reads it stays the same.

Where it makes a difference

An engineer designs an industrial controller around an ARM system on a chip and adds a temperature sensor on the I2C bus at address 0x48, plus a status LED on a GPIO pin. Instead of modifying kernel code, the engineer copies the vendor’s .dtsi file, writes a short .dts file with two new nodes and compiles it with dtc.

At boot, U-Boot loads the stock mainline kernel and the new blob. The kernel matches the sensor node with its existing hwmon driver and the LED node with the GPIO LED driver, and both devices appear in /sys without a single line of driver code. When a later hardware revision moves the sensor to address 0x49, the team changes one value in the tree and ships the same kernel image.