]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/irq/irqdesc.c
genirq: Move bus locking into __setup_irq()
[mirror_ubuntu-focal-kernel.git] / kernel / irq / irqdesc.c
CommitLineData
3795de23
TG
1/*
2 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
3 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
4 *
5 * This file contains the interrupt descriptor management code
6 *
7 * Detailed information is available in Documentation/DocBook/genericirq
8 *
9 */
10#include <linux/irq.h>
11#include <linux/slab.h>
ec53cf23 12#include <linux/export.h>
3795de23
TG
13#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
15#include <linux/radix-tree.h>
1f5a5b87 16#include <linux/bitmap.h>
76ba59f8 17#include <linux/irqdomain.h>
ecb3f394 18#include <linux/sysfs.h>
3795de23
TG
19
20#include "internals.h"
21
22/*
23 * lockdep: we want to handle all irq_desc locks as a single lock-class:
24 */
78f90d91 25static struct lock_class_key irq_desc_lock_class;
3795de23 26
fe051434 27#if defined(CONFIG_SMP)
fbf19803
TG
28static int __init irq_affinity_setup(char *str)
29{
30 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
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 comandline masks
35 */
36 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
37 return 1;
38}
39__setup("irqaffinity=", irq_affinity_setup);
40
3795de23
TG
41static void __init init_irq_default_affinity(void)
42{
fbf19803
TG
43#ifdef CONFIG_CPUMASK_OFFSTACK
44 if (!irq_default_affinity)
45 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
46#endif
47 if (cpumask_empty(irq_default_affinity))
48 cpumask_setall(irq_default_affinity);
3795de23
TG
49}
50#else
51static void __init init_irq_default_affinity(void)
52{
53}
54#endif
55
1f5a5b87 56#ifdef CONFIG_SMP
4ab764c3 57static int alloc_masks(struct irq_desc *desc, int node)
1f5a5b87 58{
9df872fa 59 if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
4ab764c3 60 GFP_KERNEL, node))
1f5a5b87
TG
61 return -ENOMEM;
62
0d3f5425
TG
63#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
64 if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity,
65 GFP_KERNEL, node)) {
66 free_cpumask_var(desc->irq_common_data.affinity);
67 return -ENOMEM;
68 }
69#endif
70
1f5a5b87 71#ifdef CONFIG_GENERIC_PENDING_IRQ
4ab764c3 72 if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) {
0d3f5425
TG
73#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
74 free_cpumask_var(desc->irq_common_data.effective_affinity);
75#endif
9df872fa 76 free_cpumask_var(desc->irq_common_data.affinity);
1f5a5b87
TG
77 return -ENOMEM;
78 }
79#endif
80 return 0;
81}
82
45ddcecb
TG
83static void desc_smp_init(struct irq_desc *desc, int node,
84 const struct cpumask *affinity)
1f5a5b87 85{
45ddcecb
TG
86 if (!affinity)
87 affinity = irq_default_affinity;
88 cpumask_copy(desc->irq_common_data.affinity, affinity);
89
b7b29338
TG
90#ifdef CONFIG_GENERIC_PENDING_IRQ
91 cpumask_clear(desc->pending_mask);
92#endif
449e9cae
JL
93#ifdef CONFIG_NUMA
94 desc->irq_common_data.node = node;
95#endif
b7b29338
TG
96}
97
1f5a5b87
TG
98#else
99static inline int
4ab764c3 100alloc_masks(struct irq_desc *desc, int node) { return 0; }
45ddcecb
TG
101static inline void
102desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
1f5a5b87
TG
103#endif
104
b6873807 105static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
45ddcecb 106 const struct cpumask *affinity, struct module *owner)
1f5a5b87 107{
6c9ae009
ED
108 int cpu;
109
af7080e0 110 desc->irq_common_data.handler_data = NULL;
b237721c 111 desc->irq_common_data.msi_desc = NULL;
af7080e0 112
0d0b4c86 113 desc->irq_data.common = &desc->irq_common_data;
1f5a5b87
TG
114 desc->irq_data.irq = irq;
115 desc->irq_data.chip = &no_irq_chip;
116 desc->irq_data.chip_data = NULL;
f9e4989e 117 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
801a0e9a 118 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
d829b8fb 119 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
1f5a5b87
TG
120 desc->handle_irq = handle_bad_irq;
121 desc->depth = 1;
b7b29338
TG
122 desc->irq_count = 0;
123 desc->irqs_unhandled = 0;
1f5a5b87 124 desc->name = NULL;
b6873807 125 desc->owner = owner;
6c9ae009
ED
126 for_each_possible_cpu(cpu)
127 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
45ddcecb 128 desc_smp_init(desc, node, affinity);
1f5a5b87
TG
129}
130
3795de23
TG
131int nr_irqs = NR_IRQS;
132EXPORT_SYMBOL_GPL(nr_irqs);
133
a05a900a 134static DEFINE_MUTEX(sparse_irq_lock);
c1ee6264 135static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
1f5a5b87 136
3795de23
TG
137#ifdef CONFIG_SPARSE_IRQ
138
ecb3f394
CG
139static void irq_kobj_release(struct kobject *kobj);
140
141#ifdef CONFIG_SYSFS
142static struct kobject *irq_kobj_base;
143
144#define IRQ_ATTR_RO(_name) \
145static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
146
147static 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}
165IRQ_ATTR_RO(per_cpu_count);
166
167static 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}
182IRQ_ATTR_RO(chip_name);
183
184static 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}
197IRQ_ATTR_RO(hwirq);
198
199static 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}
213IRQ_ATTR_RO(type);
214
215static ssize_t name_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 if (desc->name)
223 ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
224 raw_spin_unlock_irq(&desc->lock);
225
226 return ret;
227}
228IRQ_ATTR_RO(name);
229
230static ssize_t actions_show(struct kobject *kobj,
231 struct kobj_attribute *attr, char *buf)
232{
233 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
234 struct irqaction *action;
235 ssize_t ret = 0;
236 char *p = "";
237
238 raw_spin_lock_irq(&desc->lock);
239 for (action = desc->action; action != NULL; action = action->next) {
240 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
241 p, action->name);
242 p = ",";
243 }
244 raw_spin_unlock_irq(&desc->lock);
245
246 if (ret)
247 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
248
249 return ret;
250}
251IRQ_ATTR_RO(actions);
252
253static struct attribute *irq_attrs[] = {
254 &per_cpu_count_attr.attr,
255 &chip_name_attr.attr,
256 &hwirq_attr.attr,
257 &type_attr.attr,
258 &name_attr.attr,
259 &actions_attr.attr,
260 NULL
261};
262
263static struct kobj_type irq_kobj_type = {
264 .release = irq_kobj_release,
265 .sysfs_ops = &kobj_sysfs_ops,
266 .default_attrs = irq_attrs,
267};
268
269static void irq_sysfs_add(int irq, struct irq_desc *desc)
270{
271 if (irq_kobj_base) {
272 /*
273 * Continue even in case of failure as this is nothing
274 * crucial.
275 */
276 if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
277 pr_warn("Failed to add kobject for irq %d\n", irq);
278 }
279}
280
281static int __init irq_sysfs_init(void)
282{
283 struct irq_desc *desc;
284 int irq;
285
286 /* Prevent concurrent irq alloc/free */
287 irq_lock_sparse();
288
289 irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
290 if (!irq_kobj_base) {
291 irq_unlock_sparse();
292 return -ENOMEM;
293 }
294
295 /* Add the already allocated interrupts */
296 for_each_irq_desc(irq, desc)
297 irq_sysfs_add(irq, desc);
298 irq_unlock_sparse();
299
300 return 0;
301}
302postcore_initcall(irq_sysfs_init);
303
304#else /* !CONFIG_SYSFS */
305
306static struct kobj_type irq_kobj_type = {
307 .release = irq_kobj_release,
308};
309
310static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
311
312#endif /* CONFIG_SYSFS */
313
baa0d233 314static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
3795de23 315
1f5a5b87 316static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
3795de23
TG
317{
318 radix_tree_insert(&irq_desc_tree, irq, desc);
319}
320
321struct irq_desc *irq_to_desc(unsigned int irq)
322{
323 return radix_tree_lookup(&irq_desc_tree, irq);
324}
3911ff30 325EXPORT_SYMBOL(irq_to_desc);
3795de23 326
1f5a5b87
TG
327static void delete_irq_desc(unsigned int irq)
328{
329 radix_tree_delete(&irq_desc_tree, irq);
330}
331
332#ifdef CONFIG_SMP
333static void free_masks(struct irq_desc *desc)
334{
335#ifdef CONFIG_GENERIC_PENDING_IRQ
336 free_cpumask_var(desc->pending_mask);
337#endif
9df872fa 338 free_cpumask_var(desc->irq_common_data.affinity);
0d3f5425
TG
339#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
340 free_cpumask_var(desc->irq_common_data.effective_affinity);
341#endif
1f5a5b87
TG
342}
343#else
344static inline void free_masks(struct irq_desc *desc) { }
345#endif
346
c291ee62
TG
347void irq_lock_sparse(void)
348{
349 mutex_lock(&sparse_irq_lock);
350}
351
352void irq_unlock_sparse(void)
353{
354 mutex_unlock(&sparse_irq_lock);
355}
356
45ddcecb
TG
357static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
358 const struct cpumask *affinity,
359 struct module *owner)
1f5a5b87
TG
360{
361 struct irq_desc *desc;
1f5a5b87 362
4ab764c3 363 desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
1f5a5b87
TG
364 if (!desc)
365 return NULL;
366 /* allocate based on nr_cpu_ids */
6c9ae009 367 desc->kstat_irqs = alloc_percpu(unsigned int);
1f5a5b87
TG
368 if (!desc->kstat_irqs)
369 goto err_desc;
370
4ab764c3 371 if (alloc_masks(desc, node))
1f5a5b87
TG
372 goto err_kstat;
373
374 raw_spin_lock_init(&desc->lock);
375 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
425a5072 376 init_rcu_head(&desc->rcu);
1f5a5b87 377
45ddcecb
TG
378 desc_set_defaults(irq, desc, node, affinity, owner);
379 irqd_set(&desc->irq_data, flags);
ecb3f394 380 kobject_init(&desc->kobj, &irq_kobj_type);
1f5a5b87
TG
381
382 return desc;
383
384err_kstat:
6c9ae009 385 free_percpu(desc->kstat_irqs);
1f5a5b87
TG
386err_desc:
387 kfree(desc);
388 return NULL;
389}
390
ecb3f394 391static void irq_kobj_release(struct kobject *kobj)
425a5072 392{
ecb3f394 393 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
425a5072
TG
394
395 free_masks(desc);
396 free_percpu(desc->kstat_irqs);
397 kfree(desc);
398}
399
ecb3f394
CG
400static void delayed_free_desc(struct rcu_head *rhp)
401{
402 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
403
404 kobject_put(&desc->kobj);
405}
406
1f5a5b87
TG
407static void free_desc(unsigned int irq)
408{
409 struct irq_desc *desc = irq_to_desc(irq);
1f5a5b87 410
087cdfb6 411 irq_remove_debugfs_entry(desc);
13bfe99e
TG
412 unregister_irq_proc(irq, desc);
413
c291ee62
TG
414 /*
415 * sparse_irq_lock protects also show_interrupts() and
416 * kstat_irq_usr(). Once we deleted the descriptor from the
417 * sparse tree we can free it. Access in proc will fail to
418 * lookup the descriptor.
ecb3f394
CG
419 *
420 * The sysfs entry must be serialized against a concurrent
421 * irq_sysfs_init() as well.
c291ee62 422 */
a05a900a 423 mutex_lock(&sparse_irq_lock);
ecb3f394 424 kobject_del(&desc->kobj);
1f5a5b87 425 delete_irq_desc(irq);
a05a900a 426 mutex_unlock(&sparse_irq_lock);
1f5a5b87 427
425a5072
TG
428 /*
429 * We free the descriptor, masks and stat fields via RCU. That
430 * allows demultiplex interrupts to do rcu based management of
431 * the child interrupts.
432 */
433 call_rcu(&desc->rcu, delayed_free_desc);
1f5a5b87
TG
434}
435
b6873807 436static int alloc_descs(unsigned int start, unsigned int cnt, int node,
06ee6d57 437 const struct cpumask *affinity, struct module *owner)
1f5a5b87 438{
45ddcecb 439 const struct cpumask *mask = NULL;
1f5a5b87 440 struct irq_desc *desc;
45ddcecb 441 unsigned int flags;
e75eafb9 442 int i;
45ddcecb 443
e75eafb9
TG
444 /* Validate affinity mask(s) */
445 if (affinity) {
446 for (i = 0, mask = affinity; i < cnt; i++, mask++) {
447 if (cpumask_empty(mask))
448 return -EINVAL;
449 }
450 }
45ddcecb
TG
451
452 flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
e75eafb9 453 mask = NULL;
1f5a5b87
TG
454
455 for (i = 0; i < cnt; i++) {
45ddcecb 456 if (affinity) {
e75eafb9
TG
457 node = cpu_to_node(cpumask_first(affinity));
458 mask = affinity;
459 affinity++;
45ddcecb
TG
460 }
461 desc = alloc_desc(start + i, node, flags, mask, owner);
1f5a5b87
TG
462 if (!desc)
463 goto err;
a05a900a 464 mutex_lock(&sparse_irq_lock);
1f5a5b87 465 irq_insert_desc(start + i, desc);
ecb3f394 466 irq_sysfs_add(start + i, desc);
a05a900a 467 mutex_unlock(&sparse_irq_lock);
1f5a5b87
TG
468 }
469 return start;
470
471err:
472 for (i--; i >= 0; i--)
473 free_desc(start + i);
474
a05a900a 475 mutex_lock(&sparse_irq_lock);
1f5a5b87 476 bitmap_clear(allocated_irqs, start, cnt);
a05a900a 477 mutex_unlock(&sparse_irq_lock);
1f5a5b87
TG
478 return -ENOMEM;
479}
480
ed4dea6e 481static int irq_expand_nr_irqs(unsigned int nr)
e7bcecb7 482{
ed4dea6e 483 if (nr > IRQ_BITMAP_BITS)
e7bcecb7 484 return -ENOMEM;
ed4dea6e 485 nr_irqs = nr;
e7bcecb7
TG
486 return 0;
487}
488
3795de23
TG
489int __init early_irq_init(void)
490{
b683de2b 491 int i, initcnt, node = first_online_node;
3795de23 492 struct irq_desc *desc;
3795de23
TG
493
494 init_irq_default_affinity();
495
b683de2b
TG
496 /* Let arch update nr_irqs and return the nr of preallocated irqs */
497 initcnt = arch_probe_nr_irqs();
5a29ef22
VL
498 printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
499 NR_IRQS, nr_irqs, initcnt);
3795de23 500
c1ee6264
TG
501 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
502 nr_irqs = IRQ_BITMAP_BITS;
503
504 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
505 initcnt = IRQ_BITMAP_BITS;
506
507 if (initcnt > nr_irqs)
508 nr_irqs = initcnt;
509
b683de2b 510 for (i = 0; i < initcnt; i++) {
45ddcecb 511 desc = alloc_desc(i, node, 0, NULL, NULL);
aa99ec0f
TG
512 set_bit(i, allocated_irqs);
513 irq_insert_desc(i, desc);
3795de23 514 }
3795de23
TG
515 return arch_early_irq_init();
516}
517
3795de23
TG
518#else /* !CONFIG_SPARSE_IRQ */
519
520struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
521 [0 ... NR_IRQS-1] = {
3795de23
TG
522 .handle_irq = handle_bad_irq,
523 .depth = 1,
524 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
525 }
526};
527
3795de23
TG
528int __init early_irq_init(void)
529{
aa99ec0f 530 int count, i, node = first_online_node;
3795de23 531 struct irq_desc *desc;
3795de23
TG
532
533 init_irq_default_affinity();
534
5a29ef22 535 printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
3795de23
TG
536
537 desc = irq_desc;
538 count = ARRAY_SIZE(irq_desc);
539
540 for (i = 0; i < count; i++) {
6c9ae009 541 desc[i].kstat_irqs = alloc_percpu(unsigned int);
4ab764c3 542 alloc_masks(&desc[i], node);
e7fbad30 543 raw_spin_lock_init(&desc[i].lock);
154cd387 544 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
45ddcecb 545 desc_set_defaults(i, &desc[i], node, NULL, NULL);
3795de23
TG
546 }
547 return arch_early_irq_init();
548}
549
550struct irq_desc *irq_to_desc(unsigned int irq)
551{
552 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
553}
2c45aada 554EXPORT_SYMBOL(irq_to_desc);
3795de23 555
1f5a5b87
TG
556static void free_desc(unsigned int irq)
557{
d8179bc0
TG
558 struct irq_desc *desc = irq_to_desc(irq);
559 unsigned long flags;
560
561 raw_spin_lock_irqsave(&desc->lock, flags);
45ddcecb 562 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
d8179bc0 563 raw_spin_unlock_irqrestore(&desc->lock, flags);
1f5a5b87
TG
564}
565
b6873807 566static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
06ee6d57 567 const struct cpumask *affinity,
b6873807 568 struct module *owner)
1f5a5b87 569{
b6873807
SAS
570 u32 i;
571
572 for (i = 0; i < cnt; i++) {
573 struct irq_desc *desc = irq_to_desc(start + i);
574
575 desc->owner = owner;
576 }
1f5a5b87
TG
577 return start;
578}
e7bcecb7 579
ed4dea6e 580static int irq_expand_nr_irqs(unsigned int nr)
e7bcecb7
TG
581{
582 return -ENOMEM;
583}
584
f63b6a05
TG
585void irq_mark_irq(unsigned int irq)
586{
587 mutex_lock(&sparse_irq_lock);
588 bitmap_set(allocated_irqs, irq, 1);
589 mutex_unlock(&sparse_irq_lock);
590}
591
c940e01c
TG
592#ifdef CONFIG_GENERIC_IRQ_LEGACY
593void irq_init_desc(unsigned int irq)
594{
d8179bc0 595 free_desc(irq);
c940e01c
TG
596}
597#endif
598
3795de23
TG
599#endif /* !CONFIG_SPARSE_IRQ */
600
fe12bc2c
TG
601/**
602 * generic_handle_irq - Invoke the handler for a particular irq
603 * @irq: The irq number to handle
604 *
605 */
606int generic_handle_irq(unsigned int irq)
607{
608 struct irq_desc *desc = irq_to_desc(irq);
609
610 if (!desc)
611 return -EINVAL;
bd0b9ac4 612 generic_handle_irq_desc(desc);
fe12bc2c
TG
613 return 0;
614}
edf76f83 615EXPORT_SYMBOL_GPL(generic_handle_irq);
fe12bc2c 616
76ba59f8
MZ
617#ifdef CONFIG_HANDLE_DOMAIN_IRQ
618/**
619 * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
620 * @domain: The domain where to perform the lookup
621 * @hwirq: The HW irq number to convert to a logical one
622 * @lookup: Whether to perform the domain lookup or not
623 * @regs: Register file coming from the low-level handling code
624 *
625 * Returns: 0 on success, or -EINVAL if conversion has failed
626 */
627int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
628 bool lookup, struct pt_regs *regs)
629{
630 struct pt_regs *old_regs = set_irq_regs(regs);
631 unsigned int irq = hwirq;
632 int ret = 0;
633
634 irq_enter();
635
636#ifdef CONFIG_IRQ_DOMAIN
637 if (lookup)
638 irq = irq_find_mapping(domain, hwirq);
639#endif
640
641 /*
642 * Some hardware gives randomly wrong interrupts. Rather
643 * than crashing, do something sensible.
644 */
645 if (unlikely(!irq || irq >= nr_irqs)) {
646 ack_bad_irq(irq);
647 ret = -EINVAL;
648 } else {
649 generic_handle_irq(irq);
650 }
651
652 irq_exit();
653 set_irq_regs(old_regs);
654 return ret;
655}
656#endif
657
1f5a5b87
TG
658/* Dynamic interrupt handling */
659
660/**
661 * irq_free_descs - free irq descriptors
662 * @from: Start of descriptor range
663 * @cnt: Number of consecutive irqs to free
664 */
665void irq_free_descs(unsigned int from, unsigned int cnt)
666{
1f5a5b87
TG
667 int i;
668
669 if (from >= nr_irqs || (from + cnt) > nr_irqs)
670 return;
671
672 for (i = 0; i < cnt; i++)
673 free_desc(from + i);
674
a05a900a 675 mutex_lock(&sparse_irq_lock);
1f5a5b87 676 bitmap_clear(allocated_irqs, from, cnt);
a05a900a 677 mutex_unlock(&sparse_irq_lock);
1f5a5b87 678}
edf76f83 679EXPORT_SYMBOL_GPL(irq_free_descs);
1f5a5b87
TG
680
681/**
682 * irq_alloc_descs - allocate and initialize a range of irq descriptors
683 * @irq: Allocate for specific irq number if irq >= 0
684 * @from: Start the search from this irq number
685 * @cnt: Number of consecutive irqs to allocate.
686 * @node: Preferred node on which the irq descriptor should be allocated
d522a0d1 687 * @owner: Owning module (can be NULL)
e75eafb9
TG
688 * @affinity: Optional pointer to an affinity mask array of size @cnt which
689 * hints where the irq descriptors should be allocated and which
690 * default affinities to use
1f5a5b87
TG
691 *
692 * Returns the first irq number or error code
693 */
694int __ref
b6873807 695__irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
06ee6d57 696 struct module *owner, const struct cpumask *affinity)
1f5a5b87 697{
1f5a5b87
TG
698 int start, ret;
699
700 if (!cnt)
701 return -EINVAL;
702
c5182b88
MB
703 if (irq >= 0) {
704 if (from > irq)
705 return -EINVAL;
706 from = irq;
62a08ae2
TG
707 } else {
708 /*
709 * For interrupts which are freely allocated the
710 * architecture can force a lower bound to the @from
711 * argument. x86 uses this to exclude the GSI space.
712 */
713 from = arch_dynirq_lower_bound(from);
c5182b88
MB
714 }
715
a05a900a 716 mutex_lock(&sparse_irq_lock);
1f5a5b87 717
ed4dea6e
YL
718 start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
719 from, cnt, 0);
1f5a5b87
TG
720 ret = -EEXIST;
721 if (irq >=0 && start != irq)
722 goto err;
723
ed4dea6e
YL
724 if (start + cnt > nr_irqs) {
725 ret = irq_expand_nr_irqs(start + cnt);
e7bcecb7
TG
726 if (ret)
727 goto err;
728 }
1f5a5b87
TG
729
730 bitmap_set(allocated_irqs, start, cnt);
a05a900a 731 mutex_unlock(&sparse_irq_lock);
06ee6d57 732 return alloc_descs(start, cnt, node, affinity, owner);
1f5a5b87
TG
733
734err:
a05a900a 735 mutex_unlock(&sparse_irq_lock);
1f5a5b87
TG
736 return ret;
737}
b6873807 738EXPORT_SYMBOL_GPL(__irq_alloc_descs);
1f5a5b87 739
7b6ef126
TG
740#ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
741/**
742 * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
743 * @cnt: number of interrupts to allocate
744 * @node: node on which to allocate
745 *
746 * Returns an interrupt number > 0 or 0, if the allocation fails.
747 */
748unsigned int irq_alloc_hwirqs(int cnt, int node)
749{
06ee6d57 750 int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
7b6ef126
TG
751
752 if (irq < 0)
753 return 0;
754
755 for (i = irq; cnt > 0; i++, cnt--) {
756 if (arch_setup_hwirq(i, node))
757 goto err;
758 irq_clear_status_flags(i, _IRQ_NOREQUEST);
759 }
760 return irq;
761
762err:
763 for (i--; i >= irq; i--) {
764 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
765 arch_teardown_hwirq(i);
766 }
767 irq_free_descs(irq, cnt);
768 return 0;
769}
770EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
771
772/**
773 * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
774 * @from: Free from irq number
775 * @cnt: number of interrupts to free
776 *
777 */
778void irq_free_hwirqs(unsigned int from, int cnt)
779{
8844aad8 780 int i, j;
7b6ef126 781
8844aad8 782 for (i = from, j = cnt; j > 0; i++, j--) {
7b6ef126
TG
783 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
784 arch_teardown_hwirq(i);
785 }
786 irq_free_descs(from, cnt);
787}
788EXPORT_SYMBOL_GPL(irq_free_hwirqs);
789#endif
790
a98d24b7
TG
791/**
792 * irq_get_next_irq - get next allocated irq number
793 * @offset: where to start the search
794 *
795 * Returns next irq number after offset or nr_irqs if none is found.
796 */
797unsigned int irq_get_next_irq(unsigned int offset)
798{
799 return find_next_bit(allocated_irqs, nr_irqs, offset);
800}
801
d5eb4ad2 802struct irq_desc *
31d9d9b6
MZ
803__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
804 unsigned int check)
d5eb4ad2
TG
805{
806 struct irq_desc *desc = irq_to_desc(irq);
807
808 if (desc) {
31d9d9b6
MZ
809 if (check & _IRQ_DESC_CHECK) {
810 if ((check & _IRQ_DESC_PERCPU) &&
811 !irq_settings_is_per_cpu_devid(desc))
812 return NULL;
813
814 if (!(check & _IRQ_DESC_PERCPU) &&
815 irq_settings_is_per_cpu_devid(desc))
816 return NULL;
817 }
818
d5eb4ad2
TG
819 if (bus)
820 chip_bus_lock(desc);
821 raw_spin_lock_irqsave(&desc->lock, *flags);
822 }
823 return desc;
824}
825
826void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
827{
828 raw_spin_unlock_irqrestore(&desc->lock, flags);
829 if (bus)
830 chip_bus_sync_unlock(desc);
831}
832
222df54f
MZ
833int irq_set_percpu_devid_partition(unsigned int irq,
834 const struct cpumask *affinity)
31d9d9b6
MZ
835{
836 struct irq_desc *desc = irq_to_desc(irq);
837
838 if (!desc)
839 return -EINVAL;
840
841 if (desc->percpu_enabled)
842 return -EINVAL;
843
844 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
845
846 if (!desc->percpu_enabled)
847 return -ENOMEM;
848
222df54f
MZ
849 if (affinity)
850 desc->percpu_affinity = affinity;
851 else
852 desc->percpu_affinity = cpu_possible_mask;
853
31d9d9b6
MZ
854 irq_set_percpu_devid_flags(irq);
855 return 0;
856}
857
222df54f
MZ
858int irq_set_percpu_devid(unsigned int irq)
859{
860 return irq_set_percpu_devid_partition(irq, NULL);
861}
862
863int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
864{
865 struct irq_desc *desc = irq_to_desc(irq);
866
867 if (!desc || !desc->percpu_enabled)
868 return -EINVAL;
869
870 if (affinity)
871 cpumask_copy(affinity, desc->percpu_affinity);
872
873 return 0;
874}
875
792d0018
TG
876void kstat_incr_irq_this_cpu(unsigned int irq)
877{
b51bf95c 878 kstat_incr_irqs_this_cpu(irq_to_desc(irq));
792d0018
TG
879}
880
c291ee62
TG
881/**
882 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
883 * @irq: The interrupt number
884 * @cpu: The cpu number
885 *
886 * Returns the sum of interrupt counts on @cpu since boot for
887 * @irq. The caller must ensure that the interrupt is not removed
888 * concurrently.
889 */
3795de23
TG
890unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
891{
892 struct irq_desc *desc = irq_to_desc(irq);
6c9ae009
ED
893
894 return desc && desc->kstat_irqs ?
895 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
3795de23 896}
478735e3 897
c291ee62
TG
898/**
899 * kstat_irqs - Get the statistics for an interrupt
900 * @irq: The interrupt number
901 *
902 * Returns the sum of interrupt counts on all cpus since boot for
903 * @irq. The caller must ensure that the interrupt is not removed
904 * concurrently.
905 */
478735e3
KH
906unsigned int kstat_irqs(unsigned int irq)
907{
908 struct irq_desc *desc = irq_to_desc(irq);
909 int cpu;
5e9662fa 910 unsigned int sum = 0;
478735e3 911
6c9ae009 912 if (!desc || !desc->kstat_irqs)
478735e3
KH
913 return 0;
914 for_each_possible_cpu(cpu)
6c9ae009 915 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
478735e3
KH
916 return sum;
917}
c291ee62
TG
918
919/**
920 * kstat_irqs_usr - Get the statistics for an interrupt
921 * @irq: The interrupt number
922 *
923 * Returns the sum of interrupt counts on all cpus since boot for
924 * @irq. Contrary to kstat_irqs() this can be called from any
925 * preemptible context. It's protected against concurrent removal of
926 * an interrupt descriptor when sparse irqs are enabled.
927 */
928unsigned int kstat_irqs_usr(unsigned int irq)
929{
7df0b278 930 unsigned int sum;
c291ee62
TG
931
932 irq_lock_sparse();
933 sum = kstat_irqs(irq);
934 irq_unlock_sparse();
935 return sum;
936}