irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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_cpu(j).irqs[i]);
  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. 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 pid, events, err, n;
  94. pid = os_getpid();
  95. err = os_set_fd_async(fd, pid);
  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. .pid = pid,
  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. 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. err = request_irq(irq, handler, irqflags, devname, dev_id);
  295. if (err)
  296. return err;
  297. if (fd != -1)
  298. err = activate_fd(irq, fd, type, dev_id);
  299. return err;
  300. }
  301. EXPORT_SYMBOL(um_request_irq);
  302. EXPORT_SYMBOL(reactivate_fd);
  303. /*
  304. * hw_interrupt_type 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 hw_interrupt_type 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 hw_interrupt_type 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. int init_aio_irq(int irq, char *name, irq_handler_t handler)
  345. {
  346. int fds[2], err;
  347. err = os_pipe(fds, 1, 1);
  348. if (err) {
  349. printk(KERN_ERR "init_aio_irq - os_pipe failed, err = %d\n",
  350. -err);
  351. goto out;
  352. }
  353. err = um_request_irq(irq, fds[0], IRQ_READ, handler,
  354. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, name,
  355. (void *) (long) fds[0]);
  356. if (err) {
  357. printk(KERN_ERR "init_aio_irq - : um_request_irq failed, "
  358. "err = %d\n",
  359. err);
  360. goto out_close;
  361. }
  362. err = fds[1];
  363. goto out;
  364. out_close:
  365. os_close_file(fds[0]);
  366. os_close_file(fds[1]);
  367. out:
  368. return err;
  369. }
  370. /*
  371. * IRQ stack entry and exit:
  372. *
  373. * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
  374. * and switch over to the IRQ stack after some preparation. We use
  375. * sigaltstack to receive signals on a separate stack from the start.
  376. * These two functions make sure the rest of the kernel won't be too
  377. * upset by being on a different stack. The IRQ stack has a
  378. * thread_info structure at the bottom so that current et al continue
  379. * to work.
  380. *
  381. * to_irq_stack copies the current task's thread_info to the IRQ stack
  382. * thread_info and sets the tasks's stack to point to the IRQ stack.
  383. *
  384. * from_irq_stack copies the thread_info struct back (flags may have
  385. * been modified) and resets the task's stack pointer.
  386. *
  387. * Tricky bits -
  388. *
  389. * What happens when two signals race each other? UML doesn't block
  390. * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
  391. * could arrive while a previous one is still setting up the
  392. * thread_info.
  393. *
  394. * There are three cases -
  395. * The first interrupt on the stack - sets up the thread_info and
  396. * handles the interrupt
  397. * A nested interrupt interrupting the copying of the thread_info -
  398. * can't handle the interrupt, as the stack is in an unknown state
  399. * A nested interrupt not interrupting the copying of the
  400. * thread_info - doesn't do any setup, just handles the interrupt
  401. *
  402. * The first job is to figure out whether we interrupted stack setup.
  403. * This is done by xchging the signal mask with thread_info->pending.
  404. * If the value that comes back is zero, then there is no setup in
  405. * progress, and the interrupt can be handled. If the value is
  406. * non-zero, then there is stack setup in progress. In order to have
  407. * the interrupt handled, we leave our signal in the mask, and it will
  408. * be handled by the upper handler after it has set up the stack.
  409. *
  410. * Next is to figure out whether we are the outer handler or a nested
  411. * one. As part of setting up the stack, thread_info->real_thread is
  412. * set to non-NULL (and is reset to NULL on exit). This is the
  413. * nesting indicator. If it is non-NULL, then the stack is already
  414. * set up and the handler can run.
  415. */
  416. static unsigned long pending_mask;
  417. unsigned long to_irq_stack(unsigned long *mask_out)
  418. {
  419. struct thread_info *ti;
  420. unsigned long mask, old;
  421. int nested;
  422. mask = xchg(&pending_mask, *mask_out);
  423. if (mask != 0) {
  424. /*
  425. * If any interrupts come in at this point, we want to
  426. * make sure that their bits aren't lost by our
  427. * putting our bit in. So, this loop accumulates bits
  428. * until xchg returns the same value that we put in.
  429. * When that happens, there were no new interrupts,
  430. * and pending_mask contains a bit for each interrupt
  431. * that came in.
  432. */
  433. old = *mask_out;
  434. do {
  435. old |= mask;
  436. mask = xchg(&pending_mask, old);
  437. } while (mask != old);
  438. return 1;
  439. }
  440. ti = current_thread_info();
  441. nested = (ti->real_thread != NULL);
  442. if (!nested) {
  443. struct task_struct *task;
  444. struct thread_info *tti;
  445. task = cpu_tasks[ti->cpu].task;
  446. tti = task_thread_info(task);
  447. *ti = *tti;
  448. ti->real_thread = tti;
  449. task->stack = ti;
  450. }
  451. mask = xchg(&pending_mask, 0);
  452. *mask_out |= mask | nested;
  453. return 0;
  454. }
  455. unsigned long from_irq_stack(int nested)
  456. {
  457. struct thread_info *ti, *to;
  458. unsigned long mask;
  459. ti = current_thread_info();
  460. pending_mask = 1;
  461. to = ti->real_thread;
  462. current->stack = to;
  463. ti->real_thread = NULL;
  464. *to = *ti;
  465. mask = xchg(&pending_mask, 0);
  466. return mask & ~1;
  467. }