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 /* hrtimer flags */ 41 #define HRTIMER_ACTIVE 0x01 42 43 static void 44 hrtimer_call_handler(void *arg) 45 { 46 struct hrtimer *hrtimer; 47 enum hrtimer_restart ret; 48 49 hrtimer = arg; 50 ret = hrtimer->function(hrtimer); 51 MPASS(ret == HRTIMER_NORESTART); 52 hrtimer->flags &= ~HRTIMER_ACTIVE; 53 } 54 55 bool 56 linux_hrtimer_active(struct hrtimer *hrtimer) 57 { 58 bool ret; 59 60 mtx_lock(&hrtimer->mtx); 61 ret = (hrtimer->flags & HRTIMER_ACTIVE) != 0; 62 mtx_unlock(&hrtimer->mtx); 63 return (ret); 64 } 65 66 int 67 linux_hrtimer_cancel(struct hrtimer *hrtimer) 68 { 69 70 if (!hrtimer_active(hrtimer)) 71 return (0); 72 (void)callout_drain(&hrtimer->callout); 73 return (1); 74 } 75 76 void 77 linux_hrtimer_init(struct hrtimer *hrtimer) 78 { 79 80 hrtimer->function = NULL; 81 hrtimer->flags = 0; 82 mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE); 83 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0); 84 } 85 86 void 87 linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused, 88 ktime_t time __unused) 89 { 90 } 91 92 void 93 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time) 94 { 95 96 linux_hrtimer_start_range_ns(hrtimer, time, 0); 97 } 98 99 void 100 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec) 101 { 102 103 mtx_lock(&hrtimer->mtx); 104 callout_reset_sbt(&hrtimer->callout, nstosbt(time.tv64), nstosbt(nsec), 105 hrtimer_call_handler, hrtimer, 0); 106 hrtimer->flags |= HRTIMER_ACTIVE; 107 mtx_unlock(&hrtimer->mtx); 108 } 109