Skip to content

Commit 09a48d2

Browse files
committed
Automate DTB regeneration for SMP configuration
This fixes issue where DTB CPU count was inconsistent with runtime hart count, causing kernel to detect fewer CPUs than specified with '-c' parameter. The build system now ensures DTB matches SMP parameter automatically, eliminating the need for manual "make SMP=X riscv-harts.dtsi minimal.dtb".
1 parent 7b3120b commit 09a48d2

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@ S := $E $E
205205
CFLAGS += -D SEMU_BOOT_TARGET_TIME=10
206206

207207
SMP ?= 1
208+
209+
# Track SMP value changes to force DTB regeneration
210+
.smp_stamp: FORCE
211+
@if [ ! -f .smp_stamp ] || [ "$$(cat .smp_stamp 2>/dev/null)" != "$(SMP)" ]; then \
212+
echo "$(SMP)" > .smp_stamp; \
213+
rm -f riscv-harts.dtsi minimal.dtb; \
214+
fi
215+
208216
.PHONY: riscv-harts.dtsi
209-
riscv-harts.dtsi:
217+
riscv-harts.dtsi: .smp_stamp
210218
$(Q)python3 scripts/gen-hart-dts.py $@ $(SMP) $(CLOCK_FREQ)
211219

212220
minimal.dtb: minimal.dts riscv-harts.dtsi
@@ -216,6 +224,9 @@ minimal.dtb: minimal.dts riscv-harts.dtsi
216224
$(subst ^,$S,$(filter -D^SEMU_FEATURE_%, $(subst -D$(S)SEMU_FEATURE,-D^SEMU_FEATURE,$(CFLAGS)))) $< \
217225
| $(DTC) - > $@
218226

227+
.PHONY: FORCE
228+
FORCE:
229+
219230
# Rules for downloading prebuilt Linux kernel image
220231
include mk/external.mk
221232

@@ -245,6 +256,7 @@ clean:
245256
distclean: clean
246257
$(Q)$(RM) riscv-harts.dtsi
247258
$(Q)$(RM) minimal.dtb
259+
$(Q)$(RM) .smp_stamp
248260
$(Q)$(RM) Image rootfs.cpio
249261
$(Q)$(RM) ext4.img
250262

scripts/verify-dtb.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
# Verify that DTB CPU count matches expected count
3+
4+
DTB_FILE="${1}"
5+
EXPECTED_COUNT="${2}"
6+
7+
if [ -z "$DTB_FILE" ] || [ -z "$EXPECTED_COUNT" ]; then
8+
echo "Usage: $0 <dtb_file> <expected_cpu_count>"
9+
exit 1
10+
fi
11+
12+
if [ ! -f "$DTB_FILE" ]; then
13+
echo "Error: DTB file '$DTB_FILE' not found"
14+
exit 1
15+
fi
16+
17+
# Count CPUs in DTB using dtc
18+
DTC=$(which dtc 2>/dev/null)
19+
if [ -z "$DTC" ]; then
20+
echo "Warning: dtc not found, skipping DTB verification"
21+
exit 0
22+
fi
23+
24+
CPU_COUNT=$($DTC -I dtb -O dts "$DTB_FILE" 2>/dev/null | grep -c "cpu@")
25+
26+
if [ "$CPU_COUNT" -ne "$EXPECTED_COUNT" ]; then
27+
echo "========================================="
28+
echo "DTB Configuration Mismatch Detected!"
29+
echo "========================================="
30+
echo "DTB file '$DTB_FILE' contains $CPU_COUNT CPU(s)"
31+
echo "But you requested $EXPECTED_COUNT CPU(s)"
32+
echo ""
33+
echo "Solution:"
34+
echo " make SMP=$EXPECTED_COUNT riscv-harts.dtsi minimal.dtb"
35+
echo ""
36+
echo "This will regenerate the DTB with correct CPU count."
37+
echo "========================================="
38+
exit 1
39+
fi
40+
41+
exit 0

0 commit comments

Comments
 (0)