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 <asm-generic/dma-coherent.h>
21 dma_addr_t dma_handle
;
24 static void dmam_coherent_release(struct device
*dev
, void *res
)
26 struct dma_devres
*this = res
;
28 dma_free_coherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
31 static void dmam_noncoherent_release(struct device
*dev
, void *res
)
33 struct dma_devres
*this = res
;
35 dma_free_noncoherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
38 static int dmam_match(struct device
*dev
, void *res
, void *match_data
)
40 struct dma_devres
*this = res
, *match
= match_data
;
42 if (this->vaddr
== match
->vaddr
) {
43 WARN_ON(this->size
!= match
->size
||
44 this->dma_handle
!= match
->dma_handle
);
51 * dmam_alloc_coherent - Managed dma_alloc_coherent()
52 * @dev: Device to allocate coherent memory for
53 * @size: Size of allocation
54 * @dma_handle: Out argument for allocated DMA handle
55 * @gfp: Allocation flags
57 * Managed dma_alloc_coherent(). Memory allocated using this function
58 * will be automatically released on driver detach.
61 * Pointer to allocated memory on success, NULL on failure.
63 void * dmam_alloc_coherent(struct device
*dev
, size_t size
,
64 dma_addr_t
*dma_handle
, gfp_t gfp
)
66 struct dma_devres
*dr
;
69 dr
= devres_alloc(dmam_coherent_release
, sizeof(*dr
), gfp
);
73 vaddr
= dma_alloc_coherent(dev
, size
, dma_handle
, gfp
);
80 dr
->dma_handle
= *dma_handle
;
87 EXPORT_SYMBOL(dmam_alloc_coherent
);
90 * dmam_free_coherent - Managed dma_free_coherent()
91 * @dev: Device to free coherent memory for
92 * @size: Size of allocation
93 * @vaddr: Virtual address of the memory to free
94 * @dma_handle: DMA handle of the memory to free
96 * Managed dma_free_coherent().
98 void dmam_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
99 dma_addr_t dma_handle
)
101 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
103 dma_free_coherent(dev
, size
, vaddr
, dma_handle
);
104 WARN_ON(devres_destroy(dev
, dmam_coherent_release
, dmam_match
,
107 EXPORT_SYMBOL(dmam_free_coherent
);
110 * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
111 * @dev: Device to allocate non_coherent memory for
112 * @size: Size of allocation
113 * @dma_handle: Out argument for allocated DMA handle
114 * @gfp: Allocation flags
116 * Managed dma_alloc_non_coherent(). Memory allocated using this
117 * function will be automatically released on driver detach.
120 * Pointer to allocated memory on success, NULL on failure.
122 void *dmam_alloc_noncoherent(struct device
*dev
, size_t size
,
123 dma_addr_t
*dma_handle
, gfp_t gfp
)
125 struct dma_devres
*dr
;
128 dr
= devres_alloc(dmam_noncoherent_release
, sizeof(*dr
), gfp
);
132 vaddr
= dma_alloc_noncoherent(dev
, size
, dma_handle
, gfp
);
139 dr
->dma_handle
= *dma_handle
;
146 EXPORT_SYMBOL(dmam_alloc_noncoherent
);
149 * dmam_free_coherent - Managed dma_free_noncoherent()
150 * @dev: Device to free noncoherent memory for
151 * @size: Size of allocation
152 * @vaddr: Virtual address of the memory to free
153 * @dma_handle: DMA handle of the memory to free
155 * Managed dma_free_noncoherent().
157 void dmam_free_noncoherent(struct device
*dev
, size_t size
, void *vaddr
,
158 dma_addr_t dma_handle
)
160 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
162 dma_free_noncoherent(dev
, size
, vaddr
, dma_handle
);
163 WARN_ON(!devres_destroy(dev
, dmam_noncoherent_release
, dmam_match
,
166 EXPORT_SYMBOL(dmam_free_noncoherent
);
168 #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
170 static void dmam_coherent_decl_release(struct device
*dev
, void *res
)
172 dma_release_declared_memory(dev
);
176 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
177 * @dev: Device to declare coherent memory for
178 * @bus_addr: Bus address of coherent memory to be declared
179 * @device_addr: Device address of coherent memory to be declared
180 * @size: Size of coherent memory to be declared
183 * Managed dma_declare_coherent_memory().
186 * 0 on success, -errno on failure.
188 int dmam_declare_coherent_memory(struct device
*dev
, dma_addr_t bus_addr
,
189 dma_addr_t device_addr
, size_t size
, int flags
)
194 res
= devres_alloc(dmam_coherent_decl_release
, 0, GFP_KERNEL
);
198 rc
= dma_declare_coherent_memory(dev
, bus_addr
, device_addr
, size
,
201 devres_add(dev
, res
);
207 EXPORT_SYMBOL(dmam_declare_coherent_memory
);
210 * dmam_release_declared_memory - Managed dma_release_declared_memory().
211 * @dev: Device to release declared coherent memory for
213 * Managed dmam_release_declared_memory().
215 void dmam_release_declared_memory(struct device
*dev
)
217 WARN_ON(devres_destroy(dev
, dmam_coherent_decl_release
, NULL
, NULL
));
219 EXPORT_SYMBOL(dmam_release_declared_memory
);
224 * Create scatter-list for the already allocated DMA buffer.
226 int dma_common_get_sgtable(struct device
*dev
, struct sg_table
*sgt
,
227 void *cpu_addr
, dma_addr_t handle
, size_t size
)
229 struct page
*page
= virt_to_page(cpu_addr
);
232 ret
= sg_alloc_table(sgt
, 1, GFP_KERNEL
);
236 sg_set_page(sgt
->sgl
, page
, PAGE_ALIGN(size
), 0);
239 EXPORT_SYMBOL(dma_common_get_sgtable
);
242 * Create userspace mapping for the DMA-coherent memory.
244 int dma_common_mmap(struct device
*dev
, struct vm_area_struct
*vma
,
245 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
249 unsigned long user_count
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
250 unsigned long count
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
251 unsigned long pfn
= page_to_pfn(virt_to_page(cpu_addr
));
252 unsigned long off
= vma
->vm_pgoff
;
254 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
256 if (dma_mmap_from_coherent(dev
, vma
, cpu_addr
, size
, &ret
))
259 if (off
< count
&& user_count
<= (count
- off
)) {
260 ret
= remap_pfn_range(vma
, vma
->vm_start
,
262 user_count
<< PAGE_SHIFT
,
265 #endif /* CONFIG_MMU */
269 EXPORT_SYMBOL(dma_common_mmap
);