xref: /optee_os/core/kernel/dt_driver.c (revision ad0ae8003583f7270d62568c85a32a5f25a601c6)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  * Copyright (c) 2021, Bootlin
5  * Copyright (c) 2021, Linaro Limited
6  * Copyright (c) 2021, STMicroelectronics
7  */
8 
9 #include <assert.h>
10 #include <config.h>
11 #include <initcall.h>
12 #include <kernel/boot.h>
13 #include <kernel/dt.h>
14 #include <kernel/dt_driver.h>
15 #include <libfdt.h>
16 #include <malloc.h>
17 #include <sys/queue.h>
18 #include <tee_api_defines_extensions.h>
19 #include <tee_api_types.h>
20 
21 /*
22  * struct dt_driver_probe - Node instance in secure FDT to probe a driver for
23  *
24  * @link: List hook
25  * @nodeoffset: Node offset of device referenced in the FDT
26  * @type: One of DT_DRIVER_* or DT_DRIVER_NOTYPE.
27  * @deferrals: Driver probe deferrals count
28  * @dt_drv: Matching driver to probe if found or NULL
29  * @dm: Matching reference if applicable or NULL
30  */
31 struct dt_driver_probe {
32 	int nodeoffset;
33 	enum dt_driver_type type;
34 	unsigned int deferrals;
35 	const struct dt_driver *dt_drv;
36 	const struct dt_device_match *dm;
37 	TAILQ_ENTRY(dt_driver_probe) link;
38 };
39 
40 /*
41  * struct dt_driver_provider - DT related info on probed device
42  *
43  * Saves information on the probed device so that device
44  * drivers can get resources from DT phandle and related arguments.
45  *
46  * @nodeoffset: Node offset of device referenced in the FDT
47  * @type: One of DT_DRIVER_* or DT_DRIVER_NOTYPE.
48  * @provider_cells: Cells count in the FDT used by the driver's references
49  * @get_of_device: Function to get driver's device ref from phandle data
50  * @priv_data: Driver private data passed as @get_of_device argument
51  * @link: Reference in DT driver providers list
52  */
53 struct dt_driver_provider {
54 	int nodeoffset;
55 	enum dt_driver_type type;
56 	unsigned int provider_cells;
57 	uint32_t phandle;
58 	get_of_device_func get_of_device;
59 	void *priv_data;
60 	SLIST_ENTRY(dt_driver_provider) link;
61 };
62 
63 /*
64  * Device driver providers are able to provide a driver specific instance
65  * related to device phandle arguments found in the secure embedded FDT.
66  */
67 static SLIST_HEAD(, dt_driver_provider) dt_driver_provider_list =
68 	SLIST_HEAD_INITIALIZER(dt_driver_provider_list);
69 
70 /* FDT nodes for which a matching driver is to be probed */
71 static TAILQ_HEAD(dt_driver_probe_head, dt_driver_probe) dt_driver_probe_list =
72 	TAILQ_HEAD_INITIALIZER(dt_driver_probe_list);
73 
74 /* FDT nodes for which a matching driver has been successfully probed */
75 static TAILQ_HEAD(, dt_driver_probe) dt_driver_ready_list =
76 	TAILQ_HEAD_INITIALIZER(dt_driver_ready_list);
77 
78 /* List of the nodes for which a compatible driver but reported a failure */
79 static TAILQ_HEAD(, dt_driver_probe) dt_driver_failed_list =
80 	TAILQ_HEAD_INITIALIZER(dt_driver_failed_list);
81 
82 /* Flag enabled when a new node (possibly typed) is added in the probe list */
83 static bool added_node;
84 
85 /* Resolve drivers dependencies on core crypto layer */
86 static bool tee_crypt_is_ready;
87 
88 void dt_driver_crypt_init_complete(void)
89 {
90 	assert(!tee_crypt_is_ready);
91 	tee_crypt_is_ready = true;
92 }
93 
94 TEE_Result dt_driver_get_crypto(void)
95 {
96 	if (tee_crypt_is_ready)
97 		return TEE_SUCCESS;
98 	else
99 		return TEE_ERROR_DEFER_DRIVER_INIT;
100 }
101 
102 static void assert_type_is_valid(enum dt_driver_type type)
103 {
104 	switch (type) {
105 	case DT_DRIVER_NOTYPE:
106 	case DT_DRIVER_CLK:
107 	case DT_DRIVER_RSTCTRL:
108 	case DT_DRIVER_UART:
109 		return;
110 	default:
111 		assert(0);
112 	}
113 }
114 
115 /*
116  * Driver provider registering API functions
117  */
118 
119 TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset,
120 				       get_of_device_func get_of_device,
121 				       void *priv, enum dt_driver_type type)
122 {
123 	struct dt_driver_provider *prv = NULL;
124 	int provider_cells = 0;
125 	uint32_t phandle = 0;
126 
127 	assert_type_is_valid(type);
128 
129 	provider_cells = fdt_get_dt_driver_cells(fdt, nodeoffset, type);
130 	if (provider_cells < 0) {
131 		DMSG("Failed to find provider cells: %d", provider_cells);
132 		return TEE_ERROR_GENERIC;
133 	}
134 
135 	phandle = fdt_get_phandle(fdt, nodeoffset);
136 	if (!phandle || phandle == (uint32_t)-1) {
137 		DMSG("Failed to find provide phandle");
138 		return TEE_ERROR_GENERIC;
139 	}
140 
141 	prv = calloc(1, sizeof(*prv));
142 	if (!prv)
143 		return TEE_ERROR_OUT_OF_MEMORY;
144 
145 	prv->nodeoffset = nodeoffset;
146 	prv->type = type;
147 	prv->provider_cells = provider_cells;
148 	prv->phandle = phandle;
149 	prv->get_of_device = get_of_device;
150 	prv->priv_data = priv;
151 
152 	SLIST_INSERT_HEAD(&dt_driver_provider_list, prv, link);
153 
154 	return TEE_SUCCESS;
155 }
156 
157 /*
158  * Helper functions for dt_drivers querying driver provider information
159  */
160 
161 int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset,
162 			    enum dt_driver_type type)
163 {
164 	const char *cells_name = NULL;
165 	const fdt32_t *c = NULL;
166 	int len = 0;
167 
168 	switch (type) {
169 	case DT_DRIVER_CLK:
170 		cells_name = "#clock-cells";
171 		break;
172 	case DT_DRIVER_RSTCTRL:
173 		cells_name = "#reset-cells";
174 		break;
175 	default:
176 		panic();
177 	}
178 
179 	c = fdt_getprop(fdt, nodeoffset, cells_name, &len);
180 	if (!c)
181 		return len;
182 
183 	if (len != sizeof(*c))
184 		return -FDT_ERR_BADNCELLS;
185 
186 	return fdt32_to_cpu(*c);
187 }
188 
189 unsigned int dt_driver_provider_cells(struct dt_driver_provider *prv)
190 {
191 	return prv->provider_cells;
192 }
193 
194 struct dt_driver_provider *
195 dt_driver_get_provider_by_node(int nodeoffset, enum dt_driver_type type)
196 {
197 	struct dt_driver_provider *prv = NULL;
198 
199 	SLIST_FOREACH(prv, &dt_driver_provider_list, link)
200 		if (prv->nodeoffset == nodeoffset && prv->type == type)
201 			return prv;
202 
203 	return NULL;
204 }
205 
206 struct dt_driver_provider *
207 dt_driver_get_provider_by_phandle(uint32_t phandle, enum dt_driver_type type)
208 {
209 	struct dt_driver_provider *prv = NULL;
210 
211 	SLIST_FOREACH(prv, &dt_driver_provider_list, link)
212 		if (prv->phandle == phandle && prv->type == type)
213 			return prv;
214 
215 	return NULL;
216 }
217 
218 static void *device_from_provider_prop(struct dt_driver_provider *prv,
219 					  const uint32_t *prop,
220 					  TEE_Result *res)
221 {
222 	struct dt_driver_phandle_args *pargs = NULL;
223 	unsigned int n = 0;
224 	void *device = NULL;
225 
226 	pargs = calloc(1, prv->provider_cells * sizeof(uint32_t *) +
227 		       sizeof(*pargs));
228 	if (!pargs) {
229 		*res = TEE_ERROR_OUT_OF_MEMORY;
230 		return NULL;
231 	}
232 
233 	pargs->args_count = prv->provider_cells;
234 	for (n = 0; n < prv->provider_cells; n++)
235 		pargs->args[n] = fdt32_to_cpu(prop[n + 1]);
236 
237 	device = prv->get_of_device(pargs, prv->priv_data, res);
238 
239 	free(pargs);
240 
241 	return device;
242 }
243 
244 void *dt_driver_device_from_node_idx_prop(const char *prop_name,
245 					  const void *fdt, int nodeoffset,
246 					  unsigned int prop_idx,
247 					  enum dt_driver_type type,
248 					  TEE_Result *res)
249 {
250 	int len = 0;
251 	int idx = 0;
252 	int idx32 = 0;
253 	int prv_cells = 0;
254 	uint32_t phandle = 0;
255 	const uint32_t *prop = NULL;
256 	struct dt_driver_provider *prv = NULL;
257 
258 	prop = fdt_getprop(fdt, nodeoffset, prop_name, &len);
259 	if (!prop) {
260 		DMSG("Property %s missing in node %s", prop_name,
261 		     fdt_get_name(fdt, nodeoffset, NULL));
262 		*res = TEE_ERROR_GENERIC;
263 		return NULL;
264 	}
265 
266 	while (idx < len) {
267 		idx32 = idx / sizeof(uint32_t);
268 		phandle = fdt32_to_cpu(prop[idx32]);
269 		if (!phandle) {
270 			if (!prop_idx)
271 				break;
272 			idx += sizeof(phandle);
273 			prop_idx--;
274 			continue;
275 		}
276 
277 		prv = dt_driver_get_provider_by_phandle(phandle, type);
278 		if (!prv) {
279 			/* No provider registered yet */
280 			*res = TEE_ERROR_DEFER_DRIVER_INIT;
281 			return NULL;
282 		}
283 
284 		prv_cells = dt_driver_provider_cells(prv);
285 		if (prop_idx) {
286 			prop_idx--;
287 			idx += sizeof(phandle) + prv_cells * sizeof(uint32_t);
288 			continue;
289 		}
290 
291 		return device_from_provider_prop(prv, prop + idx32, res);
292 	}
293 
294 	*res = TEE_ERROR_GENERIC;
295 	return NULL;
296 }
297 
298 static void __maybe_unused print_probe_list(const void *fdt __maybe_unused)
299 {
300 	struct dt_driver_probe *elt = NULL;
301 	unsigned int count = 0;
302 
303 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
304 		count++;
305 
306 	DMSG("Probe list: %u elements", count);
307 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
308 		DMSG("|- Driver %s probes on node %s",
309 		     elt->dt_drv->name,
310 		     fdt_get_name(fdt, elt->nodeoffset, NULL));
311 
312 	DMSG("`- Probe list end");
313 
314 	count = 0;
315 	TAILQ_FOREACH(elt, &dt_driver_failed_list, link)
316 		count++;
317 
318 	DMSG("Failed list: %u elements", count);
319 	TAILQ_FOREACH(elt, &dt_driver_failed_list, link)
320 		EMSG("|- Driver %s on node %s failed", elt->dt_drv->name,
321 		     fdt_get_name(fdt, elt->nodeoffset, NULL));
322 
323 	DMSG("`- Failed list end");
324 }
325 
326 /*
327  * Probe element: push to ready list if succeeds, push to probe list if probe
328  * if deferred, panic with an error trace otherwise.
329  */
330 static TEE_Result probe_driver_node(const void *fdt,
331 				    struct dt_driver_probe *elt)
332 {
333 	TEE_Result res = TEE_ERROR_GENERIC;
334 	const char __maybe_unused *drv_name = NULL;
335 	const char __maybe_unused *node_name = NULL;
336 
337 	node_name = fdt_get_name(fdt, elt->nodeoffset, NULL);
338 	drv_name = elt->dt_drv->name;
339 
340 	if (!elt->dt_drv->probe) {
341 		DMSG("No probe operator for driver %s, skipped", drv_name);
342 		return TEE_SUCCESS;
343 	}
344 
345 	FMSG("Probing %s on node %s", drv_name, node_name);
346 
347 	res = elt->dt_drv->probe(fdt, elt->nodeoffset, elt->dm->compat_data);
348 	switch (res) {
349 	case TEE_SUCCESS:
350 		TAILQ_INSERT_HEAD(&dt_driver_ready_list, elt, link);
351 
352 		DMSG("element: %s on node %s initialized", drv_name, node_name);
353 		break;
354 	case TEE_ERROR_DEFER_DRIVER_INIT:
355 		elt->deferrals++;
356 		TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link);
357 
358 		DMSG("element: %s on node %s deferred %u time(s)", drv_name,
359 		     node_name, elt->deferrals);
360 		break;
361 	case TEE_ERROR_NODE_DISABLED:
362 		DMSG("element: %s on node %s is disabled", drv_name, node_name);
363 		break;
364 	default:
365 		TAILQ_INSERT_HEAD(&dt_driver_failed_list, elt, link);
366 
367 		EMSG("Failed to probe %s on node %s: %#"PRIx32,
368 		     drv_name, node_name, res);
369 		break;
370 	}
371 
372 	return res;
373 }
374 
375 static TEE_Result alloc_elt_and_probe(const void *fdt, int node,
376 				      const struct dt_driver *dt_drv,
377 				      const struct dt_device_match *dm)
378 {
379 	struct dt_driver_probe *elt = NULL;
380 
381 	/* Will be freed when lists are released */
382 	elt = calloc(1, sizeof(*elt));
383 	if (!elt)
384 		return TEE_ERROR_OUT_OF_MEMORY;
385 
386 	elt->nodeoffset = node;
387 	elt->dt_drv = dt_drv;
388 	elt->dm = dm;
389 	elt->type = dt_drv->type;
390 
391 	return probe_driver_node(fdt, elt);
392 }
393 
394 /* Lookup a compatible driver, possibly of a specific @type, for the FDT node */
395 static TEE_Result probe_device_by_compat(const void *fdt, int node,
396 					 const char *compat,
397 					 enum dt_driver_type type)
398 {
399 	const struct dt_driver *drv = NULL;
400 	const struct dt_device_match *dm = NULL;
401 
402 	for_each_dt_driver(drv) {
403 		if (drv->type != type)
404 			continue;
405 
406 		for (dm = drv->match_table; dm && dm->compatible; dm++)
407 			if (strcmp(dm->compatible, compat) == 0)
408 				return alloc_elt_and_probe(fdt, node, drv, dm);
409 	}
410 
411 	return TEE_ERROR_ITEM_NOT_FOUND;
412 }
413 
414 /*
415  * Lookup the best matching compatible driver, possibly of a specific @type,
416  * for the FDT node.
417  */
418 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
419 					  enum dt_driver_type type)
420 {
421 	int idx = 0;
422 	int len = 0;
423 	int count = 0;
424 	const char *compat = NULL;
425 	TEE_Result res = TEE_ERROR_GENERIC;
426 
427 	assert_type_is_valid(type);
428 
429 	count = fdt_stringlist_count(fdt, nodeoffset, "compatible");
430 	if (count < 0)
431 		return TEE_ERROR_ITEM_NOT_FOUND;
432 
433 	for (idx = 0; idx < count; idx++) {
434 		compat = fdt_stringlist_get(fdt, nodeoffset, "compatible",
435 					    idx, &len);
436 		if (!compat)
437 			return TEE_ERROR_GENERIC;
438 
439 		res = probe_device_by_compat(fdt, nodeoffset, compat, type);
440 
441 		if (res != TEE_ERROR_ITEM_NOT_FOUND)
442 			return res;
443 	}
444 
445 	return TEE_ERROR_ITEM_NOT_FOUND;
446 }
447 
448 static TEE_Result process_probe_list(const void *fdt)
449 {
450 	struct dt_driver_probe *elt = NULL;
451 	struct dt_driver_probe *prev = NULL;
452 	static unsigned int __maybe_unused loop_count;
453 	static unsigned int __maybe_unused deferral_loop_count;
454 	bool __maybe_unused one_deferred = false;
455 	bool one_probed_ok = false;
456 
457 	do {
458 		loop_count++;
459 		FMSG("Probe loop %u after %u for deferral(s)", loop_count,
460 		     deferral_loop_count);
461 
462 		/* Hack here for TRACE_DEBUG messages on probe list elements */
463 		if (TRACE_LEVEL >= TRACE_FLOW)
464 			print_probe_list(fdt);
465 
466 		if (TAILQ_EMPTY(&dt_driver_probe_list))
467 			return TEE_SUCCESS;
468 
469 		/*
470 		 * Probe from current end to top. Deferred probed node are
471 		 * pushed back after current tail for the next probe round.
472 		 * Reset probe result flags and see status after probe round.
473 		 */
474 		one_deferred = false;
475 		one_probed_ok = false;
476 		added_node = false;
477 
478 		TAILQ_FOREACH_REVERSE_SAFE(elt, &dt_driver_probe_list,
479 					   dt_driver_probe_head, link, prev) {
480 			TAILQ_REMOVE(&dt_driver_probe_list, elt, link);
481 
482 			switch (probe_driver_node(fdt, elt)) {
483 			case TEE_SUCCESS:
484 				one_probed_ok = true;
485 				break;
486 			case TEE_ERROR_DEFER_DRIVER_INIT:
487 				one_deferred = true;
488 				break;
489 			default:
490 				break;
491 			}
492 		}
493 
494 		if (one_deferred)
495 			deferral_loop_count++;
496 
497 	} while (added_node || one_probed_ok);
498 
499 	DMSG("Unresolved dependencies after %u rounds, %u deferred",
500 	     loop_count, deferral_loop_count);
501 
502 	if (one_deferred)
503 		return TEE_ERROR_DEFER_DRIVER_INIT;
504 	else
505 		return TEE_ERROR_GENERIC;
506 }
507 
508 static int driver_probe_compare(struct dt_driver_probe *candidate,
509 				struct dt_driver_probe *elt)
510 {
511 	if (candidate->nodeoffset != elt->nodeoffset ||
512 	    candidate->type != elt->type)
513 		return 1;
514 
515 	assert(elt->dt_drv == candidate->dt_drv);
516 	return 0;
517 }
518 
519 /*
520  * Return TEE_SUCCESS if compatible found
521  *	  TEE_ERROR_OUT_OF_MEMORY if heap is exhausted
522  */
523 static TEE_Result add_node_to_probe(const void *fdt, int node,
524 				    const struct dt_driver *dt_drv,
525 				    const struct dt_device_match *dm)
526 {
527 	const char __maybe_unused *node_name = fdt_get_name(fdt, node, NULL);
528 	const char __maybe_unused *drv_name = dt_drv->name;
529 	struct dt_driver_probe *elt = NULL;
530 	struct dt_driver_probe elt_new = {
531 		.dm = dm,
532 		.dt_drv = dt_drv,
533 		.nodeoffset = node,
534 		.type = dt_drv->type,
535 	};
536 
537 	/* If node/type found in probe list or ready list, nothing to do */
538 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
539 		if (!driver_probe_compare(&elt_new, elt))
540 			return TEE_SUCCESS;
541 
542 	TAILQ_FOREACH(elt, &dt_driver_ready_list, link)
543 		if (!driver_probe_compare(&elt_new, elt))
544 			return TEE_SUCCESS;
545 
546 	elt = malloc(sizeof(*elt));
547 	if (!elt)
548 		return TEE_ERROR_OUT_OF_MEMORY;
549 
550 	DMSG("element: %s on node %s", node_name, drv_name);
551 
552 	memcpy(elt, &elt_new, sizeof(*elt));
553 
554 	added_node = true;
555 
556 	TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link);
557 
558 	/* Hack here for TRACE_DEBUG messages on current probe list elements */
559 	if (TRACE_LEVEL >= TRACE_FLOW)
560 		print_probe_list(fdt);
561 
562 	return TEE_SUCCESS;
563 }
564 
565 /*
566  * Add a node to the probe list if a dt_driver matches target compatible.
567  *
568  * If @type is DT_DRIVER_ANY, probe list can hold only 1 driver to probe for
569  * the node. A node may probe several drivers if have a unique driver type.
570  *
571  * Return TEE_SUCCESS if compatible found
572  *	  TEE_ERROR_ITEM_NOT_FOUND if no matching driver
573  *	  TEE_ERROR_OUT_OF_MEMORY if heap is exhausted
574  */
575 static TEE_Result add_probe_node_by_compat(const void *fdt, int node,
576 					   const char *compat)
577 {
578 	TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND;
579 	const struct dt_driver *dt_drv = NULL;
580 	const struct dt_device_match *dm = NULL;
581 	uint32_t found_types = 0;
582 
583 	for_each_dt_driver(dt_drv) {
584 		for (dm = dt_drv->match_table; dm && dm->compatible; dm++) {
585 			if (strcmp(dm->compatible, compat) == 0) {
586 				assert(dt_drv->type < 32);
587 
588 				res = add_node_to_probe(fdt, node, dt_drv, dm);
589 				if (res)
590 					return res;
591 
592 				if (found_types & BIT(dt_drv->type)) {
593 					EMSG("Driver %s multi hit on type %u",
594 					     dt_drv->name, dt_drv->type);
595 					panic();
596 				}
597 				found_types |= BIT(dt_drv->type);
598 
599 				/* Matching found for this driver, try next */
600 				break;
601 			}
602 		}
603 	}
604 
605 	return res;
606 }
607 
608 /*
609  * Add the node to the probe list if matching compatible drivers are found.
610  * Follow node's compatible property list ordering to find matching driver.
611  */
612 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int node)
613 {
614 	int idx = 0;
615 	int len = 0;
616 	int count = 0;
617 	const char *compat = NULL;
618 	TEE_Result res = TEE_ERROR_GENERIC;
619 
620 	if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED)
621 		return TEE_SUCCESS;
622 
623 	count = fdt_stringlist_count(fdt, node, "compatible");
624 	if (count < 0)
625 		return TEE_SUCCESS;
626 
627 	for (idx = 0; idx < count; idx++) {
628 		compat = fdt_stringlist_get(fdt, node, "compatible", idx, &len);
629 		assert(compat && len > 0);
630 
631 		res = add_probe_node_by_compat(fdt, node, compat);
632 
633 		/* Stop lookup if something was found */
634 		if (res != TEE_ERROR_ITEM_NOT_FOUND)
635 			return res;
636 	}
637 
638 	return TEE_SUCCESS;
639 }
640 
641 static void parse_node(const void *fdt, int node)
642 {
643 	TEE_Result __maybe_unused res = TEE_ERROR_GENERIC;
644 	int subnode = 0;
645 
646 	fdt_for_each_subnode(subnode, fdt, node) {
647 		res = dt_driver_maybe_add_probe_node(fdt, subnode);
648 		if (res) {
649 			EMSG("Failed on node %s with %#"PRIx32,
650 			     fdt_get_name(fdt, subnode, NULL), res);
651 			panic();
652 		}
653 
654 		/*
655 		 * Rescursively parse the FDT, skipping disabled nodes.
656 		 * FDT is expected reliable and core shall have sufficient
657 		 * stack depth to possibly parse all DT nodes.
658 		 */
659 		if (IS_ENABLED(CFG_DRIVERS_DT_RECURSIVE_PROBE)) {
660 			if (_fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED)
661 				continue;
662 
663 			parse_node(fdt, subnode);
664 		}
665 	}
666 }
667 
668 /*
669  * Parse FDT for nodes and save in probe list the node for which a dt_driver
670  * matches node's compatible property.
671  */
672 static TEE_Result probe_dt_drivers_early(void)
673 {
674 	TEE_Result res = TEE_ERROR_GENERIC;
675 	const void *fdt = NULL;
676 
677 	if (!IS_ENABLED(CFG_EMBED_DTB))
678 		return TEE_SUCCESS;
679 
680 	fdt = get_embedded_dt();
681 	assert(fdt);
682 
683 	parse_node(fdt, fdt_path_offset(fdt, "/"));
684 
685 	res = process_probe_list(fdt);
686 	if (res == TEE_ERROR_DEFER_DRIVER_INIT) {
687 		DMSG("Deferred drivers probing");
688 		print_probe_list(fdt);
689 		res = TEE_SUCCESS;
690 	}
691 
692 	return res;
693 }
694 
695 static TEE_Result probe_dt_drivers(void)
696 {
697 	TEE_Result res = TEE_ERROR_GENERIC;
698 	const void *fdt = NULL;
699 
700 	if (!IS_ENABLED(CFG_EMBED_DTB))
701 		return TEE_SUCCESS;
702 
703 	fdt = get_embedded_dt();
704 	assert(fdt);
705 
706 	res = process_probe_list(fdt);
707 	if (res || !TAILQ_EMPTY(&dt_driver_failed_list)) {
708 		EMSG("Probe sequence result: %#"PRIx32, res);
709 		print_probe_list(fdt);
710 	}
711 	if (res)
712 		panic();
713 
714 	return TEE_SUCCESS;
715 }
716 
717 early_init_late(probe_dt_drivers_early);
718 driver_init(probe_dt_drivers);
719 
720 static TEE_Result release_probe_lists(void)
721 {
722 	struct dt_driver_probe *elt = NULL;
723 	struct dt_driver_probe *next = NULL;
724 	struct dt_driver_provider *prov = NULL;
725 	struct dt_driver_provider *next_prov = NULL;
726 	const void * __maybe_unused fdt = NULL;
727 
728 	if (!IS_ENABLED(CFG_EMBED_DTB))
729 		return TEE_SUCCESS;
730 
731 	fdt = get_embedded_dt();
732 
733 	assert(fdt && TAILQ_EMPTY(&dt_driver_probe_list));
734 
735 	TAILQ_FOREACH_SAFE(elt, &dt_driver_ready_list, link, next)
736 		free(elt);
737 
738 	TAILQ_FOREACH_SAFE(elt, &dt_driver_failed_list, link, next)
739 	       free(elt);
740 
741 	SLIST_FOREACH_SAFE(prov, &dt_driver_provider_list, link, next_prov)
742 	       free(prov);
743 
744 	return TEE_SUCCESS;
745 }
746 
747 release_init_resource(release_probe_lists);
748 
749 /*
750  * Simple bus support: handy to parse subnodes
751  */
752 static TEE_Result simple_bus_probe(const void *fdt, int node,
753 				   const void *compat_data __unused)
754 {
755 	TEE_Result res = TEE_ERROR_GENERIC;
756 	int subnode = 0;
757 
758 	fdt_for_each_subnode(subnode, fdt, node) {
759 		res = dt_driver_maybe_add_probe_node(fdt, subnode);
760 		if (res) {
761 			EMSG("Failed on node %s with %#"PRIx32,
762 			     fdt_get_name(fdt, subnode, NULL), res);
763 			panic();
764 		}
765 	}
766 
767 	return TEE_SUCCESS;
768 }
769 
770 static const struct dt_device_match simple_bus_match_table[] = {
771 	{ .compatible = "simple-bus" },
772 	{ }
773 };
774 
775 DEFINE_DT_DRIVER(simple_bus_dt_driver) = {
776 	.name = "simple-bus",
777 	.match_table = simple_bus_match_table,
778 	.probe = simple_bus_probe,
779 };
780