From patchwork Tue Apr 16 18:38:30 2019 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: 10903827 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E952A161F for ; Tue, 16 Apr 2019 18:40:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D5664286EC for ; Tue, 16 Apr 2019 18:40:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C73D428720; Tue, 16 Apr 2019 18:40:04 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7712C28720 for ; Tue, 16 Apr 2019 18:40:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730308AbfDPSit (ORCPT ); Tue, 16 Apr 2019 14:38:49 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43703 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730266AbfDPSit (ORCPT ); Tue, 16 Apr 2019 14:38:49 -0400 Received: by mail-wr1-f67.google.com with SMTP id k17so23794423wrx.10; Tue, 16 Apr 2019 11:38:48 -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=FQjGrY5BOcl8yYSAGB0H12JRjT7ZcBW9E7af5GsHYPk=; b=GAsCHbJLbkAY2QKeOH6xG7dbIdOQF8roD0mkA+N5KbhfZfOE7d13/I1tSthup4EHq+ uKGQs8YoK/PsJDmTmdhwG2uLXf9Hkrk2KB2Jc3cF9vfneK3FzsF1KPffq2TgtJb/V/AT e3fWZ1+ckhxtl/maSPZVK3CTZcwTv4wMB20zAuPLj393Z7v8nCdjOQL9a9UN4dL9M0sA fsbAyZnKD9yGZxMXXBxGGwOHTA+HzarYlbHHc5Cm8Buqfvkcf+uXVufUIZLLDvAOYTEM OFk+ZXMab6A8b7B7bnSpD6CAqh2OHklloPSif6HYgRjlGjN+b2BcUWjo67BGW8TNOBof m6MA== 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=FQjGrY5BOcl8yYSAGB0H12JRjT7ZcBW9E7af5GsHYPk=; b=Ddb05fRSTpPZlHlLA1mUPqiqLaVZ4XXClGRefukIPkpkBqdJuC7cUGbXaSIBmMyGyO qaRABxOG3Wq8hJzo8Y+VE7SRlXSsFjTjKD8rWorioos35pQ61loatTdJR7fEiQuA0by1 9lZ4TSvPMbquIlBBUp6htoYq1nEOxIrMm4tGpBlrDU6ibruW7hgqAtIkj/aaAQmYhaFu XbXQe4fbXrkkHV8lz7xplBTx6gy80Q6aWBUlMFW4SITxXLleBJI4mw5ktki+AMuVAoYt rE80GCoyXiAvLG28vm9XYKFZuVLlXvtApQXej9EnKFoGfZ8GjDJ5z6U5+D3Wq5PZZeDo 6LIw== X-Gm-Message-State: APjAAAXAYDW28+F8hbFz+/9RV9jrWkXeCVUxd/SumPN4esN2wnMxz2Rg cnG09x72nRS4VkT15objE3w= X-Google-Smtp-Source: APXvYqz9bGkrKovIQjBoaiEcUvlwH8O2iiEPhRcRzRKJ+bK5KogrUdwYMoNwoy0d5uWpMmxL3BzDxA== X-Received: by 2002:adf:e547:: with SMTP id z7mr56995337wrm.295.1555439927536; Tue, 16 Apr 2019 11:38:47 -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.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 11:38:47 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=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 01/12] dma-buf: add dynamic caching of sg_table Date: Tue, 16 Apr 2019 20:38:30 +0200 Message-Id: <20190416183841.1577-2-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 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP To allow a smooth transition from pinning buffer objects to dynamic invalidation we first start to cache the sg_table for an attachment unless the driver explicitly says to not do so. Signed-off-by: Christian König --- drivers/dma-buf/dma-buf.c | 24 ++++++++++++++++++++++++ include/linux/dma-buf.h | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 7c858020d14b..65161a82d4d5 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -573,6 +573,20 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, list_add(&attach->node, &dmabuf->attachments); mutex_unlock(&dmabuf->lock); + + if (!dmabuf->ops->dynamic_sgt_mapping) { + struct sg_table *sgt; + + sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL); + if (!sgt) + sgt = ERR_PTR(-ENOMEM); + if (IS_ERR(sgt)) { + dma_buf_detach(dmabuf, attach); + return ERR_CAST(sgt); + } + attach->sgt = sgt; + } + return attach; err_attach: @@ -595,6 +609,10 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) if (WARN_ON(!dmabuf || !attach)) return; + if (attach->sgt) + dmabuf->ops->unmap_dma_buf(attach, attach->sgt, + DMA_BIDIRECTIONAL); + mutex_lock(&dmabuf->lock); list_del(&attach->node); if (dmabuf->ops->detach) @@ -630,6 +648,9 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, if (WARN_ON(!attach || !attach->dmabuf)) return ERR_PTR(-EINVAL); + if (attach->sgt) + return attach->sgt; + sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); if (!sg_table) sg_table = ERR_PTR(-ENOMEM); @@ -657,6 +678,9 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) return; + if (attach->sgt == sg_table) + return; + attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); } diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 58725f890b5b..0d9c3c13c9fb 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -51,6 +51,16 @@ struct dma_buf_attachment; * @vunmap: [optional] unmaps a vmap from the buffer */ struct dma_buf_ops { + /** + * @dynamic_sgt_mapping: + * + * Flag controlling the caching of the sg_table in the DMA-buf helpers. + * If not set the sg_table is created during device attaching, if set + * the sg_table is created dynamically when dma_buf_map_attachment() is + * called. + */ + bool dynamic_sgt_mapping; + /** * @attach: * @@ -323,6 +333,7 @@ struct dma_buf_attachment { struct device *dev; struct list_head node; void *priv; + struct sg_table *sgt; }; /**