irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.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/cpumask.h>
  8. #include <linux/hardirq.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel_stat.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <as-layout.h>
  16. #include <kern_util.h>
  17. #include <os.h>
  18. /*
  19. * This list is accessed under irq_lock, except in sigio_handler,
  20. * where it is safe from being modified. IRQ handlers won't change it -
  21. * if an IRQ source has vanished, it will be freed by free_irqs just
  22. * before returning from sigio_handler. That will process a separate
  23. * list of irqs to free, with its own locking, coming back here to
  24. * remove list elements, taking the irq_lock to do so.
  25. */
  26. static struct irq_fd *active_fds = NULL;
  27. static struct irq_fd **last_irq_ptr = &active_fds;
  28. extern void free_irqs(void);
  29. void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  30. {
  31. struct irq_fd *irq_fd;
  32. int n;
  33. while (1) {
  34. n = os_waiting_for_events(active_fds);
  35. if (n <= 0) {
  36. if (n == -EINTR)
  37. continue;
  38. else break;
  39. }
  40. for (irq_fd = active_fds; irq_fd != NULL;
  41. irq_fd = irq_fd->next) {
  42. if (irq_fd->current_events != 0) {
  43. irq_fd->current_events = 0;
  44. do_IRQ(irq_fd->irq, regs);
  45. }
  46. }
  47. }
  48. free_irqs();
  49. }
  50. static DEFINE_SPINLOCK(irq_lock);
  51. static int activate_fd(int irq, int fd, int type, void *dev_id)
  52. {
  53. struct pollfd *tmp_pfd;
  54. struct irq_fd *new_fd, *irq_fd;
  55. unsigned long flags;
  56. int events, err, n;
  57. err = os_set_fd_async(fd);
  58. if (err < 0)
  59. goto out;
  60. err = -ENOMEM;
  61. new_fd = kmalloc(sizeof(struct irq_fd), GFP_KERNEL);
  62. if (new_fd == NULL)
  63. goto out;
  64. if (type == IRQ_READ)
  65. events = UM_POLLIN | UM_POLLPRI;
  66. else events = UM_POLLOUT;
  67. *new_fd = ((struct irq_fd) { .next = NULL,
  68. .id = dev_id,
  69. .fd = fd,
  70. .type = type,
  71. .irq = irq,
  72. .events = events,
  73. .current_events = 0 } );
  74. err = -EBUSY;
  75. spin_lock_irqsave(&irq_lock, flags);
  76. for (irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next) {
  77. if ((irq_fd->fd == fd) && (irq_fd->type == type)) {
  78. printk(KERN_ERR "Registering fd %d twice\n", fd);
  79. printk(KERN_ERR "Irqs : %d, %d\n", irq_fd->irq, irq);
  80. printk(KERN_ERR "Ids : 0x%p, 0x%p\n", irq_fd->id,
  81. dev_id);
  82. goto out_unlock;
  83. }
  84. }
  85. if (type == IRQ_WRITE)
  86. fd = -1;
  87. tmp_pfd = NULL;
  88. n = 0;
  89. while (1) {
  90. n = os_create_pollfd(fd, events, tmp_pfd, n);
  91. if (n == 0)
  92. break;
  93. /*
  94. * n > 0
  95. * It means we couldn't put new pollfd to current pollfds
  96. * and tmp_fds is NULL or too small for new pollfds array.
  97. * Needed size is equal to n as minimum.
  98. *
  99. * Here we have to drop the lock in order to call
  100. * kmalloc, which might sleep.
  101. * If something else came in and changed the pollfds array
  102. * so we will not be able to put new pollfd struct to pollfds
  103. * then we free the buffer tmp_fds and try again.
  104. */
  105. spin_unlock_irqrestore(&irq_lock, flags);
  106. kfree(tmp_pfd);
  107. tmp_pfd = kmalloc(n, GFP_KERNEL);
  108. if (tmp_pfd == NULL)
  109. goto out_kfree;
  110. spin_lock_irqsave(&irq_lock, flags);
  111. }
  112. *last_irq_ptr = new_fd;
  113. last_irq_ptr = &new_fd->next;
  114. spin_unlock_irqrestore(&irq_lock, flags);
  115. /*
  116. * This calls activate_fd, so it has to be outside the critical
  117. * section.
  118. */
  119. maybe_sigio_broken(fd, (type == IRQ_READ));
  120. return 0;
  121. out_unlock:
  122. spin_unlock_irqrestore(&irq_lock, flags);
  123. out_kfree:
  124. kfree(new_fd);
  125. out:
  126. return err;
  127. }
  128. static void free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg)
  129. {
  130. unsigned long flags;
  131. spin_lock_irqsave(&irq_lock, flags);
  132. os_free_irq_by_cb(test, arg, active_fds, &last_irq_ptr);
  133. spin_unlock_irqrestore(&irq_lock, flags);
  134. }
  135. struct irq_and_dev {
  136. int irq;
  137. void *dev;
  138. };
  139. static int same_irq_and_dev(struct irq_fd *irq, void *d)
  140. {
  141. struct irq_and_dev *data = d;
  142. return ((irq->irq == data->irq) && (irq->id == data->dev));
  143. }
  144. static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  145. {
  146. struct irq_and_dev data = ((struct irq_and_dev) { .irq = irq,
  147. .dev = dev });
  148. free_irq_by_cb(same_irq_and_dev, &data);
  149. }
  150. static int same_fd(struct irq_fd *irq, void *fd)
  151. {
  152. return (irq->fd == *((int *)fd));
  153. }
  154. void free_irq_by_fd(int fd)
  155. {
  156. free_irq_by_cb(same_fd, &fd);
  157. }
  158. /* Must be called with irq_lock held */
  159. static struct irq_fd *find_irq_by_fd(int fd, int irqnum, int *index_out)
  160. {
  161. struct irq_fd *irq;
  162. int i = 0;
  163. int fdi;
  164. for (irq = active_fds; irq != NULL; irq = irq->next) {
  165. if ((irq->fd == fd) && (irq->irq == irqnum))
  166. break;
  167. i++;
  168. }
  169. if (irq == NULL) {
  170. printk(KERN_ERR "find_irq_by_fd doesn't have descriptor %d\n",
  171. fd);
  172. goto out;
  173. }
  174. fdi = os_get_pollfd(i);
  175. if ((fdi != -1) && (fdi != fd)) {
  176. printk(KERN_ERR "find_irq_by_fd - mismatch between active_fds "
  177. "and pollfds, fd %d vs %d, need %d\n", irq->fd,
  178. fdi, fd);
  179. irq = NULL;
  180. goto out;
  181. }
  182. *index_out = i;
  183. out:
  184. return irq;
  185. }
  186. void reactivate_fd(int fd, int irqnum)
  187. {
  188. struct irq_fd *irq;
  189. unsigned long flags;
  190. int i;
  191. spin_lock_irqsave(&irq_lock, flags);
  192. irq = find_irq_by_fd(fd, irqnum, &i);
  193. if (irq == NULL) {
  194. spin_unlock_irqrestore(&irq_lock, flags);
  195. return;
  196. }
  197. os_set_pollfd(i, irq->fd);
  198. spin_unlock_irqrestore(&irq_lock, flags);
  199. add_sigio_fd(fd);
  200. }
  201. void deactivate_fd(int fd, int irqnum)
  202. {
  203. struct irq_fd *irq;
  204. unsigned long flags;
  205. int i;
  206. spin_lock_irqsave(&irq_lock, flags);
  207. irq = find_irq_by_fd(fd, irqnum, &i);
  208. if (irq == NULL) {
  209. spin_unlock_irqrestore(&irq_lock, flags);
  210. return;
  211. }
  212. os_set_pollfd(i, -1);
  213. spin_unlock_irqrestore(&irq_lock, flags);
  214. ignore_sigio_fd(fd);
  215. }
  216. EXPORT_SYMBOL(deactivate_fd);
  217. /*
  218. * Called just before shutdown in order to provide a clean exec
  219. * environment in case the system is rebooting. No locking because
  220. * that would cause a pointless shutdown hang if something hadn't
  221. * released the lock.
  222. */
  223. int deactivate_all_fds(void)
  224. {
  225. struct irq_fd *irq;
  226. int err;
  227. for (irq = active_fds; irq != NULL; irq = irq->next) {
  228. err = os_clear_fd_async(irq->fd);
  229. if (err)
  230. return err;
  231. }
  232. /* If there is a signal already queued, after unblocking ignore it */
  233. os_set_ioignore();
  234. return 0;
  235. }
  236. /*
  237. * do_IRQ handles all normal device IRQs (the special
  238. * SMP cross-CPU interrupts have their own specific
  239. * handlers).
  240. */
  241. unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
  242. {
  243. struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
  244. irq_enter();
  245. generic_handle_irq(irq);
  246. irq_exit();
  247. set_irq_regs(old_regs);
  248. return 1;
  249. }
  250. void um_free_irq(unsigned int irq, void *dev)
  251. {
  252. free_irq_by_irq_and_dev(irq, dev);
  253. free_irq(irq, dev);
  254. }
  255. EXPORT_SYMBOL(um_free_irq);
  256. int um_request_irq(unsigned int irq, int fd, int type,
  257. irq_handler_t handler,
  258. unsigned long irqflags, const char * devname,
  259. void *dev_id)
  260. {
  261. int err;
  262. if (fd != -1) {
  263. err = activate_fd(irq, fd, type, dev_id);
  264. if (err)
  265. return err;
  266. }
  267. return request_irq(irq, handler, irqflags, devname, dev_id);
  268. }
  269. EXPORT_SYMBOL(um_request_irq);
  270. EXPORT_SYMBOL(reactivate_fd);
  271. /*
  272. * irq_chip must define at least enable/disable and ack when
  273. * the edge handler is used.
  274. */
  275. static void dummy(struct irq_data *d)
  276. {
  277. }
  278. /* This is used for everything else than the timer. */
  279. static struct irq_chip normal_irq_type = {
  280. .name = "SIGIO",
  281. .irq_disable = dummy,
  282. .irq_enable = dummy,
  283. .irq_ack = dummy,
  284. .irq_mask = dummy,
  285. .irq_unmask = dummy,
  286. };
  287. static struct irq_chip SIGVTALRM_irq_type = {
  288. .name = "SIGVTALRM",
  289. .irq_disable = dummy,
  290. .irq_enable = dummy,
  291. .irq_ack = dummy,
  292. .irq_mask = dummy,
  293. .irq_unmask = dummy,
  294. };
  295. void __init init_IRQ(void)
  296. {
  297. int i;
  298. irq_set_chip_and_handler(TIMER_IRQ, &SIGVTALRM_irq_type, handle_edge_irq);
  299. for (i = 1; i < NR_IRQS; i++)
  300. irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
  301. }
  302. /*
  303. * IRQ stack entry and exit:
  304. *
  305. * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
  306. * and switch over to the IRQ stack after some preparation. We use
  307. * sigaltstack to receive signals on a separate stack from the start.
  308. * These two functions make sure the rest of the kernel won't be too
  309. * upset by being on a different stack. The IRQ stack has a
  310. * thread_info structure at the bottom so that current et al continue
  311. * to work.
  312. *
  313. * to_irq_stack copies the current task's thread_info to the IRQ stack
  314. * thread_info and sets the tasks's stack to point to the IRQ stack.
  315. *
  316. * from_irq_stack copies the thread_info struct back (flags may have
  317. * been modified) and resets the task's stack pointer.
  318. *
  319. * Tricky bits -
  320. *
  321. * What happens when two signals race each other? UML doesn't block
  322. * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
  323. * could arrive while a previous one is still setting up the
  324. * thread_info.
  325. *
  326. * There are three cases -
  327. * The first interrupt on the stack - sets up the thread_info and
  328. * handles the interrupt
  329. * A nested interrupt interrupting the copying of the thread_info -
  330. * can't handle the interrupt, as the stack is in an unknown state
  331. * A nested interrupt not interrupting the copying of the
  332. * thread_info - doesn't do any setup, just handles the interrupt
  333. *
  334. * The first job is to figure out whether we interrupted stack setup.
  335. * This is done by xchging the signal mask with thread_info->pending.
  336. * If the value that comes back is zero, then there is no setup in
  337. * progress, and the interrupt can be handled. If the value is
  338. * non-zero, then there is stack setup in progress. In order to have
  339. * the interrupt handled, we leave our signal in the mask, and it will
  340. * be handled by the upper handler after it has set up the stack.
  341. *
  342. * Next is to figure out whether we are the outer handler or a nested
  343. * one. As part of setting up the stack, thread_info->real_thread is
  344. * set to non-NULL (and is reset to NULL on exit). This is the
  345. * nesting indicator. If it is non-NULL, then the stack is already
  346. * set up and the handler can run.
  347. */
  348. static unsigned long pending_mask;
  349. unsigned long to_irq_stack(unsigned long *mask_out)
  350. {
  351. struct thread_info *ti;
  352. unsigned long mask, old;
  353. int nested;
  354. mask = xchg(&pending_mask, *mask_out);
  355. if (mask != 0) {
  356. /*
  357. * If any interrupts come in at this point, we want to
  358. * make sure that their bits aren't lost by our
  359. * putting our bit in. So, this loop accumulates bits
  360. * until xchg returns the same value that we put in.
  361. * When that happens, there were no new interrupts,
  362. * and pending_mask contains a bit for each interrupt
  363. * that came in.
  364. */
  365. old = *mask_out;
  366. do {
  367. old |= mask;
  368. mask = xchg(&pending_mask, old);
  369. } while (mask != old);
  370. return 1;
  371. }
  372. ti = current_thread_info();
  373. nested = (ti->real_thread != NULL);
  374. if (!nested) {
  375. struct task_struct *task;
  376. struct thread_info *tti;
  377. task = cpu_tasks[ti->cpu].task;
  378. tti = task_thread_info(task);
  379. *ti = *tti;
  380. ti->real_thread = tti;
  381. task->stack = ti;
  382. }
  383. mask = xchg(&pending_mask, 0);
  384. *mask_out |= mask | nested;
  385. return 0;
  386. }
  387. unsigned long from_irq_stack(int nested)
  388. {
  389. struct thread_info *ti, *to;
  390. unsigned long mask;
  391. ti = current_thread_info();
  392. pending_mask = 1;
  393. to = ti->real_thread;
  394. current->stack = to;
  395. ti->real_thread = NULL;
  396. *to = *ti;
  397. mask = xchg(&pending_mask, 0);
  398. return mask & ~1;
  399. }