Skip to content

Commit f0edfea

Browse files
author
Christoph Hellwig
committed
dma-mapping: move the remap helpers to a separate file
The dma remap code only makes sense for not cache coherent architectures (or possibly the corner case of highmem CMA allocations) and currently is only used by arm, arm64, csky and xtensa. Split it out into a separate file with a separate Kconfig symbol, which gets the right copyright notice given that this code was written by Laura Abbott working for Code Aurora at that point. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Laura Abbott <labbott@redhat.com> Reviewed-by: Robin Murphy <robin.murphy@arm.com>
1 parent 704f2c2 commit f0edfea

8 files changed

Lines changed: 97 additions & 85 deletions

File tree

‎arch/arm/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ config ARM
3030
select CPU_PM if (SUSPEND || CPU_IDLE)
3131
select DCACHE_WORD_ACCESS if HAVE_EFFICIENT_UNALIGNED_ACCESS
3232
select DMA_DIRECT_OPS if !MMU
33+
select DMA_REMAP if MMU
3334
select EDAC_SUPPORT
3435
select EDAC_ATOMIC_SCRUB
3536
select GENERIC_ALLOCATOR

‎arch/arm64/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ config ARM64
8282
select CRC32
8383
select DCACHE_WORD_ACCESS
8484
select DMA_DIRECT_OPS
85+
select DMA_REMAP
8586
select EDAC_SUPPORT
8687
select FRAME_POINTER
8788
select GENERIC_ALLOCATOR

‎arch/csky/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config CSKY
99
select CLKSRC_OF
1010
select DMA_DIRECT_OPS
1111
select DMA_NONCOHERENT_OPS
12+
select DMA_REMAP
1213
select IRQ_DOMAIN
1314
select HANDLE_DOMAIN_IRQ
1415
select DW_APB_TIMER_OF

‎arch/xtensa/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ config XTENSA
1111
select CLONE_BACKWARDS
1212
select COMMON_CLK
1313
select DMA_DIRECT_OPS
14+
select DMA_REMAP if MMU
1415
select GENERIC_ATOMIC64
1516
select GENERIC_CLOCKEVENTS
1617
select GENERIC_IRQ_SHOW

‎kernel/dma/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ config SWIOTLB
5151
bool
5252
select DMA_DIRECT_OPS
5353
select NEED_DMA_MAP_STATE
54+
55+
config DMA_REMAP
56+
depends on MMU
57+
bool

‎kernel/dma/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ obj-$(CONFIG_DMA_DIRECT_OPS) += direct.o
77
obj-$(CONFIG_DMA_VIRT_OPS) += virt.o
88
obj-$(CONFIG_DMA_API_DEBUG) += debug.o
99
obj-$(CONFIG_SWIOTLB) += swiotlb.o
10-
10+
obj-$(CONFIG_DMA_REMAP) += remap.o

