Skip to contentSkip to conversation

add dma noncoherent API

PATCH Superseded Xu Yang 4 participants 11 messages Jul 2, 2025 – Jul 3, 2025

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Xu Yang

author
#0 lore raw

v3 cover add dma noncoherent API

On architectures where there is no coherent caching such as ARM it's proved that using dma_alloc_noncontiguous API and handling manually the cache flushing will significantly improve performance.

Refer to:
commit 20e1dbf2bbe2 ("media: uvcvideo: Use dma_alloc_noncontiguous API") commit 68d0c3311ec1 ("media: stk1160: use dma_alloc_noncontiguous API")

However, it's obvious that there is significant code duplication between these two commits. Besides, a potential user USB Monitor may read outdated data before the driver do DMA sync for CPU which will make the data unreliable.

To reduce code duplication and avoid USB Monitor result unreliable, this series will introduce DMA noncoherent API to USB core. And the USB core layer will manage synchronization itself.

Then the last 2 patches have used the API.

I have tested uvcvideo driver. But I haven't tested stk1160 driver as I don't have such boards. @Ezequiel Garcia, @Dafna Hirschfeld do you have time to test it? Your support on this would be greatly appreciated.

Changes in v3:
- put Return section at the end of description
- correct some abbreviations
- remove usb_dma_noncoherent_sync_for_cpu() and usb_dma_noncoherent_sync_for_device()
- do DMA sync in usb_hcd_map_urb_for_dma() and usb_hcd_unmap_urb_for_dma()
- call flush_kernel_vmap_range() for OUT transfers and invalidate_kernel_vmap_range() for IN transfers

Changes in v2:
- https://lore.kernel.org/all/[email protected]/
- handle it in USB core

v1:
- https://lore.kernel.org/linux-usb/[email protected]/

Xu Yang (3):
usb: core: add dma-noncoherent buffer alloc and free API
media: uvcvideo: use usb_alloc_noncoherent/usb_free_noncoherent()
media: stk1160: use usb_alloc_noncoherent/usb_free_noncoherent()

 drivers/media/usb/stk1160/stk1160-v4l.c   |  4 --
 drivers/media/usb/stk1160/stk1160-video.c | 43 ++++--------
 drivers/media/usb/stk1160/stk1160.h       |  7 --
 drivers/media/usb/uvc/uvc_video.c         | 56 ++++------------
 drivers/usb/core/hcd.c                    | 19 ++++++
 drivers/usb/core/usb.c                    | 80 +++++++++++++++++++++++
 include/linux/usb.h                       | 11 ++++
 7 files changed, 135 insertions(+), 85 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/3 usb: core: add dma-noncoherent buffer alloc and free API

This will add usb_alloc_noncoherent() and usb_free_noncoherent() functions to support alloc and free buffer in a dma-noncoherent way.

To explicit manage the memory ownership for the kernel and device, this will also add usb_dma_noncoherent_sync_for_cpu/device() functions and call it at proper time. The management requires the user save sg_table returned by usb_alloc_noncoherent() to urb->sgt.

---
Changes in v3:
- put Return section at the end of description
- correct some abbreviations
- remove usb_dma_noncoherent_sync_for_cpu() and usb_dma_noncoherent_sync_for_device()
- do DMA sync in usb_hcd_map_urb_for_dma() and usb_hcd_unmap_urb_for_dma()
- call flush_kernel_vmap_range() for OUT transfers and invalidate_kernel_vmap_range() for IN transfers

---
 drivers/usb/core/hcd.c | 19 ++++++++++
 drivers/usb/core/usb.c | 80 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/usb.h    | 11 ++++++
 3 files changed, 110 insertions(+)
drivers/usb/core/hcd.c +19 -0
index c22de97432a0..e0fa6d6d273b 100644
@@ -1366,6 +1366,14 @@ void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
 				urb->transfer_buffer_length,
 				dir);
 
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {
+		dma_sync_sgtable_for_cpu(hcd->self.sysdev, urb->sgt, dir);
+		if (dir == DMA_FROM_DEVICE)
+			invalidate_kernel_vmap_range(urb->transfer_buffer,
+						     urb->transfer_buffer_length);
+	}
+
 	/* Make it safe to call this routine more than once */
 	urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
 			URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
@@ -1491,7 +1499,18 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 		if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
 				URB_SETUP_MAP_LOCAL)))
 			usb_hcd_unmap_urb_for_dma(hcd, urb);
+
+		return ret;
 	}
+
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {
+		if (dir == DMA_TO_DEVICE)
+			flush_kernel_vmap_range(urb->transfer_buffer,
+						urb->transfer_buffer_length);
+		dma_sync_sgtable_for_device(hcd->self.sysdev, urb->sgt, dir);
+	}
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
drivers/usb/core/usb.c +80 -0

86 lines not on this page · raw message

