fconf_ethosn_getter.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <common/debug.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <libfdt.h>
  11. #include <plat/arm/common/fconf_ethosn_getter.h>
  12. struct ethosn_config_t ethosn_config = {0};
  13. struct ethosn_sub_allocator_t {
  14. const char *name;
  15. size_t name_len;
  16. uint32_t stream_id;
  17. };
  18. static int fdt_node_read_reserved_memory_addr(const void *fdt,
  19. int dev_node,
  20. uint64_t *reserved_mem_addrs)
  21. {
  22. uintptr_t addr;
  23. uint32_t phandle;
  24. int err;
  25. int mem_node;
  26. err = fdt_read_uint32(fdt, dev_node, "memory-region", &phandle);
  27. if (err != 0) {
  28. ERROR("FCONF: Failed to get reserved memory phandle\n");
  29. return err;
  30. }
  31. mem_node = fdt_node_offset_by_phandle(fdt, phandle);
  32. if (mem_node < 0) {
  33. ERROR("FCONF: Failed to find reserved memory node from phandle\n");
  34. return mem_node;
  35. }
  36. err = fdt_get_reg_props_by_index(fdt, mem_node, 0U, &addr, NULL);
  37. if (err != 0) {
  38. ERROR("FCONF: Failed to read reserved memory address\n");
  39. return err;
  40. }
  41. *reserved_mem_addrs = addr;
  42. return 0;
  43. }
  44. static bool fdt_node_has_reserved_memory(const void *fdt, int dev_node)
  45. {
  46. return fdt_get_property(fdt, dev_node, "memory-region", NULL) != NULL;
  47. }
  48. static int fdt_node_get_iommus_stream_id(const void *fdt, int node, uint32_t *stream_id)
  49. {
  50. int err;
  51. uint32_t iommus_array[2] = {0U};
  52. err = fdt_read_uint32_array(fdt, node, "iommus", 2U, iommus_array);
  53. if (err) {
  54. return err;
  55. }
  56. *stream_id = iommus_array[1];
  57. return 0;
  58. }
  59. static int fdt_node_populate_sub_allocators(const void *fdt,
  60. int alloc_node,
  61. struct ethosn_sub_allocator_t *sub_allocators,
  62. size_t num_allocs)
  63. {
  64. int sub_node;
  65. size_t i;
  66. int err = -FDT_ERR_NOTFOUND;
  67. uint32_t found_sub_allocators = 0U;
  68. fdt_for_each_subnode(sub_node, fdt, alloc_node) {
  69. const char *node_name;
  70. if (!fdt_node_is_enabled(fdt, sub_node)) {
  71. /* Ignore disabled node */
  72. continue;
  73. }
  74. if (fdt_node_check_compatible(fdt, sub_node, "ethosn-memory") != 0) {
  75. continue;
  76. }
  77. node_name = fdt_get_name(fdt, sub_node, NULL);
  78. for (i = 0U; i < num_allocs; ++i) {
  79. if (strncmp(node_name, sub_allocators[i].name,
  80. sub_allocators[i].name_len) != 0) {
  81. continue;
  82. }
  83. err = fdt_node_get_iommus_stream_id(fdt, sub_node,
  84. &sub_allocators[i].stream_id);
  85. if (err) {
  86. ERROR("FCONF: Failed to get stream ID from sub-allocator %s\n",
  87. node_name);
  88. return err;
  89. }
  90. ++found_sub_allocators;
  91. /* Nothing more to do for this node */
  92. break;
  93. }
  94. /* Check that at least one of the sub-allocators matched */
  95. if (i == num_allocs) {
  96. ERROR("FCONF: Unknown sub-allocator %s\n", node_name);
  97. return -FDT_ERR_BADSTRUCTURE;
  98. }
  99. }
  100. if ((sub_node < 0) && (sub_node != -FDT_ERR_NOTFOUND)) {
  101. ERROR("FCONF: Failed to parse sub-allocators\n");
  102. return -FDT_ERR_BADSTRUCTURE;
  103. }
  104. if (err == -FDT_ERR_NOTFOUND) {
  105. ERROR("FCONF: No matching sub-allocator found\n");
  106. return err;
  107. }
  108. if (found_sub_allocators != num_allocs) {
  109. ERROR("FCONF: Not all sub-allocators were found\n");
  110. return -FDT_ERR_BADSTRUCTURE;
  111. }
  112. return 0;
  113. }
  114. static int fdt_node_populate_main_allocator(const void *fdt,
  115. int alloc_node,
  116. struct ethosn_main_allocator_t *allocator)
  117. {
  118. int err;
  119. struct ethosn_sub_allocator_t sub_allocators[] = {
  120. {.name = "firmware", .name_len = 8U},
  121. {.name = "working_data", .name_len = 12U}
  122. };
  123. err = fdt_node_populate_sub_allocators(fdt, alloc_node, sub_allocators,
  124. ARRAY_SIZE(sub_allocators));
  125. if (err) {
  126. return err;
  127. }
  128. allocator->firmware.stream_id = sub_allocators[0].stream_id;
  129. allocator->working_data.stream_id = sub_allocators[1].stream_id;
  130. return 0;
  131. }
  132. static int fdt_node_populate_asset_allocator(const void *fdt,
  133. int alloc_node,
  134. struct ethosn_asset_allocator_t *allocator)
  135. {
  136. int err;
  137. struct ethosn_sub_allocator_t sub_allocators[] = {
  138. {.name = "command_stream", .name_len = 14U},
  139. {.name = "weight_data", .name_len = 11U},
  140. {.name = "buffer_data", .name_len = 11U},
  141. {.name = "intermediate_data", .name_len = 17U}
  142. };
  143. err = fdt_node_populate_sub_allocators(fdt, alloc_node, sub_allocators,
  144. ARRAY_SIZE(sub_allocators));
  145. if (err) {
  146. return err;
  147. }
  148. allocator->command_stream.stream_id = sub_allocators[0].stream_id;
  149. allocator->weight_data.stream_id = sub_allocators[1].stream_id;
  150. allocator->buffer_data.stream_id = sub_allocators[2].stream_id;
  151. allocator->intermediate_data.stream_id = sub_allocators[3].stream_id;
  152. return 0;
  153. }
  154. static int fdt_node_populate_core(const void *fdt,
  155. int device_node,
  156. int core_node,
  157. bool has_reserved_memory,
  158. uint32_t core_index,
  159. struct ethosn_core_t *core)
  160. {
  161. int err;
  162. int sub_node;
  163. uintptr_t core_addr;
  164. err = fdt_get_reg_props_by_index(fdt, device_node, core_index,
  165. &core_addr, NULL);
  166. if (err < 0) {
  167. ERROR("FCONF: Failed to read reg property for NPU core %u\n",
  168. core_index);
  169. return err;
  170. }
  171. err = -FDT_ERR_NOTFOUND;
  172. fdt_for_each_subnode(sub_node, fdt, core_node) {
  173. if (!fdt_node_is_enabled(fdt, sub_node)) {
  174. continue;
  175. }
  176. if (fdt_node_check_compatible(fdt,
  177. sub_node,
  178. "ethosn-main_allocator") != 0) {
  179. continue;
  180. }
  181. if (has_reserved_memory) {
  182. ERROR("FCONF: Main allocator not supported when using reserved memory\n");
  183. return -FDT_ERR_BADSTRUCTURE;
  184. }
  185. if (err != -FDT_ERR_NOTFOUND) {
  186. ERROR("FCONF: NPU core 0x%lx has more than one main allocator\n",
  187. core_addr);
  188. return -FDT_ERR_BADSTRUCTURE;
  189. }
  190. err = fdt_node_populate_main_allocator(fdt, sub_node, &core->main_allocator);
  191. if (err) {
  192. ERROR("FCONF: Failed to parse main allocator for NPU core 0x%lx\n",
  193. core_addr);
  194. return err;
  195. }
  196. }
  197. if ((sub_node < 0) && (sub_node != -FDT_ERR_NOTFOUND)) {
  198. ERROR("FCONF: Failed to parse core sub nodes\n");
  199. return -FDT_ERR_BADSTRUCTURE;
  200. }
  201. if (!has_reserved_memory && err) {
  202. ERROR("FCONF: Main allocator not found for NPU core 0x%lx\n",
  203. core_addr);
  204. return err;
  205. }
  206. core->addr = core_addr;
  207. return 0;
  208. }
  209. int fconf_populate_ethosn_config(uintptr_t config)
  210. {
  211. int ethosn_node;
  212. uint32_t dev_count = 0U;
  213. const void *hw_conf_dtb = (const void *)config;
  214. INFO("Probing Arm(R) Ethos(TM)-N NPU\n");
  215. fdt_for_each_compatible_node(hw_conf_dtb, ethosn_node, "ethosn") {
  216. struct ethosn_device_t *dev = &ethosn_config.devices[dev_count];
  217. uint32_t dev_asset_alloc_count = 0U;
  218. uint32_t dev_core_count = 0U;
  219. uint64_t reserved_memory_addr = 0U;
  220. bool has_reserved_memory;
  221. int sub_node;
  222. int err;
  223. if (!fdt_node_is_enabled(hw_conf_dtb, ethosn_node)) {
  224. continue;
  225. }
  226. if (dev_count >= ETHOSN_DEV_NUM_MAX) {
  227. ERROR("FCONF: Reached max number of NPUs\n");
  228. return -FDT_ERR_BADSTRUCTURE;
  229. }
  230. has_reserved_memory = fdt_node_has_reserved_memory(hw_conf_dtb, ethosn_node);
  231. if (has_reserved_memory) {
  232. err = fdt_node_read_reserved_memory_addr(hw_conf_dtb,
  233. ethosn_node,
  234. &reserved_memory_addr);
  235. if (err != 0) {
  236. return err;
  237. }
  238. }
  239. fdt_for_each_subnode(sub_node, hw_conf_dtb, ethosn_node) {
  240. if (!fdt_node_is_enabled(hw_conf_dtb, sub_node)) {
  241. /* Ignore disabled sub node */
  242. continue;
  243. }
  244. if (fdt_node_check_compatible(hw_conf_dtb,
  245. sub_node,
  246. "ethosn-core") == 0) {
  247. if (dev_core_count >= ETHOSN_DEV_CORE_NUM_MAX) {
  248. ERROR("FCONF: Reached max number of NPU cores for NPU %u\n",
  249. dev_count);
  250. return -FDT_ERR_BADSTRUCTURE;
  251. }
  252. err = fdt_node_populate_core(hw_conf_dtb,
  253. ethosn_node,
  254. sub_node,
  255. has_reserved_memory,
  256. dev_core_count,
  257. &(dev->cores[dev_core_count]));
  258. if (err) {
  259. return err;
  260. }
  261. ++dev_core_count;
  262. } else if (fdt_node_check_compatible(hw_conf_dtb,
  263. sub_node,
  264. "ethosn-asset_allocator") == 0) {
  265. if (dev_asset_alloc_count >=
  266. ETHOSN_DEV_ASSET_ALLOCATOR_NUM_MAX) {
  267. ERROR("FCONF: Reached max number of asset allocators for NPU %u\n",
  268. dev_count);
  269. return -FDT_ERR_BADSTRUCTURE;
  270. }
  271. if (has_reserved_memory) {
  272. ERROR("FCONF: Asset allocator not supported when using reserved memory\n");
  273. return -FDT_ERR_BADSTRUCTURE;
  274. }
  275. err = fdt_node_populate_asset_allocator(hw_conf_dtb,
  276. sub_node,
  277. &(dev->asset_allocators[dev_asset_alloc_count]));
  278. if (err) {
  279. ERROR("FCONF: Failed to parse asset allocator for NPU %u\n",
  280. dev_count);
  281. return err;
  282. }
  283. ++dev_asset_alloc_count;
  284. }
  285. }
  286. if ((sub_node < 0) && (sub_node != -FDT_ERR_NOTFOUND)) {
  287. ERROR("FCONF: Failed to parse sub nodes for NPU %u\n",
  288. dev_count);
  289. return -FDT_ERR_BADSTRUCTURE;
  290. }
  291. if (dev_core_count == 0U) {
  292. ERROR("FCONF: NPU %u must have at least one enabled core\n",
  293. dev_count);
  294. return -FDT_ERR_BADSTRUCTURE;
  295. }
  296. if (!has_reserved_memory && dev_asset_alloc_count == 0U) {
  297. ERROR("FCONF: NPU %u must have at least one asset allocator\n",
  298. dev_count);
  299. return -FDT_ERR_BADSTRUCTURE;
  300. }
  301. dev->num_cores = dev_core_count;
  302. dev->num_allocators = dev_asset_alloc_count;
  303. dev->has_reserved_memory = has_reserved_memory;
  304. dev->reserved_memory_addr = reserved_memory_addr;
  305. ++dev_count;
  306. }
  307. if (dev_count == 0U) {
  308. ERROR("FCONF: Can't find 'ethosn' compatible node in dtb\n");
  309. return -FDT_ERR_BADSTRUCTURE;
  310. }
  311. ethosn_config.num_devices = dev_count;
  312. return 0;
  313. }
  314. FCONF_REGISTER_POPULATOR(HW_CONFIG, ethosn_config, fconf_populate_ethosn_config);