irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 void maybe_sigio_broken(int fd, int type)
  98. {
  99. if (os_isatty(fd)) {
  100. if ((type == IRQ_WRITE) && !pty_output_sigio) {
  101. write_sigio_workaround();
  102. add_sigio_fd(fd, 0);
  103. } else if ((type == IRQ_READ) && !pty_close_sigio) {
  104. write_sigio_workaround();
  105. add_sigio_fd(fd, 1);
  106. }
  107. }
  108. }
  109. int activate_fd(int irq, int fd, int type, void *dev_id)
  110. {
  111. struct pollfd *tmp_pfd;
  112. struct irq_fd *new_fd, *irq_fd;
  113. unsigned long flags;
  114. int pid, events, err, n;
  115. pid = os_getpid();
  116. err = os_set_fd_async(fd, pid);
  117. if (err < 0)
  118. goto out;
  119. new_fd = um_kmalloc(sizeof(*new_fd));
  120. err = -ENOMEM;
  121. if (new_fd == NULL)
  122. goto out;
  123. if (type == IRQ_READ)
  124. events = UM_POLLIN | UM_POLLPRI;
  125. else
  126. events = UM_POLLOUT;
  127. *new_fd = ((struct irq_fd) { .next = NULL,
  128. .id = dev_id,
  129. .fd = fd,
  130. .type = type,
  131. .irq = irq,
  132. .pid = pid,
  133. .events = events,
  134. .current_events = 0 } );
  135. /* Critical section - locked by a spinlock because this stuff can
  136. * be changed from interrupt handlers. The stuff above is done
  137. * outside the lock because it allocates memory.
  138. */
  139. /* Actually, it only looks like it can be called from interrupt
  140. * context. The culprit is reactivate_fd, which calls
  141. * maybe_sigio_broken, which calls write_sigio_workaround,
  142. * which calls activate_fd. However, write_sigio_workaround should
  143. * only be called once, at boot time. That would make it clear that
  144. * this is called only from process context, and can be locked with
  145. * a semaphore.
  146. */
  147. flags = irq_lock();
  148. for (irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next) {
  149. if ((irq_fd->fd == fd) && (irq_fd->type == type)) {
  150. printk("Registering fd %d twice\n", fd);
  151. printk("Irqs : %d, %d\n", irq_fd->irq, irq);
  152. printk("Ids : 0x%p, 0x%p\n", irq_fd->id, dev_id);
  153. goto out_unlock;
  154. }
  155. }
  156. /*-------------*/
  157. if (type == IRQ_WRITE)
  158. fd = -1;
  159. tmp_pfd = NULL;
  160. n = 0;
  161. while (1) {
  162. n = os_create_pollfd(fd, events, tmp_pfd, n);
  163. if (n == 0)
  164. break;
  165. /* n > 0
  166. * It means we couldn't put new pollfd to current pollfds
  167. * and tmp_fds is NULL or too small for new pollfds array.
  168. * Needed size is equal to n as minimum.
  169. *
  170. * Here we have to drop the lock in order to call
  171. * kmalloc, which might sleep.
  172. * If something else came in and changed the pollfds array
  173. * so we will not be able to put new pollfd struct to pollfds
  174. * then we free the buffer tmp_fds and try again.
  175. */
  176. irq_unlock(flags);
  177. kfree(tmp_pfd);
  178. tmp_pfd = NULL;
  179. tmp_pfd = um_kmalloc(n);
  180. if (tmp_pfd == NULL)
  181. goto out_kfree;
  182. flags = irq_lock();
  183. }
  184. /*-------------*/
  185. *last_irq_ptr = new_fd;
  186. last_irq_ptr = &new_fd->next;
  187. irq_unlock(flags);
  188. /* This calls activate_fd, so it has to be outside the critical
  189. * section.
  190. */
  191. maybe_sigio_broken(fd, type);
  192. return(0);
  193. out_unlock:
  194. irq_unlock(flags);
  195. out_kfree:
  196. kfree(new_fd);
  197. out:
  198. return(err);
  199. }
  200. static void free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg)
  201. {
  202. unsigned long flags;
  203. flags = irq_lock();
  204. os_free_irq_by_cb(test, arg, active_fds, &last_irq_ptr);
  205. irq_unlock(flags);
  206. }
  207. struct irq_and_dev {
  208. int irq;
  209. void *dev;
  210. };
  211. static int same_irq_and_dev(struct irq_fd *irq, void *d)
  212. {
  213. struct irq_and_dev *data = d;
  214. return ((irq->irq == data->irq) && (irq->id == data->dev));
  215. }
  216. void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  217. {
  218. struct irq_and_dev data = ((struct irq_and_dev) { .irq = irq,
  219. .dev = dev });
  220. free_irq_by_cb(same_irq_and_dev, &data);
  221. }
  222. static int same_fd(struct irq_fd *irq, void *fd)
  223. {
  224. return (irq->fd == *((int *)fd));
  225. }
  226. void free_irq_by_fd(int fd)
  227. {
  228. free_irq_by_cb(same_fd, &fd);
  229. }
  230. static struct irq_fd *find_irq_by_fd(int fd, int irqnum, int *index_out)
  231. {
  232. struct irq_fd *irq;
  233. int i = 0;
  234. int fdi;
  235. for (irq = active_fds; irq != NULL; irq = irq->next) {
  236. if ((irq->fd == fd) && (irq->irq == irqnum))
  237. break;
  238. i++;
  239. }
  240. if (irq == NULL) {
  241. printk("find_irq_by_fd doesn't have descriptor %d\n", fd);
  242. goto out;
  243. }
  244. fdi = os_get_pollfd(i);
  245. if ((fdi != -1) && (fdi != fd)) {
  246. printk("find_irq_by_fd - mismatch between active_fds and "
  247. "pollfds, fd %d vs %d, need %d\n", irq->fd,
  248. fdi, fd);
  249. irq = NULL;
  250. goto out;
  251. }
  252. *index_out = i;
  253. out:
  254. return irq;
  255. }
  256. void reactivate_fd(int fd, int irqnum)
  257. {
  258. struct irq_fd *irq;
  259. unsigned long flags;
  260. int i;
  261. flags = irq_lock();
  262. irq = find_irq_by_fd(fd, irqnum, &i);
  263. if (irq == NULL) {
  264. irq_unlock(flags);
  265. return;
  266. }
  267. os_set_pollfd(i, irq->fd);
  268. irq_unlock(flags);
  269. /* This calls activate_fd, so it has to be outside the critical
  270. * section.
  271. */
  272. maybe_sigio_broken(fd, irq->type);
  273. }
  274. void deactivate_fd(int fd, int irqnum)
  275. {
  276. struct irq_fd *irq;
  277. unsigned long flags;
  278. int i;
  279. flags = irq_lock();
  280. irq = find_irq_by_fd(fd, irqnum, &i);
  281. if (irq == NULL)
  282. goto out;
  283. os_set_pollfd(i, -1);
  284. out:
  285. irq_unlock(flags);
  286. }
  287. int deactivate_all_fds(void)
  288. {
  289. struct irq_fd *irq;
  290. int err;
  291. for (irq = active_fds; irq != NULL; irq = irq->next) {
  292. err = os_clear_fd_async(irq->fd);
  293. if (err)
  294. return err;
  295. }
  296. /* If there is a signal already queued, after unblocking ignore it */
  297. os_set_ioignore();
  298. return 0;
  299. }
  300. #ifdef CONFIG_MODE_TT
  301. void forward_interrupts(int pid)
  302. {
  303. struct irq_fd *irq;
  304. unsigned long flags;
  305. int err;
  306. flags = irq_lock();
  307. for (irq = active_fds; irq != NULL; irq = irq->next) {
  308. err = os_set_owner(irq->fd, pid);
  309. if (err < 0) {
  310. /* XXX Just remove the irq rather than
  311. * print out an infinite stream of these
  312. */
  313. printk("Failed to forward %d to pid %d, err = %d\n",
  314. irq->fd, pid, -err);
  315. }
  316. irq->pid = pid;
  317. }
  318. irq_unlock(flags);
  319. }
  320. #endif
  321. /*
  322. * do_IRQ handles all normal device IRQ's (the special
  323. * SMP cross-CPU interrupts have their own specific
  324. * handlers).
  325. */
  326. unsigned int do_IRQ(int irq, union uml_pt_regs *regs)
  327. {
  328. irq_enter();
  329. __do_IRQ(irq, (struct pt_regs *)regs);
  330. irq_exit();
  331. return 1;
  332. }
  333. int um_request_irq(unsigned int irq, int fd, int type,
  334. irqreturn_t (*handler)(int, void *, struct pt_regs *),
  335. unsigned long irqflags, const char * devname,
  336. void *dev_id)
  337. {
  338. int err;
  339. err = request_irq(irq, handler, irqflags, devname, dev_id);
  340. if (err)
  341. return err;
  342. if (fd != -1)
  343. err = activate_fd(irq, fd, type, dev_id);
  344. return err;
  345. }
  346. EXPORT_SYMBOL(um_request_irq);
  347. EXPORT_SYMBOL(reactivate_fd);
  348. static DEFINE_SPINLOCK(irq_spinlock);
  349. unsigned long irq_lock(void)
  350. {
  351. unsigned long flags;
  352. spin_lock_irqsave(&irq_spinlock, flags);
  353. return flags;
  354. }
  355. void irq_unlock(unsigned long flags)
  356. {
  357. spin_unlock_irqrestore(&irq_spinlock, flags);
  358. }
  359. /* hw_interrupt_type must define (startup || enable) &&
  360. * (shutdown || disable) && end */
  361. static void dummy(unsigned int irq)
  362. {
  363. }
  364. /* This is used for everything else than the timer. */
  365. static struct hw_interrupt_type normal_irq_type = {
  366. .typename = "SIGIO",
  367. .release = free_irq_by_irq_and_dev,
  368. .disable = dummy,
  369. .enable = dummy,
  370. .ack = dummy,
  371. .end = dummy
  372. };
  373. static struct hw_interrupt_type SIGVTALRM_irq_type = {
  374. .typename = "SIGVTALRM",
  375. .release = free_irq_by_irq_and_dev,
  376. .shutdown = dummy, /* never called */
  377. .disable = dummy,
  378. .enable = dummy,
  379. .ack = dummy,
  380. .end = dummy
  381. };
  382. void __init init_IRQ(void)
  383. {
  384. int i;
  385. irq_desc[TIMER_IRQ].status = IRQ_DISABLED;
  386. irq_desc[TIMER_IRQ].action = NULL;
  387. irq_desc[TIMER_IRQ].depth = 1;
  388. irq_desc[TIMER_IRQ].chip = &SIGVTALRM_irq_type;
  389. enable_irq(TIMER_IRQ);
  390. for (i = 1; i < NR_IRQS; i++) {
  391. irq_desc[i].status = IRQ_DISABLED;
  392. irq_desc[i].action = NULL;
  393. irq_desc[i].depth = 1;
  394. irq_desc[i].chip = &normal_irq_type;
  395. enable_irq(i);
  396. }
  397. }
  398. int init_aio_irq(int irq, char *name, irqreturn_t (*handler)(int, void *,
  399. struct pt_regs *))
  400. {
  401. int fds[2], err;
  402. err = os_pipe(fds, 1, 1);
  403. if (err) {
  404. printk("init_aio_irq - os_pipe failed, err = %d\n", -err);
  405. goto out;
  406. }
  407. err = um_request_irq(irq, fds[0], IRQ_READ, handler,
  408. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, name,
  409. (void *) (long) fds[0]);
  410. if (err) {
  411. printk("init_aio_irq - : um_request_irq failed, err = %d\n",
  412. err);
  413. goto out_close;
  414. }
  415. err = fds[1];
  416. goto out;
  417. out_close:
  418. os_close_file(fds[0]);
  419. os_close_file(fds[1]);
  420. out:
  421. return err;
  422. }