hrtimer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * include/linux/hrtimer.h
  3. *
  4. * hrtimers - High-resolution kernel timers
  5. *
  6. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
  8. *
  9. * data type definitions, declarations, prototypes
  10. *
  11. * Started by: Thomas Gleixner and Ingo Molnar
  12. *
  13. * For licencing details see kernel-base/COPYING
  14. */
  15. #ifndef _LINUX_HRTIMER_H
  16. #define _LINUX_HRTIMER_H
  17. #include <linux/rbtree.h>
  18. #include <linux/ktime.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/wait.h>
  22. struct hrtimer_clock_base;
  23. struct hrtimer_cpu_base;
  24. /*
  25. * Mode arguments of xxx_hrtimer functions:
  26. */
  27. enum hrtimer_mode {
  28. HRTIMER_MODE_ABS, /* Time value is absolute */
  29. HRTIMER_MODE_REL, /* Time value is relative to now */
  30. };
  31. /*
  32. * Return values for the callback function
  33. */
  34. enum hrtimer_restart {
  35. HRTIMER_NORESTART, /* Timer is not restarted */
  36. HRTIMER_RESTART, /* Timer must be restarted */
  37. };
  38. /*
  39. * hrtimer callback modes:
  40. *
  41. * HRTIMER_CB_SOFTIRQ: Callback must run in softirq context
  42. * HRTIMER_CB_IRQSAFE: Callback may run in hardirq context
  43. * HRTIMER_CB_IRQSAFE_NO_RESTART: Callback may run in hardirq context and
  44. * does not restart the timer
  45. * HRTIMER_CB_IRQSAFE_NO_SOFTIRQ: Callback must run in softirq context
  46. * Special mode for tick emultation
  47. */
  48. enum hrtimer_cb_mode {
  49. HRTIMER_CB_SOFTIRQ,
  50. HRTIMER_CB_IRQSAFE,
  51. HRTIMER_CB_IRQSAFE_NO_RESTART,
  52. HRTIMER_CB_IRQSAFE_NO_SOFTIRQ,
  53. };
  54. /*
  55. * Values to track state of the timer
  56. *
  57. * Possible states:
  58. *
  59. * 0x00 inactive
  60. * 0x01 enqueued into rbtree
  61. * 0x02 callback function running
  62. * 0x04 callback pending (high resolution mode)
  63. *
  64. * Special case:
  65. * 0x03 callback function running and enqueued
  66. * (was requeued on another CPU)
  67. * The "callback function running and enqueued" status is only possible on
  68. * SMP. It happens for example when a posix timer expired and the callback
  69. * queued a signal. Between dropping the lock which protects the posix timer
  70. * and reacquiring the base lock of the hrtimer, another CPU can deliver the
  71. * signal and rearm the timer. We have to preserve the callback running state,
  72. * as otherwise the timer could be removed before the softirq code finishes the
  73. * the handling of the timer.
  74. *
  75. * The HRTIMER_STATE_ENQUEUE bit is always or'ed to the current state to
  76. * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
  77. *
  78. * All state transitions are protected by cpu_base->lock.
  79. */
  80. #define HRTIMER_STATE_INACTIVE 0x00
  81. #define HRTIMER_STATE_ENQUEUED 0x01
  82. #define HRTIMER_STATE_CALLBACK 0x02
  83. #define HRTIMER_STATE_PENDING 0x04
  84. /**
  85. * struct hrtimer - the basic hrtimer structure
  86. * @node: red black tree node for time ordered insertion
  87. * @expires: the absolute expiry time in the hrtimers internal
  88. * representation. The time is related to the clock on
  89. * which the timer is based.
  90. * @function: timer expiry callback function
  91. * @base: pointer to the timer base (per cpu and per clock)
  92. * @state: state information (See bit values above)
  93. * @cb_mode: high resolution timer feature to select the callback execution
  94. * mode
  95. * @cb_entry: list head to enqueue an expired timer into the callback list
  96. * @start_site: timer statistics field to store the site where the timer
  97. * was started
  98. * @start_comm: timer statistics field to store the name of the process which
  99. * started the timer
  100. * @start_pid: timer statistics field to store the pid of the task which
  101. * started the timer
  102. *
  103. * The hrtimer structure must be initialized by hrtimer_init()
  104. */
  105. struct hrtimer {
  106. struct rb_node node;
  107. ktime_t expires;
  108. enum hrtimer_restart (*function)(struct hrtimer *);
  109. struct hrtimer_clock_base *base;
  110. unsigned long state;
  111. #ifdef CONFIG_HIGH_RES_TIMERS
  112. enum hrtimer_cb_mode cb_mode;
  113. struct list_head cb_entry;
  114. #endif
  115. #ifdef CONFIG_TIMER_STATS
  116. void *start_site;
  117. char start_comm[16];
  118. int start_pid;
  119. #endif
  120. };
  121. /**
  122. * struct hrtimer_sleeper - simple sleeper structure
  123. * @timer: embedded timer structure
  124. * @task: task to wake up
  125. *
  126. * task is set to NULL, when the timer expires.
  127. */
  128. struct hrtimer_sleeper {
  129. struct hrtimer timer;
  130. struct task_struct *task;
  131. };
  132. /**
  133. * struct hrtimer_base - the timer base for a specific clock
  134. * @cpu_base: per cpu clock base
  135. * @index: clock type index for per_cpu support when moving a
  136. * timer to a base on another cpu.
  137. * @active: red black tree root node for the active timers
  138. * @first: pointer to the timer node which expires first
  139. * @resolution: the resolution of the clock, in nanoseconds
  140. * @get_time: function to retrieve the current time of the clock
  141. * @get_softirq_time: function to retrieve the current time from the softirq
  142. * @softirq_time: the time when running the hrtimer queue in the softirq
  143. * @cb_pending: list of timers where the callback is pending
  144. * @offset: offset of this clock to the monotonic base
  145. * @reprogram: function to reprogram the timer event
  146. */
  147. struct hrtimer_clock_base {
  148. struct hrtimer_cpu_base *cpu_base;
  149. clockid_t index;
  150. struct rb_root active;
  151. struct rb_node *first;
  152. ktime_t resolution;
  153. ktime_t (*get_time)(void);
  154. ktime_t (*get_softirq_time)(void);
  155. ktime_t softirq_time;
  156. #ifdef CONFIG_HIGH_RES_TIMERS
  157. ktime_t offset;
  158. int (*reprogram)(struct hrtimer *t,
  159. struct hrtimer_clock_base *b,
  160. ktime_t n);
  161. #endif
  162. };
  163. #define HRTIMER_MAX_CLOCK_BASES 2
  164. /*
  165. * struct hrtimer_cpu_base - the per cpu clock bases
  166. * @lock: lock protecting the base and associated clock bases
  167. * and timers
  168. * @lock_key: the lock_class_key for use with lockdep
  169. * @clock_base: array of clock bases for this cpu
  170. * @curr_timer: the timer which is executing a callback right now
  171. * @expires_next: absolute time of the next event which was scheduled
  172. * via clock_set_next_event()
  173. * @hres_active: State of high resolution mode
  174. * @check_clocks: Indictator, when set evaluate time source and clock
  175. * event devices whether high resolution mode can be
  176. * activated.
  177. * @cb_pending: Expired timers are moved from the rbtree to this
  178. * list in the timer interrupt. The list is processed
  179. * in the softirq.
  180. * @nr_events: Total number of timer interrupt events
  181. */
  182. struct hrtimer_cpu_base {
  183. spinlock_t lock;
  184. struct lock_class_key lock_key;
  185. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  186. #ifdef CONFIG_HIGH_RES_TIMERS
  187. ktime_t expires_next;
  188. int hres_active;
  189. struct list_head cb_pending;
  190. unsigned long nr_events;
  191. #endif
  192. };
  193. #ifdef CONFIG_HIGH_RES_TIMERS
  194. struct clock_event_device;
  195. extern void clock_was_set(void);
  196. extern void hrtimer_interrupt(struct clock_event_device *dev);
  197. /*
  198. * In high resolution mode the time reference must be read accurate
  199. */
  200. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  201. {
  202. return timer->base->get_time();
  203. }
  204. /*
  205. * The resolution of the clocks. The resolution value is returned in
  206. * the clock_getres() system call to give application programmers an
  207. * idea of the (in)accuracy of timers. Timer values are rounded up to
  208. * this resolution values.
  209. */
  210. # define KTIME_HIGH_RES (ktime_t) { .tv64 = 1 }
  211. # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
  212. #else
  213. # define KTIME_MONOTONIC_RES KTIME_LOW_RES
  214. /*
  215. * clock_was_set() is a NOP for non- high-resolution systems. The
  216. * time-sorted order guarantees that a timer does not expire early and
  217. * is expired in the next softirq when the clock was advanced.
  218. */
  219. static inline void clock_was_set(void) { }
  220. /*
  221. * In non high resolution mode the time reference is taken from
  222. * the base softirq time variable.
  223. */
  224. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  225. {
  226. return timer->base->softirq_time;
  227. }
  228. #endif
  229. extern ktime_t ktime_get(void);
  230. extern ktime_t ktime_get_real(void);
  231. /* Exported timer functions: */
  232. /* Initialize timers: */
  233. extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
  234. enum hrtimer_mode mode);
  235. /* Basic timer operations: */
  236. extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
  237. const enum hrtimer_mode mode);
  238. extern int hrtimer_cancel(struct hrtimer *timer);
  239. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  240. static inline int hrtimer_restart(struct hrtimer *timer)
  241. {
  242. return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
  243. }
  244. /* Query timers: */
  245. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  246. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  247. extern ktime_t hrtimer_get_next_event(void);
  248. /*
  249. * A timer is active, when it is enqueued into the rbtree or the callback
  250. * function is running.
  251. */
  252. static inline int hrtimer_active(const struct hrtimer *timer)
  253. {
  254. return timer->state != HRTIMER_STATE_INACTIVE;
  255. }
  256. /*
  257. * Helper function to check, whether the timer is on one of the queues
  258. */
  259. static inline int hrtimer_is_queued(struct hrtimer *timer)
  260. {
  261. return timer->state &
  262. (HRTIMER_STATE_ENQUEUED | HRTIMER_STATE_PENDING);
  263. }
  264. /* Forward a hrtimer so it expires after now: */
  265. extern unsigned long
  266. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
  267. /* Precise sleep: */
  268. extern long hrtimer_nanosleep(struct timespec *rqtp,
  269. struct timespec __user *rmtp,
  270. const enum hrtimer_mode mode,
  271. const clockid_t clockid);
  272. extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
  273. extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  274. struct task_struct *tsk);
  275. /* Soft interrupt function to run the hrtimer queues: */
  276. extern void hrtimer_run_queues(void);
  277. /* Bootup initialization: */
  278. extern void __init hrtimers_init(void);
  279. #if BITS_PER_LONG < 64
  280. extern unsigned long ktime_divns(const ktime_t kt, s64 div);
  281. #else /* BITS_PER_LONG < 64 */
  282. # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
  283. #endif
  284. /* Show pending timers: */
  285. extern void sysrq_timer_list_show(void);
  286. /*
  287. * Timer-statistics info:
  288. */
  289. #ifdef CONFIG_TIMER_STATS
  290. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  291. void *timerf, char * comm);
  292. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  293. {
  294. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  295. timer->function, timer->start_comm);
  296. }
  297. extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
  298. void *addr);
  299. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  300. {
  301. __timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
  302. }
  303. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  304. {
  305. timer->start_site = NULL;
  306. }
  307. #else
  308. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  309. {
  310. }
  311. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  312. {
  313. }
  314. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  315. {
  316. }
  317. #endif
  318. #endif