internals.h 14 KB

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