1 /*-
2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/time.h>
35
36 #include <machine/cpu.h>
37
38 #include <linux/hrtimer.h>
39
40 static void
hrtimer_call_handler(void * arg)41 hrtimer_call_handler(void *arg)
42 {
43 struct hrtimer *hrtimer;
44 enum hrtimer_restart ret;
45
46 hrtimer = arg;
47 ret = hrtimer->function(hrtimer);
48
49 if (ret == HRTIMER_RESTART) {
50 callout_schedule_sbt(&hrtimer->callout,
51 nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0);
52 } else {
53 callout_deactivate(&hrtimer->callout);
54 }
55 }
56
57 bool
linux_hrtimer_active(struct hrtimer * hrtimer)58 linux_hrtimer_active(struct hrtimer *hrtimer)
59 {
60 bool ret;
61
62 mtx_lock(&hrtimer->mtx);
63 ret = callout_active(&hrtimer->callout);
64 mtx_unlock(&hrtimer->mtx);
65
66 return (ret);
67 }
68
69 /*
70 * Cancel active hrtimer.
71 * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
72 */
73 int
linux_hrtimer_cancel(struct hrtimer * hrtimer)74 linux_hrtimer_cancel(struct hrtimer *hrtimer)
75 {
76
77 return (callout_drain(&hrtimer->callout) > 0);
78 }
79
80 void
linux_hrtimer_init(struct hrtimer * hrtimer)81 linux_hrtimer_init(struct hrtimer *hrtimer)
82 {
83
84 memset(hrtimer, 0, sizeof(*hrtimer));
85 mtx_init(&hrtimer->mtx, "hrtimer", NULL,
86 MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
87 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
88 }
89
90 void
linux_hrtimer_set_expires(struct hrtimer * hrtimer,ktime_t time)91 linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)
92 {
93 hrtimer->expires = ktime_to_ns(time);
94 }
95
96 void
linux_hrtimer_start(struct hrtimer * hrtimer,ktime_t time)97 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
98 {
99
100 linux_hrtimer_start_range_ns(hrtimer, time, 0);
101 }
102
103 void
linux_hrtimer_start_range_ns(struct hrtimer * hrtimer,ktime_t time,int64_t nsec)104 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,
105 int64_t nsec)
106 {
107
108 mtx_lock(&hrtimer->mtx);
109 hrtimer->precision = nsec;
110 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),
111 nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);
112 mtx_unlock(&hrtimer->mtx);
113 }
114
115 void
linux_hrtimer_forward_now(struct hrtimer * hrtimer,ktime_t interval)116 linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)
117 {
118
119 mtx_lock(&hrtimer->mtx);
120 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),
121 nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);
122 mtx_unlock(&hrtimer->mtx);
123 }
124
125