irq.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. */
  7. #include "linux/kernel.h"
  8. #include "linux/module.h"
  9. #include "linux/smp.h"
  10. #include "linux/kernel_stat.h"
  11. #include "linux/interrupt.h"
  12. #include "linux/random.h"
  13. #include "linux/slab.h"
  14. #include "linux/file.h"
  15. #include "linux/proc_fs.h"
  16. #include "linux/init.h"
  17. #include "linux/seq_file.h"
  18. #include "linux/profile.h"
  19. #include "linux/hardirq.h"
  20. #include "asm/irq.h"
  21. #include "asm/hw_irq.h"
  22. #include "asm/atomic.h"
  23. #include "asm/signal.h"
  24. #include "asm/system.h"
  25. #include "asm/errno.h"
  26. #include "asm/uaccess.h"
  27. #include "user_util.h"
  28. #include "kern_util.h"
  29. #include "irq_user.h"
  30. #include "irq_kern.h"
  31. #include "os.h"
  32. #include "sigio.h"
  33. #include "misc_constants.h"
  34. /*
  35. * Generic, controller-independent functions:
  36. */
  37. int show_interrupts(struct seq_file *p, void *v)
  38. {
  39. int i = *(loff_t *) v, j;
  40. struct irqaction * action;
  41. unsigned long flags;
  42. if (i == 0) {
  43. seq_printf(p, " ");
  44. for_each_online_cpu(j)
  45. seq_printf(p, "CPU%d ",j);
  46. seq_putc(p, '\n');
  47. }
  48. if (i < NR_IRQS) {
  49. spin_lock_irqsave(&irq_desc[i].lock, flags);
  50. action = irq_desc[i].action;
  51. if (!action)
  52. goto skip;
  53. seq_printf(p, "%3d: ",i);
  54. #ifndef CONFIG_SMP
  55. seq_printf(p, "%10u ", kstat_irqs(i));
  56. #else
  57. for_each_online_cpu(j)
  58. seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
  59. #endif
  60. seq_printf(p, " %14s", irq_desc[i].chip->typename);
  61. seq_printf(p, " %s", action->name);
  62. for (action=action->next; action; action = action->next)
  63. seq_printf(p, ", %s", action->name);
  64. seq_putc(p, '\n');
  65. skip:
  66. spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  67. } else if (i == NR_IRQS) {
  68. seq_putc(p, '\n');
  69. }
  70. return 0;
  71. }
  72. struct irq_fd *active_fds = NULL;
  73. static struct irq_fd **last_irq_ptr = &active_fds;
  74. extern void free_irqs(void);
  75. void sigio_handler(int sig, union uml_pt_regs *regs)
  76. {
  77. struct irq_fd *irq_fd;
  78. int n;
  79. if (smp_sigio_handler())
  80. return;
  81. while (1) {
  82. n = os_waiting_for_events(active_fds);
  83. if (n <= 0) {
  84. if(n == -EINTR) continue;
  85. else break;
  86. }
  87. for (irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next) {
  88. if (irq_fd->current_events != 0) {
  89. irq_fd->current_events = 0;
  90. do_IRQ(irq_fd->irq, regs);
  91. }
  92. }
  93. }
  94. free_irqs();
  95. }
  96. static DEFINE_SPINLOCK(irq_lock);
  97. int activate_fd(int irq, int fd, int type, void *dev_id)
  98. {
  99. struct pollfd *tmp_pfd;
  100. struct irq_fd *new_fd, *irq_fd;
  101. unsigned long flags;
  102. int pid, events, err, n;
  103. pid = os_getpid();
  104. err = os_set_fd_async(fd, pid);
  105. if (err < 0)
  106. goto out;
  107. new_fd = um_kmalloc(sizeof(*new_fd));
  108. err = -ENOMEM;
  109. if (new_fd == NULL)
  110. goto out;
  111. if (type == IRQ_READ)
  112. events = UM_POLLIN | UM_POLLPRI;
  113. else
  114. events = UM_POLLOUT;
  115. *new_fd = ((struct irq_fd) { .next = NULL,
  116. .id = dev_id,
  117. .fd = fd,
  118. .type = type,
  119. .irq = irq,
  120. .pid = pid,
  121. .events = events,
  122. .current_events = 0 } );
  123. spin_lock_irqsave(&irq_lock, flags);
  124. for (irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next) {
  125. if ((irq_fd->fd == fd) && (irq_fd->type == type)) {
  126. printk("Registering fd %d twice\n", fd);
  127. printk("Irqs : %d, %d\n", irq_fd->irq, irq);
  128. printk("Ids : 0x%p, 0x%p\n", irq_fd->id, dev_id);
  129. goto out_unlock;
  130. }
  131. }
  132. if (type == IRQ_WRITE)
  133. fd = -1;
  134. tmp_pfd = NULL;
  135. n = 0;
  136. while (1) {
  137. n = os_create_pollfd(fd, events, tmp_pfd, n);
  138. if (n == 0)
  139. break;
  140. /* n > 0
  141. * It means we couldn't put new pollfd to current pollfds
  142. * and tmp_fds is NULL or too small for new pollfds array.
  143. * Needed size is equal to n as minimum.
  144. *
  145. * Here we have to drop the lock in order to call
  146. * kmalloc, which might sleep.
  147. * If something else came in and changed the pollfds array
  148. * so we will not be able to put new pollfd struct to pollfds
  149. * then we free the buffer tmp_fds and try again.
  150. */
  151. spin_unlock_irqrestore(&irq_lock, flags);
  152. kfree(tmp_pfd);
  153. tmp_pfd = NULL;
  154. tmp_pfd = um_kmalloc(n);
  155. if (tmp_pfd == NULL)
  156. goto out_kfree;
  157. spin_lock_irqsave(&irq_lock, flags);
  158. }
  159. *last_irq_ptr = new_fd;
  160. last_irq_ptr = &new_fd->next;
  161. spin_unlock_irqrestore(&irq_lock, flags);
  162. /* This calls activate_fd, so it has to be outside the critical
  163. * section.
  164. */
  165. maybe_sigio_broken(fd, (type == IRQ_READ));
  166. return 0;
  167. out_unlock:
  168. spin_unlock_irqrestore(&irq_lock, flags);
  169. out_kfree:
  170. kfree(new_fd);
  171. out:
  172. return err;
  173. }
  174. static void free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg)
  175. {
  176. unsigned long flags;
  177. spin_lock_irqsave(&irq_lock, flags);
  178. os_free_irq_by_cb(test, arg, active_fds, &last_irq_ptr);
  179. spin_unlock_irqrestore(&irq_lock, flags);
  180. }
  181. struct irq_and_dev {
  182. int irq;
  183. void *dev;
  184. };
  185. static int same_irq_and_dev(struct irq_fd *irq, void *d)
  186. {
  187. struct irq_and_dev *data = d;
  188. return ((irq->irq == data->irq) && (irq->id == data->dev));
  189. }
  190. void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  191. {
  192. struct irq_and_dev data = ((struct irq_and_dev) { .irq = irq,
  193. .dev = dev });
  194. free_irq_by_cb(same_irq_and_dev, &data);
  195. }
  196. static int same_fd(struct irq_fd *irq, void *fd)
  197. {
  198. return (irq->fd == *((int *)fd));
  199. }
  200. void free_irq_by_fd(int fd)
  201. {
  202. free_irq_by_cb(same_fd, &fd);
  203. }
  204. static struct irq_fd *find_irq_by_fd(int fd, int irqnum, int *index_out)
  205. {
  206. struct irq_fd *irq;
  207. int i = 0;
  208. int fdi;
  209. for (irq = active_fds; irq != NULL; irq = irq->next) {
  210. if ((irq->fd == fd) && (irq->irq == irqnum))
  211. break;
  212. i++;
  213. }
  214. if (irq == NULL) {
  215. printk("find_irq_by_fd doesn't have descriptor %d\n", fd);
  216. goto out;
  217. }
  218. fdi = os_get_pollfd(i);
  219. if ((fdi != -1) && (fdi != fd)) {
  220. printk("find_irq_by_fd - mismatch between active_fds and "
  221. "pollfds, fd %d vs %d, need %d\n", irq->fd,
  222. fdi, fd);
  223. irq = NULL;
  224. goto out;
  225. }
  226. *index_out = i;
  227. out:
  228. return irq;
  229. }
  230. void reactivate_fd(int fd, int irqnum)
  231. {
  232. struct irq_fd *irq;
  233. unsigned long flags;
  234. int i;
  235. spin_lock_irqsave(&irq_lock, flags);
  236. irq = find_irq_by_fd(fd, irqnum, &i);
  237. if (irq == NULL) {
  238. spin_unlock_irqrestore(&irq_lock, flags);
  239. return;
  240. }
  241. os_set_pollfd(i, irq->fd);
  242. spin_unlock_irqrestore(&irq_lock, flags);
  243. add_sigio_fd(fd);
  244. }
  245. void deactivate_fd(int fd, int irqnum)
  246. {
  247. struct irq_fd *irq;
  248. unsigned long flags;
  249. int i;
  250. spin_lock_irqsave(&irq_lock, flags);
  251. irq = find_irq_by_fd(fd, irqnum, &i);
  252. if(irq == NULL){
  253. spin_unlock_irqrestore(&irq_lock, flags);
  254. return;
  255. }
  256. os_set_pollfd(i, -1);
  257. spin_unlock_irqrestore(&irq_lock, flags);
  258. ignore_sigio_fd(fd);
  259. }
  260. int deactivate_all_fds(void)
  261. {
  262. struct irq_fd *irq;
  263. int err;
  264. for (irq = active_fds; irq != NULL; irq = irq->next) {
  265. err = os_clear_fd_async(irq->fd);
  266. if (err)
  267. return err;
  268. }
  269. /* If there is a signal already queued, after unblocking ignore it */
  270. os_set_ioignore();
  271. return 0;
  272. }
  273. #ifdef CONFIG_MODE_TT
  274. void forward_interrupts(int pid)
  275. {
  276. struct irq_fd *irq;
  277. unsigned long flags;
  278. int err;
  279. spin_lock_irqsave(&irq_lock, flags);
  280. for (irq = active_fds; irq != NULL; irq = irq->next) {
  281. err = os_set_owner(irq->fd, pid);
  282. if (err < 0) {
  283. /* XXX Just remove the irq rather than
  284. * print out an infinite stream of these
  285. */
  286. printk("Failed to forward %d to pid %d, err = %d\n",
  287. irq->fd, pid, -err);
  288. }
  289. irq->pid = pid;
  290. }
  291. spin_unlock_irqrestore(&irq_lock, flags);
  292. }
  293. #endif
  294. /*
  295. * do_IRQ handles all normal device IRQ's (the special
  296. * SMP cross-CPU interrupts have their own specific
  297. * handlers).
  298. */
  299. unsigned int do_IRQ(int irq, union uml_pt_regs *regs)
  300. {
  301. irq_enter();
  302. __do_IRQ(irq, (struct pt_regs *)regs);
  303. irq_exit();
  304. return 1;
  305. }
  306. int um_request_irq(unsigned int irq, int fd, int type,
  307. irqreturn_t (*handler)(int, void *, struct pt_regs *),
  308. unsigned long irqflags, const char * devname,
  309. void *dev_id)
  310. {
  311. int err;
  312. err = request_irq(irq, handler, irqflags, devname, dev_id);
  313. if (err)
  314. return err;
  315. if (fd != -1)
  316. err = activate_fd(irq, fd, type, dev_id);
  317. return err;
  318. }
  319. EXPORT_SYMBOL(um_request_irq);
  320. EXPORT_SYMBOL(reactivate_fd);
  321. /* hw_interrupt_type must define (startup || enable) &&
  322. * (shutdown || disable) && end */
  323. static void dummy(unsigned int irq)
  324. {
  325. }
  326. /* This is used for everything else than the timer. */
  327. static struct hw_interrupt_type normal_irq_type = {
  328. .typename = "SIGIO",
  329. .release = free_irq_by_irq_and_dev,
  330. .disable = dummy,
  331. .enable = dummy,
  332. .ack = dummy,
  333. .end = dummy
  334. };
  335. static struct hw_interrupt_type SIGVTALRM_irq_type = {
  336. .typename = "SIGVTALRM",
  337. .release = free_irq_by_irq_and_dev,
  338. .shutdown = dummy, /* never called */
  339. .disable = dummy,
  340. .enable = dummy,
  341. .ack = dummy,
  342. .end = dummy
  343. };
  344. void __init init_IRQ(void)
  345. {
  346. int i;
  347. irq_desc[TIMER_IRQ].status = IRQ_DISABLED;
  348. irq_desc[TIMER_IRQ].action = NULL;
  349. irq_desc[TIMER_IRQ].depth = 1;
  350. irq_desc[TIMER_IRQ].chip = &SIGVTALRM_irq_type;
  351. enable_irq(TIMER_IRQ);
  352. for (i = 1; i < NR_IRQS; i++) {
  353. irq_desc[i].status = IRQ_DISABLED;
  354. irq_desc[i].action = NULL;
  355. irq_desc[i].depth = 1;
  356. irq_desc[i].chip = &normal_irq_type;
  357. enable_irq(i);
  358. }
  359. }
  360. int init_aio_irq(int irq, char *name, irqreturn_t (*handler)(int, void *,
  361. struct pt_regs *))
  362. {
  363. int fds[2], err;
  364. err = os_pipe(fds, 1, 1);
  365. if (err) {
  366. printk("init_aio_irq - os_pipe failed, err = %d\n", -err);
  367. goto out;
  368. }
  369. err = um_request_irq(irq, fds[0], IRQ_READ, handler,
  370. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, name,
  371. (void *) (long) fds[0]);
  372. if (err) {
  373. printk("init_aio_irq - : um_request_irq failed, err = %d\n",
  374. err);
  375. goto out_close;
  376. }
  377. err = fds[1];
  378. goto out;
  379. out_close:
  380. os_close_file(fds[0]);
  381. os_close_file(fds[1]);
  382. out:
  383. return err;
  384. }