2 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
7 * This file is released under the GPLv2.
10 #include <linux/dma-mapping.h>
11 #include <linux/export.h>
12 #include <linux/gfp.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
22 dma_addr_t dma_handle
;
25 static void dmam_coherent_release(struct device
*dev
, void *res
)
27 struct dma_devres
*this = res
;
29 dma_free_coherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
32 static void dmam_noncoherent_release(struct device
*dev
, void *res
)
34 struct dma_devres
*this = res
;
36 dma_free_noncoherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
39 static int dmam_match(struct device
*dev
, void *res
, void *match_data
)
41 struct dma_devres
*this = res
, *match
= match_data
;
43 if (this->vaddr
== match
->vaddr
) {
44 WARN_ON(this->size
!= match
->size
||
45 this->dma_handle
!= match
->dma_handle
);
52 * dmam_alloc_coherent - Managed dma_alloc_coherent()
53 * @dev: Device to allocate coherent memory for
54 * @size: Size of allocation
55 * @dma_handle: Out argument for allocated DMA handle
56 * @gfp: Allocation flags
58 * Managed dma_alloc_coherent(). Memory allocated using this function
59 * will be automatically released on driver detach.
62 * Pointer to allocated memory on success, NULL on failure.
64 void *dmam_alloc_coherent(struct device
*dev
, size_t size
,
65 dma_addr_t
*dma_handle
, gfp_t gfp
)
67 struct dma_devres
*dr
;
70 dr
= devres_alloc(dmam_coherent_release
, sizeof(*dr
), gfp
);
74 vaddr
= dma_alloc_coherent(dev
, size
, dma_handle
, gfp
);
81 dr
->dma_handle
= *dma_handle
;
88 EXPORT_SYMBOL(dmam_alloc_coherent
);
91 * dmam_free_coherent - Managed dma_free_coherent()
92 * @dev: Device to free coherent memory for
93 * @size: Size of allocation
94 * @vaddr: Virtual address of the memory to free
95 * @dma_handle: DMA handle of the memory to free
97 * Managed dma_free_coherent().
99 void dmam_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
100 dma_addr_t dma_handle
)
102 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
104 dma_free_coherent(dev
, size
, vaddr
, dma_handle
);
105 WARN_ON(devres_destroy(dev
, dmam_coherent_release
, dmam_match
,
108 EXPORT_SYMBOL(dmam_free_coherent
);
111 * dmam_alloc_non_coherent - Managed dma_alloc_noncoherent()
112 * @dev: Device to allocate non_coherent memory for
113 * @size: Size of allocation
114 * @dma_handle: Out argument for allocated DMA handle
115 * @gfp: Allocation flags
117 * Managed dma_alloc_noncoherent(). Memory allocated using this
118 * function will be automatically released on driver detach.
121 * Pointer to allocated memory on success, NULL on failure.
123 void *dmam_alloc_noncoherent(struct device
*dev
, size_t size
,
124 dma_addr_t
*dma_handle
, gfp_t gfp
)
126 struct dma_devres
*dr
;
129 dr
= devres_alloc(dmam_noncoherent_release
, sizeof(*dr
), gfp
);
133 vaddr
= dma_alloc_noncoherent(dev
, size
, dma_handle
, gfp
);
140 dr
->dma_handle
= *dma_handle
;
147 EXPORT_SYMBOL(dmam_alloc_noncoherent
);
150 * dmam_free_coherent - Managed dma_free_noncoherent()
151 * @dev: Device to free noncoherent memory for
152 * @size: Size of allocation
153 * @vaddr: Virtual address of the memory to free
154 * @dma_handle: DMA handle of the memory to free
156 * Managed dma_free_noncoherent().
158 void dmam_free_noncoherent(struct device
*dev
, size_t size
, void *vaddr
,
159 dma_addr_t dma_handle
)
161 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
163 dma_free_noncoherent(dev
, size
, vaddr
, dma_handle
);
164 WARN_ON(!devres_destroy(dev
, dmam_noncoherent_release
, dmam_match
,
167 EXPORT_SYMBOL(dmam_free_noncoherent
);
169 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
171 static void dmam_coherent_decl_release(struct device
*dev
, void *res
)
173 dma_release_declared_memory(dev
);
177 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
178 * @dev: Device to declare coherent memory for
179 * @phys_addr: Physical address of coherent memory to be declared
180 * @device_addr: Device address of coherent memory to be declared
181 * @size: Size of coherent memory to be declared
184 * Managed dma_declare_coherent_memory().
187 * 0 on success, -errno on failure.
189 int dmam_declare_coherent_memory(struct device
*dev
, phys_addr_t phys_addr
,
190 dma_addr_t device_addr
, size_t size
, int flags
)
195 res
= devres_alloc(dmam_coherent_decl_release
, 0, GFP_KERNEL
);
199 rc
= dma_declare_coherent_memory(dev
, phys_addr
, device_addr
, size
,
202 devres_add(dev
, res
);
211 EXPORT_SYMBOL(dmam_declare_coherent_memory
);
214 * dmam_release_declared_memory - Managed dma_release_declared_memory().
215 * @dev: Device to release declared coherent memory for
217 * Managed dmam_release_declared_memory().
219 void dmam_release_declared_memory(struct device
*dev
)
221 WARN_ON(devres_destroy(dev
, dmam_coherent_decl_release
, NULL
, NULL
));
223 EXPORT_SYMBOL(dmam_release_declared_memory
);
228 * Create scatter-list for the already allocated DMA buffer.
230 int dma_common_get_sgtable(struct device
*dev
, struct sg_table
*sgt
,
231 void *cpu_addr
, dma_addr_t handle
, size_t size
)
233 struct page
*page
= virt_to_page(cpu_addr
);
236 ret
= sg_alloc_table(sgt
, 1, GFP_KERNEL
);
240 sg_set_page(sgt
->sgl
, page
, PAGE_ALIGN(size
), 0);
243 EXPORT_SYMBOL(dma_common_get_sgtable
);
246 * Create userspace mapping for the DMA-coherent memory.
248 int dma_common_mmap(struct device
*dev
, struct vm_area_struct
*vma
,
249 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
252 #if defined(CONFIG_MMU) && !defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP)
253 unsigned long user_count
= vma_pages(vma
);
254 unsigned long count
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
255 unsigned long pfn
= page_to_pfn(virt_to_page(cpu_addr
));
256 unsigned long off
= vma
->vm_pgoff
;
258 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
260 if (dma_mmap_from_coherent(dev
, vma
, cpu_addr
, size
, &ret
))
263 if (off
< count
&& user_count
<= (count
- off
)) {
264 ret
= remap_pfn_range(vma
, vma
->vm_start
,
266 user_count
<< PAGE_SHIFT
,
269 #endif /* CONFIG_MMU && !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
273 EXPORT_SYMBOL(dma_common_mmap
);
277 * remaps an array of PAGE_SIZE pages into another vm_area
278 * Cannot be used in non-sleeping contexts
280 void *dma_common_pages_remap(struct page
**pages
, size_t size
,
281 unsigned long vm_flags
, pgprot_t prot
,
284 struct vm_struct
*area
;
286 area
= get_vm_area_caller(size
, vm_flags
, caller
);
292 if (map_vm_area(area
, prot
, pages
)) {
301 * remaps an allocated contiguous region into another vm_area.
302 * Cannot be used in non-sleeping contexts
305 void *dma_common_contiguous_remap(struct page
*page
, size_t size
,
306 unsigned long vm_flags
,
307 pgprot_t prot
, const void *caller
)
314 pages
= kmalloc(sizeof(struct page
*) << get_order(size
), GFP_KERNEL
);
318 for (i
= 0, pfn
= page_to_pfn(page
); i
< (size
>> PAGE_SHIFT
); i
++)
319 pages
[i
] = pfn_to_page(pfn
+ i
);
321 ptr
= dma_common_pages_remap(pages
, size
, vm_flags
, prot
, caller
);
329 * unmaps a range previously mapped by dma_common_*_remap
331 void dma_common_free_remap(void *cpu_addr
, size_t size
, unsigned long vm_flags
)
333 struct vm_struct
*area
= find_vm_area(cpu_addr
);
335 if (!area
|| (area
->flags
& vm_flags
) != vm_flags
) {
336 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr
);
340 unmap_kernel_range((unsigned long)cpu_addr
, PAGE_ALIGN(size
));