u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
From: "Ying-Chun Liu (PaulLiu)" <paul.liu@linaro.org>
To: u-boot@lists.denx.de
Cc: Will Deacon <willdeacon@google.com>,
	Ying-Chun Liu <paul.liu@linaro.org>,
	Bin Meng <bmeng.cn@gmail.com>
Subject: [PATCH 6/6] virtio: Use bounce buffers when VIRTIO_F_IOMMU_PLATFORM is set
Date: Wed, 29 Mar 2023 22:25:00 +0800	[thread overview]
Message-ID: <20230329142500.682922-7-paul.liu@linaro.org> (raw)
In-Reply-To: <20230329142500.682922-1-paul.liu@linaro.org>

From: Will Deacon <willdeacon@google.com>

Devices advertising the VIRTIO_F_IOMMU_PLATFORM feature require
platform-specific handling to configure their DMA transactions.

When handling virtio descriptors for such a device, use bounce
buffers to ensure that the underlying buffers are always aligned
to and padded to PAGE_SIZE in preparation for platform specific
handling at page granularity.

Signed-off-by: Will Deacon <willdeacon@google.com>
[ Paul: pick from the Android tree. Rebase to the upstream ]
Signed-off-by: Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>
Cc: Bin Meng <bmeng.cn@gmail.com>
Link: https://android.googlesource.com/platform/external/u-boot/+/1eff171e613ee67dca71dbe97be7282e2db17011
---
 drivers/virtio/virtio_ring.c | 48 +++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index de75786ca7..c9adcce5c0 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -28,14 +28,51 @@ static void virtio_free_pages(struct udevice *vdev, void *ptr, u32 npages)
 	free(ptr);
 }
 
+static int __bb_force_page_align(struct bounce_buffer *state)
+{
+	const ulong align_mask = PAGE_SIZE - 1;
+
+	if ((ulong)state->user_buffer & align_mask)
+		return 0;
+
+	if (state->len != state->len_aligned)
+		return 0;
+
+	return 1;
+}
+
 static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
 					  struct virtio_sg *sg, u16 flags)
 {
 	struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i];
 	struct vring_desc *desc = &vq->vring.desc[i];
+	void *addr;
+
+	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && vq->vring.bouncebufs) {
+		struct bounce_buffer *bb = &vq->vring.bouncebufs[i];
+		unsigned int bbflags;
+		int ret;
+
+		if (flags & VRING_DESC_F_WRITE)
+			bbflags = GEN_BB_WRITE;
+		else
+			bbflags = GEN_BB_READ;
+
+		ret = bounce_buffer_start_extalign(bb, sg->addr, sg->length,
+						   bbflags, PAGE_SIZE,
+						   __bb_force_page_align);
+		if (ret) {
+			debug("%s: failed to allocate bounce buffer (length 0x%zx)\n",
+			      vq->vdev->name, sg->length);
+		}
+
+		addr = bb->bounce_buffer;
+	} else {
+		addr = sg->addr;
+	}
 
 	/* Update the shadow descriptor. */
-	desc_shadow->addr = (u64)(uintptr_t)sg->addr;
+	desc_shadow->addr = (u64)(uintptr_t)addr;
 	desc_shadow->len = sg->length;
 	desc_shadow->flags = flags;
 
@@ -50,6 +87,15 @@ static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
 
 static void virtqueue_detach_desc(struct virtqueue *vq, unsigned int idx)
 {
+	struct vring_desc *desc = &vq->vring.desc[idx];
+	struct bounce_buffer *bb;
+
+	if (!IS_ENABLED(CONFIG_BOUNCE_BUFFER) || !vq->vring.bouncebufs)
+		return;
+
+	bb = &vq->vring.bouncebufs[idx];
+	bounce_buffer_stop(bb);
+	desc->addr = cpu_to_virtio64(vq->vdev, (u64)(uintptr_t)bb->user_buffer);
 }
 
 int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
-- 
2.39.2


  parent reply	other threads:[~2023-03-29 14:27 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-29 14:24 [PATCH 0/6] virtio: Use bounce buffers when VIRTIO_F_IOMMU_PLATFORM set Ying-Chun Liu (PaulLiu)
2023-03-29 14:24 ` [PATCH 1/6] virtio: Expose VIRTIO_F_IOMMU_PLATFORM in device features Ying-Chun Liu (PaulLiu)
2023-04-01  6:31   ` Simon Glass
2023-03-29 14:24 ` [PATCH 2/6] virtio: pci: Tear down VQs in virtio_pci_reset() Ying-Chun Liu (PaulLiu)
2023-04-01  6:31   ` Simon Glass
2023-03-29 14:24 ` [PATCH 3/6] virtio: Allocate virtqueue in page-size units Ying-Chun Liu (PaulLiu)
2023-04-01  6:31   ` Simon Glass
2023-03-29 14:24 ` [PATCH 4/6] virtio: Add helper functions to attach/detach vring descriptors Ying-Chun Liu (PaulLiu)
2023-04-01  6:32   ` Simon Glass
2023-03-29 14:24 ` [PATCH 5/6] virtio: Allocate bounce buffers for devices with VIRTIO_F_IOMMU_PLATFORM Ying-Chun Liu (PaulLiu)
2023-04-01  6:32   ` Simon Glass
2023-03-29 14:25 ` Ying-Chun Liu (PaulLiu) [this message]
2023-04-01  6:32   ` [PATCH 6/6] virtio: Use bounce buffers when VIRTIO_F_IOMMU_PLATFORM is set Simon Glass
2023-04-25 18:31 ` [PATCH 0/6] virtio: Use bounce buffers when VIRTIO_F_IOMMU_PLATFORM set Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230329142500.682922-7-paul.liu@linaro.org \
    --to=paul.liu@linaro.org \
    --cc=bmeng.cn@gmail.com \
    --cc=u-boot@lists.denx.de \
    --cc=willdeacon@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).