xref: /linux/kernel/irq/chip.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
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, Russell King
5  *
6  * This file contains the core interrupt handling code, for irq-chip based
7  * architectures. Detailed information is available in
8  * Documentation/core-api/genericirq.rst
9  */
10 
11 #include <linux/irq.h>
12 #include <linux/msi.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/irqdomain.h>
17 
18 #include <trace/events/irq.h>
19 
20 #include "internals.h"
21 
22 static irqreturn_t bad_chained_irq(int irq, void *dev_id)
23 {
24 	WARN_ONCE(1, "Chained irq %d should not call an action\n", irq);
25 	return IRQ_NONE;
26 }
27 
28 /*
29  * Chained handlers should never call action on their IRQ. This default
30  * action will emit warning if such thing happens.
31  */
32 struct irqaction chained_action = {
33 	.handler = bad_chained_irq,
34 };
35 
36 /**
37  * irq_set_chip - set the irq chip for an irq
38  * @irq:	irq number
39  * @chip:	pointer to irq chip description structure
40  */
41 int irq_set_chip(unsigned int irq, const struct irq_chip *chip)
42 {
43 	int ret = -EINVAL;
44 
45 	scoped_irqdesc_get_and_lock(irq, 0) {
46 		scoped_irqdesc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
47 		ret = 0;
48 	}
49 	/* For !CONFIG_SPARSE_IRQ make the irq show up in allocated_irqs. */
50 	if (!ret)
51 		irq_mark_irq(irq);
52 	return ret;
53 }
54 EXPORT_SYMBOL(irq_set_chip);
55 
56 /**
57  * irq_set_irq_type - set the irq trigger type for an irq
58  * @irq:	irq number
59  * @type:	IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
60  */
61 int irq_set_irq_type(unsigned int irq, unsigned int type)
62 {
63 	scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL)
64 		return __irq_set_trigger(scoped_irqdesc, type);
65 	return -EINVAL;
66 }
67 EXPORT_SYMBOL(irq_set_irq_type);
68 
69 /**
70  * irq_set_handler_data - set irq handler data for an irq
71  * @irq:	Interrupt number
72  * @data:	Pointer to interrupt specific data
73  *
74  * Set the hardware irq controller data for an irq
75  */
76 int irq_set_handler_data(unsigned int irq, void *data)
77 {
78 	scoped_irqdesc_get_and_lock(irq, 0) {
79 		scoped_irqdesc->irq_common_data.handler_data = data;
80 		return 0;
81 	}
82 	return -EINVAL;
83 }
84 EXPORT_SYMBOL(irq_set_handler_data);
85 
86 /**
87  * irq_set_msi_desc_off - set MSI descriptor data for an irq at offset
88  * @irq_base:	Interrupt number base
89  * @irq_offset:	Interrupt number offset
90  * @entry:		Pointer to MSI descriptor data
91  *
92  * Set the MSI descriptor entry for an irq at offset
93  */
94 int irq_set_msi_desc_off(unsigned int irq_base, unsigned int irq_offset, struct msi_desc *entry)
95 {
96 	scoped_irqdesc_get_and_lock(irq_base + irq_offset, IRQ_GET_DESC_CHECK_GLOBAL) {
97 		scoped_irqdesc->irq_common_data.msi_desc = entry;
98 		if (entry && !irq_offset)
99 			entry->irq = irq_base;
100 		return 0;
101 	}
102 	return -EINVAL;
103 }
104 
105 /**
106  * irq_set_msi_desc - set MSI descriptor data for an irq
107  * @irq:	Interrupt number
108  * @entry:	Pointer to MSI descriptor data
109  *
110  * Set the MSI descriptor entry for an irq
111  */
112 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
113 {
114 	return irq_set_msi_desc_off(irq, 0, entry);
115 }
116 
117 /**
118  * irq_set_chip_data - set irq chip data for an irq
119  * @irq:	Interrupt number
120  * @data:	Pointer to chip specific data
121  *
122  * Set the hardware irq chip data for an irq
123  */
124 int irq_set_chip_data(unsigned int irq, void *data)
125 {
126 	scoped_irqdesc_get_and_lock(irq, 0) {
127 		scoped_irqdesc->irq_data.chip_data = data;
128 		return 0;
129 	}
130 	return -EINVAL;
131 }
132 EXPORT_SYMBOL(irq_set_chip_data);
133 
134 struct irq_data *irq_get_irq_data(unsigned int irq)
135 {
136 	struct irq_desc *desc = irq_to_desc(irq);
137 
138 	return desc ? &desc->irq_data : NULL;
139 }
140 EXPORT_SYMBOL_GPL(irq_get_irq_data);
141 
142 static void irq_state_clr_disabled(struct irq_desc *desc)
143 {
144 	irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
145 }
146 
147 static void irq_state_clr_masked(struct irq_desc *desc)
148 {
149 	irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
150 }
151 
152 static void irq_state_clr_started(struct irq_desc *desc)
153 {
154 	irqd_clear(&desc->irq_data, IRQD_IRQ_STARTED);
155 }
156 
157 static void irq_state_set_started(struct irq_desc *desc)
158 {
159 	irqd_set(&desc->irq_data, IRQD_IRQ_STARTED);
160 }
161 
162 enum {
163 	IRQ_STARTUP_NORMAL,
164 	IRQ_STARTUP_MANAGED,
165 	IRQ_STARTUP_ABORT,
166 };
167 
168 #ifdef CONFIG_SMP
169 static int
170 __irq_startup_managed(struct irq_desc *desc, const struct cpumask *aff,
171 		      bool force)
172 {
173 	struct irq_data *d = irq_desc_get_irq_data(desc);
174 
175 	if (!irqd_affinity_is_managed(d))
176 		return IRQ_STARTUP_NORMAL;
177 
178 	irqd_clr_managed_shutdown(d);
179 
180 	if (!cpumask_intersects(aff, cpu_online_mask)) {
181 		/*
182 		 * Catch code which fiddles with enable_irq() on a managed
183 		 * and potentially shutdown IRQ. Chained interrupt
184 		 * installment or irq auto probing should not happen on
185 		 * managed irqs either.
186 		 */
187 		if (WARN_ON_ONCE(force))
188 			return IRQ_STARTUP_ABORT;
189 		/*
190 		 * The interrupt was requested, but there is no online CPU
191 		 * in it's affinity mask. Put it into managed shutdown
192 		 * state and let the cpu hotplug mechanism start it up once
193 		 * a CPU in the mask becomes available.
194 		 */
195 		return IRQ_STARTUP_ABORT;
196 	}
197 	/*
198 	 * Managed interrupts have reserved resources, so this should not
199 	 * happen.
200 	 */
201 	if (WARN_ON(irq_domain_activate_irq(d, false)))
202 		return IRQ_STARTUP_ABORT;
203 	return IRQ_STARTUP_MANAGED;
204 }
205 
206 void irq_startup_managed(struct irq_desc *desc)
207 {
208 	struct irq_data *d = irq_desc_get_irq_data(desc);
209 
210 	/*
211 	 * Clear managed-shutdown flag, so we don't repeat managed-startup for
212 	 * multiple hotplugs, and cause imbalanced disable depth.
213 	 */
214 	irqd_clr_managed_shutdown(d);
215 
216 	/*
217 	 * Only start it up when the disable depth is 1, so that a disable,
218 	 * hotunplug, hotplug sequence does not end up enabling it during
219 	 * hotplug unconditionally.
220 	 */
221 	desc->depth--;
222 	if (!desc->depth)
223 		irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
224 }
225 
226 #else
227 static __always_inline int
228 __irq_startup_managed(struct irq_desc *desc, const struct cpumask *aff,
229 		      bool force)
230 {
231 	return IRQ_STARTUP_NORMAL;
232 }
233 #endif
234 
235 static void irq_enable(struct irq_desc *desc)
236 {
237 	if (!irqd_irq_disabled(&desc->irq_data)) {
238 		unmask_irq(desc);
239 	} else {
240 		irq_state_clr_disabled(desc);
241 		if (desc->irq_data.chip->irq_enable) {
242 			desc->irq_data.chip->irq_enable(&desc->irq_data);
243 			irq_state_clr_masked(desc);
244 		} else {
245 			unmask_irq(desc);
246 		}
247 	}
248 }
249 
250 static int __irq_startup(struct irq_desc *desc)
251 {
252 	struct irq_data *d = irq_desc_get_irq_data(desc);
253 	int ret = 0;
254 
255 	/* Warn if this interrupt is not activated but try nevertheless */
256 	WARN_ON_ONCE(!irqd_is_activated(d));
257 
258 	if (d->chip->irq_startup) {
259 		ret = d->chip->irq_startup(d);
260 		irq_state_clr_disabled(desc);
261 		irq_state_clr_masked(desc);
262 	} else {
263 		irq_enable(desc);
264 	}
265 	irq_state_set_started(desc);
266 	return ret;
267 }
268 
269 int irq_startup(struct irq_desc *desc, bool resend, bool force)
270 {
271 	struct irq_data *d = irq_desc_get_irq_data(desc);
272 	const struct cpumask *aff = irq_data_get_affinity_mask(d);
273 	int ret = 0;
274 
275 	desc->depth = 0;
276 
277 	if (irqd_is_started(d)) {
278 		irq_enable(desc);
279 	} else {
280 		switch (__irq_startup_managed(desc, aff, force)) {
281 		case IRQ_STARTUP_NORMAL:
282 			if (d->chip->flags & IRQCHIP_AFFINITY_PRE_STARTUP)
283 				irq_setup_affinity(desc);
284 			ret = __irq_startup(desc);
285 			if (!(d->chip->flags & IRQCHIP_AFFINITY_PRE_STARTUP))
286 				irq_setup_affinity(desc);
287 			break;
288 		case IRQ_STARTUP_MANAGED:
289 			irq_do_set_affinity(d, aff, false);
290 			ret = __irq_startup(desc);
291 			break;
292 		case IRQ_STARTUP_ABORT:
293 			desc->depth = 1;
294 			irqd_set_managed_shutdown(d);
295 			return 0;
296 		}
297 	}
298 	if (resend)
299 		check_irq_resend(desc, false);
300 
301 	return ret;
302 }
303 
304 int irq_activate(struct irq_desc *desc)
305 {
306 	struct irq_data *d = irq_desc_get_irq_data(desc);
307 
308 	if (!irqd_affinity_is_managed(d))
309 		return irq_domain_activate_irq(d, false);
310 	return 0;
311 }
312 
313 int irq_activate_and_startup(struct irq_desc *desc, bool resend)
314 {
315 	if (WARN_ON(irq_activate(desc)))
316 		return 0;
317 	return irq_startup(desc, resend, IRQ_START_FORCE);
318 }
319 
320 static void __irq_disable(struct irq_desc *desc, bool mask);
321 
322 void irq_shutdown(struct irq_desc *desc)
323 {
324 	if (irqd_is_started(&desc->irq_data)) {
325 		clear_irq_resend(desc);
326 		/*
327 		 * Increment disable depth, so that a managed shutdown on
328 		 * CPU hotunplug preserves the actual disabled state when the
329 		 * CPU comes back online. See irq_startup_managed().
330 		 */
331 		desc->depth++;
332 
333 		if (desc->irq_data.chip->irq_shutdown) {
334 			desc->irq_data.chip->irq_shutdown(&desc->irq_data);
335 			irq_state_set_disabled(desc);
336 			irq_state_set_masked(desc);
337 		} else {
338 			__irq_disable(desc, true);
339 		}
340 		irq_state_clr_started(desc);
341 	}
342 }
343 
344 
345 void irq_shutdown_and_deactivate(struct irq_desc *desc)
346 {
347 	irq_shutdown(desc);
348 	/*
349 	 * This must be called even if the interrupt was never started up,
350 	 * because the activation can happen before the interrupt is
351 	 * available for request/startup. It has it's own state tracking so
352 	 * it's safe to call it unconditionally.
353 	 */
354 	irq_domain_deactivate_irq(&desc->irq_data);
355 }
356 
357 static void __irq_disable(struct irq_desc *desc, bool mask)
358 {
359 	if (irqd_irq_disabled(&desc->irq_data)) {
360 		if (mask)
361 			mask_irq(desc);
362 	} else {
363 		irq_state_set_disabled(desc);
364 		if (desc->irq_data.chip->irq_disable) {
365 			desc->irq_data.chip->irq_disable(&desc->irq_data);
366 			irq_state_set_masked(desc);
367 		} else if (mask) {
368 			mask_irq(desc);
369 		}
370 	}
371 }
372 
373 /**
374  * irq_disable - Mark interrupt disabled
375  * @desc:	irq descriptor which should be disabled
376  *
377  * If the chip does not implement the irq_disable callback, we
378  * use a lazy disable approach. That means we mark the interrupt
379  * disabled, but leave the hardware unmasked. That's an
380  * optimization because we avoid the hardware access for the
381  * common case where no interrupt happens after we marked it
382  * disabled. If an interrupt happens, then the interrupt flow
383  * handler masks the line at the hardware level and marks it
384  * pending.
385  *
386  * If the interrupt chip does not implement the irq_disable callback,
387  * a driver can disable the lazy approach for a particular irq line by
388  * calling 'irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY)'. This can
389  * be used for devices which cannot disable the interrupt at the
390  * device level under certain circumstances and have to use
391  * disable_irq[_nosync] instead.
392  */
393 void irq_disable(struct irq_desc *desc)
394 {
395 	__irq_disable(desc, irq_settings_disable_unlazy(desc));
396 }
397 
398 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
399 {
400 	if (desc->irq_data.chip->irq_enable)
401 		desc->irq_data.chip->irq_enable(&desc->irq_data);
402 	else
403 		desc->irq_data.chip->irq_unmask(&desc->irq_data);
404 	cpumask_set_cpu(cpu, desc->percpu_enabled);
405 }
406 
407 void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
408 {
409 	if (desc->irq_data.chip->irq_disable)
410 		desc->irq_data.chip->irq_disable(&desc->irq_data);
411 	else
412 		desc->irq_data.chip->irq_mask(&desc->irq_data);
413 	cpumask_clear_cpu(cpu, desc->percpu_enabled);
414 }
415 
416 static inline void mask_ack_irq(struct irq_desc *desc)
417 {
418 	if (desc->irq_data.chip->irq_mask_ack) {
419 		desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
420 		irq_state_set_masked(desc);
421 	} else {
422 		mask_irq(desc);
423 		if (desc->irq_data.chip->irq_ack)
424 			desc->irq_data.chip->irq_ack(&desc->irq_data);
425 	}
426 }
427 
428 void mask_irq(struct irq_desc *desc)
429 {
430 	if (irqd_irq_masked(&desc->irq_data))
431 		return;
432 
433 	if (desc->irq_data.chip->irq_mask) {
434 		desc->irq_data.chip->irq_mask(&desc->irq_data);
435 		irq_state_set_masked(desc);
436 	}
437 }
438 
439 void unmask_irq(struct irq_desc *desc)
440 {
441 	if (!irqd_irq_masked(&desc->irq_data))
442 		return;
443 
444 	if (desc->irq_data.chip->irq_unmask) {
445 		desc->irq_data.chip->irq_unmask(&desc->irq_data);
446 		irq_state_clr_masked(desc);
447 	}
448 }
449 
450 void unmask_threaded_irq(struct irq_desc *desc)
451 {
452 	struct irq_chip *chip = desc->irq_data.chip;
453 
454 	if (chip->flags & IRQCHIP_EOI_THREADED)
455 		chip->irq_eoi(&desc->irq_data);
456 
457 	unmask_irq(desc);
458 }
459 
460 /* Busy wait until INPROGRESS is cleared */
461 static bool irq_wait_on_inprogress(struct irq_desc *desc)
462 {
463 	if (IS_ENABLED(CONFIG_SMP)) {
464 		do {
465 			raw_spin_unlock(&desc->lock);
466 			while (irqd_irq_inprogress(&desc->irq_data))
467 				cpu_relax();
468 			raw_spin_lock(&desc->lock);
469 		} while (irqd_irq_inprogress(&desc->irq_data));
470 
471 		/* Might have been disabled in meantime */
472 		return !irqd_irq_disabled(&desc->irq_data) && desc->action;
473 	}
474 	return false;
475 }
476 
477 static bool irq_can_handle_pm(struct irq_desc *desc)
478 {
479 	struct irq_data *irqd = &desc->irq_data;
480 	const struct cpumask *aff;
481 
482 	/*
483 	 * If the interrupt is not in progress and is not an armed
484 	 * wakeup interrupt, proceed.
485 	 */
486 	if (!irqd_has_set(irqd, IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED))
487 		return true;
488 
489 	/*
490 	 * If the interrupt is an armed wakeup source, mark it pending
491 	 * and suspended, disable it and notify the pm core about the
492 	 * event.
493 	 */
494 	if (unlikely(irqd_has_set(irqd, IRQD_WAKEUP_ARMED))) {
495 		irq_pm_handle_wakeup(desc);
496 		return false;
497 	}
498 
499 	/* Check whether the interrupt is polled on another CPU */
500 	if (unlikely(desc->istate & IRQS_POLL_INPROGRESS)) {
501 		if (WARN_ONCE(irq_poll_cpu == smp_processor_id(),
502 			      "irq poll in progress on cpu %d for irq %d\n",
503 			      smp_processor_id(), desc->irq_data.irq))
504 			return false;
505 		return irq_wait_on_inprogress(desc);
506 	}
507 
508 	/* The below works only for single target interrupts */
509 	if (!IS_ENABLED(CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK) ||
510 	    !irqd_is_single_target(irqd) || desc->handle_irq != handle_edge_irq)
511 		return false;
512 
513 	/*
514 	 * If the interrupt affinity was moved to this CPU and the
515 	 * interrupt is currently handled on the previous target CPU, then
516 	 * busy wait for INPROGRESS to be cleared. Otherwise for edge type
517 	 * interrupts the handler might get stuck on the previous target:
518 	 *
519 	 * CPU 0			CPU 1 (new target)
520 	 * handle_edge_irq()
521 	 * repeat:
522 	 *	handle_event()		handle_edge_irq()
523 	 *			        if (INPROGESS) {
524 	 *				  set(PENDING);
525 	 *				  mask();
526 	 *				  return;
527 	 *				}
528 	 *	if (PENDING) {
529 	 *	  clear(PENDING);
530 	 *	  unmask();
531 	 *	  goto repeat;
532 	 *	}
533 	 *
534 	 * This happens when the device raises interrupts with a high rate
535 	 * and always before handle_event() completes and the CPU0 handler
536 	 * can clear INPROGRESS. This has been observed in virtual machines.
537 	 */
538 	aff = irq_data_get_effective_affinity_mask(irqd);
539 	if (cpumask_first(aff) != smp_processor_id())
540 		return false;
541 	return irq_wait_on_inprogress(desc);
542 }
543 
544 static inline bool irq_can_handle_actions(struct irq_desc *desc)
545 {
546 	desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
547 
548 	if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
549 		desc->istate |= IRQS_PENDING;
550 		return false;
551 	}
552 	return true;
553 }
554 
555 static inline bool irq_can_handle(struct irq_desc *desc)
556 {
557 	if (!irq_can_handle_pm(desc))
558 		return false;
559 
560 	return irq_can_handle_actions(desc);
561 }
562 
563 /**
564  * handle_nested_irq - Handle a nested irq from a irq thread
565  * @irq:	the interrupt number
566  *
567  * Handle interrupts which are nested into a threaded interrupt
568  * handler. The handler function is called inside the calling threads
569  * context.
570  */
571 void handle_nested_irq(unsigned int irq)
572 {
573 	struct irq_desc *desc = irq_to_desc(irq);
574 	struct irqaction *action;
575 	irqreturn_t action_ret;
576 
577 	might_sleep();
578 
579 	scoped_guard(raw_spinlock_irq, &desc->lock) {
580 		if (!irq_can_handle_actions(desc))
581 			return;
582 
583 		action = desc->action;
584 		kstat_incr_irqs_this_cpu(desc);
585 		atomic_inc(&desc->threads_active);
586 	}
587 
588 	action_ret = IRQ_NONE;
589 	for_each_action_of_desc(desc, action)
590 		action_ret |= action->thread_fn(action->irq, action->dev_id);
591 
592 	if (!irq_settings_no_debug(desc))
593 		note_interrupt(desc, action_ret);
594 
595 	wake_threads_waitq(desc);
596 }
597 EXPORT_SYMBOL_GPL(handle_nested_irq);
598 
599 /**
600  * handle_simple_irq - Simple and software-decoded IRQs.
601  * @desc:	the interrupt description structure for this irq
602  *
603  * Simple interrupts are either sent from a demultiplexing interrupt
604  * handler or come from hardware, where no interrupt hardware control is
605  * necessary.
606  *
607  * Note: The caller is expected to handle the ack, clear, mask and unmask
608  * issues if necessary.
609  */
610 void handle_simple_irq(struct irq_desc *desc)
611 {
612 	guard(raw_spinlock)(&desc->lock);
613 
614 	if (!irq_can_handle(desc))
615 		return;
616 
617 	kstat_incr_irqs_this_cpu(desc);
618 	handle_irq_event(desc);
619 }
620 EXPORT_SYMBOL_GPL(handle_simple_irq);
621 
622 /**
623  * handle_untracked_irq - Simple and software-decoded IRQs.
624  * @desc:	the interrupt description structure for this irq
625  *
626  * Untracked interrupts are sent from a demultiplexing interrupt handler
627  * when the demultiplexer does not know which device it its multiplexed irq
628  * domain generated the interrupt. IRQ's handled through here are not
629  * subjected to stats tracking, randomness, or spurious interrupt
630  * detection.
631  *
632  * Note: Like handle_simple_irq, the caller is expected to handle the ack,
633  * clear, mask and unmask issues if necessary.
634  */
635 void handle_untracked_irq(struct irq_desc *desc)
636 {
637 	scoped_guard(raw_spinlock, &desc->lock) {
638 		if (!irq_can_handle(desc))
639 			return;
640 
641 		desc->istate &= ~IRQS_PENDING;
642 		irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
643 	}
644 
645 	__handle_irq_event_percpu(desc);
646 
647 	scoped_guard(raw_spinlock, &desc->lock)
648 		irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
649 }
650 EXPORT_SYMBOL_GPL(handle_untracked_irq);
651 
652 /*
653  * Called unconditionally from handle_level_irq() and only for oneshot
654  * interrupts from handle_fasteoi_irq()
655  */
656 static void cond_unmask_irq(struct irq_desc *desc)
657 {
658 	/*
659 	 * We need to unmask in the following cases:
660 	 * - Standard level irq (IRQF_ONESHOT is not set)
661 	 * - Oneshot irq which did not wake the thread (caused by a
662 	 *   spurious interrupt or a primary handler handling it
663 	 *   completely).
664 	 */
665 	if (!irqd_irq_disabled(&desc->irq_data) &&
666 	    irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
667 		unmask_irq(desc);
668 }
669 
670 /**
671  * handle_level_irq - Level type irq handler
672  * @desc:	the interrupt description structure for this irq
673  *
674  * Level type interrupts are active as long as the hardware line has the
675  * active level. This may require to mask the interrupt and unmask it after
676  * the associated handler has acknowledged the device, so the interrupt
677  * line is back to inactive.
678  */
679 void handle_level_irq(struct irq_desc *desc)
680 {
681 	guard(raw_spinlock)(&desc->lock);
682 	mask_ack_irq(desc);
683 
684 	if (!irq_can_handle(desc))
685 		return;
686 
687 	kstat_incr_irqs_this_cpu(desc);
688 	handle_irq_event(desc);
689 
690 	cond_unmask_irq(desc);
691 }
692 EXPORT_SYMBOL_GPL(handle_level_irq);
693 
694 static void cond_unmask_eoi_irq(struct irq_desc *desc, struct irq_chip *chip)
695 {
696 	if (!(desc->istate & IRQS_ONESHOT)) {
697 		chip->irq_eoi(&desc->irq_data);
698 		return;
699 	}
700 	/*
701 	 * We need to unmask in the following cases:
702 	 * - Oneshot irq which did not wake the thread (caused by a
703 	 *   spurious interrupt or a primary handler handling it
704 	 *   completely).
705 	 */
706 	if (!irqd_irq_disabled(&desc->irq_data) &&
707 	    irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot) {
708 		chip->irq_eoi(&desc->irq_data);
709 		unmask_irq(desc);
710 	} else if (!(chip->flags & IRQCHIP_EOI_THREADED)) {
711 		chip->irq_eoi(&desc->irq_data);
712 	}
713 }
714 
715 static inline void cond_eoi_irq(struct irq_chip *chip, struct irq_data *data)
716 {
717 	if (!(chip->flags & IRQCHIP_EOI_IF_HANDLED))
718 		chip->irq_eoi(data);
719 }
720 
721 /**
722  * handle_fasteoi_irq - irq handler for transparent controllers
723  * @desc:	the interrupt description structure for this irq
724  *
725  * Only a single callback will be issued to the chip: an ->eoi() call when
726  * the interrupt has been serviced. This enables support for modern forms
727  * of interrupt handlers, which handle the flow details in hardware,
728  * transparently.
729  */
730 void handle_fasteoi_irq(struct irq_desc *desc)
731 {
732 	struct irq_chip *chip = desc->irq_data.chip;
733 
734 	guard(raw_spinlock)(&desc->lock);
735 
736 	/*
737 	 * When an affinity change races with IRQ handling, the next interrupt
738 	 * can arrive on the new CPU before the original CPU has completed
739 	 * handling the previous one - it may need to be resent.
740 	 */
741 	if (!irq_can_handle_pm(desc)) {
742 		if (irqd_needs_resend_when_in_progress(&desc->irq_data))
743 			desc->istate |= IRQS_PENDING;
744 		cond_eoi_irq(chip, &desc->irq_data);
745 		return;
746 	}
747 
748 	if (!irq_can_handle_actions(desc)) {
749 		mask_irq(desc);
750 		cond_eoi_irq(chip, &desc->irq_data);
751 		return;
752 	}
753 
754 	kstat_incr_irqs_this_cpu(desc);
755 	if (desc->istate & IRQS_ONESHOT)
756 		mask_irq(desc);
757 
758 	handle_irq_event(desc);
759 
760 	cond_unmask_eoi_irq(desc, chip);
761 
762 	/*
763 	 * When the race described above happens this will resend the interrupt.
764 	 */
765 	if (unlikely(desc->istate & IRQS_PENDING))
766 		check_irq_resend(desc, false);
767 }
768 EXPORT_SYMBOL_GPL(handle_fasteoi_irq);
769 
770 /**
771  *	handle_fasteoi_nmi - irq handler for NMI interrupt lines
772  *	@desc:	the interrupt description structure for this irq
773  *
774  *	A simple NMI-safe handler, considering the restrictions
775  *	from request_nmi.
776  *
777  *	Only a single callback will be issued to the chip: an ->eoi()
778  *	call when the interrupt has been serviced. This enables support
779  *	for modern forms of interrupt handlers, which handle the flow
780  *	details in hardware, transparently.
781  */
782 void handle_fasteoi_nmi(struct irq_desc *desc)
783 {
784 	struct irq_chip *chip = irq_desc_get_chip(desc);
785 	struct irqaction *action = desc->action;
786 	unsigned int irq = irq_desc_get_irq(desc);
787 	irqreturn_t res;
788 
789 	__kstat_incr_irqs_this_cpu(desc);
790 
791 	trace_irq_handler_entry(irq, action);
792 	/*
793 	 * NMIs cannot be shared, there is only one action.
794 	 */
795 	res = action->handler(irq, action->dev_id);
796 	trace_irq_handler_exit(irq, action, res);
797 
798 	if (chip->irq_eoi)
799 		chip->irq_eoi(&desc->irq_data);
800 }
801 EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
802 
803 /**
804  * handle_edge_irq - edge type IRQ handler
805  * @desc:	the interrupt description structure for this irq
806  *
807  * Interrupt occurs on the falling and/or rising edge of a hardware
808  * signal. The occurrence is latched into the irq controller hardware and
809  * must be acked in order to be reenabled. After the ack another interrupt
810  * can happen on the same source even before the first one is handled by
811  * the associated event handler. If this happens it might be necessary to
812  * disable (mask) the interrupt depending on the controller hardware. This
813  * requires to reenable the interrupt inside of the loop which handles the
814  * interrupts which have arrived while the handler was running. If all
815  * pending interrupts are handled, the loop is left.
816  */
817 void handle_edge_irq(struct irq_desc *desc)
818 {
819 	guard(raw_spinlock)(&desc->lock);
820 
821 	if (!irq_can_handle(desc)) {
822 		desc->istate |= IRQS_PENDING;
823 		mask_ack_irq(desc);
824 		return;
825 	}
826 
827 	kstat_incr_irqs_this_cpu(desc);
828 
829 	/* Start handling the irq */
830 	desc->irq_data.chip->irq_ack(&desc->irq_data);
831 
832 	do {
833 		if (unlikely(!desc->action)) {
834 			mask_irq(desc);
835 			return;
836 		}
837 
838 		/*
839 		 * When another irq arrived while we were handling
840 		 * one, we could have masked the irq.
841 		 * Reenable it, if it was not disabled in meantime.
842 		 */
843 		if (unlikely(desc->istate & IRQS_PENDING)) {
844 			if (!irqd_irq_disabled(&desc->irq_data) &&
845 			    irqd_irq_masked(&desc->irq_data))
846 				unmask_irq(desc);
847 		}
848 
849 		handle_irq_event(desc);
850 
851 	} while ((desc->istate & IRQS_PENDING) && !irqd_irq_disabled(&desc->irq_data));
852 }
853 EXPORT_SYMBOL(handle_edge_irq);
854 
855 /**
856  *	handle_percpu_irq - Per CPU local irq handler
857  *	@desc:	the interrupt description structure for this irq
858  *
859  *	Per CPU interrupts on SMP machines without locking requirements
860  */
861 void handle_percpu_irq(struct irq_desc *desc)
862 {
863 	struct irq_chip *chip = irq_desc_get_chip(desc);
864 
865 	/*
866 	 * PER CPU interrupts are not serialized. Do not touch
867 	 * desc->tot_count.
868 	 */
869 	__kstat_incr_irqs_this_cpu(desc);
870 
871 	if (chip->irq_ack)
872 		chip->irq_ack(&desc->irq_data);
873 
874 	handle_irq_event_percpu(desc);
875 
876 	if (chip->irq_eoi)
877 		chip->irq_eoi(&desc->irq_data);
878 }
879 
880 /**
881  * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
882  * @desc:	the interrupt description structure for this irq
883  *
884  * Per CPU interrupts on SMP machines without locking requirements. Same as
885  * handle_percpu_irq() above but with the following extras:
886  *
887  * action->percpu_dev_id is a pointer to percpu variables which
888  * contain the real device id for the cpu on which this handler is
889  * called
890  */
891 void handle_percpu_devid_irq(struct irq_desc *desc)
892 {
893 	struct irq_chip *chip = irq_desc_get_chip(desc);
894 	struct irqaction *action = desc->action;
895 	unsigned int irq = irq_desc_get_irq(desc);
896 	irqreturn_t res;
897 
898 	/*
899 	 * PER CPU interrupts are not serialized. Do not touch
900 	 * desc->tot_count.
901 	 */
902 	__kstat_incr_irqs_this_cpu(desc);
903 
904 	if (chip->irq_ack)
905 		chip->irq_ack(&desc->irq_data);
906 
907 	if (likely(action)) {
908 		trace_irq_handler_entry(irq, action);
909 		res = action->handler(irq, raw_cpu_ptr(action->percpu_dev_id));
910 		trace_irq_handler_exit(irq, action, res);
911 	} else {
912 		unsigned int cpu = smp_processor_id();
913 		bool enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
914 
915 		if (enabled)
916 			irq_percpu_disable(desc, cpu);
917 
918 		pr_err_once("Spurious%s percpu IRQ%u on CPU%u\n",
919 			    enabled ? " and unmasked" : "", irq, cpu);
920 	}
921 
922 	if (chip->irq_eoi)
923 		chip->irq_eoi(&desc->irq_data);
924 }
925 
926 /**
927  * handle_percpu_devid_fasteoi_nmi - Per CPU local NMI handler with per cpu
928  *				     dev ids
929  * @desc:	the interrupt description structure for this irq
930  *
931  * Similar to handle_fasteoi_nmi, but handling the dev_id cookie
932  * as a percpu pointer.
933  */
934 void handle_percpu_devid_fasteoi_nmi(struct irq_desc *desc)
935 {
936 	struct irq_chip *chip = irq_desc_get_chip(desc);
937 	struct irqaction *action = desc->action;
938 	unsigned int irq = irq_desc_get_irq(desc);
939 	irqreturn_t res;
940 
941 	__kstat_incr_irqs_this_cpu(desc);
942 
943 	trace_irq_handler_entry(irq, action);
944 	res = action->handler(irq, raw_cpu_ptr(action->percpu_dev_id));
945 	trace_irq_handler_exit(irq, action, res);
946 
947 	if (chip->irq_eoi)
948 		chip->irq_eoi(&desc->irq_data);
949 }
950 
951 static void
952 __irq_do_set_handler(struct irq_desc *desc, irq_flow_handler_t handle,
953 		     int is_chained, const char *name)
954 {
955 	if (!handle) {
956 		handle = handle_bad_irq;
957 	} else {
958 		struct irq_data *irq_data = &desc->irq_data;
959 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
960 		/*
961 		 * With hierarchical domains we might run into a
962 		 * situation where the outermost chip is not yet set
963 		 * up, but the inner chips are there.  Instead of
964 		 * bailing we install the handler, but obviously we
965 		 * cannot enable/startup the interrupt at this point.
966 		 */
967 		while (irq_data) {
968 			if (irq_data->chip != &no_irq_chip)
969 				break;
970 			/*
971 			 * Bail out if the outer chip is not set up
972 			 * and the interrupt supposed to be started
973 			 * right away.
974 			 */
975 			if (WARN_ON(is_chained))
976 				return;
977 			/* Try the parent */
978 			irq_data = irq_data->parent_data;
979 		}
980 #endif
981 		if (WARN_ON(!irq_data || irq_data->chip == &no_irq_chip))
982 			return;
983 	}
984 
985 	/* Uninstall? */
986 	if (handle == handle_bad_irq) {
987 		if (desc->irq_data.chip != &no_irq_chip)
988 			mask_ack_irq(desc);
989 		irq_state_set_disabled(desc);
990 		if (is_chained) {
991 			desc->action = NULL;
992 			WARN_ON(irq_chip_pm_put(irq_desc_get_irq_data(desc)));
993 		}
994 		desc->depth = 1;
995 	}
996 	desc->handle_irq = handle;
997 	desc->name = name;
998 
999 	if (handle != handle_bad_irq && is_chained) {
1000 		unsigned int type = irqd_get_trigger_type(&desc->irq_data);
1001 
1002 		/*
1003 		 * We're about to start this interrupt immediately,
1004 		 * hence the need to set the trigger configuration.
1005 		 * But the .set_type callback may have overridden the
1006 		 * flow handler, ignoring that we're dealing with a
1007 		 * chained interrupt. Reset it immediately because we
1008 		 * do know better.
1009 		 */
1010 		if (type != IRQ_TYPE_NONE) {
1011 			__irq_set_trigger(desc, type);
1012 			desc->handle_irq = handle;
1013 		}
1014 
1015 		irq_settings_set_noprobe(desc);
1016 		irq_settings_set_norequest(desc);
1017 		irq_settings_set_nothread(desc);
1018 		desc->action = &chained_action;
1019 		WARN_ON(irq_chip_pm_get(irq_desc_get_irq_data(desc)));
1020 		irq_activate_and_startup(desc, IRQ_RESEND);
1021 	}
1022 }
1023 
1024 void __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
1025 		       const char *name)
1026 {
1027 	scoped_irqdesc_get_and_lock(irq, 0)
1028 		__irq_do_set_handler(scoped_irqdesc, handle, is_chained, name);
1029 }
1030 EXPORT_SYMBOL_GPL(__irq_set_handler);
1031 
1032 void irq_set_chained_handler_and_data(unsigned int irq, irq_flow_handler_t handle,
1033 				      void *data)
1034 {
1035 	scoped_irqdesc_get_and_buslock(irq, 0) {
1036 		struct irq_desc *desc = scoped_irqdesc;
1037 
1038 		desc->irq_common_data.handler_data = data;
1039 		__irq_do_set_handler(desc, handle, 1, NULL);
1040 	}
1041 }
1042 EXPORT_SYMBOL_GPL(irq_set_chained_handler_and_data);
1043 
1044 void
1045 irq_set_chip_and_handler_name(unsigned int irq, const struct irq_chip *chip,
1046 			      irq_flow_handler_t handle, const char *name)
1047 {
1048 	irq_set_chip(irq, chip);
1049 	__irq_set_handler(irq, handle, 0, name);
1050 }
1051 EXPORT_SYMBOL_GPL(irq_set_chip_and_handler_name);
1052 
1053 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
1054 {
1055 	scoped_irqdesc_get_and_lock(irq, 0) {
1056 		struct irq_desc *desc = scoped_irqdesc;
1057 		unsigned long trigger, tmp;
1058 		/*
1059 		 * Warn when a driver sets the no autoenable flag on an already
1060 		 * active interrupt.
1061 		 */
1062 		WARN_ON_ONCE(!desc->depth && (set & _IRQ_NOAUTOEN));
1063 
1064 		irq_settings_clr_and_set(desc, clr, set);
1065 
1066 		trigger = irqd_get_trigger_type(&desc->irq_data);
1067 
1068 		irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
1069 			   IRQD_TRIGGER_MASK | IRQD_LEVEL);
1070 		if (irq_settings_has_no_balance_set(desc))
1071 			irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1072 		if (irq_settings_is_per_cpu(desc))
1073 			irqd_set(&desc->irq_data, IRQD_PER_CPU);
1074 		if (irq_settings_is_level(desc))
1075 			irqd_set(&desc->irq_data, IRQD_LEVEL);
1076 
1077 		tmp = irq_settings_get_trigger_mask(desc);
1078 		if (tmp != IRQ_TYPE_NONE)
1079 			trigger = tmp;
1080 
1081 		irqd_set(&desc->irq_data, trigger);
1082 	}
1083 }
1084 EXPORT_SYMBOL_GPL(irq_modify_status);
1085 
1086 #ifdef CONFIG_DEPRECATED_IRQ_CPU_ONOFFLINE
1087 /**
1088  *	irq_cpu_online - Invoke all irq_cpu_online functions.
1089  *
1090  *	Iterate through all irqs and invoke the chip.irq_cpu_online()
1091  *	for each.
1092  */
1093 void irq_cpu_online(void)
1094 {
1095 	unsigned int irq;
1096 
1097 	for_each_active_irq(irq) {
1098 		struct irq_desc *desc = irq_to_desc(irq);
1099 		struct irq_chip *chip;
1100 
1101 		if (!desc)
1102 			continue;
1103 
1104 		guard(raw_spinlock_irqsave)(&desc->lock);
1105 		chip = irq_data_get_irq_chip(&desc->irq_data);
1106 		if (chip && chip->irq_cpu_online &&
1107 		    (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
1108 		     !irqd_irq_disabled(&desc->irq_data)))
1109 			chip->irq_cpu_online(&desc->irq_data);
1110 	}
1111 }
1112 
1113 /**
1114  *	irq_cpu_offline - Invoke all irq_cpu_offline functions.
1115  *
1116  *	Iterate through all irqs and invoke the chip.irq_cpu_offline()
1117  *	for each.
1118  */
1119 void irq_cpu_offline(void)
1120 {
1121 	unsigned int irq;
1122 
1123 	for_each_active_irq(irq) {
1124 		struct irq_desc *desc = irq_to_desc(irq);
1125 		struct irq_chip *chip;
1126 
1127 		if (!desc)
1128 			continue;
1129 
1130 		guard(raw_spinlock_irqsave)(&desc->lock);
1131 		chip = irq_data_get_irq_chip(&desc->irq_data);
1132 		if (chip && chip->irq_cpu_offline &&
1133 		    (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
1134 		     !irqd_irq_disabled(&desc->irq_data)))
1135 			chip->irq_cpu_offline(&desc->irq_data);
1136 	}
1137 }
1138 #endif
1139 
1140 #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
1141 
1142 #ifdef CONFIG_IRQ_FASTEOI_HIERARCHY_HANDLERS
1143 /**
1144  * handle_fasteoi_ack_irq - irq handler for edge hierarchy stacked on
1145  *			    transparent controllers
1146  *
1147  * @desc:	the interrupt description structure for this irq
1148  *
1149  * Like handle_fasteoi_irq(), but for use with hierarchy where the irq_chip
1150  * also needs to have its ->irq_ack() function called.
1151  */
1152 void handle_fasteoi_ack_irq(struct irq_desc *desc)
1153 {
1154 	struct irq_chip *chip = desc->irq_data.chip;
1155 
1156 	guard(raw_spinlock)(&desc->lock);
1157 
1158 	if (!irq_can_handle_pm(desc)) {
1159 		cond_eoi_irq(chip, &desc->irq_data);
1160 		return;
1161 	}
1162 
1163 	if (unlikely(!irq_can_handle_actions(desc))) {
1164 		mask_irq(desc);
1165 		cond_eoi_irq(chip, &desc->irq_data);
1166 		return;
1167 	}
1168 
1169 	kstat_incr_irqs_this_cpu(desc);
1170 	if (desc->istate & IRQS_ONESHOT)
1171 		mask_irq(desc);
1172 
1173 	desc->irq_data.chip->irq_ack(&desc->irq_data);
1174 
1175 	handle_irq_event(desc);
1176 
1177 	cond_unmask_eoi_irq(desc, chip);
1178 }
1179 EXPORT_SYMBOL_GPL(handle_fasteoi_ack_irq);
1180 
1181 /**
1182  * handle_fasteoi_mask_irq - irq handler for level hierarchy stacked on
1183  *			     transparent controllers
1184  *
1185  * @desc:	the interrupt description structure for this irq
1186  *
1187  * Like handle_fasteoi_irq(), but for use with hierarchy where the irq_chip
1188  * also needs to have its ->irq_mask_ack() function called.
1189  */
1190 void handle_fasteoi_mask_irq(struct irq_desc *desc)
1191 {
1192 	struct irq_chip *chip = desc->irq_data.chip;
1193 
1194 	guard(raw_spinlock)(&desc->lock);
1195 	mask_ack_irq(desc);
1196 
1197 	if (!irq_can_handle(desc)) {
1198 		cond_eoi_irq(chip, &desc->irq_data);
1199 		return;
1200 	}
1201 
1202 	kstat_incr_irqs_this_cpu(desc);
1203 
1204 	handle_irq_event(desc);
1205 
1206 	cond_unmask_eoi_irq(desc, chip);
1207 }
1208 EXPORT_SYMBOL_GPL(handle_fasteoi_mask_irq);
1209 
1210 #endif /* CONFIG_IRQ_FASTEOI_HIERARCHY_HANDLERS */
1211 
1212 /**
1213  * irq_chip_set_parent_state - set the state of a parent interrupt.
1214  *
1215  * @data: Pointer to interrupt specific data
1216  * @which: State to be restored (one of IRQCHIP_STATE_*)
1217  * @val: Value corresponding to @which
1218  *
1219  * Conditional success, if the underlying irqchip does not implement it.
1220  */
1221 int irq_chip_set_parent_state(struct irq_data *data,
1222 			      enum irqchip_irq_state which,
1223 			      bool val)
1224 {
1225 	data = data->parent_data;
1226 
1227 	if (!data || !data->chip->irq_set_irqchip_state)
1228 		return 0;
1229 
1230 	return data->chip->irq_set_irqchip_state(data, which, val);
1231 }
1232 EXPORT_SYMBOL_GPL(irq_chip_set_parent_state);
1233 
1234 /**
1235  * irq_chip_get_parent_state - get the state of a parent interrupt.
1236  *
1237  * @data: Pointer to interrupt specific data
1238  * @which: one of IRQCHIP_STATE_* the caller wants to know
1239  * @state: a pointer to a boolean where the state is to be stored
1240  *
1241  * Conditional success, if the underlying irqchip does not implement it.
1242  */
1243 int irq_chip_get_parent_state(struct irq_data *data,
1244 			      enum irqchip_irq_state which,
1245 			      bool *state)
1246 {
1247 	data = data->parent_data;
1248 
1249 	if (!data || !data->chip->irq_get_irqchip_state)
1250 		return 0;
1251 
1252 	return data->chip->irq_get_irqchip_state(data, which, state);
1253 }
1254 EXPORT_SYMBOL_GPL(irq_chip_get_parent_state);
1255 
1256 /**
1257  * irq_chip_enable_parent - Enable the parent interrupt (defaults to unmask if
1258  * NULL)
1259  * @data:	Pointer to interrupt specific data
1260  */
1261 void irq_chip_enable_parent(struct irq_data *data)
1262 {
1263 	data = data->parent_data;
1264 	if (data->chip->irq_enable)
1265 		data->chip->irq_enable(data);
1266 	else
1267 		data->chip->irq_unmask(data);
1268 }
1269 EXPORT_SYMBOL_GPL(irq_chip_enable_parent);
1270 
1271 /**
1272  * irq_chip_disable_parent - Disable the parent interrupt (defaults to mask if
1273  * NULL)
1274  * @data:	Pointer to interrupt specific data
1275  */
1276 void irq_chip_disable_parent(struct irq_data *data)
1277 {
1278 	data = data->parent_data;
1279 	if (data->chip->irq_disable)
1280 		data->chip->irq_disable(data);
1281 	else
1282 		data->chip->irq_mask(data);
1283 }
1284 EXPORT_SYMBOL_GPL(irq_chip_disable_parent);
1285 
1286 /**
1287  * irq_chip_ack_parent - Acknowledge the parent interrupt
1288  * @data:	Pointer to interrupt specific data
1289  */
1290 void irq_chip_ack_parent(struct irq_data *data)
1291 {
1292 	data = data->parent_data;
1293 	data->chip->irq_ack(data);
1294 }
1295 EXPORT_SYMBOL_GPL(irq_chip_ack_parent);
1296 
1297 /**
1298  * irq_chip_mask_parent - Mask the parent interrupt
1299  * @data:	Pointer to interrupt specific data
1300  */
1301 void irq_chip_mask_parent(struct irq_data *data)
1302 {
1303 	data = data->parent_data;
1304 	data->chip->irq_mask(data);
1305 }
1306 EXPORT_SYMBOL_GPL(irq_chip_mask_parent);
1307 
1308 /**
1309  * irq_chip_mask_ack_parent - Mask and acknowledge the parent interrupt
1310  * @data:	Pointer to interrupt specific data
1311  */
1312 void irq_chip_mask_ack_parent(struct irq_data *data)
1313 {
1314 	data = data->parent_data;
1315 	data->chip->irq_mask_ack(data);
1316 }
1317 EXPORT_SYMBOL_GPL(irq_chip_mask_ack_parent);
1318 
1319 /**
1320  * irq_chip_unmask_parent - Unmask the parent interrupt
1321  * @data:	Pointer to interrupt specific data
1322  */
1323 void irq_chip_unmask_parent(struct irq_data *data)
1324 {
1325 	data = data->parent_data;
1326 	data->chip->irq_unmask(data);
1327 }
1328 EXPORT_SYMBOL_GPL(irq_chip_unmask_parent);
1329 
1330 /**
1331  * irq_chip_eoi_parent - Invoke EOI on the parent interrupt
1332  * @data:	Pointer to interrupt specific data
1333  */
1334 void irq_chip_eoi_parent(struct irq_data *data)
1335 {
1336 	data = data->parent_data;
1337 	data->chip->irq_eoi(data);
1338 }
1339 EXPORT_SYMBOL_GPL(irq_chip_eoi_parent);
1340 
1341 /**
1342  * irq_chip_set_affinity_parent - Set affinity on the parent interrupt
1343  * @data:	Pointer to interrupt specific data
1344  * @dest:	The affinity mask to set
1345  * @force:	Flag to enforce setting (disable online checks)
1346  *
1347  * Conditional, as the underlying parent chip might not implement it.
1348  */
1349 int irq_chip_set_affinity_parent(struct irq_data *data,
1350 				 const struct cpumask *dest, bool force)
1351 {
1352 	data = data->parent_data;
1353 	if (data->chip->irq_set_affinity)
1354 		return data->chip->irq_set_affinity(data, dest, force);
1355 
1356 	return -ENOSYS;
1357 }
1358 EXPORT_SYMBOL_GPL(irq_chip_set_affinity_parent);
1359 
1360 /**
1361  * irq_chip_set_type_parent - Set IRQ type on the parent interrupt
1362  * @data:	Pointer to interrupt specific data
1363  * @type:	IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
1364  *
1365  * Conditional, as the underlying parent chip might not implement it.
1366  */
1367 int irq_chip_set_type_parent(struct irq_data *data, unsigned int type)
1368 {
1369 	data = data->parent_data;
1370 
1371 	if (data->chip->irq_set_type)
1372 		return data->chip->irq_set_type(data, type);
1373 
1374 	return -ENOSYS;
1375 }
1376 EXPORT_SYMBOL_GPL(irq_chip_set_type_parent);
1377 
1378 /**
1379  * irq_chip_retrigger_hierarchy - Retrigger an interrupt in hardware
1380  * @data:	Pointer to interrupt specific data
1381  *
1382  * Iterate through the domain hierarchy of the interrupt and check
1383  * whether a hw retrigger function exists. If yes, invoke it.
1384  */
1385 int irq_chip_retrigger_hierarchy(struct irq_data *data)
1386 {
1387 	for (data = data->parent_data; data; data = data->parent_data)
1388 		if (data->chip && data->chip->irq_retrigger)
1389 			return data->chip->irq_retrigger(data);
1390 
1391 	return 0;
1392 }
1393 EXPORT_SYMBOL_GPL(irq_chip_retrigger_hierarchy);
1394 
1395 /**
1396  * irq_chip_set_vcpu_affinity_parent - Set vcpu affinity on the parent interrupt
1397  * @data:	Pointer to interrupt specific data
1398  * @vcpu_info:	The vcpu affinity information
1399  */
1400 int irq_chip_set_vcpu_affinity_parent(struct irq_data *data, void *vcpu_info)
1401 {
1402 	data = data->parent_data;
1403 	if (data->chip->irq_set_vcpu_affinity)
1404 		return data->chip->irq_set_vcpu_affinity(data, vcpu_info);
1405 
1406 	return -ENOSYS;
1407 }
1408 EXPORT_SYMBOL_GPL(irq_chip_set_vcpu_affinity_parent);
1409 /**
1410  * irq_chip_set_wake_parent - Set/reset wake-up on the parent interrupt
1411  * @data:	Pointer to interrupt specific data
1412  * @on:		Whether to set or reset the wake-up capability of this irq
1413  *
1414  * Conditional, as the underlying parent chip might not implement it.
1415  */
1416 int irq_chip_set_wake_parent(struct irq_data *data, unsigned int on)
1417 {
1418 	data = data->parent_data;
1419 
1420 	if (data->chip->flags & IRQCHIP_SKIP_SET_WAKE)
1421 		return 0;
1422 
1423 	if (data->chip->irq_set_wake)
1424 		return data->chip->irq_set_wake(data, on);
1425 
1426 	return -ENOSYS;
1427 }
1428 EXPORT_SYMBOL_GPL(irq_chip_set_wake_parent);
1429 
1430 /**
1431  * irq_chip_request_resources_parent - Request resources on the parent interrupt
1432  * @data:	Pointer to interrupt specific data
1433  */
1434 int irq_chip_request_resources_parent(struct irq_data *data)
1435 {
1436 	data = data->parent_data;
1437 
1438 	if (data->chip->irq_request_resources)
1439 		return data->chip->irq_request_resources(data);
1440 
1441 	/* no error on missing optional irq_chip::irq_request_resources */
1442 	return 0;
1443 }
1444 EXPORT_SYMBOL_GPL(irq_chip_request_resources_parent);
1445 
1446 /**
1447  * irq_chip_release_resources_parent - Release resources on the parent interrupt
1448  * @data:	Pointer to interrupt specific data
1449  */
1450 void irq_chip_release_resources_parent(struct irq_data *data)
1451 {
1452 	data = data->parent_data;
1453 	if (data->chip->irq_release_resources)
1454 		data->chip->irq_release_resources(data);
1455 }
1456 EXPORT_SYMBOL_GPL(irq_chip_release_resources_parent);
1457 #endif
1458 
1459 /**
1460  * irq_chip_compose_msi_msg - Compose msi message for a irq chip
1461  * @data:	Pointer to interrupt specific data
1462  * @msg:	Pointer to the MSI message
1463  *
1464  * For hierarchical domains we find the first chip in the hierarchy
1465  * which implements the irq_compose_msi_msg callback. For non
1466  * hierarchical we use the top level chip.
1467  */
1468 int irq_chip_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1469 {
1470 	struct irq_data *pos;
1471 
1472 	for (pos = NULL; !pos && data; data = irqd_get_parent_data(data)) {
1473 		if (data->chip && data->chip->irq_compose_msi_msg)
1474 			pos = data;
1475 	}
1476 
1477 	if (!pos)
1478 		return -ENOSYS;
1479 
1480 	pos->chip->irq_compose_msi_msg(pos, msg);
1481 	return 0;
1482 }
1483 
1484 static struct device *irq_get_pm_device(struct irq_data *data)
1485 {
1486 	if (data->domain)
1487 		return data->domain->pm_dev;
1488 
1489 	return NULL;
1490 }
1491 
1492 /**
1493  * irq_chip_pm_get - Enable power for an IRQ chip
1494  * @data:	Pointer to interrupt specific data
1495  *
1496  * Enable the power to the IRQ chip referenced by the interrupt data
1497  * structure.
1498  */
1499 int irq_chip_pm_get(struct irq_data *data)
1500 {
1501 	struct device *dev = irq_get_pm_device(data);
1502 	int retval = 0;
1503 
1504 	if (IS_ENABLED(CONFIG_PM) && dev)
1505 		retval = pm_runtime_resume_and_get(dev);
1506 
1507 	return retval;
1508 }
1509 
1510 /**
1511  * irq_chip_pm_put - Disable power for an IRQ chip
1512  * @data:	Pointer to interrupt specific data
1513  *
1514  * Disable the power to the IRQ chip referenced by the interrupt data
1515  * structure, belongs. Note that power will only be disabled, once this
1516  * function has been called for all IRQs that have called irq_chip_pm_get().
1517  */
1518 int irq_chip_pm_put(struct irq_data *data)
1519 {
1520 	struct device *dev = irq_get_pm_device(data);
1521 	int retval = 0;
1522 
1523 	if (IS_ENABLED(CONFIG_PM) && dev)
1524 		retval = pm_runtime_put(dev);
1525 
1526 	return (retval < 0) ? retval : 0;
1527 }
1528