LoongArch: Allow device trees be built into the kernel
authorBinbin Zhou <zhoubinbin@loongson.cn>
Wed, 17 Jan 2024 04:43:00 +0000 (12:43 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Wed, 17 Jan 2024 04:43:00 +0000 (12:43 +0800)
During the upstream progress of those DT-based drivers, DT properties
are changed a lot so very different from those in existing bootloaders.
It is inevitably that some existing systems do not provide a standard,
canonical device tree to the kernel at boot time. So let's provide a
device tree table in the kernel, keyed by the dts filename, containing
the relevant DTBs.

We can use the built-in dts files as references. Each SoC has only one
built-in dts file which describes all possible device information of
that SoC, so the dts files are good examples during development.

And as a reference, our built-in dts file only enables the most basic
bootable combinations (so it is generic enough), acts as an alternative
in case the dts in the bootloader is unexpected.

Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/Kbuild
arch/loongarch/Kconfig
arch/loongarch/Makefile
arch/loongarch/boot/dts/Makefile
arch/loongarch/kernel/setup.c

index beb8499dd8ed84330beecbcd61977df0aa3474f8..bfa21465d83afcd4908cc000e49a8c47aea0d165 100644 (file)
@@ -4,6 +4,7 @@ obj-y += net/
 obj-y += vdso/
 
 obj-$(CONFIG_KVM) += kvm/
+obj-$(CONFIG_BUILTIN_DTB) += boot/dts/
 
 # for cleaning
 subdir- += boot
index 6b9da3effdf8ef34577ec7dbbadf2159ff83bde0..ede2ef26726a8d9f58dacebed5ee9efd1f2a4e0e 100644 (file)
@@ -375,6 +375,24 @@ config CMDLINE_FORCE
 
 endchoice
 
+config BUILTIN_DTB
+       bool "Enable built-in dtb in kernel"
+       depends on OF
+       help
+         Some existing systems do not provide a canonical device tree to
+         the kernel at boot time. Let's provide a device tree table in the
+         kernel, keyed by the dts filename, containing the relevant DTBs.
+
+         Built-in DTBs are generic enough and can be used as references.
+
+config BUILTIN_DTB_NAME
+       string "Source file for built-in dtb"
+       depends on BUILTIN_DTB
+       help
+         Base name (without suffix, relative to arch/loongarch/boot/dts/)
+         for the DTS file that will be used to produce the DTB linked into
+         the kernel.
+
 config DMI
        bool "Enable DMI scanning"
        select DMI_SCAN_MACHINE_NON_EFI_FALLBACK
index ba45cb7b621cdc1fb57e1d41dca591629e3ab55a..983aa2b1629a69fe74c4c8358d094fbd424283b9 100644 (file)
@@ -6,6 +6,7 @@
 boot   := arch/loongarch/boot
 
 KBUILD_DEFCONFIG := loongson3_defconfig
+KBUILD_DTBS      := dtbs
 
 image-name-y                   := vmlinux
 image-name-$(CONFIG_EFI_ZBOOT) := vmlinuz
@@ -144,7 +145,7 @@ endif
 
 vdso-install-y += arch/loongarch/vdso/vdso.so.dbg
 
-all:   $(notdir $(KBUILD_IMAGE))
+all:   $(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS)
 
 vmlinuz.efi: vmlinux.efi
 
index 5f1f55e911adf543ab5c113b06f81488ee984e59..1e24cdb5180aff1af7298300668bb0533b945500 100644 (file)
@@ -1,4 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
-dtstree        := $(srctree)/$(src)
 
-dtb-y := $(patsubst $(dtstree)/%.dts,%.dtb, $(wildcard $(dtstree)/*.dts))
+obj-$(CONFIG_BUILTIN_DTB)      += $(addsuffix .dtb.o, $(CONFIG_BUILTIN_DTB_NAME))
index d183a745fb85d4efcef51bbdab6d09a1e047b966..15d366b8407c3acbde526918c5846513f080c7ce 100644 (file)
@@ -295,8 +295,12 @@ static void __init fdt_setup(void)
        if (acpi_os_get_root_pointer())
                return;
 
-       /* Look for a device tree configuration table entry */
-       fdt_pointer = efi_fdt_pointer();
+       /* Prefer to use built-in dtb, checking its legality first. */
+       if (!fdt_check_header(__dtb_start))
+               fdt_pointer = __dtb_start;
+       else
+               fdt_pointer = efi_fdt_pointer(); /* Fallback to firmware dtb */
+
        if (!fdt_pointer || fdt_check_header(fdt_pointer))
                return;
 
@@ -330,7 +334,9 @@ static void __init bootcmdline_init(char **cmdline_p)
                if (boot_command_line[0])
                        strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
 
-               strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
+               if (!strstr(boot_command_line, init_command_line))
+                       strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
+
                goto out;
        }
 #endif