xref: /freebsd-src/sys/compat/linuxkpi/common/src/linux_hrtimer.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
19ea3e141SMark Johnston /*-
29ea3e141SMark Johnston  * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
39ea3e141SMark Johnston  *
49ea3e141SMark Johnston  * Redistribution and use in source and binary forms, with or without
59ea3e141SMark Johnston  * modification, are permitted provided that the following conditions
69ea3e141SMark Johnston  * are met:
79ea3e141SMark Johnston  * 1. Redistributions of source code must retain the above copyright
89ea3e141SMark Johnston  *    notice unmodified, this list of conditions, and the following
99ea3e141SMark Johnston  *    disclaimer.
109ea3e141SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
119ea3e141SMark Johnston  *    notice, this list of conditions and the following disclaimer in the
129ea3e141SMark Johnston  *    documentation and/or other materials provided with the distribution.
139ea3e141SMark Johnston  *
149ea3e141SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159ea3e141SMark Johnston  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169ea3e141SMark Johnston  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
179ea3e141SMark Johnston  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189ea3e141SMark Johnston  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199ea3e141SMark Johnston  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
209ea3e141SMark Johnston  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
219ea3e141SMark Johnston  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
229ea3e141SMark Johnston  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
239ea3e141SMark Johnston  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
249ea3e141SMark Johnston  */
259ea3e141SMark Johnston 
269ea3e141SMark Johnston #include <sys/param.h>
279ea3e141SMark Johnston #include <sys/systm.h>
289ea3e141SMark Johnston #include <sys/lock.h>
299ea3e141SMark Johnston #include <sys/mutex.h>
309ea3e141SMark Johnston #include <sys/time.h>
319ea3e141SMark Johnston 
329ea3e141SMark Johnston #include <machine/cpu.h>
339ea3e141SMark Johnston 
349ea3e141SMark Johnston #include <linux/hrtimer.h>
359ea3e141SMark Johnston 
369ea3e141SMark Johnston static void
hrtimer_call_handler(void * arg)379ea3e141SMark Johnston hrtimer_call_handler(void *arg)
389ea3e141SMark Johnston {
399ea3e141SMark Johnston 	struct hrtimer *hrtimer;
409ea3e141SMark Johnston 	enum hrtimer_restart ret;
419ea3e141SMark Johnston 
429ea3e141SMark Johnston 	hrtimer = arg;
439ea3e141SMark Johnston 	ret = hrtimer->function(hrtimer);
440a854962SHans Petter Selasky 
450a854962SHans Petter Selasky 	if (ret == HRTIMER_RESTART) {
460a854962SHans Petter Selasky 		callout_schedule_sbt(&hrtimer->callout,
470a854962SHans Petter Selasky 		    nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0);
480a854962SHans Petter Selasky 	} else {
49e1cf70fbSAlexander Motin 		callout_deactivate(&hrtimer->callout);
509ea3e141SMark Johnston 	}
510a854962SHans Petter Selasky }
529ea3e141SMark Johnston 
539ea3e141SMark Johnston bool
linux_hrtimer_active(struct hrtimer * hrtimer)549ea3e141SMark Johnston linux_hrtimer_active(struct hrtimer *hrtimer)
559ea3e141SMark Johnston {
569ea3e141SMark Johnston 	bool ret;
579ea3e141SMark Johnston 
589ea3e141SMark Johnston 	mtx_lock(&hrtimer->mtx);
59e1cf70fbSAlexander Motin 	ret = callout_active(&hrtimer->callout);
609ea3e141SMark Johnston 	mtx_unlock(&hrtimer->mtx);
610a854962SHans Petter Selasky 
629ea3e141SMark Johnston 	return (ret);
639ea3e141SMark Johnston }
649ea3e141SMark Johnston 
65e1cf70fbSAlexander Motin /*
66*bb651c77SJean-Sébastien Pédron  * Try to cancel active hrtimer.
67*bb651c77SJean-Sébastien Pédron  * Return 1 if timer was active and cancellation succeeded, 0 if timer was
68*bb651c77SJean-Sébastien Pédron  * inactive, or -1 if the timer is being serviced and can't be cancelled.
69*bb651c77SJean-Sébastien Pédron  */
70*bb651c77SJean-Sébastien Pédron int
linux_hrtimer_try_to_cancel(struct hrtimer * hrtimer)71*bb651c77SJean-Sébastien Pédron linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer)
72*bb651c77SJean-Sébastien Pédron {
73*bb651c77SJean-Sébastien Pédron 	int ret;
74*bb651c77SJean-Sébastien Pédron 
75*bb651c77SJean-Sébastien Pédron 	mtx_lock(&hrtimer->mtx);
76*bb651c77SJean-Sébastien Pédron 	ret = callout_stop(&hrtimer->callout);
77*bb651c77SJean-Sébastien Pédron 	mtx_unlock(&hrtimer->mtx);
78*bb651c77SJean-Sébastien Pédron 	if (ret > 0) {
79*bb651c77SJean-Sébastien Pédron 		return (1);
80*bb651c77SJean-Sébastien Pédron 	} else if (ret < 0) {
81*bb651c77SJean-Sébastien Pédron 		return (0);
82*bb651c77SJean-Sébastien Pédron 	} else {
83*bb651c77SJean-Sébastien Pédron 		return (-1);
84*bb651c77SJean-Sébastien Pédron 	}
85*bb651c77SJean-Sébastien Pédron }
86*bb651c77SJean-Sébastien Pédron 
87*bb651c77SJean-Sébastien Pédron /*
88e1cf70fbSAlexander Motin  * Cancel active hrtimer.
89e1cf70fbSAlexander Motin  * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
90e1cf70fbSAlexander Motin  */
919ea3e141SMark Johnston int
linux_hrtimer_cancel(struct hrtimer * hrtimer)929ea3e141SMark Johnston linux_hrtimer_cancel(struct hrtimer *hrtimer)
939ea3e141SMark Johnston {
949ea3e141SMark Johnston 
95e1cf70fbSAlexander Motin 	return (callout_drain(&hrtimer->callout) > 0);
969ea3e141SMark Johnston }
979ea3e141SMark Johnston 
989ea3e141SMark Johnston void
linux_hrtimer_init(struct hrtimer * hrtimer)999ea3e141SMark Johnston linux_hrtimer_init(struct hrtimer *hrtimer)
1009ea3e141SMark Johnston {
1019ea3e141SMark Johnston 
1020a854962SHans Petter Selasky 	memset(hrtimer, 0, sizeof(*hrtimer));
1030a854962SHans Petter Selasky 	mtx_init(&hrtimer->mtx, "hrtimer", NULL,
1040a854962SHans Petter Selasky 	    MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
1059ea3e141SMark Johnston 	callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
1069ea3e141SMark Johnston }
1079ea3e141SMark Johnston 
1089ea3e141SMark Johnston void
linux_hrtimer_set_expires(struct hrtimer * hrtimer,ktime_t time)1090a854962SHans Petter Selasky linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)
1109ea3e141SMark Johnston {
1110a854962SHans Petter Selasky 	hrtimer->expires = ktime_to_ns(time);
1129ea3e141SMark Johnston }
1139ea3e141SMark Johnston 
1149ea3e141SMark Johnston void
linux_hrtimer_start(struct hrtimer * hrtimer,ktime_t time)1159ea3e141SMark Johnston linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
1169ea3e141SMark Johnston {
1179ea3e141SMark Johnston 
1189ea3e141SMark Johnston 	linux_hrtimer_start_range_ns(hrtimer, time, 0);
1199ea3e141SMark Johnston }
1209ea3e141SMark Johnston 
1219ea3e141SMark Johnston void
linux_hrtimer_start_range_ns(struct hrtimer * hrtimer,ktime_t time,int64_t nsec)1220a854962SHans Petter Selasky linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,
1230a854962SHans Petter Selasky     int64_t nsec)
1249ea3e141SMark Johnston {
1259ea3e141SMark Johnston 
1269ea3e141SMark Johnston 	mtx_lock(&hrtimer->mtx);
1270a854962SHans Petter Selasky 	hrtimer->precision = nsec;
1280a854962SHans Petter Selasky 	callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),
1290a854962SHans Petter Selasky 	    nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);
1309ea3e141SMark Johnston 	mtx_unlock(&hrtimer->mtx);
1319ea3e141SMark Johnston }
1320a854962SHans Petter Selasky 
1330a854962SHans Petter Selasky void
linux_hrtimer_forward_now(struct hrtimer * hrtimer,ktime_t interval)1340a854962SHans Petter Selasky linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)
1350a854962SHans Petter Selasky {
1360a854962SHans Petter Selasky 
1370a854962SHans Petter Selasky 	mtx_lock(&hrtimer->mtx);
1380a854962SHans Petter Selasky 	callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),
1390a854962SHans Petter Selasky 	    nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);
1400a854962SHans Petter Selasky 	mtx_unlock(&hrtimer->mtx);
1410a854962SHans Petter Selasky }
142