ia64/xen-unstable

view xen/arch/ia64/irq.c @ 5374:22e42640bcff

bitkeeper revision 1.1691.1.8 (42a6fb21d3oJwpLmOxa2jKHRJ-8fJg)

First phase of removing IRQ numbers from Xen (transitioning to
IRQ addressing by 'legacy ISA IRQ', 'interrupt vector', and
'I/O APIC address + pin' as appropriate). Overall plan is to move
I/O APIC parsing and setup out of Xen (so we start DOM0 in virtual wire
mode).
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Jun 08 14:05:21 2005 +0000 (2005-06-08)
parents 8651a99cdc09
children 949970efef98
line source
1 /*
2 * linux/arch/ia64/kernel/irq.c
3 *
4 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
5 *
6 * This file contains the code used by various IRQ handling routines:
7 * asking for different IRQ's should be done through these routines
8 * instead of just grabbing them. Thus setups with different IRQ numbers
9 * shouldn't result in any weird surprises, and installing new handlers
10 * should be easier.
11 *
12 * Copyright (C) Ashok Raj<ashok.raj@intel.com>, Intel Corporation 2004
13 *
14 * 4/14/2004: Added code to handle cpu migration and do safe irq
15 * migration without lossing interrupts for iosapic
16 * architecture.
17 */
19 /*
20 * (mostly architecture independent, will move to kernel/irq.c in 2.5.)
21 *
22 * IRQs are in fact implemented a bit like signal handlers for the kernel.
23 * Naturally it's not a 1:1 relation, but there are similarities.
24 */
26 #include <linux/config.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #ifndef XEN
30 #include <linux/signal.h>
31 #endif
32 #include <linux/sched.h>
33 #include <linux/ioport.h>
34 #include <linux/interrupt.h>
35 #include <linux/timex.h>
36 #include <linux/slab.h>
37 #ifndef XEN
38 #include <linux/random.h>
39 #include <linux/cpu.h>
40 #endif
41 #include <linux/ctype.h>
42 #ifndef XEN
43 #include <linux/smp_lock.h>
44 #endif
45 #include <linux/init.h>
46 #ifndef XEN
47 #include <linux/kernel_stat.h>
48 #endif
49 #include <linux/irq.h>
50 #ifndef XEN
51 #include <linux/proc_fs.h>
52 #endif
53 #include <linux/seq_file.h>
54 #ifndef XEN
55 #include <linux/kallsyms.h>
56 #include <linux/notifier.h>
57 #endif
59 #include <asm/atomic.h>
60 #ifndef XEN
61 #include <asm/cpu.h>
62 #endif
63 #include <asm/io.h>
64 #include <asm/smp.h>
65 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
68 #include <asm/pgalloc.h>
69 #ifndef XEN
70 #include <asm/tlbflush.h>
71 #endif
72 #include <asm/delay.h>
73 #include <asm/irq.h>
75 #ifdef XEN
76 #include <xen/event.h>
77 #define _irq_desc irq_desc
78 #define irq_descp(irq) &irq_desc[irq]
79 #define apicid_to_phys_cpu_present(x) 1
80 #endif
83 /*
84 * Linux has a controller-independent x86 interrupt architecture.
85 * every controller has a 'controller-template', that is used
86 * by the main code to do the right thing. Each driver-visible
87 * interrupt source is transparently wired to the appropriate
88 * controller. Thus drivers need not be aware of the
89 * interrupt-controller.
90 *
91 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
92 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
93 * (IO-APICs assumed to be messaging to Pentium local-APICs)
94 *
95 * the code is designed to be easily extended with new/different
96 * interrupt controllers, without having to do assembly magic.
97 */
99 /*
100 * Controller mappings for all interrupt sources:
101 */
102 irq_desc_t _irq_desc[NR_IRQS] __cacheline_aligned = {
103 [0 ... NR_IRQS-1] = {
104 .status = IRQ_DISABLED,
105 .handler = &no_irq_type,
106 .lock = SPIN_LOCK_UNLOCKED
107 }
108 };
110 /*
111 * This is updated when the user sets irq affinity via /proc
112 */
113 cpumask_t __cacheline_aligned pending_irq_cpumask[NR_IRQS];
115 #ifdef CONFIG_IA64_GENERIC
116 irq_desc_t * __ia64_irq_desc (unsigned int irq)
117 {
118 return _irq_desc + irq;
119 }
121 ia64_vector __ia64_irq_to_vector (unsigned int irq)
122 {
123 return (ia64_vector) irq;
124 }
126 unsigned int __ia64_local_vector_to_irq (ia64_vector vec)
127 {
128 return (unsigned int) vec;
129 }
130 #endif
132 static void register_irq_proc (unsigned int irq);
134 /*
135 * Special irq handlers.
136 */
138 #ifdef XEN
139 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
140 #else
141 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
142 { return IRQ_NONE; }
143 #endif
145 /*
146 * Generic no controller code
147 */
149 static void enable_none(unsigned int irq) { }
150 static unsigned int startup_none(unsigned int irq) { return 0; }
151 static void disable_none(unsigned int irq) { }
152 static void ack_none(unsigned int irq)
153 {
154 /*
155 * 'what should we do if we get a hw irq event on an illegal vector'.
156 * each architecture has to answer this themselves, it doesn't deserve
157 * a generic callback i think.
158 */
159 #ifdef CONFIG_X86
160 printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
161 #ifdef CONFIG_X86_LOCAL_APIC
162 /*
163 * Currently unexpected vectors happen only on SMP and APIC.
164 * We _must_ ack these because every local APIC has only N
165 * irq slots per priority level, and a 'hanging, unacked' IRQ
166 * holds up an irq slot - in excessive cases (when multiple
167 * unexpected vectors occur) that might lock up the APIC
168 * completely.
169 */
170 ack_APIC_irq();
171 #endif
172 #endif
173 #ifdef CONFIG_IA64
174 printk(KERN_ERR "Unexpected irq vector 0x%x on CPU %u!\n", irq, smp_processor_id());
175 #endif
176 }
178 /* startup is the same as "enable", shutdown is same as "disable" */
179 #define shutdown_none disable_none
180 #define end_none enable_none
182 struct hw_interrupt_type no_irq_type = {
183 "none",
184 startup_none,
185 shutdown_none,
186 enable_none,
187 disable_none,
188 ack_none,
189 end_none
190 };
192 atomic_t irq_err_count;
193 #ifdef CONFIG_X86_IO_APIC
194 #ifdef APIC_MISMATCH_DEBUG
195 atomic_t irq_mis_count;
196 #endif
197 #endif
199 /*
200 * Generic, controller-independent functions:
201 */
203 #ifndef XEN
204 int show_interrupts(struct seq_file *p, void *v)
205 {
206 int j, i = *(loff_t *) v;
207 struct irqaction * action;
208 irq_desc_t *idesc;
209 unsigned long flags;
211 if (i == 0) {
212 seq_puts(p, " ");
213 for (j=0; j<NR_CPUS; j++)
214 if (cpu_online(j))
215 seq_printf(p, "CPU%d ",j);
216 seq_putc(p, '\n');
217 }
219 if (i < NR_IRQS) {
220 idesc = irq_descp(i);
221 spin_lock_irqsave(&idesc->lock, flags);
222 action = idesc->action;
223 if (!action)
224 goto skip;
225 seq_printf(p, "%3d: ",i);
226 #ifndef CONFIG_SMP
227 seq_printf(p, "%10u ", kstat_irqs(i));
228 #else
229 for (j = 0; j < NR_CPUS; j++)
230 if (cpu_online(j))
231 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
232 #endif
233 seq_printf(p, " %14s", idesc->handler->typename);
234 seq_printf(p, " %s", action->name);
236 for (action=action->next; action; action = action->next)
237 seq_printf(p, ", %s", action->name);
239 seq_putc(p, '\n');
240 skip:
241 spin_unlock_irqrestore(&idesc->lock, flags);
242 } else if (i == NR_IRQS) {
243 seq_puts(p, "NMI: ");
244 for (j = 0; j < NR_CPUS; j++)
245 if (cpu_online(j))
246 seq_printf(p, "%10u ", nmi_count(j));
247 seq_putc(p, '\n');
248 #ifdef CONFIG_X86_LOCAL_APIC
249 seq_puts(p, "LOC: ");
250 for (j = 0; j < NR_CPUS; j++)
251 if (cpu_online(j))
252 seq_printf(p, "%10u ", irq_stat[j].apic_timer_irqs);
253 seq_putc(p, '\n');
254 #endif
255 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
256 #ifdef CONFIG_X86_IO_APIC
257 #ifdef APIC_MISMATCH_DEBUG
258 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
259 #endif
260 #endif
261 }
262 return 0;
263 }
264 #endif
266 #ifdef CONFIG_SMP
267 inline void synchronize_irq(unsigned int irq)
268 {
269 while (irq_descp(irq)->status & IRQ_INPROGRESS)
270 cpu_relax();
271 }
272 EXPORT_SYMBOL(synchronize_irq);
273 #endif
275 /*
276 * This should really return information about whether
277 * we should do bottom half handling etc. Right now we
278 * end up _always_ checking the bottom half, which is a
279 * waste of time and is not what some drivers would
280 * prefer.
281 */
282 int handle_IRQ_event(unsigned int irq,
283 struct pt_regs *regs, struct irqaction *action)
284 {
285 int status = 1; /* Force the "do bottom halves" bit */
286 int retval = 0;
288 #ifndef XEN
289 if (!(action->flags & SA_INTERRUPT))
290 #endif
291 local_irq_enable();
293 #ifdef XEN
294 action->handler(irq, action->dev_id, regs);
295 #else
296 do {
297 status |= action->flags;
298 retval |= action->handler(irq, action->dev_id, regs);
299 action = action->next;
300 } while (action);
301 if (status & SA_SAMPLE_RANDOM)
302 add_interrupt_randomness(irq);
303 #endif
304 local_irq_disable();
305 return retval;
306 }
308 #ifndef XEN
309 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
310 {
311 struct irqaction *action;
313 if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
314 printk(KERN_ERR "irq event %d: bogus return value %x\n",
315 irq, action_ret);
316 } else {
317 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
318 }
319 dump_stack();
320 printk(KERN_ERR "handlers:\n");
321 action = desc->action;
322 do {
323 printk(KERN_ERR "[<%p>]", action->handler);
324 print_symbol(" (%s)",
325 (unsigned long)action->handler);
326 printk("\n");
327 action = action->next;
328 } while (action);
329 }
331 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
332 {
333 static int count = 100;
335 if (count) {
336 count--;
337 __report_bad_irq(irq, desc, action_ret);
338 }
339 }
340 #endif
342 static int noirqdebug;
344 static int __init noirqdebug_setup(char *str)
345 {
346 noirqdebug = 1;
347 printk("IRQ lockup detection disabled\n");
348 return 1;
349 }
351 __setup("noirqdebug", noirqdebug_setup);
353 /*
354 * If 99,900 of the previous 100,000 interrupts have not been handled then
355 * assume that the IRQ is stuck in some manner. Drop a diagnostic and try to
356 * turn the IRQ off.
357 *
358 * (The other 100-of-100,000 interrupts may have been a correctly-functioning
359 * device sharing an IRQ with the failing one)
360 *
361 * Called under desc->lock
362 */
363 #ifndef XEN
364 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
365 {
366 if (action_ret != IRQ_HANDLED) {
367 desc->irqs_unhandled++;
368 if (action_ret != IRQ_NONE)
369 report_bad_irq(irq, desc, action_ret);
370 }
372 desc->irq_count++;
373 if (desc->irq_count < 100000)
374 return;
376 desc->irq_count = 0;
377 if (desc->irqs_unhandled > 99900) {
378 /*
379 * The interrupt is stuck
380 */
381 __report_bad_irq(irq, desc, action_ret);
382 /*
383 * Now kill the IRQ
384 */
385 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
386 desc->status |= IRQ_DISABLED;
387 desc->handler->disable(irq);
388 }
389 desc->irqs_unhandled = 0;
390 }
391 #endif
393 /*
394 * Generic enable/disable code: this just calls
395 * down into the PIC-specific version for the actual
396 * hardware disable after having gotten the irq
397 * controller lock.
398 */
400 /**
401 * disable_irq_nosync - disable an irq without waiting
402 * @irq: Interrupt to disable
403 *
404 * Disable the selected interrupt line. Disables and Enables are
405 * nested.
406 * Unlike disable_irq(), this function does not ensure existing
407 * instances of the IRQ handler have completed before returning.
408 *
409 * This function may be called from IRQ context.
410 */
412 inline void disable_irq_nosync(unsigned int irq)
413 {
414 irq_desc_t *desc = irq_descp(irq);
415 unsigned long flags;
417 spin_lock_irqsave(&desc->lock, flags);
418 if (!desc->depth++) {
419 desc->status |= IRQ_DISABLED;
420 desc->handler->disable(irq);
421 }
422 spin_unlock_irqrestore(&desc->lock, flags);
423 }
424 EXPORT_SYMBOL(disable_irq_nosync);
426 /**
427 * disable_irq - disable an irq and wait for completion
428 * @irq: Interrupt to disable
429 *
430 * Disable the selected interrupt line. Enables and Disables are
431 * nested.
432 * This function waits for any pending IRQ handlers for this interrupt
433 * to complete before returning. If you use this function while
434 * holding a resource the IRQ handler may need you will deadlock.
435 *
436 * This function may be called - with care - from IRQ context.
437 */
439 void disable_irq(unsigned int irq)
440 {
441 irq_desc_t *desc = irq_descp(irq);
443 disable_irq_nosync(irq);
444 if (desc->action)
445 synchronize_irq(irq);
446 }
447 EXPORT_SYMBOL(disable_irq);
449 /**
450 * enable_irq - enable handling of an irq
451 * @irq: Interrupt to enable
452 *
453 * Undoes the effect of one call to disable_irq(). If this
454 * matches the last disable, processing of interrupts on this
455 * IRQ line is re-enabled.
456 *
457 * This function may be called from IRQ context.
458 */
460 void enable_irq(unsigned int irq)
461 {
462 irq_desc_t *desc = irq_descp(irq);
463 unsigned long flags;
465 spin_lock_irqsave(&desc->lock, flags);
466 switch (desc->depth) {
467 case 1: {
468 unsigned int status = desc->status & ~IRQ_DISABLED;
469 desc->status = status;
470 #ifndef XEN
471 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
472 desc->status = status | IRQ_REPLAY;
473 hw_resend_irq(desc->handler,irq);
474 }
475 #endif
476 desc->handler->enable(irq);
477 /* fall-through */
478 }
479 default:
480 desc->depth--;
481 break;
482 case 0:
483 printk(KERN_ERR "enable_irq(%u) unbalanced from %p\n",
484 irq, (void *) __builtin_return_address(0));
485 }
486 spin_unlock_irqrestore(&desc->lock, flags);
487 }
488 EXPORT_SYMBOL(enable_irq);
490 /*
491 * do_IRQ handles all normal device IRQ's (the special
492 * SMP cross-CPU interrupts have their own specific
493 * handlers).
494 */
495 fastcall unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs)
496 {
497 irq_desc_t *desc = irq_desc + irq;
498 struct irqaction * action;
499 unsigned int status;
501 #ifndef XEN
502 kstat_this_cpu.irqs[irq]++;
503 #endif
504 if (desc->status & IRQ_PER_CPU) {
505 irqreturn_t action_ret;
507 /*
508 * No locking required for CPU-local interrupts:
509 */
510 desc->handler->ack(irq);
511 action_ret = handle_IRQ_event(irq, regs, desc->action);
512 #ifndef XEN
513 if (!noirqdebug)
514 note_interrupt(irq, desc, action_ret);
515 #endif
516 desc->handler->end(irq);
517 return 1;
518 }
520 spin_lock(&desc->lock);
521 desc->handler->ack(irq);
522 /*
523 * REPLAY is when Linux resends an IRQ that was dropped earlier
524 * WAITING is used by probe to mark irqs that are being tested
525 */
526 #ifdef XEN
527 status = desc->status & ~IRQ_REPLAY;
528 #else
529 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
530 #endif
531 status |= IRQ_PENDING; /* we _want_ to handle it */
533 /*
534 * If the IRQ is disabled for whatever reason, we cannot
535 * use the action we have.
536 */
537 action = NULL;
538 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
539 action = desc->action;
540 status &= ~IRQ_PENDING; /* we commit to handling */
541 status |= IRQ_INPROGRESS; /* we are handling it */
542 }
543 desc->status = status;
545 /*
546 * If there is no IRQ handler or it was disabled, exit early.
547 * Since we set PENDING, if another processor is handling
548 * a different instance of this same irq, the other processor
549 * will take care of it.
550 */
551 if (unlikely(!action))
552 goto out;
554 /*
555 * Edge triggered interrupts need to remember
556 * pending events.
557 * This applies to any hw interrupts that allow a second
558 * instance of the same irq to arrive while we are in do_IRQ
559 * or in the handler. But the code here only handles the _second_
560 * instance of the irq, not the third or fourth. So it is mostly
561 * useful for irq hardware that does not mask cleanly in an
562 * SMP environment.
563 */
564 for (;;) {
565 irqreturn_t action_ret;
567 spin_unlock(&desc->lock);
569 action_ret = handle_IRQ_event(irq, regs, action);
571 spin_lock(&desc->lock);
572 #ifndef XEN
573 if (!noirqdebug)
574 note_interrupt(irq, desc, action_ret);
575 #endif
576 if (likely(!(desc->status & IRQ_PENDING)))
577 break;
578 desc->status &= ~IRQ_PENDING;
579 }
580 desc->status &= ~IRQ_INPROGRESS;
582 out:
583 /*
584 * The ->end() handler has to deal with interrupts which got
585 * disabled while the handler was running.
586 */
587 desc->handler->end(irq);
588 spin_unlock(&desc->lock);
590 return 1;
591 }
593 /**
594 * request_irq - allocate an interrupt line
595 * @irq: Interrupt line to allocate
596 * @handler: Function to be called when the IRQ occurs
597 * @irqflags: Interrupt type flags
598 * @devname: An ascii name for the claiming device
599 * @dev_id: A cookie passed back to the handler function
600 *
601 * This call allocates interrupt resources and enables the
602 * interrupt line and IRQ handling. From the point this
603 * call is made your handler function may be invoked. Since
604 * your handler function must clear any interrupt the board
605 * raises, you must take care both to initialise your hardware
606 * and to set up the interrupt handler in the right order.
607 *
608 * Dev_id must be globally unique. Normally the address of the
609 * device data structure is used as the cookie. Since the handler
610 * receives this value it makes sense to use it.
611 *
612 * If your interrupt is shared you must pass a non NULL dev_id
613 * as this is required when freeing the interrupt.
614 *
615 * Flags:
616 *
617 * SA_SHIRQ Interrupt is shared
618 *
619 * SA_INTERRUPT Disable local interrupts while processing
620 *
621 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
622 *
623 */
625 int request_irq(unsigned int irq,
626 irqreturn_t (*handler)(int, void *, struct pt_regs *),
627 unsigned long irqflags,
628 const char * devname,
629 void *dev_id)
630 {
631 int retval;
632 struct irqaction * action;
634 #if 1
635 /*
636 * Sanity-check: shared interrupts should REALLY pass in
637 * a real dev-ID, otherwise we'll have trouble later trying
638 * to figure out which interrupt is which (messes up the
639 * interrupt freeing logic etc).
640 */
641 if (irqflags & SA_SHIRQ) {
642 if (!dev_id)
643 printk(KERN_ERR "Bad boy: %s called us without a dev_id!\n", devname);
644 }
645 #endif
647 if (irq >= NR_IRQS)
648 return -EINVAL;
649 if (!handler)
650 return -EINVAL;
652 action = xmalloc(struct irqaction);
653 if (!action)
654 return -ENOMEM;
656 action->handler = handler;
657 #ifndef XEN
658 action->flags = irqflags;
659 action->mask = 0;
660 #endif
661 action->name = devname;
662 #ifndef XEN
663 action->next = NULL;
664 #endif
665 action->dev_id = dev_id;
667 retval = setup_irq(irq, action);
668 if (retval)
669 xfree(action);
670 return retval;
671 }
673 EXPORT_SYMBOL(request_irq);
675 /**
676 * free_irq - free an interrupt
677 * @irq: Interrupt line to free
678 * @dev_id: Device identity to free
679 *
680 * Remove an interrupt handler. The handler is removed and if the
681 * interrupt line is no longer in use by any driver it is disabled.
682 * On a shared IRQ the caller must ensure the interrupt is disabled
683 * on the card it drives before calling this function. The function
684 * does not return until any executing interrupts for this IRQ
685 * have completed.
686 *
687 * This function must not be called from interrupt context.
688 */
690 #ifdef XEN
691 void free_irq(unsigned int irq)
692 #else
693 void free_irq(unsigned int irq, void *dev_id)
694 #endif
695 {
696 irq_desc_t *desc;
697 struct irqaction **p;
698 unsigned long flags;
700 if (irq >= NR_IRQS)
701 return;
703 desc = irq_descp(irq);
704 spin_lock_irqsave(&desc->lock,flags);
705 #ifdef XEN
706 if (desc->action) {
707 struct irqaction * action = desc->action;
708 desc->action = NULL;
709 #else
710 p = &desc->action;
711 for (;;) {
712 struct irqaction * action = *p;
713 if (action) {
714 struct irqaction **pp = p;
715 p = &action->next;
716 if (action->dev_id != dev_id)
717 continue;
719 /* Found it - now remove it from the list of entries */
720 *pp = action->next;
721 if (!desc->action) {
722 #endif
723 desc->status |= IRQ_DISABLED;
724 desc->handler->shutdown(irq);
725 #ifndef XEN
726 }
727 #endif
728 spin_unlock_irqrestore(&desc->lock,flags);
730 /* Wait to make sure it's not being used on another CPU */
731 synchronize_irq(irq);
732 xfree(action);
733 return;
734 }
735 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
736 spin_unlock_irqrestore(&desc->lock,flags);
737 #ifndef XEN
738 return;
739 }
740 #endif
741 }
743 EXPORT_SYMBOL(free_irq);
745 /*
746 * IRQ autodetection code..
747 *
748 * This depends on the fact that any interrupt that
749 * comes in on to an unassigned handler will get stuck
750 * with "IRQ_WAITING" cleared and the interrupt
751 * disabled.
752 */
754 static DECLARE_MUTEX(probe_sem);
756 /**
757 * probe_irq_on - begin an interrupt autodetect
758 *
759 * Commence probing for an interrupt. The interrupts are scanned
760 * and a mask of potential interrupt lines is returned.
761 *
762 */
764 #ifndef XEN
765 unsigned long probe_irq_on(void)
766 {
767 unsigned int i;
768 irq_desc_t *desc;
769 unsigned long val;
770 unsigned long delay;
772 down(&probe_sem);
773 /*
774 * something may have generated an irq long ago and we want to
775 * flush such a longstanding irq before considering it as spurious.
776 */
777 for (i = NR_IRQS-1; i > 0; i--) {
778 desc = irq_descp(i);
780 spin_lock_irq(&desc->lock);
781 if (!desc->action)
782 desc->handler->startup(i);
783 spin_unlock_irq(&desc->lock);
784 }
786 /* Wait for longstanding interrupts to trigger. */
787 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
788 /* about 20ms delay */ barrier();
790 /*
791 * enable any unassigned irqs
792 * (we must startup again here because if a longstanding irq
793 * happened in the previous stage, it may have masked itself)
794 */
795 for (i = NR_IRQS-1; i > 0; i--) {
796 desc = irq_descp(i);
798 spin_lock_irq(&desc->lock);
799 if (!desc->action) {
800 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
801 if (desc->handler->startup(i))
802 desc->status |= IRQ_PENDING;
803 }
804 spin_unlock_irq(&desc->lock);
805 }
807 /*
808 * Wait for spurious interrupts to trigger
809 */
810 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
811 /* about 100ms delay */ barrier();
813 /*
814 * Now filter out any obviously spurious interrupts
815 */
816 val = 0;
817 for (i = 0; i < NR_IRQS; i++) {
818 irq_desc_t *desc = irq_descp(i);
819 unsigned int status;
821 spin_lock_irq(&desc->lock);
822 status = desc->status;
824 if (status & IRQ_AUTODETECT) {
825 /* It triggered already - consider it spurious. */
826 if (!(status & IRQ_WAITING)) {
827 desc->status = status & ~IRQ_AUTODETECT;
828 desc->handler->shutdown(i);
829 } else
830 if (i < 32)
831 val |= 1 << i;
832 }
833 spin_unlock_irq(&desc->lock);
834 }
836 return val;
837 }
839 EXPORT_SYMBOL(probe_irq_on);
841 /**
842 * probe_irq_mask - scan a bitmap of interrupt lines
843 * @val: mask of interrupts to consider
844 *
845 * Scan the ISA bus interrupt lines and return a bitmap of
846 * active interrupts. The interrupt probe logic state is then
847 * returned to its previous value.
848 *
849 * Note: we need to scan all the irq's even though we will
850 * only return ISA irq numbers - just so that we reset them
851 * all to a known state.
852 */
853 unsigned int probe_irq_mask(unsigned long val)
854 {
855 int i;
856 unsigned int mask;
858 mask = 0;
859 for (i = 0; i < 16; i++) {
860 irq_desc_t *desc = irq_descp(i);
861 unsigned int status;
863 spin_lock_irq(&desc->lock);
864 status = desc->status;
866 if (status & IRQ_AUTODETECT) {
867 if (!(status & IRQ_WAITING))
868 mask |= 1 << i;
870 desc->status = status & ~IRQ_AUTODETECT;
871 desc->handler->shutdown(i);
872 }
873 spin_unlock_irq(&desc->lock);
874 }
875 up(&probe_sem);
877 return mask & val;
878 }
879 EXPORT_SYMBOL(probe_irq_mask);
881 /**
882 * probe_irq_off - end an interrupt autodetect
883 * @val: mask of potential interrupts (unused)
884 *
885 * Scans the unused interrupt lines and returns the line which
886 * appears to have triggered the interrupt. If no interrupt was
887 * found then zero is returned. If more than one interrupt is
888 * found then minus the first candidate is returned to indicate
889 * their is doubt.
890 *
891 * The interrupt probe logic state is returned to its previous
892 * value.
893 *
894 * BUGS: When used in a module (which arguably shouldn't happen)
895 * nothing prevents two IRQ probe callers from overlapping. The
896 * results of this are non-optimal.
897 */
899 int probe_irq_off(unsigned long val)
900 {
901 int i, irq_found, nr_irqs;
903 nr_irqs = 0;
904 irq_found = 0;
905 for (i = 0; i < NR_IRQS; i++) {
906 irq_desc_t *desc = irq_descp(i);
907 unsigned int status;
909 spin_lock_irq(&desc->lock);
910 status = desc->status;
912 if (status & IRQ_AUTODETECT) {
913 if (!(status & IRQ_WAITING)) {
914 if (!nr_irqs)
915 irq_found = i;
916 nr_irqs++;
917 }
918 desc->status = status & ~IRQ_AUTODETECT;
919 desc->handler->shutdown(i);
920 }
921 spin_unlock_irq(&desc->lock);
922 }
923 up(&probe_sem);
925 if (nr_irqs > 1)
926 irq_found = -irq_found;
927 return irq_found;
928 }
930 EXPORT_SYMBOL(probe_irq_off);
931 #endif
933 int setup_irq(unsigned int irq, struct irqaction * new)
934 {
935 int shared = 0;
936 unsigned long flags;
937 struct irqaction *old, **p;
938 irq_desc_t *desc = irq_descp(irq);
940 #ifndef XEN
941 if (desc->handler == &no_irq_type)
942 return -ENOSYS;
943 /*
944 * Some drivers like serial.c use request_irq() heavily,
945 * so we have to be careful not to interfere with a
946 * running system.
947 */
948 if (new->flags & SA_SAMPLE_RANDOM) {
949 /*
950 * This function might sleep, we want to call it first,
951 * outside of the atomic block.
952 * Yes, this might clear the entropy pool if the wrong
953 * driver is attempted to be loaded, without actually
954 * installing a new handler, but is this really a problem,
955 * only the sysadmin is able to do this.
956 */
957 rand_initialize_irq(irq);
958 }
960 if (new->flags & SA_PERCPU_IRQ) {
961 desc->status |= IRQ_PER_CPU;
962 desc->handler = &irq_type_ia64_lsapic;
963 }
964 #endif
966 /*
967 * The following block of code has to be executed atomically
968 */
969 spin_lock_irqsave(&desc->lock,flags);
970 p = &desc->action;
971 if ((old = *p) != NULL) {
972 #ifdef XEN
973 if (1) {
974 /* Can't share interrupts unless both agree to */
975 #else
976 if (!(old->flags & new->flags & SA_SHIRQ)) {
977 #endif
978 spin_unlock_irqrestore(&desc->lock,flags);
979 return -EBUSY;
980 }
982 #ifndef XEN
983 /* add new interrupt at end of irq queue */
984 do {
985 p = &old->next;
986 old = *p;
987 } while (old);
988 shared = 1;
989 #endif
990 }
992 *p = new;
994 #ifndef XEN
995 if (!shared) {
996 #else
997 {
998 #endif
999 desc->depth = 0;
1000 #ifdef XEN
1001 desc->status &= ~(IRQ_DISABLED | IRQ_INPROGRESS);
1002 #else
1003 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
1004 #endif
1005 desc->handler->startup(irq);
1007 spin_unlock_irqrestore(&desc->lock,flags);
1009 #ifndef XEN
1010 register_irq_proc(irq);
1011 #endif
1012 return 0;
1015 static struct proc_dir_entry * root_irq_dir;
1016 static struct proc_dir_entry * irq_dir [NR_IRQS];
1018 #ifdef CONFIG_SMP
1020 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
1022 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
1024 static char irq_redir [NR_IRQS]; // = { [0 ... NR_IRQS-1] = 1 };
1026 void set_irq_affinity_info (unsigned int irq, int hwid, int redir)
1028 cpumask_t mask = CPU_MASK_NONE;
1030 cpu_set(cpu_logical_id(hwid), mask);
1032 if (irq < NR_IRQS) {
1033 irq_affinity[irq] = mask;
1034 irq_redir[irq] = (char) (redir & 0xff);
1038 static int irq_affinity_read_proc (char *page, char **start, off_t off,
1039 int count, int *eof, void *data)
1041 int len = sprintf(page, "%s", irq_redir[(long)data] ? "r " : "");
1043 len += cpumask_scnprintf(page+len, count, irq_affinity[(long)data]);
1044 if (count - len < 2)
1045 return -EINVAL;
1046 len += sprintf(page + len, "\n");
1047 return len;
1050 static int irq_affinity_write_proc (struct file *file, const char *buffer,
1051 unsigned long count, void *data)
1053 unsigned int irq = (unsigned long) data;
1054 int full_count = count, err;
1055 cpumask_t new_value, tmp;
1056 # define R_PREFIX_LEN 16
1057 char rbuf[R_PREFIX_LEN];
1058 int rlen;
1059 int prelen;
1060 irq_desc_t *desc = irq_descp(irq);
1061 unsigned long flags;
1063 if (!desc->handler->set_affinity)
1064 return -EIO;
1066 /*
1067 * If string being written starts with a prefix of 'r' or 'R'
1068 * and some limited number of spaces, set IA64_IRQ_REDIRECTED.
1069 * If more than (R_PREFIX_LEN - 2) spaces are passed, they won't
1070 * all be trimmed as part of prelen, the untrimmed spaces will
1071 * cause the hex parsing to fail, and this write() syscall will
1072 * fail with EINVAL.
1073 */
1075 if (!count)
1076 return -EINVAL;
1077 rlen = min(sizeof(rbuf)-1, count);
1078 if (copy_from_user(rbuf, buffer, rlen))
1079 return -EFAULT;
1080 rbuf[rlen] = 0;
1081 prelen = 0;
1082 if (tolower(*rbuf) == 'r') {
1083 prelen = strspn(rbuf, "Rr ");
1084 irq |= IA64_IRQ_REDIRECTED;
1087 err = cpumask_parse(buffer+prelen, count-prelen, new_value);
1088 if (err)
1089 return err;
1091 /*
1092 * Do not allow disabling IRQs completely - it's a too easy
1093 * way to make the system unusable accidentally :-) At least
1094 * one online CPU still has to be targeted.
1095 */
1096 cpus_and(tmp, new_value, cpu_online_map);
1097 if (cpus_empty(tmp))
1098 return -EINVAL;
1100 spin_lock_irqsave(&desc->lock, flags);
1101 pending_irq_cpumask[irq] = new_value;
1102 spin_unlock_irqrestore(&desc->lock, flags);
1104 return full_count;
1107 void move_irq(int irq)
1109 /* note - we hold desc->lock */
1110 cpumask_t tmp;
1111 irq_desc_t *desc = irq_descp(irq);
1113 if (!cpus_empty(pending_irq_cpumask[irq])) {
1114 cpus_and(tmp, pending_irq_cpumask[irq], cpu_online_map);
1115 if (unlikely(!cpus_empty(tmp))) {
1116 desc->handler->set_affinity(irq, pending_irq_cpumask[irq]);
1118 cpus_clear(pending_irq_cpumask[irq]);
1123 #endif /* CONFIG_SMP */
1125 #ifdef CONFIG_HOTPLUG_CPU
1126 unsigned int vectors_in_migration[NR_IRQS];
1128 /*
1129 * Since cpu_online_map is already updated, we just need to check for
1130 * affinity that has zeros
1131 */
1132 static void migrate_irqs(void)
1134 cpumask_t mask;
1135 irq_desc_t *desc;
1136 int irq, new_cpu;
1138 for (irq=0; irq < NR_IRQS; irq++) {
1139 desc = irq_descp(irq);
1141 /*
1142 * No handling for now.
1143 * TBD: Implement a disable function so we can now
1144 * tell CPU not to respond to these local intr sources.
1145 * such as ITV,CPEI,MCA etc.
1146 */
1147 if (desc->status == IRQ_PER_CPU)
1148 continue;
1150 cpus_and(mask, irq_affinity[irq], cpu_online_map);
1151 if (any_online_cpu(mask) == NR_CPUS) {
1152 /*
1153 * Save it for phase 2 processing
1154 */
1155 vectors_in_migration[irq] = irq;
1157 new_cpu = any_online_cpu(cpu_online_map);
1158 mask = cpumask_of_cpu(new_cpu);
1160 /*
1161 * Al three are essential, currently WARN_ON.. maybe panic?
1162 */
1163 if (desc->handler && desc->handler->disable &&
1164 desc->handler->enable && desc->handler->set_affinity) {
1165 desc->handler->disable(irq);
1166 desc->handler->set_affinity(irq, mask);
1167 desc->handler->enable(irq);
1168 } else {
1169 WARN_ON((!(desc->handler) || !(desc->handler->disable) ||
1170 !(desc->handler->enable) ||
1171 !(desc->handler->set_affinity)));
1177 void fixup_irqs(void)
1179 unsigned int irq;
1180 extern void ia64_process_pending_intr(void);
1182 ia64_set_itv(1<<16);
1183 /*
1184 * Phase 1: Locate irq's bound to this cpu and
1185 * relocate them for cpu removal.
1186 */
1187 migrate_irqs();
1189 /*
1190 * Phase 2: Perform interrupt processing for all entries reported in
1191 * local APIC.
1192 */
1193 ia64_process_pending_intr();
1195 /*
1196 * Phase 3: Now handle any interrupts not captured in local APIC.
1197 * This is to account for cases that device interrupted during the time the
1198 * rte was being disabled and re-programmed.
1199 */
1200 for (irq=0; irq < NR_IRQS; irq++) {
1201 if (vectors_in_migration[irq]) {
1202 vectors_in_migration[irq]=0;
1203 do_IRQ(irq, NULL);
1207 /*
1208 * Now let processor die. We do irq disable and max_xtp() to
1209 * ensure there is no more interrupts routed to this processor.
1210 * But the local timer interrupt can have 1 pending which we
1211 * take care in timer_interrupt().
1212 */
1213 max_xtp();
1214 local_irq_disable();
1216 #endif
1218 #ifndef XEN
1219 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1220 int count, int *eof, void *data)
1222 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
1223 if (count - len < 2)
1224 return -EINVAL;
1225 len += sprintf(page + len, "\n");
1226 return len;
1229 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
1230 unsigned long count, void *data)
1232 cpumask_t *mask = (cpumask_t *)data;
1233 unsigned long full_count = count, err;
1234 cpumask_t new_value;
1236 err = cpumask_parse(buffer, count, new_value);
1237 if (err)
1238 return err;
1240 *mask = new_value;
1241 return full_count;
1244 #define MAX_NAMELEN 10
1246 static void register_irq_proc (unsigned int irq)
1248 char name [MAX_NAMELEN];
1250 if (!root_irq_dir || (irq_descp(irq)->handler == &no_irq_type) || irq_dir[irq])
1251 return;
1253 memset(name, 0, MAX_NAMELEN);
1254 sprintf(name, "%d", irq);
1256 /* create /proc/irq/1234 */
1257 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1259 #ifdef CONFIG_SMP
1261 struct proc_dir_entry *entry;
1263 /* create /proc/irq/1234/smp_affinity */
1264 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1266 if (entry) {
1267 entry->nlink = 1;
1268 entry->data = (void *)(long)irq;
1269 entry->read_proc = irq_affinity_read_proc;
1270 entry->write_proc = irq_affinity_write_proc;
1273 smp_affinity_entry[irq] = entry;
1275 #endif
1278 cpumask_t prof_cpu_mask = CPU_MASK_ALL;
1280 void init_irq_proc (void)
1282 struct proc_dir_entry *entry;
1283 int i;
1285 /* create /proc/irq */
1286 root_irq_dir = proc_mkdir("irq", 0);
1288 /* create /proc/irq/prof_cpu_mask */
1289 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1291 if (!entry)
1292 return;
1294 entry->nlink = 1;
1295 entry->data = (void *)&prof_cpu_mask;
1296 entry->read_proc = prof_cpu_mask_read_proc;
1297 entry->write_proc = prof_cpu_mask_write_proc;
1299 /*
1300 * Create entries for all existing IRQs.
1301 */
1302 for (i = 0; i < NR_IRQS; i++) {
1303 if (irq_descp(i)->handler == &no_irq_type)
1304 continue;
1305 register_irq_proc(i);
1308 #endif
1311 #ifdef XEN
1312 /*
1313 * HANDLING OF GUEST-BOUND PHYSICAL IRQS
1314 */
1316 #define IRQ_MAX_GUESTS 7
1317 typedef struct {
1318 u8 nr_guests;
1319 u8 in_flight;
1320 u8 shareable;
1321 struct domain *guest[IRQ_MAX_GUESTS];
1322 } irq_guest_action_t;
1324 static void __do_IRQ_guest(int irq)
1326 irq_desc_t *desc = &irq_desc[irq];
1327 irq_guest_action_t *action = (irq_guest_action_t *)desc->action;
1328 struct domain *d;
1329 int i;
1331 for ( i = 0; i < action->nr_guests; i++ )
1333 d = action->guest[i];
1334 if ( !test_and_set_bit(irq, &d->pirq_mask) )
1335 action->in_flight++;
1336 send_guest_pirq(d, irq);
1340 int pirq_guest_unmask(struct domain *d)
1342 irq_desc_t *desc;
1343 int i, j, pirq;
1344 u32 m;
1345 shared_info_t *s = d->shared_info;
1347 for ( i = 0; i < ARRAY_SIZE(d->pirq_mask); i++ )
1349 m = d->pirq_mask[i];
1350 while ( (j = ffs(m)) != 0 )
1352 m &= ~(1 << --j);
1353 pirq = (i << 5) + j;
1354 desc = &irq_desc[pirq];
1355 spin_lock_irq(&desc->lock);
1356 if ( !test_bit(d->pirq_to_evtchn[pirq], &s->evtchn_mask[0]) &&
1357 test_and_clear_bit(pirq, &d->pirq_mask) &&
1358 (--((irq_guest_action_t *)desc->action)->in_flight == 0) )
1359 desc->handler->end(pirq);
1360 spin_unlock_irq(&desc->lock);
1364 return 0;
1367 int pirq_guest_bind(struct vcpu *d, int irq, int will_share)
1369 irq_desc_t *desc = &irq_desc[irq];
1370 irq_guest_action_t *action;
1371 unsigned long flags;
1372 int rc = 0;
1374 if ( !IS_CAPABLE_PHYSDEV(d->domain) )
1375 return -EPERM;
1377 spin_lock_irqsave(&desc->lock, flags);
1379 action = (irq_guest_action_t *)desc->action;
1381 if ( !(desc->status & IRQ_GUEST) )
1383 if ( desc->action != NULL )
1385 DPRINTK("Cannot bind IRQ %d to guest. In use by '%s'.\n",
1386 irq, desc->action->name);
1387 rc = -EBUSY;
1388 goto out;
1391 action = xmalloc(irq_guest_action_t);
1392 if ( (desc->action = (struct irqaction *)action) == NULL )
1394 DPRINTK("Cannot bind IRQ %d to guest. Out of memory.\n", irq);
1395 rc = -ENOMEM;
1396 goto out;
1399 action->nr_guests = 0;
1400 action->in_flight = 0;
1401 action->shareable = will_share;
1403 desc->depth = 0;
1404 desc->status |= IRQ_GUEST;
1405 desc->status &= ~IRQ_DISABLED;
1406 desc->handler->startup(irq);
1408 /* Attempt to bind the interrupt target to the correct CPU. */
1409 #if 0 /* FIXME CONFIG_SMP ??? */
1410 if ( desc->handler->set_affinity != NULL )
1411 desc->handler->set_affinity(
1412 irq, apicid_to_phys_cpu_present(d->processor));
1413 #endif
1415 else if ( !will_share || !action->shareable )
1417 DPRINTK("Cannot bind IRQ %d to guest. Will not share with others.\n",
1418 irq);
1419 rc = -EBUSY;
1420 goto out;
1423 if ( action->nr_guests == IRQ_MAX_GUESTS )
1425 DPRINTK("Cannot bind IRQ %d to guest. Already at max share.\n", irq);
1426 rc = -EBUSY;
1427 goto out;
1430 action->guest[action->nr_guests++] = d;
1432 out:
1433 spin_unlock_irqrestore(&desc->lock, flags);
1434 return rc;
1437 int pirq_guest_unbind(struct domain *d, int irq)
1439 irq_desc_t *desc = &irq_desc[irq];
1440 irq_guest_action_t *action;
1441 unsigned long flags;
1442 int i;
1444 spin_lock_irqsave(&desc->lock, flags);
1446 action = (irq_guest_action_t *)desc->action;
1448 if ( test_and_clear_bit(irq, &d->pirq_mask) &&
1449 (--action->in_flight == 0) )
1450 desc->handler->end(irq);
1452 if ( action->nr_guests == 1 )
1454 desc->action = NULL;
1455 xfree(action);
1456 desc->depth = 1;
1457 desc->status |= IRQ_DISABLED;
1458 desc->status &= ~IRQ_GUEST;
1459 desc->handler->shutdown(irq);
1461 else
1463 i = 0;
1464 while ( action->guest[i] != d )
1465 i++;
1466 memmove(&action->guest[i], &action->guest[i+1], IRQ_MAX_GUESTS-i-1);
1467 action->nr_guests--;
1470 spin_unlock_irqrestore(&desc->lock, flags);
1471 return 0;
1474 #endif
1476 #ifdef XEN
1477 #ifdef IA64
1478 // this is a temporary hack until real console input is implemented
1479 irqreturn_t guest_forward_keyboard_input(int irq, void *nada, struct pt_regs *regs)
1481 domain_pend_keyboard_interrupt(irq);
1484 void serial_input_init(void)
1486 int retval;
1487 int irq = 0x30; // FIXME
1489 retval = request_irq(irq,guest_forward_keyboard_input,SA_INTERRUPT,"siminput",NULL);
1490 if (retval) {
1491 printk("serial_input_init: broken request_irq call\n");
1492 while(1);
1495 #endif
1496 #endif