xref: /linux-6.15/include/linux/interrupt.h (revision ec7ac6af)
1 /* interrupt.h */
2 #ifndef _LINUX_INTERRUPT_H
3 #define _LINUX_INTERRUPT_H
4 
5 #include <linux/kernel.h>
6 #include <linux/linkage.h>
7 #include <linux/bitops.h>
8 #include <linux/preempt.h>
9 #include <linux/cpumask.h>
10 #include <linux/irqreturn.h>
11 #include <linux/irqnr.h>
12 #include <linux/hardirq.h>
13 #include <linux/irqflags.h>
14 #include <linux/hrtimer.h>
15 #include <linux/kref.h>
16 #include <linux/workqueue.h>
17 
18 #include <linux/atomic.h>
19 #include <asm/ptrace.h>
20 #include <asm/irq.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_DISABLED - keep irqs disabled when calling the action handler.
43  *                 DEPRECATED. This flag is a NOOP and scheduled to be removed
44  * IRQF_SHARED - allow sharing the irq among several devices
45  * IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
46  * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
47  * IRQF_PERCPU - Interrupt is per cpu
48  * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
49  * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
50  *                registered first in an shared interrupt is considered for
51  *                performance reasons)
52  * IRQF_ONESHOT - Interrupt is not reenabled after the hardirq handler finished.
53  *                Used by threaded interrupts which need to keep the
54  *                irq line disabled until the threaded handler has been run.
55  * IRQF_NO_SUSPEND - Do not disable this IRQ during suspend
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  */
61 #define IRQF_DISABLED		0x00000020
62 #define IRQF_SHARED		0x00000080
63 #define IRQF_PROBE_SHARED	0x00000100
64 #define __IRQF_TIMER		0x00000200
65 #define IRQF_PERCPU		0x00000400
66 #define IRQF_NOBALANCING	0x00000800
67 #define IRQF_IRQPOLL		0x00001000
68 #define IRQF_ONESHOT		0x00002000
69 #define IRQF_NO_SUSPEND		0x00004000
70 #define IRQF_FORCE_RESUME	0x00008000
71 #define IRQF_NO_THREAD		0x00010000
72 #define IRQF_EARLY_RESUME	0x00020000
73 
74 #define IRQF_TIMER		(__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
75 
76 /*
77  * These values can be returned by request_any_context_irq() and
78  * describe the context the interrupt will be run in.
79  *
80  * IRQC_IS_HARDIRQ - interrupt runs in hardirq context
81  * IRQC_IS_NESTED - interrupt runs in a nested threaded context
82  */
83 enum {
84 	IRQC_IS_HARDIRQ	= 0,
85 	IRQC_IS_NESTED,
86 };
87 
88 typedef irqreturn_t (*irq_handler_t)(int, void *);
89 
90 /**
91  * struct irqaction - per interrupt action descriptor
92  * @handler:	interrupt handler function
93  * @name:	name of the device
94  * @dev_id:	cookie to identify the device
95  * @percpu_dev_id:	cookie to identify the device
96  * @next:	pointer to the next irqaction for shared interrupts
97  * @irq:	interrupt number
98  * @flags:	flags (see IRQF_* above)
99  * @thread_fn:	interrupt handler function for threaded interrupts
100  * @thread:	thread pointer for threaded interrupts
101  * @thread_flags:	flags related to @thread
102  * @thread_mask:	bitmask for keeping track of @thread activity
103  * @dir:	pointer to the proc/irq/NN/name entry
104  */
105 struct irqaction {
106 	irq_handler_t		handler;
107 	void			*dev_id;
108 	void __percpu		*percpu_dev_id;
109 	struct irqaction	*next;
110 	irq_handler_t		thread_fn;
111 	struct task_struct	*thread;
112 	unsigned int		irq;
113 	unsigned int		flags;
114 	unsigned long		thread_flags;
115 	unsigned long		thread_mask;
116 	const char		*name;
117 	struct proc_dir_entry	*dir;
118 } ____cacheline_internodealigned_in_smp;
119 
120 extern irqreturn_t no_action(int cpl, void *dev_id);
121 
122 extern int __must_check
123 request_threaded_irq(unsigned int irq, irq_handler_t handler,
124 		     irq_handler_t thread_fn,
125 		     unsigned long flags, const char *name, void *dev);
126 
127 static inline int __must_check
128 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
129 	    const char *name, void *dev)
130 {
131 	return request_threaded_irq(irq, handler, NULL, flags, name, dev);
132 }
133 
134 extern int __must_check
135 request_any_context_irq(unsigned int irq, irq_handler_t handler,
136 			unsigned long flags, const char *name, void *dev_id);
137 
138 extern int __must_check
139 request_percpu_irq(unsigned int irq, irq_handler_t handler,
140 		   const char *devname, void __percpu *percpu_dev_id);
141 
142 extern void free_irq(unsigned int, void *);
143 extern void free_percpu_irq(unsigned int, void __percpu *);
144 
145 struct device;
146 
147 extern int __must_check
148 devm_request_threaded_irq(struct device *dev, unsigned int irq,
149 			  irq_handler_t handler, irq_handler_t thread_fn,
150 			  unsigned long irqflags, const char *devname,
151 			  void *dev_id);
152 
153 static inline int __must_check
154 devm_request_irq(struct device *dev, unsigned int irq, irq_handler_t handler,
155 		 unsigned long irqflags, const char *devname, void *dev_id)
156 {
157 	return devm_request_threaded_irq(dev, irq, handler, NULL, irqflags,
158 					 devname, dev_id);
159 }
160 
161 extern int __must_check
162 devm_request_any_context_irq(struct device *dev, unsigned int irq,
163 		 irq_handler_t handler, unsigned long irqflags,
164 		 const char *devname, void *dev_id);
165 
166 extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
167 
168 /*
169  * On lockdep we dont want to enable hardirqs in hardirq
170  * context. Use local_irq_enable_in_hardirq() to annotate
171  * kernel code that has to do this nevertheless (pretty much
172  * the only valid case is for old/broken hardware that is
173  * insanely slow).
174  *
175  * NOTE: in theory this might break fragile code that relies
176  * on hardirq delivery - in practice we dont seem to have such
177  * places left. So the only effect should be slightly increased
178  * irqs-off latencies.
179  */
180 #ifdef CONFIG_LOCKDEP
181 # define local_irq_enable_in_hardirq()	do { } while (0)
182 #else
183 # define local_irq_enable_in_hardirq()	local_irq_enable()
184 #endif
185 
186 extern void disable_irq_nosync(unsigned int irq);
187 extern void disable_irq(unsigned int irq);
188 extern void disable_percpu_irq(unsigned int irq);
189 extern void enable_irq(unsigned int irq);
190 extern void enable_percpu_irq(unsigned int irq, unsigned int type);
191 
192 /* The following three functions are for the core kernel use only. */
193 extern void suspend_device_irqs(void);
194 extern void resume_device_irqs(void);
195 #ifdef CONFIG_PM_SLEEP
196 extern int check_wakeup_irqs(void);
197 #else
198 static inline int check_wakeup_irqs(void) { return 0; }
199 #endif
200 
201 #if defined(CONFIG_SMP)
202 
203 extern cpumask_var_t irq_default_affinity;
204 
205 extern int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask);
206 extern int irq_can_set_affinity(unsigned int irq);
207 extern int irq_select_affinity(unsigned int irq);
208 
209 extern int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m);
210 
211 /**
212  * struct irq_affinity_notify - context for notification of IRQ affinity changes
213  * @irq:		Interrupt to which notification applies
214  * @kref:		Reference count, for internal use
215  * @work:		Work item, for internal use
216  * @notify:		Function to be called on change.  This will be
217  *			called in process context.
218  * @release:		Function to be called on release.  This will be
219  *			called in process context.  Once registered, the
220  *			structure must only be freed when this function is
221  *			called or later.
222  */
223 struct irq_affinity_notify {
224 	unsigned int irq;
225 	struct kref kref;
226 	struct work_struct work;
227 	void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
228 	void (*release)(struct kref *ref);
229 };
230 
231 extern int
232 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
233 
234 #else /* CONFIG_SMP */
235 
236 static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
237 {
238 	return -EINVAL;
239 }
240 
241 static inline int irq_can_set_affinity(unsigned int irq)
242 {
243 	return 0;
244 }
245 
246 static inline int irq_select_affinity(unsigned int irq)  { return 0; }
247 
248 static inline int irq_set_affinity_hint(unsigned int irq,
249 					const struct cpumask *m)
250 {
251 	return -EINVAL;
252 }
253 #endif /* CONFIG_SMP */
254 
255 /*
256  * Special lockdep variants of irq disabling/enabling.
257  * These should be used for locking constructs that
258  * know that a particular irq context which is disabled,
259  * and which is the only irq-context user of a lock,
260  * that it's safe to take the lock in the irq-disabled
261  * section without disabling hardirqs.
262  *
263  * On !CONFIG_LOCKDEP they are equivalent to the normal
264  * irq disable/enable methods.
265  */
266 static inline void disable_irq_nosync_lockdep(unsigned int irq)
267 {
268 	disable_irq_nosync(irq);
269 #ifdef CONFIG_LOCKDEP
270 	local_irq_disable();
271 #endif
272 }
273 
274 static inline void disable_irq_nosync_lockdep_irqsave(unsigned int irq, unsigned long *flags)
275 {
276 	disable_irq_nosync(irq);
277 #ifdef CONFIG_LOCKDEP
278 	local_irq_save(*flags);
279 #endif
280 }
281 
282 static inline void disable_irq_lockdep(unsigned int irq)
283 {
284 	disable_irq(irq);
285 #ifdef CONFIG_LOCKDEP
286 	local_irq_disable();
287 #endif
288 }
289 
290 static inline void enable_irq_lockdep(unsigned int irq)
291 {
292 #ifdef CONFIG_LOCKDEP
293 	local_irq_enable();
294 #endif
295 	enable_irq(irq);
296 }
297 
298 static inline void enable_irq_lockdep_irqrestore(unsigned int irq, unsigned long *flags)
299 {
300 #ifdef CONFIG_LOCKDEP
301 	local_irq_restore(*flags);
302 #endif
303 	enable_irq(irq);
304 }
305 
306 /* IRQ wakeup (PM) control: */
307 extern int irq_set_irq_wake(unsigned int irq, unsigned int on);
308 
309 static inline int enable_irq_wake(unsigned int irq)
310 {
311 	return irq_set_irq_wake(irq, 1);
312 }
313 
314 static inline int disable_irq_wake(unsigned int irq)
315 {
316 	return irq_set_irq_wake(irq, 0);
317 }
318 
319 
320 #ifdef CONFIG_IRQ_FORCED_THREADING
321 extern bool force_irqthreads;
322 #else
323 #define force_irqthreads	(0)
324 #endif
325 
326 #ifndef __ARCH_SET_SOFTIRQ_PENDING
327 #define set_softirq_pending(x) (local_softirq_pending() = (x))
328 #define or_softirq_pending(x)  (local_softirq_pending() |= (x))
329 #endif
330 
331 /* Some architectures might implement lazy enabling/disabling of
332  * interrupts. In some cases, such as stop_machine, we might want
333  * to ensure that after a local_irq_disable(), interrupts have
334  * really been disabled in hardware. Such architectures need to
335  * implement the following hook.
336  */
337 #ifndef hard_irq_disable
338 #define hard_irq_disable()	do { } while(0)
339 #endif
340 
341 /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
342    frequency threaded job scheduling. For almost all the purposes
343    tasklets are more than enough. F.e. all serial device BHs et
344    al. should be converted to tasklets, not to softirqs.
345  */
346 
347 enum
348 {
349 	HI_SOFTIRQ=0,
350 	TIMER_SOFTIRQ,
351 	NET_TX_SOFTIRQ,
352 	NET_RX_SOFTIRQ,
353 	BLOCK_SOFTIRQ,
354 	BLOCK_IOPOLL_SOFTIRQ,
355 	TASKLET_SOFTIRQ,
356 	SCHED_SOFTIRQ,
357 	HRTIMER_SOFTIRQ,
358 	RCU_SOFTIRQ,    /* Preferable RCU should always be the last softirq */
359 
360 	NR_SOFTIRQS
361 };
362 
363 #define SOFTIRQ_STOP_IDLE_MASK (~(1 << RCU_SOFTIRQ))
364 
365 /* map softirq index to softirq name. update 'softirq_to_name' in
366  * kernel/softirq.c when adding a new softirq.
367  */
368 extern const char * const softirq_to_name[NR_SOFTIRQS];
369 
370 /* softirq mask and active fields moved to irq_cpustat_t in
371  * asm/hardirq.h to get better cache usage.  KAO
372  */
373 
374 struct softirq_action
375 {
376 	void	(*action)(struct softirq_action *);
377 };
378 
379 asmlinkage void do_softirq(void);
380 asmlinkage void __do_softirq(void);
381 
382 #ifdef __ARCH_HAS_DO_SOFTIRQ
383 void do_softirq_own_stack(void);
384 #else
385 static inline void do_softirq_own_stack(void)
386 {
387 	__do_softirq();
388 }
389 #endif
390 
391 extern void open_softirq(int nr, void (*action)(struct softirq_action *));
392 extern void softirq_init(void);
393 extern void __raise_softirq_irqoff(unsigned int nr);
394 
395 extern void raise_softirq_irqoff(unsigned int nr);
396 extern void raise_softirq(unsigned int nr);
397 
398 DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
399 
400 static inline struct task_struct *this_cpu_ksoftirqd(void)
401 {
402 	return this_cpu_read(ksoftirqd);
403 }
404 
405 /* Tasklets --- multithreaded analogue of BHs.
406 
407    Main feature differing them of generic softirqs: tasklet
408    is running only on one CPU simultaneously.
409 
410    Main feature differing them of BHs: different tasklets
411    may be run simultaneously on different CPUs.
412 
413    Properties:
414    * If tasklet_schedule() is called, then tasklet is guaranteed
415      to be executed on some cpu at least once after this.
416    * If the tasklet is already scheduled, but its execution is still not
417      started, it will be executed only once.
418    * If this tasklet is already running on another CPU (or schedule is called
419      from tasklet itself), it is rescheduled for later.
420    * Tasklet is strictly serialized wrt itself, but not
421      wrt another tasklets. If client needs some intertask synchronization,
422      he makes it with spinlocks.
423  */
424 
425 struct tasklet_struct
426 {
427 	struct tasklet_struct *next;
428 	unsigned long state;
429 	atomic_t count;
430 	void (*func)(unsigned long);
431 	unsigned long data;
432 };
433 
434 #define DECLARE_TASKLET(name, func, data) \
435 struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }
436 
437 #define DECLARE_TASKLET_DISABLED(name, func, data) \
438 struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
439 
440 
441 enum
442 {
443 	TASKLET_STATE_SCHED,	/* Tasklet is scheduled for execution */
444 	TASKLET_STATE_RUN	/* Tasklet is running (SMP only) */
445 };
446 
447 #ifdef CONFIG_SMP
448 static inline int tasklet_trylock(struct tasklet_struct *t)
449 {
450 	return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
451 }
452 
453 static inline void tasklet_unlock(struct tasklet_struct *t)
454 {
455 	smp_mb__before_clear_bit();
456 	clear_bit(TASKLET_STATE_RUN, &(t)->state);
457 }
458 
459 static inline void tasklet_unlock_wait(struct tasklet_struct *t)
460 {
461 	while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
462 }
463 #else
464 #define tasklet_trylock(t) 1
465 #define tasklet_unlock_wait(t) do { } while (0)
466 #define tasklet_unlock(t) do { } while (0)
467 #endif
468 
469 extern void __tasklet_schedule(struct tasklet_struct *t);
470 
471 static inline void tasklet_schedule(struct tasklet_struct *t)
472 {
473 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
474 		__tasklet_schedule(t);
475 }
476 
477 extern void __tasklet_hi_schedule(struct tasklet_struct *t);
478 
479 static inline void tasklet_hi_schedule(struct tasklet_struct *t)
480 {
481 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
482 		__tasklet_hi_schedule(t);
483 }
484 
485 extern void __tasklet_hi_schedule_first(struct tasklet_struct *t);
486 
487 /*
488  * This version avoids touching any other tasklets. Needed for kmemcheck
489  * in order not to take any page faults while enqueueing this tasklet;
490  * consider VERY carefully whether you really need this or
491  * tasklet_hi_schedule()...
492  */
493 static inline void tasklet_hi_schedule_first(struct tasklet_struct *t)
494 {
495 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
496 		__tasklet_hi_schedule_first(t);
497 }
498 
499 
500 static inline void tasklet_disable_nosync(struct tasklet_struct *t)
501 {
502 	atomic_inc(&t->count);
503 	smp_mb__after_atomic_inc();
504 }
505 
506 static inline void tasklet_disable(struct tasklet_struct *t)
507 {
508 	tasklet_disable_nosync(t);
509 	tasklet_unlock_wait(t);
510 	smp_mb();
511 }
512 
513 static inline void tasklet_enable(struct tasklet_struct *t)
514 {
515 	smp_mb__before_atomic_dec();
516 	atomic_dec(&t->count);
517 }
518 
519 static inline void tasklet_hi_enable(struct tasklet_struct *t)
520 {
521 	smp_mb__before_atomic_dec();
522 	atomic_dec(&t->count);
523 }
524 
525 extern void tasklet_kill(struct tasklet_struct *t);
526 extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
527 extern void tasklet_init(struct tasklet_struct *t,
528 			 void (*func)(unsigned long), unsigned long data);
529 
530 struct tasklet_hrtimer {
531 	struct hrtimer		timer;
532 	struct tasklet_struct	tasklet;
533 	enum hrtimer_restart	(*function)(struct hrtimer *);
534 };
535 
536 extern void
537 tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
538 		     enum hrtimer_restart (*function)(struct hrtimer *),
539 		     clockid_t which_clock, enum hrtimer_mode mode);
540 
541 static inline
542 int tasklet_hrtimer_start(struct tasklet_hrtimer *ttimer, ktime_t time,
543 			  const enum hrtimer_mode mode)
544 {
545 	return hrtimer_start(&ttimer->timer, time, mode);
546 }
547 
548 static inline
549 void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
550 {
551 	hrtimer_cancel(&ttimer->timer);
552 	tasklet_kill(&ttimer->tasklet);
553 }
554 
555 /*
556  * Autoprobing for irqs:
557  *
558  * probe_irq_on() and probe_irq_off() provide robust primitives
559  * for accurate IRQ probing during kernel initialization.  They are
560  * reasonably simple to use, are not "fooled" by spurious interrupts,
561  * and, unlike other attempts at IRQ probing, they do not get hung on
562  * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
563  *
564  * For reasonably foolproof probing, use them as follows:
565  *
566  * 1. clear and/or mask the device's internal interrupt.
567  * 2. sti();
568  * 3. irqs = probe_irq_on();      // "take over" all unassigned idle IRQs
569  * 4. enable the device and cause it to trigger an interrupt.
570  * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
571  * 6. irq = probe_irq_off(irqs);  // get IRQ number, 0=none, negative=multiple
572  * 7. service the device to clear its pending interrupt.
573  * 8. loop again if paranoia is required.
574  *
575  * probe_irq_on() returns a mask of allocated irq's.
576  *
577  * probe_irq_off() takes the mask as a parameter,
578  * and returns the irq number which occurred,
579  * or zero if none occurred, or a negative irq number
580  * if more than one irq occurred.
581  */
582 
583 #if !defined(CONFIG_GENERIC_IRQ_PROBE)
584 static inline unsigned long probe_irq_on(void)
585 {
586 	return 0;
587 }
588 static inline int probe_irq_off(unsigned long val)
589 {
590 	return 0;
591 }
592 static inline unsigned int probe_irq_mask(unsigned long val)
593 {
594 	return 0;
595 }
596 #else
597 extern unsigned long probe_irq_on(void);	/* returns 0 on failure */
598 extern int probe_irq_off(unsigned long);	/* returns 0 or negative on failure */
599 extern unsigned int probe_irq_mask(unsigned long);	/* returns mask of ISA interrupts */
600 #endif
601 
602 #ifdef CONFIG_PROC_FS
603 /* Initialize /proc/irq/ */
604 extern void init_irq_proc(void);
605 #else
606 static inline void init_irq_proc(void)
607 {
608 }
609 #endif
610 
611 struct seq_file;
612 int show_interrupts(struct seq_file *p, void *v);
613 int arch_show_interrupts(struct seq_file *p, int prec);
614 
615 extern int early_irq_init(void);
616 extern int arch_probe_nr_irqs(void);
617 extern int arch_early_irq_init(void);
618 
619 #endif
620