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