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/acpi.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/export.h>
13 #include <linux/gfp.h>
14 #include <linux/of_device.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
24 dma_addr_t dma_handle
;
27 static void dmam_coherent_release(struct device
*dev
, void *res
)
29 struct dma_devres
*this = res
;
31 dma_free_coherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
34 static void dmam_noncoherent_release(struct device
*dev
, void *res
)
36 struct dma_devres
*this = res
;
38 dma_free_noncoherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
41 static int dmam_match(struct device
*dev
, void *res
, void *match_data
)
43 struct dma_devres
*this = res
, *match
= match_data
;
45 if (this->vaddr
== match
->vaddr
) {
46 WARN_ON(this->size
!= match
->size
||
47 this->dma_handle
!= match
->dma_handle
);
54 * dmam_alloc_coherent - Managed dma_alloc_coherent()
55 * @dev: Device to allocate coherent memory for
56 * @size: Size of allocation
57 * @dma_handle: Out argument for allocated DMA handle
58 * @gfp: Allocation flags
60 * Managed dma_alloc_coherent(). Memory allocated using this function
61 * will be automatically released on driver detach.
64 * Pointer to allocated memory on success, NULL on failure.
66 void *dmam_alloc_coherent(struct device
*dev
, size_t size
,
67 dma_addr_t
*dma_handle
, gfp_t gfp
)
69 struct dma_devres
*dr
;
72 dr
= devres_alloc(dmam_coherent_release
, sizeof(*dr
), gfp
);
76 vaddr
= dma_alloc_coherent(dev
, size
, dma_handle
, gfp
);
83 dr
->dma_handle
= *dma_handle
;
90 EXPORT_SYMBOL(dmam_alloc_coherent
);
93 * dmam_free_coherent - Managed dma_free_coherent()
94 * @dev: Device to free coherent memory for
95 * @size: Size of allocation
96 * @vaddr: Virtual address of the memory to free
97 * @dma_handle: DMA handle of the memory to free
99 * Managed dma_free_coherent().
101 void dmam_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
102 dma_addr_t dma_handle
)
104 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
106 dma_free_coherent(dev
, size
, vaddr
, dma_handle
);
107 WARN_ON(devres_destroy(dev
, dmam_coherent_release
, dmam_match
,
110 EXPORT_SYMBOL(dmam_free_coherent
);
113 * dmam_alloc_non_coherent - Managed dma_alloc_noncoherent()
114 * @dev: Device to allocate non_coherent memory for
115 * @size: Size of allocation
116 * @dma_handle: Out argument for allocated DMA handle
117 * @gfp: Allocation flags
119 * Managed dma_alloc_noncoherent(). Memory allocated using this
120 * function will be automatically released on driver detach.
123 * Pointer to allocated memory on success, NULL on failure.
125 void *dmam_alloc_noncoherent(struct device
*dev
, size_t size
,
126 dma_addr_t
*dma_handle
, gfp_t gfp
)
128 struct dma_devres
*dr
;
131 dr
= devres_alloc(dmam_noncoherent_release
, sizeof(*dr
), gfp
);
135 vaddr
= dma_alloc_noncoherent(dev
, size
, dma_handle
, gfp
);
142 dr
->dma_handle
= *dma_handle
;
149 EXPORT_SYMBOL(dmam_alloc_noncoherent
);
152 * dmam_free_coherent - Managed dma_free_noncoherent()
153 * @dev: Device to free noncoherent memory for
154 * @size: Size of allocation
155 * @vaddr: Virtual address of the memory to free
156 * @dma_handle: DMA handle of the memory to free
158 * Managed dma_free_noncoherent().
160 void dmam_free_noncoherent(struct device
*dev
, size_t size
, void *vaddr
,
161 dma_addr_t dma_handle
)
163 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
165 dma_free_noncoherent(dev
, size
, vaddr
, dma_handle
);
166 WARN_ON(!devres_destroy(dev
, dmam_noncoherent_release
, dmam_match
,
169 EXPORT_SYMBOL(dmam_free_noncoherent
);
171 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
173 static void dmam_coherent_decl_release(struct device
*dev
, void *res
)
175 dma_release_declared_memory(dev
);
179 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
180 * @dev: Device to declare coherent memory for
181 * @phys_addr: Physical address of coherent memory to be declared
182 * @device_addr: Device address of coherent memory to be declared
183 * @size: Size of coherent memory to be declared
186 * Managed dma_declare_coherent_memory().
189 * 0 on success, -errno on failure.
191 int dmam_declare_coherent_memory(struct device
*dev
, phys_addr_t phys_addr
,
192 dma_addr_t device_addr
, size_t size
, int flags
)
197 res
= devres_alloc(dmam_coherent_decl_release
, 0, GFP_KERNEL
);
201 rc
= dma_declare_coherent_memory(dev
, phys_addr
, device_addr
, size
,
204 devres_add(dev
, res
);
213 EXPORT_SYMBOL(dmam_declare_coherent_memory
);
216 * dmam_release_declared_memory - Managed dma_release_declared_memory().
217 * @dev: Device to release declared coherent memory for
219 * Managed dmam_release_declared_memory().
221 void dmam_release_declared_memory(struct device
*dev
)
223 WARN_ON(devres_destroy(dev
, dmam_coherent_decl_release
, NULL
, NULL
));
225 EXPORT_SYMBOL(dmam_release_declared_memory
);
230 * Create scatter-list for the already allocated DMA buffer.
232 int dma_common_get_sgtable(struct device
*dev
, struct sg_table
*sgt
,
233 void *cpu_addr
, dma_addr_t handle
, size_t size
)
235 struct page
*page
= virt_to_page(cpu_addr
);
238 ret
= sg_alloc_table(sgt
, 1, GFP_KERNEL
);
242 sg_set_page(sgt
->sgl
, page
, PAGE_ALIGN(size
), 0);
245 EXPORT_SYMBOL(dma_common_get_sgtable
);
248 * Create userspace mapping for the DMA-coherent memory.
250 int dma_common_mmap(struct device
*dev
, struct vm_area_struct
*vma
,
251 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
254 #if defined(CONFIG_MMU) && !defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP)
255 unsigned long user_count
= vma_pages(vma
);
256 unsigned long count
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
257 unsigned long pfn
= page_to_pfn(virt_to_page(cpu_addr
));
258 unsigned long off
= vma
->vm_pgoff
;
260 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
262 if (dma_mmap_from_coherent(dev
, vma
, cpu_addr
, size
, &ret
))
265 if (off
< count
&& user_count
<= (count
- off
)) {
266 ret
= remap_pfn_range(vma
, vma
->vm_start
,
268 user_count
<< PAGE_SHIFT
,
271 #endif /* CONFIG_MMU && !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
275 EXPORT_SYMBOL(dma_common_mmap
);
279 * remaps an array of PAGE_SIZE pages into another vm_area
280 * Cannot be used in non-sleeping contexts
282 void *dma_common_pages_remap(struct page
**pages
, size_t size
,
283 unsigned long vm_flags
, pgprot_t prot
,
286 struct vm_struct
*area
;
288 area
= get_vm_area_caller(size
, vm_flags
, caller
);
294 if (map_vm_area(area
, prot
, pages
)) {
303 * remaps an allocated contiguous region into another vm_area.
304 * Cannot be used in non-sleeping contexts
307 void *dma_common_contiguous_remap(struct page
*page
, size_t size
,
308 unsigned long vm_flags
,
309 pgprot_t prot
, const void *caller
)
315 pages
= kmalloc(sizeof(struct page
*) << get_order(size
), GFP_KERNEL
);
319 for (i
= 0; i
< (size
>> PAGE_SHIFT
); i
++)
320 pages
[i
] = nth_page(page
, i
);
322 ptr
= dma_common_pages_remap(pages
, size
, vm_flags
, prot
, caller
);
330 * unmaps a range previously mapped by dma_common_*_remap
332 void dma_common_free_remap(void *cpu_addr
, size_t size
, unsigned long vm_flags
)
334 struct vm_struct
*area
= find_vm_area(cpu_addr
);
336 if (!area
|| (area
->flags
& vm_flags
) != vm_flags
) {
337 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr
);
341 unmap_kernel_range((unsigned long)cpu_addr
, PAGE_ALIGN(size
));
347 * Common configuration to enable DMA API use for a device
349 #include <linux/pci.h>
351 int dma_configure(struct device
*dev
)
353 struct device
*bridge
= NULL
, *dma_dev
= dev
;
354 enum dev_dma_attr attr
;
357 if (dev_is_pci(dev
)) {
358 bridge
= pci_get_host_bridge_device(to_pci_dev(dev
));
360 if (IS_ENABLED(CONFIG_OF
) && dma_dev
->parent
&&
361 dma_dev
->parent
->of_node
)
362 dma_dev
= dma_dev
->parent
;
365 if (dma_dev
->of_node
) {
366 ret
= of_dma_configure(dev
, dma_dev
->of_node
);
367 } else if (has_acpi_companion(dma_dev
)) {
368 attr
= acpi_get_dma_attr(to_acpi_device_node(dma_dev
->fwnode
));
369 if (attr
!= DEV_DMA_NOT_SUPPORTED
)
370 ret
= acpi_dma_configure(dev
, attr
);
374 pci_put_host_bridge_device(bridge
);
379 void dma_deconfigure(struct device
*dev
)
381 of_dma_deconfigure(dev
);
382 acpi_dma_deconfigure(dev
);