Skip to content

Commit

Permalink
xhci: Set DESI bits in ERDP register correctly
Browse files Browse the repository at this point in the history
When using more than one Event Ring segment (ERSTSZ > 1), software shall
set the DESI bits in the ERDP register to the number of the segment to
which the upper ERDP bits are pointing.  The xHC may use the DESI bits
as a shortcut to determine whether it needs to check for an Event Ring
Full condition:  If it's enqueueing events in a different segment, it
need not compare its internal Enqueue Pointer with the Dequeue Pointer
in the upper bits of the ERDP register (sec 5.5.2.3.3).

Not setting the DESI bits correctly can result in the xHC enqueueing
events past the Dequeue Pointer.  On Renesas uPD720201 host controllers,
incorrect DESI bits cause an interrupt storm.  For comparison, VIA VL805
host controllers do not exhibit such problems.  Perhaps they do not take
advantage of the optimization afforded by the DESI bits.

To fix the issue, assign the segment number to each struct xhci_segment
in xhci_segment_alloc().  When advancing the Dequeue Pointer in
xhci_update_erst_dequeue(), write the segment number to the DESI bits.

On driver probe, set the DESI bits to zero in xhci_set_hc_event_deq() as
processing starts in segment 0.  Previously that function (incorrectly)
treated the DESI bits as if they're declared RsvdP.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
  • Loading branch information
l1k committed Aug 28, 2023
1 parent f5448e8 commit 148d67f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
20 changes: 10 additions & 10 deletions drivers/usb/host/xhci-mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
unsigned int cycle_state,
unsigned int max_packet,
unsigned int num,
gfp_t flags)
{
struct xhci_segment *seg;
Expand Down Expand Up @@ -59,6 +60,7 @@ static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
for (i = 0; i < TRBS_PER_SEGMENT; i++)
seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE);
}
seg->num = num;
seg->dma = dma;
seg->next = NULL;

Expand Down Expand Up @@ -328,23 +330,25 @@ static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci,
unsigned int max_packet, gfp_t flags)
{
struct xhci_segment *prev;
unsigned int num = 0;
bool chain_links;

/* Set chain bit for 0.95 hosts, and for isoc rings on AMD 0.96 host */
chain_links = !!(xhci_link_trb_quirk(xhci) ||
(type == TYPE_ISOC &&
(xhci->quirks & XHCI_AMD_0x96_HOST)));

prev = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
prev = xhci_segment_alloc(xhci, cycle_state, max_packet, num, flags);
if (!prev)
return -ENOMEM;
num_segs--;
num++;

*first = prev;
while (num_segs > 0) {
while (num < num_segs) {
struct xhci_segment *next;

next = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
next = xhci_segment_alloc(xhci, cycle_state, max_packet, num,
flags);
if (!next) {
prev = *first;
while (prev) {
Expand All @@ -357,7 +361,7 @@ static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci,
xhci_link_segments(prev, next, trbs_per_seg, type, chain_links);

prev = next;
num_segs--;
num++;
}
xhci_link_segments(prev, *first, trbs_per_seg, type, chain_links);
*last = prev;
Expand Down Expand Up @@ -2126,7 +2130,6 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci)

static 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,
Expand All @@ -2135,16 +2138,13 @@ static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
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_trace(xhci, trace_xhci_dbg_init,
"// Write event ring dequeue pointer, "
"preserving EHB bit");
xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK),
&xhci->ir_set->erst_dequeue);
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/host/xhci-ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2908,7 +2908,7 @@ static void xhci_update_erst_dequeue(struct xhci_hcd *xhci,
return;

/* Update HC event ring dequeue pointer */
temp_64 &= ERST_DESI_MASK;
temp_64 = xhci->event_ring->deq_seg->num & ERST_DESI_MASK;
temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
}

Expand Down
1 change: 1 addition & 0 deletions drivers/usb/host/xhci.h
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ struct xhci_segment {
union xhci_trb *trbs;
/* private to HCD */
struct xhci_segment *next;
unsigned int num;
dma_addr_t dma;
/* Max packet sized bounce buffer for td-fragmant alignment */
dma_addr_t bounce_dma;
Expand Down

0 comments on commit 148d67f

Please sign in to comment.