ehci-timer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2012 by Alan Stern
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. /* This file is part of ehci-hcd.c */
  15. /*-------------------------------------------------------------------------*/
  16. /* Set a bit in the USBCMD register */
  17. static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit)
  18. {
  19. ehci->command |= bit;
  20. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  21. /* unblock posted write */
  22. ehci_readl(ehci, &ehci->regs->command);
  23. }
  24. /* Clear a bit in the USBCMD register */
  25. static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit)
  26. {
  27. ehci->command &= ~bit;
  28. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  29. /* unblock posted write */
  30. ehci_readl(ehci, &ehci->regs->command);
  31. }
  32. /*-------------------------------------------------------------------------*/
  33. /*
  34. * EHCI timer support... Now using hrtimers.
  35. *
  36. * Lots of different events are triggered from ehci->hrtimer. Whenever
  37. * the timer routine runs, it checks each possible event; events that are
  38. * currently enabled and whose expiration time has passed get handled.
  39. * The set of enabled events is stored as a collection of bitflags in
  40. * ehci->enabled_hrtimer_events, and they are numbered in order of
  41. * increasing delay values (ranging between 1 ms and 100 ms).
  42. *
  43. * Rather than implementing a sorted list or tree of all pending events,
  44. * we keep track only of the lowest-numbered pending event, in
  45. * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its
  46. * expiration time is set to the timeout value for this event.
  47. *
  48. * As a result, events might not get handled right away; the actual delay
  49. * could be anywhere up to twice the requested delay. This doesn't
  50. * matter, because none of the events are especially time-critical. The
  51. * ones that matter most all have a delay of 1 ms, so they will be
  52. * handled after 2 ms at most, which is okay. In addition to this, we
  53. * allow for an expiration range of 1 ms.
  54. */
  55. /*
  56. * Delay lengths for the hrtimer event types.
  57. * Keep this list sorted by delay length, in the same order as
  58. * the event types indexed by enum ehci_hrtimer_event in ehci.h.
  59. */
  60. static unsigned event_delays_ns[] = {
  61. 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_ASS */
  62. 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_PSS */
  63. 1125 * NSEC_PER_USEC, /* EHCI_HRTIMER_UNLINK_INTR */
  64. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  65. 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
  66. };
  67. /* Enable a pending hrtimer event */
  68. static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
  69. bool resched)
  70. {
  71. ktime_t *timeout = &ehci->hr_timeouts[event];
  72. if (resched)
  73. *timeout = ktime_add(ktime_get(),
  74. ktime_set(0, event_delays_ns[event]));
  75. ehci->enabled_hrtimer_events |= (1 << event);
  76. /* Track only the lowest-numbered pending event */
  77. if (event < ehci->next_hrtimer_event) {
  78. ehci->next_hrtimer_event = event;
  79. hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
  80. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  81. }
  82. }
  83. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  84. static void ehci_poll_ASS(struct ehci_hcd *ehci)
  85. {
  86. unsigned actual, want;
  87. /* Don't enable anything if the controller isn't running (e.g., died) */
  88. if (ehci->rh_state != EHCI_RH_RUNNING)
  89. return;
  90. want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
  91. actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
  92. if (want != actual) {
  93. /* Poll again later, but give up after about 20 ms */
  94. if (ehci->ASS_poll_count++ < 20) {
  95. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
  96. return;
  97. }
  98. ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n");
  99. }
  100. ehci->ASS_poll_count = 0;
  101. /* The status is up-to-date; restart or stop the schedule as needed */
  102. if (want == 0) { /* Stopped */
  103. if (ehci->async_count > 0)
  104. ehci_set_command_bit(ehci, CMD_ASE);
  105. } else { /* Running */
  106. if (ehci->async_count == 0) {
  107. /* Turn off the schedule after a while */
  108. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
  109. true);
  110. }
  111. }
  112. }
  113. /* Turn off the async schedule after a brief delay */
  114. static void ehci_disable_ASE(struct ehci_hcd *ehci)
  115. {
  116. ehci_clear_command_bit(ehci, CMD_ASE);
  117. }
  118. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  119. static void ehci_poll_PSS(struct ehci_hcd *ehci)
  120. {
  121. unsigned actual, want;
  122. /* Don't do anything if the controller isn't running (e.g., died) */
  123. if (ehci->rh_state != EHCI_RH_RUNNING)
  124. return;
  125. want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
  126. actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
  127. if (want != actual) {
  128. /* Poll again later, but give up after about 20 ms */
  129. if (ehci->PSS_poll_count++ < 20) {
  130. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  131. return;
  132. }
  133. ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n");
  134. }
  135. ehci->PSS_poll_count = 0;
  136. /* The status is up-to-date; restart or stop the schedule as needed */
  137. if (want == 0) { /* Stopped */
  138. free_cached_lists(ehci);
  139. if (ehci->periodic_count > 0) {
  140. /* make sure ehci_work scans these */
  141. ehci->next_uframe = ehci_read_frame_index(ehci)
  142. & ((ehci->periodic_size << 3) - 1);
  143. ehci_set_command_bit(ehci, CMD_PSE);
  144. }
  145. } else { /* Running */
  146. if (ehci->periodic_count == 0) {
  147. /* Turn off the schedule after a while */
  148. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
  149. true);
  150. }
  151. }
  152. }
  153. /* Turn off the periodic schedule after a brief delay */
  154. static void ehci_disable_PSE(struct ehci_hcd *ehci)
  155. {
  156. ehci_clear_command_bit(ehci, CMD_PSE);
  157. /* Poll to see when it actually stops */
  158. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  159. }
  160. /* Handle unlinked interrupt QHs once they are gone from the hardware */
  161. static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
  162. {
  163. bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
  164. /*
  165. * Process all the QHs on the intr_unlink list that were added
  166. * before the current unlink cycle began. The list is in
  167. * temporal order, so stop when we reach the first entry in the
  168. * current cycle. But if the root hub isn't running then
  169. * process all the QHs on the list.
  170. */
  171. ehci->intr_unlinking = true;
  172. while (ehci->intr_unlink) {
  173. struct ehci_qh *qh = ehci->intr_unlink;
  174. if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
  175. break;
  176. ehci->intr_unlink = qh->unlink_next;
  177. qh->unlink_next = NULL;
  178. end_unlink_intr(ehci, qh);
  179. }
  180. /* Handle remaining entries later */
  181. if (ehci->intr_unlink) {
  182. ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
  183. ++ehci->intr_unlink_cycle;
  184. }
  185. ehci->intr_unlinking = false;
  186. }
  187. /*
  188. * Handler functions for the hrtimer event types.
  189. * Keep this array in the same order as the event types indexed by
  190. * enum ehci_hrtimer_event in ehci.h.
  191. */
  192. static void (*event_handlers[])(struct ehci_hcd *) = {
  193. ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
  194. ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
  195. ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */
  196. ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  197. ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
  198. };
  199. static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
  200. {
  201. struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
  202. ktime_t now;
  203. unsigned long events;
  204. unsigned long flags;
  205. unsigned e;
  206. spin_lock_irqsave(&ehci->lock, flags);
  207. events = ehci->enabled_hrtimer_events;
  208. ehci->enabled_hrtimer_events = 0;
  209. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  210. /*
  211. * Check each pending event. If its time has expired, handle
  212. * the event; otherwise re-enable it.
  213. */
  214. now = ktime_get();
  215. for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
  216. if (now.tv64 >= ehci->hr_timeouts[e].tv64)
  217. event_handlers[e](ehci);
  218. else
  219. ehci_enable_event(ehci, e, false);
  220. }
  221. spin_unlock_irqrestore(&ehci->lock, flags);
  222. return HRTIMER_NORESTART;
  223. }