interrupt.h 13 KB

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