History

dma-mapping: add a dma_alloc_need_uncached helper

Check if we need to allocate uncached memory for a device given the allocation flags. Switch over the uncached segment check to this helper to deal with architectures that do not support the dma_cache_sync operation and thus should not returned cacheable memory for DMA_ATTR_NON_CONSISTENT allocations.

Christoph Hellwig committed 7 years ago

Commit 4b85fae1 parent 961729b2 files · +16 −2Browse files

Unified · Split

2 files
  1. include/linux/dma-noncoherent.h +14 −0
  2. kernel/dma/direct.c +2 −2
include/linux/dma-noncoherent.h+14−0
@@ -20,6 +20,20 @@ static inline bool dev_is_dma_coherent(struct device *dev) · 19 unmodified lines
2020 }
2121 #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
2222
23+/*
24+ * Check if an allocation needs to be marked uncached to be coherent.
25+ */
26+static inline bool dma_alloc_need_uncached(struct device *dev,
27+ unsigned long attrs)
28+{
29+ if (dev_is_dma_coherent(dev))
30+ return false;
31+ if (IS_ENABLED(CONFIG_DMA_NONCOHERENT_CACHE_SYNC) &&
32+ (attrs & DMA_ATTR_NON_CONSISTENT))
33+ return false;
34+ return true;
35+}
36+
2337 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
2438 gfp_t gfp, unsigned long attrs);
2539 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
kernel/dma/direct.c+2−2
@@ -160,7 +160,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, · 159 unmodified lines
160160 memset(ret, 0, size);
161161
162162 if (IS_ENABLED(CONFIG_ARCH_HAS_UNCACHED_SEGMENT) &&
163− !dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_NON_CONSISTENT)) {
163+ dma_alloc_need_uncached(dev, attrs)) {
164164 arch_dma_prep_coherent(page, size);
165165 ret = uncached_kernel_address(ret);
166166 }
@@ -182,7 +182,7 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, · 15 unmodified lines
182182 set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order);
183183
184184 if (IS_ENABLED(CONFIG_ARCH_HAS_UNCACHED_SEGMENT) &&
185− !dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_NON_CONSISTENT))
185+ dma_alloc_need_uncached(dev, attrs))
186186 cpu_addr = cached_kernel_address(cpu_addr);
187187 __dma_direct_free_pages(dev, size, virt_to_page(cpu_addr));
188188 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t