irq.c 12 KB

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