4

If I build a kernel driver as a module, it can be inserted into the kernel at run-time manually with insmod or modprobe, that i am clear. But are the following statements about device tree, correct?

  1. If I build a kernel driver as a module it will be auto inserted into the kernel at boot time, depending on whether it is listed in device tree or not
  2. If I build a kernel driver as a built-in, it will be auto inserted into kernel at boot time regardless of device tree
Abhinandan
  • 159
  • 1
  • 14
Consy
  • 326
  • 3
  • 22
  • 2
    If you expect the (built-in or loadable) driver to be used, then the Device Tree has to reference it. See https://stackoverflow.com/questions/26840267/driver-code-in-kernel-module-doesnt-execute – sawdust Oct 03 '17 at 09:20

2 Answers2

7

Built-in kernel driver still need device tree? Yes. Concept of device tree is orthogonal to whether drivers are built-in or compiled as modules. Device tree contains information about the hardware. Platform bus passes that info to relevant drivers.

This is a good brief article about platform bus, and how things used to be before device tree: https://www.codeproject.com/tips/1080177/linux-platform-device-driver. Device tree factors out harware info otherwiese hard-coded in kernel code, making the code more portable.

bytefire
  • 4,156
  • 2
  • 27
  • 36
  • But you can run a system without any device tree – Basile Starynkevitch Oct 03 '17 at 09:55
  • 1
    True, but what has that to do with whether device tree is needed when drivers are built as modules vs built into kernel? – bytefire Oct 03 '17 at 09:58
  • It is easy if you don't use any modules (and don't need any initrd) – Basile Starynkevitch Oct 03 '17 at 09:59
  • From my understanding, I don't think that's what's being asked. What you say is correct though. – bytefire Oct 03 '17 at 10:02
  • Are you sure that a fully static kernel won't run without a device tree? Your first "Yes" is supposing that. I don't think it is true, but if I am wrong, please give sources. – Basile Starynkevitch Oct 03 '17 at 10:03
  • 1
    Depends on your definition of fully static. You can choose to hard code device info into kernel code and thereby not require device tree. But that's not related to whether drivers are built into kernel or not :) – bytefire Oct 03 '17 at 10:04
  • A fully static kernel don't have any module and don't load any. – Basile Starynkevitch Oct 03 '17 at 10:11
  • If today's computers really need a device tree, please explain why. I'm pretty sure they don't need one in principle. It is a *convenience* – Basile Starynkevitch Oct 03 '17 at 10:20
  • Embedded systems do. Read up on platform bus, that will help in understanding rationale behind device tree. – bytefire Oct 03 '17 at 10:21
  • Any references please? – Basile Starynkevitch Oct 03 '17 at 10:24
  • @BasileStarynkevitch *"I'm pretty sure they don't need one in principle."* -- Depends on the arch. Citing x86 or SPARC examples probably aren't relevant to the OP. **The DT provides the configuration parameters that describes the system hardware.** All modern Linux drivers require configuration parameters, e.g. the platform structure. For modern ARM (and maybe PowerPC) Linux the DT is required, otherwise ATAGs are required per https://www.kernel.org/doc/Documentation/arm/Booting Also see https://events.linuxfoundation.org/sites/events/files/slides/petazzoni-device-tree-dummies.pdf – sawdust Oct 04 '17 at 00:06
  • I think that is the answer i am looking for, a fully static kernel "may" still need DT for configuration parameters, depending on how the driver is being written (parameters are hard coded in driver or made configurable through DT). – Consy Oct 04 '17 at 01:20
-1

In 2, there is no insertion step; the builtin driver is statically part of the kernel (and there is no moment where it is not part of it)

You can configure then compile and use a fully static kernel with all useful drivers built-in (so without modules and without initrd). You'll set CONFIG_MODULES=n, you'll never use m in your kernel .config file, and you'll set to y all the stuff needed for your particular hardware and system (e.g. file systems). See also this unanswered question. I guess that many embedded systems using Linux have a kernel compiled that way and don't use any initrd.

You can use a driver (either a builtin one, or a module which has been loaded) without the device tree since you could mknod(2) the device into a file path anywhere.

See https://www.devicetree.org/ and notice that older Linux systems did not have any. The device tree is mostly a useful convenience (improving over udev and devfsd). And you can avoid it entirely: you could run a fully static kernel without any device tree, on a root file system populated with block and character devices, like Linux distros of 1995 did. Hence a built-in kernel driver don't need any device tree, even today.

Notice that old Unix systems (e.g. SunOS3 in the mid-1980s, or Linux 1.x kernels in the 1990s) did not have anything of the sort. At that time you just relinked statically the kernel with appropriate drivers then used device files: character devices or block devices (and their driver was a builtin part of the kernel). Most driver code had some initialization which probed the existence of the relevant hardware (and that did slow down the boot process).

BTW, your question is dependent of how the kernel is loaded, of the hardware, and of the firmware loading it. For example, you can build hardware with the Linux kernel embedded in its firmware (in ROM).

Maybe with recent UEFI some recent hardware requires a device tree. AFAIK, BIOS don't need any. You could configure and compile a static kernel and a special init program which don't even use any device tree, and use very few pre-existing device files. Of course you need some root file system providing them.

Look also on OSDEV, and into hobby OSes mentionned there. Or into FreeBSD etc.... They don't use (or at least can be configured to avoid) any device tree AFAIK.

I probably could boot a 1995 Linux distro on my PC (at least if I have a floppy disk unit) -at least with a recent fully static kernel tailored to my hardware- and it would work without any device tree (which at that time did not exist).

Read Operating Systems: Three Easy Pieces (freely downloadable).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • So is it true that it still depends on how the driver is being written, it may still look for driver parameters from the device tree node, if it is not existent on the device tree the driver may fail? – Consy Oct 03 '17 at 07:46
  • I don't even understand all of that question. Ask it separately. – Basile Starynkevitch Oct 03 '17 at 07:52
  • This answer has several errors. *"the builtin driver ...and there is no moment where it is not part of it"* -- A built-in driver will not be used (i.e. its probe function will not even be called) if the driver is not referenced in the Device Tree. *"It is mostly a useful convenience"* -- Not every OS or arch in Linux uses DT, but DT is more than a *"useful convenience"*; it's necessary to configure the system, i.e. have the proper drivers to support the hardware. For old Linux on ARM, config was done with ATAGs and platform file (hardcoded config). – sawdust Oct 03 '17 at 09:20
  • 1
    Contains a lot of unrelated info – bytefire Oct 03 '17 at 09:27
  • AFAIK, you can run a kernel without any device tree, even today. The device tree is really optional. You could build a system without any. – Basile Starynkevitch Oct 03 '17 at 09:53