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