Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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/hardirq.h>
 12 #include <linux/sched.h>
 13 #include <linux/irqflags.h>
 14 #include <asm/atomic.h>
 15 #include <asm/ptrace.h>
 16 #include <asm/system.h>
 17 
 18 /*
 19  * These correspond to the IORESOURCE_IRQ_* defines in
 20  * linux/ioport.h to select the interrupt line behaviour.  When
 21  * requesting an interrupt without specifying a IRQF_TRIGGER, the
 22  * setting should be assumed to be "as already configured", which
 23  * may be as per machine or firmware initialisation.
 24  */
 25 #define IRQF_TRIGGER_NONE       0x00000000
 26 #define IRQF_TRIGGER_RISING     0x00000001
 27 #define IRQF_TRIGGER_FALLING    0x00000002
 28 #define IRQF_TRIGGER_HIGH       0x00000004
 29 #define IRQF_TRIGGER_LOW        0x00000008
 30 #define IRQF_TRIGGER_MASK       (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
 31                                  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
 32 #define IRQF_TRIGGER_PROBE      0x00000010
 33 
 34 /*
 35  * These flags used only by the kernel as part of the
 36  * irq handling routines.
 37  *
 38  * IRQF_DISABLED - keep irqs disabled when calling the action handler
 39  * IRQF_SAMPLE_RANDOM - irq is used to feed the random generator
 40  * IRQF_SHARED - allow sharing the irq among several devices
 41  * IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
 42  * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
 43  * IRQF_PERCPU - Interrupt is per cpu
 44  * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
 45  * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
 46  *                registered first in an shared interrupt is considered for
 47  *                performance reasons)
 48  */
 49 #define IRQF_DISABLED           0x00000020
 50 #define IRQF_SAMPLE_RANDOM      0x00000040
 51 #define IRQF_SHARED             0x00000080
 52 #define IRQF_PROBE_SHARED       0x00000100
 53 #define __IRQF_TIMER            0x00000200
 54 #define IRQF_PERCPU             0x00000400
 55 #define IRQF_NOBALANCING        0x00000800
 56 #define IRQF_IRQPOLL            0x00001000
 57 #define IRQF_NODELAY            0x00002000
 58 #define IRQF_TIMER              (__IRQF_TIMER | IRQF_NODELAY)
 59 
 60 typedef irqreturn_t (*irq_handler_t)(int, void *);
 61 
 62 struct irqaction {
 63         irq_handler_t handler;
 64         unsigned long flags;
 65         cpumask_t mask;
 66         const char *name;
 67         void *dev_id;
 68         struct irqaction *next;
 69         int irq;
 70         struct proc_dir_entry *dir, *threaded;
 71 };
 72 
 73 extern irqreturn_t no_action(int cpl, void *dev_id);
 74 extern int __must_check request_irq(unsigned int, irq_handler_t handler,
 75                        unsigned long, const char *, void *);
 76 extern void free_irq(unsigned int, void *);
 77 
 78 struct device;
 79 
 80 extern int __must_check devm_request_irq(struct device *dev, unsigned int irq,
 81                             irq_handler_t handler, unsigned long irqflags,
 82                             const char *devname, void *dev_id);
 83 extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
 84 
 85 /*
 86  * On lockdep we dont want to enable hardirqs in hardirq
 87  * context. Use local_irq_enable_in_hardirq() to annotate
 88  * kernel code that has to do this nevertheless (pretty much
 89  * the only valid case is for old/broken hardware that is
 90  * insanely slow).
 91  *
 92  * NOTE: in theory this might break fragile code that relies
 93  * on hardirq delivery - in practice we dont seem to have such
 94  * places left. So the only effect should be slightly increased
 95  * irqs-off latencies.
 96  */
 97 #ifdef CONFIG_LOCKDEP
 98 # define local_irq_enable_in_hardirq()  do { } while (0)
 99 #else
