• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* interrupt.h */
3 #ifndef _LINUX_INTERRUPT_H
4 #define _LINUX_INTERRUPT_H
5 
6 #include <linux/kernel.h>
7 #include <linux/bitops.h>
8 #include <linux/cpumask.h>
9 #include <linux/irqreturn.h>
10 #include <linux/irqnr.h>
11 #include <linux/hardirq.h>
12 #include <linux/irqflags.h>
13 #include <linux/hrtimer.h>
14 #include <linux/kref.h>
15 #include <linux/workqueue.h>
16 
17 #include <linux/atomic.h>
18 #include <asm/ptrace.h>
19 #include <asm/irq.h>
20 #include <asm/sections.h>
21 
22 /*
23  * These correspond to the IORESOURCE_IRQ_* defines in
24  * linux/ioport.h to select the interrupt line behaviour.  When
25  * requesting an interrupt without specifying a IRQF_TRIGGER, the
26  * setting should be assumed to be "as already configured", which
27  * may be as per machine or firmware initialisation.
28  */
29 #define IRQF_TRIGGER_NONE	0x00000000
30 #define IRQF_TRIGGER_RISING	0x00000001
31 #define IRQF_TRIGGER_FALLING	0x00000002
32 #define IRQF_TRIGGER_HIGH	0x00000004
33 #define IRQF_TRIGGER_LOW	0x00000008
34 #define IRQF_TRIGGER_MASK	(IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
35 				 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
36 #define IRQF_TRIGGER_PROBE	0x00000010
37 
38 /*
39  * These flags used only by the kernel as part of the
40  * irq handling routines.
41  *
42  * IRQF_SHARED - allow sharing the irq among several devices
43  * IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
44  * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
45  * IRQF_PERCPU - Interrupt is per cpu
46  * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
47  * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
48  *                registered first in a shared interrupt is considered for
49  *                performance reasons)
50  * IRQF_ONESHOT - Interrupt is not reenabled after the hardirq handler finished.
51  *                Used by threaded interrupts which need to keep the
52  *                irq line disabled until the threaded handler has been run.
53  * IRQF_NO_SUSPEND - Do not disable this IRQ during suspend.  Does not guarantee
54  *                   that this interrupt will wake the system from a suspended
55  *                   state.  See Documentation/power/suspend-and-interrupts.rst
56  * IRQF_FORCE_RESUME - Force enable it on resume even if IRQF_NO_SUSPEND is set
57  * IRQF_NO_THREAD - Interrupt cannot be threaded
58  * IRQF_EARLY_RESUME - Resume IRQ early during syscore instead of at device
59  *                resume time.
60  * IRQF_COND_SUSPEND - If the IRQ is shared with a NO_SUSPEND user, execute this
61  *                interrupt handler after suspending interrupts. For system
62  *                wakeup devices users need to implement wakeup detection in
63  *                their interrupt handlers.
64  * IRQF_NO_AUTOEN - Don't enable IRQ or NMI automatically when users request it.
65  *                Users will enable it explicitly by enable_irq() or enable_nmi()
66  *                later.
67  */
68 #define IRQF_SHARED		0x00000080
69 #define IRQF_PROBE_SHARED	0x00000100
70 #define __IRQF_TIMER		0x00000200
71 #define IRQF_PERCPU		0x00000400
72 #define IRQF_NOBALANCING	0x00000800
73 #define IRQF_IRQPOLL		0x00001000
74 #define IRQF_ONESHOT		0x00002000
75 #define IRQF_NO_SUSPEND		0x00004000
76 #define IRQF_FORCE_RESUME	0x00008000
77 #define IRQF_NO_THREAD		0x00010000
78 #define IRQF_EARLY_RESUME	0x00020000
79 #define IRQF_COND_SUSPEND	0x00040000
80 #define IRQF_NO_AUTOEN		0x00080000
81 
82 #define IRQF_TIMER		(__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
83 
84 /*
85  * These values can be returned by request_any_context_irq() and
86  * describe the context the interrupt will be run in.
87  *
88  * IRQC_IS_HARDIRQ - interrupt runs in hardirq context
89  * IRQC_IS_NESTED - interrupt runs in a nested threaded context
90  */
91 enum {
92 	IRQC_IS_HARDIRQ	= 0,
93 	IRQC_IS_NESTED,
94 };
95 
96 typedef irqreturn_t (*irq_handler_t)(int, void *);
97 
98 /**
99  * struct irqaction - per interrupt action descriptor
100  * @handler:	interrupt handler function
101  * @name:	name of the device
102  * @dev_id:	cookie to identify the device
103  * @percpu_dev_id:	cookie to identify the device
104  * @next:	pointer to the next irqaction for shared interrupts
105  * @irq:	interrupt number
106  * @flags:	flags (see IRQF_* above)
107  * @thread_fn:	interrupt handler function for threaded interrupts
108  * @thread:	thread pointer for threaded interrupts
109  * @secondary:	pointer to secondary irqaction (force threading)
110  * @thread_flags:	flags related to @thread
111  * @thread_mask:	bitmask for keeping track of @thread activity
112  * @dir:	pointer to the proc/irq/NN/name entry
113  */
114 struct irqaction {
115 	irq_handler_t		handler;
116 	void			*dev_id;
117 	void __percpu		*percpu_dev_id;
118 	struct irqaction	*next;
119 	irq_handler_t		thread_fn;
120 	struct task_struct	*thread;
121 	struct irqaction	*secondary;
122 	unsigned int		irq;
123 	unsigned int		flags;
124 	unsigned long		thread_flags;
125 	unsigned long		thread_mask;
126 	const char		*name;
127 	struct proc_dir_entry	*dir;
128 } ____cacheline_internodealigned_in_smp;
129 
130 extern irqreturn_t no_action(int cpl, void *dev_id);
131 
132 /*
133  * If a (PCI) device interrupt is not connected we set dev->irq to
134  * IRQ_NOTCONNECTED. This causes request_irq() to fail with -ENOTCONN, so we
135  * can distingiush that case from other error returns.
136  *
137  * 0x80000000 is guaranteed to be outside the available range of interrupts
138  * and easy to distinguish from other possible incorrect values.
139  */
140 #define IRQ_NOTCONNECTED	(1U << 31)
141 
142 extern int __must_check
143 request_threaded_irq(unsigned int irq, irq_handler_t handler,
144 		     irq_handler_t thread_fn,
145 		     unsigned long flags, const char *name, void *dev);
146 
147 static inline int __must_check
request_irq(unsigned int irq,irq_handler_t handler,unsigned long flags,const char * name,void * dev)148 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
149 	    const char *name, void *dev)
150 {
151 	return request_threaded_irq(irq, handler, NULL, flags, name, dev);
152 }
153 
154 extern int __must_check
155 request_any_context_irq(unsigned int irq, irq_handler_t handler,
156 			unsigned long flags, const char *name, void *dev_id);
157 
158 extern int __must_check
159 __request_percpu_irq(unsigned int irq, irq_handler_t handler,
160 		     unsigned long flags, const char *devname,
161 		     void __percpu *percpu_dev_id);
162 
163 extern int __must_check
164 request_nmi(unsigned int irq, irq_handler_t handler, unsigned long flags,
165 	    const char *name, void *dev);
166 
167 static inline int __must_check
request_percpu_irq(unsigned int irq,irq_handler_t handler,const char * devname,void __percpu * percpu_dev_id)168 request_percpu_irq(unsigned int irq, irq_handler_t handler,
169 		   const char *devname, void __percpu *percpu_dev_id)
170 {
171 	return __request_percpu_irq(irq, handler, 0,
172 				    devname, percpu_dev_id);
173 }
174 
175 extern int __must_check
176 request_percpu_nmi(unsigned int irq, irq_handler_t handler,
177 		   const char *devname, void __percpu *dev);
178 
179 extern const void *free_irq(unsigned int, void *);
180 extern void free_percpu_irq(unsigned int, void __percpu *);
181 
182 extern const void *free_nmi(unsigned int irq, void *dev_id);
183 extern void free_percpu_nmi(unsigned int irq, void __percpu *percpu_dev_id);
184 
185 struct device;
186 
187 extern int __must_check
188 devm_request_threaded_irq(struct device *dev, unsigned int irq,
189 			  irq_handler_t handler, irq_handler_t thread_fn,
190 			  unsigned long irqflags, const char *devname,
191 			  void *dev_id);
192 
193 static inline int __must_check
devm_request_irq(struct device * dev,unsigned int irq,irq_handler_t handler,unsigned long irqflags,const char * devname,void * dev_id)194 devm_request_irq(struct device *dev, unsigned int irq, irq_handler_t handler,
195 		 unsigned long irqflags, const char *devname, void *dev_id)
196 {
197 	return devm_request_threaded_irq(dev, irq, handler, NULL, irqflags,
198 					 devname, dev_id);
199 }
200 
201 extern int __must_check
202 devm_request_any_context_irq(struct device *dev, unsigned int irq,
203 		 irq_handler_t handler, unsigned long irqflags,
204 		 const char *devname, void *dev_id);
205 
206 extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
207 
208 /*
209  * On lockdep we dont want to enable hardirqs in hardirq
210  * context. Use local_irq_enable_in_hardirq() to annotate
211  * kernel code that has to do this nevertheless (pretty much
212  * the only valid case is for old/broken hardware that is
213  * insanely slow).
214  *
215  * NOTE: in theory this might break fragile code that relies
216  * on hardirq delivery - in practice we dont seem to have such
217  * places left. So the only effect should be slightly increased
218  * irqs-off latencies.
219  */
220 #ifdef CONFIG_LOCKDEP
221 # define local_irq_enable_in_hardirq()	do { } while (0)
222 #else
223 # define local_irq_enable_in_hardirq()	local_irq_enable()
224 #endif
225 
226 extern void disable_irq_nosync(unsigned int irq);
227 extern bool disable_hardirq(unsigned int irq);
228 extern void disable_irq(unsigned int irq);
229 extern void disable_percpu_irq(unsigned int irq);
230 extern void enable_irq(unsigned int irq);
231 extern void enable_percpu_irq(unsigned int irq, unsigned int type);
232 extern bool irq_percpu_is_enabled(unsigned int irq);
233 extern void irq_wake_thread(unsigned int irq, void *dev_id);
234 
235 extern void disable_nmi_nosync(unsigned int irq);
236 extern void disable_percpu_nmi(unsigned int irq);
237 extern void enable_nmi(unsigned int irq);
238 extern void enable_percpu_nmi(unsigned int irq, unsigned int type);
239 extern int prepare_percpu_nmi(unsigned int irq);
240 extern void teardown_percpu_nmi(unsigned int irq);
241 
242 /* The following three functions are for the core kernel use only. */
243 extern void suspend_device_irqs(void);
244 extern void resume_device_irqs(void);
245 extern void rearm_wake_irq(unsigned int irq);
246 
247 /**
248  * struct irq_affinity_notify - context for notification of IRQ affinity changes
249  * @irq:		Interrupt to which notification applies
250  * @kref:		Reference count, for internal use
251  * @work:		Work item, for internal use
252  * @notify:		Function to be called on change.  This will be
253  *			called in process context.
254  * @release:		Function to be called on release.  This will be
255  *			called in process context.  Once registered, the
256  *			structure must only be freed when this function is
257  *			called or later.
258  */
259 struct irq_affinity_notify {
260 	unsigned int irq;
261 	struct kref kref;
262 	struct work_struct work;
263 	void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
264 	void (*release)(struct kref *ref);
265 };
266 
267 #define	IRQ_AFFINITY_MAX_SETS  4
268 
269 /**
270  * struct irq_affinity - Description for automatic irq affinity assignements
271  * @pre_vectors:	Don't apply affinity to @pre_vectors at beginning of
272  *			the MSI(-X) vector space
273  * @post_vectors:	Don't apply affinity to @post_vectors at end of
274  *			the MSI(-X) vector space
275  * @nr_sets:		The number of interrupt sets for which affinity
276  *			spreading is required
277  * @set_size:		Array holding the size of each interrupt set
278  * @calc_sets:		Callback for calculating the number and size
279  *			of interrupt sets
280  * @priv:		Private data for usage by @calc_sets, usually a
281  *			pointer to driver/device specific data.
282  */
283 struct irq_affinity {
284 	unsigned int	pre_vectors;
285 	unsigned int	post_vectors;
286 	unsigned int	nr_sets;
287 	unsigned int	set_size[IRQ_AFFINITY_MAX_SETS];
288 	void		(*calc_sets)(struct irq_affinity *, unsigned int nvecs);
289 	void		*priv;
290 };
291 
292 /**
293  * struct irq_affinity_desc - Interrupt affinity descriptor
294  * @mask:	cpumask to hold the affinity assignment
295  * @is_managed: 1 if the interrupt is managed internally
296  */
297 struct irq_affinity_desc {
298 	struct cpumask	mask;
299 	unsigned int	is_managed : 1;
300 };
301 
302 #if defined(CONFIG_SMP)
303 
304 extern cpumask_var_t irq_default_affinity;
305 
306 /* Internal implementation. Use the helpers below */
307 extern int __irq_set_affinity(unsigned int irq, const struct cpumask *cpumask,
308 			      bool force);
309 
310 /**
311  * irq_set_affinity - Set the irq affinity of a given irq
312  * @irq:	Interrupt to set affinity
313  * @cpumask:	cpumask
314  *
315  * Fails if cpumask does not contain an online CPU
316  */
317 static inline int
irq_set_affinity(unsigned int irq,const struct cpumask * cpumask)318 irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
319 {
320 	return __irq_set_affinity(irq, cpumask, false);
321 }
322 
323 /**
324  * irq_force_affinity - Force the irq affinity of a given irq
325  * @irq:	Interrupt to set affinity
326  * @cpumask:	cpumask
327  *
328  * Same as irq_set_affinity, but without checking the mask against
329  * online cpus.
330  *
331  * Solely for low level cpu hotplug code, where we need to make per
332  * cpu interrupts affine before the cpu becomes online.
333  */
334 static inline int
irq_force_affinity(unsigned int irq,const struct cpumask * cpumask)335 irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
336 {
337 	return __irq_set_affinity(irq, cpumask, true);
338 }
339 
340 extern int irq_can_set_affinity(unsigned int irq);
341 extern int irq_select_affinity(unsigned int irq);
342 
343 extern int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m);
344 
345 extern int
346 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
347 
348 struct irq_affinity_desc *
349 irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd);
350 
351 unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
352 				       const struct irq_affinity *affd);
353 
354 #else /* CONFIG_SMP */
355 
irq_set_affinity(unsigned int irq,const struct cpumask * m)356 static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
357 {
358 	return -EINVAL;
359 }
360 
irq_force_affinity(unsigned int irq,const struct cpumask * cpumask)361 static inline int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
362 {
363 	return 0;
364 }
365 
irq_can_set_affinity(unsigned int irq)366 static inline int irq_can_set_affinity(unsigned int irq)
367 {
368 	return 0;
369 }
370 
irq_select_affinity(unsigned int irq)371 static inline int irq_select_affinity(unsigned int irq)  { return 0; }
372 
irq_set_affinity_hint(unsigned int irq,const struct cpumask * m)373 static inline int irq_set_affinity_hint(unsigned int irq,
374 					const struct cpumask *m)
375 {
376 	return -EINVAL;
377 }
378 
379 static inline int
irq_set_affinity_notifier(unsigned int irq,struct irq_affinity_notify * notify)380 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
381 {
382 	return 0;
383 }
384 
385 static inline struct irq_affinity_desc *
irq_create_affinity_masks(unsigned int nvec,struct irq_affinity * affd)386 irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd)
387 {
388 	return NULL;
389 }
390 
391 static inline unsigned int
irq_calc_affinity_vectors(unsigned int minvec,unsigned int maxvec,const struct irq_affinity * affd)392 irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
393 			  const struct irq_affinity *affd)
394 {
395 	return maxvec;
396 }
397 
398 #endif /* CONFIG_SMP */
399 
400 /*
401  * Special lockdep variants of irq disabling/enabling.
402  * These should be used for locking constructs that
403  * know that a particular irq context which is disabled,
404  * and which is the only irq-context user of a lock,
405  * that it's safe to take the lock in the irq-disabled
406  * section without disabling hardirqs.
407  *
408  * On !CONFIG_LOCKDEP they are equivalent to the normal
409  * irq disable/enable methods.
410  */
disable_irq_nosync_lockdep(unsigned int irq)411 static inline void disable_irq_nosync_lockdep(unsigned int irq)
412 {
413 	disable_irq_nosync(irq);
414 #ifdef CONFIG_LOCKDEP
415 	local_irq_disable();
416 #endif
417 }
418 
disable_irq_nosync_lockdep_irqsave(unsigned int irq,unsigned long * flags)419 static inline void disable_irq_nosync_lockdep_irqsave(unsigned int irq, unsigned long *flags)
420 {
421 	disable_irq_nosync(irq);
422 #ifdef CONFIG_LOCKDEP
423 	local_irq_save(*flags);
424 #endif
425 }
426 
disable_irq_lockdep(unsigned int irq)427 static inline void disable_irq_lockdep(unsigned int irq)
428 {
429 	disable_irq(irq);
430 #ifdef CONFIG_LOCKDEP
431 	local_irq_disable();
432 #endif
433 }
434 
enable_irq_lockdep(unsigned int irq)435 static inline void enable_irq_lockdep(unsigned int irq)
436 {
437 #ifdef CONFIG_LOCKDEP
438 	local_irq_enable();
439 #endif
440 	enable_irq(irq);
441 }
442 
enable_irq_lockdep_irqrestore(unsigned int irq,unsigned long * flags)443 static inline void enable_irq_lockdep_irqrestore(unsigned int irq, unsigned long *flags)
444 {
445 #ifdef CONFIG_LOCKDEP
446 	local_irq_restore(*flags);
447 #endif
448 	enable_irq(irq);
449 }
450 
451 /* IRQ wakeup (PM) control: */
452 extern int irq_set_irq_wake(unsigned int irq, unsigned int on);
453 
enable_irq_wake(unsigned int irq)454 static inline int enable_irq_wake(unsigned int irq)
455 {
456 	return irq_set_irq_wake(irq, 1);
457 }
458 
disable_irq_wake(unsigned int irq)459 static inline int disable_irq_wake(unsigned int irq)
460 {
461 	return irq_set_irq_wake(irq, 0);
462 }
463 
464 /*
465  * irq_get_irqchip_state/irq_set_irqchip_state specific flags
466  */
467 enum irqchip_irq_state {
468 	IRQCHIP_STATE_PENDING,		/* Is interrupt pending? */
469 	IRQCHIP_STATE_ACTIVE,		/* Is interrupt in progress? */
470 	IRQCHIP_STATE_MASKED,		/* Is interrupt masked? */
471 	IRQCHIP_STATE_LINE_LEVEL,	/* Is IRQ line high? */
472 };
473 
474 extern int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
475 				 bool *state);
476 extern int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
477 				 bool state);
478 
479 #ifdef CONFIG_IRQ_FORCED_THREADING
480 # ifdef CONFIG_PREEMPT_RT
481 #  define force_irqthreads	(true)
482 # else
483 extern bool force_irqthreads;
484 # endif
485 #else
486 #define force_irqthreads	(0)
487 #endif
488 
489 #ifndef local_softirq_pending
490 
491 #ifndef local_softirq_pending_ref
492 #define local_softirq_pending_ref irq_stat.__softirq_pending
493 #endif
494 
495 #define local_softirq_pending()	(__this_cpu_read(local_softirq_pending_ref))
496 #define set_softirq_pending(x)	(__this_cpu_write(local_softirq_pending_ref, (x)))
497 #define or_softirq_pending(x)	(__this_cpu_or(local_softirq_pending_ref, (x)))
498 
499 #endif /* local_softirq_pending */
500 
501 /* Some architectures might implement lazy enabling/disabling of
502  * interrupts. In some cases, such as stop_machine, we might want
503  * to ensure that after a local_irq_disable(), interrupts have
504  * really been disabled in hardware. Such architectures need to
505  * implement the following hook.
506  */
507 #ifndef hard_irq_disable
508 #define hard_irq_disable()	do { } while(0)
509 #endif
510 
511 /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
512    frequency threaded job scheduling. For almost all the purposes
513    tasklets are more than enough. F.e. all serial device BHs et
514    al. should be converted to tasklets, not to softirqs.
515  */
516 
517 enum
518 {
519 	HI_SOFTIRQ=0,
520 	TIMER_SOFTIRQ,
521 	NET_TX_SOFTIRQ,
522 	NET_RX_SOFTIRQ,
523 	BLOCK_SOFTIRQ,
524 	IRQ_POLL_SOFTIRQ,
525 	TASKLET_SOFTIRQ,
526 	SCHED_SOFTIRQ,
527 	HRTIMER_SOFTIRQ, /* Unused, but kept as tools rely on the
528 			    numbering. Sigh! */
529 	RCU_SOFTIRQ,    /* Preferable RCU should always be the last softirq */
530 
531 	NR_SOFTIRQS
532 };
533 
534 #define SOFTIRQ_STOP_IDLE_MASK (~(1 << RCU_SOFTIRQ))
535 
536 /* map softirq index to softirq name. update 'softirq_to_name' in
537  * kernel/softirq.c when adding a new softirq.
538  */
539 extern const char * const softirq_to_name[NR_SOFTIRQS];
540 
541 /* softirq mask and active fields moved to irq_cpustat_t in
542  * asm/hardirq.h to get better cache usage.  KAO
543  */
544 
545 struct softirq_action
546 {
547 	void	(*action)(struct softirq_action *);
548 };
549 
550 asmlinkage void do_softirq(void);
551 asmlinkage void __do_softirq(void);
552 
553 #ifdef __ARCH_HAS_DO_SOFTIRQ
554 void do_softirq_own_stack(void);
555 #else
do_softirq_own_stack(void)556 static inline void do_softirq_own_stack(void)
557 {
558 	__do_softirq();
559 }
560 #endif
561 
562 extern void open_softirq(int nr, void (*action)(struct softirq_action *));
563 extern void softirq_init(void);
564 extern void __raise_softirq_irqoff(unsigned int nr);
565 
566 extern void raise_softirq_irqoff(unsigned int nr);
567 extern void raise_softirq(unsigned int nr);
568 
569 DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
570 
this_cpu_ksoftirqd(void)571 static inline struct task_struct *this_cpu_ksoftirqd(void)
572 {
573 	return this_cpu_read(ksoftirqd);
574 }
575 
576 /* Tasklets --- multithreaded analogue of BHs.
577 
578    Main feature differing them of generic softirqs: tasklet
579    is running only on one CPU simultaneously.
580 
581    Main feature differing them of BHs: different tasklets
582    may be run simultaneously on different CPUs.
583 
584    Properties:
585    * If tasklet_schedule() is called, then tasklet is guaranteed
586      to be executed on some cpu at least once after this.
587    * If the tasklet is already scheduled, but its execution is still not
588      started, it will be executed only once.
589    * If this tasklet is already running on another CPU (or schedule is called
590      from tasklet itself), it is rescheduled for later.
591    * Tasklet is strictly serialized wrt itself, but not
592      wrt another tasklets. If client needs some intertask synchronization,
593      he makes it with spinlocks.
594  */
595 
596 struct tasklet_struct
597 {
598 	struct tasklet_struct *next;
599 	unsigned long state;
600 	atomic_t count;
601 	void (*func)(unsigned long);
602 	unsigned long data;
603 };
604 
605 #define DECLARE_TASKLET_OLD(name, _func)		\
606 struct tasklet_struct name = {				\
607 	.count = ATOMIC_INIT(0),			\
608 	.func = _func,					\
609 }
610 
611 #define DECLARE_TASKLET_DISABLED_OLD(name, _func)	\
612 struct tasklet_struct name = {				\
613 	.count = ATOMIC_INIT(1),			\
614 	.func = _func,					\
615 }
616 
617 enum
618 {
619 	TASKLET_STATE_SCHED,	/* Tasklet is scheduled for execution */
620 	TASKLET_STATE_RUN	/* Tasklet is running (SMP only) */
621 };
622 
623 #ifdef CONFIG_SMP
tasklet_trylock(struct tasklet_struct * t)624 static inline int tasklet_trylock(struct tasklet_struct *t)
625 {
626 	return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
627 }
628 
tasklet_unlock(struct tasklet_struct * t)629 static inline void tasklet_unlock(struct tasklet_struct *t)
630 {
631 	smp_mb__before_atomic();
632 	clear_bit(TASKLET_STATE_RUN, &(t)->state);
633 }
634 
tasklet_unlock_wait(struct tasklet_struct * t)635 static inline void tasklet_unlock_wait(struct tasklet_struct *t)
636 {
637 	while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
638 }
639 #else
640 #define tasklet_trylock(t) 1
641 #define tasklet_unlock_wait(t) do { } while (0)
642 #define tasklet_unlock(t) do { } while (0)
643 #endif
644 
645 extern void __tasklet_schedule(struct tasklet_struct *t);
646 
tasklet_schedule(struct tasklet_struct * t)647 static inline void tasklet_schedule(struct tasklet_struct *t)
648 {
649 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
650 		__tasklet_schedule(t);
651 }
652 
653 extern void __tasklet_hi_schedule(struct tasklet_struct *t);
654 
tasklet_hi_schedule(struct tasklet_struct * t)655 static inline void tasklet_hi_schedule(struct tasklet_struct *t)
656 {
657 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
658 		__tasklet_hi_schedule(t);
659 }
660 
tasklet_disable_nosync(struct tasklet_struct * t)661 static inline void tasklet_disable_nosync(struct tasklet_struct *t)
662 {
663 	atomic_inc(&t->count);
664 	smp_mb__after_atomic();
665 }
666 
tasklet_disable(struct tasklet_struct * t)667 static inline void tasklet_disable(struct tasklet_struct *t)
668 {
669 	tasklet_disable_nosync(t);
670 	tasklet_unlock_wait(t);
671 	smp_mb();
672 }
673 
tasklet_enable(struct tasklet_struct * t)674 static inline void tasklet_enable(struct tasklet_struct *t)
675 {
676 	smp_mb__before_atomic();
677 	atomic_dec(&t->count);
678 }
679 
680 extern void tasklet_kill(struct tasklet_struct *t);
681 extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
682 extern void tasklet_init(struct tasklet_struct *t,
683 			 void (*func)(unsigned long), unsigned long data);
684 
685 /*
686  * Autoprobing for irqs:
687  *
688  * probe_irq_on() and probe_irq_off() provide robust primitives
689  * for accurate IRQ probing during kernel initialization.  They are
690  * reasonably simple to use, are not "fooled" by spurious interrupts,
691  * and, unlike other attempts at IRQ probing, they do not get hung on
692  * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
693  *
694  * For reasonably foolproof probing, use them as follows:
695  *
696  * 1. clear and/or mask the device's internal interrupt.
697  * 2. sti();
698  * 3. irqs = probe_irq_on();      // "take over" all unassigned idle IRQs
699  * 4. enable the device and cause it to trigger an interrupt.
700  * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
701  * 6. irq = probe_irq_off(irqs);  // get IRQ number, 0=none, negative=multiple
702  * 7. service the device to clear its pending interrupt.
703  * 8. loop again if paranoia is required.
704  *
705  * probe_irq_on() returns a mask of allocated irq's.
706  *
707  * probe_irq_off() takes the mask as a parameter,
708  * and returns the irq number which occurred,
709  * or zero if none occurred, or a negative irq number
710  * if more than one irq occurred.
711  */
712 
713 #if !defined(CONFIG_GENERIC_IRQ_PROBE)
probe_irq_on(void)714 static inline unsigned long probe_irq_on(void)
715 {
716 	return 0;
717 }
probe_irq_off(unsigned long val)718 static inline int probe_irq_off(unsigned long val)
719 {
720 	return 0;
721 }
probe_irq_mask(unsigned long val)722 static inline unsigned int probe_irq_mask(unsigned long val)
723 {
724 	return 0;
725 }
726 #else
727 extern unsigned long probe_irq_on(void);	/* returns 0 on failure */
728 extern int probe_irq_off(unsigned long);	/* returns 0 or negative on failure */
729 extern unsigned int probe_irq_mask(unsigned long);	/* returns mask of ISA interrupts */
730 #endif
731 
732 #ifdef CONFIG_PROC_FS
733 /* Initialize /proc/irq/ */
734 extern void init_irq_proc(void);
735 #else
init_irq_proc(void)736 static inline void init_irq_proc(void)
737 {
738 }
739 #endif
740 
741 #ifdef CONFIG_IRQ_TIMINGS
742 void irq_timings_enable(void);
743 void irq_timings_disable(void);
744 u64 irq_timings_next_event(u64 now);
745 #endif
746 
747 struct seq_file;
748 int show_interrupts(struct seq_file *p, void *v);
749 int arch_show_interrupts(struct seq_file *p, int prec);
750 
751 extern int early_irq_init(void);
752 extern int arch_probe_nr_irqs(void);
753 extern int arch_early_irq_init(void);
754 
755 /*
756  * We want to know which function is an entrypoint of a hardirq or a softirq.
757  */
758 #define __irq_entry		 __attribute__((__section__(".irqentry.text")))
759 #define __softirq_entry  \
760 	__attribute__((__section__(".softirqentry.text")))
761 
762 #endif
763