xref: /openbmc/qemu/include/hw/clock.h (revision b3c818a4)
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 typedef void ClockCallback(void *opaque);
26 
27 /*
28  * clock store a value representing the clock's period in 2^-32ns unit.
29  * It can represent:
30  *  + periods from 2^-32ns up to 4seconds
31  *  + frequency from ~0.25Hz 2e10Ghz
32  * Resolution of frequency representation decreases with frequency:
33  * + at 100MHz, resolution is ~2mHz
34  * + at 1Ghz,   resolution is ~0.2Hz
35  * + at 10Ghz,  resolution is ~20Hz
36  */
37 #define CLOCK_PERIOD_1SEC (1000000000llu << 32)
38 
39 /*
40  * macro helpers to convert to hertz / nanosecond
41  */
42 #define CLOCK_PERIOD_FROM_NS(ns) ((ns) * (CLOCK_PERIOD_1SEC / 1000000000llu))
43 #define CLOCK_PERIOD_FROM_HZ(hz) (((hz) != 0) ? CLOCK_PERIOD_1SEC / (hz) : 0u)
44 #define CLOCK_PERIOD_TO_HZ(per) (((per) != 0) ? CLOCK_PERIOD_1SEC / (per) : 0u)
45 
46 /**
47  * Clock:
48  * @parent_obj: parent class
49  * @period: unsigned integer representing the period of the clock
50  * @canonical_path: clock path string cache (used for trace purpose)
51  * @callback: called when clock changes
52  * @callback_opaque: argument for @callback
53  * @source: source (or parent in clock tree) of the clock
54  * @children: list of clocks connected to this one (it is their source)
55  * @sibling: structure used to form a clock list
56  */
57 
58 
59 struct Clock {
60     /*< private >*/
61     Object parent_obj;
62 
63     /* all fields are private and should not be modified directly */
64 
65     /* fields */
66     uint64_t period;
67     char *canonical_path;
68     ClockCallback *callback;
69     void *callback_opaque;
70 
71     /* Clocks are organized in a clock tree */
72     Clock *source;
73     QLIST_HEAD(, Clock) children;
74     QLIST_ENTRY(Clock) sibling;
75 };
76 
77 /*
78  * vmstate description entry to be added in device vmsd.
79  */
80 extern const VMStateDescription vmstate_clock;
81 #define VMSTATE_CLOCK(field, state) \
82     VMSTATE_CLOCK_V(field, state, 0)
83 #define VMSTATE_CLOCK_V(field, state, version) \
84     VMSTATE_STRUCT_POINTER_V(field, state, version, vmstate_clock, Clock)
85 #define VMSTATE_ARRAY_CLOCK(field, state, num) \
86     VMSTATE_ARRAY_CLOCK_V(field, state, num, 0)
87 #define VMSTATE_ARRAY_CLOCK_V(field, state, num, version)          \
88     VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(field, state, num, version, \
89                                        vmstate_clock, Clock)
90 
91 /**
92  * clock_setup_canonical_path:
93  * @clk: clock
94  *
95  * compute the canonical path of the clock (used by log messages)
96  */
97 void clock_setup_canonical_path(Clock *clk);
98 
99 /**
100  * clock_new:
101  * @parent: the clock parent
102  * @name: the clock object name
103  *
104  * Helper function to create a new clock and parent it to @parent. There is no
105  * need to call clock_setup_canonical_path on the returned clock as it is done
106  * by this function.
107  *
108  * @return the newly created clock
109  */
110 Clock *clock_new(Object *parent, const char *name);
111 
112 /**
113  * clock_set_callback:
114  * @clk: the clock to register the callback into
115  * @cb: the callback function
116  * @opaque: the argument to the callback
117  *
118  * Register a callback called on every clock update.
119  */
120 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque);
121 
122 /**
123  * clock_clear_callback:
124  * @clk: the clock to delete the callback from
125  *
126  * Unregister the callback registered with clock_set_callback.
127  */
128 void clock_clear_callback(Clock *clk);
129 
130 /**
131  * clock_set_source:
132  * @clk: the clock.
133  * @src: the source clock
134  *
135  * Setup @src as the clock source of @clk. The current @src period
136  * value is also copied to @clk and its subtree but no callback is
137  * called.
138  * Further @src update will be propagated to @clk and its subtree.
139  */
140 void clock_set_source(Clock *clk, Clock *src);
141 
142 /**
143  * clock_set:
144  * @clk: the clock to initialize.
145  * @value: the clock's value, 0 means unclocked
146  *
147  * Set the local cached period value of @clk to @value.
148  *
149  * @return: true if the clock is changed.
150  */
151 bool clock_set(Clock *clk, uint64_t value);
152 
153 static inline bool clock_set_hz(Clock *clk, unsigned hz)
154 {
155     return clock_set(clk, CLOCK_PERIOD_FROM_HZ(hz));
156 }
157 
158 static inline bool clock_set_ns(Clock *clk, unsigned ns)
159 {
160     return clock_set(clk, CLOCK_PERIOD_FROM_NS(ns));
161 }
162 
163 /**
164  * clock_propagate:
165  * @clk: the clock
166  *
167  * Propagate the clock period that has been previously configured using
168  * @clock_set(). This will update recursively all connected clocks.
169  * It is an error to call this function on a clock which has a source.
170  * Note: this function must not be called during device inititialization
171  * or migration.
172  */
173 void clock_propagate(Clock *clk);
174 
175 /**
176  * clock_update:
177  * @clk: the clock to update.
178  * @value: the new clock's value, 0 means unclocked
179  *
180  * Update the @clk to the new @value. All connected clocks will be informed
181  * of this update. This is equivalent to call @clock_set() then
182  * @clock_propagate().
183  */
184 static inline void clock_update(Clock *clk, uint64_t value)
185 {
186     if (clock_set(clk, value)) {
187         clock_propagate(clk);
188     }
189 }
190 
191 static inline void clock_update_hz(Clock *clk, unsigned hz)
192 {
193     clock_update(clk, CLOCK_PERIOD_FROM_HZ(hz));
194 }
195 
196 static inline void clock_update_ns(Clock *clk, unsigned ns)
197 {
198     clock_update(clk, CLOCK_PERIOD_FROM_NS(ns));
199 }
200 
201 /**
202  * clock_get:
203  * @clk: the clk to fetch the clock
204  *
205  * @return: the current period.
206  */
207 static inline uint64_t clock_get(const Clock *clk)
208 {
209     return clk->period;
210 }
211 
212 static inline unsigned clock_get_hz(Clock *clk)
213 {
214     return CLOCK_PERIOD_TO_HZ(clock_get(clk));
215 }
216 
217 /**
218  * clock_ticks_to_ns:
219  * @clk: the clock to query
220  * @ticks: number of ticks
221  *
222  * Returns the length of time in nanoseconds for this clock
223  * to tick @ticks times. Because a clock can have a period
224  * which is not a whole number of nanoseconds, it is important
225  * to use this function when calculating things like timer
226  * expiry deadlines, rather than attempting to obtain a "period
227  * in nanoseconds" value and then multiplying that by a number
228  * of ticks.
229  *
230  * The result could in theory be too large to fit in a 64-bit
231  * value if the number of ticks and the clock period are both
232  * large; to avoid overflow the result will be saturated to INT64_MAX
233  * (because this is the largest valid input to the QEMUTimer APIs).
234  * Since INT64_MAX nanoseconds is almost 300 years, anything with
235  * an expiry later than that is in the "will never happen" category
236  * and callers can reasonably not special-case the saturated result.
237  */
238 static inline uint64_t clock_ticks_to_ns(const Clock *clk, uint64_t ticks)
239 {
240     uint64_t ns_low, ns_high;
241 
242     /*
243      * clk->period is the period in units of 2^-32 ns, so
244      * (clk->period * ticks) is the required length of time in those
245      * units, and we can convert to nanoseconds by multiplying by
246      * 2^32, which is the same as shifting the 128-bit multiplication
247      * result right by 32.
248      */
249     mulu64(&ns_low, &ns_high, clk->period, ticks);
250     if (ns_high & MAKE_64BIT_MASK(31, 33)) {
251         return INT64_MAX;
252     }
253     return ns_low >> 32 | ns_high << 32;
254 }
255 
256 /**
257  * clock_is_enabled:
258  * @clk: a clock
259  *
260  * @return: true if the clock is running.
261  */
262 static inline bool clock_is_enabled(const Clock *clk)
263 {
264     return clock_get(clk) != 0;
265 }
266 
267 /**
268  * clock_display_freq: return human-readable representation of clock frequency
269  * @clk: clock
270  *
271  * Return a string which has a human-readable representation of the
272  * clock's frequency, e.g. "33.3 MHz". This is intended for debug
273  * and display purposes.
274  *
275  * The caller is responsible for freeing the string with g_free().
276  */
277 char *clock_display_freq(Clock *clk);
278 
279 #endif /* QEMU_HW_CLOCK_H */
280