irq.c 10 KB

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