Commit b319940f authored by Xuan Zhuo's avatar Xuan Zhuo Committed by Michael S. Tsirkin

virtio_ring: skip unmap for premapped

Now we add a case where we skip dma unmap, the vq->premapped is true.

We can't just rely on use_dma_api to determine whether to skip the dma
operation. For convenience, I introduced the "do_unmap". By default, it
is the same as use_dma_api. If the driver is configured with premapped,
then do_unmap is false.

So as long as do_unmap is false, for addr of desc, we should skip dma
unmap operation.
Signed-off-by: default avatarXuan Zhuo <xuanzhuo@linux.alibaba.com>
Message-Id: <20230810123057.43407-7-xuanzhuo@linux.alibaba.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 2df64759
...@@ -175,6 +175,11 @@ struct vring_virtqueue { ...@@ -175,6 +175,11 @@ struct vring_virtqueue {
/* Do DMA mapping by driver */ /* Do DMA mapping by driver */
bool premapped; bool premapped;
/* Do unmap or not for desc. Just when premapped is False and
* use_dma_api is true, this is true.
*/
bool do_unmap;
/* Head of free buffer list. */ /* Head of free buffer list. */
unsigned int free_head; unsigned int free_head;
/* Number we've added since last sync. */ /* Number we've added since last sync. */
...@@ -440,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq, ...@@ -440,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
{ {
u16 flags; u16 flags;
if (!vq->use_dma_api) if (!vq->do_unmap)
return; return;
flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
...@@ -458,18 +463,21 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, ...@@ -458,18 +463,21 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
struct vring_desc_extra *extra = vq->split.desc_extra; struct vring_desc_extra *extra = vq->split.desc_extra;
u16 flags; u16 flags;
if (!vq->use_dma_api)
goto out;
flags = extra[i].flags; flags = extra[i].flags;
if (flags & VRING_DESC_F_INDIRECT) { if (flags & VRING_DESC_F_INDIRECT) {
if (!vq->use_dma_api)
goto out;
dma_unmap_single(vring_dma_dev(vq), dma_unmap_single(vring_dma_dev(vq),
extra[i].addr, extra[i].addr,
extra[i].len, extra[i].len,
(flags & VRING_DESC_F_WRITE) ? (flags & VRING_DESC_F_WRITE) ?
DMA_FROM_DEVICE : DMA_TO_DEVICE); DMA_FROM_DEVICE : DMA_TO_DEVICE);
} else { } else {
if (!vq->do_unmap)
goto out;
dma_unmap_page(vring_dma_dev(vq), dma_unmap_page(vring_dma_dev(vq),
extra[i].addr, extra[i].addr,
extra[i].len, extra[i].len,
...@@ -635,7 +643,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, ...@@ -635,7 +643,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
} }
/* Last one doesn't continue. */ /* Last one doesn't continue. */
desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
if (!indirect && vq->use_dma_api) if (!indirect && vq->do_unmap)
vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &= vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
~VRING_DESC_F_NEXT; ~VRING_DESC_F_NEXT;
...@@ -794,7 +802,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, ...@@ -794,7 +802,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
VRING_DESC_F_INDIRECT)); VRING_DESC_F_INDIRECT));
BUG_ON(len == 0 || len % sizeof(struct vring_desc)); BUG_ON(len == 0 || len % sizeof(struct vring_desc));
if (vq->use_dma_api) { if (vq->do_unmap) {
for (j = 0; j < len / sizeof(struct vring_desc); j++) for (j = 0; j < len / sizeof(struct vring_desc); j++)
vring_unmap_one_split_indirect(vq, &indir_desc[j]); vring_unmap_one_split_indirect(vq, &indir_desc[j]);
} }
...@@ -1217,17 +1225,20 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq, ...@@ -1217,17 +1225,20 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
{ {
u16 flags; u16 flags;
if (!vq->use_dma_api)
return;
flags = extra->flags; flags = extra->flags;
if (flags & VRING_DESC_F_INDIRECT) { if (flags & VRING_DESC_F_INDIRECT) {
if (!vq->use_dma_api)
return;
dma_unmap_single(vring_dma_dev(vq), dma_unmap_single(vring_dma_dev(vq),
extra->addr, extra->len, extra->addr, extra->len,
(flags & VRING_DESC_F_WRITE) ? (flags & VRING_DESC_F_WRITE) ?
DMA_FROM_DEVICE : DMA_TO_DEVICE); DMA_FROM_DEVICE : DMA_TO_DEVICE);
} else { } else {
if (!vq->do_unmap)
return;
dma_unmap_page(vring_dma_dev(vq), dma_unmap_page(vring_dma_dev(vq),
extra->addr, extra->len, extra->addr, extra->len,
(flags & VRING_DESC_F_WRITE) ? (flags & VRING_DESC_F_WRITE) ?
...@@ -1240,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq, ...@@ -1240,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
{ {
u16 flags; u16 flags;
if (!vq->use_dma_api) if (!vq->do_unmap)
return; return;
flags = le16_to_cpu(desc->flags); flags = le16_to_cpu(desc->flags);
...@@ -1329,7 +1340,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, ...@@ -1329,7 +1340,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
sizeof(struct vring_packed_desc)); sizeof(struct vring_packed_desc));
vq->packed.vring.desc[head].id = cpu_to_le16(id); vq->packed.vring.desc[head].id = cpu_to_le16(id);
if (vq->use_dma_api) { if (vq->do_unmap) {
vq->packed.desc_extra[id].addr = addr; vq->packed.desc_extra[id].addr = addr;
vq->packed.desc_extra[id].len = total_sg * vq->packed.desc_extra[id].len = total_sg *
sizeof(struct vring_packed_desc); sizeof(struct vring_packed_desc);
...@@ -1470,7 +1481,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, ...@@ -1470,7 +1481,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
desc[i].len = cpu_to_le32(sg->length); desc[i].len = cpu_to_le32(sg->length);
desc[i].id = cpu_to_le16(id); desc[i].id = cpu_to_le16(id);
if (unlikely(vq->use_dma_api)) { if (unlikely(vq->do_unmap)) {
vq->packed.desc_extra[curr].addr = addr; vq->packed.desc_extra[curr].addr = addr;
vq->packed.desc_extra[curr].len = sg->length; vq->packed.desc_extra[curr].len = sg->length;
vq->packed.desc_extra[curr].flags = vq->packed.desc_extra[curr].flags =
...@@ -1604,7 +1615,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq, ...@@ -1604,7 +1615,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
vq->free_head = id; vq->free_head = id;
vq->vq.num_free += state->num; vq->vq.num_free += state->num;
if (unlikely(vq->use_dma_api)) { if (unlikely(vq->do_unmap)) {
curr = id; curr = id;
for (i = 0; i < state->num; i++) { for (i = 0; i < state->num; i++) {
vring_unmap_extra_packed(vq, vring_unmap_extra_packed(vq,
...@@ -1621,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq, ...@@ -1621,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
if (!desc) if (!desc)
return; return;
if (vq->use_dma_api) { if (vq->do_unmap) {
len = vq->packed.desc_extra[id].len; len = vq->packed.desc_extra[id].len;
for (i = 0; i < len / sizeof(struct vring_packed_desc); for (i = 0; i < len / sizeof(struct vring_packed_desc);
i++) i++)
...@@ -2080,6 +2091,7 @@ static struct virtqueue *vring_create_virtqueue_packed( ...@@ -2080,6 +2091,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
vq->dma_dev = dma_dev; vq->dma_dev = dma_dev;
vq->use_dma_api = vring_use_dma_api(vdev); vq->use_dma_api = vring_use_dma_api(vdev);
vq->premapped = false; vq->premapped = false;
vq->do_unmap = vq->use_dma_api;
vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
!context; !context;
...@@ -2587,6 +2599,7 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index, ...@@ -2587,6 +2599,7 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
vq->dma_dev = dma_dev; vq->dma_dev = dma_dev;
vq->use_dma_api = vring_use_dma_api(vdev); vq->use_dma_api = vring_use_dma_api(vdev);
vq->premapped = false; vq->premapped = false;
vq->do_unmap = vq->use_dma_api;
vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
!context; !context;
...@@ -2771,6 +2784,7 @@ int virtqueue_set_dma_premapped(struct virtqueue *_vq) ...@@ -2771,6 +2784,7 @@ int virtqueue_set_dma_premapped(struct virtqueue *_vq)
} }
vq->premapped = true; vq->premapped = true;
vq->do_unmap = false;
END_USE(vq); END_USE(vq);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment