1*9ea3e141SMark Johnston /*-
2*9ea3e141SMark Johnston  * Copyright (c) 2017 Mark Johnston <[email protected]>
3*9ea3e141SMark Johnston  * All rights reserved.
4*9ea3e141SMark Johnston  *
5*9ea3e141SMark Johnston  * Redistribution and use in source and binary forms, with or without
6*9ea3e141SMark Johnston  * modification, are permitted provided that the following conditions
7*9ea3e141SMark Johnston  * are met:
8*9ea3e141SMark Johnston  * 1. Redistributions of source code must retain the above copyright
9*9ea3e141SMark Johnston  *    notice unmodified, this list of conditions, and the following
10*9ea3e141SMark Johnston  *    disclaimer.
11*9ea3e141SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
12*9ea3e141SMark Johnston  *    notice, this list of conditions and the following disclaimer in the
13*9ea3e141SMark Johnston  *    documentation and/or other materials provided with the distribution.
14*9ea3e141SMark Johnston  *
15*9ea3e141SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*9ea3e141SMark Johnston  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*9ea3e141SMark Johnston  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*9ea3e141SMark Johnston  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*9ea3e141SMark Johnston  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*9ea3e141SMark Johnston  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*9ea3e141SMark Johnston  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*9ea3e141SMark Johnston  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*9ea3e141SMark Johnston  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*9ea3e141SMark Johnston  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*9ea3e141SMark Johnston  */
26*9ea3e141SMark Johnston 
27*9ea3e141SMark Johnston #include <sys/cdefs.h>
28*9ea3e141SMark Johnston __FBSDID("$FreeBSD$");
29*9ea3e141SMark Johnston 
30*9ea3e141SMark Johnston #include <sys/param.h>
31*9ea3e141SMark Johnston #include <sys/systm.h>
32*9ea3e141SMark Johnston #include <sys/lock.h>
33*9ea3e141SMark Johnston #include <sys/mutex.h>
34*9ea3e141SMark Johnston #include <sys/time.h>
35*9ea3e141SMark Johnston 
36*9ea3e141SMark Johnston #include <machine/cpu.h>
37*9ea3e141SMark Johnston 
38*9ea3e141SMark Johnston #include <linux/hrtimer.h>
39*9ea3e141SMark Johnston 
40*9ea3e141SMark Johnston /* hrtimer flags */
41*9ea3e141SMark Johnston #define	HRTIMER_ACTIVE		0x01
42*9ea3e141SMark Johnston 
43*9ea3e141SMark Johnston static void
44*9ea3e141SMark Johnston hrtimer_call_handler(void *arg)
45*9ea3e141SMark Johnston {
46*9ea3e141SMark Johnston 	struct hrtimer *hrtimer;
47*9ea3e141SMark Johnston 	enum hrtimer_restart ret;
48*9ea3e141SMark Johnston 
49*9ea3e141SMark Johnston 	hrtimer = arg;
50*9ea3e141SMark Johnston 	ret = hrtimer->function(hrtimer);
51*9ea3e141SMark Johnston 	MPASS(ret == HRTIMER_NORESTART);
52*9ea3e141SMark Johnston 	hrtimer->flags &= ~HRTIMER_ACTIVE;
53*9ea3e141SMark Johnston }
54*9ea3e141SMark Johnston 
55*9ea3e141SMark Johnston bool
56*9ea3e141SMark Johnston linux_hrtimer_active(struct hrtimer *hrtimer)
57*9ea3e141SMark Johnston {
58*9ea3e141SMark Johnston 	bool ret;
59*9ea3e141SMark Johnston 
60*9ea3e141SMark Johnston 	mtx_lock(&hrtimer->mtx);
61*9ea3e141SMark Johnston 	ret = (hrtimer->flags & HRTIMER_ACTIVE) != 0;
62*9ea3e141SMark Johnston 	mtx_unlock(&hrtimer->mtx);
63*9ea3e141SMark Johnston 	return (ret);
64*9ea3e141SMark Johnston }
65*9ea3e141SMark Johnston 
66*9ea3e141SMark Johnston int
67*9ea3e141SMark Johnston linux_hrtimer_cancel(struct hrtimer *hrtimer)
68*9ea3e141SMark Johnston {
69*9ea3e141SMark Johnston 
70*9ea3e141SMark Johnston 	if (!hrtimer_active(hrtimer))
71*9ea3e141SMark Johnston 		return (0);
72*9ea3e141SMark Johnston 	(void)callout_drain(&hrtimer->callout);
73*9ea3e141SMark Johnston 	return (1);
74*9ea3e141SMark Johnston }
75*9ea3e141SMark Johnston 
76*9ea3e141SMark Johnston void
77*9ea3e141SMark Johnston linux_hrtimer_init(struct hrtimer *hrtimer)
78*9ea3e141SMark Johnston {
79*9ea3e141SMark Johnston 
80*9ea3e141SMark Johnston 	hrtimer->function = NULL;
81*9ea3e141SMark Johnston 	hrtimer->flags = 0;
82*9ea3e141SMark Johnston 	mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE);
83*9ea3e141SMark Johnston 	callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
84*9ea3e141SMark Johnston }
85*9ea3e141SMark Johnston 
86*9ea3e141SMark Johnston void
87*9ea3e141SMark Johnston linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused,
88*9ea3e141SMark Johnston     ktime_t time __unused)
89*9ea3e141SMark Johnston {
90*9ea3e141SMark Johnston }
91*9ea3e141SMark Johnston 
92*9ea3e141SMark Johnston void
93*9ea3e141SMark Johnston linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
94*9ea3e141SMark Johnston {
95*9ea3e141SMark Johnston 
96*9ea3e141SMark Johnston 	linux_hrtimer_start_range_ns(hrtimer, time, 0);
97*9ea3e141SMark Johnston }
98*9ea3e141SMark Johnston 
99*9ea3e141SMark Johnston void
100*9ea3e141SMark Johnston linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec)
101*9ea3e141SMark Johnston {
102*9ea3e141SMark Johnston 
103*9ea3e141SMark Johnston 	mtx_lock(&hrtimer->mtx);
104*9ea3e141SMark Johnston 	callout_reset_sbt(&hrtimer->callout, time.tv64 * SBT_1NS,
105*9ea3e141SMark Johnston 	    nsec * SBT_1NS, hrtimer_call_handler, hrtimer, 0);
106*9ea3e141SMark Johnston 	hrtimer->flags |= HRTIMER_ACTIVE;
107*9ea3e141SMark Johnston 	mtx_unlock(&hrtimer->mtx);
108*9ea3e141SMark Johnston }
109