xref: /linux/kernel/irq/internals.h (revision 6678ae19)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * IRQ subsystem internal functions and variables:
4dbec07baSThomas Gleixner  *
5dbec07baSThomas Gleixner  * Do not ever include this file from anything else than
6dbec07baSThomas Gleixner  * kernel/irq/. Do not even think about using any information outside
7dbec07baSThomas Gleixner  * of this file for your non core code.
81da177e4SLinus Torvalds  */
9e144710bSThomas Gleixner #include <linux/irqdesc.h>
108f945a33SThomas Gleixner #include <linux/kernel_stat.h>
11be45beb2SJon Hunter #include <linux/pm_runtime.h>
12b2d3d61aSDaniel Lezcano #include <linux/sched/clock.h>
131da177e4SLinus Torvalds 
14c1ee6264SThomas Gleixner #ifdef CONFIG_SPARSE_IRQ
15721255b9SShanker Donthineni # define MAX_SPARSE_IRQS	INT_MAX
16c1ee6264SThomas Gleixner #else
175e630aa8SShanker Donthineni # define MAX_SPARSE_IRQS	NR_IRQS
18c1ee6264SThomas Gleixner #endif
19c1ee6264SThomas Gleixner 
20dbec07baSThomas Gleixner #define istate core_internal_state__do_not_mess_with_it
21dbec07baSThomas Gleixner 
222329abfaSRusty Russell extern bool noirqdebug;
231da177e4SLinus Torvalds 
24e509bd7dSMika Westerberg extern struct irqaction chained_action;
25e509bd7dSMika Westerberg 
261535dfacSThomas Gleixner /*
271535dfacSThomas Gleixner  * Bits used by threaded handlers:
281535dfacSThomas Gleixner  * IRQTF_RUNTHREAD - signals that the interrupt handler thread should run
291535dfacSThomas Gleixner  * IRQTF_WARNED    - warning "IRQ_WAKE_THREAD w/o thread_fn" has been printed
301535dfacSThomas Gleixner  * IRQTF_AFFINITY  - irq thread is requested to adjust affinity
318d32a307SThomas Gleixner  * IRQTF_FORCED_THREAD  - irq action is force threaded
328707898eSThomas Pfaff  * IRQTF_READY     - signals that irq thread is ready
331535dfacSThomas Gleixner  */
341535dfacSThomas Gleixner enum {
351535dfacSThomas Gleixner 	IRQTF_RUNTHREAD,
361535dfacSThomas Gleixner 	IRQTF_WARNED,
371535dfacSThomas Gleixner 	IRQTF_AFFINITY,
388d32a307SThomas Gleixner 	IRQTF_FORCED_THREAD,
398707898eSThomas Pfaff 	IRQTF_READY,
401535dfacSThomas Gleixner };
411535dfacSThomas Gleixner 
42bd062e76SThomas Gleixner /*
43a257954bSJiang Liu  * Bit masks for desc->core_internal_state__do_not_mess_with_it
44bd062e76SThomas Gleixner  *
45bd062e76SThomas Gleixner  * IRQS_AUTODETECT		- autodetection in progress
467acdd53eSThomas Gleixner  * IRQS_SPURIOUS_DISABLED	- was disabled due to spurious interrupt
477acdd53eSThomas Gleixner  *				  detection
486954b75bSThomas Gleixner  * IRQS_POLL_INPROGRESS		- polling in progress
493d67baecSThomas Gleixner  * IRQS_ONESHOT			- irq is not unmasked in primary handler
507cc148a3SJames Gowans  * IRQS_REPLAY			- irq has been resent and will not be resent
517cc148a3SJames Gowans  * 				  again until the handler has run and cleared
527cc148a3SJames Gowans  * 				  this flag.
53163ef309SThomas Gleixner  * IRQS_WAITING			- irq is waiting
547cc148a3SJames Gowans  * IRQS_PENDING			- irq needs to be resent and should be resent
557cc148a3SJames Gowans  * 				  at the next available opportunity.
56c531e836SThomas Gleixner  * IRQS_SUSPENDED		- irq is suspended
57b525903cSJulien Thierry  * IRQS_NMI			- irq line is used to deliver NMIs
589049e1caSYang Yingliang  * IRQS_SYSFS			- descriptor has been added to sysfs
59bd062e76SThomas Gleixner  */
60bd062e76SThomas Gleixner enum {
61bd062e76SThomas Gleixner 	IRQS_AUTODETECT		= 0x00000001,
627acdd53eSThomas Gleixner 	IRQS_SPURIOUS_DISABLED	= 0x00000002,
636954b75bSThomas Gleixner 	IRQS_POLL_INPROGRESS	= 0x00000008,
643d67baecSThomas Gleixner 	IRQS_ONESHOT		= 0x00000020,
65163ef309SThomas Gleixner 	IRQS_REPLAY		= 0x00000040,
66163ef309SThomas Gleixner 	IRQS_WAITING		= 0x00000080,
672a0d6fb3SThomas Gleixner 	IRQS_PENDING		= 0x00000200,
68c531e836SThomas Gleixner 	IRQS_SUSPENDED		= 0x00000800,
69b2d3d61aSDaniel Lezcano 	IRQS_TIMINGS		= 0x00001000,
70b525903cSJulien Thierry 	IRQS_NMI		= 0x00002000,
719049e1caSYang Yingliang 	IRQS_SYSFS		= 0x00004000,
72bd062e76SThomas Gleixner };
73bd062e76SThomas Gleixner 
741ce6068dSThomas Gleixner #include "debug.h"
751ce6068dSThomas Gleixner #include "settings.h"
761ce6068dSThomas Gleixner 
77a1ff541aSJiang Liu extern int __irq_set_trigger(struct irq_desc *desc, unsigned long flags);
7879ff1cdaSJiang Liu extern void __disable_irq(struct irq_desc *desc);
7979ff1cdaSJiang Liu extern void __enable_irq(struct irq_desc *desc);
800c5d1eb7SDavid Brownell 
814cde9c6bSThomas Gleixner #define IRQ_RESEND	true
824cde9c6bSThomas Gleixner #define IRQ_NORESEND	false
834cde9c6bSThomas Gleixner 
844cde9c6bSThomas Gleixner #define IRQ_START_FORCE	true
854cde9c6bSThomas Gleixner #define IRQ_START_COND	false
864cde9c6bSThomas Gleixner 
87c942cee4SThomas Gleixner extern int irq_activate(struct irq_desc *desc);
881beaeacdSThomas Gleixner extern int irq_activate_and_startup(struct irq_desc *desc, bool resend);
894cde9c6bSThomas Gleixner extern int irq_startup(struct irq_desc *desc, bool resend, bool force);
904cde9c6bSThomas Gleixner 
9146999238SThomas Gleixner extern void irq_shutdown(struct irq_desc *desc);
924001d8e8SThomas Gleixner extern void irq_shutdown_and_deactivate(struct irq_desc *desc);
9387923470SThomas Gleixner extern void irq_enable(struct irq_desc *desc);
9487923470SThomas Gleixner extern void irq_disable(struct irq_desc *desc);
9531d9d9b6SMarc Zyngier extern void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu);
9631d9d9b6SMarc Zyngier extern void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu);
97d4d5e089SThomas Gleixner extern void mask_irq(struct irq_desc *desc);
98d4d5e089SThomas Gleixner extern void unmask_irq(struct irq_desc *desc);
99328a4978SThomas Gleixner extern void unmask_threaded_irq(struct irq_desc *desc);
10046999238SThomas Gleixner 
10125a4a015SBitao Hu extern unsigned int kstat_irqs_desc(struct irq_desc *desc, const struct cpumask *cpumask);
10225a4a015SBitao Hu 
103f63b6a05SThomas Gleixner #ifdef CONFIG_SPARSE_IRQ
irq_mark_irq(unsigned int irq)104f63b6a05SThomas Gleixner static inline void irq_mark_irq(unsigned int irq) { }
105f63b6a05SThomas Gleixner #else
106f63b6a05SThomas Gleixner extern void irq_mark_irq(unsigned int irq);
107f63b6a05SThomas Gleixner #endif
108f63b6a05SThomas Gleixner 
10962e04686SThomas Gleixner extern int __irq_get_irqchip_state(struct irq_data *data,
11062e04686SThomas Gleixner 				   enum irqchip_irq_state which,
11162e04686SThomas Gleixner 				   bool *state);
11262e04686SThomas Gleixner 
1135320eb42SSebastian Andrzej Siewior irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc);
11471f64340SHuang Shijie irqreturn_t handle_irq_event_percpu(struct irq_desc *desc);
1154912609fSThomas Gleixner irqreturn_t handle_irq_event(struct irq_desc *desc);
1164912609fSThomas Gleixner 
117e144710bSThomas Gleixner /* Resending of interrupts :*/
118acd26bcfSThomas Gleixner int check_irq_resend(struct irq_desc *desc, bool inject);
119bc06a9e0SShanker Donthineni void clear_irq_resend(struct irq_desc *desc);
120bc06a9e0SShanker Donthineni void irq_resend_init(struct irq_desc *desc);
121fe200ae4SThomas Gleixner bool irq_wait_for_poll(struct irq_desc *desc);
122a92444c6SThomas Gleixner void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action);
123e144710bSThomas Gleixner 
124e2c12739SVincent Whitchurch void wake_threads_waitq(struct irq_desc *desc);
125e2c12739SVincent Whitchurch 
1261da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1272c6927a3SYinghai Lu extern void register_irq_proc(unsigned int irq, struct irq_desc *desc);
12813bfe99eSThomas Gleixner extern void unregister_irq_proc(unsigned int irq, struct irq_desc *desc);
1291da177e4SLinus Torvalds extern void register_handler_proc(unsigned int irq, struct irqaction *action);
1301da177e4SLinus Torvalds extern void unregister_handler_proc(unsigned int irq, struct irqaction *action);
1311da177e4SLinus Torvalds #else
register_irq_proc(unsigned int irq,struct irq_desc * desc)1322c6927a3SYinghai Lu static inline void register_irq_proc(unsigned int irq, struct irq_desc *desc) { }
unregister_irq_proc(unsigned int irq,struct irq_desc * desc)13313bfe99eSThomas Gleixner static inline void unregister_irq_proc(unsigned int irq, struct irq_desc *desc) { }
register_handler_proc(unsigned int irq,struct irqaction * action)1341da177e4SLinus Torvalds static inline void register_handler_proc(unsigned int irq,
1351da177e4SLinus Torvalds 					 struct irqaction *action) { }
unregister_handler_proc(unsigned int irq,struct irqaction * action)1361da177e4SLinus Torvalds static inline void unregister_handler_proc(unsigned int irq,
1371da177e4SLinus Torvalds 					   struct irqaction *action) { }
1381da177e4SLinus Torvalds #endif
1391da177e4SLinus Torvalds 
1409c255583SThomas Gleixner extern bool irq_can_set_affinity_usr(unsigned int irq);
1419c255583SThomas Gleixner 
142591d2fb0SThomas Gleixner extern void irq_set_thread_affinity(struct irq_desc *desc);
14357b150ccSYinghai Lu 
144818b0f3bSJiang Liu extern int irq_do_set_affinity(struct irq_data *data,
145818b0f3bSJiang Liu 			       const struct cpumask *dest, bool force);
146818b0f3bSJiang Liu 
14743564bd9SThomas Gleixner #ifdef CONFIG_SMP
14843564bd9SThomas Gleixner extern int irq_setup_affinity(struct irq_desc *desc);
14943564bd9SThomas Gleixner #else
irq_setup_affinity(struct irq_desc * desc)15043564bd9SThomas Gleixner static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; }
15143564bd9SThomas Gleixner #endif
15243564bd9SThomas Gleixner 
15370aedd24SThomas Gleixner /* Inline functions for support of irq chips on slow busses */
chip_bus_lock(struct irq_desc * desc)1543876ec9eSThomas Gleixner static inline void chip_bus_lock(struct irq_desc *desc)
15570aedd24SThomas Gleixner {
1563876ec9eSThomas Gleixner 	if (unlikely(desc->irq_data.chip->irq_bus_lock))
1573876ec9eSThomas Gleixner 		desc->irq_data.chip->irq_bus_lock(&desc->irq_data);
15870aedd24SThomas Gleixner }
15970aedd24SThomas Gleixner 
chip_bus_sync_unlock(struct irq_desc * desc)1603876ec9eSThomas Gleixner static inline void chip_bus_sync_unlock(struct irq_desc *desc)
16170aedd24SThomas Gleixner {
1623876ec9eSThomas Gleixner 	if (unlikely(desc->irq_data.chip->irq_bus_sync_unlock))
1633876ec9eSThomas Gleixner 		desc->irq_data.chip->irq_bus_sync_unlock(&desc->irq_data);
16470aedd24SThomas Gleixner }
16570aedd24SThomas Gleixner 
16631d9d9b6SMarc Zyngier #define _IRQ_DESC_CHECK		(1 << 0)
16731d9d9b6SMarc Zyngier #define _IRQ_DESC_PERCPU	(1 << 1)
16831d9d9b6SMarc Zyngier 
16931d9d9b6SMarc Zyngier #define IRQ_GET_DESC_CHECK_GLOBAL	(_IRQ_DESC_CHECK)
17031d9d9b6SMarc Zyngier #define IRQ_GET_DESC_CHECK_PERCPU	(_IRQ_DESC_CHECK | _IRQ_DESC_PERCPU)
17131d9d9b6SMarc Zyngier 
172f944b5a7SDaniel Lezcano #define for_each_action_of_desc(desc, act)			\
173163616cfSMasahiro Yamada 	for (act = desc->action; act; act = act->next)
174f944b5a7SDaniel Lezcano 
175d5eb4ad2SThomas Gleixner struct irq_desc *
17631d9d9b6SMarc Zyngier __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
17731d9d9b6SMarc Zyngier 		    unsigned int check);
178d5eb4ad2SThomas Gleixner void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus);
179d5eb4ad2SThomas Gleixner 
180d5eb4ad2SThomas Gleixner static inline struct irq_desc *
irq_get_desc_buslock(unsigned int irq,unsigned long * flags,unsigned int check)18131d9d9b6SMarc Zyngier irq_get_desc_buslock(unsigned int irq, unsigned long *flags, unsigned int check)
182d5eb4ad2SThomas Gleixner {
18331d9d9b6SMarc Zyngier 	return __irq_get_desc_lock(irq, flags, true, check);
184d5eb4ad2SThomas Gleixner }
185d5eb4ad2SThomas Gleixner 
186d5eb4ad2SThomas Gleixner static inline void
irq_put_desc_busunlock(struct irq_desc * desc,unsigned long flags)187d5eb4ad2SThomas Gleixner irq_put_desc_busunlock(struct irq_desc *desc, unsigned long flags)
188d5eb4ad2SThomas Gleixner {
189d5eb4ad2SThomas Gleixner 	__irq_put_desc_unlock(desc, flags, true);
190d5eb4ad2SThomas Gleixner }
191d5eb4ad2SThomas Gleixner 
192d5eb4ad2SThomas Gleixner static inline struct irq_desc *
irq_get_desc_lock(unsigned int irq,unsigned long * flags,unsigned int check)19331d9d9b6SMarc Zyngier irq_get_desc_lock(unsigned int irq, unsigned long *flags, unsigned int check)
194d5eb4ad2SThomas Gleixner {
19531d9d9b6SMarc Zyngier 	return __irq_get_desc_lock(irq, flags, false, check);
196d5eb4ad2SThomas Gleixner }
197d5eb4ad2SThomas Gleixner 
198d5eb4ad2SThomas Gleixner static inline void
irq_put_desc_unlock(struct irq_desc * desc,unsigned long flags)199d5eb4ad2SThomas Gleixner irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags)
200d5eb4ad2SThomas Gleixner {
201d5eb4ad2SThomas Gleixner 	__irq_put_desc_unlock(desc, flags, false);
202d5eb4ad2SThomas Gleixner }
203d5eb4ad2SThomas Gleixner 
204b354286eSBoqun Feng #define __irqd_to_state(d) ACCESS_PRIVATE((d)->common, state_use_accessors)
205b354286eSBoqun Feng 
irqd_get(struct irq_data * d)206087cdfb6SThomas Gleixner static inline unsigned int irqd_get(struct irq_data *d)
207087cdfb6SThomas Gleixner {
208087cdfb6SThomas Gleixner 	return __irqd_to_state(d);
209087cdfb6SThomas Gleixner }
210087cdfb6SThomas Gleixner 
21143f77759SIngo Molnar /*
212f230b6d5SThomas Gleixner  * Manipulation functions for irq_data.state
213f230b6d5SThomas Gleixner  */
irqd_set_move_pending(struct irq_data * d)214f230b6d5SThomas Gleixner static inline void irqd_set_move_pending(struct irq_data *d)
215f230b6d5SThomas Gleixner {
2160d0b4c86SJiang Liu 	__irqd_to_state(d) |= IRQD_SETAFFINITY_PENDING;
217f230b6d5SThomas Gleixner }
218f230b6d5SThomas Gleixner 
irqd_clr_move_pending(struct irq_data * d)219f230b6d5SThomas Gleixner static inline void irqd_clr_move_pending(struct irq_data *d)
220f230b6d5SThomas Gleixner {
2210d0b4c86SJiang Liu 	__irqd_to_state(d) &= ~IRQD_SETAFFINITY_PENDING;
222f230b6d5SThomas Gleixner }
223a005677bSThomas Gleixner 
irqd_set_managed_shutdown(struct irq_data * d)22454fdf6a0SThomas Gleixner static inline void irqd_set_managed_shutdown(struct irq_data *d)
22554fdf6a0SThomas Gleixner {
22654fdf6a0SThomas Gleixner 	__irqd_to_state(d) |= IRQD_MANAGED_SHUTDOWN;
22754fdf6a0SThomas Gleixner }
22854fdf6a0SThomas Gleixner 
irqd_clr_managed_shutdown(struct irq_data * d)22954fdf6a0SThomas Gleixner static inline void irqd_clr_managed_shutdown(struct irq_data *d)
23054fdf6a0SThomas Gleixner {
23154fdf6a0SThomas Gleixner 	__irqd_to_state(d) &= ~IRQD_MANAGED_SHUTDOWN;
23254fdf6a0SThomas Gleixner }
23354fdf6a0SThomas Gleixner 
irqd_clear(struct irq_data * d,unsigned int mask)234a005677bSThomas Gleixner static inline void irqd_clear(struct irq_data *d, unsigned int mask)
235a005677bSThomas Gleixner {
2360d0b4c86SJiang Liu 	__irqd_to_state(d) &= ~mask;
237a005677bSThomas Gleixner }
238a005677bSThomas Gleixner 
irqd_set(struct irq_data * d,unsigned int mask)239a005677bSThomas Gleixner static inline void irqd_set(struct irq_data *d, unsigned int mask)
240a005677bSThomas Gleixner {
2410d0b4c86SJiang Liu 	__irqd_to_state(d) |= mask;
242a005677bSThomas Gleixner }
243a005677bSThomas Gleixner 
irqd_has_set(struct irq_data * d,unsigned int mask)2442bdd1055SThomas Gleixner static inline bool irqd_has_set(struct irq_data *d, unsigned int mask)
2452bdd1055SThomas Gleixner {
2460d0b4c86SJiang Liu 	return __irqd_to_state(d) & mask;
2472bdd1055SThomas Gleixner }
2488f945a33SThomas Gleixner 
irq_state_set_disabled(struct irq_desc * desc)249a696712cSJuergen Gross static inline void irq_state_set_disabled(struct irq_desc *desc)
250a696712cSJuergen Gross {
251a696712cSJuergen Gross 	irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
252a696712cSJuergen Gross }
253a696712cSJuergen Gross 
irq_state_set_masked(struct irq_desc * desc)254a696712cSJuergen Gross static inline void irq_state_set_masked(struct irq_desc *desc)
255a696712cSJuergen Gross {
256a696712cSJuergen Gross 	irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
257a696712cSJuergen Gross }
258a696712cSJuergen Gross 
259b354286eSBoqun Feng #undef __irqd_to_state
260b354286eSBoqun Feng 
__kstat_incr_irqs_this_cpu(struct irq_desc * desc)2611136b072SThomas Gleixner static inline void __kstat_incr_irqs_this_cpu(struct irq_desc *desc)
2628f945a33SThomas Gleixner {
26386d2a2f5SBitao Hu 	__this_cpu_inc(desc->kstat_irqs->cnt);
2648f945a33SThomas Gleixner 	__this_cpu_inc(kstat.irqs_sum);
2658f945a33SThomas Gleixner }
266cab303beSThomas Gleixner 
kstat_incr_irqs_this_cpu(struct irq_desc * desc)2671136b072SThomas Gleixner static inline void kstat_incr_irqs_this_cpu(struct irq_desc *desc)
2681136b072SThomas Gleixner {
2691136b072SThomas Gleixner 	__kstat_incr_irqs_this_cpu(desc);
2701136b072SThomas Gleixner 	desc->tot_count++;
2711136b072SThomas Gleixner }
2721136b072SThomas Gleixner 
irq_desc_get_node(struct irq_desc * desc)2736783011bSJiang Liu static inline int irq_desc_get_node(struct irq_desc *desc)
2746783011bSJiang Liu {
275449e9caeSJiang Liu 	return irq_common_data_get_node(&desc->irq_common_data);
2766783011bSJiang Liu }
2776783011bSJiang Liu 
irq_desc_is_chained(struct irq_desc * desc)2784717f133SGrygorii Strashko static inline int irq_desc_is_chained(struct irq_desc *desc)
2794717f133SGrygorii Strashko {
2804717f133SGrygorii Strashko 	return (desc->action && desc->action == &chained_action);
2814717f133SGrygorii Strashko }
2824717f133SGrygorii Strashko 
irq_is_nmi(struct irq_desc * desc)283*6678ae19SJinjie Ruan static inline bool irq_is_nmi(struct irq_desc *desc)
284*6678ae19SJinjie Ruan {
285*6678ae19SJinjie Ruan 	return desc->istate & IRQS_NMI;
286*6678ae19SJinjie Ruan }
287*6678ae19SJinjie Ruan 
288cab303beSThomas Gleixner #ifdef CONFIG_PM_SLEEP
2899ce7a258SThomas Gleixner bool irq_pm_check_wakeup(struct irq_desc *desc);
290cab303beSThomas Gleixner void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action);
291cab303beSThomas Gleixner void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action);
292cab303beSThomas Gleixner #else
irq_pm_check_wakeup(struct irq_desc * desc)2939ce7a258SThomas Gleixner static inline bool irq_pm_check_wakeup(struct irq_desc *desc) { return false; }
294cab303beSThomas Gleixner static inline void
irq_pm_install_action(struct irq_desc * desc,struct irqaction * action)295cab303beSThomas Gleixner irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) { }
296cab303beSThomas Gleixner static inline void
irq_pm_remove_action(struct irq_desc * desc,struct irqaction * action)297cab303beSThomas Gleixner irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { }
298cab303beSThomas Gleixner #endif
299f1602039SBartosz Golaszewski 
300b2d3d61aSDaniel Lezcano #ifdef CONFIG_IRQ_TIMINGS
301b2d3d61aSDaniel Lezcano 
302b2d3d61aSDaniel Lezcano #define IRQ_TIMINGS_SHIFT	5
303b2d3d61aSDaniel Lezcano #define IRQ_TIMINGS_SIZE	(1 << IRQ_TIMINGS_SHIFT)
304b2d3d61aSDaniel Lezcano #define IRQ_TIMINGS_MASK	(IRQ_TIMINGS_SIZE - 1)
305b2d3d61aSDaniel Lezcano 
306b2d3d61aSDaniel Lezcano /**
307b2d3d61aSDaniel Lezcano  * struct irq_timings - irq timings storing structure
308b2d3d61aSDaniel Lezcano  * @values: a circular buffer of u64 encoded <timestamp,irq> values
309b2d3d61aSDaniel Lezcano  * @count: the number of elements in the array
310b2d3d61aSDaniel Lezcano  */
311b2d3d61aSDaniel Lezcano struct irq_timings {
312b2d3d61aSDaniel Lezcano 	u64	values[IRQ_TIMINGS_SIZE];
313b2d3d61aSDaniel Lezcano 	int	count;
314b2d3d61aSDaniel Lezcano };
315b2d3d61aSDaniel Lezcano 
316b2d3d61aSDaniel Lezcano DECLARE_PER_CPU(struct irq_timings, irq_timings);
317b2d3d61aSDaniel Lezcano 
318e1c92149SDaniel Lezcano extern void irq_timings_free(int irq);
319e1c92149SDaniel Lezcano extern int irq_timings_alloc(int irq);
320e1c92149SDaniel Lezcano 
irq_remove_timings(struct irq_desc * desc)321b2d3d61aSDaniel Lezcano static inline void irq_remove_timings(struct irq_desc *desc)
322b2d3d61aSDaniel Lezcano {
323b2d3d61aSDaniel Lezcano 	desc->istate &= ~IRQS_TIMINGS;
324e1c92149SDaniel Lezcano 
325e1c92149SDaniel Lezcano 	irq_timings_free(irq_desc_get_irq(desc));
326b2d3d61aSDaniel Lezcano }
327b2d3d61aSDaniel Lezcano 
irq_setup_timings(struct irq_desc * desc,struct irqaction * act)328b2d3d61aSDaniel Lezcano static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act)
329b2d3d61aSDaniel Lezcano {
330e1c92149SDaniel Lezcano 	int irq = irq_desc_get_irq(desc);
331e1c92149SDaniel Lezcano 	int ret;
332e1c92149SDaniel Lezcano 
333b2d3d61aSDaniel Lezcano 	/*
334b2d3d61aSDaniel Lezcano 	 * We don't need the measurement because the idle code already
335b2d3d61aSDaniel Lezcano 	 * knows the next expiry event.
336b2d3d61aSDaniel Lezcano 	 */
337b2d3d61aSDaniel Lezcano 	if (act->flags & __IRQF_TIMER)
338b2d3d61aSDaniel Lezcano 		return;
339b2d3d61aSDaniel Lezcano 
340e1c92149SDaniel Lezcano 	/*
341e1c92149SDaniel Lezcano 	 * In case the timing allocation fails, we just want to warn,
342e1c92149SDaniel Lezcano 	 * not fail, so letting the system boot anyway.
343e1c92149SDaniel Lezcano 	 */
344e1c92149SDaniel Lezcano 	ret = irq_timings_alloc(irq);
345e1c92149SDaniel Lezcano 	if (ret) {
346e1c92149SDaniel Lezcano 		pr_warn("Failed to allocate irq timing stats for irq%d (%d)",
347e1c92149SDaniel Lezcano 			irq, ret);
348e1c92149SDaniel Lezcano 		return;
349e1c92149SDaniel Lezcano 	}
350e1c92149SDaniel Lezcano 
351b2d3d61aSDaniel Lezcano 	desc->istate |= IRQS_TIMINGS;
352b2d3d61aSDaniel Lezcano }
353b2d3d61aSDaniel Lezcano 
354b2d3d61aSDaniel Lezcano extern void irq_timings_enable(void);
355b2d3d61aSDaniel Lezcano extern void irq_timings_disable(void);
356b2d3d61aSDaniel Lezcano 
357b2d3d61aSDaniel Lezcano DECLARE_STATIC_KEY_FALSE(irq_timing_enabled);
358b2d3d61aSDaniel Lezcano 
359b2d3d61aSDaniel Lezcano /*
360b2d3d61aSDaniel Lezcano  * The interrupt number and the timestamp are encoded into a single
361b2d3d61aSDaniel Lezcano  * u64 variable to optimize the size.
362b2d3d61aSDaniel Lezcano  * 48 bit time stamp and 16 bit IRQ number is way sufficient.
363b2d3d61aSDaniel Lezcano  *  Who cares an IRQ after 78 hours of idle time?
364b2d3d61aSDaniel Lezcano  */
irq_timing_encode(u64 timestamp,int irq)365b2d3d61aSDaniel Lezcano static inline u64 irq_timing_encode(u64 timestamp, int irq)
366b2d3d61aSDaniel Lezcano {
367b2d3d61aSDaniel Lezcano 	return (timestamp << 16) | irq;
368b2d3d61aSDaniel Lezcano }
369b2d3d61aSDaniel Lezcano 
irq_timing_decode(u64 value,u64 * timestamp)370b2d3d61aSDaniel Lezcano static inline int irq_timing_decode(u64 value, u64 *timestamp)
371b2d3d61aSDaniel Lezcano {
372b2d3d61aSDaniel Lezcano 	*timestamp = value >> 16;
373b2d3d61aSDaniel Lezcano 	return value & U16_MAX;
374b2d3d61aSDaniel Lezcano }
375b2d3d61aSDaniel Lezcano 
irq_timings_push(u64 ts,int irq)376df025e47SDaniel Lezcano static __always_inline void irq_timings_push(u64 ts, int irq)
377df025e47SDaniel Lezcano {
378df025e47SDaniel Lezcano 	struct irq_timings *timings = this_cpu_ptr(&irq_timings);
379df025e47SDaniel Lezcano 
380df025e47SDaniel Lezcano 	timings->values[timings->count & IRQ_TIMINGS_MASK] =
381df025e47SDaniel Lezcano 		irq_timing_encode(ts, irq);
382df025e47SDaniel Lezcano 
383df025e47SDaniel Lezcano 	timings->count++;
384df025e47SDaniel Lezcano }
385df025e47SDaniel Lezcano 
386b2d3d61aSDaniel Lezcano /*
387b2d3d61aSDaniel Lezcano  * The function record_irq_time is only called in one place in the
388b2d3d61aSDaniel Lezcano  * interrupts handler. We want this function always inline so the code
389b2d3d61aSDaniel Lezcano  * inside is embedded in the function and the static key branching
390b2d3d61aSDaniel Lezcano  * code can act at the higher level. Without the explicit
391b2d3d61aSDaniel Lezcano  * __always_inline we can end up with a function call and a small
392b2d3d61aSDaniel Lezcano  * overhead in the hotpath for nothing.
393b2d3d61aSDaniel Lezcano  */
record_irq_time(struct irq_desc * desc)394b2d3d61aSDaniel Lezcano static __always_inline void record_irq_time(struct irq_desc *desc)
395b2d3d61aSDaniel Lezcano {
396b2d3d61aSDaniel Lezcano 	if (!static_branch_likely(&irq_timing_enabled))
397b2d3d61aSDaniel Lezcano 		return;
398b2d3d61aSDaniel Lezcano 
399df025e47SDaniel Lezcano 	if (desc->istate & IRQS_TIMINGS)
400df025e47SDaniel Lezcano 		irq_timings_push(local_clock(), irq_desc_get_irq(desc));
401b2d3d61aSDaniel Lezcano }
402b2d3d61aSDaniel Lezcano #else
irq_remove_timings(struct irq_desc * desc)403b2d3d61aSDaniel Lezcano static inline void irq_remove_timings(struct irq_desc *desc) {}
irq_setup_timings(struct irq_desc * desc,struct irqaction * act)404b2d3d61aSDaniel Lezcano static inline void irq_setup_timings(struct irq_desc *desc,
405b2d3d61aSDaniel Lezcano 				     struct irqaction *act) {};
record_irq_time(struct irq_desc * desc)406b2d3d61aSDaniel Lezcano static inline void record_irq_time(struct irq_desc *desc) {}
407b2d3d61aSDaniel Lezcano #endif /* CONFIG_IRQ_TIMINGS */
408b2d3d61aSDaniel Lezcano 
409b2d3d61aSDaniel Lezcano 
410f1602039SBartosz Golaszewski #ifdef CONFIG_GENERIC_IRQ_CHIP
411f1602039SBartosz Golaszewski void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
412f1602039SBartosz Golaszewski 			   int num_ct, unsigned int irq_base,
413f1602039SBartosz Golaszewski 			   void __iomem *reg_base, irq_flow_handler_t handler);
414f1602039SBartosz Golaszewski #else
415f1602039SBartosz Golaszewski static inline void
irq_init_generic_chip(struct irq_chip_generic * gc,const char * name,int num_ct,unsigned int irq_base,void __iomem * reg_base,irq_flow_handler_t handler)416f1602039SBartosz Golaszewski irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
417f1602039SBartosz Golaszewski 		      int num_ct, unsigned int irq_base,
418f1602039SBartosz Golaszewski 		      void __iomem *reg_base, irq_flow_handler_t handler) { }
419f1602039SBartosz Golaszewski #endif /* CONFIG_GENERIC_IRQ_CHIP */
420087cdfb6SThomas Gleixner 
421137221dfSChristoph Hellwig #ifdef CONFIG_GENERIC_PENDING_IRQ
irq_can_move_pcntxt(struct irq_data * data)422137221dfSChristoph Hellwig static inline bool irq_can_move_pcntxt(struct irq_data *data)
423137221dfSChristoph Hellwig {
424137221dfSChristoph Hellwig 	return irqd_can_move_in_process_context(data);
425137221dfSChristoph Hellwig }
irq_move_pending(struct irq_data * data)426137221dfSChristoph Hellwig static inline bool irq_move_pending(struct irq_data *data)
427137221dfSChristoph Hellwig {
428137221dfSChristoph Hellwig 	return irqd_is_setaffinity_pending(data);
429137221dfSChristoph Hellwig }
430137221dfSChristoph Hellwig static inline void
irq_copy_pending(struct irq_desc * desc,const struct cpumask * mask)431137221dfSChristoph Hellwig irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
432137221dfSChristoph Hellwig {
433137221dfSChristoph Hellwig 	cpumask_copy(desc->pending_mask, mask);
434137221dfSChristoph Hellwig }
435137221dfSChristoph Hellwig static inline void
irq_get_pending(struct cpumask * mask,struct irq_desc * desc)436137221dfSChristoph Hellwig irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
437137221dfSChristoph Hellwig {
438137221dfSChristoph Hellwig 	cpumask_copy(mask, desc->pending_mask);
439137221dfSChristoph Hellwig }
irq_desc_get_pending_mask(struct irq_desc * desc)440f0383c24SThomas Gleixner static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc)
441f0383c24SThomas Gleixner {
442f0383c24SThomas Gleixner 	return desc->pending_mask;
443f0383c24SThomas Gleixner }
handle_enforce_irqctx(struct irq_data * data)444c16816acSThomas Gleixner static inline bool handle_enforce_irqctx(struct irq_data *data)
445c16816acSThomas Gleixner {
446c16816acSThomas Gleixner 	return irqd_is_handle_enforce_irqctx(data);
447c16816acSThomas Gleixner }
44836d84fb4SThomas Gleixner bool irq_fixup_move_pending(struct irq_desc *desc, bool force_clear);
449137221dfSChristoph Hellwig #else /* CONFIG_GENERIC_PENDING_IRQ */
irq_can_move_pcntxt(struct irq_data * data)450137221dfSChristoph Hellwig static inline bool irq_can_move_pcntxt(struct irq_data *data)
451137221dfSChristoph Hellwig {
452137221dfSChristoph Hellwig 	return true;
453137221dfSChristoph Hellwig }
irq_move_pending(struct irq_data * data)454137221dfSChristoph Hellwig static inline bool irq_move_pending(struct irq_data *data)
455137221dfSChristoph Hellwig {
456137221dfSChristoph Hellwig 	return false;
457137221dfSChristoph Hellwig }
458137221dfSChristoph Hellwig static inline void
irq_copy_pending(struct irq_desc * desc,const struct cpumask * mask)459137221dfSChristoph Hellwig irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
460137221dfSChristoph Hellwig {
461137221dfSChristoph Hellwig }
462137221dfSChristoph Hellwig static inline void
irq_get_pending(struct cpumask * mask,struct irq_desc * desc)463137221dfSChristoph Hellwig irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
464137221dfSChristoph Hellwig {
465137221dfSChristoph Hellwig }
irq_desc_get_pending_mask(struct irq_desc * desc)466f0383c24SThomas Gleixner static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc)
467f0383c24SThomas Gleixner {
468f0383c24SThomas Gleixner 	return NULL;
469f0383c24SThomas Gleixner }
irq_fixup_move_pending(struct irq_desc * desc,bool fclear)47036d84fb4SThomas Gleixner static inline bool irq_fixup_move_pending(struct irq_desc *desc, bool fclear)
47136d84fb4SThomas Gleixner {
47236d84fb4SThomas Gleixner 	return false;
47336d84fb4SThomas Gleixner }
handle_enforce_irqctx(struct irq_data * data)474c16816acSThomas Gleixner static inline bool handle_enforce_irqctx(struct irq_data *data)
475c16816acSThomas Gleixner {
476c16816acSThomas Gleixner 	return false;
477c16816acSThomas Gleixner }
478f0383c24SThomas Gleixner #endif /* !CONFIG_GENERIC_PENDING_IRQ */
479137221dfSChristoph Hellwig 
480457f6d35SThomas Gleixner #if !defined(CONFIG_IRQ_DOMAIN) || !defined(CONFIG_IRQ_DOMAIN_HIERARCHY)
irq_domain_activate_irq(struct irq_data * data,bool reserve)481702cb0a0SThomas Gleixner static inline int irq_domain_activate_irq(struct irq_data *data, bool reserve)
482457f6d35SThomas Gleixner {
483457f6d35SThomas Gleixner 	irqd_set_activated(data);
484bb9b428aSThomas Gleixner 	return 0;
485457f6d35SThomas Gleixner }
irq_domain_deactivate_irq(struct irq_data * data)486457f6d35SThomas Gleixner static inline void irq_domain_deactivate_irq(struct irq_data *data)
487457f6d35SThomas Gleixner {
488457f6d35SThomas Gleixner 	irqd_clr_activated(data);
489457f6d35SThomas Gleixner }
490457f6d35SThomas Gleixner #endif
491457f6d35SThomas Gleixner 
irqd_get_parent_data(struct irq_data * irqd)49213b90cadSThomas Gleixner static inline struct irq_data *irqd_get_parent_data(struct irq_data *irqd)
49313b90cadSThomas Gleixner {
49413b90cadSThomas Gleixner #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
49513b90cadSThomas Gleixner 	return irqd->parent_data;
49613b90cadSThomas Gleixner #else
49713b90cadSThomas Gleixner 	return NULL;
49813b90cadSThomas Gleixner #endif
49913b90cadSThomas Gleixner }
50013b90cadSThomas Gleixner 
501087cdfb6SThomas Gleixner #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
502c2ce34c0SThomas Gleixner #include <linux/debugfs.h>
503c2ce34c0SThomas Gleixner 
504087cdfb6SThomas Gleixner void irq_add_debugfs_entry(unsigned int irq, struct irq_desc *desc);
irq_remove_debugfs_entry(struct irq_desc * desc)505c2ce34c0SThomas Gleixner static inline void irq_remove_debugfs_entry(struct irq_desc *desc)
506c2ce34c0SThomas Gleixner {
507c2ce34c0SThomas Gleixner 	debugfs_remove(desc->debugfs_file);
50807557ccbSThomas Gleixner 	kfree(desc->dev_name);
509c2ce34c0SThomas Gleixner }
51007557ccbSThomas Gleixner void irq_debugfs_copy_devname(int irq, struct device *dev);
511087cdfb6SThomas Gleixner # ifdef CONFIG_IRQ_DOMAIN
512087cdfb6SThomas Gleixner void irq_domain_debugfs_init(struct dentry *root);
513087cdfb6SThomas Gleixner # else
irq_domain_debugfs_init(struct dentry * root)514e5682b4eSSebastian Ott static inline void irq_domain_debugfs_init(struct dentry *root)
515e5682b4eSSebastian Ott {
516e5682b4eSSebastian Ott }
517087cdfb6SThomas Gleixner # endif
518087cdfb6SThomas Gleixner #else /* CONFIG_GENERIC_IRQ_DEBUGFS */
irq_add_debugfs_entry(unsigned int irq,struct irq_desc * d)519087cdfb6SThomas Gleixner static inline void irq_add_debugfs_entry(unsigned int irq, struct irq_desc *d)
520087cdfb6SThomas Gleixner {
521087cdfb6SThomas Gleixner }
irq_remove_debugfs_entry(struct irq_desc * d)522087cdfb6SThomas Gleixner static inline void irq_remove_debugfs_entry(struct irq_desc *d)
523087cdfb6SThomas Gleixner {
524087cdfb6SThomas Gleixner }
irq_debugfs_copy_devname(int irq,struct device * dev)52507557ccbSThomas Gleixner static inline void irq_debugfs_copy_devname(int irq, struct device *dev)
52607557ccbSThomas Gleixner {
52707557ccbSThomas Gleixner }
528087cdfb6SThomas Gleixner #endif /* CONFIG_GENERIC_IRQ_DEBUGFS */
529