본문으로 건너뛰기
Bootloader Internals · 7/37

U-Boot Driver Model 내부 — uclass·driver·device 추상화 구조

· Hawk · 6분 읽기

#한 줄 요약

U-Boot Driver Model은 uclass(같은 인터페이스의 장치 묶음), driver(코드), device(struct udevice, 드라이버의 인스턴스) 셋으로 장치를 다룹니다. 장치가 생기는 단계(bind)와 하드웨어를 깨우는 단계(probe)는 따로 있고, U-Boot는 probe를 쓰일 때까지 미룹니다. QEMU의 qemu_arm64에서 dm tree를 찍어 보면 장치 대부분이 bind만 된 상태입니다. NVMe는 nvme scan을 한 뒤에야 [ + ]가 되고 그 아래 블록 장치가 생깁니다. 이 장은 그 차이를 dm 명령과 실제 드라이버 선언으로 따라갑니다.

#세 요소

U-Boot v2024.04 doc/develop/driver-model/design.rst의 정의입니다.

요소문서의 정의
Uclass“a group of devices which operate in the same way” — 같은 인터페이스로 접근
Driver“some code which talks to a peripheral and presents a higher-level interface to it”
Device“an instance of a driver, tied to a particular port or peripheral”

QEMU U-Boot는 부팅 배너에서 이미 규모를 알려 줍니다.

Core: 51 devices, 14 uclasses, devicetree: board

dm uclass serial은 serial uclass에 속한 장치를 보여 줍니다.

=> dm uclass serial
uclass 108: serial
0 * pl011@9000000 @ 7e698d10, seq 0

장치 하나(pl011@9000000)가 seq 0으로 있고, *는 probe(activate)된 장치라는 표시입니다(drivers/core/dump.c의 dm_display_line()이 DM_FLAG_ACTIVATED일 때 *를 찍음). @ 7e698d10은 relocation 뒤 RAM에 있는 struct udevice의 주소입니다.

uclass 하나에 MMC controller instance 두 개가 소속되고 두 instance가 같은 driver를 쓰는 관계, 그리고 DT node의 compatible이 bind와 probe를 거쳐 사용 가능한 device가 되는 순서

#드라이버 선언 — PL011 UART

위 장치를 만든 드라이버는 drivers/serial/serial_pl01x.c에 있습니다.

static const struct udevice_id pl01x_serial_id[] ={
{.compatible = "arm,pl011", .data = TYPE_PL011},
{.compatible = "arm,pl010", .data = TYPE_PL010},
{}
};
U_BOOT_DRIVER(serial_pl01x) = {
.name = "serial_pl01x",
.id = UCLASS_SERIAL,
#if CONFIG_IS_ENABLED(OF_REAL)
.of_match = of_match_ptr(pl01x_serial_id),
.of_to_plat = of_match_ptr(pl01x_serial_of_to_plat),
#endif
.plat_auto = sizeof(struct pl01x_serial_plat),
.probe = pl01x_serial_probe,
.ops = &pl01x_serial_ops,
.flags = DM_FLAG_PRE_RELOC,
.priv_auto = sizeof(struct pl01x_priv),
};
필드이 드라이버에서뜻
.idUCLASS_SERIAL소속 uclass
.of_matcharm,pl011, arm,pl010DT compatible과 맞출 문자열, .data로 변종 구분
.of_to_platpl01x_serial_of_to_platDT 속성을 plat 구조체로 옮김 (probe 직전)
.probepl01x_serial_probe하드웨어 초기화
.opspl01x_serial_opsuclass가 부르는 함수 표 (putc, getc 등)
.flagsDM_FLAG_PRE_RELOCrelocation 전에도 bind
*_auto구조체 크기driver model이 대신 할당

이 드라이버와 짝이 되는 DT 노드는 QEMU가 만든 control DTB에 있습니다.

=> fdt addr $fdtcontroladdr
Working FDT set to 7e596d90
=> fdt print /pl011@9000000
pl011@9000000 {
clock-names = "uartclk", "apb_pclk";
clocks = <0x00008000 0x00008000>;
interrupts = <0x00000000 0x00000001 0x00000004>;
reg = <0x00000000 0x09000000 0x00000000 0x00001000>;
compatible = "arm,pl011", "arm,primecell";
};

