From patchwork Wed Feb 19 12:59:07 2020 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: 11392003 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 148A192A for ; Wed, 19 Feb 2020 16:56:06 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E6B012464E for ; Wed, 19 Feb 2020 16:56:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RBB/zrjI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E6B012464E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=intel-gfx-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2D98089F3B; Wed, 19 Feb 2020 16:56:02 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3B3786E5B2; Wed, 19 Feb 2020 12:59:16 +0000 (UTC) Received: by mail-wr1-x444.google.com with SMTP id t3so405635wru.7; Wed, 19 Feb 2020 04:59:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=L4Q/3oQgwK0gy+FT05AGDGX84O8UR+MiHQosGOKcELg=; b=RBB/zrjIr1dwXb8I+5UI0vwoe41f81rcHhsQx7bOx/gD4XgGp6pZNWcHn14XEZG5IB iSSuJRFuUnqFXMd3Khe+iRWKCHcM4n2xpFQnqlykulflhqksJ4Uyrp4aOo2VFgfb62Zc +z8oAuI8ZnsKB+IvXhSs1uZYngzckBAYIDsfvEuR7YK0s4jFl5C6pk48kFmmbz2fQVeY mcmVMlBRG9YJqclUmU9JU6QclQMn/BFQ34W57y0YIh83eSu+7VrWbyR2PtVVqMUcOcSq WNTtIrqCkuCJGucZ45jBM+v7Q7LTN4ygKikbTpmRj5tvEVgqbbKaScJV+VaowaoSLHAl Z79w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L4Q/3oQgwK0gy+FT05AGDGX84O8UR+MiHQosGOKcELg=; b=Jkuz7TnwjmSzjftwclrbXREPi/s7uKDqK8F/hoAgP/LMeG7QmeaJ58V46GwL+BjigP PI7gWOspDNhJBBhGZ7qTj04ba69fh686HtYsBt1KL5BBciwfiSA4qykUwKeU/eVlcRuG rwNjpb9cslssXBh1FpGzV+MUm6y+2BL+XnP9x65iXhtR91q4V16yyHNh6k7QTO2hD82F YjVtK8560SUpIXgBtcH5QyUDcrDCkCgCw1GPm5AC+VOBCNpzGcsXscA5ymN5/vyLuhdk LI2OSCO5hFpyFr3LO0ZlAa6na2jR8rHwqGBee3s76tzEusgPT7mZCD7wjKPEQYQuQbbN s8yA== X-Gm-Message-State: APjAAAU1o27t9xAcgd946RZ0UR0gDG9xY4x1igLbJrn4ZVtiMw0rOXYY T/QfBfnBsQ4aXCZXxQiJ4Ew1Fr7l X-Google-Smtp-Source: APXvYqzpPRKe4aTBgdEvsOC7KBRfOYRIWa91GiCQmxAKibydi60pfA01AKZSEWCmw2Bi+N2RdKUYMg== X-Received: by 2002:a5d:638f:: with SMTP id p15mr35502478wru.402.1582117154365; Wed, 19 Feb 2020 04:59:14 -0800 (PST) Received: from abel.fritz.box ([2a02:908:1252:fb60:90ea:9d7:18a3:895e]) by smtp.gmail.com with ESMTPSA id h2sm3079024wrt.45.2020.02.19.04.59.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2020 04:59:13 -0800 (PST) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, intel-gfx@lists.freedesktop.org, daniel@ffwll.ch Date: Wed, 19 Feb 2020 13:59:07 +0100 Message-Id: <20200219125910.89147-4-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200219125910.89147-1-christian.koenig@amd.com> References: <20200219125910.89147-1-christian.koenig@amd.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 19 Feb 2020 16:56:01 +0000 Subject: [Intel-gfx] [PATCH 4/7] drm/amdgpu: add amdgpu_dma_buf_pin/unpin v2 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" This implements the exporter side of unpinned DMA-buf handling. v2: fix minor coding style issues Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 53 ++++++++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 5 ++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c index 7cafc65fd76a..86000c75b133 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c @@ -222,6 +222,37 @@ static void amdgpu_dma_buf_detach(struct dma_buf *dmabuf, bo->prime_shared_count--; } +/** + * amdgpu_dma_buf_pin - &dma_buf_ops.pin implementation + * + * @attach: attachment to pin down + * + * Pin the BO which is backing the DMA-buf so that it can't move any more. + */ +static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach) +{ + struct drm_gem_object *obj = attach->dmabuf->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + + /* pin buffer into GTT */ + return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT); +} + +/** + * amdgpu_dma_buf_unpin - &dma_buf_ops.unpin implementation + * + * @attach: attachment to unpin + * + * Unpin a previously pinned BO to make it movable again. + */ +static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach) +{ + struct drm_gem_object *obj = attach->dmabuf->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + + amdgpu_bo_unpin(bo); +} + /** * amdgpu_dma_buf_map - &dma_buf_ops.map_dma_buf implementation * @attach: DMA-buf attachment @@ -244,9 +275,19 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach, struct sg_table *sgt; long r; - r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT); - if (r) - return ERR_PTR(r); + if (!bo->pin_count) { + /* move buffer into GTT */ + struct ttm_operation_ctx ctx = { false, false }; + + amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); + r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); + if (r) + return ERR_PTR(r); + + } else if (!(amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type) & + AMDGPU_GEM_DOMAIN_GTT)) { + return ERR_PTR(-EBUSY); + } sgt = drm_prime_pages_to_sg(bo->tbo.ttm->pages, bo->tbo.num_pages); if (IS_ERR(sgt)) @@ -277,13 +318,9 @@ static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach, struct sg_table *sgt, enum dma_data_direction dir) { - struct drm_gem_object *obj = attach->dmabuf->priv; - struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); - dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir); sg_free_table(sgt); kfree(sgt); - amdgpu_bo_unpin(bo); } /** @@ -330,6 +367,8 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = { .dynamic_mapping = true, .attach = amdgpu_dma_buf_attach, .detach = amdgpu_dma_buf_detach, + .pin = amdgpu_dma_buf_pin, + .unpin = amdgpu_dma_buf_unpin, .map_dma_buf = amdgpu_dma_buf_map, .unmap_dma_buf = amdgpu_dma_buf_unmap, .release = drm_gem_dmabuf_release, diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c index e3f16b49e970..9de8374bbbab 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c @@ -31,6 +31,7 @@ */ #include #include +#include #include #include @@ -1274,6 +1275,10 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, amdgpu_bo_kunmap(abo); + if (abo->tbo.base.dma_buf && !abo->tbo.base.import_attach && + bo->mem.mem_type != TTM_PL_SYSTEM) + dma_buf_move_notify(abo->tbo.base.dma_buf); + /* remember the eviction */ if (evict) atomic64_inc(&adev->num_evictions);