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
95 #else
96 static inline int
alloc_masks(struct irq_desc * desc,int node)97 alloc_masks(struct irq_desc *desc, int node) { return 0; }
98 static inline void
desc_smp_init(struct irq_desc * desc,int node,const struct cpumask * affinity)99 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
100 #endif
101
desc_set_defaults(unsigned int irq,struct irq_desc * desc,int node,const struct cpumask * affinity,struct module * owner)102 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
103 const struct cpumask *affinity, struct module *owner)
104 {
105 int cpu;
106
107 desc->irq_common_data.handler_data = NULL;
108 desc->irq_common_data.msi_desc = NULL;
109
110 desc->irq_data.common = &desc->irq_common_data;
111 desc->irq_data.irq = irq;
112 desc->irq_data.chip = &no_irq_chip;
113 desc->irq_data.chip_data = NULL;
114 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
115 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
116 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
117 desc->handle_irq = handle_bad_irq;
118 desc->depth = 1;
119 desc->irq_count = 0;
120 desc->irqs_unhandled = 0;
121 desc->tot_count = 0;
122 desc->name = NULL;
123 desc->owner = owner;
124 for_each_possible_cpu(cpu)
125 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
126 desc_smp_init(desc, node, affinity);
127 }
128
129 int nr_irqs = NR_IRQS;
130 EXPORT_SYMBOL_GPL(nr_irqs);
131
132 static DEFINE_MUTEX(sparse_irq_lock);
133 static struct maple_tree sparse_irqs = MTREE_INIT_EXT(sparse_irqs,
134 MT_FLAGS_ALLOC_RANGE |
135 MT_FLAGS_LOCK_EXTERN |
136 MT_FLAGS_USE_RCU,
137 sparse_irq_lock);
138
irq_find_free_area(unsigned int from,unsigned int cnt)139 static int irq_find_free_area(unsigned int from, unsigned int cnt)
140 {
141 MA_STATE(mas, &sparse_irqs, 0, 0);
142
143 if (mas_empty_area(&mas, from, MAX_SPARSE_IRQS, cnt))
144 return -ENOSPC;
145 return mas.index;
146 }
147
irq_find_at_or_after(unsigned int offset)148 static unsigned int irq_find_at_or_after(unsigned int offset)
149 {
150 unsigned long index = offset;
151 struct irq_desc *desc;
152
153 guard(rcu)();
154 desc = mt_find(&sparse_irqs, &index, nr_irqs);
155
156 return desc ? irq_desc_get_irq(desc) : nr_irqs;
157 }
158
irq_insert_desc(unsigned int irq,struct irq_desc * desc)159 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
160 {
161 MA_STATE(mas, &sparse_irqs, irq, irq);
162 WARN_ON(mas_store_gfp(&mas, desc, GFP_KERNEL) != 0);
163 }
164
delete_irq_desc(unsigned int irq)165 static void delete_irq_desc(unsigned int irq)
166 {
167 MA_STATE(mas, &sparse_irqs, irq, irq);
168 mas_erase(&mas);
169 }
170
171 #ifdef CONFIG_SPARSE_IRQ
172
173 static void irq_kobj_release(struct kobject *kobj);
174
175 #ifdef CONFIG_SYSFS
176 static struct kobject *irq_kobj_base;
177
178 #define IRQ_ATTR_RO(_name) \
179 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
180
per_cpu_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)181 static ssize_t per_cpu_count_show(struct kobject *kobj,
182 struct kobj_attribute *attr, char *buf)
183 {
184 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
185 ssize_t ret = 0;
186 char *p = "";
187 int cpu;
188
189 for_each_possible_cpu(cpu) {
190 unsigned int c = irq_desc_kstat_cpu(desc, cpu);
191
192 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
193 p = ",";
194 }
195
196 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
197 return ret;
198 }
199 IRQ_ATTR_RO(per_cpu_count);
200
chip_name_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)201 static ssize_t chip_name_show(struct kobject *kobj,
202 struct kobj_attribute *attr, char *buf)
203 {
204 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
205 ssize_t ret = 0;
206
207 raw_spin_lock_irq(&desc->lock);
208 if (desc->irq_data.chip && desc->irq_data.chip->name) {
209 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
210 desc->irq_data.chip->name);
211 }
212 raw_spin_unlock_irq(&desc->lock);
213
214 return ret;
215 }
216 IRQ_ATTR_RO(chip_name);
217
hwirq_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)218 static ssize_t hwirq_show(struct kobject *kobj,
219 struct kobj_attribute *attr, char *buf)
220 {
221 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
222 ssize_t ret = 0;
223
224 raw_spin_lock_irq(&desc->lock);
225 if (desc->irq_data.domain)
226 ret = sprintf(buf, "%lu\n", desc->irq_data.hwirq);
227 raw_spin_unlock_irq(&desc->lock);
228
229 return ret;
230 }
231 IRQ_ATTR_RO(hwirq);
232
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)233 static ssize_t type_show(struct kobject *kobj,
234 struct kobj_attribute *attr, char *buf)
235 {
236 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
237 ssize_t ret = 0;
238
239 raw_spin_lock_irq(&desc->lock);
240 ret = sprintf(buf, "%s\n",
241 irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
242 raw_spin_unlock_irq(&desc->lock);
243
244 return ret;
245
246 }
247 IRQ_ATTR_RO(type);
248
wakeup_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)249 static ssize_t wakeup_show(struct kobject *kobj,
250 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
255 raw_spin_lock_irq(&desc->lock);
256 ret = sprintf(buf, "%s\n",
257 irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
258 raw_spin_unlock_irq(&desc->lock);
259
260 return ret;
261
262 }
263 IRQ_ATTR_RO(wakeup);
264
name_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)265 static ssize_t name_show(struct kobject *kobj,
266 struct kobj_attribute *attr, char *buf)
267 {
268 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
269 ssize_t ret = 0;
270
271 raw_spin_lock_irq(&desc->lock);
272 if (desc->name)
273 ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
274 raw_spin_unlock_irq(&desc->lock);
275
276 return ret;
277 }
278 IRQ_ATTR_RO(name);
279
actions_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)280 static ssize_t actions_show(struct kobject *kobj,
281 struct kobj_attribute *attr, char *buf)
282 {
283 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
284 struct irqaction *action;
285 ssize_t ret = 0;
286 char *p = "";
287
288 raw_spin_lock_irq(&desc->lock);
289 for_each_action_of_desc(desc, action) {
290 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
291 p, action->name);
292 p = ",";
293 }
294 raw_spin_unlock_irq(&desc->lock);
295
296 if (ret)
297 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
298
299 return ret;
300 }
301 IRQ_ATTR_RO(actions);
302
303 static struct attribute *irq_attrs[] = {
304 &per_cpu_count_attr.attr,
305 &chip_name_attr.attr,
306 &hwirq_attr.attr,
307 &type_attr.attr,
308 &wakeup_attr.attr,
309 &name_attr.attr,
310 &actions_attr.attr,
311 NULL
312 };
313 ATTRIBUTE_GROUPS(irq);
314
315 static const struct kobj_type irq_kobj_type = {
316 .release = irq_kobj_release,
317 .sysfs_ops = &kobj_sysfs_ops,
318 .default_groups = irq_groups,
319 };
320
irq_sysfs_add(int irq,struct irq_desc * desc)321 static void irq_sysfs_add(int irq, struct irq_desc *desc)
322 {
323 if (irq_kobj_base) {
324 /*
325 * Continue even in case of failure as this is nothing
326 * crucial and failures in the late irq_sysfs_init()
327 * cannot be rolled back.
328 */
329 if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
330 pr_warn("Failed to add kobject for irq %d\n", irq);
331 else
332 desc->istate |= IRQS_SYSFS;
333 }
334 }
335
irq_sysfs_del(struct irq_desc * desc)336 static void irq_sysfs_del(struct irq_desc *desc)
337 {
338 /*
339 * Only invoke kobject_del() when kobject_add() was successfully
340 * invoked for the descriptor. This covers both early boot, where
341 * sysfs is not initialized yet, and the case of a failed
342 * kobject_add() invocation.
343 */
344 if (desc->istate & IRQS_SYSFS)
345 kobject_del(&desc->kobj);
346 }
347
irq_sysfs_init(void)348 static int __init irq_sysfs_init(void)
349 {
350 struct irq_desc *desc;
351 int irq;
352
353 /* Prevent concurrent irq alloc/free */
354 irq_lock_sparse();
355
356 irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
357 if (!irq_kobj_base) {
358 irq_unlock_sparse();
359 return -ENOMEM;
360 }
361
362 /* Add the already allocated interrupts */
363 for_each_irq_desc(irq, desc)
364 irq_sysfs_add(irq, desc);
365 irq_unlock_sparse();
366
367 return 0;
368 }
369 postcore_initcall(irq_sysfs_init);
370
371 #else /* !CONFIG_SYSFS */
372
373 static const struct kobj_type irq_kobj_type = {
374 .release = irq_kobj_release,
375 };
376
irq_sysfs_add(int irq,struct irq_desc * desc)377 static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
irq_sysfs_del(struct irq_desc * desc)378 static void irq_sysfs_del(struct irq_desc *desc) {}
379
380 #endif /* CONFIG_SYSFS */
381
irq_to_desc(unsigned int irq)382 struct irq_desc *irq_to_desc(unsigned int irq)
383 {
384 return mtree_load(&sparse_irqs, irq);
385 }
386 #ifdef CONFIG_KVM_BOOK3S_64_HV_MODULE
387 EXPORT_SYMBOL_GPL(irq_to_desc);
388 #endif
389
390 #ifdef CONFIG_SMP
free_masks(struct irq_desc * desc)391 static void free_masks(struct irq_desc *desc)
392 {
393 #ifdef CONFIG_GENERIC_PENDING_IRQ
394 free_cpumask_var(desc->pending_mask);
395 #endif
396 free_cpumask_var(desc->irq_common_data.affinity);
397 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
398 free_cpumask_var(desc->irq_common_data.effective_affinity);
399 #endif
400 }
401 #else
free_masks(struct irq_desc * desc)402 static inline void free_masks(struct irq_desc *desc) { }
403 #endif
404
irq_lock_sparse(void)405 void irq_lock_sparse(void)
406 {
407 mutex_lock(&sparse_irq_lock);
408 }
409
irq_unlock_sparse(void)410 void irq_unlock_sparse(void)
411 {
412 mutex_unlock(&sparse_irq_lock);
413 }
414
alloc_desc(int irq,int node,unsigned int flags,const struct cpumask * affinity,struct module * owner)415 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
416 const struct cpumask *affinity,
417 struct module *owner)
418 {
419 struct irq_desc *desc;
420
421 desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
422 if (!desc)
423 return NULL;
424 /* allocate based on nr_cpu_ids */
425 desc->kstat_irqs = alloc_percpu(unsigned int);
426 if (!desc->kstat_irqs)
427 goto err_desc;
428
429 if (alloc_masks(desc, node))
430 goto err_kstat;
431
432 raw_spin_lock_init(&desc->lock);
433 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
434 mutex_init(&desc->request_mutex);
435 init_rcu_head(&desc->rcu);
436 init_waitqueue_head(&desc->wait_for_threads);
437
438 desc_set_defaults(irq, desc, node, affinity, owner);
439 irqd_set(&desc->irq_data, flags);
440 kobject_init(&desc->kobj, &irq_kobj_type);
441 irq_resend_init(desc);
442
443 return desc;
444
445 err_kstat:
446 free_percpu(desc->kstat_irqs);
447 err_desc:
448 kfree(desc);
449 return NULL;
450 }
451
irq_kobj_release(struct kobject * kobj)452 static void irq_kobj_release(struct kobject *kobj)
453 {
454 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
455
456 free_masks(desc);
457 free_percpu(desc->kstat_irqs);
458 kfree(desc);
459 }
460
delayed_free_desc(struct rcu_head * rhp)461 static void delayed_free_desc(struct rcu_head *rhp)
462 {
463 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
464
465 kobject_put(&desc->kobj);
466 }
467
free_desc(unsigned int irq)468 static void free_desc(unsigned int irq)
469 {
470 struct irq_desc *desc = irq_to_desc(irq);
471
472 irq_remove_debugfs_entry(desc);
473 unregister_irq_proc(irq, desc);
474
475 /*
476 * sparse_irq_lock protects also show_interrupts() and
477 * kstat_irq_usr(). Once we deleted the descriptor from the
478 * sparse tree we can free it. Access in proc will fail to
479 * lookup the descriptor.
480 *
481 * The sysfs entry must be serialized against a concurrent
482 * irq_sysfs_init() as well.
483 */
484 irq_sysfs_del(desc);
485 delete_irq_desc(irq);
486
487 /*
488 * We free the descriptor, masks and stat fields via RCU. That
489 * allows demultiplex interrupts to do rcu based management of
490 * the child interrupts.
491 * This also allows us to use rcu in kstat_irqs_usr().
492 */
493 call_rcu(&desc->rcu, delayed_free_desc);
494 }
495
alloc_descs(unsigned int start,unsigned int cnt,int node,const struct irq_affinity_desc * affinity,struct module * owner)496 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
497 const struct irq_affinity_desc *affinity,
498 struct module *owner)
499 {
500 struct irq_desc *desc;
501 int i;
502
503 /* Validate affinity mask(s) */
504 if (affinity) {
505 for (i = 0; i < cnt; i++) {
506 if (cpumask_empty(&affinity[i].mask))
507 return -EINVAL;
508 }
509 }
510
511 for (i = 0; i < cnt; i++) {
512 const struct cpumask *mask = NULL;
513 unsigned int flags = 0;
514
515 if (affinity) {
516 if (affinity->is_managed) {
517 flags = IRQD_AFFINITY_MANAGED |
518 IRQD_MANAGED_SHUTDOWN;
519 }
520 mask = &affinity->mask;
521 node = cpu_to_node(cpumask_first(mask));
522 affinity++;
523 }
524
525 desc = alloc_desc(start + i, node, flags, mask, owner);
526 if (!desc)
527 goto err;
528 irq_insert_desc(start + i, desc);
529 irq_sysfs_add(start + i, desc);
530 irq_add_debugfs_entry(start + i, desc);
531 }
532 return start;
533
534 err:
535 for (i--; i >= 0; i--)
536 free_desc(start + i);
537 return -ENOMEM;
538 }
539
irq_expand_nr_irqs(unsigned int nr)540 static int irq_expand_nr_irqs(unsigned int nr)
541 {
542 if (nr > MAX_SPARSE_IRQS)
543 return -ENOMEM;
544 nr_irqs = nr;
545 return 0;
546 }
547
early_irq_init(void)548 int __init early_irq_init(void)
549 {
550 int i, initcnt, node = first_online_node;
551 struct irq_desc *desc;
552
553 init_irq_default_affinity();
554
555 /* Let arch update nr_irqs and return the nr of preallocated irqs */
556 initcnt = arch_probe_nr_irqs();
557 printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
558 NR_IRQS, nr_irqs, initcnt);
559
560 if (WARN_ON(nr_irqs > MAX_SPARSE_IRQS))
561 nr_irqs = MAX_SPARSE_IRQS;
562
563 if (WARN_ON(initcnt > MAX_SPARSE_IRQS))
564 initcnt = MAX_SPARSE_IRQS;
565
566 if (initcnt > nr_irqs)
567 nr_irqs = initcnt;
568
569 for (i = 0; i < initcnt; i++) {
570 desc = alloc_desc(i, node, 0, NULL, NULL);
571 irq_insert_desc(i, desc);
572 }
573 return arch_early_irq_init();
574 }
575
576 #else /* !CONFIG_SPARSE_IRQ */
577
578 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
579 [0 ... NR_IRQS-1] = {
580 .handle_irq = handle_bad_irq,
581 .depth = 1,
582 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
583 }
584 };
585
early_irq_init(void)586 int __init early_irq_init(void)
587 {
588 int count, i, node = first_online_node;
589 struct irq_desc *desc;
590
591 init_irq_default_affinity();
592
593 printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
594
595 desc = irq_desc;
596 count = ARRAY_SIZE(irq_desc);
597
598 for (i = 0; i < count; i++) {
599 desc[i].kstat_irqs = alloc_percpu(unsigned int);
600 alloc_masks(&desc[i], node);
601 raw_spin_lock_init(&desc[i].lock);
602 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
603 mutex_init(&desc[i].request_mutex);
604 init_waitqueue_head(&desc[i].wait_for_threads);
605 desc_set_defaults(i, &desc[i], node, NULL, NULL);
606 irq_resend_init(&desc[i]);
607 }
608 return arch_early_irq_init();
609 }
610
irq_to_desc(unsigned int irq)611 struct irq_desc *irq_to_desc(unsigned int irq)
612 {
613 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
614 }
615 EXPORT_SYMBOL(irq_to_desc);
616
free_desc(unsigned int irq)617 static void free_desc(unsigned int irq)
618 {
619 struct irq_desc *desc = irq_to_desc(irq);
620 unsigned long flags;
621
622 raw_spin_lock_irqsave(&desc->lock, flags);
623 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
624 raw_spin_unlock_irqrestore(&desc->lock, flags);
625 delete_irq_desc(irq);
626 }
627
alloc_descs(unsigned int start,unsigned int cnt,int node,const struct irq_affinity_desc * affinity,struct module * owner)628 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
629 const struct irq_affinity_desc *affinity,
630 struct module *owner)
631 {
632 u32 i;
633
634 for (i = 0; i < cnt; i++) {
635 struct irq_desc *desc = irq_to_desc(start + i);
636
637 desc->owner = owner;
638 irq_insert_desc(start + i, desc);
639 }
640 return start;
641 }
642
irq_expand_nr_irqs(unsigned int nr)643 static int irq_expand_nr_irqs(unsigned int nr)
644 {
645 return -ENOMEM;
646 }
647
irq_mark_irq(unsigned int irq)648 void irq_mark_irq(unsigned int irq)
649 {
650 mutex_lock(&sparse_irq_lock);
651 irq_insert_desc(irq, irq_desc + irq);
652 mutex_unlock(&sparse_irq_lock);
653 }
654
655 #ifdef CONFIG_GENERIC_IRQ_LEGACY
irq_init_desc(unsigned int irq)656 void irq_init_desc(unsigned int irq)
657 {
658 free_desc(irq);
659 }
660 #endif
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() && 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,unsigned int hwirq)729 int generic_handle_domain_irq(struct irq_domain *domain, unsigned int 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,unsigned int hwirq)747 int generic_handle_domain_irq_safe(struct irq_domain *domain, unsigned int 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,unsigned int hwirq)770 int generic_handle_domain_nmi(struct irq_domain *domain, unsigned int hwirq)
771 {
772 WARN_ON_ONCE(!in_nmi());
773 return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
774 }
775 #endif
776
777 /* Dynamic interrupt handling */
778
779 /**
780 * irq_free_descs - free irq descriptors
781 * @from: Start of descriptor range
782 * @cnt: Number of consecutive irqs to free
783 */
irq_free_descs(unsigned int from,unsigned int cnt)784 void irq_free_descs(unsigned int from, unsigned int cnt)
785 {
786 int i;
787
788 if (from >= nr_irqs || (from + cnt) > nr_irqs)
789 return;
790
791 mutex_lock(&sparse_irq_lock);
792 for (i = 0; i < cnt; i++)
793 free_desc(from + i);
794
795 mutex_unlock(&sparse_irq_lock);
796 }
797 EXPORT_SYMBOL_GPL(irq_free_descs);
798
799 /**
800 * __irq_alloc_descs - allocate and initialize a range of irq descriptors
801 * @irq: Allocate for specific irq number if irq >= 0
802 * @from: Start the search from this irq number
803 * @cnt: Number of consecutive irqs to allocate.
804 * @node: Preferred node on which the irq descriptor should be allocated
805 * @owner: Owning module (can be NULL)
806 * @affinity: Optional pointer to an affinity mask array of size @cnt which
807 * hints where the irq descriptors should be allocated and which
808 * default affinities to use
809 *
810 * Returns the first irq number or error code
811 */
812 int __ref
__irq_alloc_descs(int irq,unsigned int from,unsigned int cnt,int node,struct module * owner,const struct irq_affinity_desc * affinity)813 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
814 struct module *owner, const struct irq_affinity_desc *affinity)
815 {
816 int start, ret;
817
818 if (!cnt)
819 return -EINVAL;
820
821 if (irq >= 0) {
822 if (from > irq)
823 return -EINVAL;
824 from = irq;
825 } else {
826 /*
827 * For interrupts which are freely allocated the
828 * architecture can force a lower bound to the @from
829 * argument. x86 uses this to exclude the GSI space.
830 */
831 from = arch_dynirq_lower_bound(from);
832 }
833
834 mutex_lock(&sparse_irq_lock);
835
836 start = irq_find_free_area(from, cnt);
837 ret = -EEXIST;
838 if (irq >=0 && start != irq)
839 goto unlock;
840
841 if (start + cnt > nr_irqs) {
842 ret = irq_expand_nr_irqs(start + cnt);
843 if (ret)
844 goto unlock;
845 }
846 ret = alloc_descs(start, cnt, node, affinity, owner);
847 unlock:
848 mutex_unlock(&sparse_irq_lock);
849 return ret;
850 }
851 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
852
853 /**
854 * irq_get_next_irq - get next allocated irq number
855 * @offset: where to start the search
856 *
857 * Returns next irq number after offset or nr_irqs if none is found.
858 */
irq_get_next_irq(unsigned int offset)859 unsigned int irq_get_next_irq(unsigned int offset)
860 {
861 return irq_find_at_or_after(offset);
862 }
863
864 struct irq_desc *
__irq_get_desc_lock(unsigned int irq,unsigned long * flags,bool bus,unsigned int check)865 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
866 unsigned int check)
867 {
868 struct irq_desc *desc = irq_to_desc(irq);
869
870 if (desc) {
871 if (check & _IRQ_DESC_CHECK) {
872 if ((check & _IRQ_DESC_PERCPU) &&
873 !irq_settings_is_per_cpu_devid(desc))
874 return NULL;
875
876 if (!(check & _IRQ_DESC_PERCPU) &&
877 irq_settings_is_per_cpu_devid(desc))
878 return NULL;
879 }
880
881 if (bus)
882 chip_bus_lock(desc);
883 raw_spin_lock_irqsave(&desc->lock, *flags);
884 }
885 return desc;
886 }
887
__irq_put_desc_unlock(struct irq_desc * desc,unsigned long flags,bool bus)888 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
889 __releases(&desc->lock)
890 {
891 raw_spin_unlock_irqrestore(&desc->lock, flags);
892 if (bus)
893 chip_bus_sync_unlock(desc);
894 }
895
irq_set_percpu_devid_partition(unsigned int irq,const struct cpumask * affinity)896 int irq_set_percpu_devid_partition(unsigned int irq,
897 const struct cpumask *affinity)
898 {
899 struct irq_desc *desc = irq_to_desc(irq);
900
901 if (!desc)
902 return -EINVAL;
903
904 if (desc->percpu_enabled)
905 return -EINVAL;
906
907 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
908
909 if (!desc->percpu_enabled)
910 return -ENOMEM;
911
912 if (affinity)
913 desc->percpu_affinity = affinity;
914 else
915 desc->percpu_affinity = cpu_possible_mask;
916
917 irq_set_percpu_devid_flags(irq);
918 return 0;
919 }
920
irq_set_percpu_devid(unsigned int irq)921 int irq_set_percpu_devid(unsigned int irq)
922 {
923 return irq_set_percpu_devid_partition(irq, NULL);
924 }
925
irq_get_percpu_devid_partition(unsigned int irq,struct cpumask * affinity)926 int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
927 {
928 struct irq_desc *desc = irq_to_desc(irq);
929
930 if (!desc || !desc->percpu_enabled)
931 return -EINVAL;
932
933 if (affinity)
934 cpumask_copy(affinity, desc->percpu_affinity);
935
936 return 0;
937 }
938 EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
939
kstat_incr_irq_this_cpu(unsigned int irq)940 void kstat_incr_irq_this_cpu(unsigned int irq)
941 {
942 kstat_incr_irqs_this_cpu(irq_to_desc(irq));
943 }
944
945 /**
946 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
947 * @irq: The interrupt number
948 * @cpu: The cpu number
949 *
950 * Returns the sum of interrupt counts on @cpu since boot for
951 * @irq. The caller must ensure that the interrupt is not removed
952 * concurrently.
953 */
kstat_irqs_cpu(unsigned int irq,int cpu)954 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
955 {
956 struct irq_desc *desc = irq_to_desc(irq);
957
958 return desc && desc->kstat_irqs ?
959 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
960 }
961
irq_is_nmi(struct irq_desc * desc)962 static bool irq_is_nmi(struct irq_desc *desc)
963 {
964 return desc->istate & IRQS_NMI;
965 }
966
kstat_irqs(unsigned int irq)967 static unsigned int kstat_irqs(unsigned int irq)
968 {
969 struct irq_desc *desc = irq_to_desc(irq);
970 unsigned int sum = 0;
971 int cpu;
972
973 if (!desc || !desc->kstat_irqs)
974 return 0;
975 if (!irq_settings_is_per_cpu_devid(desc) &&
976 !irq_settings_is_per_cpu(desc) &&
977 !irq_is_nmi(desc))
978 return data_race(desc->tot_count);
979
980 for_each_possible_cpu(cpu)
981 sum += data_race(*per_cpu_ptr(desc->kstat_irqs, cpu));
982 return sum;
983 }
984
985 /**
986 * kstat_irqs_usr - Get the statistics for an interrupt from thread context
987 * @irq: The interrupt number
988 *
989 * Returns the sum of interrupt counts on all cpus since boot for @irq.
990 *
991 * It uses rcu to protect the access since a concurrent removal of an
992 * interrupt descriptor is observing an rcu grace period before
993 * delayed_free_desc()/irq_kobj_release().
994 */
kstat_irqs_usr(unsigned int irq)995 unsigned int kstat_irqs_usr(unsigned int irq)
996 {
997 unsigned int sum;
998
999 rcu_read_lock();
1000 sum = kstat_irqs(irq);
1001 rcu_read_unlock();
1002 return sum;
1003 }
1004
1005 #ifdef CONFIG_LOCKDEP
__irq_set_lockdep_class(unsigned int irq,struct lock_class_key * lock_class,struct lock_class_key * request_class)1006 void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class,
1007 struct lock_class_key *request_class)
1008 {
1009 struct irq_desc *desc = irq_to_desc(irq);
1010
1011 if (desc) {
1012 lockdep_set_class(&desc->lock, lock_class);
1013 lockdep_set_class(&desc->request_mutex, request_class);
1014 }
1015 }
1016 EXPORT_SYMBOL_GPL(__irq_set_lockdep_class);
1017 #endif
1018