Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3093392pxb; Mon, 1 Mar 2021 01:07:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJzN4dZPxPz7tbifOTN386uIK10DMNv7obyixvNztnXrbXQTjm4cvS9jcEZ4gZa4bMSJSry1 X-Received: by 2002:aa7:d41a:: with SMTP id z26mr15165228edq.359.1614589658678; Mon, 01 Mar 2021 01:07:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614589658; cv=none; d=google.com; s=arc-20160816; b=ySxnSoKh60cYJo/NGcu/lXQGjdKHcJm5Qy5YjijNK7t0WQ5tzkix6hGADbhdB6g6N4 qHjSvYxdpzI79zWlZaTnOjU7d89lohCAHby9Tgnsn6gSBFp72BKAbqTKp5YfbVirqg20 bGOzQVFOdrLPLu8BBfPyQtKMgmwOnz88t0lJSbftf8iTMRnqoT1ChMc/hqdKLjETjBTf ohWjGMvNNtg+V+99j+TjbC2ci0Bl0wiuJS7O4YHIfaGls0xA+kHWT8U0jSF0ZpAXA7Rc Ks2brJMBzzVjeWuyjVZ9xyhMpBD8TkbOWl/sBd8riwwt4zS6fmU/XzIOy7AiaZ1ZKceA lmkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=FpiyQH/WZsneXAYudL4P437T+laVaJ1w/DKPG1+Xs5M=; b=Pi00cIiVGIk/gWR+V5E+2tHD5COvGVLf8orMd33kl6eS+BNQrzyq9evjtDvgaj7AKy /QkhL6YqXkAh0lbp7E8HUQnnV/lLEBdxAvEwxreNeUhWo+rzAjG9axTsFaitBPwbwYXd Z8KGmc/TF96BzZJqJkwomwrDPa+QX/2X7jH9ZRObDTM+p/EqKNHteiMXtXx7zOdzKBo4 8myBElMRdbPmUq+n5X8LjdZbvbIJsZfilEgR91NuZYwi5y4eDvEV+GCwvWox9Q0NXA/f JrbWjGZ/gTzhs041H4KsZjG0TrBwP6GFw6GGFzljPWZ/mw4pYCaovUhlqP2Te2uKiQjD 9big== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=knEBVjSJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i19si9930092edt.336.2021.03.01.01.07.15; Mon, 01 Mar 2021 01:07:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=knEBVjSJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233622AbhCAJBl (ORCPT + 99 others); Mon, 1 Mar 2021 04:01:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233700AbhCAI4D (ORCPT ); Mon, 1 Mar 2021 03:56:03 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 773A0C061788; Mon, 1 Mar 2021 00:55:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=FpiyQH/WZsneXAYudL4P437T+laVaJ1w/DKPG1+Xs5M=; b=knEBVjSJ6l72ReR9UB3FsNA6Qa WEIhkOFFDmsvaRLMNiUoiQCu11eTBvxaWZRnD3KGRsnqQDR82Ts1jcNN7dpkPuXE0twmcRr20npyU ut3zAR45gcey0R7fAHp5aMIIA2rD+zdT8uyZuhcP8Ni8Zcb/gvBOfomMJdJjA9HD8bfW2uL6l0x6V GlYvqeYXyHav7NIaElgDSh/Eg9Pjs6lRBO4600cTM9oePUHgwub232EwQNH7xeRFr7w5Cx+gcVIj6 8SbPTHMXXkFR3UJqaC4zxL/UFeus3e9F0IPAZ1di4q9L9lILrP4FJWIEcrQivZSEex76JQHc524Cm N85UdDUw==; Received: from [2001:4bb8:19b:e4b7:cdf9:733f:4874:8eb4] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lGeKM-00FVT4-3x; Mon, 01 Mar 2021 08:54:55 +0000 From: Christoph Hellwig To: Mauro Carvalho Chehab , Marek Szyprowski , Tomasz Figa , Ricardo Ribalda , Sergey Senozhatsky , Laurent Pinchart , iommu@lists.linux-foundation.org Cc: Robin Murphy , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org Subject: [PATCH 5/6] dma-iommu: implement ->alloc_noncontiguous Date: Mon, 1 Mar 2021 09:52:35 +0100 Message-Id: <20210301085236.947011-6-hch@lst.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210301085236.947011-1-hch@lst.de> References: <20210301085236.947011-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement support for allocating a non-contiguous DMA region. Signed-off-by: Christoph Hellwig Reviewed-by: Tomasz Figa Tested-by: Ricardo Ribalda --- drivers/iommu/dma-iommu.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index b4d7bfffb3a0d2..714fa930d7b576 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -707,6 +707,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev, goto out_free_sg; sgt->sgl->dma_address = iova; + sgt->sgl->dma_length = size; return pages; out_free_sg: @@ -744,6 +745,37 @@ static void *iommu_dma_alloc_remap(struct device *dev, size_t size, return NULL; } +#ifdef CONFIG_DMA_REMAP +static struct sg_table *iommu_dma_alloc_noncontiguous(struct device *dev, + size_t size, enum dma_data_direction dir, gfp_t gfp, + unsigned long attrs) +{ + struct dma_sgt_handle *sh; + + sh = kmalloc(sizeof(*sh), gfp); + if (!sh) + return NULL; + + sh->pages = __iommu_dma_alloc_noncontiguous(dev, size, &sh->sgt, gfp, + PAGE_KERNEL, attrs); + if (!sh->pages) { + kfree(sh); + return NULL; + } + return &sh->sgt; +} + +static void iommu_dma_free_noncontiguous(struct device *dev, size_t size, + struct sg_table *sgt, enum dma_data_direction dir) +{ + struct dma_sgt_handle *sh = sgt_handle(sgt); + + __iommu_dma_unmap(dev, sgt->sgl->dma_address, size); + __iommu_dma_free_pages(sh->pages, PAGE_ALIGN(size) >> PAGE_SHIFT); + sg_free_table(&sh->sgt); +} +#endif /* CONFIG_DMA_REMAP */ + static void iommu_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction dir) { @@ -1260,6 +1292,10 @@ static const struct dma_map_ops iommu_dma_ops = { .free = iommu_dma_free, .alloc_pages = dma_common_alloc_pages, .free_pages = dma_common_free_pages, +#ifdef CONFIG_DMA_REMAP + .alloc_noncontiguous = iommu_dma_alloc_noncontiguous, + .free_noncontiguous = iommu_dma_free_noncontiguous, +#endif .mmap = iommu_dma_mmap, .get_sgtable = iommu_dma_get_sgtable, .map_page = iommu_dma_map_page, -- 2.29.2