fdt_wrappers.c 15 KB

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