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