include/linux/usb.h +11 -0
index e8662843e68c..9ade441ab4c8 100644
@@ -1619,6 +1619,7 @@ struct urb {
 	void *transfer_buffer;		/* (in) associated data buffer */
 	dma_addr_t transfer_dma;	/* (in) dma addr for transfer_buffer */
 	struct scatterlist *sg;		/* (in) scatter gather buffer list */
+	struct sg_table *sgt;		/* (in) scatter gather table for noncoherent buffer */
 	int num_mapped_sgs;		/* (internal) mapped sg entries */
 	int num_sgs;			/* (in) number of entries in the sg list */
 	u32 transfer_buffer_length;	/* (in) data buffer length */
@@ -1824,6 +1825,16 @@ void *usb_alloc_coherent(struct usb_device *dev, size_t size,
 void usb_free_coherent(struct usb_device *dev, size_t size,
 	void *addr, dma_addr_t dma);
 
+enum dma_data_direction;
+
+void *usb_alloc_noncoherent(struct usb_device *dev, size_t size,
+			    gfp_t mem_flags, dma_addr_t *dma,
+			    enum dma_data_direction dir,
+			    struct sg_table **table);
+void usb_free_noncoherent(struct usb_device *dev, size_t size,
+			  void *addr, enum dma_data_direction dir,
+			  struct sg_table *table);
+
 /*-------------------------------------------------------------------*
  *                         SYNCHRONOUS CALL SUPPORT                  *
  *-------------------------------------------------------------------*/
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/3] usb: core: add dma-noncoherent buffer alloc and free API

Quoted 7 lines · Xu Yang

This will add usb_alloc_noncoherent() and usb_free_noncoherent() functions to support alloc and free buffer in a dma-noncoherent way.

To explicit manage the memory ownership for the kernel and device, this will also add usb_dma_noncoherent_sync_for_cpu/device() functions and call it at proper time. The management requires the user save sg_table returned by usb_alloc_noncoherent() to urb->sgt.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {

Something weird with the indentation. Here you wrapped, but the very next line is not, and they both satisfy 80 limit (even though we have it relaxed now).

Quoted 5 lines
drivers/usb/core/hcd.c +5 -0
+		dma_sync_sgtable_for_cpu(hcd->self.sysdev, urb->sgt, dir);
+		if (dir == DMA_FROM_DEVICE)
+			invalidate_kernel_vmap_range(urb->transfer_buffer,
+						     urb->transfer_buffer_length);
+	}

...

drivers/usb/core/hcd.c int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, · line 1507

Quoted 6 lines
drivers/usb/core/hcd.c +5 -0
+
+		return ret;
 	}
+
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {

Why not

	} else if {

and drop the above return ret; ?

Quoted 7 lines
drivers/usb/core/hcd.c +6 -0
+		if (dir == DMA_TO_DEVICE)
+			flush_kernel_vmap_range(urb->transfer_buffer,
+						urb->transfer_buffer_length);
+		dma_sync_sgtable_for_device(hcd->self.sysdev, urb->sgt, dir);
+	}
+
 	return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw

Re: [PATCH v3 1/3] usb: core: add dma-noncoherent buffer alloc and free API

Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Xu Yang

This will add usb_alloc_noncoherent() and usb_free_noncoherent() functions to support alloc and free buffer in a dma-noncoherent way.

To explicit manage the memory ownership for the kernel and device, this will also add usb_dma_noncoherent_sync_for_cpu/device() functions and call it at proper time. The management requires the user save sg_table returned by usb_alloc_noncoherent() to urb->sgt.

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {

Something weird with the indentation. Here you wrapped, but the very next line is not, and they both satisfy 80 limit (even though we have it relaxed now).

Indeed. Okay, I'll not wrap this line.

Quoted 18 lines
Quoted 6 lines
drivers/usb/core/hcd.c +5 -0
+		dma_sync_sgtable_for_cpu(hcd->self.sysdev, urb->sgt, dir);
+		if (dir == DMA_FROM_DEVICE)
+			invalidate_kernel_vmap_range(urb->transfer_buffer,
+						     urb->transfer_buffer_length);
+	}

...

drivers/usb/core/hcd.c int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, · line 1507

Quoted 6 lines
drivers/usb/core/hcd.c +5 -0
+
+		return ret;
 	}
+
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {

Why not

Quoted 3 lines
	} else if {

and drop the above return ret; ?

Okay.

Thanks,
Xu Yang

Quoted 14 lines
Quoted 8 lines
drivers/usb/core/hcd.c +6 -0
+		if (dir == DMA_TO_DEVICE)
+			flush_kernel_vmap_range(urb->transfer_buffer,
+						urb->transfer_buffer_length);
+		dma_sync_sgtable_for_device(hcd->self.sysdev, urb->sgt, dir);
+	}
+
 	return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v3 1/3] usb: core: add dma-noncoherent buffer alloc and free API

In reply to Xu Yang
Quoted 31 lines · Xu Yang

This will add usb_alloc_noncoherent() and usb_free_noncoherent() functions to support alloc and free buffer in a dma-noncoherent way.

To explicit manage the memory ownership for the kernel and device, this will also add usb_dma_noncoherent_sync_for_cpu/device() functions and call it at proper time. The management requires the user save sg_table returned by usb_alloc_noncoherent() to urb->sgt.

---
Changes in v3:
- put Return section at the end of description
- correct some abbreviations
- remove usb_dma_noncoherent_sync_for_cpu() and usb_dma_noncoherent_sync_for_device()
- do DMA sync in usb_hcd_map_urb_for_dma() and usb_hcd_unmap_urb_for_dma()
- call flush_kernel_vmap_range() for OUT transfers and invalidate_kernel_vmap_range() for IN transfers

