xref: /netbsd/sys/external/bsd/drm2/include/linux/timer.h (revision dd66672a)
1 /*	$NetBSD: timer.h,v 1.17 2021/12/19 12:33:48 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_TIMER_H_
33 #define _LINUX_TIMER_H_
34 
35 #include <sys/callout.h>
36 
37 #include <linux/jiffies.h>
38 #include <linux/ktime.h>
39 
40 struct timer_list {
41 	struct callout tl_callout;
42 };
43 
44 #define	from_timer(V, T, F)	container_of(T, __typeof__(*(V)), F)
45 
46 #define	TIMER_IRQSAFE		__BIT(0)
47 
48 static inline void
timer_setup(struct timer_list * timer,void (* fn)(struct timer_list *),int flags)49 timer_setup(struct timer_list *timer, void (*fn)(struct timer_list *),
50     int flags)
51 {
52 
53 	callout_init(&timer->tl_callout, 0);
54 
55 	/* XXX Super-sketchy casts!  */
56 	callout_setfunc(&timer->tl_callout, (void (*)(void *))(void *)fn,
57 	    (void *)timer);
58 }
59 
60 static inline int
mod_timer(struct timer_list * timer,unsigned long then)61 mod_timer(struct timer_list *timer, unsigned long then)
62 {
63 	const unsigned long now = jiffies;
64 	int pending;
65 
66 	pending = callout_pending(&timer->tl_callout);
67 	callout_schedule(&timer->tl_callout, (now < then? (then - now) : 0));
68 	return pending;
69 }
70 
71 static inline void
mod_timer_pinned(struct timer_list * timer,unsigned long then)72 mod_timer_pinned(struct timer_list *timer, unsigned long then)
73 {
74 
75 	/* XXX Stay on the same CPU it was originally on...  */
76 	mod_timer(timer, then);
77 }
78 
79 static inline int
del_timer(struct timer_list * timer)80 del_timer(struct timer_list *timer)
81 {
82 
83 	/*
84 	 * Linux: `del_timer of an inactive timer returns 0, del_timer
85 	 * of an active timer returns 1.'
86 	 *
87 	 * NetBSD: `callout_stop will return a non-zero value if the
88 	 * callout was EXPIRED.', meaning it is no longer pending.
89 	 */
90 	if (!callout_pending(&timer->tl_callout))
91 		return 0;	/* not active */
92 	if (callout_stop(&timer->tl_callout))
93 		return 0;	/* too late, already expired */
94 	return 1;		/* we stopped it while active */
95 }
96 
97 static inline int
del_timer_sync(struct timer_list * timer)98 del_timer_sync(struct timer_list *timer)
99 {
100 
101 	/*
102 	 * Linux: `The function returns whether it has deactivated a
103 	 * pending timer or not.'
104 	 *
105 	 * NetBSD: `[callout_halt] will return a non-zero value if the
106 	 * callout was EXPIRED.', meaning it is no longer pending.
107 	 */
108 	if (!callout_pending(&timer->tl_callout))
109 		return 0;	/* not active */
110 	if (callout_halt(&timer->tl_callout, NULL))
111 		return 0;	/* too late, already expired */
112 	return 1;		/* we stopped it while active */
113 }
114 
115 static inline bool
timer_pending(struct timer_list * timer)116 timer_pending(struct timer_list *timer)
117 {
118 
119 	return callout_pending(&timer->tl_callout);
120 }
121 
122 /*
123  * XXX This is bogus -- the Linux version does various machinations to
124  * give some jitter so that stuff doesn't wake up all at once.
125  */
126 
127 static inline unsigned long
round_jiffies_up(unsigned long j)128 round_jiffies_up(unsigned long j)
129 {
130 	return roundup(j, hz);
131 }
132 
133 static inline unsigned long
round_jiffies_up_relative(unsigned long j)134 round_jiffies_up_relative(unsigned long j)
135 {
136 	return roundup(j, hz);
137 }
138 
139 #endif  /* _LINUX_TIMER_H_ */
140