internals.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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_activate(struct irq_desc *desc);
  68. extern void irq_activate_and_startup(struct irq_desc *desc, bool resend);
  69. extern int irq_startup(struct irq_desc *desc, bool resend, bool force);
  70. extern void irq_shutdown(struct irq_desc *desc);
  71. extern void irq_enable(struct irq_desc *desc);
  72. extern void irq_disable(struct irq_desc *desc);
  73. extern void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu);
  74. extern void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu);
  75. extern void mask_irq(struct irq_desc *desc);
  76. extern void unmask_irq(struct irq_desc *desc);
  77. extern void unmask_threaded_irq(struct irq_desc *desc);
  78. #ifdef CONFIG_SPARSE_IRQ
  79. static inline void irq_mark_irq(unsigned int irq) { }
  80. #else
  81. extern void irq_mark_irq(unsigned int irq);
  82. #endif
  83. extern void init_kstat_irqs(struct irq_desc *desc, int node, int nr);
  84. irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags);
  85. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc);
  86. irqreturn_t handle_irq_event(struct irq_desc *desc);
  87. /* Resending of interrupts :*/
  88. void check_irq_resend(struct irq_desc *desc);
  89. bool irq_wait_for_poll(struct irq_desc *desc);
  90. void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action);
  91. #ifdef CONFIG_PROC_FS
  92. extern void register_irq_proc(unsigned int irq, struct irq_desc *desc);
  93. extern void unregister_irq_proc(unsigned int irq, struct irq_desc *desc);
  94. extern void register_handler_proc(unsigned int irq, struct irqaction *action);
  95. extern void unregister_handler_proc(unsigned int irq, struct irqaction *action);
  96. #else
  97. static inline void register_irq_proc(unsigned int irq, struct irq_desc *desc) { }
  98. static inline void unregister_irq_proc(unsigned int irq, struct irq_desc *desc) { }
  99. static inline void register_handler_proc(unsigned int irq,
  100. struct irqaction *action) { }
  101. static inline void unregister_handler_proc(unsigned int irq,
  102. struct irqaction *action) { }
  103. #endif
  104. extern bool irq_can_set_affinity_usr(unsigned int irq);
  105. extern int irq_select_affinity_usr(unsigned int irq);
  106. extern void irq_set_thread_affinity(struct irq_desc *desc);
  107. extern int irq_do_set_affinity(struct irq_data *data,
  108. const struct cpumask *dest, bool force);
  109. #ifdef CONFIG_SMP
  110. extern int irq_setup_affinity(struct irq_desc *desc);
  111. #else
  112. static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; }
  113. #endif
  114. /* Inline functions for support of irq chips on slow busses */
  115. static inline void chip_bus_lock(struct irq_desc *desc)
  116. {
  117. if (unlikely(desc->irq_data.chip->irq_bus_lock))
  118. desc->irq_data.chip->irq_bus_lock(&desc->irq_data);
  119. }
  120. static inline void chip_bus_sync_unlock(struct irq_desc *desc)
  121. {
  122. if (unlikely(desc->irq_data.chip->irq_bus_sync_unlock))
  123. desc->irq_data.chip->irq_bus_sync_unlock(&desc->irq_data);
  124. }
  125. #define _IRQ_DESC_CHECK (1 << 0)
  126. #define _IRQ_DESC_PERCPU (1 << 1)
  127. #define IRQ_GET_DESC_CHECK_GLOBAL (_IRQ_DESC_CHECK)
  128. #define IRQ_GET_DESC_CHECK_PERCPU (_IRQ_DESC_CHECK | _IRQ_DESC_PERCPU)
  129. #define for_each_action_of_desc(desc, act) \
  130. for (act = desc->action; act; act = act->next)
  131. struct irq_desc *
  132. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  133. unsigned int check);
  134. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus);
  135. static inline struct irq_desc *
  136. irq_get_desc_buslock(unsigned int irq, unsigned long *flags, unsigned int check)
  137. {
  138. return __irq_get_desc_lock(irq, flags, true, check);
  139. }
  140. static inline void
  141. irq_put_desc_busunlock(struct irq_desc *desc, unsigned long flags)
  142. {
  143. __irq_put_desc_unlock(desc, flags, true);
  144. }
  145. static inline struct irq_desc *
  146. irq_get_desc_lock(unsigned int irq, unsigned long *flags, unsigned int check)
  147. {
  148. return __irq_get_desc_lock(irq, flags, false, check);
  149. }
  150. static inline void
  151. irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags)
  152. {
  153. __irq_put_desc_unlock(desc, flags, false);
  154. }
  155. #define __irqd_to_state(d) ACCESS_PRIVATE((d)->common, state_use_accessors)
  156. static inline unsigned int irqd_get(struct irq_data *d)
  157. {
  158. return __irqd_to_state(d);
  159. }
  160. /*
  161. * Manipulation functions for irq_data.state
  162. */
  163. static inline void irqd_set_move_pending(struct irq_data *d)
  164. {
  165. __irqd_to_state(d) |= IRQD_SETAFFINITY_PENDING;
  166. }
  167. static inline void irqd_clr_move_pending(struct irq_data *d)
  168. {
  169. __irqd_to_state(d) &= ~IRQD_SETAFFINITY_PENDING;
  170. }
  171. static inline void irqd_set_managed_shutdown(struct irq_data *d)
  172. {
  173. __irqd_to_state(d) |= IRQD_MANAGED_SHUTDOWN;
  174. }
  175. static inline void irqd_clr_managed_shutdown(struct irq_data *d)
  176. {
  177. __irqd_to_state(d) &= ~IRQD_MANAGED_SHUTDOWN;
  178. }
  179. static inline void irqd_clear(struct irq_data *d, unsigned int mask)
  180. {
  181. __irqd_to_state(d) &= ~mask;
  182. }
  183. static inline void irqd_set(struct irq_data *d, unsigned int mask)
  184. {
  185. __irqd_to_state(d) |= mask;
  186. }
  187. static inline bool irqd_has_set(struct irq_data *d, unsigned int mask)
  188. {
  189. return __irqd_to_state(d) & mask;
  190. }
  191. static inline void irq_state_set_disabled(struct irq_desc *desc)
  192. {
  193. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  194. }
  195. static inline void irq_state_set_masked(struct irq_desc *desc)
  196. {
  197. irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
  198. }
  199. #undef __irqd_to_state
  200. static inline void kstat_incr_irqs_this_cpu(struct irq_desc *desc)
  201. {
  202. __this_cpu_inc(*desc->kstat_irqs);
  203. __this_cpu_inc(kstat.irqs_sum);
  204. }
  205. static inline int irq_desc_get_node(struct irq_desc *desc)
  206. {
  207. return irq_common_data_get_node(&desc->irq_common_data);
  208. }
  209. static inline int irq_desc_is_chained(struct irq_desc *desc)
  210. {
  211. return (desc->action && desc->action == &chained_action);
  212. }
  213. #ifdef CONFIG_PM_SLEEP
  214. bool irq_pm_check_wakeup(struct irq_desc *desc);
  215. void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action);
  216. void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action);
  217. #else
  218. static inline bool irq_pm_check_wakeup(struct irq_desc *desc) { return false; }
  219. static inline void
  220. irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) { }
  221. static inline void
  222. irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { }
  223. #endif
  224. #ifdef CONFIG_IRQ_TIMINGS
  225. #define IRQ_TIMINGS_SHIFT 5
  226. #define IRQ_TIMINGS_SIZE (1 << IRQ_TIMINGS_SHIFT)
  227. #define IRQ_TIMINGS_MASK (IRQ_TIMINGS_SIZE - 1)
  228. /**
  229. * struct irq_timings - irq timings storing structure
  230. * @values: a circular buffer of u64 encoded <timestamp,irq> values
  231. * @count: the number of elements in the array
  232. */
  233. struct irq_timings {
  234. u64 values[IRQ_TIMINGS_SIZE];
  235. int count;
  236. };
  237. DECLARE_PER_CPU(struct irq_timings, irq_timings);
  238. extern void irq_timings_free(int irq);
  239. extern int irq_timings_alloc(int irq);
  240. static inline void irq_remove_timings(struct irq_desc *desc)
  241. {
  242. desc->istate &= ~IRQS_TIMINGS;
  243. irq_timings_free(irq_desc_get_irq(desc));
  244. }
  245. static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act)
  246. {
  247. int irq = irq_desc_get_irq(desc);
  248. int ret;
  249. /*
  250. * We don't need the measurement because the idle code already
  251. * knows the next expiry event.
  252. */
  253. if (act->flags & __IRQF_TIMER)
  254. return;
  255. /*
  256. * In case the timing allocation fails, we just want to warn,
  257. * not fail, so letting the system boot anyway.
  258. */
  259. ret = irq_timings_alloc(irq);
  260. if (ret) {
  261. pr_warn("Failed to allocate irq timing stats for irq%d (%d)",
  262. irq, ret);
  263. return;
  264. }
  265. desc->istate |= IRQS_TIMINGS;
  266. }
  267. extern void irq_timings_enable(void);
  268. extern void irq_timings_disable(void);
  269. DECLARE_STATIC_KEY_FALSE(irq_timing_enabled);
  270. /*
  271. * The interrupt number and the timestamp are encoded into a single
  272. * u64 variable to optimize the size.
  273. * 48 bit time stamp and 16 bit IRQ number is way sufficient.
  274. * Who cares an IRQ after 78 hours of idle time?
  275. */
  276. static inline u64 irq_timing_encode(u64 timestamp, int irq)
  277. {
  278. return (timestamp << 16) | irq;
  279. }
  280. static inline int irq_timing_decode(u64 value, u64 *timestamp)
  281. {
  282. *timestamp = value >> 16;
  283. return value & U16_MAX;
  284. }
  285. /*
  286. * The function record_irq_time is only called in one place in the
  287. * interrupts handler. We want this function always inline so the code
  288. * inside is embedded in the function and the static key branching
  289. * code can act at the higher level. Without the explicit
  290. * __always_inline we can end up with a function call and a small
  291. * overhead in the hotpath for nothing.
  292. */
  293. static __always_inline void record_irq_time(struct irq_desc *desc)
  294. {
  295. if (!static_branch_likely(&irq_timing_enabled))
  296. return;
  297. if (desc->istate & IRQS_TIMINGS) {
  298. struct irq_timings *timings = this_cpu_ptr(&irq_timings);
  299. timings->values[timings->count & IRQ_TIMINGS_MASK] =
  300. irq_timing_encode(local_clock(),
  301. irq_desc_get_irq(desc));
  302. timings->count++;
  303. }
  304. }
  305. #else
  306. static inline void irq_remove_timings(struct irq_desc *desc) {}
  307. static inline void irq_setup_timings(struct irq_desc *desc,
  308. struct irqaction *act) {};
  309. static inline void record_irq_time(struct irq_desc *desc) {}
  310. #endif /* CONFIG_IRQ_TIMINGS */
  311. #ifdef CONFIG_GENERIC_IRQ_CHIP
  312. void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
  313. int num_ct, unsigned int irq_base,
  314. void __iomem *reg_base, irq_flow_handler_t handler);
  315. #else
  316. static inline void
  317. irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
  318. int num_ct, unsigned int irq_base,
  319. void __iomem *reg_base, irq_flow_handler_t handler) { }
  320. #endif /* CONFIG_GENERIC_IRQ_CHIP */
  321. #ifdef CONFIG_GENERIC_PENDING_IRQ
  322. static inline bool irq_can_move_pcntxt(struct irq_data *data)
  323. {
  324. return irqd_can_move_in_process_context(data);
  325. }
  326. static inline bool irq_move_pending(struct irq_data *data)
  327. {
  328. return irqd_is_setaffinity_pending(data);
  329. }
  330. static inline void
  331. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  332. {
  333. cpumask_copy(desc->pending_mask, mask);
  334. }
  335. static inline void
  336. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  337. {
  338. cpumask_copy(mask, desc->pending_mask);
  339. }
  340. static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc)
  341. {
  342. return desc->pending_mask;
  343. }
  344. bool irq_fixup_move_pending(struct irq_desc *desc, bool force_clear);
  345. #else /* CONFIG_GENERIC_PENDING_IRQ */
  346. static inline bool irq_can_move_pcntxt(struct irq_data *data)
  347. {
  348. return true;
  349. }
  350. static inline bool irq_move_pending(struct irq_data *data)
  351. {
  352. return false;
  353. }
  354. static inline void
  355. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  356. {
  357. }
  358. static inline void
  359. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  360. {
  361. }
  362. static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc)
  363. {
  364. return NULL;
  365. }
  366. static inline bool irq_fixup_move_pending(struct irq_desc *desc, bool fclear)
  367. {
  368. return false;
  369. }
  370. #endif /* !CONFIG_GENERIC_PENDING_IRQ */
  371. #if !defined(CONFIG_IRQ_DOMAIN) || !defined(CONFIG_IRQ_DOMAIN_HIERARCHY)
  372. static inline int irq_domain_activate_irq(struct irq_data *data, bool early)
  373. {
  374. irqd_set_activated(data);
  375. return 0;
  376. }
  377. static inline void irq_domain_deactivate_irq(struct irq_data *data)
  378. {
  379. irqd_clr_activated(data);
  380. }
  381. #endif
  382. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  383. #include <linux/debugfs.h>
  384. void irq_add_debugfs_entry(unsigned int irq, struct irq_desc *desc);
  385. static inline void irq_remove_debugfs_entry(struct irq_desc *desc)
  386. {
  387. debugfs_remove(desc->debugfs_file);
  388. kfree(desc->dev_name);
  389. }
  390. void irq_debugfs_copy_devname(int irq, struct device *dev);
  391. # ifdef CONFIG_IRQ_DOMAIN
  392. void irq_domain_debugfs_init(struct dentry *root);
  393. # else
  394. static inline void irq_domain_debugfs_init(struct dentry *root)
  395. {
  396. }
  397. # endif
  398. #else /* CONFIG_GENERIC_IRQ_DEBUGFS */
  399. static inline void irq_add_debugfs_entry(unsigned int irq, struct irq_desc *d)
  400. {
  401. }
  402. static inline void irq_remove_debugfs_entry(struct irq_desc *d)
  403. {
  404. }
  405. static inline void irq_debugfs_copy_devname(int irq, struct device *dev)
  406. {
  407. }
  408. #endif /* CONFIG_GENERIC_IRQ_DEBUGFS */