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