hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
commit | author | age
a07526 1 // SPDX-License-Identifier: GPL-2.0
H 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/radix-tree.h>
16 #include <linux/bitmap.h>
17 #include <linux/irqdomain.h>
18 #include <linux/sysfs.h>
2f529f 19 #include <linux/irq_pipeline.h>
a07526 20
H 21 #include "internals.h"
22
23 /*
24  * lockdep: we want to handle all irq_desc locks as a single lock-class:
25  */
26 static struct lock_class_key irq_desc_lock_class;
27
28 #if defined(CONFIG_SMP)
29 static int __init irq_affinity_setup(char *str)
30 {
31    alloc_bootmem_cpumask_var(&irq_default_affinity);
32    cpulist_parse(str, irq_default_affinity);
33    /*
34     * Set at least the boot cpu. We don't want to end up with
35     * bugreports caused by random comandline masks
36     */
37    cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
38    return 1;
39 }
40 __setup("irqaffinity=", irq_affinity_setup);
41
42 static void __init init_irq_default_affinity(void)
43 {
44    if (!cpumask_available(irq_default_affinity))
45        zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
46    if (cpumask_empty(irq_default_affinity))
47        cpumask_setall(irq_default_affinity);
48 }
49 #else
50 static void __init init_irq_default_affinity(void)
51 {
52 }
53 #endif
54
55 #ifdef CONFIG_SMP
56 static int alloc_masks(struct irq_desc *desc, int node)
57 {
58    if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
59                     GFP_KERNEL, node))
60        return -ENOMEM;
61
62 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
63    if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity,
64                     GFP_KERNEL, node)) {
65        free_cpumask_var(desc->irq_common_data.affinity);
66        return -ENOMEM;
67    }
68 #endif
69
70 #ifdef CONFIG_GENERIC_PENDING_IRQ
71    if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) {
72 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
73        free_cpumask_var(desc->irq_common_data.effective_affinity);
74 #endif
75        free_cpumask_var(desc->irq_common_data.affinity);
76        return -ENOMEM;
77    }
78 #endif
79    return 0;
80 }
81
82 static void desc_smp_init(struct irq_desc *desc, int node,
83              const struct cpumask *affinity)
84 {
85    if (!affinity)
86        affinity = irq_default_affinity;
87    cpumask_copy(desc->irq_common_data.affinity, affinity);
88
89 #ifdef CONFIG_GENERIC_PENDING_IRQ
90    cpumask_clear(desc->pending_mask);
91 #endif
92 #ifdef CONFIG_NUMA
93    desc->irq_common_data.node = node;
94 #endif
95 }
96
97 #else
98 static inline int
99 alloc_masks(struct irq_desc *desc, int node) { return 0; }
100 static inline void
101 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
102 #endif
103
104 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
105                  const struct cpumask *affinity, struct module *owner)
106 {
107    int cpu;
108
109    desc->irq_common_data.handler_data = NULL;
110    desc->irq_common_data.msi_desc = NULL;
111
112    desc->irq_data.common = &desc->irq_common_data;
113    desc->irq_data.irq = irq;
114    desc->irq_data.chip = &no_irq_chip;
115    desc->irq_data.chip_data = NULL;
116    irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
117    irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
118    irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
119    desc->handle_irq = handle_bad_irq;
120    desc->depth = 1;
121    desc->irq_count = 0;
122    desc->irqs_unhandled = 0;
123    desc->tot_count = 0;
124    desc->name = NULL;
125    desc->owner = owner;
126    for_each_possible_cpu(cpu)
127        *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
128    desc_smp_init(desc, node, affinity);
129 }
130
131 int nr_irqs = NR_IRQS;
132 EXPORT_SYMBOL_GPL(nr_irqs);
133
134 static DEFINE_MUTEX(sparse_irq_lock);
135 static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
136
137 #ifdef CONFIG_SPARSE_IRQ
138
139 static void irq_kobj_release(struct kobject *kobj);
140
141 #ifdef CONFIG_SYSFS
142 static struct kobject *irq_kobj_base;
143
144 #define IRQ_ATTR_RO(_name) \
145 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
146
147 static ssize_t per_cpu_count_show(struct kobject *kobj,
148                  struct kobj_attribute *attr, char *buf)
149 {
150    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
151    int cpu, irq = desc->irq_data.irq;
152    ssize_t ret = 0;
153    char *p = "";
154
155    for_each_possible_cpu(cpu) {
156        unsigned int c = kstat_irqs_cpu(irq, cpu);
157
158        ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
159        p = ",";
160    }
161
162    ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
163    return ret;
164 }
165 IRQ_ATTR_RO(per_cpu_count);
166
167 static ssize_t chip_name_show(struct kobject *kobj,
168                  struct kobj_attribute *attr, char *buf)
169 {
170    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
171    ssize_t ret = 0;
172
173    raw_spin_lock_irq(&desc->lock);
174    if (desc->irq_data.chip && desc->irq_data.chip->name) {
175        ret = scnprintf(buf, PAGE_SIZE, "%s\n",
176                desc->irq_data.chip->name);
177    }
178    raw_spin_unlock_irq(&desc->lock);
179
180    return ret;
181 }
182 IRQ_ATTR_RO(chip_name);
183
184 static ssize_t hwirq_show(struct kobject *kobj,
185              struct kobj_attribute *attr, char *buf)
186 {
187    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
188    ssize_t ret = 0;
189
190    raw_spin_lock_irq(&desc->lock);
191    if (desc->irq_data.domain)
192        ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
193    raw_spin_unlock_irq(&desc->lock);
194
195    return ret;
196 }
197 IRQ_ATTR_RO(hwirq);
198
199 static ssize_t type_show(struct kobject *kobj,
200             struct kobj_attribute *attr, char *buf)
201 {
202    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
203    ssize_t ret = 0;
204
205    raw_spin_lock_irq(&desc->lock);
206    ret = sprintf(buf, "%s\n",
207              irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
208    raw_spin_unlock_irq(&desc->lock);
209
210    return ret;
211
212 }
213 IRQ_ATTR_RO(type);
214
215 static ssize_t wakeup_show(struct kobject *kobj,
216               struct kobj_attribute *attr, char *buf)
217 {
218    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
219    ssize_t ret = 0;
220
221    raw_spin_lock_irq(&desc->lock);
222    ret = sprintf(buf, "%s\n",
223              irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
224    raw_spin_unlock_irq(&desc->lock);
225
226    return ret;
227
228 }
229 IRQ_ATTR_RO(wakeup);
230
231 static ssize_t name_show(struct kobject *kobj,
232             struct kobj_attribute *attr, char *buf)
233 {
234    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
235    ssize_t ret = 0;
236
237    raw_spin_lock_irq(&desc->lock);
238    if (desc->name)
239        ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
240    raw_spin_unlock_irq(&desc->lock);
241
242    return ret;
243 }
244 IRQ_ATTR_RO(name);
245
246 static ssize_t actions_show(struct kobject *kobj,
247                struct kobj_attribute *attr, char *buf)
248 {
249    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
250    struct irqaction *action;
251    ssize_t ret = 0;
252    char *p = "";
253
254    raw_spin_lock_irq(&desc->lock);
255    for (action = desc->action; action != NULL; action = action->next) {
256        ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
257                 p, action->name);
258        p = ",";
259    }
260    raw_spin_unlock_irq(&desc->lock);
261
262    if (ret)
263        ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
264
265    return ret;
266 }
267 IRQ_ATTR_RO(actions);
268
269 static struct attribute *irq_attrs[] = {
270    &per_cpu_count_attr.attr,
271    &chip_name_attr.attr,
272    &hwirq_attr.attr,
273    &type_attr.attr,
274    &wakeup_attr.attr,
275    &name_attr.attr,
276    &actions_attr.attr,
277    NULL
278 };
279 ATTRIBUTE_GROUPS(irq);
280
281 static struct kobj_type irq_kobj_type = {
282    .release    = irq_kobj_release,
283    .sysfs_ops    = &kobj_sysfs_ops,
284    .default_groups = irq_groups,
285 };
286
287 static void irq_sysfs_add(int irq, struct irq_desc *desc)
288 {
289    if (irq_kobj_base) {
290        /*
291         * Continue even in case of failure as this is nothing
292         * crucial.
293         */
294        if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
295            pr_warn("Failed to add kobject for irq %d\n", irq);
296    }
297 }
298
299 static void irq_sysfs_del(struct irq_desc *desc)
300 {
301    /*
302     * If irq_sysfs_init() has not yet been invoked (early boot), then
303     * irq_kobj_base is NULL and the descriptor was never added.
304     * kobject_del() complains about a object with no parent, so make
305     * it conditional.
306     */
307    if (irq_kobj_base)
308        kobject_del(&desc->kobj);
309 }
310
311 static int __init irq_sysfs_init(void)
312 {
313    struct irq_desc *desc;
314    int irq;
315
316    /* Prevent concurrent irq alloc/free */
317    irq_lock_sparse();
318
319    irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
320    if (!irq_kobj_base) {
321        irq_unlock_sparse();
322        return -ENOMEM;
323    }
324
325    /* Add the already allocated interrupts */
326    for_each_irq_desc(irq, desc)
327        irq_sysfs_add(irq, desc);
328    irq_unlock_sparse();
329
330    return 0;
331 }
332 postcore_initcall(irq_sysfs_init);
333
334 #else /* !CONFIG_SYSFS */
335
336 static struct kobj_type irq_kobj_type = {
337    .release    = irq_kobj_release,
338 };
339
340 static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
341 static void irq_sysfs_del(struct irq_desc *desc) {}
342
343 #endif /* CONFIG_SYSFS */
344
345 static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
346
347 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
348 {
349    radix_tree_insert(&irq_desc_tree, irq, desc);
350 }
351
352 struct irq_desc *irq_to_desc(unsigned int irq)
353 {
354    return radix_tree_lookup(&irq_desc_tree, irq);
355 }
356 EXPORT_SYMBOL(irq_to_desc);
357
358 static void delete_irq_desc(unsigned int irq)
359 {
360    radix_tree_delete(&irq_desc_tree, irq);
361 }
362
363 #ifdef CONFIG_SMP
364 static void free_masks(struct irq_desc *desc)
365 {
366 #ifdef CONFIG_GENERIC_PENDING_IRQ
367    free_cpumask_var(desc->pending_mask);
368 #endif
369    free_cpumask_var(desc->irq_common_data.affinity);
370 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
371    free_cpumask_var(desc->irq_common_data.effective_affinity);
372 #endif
373 }
374 #else
375 static inline void free_masks(struct irq_desc *desc) { }
376 #endif
377
378 void irq_lock_sparse(void)
379 {
380    mutex_lock(&sparse_irq_lock);
381 }
382
383 void irq_unlock_sparse(void)
384 {
385    mutex_unlock(&sparse_irq_lock);
386 }
387
388 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
389                   const struct cpumask *affinity,
390                   struct module *owner)
391 {
392    struct irq_desc *desc;
393
394    desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
395    if (!desc)
396        return NULL;
397    /* allocate based on nr_cpu_ids */
398    desc->kstat_irqs = alloc_percpu(unsigned int);
399    if (!desc->kstat_irqs)
400        goto err_desc;
401
402    if (alloc_masks(desc, node))
403        goto err_kstat;
404
405    raw_spin_lock_init(&desc->lock);
406    lockdep_set_class(&desc->lock, &irq_desc_lock_class);
407    mutex_init(&desc->request_mutex);
408    init_rcu_head(&desc->rcu);
409    init_waitqueue_head(&desc->wait_for_threads);
410
411    desc_set_defaults(irq, desc, node, affinity, owner);
412    irqd_set(&desc->irq_data, flags);
413    kobject_init(&desc->kobj, &irq_kobj_type);
414
415    return desc;
416
417 err_kstat:
418    free_percpu(desc->kstat_irqs);
419 err_desc:
420    kfree(desc);
421    return NULL;
422 }
423
424 static void irq_kobj_release(struct kobject *kobj)
425 {
426    struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
427
428    free_masks(desc);
429    free_percpu(desc->kstat_irqs);
430    kfree(desc);
431 }
432
433 static void delayed_free_desc(struct rcu_head *rhp)
434 {
435    struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
436
437    kobject_put(&desc->kobj);
438 }
439
440 static void free_desc(unsigned int irq)
441 {
442    struct irq_desc *desc = irq_to_desc(irq);
443
444    irq_remove_debugfs_entry(desc);
445    unregister_irq_proc(irq, desc);
446
447    /*
448     * sparse_irq_lock protects also show_interrupts() and
449     * kstat_irq_usr(). Once we deleted the descriptor from the
450     * sparse tree we can free it. Access in proc will fail to
451     * lookup the descriptor.
452     *
453     * The sysfs entry must be serialized against a concurrent
454     * irq_sysfs_init() as well.
455     */
456    irq_sysfs_del(desc);
2f529f 457    uncache_irq_desc(irq);
a07526 458    delete_irq_desc(irq);
H 459
460    /*
461     * We free the descriptor, masks and stat fields via RCU. That
462     * allows demultiplex interrupts to do rcu based management of
463     * the child interrupts.
464     * This also allows us to use rcu in kstat_irqs_usr().
465     */
466    call_rcu(&desc->rcu, delayed_free_desc);
467 }
468
469 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
470               const struct irq_affinity_desc *affinity,
471               struct module *owner)
472 {
473    struct irq_desc *desc;
474    int i;
475
476    /* Validate affinity mask(s) */
477    if (affinity) {
478        for (i = 0; i < cnt; i++) {
479            if (cpumask_empty(&affinity[i].mask))
480                return -EINVAL;
481        }
482    }
483
484    for (i = 0; i < cnt; i++) {
485        const struct cpumask *mask = NULL;
486        unsigned int flags = 0;
487
488        if (affinity) {
489            if (affinity->is_managed) {
490                flags = IRQD_AFFINITY_MANAGED |
491                    IRQD_MANAGED_SHUTDOWN;
492            }
493            mask = &affinity->mask;
494            node = cpu_to_node(cpumask_first(mask));
495            affinity++;
496        }
497
498        desc = alloc_desc(start + i, node, flags, mask, owner);
499        if (!desc)
500            goto err;
501        irq_insert_desc(start + i, desc);
502        irq_sysfs_add(start + i, desc);
503        irq_add_debugfs_entry(start + i, desc);
504    }
505    bitmap_set(allocated_irqs, start, cnt);
506    return start;
507
508 err:
509    for (i--; i >= 0; i--)
510        free_desc(start + i);
511    return -ENOMEM;
512 }
513
514 static int irq_expand_nr_irqs(unsigned int nr)
515 {
516    if (nr > IRQ_BITMAP_BITS)
517        return -ENOMEM;
518    nr_irqs = nr;
519    return 0;
520 }
521
522 int __init early_irq_init(void)
523 {
524    int i, initcnt, node = first_online_node;
525    struct irq_desc *desc;
526
527    init_irq_default_affinity();
528
529    /* Let arch update nr_irqs and return the nr of preallocated irqs */
530    initcnt = arch_probe_nr_irqs();
531    printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
532           NR_IRQS, nr_irqs, initcnt);
533
534    if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
535        nr_irqs = IRQ_BITMAP_BITS;
536
537    if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
538        initcnt = IRQ_BITMAP_BITS;
539
540    if (initcnt > nr_irqs)
541        nr_irqs = initcnt;
542
543    for (i = 0; i < initcnt; i++) {
544        desc = alloc_desc(i, node, 0, NULL, NULL);
545        set_bit(i, allocated_irqs);
546        irq_insert_desc(i, desc);
547    }
548    return arch_early_irq_init();
549 }
550
551 #else /* !CONFIG_SPARSE_IRQ */
552
553 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
554    [0 ... NR_IRQS-1] = {
555        .handle_irq    = handle_bad_irq,
556        .depth        = 1,
557        .lock        = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
558    }
559 };
560
561 int __init early_irq_init(void)
562 {
563    int count, i, node = first_online_node;
564    struct irq_desc *desc;
565
566    init_irq_default_affinity();
567
568    printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
569
570    desc = irq_desc;
571    count = ARRAY_SIZE(irq_desc);
572
573    for (i = 0; i < count; i++) {
574        desc[i].kstat_irqs = alloc_percpu(unsigned int);
575        alloc_masks(&desc[i], node);
576        raw_spin_lock_init(&desc[i].lock);
577        lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
578        mutex_init(&desc[i].request_mutex);
579        init_waitqueue_head(&desc[i].wait_for_threads);
580        desc_set_defaults(i, &desc[i], node, NULL, NULL);
581    }
582    return arch_early_irq_init();
583 }
584
585 struct irq_desc *irq_to_desc(unsigned int irq)
586 {
587    return (irq < NR_IRQS) ? irq_desc + irq : NULL;
588 }
589 EXPORT_SYMBOL(irq_to_desc);
590
591 static void free_desc(unsigned int irq)
592 {
593    struct irq_desc *desc = irq_to_desc(irq);
594    unsigned long flags;
595
596    raw_spin_lock_irqsave(&desc->lock, flags);
597    desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
598    raw_spin_unlock_irqrestore(&desc->lock, flags);
599 }
600
601 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
602                  const struct irq_affinity_desc *affinity,
603                  struct module *owner)
604 {
605    u32 i;
606
607    for (i = 0; i < cnt; i++) {
608        struct irq_desc *desc = irq_to_desc(start + i);
609
610        desc->owner = owner;
611    }
612    bitmap_set(allocated_irqs, start, cnt);
613    return start;
614 }
615
616 static int irq_expand_nr_irqs(unsigned int nr)
617 {
618    return -ENOMEM;
619 }
620
621 void irq_mark_irq(unsigned int irq)
622 {
623    mutex_lock(&sparse_irq_lock);
624    bitmap_set(allocated_irqs, irq, 1);
625    mutex_unlock(&sparse_irq_lock);
626 }
627
628 #ifdef CONFIG_GENERIC_IRQ_LEGACY
629 void irq_init_desc(unsigned int irq)
630 {
631    free_desc(irq);
632 }
633 #endif
634
635 #endif /* !CONFIG_SPARSE_IRQ */
636
637 /**
2f529f 638  * generic_handle_irq - Handle a particular irq
a07526 639  * @irq:    The irq number to handle
H 640  *
2f529f 641  * The handler is invoked, unless we are entering the interrupt
H 642  * pipeline, in which case the incoming IRQ is only scheduled for
643  * deferred delivery.
a07526 644  */
H 645 int generic_handle_irq(unsigned int irq)
646 {
647    struct irq_desc *desc = irq_to_desc(irq);
648    struct irq_data *data;
649
650    if (!desc)
651        return -EINVAL;
652
653    data = irq_desc_get_irq_data(desc);
2f529f 654    if (WARN_ON_ONCE(!in_hard_irq() && handle_enforce_irqctx(data)))
a07526 655        return -EPERM;
H 656
657    generic_handle_irq_desc(desc);
658    return 0;
659 }
660 EXPORT_SYMBOL_GPL(generic_handle_irq);
661
662 #ifdef CONFIG_HANDLE_DOMAIN_IRQ
663 /**
664  * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
665  * @domain:    The domain where to perform the lookup
666  * @hwirq:    The HW irq number to convert to a logical one
667  * @lookup:    Whether to perform the domain lookup or not
668  * @regs:    Register file coming from the low-level handling code
669  *
670  * Returns:    0 on success, or -EINVAL if conversion has failed
671  */
672 int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
673            bool lookup, struct pt_regs *regs)
674 {
675    struct pt_regs *old_regs = set_irq_regs(regs);
676    unsigned int irq = hwirq;
677    struct irq_desc *desc;
678    int ret = 0;
679
680 #ifdef CONFIG_IRQ_DOMAIN
681    if (lookup)
682        irq = irq_find_mapping(domain, hwirq);
683 #endif
684
685    /*
686     * Some hardware gives randomly wrong interrupts.  Rather
687     * than crashing, do something sensible.
688     */
689    if (unlikely(!irq || irq >= nr_irqs || !(desc = irq_to_desc(irq)))) {
690        ack_bad_irq(irq);
691        ret = -EINVAL;
692        goto out;
693    }
694
695    if (IS_ENABLED(CONFIG_ARCH_WANTS_IRQ_RAW) &&
696        unlikely(irq_settings_is_raw(desc))) {
697        generic_handle_irq_desc(desc);
698    } else {
699        irq_enter();
700        generic_handle_irq_desc(desc);
701        irq_exit();
702    }
703
704 out:
705    set_irq_regs(old_regs);
706    return ret;
707 }
708
709 #ifdef CONFIG_IRQ_DOMAIN
710 /**
711  * handle_domain_nmi - Invoke the handler for a HW irq belonging to a domain
712  * @domain:    The domain where to perform the lookup
713  * @hwirq:    The HW irq number to convert to a logical one
714  * @regs:    Register file coming from the low-level handling code
715  *
716  *        This function must be called from an NMI context.
717  *
718  * Returns:    0 on success, or -EINVAL if conversion has failed
719  */
720 int handle_domain_nmi(struct irq_domain *domain, unsigned int hwirq,
721              struct pt_regs *regs)
722 {
723    struct pt_regs *old_regs = set_irq_regs(regs);
724    unsigned int irq;
725    int ret = 0;
726
727    /*
728     * NMI context needs to be setup earlier in order to deal with tracing.
729     */
730    WARN_ON(!in_nmi());
731
732    irq = irq_find_mapping(domain, hwirq);
733
734    /*
735     * ack_bad_irq is not NMI-safe, just report
736     * an invalid interrupt.
737     */
738    if (likely(irq))
739        generic_handle_irq(irq);
740    else
741        ret = -EINVAL;
742
743    set_irq_regs(old_regs);
744    return ret;
745 }
746 #endif
747 #endif
748
749 /* Dynamic interrupt handling */
750
751 /**
752  * irq_free_descs - free irq descriptors
753  * @from:    Start of descriptor range
754  * @cnt:    Number of consecutive irqs to free
755  */
756 void irq_free_descs(unsigned int from, unsigned int cnt)
757 {
758    int i;
759
760    if (from >= nr_irqs || (from + cnt) > nr_irqs)
761        return;
762
763    mutex_lock(&sparse_irq_lock);
764    for (i = 0; i < cnt; i++)
765        free_desc(from + i);
766
767    bitmap_clear(allocated_irqs, from, cnt);
768    mutex_unlock(&sparse_irq_lock);
769 }
770 EXPORT_SYMBOL_GPL(irq_free_descs);
771
772 /**
773  * __irq_alloc_descs - allocate and initialize a range of irq descriptors
774  * @irq:    Allocate for specific irq number if irq >= 0
775  * @from:    Start the search from this irq number
776  * @cnt:    Number of consecutive irqs to allocate.
777  * @node:    Preferred node on which the irq descriptor should be allocated
778  * @owner:    Owning module (can be NULL)
779  * @affinity:    Optional pointer to an affinity mask array of size @cnt which
780  *        hints where the irq descriptors should be allocated and which
781  *        default affinities to use
782  *
783  * Returns the first irq number or error code
784  */
785 int __ref
786 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
787          struct module *owner, const struct irq_affinity_desc *affinity)
788 {
789    int start, ret;
790
791    if (!cnt)
792        return -EINVAL;
793
794    if (irq >= 0) {
795        if (from > irq)
796            return -EINVAL;
797        from = irq;
798    } else {
799        /*
800         * For interrupts which are freely allocated the
801         * architecture can force a lower bound to the @from
802         * argument. x86 uses this to exclude the GSI space.
803         */
804        from = arch_dynirq_lower_bound(from);
805    }
806
807    mutex_lock(&sparse_irq_lock);
808
809    start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
810                       from, cnt, 0);
811    ret = -EEXIST;
812    if (irq >=0 && start != irq)
813        goto unlock;
814
815    if (start + cnt > nr_irqs) {
816        ret = irq_expand_nr_irqs(start + cnt);
817        if (ret)
818            goto unlock;
819    }
820    ret = alloc_descs(start, cnt, node, affinity, owner);
821 unlock:
822    mutex_unlock(&sparse_irq_lock);
823    return ret;
824 }
825 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
826
827 #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
828 /**
829  * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
830  * @cnt:    number of interrupts to allocate
831  * @node:    node on which to allocate
832  *
833  * Returns an interrupt number > 0 or 0, if the allocation fails.
834  */
835 unsigned int irq_alloc_hwirqs(int cnt, int node)
836 {
837    int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
838
839    if (irq < 0)
840        return 0;
841
842    for (i = irq; cnt > 0; i++, cnt--) {
843        if (arch_setup_hwirq(i, node))
844            goto err;
845        irq_clear_status_flags(i, _IRQ_NOREQUEST);
846    }
847    return irq;
848
849 err:
850    for (i--; i >= irq; i--) {
851        irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
852        arch_teardown_hwirq(i);
853    }
854    irq_free_descs(irq, cnt);
855    return 0;
856 }
857 EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
858
859 /**
860  * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
861  * @from:    Free from irq number
862  * @cnt:    number of interrupts to free
863  *
864  */
865 void irq_free_hwirqs(unsigned int from, int cnt)
866 {
867    int i, j;
868
869    for (i = from, j = cnt; j > 0; i++, j--) {
870        irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
871        arch_teardown_hwirq(i);
872    }
873    irq_free_descs(from, cnt);
874 }
875 EXPORT_SYMBOL_GPL(irq_free_hwirqs);
876 #endif
877
878 /**
879  * irq_get_next_irq - get next allocated irq number
880  * @offset:    where to start the search
881  *
882  * Returns next irq number after offset or nr_irqs if none is found.
883  */
884 unsigned int irq_get_next_irq(unsigned int offset)
885 {
886    return find_next_bit(allocated_irqs, nr_irqs, offset);
887 }
888
889 struct irq_desc *
890 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
891            unsigned int check)
892 {
893    struct irq_desc *desc = irq_to_desc(irq);
894
895    if (desc) {
896        if (check & _IRQ_DESC_CHECK) {
897            if ((check & _IRQ_DESC_PERCPU) &&
898                !irq_settings_is_per_cpu_devid(desc))
899                return NULL;
900
901            if (!(check & _IRQ_DESC_PERCPU) &&
902                irq_settings_is_per_cpu_devid(desc))
903                return NULL;
904        }
905
906        if (bus)
907            chip_bus_lock(desc);
908        raw_spin_lock_irqsave(&desc->lock, *flags);
909    }
910    return desc;
911 }
912
913 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
914    __releases(&desc->lock)
915 {
916    raw_spin_unlock_irqrestore(&desc->lock, flags);
917    if (bus)
918        chip_bus_sync_unlock(desc);
919 }
920
921 int irq_set_percpu_devid_partition(unsigned int irq,
922                   const struct cpumask *affinity)
923 {
924    struct irq_desc *desc = irq_to_desc(irq);
925
926    if (!desc)
927        return -EINVAL;
928
929    if (desc->percpu_enabled)
930        return -EINVAL;
931
932    desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
933
934    if (!desc->percpu_enabled)
935        return -ENOMEM;
936
937    if (affinity)
938        desc->percpu_affinity = affinity;
939    else
940        desc->percpu_affinity = cpu_possible_mask;
941
942    irq_set_percpu_devid_flags(irq);
943    return 0;
944 }
945
946 int irq_set_percpu_devid(unsigned int irq)
947 {
948    return irq_set_percpu_devid_partition(irq, NULL);
949 }
950
951 int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
952 {
953    struct irq_desc *desc = irq_to_desc(irq);
954
955    if (!desc || !desc->percpu_enabled)
956        return -EINVAL;
957
958    if (affinity)
959        cpumask_copy(affinity, desc->percpu_affinity);
960
961    return 0;
962 }
963 EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
964
965 void kstat_incr_irq_this_cpu(unsigned int irq)
966 {
967    kstat_incr_irqs_this_cpu(irq_to_desc(irq));
968 }
969
970 /**
971  * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
972  * @irq:    The interrupt number
973  * @cpu:    The cpu number
974  *
975  * Returns the sum of interrupt counts on @cpu since boot for
976  * @irq. The caller must ensure that the interrupt is not removed
977  * concurrently.
978  */
979 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
980 {
981    struct irq_desc *desc = irq_to_desc(irq);
982
983    return desc && desc->kstat_irqs ?
984            *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
985 }
986 EXPORT_SYMBOL_GPL(kstat_irqs_cpu);
987
988 static bool irq_is_nmi(struct irq_desc *desc)
989 {
990    return desc->istate & IRQS_NMI;
991 }
992
993 /**
994  * kstat_irqs - Get the statistics for an interrupt
995  * @irq:    The interrupt number
996  *
997  * Returns the sum of interrupt counts on all cpus since boot for
998  * @irq. The caller must ensure that the interrupt is not removed
999  * concurrently.
1000  */
1001 unsigned int kstat_irqs(unsigned int irq)
1002 {
1003    struct irq_desc *desc = irq_to_desc(irq);
1004    unsigned int sum = 0;
1005    int cpu;
1006
1007    if (!desc || !desc->kstat_irqs)
1008        return 0;
1009    if (!irq_settings_is_per_cpu_devid(desc) &&
1010        !irq_settings_is_per_cpu(desc) &&
1011        !irq_is_nmi(desc))
1012        return desc->tot_count;
1013
1014    for_each_possible_cpu(cpu)
1015        sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
1016    return sum;
1017 }
1018
1019 /**
1020  * kstat_irqs_usr - Get the statistics for an interrupt
1021  * @irq:    The interrupt number
1022  *
1023  * Returns the sum of interrupt counts on all cpus since boot for @irq.
1024  * Contrary to kstat_irqs() this can be called from any context.
1025  * It uses rcu since a concurrent removal of an interrupt descriptor is
1026  * observing an rcu grace period before delayed_free_desc()/irq_kobj_release().
1027  */
1028 unsigned int kstat_irqs_usr(unsigned int irq)
1029 {
1030    unsigned int sum;
1031
1032    rcu_read_lock();
1033    sum = kstat_irqs(irq);
1034    rcu_read_unlock();
1035    return sum;
1036 }
1037 EXPORT_SYMBOL_GPL(kstat_irqs_usr);