blob: 6f6644f819dd9607b8d28a59856d9234bba0c8eb [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{
Eric Dumazet6c9ae002011-01-13 15:45:38 -080075 int cpu;
76
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020077 desc->irq_data.irq = irq;
78 desc->irq_data.chip = &no_irq_chip;
79 desc->irq_data.chip_data = NULL;
80 desc->irq_data.handler_data = NULL;
81 desc->irq_data.msi_desc = NULL;
82 desc->status = IRQ_DEFAULT_INIT_FLAGS;
83 desc->handle_irq = handle_bad_irq;
84 desc->depth = 1;
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020085 desc->irq_count = 0;
86 desc->irqs_unhandled = 0;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020087 desc->name = NULL;
Eric Dumazet6c9ae002011-01-13 15:45:38 -080088 for_each_possible_cpu(cpu)
89 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020090 desc_smp_init(desc, node);
91}
92
Thomas Gleixner3795de22010-09-22 17:09:43 +020093int nr_irqs = NR_IRQS;
94EXPORT_SYMBOL_GPL(nr_irqs);
95
Thomas Gleixnera05a9002010-10-08 12:47:53 +020096static DEFINE_MUTEX(sparse_irq_lock);
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +010097static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020098
Thomas Gleixner3795de22010-09-22 17:09:43 +020099#ifdef CONFIG_SPARSE_IRQ
100
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200101static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200102
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200103static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner3795de22010-09-22 17:09:43 +0200104{
105 radix_tree_insert(&irq_desc_tree, irq, desc);
106}
107
108struct irq_desc *irq_to_desc(unsigned int irq)
109{
110 return radix_tree_lookup(&irq_desc_tree, irq);
111}
112
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200113static void delete_irq_desc(unsigned int irq)
114{
115 radix_tree_delete(&irq_desc_tree, irq);
116}
117
118#ifdef CONFIG_SMP
119static void free_masks(struct irq_desc *desc)
120{
121#ifdef CONFIG_GENERIC_PENDING_IRQ
122 free_cpumask_var(desc->pending_mask);
123#endif
Thomas Gleixnerc0a19eb2010-10-12 21:58:27 +0200124 free_cpumask_var(desc->irq_data.affinity);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200125}
126#else
127static inline void free_masks(struct irq_desc *desc) { }
128#endif
129
130static struct irq_desc *alloc_desc(int irq, int node)
131{
132 struct irq_desc *desc;
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200133 gfp_t gfp = GFP_KERNEL;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200134
135 desc = kzalloc_node(sizeof(*desc), gfp, node);
136 if (!desc)
137 return NULL;
138 /* allocate based on nr_cpu_ids */
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800139 desc->kstat_irqs = alloc_percpu(unsigned int);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200140 if (!desc->kstat_irqs)
141 goto err_desc;
142
143 if (alloc_masks(desc, gfp, node))
144 goto err_kstat;
145
146 raw_spin_lock_init(&desc->lock);
147 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
148
149 desc_set_defaults(irq, desc, node);
150
151 return desc;
152
153err_kstat:
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800154 free_percpu(desc->kstat_irqs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200155err_desc:
156 kfree(desc);
157 return NULL;
158}
159
160static void free_desc(unsigned int irq)
161{
162 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200163
Thomas Gleixner13bfe992010-09-30 02:46:07 +0200164 unregister_irq_proc(irq, desc);
165
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200166 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200167 delete_irq_desc(irq);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200168 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200169
170 free_masks(desc);
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800171 free_percpu(desc->kstat_irqs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200172 kfree(desc);
173}
174
175static int alloc_descs(unsigned int start, unsigned int cnt, int node)
176{
177 struct irq_desc *desc;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200178 int i;
179
180 for (i = 0; i < cnt; i++) {
181 desc = alloc_desc(start + i, node);
182 if (!desc)
183 goto err;
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200184 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200185 irq_insert_desc(start + i, desc);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200186 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200187 }
188 return start;
189
190err:
191 for (i--; i >= 0; i--)
192 free_desc(start + i);
193
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200194 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200195 bitmap_clear(allocated_irqs, start, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200196 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200197 return -ENOMEM;
198}
199
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200200struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node)
201{
202 int res = irq_alloc_descs(irq, irq, 1, node);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200203
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200204 if (res == -EEXIST || res == irq)
205 return irq_to_desc(irq);
206 return NULL;
207}
Thomas Gleixner3795de22010-09-22 17:09:43 +0200208
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100209static int irq_expand_nr_irqs(unsigned int cnt)
210{
211 if (nr_irqs + cnt > IRQ_BITMAP_BITS)
212 return -ENOMEM;
213 nr_irqs += cnt;
214 return 0;
215}
216
Thomas Gleixner3795de22010-09-22 17:09:43 +0200217int __init early_irq_init(void)
218{
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200219 int i, initcnt, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200220 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200221
222 init_irq_default_affinity();
223
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200224 /* Let arch update nr_irqs and return the nr of preallocated irqs */
225 initcnt = arch_probe_nr_irqs();
226 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200227
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +0100228 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
229 nr_irqs = IRQ_BITMAP_BITS;
230
231 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
232 initcnt = IRQ_BITMAP_BITS;
233
234 if (initcnt > nr_irqs)
235 nr_irqs = initcnt;
236
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200237 for (i = 0; i < initcnt; i++) {
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200238 desc = alloc_desc(i, node);
239 set_bit(i, allocated_irqs);
240 irq_insert_desc(i, desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200241 }
Thomas Gleixner3795de22010-09-22 17:09:43 +0200242 return arch_early_irq_init();
243}
244
Thomas Gleixner3795de22010-09-22 17:09:43 +0200245#else /* !CONFIG_SPARSE_IRQ */
246
247struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
248 [0 ... NR_IRQS-1] = {
Thomas Gleixner1318a482010-09-27 21:01:37 +0200249 .status = IRQ_DEFAULT_INIT_FLAGS,
Thomas Gleixner3795de22010-09-22 17:09:43 +0200250 .handle_irq = handle_bad_irq,
251 .depth = 1,
252 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
253 }
254};
255
Thomas Gleixner3795de22010-09-22 17:09:43 +0200256int __init early_irq_init(void)
257{
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200258 int count, i, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200259 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200260
261 init_irq_default_affinity();
262
263 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
264
265 desc = irq_desc;
266 count = ARRAY_SIZE(irq_desc);
267
268 for (i = 0; i < count; i++) {
269 desc[i].irq_data.irq = i;
270 desc[i].irq_data.chip = &no_irq_chip;
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800271 desc[i].kstat_irqs = alloc_percpu(unsigned int);
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200272 alloc_masks(desc + i, GFP_KERNEL, node);
273 desc_smp_init(desc + i, node);
Thomas Gleixner154cd382010-09-22 15:58:45 +0200274 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200275 }
276 return arch_early_irq_init();
277}
278
279struct irq_desc *irq_to_desc(unsigned int irq)
280{
281 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
282}
283
284struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
285{
286 return irq_to_desc(irq);
287}
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200288
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200289static void free_desc(unsigned int irq)
290{
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200291 dynamic_irq_cleanup(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200292}
293
294static inline int alloc_descs(unsigned int start, unsigned int cnt, int node)
295{
296 return start;
297}
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100298
299static int irq_expand_nr_irqs(unsigned int cnt)
300{
301 return -ENOMEM;
302}
303
Thomas Gleixner3795de22010-09-22 17:09:43 +0200304#endif /* !CONFIG_SPARSE_IRQ */
305
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200306/* Dynamic interrupt handling */
307
308/**
309 * irq_free_descs - free irq descriptors
310 * @from: Start of descriptor range
311 * @cnt: Number of consecutive irqs to free
312 */
313void irq_free_descs(unsigned int from, unsigned int cnt)
314{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200315 int i;
316
317 if (from >= nr_irqs || (from + cnt) > nr_irqs)
318 return;
319
320 for (i = 0; i < cnt; i++)
321 free_desc(from + i);
322
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200323 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200324 bitmap_clear(allocated_irqs, from, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200325 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200326}
327
328/**
329 * irq_alloc_descs - allocate and initialize a range of irq descriptors
330 * @irq: Allocate for specific irq number if irq >= 0
331 * @from: Start the search from this irq number
332 * @cnt: Number of consecutive irqs to allocate.
333 * @node: Preferred node on which the irq descriptor should be allocated
334 *
335 * Returns the first irq number or error code
336 */
337int __ref
338irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
339{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200340 int start, ret;
341
342 if (!cnt)
343 return -EINVAL;
344
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200345 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200346
347 start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
348 ret = -EEXIST;
349 if (irq >=0 && start != irq)
350 goto err;
351
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100352 if (start >= nr_irqs) {
353 ret = irq_expand_nr_irqs(cnt);
354 if (ret)
355 goto err;
356 }
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200357
358 bitmap_set(allocated_irqs, start, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200359 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200360 return alloc_descs(start, cnt, node);
361
362err:
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200363 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200364 return ret;
365}
366
Thomas Gleixnera98d24b2010-09-30 10:45:07 +0200367/**
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200368 * irq_reserve_irqs - mark irqs allocated
369 * @from: mark from irq number
370 * @cnt: number of irqs to mark
371 *
372 * Returns 0 on success or an appropriate error code
373 */
374int irq_reserve_irqs(unsigned int from, unsigned int cnt)
375{
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200376 unsigned int start;
377 int ret = 0;
378
379 if (!cnt || (from + cnt) > nr_irqs)
380 return -EINVAL;
381
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200382 mutex_lock(&sparse_irq_lock);
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200383 start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
384 if (start == from)
385 bitmap_set(allocated_irqs, start, cnt);
386 else
387 ret = -EEXIST;
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200388 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner06f6c332010-10-12 12:31:46 +0200389 return ret;
390}
391
392/**
Thomas Gleixnera98d24b2010-09-30 10:45:07 +0200393 * irq_get_next_irq - get next allocated irq number
394 * @offset: where to start the search
395 *
396 * Returns next irq number after offset or nr_irqs if none is found.
397 */
398unsigned int irq_get_next_irq(unsigned int offset)
399{
400 return find_next_bit(allocated_irqs, nr_irqs, offset);
401}
402
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200403/**
404 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
405 * @irq: irq number to initialize
406 */
407void dynamic_irq_cleanup(unsigned int irq)
Thomas Gleixner3795de22010-09-22 17:09:43 +0200408{
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200409 struct irq_desc *desc = irq_to_desc(irq);
410 unsigned long flags;
411
412 raw_spin_lock_irqsave(&desc->lock, flags);
413 desc_set_defaults(irq, desc, desc_node(desc));
414 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200415}
416
Thomas Gleixner3795de22010-09-22 17:09:43 +0200417unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
418{
419 struct irq_desc *desc = irq_to_desc(irq);
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800420
421 return desc && desc->kstat_irqs ?
422 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200423}
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700424
425#ifdef CONFIG_GENERIC_HARDIRQS
426unsigned int kstat_irqs(unsigned int irq)
427{
428 struct irq_desc *desc = irq_to_desc(irq);
429 int cpu;
430 int sum = 0;
431
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800432 if (!desc || !desc->kstat_irqs)
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700433 return 0;
434 for_each_possible_cpu(cpu)
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800435 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700436 return sum;
437}
438#endif /* CONFIG_GENERIC_HARDIRQS */