---
 drivers/usb/core/hcd.c | 19 ++++++++++
 drivers/usb/core/usb.c | 80 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/usb.h    | 11 ++++++
 3 files changed, 110 insertions(+)
drivers/usb/core/hcd.c +0 -0
index c22de97432a0..e0fa6d6d273b 100644

drivers/usb/core/hcd.c void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) · line 1370

Quoted 5 lines
drivers/usb/core/hcd.c +2 -0
@@ -1366,6 +1366,14 @@ void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
 				urb->transfer_buffer_length,
 				dir);
 
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {

Shouldn't this be "else if"? There aren't any circumstances where a driver might want to do two DMA mappings for the same buffer, are there?

drivers/usb/core/hcd.c void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) · line 1376

Quoted 6 lines
drivers/usb/core/hcd.c +6 -0
+		dma_sync_sgtable_for_cpu(hcd->self.sysdev, urb->sgt, dir);
+		if (dir == DMA_FROM_DEVICE)
+			invalidate_kernel_vmap_range(urb->transfer_buffer,
+						     urb->transfer_buffer_length);
+	}
+

Alan Stern

author
#5 lore raw

Re: [PATCH v3 1/3] usb: core: add dma-noncoherent buffer alloc and free API

Quoted 35 lines · Alan Stern
Quoted 34 lines · Xu Yang

This will add usb_alloc_noncoherent() and usb_free_noncoherent() functions to support alloc and free buffer in a dma-noncoherent way.

To explicit manage the memory ownership for the kernel and device, this will also add usb_dma_noncoherent_sync_for_cpu/device() functions and call it at proper time. The management requires the user save sg_table returned by usb_alloc_noncoherent() to urb->sgt.

---
Changes in v3:
- put Return section at the end of description
- correct some abbreviations
- remove usb_dma_noncoherent_sync_for_cpu() and usb_dma_noncoherent_sync_for_device()
- do DMA sync in usb_hcd_map_urb_for_dma() and usb_hcd_unmap_urb_for_dma()
- call flush_kernel_vmap_range() for OUT transfers and invalidate_kernel_vmap_range() for IN transfers

---
 drivers/usb/core/hcd.c | 19 ++++++++++
 drivers/usb/core/usb.c | 80 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/usb.h    | 11 ++++++
 3 files changed, 110 insertions(+)
drivers/usb/core/hcd.c +0 -0
index c22de97432a0..e0fa6d6d273b 100644
@@ -1366,6 +1366,14 @@ void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
 				urb->transfer_buffer_length,
 				dir);
 
Quoted 5 lines

drivers/usb/core/hcd.c void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) · line 1370

Quoted 2 lines
drivers/usb/core/hcd.c +2 -0
+	if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) &&
+	    urb->sgt) {

Shouldn't this be "else if"? There aren't any circumstances where a driver might want to do two DMA mappings for the same buffer, are there?

Yes, right. I'll put these line to "else if".

Thanks,
Xu Yang

Quoted 9 lines

drivers/usb/core/hcd.c void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) · line 1376

Quoted 7 lines
drivers/usb/core/hcd.c +6 -0
+		dma_sync_sgtable_for_cpu(hcd->self.sysdev, urb->sgt, dir);
+		if (dir == DMA_FROM_DEVICE)
+			invalidate_kernel_vmap_range(urb->transfer_buffer,
+						     urb->transfer_buffer_length);
+	}
+

Alan Stern

author
#6 lore raw

PATCH v3 2/3 media: uvcvideo: use usb_alloc_noncoherent/usb_free_noncoherent()

In reply to Xu Yang

This will use USB noncoherent API to alloc/free urb buffers, then uvc driver needn't to do dma sync operations by itself.

---
Changes in v3:
- no changes

---
 drivers/media/usb/uvc/uvc_video.c | 56 ++++++++-----------------------
 1 file changed, 14 insertions(+), 42 deletions(-)
drivers/media/usb/uvc/uvc_video.c +14 -42
index e3567aeb0007..614cf4781221 100644
@@ -1280,15 +1280,6 @@ static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream)
 	return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
 }
 
-static int uvc_submit_urb(struct uvc_urb *uvc_urb, gfp_t mem_flags)
-{
-	/* Sync DMA. */
-	dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream),
-				    uvc_urb->sgt,
-				    uvc_stream_dir(uvc_urb->stream));
-	return usb_submit_urb(uvc_urb->urb, mem_flags);
-}
-
 /*
  * uvc_video_decode_data_work: Asynchronous memcpy processing
  *
@@ -1310,7 +1301,7 @@ static void uvc_video_copy_data_work(struct work_struct *work)
 		uvc_queue_buffer_release(op->buf);
 	}
 
-	ret = uvc_submit_urb(uvc_urb, GFP_KERNEL);
+	ret = usb_submit_urb(uvc_urb->urb, GFP_KERNEL);
 	if (ret < 0)
 		dev_err(&uvc_urb->stream->intf->dev,
 			"Failed to resubmit video URB (%d).\n", ret);
@@ -1736,12 +1727,6 @@ static void uvc_video_complete(struct urb *urb)
 	/* Re-initialise the URB async work. */
 	uvc_urb->async_operations = 0;
 
-	/* Sync DMA and invalidate vmap range. */
-	dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream),
-				 uvc_urb->sgt, uvc_stream_dir(stream));
-	invalidate_kernel_vmap_range(uvc_urb->buffer,
-				     uvc_urb->stream->urb_size);
-
 	/*
 	 * Process the URB headers, and optionally queue expensive memcpy tasks
 	 * to be deferred to a work queue.
@@ -1750,7 +1735,7 @@ static void uvc_video_complete(struct urb *urb)
 
 	/* If no async work is needed, resubmit the URB immediately. */
 	if (!uvc_urb->async_operations) {
-		ret = uvc_submit_urb(uvc_urb, GFP_ATOMIC);
+		ret = usb_submit_urb(uvc_urb->urb, GFP_ATOMIC);
 		if (ret < 0)
 			dev_err(&stream->intf->dev,
 				"Failed to resubmit video URB (%d).\n", ret);
@@ -1765,17 +1750,15 @@ static void uvc_video_complete(struct urb *urb)
  */
 static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 {
-	struct device *dma_dev = uvc_stream_to_dmadev(stream);
+	struct usb_device *udev = stream->dev->udev;
 	struct uvc_urb *uvc_urb;
 
 	for_each_uvc_urb(uvc_urb, stream) {
 		if (!uvc_urb->buffer)
 			continue;
 
-		dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer);
-		dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt,
-				       uvc_stream_dir(stream));
-
+		usb_free_noncoherent(udev, stream->urb_size, uvc_urb->buffer,
+				     uvc_stream_dir(stream), uvc_urb->sgt);
 		uvc_urb->buffer = NULL;
 		uvc_urb->sgt = NULL;
 	}
@@ -1786,26 +1769,13 @@ static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream,
 				 struct uvc_urb *uvc_urb, gfp_t gfp_flags)
 {
-	struct device *dma_dev = uvc_stream_to_dmadev(stream);
-
-	uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size,
-					       uvc_stream_dir(stream),
-					       gfp_flags, 0);
-	if (!uvc_urb->sgt)
-		return false;
-	uvc_urb->dma = uvc_urb->sgt->sgl->dma_address;
-
-	uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size,
-						 uvc_urb->sgt);
-	if (!uvc_urb->buffer) {
-		dma_free_noncontiguous(dma_dev, stream->urb_size,
-				       uvc_urb->sgt,
-				       uvc_stream_dir(stream));
-		uvc_urb->sgt = NULL;
-		return false;
-	}
+	struct usb_device *udev = stream->dev->udev;
 
-	return true;
+	uvc_urb->buffer = usb_alloc_noncoherent(udev, stream->urb_size,
+						gfp_flags, &uvc_urb->dma,
+						uvc_stream_dir(stream),
+						&uvc_urb->sgt);
+	return !!uvc_urb->buffer;
 }
 
 /*
@@ -1953,6 +1923,7 @@ static int uvc_init_video_isoc(struct uvc_streaming *stream,
 		urb->complete = uvc_video_complete;
 		urb->number_of_packets = npackets;
 		urb->transfer_buffer_length = size;
+		urb->sgt = uvc_urb->sgt;
 
 		for (i = 0; i < npackets; ++i) {
 			urb->iso_frame_desc[i].offset = i * psize;
@@ -2009,6 +1980,7 @@ static int uvc_init_video_bulk(struct uvc_streaming *stream,
 				  size, uvc_video_complete, uvc_urb);
 		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
 		urb->transfer_dma = uvc_urb->dma;
+		urb->sgt = uvc_urb->sgt;
 
 		uvc_urb->urb = urb;
 	}
@@ -2120,7 +2092,7 @@ static int uvc_video_start_transfer(struct uvc_streaming *stream,
 
 	/* Submit the URBs. */
 	for_each_uvc_urb(uvc_urb, stream) {
-		ret = uvc_submit_urb(uvc_urb, gfp_flags);
+		ret = usb_submit_urb(uvc_urb->urb, gfp_flags);
 		if (ret < 0) {
 			dev_err(&stream->intf->dev,
 				"Failed to submit URB %u (%d).\n",
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v3 2/3] media: uvcvideo: use usb_alloc_noncoherent/usb_free_noncoherent()

Hi Xu

The code looks much cleaner :)

It seems that the hcd.c uses the urb transfer_flags to know the direction of the transfer.
But the uvc driver is not setting it, you probably want to set it.

Quoted 18 lines · Xu Yang

This will use USB noncoherent API to alloc/free urb buffers, then uvc driver needn't to do dma sync operations by itself.

---
Changes in v3:
- no changes

---
 drivers/media/usb/uvc/uvc_video.c | 56 ++++++++-----------------------
 1 file changed, 14 insertions(+), 42 deletions(-)
drivers/media/usb/uvc/uvc_video.c +0 -0
index e3567aeb0007..614cf4781221 100644

drivers/media/usb/uvc/uvc_video.c static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream) · line 1281

Quoted 2 lines
drivers/media/usb/uvc/uvc_video.c +0 -0
@@ -1280,15 +1280,6 @@ static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream)
        return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
 }

The uvc_stream_to_dmadev() function is not used anymore, please drop it.

