irq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. *
  3. * linux/arch/cris/kernel/irq.c
  4. *
  5. * Copyright (c) 2000,2001 Axis Communications AB
  6. *
  7. * Authors: Bjorn Wesen (bjornw@axis.com)
  8. *
  9. * This file contains the code used by various IRQ handling routines:
  10. * asking for different IRQ's should be done through these routines
  11. * instead of just grabbing them. Thus setups with different IRQ numbers
  12. * shouldn't result in any weird surprises, and installing new handlers
  13. * should be easier.
  14. *
  15. * Notice Linux/CRIS: these routines do not care about SMP
  16. *
  17. */
  18. /*
  19. * IRQ's are in fact implemented a bit like signal handlers for the kernel.
  20. * Naturally it's not a 1:1 relation, but there are similarities.
  21. */
  22. #include <linux/config.h>
  23. #include <linux/module.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/signal.h>
  27. #include <linux/sched.h>
  28. #include <linux/ioport.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/timex.h>
  31. #include <linux/slab.h>
  32. #include <linux/random.h>
  33. #include <linux/init.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/errno.h>
  36. #include <linux/bitops.h>
  37. #include <asm/io.h>
  38. /* Defined in arch specific irq.c */
  39. extern void arch_setup_irq(int irq);
  40. extern void arch_free_irq(int irq);
  41. void
  42. disable_irq(unsigned int irq_nr)
  43. {
  44. unsigned long flags;
  45. local_save_flags(flags);
  46. local_irq_disable();
  47. mask_irq(irq_nr);
  48. local_irq_restore(flags);
  49. }
  50. void
  51. enable_irq(unsigned int irq_nr)
  52. {
  53. unsigned long flags;
  54. local_save_flags(flags);
  55. local_irq_disable();
  56. unmask_irq(irq_nr);
  57. local_irq_restore(flags);
  58. }
  59. unsigned long
  60. probe_irq_on()
  61. {
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(probe_irq_on);
  65. int
  66. probe_irq_off(unsigned long x)
  67. {
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(probe_irq_off);
  71. /*
  72. * Initial irq handlers.
  73. */
  74. static struct irqaction *irq_action[NR_IRQS];
  75. int show_interrupts(struct seq_file *p, void *v)
  76. {
  77. int i = *(loff_t *) v;
  78. struct irqaction * action;
  79. unsigned long flags;
  80. if (i < NR_IRQS) {
  81. local_irq_save(flags);
  82. action = irq_action[i];
  83. if (!action)
  84. goto skip;
  85. seq_printf(p, "%2d: %10u %c %s",
  86. i, kstat_this_cpu.irqs[i],
  87. (action->flags & SA_INTERRUPT) ? '+' : ' ',
  88. action->name);
  89. for (action = action->next; action; action = action->next) {
  90. seq_printf(p, ",%s %s",
  91. (action->flags & SA_INTERRUPT) ? " +" : "",
  92. action->name);
  93. }
  94. seq_putc(p, '\n');
  95. skip:
  96. local_irq_restore(flags);
  97. }
  98. return 0;
  99. }
  100. /* called by the assembler IRQ entry functions defined in irq.h
  101. * to dispatch the interrupts to registred handlers
  102. * interrupts are disabled upon entry - depending on if the
  103. * interrupt was registred with SA_INTERRUPT or not, interrupts
  104. * are re-enabled or not.
  105. */
  106. asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
  107. {
  108. struct irqaction *action;
  109. int do_random, cpu;
  110. int ret, retval = 0;
  111. cpu = smp_processor_id();
  112. irq_enter();
  113. kstat_cpu(cpu).irqs[irq - FIRST_IRQ]++;
  114. action = irq_action[irq - FIRST_IRQ];
  115. if (action) {
  116. if (!(action->flags & SA_INTERRUPT))
  117. local_irq_enable();
  118. do_random = 0;
  119. do {
  120. ret = action->handler(irq, action->dev_id, regs);
  121. if (ret == IRQ_HANDLED)
  122. do_random |= action->flags;
  123. retval |= ret;
  124. action = action->next;
  125. } while (action);
  126. if (retval != 1) {
  127. if (retval) {
  128. printk("irq event %d: bogus retval mask %x\n",
  129. irq, retval);
  130. } else {
  131. printk("irq %d: nobody cared\n", irq);
  132. }
  133. }
  134. if (do_random & SA_SAMPLE_RANDOM)
  135. add_interrupt_randomness(irq);
  136. local_irq_disable();
  137. }
  138. irq_exit();
  139. }
  140. /* this function links in a handler into the chain of handlers for the
  141. given irq, and if the irq has never been registred, the appropriate
  142. handler is entered into the interrupt vector
  143. */
  144. int setup_irq(int irq, struct irqaction * new)
  145. {
  146. int shared = 0;
  147. struct irqaction *old, **p;
  148. unsigned long flags;
  149. p = irq_action + irq - FIRST_IRQ;
  150. if ((old = *p) != NULL) {
  151. /* Can't share interrupts unless both agree to */
  152. if (!(old->flags & new->flags & SA_SHIRQ))
  153. return -EBUSY;
  154. /* Can't share interrupts unless both are same type */
  155. if ((old->flags ^ new->flags) & SA_INTERRUPT)
  156. return -EBUSY;
  157. /* add new interrupt at end of irq queue */
  158. do {
  159. p = &old->next;
  160. old = *p;
  161. } while (old);
  162. shared = 1;
  163. }
  164. if (new->flags & SA_SAMPLE_RANDOM)
  165. rand_initialize_irq(irq);
  166. local_save_flags(flags);
  167. local_irq_disable();
  168. *p = new;
  169. if (!shared) {
  170. /* if the irq wasn't registred before, enter it into the vector table
  171. and unmask it physically
  172. */
  173. arch_setup_irq(irq);
  174. unmask_irq(irq);
  175. }
  176. local_irq_restore(flags);
  177. return 0;
  178. }
  179. /* this function is called by a driver to register an irq handler
  180. Valid flags:
  181. SA_INTERRUPT -> it's a fast interrupt, handler called with irq disabled and
  182. no signal checking etc is performed upon exit
  183. SA_SHIRQ -> the interrupt can be shared between different handlers, the handler
  184. is required to check if the irq was "aimed" at it explicitely
  185. SA_RANDOM -> the interrupt will add to the random generators entropy
  186. */
  187. int request_irq(unsigned int irq,
  188. irqreturn_t (*handler)(int, void *, struct pt_regs *),
  189. unsigned long irqflags,
  190. const char * devname,
  191. void *dev_id)
  192. {
  193. int retval;
  194. struct irqaction * action;
  195. if(!handler)
  196. return -EINVAL;
  197. /* allocate and fill in a handler structure and setup the irq */
  198. action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
  199. if (!action)
  200. return -ENOMEM;
  201. action->handler = handler;
  202. action->flags = irqflags;
  203. cpus_clear(action->mask);
  204. action->name = devname;
  205. action->next = NULL;
  206. action->dev_id = dev_id;
  207. retval = setup_irq(irq, action);
  208. if (retval)
  209. kfree(action);
  210. return retval;
  211. }
  212. EXPORT_SYMBOL(request_irq);
  213. void free_irq(unsigned int irq, void *dev_id)
  214. {
  215. struct irqaction * action, **p;
  216. unsigned long flags;
  217. if (irq >= NR_IRQS) {
  218. printk("Trying to free IRQ%d\n",irq);
  219. return;
  220. }
  221. for (p = irq - FIRST_IRQ + irq_action; (action = *p) != NULL; p = &action->next) {
  222. if (action->dev_id != dev_id)
  223. continue;
  224. /* Found it - now free it */
  225. local_save_flags(flags);
  226. local_irq_disable();
  227. *p = action->next;
  228. if (!irq_action[irq - FIRST_IRQ]) {
  229. mask_irq(irq);
  230. arch_free_irq(irq);
  231. }
  232. local_irq_restore(flags);
  233. kfree(action);
  234. return;
  235. }
  236. printk("Trying to free free IRQ%d\n",irq);
  237. }
  238. EXPORT_SYMBOL(free_irq);
  239. void weird_irq(void)
  240. {
  241. local_irq_disable();
  242. printk("weird irq\n");
  243. while(1);
  244. }
  245. #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
  246. /* Used by other archs to show/control IRQ steering during SMP */
  247. void __init
  248. init_irq_proc(void)
  249. {
  250. }
  251. #endif