irq.c 9.5 KB

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