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