Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1055639pxb; Thu, 28 Jan 2021 07:06:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJyHJyVZllEY825jY91vfu4Ge52C3jgahoh8UtDOcCvCvp++o82LIdz8U3ul5itPtSfCEy+x X-Received: by 2002:a5d:4203:: with SMTP id n3mr16781058wrq.49.1611846405981; Thu, 28 Jan 2021 07:06:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611846405; cv=none; d=google.com; s=arc-20160816; b=AXDbeEBSf1heApmkC6wHgcqfDFJMSsopfQd1ZJCWTAL2Co7PlW4T9G7LHXtjTQcTXe /E7SiI7B01SDHtbcksFGbGQo+qXCyO1v49zSwTFz6R+vC3EQv/pZNBdbiWc23CZVC5tB pb0uFpfVc6z4hRJ1Y7mDLcZP/Hp0y8I1lqjWVF1c8rHilyv9ULrQJF2mKyCXmAofxGri 3MROzNc5tn6HIp3jU8j55PEtixnV0txFfEsBa+7FTpccwJ2VeKWCniuiXALH+maxa8Ze 4Mhh8XqaqnaDX1OEOKBxWHWDZySMh9zkT/20MNG0xsYe0bd8mAEsCNd+4udkEeZNFiyw IKCQ== 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=6h4Clr4yrvyrSY276Nk233XB/1n9S2aGkvEz85NFWNA=; b=GjdycyfSyDrb0K40GnGr6v+nSgbecewbEbPXa+2uDo4IFyBQq313Tz6hd+IMso8N5E Eo3XduiJ3uRCMDaWLC2vXGoaJdTkhrD/LC/5KpSG/pGveA5j3mz8l4Xs4exvuIYIUK3p 9KMuoWBrXXDAfju42H3GUagbZmYa9YOB7s5Ndtcr/pADQUadnAV/HgsdP3WzcBBxf+58 Oj2F9eMm1sUsN7SMHaEB/MOYobHOt7m3ryWh0OxYoydFhkFd2qWqG4C7Uz0XW+w3A2Jl N0h89PyfAI1dwzqgSRidP6Lm4bU74ap1QPbUQZZ0tifEqHO/IVguPwEvMel1N3tUJm5F Kgeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=KfBRW4pI; 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 a22si3060413eds.543.2021.01.28.07.06.20; Thu, 28 Jan 2021 07:06:45 -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=KfBRW4pI; 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 S232428AbhA1PFM (ORCPT + 99 others); Thu, 28 Jan 2021 10:05:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232277AbhA1PEO (ORCPT ); Thu, 28 Jan 2021 10:04:14 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E027DC0613ED; Thu, 28 Jan 2021 07:03:33 -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=6h4Clr4yrvyrSY276Nk233XB/1n9S2aGkvEz85NFWNA=; b=KfBRW4pI01u3GQvXdslcgMQsFG sCoM1TdFacyc8bszZAac77VCZKXVRdPuDZlW65VRprx8eYIU/4utKN9SW27AmrmtDIdQEng5F/Hdm Tmltejx0op3G56rXsv5mr+Wsa02HmrlLpUUM8dlwkMExhgOBfjcLGZmqzpukHZfi9ZMqQT8arwC3L NUoM13BC6tdhS/qajuSokdhi9DQfyVECgJ8dZWZXx8xKpepCKc162PfXUzk+pYdH5HbbmIE0VWrki FeWeR0ZarhpB/ej+t4Ey060Qjaw/QuI3v3WLnJGUS1oDUEfAVxrhnLha655P0nu6X0DRb7N8A0LtR ER4pjXZA==; Received: from [2001:4bb8:18c:25a3:5b32:95f:aa1f:ea4] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1l58pK-008bLO-3x; Thu, 28 Jan 2021 15:03:19 +0000 From: Christoph Hellwig To: Mauro Carvalho Chehab , Marek Szyprowski , Tomasz Figa , Ricardo Ribalda , Sergey Senozhatsky , 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: Thu, 28 Jan 2021 15:58:36 +0100 Message-Id: <20210128145837.2250561-6-hch@lst.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210128145837.2250561-1-hch@lst.de> References: <20210128145837.2250561-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 --- drivers/iommu/dma-iommu.c | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 65af875ba8495c..938a2856b4a455 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -756,6 +756,49 @@ 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, dma_addr_t *dma_handle, + enum dma_data_direction dir, gfp_t gfp) +{ + unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT; + struct dma_sgt_handle *sh; + + sh = kmalloc(sizeof(*sh), gfp); + if (!sh) + return NULL; + + sh->pages = __iommu_dma_alloc_noncontiguous(dev, size, &sh->sgt, + dma_handle, gfp, + PAGE_KERNEL, 0); + if (!sh->pages) + goto out_free_sh; + + if (sg_alloc_table_from_pages(&sh->sgt, sh->pages, count, 0, size, + GFP_KERNEL)) + goto out_free_pages; + + return &sh->sgt; + +out_free_pages: + __iommu_dma_free_pages(sh->pages, count); +out_free_sh: + kfree(sh); + return NULL; +} + +static void iommu_dma_free_noncontiguous(struct device *dev, size_t size, + struct sg_table *sgt, dma_addr_t dma_handle, + enum dma_data_direction dir) +{ + struct dma_sgt_handle *sh = sgt_handle(sgt); + + __iommu_dma_unmap(dev, dma_handle, 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) { @@ -1271,6 +1314,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