xref: /optee_os/core/kernel/dt_driver.c (revision 32b3180828fa15a49ccc86ecb4be9d274c140c89)
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 + 1]);
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 		return device_from_provider_prop(prv, fdt, phandle_node,
399 						 prop + idx32, device);
400 	}
401 
402 	return TEE_ERROR_ITEM_NOT_FOUND;
403 }
404 
405 static void __maybe_unused print_probe_list(const void *fdt __maybe_unused)
406 {
407 	struct dt_driver_probe *elt = NULL;
408 	unsigned int count = 0;
409 
410 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
411 		count++;
412 
413 	DMSG("Probe list: %u elements", count);
414 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
415 		DMSG("|- Driver %s probes on node %s",
416 		     elt->dt_drv->name,
417 		     fdt_get_name(fdt, elt->nodeoffset, NULL));
418 
419 	DMSG("`- Probe list end");
420 
421 	count = 0;
422 	TAILQ_FOREACH(elt, &dt_driver_failed_list, link)
423 		count++;
424 
425 	DMSG("Failed list: %u elements", count);
426 	TAILQ_FOREACH(elt, &dt_driver_failed_list, link)
427 		EMSG("|- Driver %s on node %s failed", elt->dt_drv->name,
428 		     fdt_get_name(fdt, elt->nodeoffset, NULL));
429 
430 	DMSG("`- Failed list end");
431 }
432 
433 /*
434  * Probe element: push to ready list if succeeds, push to probe list if probe
435  * if deferred, panic with an error trace otherwise.
436  */
437 static TEE_Result probe_driver_node(const void *fdt,
438 				    struct dt_driver_probe *elt)
439 {
440 	TEE_Result res = TEE_ERROR_GENERIC;
441 	const char __maybe_unused *drv_name = NULL;
442 	const char __maybe_unused *node_name = NULL;
443 
444 	node_name = fdt_get_name(fdt, elt->nodeoffset, NULL);
445 	drv_name = elt->dt_drv->name;
446 
447 	if (!elt->dt_drv->probe) {
448 		DMSG("No probe operator for driver %s, skipped", drv_name);
449 		return TEE_SUCCESS;
450 	}
451 
452 	FMSG("Probing %s on node %s", drv_name, node_name);
453 
454 	res = elt->dt_drv->probe(fdt, elt->nodeoffset, elt->dm->compat_data);
455 	switch (res) {
456 	case TEE_SUCCESS:
457 		TAILQ_INSERT_HEAD(&dt_driver_ready_list, elt, link);
458 
459 		DMSG("element: %s on node %s initialized", drv_name, node_name);
460 		break;
461 	case TEE_ERROR_DEFER_DRIVER_INIT:
462 		elt->deferrals++;
463 		TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link);
464 
465 		DMSG("element: %s on node %s deferred %u time(s)", drv_name,
466 		     node_name, elt->deferrals);
467 		break;
468 	case TEE_ERROR_NODE_DISABLED:
469 		DMSG("element: %s on node %s is disabled", drv_name, node_name);
470 		break;
471 	default:
472 		TAILQ_INSERT_HEAD(&dt_driver_failed_list, elt, link);
473 
474 		EMSG("Failed to probe %s on node %s: %#"PRIx32,
475 		     drv_name, node_name, res);
476 		break;
477 	}
478 
479 	return res;
480 }
481 
482 static TEE_Result alloc_elt_and_probe(const void *fdt, int node,
483 				      const struct dt_driver *dt_drv,
484 				      const struct dt_device_match *dm)
485 {
486 	struct dt_driver_probe *elt = NULL;
487 
488 	/* Will be freed when lists are released */
489 	elt = calloc(1, sizeof(*elt));
490 	if (!elt)
491 		return TEE_ERROR_OUT_OF_MEMORY;
492 
493 	elt->nodeoffset = node;
494 	elt->dt_drv = dt_drv;
495 	elt->dm = dm;
496 	elt->type = dt_drv->type;
497 
498 	return probe_driver_node(fdt, elt);
499 }
500 
501 /* Lookup a compatible driver, possibly of a specific @type, for the FDT node */
502 static TEE_Result probe_device_by_compat(const void *fdt, int node,
503 					 const char *compat,
504 					 enum dt_driver_type type)
505 {
506 	const struct dt_driver *drv = NULL;
507 	const struct dt_device_match *dm = NULL;
508 
509 	for_each_dt_driver(drv) {
510 		if (drv->type != type)
511 			continue;
512 
513 		for (dm = drv->match_table; dm && dm->compatible; dm++)
514 			if (strcmp(dm->compatible, compat) == 0)
515 				return alloc_elt_and_probe(fdt, node, drv, dm);
516 	}
517 
518 	return TEE_ERROR_ITEM_NOT_FOUND;
519 }
520 
521 /*
522  * Lookup the best matching compatible driver, possibly of a specific @type,
523  * for the FDT node.
524  */
525 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
526 					  enum dt_driver_type type)
527 {
528 	int idx = 0;
529 	int len = 0;
530 	int count = 0;
531 	const char *compat = NULL;
532 	TEE_Result res = TEE_ERROR_GENERIC;
533 
534 	assert_type_is_valid(type);
535 
536 	count = fdt_stringlist_count(fdt, nodeoffset, "compatible");
537 	if (count < 0)
538 		return TEE_ERROR_ITEM_NOT_FOUND;
539 
540 	for (idx = 0; idx < count; idx++) {
541 		compat = fdt_stringlist_get(fdt, nodeoffset, "compatible",
542 					    idx, &len);
543 		if (!compat)
544 			return TEE_ERROR_GENERIC;
545 
546 		res = probe_device_by_compat(fdt, nodeoffset, compat, type);
547 
548 		if (res != TEE_ERROR_ITEM_NOT_FOUND)
549 			return res;
550 	}
551 
552 	return TEE_ERROR_ITEM_NOT_FOUND;
553 }
554 
555 static TEE_Result process_probe_list(const void *fdt)
556 {
557 	struct dt_driver_probe *elt = NULL;
558 	struct dt_driver_probe *prev = NULL;
559 	static unsigned int __maybe_unused loop_count;
560 	static unsigned int __maybe_unused deferral_loop_count;
561 	bool __maybe_unused one_deferred = false;
562 	bool one_probed_ok = false;
563 
564 	do {
565 		loop_count++;
566 		FMSG("Probe loop %u after %u for deferral(s)", loop_count,
567 		     deferral_loop_count);
568 
569 		/* Hack here for TRACE_DEBUG messages on probe list elements */
570 		if (TRACE_LEVEL >= TRACE_FLOW)
571 			print_probe_list(fdt);
572 
573 		if (TAILQ_EMPTY(&dt_driver_probe_list))
574 			return TEE_SUCCESS;
575 
576 		/*
577 		 * Probe from current end to top. Deferred probed node are
578 		 * pushed back after current tail for the next probe round.
579 		 * Reset probe result flags and see status after probe round.
580 		 */
581 		one_deferred = false;
582 		one_probed_ok = false;
583 		added_node = false;
584 
585 		TAILQ_FOREACH_REVERSE_SAFE(elt, &dt_driver_probe_list,
586 					   dt_driver_probe_head, link, prev) {
587 			TAILQ_REMOVE(&dt_driver_probe_list, elt, link);
588 
589 			switch (probe_driver_node(fdt, elt)) {
590 			case TEE_SUCCESS:
591 				one_probed_ok = true;
592 				break;
593 			case TEE_ERROR_DEFER_DRIVER_INIT:
594 				one_deferred = true;
595 				break;
596 			default:
597 				break;
598 			}
599 		}
600 
601 		if (one_deferred)
602 			deferral_loop_count++;
603 
604 	} while (added_node || one_probed_ok);
605 
606 	DMSG("Unresolved dependencies after %u rounds, %u deferred",
607 	     loop_count, deferral_loop_count);
608 
609 	if (one_deferred)
610 		return TEE_ERROR_DEFER_DRIVER_INIT;
611 	else
612 		return TEE_ERROR_GENERIC;
613 }
614 
615 static int driver_probe_compare(struct dt_driver_probe *candidate,
616 				struct dt_driver_probe *elt)
617 {
618 	if (candidate->nodeoffset != elt->nodeoffset ||
619 	    candidate->type != elt->type)
620 		return 1;
621 
622 	assert(elt->dt_drv == candidate->dt_drv);
623 	return 0;
624 }
625 
626 /*
627  * Return TEE_SUCCESS if compatible found
628  *	  TEE_ERROR_OUT_OF_MEMORY if heap is exhausted
629  */
630 static TEE_Result add_node_to_probe(const void *fdt, int node,
631 				    const struct dt_driver *dt_drv,
632 				    const struct dt_device_match *dm)
633 {
634 	const char __maybe_unused *node_name = fdt_get_name(fdt, node, NULL);
635 	const char __maybe_unused *drv_name = dt_drv->name;
636 	struct dt_driver_probe *elt = NULL;
637 	struct dt_driver_probe elt_new = {
638 		.dm = dm,
639 		.dt_drv = dt_drv,
640 		.nodeoffset = node,
641 		.type = dt_drv->type,
642 	};
643 
644 	/* If node/type found in probe list or ready list, nothing to do */
645 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
646 		if (!driver_probe_compare(&elt_new, elt))
647 			return TEE_SUCCESS;
648 
649 	TAILQ_FOREACH(elt, &dt_driver_ready_list, link)
650 		if (!driver_probe_compare(&elt_new, elt))
651 			return TEE_SUCCESS;
652 
653 	elt = malloc(sizeof(*elt));
654 	if (!elt)
655 		return TEE_ERROR_OUT_OF_MEMORY;
656 
657 	DMSG("element: %s on node %s", node_name, drv_name);
658 
659 	memcpy(elt, &elt_new, sizeof(*elt));
660 
661 	added_node = true;
662 
663 	TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link);
664 
665 	/* Hack here for TRACE_DEBUG messages on current probe list elements */
666 	if (TRACE_LEVEL >= TRACE_FLOW)
667 		print_probe_list(fdt);
668 
669 	return TEE_SUCCESS;
670 }
671 
672 /*
673  * Add a node to the probe list if a dt_driver matches target compatible.
674  *
675  * If @type is DT_DRIVER_ANY, probe list can hold only 1 driver to probe for
676  * the node. A node may probe several drivers if have a unique driver type.
677  *
678  * Return TEE_SUCCESS if compatible found
679  *	  TEE_ERROR_ITEM_NOT_FOUND if no matching driver
680  *	  TEE_ERROR_OUT_OF_MEMORY if heap is exhausted
681  */
682 static TEE_Result add_probe_node_by_compat(const void *fdt, int node,
683 					   const char *compat)
684 {
685 	TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND;
686 	const struct dt_driver *dt_drv = NULL;
687 	const struct dt_device_match *dm = NULL;
688 	uint32_t found_types = 0;
689 
690 	for_each_dt_driver(dt_drv) {
691 		for (dm = dt_drv->match_table; dm && dm->compatible; dm++) {
692 			if (strcmp(dm->compatible, compat) == 0) {
693 				assert(dt_drv->type < 32);
694 
695 				res = add_node_to_probe(fdt, node, dt_drv, dm);
696 				if (res)
697 					return res;
698 
699 				if (found_types & BIT(dt_drv->type)) {
700 					EMSG("Driver %s multi hit on type %u",
701 					     dt_drv->name, dt_drv->type);
702 					panic();
703 				}
704 				found_types |= BIT(dt_drv->type);
705 
706 				/* Matching found for this driver, try next */
707 				break;
708 			}
709 		}
710 	}
711 
712 	return res;
713 }
714 
715 /*
716  * Add the node to the probe list if matching compatible drivers are found.
717  * Follow node's compatible property list ordering to find matching driver.
718  */
719 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int node)
720 {
721 	int idx = 0;
722 	int len = 0;
723 	int count = 0;
724 	const char *compat = NULL;
725 	TEE_Result res = TEE_ERROR_GENERIC;
726 
727 	if (fdt_get_status(fdt, node) == DT_STATUS_DISABLED)
728 		return TEE_SUCCESS;
729 
730 	count = fdt_stringlist_count(fdt, node, "compatible");
731 	if (count < 0)
732 		return TEE_SUCCESS;
733 
734 	for (idx = 0; idx < count; idx++) {
735 		compat = fdt_stringlist_get(fdt, node, "compatible", idx, &len);
736 		assert(compat && len > 0);
737 
738 		res = add_probe_node_by_compat(fdt, node, compat);
739 
740 		/* Stop lookup if something was found */
741 		if (res != TEE_ERROR_ITEM_NOT_FOUND)
742 			return res;
743 	}
744 
745 	return TEE_SUCCESS;
746 }
747 
748 static void parse_node(const void *fdt, int node)
749 {
750 	TEE_Result __maybe_unused res = TEE_ERROR_GENERIC;
751 	int subnode = 0;
752 
753 	fdt_for_each_subnode(subnode, fdt, node) {
754 		res = dt_driver_maybe_add_probe_node(fdt, subnode);
755 		if (res) {
756 			EMSG("Failed on node %s with %#"PRIx32,
757 			     fdt_get_name(fdt, subnode, NULL), res);
758 			panic();
759 		}
760 
761 		/*
762 		 * Rescursively parse the FDT, skipping disabled nodes.
763 		 * FDT is expected reliable and core shall have sufficient
764 		 * stack depth to possibly parse all DT nodes.
765 		 */
766 		if (IS_ENABLED(CFG_DRIVERS_DT_RECURSIVE_PROBE)) {
767 			if (fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED)
768 				continue;
769 
770 			parse_node(fdt, subnode);
771 		}
772 	}
773 }
774 
775 /*
776  * Parse FDT for nodes and save in probe list the node for which a dt_driver
777  * matches node's compatible property.
778  */
779 static TEE_Result probe_dt_drivers_early(void)
780 {
781 	TEE_Result res = TEE_ERROR_GENERIC;
782 	const void *fdt = NULL;
783 
784 	fdt = get_secure_dt();
785 	if (!fdt)
786 		return TEE_SUCCESS;
787 
788 	parse_node(fdt, fdt_path_offset(fdt, "/"));
789 
790 	res = process_probe_list(fdt);
791 	if (res == TEE_ERROR_DEFER_DRIVER_INIT) {
792 		DMSG("Deferred drivers probing");
793 		print_probe_list(fdt);
794 		res = TEE_SUCCESS;
795 	}
796 
797 	return res;
798 }
799 
800 static TEE_Result probe_dt_drivers(void)
801 {
802 	TEE_Result res = TEE_ERROR_GENERIC;
803 	const void *fdt = NULL;
804 
805 	fdt = get_secure_dt();
806 	if (!fdt)
807 		return TEE_SUCCESS;
808 
809 	res = process_probe_list(fdt);
810 	if (res || !TAILQ_EMPTY(&dt_driver_failed_list)) {
811 		EMSG("Probe sequence result: %#"PRIx32, res);
812 		print_probe_list(fdt);
813 		panic();
814 	}
815 
816 	return TEE_SUCCESS;
817 }
818 
819 early_init_late(probe_dt_drivers_early);
820 driver_init(probe_dt_drivers);
821 
822 static TEE_Result release_probe_lists(void)
823 {
824 	struct dt_driver_probe *elt = NULL;
825 	struct dt_driver_probe *next = NULL;
826 	struct dt_driver_provider *prov = NULL;
827 	struct dt_driver_provider *next_prov = NULL;
828 	const void *fdt = NULL;
829 
830 	fdt = get_secure_dt();
831 	if (!fdt)
832 		return TEE_SUCCESS;
833 
834 	assert(fdt && TAILQ_EMPTY(&dt_driver_probe_list));
835 
836 	TAILQ_FOREACH_SAFE(elt, &dt_driver_ready_list, link, next)
837 		free(elt);
838 
839 	TAILQ_FOREACH_SAFE(elt, &dt_driver_failed_list, link, next)
840 	       free(elt);
841 
842 	SLIST_FOREACH_SAFE(prov, &dt_driver_provider_list, link, next_prov)
843 	       free(prov);
844 
845 	return TEE_SUCCESS;
846 }
847 
848 release_init_resource(release_probe_lists);
849 
850 /*
851  * Simple bus support: handy to parse subnodes
852  */
853 static TEE_Result simple_bus_probe(const void *fdt, int node,
854 				   const void *compat_data __unused)
855 {
856 	TEE_Result res = TEE_ERROR_GENERIC;
857 	int subnode = 0;
858 
859 	fdt_for_each_subnode(subnode, fdt, node) {
860 		res = dt_driver_maybe_add_probe_node(fdt, subnode);
861 		if (res) {
862 			EMSG("Failed on node %s with %#"PRIx32,
863 			     fdt_get_name(fdt, subnode, NULL), res);
864 			panic();
865 		}
866 	}
867 
868 	return TEE_SUCCESS;
869 }
870 
871 static const struct dt_device_match simple_bus_match_table[] = {
872 	{ .compatible = "simple-bus" },
873 	{ }
874 };
875 
876 DEFINE_DT_DRIVER(simple_bus_dt_driver) = {
877 	.name = "simple-bus",
878 	.match_table = simple_bus_match_table,
879 	.probe = simple_bus_probe,
880 };
881