Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 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> |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 16 | #include <linux/bitmap.h> |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 17 | |
| 18 | #include "internals.h" |
| 19 | |
| 20 | /* |
| 21 | * lockdep: we want to handle all irq_desc locks as a single lock-class: |
| 22 | */ |
Thomas Gleixner | 78f90d9 | 2010-09-29 17:18:47 +0200 | [diff] [blame] | 23 | static struct lock_class_key irq_desc_lock_class; |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 24 | |
| 25 | #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS) |
| 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 | |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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 | { |
Thomas Gleixner | aa99ec0 | 2010-09-27 20:02:56 +0200 | [diff] [blame] | 54 | desc->irq_data.node = node; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 55 | cpumask_copy(desc->irq_data.affinity, irq_default_affinity); |
Thomas Gleixner | b7b2933 | 2010-09-29 18:46:55 +0200 | [diff] [blame] | 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; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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) { } |
Thomas Gleixner | b7b2933 | 2010-09-29 18:46:55 +0200 | [diff] [blame] | 70 | static inline int desc_node(struct irq_desc *desc) { return 0; } |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node) |
| 74 | { |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 75 | int cpu; |
| 76 | |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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; |
Thomas Gleixner | f9e4989 | 2011-02-09 14:54:49 +0100 | [diff] [blame^] | 82 | irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS); |
Thomas Gleixner | c1594b7 | 2011-02-07 22:11:30 +0100 | [diff] [blame] | 83 | desc->istate = IRQS_DISABLED; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 84 | desc->handle_irq = handle_bad_irq; |
| 85 | desc->depth = 1; |
Thomas Gleixner | b7b2933 | 2010-09-29 18:46:55 +0200 | [diff] [blame] | 86 | desc->irq_count = 0; |
| 87 | desc->irqs_unhandled = 0; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 88 | desc->name = NULL; |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 89 | for_each_possible_cpu(cpu) |
| 90 | *per_cpu_ptr(desc->kstat_irqs, cpu) = 0; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 91 | desc_smp_init(desc, node); |
| 92 | } |
| 93 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 94 | int nr_irqs = NR_IRQS; |
| 95 | EXPORT_SYMBOL_GPL(nr_irqs); |
| 96 | |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 97 | static DEFINE_MUTEX(sparse_irq_lock); |
Thomas Gleixner | c1ee626 | 2011-02-17 17:45:15 +0100 | [diff] [blame] | 98 | static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 99 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 100 | #ifdef CONFIG_SPARSE_IRQ |
| 101 | |
Thomas Gleixner | baa0d23 | 2010-10-05 15:14:35 +0200 | [diff] [blame] | 102 | static RADIX_TREE(irq_desc_tree, GFP_KERNEL); |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 103 | |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 104 | static void irq_insert_desc(unsigned int irq, struct irq_desc *desc) |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 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 | |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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 |
Thomas Gleixner | c0a19eb | 2010-10-12 21:58:27 +0200 | [diff] [blame] | 125 | free_cpumask_var(desc->irq_data.affinity); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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; |
Thomas Gleixner | baa0d23 | 2010-10-05 15:14:35 +0200 | [diff] [blame] | 134 | gfp_t gfp = GFP_KERNEL; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 135 | |
| 136 | desc = kzalloc_node(sizeof(*desc), gfp, node); |
| 137 | if (!desc) |
| 138 | return NULL; |
| 139 | /* allocate based on nr_cpu_ids */ |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 140 | desc->kstat_irqs = alloc_percpu(unsigned int); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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: |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 155 | free_percpu(desc->kstat_irqs); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 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); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 164 | |
Thomas Gleixner | 13bfe99 | 2010-09-30 02:46:07 +0200 | [diff] [blame] | 165 | unregister_irq_proc(irq, desc); |
| 166 | |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 167 | mutex_lock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 168 | delete_irq_desc(irq); |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 169 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 170 | |
| 171 | free_masks(desc); |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 172 | free_percpu(desc->kstat_irqs); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 173 | kfree(desc); |
| 174 | } |
| 175 | |
| 176 | static int alloc_descs(unsigned int start, unsigned int cnt, int node) |
| 177 | { |
| 178 | struct irq_desc *desc; |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < cnt; i++) { |
| 182 | desc = alloc_desc(start + i, node); |
| 183 | if (!desc) |
| 184 | goto err; |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 185 | mutex_lock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 186 | irq_insert_desc(start + i, desc); |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 187 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 188 | } |
| 189 | return start; |
| 190 | |
| 191 | err: |
| 192 | for (i--; i >= 0; i--) |
| 193 | free_desc(start + i); |
| 194 | |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 195 | mutex_lock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 196 | bitmap_clear(allocated_irqs, start, cnt); |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 197 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 198 | return -ENOMEM; |
| 199 | } |
| 200 | |
Thomas Gleixner | aa99ec0 | 2010-09-27 20:02:56 +0200 | [diff] [blame] | 201 | struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node) |
| 202 | { |
| 203 | int res = irq_alloc_descs(irq, irq, 1, node); |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 204 | |
Thomas Gleixner | aa99ec0 | 2010-09-27 20:02:56 +0200 | [diff] [blame] | 205 | if (res == -EEXIST || res == irq) |
| 206 | return irq_to_desc(irq); |
| 207 | return NULL; |
| 208 | } |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 209 | |
Thomas Gleixner | e7bcecb | 2011-02-16 17:12:57 +0100 | [diff] [blame] | 210 | static int irq_expand_nr_irqs(unsigned int cnt) |
| 211 | { |
| 212 | if (nr_irqs + cnt > IRQ_BITMAP_BITS) |
| 213 | return -ENOMEM; |
| 214 | nr_irqs += cnt; |
| 215 | return 0; |
| 216 | } |
| 217 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 218 | int __init early_irq_init(void) |
| 219 | { |
Thomas Gleixner | b683de2 | 2010-09-27 20:55:03 +0200 | [diff] [blame] | 220 | int i, initcnt, node = first_online_node; |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 221 | struct irq_desc *desc; |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 222 | |
| 223 | init_irq_default_affinity(); |
| 224 | |
Thomas Gleixner | b683de2 | 2010-09-27 20:55:03 +0200 | [diff] [blame] | 225 | /* Let arch update nr_irqs and return the nr of preallocated irqs */ |
| 226 | initcnt = arch_probe_nr_irqs(); |
| 227 | printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt); |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 228 | |
Thomas Gleixner | c1ee626 | 2011-02-17 17:45:15 +0100 | [diff] [blame] | 229 | if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS)) |
| 230 | nr_irqs = IRQ_BITMAP_BITS; |
| 231 | |
| 232 | if (WARN_ON(initcnt > IRQ_BITMAP_BITS)) |
| 233 | initcnt = IRQ_BITMAP_BITS; |
| 234 | |
| 235 | if (initcnt > nr_irqs) |
| 236 | nr_irqs = initcnt; |
| 237 | |
Thomas Gleixner | b683de2 | 2010-09-27 20:55:03 +0200 | [diff] [blame] | 238 | for (i = 0; i < initcnt; i++) { |
Thomas Gleixner | aa99ec0 | 2010-09-27 20:02:56 +0200 | [diff] [blame] | 239 | desc = alloc_desc(i, node); |
| 240 | set_bit(i, allocated_irqs); |
| 241 | irq_insert_desc(i, desc); |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 242 | } |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 243 | return arch_early_irq_init(); |
| 244 | } |
| 245 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 246 | #else /* !CONFIG_SPARSE_IRQ */ |
| 247 | |
| 248 | struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = { |
| 249 | [0 ... NR_IRQS-1] = { |
Thomas Gleixner | c1594b7 | 2011-02-07 22:11:30 +0100 | [diff] [blame] | 250 | .istate = IRQS_DISABLED, |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 251 | .handle_irq = handle_bad_irq, |
| 252 | .depth = 1, |
| 253 | .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock), |
| 254 | } |
| 255 | }; |
| 256 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 257 | int __init early_irq_init(void) |
| 258 | { |
Thomas Gleixner | aa99ec0 | 2010-09-27 20:02:56 +0200 | [diff] [blame] | 259 | int count, i, node = first_online_node; |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 260 | struct irq_desc *desc; |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 261 | |
| 262 | init_irq_default_affinity(); |
| 263 | |
| 264 | printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS); |
| 265 | |
| 266 | desc = irq_desc; |
| 267 | count = ARRAY_SIZE(irq_desc); |
| 268 | |
| 269 | for (i = 0; i < count; i++) { |
| 270 | desc[i].irq_data.irq = i; |
| 271 | desc[i].irq_data.chip = &no_irq_chip; |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 272 | desc[i].kstat_irqs = alloc_percpu(unsigned int); |
Thomas Gleixner | f9e4989 | 2011-02-09 14:54:49 +0100 | [diff] [blame^] | 273 | irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS); |
Thomas Gleixner | aa99ec0 | 2010-09-27 20:02:56 +0200 | [diff] [blame] | 274 | alloc_masks(desc + i, GFP_KERNEL, node); |
| 275 | desc_smp_init(desc + i, node); |
Thomas Gleixner | 154cd38 | 2010-09-22 15:58:45 +0200 | [diff] [blame] | 276 | lockdep_set_class(&desc[i].lock, &irq_desc_lock_class); |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 277 | } |
| 278 | return arch_early_irq_init(); |
| 279 | } |
| 280 | |
| 281 | struct irq_desc *irq_to_desc(unsigned int irq) |
| 282 | { |
| 283 | return (irq < NR_IRQS) ? irq_desc + irq : NULL; |
| 284 | } |
| 285 | |
| 286 | struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node) |
| 287 | { |
| 288 | return irq_to_desc(irq); |
| 289 | } |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 290 | |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 291 | static void free_desc(unsigned int irq) |
| 292 | { |
Thomas Gleixner | b7b2933 | 2010-09-29 18:46:55 +0200 | [diff] [blame] | 293 | dynamic_irq_cleanup(irq); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static inline int alloc_descs(unsigned int start, unsigned int cnt, int node) |
| 297 | { |
| 298 | return start; |
| 299 | } |
Thomas Gleixner | e7bcecb | 2011-02-16 17:12:57 +0100 | [diff] [blame] | 300 | |
| 301 | static int irq_expand_nr_irqs(unsigned int cnt) |
| 302 | { |
| 303 | return -ENOMEM; |
| 304 | } |
| 305 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 306 | #endif /* !CONFIG_SPARSE_IRQ */ |
| 307 | |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 308 | /* Dynamic interrupt handling */ |
| 309 | |
| 310 | /** |
| 311 | * irq_free_descs - free irq descriptors |
| 312 | * @from: Start of descriptor range |
| 313 | * @cnt: Number of consecutive irqs to free |
| 314 | */ |
| 315 | void irq_free_descs(unsigned int from, unsigned int cnt) |
| 316 | { |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 317 | int i; |
| 318 | |
| 319 | if (from >= nr_irqs || (from + cnt) > nr_irqs) |
| 320 | return; |
| 321 | |
| 322 | for (i = 0; i < cnt; i++) |
| 323 | free_desc(from + i); |
| 324 | |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 325 | mutex_lock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 326 | bitmap_clear(allocated_irqs, from, cnt); |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 327 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * irq_alloc_descs - allocate and initialize a range of irq descriptors |
| 332 | * @irq: Allocate for specific irq number if irq >= 0 |
| 333 | * @from: Start the search from this irq number |
| 334 | * @cnt: Number of consecutive irqs to allocate. |
| 335 | * @node: Preferred node on which the irq descriptor should be allocated |
| 336 | * |
| 337 | * Returns the first irq number or error code |
| 338 | */ |
| 339 | int __ref |
| 340 | irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node) |
| 341 | { |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 342 | int start, ret; |
| 343 | |
| 344 | if (!cnt) |
| 345 | return -EINVAL; |
| 346 | |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 347 | mutex_lock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 348 | |
| 349 | start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0); |
| 350 | ret = -EEXIST; |
| 351 | if (irq >=0 && start != irq) |
| 352 | goto err; |
| 353 | |
Thomas Gleixner | e7bcecb | 2011-02-16 17:12:57 +0100 | [diff] [blame] | 354 | if (start >= nr_irqs) { |
| 355 | ret = irq_expand_nr_irqs(cnt); |
| 356 | if (ret) |
| 357 | goto err; |
| 358 | } |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 359 | |
| 360 | bitmap_set(allocated_irqs, start, cnt); |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 361 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 362 | return alloc_descs(start, cnt, node); |
| 363 | |
| 364 | err: |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 365 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 1f5a5b8 | 2010-09-27 17:48:26 +0200 | [diff] [blame] | 366 | return ret; |
| 367 | } |
| 368 | |
Thomas Gleixner | a98d24b | 2010-09-30 10:45:07 +0200 | [diff] [blame] | 369 | /** |
Thomas Gleixner | 06f6c33 | 2010-10-12 12:31:46 +0200 | [diff] [blame] | 370 | * irq_reserve_irqs - mark irqs allocated |
| 371 | * @from: mark from irq number |
| 372 | * @cnt: number of irqs to mark |
| 373 | * |
| 374 | * Returns 0 on success or an appropriate error code |
| 375 | */ |
| 376 | int irq_reserve_irqs(unsigned int from, unsigned int cnt) |
| 377 | { |
Thomas Gleixner | 06f6c33 | 2010-10-12 12:31:46 +0200 | [diff] [blame] | 378 | unsigned int start; |
| 379 | int ret = 0; |
| 380 | |
| 381 | if (!cnt || (from + cnt) > nr_irqs) |
| 382 | return -EINVAL; |
| 383 | |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 384 | mutex_lock(&sparse_irq_lock); |
Thomas Gleixner | 06f6c33 | 2010-10-12 12:31:46 +0200 | [diff] [blame] | 385 | start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0); |
| 386 | if (start == from) |
| 387 | bitmap_set(allocated_irqs, start, cnt); |
| 388 | else |
| 389 | ret = -EEXIST; |
Thomas Gleixner | a05a900 | 2010-10-08 12:47:53 +0200 | [diff] [blame] | 390 | mutex_unlock(&sparse_irq_lock); |
Thomas Gleixner | 06f6c33 | 2010-10-12 12:31:46 +0200 | [diff] [blame] | 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | /** |
Thomas Gleixner | a98d24b | 2010-09-30 10:45:07 +0200 | [diff] [blame] | 395 | * irq_get_next_irq - get next allocated irq number |
| 396 | * @offset: where to start the search |
| 397 | * |
| 398 | * Returns next irq number after offset or nr_irqs if none is found. |
| 399 | */ |
| 400 | unsigned int irq_get_next_irq(unsigned int offset) |
| 401 | { |
| 402 | return find_next_bit(allocated_irqs, nr_irqs, offset); |
| 403 | } |
| 404 | |
Thomas Gleixner | b7b2933 | 2010-09-29 18:46:55 +0200 | [diff] [blame] | 405 | /** |
| 406 | * dynamic_irq_cleanup - cleanup a dynamically allocated irq |
| 407 | * @irq: irq number to initialize |
| 408 | */ |
| 409 | void dynamic_irq_cleanup(unsigned int irq) |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 410 | { |
Thomas Gleixner | b7b2933 | 2010-09-29 18:46:55 +0200 | [diff] [blame] | 411 | struct irq_desc *desc = irq_to_desc(irq); |
| 412 | unsigned long flags; |
| 413 | |
| 414 | raw_spin_lock_irqsave(&desc->lock, flags); |
| 415 | desc_set_defaults(irq, desc, desc_node(desc)); |
| 416 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 419 | unsigned int kstat_irqs_cpu(unsigned int irq, int cpu) |
| 420 | { |
| 421 | struct irq_desc *desc = irq_to_desc(irq); |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 422 | |
| 423 | return desc && desc->kstat_irqs ? |
| 424 | *per_cpu_ptr(desc->kstat_irqs, cpu) : 0; |
Thomas Gleixner | 3795de2 | 2010-09-22 17:09:43 +0200 | [diff] [blame] | 425 | } |
KAMEZAWA Hiroyuki | 478735e3 | 2010-10-27 15:34:15 -0700 | [diff] [blame] | 426 | |
| 427 | #ifdef CONFIG_GENERIC_HARDIRQS |
| 428 | unsigned int kstat_irqs(unsigned int irq) |
| 429 | { |
| 430 | struct irq_desc *desc = irq_to_desc(irq); |
| 431 | int cpu; |
| 432 | int sum = 0; |
| 433 | |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 434 | if (!desc || !desc->kstat_irqs) |
KAMEZAWA Hiroyuki | 478735e3 | 2010-10-27 15:34:15 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | for_each_possible_cpu(cpu) |
Eric Dumazet | 6c9ae00 | 2011-01-13 15:45:38 -0800 | [diff] [blame] | 437 | sum += *per_cpu_ptr(desc->kstat_irqs, cpu); |
KAMEZAWA Hiroyuki | 478735e3 | 2010-10-27 15:34:15 -0700 | [diff] [blame] | 438 | return sum; |
| 439 | } |
| 440 | #endif /* CONFIG_GENERIC_HARDIRQS */ |