initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / um / kernel / irq.c
blob2b82509550fe0192f714d05b1d2cc7d2dd57fd40
1 /*
2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 * Derived (i.e. mostly copied) from arch/i386/kernel/irq.c:
5 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
6 */
8 #include "linux/config.h"
9 #include "linux/kernel.h"
10 #include "linux/module.h"
11 #include "linux/smp.h"
12 #include "linux/irq.h"
13 #include "linux/kernel_stat.h"
14 #include "linux/interrupt.h"
15 #include "linux/random.h"
16 #include "linux/slab.h"
17 #include "linux/file.h"
18 #include "linux/proc_fs.h"
19 #include "linux/init.h"
20 #include "linux/seq_file.h"
21 #include "linux/profile.h"
22 #include "linux/hardirq.h"
23 #include "asm/irq.h"
24 #include "asm/hw_irq.h"
25 #include "asm/atomic.h"
26 #include "asm/signal.h"
27 #include "asm/system.h"
28 #include "asm/errno.h"
29 #include "asm/uaccess.h"
30 #include "user_util.h"
31 #include "kern_util.h"
32 #include "irq_user.h"
33 #include "irq_kern.h"
35 static void register_irq_proc (unsigned int irq);
37 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
38 [0 ... NR_IRQS-1] = {
39 .handler = &no_irq_type,
40 .lock = SPIN_LOCK_UNLOCKED
45 * Generic no controller code
48 static void enable_none(unsigned int irq) { }
49 static unsigned int startup_none(unsigned int irq) { return 0; }
50 static void disable_none(unsigned int irq) { }
51 static void ack_none(unsigned int irq)
54 * 'what should we do if we get a hw irq event on an illegal vector'.
55 * each architecture has to answer this themselves, it doesn't deserve
56 * a generic callback i think.
58 #ifdef CONFIG_X86
59 printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
60 #ifdef CONFIG_X86_LOCAL_APIC
62 * Currently unexpected vectors happen only on SMP and APIC.
63 * We _must_ ack these because every local APIC has only N
64 * irq slots per priority level, and a 'hanging, unacked' IRQ
65 * holds up an irq slot - in excessive cases (when multiple
66 * unexpected vectors occur) that might lock up the APIC
67 * completely.
69 ack_APIC_irq();
70 #endif
71 #endif
74 /* startup is the same as "enable", shutdown is same as "disable" */
75 #define shutdown_none disable_none
76 #define end_none enable_none
78 struct hw_interrupt_type no_irq_type = {
79 "none",
80 startup_none,
81 shutdown_none,
82 enable_none,
83 disable_none,
84 ack_none,
85 end_none
89 * Generic, controller-independent functions:
92 int show_interrupts(struct seq_file *p, void *v)
94 int i = *(loff_t *) v, j;
95 struct irqaction * action;
96 unsigned long flags;
98 if (i == 0) {
99 seq_printf(p, " ");
100 for (j=0; j<NR_CPUS; j++)
101 if (cpu_online(j))
102 seq_printf(p, "CPU%d ",j);
103 seq_putc(p, '\n');
106 if (i < NR_IRQS) {
107 spin_lock_irqsave(&irq_desc[i].lock, flags);
108 action = irq_desc[i].action;
109 if (!action)
110 goto skip;
111 seq_printf(p, "%3d: ",i);
112 #ifndef CONFIG_SMP
113 seq_printf(p, "%10u ", kstat_irqs(i));
114 #else
115 for (j = 0; j < NR_CPUS; j++)
116 if (cpu_online(j))
117 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
118 #endif
119 seq_printf(p, " %14s", irq_desc[i].handler->typename);
120 seq_printf(p, " %s", action->name);
122 for (action=action->next; action; action = action->next)
123 seq_printf(p, ", %s", action->name);
125 seq_putc(p, '\n');
126 skip:
127 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
128 } else if (i == NR_IRQS) {
129 seq_printf(p, "NMI: ");
130 for (j = 0; j < NR_CPUS; j++)
131 if (cpu_online(j))
132 seq_printf(p, "%10u ", nmi_count(j));
133 seq_putc(p, '\n');
136 return 0;
140 * This should really return information about whether
141 * we should do bottom half handling etc. Right now we
142 * end up _always_ checking the bottom half, which is a
143 * waste of time and is not what some drivers would
144 * prefer.
146 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs,
147 struct irqaction * action)
149 int status = 1; /* Force the "do bottom halves" bit */
150 int ret, retval = 0;
152 if (!(action->flags & SA_INTERRUPT))
153 local_irq_enable();
155 do {
156 ret = action->handler(irq, action->dev_id, regs);
157 if (ret == IRQ_HANDLED)
158 status |= action->flags;
159 retval |= ret;
160 action = action->next;
161 } while (action);
162 if (status & SA_SAMPLE_RANDOM)
163 add_interrupt_randomness(irq);
165 local_irq_disable();
167 return retval;
171 * Generic enable/disable code: this just calls
172 * down into the PIC-specific version for the actual
173 * hardware disable after having gotten the irq
174 * controller lock.
178 * disable_irq_nosync - disable an irq without waiting
179 * @irq: Interrupt to disable
181 * Disable the selected interrupt line. Disables of an interrupt
182 * stack. Unlike disable_irq(), this function does not ensure existing
183 * instances of the IRQ handler have completed before returning.
185 * This function may be called from IRQ context.
188 inline void disable_irq_nosync(unsigned int irq)
190 irq_desc_t *desc = irq_desc + irq;
191 unsigned long flags;
193 spin_lock_irqsave(&desc->lock, flags);
194 if (!desc->depth++) {
195 desc->status |= IRQ_DISABLED;
196 desc->handler->disable(irq);
198 spin_unlock_irqrestore(&desc->lock, flags);
201 #ifdef CONFIG_SMP
202 inline void synchronize_irq(unsigned int irq)
204 /* is there anything to synchronize with? */
205 if (!irq_desc[irq].action)
206 return;
208 while (irq_desc[irq].status & IRQ_INPROGRESS)
209 cpu_relax();
211 #endif
214 * disable_irq - disable an irq and wait for completion
215 * @irq: Interrupt to disable
217 * Disable the selected interrupt line. Disables of an interrupt
218 * stack. That is for two disables you need two enables. This
219 * function waits for any pending IRQ handlers for this interrupt
220 * to complete before returning. If you use this function while
221 * holding a resource the IRQ handler may need you will deadlock.
223 * This function may be called - with care - from IRQ context.
226 void disable_irq(unsigned int irq)
228 disable_irq_nosync(irq);
229 synchronize_irq(irq);
233 * enable_irq - enable interrupt handling on an irq
234 * @irq: Interrupt to enable
236 * Re-enables the processing of interrupts on this IRQ line
237 * providing no disable_irq calls are now in effect.
239 * This function may be called from IRQ context.
242 void enable_irq(unsigned int irq)
244 irq_desc_t *desc = irq_desc + irq;
245 unsigned long flags;
247 spin_lock_irqsave(&desc->lock, flags);
248 switch (desc->depth) {
249 case 1: {
250 unsigned int status = desc->status & ~IRQ_DISABLED;
251 desc->status = status;
252 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
253 desc->status = status | IRQ_REPLAY;
254 hw_resend_irq(desc->handler,irq);
256 desc->handler->enable(irq);
257 /* fall-through */
259 default:
260 desc->depth--;
261 break;
262 case 0:
263 printk(KERN_ERR "enable_irq() unbalanced from %p\n",
264 __builtin_return_address(0));
266 spin_unlock_irqrestore(&desc->lock, flags);
270 * do_IRQ handles all normal device IRQ's (the special
271 * SMP cross-CPU interrupts have their own specific
272 * handlers).
274 unsigned int do_IRQ(int irq, union uml_pt_regs *regs)
277 * 0 return value means that this irq is already being
278 * handled by some other CPU. (or is disabled)
280 irq_desc_t *desc = irq_desc + irq;
281 struct irqaction * action;
282 unsigned int status;
284 irq_enter();
285 kstat_this_cpu.irqs[irq]++;
286 spin_lock(&desc->lock);
287 desc->handler->ack(irq);
289 REPLAY is when Linux resends an IRQ that was dropped earlier
290 WAITING is used by probe to mark irqs that are being tested
292 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
293 status |= IRQ_PENDING; /* we _want_ to handle it */
296 * If the IRQ is disabled for whatever reason, we cannot
297 * use the action we have.
299 action = NULL;
300 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
301 action = desc->action;
302 status &= ~IRQ_PENDING; /* we commit to handling */
303 status |= IRQ_INPROGRESS; /* we are handling it */
305 desc->status = status;
308 * If there is no IRQ handler or it was disabled, exit early.
309 Since we set PENDING, if another processor is handling
310 a different instance of this same irq, the other processor
311 will take care of it.
313 if (!action)
314 goto out;
317 * Edge triggered interrupts need to remember
318 * pending events.
319 * This applies to any hw interrupts that allow a second
320 * instance of the same irq to arrive while we are in do_IRQ
321 * or in the handler. But the code here only handles the _second_
322 * instance of the irq, not the third or fourth. So it is mostly
323 * useful for irq hardware that does not mask cleanly in an
324 * SMP environment.
326 for (;;) {
327 spin_unlock(&desc->lock);
328 handle_IRQ_event(irq, (struct pt_regs *) regs, action);
329 spin_lock(&desc->lock);
331 if (!(desc->status & IRQ_PENDING))
332 break;
333 desc->status &= ~IRQ_PENDING;
335 desc->status &= ~IRQ_INPROGRESS;
336 out:
338 * The ->end() handler has to deal with interrupts which got
339 * disabled while the handler was running.
341 desc->handler->end(irq);
342 spin_unlock(&desc->lock);
344 irq_exit();
346 return 1;
350 * request_irq - allocate an interrupt line
351 * @irq: Interrupt line to allocate
352 * @handler: Function to be called when the IRQ occurs
353 * @irqflags: Interrupt type flags
354 * @devname: An ascii name for the claiming device
355 * @dev_id: A cookie passed back to the handler function
357 * This call allocates interrupt resources and enables the
358 * interrupt line and IRQ handling. From the point this
359 * call is made your handler function may be invoked. Since
360 * your handler function must clear any interrupt the board
361 * raises, you must take care both to initialise your hardware
362 * and to set up the interrupt handler in the right order.
364 * Dev_id must be globally unique. Normally the address of the
365 * device data structure is used as the cookie. Since the handler
366 * receives this value it makes sense to use it.
368 * If your interrupt is shared you must pass a non NULL dev_id
369 * as this is required when freeing the interrupt.
371 * Flags:
373 * SA_SHIRQ Interrupt is shared
375 * SA_INTERRUPT Disable local interrupts while processing
377 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
381 int request_irq(unsigned int irq,
382 irqreturn_t (*handler)(int, void *, struct pt_regs *),
383 unsigned long irqflags,
384 const char * devname,
385 void *dev_id)
387 int retval;
388 struct irqaction * action;
390 #if 1
392 * Sanity-check: shared interrupts should REALLY pass in
393 * a real dev-ID, otherwise we'll have trouble later trying
394 * to figure out which interrupt is which (messes up the
395 * interrupt freeing logic etc).
397 if (irqflags & SA_SHIRQ) {
398 if (!dev_id)
399 printk(KERN_ERR "Bad boy: %s (at 0x%x) called us "
400 "without a dev_id!\n", devname, (&irq)[-1]);
402 #endif
404 if (irq >= NR_IRQS)
405 return -EINVAL;
406 if (!handler)
407 return -EINVAL;
409 action = (struct irqaction *)
410 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
411 if (!action)
412 return -ENOMEM;
414 action->handler = handler;
415 action->flags = irqflags;
416 cpus_clear(action->mask);
417 action->name = devname;
418 action->next = NULL;
419 action->dev_id = dev_id;
421 retval = setup_irq(irq, action);
422 if (retval)
423 kfree(action);
424 return retval;
427 EXPORT_SYMBOL(request_irq);
429 int um_request_irq(unsigned int irq, int fd, int type,
430 irqreturn_t (*handler)(int, void *, struct pt_regs *),
431 unsigned long irqflags, const char * devname,
432 void *dev_id)
434 int err;
436 err = request_irq(irq, handler, irqflags, devname, dev_id);
437 if(err)
438 return(err);
440 if(fd != -1)
441 err = activate_fd(irq, fd, type, dev_id);
442 return(err);
444 EXPORT_SYMBOL(um_request_irq);
445 EXPORT_SYMBOL(reactivate_fd);
447 /* this was setup_x86_irq but it seems pretty generic */
448 int setup_irq(unsigned int irq, struct irqaction * new)
450 int shared = 0;
451 unsigned long flags;
452 struct irqaction *old, **p;
453 irq_desc_t *desc = irq_desc + irq;
456 * Some drivers like serial.c use request_irq() heavily,
457 * so we have to be careful not to interfere with a
458 * running system.
460 if (new->flags & SA_SAMPLE_RANDOM) {
462 * This function might sleep, we want to call it first,
463 * outside of the atomic block.
464 * Yes, this might clear the entropy pool if the wrong
465 * driver is attempted to be loaded, without actually
466 * installing a new handler, but is this really a problem,
467 * only the sysadmin is able to do this.
469 rand_initialize_irq(irq);
473 * The following block of code has to be executed atomically
475 spin_lock_irqsave(&desc->lock,flags);
476 p = &desc->action;
477 old = *p;
478 if (old != NULL) {
479 /* Can't share interrupts unless both agree to */
480 if (!(old->flags & new->flags & SA_SHIRQ)) {
481 spin_unlock_irqrestore(&desc->lock,flags);
482 return -EBUSY;
485 /* add new interrupt at end of irq queue */
486 do {
487 p = &old->next;
488 old = *p;
489 } while (old);
490 shared = 1;
493 *p = new;
495 if (!shared) {
496 desc->depth = 0;
497 desc->status &= ~IRQ_DISABLED;
498 desc->handler->startup(irq);
500 spin_unlock_irqrestore(&desc->lock,flags);
502 register_irq_proc(irq);
503 return 0;
507 * free_irq - free an interrupt
508 * @irq: Interrupt line to free
509 * @dev_id: Device identity to free
511 * Remove an interrupt handler. The handler is removed and if the
512 * interrupt line is no longer in use by any driver it is disabled.
513 * On a shared IRQ the caller must ensure the interrupt is disabled
514 * on the card it drives before calling this function. The function
515 * does not return until any executing interrupts for this IRQ
516 * have completed.
518 * This function may be called from interrupt context.
520 * Bugs: Attempting to free an irq in a handler for the same irq hangs
521 * the machine.
524 void free_irq(unsigned int irq, void *dev_id)
526 irq_desc_t *desc;
527 struct irqaction **p;
528 unsigned long flags;
530 if (irq >= NR_IRQS)
531 return;
533 desc = irq_desc + irq;
534 spin_lock_irqsave(&desc->lock,flags);
535 p = &desc->action;
536 for (;;) {
537 struct irqaction * action = *p;
538 if (action) {
539 struct irqaction **pp = p;
540 p = &action->next;
541 if (action->dev_id != dev_id)
542 continue;
544 /* Found it - now remove it from the list of entries */
545 *pp = action->next;
546 if (!desc->action) {
547 desc->status |= IRQ_DISABLED;
548 desc->handler->shutdown(irq);
550 free_irq_by_irq_and_dev(irq, dev_id);
551 spin_unlock_irqrestore(&desc->lock,flags);
553 /* Wait to make sure it's not being used on another CPU */
554 synchronize_irq(irq);
555 kfree(action);
556 return;
558 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
559 spin_unlock_irqrestore(&desc->lock,flags);
560 return;
564 EXPORT_SYMBOL(free_irq);
566 /* These are initialized by sysctl_init, which is called from init/main.c */
567 static struct proc_dir_entry * root_irq_dir;
568 static struct proc_dir_entry * irq_dir [NR_IRQS];
569 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
571 /* These are read and written as longs, so a read won't see a partial write
572 * even during a race.
574 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
576 static int irq_affinity_read_proc (char *page, char **start, off_t off,
577 int count, int *eof, void *data)
579 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
580 if (count - len < 2)
581 return -EINVAL;
582 len += sprintf(page + len, "\n");
583 return len;
586 static int irq_affinity_write_proc (struct file *file, const char *buffer,
587 unsigned long count, void *data)
589 int irq = (long) data, full_count = count, err;
590 cpumask_t new_value;
592 if (!irq_desc[irq].handler->set_affinity)
593 return -EIO;
595 err = cpumask_parse(buffer, count, new_value);
596 if(err)
597 return(err);
599 #ifdef CONFIG_SMP
601 * Do not allow disabling IRQs completely - it's a too easy
602 * way to make the system unusable accidentally :-) At least
603 * one online CPU still has to be targeted.
605 { cpumask_t tmp;
606 cpus_and(tmp, new_value, cpu_online_map);
607 if (cpus_empty(tmp))
608 return -EINVAL;
610 #endif
612 irq_affinity[irq] = new_value;
613 irq_desc[irq].handler->set_affinity(irq, new_value);
615 return full_count;
618 #define MAX_NAMELEN 10
620 static void register_irq_proc (unsigned int irq)
622 struct proc_dir_entry *entry;
623 char name [MAX_NAMELEN];
625 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
626 irq_dir[irq])
627 return;
629 memset(name, 0, MAX_NAMELEN);
630 sprintf(name, "%d", irq);
632 /* create /proc/irq/1234 */
633 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
635 /* create /proc/irq/1234/smp_affinity */
636 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
638 entry->nlink = 1;
639 entry->data = (void *)(long)irq;
640 entry->read_proc = irq_affinity_read_proc;
641 entry->write_proc = irq_affinity_write_proc;
643 smp_affinity_entry[irq] = entry;
646 void __init init_irq_proc (void)
648 int i;
650 /* create /proc/irq */
651 root_irq_dir = proc_mkdir("irq", 0);
653 /* create /proc/irq/prof_cpu_mask */
654 create_prof_cpu_mask(root_irq_dir);
657 * Create entries for all existing IRQs.
659 for (i = 0; i < NR_IRQS; i++)
660 register_irq_proc(i);
663 static spinlock_t irq_spinlock = SPIN_LOCK_UNLOCKED;
665 unsigned long irq_lock(void)
667 unsigned long flags;
669 spin_lock_irqsave(&irq_spinlock, flags);
670 return(flags);
673 void irq_unlock(unsigned long flags)
675 spin_unlock_irqrestore(&irq_spinlock, flags);
678 unsigned long probe_irq_on(void)
680 return(0);
683 EXPORT_SYMBOL(probe_irq_on);
685 int probe_irq_off(unsigned long val)
687 return(0);
690 EXPORT_SYMBOL(probe_irq_off);
692 static unsigned int startup_SIGIO_irq(unsigned int irq)
694 return(0);
697 static void shutdown_SIGIO_irq(unsigned int irq)
701 static void enable_SIGIO_irq(unsigned int irq)
705 static void disable_SIGIO_irq(unsigned int irq)
709 static void mask_and_ack_SIGIO(unsigned int irq)
713 static void end_SIGIO_irq(unsigned int irq)
717 static unsigned int startup_SIGVTALRM_irq(unsigned int irq)
719 return(0);
722 static void shutdown_SIGVTALRM_irq(unsigned int irq)
726 static void enable_SIGVTALRM_irq(unsigned int irq)
730 static void disable_SIGVTALRM_irq(unsigned int irq)
734 static void mask_and_ack_SIGVTALRM(unsigned int irq)
738 static void end_SIGVTALRM_irq(unsigned int irq)
742 static struct hw_interrupt_type SIGIO_irq_type = {
743 "SIGIO",
744 startup_SIGIO_irq,
745 shutdown_SIGIO_irq,
746 enable_SIGIO_irq,
747 disable_SIGIO_irq,
748 mask_and_ack_SIGIO,
749 end_SIGIO_irq,
750 NULL
753 static struct hw_interrupt_type SIGVTALRM_irq_type = {
754 "SIGVTALRM",
755 startup_SIGVTALRM_irq,
756 shutdown_SIGVTALRM_irq,
757 enable_SIGVTALRM_irq,
758 disable_SIGVTALRM_irq,
759 mask_and_ack_SIGVTALRM,
760 end_SIGVTALRM_irq,
761 NULL
764 void __init init_IRQ(void)
766 int i;
768 irq_desc[TIMER_IRQ].status = IRQ_DISABLED;
769 irq_desc[TIMER_IRQ].action = 0;
770 irq_desc[TIMER_IRQ].depth = 1;
771 irq_desc[TIMER_IRQ].handler = &SIGVTALRM_irq_type;
772 enable_irq(TIMER_IRQ);
773 for(i=1;i<NR_IRQS;i++){
774 irq_desc[i].status = IRQ_DISABLED;
775 irq_desc[i].action = 0;
776 irq_desc[i].depth = 1;
777 irq_desc[i].handler = &SIGIO_irq_type;
778 enable_irq(i);
780 init_irq_signals(0);
784 * Overrides for Emacs so that we follow Linus's tabbing style.
785 * Emacs will notice this stuff at the end of the file and automatically
786 * adjust the settings for this buffer only. This must remain at the end
787 * of the file.
788 * ---------------------------------------------------------------------------
789 * Local variables:
790 * c-file-style: "linux"
791 * End: