interrupt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* interrupt.h */
  2. #ifndef _LINUX_INTERRUPT_H
  3. #define _LINUX_INTERRUPT_H
  4. #include <linux/kernel.h>
  5. #include <linux/linkage.h>
  6. #include <linux/bitops.h>
  7. #include <linux/preempt.h>
  8. #include <linux/cpumask.h>
  9. #include <linux/irqreturn.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/sched.h>
  12. #include <linux/irqflags.h>
  13. #include <linux/bottom_half.h>
  14. #include <linux/device.h>
  15. #include <asm/atomic.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/system.h>
  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. * These flags used only by the kernel as part of the
  35. * irq handling routines.
  36. *
  37. * IRQF_DISABLED - keep irqs disabled when calling the action handler
  38. * IRQF_SAMPLE_RANDOM - irq is used to feed the random generator
  39. * IRQF_SHARED - allow sharing the irq among several devices
  40. * IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
  41. * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
  42. * IRQF_PERCPU - Interrupt is per cpu
  43. * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
  44. */
  45. #define IRQF_DISABLED 0x00000020
  46. #define IRQF_SAMPLE_RANDOM 0x00000040
  47. #define IRQF_SHARED 0x00000080
  48. #define IRQF_PROBE_SHARED 0x00000100
  49. #define IRQF_TIMER 0x00000200
  50. #define IRQF_PERCPU 0x00000400
  51. #define IRQF_NOBALANCING 0x00000800
  52. /*
  53. * Migration helpers. Scheduled for removal in 1/2007
  54. * Do not use for new code !
  55. */
  56. #define SA_INTERRUPT IRQF_DISABLED
  57. #define SA_SAMPLE_RANDOM IRQF_SAMPLE_RANDOM
  58. #define SA_SHIRQ IRQF_SHARED
  59. #define SA_PROBEIRQ IRQF_PROBE_SHARED
  60. #define SA_PERCPU IRQF_PERCPU
  61. #define SA_TRIGGER_LOW IRQF_TRIGGER_LOW
  62. #define SA_TRIGGER_HIGH IRQF_TRIGGER_HIGH
  63. #define SA_TRIGGER_FALLING IRQF_TRIGGER_FALLING
  64. #define SA_TRIGGER_RISING IRQF_TRIGGER_RISING
  65. #define SA_TRIGGER_MASK IRQF_TRIGGER_MASK
  66. typedef irqreturn_t (*irq_handler_t)(int, void *);
  67. struct irqaction {
  68. irq_handler_t handler;
  69. unsigned long flags;
  70. cpumask_t mask;
  71. const char *name;
  72. void *dev_id;
  73. struct irqaction *next;
  74. int irq;
  75. struct proc_dir_entry *dir;
  76. };
  77. extern irqreturn_t no_action(int cpl, void *dev_id);
  78. extern int request_irq(unsigned int, irq_handler_t handler,
  79. unsigned long, const char *, void *);
  80. extern void free_irq(unsigned int, void *);
  81. extern int devm_request_irq(struct device *dev, unsigned int irq,
  82. irq_handler_t handler, unsigned long irqflags,
  83. const char *devname, void *dev_id);
  84. extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
  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()
  101. #endif
  102. #ifdef CONFIG_GENERIC_HARDIRQS
  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. * Special lockdep variants of irq disabling/enabling.
  108. * These should be used for locking constructs that
  109. * know that a particular irq context which is disabled,
  110. * and which is the only irq-context user of a lock,
  111. * that it's safe to take the lock in the irq-disabled
  112. * section without disabling hardirqs.
  113. *
  114. * On !CONFIG_LOCKDEP they are equivalent to the normal
  115. * irq disable/enable methods.
  116. */
  117. static inline void disable_irq_nosync_lockdep(unsigned int irq)
  118. {
  119. disable_irq_nosync(irq);
  120. #ifdef CONFIG_LOCKDEP
  121. local_irq_disable();
  122. #endif
  123. }
  124. static inline void disable_irq_nosync_lockdep_irqsave(unsigned int irq, unsigned long *flags)
  125. {
  126. disable_irq_nosync(irq);
  127. #ifdef CONFIG_LOCKDEP
  128. local_irq_save(*flags);
  129. #endif
  130. }
  131. static inline void disable_irq_lockdep(unsigned int irq)
  132. {
  133. disable_irq(irq);
  134. #ifdef CONFIG_LOCKDEP
  135. local_irq_disable();
  136. #endif
  137. }
  138. static inline void enable_irq_lockdep(unsigned int irq)
  139. {
  140. #ifdef CONFIG_LOCKDEP
  141. local_irq_enable();
  142. #endif
  143. enable_irq(irq);
  144. }
  145. static inline void enable_irq_lockdep_irqrestore(unsigned int irq, unsigned long *flags)
  146. {
  147. #ifdef CONFIG_LOCKDEP
  148. local_irq_restore(*flags);
  149. #endif
  150. enable_irq(irq);
  151. }
  152. /* IRQ wakeup (PM) control: */
  153. extern int set_irq_wake(unsigned int irq, unsigned int on);
  154. static inline int enable_irq_wake(unsigned int irq)
  155. {
  156. return set_irq_wake(irq, 1);
  157. }
  158. static inline int disable_irq_wake(unsigned int irq)
  159. {
  160. return set_irq_wake(irq, 0);
  161. }
  162. #else /* !CONFIG_GENERIC_HARDIRQS */
  163. /*
  164. * NOTE: non-genirq architectures, if they want to support the lock
  165. * validator need to define the methods below in their asm/irq.h
  166. * files, under an #ifdef CONFIG_LOCKDEP section.
  167. */
  168. # ifndef CONFIG_LOCKDEP
  169. # define disable_irq_nosync_lockdep(irq) disable_irq_nosync(irq)
  170. # define disable_irq_lockdep(irq) disable_irq(irq)
  171. # define enable_irq_lockdep(irq) enable_irq(irq)
  172. # endif
  173. #endif /* CONFIG_GENERIC_HARDIRQS */
  174. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  175. #define set_softirq_pending(x) (local_softirq_pending() = (x))
  176. #define or_softirq_pending(x) (local_softirq_pending() |= (x))
  177. #endif
  178. /*
  179. * Temporary defines for UP kernels, until all code gets fixed.
  180. */
  181. #ifndef CONFIG_SMP
  182. static inline void __deprecated cli(void)
  183. {
  184. local_irq_disable();
  185. }
  186. static inline void __deprecated sti(void)
  187. {
  188. local_irq_enable();
  189. }
  190. static inline void __deprecated save_flags(unsigned long *x)
  191. {
  192. local_save_flags(*x);
  193. }
  194. #define save_flags(x) save_flags(&x)
  195. static inline void __deprecated restore_flags(unsigned long x)
  196. {
  197. local_irq_restore(x);
  198. }
  199. static inline void __deprecated save_and_cli(unsigned long *x)
  200. {
  201. local_irq_save(*x);
  202. }
  203. #define save_and_cli(x) save_and_cli(&x)
  204. #endif /* CONFIG_SMP */
  205. /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
  206. frequency threaded job scheduling. For almost all the purposes
  207. tasklets are more than enough. F.e. all serial device BHs et
  208. al. should be converted to tasklets, not to softirqs.
  209. */
  210. enum
  211. {
  212. HI_SOFTIRQ=0,
  213. TIMER_SOFTIRQ,
  214. NET_TX_SOFTIRQ,
  215. NET_RX_SOFTIRQ,
  216. BLOCK_SOFTIRQ,
  217. TASKLET_SOFTIRQ,
  218. SCHED_SOFTIRQ,
  219. #ifdef CONFIG_HIGH_RES_TIMERS
  220. HRTIMER_SOFTIRQ,
  221. #endif
  222. };
  223. /* softirq mask and active fields moved to irq_cpustat_t in
  224. * asm/hardirq.h to get better cache usage. KAO
  225. */
  226. struct softirq_action
  227. {
  228. void (*action)(struct softirq_action *);
  229. void *data;
  230. };
  231. asmlinkage void do_softirq(void);
  232. extern void open_softirq(int nr, void (*action)(struct softirq_action*), void *data);
  233. extern void softirq_init(void);
  234. #define __raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
  235. extern void FASTCALL(raise_softirq_irqoff(unsigned int nr));
  236. extern void FASTCALL(raise_softirq(unsigned int nr));
  237. /* Tasklets --- multithreaded analogue of BHs.
  238. Main feature differing them of generic softirqs: tasklet
  239. is running only on one CPU simultaneously.
  240. Main feature differing them of BHs: different tasklets
  241. may be run simultaneously on different CPUs.
  242. Properties:
  243. * If tasklet_schedule() is called, then tasklet is guaranteed
  244. to be executed on some cpu at least once after this.
  245. * If the tasklet is already scheduled, but its excecution is still not
  246. started, it will be executed only once.
  247. * If this tasklet is already running on another CPU (or schedule is called
  248. from tasklet itself), it is rescheduled for later.
  249. * Tasklet is strictly serialized wrt itself, but not
  250. wrt another tasklets. If client needs some intertask synchronization,
  251. he makes it with spinlocks.
  252. */
  253. struct tasklet_struct
  254. {
  255. struct tasklet_struct *next;
  256. unsigned long state;
  257. atomic_t count;
  258. void (*func)(unsigned long);
  259. unsigned long data;
  260. };
  261. #define DECLARE_TASKLET(name, func, data) \
  262. struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }
  263. #define DECLARE_TASKLET_DISABLED(name, func, data) \
  264. struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
  265. enum
  266. {
  267. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  268. TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  269. };
  270. #ifdef CONFIG_SMP
  271. static inline int tasklet_trylock(struct tasklet_struct *t)
  272. {
  273. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  274. }
  275. static inline void tasklet_unlock(struct tasklet_struct *t)
  276. {
  277. smp_mb__before_clear_bit();
  278. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  279. }
  280. static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  281. {
  282. while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  283. }
  284. #else
  285. #define tasklet_trylock(t) 1
  286. #define tasklet_unlock_wait(t) do { } while (0)
  287. #define tasklet_unlock(t) do { } while (0)
  288. #endif
  289. extern void FASTCALL(__tasklet_schedule(struct tasklet_struct *t));
  290. static inline void tasklet_schedule(struct tasklet_struct *t)
  291. {
  292. if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
  293. __tasklet_schedule(t);
  294. }
  295. extern void FASTCALL(__tasklet_hi_schedule(struct tasklet_struct *t));
  296. static inline void tasklet_hi_schedule(struct tasklet_struct *t)
  297. {
  298. if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
  299. __tasklet_hi_schedule(t);
  300. }
  301. static inline void tasklet_disable_nosync(struct tasklet_struct *t)
  302. {
  303. atomic_inc(&t->count);
  304. smp_mb__after_atomic_inc();
  305. }
  306. static inline void tasklet_disable(struct tasklet_struct *t)
  307. {
  308. tasklet_disable_nosync(t);
  309. tasklet_unlock_wait(t);
  310. smp_mb();
  311. }
  312. static inline void tasklet_enable(struct tasklet_struct *t)
  313. {
  314. smp_mb__before_atomic_dec();
  315. atomic_dec(&t->count);
  316. }
  317. static inline void tasklet_hi_enable(struct tasklet_struct *t)
  318. {
  319. smp_mb__before_atomic_dec();
  320. atomic_dec(&t->count);
  321. }
  322. extern void tasklet_kill(struct tasklet_struct *t);
  323. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  324. extern void tasklet_init(struct tasklet_struct *t,
  325. void (*func)(unsigned long), unsigned long data);
  326. /*
  327. * Autoprobing for irqs:
  328. *
  329. * probe_irq_on() and probe_irq_off() provide robust primitives
  330. * for accurate IRQ probing during kernel initialization. They are
  331. * reasonably simple to use, are not "fooled" by spurious interrupts,
  332. * and, unlike other attempts at IRQ probing, they do not get hung on
  333. * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
  334. *
  335. * For reasonably foolproof probing, use them as follows:
  336. *
  337. * 1. clear and/or mask the device's internal interrupt.
  338. * 2. sti();
  339. * 3. irqs = probe_irq_on(); // "take over" all unassigned idle IRQs
  340. * 4. enable the device and cause it to trigger an interrupt.
  341. * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
  342. * 6. irq = probe_irq_off(irqs); // get IRQ number, 0=none, negative=multiple
  343. * 7. service the device to clear its pending interrupt.
  344. * 8. loop again if paranoia is required.
  345. *
  346. * probe_irq_on() returns a mask of allocated irq's.
  347. *
  348. * probe_irq_off() takes the mask as a parameter,
  349. * and returns the irq number which occurred,
  350. * or zero if none occurred, or a negative irq number
  351. * if more than one irq occurred.
  352. */
  353. #if defined(CONFIG_GENERIC_HARDIRQS) && !defined(CONFIG_GENERIC_IRQ_PROBE)
  354. static inline unsigned long probe_irq_on(void)
  355. {
  356. return 0;
  357. }
  358. static inline int probe_irq_off(unsigned long val)
  359. {
  360. return 0;
  361. }
  362. static inline unsigned int probe_irq_mask(unsigned long val)
  363. {
  364. return 0;
  365. }
  366. #else
  367. extern unsigned long probe_irq_on(void); /* returns 0 on failure */
  368. extern int probe_irq_off(unsigned long); /* returns 0 or negative on failure */
  369. extern unsigned int probe_irq_mask(unsigned long); /* returns mask of ISA interrupts */
  370. #endif
  371. #ifdef CONFIG_PROC_FS
  372. /* Initialize /proc/irq/ */
  373. extern void init_irq_proc(void);
  374. #else
  375. static inline void init_irq_proc(void)
  376. {
  377. }
  378. #endif
  379. #endif