100 # define local_irq_enable_in_hardirq()  local_irq_enable_nort()
101 #endif
102 
103 extern void disable_irq_nosync(unsigned int irq);
104 extern void disable_irq(unsigned int irq);
105 extern void enable_irq(unsigned int irq);
106 
107 #ifdef CONFIG_GENERIC_HARDIRQS
108 /*
109  * Special lockdep variants of irq disabling/enabling.
110  * These should be used for locking constructs that
111  * know that a particular irq context which is disabled,
112  * and which is the only irq-context user of a lock,
113  * that it's safe to take the lock in the irq-disabled
114  * section without disabling hardirqs.
115  *
116  * On !CONFIG_LOCKDEP they are equivalent to the normal
117  * irq disable/enable methods.
118  */
119 static inline void disable_irq_nosync_lockdep(unsigned int irq)
120 {
121         disable_irq_nosync(irq);
122 #ifdef CONFIG_LOCKDEP
123         local_irq_disable();
124 #endif
125 }
126 
127 static inline void disable_irq_nosync_lockdep_irqsave(unsigned int irq, unsigned long *flags)
128 {
129         disable_irq_nosync(irq);
130 #ifdef CONFIG_LOCKDEP
131         local_irq_save(*flags);
132 #endif
133 }
134 
135 static inline void disable_irq_lockdep(unsigned int irq)
136 {
137         disable_irq(irq);
138 #ifdef CONFIG_LOCKDEP
139         local_irq_disable();
140 #endif
141 }
142 
143 static inline void enable_irq_lockdep(unsigned int irq)
144 {
145 #ifdef CONFIG_LOCKDEP
146         local_irq_enable();
147 #endif
148         enable_irq(irq);
149 }
150 
151 static inline void enable_irq_lockdep_irqrestore(unsigned int irq, unsigned long *flags)
152 {
153 #ifdef CONFIG_LOCKDEP
154         local_irq_restore(*flags);
155 #endif
156         enable_irq(irq);
157 }
158 
159 /* IRQ wakeup (PM) control: */
160 extern int set_irq_wake(unsigned int irq, unsigned int on);
161 
162 static inline int enable_irq_wake(unsigned int irq)
163 {
164         return set_irq_wake(irq, 1);
165 }
166 
167 static inline int disable_irq_wake(unsigned int irq)
168 {
169         return set_irq_wake(irq, 0);
170 }
171 
172 #else /* !CONFIG_GENERIC_HARDIRQS */
173 /*
174  * NOTE: non-genirq architectures, if they want to support the lock
175  * validator need to define the methods below in their asm/irq.h
176  * files, under an #ifdef CONFIG_LOCKDEP section.
177  */
178 #ifndef CONFIG_LOCKDEP
179 #  define disable_irq_nosync_lockdep(irq)       disable_irq_nosync(irq)
180 #  define disable_irq_nosync_lockdep_irqsave(irq, flags) \
181                                                 disable_irq_nosync(irq)
182 #  define disable_irq_lockdep(irq)              disable_irq(irq)
183 #  define enable_irq_lockdep(irq)               enable_irq(irq)
184 #  define enable_irq_lockdep_irqrestore(irq, flags) \
185                                                 enable_irq(irq)
186 # endif
187 
188 static inline int enable_irq_wake(unsigned int irq)
189 {
190         return 0;
191 }
192 
193 static inline int disable_irq_wake(unsigned int irq)
194 {
195         return 0;
196 }
197 #endif /* CONFIG_GENERIC_HARDIRQS */
198 
199 #ifndef __ARCH_SET_SOFTIRQ_PENDING
200 #define set_softirq_pending(x) (local_softirq_pending() = (x))
201 // FIXME: PREEMPT_RT: set_bit()?
202 #define or_softirq_pending(x)  (local_softirq_pending() |= (x))
203 #endif
204 
205 /*
206  * Temporary defines for UP kernels, until all code gets fixed.
207  */
208 #ifndef CONFIG_SMP
209 static inline void __deprecated cli(void)
210 {
211         local_irq_disable();
212 }
213 static inline void __deprecated sti(void)
214 {
215         local_irq_enable();
216 }
217 static inline void __deprecated save_flags(unsigned long *x)
218 {
219         local_save_flags(*x);
220 }
221 #define save_flags(x) save_flags(&x)
222 static inline void __deprecated restore_flags(unsigned long x)
223 {
224         local_irq_restore(x);
225 }
226 
227 static inline void __deprecated save_and_cli(unsigned long *x)
228 {
229         local_irq_save(*x);
230 }
231 #define save_and_cli(x) save_and_cli(&x)
232 #endif /* CONFIG_SMP */
233 
234 /* Some architectures might implement lazy enabling/disabling of
235  * interrupts. In some cases, such as stop_machine, we might want
236  * to ensure that after a local_irq_disable(), interrupts have
237  * really been disabled in hardware. Such architectures need to
238  * implement the following hook.
239  */
240 #ifndef hard_irq_disable
241 #define hard_irq_disable()      do { } while(0)
242 #endif
243 
244 /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
245    frequency threaded job scheduling. For almost all the purposes
246    tasklets are more than enough. F.e. all serial device BHs et
247    al. should be converted to tasklets, not to softirqs.
248  */
249 
250 enum
251 {
252         HI_SOFTIRQ=0,
253         TIMER_SOFTIRQ,
254         NET_TX_SOFTIRQ,
255         NET_RX_SOFTIRQ,
256         BLOCK_SOFTIRQ,
257         TASKLET_SOFTIRQ,
258         SCHED_SOFTIRQ,
259 #ifdef CONFIG_HIGH_RES_TIMERS
260         HRTIMER_SOFTIRQ,
261 #endif
262         RCU_SOFTIRQ,    /* Preferable RCU should always be the last softirq */
263         /* Entries after this are ignored in split softirq mode */
264         MAX_SOFTIRQ,
265 };
266 
267 /* softirq mask and active fields moved to irq_cpustat_t in
268  * asm/hardirq.h to get better cache usage.  KAO
269  */
270 
271 struct softirq_action
272 {
273         void    (*action)(struct softirq_action *);
274         void    *data;
275 };
276 
277 #ifdef CONFIG_PREEMPT_HARDIRQS
278 # define __raise_softirq_irqoff(nr) raise_softirq_irqoff(nr)
279 # define __do_raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
280 #else
281 # define __raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
282 # define __do_raise_softirq_irqoff(nr) __raise_softirq_irqoff(nr)
283 #endif
284 
285 asmlinkage void do_softirq(void);
286 extern void open_softirq(int nr, void (*action)(struct softirq_action*), void *data);
287 extern void softirq_init(void);
288 extern void raise_softirq_irqoff(unsigned int nr);
289 extern void raise_softirq(unsigned int nr);
290 extern void wakeup_irqd(void);
291 
292 #ifdef CONFIG_PREEMPT_SOFTIRQS
293 extern void wait_for_softirq(int softirq);
294 #else
295 # define wait_for_softirq(x) do {} while(0)
296 #endif
297 
298 /* Tasklets --- multithreaded analogue of BHs.
299 
300    Main feature differing them of generic softirqs: tasklet
301    is running only on one CPU simultaneously.
302 
303    Main feature differing them of BHs: different tasklets
304    may be run simultaneously on different CPUs.
305 
306    Properties:
307    * If tasklet_schedule() is called, then tasklet is guaranteed
308      to be executed on some cpu at least once after this.
309    * If the tasklet is already scheduled, but its excecution is still not
310      started, it will be executed only once.
311    * If this tasklet is already running on another CPU, it is rescheduled
312      for later.
313    * Schedule must not be called from the tasklet itself (a lockup occurs)
314    * Tasklet is strictly serialized wrt itself, but not
315      wrt another tasklets. If client needs some intertask synchronization,
316      he makes it with spinlocks.
317  */
318 
319 struct tasklet_struct
320 {
321         struct tasklet_struct *next;
322         unsigned long state;
323         atomic_t count;
324         void (*func)(unsigned long);
325         unsigned long data;
326 };
327 
328 #define DECLARE_TASKLET(name, func, data) \
329 struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }
330 
331 #define DECLARE_TASKLET_DISABLED(name, func, data) \
332 struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
333 
334 
335 enum
336 {
337         TASKLET_STATE_SCHED,    /* Tasklet is scheduled for execution */
338         TASKLET_STATE_RUN,      /* Tasklet is running (SMP only) */
339         TASKLET_STATE_PENDING   /* Tasklet is pending */
340 };
341 
342 #define TASKLET_STATEF_SCHED    (1 << TASKLET_STATE_SCHED)
343 #define TASKLET_STATEF_RUN      (1 << TASKLET_STATE_RUN)
344 #define TASKLET_STATEF_PENDING  (1 << TASKLET_STATE_PENDING)
345 
346 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
347 static inline int tasklet_trylock(struct tasklet_struct *t)
348 {
349         return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
350 }
351 
352 static inline int tasklet_tryunlock(struct tasklet_struct *t)
353 {
354         return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
355 }
356 
357 static inline void tasklet_unlock(struct tasklet_struct *t)
358 {
359         smp_mb__before_clear_bit(); 
360         clear_bit(TASKLET_STATE_RUN, &(t)->state);
361 }
362 
363 extern void tasklet_unlock_wait(struct tasklet_struct *t);
364 
365 #else
366 #define tasklet_trylock(t) 1
367 #define tasklet_tryunlock(t)    1
368 #define tasklet_unlock_wait(t) do { } while (0)
369 #define tasklet_unlock(t) do { } while (0)
370 #endif
371 
372 extern void __tasklet_schedule(struct tasklet_struct *t);
373 
374 static inline void tasklet_schedule(struct tasklet_struct *t)
375 {
376         if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
377                 __tasklet_schedule(t);
378 }
379 
380 extern void __tasklet_hi_schedule(struct tasklet_struct *t);
381 
382 static inline void tasklet_hi_schedule(struct tasklet_struct *t)
383 {
384         if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
385                 __tasklet_hi_schedule(t);
386 }
387 
388 
389 static inline void tasklet_disable_nosync(struct tasklet_struct *t)
390 {
391         atomic_inc(&t->count);
392         smp_mb__after_atomic_inc();
393 }
394 
395 static inline void tasklet_disable(struct tasklet_struct *t)
396 {
397         tasklet_disable_nosync(t);
398         tasklet_unlock_wait(t);
399         smp_mb();
400 }
401 
402 extern  void tasklet_enable(struct tasklet_struct *t);
403 extern  void tasklet_hi_enable(struct tasklet_struct *t);
404 
405 extern void tasklet_kill(struct tasklet_struct *t);
406 extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
407 extern void tasklet_init(struct tasklet_struct *t,
408                          void (*func)(unsigned long), unsigned long data);
409 void takeover_tasklets(unsigned int cpu);
410 
411 /*
412  * Autoprobing for irqs:
413  *
414  * probe_irq_on() and probe_irq_off() provide robust primitives
415  * for accurate IRQ probing during kernel initialization.  They are
416  * reasonably simple to use, are not "fooled" by spurious interrupts,
417  * and, unlike other attempts at IRQ probing, they do not get hung on
418  * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
419  *
420  * For reasonably foolproof probing, use them as follows:
421  *
422  * 1. clear and/or mask the device's internal interrupt.
423  * 2. sti();
424  * 3. irqs = probe_irq_on();      // "take over" all unassigned idle IRQs
425  * 4. enable the device and cause it to trigger an interrupt.
426  * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
427  * 6. irq = probe_irq_off(irqs);  // get IRQ number, 0=none, negative=multiple
428  * 7. service the device to clear its pending interrupt.
429  * 8. loop again if paranoia is required.
430  *
431  * probe_irq_on() returns a mask of allocated irq's.
432  *
433  * probe_irq_off() takes the mask as a parameter,
434  * and returns the irq number which occurred,
435  * or zero if none occurred, or a negative irq number
436  * if more than one irq occurred.
437  */
438 
439 #if defined(CONFIG_GENERIC_HARDIRQS) && !defined(CONFIG_GENERIC_IRQ_PROBE) 
440 static inline unsigned long probe_irq_on(void)
441 {
442         return 0;
443 }
444 static inline int probe_irq_off(unsigned long val)
445 {
446         return 0;
447 }
448 static inline unsigned int probe_irq_mask(unsigned long val)
449 {
450         return 0;
451 }
452 #else
453 extern unsigned long probe_irq_on(void);        /* returns 0 on failure */
454 extern int probe_irq_off(unsigned long);        /* returns 0 or negative on failure */
455 extern unsigned int probe_irq_mask(unsigned long);      /* returns mask of ISA interrupts */
456 #endif
457 
458 #ifdef CONFIG_PROC_FS
459 /* Initialize /proc/irq/ */
460 extern void init_irq_proc(void);
461 #else
462 static inline void init_irq_proc(void)
463 {
464 }
465 #endif
466 
467 int show_interrupts(struct seq_file *p, void *v);
468 
469 #ifdef CONFIG_PREEMPT_RT
470 # define local_irq_disable_nort()       do { } while (0)
471 # define local_irq_enable_nort()        do { } while (0)
472 # define local_irq_enable_rt()          local_irq_enable()
473 # define local_irq_save_nort(flags)     do { local_save_flags(flags); } while (0)
474 # define local_irq_restore_nort(flags)  do { (void)(flags); } while (0)
475 # define spin_lock_nort(lock)           do { } while (0)
476 # define spin_unlock_nort(lock)         do { } while (0)
477 # define spin_lock_bh_nort(lock)        do { } while (0)
478 # define spin_unlock_bh_nort(lock)      do { } while (0)
479 # define spin_lock_rt(lock)             spin_lock(lock)
480 # define spin_unlock_rt(lock)           spin_unlock(lock)
481 # define smp_processor_id_rt(cpu)       (cpu)
482 # define in_atomic_rt()                 (!oops_in_progress && \
483                                           (in_atomic() || irqs_disabled()))
484 # define read_trylock_rt(lock)          ({read_lock(lock); 1; })
485 #else
486 # define local_irq_disable_nort()       local_irq_disable()
487 # define local_irq_enable_nort()        local_irq_enable()
488 # define local_irq_enable_rt()          do { } while (0)
489 # define local_irq_save_nort(flags)     local_irq_save(flags)
490 # define local_irq_restore_nort(flags)  local_irq_restore(flags)
491 # define spin_lock_rt(lock)             do { } while (0)
492 # define spin_unlock_rt(lock)           do { } while (0)
493 # define spin_lock_nort(lock)           spin_lock(lock)
494 # define spin_unlock_nort(lock)         spin_unlock(lock)
495 # define spin_lock_bh_nort(lock)        spin_lock_bh(lock)
496 # define spin_unlock_bh_nort(lock)      spin_unlock_bh(lock)
497 # define smp_processor_id_rt(cpu)       smp_processor_id()
498 # define in_atomic_rt()                 0
499 # define read_trylock_rt(lock)          read_trylock(lock)
500 #endif
501 
502 #endif
503 
  This page was automatically generated by the LXR engine.