Quoted 124 lines
drivers/media/usb/uvc/uvc_video.c +14 -43
-static int uvc_submit_urb(struct uvc_urb *uvc_urb, gfp_t mem_flags)
-{
-       /* Sync DMA. */
-       dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream),
-                                   uvc_urb->sgt,
-                                   uvc_stream_dir(uvc_urb->stream));
-       return usb_submit_urb(uvc_urb->urb, mem_flags);
-}
-
 /*
  * uvc_video_decode_data_work: Asynchronous memcpy processing
  *
@@ -1310,7 +1301,7 @@ static void uvc_video_copy_data_work(struct work_struct *work)
                uvc_queue_buffer_release(op->buf);
        }
 
-       ret = uvc_submit_urb(uvc_urb, GFP_KERNEL);
+       ret = usb_submit_urb(uvc_urb->urb, GFP_KERNEL);
        if (ret < 0)
                dev_err(&uvc_urb->stream->intf->dev,
                        "Failed to resubmit video URB (%d).\n", ret);
@@ -1736,12 +1727,6 @@ static void uvc_video_complete(struct urb *urb)
        /* Re-initialise the URB async work. */
        uvc_urb->async_operations = 0;
 
-       /* Sync DMA and invalidate vmap range. */
-       dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream),
-                                uvc_urb->sgt, uvc_stream_dir(stream));
-       invalidate_kernel_vmap_range(uvc_urb->buffer,
-                                    uvc_urb->stream->urb_size);
-
        /*
         * Process the URB headers, and optionally queue expensive memcpy tasks
         * to be deferred to a work queue.
@@ -1750,7 +1735,7 @@ static void uvc_video_complete(struct urb *urb)
 
        /* If no async work is needed, resubmit the URB immediately. */
        if (!uvc_urb->async_operations) {
-               ret = uvc_submit_urb(uvc_urb, GFP_ATOMIC);
+               ret = usb_submit_urb(uvc_urb->urb, GFP_ATOMIC);
                if (ret < 0)
                        dev_err(&stream->intf->dev,
                                "Failed to resubmit video URB (%d).\n", ret);
@@ -1765,17 +1750,15 @@ static void uvc_video_complete(struct urb *urb)
  */
 static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 {
-       struct device *dma_dev = uvc_stream_to_dmadev(stream);
+       struct usb_device *udev = stream->dev->udev;
        struct uvc_urb *uvc_urb;
 
        for_each_uvc_urb(uvc_urb, stream) {
                if (!uvc_urb->buffer)
                        continue;
 
-               dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer);
-               dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt,
-                                      uvc_stream_dir(stream));
-
+               usb_free_noncoherent(udev, stream->urb_size, uvc_urb->buffer,
+                                    uvc_stream_dir(stream), uvc_urb->sgt);
                uvc_urb->buffer = NULL;
                uvc_urb->sgt = NULL;
        }
@@ -1786,26 +1769,13 @@ static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream,
                                 struct uvc_urb *uvc_urb, gfp_t gfp_flags)
 {
-       struct device *dma_dev = uvc_stream_to_dmadev(stream);
-
-       uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size,
-                                              uvc_stream_dir(stream),
-                                              gfp_flags, 0);
-       if (!uvc_urb->sgt)
-               return false;
-       uvc_urb->dma = uvc_urb->sgt->sgl->dma_address;
-
-       uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size,
-                                                uvc_urb->sgt);
-       if (!uvc_urb->buffer) {
-               dma_free_noncontiguous(dma_dev, stream->urb_size,
-                                      uvc_urb->sgt,
-                                      uvc_stream_dir(stream));
-               uvc_urb->sgt = NULL;
-               return false;
-       }
+       struct usb_device *udev = stream->dev->udev;
 
-       return true;
+       uvc_urb->buffer = usb_alloc_noncoherent(udev, stream->urb_size,
+                                               gfp_flags, &uvc_urb->dma,
+                                               uvc_stream_dir(stream),
+                                               &uvc_urb->sgt);
+       return !!uvc_urb->buffer;
 }
 
 /*
@@ -1953,6 +1923,7 @@ static int uvc_init_video_isoc(struct uvc_streaming *stream,
                urb->complete = uvc_video_complete;
                urb->number_of_packets = npackets;
                urb->transfer_buffer_length = size;
+               urb->sgt = uvc_urb->sgt;
 
                for (i = 0; i < npackets; ++i) {
                        urb->iso_frame_desc[i].offset = i * psize;
@@ -2009,6 +1980,7 @@ static int uvc_init_video_bulk(struct uvc_streaming *stream,
                                  size, uvc_video_complete, uvc_urb);
                urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
                urb->transfer_dma = uvc_urb->dma;
+               urb->sgt = uvc_urb->sgt;
 
                uvc_urb->urb = urb;
        }
@@ -2120,7 +2092,7 @@ static int uvc_video_start_transfer(struct uvc_streaming *stream,
 
        /* Submit the URBs. */
        for_each_uvc_urb(uvc_urb, stream) {
-               ret = uvc_submit_urb(uvc_urb, gfp_flags);
+               ret = usb_submit_urb(uvc_urb->urb, gfp_flags);
                if (ret < 0) {
                        dev_err(&stream->intf->dev,
                                "Failed to submit URB %u (%d).\n",
--
Quoted 3 lines

2.34.1

--
Ricardo Ribalda

author
#8 lore raw

Re: [PATCH v3 2/3] media: uvcvideo: use usb_alloc_noncoherent/usb_free_noncoherent()

Hi Ricardo,

Quoted 7 lines · Ricardo Ribalda

Hi Xu

The code looks much cleaner :)

It seems that the hcd.c uses the urb transfer_flags to know the direction of the transfer.
But the uvc driver is not setting it, you probably want to set it.

The USB HCD will get transfer direction from endpoint capability. So we needn't add such info to transfer_flags.

Quoted 15 lines · Xu Yang
Quoted 13 lines

This will use USB noncoherent API to alloc/free urb buffers, then uvc driver needn't to do dma sync operations by itself.

---
Changes in v3:
- no changes

---
 drivers/media/usb/uvc/uvc_video.c | 56 ++++++++-----------------------
 1 file changed, 14 insertions(+), 42 deletions(-)
Quoted 9 lines

drivers/media/usb/uvc/uvc_video.c static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream) · line 1281

Quoted 7 lines
drivers/media/usb/uvc/uvc_video.c +0 -0
index e3567aeb0007..614cf4781221 100644
@@ -1280,15 +1280,6 @@ static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream)
        return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
 }

