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