irq.c 14 KB

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