linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: Christoph Hellwig <hch@lst.de>, iommu@lists.linux-foundation.org
Cc: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>, Guo Ren <ren_guo@c-sky.com>,
	Laura Abbott <labbott@redhat.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/9] dma-mapping: move the arm64 ncoherent alloc/free support to common code
Date: Fri, 30 Nov 2018 19:05:23 +0000	[thread overview]
Message-ID: <5526bc61-57a3-54ff-60c6-e9963230af22@arm.com> (raw)
In-Reply-To: <20181105121931.13481-5-hch@lst.de>

On 05/11/2018 12:19, Christoph Hellwig wrote:
> The arm64 codebase to implement coherent dma allocation for architectures
> with non-coherent DMA is a good start for a generic implementation, given
> that is uses the generic remap helpers, provides the atomic pool for
> allocations that can't sleep and still is realtively simple and well
> tested.  Move it to kernel/dma and allow architectures to opt into it
> using a config symbol.  Architectures just need to provide a new
> arch_dma_prep_coherent helper to writeback an invalidate the caches
> for any memory that gets remapped for uncached access.

It's a bit yuck that we now end up with arch_* hooks being a mix of arch 
code and not-actually-arch-code, but I guess there's some hope of coming 
back and streamlining things in future once all the big moves are done.

I can't really be bothered to nitpick the typos above and the slight 
inconsistencies in some of the cosmetic code changes, but one worthwhile 
thing stands out...

> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>   arch/arm64/Kconfig              |   2 +-
>   arch/arm64/mm/dma-mapping.c     | 184 ++------------------------------
>   include/linux/dma-mapping.h     |   5 +
>   include/linux/dma-noncoherent.h |   2 +
>   kernel/dma/Kconfig              |   6 ++
>   kernel/dma/remap.c              | 158 ++++++++++++++++++++++++++-
>   6 files changed, 181 insertions(+), 176 deletions(-)

[...]