The uvc_stream_to_dmadev() function is not used anymore, please drop it.

Sure.

Thanks,
Xu Yang

Quoted 134 lines
Quoted 127 lines
drivers/media/usb/uvc/uvc_video.c +14 -43
-static int uvc_submit_urb(struct uvc_urb *uvc_urb, gfp_t mem_flags)
-{
-       /* Sync DMA. */
-       dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream),
-                                   uvc_urb->sgt,
-                                   uvc_stream_dir(uvc_urb->stream));
-       return usb_submit_urb(uvc_urb->urb, mem_flags);
-}
-
 /*
  * uvc_video_decode_data_work: Asynchronous memcpy processing
  *
@@ -1310,7 +1301,7 @@ static void uvc_video_copy_data_work(struct work_struct *work)
                uvc_queue_buffer_release(op->buf);
        }
 
-       ret = uvc_submit_urb(uvc_urb, GFP_KERNEL);
+       ret = usb_submit_urb(uvc_urb->urb, GFP_KERNEL);
        if (ret < 0)
                dev_err(&uvc_urb->stream->intf->dev,
                        "Failed to resubmit video URB (%d).\n", ret);
@@ -1736,12 +1727,6 @@ static void uvc_video_complete(struct urb *urb)
        /* Re-initialise the URB async work. */
        uvc_urb->async_operations = 0;
 
-       /* Sync DMA and invalidate vmap range. */
-       dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream),
-                                uvc_urb->sgt, uvc_stream_dir(stream));
-       invalidate_kernel_vmap_range(uvc_urb->buffer,
-                                    uvc_urb->stream->urb_size);
-
        /*
         * Process the URB headers, and optionally queue expensive memcpy tasks
         * to be deferred to a work queue.
@@ -1750,7 +1735,7 @@ static void uvc_video_complete(struct urb *urb)
 
        /* If no async work is needed, resubmit the URB immediately. */
        if (!uvc_urb->async_operations) {
-               ret = uvc_submit_urb(uvc_urb, GFP_ATOMIC);
+               ret = usb_submit_urb(uvc_urb->urb, GFP_ATOMIC);
                if (ret < 0)
                        dev_err(&stream->intf->dev,
                                "Failed to resubmit video URB (%d).\n", ret);
@@ -1765,17 +1750,15 @@ static void uvc_video_complete(struct urb *urb)
  */
 static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 {
-       struct device *dma_dev = uvc_stream_to_dmadev(stream);
+       struct usb_device *udev = stream->dev->udev;
        struct uvc_urb *uvc_urb;
 
        for_each_uvc_urb(uvc_urb, stream) {
                if (!uvc_urb->buffer)
                        continue;
 
-               dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer);
-               dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt,
-                                      uvc_stream_dir(stream));
-
+               usb_free_noncoherent(udev, stream->urb_size, uvc_urb->buffer,
+                                    uvc_stream_dir(stream), uvc_urb->sgt);
                uvc_urb->buffer = NULL;
                uvc_urb->sgt = NULL;
        }
@@ -1786,26 +1769,13 @@ static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream,
                                 struct uvc_urb *uvc_urb, gfp_t gfp_flags)
 {
-       struct device *dma_dev = uvc_stream_to_dmadev(stream);
-
-       uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size,
-                                              uvc_stream_dir(stream),
-                                              gfp_flags, 0);
-       if (!uvc_urb->sgt)
-               return false;
-       uvc_urb->dma = uvc_urb->sgt->sgl->dma_address;
-
-       uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size,
-                                                uvc_urb->sgt);
-       if (!uvc_urb->buffer) {
-               dma_free_noncontiguous(dma_dev, stream->urb_size,
-                                      uvc_urb->sgt,
-                                      uvc_stream_dir(stream));
-               uvc_urb->sgt = NULL;
-               return false;
-       }
+       struct usb_device *udev = stream->dev->udev;
 
