ehci-timer.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_DEAD */
  64. 1125 * NSEC_PER_USEC, /* EHCI_HRTIMER_UNLINK_INTR */
  65. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  66. 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
  67. };
  68. /* Enable a pending hrtimer event */
  69. static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
  70. bool resched)
  71. {
  72. ktime_t *timeout = &ehci->hr_timeouts[event];
  73. if (resched)
  74. *timeout = ktime_add(ktime_get(),
  75. ktime_set(0, event_delays_ns[event]));
  76. ehci->enabled_hrtimer_events |= (1 << event);
  77. /* Track only the lowest-numbered pending event */
  78. if (event < ehci->next_hrtimer_event) {
  79. ehci->next_hrtimer_event = event;
  80. hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
  81. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  82. }
  83. }
  84. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  85. static void ehci_poll_ASS(struct ehci_hcd *ehci)
  86. {
  87. unsigned actual, want;
  88. /* Don't enable anything if the controller isn't running (e.g., died) */
  89. if (ehci->rh_state != EHCI_RH_RUNNING)
  90. return;
  91. want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
  92. actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
  93. if (want != actual) {
  94. /* Poll again later, but give up after about 20 ms */
  95. if (ehci->ASS_poll_count++ < 20) {
  96. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
  97. return;
  98. }
  99. ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n");
  100. }
  101. ehci->ASS_poll_count = 0;
  102. /* The status is up-to-date; restart or stop the schedule as needed */
  103. if (want == 0) { /* Stopped */
  104. if (ehci->async_count > 0)
  105. ehci_set_command_bit(ehci, CMD_ASE);
  106. } else { /* Running */
  107. if (ehci->async_count == 0) {
  108. /* Turn off the schedule after a while */
  109. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
  110. true);
  111. }
  112. }
  113. }
  114. /* Turn off the async schedule after a brief delay */
  115. static void ehci_disable_ASE(struct ehci_hcd *ehci)
  116. {
  117. ehci_clear_command_bit(ehci, CMD_ASE);
  118. }
  119. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  120. static void ehci_poll_PSS(struct ehci_hcd *ehci)
  121. {
  122. unsigned actual, want;
  123. /* Don't do anything if the controller isn't running (e.g., died) */
  124. if (ehci->rh_state != EHCI_RH_RUNNING)
  125. return;
  126. want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
  127. actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
  128. if (want != actual) {
  129. /* Poll again later, but give up after about 20 ms */
  130. if (ehci->PSS_poll_count++ < 20) {
  131. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  132. return;
  133. }
  134. ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n");
  135. }
  136. ehci->PSS_poll_count = 0;
  137. /* The status is up-to-date; restart or stop the schedule as needed */
  138. if (want == 0) { /* Stopped */
  139. free_cached_lists(ehci);
  140. if (ehci->periodic_count > 0) {
  141. /* make sure ehci_work scans these */
  142. ehci->next_uframe = ehci_read_frame_index(ehci)
  143. & ((ehci->periodic_size << 3) - 1);
  144. ehci_set_command_bit(ehci, CMD_PSE);
  145. }
  146. } else { /* Running */
  147. if (ehci->periodic_count == 0) {
  148. /* Turn off the schedule after a while */
  149. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
  150. true);
  151. }
  152. }
  153. }
  154. /* Turn off the periodic schedule after a brief delay */
  155. static void ehci_disable_PSE(struct ehci_hcd *ehci)
  156. {
  157. ehci_clear_command_bit(ehci, CMD_PSE);
  158. /* Poll to see when it actually stops */
  159. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  160. }
  161. /* Poll the STS_HALT status bit; see when a dead controller stops */
  162. static void ehci_handle_controller_death(struct ehci_hcd *ehci)
  163. {
  164. if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) {
  165. /* Give up after a few milliseconds */
  166. if (ehci->died_poll_count++ < 5) {
  167. /* Try again later */
  168. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_DEAD, true);
  169. return;
  170. }
  171. ehci_warn(ehci, "Waited too long for the controller to stop, giving up\n");
  172. }
  173. /* Clean up the mess */
  174. ehci->rh_state = EHCI_RH_HALTED;
  175. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  176. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  177. ehci_work(ehci);
  178. /* Not in process context, so don't try to reset the controller */
  179. }
  180. /* Handle unlinked interrupt QHs once they are gone from the hardware */
  181. static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
  182. {
  183. bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
  184. /*
  185. * Process all the QHs on the intr_unlink list that were added
  186. * before the current unlink cycle began. The list is in
  187. * temporal order, so stop when we reach the first entry in the
  188. * current cycle. But if the root hub isn't running then
  189. * process all the QHs on the list.
  190. */
  191. ehci->intr_unlinking = true;
  192. while (ehci->intr_unlink) {
  193. struct ehci_qh *qh = ehci->intr_unlink;
  194. if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
  195. break;
  196. ehci->intr_unlink = qh->unlink_next;
  197. qh->unlink_next = NULL;
  198. end_unlink_intr(ehci, qh);
  199. }
  200. /* Handle remaining entries later */
  201. if (ehci->intr_unlink) {
  202. ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
  203. ++ehci->intr_unlink_cycle;
  204. }
  205. ehci->intr_unlinking = false;
  206. }
  207. /*
  208. * Handler functions for the hrtimer event types.
  209. * Keep this array in the same order as the event types indexed by
  210. * enum ehci_hrtimer_event in ehci.h.
  211. */
  212. static void (*event_handlers[])(struct ehci_hcd *) = {
  213. ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
  214. ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
  215. ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */
  216. ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */
  217. ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  218. ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
  219. };
  220. static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
  221. {
  222. struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
  223. ktime_t now;
  224. unsigned long events;
  225. unsigned long flags;
  226. unsigned e;
  227. spin_lock_irqsave(&ehci->lock, flags);
  228. events = ehci->enabled_hrtimer_events;
  229. ehci->enabled_hrtimer_events = 0;
  230. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  231. /*
  232. * Check each pending event. If its time has expired, handle
  233. * the event; otherwise re-enable it.
  234. */
  235. now = ktime_get();
  236. for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
  237. if (now.tv64 >= ehci->hr_timeouts[e].tv64)
  238. event_handlers[e](ehci);
  239. else
  240. ehci_enable_event(ehci, e, false);
  241. }
  242. spin_unlock_irqrestore(&ehci->lock, flags);
  243. return HRTIMER_NORESTART;
  244. }