irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright (C) 2017 - Cambridge Greys Ltd
  3. * Copyright (C) 2011 - 2014 Cisco Systems Inc
  4. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  5. * Licensed under the GPL
  6. * Derived (i.e. mostly copied) from arch/i386/kernel/irq.c:
  7. * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
  8. */
  9. #include <linux/cpumask.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/slab.h>
  17. #include <as-layout.h>
  18. #include <kern_util.h>
  19. #include <os.h>
  20. #include <irq_user.h>
  21. extern void free_irqs(void);
  22. /* When epoll triggers we do not know why it did so
  23. * we can also have different IRQs for read and write.
  24. * This is why we keep a small irq_fd array for each fd -
  25. * one entry per IRQ type
  26. */
  27. struct irq_entry {
  28. struct irq_entry *next;
  29. int fd;
  30. struct irq_fd *irq_array[MAX_IRQ_TYPE + 1];
  31. };
  32. static struct irq_entry *active_fds;
  33. static DEFINE_SPINLOCK(irq_lock);
  34. static void irq_io_loop(struct irq_fd *irq, struct uml_pt_regs *regs)
  35. {
  36. /*
  37. * irq->active guards against reentry
  38. * irq->pending accumulates pending requests
  39. * if pending is raised the irq_handler is re-run
  40. * until pending is cleared
  41. */
  42. if (irq->active) {
  43. irq->active = false;
  44. do {
  45. irq->pending = false;
  46. do_IRQ(irq->irq, regs);
  47. } while (irq->pending && (!irq->purge));
  48. if (!irq->purge)
  49. irq->active = true;
  50. } else {
  51. irq->pending = true;
  52. }
  53. }
  54. void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  55. {
  56. struct irq_entry *irq_entry;
  57. struct irq_fd *irq;
  58. int n, i, j;
  59. while (1) {
  60. /* This is now lockless - epoll keeps back-referencesto the irqs
  61. * which have trigger it so there is no need to walk the irq
  62. * list and lock it every time. We avoid locking by turning off
  63. * IO for a specific fd by executing os_del_epoll_fd(fd) before
  64. * we do any changes to the actual data structures
  65. */
  66. n = os_waiting_for_events_epoll();
  67. if (n <= 0) {
  68. if (n == -EINTR)
  69. continue;
  70. else
  71. break;
  72. }
  73. for (i = 0; i < n ; i++) {
  74. /* Epoll back reference is the entry with 3 irq_fd
  75. * leaves - one for each irq type.
  76. */
  77. irq_entry = (struct irq_entry *)
  78. os_epoll_get_data_pointer(i);
  79. for (j = 0; j < MAX_IRQ_TYPE ; j++) {
  80. irq = irq_entry->irq_array[j];
  81. if (irq == NULL)
  82. continue;
  83. if (os_epoll_triggered(i, irq->events) > 0)
  84. irq_io_loop(irq, regs);
  85. if (irq->purge) {
  86. irq_entry->irq_array[j] = NULL;
  87. kfree(irq);
  88. }
  89. }
  90. }
  91. }
  92. free_irqs();
  93. }
  94. static int assign_epoll_events_to_irq(struct irq_entry *irq_entry)
  95. {
  96. int i;
  97. int events = 0;
  98. struct irq_fd *irq;
  99. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  100. irq = irq_entry->irq_array[i];
  101. if (irq != NULL)
  102. events = irq->events | events;
  103. }
  104. if (events > 0) {
  105. /* os_add_epoll will call os_mod_epoll if this already exists */
  106. return os_add_epoll_fd(events, irq_entry->fd, irq_entry);
  107. }
  108. /* No events - delete */
  109. return os_del_epoll_fd(irq_entry->fd);
  110. }
  111. static int activate_fd(int irq, int fd, int type, void *dev_id)
  112. {
  113. struct irq_fd *new_fd;
  114. struct irq_entry *irq_entry;
  115. int i, err, events;
  116. unsigned long flags;
  117. err = os_set_fd_async(fd);
  118. if (err < 0)
  119. goto out;
  120. spin_lock_irqsave(&irq_lock, flags);
  121. /* Check if we have an entry for this fd */
  122. err = -EBUSY;
  123. for (irq_entry = active_fds;
  124. irq_entry != NULL; irq_entry = irq_entry->next) {
  125. if (irq_entry->fd == fd)
  126. break;
  127. }
  128. if (irq_entry == NULL) {
  129. /* This needs to be atomic as it may be called from an
  130. * IRQ context.
  131. */
  132. irq_entry = kmalloc(sizeof(struct irq_entry), GFP_ATOMIC);
  133. if (irq_entry == NULL) {
  134. printk(KERN_ERR
  135. "Failed to allocate new IRQ entry\n");
  136. goto out_unlock;
  137. }
  138. irq_entry->fd = fd;
  139. for (i = 0; i < MAX_IRQ_TYPE; i++)
  140. irq_entry->irq_array[i] = NULL;
  141. irq_entry->next = active_fds;
  142. active_fds = irq_entry;
  143. }
  144. /* Check if we are trying to re-register an interrupt for a
  145. * particular fd
  146. */
  147. if (irq_entry->irq_array[type] != NULL) {
  148. printk(KERN_ERR
  149. "Trying to reregister IRQ %d FD %d TYPE %d ID %p\n",
  150. irq, fd, type, dev_id
  151. );
  152. goto out_unlock;
  153. } else {
  154. /* New entry for this fd */
  155. err = -ENOMEM;
  156. new_fd = kmalloc(sizeof(struct irq_fd), GFP_ATOMIC);
  157. if (new_fd == NULL)
  158. goto out_unlock;
  159. events = os_event_mask(type);
  160. *new_fd = ((struct irq_fd) {
  161. .id = dev_id,
  162. .irq = irq,
  163. .type = type,
  164. .events = events,
  165. .active = true,
  166. .pending = false,
  167. .purge = false
  168. });
  169. /* Turn off any IO on this fd - allows us to
  170. * avoid locking the IRQ loop
  171. */
  172. os_del_epoll_fd(irq_entry->fd);
  173. irq_entry->irq_array[type] = new_fd;
  174. }
  175. /* Turn back IO on with the correct (new) IO event mask */
  176. assign_epoll_events_to_irq(irq_entry);
  177. spin_unlock_irqrestore(&irq_lock, flags);
  178. maybe_sigio_broken(fd, (type != IRQ_NONE));
  179. return 0;
  180. out_unlock:
  181. spin_unlock_irqrestore(&irq_lock, flags);
  182. out:
  183. return err;
  184. }
  185. /*
  186. * Walk the IRQ list and dispose of any unused entries.
  187. * Should be done under irq_lock.
  188. */
  189. static void garbage_collect_irq_entries(void)
  190. {
  191. int i;
  192. bool reap;
  193. struct irq_entry *walk;
  194. struct irq_entry *previous = NULL;
  195. struct irq_entry *to_free;
  196. if (active_fds == NULL)
  197. return;
  198. walk = active_fds;
  199. while (walk != NULL) {
  200. reap = true;
  201. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  202. if (walk->irq_array[i] != NULL) {
  203. reap = false;
  204. break;
  205. }
  206. }
  207. if (reap) {
  208. if (previous == NULL)
  209. active_fds = walk->next;
  210. else
  211. previous->next = walk->next;
  212. to_free = walk;
  213. } else {
  214. to_free = NULL;
  215. }
  216. walk = walk->next;
  217. if (to_free != NULL)
  218. kfree(to_free);
  219. }
  220. }
  221. /*
  222. * Walk the IRQ list and get the descriptor for our FD
  223. */
  224. static struct irq_entry *get_irq_entry_by_fd(int fd)
  225. {
  226. struct irq_entry *walk = active_fds;
  227. while (walk != NULL) {
  228. if (walk->fd == fd)
  229. return walk;
  230. walk = walk->next;
  231. }
  232. return NULL;
  233. }
  234. /*
  235. * Walk the IRQ list and dispose of an entry for a specific
  236. * device, fd and number. Note - if sharing an IRQ for read
  237. * and writefor the same FD it will be disposed in either case.
  238. * If this behaviour is undesirable use different IRQ ids.
  239. */
  240. #define IGNORE_IRQ 1
  241. #define IGNORE_DEV (1<<1)
  242. static void do_free_by_irq_and_dev(
  243. struct irq_entry *irq_entry,
  244. unsigned int irq,
  245. void *dev,
  246. int flags
  247. )
  248. {
  249. int i;
  250. struct irq_fd *to_free;
  251. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  252. if (irq_entry->irq_array[i] != NULL) {
  253. if (
  254. ((flags & IGNORE_IRQ) ||
  255. (irq_entry->irq_array[i]->irq == irq)) &&
  256. ((flags & IGNORE_DEV) ||
  257. (irq_entry->irq_array[i]->id == dev))
  258. ) {
  259. /* Turn off any IO on this fd - allows us to
  260. * avoid locking the IRQ loop
  261. */
  262. os_del_epoll_fd(irq_entry->fd);
  263. to_free = irq_entry->irq_array[i];
  264. irq_entry->irq_array[i] = NULL;
  265. assign_epoll_events_to_irq(irq_entry);
  266. if (to_free->active)
  267. to_free->purge = true;
  268. else
  269. kfree(to_free);
  270. }
  271. }
  272. }
  273. }
  274. void free_irq_by_fd(int fd)
  275. {
  276. struct irq_entry *to_free;
  277. unsigned long flags;
  278. spin_lock_irqsave(&irq_lock, flags);
  279. to_free = get_irq_entry_by_fd(fd);
  280. if (to_free != NULL) {
  281. do_free_by_irq_and_dev(
  282. to_free,
  283. -1,
  284. NULL,
  285. IGNORE_IRQ | IGNORE_DEV
  286. );
  287. }
  288. garbage_collect_irq_entries();
  289. spin_unlock_irqrestore(&irq_lock, flags);
  290. }
  291. EXPORT_SYMBOL(free_irq_by_fd);
  292. static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  293. {
  294. struct irq_entry *to_free;
  295. unsigned long flags;
  296. spin_lock_irqsave(&irq_lock, flags);
  297. to_free = active_fds;
  298. while (to_free != NULL) {
  299. do_free_by_irq_and_dev(
  300. to_free,
  301. irq,
  302. dev,
  303. 0
  304. );
  305. to_free = to_free->next;
  306. }
  307. garbage_collect_irq_entries();
  308. spin_unlock_irqrestore(&irq_lock, flags);
  309. }
  310. void reactivate_fd(int fd, int irqnum)
  311. {
  312. /** NOP - we do auto-EOI now **/
  313. }
  314. void deactivate_fd(int fd, int irqnum)
  315. {
  316. struct irq_entry *to_free;
  317. unsigned long flags;
  318. os_del_epoll_fd(fd);
  319. spin_lock_irqsave(&irq_lock, flags);
  320. to_free = get_irq_entry_by_fd(fd);
  321. if (to_free != NULL) {
  322. do_free_by_irq_and_dev(
  323. to_free,
  324. irqnum,
  325. NULL,
  326. IGNORE_DEV
  327. );
  328. }
  329. garbage_collect_irq_entries();
  330. spin_unlock_irqrestore(&irq_lock, flags);
  331. ignore_sigio_fd(fd);
  332. }
  333. EXPORT_SYMBOL(deactivate_fd);
  334. /*
  335. * Called just before shutdown in order to provide a clean exec
  336. * environment in case the system is rebooting. No locking because
  337. * that would cause a pointless shutdown hang if something hadn't
  338. * released the lock.
  339. */
  340. int deactivate_all_fds(void)
  341. {
  342. unsigned long flags;
  343. struct irq_entry *to_free;
  344. spin_lock_irqsave(&irq_lock, flags);
  345. /* Stop IO. The IRQ loop has no lock so this is our
  346. * only way of making sure we are safe to dispose
  347. * of all IRQ handlers
  348. */
  349. os_set_ioignore();
  350. to_free = active_fds;
  351. while (to_free != NULL) {
  352. do_free_by_irq_and_dev(
  353. to_free,
  354. -1,
  355. NULL,
  356. IGNORE_IRQ | IGNORE_DEV
  357. );
  358. to_free = to_free->next;
  359. }
  360. garbage_collect_irq_entries();
  361. spin_unlock_irqrestore(&irq_lock, flags);
  362. os_close_epoll_fd();
  363. return 0;
  364. }
  365. /*
  366. * do_IRQ handles all normal device IRQs (the special
  367. * SMP cross-CPU interrupts have their own specific
  368. * handlers).
  369. */
  370. unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
  371. {
  372. struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
  373. irq_enter();
  374. generic_handle_irq(irq);
  375. irq_exit();
  376. set_irq_regs(old_regs);
  377. return 1;
  378. }
  379. void um_free_irq(unsigned int irq, void *dev)
  380. {
  381. free_irq_by_irq_and_dev(irq, dev);
  382. free_irq(irq, dev);
  383. }
  384. EXPORT_SYMBOL(um_free_irq);
  385. int um_request_irq(unsigned int irq, int fd, int type,
  386. irq_handler_t handler,
  387. unsigned long irqflags, const char * devname,
  388. void *dev_id)
  389. {
  390. int err;
  391. if (fd != -1) {
  392. err = activate_fd(irq, fd, type, dev_id);
  393. if (err)
  394. return err;
  395. }
  396. return request_irq(irq, handler, irqflags, devname, dev_id);
  397. }
  398. EXPORT_SYMBOL(um_request_irq);
  399. EXPORT_SYMBOL(reactivate_fd);
  400. /*
  401. * irq_chip must define at least enable/disable and ack when
  402. * the edge handler is used.
  403. */
  404. static void dummy(struct irq_data *d)
  405. {
  406. }
  407. /* This is used for everything else than the timer. */
  408. static struct irq_chip normal_irq_type = {
  409. .name = "SIGIO",
  410. .irq_disable = dummy,
  411. .irq_enable = dummy,
  412. .irq_ack = dummy,
  413. .irq_mask = dummy,
  414. .irq_unmask = dummy,
  415. };
  416. static struct irq_chip SIGVTALRM_irq_type = {
  417. .name = "SIGVTALRM",
  418. .irq_disable = dummy,
  419. .irq_enable = dummy,
  420. .irq_ack = dummy,
  421. .irq_mask = dummy,
  422. .irq_unmask = dummy,
  423. };
  424. void __init init_IRQ(void)
  425. {
  426. int i;
  427. irq_set_chip_and_handler(TIMER_IRQ, &SIGVTALRM_irq_type, handle_edge_irq);
  428. for (i = 1; i < NR_IRQS; i++)
  429. irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
  430. /* Initialize EPOLL Loop */
  431. os_setup_epoll();
  432. }
  433. /*
  434. * IRQ stack entry and exit:
  435. *
  436. * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
  437. * and switch over to the IRQ stack after some preparation. We use
  438. * sigaltstack to receive signals on a separate stack from the start.
  439. * These two functions make sure the rest of the kernel won't be too
  440. * upset by being on a different stack. The IRQ stack has a
  441. * thread_info structure at the bottom so that current et al continue
  442. * to work.
  443. *
  444. * to_irq_stack copies the current task's thread_info to the IRQ stack
  445. * thread_info and sets the tasks's stack to point to the IRQ stack.
  446. *
  447. * from_irq_stack copies the thread_info struct back (flags may have
  448. * been modified) and resets the task's stack pointer.
  449. *
  450. * Tricky bits -
  451. *
  452. * What happens when two signals race each other? UML doesn't block
  453. * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
  454. * could arrive while a previous one is still setting up the
  455. * thread_info.
  456. *
  457. * There are three cases -
  458. * The first interrupt on the stack - sets up the thread_info and
  459. * handles the interrupt
  460. * A nested interrupt interrupting the copying of the thread_info -
  461. * can't handle the interrupt, as the stack is in an unknown state
  462. * A nested interrupt not interrupting the copying of the
  463. * thread_info - doesn't do any setup, just handles the interrupt
  464. *
  465. * The first job is to figure out whether we interrupted stack setup.
  466. * This is done by xchging the signal mask with thread_info->pending.
  467. * If the value that comes back is zero, then there is no setup in
  468. * progress, and the interrupt can be handled. If the value is
  469. * non-zero, then there is stack setup in progress. In order to have
  470. * the interrupt handled, we leave our signal in the mask, and it will
  471. * be handled by the upper handler after it has set up the stack.
  472. *
  473. * Next is to figure out whether we are the outer handler or a nested
  474. * one. As part of setting up the stack, thread_info->real_thread is
  475. * set to non-NULL (and is reset to NULL on exit). This is the
  476. * nesting indicator. If it is non-NULL, then the stack is already
  477. * set up and the handler can run.
  478. */
  479. static unsigned long pending_mask;
  480. unsigned long to_irq_stack(unsigned long *mask_out)
  481. {
  482. struct thread_info *ti;
  483. unsigned long mask, old;
  484. int nested;
  485. mask = xchg(&pending_mask, *mask_out);
  486. if (mask != 0) {
  487. /*
  488. * If any interrupts come in at this point, we want to
  489. * make sure that their bits aren't lost by our
  490. * putting our bit in. So, this loop accumulates bits
  491. * until xchg returns the same value that we put in.
  492. * When that happens, there were no new interrupts,
  493. * and pending_mask contains a bit for each interrupt
  494. * that came in.
  495. */
  496. old = *mask_out;
  497. do {
  498. old |= mask;
  499. mask = xchg(&pending_mask, old);
  500. } while (mask != old);
  501. return 1;
  502. }
  503. ti = current_thread_info();
  504. nested = (ti->real_thread != NULL);
  505. if (!nested) {
  506. struct task_struct *task;
  507. struct thread_info *tti;
  508. task = cpu_tasks[ti->cpu].task;
  509. tti = task_thread_info(task);
  510. *ti = *tti;
  511. ti->real_thread = tti;
  512. task->stack = ti;
  513. }
  514. mask = xchg(&pending_mask, 0);
  515. *mask_out |= mask | nested;
  516. return 0;
  517. }
  518. unsigned long from_irq_stack(int nested)
  519. {
  520. struct thread_info *ti, *to;
  521. unsigned long mask;
  522. ti = current_thread_info();
  523. pending_mask = 1;
  524. to = ti->real_thread;
  525. current->stack = to;
  526. ti->real_thread = NULL;
  527. *to = *ti;
  528. mask = xchg(&pending_mask, 0);
  529. return mask & ~1;
  530. }