xref: /linux-6.15/include/linux/interrupt.h (revision 4e57b681)
1 /* interrupt.h */
2 #ifndef _LINUX_INTERRUPT_H
3 #define _LINUX_INTERRUPT_H
4 
5 #include <linux/config.h>
6 #include <linux/kernel.h>
7 #include <linux/linkage.h>
8 #include <linux/bitops.h>
9 #include <linux/preempt.h>
10 #include <linux/cpumask.h>
11 #include <linux/hardirq.h>
12 #include <asm/atomic.h>
13 #include <asm/ptrace.h>
14 #include <asm/system.h>
15 
16 /*
17  * For 2.4.x compatibility, 2.4.x can use
18  *
19  *	typedef void irqreturn_t;
20  *	#define IRQ_NONE
21  *	#define IRQ_HANDLED
22  *	#define IRQ_RETVAL(x)
23  *
24  * To mix old-style and new-style irq handler returns.
25  *
26  * IRQ_NONE means we didn't handle it.
27  * IRQ_HANDLED means that we did have a valid interrupt and handled it.
28  * IRQ_RETVAL(x) selects on the two depending on x being non-zero (for handled)
29  */
30 typedef int irqreturn_t;
31 
32 #define IRQ_NONE	(0)
33 #define IRQ_HANDLED	(1)
34 #define IRQ_RETVAL(x)	((x) != 0)
35 
36 struct irqaction {
37 	irqreturn_t (*handler)(int, void *, struct pt_regs *);
38 	unsigned long flags;
39 	cpumask_t mask;
40 	const char *name;
41 	void *dev_id;
42 	struct irqaction *next;
43 	int irq;
44 	struct proc_dir_entry *dir;
45 };
46 
47 extern irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs);
48 extern int request_irq(unsigned int,
49 		       irqreturn_t (*handler)(int, void *, struct pt_regs *),
50 		       unsigned long, const char *, void *);
51 extern void free_irq(unsigned int, void *);
52 
53 
54 #ifdef CONFIG_GENERIC_HARDIRQS
55 extern void disable_irq_nosync(unsigned int irq);
56 extern void disable_irq(unsigned int irq);
57 extern void enable_irq(unsigned int irq);
58 #endif
59 
60 #ifndef __ARCH_SET_SOFTIRQ_PENDING
61 #define set_softirq_pending(x) (local_softirq_pending() = (x))
62 #define or_softirq_pending(x)  (local_softirq_pending() |= (x))
63 #endif
64 
65 /*
66  * Temporary defines for UP kernels, until all code gets fixed.
67  */
68 #ifndef CONFIG_SMP
69 static inline void __deprecated cli(void)
70 {
71 	local_irq_disable();
72 }
73 static inline void __deprecated sti(void)
74 {
75 	local_irq_enable();
76 }
77 static inline void __deprecated save_flags(unsigned long *x)
78 {
79 	local_save_flags(*x);
80 }
81 #define save_flags(x) save_flags(&x);
82 static inline void __deprecated restore_flags(unsigned long x)
83 {
84 	local_irq_restore(x);
85 }
86 
87 static inline void __deprecated save_and_cli(unsigned long *x)
88 {
89 	local_irq_save(*x);
90 }
91 #define save_and_cli(x)	save_and_cli(&x)
92 #endif /* CONFIG_SMP */
93 
94 /* SoftIRQ primitives.  */
95 #define local_bh_disable() \
96 		do { add_preempt_count(SOFTIRQ_OFFSET); barrier(); } while (0)
97 #define __local_bh_enable() \
98 		do { barrier(); sub_preempt_count(SOFTIRQ_OFFSET); } while (0)
99 
100 extern void local_bh_enable(void);
101 
102 /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
103    frequency threaded job scheduling. For almost all the purposes
104    tasklets are more than enough. F.e. all serial device BHs et
105    al. should be converted to tasklets, not to softirqs.
106  */
107 
108 enum
109 {
110 	HI_SOFTIRQ=0,
111 	TIMER_SOFTIRQ,
112 	NET_TX_SOFTIRQ,
113 	NET_RX_SOFTIRQ,
114 	SCSI_SOFTIRQ,
115 	TASKLET_SOFTIRQ
116 };
117 
118 /* softirq mask and active fields moved to irq_cpustat_t in
119  * asm/hardirq.h to get better cache usage.  KAO
120  */
121 
122 struct softirq_action
123 {
124 	void	(*action)(struct softirq_action *);
125 	void	*data;
126 };
127 
128 asmlinkage void do_softirq(void);
129 extern void open_softirq(int nr, void (*action)(struct softirq_action*), void *data);
130 extern void softirq_init(void);
131 #define __raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
132 extern void FASTCALL(raise_softirq_irqoff(unsigned int nr));
133 extern void FASTCALL(raise_softirq(unsigned int nr));
134 
135 
136 /* Tasklets --- multithreaded analogue of BHs.
137 
138    Main feature differing them of generic softirqs: tasklet
139    is running only on one CPU simultaneously.
140 
141    Main feature differing them of BHs: different tasklets
142    may be run simultaneously on different CPUs.
143 
144    Properties:
145    * If tasklet_schedule() is called, then tasklet is guaranteed
146      to be executed on some cpu at least once after this.
147    * If the tasklet is already scheduled, but its excecution is still not
148      started, it will be executed only once.
149    * If this tasklet is already running on another CPU (or schedule is called
150      from tasklet itself), it is rescheduled for later.
151    * Tasklet is strictly serialized wrt itself, but not
152      wrt another tasklets. If client needs some intertask synchronization,
153      he makes it with spinlocks.
154  */
155 
156 struct tasklet_struct
157 {
158 	struct tasklet_struct *next;
159 	unsigned long state;
160 	atomic_t count;
161 	void (*func)(unsigned long);
162 	unsigned long data;
163 };
164 
165 #define DECLARE_TASKLET(name, func, data) \
166 struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }
167 
168 #define DECLARE_TASKLET_DISABLED(name, func, data) \
169 struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
170 
171 
172 enum
173 {
174 	TASKLET_STATE_SCHED,	/* Tasklet is scheduled for execution */
175 	TASKLET_STATE_RUN	/* Tasklet is running (SMP only) */
176 };
177 
178 #ifdef CONFIG_SMP
179 static inline int tasklet_trylock(struct tasklet_struct *t)
180 {
181 	return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
182 }
183 
184 static inline void tasklet_unlock(struct tasklet_struct *t)
185 {
186 	smp_mb__before_clear_bit();
187 	clear_bit(TASKLET_STATE_RUN, &(t)->state);
188 }
189 
190 static inline void tasklet_unlock_wait(struct tasklet_struct *t)
191 {
192 	while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
193 }
194 #else
195 #define tasklet_trylock(t) 1
196 #define tasklet_unlock_wait(t) do { } while (0)
197 #define tasklet_unlock(t) do { } while (0)
198 #endif
199 
200 extern void FASTCALL(__tasklet_schedule(struct tasklet_struct *t));
201 
202 static inline void tasklet_schedule(struct tasklet_struct *t)
203 {
204 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
205 		__tasklet_schedule(t);
206 }
207 
208 extern void FASTCALL(__tasklet_hi_schedule(struct tasklet_struct *t));
209 
210 static inline void tasklet_hi_schedule(struct tasklet_struct *t)
211 {
212 	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
213 		__tasklet_hi_schedule(t);
214 }
215 
216 
217 static inline void tasklet_disable_nosync(struct tasklet_struct *t)
218 {
219 	atomic_inc(&t->count);
220 	smp_mb__after_atomic_inc();
221 }
222 
223 static inline void tasklet_disable(struct tasklet_struct *t)
224 {
225 	tasklet_disable_nosync(t);
226 	tasklet_unlock_wait(t);
227 	smp_mb();
228 }
229 
230 static inline void tasklet_enable(struct tasklet_struct *t)
231 {
232 	smp_mb__before_atomic_dec();
233 	atomic_dec(&t->count);
234 }
235 
236 static inline void tasklet_hi_enable(struct tasklet_struct *t)
237 {
238 	smp_mb__before_atomic_dec();
239 	atomic_dec(&t->count);
240 }
241 
242 extern void tasklet_kill(struct tasklet_struct *t);
243 extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
244 extern void tasklet_init(struct tasklet_struct *t,
245 			 void (*func)(unsigned long), unsigned long data);
246 
247 /*
248  * Autoprobing for irqs:
249  *
250  * probe_irq_on() and probe_irq_off() provide robust primitives
251  * for accurate IRQ probing during kernel initialization.  They are
252  * reasonably simple to use, are not "fooled" by spurious interrupts,
253  * and, unlike other attempts at IRQ probing, they do not get hung on
254  * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
255  *
256  * For reasonably foolproof probing, use them as follows:
257  *
258  * 1. clear and/or mask the device's internal interrupt.
259  * 2. sti();
260  * 3. irqs = probe_irq_on();      // "take over" all unassigned idle IRQs
261  * 4. enable the device and cause it to trigger an interrupt.
262  * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
263  * 6. irq = probe_irq_off(irqs);  // get IRQ number, 0=none, negative=multiple
264  * 7. service the device to clear its pending interrupt.
265  * 8. loop again if paranoia is required.
266  *
267  * probe_irq_on() returns a mask of allocated irq's.
268  *
269  * probe_irq_off() takes the mask as a parameter,
270  * and returns the irq number which occurred,
271  * or zero if none occurred, or a negative irq number
272  * if more than one irq occurred.
273  */
274 
275 #if defined(CONFIG_GENERIC_HARDIRQS) && !defined(CONFIG_GENERIC_IRQ_PROBE)
276 static inline unsigned long probe_irq_on(void)
277 {
278 	return 0;
279 }
280 static inline int probe_irq_off(unsigned long val)
281 {
282 	return 0;
283 }
284 static inline unsigned int probe_irq_mask(unsigned long val)
285 {
286 	return 0;
287 }
288 #else
289 extern unsigned long probe_irq_on(void);	/* returns 0 on failure */
290 extern int probe_irq_off(unsigned long);	/* returns 0 or negative on failure */
291 extern unsigned int probe_irq_mask(unsigned long);	/* returns mask of ISA interrupts */
292 #endif
293 
294 #endif
295