From patchwork Tue Feb 2 23:37:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zach Reizner X-Patchwork-Id: 8196531 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B55089F38B for ; Tue, 2 Feb 2016 23:37:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B2429202E9 for ; Tue, 2 Feb 2016 23:37:49 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id C12A82027D for ; Tue, 2 Feb 2016 23:37:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 91BB56E639; Tue, 2 Feb 2016 15:37:47 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pa0-f45.google.com (mail-pa0-f45.google.com [209.85.220.45]) by gabe.freedesktop.org (Postfix) with ESMTPS id 64A066E639 for ; Tue, 2 Feb 2016 15:37:46 -0800 (PST) Received: by mail-pa0-f45.google.com with SMTP id cy9so2364427pac.0 for ; Tue, 02 Feb 2016 15:37:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=6dTblZ6sNoG3UEjfv/4B3vcqBi+SXEyEQfPug78/T9I=; b=dKoqdlhATR4indVKuNJtexOOZ1rRMozJI+LzSmlDi28OMLpBKLgxYS6sHdYjxMAd32 iFrKiJb//owP5H2QWf/8TTNYpNZHoM6LDN6qB1by0RSQwR07kOpgrqmfDAG4f0qS1ITg tOSffbB7Tuxm7YAlqJrqNfw8YnQy2Nmr7eu7N2eO6CNFpPXI4LLzPAkt/rdEsGZd/UYo pxiLqh1WRsmRgE5vTyhracOIB+4SE/dXSxUtwvrsPeirAqPtNTnqs3EWOBP38sd/7CAf UaAhjN6t7fH0CfL3TYbJp7lY6Z2VRDL9953Lc8mH93J72tCQZInuGycEJypYLTDWgyWI vb0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=6dTblZ6sNoG3UEjfv/4B3vcqBi+SXEyEQfPug78/T9I=; b=WalBZaNqb5Yr4t9PWIu7cNSSwRCKwgP9y0Z7HDOzPtXbV4UAUxSo0pVB+kOsOuvlY+ YbV5SYvsFJ+NNB8w1CZ2QMBizdblMULQzv08C1nwizcDIq5KXfVnC5zEyReLldM37dq3 pvPxLYCtWBpx2QSUzTjLATUNfMb8dwFSfvNE1d/tNfVFU+VN8i/jyrhXUDLAHwLoOyvd mKQUfA5fwKUkEJ6Y6BuzfcxrmLvWrSZ1TO03DvTWEYspuug8ZH4UPt1+leikQwoIpfDx wEYFfj9lVXsFX/lfLzD9xITrd0jnOw2Bf4afQgR+12brglOKuEUpGxFjHuRSoNcA7jwV 6bDA== X-Gm-Message-State: AG10YOSpcJCXB/9DAgA1MIKyerLGioPPMT4tJCxGbgh5fWiqA1oPR8tzmwY5OvptzNoxdVI1 X-Received: by 10.67.3.134 with SMTP id bw6mr50929645pad.154.1454456265884; Tue, 02 Feb 2016 15:37:45 -0800 (PST) Received: from zachr.mtv.corp.google.com ([172.22.64.53]) by smtp.gmail.com with ESMTPSA id xr8sm5039395pab.26.2016.02.02.15.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Feb 2016 15:37:45 -0800 (PST) From: Zach Reizner To: dri-devel@lists.freedesktop.org Subject: [PATCH] drm/rockchip: support prime fd import Date: Tue, 2 Feb 2016 15:37:18 -0800 Message-Id: <1454456238-18742-1-git-send-email-zachr@google.com> X-Mailer: git-send-email 2.7.0.rc3.207.g0ac5344 Cc: =?UTF-8?q?St=C3=A9phane=20Marchesin?= , Zach Reizner X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The prime fd to handle ioctl was not used with rockchip before. Support was added in order to support potential uses (e.g. zero-copy video decode, camera). Signed-off-by: Zach Reizner Reviewed-by: Stéphane Marchesin --- drivers/gpu/drm/rockchip/rockchip_drm_drv.c | 1 + drivers/gpu/drm/rockchip/rockchip_drm_gem.c | 92 ++++++++++++++++++++++++++--- drivers/gpu/drm/rockchip/rockchip_drm_gem.h | 7 ++- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c index dbfec29..4a01d92 100644 --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c @@ -307,6 +307,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 a5c512e..cf6b50b 100644 --- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c +++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c @@ -19,6 +19,7 @@ #include #include +#include #include "rockchip_drm_drv.h" #include "rockchip_drm_gem.h" @@ -105,13 +106,11 @@ int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma) return rockchip_drm_gem_object_mmap(obj, vma); } -struct rockchip_gem_object * - rockchip_gem_create_object(struct drm_device *drm, unsigned int size, - bool alloc_kmap) +static struct rockchip_gem_object * + 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); @@ -123,6 +122,20 @@ struct rockchip_gem_object * drm_gem_private_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; @@ -140,13 +153,18 @@ err_free_rk_obj: */ void rockchip_gem_free_object(struct drm_gem_object *obj) { - struct rockchip_gem_object *rk_obj; + struct drm_device *drm = obj->dev; + struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); drm_gem_free_mmap_offset(obj); - rk_obj = to_rockchip_obj(obj); - - rockchip_gem_free_buf(rk_obj); + if (rk_obj->sg) { + dma_unmap_sg(drm->dev, rk_obj->sg->sgl, rk_obj->sg->nents, + DMA_BIDIRECTIONAL); + drm_prime_gem_destroy(obj, rk_obj->sg); + } else { + rockchip_gem_free_buf(rk_obj); + } kfree(rk_obj); } @@ -289,6 +307,64 @@ 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; +} + + +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_gem_object *rk_obj; + size_t size = attach->dmabuf->size; + int count; + int ret; + + rk_obj = rockchip_gem_alloc_object(drm, size); + if (IS_ERR(rk_obj)) + return ERR_CAST(rk_obj); + + count = dma_map_sg(drm->dev, sg->sgl, sg->nents, + DMA_BIDIRECTIONAL); + + if (!count) { + ret = -EINVAL; + goto err_free_rk_obj; + } + + if (rockchip_sg_get_contiguous_size(sg, count) < size) { + DRM_ERROR("failed to map sg_table to contiguous linear address.\n"); + dma_unmap_sg(drm->dev, sg->sgl, sg->nents, + DMA_BIDIRECTIONAL); + ret = -EINVAL; + goto err_free_rk_obj; + } + + rk_obj->dma_addr = sg_dma_address(sg->sgl); + rk_obj->sg = sg; + + return &rk_obj->base; + +err_free_rk_obj: + kfree(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 d5d8010..b6dc3b1 100644 --- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h +++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h @@ -17,6 +17,8 @@ #define to_rockchip_obj(x) container_of(x, struct rockchip_gem_object, base) +struct sg_table; + struct rockchip_gem_object { struct drm_gem_object base; unsigned int flags; @@ -24,11 +26,14 @@ struct rockchip_gem_object { void *kvaddr; dma_addr_t dma_addr; struct dma_attrs dma_attrs; + + struct sg_table *sg; }; 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, +rockchip_gem_prime_import_sg_table(struct drm_device *dev, + struct dma_buf_attachment *attach, struct sg_table *sgt); void *rockchip_gem_prime_vmap(struct drm_gem_object *obj); void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);