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