2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice unmodified, this list of conditions, and the following
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/mutex.h>
35 #include <machine/cpu.h>
37 #include <linux/hrtimer.h>
40 hrtimer_call_handler(void *arg)
42 struct hrtimer *hrtimer;
43 enum hrtimer_restart ret;
46 ret = hrtimer->function(hrtimer);
48 if (ret == HRTIMER_RESTART) {
49 callout_schedule_sbt(&hrtimer->callout,
50 nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0);
52 callout_deactivate(&hrtimer->callout);
57 linux_hrtimer_active(struct hrtimer *hrtimer)
61 mtx_lock(&hrtimer->mtx);
62 ret = callout_active(&hrtimer->callout);
63 mtx_unlock(&hrtimer->mtx);
69 * Cancel active hrtimer.
70 * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
73 linux_hrtimer_cancel(struct hrtimer *hrtimer)
76 return (callout_drain(&hrtimer->callout) > 0);
80 linux_hrtimer_init(struct hrtimer *hrtimer)
83 memset(hrtimer, 0, sizeof(*hrtimer));
84 mtx_init(&hrtimer->mtx, "hrtimer", NULL,
85 MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
86 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
90 linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)
92 hrtimer->expires = ktime_to_ns(time);
96 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
99 linux_hrtimer_start_range_ns(hrtimer, time, 0);
103 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,
107 mtx_lock(&hrtimer->mtx);
108 hrtimer->precision = nsec;
109 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),
110 nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);
111 mtx_unlock(&hrtimer->mtx);
115 linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)
118 mtx_lock(&hrtimer->mtx);
119 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),
120 nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);
121 mtx_unlock(&hrtimer->mtx);