irq.c 14 KB

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