From patchwork Wed Mar 29 14:24:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Liu X-Patchwork-Id: 668115 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d08:0:0:0:0:0 with SMTP id z8csp394069wrt; Wed, 29 Mar 2023 07:26:01 -0700 (PDT) X-Google-Smtp-Source: AKy350Y5E1hMoEfY+erBbtO4ymm0ZCDJ4h1otTxl4wzBmYbWLkPAToydZSmCLxhfWK4hk2k/g9BX X-Received: by 2002:aa7:968b:0:b0:627:e61e:1ae with SMTP id f11-20020aa7968b000000b00627e61e01aemr18223986pfk.12.1680099961586; Wed, 29 Mar 2023 07:26:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680099961; cv=none; d=google.com; s=arc-20160816; b=YLzZoRIvIB2itlOA69FqczORo05HU45Hhz5j8hTIkJ9VB6Eg22DDwRG1olOD8mFS+r 0z4/8uUnLZCz3G4cfQkVAd3w/gVYBpzJew1cau9IqPMAjJ6bLePFIHlolQMX3yjXyRJi /kQndHNiBw2MROPRc7O38oYGXFFRep+pltu9zQYlM1Csi3wmN/roUSS+KVTD8AwenR9y Bwygd4b81nZ6SJ5p1DXqduRB+GiAAHJ2Ef5ULh2D+VrSyRmM9dQ6FwUmo1OrHZ+toDLd sj/onquO437rC1z0FfNbZG3nFeqd+BJi4zCqL58cV5JTQs+3H2vTyrm72r5eUs6RKkVf DEYQ== 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=lshIgirxJIJIK51W/n1rok2n86MeghDPYgGiKNUxpYA=; b=ZqzTc+DHJSstEfC7u7Cwdv/fwaXggH8Fdk39Q4BGhHcIEUeddi8FJFz6aiuxkGNjCy Am++jejA2+P3bh8mtYjIRfdNXUOK360oCQrNjBcWFq05zgg98N/fKd/8Dk3cTGiiHK6E XfRsXjbDb8shfj9QtvtP7/RJe540Y4J6ctb5ov8sZJYmXtJE8hKFxasb0ajfmV15cdet WJ2tk+QwZ8bKzyF6ApRzNe6zXQjK0XMHnzA2dc4dpwR5qQNHvGlkRDD1/MYeIc2kfleL xDzVr9/4TKQX1m/IsM+wx9hR//SMObhPP1nmgwC0Oh+E0MDSzS9RQCmTxBGFMPB5RBne cVGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="aXQ/kWlT"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id f26-20020aa7969a000000b0062dbc05a323si299974pfk.298.2023.03.29.07.26.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 07:26:01 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="aXQ/kWlT"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 C88ED85DDB; Wed, 29 Mar 2023 16:25:49 +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="aXQ/kWlT"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8138185CB4; Wed, 29 Mar 2023 16:25:26 +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-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) (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 5215985D34 for ; Wed, 29 Mar 2023 16:25:13 +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-pl1-x62b.google.com with SMTP id z19so15103778plo.2 for ; Wed, 29 Mar 2023 07:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680099911; 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=lshIgirxJIJIK51W/n1rok2n86MeghDPYgGiKNUxpYA=; b=aXQ/kWlTKRtG8bkczLrLmFWp753PX9aMVc6gvcLlCFHZq1500MyOYA6H5IN3XSUZFD PhWxYEptfYhXfefWF2p025MmD3F0Y71v5SfwNQ3fJsvKIVdVl5RDIScZndWs0DStqkEy wh2CpP9UjGqJWC/GmFOIGG0f7J6ur+4h9Lqep9Qw7GINhDpFH0P/NScj0jsSiP/PH+u+ MqRtiAg4mil06bObxMCzpunwvB+DpKIDhEEDavsIv7pAV7SLS4+JHMG3uxf/Asb0murU G2uG246FIX22m9nmO88rlgR+riAadAQOvNshGhX202XAFIol5ko/tgEgvnJ8tXvZCAKJ A7cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680099911; 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=lshIgirxJIJIK51W/n1rok2n86MeghDPYgGiKNUxpYA=; b=zAYGrQOpG59YYNef1fSC1VMP2hB7kD8dwfgvAcUBfe522hnM8ARzsIhQEEyafdsWEO r5dRMbQ4cKa74vQu/LZqx012SgJ6Bzw7Nv2lbE7QuhW6/ruPv1oi3fDLm6iqi2i0YKW5 G2/j+StBg1Zsb4+xRmwfSw6D8oDOQqqe+YVzQ3MWIVlX4WPm4cGRs/jOtnj06hRJCOzl hvbzy9vLu/BulRP5S2VHTW4SbzIyNBwPJEMqnhozqqxojL1tB/zvblIL/OZR9RAaHZLm iDP4g58z0Fk3dOvlanozeaw3Ywt4GBO+ii5/cyIqidFdkL3tFeHAtk+kJisHXbX7wTYX jdIg== X-Gm-Message-State: AAQBX9fFMb+/gKGTaUYlsqngp5aZSfoQRawV4BMKLlcqexcjg32s5OYu MZzwAZl2fuko8BLLFZZzic9IBZ0DTcc8miui3uM= X-Received: by 2002:a17:90b:4f86:b0:237:9fbd:31c8 with SMTP id qe6-20020a17090b4f8600b002379fbd31c8mr20994574pjb.15.1680099911380; Wed, 29 Mar 2023 07:25:11 -0700 (PDT) Received: from localhost ([111.184.129.17]) by smtp.gmail.com with ESMTPSA id e12-20020a17090ab38c00b0023d16f05dd8sm1481294pjr.36.2023.03.29.07.25.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 07:25:11 -0700 (PDT) From: "Ying-Chun Liu (PaulLiu)" To: u-boot@lists.denx.de Cc: Will Deacon , Ying-Chun Liu , Bin Meng Subject: [PATCH 3/6] virtio: Allocate virtqueue in page-size units Date: Wed, 29 Mar 2023 22:24:57 +0800 Message-Id: <20230329142500.682922-4-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 In preparation for explicit bouncing of virtqueue pages for devices advertising the VIRTIO_F_IOMMU_PLATFORM feature, introduce a couple of wrappers around virtqueue allocation and freeing operations, ensuring that buffers are handled in terms of page-size units. 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/+/b4bb5227d4cf4fdfcd8b4e1ff2692d3a54d1482a Reviewed-by: Simon Glass --- drivers/virtio/virtio_ring.c | 24 ++++++++++++++++++++---- include/virtio_ring.h | 16 +++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index f71bab7847..5aeb13fd59 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -15,6 +15,17 @@ #include #include #include +#include + +static void *virtio_alloc_pages(struct udevice *vdev, u32 npages) +{ + return memalign(PAGE_SIZE, npages * PAGE_SIZE); +} + +static void virtio_free_pages(struct udevice *vdev, void *ptr, u32 npages) +{ + free(ptr); +} static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i, struct virtio_sg *sg, u16 flags) @@ -271,6 +282,8 @@ struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, unsigned int vring_align, struct udevice *udev) { + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct udevice *vdev = uc_priv->vdev; struct virtqueue *vq; void *queue = NULL; struct vring vring; @@ -283,7 +296,9 @@ struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, /* TODO: allocate each queue chunk individually */ for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { - queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); + size_t sz = vring_size(num, vring_align); + + queue = virtio_alloc_pages(vdev, DIV_ROUND_UP(sz, PAGE_SIZE)); if (queue) break; } @@ -293,7 +308,7 @@ struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, if (!queue) { /* Try to get a single page. You are my only hope! */ - queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); + queue = virtio_alloc_pages(vdev, 1); } if (!queue) return NULL; @@ -303,7 +318,7 @@ struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, vq = __vring_new_virtqueue(index, vring, udev); if (!vq) { - free(queue); + virtio_free_pages(vdev, queue, DIV_ROUND_UP(vring.size, PAGE_SIZE)); return NULL; } debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name, @@ -314,7 +329,8 @@ struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, void vring_del_virtqueue(struct virtqueue *vq) { - free(vq->vring.desc); + virtio_free_pages(vq->vdev, vq->vring.desc, + DIV_ROUND_UP(vq->vring.size, PAGE_SIZE)); free(vq->vring_desc_shadow); list_del(&vq->list); free(vq); diff --git a/include/virtio_ring.h b/include/virtio_ring.h index c77c212cff..8f8a55c7bd 100644 --- a/include/virtio_ring.h +++ b/include/virtio_ring.h @@ -86,6 +86,7 @@ struct vring_used { struct vring { unsigned int num; + size_t size; struct vring_desc *desc; struct vring_avail *avail; struct vring_used *used; @@ -137,23 +138,24 @@ struct virtqueue { #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num]) +static inline unsigned int vring_size(unsigned int num, unsigned long align) +{ + return ((sizeof(struct vring_desc) * num + + sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) + + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num; +} + static inline void vring_init(struct vring *vr, unsigned int num, void *p, unsigned long align) { vr->num = num; + vr->size = vring_size(num, align); vr->desc = p; vr->avail = p + num * sizeof(struct vring_desc); vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) + align - 1) & ~(align - 1)); } -static inline unsigned int vring_size(unsigned int num, unsigned long align) -{ - return ((sizeof(struct vring_desc) * num + - sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) + - sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num; -} - /* * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX. * Assuming a given event_idx value from the other side, if we have just