USB: xhci: Reduce reads and writes of interrupter registers.
[linux-flexiantxendom0.git] / drivers / usb / host / xhci-ring.c
index da3519e..6860e9f 100644 (file)
@@ -301,28 +301,6 @@ static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
        return 1;
 }
 
-void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
-{
-       u64 temp;
-       dma_addr_t deq;
-
-       deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
-                       xhci->event_ring->dequeue);
-       if (deq == 0 && !in_interrupt())
-               xhci_warn(xhci, "WARN something wrong with SW event ring "
-                               "dequeue ptr.\n");
-       /* Update HC event ring dequeue pointer */
-       temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
-       temp &= ERST_PTR_MASK;
-       /* Don't clear the EHB bit (which is RW1C) because
-        * there might be more events to service.
-        */
-       temp &= ~ERST_EHB;
-       xhci_dbg(xhci, "// Write event ring dequeue pointer, preserving EHB bit\n");
-       xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
-                       &xhci->ir_set->erst_dequeue);
-}
-
 /* Ring the host controller doorbell after placing a command on the ring */
 void xhci_ring_cmd_db(struct xhci_hcd *xhci)
 {
@@ -419,6 +397,50 @@ static struct xhci_segment *find_trb_seg(
        return cur_seg;
 }
 
+
+static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
+               unsigned int slot_id, unsigned int ep_index,
+               unsigned int stream_id)
+{
+       struct xhci_virt_ep *ep;
+
+       ep = &xhci->devs[slot_id]->eps[ep_index];
+       /* Common case: no streams */
+       if (!(ep->ep_state & EP_HAS_STREAMS))
+               return ep->ring;
+
+       if (stream_id == 0) {
+               xhci_warn(xhci,
+                               "WARN: Slot ID %u, ep index %u has streams, "
+                               "but URB has no stream ID.\n",
+                               slot_id, ep_index);
+               return NULL;
+       }
+
+       if (stream_id < ep->stream_info->num_streams)
+               return ep->stream_info->stream_rings[stream_id];
+
+       xhci_warn(xhci,
+                       "WARN: Slot ID %u, ep index %u has "
+                       "stream IDs 1 to %u allocated, "
+                       "but stream ID %u is requested.\n",
+                       slot_id, ep_index,
+                       ep->stream_info->num_streams - 1,
+                       stream_id);
+       return NULL;
+}
+
+/* Get the right ring for the given URB.
+ * If the endpoint supports streams, boundary check the URB's stream ID.
+ * If the endpoint doesn't support streams, return the singular endpoint ring.
+ */
+static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
+               struct urb *urb)
+{
+       return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
+               xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
+}
+
 /*
  * Move the xHC's endpoint ring dequeue pointer past cur_td.
  * Record the new state of the xHC's endpoint ring dequeue segment,
@@ -1140,7 +1162,6 @@ static void handle_port_status(struct xhci_hcd *xhci,
 
        /* Update event ring dequeue pointer before dropping the lock */
        inc_deq(xhci, xhci->event_ring, true);
-       xhci_set_hc_event_deq(xhci);
 
        spin_unlock(&xhci->lock);
        /* Pass this up to the core */
@@ -1880,7 +1901,6 @@ cleanup:
                 */
                if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
                        inc_deq(xhci, xhci->event_ring, true);
-                       xhci_set_hc_event_deq(xhci);
                }
 
                if (ret) {
@@ -1920,7 +1940,7 @@ cleanup:
  * This function handles all OS-owned events on the event ring.  It may drop
  * xhci->lock between event processing (e.g. to pass up port status changes).
  */
-void xhci_handle_event(struct xhci_hcd *xhci)
+static void xhci_handle_event(struct xhci_hcd *xhci)
 {
        union xhci_trb *event;
        int update_ptrs = 1;
@@ -1978,15 +1998,130 @@ void xhci_handle_event(struct xhci_hcd *xhci)
                return;
        }
 
-       if (update_ptrs) {
-               /* Update SW and HC event ring dequeue pointer */
+       if (update_ptrs)
+               /* Update SW event ring dequeue pointer */
                inc_deq(xhci, xhci->event_ring, true);
-               xhci_set_hc_event_deq(xhci);
-       }
+
        /* Are there more items on the event ring? */
        xhci_handle_event(xhci);
 }
 
