1 /* 2 * linux/kernel/irq/manage.c 3 * 4 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar 5 * 6 * This file contains driver APIs to the irq subsystem. 7 */ 8 9 #include <linux/config.h> 10 #include <linux/irq.h> 11 #include <linux/module.h> 12 #include <linux/random.h> 13 #include <linux/interrupt.h> 14 15 #include "internals.h" 16 17 #ifdef CONFIG_SMP 18 19 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL }; 20 21 /** 22 * synchronize_irq - wait for pending IRQ handlers (on other CPUs) 23 * 24 * This function waits for any pending IRQ handlers for this interrupt 25 * to complete before returning. If you use this function while 26 * holding a resource the IRQ handler may need you will deadlock. 27 * 28 * This function may be called - with care - from IRQ context. 29 */ 30 void synchronize_irq(unsigned int irq) 31 { 32 struct irq_desc *desc = irq_desc + irq; 33 34 while (desc->status & IRQ_INPROGRESS) 35 cpu_relax(); 36 } 37 38 EXPORT_SYMBOL(synchronize_irq); 39 40 #endif 41 42 /** 43 * disable_irq_nosync - disable an irq without waiting 44 * @irq: Interrupt to disable 45 * 46 * Disable the selected interrupt line. Disables and Enables are 47 * nested. 48 * Unlike disable_irq(), this function does not ensure existing 49 * instances of the IRQ handler have completed before returning. 50 * 51 * This function may be called from IRQ context. 52 */ 53 void disable_irq_nosync(unsigned int irq) 54 { 55 irq_desc_t *desc = irq_desc + irq; 56 unsigned long flags; 57 58 spin_lock_irqsave(&desc->lock, flags); 59 if (!desc->depth++) { 60 desc->status |= IRQ_DISABLED; 61 desc->handler->disable(irq); 62 } 63 spin_unlock_irqrestore(&desc->lock, flags); 64 } 65 66 EXPORT_SYMBOL(disable_irq_nosync); 67 68 /** 69 * disable_irq - disable an irq and wait for completion 70 * @irq: Interrupt to disable 71 * 72 * Disable the selected interrupt line. Enables and Disables are 73 * nested. 74 * This function waits for any pending IRQ handlers for this interrupt 75 * to complete before returning. If you use this function while 76 * holding a resource the IRQ handler may need you will deadlock. 77 * 78 * This function may be called - with care - from IRQ context. 79 */ 80 void disable_irq(unsigned int irq) 81 { 82 irq_desc_t *desc = irq_desc + irq; 83 84 disable_irq_nosync(irq); 85 if (desc->action) 86 synchronize_irq(irq); 87 } 88 89 EXPORT_SYMBOL(disable_irq); 90 91 /** 92 * enable_irq - enable handling of an irq 93 * @irq: Interrupt to enable 94 * 95 * Undoes the effect of one call to disable_irq(). If this 96 * matches the last disable, processing of interrupts on this 97 * IRQ line is re-enabled. 98 * 99 * This function may be called from IRQ context. 100 */ 101 void enable_irq(unsigned int irq) 102 { 103 irq_desc_t *desc = irq_desc + irq; 104 unsigned long flags; 105 106 spin_lock_irqsave(&desc->lock, flags); 107 switch (desc->depth) { 108 case 0: 109 WARN_ON(1); 110 break; 111 case 1: { 112 unsigned int status = desc->status & ~IRQ_DISABLED; 113 114 desc->status = status; 115 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) { 116 desc->status = status | IRQ_REPLAY; 117 hw_resend_irq(desc->handler,irq); 118 } 119 desc->handler->enable(irq); 120 /* fall-through */ 121 } 122 default: 123 desc->depth--; 124 } 125 spin_unlock_irqrestore(&desc->lock, flags); 126 } 127 128 EXPORT_SYMBOL(enable_irq); 129 130 /* 131 * Internal function that tells the architecture code whether a 132 * particular irq has been exclusively allocated or is available 133 * for driver use. 134 */ 135 int can_request_irq(unsigned int irq, unsigned long irqflags) 136 { 137 struct irqaction *action; 138 139 if (irq >= NR_IRQS) 140 return 0; 141 142 action = irq_desc[irq].action; 143 if (action) 144 if (irqflags & action->flags & SA_SHIRQ) 145 action = NULL; 146 147 return !action; 148 } 149 150 /* 151 * Internal function to register an irqaction - typically used to 152 * allocate special interrupts that are part of the architecture. 153 */ 154 int setup_irq(unsigned int irq, struct irqaction * new) 155 { 156 struct irq_desc *desc = irq_desc + irq; 157 struct irqaction *old, **p; 158 unsigned long flags; 159 int shared = 0; 160 161 if (desc->handler == &no_irq_type) 162 return -ENOSYS; 163 /* 164 * Some drivers like serial.c use request_irq() heavily, 165 * so we have to be careful not to interfere with a 166 * running system. 167 */ 168 if (new->flags & SA_SAMPLE_RANDOM) { 169 /* 170 * This function might sleep, we want to call it first, 171 * outside of the atomic block. 172 * Yes, this might clear the entropy pool if the wrong 173 * driver is attempted to be loaded, without actually 174 * installing a new handler, but is this really a problem, 175 * only the sysadmin is able to do this. 176 */ 177 rand_initialize_irq(irq); 178 } 179 180 /* 181 * The following block of code has to be executed atomically 182 */ 183 spin_lock_irqsave(&desc->lock,flags); 184 p = &desc->action; 185 if ((old = *p) != NULL) { 186 /* Can't share interrupts unless both agree to */ 187 if (!(old->flags & new->flags & SA_SHIRQ)) { 188 spin_unlock_irqrestore(&desc->lock,flags); 189 return -EBUSY; 190 } 191 192 /* add new interrupt at end of irq queue */ 193 do { 194 p = &old->next; 195 old = *p; 196 } while (old); 197 shared = 1; 198 } 199 200 *p = new; 201 202 if (!shared) { 203 desc->depth = 0; 204 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | 205 IRQ_WAITING | IRQ_INPROGRESS); 206 if (desc->handler->startup) 207 desc->handler->startup(irq); 208 else 209 desc->handler->enable(irq); 210 } 211 spin_unlock_irqrestore(&desc->lock,flags); 212 213 new->irq = irq; 214 register_irq_proc(irq); 215 new->dir = NULL; 216 register_handler_proc(irq, new); 217 218 return 0; 219 } 220 221 /** 222 * free_irq - free an interrupt 223 * @irq: Interrupt line to free 224 * @dev_id: Device identity to free 225 * 226 * Remove an interrupt handler. The handler is removed and if the 227 * interrupt line is no longer in use by any driver it is disabled. 228 * On a shared IRQ the caller must ensure the interrupt is disabled 229 * on the card it drives before calling this function. The function 230 * does not return until any executing interrupts for this IRQ 231 * have completed. 232 * 233 * This function must not be called from interrupt context. 234 */ 235 void free_irq(unsigned int irq, void *dev_id) 236 { 237 struct irq_desc *desc; 238 struct irqaction **p; 239 unsigned long flags; 240 241 if (irq >= NR_IRQS) 242 return; 243 244 desc = irq_desc + irq; 245 spin_lock_irqsave(&desc->lock,flags); 246 p = &desc->action; 247 for (;;) { 248 struct irqaction * action = *p; 249 250 if (action) { 251 struct irqaction **pp = p; 252 253 p = &action->next; 254 if (action->dev_id != dev_id) 255 continue; 256 257 /* Found it - now remove it from the list of entries */ 258 *pp = action->next; 259 260 /* Currently used only by UML, might disappear one day.*/ 261 #ifdef CONFIG_IRQ_RELEASE_METHOD 262 if (desc->handler->release) 263 desc->handler->release(irq, dev_id); 264 #endif 265 266 if (!desc->action) { 267 desc->status |= IRQ_DISABLED; 268 if (desc->handler->shutdown) 269 desc->handler->shutdown(irq); 270 else 271 desc->handler->disable(irq); 272 } 273 spin_unlock_irqrestore(&desc->lock,flags); 274 unregister_handler_proc(irq, action); 275 276 /* Make sure it's not being used on another CPU */ 277 synchronize_irq(irq); 278 kfree(action); 279 return; 280 } 281 printk(KERN_ERR "Trying to free free IRQ%d\n",irq); 282 spin_unlock_irqrestore(&desc->lock,flags); 283 return; 284 } 285 } 286 287 EXPORT_SYMBOL(free_irq); 288 289 /** 290 * request_irq - allocate an interrupt line 291 * @irq: Interrupt line to allocate 292 * @handler: Function to be called when the IRQ occurs 293 * @irqflags: Interrupt type flags 294 * @devname: An ascii name for the claiming device 295 * @dev_id: A cookie passed back to the handler function 296 * 297 * This call allocates interrupt resources and enables the 298 * interrupt line and IRQ handling. From the point this 299 * call is made your handler function may be invoked. Since 300 * your handler function must clear any interrupt the board 301 * raises, you must take care both to initialise your hardware 302 * and to set up the interrupt handler in the right order. 303 * 304 * Dev_id must be globally unique. Normally the address of the 305 * device data structure is used as the cookie. Since the handler 306 * receives this value it makes sense to use it. 307 * 308 * If your interrupt is shared you must pass a non NULL dev_id 309 * as this is required when freeing the interrupt. 310 * 311 * Flags: 312 * 313 * SA_SHIRQ Interrupt is shared 314 * SA_INTERRUPT Disable local interrupts while processing 315 * SA_SAMPLE_RANDOM The interrupt can be used for entropy 316 * 317 */ 318 int request_irq(unsigned int irq, 319 irqreturn_t (*handler)(int, void *, struct pt_regs *), 320 unsigned long irqflags, const char * devname, void *dev_id) 321 { 322 struct irqaction * action; 323 int retval; 324 325 /* 326 * Sanity-check: shared interrupts must pass in a real dev-ID, 327 * otherwise we'll have trouble later trying to figure out 328 * which interrupt is which (messes up the interrupt freeing 329 * logic etc). 330 */ 331 if ((irqflags & SA_SHIRQ) && !dev_id) 332 return -EINVAL; 333 if (irq >= NR_IRQS) 334 return -EINVAL; 335 if (!handler) 336 return -EINVAL; 337 338 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC); 339 if (!action) 340 return -ENOMEM; 341 342 action->handler = handler; 343 action->flags = irqflags; 344 cpus_clear(action->mask); 345 action->name = devname; 346 action->next = NULL; 347 action->dev_id = dev_id; 348 349 retval = setup_irq(irq, action); 350 if (retval) 351 kfree(action); 352 353 return retval; 354 } 355 356 EXPORT_SYMBOL(request_irq); 357 358