xref: /dflybsd-src/sys/dev/drm/linux_hrtimer.c (revision 0856d15dd1f43a4353e2bd49c58f468f19865e11)
15915b712SFrançois Tigeot /*
2*0856d15dSFrançois Tigeot  * Copyright (c) 2017-2019 François Tigeot <ftigeot@wolfpond.org>
35915b712SFrançois Tigeot  * All rights reserved.
45915b712SFrançois Tigeot  *
55915b712SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
65915b712SFrançois Tigeot  * modification, are permitted provided that the following conditions
75915b712SFrançois Tigeot  * are met:
85915b712SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
95915b712SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
105915b712SFrançois Tigeot  *    disclaimer.
115915b712SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
125915b712SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
135915b712SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
145915b712SFrançois Tigeot  *
155915b712SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
165915b712SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
175915b712SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
185915b712SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
195915b712SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
205915b712SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
215915b712SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
225915b712SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
235915b712SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
245915b712SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255915b712SFrançois Tigeot  */
265915b712SFrançois Tigeot 
275915b712SFrançois Tigeot #include <linux/hrtimer.h>
285915b712SFrançois Tigeot #include <linux/bug.h>
295915b712SFrançois Tigeot 
305915b712SFrançois Tigeot static inline void
__hrtimer_function(void * arg)315915b712SFrançois Tigeot __hrtimer_function(void *arg)
325915b712SFrançois Tigeot {
335915b712SFrançois Tigeot 	struct hrtimer *timer = arg;
345915b712SFrançois Tigeot 	enum hrtimer_restart restart = HRTIMER_RESTART;
355915b712SFrançois Tigeot 
365915b712SFrançois Tigeot 	if (timer->function) {
375915b712SFrançois Tigeot 		restart = timer->function(timer);
385915b712SFrançois Tigeot 	}
395915b712SFrançois Tigeot 
40a1c669c2SImre Vadász 	if (restart == HRTIMER_RESTART) {
41a1c669c2SImre Vadász 		/*
42a1c669c2SImre Vadász 		 * XXX Not implemented yet, we would need to store the
43a1c669c2SImre Vadász 		 *     expiration period, to do the callout_reset here.
44a1c669c2SImre Vadász 		 */
45a1c669c2SImre Vadász 	} else {
46a1c669c2SImre Vadász 		timer->active = false;
47a1c669c2SImre Vadász 	}
485915b712SFrançois Tigeot }
495915b712SFrançois Tigeot 
hrtimer_init(struct hrtimer * timer,clockid_t clock_id,enum hrtimer_mode mode)505915b712SFrançois Tigeot void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
515915b712SFrançois Tigeot 			   enum hrtimer_mode mode)
525915b712SFrançois Tigeot {
535915b712SFrançois Tigeot 	BUG_ON(clock_id != CLOCK_MONOTONIC);
545915b712SFrançois Tigeot 
555915b712SFrançois Tigeot 	memset(timer, 0, sizeof(struct hrtimer));
565915b712SFrançois Tigeot 	timer->clock_id = clock_id;
575915b712SFrançois Tigeot 	timer->ht_mode = mode;
585915b712SFrançois Tigeot 
595915b712SFrançois Tigeot 	lwkt_token_init(&timer->timer_token, "timer token");
605915b712SFrançois Tigeot 	callout_init_mp(&(timer)->timer_callout);
615915b712SFrançois Tigeot }
625915b712SFrançois Tigeot 
635915b712SFrançois Tigeot void
hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 range_ns,const enum hrtimer_mode mode)645915b712SFrançois Tigeot hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
655915b712SFrançois Tigeot 		       u64 range_ns, const enum hrtimer_mode mode)
665915b712SFrançois Tigeot {
67*0856d15dSFrançois Tigeot 	int expire_ticks = tim / (NSEC_PER_SEC / hz);
685915b712SFrançois Tigeot 
695915b712SFrançois Tigeot 	if (mode == HRTIMER_MODE_ABS)
705915b712SFrançois Tigeot 		expire_ticks -= ticks;
715915b712SFrançois Tigeot 
725915b712SFrançois Tigeot 	if (expire_ticks <= 0)
735915b712SFrançois Tigeot 		expire_ticks = 1;
745915b712SFrançois Tigeot 
755915b712SFrançois Tigeot 	lwkt_gettoken(&timer->timer_token);
765915b712SFrançois Tigeot 
775915b712SFrançois Tigeot 	timer->active = true;
785915b712SFrançois Tigeot 	callout_reset(&timer->timer_callout,
795915b712SFrançois Tigeot 		      expire_ticks, __hrtimer_function, timer);
805915b712SFrançois Tigeot 
815915b712SFrançois Tigeot 	lwkt_reltoken(&timer->timer_token);
825915b712SFrançois Tigeot }
835915b712SFrançois Tigeot 
845915b712SFrançois Tigeot int
hrtimer_cancel(struct hrtimer * timer)855915b712SFrançois Tigeot hrtimer_cancel(struct hrtimer *timer)
865915b712SFrançois Tigeot {
87eb67213aSMatthew Dillon 	return callout_cancel(&timer->timer_callout) == 0;
885915b712SFrançois Tigeot }
895915b712SFrançois Tigeot 
905915b712SFrançois Tigeot /* Returns non-zero if the timer is already on the queue */
915915b712SFrançois Tigeot bool
hrtimer_active(const struct hrtimer * const_timer)92eb67213aSMatthew Dillon hrtimer_active(const struct hrtimer *const_timer)
935915b712SFrançois Tigeot {
94eb67213aSMatthew Dillon 	struct hrtimer *timer = __DECONST(struct hrtimer *, const_timer);
955915b712SFrançois Tigeot 	return callout_pending(&timer->timer_callout);
965915b712SFrançois Tigeot }
97