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