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