compatible의 첫 문자열 arm,pl011이 .of_match와 맞습니다. dm compat는 빌드에 들어간 드라이버마다 이 표를 보여 줍니다. 이 빌드에서는 드라이버 52개가 나왔고, 그중 PL011 줄은 이렇습니다.

serial_pl01x arm,pl011

#DT가 없는 bind — PCI 장치

모든 장치가 DT compatible로 bind되는 것은 아닙니다. dm compat에서 nvme 줄은 compatible이 비어 있습니다. NVMe 드라이버(drivers/nvme/nvme_pci.c)는 DT 대신 PCI class 코드로 짝을 찾습니다.

U_BOOT_DRIVER(nvme) = {
.name = "nvme",
.id = UCLASS_NVME,
.bind = nvme_bind,
.probe = nvme_probe,
.priv_auto = sizeof(struct nvme_dev),
};
struct pci_device_id nvme_supported[] = {
{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, ~0) },
{}
};
U_BOOT_PCI_DEVICE(nvme, nvme_supported);

PCI bus를 scan해서 class가 NVMe인 장치를 찾으면 이 드라이버가 bind됩니다(Ch 34). design.rst의 “Bind stage”도 발견 방법을 둘로 적습니다. U_BOOT_DRVINFO() 정의를 훑는 방법과, DT 노드의 compatible을 of_match 표로 찾는 방법입니다. PCI처럼 버스가 직접 찾아 bind하는 경로는 그 버스 uclass가 따로 갖습니다.

#bind와 probe는 따로 일어난다

QEMU에 NVMe 장치 하나를 붙이고 부팅 직후 dm tree를 찍었습니다(virtio-mmio 32개 줄 생략).

=> dm tree
Class Index Probed Driver Name
-----------------------------------------------------------
root 0 [ + ] root_driver root_driver
rsa_mod_ex 0 [ ] mod_exp_sw |-- mod_exp_sw
firmware 0 [ ] psci |-- psci
sysreset 0 [ ] psci-sysreset | `-- psci-sysreset
simple_bus 0 [ ] simple_bus |-- platform-bus@c000000
qfw 0 [ + ] qfw_mmio |-- fw-cfg@9020000
bootdev 0 [ ] qfw_bootdev | `-- fw-cfg@9020000.bootdev
pci 0 [ + ] pci_generic_ecam |-- pcie@10000000
pci_generi 0 [ ] pci_generic_drv | |-- pci_0:0.0
nvme 0 [ ] nvme | `-- nvme#0
rtc 0 [ ] rtc-pl031 |-- pl031@9010000
serial 0 [ + ] serial_pl01x |-- pl011@9000000
mtd 0 [ + ] cfi_flash |-- flash@0
bootstd 0 [ ] bootstd_drv `-- bootstd
...

[ + ]가 probe된 장치이고 [ ]는 bind만 된 장치입니다. 콘솔(PL011), 환경 변수를 읽은 flash(CFI), PCI 버스, fw-cfg는 이미 쓰였으니 probe됐습니다. RTC, PSCI, NVMe는 아직 누구도 쓰지 않아서 struct udevice만 있습니다. nvme scan을 한 뒤 같은 줄은 이렇게 바뀝니다.

=> nvme scan
=> dm tree
nvme 0 [ + ] nvme | `-- nvme#0
blk 0 [ + ] nvme-blk | |-- nvme#0.blk#1
bootdev 1 [ ] nvme_bootdev | `-- nvme#0.blk#1.bootdev

