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