Received: by 10.223.176.5 with SMTP id f5csp3735066wra; Mon, 29 Jan 2018 18:41:46 -0800 (PST) X-Google-Smtp-Source: AH8x226sE7mRmXi5qpq9tAAE/JePfl4fSVR6R/3Oyc/wxJqV3zQTi5U0MvPCXObb0k8k4+Fd+7Ur X-Received: by 10.99.49.200 with SMTP id x191mr22040839pgx.394.1517280106204; Mon, 29 Jan 2018 18:41:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517280106; cv=none; d=google.com; s=arc-20160816; b=fxreVbnbnAtQ61ddZhDh6XjMmYnIWuwVEI9TgKM/rHCSKZ9Ak5FxAXbmCPwFDLfeJL RFohM0owTaD3z2VVuuJYT4nzBslwokPbWPKJfAfCxLmAR522Ky+0L8iloS27JTNxnuHQ xVq69/ns47Mxob3YfzDFifhUWH3gjRttbAxnl6Rrytkn/e6MnLQl+lT6gSsQZp/0nJap nIJ431VRCeTg6D/wIyK6alAcyl2v1wRFNCU8QYhLaGpi3G98/zW5IL0aoQdRwT+2pvhA xRu4cG6x5mr/WQ2ny3HBCnVTX+w/QfRMaC8Jx2NJyvmGf349Uq/M9CxUIQ6+J0596mOt BV/Q== 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:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject:arc-authentication-results; bh=5nuQOk794tv2gDxGPdCH/4B11AQVc0ZuDWYD4DktDPg=; b=ZyfoLr86YH5NbE9UaIQme9mNbEH7/T0yzkKafVe6IYWtv6D+hDH9eqrPQIRmrO/zRW 92b1juN/GWmF6ZoQ1eiZDwGFAqFoiZ3qpuwKF9bpgIczkIpE6MjOY4XQ5ryFerif2vt7 tLdL+X69WbBQazx8xpPLyhHp3KRKRAUZRed1d+dxt7/SS6Fjtqy8ULsn2jGOXNm5yD5O OUegKIOEZk4ujASsETkFq1F7jr7dTP/OH0o3N5dLbVG0/neZLtsC6JiDSxikKs6aAXMS 03isb0e/w7xZEu8GdTOuePRZFFwkTJ2stJj6VN130EVBcSP6G8cgAHrkbEMixRQXlVno dCLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t62si620005pgt.130.2018.01.29.18.41.31; Mon, 29 Jan 2018 18:41:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752628AbeA3ClA (ORCPT + 99 others); Mon, 29 Jan 2018 21:41:00 -0500 Received: from regular1.263xmail.com ([211.150.99.132]:46807 "EHLO regular1.263xmail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752599AbeA3Ck7 (ORCPT ); Mon, 29 Jan 2018 21:40:59 -0500 Received: from hjc?rock-chips.com (unknown [192.168.167.223]) by regular1.263xmail.com (Postfix) with ESMTP id 3D64894B6; Tue, 30 Jan 2018 10:40:55 +0800 (CST) X-263anti-spam: KSV:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-KSVirus-check: 0 X-ABS-CHECKED: 4 Received: from [172.16.10.72] (localhost [127.0.0.1]) by smtp.263.net (Postfix) with ESMTPA id EEF8A308; Tue, 30 Jan 2018 10:40:45 +0800 (CST) X-RL-SENDER: hjc@rock-chips.com X-FST-TO: sandy.huang@rock-chips.com X-SENDER-IP: 58.22.7.114 X-LOGIN-NAME: hjc@rock-chips.com X-UNIQUE-TAG: <1e326803f5b443e85db1d27211f1a0c2> X-ATTACHMENT-NUM: 0 X-SENDER: hjc@rock-chips.com X-DNS-TYPE: 0 Received: from [172.16.10.72] (unknown [58.22.7.114]) by smtp.263.net (Postfix) whith ESMTP id 26370Q6TGBJ; Tue, 30 Jan 2018 10:40:54 +0800 (CST) Subject: Re: [PATCH v2 02/43] drm/rockchip: support prime import sg table To: Thierry Escande , Archit Taneja , Inki Dae , Thierry Reding , Sean Paul , David Airlie , Tomasz Figa Cc: Zain Wang , Lin Huang , Douglas Anderson , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, =?UTF-8?Q?=c3=98rjan_Eide?= , Haixia Shi References: <20180126131710.7622-1-thierry.escande@collabora.com> <20180126131710.7622-3-thierry.escande@collabora.com> From: Sandy Huang Message-ID: <0898ce3d-0f22-7063-7179-a4a7f942eaeb@rock-chips.com> Date: Tue, 30 Jan 2018 10:40:49 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <20180126131710.7622-3-thierry.escande@collabora.com> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ?? 2018/1/26 21:16, Thierry Escande ะด??: > From: Haixia Shi > > The prime fd to handle ioctl was not used with rockchip before. Support > was added in order to pass graphics_Gbm and to support potential uses > within Chrome OS (e.g. zero-copy video decode, camera). > > Signed-off-by: Haixia Shi > Signed-off-by: Sean Paul > Signed-off-by: Thierry Escande > Tested-by: Heiko Stuebner > --- > drivers/gpu/drm/rockchip/rockchip_drm_drv.c | 1 + > drivers/gpu/drm/rockchip/rockchip_drm_gem.c | 118 ++++++++++++++++++++++++++-- > drivers/gpu/drm/rockchip/rockchip_drm_gem.h | 5 +- > 3 files changed, 115 insertions(+), 9 deletions(-) > > diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c > index d85431400a0d..88084ca15115 100644 > --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c > +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c > @@ -230,6 +230,7 @@ static struct drm_driver rockchip_drm_driver = { > .gem_prime_import = drm_gem_prime_import, > .gem_prime_export = drm_gem_prime_export, > .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table, > + .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table, > .gem_prime_vmap = rockchip_gem_prime_vmap, > .gem_prime_vunmap = rockchip_gem_prime_vunmap, > .gem_prime_mmap = rockchip_gem_mmap_buf, > diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c > index 1d9655576b6e..5d52020deca1 100644 > --- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c > +++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c > @@ -16,6 +16,8 @@ > #include > #include > #include > + > +#include > #include > > #include "rockchip_drm_drv.h" > @@ -309,12 +311,10 @@ static void rockchip_gem_release_object(struct rockchip_gem_object *rk_obj) > } > > struct rockchip_gem_object * > - rockchip_gem_create_object(struct drm_device *drm, unsigned int size, > - bool alloc_kmap) > + rockchip_gem_alloc_object(struct drm_device *drm, unsigned int size) > { > struct rockchip_gem_object *rk_obj; > struct drm_gem_object *obj; > - int ret; > > size = round_up(size, PAGE_SIZE); > > @@ -326,6 +326,20 @@ struct rockchip_gem_object * > > drm_gem_object_init(drm, obj, size); > > + return rk_obj; > +} > + > +struct rockchip_gem_object * > +rockchip_gem_create_object(struct drm_device *drm, unsigned int size, > + bool alloc_kmap) > +{ > + struct rockchip_gem_object *rk_obj; > + int ret; > + > + rk_obj = rockchip_gem_alloc_object(drm, size); > + if (IS_ERR(rk_obj)) > + return rk_obj; > + > ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap); > if (ret) > goto err_free_rk_obj; > @@ -343,11 +357,21 @@ struct rockchip_gem_object * > */ > void rockchip_gem_free_object(struct drm_gem_object *obj) > { > - struct rockchip_gem_object *rk_obj; > - > - rk_obj = to_rockchip_obj(obj); > + struct drm_device *drm = obj->dev; > + struct rockchip_drm_private *private = drm->dev_private; > + struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); > > - rockchip_gem_free_buf(rk_obj); > + if (obj->import_attach) { > + if (private->domain) { > + rockchip_gem_iommu_unmap(rk_obj); > + } else { > + dma_unmap_sg(drm->dev, rk_obj->sgt->sgl, > + rk_obj->sgt->nents, DMA_BIDIRECTIONAL); > + } > + drm_prime_gem_destroy(obj, rk_obj->sgt); > + } else { > + rockchip_gem_free_buf(rk_obj); > + } > > rockchip_gem_release_object(rk_obj); > } > @@ -451,6 +475,86 @@ struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj) > return sgt; > } > > +static unsigned long rockchip_sg_get_contiguous_size(struct sg_table *sgt, > + int count) > +{ > + struct scatterlist *s; > + dma_addr_t expected = sg_dma_address(sgt->sgl); > + unsigned int i; > + unsigned long size = 0; > + > + for_each_sg(sgt->sgl, s, count, i) { > + if (sg_dma_address(s) != expected) > + break; > + expected = sg_dma_address(s) + sg_dma_len(s); > + size += sg_dma_len(s); > + } > + return size; > +} > + > +static int > +rockchip_gem_iommu_map_sg(struct drm_device *drm, > + struct dma_buf_attachment *attach, > + struct sg_table *sg, > + struct rockchip_gem_object *rk_obj) > +{ > + rk_obj->sgt = sg; > + return rockchip_gem_iommu_map(rk_obj); > +} > + > +static int > +rockchip_gem_dma_map_sg(struct drm_device *drm, > + struct dma_buf_attachment *attach, > + struct sg_table *sg, > + struct rockchip_gem_object *rk_obj) > +{ > + int count = dma_map_sg(drm->dev, sg->sgl, sg->nents, > + DMA_BIDIRECTIONAL); > + if (!count) > + return -EINVAL; > + > + if (rockchip_sg_get_contiguous_size(sg, count) < attach->dmabuf->size) { > + DRM_ERROR("failed to map sg_table to contiguous linear address.\n"); > + dma_unmap_sg(drm->dev, sg->sgl, sg->nents, > + DMA_BIDIRECTIONAL); > + return -EINVAL; > + } > + > + rk_obj->dma_addr = sg_dma_address(sg->sgl); > + rk_obj->sgt = sg; > + return 0; > +} > + > +struct drm_gem_object * > +rockchip_gem_prime_import_sg_table(struct drm_device *drm, > + struct dma_buf_attachment *attach, > + struct sg_table *sg) > +{ > + struct rockchip_drm_private *private = drm->dev_private; > + struct rockchip_gem_object *rk_obj; > + int ret; > + > + rk_obj = rockchip_gem_alloc_object(drm, attach->dmabuf->size); > + if (IS_ERR(rk_obj)) > + return ERR_CAST(rk_obj); > + > + if (private->domain) > + ret = rockchip_gem_iommu_map_sg(drm, attach, sg, rk_obj); > + else > + ret = rockchip_gem_dma_map_sg(drm, attach, sg, rk_obj); > + > + if (ret < 0) { > + DRM_ERROR("failed to import sg table: %d\n", ret); > + goto err_free_rk_obj; > + } > + > + return &rk_obj->base; > + > +err_free_rk_obj: > + rockchip_gem_release_object(rk_obj); > + return ERR_PTR(ret); > +} > + > void *rockchip_gem_prime_vmap(struct drm_gem_object *obj) > { > struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); > diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h > index f237375582fb..d41fa65219d2 100644 > --- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h > +++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h > @@ -36,8 +36,9 @@ struct rockchip_gem_object { > > struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj); > struct drm_gem_object * > -rockchip_gem_prime_import_sg_table(struct drm_device *dev, size_t size, > - struct sg_table *sgt); > +rockchip_gem_prime_import_sg_table(struct drm_device *dev, > + struct dma_buf_attachment *attach, > + struct sg_table *sg); > void *rockchip_gem_prime_vmap(struct drm_gem_object *obj); > void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr); > > Reviewed-by: Sandy huang