xref: /openbmc/linux/include/linux/timer.h (revision f571faf6)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_TIMER_H
31da177e4SLinus Torvalds #define _LINUX_TIMER_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/list.h>
682f67cd9SIngo Molnar #include <linux/ktime.h>
71da177e4SLinus Torvalds #include <linux/stddef.h>
8c6f3a97fSThomas Gleixner #include <linux/debugobjects.h>
96f2b9b9aSJohannes Berg #include <linux/stringify.h>
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds struct timer_list {
123bbb9ec9SArjan van de Ven 	/*
133bbb9ec9SArjan van de Ven 	 * All fields that change during normal runtime grouped to the
143bbb9ec9SArjan van de Ven 	 * same cacheline
153bbb9ec9SArjan van de Ven 	 */
161dabbcecSThomas Gleixner 	struct hlist_node	entry;
171da177e4SLinus Torvalds 	unsigned long		expires;
18354b46b1SKees Cook 	void			(*function)(struct timer_list *);
190eeda71bSThomas Gleixner 	u32			flags;
203bbb9ec9SArjan van de Ven 
216f2b9b9aSJohannes Berg #ifdef CONFIG_LOCKDEP
226f2b9b9aSJohannes Berg 	struct lockdep_map	lockdep_map;
236f2b9b9aSJohannes Berg #endif
241da177e4SLinus Torvalds };
251da177e4SLinus Torvalds 
266f2b9b9aSJohannes Berg #ifdef CONFIG_LOCKDEP
276f2b9b9aSJohannes Berg /*
286f2b9b9aSJohannes Berg  * NB: because we have to copy the lockdep_map, setting the lockdep_map key
296f2b9b9aSJohannes Berg  * (second argument) here is required, otherwise it could be initialised to
306f2b9b9aSJohannes Berg  * the copy of the lockdep_map later! We use the pointer to and the string
316f2b9b9aSJohannes Berg  * "<file>:<line>" as the key resp. the name of the lockdep_map.
326f2b9b9aSJohannes Berg  */
336f2b9b9aSJohannes Berg #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)				\
346f2b9b9aSJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
356f2b9b9aSJohannes Berg #else
366f2b9b9aSJohannes Berg #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
376f2b9b9aSJohannes Berg #endif
386f2b9b9aSJohannes Berg 
3928ef2db8SPeter Xu /**
4028ef2db8SPeter Xu  * @TIMER_DEFERRABLE: A deferrable timer will work normally when the
4128ef2db8SPeter Xu  * system is busy, but will not cause a CPU to come out of idle just
4228ef2db8SPeter Xu  * to service it; instead, the timer will be serviced when the CPU
4328ef2db8SPeter Xu  * eventually wakes up with a subsequent non-deferrable timer.
44c5f66e99STejun Heo  *
4528ef2db8SPeter Xu  * @TIMER_IRQSAFE: An irqsafe timer is executed with IRQ disabled and
4628ef2db8SPeter Xu  * it's safe to wait for the completion of the running instance from
4728ef2db8SPeter Xu  * IRQ handlers, for example, by calling del_timer_sync().
48c5f66e99STejun Heo  *
49c5f66e99STejun Heo  * Note: The irq disabled callback execution is a special case for
50c5f66e99STejun Heo  * workqueue locking issues. It's not meant for executing random crap
51c5f66e99STejun Heo  * with interrupts disabled. Abuse is monitored!
5228ef2db8SPeter Xu  *
5328ef2db8SPeter Xu  * @TIMER_PINNED: A pinned timer will not be affected by any timer
5428ef2db8SPeter Xu  * placement heuristics (like, NOHZ) and will always expire on the CPU
5528ef2db8SPeter Xu  * on which the timer was enqueued.
5628ef2db8SPeter Xu  *
5728ef2db8SPeter Xu  * Note: Because enqueuing of timers can migrate the timer from one
5828ef2db8SPeter Xu  * CPU to another, pinned timers are not guaranteed to stay on the
5928ef2db8SPeter Xu  * initialy selected CPU.  They move to the CPU on which the enqueue
6028ef2db8SPeter Xu  * function is invoked via mod_timer() or add_timer().  If the timer
6128ef2db8SPeter Xu  * should be placed on a particular CPU, then add_timer_on() has to be
6228ef2db8SPeter Xu  * used.
63dd6414b5SPhil Carmody  */
64b0d6e2dcSThomas Gleixner #define TIMER_CPUMASK		0x0003FFFF
65b0d6e2dcSThomas Gleixner #define TIMER_MIGRATING		0x00040000
660eeda71bSThomas Gleixner #define TIMER_BASEMASK		(TIMER_CPUMASK | TIMER_MIGRATING)
67b0d6e2dcSThomas Gleixner #define TIMER_DEFERRABLE	0x00080000
68b0d6e2dcSThomas Gleixner #define TIMER_PINNED		0x00100000
69b0d6e2dcSThomas Gleixner #define TIMER_IRQSAFE		0x00200000
70b952caf2SQianli Zhao #define TIMER_INIT_FLAGS	(TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
71500462a9SThomas Gleixner #define TIMER_ARRAYSHIFT	22
72500462a9SThomas Gleixner #define TIMER_ARRAYMASK		0xFFC00000
73dd6414b5SPhil Carmody 
748a58a34bSThomas Gleixner #define TIMER_TRACE_FLAGMASK	(TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
758a58a34bSThomas Gleixner 
761fe66ba5SKees Cook #define __TIMER_INITIALIZER(_function, _flags) {		\
771dabbcecSThomas Gleixner 		.entry = { .next = TIMER_ENTRY_STATIC },	\
781da177e4SLinus Torvalds 		.function = (_function),			\
790eeda71bSThomas Gleixner 		.flags = (_flags),				\
806f2b9b9aSJohannes Berg 		__TIMER_LOCKDEP_MAP_INITIALIZER(		\
816f2b9b9aSJohannes Berg 			__FILE__ ":" __stringify(__LINE__))	\
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 
841d27e3e2SKees Cook #define DEFINE_TIMER(_name, _function)				\
858d06afabSIngo Molnar 	struct timer_list _name =				\
86841b86f3SKees Cook 		__TIMER_INITIALIZER(_function, 0)
878d06afabSIngo Molnar 
88919b250fSKees Cook /*
89919b250fSKees Cook  * LOCKDEP and DEBUG timer interfaces.
90919b250fSKees Cook  */
91188665b2SKees Cook void init_timer_key(struct timer_list *timer,
92188665b2SKees Cook 		    void (*func)(struct timer_list *), unsigned int flags,
93fc683995STejun Heo 		    const char *name, struct lock_class_key *key);
946f2b9b9aSJohannes Berg 
955a9af38dSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
965a9af38dSTejun Heo extern void init_timer_on_stack_key(struct timer_list *timer,
97188665b2SKees Cook 				    void (*func)(struct timer_list *),
98fc683995STejun Heo 				    unsigned int flags, const char *name,
995a9af38dSTejun Heo 				    struct lock_class_key *key);
1005a9af38dSTejun Heo #else
init_timer_on_stack_key(struct timer_list * timer,void (* func)(struct timer_list *),unsigned int flags,const char * name,struct lock_class_key * key)1015a9af38dSTejun Heo static inline void init_timer_on_stack_key(struct timer_list *timer,
102188665b2SKees Cook 					   void (*func)(struct timer_list *),
103188665b2SKees Cook 					   unsigned int flags,
104188665b2SKees Cook 					   const char *name,
1055a9af38dSTejun Heo 					   struct lock_class_key *key)
1065a9af38dSTejun Heo {
107188665b2SKees Cook 	init_timer_key(timer, func, flags, name, key);
1085a9af38dSTejun Heo }
1095a9af38dSTejun Heo #endif
1105a9af38dSTejun Heo 
1116f2b9b9aSJohannes Berg #ifdef CONFIG_LOCKDEP
112188665b2SKees Cook #define __init_timer(_timer, _fn, _flags)				\
1136f2b9b9aSJohannes Berg 	do {								\
1146f2b9b9aSJohannes Berg 		static struct lock_class_key __key;			\
115188665b2SKees Cook 		init_timer_key((_timer), (_fn), (_flags), #_timer, &__key);\
1166f2b9b9aSJohannes Berg 	} while (0)
1176f2b9b9aSJohannes Berg 
118188665b2SKees Cook #define __init_timer_on_stack(_timer, _fn, _flags)			\
1196f2b9b9aSJohannes Berg 	do {								\
1206f2b9b9aSJohannes Berg 		static struct lock_class_key __key;			\
121188665b2SKees Cook 		init_timer_on_stack_key((_timer), (_fn), (_flags),	\
122188665b2SKees Cook 					#_timer, &__key);		 \
1238cadd283SJesse Barnes 	} while (0)
1246f2b9b9aSJohannes Berg #else
125188665b2SKees Cook #define __init_timer(_timer, _fn, _flags)				\
126188665b2SKees Cook 	init_timer_key((_timer), (_fn), (_flags), NULL, NULL)
127188665b2SKees Cook #define __init_timer_on_stack(_timer, _fn, _flags)			\
128188665b2SKees Cook 	init_timer_on_stack_key((_timer), (_fn), (_flags), NULL, NULL)
1296f2b9b9aSJohannes Berg #endif
1301da177e4SLinus Torvalds 
131919b250fSKees Cook /**
132919b250fSKees Cook  * timer_setup - prepare a timer for first use
133919b250fSKees Cook  * @timer: the timer in question
134919b250fSKees Cook  * @callback: the function to call when timer expires
135919b250fSKees Cook  * @flags: any TIMER_* flags
136919b250fSKees Cook  *
137919b250fSKees Cook  * Regular timer initialization should use either DEFINE_TIMER() above,
138919b250fSKees Cook  * or timer_setup(). For timers on the stack, timer_setup_on_stack() must
139919b250fSKees Cook  * be used and must be balanced with a call to destroy_timer_on_stack().
14052f737c2SKees Cook  */
14152f737c2SKees Cook #define timer_setup(timer, callback, flags)			\
142919b250fSKees Cook 	__init_timer((timer), (callback), (flags))
143919b250fSKees Cook 
14452f737c2SKees Cook #define timer_setup_on_stack(timer, callback, flags)		\
145919b250fSKees Cook 	__init_timer_on_stack((timer), (callback), (flags))
146919b250fSKees Cook 
147919b250fSKees Cook #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
148919b250fSKees Cook extern void destroy_timer_on_stack(struct timer_list *timer);
149919b250fSKees Cook #else
destroy_timer_on_stack(struct timer_list * timer)150919b250fSKees Cook static inline void destroy_timer_on_stack(struct timer_list *timer) { }
15152f737c2SKees Cook #endif
15258e1177bSKees Cook 
153686fef92SKees Cook #define from_timer(var, callback_timer, timer_fieldname) \
154686fef92SKees Cook 	container_of(callback_timer, typeof(*var), timer_fieldname)
155686fef92SKees Cook 
15645f8bde0SRobert P. J. Day /**
1571da177e4SLinus Torvalds  * timer_pending - is a timer pending?
1581da177e4SLinus Torvalds  * @timer: the timer in question
1591da177e4SLinus Torvalds  *
1601da177e4SLinus Torvalds  * timer_pending will tell whether a given timer is currently pending,
1611da177e4SLinus Torvalds  * or not. Callers must ensure serialization wrt. other operations done
1621da177e4SLinus Torvalds  * to this timer, eg. interrupt contexts, or other CPUs on SMP.
1631da177e4SLinus Torvalds  *
1641da177e4SLinus Torvalds  * return value: 1 if the timer is pending, 0 if not.
1651da177e4SLinus Torvalds  */
timer_pending(const struct timer_list * timer)1661da177e4SLinus Torvalds static inline int timer_pending(const struct timer_list * timer)
1671da177e4SLinus Torvalds {
16890c01894SEric Dumazet 	return !hlist_unhashed_lockless(&timer->entry);
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds extern void add_timer_on(struct timer_list *timer, int cpu);
1721da177e4SLinus Torvalds extern int mod_timer(struct timer_list *timer, unsigned long expires);
17374019224SIngo Molnar extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
174b24591e2SDavid Howells extern int timer_reduce(struct timer_list *timer, unsigned long expires);
1751da177e4SLinus Torvalds 
176fd064b9bSThomas Gleixner /*
177eaad084bSThomas Gleixner  * The jiffies value which is added to now, when there is no timer
178eaad084bSThomas Gleixner  * in the timer wheel:
179eaad084bSThomas Gleixner  */
180eaad084bSThomas Gleixner #define NEXT_TIMER_MAX_DELTA	((1UL << 30) - 1)
181eaad084bSThomas Gleixner 
18274019224SIngo Molnar extern void add_timer(struct timer_list *timer);
1831da177e4SLinus Torvalds 
184fd450b73SOleg Nesterov extern int try_to_del_timer_sync(struct timer_list *timer);
1859b13df3fSThomas Gleixner extern int timer_delete_sync(struct timer_list *timer);
186bb663f0fSThomas Gleixner extern int timer_delete(struct timer_list *timer);
187*f571faf6SThomas Gleixner extern int timer_shutdown_sync(struct timer_list *timer);
188*f571faf6SThomas Gleixner extern int timer_shutdown(struct timer_list *timer);
1899b13df3fSThomas Gleixner 
1909b13df3fSThomas Gleixner /**
1919b13df3fSThomas Gleixner  * del_timer_sync - Delete a pending timer and wait for a running callback
1929b13df3fSThomas Gleixner  * @timer:	The timer to be deleted
1939b13df3fSThomas Gleixner  *
1949b13df3fSThomas Gleixner  * See timer_delete_sync() for detailed explanation.
1959b13df3fSThomas Gleixner  *
1969b13df3fSThomas Gleixner  * Do not use in new code. Use timer_delete_sync() instead.
1979b13df3fSThomas Gleixner  */
del_timer_sync(struct timer_list * timer)1989b13df3fSThomas Gleixner static inline int del_timer_sync(struct timer_list *timer)
1999b13df3fSThomas Gleixner {
2009b13df3fSThomas Gleixner 	return timer_delete_sync(timer);
2019b13df3fSThomas Gleixner }
2021da177e4SLinus Torvalds 
203bb663f0fSThomas Gleixner /**
204bb663f0fSThomas Gleixner  * del_timer - Delete a pending timer
205bb663f0fSThomas Gleixner  * @timer:	The timer to be deleted
206bb663f0fSThomas Gleixner  *
207bb663f0fSThomas Gleixner  * See timer_delete() for detailed explanation.
208bb663f0fSThomas Gleixner  *
209bb663f0fSThomas Gleixner  * Do not use in new code. Use timer_delete() instead.
210bb663f0fSThomas Gleixner  */
del_timer(struct timer_list * timer)211bb663f0fSThomas Gleixner static inline int del_timer(struct timer_list *timer)
212bb663f0fSThomas Gleixner {
213bb663f0fSThomas Gleixner 	return timer_delete(timer);
214bb663f0fSThomas Gleixner }
215bb663f0fSThomas Gleixner 
2161da177e4SLinus Torvalds extern void init_timers(void);
21705cfb614SRoman Zippel struct hrtimer;
218c9cb2e3dSThomas Gleixner extern enum hrtimer_restart it_real_fn(struct hrtimer *);
2191da177e4SLinus Torvalds 
2204c36a5deSArjan van de Ven unsigned long __round_jiffies(unsigned long j, int cpu);
2214c36a5deSArjan van de Ven unsigned long __round_jiffies_relative(unsigned long j, int cpu);
2224c36a5deSArjan van de Ven unsigned long round_jiffies(unsigned long j);
2234c36a5deSArjan van de Ven unsigned long round_jiffies_relative(unsigned long j);
2244c36a5deSArjan van de Ven 
2259c133c46SAlan Stern unsigned long __round_jiffies_up(unsigned long j, int cpu);
2269c133c46SAlan Stern unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
2279c133c46SAlan Stern unsigned long round_jiffies_up(unsigned long j);
2289c133c46SAlan Stern unsigned long round_jiffies_up_relative(unsigned long j);
2299c133c46SAlan Stern 
23024f73b99SRichard Cochran #ifdef CONFIG_HOTPLUG_CPU
23126456f87SThomas Gleixner int timers_prepare_cpu(unsigned int cpu);
23224f73b99SRichard Cochran int timers_dead_cpu(unsigned int cpu);
23324f73b99SRichard Cochran #else
23426456f87SThomas Gleixner #define timers_prepare_cpu	NULL
23524f73b99SRichard Cochran #define timers_dead_cpu		NULL
23624f73b99SRichard Cochran #endif
23724f73b99SRichard Cochran 
2381da177e4SLinus Torvalds #endif
239