Skip to contentSkip to conversation

usb: xhci: add Immediate Data Transfer support

RFC In review Nicolas Saenz Julienne 2 participants 4 messages Feb 5, 2019 – Feb 6, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 usb: xhci: add Immediate Data Transfer support

Immediate data transfers (IDT) allow the HCD to copy small chunks of data (up to 8bytes) directly into its output transfer TRBs. This avoids the somewhat expensive DMA mappings that are performed by default on most URBs submissions.

In the case an URB was suitable for IDT. The data is directly copied into the "Data Buffer Pointer" region of the TRB and the IDT flag is set. Instead of triggering memory accesses the HC will use the data directly.

The implementation covers all kind of output endpoints. All have been tested successfully with multiple devices except for isochronous ones. I can't seem to find a device that'll perform transfers with such small packet sizes.

The implementation takes into account that the 8 byte buffers provided by the URB will never cross a 64KB boundary.

---
Chages since first RFC:
- Got rid of URB flag
- Implement feature for Control transfers
- Implement feature for Isoc transfers

 drivers/usb/host/xhci-ring.c | 20 ++++++++++++++++++++
 drivers/usb/host/xhci.c      | 16 ++++++++++++++++
 drivers/usb/host/xhci.h      | 12 ++++++++++++
 3 files changed, 48 insertions(+)
drivers/usb/host/xhci-ring.c +20 -0
index 40fa25c4d041..a4efbe62a1a3 100644
@@ -3272,8 +3272,15 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 			field |= TRB_IOC;
 			more_trbs_coming = false;
 			td->last_trb = ring->enqueue;
+
+			if (xhci_urb_suitable_for_idt(urb)) {
+				memcpy(&send_addr, urb->transfer_buffer,
+				       trb_buff_len);
+				field |= TRB_IDT;
+			}
 		}
 
+
 		/* Only set interrupt on short packet for IN endpoints */
 		if (usb_urb_dir_in(urb))
 			field |= TRB_ISP;