> +void *dma_alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
> +{
> +	unsigned long val;
> +	void *ptr = NULL;
> +
> +	if (!atomic_pool) {
> +		WARN(1, "coherent pool not initialised!\n");
> +		return NULL;
> +	}
> +
> +	val = gen_pool_alloc(atomic_pool, size);
> +	if (val) {
> +		phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
> +
> +		*ret_page = phys_to_page(phys);

Looks like phys_to_page() isn't particularly portable, so we probably 
want an explicit pfn_to_page(__phys_to_pfn(phys)) here. Otherwise, the 
fundamental refactoring looks OK.

Reviewed-by: Robin Murphy <robin.murphy@arm.com>

[ In fact, looking at phys_to_page(), microblaze, riscv, unicore and 
csky (by the end of this series if it's fixed here) don't need to define 
it at all; s390 defines it for the sake of a single call in a single 
driver; it's used in two other places in arm-related drivers but at 
least one of those is clearly wrong. All in all it's quite the sorry mess. ]

> +		ptr = (void *)val;
> +		memset(ptr, 0, size);
> +	}
> +
> +	return ptr;
> +}
> +
> +bool dma_free_from_pool(void *start, size_t size)
> +{
> +	if (!dma_in_atomic_pool(start, size))
> +		return false;
> +	gen_pool_free(atomic_pool, (unsigned long)start, size);
> +	return true;
> +}
> +
> +void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
> +		gfp_t flags, unsigned long attrs)
> +{
> +	struct page *page = NULL;
> +	void *ret, *kaddr;
> +
> +	size = PAGE_ALIGN(size);
> +
> +	if (!gfpflags_allow_blocking(flags)) {
> +		ret = dma_alloc_from_pool(size, &page, flags);
> +		if (!ret)
> +			return NULL;
> +		*dma_handle = phys_to_dma(dev, page_to_phys(page));
> +		return ret;
> +	}
> +
> +	kaddr = dma_direct_alloc_pages(dev, size, dma_handle, flags, attrs);
> +	if (!kaddr)
> +		return NULL;
> +	page = virt_to_page(kaddr);
> +
> +	/* remove any dirty cache lines on the kernel alias */
> +	arch_dma_prep_coherent(page, size);
> +
> +	/* create a coherent mapping */
> +	ret = dma_common_contiguous_remap(page, size, VM_USERMAP,
> +			arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs),
> +			__builtin_return_address(0));
> +	if (!ret)
> +		dma_direct_free_pages(dev, size, kaddr, *dma_handle, attrs);
> +	return ret;
> +}
> +
> +void arch_dma_free(struct device *dev, size_t size, void *vaddr,
> +		dma_addr_t dma_handle, unsigned long attrs)
> +{
> +	if (!dma_free_from_pool(vaddr, PAGE_ALIGN(size))) {
> +		void *kaddr = phys_to_virt(dma_to_phys(dev, dma_handle));
> +
> +		vunmap(vaddr);
> +		dma_direct_free_pages(dev, size, kaddr, dma_handle, attrs);
> +	}
> +}
> +
> +long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
> +		dma_addr_t dma_addr)
> +{
> +	return __phys_to_pfn(dma_to_phys(dev, dma_addr));
> +}
> +#endif /* CONFIG_DMA_DIRECT_REMAP */
> 

  parent reply	other threads:[~2018-11-30 19:05 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-05 12:19 move the arm arch_dma_alloc implementation to common code Christoph Hellwig
2018-11-05 12:19 ` [PATCH 1/9] dma-direct: provide page based alloc/free helpers Christoph Hellwig
2018-11-30 19:04   ` Robin Murphy
2018-12-01 16:56     ` Christoph Hellwig
2018-11-05 12:19 ` [PATCH 2/9] dma-direct: reject highmem pages from dma_alloc_from_contiguous Christoph Hellwig
2018-11-30 19:04   ` Robin Murphy
2018-12-01 16:57     ` Christoph Hellwig
2018-11-05 12:19 ` [PATCH 3/9] dma-mapping: move the remap helpers to a separate file Christoph Hellwig
2018-11-30 19:05   ` Robin Murphy
2018-12-01 16:59     ` Christoph Hellwig
2018-11-05 12:19 ` [PATCH 4/9] dma-mapping: move the arm64 ncoherent alloc/free support to common code Christoph Hellwig
2018-11-15 19:50   ` Will Deacon
2018-11-30 19:05   ` Robin Murphy [this message]
2018-12-01 17:03     ` Christoph Hellwig
2018-12-04 10:09   ` Russell King - ARM Linux
2018-12-04 14:22     ` Christoph Hellwig
2018-11-05 12:19 ` [PATCH 5/9] dma-mapping: support highmem in the generic remap allocator Christoph Hellwig
2018-11-30 19:05   ` Robin Murphy
2018-12-04  8:38     ` Marek Szyprowski
2018-12-04 14:23       ` Christoph Hellwig
     [not found]         ` <CGME20181205101414eucas1p2fdde1c06ad6352293980b94b86b022f9@eucas1p2.samsung.com>
2018-12-05 10:14           ` [PATCH] dma-mapping: fix lack of DMA address assignment in " Marek Szyprowski
2018-12-05 12:35             ` Thierry Reding
2018-12-05 13:49             ` Christoph Hellwig
2018-11-05 12:19 ` [PATCH 6/9] dma-remap: support DMA_ATTR_NO_KERNEL_MAPPING Christoph Hellwig
2018-11-30 19:05   ` Robin Murphy
2018-12-01 17:05     ` Christoph Hellwig
2018-11-05 12:19 ` [PATCH 7/9] csky: don't select DMA_NONCOHERENT_OPS Christoph Hellwig
2018-11-05 15:47   ` Guo Ren
2018-11-05 12:19 ` [PATCH 8/9] csky: don't use GFP_DMA in atomic_pool_init Christoph Hellwig
2018-11-05 15:47   ` Guo Ren
2018-11-05 12:19 ` [PATCH 9/9] csky: use the generic remapping dma alloc implementation Christoph Hellwig
2018-11-06  7:01   ` Guo Ren
2018-11-09  7:51     ` Christoph Hellwig
2018-11-09  7:52 ` move the arm arch_dma_alloc implementation to common code Christoph Hellwig
2018-11-15 19:50   ` Will Deacon
2018-11-15 20:58     ` Robin Murphy
2018-11-27  7:37       ` Christoph Hellwig
2018-11-28 12:18         ` Robin Murphy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5526bc61-57a3-54ff-60c6-e9963230af22@arm.com \
    --to=robin.murphy@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux-foundation.org \
    --cc=labbott@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ren_guo@c-sky.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).