internals.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * IRQ subsystem internal functions and variables:
  3. *
  4. * Do not ever include this file from anything else than
  5. * kernel/irq/. Do not even think about using any information outside
  6. * of this file for your non core code.
  7. */
  8. #include <linux/irqdesc.h>
  9. #include <linux/kernel_stat.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/sched/clock.h>
  12. #ifdef CONFIG_SPARSE_IRQ
  13. # define IRQ_BITMAP_BITS (NR_IRQS + 8196)
  14. #else
  15. # define IRQ_BITMAP_BITS NR_IRQS
  16. #endif
  17. #define istate core_internal_state__do_not_mess_with_it
  18. extern bool noirqdebug;
  19. extern struct irqaction chained_action;
  20. /*
  21. * Bits used by threaded handlers:
  22. * IRQTF_RUNTHREAD - signals that the interrupt handler thread should run
  23. * IRQTF_WARNED - warning "IRQ_WAKE_THREAD w/o thread_fn" has been printed
  24. * IRQTF_AFFINITY - irq thread is requested to adjust affinity
  25. * IRQTF_FORCED_THREAD - irq action is force threaded
  26. */
  27. enum {
  28. IRQTF_RUNTHREAD,
  29. IRQTF_WARNED,
  30. IRQTF_AFFINITY,
  31. IRQTF_FORCED_THREAD,
  32. };
  33. /*
  34. * Bit masks for desc->core_internal_state__do_not_mess_with_it
  35. *
  36. * IRQS_AUTODETECT - autodetection in progress
  37. * IRQS_SPURIOUS_DISABLED - was disabled due to spurious interrupt
  38. * detection
  39. * IRQS_POLL_INPROGRESS - polling in progress
  40. * IRQS_ONESHOT - irq is not unmasked in primary handler
  41. * IRQS_REPLAY - irq is replayed
  42. * IRQS_WAITING - irq is waiting
  43. * IRQS_PENDING - irq is pending and replayed later
  44. * IRQS_SUSPENDED - irq is suspended
  45. */
  46. enum {
  47. IRQS_AUTODETECT = 0x00000001,
  48. IRQS_SPURIOUS_DISABLED = 0x00000002,
  49. IRQS_POLL_INPROGRESS = 0x00000008,
  50. IRQS_ONESHOT = 0x00000020,
  51. IRQS_REPLAY = 0x00000040,
  52. IRQS_WAITING = 0x00000080,
  53. IRQS_PENDING = 0x00000200,
  54. IRQS_SUSPENDED = 0x00000800,
  55. IRQS_TIMINGS = 0x00001000,
  56. };
  57. #include "debug.h"
  58. #include "settings.h"
  59. extern int __irq_set_trigger(struct irq_desc *desc, unsigned long flags);
  60. extern void __disable_irq(struct irq_desc *desc);
  61. extern void __enable_irq(struct irq_desc *desc);
  62. #define IRQ_RESEND true
  63. #define IRQ_NORESEND false
  64. #define IRQ_START_FORCE true
  65. #define IRQ_START_COND false
  66. extern int irq_activate(struct irq_desc *desc);
  67. extern void irq_activate_and_startup(struct irq_desc *desc, bool resend);
  68. extern int irq_startup(struct irq_desc *desc, bool resend, bool force);
  69. extern void irq_shutdown(struct irq_desc *desc);
  70. extern void irq_enable(struct irq_desc *desc);
  71. extern void irq_disable(struct irq_desc *desc);
  72. extern void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu);
  73. extern void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu);
  74. extern void mask_irq(struct irq_desc *desc);
  75. extern void unmask_irq(struct irq_desc *desc);
  76. extern void unmask_threaded_irq(struct irq_desc *desc);
  77. #ifdef CONFIG_SPARSE_IRQ
  78. static inline void irq_mark_irq(unsigned int irq) { }
  79. #else
  80. extern void irq_mark_irq(unsigned int irq);
  81. #endif
  82. extern void init_kstat_irqs(struct irq_desc *desc, int node, int nr);
  83. irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags);
  84. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc);
  85. irqreturn_t handle_irq_event(struct irq_desc *desc);
  86. /* Resending of interrupts :*/
  87. void check_irq_resend(struct irq_desc *desc);
  88. bool irq_wait_for_poll(struct irq_desc *desc);
  89. void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action);
  90. #ifdef CONFIG_PROC_FS
  91. extern void register_irq_proc(unsigned int irq, struct irq_desc *desc);
  92. extern void unregister_irq_proc(unsigned int irq, struct irq_desc *desc);
  93. extern void register_handler_proc(unsigned int irq, struct irqaction *action);
  94. extern void unregister_handler_proc(unsigned int irq, struct irqaction *action);
  95. #else
  96. static inline void register_irq_proc(unsigned int irq, struct irq_desc *desc) { }
  97. static inline void unregister_irq_proc(unsigned int irq, struct irq_desc *desc) { }
  98. static inline void register_handler_proc(unsigned int irq,
  99. struct irqaction *action) { }
  100. static inline void unregister_handler_proc(unsigned int irq,
  101. struct irqaction *action) { }
  102. #endif
  103. extern bool irq_can_set_affinity_usr(unsigned int irq);
  104. extern int irq_select_affinity_usr(unsigned int irq);
  105. extern void irq_set_thread_affinity(struct irq_desc *desc);
  106. extern int irq_do_set_affinity(struct irq_data *data,
  107. const struct cpumask *dest, bool force);
  108. #ifdef CONFIG_SMP
  109. extern int irq_setup_affinity(struct irq_desc *desc);
  110. #else
  111. static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; }
  112. #endif
  113. /* Inline functions for support of irq chips on slow busses */
  114. static inline void chip_bus_lock(struct irq_desc *desc)
  115. {
  116. if (unlikely(desc->irq_data.chip->irq_bus_lock))
  117. desc->irq_data.chip->irq_bus_lock(&desc->irq_data);
  118. }
  119. static inline void chip_bus_sync_unlock(struct irq_desc *desc)
  120. {
  121. if (unlikely(desc->irq_data.chip->irq_bus_sync_unlock))
  122. desc->irq_data.chip->irq_bus_sync_unlock(&desc->irq_data);
  123. }
  124. #define _IRQ_DESC_CHECK (1 << 0)
  125. #define _IRQ_DESC_PERCPU (1 << 1)
  126. #define IRQ_GET_DESC_CHECK_GLOBAL (_IRQ_DESC_CHECK)
  127. #define IRQ_GET_DESC_CHECK_PERCPU (_IRQ_DESC_CHECK | _IRQ_DESC_PERCPU)
  128. #define for_each_action_of_desc(desc, act) \
  129. for (act = desc->action; act; act = act->next)
  130. struct irq_desc *
  131. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  132. unsigned int check);
  133. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus);
  134. static inline struct irq_desc *
  135. irq_get_desc_buslock(unsigned int irq, unsigned long *flags, unsigned int check)
  136. {
  137. return __irq_get_desc_lock(irq, flags, true, check);
  138. }
  139. static inline void
  140. irq_put_desc_busunlock(struct irq_desc *desc, unsigned long flags)
  141. {
  142. __irq_put_desc_unlock(desc, flags, true);
  143. }
  144. static inline struct irq_desc *
  145. irq_get_desc_lock(unsigned int irq, unsigned long *flags, unsigned int check)
  146. {
  147. return __irq_get_desc_lock(irq, flags, false, check);
  148. }
  149. static inline void
  150. irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags)
  151. {
  152. __irq_put_desc_unlock(desc, flags, false);
  153. }
  154. #define __irqd_to_state(d) ACCESS_PRIVATE((d)->common, state_use_accessors)
  155. static inline unsigned int irqd_get(struct irq_data *d)
  156. {
  157. return __irqd_to_state(d);
  158. }
  159. /*
  160. * Manipulation functions for irq_data.state
  161. */
  162. static inline void irqd_set_move_pending(struct irq_data *d)
  163. {
  164. __irqd_to_state(d) |= IRQD_SETAFFINITY_PENDING;
  165. }
  166. static inline void irqd_clr_move_pending(struct irq_data *d)
  167. {
  168. __irqd_to_state(d) &= ~IRQD_SETAFFINITY_PENDING;
  169. }
  170. static inline void irqd_set_managed_shutdown(struct irq_data *d)
  171. {
  172. __irqd_to_state(d) |= IRQD_MANAGED_SHUTDOWN;
  173. }
  174. static inline void irqd_clr_managed_shutdown(struct irq_data *d)
  175. {
  176. __irqd_to_state(d) &= ~IRQD_MANAGED_SHUTDOWN;
  177. }
  178. static inline void irqd_clear(struct irq_data *d, unsigned int mask)
  179. {
  180. __irqd_to_state(d) &= ~mask;
  181. }
  182. static inline void irqd_set(struct irq_data *d, unsigned int mask)
  183. {
  184. __irqd_to_state(d) |= mask;
  185. }
  186. static inline bool irqd_has_set(struct irq_data *d, unsigned int mask)
  187. {
  188. return __irqd_to_state(d) & mask;
  189. }
  190. static inline void irq_state_set_disabled(struct irq_desc *desc)
  191. {
  192. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  193. }
  194. static inline void irq_state_set_masked(struct irq_desc *desc)
  195. {
  196. irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
  197. }
  198. #undef __irqd_to_state
  199. static inline void kstat_incr_irqs_this_cpu(struct irq_desc *desc)
  200. {
  201. __this_cpu_inc(*desc->kstat_irqs);
  202. __this_cpu_inc(kstat.irqs_sum);
  203. }
  204. static inline int irq_desc_get_node(struct irq_desc *desc)
  205. {
  206. return irq_common_data_get_node(&desc->irq_common_data);
  207. }
  208. static inline int irq_desc_is_chained(struct irq_desc *desc)
  209. {
  210. return (desc->action && desc->action == &chained_action);
  211. }
  212. #ifdef CONFIG_PM_SLEEP
  213. bool irq_pm_check_wakeup(struct irq_desc *desc);
  214. void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action);
  215. void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action);
  216. #else
  217. static inline bool irq_pm_check_wakeup(struct irq_desc *desc) { return false; }
  218. static inline void
  219. irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) { }
  220. static inline void
  221. irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { }
  222. #endif
  223. #ifdef CONFIG_IRQ_TIMINGS
  224. #define IRQ_TIMINGS_SHIFT 5
  225. #define IRQ_TIMINGS_SIZE (1 << IRQ_TIMINGS_SHIFT)
  226. #define IRQ_TIMINGS_MASK (IRQ_TIMINGS_SIZE - 1)
  227. /**
  228. * struct irq_timings - irq timings storing structure
  229. * @values: a circular buffer of u64 encoded <timestamp,irq> values
  230. * @count: the number of elements in the array
  231. */
  232. struct irq_timings {
  233. u64 values[IRQ_TIMINGS_SIZE];
  234. int count;
  235. };
  236. DECLARE_PER_CPU(struct irq_timings, irq_timings);
  237. extern void irq_timings_free(int irq);
  238. extern int irq_timings_alloc(int irq);
  239. static inline void irq_remove_timings(struct irq_desc *desc)
  240. {
  241. desc->istate &= ~IRQS_TIMINGS;
  242. irq_timings_free(irq_desc_get_irq(desc));
  243. }
  244. static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act)
  245. {
  246. int irq = irq_desc_get_irq(desc);
  247. int ret;
  248. /*
  249. * We don't need the measurement because the idle code already
  250. * knows the next expiry event.
  251. */
  252. if (act->flags & __IRQF_TIMER)
  253. return;
  254. /*
  255. * In case the timing allocation fails, we just want to warn,
  256. * not fail, so letting the system boot anyway.
  257. */
  258. ret = irq_timings_alloc(irq);
  259. if (ret) {
  260. pr_warn("Failed to allocate irq timing stats for irq%d (%d)",
  261. irq, ret);
  262. return;
  263. }
  264. desc->istate |= IRQS_TIMINGS;
  265. }
  266. extern void irq_timings_enable(void);
  267. extern void irq_timings_disable(void);
  268. DECLARE_STATIC_KEY_FALSE(irq_timing_enabled);
  269. /*
  270. * The interrupt number and the timestamp are encoded into a single
  271. * u64 variable to optimize the size.
  272. * 48 bit time stamp and 16 bit IRQ number is way sufficient.
  273. * Who cares an IRQ after 78 hours of idle time?
  274. */
  275. static inline u64 irq_timing_encode(u64 timestamp, int irq)
  276. {
  277. return (timestamp << 16) | irq;
  278. }
  279. static inline int irq_timing_decode(u64 value, u64 *timestamp)
  280. {
  281. *timestamp = value >> 16;
  282. return value & U16_MAX;
  283. }
  284. /*
  285. * The function record_irq_time is only called in one place in the
  286. * interrupts handler. We want this function always inline so the code
  287. * inside is embedded in the function and the static key branching
  288. * code can act at the higher level. Without the explicit
  289. * __always_inline we can end up with a function call and a small
  290. * overhead in the hotpath for nothing.
  291. */
  292. static __always_inline void record_irq_time(struct irq_desc *desc)
  293. {
  294. if (!static_branch_likely(&irq_timing_enabled))
  295. return;
  296. if (desc->istate & IRQS_TIMINGS) {
  297. struct irq_timings *timings = this_cpu_ptr(&irq_timings);
  298. timings->values[timings->count & IRQ_TIMINGS_MASK] =
  299. irq_timing_encode(local_clock(),
  300. irq_desc_get_irq(desc));
  301. timings->count++;
  302. }
  303. }
  304. #else
  305. static inline void irq_remove_timings(struct irq_desc *desc) {}
  306. static inline void irq_setup_timings(struct irq_desc *desc,
  307. struct irqaction *act) {};
  308. static inline void record_irq_time(struct irq_desc *desc) {}
  309. #endif /* CONFIG_IRQ_TIMINGS */
  310. #ifdef CONFIG_GENERIC_IRQ_CHIP
  311. void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
  312. int num_ct, unsigned int irq_base,
  313. void __iomem *reg_base, irq_flow_handler_t handler);
  314. #else
  315. static inline void
  316. irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
  317. int num_ct, unsigned int irq_base,
  318. void __iomem *reg_base, irq_flow_handler_t handler) { }
  319. #endif /* CONFIG_GENERIC_IRQ_CHIP */
  320. #ifdef CONFIG_GENERIC_PENDING_IRQ
  321. static inline bool irq_can_move_pcntxt(struct irq_data *data)
  322. {
  323. return irqd_can_move_in_process_context(data);
  324. }
  325. static inline bool irq_move_pending(struct irq_data *data)
  326. {
  327. return irqd_is_setaffinity_pending(data);
  328. }
  329. static inline void
  330. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  331. {
  332. cpumask_copy(desc->pending_mask, mask);
  333. }
  334. static inline void
  335. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  336. {
  337. cpumask_copy(mask, desc->pending_mask);
  338. }
  339. static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc)
  340. {
  341. return desc->pending_mask;
  342. }
  343. bool irq_fixup_move_pending(struct irq_desc *desc, bool force_clear);
  344. #else /* CONFIG_GENERIC_PENDING_IRQ */
  345. static inline bool irq_can_move_pcntxt(struct irq_data *data)
  346. {
  347. return true;
  348. }
  349. static inline bool irq_move_pending(struct irq_data *data)
  350. {
  351. return false;
  352. }
  353. static inline void
  354. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  355. {
  356. }
  357. static inline void
  358. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  359. {
  360. }
  361. static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc)
  362. {
  363. return NULL;
  364. }
  365. static inline bool irq_fixup_move_pending(struct irq_desc *desc, bool fclear)
  366. {
  367. return false;
  368. }
  369. #endif /* !CONFIG_GENERIC_PENDING_IRQ */
  370. #if !defined(CONFIG_IRQ_DOMAIN) || !defined(CONFIG_IRQ_DOMAIN_HIERARCHY)
  371. static inline void irq_domain_activate_irq(struct irq_data *data)
  372. {
  373. irqd_set_activated(data);
  374. }
  375. static inline void irq_domain_deactivate_irq(struct irq_data *data)
  376. {
  377. irqd_clr_activated(data);
  378. }
  379. #endif
  380. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  381. #include <linux/debugfs.h>
  382. void irq_add_debugfs_entry(unsigned int irq, struct irq_desc *desc);
  383. static inline void irq_remove_debugfs_entry(struct irq_desc *desc)
  384. {
  385. debugfs_remove(desc->debugfs_file);
  386. kfree(desc->dev_name);
  387. }
  388. void irq_debugfs_copy_devname(int irq, struct device *dev);
  389. # ifdef CONFIG_IRQ_DOMAIN
  390. void irq_domain_debugfs_init(struct dentry *root);
  391. # else
  392. static inline void irq_domain_debugfs_init(struct dentry *root)
  393. {
  394. }
  395. # endif
  396. #else /* CONFIG_GENERIC_IRQ_DEBUGFS */
  397. static inline void irq_add_debugfs_entry(unsigned int irq, struct irq_desc *d)
  398. {
  399. }
  400. static inline void irq_remove_debugfs_entry(struct irq_desc *d)
  401. {
  402. }
  403. static inline void irq_debugfs_copy_devname(int irq, struct device *dev)
  404. {
  405. }
  406. #endif /* CONFIG_GENERIC_IRQ_DEBUGFS */