irq.c 14 KB

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