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 release_init_resource(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 DMSG("Property %s missing in node %s", prop_name, 249 fdt_get_name(fdt, nodeoffset, NULL)); 250 *res = TEE_ERROR_GENERIC; 251 return NULL; 252 } 253 254 while (idx < len) { 255 idx32 = idx / sizeof(uint32_t); 256 phandle = fdt32_to_cpu(prop[idx32]); 257 258 prv = dt_driver_get_provider_by_phandle(phandle); 259 if (!prv) { 260 /* No provider registered yet */ 261 *res = TEE_ERROR_DEFER_DRIVER_INIT; 262 return NULL; 263 } 264 265 prv_cells = dt_driver_provider_cells(prv); 266 if (prop_idx) { 267 prop_idx--; 268 idx += sizeof(phandle) + prv_cells * sizeof(uint32_t); 269 continue; 270 } 271 272 return device_from_provider_prop(prv, prop + idx32, res); 273 } 274 275 *res = TEE_ERROR_GENERIC; 276 return NULL; 277 } 278 279 static unsigned int __maybe_unused probe_list_count(void) 280 { 281 struct dt_driver_probe *elt = NULL; 282 unsigned int count = 0; 283 284 TAILQ_FOREACH(elt, &dt_driver_probe_list, link) 285 count++; 286 287 return count; 288 } 289 290 static void __maybe_unused print_probe_list(const void *fdt __maybe_unused) 291 { 292 struct dt_driver_probe *elt = NULL; 293 294 DMSG("Probe list: %u elements", probe_list_count()); 295 296 TAILQ_FOREACH(elt, &dt_driver_probe_list, link) 297 DMSG("- Driver %s probes on node %s", 298 elt->dt_drv->name, 299 fdt_get_name(fdt, elt->nodeoffset, NULL)); 300 301 DMSG("Probe list end"); 302 } 303 304 /* 305 * Probe element: push to ready list if succeeds, push to probe list if probe 306 * if deferred, panic with an error trace otherwise. 307 */ 308 static TEE_Result probe_driver_node(const void *fdt, 309 struct dt_driver_probe *elt) 310 { 311 TEE_Result res = TEE_ERROR_GENERIC; 312 const char __maybe_unused *drv_name = NULL; 313 const char __maybe_unused *node_name = NULL; 314 315 node_name = fdt_get_name(fdt, elt->nodeoffset, NULL); 316 drv_name = elt->dt_drv->name; 317 FMSG("Probing %s on node %s", drv_name, node_name); 318 319 res = elt->dt_drv->probe(fdt, elt->nodeoffset, elt->dm->compat_data); 320 switch (res) { 321 case TEE_SUCCESS: 322 TAILQ_INSERT_HEAD(&dt_driver_ready_list, elt, link); 323 324 DMSG("element: %s on node %s initialized", drv_name, node_name); 325 break; 326 case TEE_ERROR_DEFER_DRIVER_INIT: 327 elt->deferrals++; 328 TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link); 329 330 DMSG("element: %s on node %s deferred %u time(s)", drv_name, 331 node_name, elt->deferrals); 332 break; 333 default: 334 EMSG("Fail to probe %s on node %s: %#"PRIx32, 335 drv_name, node_name, res); 336 panic(); 337 } 338 339 return res; 340 } 341 342 static TEE_Result alloc_elt_and_probe(const void *fdt, int node, 343 const struct dt_driver *dt_drv, 344 const struct dt_device_match *dm) 345 { 346 struct dt_driver_probe *elt = NULL; 347 348 /* Will be freed when lists are released */ 349 elt = calloc(1, sizeof(*elt)); 350 if (!elt) 351 return TEE_ERROR_OUT_OF_MEMORY; 352 353 elt->nodeoffset = node; 354 elt->dt_drv = dt_drv; 355 elt->dm = dm; 356 elt->type = dt_drv->type; 357 358 return probe_driver_node(fdt, elt); 359 } 360 361 /* Lookup a compatible driver, possibly of a specific @type, for the FDT node */ 362 static TEE_Result probe_device_by_compat(const void *fdt, int node, 363 const char *compat, 364 enum dt_driver_type type) 365 { 366 const struct dt_driver *drv = NULL; 367 const struct dt_device_match *dm = NULL; 368 369 for_each_dt_driver(drv) { 370 if (drv->type != type) 371 continue; 372 373 for (dm = drv->match_table; dm && dm->compatible; dm++) 374 if (strcmp(dm->compatible, compat) == 0) 375 return alloc_elt_and_probe(fdt, node, drv, dm); 376 } 377 378 return TEE_ERROR_ITEM_NOT_FOUND; 379 } 380 381 /* 382 * Lookup the best matching compatible driver, possibly of a specific @type, 383 * for the FDT node. 384 */ 385 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset, 386 enum dt_driver_type type) 387 { 388 int idx = 0; 389 int len = 0; 390 int count = 0; 391 const char *compat = NULL; 392 TEE_Result res = TEE_ERROR_GENERIC; 393 394 assert_type_is_valid(type); 395 396 count = fdt_stringlist_count(fdt, nodeoffset, "compatible"); 397 if (count < 0) 398 return TEE_ERROR_ITEM_NOT_FOUND; 399 400 for (idx = 0; idx < count; idx++) { 401 compat = fdt_stringlist_get(fdt, nodeoffset, "compatible", 402 idx, &len); 403 if (!compat) 404 return TEE_ERROR_GENERIC; 405 406 res = probe_device_by_compat(fdt, nodeoffset, compat, type); 407 408 if (res != TEE_ERROR_ITEM_NOT_FOUND) 409 return res; 410 } 411 412 return TEE_ERROR_ITEM_NOT_FOUND; 413 } 414 415 static TEE_Result process_probe_list(const void *fdt) 416 { 417 struct dt_driver_probe *elt = NULL; 418 struct dt_driver_probe *prev = NULL; 419 unsigned int __maybe_unused loop_count = 0; 420 unsigned int __maybe_unused deferral_loop_count = 0; 421 bool __maybe_unused one_deferred = false; 422 bool one_probed_ok = false; 423 424 do { 425 loop_count++; 426 FMSG("Probe loop %u after %u for deferral(s)", loop_count, 427 deferral_loop_count); 428 429 /* Hack here for TRACE_DEBUG messages on probe list elements */ 430 if (TRACE_LEVEL >= TRACE_FLOW) 431 print_probe_list(fdt); 432 433 if (TAILQ_EMPTY(&dt_driver_probe_list)) 434 return TEE_SUCCESS; 435 436 /* 437 * Probe from current end to top. Deferred probed node are 438 * pushed back after current tail for the next probe round. 439 * Reset probe result flags and see status after probe round. 440 */ 441 one_deferred = false; 442 one_probed_ok = false; 443 added_node = false; 444 445 TAILQ_FOREACH_REVERSE_SAFE(elt, &dt_driver_probe_list, 446 dt_driver_probe_head, link, prev) { 447 TAILQ_REMOVE(&dt_driver_probe_list, elt, link); 448 449 switch (probe_driver_node(fdt, elt)) { 450 case TEE_SUCCESS: 451 one_probed_ok = true; 452 break; 453 case TEE_ERROR_DEFER_DRIVER_INIT: 454 one_deferred = true; 455 break; 456 default: 457 /* We don't expect error return codes */ 458 assert(0); 459 } 460 } 461 462 if (one_deferred) 463 deferral_loop_count++; 464 465 } while (added_node || one_probed_ok); 466 467 EMSG("Panic on unresolved dependencies after %u rounds, %u deferred:", 468 loop_count, deferral_loop_count); 469 470 TAILQ_FOREACH(elt, &dt_driver_probe_list, link) 471 EMSG("- %s on node %s", elt->dt_drv->name, 472 fdt_get_name(fdt, elt->nodeoffset, NULL)); 473 474 panic(); 475 } 476 477 static int driver_probe_compare(struct dt_driver_probe *candidate, 478 struct dt_driver_probe *elt) 479 { 480 if (candidate->nodeoffset != elt->nodeoffset || 481 candidate->type != elt->type) 482 return 1; 483 484 assert(elt->dt_drv == candidate->dt_drv); 485 return 0; 486 } 487 488 /* 489 * Return TEE_SUCCESS if compatible found 490 * TEE_ERROR_OUT_OF_MEMORY if heap is exhausted 491 */ 492 static TEE_Result add_node_to_probe(const void *fdt, int node, 493 const struct dt_driver *dt_drv, 494 const struct dt_device_match *dm) 495 { 496 const char __maybe_unused *node_name = fdt_get_name(fdt, node, NULL); 497 const char __maybe_unused *drv_name = dt_drv->name; 498 struct dt_driver_probe *elt = NULL; 499 struct dt_driver_probe elt_new = { 500 .dm = dm, 501 .dt_drv = dt_drv, 502 .nodeoffset = node, 503 .type = dt_drv->type, 504 }; 505 506 /* If node/type found in probe list or ready list, nothing to do */ 507 TAILQ_FOREACH(elt, &dt_driver_probe_list, link) 508 if (!driver_probe_compare(&elt_new, elt)) 509 return TEE_SUCCESS; 510 511 TAILQ_FOREACH(elt, &dt_driver_ready_list, link) 512 if (!driver_probe_compare(&elt_new, elt)) 513 return TEE_SUCCESS; 514 515 elt = malloc(sizeof(*elt)); 516 if (!elt) 517 return TEE_ERROR_OUT_OF_MEMORY; 518 519 DMSG("element: %s on node %s", node_name, drv_name); 520 521 memcpy(elt, &elt_new, sizeof(*elt)); 522 523 added_node = true; 524 525 TAILQ_INSERT_TAIL(&dt_driver_probe_list, elt, link); 526 527 /* Hack here for TRACE_DEBUG messages on current probe list elements */ 528 if (TRACE_LEVEL >= TRACE_FLOW) 529 print_probe_list(fdt); 530 531 return TEE_SUCCESS; 532 } 533 534 /* 535 * Add a node to the probe list if a dt_driver matches target compatible. 536 * 537 * If @type is DT_DRIVER_ANY, probe list can hold only 1 driver to probe for 538 * the node. A node may probe several drivers if have a unique driver type. 539 * 540 * Return TEE_SUCCESS if compatible found 541 * TEE_ERROR_ITEM_NOT_FOUND if no matching driver 542 * TEE_ERROR_OUT_OF_MEMORY if heap is exhausted 543 */ 544 static TEE_Result add_probe_node_by_compat(const void *fdt, int node, 545 const char *compat) 546 { 547 TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; 548 const struct dt_driver *dt_drv = NULL; 549 const struct dt_device_match *dm = NULL; 550 uint32_t found_types = 0; 551 552 for_each_dt_driver(dt_drv) { 553 for (dm = dt_drv->match_table; dm && dm->compatible; dm++) { 554 if (strcmp(dm->compatible, compat) == 0) { 555 assert(dt_drv->type < 32); 556 557 res = add_node_to_probe(fdt, node, dt_drv, dm); 558 if (res) 559 return res; 560 561 if (found_types & BIT(dt_drv->type)) { 562 EMSG("Driver %s multi hit on type %u", 563 dt_drv->name, dt_drv->type); 564 panic(); 565 } 566 found_types |= BIT(dt_drv->type); 567 568 /* Matching found for this driver, try next */ 569 break; 570 } 571 } 572 } 573 574 return res; 575 } 576 577 /* 578 * Add the node to the probe list if matching compatible drivers are found. 579 * Follow node's compatible property list ordering to find matching driver. 580 */ 581 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int node) 582 { 583 int idx = 0; 584 int len = 0; 585 int count = 0; 586 const char *compat = NULL; 587 TEE_Result res = TEE_ERROR_GENERIC; 588 589 if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED) 590 return TEE_SUCCESS; 591 592 count = fdt_stringlist_count(fdt, node, "compatible"); 593 if (count < 0) 594 return TEE_SUCCESS; 595 596 for (idx = 0; idx < count; idx++) { 597 compat = fdt_stringlist_get(fdt, node, "compatible", idx, &len); 598 assert(compat && len > 0); 599 600 res = add_probe_node_by_compat(fdt, node, compat); 601 602 /* Stop lookup if something was found */ 603 if (res != TEE_ERROR_ITEM_NOT_FOUND) 604 return res; 605 } 606 607 return TEE_SUCCESS; 608 } 609 610 static void parse_node(const void *fdt, int node) 611 { 612 TEE_Result __maybe_unused res = TEE_ERROR_GENERIC; 613 int subnode = 0; 614 615 fdt_for_each_subnode(subnode, fdt, node) { 616 res = dt_driver_maybe_add_probe_node(fdt, subnode); 617 if (res) { 618 EMSG("Failed on node %s with %#"PRIx32, 619 fdt_get_name(fdt, subnode, NULL), res); 620 panic(); 621 } 622 623 /* 624 * Rescursively parse the FDT, skipping disabled nodes. 625 * FDT is expected reliable and core shall have sufficient 626 * stack depth to possibly parse all DT nodes. 627 */ 628 if (IS_ENABLED(CFG_DRIVERS_DT_RECURSIVE_PROBE)) { 629 if (_fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED) 630 continue; 631 632 parse_node(fdt, subnode); 633 } 634 } 635 } 636 637 /* 638 * Parse FDT for nodes and save in probe list the node for which a dt_driver 639 * matches node's compatible property. 640 */ 641 static TEE_Result probe_dt_drivers(void) 642 { 643 const void *fdt = NULL; 644 645 if (!IS_ENABLED(CFG_EMBED_DTB)) 646 return TEE_SUCCESS; 647 648 fdt = get_embedded_dt(); 649 assert(fdt); 650 651 parse_node(fdt, fdt_path_offset(fdt, "/")); 652 653 return process_probe_list(fdt); 654 } 655 656 driver_init(probe_dt_drivers); 657 658 static TEE_Result release_probe_lists(void) 659 { 660 struct dt_driver_probe *elt = NULL; 661 struct dt_driver_probe *next = NULL; 662 const void * __maybe_unused fdt = NULL; 663 664 if (!IS_ENABLED(CFG_EMBED_DTB)) 665 return TEE_SUCCESS; 666 667 fdt = get_embedded_dt(); 668 669 assert(fdt && TAILQ_EMPTY(&dt_driver_probe_list)); 670 671 TAILQ_FOREACH_SAFE(elt, &dt_driver_ready_list, link, next) { 672 DMSG("element: %s on node %s", elt->dt_drv->name, 673 fdt_get_name(fdt, elt->nodeoffset, NULL)); 674 free(elt); 675 } 676 677 return TEE_SUCCESS; 678 } 679 680 release_init_resource(release_probe_lists); 681 682 /* 683 * Simple bus support: handy to parse subnodes 684 */ 685 static TEE_Result simple_bus_probe(const void *fdt, int node, 686 const void *compat_data __unused) 687 { 688 TEE_Result res = TEE_ERROR_GENERIC; 689 int subnode = 0; 690 691 fdt_for_each_subnode(subnode, fdt, node) { 692 res = dt_driver_maybe_add_probe_node(fdt, subnode); 693 if (res) { 694 EMSG("Failed on node %s with %#"PRIx32, 695 fdt_get_name(fdt, subnode, NULL), res); 696 panic(); 697 } 698 } 699 700 return TEE_SUCCESS; 701 } 702 703 static const struct dt_device_match simple_bus_match_table[] = { 704 { .compatible = "simple-bus" }, 705 { } 706 }; 707 708 const struct dt_driver simple_bus_dt_driver __dt_driver = { 709 .name = "simple-bus", 710 .match_table = simple_bus_match_table, 711 .probe = simple_bus_probe, 712 }; 713