xref: /linux/kernel/irq/manage.c (revision 89a9e003a2ef73f67dff03dfd59e0a5474b56b0b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4  * Copyright (C) 2005-2006 Thomas Gleixner
5  *
6  * This file contains driver APIs to the irq subsystem.
7  */
8 
9 #define pr_fmt(fmt) "genirq: " fmt
10 
11 #include <linux/irq.h>
12 #include <linux/kthread.h>
13 #include <linux/module.h>
14 #include <linux/random.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/sched/rt.h>
20 #include <linux/sched/task.h>
21 #include <linux/sched/isolation.h>
22 #include <uapi/linux/sched/types.h>
23 #include <linux/task_work.h>
24 #include <linux/wait_bit.h>
25 
26 #include "internals.h"
27 
28 #if defined(CONFIG_IRQ_FORCED_THREADING) && !defined(CONFIG_PREEMPT_RT)
29 DEFINE_STATIC_KEY_FALSE(force_irqthreads_key);
30 
31 static int __init setup_forced_irqthreads(char *arg)
32 {
33 	static_branch_enable(&force_irqthreads_key);
34 	return 0;
35 }
36 early_param("threadirqs", setup_forced_irqthreads);
37 #endif
38 
39 #ifdef CONFIG_SMP
40 static inline void synchronize_irqwork(struct irq_desc *desc)
41 {
42 	/* Synchronize pending or on the fly redirect work */
43 	irq_work_sync(&desc->redirect.work);
44 }
45 #else
46 static inline void synchronize_irqwork(struct irq_desc *desc) { }
47 #endif
48 
49 static int __irq_get_irqchip_state(struct irq_data *d, enum irqchip_irq_state which, bool *state);
50 
51 static void __synchronize_hardirq(struct irq_desc *desc, bool sync_chip)
52 {
53 	struct irq_data *irqd = irq_desc_get_irq_data(desc);
54 	bool inprogress;
55 
56 	do {
57 		/*
58 		 * Wait until we're out of the critical section.  This might
59 		 * give the wrong answer due to the lack of memory barriers.
60 		 */
61 		while (irqd_irq_inprogress(&desc->irq_data))
62 			cpu_relax();
63 
64 		/* Ok, that indicated we're done: double-check carefully. */
65 		guard(raw_spinlock_irqsave)(&desc->lock);
66 		inprogress = irqd_irq_inprogress(&desc->irq_data);
67 
68 		/*
69 		 * If requested and supported, check at the chip whether it
70 		 * is in flight at the hardware level, i.e. already pending
71 		 * in a CPU and waiting for service and acknowledge.
72 		 */
73 		if (!inprogress && sync_chip) {
74 			/*
75 			 * Ignore the return code. inprogress is only updated
76 			 * when the chip supports it.
77 			 */
78 			__irq_get_irqchip_state(irqd, IRQCHIP_STATE_ACTIVE,
79 						&inprogress);
80 		}
81 		/* Oops, that failed? */
82 	} while (inprogress);
83 }
84 
85 /**
86  * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
87  * @irq: interrupt number to wait for
88  *
89  * This function waits for any pending hard IRQ handlers for this interrupt
90  * to complete before returning. If you use this function while holding a
91  * resource the IRQ handler may need you will deadlock. It does not take
92  * associated threaded handlers into account.
93  *
94  * Do not use this for shutdown scenarios where you must be sure that all
95  * parts (hardirq and threaded handler) have completed.
96  *
97  * Returns: false if a threaded handler is active.
98  *
99  * This function may be called - with care - from IRQ context.
100  *
101  * It does not check whether there is an interrupt in flight at the
102  * hardware level, but not serviced yet, as this might deadlock when called
103  * with interrupts disabled and the target CPU of the interrupt is the
104  * current CPU.
105  */
106 bool synchronize_hardirq(unsigned int irq)
107 {
108 	struct irq_desc *desc = irq_to_desc(irq);
109 
110 	if (desc) {
111 		__synchronize_hardirq(desc, false);
112 		return !atomic_read(&desc->threads_active);
113 	}
114 
115 	return true;
116 }
117 EXPORT_SYMBOL(synchronize_hardirq);
118 
119 static void __synchronize_irq(struct irq_desc *desc)
120 {
121 	synchronize_irqwork(desc);
122 	__synchronize_hardirq(desc, true);
123 
124 	/*
125 	 * We made sure that no hardirq handler is running. Now verify that no
126 	 * threaded handlers are active.
127 	 */
128 	wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
129 }
130 
131 /**
132  * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
133  * @irq: interrupt number to wait for
134  *
135  * This function waits for any pending IRQ handlers for this interrupt to
136  * complete before returning. If you use this function while holding a
137  * resource the IRQ handler may need you will deadlock.
138  *
139  * Can only be called from preemptible code as it might sleep when
140  * an interrupt thread is associated to @irq.
141  *
142  * It optionally makes sure (when the irq chip supports that method)
143  * that the interrupt is not pending in any CPU and waiting for
144  * service.
145  */
146 void synchronize_irq(unsigned int irq)
147 {
148 	struct irq_desc *desc = irq_to_desc(irq);
149 
150 	if (desc)
151 		__synchronize_irq(desc);
152 }
153 EXPORT_SYMBOL(synchronize_irq);
154 
155 #ifdef CONFIG_SMP
156 cpumask_var_t irq_default_affinity;
157 
158 static bool __irq_can_set_affinity(struct irq_desc *desc)
159 {
160 	if (!desc || !irqd_can_balance(&desc->irq_data) ||
161 	    !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
162 		return false;
163 	return true;
164 }
165 
166 /**
167  * irq_can_set_affinity - Check if the affinity of a given irq can be set
168  * @irq:	Interrupt to check
169  *
170  */
171 int irq_can_set_affinity(unsigned int irq)
172 {
173 	return __irq_can_set_affinity(irq_to_desc(irq));
174 }
175 EXPORT_SYMBOL_GPL(irq_can_set_affinity);
176 
177 /**
178  * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
179  * @irq:	Interrupt to check
180  *
181  * Like irq_can_set_affinity() above, but additionally checks for the
182  * AFFINITY_MANAGED flag.
183  */
184 bool irq_can_set_affinity_usr(unsigned int irq)
185 {
186 	struct irq_desc *desc = irq_to_desc(irq);
187 
188 	return __irq_can_set_affinity(desc) &&
189 		!irqd_affinity_is_managed(&desc->irq_data);
190 }
191 
192 /**
193  * irq_set_thread_affinity - Notify irq threads to adjust affinity
194  * @desc:	irq descriptor which has affinity changed
195  *
196  * Just set IRQTF_AFFINITY and delegate the affinity setting to the
197  * interrupt thread itself. We can not call set_cpus_allowed_ptr() here as
198  * we hold desc->lock and this code can be called from hard interrupt
199  * context.
200  */
201 static void irq_set_thread_affinity(struct irq_desc *desc)
202 {
203 	struct irqaction *action;
204 
205 	for_each_action_of_desc(desc, action) {
206 		if (action->thread) {
207 			set_bit(IRQTF_AFFINITY, &action->thread_flags);
208 			wake_up_process(action->thread);
209 		}
210 		if (action->secondary && action->secondary->thread) {
211 			set_bit(IRQTF_AFFINITY, &action->secondary->thread_flags);
212 			wake_up_process(action->secondary->thread);
213 		}
214 	}
215 }
216 
217 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
218 static void irq_validate_effective_affinity(struct irq_data *data)
219 {
220 	const struct cpumask *m = irq_data_get_effective_affinity_mask(data);
221 	struct irq_chip *chip = irq_data_get_irq_chip(data);
222 
223 	if (!cpumask_empty(m))
224 		return;
225 	pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
226 		     chip->name, data->irq);
227 }
228 #else
229 static inline void irq_validate_effective_affinity(struct irq_data *data) { }
230 #endif
231 
232 static DEFINE_PER_CPU(struct cpumask, __tmp_mask);
233 
234 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
235 {
236 	struct cpumask *tmp_mask = this_cpu_ptr(&__tmp_mask);
237 	struct irq_desc *desc = irq_data_to_desc(data);
238 	struct irq_chip *chip = irq_data_get_irq_chip(data);
239 	const struct cpumask  *prog_mask;
240 	int ret;
241 
242 	if (!chip || !chip->irq_set_affinity)
243 		return -EINVAL;
244 
245 	/*
246 	 * If this is a managed interrupt and housekeeping is enabled on
247 	 * it check whether the requested affinity mask intersects with
248 	 * a housekeeping CPU. If so, then remove the isolated CPUs from
249 	 * the mask and just keep the housekeeping CPU(s). This prevents
250 	 * the affinity setter from routing the interrupt to an isolated
251 	 * CPU to avoid that I/O submitted from a housekeeping CPU causes
252 	 * interrupts on an isolated one.
253 	 *
254 	 * If the masks do not intersect or include online CPU(s) then
255 	 * keep the requested mask. The isolated target CPUs are only
256 	 * receiving interrupts when the I/O operation was submitted
257 	 * directly from them.
258 	 *
259 	 * If all housekeeping CPUs in the affinity mask are offline, the
260 	 * interrupt will be migrated by the CPU hotplug code once a
261 	 * housekeeping CPU which belongs to the affinity mask comes
262 	 * online.
263 	 */
264 	if (irqd_affinity_is_managed(data) &&
265 	    housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) {
266 		const struct cpumask *hk_mask;
267 
268 		hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
269 
270 		cpumask_and(tmp_mask, mask, hk_mask);
271 		if (!cpumask_intersects(tmp_mask, cpu_online_mask))
272 			prog_mask = mask;
273 		else
274 			prog_mask = tmp_mask;
275 	} else {
276 		prog_mask = mask;
277 	}
278 
279 	/*
280 	 * Make sure we only provide online CPUs to the irqchip,
281 	 * unless we are being asked to force the affinity (in which
282 	 * case we do as we are told).
283 	 */
284 	cpumask_and(tmp_mask, prog_mask, cpu_online_mask);
285 	if (!force && !cpumask_empty(tmp_mask))
286 		ret = chip->irq_set_affinity(data, tmp_mask, force);
287 	else if (force)
288 		ret = chip->irq_set_affinity(data, mask, force);
289 	else
290 		ret = -EINVAL;
291 
292 	switch (ret) {
293 	case IRQ_SET_MASK_OK:
294 	case IRQ_SET_MASK_OK_DONE:
295 		cpumask_copy(desc->irq_common_data.affinity, mask);
296 		fallthrough;
297 	case IRQ_SET_MASK_OK_NOCOPY:
298 		irq_validate_effective_affinity(data);
299 		irq_set_thread_affinity(desc);
300 		ret = 0;
301 	}
302 
303 	return ret;
304 }
305 
306 #ifdef CONFIG_GENERIC_PENDING_IRQ
307 static inline int irq_set_affinity_pending(struct irq_data *data,
308 					   const struct cpumask *dest)
309 {
310 	struct irq_desc *desc = irq_data_to_desc(data);
311 
312 	irqd_set_move_pending(data);
313 	irq_copy_pending(desc, dest);
314 	return 0;
315 }
316 #else
317 static inline int irq_set_affinity_pending(struct irq_data *data,
318 					   const struct cpumask *dest)
319 {
320 	return -EBUSY;
321 }
322 #endif
323 
324 static int irq_try_set_affinity(struct irq_data *data,
325 				const struct cpumask *dest, bool force)
326 {
327 	int ret = irq_do_set_affinity(data, dest, force);
328 
329 	/*
330 	 * In case that the underlying vector management is busy and the
331 	 * architecture supports the generic pending mechanism then utilize
332 	 * this to avoid returning an error to user space.
333 	 */
334 	if (ret == -EBUSY && !force)
335 		ret = irq_set_affinity_pending(data, dest);
336 	return ret;
337 }
338 
339 static bool irq_set_affinity_deactivated(struct irq_data *data,
340 					 const struct cpumask *mask)
341 {
342 	struct irq_desc *desc = irq_data_to_desc(data);
343 
344 	/*
345 	 * Handle irq chips which can handle affinity only in activated
346 	 * state correctly
347 	 *
348 	 * If the interrupt is not yet activated, just store the affinity
349 	 * mask and do not call the chip driver at all. On activation the
350 	 * driver has to make sure anyway that the interrupt is in a
351 	 * usable state so startup works.
352 	 */
353 	if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) ||
354 	    irqd_is_activated(data) || !irqd_affinity_on_activate(data))
355 		return false;
356 
357 	cpumask_copy(desc->irq_common_data.affinity, mask);
358 	irq_data_update_effective_affinity(data, mask);
359 	irqd_set(data, IRQD_AFFINITY_SET);
360 	return true;
361 }
362 
363 /**
364  * irq_affinity_schedule_notify_work - Schedule work to notify about affinity change
365  * @desc:  Interrupt descriptor whose affinity changed
366  */
367 void irq_affinity_schedule_notify_work(struct irq_desc *desc)
368 {
369 	lockdep_assert_held(&desc->lock);
370 
371 	kref_get(&desc->affinity_notify->kref);
372 	if (!schedule_work(&desc->affinity_notify->work)) {
373 		/* Work was already scheduled, drop our extra ref */
374 		kref_put(&desc->affinity_notify->kref, desc->affinity_notify->release);
375 	}
376 }
377 
378 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
379 			    bool force)
380 {
381 	struct irq_chip *chip = irq_data_get_irq_chip(data);
382 	struct irq_desc *desc = irq_data_to_desc(data);
383 	int ret = 0;
384 
385 	if (!chip || !chip->irq_set_affinity)
386 		return -EINVAL;
387 
388 	if (irq_set_affinity_deactivated(data, mask))
389 		return 0;
390 
391 	if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
392 		ret = irq_try_set_affinity(data, mask, force);
393 	} else {
394 		irqd_set_move_pending(data);
395 		irq_copy_pending(desc, mask);
396 	}
397 
398 	if (desc->affinity_notify)
399 		irq_affinity_schedule_notify_work(desc);
400 
401 	irqd_set(data, IRQD_AFFINITY_SET);
402 
403 	return ret;
404 }
405 
406 /**
407  * irq_update_affinity_desc - Update affinity management for an interrupt
408  * @irq:	The interrupt number to update
409  * @affinity:	Pointer to the affinity descriptor
410  *
411  * This interface can be used to configure the affinity management of
412  * interrupts which have been allocated already.
413  *
414  * There are certain limitations on when it may be used - attempts to use it
415  * for when the kernel is configured for generic IRQ reservation mode (in
416  * config GENERIC_IRQ_RESERVATION_MODE) will fail, as it may conflict with
417  * managed/non-managed interrupt accounting. In addition, attempts to use it on
418  * an interrupt which is already started or which has already been configured
419  * as managed will also fail, as these mean invalid init state or double init.
420  */
421 int irq_update_affinity_desc(unsigned int irq, struct irq_affinity_desc *affinity)
422 {
423 	/*
424 	 * Supporting this with the reservation scheme used by x86 needs
425 	 * some more thought. Fail it for now.
426 	 */
427 	if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
428 		return -EOPNOTSUPP;
429 
430 	scoped_irqdesc_get_and_buslock(irq, 0) {
431 		struct irq_desc *desc = scoped_irqdesc;
432 		bool activated;
433 
434 		/* Requires the interrupt to be shut down */
435 		if (irqd_is_started(&desc->irq_data))
436 			return -EBUSY;
437 
438 		/* Interrupts which are already managed cannot be modified */
439 		if (irqd_affinity_is_managed(&desc->irq_data))
440 			return -EBUSY;
441 		/*
442 		 * Deactivate the interrupt. That's required to undo
443 		 * anything an earlier activation has established.
444 		 */
445 		activated = irqd_is_activated(&desc->irq_data);
446 		if (activated)
447 			irq_domain_deactivate_irq(&desc->irq_data);
448 
449 		if (affinity->is_managed) {
450 			irqd_set(&desc->irq_data, IRQD_AFFINITY_MANAGED);
451 			irqd_set(&desc->irq_data, IRQD_MANAGED_SHUTDOWN);
452 		}
453 
454 		cpumask_copy(desc->irq_common_data.affinity, &affinity->mask);
455 
456 		/* Restore the activation state */
457 		if (activated)
458 			irq_domain_activate_irq(&desc->irq_data, false);
459 		return 0;
460 	}
461 	return -EINVAL;
462 }
463 
464 static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask,
465 			      bool force)
466 {
467 	struct irq_desc *desc = irq_to_desc(irq);
468 
469 	if (!desc)
470 		return -EINVAL;
471 
472 	guard(raw_spinlock_irqsave)(&desc->lock);
473 	return irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
474 }
475 
476 /**
477  * irq_set_affinity - Set the irq affinity of a given irq
478  * @irq:	Interrupt to set affinity
479  * @cpumask:	cpumask
480  *
481  * Fails if cpumask does not contain an online CPU
482  */
483 int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
484 {
485 	return __irq_set_affinity(irq, cpumask, false);
486 }
487 EXPORT_SYMBOL_GPL(irq_set_affinity);
488 
489 /**
490  * irq_force_affinity - Force the irq affinity of a given irq
491  * @irq:	Interrupt to set affinity
492  * @cpumask:	cpumask
493  *
494  * Same as irq_set_affinity, but without checking the mask against
495  * online cpus.
496  *
497  * Solely for low level cpu hotplug code, where we need to make per
498  * cpu interrupts affine before the cpu becomes online.
499  */
500 int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
501 {
502 	return __irq_set_affinity(irq, cpumask, true);
503 }
504 EXPORT_SYMBOL_GPL(irq_force_affinity);
505 
506 int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool setaffinity)
507 {
508 	int ret = -EINVAL;
509 
510 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_GLOBAL) {
511 		scoped_irqdesc->affinity_hint = m;
512 		ret = 0;
513 	}
514 
515 	if (!ret && m && setaffinity)
516 		__irq_set_affinity(irq, m, false);
517 	return ret;
518 }
519 EXPORT_SYMBOL_GPL(__irq_apply_affinity_hint);
520 
521 static void irq_affinity_notify(struct work_struct *work)
522 {
523 	struct irq_affinity_notify *notify = container_of(work, struct irq_affinity_notify, work);
524 	struct irq_desc *desc = irq_to_desc(notify->irq);
525 	cpumask_var_t cpumask;
526 
527 	if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
528 		goto out;
529 
530 	scoped_guard(raw_spinlock_irqsave, &desc->lock) {
531 		if (irq_move_pending(&desc->irq_data))
532 			irq_get_pending(cpumask, desc);
533 		else
534 			cpumask_copy(cpumask, desc->irq_common_data.affinity);
535 	}
536 
537 	notify->notify(notify, cpumask);
538 
539 	free_cpumask_var(cpumask);
540 out:
541 	kref_put(&notify->kref, notify->release);
542 }
543 
544 /**
545  * irq_set_affinity_notifier - control notification of IRQ affinity changes
546  * @irq:	Interrupt for which to enable/disable notification
547  * @notify:	Context for notification, or %NULL to disable
548  *		notification.  Function pointers must be initialised;
549  *		the other fields will be initialised by this function.
550  *
551  * Must be called in process context.  Notification may only be enabled
552  * after the IRQ is allocated and must be disabled before the IRQ is freed
553  * using free_irq().
554  */
555 int irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
556 {
557 	struct irq_desc *desc = irq_to_desc(irq);
558 	struct irq_affinity_notify *old_notify;
559 
560 	/* The release function is promised process context */
561 	might_sleep();
562 
563 	if (!desc || irq_is_nmi(desc))
564 		return -EINVAL;
565 
566 	/* Complete initialisation of *notify */
567 	if (notify) {
568 		notify->irq = irq;
569 		kref_init(&notify->kref);
570 		INIT_WORK(&notify->work, irq_affinity_notify);
571 	}
572 
573 	scoped_guard(raw_spinlock_irq, &desc->lock) {
574 		old_notify = desc->affinity_notify;
575 		desc->affinity_notify = notify;
576 	}
577 
578 	if (old_notify) {
579 		if (cancel_work_sync(&old_notify->work)) {
580 			/* Pending work had a ref, put that one too */
581 			kref_put(&old_notify->kref, old_notify->release);
582 		}
583 		kref_put(&old_notify->kref, old_notify->release);
584 	}
585 
586 	return 0;
587 }
588 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
589 
590 #ifndef CONFIG_AUTO_IRQ_AFFINITY
591 /*
592  * Generic version of the affinity autoselector.
593  */
594 int irq_setup_affinity(struct irq_desc *desc)
595 {
596 	struct cpumask *set = irq_default_affinity;
597 	int node = irq_desc_get_node(desc);
598 
599 	static DEFINE_RAW_SPINLOCK(mask_lock);
600 	static struct cpumask mask;
601 
602 	/* Excludes PER_CPU and NO_BALANCE interrupts */
603 	if (!__irq_can_set_affinity(desc))
604 		return 0;
605 
606 	guard(raw_spinlock)(&mask_lock);
607 	/*
608 	 * Preserve the managed affinity setting and a userspace affinity
609 	 * setup, but make sure that one of the targets is online.
610 	 */
611 	if (irqd_affinity_is_managed(&desc->irq_data) ||
612 	    irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
613 		if (cpumask_intersects(desc->irq_common_data.affinity,
614 				       cpu_online_mask))
615 			set = desc->irq_common_data.affinity;
616 		else
617 			irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
618 	}
619 
620 	cpumask_and(&mask, cpu_online_mask, set);
621 	if (cpumask_empty(&mask))
622 		cpumask_copy(&mask, cpu_online_mask);
623 
624 	if (node != NUMA_NO_NODE) {
625 		const struct cpumask *nodemask = cpumask_of_node(node);
626 
627 		/* make sure at least one of the cpus in nodemask is online */
628 		if (cpumask_intersects(&mask, nodemask))
629 			cpumask_and(&mask, &mask, nodemask);
630 	}
631 	return irq_do_set_affinity(&desc->irq_data, &mask, false);
632 }
633 #else
634 /* Wrapper for ALPHA specific affinity selector magic */
635 int irq_setup_affinity(struct irq_desc *desc)
636 {
637 	return irq_select_affinity(irq_desc_get_irq(desc));
638 }
639 #endif /* CONFIG_AUTO_IRQ_AFFINITY */
640 #endif /* CONFIG_SMP */
641 
642 
643 /**
644  * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
645  * @irq:	interrupt number to set affinity
646  * @vcpu_info:	vCPU specific data or pointer to a percpu array of vCPU
647  *		specific data for percpu_devid interrupts
648  *
649  * This function uses the vCPU specific data to set the vCPU affinity for
650  * an irq. The vCPU specific data is passed from outside, such as KVM. One
651  * example code path is as below: KVM -> IOMMU -> irq_set_vcpu_affinity().
652  */
653 int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
654 {
655 	scoped_irqdesc_get_and_lock(irq, 0) {
656 		struct irq_desc *desc = scoped_irqdesc;
657 		struct irq_data *data;
658 		struct irq_chip *chip;
659 
660 		data = irq_desc_get_irq_data(desc);
661 		do {
662 			chip = irq_data_get_irq_chip(data);
663 			if (chip && chip->irq_set_vcpu_affinity)
664 				break;
665 
666 			data = irqd_get_parent_data(data);
667 		} while (data);
668 
669 		if (!data)
670 			return -ENOSYS;
671 		return chip->irq_set_vcpu_affinity(data, vcpu_info);
672 	}
673 	return -EINVAL;
674 }
675 EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
676 
677 void __disable_irq(struct irq_desc *desc)
678 {
679 	if (!desc->depth++)
680 		irq_disable(desc);
681 }
682 
683 static int __disable_irq_nosync(unsigned int irq)
684 {
685 	scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) {
686 		__disable_irq(scoped_irqdesc);
687 		return 0;
688 	}
689 	return -EINVAL;
690 }
691 
692 /**
693  * disable_irq_nosync - disable an irq without waiting
694  * @irq: Interrupt to disable
695  *
696  * Disable the selected interrupt line.  Disables and Enables are
697  * nested.
698  * Unlike disable_irq(), this function does not ensure existing
699  * instances of the IRQ handler have completed before returning.
700  *
701  * This function may be called from IRQ context.
702  */
703 void disable_irq_nosync(unsigned int irq)
704 {
705 	__disable_irq_nosync(irq);
706 }
707 EXPORT_SYMBOL(disable_irq_nosync);
708 
709 /**
710  * disable_irq - disable an irq and wait for completion
711  * @irq: Interrupt to disable
712  *
713  * Disable the selected interrupt line.  Enables and Disables are nested.
714  *
715  * This function waits for any pending IRQ handlers for this interrupt to
716  * complete before returning. If you use this function while holding a
717  * resource the IRQ handler may need you will deadlock.
718  *
719  * Can only be called from preemptible code as it might sleep when an
720  * interrupt thread is associated to @irq.
721  *
722  */
723 void disable_irq(unsigned int irq)
724 {
725 	might_sleep();
726 	if (!__disable_irq_nosync(irq))
727 		synchronize_irq(irq);
728 }
729 EXPORT_SYMBOL(disable_irq);
730 
731 /**
732  * disable_hardirq - disables an irq and waits for hardirq completion
733  * @irq: Interrupt to disable
734  *
735  * Disable the selected interrupt line.  Enables and Disables are nested.
736  *
737  * This function waits for any pending hard IRQ handlers for this interrupt
738  * to complete before returning. If you use this function while holding a
739  * resource the hard IRQ handler may need you will deadlock.
740  *
741  * When used to optimistically disable an interrupt from atomic context the
742  * return value must be checked.
743  *
744  * Returns: false if a threaded handler is active.
745  *
746  * This function may be called - with care - from IRQ context.
747  */
748 bool disable_hardirq(unsigned int irq)
749 {
750 	if (!__disable_irq_nosync(irq))
751 		return synchronize_hardirq(irq);
752 	return false;
753 }
754 EXPORT_SYMBOL_GPL(disable_hardirq);
755 
756 /**
757  * disable_nmi_nosync - disable an nmi without waiting
758  * @irq: Interrupt to disable
759  *
760  * Disable the selected interrupt line. Disables and enables are nested.
761  *
762  * The interrupt to disable must have been requested through request_nmi.
763  * Unlike disable_nmi(), this function does not ensure existing
764  * instances of the IRQ handler have completed before returning.
765  */
766 void disable_nmi_nosync(unsigned int irq)
767 {
768 	disable_irq_nosync(irq);
769 }
770 
771 void __enable_irq(struct irq_desc *desc)
772 {
773 	switch (desc->depth) {
774 	case 0:
775  err_out:
776 		WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
777 		     irq_desc_get_irq(desc));
778 		break;
779 	case 1: {
780 		if (desc->istate & IRQS_SUSPENDED)
781 			goto err_out;
782 		/* Prevent probing on this irq: */
783 		irq_settings_set_noprobe(desc);
784 		/*
785 		 * Call irq_startup() not irq_enable() here because the
786 		 * interrupt might be marked NOAUTOEN so irq_startup()
787 		 * needs to be invoked when it gets enabled the first time.
788 		 * This is also required when __enable_irq() is invoked for
789 		 * a managed and shutdown interrupt from the S3 resume
790 		 * path.
791 		 *
792 		 * If it was already started up, then irq_startup() will
793 		 * invoke irq_enable() under the hood.
794 		 */
795 		irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
796 		break;
797 	}
798 	default:
799 		desc->depth--;
800 	}
801 }
802 
803 /**
804  * enable_irq - enable handling of an irq
805  * @irq: Interrupt to enable
806  *
807  * Undoes the effect of one call to disable_irq().  If this matches the
808  * last disable, processing of interrupts on this IRQ line is re-enabled.
809  *
810  * This function may be called from IRQ context only when
811  * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
812  */
813 void enable_irq(unsigned int irq)
814 {
815 	scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) {
816 		struct irq_desc *desc = scoped_irqdesc;
817 
818 		if (WARN(!desc->irq_data.chip, "enable_irq before setup/request_irq: irq %u\n", irq))
819 			return;
820 		__enable_irq(desc);
821 	}
822 }
823 EXPORT_SYMBOL(enable_irq);
824 
825 /**
826  * enable_nmi - enable handling of an nmi
827  * @irq: Interrupt to enable
828  *
829  * The interrupt to enable must have been requested through request_nmi.
830  * Undoes the effect of one call to disable_nmi(). If this matches the last
831  * disable, processing of interrupts on this IRQ line is re-enabled.
832  */
833 void enable_nmi(unsigned int irq)
834 {
835 	enable_irq(irq);
836 }
837 
838 static int set_irq_wake_real(unsigned int irq, unsigned int on)
839 {
840 	struct irq_desc *desc = irq_to_desc(irq);
841 	int ret = -ENXIO;
842 
843 	if (irq_desc_get_chip(desc)->flags &  IRQCHIP_SKIP_SET_WAKE)
844 		return 0;
845 
846 	if (desc->irq_data.chip->irq_set_wake)
847 		ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
848 
849 	return ret;
850 }
851 
852 /**
853  * irq_set_irq_wake - control irq power management wakeup
854  * @irq:	interrupt to control
855  * @on:	enable/disable power management wakeup
856  *
857  * Enable/disable power management wakeup mode, which is disabled by
858  * default.  Enables and disables must match, just as they match for
859  * non-wakeup mode support.
860  *
861  * Wakeup mode lets this IRQ wake the system from sleep states like
862  * "suspend to RAM".
863  *
864  * Note: irq enable/disable state is completely orthogonal to the
865  * enable/disable state of irq wake. An irq can be disabled with
866  * disable_irq() and still wake the system as long as the irq has wake
867  * enabled. If this does not hold, then the underlying irq chip and the
868  * related driver need to be investigated.
869  */
870 int irq_set_irq_wake(unsigned int irq, unsigned int on)
871 {
872 	scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) {
873 		struct irq_desc *desc = scoped_irqdesc;
874 		int ret = 0;
875 
876 		/* Don't use NMIs as wake up interrupts please */
877 		if (irq_is_nmi(desc))
878 			return -EINVAL;
879 
880 		/*
881 		 * wakeup-capable irqs can be shared between drivers that
882 		 * don't need to have the same sleep mode behaviors.
883 		 */
884 		if (on) {
885 			if (desc->wake_depth++ == 0) {
886 				ret = set_irq_wake_real(irq, on);
887 				if (ret)
888 					desc->wake_depth = 0;
889 				else
890 					irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
891 			}
892 		} else {
893 			if (desc->wake_depth == 0) {
894 				WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
895 			} else if (--desc->wake_depth == 0) {
896 				ret = set_irq_wake_real(irq, on);
897 				if (ret)
898 					desc->wake_depth = 1;
899 				else
900 					irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
901 			}
902 		}
903 		return ret;
904 	}
905 	return -EINVAL;
906 }
907 EXPORT_SYMBOL(irq_set_irq_wake);
908 
909 /*
910  * Internal function that tells the architecture code whether a
911  * particular irq has been exclusively allocated or is available
912  * for driver use.
913  */
914 bool can_request_irq(unsigned int irq, unsigned long irqflags)
915 {
916 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_GLOBAL) {
917 		struct irq_desc *desc = scoped_irqdesc;
918 
919 		if (irq_settings_can_request(desc)) {
920 			if (!desc->action || irqflags & desc->action->flags & IRQF_SHARED)
921 				return true;
922 		}
923 	}
924 	return false;
925 }
926 
927 int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
928 {
929 	struct irq_chip *chip = desc->irq_data.chip;
930 	int ret, unmask = 0;
931 
932 	if (!chip || !chip->irq_set_type) {
933 		/*
934 		 * IRQF_TRIGGER_* but the PIC does not support multiple
935 		 * flow-types?
936 		 */
937 		pr_debug("No set_type function for IRQ %d (%s)\n",
938 			 irq_desc_get_irq(desc),
939 			 chip ? (chip->name ? : "unknown") : "unknown");
940 		return 0;
941 	}
942 
943 	if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
944 		if (!irqd_irq_masked(&desc->irq_data))
945 			mask_irq(desc);
946 		if (!irqd_irq_disabled(&desc->irq_data))
947 			unmask = 1;
948 	}
949 
950 	/* Mask all flags except trigger mode */
951 	flags &= IRQ_TYPE_SENSE_MASK;
952 	ret = chip->irq_set_type(&desc->irq_data, flags);
953 
954 	switch (ret) {
955 	case IRQ_SET_MASK_OK:
956 	case IRQ_SET_MASK_OK_DONE:
957 		irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
958 		irqd_set(&desc->irq_data, flags);
959 		fallthrough;
960 
961 	case IRQ_SET_MASK_OK_NOCOPY:
962 		flags = irqd_get_trigger_type(&desc->irq_data);
963 		irq_settings_set_trigger_mask(desc, flags);
964 		irqd_clear(&desc->irq_data, IRQD_LEVEL);
965 		irq_settings_clr_level(desc);
966 		if (flags & IRQ_TYPE_LEVEL_MASK) {
967 			irq_settings_set_level(desc);
968 			irqd_set(&desc->irq_data, IRQD_LEVEL);
969 		}
970 
971 		ret = 0;
972 		break;
973 	default:
974 		pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n",
975 		       flags, irq_desc_get_irq(desc), chip->irq_set_type);
976 	}
977 	if (unmask)
978 		unmask_irq(desc);
979 	return ret;
980 }
981 
982 #ifdef CONFIG_HARDIRQS_SW_RESEND
983 int irq_set_parent(int irq, int parent_irq)
984 {
985 	scoped_irqdesc_get_and_lock(irq, 0) {
986 		scoped_irqdesc->parent_irq = parent_irq;
987 		return 0;
988 	}
989 	return -EINVAL;
990 }
991 EXPORT_SYMBOL_GPL(irq_set_parent);
992 #endif
993 
994 /*
995  * Default primary interrupt handler for threaded interrupts. Is
996  * assigned as primary handler when request_threaded_irq is called
997  * with handler == NULL. Useful for oneshot interrupts.
998  */
999 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
1000 {
1001 	return IRQ_WAKE_THREAD;
1002 }
1003 
1004 /*
1005  * Primary handler for nested threaded interrupts. Should never be
1006  * called.
1007  */
1008 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
1009 {
1010 	WARN(1, "Primary handler called for nested irq %d\n", irq);
1011 	return IRQ_NONE;
1012 }
1013 
1014 static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
1015 {
1016 	WARN(1, "Secondary action handler called for irq %d\n", irq);
1017 	return IRQ_NONE;
1018 }
1019 
1020 #ifdef CONFIG_SMP
1021 /*
1022  * Check whether we need to change the affinity of the interrupt thread.
1023  */
1024 static void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
1025 {
1026 	cpumask_var_t mask;
1027 
1028 	if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
1029 		return;
1030 
1031 	__set_current_state(TASK_RUNNING);
1032 
1033 	/*
1034 	 * In case we are out of memory we set IRQTF_AFFINITY again and
1035 	 * try again next time
1036 	 */
1037 	if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1038 		set_bit(IRQTF_AFFINITY, &action->thread_flags);
1039 		return;
1040 	}
1041 
1042 	scoped_guard(raw_spinlock_irq, &desc->lock) {
1043 		const struct cpumask *m;
1044 
1045 		m = irq_data_get_effective_affinity_mask(&desc->irq_data);
1046 		cpumask_copy(mask, m);
1047 	}
1048 
1049 	set_cpus_allowed_ptr(current, mask);
1050 	free_cpumask_var(mask);
1051 }
1052 #else
1053 static inline void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
1054 #endif
1055 
1056 static int irq_wait_for_interrupt(struct irq_desc *desc,
1057 				  struct irqaction *action)
1058 {
1059 	for (;;) {
1060 		set_current_state(TASK_INTERRUPTIBLE);
1061 		irq_thread_check_affinity(desc, action);
1062 
1063 		if (kthread_should_stop()) {
1064 			/* may need to run one last time */
1065 			if (test_and_clear_bit(IRQTF_RUNTHREAD,
1066 					       &action->thread_flags)) {
1067 				__set_current_state(TASK_RUNNING);
1068 				return 0;
1069 			}
1070 			__set_current_state(TASK_RUNNING);
1071 			return -1;
1072 		}
1073 
1074 		if (test_and_clear_bit(IRQTF_RUNTHREAD,
1075 				       &action->thread_flags)) {
1076 			__set_current_state(TASK_RUNNING);
1077 			return 0;
1078 		}
1079 		schedule();
1080 	}
1081 }
1082 
1083 /*
1084  * Oneshot interrupts keep the irq line masked until the threaded
1085  * handler finished. unmask if the interrupt has not been disabled and
1086  * is marked MASKED.
1087  */
1088 static void irq_finalize_oneshot(struct irq_desc *desc,
1089 				 struct irqaction *action)
1090 {
1091 	if (!(desc->istate & IRQS_ONESHOT) ||
1092 	    action->handler == irq_forced_secondary_handler)
1093 		return;
1094 again:
1095 	chip_bus_lock(desc);
1096 	raw_spin_lock_irq(&desc->lock);
1097 
1098 	/*
1099 	 * Implausible though it may be we need to protect us against
1100 	 * the following scenario:
1101 	 *
1102 	 * The thread is faster done than the hard interrupt handler
1103 	 * on the other CPU. If we unmask the irq line then the
1104 	 * interrupt can come in again and masks the line, leaves due
1105 	 * to IRQS_INPROGRESS and the irq line is masked forever.
1106 	 *
1107 	 * This also serializes the state of shared oneshot handlers
1108 	 * versus "desc->threads_oneshot |= action->thread_mask;" in
1109 	 * irq_wake_thread(). See the comment there which explains the
1110 	 * serialization.
1111 	 */
1112 	if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
1113 		raw_spin_unlock_irq(&desc->lock);
1114 		chip_bus_sync_unlock(desc);
1115 		cpu_relax();
1116 		goto again;
1117 	}
1118 
1119 	/*
1120 	 * Now check again, whether the thread should run. Otherwise
1121 	 * we would clear the threads_oneshot bit of this thread which
1122 	 * was just set.
1123 	 */
1124 	if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
1125 		goto out_unlock;
1126 
1127 	desc->threads_oneshot &= ~action->thread_mask;
1128 
1129 	if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
1130 	    irqd_irq_masked(&desc->irq_data))
1131 		unmask_threaded_irq(desc);
1132 
1133 out_unlock:
1134 	raw_spin_unlock_irq(&desc->lock);
1135 	chip_bus_sync_unlock(desc);
1136 }
1137 
1138 /*
1139  * Interrupts explicitly requested as threaded interrupts want to be
1140  * preemptible - many of them need to sleep and wait for slow busses to
1141  * complete.
1142  */
1143 static irqreturn_t irq_thread_fn(struct irq_desc *desc,	struct irqaction *action)
1144 {
1145 	irqreturn_t ret = action->thread_fn(action->irq, action->dev_id);
1146 
1147 	if (ret == IRQ_HANDLED)
1148 		atomic_inc(&desc->threads_handled);
1149 
1150 	irq_finalize_oneshot(desc, action);
1151 	return ret;
1152 }
1153 
1154 /*
1155  * Interrupts which are not explicitly requested as threaded
1156  * interrupts rely on the implicit bh/preempt disable of the hard irq
1157  * context. So we need to disable bh here to avoid deadlocks and other
1158  * side effects.
1159  */
1160 static irqreturn_t irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
1161 {
1162 	irqreturn_t ret;
1163 
1164 	local_bh_disable();
1165 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
1166 		local_irq_disable();
1167 	ret = irq_thread_fn(desc, action);
1168 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
1169 		local_irq_enable();
1170 	local_bh_enable();
1171 	return ret;
1172 }
1173 
1174 void wake_threads_waitq(struct irq_desc *desc)
1175 {
1176 	if (atomic_dec_and_test(&desc->threads_active))
1177 		wake_up(&desc->wait_for_threads);
1178 }
1179 
1180 static void __irq_thread_dtor(struct irqaction *action)
1181 {
1182 	struct task_struct *tsk = current;
1183 	struct irq_desc *desc;
1184 
1185 	if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
1186 		return;
1187 
1188 	pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
1189 	       tsk->comm, tsk->pid, action->irq);
1190 
1191 
1192 	desc = irq_to_desc(action->irq);
1193 	/*
1194 	 * If IRQTF_RUNTHREAD is set, we need to decrement
1195 	 * desc->threads_active and wake possible waiters.
1196 	 */
1197 	if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
1198 		wake_threads_waitq(desc);
1199 
1200 	/* Prevent a stale desc->threads_oneshot */
1201 	irq_finalize_oneshot(desc, action);
1202 }
1203 
1204 static void irq_thread_dtor(struct callback_head *work)
1205 {
1206 	struct irqaction *action = container_of(work, struct irqaction, thread_dtor);
1207 
1208 	__irq_thread_dtor(action);
1209 	clear_and_wake_up_bit(IRQTF_EXIT_WORK, &action->thread_flags);
1210 }
1211 
1212 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
1213 {
1214 	struct irqaction *secondary = action->secondary;
1215 
1216 	if (WARN_ON_ONCE(!secondary))
1217 		return;
1218 
1219 	guard(raw_spinlock_irq)(&desc->lock);
1220 	__irq_wake_thread(desc, secondary);
1221 }
1222 
1223 /*
1224  * Internal function to notify that a interrupt thread is ready.
1225  */
1226 static void irq_thread_set_ready(struct irq_desc *desc,
1227 				 struct irqaction *action)
1228 {
1229 	set_bit(IRQTF_READY, &action->thread_flags);
1230 	wake_up(&desc->wait_for_threads);
1231 }
1232 
1233 /*
1234  * Internal function to wake up a interrupt thread and wait until it is
1235  * ready.
1236  */
1237 static void wake_up_and_wait_for_irq_thread_ready(struct irq_desc *desc,
1238 						  struct irqaction *action)
1239 {
1240 	if (!action || !action->thread)
1241 		return;
1242 
1243 	wake_up_process(action->thread);
1244 	wait_event(desc->wait_for_threads,
1245 		   test_bit(IRQTF_READY, &action->thread_flags));
1246 }
1247 
1248 /*
1249  * Interrupt handler thread
1250  */
1251 static int irq_thread(void *data)
1252 {
1253 	struct irqaction *action = data;
1254 	struct irq_desc *desc = irq_to_desc(action->irq);
1255 	irqreturn_t (*handler_fn)(struct irq_desc *desc,
1256 			struct irqaction *action);
1257 
1258 	irq_thread_set_ready(desc, action);
1259 
1260 	if (action->handler == irq_forced_secondary_handler)
1261 		sched_set_fifo_secondary(current);
1262 	else
1263 		sched_set_fifo(current);
1264 
1265 	if (force_irqthreads() && test_bit(IRQTF_FORCED_THREAD,
1266 					   &action->thread_flags))
1267 		handler_fn = irq_forced_thread_fn;
1268 	else
1269 		handler_fn = irq_thread_fn;
1270 
1271 	set_bit(IRQTF_EXIT_WORK, &action->thread_flags);
1272 	init_task_work(&action->thread_dtor, irq_thread_dtor);
1273 	task_work_add(current, &action->thread_dtor, TWA_NONE);
1274 
1275 	while (!irq_wait_for_interrupt(desc, action)) {
1276 		irqreturn_t action_ret;
1277 
1278 		action_ret = handler_fn(desc, action);
1279 		if (action_ret == IRQ_WAKE_THREAD)
1280 			irq_wake_secondary(desc, action);
1281 
1282 		wake_threads_waitq(desc);
1283 	}
1284 
1285 	/*
1286 	 * This is the regular exit path. __free_irq() is stopping the
1287 	 * thread via kthread_stop() after calling
1288 	 * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the
1289 	 * oneshot mask bit can be set.
1290 	 */
1291 	task_work_cancel_func(current, irq_thread_dtor);
1292 	clear_and_wake_up_bit(IRQTF_EXIT_WORK, &action->thread_flags);
1293 	return 0;
1294 }
1295 
1296 /**
1297  * irq_wake_thread - wake the irq thread for the action identified by dev_id
1298  * @irq:	Interrupt line
1299  * @dev_id:	Device identity for which the thread should be woken
1300  */
1301 void irq_wake_thread(unsigned int irq, void *dev_id)
1302 {
1303 	struct irq_desc *desc = irq_to_desc(irq);
1304 	struct irqaction *action;
1305 
1306 	if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1307 		return;
1308 
1309 	guard(raw_spinlock_irqsave)(&desc->lock);
1310 	for_each_action_of_desc(desc, action) {
1311 		if (action->dev_id == dev_id) {
1312 			if (action->thread)
1313 				__irq_wake_thread(desc, action);
1314 			break;
1315 		}
1316 	}
1317 }
1318 EXPORT_SYMBOL_GPL(irq_wake_thread);
1319 
1320 static int irq_setup_forced_threading(struct irqaction *new)
1321 {
1322 	if (!force_irqthreads())
1323 		return 0;
1324 	if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1325 		return 0;
1326 
1327 	/*
1328 	 * No further action required for interrupts which are requested as
1329 	 * threaded interrupts already
1330 	 */
1331 	if (new->handler == irq_default_primary_handler)
1332 		return 0;
1333 
1334 	new->flags |= IRQF_ONESHOT;
1335 
1336 	/*
1337 	 * Handle the case where we have a real primary handler and a
1338 	 * thread handler. We force thread them as well by creating a
1339 	 * secondary action.
1340 	 */
1341 	if (new->handler && new->thread_fn) {
1342 		/* Allocate the secondary action */
1343 		new->secondary = kzalloc_obj(struct irqaction);
1344 		if (!new->secondary)
1345 			return -ENOMEM;
1346 		new->secondary->handler = irq_forced_secondary_handler;
1347 		new->secondary->thread_fn = new->thread_fn;
1348 		new->secondary->dev_id = new->dev_id;
1349 		new->secondary->irq = new->irq;
1350 		new->secondary->name = new->name;
1351 	}
1352 	/* Deal with the primary handler */
1353 	set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1354 	new->thread_fn = new->handler;
1355 	new->handler = irq_default_primary_handler;
1356 	return 0;
1357 }
1358 
1359 static int irq_request_resources(struct irq_desc *desc)
1360 {
1361 	struct irq_data *d = &desc->irq_data;
1362 	struct irq_chip *c = d->chip;
1363 
1364 	return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1365 }
1366 
1367 static void irq_release_resources(struct irq_desc *desc)
1368 {
1369 	struct irq_data *d = &desc->irq_data;
1370 	struct irq_chip *c = d->chip;
1371 
1372 	if (c->irq_release_resources)
1373 		c->irq_release_resources(d);
1374 }
1375 
1376 static bool irq_supports_nmi(struct irq_desc *desc)
1377 {
1378 	struct irq_data *d = irq_desc_get_irq_data(desc);
1379 
1380 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1381 	/* Only IRQs directly managed by the root irqchip can be set as NMI */
1382 	if (d->parent_data)
1383 		return false;
1384 #endif
1385 	/* Don't support NMIs for chips behind a slow bus */
1386 	if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
1387 		return false;
1388 
1389 	return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
1390 }
1391 
1392 static int irq_nmi_setup(struct irq_desc *desc)
1393 {
1394 	struct irq_data *d = irq_desc_get_irq_data(desc);
1395 	struct irq_chip *c = d->chip;
1396 
1397 	return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
1398 }
1399 
1400 static void irq_nmi_teardown(struct irq_desc *desc)
1401 {
1402 	struct irq_data *d = irq_desc_get_irq_data(desc);
1403 	struct irq_chip *c = d->chip;
1404 
1405 	if (c->irq_nmi_teardown)
1406 		c->irq_nmi_teardown(d);
1407 }
1408 
1409 static int
1410 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1411 {
1412 	struct task_struct *t;
1413 
1414 	if (!secondary) {
1415 		t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1416 				   new->name);
1417 	} else {
1418 		t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1419 				   new->name);
1420 	}
1421 
1422 	if (IS_ERR(t))
1423 		return PTR_ERR(t);
1424 
1425 	/*
1426 	 * We keep the reference to the task struct even if
1427 	 * the thread dies to avoid that the interrupt code
1428 	 * references an already freed task_struct.
1429 	 */
1430 	new->thread = get_task_struct(t);
1431 
1432 	/*
1433 	 * The affinity can not be established yet, but it will be once the
1434 	 * interrupt is enabled. Delay and defer the actual setting to the
1435 	 * thread itself once it is ready to run. In the meantime, prevent
1436 	 * it from ever being re-affined directly by cpuset or
1437 	 * housekeeping. The proper way to do it is to re-affine the whole
1438 	 * vector.
1439 	 */
1440 	kthread_bind_mask(t, cpu_possible_mask);
1441 
1442 	/*
1443 	 * Ensure the thread adjusts the affinity once it reaches the
1444 	 * thread function.
1445 	 */
1446 	set_bit(IRQTF_AFFINITY, &new->thread_flags);
1447 
1448 	return 0;
1449 }
1450 
1451 static bool valid_percpu_irqaction(struct irqaction *old, struct irqaction *new)
1452 {
1453 	do {
1454 		if (cpumask_intersects(old->affinity, new->affinity) ||
1455 		    old->percpu_dev_id == new->percpu_dev_id)
1456 			return false;
1457 
1458 		old = old->next;
1459 	} while (old);
1460 
1461 	return true;
1462 }
1463 
1464 /*
1465  * Internal function to register an irqaction - typically used to
1466  * allocate special interrupts that are part of the architecture.
1467  *
1468  * Locking rules:
1469  *
1470  * desc->request_mutex	Provides serialization against a concurrent free_irq()
1471  *   chip_bus_lock	Provides serialization for slow bus operations
1472  *     desc->lock	Provides serialization against hard interrupts
1473  *
1474  * chip_bus_lock and desc->lock are sufficient for all other management and
1475  * interrupt related functions. desc->request_mutex solely serializes
1476  * request/free_irq().
1477  */
1478 static int
1479 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1480 {
1481 	struct irqaction *old, **old_ptr;
1482 	unsigned long flags, thread_mask = 0;
1483 	int ret, nested, shared = 0;
1484 	bool per_cpu_devid;
1485 
1486 	if (!desc)
1487 		return -EINVAL;
1488 
1489 	if (desc->irq_data.chip == &no_irq_chip)
1490 		return -ENOSYS;
1491 	if (!try_module_get(desc->owner))
1492 		return -ENODEV;
1493 
1494 	per_cpu_devid = irq_settings_is_per_cpu_devid(desc);
1495 
1496 	new->irq = irq;
1497 
1498 	/*
1499 	 * If the trigger type is not specified by the caller,
1500 	 * then use the default for this interrupt.
1501 	 */
1502 	if (!(new->flags & IRQF_TRIGGER_MASK))
1503 		new->flags |= irqd_get_trigger_type(&desc->irq_data);
1504 
1505 	/*
1506 	 * IRQF_ONESHOT means the interrupt source in the IRQ chip will be
1507 	 * masked until the threaded handled is done. If there is no thread
1508 	 * handler then it makes no sense to have IRQF_ONESHOT.
1509 	 */
1510 	WARN_ON_ONCE(new->flags & IRQF_ONESHOT && !new->thread_fn);
1511 
1512 	/*
1513 	 * Check whether the interrupt nests into another interrupt
1514 	 * thread.
1515 	 */
1516 	nested = irq_settings_is_nested_thread(desc);
1517 	if (nested) {
1518 		if (!new->thread_fn) {
1519 			ret = -EINVAL;
1520 			goto out_mput;
1521 		}
1522 		/*
1523 		 * Replace the primary handler which was provided from
1524 		 * the driver for non nested interrupt handling by the
1525 		 * dummy function which warns when called.
1526 		 */
1527 		new->handler = irq_nested_primary_handler;
1528 	} else {
1529 		if (irq_settings_can_thread(desc)) {
1530 			ret = irq_setup_forced_threading(new);
1531 			if (ret)
1532 				goto out_mput;
1533 		}
1534 	}
1535 
1536 	/*
1537 	 * Create a handler thread when a thread function is supplied
1538 	 * and the interrupt does not nest into another interrupt
1539 	 * thread.
1540 	 */
1541 	if (new->thread_fn && !nested) {
1542 		ret = setup_irq_thread(new, irq, false);
1543 		if (ret)
1544 			goto out_mput;
1545 		if (new->secondary) {
1546 			ret = setup_irq_thread(new->secondary, irq, true);
1547 			if (ret)
1548 				goto out_thread;
1549 		}
1550 	}
1551 
1552 	/*
1553 	 * Drivers are often written to work w/o knowledge about the
1554 	 * underlying irq chip implementation, so a request for a
1555 	 * threaded irq without a primary hard irq context handler
1556 	 * requires the ONESHOT flag to be set. Some irq chips like
1557 	 * MSI based interrupts are per se one shot safe. Check the
1558 	 * chip flags, so we can avoid the unmask dance at the end of
1559 	 * the threaded handler for those.
1560 	 */
1561 	if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1562 		new->flags &= ~IRQF_ONESHOT;
1563 
1564 	/*
1565 	 * Protects against a concurrent __free_irq() call which might wait
1566 	 * for synchronize_hardirq() to complete without holding the optional
1567 	 * chip bus lock and desc->lock. Also protects against handing out
1568 	 * a recycled oneshot thread_mask bit while it's still in use by
1569 	 * its previous owner.
1570 	 */
1571 	mutex_lock(&desc->request_mutex);
1572 
1573 	/*
1574 	 * Acquire bus lock as the irq_request_resources() callback below
1575 	 * might rely on the serialization or the magic power management
1576 	 * functions which are abusing the irq_bus_lock() callback,
1577 	 */
1578 	chip_bus_lock(desc);
1579 
1580 	/* First installed action requests resources. */
1581 	if (!desc->action) {
1582 		ret = irq_request_resources(desc);
1583 		if (ret) {
1584 			pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1585 			       new->name, irq, desc->irq_data.chip->name);
1586 			goto out_bus_unlock;
1587 		}
1588 	}
1589 
1590 	/*
1591 	 * The following block of code has to be executed atomically
1592 	 * protected against a concurrent interrupt and any of the other
1593 	 * management calls which are not serialized via
1594 	 * desc->request_mutex or the optional bus lock.
1595 	 */
1596 	raw_spin_lock_irqsave(&desc->lock, flags);
1597 	old_ptr = &desc->action;
1598 	old = *old_ptr;
1599 	if (old) {
1600 		/*
1601 		 * Can't share interrupts unless both agree to and are
1602 		 * the same type (level, edge, polarity). So both flag
1603 		 * fields must have IRQF_SHARED set and the bits which
1604 		 * set the trigger type must match. Also all must
1605 		 * agree on ONESHOT.
1606 		 * Interrupt lines used for NMIs cannot be shared.
1607 		 */
1608 		unsigned int oldtype;
1609 
1610 		if (irq_is_nmi(desc) && !per_cpu_devid) {
1611 			pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
1612 				new->name, irq, desc->irq_data.chip->name);
1613 			ret = -EINVAL;
1614 			goto out_unlock;
1615 		}
1616 
1617 		if (per_cpu_devid && !valid_percpu_irqaction(old, new)) {
1618 			pr_err("Overlapping affinities for %s (irq %d) on irqchip %s.\n",
1619 				new->name, irq, desc->irq_data.chip->name);
1620 			ret = -EINVAL;
1621 			goto out_unlock;
1622 		}
1623 
1624 		/*
1625 		 * If nobody did set the configuration before, inherit
1626 		 * the one provided by the requester.
1627 		 */
1628 		if (irqd_trigger_type_was_set(&desc->irq_data)) {
1629 			oldtype = irqd_get_trigger_type(&desc->irq_data);
1630 		} else {
1631 			oldtype = new->flags & IRQF_TRIGGER_MASK;
1632 			irqd_set_trigger_type(&desc->irq_data, oldtype);
1633 		}
1634 
1635 		if (!((old->flags & new->flags) & IRQF_SHARED) ||
1636 		    (oldtype != (new->flags & IRQF_TRIGGER_MASK)))
1637 			goto mismatch;
1638 
1639 		if ((old->flags & IRQF_ONESHOT) &&
1640 		    (new->flags & IRQF_COND_ONESHOT))
1641 			new->flags |= IRQF_ONESHOT;
1642 		else if ((old->flags ^ new->flags) & IRQF_ONESHOT)
1643 			goto mismatch;
1644 
1645 		/* All handlers must agree on per-cpuness */
1646 		if ((old->flags & IRQF_PERCPU) !=
1647 		    (new->flags & IRQF_PERCPU))
1648 			goto mismatch;
1649 
1650 		/* add new interrupt at end of irq queue */
1651 		do {
1652 			/*
1653 			 * Or all existing action->thread_mask bits,
1654 			 * so we can find the next zero bit for this
1655 			 * new action.
1656 			 */
1657 			thread_mask |= old->thread_mask;
1658 			old_ptr = &old->next;
1659 			old = *old_ptr;
1660 		} while (old);
1661 		shared = 1;
1662 	}
1663 
1664 	/*
1665 	 * Setup the thread mask for this irqaction for ONESHOT. For
1666 	 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1667 	 * conditional in irq_wake_thread().
1668 	 */
1669 	if (new->flags & IRQF_ONESHOT) {
1670 		/*
1671 		 * Unlikely to have 32 resp 64 irqs sharing one line,
1672 		 * but who knows.
1673 		 */
1674 		if (thread_mask == ~0UL) {
1675 			ret = -EBUSY;
1676 			goto out_unlock;
1677 		}
1678 		/*
1679 		 * The thread_mask for the action is or'ed to
1680 		 * desc->thread_active to indicate that the
1681 		 * IRQF_ONESHOT thread handler has been woken, but not
1682 		 * yet finished. The bit is cleared when a thread
1683 		 * completes. When all threads of a shared interrupt
1684 		 * line have completed desc->threads_active becomes
1685 		 * zero and the interrupt line is unmasked. See
1686 		 * handle.c:irq_wake_thread() for further information.
1687 		 *
1688 		 * If no thread is woken by primary (hard irq context)
1689 		 * interrupt handlers, then desc->threads_active is
1690 		 * also checked for zero to unmask the irq line in the
1691 		 * affected hard irq flow handlers
1692 		 * (handle_[fasteoi|level]_irq).
1693 		 *
1694 		 * The new action gets the first zero bit of
1695 		 * thread_mask assigned. See the loop above which or's
1696 		 * all existing action->thread_mask bits.
1697 		 */
1698 		new->thread_mask = 1UL << ffz(thread_mask);
1699 
1700 	} else if (new->handler == irq_default_primary_handler &&
1701 		   !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1702 		/*
1703 		 * The interrupt was requested with handler = NULL, so
1704 		 * we use the default primary handler for it. But it
1705 		 * does not have the oneshot flag set. In combination
1706 		 * with level interrupts this is deadly, because the
1707 		 * default primary handler just wakes the thread, then
1708 		 * the irq lines is reenabled, but the device still
1709 		 * has the level irq asserted. Rinse and repeat....
1710 		 *
1711 		 * While this works for edge type interrupts, we play
1712 		 * it safe and reject unconditionally because we can't
1713 		 * say for sure which type this interrupt really
1714 		 * has. The type flags are unreliable as the
1715 		 * underlying chip implementation can override them.
1716 		 */
1717 		pr_err("Threaded irq requested with handler=NULL and !ONESHOT for %s (irq %d)\n",
1718 		       new->name, irq);
1719 		ret = -EINVAL;
1720 		goto out_unlock;
1721 	}
1722 
1723 	if (!shared) {
1724 		/* Setup the type (level, edge polarity) if configured: */
1725 		if (new->flags & IRQF_TRIGGER_MASK) {
1726 			ret = __irq_set_trigger(desc,
1727 						new->flags & IRQF_TRIGGER_MASK);
1728 
1729 			if (ret)
1730 				goto out_unlock;
1731 		}
1732 
1733 		/*
1734 		 * Activate the interrupt. That activation must happen
1735 		 * independently of IRQ_NOAUTOEN. request_irq() can fail
1736 		 * and the callers are supposed to handle
1737 		 * that. enable_irq() of an interrupt requested with
1738 		 * IRQ_NOAUTOEN is not supposed to fail. The activation
1739 		 * keeps it in shutdown mode, it merily associates
1740 		 * resources if necessary and if that's not possible it
1741 		 * fails. Interrupts which are in managed shutdown mode
1742 		 * will simply ignore that activation request.
1743 		 */
1744 		ret = irq_activate(desc);
1745 		if (ret)
1746 			goto out_unlock;
1747 
1748 		desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1749 				  IRQS_ONESHOT | IRQS_WAITING);
1750 		irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1751 
1752 		if (new->flags & IRQF_PERCPU) {
1753 			irqd_set(&desc->irq_data, IRQD_PER_CPU);
1754 			irq_settings_set_per_cpu(desc);
1755 			if (new->flags & IRQF_NO_DEBUG)
1756 				irq_settings_set_no_debug(desc);
1757 		}
1758 
1759 		if (noirqdebug)
1760 			irq_settings_set_no_debug(desc);
1761 
1762 		if (new->flags & IRQF_ONESHOT)
1763 			desc->istate |= IRQS_ONESHOT;
1764 
1765 		/* Exclude IRQ from balancing if requested */
1766 		if (new->flags & IRQF_NOBALANCING) {
1767 			irq_settings_set_no_balancing(desc);
1768 			irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1769 		}
1770 
1771 		if (!(new->flags & IRQF_NO_AUTOEN) &&
1772 		    irq_settings_can_autoenable(desc)) {
1773 			irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
1774 		} else if (!per_cpu_devid) {
1775 			/*
1776 			 * Shared interrupts do not go well with disabling
1777 			 * auto enable. The sharing interrupt might request
1778 			 * it while it's still disabled and then wait for
1779 			 * interrupts forever.
1780 			 */
1781 			WARN_ON_ONCE(new->flags & IRQF_SHARED);
1782 			/* Undo nested disables: */
1783 			desc->depth = 1;
1784 		}
1785 
1786 	} else if (new->flags & IRQF_TRIGGER_MASK) {
1787 		unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1788 		unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
1789 
1790 		if (nmsk != omsk)
1791 			/* hope the handler works with current  trigger mode */
1792 			pr_warn("irq %d uses trigger mode %u; requested %u\n",
1793 				irq, omsk, nmsk);
1794 	}
1795 
1796 	*old_ptr = new;
1797 
1798 	irq_pm_install_action(desc, new);
1799 
1800 	/* Reset broken irq detection when installing new handler */
1801 	desc->irq_count = 0;
1802 	desc->irqs_unhandled = 0;
1803 
1804 	/*
1805 	 * Check whether we disabled the irq via the spurious handler
1806 	 * before. Reenable it and give it another chance.
1807 	 */
1808 	if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1809 		desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1810 		__enable_irq(desc);
1811 	}
1812 
1813 	irq_proc_update_valid(desc);
1814 	raw_spin_unlock_irqrestore(&desc->lock, flags);
1815 	chip_bus_sync_unlock(desc);
1816 	mutex_unlock(&desc->request_mutex);
1817 
1818 	wake_up_and_wait_for_irq_thread_ready(desc, new);
1819 	wake_up_and_wait_for_irq_thread_ready(desc, new->secondary);
1820 
1821 	register_irq_proc(irq, desc);
1822 	new->dir = NULL;
1823 	register_handler_proc(irq, new);
1824 	return 0;
1825 
1826 mismatch:
1827 	if (!(new->flags & IRQF_PROBE_SHARED)) {
1828 		pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1829 		       irq, new->flags, new->name, old->flags, old->name);
1830 #ifdef CONFIG_DEBUG_SHIRQ
1831 		dump_stack();
1832 #endif
1833 	}
1834 	ret = -EBUSY;
1835 
1836 out_unlock:
1837 	raw_spin_unlock_irqrestore(&desc->lock, flags);
1838 
1839 	if (!desc->action)
1840 		irq_release_resources(desc);
1841 out_bus_unlock:
1842 	chip_bus_sync_unlock(desc);
1843 	mutex_unlock(&desc->request_mutex);
1844 
1845 out_thread:
1846 	if (new->thread) {
1847 		struct task_struct *t = new->thread;
1848 
1849 		new->thread = NULL;
1850 		kthread_stop_put(t);
1851 	}
1852 	if (new->secondary && new->secondary->thread) {
1853 		struct task_struct *t = new->secondary->thread;
1854 
1855 		new->secondary->thread = NULL;
1856 		kthread_stop_put(t);
1857 	}
1858 out_mput:
1859 	module_put(desc->owner);
1860 	return ret;
1861 }
1862 
1863 static void irq_stop_thread(struct irqaction *action)
1864 {
1865 	kthread_stop_put(action->thread);
1866 	wait_on_bit(&action->thread_flags, IRQTF_EXIT_WORK, TASK_UNINTERRUPTIBLE);
1867 }
1868 
1869 /*
1870  * Internal function to unregister an irqaction - used to free
1871  * regular and special interrupts that are part of the architecture.
1872  */
1873 static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
1874 {
1875 	unsigned irq = desc->irq_data.irq;
1876 	struct irqaction *action, **action_ptr;
1877 	unsigned long flags;
1878 
1879 	WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1880 
1881 	mutex_lock(&desc->request_mutex);
1882 	chip_bus_lock(desc);
1883 	raw_spin_lock_irqsave(&desc->lock, flags);
1884 
1885 	/*
1886 	 * There can be multiple actions per IRQ descriptor, find the right
1887 	 * one based on the dev_id:
1888 	 */
1889 	action_ptr = &desc->action;
1890 	for (;;) {
1891 		action = *action_ptr;
1892 
1893 		if (!action) {
1894 			WARN(1, "Trying to free already-free IRQ %d\n", irq);
1895 			raw_spin_unlock_irqrestore(&desc->lock, flags);
1896 			chip_bus_sync_unlock(desc);
1897 			mutex_unlock(&desc->request_mutex);
1898 			return NULL;
1899 		}
1900 
1901 		if (action->dev_id == dev_id)
1902 			break;
1903 		action_ptr = &action->next;
1904 	}
1905 
1906 	/* Found it - now remove it from the list of entries: */
1907 	*action_ptr = action->next;
1908 
1909 	irq_pm_remove_action(desc, action);
1910 
1911 	/* If this was the last handler, shut down the IRQ line: */
1912 	if (!desc->action) {
1913 		irq_settings_clr_disable_unlazy(desc);
1914 		/* Only shutdown. Deactivate after synchronize_hardirq() */
1915 		irq_shutdown(desc);
1916 	}
1917 
1918 #ifdef CONFIG_SMP
1919 	/* make sure affinity_hint is cleaned up */
1920 	if (WARN_ON_ONCE(desc->affinity_hint))
1921 		desc->affinity_hint = NULL;
1922 #endif
1923 
1924 	irq_proc_update_valid(desc);
1925 	raw_spin_unlock_irqrestore(&desc->lock, flags);
1926 	/*
1927 	 * Drop bus_lock here so the changes which were done in the chip
1928 	 * callbacks above are synced out to the irq chips which hang
1929 	 * behind a slow bus (I2C, SPI) before calling synchronize_hardirq().
1930 	 *
1931 	 * Aside of that the bus_lock can also be taken from the threaded
1932 	 * handler in irq_finalize_oneshot() which results in a deadlock
1933 	 * because kthread_stop() would wait forever for the thread to
1934 	 * complete, which is blocked on the bus lock.
1935 	 *
1936 	 * The still held desc->request_mutex() protects against a
1937 	 * concurrent request_irq() of this irq so the release of resources
1938 	 * and timing data is properly serialized.
1939 	 */
1940 	chip_bus_sync_unlock(desc);
1941 
1942 	unregister_handler_proc(irq, action);
1943 
1944 	/*
1945 	 * Make sure it's not being used on another CPU and if the chip
1946 	 * supports it also make sure that there is no (not yet serviced)
1947 	 * interrupt in flight at the hardware level.
1948 	 */
1949 	__synchronize_irq(desc);
1950 
1951 #ifdef CONFIG_DEBUG_SHIRQ
1952 	/*
1953 	 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1954 	 * event to happen even now it's being freed, so let's make sure that
1955 	 * is so by doing an extra call to the handler ....
1956 	 *
1957 	 * ( We do this after actually deregistering it, to make sure that a
1958 	 *   'real' IRQ doesn't run in parallel with our fake. )
1959 	 */
1960 	if (action->flags & IRQF_SHARED) {
1961 		local_irq_save(flags);
1962 		action->handler(irq, dev_id);
1963 		local_irq_restore(flags);
1964 	}
1965 #endif
1966 
1967 	/*
1968 	 * The action has already been removed above, but the thread writes
1969 	 * its oneshot mask bit when it completes. Though request_mutex is
1970 	 * held across this which prevents __setup_irq() from handing out
1971 	 * the same bit to a newly requested action.
1972 	 */
1973 	if (action->thread) {
1974 		irq_stop_thread(action);
1975 		if (action->secondary && action->secondary->thread)
1976 			irq_stop_thread(action->secondary);
1977 	}
1978 
1979 	/* Last action releases resources */
1980 	if (!desc->action) {
1981 		/*
1982 		 * Reacquire bus lock as irq_release_resources() might
1983 		 * require it to deallocate resources over the slow bus.
1984 		 */
1985 		chip_bus_lock(desc);
1986 		/*
1987 		 * There is no interrupt on the fly anymore. Deactivate it
1988 		 * completely.
1989 		 */
1990 		scoped_guard(raw_spinlock_irqsave, &desc->lock)
1991 			irq_domain_deactivate_irq(&desc->irq_data);
1992 
1993 		irq_release_resources(desc);
1994 		chip_bus_sync_unlock(desc);
1995 	}
1996 
1997 	mutex_unlock(&desc->request_mutex);
1998 
1999 	irq_chip_pm_put(&desc->irq_data);
2000 	module_put(desc->owner);
2001 	kfree(action->secondary);
2002 	return action;
2003 }
2004 
2005 /**
2006  * free_irq - free an interrupt allocated with request_irq
2007  * @irq:	Interrupt line to free
2008  * @dev_id:	Device identity to free
2009  *
2010  * Remove an interrupt handler. The handler is removed and if the interrupt
2011  * line is no longer in use by any driver it is disabled.  On a shared IRQ
2012  * the caller must ensure the interrupt is disabled on the card it drives
2013  * before calling this function. The function does not return until any
2014  * executing interrupts for this IRQ have completed.
2015  *
2016  * This function must not be called from interrupt context.
2017  *
2018  * Returns the devname argument passed to request_irq.
2019  */
2020 const void *free_irq(unsigned int irq, void *dev_id)
2021 {
2022 	struct irq_desc *desc = irq_to_desc(irq);
2023 	struct irqaction *action;
2024 	const char *devname;
2025 
2026 	if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
2027 		return NULL;
2028 
2029 #ifdef CONFIG_SMP
2030 	if (WARN_ON(desc->affinity_notify))
2031 		desc->affinity_notify = NULL;
2032 #endif
2033 
2034 	action = __free_irq(desc, dev_id);
2035 
2036 	if (!action)
2037 		return NULL;
2038 
2039 	devname = action->name;
2040 	kfree(action);
2041 	return devname;
2042 }
2043 EXPORT_SYMBOL(free_irq);
2044 
2045 static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
2046 {
2047 	struct irqaction *action = NULL;
2048 	const char *devname = NULL;
2049 
2050 	scoped_guard(raw_spinlock_irqsave, &desc->lock) {
2051 		irq_nmi_teardown(desc);
2052 
2053 		desc->istate &= ~IRQS_NMI;
2054 
2055 		if (!WARN_ON(desc->action == NULL)) {
2056 			action = desc->action;
2057 			irq_pm_remove_action(desc, action);
2058 			devname = action->name;
2059 		}
2060 		desc->action = NULL;
2061 
2062 		irq_settings_clr_disable_unlazy(desc);
2063 		irq_shutdown_and_deactivate(desc);
2064 	}
2065 
2066 	irq_proc_update_valid(desc);
2067 
2068 	if (action)
2069 		unregister_handler_proc(irq, action);
2070 	kfree(action);
2071 
2072 	irq_release_resources(desc);
2073 
2074 	irq_chip_pm_put(&desc->irq_data);
2075 	module_put(desc->owner);
2076 
2077 	return devname;
2078 }
2079 
2080 const void *free_nmi(unsigned int irq, void *dev_id)
2081 {
2082 	struct irq_desc *desc = irq_to_desc(irq);
2083 
2084 	if (!desc || WARN_ON(!irq_is_nmi(desc)))
2085 		return NULL;
2086 
2087 	if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
2088 		return NULL;
2089 
2090 	/* NMI still enabled */
2091 	if (WARN_ON(desc->depth == 0))
2092 		disable_nmi_nosync(irq);
2093 
2094 	return __cleanup_nmi(irq, desc);
2095 }
2096 
2097 /**
2098  * request_threaded_irq - allocate an interrupt line
2099  * @irq:	Interrupt line to allocate
2100  * @handler:	Function to be called when the IRQ occurs.
2101  *		Primary handler for threaded interrupts.
2102  *		If handler is NULL and thread_fn != NULL
2103  *		the default primary handler is installed.
2104  * @thread_fn:	Function called from the irq handler thread
2105  *		If NULL, no irq thread is created
2106  * @irqflags:	Interrupt type flags
2107  * @devname:	An ascii name for the claiming device
2108  * @dev_id:	A cookie passed back to the handler function
2109  *
2110  * This call allocates interrupt resources and enables the interrupt line
2111  * and IRQ handling. From the point this call is made your handler function
2112  * may be invoked. Since your handler function must clear any interrupt the
2113  * board raises, you must take care both to initialise your hardware and to
2114  * set up the interrupt handler in the right order.
2115  *
2116  * If you want to set up a threaded irq handler for your device then you
2117  * need to supply @handler and @thread_fn. @handler is still called in hard
2118  * interrupt context and has to check whether the interrupt originates from
2119  * the device. If yes it needs to disable the interrupt on the device and
2120  * return IRQ_WAKE_THREAD which will wake up the handler thread and run
2121  * @thread_fn. This split handler design is necessary to support shared
2122  * interrupts.
2123  *
2124  * @dev_id must be globally unique. Normally the address of the device data
2125  * structure is used as the cookie. Since the handler receives this value
2126  * it makes sense to use it.
2127  *
2128  * If your interrupt is shared you must pass a non NULL dev_id as this is
2129  * required when freeing the interrupt.
2130  *
2131  * Flags:
2132  *
2133  *	IRQF_SHARED		Interrupt is shared
2134  *	IRQF_TRIGGER_*		Specify active edge(s) or level
2135  *	IRQF_ONESHOT		Run thread_fn with interrupt line masked
2136  */
2137 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
2138 			 irq_handler_t thread_fn, unsigned long irqflags,
2139 			 const char *devname, void *dev_id)
2140 {
2141 	struct irqaction *action;
2142 	struct irq_desc *desc;
2143 	int retval;
2144 
2145 	if (irq == IRQ_NOTCONNECTED)
2146 		return -ENOTCONN;
2147 
2148 	/*
2149 	 * Sanity-check: shared interrupts must pass in a real dev-ID,
2150 	 * otherwise we'll have trouble later trying to figure out
2151 	 * which interrupt is which (messes up the interrupt freeing
2152 	 * logic etc).
2153 	 *
2154 	 * Also shared interrupts do not go well with disabling auto enable.
2155 	 * The sharing interrupt might request it while it's still disabled
2156 	 * and then wait for interrupts forever.
2157 	 *
2158 	 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
2159 	 * it cannot be set along with IRQF_NO_SUSPEND.
2160 	 */
2161 	if (((irqflags & IRQF_SHARED) && !dev_id) ||
2162 	    ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) ||
2163 	    (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
2164 	    ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
2165 		return -EINVAL;
2166 
2167 	desc = irq_to_desc(irq);
2168 	if (!desc)
2169 		return -EINVAL;
2170 
2171 	if (!irq_settings_can_request(desc) ||
2172 	    WARN_ON(irq_settings_is_per_cpu_devid(desc)))
2173 		return -EINVAL;
2174 
2175 	if (!handler) {
2176 		if (!thread_fn)
2177 			return -EINVAL;
2178 		handler = irq_default_primary_handler;
2179 	}
2180 
2181 	action = kzalloc_obj(struct irqaction);
2182 	if (!action)
2183 		return -ENOMEM;
2184 
2185 	action->handler = handler;
2186 	action->thread_fn = thread_fn;
2187 	action->flags = irqflags;
2188 	action->name = devname;
2189 	action->dev_id = dev_id;
2190 
2191 	retval = irq_chip_pm_get(&desc->irq_data);
2192 	if (retval < 0) {
2193 		kfree(action);
2194 		return retval;
2195 	}
2196 
2197 	retval = __setup_irq(irq, desc, action);
2198 
2199 	if (retval) {
2200 		irq_chip_pm_put(&desc->irq_data);
2201 		kfree(action->secondary);
2202 		kfree(action);
2203 	}
2204 
2205 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
2206 	if (!retval && (irqflags & IRQF_SHARED)) {
2207 		/*
2208 		 * It's a shared IRQ -- the driver ought to be prepared for it
2209 		 * to happen immediately, so let's make sure....
2210 		 * We disable the irq to make sure that a 'real' IRQ doesn't
2211 		 * run in parallel with our fake.
2212 		 */
2213 		unsigned long flags;
2214 
2215 		disable_irq(irq);
2216 		local_irq_save(flags);
2217 
2218 		handler(irq, dev_id);
2219 
2220 		local_irq_restore(flags);
2221 		enable_irq(irq);
2222 	}
2223 #endif
2224 	return retval;
2225 }
2226 EXPORT_SYMBOL(request_threaded_irq);
2227 
2228 /**
2229  * request_any_context_irq - allocate an interrupt line
2230  * @irq:	Interrupt line to allocate
2231  * @handler:	Function to be called when the IRQ occurs.
2232  *		Threaded handler for threaded interrupts.
2233  * @flags:	Interrupt type flags
2234  * @name:	An ascii name for the claiming device
2235  * @dev_id:	A cookie passed back to the handler function
2236  *
2237  * This call allocates interrupt resources and enables the interrupt line
2238  * and IRQ handling. It selects either a hardirq or threaded handling
2239  * method depending on the context.
2240  *
2241  * Returns: On failure, it returns a negative value. On success, it returns either
2242  * IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
2243  */
2244 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
2245 			    unsigned long flags, const char *name, void *dev_id)
2246 {
2247 	struct irq_desc *desc;
2248 	int ret;
2249 
2250 	if (irq == IRQ_NOTCONNECTED)
2251 		return -ENOTCONN;
2252 
2253 	desc = irq_to_desc(irq);
2254 	if (!desc)
2255 		return -EINVAL;
2256 
2257 	if (irq_settings_is_nested_thread(desc)) {
2258 		ret = request_threaded_irq(irq, NULL, handler,
2259 					   flags, name, dev_id);
2260 		return !ret ? IRQC_IS_NESTED : ret;
2261 	}
2262 
2263 	ret = request_irq(irq, handler, flags, name, dev_id);
2264 	return !ret ? IRQC_IS_HARDIRQ : ret;
2265 }
2266 EXPORT_SYMBOL_GPL(request_any_context_irq);
2267 
2268 /**
2269  * request_nmi - allocate an interrupt line for NMI delivery
2270  * @irq:	Interrupt line to allocate
2271  * @handler:	Function to be called when the IRQ occurs.
2272  *		Threaded handler for threaded interrupts.
2273  * @irqflags:	Interrupt type flags
2274  * @name:	An ascii name for the claiming device
2275  * @dev_id:	A cookie passed back to the handler function
2276  *
2277  * This call allocates interrupt resources and enables the interrupt line
2278  * and IRQ handling. It sets up the IRQ line to be handled as an NMI.
2279  *
2280  * An interrupt line delivering NMIs cannot be shared and IRQ handling
2281  * cannot be threaded.
2282  *
2283  * Interrupt lines requested for NMI delivering must produce per cpu
2284  * interrupts and have auto enabling setting disabled.
2285  *
2286  * @dev_id must be globally unique. Normally the address of the device data
2287  * structure is used as the cookie. Since the handler receives this value
2288  * it makes sense to use it.
2289  *
2290  * If the interrupt line cannot be used to deliver NMIs, function will fail
2291  * and return a negative value.
2292  */
2293 int request_nmi(unsigned int irq, irq_handler_t handler,
2294 		unsigned long irqflags, const char *name, void *dev_id)
2295 {
2296 	struct irqaction *action;
2297 	struct irq_desc *desc;
2298 	int retval;
2299 
2300 	if (irq == IRQ_NOTCONNECTED)
2301 		return -ENOTCONN;
2302 
2303 	/* NMI cannot be shared, used for Polling */
2304 	if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
2305 		return -EINVAL;
2306 
2307 	if (!(irqflags & IRQF_PERCPU))
2308 		return -EINVAL;
2309 
2310 	if (!handler)
2311 		return -EINVAL;
2312 
2313 	desc = irq_to_desc(irq);
2314 
2315 	if (!desc || (irq_settings_can_autoenable(desc) &&
2316 	    !(irqflags & IRQF_NO_AUTOEN)) ||
2317 	    !irq_settings_can_request(desc) ||
2318 	    WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
2319 	    !irq_supports_nmi(desc))
2320 		return -EINVAL;
2321 
2322 	action = kzalloc_obj(struct irqaction);
2323 	if (!action)
2324 		return -ENOMEM;
2325 
2326 	action->handler = handler;
2327 	action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
2328 	action->name = name;
2329 	action->dev_id = dev_id;
2330 
2331 	retval = irq_chip_pm_get(&desc->irq_data);
2332 	if (retval < 0)
2333 		goto err_out;
2334 
2335 	retval = __setup_irq(irq, desc, action);
2336 	if (retval)
2337 		goto err_irq_setup;
2338 
2339 	scoped_guard(raw_spinlock_irqsave, &desc->lock) {
2340 		/* Setup NMI state */
2341 		desc->istate |= IRQS_NMI;
2342 		retval = irq_nmi_setup(desc);
2343 	}
2344 
2345 	if (retval) {
2346 		__cleanup_nmi(irq, desc);
2347 		return -EINVAL;
2348 	}
2349 	return 0;
2350 
2351 err_irq_setup:
2352 	irq_chip_pm_put(&desc->irq_data);
2353 err_out:
2354 	kfree(action);
2355 
2356 	return retval;
2357 }
2358 
2359 void enable_percpu_irq(unsigned int irq, unsigned int type)
2360 {
2361 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) {
2362 		struct irq_desc *desc = scoped_irqdesc;
2363 
2364 		/*
2365 		 * If the trigger type is not specified by the caller, then
2366 		 * use the default for this interrupt.
2367 		 */
2368 		type &= IRQ_TYPE_SENSE_MASK;
2369 		if (type == IRQ_TYPE_NONE)
2370 			type = irqd_get_trigger_type(&desc->irq_data);
2371 
2372 		if (type != IRQ_TYPE_NONE) {
2373 			if (__irq_set_trigger(desc, type)) {
2374 				WARN(1, "failed to set type for IRQ%d\n", irq);
2375 				return;
2376 			}
2377 		}
2378 		irq_percpu_enable(desc, smp_processor_id());
2379 	}
2380 }
2381 EXPORT_SYMBOL_GPL(enable_percpu_irq);
2382 
2383 void enable_percpu_nmi(unsigned int irq, unsigned int type)
2384 {
2385 	enable_percpu_irq(irq, type);
2386 }
2387 
2388 /**
2389  * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
2390  * @irq:	Linux irq number to check for
2391  *
2392  * Must be called from a non migratable context. Returns the enable
2393  * state of a per cpu interrupt on the current cpu.
2394  */
2395 bool irq_percpu_is_enabled(unsigned int irq)
2396 {
2397 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU)
2398 		return cpumask_test_cpu(smp_processor_id(), scoped_irqdesc->percpu_enabled);
2399 	return false;
2400 }
2401 EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
2402 
2403 void disable_percpu_irq(unsigned int irq)
2404 {
2405 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU)
2406 		irq_percpu_disable(scoped_irqdesc, smp_processor_id());
2407 }
2408 EXPORT_SYMBOL_GPL(disable_percpu_irq);
2409 
2410 void disable_percpu_nmi(unsigned int irq)
2411 {
2412 	disable_percpu_irq(irq);
2413 }
2414 
2415 /*
2416  * Internal function to unregister a percpu irqaction.
2417  */
2418 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2419 {
2420 	struct irq_desc *desc = irq_to_desc(irq);
2421 	struct irqaction *action, **action_ptr;
2422 
2423 	WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
2424 
2425 	if (!desc)
2426 		return NULL;
2427 
2428 	scoped_guard(raw_spinlock_irqsave, &desc->lock) {
2429 		action_ptr = &desc->action;
2430 		for (;;) {
2431 			action = *action_ptr;
2432 
2433 			if (!action) {
2434 				WARN(1, "Trying to free already-free IRQ %d\n", irq);
2435 				return NULL;
2436 			}
2437 
2438 			if (action->percpu_dev_id == dev_id)
2439 				break;
2440 
2441 			action_ptr = &action->next;
2442 		}
2443 
2444 		if (cpumask_intersects(desc->percpu_enabled, action->affinity)) {
2445 			WARN(1, "percpu IRQ %d still enabled on CPU%d!\n", irq,
2446 			     cpumask_first_and(desc->percpu_enabled, action->affinity));
2447 			return NULL;
2448 		}
2449 
2450 		/* Found it - now remove it from the list of entries: */
2451 		*action_ptr = action->next;
2452 
2453 		/* Demote from NMI if we killed the last action */
2454 		if (!desc->action) {
2455 			desc->istate &= ~IRQS_NMI;
2456 			irq_proc_update_valid(desc);
2457 		}
2458 	}
2459 
2460 	unregister_handler_proc(irq, action);
2461 	irq_chip_pm_put(&desc->irq_data);
2462 	module_put(desc->owner);
2463 	return action;
2464 }
2465 
2466 /**
2467  * free_percpu_irq - free an interrupt allocated with request_percpu_irq
2468  * @irq:	Interrupt line to free
2469  * @dev_id:	Device identity to free
2470  *
2471  * Remove a percpu interrupt handler. The handler is removed, but the
2472  * interrupt line is not disabled. This must be done on each CPU before
2473  * calling this function. The function does not return until any executing
2474  * interrupts for this IRQ have completed.
2475  *
2476  * This function must not be called from interrupt context.
2477  */
2478 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2479 {
2480 	struct irq_desc *desc = irq_to_desc(irq);
2481 
2482 	if (!desc || !irq_settings_is_per_cpu_devid(desc))
2483 		return;
2484 
2485 	chip_bus_lock(desc);
2486 	kfree(__free_percpu_irq(irq, dev_id));
2487 	chip_bus_sync_unlock(desc);
2488 }
2489 EXPORT_SYMBOL_GPL(free_percpu_irq);
2490 
2491 void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
2492 {
2493 	struct irq_desc *desc = irq_to_desc(irq);
2494 
2495 	if (!desc || !irq_settings_is_per_cpu_devid(desc))
2496 		return;
2497 
2498 	if (WARN_ON(!irq_is_nmi(desc)))
2499 		return;
2500 
2501 	kfree(__free_percpu_irq(irq, dev_id));
2502 }
2503 
2504 static
2505 struct irqaction *create_percpu_irqaction(irq_handler_t handler, unsigned long flags,
2506 					  const char *devname, const cpumask_t *affinity,
2507 					  void __percpu *dev_id)
2508 {
2509 	struct irqaction *action;
2510 
2511 	if (!affinity)
2512 		affinity = cpu_possible_mask;
2513 
2514 	action = kzalloc_obj(struct irqaction);
2515 	if (!action)
2516 		return NULL;
2517 
2518 	action->handler = handler;
2519 	action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
2520 	action->name = devname;
2521 	action->percpu_dev_id = dev_id;
2522 	action->affinity = affinity;
2523 
2524 	/*
2525 	 * We allow some form of sharing for non-overlapping affinity
2526 	 * masks. Obviously, covering all CPUs prevents any sharing in
2527 	 * the first place.
2528 	 */
2529 	if (!cpumask_equal(affinity, cpu_possible_mask))
2530 		action->flags |= IRQF_SHARED;
2531 
2532 	return action;
2533 }
2534 
2535 /**
2536  * request_percpu_irq_affinity - allocate a percpu interrupt line
2537  * @irq:	Interrupt line to allocate
2538  * @handler:	Function to be called when the IRQ occurs.
2539  * @devname:	An ascii name for the claiming device
2540  * @affinity:	A cpumask describing the target CPUs for this interrupt
2541  * @dev_id:	A percpu cookie passed back to the handler function
2542  *
2543  * This call allocates interrupt resources, but doesn't enable the interrupt
2544  * on any CPU, as all percpu-devid interrupts are flagged with IRQ_NOAUTOEN.
2545  * It has to be done on each CPU using enable_percpu_irq().
2546  *
2547  * @dev_id must be globally unique. It is a per-cpu variable, and
2548  * the handler gets called with the interrupted CPU's instance of
2549  * that variable.
2550  */
2551 int request_percpu_irq_affinity(unsigned int irq, irq_handler_t handler, const char *devname,
2552 				const cpumask_t *affinity, void __percpu *dev_id)
2553 {
2554 	struct irqaction *action;
2555 	struct irq_desc *desc;
2556 	int retval;
2557 
2558 	if (!dev_id)
2559 		return -EINVAL;
2560 
2561 	desc = irq_to_desc(irq);
2562 	if (!desc || !irq_settings_can_request(desc) ||
2563 	    !irq_settings_is_per_cpu_devid(desc))
2564 		return -EINVAL;
2565 
2566 	action = create_percpu_irqaction(handler, 0, devname, affinity, dev_id);
2567 	if (!action)
2568 		return -ENOMEM;
2569 
2570 	retval = irq_chip_pm_get(&desc->irq_data);
2571 	if (retval < 0) {
2572 		kfree(action);
2573 		return retval;
2574 	}
2575 
2576 	retval = __setup_irq(irq, desc, action);
2577 
2578 	if (retval) {
2579 		irq_chip_pm_put(&desc->irq_data);
2580 		kfree(action);
2581 	}
2582 
2583 	return retval;
2584 }
2585 EXPORT_SYMBOL_GPL(request_percpu_irq_affinity);
2586 
2587 /**
2588  * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
2589  * @irq:	Interrupt line to allocate
2590  * @handler:	Function to be called when the IRQ occurs.
2591  * @name:	An ascii name for the claiming device
2592  * @affinity:	A cpumask describing the target CPUs for this interrupt
2593  * @dev_id:	A percpu cookie passed back to the handler function
2594  *
2595  * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
2596  * have to be setup on each CPU by calling prepare_percpu_nmi() before
2597  * being enabled on the same CPU by using enable_percpu_nmi().
2598  *
2599  * @dev_id must be globally unique. It is a per-cpu variable, and the
2600  * handler gets called with the interrupted CPU's instance of that
2601  * variable.
2602  *
2603  * Interrupt lines requested for NMI delivering should have auto enabling
2604  * setting disabled.
2605  *
2606  * If the interrupt line cannot be used to deliver NMIs, function
2607  * will fail returning a negative value.
2608  */
2609 int request_percpu_nmi(unsigned int irq, irq_handler_t handler, const char *name,
2610 		       const struct cpumask *affinity, void __percpu *dev_id)
2611 {
2612 	struct irqaction *action;
2613 	struct irq_desc *desc;
2614 	int retval;
2615 
2616 	if (!handler)
2617 		return -EINVAL;
2618 
2619 	desc = irq_to_desc(irq);
2620 
2621 	if (!desc || !irq_settings_can_request(desc) ||
2622 	    !irq_settings_is_per_cpu_devid(desc) ||
2623 	    irq_settings_can_autoenable(desc) ||
2624 	    !irq_supports_nmi(desc))
2625 		return -EINVAL;
2626 
2627 	/* The line cannot be NMI already if the new request covers all CPUs */
2628 	if (irq_is_nmi(desc) &&
2629 	    (!affinity || cpumask_equal(affinity, cpu_possible_mask)))
2630 		return -EINVAL;
2631 
2632 	action = create_percpu_irqaction(handler, IRQF_NO_THREAD | IRQF_NOBALANCING,
2633 					 name, affinity, dev_id);
2634 	if (!action)
2635 		return -ENOMEM;
2636 
2637 	retval = irq_chip_pm_get(&desc->irq_data);
2638 	if (retval < 0)
2639 		goto err_out;
2640 
2641 	retval = __setup_irq(irq, desc, action);
2642 	if (retval)
2643 		goto err_irq_setup;
2644 
2645 	scoped_guard(raw_spinlock_irqsave, &desc->lock)
2646 		desc->istate |= IRQS_NMI;
2647 	return 0;
2648 
2649 err_irq_setup:
2650 	irq_chip_pm_put(&desc->irq_data);
2651 err_out:
2652 	kfree(action);
2653 
2654 	return retval;
2655 }
2656 
2657 /**
2658  * prepare_percpu_nmi - performs CPU local setup for NMI delivery
2659  * @irq: Interrupt line to prepare for NMI delivery
2660  *
2661  * This call prepares an interrupt line to deliver NMI on the current CPU,
2662  * before that interrupt line gets enabled with enable_percpu_nmi().
2663  *
2664  * As a CPU local operation, this should be called from non-preemptible
2665  * context.
2666  *
2667  * If the interrupt line cannot be used to deliver NMIs, function will fail
2668  * returning a negative value.
2669  */
2670 int prepare_percpu_nmi(unsigned int irq)
2671 {
2672 	int ret = -EINVAL;
2673 
2674 	WARN_ON(preemptible());
2675 
2676 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) {
2677 		if (WARN(!irq_is_nmi(scoped_irqdesc),
2678 			 "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n", irq))
2679 			return -EINVAL;
2680 
2681 		ret = irq_nmi_setup(scoped_irqdesc);
2682 		if (ret)
2683 			pr_err("Failed to setup NMI delivery: irq %u\n", irq);
2684 	}
2685 	return ret;
2686 }
2687 
2688 /**
2689  * teardown_percpu_nmi - undoes NMI setup of IRQ line
2690  * @irq: Interrupt line from which CPU local NMI configuration should be removed
2691  *
2692  * This call undoes the setup done by prepare_percpu_nmi().
2693  *
2694  * IRQ line should not be enabled for the current CPU.
2695  * As a CPU local operation, this should be called from non-preemptible
2696  * context.
2697  */
2698 void teardown_percpu_nmi(unsigned int irq)
2699 {
2700 	WARN_ON(preemptible());
2701 
2702 	scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) {
2703 		if (WARN_ON(!irq_is_nmi(scoped_irqdesc)))
2704 			return;
2705 		irq_nmi_teardown(scoped_irqdesc);
2706 	}
2707 }
2708 
2709 static int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which, bool *state)
2710 {
2711 	struct irq_chip *chip;
2712 	int err = -EINVAL;
2713 
2714 	do {
2715 		chip = irq_data_get_irq_chip(data);
2716 		if (WARN_ON_ONCE(!chip))
2717 			return -ENODEV;
2718 		if (chip->irq_get_irqchip_state)
2719 			break;
2720 		data = irqd_get_parent_data(data);
2721 	} while (data);
2722 
2723 	if (data)
2724 		err = chip->irq_get_irqchip_state(data, which, state);
2725 	return err;
2726 }
2727 
2728 /**
2729  * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2730  * @irq:	Interrupt line that is forwarded to a VM
2731  * @which:	One of IRQCHIP_STATE_* the caller wants to know about
2732  * @state:	a pointer to a boolean where the state is to be stored
2733  *
2734  * This call snapshots the internal irqchip state of an interrupt,
2735  * returning into @state the bit corresponding to stage @which
2736  *
2737  * This function should be called with preemption disabled if the interrupt
2738  * controller has per-cpu registers.
2739  */
2740 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which, bool *state)
2741 {
2742 	scoped_irqdesc_get_and_buslock(irq, 0) {
2743 		struct irq_data *data = irq_desc_get_irq_data(scoped_irqdesc);
2744 
2745 		return __irq_get_irqchip_state(data, which, state);
2746 	}
2747 	return -EINVAL;
2748 }
2749 EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
2750 
2751 /**
2752  * irq_set_irqchip_state - set the state of a forwarded interrupt.
2753  * @irq:	Interrupt line that is forwarded to a VM
2754  * @which:	State to be restored (one of IRQCHIP_STATE_*)
2755  * @val:	Value corresponding to @which
2756  *
2757  * This call sets the internal irqchip state of an interrupt, depending on
2758  * the value of @which.
2759  *
2760  * This function should be called with migration disabled if the interrupt
2761  * controller has per-cpu registers.
2762  */
2763 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which, bool val)
2764 {
2765 	scoped_irqdesc_get_and_buslock(irq, 0) {
2766 		struct irq_data *data = irq_desc_get_irq_data(scoped_irqdesc);
2767 		struct irq_chip *chip;
2768 
2769 		do {
2770 			chip = irq_data_get_irq_chip(data);
2771 
2772 			if (WARN_ON_ONCE(!chip))
2773 				return -ENODEV;
2774 
2775 			if (chip->irq_set_irqchip_state)
2776 				break;
2777 
2778 			data = irqd_get_parent_data(data);
2779 		} while (data);
2780 
2781 		if (data)
2782 			return chip->irq_set_irqchip_state(data, which, val);
2783 	}
2784 	return -EINVAL;
2785 }
2786 EXPORT_SYMBOL_GPL(irq_set_irqchip_state);
2787 
2788 /**
2789  * irq_has_action - Check whether an interrupt is requested
2790  * @irq:	The linux irq number
2791  *
2792  * Returns: A snapshot of the current state
2793  */
2794 bool irq_has_action(unsigned int irq)
2795 {
2796 	bool res;
2797 
2798 	rcu_read_lock();
2799 	res = irq_desc_has_action(irq_to_desc(irq));
2800 	rcu_read_unlock();
2801 	return res;
2802 }
2803 EXPORT_SYMBOL_GPL(irq_has_action);
2804 
2805 /**
2806  * irq_check_status_bit - Check whether bits in the irq descriptor status are set
2807  * @irq:	The linux irq number
2808  * @bitmask:	The bitmask to evaluate
2809  *
2810  * Returns: True if one of the bits in @bitmask is set
2811  */
2812 bool irq_check_status_bit(unsigned int irq, unsigned int bitmask)
2813 {
2814 	struct irq_desc *desc;
2815 	bool res = false;
2816 
2817 	rcu_read_lock();
2818 	desc = irq_to_desc(irq);
2819 	if (desc)
2820 		res = !!(desc->status_use_accessors & bitmask);
2821 	rcu_read_unlock();
2822 	return res;
2823 }
2824 EXPORT_SYMBOL_GPL(irq_check_status_bit);
2825