These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / kernel / irq / irqdesc.c
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>
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
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)
27 static int __init irq_affinity_setup(char *str)
28 {
29         zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
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 comandline masks
34          */
35         cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
36         return 1;
37 }
38 __setup("irqaffinity=", irq_affinity_setup);
39
40 static void __init init_irq_default_affinity(void)
41 {
42 #ifdef CONFIG_CPUMASK_OFFSTACK
43         if (!irq_default_affinity)
44                 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
45 #endif
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, gfp_t gfp, int node)
57 {
58         if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
59                                      gfp, node))
60                 return -ENOMEM;
61
62 #ifdef CONFIG_GENERIC_PENDING_IRQ
63         if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
64                 free_cpumask_var(desc->irq_common_data.affinity);
65                 return -ENOMEM;
66         }
67 #endif
68         return 0;
69 }
70
71 static void desc_smp_init(struct irq_desc *desc, int node)
72 {
73         cpumask_copy(desc->irq_common_data.affinity, irq_default_affinity);
74 #ifdef CONFIG_GENERIC_PENDING_IRQ
75         cpumask_clear(desc->pending_mask);
76 #endif
77 #ifdef CONFIG_NUMA
78         desc->irq_common_data.node = node;
79 #endif
80 }
81
82 #else
83 static inline int
84 alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
85 static inline void desc_smp_init(struct irq_desc *desc, int node) { }
86 #endif
87
88 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
89                 struct module *owner)
90 {
91         int cpu;
92
93         desc->irq_common_data.handler_data = NULL;
94         desc->irq_common_data.msi_desc = NULL;
95
96         desc->irq_data.common = &desc->irq_common_data;
97         desc->irq_data.irq = irq;
98         desc->irq_data.chip = &no_irq_chip;
99         desc->irq_data.chip_data = NULL;
100         irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
101         irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
102         desc->handle_irq = handle_bad_irq;
103         desc->depth = 1;
104         desc->irq_count = 0;
105         desc->irqs_unhandled = 0;
106         desc->name = NULL;
107         desc->owner = owner;
108         for_each_possible_cpu(cpu)
109                 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
110         desc_smp_init(desc, node);
111 }
112
113 int nr_irqs = NR_IRQS;
114 EXPORT_SYMBOL_GPL(nr_irqs);
115
116 static DEFINE_MUTEX(sparse_irq_lock);
117 static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
118
119 #ifdef CONFIG_SPARSE_IRQ
120
121 static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
122
123 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
124 {
125         radix_tree_insert(&irq_desc_tree, irq, desc);
126 }
127
128 struct irq_desc *irq_to_desc(unsigned int irq)
129 {
130         return radix_tree_lookup(&irq_desc_tree, irq);
131 }
132 EXPORT_SYMBOL(irq_to_desc);
133
134 static void delete_irq_desc(unsigned int irq)
135 {
136         radix_tree_delete(&irq_desc_tree, irq);
137 }
138
139 #ifdef CONFIG_SMP
140 static void free_masks(struct irq_desc *desc)
141 {
142 #ifdef CONFIG_GENERIC_PENDING_IRQ
143         free_cpumask_var(desc->pending_mask);
144 #endif
145         free_cpumask_var(desc->irq_common_data.affinity);
146 }
147 #else
148 static inline void free_masks(struct irq_desc *desc) { }
149 #endif
150
151 void irq_lock_sparse(void)
152 {
153         mutex_lock(&sparse_irq_lock);
154 }
155
156 void irq_unlock_sparse(void)
157 {
158         mutex_unlock(&sparse_irq_lock);
159 }
160
161 static struct irq_desc *alloc_desc(int irq, int node, struct module *owner)
162 {
163         struct irq_desc *desc;
164         gfp_t gfp = GFP_KERNEL;
165
166         desc = kzalloc_node(sizeof(*desc), gfp, node);
167         if (!desc)
168                 return NULL;
169         /* allocate based on nr_cpu_ids */
170         desc->kstat_irqs = alloc_percpu(unsigned int);
171         if (!desc->kstat_irqs)
172                 goto err_desc;
173
174         if (alloc_masks(desc, gfp, node))
175                 goto err_kstat;
176
177         raw_spin_lock_init(&desc->lock);
178         lockdep_set_class(&desc->lock, &irq_desc_lock_class);
179
180         desc_set_defaults(irq, desc, node, owner);
181
182         return desc;
183
184 err_kstat:
185         free_percpu(desc->kstat_irqs);
186 err_desc:
187         kfree(desc);
188         return NULL;
189 }
190
191 static void free_desc(unsigned int irq)
192 {
193         struct irq_desc *desc = irq_to_desc(irq);
194
195         unregister_irq_proc(irq, desc);
196
197         /*
198          * sparse_irq_lock protects also show_interrupts() and
199          * kstat_irq_usr(). Once we deleted the descriptor from the
200          * sparse tree we can free it. Access in proc will fail to
201          * lookup the descriptor.
202          */
203         mutex_lock(&sparse_irq_lock);
204         delete_irq_desc(irq);
205         mutex_unlock(&sparse_irq_lock);
206
207         free_masks(desc);
208         free_percpu(desc->kstat_irqs);
209         kfree(desc);
210 }
211
212 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
213                        struct module *owner)
214 {
215         struct irq_desc *desc;
216         int i;
217
218         for (i = 0; i < cnt; i++) {
219                 desc = alloc_desc(start + i, node, owner);
220                 if (!desc)
221                         goto err;
222                 mutex_lock(&sparse_irq_lock);
223                 irq_insert_desc(start + i, desc);
224                 mutex_unlock(&sparse_irq_lock);
225         }
226         return start;
227
228 err:
229         for (i--; i >= 0; i--)
230                 free_desc(start + i);
231
232         mutex_lock(&sparse_irq_lock);
233         bitmap_clear(allocated_irqs, start, cnt);
234         mutex_unlock(&sparse_irq_lock);
235         return -ENOMEM;
236 }
237
238 static int irq_expand_nr_irqs(unsigned int nr)
239 {
240         if (nr > IRQ_BITMAP_BITS)
241                 return -ENOMEM;
242         nr_irqs = nr;
243         return 0;
244 }
245
246 int __init early_irq_init(void)
247 {
248         int i, initcnt, node = first_online_node;
249         struct irq_desc *desc;
250
251         init_irq_default_affinity();
252
253         /* Let arch update nr_irqs and return the nr of preallocated irqs */
254         initcnt = arch_probe_nr_irqs();
255         printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
256
257         if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
258                 nr_irqs = IRQ_BITMAP_BITS;
259
260         if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
261                 initcnt = IRQ_BITMAP_BITS;
262
263         if (initcnt > nr_irqs)
264                 nr_irqs = initcnt;
265
266         for (i = 0; i < initcnt; i++) {
267                 desc = alloc_desc(i, node, NULL);
268                 set_bit(i, allocated_irqs);
269                 irq_insert_desc(i, desc);
270         }
271         return arch_early_irq_init();
272 }
273
274 #else /* !CONFIG_SPARSE_IRQ */
275
276 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
277         [0 ... NR_IRQS-1] = {
278                 .handle_irq     = handle_bad_irq,
279                 .depth          = 1,
280                 .lock           = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
281         }
282 };
283
284 int __init early_irq_init(void)
285 {
286         int count, i, node = first_online_node;
287         struct irq_desc *desc;
288
289         init_irq_default_affinity();
290
291         printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
292
293         desc = irq_desc;
294         count = ARRAY_SIZE(irq_desc);
295
296         for (i = 0; i < count; i++) {
297                 desc[i].kstat_irqs = alloc_percpu(unsigned int);
298                 alloc_masks(&desc[i], GFP_KERNEL, node);
299                 raw_spin_lock_init(&desc[i].lock);
300                 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
301                 desc_set_defaults(i, &desc[i], node, NULL);
302         }
303         return arch_early_irq_init();
304 }
305
306 struct irq_desc *irq_to_desc(unsigned int irq)
307 {
308         return (irq < NR_IRQS) ? irq_desc + irq : NULL;
309 }
310 EXPORT_SYMBOL(irq_to_desc);
311
312 static void free_desc(unsigned int irq)
313 {
314         struct irq_desc *desc = irq_to_desc(irq);
315         unsigned long flags;
316
317         raw_spin_lock_irqsave(&desc->lock, flags);
318         desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL);
319         raw_spin_unlock_irqrestore(&desc->lock, flags);
320 }
321
322 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
323                               struct module *owner)
324 {
325         u32 i;
326
327         for (i = 0; i < cnt; i++) {
328                 struct irq_desc *desc = irq_to_desc(start + i);
329
330                 desc->owner = owner;
331         }
332         return start;
333 }
334
335 static int irq_expand_nr_irqs(unsigned int nr)
336 {
337         return -ENOMEM;
338 }
339
340 void irq_mark_irq(unsigned int irq)
341 {
342         mutex_lock(&sparse_irq_lock);
343         bitmap_set(allocated_irqs, irq, 1);
344         mutex_unlock(&sparse_irq_lock);
345 }
346
347 #ifdef CONFIG_GENERIC_IRQ_LEGACY
348 void irq_init_desc(unsigned int irq)
349 {
350         free_desc(irq);
351 }
352 #endif
353
354 #endif /* !CONFIG_SPARSE_IRQ */
355
356 /**
357  * generic_handle_irq - Invoke the handler for a particular irq
358  * @irq:        The irq number to handle
359  *
360  */
361 int generic_handle_irq(unsigned int irq)
362 {
363         struct irq_desc *desc = irq_to_desc(irq);
364
365         if (!desc)
366                 return -EINVAL;
367         generic_handle_irq_desc(desc);
368         return 0;
369 }
370 EXPORT_SYMBOL_GPL(generic_handle_irq);
371
372 #ifdef CONFIG_HANDLE_DOMAIN_IRQ
373 /**
374  * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
375  * @domain:     The domain where to perform the lookup
376  * @hwirq:      The HW irq number to convert to a logical one
377  * @lookup:     Whether to perform the domain lookup or not
378  * @regs:       Register file coming from the low-level handling code
379  *
380  * Returns:     0 on success, or -EINVAL if conversion has failed
381  */
382 int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
383                         bool lookup, struct pt_regs *regs)
384 {
385         struct pt_regs *old_regs = set_irq_regs(regs);
386         unsigned int irq = hwirq;
387         int ret = 0;
388
389         irq_enter();
390
391 #ifdef CONFIG_IRQ_DOMAIN
392         if (lookup)
393                 irq = irq_find_mapping(domain, hwirq);
394 #endif
395
396         /*
397          * Some hardware gives randomly wrong interrupts.  Rather
398          * than crashing, do something sensible.
399          */
400         if (unlikely(!irq || irq >= nr_irqs)) {
401                 ack_bad_irq(irq);
402                 ret = -EINVAL;
403         } else {
404                 generic_handle_irq(irq);
405         }
406
407         irq_exit();
408         set_irq_regs(old_regs);
409         return ret;
410 }
411 #endif
412
413 /* Dynamic interrupt handling */
414
415 /**
416  * irq_free_descs - free irq descriptors
417  * @from:       Start of descriptor range
418  * @cnt:        Number of consecutive irqs to free
419  */
420 void irq_free_descs(unsigned int from, unsigned int cnt)
421 {
422         int i;
423
424         if (from >= nr_irqs || (from + cnt) > nr_irqs)
425                 return;
426
427         for (i = 0; i < cnt; i++)
428                 free_desc(from + i);
429
430         mutex_lock(&sparse_irq_lock);
431         bitmap_clear(allocated_irqs, from, cnt);
432         mutex_unlock(&sparse_irq_lock);
433 }
434 EXPORT_SYMBOL_GPL(irq_free_descs);
435
436 /**
437  * irq_alloc_descs - allocate and initialize a range of irq descriptors
438  * @irq:        Allocate for specific irq number if irq >= 0
439  * @from:       Start the search from this irq number
440  * @cnt:        Number of consecutive irqs to allocate.
441  * @node:       Preferred node on which the irq descriptor should be allocated
442  * @owner:      Owning module (can be NULL)
443  *
444  * Returns the first irq number or error code
445  */
446 int __ref
447 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
448                   struct module *owner)
449 {
450         int start, ret;
451
452         if (!cnt)
453                 return -EINVAL;
454
455         if (irq >= 0) {
456                 if (from > irq)
457                         return -EINVAL;
458                 from = irq;
459         } else {
460                 /*
461                  * For interrupts which are freely allocated the
462                  * architecture can force a lower bound to the @from
463                  * argument. x86 uses this to exclude the GSI space.
464                  */
465                 from = arch_dynirq_lower_bound(from);
466         }
467
468         mutex_lock(&sparse_irq_lock);
469
470         start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
471                                            from, cnt, 0);
472         ret = -EEXIST;
473         if (irq >=0 && start != irq)
474                 goto err;
475
476         if (start + cnt > nr_irqs) {
477                 ret = irq_expand_nr_irqs(start + cnt);
478                 if (ret)
479                         goto err;
480         }
481
482         bitmap_set(allocated_irqs, start, cnt);
483         mutex_unlock(&sparse_irq_lock);
484         return alloc_descs(start, cnt, node, owner);
485
486 err:
487         mutex_unlock(&sparse_irq_lock);
488         return ret;
489 }
490 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
491
492 #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
493 /**
494  * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
495  * @cnt:        number of interrupts to allocate
496  * @node:       node on which to allocate
497  *
498  * Returns an interrupt number > 0 or 0, if the allocation fails.
499  */
500 unsigned int irq_alloc_hwirqs(int cnt, int node)
501 {
502         int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL);
503
504         if (irq < 0)
505                 return 0;
506
507         for (i = irq; cnt > 0; i++, cnt--) {
508                 if (arch_setup_hwirq(i, node))
509                         goto err;
510                 irq_clear_status_flags(i, _IRQ_NOREQUEST);
511         }
512         return irq;
513
514 err:
515         for (i--; i >= irq; i--) {
516                 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
517                 arch_teardown_hwirq(i);
518         }
519         irq_free_descs(irq, cnt);
520         return 0;
521 }
522 EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
523
524 /**
525  * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
526  * @from:       Free from irq number
527  * @cnt:        number of interrupts to free
528  *
529  */
530 void irq_free_hwirqs(unsigned int from, int cnt)
531 {
532         int i, j;
533
534         for (i = from, j = cnt; j > 0; i++, j--) {
535                 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
536                 arch_teardown_hwirq(i);
537         }
538         irq_free_descs(from, cnt);
539 }
540 EXPORT_SYMBOL_GPL(irq_free_hwirqs);
541 #endif
542
543 /**
544  * irq_get_next_irq - get next allocated irq number
545  * @offset:     where to start the search
546  *
547  * Returns next irq number after offset or nr_irqs if none is found.
548  */
549 unsigned int irq_get_next_irq(unsigned int offset)
550 {
551         return find_next_bit(allocated_irqs, nr_irqs, offset);
552 }
553
554 struct irq_desc *
555 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
556                     unsigned int check)
557 {
558         struct irq_desc *desc = irq_to_desc(irq);
559
560         if (desc) {
561                 if (check & _IRQ_DESC_CHECK) {
562                         if ((check & _IRQ_DESC_PERCPU) &&
563                             !irq_settings_is_per_cpu_devid(desc))
564                                 return NULL;
565
566                         if (!(check & _IRQ_DESC_PERCPU) &&
567                             irq_settings_is_per_cpu_devid(desc))
568                                 return NULL;
569                 }
570
571                 if (bus)
572                         chip_bus_lock(desc);
573                 raw_spin_lock_irqsave(&desc->lock, *flags);
574         }
575         return desc;
576 }
577
578 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
579 {
580         raw_spin_unlock_irqrestore(&desc->lock, flags);
581         if (bus)
582                 chip_bus_sync_unlock(desc);
583 }
584
585 int irq_set_percpu_devid(unsigned int irq)
586 {
587         struct irq_desc *desc = irq_to_desc(irq);
588
589         if (!desc)
590                 return -EINVAL;
591
592         if (desc->percpu_enabled)
593                 return -EINVAL;
594
595         desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
596
597         if (!desc->percpu_enabled)
598                 return -ENOMEM;
599
600         irq_set_percpu_devid_flags(irq);
601         return 0;
602 }
603
604 void kstat_incr_irq_this_cpu(unsigned int irq)
605 {
606         kstat_incr_irqs_this_cpu(irq_to_desc(irq));
607 }
608
609 /**
610  * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
611  * @irq:        The interrupt number
612  * @cpu:        The cpu number
613  *
614  * Returns the sum of interrupt counts on @cpu since boot for
615  * @irq. The caller must ensure that the interrupt is not removed
616  * concurrently.
617  */
618 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
619 {
620         struct irq_desc *desc = irq_to_desc(irq);
621
622         return desc && desc->kstat_irqs ?
623                         *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
624 }
625
626 /**
627  * kstat_irqs - Get the statistics for an interrupt
628  * @irq:        The interrupt number
629  *
630  * Returns the sum of interrupt counts on all cpus since boot for
631  * @irq. The caller must ensure that the interrupt is not removed
632  * concurrently.
633  */
634 unsigned int kstat_irqs(unsigned int irq)
635 {
636         struct irq_desc *desc = irq_to_desc(irq);
637         int cpu;
638         unsigned int sum = 0;
639
640         if (!desc || !desc->kstat_irqs)
641                 return 0;
642         for_each_possible_cpu(cpu)
643                 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
644         return sum;
645 }
646
647 /**
648  * kstat_irqs_usr - Get the statistics for an interrupt
649  * @irq:        The interrupt number
650  *
651  * Returns the sum of interrupt counts on all cpus since boot for
652  * @irq. Contrary to kstat_irqs() this can be called from any
653  * preemptible context. It's protected against concurrent removal of
654  * an interrupt descriptor when sparse irqs are enabled.
655  */
656 unsigned int kstat_irqs_usr(unsigned int irq)
657 {
658         unsigned int sum;
659
660         irq_lock_sparse();
661         sum = kstat_irqs(irq);
662         irq_unlock_sparse();
663         return sum;
664 }