blob: 9988d03797f5660dea26f417d9002fae94fd2798 [file] [log] [blame]
Thomas Gleixner3795de22010-09-22 17:09:43 +02001/*
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/module.h>
13#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
15#include <linux/radix-tree.h>
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020016#include <linux/bitmap.h>
Thomas Gleixner3795de22010-09-22 17:09:43 +020017
18#include "internals.h"
19
20/*
21 * lockdep: we want to handle all irq_desc locks as a single lock-class:
22 */
Thomas Gleixner78f90d92010-09-29 17:18:47 +020023static struct lock_class_key irq_desc_lock_class;
Thomas Gleixner3795de22010-09-22 17:09:43 +020024
25#if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
26static void __init init_irq_default_affinity(void)
27{
28 alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
29 cpumask_setall(irq_default_affinity);
30}
31#else
32static void __init init_irq_default_affinity(void)
33{
34}
35#endif
36
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020037#ifdef CONFIG_SMP
38static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
39{
40 if (!zalloc_cpumask_var_node(&desc->irq_data.affinity, gfp, node))
41 return -ENOMEM;
42
43#ifdef CONFIG_GENERIC_PENDING_IRQ
44 if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
45 free_cpumask_var(desc->irq_data.affinity);
46 return -ENOMEM;
47 }
48#endif
49 return 0;
50}
51
52static void desc_smp_init(struct irq_desc *desc, int node)
53{
Thomas Gleixneraa99ec02010-09-27 20:02:56 +020054 desc->irq_data.node = node;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020055 cpumask_copy(desc->irq_data.affinity, irq_default_affinity);
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020056#ifdef CONFIG_GENERIC_PENDING_IRQ
57 cpumask_clear(desc->pending_mask);
58#endif
59}
60
61static inline int desc_node(struct irq_desc *desc)
62{
63 return desc->irq_data.node;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020064}
65
66#else
67static inline int
68alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
69static inline void desc_smp_init(struct irq_desc *desc, int node) { }
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020070static inline int desc_node(struct irq_desc *desc) { return 0; }
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020071#endif
72
73static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node)
74{
75 desc->irq_data.irq = irq;
76 desc->irq_data.chip = &no_irq_chip;
77 desc->irq_data.chip_data = NULL;
78 desc->irq_data.handler_data = NULL;
79 desc->irq_data.msi_desc = NULL;
80 desc->status = IRQ_DEFAULT_INIT_FLAGS;
81 desc->handle_irq = handle_bad_irq;
82 desc->depth = 1;
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020083 desc->irq_count = 0;
84 desc->irqs_unhandled = 0;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020085 desc->name = NULL;
86 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
87 desc_smp_init(desc, node);
88}
89
Thomas Gleixner3795de22010-09-22 17:09:43 +020090int nr_irqs = NR_IRQS;
91EXPORT_SYMBOL_GPL(nr_irqs);
92
Thomas Gleixnera05a9002010-10-08 12:47:53 +020093static DEFINE_MUTEX(sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020094static DECLARE_BITMAP(allocated_irqs, NR_IRQS);
95
Thomas Gleixner3795de22010-09-22 17:09:43 +020096#ifdef CONFIG_SPARSE_IRQ
97
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +020098static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
Thomas Gleixner3795de22010-09-22 17:09:43 +020099
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200100static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner3795de22010-09-22 17:09:43 +0200101{
102 radix_tree_insert(&irq_desc_tree, irq, desc);
103}
104
105struct irq_desc *irq_to_desc(unsigned int irq)
106{
107 return radix_tree_lookup(&irq_desc_tree, irq);
108}
109
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200110static void delete_irq_desc(unsigned int irq)
111{
112 radix_tree_delete(&irq_desc_tree, irq);
113}
114
115#ifdef CONFIG_SMP
116static void free_masks(struct irq_desc *desc)
117{
118#ifdef CONFIG_GENERIC_PENDING_IRQ
119 free_cpumask_var(desc->pending_mask);
120#endif
Thomas Gleixnerc0a19eb2010-10-12 21:58:27 +0200121 free_cpumask_var(desc->irq_data.affinity);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200122}
123#else
124static inline void free_masks(struct irq_desc *desc) { }
125#endif
126
127static struct irq_desc *alloc_desc(int irq, int node)
128{
129 struct irq_desc *desc;
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200130 gfp_t gfp = GFP_KERNEL;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200131
132 desc = kzalloc_node(sizeof(*desc), gfp, node);
133 if (!desc)
134 return NULL;
135 /* allocate based on nr_cpu_ids */
136 desc->kstat_irqs = kzalloc_node(nr_cpu_ids * sizeof(*desc->kstat_irqs),
137 gfp, node);
138 if (!desc->kstat_irqs)
139 goto err_desc;
140
141 if (alloc_masks(desc, gfp, node))
142 goto err_kstat;
143
144 raw_spin_lock_init(&desc->lock);
145 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
146
147 desc_set_defaults(irq, desc, node);
148
149 return desc;
150
151err_kstat:
152 kfree(desc->kstat_irqs);
153err_desc:
154 kfree(desc);
155 return NULL;
156}
157
158static void free_desc(unsigned int irq)
159{
160 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200161
Thomas Gleixner13bfe992010-09-30 02:46:07 +0200162 unregister_irq_proc(irq, desc);
163
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200164 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200165 delete_irq_desc(irq);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200166 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200167
168 free_masks(desc);
169 kfree(desc->kstat_irqs);
170 kfree(desc);
171}
172
173static int alloc_descs(unsigned int start, unsigned int cnt, int node)
174{
175 struct irq_desc *desc;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200176 int i;
177
178 for (i = 0; i < cnt; i++) {
179 desc = alloc_desc(start + i, node);
180 if (!desc)
181 goto err;
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200182 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200183 irq_insert_desc(start + i, desc);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200184 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200185 }
186 return start;
187
188err:
189 for (i--; i >= 0; i--)
190 free_desc(start + i);
191
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200192 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200193 bitmap_clear(allocated_irqs, start, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200194 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200195 return -ENOMEM;
196}
197
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200198struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node)
199{
200 int res = irq_alloc_descs(irq, irq, 1, node);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200201
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200202 if (res == -EEXIST || res == irq)
203 return irq_to_desc(irq);
204 return NULL;
205}
Thomas Gleixner3795de22010-09-22 17:09:43 +0200206
207int __init early_irq_init(void)
208{
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200209 int i, initcnt, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200210 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200211
212 init_irq_default_affinity();
213
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200214 /* Let arch update nr_irqs and return the nr of preallocated irqs */
215 initcnt = arch_probe_nr_irqs();
216 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200217
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200218 for (i = 0; i < initcnt; i++) {
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200219 desc = alloc_desc(i, node);
220 set_bit(i, allocated_irqs);
221 irq_insert_desc(i, desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200222 }
Thomas Gleixner3795de22010-09-22 17:09:43 +0200223 return arch_early_irq_init();
224}
225
Thomas Gleixner3795de22010-09-22 17:09:43 +0200226#else /* !CONFIG_SPARSE_IRQ */
227
228struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
229 [0 ... NR_IRQS-1] = {
Thomas Gleixner1318a482010-09-27 21:01:37 +0200230 .status = IRQ_DEFAULT_INIT_FLAGS,
Thomas Gleixner3795de22010-09-22 17:09:43 +0200231 .handle_irq = handle_bad_irq,
232 .depth = 1,
233 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
234 }
235};
236
237static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
238int __init early_irq_init(void)
239{
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200240 int count, i, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200241 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200242
243 init_irq_default_affinity();
244
245 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
246
247 desc = irq_desc;
248 count = ARRAY_SIZE(irq_desc);
249
250 for (i = 0; i < count; i++) {
251 desc[i].irq_data.irq = i;
252 desc[i].irq_data.chip = &no_irq_chip;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200253 desc[i].kstat_irqs = kstat_irqs_all[i];
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200254 alloc_masks(desc + i, GFP_KERNEL, node);
255 desc_smp_init(desc + i, node);
Thomas Gleixner154cd382010-09-22 15:58:45 +0200256 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200257 }
258 return arch_early_irq_init();
259}
260
261struct irq_desc *irq_to_desc(unsigned int irq)
262{
263 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
264}
265
266struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
267{
268 return irq_to_desc(irq);
269}
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200270
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200271static void free_desc(unsigned int irq)
272{
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200273 dynamic_irq_cleanup(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200274}
275
276static inline int alloc_descs(unsigned int start, unsigned int cnt, int node)
277{
278 return start;
279}
Thomas Gleixner3795de22010-09-22 17:09:43 +0200280#endif /* !CONFIG_SPARSE_IRQ */
281
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200282/* Dynamic interrupt handling */
283
284/**
285 * irq_free_descs - free irq descriptors
286 * @from: Start of descriptor range
287 * @cnt: Number of consecutive irqs to free
288 */
289void irq_free_descs(unsigned int from, unsigned int cnt)
290{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200291 int i;
292
293 if (from >= nr_irqs || (from + cnt) > nr_irqs)
294 return;
295
296 for (i = 0; i < cnt; i++)
297 free_desc(from + i);
298
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200299 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200300 bitmap_clear(allocated_irqs, from, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200301 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200302}
303
304/**
305 * irq_alloc_descs - allocate and initialize a range of irq descriptors
306 * @irq: Allocate for specific irq number if irq >= 0
307 * @from: Start the search from this irq number
308 * @cnt: Number of consecutive irqs to allocate.
309 * @node: Preferred node on which the irq descriptor should be allocated
310 *
311 * Returns the first irq number or error code
312 */
313int __ref
314irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
315{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200316 int start, ret;
317
318 if (!cnt)
319 return -EINVAL;
320
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200321 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200322
323 start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
324 ret = -EEXIST;
325 if (irq >=0 && start != irq)
326 goto err;
327
328 ret = -ENOMEM;
329 if (start >= nr_irqs)
330 goto err;
331
332 bitmap_set(allocated_irqs, start, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200333 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200334 return alloc_descs(start, cnt, node);
335
336err:
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200337 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200338 return ret;
339}
340
Thomas Gleixnera98d24b2010-09-30 10:45:07 +0200341/**
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200342 * irq_reserve_irqs - mark irqs allocated
343 * @from: mark from irq number
344 * @cnt: number of irqs to mark
345 *
346 * Returns 0 on success or an appropriate error code
347 */
348int irq_reserve_irqs(unsigned int from, unsigned int cnt)
349{
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200350 unsigned int start;
351 int ret = 0;
352
353 if (!cnt || (from + cnt) > nr_irqs)
354 return -EINVAL;
355
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200356 mutex_lock(&sparse_irq_lock);
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200357 start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
358 if (start == from)
359 bitmap_set(allocated_irqs, start, cnt);
360 else
361 ret = -EEXIST;
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200362 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200363 return ret;
364}
365
366/**
Thomas Gleixnera98d24b2010-09-30 10:45:07 +0200367 * irq_get_next_irq - get next allocated irq number
368 * @offset: where to start the search
369 *
370 * Returns next irq number after offset or nr_irqs if none is found.
371 */
372unsigned int irq_get_next_irq(unsigned int offset)
373{
374 return find_next_bit(allocated_irqs, nr_irqs, offset);
375}
376
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200377/**
378 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
379 * @irq: irq number to initialize
380 */
381void dynamic_irq_cleanup(unsigned int irq)
Thomas Gleixner3795de22010-09-22 17:09:43 +0200382{
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200383 struct irq_desc *desc = irq_to_desc(irq);
384 unsigned long flags;
385
386 raw_spin_lock_irqsave(&desc->lock, flags);
387 desc_set_defaults(irq, desc, desc_node(desc));
388 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200389}
390
Thomas Gleixner3795de22010-09-22 17:09:43 +0200391unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
392{
393 struct irq_desc *desc = irq_to_desc(irq);
394 return desc ? desc->kstat_irqs[cpu] : 0;
395}
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700396
397#ifdef CONFIG_GENERIC_HARDIRQS
398unsigned int kstat_irqs(unsigned int irq)
399{
400 struct irq_desc *desc = irq_to_desc(irq);
401 int cpu;
402 int sum = 0;
403
404 if (!desc)
405 return 0;
406 for_each_possible_cpu(cpu)
407 sum += desc->kstat_irqs[cpu];
408 return sum;
409}
410#endif /* CONFIG_GENERIC_HARDIRQS */