@@ -3411,6 +3418,12 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 	if (urb->transfer_buffer_length > 0) {
 		u32 length_field, remainder;
 
+		if (xhci_urb_suitable_for_idt(urb)) {
+			memcpy(&urb->transfer_dma, urb->transfer_buffer,
+			       urb->transfer_buffer_length);
+			field |= TRB_IDT;
+		}
+
 		remainder = xhci_td_remainder(xhci, 0,
 				urb->transfer_buffer_length,
 				urb->transfer_buffer_length,
@@ -3420,6 +3433,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 				TRB_INTR_TARGET(0);
 		if (setup->bRequestType & USB_DIR_IN)
 			field |= TRB_DIR_IN;
+
 		queue_trb(xhci, ep_ring, true,
 				lower_32_bits(urb->transfer_dma),
 				upper_32_bits(urb->transfer_dma),
@@ -3710,6 +3724,12 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 			if (trb_buff_len > td_remain_len)
 				trb_buff_len = td_remain_len;
 
+			if (xhci_urb_suitable_for_idt(urb)) {
+				memcpy(&addr, urb->transfer_buffer,
+				       trb_buff_len);
+				field |= TRB_IDT;
+			}
+
 			/* Set the TRB length, TD size, & interrupter fields. */
 			remainder = xhci_td_remainder(xhci, running_total,
 						   trb_buff_len, td_len,
drivers/usb/host/xhci.c +16 -0
index 005e65922608..dec62f7f5dc8 100644
@@ -1238,6 +1238,21 @@ EXPORT_SYMBOL_GPL(xhci_resume);
 
 /*-------------------------------------------------------------------------*/
 
+/*
+ * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
+ * we'll copy the actual data into the TRB address register. This is limited to
+ * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize
+ * >= 8 bytes.
+ */
+static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
+				gfp_t mem_flags)
+{
+	if (xhci_urb_suitable_for_idt(urb))
+		return 0;
+
+	return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
+}
+
 /**
  * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  * HCDs.  Find the index for an endpoint given its descriptor.  Use the return
@@ -5155,6 +5170,7 @@ static const struct hc_driver xhci_hc_driver = {
 	/*
 	 * managing i/o requests and associated device resources
 	 */
+	.map_urb_for_dma =      xhci_map_urb_for_dma,
 	.urb_enqueue =		xhci_urb_enqueue,
 	.urb_dequeue =		xhci_urb_dequeue,
 	.alloc_dev =		xhci_alloc_dev,
drivers/usb/host/xhci.h +12 -0
index 652dc36e3012..9d77b0901ab7 100644
@@ -1295,6 +1295,8 @@ enum xhci_setup_dev {
 #define TRB_IOC			(1<<5)
 /* The buffer pointer contains immediate data */
 #define TRB_IDT			(1<<6)
+/* TDs smaller than this might use IDT */
+#define TRB_IDT_MAX_SIZE	8
 
 /* Block Event Interrupt */
 #define	TRB_BEI			(1<<9)
@@ -2141,6 +2143,16 @@ static inline struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
 					urb->stream_id);
 }
 
+static inline bool xhci_urb_suitable_for_idt(struct urb *urb)
+{
+	if (usb_endpoint_maxp(&urb->ep->desc) >= TRB_IDT_MAX_SIZE &&
+	    urb->transfer_buffer_length <= TRB_IDT_MAX_SIZE &&
+	    usb_urb_dir_out(urb))
+		return true;
+
+	return false;
+}
+
 static inline char *xhci_slot_state_string(u32 state)
 {
 	switch (state) {
#1 lore raw

Hi,

Quoted 7 lines · Nicolas Saenz Julienne
drivers/usb/host/xhci-ring.c +0 -0
index 40fa25c4d041..a4efbe62a1a3 100644
@@ -3272,8 +3272,15 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 			field |= TRB_IOC;
 			more_trbs_coming = false;

drivers/usb/host/xhci-ring.c int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, · line 3283

Quoted 10 lines
drivers/usb/host/xhci-ring.c +7 -0
 			td->last_trb = ring->enqueue;
+
+			if (xhci_urb_suitable_for_idt(urb)) {
+				memcpy(&send_addr, urb->transfer_buffer,
+				       trb_buff_len);
+				field |= TRB_IDT;
+			}
 		}
 
+

trailing change

Quoted 14 lines
drivers/usb/host/xhci-ring.c +6 -0
@@ -3411,6 +3418,12 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 	if (urb->transfer_buffer_length > 0) {
 		u32 length_field, remainder;
 
+		if (xhci_urb_suitable_for_idt(urb)) {
+			memcpy(&urb->transfer_dma, urb->transfer_buffer,
+			       urb->transfer_buffer_length);
+			field |= TRB_IDT;
+		}
+
 		remainder = xhci_td_remainder(xhci, 0,
 				urb->transfer_buffer_length,
 				urb->transfer_buffer_length,

drivers/usb/host/xhci-ring.c int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, · line 3436

Quoted 4 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -3420,6 +3433,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
 				TRB_INTR_TARGET(0);
 		if (setup->bRequestType & USB_DIR_IN)
 			field |= TRB_DIR_IN;
+

trailing change

Quoted 12 lines
drivers/usb/host/xhci.c +4 -0
index 005e65922608..dec62f7f5dc8 100644
@@ -1238,6 +1238,21 @@ EXPORT_SYMBOL_GPL(xhci_resume);
 
 /*-------------------------------------------------------------------------*/
 
+/*
+ * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
+ * we'll copy the actual data into the TRB address register. This is limited to
+ * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize

drivers/usb/host/xhci.c EXPORT_SYMBOL_GPL(xhci_resume); · line 1254

Quoted 10 lines
drivers/usb/host/xhci.c +10 -0
+ * >= 8 bytes.
+ */
+static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
+				gfp_t mem_flags)
+{
+	if (xhci_urb_suitable_for_idt(urb))
+		return 0;
+
+	return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
+}

don't you need a matching unmap_urb_for_dma()??

drivers/usb/host/xhci.h enum xhci_setup_dev { · line 1298

Quoted 9 lines
drivers/usb/host/xhci.h +1 -0
index 652dc36e3012..9d77b0901ab7 100644
@@ -1295,6 +1295,8 @@ enum xhci_setup_dev {
 #define TRB_IOC			(1<<5)
 /* The buffer pointer contains immediate data */
 #define TRB_IDT			(1<<6)
+/* TDs smaller than this might use IDT */

Technically, "TDs at most this" since you're 8 itself is an allowed size.

#2 lore raw

Hi Felipe, thanks for the review!

Quoted 20 lines · Felipe Balbi

Hi,

Quoted 17 lines · Nicolas Saenz Julienne
drivers/usb/host/xhci-ring.c +6 -0
index 40fa25c4d041..a4efbe62a1a3 100644
@@ -3272,8 +3272,15 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t
 mem_flags,
 			field |= TRB_IOC;
 			more_trbs_coming = false;
 			td->last_trb = ring->enqueue;
+
+			if (xhci_urb_suitable_for_idt(urb)) {
+				memcpy(&send_addr, urb->transfer_buffer,
+				       trb_buff_len);
+				field |= TRB_IDT;
+			}
 		}
 
Quoted 3 lines

drivers/usb/host/xhci-ring.c int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t · line 3284

Quoted 1 line
drivers/usb/host/xhci-ring.c +1 -0
+

trailing change

Noted

Quoted 15 lines
Quoted 15 lines
drivers/usb/host/xhci-ring.c +6 -0
@@ -3411,6 +3418,12 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t
 mem_flags,
 	if (urb->transfer_buffer_length > 0) {
 		u32 length_field, remainder;
 
+		if (xhci_urb_suitable_for_idt(urb)) {
+			memcpy(&urb->transfer_dma, urb->transfer_buffer,
+			       urb->transfer_buffer_length);
+			field |= TRB_IDT;
+		}
+
 		remainder = xhci_td_remainder(xhci, 0,
 				urb->transfer_buffer_length,

urb->transfer_buffer_length,

Quoted 8 lines

drivers/usb/host/xhci-ring.c int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t · line 3437

Quoted 6 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -3420,6 +3433,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t
 mem_flags,
 				TRB_INTR_TARGET(0);
 		if (setup->bRequestType & USB_DIR_IN)
 			field |= TRB_DIR_IN;
+

trailing change

Noted

Quoted 20 lines
Quoted 20 lines
drivers/usb/host/xhci.c +3 -0
index 005e65922608..dec62f7f5dc8 100644
@@ -1238,6 +1238,21 @@ EXPORT_SYMBOL_GPL(xhci_resume);
 
 /*-------------------------------------------------------------------------
 */
 
+/*
+ * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
+ * we'll copy the actual data into the TRB address register. This is

limited to
+ * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize
+ * >= 8 bytes.
+ */

drivers/usb/host/xhci.c +2 -0
+static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
+				gfp_t mem_flags)
Quoted 8 lines

drivers/usb/host/xhci.c EXPORT_SYMBOL_GPL(xhci_resume); · line 1254

Quoted 6 lines
drivers/usb/host/xhci.c +6 -0
+{
+	if (xhci_urb_suitable_for_idt(urb))
+		return 0;
+
+	return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
+}

don't you need a matching unmap_urb_for_dma()??

Not really as every DMA mapping sets a matching URB flag to track it. For example when usb_hcd_map_urb_for_dma() uses dma_map_single() it will set URB_DMA_MAP_SINGLE in urb->transfer_flags, later on unmap_urb_for_dma() will catch it and unmap it. As I bypass the mapping altogether there are no flags set, so unmap_urb_for_dma() won't have any effect.

I could still add it for clarity, and well, I guess it'll save some instructions on the IDT suitable side.

Quoted 14 lines

drivers/usb/host/xhci.h enum xhci_setup_dev { · line 1298

Quoted 10 lines
drivers/usb/host/xhci.h +1 -0
index 652dc36e3012..9d77b0901ab7 100644
@@ -1295,6 +1295,8 @@ enum xhci_setup_dev {
 #define TRB_IOC			(1<<5)
 /* The buffer pointer contains immediate data */
 #define TRB_IDT			(1<<6)
+/* TDs smaller than this might use IDT */

Technically, "TDs at most this" since you're 8 itself is an allowed size.

Noted

Regards,
Nicolas

#3 lore raw

Hi,

Quoted 28 lines · Nicolas Saenz Julienne
Quoted 19 lines
Quoted 19 lines
drivers/usb/host/xhci.c +3 -0
index 005e65922608..dec62f7f5dc8 100644
@@ -1238,6 +1238,21 @@ EXPORT_SYMBOL_GPL(xhci_resume);
 
 /*-------------------------------------------------------------------------
 */
 
+/*
+ * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
+ * we'll copy the actual data into the TRB address register. This is

limited to
+ * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize
+ * >= 8 bytes.
+ */

drivers/usb/host/xhci.c +2 -0
+static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
+				gfp_t mem_flags)
Quoted 8 lines

drivers/usb/host/xhci.c EXPORT_SYMBOL_GPL(xhci_resume); · line 1254

Quoted 6 lines
drivers/usb/host/xhci.c +6 -0
+{
+	if (xhci_urb_suitable_for_idt(urb))
+		return 0;
+
+	return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
+}

don't you need a matching unmap_urb_for_dma()??

Quoted 8 lines

Not really as every DMA mapping sets a matching URB flag to track it. For example when usb_hcd_map_urb_for_dma() uses dma_map_single() it will set URB_DMA_MAP_SINGLE in urb->transfer_flags, later on unmap_urb_for_dma() will catch it and unmap it. As I bypass the mapping altogether there are no flags set, so unmap_urb_for_dma() won't have any effect.

I could still add it for clarity, and well, I guess it'll save some instructions on the IDT suitable side.

thanks for the clarification.

Quoted 11 lines
Quoted 11 lines

drivers/usb/host/xhci.h enum xhci_setup_dev { · line 1298

Quoted 9 lines
drivers/usb/host/xhci.h +1 -0
index 652dc36e3012..9d77b0901ab7 100644
@@ -1295,6 +1295,8 @@ enum xhci_setup_dev {
 #define TRB_IOC			(1<<5)
 /* The buffer pointer contains immediate data */
 #define TRB_IDT			(1<<6)
+/* TDs smaller than this might use IDT */

Technically, "TDs at most this" since you're 8 itself is an allowed

heh, I made a mess on this sentence, but I guess you got the gist of it.

cheers