NVMe가 probe되자 그 아래 블록 장치(nvme#0.blk#1)와 bootdev가 새로 생겼습니다. design.rst의 “Activation/probe” 절이 이 동작을 설명합니다. “To save resources devices in U-Boot are probed lazily. U-Boot is a bootloader, not an operating system.” 대부분의 장치는 U-Boot가 한 번 도는 동안 한 번도 쓰이지 않기 때문입니다.

bind 단계에서 드라이버의 bind()가 할 수 있는 일도 정해져 있습니다. 같은 문서는 bind()가 “should not scan the device tree node, not initialise hardware, nor set up structures or allocate memory”라고 적습니다. DT 속성을 읽는 것은 of_to_plat(), 하드웨어 초기화는 probe()의 일입니다. 바로 probe하고 싶은 드라이버는 DM_FLAG_PROBE_AFTER_BIND(include/dm/device.h, 1 << 15)를 쓸 수 있습니다.

#relocation 전과 SPL

relocation 전에는 메모리가 적어서 모든 장치를 bind하지 않습니다. design.rst의 “Pre-Relocation Support” 절입니다.

Only drivers marked with DM_FLAG_PRE_RELOC or the device tree ‘bootph-all’ property are initialised prior to relocation.

PL011 드라이버가 DM_FLAG_PRE_RELOC를 가진 것은 relocation 전부터 콘솔이 필요하기 때문입니다. 같은 문서는 두 방법의 차이도 적습니다. 플래그를 쓰면 그 드라이버를 쓰는 장치가 모두 bind되어 메모리를 더 쓰고, DT의 bootph-* 속성은 노드 단위로 고를 수 있습니다. DT 없이 U_BOOT_DRVINFO()로 선언한 장치는 이 플래그가 유일한 방법입니다.

SPL에서는 이 선택이 DT 자체에 반영됩니다. Ch 6에서 본 대로 imx8mm SPL의 DTB에는 bootph-pre-ram·bootph-all이 붙은 노드와 그 부모, /chosen을 합쳐 42개만 남습니다. 거기 없는 장치는 SPL의 dm tree에 나타날 수 없습니다.

#새 드라이버를 붙일 때의 확인 순서

증상확인이 장의 도구
dm tree에 장치가 없음드라이버가 빌드에 들어갔나, compatible이 .of_match와 맞나, 노드 status가 okay인가dm compat, dm drivers, fdt print
장치는 있는데 [ ]아직 아무도 안 썼을 수 있음. 해당 서브시스템 명령으로 사용nvme scan처럼 uclass 명령
probe가 실패clock·reset·pinctrl 등 의존 장치, of_to_plat에서 읽는 속성드라이버의 probe 반환값, 로그
relocation 전·SPL에서 없음DM_FLAG_PRE_RELOC 또는 bootph-*SPL DTB를 dtc로 확인

dm drivers는 드라이버마다 uclass와 그 드라이버를 쓰는 장치를 보여 줍니다. 이 빌드에서는 머리줄을 빼고 83줄이었고(같은 드라이버의 장치가 여럿이면 줄이 늘어남), 장치가 <none>인 드라이버는 빌드에 들어갔지만 짝을 찾지 못한 것입니다.

nvme 078 nvme nvme#0
nvme-blk 022 blk <none>
serial_pl01x 108 serial pl011@9000000

(위 출력은 nvme scan 전. 일부 줄만 발췌)

#정리

  • uclass는 같은 인터페이스의 장치 묶음, driver는 코드, device는 드라이버의 인스턴스입니다. QEMU 빌드에는 장치 51개, uclass 14개가 있었습니다.
  • DT 장치는 compatible과 .of_match로, PCI 장치는 U_BOOT_PCI_DEVICE의 class 코드로 bind됩니다.
  • probe는 쓰일 때 일어납니다. NVMe는 nvme scan 전까지 bind만 된 상태였고, probe 뒤에 블록 장치가 생겼습니다.
  • relocation 전과 SPL에서는 DM_FLAG_PRE_RELOC 드라이버나 bootph-* 노드만 bind됩니다.

#다음 편

Ch 8: 보드 초기화 — board_init_f와 board_init_r에서는 relocation 전후의 초기화와 보드 hook을 살펴봅니다.

#근거 자료

  • U-Boot v2024.04 Driver Model design (doc/develop/driver-model/design.rst): 용어 정의, Bind stage, Activation/probe, Pre-Relocation Support.
  • U-Boot v2024.04 소스: drivers/serial/serial_pl01x.c, drivers/nvme/nvme_pci.c, include/dm/device.h(DM_FLAG_PRE_RELOC, DM_FLAG_PROBE_AFTER_BIND), drivers/core/dump.c.
  • 이 글의 실행: QEMU 11.1.3 qemu_arm64에 NVMe 하나를 붙이고 dm uclass·dm compat·dm drivers·dm tree·nvme scan·fdt print.

#관련 항목

Bootloader Internals · 7 of 37 전체 목차
  1. 1 ROM부터 init까지 — 부트로더가 채우는 부팅 단계
  2. 2 Das U-Boot vs TF-A vs EDK II — 임베디드 부트로더 생태계 비교
  3. 3 U-Boot 빌드 시스템 분석 — Kconfig·Makefile·defconfig 동작 추적
  4. 4 Arm 임베디드 부트 단계 — TF-A BL1·BL2·BL31·BL33과 U-Boot SPL/TPL
  5. 5 U-Boot Falcon Mode — SPL이 U-Boot Proper 없이 커널 직접 부팅
  6. 6 Device Tree DTB 부트로더 처리 — 로딩 시점과 fixup 메커니즘 추적
  7. 7 U-Boot Driver Model 내부 — uclass·driver·device 추상화 구조
  8. 8 U-Boot 보드 초기화 시퀀스 — board_init_f와 board_init_r 분리 이유
  9. 9 DDR Controller 프로그래밍과 PHY Training — SPL의 가장 어려운 작업
  10. 10 임베디드 스토리지 부팅 분석 — MMC·SCSI·NAND·SPI Flash 비교
  11. 11 임베디드 네트워크 부팅 — TFTP·PXE·BOOTP 시퀀스 분석
  12. 12 U-Boot USB 부팅 — fastboot·UMS·USB host 메커니즘
  13. 13 U-Boot 환경 변수와 bootcmd — 부팅 시나리오 정의하기
  14. 14 Modern U-Boot bootflow / bootmeth — 새 추상화 레이어 분석
  15. 15 FIT image 구조 분석 — multi-image·hash·configuration 추적
  16. 16 U-Boot Verified Boot — RSA 서명과 public key 임베딩 흐름
  17. 17 임베디드 A/B 부팅 이중화 — OTA 안전성을 위한 부트 슬롯 설계
  18. 18 U-Boot의 EFI 호환 분석 — bootefi 명령과 EFI loader 동작 원리
  19. 19 Linux Kernel Boot ABI — ARM, RISC-V, x86 진입 규약
  20. 20 임베디드 펌웨어 업데이트 — RAUC vs SWUpdate 비교
  21. 21 새 보드 U-Boot 포팅 실전 — defconfig 작성부터 첫 부팅까지
  22. 22 부트로더 디버깅 기법 — DEBUG·JTAG·serial·post-mortem 분석
  23. 23 SoC BootROM·eFuse·OTP — 부팅의 0단계 분석
  24. 24 SPL·TPL 내부 해부 — 가장 작은 부트 단계의 동작 추적
  25. 25 ARM Trusted Firmware-A 통합 — BL1·BL2·BL31·BL32·BL33 흐름
  26. 26 DDR training 깊이 — PHY·컨트롤러·보드 파라미터 검증
  27. 27 임베디드 Chain of Trust — 다단계 서명 검증의 전체 흐름
  28. 28 임베디드 Flash Layout 설계 — partition·NAND·eMMC·UBI 비교
  29. 29 U-Boot Distro Boot — extlinux·boot.scr 표준화 분석
  30. 30 부트로더 CI 구축 — build matrix와 자동 부팅 테스트
  31. 31 TF-A BL31 EL3 Runtime — 서비스 등록과 요청 처리 경계
  32. 32 PSCI와 SMCCC ABI — ARM 표준 SMC 호출 규약 분석
  33. 33 ARM64 Secondary Core Bring-up — PSCI와 spin-table 시작 계약
  34. 34 U-Boot PCIe Enumeration — 장치 발견·BAR 할당과 CXL 메모리의 구분
  35. 35 EFI·UEFI에서 CXL 초기화 — CEDT 인계와 HDM Decoder 설정의 경계
  36. 36 부트 시 메모리 토폴로지 — Firmware 정보와 Linux NUMA·Tier·Zone의 경계
  37. 37 UEFI Secure Boot 인증서 만료 — 2011→2023 CA 롤오버와 PQC 대비