19ea3e141SMark Johnston /*- 29ea3e141SMark Johnston * Copyright (c) 2017 Mark Johnston <[email protected]> 39ea3e141SMark Johnston * All rights reserved. 49ea3e141SMark Johnston * 59ea3e141SMark Johnston * Redistribution and use in source and binary forms, with or without 69ea3e141SMark Johnston * modification, are permitted provided that the following conditions 79ea3e141SMark Johnston * are met: 89ea3e141SMark Johnston * 1. Redistributions of source code must retain the above copyright 99ea3e141SMark Johnston * notice unmodified, this list of conditions, and the following 109ea3e141SMark Johnston * disclaimer. 119ea3e141SMark Johnston * 2. Redistributions in binary form must reproduce the above copyright 129ea3e141SMark Johnston * notice, this list of conditions and the following disclaimer in the 139ea3e141SMark Johnston * documentation and/or other materials provided with the distribution. 149ea3e141SMark Johnston * 159ea3e141SMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 169ea3e141SMark Johnston * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 179ea3e141SMark Johnston * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 189ea3e141SMark Johnston * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 199ea3e141SMark Johnston * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 209ea3e141SMark Johnston * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 219ea3e141SMark Johnston * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 229ea3e141SMark Johnston * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 239ea3e141SMark Johnston * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 249ea3e141SMark Johnston * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 259ea3e141SMark Johnston */ 269ea3e141SMark Johnston 279ea3e141SMark Johnston #include <sys/cdefs.h> 289ea3e141SMark Johnston __FBSDID("$FreeBSD$"); 299ea3e141SMark Johnston 309ea3e141SMark Johnston #include <sys/param.h> 319ea3e141SMark Johnston #include <sys/systm.h> 329ea3e141SMark Johnston #include <sys/lock.h> 339ea3e141SMark Johnston #include <sys/mutex.h> 349ea3e141SMark Johnston #include <sys/time.h> 359ea3e141SMark Johnston 369ea3e141SMark Johnston #include <machine/cpu.h> 379ea3e141SMark Johnston 389ea3e141SMark Johnston #include <linux/hrtimer.h> 399ea3e141SMark Johnston 409ea3e141SMark Johnston static void 419ea3e141SMark Johnston hrtimer_call_handler(void *arg) 429ea3e141SMark Johnston { 439ea3e141SMark Johnston struct hrtimer *hrtimer; 449ea3e141SMark Johnston enum hrtimer_restart ret; 459ea3e141SMark Johnston 469ea3e141SMark Johnston hrtimer = arg; 479ea3e141SMark Johnston ret = hrtimer->function(hrtimer); 489ea3e141SMark Johnston MPASS(ret == HRTIMER_NORESTART); 49*e1cf70fbSAlexander Motin callout_deactivate(&hrtimer->callout); 509ea3e141SMark Johnston } 519ea3e141SMark Johnston 529ea3e141SMark Johnston bool 539ea3e141SMark Johnston linux_hrtimer_active(struct hrtimer *hrtimer) 549ea3e141SMark Johnston { 559ea3e141SMark Johnston bool ret; 569ea3e141SMark Johnston 579ea3e141SMark Johnston mtx_lock(&hrtimer->mtx); 58*e1cf70fbSAlexander Motin ret = callout_active(&hrtimer->callout); 599ea3e141SMark Johnston mtx_unlock(&hrtimer->mtx); 609ea3e141SMark Johnston return (ret); 619ea3e141SMark Johnston } 629ea3e141SMark Johnston 63*e1cf70fbSAlexander Motin /* 64*e1cf70fbSAlexander Motin * Cancel active hrtimer. 65*e1cf70fbSAlexander Motin * Return 1 if timer was active and cancellation succeeded, or 0 otherwise. 66*e1cf70fbSAlexander Motin */ 679ea3e141SMark Johnston int 689ea3e141SMark Johnston linux_hrtimer_cancel(struct hrtimer *hrtimer) 699ea3e141SMark Johnston { 709ea3e141SMark Johnston 71*e1cf70fbSAlexander Motin return (callout_drain(&hrtimer->callout) > 0); 729ea3e141SMark Johnston } 739ea3e141SMark Johnston 749ea3e141SMark Johnston void 759ea3e141SMark Johnston linux_hrtimer_init(struct hrtimer *hrtimer) 769ea3e141SMark Johnston { 779ea3e141SMark Johnston 789ea3e141SMark Johnston hrtimer->function = NULL; 799ea3e141SMark Johnston mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE); 809ea3e141SMark Johnston callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0); 819ea3e141SMark Johnston } 829ea3e141SMark Johnston 839ea3e141SMark Johnston void 849ea3e141SMark Johnston linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused, 859ea3e141SMark Johnston ktime_t time __unused) 869ea3e141SMark Johnston { 879ea3e141SMark Johnston } 889ea3e141SMark Johnston 899ea3e141SMark Johnston void 909ea3e141SMark Johnston linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time) 919ea3e141SMark Johnston { 929ea3e141SMark Johnston 939ea3e141SMark Johnston linux_hrtimer_start_range_ns(hrtimer, time, 0); 949ea3e141SMark Johnston } 959ea3e141SMark Johnston 969ea3e141SMark Johnston void 979ea3e141SMark Johnston linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec) 989ea3e141SMark Johnston { 999ea3e141SMark Johnston 1009ea3e141SMark Johnston mtx_lock(&hrtimer->mtx); 101d35f6548SIan Lepore callout_reset_sbt(&hrtimer->callout, nstosbt(time.tv64), nstosbt(nsec), 102d35f6548SIan Lepore hrtimer_call_handler, hrtimer, 0); 1039ea3e141SMark Johnston mtx_unlock(&hrtimer->mtx); 1049ea3e141SMark Johnston } 105