-       return true;
+       uvc_urb->buffer = usb_alloc_noncoherent(udev, stream->urb_size,
+                                               gfp_flags, &uvc_urb->dma,
+                                               uvc_stream_dir(stream),
+                                               &uvc_urb->sgt);
+       return !!uvc_urb->buffer;
 }
 
 /*
@@ -1953,6 +1923,7 @@ static int uvc_init_video_isoc(struct uvc_streaming *stream,
                urb->complete = uvc_video_complete;
                urb->number_of_packets = npackets;
                urb->transfer_buffer_length = size;
+               urb->sgt = uvc_urb->sgt;
 
                for (i = 0; i < npackets; ++i) {
                        urb->iso_frame_desc[i].offset = i * psize;
@@ -2009,6 +1980,7 @@ static int uvc_init_video_bulk(struct uvc_streaming *stream,
                                  size, uvc_video_complete, uvc_urb);
                urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
                urb->transfer_dma = uvc_urb->dma;
+               urb->sgt = uvc_urb->sgt;
 
                uvc_urb->urb = urb;
        }
@@ -2120,7 +2092,7 @@ static int uvc_video_start_transfer(struct uvc_streaming *stream,
 
        /* Submit the URBs. */
        for_each_uvc_urb(uvc_urb, stream) {
-               ret = uvc_submit_urb(uvc_urb, gfp_flags);
+               ret = usb_submit_urb(uvc_urb->urb, gfp_flags);
                if (ret < 0) {
                        dev_err(&stream->intf->dev,
                                "Failed to submit URB %u (%d).\n",
--
Quoted 3 lines

2.34.1

--
Ricardo Ribalda

#9 lore raw

Re: [PATCH v3 2/3] media: uvcvideo: use usb_alloc_noncoherent/usb_free_noncoherent()

Hi Xu

Quoted 6 lines · Xu Yang

Hi Ricardo,

Quoted 2 lines · Ricardo Ribalda

Hi Xu

Quoted 8 lines
Quoted 5 lines

The code looks much cleaner :)

It seems that the hcd.c uses the urb transfer_flags to know the direction of the transfer.
But the uvc driver is not setting it, you probably want to set it.

The USB HCD will get transfer direction from endpoint capability. So we needn't add such info to transfer_flags.

For future me...

usb_submit_urb() set that flag. :)

Thanks!

Quoted 165 lines
Quoted 16 lines · Xu Yang
Quoted 13 lines

This will use USB noncoherent API to alloc/free urb buffers, then uvc driver needn't to do dma sync operations by itself.

---
Changes in v3:
- no changes

---
 drivers/media/usb/uvc/uvc_video.c | 56 ++++++++-----------------------
 1 file changed, 14 insertions(+), 42 deletions(-)
Quoted 9 lines

drivers/media/usb/uvc/uvc_video.c static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream) · line 1281

Quoted 7 lines
drivers/media/usb/uvc/uvc_video.c +0 -0
index e3567aeb0007..614cf4781221 100644
@@ -1280,15 +1280,6 @@ static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream)
        return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
 }

The uvc_stream_to_dmadev() function is not used anymore, please drop it.

Sure.

Thanks,
Xu Yang

Quoted 134 lines
Quoted 127 lines
drivers/media/usb/uvc/uvc_video.c +14 -43
-static int uvc_submit_urb(struct uvc_urb *uvc_urb, gfp_t mem_flags)
-{
-       /* Sync DMA. */
-       dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream),
-                                   uvc_urb->sgt,
-                                   uvc_stream_dir(uvc_urb->stream));
-       return usb_submit_urb(uvc_urb->urb, mem_flags);
-}
-
 /*
  * uvc_video_decode_data_work: Asynchronous memcpy processing
  *
@@ -1310,7 +1301,7 @@ static void uvc_video_copy_data_work(struct work_struct *work)
                uvc_queue_buffer_release(op->buf);
        }
 
-       ret = uvc_submit_urb(uvc_urb, GFP_KERNEL);
+       ret = usb_submit_urb(uvc_urb->urb, GFP_KERNEL);
        if (ret < 0)
                dev_err(&uvc_urb->stream->intf->dev,
                        "Failed to resubmit video URB (%d).\n", ret);
@@ -1736,12 +1727,6 @@ static void uvc_video_complete(struct urb *urb)
        /* Re-initialise the URB async work. */
        uvc_urb->async_operations = 0;
 
-       /* Sync DMA and invalidate vmap range. */
-       dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream),
-                                uvc_urb->sgt, uvc_stream_dir(stream));
-       invalidate_kernel_vmap_range(uvc_urb->buffer,
-                                    uvc_urb->stream->urb_size);
-
        /*
         * Process the URB headers, and optionally queue expensive memcpy tasks
         * to be deferred to a work queue.
@@ -1750,7 +1735,7 @@ static void uvc_video_complete(struct urb *urb)
 
        /* If no async work is needed, resubmit the URB immediately. */
        if (!uvc_urb->async_operations) {
-               ret = uvc_submit_urb(uvc_urb, GFP_ATOMIC);
+               ret = usb_submit_urb(uvc_urb->urb, GFP_ATOMIC);
                if (ret < 0)
                        dev_err(&stream->intf->dev,
                                "Failed to resubmit video URB (%d).\n", ret);
@@ -1765,17 +1750,15 @@ static void uvc_video_complete(struct urb *urb)
  */
 static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 {
-       struct device *dma_dev = uvc_stream_to_dmadev(stream);
+       struct usb_device *udev = stream->dev->udev;
        struct uvc_urb *uvc_urb;
 
        for_each_uvc_urb(uvc_urb, stream) {
                if (!uvc_urb->buffer)
                        continue;
 
-               dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer);
-               dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt,
-                                      uvc_stream_dir(stream));
-
+               usb_free_noncoherent(udev, stream->urb_size, uvc_urb->buffer,
+                                    uvc_stream_dir(stream), uvc_urb->sgt);
                uvc_urb->buffer = NULL;
                uvc_urb->sgt = NULL;
        }
