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