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