interrupt.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* interrupt.h */
  3. #ifndef _LINUX_INTERRUPT_H
  4. #define _LINUX_INTERRUPT_H
  5. #include <linux/kernel.h>
  6. #include <linux/bitops.h>
  7. #include <linux/cpumask.h>
  8. #include <linux/irqreturn.h>
  9. #include <linux/irqnr.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/irqflags.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/kref.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/jump_label.h>
  16. #include <linux/atomic.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/irq.h>
  19. #include <asm/sections.h>
  20. /*
  21. * These correspond to the IORESOURCE_IRQ_* defines in
  22. * linux/ioport.h to select the interrupt line behaviour. When
  23. * requesting an interrupt without specifying a IRQF_TRIGGER, the
  24. * setting should be assumed to be "as already configured", which
  25. * may be as per machine or firmware initialisation.
  26. */
  27. #define IRQF_TRIGGER_NONE 0x00000000
  28. #define IRQF_TRIGGER_RISING 0x00000001
  29. #define IRQF_TRIGGER_FALLING 0x00000002
  30. #define IRQF_TRIGGER_HIGH 0x00000004
  31. #define IRQF_TRIGGER_LOW 0x00000008
  32. #define IRQF_TRIGGER_MASK (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
  33. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
  34. #define IRQF_TRIGGER_PROBE 0x00000010
  35. /*
  36. * These flags used only by the kernel as part of the
  37. * irq handling routines.
  38. *
  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. * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
  45. * registered first in a shared interrupt is considered for
  46. * performance reasons)
  47. * IRQF_ONESHOT - Interrupt is not reenabled after the hardirq handler finished.
  48. * Used by threaded interrupts which need to keep the
  49. * irq line disabled until the threaded handler has been run.
  50. * IRQF_NO_SUSPEND - Do not disable this IRQ during suspend. Does not guarantee
  51. * that this interrupt will wake the system from a suspended
  52. * state. See Documentation/power/suspend-and-interrupts.rst
  53. * IRQF_FORCE_RESUME - Force enable it on resume even if IRQF_NO_SUSPEND is set
  54. * IRQF_NO_THREAD - Interrupt cannot be threaded
  55. * IRQF_EARLY_RESUME - Resume IRQ early during syscore instead of at device
  56. * resume time.
  57. * IRQF_COND_SUSPEND - If the IRQ is shared with a NO_SUSPEND user, execute this
  58. * interrupt handler after suspending interrupts. For system
  59. * wakeup devices users need to implement wakeup detection in
  60. * their interrupt handlers.
  61. * IRQF_NO_AUTOEN - Don't enable IRQ or NMI automatically when users request it.
  62. * Users will enable it explicitly by enable_irq() or enable_nmi()
  63. * later.
  64. * IRQF_NO_DEBUG - Exclude from runnaway detection for IPI and similar handlers,
  65. * depends on IRQF_PERCPU.
  66. */
  67. #define IRQF_SHARED 0x00000080
  68. #define IRQF_PROBE_SHARED 0x00000100
  69. #define __IRQF_TIMER 0x00000200
  70. #define IRQF_PERCPU 0x00000400
  71. #define IRQF_NOBALANCING 0x00000800
  72. #define IRQF_IRQPOLL 0x00001000
  73. #define IRQF_ONESHOT 0x00002000
  74. #define IRQF_NO_SUSPEND 0x00004000
  75. #define IRQF_FORCE_RESUME 0x00008000
  76. #define IRQF_NO_THREAD 0x00010000
  77. #define IRQF_EARLY_RESUME 0x00020000
  78. #define IRQF_COND_SUSPEND 0x00040000
  79. #define IRQF_NO_AUTOEN 0x00080000
  80. #define IRQF_NO_DEBUG 0x00100000
  81. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  82. /*
  83. * These values can be returned by request_any_context_irq() and
  84. * describe the context the interrupt will be run in.
  85. *
  86. * IRQC_IS_HARDIRQ - interrupt runs in hardirq context
  87. * IRQC_IS_NESTED - interrupt runs in a nested threaded context
  88. */
  89. enum {
  90. IRQC_IS_HARDIRQ = 0,
  91. IRQC_IS_NESTED,
  92. };
  93. typedef irqreturn_t (*irq_handler_t)(int, void *);
  94. /**
  95. * struct irqaction - per interrupt action descriptor
  96. * @handler: interrupt handler function
  97. * @name: name of the device
  98. * @dev_id: cookie to identify the device
  99. * @percpu_dev_id: cookie to identify the device
  100. * @next: pointer to the next irqaction for shared interrupts
  101. * @irq: interrupt number
  102. * @flags: flags (see IRQF_* above)
  103. * @thread_fn: interrupt handler function for threaded interrupts
  104. * @thread: thread pointer for threaded interrupts
  105. * @secondary: pointer to secondary irqaction (force threading)
  106. * @thread_flags: flags related to @thread
  107. * @thread_mask: bitmask for keeping track of @thread activity
  108. * @dir: pointer to the proc/irq/NN/name entry
  109. */
  110. struct irqaction {
  111. irq_handler_t handler;
  112. void *dev_id;
  113. void __percpu *percpu_dev_id;
  114. struct irqaction *next;
  115. irq_handler_t thread_fn;
  116. struct task_struct *thread;
  117. struct irqaction *secondary;
  118. unsigned int irq;
  119. unsigned int flags;
  120. unsigned long thread_flags;
  121. unsigned long thread_mask;
  122. const char *name;
  123. struct proc_dir_entry *dir;
  124. } ____cacheline_internodealigned_in_smp;
  125. extern irqreturn_t no_action(int cpl, void *dev_id);
  126. /*
  127. * If a (PCI) device interrupt is not connected we set dev->irq to
  128. * IRQ_NOTCONNECTED. This causes request_irq() to fail with -ENOTCONN, so we
  129. * can distingiush that case from other error returns.
  130. *
  131. * 0x80000000 is guaranteed to be outside the available range of interrupts
  132. * and easy to distinguish from other possible incorrect values.
  133. */
  134. #define IRQ_NOTCONNECTED (1U << 31)
  135. extern int __must_check
  136. request_threaded_irq(unsigned int irq, irq_handler_t handler,
  137. irq_handler_t thread_fn,
  138. unsigned long flags, const char *name, void *dev);
  139. /**
  140. * request_irq - Add a handler for an interrupt line
  141. * @irq: The interrupt line to allocate
  142. * @handler: Function to be called when the IRQ occurs.
  143. * Primary handler for threaded interrupts
  144. * If NULL, the default primary handler is installed
  145. * @flags: Handling flags
  146. * @name: Name of the device generating this interrupt
  147. * @dev: A cookie passed to the handler function
  148. *
  149. * This call allocates an interrupt and establishes a handler; see
  150. * the documentation for request_threaded_irq() for details.
  151. */
  152. static inline int __must_check
  153. request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
  154. const char *name, void *dev)
  155. {
  156. return request_threaded_irq(irq, handler, NULL, flags, name, dev);
  157. }
  158. extern int __must_check
  159. request_any_context_irq(unsigned int irq, irq_handler_t handler,
  160. unsigned long flags, const char *name, void *dev_id);
  161. extern int __must_check
  162. __request_percpu_irq(unsigned int irq, irq_handler_t handler,
  163. unsigned long flags, const char *devname,
  164. void __percpu *percpu_dev_id);
  165. extern int __must_check
  166. request_nmi(unsigned int irq, irq_handler_t handler, unsigned long flags,
  167. const char *name, void *dev);
  168. static inline int __must_check
  169. request_percpu_irq(unsigned int irq, irq_handler_t handler,
  170. const char *devname, void __percpu *percpu_dev_id)
  171. {
  172. return __request_percpu_irq(irq, handler, 0,
  173. devname, percpu_dev_id);
  174. }
  175. extern int __must_check
  176. request_percpu_nmi(unsigned int irq, irq_handler_t handler,
  177. const char *devname, void __percpu *dev);
  178. extern const void *free_irq(unsigned int, void *);
  179. extern void free_percpu_irq(unsigned int, void __percpu *);
  180. extern const void *free_nmi(unsigned int irq, void *dev_id);
  181. extern void free_percpu_nmi(unsigned int irq, void __percpu *percpu_dev_id);
  182. struct device;
  183. extern int __must_check
  184. devm_request_threaded_irq(struct device *dev, unsigned int irq,
  185. irq_handler_t handler, irq_handler_t thread_fn,
  186. unsigned long irqflags, const char *devname,
  187. void *dev_id);
  188. static inline int __must_check
  189. devm_request_irq(struct device *dev, unsigned int irq, irq_handler_t handler,
  190. unsigned long irqflags, const char *devname, void *dev_id)
  191. {
  192. return devm_request_threaded_irq(dev, irq, handler, NULL, irqflags,
  193. devname, dev_id);
  194. }
  195. extern int __must_check
  196. devm_request_any_context_irq(struct device *dev, unsigned int irq,
  197. irq_handler_t handler, unsigned long irqflags,
  198. const char *devname, void *dev_id);
  199. extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
  200. bool irq_has_action(unsigned int irq);
  201. extern void disable_irq_nosync(unsigned int irq);
  202. extern bool disable_hardirq(unsigned int irq);
  203. extern void disable_irq(unsigned int irq);
  204. extern void disable_percpu_irq(unsigned int irq);
  205. extern void enable_irq(unsigned int irq);
  206. extern void enable_percpu_irq(unsigned int irq, unsigned int type);
  207. extern bool irq_percpu_is_enabled(unsigned int irq);
  208. extern void irq_wake_thread(unsigned int irq, void *dev_id);
  209. extern void disable_nmi_nosync(unsigned int irq);
  210. extern void disable_percpu_nmi(unsigned int irq);
  211. extern void enable_nmi(unsigned int irq);
  212. extern void enable_percpu_nmi(unsigned int irq, unsigned int type);
  213. extern int prepare_percpu_nmi(unsigned int irq);
  214. extern void teardown_percpu_nmi(unsigned int irq);
  215. extern int irq_inject_interrupt(unsigned int irq);
  216. /* The following three functions are for the core kernel use only. */
  217. extern void suspend_device_irqs(void);
  218. extern void resume_device_irqs(void);
  219. extern void rearm_wake_irq(unsigned int irq);
  220. /**
  221. * struct irq_affinity_notify - context for notification of IRQ affinity changes
  222. * @irq: Interrupt to which notification applies
  223. * @kref: Reference count, for internal use
  224. * @work: Work item, for internal use
  225. * @notify: Function to be called on change. This will be
  226. * called in process context.
  227. * @release: Function to be called on release. This will be
  228. * called in process context. Once registered, the
  229. * structure must only be freed when this function is
  230. * called or later.
  231. */
  232. struct irq_affinity_notify {
  233. unsigned int irq;
  234. struct kref kref;
  235. struct work_struct work;
  236. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  237. void (*release)(struct kref *ref);
  238. };
  239. #define IRQ_AFFINITY_MAX_SETS 4
  240. /**
  241. * struct irq_affinity - Description for automatic irq affinity assignements
  242. * @pre_vectors: Don't apply affinity to @pre_vectors at beginning of
  243. * the MSI(-X) vector space
  244. * @post_vectors: Don't apply affinity to @post_vectors at end of
  245. * the MSI(-X) vector space
  246. * @nr_sets: The number of interrupt sets for which affinity
  247. * spreading is required
  248. * @set_size: Array holding the size of each interrupt set
  249. * @calc_sets: Callback for calculating the number and size
  250. * of interrupt sets
  251. * @priv: Private data for usage by @calc_sets, usually a
  252. * pointer to driver/device specific data.
  253. */
  254. struct irq_affinity {
  255. unsigned int pre_vectors;
  256. unsigned int post_vectors;
  257. unsigned int nr_sets;
  258. unsigned int set_size[IRQ_AFFINITY_MAX_SETS];
  259. void (*calc_sets)(struct irq_affinity *, unsigned int nvecs);
  260. void *priv;
  261. };
  262. /**
  263. * struct irq_affinity_desc - Interrupt affinity descriptor
  264. * @mask: cpumask to hold the affinity assignment
  265. * @is_managed: 1 if the interrupt is managed internally
  266. */
  267. struct irq_affinity_desc {
  268. struct cpumask mask;
  269. unsigned int is_managed : 1;
  270. };
  271. #if defined(CONFIG_SMP)
  272. extern cpumask_var_t irq_default_affinity;
  273. extern int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask);
  274. extern int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask);
  275. extern int irq_can_set_affinity(unsigned int irq);
  276. extern int irq_select_affinity(unsigned int irq);
  277. extern int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m,
  278. bool setaffinity);
  279. /**
  280. * irq_update_affinity_hint - Update the affinity hint
  281. * @irq: Interrupt to update
  282. * @m: cpumask pointer (NULL to clear the hint)
  283. *
  284. * Updates the affinity hint, but does not change the affinity of the interrupt.
  285. */
  286. static inline int
  287. irq_update_affinity_hint(unsigned int irq, const struct cpumask *m)
  288. {
  289. return __irq_apply_affinity_hint(irq, m, false);
  290. }
  291. /**
  292. * irq_set_affinity_and_hint - Update the affinity hint and apply the provided
  293. * cpumask to the interrupt
  294. * @irq: Interrupt to update
  295. * @m: cpumask pointer (NULL to clear the hint)
  296. *
  297. * Updates the affinity hint and if @m is not NULL it applies it as the
  298. * affinity of that interrupt.
  299. */
  300. static inline int
  301. irq_set_affinity_and_hint(unsigned int irq, const struct cpumask *m)
  302. {
  303. return __irq_apply_affinity_hint(irq, m, true);
  304. }
  305. /*
  306. * Deprecated. Use irq_update_affinity_hint() or irq_set_affinity_and_hint()
  307. * instead.
  308. */
  309. static inline int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  310. {
  311. return irq_set_affinity_and_hint(irq, m);
  312. }
  313. extern int irq_update_affinity_desc(unsigned int irq,
  314. struct irq_affinity_desc *affinity);
  315. extern int
  316. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
  317. struct irq_affinity_desc *
  318. irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd);
  319. unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
  320. const struct irq_affinity *affd);
  321. #else /* CONFIG_SMP */
  322. static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
  323. {
  324. return -EINVAL;
  325. }
  326. static inline int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
  327. {
  328. return 0;
  329. }
  330. static inline int irq_can_set_affinity(unsigned int irq)
  331. {
  332. return 0;
  333. }
  334. static inline int irq_select_affinity(unsigned int irq) { return 0; }
  335. static inline int irq_update_affinity_hint(unsigned int irq,
  336. const struct cpumask *m)
  337. {
  338. return -EINVAL;
  339. }
  340. static inline int irq_set_affinity_and_hint(unsigned int irq,
  341. const struct cpumask *m)
  342. {
  343. return -EINVAL;
  344. }
  345. static inline int irq_set_affinity_hint(unsigned int irq,
  346. const struct cpumask *m)
  347. {
  348. return -EINVAL;
  349. }
  350. static inline int irq_update_affinity_desc(unsigned int irq,
  351. struct irq_affinity_desc *affinity)
  352. {
  353. return -EINVAL;
  354. }
  355. static inline int
  356. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  357. {
  358. return 0;
  359. }
  360. static inline struct irq_affinity_desc *
  361. irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd)
  362. {
  363. return NULL;
  364. }
  365. static inline unsigned int
  366. irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
  367. const struct irq_affinity *affd)
  368. {
  369. return maxvec;
  370. }
  371. #endif /* CONFIG_SMP */
  372. /*
  373. * Special lockdep variants of irq disabling/enabling.
  374. * These should be used for locking constructs that
  375. * know that a particular irq context which is disabled,
  376. * and which is the only irq-context user of a lock,
  377. * that it's safe to take the lock in the irq-disabled
  378. * section without disabling hardirqs.
  379. *
  380. * On !CONFIG_LOCKDEP they are equivalent to the normal
  381. * irq disable/enable methods.
  382. */
  383. static inline void disable_irq_nosync_lockdep(unsigned int irq)
  384. {
  385. disable_irq_nosync(irq);
  386. #ifdef CONFIG_LOCKDEP
  387. local_irq_disable();
  388. #endif
  389. }
  390. static inline void disable_irq_nosync_lockdep_irqsave(unsigned int irq, unsigned long *flags)
  391. {
  392. disable_irq_nosync(irq);
  393. #ifdef CONFIG_LOCKDEP
  394. local_irq_save(*flags);
  395. #endif
  396. }
  397. static inline void disable_irq_lockdep(unsigned int irq)
  398. {
  399. disable_irq(irq);
  400. #ifdef CONFIG_LOCKDEP
  401. local_irq_disable();
  402. #endif
  403. }
  404. static inline void enable_irq_lockdep(unsigned int irq)
  405. {
  406. #ifdef CONFIG_LOCKDEP
  407. local_irq_enable();
  408. #endif
  409. enable_irq(irq);
  410. }
  411. static inline void enable_irq_lockdep_irqrestore(unsigned int irq, unsigned long *flags)
  412. {
  413. #ifdef CONFIG_LOCKDEP
  414. local_irq_restore(*flags);
  415. #endif
  416. enable_irq(irq);
  417. }
  418. /* IRQ wakeup (PM) control: */
  419. extern int irq_set_irq_wake(unsigned int irq, unsigned int on);
  420. static inline int enable_irq_wake(unsigned int irq)
  421. {
  422. return irq_set_irq_wake(irq, 1);
  423. }
  424. static inline int disable_irq_wake(unsigned int irq)
  425. {
  426. return irq_set_irq_wake(irq, 0);
  427. }
  428. /*
  429. * irq_get_irqchip_state/irq_set_irqchip_state specific flags
  430. */
  431. enum irqchip_irq_state {
  432. IRQCHIP_STATE_PENDING, /* Is interrupt pending? */
  433. IRQCHIP_STATE_ACTIVE, /* Is interrupt in progress? */
  434. IRQCHIP_STATE_MASKED, /* Is interrupt masked? */
  435. IRQCHIP_STATE_LINE_LEVEL, /* Is IRQ line high? */
  436. };
  437. extern int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  438. bool *state);
  439. extern int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  440. bool state);
  441. #ifdef CONFIG_IRQ_FORCED_THREADING
  442. # ifdef CONFIG_PREEMPT_RT
  443. # define force_irqthreads() (true)
  444. # else
  445. DECLARE_STATIC_KEY_FALSE(force_irqthreads_key);
  446. # define force_irqthreads() (static_branch_unlikely(&force_irqthreads_key))
  447. # endif
  448. #else
  449. #define force_irqthreads() (false)
  450. #endif
  451. #ifndef local_softirq_pending
  452. #ifndef local_softirq_pending_ref
  453. #define local_softirq_pending_ref irq_stat.__softirq_pending
  454. #endif
  455. #define local_softirq_pending() (__this_cpu_read(local_softirq_pending_ref))
  456. #define set_softirq_pending(x) (__this_cpu_write(local_softirq_pending_ref, (x)))
  457. #define or_softirq_pending(x) (__this_cpu_or(local_softirq_pending_ref, (x)))
  458. /**
  459. * __cpu_softirq_pending() - Checks to see if softirq is pending on a cpu
  460. *
  461. * This helper is inherently racy, as we're accessing per-cpu data w/o locks.
  462. * But peeking at the flag can still be useful when deciding where to place a
  463. * task.
  464. */
  465. static inline u32 __cpu_softirq_pending(int cpu)
  466. {
  467. return (u32)per_cpu(local_softirq_pending_ref, cpu);
  468. }
  469. #endif /* local_softirq_pending */
  470. /* Some architectures might implement lazy enabling/disabling of
  471. * interrupts. In some cases, such as stop_machine, we might want
  472. * to ensure that after a local_irq_disable(), interrupts have
  473. * really been disabled in hardware. Such architectures need to
  474. * implement the following hook.
  475. */
  476. #ifndef hard_irq_disable
  477. #define hard_irq_disable() do { } while(0)
  478. #endif
  479. /* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
  480. frequency threaded job scheduling. For almost all the purposes
  481. tasklets are more than enough. F.e. all serial device BHs et
  482. al. should be converted to tasklets, not to softirqs.
  483. */
  484. enum
  485. {
  486. HI_SOFTIRQ=0,
  487. TIMER_SOFTIRQ,
  488. NET_TX_SOFTIRQ,
  489. NET_RX_SOFTIRQ,
  490. BLOCK_SOFTIRQ,
  491. IRQ_POLL_SOFTIRQ,
  492. TASKLET_SOFTIRQ,
  493. SCHED_SOFTIRQ,
  494. HRTIMER_SOFTIRQ,
  495. RCU_SOFTIRQ, /* Preferable RCU should always be the last softirq */
  496. NR_SOFTIRQS
  497. };
  498. /*
  499. * The following vectors can be safely ignored after ksoftirqd is parked:
  500. *
  501. * _ RCU:
  502. * 1) rcutree_migrate_callbacks() migrates the queue.
  503. * 2) rcu_report_dead() reports the final quiescent states.
  504. *
  505. * _ IRQ_POLL: irq_poll_cpu_dead() migrates the queue
  506. *
  507. * _ (HR)TIMER_SOFTIRQ: (hr)timers_dead_cpu() migrates the queue
  508. */
  509. #define SOFTIRQ_HOTPLUG_SAFE_MASK (BIT(TIMER_SOFTIRQ) | BIT(IRQ_POLL_SOFTIRQ) |\
  510. BIT(HRTIMER_SOFTIRQ) | BIT(RCU_SOFTIRQ))
  511. /* Softirq's where the handling might be long: */
  512. #define LONG_SOFTIRQ_MASK (BIT(NET_TX_SOFTIRQ) | \
  513. BIT(NET_RX_SOFTIRQ) | \
  514. BIT(BLOCK_SOFTIRQ) | \
  515. BIT(IRQ_POLL_SOFTIRQ))
  516. /* map softirq index to softirq name. update 'softirq_to_name' in
  517. * kernel/softirq.c when adding a new softirq.
  518. */
  519. extern const char * const softirq_to_name[NR_SOFTIRQS];
  520. /* softirq mask and active fields moved to irq_cpustat_t in
  521. * asm/hardirq.h to get better cache usage. KAO
  522. */
  523. struct softirq_action
  524. {
  525. void (*action)(struct softirq_action *);
  526. };
  527. asmlinkage void do_softirq(void);
  528. asmlinkage void __do_softirq(void);
  529. #ifdef CONFIG_PREEMPT_RT
  530. extern void do_softirq_post_smp_call_flush(unsigned int was_pending);
  531. #else
  532. static inline void do_softirq_post_smp_call_flush(unsigned int unused)
  533. {
  534. do_softirq();
  535. }
  536. #endif
  537. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  538. extern void softirq_init(void);
  539. extern void __raise_softirq_irqoff(unsigned int nr);
  540. extern void raise_softirq_irqoff(unsigned int nr);
  541. extern void raise_softirq(unsigned int nr);
  542. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  543. #ifdef CONFIG_RT_SOFTIRQ_AWARE_SCHED
  544. DECLARE_PER_CPU(u32, active_softirqs);
  545. #endif
  546. static inline struct task_struct *this_cpu_ksoftirqd(void)
  547. {
  548. return this_cpu_read(ksoftirqd);
  549. }
  550. /* Tasklets --- multithreaded analogue of BHs.
  551. This API is deprecated. Please consider using threaded IRQs instead:
  552. https://lore.kernel.org/lkml/[email protected]
  553. Main feature differing them of generic softirqs: tasklet
  554. is running only on one CPU simultaneously.
  555. Main feature differing them of BHs: different tasklets
  556. may be run simultaneously on different CPUs.
  557. Properties:
  558. * If tasklet_schedule() is called, then tasklet is guaranteed
  559. to be executed on some cpu at least once after this.
  560. * If the tasklet is already scheduled, but its execution is still not
  561. started, it will be executed only once.
  562. * If this tasklet is already running on another CPU (or schedule is called
  563. from tasklet itself), it is rescheduled for later.
  564. * Tasklet is strictly serialized wrt itself, but not
  565. wrt another tasklets. If client needs some intertask synchronization,
  566. he makes it with spinlocks.
  567. */
  568. struct tasklet_struct
  569. {
  570. struct tasklet_struct *next;
  571. unsigned long state;
  572. atomic_t count;
  573. bool use_callback;
  574. union {
  575. void (*func)(unsigned long data);
  576. void (*callback)(struct tasklet_struct *t);
  577. };
  578. unsigned long data;
  579. };
  580. #define DECLARE_TASKLET(name, _callback) \
  581. struct tasklet_struct name = { \
  582. .count = ATOMIC_INIT(0), \
  583. .callback = _callback, \
  584. .use_callback = true, \
  585. }
  586. #define DECLARE_TASKLET_DISABLED(name, _callback) \
  587. struct tasklet_struct name = { \
  588. .count = ATOMIC_INIT(1), \
  589. .callback = _callback, \
  590. .use_callback = true, \
  591. }
  592. #define from_tasklet(var, callback_tasklet, tasklet_fieldname) \
  593. container_of(callback_tasklet, typeof(*var), tasklet_fieldname)
  594. #define DECLARE_TASKLET_OLD(name, _func) \
  595. struct tasklet_struct name = { \
  596. .count = ATOMIC_INIT(0), \
  597. .func = _func, \
  598. }
  599. #define DECLARE_TASKLET_DISABLED_OLD(name, _func) \
  600. struct tasklet_struct name = { \
  601. .count = ATOMIC_INIT(1), \
  602. .func = _func, \
  603. }
  604. enum
  605. {
  606. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  607. TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  608. };
  609. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
  610. static inline int tasklet_trylock(struct tasklet_struct *t)
  611. {
  612. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  613. }
  614. void tasklet_unlock(struct tasklet_struct *t);
  615. void tasklet_unlock_wait(struct tasklet_struct *t);
  616. void tasklet_unlock_spin_wait(struct tasklet_struct *t);
  617. #else
  618. static inline int tasklet_trylock(struct tasklet_struct *t) { return 1; }
  619. static inline void tasklet_unlock(struct tasklet_struct *t) { }
  620. static inline void tasklet_unlock_wait(struct tasklet_struct *t) { }
  621. static inline void tasklet_unlock_spin_wait(struct tasklet_struct *t) { }
  622. #endif
  623. extern void __tasklet_schedule(struct tasklet_struct *t);
  624. static inline void tasklet_schedule(struct tasklet_struct *t)
  625. {
  626. if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
  627. __tasklet_schedule(t);
  628. }
  629. extern void __tasklet_hi_schedule(struct tasklet_struct *t);
  630. static inline void tasklet_hi_schedule(struct tasklet_struct *t)
  631. {
  632. if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
  633. __tasklet_hi_schedule(t);
  634. }
  635. static inline void tasklet_disable_nosync(struct tasklet_struct *t)
  636. {
  637. atomic_inc(&t->count);
  638. smp_mb__after_atomic();
  639. }
  640. /*
  641. * Do not use in new code. Disabling tasklets from atomic contexts is
  642. * error prone and should be avoided.
  643. */
  644. static inline void tasklet_disable_in_atomic(struct tasklet_struct *t)
  645. {
  646. tasklet_disable_nosync(t);
  647. tasklet_unlock_spin_wait(t);
  648. smp_mb();
  649. }
  650. static inline void tasklet_disable(struct tasklet_struct *t)
  651. {
  652. tasklet_disable_nosync(t);
  653. tasklet_unlock_wait(t);
  654. smp_mb();
  655. }
  656. static inline void tasklet_enable(struct tasklet_struct *t)
  657. {
  658. smp_mb__before_atomic();
  659. atomic_dec(&t->count);
  660. }
  661. extern void tasklet_kill(struct tasklet_struct *t);
  662. extern void tasklet_init(struct tasklet_struct *t,
  663. void (*func)(unsigned long), unsigned long data);
  664. extern void tasklet_setup(struct tasklet_struct *t,
  665. void (*callback)(struct tasklet_struct *));
  666. /*
  667. * Autoprobing for irqs:
  668. *
  669. * probe_irq_on() and probe_irq_off() provide robust primitives
  670. * for accurate IRQ probing during kernel initialization. They are
  671. * reasonably simple to use, are not "fooled" by spurious interrupts,
  672. * and, unlike other attempts at IRQ probing, they do not get hung on
  673. * stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
  674. *
  675. * For reasonably foolproof probing, use them as follows:
  676. *
  677. * 1. clear and/or mask the device's internal interrupt.
  678. * 2. sti();
  679. * 3. irqs = probe_irq_on(); // "take over" all unassigned idle IRQs
  680. * 4. enable the device and cause it to trigger an interrupt.
  681. * 5. wait for the device to interrupt, using non-intrusive polling or a delay.
  682. * 6. irq = probe_irq_off(irqs); // get IRQ number, 0=none, negative=multiple
  683. * 7. service the device to clear its pending interrupt.
  684. * 8. loop again if paranoia is required.
  685. *
  686. * probe_irq_on() returns a mask of allocated irq's.
  687. *
  688. * probe_irq_off() takes the mask as a parameter,
  689. * and returns the irq number which occurred,
  690. * or zero if none occurred, or a negative irq number
  691. * if more than one irq occurred.
  692. */
  693. #if !defined(CONFIG_GENERIC_IRQ_PROBE)
  694. static inline unsigned long probe_irq_on(void)
  695. {
  696. return 0;
  697. }
  698. static inline int probe_irq_off(unsigned long val)
  699. {
  700. return 0;
  701. }
  702. static inline unsigned int probe_irq_mask(unsigned long val)
  703. {
  704. return 0;
  705. }
  706. #else
  707. extern unsigned long probe_irq_on(void); /* returns 0 on failure */
  708. extern int probe_irq_off(unsigned long); /* returns 0 or negative on failure */
  709. extern unsigned int probe_irq_mask(unsigned long); /* returns mask of ISA interrupts */
  710. #endif
  711. #ifdef CONFIG_PROC_FS
  712. /* Initialize /proc/irq/ */
  713. extern void init_irq_proc(void);
  714. #else
  715. static inline void init_irq_proc(void)
  716. {
  717. }
  718. #endif
  719. #ifdef CONFIG_IRQ_TIMINGS
  720. void irq_timings_enable(void);
  721. void irq_timings_disable(void);
  722. u64 irq_timings_next_event(u64 now);
  723. #endif
  724. struct seq_file;
  725. int show_interrupts(struct seq_file *p, void *v);
  726. int arch_show_interrupts(struct seq_file *p, int prec);
  727. extern int early_irq_init(void);
  728. extern int arch_probe_nr_irqs(void);
  729. extern int arch_early_irq_init(void);
  730. /*
  731. * We want to know which function is an entrypoint of a hardirq or a softirq.
  732. */
  733. #ifndef __irq_entry
  734. # define __irq_entry __section(".irqentry.text")
  735. #endif
  736. #define __softirq_entry __section(".softirqentry.text")
  737. #endif