interrupt.h 13 KB

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