xhci-ring.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. int slot_id = TRB_TO_SLOT_ID(event->flags);
  238. u64 cmd_dma;
  239. dma_addr_t cmd_dequeue_dma;
  240. cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0];
  241. cmd_dequeue_dma = trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  242. xhci->cmd_ring->dequeue);
  243. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  244. if (cmd_dequeue_dma == 0) {
  245. xhci->error_bitmask |= 1 << 4;
  246. return;
  247. }
  248. /* Does the DMA address match our internal dequeue pointer address? */
  249. if (cmd_dma != (u64) cmd_dequeue_dma) {
  250. xhci->error_bitmask |= 1 << 5;
  251. return;
  252. }
  253. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  254. case TRB_TYPE(TRB_ENABLE_SLOT):
  255. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  256. xhci->slot_id = slot_id;
  257. else
  258. xhci->slot_id = 0;
  259. complete(&xhci->addr_dev);
  260. break;
  261. case TRB_TYPE(TRB_DISABLE_SLOT):
  262. if (xhci->devs[slot_id])
  263. xhci_free_virt_device(xhci, slot_id);
  264. break;
  265. case TRB_TYPE(TRB_ADDR_DEV):
  266. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  267. complete(&xhci->addr_dev);
  268. break;
  269. case TRB_TYPE(TRB_CMD_NOOP):
  270. ++xhci->noops_handled;
  271. break;
  272. default:
  273. /* Skip over unknown commands on the event ring */
  274. xhci->error_bitmask |= 1 << 6;
  275. break;
  276. }
  277. inc_deq(xhci, xhci->cmd_ring, false);
  278. }
  279. static void handle_port_status(struct xhci_hcd *xhci,
  280. union xhci_trb *event)
  281. {
  282. u32 port_id;
  283. /* Port status change events always have a successful completion code */
  284. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  285. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  286. xhci->error_bitmask |= 1 << 8;
  287. }
  288. /* FIXME: core doesn't care about all port link state changes yet */
  289. port_id = GET_PORT_ID(event->generic.field[0]);
  290. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  291. /* Update event ring dequeue pointer before dropping the lock */
  292. inc_deq(xhci, xhci->event_ring, true);
  293. set_hc_event_deq(xhci);
  294. spin_unlock(&xhci->lock);
  295. /* Pass this up to the core */
  296. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  297. spin_lock(&xhci->lock);
  298. }
  299. /*
  300. * This function handles all OS-owned events on the event ring. It may drop
  301. * xhci->lock between event processing (e.g. to pass up port status changes).
  302. */
  303. void handle_event(struct xhci_hcd *xhci)
  304. {
  305. union xhci_trb *event;
  306. int update_ptrs = 1;
  307. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  308. xhci->error_bitmask |= 1 << 1;
  309. return;
  310. }
  311. event = xhci->event_ring->dequeue;
  312. /* Does the HC or OS own the TRB? */
  313. if ((event->event_cmd.flags & TRB_CYCLE) !=
  314. xhci->event_ring->cycle_state) {
  315. xhci->error_bitmask |= 1 << 2;
  316. return;
  317. }
  318. /* FIXME: Handle more event types. */
  319. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  320. case TRB_TYPE(TRB_COMPLETION):
  321. handle_cmd_completion(xhci, &event->event_cmd);
  322. break;
  323. case TRB_TYPE(TRB_PORT_STATUS):
  324. handle_port_status(xhci, event);
  325. update_ptrs = 0;
  326. break;
  327. default:
  328. xhci->error_bitmask |= 1 << 3;
  329. }
  330. if (update_ptrs) {
  331. /* Update SW and HC event ring dequeue pointer */
  332. inc_deq(xhci, xhci->event_ring, true);
  333. set_hc_event_deq(xhci);
  334. }
  335. /* Are there more items on the event ring? */
  336. handle_event(xhci);
  337. }
  338. /*
  339. * Generic function for queueing a TRB on a ring.
  340. * The caller must have checked to make sure there's room on the ring.
  341. */
  342. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  343. bool consumer,
  344. u32 field1, u32 field2, u32 field3, u32 field4)
  345. {
  346. struct xhci_generic_trb *trb;
  347. trb = &ring->enqueue->generic;
  348. trb->field[0] = field1;
  349. trb->field[1] = field2;
  350. trb->field[2] = field3;
  351. trb->field[3] = field4;
  352. inc_enq(xhci, ring, consumer);
  353. }
  354. /* Generic function for queueing a command TRB on the command ring */
  355. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  356. {
  357. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  358. if (!in_interrupt())
  359. xhci_err(xhci, "ERR: No room for command on command ring\n");
  360. return -ENOMEM;
  361. }
  362. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  363. field4 | xhci->cmd_ring->cycle_state);
  364. return 0;
  365. }
  366. /* Queue a no-op command on the command ring */
  367. static int queue_cmd_noop(struct xhci_hcd *xhci)
  368. {
  369. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  370. }
  371. /*
  372. * Place a no-op command on the command ring to test the command and
  373. * event ring.
  374. */
  375. void *setup_one_noop(struct xhci_hcd *xhci)
  376. {
  377. if (queue_cmd_noop(xhci) < 0)
  378. return NULL;
  379. xhci->noops_submitted++;
  380. return ring_cmd_db;
  381. }
  382. /* Queue a slot enable or disable request on the command ring */
  383. int queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  384. {
  385. return queue_command(xhci, 0, 0, 0,
  386. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  387. }
  388. /* Queue an address device command TRB */
  389. int queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  390. {
  391. return queue_command(xhci, in_ctx_ptr, 0, 0,
  392. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  393. }