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