Barebox · source analysis

Device Tree population과 deep probe

일반 DT population의 선택적 device 생성과 요청 node의 on-demand probe 확인 경계를 읽습니다.

Series
08 / 19
Baseline
v2026.06.1
Commit
314c679eea2d
Source
drivers/of/platform.c:397-485 (발췌 범위)

Device Tree population과 deep probe 단계에서 실제로 바뀌는 상태는 무엇인가?

DT node의 존재, device 등록, driver probe 확인을 구분한다. of_platform_populate의 child walk와 of_device_ensure_probed의 요청 node 처리는 서로 다른 진입 경로다.

일반 population의 0 반환을 모든 child의 등록·probe 완료로 해석하지 않는다. on-demand 경로도 deep probe가 지원되지 않으면 작업 없이 0을 반환한다.

객체와 주소가 놓이는 구조

그림 1. Device Tree population과 deep probe에서 입력, 내부 상태, 출력의 경계
입력과 전제내부 상태외부로 공개되는 결과
01device_node tree
02platform devices
03aliases/resources
04probe policy

입력 node, 생성 가능한 device, 선택적 resource, probe 확인 상태를 역할별로 놓았다. 이 그림은 네 블록을 모두 지나는 직렬 실행 순서가 아니다.

서로 다른 두 진입 경로

그림 2. 일반 population과 요청 node의 deep probe는 직렬 필수 단계가 아니다

일반: of_platform_populate

  1. root가 없으면 /를 찾고, 없으면 -EINVAL.
  2. 직계 child마다 of_platform_bus_create 호출.
  3. compatible·available·AMBA·bus match 조건에 따라 생성·재귀 또는 건너뜀.

결과는 walk의 rc. 모든 node의 device 또는 driver probe 완료를 보장하지 않는다.

요청 시: of_device_ensure_probed(np)

  1. node가 없거나 deep probe 미지원이면 작업 없이 0.
  2. 필요 시 parent rescan·재귀 생성 후 요청 node 재확인.
  3. device와 dev->driver 확인: 없으면 -EPROBE_DEFER.

요청 node의 성공 여부를 확인하는 별도 호출이다. 일반 walk 뒤에 항상 자동 실행되지 않는다.

platform.c 380–515행: AMBA 장치는 별도 생성 분기이며, 일반 child 재귀는 생성된 device와 bus match가 모두 있을 때만 진행한다.

경로별 조건과 반환 상태

두 함수의 행은 한 번에 모두 통과하는 직렬 목록이 아니다. 일반 walk의 반환값과 요청 node의 probe 확인 결과를 따로 읽는다.

#경로·조건실제 단계남는 상태 / 반환다음 확인
A1일반 · root NULLof_find_node_by_path("/")root도 없으면 -EINVALroot node 존재
A2일반 · 각 직계 childof_platform_bus_createcompatible 없으면 해당 subtree를 건너뜀node compatible
A3일반 · 생성 가능AMBA 분기 또는 of_platform_device_createavailable·기존 np->dev·resource·등록 조건 반영device / resource
A4일반 · device와 bus matchchild 재귀; 아니면 종료walk rc 반환; probe 보장 아님실제 방문 범위
B1요청 · NULL 또는 deep 미지원of_device_ensure_probed guard작업 없이 0; probe 완료 증거 아님deep_probe_is_supported
B2요청 · 지원됨parent rescan / 필요한 parent 재귀 생성기존 np->dev 재사용 또는 device 생성 시도parent와 compatible
B3요청 · device 확인dev->driver 검사device·driver가 있으면 0, 아니면 -EPROBE_DEFERdriver bind·probe 결과

고정 v2026.06.1 구현에서 일반 population은 선택적 subtree walk이고 deep probe는 요청 시 검증이다.

일반 walk와 on-demand 생성의 경계

각 카드의 번호는 해당 함수 안의 순서다. 그림 3의 끝이 그림 4의 시작을 뜻하지 않는다.

그림 3. of_platform_populate의 선택적 child walk

직계 child에서 시작하고, descendant 재귀는 bus match와 생성 결과에 따라 달라진다.

01 · ROOTroot 또는 / 조회

root가 없으면 -EINVAL.

02 · CHILD직계 child마다 bus create

compatible이 없으면 그 subtree를 건너뛴다.

03 · DEVICEAMBA 분기 또는 platform device

available, 기존 np->dev, resource와 등록 결과를 반영한다.

04 · RECURSEdevice + bus match일 때 child 재귀

그 외에는 해당 node에서 멈춘다. driver probe를 일괄 강제하지 않는다.

그림 4. of_device_ensure_probed의 요청 node 경로

