Xuan Zhuo
2023-Feb-14 07:27 UTC
[PATCH vhost 08/10] virtio_ring: introduce dma sync api for virtio
These API has been introduced:
* virtio_dma_need_sync
* virtio_dma_sync_single_range_for_cpu
* virtio_dma_sync_single_range_for_device
These APIs can be used together with the premapped mechanism to sync the
DMA address.
Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 70 ++++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 8 +++++
2 files changed, 78 insertions(+)
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 855338609c7f..84129b8c3e2a 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -3264,4 +3264,74 @@ void virtio_dma_unmap(struct device *dev, dma_addr_t dma,
unsigned int length,
}
EXPORT_SYMBOL_GPL(virtio_dma_unmap);
+/**
+ * virtio_dma_need_sync - check a dma address needs sync
+ * @dev: virtio device
+ * @addr: DMA address
+ *
+ * This API is only for pre-mapped buffers, for non premapped buffers virtio
+ * core handles DMA API internally.
+ */
+bool virtio_dma_need_sync(struct device *dev, dma_addr_t addr)
+{
+ struct virtio_device *vdev = dev_to_virtio(dev);
+
+ if (!vring_use_dma_api(vdev))
+ return 0;
+
+ return dma_need_sync(vdev->dev.parent, addr);
+}
+EXPORT_SYMBOL_GPL(virtio_dma_need_sync);
+
+/**
+ * virtio_dma_sync_single_range_for_cpu - dma sync for cpu
+ * @dev: virtio device
+ * @addr: DMA address
+ * @offset: DMA address offset
+ * @size: mem size for sync
+ * @dir: DMA direction
+ *
+ * Before calling this function, use virtio_dma_need_sync() to confirm that the
+ * DMA address really needs to be synchronized
+ *
+ * This API is only for pre-mapped buffers, for non premapped buffers virtio
+ * core handles DMA API internally.
+ */
+void virtio_dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t addr,
+ unsigned long offset, size_t size,
+ enum dma_data_direction dir)
+{
+ struct virtio_device *vdev = dev_to_virtio(dev);
+
+ dma_sync_single_range_for_cpu(vdev->dev.parent, addr, offset,
+ size, DMA_BIDIRECTIONAL);
+}
+EXPORT_SYMBOL_GPL(virtio_dma_sync_single_range_for_cpu);
+
+/**
+ * virtio_dma_sync_single_range_for_device - dma sync for device
+ * @dev: virtio device
+ * @addr: DMA address
+ * @offset: DMA address offset
+ * @size: mem size for sync
+ * @dir: DMA direction
+ *
+ * Before calling this function, use virtio_dma_need_sync() to confirm that the
+ * DMA address really needs to be synchronized
+ *
+ * This API is only for pre-mapped buffers, for non premapped buffers virtio
+ * core handles DMA API internally.
+ */
+void virtio_dma_sync_single_range_for_device(struct device *dev,
+ dma_addr_t addr,
+ unsigned long offset, size_t size,
+ enum dma_data_direction dir)
+{
+ struct virtio_device *vdev = dev_to_virtio(dev);
+
+ dma_sync_single_range_for_device(vdev->dev.parent, addr, offset,
+ size, DMA_BIDIRECTIONAL);
+}
+EXPORT_SYMBOL_GPL(virtio_dma_sync_single_range_for_device);
+
MODULE_LICENSE("GPL");
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index b5fa71476737..d0e707d744a0 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -225,4 +225,12 @@ dma_addr_t virtio_dma_map(struct device *dev, void *addr,
unsigned int length,
int virtio_dma_mapping_error(struct device *dev, dma_addr_t addr);
void virtio_dma_unmap(struct device *dev, dma_addr_t dma, unsigned int length,
enum dma_data_direction dir);
+bool virtio_dma_need_sync(struct device *dev, dma_addr_t addr);
+void virtio_dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t addr,
+ unsigned long offset, size_t size,
+ enum dma_data_direction dir);
+void virtio_dma_sync_single_range_for_device(struct device *dev,
+ dma_addr_t addr,
+ unsigned long offset, size_t size,
+ enum dma_data_direction dir);
#endif /* _LINUX_VIRTIO_H */
--
2.32.0.3.g01195cf9f
Jason Wang
2023-Feb-20 05:38 UTC
[PATCH vhost 08/10] virtio_ring: introduce dma sync api for virtio
On Tue, Feb 14, 2023 at 3:27 PM Xuan Zhuo <xuanzhuo at linux.alibaba.com> wrote:> > These API has been introduced: > > * virtio_dma_need_sync > * virtio_dma_sync_single_range_for_cpu > * virtio_dma_sync_single_range_for_deviceWhat's the advantages of exporting internal logic like virtio_dma_need_sync() over hiding it in virtio_dma_sync_single_range_for_cpu() and virtio_dma_sync_single_range_for_device()? Thanks> > These APIs can be used together with the premapped mechanism to sync the > DMA address. > > Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com> > --- > drivers/virtio/virtio_ring.c | 70 ++++++++++++++++++++++++++++++++++++ > include/linux/virtio.h | 8 +++++ > 2 files changed, 78 insertions(+) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index 855338609c7f..84129b8c3e2a 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -3264,4 +3264,74 @@ void virtio_dma_unmap(struct device *dev, dma_addr_t dma, unsigned int length, > } > EXPORT_SYMBOL_GPL(virtio_dma_unmap); > > +/** > + * virtio_dma_need_sync - check a dma address needs sync > + * @dev: virtio device > + * @addr: DMA address > + * > + * This API is only for pre-mapped buffers, for non premapped buffers virtio > + * core handles DMA API internally. > + */ > +bool virtio_dma_need_sync(struct device *dev, dma_addr_t addr) > +{ > + struct virtio_device *vdev = dev_to_virtio(dev); > + > + if (!vring_use_dma_api(vdev)) > + return 0; > + > + return dma_need_sync(vdev->dev.parent, addr); > +} > +EXPORT_SYMBOL_GPL(virtio_dma_need_sync); > + > +/** > + * virtio_dma_sync_single_range_for_cpu - dma sync for cpu > + * @dev: virtio device > + * @addr: DMA address > + * @offset: DMA address offset > + * @size: mem size for sync > + * @dir: DMA direction > + * > + * Before calling this function, use virtio_dma_need_sync() to confirm that the > + * DMA address really needs to be synchronized > + * > + * This API is only for pre-mapped buffers, for non premapped buffers virtio > + * core handles DMA API internally. > + */ > +void virtio_dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t addr, > + unsigned long offset, size_t size, > + enum dma_data_direction dir) > +{ > + struct virtio_device *vdev = dev_to_virtio(dev); > + > + dma_sync_single_range_for_cpu(vdev->dev.parent, addr, offset, > + size, DMA_BIDIRECTIONAL); > +} > +EXPORT_SYMBOL_GPL(virtio_dma_sync_single_range_for_cpu); > + > +/** > + * virtio_dma_sync_single_range_for_device - dma sync for device > + * @dev: virtio device > + * @addr: DMA address > + * @offset: DMA address offset > + * @size: mem size for sync > + * @dir: DMA direction > + * > + * Before calling this function, use virtio_dma_need_sync() to confirm that the > + * DMA address really needs to be synchronized > + * > + * This API is only for pre-mapped buffers, for non premapped buffers virtio > + * core handles DMA API internally. > + */ > +void virtio_dma_sync_single_range_for_device(struct device *dev, > + dma_addr_t addr, > + unsigned long offset, size_t size, > + enum dma_data_direction dir) > +{ > + struct virtio_device *vdev = dev_to_virtio(dev); > + > + dma_sync_single_range_for_device(vdev->dev.parent, addr, offset, > + size, DMA_BIDIRECTIONAL); > +} > +EXPORT_SYMBOL_GPL(virtio_dma_sync_single_range_for_device); > + > MODULE_LICENSE("GPL"); > diff --git a/include/linux/virtio.h b/include/linux/virtio.h > index b5fa71476737..d0e707d744a0 100644 > --- a/include/linux/virtio.h > +++ b/include/linux/virtio.h > @@ -225,4 +225,12 @@ dma_addr_t virtio_dma_map(struct device *dev, void *addr, unsigned int length, > int virtio_dma_mapping_error(struct device *dev, dma_addr_t addr); > void virtio_dma_unmap(struct device *dev, dma_addr_t dma, unsigned int length, > enum dma_data_direction dir); > +bool virtio_dma_need_sync(struct device *dev, dma_addr_t addr); > +void virtio_dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t addr, > + unsigned long offset, size_t size, > + enum dma_data_direction dir); > +void virtio_dma_sync_single_range_for_device(struct device *dev, > + dma_addr_t addr, > + unsigned long offset, size_t size, > + enum dma_data_direction dir); > #endif /* _LINUX_VIRTIO_H */ > -- > 2.32.0.3.g01195cf9f >