irq.c 12 KB

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