xref: /optee_os/core/kernel/dt_driver.c (revision b3a88b52a17cc124fd7ef3dc7a37ec2ba51febc0)
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 /* Flag enabled when a new node (possibly typed) is added in the probe list */
79 static bool added_node;
80 
81 static void assert_type_is_valid(enum dt_driver_type type)
82 {
83 	switch (type) {
84 	case DT_DRIVER_NOTYPE:
85 	case DT_DRIVER_UART:
86 	case DT_DRIVER_CLK:
87 		return;
88 	default:
89 		assert(0);
90 	}
91 }
92 
93 /*
94  * Driver provider registering API functions
95  */
96 
97 TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset,
98 				       get_of_device_func get_of_device,
99 				       void *priv, enum dt_driver_type type)
100 {
101 	struct dt_driver_provider *prv = NULL;
102 	int provider_cells = 0;
103 	uint32_t phandle = 0;
104 
105 	assert_type_is_valid(type);
106 
107 	provider_cells = fdt_get_dt_driver_cells(fdt, nodeoffset, type);
108 	if (provider_cells < 0) {
109 		DMSG("Failed to find provider cells: %d", provider_cells);
110 		return TEE_ERROR_GENERIC;
111 	}
112 
113 	phandle = fdt_get_phandle(fdt, nodeoffset);
114 	if (!phandle || phandle == (uint32_t)-1) {
115 		DMSG("Failed to find provide phandle");
116 		return TEE_ERROR_GENERIC;
117 	}
118 
119 	prv = calloc(1, sizeof(*prv));
120 	if (!prv)
121 		return TEE_ERROR_OUT_OF_MEMORY;
122 
123 	prv->nodeoffset = nodeoffset;
124 	prv->type = type;
125 	prv->provider_cells = provider_cells;
126 	prv->phandle = phandle;
127 	prv->get_of_device = get_of_device;
128 	prv->priv_data = priv;
129 
130 	SLIST_INSERT_HEAD(&dt_driver_provider_list, prv, link);
131 
132 	return TEE_SUCCESS;
133 }
134 
135 /* Release driver provider references once all dt_drivers are initialized */
136 static TEE_Result dt_driver_release_provider(void)
137 {
138 	struct dt_driver_provider *prv = NULL;
139 
140 	while (!SLIST_EMPTY(&dt_driver_provider_list)) {
141 		prv = SLIST_FIRST(&dt_driver_provider_list);
142 		SLIST_REMOVE_HEAD(&dt_driver_provider_list, link);
143 		free(prv);
144 	}
145 
146 	return TEE_SUCCESS;
147 }
148 
149 driver_init_late(dt_driver_release_provider);
150 
151 /*
152  * Helper functions for dt_drivers querying driver provider information
153  */
154 
155 int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset,
156 			    enum dt_driver_type type)
157 {
158 	const char *cells_name = NULL;
159 	const fdt32_t *c = NULL;
160 	int len = 0;
161 
162 	switch (type) {
163 	case DT_DRIVER_CLK:
164 		cells_name = "#clock-cells";
165 		break;
166 	default:
167 		panic();
168 	}
169 
170 	c = fdt_getprop(fdt, nodeoffset, cells_name, &len);
171 	if (!c)
172 		return len;
173 
174 	if (len != sizeof(*c))
175 		return -FDT_ERR_BADNCELLS;
176 
177 	return fdt32_to_cpu(*c);
178 }
179 
180 unsigned int dt_driver_provider_cells(struct dt_driver_provider *prv)
181 {
182 	return prv->provider_cells;
183 }
184 
185 struct dt_driver_provider *dt_driver_get_provider_by_node(int nodeoffset)
186 {
187 	struct dt_driver_provider *prv = NULL;
188 
189 	SLIST_FOREACH(prv, &dt_driver_provider_list, link)
190 		if (prv->nodeoffset == nodeoffset)
191 			return prv;
192 
193 	return NULL;
194 }
195 
196 struct dt_driver_provider *dt_driver_get_provider_by_phandle(uint32_t phandle)
197 {
198 	struct dt_driver_provider *prv = NULL;
199 
200 	SLIST_FOREACH(prv, &dt_driver_provider_list, link)
201 		if (prv->phandle == phandle)
202 			return prv;
203 
204 	return NULL;
205 }
206 
207 static void *device_from_provider_prop(struct dt_driver_provider *prv,
208 					  const uint32_t *prop,
209 					  TEE_Result *res)
210 {
211 	struct dt_driver_phandle_args *pargs = NULL;
212 	unsigned int n = 0;
213 	void *device = NULL;
214 
215 	pargs = calloc(1, prv->provider_cells * sizeof(uint32_t *) +
216 		       sizeof(*pargs));
217 	if (!pargs) {
218 		*res = TEE_ERROR_OUT_OF_MEMORY;
219 		return NULL;
220 	}
221 
222 	pargs->args_count = prv->provider_cells;
223 	for (n = 0; n < prv->provider_cells; n++)
224 		pargs->args[n] = fdt32_to_cpu(prop[n + 1]);
225 
226 	device = prv->get_of_device(pargs, prv->priv_data, res);
227 
228 	free(pargs);
229 
230 	return device;
231 }
232 
233 void *dt_driver_device_from_node_idx_prop(const char *prop_name,
234 					  const void *fdt, int nodeoffset,
235 					  unsigned int prop_idx,
236 					  TEE_Result *res)
237 {
238 	int len = 0;
239 	int idx = 0;
240 	int idx32 = 0;
241 	int prv_cells = 0;
242 	uint32_t phandle = 0;
243 	const uint32_t *prop = NULL;
244 	struct dt_driver_provider *prv = NULL;
245 
246 	prop = fdt_getprop(fdt, nodeoffset, prop_name, &len);
247 	if (!prop) {
248 		*res = TEE_ERROR_GENERIC;
249 		return NULL;
250 	}
251 
252 	while (idx < len) {
253 		idx32 = idx / sizeof(uint32_t);
254 		phandle = fdt32_to_cpu(prop[idx32]);
255 
256 		prv = dt_driver_get_provider_by_phandle(phandle);
257 		if (!prv) {
258 			*res = TEE_ERROR_GENERIC;
259 			return NULL;
260 		}
261 
262 		prv_cells = dt_driver_provider_cells(prv);
263 		if (prop_idx) {
264 			prop_idx--;
265 			idx += sizeof(phandle) + prv_cells * sizeof(uint32_t);
266 			continue;
267 		}
268 
269 		return device_from_provider_prop(prv, prop + idx32, res);
270 	}
271 
272 	*res = TEE_ERROR_GENERIC;
273 	return NULL;
274 }
275 
276 static unsigned int __maybe_unused probe_list_count(void)
277 {
278 	struct dt_driver_probe *elt = NULL;
279 	unsigned int count = 0;
280 
281 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
282 		count++;
283 
284 	return count;
285 }
286 
287 static void __maybe_unused print_probe_list(const void *fdt __maybe_unused)
288 {
289 	struct dt_driver_probe *elt = NULL;
290 
291 	DMSG("Probe list: %u elements", probe_list_count());
292 
293 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
294 		DMSG("- Driver %s probes on node %s",
295 		     elt->dt_drv->name,
296 		     fdt_get_name(fdt, elt->nodeoffset, NULL));
297 
298 	DMSG("Probe list end");
299 }
300 
301 /*
302  * Probe element: push to ready list if succeeds, push to probe list if probe
303  * if deferred, panic with an error trace otherwise.
304  */
305 static TEE_Result probe_driver_node(const void *fdt,
306 				    struct dt_driver_probe *elt)
307 {
308 	TEE_Result res = TEE_ERROR_GENERIC;
309 	const char __maybe_unused *drv_name = NULL;
310 	const char __maybe_unused *node_name = NULL;
311 
312 	node_name = fdt_get_name(fdt, elt->nodeoffset, NULL);
313 	drv_name = elt->dt_drv->name;
314 	FMSG("Probing %s on node %s", drv_name, node_name);
315 
316 	res = elt->dt_drv->probe(fdt, elt->nodeoffset, elt->dm->compat_data);
317 	switch (res) {
318 	case TEE_SUCCESS:
319 		TAILQ_INSERT_HEAD(&dt_driver_ready_list, elt, link);
320 
321 		DMSG("element: %s on node %s initialized", drv_name, node_name);
322 		break;
323 	case TEE_ERROR_DEFER_DRIVER_INIT:
324 		elt->deferrals++;
325 		TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link);
326 
327 		DMSG("element: %s on node %s deferred %u time(s)", drv_name,
328 		     node_name, elt->deferrals);
329 		break;
330 	default:
331 		EMSG("Fail to probe %s on node %s: %#"PRIx32,
332 		     drv_name, node_name, res);
333 		panic();
334 	}
335 
336 	return res;
337 }
338 
339 static TEE_Result alloc_elt_and_probe(const void *fdt, int node,
340 				      const struct dt_driver *dt_drv,
341 				      const struct dt_device_match *dm)
342 {
343 	struct dt_driver_probe *elt = NULL;
344 
345 	/* Will be freed when lists are released */
346 	elt = calloc(1, sizeof(*elt));
347 	if (!elt)
348 		return TEE_ERROR_OUT_OF_MEMORY;
349 
350 	elt->nodeoffset = node;
351 	elt->dt_drv = dt_drv;
352 	elt->dm = dm;
353 	elt->type = dt_drv->type;
354 
355 	return probe_driver_node(fdt, elt);
356 }
357 
358 /* Lookup a compatible driver, possibly of a specific @type, for the FDT node */
359 static TEE_Result probe_device_by_compat(const void *fdt, int node,
360 					 const char *compat,
361 					 enum dt_driver_type type)
362 {
363 	const struct dt_driver *drv = NULL;
364 	const struct dt_device_match *dm = NULL;
365 
366 	for_each_dt_driver(drv) {
367 		if (drv->type != type)
368 			continue;
369 
370 		for (dm = drv->match_table; dm && dm->compatible; dm++)
371 			if (strcmp(dm->compatible, compat) == 0)
372 				return alloc_elt_and_probe(fdt, node, drv, dm);
373 	}
374 
375 	return TEE_ERROR_ITEM_NOT_FOUND;
376 }
377 
378 /*
379  * Lookup the best matching compatible driver, possibly of a specific @type,
380  * for the FDT node.
381  */
382 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
383 					  enum dt_driver_type type)
384 {
385 	int idx = 0;
386 	int len = 0;
387 	int count = 0;
388 	const char *compat = NULL;
389 	TEE_Result res = TEE_ERROR_GENERIC;
390 
391 	assert_type_is_valid(type);
392 
393 	count = fdt_stringlist_count(fdt, nodeoffset, "compatible");
394 	if (count < 0)
395 		return TEE_ERROR_ITEM_NOT_FOUND;
396 
397 	for (idx = 0; idx < count; idx++) {
398 		compat = fdt_stringlist_get(fdt, nodeoffset, "compatible",
399 					    idx, &len);
400 		if (!compat)
401 			return TEE_ERROR_GENERIC;
402 
403 		res = probe_device_by_compat(fdt, nodeoffset, compat, type);
404 
405 		if (res != TEE_ERROR_ITEM_NOT_FOUND)
406 			return res;
407 	}
408 
409 	return TEE_ERROR_ITEM_NOT_FOUND;
410 }
411 
412 static TEE_Result process_probe_list(const void *fdt)
413 {
414 	struct dt_driver_probe *elt = NULL;
415 	struct dt_driver_probe *prev = NULL;
416 	unsigned int __maybe_unused loop_count = 0;
417 	unsigned int __maybe_unused deferral_loop_count = 0;
418 	bool __maybe_unused one_deferred = false;
419 	bool one_probed_ok = false;
420 
421 	do {
422 		loop_count++;
423 		FMSG("Probe loop %u after %u for deferral(s)", loop_count,
424 		     deferral_loop_count);
425 
426 		/* Hack here for TRACE_DEBUG messages on probe list elements */
427 		if (TRACE_LEVEL >= TRACE_FLOW)
428 			print_probe_list(fdt);
429 
430 		if (TAILQ_EMPTY(&dt_driver_probe_list))
431 			return TEE_SUCCESS;
432 
433 		/*
434 		 * Probe from current end to top. Deferred probed node are
435 		 * pushed back after current tail for the next probe round.
436 		 * Reset probe result flags and see status after probe round.
437 		 */
438 		one_deferred = false;
439 		one_probed_ok = false;
440 		added_node = false;
441 
442 		TAILQ_FOREACH_REVERSE_SAFE(elt, &dt_driver_probe_list,
443 					   dt_driver_probe_head, link, prev) {
444 			TAILQ_REMOVE(&dt_driver_probe_list, elt, link);
445 
446 			switch (probe_driver_node(fdt, elt)) {
447 			case TEE_SUCCESS:
448 				one_probed_ok = true;
449 				break;
450 			case TEE_ERROR_DEFER_DRIVER_INIT:
451 				one_deferred = true;
452 				break;
453 			default:
454 				/* We don't expect error return codes */
455 				assert(0);
456 			}
457 		}
458 
459 		if (one_deferred)
460 			deferral_loop_count++;
461 
462 	} while (added_node || one_probed_ok);
463 
464 	EMSG("Panic on unresolved dependencies after %u rounds, %u deferred:",
465 	     loop_count, deferral_loop_count);
466 
467 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
468 		EMSG("- %s on node %s", elt->dt_drv->name,
469 		     fdt_get_name(fdt, elt->nodeoffset, NULL));
470 
471 	panic();
472 }
473 
474 static int driver_probe_compare(struct dt_driver_probe *candidate,
475 				struct dt_driver_probe *elt)
476 {
477 	if (candidate->nodeoffset != elt->nodeoffset ||
478 	    candidate->type != elt->type)
479 		return 1;
480 
481 	assert(elt->dt_drv == candidate->dt_drv);
482 	return 0;
483 }
484 
485 /*
486  * Return TEE_SUCCESS if compatible found
487  *	  TEE_ERROR_OUT_OF_MEMORY if heap is exhausted
488  */
489 static TEE_Result add_node_to_probe(const void *fdt, int node,
490 				    const struct dt_driver *dt_drv,
491 				    const struct dt_device_match *dm)
492 {
493 	const char __maybe_unused *node_name = fdt_get_name(fdt, node, NULL);
494 	const char __maybe_unused *drv_name = dt_drv->name;
495 	struct dt_driver_probe *elt = NULL;
496 	struct dt_driver_probe elt_new = {
497 		.dm = dm,
498 		.dt_drv = dt_drv,
499 		.nodeoffset = node,
500 		.type = dt_drv->type,
501 	};
502 
503 	/* If node/type found in probe list or ready list, nothing to do */
504 	TAILQ_FOREACH(elt, &dt_driver_probe_list, link)
505 		if (!driver_probe_compare(&elt_new, elt))
506 			return TEE_SUCCESS;
507 
508 	TAILQ_FOREACH(elt, &dt_driver_ready_list, link)
509 		if (!driver_probe_compare(&elt_new, elt))
510 			return TEE_SUCCESS;
511 
512 	elt = malloc(sizeof(*elt));
513 	if (!elt)
514 		return TEE_ERROR_OUT_OF_MEMORY;
515 
516 	DMSG("element: %s on node %s", node_name, drv_name);
517 
518 	memcpy(elt, &elt_new, sizeof(*elt));
519 
520 	added_node = true;
521 
522 	TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link);
523 
524 	/* Hack here for TRACE_DEBUG messages on current probe list elements */
525 	if (TRACE_LEVEL >= TRACE_FLOW)
526 		print_probe_list(fdt);
527 
528 	return TEE_SUCCESS;
529 }
530 
531 /*
532  * Add a node to the probe list if a dt_driver matches target compatible.
533  *
534  * If @type is DT_DRIVER_ANY, probe list can hold only 1 driver to probe for
535  * the node. A node may probe several drivers if have a unique driver type.
536  *
537  * Return TEE_SUCCESS if compatible found
538  *	  TEE_ERROR_ITEM_NOT_FOUND if no matching driver
539  *	  TEE_ERROR_OUT_OF_MEMORY if heap is exhausted
540  */
541 static TEE_Result add_probe_node_by_compat(const void *fdt, int node,
542 					   const char *compat)
543 {
544 	TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND;
545 	const struct dt_driver *dt_drv = NULL;
546 	const struct dt_device_match *dm = NULL;
547 	uint32_t found_types = 0;
548 
549 	for_each_dt_driver(dt_drv) {
550 		for (dm = dt_drv->match_table; dm && dm->compatible; dm++) {
551 			if (strcmp(dm->compatible, compat) == 0) {
552 				assert(dt_drv->type < 32);
553 
554 				res = add_node_to_probe(fdt, node, dt_drv, dm);
555 				if (res)
556 					return res;
557 
558 				if (found_types & BIT(dt_drv->type)) {
559 					EMSG("Driver %s multi hit on type %u",
560 					     dt_drv->name, dt_drv->type);
561 					panic();
562 				}
563 				found_types |= BIT(dt_drv->type);
564 
565 				/* Matching found for this driver, try next */
566 				break;
567 			}
568 		}
569 	}
570 
571 	return res;
572 }
573 
574 /*
575  * Add the node to the probe list if matching compatible drivers are found.
576  * Follow node's compatible property list ordering to find matching driver.
577  */
578 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int node)
579 {
580 	int idx = 0;
581 	int len = 0;
582 	int count = 0;
583 	const char *compat = NULL;
584 	TEE_Result res = TEE_ERROR_GENERIC;
585 
586 	if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED)
587 		return TEE_SUCCESS;
588 
589 	count = fdt_stringlist_count(fdt, node, "compatible");
590 	if (count < 0)
591 		return TEE_SUCCESS;
592 
593 	for (idx = 0; idx < count; idx++) {
594 		compat = fdt_stringlist_get(fdt, node, "compatible", idx, &len);
595 		assert(compat && len > 0);
596 
597 		res = add_probe_node_by_compat(fdt, node, compat);
598 
599 		/* Stop lookup if something was found */
600 		if (res != TEE_ERROR_ITEM_NOT_FOUND)
601 			return res;
602 	}
603 
604 	return TEE_SUCCESS;
605 }
606 
607 static void parse_node(const void *fdt, int node)
608 {
609 	TEE_Result __maybe_unused res = TEE_ERROR_GENERIC;
610 	int subnode = 0;
611 
612 	fdt_for_each_subnode(subnode, fdt, node) {
613 		res = dt_driver_maybe_add_probe_node(fdt, subnode);
614 		if (res) {
615 			EMSG("Failed on node %s with %#"PRIx32,
616 			     fdt_get_name(fdt, subnode, NULL), res);
617 			panic();
618 		}
619 
620 		/*
621 		 * Rescursively parse the FDT, skipping disabled nodes.
622 		 * FDT is expected reliable and core shall have sufficient
623 		 * stack depth to possibly parse all DT nodes.
624 		 */
625 		if (IS_ENABLED(CFG_DRIVERS_DT_RECURSIVE_PROBE)) {
626 			if (_fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED)
627 				continue;
628 
629 			parse_node(fdt, subnode);
630 		}
631 	}
632 }
633 
634 /*
635  * Parse FDT for nodes and save in probe list the node for which a dt_driver
636  * matches node's compatible property.
637  */
638 static TEE_Result probe_dt_drivers(void)
639 {
640 	const void *fdt = NULL;
641 
642 	if (!IS_ENABLED(CFG_EMBED_DTB))
643 		return TEE_SUCCESS;
644 
645 	fdt = get_embedded_dt();
646 	assert(fdt);
647 
648 	parse_node(fdt, fdt_path_offset(fdt, "/"));
649 
650 	return process_probe_list(fdt);
651 }
652 
653 driver_init(probe_dt_drivers);
654