internals.h 13 KB

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