1 /*- 2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice unmodified, this list of conditions, and the following 9 * disclaimer. 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. 13 * 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. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/lock.h> 32 #include <sys/mutex.h> 33 #include <sys/time.h> 34 35 #include <machine/cpu.h> 36 37 #include <linux/hrtimer.h> 38 39 static void 40 hrtimer_call_handler(void *arg) 41 { 42 struct hrtimer *hrtimer; 43 enum hrtimer_restart ret; 44 45 hrtimer = arg; 46 ret = hrtimer->function(hrtimer); 47 MPASS(ret == HRTIMER_NORESTART); 48 callout_deactivate(&hrtimer->callout); 49 } 50 51 bool 52 linux_hrtimer_active(struct hrtimer *hrtimer) 53 { 54 bool ret; 55 56 mtx_lock(&hrtimer->mtx); 57 ret = callout_active(&hrtimer->callout); 58 mtx_unlock(&hrtimer->mtx); 59 return (ret); 60 } 61 62 /* 63 * Cancel active hrtimer. 64 * Return 1 if timer was active and cancellation succeeded, or 0 otherwise. 65 */ 66 int 67 linux_hrtimer_cancel(struct hrtimer *hrtimer) 68 { 69 70 return (callout_drain(&hrtimer->callout) > 0); 71 } 72 73 void 74 linux_hrtimer_init(struct hrtimer *hrtimer) 75 { 76 77 hrtimer->function = NULL; 78 mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE); 79 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0); 80 } 81 82 void 83 linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused, 84 ktime_t time __unused) 85 { 86 } 87 88 void 89 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time) 90 { 91 92 linux_hrtimer_start_range_ns(hrtimer, time, 0); 93 } 94 95 void 96 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec) 97 { 98 99 mtx_lock(&hrtimer->mtx); 100 callout_reset_sbt(&hrtimer->callout, nstosbt(time), nstosbt(nsec), 101 hrtimer_call_handler, hrtimer, 0); 102 mtx_unlock(&hrtimer->mtx); 103 } 104