]>
Commit | Line | Data |
---|---|---|
3795de23 TG |
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/module.h> | |
13 | #include <linux/interrupt.h> | |
14 | #include <linux/kernel_stat.h> | |
15 | #include <linux/radix-tree.h> | |
1f5a5b87 | 16 | #include <linux/bitmap.h> |
3795de23 TG |
17 | |
18 | #include "internals.h" | |
19 | ||
20 | /* | |
21 | * lockdep: we want to handle all irq_desc locks as a single lock-class: | |
22 | */ | |
78f90d91 | 23 | static struct lock_class_key irq_desc_lock_class; |
3795de23 | 24 | |
fe051434 | 25 | #if defined(CONFIG_SMP) |
3795de23 TG |
26 | static 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 | |
32 | static void __init init_irq_default_affinity(void) | |
33 | { | |
34 | } | |
35 | #endif | |
36 | ||
1f5a5b87 TG |
37 | #ifdef CONFIG_SMP |
38 | static 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 | ||
52 | static void desc_smp_init(struct irq_desc *desc, int node) | |
53 | { | |
aa99ec0f | 54 | desc->irq_data.node = node; |
1f5a5b87 | 55 | cpumask_copy(desc->irq_data.affinity, irq_default_affinity); |
b7b29338 TG |
56 | #ifdef CONFIG_GENERIC_PENDING_IRQ |
57 | cpumask_clear(desc->pending_mask); | |
58 | #endif | |
59 | } | |
60 | ||
61 | static inline int desc_node(struct irq_desc *desc) | |
62 | { | |
63 | return desc->irq_data.node; | |
1f5a5b87 TG |
64 | } |
65 | ||
66 | #else | |
67 | static inline int | |
68 | alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; } | |
69 | static inline void desc_smp_init(struct irq_desc *desc, int node) { } | |
b7b29338 | 70 | static inline int desc_node(struct irq_desc *desc) { return 0; } |
1f5a5b87 TG |
71 | #endif |
72 | ||
73 | static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node) | |
74 | { | |
6c9ae009 ED |
75 | int cpu; |
76 | ||
1f5a5b87 TG |
77 | 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; | |
f9e4989e | 82 | irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS); |
801a0e9a | 83 | irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED); |
1f5a5b87 TG |
84 | desc->handle_irq = handle_bad_irq; |
85 | desc->depth = 1; | |
b7b29338 TG |
86 | desc->irq_count = 0; |
87 | desc->irqs_unhandled = 0; | |
1f5a5b87 | 88 | desc->name = NULL; |
6c9ae009 ED |
89 | for_each_possible_cpu(cpu) |
90 | *per_cpu_ptr(desc->kstat_irqs, cpu) = 0; | |
1f5a5b87 TG |
91 | desc_smp_init(desc, node); |
92 | } | |
93 | ||
3795de23 TG |
94 | int nr_irqs = NR_IRQS; |
95 | EXPORT_SYMBOL_GPL(nr_irqs); | |
96 | ||
a05a900a | 97 | static DEFINE_MUTEX(sparse_irq_lock); |
c1ee6264 | 98 | static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS); |
1f5a5b87 | 99 | |
3795de23 TG |
100 | #ifdef CONFIG_SPARSE_IRQ |
101 | ||
baa0d233 | 102 | static RADIX_TREE(irq_desc_tree, GFP_KERNEL); |
3795de23 | 103 | |
1f5a5b87 | 104 | static void irq_insert_desc(unsigned int irq, struct irq_desc *desc) |
3795de23 TG |
105 | { |
106 | radix_tree_insert(&irq_desc_tree, irq, desc); | |
107 | } | |
108 | ||
109 | struct irq_desc *irq_to_desc(unsigned int irq) | |
110 | { | |
111 | return radix_tree_lookup(&irq_desc_tree, irq); | |
112 | } | |
113 | ||
1f5a5b87 TG |
114 | static void delete_irq_desc(unsigned int irq) |
115 | { | |
116 | radix_tree_delete(&irq_desc_tree, irq); | |
117 | } | |
118 | ||
119 | #ifdef CONFIG_SMP | |
120 | static void free_masks(struct irq_desc *desc) | |
121 | { | |
122 | #ifdef CONFIG_GENERIC_PENDING_IRQ | |
123 | free_cpumask_var(desc->pending_mask); | |
124 | #endif | |
c0a19ebc | 125 | free_cpumask_var(desc->irq_data.affinity); |
1f5a5b87 TG |
126 | } |
127 | #else | |
128 | static inline void free_masks(struct irq_desc *desc) { } | |
129 | #endif | |
130 | ||
131 | static struct irq_desc *alloc_desc(int irq, int node) | |
132 | { | |
133 | struct irq_desc *desc; | |
baa0d233 | 134 | gfp_t gfp = GFP_KERNEL; |
1f5a5b87 TG |
135 | |
136 | desc = kzalloc_node(sizeof(*desc), gfp, node); | |
137 | if (!desc) | |
138 | return NULL; | |
139 | /* allocate based on nr_cpu_ids */ | |
6c9ae009 | 140 | desc->kstat_irqs = alloc_percpu(unsigned int); |
1f5a5b87 TG |
141 | if (!desc->kstat_irqs) |
142 | goto err_desc; | |
143 | ||
144 | if (alloc_masks(desc, gfp, node)) | |
145 | goto err_kstat; | |
146 | ||
147 | raw_spin_lock_init(&desc->lock); | |
148 | lockdep_set_class(&desc->lock, &irq_desc_lock_class); | |
149 | ||
150 | desc_set_defaults(irq, desc, node); | |
151 | ||
152 | return desc; | |
153 | ||
154 | err_kstat: | |
6c9ae009 | 155 | free_percpu(desc->kstat_irqs); |
1f5a5b87 TG |
156 | err_desc: |
157 | kfree(desc); | |
158 | return NULL; | |
159 | } | |
160 | ||
161 | static void free_desc(unsigned int irq) | |
162 | { | |
163 | struct irq_desc *desc = irq_to_desc(irq); | |
1f5a5b87 | 164 | |
13bfe99e TG |
165 | unregister_irq_proc(irq, desc); |
166 | ||
a05a900a | 167 | mutex_lock(&sparse_irq_lock); |
1f5a5b87 | 168 | delete_irq_desc(irq); |
a05a900a | 169 | mutex_unlock(&sparse_irq_lock); |
1f5a5b87 TG |
170 | |
171 | free_masks(desc); | |
6c9ae009 | 172 | free_percpu(desc->kstat_irqs); |
1f5a5b87 TG |
173 | kfree(desc); |
174 | } | |
175 | ||
176 | static int alloc_descs(unsigned int start, unsigned int cnt, int node) | |
177 | { | |
178 | struct irq_desc *desc; | |
1f5a5b87 TG |
179 | int i; |
180 | ||
181 | for (i = 0; i < cnt; i++) { | |
182 | desc = alloc_desc(start + i, node); | |
183 | if (!desc) | |
184 | goto err; | |
a05a900a | 185 | mutex_lock(&sparse_irq_lock); |
1f5a5b87 | 186 | irq_insert_desc(start + i, desc); |
a05a900a | 187 | mutex_unlock(&sparse_irq_lock); |
1f5a5b87 TG |
188 | } |
189 | return start; | |
190 | ||
191 | err: | |
192 | for (i--; i >= 0; i--) | |
193 | free_desc(start + i); | |
194 | ||
a05a900a | 195 | mutex_lock(&sparse_irq_lock); |
1f5a5b87 | 196 | bitmap_clear(allocated_irqs, start, cnt); |
a05a900a | 197 | mutex_unlock(&sparse_irq_lock); |
1f5a5b87 TG |
198 | return -ENOMEM; |
199 | } | |
200 | ||
ed4dea6e | 201 | static int irq_expand_nr_irqs(unsigned int nr) |
e7bcecb7 | 202 | { |
ed4dea6e | 203 | if (nr > IRQ_BITMAP_BITS) |
e7bcecb7 | 204 | return -ENOMEM; |
ed4dea6e | 205 | nr_irqs = nr; |
e7bcecb7 TG |
206 | return 0; |
207 | } | |
208 | ||
3795de23 TG |
209 | int __init early_irq_init(void) |
210 | { | |
b683de2b | 211 | int i, initcnt, node = first_online_node; |
3795de23 | 212 | struct irq_desc *desc; |
3795de23 TG |
213 | |
214 | init_irq_default_affinity(); | |
215 | ||
b683de2b TG |
216 | /* Let arch update nr_irqs and return the nr of preallocated irqs */ |
217 | initcnt = arch_probe_nr_irqs(); | |
218 | printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt); | |
3795de23 | 219 | |
c1ee6264 TG |
220 | if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS)) |
221 | nr_irqs = IRQ_BITMAP_BITS; | |
222 | ||
223 | if (WARN_ON(initcnt > IRQ_BITMAP_BITS)) | |
224 | initcnt = IRQ_BITMAP_BITS; | |
225 | ||
226 | if (initcnt > nr_irqs) | |
227 | nr_irqs = initcnt; | |
228 | ||
b683de2b | 229 | for (i = 0; i < initcnt; i++) { |
aa99ec0f TG |
230 | desc = alloc_desc(i, node); |
231 | set_bit(i, allocated_irqs); | |
232 | irq_insert_desc(i, desc); | |
3795de23 | 233 | } |
3795de23 TG |
234 | return arch_early_irq_init(); |
235 | } | |
236 | ||
3795de23 TG |
237 | #else /* !CONFIG_SPARSE_IRQ */ |
238 | ||
239 | struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = { | |
240 | [0 ... NR_IRQS-1] = { | |
3795de23 TG |
241 | .handle_irq = handle_bad_irq, |
242 | .depth = 1, | |
243 | .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock), | |
244 | } | |
245 | }; | |
246 | ||
3795de23 TG |
247 | int __init early_irq_init(void) |
248 | { | |
aa99ec0f | 249 | int count, i, node = first_online_node; |
3795de23 | 250 | struct irq_desc *desc; |
3795de23 TG |
251 | |
252 | init_irq_default_affinity(); | |
253 | ||
254 | printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS); | |
255 | ||
256 | desc = irq_desc; | |
257 | count = ARRAY_SIZE(irq_desc); | |
258 | ||
259 | for (i = 0; i < count; i++) { | |
260 | desc[i].irq_data.irq = i; | |
261 | desc[i].irq_data.chip = &no_irq_chip; | |
6c9ae009 | 262 | desc[i].kstat_irqs = alloc_percpu(unsigned int); |
f9e4989e | 263 | irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS); |
aa99ec0f TG |
264 | alloc_masks(desc + i, GFP_KERNEL, node); |
265 | desc_smp_init(desc + i, node); | |
154cd387 | 266 | lockdep_set_class(&desc[i].lock, &irq_desc_lock_class); |
3795de23 TG |
267 | } |
268 | return arch_early_irq_init(); | |
269 | } | |
270 | ||
271 | struct irq_desc *irq_to_desc(unsigned int irq) | |
272 | { | |
273 | return (irq < NR_IRQS) ? irq_desc + irq : NULL; | |
274 | } | |
275 | ||
1f5a5b87 TG |
276 | static void free_desc(unsigned int irq) |
277 | { | |
b7b29338 | 278 | dynamic_irq_cleanup(irq); |
1f5a5b87 TG |
279 | } |
280 | ||
281 | static inline int alloc_descs(unsigned int start, unsigned int cnt, int node) | |
282 | { | |
283 | return start; | |
284 | } | |
e7bcecb7 | 285 | |
ed4dea6e | 286 | static int irq_expand_nr_irqs(unsigned int nr) |
e7bcecb7 TG |
287 | { |
288 | return -ENOMEM; | |
289 | } | |
290 | ||
3795de23 TG |
291 | #endif /* !CONFIG_SPARSE_IRQ */ |
292 | ||
1f5a5b87 TG |
293 | /* Dynamic interrupt handling */ |
294 | ||
295 | /** | |
296 | * irq_free_descs - free irq descriptors | |
297 | * @from: Start of descriptor range | |
298 | * @cnt: Number of consecutive irqs to free | |
299 | */ | |
300 | void irq_free_descs(unsigned int from, unsigned int cnt) | |
301 | { | |
1f5a5b87 TG |
302 | int i; |
303 | ||
304 | if (from >= nr_irqs || (from + cnt) > nr_irqs) | |
305 | return; | |
306 | ||
307 | for (i = 0; i < cnt; i++) | |
308 | free_desc(from + i); | |
309 | ||
a05a900a | 310 | mutex_lock(&sparse_irq_lock); |
1f5a5b87 | 311 | bitmap_clear(allocated_irqs, from, cnt); |
a05a900a | 312 | mutex_unlock(&sparse_irq_lock); |
1f5a5b87 TG |
313 | } |
314 | ||
315 | /** | |
316 | * irq_alloc_descs - allocate and initialize a range of irq descriptors | |
317 | * @irq: Allocate for specific irq number if irq >= 0 | |
318 | * @from: Start the search from this irq number | |
319 | * @cnt: Number of consecutive irqs to allocate. | |
320 | * @node: Preferred node on which the irq descriptor should be allocated | |
321 | * | |
322 | * Returns the first irq number or error code | |
323 | */ | |
324 | int __ref | |
325 | irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node) | |
326 | { | |
1f5a5b87 TG |
327 | int start, ret; |
328 | ||
329 | if (!cnt) | |
330 | return -EINVAL; | |
331 | ||
a05a900a | 332 | mutex_lock(&sparse_irq_lock); |
1f5a5b87 | 333 | |
ed4dea6e YL |
334 | start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS, |
335 | from, cnt, 0); | |
1f5a5b87 TG |
336 | ret = -EEXIST; |
337 | if (irq >=0 && start != irq) | |
338 | goto err; | |
339 | ||
ed4dea6e YL |
340 | if (start + cnt > nr_irqs) { |
341 | ret = irq_expand_nr_irqs(start + cnt); | |
e7bcecb7 TG |
342 | if (ret) |
343 | goto err; | |
344 | } | |
1f5a5b87 TG |
345 | |
346 | bitmap_set(allocated_irqs, start, cnt); | |
a05a900a | 347 | mutex_unlock(&sparse_irq_lock); |
1f5a5b87 TG |
348 | return alloc_descs(start, cnt, node); |
349 | ||
350 | err: | |
a05a900a | 351 | mutex_unlock(&sparse_irq_lock); |
1f5a5b87 TG |
352 | return ret; |
353 | } | |
354 | ||
06f6c339 TG |
355 | /** |
356 | * irq_reserve_irqs - mark irqs allocated | |
357 | * @from: mark from irq number | |
358 | * @cnt: number of irqs to mark | |
359 | * | |
360 | * Returns 0 on success or an appropriate error code | |
361 | */ | |
362 | int irq_reserve_irqs(unsigned int from, unsigned int cnt) | |
363 | { | |
06f6c339 TG |
364 | unsigned int start; |
365 | int ret = 0; | |
366 | ||
367 | if (!cnt || (from + cnt) > nr_irqs) | |
368 | return -EINVAL; | |
369 | ||
a05a900a | 370 | mutex_lock(&sparse_irq_lock); |
06f6c339 TG |
371 | start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0); |
372 | if (start == from) | |
373 | bitmap_set(allocated_irqs, start, cnt); | |
374 | else | |
375 | ret = -EEXIST; | |
a05a900a | 376 | mutex_unlock(&sparse_irq_lock); |
06f6c339 TG |
377 | return ret; |
378 | } | |
379 | ||
a98d24b7 TG |
380 | /** |
381 | * irq_get_next_irq - get next allocated irq number | |
382 | * @offset: where to start the search | |
383 | * | |
384 | * Returns next irq number after offset or nr_irqs if none is found. | |
385 | */ | |
386 | unsigned int irq_get_next_irq(unsigned int offset) | |
387 | { | |
388 | return find_next_bit(allocated_irqs, nr_irqs, offset); | |
389 | } | |
390 | ||
d5eb4ad2 TG |
391 | struct irq_desc * |
392 | __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus) | |
393 | { | |
394 | struct irq_desc *desc = irq_to_desc(irq); | |
395 | ||
396 | if (desc) { | |
397 | if (bus) | |
398 | chip_bus_lock(desc); | |
399 | raw_spin_lock_irqsave(&desc->lock, *flags); | |
400 | } | |
401 | return desc; | |
402 | } | |
403 | ||
404 | void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus) | |
405 | { | |
406 | raw_spin_unlock_irqrestore(&desc->lock, flags); | |
407 | if (bus) | |
408 | chip_bus_sync_unlock(desc); | |
409 | } | |
410 | ||
b7b29338 TG |
411 | /** |
412 | * dynamic_irq_cleanup - cleanup a dynamically allocated irq | |
413 | * @irq: irq number to initialize | |
414 | */ | |
415 | void dynamic_irq_cleanup(unsigned int irq) | |
3795de23 | 416 | { |
b7b29338 TG |
417 | struct irq_desc *desc = irq_to_desc(irq); |
418 | unsigned long flags; | |
419 | ||
420 | raw_spin_lock_irqsave(&desc->lock, flags); | |
421 | desc_set_defaults(irq, desc, desc_node(desc)); | |
422 | raw_spin_unlock_irqrestore(&desc->lock, flags); | |
3795de23 TG |
423 | } |
424 | ||
3795de23 TG |
425 | unsigned int kstat_irqs_cpu(unsigned int irq, int cpu) |
426 | { | |
427 | struct irq_desc *desc = irq_to_desc(irq); | |
6c9ae009 ED |
428 | |
429 | return desc && desc->kstat_irqs ? | |
430 | *per_cpu_ptr(desc->kstat_irqs, cpu) : 0; | |
3795de23 | 431 | } |
478735e3 | 432 | |
478735e3 KH |
433 | unsigned int kstat_irqs(unsigned int irq) |
434 | { | |
435 | struct irq_desc *desc = irq_to_desc(irq); | |
436 | int cpu; | |
437 | int sum = 0; | |
438 | ||
6c9ae009 | 439 | if (!desc || !desc->kstat_irqs) |
478735e3 KH |
440 | return 0; |
441 | for_each_possible_cpu(cpu) | |
6c9ae009 | 442 | sum += *per_cpu_ptr(desc->kstat_irqs, cpu); |
478735e3 KH |
443 | return sum; |
444 | } |