Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp461913ybj; Tue, 5 May 2020 01:49:00 -0700 (PDT) X-Google-Smtp-Source: APiQypJBWQ7QXCTbcqObnLz+zTpbuPLP7w3ieHvrOTEdxmUVzX6qICyZHBPOaciuj2oYPZBW4Occ X-Received: by 2002:a05:6402:356:: with SMTP id r22mr1706654edw.3.1588668539913; Tue, 05 May 2020 01:48:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588668539; cv=none; d=google.com; s=arc-20160816; b=lhtFthiewurwmt6oc+Bc934kyb+hqJ9H54lLJfa3Fo51eIv/Vu5roQWZ3QoX6xC4Xi LfunwMLgmoGo/Fa27ISKzYN5COrvZ3RIVBUhrmGuhuLWQNp7DQiYbTz+MXHFIOEwoXEm v3DC9QYSN25PjIla91O2mpB631HI4fanxEjQdrKYZ+Bn8Xlw2qJeV+HxHjvwsnozsboL mtuXUYMeefwoKlKnhBXLsepFHxrDuN/RcMnL3zz8Gt7yW4GsO1QPqVTD8CiJckMYFnH1 Hob7gXglliHXcgbhMgwkItzXU84VJUhX8hb77R/o6JaT8nSR4F/OZYJA0ePABoeHvj4T KDQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter; bh=hDXMGscph/JtiXtJfzELMo5REY6cGQYOcmjoYLrYu9s=; b=MBpBrC+Av/NzRQrq7k+KgbRfYHUafe0hpFn6pvKTuZCxHzFgVoOOLUsEhCFONGvjqo MZ4P741O7Tyvpk2fgQRGazeMnP6b65Yls69ZJE6EyrY8VEqTFP0fPPIIfLqAoKVRPWYe HKvfA0qwNmKeCxtknVnDJdLWu60AFWA/368+NlGX+bErW60FMw4Oi83Jws/HsDwbwl8u TGLuYLyWEMalUo/vPYtncaeQsydKV9IiUhep7Br9Ne1MkVlrzzfMlSw6cAEAwOx+ikkF pMZjmjiBokYQWF5Qr2Ef1HI4MaDnhKk1gBeoVEjm5JwL24s+4QRE0FYkg6HXxc5wTOrc afDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=YcwUfZqY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bu22si832656edb.507.2020.05.05.01.48.36; Tue, 05 May 2020 01:48:59 -0700 (PDT) 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=@samsung.com header.s=mail20170921 header.b=YcwUfZqY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728736AbgEEIqm (ORCPT + 99 others); Tue, 5 May 2020 04:46:42 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:51871 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728220AbgEEIqa (ORCPT ); Tue, 5 May 2020 04:46:30 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200505084625euoutp02f6f2cdba5ef9420e0a793fca7e65b03f~MFXqwv91N2627826278euoutp02W for ; Tue, 5 May 2020 08:46:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200505084625euoutp02f6f2cdba5ef9420e0a793fca7e65b03f~MFXqwv91N2627826278euoutp02W DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1588668385; bh=hDXMGscph/JtiXtJfzELMo5REY6cGQYOcmjoYLrYu9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YcwUfZqYNaEb+qnytinC5ix1aNOoLi/9yRjbImDFJoDIAPvC6qJQlQ07EPB9IqgjK 5P86Sk54Ce7rBpFB90seW50M9Z4xjlHCO74WBS6Jp+HYXTK9iBEKiRF9GwJlHPpsSB v5KBYjHLz5RQ+UZiARdGp0Zzgm/SydCf7wSe3eJM= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20200505084624eucas1p259ece9904ddb2ff19cde8d111e3a1357~MFXqJND-G1942419424eucas1p2B; Tue, 5 May 2020 08:46:24 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 06.32.60679.0E721BE5; Tue, 5 May 2020 09:46:24 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20200505084624eucas1p2a9a5c4d2aece2c1555a5480c19c2e050~MFXpz_Jfx2336923369eucas1p21; Tue, 5 May 2020 08:46:24 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20200505084624eusmtrp1f7575e3c6e9734b2b0ad0a89f79fa220~MFXpx1HIA0942509425eusmtrp1y; Tue, 5 May 2020 08:46:24 +0000 (GMT) X-AuditID: cbfec7f4-0cbff7000001ed07-25-5eb127e06c81 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 20.21.07950.0E721BE5; Tue, 5 May 2020 09:46:24 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200505084623eusmtip1889e719913a78f767a219409dc8c3bf4~MFXpOhphi0686606866eusmtip1E; Tue, 5 May 2020 08:46:23 +0000 (GMT) From: Marek Szyprowski To: dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Christoph Hellwig , Robin Murphy , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Joerg Roedel Subject: [PATCH v3 01/25] dma-mapping: add generic helpers for mapping sgtable objects Date: Tue, 5 May 2020 10:45:50 +0200 Message-Id: <20200505084614.30424-1-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200505083926.28503-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA0WSbUhTcRTG+d+73V2nk9sU/GeaMagoUbMsLliSWXQ/REgQmZC58jat6WR3 mgrl1BRbmi0jxUqG+JJOcy6dqclyqfOFlqmEkTWdVmhJolNMxWpes2+/55zn8BwOB0eF9VxP PC5BQcsTxFIRxucYun+99Rvb3RC1z2LaQeZbehGyobieS/42qFFyeOEnRlZruxBSYwwmbz/S 8Uj78DhC6ifec8mh1scYWdf5iUd2zE5yj7pQkx2lCFVbWguo9kUNh2peHONS1jtmhHpenk59 XJtAqcKRKkC1fVBi1N3GGkDN67eHO0fyD8fQ0rhkWh4QEs2P7dNOg8Rq75Sv7WpECaxQBXAc EkEw1xikAnxcSDwFMC/jNWCFHUD9aC2HFfMAdlunEBVwYicKslG2UQXgzOAXZHNktqUWc7gw IhCqZlTr7E5kA9iT7+JglGhFYEmW2JHtRkTAyneJjjKH2AkH9J95DhYQR6BVVYSxYT5Qq3uF OtiJCIErPcb1YEiYeDDb8H3DdBx2NU9usBucNjfyWPaC/YV5HHYgC8BxSx2PFXkADmUWA9YV DEcty5hjI5TYA+tbA9hyKJxruoWxR3KFIzNb2P1d4X1DEcqWBTA3R8i6d8ES87PN2I6BQZRl CtoKz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0MXFwSjUwBpcLJ5+SCD8Yz9wezZX78+7K iXvrghiF/imF7PyiYmEr75P6dHVFk1jP37est+tOXr6Q+3G2vtDuTAsrO7f1jF/Fv53+scun 9r1mJc9K30X5vkt/c+4OKIhfYaSpGLU4Q1Q2xqCn/80/LZFrigdvW+h99nr87E38nuTe7BfL ZkRnbJlm5npTiaU4I9FQi7moOBEAhIgjf6YCAAA= X-CMS-MailID: 20200505084624eucas1p2a9a5c4d2aece2c1555a5480c19c2e050 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200505084624eucas1p2a9a5c4d2aece2c1555a5480c19c2e050 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200505084624eucas1p2a9a5c4d2aece2c1555a5480c19c2e050 References: <20200505083926.28503-1-m.szyprowski@samsung.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org struct sg_table is a common structure used for describing a memory buffer. It consists of a scatterlist with memory pages and DMA addresses (sgl entry), as well as the number of scatterlist entries: CPU pages (orig_nents entry) and DMA pages (nents entry). It turned out that it was a common mistake to misuse nents and orig_nents entries, call dma-mapping functions with a wrong number of entries or ignoring the number of mapped entries returned by the dma_map_sg function. To avoid such issues, lets introduce a common wrappers operating directly on the struct sg_table objects, which take care of the proper use of the nents and orig_nents entries. Signed-off-by: Marek Szyprowski --- include/linux/dma-mapping.h | 32 ++++++++++++++++++++++++++++++++ include/linux/iommu.h | 6 ++++++ 2 files changed, 38 insertions(+) diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index b43116a..8364c20d 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -609,6 +609,36 @@ static inline void dma_sync_single_range_for_device(struct device *dev, return dma_sync_single_for_device(dev, addr + offset, size, dir); } +static inline int dma_map_sgtable_attrs(struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir, unsigned long attrs) +{ + int n = dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs); + + if (n > 0) { + sgt->nents = n; + return 0; + } + return -EINVAL; +} + +static inline void dma_unmap_sgtable_attrs(struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir, unsigned long attrs) +{ + dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs); +} + +static inline void dma_sync_sgtable_for_cpu(struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir) +{ + dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir); +} + +static inline void dma_sync_sgtable_for_device(struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir) +{ + dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir); +} + #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0) #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0) #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0) @@ -617,6 +647,8 @@ static inline void dma_sync_single_range_for_device(struct device *dev, #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0) #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0) #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0) +#define dma_map_sgtable(d, s, r) dma_map_sgtable_attrs(d, s, r, 0) +#define dma_unmap_sgtable(d, s, r) dma_unmap_sgtable_attrs(d, s, r, 0) extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, dma_addr_t dma_addr, size_t size, diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 7ef8b0b..5106b65 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -466,6 +466,12 @@ extern size_t iommu_map_sg_atomic(struct iommu_domain *domain, extern void iommu_set_fault_handler(struct iommu_domain *domain, iommu_fault_handler_t handler, void *token); +static inline size_t iommu_map_sgtable(struct iommu_domain *domain, + unsigned long iova, struct sg_table *sgt, int prot) +{ + return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot); +} + extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); extern void generic_iommu_put_resv_regions(struct device *dev, -- 1.9.1