fdt_wrappers.c 17 KB

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