internals.h 14 KB

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