xref: /qemu/include/hw/clock.h (revision 1eec82cc06cd68b6ffaf13ba8337fac0080c7bce)
1 /*
2  * Hardware Clocks
3  *
4  * Copyright GreenSocs 2016-2020
5  *
6  * Authors:
7  *  Frederic Konrad
8  *  Damien Hedde
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  */
13 
14 #ifndef QEMU_HW_CLOCK_H
15 #define QEMU_HW_CLOCK_H
16 
17 #include "qom/object.h"
18 #include "qemu/queue.h"
19 #include "qemu/host-utils.h"
20 #include "qemu/bitops.h"
21 
22 #define TYPE_CLOCK "clock"
23 OBJECT_DECLARE_SIMPLE_TYPE(Clock, CLOCK)
24 
25 /*
26  * Argument to ClockCallback functions indicating why the callback
27  * has been called. A mask of these values logically ORed together
28  * is used to specify which events are interesting when the callback
29  * is registered, so these values must all be different bit values.
30  */
31 typedef enum ClockEvent {
32     ClockUpdate = 1, /* Clock period has just updated */
33     ClockPreUpdate = 2, /* Clock period is about to update */
34 } ClockEvent;
35 
36 typedef void ClockCallback(void *opaque, ClockEvent event);
37 
38 /*
39  * clock store a value representing the clock's period in 2^-32ns unit.
40  * It can represent:
41  *  + periods from 2^-32ns up to 4seconds
42  *  + frequency from ~0.25Hz 2e10Ghz
43  * Resolution of frequency representation decreases with frequency:
44  * + at 100MHz, resolution is ~2mHz
45  * + at 1Ghz,   resolution is ~0.2Hz
46  * + at 10Ghz,  resolution is ~20Hz
47  */
48 #define CLOCK_PERIOD_1SEC (1000000000llu << 32)
49 
50 /*
51  * macro helpers to convert to hertz / nanosecond
52  */
53 #define CLOCK_PERIOD_FROM_NS(ns) ((ns) * (CLOCK_PERIOD_1SEC / 1000000000llu))
54 #define CLOCK_PERIOD_FROM_HZ(hz) (((hz) != 0) ? CLOCK_PERIOD_1SEC / (hz) : 0u)
55 #define CLOCK_PERIOD_TO_HZ(per) (((per) != 0) ? CLOCK_PERIOD_1SEC / (per) : 0u)
56 
57 /**
58  * Clock:
59  * @parent_obj: parent class
60  * @period: unsigned integer representing the period of the clock
61  * @canonical_path: clock path string cache (used for trace purpose)
62  * @callback: called when clock changes
63  * @callback_opaque: argument for @callback
64  * @callback_events: mask of events when callback should be called
65  * @source: source (or parent in clock tree) of the clock
66  * @children: list of clocks connected to this one (it is their source)
67  * @sibling: structure used to form a clock list
68  */
69 
70 
71 struct Clock {
72     /*< private >*/
73     Object parent_obj;
74 
75     /* all fields are private and should not be modified directly */
76 
77     /* fields */
78     uint64_t period;
79     char *canonical_path;
80     ClockCallback *callback;
81     void *callback_opaque;
82     unsigned int callback_events;
83 
84     /* Ratio of the parent clock to run the child clocks at */
85     uint32_t multiplier;
86     uint32_t divider;
87 
88     /* Clocks are organized in a clock tree */
89     Clock *source;
90     QLIST_HEAD(, Clock) children;
91     QLIST_ENTRY(Clock) sibling;
92 };
93 
94 /*
95  * vmstate description entry to be added in device vmsd.
96  */
97 extern const VMStateDescription vmstate_clock;
98 #define VMSTATE_CLOCK(field, state) \
99     VMSTATE_CLOCK_V(field, state, 0)
100 #define VMSTATE_CLOCK_V(field, state, version) \
101     VMSTATE_STRUCT_POINTER_V(field, state, version, vmstate_clock, Clock)
102 #define VMSTATE_ARRAY_CLOCK(field, state, num) \
103     VMSTATE_ARRAY_CLOCK_V(field, state, num, 0)
104 #define VMSTATE_ARRAY_CLOCK_V(field, state, num, version)          \
105     VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(field, state, num, version, \
106                                        vmstate_clock, Clock)
107 
108 /**
109  * clock_setup_canonical_path:
110  * @clk: clock
111  *
112  * compute the canonical path of the clock (used by log messages)
113  */
114 void clock_setup_canonical_path(Clock *clk);
115 
116 /**
117  * clock_new:
118  * @parent: the clock parent
119  * @name: the clock object name
120  *
121  * Helper function to create a new clock and parent it to @parent. There is no
122  * need to call clock_setup_canonical_path on the returned clock as it is done
123  * by this function.
124  *
125  * @return the newly created clock
126  */
127 Clock *clock_new(Object *parent, const char *name);
128 
129 /**
130  * clock_set_callback:
131  * @clk: the clock to register the callback into
132  * @cb: the callback function
133  * @opaque: the argument to the callback
134  * @events: the events the callback should be called for
135  *          (logical OR of ClockEvent enum values)
136  *
137  * Register a callback called on every clock update.
138  * Note that a clock has only one callback: you cannot register
139  * different callback functions for different events.
140  */
141 void clock_set_callback(Clock *clk, ClockCallback *cb,
142                         void *opaque, unsigned int events);
143 
144 /**
145  * clock_set_source:
146  * @clk: the clock.
147  * @src: the source clock
148  *
149  * Setup @src as the clock source of @clk. The current @src period
150  * value is also copied to @clk and its subtree but no callback is
151  * called.
152  * Further @src update will be propagated to @clk and its subtree.
153  */
154 void clock_set_source(Clock *clk, Clock *src);
155 
156 /**
157  * clock_has_source:
158  * @clk: the clock
159  *
160  * Returns true if the clock has a source clock connected to it.
161  * This is useful for devices which have input clocks which must
162  * be connected by the board/SoC code which creates them. The
163  * device code can use this to check in its realize method that
164  * the clock has been connected.
165  */
clock_has_source(const Clock * clk)166 static inline bool clock_has_source(const Clock *clk)
167 {
168     return clk->source != NULL;
169 }
170 
171 /**
172  * clock_set:
173  * @clk: the clock to initialize.
174  * @value: the clock's value, 0 means unclocked
175  *
176  * Set the local cached period value of @clk to @value.
177  *
178  * @return: true if the clock is changed.
179  */
180 bool clock_set(Clock *clk, uint64_t value);
181 
clock_set_hz(Clock * clk,unsigned hz)182 static inline bool clock_set_hz(Clock *clk, unsigned hz)
183 {
184     return clock_set(clk, CLOCK_PERIOD_FROM_HZ(hz));
185 }
186 
clock_set_ns(Clock * clk,unsigned ns)187 static inline bool clock_set_ns(Clock *clk, unsigned ns)
188 {
189     return clock_set(clk, CLOCK_PERIOD_FROM_NS(ns));
190 }
191 
192 /**
193  * clock_propagate:
194  * @clk: the clock
195  *
196  * Propagate the clock period that has been previously configured using
197  * @clock_set(). This will update recursively all connected clocks.
198  * It is an error to call this function on a clock which has a source.
199  * Note: this function must not be called during device initialization
200  * or migration.
201  */
202 void clock_propagate(Clock *clk);
203 
204 /**
205  * clock_update:
206  * @clk: the clock to update.
207  * @value: the new clock's value, 0 means unclocked
208  *
209  * Update the @clk to the new @value. All connected clocks will be informed
210  * of this update. This is equivalent to call @clock_set() then
211  * @clock_propagate().
212  */
clock_update(Clock * clk,uint64_t value)213 static inline void clock_update(Clock *clk, uint64_t value)
214 {
215     if (clock_set(clk, value)) {
216         clock_propagate(clk);
217     }
218 }
219 
clock_update_hz(Clock * clk,unsigned hz)220 static inline void clock_update_hz(Clock *clk, unsigned hz)
221 {
222     clock_update(clk, CLOCK_PERIOD_FROM_HZ(hz));
223 }
224 
clock_update_ns(Clock * clk,unsigned ns)225 static inline void clock_update_ns(Clock *clk, unsigned ns)
226 {
227     clock_update(clk, CLOCK_PERIOD_FROM_NS(ns));
228 }
229 
230 /**
231  * clock_get:
232  * @clk: the clk to fetch the clock
233  *
234  * @return: the current period.
235  */
clock_get(const Clock * clk)236 static inline uint64_t clock_get(const Clock *clk)
237 {
238     return clk->period;
239 }
240 
clock_get_hz(Clock * clk)241 static inline unsigned clock_get_hz(Clock *clk)
242 {
243     return CLOCK_PERIOD_TO_HZ(clock_get(clk));
244 }
245 
246 /**
247  * clock_ticks_to_ns:
248  * @clk: the clock to query
249  * @ticks: number of ticks
250  *
251  * Returns the length of time in nanoseconds for this clock
252  * to tick @ticks times. Because a clock can have a period
253  * which is not a whole number of nanoseconds, it is important
254  * to use this function when calculating things like timer
255  * expiry deadlines, rather than attempting to obtain a "period
256  * in nanoseconds" value and then multiplying that by a number
257  * of ticks.
258  *
259  * The result could in theory be too large to fit in a 64-bit
260  * value if the number of ticks and the clock period are both
261  * large; to avoid overflow the result will be saturated to INT64_MAX
262  * (because this is the largest valid input to the QEMUTimer APIs).
263  * Since INT64_MAX nanoseconds is almost 300 years, anything with
264  * an expiry later than that is in the "will never happen" category
265  * and callers can reasonably not special-case the saturated result.
266  */
clock_ticks_to_ns(const Clock * clk,uint64_t ticks)267 static inline uint64_t clock_ticks_to_ns(const Clock *clk, uint64_t ticks)
268 {
269     uint64_t ns_low, ns_high;
270 
271     /*
272      * clk->period is the period in units of 2^-32 ns, so
273      * (clk->period * ticks) is the required length of time in those
274      * units, and we can convert to nanoseconds by multiplying by
275      * 2^32, which is the same as shifting the 128-bit multiplication
276      * result right by 32.
277      */
278     mulu64(&ns_low, &ns_high, clk->period, ticks);
279     if (ns_high & MAKE_64BIT_MASK(31, 33)) {
280         return INT64_MAX;
281     }
282     return ns_low >> 32 | ns_high << 32;
283 }
284 
285 /**
286  * clock_ns_to_ticks:
287  * @clk: the clock to query
288  * @ns: duration in nanoseconds
289  *
290  * Returns the number of ticks this clock would make in the given
291  * number of nanoseconds. Because a clock can have a period which
292  * is not a whole number of nanoseconds, it is important to use this
293  * function rather than attempting to obtain a "period in nanoseconds"
294  * value and then dividing the duration by that value.
295  *
296  * If the clock is stopped (ie it has period zero), returns 0.
297  *
298  * For some inputs the result could overflow a 64-bit value (because
299  * the clock's period is short and the duration is long). In these
300  * cases we truncate the result to a 64-bit value. This is on the
301  * assumption that generally the result is going to be used to report
302  * a 32-bit or 64-bit guest register value, so wrapping either cannot
303  * happen or is the desired behaviour.
304  */
clock_ns_to_ticks(const Clock * clk,uint64_t ns)305 static inline uint64_t clock_ns_to_ticks(const Clock *clk, uint64_t ns)
306 {
307     /*
308      * ticks = duration_in_ns / period_in_ns
309      *       = ns / (period / 2^32)
310      *       = (ns * 2^32) / period
311      * The hi, lo inputs to divu128() are (ns << 32) as a 128 bit value.
312      */
313     uint64_t lo = ns << 32;
314     uint64_t hi = ns >> 32;
315     if (clk->period == 0) {
316         return 0;
317     }
318 
319     divu128(&lo, &hi, clk->period);
320     return lo;
321 }
322 
323 /**
324  * clock_is_enabled:
325  * @clk: a clock
326  *
327  * @return: true if the clock is running.
328  */
clock_is_enabled(const Clock * clk)329 static inline bool clock_is_enabled(const Clock *clk)
330 {
331     return clock_get(clk) != 0;
332 }
333 
334 /**
335  * clock_display_freq: return human-readable representation of clock frequency
336  * @clk: clock
337  *
338  * Return a string which has a human-readable representation of the
339  * clock's frequency, e.g. "33.3 MHz". This is intended for debug
340  * and display purposes.
341  *
342  * The caller is responsible for freeing the string with g_free().
343  */
344 char *clock_display_freq(Clock *clk);
345 
346 /**
347  * clock_set_mul_div: set multiplier/divider for child clocks
348  * @clk: clock
349  * @multiplier: multiplier value
350  * @divider: divider value
351  *
352  * @return: true if the clock is changed.
353  *
354  * By default, a Clock's children will all run with the same period
355  * as their parent. This function allows you to adjust the multiplier
356  * and divider used to derive the child clock frequency.
357  * For example, setting a multiplier of 2 and a divider of 3
358  * will run child clocks with a period 2/3 of the parent clock,
359  * so if the parent clock is an 8MHz clock the children will
360  * be 12MHz.
361  *
362  * Setting the multiplier to 0 will stop the child clocks.
363  * Setting the divider to 0 is a programming error (diagnosed with
364  * an assertion failure).
365  * Setting a multiplier value that results in the child period
366  * overflowing is not diagnosed.
367  *
368  * Note that this function does not call clock_propagate(); the
369  * caller should do that if necessary.
370  */
371 bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
372 
373 #endif /* QEMU_HW_CLOCK_H */
374