xhci-ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*
  23. * Ring initialization rules:
  24. * 1. Each segment is initialized to zero, except for link TRBs.
  25. * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
  26. * Consumer Cycle State (CCS), depending on ring function.
  27. * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
  28. *
  29. * Ring behavior rules:
  30. * 1. A ring is empty if enqueue == dequeue. This means there will always be at
  31. * least one free TRB in the ring. This is useful if you want to turn that
  32. * into a link TRB and expand the ring.
  33. * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
  34. * link TRB, then load the pointer with the address in the link TRB. If the
  35. * link TRB had its toggle bit set, you may need to update the ring cycle
  36. * state (see cycle bit rules). You may have to do this multiple times
  37. * until you reach a non-link TRB.
  38. * 3. A ring is full if enqueue++ (for the definition of increment above)
  39. * equals the dequeue pointer.
  40. *
  41. * Cycle bit rules:
  42. * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
  43. * in a link TRB, it must toggle the ring cycle state.
  44. * 2. When a producer increments an enqueue pointer and encounters a toggle bit
  45. * in a link TRB, it must toggle the ring cycle state.
  46. *
  47. * Producer rules:
  48. * 1. Check if ring is full before you enqueue.
  49. * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
  50. * Update enqueue pointer between each write (which may update the ring
  51. * cycle state).
  52. * 3. Notify consumer. If SW is producer, it rings the doorbell for command
  53. * and endpoint rings. If HC is the producer for the event ring,
  54. * and it generates an interrupt according to interrupt modulation rules.
  55. *
  56. * Consumer rules:
  57. * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
  58. * the TRB is owned by the consumer.
  59. * 2. Update dequeue pointer (which may update the ring cycle state) and
  60. * continue processing TRBs until you reach a TRB which is not owned by you.
  61. * 3. Notify the producer. SW is the consumer for the event ring, and it
  62. * updates event ring dequeue pointer. HC is the consumer for the command and
  63. * endpoint rings; it generates events on the event ring for these.
  64. */
  65. #include "xhci.h"
  66. /*
  67. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  68. * address of the TRB.
  69. */
  70. dma_addr_t trb_virt_to_dma(struct xhci_segment *seg,
  71. union xhci_trb *trb)
  72. {
  73. unsigned int offset;
  74. if (!seg || !trb || (void *) trb < (void *) seg->trbs)
  75. return 0;
  76. /* offset in bytes, since these are byte-addressable */
  77. offset = (unsigned int) trb - (unsigned int) seg->trbs;
  78. /* SEGMENT_SIZE in bytes, trbs are 16-byte aligned */
  79. if (offset > SEGMENT_SIZE || (offset % sizeof(*trb)) != 0)
  80. return 0;
  81. return seg->dma + offset;
  82. }
  83. /* Does this link TRB point to the first segment in a ring,
  84. * or was the previous TRB the last TRB on the last segment in the ERST?
  85. */
  86. static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
  87. struct xhci_segment *seg, union xhci_trb *trb)
  88. {
  89. if (ring == xhci->event_ring)
  90. return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
  91. (seg->next == xhci->event_ring->first_seg);
  92. else
  93. return trb->link.control & LINK_TOGGLE;
  94. }
  95. /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
  96. * segment? I.e. would the updated event TRB pointer step off the end of the
  97. * event seg?
  98. */
  99. static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  100. struct xhci_segment *seg, union xhci_trb *trb)
  101. {
  102. if (ring == xhci->event_ring)
  103. return trb == &seg->trbs[TRBS_PER_SEGMENT];
  104. else
  105. return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
  106. }
  107. /*
  108. * See Cycle bit rules. SW is the consumer for the event ring only.
  109. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  110. */
  111. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  112. {
  113. union xhci_trb *next = ++(ring->dequeue);
  114. ring->deq_updates++;
  115. /* Update the dequeue pointer further if that was a link TRB or we're at
  116. * the end of an event ring segment (which doesn't have link TRBS)
  117. */
  118. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  119. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  120. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  121. if (!in_interrupt())
  122. xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n",
  123. (unsigned int) ring,
  124. (unsigned int) ring->cycle_state);
  125. }
  126. ring->deq_seg = ring->deq_seg->next;
  127. ring->dequeue = ring->deq_seg->trbs;
  128. next = ring->dequeue;
  129. }
  130. }
  131. /*
  132. * See Cycle bit rules. SW is the consumer for the event ring only.
  133. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  134. *
  135. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  136. * chain bit is set), then set the chain bit in all the following link TRBs.
  137. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  138. * have their chain bit cleared (so that each Link TRB is a separate TD).
  139. *
  140. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  141. * set, but other sections talk about dealing with the chain bit set.
  142. * Assume section 6.4.4.1 is wrong, and the chain bit can be set in a Link TRB.
  143. */
  144. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  145. {
  146. u32 chain;
  147. union xhci_trb *next;
  148. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  149. next = ++(ring->enqueue);
  150. ring->enq_updates++;
  151. /* Update the dequeue pointer further if that was a link TRB or we're at
  152. * the end of an event ring segment (which doesn't have link TRBS)
  153. */
  154. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  155. if (!consumer) {
  156. if (ring != xhci->event_ring) {
  157. /* Give this link TRB to the hardware */
  158. if (next->link.control & TRB_CYCLE)
  159. next->link.control &= (u32) ~TRB_CYCLE;
  160. else
  161. next->link.control |= (u32) TRB_CYCLE;
  162. next->link.control &= TRB_CHAIN;
  163. next->link.control |= chain;
  164. }
  165. /* Toggle the cycle bit after the last ring segment. */
  166. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  167. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  168. if (!in_interrupt())
  169. xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n",
  170. (unsigned int) ring,
  171. (unsigned int) ring->cycle_state);
  172. }
  173. }
  174. ring->enq_seg = ring->enq_seg->next;
  175. ring->enqueue = ring->enq_seg->trbs;
  176. next = ring->enqueue;
  177. }
  178. }
  179. /*
  180. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  181. * above.
  182. * FIXME: this would be simpler and faster if we just kept track of the number
  183. * of free TRBs in a ring.
  184. */
  185. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  186. unsigned int num_trbs)
  187. {
  188. int i;
  189. union xhci_trb *enq = ring->enqueue;
  190. struct xhci_segment *enq_seg = ring->enq_seg;
  191. /* Check if ring is empty */
  192. if (enq == ring->dequeue)
  193. return 1;
  194. /* Make sure there's an extra empty TRB available */
  195. for (i = 0; i <= num_trbs; ++i) {
  196. if (enq == ring->dequeue)
  197. return 0;
  198. enq++;
  199. while (last_trb(xhci, ring, enq_seg, enq)) {
  200. enq_seg = enq_seg->next;
  201. enq = enq_seg->trbs;
  202. }
  203. }
  204. return 1;
  205. }
  206. void set_hc_event_deq(struct xhci_hcd *xhci)
  207. {
  208. u32 temp;
  209. dma_addr_t deq;
  210. deq = trb_virt_to_dma(xhci->event_ring->deq_seg,
  211. xhci->event_ring->dequeue);
  212. if (deq == 0 && !in_interrupt())
  213. xhci_warn(xhci, "WARN something wrong with SW event ring "
  214. "dequeue ptr.\n");
  215. /* Update HC event ring dequeue pointer */
  216. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  217. temp &= ERST_PTR_MASK;
  218. if (!in_interrupt())
  219. xhci_dbg(xhci, "// Write event ring dequeue pointer\n");
  220. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  221. xhci_writel(xhci, (deq & ~ERST_PTR_MASK) | temp,
  222. &xhci->ir_set->erst_dequeue[0]);
  223. }
  224. /* Ring the host controller doorbell after placing a command on the ring */
  225. void ring_cmd_db(struct xhci_hcd *xhci)
  226. {
  227. u32 temp;
  228. xhci_dbg(xhci, "// Ding dong!\n");
  229. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  230. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  231. /* Flush PCI posted writes */
  232. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  233. }
  234. static void handle_cmd_completion(struct xhci_hcd *xhci,
  235. struct xhci_event_cmd *event)
  236. {
  237. u64 cmd_dma;
  238. dma_addr_t cmd_dequeue_dma;
  239. /* Check completion code */
  240. if (GET_COMP_CODE(event->status) != COMP_SUCCESS)
  241. xhci_dbg(xhci, "WARN: unsuccessful no-op command\n");
  242. cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0];
  243. cmd_dequeue_dma = trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  244. xhci->cmd_ring->dequeue);
  245. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  246. if (cmd_dequeue_dma == 0) {
  247. xhci->error_bitmask |= 1 << 4;
  248. return;
  249. }
  250. /* Does the DMA address match our internal dequeue pointer address? */
  251. if (cmd_dma != (u64) cmd_dequeue_dma) {
  252. xhci->error_bitmask |= 1 << 5;
  253. return;
  254. }
  255. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  256. case TRB_TYPE(TRB_CMD_NOOP):
  257. ++xhci->noops_handled;
  258. break;
  259. default:
  260. /* Skip over unknown commands on the event ring */
  261. xhci->error_bitmask |= 1 << 6;
  262. break;
  263. }
  264. inc_deq(xhci, xhci->cmd_ring, false);
  265. }
  266. static void handle_port_status(struct xhci_hcd *xhci,
  267. union xhci_trb *event)
  268. {
  269. u32 port_id;
  270. /* Port status change events always have a successful completion code */
  271. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  272. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  273. xhci->error_bitmask |= 1 << 8;
  274. }
  275. /* FIXME: core doesn't care about all port link state changes yet */
  276. port_id = GET_PORT_ID(event->generic.field[0]);
  277. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  278. /* Update event ring dequeue pointer before dropping the lock */
  279. inc_deq(xhci, xhci->event_ring, true);
  280. set_hc_event_deq(xhci);
  281. spin_unlock(&xhci->lock);
  282. /* Pass this up to the core */
  283. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  284. spin_lock(&xhci->lock);
  285. }
  286. /*
  287. * This function handles all OS-owned events on the event ring. It may drop
  288. * xhci->lock between event processing (e.g. to pass up port status changes).
  289. */
  290. void handle_event(struct xhci_hcd *xhci)
  291. {
  292. union xhci_trb *event;
  293. int update_ptrs = 1;
  294. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  295. xhci->error_bitmask |= 1 << 1;
  296. return;
  297. }
  298. event = xhci->event_ring->dequeue;
  299. /* Does the HC or OS own the TRB? */
  300. if ((event->event_cmd.flags & TRB_CYCLE) !=
  301. xhci->event_ring->cycle_state) {
  302. xhci->error_bitmask |= 1 << 2;
  303. return;
  304. }
  305. /* FIXME: Handle more event types. */
  306. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  307. case TRB_TYPE(TRB_COMPLETION):
  308. handle_cmd_completion(xhci, &event->event_cmd);
  309. break;
  310. case TRB_TYPE(TRB_PORT_STATUS):
  311. handle_port_status(xhci, event);
  312. update_ptrs = 0;
  313. break;
  314. default:
  315. xhci->error_bitmask |= 1 << 3;
  316. }
  317. if (update_ptrs) {
  318. /* Update SW and HC event ring dequeue pointer */
  319. inc_deq(xhci, xhci->event_ring, true);
  320. set_hc_event_deq(xhci);
  321. }
  322. /* Are there more items on the event ring? */
  323. handle_event(xhci);
  324. }
  325. /*
  326. * Generic function for queueing a TRB on a ring.
  327. * The caller must have checked to make sure there's room on the ring.
  328. */
  329. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  330. bool consumer,
  331. u32 field1, u32 field2, u32 field3, u32 field4)
  332. {
  333. struct xhci_generic_trb *trb;
  334. trb = &ring->enqueue->generic;
  335. trb->field[0] = field1;
  336. trb->field[1] = field2;
  337. trb->field[2] = field3;
  338. trb->field[3] = field4;
  339. inc_enq(xhci, ring, consumer);
  340. }
  341. /* Generic function for queueing a command TRB on the command ring */
  342. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  343. {
  344. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  345. if (!in_interrupt())
  346. xhci_err(xhci, "ERR: No room for command on command ring\n");
  347. return -ENOMEM;
  348. }
  349. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  350. field4 | xhci->cmd_ring->cycle_state);
  351. return 0;
  352. }
  353. /* Queue a no-op command on the command ring */
  354. static int queue_cmd_noop(struct xhci_hcd *xhci)
  355. {
  356. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  357. }
  358. /*
  359. * Place a no-op command on the command ring to test the command and
  360. * event ring.
  361. */
  362. void *setup_one_noop(struct xhci_hcd *xhci)
  363. {
  364. if (queue_cmd_noop(xhci) < 0)
  365. return NULL;
  366. xhci->noops_submitted++;
  367. return ring_cmd_db;
  368. }