interrupt.h 15 KB

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