Path: blob/main/sys/compat/linuxkpi/common/src/linux_hrtimer.c
39586 views
/*-1* Copyright (c) 2017 Mark Johnston <[email protected]>2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice unmodified, this list of conditions, and the following8* disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#include <sys/param.h>26#include <sys/systm.h>27#include <sys/lock.h>28#include <sys/mutex.h>29#include <sys/time.h>3031#include <machine/cpu.h>3233#include <linux/hrtimer.h>3435static void36hrtimer_call_handler(void *arg)37{38struct hrtimer *hrtimer;39enum hrtimer_restart ret;4041hrtimer = arg;42ret = hrtimer->function(hrtimer);4344if (ret == HRTIMER_RESTART) {45callout_schedule_sbt(&hrtimer->callout,46nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0);47} else {48callout_deactivate(&hrtimer->callout);49}50}5152bool53linux_hrtimer_active(struct hrtimer *hrtimer)54{55bool ret;5657mtx_lock(&hrtimer->mtx);58ret = callout_active(&hrtimer->callout);59mtx_unlock(&hrtimer->mtx);6061return (ret);62}6364/*65* Try to cancel active hrtimer.66* Return 1 if timer was active and cancellation succeeded, 0 if timer was67* inactive, or -1 if the timer is being serviced and can't be cancelled.68*/69int70linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer)71{72int ret;7374mtx_lock(&hrtimer->mtx);75ret = callout_stop(&hrtimer->callout);76mtx_unlock(&hrtimer->mtx);77if (ret > 0) {78return (1);79} else if (ret < 0) {80return (0);81} else {82return (-1);83}84}8586/*87* Cancel active hrtimer.88* Return 1 if timer was active and cancellation succeeded, or 0 otherwise.89*/90int91linux_hrtimer_cancel(struct hrtimer *hrtimer)92{9394return (callout_drain(&hrtimer->callout) > 0);95}9697void98linux_hrtimer_init(struct hrtimer *hrtimer)99{100101memset(hrtimer, 0, sizeof(*hrtimer));102mtx_init(&hrtimer->mtx, "hrtimer", NULL,103MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);104callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);105}106107void108linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)109{110hrtimer->expires = ktime_to_ns(time);111}112113void114linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)115{116117linux_hrtimer_start_range_ns(hrtimer, time, 0);118}119120void121linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,122int64_t nsec)123{124125mtx_lock(&hrtimer->mtx);126hrtimer->precision = nsec;127callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),128nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);129mtx_unlock(&hrtimer->mtx);130}131132void133linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)134{135136mtx_lock(&hrtimer->mtx);137callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),138nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);139mtx_unlock(&hrtimer->mtx);140}141142143