@@ -1786,26 +1769,13 @@ static void uvc_free_urb_buffers(struct uvc_streaming *stream)
 static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream,
                                 struct uvc_urb *uvc_urb, gfp_t gfp_flags)
 {
-       struct device *dma_dev = uvc_stream_to_dmadev(stream);
-
-       uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size,
-                                              uvc_stream_dir(stream),
-                                              gfp_flags, 0);
-       if (!uvc_urb->sgt)
-               return false;
-       uvc_urb->dma = uvc_urb->sgt->sgl->dma_address;
-
-       uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size,
-                                                uvc_urb->sgt);
-       if (!uvc_urb->buffer) {
-               dma_free_noncontiguous(dma_dev, stream->urb_size,
-                                      uvc_urb->sgt,
-                                      uvc_stream_dir(stream));
-               uvc_urb->sgt = NULL;
-               return false;
-       }
+       struct usb_device *udev = stream->dev->udev;
 
-       return true;
+       uvc_urb->buffer = usb_alloc_noncoherent(udev, stream->urb_size,
+                                               gfp_flags, &uvc_urb->dma,
+                                               uvc_stream_dir(stream),
+                                               &uvc_urb->sgt);
+       return !!uvc_urb->buffer;
 }
 
 /*
@@ -1953,6 +1923,7 @@ static int uvc_init_video_isoc(struct uvc_streaming *stream,
                urb->complete = uvc_video_complete;
                urb->number_of_packets = npackets;
                urb->transfer_buffer_length = size;
+               urb->sgt = uvc_urb->sgt;
 
                for (i = 0; i < npackets; ++i) {
                        urb->iso_frame_desc[i].offset = i * psize;
@@ -2009,6 +1980,7 @@ static int uvc_init_video_bulk(struct uvc_streaming *stream,
                                  size, uvc_video_complete, uvc_urb);
                urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
                urb->transfer_dma = uvc_urb->dma;
+               urb->sgt = uvc_urb->sgt;
 
                uvc_urb->urb = urb;
        }
@@ -2120,7 +2092,7 @@ static int uvc_video_start_transfer(struct uvc_streaming *stream,
 
        /* Submit the URBs. */
        for_each_uvc_urb(uvc_urb, stream) {
-               ret = uvc_submit_urb(uvc_urb, gfp_flags);
+               ret = usb_submit_urb(uvc_urb->urb, gfp_flags);
                if (ret < 0) {
                        dev_err(&stream->intf->dev,
                                "Failed to submit URB %u (%d).\n",
--
Quoted 3 lines

2.34.1

--
Ricardo Ribalda

Signature · 1 line
Ricardo Ribalda
author
#10 lore raw

PATCH v3 3/3 media: stk1160: use usb_alloc_noncoherent/usb_free_noncoherent()

In reply to Xu Yang

This will use USB noncoherent API to alloc/free urb buffers, then stk1160 driver needn't to do dma sync operations by itself.

---
Changes in v3:
- no changes

---
 drivers/media/usb/stk1160/stk1160-v4l.c   |  4 ---
 drivers/media/usb/stk1160/stk1160-video.c | 43 ++++++-----------------
 drivers/media/usb/stk1160/stk1160.h       |  7 ----
 3 files changed, 11 insertions(+), 43 deletions(-)
drivers/media/usb/stk1160/stk1160-v4l.c +0 -4
index 5ba3d9c4b3fb..715ce1dcb304 100644
@@ -232,10 +232,6 @@ static int stk1160_start_streaming(struct stk1160 *dev)
 
 	/* submit urbs and enables IRQ */
 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
-		struct stk1160_urb *stk_urb = &dev->isoc_ctl.urb_ctl[i];
-
-		dma_sync_sgtable_for_device(stk1160_get_dmadev(dev), stk_urb->sgt,
-					    DMA_FROM_DEVICE);
 		rc = usb_submit_urb(dev->isoc_ctl.urb_ctl[i].urb, GFP_KERNEL);
 		if (rc) {
 			stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
drivers/media/usb/stk1160/stk1160-video.c +11 -32

92 lines not on this page · raw message

drivers/media/usb/stk1160/stk1160.h +0 -7
index 7b498d14ed7a..4cbcb0a03bab 100644
@@ -16,8 +16,6 @@
 #include <media/videobuf2-v4l2.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ctrls.h>
-#include <linux/usb.h>
-#include <linux/usb/hcd.h>
 
 #define STK1160_VERSION		"0.9.5"
 #define STK1160_VERSION_NUM	0x000905
@@ -195,8 +193,3 @@ void stk1160_select_input(struct stk1160 *dev);
 
 /* Provided by stk1160-ac97.c */
 void stk1160_ac97_setup(struct stk1160 *dev);
-
-static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
-{
-	return bus_to_hcd(dev->udev->bus)->self.sysdev;
-}
Signature · 1 line
2.34.1