livetree.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. Driver Model with Live Device Tree
  2. ==================================
  3. Introduction
  4. ------------
  5. Traditionally U-Boot has used a 'flat' device tree. This means that it
  6. reads directly from the device tree binary structure. It is called a flat
  7. device tree because nodes are listed one after the other, with the
  8. hierarchy detected by tags in the format.
  9. This document describes U-Boot's support for a 'live' device tree, meaning
  10. that the tree is loaded into a hierarchical data structure within U-Boot.
  11. Motivation
  12. ----------
  13. The flat device tree has several advantages:
  14. - it is the format produced by the device tree compiler, so no translation
  15. is needed
  16. - it is fairly compact (e.g. there is no need for pointers)
  17. - it is accessed by the libfdt library, which is well tested and stable
  18. However the flat device tree does have some limitations. Adding new
  19. properties can involve copying large amounts of data around to make room.
  20. The overall tree has a fixed maximum size so sometimes the tree must be
  21. rebuilt in a new location to create more space. Even if not adding new
  22. properties or nodes, scanning the tree can be slow. For example, finding
  23. the parent of a node is a slow process. Reading from nodes involves a
  24. small amount parsing which takes a little time.
  25. Driver model scans the entire device tree sequentially on start-up which
  26. avoids the worst of the flat tree's limitations. But if the tree is to be
  27. modified at run-time, a live tree is much faster. Even if no modification
  28. is necessary, parsing the tree once and using a live tree from then on
  29. seems to save a little time.
  30. Implementation
  31. --------------
  32. In U-Boot a live device tree ('livetree') is currently supported only
  33. after relocation. Therefore we need a mechanism to specify a device
  34. tree node regardless of whether it is in the flat tree or livetree.
  35. The 'ofnode' type provides this. An ofnode can point to either a flat tree
  36. node (when the live tree node is not yet set up) or a livetree node. The
  37. caller of an ofnode function does not need to worry about these details.
  38. The main users of the information in a device tree are drivers. These have
  39. a 'struct udevice *' which is attached to a device tree node. Therefore it
  40. makes sense to be able to read device tree properties using the
  41. 'struct udevice *', rather than having to obtain the ofnode first.
  42. The 'dev_read_...()' interface provides this. It allows properties to be
  43. easily read from the device tree using only a device pointer. Under the
  44. hood it uses ofnode so it works with both flat and live device trees.
  45. Enabling livetree
  46. -----------------
  47. CONFIG_OF_LIVE enables livetree. When this option is enabled, the flat
  48. tree will be used in SPL and before relocation in U-Boot proper. Just
  49. before relocation a livetree is built, and this is used for U-Boot proper
  50. after relocation.
  51. Most checks for livetree use CONFIG_IS_ENABLED(OF_LIVE). This means that
  52. for SPL, the CONFIG_SPL_OF_LIVE option is checked. At present this does
  53. not exist, since SPL does not support livetree.
  54. Porting drivers
  55. ---------------
  56. Many existing drivers use the fdtdec interface to read device tree
  57. properties. This only works with a flat device tree. The drivers should be
  58. converted to use the dev_read_() interface.
  59. For example, the old code may be like this:
  60. struct udevice *bus;
  61. const void *blob = gd->fdt_blob;
  62. int node = dev_of_offset(bus);
  63. i2c_bus->regs = (struct i2c_ctlr *)devfdt_get_addr(dev);
  64. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 500000);
  65. The new code is:
  66. struct udevice *bus;
  67. i2c_bus->regs = (struct i2c_ctlr *)dev_read_addr(dev);
  68. plat->frequency = dev_read_u32_default(bus, "spi-max-frequency", 500000);
  69. The dev_read_...() interface is more convenient and works with both the
  70. flat and live device trees. See include/dm/read.h for a list of functions.
  71. Where properties must be read from sub-nodes or other nodes, you must fall
  72. back to using ofnode. For example, for old code like this:
  73. const void *blob = gd->fdt_blob;
  74. int subnode;
  75. fdt_for_each_subnode(subnode, blob, dev_of_offset(dev)) {
  76. freq = fdtdec_get_int(blob, node, "spi-max-frequency", 500000);
  77. ...
  78. }
  79. you should use:
  80. ofnode subnode;
  81. ofnode_for_each_subnode(subnode, dev_ofnode(dev)) {
  82. freq = ofnode_read_u32(node, "spi-max-frequency", 500000);
  83. ...
  84. }
  85. Useful ofnode functions
  86. -----------------------
  87. The internal data structures of the livetree are defined in include/dm/of.h :
  88. struct device_node - holds information about a device tree node
  89. struct property - holds information about a property within a node
  90. Nodes have pointers to their first property, their parent, their first child
  91. and their sibling. This allows nodes to be linked together in a hierarchical
  92. tree.
  93. Properties have pointers to the next property. This allows all properties of
  94. a node to be linked together in a chain.
  95. It should not be necessary to use these data structures in normal code. In
  96. particular, you should refrain from using functions which access the livetree
  97. directly, such as of_read_u32(). Use ofnode functions instead, to allow your
  98. code to work with a flat tree also.
  99. Some conversion functions are used internally. Generally these are not needed
  100. for driver code. Note that they will not work if called in the wrong context.
  101. For example it is invalid to call ofnode_to_no() when a flat tree is being
  102. used. Similarly it is not possible to call ofnode_to_offset() on a livetree
  103. node.
  104. ofnode_to_np() - converts ofnode to struct device_node *
  105. ofnode_to_offset() - converts ofnode to offset
  106. no_to_ofnode() - converts node pointer to ofnode
  107. offset_to_ofnode() - converts offset to ofnode
  108. Other useful functions:
  109. of_live_active() returns true if livetree is in use, false if flat tree
  110. ofnode_valid() return true if a given node is valid
  111. ofnode_is_np() returns true if a given node is a livetree node
  112. ofnode_equal() compares two ofnodes
  113. ofnode_null() returns a null ofnode (for which ofnode_valid() returns false)
  114. Phandles
  115. --------
  116. There is full phandle support for live tree. All functions make use of
  117. struct ofnode_phandle_args, which has an ofnode within it. This supports both
  118. livetree and flat tree transparently. See for example
  119. ofnode_parse_phandle_with_args().
  120. Reading addresses
  121. -----------------
  122. You should use dev_read_addr() and friends to read addresses from device-tree
  123. nodes.
  124. fdtdec
  125. ------
  126. The existing fdtdec interface will eventually be retired. Please try to avoid
  127. using it in new code.
  128. Modifying the livetree
  129. ----------------------
  130. This is not currently supported. Once implemented it should provide a much
  131. more efficient implementation for modification of the device tree than using
  132. the flat tree.
  133. Internal implementation
  134. -----------------------
  135. The dev_read_...() functions have two implementations. When
  136. CONFIG_DM_DEV_READ_INLINE is enabled, these functions simply call the ofnode
  137. functions directly. This is useful when livetree is not enabled. The ofnode
  138. functions call ofnode_is_np(node) which will always return false if livetree
  139. is disabled, just falling back to flat tree code.
  140. This optimisation means that without livetree enabled, the dev_read_...() and
  141. ofnode interfaces do not noticeably add to code size.
  142. The CONFIG_DM_DEV_READ_INLINE option defaults to enabled when livetree is
  143. disabled.
  144. Most livetree code comes directly from Linux and is modified as little as
  145. possible. This is deliberate since this code is fairly stable and does what
  146. we want. Some features (such as get/put) are not supported. Internal macros
  147. take care of removing these features silently.
  148. Within the of_access.c file there are pointers to the alias node, the chosen
  149. node and the stdout-path alias.
  150. Errors
  151. ------
  152. With a flat device tree, libfdt errors are returned (e.g. -FDT_ERR_NOTFOUND).
  153. For livetree normal 'errno' errors are returned (e.g. -ENOTFOUND). At present
  154. the ofnode and dev_read_...() functions return either one or other type of
  155. error. This is clearly not desirable. Once tests are added for all the
  156. functions this can be tidied up.
  157. Adding new access functions
  158. ---------------------------
  159. Adding a new function for device-tree access involves the following steps:
  160. - Add two dev_read() functions:
  161. - inline version in the read.h header file, which calls an ofnode
  162. function
  163. - standard version in the read.c file (or perhaps another file), which
  164. also calls an ofnode function
  165. The implementations of these functions can be the same. The purpose
  166. of the inline version is purely to reduce code size impact.
  167. - Add an ofnode function. This should call ofnode_is_np() to work out
  168. whether a livetree or flat tree is used. For the livetree it should
  169. call an of_...() function. For the flat tree it should call an
  170. fdt_...() function. The livetree version will be optimised out at
  171. compile time if livetree is not enabled.
  172. - Add an of_...() function for the livetree implementation. If a similar
  173. function is available in Linux, the implementation should be taken
  174. from there and modified as little as possible (generally not at all).
  175. Future work
  176. -----------
  177. Live tree support was introduced in U-Boot 2017.07. There is still quite a bit
  178. of work to do to flesh this out:
  179. - tests for all access functions
  180. - support for livetree modification
  181. - addition of more access functions as needed
  182. - support for livetree in SPL and before relocation (if desired)
  183. --
  184. Simon Glass <sjg@chromium.org>
  185. 5-Aug-17