irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. if (to_free != NULL)
  216. kfree(to_free);
  217. }
  218. }
  219. /*
  220. * Walk the IRQ list and get the descriptor for our FD
  221. */
  222. static struct irq_entry *get_irq_entry_by_fd(int fd)
  223. {
  224. struct irq_entry *walk = active_fds;
  225. while (walk != NULL) {
  226. if (walk->fd == fd)
  227. return walk;
  228. walk = walk->next;
  229. }
  230. return NULL;
  231. }
  232. /*
  233. * Walk the IRQ list and dispose of an entry for a specific
  234. * device, fd and number. Note - if sharing an IRQ for read
  235. * and writefor the same FD it will be disposed in either case.
  236. * If this behaviour is undesirable use different IRQ ids.
  237. */
  238. #define IGNORE_IRQ 1
  239. #define IGNORE_DEV (1<<1)
  240. static void do_free_by_irq_and_dev(
  241. struct irq_entry *irq_entry,
  242. unsigned int irq,
  243. void *dev,
  244. int flags
  245. )
  246. {
  247. int i;
  248. struct irq_fd *to_free;
  249. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  250. if (irq_entry->irq_array[i] != NULL) {
  251. if (
  252. ((flags & IGNORE_IRQ) ||
  253. (irq_entry->irq_array[i]->irq == irq)) &&
  254. ((flags & IGNORE_DEV) ||
  255. (irq_entry->irq_array[i]->id == dev))
  256. ) {
  257. /* Turn off any IO on this fd - allows us to
  258. * avoid locking the IRQ loop
  259. */
  260. os_del_epoll_fd(irq_entry->fd);
  261. to_free = irq_entry->irq_array[i];
  262. irq_entry->irq_array[i] = NULL;
  263. assign_epoll_events_to_irq(irq_entry);
  264. if (to_free->active)
  265. to_free->purge = true;
  266. else
  267. kfree(to_free);
  268. }
  269. }
  270. }
  271. }
  272. void free_irq_by_fd(int fd)
  273. {
  274. struct irq_entry *to_free;
  275. unsigned long flags;
  276. spin_lock_irqsave(&irq_lock, flags);
  277. to_free = get_irq_entry_by_fd(fd);
  278. if (to_free != NULL) {
  279. do_free_by_irq_and_dev(
  280. to_free,
  281. -1,
  282. NULL,
  283. IGNORE_IRQ | IGNORE_DEV
  284. );
  285. }
  286. garbage_collect_irq_entries();
  287. spin_unlock_irqrestore(&irq_lock, flags);
  288. }
  289. EXPORT_SYMBOL(free_irq_by_fd);
  290. static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  291. {
  292. struct irq_entry *to_free;
  293. unsigned long flags;
  294. spin_lock_irqsave(&irq_lock, flags);
  295. to_free = active_fds;
  296. while (to_free != NULL) {
  297. do_free_by_irq_and_dev(
  298. to_free,
  299. irq,
  300. dev,
  301. 0
  302. );
  303. to_free = to_free->next;
  304. }
  305. garbage_collect_irq_entries();
  306. spin_unlock_irqrestore(&irq_lock, flags);
  307. }
  308. void reactivate_fd(int fd, int irqnum)
  309. {
  310. /** NOP - we do auto-EOI now **/
  311. }
  312. void deactivate_fd(int fd, int irqnum)
  313. {
  314. struct irq_entry *to_free;
  315. unsigned long flags;
  316. os_del_epoll_fd(fd);
  317. spin_lock_irqsave(&irq_lock, flags);
  318. to_free = get_irq_entry_by_fd(fd);
  319. if (to_free != NULL) {
  320. do_free_by_irq_and_dev(
  321. to_free,
  322. irqnum,
  323. NULL,
  324. IGNORE_DEV
  325. );
  326. }
  327. garbage_collect_irq_entries();
  328. spin_unlock_irqrestore(&irq_lock, flags);
  329. ignore_sigio_fd(fd);
  330. }
  331. EXPORT_SYMBOL(deactivate_fd);
  332. /*
  333. * Called just before shutdown in order to provide a clean exec
  334. * environment in case the system is rebooting. No locking because
  335. * that would cause a pointless shutdown hang if something hadn't
  336. * released the lock.
  337. */
  338. int deactivate_all_fds(void)
  339. {
  340. unsigned long flags;
  341. struct irq_entry *to_free;
  342. spin_lock_irqsave(&irq_lock, flags);
  343. /* Stop IO. The IRQ loop has no lock so this is our
  344. * only way of making sure we are safe to dispose
  345. * of all IRQ handlers
  346. */
  347. os_set_ioignore();
  348. to_free = active_fds;
  349. while (to_free != NULL) {
  350. do_free_by_irq_and_dev(
  351. to_free,
  352. -1,
  353. NULL,
  354. IGNORE_IRQ | IGNORE_DEV
  355. );
  356. to_free = to_free->next;
  357. }
  358. garbage_collect_irq_entries();
  359. spin_unlock_irqrestore(&irq_lock, flags);
  360. os_close_epoll_fd();
  361. return 0;
  362. }
  363. /*
  364. * do_IRQ handles all normal device IRQs (the special
  365. * SMP cross-CPU interrupts have their own specific
  366. * handlers).
  367. */
  368. unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
  369. {
  370. struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
  371. irq_enter();
  372. generic_handle_irq(irq);
  373. irq_exit();
  374. set_irq_regs(old_regs);
  375. return 1;
  376. }
  377. void um_free_irq(unsigned int irq, void *dev)
  378. {
  379. free_irq_by_irq_and_dev(irq, dev);
  380. free_irq(irq, dev);
  381. }
  382. EXPORT_SYMBOL(um_free_irq);
  383. int um_request_irq(unsigned int irq, int fd, int type,
  384. irq_handler_t handler,
  385. unsigned long irqflags, const char * devname,
  386. void *dev_id)
  387. {
  388. int err;
  389. if (fd != -1) {
  390. err = activate_fd(irq, fd, type, dev_id);
  391. if (err)
  392. return err;
  393. }
  394. return request_irq(irq, handler, irqflags, devname, dev_id);
  395. }
  396. EXPORT_SYMBOL(um_request_irq);
  397. EXPORT_SYMBOL(reactivate_fd);
  398. /*
  399. * irq_chip must define at least enable/disable and ack when
  400. * the edge handler is used.
  401. */
  402. static void dummy(struct irq_data *d)
  403. {
  404. }
  405. /* This is used for everything else than the timer. */
  406. static struct irq_chip normal_irq_type = {
  407. .name = "SIGIO",
  408. .irq_disable = dummy,
  409. .irq_enable = dummy,
  410. .irq_ack = dummy,
  411. .irq_mask = dummy,
  412. .irq_unmask = dummy,
  413. };
  414. static struct irq_chip SIGVTALRM_irq_type = {
  415. .name = "SIGVTALRM",
  416. .irq_disable = dummy,
  417. .irq_enable = dummy,
  418. .irq_ack = dummy,
  419. .irq_mask = dummy,
  420. .irq_unmask = dummy,
  421. };
  422. void __init init_IRQ(void)
  423. {
  424. int i;
  425. irq_set_chip_and_handler(TIMER_IRQ, &SIGVTALRM_irq_type, handle_edge_irq);
  426. for (i = 1; i < NR_IRQS; i++)
  427. irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
  428. /* Initialize EPOLL Loop */
  429. os_setup_epoll();
  430. }
  431. /*
  432. * IRQ stack entry and exit:
  433. *
  434. * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
  435. * and switch over to the IRQ stack after some preparation. We use
  436. * sigaltstack to receive signals on a separate stack from the start.
  437. * These two functions make sure the rest of the kernel won't be too
  438. * upset by being on a different stack. The IRQ stack has a
  439. * thread_info structure at the bottom so that current et al continue
  440. * to work.
  441. *
  442. * to_irq_stack copies the current task's thread_info to the IRQ stack
  443. * thread_info and sets the tasks's stack to point to the IRQ stack.
  444. *
  445. * from_irq_stack copies the thread_info struct back (flags may have
  446. * been modified) and resets the task's stack pointer.
  447. *
  448. * Tricky bits -
  449. *
  450. * What happens when two signals race each other? UML doesn't block
  451. * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
  452. * could arrive while a previous one is still setting up the
  453. * thread_info.
  454. *
  455. * There are three cases -
  456. * The first interrupt on the stack - sets up the thread_info and
  457. * handles the interrupt
  458. * A nested interrupt interrupting the copying of the thread_info -
  459. * can't handle the interrupt, as the stack is in an unknown state
  460. * A nested interrupt not interrupting the copying of the
  461. * thread_info - doesn't do any setup, just handles the interrupt
  462. *
  463. * The first job is to figure out whether we interrupted stack setup.
  464. * This is done by xchging the signal mask with thread_info->pending.
  465. * If the value that comes back is zero, then there is no setup in
  466. * progress, and the interrupt can be handled. If the value is
  467. * non-zero, then there is stack setup in progress. In order to have
  468. * the interrupt handled, we leave our signal in the mask, and it will
  469. * be handled by the upper handler after it has set up the stack.
  470. *
  471. * Next is to figure out whether we are the outer handler or a nested
  472. * one. As part of setting up the stack, thread_info->real_thread is
  473. * set to non-NULL (and is reset to NULL on exit). This is the
  474. * nesting indicator. If it is non-NULL, then the stack is already
  475. * set up and the handler can run.
  476. */
  477. static unsigned long pending_mask;
  478. unsigned long to_irq_stack(unsigned long *mask_out)
  479. {
  480. struct thread_info *ti;
  481. unsigned long mask, old;
  482. int nested;
  483. mask = xchg(&pending_mask, *mask_out);
  484. if (mask != 0) {
  485. /*
  486. * If any interrupts come in at this point, we want to
  487. * make sure that their bits aren't lost by our
  488. * putting our bit in. So, this loop accumulates bits
  489. * until xchg returns the same value that we put in.
  490. * When that happens, there were no new interrupts,
  491. * and pending_mask contains a bit for each interrupt
  492. * that came in.
  493. */
  494. old = *mask_out;
  495. do {
  496. old |= mask;
  497. mask = xchg(&pending_mask, old);
  498. } while (mask != old);
  499. return 1;
  500. }
  501. ti = current_thread_info();
  502. nested = (ti->real_thread != NULL);
  503. if (!nested) {
  504. struct task_struct *task;
  505. struct thread_info *tti;
  506. task = cpu_tasks[ti->cpu].task;
  507. tti = task_thread_info(task);
  508. *ti = *tti;
  509. ti->real_thread = tti;
  510. task->stack = ti;
  511. }
  512. mask = xchg(&pending_mask, 0);
  513. *mask_out |= mask | nested;
  514. return 0;
  515. }
  516. unsigned long from_irq_stack(int nested)
  517. {
  518. struct thread_info *ti, *to;
  519. unsigned long mask;
  520. ti = current_thread_info();
  521. pending_mask = 1;
  522. to = ti->real_thread;
  523. current->stack = to;
  524. ti->real_thread = NULL;
  525. *to = *ti;
  526. mask = xchg(&pending_mask, 0);
  527. return mask & ~1;
  528. }