clocks.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. Modelling a clock tree in QEMU
  2. ==============================
  3. What are clocks?
  4. ----------------
  5. Clocks are QOM objects developed for the purpose of modelling the
  6. distribution of clocks in QEMU.
  7. They allow us to model the clock distribution of a platform and detect
  8. configuration errors in the clock tree such as badly configured PLL, clock
  9. source selection or disabled clock.
  10. The object is *Clock* and its QOM name is ``clock`` (in C code, the macro
  11. ``TYPE_CLOCK``).
  12. Clocks are typically used with devices where they are used to model inputs
  13. and outputs. They are created in a similar way to GPIOs. Inputs and outputs
  14. of different devices can be connected together.
  15. In these cases a Clock object is a child of a Device object, but this
  16. is not a requirement. Clocks can be independent of devices. For
  17. example it is possible to create a clock outside of any device to
  18. model the main clock source of a machine.
  19. Here is an example of clocks::
  20. +---------+ +----------------------+ +--------------+
  21. | Clock 1 | | Device B | | Device C |
  22. | | | +-------+ +-------+ | | +-------+ |
  23. | |>>-+-->>|Clock 2| |Clock 3|>>--->>|Clock 6| |
  24. +---------+ | | | (in) | | (out) | | | | (in) | |
  25. | | +-------+ +-------+ | | +-------+ |
  26. | | +-------+ | +--------------+
  27. | | |Clock 4|>>
  28. | | | (out) | | +--------------+
  29. | | +-------+ | | Device D |
  30. | | +-------+ | | +-------+ |
  31. | | |Clock 5|>>--->>|Clock 7| |
  32. | | | (out) | | | | (in) | |
  33. | | +-------+ | | +-------+ |
  34. | +----------------------+ | |
  35. | | +-------+ |
  36. +----------------------------->>|Clock 8| |
  37. | | (in) | |
  38. | +-------+ |
  39. +--------------+
  40. Clocks are defined in the ``include/hw/clock.h`` header and device
  41. related functions are defined in the ``include/hw/qdev-clock.h``
  42. header.
  43. The clock state
  44. ---------------
  45. The state of a clock is its period; it is stored as an integer
  46. representing it in units of 2 :sup:`-32` ns. The special value of 0 is used to
  47. represent the clock being inactive or gated. The clocks do not model
  48. the signal itself (pin toggling) or other properties such as the duty
  49. cycle.
  50. All clocks contain this state: outputs as well as inputs. This allows
  51. the current period of a clock to be fetched at any time. When a clock
  52. is updated, the value is immediately propagated to all connected
  53. clocks in the tree.
  54. To ease interaction with clocks, helpers with a unit suffix are defined for
  55. every clock state setter or getter. The suffixes are:
  56. - ``_ns`` for handling periods in nanoseconds
  57. - ``_hz`` for handling frequencies in hertz
  58. The 0 period value is converted to 0 in hertz and vice versa. 0 always means
  59. that the clock is disabled.
  60. Adding a new clock
  61. ------------------
  62. Adding clocks to a device must be done during the init method of the Device
  63. instance.
  64. To add an input clock to a device, the function ``qdev_init_clock_in()``
  65. must be used. It takes the name, a callback and an opaque parameter
  66. for the callback (this will be explained in a following section).
  67. Output is simpler; only the name is required. Typically::
  68. qdev_init_clock_in(DEVICE(dev), "clk_in", clk_in_callback, dev);
  69. qdev_init_clock_out(DEVICE(dev), "clk_out");
  70. Both functions return the created Clock pointer, which should be saved in the
  71. device's state structure for further use.
  72. These objects will be automatically deleted by the QOM reference mechanism.
  73. Note that it is possible to create a static array describing clock inputs and
  74. outputs. The function ``qdev_init_clocks()`` must be called with the array as
  75. parameter to initialize the clocks: it has the same behaviour as calling the
  76. ``qdev_init_clock_in/out()`` for each clock in the array. To ease the array
  77. construction, some macros are defined in ``include/hw/qdev-clock.h``.
  78. As an example, the following creates 2 clocks to a device: one input and one
  79. output.
  80. .. code-block:: c
  81. /* device structure containing pointers to the clock objects */
  82. typedef struct MyDeviceState {
  83. DeviceState parent_obj;
  84. Clock *clk_in;
  85. Clock *clk_out;
  86. } MyDeviceState;
  87. /*
  88. * callback for the input clock (see "Callback on input clock
  89. * change" section below for more information).
  90. */
  91. static void clk_in_callback(void *opaque);
  92. /*
  93. * static array describing clocks:
  94. * + a clock input named "clk_in", whose pointer is stored in
  95. * the clk_in field of a MyDeviceState structure with callback
  96. * clk_in_callback.
  97. * + a clock output named "clk_out" whose pointer is stored in
  98. * the clk_out field of a MyDeviceState structure.
  99. */
  100. static const ClockPortInitArray mydev_clocks = {
  101. QDEV_CLOCK_IN(MyDeviceState, clk_in, clk_in_callback),
  102. QDEV_CLOCK_OUT(MyDeviceState, clk_out),
  103. QDEV_CLOCK_END
  104. };
  105. /* device initialization function */
  106. static void mydev_init(Object *obj)
  107. {
  108. /* cast to MyDeviceState */
  109. MyDeviceState *mydev = MYDEVICE(obj);
  110. /* create and fill the pointer fields in the MyDeviceState */
  111. qdev_init_clocks(mydev, mydev_clocks);
  112. [...]
  113. }
  114. An alternative way to create a clock is to simply call
  115. ``object_new(TYPE_CLOCK)``. In that case the clock will neither be an
  116. input nor an output of a device. After the whole QOM hierarchy of the
  117. clock has been set ``clock_setup_canonical_path()`` should be called.
  118. At creation, the period of the clock is 0: the clock is disabled. You can
  119. change it using ``clock_set_ns()`` or ``clock_set_hz()``.
  120. Note that if you are creating a clock with a fixed period which will never
  121. change (for example the main clock source of a board), then you'll have
  122. nothing else to do. This value will be propagated to other clocks when
  123. connecting the clocks together and devices will fetch the right value during
  124. the first reset.
  125. Retrieving clocks from a device
  126. -------------------------------
  127. ``qdev_get_clock_in()`` and ``dev_get_clock_out()`` are available to
  128. get the clock inputs or outputs of a device. For example:
  129. .. code-block:: c
  130. Clock *clk = qdev_get_clock_in(DEVICE(mydev), "clk_in");
  131. or:
  132. .. code-block:: c
  133. Clock *clk = qdev_get_clock_out(DEVICE(mydev), "clk_out");
  134. Connecting two clocks together
  135. ------------------------------
  136. To connect two clocks together, use the ``clock_set_source()`` function.
  137. Given two clocks ``clk1``, and ``clk2``, ``clock_set_source(clk2, clk1);``
  138. configures ``clk2`` to follow the ``clk1`` period changes. Every time ``clk1``
  139. is updated, ``clk2`` will be updated too.
  140. When connecting clock between devices, prefer using the
  141. ``qdev_connect_clock_in()`` function to set the source of an input
  142. device clock. For example, to connect the input clock ``clk2`` of
  143. ``devB`` to the output clock ``clk1`` of ``devA``, do:
  144. .. code-block:: c
  145. qdev_connect_clock_in(devB, "clk2", qdev_get_clock_out(devA, "clk1"))
  146. We used ``qdev_get_clock_out()`` above, but any clock can drive an
  147. input clock, even another input clock. The following diagram shows
  148. some examples of connections. Note also that a clock can drive several
  149. other clocks.
  150. ::
  151. +------------+ +--------------------------------------------------+
  152. | Device A | | Device B |
  153. | | | +---------------------+ |
  154. | | | | Device C | |
  155. | +-------+ | | +-------+ | +-------+ +-------+ | +-------+ |
  156. | |Clock 1|>>-->>|Clock 2|>>+-->>|Clock 3| |Clock 5|>>>>|Clock 6|>>
  157. | | (out) | | | | (in) | | | | (in) | | (out) | | | (out) | |
  158. | +-------+ | | +-------+ | | +-------+ +-------+ | +-------+ |
  159. +------------+ | | +---------------------+ |
  160. | | |
  161. | | +--------------+ |
  162. | | | Device D | |
  163. | | | +-------+ | |
  164. | +-->>|Clock 4| | |
  165. | | | (in) | | |
  166. | | +-------+ | |
  167. | +--------------+ |
  168. +--------------------------------------------------+
  169. In the above example, when *Clock 1* is updated by *Device A*, three
  170. clocks get the new clock period value: *Clock 2*, *Clock 3* and *Clock 4*.
  171. It is not possible to disconnect a clock or to change the clock connection
  172. after it is connected.
  173. Unconnected input clocks
  174. ------------------------
  175. A newly created input clock is disabled (period of 0). This means the
  176. clock will be considered as disabled until the period is updated. If
  177. the clock remains unconnected it will always keep its initial value
  178. of 0. If this is not the desired behaviour, ``clock_set()``,
  179. ``clock_set_ns()`` or ``clock_set_hz()`` should be called on the Clock
  180. object during device instance init. For example:
  181. .. code-block:: c
  182. clk = qdev_init_clock_in(DEVICE(dev), "clk-in", clk_in_callback,
  183. dev);
  184. /* set initial value to 10ns / 100MHz */
  185. clock_set_ns(clk, 10);
  186. Fetching clock frequency/period
  187. -------------------------------
  188. To get the current state of a clock, use the functions ``clock_get()``,
  189. ``clock_get_ns()`` or ``clock_get_hz()``.
  190. It is also possible to register a callback on clock frequency changes.
  191. Here is an example:
  192. .. code-block:: c
  193. void clock_callback(void *opaque) {
  194. MyDeviceState *s = (MyDeviceState *) opaque;
  195. /*
  196. * 'opaque' is the argument passed to qdev_init_clock_in();
  197. * usually this will be the device state pointer.
  198. */
  199. /* do something with the new period */
  200. fprintf(stdout, "device new period is %" PRIu64 "ns\n",
  201. clock_get_ns(dev->my_clk_input));
  202. }
  203. Changing a clock period
  204. -----------------------
  205. A device can change its outputs using the ``clock_update()``,
  206. ``clock_update_ns()`` or ``clock_update_hz()`` function. It will trigger
  207. updates on every connected input.
  208. For example, let's say that we have an output clock *clkout* and we
  209. have a pointer to it in the device state because we did the following
  210. in init phase:
  211. .. code-block:: c
  212. dev->clkout = qdev_init_clock_out(DEVICE(dev), "clkout");
  213. Then at any time (apart from the cases listed below), it is possible to
  214. change the clock value by doing:
  215. .. code-block:: c
  216. clock_update_hz(dev->clkout, 1000 * 1000 * 1000); /* 1GHz */
  217. Because updating a clock may trigger any side effects through
  218. connected clocks and their callbacks, this operation must be done
  219. while holding the qemu io lock.
  220. For the same reason, one can update clocks only when it is allowed to have
  221. side effects on other objects. In consequence, it is forbidden:
  222. * during migration,
  223. * and in the enter phase of reset.
  224. Note that calling ``clock_update[_ns|_hz]()`` is equivalent to calling
  225. ``clock_set[_ns|_hz]()`` (with the same arguments) then
  226. ``clock_propagate()`` on the clock. Thus, setting the clock value can
  227. be separated from triggering the side-effects. This is often required
  228. to factorize code to handle reset and migration in devices.
  229. Aliasing clocks
  230. ---------------
  231. Sometimes, one needs to forward, or inherit, a clock from another
  232. device. Typically, when doing device composition, a device might
  233. expose a sub-device's clock without interfering with it. The function
  234. ``qdev_alias_clock()`` can be used to achieve this behaviour. Note
  235. that it is possible to expose the clock under a different name.
  236. ``qdev_alias_clock()`` works for both input and output clocks.
  237. For example, if device B is a child of device A,
  238. ``device_a_instance_init()`` may do something like this:
  239. .. code-block:: c
  240. void device_a_instance_init(Object *obj)
  241. {
  242. AState *A = DEVICE_A(obj);
  243. BState *B;
  244. /* create object B as child of A */
  245. [...]
  246. qdev_alias_clock(B, "clk", A, "b_clk");
  247. /*
  248. * Now A has a clock "b_clk" which is an alias to
  249. * the clock "clk" of its child B.
  250. */
  251. }
  252. This function does not return any clock object. The new clock has the
  253. same direction (input or output) as the original one. This function
  254. only adds a link to the existing clock. In the above example, object B
  255. remains the only object allowed to use the clock and device A must not
  256. try to change the clock period or set a callback to the clock. This
  257. diagram describes the example with an input clock::
  258. +--------------------------+
  259. | Device A |
  260. | +--------------+ |
  261. | | Device B | |
  262. | | +-------+ | |
  263. >>"b_clk">>>| "clk" | | |
  264. | (in) | | (in) | | |
  265. | | +-------+ | |
  266. | +--------------+ |
  267. +--------------------------+
  268. Migration
  269. ---------
  270. Clock state is not migrated automatically. Every device must handle its
  271. clock migration. Alias clocks must not be migrated.
  272. To ensure clock states are restored correctly during migration, there
  273. are two solutions.
  274. Clock states can be migrated by adding an entry into the device
  275. vmstate description. You should use the ``VMSTATE_CLOCK`` macro for this.
  276. This is typically used to migrate an input clock state. For example:
  277. .. code-block:: c
  278. MyDeviceState {
  279. DeviceState parent_obj;
  280. [...] /* some fields */
  281. Clock *clk;
  282. };
  283. VMStateDescription my_device_vmstate = {
  284. .name = "my_device",
  285. .fields = (VMStateField[]) {
  286. [...], /* other migrated fields */
  287. VMSTATE_CLOCK(clk, MyDeviceState),
  288. VMSTATE_END_OF_LIST()
  289. }
  290. };
  291. The second solution is to restore the clock state using information already
  292. at our disposal. This can be used to restore output clock states using the
  293. device state. The functions ``clock_set[_ns|_hz]()`` can be used during the
  294. ``post_load()`` migration callback.
  295. When adding clock support to an existing device, if you care about
  296. migration compatibility you will need to be careful, as simply adding
  297. a ``VMSTATE_CLOCK()`` line will break compatibility. Instead, you can
  298. put the ``VMSTATE_CLOCK()`` line into a vmstate subsection with a
  299. suitable ``needed`` function, and use ``clock_set()`` in a
  300. ``pre_load()`` function to set the default value that will be used if
  301. the source virtual machine in the migration does not send the clock
  302. state.
  303. Care should be taken not to use ``clock_update[_ns|_hz]()`` or
  304. ``clock_propagate()`` during the whole migration procedure because it
  305. will trigger side effects to other devices in an unknown state.