Skip to content

Commit f3ecc0f

Browse files
author
Christoph Hellwig
committed
dma-mapping: move the dma_coherent flag to struct device
Various 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. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Paul Burton <paul.burton@mips.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5748e1b commit f3ecc0f

File tree

9 files changed

+41
-46
lines changed

9 files changed

+41
-46
lines changed

arch/mips/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ config ARCH_SUPPORTS_UPROBES
11061106
bool
11071107

11081108
config DMA_MAYBE_COHERENT
1109+
select ARCH_HAS_DMA_COHERENCE_H
11091110
select DMA_NONCOHERENT
11101111
bool
11111112

arch/mips/include/asm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# MIPS headers
22
generic-(CONFIG_GENERIC_CSUM) += checksum.h
33
generic-y += current.h
4+
generic-y += device.h
45
generic-y += dma-contiguous.h
56
generic-y += emergency-restart.h
67
generic-y += export.h

arch/mips/include/asm/device.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

arch/mips/include/asm/dma-coherence.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ enum coherent_io_user_state {
2020
#elif defined(CONFIG_DMA_MAYBE_COHERENT)
2121
extern enum coherent_io_user_state coherentio;
2222
extern int hw_coherentio;
23+
24+
static inline bool dev_is_dma_coherent(struct device *dev)
25+
{
26+
return coherentio == IO_COHERENCE_ENABLED ||
27+
(coherentio == IO_COHERENCE_DEFAULT && hw_coherentio);
28+
}
2329
#else
2430
#ifdef CONFIG_DMA_NONCOHERENT
2531
#define coherentio IO_COHERENCE_DISABLED

arch/mips/include/asm/dma-mapping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
2525
bool coherent)
2626
{
2727
#ifdef CONFIG_DMA_PERDEV_COHERENT
28-
dev->archdata.dma_coherent = coherent;
28+
dev->dma_coherent = coherent;
2929
#endif
3030
}
3131

arch/mips/mm/dma-noncoherent.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@
1414
#include <asm/dma-coherence.h>
1515
#include <asm/io.h>
1616

17-
#ifdef CONFIG_DMA_PERDEV_COHERENT
18-
static inline int dev_is_coherent(struct device *dev)
19-
{
20-
return dev->archdata.dma_coherent;
21-
}
22-
#else
23-
static inline int dev_is_coherent(struct device *dev)
24-
{
25-
switch (coherentio) {
26-
default:
27-
case IO_COHERENCE_DEFAULT:
28-
return hw_coherentio;
29-
case IO_COHERENCE_ENABLED:
30-
return 1;
31-
case IO_COHERENCE_DISABLED:
32-
return 0;
33-
}
34-
}
35-
#endif /* CONFIG_DMA_PERDEV_COHERENT */
36-
3717
/*
3818
* The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
3919
* 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)
4929
*/
5030
static inline bool cpu_needs_post_dma_flush(struct device *dev)
5131
{
52-
if (dev_is_coherent(dev))
32+
if (dev_is_dma_coherent(dev))
5333
return false;
5434

5535
switch (boot_cpu_type()) {
@@ -76,7 +56,7 @@ void *arch_dma_alloc(struct device *dev, size_t size,
7656
if (!ret)
7757
return NULL;
7858

79-
if (!dev_is_coherent(dev) && !(attrs & DMA_ATTR_NON_CONSISTENT)) {
59+
if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_NON_CONSISTENT)) {
8060
dma_cache_wback_inv((unsigned long) ret, size);
8161
ret = (void *)UNCAC_ADDR(ret);
8262
}
@@ -87,7 +67,7 @@ void *arch_dma_alloc(struct device *dev, size_t size,
8767
void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
8868
dma_addr_t dma_addr, unsigned long attrs)
8969
{
90-
if (!(attrs & DMA_ATTR_NON_CONSISTENT) && !dev_is_coherent(dev))
70+
if (!(attrs & DMA_ATTR_NON_CONSISTENT) && !dev_is_dma_coherent(dev))
9171
cpu_addr = (void *)CAC_ADDR((unsigned long)cpu_addr);
9272
dma_direct_free(dev, size, cpu_addr, dma_addr, attrs);
9373
}
@@ -103,7 +83,7 @@ int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
10383
unsigned long pfn;
10484
int ret = -ENXIO;
10585

106-
if (!dev_is_coherent(dev))
86+
if (!dev_is_dma_coherent(dev))
10787
addr = CAC_ADDR(addr);
10888

10989
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,
187167
void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
188168
size_t size, enum dma_data_direction dir)
189169
{
190-
if (!dev_is_coherent(dev))
170+
if (!dev_is_dma_coherent(dev))
191171
dma_sync_phys(paddr, size, dir);
192172
}
193173

@@ -203,6 +183,6 @@ void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
203183
{
204184
BUG_ON(direction == DMA_NONE);
205185

206-
if (!dev_is_coherent(dev))
186+
if (!dev_is_dma_coherent(dev))
207187
dma_sync_virt(vaddr, size, direction);
208188
}

include/linux/device.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ struct dev_links_info {
927927
* @offline: Set after successful invocation of bus type's .offline().
928928
* @of_node_reused: Set if the device-tree node is shared with an ancestor
929929
* device.
930+
* @dma_coherent: this particular device is dma coherent, even if the
931+
* architecture supports non-coherent devices.
930932
*
931933
* At the lowest level, every device in a Linux system is represented by an
932934
* instance of struct device. The device structure contains the information
@@ -1016,6 +1018,11 @@ struct device {
10161018
bool offline_disabled:1;
10171019
bool offline:1;
10181020
bool of_node_reused:1;
1021+
#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
1022+
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
1023+
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
1024+
bool dma_coherent:1;
1025+
#endif
10191026
};
10201027

10211028
static inline struct device *kobj_to_dev(struct kobject *kobj)

include/linux/dma-noncoherent.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44

55
#include <linux/dma-mapping.h>
66

7+
#ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H
8+
#include <asm/dma-coherence.h>
9+
#elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
10+
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
11+
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
12+
static inline bool dev_is_dma_coherent(struct device *dev)
13+
{
14+
return dev->dma_coherent;
15+
}
16+
#else
17+
static inline bool dev_is_dma_coherent(struct device *dev)
18+
{
19+
return true;
20+
}
21+
#endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
22+
723
void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
824
gfp_t gfp, unsigned long attrs);
925
void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,

kernel/dma/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ config NEED_DMA_MAP_STATE
1313
config ARCH_DMA_ADDR_T_64BIT
1414
def_bool 64BIT || PHYS_ADDR_T_64BIT
1515

16+
config ARCH_HAS_DMA_COHERENCE_H
17+
bool
18+
1619
config HAVE_GENERIC_DMA_COHERENT
1720
bool
1821

0 commit comments

Comments
 (0)