Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3455048yba; Tue, 16 Apr 2019 11:40:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqwineluU0GXR3iuJ+2O6A/go6YWYlshITG/3CNVJBzA+TTjGTXxlQvZgZDKwyvIQL/UCOH4 X-Received: by 2002:a63:b811:: with SMTP id p17mr78283804pge.219.1555440039960; Tue, 16 Apr 2019 11:40:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555440039; cv=none; d=google.com; s=arc-20160816; b=XaKUUbWYRwFqat2VZtpP0xuWI7wcBfdHNCdl3TMYqymhuxaRQfZ7kIYUOYPUQ/4zab Tjw9AoYcQxV+gROjMHN3T+DEZNCHJ5mESHKt9kp5SW36UGOLe6EgHGUxxs7gofyTJJbf CGRYBPjbaAPyEOds3uKm5N1v+ZJsbaersdsvUdI9ka5RVyM98zWSBqlPeJMcadlrPKyv RtkPAbhtTxLrYKl6kX29zfuEfiC242+9V7Taa/MdQaWwQhYMjALE0Yeaj8zLEqTxb+0N U+7fSFtxK4TwbNHRSvsOgcOVRWgFHFVuWaFlbcTowTKKuIgrywJiOL42PsRY64KemrtA FRcQ== 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:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=M/kIjOqVGNv4vBgweSTRQ/BBvI/9hw4+zPVGvnTZMfc=; b=XxTY03lKEi4yT8KhDqMDSZjLR7LBDTITFW3P7UF60BajzqokIGsSQTg/NFOhNEWcgs SmJ7f0pvZb/azSWe+X2NlX+AXvqlFIe/dqG7ZVdgwMwLuH9yzhH+ahNPLAeyjn35J2WW vhn70319RQFGPoN0JVzJog2icDFvdqtec79h3nH5DDb24l+t4kaL3dRZdheY/a9Eh+oV E78/u9BQVv6/8IDn53Dev/drdl28geTf8uoXPH3WjWddRjCnZNG7poMS2B9Yr/DmfWv2 StYPGhAI7brWPfY+1jYx8hUnf4V7xqk19qeaLXYm17r2iIEzDQ8Q9ObPL/N5i9zjqe7H AD5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oNMJuMa5; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v64si29478031pgd.91.2019.04.16.11.40.24; Tue, 16 Apr 2019 11:40:39 -0700 (PDT) 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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oNMJuMa5; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730519AbfDPSj2 (ORCPT + 99 others); Tue, 16 Apr 2019 14:39:28 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:37040 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730403AbfDPSi6 (ORCPT ); Tue, 16 Apr 2019 14:38:58 -0400 Received: by mail-wr1-f68.google.com with SMTP id w10so28530945wrm.4; Tue, 16 Apr 2019 11:38:56 -0700 (PDT) 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=M/kIjOqVGNv4vBgweSTRQ/BBvI/9hw4+zPVGvnTZMfc=; b=oNMJuMa5GyVmvUDw5A5Gfv0UMNnC0M+zMSU/Qmk44BqywANc1tkyAc689yuXQYf4ql 6rWk9ANB4QO1AdurXSb25dMLVCQHCTn24K6+sXy1piiUkjhSJ1UmnhD9JeOzpXSusL/c sREJeQeSiOb1OQCp/f4EAZLhbywrb6q/S1c/rB0dPuF5RAX3tNBWZw0UqtX3lVOXe661 3eky9XVttJl46y4A44gT6lJud+Je0a0H3xTHrBYIE9VdHtCDlNNjL61xDEeC2kSvyU2J y8IV3zoBeD3bjywUFpoOGeSUMTSW+dLleL7674fT1DCpcZrx4s9RHkmxMN1RZFSlv+n6 7ckg== 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=M/kIjOqVGNv4vBgweSTRQ/BBvI/9hw4+zPVGvnTZMfc=; b=BvNQcKfHo3mGArXYvDHVWvfmcBMaQ3Rby2jLVvJQOwvJawZ46T1gksig5aifOP08hv 7a3KN/3dyEi5ZaRAIKbZ9ODSZaspSV9GpOgIsdGmwqS9LDs7xL62HyZrq/ripgwqDQhZ YEjzUAv7Ys9/0rhQRMbji3Clk0qO95dxdmxAC6XSf0cXZygSjfdCIoUhNiPACiXwiEON 04uW7pZucfuGsmreiU1BeMnITLtj1vWa5adhpiClEGpzS8pb7N1fRpv4VhOU35t74ult pJ6n8sAyq2Q40zASWxYDaTF/qnKSZxRDVzrzrb5eFDxNHkf+rHzq1e44Hl87cMEYjGWo nLyg== X-Gm-Message-State: APjAAAUR2WYRiauwmUwF5aRnakWbdyVAn2F2qmlei4NHTfkJ0Jbv6iWC f8Oy9CYWHaVjFG1ZeP/mst0= X-Received: by 2002:a5d:4f07:: with SMTP id c7mr25160351wru.104.1555439936203; Tue, 16 Apr 2019 11:38:56 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:fcfa:e34f:82c0:3f66]) by smtp.gmail.com with ESMTPSA id m13sm128823wmg.42.2019.04.16.11.38.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 11:38:55 -0700 (PDT) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: sumit.semwal@linaro.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org Subject: [PATCH 11/12] drm/amdgpu: add DMA-buf pin/unpin implementation Date: Tue, 16 Apr 2019 20:38:40 +0200 Message-Id: <20190416183841.1577-12-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190416183841.1577-1-christian.koenig@amd.com> References: <20190416183841.1577-1-christian.koenig@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Pin and unpin the DMA-buf exported BOs only on demand and invalidate all DMA-buf mappings when the underlying BO moves. Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 5 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c | 73 ++++++++++++++++++++-- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c index ec9e45004bff..fdb98eb562db 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 #include @@ -1192,6 +1193,10 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, amdgpu_bo_kunmap(abo); + if (abo->gem_base.dma_buf && !abo->gem_base.import_attach && + bo->mem.mem_type != TTM_PL_SYSTEM) + dma_buf_invalidate_mappings(abo->gem_base.dma_buf); + /* remember the eviction */ if (evict) atomic64_inc(&adev->num_evictions); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c index 56e2a606b9a1..40cd89271b20 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c @@ -209,6 +209,61 @@ __reservation_object_make_exclusive(struct reservation_object *obj) return -ENOMEM; } +/** + * amdgpu_gem_pin_dma_buf - &dma_buf_ops.pin_dma_buf implementation + * + * @dma_buf: DMA-buf to pin in memory + * + * Pin the BO which is backing the DMA-buf so that it can't move any more. + */ +static int amdgpu_gem_pin_dma_buf(struct dma_buf *dma_buf) +{ + struct drm_gem_object *obj = dma_buf->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + + /* pin buffer into GTT */ + return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT); +} + +/** + * amdgpu_gem_unpin_dma_buf - &dma_buf_ops.unpin_dma_buf implementation + * + * @dma_buf: DMA-buf to unpin + * + * Unpin a previously pinned BO to make it movable again. + */ +static void amdgpu_gem_unpin_dma_buf(struct dma_buf *dma_buf) +{ + struct drm_gem_object *obj = dma_buf->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + + amdgpu_bo_unpin(bo); +} + +/** + * amdgpu_gem_dma_buf_attach - &dma_buf_ops.attach implementation + * + * @dma_buf: DMA-buf we attach to + * @attach: DMA-buf attachment + * + * Returns: + * Always zero for success. + */ +static int amdgpu_gem_dma_buf_attach(struct dma_buf *dma_buf, + struct dma_buf_attachment *attach) +{ + struct drm_gem_object *obj = dma_buf->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + + /* Make sure the buffer is pinned when userspace didn't set GTT as + * preferred domain. This avoid ping/pong situations with scan out BOs. + */ + if (!(bo->preferred_domains & AMDGPU_GEM_DOMAIN_GTT)) + attach->invalidate = NULL; + + return 0; +} + /** * amdgpu_gem_map_dma_buf - &dma_buf_ops.map_dma_buf implementation * @attach: DMA-buf attachment @@ -247,10 +302,15 @@ amdgpu_gem_map_dma_buf(struct dma_buf_attachment *attach, return ERR_PTR(r); } - /* pin buffer into GTT */ - r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT); - if (r) - return ERR_PTR(r); + if (attach->invalidate) { + /* 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); + } sgt = drm_prime_pages_to_sg(bo->tbo.ttm->pages, bo->tbo.num_pages); if (IS_ERR(sgt)) @@ -289,8 +349,6 @@ static void amdgpu_gem_unmap_dma_buf(struct dma_buf_attachment *attach, struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); - amdgpu_bo_unpin(bo); - if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count) bo->prime_shared_count--; @@ -357,6 +415,9 @@ static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf, const struct dma_buf_ops amdgpu_dmabuf_ops = { .dynamic_sgt_mapping = true, + .attach = amdgpu_gem_dma_buf_attach, + .pin = amdgpu_gem_pin_dma_buf, + .unpin = amdgpu_gem_unpin_dma_buf, .map_dma_buf = amdgpu_gem_map_dma_buf, .unmap_dma_buf = amdgpu_gem_unmap_dma_buf, .release = drm_gem_dmabuf_release, -- 2.17.1