From patchwork Wed Mar 29 14:25:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Liu X-Patchwork-Id: 668118 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d08:0:0:0:0:0 with SMTP id z8csp394612wrt; Wed, 29 Mar 2023 07:27:05 -0700 (PDT) X-Google-Smtp-Source: AKy350YJsr79rCPySrP7X9V3kSQAvYWpMf/hhqdAyX/5aNVem+5Y1BDpf5hdys0WlWH2Q/x8IDwl X-Received: by 2002:a17:90b:3851:b0:23d:4e9d:2eb0 with SMTP id nl17-20020a17090b385100b0023d4e9d2eb0mr21044372pjb.36.1680100024819; Wed, 29 Mar 2023 07:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680100024; cv=none; d=google.com; s=arc-20160816; b=Pcy1khbsk2NP42/pZeG0iP0bjsXpR54gMZ5cBUOM20Jm5fT5AbonRJwIm0Wbu8dQuX /Zg92LB3FIYWG1IFkhpR2QUm2OfXnJHLeGw/xFkZOOEi2lu/uC0PCwr3pouwuai9x+Zf rDCs8u7dJbVKtA0JSDaeZL0afUMZEQf1muC/fTL7zS7faXogyIw+KWmnl4uiMcDycPRX lKK8SGP1SQY18Rr/rwAFtAUj6bIFWCS6EWaRaJznHSaTOcv0wj8cfJwjkj9eLDscGa78 RomqciHsmT6CQOmyiloycUTdZpaVIj4NF9KVcYj8WFaaPZ5N3PCNppZFkIlCp6c+ikrC GX2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SrYDOGu77eGaRtbf1G+Dcj+IX0mHeAKzIJwgu0r4OJU=; b=F4ZzlLni/yOHN1SaZpO/DMo5i51S96HVbhWSTZQcx3cfyUWPCLy/t8gDyv9wHjf9/P 2HtuogA9wFQXXMwiGesZgvubj+Ir2xYfGHQNjfrVzGHMxvkEDrGjtFsJeiSU58rRQe3B w3YS3scTwW17D7SncA3eAzfJo8bQL8eYw/fXIYkWT4KJXi11qBgEfxiX43f5GbnhEUQ1 GnhVR6smLk9WMwQuDoNHbnDT5bU+mOKrHpXoG1M/JQcb0+Ks9YDg51MhJxv3hGUl6z8F FAexDICVqWBCetO/QWTrJ+AuAnQytkWJJyAvlupTl9suT3arB49r1y5MhNfOOh07J+Kt GHTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VffcFn5r; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id o35-20020a635a23000000b00513234112b9si424606pgb.901.2023.03.29.07.27.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 07:27:04 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VffcFn5r; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 565EC85DDB; Wed, 29 Mar 2023 16:26:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="VffcFn5r"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C490985D4E; Wed, 29 Mar 2023 16:25:34 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0BB2985DDB for ; Wed, 29 Mar 2023 16:25:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=paul.liu@linaro.org Received: by mail-pj1-x1036.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso18770108pjb.0 for ; Wed, 29 Mar 2023 07:25:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680099916; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SrYDOGu77eGaRtbf1G+Dcj+IX0mHeAKzIJwgu0r4OJU=; b=VffcFn5r+HgO1ke/zmM6mALDrEwf6NGmXhksXpPW9hU0p+lIrDbZnMkEW8lUnihJR5 BVhpwoxN5pJMx7m0njoDnLXn8m9o/yVXZBrL62UoFaEiudjxGiuqYrnIHjDVP8h/jWM+ FtNdmHIBfbDVbD4JBZ1hew+EauvbgTJSDUy9cJZwyYpOaH8whZrkITlobgjByjXkYFgz 2eBi42b430rrFeywmh0lRHYCAz1QVXqd6syKSBOluc8eERnV+MhCGNIjUJwUBKfewMvN abM3mcRLYYvctriIvvEDoW3e9NJbqVE7zMabD4naCHSvDHtYMKH0yCx9Aft5hUl3gtVA fsuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680099916; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SrYDOGu77eGaRtbf1G+Dcj+IX0mHeAKzIJwgu0r4OJU=; b=VPefzB1omP3xyQ0I53aAGDpeJiQYfgwKfOXHFBrmj+jrswpbfXPU/rCxssCTY42yCB YyRiLfBQo2RRT4xoTUq9hpdBaJjSRKRXUiLty0/wHIEjU6WWjVc2u28JOzowU2Qvw3/H 9LXclB3bt/V1ZO3HkqrdhzwjIhj9U3nKEzGIqJKP1EY8VopV2o5TFIsqM7UC6lNwvFtn 5U21MqP0w1jGX+BJF5eCNPt5e4iT67RQgUiS4qpSmesdqoF9QiHeUNkLpZ/TqmbwnIsD di9HWQLVB7/LJkB7pH/qfEDT+1Zzwx6i+3q8NSip3bf/76vVhW8j3zuhB5ZKslE+phma g+DQ== X-Gm-Message-State: AAQBX9eBxyHeOzCup5J7Pbt42ZWzw0snyslmuxGmXphi5Day4BadS/4p bfAsYY5Sa3kisVd10x2IsfK3B2ckXI20/7GhjVk= X-Received: by 2002:a17:90b:4d92:b0:23d:3c7b:8684 with SMTP id oj18-20020a17090b4d9200b0023d3c7b8684mr21414390pjb.41.1680099916518; Wed, 29 Mar 2023 07:25:16 -0700 (PDT) Received: from localhost ([111.184.129.17]) by smtp.gmail.com with ESMTPSA id x5-20020a1709028ec500b001a04d37a4acsm23119432plo.9.2023.03.29.07.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 07:25:16 -0700 (PDT) From: "Ying-Chun Liu (PaulLiu)" To: u-boot@lists.denx.de Cc: Will Deacon , Ying-Chun Liu , Bin Meng Subject: [PATCH 6/6] virtio: Use bounce buffers when VIRTIO_F_IOMMU_PLATFORM is set Date: Wed, 29 Mar 2023 22:25:00 +0800 Message-Id: <20230329142500.682922-7-paul.liu@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230329142500.682922-1-paul.liu@linaro.org> References: <20230329142500.682922-1-paul.liu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean From: Will Deacon 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 [ Paul: pick from the Android tree. Rebase to the upstream ] Signed-off-by: Ying-Chun Liu (PaulLiu) Cc: Bin Meng Link: https://android.googlesource.com/platform/external/u-boot/+/1eff171e613ee67dca71dbe97be7282e2db17011 Reviewed-by: Simon Glass --- 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[],