aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Deacon <willdeacon@google.com>2023-03-29 22:25:00 +0800
committerTom Rini <trini@konsulko.com>2023-04-25 11:53:15 -0400
commitb0a2fe148a70dc805f1e11702a08a7065418a8dd (patch)
treedd96eae340d934015017a673ef06127c49c7a645
parent37e53db38bdbeb58732e36d1efcdf58f6ce3a138 (diff)
downloadu-boot-WIP/2023-04-25-use-bounce-buffers-for-VIRTIO_F_IOMMU_PLATFORM.zip
u-boot-WIP/2023-04-25-use-bounce-buffers-for-VIRTIO_F_IOMMU_PLATFORM.tar.gz
u-boot-WIP/2023-04-25-use-bounce-buffers-for-VIRTIO_F_IOMMU_PLATFORM.tar.bz2
virtio: Use bounce buffers when VIRTIO_F_IOMMU_PLATFORM is setWIP/2023-04-25-use-bounce-buffers-for-VIRTIO_F_IOMMU_PLATFORM
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 Reviewed-by: Simon Glass <sjg@chromium.org>
-rw-r--r--drivers/virtio/virtio_ring.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index de75786..c9adcce 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[],