blob: 7339e42a85ab9bb7f425a4da7806a0a37d9ec36c [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>
Paul Gortmakerec53cf22011-09-19 20:33:19 -040012#include <linux/export.h>
Thomas Gleixner3795de22010-09-22 17:09:43 +020013#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
Thomas Gleixnerfe051432011-05-18 12:53:03 +020025#if defined(CONFIG_SMP)
Thomas Gleixner3795de22010-09-22 17:09:43 +020026static 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
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +020073static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
74 struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020075{
Eric Dumazet6c9ae002011-01-13 15:45:38 -080076 int cpu;
77
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020078 desc->irq_data.irq = irq;
79 desc->irq_data.chip = &no_irq_chip;
80 desc->irq_data.chip_data = NULL;
81 desc->irq_data.handler_data = NULL;
82 desc->irq_data.msi_desc = NULL;
Thomas Gleixnerf9e49892011-02-09 14:54:49 +010083 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
Thomas Gleixner801a0e92011-03-27 11:02:49 +020084 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020085 desc->handle_irq = handle_bad_irq;
86 desc->depth = 1;
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020087 desc->irq_count = 0;
88 desc->irqs_unhandled = 0;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020089 desc->name = NULL;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +020090 desc->owner = owner;
Eric Dumazet6c9ae002011-01-13 15:45:38 -080091 for_each_possible_cpu(cpu)
92 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020093 desc_smp_init(desc, node);
94}
95
Thomas Gleixner3795de22010-09-22 17:09:43 +020096int nr_irqs = NR_IRQS;
97EXPORT_SYMBOL_GPL(nr_irqs);
98
Thomas Gleixnera05a9002010-10-08 12:47:53 +020099static DEFINE_MUTEX(sparse_irq_lock);
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +0100100static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200101
Thomas Gleixner3795de22010-09-22 17:09:43 +0200102#ifdef CONFIG_SPARSE_IRQ
103
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200104static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200105
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200106static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner3795de22010-09-22 17:09:43 +0200107{
108 radix_tree_insert(&irq_desc_tree, irq, desc);
109}
110
111struct irq_desc *irq_to_desc(unsigned int irq)
112{
113 return radix_tree_lookup(&irq_desc_tree, irq);
114}
Jiri Kosina3911ff32012-05-13 12:13:15 +0200115EXPORT_SYMBOL(irq_to_desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200116
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200117static void delete_irq_desc(unsigned int irq)
118{
119 radix_tree_delete(&irq_desc_tree, irq);
120}
121
122#ifdef CONFIG_SMP
123static void free_masks(struct irq_desc *desc)
124{
125#ifdef CONFIG_GENERIC_PENDING_IRQ
126 free_cpumask_var(desc->pending_mask);
127#endif
Thomas Gleixnerc0a19eb2010-10-12 21:58:27 +0200128 free_cpumask_var(desc->irq_data.affinity);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200129}
130#else
131static inline void free_masks(struct irq_desc *desc) { }
132#endif
133
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200134static struct irq_desc *alloc_desc(int irq, int node, struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200135{
136 struct irq_desc *desc;
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200137 gfp_t gfp = GFP_KERNEL;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200138
139 desc = kzalloc_node(sizeof(*desc), gfp, node);
140 if (!desc)
141 return NULL;
142 /* allocate based on nr_cpu_ids */
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800143 desc->kstat_irqs = alloc_percpu(unsigned int);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200144 if (!desc->kstat_irqs)
145 goto err_desc;
146
147 if (alloc_masks(desc, gfp, node))
148 goto err_kstat;
149
150 raw_spin_lock_init(&desc->lock);
151 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
152
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200153 desc_set_defaults(irq, desc, node, owner);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200154
155 return desc;
156
157err_kstat:
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800158 free_percpu(desc->kstat_irqs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200159err_desc:
160 kfree(desc);
161 return NULL;
162}
163
164static void free_desc(unsigned int irq)
165{
166 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200167
Thomas Gleixner13bfe992010-09-30 02:46:07 +0200168 unregister_irq_proc(irq, desc);
169
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200170 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200171 delete_irq_desc(irq);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200172 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200173
174 free_masks(desc);
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800175 free_percpu(desc->kstat_irqs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200176 kfree(desc);
177}
178
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200179static int alloc_descs(unsigned int start, unsigned int cnt, int node,
180 struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200181{
182 struct irq_desc *desc;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200183 int i;
184
185 for (i = 0; i < cnt; i++) {
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200186 desc = alloc_desc(start + i, node, owner);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200187 if (!desc)
188 goto err;
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200189 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200190 irq_insert_desc(start + i, desc);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200191 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200192 }
193 return start;
194
195err:
196 for (i--; i >= 0; i--)
197 free_desc(start + i);
198
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200199 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200200 bitmap_clear(allocated_irqs, start, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200201 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200202 return -ENOMEM;
203}
204
Yinghai Lued4dea62011-02-19 11:07:37 -0800205static int irq_expand_nr_irqs(unsigned int nr)
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100206{
Yinghai Lued4dea62011-02-19 11:07:37 -0800207 if (nr > IRQ_BITMAP_BITS)
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100208 return -ENOMEM;
Yinghai Lued4dea62011-02-19 11:07:37 -0800209 nr_irqs = nr;
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100210 return 0;
211}
212
Thomas Gleixner3795de22010-09-22 17:09:43 +0200213int __init early_irq_init(void)
214{
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200215 int i, initcnt, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200216 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200217
218 init_irq_default_affinity();
219
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200220 /* Let arch update nr_irqs and return the nr of preallocated irqs */
221 initcnt = arch_probe_nr_irqs();
222 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200223
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +0100224 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
225 nr_irqs = IRQ_BITMAP_BITS;
226
227 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
228 initcnt = IRQ_BITMAP_BITS;
229
230 if (initcnt > nr_irqs)
231 nr_irqs = initcnt;
232
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200233 for (i = 0; i < initcnt; i++) {
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200234 desc = alloc_desc(i, node, NULL);
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200235 set_bit(i, allocated_irqs);
236 irq_insert_desc(i, desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200237 }
Thomas Gleixner3795de22010-09-22 17:09:43 +0200238 return arch_early_irq_init();
239}
240
Thomas Gleixner3795de22010-09-22 17:09:43 +0200241#else /* !CONFIG_SPARSE_IRQ */
242
243struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
244 [0 ... NR_IRQS-1] = {
Thomas Gleixner3795de22010-09-22 17:09:43 +0200245 .handle_irq = handle_bad_irq,
246 .depth = 1,
247 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
248 }
249};
250
Thomas Gleixner3795de22010-09-22 17:09:43 +0200251int __init early_irq_init(void)
252{
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200253 int count, i, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200254 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200255
256 init_irq_default_affinity();
257
258 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
259
260 desc = irq_desc;
261 count = ARRAY_SIZE(irq_desc);
262
263 for (i = 0; i < count; i++) {
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800264 desc[i].kstat_irqs = alloc_percpu(unsigned int);
Linus Walleije7fbad32011-05-31 18:14:39 +0200265 alloc_masks(&desc[i], GFP_KERNEL, node);
266 raw_spin_lock_init(&desc[i].lock);
Thomas Gleixner154cd382010-09-22 15:58:45 +0200267 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200268 desc_set_defaults(i, &desc[i], node, NULL);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200269 }
270 return arch_early_irq_init();
271}
272
273struct irq_desc *irq_to_desc(unsigned int irq)
274{
275 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
276}
Paul Gortmaker2c45aad2014-02-10 13:39:53 -0500277EXPORT_SYMBOL(irq_to_desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200278
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200279static void free_desc(unsigned int irq)
280{
Thomas Gleixnerd8179bc2014-05-07 15:44:23 +0000281 struct irq_desc *desc = irq_to_desc(irq);
282 unsigned long flags;
283
284 raw_spin_lock_irqsave(&desc->lock, flags);
285 desc_set_defaults(irq, desc, desc_node(desc), NULL);
286 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200287}
288
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200289static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
290 struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200291{
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200292 u32 i;
293
294 for (i = 0; i < cnt; i++) {
295 struct irq_desc *desc = irq_to_desc(start + i);
296
297 desc->owner = owner;
298 }
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200299 return start;
300}
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100301
Yinghai Lued4dea62011-02-19 11:07:37 -0800302static int irq_expand_nr_irqs(unsigned int nr)
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100303{
304 return -ENOMEM;
305}
306
Thomas Gleixnerf63b6a02014-05-07 15:44:21 +0000307void irq_mark_irq(unsigned int irq)
308{
309 mutex_lock(&sparse_irq_lock);
310 bitmap_set(allocated_irqs, irq, 1);
311 mutex_unlock(&sparse_irq_lock);
312}
313
Thomas Gleixnerc940e012014-05-07 15:44:22 +0000314#ifdef CONFIG_GENERIC_IRQ_LEGACY
315void irq_init_desc(unsigned int irq)
316{
Thomas Gleixnerd8179bc2014-05-07 15:44:23 +0000317 free_desc(irq);
Thomas Gleixnerc940e012014-05-07 15:44:22 +0000318}
319#endif
320
Thomas Gleixner3795de22010-09-22 17:09:43 +0200321#endif /* !CONFIG_SPARSE_IRQ */
322
Thomas Gleixnerfe12bc22011-05-18 12:48:00 +0200323/**
324 * generic_handle_irq - Invoke the handler for a particular irq
325 * @irq: The irq number to handle
326 *
327 */
328int generic_handle_irq(unsigned int irq)
329{
330 struct irq_desc *desc = irq_to_desc(irq);
331
332 if (!desc)
333 return -EINVAL;
334 generic_handle_irq_desc(irq, desc);
335 return 0;
336}
Jonathan Cameronedf76f82011-05-18 10:39:04 +0100337EXPORT_SYMBOL_GPL(generic_handle_irq);
Thomas Gleixnerfe12bc22011-05-18 12:48:00 +0200338
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200339/* Dynamic interrupt handling */
340
341/**
342 * irq_free_descs - free irq descriptors
343 * @from: Start of descriptor range
344 * @cnt: Number of consecutive irqs to free
345 */
346void irq_free_descs(unsigned int from, unsigned int cnt)
347{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200348 int i;
349
350 if (from >= nr_irqs || (from + cnt) > nr_irqs)
351 return;
352
353 for (i = 0; i < cnt; i++)
354 free_desc(from + i);
355
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200356 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200357 bitmap_clear(allocated_irqs, from, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200358 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200359}
Jonathan Cameronedf76f82011-05-18 10:39:04 +0100360EXPORT_SYMBOL_GPL(irq_free_descs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200361
362/**
363 * irq_alloc_descs - allocate and initialize a range of irq descriptors
364 * @irq: Allocate for specific irq number if irq >= 0
365 * @from: Start the search from this irq number
366 * @cnt: Number of consecutive irqs to allocate.
367 * @node: Preferred node on which the irq descriptor should be allocated
Randy Dunlapd522a0d2011-08-18 12:19:27 -0700368 * @owner: Owning module (can be NULL)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200369 *
370 * Returns the first irq number or error code
371 */
372int __ref
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200373__irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
374 struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200375{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200376 int start, ret;
377
378 if (!cnt)
379 return -EINVAL;
380
Mark Brownc5182b82011-06-02 18:55:13 +0100381 if (irq >= 0) {
382 if (from > irq)
383 return -EINVAL;
384 from = irq;
Thomas Gleixner62a08ae2014-04-24 09:50:53 +0200385 } else {
386 /*
387 * For interrupts which are freely allocated the
388 * architecture can force a lower bound to the @from
389 * argument. x86 uses this to exclude the GSI space.
390 */
391 from = arch_dynirq_lower_bound(from);
Mark Brownc5182b82011-06-02 18:55:13 +0100392 }
393
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200394 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200395
Yinghai Lued4dea62011-02-19 11:07:37 -0800396 start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
397 from, cnt, 0);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200398 ret = -EEXIST;
399 if (irq >=0 && start != irq)
400 goto err;
401
Yinghai Lued4dea62011-02-19 11:07:37 -0800402 if (start + cnt > nr_irqs) {
403 ret = irq_expand_nr_irqs(start + cnt);
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100404 if (ret)
405 goto err;
406 }
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200407
408 bitmap_set(allocated_irqs, start, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200409 mutex_unlock(&sparse_irq_lock);
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200410 return alloc_descs(start, cnt, node, owner);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200411
412err:
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200413 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200414 return ret;
415}
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200416EXPORT_SYMBOL_GPL(__irq_alloc_descs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200417
Thomas Gleixner7b6ef122014-05-07 15:44:05 +0000418#ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
419/**
420 * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
421 * @cnt: number of interrupts to allocate
422 * @node: node on which to allocate
423 *
424 * Returns an interrupt number > 0 or 0, if the allocation fails.
425 */
426unsigned int irq_alloc_hwirqs(int cnt, int node)
427{
428 int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL);
429
430 if (irq < 0)
431 return 0;
432
433 for (i = irq; cnt > 0; i++, cnt--) {
434 if (arch_setup_hwirq(i, node))
435 goto err;
436 irq_clear_status_flags(i, _IRQ_NOREQUEST);
437 }
438 return irq;
439
440err:
441 for (i--; i >= irq; i--) {
442 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
443 arch_teardown_hwirq(i);
444 }
445 irq_free_descs(irq, cnt);
446 return 0;
447}
448EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
449
450/**
451 * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
452 * @from: Free from irq number
453 * @cnt: number of interrupts to free
454 *
455 */
456void irq_free_hwirqs(unsigned int from, int cnt)
457{
458 int i;
459
460 for (i = from; cnt > 0; i++, cnt--) {
461 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
462 arch_teardown_hwirq(i);
463 }
464 irq_free_descs(from, cnt);
465}
466EXPORT_SYMBOL_GPL(irq_free_hwirqs);
467#endif
468
Thomas Gleixnera98d24b2010-09-30 10:45:07 +0200469/**
470 * irq_get_next_irq - get next allocated irq number
471 * @offset: where to start the search
472 *
473 * Returns next irq number after offset or nr_irqs if none is found.
474 */
475unsigned int irq_get_next_irq(unsigned int offset)
476{
477 return find_next_bit(allocated_irqs, nr_irqs, offset);
478}
479
Thomas Gleixnerd5eb4ad22011-02-12 12:16:16 +0100480struct irq_desc *
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100481__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
482 unsigned int check)
Thomas Gleixnerd5eb4ad22011-02-12 12:16:16 +0100483{
484 struct irq_desc *desc = irq_to_desc(irq);
485
486 if (desc) {
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100487 if (check & _IRQ_DESC_CHECK) {
488 if ((check & _IRQ_DESC_PERCPU) &&
489 !irq_settings_is_per_cpu_devid(desc))
490 return NULL;
491
492 if (!(check & _IRQ_DESC_PERCPU) &&
493 irq_settings_is_per_cpu_devid(desc))
494 return NULL;
495 }
496
Thomas Gleixnerd5eb4ad22011-02-12 12:16:16 +0100497 if (bus)
498 chip_bus_lock(desc);
499 raw_spin_lock_irqsave(&desc->lock, *flags);
500 }
501 return desc;
502}
503
504void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
505{
506 raw_spin_unlock_irqrestore(&desc->lock, flags);
507 if (bus)
508 chip_bus_sync_unlock(desc);
509}
510
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100511int irq_set_percpu_devid(unsigned int irq)
512{
513 struct irq_desc *desc = irq_to_desc(irq);
514
515 if (!desc)
516 return -EINVAL;
517
518 if (desc->percpu_enabled)
519 return -EINVAL;
520
521 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
522
523 if (!desc->percpu_enabled)
524 return -ENOMEM;
525
526 irq_set_percpu_devid_flags(irq);
527 return 0;
528}
529
Thomas Gleixner792d0012014-02-23 21:40:14 +0000530void kstat_incr_irq_this_cpu(unsigned int irq)
531{
532 kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
533}
534
Thomas Gleixner3795de22010-09-22 17:09:43 +0200535unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
536{
537 struct irq_desc *desc = irq_to_desc(irq);
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800538
539 return desc && desc->kstat_irqs ?
540 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200541}
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700542
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700543unsigned int kstat_irqs(unsigned int irq)
544{
545 struct irq_desc *desc = irq_to_desc(irq);
546 int cpu;
547 int sum = 0;
548
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800549 if (!desc || !desc->kstat_irqs)
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700550 return 0;
551 for_each_possible_cpu(cpu)
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800552 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700553 return sum;
554}