‎kernel/dma/mapping.c‎

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -262,87 +262,3 @@ int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
262262
#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
263263
}
264264
EXPORT_SYMBOL(dma_common_mmap);
265-
266-
#ifdef CONFIG_MMU
267-
static struct vm_struct *__dma_common_pages_remap(struct page **pages,
268-
size_t size, unsigned long vm_flags, pgprot_t prot,
269-
const void *caller)
270-
{
271-
struct vm_struct *area;
272-
273-
area = get_vm_area_caller(size, vm_flags, caller);
274-
if (!area)
275-
return NULL;
276-
277-
if (map_vm_area(area, prot, pages)) {
278-
vunmap(area->addr);
279-
return NULL;
280-
}
281-
282-
return area;
283-
}
284-
285-
/*
286-
* remaps an array of PAGE_SIZE pages into another vm_area
287-
* Cannot be used in non-sleeping contexts
288-
*/
289-
void *dma_common_pages_remap(struct page **pages, size_t size,
290-
unsigned long vm_flags, pgprot_t prot,
291-
const void *caller)
292-
{
293-
struct vm_struct *area;
294-
295-
area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
296-
if (!area)
297-
return NULL;
298-
299-
area->pages = pages;
300-
301-
return area->addr;
302-
}
303-
304-
/*
305-
* remaps an allocated contiguous region into another vm_area.
306-
* Cannot be used in non-sleeping contexts
307-
*/
308-
309-
void *dma_common_contiguous_remap(struct page *page, size_t size,
310-
unsigned long vm_flags,
311-
pgprot_t prot, const void *caller)
312-
{
313-
int i;
314-
struct page **pages;
315-
struct vm_struct *area;
316-
317-
pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
318-
if (!pages)
319-
return NULL;
320-
321-
for (i = 0; i < (size >> PAGE_SHIFT); i++)
322-
pages[i] = nth_page(page, i);
323-
324-
area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
325-
326-
kfree(pages);
327-
328-
if (!area)
329-
return NULL;
330-
return area->addr;
331-
}
332-
333-
/*
334-
* unmaps a range previously mapped by dma_common_*_remap
335-
*/
336-
void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
337-
{
338-
struct vm_struct *area = find_vm_area(cpu_addr);
339-
340-
if (!area || (area->flags & vm_flags) != vm_flags) {
341-
WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
342-
return;
343-
}
344-
345-
unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
346-
vunmap(cpu_addr);
347-
}
348-
#endif

‎kernel/dma/remap.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (c) 2014 The Linux Foundation
4+
*/
5+
#include <linux/dma-mapping.h>
6+
#include <linux/slab.h>
7+
#include <linux/vmalloc.h>
8+
9+
static struct vm_struct *__dma_common_pages_remap(struct page **pages,
10+
size_t size, unsigned long vm_flags, pgprot_t prot,
11+
const void *caller)
12+
{
13+
struct vm_struct *area;
14+
15+
area = get_vm_area_caller(size, vm_flags, caller);
16+
if (!area)
17+
return NULL;
18+
19+
if (map_vm_area(area, prot, pages)) {
20+
vunmap(area->addr);
21+
return NULL;
22+
}
23+
24+
return area;
25+
}
26+
27+
/*
28+
* Remaps an array of PAGE_SIZE pages into another vm_area.
29+
* Cannot be used in non-sleeping contexts
30+
*/
31+
void *dma_common_pages_remap(struct page **pages, size_t size,
32+
unsigned long vm_flags, pgprot_t prot,
33+
const void *caller)
34+
{
35+
struct vm_struct *area;
36+
37+
area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
38+
if (!area)
39+
return NULL;
40+
41+
area->pages = pages;
42+
43+
return area->addr;
44+
}
45+
46+
/*
47+
* Remaps an allocated contiguous region into another vm_area.
48+
* Cannot be used in non-sleeping contexts
49+
*/
50+
void *dma_common_contiguous_remap(struct page *page, size_t size,
51+
unsigned long vm_flags,
52+
pgprot_t prot, const void *caller)
53+
{
54+
int i;
55+
struct page **pages;
56+
struct vm_struct *area;
57+
58+
pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
59+
if (!pages)
60+
return NULL;
61+
62+
for (i = 0; i < (size >> PAGE_SHIFT); i++)
63+
pages[i] = nth_page(page, i);
64+
65+
area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
66+
67+
kfree(pages);
68+
69+
if (!area)
70+
return NULL;
71+
return area->addr;
72+
}
73+
74+
/*
75+
* Unmaps a range previously mapped by dma_common_*_remap
76+
*/
77+
void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
78+
{
79+
struct vm_struct *area = find_vm_area(cpu_addr);
80+
81+
if (!area || (area->flags & vm_flags) != vm_flags) {
82+
WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
83+
return;
84+
}
85+
86+
unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
87+
vunmap(cpu_addr);
88+
}

0 commit comments

Comments
 (0)