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