From patchwork Wed Mar 11 13:51:54 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: 210677 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD272C10F29 for ; Wed, 11 Mar 2020 13:52:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B409421D56 for ; Wed, 11 Mar 2020 13:52:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="r7MESjrL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729758AbgCKNwG (ORCPT ); Wed, 11 Mar 2020 09:52:06 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:50619 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729740AbgCKNwF (ORCPT ); Wed, 11 Mar 2020 09:52:05 -0400 Received: by mail-wm1-f66.google.com with SMTP id a5so2194187wmb.0 for ; Wed, 11 Mar 2020 06:52:03 -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=xEoFbydtA55bQRoTqtzVl54mwFx3SL0wixHy09grPa0=; b=r7MESjrLBguiuqkJWV5+QhG2ZuQSFkodxlA1aWbNaIXYzGHoRgec96ylrOYaaeMcSQ h3g9fCl8X+vf4W9OEJNZUn97AGKgc4JTxzWK6LeWdFf8qVMDJy6wwf2J9IDxPUc5D5ft jAMp5egEe2WKoUAqyLz7AcTr6SpCm0tBpbPcUClXcPRp9xyWSqv3I6ueKAnu8wfJnCey 57rUV5EWInn60cNMUJyI7KBKwAED8wMsCk1fC7Lu+WyPgfyn/9+AhRGeF+USeM1U9nZN YmMp41PV9WE/HlSyIkO2hHfqLJjZr0sHHqv5N1TC593JyqRaj2XMKu3KAlFNnQDvZ4hr oq7A== 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=xEoFbydtA55bQRoTqtzVl54mwFx3SL0wixHy09grPa0=; b=uDVBJDzMTtWkgxEDY72hAWjHtoxJLyAiRHOiMy0/0nFw2DOoyWfqIsciGu+U7Cgn1d y+kp0pKpDaKD3UchJoyDPP/a+HVA+jSOEwiYnF30toN3ji4T+2uavHCa9JOLqagT/J4F V1RhyMuOUGIjUx6OGAOfhZUj6PejW7+mWzvCcZ2u7tOPTQmt9wd0nUdBUc+LloMnxmmi lexhITyu1N8EaaWWARssmxVUpUYrPTdB6n+q+eHOZAhHjyfdwCElNg2VEU4WN9I0x2OG Fbp5n9S0TzM/JgnnCaOssN41vCVat4T1gPylhEbBf7v7AuRNTK1yQQTtiC+Yp5OOUrEJ Gtsg== X-Gm-Message-State: ANhLgQ26kpbDYZDTtuo1KOrlF+YrxjQMM17MldqPhcs5mx6943FroBB6 Y5Gg7mfGYPCj/alDUf4/X1M= X-Google-Smtp-Source: ADFU+vuIhHcOqb5hYvyJLF0VXhRNUC5gsOSa+CkV8rcXv1Lmvo1T20WArTWant8WOU2t0Mvf9O7HNQ== X-Received: by 2002:a05:600c:4013:: with SMTP id i19mr2532147wmm.28.1583934722955; Wed, 11 Mar 2020 06:52:02 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:8dc:e796:e7da:a319]) by smtp.gmail.com with ESMTPSA id 138sm3183811wmb.21.2020.03.11.06.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 06:52:02 -0700 (PDT) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: David1.Zhou@amd.com, hch@infradead.org, jgg@ziepe.ca, daniel@ffwll.ch, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, intel-gfx@lists.freedesktop.org Subject: [PATCH 2/6] dma-buf: add peer2peer flag Date: Wed, 11 Mar 2020 14:51:54 +0100 Message-Id: <20200311135158.3310-3-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200311135158.3310-1-christian.koenig@amd.com> References: <20200311135158.3310-1-christian.koenig@amd.com> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add a peer2peer flag noting that the importer can deal with device resources which are not backed by pages. Signed-off-by: Christian König --- drivers/dma-buf/dma-buf.c | 2 ++ include/linux/dma-buf.h | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index f4ace9af2191..f9220928ec90 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -689,6 +689,8 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, attach->dev = dev; attach->dmabuf = dmabuf; + if (importer_ops) + attach->peer2peer = importer_ops->allow_peer2peer; attach->importer_ops = importer_ops; attach->importer_priv = importer_priv; diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 1ade486fc2bb..82e0a4a64601 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -334,6 +334,14 @@ struct dma_buf { * Attachment operations implemented by the importer. */ struct dma_buf_attach_ops { + /** + * @allow_peer2peer: + * + * If this is set to true the importer must be able to handle peer + * resources without struct pages. + */ + bool allow_peer2peer; + /** * @move_notify * @@ -362,6 +370,7 @@ struct dma_buf_attach_ops { * @node: list of dma_buf_attachment, protected by dma_resv lock of the dmabuf. * @sgt: cached mapping. * @dir: direction of cached mapping. + * @peer2peer: true if the importer can handle peer resources without pages. * @priv: exporter specific attachment data. * @importer_ops: importer operations for this attachment, if provided * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held. @@ -382,6 +391,7 @@ struct dma_buf_attachment { struct list_head node; struct sg_table *sgt; enum dma_data_direction dir; + bool peer2peer; const struct dma_buf_attach_ops *importer_ops; void *importer_priv; void *priv; From patchwork Wed Mar 11 13:51:56 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: 210675 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C163EC10F27 for ; Wed, 11 Mar 2020 13:52:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 93A3121D56 for ; Wed, 11 Mar 2020 13:52:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dS70QwaB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729744AbgCKNwZ (ORCPT ); Wed, 11 Mar 2020 09:52:25 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:35902 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729748AbgCKNwG (ORCPT ); Wed, 11 Mar 2020 09:52:06 -0400 Received: by mail-wm1-f66.google.com with SMTP id g62so2235506wme.1 for ; Wed, 11 Mar 2020 06:52:06 -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=BrBZ7BRIxlz8FrRAnyr/unjtGHr0zUbPTL3/EC4XXLw=; b=dS70QwaBALfXqGFh/hPS8VDcp5/7jII+D3MVVverNaHTK0+3xxLLx5wdKoel7HDXvH 2arfrNFDrlq4xaO0fExSZFdqDXFqzM/swpL9Yh3vbc2a420rhI6Yf8H7uW/PYnHz0798 Ub6jVwv4dTq8r3RDb5Z1Wu8tkx23tosGteiETYrR/esHHoiQouzpRG5TBWDMnZg4Xv0d JZ8lW2kXCthRwELDrGi5AdQyu8Cd3mvP682JoPMNBkIoX4wIy9WiC8H7psnVj99+7vgq 1XQ5Oy2eNN+1260mvCQiVl9c8lRsZaa9vHb239GMQLVVtNFZOTD2lxsEDS2iijhG6Y1U 4wRQ== 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=BrBZ7BRIxlz8FrRAnyr/unjtGHr0zUbPTL3/EC4XXLw=; b=Wm+OO38E/Wku4lrFE7EretFRH90lF0nFG3zZJcYOJa9/5Mw6UcIccQlt9zW8k86rMg n1r8HV7ILzKyBwkWLvIS/T4m8upgBQw3XANeEtIzs8HHD0sqhiQNbTirmiL6I867FSte AnauTbB01SGMe7ta01A+VjbcoHA+tZNm7kZj4vgDSIigTmTnyi3YsDQpTUFM2pp+OPgj wVdm994UG1+z3M/LmjeId0BbLtnzpBT+rWttYyTtgZs4ErpHLLAsgVyOPkcBBHwEgrsY iAB8HxvwaG5szbawtJ0pZ4YxNoc0lj4x16YFrTXXWTrGZgSjQirpnb1bzlaqvbv3W8MO WWPw== X-Gm-Message-State: ANhLgQ2EeIppu/zEhzNQ4FVlmHLNDnvXxGVszZqTHb+599JeA1EVm9th RJyIiIasFwos1s2NGuwuB74= X-Google-Smtp-Source: ADFU+vsPAfdCms9K/oMpKj27gQSzAvyZZSi4wAa/XqnIbxORrNV6RGa/WoMb+HcXHpWWm6Gw162qsA== X-Received: by 2002:a1c:35c6:: with SMTP id c189mr3752519wma.108.1583934725433; Wed, 11 Mar 2020 06:52:05 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:8dc:e796:e7da:a319]) by smtp.gmail.com with ESMTPSA id 138sm3183811wmb.21.2020.03.11.06.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 06:52:04 -0700 (PDT) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: David1.Zhou@amd.com, hch@infradead.org, jgg@ziepe.ca, daniel@ffwll.ch, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, intel-gfx@lists.freedesktop.org Subject: [PATCH 4/6] drm/amdgpu: add checks if DMA-buf P2P is supported Date: Wed, 11 Mar 2020 14:51:56 +0100 Message-Id: <20200311135158.3310-5-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200311135158.3310-1-christian.koenig@amd.com> References: <20200311135158.3310-1-christian.koenig@amd.com> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Check if we can do peer2peer on the PCIe bus. Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c index aef12ee2f1e3..bbf67800c8a6 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c @@ -38,6 +38,7 @@ #include #include #include +#include /** * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation @@ -179,6 +180,9 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf, struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); int r; + if (pci_p2pdma_distance_many(adev->pdev, &attach->dev, 1, true) < 0) + attach->peer2peer = false; + if (attach->dev->driver == adev->dev->driver) return 0; From patchwork Wed Mar 11 13:51:57 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: 210676 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6DA8C18E5B for ; Wed, 11 Mar 2020 13:52:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A2F4921D56 for ; Wed, 11 Mar 2020 13:52:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Rev8PwV8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729782AbgCKNwK (ORCPT ); Wed, 11 Mar 2020 09:52:10 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:54032 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729737AbgCKNwI (ORCPT ); Wed, 11 Mar 2020 09:52:08 -0400 Received: by mail-wm1-f66.google.com with SMTP id 25so2175510wmk.3 for ; Wed, 11 Mar 2020 06:52:07 -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=2jcARVtU7yHOinId9qpR+Hw2dvEtPObOk+bGG1YCV/I=; b=Rev8PwV8NSO3oQAw2LPqgoEXhvX6BYGUxYbiA+lZkiM45h0DZY9dnfvap0AVX0PmrV GWOxV97wObC6ucxP2TzwGJYI70uLFztyWABHxbPC3RuwZQ7dOpqV7krX0OgYeM/ejPQC FM2XCzZOtBnmBOyL4rwEsS4803uB5w2op9/QGYmJ8VusjgwUkRXth0NHVC/8Y9PLZpzq +G5G8DmRDUPaI9gnYPq0ytyfwnWiUbzO5pPM4o1qGV465MDR3q35sYVQQMqzzm9k2gH7 YBjzps9jxuGiVQXmQS6ddaWTDNgrwY2hCqIbKRh4+wCQflbUAgQ7+fdaEE6qbYNR0WFQ jKnA== 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=2jcARVtU7yHOinId9qpR+Hw2dvEtPObOk+bGG1YCV/I=; b=qqtHeHKIt/i9H1VeXK5Uv39RKPMWnMKyd2KfC/1CovaG0tkefLKq7QP6z5Jw7h8yVk RFebsSTbJx0uBJ1vtvizy5I1ONeyd30o0X4yfwed94KynwZw3ct4JKAG3CsQ/5uqqR6B fK7isKldufeRCH0Q2gnMPL1lkQaYEo0iyMyzHyleMl+22Io3rMJ9Bq5cd6LBBgOJ4UYR HkIBW+lwXc/YAALld0fpko9OxSNLd0qOrb9MMo7zLjd4qYsRKKRC9D5t1g+4j8vwr1T1 +tI6r+TZ1skvRoBcoEO6+30rNSiNNED6tPI6CZ/6MDXRYkFTRqtTE//VWkHhMyxghyea KxJw== X-Gm-Message-State: ANhLgQ2/sQ1StqaXRt/9RJinl8zScqbo0lgPuSYlfv3B7Ya2mli2JIbv h0yN/uX5sZJXE83LHeA1wI8= X-Google-Smtp-Source: ADFU+vvUSYX5/OM5hcn0nhu2x1Tew/qLu46Zbs/YfGjEMxA7BATk4X93nC/7GYaJ7x30Kuv7UgiyNQ== X-Received: by 2002:a05:600c:258:: with SMTP id 24mr3917220wmj.59.1583934726458; Wed, 11 Mar 2020 06:52:06 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:8dc:e796:e7da:a319]) by smtp.gmail.com with ESMTPSA id 138sm3183811wmb.21.2020.03.11.06.52.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 06:52:06 -0700 (PDT) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: David1.Zhou@amd.com, hch@infradead.org, jgg@ziepe.ca, daniel@ffwll.ch, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, intel-gfx@lists.freedesktop.org Subject: [PATCH 5/6] drm/amdgpu: add support for exporting VRAM using DMA-buf v2 Date: Wed, 11 Mar 2020 14:51:57 +0100 Message-Id: <20200311135158.3310-6-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200311135158.3310-1-christian.koenig@amd.com> References: <20200311135158.3310-1-christian.koenig@amd.com> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org We should be able to do this now after checking all the prerequisites. v2: fix entrie count in the sgt Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 56 ++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 12 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 97 ++++++++++++++++++++ 3 files changed, 151 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c index bbf67800c8a6..43d8ed7dbd00 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c @@ -276,14 +276,21 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach, struct dma_buf *dma_buf = attach->dmabuf; struct drm_gem_object *obj = dma_buf->priv; struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); struct sg_table *sgt; long r; if (!bo->pin_count) { - /* move buffer into GTT */ + /* move buffer into GTT or VRAM */ struct ttm_operation_ctx ctx = { false, false }; + unsigned domains = AMDGPU_GEM_DOMAIN_GTT; - amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); + if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM && + attach->peer2peer) { + bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; + domains |= AMDGPU_GEM_DOMAIN_VRAM; + } + amdgpu_bo_placement_from_domain(bo, domains); r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); if (r) return ERR_PTR(r); @@ -293,20 +300,34 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach, return ERR_PTR(-EBUSY); } - sgt = drm_prime_pages_to_sg(bo->tbo.ttm->pages, bo->tbo.num_pages); - if (IS_ERR(sgt)) - return sgt; - - if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir, - DMA_ATTR_SKIP_CPU_SYNC)) - goto error_free; + switch (bo->tbo.mem.mem_type) { + case TTM_PL_TT: + sgt = drm_prime_pages_to_sg(bo->tbo.ttm->pages, + bo->tbo.num_pages); + if (IS_ERR(sgt)) + return sgt; + + if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir, + DMA_ATTR_SKIP_CPU_SYNC)) + goto error_free; + break; + + case TTM_PL_VRAM: + r = amdgpu_vram_mgr_alloc_sgt(adev, &bo->tbo.mem, attach->dev, + dir, &sgt); + if (r) + return ERR_PTR(r); + break; + default: + return ERR_PTR(-EINVAL); + } return sgt; error_free: sg_free_table(sgt); kfree(sgt); - return ERR_PTR(-ENOMEM); + return ERR_PTR(-EBUSY); } /** @@ -322,9 +343,18 @@ static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach, struct sg_table *sgt, enum dma_data_direction dir) { - dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir); - sg_free_table(sgt); - kfree(sgt); + struct dma_buf *dma_buf = attach->dmabuf; + struct drm_gem_object *obj = dma_buf->priv; + struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); + + if (sgt->sgl->page_link) { + dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir); + sg_free_table(sgt); + kfree(sgt); + } else { + amdgpu_vram_mgr_free_sgt(adev, attach->dev, dir, sgt); + } } /** diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h index 7551f3729445..a99d813b23a5 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h @@ -24,8 +24,9 @@ #ifndef __AMDGPU_TTM_H__ #define __AMDGPU_TTM_H__ -#include "amdgpu.h" +#include #include +#include "amdgpu.h" #define AMDGPU_PL_GDS (TTM_PL_PRIV + 0) #define AMDGPU_PL_GWS (TTM_PL_PRIV + 1) @@ -74,6 +75,15 @@ uint64_t amdgpu_gtt_mgr_usage(struct ttm_mem_type_manager *man); int amdgpu_gtt_mgr_recover(struct ttm_mem_type_manager *man); u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo); +int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, + struct ttm_mem_reg *mem, + struct device *dev, + enum dma_data_direction dir, + struct sg_table **sgt); +void amdgpu_vram_mgr_free_sgt(struct amdgpu_device *adev, + struct device *dev, + enum dma_data_direction dir, + struct sg_table *sgt); uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man); uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c index 82a3299e53c0..c6e7f00c5b21 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c @@ -22,6 +22,7 @@ * Authors: Christian König */ +#include #include "amdgpu.h" #include "amdgpu_vm.h" #include "amdgpu_atomfirmware.h" @@ -458,6 +459,102 @@ static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man, mem->mm_node = NULL; } +/** + * amdgpu_vram_mgr_alloc_sgt - allocate and fill a sg table + * + * @adev: amdgpu device pointer + * @mem: TTM memory object + * @dev: the other device + * @dir: dma direction + * @sgt: resulting sg table + * + * Allocate and fill a sg table from a VRAM allocation. + */ +int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, + struct ttm_mem_reg *mem, + struct device *dev, + enum dma_data_direction dir, + struct sg_table **sgt) +{ + struct drm_mm_node *node; + struct scatterlist *sg; + int num_entries = 0; + unsigned int pages; + int i, r; + + *sgt = kmalloc(sizeof(*sg), GFP_KERNEL); + if (!*sgt) + return -ENOMEM; + + for (pages = mem->num_pages, node = mem->mm_node; + pages; pages -= node->size, ++node) + ++num_entries; + + r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL); + if (r) + goto error_free; + + for_each_sg((*sgt)->sgl, sg, num_entries, i) + sg->length = 0; + + node = mem->mm_node; + for_each_sg((*sgt)->sgl, sg, num_entries, i) { + phys_addr_t phys = (node->start << PAGE_SHIFT) + + adev->gmc.aper_base; + size_t size = node->size << PAGE_SHIFT; + dma_addr_t addr; + + ++node; + addr = dma_map_resource(dev, phys, size, dir, + DMA_ATTR_SKIP_CPU_SYNC); + r = dma_mapping_error(dev, addr); + if (r) + goto error_unmap; + + sg_set_dma_addr(sg, addr, size, 0); + } + return 0; + +error_unmap: + for_each_sg((*sgt)->sgl, sg, num_entries, i) { + if (!sg->length) + continue; + + dma_unmap_resource(dev, sg->dma_address, + sg->length, dir, + DMA_ATTR_SKIP_CPU_SYNC); + } + sg_free_table(*sgt); + +error_free: + kfree(*sgt); + return r; +} + +/** + * amdgpu_vram_mgr_alloc_sgt - allocate and fill a sg table + * + * @adev: amdgpu device pointer + * @sgt: sg table to free + * + * Free a previously allocate sg table. + */ +void amdgpu_vram_mgr_free_sgt(struct amdgpu_device *adev, + struct device *dev, + enum dma_data_direction dir, + struct sg_table *sgt) +{ + struct scatterlist *sg; + int i; + + for_each_sg(sgt->sgl, sg, sgt->nents, i) + dma_unmap_resource(dev, sg->dma_address, + sg->length, dir, + DMA_ATTR_SKIP_CPU_SYNC); + sg_free_table(sgt); + kfree(sgt); +} + /** * amdgpu_vram_mgr_usage - how many bytes are used in this domain *