+/*
+ * xHCI spec says we can get an interrupt, and if the HC has an error condition,
+ * we might get bad data out of the event ring.  Section 4.10.2.7 has a list of
+ * indicators of an event TRB error, but we check the status *first* to be safe.
+ */
+irqreturn_t xhci_irq(struct usb_hcd *hcd)
+{
+       struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       u32 status;
+       union xhci_trb *trb;
+       u64 temp_64;
+       union xhci_trb *event_ring_deq;
+       dma_addr_t deq;
+
+       spin_lock(&xhci->lock);
+       trb = xhci->event_ring->dequeue;
+       /* Check if the xHC generated the interrupt, or the irq is shared */
+       status = xhci_readl(xhci, &xhci->op_regs->status);
+       if (status == 0xffffffff)
+               goto hw_died;
+
+       if (!(status & STS_EINT)) {
+               spin_unlock(&xhci->lock);
+               xhci_warn(xhci, "Spurious interrupt.\n");
+               return IRQ_NONE;
+       }
+       xhci_dbg(xhci, "op reg status = %08x\n", status);
+       xhci_dbg(xhci, "Event ring dequeue ptr:\n");
+       xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
+                       (unsigned long long)
+                       xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
+                       lower_32_bits(trb->link.segment_ptr),
+                       upper_32_bits(trb->link.segment_ptr),
+                       (unsigned int) trb->link.intr_target,
+                       (unsigned int) trb->link.control);
+
+       if (status & STS_FATAL) {
+               xhci_warn(xhci, "WARNING: Host System Error\n");
+               xhci_halt(xhci);
+hw_died:
+               xhci_to_hcd(xhci)->state = HC_STATE_HALT;
+               spin_unlock(&xhci->lock);
+               return -ESHUTDOWN;
+       }
+
+       /*
+        * Clear the op reg interrupt status first,
+        * so we can receive interrupts from other MSI-X interrupters.
+        * Write 1 to clear the interrupt status.
+        */
+       status |= STS_EINT;
+       xhci_writel(xhci, status, &xhci->op_regs->status);
+       /* FIXME when MSI-X is supported and there are multiple vectors */
+       /* Clear the MSI-X event interrupt status */
+
+       if (hcd->irq != -1) {
+               u32 irq_pending;
+               /* Acknowledge the PCI interrupt */
+               irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
+               irq_pending |= 0x3;
+               xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
+       }
+
+       if (xhci->xhc_state & XHCI_STATE_DYING) {
+               xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
+                               "Shouldn't IRQs be disabled?\n");
+               /* Clear the event handler busy flag (RW1C);
+                * the event ring should be empty.
+                */
+               temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
+               xhci_write_64(xhci, temp_64 | ERST_EHB,
+                               &xhci->ir_set->erst_dequeue);
+               spin_unlock(&xhci->lock);
+
+               return IRQ_HANDLED;
+       }
+
+       event_ring_deq = xhci->event_ring->dequeue;
+       /* FIXME this should be a delayed service routine
+        * that clears the EHB.
+        */
+       xhci_handle_event(xhci);
+
+       temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
+       /* If necessary, update the HW's version of the event ring deq ptr. */
+       if (event_ring_deq != xhci->event_ring->dequeue) {
+               deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
+                               xhci->event_ring->dequeue);
+               if (deq == 0)
+                       xhci_warn(xhci, "WARN something wrong with SW event "
+                                       "ring dequeue ptr.\n");
+               /* Update HC event ring dequeue pointer */
+               temp_64 &= ERST_PTR_MASK;
+               temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
+       }
+
+       /* Clear the event handler busy flag (RW1C); event ring is empty. */
+       temp_64 |= ERST_EHB;
+       xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
+
+       spin_unlock(&xhci->lock);
+
+       return IRQ_HANDLED;
+}
+
+irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
+{
+       irqreturn_t ret;
+
+       set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
+
+       ret = xhci_irq(hcd);
+
+       return ret;
+}
+
 /****          Endpoint Ring Operations        ****/
 
 /*