1 /*
2 * linux/kernel/irq/manage.c
3 *
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006 Thomas Gleixner
6 *
7 * This file contains driver APIs to the irq subsystem.
8 */
9
10 #include <linux/irq.h>
11 #include <linux/random.h>
12 #include <linux/module.h>
13 #include <linux/kthread.h>
14 #include <linux/syscalls.h>
15 #include <linux/interrupt.h>
16
17 #include "internals.h"
18
19 #ifdef CONFIG_SMP
20
21 /**
22 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
23 * @irq: interrupt number to wait for
24 *
25 * This function waits for any pending IRQ handlers for this interrupt
26 * to complete before returning. If you use this function while
27 * holding a resource the IRQ handler may need you will deadlock.
28 *
29 * This function may be called - with care - from IRQ context.
30 */
31 void synchronize_irq(unsigned int irq)
32 {
33 struct irq_desc *desc = irq_desc + irq;
34 unsigned int status;
35
36 if (irq >= NR_IRQS)
37 return;
38
39 do {
40 unsigned long flags;
41
42 /*
43 * Wait until we're out of the critical section. This might
44 * give the wrong answer due to the lack of memory barriers.
45 */
46 if (hardirq_preemption && !(desc->status & IRQ_NODELAY))
47 wait_event(desc->wait_for_handler,
48 !(desc->status & IRQ_INPROGRESS));
49 else
50 while (desc->status & IRQ_INPROGRESS)
51 cpu_relax();
52
53 /* Ok, that indicated we're done: double-check carefully. */
54 spin_lock_irqsave(&desc->lock, flags);
55 status = desc->status;
56 spin_unlock_irqrestore(&desc->lock, flags);
57
58 /* Oops, that failed? */
59 } while (status & IRQ_INPROGRESS);
60 }
61 EXPORT_SYMBOL(synchronize_irq);
62
63 /**
64 * irq_can_set_affinity - Check if the affinity of a given irq can be set
65 * @irq: Interrupt to check
66 *
67 */
68 int irq_can_set_affinity(unsigned int irq)
69 {
70 struct irq_desc *desc = irq_desc + irq;
71
72 if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip ||
73 !desc->chip->set_affinity)
74 return 0;
75
76 return 1;
77 }
78
79 /**
80 * irq_set_affinity - Set the irq affinity of a given irq
81 * @irq: Interrupt to set affinity
82 * @cpumask: cpumask
83 *
84 */
85 int irq_set_affinity(unsigned int irq, cpumask_t cpumask)
86 {
87 struct irq_desc *desc = irq_desc + irq;
88
89 if (!desc->chip->set_affinity)
90 return -EINVAL;
91
92 set_balance_irq_affinity(irq, cpumask);
93
94 #ifdef CONFIG_GENERIC_PENDING_IRQ
95 set_pending_irq(irq, cpumask);
96 #else
97 desc->affinity = cpumask;
98 desc->chip->set_affinity(irq, cpumask);
99 #endif
100 return 0;
101 }
102
103 #endif
104
105 /**
106 * disable_irq_nosync - disable an irq without waiting
107 * @irq: Interrupt to disable
108 *
109 * Disable the selected interrupt line. Disables and Enables are
110 * nested.
111 * Unlike disable_irq(), this function does not ensure existing
112 * instances of the IRQ handler have completed before returning.
113 *
114 * This function may be called from IRQ context.
115 */
116 void disable_irq_nosync(unsigned int irq)
117 {
118 struct irq_desc *desc = irq_desc + irq;
119 unsigned long flags;
120
121 if (irq >= NR_IRQS)
122 return;
123
124 spin_lock_irqsave(&desc->lock, flags);
125 if (!desc->depth++) {
126 desc->status |= IRQ_DISABLED;
127 desc->chip->disable(irq);
128 }
129 spin_unlock_irqrestore(&desc->lock, flags);
130 }
131 EXPORT_SYMBOL(disable_irq_nosync);
132
133 /**
134 * disable_irq - disable an irq and wait for completion
135 * @irq: Interrupt to disable
136 *
137 * Disable the selected interrupt line. Enables and Disables are
138 * nested.
139 * This function waits for any pending IRQ handlers for this interrupt
140 * to complete before returning. If you use this function while
141 * holding a resource the IRQ handler may need you will deadlock.
142 *
143 * This function may be called - with care - from IRQ context.
144 */
145 void disable_irq(unsigned int irq)
146 {
147 struct irq_desc *desc = irq_desc + irq;
148
149 if (irq >= NR_IRQS)
150 return;
151
152 disable_irq_nosync(irq);
153 if (desc->action)
154 synchronize_irq(irq);
155 }
156 EXPORT_SYMBOL(disable_irq);
157
158 /**
159 * enable_irq - enable handling of an irq
160 * @irq: Interrupt to enable
161 *
162 * Undoes the effect of one call to disable_irq(). If this
163 * matches the last disable, processing of interrupts on this
164 * IRQ line is re-enabled.
165 *
166 * This function may be called from IRQ context.
167 */
168 void enable_irq(unsigned int irq)
169 {
170 struct irq_desc *desc = irq_desc + irq;
171 unsigned long flags;
172
173 if (irq >= NR_IRQS)
174 return;
175
176 spin_lock_irqsave(&desc->lock, flags);
177 switch (desc->depth) {
178 case 0:
179 printk(KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
180 WARN_ON(1);
181 break;
182 case 1: {
183 unsigned int status = desc->status & ~IRQ_DISABLED;
184
185 /* Prevent probing on this irq: */
186 desc->status = status | IRQ_NOPROBE;
187 check_irq_resend(desc, irq);
188 /* fall-through */
189 }
190 default:
191 desc->depth--;
192 }
193 spin_unlock_irqrestore(&desc->lock, flags);
194 #ifdef CONFIG_HARDIRQS_SW_RESEND
195 /*
196 * Do a bh disable/enable pair to trigger any pending
197 * irq resend logic:
198 */
199 local_bh_disable();
200 local_bh_enable();
201 #endif
202 }
203 EXPORT_SYMBOL(enable_irq);
204
205 /**
206 * set_irq_wake - control irq power management wakeup
207 * @irq: interrupt to control
208 * @on: enable/disable power management wakeup
209 *
210 * Enable/disable power management wakeup mode, which is
211 * disabled by default. Enables and disables must match,
212 * just as they match for non-wakeup mode support.
213 *
214 * Wakeup mode lets this IRQ wake the system from sleep
215 * states like "suspend to RAM".
216 */
217 int set_irq_wake(unsigned int irq, unsigned int on)
218 {
219 struct irq_desc *desc = irq_desc + irq;
220 unsigned long flags;
221 int ret = -ENXIO;
222 int (*set_wake)(unsigned, unsigned) = desc->chip->set_wake;
223
224 /* wakeup-capable irqs can be shared between drivers that
225 * don't need to have the same sleep mode behaviors.
226 */
227 spin_lock_irqsave(&desc->lock, flags);
228 if (on) {
229 if (desc->wake_depth++ == 0)
230 desc->status |= IRQ_WAKEUP;
231 else
232 set_wake = NULL;
233 } else {
234 if (desc->wake_depth == 0) {
235 printk(KERN_WARNING "Unbalanced IRQ %d "
236 "wake disable\n", irq);
237 WARN_ON(1);
238 } else if (--desc->wake_depth == 0)
239 desc->status &= ~IRQ_WAKEUP;
240 else
241 set_wake = NULL;
242 }
243 if (set_wake)
244 ret = desc->chip->set_wake(irq, on);
245 spin_unlock_irqrestore(&desc->lock, flags);
246 return ret;
247 }
248 EXPORT_SYMBOL(set_irq_wake);
249
250 /*
251 * If any action has IRQF_NODELAY then turn IRQ_NODELAY on:
252 */
253 void recalculate_desc_flags(struct irq_desc *desc)
254 {
255 struct irqaction *action;
256
257 desc->status &= ~IRQ_NODELAY;
258 for (action = desc->action ; action; action = action->next)
259 if (action->flags & IRQF_NODELAY)
260 desc->status |= IRQ_NODELAY;
261 }
262
263 static int start_irq_thread(int irq, struct irq_desc *desc);
264
265 /*
266 * Internal function that tells the architecture code whether a
267 * particular irq has been exclusively allocated or is available
268 * for driver use.
269 */
270 int can_request_irq(unsigned int irq, unsigned long irqflags)
271 {
272 struct irqaction *action;
273
274 if (irq >= NR_IRQS || irq_desc[irq].status & IRQ_NOREQUEST)
275 return 0;
276
277 action = irq_desc[irq].action;
278 if (action)
279 if (irqflags & action->flags & IRQF_SHARED)
280 action = NULL;
281
282 return !action;
283 }
284
285 void compat_irq_chip_set_default_handler(struct irq_desc *desc)
286 {
287 /*
288 * If the architecture still has not overriden
289 * the flow handler then zap the default. This
290 * should catch incorrect flow-type setting.
291 */
292 if (desc->handle_irq == &handle_bad_irq)
293 desc->handle_irq = NULL;
294 }
295
296 /*
297 * Internal function to register an irqaction - typically used to
298 * allocate special interrupts that are part of the architecture.
299 */
300 int setup_irq(unsigned int irq, struct irqaction *new)
301 {
302 struct irq_desc *desc = irq_desc + irq;
303 struct irqaction *old, **p;
304 const char *old_name = NULL;
305 unsigned long flags;
306 int shared = 0;
307
308 if (irq >= NR_IRQS)
309 return -EINVAL;
310
311 if (desc->chip == &no_irq_chip)
312 return -ENOSYS;
313 /*
314 * Some drivers like serial.c use request_irq() heavily,
315 * so we have to be careful not to interfere with a
316 * running system.
317 */
318 if (new->flags & IRQF_SAMPLE_RANDOM) {
319 /*
320 * This function might sleep, we want to call it first,
321 * outside of the atomic block.
322 * Yes, this might clear the entropy pool if the wrong
323 * driver is attempted to be loaded, without actually
324 * installing a new handler, but is this really a problem,
325 * only the sysadmin is able to do this.
326 */
327 rand_initialize_irq(irq);
328 }
329
330 if (!(new->flags & IRQF_NODELAY))
331 if (start_irq_thread(irq, desc))
332 return -ENOMEM;
333 /*
334 * The following block of code has to be executed atomically
335 */
336 spin_lock_irqsave(&desc->lock, flags);
337 p = &desc->action;
338 old = *p;
339 if (old) {
340 /*
341 * Can't share interrupts unless both agree to and are
342 * the same type (level, edge, polarity). So both flag
343 * fields must have IRQF_SHARED set and the bits which
344 * set the trigger type must match.
345 */
346 if (!((old->flags & new->flags) & IRQF_SHARED) ||
347 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
348 old_name = old->name;
349 goto mismatch;
350 }
351
352 #if defined(CONFIG_IRQ_PER_CPU)
353 /* All handlers must agree on per-cpuness */
354 if ((old->flags & IRQF_PERCPU) !=
355 (new->flags & IRQF_PERCPU))
356 goto mismatch;
357 #endif
358
359 /* add new interrupt at end of irq queue */
360 do {
361 p = &old->next;
362 old = *p;
363 } while (old);
364 shared = 1;
365 }
366
367 *p = new;
368
369 /* Exclude IRQ from balancing */
370 if (new->flags & IRQF_NOBALANCING)
371 desc->status |= IRQ_NO_BALANCING;
372
373 /*
374 * Propagate any possible IRQF_NODELAY flag into IRQ_NODELAY:
375 */
376 recalculate_desc_flags(desc);
377
378 if (!shared) {
379 irq_chip_set_defaults(desc->chip);
380
381 #if defined(CONFIG_IRQ_PER_CPU)
382 if (new->flags & IRQF_PERCPU)
383 desc->status |= IRQ_PER_CPU;
384 #endif
385
386 /* Setup the type (level, edge polarity) if configured: */
387 if (new->flags & IRQF_TRIGGER_MASK) {
388 if (desc->chip && desc->chip->set_type)
389 desc->chip->set_type(irq,
390 new->flags & IRQF_TRIGGER_MASK);
391 else
392 /*
393 * IRQF_TRIGGER_* but the PIC does not support
394 * multiple flow-types?
395 */
396 printk(KERN_WARNING "No IRQF_TRIGGER set_type "
397 "function for IRQ %d (%s)\n", irq,
398 desc->chip ? desc->chip->name :
399 "unknown");
400 } else
401 compat_irq_chip_set_default_handler(desc);
402
403 desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
404 IRQ_INPROGRESS);
405
406 if (!(desc->status & IRQ_NOAUTOEN)) {
407 desc->depth = 0;
408 desc->status &= ~IRQ_DISABLED;
409 if (desc->chip->startup)
410 desc->chip->startup(irq);
411 else
412 desc->chip->enable(irq);
413 } else
414 /* Undo nested disables: */
415 desc->depth = 1;
416 }
417 /* Reset broken irq detection when installing new handler */
418 desc->irq_count = 0;
419 desc->irqs_unhandled = 0;
420 spin_unlock_irqrestore(&desc->lock, flags);
421
422 new->irq = irq;
423 register_irq_proc(irq);
424 new->dir = new->threaded = NULL;
425 register_handler_proc(irq, new);
426
427 return 0;
428
429 mismatch:
430 #ifdef CONFIG_DEBUG_SHIRQ
431 if (!(new->flags & IRQF_PROBE_SHARED)) {
432 printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
433 if (old_name)
434 printk(KERN_ERR "current handler: %s\n", old_name);
435 dump_stack();
436 }
437 #endif
438 spin_unlock_irqrestore(&desc->lock, flags);
439 return -EBUSY;
440 }
441
442 /**
443 * free_irq - free an interrupt
444 * @irq: Interrupt line to free
445 * @dev_id: Device identity to free
446 *
447 * Remove an interrupt handler. The handler is removed and if the
448 * interrupt line is no longer in use by any driver it is disabled.
449 * On a shared IRQ the caller must ensure the interrupt is disabled
450 * on the card it drives before calling this function. The function
451 * does not return until any executing interrupts for this IRQ
452 * have completed.
453 *
454 * This function must not be called from interrupt context.
455 */
456 void free_irq(unsigned int irq, void *dev_id)
457 {
458 struct irq_desc *desc;
459 struct irqaction **p;
460 unsigned long flags;
461
462 WARN_ON(in_interrupt());
463 if (irq >= NR_IRQS)
464 return;
465
466 desc = irq_desc + irq;
467 spin_lock_irqsave(&desc->lock, flags);
468 p = &desc->action;
469 for (;;) {
470 struct irqaction *action = *p;
471
472 if (action) {
473 struct irqaction **pp = p;
474
475 p = &action->next;
476 if (action->dev_id != dev_id)
477 continue;
478
479 /* Found it - now remove it from the list of entries */
480 *pp = action->next;
481
482 /* Currently used only by UML, might disappear one day.*/
483 #ifdef CONFIG_IRQ_RELEASE_METHOD
484 if (desc->chip->release)
485 desc->chip->release(irq, dev_id);
486 #endif
487
488 if (!desc->action) {
489 desc->status |= IRQ_DISABLED;
490 if (desc->chip->shutdown)
491 desc->chip->shutdown(irq);
492 else
493 desc->chip->disable(irq);
494 }
495 recalculate_desc_flags(desc);
496 spin_unlock_irqrestore(&desc->lock, flags);
497 unregister_handler_proc(irq, action);
498
499 /* Make sure it's not being used on another CPU */
500 synchronize_irq(irq);
501 #ifdef CONFIG_DEBUG_SHIRQ
502 /*
503 * It's a shared IRQ -- the driver ought to be
504 * prepared for it to happen even now it's
505 * being freed, so let's make sure.... We do
506 * this after actually deregistering it, to
507 * make sure that a 'real' IRQ doesn't run in
508 * parallel with our fake
509 */
510 if (action->flags & IRQF_SHARED) {
511 local_irq_save_nort(flags);
512 action->handler(irq, dev_id);
513 local_irq_restore_nort(flags);
514 }
515 #endif
516 kfree(action);
517 return;
518 }
519 printk(KERN_ERR "Trying to free already-free IRQ %d\n", irq);
520 #ifdef CONFIG_DEBUG_SHIRQ
521 dump_stack();
522 #endif
523 spin_unlock_irqrestore(&desc->lock, flags);
524 return;
525 }
526 }
527 EXPORT_SYMBOL(free_irq);
528
529 /**
530 * request_irq - allocate an interrupt line
531 * @irq: Interrupt line to allocate
532 * @handler: Function to be called when the IRQ occurs
533 * @irqflags: Interrupt type flags
534 * @devname: An ascii name for the claiming device
535 * @dev_id: A cookie passed back to the handler function
536 *
537 * This call allocates interrupt resources and enables the
538 * interrupt line and IRQ handling. From the point this
539 * call is made your handler function may be invoked. Since
540 * your handler function must clear any interrupt the board
541 * raises, you must take care both to initialise your hardware
542 * and to set up the interrupt handler in the right order.
543 *
544 * Dev_id must be globally unique. Normally the address of the
545 * device data structure is used as the cookie. Since the handler
546 * receives this value it makes sense to use it.
547 *
548 * If your interrupt is shared you must pass a non NULL dev_id
549 * as this is required when freeing the interrupt.
550 *
551 * Flags:
552 *
553 * IRQF_SHARED Interrupt is shared
554 * IRQF_DISABLED Disable local interrupts while processing
555 * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
556 *
557 */
558 int request_irq(unsigned int irq, irq_handler_t handler,
559 unsigned long irqflags, const char *devname, void *dev_id)
560 {
561 struct irqaction *action;
562 int retval;
563
564 #ifdef CONFIG_LOCKDEP
565 /*
566 * Lockdep wants atomic interrupt handlers:
567 */
568 irqflags |= IRQF_DISABLED;
569 #endif
570 /*
571 * Sanity-check: shared interrupts must pass in a real dev-ID,
572 * otherwise we'll have trouble later trying to figure out
573 * which interrupt is which (messes up the interrupt freeing
574 * logic etc).
575 */
576 if ((irqflags & IRQF_SHARED) && !dev_id)
577 return -EINVAL;
578 if (irq >= NR_IRQS)
579 return -EINVAL;
580 if (irq_desc[irq].status & IRQ_NOREQUEST)
581 return -EINVAL;
582 if (!handler)
583 return -EINVAL;
584
585 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
586 if (!action)
587 return -ENOMEM;
588
589 action->handler = handler;
590 action->flags = irqflags;
591 cpus_clear(action->mask);
592 action->name = devname;
593 action->next = NULL;
594 action->dev_id = dev_id;
595
596 select_smp_affinity(irq);
597
598 #ifdef CONFIG_DEBUG_SHIRQ
599 if (irqflags & IRQF_SHARED) {
600 /*
601 * It's a shared IRQ -- the driver ought to be prepared for it
602 * to happen immediately, so let's make sure....
603 * We do this before actually registering it, to make sure that
604 * a 'real' IRQ doesn't run in parallel with our fake
605 */
606 unsigned long flags;
607
608 local_irq_save_nort(flags);
609 handler(irq, dev_id);
610 local_irq_restore_nort(flags);
611 }
612 #endif
613
614 retval = setup_irq(irq, action);
615 if (retval)
616 kfree(action);
617
618 return retval;
619 }
620 EXPORT_SYMBOL(request_irq);
621
622 #ifdef CONFIG_PREEMPT_HARDIRQS
623
624 int hardirq_preemption = 1;
625
626 EXPORT_SYMBOL(hardirq_preemption);
627
628 /*
629 * Real-Time Preemption depends on hardirq threading:
630 */
631 #ifndef CONFIG_PREEMPT_RT
632
633 static int __init hardirq_preempt_setup (char *str)
634 {
635 if (!strncmp(str, "off", 3))
636 hardirq_preemption = 0;
637 else
638 get_option(&str, &hardirq_preemption);
639 if (!hardirq_preemption)
640 printk("turning off hardirq preemption!\n");
641
642 return 1;
643 }
644
645 __setup("hardirq-preempt=", hardirq_preempt_setup);
646
647 #endif
648
649 /*
650 * threaded simple handler
651 */
652 static void thread_simple_irq(irq_desc_t *desc)
653 {
654 struct irqaction *action = desc->action;
655 unsigned int irq = desc - irq_desc;
656 irqreturn_t action_ret;
657
658 do {
659 if (!action || desc->depth)
660 break;
661 desc->status &= ~IRQ_PENDING;
662 spin_unlock(&desc->lock);
663 action_ret = handle_IRQ_event(irq, action);
664 cond_resched_hardirq_context();
665 spin_lock_irq(&desc->lock);
666 if (!noirqdebug)
667 note_interrupt(irq, desc, action_ret);
668 } while (desc->status & IRQ_PENDING);
669 desc->status &= ~IRQ_INPROGRESS;
670 }
671
672 /*
673 * threaded level type irq handler
674 */
675 static void thread_level_irq(irq_desc_t *desc)
676 {
677 unsigned int irq = desc - irq_desc;
678
679 thread_simple_irq(desc);
680 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
681 desc->chip->unmask(irq);
682 }
683
684 /*
685 * threaded fasteoi type irq handler
686 */
687 static void thread_fasteoi_irq(irq_desc_t *desc)
688 {
689 unsigned int irq = desc - irq_desc;
690
691 thread_simple_irq(desc);
692 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
693 desc->chip->unmask(irq);
694 }
695
696 /*
697 * threaded edge type IRQ handler
698 */
699 static void thread_edge_irq(irq_desc_t *desc)
700 {
701 unsigned int irq = desc - irq_desc;
702
703 do {
704 struct irqaction *action = desc->action;
705 irqreturn_t action_ret;
706
707 if (unlikely(!action)) {
708 desc->status &= ~IRQ_INPROGRESS;
709 desc->chip->mask(irq);
710 return;
711 }
712
713 /*
714 * When another irq arrived while we were handling
715 * one, we could have masked the irq.
716 * Renable it, if it was not disabled in meantime.
717 */
718 if (unlikely(((desc->status & (IRQ_PENDING | IRQ_MASKED)) ==
719 (IRQ_PENDING | IRQ_MASKED)) && !desc->depth))
720 desc->chip->unmask(irq);
721
722 desc->status &= ~IRQ_PENDING;
723 spin_unlock(&desc->lock);
724 action_ret = handle_IRQ_event(irq, action);
725 spin_lock_irq(&desc->lock);
726 if (!noirqdebug)
727 note_interrupt(irq, desc, action_ret);
728 } while ((desc->status & IRQ_PENDING) && !desc->depth);
729
730 desc->status &= ~IRQ_INPROGRESS;
731 }
732
733 /*
734 * threaded edge type IRQ handler
735 */
736 static void thread_do_irq(irq_desc_t *desc)
737 {
738 unsigned int irq = desc - irq_desc;
739
740 do {
741 struct irqaction *action = desc->action;
742 irqreturn_t action_ret;
743
744 if (unlikely(!action)) {
745 desc->status &= ~IRQ_INPROGRESS;
746 desc->chip->disable(irq);
747 return;
748 }
749
750 desc->status &= ~IRQ_PENDING;
751 spin_unlock(&desc->lock);
752 action_ret = handle_IRQ_event(irq, action);
753 spin_lock_irq(&desc->lock);
754 if (!noirqdebug)
755 note_interrupt(irq, desc, action_ret);
756 } while ((desc->status & IRQ_PENDING) && !desc->depth);
757
758 desc->status &= ~IRQ_INPROGRESS;
759 desc->chip->end(irq);
760 }
761
762 static void do_hardirq(struct irq_desc *desc)
763 {
764 unsigned long flags;
765
766 spin_lock_irqsave(&desc->lock, flags);
767
768 if (!(desc->status & IRQ_INPROGRESS))
769 goto out;
770
771 if (desc->handle_irq == handle_simple_irq)
772 thread_simple_irq(desc);
773 else if (desc->handle_irq == handle_level_irq)
774 thread_level_irq(desc);
775 else if (desc->handle_irq == handle_fasteoi_irq)
776 thread_fasteoi_irq(desc);
777 else if (desc->handle_irq == handle_edge_irq)
778 thread_edge_irq(desc);
779 else
780 thread_do_irq(desc);
781 out:
782 spin_unlock_irqrestore(&desc->lock, flags);
783
784 if (waitqueue_active(&desc->wait_for_handler))
785 wake_up(&desc->wait_for_handler);
786 }
787
788 static int do_irqd(void * __desc)
789 {
790 struct sched_param param = { 0, };
791 struct irq_desc *desc = __desc;
792
793 #ifdef CONFIG_SMP
794 cpumask_t cpus_allowed;
795
796 cpus_allowed = desc->affinity;
797 #endif
798 current->flags |= PF_NOFREEZE | PF_HARDIRQ;
799
800 /*
801 * Set irq thread priority to SCHED_FIFO/50:
802 */
803 param.sched_priority = MAX_USER_RT_PRIO/2;
804
805 sys_sched_setscheduler(current->pid, SCHED_FIFO, ¶m);
806
807 while (!kthread_should_stop()) {
808 local_irq_disable_nort();
809 do {
810 set_current_state(TASK_INTERRUPTIBLE);
811 do_hardirq(desc);
812 } while (current->state == TASK_RUNNING);
813
814 local_irq_enable_nort();
815 #ifdef CONFIG_SMP
816 /*
817 * Did IRQ affinities change?
818 */
819 if (!cpus_equal(cpus_allowed, desc->affinity))
820 cpus_allowed = desc->affinity;
821 #endif
822 schedule();
823 }
824 __set_current_state(TASK_RUNNING);
825
826 return 0;
827 }
828
829 static int ok_to_create_irq_threads;
830
831 static int start_irq_thread(int irq, struct irq_desc *desc)
832 {
833 if (desc->thread || !ok_to_create_irq_threads)
834 return 0;
835
836 desc->thread = kthread_create(do_irqd, desc, "IRQ-%d", irq);
837 if (!desc->thread) {
838 printk(KERN_ERR "irqd: could not create IRQ thread %d!\n", irq);
839 return -ENOMEM;
840 }
841
842 /*
843 * An interrupt may have come in before the thread pointer was
844 * stored in desc->thread; make sure the thread gets woken up in
845 * such a case:
846 */
847 smp_mb();
848 wake_up_process(desc->thread);
849
850 return 0;
851 }
852
853 void __init init_hardirqs(void)
854 {
855 int i;
856 ok_to_create_irq_threads = 1;
857
858 for (i = 0; i < NR_IRQS; i++) {
859 irq_desc_t *desc = irq_desc + i;
860
861 if (desc->action && !(desc->status & IRQ_NODELAY))
862 start_irq_thread(i, desc);
863 }
864 }
865
866 #else
867
868 static int start_irq_thread(int irq, struct irq_desc *desc)
869 {
870 return 0;
871 }
872
873 #endif
874
875 void __init early_init_hardirqs(void)
876 {
877 int i;
878
879 for (i = 0; i < NR_IRQS; i++)
880 init_waitqueue_head(&irq_desc[i].wait_for_handler);
881 }
882
|
This page was automatically generated by the
LXR engine.
|