merge dma_direct_ops and dma_noncoherent_ops v2
Subsystems (18)
While most architectures are either always or never dma coherent for a given build, the arm, arm64, mips and soon arc architectures can have different dma coherent settings on a per-device basis. Additionally some mips builds can decide at boot time if dma is coherent or not.
I've started to look into handling noncoherent dma in swiotlb, and moving the dma-iommu ops into common code [1], and for that we need a generic way to check if a given device is coherent or not. Moving this flag into struct device also simplifies the conditionally coherent architecture implementations.
These patches are also available in a git tree given that they have a few previous posted dependencies:
git://git.infradead.org/users/hch/misc.git dma-direct-noncoherent-merge
Gitweb:
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-direct-noncoherent-merge
Changes since v1:
- rebased to the latest Linus' tree which includes coherent dma support for arc
- a couple tidyups suggested by Paul Burton
PATCH 1/5 MIPS: don't select DMA_MAYBE_COHERENT from DMA_PERDEV_COHERENT
While both option select a form of conditional dma coherence they don't actually share any code in the implementation, so untangle them.
--- arch/mips/Kconfig | 2 +- arch/mips/kernel/setup.c | 2 +- arch/mips/mm/c-r4k.c | 17 ++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-)
arch/mips/Kconfig +1 -1
@@ -1111,7 +1111,7 @@ config DMA_MAYBE_COHERENT config DMA_PERDEV_COHERENT bool - select DMA_MAYBE_COHERENT + select DMA_NONCOHERENT config DMA_NONCOHERENT bool
arch/mips/kernel/setup.c +1 -1
@@ -1067,7 +1067,7 @@ static int __init debugfs_mips(void)arch_initcall (debugfs_mips);#endif -#if defined(CONFIG_DMA_MAYBE_COHERENT ) && !defined(CONFIG_DMA_PERDEV_COHERENT ) +#ifdef CONFIG_DMA_MAYBE_COHERENT /* User defined DMA coherency from command line. */ enum coherent_io_user_state coherentio =IO_COHERENCE_DEFAULT ;EXPORT_SYMBOL_GPL (coherentio);
arch/mips/mm/c-r4k.c +8 -9
@@ -1955,22 +1955,21 @@ void r4k_cache_init(void) __flush_icache_user_range = r4k_flush_icache_user_range; __local_flush_icache_user_range = local_r4k_flush_icache_user_range; -#if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT) -# if defined(CONFIG_DMA_PERDEV_COHERENT) - if (0) { -# else - if ((coherentio == IO_COHERENCE_ENABLED) || - ((coherentio == IO_COHERENCE_DEFAULT) && hw_coherentio)) { -# endif +#ifdef CONFIG_DMA_NONCOHERENT +#ifdef CONFIG_DMA_MAYBE_COHERENT +if (coherentio ==IO_COHERENCE_ENABLED || + (coherentio ==IO_COHERENCE_DEFAULT && hw_coherentio)) { _dma_cache_wback_inv = (void *)cache_noop; _dma_cache_wback = (void *)cache_noop; _dma_cache_inv = (void *)cache_noop; - } else { + } else +#endif /* CONFIG_DMA_MAYBE_COHERENT */ + { _dma_cache_wback_inv = r4k_dma_cache_wback_inv; _dma_cache_wback = r4k_dma_cache_wback_inv; _dma_cache_inv = r4k_dma_cache_inv; } -#endif +#endif /* CONFIG_DMA_NONCOHERENT */ build_clear_page ();build_copy_page ();
Signature · 1 line
2.18.0
PATCH 2/5 dma-mapping: move the dma_coherent flag to struct device
In reply to Christoph HellwigVarious architectures support both coherent and non-coherent dma on a per-device basis. Move the dma_noncoherent flag from the mips archdata field to struct device proper to prepare the infrastructure for reuse on other architectures.
--- arch/mips/Kconfig | 1 + arch/mips/include/asm/Kbuild | 1 + arch/mips/include/asm/device.h | 19 ---------------- arch/mips/include/asm/dma-coherence.h | 6 +++++ arch/mips/include/asm/dma-mapping.h | 2 +- arch/mips/mm/dma-noncoherent.c | 32 +++++---------------------- include/linux/device.h | 7 ++++++ include/linux/dma-noncoherent.h | 16 ++++++++++++++ kernel/dma/Kconfig | 3 +++ 9 files changed, 41 insertions(+), 46 deletions(-) delete mode 100644 arch/mips/include/asm/device.h
arch/mips/Kconfig +1 -0
@@ -1106,6 +1106,7 @@ config ARCH_SUPPORTS_UPROBES bool config DMA_MAYBE_COHERENT + select ARCH_HAS_DMA_COHERENCE_H select DMA_NONCOHERENT bool
arch/mips/include/asm/Kbuild +1 -0
@@ -1,6 +1,7 @@ # MIPS headers generic-(CONFIG_GENERIC_CSUM) += checksum.h generic-y += current.h +generic-y += device.h generic-y += dma-contiguous.h generic-y += emergency-restart.h generic-y += export.h
arch/mips/include/asm/device.h deleted +0 -19
@@ -1,19 +0,0 @@ -/* -* Arch specific extensions to struct device -* -* This file is released under the GPLv2 -*/ -#ifndef _ASM_MIPS_DEVICE_H -#define _ASM_MIPS_DEVICE_H - -struct dev_archdata { -#ifdef CONFIG_DMA_PERDEV_COHERENT -/* Non-zero if DMA is coherent with CPU caches */ -bool dma_coherent; -#endif -}; - -struct pdev_archdata { -}; - -#endif /* _ASM_MIPS_DEVICE_H*/
arch/mips/include/asm/dma-coherence.h +6 -0
@@ -20,6 +20,12 @@ enum coherent_io_user_state {#elif defined(CONFIG_DMA_MAYBE_COHERENT )extern enum coherent_io_user_state coherentio;extern int hw_coherentio; + +static inline int dev_is_dma_coherent (struct device *dev) +{ +return coherentio ==IO_COHERENCE_ENABLED || + (coherentio ==IO_COHERENCE_DEFAULT && hw_coherentio); +}#else #ifdef CONFIG_DMA_NONCOHERENT #define coherentio IO_COHERENCE_DISABLED
arch/mips/include/asm/dma-mapping.h +1 -1
@@ -25,7 +25,7 @@ static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, bool coherent) {#ifdef CONFIG_DMA_PERDEV_COHERENT - dev->archdata.dma_coherent = coherent; + dev->dma_coherent = coherent;#endif }
arch/mips/mm/dma-noncoherent.c +6 -26
@@ -14,26 +14,6 @@#include <asm/dma-coherence.h> #include <asm/io.h> -#ifdef CONFIG_DMA_PERDEV_COHERENT -static inline int dev_is_coherent (struct device *dev) -{ -return dev->archdata.dma_coherent; -} -#else -static inline int dev_is_coherent (struct device *dev) -{ -switch (coherentio) { -default : -case IO_COHERENCE_DEFAULT : -return hw_coherentio; -case IO_COHERENCE_ENABLED : -return 1 ; -case IO_COHERENCE_DISABLED : -return 0 ; - } -} -#endif /* CONFIG_DMA_PERDEV_COHERENT */ -/* * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively * fill random cachelines with stale data at any time, requiring an extra
@@ -49,7 +29,7 @@ static inline int dev_is_coherent(struct device *dev)*/ static inline bool cpu_needs_post_dma_flush (struct device *dev) { -if (dev_is_coherent (dev)) +if (dev_is_dma_coherent (dev))return false;switch (boot_cpu_type ()) {
@@ -76,7 +56,7 @@ void *arch_dma_alloc(struct device *dev, size_t size,if (!ret)return NULL ; -if (!dev_is_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) { +if (!dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) {dma_cache_wback_inv ((unsigned long ) ret, size); ret = (void *)UNCAC_ADDR (ret); }
@@ -87,7 +67,7 @@ void *arch_dma_alloc(struct device *dev, size_t size,void arch_dma_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs) { -if (!(attrs &DMA_ATTR_NON_CONSISTENT ) && !dev_is_coherent (dev)) +if (!(attrs &DMA_ATTR_NON_CONSISTENT ) && !dev_is_dma_coherent (dev)) cpu_addr = (void *)CAC_ADDR ((unsigned long )cpu_addr);dma_direct_free (dev, size, cpu_addr, dma_addr, attrs); }
@@ -103,7 +83,7 @@ int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,unsigned long pfn;int ret = -ENXIO ; -if (!dev_is_coherent (dev)) +if (!dev_is_dma_coherent (dev)) addr =CAC_ADDR (addr); pfn =page_to_pfn (virt_to_page ((void *)addr));
@@ -187,7 +167,7 @@ static inline void dma_sync_phys(phys_addr_t paddr, size_t size,void arch_sync_dma_for_device (struct device *dev,phys_addr_t paddr,size_t size,enum dma_data_direction dir) { -if (!dev_is_coherent (dev)) +if (!dev_is_dma_coherent (dev))dma_sync_phys (paddr, size, dir); }
@@ -203,6 +183,6 @@ void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size, {BUG_ON (direction ==DMA_NONE ); -if (!dev_is_coherent (dev)) +if (!dev_is_dma_coherent (dev))dma_sync_virt (vaddr, size, direction); }
include/linux/device.h +7 -0
@@ -927,6 +927,8 @@ struct dev_links_info {* @offline: Set after successful invocation of bus type's .offline(). * @of_node_reused: Set if the device-tree node is shared with an ancestor * device. +* @dma_coherent: this particular device is dma coherent, even if the +* architecture supports non-coherent devices. * * At the lowest level, every device in a Linux system is represented by an * instance of struct device. The device structure contains the information
@@ -1016,6 +1018,11 @@ struct device { bool offline_disabled:1; bool offline:1; bool of_node_reused:1; +#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) + bool dma_coherent:1; +#endif };static inline struct device *kobj_to_dev (struct kobject *kobj)
include/linux/dma-noncoherent.h +16 -0
@@ -4,6 +4,22 @@#include <linux/dma-mapping.h> +#ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H +#include <asm/dma-coherence.h> +#elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) +static inline int dev_is_dma_coherent (struct device *dev) +{ +return dev->dma_coherent; +} +#else +static inline int dev_is_dma_coherent (struct device *dev) +{ +return true; +} +#endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */ +void *arch_dma_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs); void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
kernel/dma/Kconfig +3 -0
@@ -13,6 +13,9 @@ config NEED_DMA_MAP_STATE config ARCH_DMA_ADDR_T_64BIT def_bool 64BIT || PHYS_ADDR_T_64BIT +config ARCH_HAS_DMA_COHERENCE_H + bool + config HAVE_GENERIC_DMA_COHERENT bool
Signature · 1 line
2.18.0
Robin Murphy
reviewerRe: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
Quoted 8 lines · Christoph Hellwig
Various architectures support both coherent and non-coherent dma on a per-device basis. Move the
dma_noncoherentflag from the mips archdata field to struct device proper to prepare the infrastructure for reuse on other architectures.Signed-off-by Christoph Hellwig <[email protected]>Acked-by Paul Burton <[email protected]>---
[...]
Quoted 14 lines
include/linux/device.h +2
-0@@ -927,6 +927,8 @@ struct dev_links_info {* @offline: Set after successful invocation of bus type's .offline(). * @of_node_reused: Set if the device-tree node is shared with an ancestor * device. +* @dma_coherent: this particular device is dma coherent, even if the +* architecture supports non-coherent devices. * * At the lowest level, every device in a Linux system is represented by an * instance of struct device. The device structure contains the information
include/linux/device.h struct device { · line 1023
Quoted 6 lines
include/linux/device.h +3
-0@@ -1016,6 +1018,11 @@ struct device { bool offline_disabled:1; bool offline:1; bool of_node_reused:1; +#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL )
If we're likely to refer to it more than once, is it worth wrapping that condition up in something like ARCH_HAS_NONCOHERENT_DMA?
Quoted 10 lines
include/linux/device.h +2
-0+ bool dma_coherent:1; +#endif };static inline struct device *kobj_to_dev (struct kobject *kobj)include/linux/dma-noncoherent.h +0
-0
include/linux/dma-noncoherent.h line 12
Quoted 9 lines
include/linux/dma-noncoherent.h +6
-0@@ -4,6 +4,22 @@#include <linux/dma-mapping.h> +#ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H +#include <asm/dma-coherence.h> +#elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) +static inline int dev_is_dma_coherent (struct device *dev)
Given that it's backed by a bool and used as a bool everywhere, this (and its equivalents) should probably return a bool ;)
Quoted 18 lines
include/linux/dma-noncoherent.h +10
-0+{ +return dev->dma_coherent; +} +#else +static inline int dev_is_dma_coherent (struct device *dev) +{ +return true; +} +#endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */ +void *arch_dma_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs); void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,kernel/dma/Kconfig +0
-0
kernel/dma/Kconfig config NEED_DMA_MAP_STATE · line 17
Quoted 5 lines
kernel/dma/Kconfig +2
-0@@ -13,6 +13,9 @@ config NEED_DMA_MAP_STATE config ARCH_DMA_ADDR_T_64BIT def_bool 64BIT || PHYS_ADDR_T_64BIT +config ARCH_HAS_DMA_COHERENCE_H + bool
This seems a little crude - is it unbearably churny to make an asm-generic/dma-coherence.h implementation for everyone else?
Nits aside, this otherwise looks sane to me for factoring out the equivalent Xen and arm64 DMA ops cases.
Robin.
Quoted 5 lines
kernel/dma/Kconfig +1
-0+ config HAVE_GENERIC_DMA_COHERENT bool
Re: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
Quoted 6 lines · Robin Murphy
include/linux/device.h struct device { · line 1023
Quoted 3 lines
include/linux/device.h +3
-0+#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL )If we're likely to refer to it more than once, is it worth wrapping that condition up in something like
ARCH_HAS_NONCOHERENT_DMA?
The idea is that this is basically wrapped by dev_is_dma_coherent. But independent of the field we have a few other uses, like the definition of dev_is_dma_coherent itself. It'll give the idea with an additional symbol a spin.
Quoted 6 lines
Quoted 6 lines
index
a0aa00cc909d..69630ec320be100644include/linux/dma-noncoherent.h +0
-0@@ -4,6 +4,22 @@#include <linux/dma-mapping.h> +#ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H
Quoted 8 lines
include/linux/dma-noncoherent.h line 12
Quoted 5 lines
include/linux/dma-noncoherent.h +5
-0+#include <asm/dma-coherence.h> +#elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) +static inline int dev_is_dma_coherent (struct device *dev)Given that it's backed by a bool and used as a bool everywhere, this (and its equivalents) should probably return a bool ;)
Indeed.
Quoted 10 lines
kernel/dma/Kconfig config NEED_DMA_MAP_STATE · line 16
Quoted 7 lines
kernel/dma/Kconfig +1
-0@@ -13,6 +13,9 @@ config NEED_DMA_MAP_STATE config ARCH_DMA_ADDR_T_64BIT def_bool 64BIT || PHYS_ADDR_T_64BIT +config ARCH_HAS_DMA_COHERENCE_H + boolThis seems a little crude - is it unbearably churny to make an
asm-generic/dma-coherence.himplementation for everyone else?
The case of having something else than the per-device flag is rather odd, and I hope we don't grow any new user in addition to mips.
In fact I'm already thinking of ways to get rid of it for mips by e.g. iterating over all devices and just setting dma_coherent, but for now I wanted to solve the more urgen issues and tackle this later, as this unification blocks a few other things
Quoted 2 lines
Nits aside, this otherwise looks sane to me for factoring out the equivalent Xen and arm64 DMA ops cases.
Like this? :)
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-maybe-coherent
Robin Murphy
reviewerRe: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines
kernel/dma/Kconfig config NEED_DMA_MAP_STATE · line 16
Quoted 7 lines
kernel/dma/Kconfig +1
-0@@ -13,6 +13,9 @@ config NEED_DMA_MAP_STATE config ARCH_DMA_ADDR_T_64BIT def_bool 64BIT || PHYS_ADDR_T_64BIT +config ARCH_HAS_DMA_COHERENCE_H + boolThis seems a little crude - is it unbearably churny to make an
asm-generic/dma-coherence.himplementation for everyone else?
Quoted 7 lines
The case of having something else than the per-device flag is rather odd, and I hope we don't grow any new user in addition to mips.
In fact I'm already thinking of ways to get rid of it for mips by e.g. iterating over all devices and just setting
dma_coherent, but for now I wanted to solve the more urgen issues and tackle this later, as this unification blocks a few other things
Ah, somehow I started thinking that arm(64) would need to implement their own as well, but I see the point now. In that case, TBH I'd be quite happy with just a simple #ifdef CONFIG_MIPS in linux/dma-noncoherent.h - plus then it looks even more like something nobody else is expected to do.
Quoted 6 lines
Quoted 2 lines
Nits aside, this otherwise looks sane to me for factoring out the equivalent Xen and arm64 DMA ops cases.
Like this? :)
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-maybe-coherent
Man, that's going to take me a *lot* of time to pick through. All those horrendous subtleties that I barely remember!
Robin.
Re: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
Quoted 9 lines · Robin Murphy
Quoted 6 lines
Quoted 2 lines
Nits aside, this otherwise looks sane to me for factoring out the equivalent Xen and arm64 DMA ops cases.
Like this? :)
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-maybe-coherent
Man, that's going to take me a *lot* of time to pick through. All those horrendous subtleties that I barely remember!
The good thing is that if I remember correctly there is just a single patch that (intentionally) changes behavior. The rest is just refactoring and moving code around.
Anyway, I want to get this series in now, and the kick off the swiotlb discussion with Konread. We might not even get to the iommu patches for this merge window depending on how long it takes.
Re: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
In reply to Robin MurphyQuoted 2 lines · Robin Murphy
If we're likely to refer to it more than once, is it worth wrapping that condition up in something like
ARCH_HAS_NONCOHERENT_DMA?
Below is what we'd need. Which to me doesn't look wortwhile for just those two conditionals:
include/linux/device.h +1 -3
@@ -1018,9 +1018,7 @@ struct device { bool offline_disabled:1; bool offline:1; bool of_node_reused:1; -#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ - defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ - defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) +#ifdef CONFIG_ARCH_HAS_NONCOHERENT_DMA bool dma_coherent:1;#endif };
include/linux/dma-noncoherent.h +1 -3
@@ -6,9 +6,7 @@#ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H #include <asm/dma-coherence.h> -#elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ - defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ - defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) +#elif defined(CONFIG_ARCH_HAS_NONCOHERENT_DMA )static inline bool dev_is_dma_coherent (struct device *dev) {return dev->dma_coherent;
kernel/dma/Kconfig +5 -0
@@ -29,6 +29,11 @@ config ARCH_HAS_SYNC_DMA_FOR_CPU config ARCH_HAS_SYNC_DMA_FOR_CPU_ALL bool +config ARCH_HAS_NONCOHERENT_DMA + def_bool ARCH_HAS_SYNC_DMA_FOR_DEVICE || \ + ARCH_HAS_SYNC_DMA_FOR_CPU || \ + ARCH_HAS_SYNC_DMA_FOR_CPU_ALL + config ARCH_HAS_DMA_COHERENT_TO_PFN bool
Greg Kroah-Hartman
maintainerRe: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
In reply to Christoph HellwigQuoted 14 lines · Christoph Hellwig
include/linux/device.h +2
-0@@ -927,6 +927,8 @@ struct dev_links_info {* @offline: Set after successful invocation of bus type's .offline(). * @of_node_reused: Set if the device-tree node is shared with an ancestor * device. +* @dma_coherent: this particular device is dma coherent, even if the +* architecture supports non-coherent devices. * * At the lowest level, every device in a Linux system is represented by an * instance of struct device. The device structure contains the information
include/linux/device.h struct device { · line 1025
Quoted 8 lines
include/linux/device.h +5
-0@@ -1016,6 +1018,11 @@ struct device { bool offline_disabled:1; bool offline:1; bool of_node_reused:1; +#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) + bool dma_coherent:1; +#endif
It's just one bit, why not always have it enabled here? If the arch uses it or doesn't, no big deal.
Or are you using this to "catch" arches that mess something up?
thanks,
greg k-h
Re: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
Quoted 10 lines · Greg Kroah-Hartman
include/linux/device.h struct device { · line 1025
Quoted 5 lines
include/linux/device.h +5
-0+#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) + bool dma_coherent:1; +#endif It's just one bit, why not always have it enabled here? If the arch uses it or doesn't, no big deal.
Or are you using this to "catch" arches that mess something up?
Yes, that is the intent - I don't want architectures to accidentally set it while not selecting the non-coherent infrastructure, as it won't have an effect.
Re: [PATCH 2/5] dma-mapping: move the dma_coherent flag to struct device
Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Greg Kroah-Hartman
include/linux/device.h struct device { · line 1025
Quoted 5 lines
include/linux/device.h +5
-0+#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \ + defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) + bool dma_coherent:1; +#endif It's just one bit, why not always have it enabled here? If the arch uses it or doesn't, no big deal.
Quoted 5 lines
Quoted 1 line
Or are you using this to "catch" arches that mess something up?
Yes, that is the intent - I don't want architectures to accidentally set it while not selecting the non-coherent infrastructure, as it won't have an effect.
Ok, fine with me:
PATCH 3/5 dma-mapping: merge direct and noncoherent ops
In reply to Christoph HellwigAll the cache maintainance is already stubbed out when not enabled, but merging the two allows us to nicely handle the case where cache maintainance is required for some devices, but not others.
--- arch/arc/Kconfig | 2 +- arch/arc/mm/dma.c | 16 ++-- arch/arm/mm/dma-mapping-nommu.c | 5 +- arch/c6x/Kconfig | 2 +- arch/hexagon/Kconfig | 2 +- arch/m68k/Kconfig | 2 +- arch/microblaze/Kconfig | 2 +- arch/mips/Kconfig | 1 - arch/mips/include/asm/dma-mapping.h | 2 - arch/mips/jazz/jazzdma.c | 6 +- arch/mips/mm/dma-noncoherent.c | 29 ++----- arch/nds32/Kconfig | 2 +- arch/nios2/Kconfig | 2 +- arch/openrisc/Kconfig | 2 +- arch/parisc/Kconfig | 2 +- arch/parisc/kernel/setup.c | 2 +- arch/sh/Kconfig | 3 +- arch/sparc/Kconfig | 2 +- arch/sparc/include/asm/dma-mapping.h | 4 +- arch/x86/kernel/amd_gart_64.c | 6 +- arch/xtensa/Kconfig | 2 +- include/asm-generic/dma-mapping.h | 9 -- include/linux/dma-direct.h | 4 + include/linux/dma-mapping.h | 1 - include/linux/dma-noncoherent.h | 5 -- kernel/dma/Kconfig | 9 +- kernel/dma/Makefile | 1 - kernel/dma/direct.c | 121 +++++++++++++++++++++++++-- kernel/dma/noncoherent.c | 106 ----------------------- 29 files changed, 160 insertions(+), 192 deletions(-) delete mode 100644 kernel/dma/noncoherent.c
arch/arc/Kconfig +1 -1
@@ -17,7 +17,7 @@ config ARC select BUILDTIME_EXTABLE_SORT select CLONE_BACKWARDS select COMMON_CLK - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select DMA_NONCOHERENT_MMAP select GENERIC_ATOMIC64 if !ISA_ARCV2 || !(ARC_HAS_LL64 && ARC_HAS_LLSC) select GENERIC_CLOCKEVENTS
arch/arc/mm/dma.c +7 -9
@@ -167,7 +167,7 @@ void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr, }/* -* Plug in coherent or noncoherent dma ops +* Plug in direct dma map ops. */ void arch_setup_dma_ops (struct device *dev,u64 dma_base,u64 size,const struct iommu_ops *iommu,bool coherent)
@@ -175,13 +175,11 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,/* * IOC hardware snoops all DMA traffic keeping the caches consistent * with memory - eliding need for any explicit cache maintenance of -* DMA buffers - so we can use dma_direct cache ops. +* DMA buffers. */ -if (is_isa_arcv2 () && ioc_enable && coherent) { -set_dma_ops (dev, &dma_direct_ops); -dev_info (dev,"use dma_direct_ops cache ops\n" ); - }else { -set_dma_ops (dev, &dma_noncoherent_ops); -dev_info (dev,"use dma_noncoherent_ops cache ops\n" ); - } +if (is_isa_arcv2 () && ioc_enable && coherent) + dev->dma_coherent = true; + +dev_info (dev,"use %sncoherent DMA ops\n" , + dev->dma_coherent ?"" :"non" ); }
arch/arm/mm/dma-mapping-nommu.c +3 -2
@@ -47,7 +47,8 @@ static void *arm_nommu_dma_alloc(struct device *dev, size_t size,*/ if (attrs &DMA_ATTR_NON_CONSISTENT ) -return dma_direct_alloc (dev, size, dma_handle, gfp, attrs); +return dma_direct_alloc_pages (dev, size, dma_handle, gfp, + attrs); ret =dma_alloc_from_global_coherent (size, dma_handle);
@@ -70,7 +71,7 @@ static void arm_nommu_dma_free(struct device *dev, size_t size, unsigned long attrs) {if (attrs &DMA_ATTR_NON_CONSISTENT ) { -dma_direct_free (dev, size, cpu_addr, dma_addr, attrs); +dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs); }else {int ret =dma_release_from_global_coherent (get_order (size), cpu_addr);
arch/c6x/Kconfig +1 -1
@@ -9,7 +9,7 @@ config C6X select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE select CLKDEV_LOOKUP - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select GENERIC_ATOMIC64 select GENERIC_IRQ_SHOW select HAVE_ARCH_TRACEHOOK
arch/hexagon/Kconfig +1 -1
@@ -30,7 +30,7 @@ config HEXAGON select GENERIC_CLOCKEVENTS_BROADCAST select MODULES_USE_ELF_RELA select GENERIC_CPU_DEVICES - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS ---help--- Qualcomm Hexagon is a processor architecture designed for high performance and low power across a wide variety of applications.
arch/m68k/Kconfig +1 -1
@@ -26,7 +26,7 @@ config M68K select MODULES_USE_ELF_RELA select OLD_SIGSUSPEND3 select OLD_SIGACTION - select DMA_NONCOHERENT_OPS if HAS_DMA + select DMA_DIRECT_OPS if HAS_DMA select HAVE_MEMBLOCK select ARCH_DISCARD_MEMBLOCK select NO_BOOTMEM
arch/microblaze/Kconfig +1 -1
@@ -11,7 +11,7 @@ config MICROBLAZE select TIMER_OF select CLONE_BACKWARDS3 select COMMON_CLK - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select DMA_NONCOHERENT_MMAP select GENERIC_ATOMIC64 select GENERIC_CLOCKEVENTS
arch/mips/Kconfig +0 -1
@@ -1121,7 +1121,6 @@ config DMA_NONCOHERENT select NEED_DMA_MAP_STATE select DMA_NONCOHERENT_MMAP select DMA_NONCOHERENT_CACHE_SYNC - select DMA_NONCOHERENT_OPS config SYS_HAS_EARLY_PRINTK bool
arch/mips/include/asm/dma-mapping.h +0 -2
@@ -12,8 +12,6 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)return &jazz_dma_ops;#elif defined(CONFIG_SWIOTLB )return &swiotlb_dma_ops; -#elif defined(CONFIG_DMA_NONCOHERENT_OPS ) -return &dma_noncoherent_ops;#else return &dma_direct_ops;#endif
arch/mips/jazz/jazzdma.c +3 -3
@@ -564,13 +564,13 @@ static void *jazz_dma_alloc(struct device *dev, size_t size, {void *ret; - ret =dma_direct_alloc (dev, size, dma_handle, gfp, attrs); + ret =dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs);if (!ret)return NULL ; *dma_handle =vdma_alloc (virt_to_phys (ret), size);if (*dma_handle ==VDMA_ERROR ) { -dma_direct_free (dev, size, ret, *dma_handle, attrs); +dma_direct_free_pages (dev, size, ret, *dma_handle, attrs);return NULL ; }
@@ -587,7 +587,7 @@ static void jazz_dma_free(struct device *dev, size_t size, void *vaddr,vdma_free (dma_handle);if (!(attrs &DMA_ATTR_NON_CONSISTENT )) vaddr = (void *)CAC_ADDR ((unsigned long )vaddr); -return dma_direct_free (dev, size, vaddr, dma_handle, attrs); +dma_direct_free_pages (dev, size, vaddr, dma_handle, attrs); } static dma_addr_t jazz_dma_map_page(struct device *dev, struct page *page,
arch/mips/mm/dma-noncoherent.c +8 -21
@@ -29,9 +29,6 @@*/ static inline bool cpu_needs_post_dma_flush (struct device *dev) { -if (dev_is_dma_coherent (dev)) -return false; -switch (boot_cpu_type ()) {case CPU_R10000 :case CPU_R12000 :
@@ -52,11 +49,8 @@ void *arch_dma_alloc(struct device *dev, size_t size, {void *ret; - ret =dma_direct_alloc (dev, size, dma_handle, gfp, attrs); -if (!ret) -return NULL ; - -if (!dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) { + ret =dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); +if (!ret && !(attrs &DMA_ATTR_NON_CONSISTENT )) {dma_cache_wback_inv ((unsigned long ) ret, size); ret = (void *)UNCAC_ADDR (ret); }
@@ -67,9 +61,9 @@ void *arch_dma_alloc(struct device *dev, size_t size,void arch_dma_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs) { -if (!(attrs &DMA_ATTR_NON_CONSISTENT ) && !dev_is_dma_coherent (dev)) +if (!(attrs &DMA_ATTR_NON_CONSISTENT )) cpu_addr = (void *)CAC_ADDR ((unsigned long )cpu_addr); -dma_direct_free (dev, size, cpu_addr, dma_addr, attrs); +dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs); } int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
@@ -78,16 +72,11 @@ int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma, {unsigned long user_count =vma_pages (vma);unsigned long count =PAGE_ALIGN (size) >>PAGE_SHIFT ; -unsigned long addr = (unsigned long )cpu_addr; +unsigned long addr =CAC_ADDR ((unsigned long )cpu_addr);unsigned long off = vma->vm_pgoff; -unsigned long pfn; +unsigned long pfn =page_to_pfn (virt_to_page ((void *)addr));int ret = -ENXIO ; -if (!dev_is_dma_coherent (dev)) - addr =CAC_ADDR (addr); - - pfn =page_to_pfn (virt_to_page ((void *)addr)); -if (attrs &DMA_ATTR_WRITE_COMBINE ) vma->vm_page_prot =pgprot_writecombine (vma->vm_page_prot);else
@@ -167,8 +156,7 @@ static inline void dma_sync_phys(phys_addr_t paddr, size_t size,void arch_sync_dma_for_device (struct device *dev,phys_addr_t paddr,size_t size,enum dma_data_direction dir) { -if (!dev_is_dma_coherent (dev)) -dma_sync_phys (paddr, size, dir); +dma_sync_phys (paddr, size, dir); } void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
@@ -183,6 +171,5 @@ void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size, {BUG_ON (direction ==DMA_NONE ); -if (!dev_is_dma_coherent (dev)) -dma_sync_virt (vaddr, size, direction); +dma_sync_virt (vaddr, size, direction); }
arch/nds32/Kconfig +1 -1
@@ -11,7 +11,7 @@ config NDS32 select CLKSRC_MMIO select CLONE_BACKWARDS select COMMON_CLK - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select GENERIC_ATOMIC64 select GENERIC_CPU_DEVICES select GENERIC_CLOCKEVENTS
arch/nios2/Kconfig +1 -1
@@ -4,7 +4,7 @@ config NIOS2 select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE select ARCH_NO_SWAP - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select TIMER_OF select GENERIC_ATOMIC64 select GENERIC_CLOCKEVENTS
arch/openrisc/Kconfig +1 -1
@@ -7,7 +7,7 @@ config OPENRISC def_bool y select ARCH_HAS_SYNC_DMA_FOR_DEVICE - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select OF select OF_EARLY_FLATTREE select IRQ_DOMAIN
arch/parisc/Kconfig +1 -1
@@ -186,7 +186,7 @@ config PA11 depends on PA7000 || PA7100LC || PA7200 || PA7300LC select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select DMA_NONCOHERENT_CACHE_SYNC config PREFETCH
arch/parisc/kernel/setup.c +1 -1
@@ -102,7 +102,7 @@ void __init dma_ops_init(void)case pcxl:/* falls through */ case pcxs:case pcxt: - hppa_dma_ops = &dma_noncoherent_ops; + hppa_dma_ops = &dma_direct_ops;break ;default :break ;
arch/sh/Kconfig +1 -2
@@ -7,6 +7,7 @@ config SUPERH select ARCH_NO_COHERENT_DMA_MMAP if !MMU select HAVE_PATA_PLATFORM select CLKDEV_LOOKUP + select DMA_DIRECT_OPS select HAVE_IDE if HAS_IOPORT_MAP select HAVE_MEMBLOCK select HAVE_MEMBLOCK_NODE_MAP
@@ -158,13 +159,11 @@ config SWAP_IO_SPACE bool config DMA_COHERENT - select DMA_DIRECT_OPS bool config DMA_NONCOHERENT def_bool !DMA_COHERENT select ARCH_HAS_SYNC_DMA_FOR_DEVICE - select DMA_NONCOHERENT_OPS config PGTABLE_LEVELS default 3 if X2TLB
arch/sparc/Kconfig +1 -1
@@ -51,7 +51,7 @@ config SPARC config SPARC32 def_bool !64BIT select ARCH_HAS_SYNC_DMA_FOR_CPU - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select GENERIC_ATOMIC64 select CLZ_TAB select HAVE_UID16
arch/sparc/include/asm/dma-mapping.h +2 -2
@@ -14,11 +14,11 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus) {#ifdef CONFIG_SPARC_LEON if (sparc_cpu_model == sparc_leon) -return &dma_noncoherent_ops; +return &dma_direct_ops;#endif #if defined(CONFIG_SPARC32 ) && defined(CONFIG_PCI )if (bus == &pci_bus_type) -return &dma_noncoherent_ops; +return &dma_direct_ops;#endif return dma_ops; }
arch/x86/kernel/amd_gart_64.c +3 -3
@@ -482,7 +482,7 @@ gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr, {void *vaddr; - vaddr =dma_direct_alloc (dev, size, dma_addr, flag, attrs); + vaddr =dma_direct_alloc_pages (dev, size, dma_addr, flag, attrs);if (!vaddr || !force_iommu || dev->coherent_dma_mask <=DMA_BIT_MASK (24 ))return vaddr;
@@ -494,7 +494,7 @@ gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr, goto out_free;return vaddr; out_free: -dma_direct_free (dev, size, vaddr, *dma_addr, attrs); +dma_direct_free_pages (dev, size, vaddr, *dma_addr, attrs);return NULL ; }
@@ -504,7 +504,7 @@ gart_free_coherent(struct device *dev, size_t size, void *vaddr, dma_addr_t dma_addr, unsigned long attrs) { gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0); -dma_direct_free (dev, size, vaddr, dma_addr, attrs); +dma_direct_free_pages (dev, size, vaddr, dma_addr, attrs); }static int gart_mapping_error (struct device *dev,dma_addr_t dma_addr)
arch/xtensa/Kconfig +1 -1
@@ -12,7 +12,7 @@ config XTENSA select BUILDTIME_EXTABLE_SORT select CLONE_BACKWARDS select COMMON_CLK - select DMA_NONCOHERENT_OPS + select DMA_DIRECT_OPS select GENERIC_ATOMIC64 select GENERIC_CLOCKEVENTS select GENERIC_IRQ_SHOW
include/asm-generic/dma-mapping.h +0 -9
@@ -4,16 +4,7 @@static inline const struct dma_map_ops *get_arch_dma_ops (struct bus_type *bus) { -/* -* Use the non-coherent ops if available. If an architecture wants a -* more fine-grained selection of operations it will have to implement -* get_arch_dma_ops itself or use the per-device dma_ops. -*/ -#ifdef CONFIG_DMA_NONCOHERENT_OPS -return &dma_noncoherent_ops; -#else return &dma_direct_ops; -#endif }#endif /* _ASM_GENERIC_DMA_MAPPING_H */
include/linux/dma-direct.h +4 -0
@@ -59,6 +59,10 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs);void dma_direct_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs); +void *dma_direct_alloc_pages (struct device *dev,size_t size, +dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs); +void dma_direct_free_pages (struct device *dev,size_t size,void *cpu_addr, +dma_addr_t dma_addr,unsigned long attrs);dma_addr_t dma_direct_map_page (struct device *dev,struct page *page,unsigned long offset,size_t size,enum dma_data_direction dir,unsigned long attrs);
include/linux/dma-mapping.h +0 -1
@@ -136,7 +136,6 @@ struct dma_map_ops { };extern const struct dma_map_ops dma_direct_ops; -extern const struct dma_map_ops dma_noncoherent_ops;extern const struct dma_map_ops dma_virt_ops;#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
include/linux/dma-noncoherent.h +0 -5
@@ -24,14 +24,9 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs);void arch_dma_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs); - -#ifdef CONFIG_DMA_NONCOHERENT_MMAP int arch_dma_mmap (struct device *dev,struct vm_area_struct *vma,void *cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs); -#else -#define arch_dma_mmap NULL -#endif /* CONFIG_DMA_NONCOHERENT_MMAP */ #ifdef CONFIG_DMA_NONCOHERENT_CACHE_SYNC void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
kernel/dma/Kconfig +2 -7
@@ -30,18 +30,13 @@ config DMA_DIRECT_OPS bool depends on HAS_DMA -config DMA_NONCOHERENT_OPS - bool - depends on HAS_DMA - select DMA_DIRECT_OPS - config DMA_NONCOHERENT_MMAP bool - depends on DMA_NONCOHERENT_OPS + depends on DMA_DIRECT_OPS config DMA_NONCOHERENT_CACHE_SYNC bool - depends on DMA_NONCOHERENT_OPS + depends on DMA_DIRECT_OPS config DMA_VIRT_OPS bool
kernel/dma/Makefile +0 -1
@@ -4,7 +4,6 @@ obj-$(CONFIG_HAS_DMA) += mapping.o obj-$(CONFIG_DMA_CMA) += contiguous.o obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += coherent.o obj-$(CONFIG_DMA_DIRECT_OPS) += direct.o -obj-$(CONFIG_DMA_NONCOHERENT_OPS) += noncoherent.o obj-$(CONFIG_DMA_VIRT_OPS) += virt.o obj-$(CONFIG_DMA_API_DEBUG) += debug.o obj-$(CONFIG_SWIOTLB) += swiotlb.o
kernel/dma/direct.c +115 -6
172 lines not on this page · raw message
kernel/dma/noncoherent.c deleted +0 -106
106 lines not on this page · raw message
Signature · 1 line
2.18.0
PATCH 4/5 dma-mapping: consolidate the dma mmap implementations
In reply to Christoph HellwigThe only functional differences (modulo a few missing fixes in the arch code) is that architectures without coherent caches need a hook to convert a virtual or dma address into a pfn, given that we don't have the kernel linear mapping available for the otherwise easy virt_to_page call. As a side effect we can support mmap of the per-device coherent area even on architectures not providing the callback, and we make previous dangerous default methods dma_common_mmap actually save for non-coherent architectures by rejecting it without the right helper.
In addition to that we need a hook so that some architectures can override the protection bits when mmaping a dma coherent allocations.
--- arch/arc/Kconfig | 2 +- arch/arc/mm/dma.c | 25 +++------------------ arch/arm/mm/dma-mapping-nommu.c | 2 +- arch/microblaze/Kconfig | 2 +- arch/microblaze/include/asm/pgtable.h | 2 -- arch/microblaze/kernel/dma.c | 22 ------------------ arch/microblaze/mm/consistent.c | 3 ++- arch/mips/Kconfig | 3 ++- arch/mips/jazz/jazzdma.c | 1 - arch/mips/mm/dma-noncoherent.c | 32 ++++++++------------------- drivers/xen/swiotlb-xen.c | 2 +- include/linux/dma-mapping.h | 5 +++-- include/linux/dma-noncoherent.h | 10 +++++++-- kernel/dma/Kconfig | 10 +++++---- kernel/dma/direct.c | 11 --------- kernel/dma/mapping.c | 32 ++++++++++++++++++--------- 16 files changed, 58 insertions(+), 106 deletions(-)
arch/arc/Kconfig +1 -1
@@ -9,6 +9,7 @@ config ARC def_bool y select ARC_TIMERS + select ARCH_HAS_DMA_COHERENT_TO_PFN select ARCH_HAS_PTE_SPECIAL select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE
@@ -18,7 +19,6 @@ config ARC select CLONE_BACKWARDS select COMMON_CLK select DMA_DIRECT_OPS - select DMA_NONCOHERENT_MMAP select GENERIC_ATOMIC64 if !ISA_ARCV2 || !(ARC_HAS_LL64 && ARC_HAS_LLSC) select GENERIC_CLOCKEVENTS select GENERIC_FIND_FIRST_BIT
arch/arc/mm/dma.c +3 -22
@@ -84,29 +84,10 @@ void arch_dma_free(struct device *dev, size_t size, void *vaddr,__free_pages (page,get_order (size)); } -int arch_dma_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t dma_addr,size_t size, -unsigned long attrs) +long arch_dma_coherent_to_pfn (struct device *dev,void *cpu_addr, +dma_addr_t dma_addr) { -unsigned long user_count =vma_pages (vma); -unsigned long count =PAGE_ALIGN (size) >>PAGE_SHIFT ; -unsigned long pfn =__phys_to_pfn (dma_addr); -unsigned long off = vma->vm_pgoff; -int ret = -ENXIO ; - - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -if (dma_mmap_from_dev_coherent (dev, vma, cpu_addr, size, &ret)) -return ret; - -if (off < count && user_count <= (count - off)) { - ret =remap_pfn_range (vma, vma->vm_start, - pfn + off, - user_count <<PAGE_SHIFT , - vma->vm_page_prot); - } - -return ret; +return __phys_to_pfn (dma_addr); }/*
arch/arm/mm/dma-mapping-nommu.c +1 -1
@@ -91,7 +91,7 @@ static int arm_nommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,if (dma_mmap_from_global_coherent (vma, cpu_addr, size, &ret))return ret; -return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); +return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size, attrs); }
arch/microblaze/Kconfig +1 -1
@@ -1,6 +1,7 @@ config MICROBLAZE def_bool y select ARCH_NO_SWAP + select ARCH_HAS_DMA_COHERENT_TO_PFN if MMU select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE
@@ -12,7 +13,6 @@ config MICROBLAZE select CLONE_BACKWARDS3 select COMMON_CLK select DMA_DIRECT_OPS - select DMA_NONCOHERENT_MMAP select GENERIC_ATOMIC64 select GENERIC_CLOCKEVENTS select GENERIC_CPU_DEVICES
arch/microblaze/include/asm/pgtable.h +0 -2
@@ -553,8 +553,6 @@ void __init *early_get_page(void);extern unsigned long ioremap_bot, ioremap_base; -unsigned long consistent_virt_to_pfn (void *vaddr); -void setup_memory (void );#endif /* __ASSEMBLY__ */
arch/microblaze/kernel/dma.c +0 -22
@@ -42,25 +42,3 @@ void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr, {__dma_sync (dev, paddr, size, dir); } - -int arch_dma_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t handle,size_t size, -unsigned long attrs) -{ -#ifdef CONFIG_MMU -unsigned long user_count =vma_pages (vma); -unsigned long count =PAGE_ALIGN (size) >>PAGE_SHIFT ; -unsigned long off = vma->vm_pgoff; -unsigned long pfn; - -if (off >= count || user_count > (count - off)) -return -ENXIO ; - - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - pfn =consistent_virt_to_pfn (cpu_addr); -return remap_pfn_range (vma, vma->vm_start, pfn + off, - vma->vm_end - vma->vm_start, vma->vm_page_prot); -#else -return -ENXIO ; -#endif -}
arch/microblaze/mm/consistent.c +2 -1
@@ -165,7 +165,8 @@ static pte_t *consistent_virt_to_pte(void *vaddr)return pte_offset_kernel (pmd_offset (pgd_offset_k (addr), addr), addr); } -unsigned long consistent_virt_to_pfn (void *vaddr) +long arch_dma_coherent_to_pfn (struct device *dev,void *vaddr, +dma_addr_t dma_addr) {pte_t *ptep =consistent_virt_to_pte (vaddr);
arch/mips/Kconfig +2 -1
@@ -1116,10 +1116,11 @@ config DMA_PERDEV_COHERENT config DMA_NONCOHERENT bool + select ARCH_HAS_DMA_MMAP_PGPROT select ARCH_HAS_SYNC_DMA_FOR_DEVICE select ARCH_HAS_SYNC_DMA_FOR_CPU select NEED_DMA_MAP_STATE - select DMA_NONCOHERENT_MMAP + select ARCH_HAS_DMA_COHERENT_TO_PFN select DMA_NONCOHERENT_CACHE_SYNC config SYS_HAS_EARLY_PRINTK
arch/mips/jazz/jazzdma.c +0 -1
@@ -682,7 +682,6 @@ static int jazz_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)const struct dma_map_ops jazz_dma_ops = { .alloc = jazz_dma_alloc, .free = jazz_dma_free, - .mmap = arch_dma_mmap, .map_page = jazz_dma_map_page, .unmap_page = jazz_dma_unmap_page, .map_sg = jazz_dma_map_sg,
arch/mips/mm/dma-noncoherent.c +9 -23
@@ -66,33 +66,19 @@ void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs); } -int arch_dma_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t dma_addr,size_t size, -unsigned long attrs) +long arch_dma_coherent_to_pfn (struct device *dev,void *cpu_addr, +dma_addr_t dma_addr) { -unsigned long user_count =vma_pages (vma); -unsigned long count =PAGE_ALIGN (size) >>PAGE_SHIFT ;unsigned long addr =CAC_ADDR ((unsigned long )cpu_addr); -unsigned long off = vma->vm_pgoff; -unsigned long pfn =page_to_pfn (virt_to_page ((void *)addr)); -int ret = -ENXIO ; +return page_to_pfn (virt_to_page ((void *)addr)); +} +pgprot_t arch_dma_mmap_pgprot (struct device *dev,pgprot_t prot, +unsigned long attrs) +{if (attrs &DMA_ATTR_WRITE_COMBINE ) - vma->vm_page_prot =pgprot_writecombine (vma->vm_page_prot); -else - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); - -if (dma_mmap_from_dev_coherent (dev, vma, cpu_addr, size, &ret)) -return ret; - -if (off < count && user_count <= (count - off)) { - ret =remap_pfn_range (vma, vma->vm_start, - pfn + off, - user_count <<PAGE_SHIFT , - vma->vm_page_prot); - } - -return ret; +return pgprot_writecombine (prot); +return pgprot_noncached (prot); } static inline void dma_sync_virt(void *addr, size_t size,
drivers/xen/swiotlb-xen.c +1 -1
@@ -662,7 +662,7 @@ xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma,return xen_get_dma_ops (dev)->mmap (dev, vma, cpu_addr, dma_addr, size, attrs);#endif -return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); +return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size, attrs); }/*
include/linux/dma-mapping.h +3 -2
@@ -444,7 +444,8 @@ dma_cache_sync(struct device *dev, void *vaddr, size_t size, }extern int dma_common_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t dma_addr,size_t size); +void *cpu_addr,dma_addr_t dma_addr,size_t size, +unsigned long attrs); void *dma_common_contiguous_remap(struct page *page, size_t size, unsigned long vm_flags,
@@ -476,7 +477,7 @@ dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,BUG_ON (!ops);if (ops->mmap)return ops->mmap (dev, vma, cpu_addr, dma_addr, size, attrs); -return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); +return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size, attrs); }#define dma_mmap_coherent (d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
include/linux/dma-noncoherent.h +8 -2
@@ -24,9 +24,15 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs);void arch_dma_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs); -int arch_dma_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t dma_addr,size_t size, +long arch_dma_coherent_to_pfn (struct device *dev,void *cpu_addr, +dma_addr_t dma_addr); + +#ifdef CONFIG_ARCH_HAS_DMA_MMAP_PGPROT +pgprot_t arch_dma_mmap_pgprot (struct device *dev,pgprot_t prot,unsigned long attrs); +#else +# define arch_dma_mmap_pgprot (dev, prot, attrs) pgprot_noncached(prot) +#endif #ifdef CONFIG_DMA_NONCOHERENT_CACHE_SYNC void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
kernel/dma/Kconfig +6 -4
@@ -26,13 +26,15 @@ config ARCH_HAS_SYNC_DMA_FOR_CPU bool select NEED_DMA_MAP_STATE -config DMA_DIRECT_OPS +config ARCH_HAS_DMA_COHERENT_TO_PFN bool - depends on HAS_DMA -config DMA_NONCOHERENT_MMAP +config ARCH_HAS_DMA_MMAP_PGPROT bool - depends on DMA_DIRECT_OPS + +config DMA_DIRECT_OPS + bool + depends on HAS_DMA config DMA_NONCOHERENT_CACHE_SYNC bool
kernel/dma/direct.c +0 -11
@@ -155,16 +155,6 @@ void dma_direct_free(struct device *dev, size_t size,dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs); } -static int dma_direct_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t dma_addr,size_t size, -unsigned long attrs) -{ -if (!dev_is_dma_coherent (dev) && -IS_ENABLED (CONFIG_DMA_NONCOHERENT_MMAP )) -return arch_dma_mmap (dev, vma, cpu_addr, dma_addr, size, attrs); -return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); -} -static void dma_direct_sync_single_for_device (struct device *dev,dma_addr_t addr,size_t size,enum dma_data_direction dir) {
@@ -293,7 +283,6 @@ int dma_direct_mapping_error(struct device *dev, dma_addr_t dma_addr)const struct dma_map_ops dma_direct_ops = { .alloc = dma_direct_alloc, .free = dma_direct_free, - .mmap = dma_direct_mmap, .map_page = dma_direct_map_page, .map_sg = dma_direct_map_sg, #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE)
kernel/dma/mapping.c +21 -11
@@ -7,7 +7,7 @@*/ #include <linux/acpi.h> -#include <linux/dma-mapping.h> +#include <linux/dma-noncoherent.h> #include <linux/export.h> #include <linux/gfp.h> #include <linux/of_device.h>
@@ -220,27 +220,37 @@ EXPORT_SYMBOL(dma_common_get_sgtable);* Create userspace mapping for the DMA-coherent memory. */ int dma_common_mmap (struct device *dev,struct vm_area_struct *vma, -void *cpu_addr,dma_addr_t dma_addr,size_t size) +void *cpu_addr,dma_addr_t dma_addr,size_t size, +unsigned long attrs) { -int ret = -ENXIO ;#ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP unsigned long user_count =vma_pages (vma);unsigned long count =PAGE_ALIGN (size) >>PAGE_SHIFT ;unsigned long off = vma->vm_pgoff; +unsigned long pfn; +int ret = -ENXIO ; - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); + vma->vm_page_prot =arch_dma_mmap_pgprot (dev, vma->vm_page_prot, attrs);if (dma_mmap_from_dev_coherent (dev, vma, cpu_addr, size, &ret))return ret; -if (off < count && user_count <= (count - off)) - ret =remap_pfn_range (vma, vma->vm_start, -page_to_pfn (virt_to_page (cpu_addr)) + off, - user_count <<PAGE_SHIFT , - vma->vm_page_prot); -#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */ +if (off >= count || user_count > count - off) +return -ENXIO ; + +if (!dev_is_dma_coherent (dev)) { +if (!IS_ENABLED (CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN )) +return -ENXIO ; + pfn =arch_dma_coherent_to_pfn (dev, cpu_addr, dma_addr); + }else { + pfn =page_to_pfn (virt_to_page (cpu_addr)); + } -return ret; +return remap_pfn_range (vma, vma->vm_start, pfn + vma->vm_pgoff, + user_count <<PAGE_SHIFT , vma->vm_page_prot); +#else +return -ENXIO ; +#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */ }EXPORT_SYMBOL (dma_common_mmap);
Signature · 1 line
2.18.0
PATCH 5/5 dma-mapping: support non-coherent devices in dma_common_get_sgtable
In reply to Christoph HellwigWe can use the arch_dma_coherent_to_pfn hook to provide a ->get_sgtable implementation. Note that this isn't an endorsement of this interface (which is a horrible bad idea), but it is required to move arm64 over to the generic code without a loss of functionality.
--- drivers/xen/swiotlb-xen.c | 2 +- include/linux/dma-mapping.h | 7 ++++--- kernel/dma/mapping.c | 23 ++++++++++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-)
drivers/xen/swiotlb-xen.c +1 -1
@@ -689,7 +689,7 @@ xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt, handle, size, attrs); }#endif -return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); +return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size, attrs); }static int xen_swiotlb_mapping_error (struct device *dev,dma_addr_t dma_addr)
include/linux/dma-mapping.h +4 -3
@@ -483,8 +483,8 @@ dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,#define dma_mmap_coherent (d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)int -dma_common_get_sgtable (struct device *dev,struct sg_table *sgt, -void *cpu_addr,dma_addr_t dma_addr,size_t size); +dma_common_get_sgtable (struct device *dev,struct sg_table *sgt,void *cpu_addr, +dma_addr_t dma_addr,size_t size,unsigned long attrs); static inline int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
@@ -496,7 +496,8 @@ dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,if (ops->get_sgtable)return ops->get_sgtable (dev, sgt, cpu_addr, dma_addr, size, attrs); -return dma_common_get_sgtable (dev, sgt, cpu_addr, dma_addr, size); +return dma_common_get_sgtable (dev, sgt, cpu_addr, dma_addr, size, + attrs); }#define dma_get_sgtable (d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
kernel/dma/mapping.c +16 -7
@@ -202,17 +202,26 @@ EXPORT_SYMBOL(dmam_release_declared_memory);* Create scatter-list for the already allocated DMA buffer. */ int dma_common_get_sgtable (struct device *dev,struct sg_table *sgt, -void *cpu_addr,dma_addr_t handle,size_t size) +void *cpu_addr,dma_addr_t dma_addr,size_t size, +unsigned long attrs) { -struct page *page =virt_to_page (cpu_addr); +struct page *page;int ret; - ret =sg_alloc_table (sgt,1 ,GFP_KERNEL ); -if (unlikely (ret)) -return ret; +if (!dev_is_dma_coherent (dev)) { +if (!IS_ENABLED (CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN )) +return -ENXIO ; -sg_set_page (sgt->sgl, page,PAGE_ALIGN (size),0 ); -return 0 ; + page =pfn_to_page (arch_dma_coherent_to_pfn (dev, cpu_addr, + dma_addr)); + }else { + page =virt_to_page (cpu_addr); + } + + ret =sg_alloc_table (sgt,1 ,GFP_KERNEL ); +if (!ret) +sg_set_page (sgt->sgl, page,PAGE_ALIGN (size),0 ); +return ret; }EXPORT_SYMBOL (dma_common_get_sgtable);
Signature · 1 line
2.18.0