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