xref: /dflybsd-src/sys/dev/drm/include/linux/timer.h (revision 77343d599cf13645abbe9b660f3985cfa5b3a044)
1*77343d59SFrançois Tigeot /*-
2*77343d59SFrançois Tigeot  * Copyright (c) 2010 Isilon Systems, Inc.
3*77343d59SFrançois Tigeot  * Copyright (c) 2010 iX Systems, Inc.
4*77343d59SFrançois Tigeot  * Copyright (c) 2010 Panasas, Inc.
5*77343d59SFrançois Tigeot  * All rights reserved.
6*77343d59SFrançois Tigeot  *
7*77343d59SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
8*77343d59SFrançois Tigeot  * modification, are permitted provided that the following conditions
9*77343d59SFrançois Tigeot  * are met:
10*77343d59SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
11*77343d59SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
12*77343d59SFrançois Tigeot  *    disclaimer.
13*77343d59SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
14*77343d59SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
15*77343d59SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
16*77343d59SFrançois Tigeot  *
17*77343d59SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*77343d59SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*77343d59SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*77343d59SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*77343d59SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*77343d59SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*77343d59SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*77343d59SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*77343d59SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*77343d59SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*77343d59SFrançois Tigeot  */
28*77343d59SFrançois Tigeot #ifndef _LINUX_TIMER_H_
29*77343d59SFrançois Tigeot #define _LINUX_TIMER_H_
30*77343d59SFrançois Tigeot 
31*77343d59SFrançois Tigeot #include <linux/types.h>
32*77343d59SFrançois Tigeot 
33*77343d59SFrançois Tigeot #include <sys/param.h>
34*77343d59SFrançois Tigeot #include <sys/kernel.h>
35*77343d59SFrançois Tigeot #include <sys/callout.h>
36*77343d59SFrançois Tigeot 
37*77343d59SFrançois Tigeot struct timer_list {
38*77343d59SFrançois Tigeot 	struct callout	timer_callout;
39*77343d59SFrançois Tigeot 	void		(*function)(unsigned long);
40*77343d59SFrançois Tigeot 	unsigned long	data;
41*77343d59SFrançois Tigeot 	unsigned long	expires;
42*77343d59SFrançois Tigeot };
43*77343d59SFrançois Tigeot 
44*77343d59SFrançois Tigeot static inline void
45*77343d59SFrançois Tigeot _timer_fn(void *context)
46*77343d59SFrançois Tigeot {
47*77343d59SFrançois Tigeot 	struct timer_list *timer;
48*77343d59SFrançois Tigeot 
49*77343d59SFrançois Tigeot 	timer = context;
50*77343d59SFrançois Tigeot 	timer->function(timer->data);
51*77343d59SFrançois Tigeot }
52*77343d59SFrançois Tigeot 
53*77343d59SFrançois Tigeot #define	setup_timer(timer, func, dat)					\
54*77343d59SFrançois Tigeot do {									\
55*77343d59SFrançois Tigeot 	(timer)->function = (func);					\
56*77343d59SFrançois Tigeot 	(timer)->data = (dat);						\
57*77343d59SFrançois Tigeot 	callout_init(&(timer)->timer_callout, CALLOUT_MPSAFE);		\
58*77343d59SFrançois Tigeot } while (0)
59*77343d59SFrançois Tigeot 
60*77343d59SFrançois Tigeot #define	init_timer(timer)						\
61*77343d59SFrançois Tigeot do {									\
62*77343d59SFrançois Tigeot 	(timer)->function = NULL;					\
63*77343d59SFrançois Tigeot 	(timer)->data = 0;						\
64*77343d59SFrançois Tigeot 	callout_init(&(timer)->timer_callout, CALLOUT_MPSAFE);		\
65*77343d59SFrançois Tigeot } while (0)
66*77343d59SFrançois Tigeot 
67*77343d59SFrançois Tigeot #define	mod_timer(timer, exp)						\
68*77343d59SFrançois Tigeot do {									\
69*77343d59SFrançois Tigeot 	(timer)->expires = (exp);					\
70*77343d59SFrançois Tigeot 	callout_reset(&(timer)->timer_callout, (exp) - jiffies,		\
71*77343d59SFrançois Tigeot 	    _timer_fn, (timer));					\
72*77343d59SFrançois Tigeot } while (0)
73*77343d59SFrançois Tigeot 
74*77343d59SFrançois Tigeot #define	add_timer(timer)						\
75*77343d59SFrançois Tigeot 	callout_reset(&(timer)->timer_callout,				\
76*77343d59SFrançois Tigeot 	    (timer)->expires - jiffies, _timer_fn, (timer))
77*77343d59SFrançois Tigeot 
78*77343d59SFrançois Tigeot #define	del_timer(timer)	callout_stop(&(timer)->timer_callout)
79*77343d59SFrançois Tigeot #define	del_timer_sync(timer)	callout_drain(&(timer)->timer_callout)
80*77343d59SFrançois Tigeot 
81*77343d59SFrançois Tigeot #define	timer_pending(timer)	callout_pending(&(timer)->timer_callout)
82*77343d59SFrançois Tigeot 
83*77343d59SFrançois Tigeot static inline unsigned long
84*77343d59SFrançois Tigeot round_jiffies(unsigned long j)
85*77343d59SFrançois Tigeot {
86*77343d59SFrançois Tigeot 	return roundup(j, hz);
87*77343d59SFrançois Tigeot }
88*77343d59SFrançois Tigeot 
89*77343d59SFrançois Tigeot #endif /* _LINUX_TIMER_H_ */
90