internals.h 15 KB

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