fdt_wrappers.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /* Helper functions to offer easier navigation of Device Tree Blob */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <inttypes.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <libfdt.h>
  13. #include <common/debug.h>
  14. #include <common/fdt_wrappers.h>
  15. #include <common/uuid.h>
  16. /*
  17. * Read cells from a given property of the given node. Any number of 32-bit
  18. * cells of the property can be read. Returns 0 on success, or a negative
  19. * FDT error value otherwise.
  20. */
  21. int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
  22. unsigned int cells, uint32_t *value)
  23. {
  24. const fdt32_t *prop;
  25. int value_len;
  26. assert(dtb != NULL);
  27. assert(prop_name != NULL);
  28. assert(value != NULL);
  29. assert(node >= 0);
  30. /* Access property and obtain its length (in bytes) */
  31. prop = fdt_getprop(dtb, node, prop_name, &value_len);
  32. if (prop == NULL) {
  33. VERBOSE("Couldn't find property %s in dtb\n", prop_name);
  34. return -FDT_ERR_NOTFOUND;
  35. }
  36. /* Verify that property length can fill the entire array. */
  37. if (NCELLS((unsigned int)value_len) < cells) {
  38. WARN("Property length mismatch\n");
  39. return -FDT_ERR_BADVALUE;
  40. }
  41. for (unsigned int i = 0U; i < cells; i++) {
  42. value[i] = fdt32_to_cpu(prop[i]);
  43. }
  44. return 0;
  45. }
  46. int fdt_read_uint32(const void *dtb, int node, const char *prop_name,
  47. uint32_t *value)
  48. {
  49. return fdt_read_uint32_array(dtb, node, prop_name, 1, value);
  50. }
  51. uint32_t fdt_read_uint32_default(const void *dtb, int node,
  52. const char *prop_name, uint32_t dflt_value)
  53. {
  54. uint32_t ret = dflt_value;
  55. int err = fdt_read_uint32(dtb, node, prop_name, &ret);
  56. if (err < 0) {
  57. return dflt_value;
  58. }
  59. return ret;
  60. }
  61. int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
  62. uint64_t *value)
  63. {
  64. uint32_t array[2] = {0, 0};
  65. int ret;
  66. ret = fdt_read_uint32_array(dtb, node, prop_name, 2, array);
  67. if (ret < 0) {
  68. return ret;
  69. }
  70. *value = ((uint64_t)array[0] << 32) | array[1];
  71. return 0;
  72. }
  73. uint64_t fdt_read_uint64_default(const void *dtb, int node,
  74. const char *prop_name, uint64_t dflt_value)
  75. {
  76. uint64_t ret = dflt_value;
  77. int err = fdt_read_uint64(dtb, node, prop_name, &ret);
  78. if (err < 0) {
  79. return dflt_value;
  80. }
  81. return ret;
  82. }
  83. /*
  84. * Read bytes from a given property of the given node. Any number of
  85. * bytes of the property can be read. The fdt pointer is updated.
  86. * Returns 0 on success, and -1 on error.
  87. */
  88. int fdtw_read_bytes(const void *dtb, int node, const char *prop,
  89. unsigned int length, void *value)
  90. {
  91. const void *ptr;
  92. int value_len;
  93. assert(dtb != NULL);
  94. assert(prop != NULL);
  95. assert(value != NULL);
  96. assert(node >= 0);
  97. /* Access property and obtain its length (in bytes) */
  98. ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
  99. &value_len);
  100. if (ptr == NULL) {
  101. WARN("Couldn't find property %s in dtb\n", prop);
  102. return -1;
  103. }
  104. /* Verify that property length is not less than number of bytes */
  105. if ((unsigned int)value_len < length) {
  106. WARN("Property length mismatch\n");
  107. return -1;
  108. }
  109. (void)memcpy(value, ptr, length);
  110. return 0;
  111. }
  112. /*
  113. * Read string from a given property of the given node. Up to 'size - 1'
  114. * characters are read, and a NUL terminator is added. Returns 0 on success,
  115. * and -1 upon error.
  116. */
  117. int fdtw_read_string(const void *dtb, int node, const char *prop,
  118. char *str, size_t size)
  119. {
  120. const char *ptr;
  121. size_t len;
  122. assert(dtb != NULL);
  123. assert(node >= 0);
  124. assert(prop != NULL);
  125. assert(str != NULL);
  126. assert(size > 0U);
  127. ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
  128. if (ptr == NULL) {
  129. WARN("Couldn't find property %s in dtb\n", prop);
  130. return -1;
  131. }
  132. len = strlcpy(str, ptr, size);
  133. if (len >= size) {
  134. WARN("String of property %s in dtb has been truncated\n", prop);
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. /*
  140. * Read UUID from a given property of the given node. Returns 0 on success,
  141. * and a negative value upon error.
  142. */
  143. int fdtw_read_uuid(const void *dtb, int node, const char *prop,
  144. unsigned int length, uint8_t *uuid)
  145. {
  146. /* Buffer for UUID string (plus NUL terminator) */
  147. char uuid_string[UUID_STRING_LENGTH + 1U];
  148. int err;
  149. assert(dtb != NULL);
  150. assert(prop != NULL);
  151. assert(uuid != NULL);
  152. assert(node >= 0);
  153. if (length < UUID_BYTES_LENGTH) {
  154. return -EINVAL;
  155. }
  156. err = fdtw_read_string(dtb, node, prop, uuid_string,
  157. UUID_STRING_LENGTH + 1U);
  158. if (err != 0) {
  159. return err;
  160. }
  161. if (read_uuid(uuid, uuid_string) != 0) {
  162. return -FDT_ERR_BADVALUE;
  163. }
  164. return 0;
  165. }
  166. /*
  167. * Write cells in place to a given property of the given node. At most 2 cells
  168. * of the property are written. Returns 0 on success, and -1 upon error.
  169. */
  170. int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
  171. unsigned int cells, void *value)
  172. {
  173. int err, len;
  174. assert(dtb != NULL);
  175. assert(prop != NULL);
  176. assert(value != NULL);
  177. assert(node >= 0);
  178. /* We expect either 1 or 2 cell property */
  179. assert(cells <= 2U);
  180. if (cells == 2U)
  181. *(fdt64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
  182. else
  183. *(fdt32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
  184. len = (int)cells * 4;
  185. /* Set property value in place */
  186. err = fdt_setprop_inplace(dtb, node, prop, value, len);
  187. if (err != 0) {
  188. WARN("Modify property %s failed with error %d\n", prop, err);
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. /*
  194. * Write bytes in place to a given property of the given node.
  195. * Any number of bytes of the property can be written.
  196. * Returns 0 on success, and < 0 on error.
  197. */
  198. int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
  199. unsigned int length, const void *data)
  200. {
  201. const void *ptr;
  202. int namelen, value_len, err;
  203. assert(dtb != NULL);
  204. assert(prop != NULL);
  205. assert(data != NULL);
  206. assert(node >= 0);
  207. namelen = (int)strlen(prop);
  208. /* Access property and obtain its length in bytes */
  209. ptr = fdt_getprop_namelen(dtb, node, prop, namelen, &value_len);
  210. if (ptr == NULL) {
  211. WARN("Couldn't find property %s in dtb\n", prop);
  212. return -1;
  213. }
  214. /* Verify that property length is not less than number of bytes */
  215. if ((unsigned int)value_len < length) {
  216. WARN("Property length mismatch\n");
  217. return -1;
  218. }
  219. /* Set property value in place */
  220. err = fdt_setprop_inplace_namelen_partial(dtb, node, prop,
  221. namelen, 0,
  222. data, (int)length);
  223. if (err != 0) {
  224. WARN("Set property %s failed with error %d\n", prop, err);
  225. }
  226. return err;
  227. }
  228. static uint64_t fdt_read_prop_cells(const fdt32_t *prop, int nr_cells)
  229. {
  230. uint64_t reg = fdt32_to_cpu(prop[0]);
  231. if (nr_cells > 1) {
  232. reg = (reg << 32) | fdt32_to_cpu(prop[1]);
  233. }
  234. return reg;
  235. }
  236. int fdt_get_reg_props_by_index(const void *dtb, int node, int index,
  237. uintptr_t *base, size_t *size)
  238. {
  239. const fdt32_t *prop;
  240. int parent, len;
  241. int ac, sc;
  242. int cell;
  243. parent = fdt_parent_offset(dtb, node);
  244. if (parent < 0) {
  245. return -FDT_ERR_BADOFFSET;
  246. }
  247. ac = fdt_address_cells(dtb, parent);
  248. sc = fdt_size_cells(dtb, parent);
  249. cell = index * (ac + sc);
  250. prop = fdt_getprop(dtb, node, "reg", &len);
  251. if (prop == NULL) {
  252. WARN("Couldn't find \"reg\" property in dtb\n");
  253. return -FDT_ERR_NOTFOUND;
  254. }
  255. if (((cell + ac + sc) * (int)sizeof(uint32_t)) > len) {
  256. return -FDT_ERR_BADVALUE;
  257. }
  258. if (base != NULL) {
  259. *base = (uintptr_t)fdt_read_prop_cells(&prop[cell], ac);
  260. }
  261. if (size != NULL) {
  262. *size = (size_t)fdt_read_prop_cells(&prop[cell + ac], sc);
  263. }
  264. return 0;
  265. }
  266. /*******************************************************************************
  267. * This function fills reg node info (base & size) with an index found by
  268. * checking the reg-names node.
  269. * Returns 0 on success and a negative FDT error code on failure.
  270. ******************************************************************************/
  271. int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name,
  272. uintptr_t *base, size_t *size)
  273. {
  274. int index;
  275. index = fdt_stringlist_search(dtb, node, "reg-names", name);
  276. if (index < 0) {
  277. return index;
  278. }
  279. return fdt_get_reg_props_by_index(dtb, node, index, base, size);
  280. }
  281. /*******************************************************************************
  282. * This function gets the stdout path node.
  283. * It reads the value indicated inside the device tree.
  284. * Returns node offset on success and a negative FDT error code on failure.
  285. ******************************************************************************/
  286. int fdt_get_stdout_node_offset(const void *dtb)
  287. {
  288. int node;
  289. const char *prop, *path;
  290. int len;
  291. /* The /secure-chosen node takes precedence over the standard one. */
  292. node = fdt_path_offset(dtb, "/secure-chosen");
  293. if (node < 0) {
  294. node = fdt_path_offset(dtb, "/chosen");
  295. if (node < 0) {
  296. return -FDT_ERR_NOTFOUND;
  297. }
  298. }
  299. prop = fdt_getprop(dtb, node, "stdout-path", NULL);
  300. if (prop == NULL) {
  301. return -FDT_ERR_NOTFOUND;
  302. }
  303. /* Determine the actual path length, as a colon terminates the path. */
  304. path = strchr(prop, ':');
  305. if (path == NULL) {
  306. len = strlen(prop);
  307. } else {
  308. len = path - prop;
  309. }
  310. /* Aliases cannot start with a '/', so it must be the actual path. */
  311. if (prop[0] == '/') {
  312. return fdt_path_offset_namelen(dtb, prop, len);
  313. }
  314. /* Lookup the alias, as this contains the actual path. */
  315. path = fdt_get_alias_namelen(dtb, prop, len);
  316. if (path == NULL) {
  317. return -FDT_ERR_NOTFOUND;
  318. }
  319. return fdt_path_offset(dtb, path);
  320. }
  321. /*******************************************************************************
  322. * Only devices which are direct children of root node use CPU address domain.
  323. * All other devices use addresses that are local to the device node and cannot
  324. * directly used by CPU. Device tree provides an address translation mechanism
  325. * through "ranges" property which provides mappings from local address space to
  326. * parent address space. Since a device could be a child of a child node to the
  327. * root node, there can be more than one level of address translation needed to
  328. * map the device local address space to CPU address space.
  329. * fdtw_translate_address() API performs address translation of a local address
  330. * to a global address with help of various helper functions.
  331. ******************************************************************************/
  332. static bool fdtw_xlat_hit(const fdt32_t *value, int child_addr_size,
  333. int parent_addr_size, int range_size, uint64_t base_address,
  334. uint64_t *translated_addr)
  335. {
  336. uint64_t local_address, parent_address, addr_range;
  337. local_address = fdt_read_prop_cells(value, child_addr_size);
  338. parent_address = fdt_read_prop_cells(value + child_addr_size,
  339. parent_addr_size);
  340. addr_range = fdt_read_prop_cells(value + child_addr_size +
  341. parent_addr_size,
  342. range_size);
  343. VERBOSE("DT: Address %" PRIx64 " mapped to %" PRIx64 " with range %" PRIx64 "\n",
  344. local_address, parent_address, addr_range);
  345. /* Perform range check */
  346. if ((base_address < local_address) ||
  347. (base_address >= local_address + addr_range)) {
  348. return false;
  349. }
  350. /* Found hit for the addr range that needs to be translated */
  351. *translated_addr = parent_address + (base_address - local_address);
  352. VERBOSE("DT: child address %" PRIx64 "mapped to %" PRIx64 " in parent bus\n",
  353. local_address, parent_address);
  354. return true;
  355. }
  356. #define ILLEGAL_ADDR ULL(~0)
  357. static uint64_t fdtw_search_all_xlat_entries(const void *dtb,
  358. const struct fdt_property *ranges_prop,
  359. int local_bus, uint64_t base_address)
  360. {
  361. uint64_t translated_addr;
  362. const fdt32_t *next_entry;
  363. int parent_bus_node, nxlat_entries, length;
  364. int self_addr_cells, parent_addr_cells, self_size_cells, ncells_xlat;
  365. /*
  366. * The number of cells in one translation entry in ranges is the sum of
  367. * the following values:
  368. * self#address-cells + parent#address-cells + self#size-cells
  369. * Ex: the iofpga ranges property has one translation entry with 4 cells
  370. * They represent iofpga#addr-cells + motherboard#addr-cells + iofpga#size-cells
  371. * = 1 + 2 + 1
  372. */
  373. parent_bus_node = fdt_parent_offset(dtb, local_bus);
  374. self_addr_cells = fdt_address_cells(dtb, local_bus);
  375. self_size_cells = fdt_size_cells(dtb, local_bus);
  376. parent_addr_cells = fdt_address_cells(dtb, parent_bus_node);
  377. /* Number of cells per translation entry i.e., mapping */
  378. ncells_xlat = self_addr_cells + parent_addr_cells + self_size_cells;
  379. assert(ncells_xlat > 0);
  380. /*
  381. * Find the number of translations(mappings) specified in the current
  382. * `ranges` property. Note that length represents number of bytes and
  383. * is stored in big endian mode.
  384. */
  385. length = fdt32_to_cpu(ranges_prop->len);
  386. nxlat_entries = (length/sizeof(uint32_t))/ncells_xlat;
  387. assert(nxlat_entries > 0);
  388. next_entry = (const fdt32_t *)ranges_prop->data;
  389. /* Iterate over the entries in the "ranges" */
  390. for (int i = 0; i < nxlat_entries; i++) {
  391. if (fdtw_xlat_hit(next_entry, self_addr_cells,
  392. parent_addr_cells, self_size_cells, base_address,
  393. &translated_addr)){
  394. return translated_addr;
  395. }
  396. next_entry = next_entry + ncells_xlat;
  397. }
  398. INFO("DT: No translation found for address %" PRIx64 " in node %s\n",
  399. base_address, fdt_get_name(dtb, local_bus, NULL));
  400. return ILLEGAL_ADDR;
  401. }
  402. /*******************************************************************************
  403. * address mapping needs to be done recursively starting from current node to
  404. * root node through all intermediate parent nodes.
  405. * Sample device tree is shown here:
  406. smb@0,0 {
  407. compatible = "simple-bus";
  408. #address-cells = <2>;
  409. #size-cells = <1>;
  410. ranges = <0 0 0 0x08000000 0x04000000>,
  411. <1 0 0 0x14000000 0x04000000>,
  412. <2 0 0 0x18000000 0x04000000>,
  413. <3 0 0 0x1c000000 0x04000000>,
  414. <4 0 0 0x0c000000 0x04000000>,
  415. <5 0 0 0x10000000 0x04000000>;
  416. motherboard {
  417. arm,v2m-memory-map = "rs1";
  418. compatible = "arm,vexpress,v2m-p1", "simple-bus";
  419. #address-cells = <2>;
  420. #size-cells = <1>;
  421. ranges;
  422. iofpga@3,00000000 {
  423. compatible = "arm,amba-bus", "simple-bus";
  424. #address-cells = <1>;
  425. #size-cells = <1>;
  426. ranges = <0 3 0 0x200000>;
  427. v2m_serial1: uart@a0000 {
  428. compatible = "arm,pl011", "arm,primecell";
  429. reg = <0x0a0000 0x1000>;
  430. interrupts = <0 6 4>;
  431. clocks = <&v2m_clk24mhz>, <&v2m_clk24mhz>;
  432. clock-names = "uartclk", "apb_pclk";
  433. };
  434. };
  435. };
  436. * As seen above, there are 3 levels of address translations needed. An empty
  437. * `ranges` property denotes identity mapping (as seen in `motherboard` node).
  438. * Each ranges property can map a set of child addresses to parent bus. Hence
  439. * there can be more than 1 (translation) entry in the ranges property as seen
  440. * in the `smb` node which has 6 translation entries.
  441. ******************************************************************************/
  442. /* Recursive implementation */
  443. uint64_t fdtw_translate_address(const void *dtb, int node,
  444. uint64_t base_address)
  445. {
  446. int length, local_bus_node;
  447. const char *node_name;
  448. uint64_t global_address;
  449. local_bus_node = fdt_parent_offset(dtb, node);
  450. node_name = fdt_get_name(dtb, local_bus_node, NULL);
  451. /*
  452. * In the example given above, starting from the leaf node:
  453. * uart@a000 represents the current node
  454. * iofpga@3,00000000 represents the local bus
  455. * motherboard represents the parent bus
  456. */
  457. /* Read the ranges property */
  458. const struct fdt_property *property = fdt_get_property(dtb,
  459. local_bus_node, "ranges", &length);
  460. if (property == NULL) {
  461. if (local_bus_node == 0) {
  462. /*
  463. * root node doesn't have range property as addresses
  464. * are in CPU address space.
  465. */
  466. return base_address;
  467. }
  468. INFO("DT: Couldn't find ranges property in node %s\n",
  469. node_name);
  470. return ILLEGAL_ADDR;
  471. } else if (length == 0) {
  472. /* empty ranges indicates identity map to parent bus */
  473. return fdtw_translate_address(dtb, local_bus_node, base_address);
  474. }
  475. VERBOSE("DT: Translation lookup in node %s at offset %d\n", node_name,
  476. local_bus_node);
  477. global_address = fdtw_search_all_xlat_entries(dtb, property,
  478. local_bus_node, base_address);
  479. if (global_address == ILLEGAL_ADDR) {
  480. return ILLEGAL_ADDR;
  481. }
  482. /* Translate the local device address recursively */
  483. return fdtw_translate_address(dtb, local_bus_node, global_address);
  484. }
  485. /*
  486. * For every CPU node (`/cpus/cpu@n`) in an FDT, execute a callback passing a
  487. * pointer to the FDT and the offset of the CPU node. If the return value of the
  488. * callback is negative, it is treated as an error and the loop is aborted. In
  489. * this situation, the value of the callback is returned from the function.
  490. *
  491. * Returns `0` on success, or a negative integer representing an error code.
  492. */
  493. int fdtw_for_each_cpu(const void *dtb,
  494. int (*callback)(const void *dtb, int node, uintptr_t mpidr))
  495. {
  496. int ret = 0;
  497. int parent, node = 0;
  498. parent = fdt_path_offset(dtb, "/cpus");
  499. if (parent < 0) {
  500. return parent;
  501. }
  502. fdt_for_each_subnode(node, dtb, parent) {
  503. const char *name;
  504. int len;
  505. uintptr_t mpidr = 0U;
  506. name = fdt_get_name(dtb, node, &len);
  507. if (strncmp(name, "cpu@", 4) != 0) {
  508. continue;
  509. }
  510. ret = fdt_get_reg_props_by_index(dtb, node, 0, &mpidr, NULL);
  511. if (ret < 0) {
  512. break;
  513. }
  514. ret = callback(dtb, node, mpidr);
  515. if (ret < 0) {
  516. break;
  517. }
  518. }
  519. return ret;
  520. }
  521. /*
  522. * Find a given node in device tree. If not present, add it.
  523. * Returns offset of node found/added on success, and < 0 on error.
  524. */
  525. int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
  526. {
  527. int offset;
  528. offset = fdt_subnode_offset(fdt, parentoffset, name);
  529. if (offset == -FDT_ERR_NOTFOUND) {
  530. offset = fdt_add_subnode(fdt, parentoffset, name);
  531. }
  532. if (offset < 0) {
  533. ERROR("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
  534. }
  535. return offset;
  536. }