internals.h 13 KB

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