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