요청이 있고 deep probe가 지원될 때 parent를 필요에 따라 준비한 뒤 해당 device의 driver를 확인한다.

01 · GUARDNULL / 미지원 → 0

이 반환은 device가 probe되었다는 뜻이 아니다.

02 · PARENT필요 시 parent rescan 또는 재귀 생성

parent 준비 뒤 요청 node가 이미 생겼으면 재사용한다.

03 · REQUESTcompatible 확인 후 device 생성

필요하면 AMBA 또는 platform device를 만든다. 실패는 defer.

04 · RESULTdev->driver 검사

있으면 0, device·driver가 없으면 -EPROBE_DEFER.

원본 코드

아래 코드는 Barebox v2026.06.1의 drivers/of/platform.c에서 397-485줄을 그대로 가져온 것이다.

commit314c679eea2d0ca847c64d1b646a1cf91225c0ecanchor · L421int of_platform_populate
397		return 0;
398
399	for_each_child_of_node(node, child) {
400		pr_debug("   create child: %pOF\n", child);
401		rc = of_platform_bus_create(child, matches, dev);
402		if (rc)
403			break;
404	}
405	return rc;
406}
407
408/**
409 * of_platform_populate() - Populate platform_devices from device tree data
410 * @root: parent of the first level to probe or NULL for the root of the tree
411 * @matches: match table, NULL to use the default
412 * @parent: parent to hook devices from, NULL for toplevel
413 *
414 * This function walks the device tree given by @root node and creates devices
415 * from nodes.  It requires all device nodes to have a 'compatible' property,
416 * and it is suitable for creating devices which are children of the root
417 * node.
418 *
419 * Returns 0 on success, < 0 on failure.
420 */
421int of_platform_populate(struct device_node *root,
422			const struct of_device_id *matches,
423			struct device *parent)
424{
425	struct device_node *child;
426	int rc = 0;
427
428	if (!root)
429		root = of_find_node_by_path("/");
430	if (!root)
431		return -EINVAL;
432
433	for_each_child_of_node(root, child) {
434		rc = of_platform_bus_create(child, matches, parent);
435		if (rc)
436			break;
437	}
438
439	return rc;
440}
441EXPORT_SYMBOL_GPL(of_platform_populate);
442
443static struct device *of_device_create_on_demand(struct device_node *np)
444{
445	struct device_node *parent;
446	struct device *parent_dev, *dev;
447
448	parent = of_get_parent(np);
449	if (!parent)
450		return NULL;
451
452	if (!np->dev && parent->dev)
453		device_rescan(parent->dev);
454
455	/* Create all parent devices needed for the requested device */
456	parent_dev = parent->dev ? : of_device_create_on_demand(parent);
457	if (IS_ERR(parent_dev))
458		return parent_dev;
459
460	/*
461	 * Parent devices like i2c/spi controllers are populating their own
462	 * devices. So it can be that the requested device already exists after
463	 * the parent device creation.
464	 */
465	if (np->dev)
466		return np->dev;
467
468	if (!of_property_present(np, "compatible"))
469		return NULL;
470
471	pr_debug("Creating device for %pOF\n", np);
472
473	if (of_device_is_compatible(np, "arm,primecell"))
474		dev = of_amba_device_create(np);
475	else
476		dev = of_platform_device_create(np, parent_dev);
477
478	return dev ? : ERR_PTR(-EPROBE_DEFER);
479}
480
481/**
482 * of_device_ensure_probed() - ensures that a device is probed
483 *
484 * @np: the device_node handle which should be probed
485 *

발췌 범위와 읽기 기준

원문 397-485줄에는 앞뒤 문맥이 포함됩니다. 기준 구문 int of_platform_populate은 L421에 있습니다. 발췌 첫 줄을 함수 정의의 시작으로 해석하지 않습니다.

원문 발췌는 485행까지다. 요청 node의 최종 dev->driver 검사와 반환 조건은 같은 버전의 487–515행에서 이어진다.

함께 읽어야 하는 원본 코드

첫 코드 조각만으로 동작이 완성되지 않는 경우 호출 매크로, 객체 정의와 실제 실행 목록을 같은 페이지에 묶었다. 각 조각은 같은 기준 commit에서 가져왔다.

01. DT node에서 resource를 읽어 device를 만드는 함수

drivers/of/platform.c 114-214줄이다.

filedrivers/of/platform.canchor · L124struct device *of_platform_device_create
114}
115
116/**
117 * of_platform_device_create - Alloc, initialize and register an of_device
118 * @np: pointer to node to create device for
119 * @parent: device model parent device.
120 *
121 * Returns pointer to created platform device, or NULL if a device was not
122 * registered. Unavailable devices will not get registered.
123 */
124struct device *of_platform_device_create(struct device_node *np,
125						struct device *parent)
126{
127	struct device *dev;
128	struct resource *res = NULL, temp_res;
129	resource_size_t resinval;
130	int i, ret, num_reg = 0;
131	u32 virt;
132
133	if (!of_device_is_available(np))
134		return NULL;
135
136	/*
137	 * Linux uses the OF_POPULATED flag to skip already populated/created
138	 * devices.
139	 */
140	if (np->dev) {
141		device_rescan(np->dev);
142		return np->dev;
143	}
144
145	/* count the io resources */
146	if (of_can_translate_address(np))
147		while (of_address_to_resource(np, num_reg, &temp_res) == 0)
148			num_reg++;
149
150	/* Populate the resource table */
151	if (num_reg) {
152		res = xzalloc(sizeof(*res) * num_reg);
153		for (i = 0; i < num_reg; i++) {
154			ret = of_address_to_resource(np, i, &res[i]);
155			if (ret) {
156				free(res);
157				return NULL;
158			}
159		}
160	}
161
162	/* setup generic device info */
163	dev = xzalloc(sizeof(*dev));
164	dev->id = DEVICE_ID_SINGLE;
165	dev->of_node = np;
166	dev->parent = parent;
167	dev->resource = res;
168	dev->num_resources = num_reg;
169	of_device_make_bus_id(dev);
170
171	of_dma_configure(dev, np);
172
173	if (num_reg && !of_property_read_u32(np, "virtual-reg", &virt)) {
174		resource_size_t remap_offset = virt - res[0].start;
175
176		for (i = 0; i < num_reg; i++) {
177			void *new_virt = (void *)res[i].start + remap_offset;
178			resource_size_t size = resource_size(&res[i]);
179
180			ret = arch_remap_range(new_virt, res[i].start, size, MAP_UNCACHED);
181			if (!ret) {
182				debug("%s: remap device %s resource %d: %pa -> 0x%p\n",
183				      __func__, dev_name(dev), i, &res[i].start, new_virt);
184
185				res[i].start = (resource_size_t)new_virt;
186				res[i].end = res[i].start + size - 1;
187			}
188		}
189	}
190
191	resinval = (-1);
192
193	debug("%s: register device %s, io=%pa\n",
194			__func__, dev_name(dev),
195		(num_reg) ? &dev->resource[0].start : &resinval);
196
197	BUG_ON(np->dev);
198	np->dev = dev;
199
200	ret = platform_device_register(dev);
201	if (ret) {
202		np->dev = NULL;
203		free_device(dev);
204		if (num_reg)
205			free(res);
206		return NULL;
207	}
208
209
210	return dev;
211}
212
213struct driver dummy_driver = {
214	.name = "dummy-driver",

조건별 상태를 소스에서 읽기

01

두 API의 진입 조건을 분리한다

of_platform_populate는 root가 NULL이면 /를 찾아 child walk를 시작한다. of_device_ensure_probed는 요청 node가 없거나 deep probe가 지원되지 않으면 즉시 0을 반환한다.

후자의 0 반환만으로 driver probe 완료를 판정하지 않는다.

02

node와 device의 생성 시점을 구분한다

일반 walk는 compatible이 없는 node를 건너뛰고, of_platform_device_create는 unavailable node를 등록하지 않는다. 기존 np->dev가 있으면 rescan 뒤 재사용한다.

on-demand 경로는 필요 시 parent rescan·재귀 생성 뒤 요청 node의 device가 이미 생겼는지 다시 확인한다.

03

resource는 생성 가능한 node에서만 채운다

of_can_translate_address인 node에서 of_address_to_resource로 I/O resource 개수를 세고 배열을 채운다. virtual-reg가 있으면 remap을 시도한다.

주소 translation 또는 등록 실패 시 해당 함수의 cleanup 경로를 확인한다.

04

등록과 probe 확인은 다른 결과다

일반 population은 device 생성 뒤 bus match가 있을 때만 child를 재귀 방문하고 walk의 rc를 반환한다. 이 반환값은 모든 descendant의 등록이나 driver probe 완료 증거가 아니다.

요청 node 경로는 device 생성·재사용 뒤 dev->driver가 없으면 -EPROBE_DEFER를 반환한다.

05

실패와 건너뜀을 구별한다

일반 경로에서 root가 없으면 -EINVAL이지만 compatible이 없거나 device 생성 결과가 NULL인 node는 그 subtree에서 멈추고 다른 child walk를 계속할 수 있다.

on-demand 경로에서 요청 device 생성 실패 또는 driver 부재는 -EPROBE_DEFER다. 설정상 deep probe 미지원은 오류가 아니라 조기 0 반환이다.

조건 분기에서 놓치기 쉬운 부분

01

compatible과 bus match는 서로 다른 관문이다

compatible이 없는 node는 of_platform_bus_create가 건너뛴다. device가 만들어져도 of_match_node(matches, node)가 맞지 않으면 child 재귀는 없다.

AMBA 장치 생성 성공도 별도 조기 반환 경로다. 소스 365–406행.

02

resource는 선택적으로 생성되고 정리된다

주소 translation 가능 여부와 reg 항목 수가 resource 배열을 결정한다. 등록 전 np->dev를 지정하지만 등록 실패 시 NULL로 되돌리고 device와 resource를 정리한다.

실제 node의 resource와 devinfo·iomem을 대조한다. 소스 129–209행.

03

deep probe는 필요한 parent와 요청 device를 확인한다

np->dev가 없고 parent device가 있을 때 device_rescan(parent->dev)을 호출한다. parent device가 없으면 재귀 생성한다. parent controller가 요청 child를 이미 만들 수 있으므로 np->dev를 다시 검사한다.

최종 dev->driver 검사에 실패하면 defer한다. 첫 접근 시간과 probe 수는 이 조건의 선택 결과로 측정한다. 소스 443–515행.

node·device·driver의 상태

대상처리 시점코드가 확인하는 상태근거
device_node *np두 API 진입 전 DT에 존재compatible·available·parent 참조bus create
np->devdevice 생성·등록 또는 기존 device rescan등록 실패 시 NULL로 복구; on-demand에서 재사용 검사device create
resource 배열주소 translation 가능하고 resource가 있으면 생성num_reg와 resource; 등록 실패 시 해제resource path
parent_devon-demand에서 필요 시 parent rescan·재귀 생성요청 child가 이미 생성됐는지 확인parent path
dev->driver요청 device 생성·재사용 뒤있으면 성공 0; 없으면 -EPROBE_DEFERensure probed

건너뜀·오류·조기 반환

#조건소스의 결과먼저 확인할 곳
01일반 경로에서 root도 /도 없음-EINVALof_platform_populate
02compatible 없음 / unavailable일반 walk에서 해당 node·subtree를 건너뜀of_platform_bus_create·device create
03device는 있지만 bus match 없음해당 child 재귀 없음; 다른 root child는 계속 처리 가능of_match_node
04요청 device 생성 실패 또는 driver 없음-EPROBE_DEFERof_device_ensure_probed
05deep probe 미지원 / 요청 node NULL작업 없이 0; probe 완료 증거 아님deep_probe_is_supported

일반 population의 0은 순회된 범위의 오류 여부를 말하고, deep probe 경로의 0도 guard에 의해 작업 없이 반환될 수 있다. 호출 조건과 np->dev·dev->driver를 함께 기록한다.

소스 밖에서 확인할 증거

소스에서 파악한 동작을 실제 빌드 결과와 대상 보드의 로그로 확인한다. 아래 명령의 보드 이름과 툴체인 접두사는 사용 중인 빌드 환경에 맞게 바꾼다.

#목적명령 또는 계측판정 기준
01빌드 구성make <board>_defconfig && make V=1Kconfig, linker section과 최종 barebox image 생성 명령을 보관한다.
02symbol과 section${CROSS_COMPILE}nm -n barebox | grep -E 'of_platform_populate|of_device_ensure_probed'두 API의 symbol이 대상 빌드에 포함됐는지 확인한다.
03주소와 resourceiomem생성된 device의 translated resource와 실제 MMIO 범위를 확인한다.
04device 상태devinfo; drvinfo; of_dumpDT node 생성, device 등록, driver probe를 서로 구분한다.
05요청 경로 반환값GDB/JTAG: of_device_ensure_probed(np)deep probe 지원 여부, parent·요청 device 상태와 반환값을 기록한다.

직접 확인할 실험

  1. 01
    같은 node의 DT·device 상태 비교

    of_platform_populate 전후 같은 child path의 np->dev와 compatible·bus match를 비교한다.

  2. 02
    요청 node의 deep probe 반환값 비교

    of_device_ensure_probed 호출 전후 요청 node와 parent의 device·driver 상태 및 반환값을 기록한다.

  3. 03
    ranges가 있는 bus의 translated resource 확인

    대표 bus child의 of_address_to_resource 결과와 등록된 device resource를 비교한다.

원문과 다음 글