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