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 /* hrtimer flags */ 419ea3e141SMark Johnston #define HRTIMER_ACTIVE 0x01 429ea3e141SMark Johnston 439ea3e141SMark Johnston static void 449ea3e141SMark Johnston hrtimer_call_handler(void *arg) 459ea3e141SMark Johnston { 469ea3e141SMark Johnston struct hrtimer *hrtimer; 479ea3e141SMark Johnston enum hrtimer_restart ret; 489ea3e141SMark Johnston 499ea3e141SMark Johnston hrtimer = arg; 509ea3e141SMark Johnston ret = hrtimer->function(hrtimer); 519ea3e141SMark Johnston MPASS(ret == HRTIMER_NORESTART); 529ea3e141SMark Johnston hrtimer->flags &= ~HRTIMER_ACTIVE; 539ea3e141SMark Johnston } 549ea3e141SMark Johnston 559ea3e141SMark Johnston bool 569ea3e141SMark Johnston linux_hrtimer_active(struct hrtimer *hrtimer) 579ea3e141SMark Johnston { 589ea3e141SMark Johnston bool ret; 599ea3e141SMark Johnston 609ea3e141SMark Johnston mtx_lock(&hrtimer->mtx); 619ea3e141SMark Johnston ret = (hrtimer->flags & HRTIMER_ACTIVE) != 0; 629ea3e141SMark Johnston mtx_unlock(&hrtimer->mtx); 639ea3e141SMark Johnston return (ret); 649ea3e141SMark Johnston } 659ea3e141SMark Johnston 669ea3e141SMark Johnston int 679ea3e141SMark Johnston linux_hrtimer_cancel(struct hrtimer *hrtimer) 689ea3e141SMark Johnston { 699ea3e141SMark Johnston 709ea3e141SMark Johnston if (!hrtimer_active(hrtimer)) 719ea3e141SMark Johnston return (0); 729ea3e141SMark Johnston (void)callout_drain(&hrtimer->callout); 739ea3e141SMark Johnston return (1); 749ea3e141SMark Johnston } 759ea3e141SMark Johnston 769ea3e141SMark Johnston void 779ea3e141SMark Johnston linux_hrtimer_init(struct hrtimer *hrtimer) 789ea3e141SMark Johnston { 799ea3e141SMark Johnston 809ea3e141SMark Johnston hrtimer->function = NULL; 819ea3e141SMark Johnston hrtimer->flags = 0; 829ea3e141SMark Johnston mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE); 839ea3e141SMark Johnston callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0); 849ea3e141SMark Johnston } 859ea3e141SMark Johnston 869ea3e141SMark Johnston void 879ea3e141SMark Johnston linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused, 889ea3e141SMark Johnston ktime_t time __unused) 899ea3e141SMark Johnston { 909ea3e141SMark Johnston } 919ea3e141SMark Johnston 929ea3e141SMark Johnston void 939ea3e141SMark Johnston linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time) 949ea3e141SMark Johnston { 959ea3e141SMark Johnston 969ea3e141SMark Johnston linux_hrtimer_start_range_ns(hrtimer, time, 0); 979ea3e141SMark Johnston } 989ea3e141SMark Johnston 999ea3e141SMark Johnston void 1009ea3e141SMark Johnston linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec) 1019ea3e141SMark Johnston { 1029ea3e141SMark Johnston 1039ea3e141SMark Johnston mtx_lock(&hrtimer->mtx); 104*d35f6548SIan Lepore callout_reset_sbt(&hrtimer->callout, nstosbt(time.tv64), nstosbt(nsec), 105*d35f6548SIan Lepore hrtimer_call_handler, hrtimer, 0); 1069ea3e141SMark Johnston hrtimer->flags |= HRTIMER_ACTIVE; 1079ea3e141SMark Johnston mtx_unlock(&hrtimer->mtx); 1089ea3e141SMark Johnston } 109