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 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/lock.h> 30 #include <sys/mutex.h> 31 #include <sys/time.h> 32 33 #include <machine/cpu.h> 34 35 #include <linux/hrtimer.h> 36 37 static void 38 hrtimer_call_handler(void *arg) 39 { 40 struct hrtimer *hrtimer; 41 enum hrtimer_restart ret; 42 43 hrtimer = arg; 44 ret = hrtimer->function(hrtimer); 45 46 if (ret == HRTIMER_RESTART) { 47 callout_schedule_sbt(&hrtimer->callout, 48 nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0); 49 } else { 50 callout_deactivate(&hrtimer->callout); 51 } 52 } 53 54 bool 55 linux_hrtimer_active(struct hrtimer *hrtimer) 56 { 57 bool ret; 58 59 mtx_lock(&hrtimer->mtx); 60 ret = callout_active(&hrtimer->callout); 61 mtx_unlock(&hrtimer->mtx); 62 63 return (ret); 64 } 65 66 /* 67 * Try to cancel active hrtimer. 68 * Return 1 if timer was active and cancellation succeeded, 0 if timer was 69 * inactive, or -1 if the timer is being serviced and can't be cancelled. 70 */ 71 int 72 linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer) 73 { 74 int ret; 75 76 mtx_lock(&hrtimer->mtx); 77 ret = callout_stop(&hrtimer->callout); 78 mtx_unlock(&hrtimer->mtx); 79 if (ret > 0) { 80 return (1); 81 } else if (ret < 0) { 82 return (0); 83 } else { 84 return (-1); 85 } 86 } 87 88 /* 89 * Cancel active hrtimer. 90 * Return 1 if timer was active and cancellation succeeded, or 0 otherwise. 91 */ 92 int 93 linux_hrtimer_cancel(struct hrtimer *hrtimer) 94 { 95 96 return (callout_drain(&hrtimer->callout) > 0); 97 } 98 99 void 100 linux_hrtimer_init(struct hrtimer *hrtimer) 101 { 102 103 memset(hrtimer, 0, sizeof(*hrtimer)); 104 mtx_init(&hrtimer->mtx, "hrtimer", NULL, 105 MTX_DEF | MTX_RECURSE | MTX_NOWITNESS); 106 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0); 107 } 108 109 void 110 linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time) 111 { 112 hrtimer->expires = ktime_to_ns(time); 113 } 114 115 void 116 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time) 117 { 118 119 linux_hrtimer_start_range_ns(hrtimer, time, 0); 120 } 121 122 void 123 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, 124 int64_t nsec) 125 { 126 127 mtx_lock(&hrtimer->mtx); 128 hrtimer->precision = nsec; 129 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)), 130 nstosbt(nsec), hrtimer_call_handler, hrtimer, 0); 131 mtx_unlock(&hrtimer->mtx); 132 } 133 134 void 135 linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval) 136 { 137 138 mtx_lock(&hrtimer->mtx); 139 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)), 140 nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0); 141 mtx_unlock(&hrtimer->mtx); 142 } 143