xref: /linux/kernel/irq/irqdesc.c (revision 7603e0575d8a92318bd4695917fce7ec2c5825a1)
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 interrupt descriptor management code. Detailed
7  * information is available in Documentation/core-api/genericirq.rst
8  *
9  */
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/maple_tree.h>
16 #include <linux/irqdomain.h>
17 #include <linux/sysfs.h>
18 #include <linux/string_choices.h>
19 
20 #include "internals.h"
21 
22 /*
23  * lockdep: we want to handle all irq_desc locks as a single lock-class:
24  */
25 static struct lock_class_key irq_desc_lock_class;
26 
27 #if defined(CONFIG_SMP)
28 static int __init irq_affinity_setup(char *str)
29 {
30 	alloc_bootmem_cpumask_var(&irq_default_affinity);
31 	cpulist_parse(str, irq_default_affinity);
32 	/*
33 	 * Set at least the boot cpu. We don't want to end up with
34 	 * bugreports caused by random commandline masks
35 	 */
36 	cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
37 	return 1;
38 }
39 __setup("irqaffinity=", irq_affinity_setup);
40 
41 static void __init init_irq_default_affinity(void)
42 {
43 	if (!cpumask_available(irq_default_affinity))
44 		zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
45 	if (cpumask_empty(irq_default_affinity))
46 		cpumask_setall(irq_default_affinity);
47 }
48 #else
49 static void __init init_irq_default_affinity(void)
50 {
51 }
52 #endif
53 
54 #ifdef CONFIG_SMP
55 static int alloc_masks(struct irq_desc *desc, int node)
56 {
57 	if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
58 				     GFP_KERNEL, node))
59 		return -ENOMEM;
60 
61 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
62 	if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity,
63 				     GFP_KERNEL, node)) {
64 		free_cpumask_var(desc->irq_common_data.affinity);
65 		return -ENOMEM;
66 	}
67 #endif
68 
69 #ifdef CONFIG_GENERIC_PENDING_IRQ
70 	if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) {
71 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
72 		free_cpumask_var(desc->irq_common_data.effective_affinity);
73 #endif
74 		free_cpumask_var(desc->irq_common_data.affinity);
75 		return -ENOMEM;
76 	}
77 #endif
78 	return 0;
79 }
80 
81 static void irq_redirect_work(struct irq_work *work)
82 {
83 	handle_irq_desc(container_of(work, struct irq_desc, redirect.work));
84 }
85 
86 static void desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity)
87 {
88 	if (!affinity)
89 		affinity = irq_default_affinity;
90 	cpumask_copy(desc->irq_common_data.affinity, affinity);
91 
92 #ifdef CONFIG_GENERIC_PENDING_IRQ
93 	cpumask_clear(desc->pending_mask);
94 #endif
95 #ifdef CONFIG_NUMA
96 	desc->irq_common_data.node = node;
97 #endif
98 	desc->redirect.work = IRQ_WORK_INIT_HARD(irq_redirect_work);
99 }
100 
101 static void free_masks(struct irq_desc *desc)
102 {
103 #ifdef CONFIG_GENERIC_PENDING_IRQ
104 	free_cpumask_var(desc->pending_mask);
105 #endif
106 	free_cpumask_var(desc->irq_common_data.affinity);
107 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
108 	free_cpumask_var(desc->irq_common_data.effective_affinity);
109 #endif
110 }
111 
112 #else
113 static inline int
114 alloc_masks(struct irq_desc *desc, int node) { return 0; }
115 static inline void
116 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
117 static inline void free_masks(struct irq_desc *desc) { }
118 #endif
119 
120 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
121 			      const struct cpumask *affinity, struct module *owner)
122 {
123 	desc->irq_common_data.handler_data = NULL;
124 	desc->irq_common_data.msi_desc = NULL;
125 
126 	desc->irq_data.common = &desc->irq_common_data;
127 	desc->irq_data.irq = irq;
128 	desc->irq_data.chip = &no_irq_chip;
129 	desc->irq_data.chip_data = NULL;
130 	irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
131 	irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
132 	irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
133 	desc->handle_irq = handle_bad_irq;
134 	desc->depth = 1;
135 	desc->irq_count = 0;
136 	desc->irqs_unhandled = 0;
137 	desc->tot_count = 0;
138 	desc->name = NULL;
139 	desc->owner = owner;
140 	rcuref_init(&desc->refcnt, 1);
141 	desc_smp_init(desc, node, affinity);
142 }
143 
144 unsigned int total_nr_irqs __read_mostly = NR_IRQS;
145 
146 /**
147  * irq_get_nr_irqs() - Number of interrupts supported by the system.
148  */
149 unsigned int irq_get_nr_irqs(void)
150 {
151 	return total_nr_irqs;
152 }
153 EXPORT_SYMBOL_GPL(irq_get_nr_irqs);
154 
155 /**
156  * irq_set_nr_irqs() - Set the number of interrupts supported by the system.
157  * @nr: New number of interrupts.
158  *
159  * Return: @nr.
160  */
161 unsigned int __init irq_set_nr_irqs(unsigned int nr)
162 {
163 	total_nr_irqs = nr;
164 	irq_proc_calc_prec();
165 	return nr;
166 }
167 
168 static DEFINE_MUTEX(sparse_irq_lock);
169 static struct maple_tree sparse_irqs = MTREE_INIT_EXT(sparse_irqs,
170 					MT_FLAGS_ALLOC_RANGE |
171 					MT_FLAGS_LOCK_EXTERN |
172 					MT_FLAGS_USE_RCU,
173 					sparse_irq_lock);
174 
175 static int irq_find_free_area(unsigned int from, unsigned int cnt)
176 {
177 	MA_STATE(mas, &sparse_irqs, 0, 0);
178 
179 	if (mas_empty_area(&mas, from, MAX_SPARSE_IRQS, cnt))
180 		return -ENOSPC;
181 	return mas.index;
182 }
183 
184 struct irq_desc *irq_find_desc_at_or_after(unsigned int offset)
185 {
186 	unsigned long index = offset;
187 
188 	lockdep_assert_in_rcu_read_lock();
189 	return mt_find(&sparse_irqs, &index, total_nr_irqs);
190 }
191 
192 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
193 {
194 	MA_STATE(mas, &sparse_irqs, irq, irq);
195 	WARN_ON(mas_store_gfp(&mas, desc, GFP_KERNEL) != 0);
196 }
197 
198 static void delete_irq_desc(unsigned int irq)
199 {
200 	MA_STATE(mas, &sparse_irqs, irq, irq);
201 	mas_erase(&mas);
202 }
203 
204 #ifdef CONFIG_SPARSE_IRQ
205 static const struct kobj_type irq_kobj_type;
206 #endif
207 
208 static int init_desc(struct irq_desc *desc, int irq, int node,
209 		     unsigned int flags,
210 		     const struct cpumask *affinity,
211 		     struct module *owner)
212 {
213 	desc->kstat_irqs = alloc_percpu(struct irqstat);
214 	if (!desc->kstat_irqs)
215 		return -ENOMEM;
216 
217 	if (alloc_masks(desc, node)) {
218 		free_percpu(desc->kstat_irqs);
219 		return -ENOMEM;
220 	}
221 
222 	raw_spin_lock_init(&desc->lock);
223 	lockdep_set_class(&desc->lock, &irq_desc_lock_class);
224 	mutex_init(&desc->request_mutex);
225 	init_waitqueue_head(&desc->wait_for_threads);
226 	desc_set_defaults(irq, desc, node, affinity, owner);
227 	irqd_set(&desc->irq_data, flags);
228 	irq_resend_init(desc);
229 #ifdef CONFIG_SPARSE_IRQ
230 	kobject_init(&desc->kobj, &irq_kobj_type);
231 	init_rcu_head(&desc->rcu);
232 #endif
233 
234 	return 0;
235 }
236 
237 #ifdef CONFIG_SPARSE_IRQ
238 
239 static void irq_kobj_release(struct kobject *kobj);
240 
241 #ifdef CONFIG_SYSFS
242 static struct kobject *irq_kobj_base;
243 
244 #define IRQ_ATTR_RO(_name) \
245 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
246 
247 static ssize_t per_cpu_count_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
248 {
249 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
250 	ssize_t ret = 0;
251 	char *p = "";
252 	int cpu;
253 
254 	for_each_possible_cpu(cpu) {
255 		unsigned int c = irq_desc_kstat_cpu(desc, cpu);
256 
257 		ret += sysfs_emit_at(buf, ret, "%s%u", p, c);
258 		p = ",";
259 	}
260 
261 	ret += sysfs_emit_at(buf, ret, "\n");
262 	return ret;
263 }
264 IRQ_ATTR_RO(per_cpu_count);
265 
266 static ssize_t chip_name_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
267 {
268 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
269 
270 	guard(raw_spinlock_irq)(&desc->lock);
271 	if (desc->irq_data.chip && desc->irq_data.chip->name)
272 		return sysfs_emit(buf, "%s\n", desc->irq_data.chip->name);
273 	return 0;
274 }
275 IRQ_ATTR_RO(chip_name);
276 
277 static ssize_t hwirq_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
278 {
279 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
280 
281 	guard(raw_spinlock_irq)(&desc->lock);
282 	if (desc->irq_data.domain)
283 		return sysfs_emit(buf, "%lu\n", desc->irq_data.hwirq);
284 	return 0;
285 }
286 IRQ_ATTR_RO(hwirq);
287 
288 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
289 {
290 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
291 
292 	guard(raw_spinlock_irq)(&desc->lock);
293 	return sysfs_emit(buf, "%s\n", irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
294 
295 }
296 IRQ_ATTR_RO(type);
297 
298 static ssize_t wakeup_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
299 {
300 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
301 
302 	guard(raw_spinlock_irq)(&desc->lock);
303 	return sysfs_emit(buf, "%s\n", str_enabled_disabled(irqd_is_wakeup_set(&desc->irq_data)));
304 }
305 IRQ_ATTR_RO(wakeup);
306 
307 static ssize_t name_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
308 {
309 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
310 
311 	guard(raw_spinlock_irq)(&desc->lock);
312 	if (desc->name)
313 		return sysfs_emit(buf, "%s\n", desc->name);
314 	return 0;
315 }
316 IRQ_ATTR_RO(name);
317 
318 static ssize_t actions_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
319 {
320 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
321 	struct irqaction *action;
322 	ssize_t ret = 0;
323 	char *p = "";
324 
325 	scoped_guard(raw_spinlock_irq, &desc->lock) {
326 		for_each_action_of_desc(desc, action) {
327 			ret += sysfs_emit_at(buf, ret, "%s%s", p, action->name);
328 			p = ",";
329 		}
330 	}
331 
332 	if (ret)
333 		ret += sysfs_emit_at(buf, ret, "\n");
334 	return ret;
335 }
336 IRQ_ATTR_RO(actions);
337 
338 static struct attribute *irq_attrs[] = {
339 	&per_cpu_count_attr.attr,
340 	&chip_name_attr.attr,
341 	&hwirq_attr.attr,
342 	&type_attr.attr,
343 	&wakeup_attr.attr,
344 	&name_attr.attr,
345 	&actions_attr.attr,
346 	NULL
347 };
348 ATTRIBUTE_GROUPS(irq);
349 
350 static const struct kobj_type irq_kobj_type = {
351 	.release	= irq_kobj_release,
352 	.sysfs_ops	= &kobj_sysfs_ops,
353 	.default_groups = irq_groups,
354 };
355 
356 static void irq_sysfs_add(int irq, struct irq_desc *desc)
357 {
358 	if (irq_kobj_base) {
359 		/*
360 		 * Continue even in case of failure as this is nothing
361 		 * crucial and failures in the late irq_sysfs_init()
362 		 * cannot be rolled back.
363 		 */
364 		if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
365 			pr_warn("Failed to add kobject for irq %d\n", irq);
366 		else
367 			desc->istate |= IRQS_SYSFS;
368 	}
369 }
370 
371 static void irq_sysfs_del(struct irq_desc *desc)
372 {
373 	/*
374 	 * Only invoke kobject_del() when kobject_add() was successfully
375 	 * invoked for the descriptor. This covers both early boot, where
376 	 * sysfs is not initialized yet, and the case of a failed
377 	 * kobject_add() invocation.
378 	 */
379 	if (desc->istate & IRQS_SYSFS)
380 		kobject_del(&desc->kobj);
381 }
382 
383 static int __init irq_sysfs_init(void)
384 {
385 	struct irq_desc *desc;
386 	int irq;
387 
388 	/* Prevent concurrent irq alloc/free */
389 	guard(mutex)(&sparse_irq_lock);
390 	irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
391 	if (!irq_kobj_base)
392 		return -ENOMEM;
393 
394 	/* Add the already allocated interrupts */
395 	for_each_irq_desc(irq, desc)
396 		irq_sysfs_add(irq, desc);
397 	return 0;
398 }
399 postcore_initcall(irq_sysfs_init);
400 
401 #else /* !CONFIG_SYSFS */
402 
403 static const struct kobj_type irq_kobj_type = {
404 	.release	= irq_kobj_release,
405 };
406 
407 static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
408 static void irq_sysfs_del(struct irq_desc *desc) {}
409 
410 #endif /* CONFIG_SYSFS */
411 
412 struct irq_desc *irq_to_desc(unsigned int irq)
413 {
414 	return mtree_load(&sparse_irqs, irq);
415 }
416 #ifdef CONFIG_KVM_BOOK3S_64_HV_MODULE
417 EXPORT_SYMBOL_GPL(irq_to_desc);
418 #endif
419 
420 void irq_lock_sparse(void)
421 {
422 	mutex_lock(&sparse_irq_lock);
423 }
424 
425 void irq_unlock_sparse(void)
426 {
427 	mutex_unlock(&sparse_irq_lock);
428 }
429 
430 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
431 				   const struct cpumask *affinity,
432 				   struct module *owner)
433 {
434 	struct irq_desc *desc;
435 	int ret;
436 
437 	desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
438 	if (!desc)
439 		return NULL;
440 
441 	ret = init_desc(desc, irq, node, flags, affinity, owner);
442 	if (unlikely(ret)) {
443 		kfree(desc);
444 		return NULL;
445 	}
446 
447 	return desc;
448 }
449 
450 static void irq_kobj_release(struct kobject *kobj)
451 {
452 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
453 
454 	free_masks(desc);
455 	free_percpu(desc->kstat_irqs);
456 	kfree(desc);
457 }
458 
459 static void delayed_free_desc(struct rcu_head *rhp)
460 {
461 	struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
462 
463 	kobject_put(&desc->kobj);
464 }
465 
466 void irq_desc_free_rcu(struct irq_desc *desc)
467 {
468 	/*
469 	 * We free the descriptor, masks and stat fields via RCU. That
470 	 * allows demultiplex interrupts to do rcu based management of
471 	 * the child interrupts.
472 	 * This also allows us to use rcu in kstat_irqs_usr().
473 	 */
474 	call_rcu(&desc->rcu, delayed_free_desc);
475 }
476 
477 static void free_desc(unsigned int irq)
478 {
479 	struct irq_desc *desc = irq_to_desc(irq);
480 
481 	irq_remove_debugfs_entry(desc);
482 	unregister_irq_proc(irq, desc);
483 
484 	/*
485 	 * sparse_irq_lock protects also show_interrupts() and
486 	 * kstat_irq_usr(). Once we deleted the descriptor from the
487 	 * sparse tree we can free it. Access in proc will fail to
488 	 * lookup the descriptor.
489 	 *
490 	 * The sysfs entry must be serialized against a concurrent
491 	 * irq_sysfs_init() as well.
492 	 */
493 	irq_sysfs_del(desc);
494 	delete_irq_desc(irq);
495 	irq_desc_put_ref(desc);
496 }
497 
498 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
499 		       const struct irq_affinity_desc *affinity,
500 		       struct module *owner)
501 {
502 	struct irq_desc *desc;
503 	int i;
504 
505 	/* Validate affinity mask(s) */
506 	if (affinity) {
507 		for (i = 0; i < cnt; i++) {
508 			if (cpumask_empty(&affinity[i].mask))
509 				return -EINVAL;
510 		}
511 	}
512 
513 	for (i = 0; i < cnt; i++) {
514 		const struct cpumask *mask = NULL;
515 		unsigned int flags = 0;
516 
517 		if (affinity) {
518 			if (affinity->is_managed) {
519 				flags = IRQD_AFFINITY_MANAGED |
520 					IRQD_MANAGED_SHUTDOWN;
521 			}
522 			flags |= IRQD_AFFINITY_SET;
523 			mask = &affinity->mask;
524 			node = cpu_to_node(cpumask_first(mask));
525 			affinity++;
526 		}
527 
528 		desc = alloc_desc(start + i, node, flags, mask, owner);
529 		if (!desc)
530 			goto err;
531 		irq_insert_desc(start + i, desc);
532 		irq_sysfs_add(start + i, desc);
533 		irq_add_debugfs_entry(start + i, desc);
534 	}
535 	return start;
536 
537 err:
538 	for (i--; i >= 0; i--)
539 		free_desc(start + i);
540 	return -ENOMEM;
541 }
542 
543 static bool irq_expand_nr_irqs(unsigned int nr)
544 {
545 	if (nr > MAX_SPARSE_IRQS)
546 		return false;
547 	total_nr_irqs = nr;
548 	irq_proc_calc_prec();
549 	return true;
550 }
551 
552 int __init early_irq_init(void)
553 {
554 	int i, initcnt, node = first_online_node;
555 	struct irq_desc *desc;
556 
557 	init_irq_default_affinity();
558 
559 	/* Let arch update nr_irqs and return the nr of preallocated irqs */
560 	initcnt = arch_probe_nr_irqs();
561 	printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
562 	       NR_IRQS, total_nr_irqs, initcnt);
563 
564 	if (WARN_ON(total_nr_irqs > MAX_SPARSE_IRQS))
565 		total_nr_irqs = MAX_SPARSE_IRQS;
566 
567 	if (WARN_ON(initcnt > MAX_SPARSE_IRQS))
568 		initcnt = MAX_SPARSE_IRQS;
569 
570 	if (initcnt > total_nr_irqs)
571 		total_nr_irqs = initcnt;
572 
573 	for (i = 0; i < initcnt; i++) {
574 		desc = alloc_desc(i, node, 0, NULL, NULL);
575 		irq_insert_desc(i, desc);
576 	}
577 	irq_proc_calc_prec();
578 	return arch_early_irq_init();
579 }
580 
581 #else /* !CONFIG_SPARSE_IRQ */
582 
583 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
584 	[0 ... NR_IRQS-1] = {
585 		.handle_irq	= handle_bad_irq,
586 		.depth		= 1,
587 		.lock		= __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
588 	}
589 };
590 
591 int __init early_irq_init(void)
592 {
593 	int count, i, node = first_online_node;
594 	int ret;
595 
596 	init_irq_default_affinity();
597 
598 	pr_info("NR_IRQS: %d\n", NR_IRQS);
599 
600 	count = ARRAY_SIZE(irq_desc);
601 
602 	for (i = 0; i < count; i++) {
603 		ret = init_desc(irq_desc + i, i, node, 0, NULL, NULL);
604 		if (unlikely(ret))
605 			goto __free_desc_res;
606 	}
607 
608 	irq_proc_calc_prec();
609 	return arch_early_irq_init();
610 
611 __free_desc_res:
612 	while (--i >= 0) {
613 		free_masks(irq_desc + i);
614 		free_percpu(irq_desc[i].kstat_irqs);
615 	}
616 
617 	return ret;
618 }
619 
620 struct irq_desc *irq_to_desc(unsigned int irq)
621 {
622 	return (irq < NR_IRQS) ? irq_desc + irq : NULL;
623 }
624 EXPORT_SYMBOL(irq_to_desc);
625 
626 static void free_desc(unsigned int irq)
627 {
628 	struct irq_desc *desc = irq_to_desc(irq);
629 	int cpu;
630 
631 	scoped_guard(raw_spinlock_irqsave, &desc->lock)
632 		desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
633 
634 	for_each_possible_cpu(cpu)
635 		*per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
636 
637 	delete_irq_desc(irq);
638 }
639 
640 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
641 			      const struct irq_affinity_desc *affinity,
642 			      struct module *owner)
643 {
644 	u32 i;
645 
646 	for (i = 0; i < cnt; i++) {
647 		struct irq_desc *desc = irq_to_desc(start + i);
648 
649 		desc->owner = owner;
650 		irq_insert_desc(start + i, desc);
651 	}
652 	return start;
653 }
654 
655 static inline bool irq_expand_nr_irqs(unsigned int nr)
656 {
657 	return false;
658 }
659 
660 void irq_mark_irq(unsigned int irq)
661 {
662 	guard(mutex)(&sparse_irq_lock);
663 	irq_insert_desc(irq, irq_desc + irq);
664 }
665 
666 #endif /* !CONFIG_SPARSE_IRQ */
667 
668 int handle_irq_desc(struct irq_desc *desc)
669 {
670 	struct irq_data *data;
671 
672 	if (!desc)
673 		return -EINVAL;
674 
675 	data = irq_desc_get_irq_data(desc);
676 	if (WARN_ON_ONCE(!in_hardirq() && irqd_is_handle_enforce_irqctx(data)))
677 		return -EPERM;
678 
679 	generic_handle_irq_desc(desc);
680 	return 0;
681 }
682 
683 /**
684  * generic_handle_irq - Invoke the handler for a particular irq
685  * @irq:	The irq number to handle
686  *
687  * Returns:	0 on success, or -EINVAL if conversion has failed
688  *
689  * 		This function must be called from an IRQ context with irq regs
690  * 		initialized.
691   */
692 int generic_handle_irq(unsigned int irq)
693 {
694 	return handle_irq_desc(irq_to_desc(irq));
695 }
696 EXPORT_SYMBOL_GPL(generic_handle_irq);
697 
698 /**
699  * generic_handle_irq_safe - Invoke the handler for a particular irq from any
700  *			     context.
701  * @irq:	The irq number to handle
702  *
703  * Returns:	0 on success, a negative value on error.
704  *
705  * This function can be called from any context (IRQ or process context). It
706  * will report an error if not invoked from IRQ context and the irq has been
707  * marked to enforce IRQ-context only.
708  */
709 int generic_handle_irq_safe(unsigned int irq)
710 {
711 	unsigned long flags;
712 	int ret;
713 
714 	local_irq_save(flags);
715 	ret = handle_irq_desc(irq_to_desc(irq));
716 	local_irq_restore(flags);
717 	return ret;
718 }
719 EXPORT_SYMBOL_GPL(generic_handle_irq_safe);
720 
721 #ifdef CONFIG_IRQ_DOMAIN
722 /**
723  * generic_handle_domain_irq - Invoke the handler for a HW irq belonging
724  *                             to a domain.
725  * @domain:	The domain where to perform the lookup
726  * @hwirq:	The HW irq number to convert to a logical one
727  *
728  * Returns:	0 on success, or -EINVAL if conversion has failed
729  *
730  * 		This function must be called from an IRQ context with irq regs
731  * 		initialized.
732  */
733 int generic_handle_domain_irq(struct irq_domain *domain, irq_hw_number_t hwirq)
734 {
735 	return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
736 }
737 EXPORT_SYMBOL_GPL(generic_handle_domain_irq);
738 
739  /**
740  * generic_handle_irq_safe - Invoke the handler for a HW irq belonging
741  *			     to a domain from any context.
742  * @domain:	The domain where to perform the lookup
743  * @hwirq:	The HW irq number to convert to a logical one
744  *
745  * Returns:	0 on success, a negative value on error.
746  *
747  * This function can be called from any context (IRQ or process
748  * context). If the interrupt is marked as 'enforce IRQ-context only' then
749  * the function must be invoked from hard interrupt context.
750  */
751 int generic_handle_domain_irq_safe(struct irq_domain *domain, irq_hw_number_t hwirq)
752 {
753 	unsigned long flags;
754 	int ret;
755 
756 	local_irq_save(flags);
757 	ret = handle_irq_desc(irq_resolve_mapping(domain, hwirq));
758 	local_irq_restore(flags);
759 	return ret;
760 }
761 EXPORT_SYMBOL_GPL(generic_handle_domain_irq_safe);
762 
763 /**
764  * generic_handle_domain_nmi - Invoke the handler for a HW nmi belonging
765  *                             to a domain.
766  * @domain:	The domain where to perform the lookup
767  * @hwirq:	The HW irq number to convert to a logical one
768  *
769  * Returns:	0 on success, or -EINVAL if conversion has failed
770  *
771  * 		This function must be called from an NMI context with irq regs
772  * 		initialized.
773  **/
774 int generic_handle_domain_nmi(struct irq_domain *domain, irq_hw_number_t hwirq)
775 {
776 	WARN_ON_ONCE(!in_nmi());
777 	return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
778 }
779 
780 #ifdef CONFIG_SMP
781 static bool demux_redirect_remote(struct irq_desc *desc)
782 {
783 	guard(raw_spinlock)(&desc->lock);
784 	const struct cpumask *m = irq_data_get_effective_affinity_mask(&desc->irq_data);
785 	unsigned int target_cpu = READ_ONCE(desc->redirect.target_cpu);
786 
787 	if (desc->irq_data.chip->irq_pre_redirect)
788 		desc->irq_data.chip->irq_pre_redirect(&desc->irq_data);
789 
790 	/*
791 	 * If the interrupt handler is already running on a CPU that's included
792 	 * in the interrupt's affinity mask, redirection is not necessary.
793 	 */
794 	if (cpumask_test_cpu(smp_processor_id(), m))
795 		return false;
796 
797 	/*
798 	 * The desc->action check protects against IRQ shutdown: __free_irq() sets
799 	 * desc->action to NULL while holding desc->lock, which we also hold.
800 	 *
801 	 * Calling irq_work_queue_on() here is safe w.r.t. CPU unplugging:
802 	 *   - takedown_cpu() schedules multi_cpu_stop() on all active CPUs,
803 	 *     including the one that's taken down.
804 	 *   - multi_cpu_stop() acts like a barrier, which means all active
805 	 *     CPUs go through MULTI_STOP_DISABLE_IRQ and disable hard IRQs
806 	 *     *before* the dying CPU runs take_cpu_down() in MULTI_STOP_RUN.
807 	 *   - Hard IRQs are re-enabled at the end of multi_cpu_stop(), *after*
808 	 *     the dying CPU has run take_cpu_down() in MULTI_STOP_RUN.
809 	 *   - Since we run in hard IRQ context, we run either before or after
810 	 *     take_cpu_down() but never concurrently.
811 	 *   - If we run before take_cpu_down(), the dying CPU hasn't been marked
812 	 *     offline yet (it's marked via take_cpu_down() -> __cpu_disable()),
813 	 *     so the WARN in irq_work_queue_on() can't occur.
814 	 *   - Furthermore, the work item we queue will be flushed later via
815 	 *     take_cpu_down() -> cpuhp_invoke_callback_range_nofail() ->
816 	 *     smpcfd_dying_cpu() -> irq_work_run().
817 	 *   - If we run after take_cpu_down(), target_cpu has been already
818 	 *     updated via take_cpu_down() -> __cpu_disable(), which eventually
819 	 *     calls irq_do_set_affinity() during IRQ migration. So, target_cpu
820 	 *     no longer points to the dying CPU in this case.
821 	 */
822 	if (desc->action)
823 		irq_work_queue_on(&desc->redirect.work, target_cpu);
824 
825 	return true;
826 }
827 #else /* CONFIG_SMP */
828 static bool demux_redirect_remote(struct irq_desc *desc)
829 {
830 	return false;
831 }
832 #endif
833 
834 /**
835  * generic_handle_demux_domain_irq - Invoke the handler for a hardware interrupt
836  *				     of a demultiplexing domain.
837  * @domain:	The domain where to perform the lookup
838  * @hwirq:	The hardware interrupt number to convert to a logical one
839  *
840  * Returns:	True on success, or false if lookup has failed
841  */
842 bool generic_handle_demux_domain_irq(struct irq_domain *domain, irq_hw_number_t hwirq)
843 {
844 	struct irq_desc *desc = irq_resolve_mapping(domain, hwirq);
845 
846 	if (unlikely(!desc))
847 		return false;
848 
849 	if (demux_redirect_remote(desc))
850 		return true;
851 
852 	return !handle_irq_desc(desc);
853 }
854 EXPORT_SYMBOL_GPL(generic_handle_demux_domain_irq);
855 
856 #endif
857 
858 /* Dynamic interrupt handling */
859 
860 /**
861  * irq_free_descs - free irq descriptors
862  * @from:	Start of descriptor range
863  * @cnt:	Number of consecutive irqs to free
864  */
865 void irq_free_descs(unsigned int from, unsigned int cnt)
866 {
867 	int i;
868 
869 	if (from >= total_nr_irqs || (from + cnt) > total_nr_irqs)
870 		return;
871 
872 	guard(mutex)(&sparse_irq_lock);
873 	for (i = 0; i < cnt; i++)
874 		free_desc(from + i);
875 }
876 EXPORT_SYMBOL_GPL(irq_free_descs);
877 
878 /**
879  * __irq_alloc_descs - allocate and initialize a range of irq descriptors
880  * @irq:	Allocate for specific irq number if irq >= 0
881  * @from:	Start the search from this irq number
882  * @cnt:	Number of consecutive irqs to allocate.
883  * @node:	Preferred node on which the irq descriptor should be allocated
884  * @owner:	Owning module (can be NULL)
885  * @affinity:	Optional pointer to an affinity mask array of size @cnt which
886  *		hints where the irq descriptors should be allocated and which
887  *		default affinities to use
888  *
889  * Returns the first irq number or error code
890  */
891 int __ref __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
892 			    struct module *owner, const struct irq_affinity_desc *affinity)
893 {
894 	int start;
895 
896 	if (!cnt)
897 		return -EINVAL;
898 
899 	if (irq >= 0) {
900 		if (from > irq)
901 			return -EINVAL;
902 		from = irq;
903 	} else {
904 		/*
905 		 * For interrupts which are freely allocated the
906 		 * architecture can force a lower bound to the @from
907 		 * argument. x86 uses this to exclude the GSI space.
908 		 */
909 		from = arch_dynirq_lower_bound(from);
910 	}
911 
912 	guard(mutex)(&sparse_irq_lock);
913 
914 	start = irq_find_free_area(from, cnt);
915 	if (irq >=0 && start != irq)
916 		return -EEXIST;
917 
918 	if (start + cnt > total_nr_irqs) {
919 		if (!irq_expand_nr_irqs(start + cnt))
920 			return -ENOMEM;
921 	}
922 	return alloc_descs(start, cnt, node, affinity, owner);
923 }
924 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
925 
926 /**
927  * irq_get_next_irq - get next allocated irq number
928  * @offset:	where to start the search
929  *
930  * Returns next irq number after offset or total_nr_irqs if none is found.
931  */
932 unsigned int irq_get_next_irq(unsigned int offset)
933 {
934 	struct irq_desc *desc;
935 
936 	guard(rcu)();
937 	desc = irq_find_desc_at_or_after(offset);
938 	return desc ? irq_desc_get_irq(desc) : total_nr_irqs;
939 }
940 
941 struct irq_desc *__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
942 				     unsigned int check)
943 {
944 	struct irq_desc *desc;
945 
946 	desc = irq_to_desc(irq);
947 	if (!desc)
948 		return NULL;
949 
950 	if (check & _IRQ_DESC_CHECK) {
951 		if ((check & _IRQ_DESC_PERCPU) && !irq_settings_is_per_cpu_devid(desc))
952 			return NULL;
953 
954 		if (!(check & _IRQ_DESC_PERCPU) && irq_settings_is_per_cpu_devid(desc))
955 			return NULL;
956 	}
957 
958 	if (bus)
959 		chip_bus_lock(desc);
960 	raw_spin_lock_irqsave(&desc->lock, *flags);
961 
962 	return desc;
963 }
964 
965 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
966 	__releases(&desc->lock)
967 {
968 	raw_spin_unlock_irqrestore(&desc->lock, flags);
969 	if (bus)
970 		chip_bus_sync_unlock(desc);
971 }
972 
973 int irq_set_percpu_devid(unsigned int irq)
974 {
975 	struct irq_desc *desc = irq_to_desc(irq);
976 
977 	if (!desc || desc->percpu_enabled)
978 		return -EINVAL;
979 
980 	desc->percpu_enabled = kzalloc_obj(*desc->percpu_enabled);
981 
982 	if (!desc->percpu_enabled)
983 		return -ENOMEM;
984 
985 	irq_set_percpu_devid_flags(irq);
986 	return 0;
987 }
988 
989 void kstat_incr_irq_this_cpu(unsigned int irq)
990 {
991 	kstat_incr_irqs_this_cpu(irq_to_desc(irq));
992 }
993 
994 /**
995  * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
996  * @irq:	The interrupt number
997  * @cpu:	The cpu number
998  *
999  * Returns the sum of interrupt counts on @cpu since boot for
1000  * @irq. The caller must ensure that the interrupt is not removed
1001  * concurrently.
1002  */
1003 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
1004 {
1005 	struct irq_desc *desc = irq_to_desc(irq);
1006 
1007 	return desc && desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, cpu) : 0;
1008 }
1009 
1010 static unsigned int kstat_irqs_desc(struct irq_desc *desc, const struct cpumask *cpumask)
1011 {
1012 	unsigned int sum = 0;
1013 	int cpu;
1014 
1015 	if (!irq_settings_is_per_cpu_devid(desc) &&
1016 	    !irq_settings_is_per_cpu(desc) &&
1017 	    !irq_is_nmi(desc))
1018 		return data_race(desc->tot_count);
1019 
1020 	for_each_cpu(cpu, cpumask)
1021 		sum += data_race(per_cpu(desc->kstat_irqs->cnt, cpu));
1022 	return sum;
1023 }
1024 
1025 static unsigned int kstat_irqs(unsigned int irq)
1026 {
1027 	struct irq_desc *desc = irq_to_desc(irq);
1028 
1029 	if (!desc || !desc->kstat_irqs)
1030 		return 0;
1031 	return kstat_irqs_desc(desc, cpu_possible_mask);
1032 }
1033 
1034 #ifdef CONFIG_GENERIC_IRQ_STAT_SNAPSHOT
1035 
1036 void kstat_snapshot_irqs(void)
1037 {
1038 	struct irq_desc *desc;
1039 	unsigned int irq;
1040 
1041 	for_each_irq_desc(irq, desc) {
1042 		if (!desc->kstat_irqs)
1043 			continue;
1044 		this_cpu_write(desc->kstat_irqs->ref, this_cpu_read(desc->kstat_irqs->cnt));
1045 	}
1046 }
1047 
1048 unsigned int kstat_get_irq_since_snapshot(unsigned int irq)
1049 {
1050 	struct irq_desc *desc = irq_to_desc(irq);
1051 
1052 	if (!desc || !desc->kstat_irqs)
1053 		return 0;
1054 	return this_cpu_read(desc->kstat_irqs->cnt) - this_cpu_read(desc->kstat_irqs->ref);
1055 }
1056 
1057 #endif
1058 
1059 /**
1060  * kstat_irqs_usr - Get the statistics for an interrupt from thread context
1061  * @irq:	The interrupt number
1062  *
1063  * Returns the sum of interrupt counts on all cpus since boot for @irq.
1064  *
1065  * It uses rcu to protect the access since a concurrent removal of an
1066  * interrupt descriptor is observing an rcu grace period before
1067  * delayed_free_desc()/irq_kobj_release().
1068  */
1069 unsigned int kstat_irqs_usr(unsigned int irq)
1070 {
1071 	unsigned int sum;
1072 
1073 	rcu_read_lock();
1074 	sum = kstat_irqs(irq);
1075 	rcu_read_unlock();
1076 	return sum;
1077 }
1078 
1079 #ifdef CONFIG_LOCKDEP
1080 void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class,
1081 			     struct lock_class_key *request_class)
1082 {
1083 	struct irq_desc *desc = irq_to_desc(irq);
1084 
1085 	if (desc) {
1086 		lockdep_set_class(&desc->lock, lock_class);
1087 		lockdep_set_class(&desc->request_mutex, request_class);
1088 	}
1089 }
1090 EXPORT_SYMBOL_GPL(__irq_set_lockdep_class);
1091 #endif
1092