2 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
4 * Copyright (c) 2006 SUSE Linux Products GmbH
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;
28 static void dmam_release(struct device *dev, void *res)
30 struct dma_devres *this = res;
32 dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
36 static int dmam_match(struct device *dev, void *res, void *match_data)
38 struct dma_devres *this = res, *match = match_data;
40 if (this->vaddr == match->vaddr) {
41 WARN_ON(this->size != match->size ||
42 this->dma_handle != match->dma_handle);
49 * dmam_alloc_coherent - Managed dma_alloc_coherent()
50 * @dev: Device to allocate coherent memory for
51 * @size: Size of allocation
52 * @dma_handle: Out argument for allocated DMA handle
53 * @gfp: Allocation flags
55 * Managed dma_alloc_coherent(). Memory allocated using this function
56 * will be automatically released on driver detach.
59 * Pointer to allocated memory on success, NULL on failure.
61 void *dmam_alloc_coherent(struct device *dev, size_t size,
62 dma_addr_t *dma_handle, gfp_t gfp)
64 struct dma_devres *dr;
67 dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
71 vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
78 dr->dma_handle = *dma_handle;
85 EXPORT_SYMBOL(dmam_alloc_coherent);
88 * dmam_free_coherent - Managed dma_free_coherent()
89 * @dev: Device to free coherent memory for
90 * @size: Size of allocation
91 * @vaddr: Virtual address of the memory to free
92 * @dma_handle: DMA handle of the memory to free
94 * Managed dma_free_coherent().
96 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
97 dma_addr_t dma_handle)
99 struct dma_devres match_data = { size, vaddr, dma_handle };
101 dma_free_coherent(dev, size, vaddr, dma_handle);
102 WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
104 EXPORT_SYMBOL(dmam_free_coherent);
107 * dmam_alloc_attrs - Managed dma_alloc_attrs()
108 * @dev: Device to allocate non_coherent memory for
109 * @size: Size of allocation
110 * @dma_handle: Out argument for allocated DMA handle
111 * @gfp: Allocation flags
112 * @attrs: Flags in the DMA_ATTR_* namespace.
114 * Managed dma_alloc_attrs(). Memory allocated using this function will be
115 * automatically released on driver detach.
118 * Pointer to allocated memory on success, NULL on failure.
120 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
121 gfp_t gfp, unsigned long attrs)
123 struct dma_devres *dr;
126 dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
130 vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
137 dr->dma_handle = *dma_handle;
145 EXPORT_SYMBOL(dmam_alloc_attrs);
147 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
149 static void dmam_coherent_decl_release(struct device *dev, void *res)
151 dma_release_declared_memory(dev);
155 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
156 * @dev: Device to declare coherent memory for
157 * @phys_addr: Physical address of coherent memory to be declared
158 * @device_addr: Device address of coherent memory to be declared
159 * @size: Size of coherent memory to be declared
162 * Managed dma_declare_coherent_memory().
165 * 0 on success, -errno on failure.
167 int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
168 dma_addr_t device_addr, size_t size, int flags)
173 res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
177 rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
180 devres_add(dev, res);
186 EXPORT_SYMBOL(dmam_declare_coherent_memory);
189 * dmam_release_declared_memory - Managed dma_release_declared_memory().
190 * @dev: Device to release declared coherent memory for
192 * Managed dmam_release_declared_memory().
194 void dmam_release_declared_memory(struct device *dev)
196 WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
198 EXPORT_SYMBOL(dmam_release_declared_memory);
203 * Create scatter-list for the already allocated DMA buffer.
205 int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
206 void *cpu_addr, dma_addr_t handle, size_t size)
208 struct page *page = virt_to_page(cpu_addr);
211 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
215 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
218 EXPORT_SYMBOL(dma_common_get_sgtable);
221 * Create userspace mapping for the DMA-coherent memory.
223 int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
224 void *cpu_addr, dma_addr_t dma_addr, size_t size)
227 #ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP
228 unsigned long user_count = vma_pages(vma);
229 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
230 unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
231 unsigned long off = vma->vm_pgoff;
233 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
235 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
238 if (off < count && user_count <= (count - off)) {
239 ret = remap_pfn_range(vma, vma->vm_start,
241 user_count << PAGE_SHIFT,
244 #endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
248 EXPORT_SYMBOL(dma_common_mmap);
251 static struct vm_struct *__dma_common_pages_remap(struct page **pages,
252 size_t size, unsigned long vm_flags, pgprot_t prot,
255 struct vm_struct *area;
257 area = get_vm_area_caller(size, vm_flags, caller);
261 if (map_vm_area(area, prot, pages)) {
270 * remaps an array of PAGE_SIZE pages into another vm_area
271 * Cannot be used in non-sleeping contexts
273 void *dma_common_pages_remap(struct page **pages, size_t size,
274 unsigned long vm_flags, pgprot_t prot,
277 struct vm_struct *area;
279 area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
289 * remaps an allocated contiguous region into another vm_area.
290 * Cannot be used in non-sleeping contexts
293 void *dma_common_contiguous_remap(struct page *page, size_t size,
294 unsigned long vm_flags,
295 pgprot_t prot, const void *caller)
299 struct vm_struct *area;
301 pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
305 for (i = 0; i < (size >> PAGE_SHIFT); i++)
306 pages[i] = nth_page(page, i);
308 area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
318 * unmaps a range previously mapped by dma_common_*_remap
320 void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
322 struct vm_struct *area = find_vm_area(cpu_addr);
324 if (!area || (area->flags & vm_flags) != vm_flags) {
325 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
329 unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
335 * Common configuration to enable DMA API use for a device
337 #include <linux/pci.h>
339 int dma_configure(struct device *dev)
341 struct device *bridge = NULL, *dma_dev = dev;
342 enum dev_dma_attr attr;
345 if (dev_is_pci(dev)) {
346 bridge = pci_get_host_bridge_device(to_pci_dev(dev));
348 if (IS_ENABLED(CONFIG_OF) && dma_dev->parent &&
349 dma_dev->parent->of_node)
350 dma_dev = dma_dev->parent;
353 if (dma_dev->of_node) {
354 ret = of_dma_configure(dev, dma_dev->of_node);
355 } else if (has_acpi_companion(dma_dev)) {
356 attr = acpi_get_dma_attr(to_acpi_device_node(dma_dev->fwnode));
357 if (attr != DEV_DMA_NOT_SUPPORTED)
358 ret = acpi_dma_configure(dev, attr);
362 pci_put_host_bridge_device(bridge);
367 void dma_deconfigure(struct device *dev)
369 of_dma_deconfigure(dev);
370 acpi_dma_deconfigure(dev);