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