ehci-timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. 5 * NSEC_PER_MSEC, /* EHCI_HRTIMER_START_UNLINK_INTR */
  67. 6 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ASYNC_UNLINKS */
  68. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IAA_WATCHDOG */
  69. 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  70. 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
  71. 100 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IO_WATCHDOG */
  72. };
  73. /* Enable a pending hrtimer event */
  74. static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
  75. bool resched)
  76. {
  77. ktime_t *timeout = &ehci->hr_timeouts[event];
  78. if (resched)
  79. *timeout = ktime_add(ktime_get(),
  80. ktime_set(0, event_delays_ns[event]));
  81. ehci->enabled_hrtimer_events |= (1 << event);
  82. /* Track only the lowest-numbered pending event */
  83. if (event < ehci->next_hrtimer_event) {
  84. ehci->next_hrtimer_event = event;
  85. hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
  86. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  87. }
  88. }
  89. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  90. static void ehci_poll_ASS(struct ehci_hcd *ehci)
  91. {
  92. unsigned actual, want;
  93. /* Don't enable anything if the controller isn't running (e.g., died) */
  94. if (ehci->rh_state != EHCI_RH_RUNNING)
  95. return;
  96. want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
  97. actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
  98. if (want != actual) {
  99. /* Poll again later, but give up after about 2-4 ms */
  100. if (ehci->ASS_poll_count++ < 2) {
  101. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
  102. return;
  103. }
  104. ehci_dbg(ehci, "Waited too long for the async schedule status (%x/%x), giving up\n",
  105. want, actual);
  106. }
  107. ehci->ASS_poll_count = 0;
  108. /* The status is up-to-date; restart or stop the schedule as needed */
  109. if (want == 0) { /* Stopped */
  110. if (ehci->async_count > 0)
  111. ehci_set_command_bit(ehci, CMD_ASE);
  112. } else { /* Running */
  113. if (ehci->async_count == 0) {
  114. /* Turn off the schedule after a while */
  115. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
  116. true);
  117. }
  118. }
  119. }
  120. /* Turn off the async schedule after a brief delay */
  121. static void ehci_disable_ASE(struct ehci_hcd *ehci)
  122. {
  123. ehci_clear_command_bit(ehci, CMD_ASE);
  124. }
  125. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  126. static void ehci_poll_PSS(struct ehci_hcd *ehci)
  127. {
  128. unsigned actual, want;
  129. /* Don't do anything if the controller isn't running (e.g., died) */
  130. if (ehci->rh_state != EHCI_RH_RUNNING)
  131. return;
  132. want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
  133. actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
  134. if (want != actual) {
  135. /* Poll again later, but give up after about 2-4 ms */
  136. if (ehci->PSS_poll_count++ < 2) {
  137. ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
  138. return;
  139. }
  140. ehci_dbg(ehci, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
  141. want, actual);
  142. }
  143. ehci->PSS_poll_count = 0;
  144. /* The status is up-to-date; restart or stop the schedule as needed */
  145. if (want == 0) { /* Stopped */
  146. if (ehci->periodic_count > 0)
  147. ehci_set_command_bit(ehci, CMD_PSE);
  148. } else { /* Running */
  149. if (ehci->periodic_count == 0) {
  150. /* Turn off the schedule after a while */
  151. ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
  152. true);
  153. }
  154. }
  155. }
  156. /* Turn off the periodic schedule after a brief delay */
  157. static void ehci_disable_PSE(struct ehci_hcd *ehci)
  158. {
  159. ehci_clear_command_bit(ehci, CMD_PSE);
  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. end_unlink_async(ehci);
  179. /* Not in process context, so don't try to reset the controller */
  180. }
  181. /* start to unlink interrupt QHs */
  182. static void ehci_handle_start_intr_unlinks(struct ehci_hcd *ehci)
  183. {
  184. bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
  185. /*
  186. * Process all the QHs on the intr_unlink list that were added
  187. * before the current unlink cycle began. The list is in
  188. * temporal order, so stop when we reach the first entry in the
  189. * current cycle. But if the root hub isn't running then
  190. * process all the QHs on the list.
  191. */
  192. while (!list_empty(&ehci->intr_unlink_wait)) {
  193. struct ehci_qh *qh;
  194. qh = list_first_entry(&ehci->intr_unlink_wait,
  195. struct ehci_qh, unlink_node);
  196. if (!stopped && (qh->unlink_cycle ==
  197. ehci->intr_unlink_wait_cycle))
  198. break;
  199. list_del_init(&qh->unlink_node);
  200. start_unlink_intr(ehci, qh);
  201. }
  202. /* Handle remaining entries later */
  203. if (!list_empty(&ehci->intr_unlink_wait)) {
  204. ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true);
  205. ++ehci->intr_unlink_wait_cycle;
  206. }
  207. }
  208. /* Handle unlinked interrupt QHs once they are gone from the hardware */
  209. static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
  210. {
  211. bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
  212. /*
  213. * Process all the QHs on the intr_unlink list that were added
  214. * before the current unlink cycle began. The list is in
  215. * temporal order, so stop when we reach the first entry in the
  216. * current cycle. But if the root hub isn't running then
  217. * process all the QHs on the list.
  218. */
  219. ehci->intr_unlinking = true;
  220. while (!list_empty(&ehci->intr_unlink)) {
  221. struct ehci_qh *qh;
  222. qh = list_first_entry(&ehci->intr_unlink, struct ehci_qh,
  223. unlink_node);
  224. if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
  225. break;
  226. list_del_init(&qh->unlink_node);
  227. end_unlink_intr(ehci, qh);
  228. }
  229. /* Handle remaining entries later */
  230. if (!list_empty(&ehci->intr_unlink)) {
  231. ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
  232. ++ehci->intr_unlink_cycle;
  233. }
  234. ehci->intr_unlinking = false;
  235. }
  236. /* Start another free-iTDs/siTDs cycle */
  237. static void start_free_itds(struct ehci_hcd *ehci)
  238. {
  239. if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) {
  240. ehci->last_itd_to_free = list_entry(
  241. ehci->cached_itd_list.prev,
  242. struct ehci_itd, itd_list);
  243. ehci->last_sitd_to_free = list_entry(
  244. ehci->cached_sitd_list.prev,
  245. struct ehci_sitd, sitd_list);
  246. ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true);
  247. }
  248. }
  249. /* Wait for controller to stop using old iTDs and siTDs */
  250. static void end_free_itds(struct ehci_hcd *ehci)
  251. {
  252. struct ehci_itd *itd, *n;
  253. struct ehci_sitd *sitd, *sn;
  254. if (ehci->rh_state < EHCI_RH_RUNNING) {
  255. ehci->last_itd_to_free = NULL;
  256. ehci->last_sitd_to_free = NULL;
  257. }
  258. list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
  259. list_del(&itd->itd_list);
  260. dma_pool_free(ehci->itd_pool, itd, itd->itd_dma);
  261. if (itd == ehci->last_itd_to_free)
  262. break;
  263. }
  264. list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
  265. list_del(&sitd->sitd_list);
  266. dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma);
  267. if (sitd == ehci->last_sitd_to_free)
  268. break;
  269. }
  270. if (!list_empty(&ehci->cached_itd_list) ||
  271. !list_empty(&ehci->cached_sitd_list))
  272. start_free_itds(ehci);
  273. }
  274. /* Handle lost (or very late) IAA interrupts */
  275. static void ehci_iaa_watchdog(struct ehci_hcd *ehci)
  276. {
  277. u32 cmd, status;
  278. /*
  279. * Lost IAA irqs wedge things badly; seen first with a vt8235.
  280. * So we need this watchdog, but must protect it against both
  281. * (a) SMP races against real IAA firing and retriggering, and
  282. * (b) clean HC shutdown, when IAA watchdog was pending.
  283. */
  284. if (!ehci->iaa_in_progress || ehci->rh_state != EHCI_RH_RUNNING)
  285. return;
  286. /* If we get here, IAA is *REALLY* late. It's barely
  287. * conceivable that the system is so busy that CMD_IAAD
  288. * is still legitimately set, so let's be sure it's
  289. * clear before we read STS_IAA. (The HC should clear
  290. * CMD_IAAD when it sets STS_IAA.)
  291. */
  292. cmd = ehci_readl(ehci, &ehci->regs->command);
  293. /*
  294. * If IAA is set here it either legitimately triggered
  295. * after the watchdog timer expired (_way_ late, so we'll
  296. * still count it as lost) ... or a silicon erratum:
  297. * - VIA seems to set IAA without triggering the IRQ;
  298. * - IAAD potentially cleared without setting IAA.
  299. */
  300. status = ehci_readl(ehci, &ehci->regs->status);
  301. if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  302. COUNT(ehci->stats.lost_iaa);
  303. ehci_writel(ehci, STS_IAA, &ehci->regs->status);
  304. }
  305. ehci_dbg(ehci, "IAA watchdog: status %x cmd %x\n", status, cmd);
  306. end_unlink_async(ehci);
  307. }
  308. /* Enable the I/O watchdog, if appropriate */
  309. static void turn_on_io_watchdog(struct ehci_hcd *ehci)
  310. {
  311. /* Not needed if the controller isn't running or it's already enabled */
  312. if (ehci->rh_state != EHCI_RH_RUNNING ||
  313. (ehci->enabled_hrtimer_events &
  314. BIT(EHCI_HRTIMER_IO_WATCHDOG)))
  315. return;
  316. /*
  317. * Isochronous transfers always need the watchdog.
  318. * For other sorts we use it only if the flag is set.
  319. */
  320. if (ehci->isoc_count > 0 || (ehci->need_io_watchdog &&
  321. ehci->async_count + ehci->intr_count > 0))
  322. ehci_enable_event(ehci, EHCI_HRTIMER_IO_WATCHDOG, true);
  323. }
  324. /*
  325. * Handler functions for the hrtimer event types.
  326. * Keep this array in the same order as the event types indexed by
  327. * enum ehci_hrtimer_event in ehci.h.
  328. */
  329. static void (*event_handlers[])(struct ehci_hcd *) = {
  330. ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
  331. ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
  332. ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */
  333. ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */
  334. end_free_itds, /* EHCI_HRTIMER_FREE_ITDS */
  335. ehci_handle_start_intr_unlinks, /* EHCI_HRTIMER_START_UNLINK_INTR */
  336. unlink_empty_async, /* EHCI_HRTIMER_ASYNC_UNLINKS */
  337. ehci_iaa_watchdog, /* EHCI_HRTIMER_IAA_WATCHDOG */
  338. ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
  339. ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
  340. ehci_work, /* EHCI_HRTIMER_IO_WATCHDOG */
  341. };
  342. static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
  343. {
  344. struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
  345. ktime_t now;
  346. unsigned long events;
  347. unsigned long flags;
  348. unsigned e;
  349. spin_lock_irqsave(&ehci->lock, flags);
  350. events = ehci->enabled_hrtimer_events;
  351. ehci->enabled_hrtimer_events = 0;
  352. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  353. /*
  354. * Check each pending event. If its time has expired, handle
  355. * the event; otherwise re-enable it.
  356. */
  357. now = ktime_get();
  358. for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
  359. if (now.tv64 >= ehci->hr_timeouts[e].tv64)
  360. event_handlers[e](ehci);
  361. else
  362. ehci_enable_event(ehci, e, false);
  363. }
  364. spin_unlock_irqrestore(&ehci->lock, flags);
  365. return HRTIMER_NORESTART;
  366. }