1 /*
2 * Copyright (c) 2017-2019 François Tigeot <ftigeot@wolfpond.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <linux/hrtimer.h>
28 #include <linux/bug.h>
29
30 static inline void
__hrtimer_function(void * arg)31 __hrtimer_function(void *arg)
32 {
33 struct hrtimer *timer = arg;
34 enum hrtimer_restart restart = HRTIMER_RESTART;
35
36 if (timer->function) {
37 restart = timer->function(timer);
38 }
39
40 if (restart == HRTIMER_RESTART) {
41 /*
42 * XXX Not implemented yet, we would need to store the
43 * expiration period, to do the callout_reset here.
44 */
45 } else {
46 timer->active = false;
47 }
48 }
49
hrtimer_init(struct hrtimer * timer,clockid_t clock_id,enum hrtimer_mode mode)50 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
51 enum hrtimer_mode mode)
52 {
53 BUG_ON(clock_id != CLOCK_MONOTONIC);
54
55 memset(timer, 0, sizeof(struct hrtimer));
56 timer->clock_id = clock_id;
57 timer->ht_mode = mode;
58
59 lwkt_token_init(&timer->timer_token, "timer token");
60 callout_init_mp(&(timer)->timer_callout);
61 }
62
63 void
hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 range_ns,const enum hrtimer_mode mode)64 hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
65 u64 range_ns, const enum hrtimer_mode mode)
66 {
67 int expire_ticks = tim / (NSEC_PER_SEC / hz);
68
69 if (mode == HRTIMER_MODE_ABS)
70 expire_ticks -= ticks;
71
72 if (expire_ticks <= 0)
73 expire_ticks = 1;
74
75 lwkt_gettoken(&timer->timer_token);
76
77 timer->active = true;
78 callout_reset(&timer->timer_callout,
79 expire_ticks, __hrtimer_function, timer);
80
81 lwkt_reltoken(&timer->timer_token);
82 }
83
84 int
hrtimer_cancel(struct hrtimer * timer)85 hrtimer_cancel(struct hrtimer *timer)
86 {
87 return callout_cancel(&timer->timer_callout) == 0;
88 }
89
90 /* Returns non-zero if the timer is already on the queue */
91 bool
hrtimer_active(const struct hrtimer * const_timer)92 hrtimer_active(const struct hrtimer *const_timer)
93 {
94 struct hrtimer *timer = __DECONST(struct hrtimer *, const_timer);
95 return callout_pending(&timer->timer_callout);
96 }
97