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