irq.c 12 KB

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