From patchwork Tue May 14 14:05:14 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: 10943053 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 60647924 for ; Tue, 14 May 2019 14:05:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F4DC285FF for ; Tue, 14 May 2019 14:05:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4368028707; Tue, 14 May 2019 14:05:27 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED 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 E426B285FF for ; Tue, 14 May 2019 14:05:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8F239892CA; Tue, 14 May 2019 14:05:22 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@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 E9222892C2 for ; Tue, 14 May 2019 14:05:20 +0000 (UTC) Received: by mail-wr1-x444.google.com with SMTP id r4so19360852wro.10 for ; Tue, 14 May 2019 07:05:20 -0700 (PDT) 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=2LlMHNFUcq8fzFvXVicCKCyvUVpZFdA179mEB64kOBk=; b=AP/iTxo1U3MAW5+wii4FA7DnzidQpOJ903N05lSE3NptBCUO9ChtKExXfay9LNoKEo poR8xPRllQlo/tsDgSoswui1VEthjxyRqOlPXZOSW6hlbCBoM2SMUBYlo+lbFsGR8s+C oXgGyq7wguwp1ZUdFrLfgZo9rynLPrmAYPH9GQwU79eoSdXusdgEH9wRzAs5hMrdsJ3u nzZgqRPDbdI75XXCqdRyhfT7GQl5YEZUa8/uNmz167OF1RQZsQZACMulXB2YO2Y/1eOM r5DEa12gtCB5cYJwb1QXAebvVkxUh4tedgMgSkSadUJ7ttxEeaGB9AnEjZSSZqQTDD/h 8g8w== X-Gm-Message-State: APjAAAULudG3vjsrpDj5xg7gZwrBLEamAMjM6d2tj0vkd+7VFLjHkbIk unzZYKW4Z/mS8+0ZOzV9dBpCu8Az X-Google-Smtp-Source: APXvYqw702W7sC8zE4MDga2yfCNBBZoCi0YfVvpX5M/mnWTAdajlFrXclSGWoNlZqPotoJyHhg13gQ== X-Received: by 2002:adf:f38a:: with SMTP id m10mr8933051wro.81.1557842719673; Tue, 14 May 2019 07:05:19 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:7d94:f6f9:368b:5a3b]) by smtp.gmail.com with ESMTPSA id c130sm8077113wmf.47.2019.05.14.07.05.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 May 2019 07:05:19 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: daniel@ffwll.ch, dri-devel@lists.freedesktop.org Subject: [PATCH 1/2] dma-buf: start caching of sg_table objects v2 Date: Tue, 14 May 2019 16:05:14 +0200 Message-Id: <20190514140515.35566-2-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190514140515.35566-1-christian.koenig@amd.com> References: <20190514140515.35566-1-christian.koenig@amd.com> MIME-Version: 1.0 X-Mailman-Original-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=2LlMHNFUcq8fzFvXVicCKCyvUVpZFdA179mEB64kOBk=; b=ODqDW2Re8+HAGARphkzrti/IfzxwC7ZzPl410WWpjE1J2PPZ935S3bxpARHYXboBQ0 KwGJU9IR3zuhTbypj3y8wnViqhb8LIsM1AU/qqLB5o9LP/1TLt4d+y4CyfSxqUaJN7x4 RpI6i77RWXClzzmFa/3AJd7qPYoW5uyJraWrlKgmMIOe3lsmIx22hQQ4YeeJcZX1rLeG Bss3I+kYqps4yTmwpUHXFkeuIxha8Zom2pkn64ER9tLBSGSeNFSjCUfyjgY2Rl95mohT tVsleMs/INjcKgvlvTyANSCzihtDNjv//0VAYpW7qB8kYYm0a9gK/cfxl7wBas9hHGTy KHiw== 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 To allow a smooth transition from pinning buffer objects to dynamic invalidation we first start to cache the sg_table for an attachment. v2: keep closer to the DRM implementation Signed-off-by: Christian König --- drivers/dma-buf/dma-buf.c | 27 +++++++++++++++++++++++++-- include/linux/dma-buf.h | 13 +++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 7c858020d14b..a2d34c6b80a5 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -573,6 +573,7 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, list_add(&attach->node, &dmabuf->attachments); mutex_unlock(&dmabuf->lock); + return attach; err_attach: @@ -595,6 +596,9 @@ 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, attach->dir); + mutex_lock(&dmabuf->lock); list_del(&attach->node); if (dmabuf->ops->detach) @@ -630,10 +634,27 @@ 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) { + /* + * Two mappings with different directions for the same + * attachment are not allowed. + */ + if (attach->dir != direction && + attach->dir != DMA_BIDIRECTIONAL) + return ERR_PTR(-EBUSY); + + return attach->sgt; + } + sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); if (!sg_table) sg_table = ERR_PTR(-ENOMEM); + if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) { + attach->sgt = sg_table; + attach->dir = direction; + } + return sg_table; } EXPORT_SYMBOL_GPL(dma_buf_map_attachment); @@ -657,8 +678,10 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) return; - attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, - direction); + if (attach->sgt == sg_table) + return; + + attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); } EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment); diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 58725f890b5b..45b9767e67ec 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -51,6 +51,15 @@ struct dma_buf_attachment; * @vunmap: [optional] unmaps a vmap from the buffer */ struct dma_buf_ops { + /** + * @cache_sgt_mapping: + * + * If true the framework will cache the first mapping made for each + * attachment. This avoids creating mappings for attachments multiple + * times. + */ + bool cache_sgt_mapping; + /** * @attach: * @@ -307,6 +316,8 @@ struct dma_buf { * @dmabuf: buffer for this attachment. * @dev: device attached to the buffer. * @node: list of dma_buf_attachment. + * @sgt: cached mapping. + * @dir: direction of cached mapping. * @priv: exporter specific attachment data. * * This structure holds the attachment information between the dma_buf buffer @@ -322,6 +333,8 @@ struct dma_buf_attachment { struct dma_buf *dmabuf; struct device *dev; struct list_head node; + struct sg_table *sgt; + enum dma_data_direction dir; void *priv; };