xref: /freebsd/sys/compat/linuxkpi/common/src/linux_hrtimer.c (revision f5147e312f43a9050468de539aeafa072caa1a60)
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
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 	MPASS(ret == HRTIMER_NORESTART);
49 	callout_deactivate(&hrtimer->callout);
50 }
51 
52 bool
53 linux_hrtimer_active(struct hrtimer *hrtimer)
54 {
55 	bool ret;
56 
57 	mtx_lock(&hrtimer->mtx);
58 	ret = callout_active(&hrtimer->callout);
59 	mtx_unlock(&hrtimer->mtx);
60 	return (ret);
61 }
62 
63 /*
64  * Cancel active hrtimer.
65  * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
66  */
67 int
68 linux_hrtimer_cancel(struct hrtimer *hrtimer)
69 {
70 
71 	return (callout_drain(&hrtimer->callout) > 0);
72 }
73 
74 void
75 linux_hrtimer_init(struct hrtimer *hrtimer)
76 {
77 
78 	hrtimer->function = NULL;
79 	mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE);
80 	callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
81 }
82 
83 void
84 linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused,
85     ktime_t time __unused)
86 {
87 }
88 
89 void
90 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
91 {
92 
93 	linux_hrtimer_start_range_ns(hrtimer, time, 0);
94 }
95 
96 void
97 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec)
98 {
99 
100 	mtx_lock(&hrtimer->mtx);
101 	callout_reset_sbt(&hrtimer->callout, nstosbt(time), nstosbt(nsec),
102 	    hrtimer_call_handler, hrtimer, 0);
103 	mtx_unlock(&hrtimer->mtx);
104 }
105