Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1905170pxk; Tue, 1 Sep 2020 10:28:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyz5XEZ2Q4f0IdTX5A15aSEvKSjyNpRKQ0m+bcjnRCOokrVmb9sHd7jAvak3bwHn2nF9XAp X-Received: by 2002:a17:906:e087:: with SMTP id gh7mr2456365ejb.322.1598981306378; Tue, 01 Sep 2020 10:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598981306; cv=none; d=google.com; s=arc-20160816; b=S7Z96smhcoe1GHirRUh7Xo3+b/KU5B1sr8+WRmz2ccd6yAdQbfggyA20JQIdNwmiey v3F85uu3wzn2inqAXZC3RHPp3HF9XxR1j2BgCi5FUAgpvAHGoOSb8cYiqlty6scY+l/c Ye82CYhQ6oSWz42a/MrliIBm5NF7mQb3dX03SVfcy7LUTV6nSp6fbG18PabSQGJCJm49 xQZzjqilycs/eXBP2YcLzNjetMdF2Gg6f0SknPZOUQism93O7wVwjkKy2J098Sgakoei 4lb6JO3AUJKBhLwJdHmS/aldHvWdc7CKjsNkLrSrj/uoEZmzVPH6u5NrDutuNjbXklBR XGLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=GhxGvVFTEfQWtSfJ05xFQ9xiV3owFgjPP5BB5sAhh04=; b=fXWDGXL5X/c1XI2BFJc2quHv58/Xm3eQAqLGYZJrEL3+xNxKsartIKhphSEQQzzZjn B6ZSqqJWDqjzHC2pVHXHVR1vTSENXhFLPs7Jr6g9jbHoHneqbzJprkIBD7fGmvsW8g0V aHuBO0RvT8pZ3DHnjkU5Z7gyn2jaayWpneqR+dyb7qtlMIEaZu8b+mKPaydDyOtdjDVt i95vISH6j9XRjlK48xWfS/83m/W28h/+pQrg/Yg+u5sR4tu7IEXRh+tcSM+Gy2tNoxw2 NmxebOrwee8q8b2aZ2jupofyj4nfUAHnTPi4E8Wre1s5s0biTVcjiF0Rti0dclfxMVlO z8dA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 40si945875edr.35.2020.09.01.10.28.02; Tue, 01 Sep 2020 10:28:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728788AbgIARZR (ORCPT + 99 others); Tue, 1 Sep 2020 13:25:17 -0400 Received: from foss.arm.com ([217.140.110.172]:47234 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732731AbgIARYA (ORCPT ); Tue, 1 Sep 2020 13:24:00 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 52E8A1FB; Tue, 1 Sep 2020 10:23:58 -0700 (PDT) Received: from [10.57.40.122] (unknown [10.57.40.122]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 401F73F71F; Tue, 1 Sep 2020 10:23:56 -0700 (PDT) Subject: Re: [PATCH v9 01/32] drm: prime: add common helper to check scatterlist contiguity To: Marek Szyprowski , dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: Christoph Hellwig , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann References: <20200826063316.23486-1-m.szyprowski@samsung.com> <20200826063316.23486-2-m.szyprowski@samsung.com> From: Robin Murphy Message-ID: Date: Tue, 1 Sep 2020 18:23:54 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200826063316.23486-2-m.szyprowski@samsung.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-08-26 07:32, Marek Szyprowski wrote: > It is a common operation done by DRM drivers to check the contiguity > of the DMA-mapped buffer described by a scatterlist in the > sg_table object. Let's add a common helper for this operation. I still think this could be hoisted even further out to the common sgtable API level, but let's get the individual subsystems straightened out first then worry about consolidation later. Reviewed-by: Robin Murphy > Signed-off-by: Marek Szyprowski > Reviewed-by: Andrzej Hajda > --- > drivers/gpu/drm/drm_gem_cma_helper.c | 23 +++------------------ > drivers/gpu/drm/drm_prime.c | 31 ++++++++++++++++++++++++++++ > include/drm/drm_prime.h | 2 ++ > 3 files changed, 36 insertions(+), 20 deletions(-) > > diff --git a/drivers/gpu/drm/drm_gem_cma_helper.c b/drivers/gpu/drm/drm_gem_cma_helper.c > index 822edeadbab3..59b9ca207b42 100644 > --- a/drivers/gpu/drm/drm_gem_cma_helper.c > +++ b/drivers/gpu/drm/drm_gem_cma_helper.c > @@ -471,26 +471,9 @@ drm_gem_cma_prime_import_sg_table(struct drm_device *dev, > { > struct drm_gem_cma_object *cma_obj; > > - if (sgt->nents != 1) { > - /* check if the entries in the sg_table are contiguous */ > - dma_addr_t next_addr = sg_dma_address(sgt->sgl); > - struct scatterlist *s; > - unsigned int i; > - > - for_each_sg(sgt->sgl, s, sgt->nents, i) { > - /* > - * sg_dma_address(s) is only valid for entries > - * that have sg_dma_len(s) != 0 > - */ > - if (!sg_dma_len(s)) > - continue; > - > - if (sg_dma_address(s) != next_addr) > - return ERR_PTR(-EINVAL); > - > - next_addr = sg_dma_address(s) + sg_dma_len(s); > - } > - } > + /* check if the entries in the sg_table are contiguous */ > + if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size) > + return ERR_PTR(-EINVAL); > > /* Create a CMA GEM buffer. */ > cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size); > diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c > index 1693aa7c14b5..4ed5ed1f078c 100644 > --- a/drivers/gpu/drm/drm_prime.c > +++ b/drivers/gpu/drm/drm_prime.c > @@ -825,6 +825,37 @@ struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_page > } > EXPORT_SYMBOL(drm_prime_pages_to_sg); > > +/** > + * drm_prime_get_contiguous_size - returns the contiguous size of the buffer > + * @sgt: sg_table describing the buffer to check > + * > + * This helper calculates the contiguous size in the DMA address space > + * of the the buffer described by the provided sg_table. > + * > + * This is useful for implementing > + * &drm_gem_object_funcs.gem_prime_import_sg_table. > + */ > +unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt) > +{ > + dma_addr_t expected = sg_dma_address(sgt->sgl); > + struct scatterlist *sg; > + unsigned long size = 0; > + int i; > + > + for_each_sgtable_dma_sg(sgt, sg, i) { > + unsigned int len = sg_dma_len(sg); > + > + if (!len) > + break; > + if (sg_dma_address(sg) != expected) > + break; > + expected += len; > + size += len; > + } > + return size; > +} > +EXPORT_SYMBOL(drm_prime_get_contiguous_size); > + > /** > * drm_gem_prime_export - helper library implementation of the export callback > * @obj: GEM object to export > diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h > index 9af7422b44cf..47ef11614627 100644 > --- a/include/drm/drm_prime.h > +++ b/include/drm/drm_prime.h > @@ -92,6 +92,8 @@ struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_page > struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj, > int flags); > > +unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt); > + > /* helper functions for importing */ > struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev, > struct dma_buf *dma_buf, >