Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4469080pxb; Tue, 2 Mar 2021 16:40:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRxFuYHxCnozKnIRihEE3jbqpVIjppzFvUv2jzA8CB1xCBxh0gYX9n51KRyRVxI9T45FNs X-Received: by 2002:a17:906:2743:: with SMTP id a3mr23645794ejd.378.1614732023880; Tue, 02 Mar 2021 16:40:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614732023; cv=none; d=google.com; s=arc-20160816; b=VyZIVukvy4EdH3KlP500I2ZKVyuuZ8rZ4AwXf8V3Ry2+pOfAVbYgoNVJdJPEdWLLJc OfC7RY0lk2q8yS8hUd48PHSHYBQDX3MmRSxFVpBCpQN+P6ocX3M1nErUH1OTc4xB2q5i eTy9RxL/txvnqekYVDAQ5BoWBlYFwi6c8PpaasjI5qIf30QAe5uXimKT2t9wS5slH0TA mOSxs6iVrN8ock/XnrPSVFjpVi8J3Dj5zwgIXgJmuujoFvkZ1/OgGBQGNsg7a6ASynwl 9h6WLgaYpiXEjbWandtg1lZQNvK8iKCZQgwC//UjlU3rAmTYjKv1TdI3Dpdc9r1VlXiZ nX+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=wtrnZc7xYst9Y2Hn1iP0GRhjvfQJv4F/+YEJZGPAD8U=; b=oKRYgCMMyQuaMdlXyQHEt3wpjxLGvl6fE/gLIDVJcvLo8tkzE0KSnb5869qeHDF7cq QxB5SIC1r2/KRl+jzDi2Fy4DzUinbheCvOnjCg/O1NBdESJGa6OPs3gd/37hT1MrOiNz KLHr2kikshTDCJC2O1meQpkZ4snCuxivMOMOOYOu/meHrBwdRNB80VJGFs/0Dgwwux5W xMIJgrIafT7lV57OjZ/ezdhSZAwPGvOtDlFeP72kvZKBNGFZYUWfciVw2hL90IxzEaoO 6xkYTEzauf4Ut1vv7oH5qbwRVn4S7+q7G1GpQ13tRZVJdt7mD/cwrfM2tqozarGls4rl RBsg== ARC-Authentication-Results: i=1; mx.google.com; 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 g8si13805872eje.538.2021.03.02.16.40.00; Tue, 02 Mar 2021 16:40:23 -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; 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 S232754AbhCAIK2 (ORCPT + 99 others); Mon, 1 Mar 2021 03:10:28 -0500 Received: from verein.lst.de ([213.95.11.211]:52707 "EHLO verein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232619AbhCAIKZ (ORCPT ); Mon, 1 Mar 2021 03:10:25 -0500 Received: by verein.lst.de (Postfix, from userid 2407) id 9341868BEB; Mon, 1 Mar 2021 09:09:42 +0100 (CET) Date: Mon, 1 Mar 2021 09:09:42 +0100 From: Christoph Hellwig To: Robin Murphy Cc: Christoph Hellwig , Mauro Carvalho Chehab , Marek Szyprowski , Tomasz Figa , Ricardo Ribalda , Sergey Senozhatsky , iommu@lists.linux-foundation.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org Subject: Re: [PATCH 4/7] dma-mapping: add a dma_alloc_noncontiguous API Message-ID: <20210301080942.GA27723@lst.de> References: <20210202095110.1215346-1-hch@lst.de> <20210202095110.1215346-5-hch@lst.de> <53a6c581-4d9f-c69a-80f5-2e95e810c4d1@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53a6c581-4d9f-c69a-80f5-2e95e810c4d1@arm.com> User-Agent: Mutt/1.5.17 (2007-11-01) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 16, 2021 at 06:55:39PM +0000, Robin Murphy wrote: > On 2021-02-02 09:51, Christoph Hellwig wrote: >> Add a new API that returns a potentiall virtually non-contigous sg_table >> and a DMA address. This API is only properly implemented for dma-iommu >> and will simply return a contigious chunk as a fallback. >> >> The intent is that media drivers can use this API if either: >> >> - no kernel mapping or only temporary kernel mappings are required. >> That is as a better replacement for DMA_ATTR_NO_KERNEL_MAPPING >> - a kernel mapping is required for cached and DMA mapped pages, but >> the driver also needs the pages to e.g. map them to userspace. >> In that sense it is a replacement for some aspects of the recently >> removed and never fully implemented DMA_ATTR_NON_CONSISTENT >> >> Signed-off-by: Christoph Hellwig >> --- >> Documentation/core-api/dma-api.rst | 74 +++++++++++++++++++++ >> include/linux/dma-map-ops.h | 18 +++++ >> include/linux/dma-mapping.h | 31 +++++++++ >> kernel/dma/mapping.c | 103 +++++++++++++++++++++++++++++ >> 4 files changed, 226 insertions(+) >> >> diff --git a/Documentation/core-api/dma-api.rst b/Documentation/core-api/dma-api.rst >> index 157a474ae54416..e24b2447f4bfe6 100644 >> --- a/Documentation/core-api/dma-api.rst >> +++ b/Documentation/core-api/dma-api.rst >> @@ -594,6 +594,80 @@ dev, size, dma_handle and dir must all be the same as those passed into >> dma_alloc_noncoherent(). cpu_addr must be the virtual address returned by >> dma_alloc_noncoherent(). >> +:: >> + >> + struct sg_table * >> + dma_alloc_noncontiguous(struct device *dev, size_t size, >> + enum dma_data_direction dir, gfp_t gfp) >> + >> +This routine allocates bytes of non-coherent and possibly non-contiguous >> +memory. It returns a pointer to struct sg_table that describes the allocated >> +and DMA mapped memory, or NULL if the allocation failed. The resulting memory >> +can be used for struct page mapped into a scatterlist are suitable for. >> + >> +The return sg_table is guaranteed to have 1 single DMA mapped segment as >> +indicated by sgt->nents, but it might have multiple CPU side segments as >> +indicated by sgt->orig_nents. >> + >> +The dir parameter specified if data is read and/or written by the device, >> +see dma_map_single() for details. >> + >> +The gfp parameter allows the caller to specify the ``GFP_`` flags (see >> +kmalloc()) for the allocation, but rejects flags used to specify a memory >> +zone such as GFP_DMA or GFP_HIGHMEM. >> + >> +Before giving the memory to the device, dma_sync_sgtable_for_device() needs >> +to be called, and before reading memory written by the device, >> +dma_sync_sgtable_for_cpu(), just like for streaming DMA mappings that are >> +reused. >> + >> +:: >> + >> + void >> + dma_free_noncontiguous(struct device *dev, size_t size, >> + struct sg_table *sgt, >> + enum dma_data_direction dir) >> + >> +Free memory previously allocated using dma_alloc_noncontiguous(). dev, size, >> +and dir must all be the same as those passed into dma_alloc_noncontiguous(). >> +sgt must be the pointer returned by dma_alloc_noncontiguous(). >> + >> +:: >> + >> + void * >> + dma_vmap_noncontiguous(struct device *dev, size_t size, >> + struct sg_table *sgt) >> + >> +Return a contiguous kernel mapping for an allocation returned from >> +dma_alloc_noncontiguous(). dev and size must be the same as those passed into >> +dma_alloc_noncontiguous(). sgt must be the pointer returned by >> +dma_alloc_noncontiguous(). >> + >> +Once a non-contiguous allocation is mapped using this function, the >> +flush_kernel_vmap_range() and invalidate_kernel_vmap_range() APIs must be used >> +to manage the coherency of the kernel mapping. > > Maybe say something like "coherency between the kernel mapping and any > userspace mappings"? Otherwise people like me may be easily confused and > think it's referring to coherency between the kernel mapping and the > device, where in most cases those APIs won't help at all :) Well, it is all of the above for a VIVT cache setup. I've ammended it to: Once a non-contiguous allocation is mapped using this function, the flush_kernel_vmap_range() and invalidate_kernel_vmap_range() APIs must be used to manage the coherency between the kernel mapping, the device and user space mappings (if any).