From patchwork Fri Mar 9 19:11:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 10271811 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 F2FF560236 for ; Fri, 9 Mar 2018 19:12:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E33EB29FA6 for ; Fri, 9 Mar 2018 19:12:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E1B9129FBF; Fri, 9 Mar 2018 19:12:02 +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 9EFD229FA9 for ; Fri, 9 Mar 2018 19:11:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6426C6EC07; Fri, 9 Mar 2018 19:11:53 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x241.google.com (mail-wr0-x241.google.com [IPv6:2a00:1450:400c:c0c::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id 425B66EC07; Fri, 9 Mar 2018 19:11:52 +0000 (UTC) Received: by mail-wr0-x241.google.com with SMTP id k9so9990935wre.9; Fri, 09 Mar 2018 11:11:52 -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 :mime-version:content-transfer-encoding; bh=vnLzAqU62QwmYbf8Upb/KVJrJzfcYp3cFITMgCaurC4=; b=qjigpmX0QdBuM6t7pV73KiIFfjvyUMUuSTvj/Ra+LFL4pB2fUmWXOvdcH1XB8rwPNY vDE1xyOIzVXViQKX85EDeQ8EWi84wats5xrG6rpxxIeBibCA8g5HDVXm+DNIQ5z2nt+W 7gEg2f+ex/DDswO+61x2etPScfd2aOWpQsBdHF7jyC0gd7FGQYkkPtSBGQ/j1U4YGu1K BUb9yol5Q+iK9lJYV7x4b/y6FvLQiFfgTVnahsY+8JkOvtI6GjOtxBkue5jQmN/qP6/h fv5HMln8bm1tys7mZJi2es+wcvQWb7lM6X/13ex11/U7o+j8BRMnVdYbsC+FdXkJzbQ7 1XdA== 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:mime-version:content-transfer-encoding; bh=vnLzAqU62QwmYbf8Upb/KVJrJzfcYp3cFITMgCaurC4=; b=TwJlQaU6Rmo9eEMCLTgrPiTWt2M2szEDHyI2nOlZeamHGT4+5QviSdgnCx6lLhf838 3zlOvsjSBxyiLHiYuQ5aNWs9NAj7ex9kqExwvTZZx8rnjEYpqs3zQjnpnCW/9z283xl7 PJRS3crFq7u+/kqw+1tdtzqbTl4qMHAPJrbueXG777UX38ND3BTOAlTM812Sbc+j4V+I 9rl73RRbyKaiKEXLyL6q6YzY89OPaqd7xjsuhj4PJog/Ups3APU8p4CLuIWzJWBCcRcS m9HGJ+gP4hWPX27+Bo7FMOJR74/dqbfEKgqBC4xEti8RvKtqkmcfy6WTGEQP4CRuF706 C2kg== X-Gm-Message-State: APf1xPAJav2LO3+c1Fnmxm2kOyUKIGj4YTxya7y/BUyKPrpuWpcYITWH wPL8dfNbI6ukiA+EPsAuhQI= X-Google-Smtp-Source: AG47ELuTBJPeCihANpmvMuEXBYvq4RqQI384GqYxVBvaw36qMrMFB7k1VGHrpucIILPivxbC7UhAsg== X-Received: by 10.223.209.197 with SMTP id m5mr26929701wri.121.1520622710724; Fri, 09 Mar 2018 11:11:50 -0800 (PST) Received: from baker.fritz.box ([2a02:908:1251:8fc0:ccef:4aaf:cf58:8fe7]) by smtp.gmail.com with ESMTPSA id o12sm1811922wrb.15.2018.03.09.11.11.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Mar 2018 11:11:50 -0800 (PST) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org Subject: [PATCH 4/4] drm/amdgpu: add independent DMA-buf import Date: Fri, 9 Mar 2018 20:11:44 +0100 Message-Id: <20180309191144.1817-5-christian.koenig@amd.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180309191144.1817-1-christian.koenig@amd.com> References: <20180309191144.1817-1-christian.koenig@amd.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Instead of relying on the DRM functions just implement our own import functions. This adds support for taking care of unpinned DMA-buf. Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c | 64 +++++++++++++++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 32 +++++++++++++--- 2 files changed, 80 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c index f575fb46d7a8..7963ce329519 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c @@ -298,22 +298,64 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev, return buf; } +static void +amdgpu_gem_prime_invalidate_mappings(struct dma_buf_attachment *attach) +{ + struct ttm_operation_ctx ctx = { false, false }; + struct drm_gem_object *obj = attach->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + int r; + + amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU); + r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); + if (r) + DRM_ERROR("Failed to unmap DMA-buf import (%d))\n", r); +} + struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf) { + struct dma_buf_attachment *attach; struct drm_gem_object *obj; + int ret; - if (dma_buf->ops == &amdgpu_dmabuf_ops) { - obj = dma_buf->priv; - if (obj->dev == dev) { - /* - * Importing dmabuf exported from out own gem increases - * refcount on gem itself instead of f_count of dmabuf. - */ - drm_gem_object_get(obj); - return obj; - } + if (dma_buf->ops != &amdgpu_dmabuf_ops) + return drm_gem_prime_import(dev, dma_buf); + + obj = dma_buf->priv; + if (obj->dev == dev) { + /* + * Importing dmabuf exported from out own gem increases + * refcount on gem itself instead of f_count of dmabuf. + */ + drm_gem_object_get(obj); + return obj; + } + + /* + * Attach, but don't map other amdgpu BOs + */ + attach = dma_buf_attach(dma_buf, dev->dev); + if (IS_ERR(attach)) + return ERR_CAST(attach); + + get_dma_buf(dma_buf); + + obj = amdgpu_gem_prime_import_sg_table(dev, attach, NULL); + if (IS_ERR(obj)) { + ret = PTR_ERR(obj); + goto fail_detach; } - return drm_gem_prime_import(dev, dma_buf); + obj->import_attach = attach; + attach->invalidate_mappings = amdgpu_gem_prime_invalidate_mappings; + attach->priv = obj; + + return obj; + +fail_detach: + dma_buf_detach(dma_buf, attach); + dma_buf_put(dma_buf); + + return ERR_PTR(ret); } diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index 291dd3d600cd..aeead0281e92 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c @@ -43,6 +43,7 @@ #include #include #include +#include #include "amdgpu.h" #include "amdgpu_object.h" #include "amdgpu_trace.h" @@ -685,6 +686,7 @@ struct amdgpu_ttm_gup_task_list { struct amdgpu_ttm_tt { struct ttm_dma_tt ttm; + struct amdgpu_bo *bo; u64 offset; uint64_t userptr; struct mm_struct *usermm; @@ -993,6 +995,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo, return NULL; } gtt->ttm.ttm.func = &amdgpu_backend_func; + gtt->bo = ttm_to_amdgpu_bo(bo); if (ttm_sg_tt_init(>t->ttm, bo, page_flags)) { kfree(gtt); return NULL; @@ -1005,7 +1008,6 @@ static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm, { struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev); struct amdgpu_ttm_tt *gtt = (void *)ttm; - bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); if (gtt && gtt->userptr) { ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL); @@ -1017,7 +1019,19 @@ static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm, return 0; } - if (slave && ttm->sg) { + if (ttm->page_flags & TTM_PAGE_FLAG_SG) { + if (!ttm->sg) { + struct dma_buf_attachment *attach; + struct sg_table *sgt; + + attach = gtt->bo->gem_base.import_attach; + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); + if (IS_ERR(sgt)) + return PTR_ERR(sgt); + + ttm->sg = sgt; + } + drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, gtt->ttm.dma_address, ttm->num_pages); @@ -1036,9 +1050,8 @@ static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm, static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm) { - struct amdgpu_device *adev; struct amdgpu_ttm_tt *gtt = (void *)ttm; - bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); + struct amdgpu_device *adev; if (gtt && gtt->userptr) { amdgpu_ttm_tt_set_user_pages(ttm, NULL); @@ -1047,7 +1060,16 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm) return; } - if (slave) + if (ttm->sg && !gtt->bo->tbo.sg) { + struct dma_buf_attachment *attach; + + attach = gtt->bo->gem_base.import_attach; + dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL); + ttm->sg = NULL; + return; + } + + if (ttm->page_flags & TTM_PAGE_FLAG_SG) return; adev = amdgpu_ttm_adev(ttm->bdev);