ehci-timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. 2 * NSEC_PER_MSEC, /* EHCI_HRTIMER_FREE_ITDS */
  66. 6 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ASYNC_UNLINKS */
  67. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IAA_WATCHDOG */
  68. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  69. 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
  70. 100 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IO_WATCHDOG */
  71. };
  72. /* Enable a pending hrtimer event */
  73. static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
  74. bool resched)
  75. {
  76. ktime_t *timeout = &ehci->hr_timeouts[event];
  77. if (resched)
  78. *timeout = ktime_add(ktime_get(),
  79. ktime_set(0, event_delays_ns[event]));
  80. ehci->enabled_hrtimer_events |= (1 << event);
  81. /* Track only the lowest-numbered pending event */
  82. if (event < ehci->next_hrtimer_event) {
  83. ehci->next_hrtimer_event = event;
  84. hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
  85. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  86. }
  87. }
  88. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  89. static void ehci_poll_ASS(struct ehci_hcd *ehci)
  90. {
  91. unsigned actual, want;
  92. /* Don't enable anything if the controller isn't running (e.g., died) */
  93. if (ehci->rh_state != EHCI_RH_RUNNING)
  94. return;
  95. want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
  96. actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
  97. if (want != actual) {
  98. /* Poll again later, but give up after about 20 ms */
  99. if (ehci->ASS_poll_count++ < 20) {
  100. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
  101. return;
  102. }
  103. ehci_dbg(ehci, "Waited too long for the async schedule status (%x/%x), giving up\n",
  104. want, actual);
  105. }
  106. ehci->ASS_poll_count = 0;
  107. /* The status is up-to-date; restart or stop the schedule as needed */
  108. if (want == 0) { /* Stopped */
  109. if (ehci->async_count > 0)
  110. ehci_set_command_bit(ehci, CMD_ASE);
  111. } else { /* Running */
  112. if (ehci->async_count == 0) {
  113. /* Turn off the schedule after a while */
  114. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
  115. true);
  116. }
  117. }
  118. }
  119. /* Turn off the async schedule after a brief delay */
  120. static void ehci_disable_ASE(struct ehci_hcd *ehci)
  121. {
  122. ehci_clear_command_bit(ehci, CMD_ASE);
  123. }
  124. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  125. static void ehci_poll_PSS(struct ehci_hcd *ehci)
  126. {
  127. unsigned actual, want;
  128. /* Don't do anything if the controller isn't running (e.g., died) */
  129. if (ehci->rh_state != EHCI_RH_RUNNING)
  130. return;
  131. want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
  132. actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
  133. if (want != actual) {
  134. /* Poll again later, but give up after about 20 ms */
  135. if (ehci->PSS_poll_count++ < 20) {
  136. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  137. return;
  138. }
  139. ehci_dbg(ehci, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
  140. want, actual);
  141. }
  142. ehci->PSS_poll_count = 0;
  143. /* The status is up-to-date; restart or stop the schedule as needed */
  144. if (want == 0) { /* Stopped */
  145. if (ehci->periodic_count > 0)
  146. ehci_set_command_bit(ehci, CMD_PSE);
  147. } else { /* Running */
  148. if (ehci->periodic_count == 0) {
  149. /* Turn off the schedule after a while */
  150. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
  151. true);
  152. }
  153. }
  154. }
  155. /* Turn off the periodic schedule after a brief delay */
  156. static void ehci_disable_PSE(struct ehci_hcd *ehci)
  157. {
  158. ehci_clear_command_bit(ehci, CMD_PSE);
  159. }
  160. /* Poll the STS_HALT status bit; see when a dead controller stops */
  161. static void ehci_handle_controller_death(struct ehci_hcd *ehci)
  162. {
  163. if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) {
  164. /* Give up after a few milliseconds */
  165. if (ehci->died_poll_count++ < 5) {
  166. /* Try again later */
  167. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_DEAD, true);
  168. return;
  169. }
  170. ehci_warn(ehci, "Waited too long for the controller to stop, giving up\n");
  171. }
  172. /* Clean up the mess */
  173. ehci->rh_state = EHCI_RH_HALTED;
  174. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  175. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  176. ehci_work(ehci);
  177. end_unlink_async(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. /* Start another free-iTDs/siTDs cycle */
  208. static void start_free_itds(struct ehci_hcd *ehci)
  209. {
  210. if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) {
  211. ehci->last_itd_to_free = list_entry(
  212. ehci->cached_itd_list.prev,
  213. struct ehci_itd, itd_list);
  214. ehci->last_sitd_to_free = list_entry(
  215. ehci->cached_sitd_list.prev,
  216. struct ehci_sitd, sitd_list);
  217. ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true);
  218. }
  219. }
  220. /* Wait for controller to stop using old iTDs and siTDs */
  221. static void end_free_itds(struct ehci_hcd *ehci)
  222. {
  223. struct ehci_itd *itd, *n;
  224. struct ehci_sitd *sitd, *sn;
  225. if (ehci->rh_state < EHCI_RH_RUNNING) {
  226. ehci->last_itd_to_free = NULL;
  227. ehci->last_sitd_to_free = NULL;
  228. }
  229. list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
  230. list_del(&itd->itd_list);
  231. dma_pool_free(ehci->itd_pool, itd, itd->itd_dma);
  232. if (itd == ehci->last_itd_to_free)
  233. break;
  234. }
  235. list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
  236. list_del(&sitd->sitd_list);
  237. dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma);
  238. if (sitd == ehci->last_sitd_to_free)
  239. break;
  240. }
  241. if (!list_empty(&ehci->cached_itd_list) ||
  242. !list_empty(&ehci->cached_sitd_list))
  243. start_free_itds(ehci);
  244. }
  245. /* Handle lost (or very late) IAA interrupts */
  246. static void ehci_iaa_watchdog(struct ehci_hcd *ehci)
  247. {
  248. if (ehci->rh_state != EHCI_RH_RUNNING)
  249. return;
  250. /*
  251. * Lost IAA irqs wedge things badly; seen first with a vt8235.
  252. * So we need this watchdog, but must protect it against both
  253. * (a) SMP races against real IAA firing and retriggering, and
  254. * (b) clean HC shutdown, when IAA watchdog was pending.
  255. */
  256. if (1) {
  257. u32 cmd, status;
  258. /* If we get here, IAA is *REALLY* late. It's barely
  259. * conceivable that the system is so busy that CMD_IAAD
  260. * is still legitimately set, so let's be sure it's
  261. * clear before we read STS_IAA. (The HC should clear
  262. * CMD_IAAD when it sets STS_IAA.)
  263. */
  264. cmd = ehci_readl(ehci, &ehci->regs->command);
  265. /*
  266. * If IAA is set here it either legitimately triggered
  267. * after the watchdog timer expired (_way_ late, so we'll
  268. * still count it as lost) ... or a silicon erratum:
  269. * - VIA seems to set IAA without triggering the IRQ;
  270. * - IAAD potentially cleared without setting IAA.
  271. */
  272. status = ehci_readl(ehci, &ehci->regs->status);
  273. if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  274. COUNT(ehci->stats.lost_iaa);
  275. ehci_writel(ehci, STS_IAA, &ehci->regs->status);
  276. }
  277. ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n",
  278. status, cmd);
  279. end_unlink_async(ehci);
  280. }
  281. }
  282. /* Enable the I/O watchdog, if appropriate */
  283. static void turn_on_io_watchdog(struct ehci_hcd *ehci)
  284. {
  285. /* Not needed if the controller isn't running or it's already enabled */
  286. if (ehci->rh_state != EHCI_RH_RUNNING ||
  287. (ehci->enabled_hrtimer_events &
  288. BIT(EHCI_HRTIMER_IO_WATCHDOG)))
  289. return;
  290. /*
  291. * Isochronous transfers always need the watchdog.
  292. * For other sorts we use it only if the flag is set.
  293. */
  294. if (ehci->isoc_count > 0 || (ehci->need_io_watchdog &&
  295. ehci->async_count + ehci->intr_count > 0))
  296. ehci_enable_event(ehci, EHCI_HRTIMER_IO_WATCHDOG, true);
  297. }
  298. /*
  299. * Handler functions for the hrtimer event types.
  300. * Keep this array in the same order as the event types indexed by
  301. * enum ehci_hrtimer_event in ehci.h.
  302. */
  303. static void (*event_handlers[])(struct ehci_hcd *) = {
  304. ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
  305. ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
  306. ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */
  307. ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */
  308. end_free_itds, /* EHCI_HRTIMER_FREE_ITDS */
  309. unlink_empty_async, /* EHCI_HRTIMER_ASYNC_UNLINKS */
  310. ehci_iaa_watchdog, /* EHCI_HRTIMER_IAA_WATCHDOG */
  311. ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  312. ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
  313. ehci_work, /* EHCI_HRTIMER_IO_WATCHDOG */
  314. };
  315. static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
  316. {
  317. struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
  318. ktime_t now;
  319. unsigned long events;
  320. unsigned long flags;
  321. unsigned e;
  322. spin_lock_irqsave(&ehci->lock, flags);
  323. events = ehci->enabled_hrtimer_events;
  324. ehci->enabled_hrtimer_events = 0;
  325. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  326. /*
  327. * Check each pending event. If its time has expired, handle
  328. * the event; otherwise re-enable it.
  329. */
  330. now = ktime_get();
  331. for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
  332. if (now.tv64 >= ehci->hr_timeouts[e].tv64)
  333. event_handlers[e](ehci);
  334. else
  335. ehci_enable_event(ehci, e, false);
  336. }
  337. spin_unlock_irqrestore(&ehci->lock, flags);
  338. return HRTIMER_NORESTART;
  339. }