xref: /dragonfly/sys/dev/drm/linux_hrtimer.c (revision 799ba435)
1 /*
2  * Copyright (c) 2017 François Tigeot
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 <linux/hrtimer.h>
28 #include <linux/bug.h>
29 
30 static inline void
31 __hrtimer_function(void *arg)
32 {
33 	struct hrtimer *timer = arg;
34 	enum hrtimer_restart restart = HRTIMER_RESTART;
35 
36 	if (timer->function) {
37 		restart = timer->function(timer);
38 	}
39 
40 	if (restart == HRTIMER_NORESTART)
41 		timer->function = NULL;
42 }
43 
44 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
45 			   enum hrtimer_mode mode)
46 {
47 	BUG_ON(clock_id != CLOCK_MONOTONIC);
48 
49 	memset(timer, 0, sizeof(struct hrtimer));
50 	timer->clock_id = clock_id;
51 	timer->ht_mode = mode;
52 
53 	lwkt_token_init(&timer->timer_token, "timer token");
54 	callout_init_mp(&(timer)->timer_callout);
55 }
56 
57 void
58 hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
59 		       u64 range_ns, const enum hrtimer_mode mode)
60 {
61 	int expire_ticks = tim.tv64 / (NSEC_PER_SEC / hz);
62 
63 	if (mode == HRTIMER_MODE_ABS)
64 		expire_ticks -= ticks;
65 
66 	if (expire_ticks <= 0)
67 		expire_ticks = 1;
68 
69 	lwkt_gettoken(&timer->timer_token);
70 
71 	timer->active = true;
72 	callout_reset(&timer->timer_callout,
73 		      expire_ticks, __hrtimer_function, timer);
74 
75 	lwkt_reltoken(&timer->timer_token);
76 }
77 
78 int
79 hrtimer_cancel(struct hrtimer *timer)
80 {
81 	return callout_drain(&timer->timer_callout) == 0;
82 }
83 
84 /* Returns non-zero if the timer is already on the queue */
85 bool
86 hrtimer_active(const struct hrtimer *timer)
87 {
88 	return callout_pending(&timer->timer_callout);
89 }
90