Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / include / hw / clock.h
blobeb58599131c7d50477808cf02127bb039b8904a5
1 /*
2 * Hardware Clocks
4 * Copyright GreenSocs 2016-2020
6 * Authors:
7 * Frederic Konrad
8 * Damien Hedde
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.
14 #ifndef QEMU_HW_CLOCK_H
15 #define QEMU_HW_CLOCK_H
17 #include "qom/object.h"
18 #include "qemu/queue.h"
19 #include "qemu/host-utils.h"
20 #include "qemu/bitops.h"
22 #define TYPE_CLOCK "clock"
23 OBJECT_DECLARE_SIMPLE_TYPE(Clock, CLOCK)
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.
31 typedef enum ClockEvent {
32 ClockUpdate = 1, /* Clock period has just updated */
33 ClockPreUpdate = 2, /* Clock period is about to update */
34 } ClockEvent;
36 typedef void ClockCallback(void *opaque, ClockEvent event);
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
48 #define CLOCK_PERIOD_1SEC (1000000000llu << 32)
51 * macro helpers to convert to hertz / nanosecond
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)
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
71 struct Clock {
72 /*< private >*/
73 Object parent_obj;
75 /* all fields are private and should not be modified directly */
77 /* fields */
78 uint64_t period;
79 char *canonical_path;
80 ClockCallback *callback;
81 void *callback_opaque;
82 unsigned int callback_events;
84 /* Ratio of the parent clock to run the child clocks at */
85 uint32_t multiplier;
86 uint32_t divider;
88 /* Clocks are organized in a clock tree */
89 Clock *source;
90 QLIST_HEAD(, Clock) children;
91 QLIST_ENTRY(Clock) sibling;
95 * vmstate description entry to be added in device vmsd.
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)
109 * clock_setup_canonical_path:
110 * @clk: clock
112 * compute the canonical path of the clock (used by log messages)
114 void clock_setup_canonical_path(Clock *clk);
117 * clock_new:
118 * @parent: the clock parent
119 * @name: the clock object name
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.
125 * @return the newly created clock
127 Clock *clock_new(Object *parent, const char *name);
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)
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.
141 void clock_set_callback(Clock *clk, ClockCallback *cb,
142 void *opaque, unsigned int events);
145 * clock_clear_callback:
146 * @clk: the clock to delete the callback from
148 * Unregister the callback registered with clock_set_callback.
150 void clock_clear_callback(Clock *clk);
153 * clock_set_source:
154 * @clk: the clock.
155 * @src: the source clock
157 * Setup @src as the clock source of @clk. The current @src period
158 * value is also copied to @clk and its subtree but no callback is
159 * called.
160 * Further @src update will be propagated to @clk and its subtree.
162 void clock_set_source(Clock *clk, Clock *src);
165 * clock_has_source:
166 * @clk: the clock
168 * Returns true if the clock has a source clock connected to it.
169 * This is useful for devices which have input clocks which must
170 * be connected by the board/SoC code which creates them. The
171 * device code can use this to check in its realize method that
172 * the clock has been connected.
174 static inline bool clock_has_source(const Clock *clk)
176 return clk->source != NULL;
180 * clock_set:
181 * @clk: the clock to initialize.
182 * @value: the clock's value, 0 means unclocked
184 * Set the local cached period value of @clk to @value.
186 * @return: true if the clock is changed.
188 bool clock_set(Clock *clk, uint64_t value);
190 static inline bool clock_set_hz(Clock *clk, unsigned hz)
192 return clock_set(clk, CLOCK_PERIOD_FROM_HZ(hz));
195 static inline bool clock_set_ns(Clock *clk, unsigned ns)
197 return clock_set(clk, CLOCK_PERIOD_FROM_NS(ns));
201 * clock_propagate:
202 * @clk: the clock
204 * Propagate the clock period that has been previously configured using
205 * @clock_set(). This will update recursively all connected clocks.
206 * It is an error to call this function on a clock which has a source.
207 * Note: this function must not be called during device initialization
208 * or migration.
210 void clock_propagate(Clock *clk);
213 * clock_update:
214 * @clk: the clock to update.
215 * @value: the new clock's value, 0 means unclocked
217 * Update the @clk to the new @value. All connected clocks will be informed
218 * of this update. This is equivalent to call @clock_set() then
219 * @clock_propagate().
221 static inline void clock_update(Clock *clk, uint64_t value)
223 if (clock_set(clk, value)) {
224 clock_propagate(clk);
228 static inline void clock_update_hz(Clock *clk, unsigned hz)
230 clock_update(clk, CLOCK_PERIOD_FROM_HZ(hz));
233 static inline void clock_update_ns(Clock *clk, unsigned ns)
235 clock_update(clk, CLOCK_PERIOD_FROM_NS(ns));
239 * clock_get:
240 * @clk: the clk to fetch the clock
242 * @return: the current period.
244 static inline uint64_t clock_get(const Clock *clk)
246 return clk->period;
249 static inline unsigned clock_get_hz(Clock *clk)
251 return CLOCK_PERIOD_TO_HZ(clock_get(clk));
255 * clock_ticks_to_ns:
256 * @clk: the clock to query
257 * @ticks: number of ticks
259 * Returns the length of time in nanoseconds for this clock
260 * to tick @ticks times. Because a clock can have a period
261 * which is not a whole number of nanoseconds, it is important
262 * to use this function when calculating things like timer
263 * expiry deadlines, rather than attempting to obtain a "period
264 * in nanoseconds" value and then multiplying that by a number
265 * of ticks.
267 * The result could in theory be too large to fit in a 64-bit
268 * value if the number of ticks and the clock period are both
269 * large; to avoid overflow the result will be saturated to INT64_MAX
270 * (because this is the largest valid input to the QEMUTimer APIs).
271 * Since INT64_MAX nanoseconds is almost 300 years, anything with
272 * an expiry later than that is in the "will never happen" category
273 * and callers can reasonably not special-case the saturated result.
275 static inline uint64_t clock_ticks_to_ns(const Clock *clk, uint64_t ticks)
277 uint64_t ns_low, ns_high;
280 * clk->period is the period in units of 2^-32 ns, so
281 * (clk->period * ticks) is the required length of time in those
282 * units, and we can convert to nanoseconds by multiplying by
283 * 2^32, which is the same as shifting the 128-bit multiplication
284 * result right by 32.
286 mulu64(&ns_low, &ns_high, clk->period, ticks);
287 if (ns_high & MAKE_64BIT_MASK(31, 33)) {
288 return INT64_MAX;
290 return ns_low >> 32 | ns_high << 32;
294 * clock_ns_to_ticks:
295 * @clk: the clock to query
296 * @ns: duration in nanoseconds
298 * Returns the number of ticks this clock would make in the given
299 * number of nanoseconds. Because a clock can have a period which
300 * is not a whole number of nanoseconds, it is important to use this
301 * function rather than attempting to obtain a "period in nanoseconds"
302 * value and then dividing the duration by that value.
304 * If the clock is stopped (ie it has period zero), returns 0.
306 * For some inputs the result could overflow a 64-bit value (because
307 * the clock's period is short and the duration is long). In these
308 * cases we truncate the result to a 64-bit value. This is on the
309 * assumption that generally the result is going to be used to report
310 * a 32-bit or 64-bit guest register value, so wrapping either cannot
311 * happen or is the desired behaviour.
313 static inline uint64_t clock_ns_to_ticks(const Clock *clk, uint64_t ns)
316 * ticks = duration_in_ns / period_in_ns
317 * = ns / (period / 2^32)
318 * = (ns * 2^32) / period
319 * The hi, lo inputs to divu128() are (ns << 32) as a 128 bit value.
321 uint64_t lo = ns << 32;
322 uint64_t hi = ns >> 32;
323 if (clk->period == 0) {
324 return 0;
327 divu128(&lo, &hi, clk->period);
328 return lo;
332 * clock_is_enabled:
333 * @clk: a clock
335 * @return: true if the clock is running.
337 static inline bool clock_is_enabled(const Clock *clk)
339 return clock_get(clk) != 0;
343 * clock_display_freq: return human-readable representation of clock frequency
344 * @clk: clock
346 * Return a string which has a human-readable representation of the
347 * clock's frequency, e.g. "33.3 MHz". This is intended for debug
348 * and display purposes.
350 * The caller is responsible for freeing the string with g_free().
352 char *clock_display_freq(Clock *clk);
355 * clock_set_mul_div: set multiplier/divider for child clocks
356 * @clk: clock
357 * @multiplier: multiplier value
358 * @divider: divider value
360 * @return: true if the clock is changed.
362 * By default, a Clock's children will all run with the same period
363 * as their parent. This function allows you to adjust the multiplier
364 * and divider used to derive the child clock frequency.
365 * For example, setting a multiplier of 2 and a divider of 3
366 * will run child clocks with a period 2/3 of the parent clock,
367 * so if the parent clock is an 8MHz clock the children will
368 * be 12MHz.
370 * Setting the multiplier to 0 will stop the child clocks.
371 * Setting the divider to 0 is a programming error (diagnosed with
372 * an assertion failure).
373 * Setting a multiplier value that results in the child period
374 * overflowing is not diagnosed.
376 * Note that this function does not call clock_propagate(); the
377 * caller should do that if necessary.
379 bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
381 #endif /* QEMU_HW_CLOCK_H */