hw/arm/allwinner-h3: add EMAC ethernet device
[qemu/ar7.git] / include / qemu / timer.h
blob6a8b48b5a9d893db828a3023b6d41c7bc002a2fa
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
4 #include "qemu/bitops.h"
5 #include "qemu/notify.h"
6 #include "qemu/host-utils.h"
8 #define NANOSECONDS_PER_SECOND 1000000000LL
10 /* timers */
12 #define SCALE_MS 1000000
13 #define SCALE_US 1000
14 #define SCALE_NS 1
16 /**
17 * QEMUClockType:
19 * The following clock types are available:
21 * @QEMU_CLOCK_REALTIME: Real time clock
23 * The real time clock should be used only for stuff which does not
24 * change the virtual machine state, as it runs even if the virtual
25 * machine is stopped.
27 * @QEMU_CLOCK_VIRTUAL: virtual clock
29 * The virtual clock only runs during the emulation. It stops
30 * when the virtual machine is stopped.
32 * @QEMU_CLOCK_HOST: host clock
34 * The host clock should be used 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).
39 * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
41 * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
42 * In icount mode, this clock counts nanoseconds while the virtual
43 * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
44 * while the CPUs are sleeping and thus not executing instructions.
47 typedef enum {
48 QEMU_CLOCK_REALTIME = 0,
49 QEMU_CLOCK_VIRTUAL = 1,
50 QEMU_CLOCK_HOST = 2,
51 QEMU_CLOCK_VIRTUAL_RT = 3,
52 QEMU_CLOCK_MAX
53 } QEMUClockType;
55 /**
56 * QEMU Timer attributes:
58 * An individual timer may be given one or multiple attributes when initialized.
59 * Each attribute corresponds to one bit. Attributes modify the processing
60 * of timers when they fire.
62 * The following attributes are available:
64 * QEMU_TIMER_ATTR_EXTERNAL: drives external subsystem
65 * QEMU_TIMER_ATTR_ALL: mask for all existing attributes
67 * Timers with this attribute do not recorded in rr mode, therefore it could be
68 * used for the subsystems that operate outside the guest core. Applicable only
69 * with virtual clock type.
72 #define QEMU_TIMER_ATTR_EXTERNAL ((int)BIT(0))
73 #define QEMU_TIMER_ATTR_ALL 0xffffffff
75 typedef struct QEMUTimerList QEMUTimerList;
77 struct QEMUTimerListGroup {
78 QEMUTimerList *tl[QEMU_CLOCK_MAX];
81 typedef void QEMUTimerCB(void *opaque);
82 typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
84 struct QEMUTimer {
85 int64_t expire_time; /* in nanoseconds */
86 QEMUTimerList *timer_list;
87 QEMUTimerCB *cb;
88 void *opaque;
89 QEMUTimer *next;
90 int attributes;
91 int scale;
94 extern QEMUTimerListGroup main_loop_tlg;
97 * qemu_clock_get_ns;
98 * @type: the clock type
100 * Get the nanosecond value of a clock with
101 * type @type
103 * Returns: the clock value in nanoseconds
105 int64_t qemu_clock_get_ns(QEMUClockType type);
108 * qemu_clock_get_ms;
109 * @type: the clock type
111 * Get the millisecond value of a clock with
112 * type @type
114 * Returns: the clock value in milliseconds
116 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
118 return qemu_clock_get_ns(type) / SCALE_MS;
122 * qemu_clock_get_us;
123 * @type: the clock type
125 * Get the microsecond value of a clock with
126 * type @type
128 * Returns: the clock value in microseconds
130 static inline int64_t qemu_clock_get_us(QEMUClockType type)
132 return qemu_clock_get_ns(type) / SCALE_US;
136 * qemu_clock_has_timers:
137 * @type: the clock type
139 * Determines whether a clock's default timer list
140 * has timers attached
142 * Note that this function should not be used when other threads also access
143 * the timer list. The return value may be outdated by the time it is acted
144 * upon.
146 * Returns: true if the clock's default timer list
147 * has timers attached
149 bool qemu_clock_has_timers(QEMUClockType type);
152 * qemu_clock_expired:
153 * @type: the clock type
155 * Determines whether a clock's default timer list
156 * has an expired timer.
158 * Returns: true if the clock's default timer list has
159 * an expired timer
161 bool qemu_clock_expired(QEMUClockType type);
164 * qemu_clock_use_for_deadline:
165 * @type: the clock type
167 * Determine whether a clock should be used for deadline
168 * calculations. Some clocks, for instance vm_clock with
169 * use_icount set, do not count in nanoseconds. Such clocks
170 * are not used for deadline calculations, and are presumed
171 * to interrupt any poll using qemu_notify/aio_notify
172 * etc.
174 * Returns: true if the clock runs in nanoseconds and
175 * should be used for a deadline.
177 bool qemu_clock_use_for_deadline(QEMUClockType type);
180 * qemu_clock_deadline_ns_all:
181 * @type: the clock type
182 * @attr_mask: mask for the timer attributes that are included
183 * in deadline calculation
185 * Calculate the deadline across all timer lists associated
186 * with a clock (as opposed to just the default one)
187 * in nanoseconds, or -1 if no timer is set to expire.
189 * Returns: time until expiry in nanoseconds or -1
191 int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask);
194 * qemu_clock_get_main_loop_timerlist:
195 * @type: the clock type
197 * Return the default timer list associated with a clock.
199 * Returns: the default timer list
201 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
204 * qemu_clock_nofify:
205 * @type: the clock type
207 * Call the notifier callback connected with the default timer
208 * list linked to the clock, or qemu_notify() if none.
210 void qemu_clock_notify(QEMUClockType type);
213 * qemu_clock_enable:
214 * @type: the clock type
215 * @enabled: true to enable, false to disable
217 * Enable or disable a clock
218 * Disabling the clock will wait for related timerlists to stop
219 * executing qemu_run_timers. Thus, this functions should not
220 * be used from the callback of a timer that is based on @clock.
221 * Doing so would cause a deadlock.
223 * Caller should hold BQL.
225 void qemu_clock_enable(QEMUClockType type, bool enabled);
228 * qemu_start_warp_timer:
230 * Starts a timer for virtual clock update
232 void qemu_start_warp_timer(void);
235 * qemu_clock_run_timers:
236 * @type: clock on which to operate
238 * Run all the timers associated with the default timer list
239 * of a clock.
241 * Returns: true if any timer ran.
243 bool qemu_clock_run_timers(QEMUClockType type);
246 * qemu_clock_run_all_timers:
248 * Run all the timers associated with the default timer list
249 * of every clock.
251 * Returns: true if any timer ran.
253 bool qemu_clock_run_all_timers(void);
257 * QEMUTimerList
261 * timerlist_new:
262 * @type: the clock type to associate with the timerlist
263 * @cb: the callback to call on notification
264 * @opaque: the opaque pointer to pass to the callback
266 * Create a new timerlist associated with the clock of
267 * type @type.
269 * Returns: a pointer to the QEMUTimerList created
271 QEMUTimerList *timerlist_new(QEMUClockType type,
272 QEMUTimerListNotifyCB *cb, void *opaque);
275 * timerlist_free:
276 * @timer_list: the timer list to free
278 * Frees a timer_list. It must have no active timers.
280 void timerlist_free(QEMUTimerList *timer_list);
283 * timerlist_has_timers:
284 * @timer_list: the timer list to operate on
286 * Determine whether a timer list has active timers
288 * Note that this function should not be used when other threads also access
289 * the timer list. The return value may be outdated by the time it is acted
290 * upon.
292 * Returns: true if the timer list has timers.
294 bool timerlist_has_timers(QEMUTimerList *timer_list);
297 * timerlist_expired:
298 * @timer_list: the timer list to operate on
300 * Determine whether a timer list has any timers which
301 * are expired.
303 * Returns: true if the timer list has timers which
304 * have expired.
306 bool timerlist_expired(QEMUTimerList *timer_list);
309 * timerlist_deadline_ns:
310 * @timer_list: the timer list to operate on
312 * Determine the deadline for a timer_list, i.e.
313 * the number of nanoseconds until the first timer
314 * expires. Return -1 if there are no timers.
316 * Returns: the number of nanoseconds until the earliest
317 * timer expires -1 if none
319 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
322 * timerlist_get_clock:
323 * @timer_list: the timer list to operate on
325 * Determine the clock type associated with a timer list.
327 * Returns: the clock type associated with the
328 * timer list.
330 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
333 * timerlist_run_timers:
334 * @timer_list: the timer list to use
336 * Call all expired timers associated with the timer list.
338 * Returns: true if any timer expired
340 bool timerlist_run_timers(QEMUTimerList *timer_list);
343 * timerlist_notify:
344 * @timer_list: the timer list to use
346 * call the notifier callback associated with the timer list.
348 void timerlist_notify(QEMUTimerList *timer_list);
351 * QEMUTimerListGroup
355 * timerlistgroup_init:
356 * @tlg: the timer list group
357 * @cb: the callback to call when a notify is required
358 * @opaque: the opaque pointer to be passed to the callback.
360 * Initialise a timer list group. This must already be
361 * allocated in memory and zeroed. The notifier callback is
362 * called whenever a clock in the timer list group is
363 * reenabled or whenever a timer associated with any timer
364 * list is modified. If @cb is specified as null, qemu_notify()
365 * is used instead.
367 void timerlistgroup_init(QEMUTimerListGroup *tlg,
368 QEMUTimerListNotifyCB *cb, void *opaque);
371 * timerlistgroup_deinit:
372 * @tlg: the timer list group
374 * Deinitialise a timer list group. This must already be
375 * initialised. Note the memory is not freed.
377 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
380 * timerlistgroup_run_timers:
381 * @tlg: the timer list group
383 * Run the timers associated with a timer list group.
384 * This will run timers on multiple clocks.
386 * Returns: true if any timer callback ran
388 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
391 * timerlistgroup_deadline_ns:
392 * @tlg: the timer list group
394 * Determine the deadline of the soonest timer to
395 * expire associated with any timer list linked to
396 * the timer list group. Only clocks suitable for
397 * deadline calculation are included.
399 * Returns: the deadline in nanoseconds or -1 if no
400 * timers are to expire.
402 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
405 * QEMUTimer
409 * timer_init_full:
410 * @ts: the timer to be initialised
411 * @timer_list_group: (optional) the timer list group to attach the timer to
412 * @type: the clock type to use
413 * @scale: the scale value for the timer
414 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
415 * @cb: the callback to be called when the timer expires
416 * @opaque: the opaque pointer to be passed to the callback
418 * Initialise a timer with the given scale and attributes,
419 * and associate it with timer list for given clock @type in @timer_list_group
420 * (or default timer list group, if NULL).
421 * The caller is responsible for allocating the memory.
423 * You need not call an explicit deinit call. Simply make
424 * sure it is not on a list with timer_del.
426 void timer_init_full(QEMUTimer *ts,
427 QEMUTimerListGroup *timer_list_group, QEMUClockType type,
428 int scale, int attributes,
429 QEMUTimerCB *cb, void *opaque);
432 * timer_init:
433 * @ts: the timer to be initialised
434 * @type: the clock to associate with the timer
435 * @scale: the scale value for the timer
436 * @cb: the callback to call when the timer expires
437 * @opaque: the opaque pointer to pass to the callback
439 * Initialize a timer with the given scale on the default timer list
440 * associated with the clock.
441 * See timer_init_full for details.
443 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
444 QEMUTimerCB *cb, void *opaque)
446 timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
450 * timer_init_ns:
451 * @ts: the timer to be initialised
452 * @type: the clock to associate with the timer
453 * @cb: the callback to call when the timer expires
454 * @opaque: the opaque pointer to pass to the callback
456 * Initialize a timer with nanosecond scale on the default timer list
457 * associated with the clock.
458 * See timer_init_full for details.
460 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
461 QEMUTimerCB *cb, void *opaque)
463 timer_init(ts, type, SCALE_NS, cb, opaque);
467 * timer_init_us:
468 * @ts: the timer to be initialised
469 * @type: the clock to associate with the timer
470 * @cb: the callback to call when the timer expires
471 * @opaque: the opaque pointer to pass to the callback
473 * Initialize a timer with microsecond scale on the default timer list
474 * associated with the clock.
475 * See timer_init_full for details.
477 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
478 QEMUTimerCB *cb, void *opaque)
480 timer_init(ts, type, SCALE_US, cb, opaque);
484 * timer_init_ms:
485 * @ts: the timer to be initialised
486 * @type: the clock to associate with the timer
487 * @cb: the callback to call when the timer expires
488 * @opaque: the opaque pointer to pass to the callback
490 * Initialize a timer with millisecond scale on the default timer list
491 * associated with the clock.
492 * See timer_init_full for details.
494 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
495 QEMUTimerCB *cb, void *opaque)
497 timer_init(ts, type, SCALE_MS, cb, opaque);
501 * timer_new_full:
502 * @timer_list_group: (optional) the timer list group to attach the timer to
503 * @type: the clock type to use
504 * @scale: the scale value for the timer
505 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
506 * @cb: the callback to be called when the timer expires
507 * @opaque: the opaque pointer to be passed to the callback
509 * Create a new timer with the given scale and attributes,
510 * and associate it with timer list for given clock @type in @timer_list_group
511 * (or default timer list group, if NULL).
512 * The memory is allocated by the function.
514 * This is not the preferred interface unless you know you
515 * are going to call timer_free. Use timer_init or timer_init_full instead.
517 * The default timer list has one special feature: in icount mode,
518 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
519 * not true of other timer lists, which are typically associated
520 * with an AioContext---each of them runs its timer callbacks in its own
521 * AioContext thread.
523 * Returns: a pointer to the timer
525 static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
526 QEMUClockType type,
527 int scale, int attributes,
528 QEMUTimerCB *cb, void *opaque)
530 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
531 timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
532 return ts;
536 * timer_new:
537 * @type: the clock type to use
538 * @scale: the scale value for the timer
539 * @cb: the callback to be called when the timer expires
540 * @opaque: the opaque pointer to be passed to the callback
542 * Create a new timer with the given scale,
543 * and associate it with the default timer list for the clock type @type.
544 * See timer_new_full for details.
546 * Returns: a pointer to the timer
548 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
549 QEMUTimerCB *cb, void *opaque)
551 return timer_new_full(NULL, type, scale, 0, cb, opaque);
555 * timer_new_ns:
556 * @type: the clock type to associate with the timer
557 * @cb: the callback to call when the timer expires
558 * @opaque: the opaque pointer to pass to the callback
560 * Create a new timer with nanosecond scale on the default timer list
561 * associated with the clock.
562 * See timer_new_full for details.
564 * Returns: a pointer to the newly created timer
566 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
567 void *opaque)
569 return timer_new(type, SCALE_NS, cb, opaque);
573 * timer_new_us:
574 * @type: the clock type to associate with the timer
575 * @cb: the callback to call when the timer expires
576 * @opaque: the opaque pointer to pass to the callback
578 * Create a new timer with microsecond scale on the default timer list
579 * associated with the clock.
580 * See timer_new_full for details.
582 * Returns: a pointer to the newly created timer
584 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
585 void *opaque)
587 return timer_new(type, SCALE_US, cb, opaque);
591 * timer_new_ms:
592 * @type: the clock type to associate with the timer
593 * @cb: the callback to call when the timer expires
594 * @opaque: the opaque pointer to pass to the callback
596 * Create a new timer with millisecond scale on the default timer list
597 * associated with the clock.
598 * See timer_new_full for details.
600 * Returns: a pointer to the newly created timer
602 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
603 void *opaque)
605 return timer_new(type, SCALE_MS, cb, opaque);
609 * timer_deinit:
610 * @ts: the timer to be de-initialised
612 * Deassociate the timer from any timerlist. You should
613 * call timer_del before. After this call, any further
614 * timer_del call cannot cause dangling pointer accesses
615 * even if the previously used timerlist is freed.
617 void timer_deinit(QEMUTimer *ts);
620 * timer_free:
621 * @ts: the timer
623 * Free a timer (it must not be on the active list)
625 static inline void timer_free(QEMUTimer *ts)
627 g_free(ts);
631 * timer_del:
632 * @ts: the timer
634 * Delete a timer from the active list.
636 * This function is thread-safe but the timer and its timer list must not be
637 * freed while this function is running.
639 void timer_del(QEMUTimer *ts);
642 * timer_mod_ns:
643 * @ts: the timer
644 * @expire_time: the expiry time in nanoseconds
646 * Modify a timer to expire at @expire_time
648 * This function is thread-safe but the timer and its timer list must not be
649 * freed while this function is running.
651 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
654 * timer_mod_anticipate_ns:
655 * @ts: the timer
656 * @expire_time: the expiry time in nanoseconds
658 * Modify a timer to expire at @expire_time or the current time,
659 * whichever comes earlier.
661 * This function is thread-safe but the timer and its timer list must not be
662 * freed while this function is running.
664 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
667 * timer_mod:
668 * @ts: the timer
669 * @expire_time: the expire time in the units associated with the timer
671 * Modify a timer to expiry at @expire_time, taking into
672 * account the scale associated with the timer.
674 * This function is thread-safe but the timer and its timer list must not be
675 * freed while this function is running.
677 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
680 * timer_mod_anticipate:
681 * @ts: the timer
682 * @expire_time: the expiry time in nanoseconds
684 * Modify a timer to expire at @expire_time or the current time, whichever
685 * comes earlier, taking into account the scale associated with the timer.
687 * This function is thread-safe but the timer and its timer list must not be
688 * freed while this function is running.
690 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
693 * timer_pending:
694 * @ts: the timer
696 * Determines whether a timer is pending (i.e. is on the
697 * active list of timers, whether or not it has not yet expired).
699 * Returns: true if the timer is pending
701 bool timer_pending(QEMUTimer *ts);
704 * timer_expired:
705 * @ts: the timer
706 * @current_time: the current time
708 * Determines whether a timer has expired.
710 * Returns: true if the timer has expired
712 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
715 * timer_expire_time_ns:
716 * @ts: the timer
718 * Determine the expiry time of a timer
720 * Returns: the expiry time in nanoseconds
722 uint64_t timer_expire_time_ns(QEMUTimer *ts);
725 * timer_get:
726 * @f: the file
727 * @ts: the timer
729 * Read a timer @ts from a file @f
731 void timer_get(QEMUFile *f, QEMUTimer *ts);
734 * timer_put:
735 * @f: the file
736 * @ts: the timer
738 void timer_put(QEMUFile *f, QEMUTimer *ts);
741 * General utility functions
745 * qemu_timeout_ns_to_ms:
746 * @ns: nanosecond timeout value
748 * Convert a nanosecond timeout value (or -1) to
749 * a millisecond value (or -1), always rounding up.
751 * Returns: millisecond timeout value
753 int qemu_timeout_ns_to_ms(int64_t ns);
756 * qemu_poll_ns:
757 * @fds: Array of file descriptors
758 * @nfds: number of file descriptors
759 * @timeout: timeout in nanoseconds
761 * Perform a poll like g_poll but with a timeout in nanoseconds.
762 * See g_poll documentation for further details.
764 * Returns: number of fds ready
766 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
769 * qemu_soonest_timeout:
770 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
771 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
773 * Calculates the soonest of two timeout values. -1 means infinite, which
774 * is later than any other value.
776 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
778 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
780 /* we can abuse the fact that -1 (which means infinite) is a maximal
781 * value when cast to unsigned. As this is disgusting, it's kept in
782 * one inline function.
784 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
788 * initclocks:
790 * Initialise the clock & timer infrastructure
792 void init_clocks(QEMUTimerListNotifyCB *notify_cb);
794 int64_t cpu_get_ticks(void);
795 /* Caller must hold BQL */
796 void cpu_enable_ticks(void);
797 /* Caller must hold BQL */
798 void cpu_disable_ticks(void);
800 static inline int64_t get_max_clock_jump(void)
802 /* This should be small enough to prevent excessive interrupts from being
803 * generated by the RTC on clock jumps, but large enough to avoid frequent
804 * unnecessary resets in idle VMs.
806 return 60 * NANOSECONDS_PER_SECOND;
810 * Low level clock functions
813 /* get host real time in nanosecond */
814 static inline int64_t get_clock_realtime(void)
816 struct timeval tv;
818 gettimeofday(&tv, NULL);
819 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
822 /* Warning: don't insert tracepoints into these functions, they are
823 also used by simpletrace backend and tracepoints would cause
824 an infinite recursion! */
825 #ifdef _WIN32
826 extern int64_t clock_freq;
828 static inline int64_t get_clock(void)
830 LARGE_INTEGER ti;
831 QueryPerformanceCounter(&ti);
832 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
835 #else
837 extern int use_rt_clock;
839 static inline int64_t get_clock(void)
841 if (use_rt_clock) {
842 struct timespec ts;
843 clock_gettime(CLOCK_MONOTONIC, &ts);
844 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
845 } else {
846 /* XXX: using gettimeofday leads to problems if the date
847 changes, so it should be avoided. */
848 return get_clock_realtime();
851 #endif
853 /* icount */
854 int64_t cpu_get_icount_raw(void);
855 int64_t cpu_get_icount(void);
856 int64_t cpu_get_clock(void);
857 int64_t cpu_icount_to_ns(int64_t icount);
858 void cpu_update_icount(CPUState *cpu);
860 /*******************************************/
861 /* host CPU ticks (if available) */
863 #if defined(_ARCH_PPC)
865 static inline int64_t cpu_get_host_ticks(void)
867 int64_t retval;
868 #ifdef _ARCH_PPC64
869 /* This reads timebase in one 64bit go and includes Cell workaround from:
870 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
872 __asm__ __volatile__ ("mftb %0\n\t"
873 "cmpwi %0,0\n\t"
874 "beq- $-8"
875 : "=r" (retval));
876 #else
877 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
878 unsigned long junk;
879 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
880 "mfspr %L0,268\n\t" /* mftb */
881 "mfspr %0,269\n\t" /* mftbu */
882 "cmpw %0,%1\n\t"
883 "bne $-16"
884 : "=r" (retval), "=r" (junk));
885 #endif
886 return retval;
889 #elif defined(__i386__)
891 static inline int64_t cpu_get_host_ticks(void)
893 int64_t val;
894 asm volatile ("rdtsc" : "=A" (val));
895 return val;
898 #elif defined(__x86_64__)
900 static inline int64_t cpu_get_host_ticks(void)
902 uint32_t low,high;
903 int64_t val;
904 asm volatile("rdtsc" : "=a" (low), "=d" (high));
905 val = high;
906 val <<= 32;
907 val |= low;
908 return val;
911 #elif defined(__hppa__)
913 static inline int64_t cpu_get_host_ticks(void)
915 int val;
916 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
917 return val;
920 #elif defined(__s390__)
922 static inline int64_t cpu_get_host_ticks(void)
924 int64_t val;
925 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
926 return val;
929 #elif defined(__sparc__)
931 static inline int64_t cpu_get_host_ticks (void)
933 #if defined(_LP64)
934 uint64_t rval;
935 asm volatile("rd %%tick,%0" : "=r"(rval));
936 return rval;
937 #else
938 /* We need an %o or %g register for this. For recent enough gcc
939 there is an "h" constraint for that. Don't bother with that. */
940 union {
941 uint64_t i64;
942 struct {
943 uint32_t high;
944 uint32_t low;
945 } i32;
946 } rval;
947 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
948 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
949 return rval.i64;
950 #endif
953 #elif defined(__mips__) && \
954 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
956 * binutils wants to use rdhwr only on mips32r2
957 * but as linux kernel emulate it, it's fine
958 * to use it.
961 #define MIPS_RDHWR(rd, value) { \
962 __asm__ __volatile__ (".set push\n\t" \
963 ".set mips32r2\n\t" \
964 "rdhwr %0, "rd"\n\t" \
965 ".set pop" \
966 : "=r" (value)); \
969 static inline int64_t cpu_get_host_ticks(void)
971 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
972 uint32_t count;
973 static uint32_t cyc_per_count = 0;
975 if (!cyc_per_count) {
976 MIPS_RDHWR("$3", cyc_per_count);
979 MIPS_RDHWR("$2", count);
980 return (int64_t)(count * cyc_per_count);
983 #elif defined(__alpha__)
985 static inline int64_t cpu_get_host_ticks(void)
987 uint64_t cc;
988 uint32_t cur, ofs;
990 asm volatile("rpcc %0" : "=r"(cc));
991 cur = cc;
992 ofs = cc >> 32;
993 return cur - ofs;
996 #else
997 /* The host CPU doesn't have an easily accessible cycle counter.
998 Just return a monotonically increasing value. This will be
999 totally wrong, but hopefully better than nothing. */
1000 static inline int64_t cpu_get_host_ticks(void)
1002 return get_clock();
1004 #endif
1006 #ifdef CONFIG_PROFILER
1007 static inline int64_t profile_getclock(void)
1009 return get_clock();
1012 extern int64_t dev_time;
1013 #endif
1015 #endif