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