fconf_dyn_cfg_getter.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2019-2023, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <common/fdt_wrappers.h>
  9. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  10. #include <lib/object_pool.h>
  11. #include <libfdt.h>
  12. #include <platform_def.h>
  13. /* We currently use FW, TB_FW, SOC_FW, TOS_FW, NT_FW and HW configs */
  14. #define MAX_DTB_INFO U(6)
  15. /*
  16. * Compile time assert if FW_CONFIG_ID is 0 which is more
  17. * unlikely as 0 is a valid image ID for FIP as per the current
  18. * code but still to avoid code breakage in case of unlikely
  19. * event when image IDs get changed.
  20. */
  21. CASSERT(FW_CONFIG_ID != U(0), assert_invalid_fw_config_id);
  22. static struct dyn_cfg_dtb_info_t dtb_infos[MAX_DTB_INFO];
  23. static OBJECT_POOL_ARRAY(dtb_info_pool, dtb_infos);
  24. /*
  25. * This function is used to alloc memory for config information from
  26. * global pool and set the configuration information.
  27. */
  28. void set_config_info(uintptr_t config_addr, uintptr_t secondary_config_addr,
  29. uint32_t config_max_size,
  30. unsigned int config_id)
  31. {
  32. struct dyn_cfg_dtb_info_t *dtb_info;
  33. dtb_info = pool_alloc(&dtb_info_pool);
  34. dtb_info->config_addr = config_addr;
  35. dtb_info->secondary_config_addr = secondary_config_addr;
  36. dtb_info->config_max_size = config_max_size;
  37. dtb_info->config_id = config_id;
  38. }
  39. /* Get index of the config_id image */
  40. unsigned int dyn_cfg_dtb_info_get_index(unsigned int config_id)
  41. {
  42. unsigned int index;
  43. /* Positions index to the proper config-id */
  44. for (index = 0U; index < MAX_DTB_INFO; index++) {
  45. if (dtb_infos[index].config_id == config_id) {
  46. return index;
  47. }
  48. }
  49. return FCONF_INVALID_IDX;
  50. }
  51. struct dyn_cfg_dtb_info_t *dyn_cfg_dtb_info_getter(unsigned int config_id)
  52. {
  53. /* Positions index to the proper config-id */
  54. unsigned int index = dyn_cfg_dtb_info_get_index(config_id);
  55. if (index < MAX_DTB_INFO) {
  56. return &dtb_infos[index];
  57. }
  58. WARN("FCONF: Invalid config id %u\n", config_id);
  59. return NULL;
  60. }
  61. int fconf_populate_dtb_registry(uintptr_t config)
  62. {
  63. int rc;
  64. int node, child;
  65. /* As libfdt use void *, we can't avoid this cast */
  66. const void *dtb = (void *)config;
  67. /*
  68. * In case of BL1, fw_config dtb information is already
  69. * populated in global dtb_infos array by 'set_config_info'
  70. * function, Below check is present to avoid re-population of
  71. * fw_config information.
  72. *
  73. * Other BLs, satisfy below check and populate fw_config information
  74. * in global dtb_infos array.
  75. */
  76. if (dtb_infos[0].config_id == 0U) {
  77. uint32_t config_max_size = fdt_totalsize(dtb);
  78. set_config_info(config, ~0UL, config_max_size, FW_CONFIG_ID);
  79. }
  80. /* Find the node offset point to "fconf,dyn_cfg-dtb_registry" compatible property */
  81. const char *compatible_str = "fconf,dyn_cfg-dtb_registry";
  82. node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
  83. if (node < 0) {
  84. ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
  85. return node;
  86. }
  87. fdt_for_each_subnode(child, dtb, node) {
  88. uint32_t config_max_size, config_id;
  89. uintptr_t config_addr;
  90. uintptr_t secondary_config_addr = ~0UL;
  91. uint64_t val64;
  92. /* Read configuration dtb information */
  93. rc = fdt_read_uint64(dtb, child, "load-address", &val64);
  94. if (rc < 0) {
  95. ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
  96. return rc;
  97. }
  98. config_addr = (uintptr_t)val64;
  99. rc = fdt_read_uint32(dtb, child, "max-size", &config_max_size);
  100. if (rc < 0) {
  101. ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
  102. return rc;
  103. }
  104. rc = fdt_read_uint32(dtb, child, "id", &config_id);
  105. if (rc < 0) {
  106. ERROR("FCONF: Incomplete configuration property in dtb-registry.\n");
  107. return rc;
  108. }
  109. VERBOSE("FCONF: dyn_cfg.dtb_registry cell found with:\n");
  110. VERBOSE("\tload-address = %lx\n", config_addr);
  111. VERBOSE("\tmax-size = 0x%x\n", config_max_size);
  112. VERBOSE("\tconfig-id = %u\n", config_id);
  113. rc = fdt_read_uint64(dtb, child, "secondary-load-address",
  114. &val64);
  115. if (rc == 0) {
  116. secondary_config_addr = (uintptr_t)val64;
  117. VERBOSE("\tsecondary-load-address = %lx\n",
  118. secondary_config_addr);
  119. }
  120. set_config_info(config_addr, secondary_config_addr,
  121. config_max_size, config_id);
  122. }
  123. if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
  124. ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, node);
  125. return child;
  126. }
  127. return 0;
  128. }
  129. FCONF_REGISTER_POPULATOR(FW_CONFIG, dyn_cfg, fconf_populate_dtb_registry);