net: cadence_gem: Make phy respond to broadcast
[qemu.git] / include / qemu / timer.h
blob7f9a074c2a3b771cf0df87d46ae5a10537775544
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
4 #include "qemu/typedefs.h"
5 #include "qemu-common.h"
6 #include "qemu/notify.h"
8 /* timers */
10 #define SCALE_MS 1000000
11 #define SCALE_US 1000
12 #define SCALE_NS 1
14 /**
15 * QEMUClockType:
17 * The following clock types are available:
19 * @QEMU_CLOCK_REALTIME: Real time clock
21 * The real time clock should be used only for stuff which does not
22 * change the virtual machine state, as it is run even if the virtual
23 * machine is stopped. The real time clock has a frequency of 1000
24 * Hz.
26 * @QEMU_CLOCK_VIRTUAL: virtual clock
28 * The virtual clock is only run during the emulation. It is stopped
29 * when the virtual machine is stopped. Virtual timers use a high
30 * precision clock, usually cpu cycles (use ticks_per_sec).
32 * @QEMU_CLOCK_HOST: host clock
34 * The host clock should be use for device models that emulate accurate
35 * real time sources. It will continue to run when the virtual machine
36 * is suspended, and it will reflect system time changes the host may
37 * undergo (e.g. due to NTP). The host clock has the same precision as
38 * the virtual clock.
41 typedef enum {
42 QEMU_CLOCK_REALTIME = 0,
43 QEMU_CLOCK_VIRTUAL = 1,
44 QEMU_CLOCK_HOST = 2,
45 QEMU_CLOCK_MAX
46 } QEMUClockType;
48 typedef struct QEMUTimerList QEMUTimerList;
50 struct QEMUTimerListGroup {
51 QEMUTimerList *tl[QEMU_CLOCK_MAX];
54 typedef void QEMUTimerCB(void *opaque);
55 typedef void QEMUTimerListNotifyCB(void *opaque);
57 struct QEMUTimer {
58 int64_t expire_time; /* in nanoseconds */
59 QEMUTimerList *timer_list;
60 QEMUTimerCB *cb;
61 void *opaque;
62 QEMUTimer *next;
63 int scale;
66 extern QEMUTimerListGroup main_loop_tlg;
69 * QEMUClockType
73 * qemu_clock_get_ns;
74 * @type: the clock type
76 * Get the nanosecond value of a clock with
77 * type @type
79 * Returns: the clock value in nanoseconds
81 int64_t qemu_clock_get_ns(QEMUClockType type);
83 /**
84 * qemu_clock_get_ms;
85 * @type: the clock type
87 * Get the millisecond value of a clock with
88 * type @type
90 * Returns: the clock value in milliseconds
92 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
94 return qemu_clock_get_ns(type) / SCALE_MS;
97 /**
98 * qemu_clock_get_us;
99 * @type: the clock type
101 * Get the microsecond value of a clock with
102 * type @type
104 * Returns: the clock value in microseconds
106 static inline int64_t qemu_clock_get_us(QEMUClockType type)
108 return qemu_clock_get_ns(type) / SCALE_US;
112 * qemu_clock_has_timers:
113 * @type: the clock type
115 * Determines whether a clock's default timer list
116 * has timers attached
118 * Note that this function should not be used when other threads also access
119 * the timer list. The return value may be outdated by the time it is acted
120 * upon.
122 * Returns: true if the clock's default timer list
123 * has timers attached
125 bool qemu_clock_has_timers(QEMUClockType type);
128 * qemu_clock_expired:
129 * @type: the clock type
131 * Determines whether a clock's default timer list
132 * has an expired clock.
134 * Returns: true if the clock's default timer list has
135 * an expired timer
137 bool qemu_clock_expired(QEMUClockType type);
140 * qemu_clock_use_for_deadline:
141 * @type: the clock type
143 * Determine whether a clock should be used for deadline
144 * calculations. Some clocks, for instance vm_clock with
145 * use_icount set, do not count in nanoseconds. Such clocks
146 * are not used for deadline calculations, and are presumed
147 * to interrupt any poll using qemu_notify/aio_notify
148 * etc.
150 * Returns: true if the clock runs in nanoseconds and
151 * should be used for a deadline.
153 bool qemu_clock_use_for_deadline(QEMUClockType type);
156 * qemu_clock_deadline_ns_all:
157 * @type: the clock type
159 * Calculate the deadline across all timer lists associated
160 * with a clock (as opposed to just the default one)
161 * in nanoseconds, or -1 if no timer is set to expire.
163 * Returns: time until expiry in nanoseconds or -1
165 int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
168 * qemu_clock_get_main_loop_timerlist:
169 * @type: the clock type
171 * Return the default timer list assocatiated with a clock.
173 * Returns: the default timer list
175 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
178 * qemu_clock_nofify:
179 * @type: the clock type
181 * Call the notifier callback connected with the default timer
182 * list linked to the clock, or qemu_notify() if none.
184 void qemu_clock_notify(QEMUClockType type);
187 * qemu_clock_enable:
188 * @type: the clock type
189 * @enabled: true to enable, false to disable
191 * Enable or disable a clock
192 * Disabling the clock will wait for related timerlists to stop
193 * executing qemu_run_timers. Thus, this functions should not
194 * be used from the callback of a timer that is based on @clock.
195 * Doing so would cause a deadlock.
197 * Caller should hold BQL.
199 void qemu_clock_enable(QEMUClockType type, bool enabled);
202 * qemu_clock_warp:
203 * @type: the clock type
205 * Warp a clock to a new value
207 void qemu_clock_warp(QEMUClockType type);
210 * qemu_clock_register_reset_notifier:
211 * @type: the clock type
212 * @notifier: the notifier function
214 * Register a notifier function to call when the clock
215 * concerned is reset.
217 void qemu_clock_register_reset_notifier(QEMUClockType type,
218 Notifier *notifier);
221 * qemu_clock_unregister_reset_notifier:
222 * @type: the clock type
223 * @notifier: the notifier function
225 * Unregister a notifier function to call when the clock
226 * concerned is reset.
228 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
229 Notifier *notifier);
232 * qemu_clock_run_timers:
233 * @type: clock on which to operate
235 * Run all the timers associated with the default timer list
236 * of a clock.
238 * Returns: true if any timer ran.
240 bool qemu_clock_run_timers(QEMUClockType type);
243 * qemu_clock_run_all_timers:
245 * Run all the timers associated with the default timer list
246 * of every clock.
248 * Returns: true if any timer ran.
250 bool qemu_clock_run_all_timers(void);
253 * QEMUTimerList
257 * timerlist_new:
258 * @type: the clock type to associate with the timerlist
259 * @cb: the callback to call on notification
260 * @opaque: the opaque pointer to pass to the callback
262 * Create a new timerlist associated with the clock of
263 * type @type.
265 * Returns: a pointer to the QEMUTimerList created
267 QEMUTimerList *timerlist_new(QEMUClockType type,
268 QEMUTimerListNotifyCB *cb, void *opaque);
271 * timerlist_free:
272 * @timer_list: the timer list to free
274 * Frees a timer_list. It must have no active timers.
276 void timerlist_free(QEMUTimerList *timer_list);
279 * timerlist_has_timers:
280 * @timer_list: the timer list to operate on
282 * Determine whether a timer list has active timers
284 * Note that this function should not be used when other threads also access
285 * the timer list. The return value may be outdated by the time it is acted
286 * upon.
288 * Returns: true if the timer list has timers.
290 bool timerlist_has_timers(QEMUTimerList *timer_list);
293 * timerlist_expired:
294 * @timer_list: the timer list to operate on
296 * Determine whether a timer list has any timers which
297 * are expired.
299 * Returns: true if the timer list has timers which
300 * have expired.
302 bool timerlist_expired(QEMUTimerList *timer_list);
305 * timerlist_deadline_ns:
306 * @timer_list: the timer list to operate on
308 * Determine the deadline for a timer_list, i.e.
309 * the number of nanoseconds until the first timer
310 * expires. Return -1 if there are no timers.
312 * Returns: the number of nanoseconds until the earliest
313 * timer expires -1 if none
315 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
318 * timerlist_get_clock:
319 * @timer_list: the timer list to operate on
321 * Determine the clock type associated with a timer list.
323 * Returns: the clock type associated with the
324 * timer list.
326 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
329 * timerlist_run_timers:
330 * @timer_list: the timer list to use
332 * Call all expired timers associated with the timer list.
334 * Returns: true if any timer expired
336 bool timerlist_run_timers(QEMUTimerList *timer_list);
339 * timerlist_notify:
340 * @timer_list: the timer list to use
342 * call the notifier callback associated with the timer list.
344 void timerlist_notify(QEMUTimerList *timer_list);
347 * QEMUTimerListGroup
351 * timerlistgroup_init:
352 * @tlg: the timer list group
353 * @cb: the callback to call when a notify is required
354 * @opaque: the opaque pointer to be passed to the callback.
356 * Initialise a timer list group. This must already be
357 * allocated in memory and zeroed. The notifier callback is
358 * called whenever a clock in the timer list group is
359 * reenabled or whenever a timer associated with any timer
360 * list is modified. If @cb is specified as null, qemu_notify()
361 * is used instead.
363 void timerlistgroup_init(QEMUTimerListGroup *tlg,
364 QEMUTimerListNotifyCB *cb, void *opaque);
367 * timerlistgroup_deinit:
368 * @tlg: the timer list group
370 * Deinitialise a timer list group. This must already be
371 * initialised. Note the memory is not freed.
373 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
376 * timerlistgroup_run_timers:
377 * @tlg: the timer list group
379 * Run the timers associated with a timer list group.
380 * This will run timers on multiple clocks.
382 * Returns: true if any timer callback ran
384 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
387 * timerlistgroup_deadline_ns:
388 * @tlg: the timer list group
390 * Determine the deadline of the soonest timer to
391 * expire associated with any timer list linked to
392 * the timer list group. Only clocks suitable for
393 * deadline calculation are included.
395 * Returns: the deadline in nanoseconds or -1 if no
396 * timers are to expire.
398 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
401 * QEMUTimer
405 * timer_init:
406 * @ts: the timer to be initialised
407 * @timer_list: the timer list to attach the timer to
408 * @scale: the scale value for the timer
409 * @cb: the callback to be called when the timer expires
410 * @opaque: the opaque pointer to be passed to the callback
412 * Initialise a new timer and associate it with @timer_list.
413 * The caller is responsible for allocating the memory.
415 * You need not call an explicit deinit call. Simply make
416 * sure it is not on a list with timer_del.
418 void timer_init(QEMUTimer *ts,
419 QEMUTimerList *timer_list, int scale,
420 QEMUTimerCB *cb, void *opaque);
423 * timer_new_tl:
424 * @timer_list: the timer list to attach the timer to
425 * @scale: the scale value for the timer
426 * @cb: the callback to be called when the timer expires
427 * @opaque: the opaque pointer to be passed to the callback
429 * Creeate a new timer and associate it with @timer_list.
430 * The memory is allocated by the function.
432 * This is not the preferred interface unless you know you
433 * are going to call timer_free. Use timer_init instead.
435 * Returns: a pointer to the timer
437 static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
438 int scale,
439 QEMUTimerCB *cb,
440 void *opaque)
442 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
443 timer_init(ts, timer_list, scale, cb, opaque);
444 return ts;
448 * timer_new:
449 * @type: the clock type to use
450 * @scale: the scale value for the timer
451 * @cb: the callback to be called when the timer expires
452 * @opaque: the opaque pointer to be passed to the callback
454 * Creeate a new timer and associate it with the default
455 * timer list for the clock type @type.
457 * Returns: a pointer to the timer
459 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
460 QEMUTimerCB *cb, void *opaque)
462 return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
466 * timer_new_ns:
467 * @clock: the clock to associate with the timer
468 * @callback: the callback to call when the timer expires
469 * @opaque: the opaque pointer to pass to the callback
471 * Create a new timer with nanosecond scale on the default timer list
472 * associated with the clock.
474 * Returns: a pointer to the newly created timer
476 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
477 void *opaque)
479 return timer_new(type, SCALE_NS, cb, opaque);
483 * timer_new_us:
484 * @clock: the clock to associate with the timer
485 * @callback: the callback to call when the timer expires
486 * @opaque: the opaque pointer to pass to the callback
488 * Create a new timer with microsecond scale on the default timer list
489 * associated with the clock.
491 * Returns: a pointer to the newly created timer
493 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
494 void *opaque)
496 return timer_new(type, SCALE_US, cb, opaque);
500 * timer_new_ms:
501 * @clock: the clock to associate with the timer
502 * @callback: the callback to call when the timer expires
503 * @opaque: the opaque pointer to pass to the callback
505 * Create a new timer with millisecond scale on the default timer list
506 * associated with the clock.
508 * Returns: a pointer to the newly created timer
510 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
511 void *opaque)
513 return timer_new(type, SCALE_MS, cb, opaque);
517 * timer_free:
518 * @ts: the timer
520 * Free a timer (it must not be on the active list)
522 void timer_free(QEMUTimer *ts);
525 * timer_del:
526 * @ts: the timer
528 * Delete a timer from the active list.
530 * This function is thread-safe but the timer and its timer list must not be
531 * freed while this function is running.
533 void timer_del(QEMUTimer *ts);
536 * timer_mod_ns:
537 * @ts: the timer
538 * @expire_time: the expiry time in nanoseconds
540 * Modify a timer to expire at @expire_time
542 * This function is thread-safe but the timer and its timer list must not be
543 * freed while this function is running.
545 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
548 * timer_mod_anticipate_ns:
549 * @ts: the timer
550 * @expire_time: the expiry time in nanoseconds
552 * Modify a timer to expire at @expire_time or the current time,
553 * whichever comes earlier.
555 * This function is thread-safe but the timer and its timer list must not be
556 * freed while this function is running.
558 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
561 * timer_mod:
562 * @ts: the timer
563 * @expire_time: the expire time in the units associated with the timer
565 * Modify a timer to expiry at @expire_time, taking into
566 * account the scale associated with the timer.
568 * This function is thread-safe but the timer and its timer list must not be
569 * freed while this function is running.
571 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
574 * timer_mod_anticipate:
575 * @ts: the timer
576 * @expire_time: the expiry time in nanoseconds
578 * Modify a timer to expire at @expire_time or the current time, whichever
579 * comes earlier, taking into account the scale associated with the timer.
581 * This function is thread-safe but the timer and its timer list must not be
582 * freed while this function is running.
584 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
587 * timer_pending:
588 * @ts: the timer
590 * Determines whether a timer is pending (i.e. is on the
591 * active list of timers, whether or not it has not yet expired).
593 * Returns: true if the timer is pending
595 bool timer_pending(QEMUTimer *ts);
598 * timer_expired:
599 * @ts: the timer
601 * Determines whether a timer has expired.
603 * Returns: true if the timer has expired
605 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
608 * timer_expire_time_ns:
609 * @ts: the timer
611 * Determine the expiry time of a timer
613 * Returns: the expiry time in nanoseconds
615 uint64_t timer_expire_time_ns(QEMUTimer *ts);
618 * timer_get:
619 * @f: the file
620 * @ts: the timer
622 * Read a timer @ts from a file @f
624 void timer_get(QEMUFile *f, QEMUTimer *ts);
627 * timer_put:
628 * @f: the file
629 * @ts: the timer
631 void timer_put(QEMUFile *f, QEMUTimer *ts);
634 * General utility functions
638 * qemu_timeout_ns_to_ms:
639 * @ns: nanosecond timeout value
641 * Convert a nanosecond timeout value (or -1) to
642 * a millisecond value (or -1), always rounding up.
644 * Returns: millisecond timeout value
646 int qemu_timeout_ns_to_ms(int64_t ns);
649 * qemu_poll_ns:
650 * @fds: Array of file descriptors
651 * @nfds: number of file descriptors
652 * @timeout: timeout in nanoseconds
654 * Perform a poll like g_poll but with a timeout in nanoseconds.
655 * See g_poll documentation for further details.
657 * Returns: number of fds ready
659 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
662 * qemu_soonest_timeout:
663 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
664 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
666 * Calculates the soonest of two timeout values. -1 means infinite, which
667 * is later than any other value.
669 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
671 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
673 /* we can abuse the fact that -1 (which means infinite) is a maximal
674 * value when cast to unsigned. As this is disgusting, it's kept in
675 * one inline function.
677 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
681 * initclocks:
683 * Initialise the clock & timer infrastructure
685 void init_clocks(void);
687 int64_t cpu_get_ticks(void);
688 /* Caller must hold BQL */
689 void cpu_enable_ticks(void);
690 /* Caller must hold BQL */
691 void cpu_disable_ticks(void);
693 static inline int64_t get_ticks_per_sec(void)
695 return 1000000000LL;
699 * Low level clock functions
702 /* real time host monotonic timer */
703 static inline int64_t get_clock_realtime(void)
705 struct timeval tv;
707 gettimeofday(&tv, NULL);
708 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
711 /* Warning: don't insert tracepoints into these functions, they are
712 also used by simpletrace backend and tracepoints would cause
713 an infinite recursion! */
714 #ifdef _WIN32
715 extern int64_t clock_freq;
717 static inline int64_t get_clock(void)
719 LARGE_INTEGER ti;
720 QueryPerformanceCounter(&ti);
721 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
724 #else
726 extern int use_rt_clock;
728 static inline int64_t get_clock(void)
730 #ifdef CLOCK_MONOTONIC
731 if (use_rt_clock) {
732 struct timespec ts;
733 clock_gettime(CLOCK_MONOTONIC, &ts);
734 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
735 } else
736 #endif
738 /* XXX: using gettimeofday leads to problems if the date
739 changes, so it should be avoided. */
740 return get_clock_realtime();
743 #endif
745 /* icount */
746 int64_t cpu_get_icount(void);
747 int64_t cpu_get_clock(void);
749 /*******************************************/
750 /* host CPU ticks (if available) */
752 #if defined(_ARCH_PPC)
754 static inline int64_t cpu_get_real_ticks(void)
756 int64_t retval;
757 #ifdef _ARCH_PPC64
758 /* This reads timebase in one 64bit go and includes Cell workaround from:
759 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
761 __asm__ __volatile__ ("mftb %0\n\t"
762 "cmpwi %0,0\n\t"
763 "beq- $-8"
764 : "=r" (retval));
765 #else
766 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
767 unsigned long junk;
768 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
769 "mfspr %L0,268\n\t" /* mftb */
770 "mfspr %0,269\n\t" /* mftbu */
771 "cmpw %0,%1\n\t"
772 "bne $-16"
773 : "=r" (retval), "=r" (junk));
774 #endif
775 return retval;
778 #elif defined(__i386__)
780 static inline int64_t cpu_get_real_ticks(void)
782 int64_t val;
783 asm volatile ("rdtsc" : "=A" (val));
784 return val;
787 #elif defined(__x86_64__)
789 static inline int64_t cpu_get_real_ticks(void)
791 uint32_t low,high;
792 int64_t val;
793 asm volatile("rdtsc" : "=a" (low), "=d" (high));
794 val = high;
795 val <<= 32;
796 val |= low;
797 return val;
800 #elif defined(__hppa__)
802 static inline int64_t cpu_get_real_ticks(void)
804 int val;
805 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
806 return val;
809 #elif defined(__ia64)
811 static inline int64_t cpu_get_real_ticks(void)
813 int64_t val;
814 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
815 return val;
818 #elif defined(__s390__)
820 static inline int64_t cpu_get_real_ticks(void)
822 int64_t val;
823 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
824 return val;
827 #elif defined(__sparc__)
829 static inline int64_t cpu_get_real_ticks (void)
831 #if defined(_LP64)
832 uint64_t rval;
833 asm volatile("rd %%tick,%0" : "=r"(rval));
834 return rval;
835 #else
836 /* We need an %o or %g register for this. For recent enough gcc
837 there is an "h" constraint for that. Don't bother with that. */
838 union {
839 uint64_t i64;
840 struct {
841 uint32_t high;
842 uint32_t low;
843 } i32;
844 } rval;
845 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
846 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
847 return rval.i64;
848 #endif
851 #elif defined(__mips__) && \
852 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
854 * binutils wants to use rdhwr only on mips32r2
855 * but as linux kernel emulate it, it's fine
856 * to use it.
859 #define MIPS_RDHWR(rd, value) { \
860 __asm__ __volatile__ (".set push\n\t" \
861 ".set mips32r2\n\t" \
862 "rdhwr %0, "rd"\n\t" \
863 ".set pop" \
864 : "=r" (value)); \
867 static inline int64_t cpu_get_real_ticks(void)
869 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
870 uint32_t count;
871 static uint32_t cyc_per_count = 0;
873 if (!cyc_per_count) {
874 MIPS_RDHWR("$3", cyc_per_count);
877 MIPS_RDHWR("$2", count);
878 return (int64_t)(count * cyc_per_count);
881 #elif defined(__alpha__)
883 static inline int64_t cpu_get_real_ticks(void)
885 uint64_t cc;
886 uint32_t cur, ofs;
888 asm volatile("rpcc %0" : "=r"(cc));
889 cur = cc;
890 ofs = cc >> 32;
891 return cur - ofs;
894 #else
895 /* The host CPU doesn't have an easily accessible cycle counter.
896 Just return a monotonically increasing value. This will be
897 totally wrong, but hopefully better than nothing. */
898 static inline int64_t cpu_get_real_ticks (void)
900 static int64_t ticks = 0;
901 return ticks++;
903 #endif
905 #ifdef CONFIG_PROFILER
906 static inline int64_t profile_getclock(void)
908 return cpu_get_real_ticks();
911 extern int64_t qemu_time, qemu_time_start;
912 extern int64_t tlb_flush_time;
913 extern int64_t dev_time;
914 #endif
916 #endif