irq.c 12 KB

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