irq.c 11 KB

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