Skip to content

Commit

Permalink
kbuild: add generic support for built-in boot DTBs
Browse files Browse the repository at this point in the history
Some architectures embed boot DTBs in vmlinux. A potential issue for
these architectures is a race condition during parallel builds because
Kbuild descends into arch/*/boot/dts/ twice.

One build thread is initiated by the 'dtbs' target, which is a
prerequisite of the 'all' target in the top-level Makefile:

  ifdef CONFIG_OF_EARLY_FLATTREE
  all: dtbs
  endif

For architectures that support the built-in boot dtb, arch/*/boot/dts/
is visited also during the ordinary directory traversal in order to
build obj-y objects that wrap DTBs.

Since these build threads are unaware of each other, they can run
simultaneously during parallel builds.

This commit introduces a generic build rule to scripts/Makefile.vmlinux
to support embedded boot DTBs in a race-free way. Architectures that
want to use this rule need to select CONFIG_GENERIC_BUILTIN_DTB.

After the migration, Makefiles under arch/*/boot/dts/ will be visited
only once to build only *.dtb files.

This change also aims to unify the CONFIG options used for built-in DTBs
support. Currently, different architectures use different CONFIG options
for the same purposes.

With this commit, the CONFIG options will be unified as follows:

 - CONFIG_GENERIC_BUILTIN_DTB

   This enables the generic rule for built-in boot DTBs. This will be
   renamed to CONFIG_BUILTIN_DTB after all architectures migrate to the
   generic rule.

 - CONFIG_BUILTIN_DTB_NAME

   This specifies the path to the embedded DTB.
   (relative to arch/*/boot/dts/)

 - CONFIG_BUILTIN_DTB_ALL

   If this is enabled, all DTB files compiled under arch/*/boot/dts/ are
   embedded into vmlinux. Only used by MIPS.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
  • Loading branch information
masahir0y committed Nov 4, 2024
1 parent 985d6cc commit 654102d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,10 @@ ifdef CONFIG_OF_EARLY_FLATTREE
all: dtbs
endif

ifdef CONFIG_GENERIC_BUILTIN_DTB
vmlinux: dtbs
endif

endif

PHONY += scripts_dtc
Expand Down Expand Up @@ -1500,7 +1504,8 @@ CLEAN_FILES += vmlinux.symvers modules-only.symvers \
modules.builtin modules.builtin.modinfo modules.nsdeps \
modules.builtin.ranges vmlinux.o.map \
compile_commands.json rust/test \
rust-project.json .vmlinux.objs .vmlinux.export.c
rust-project.json .vmlinux.objs .vmlinux.export.c \
.builtin-dtbs-list .builtin-dtb.S

# Directories & files removed with 'make mrproper'
MRPROPER_FILES += include/config include/generated \
Expand Down
6 changes: 6 additions & 0 deletions drivers/of/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
config DTC
bool

config GENERIC_BUILTIN_DTB
bool

config BUILTIN_DTB_ALL
bool

menuconfig OF
bool "Device Tree and Open Firmware support"
help
Expand Down
44 changes: 44 additions & 0 deletions scripts/Makefile.vmlinux
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,50 @@ quiet_cmd_cc_o_c = CC $@
%.o: %.c FORCE
$(call if_changed_dep,cc_o_c)

quiet_cmd_as_o_S = AS $@
cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<

%.o: %.S FORCE
$(call if_changed_dep,as_o_S)

# Built-in dtb
# ---------------------------------------------------------------------------

quiet_cmd_wrap_dtbs = WRAP $@
cmd_wrap_dtbs = { \
echo '\#include <asm-generic/vmlinux.lds.h>'; \
echo '.section .dtb.init.rodata,"a"'; \
while read dtb; do \
symbase=__dtb_$$(basename -s .dtb "$${dtb}" | tr - _); \
echo '.balign STRUCT_ALIGNMENT'; \
echo ".global $${symbase}_begin"; \
echo "$${symbase}_begin:"; \
echo '.incbin "'$$dtb'" '; \
echo ".global $${symbase}_end"; \
echo "$${symbase}_end:"; \
done < $<; \
} > $@

.builtin-dtbs.S: .builtin-dtbs-list FORCE
$(call if_changed,wrap_dtbs)

quiet_cmd_gen_dtbs_list = GEN $@
cmd_gen_dtbs_list = \
$(if $(CONFIG_BUILTIN_DTB_NAME), echo "arch/$(SRCARCH)/boot/dts/$(CONFIG_BUILTIN_DTB_NAME).dtb",:) > $@

.builtin-dtbs-list: arch/$(SRCARCH)/boot/dts/dtbs-list FORCE
$(call if_changed,$(if $(CONFIG_BUILTIN_DTB_ALL),copy,gen_dtbs_list))

targets += .builtin-dtbs-list

ifdef CONFIG_GENERIC_BUILTIN_DTB
targets += .builtin-dtbs.S .builtin-dtbs.o
vmlinux: .builtin-dtbs.o
endif

# vmlinux
# ---------------------------------------------------------------------------

ifdef CONFIG_MODULES
targets += .vmlinux.export.o
vmlinux: .vmlinux.export.o
Expand Down
4 changes: 4 additions & 0 deletions scripts/link-vmlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ vmlinux_link()
libs="${KBUILD_VMLINUX_LIBS}"
fi

if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then
objs="${objs} .builtin-dtbs.o"
fi

if is_enabled CONFIG_MODULES; then
objs="${objs} .vmlinux.export.o"
fi
Expand Down

0 comments on commit 654102d

Please sign in to comment.