xref: /linux-6.15/include/linux/interrupt.h (revision def5f127)
11da177e4SLinus Torvalds /* interrupt.h */
21da177e4SLinus Torvalds #ifndef _LINUX_INTERRUPT_H
31da177e4SLinus Torvalds #define _LINUX_INTERRUPT_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/kernel.h>
61da177e4SLinus Torvalds #include <linux/linkage.h>
71da177e4SLinus Torvalds #include <linux/bitops.h>
81da177e4SLinus Torvalds #include <linux/preempt.h>
91da177e4SLinus Torvalds #include <linux/cpumask.h>
10908dcecdSJan Beulich #include <linux/irqreturn.h>
11dd3a1db9SThomas Gleixner #include <linux/irqnr.h>
121da177e4SLinus Torvalds #include <linux/hardirq.h>
13de30a2b3SIngo Molnar #include <linux/irqflags.h>
149ba5f005SPeter Zijlstra #include <linux/hrtimer.h>
15cd7eab44SBen Hutchings #include <linux/kref.h>
16cd7eab44SBen Hutchings #include <linux/workqueue.h>
170ebb26e7SIngo Molnar 
1860063497SArun Sharma #include <linux/atomic.h>
191da177e4SLinus Torvalds #include <asm/ptrace.h>
207d65f4a6SFrederic Weisbecker #include <asm/irq.h>
211da177e4SLinus Torvalds 
226e213616SThomas Gleixner /*
236e213616SThomas Gleixner  * These correspond to the IORESOURCE_IRQ_* defines in
246e213616SThomas Gleixner  * linux/ioport.h to select the interrupt line behaviour.  When
256e213616SThomas Gleixner  * requesting an interrupt without specifying a IRQF_TRIGGER, the
266e213616SThomas Gleixner  * setting should be assumed to be "as already configured", which
276e213616SThomas Gleixner  * may be as per machine or firmware initialisation.
286e213616SThomas Gleixner  */
296e213616SThomas Gleixner #define IRQF_TRIGGER_NONE	0x00000000
306e213616SThomas Gleixner #define IRQF_TRIGGER_RISING	0x00000001
316e213616SThomas Gleixner #define IRQF_TRIGGER_FALLING	0x00000002
326e213616SThomas Gleixner #define IRQF_TRIGGER_HIGH	0x00000004
336e213616SThomas Gleixner #define IRQF_TRIGGER_LOW	0x00000008
346e213616SThomas Gleixner #define IRQF_TRIGGER_MASK	(IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
356e213616SThomas Gleixner 				 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
366e213616SThomas Gleixner #define IRQF_TRIGGER_PROBE	0x00000010
376e213616SThomas Gleixner 
386e213616SThomas Gleixner /*
396e213616SThomas Gleixner  * These flags used only by the kernel as part of the
406e213616SThomas Gleixner  * irq handling routines.
416e213616SThomas Gleixner  *
426932bf37SThomas Gleixner  * IRQF_DISABLED - keep irqs disabled when calling the action handler.
436932bf37SThomas Gleixner  *                 DEPRECATED. This flag is a NOOP and scheduled to be removed
446e213616SThomas Gleixner  * IRQF_SHARED - allow sharing the irq among several devices
456e213616SThomas Gleixner  * IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
466e213616SThomas Gleixner  * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
47950f4427SThomas Gleixner  * IRQF_PERCPU - Interrupt is per cpu
48950f4427SThomas Gleixner  * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
49d85a60d8SBernhard Walle  * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
50d85a60d8SBernhard Walle  *                registered first in an shared interrupt is considered for
51d85a60d8SBernhard Walle  *                performance reasons)
52b25c340cSThomas Gleixner  * IRQF_ONESHOT - Interrupt is not reenabled after the hardirq handler finished.
53b25c340cSThomas Gleixner  *                Used by threaded interrupts which need to keep the
54b25c340cSThomas Gleixner  *                irq line disabled until the threaded handler has been run.
55685fd0b4SIan Campbell  * IRQF_NO_SUSPEND - Do not disable this IRQ during suspend
56dc5f219eSThomas Gleixner  * IRQF_FORCE_RESUME - Force enable it on resume even if IRQF_NO_SUSPEND is set
570c4602ffSThomas Gleixner  * IRQF_NO_THREAD - Interrupt cannot be threaded
589bab0b7fSIan Campbell  * IRQF_EARLY_RESUME - Resume IRQ early during syscore instead of at device
599bab0b7fSIan Campbell  *                resume time.
606e213616SThomas Gleixner  */
616e213616SThomas Gleixner #define IRQF_DISABLED		0x00000020
626e213616SThomas Gleixner #define IRQF_SHARED		0x00000080
636e213616SThomas Gleixner #define IRQF_PROBE_SHARED	0x00000100
64685fd0b4SIan Campbell #define __IRQF_TIMER		0x00000200
65284c6680SThomas Gleixner #define IRQF_PERCPU		0x00000400
66950f4427SThomas Gleixner #define IRQF_NOBALANCING	0x00000800
67d85a60d8SBernhard Walle #define IRQF_IRQPOLL		0x00001000
68b25c340cSThomas Gleixner #define IRQF_ONESHOT		0x00002000
69685fd0b4SIan Campbell #define IRQF_NO_SUSPEND		0x00004000
70dc5f219eSThomas Gleixner #define IRQF_FORCE_RESUME	0x00008000
710c4602ffSThomas Gleixner #define IRQF_NO_THREAD		0x00010000
729bab0b7fSIan Campbell #define IRQF_EARLY_RESUME	0x00020000
73685fd0b4SIan Campbell 
740c4602ffSThomas Gleixner #define IRQF_TIMER		(__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
753aa551c9SThomas Gleixner 
76b4e6b097SRandy Dunlap /*
77ae731f8dSMarc Zyngier  * These values can be returned by request_any_context_irq() and
78ae731f8dSMarc Zyngier  * describe the context the interrupt will be run in.
79ae731f8dSMarc Zyngier  *
80ae731f8dSMarc Zyngier  * IRQC_IS_HARDIRQ - interrupt runs in hardirq context
81ae731f8dSMarc Zyngier  * IRQC_IS_NESTED - interrupt runs in a nested threaded context
82ae731f8dSMarc Zyngier  */
83ae731f8dSMarc Zyngier enum {
84ae731f8dSMarc Zyngier 	IRQC_IS_HARDIRQ	= 0,
85ae731f8dSMarc Zyngier 	IRQC_IS_NESTED,
86ae731f8dSMarc Zyngier };
87ae731f8dSMarc Zyngier 
887d12e780SDavid Howells typedef irqreturn_t (*irq_handler_t)(int, void *);
89da482792SDavid Howells 
90a9d0a1a3SThomas Gleixner /**
91a9d0a1a3SThomas Gleixner  * struct irqaction - per interrupt action descriptor
92a9d0a1a3SThomas Gleixner  * @handler:	interrupt handler function
93a9d0a1a3SThomas Gleixner  * @name:	name of the device
94a9d0a1a3SThomas Gleixner  * @dev_id:	cookie to identify the device
9531d9d9b6SMarc Zyngier  * @percpu_dev_id:	cookie to identify the device
96a9d0a1a3SThomas Gleixner  * @next:	pointer to the next irqaction for shared interrupts
97a9d0a1a3SThomas Gleixner  * @irq:	interrupt number
98c0ecaa06SThomas Gleixner  * @flags:	flags (see IRQF_* above)
9925985edcSLucas De Marchi  * @thread_fn:	interrupt handler function for threaded interrupts
1003aa551c9SThomas Gleixner  * @thread:	thread pointer for threaded interrupts
1013aa551c9SThomas Gleixner  * @thread_flags:	flags related to @thread
102b5faba21SThomas Gleixner  * @thread_mask:	bitmask for keeping track of @thread activity
103c0ecaa06SThomas Gleixner  * @dir:	pointer to the proc/irq/NN/name entry
104a9d0a1a3SThomas Gleixner  */
1051da177e4SLinus Torvalds struct irqaction {
106da482792SDavid Howells 	irq_handler_t		handler;
1071da177e4SLinus Torvalds 	void			*dev_id;
10831d9d9b6SMarc Zyngier 	void __percpu		*percpu_dev_id;
1091da177e4SLinus Torvalds 	struct irqaction	*next;
1103aa551c9SThomas Gleixner 	irq_handler_t		thread_fn;
1113aa551c9SThomas Gleixner 	struct task_struct	*thread;
112c0ecaa06SThomas Gleixner 	unsigned int		irq;
113c0ecaa06SThomas Gleixner 	unsigned int		flags;
1143aa551c9SThomas Gleixner 	unsigned long		thread_flags;
115b5faba21SThomas Gleixner 	unsigned long		thread_mask;
116f6cd2477SEric Dumazet 	const char		*name;
117f6cd2477SEric Dumazet 	struct proc_dir_entry	*dir;
118f6cd2477SEric Dumazet } ____cacheline_internodealigned_in_smp;
1191da177e4SLinus Torvalds 
1207d12e780SDavid Howells extern irqreturn_t no_action(int cpl, void *dev_id);
1213aa551c9SThomas Gleixner 
1223aa551c9SThomas Gleixner extern int __must_check
1233aa551c9SThomas Gleixner request_threaded_irq(unsigned int irq, irq_handler_t handler,
1243aa551c9SThomas Gleixner 		     irq_handler_t thread_fn,
1253aa551c9SThomas Gleixner 		     unsigned long flags, const char *name, void *dev);
1263aa551c9SThomas Gleixner 
1273aa551c9SThomas Gleixner static inline int __must_check
1283aa551c9SThomas Gleixner request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
1293aa551c9SThomas Gleixner 	    const char *name, void *dev)
1303aa551c9SThomas Gleixner {
1313aa551c9SThomas Gleixner 	return request_threaded_irq(irq, handler, NULL, flags, name, dev);
1323aa551c9SThomas Gleixner }
1333aa551c9SThomas Gleixner 
134ae731f8dSMarc Zyngier extern int __must_check
135ae731f8dSMarc Zyngier request_any_context_irq(unsigned int irq, irq_handler_t handler,
136ae731f8dSMarc Zyngier 			unsigned long flags, const char *name, void *dev_id);
137ae731f8dSMarc Zyngier 
13831d9d9b6SMarc Zyngier extern int __must_check
13931d9d9b6SMarc Zyngier request_percpu_irq(unsigned int irq, irq_handler_t handler,
14031d9d9b6SMarc Zyngier 		   const char *devname, void __percpu *percpu_dev_id);
1413aa551c9SThomas Gleixner 
1421da177e4SLinus Torvalds extern void free_irq(unsigned int, void *);
14331d9d9b6SMarc Zyngier extern void free_percpu_irq(unsigned int, void __percpu *);
1441da177e4SLinus Torvalds 
1450af3678fSAl Viro struct device;
1460af3678fSAl Viro 
147935bd5b9SArjan van de Ven extern int __must_check
148935bd5b9SArjan van de Ven devm_request_threaded_irq(struct device *dev, unsigned int irq,
149935bd5b9SArjan van de Ven 			  irq_handler_t handler, irq_handler_t thread_fn,
150935bd5b9SArjan van de Ven 			  unsigned long irqflags, const char *devname,
151935bd5b9SArjan van de Ven 			  void *dev_id);
152935bd5b9SArjan van de Ven 
153935bd5b9SArjan van de Ven static inline int __must_check
154935bd5b9SArjan van de Ven devm_request_irq(struct device *dev, unsigned int irq, irq_handler_t handler,
155935bd5b9SArjan van de Ven 		 unsigned long irqflags, const char *devname, void *dev_id)
156935bd5b9SArjan van de Ven {
157935bd5b9SArjan van de Ven 	return devm_request_threaded_irq(dev, irq, handler, NULL, irqflags,
158935bd5b9SArjan van de Ven 					 devname, dev_id);
159935bd5b9SArjan van de Ven }
160935bd5b9SArjan van de Ven 
1610668d306SStephen Boyd extern int __must_check
1620668d306SStephen Boyd devm_request_any_context_irq(struct device *dev, unsigned int irq,
1630668d306SStephen Boyd 		 irq_handler_t handler, unsigned long irqflags,
1640668d306SStephen Boyd 		 const char *devname, void *dev_id);
1650668d306SStephen Boyd 
1669ac7849eSTejun Heo extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
1679ac7849eSTejun Heo 
168d7e9629dSIngo Molnar /*
169d7e9629dSIngo Molnar  * On lockdep we dont want to enable hardirqs in hardirq
170d7e9629dSIngo Molnar  * context. Use local_irq_enable_in_hardirq() to annotate
171d7e9629dSIngo Molnar  * kernel code that has to do this nevertheless (pretty much
172d7e9629dSIngo Molnar  * the only valid case is for old/broken hardware that is
173d7e9629dSIngo Molnar  * insanely slow).
174d7e9629dSIngo Molnar  *
175d7e9629dSIngo Molnar  * NOTE: in theory this might break fragile code that relies
176d7e9629dSIngo Molnar  * on hardirq delivery - in practice we dont seem to have such
177d7e9629dSIngo Molnar  * places left. So the only effect should be slightly increased
178d7e9629dSIngo Molnar  * irqs-off latencies.
179d7e9629dSIngo Molnar  */
180d7e9629dSIngo Molnar #ifdef CONFIG_LOCKDEP
181d7e9629dSIngo Molnar # define local_irq_enable_in_hardirq()	do { } while (0)
182d7e9629dSIngo Molnar #else
183d7e9629dSIngo Molnar # define local_irq_enable_in_hardirq()	local_irq_enable()
184d7e9629dSIngo Molnar #endif
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds extern void disable_irq_nosync(unsigned int irq);
1871da177e4SLinus Torvalds extern void disable_irq(unsigned int irq);
18831d9d9b6SMarc Zyngier extern void disable_percpu_irq(unsigned int irq);
1891da177e4SLinus Torvalds extern void enable_irq(unsigned int irq);
1901e7c5fd2SMarc Zyngier extern void enable_percpu_irq(unsigned int irq, unsigned int type);
191a92444c6SThomas Gleixner extern void irq_wake_thread(unsigned int irq, void *dev_id);
192ba9a2331SThomas Gleixner 
1930a0c5168SRafael J. Wysocki /* The following three functions are for the core kernel use only. */
1940a0c5168SRafael J. Wysocki extern void suspend_device_irqs(void);
1950a0c5168SRafael J. Wysocki extern void resume_device_irqs(void);
1960a0c5168SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
1970a0c5168SRafael J. Wysocki extern int check_wakeup_irqs(void);
1980a0c5168SRafael J. Wysocki #else
1990a0c5168SRafael J. Wysocki static inline int check_wakeup_irqs(void) { return 0; }
2000a0c5168SRafael J. Wysocki #endif
2010a0c5168SRafael J. Wysocki 
2020244ad00SMartin Schwidefsky #if defined(CONFIG_SMP)
203d7b90689SRussell King 
204d036e67bSRusty Russell extern cpumask_var_t irq_default_affinity;
20518404756SMax Krasnyansky 
20601f8fa4fSThomas Gleixner /* Internal implementation. Use the helpers below */
20701f8fa4fSThomas Gleixner extern int __irq_set_affinity(unsigned int irq, const struct cpumask *cpumask,
20801f8fa4fSThomas Gleixner 			      bool force);
20901f8fa4fSThomas Gleixner 
21001f8fa4fSThomas Gleixner /**
21101f8fa4fSThomas Gleixner  * irq_set_affinity - Set the irq affinity of a given irq
21201f8fa4fSThomas Gleixner  * @irq:	Interrupt to set affinity
213*def5f127SRandy Dunlap  * @cpumask:	cpumask
21401f8fa4fSThomas Gleixner  *
21501f8fa4fSThomas Gleixner  * Fails if cpumask does not contain an online CPU
21601f8fa4fSThomas Gleixner  */
21701f8fa4fSThomas Gleixner static inline int
21801f8fa4fSThomas Gleixner irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
21901f8fa4fSThomas Gleixner {
22001f8fa4fSThomas Gleixner 	return __irq_set_affinity(irq, cpumask, false);
22101f8fa4fSThomas Gleixner }
22201f8fa4fSThomas Gleixner 
22301f8fa4fSThomas Gleixner /**
22401f8fa4fSThomas Gleixner  * irq_force_affinity - Force the irq affinity of a given irq
22501f8fa4fSThomas Gleixner  * @irq:	Interrupt to set affinity
226*def5f127SRandy Dunlap  * @cpumask:	cpumask
22701f8fa4fSThomas Gleixner  *
22801f8fa4fSThomas Gleixner  * Same as irq_set_affinity, but without checking the mask against
22901f8fa4fSThomas Gleixner  * online cpus.
23001f8fa4fSThomas Gleixner  *
23101f8fa4fSThomas Gleixner  * Solely for low level cpu hotplug code, where we need to make per
23201f8fa4fSThomas Gleixner  * cpu interrupts affine before the cpu becomes online.
23301f8fa4fSThomas Gleixner  */
23401f8fa4fSThomas Gleixner static inline int
23501f8fa4fSThomas Gleixner irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
23601f8fa4fSThomas Gleixner {
23701f8fa4fSThomas Gleixner 	return __irq_set_affinity(irq, cpumask, true);
23801f8fa4fSThomas Gleixner }
23901f8fa4fSThomas Gleixner 
240d7b90689SRussell King extern int irq_can_set_affinity(unsigned int irq);
24118404756SMax Krasnyansky extern int irq_select_affinity(unsigned int irq);
242d7b90689SRussell King 
243e7a297b0SPeter P Waskiewicz Jr extern int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m);
244cd7eab44SBen Hutchings 
245cd7eab44SBen Hutchings /**
246cd7eab44SBen Hutchings  * struct irq_affinity_notify - context for notification of IRQ affinity changes
247cd7eab44SBen Hutchings  * @irq:		Interrupt to which notification applies
248cd7eab44SBen Hutchings  * @kref:		Reference count, for internal use
249cd7eab44SBen Hutchings  * @work:		Work item, for internal use
250cd7eab44SBen Hutchings  * @notify:		Function to be called on change.  This will be
251cd7eab44SBen Hutchings  *			called in process context.
252cd7eab44SBen Hutchings  * @release:		Function to be called on release.  This will be
253cd7eab44SBen Hutchings  *			called in process context.  Once registered, the
254cd7eab44SBen Hutchings  *			structure must only be freed when this function is
255cd7eab44SBen Hutchings  *			called or later.
256cd7eab44SBen Hutchings  */
257cd7eab44SBen Hutchings struct irq_affinity_notify {
258cd7eab44SBen Hutchings 	unsigned int irq;
259cd7eab44SBen Hutchings 	struct kref kref;
260cd7eab44SBen Hutchings 	struct work_struct work;
261cd7eab44SBen Hutchings 	void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
262cd7eab44SBen Hutchings 	void (*release)(struct kref *ref);
263cd7eab44SBen Hutchings };
264cd7eab44SBen Hutchings 
265cd7eab44SBen Hutchings extern int
266cd7eab44SBen Hutchings irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
267cd7eab44SBen Hutchings 
268d7b90689SRussell King #else /* CONFIG_SMP */
269d7b90689SRussell King 
2700de26520SRusty Russell static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
271d7b90689SRussell King {
272d7b90689SRussell King 	return -EINVAL;
273d7b90689SRussell King }
274d7b90689SRussell King 
275d7b90689SRussell King static inline int irq_can_set_affinity(unsigned int irq)
276d7b90689SRussell King {
277d7b90689SRussell King 	return 0;
278d7b90689SRussell King }
279d7b90689SRussell King 
28018404756SMax Krasnyansky static inline int irq_select_affinity(unsigned int irq)  { return 0; }
28118404756SMax Krasnyansky 
282e7a297b0SPeter P Waskiewicz Jr static inline int irq_set_affinity_hint(unsigned int irq,
283e7a297b0SPeter P Waskiewicz Jr 					const struct cpumask *m)
284e7a297b0SPeter P Waskiewicz Jr {
285e7a297b0SPeter P Waskiewicz Jr 	return -EINVAL;
286e7a297b0SPeter P Waskiewicz Jr }
2870244ad00SMartin Schwidefsky #endif /* CONFIG_SMP */
288d7b90689SRussell King 
289c01d403bSIngo Molnar /*
290c01d403bSIngo Molnar  * Special lockdep variants of irq disabling/enabling.
291c01d403bSIngo Molnar  * These should be used for locking constructs that
292c01d403bSIngo Molnar  * know that a particular irq context which is disabled,
293c01d403bSIngo Molnar  * and which is the only irq-context user of a lock,
294c01d403bSIngo Molnar  * that it's safe to take the lock in the irq-disabled
295c01d403bSIngo Molnar  * section without disabling hardirqs.
296c01d403bSIngo Molnar  *
297c01d403bSIngo Molnar  * On !CONFIG_LOCKDEP they are equivalent to the normal
298c01d403bSIngo Molnar  * irq disable/enable methods.
299c01d403bSIngo Molnar  */
300c01d403bSIngo Molnar static inline void disable_irq_nosync_lockdep(unsigned int irq)
301c01d403bSIngo Molnar {
302c01d403bSIngo Molnar 	disable_irq_nosync(irq);
303c01d403bSIngo Molnar #ifdef CONFIG_LOCKDEP
304c01d403bSIngo Molnar 	local_irq_disable();
305c01d403bSIngo Molnar #endif
306c01d403bSIngo Molnar }
307c01d403bSIngo Molnar 
308e8106b94SArjan van de Ven static inline void disable_irq_nosync_lockdep_irqsave(unsigned int irq, unsigned long *flags)
309e8106b94SArjan van de Ven {
310e8106b94SArjan van de Ven 	disable_irq_nosync(irq);
311e8106b94SArjan van de Ven #ifdef CONFIG_LOCKDEP
312e8106b94SArjan van de Ven 	local_irq_save(*flags);
313e8106b94SArjan van de Ven #endif
314e8106b94SArjan van de Ven }
315e8106b94SArjan van de Ven 
316c01d403bSIngo Molnar static inline void disable_irq_lockdep(unsigned int irq)
317c01d403bSIngo Molnar {
318c01d403bSIngo Molnar 	disable_irq(irq);
319c01d403bSIngo Molnar #ifdef CONFIG_LOCKDEP
320c01d403bSIngo Molnar 	local_irq_disable();
321c01d403bSIngo Molnar #endif
322c01d403bSIngo Molnar }
323c01d403bSIngo Molnar 
324c01d403bSIngo Molnar static inline void enable_irq_lockdep(unsigned int irq)
325c01d403bSIngo Molnar {
326c01d403bSIngo Molnar #ifdef CONFIG_LOCKDEP
327c01d403bSIngo Molnar 	local_irq_enable();
328c01d403bSIngo Molnar #endif
329c01d403bSIngo Molnar 	enable_irq(irq);
330c01d403bSIngo Molnar }
331c01d403bSIngo Molnar 
332e8106b94SArjan van de Ven static inline void enable_irq_lockdep_irqrestore(unsigned int irq, unsigned long *flags)
333e8106b94SArjan van de Ven {
334e8106b94SArjan van de Ven #ifdef CONFIG_LOCKDEP
335e8106b94SArjan van de Ven 	local_irq_restore(*flags);
336e8106b94SArjan van de Ven #endif
337e8106b94SArjan van de Ven 	enable_irq(irq);
338e8106b94SArjan van de Ven }
339e8106b94SArjan van de Ven 
340ba9a2331SThomas Gleixner /* IRQ wakeup (PM) control: */
341a0cd9ca2SThomas Gleixner extern int irq_set_irq_wake(unsigned int irq, unsigned int on);
342a0cd9ca2SThomas Gleixner 
343ba9a2331SThomas Gleixner static inline int enable_irq_wake(unsigned int irq)
344ba9a2331SThomas Gleixner {
345a0cd9ca2SThomas Gleixner 	return irq_set_irq_wake(irq, 1);
346ba9a2331SThomas Gleixner }
347ba9a2331SThomas Gleixner 
348ba9a2331SThomas Gleixner static inline int disable_irq_wake(unsigned int irq)
349ba9a2331SThomas Gleixner {
350a0cd9ca2SThomas Gleixner 	return irq_set_irq_wake(irq, 0);
351ba9a2331SThomas Gleixner }
352ba9a2331SThomas Gleixner 
3538d32a307SThomas Gleixner 
3548d32a307SThomas Gleixner #ifdef CONFIG_IRQ_FORCED_THREADING
3558d32a307SThomas Gleixner extern bool force_irqthreads;
3568d32a307SThomas Gleixner #else
3578d32a307SThomas Gleixner #define force_irqthreads	(0)
3588d32a307SThomas Gleixner #endif
3598d32a307SThomas Gleixner 
3603f74478bSAndi Kleen #ifndef __ARCH_SET_SOFTIRQ_PENDING
3613f74478bSAndi Kleen #define set_softirq_pending(x) (local_softirq_pending() = (x))
3623f74478bSAndi Kleen #define or_softirq_pending(x)  (local_softirq_pending() |= (x))
3633f74478bSAndi Kleen #endif
3643f74478bSAndi Kleen 
3652d3fbbb3SBenjamin Herrenschmidt /* Some architectures might implement lazy enabling/disabling of
3662d3fbbb3SBenjamin Herrenschmidt  * interrupts. In some cases, such as stop_machine, we might want
3672d3fbbb3SBenjamin Herrenschmidt  * to ensure that after a local_irq_disable(), interrupts have
3682d3fbbb3SBenjamin Herrenschmidt  * really been disabled in hardware. Such architectures need to
3692d3fbbb3SBenjamin Herrenschmidt  * implement the following hook.
3702d3fbbb3SBenjamin Herrenschmidt  */
3712d3fbbb3SBenjamin Herrenschmidt #ifndef hard_irq_disable
3722d3fbbb3SBenjamin Herrenschmidt #define hard_irq_disable()	do { } while(0)
3732d3fbbb3SBenjamin Herrenschmidt #endif
3742d3fbbb3SBenjamin Herrenschmidt 
3751da177e4SLinus Torvalds /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
3761da177e4SLinus Torvalds    frequency threaded job scheduling. For almost all the purposes
3771da177e4SLinus Torvalds    tasklets are more than enough. F.e. all serial device BHs et
3781da177e4SLinus Torvalds    al. should be converted to tasklets, not to softirqs.
3791da177e4SLinus Torvalds  */
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds enum
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	HI_SOFTIRQ=0,
3841da177e4SLinus Torvalds 	TIMER_SOFTIRQ,
3851da177e4SLinus Torvalds 	NET_TX_SOFTIRQ,
3861da177e4SLinus Torvalds 	NET_RX_SOFTIRQ,
387ff856badSJens Axboe 	BLOCK_SOFTIRQ,
3885e605b64SJens Axboe 	BLOCK_IOPOLL_SOFTIRQ,
389c9819f45SChristoph Lameter 	TASKLET_SOFTIRQ,
390c9819f45SChristoph Lameter 	SCHED_SOFTIRQ,
391a6037b61SPeter Zijlstra 	HRTIMER_SOFTIRQ,
39209223371SShaohua Li 	RCU_SOFTIRQ,    /* Preferable RCU should always be the last softirq */
393978b0116SAlexey Dobriyan 
394978b0116SAlexey Dobriyan 	NR_SOFTIRQS
3951da177e4SLinus Torvalds };
3961da177e4SLinus Torvalds 
397803b0ebaSPaul E. McKenney #define SOFTIRQ_STOP_IDLE_MASK (~(1 << RCU_SOFTIRQ))
398803b0ebaSPaul E. McKenney 
3995d592b44SJason Baron /* map softirq index to softirq name. update 'softirq_to_name' in
4005d592b44SJason Baron  * kernel/softirq.c when adding a new softirq.
4015d592b44SJason Baron  */
402ce85b4f2SJoe Perches extern const char * const softirq_to_name[NR_SOFTIRQS];
4035d592b44SJason Baron 
4041da177e4SLinus Torvalds /* softirq mask and active fields moved to irq_cpustat_t in
4051da177e4SLinus Torvalds  * asm/hardirq.h to get better cache usage.  KAO
4061da177e4SLinus Torvalds  */
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds struct softirq_action
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	void	(*action)(struct softirq_action *);
4111da177e4SLinus Torvalds };
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds asmlinkage void do_softirq(void);
414eb0f1c44SAdrian Bunk asmlinkage void __do_softirq(void);
4157d65f4a6SFrederic Weisbecker 
4167d65f4a6SFrederic Weisbecker #ifdef __ARCH_HAS_DO_SOFTIRQ
4177d65f4a6SFrederic Weisbecker void do_softirq_own_stack(void);
4187d65f4a6SFrederic Weisbecker #else
4197d65f4a6SFrederic Weisbecker static inline void do_softirq_own_stack(void)
4207d65f4a6SFrederic Weisbecker {
4217d65f4a6SFrederic Weisbecker 	__do_softirq();
4227d65f4a6SFrederic Weisbecker }
4237d65f4a6SFrederic Weisbecker #endif
4247d65f4a6SFrederic Weisbecker 
425962cf36cSCarlos R. Mafra extern void open_softirq(int nr, void (*action)(struct softirq_action *));
4261da177e4SLinus Torvalds extern void softirq_init(void);
427f069686eSSteven Rostedt extern void __raise_softirq_irqoff(unsigned int nr);
4282bf2160dSLai Jiangshan 
429b3c97528SHarvey Harrison extern void raise_softirq_irqoff(unsigned int nr);
430b3c97528SHarvey Harrison extern void raise_softirq(unsigned int nr);
4311da177e4SLinus Torvalds 
4324dd53d89SVenkatesh Pallipadi DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
4334dd53d89SVenkatesh Pallipadi 
4344dd53d89SVenkatesh Pallipadi static inline struct task_struct *this_cpu_ksoftirqd(void)
4354dd53d89SVenkatesh Pallipadi {
4364dd53d89SVenkatesh Pallipadi 	return this_cpu_read(ksoftirqd);
4374dd53d89SVenkatesh Pallipadi }
4384dd53d89SVenkatesh Pallipadi 
4391da177e4SLinus Torvalds /* Tasklets --- multithreaded analogue of BHs.
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds    Main feature differing them of generic softirqs: tasklet
4421da177e4SLinus Torvalds    is running only on one CPU simultaneously.
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds    Main feature differing them of BHs: different tasklets
4451da177e4SLinus Torvalds    may be run simultaneously on different CPUs.
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds    Properties:
4481da177e4SLinus Torvalds    * If tasklet_schedule() is called, then tasklet is guaranteed
4491da177e4SLinus Torvalds      to be executed on some cpu at least once after this.
45025985edcSLucas De Marchi    * If the tasklet is already scheduled, but its execution is still not
4511da177e4SLinus Torvalds      started, it will be executed only once.
4521da177e4SLinus Torvalds    * If this tasklet is already running on another CPU (or schedule is called
4531da177e4SLinus Torvalds      from tasklet itself), it is rescheduled for later.
4541da177e4SLinus Torvalds    * Tasklet is strictly serialized wrt itself, but not
4551da177e4SLinus Torvalds      wrt another tasklets. If client needs some intertask synchronization,
4561da177e4SLinus Torvalds      he makes it with spinlocks.
4571da177e4SLinus Torvalds  */
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds struct tasklet_struct
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	struct tasklet_struct *next;
4621da177e4SLinus Torvalds 	unsigned long state;
4631da177e4SLinus Torvalds 	atomic_t count;
4641da177e4SLinus Torvalds 	void (*func)(unsigned long);
4651da177e4SLinus Torvalds 	unsigned long data;
4661da177e4SLinus Torvalds };
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds #define DECLARE_TASKLET(name, func, data) \
4691da177e4SLinus Torvalds struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds #define DECLARE_TASKLET_DISABLED(name, func, data) \
4721da177e4SLinus Torvalds struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds enum
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	TASKLET_STATE_SCHED,	/* Tasklet is scheduled for execution */
4781da177e4SLinus Torvalds 	TASKLET_STATE_RUN	/* Tasklet is running (SMP only) */
4791da177e4SLinus Torvalds };
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds #ifdef CONFIG_SMP
4821da177e4SLinus Torvalds static inline int tasklet_trylock(struct tasklet_struct *t)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds static inline void tasklet_unlock(struct tasklet_struct *t)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	smp_mb__before_clear_bit();
4901da177e4SLinus Torvalds 	clear_bit(TASKLET_STATE_RUN, &(t)->state);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static inline void tasklet_unlock_wait(struct tasklet_struct *t)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds #else
4981da177e4SLinus Torvalds #define tasklet_trylock(t) 1
4991da177e4SLinus Torvalds #define tasklet_unlock_wait(t) do { } while (0)
5001da177e4SLinus Torvalds #define tasklet_unlock(t) do { } while (0)
5011da177e4SLinus Torvalds #endif
5021da177e4SLinus Torvalds 
503b3c97528SHarvey Harrison extern void __tasklet_schedule(struct tasklet_struct *t);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds static inline void tasklet_schedule(struct tasklet_struct *t)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
5081da177e4SLinus Torvalds 		__tasklet_schedule(t);
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511b3c97528SHarvey Harrison extern void __tasklet_hi_schedule(struct tasklet_struct *t);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static inline void tasklet_hi_schedule(struct tasklet_struct *t)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
5161da177e4SLinus Torvalds 		__tasklet_hi_schedule(t);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5197c692cbaSVegard Nossum extern void __tasklet_hi_schedule_first(struct tasklet_struct *t);
5207c692cbaSVegard Nossum 
5217c692cbaSVegard Nossum /*
5227c692cbaSVegard Nossum  * This version avoids touching any other tasklets. Needed for kmemcheck
5237c692cbaSVegard Nossum  * in order not to take any page faults while enqueueing this tasklet;
5247c692cbaSVegard Nossum  * consider VERY carefully whether you really need this or
5257c692cbaSVegard Nossum  * tasklet_hi_schedule()...
5267c692cbaSVegard Nossum  */
5277c692cbaSVegard Nossum static inline void tasklet_hi_schedule_first(struct tasklet_struct *t)
5287c692cbaSVegard Nossum {
5297c692cbaSVegard Nossum 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
5307c692cbaSVegard Nossum 		__tasklet_hi_schedule_first(t);
5317c692cbaSVegard Nossum }
5327c692cbaSVegard Nossum 
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds static inline void tasklet_disable_nosync(struct tasklet_struct *t)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	atomic_inc(&t->count);
5371da177e4SLinus Torvalds 	smp_mb__after_atomic_inc();
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds static inline void tasklet_disable(struct tasklet_struct *t)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	tasklet_disable_nosync(t);
5431da177e4SLinus Torvalds 	tasklet_unlock_wait(t);
5441da177e4SLinus Torvalds 	smp_mb();
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static inline void tasklet_enable(struct tasklet_struct *t)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	smp_mb__before_atomic_dec();
5501da177e4SLinus Torvalds 	atomic_dec(&t->count);
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds static inline void tasklet_hi_enable(struct tasklet_struct *t)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	smp_mb__before_atomic_dec();
5561da177e4SLinus Torvalds 	atomic_dec(&t->count);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds extern void tasklet_kill(struct tasklet_struct *t);
5601da177e4SLinus Torvalds extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
5611da177e4SLinus Torvalds extern void tasklet_init(struct tasklet_struct *t,
5621da177e4SLinus Torvalds 			 void (*func)(unsigned long), unsigned long data);
5631da177e4SLinus Torvalds 
5649ba5f005SPeter Zijlstra struct tasklet_hrtimer {
5659ba5f005SPeter Zijlstra 	struct hrtimer		timer;
5669ba5f005SPeter Zijlstra 	struct tasklet_struct	tasklet;
5679ba5f005SPeter Zijlstra 	enum hrtimer_restart	(*function)(struct hrtimer *);
5689ba5f005SPeter Zijlstra };
5699ba5f005SPeter Zijlstra 
5709ba5f005SPeter Zijlstra extern void
5719ba5f005SPeter Zijlstra tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
5729ba5f005SPeter Zijlstra 		     enum hrtimer_restart (*function)(struct hrtimer *),
5739ba5f005SPeter Zijlstra 		     clockid_t which_clock, enum hrtimer_mode mode);
5749ba5f005SPeter Zijlstra 
5759ba5f005SPeter Zijlstra static inline
5769ba5f005SPeter Zijlstra int tasklet_hrtimer_start(struct tasklet_hrtimer *ttimer, ktime_t time,
5779ba5f005SPeter Zijlstra 			  const enum hrtimer_mode mode)
5789ba5f005SPeter Zijlstra {
5799ba5f005SPeter Zijlstra 	return hrtimer_start(&ttimer->timer, time, mode);
5809ba5f005SPeter Zijlstra }
5819ba5f005SPeter Zijlstra 
5829ba5f005SPeter Zijlstra static inline
5839ba5f005SPeter Zijlstra void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
5849ba5f005SPeter Zijlstra {
5859ba5f005SPeter Zijlstra 	hrtimer_cancel(&ttimer->timer);
5869ba5f005SPeter Zijlstra 	tasklet_kill(&ttimer->tasklet);
5879ba5f005SPeter Zijlstra }
5889ba5f005SPeter Zijlstra 
5891da177e4SLinus Torvalds /*
5901da177e4SLinus Torvalds  * Autoprobing for irqs:
5911da177e4SLinus Torvalds  *
5921da177e4SLinus Torvalds  * probe_irq_on() and probe_irq_off() provide robust primitives
5931da177e4SLinus Torvalds  * for accurate IRQ probing during kernel initialization.  They are
5941da177e4SLinus Torvalds  * reasonably simple to use, are not "fooled" by spurious interrupts,
5951da177e4SLinus Torvalds  * and, unlike other attempts at IRQ probing, they do not get hung on
5961da177e4SLinus Torvalds  * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
5971da177e4SLinus Torvalds  *
5981da177e4SLinus Torvalds  * For reasonably foolproof probing, use them as follows:
5991da177e4SLinus Torvalds  *
6001da177e4SLinus Torvalds  * 1. clear and/or mask the device's internal interrupt.
6011da177e4SLinus Torvalds  * 2. sti();
6021da177e4SLinus Torvalds  * 3. irqs = probe_irq_on();      // "take over" all unassigned idle IRQs
6031da177e4SLinus Torvalds  * 4. enable the device and cause it to trigger an interrupt.
6041da177e4SLinus Torvalds  * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
6051da177e4SLinus Torvalds  * 6. irq = probe_irq_off(irqs);  // get IRQ number, 0=none, negative=multiple
6061da177e4SLinus Torvalds  * 7. service the device to clear its pending interrupt.
6071da177e4SLinus Torvalds  * 8. loop again if paranoia is required.
6081da177e4SLinus Torvalds  *
6091da177e4SLinus Torvalds  * probe_irq_on() returns a mask of allocated irq's.
6101da177e4SLinus Torvalds  *
6111da177e4SLinus Torvalds  * probe_irq_off() takes the mask as a parameter,
6121da177e4SLinus Torvalds  * and returns the irq number which occurred,
6131da177e4SLinus Torvalds  * or zero if none occurred, or a negative irq number
6141da177e4SLinus Torvalds  * if more than one irq occurred.
6151da177e4SLinus Torvalds  */
6161da177e4SLinus Torvalds 
6170244ad00SMartin Schwidefsky #if !defined(CONFIG_GENERIC_IRQ_PROBE)
6181da177e4SLinus Torvalds static inline unsigned long probe_irq_on(void)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	return 0;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds static inline int probe_irq_off(unsigned long val)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	return 0;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds static inline unsigned int probe_irq_mask(unsigned long val)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	return 0;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds #else
6311da177e4SLinus Torvalds extern unsigned long probe_irq_on(void);	/* returns 0 on failure */
6321da177e4SLinus Torvalds extern int probe_irq_off(unsigned long);	/* returns 0 or negative on failure */
6331da177e4SLinus Torvalds extern unsigned int probe_irq_mask(unsigned long);	/* returns mask of ISA interrupts */
6341da177e4SLinus Torvalds #endif
6351da177e4SLinus Torvalds 
6366168a702SAndrew Morton #ifdef CONFIG_PROC_FS
6376168a702SAndrew Morton /* Initialize /proc/irq/ */
6386168a702SAndrew Morton extern void init_irq_proc(void);
6396168a702SAndrew Morton #else
6406168a702SAndrew Morton static inline void init_irq_proc(void)
6416168a702SAndrew Morton {
6426168a702SAndrew Morton }
6436168a702SAndrew Morton #endif
6446168a702SAndrew Morton 
645d43c36dcSAlexey Dobriyan struct seq_file;
646f74596d0SAdrian Bunk int show_interrupts(struct seq_file *p, void *v);
647c78b9b65SThomas Gleixner int arch_show_interrupts(struct seq_file *p, int prec);
648f74596d0SAdrian Bunk 
64943a25632SYinghai Lu extern int early_irq_init(void);
6504a046d17SYinghai Lu extern int arch_probe_nr_irqs(void);
65143a25632SYinghai Lu extern int arch_early_irq_init(void);
65243a25632SYinghai Lu 
6531da177e4SLinus Torvalds #endif
654