From patchwork Thu Jan 11 21:44:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 10158651 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1803D605F8 for ; Thu, 11 Jan 2018 21:44:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 05C02286EF for ; Thu, 11 Jan 2018 21:44:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EE4012874F; Thu, 11 Jan 2018 21:44:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 300BB286EF for ; Thu, 11 Jan 2018 21:44:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B24496E4A5; Thu, 11 Jan 2018 21:44:30 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-qt0-x244.google.com (mail-qt0-x244.google.com [IPv6:2607:f8b0:400d:c0d::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id A84506E49E; Thu, 11 Jan 2018 21:44:28 +0000 (UTC) Received: by mail-qt0-x244.google.com with SMTP id k19so3720605qtj.6; Thu, 11 Jan 2018 13:44:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OUOtRJp3d+BZw/g/0k6q60ghTdmIXpflbZVFY6L+apc=; b=Lln5URRmhKawWe5zUjBCJYHurIIpElIu7ql6Yx/6Zd7qOcmTBmMoeLJT6BGo0LXlSd Uiij6G26AQaWNcOMf/bUfFS9MicM8F/9rgSPi+scEadd/zUB9r0mrr0zK/ERpeV9teoQ NIxYeps9XvgQV1xKsNQ21omzomzUPGW9TQ4Ht2xJOIk277m2elQOntSORgvrD4LWInI1 D26IGcIpy0f6p4BKolGZ/GuEIW27IllwaxVMTrdRSqBIwqqkalxzCa1Q5eGkZjozZWpH +dOhr9Lr2kRx9BaXPabX1elMJJZpsM1cgz4DPZM8ecOQj2aeux+Ao5qTcBxQq8MuXRve mTIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OUOtRJp3d+BZw/g/0k6q60ghTdmIXpflbZVFY6L+apc=; b=TLtQYrHIveVui81OSUTIic2Nv9BTsNxfCpjiuMIdhMbHTaiTZIDGuPN4kE5gqutSAz h8o6ck67SR1k3LpZeWjoVq4hswfqpI9Xp8sRdSITDBeGpw2RPRnptPsfAC9egwGjDwS9 eukqb4+s7QOwR10UEZ3YrsCXCz9FpsbvSUnhUbmE7Kgd+10wGhWYjO9q1IywwZ8u2Ak+ UDu1rNwr8PmXPzrwCydEWlXuciiMJz+JdukFeQI0z3eK2mO3+nJJ3/6qsOHUJ1FuMkEU 8o3sItgVEPlqTQjVjUrih2r0XOVZY3UO9BZb6T3kKCKQ9bqmRlQUdYYRAhgEbGmptB9D XfPw== X-Gm-Message-State: AKwxytd5N51QiPiC9dBthMv+/aftsHTedZDjtKjJuvBlUaniQfKhF2Fv BJsnjyGINApDOm5K9hTfkLk= X-Google-Smtp-Source: ACJfBouDDr1P7c77cHpAWKgV8Ksp0pbS6wu8b1cW6q8oyaQrmWkpn/hVM3mfzTRxdCArgQTMp12+Vg== X-Received: by 10.237.54.8 with SMTP id e8mr35435815qtb.25.1515707067639; Thu, 11 Jan 2018 13:44:27 -0800 (PST) Received: from localhost (p200300E41F21CF00227DB10B7BBDB590.dip0.t-ipconnect.de. [2003:e4:1f21:cf00:227d:b10b:7bbd:b590]) by smtp.gmail.com with ESMTPSA id e63sm12952388qkb.46.2018.01.11.13.44.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 11 Jan 2018 13:44:27 -0800 (PST) From: Thierry Reding To: Ben Skeggs Subject: [PATCH 3/5] drm/nouveau: Provide custom struct dma_buf_ops Date: Thu, 11 Jan 2018 22:44:18 +0100 Message-Id: <20180111214420.26203-3-thierry.reding@gmail.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180111214420.26203-1-thierry.reding@gmail.com> References: <20180111214420.26203-1-thierry.reding@gmail.com> Cc: linux-tegra@vger.kernel.org, Daniel Vetter , dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org 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-Virus-Scanned: ClamAV using ClamSMTP From: Thierry Reding Instead of relying on the DRM PRIME helpers to provide all of the DMA-BUF operations, provide a custom version of struct dma_buf_ops to allow additional functionality to be implemented. Signed-off-by: Thierry Reding --- drivers/gpu/drm/nouveau/nouveau_drm.c | 9 +- drivers/gpu/drm/nouveau/nouveau_gem.h | 15 +-- drivers/gpu/drm/nouveau/nouveau_prime.c | 157 +++++++++++++++++++++++++------- 3 files changed, 135 insertions(+), 46 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_drm.c b/drivers/gpu/drm/nouveau/nouveau_drm.c index 56fe261b6268..3ce2f02e9e58 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drm.c +++ b/drivers/gpu/drm/nouveau/nouveau_drm.c @@ -1081,15 +1081,12 @@ driver_stub = { .prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_fd_to_handle = drm_gem_prime_fd_to_handle, - .gem_prime_export = drm_gem_prime_export, - .gem_prime_import = drm_gem_prime_import, + .gem_prime_export = nouveau_gem_prime_export, + .gem_prime_import = nouveau_gem_prime_import, .gem_prime_pin = nouveau_gem_prime_pin, - .gem_prime_res_obj = nouveau_gem_prime_res_obj, .gem_prime_unpin = nouveau_gem_prime_unpin, + .gem_prime_res_obj = nouveau_gem_prime_res_obj, .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table, - .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table, - .gem_prime_vmap = nouveau_gem_prime_vmap, - .gem_prime_vunmap = nouveau_gem_prime_vunmap, .gem_free_object_unlocked = nouveau_gem_object_del, .gem_open_object = nouveau_gem_object_open, diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.h b/drivers/gpu/drm/nouveau/nouveau_gem.h index fe39998f65cc..eb55c1eb1d9f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_gem.h +++ b/drivers/gpu/drm/nouveau/nouveau_gem.h @@ -32,13 +32,14 @@ extern int nouveau_gem_ioctl_cpu_fini(struct drm_device *, void *, extern int nouveau_gem_ioctl_info(struct drm_device *, void *, struct drm_file *); -extern int nouveau_gem_prime_pin(struct drm_gem_object *); +struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev, + struct drm_gem_object *obj, + int flags); +struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *drm, + struct dma_buf *buf); +int nouveau_gem_prime_pin(struct drm_gem_object *obj); +void nouveau_gem_prime_unpin(struct drm_gem_object *obj); struct reservation_object *nouveau_gem_prime_res_obj(struct drm_gem_object *); -extern void nouveau_gem_prime_unpin(struct drm_gem_object *); -extern struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *); -extern struct drm_gem_object *nouveau_gem_prime_import_sg_table( - struct drm_device *, struct dma_buf_attachment *, struct sg_table *); -extern void *nouveau_gem_prime_vmap(struct drm_gem_object *); -extern void nouveau_gem_prime_vunmap(struct drm_gem_object *, void *); +struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj); #endif diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c index 1fefc93af1d7..63cf4aca2d8e 100644 --- a/drivers/gpu/drm/nouveau/nouveau_prime.c +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c @@ -23,69 +23,152 @@ */ #include +#include #include #include "nouveau_drv.h" #include "nouveau_gem.h" -struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj) +static void *nouveau_gem_prime_kmap_atomic(struct dma_buf *buf, + unsigned long page) { - struct nouveau_bo *nvbo = nouveau_gem_object(obj); - int npages = nvbo->bo.num_pages; + return NULL; +} - return drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages); +static void nouveau_gem_prime_kunmap_atomic(struct dma_buf *buf, + unsigned long page, void *addr) +{ } -void *nouveau_gem_prime_vmap(struct drm_gem_object *obj) +static void *nouveau_gem_prime_kmap(struct dma_buf *buf, unsigned long page) { - struct nouveau_bo *nvbo = nouveau_gem_object(obj); + return NULL; +} + +static void nouveau_gem_prime_kunmap(struct dma_buf *buf, unsigned long page, + void *addr) +{ +} + +static void *nouveau_gem_prime_vmap(struct dma_buf *buf) +{ + struct nouveau_bo *bo = nouveau_gem_object(buf->priv); int ret; - ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages, - &nvbo->dma_buf_vmap); + ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->dma_buf_vmap); if (ret) return ERR_PTR(ret); - return nvbo->dma_buf_vmap.virtual; + return bo->dma_buf_vmap.virtual; } -void nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) +static void nouveau_gem_prime_vunmap(struct dma_buf *buf, void *vaddr) { - struct nouveau_bo *nvbo = nouveau_gem_object(obj); + struct nouveau_bo *bo = nouveau_gem_object(buf->priv); + + ttm_bo_kunmap(&bo->dma_buf_vmap); +} + +static const struct dma_buf_ops nouveau_gem_prime_dmabuf_ops = { + .attach = drm_gem_map_attach, + .detach = drm_gem_map_detach, + .map_dma_buf = drm_gem_map_dma_buf, + .unmap_dma_buf = drm_gem_unmap_dma_buf, + .release = drm_gem_dmabuf_release, + .map_atomic = nouveau_gem_prime_kmap_atomic, + .unmap_atomic = nouveau_gem_prime_kunmap_atomic, + .map = nouveau_gem_prime_kmap, + .unmap = nouveau_gem_prime_kunmap, + .vmap = nouveau_gem_prime_vmap, + .vunmap = nouveau_gem_prime_vunmap, +}; + +struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev, + struct drm_gem_object *obj, + int flags) +{ + DEFINE_DMA_BUF_EXPORT_INFO(info); + + info.exp_name = KBUILD_MODNAME; + info.owner = dev->driver->fops->owner; + info.ops = &nouveau_gem_prime_dmabuf_ops; + info.size = obj->size; + info.flags = flags; + info.priv = obj; + + if (dev->driver->gem_prime_res_obj) + info.resv = dev->driver->gem_prime_res_obj(obj); - ttm_bo_kunmap(&nvbo->dma_buf_vmap); + return drm_gem_dmabuf_export(dev, &info); } -struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev, - struct dma_buf_attachment *attach, - struct sg_table *sg) +struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev, + struct dma_buf *buf) { struct nouveau_drm *drm = nouveau_drm(dev); - struct nouveau_bo *nvbo; - struct reservation_object *robj = attach->dmabuf->resv; - u32 flags = 0; + struct dma_buf_attachment *attach; + struct drm_gem_object *obj; + u32 flags = TTM_PL_FLAG_TT; + struct nouveau_bo *bo; + struct sg_table *sgt; int ret; - flags = TTM_PL_FLAG_TT; + if (buf->ops == &nouveau_gem_prime_dmabuf_ops) { + obj = buf->priv; + + if (obj->dev == dev) { + /* + * Importing a DMA-BUF exported from our own GEM + * increases the reference count on the GEM itself + * instead of the f_count of the DMA-BUF. + */ + drm_gem_object_get(obj); + return obj; + } + } - ww_mutex_lock(&robj->lock, NULL); - ret = nouveau_bo_new(&drm->client, attach->dmabuf->size, 0, flags, 0, 0, - sg, robj, &nvbo); - ww_mutex_unlock(&robj->lock); - if (ret) - return ERR_PTR(ret); + attach = dma_buf_attach(buf, dev->dev); + if (IS_ERR(attach)) + return ERR_CAST(attach); - nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART; + get_dma_buf(buf); - /* Initialize the embedded gem-object. We return a single gem-reference - * to the caller, instead of a normal nouveau_bo ttm reference. */ - ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size); - if (ret) { - nouveau_bo_ref(NULL, &nvbo); - return ERR_PTR(-ENOMEM); + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); + if (IS_ERR(sgt)) { + ret = PTR_ERR(sgt); + goto detach; } - return &nvbo->gem; + ww_mutex_lock(&attach->dmabuf->resv->lock, NULL); + ret = nouveau_bo_new(&drm->client, attach->dmabuf->size, 0, flags, 0, + 0, sgt, attach->dmabuf->resv, &bo); + ww_mutex_unlock(&attach->dmabuf->resv->lock); + if (ret) + goto unmap; + + bo->valid_domains = NOUVEAU_GEM_DOMAIN_GART; + + /* + * Initialize the embedded GEM object. We return a single GEM reference + * to the caller, instead of a normal nouveau_bo TTM reference. + */ + ret = drm_gem_object_init(dev, &bo->gem, bo->bo.mem.size); + if (ret) + goto unref; + + bo->gem.import_attach = attach; + + return &bo->gem; + +unref: + nouveau_bo_ref(NULL, &bo); +unmap: + dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); +detach: + dma_buf_detach(buf, attach); + dma_buf_put(buf); + + return ERR_PTR(ret); } int nouveau_gem_prime_pin(struct drm_gem_object *obj) @@ -114,3 +197,11 @@ struct reservation_object *nouveau_gem_prime_res_obj(struct drm_gem_object *obj) return nvbo->bo.resv; } + +struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj) +{ + struct nouveau_bo *nvbo = nouveau_gem_object(obj); + int npages = nvbo->bo.num_pages; + + return drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages); +}