1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
5 * Copyright (c) 2006 SUSE Linux Products GmbH
6 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
9 #include <linux/acpi.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/export.h>
12 #include <linux/gfp.h>
13 #include <linux/of_device.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
23 dma_addr_t dma_handle
;
27 static void dmam_release(struct device
*dev
, void *res
)
29 struct dma_devres
*this = res
;
31 dma_free_attrs(dev
, this->size
, this->vaddr
, this->dma_handle
,
35 static int dmam_match(struct device
*dev
, void *res
, void *match_data
)
37 struct dma_devres
*this = res
, *match
= match_data
;
39 if (this->vaddr
== match
->vaddr
) {
40 WARN_ON(this->size
!= match
->size
||
41 this->dma_handle
!= match
->dma_handle
);
48 * dmam_alloc_coherent - Managed dma_alloc_coherent()
49 * @dev: Device to allocate coherent memory for
50 * @size: Size of allocation
51 * @dma_handle: Out argument for allocated DMA handle
52 * @gfp: Allocation flags
54 * Managed dma_alloc_coherent(). Memory allocated using this function
55 * will be automatically released on driver detach.
58 * Pointer to allocated memory on success, NULL on failure.
60 void *dmam_alloc_coherent(struct device
*dev
, size_t size
,
61 dma_addr_t
*dma_handle
, gfp_t gfp
)
63 struct dma_devres
*dr
;
66 dr
= devres_alloc(dmam_release
, sizeof(*dr
), gfp
);
70 vaddr
= dma_alloc_coherent(dev
, size
, dma_handle
, gfp
);
77 dr
->dma_handle
= *dma_handle
;
84 EXPORT_SYMBOL(dmam_alloc_coherent
);
87 * dmam_free_coherent - Managed dma_free_coherent()
88 * @dev: Device to free coherent memory for
89 * @size: Size of allocation
90 * @vaddr: Virtual address of the memory to free
91 * @dma_handle: DMA handle of the memory to free
93 * Managed dma_free_coherent().
95 void dmam_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
96 dma_addr_t dma_handle
)
98 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
100 dma_free_coherent(dev
, size
, vaddr
, dma_handle
);
101 WARN_ON(devres_destroy(dev
, dmam_release
, dmam_match
, &match_data
));
103 EXPORT_SYMBOL(dmam_free_coherent
);
106 * dmam_alloc_attrs - Managed dma_alloc_attrs()
107 * @dev: Device to allocate non_coherent memory for
108 * @size: Size of allocation
109 * @dma_handle: Out argument for allocated DMA handle
110 * @gfp: Allocation flags
111 * @attrs: Flags in the DMA_ATTR_* namespace.
113 * Managed dma_alloc_attrs(). Memory allocated using this function will be
114 * automatically released on driver detach.
117 * Pointer to allocated memory on success, NULL on failure.
119 void *dmam_alloc_attrs(struct device
*dev
, size_t size
, dma_addr_t
*dma_handle
,
120 gfp_t gfp
, unsigned long attrs
)
122 struct dma_devres
*dr
;
125 dr
= devres_alloc(dmam_release
, sizeof(*dr
), gfp
);
129 vaddr
= dma_alloc_attrs(dev
, size
, dma_handle
, gfp
, attrs
);
136 dr
->dma_handle
= *dma_handle
;
144 EXPORT_SYMBOL(dmam_alloc_attrs
);
146 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
148 static void dmam_coherent_decl_release(struct device
*dev
, void *res
)
150 dma_release_declared_memory(dev
);
154 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
155 * @dev: Device to declare coherent memory for
156 * @phys_addr: Physical address of coherent memory to be declared
157 * @device_addr: Device address of coherent memory to be declared
158 * @size: Size of coherent memory to be declared
161 * Managed dma_declare_coherent_memory().
164 * 0 on success, -errno on failure.
166 int dmam_declare_coherent_memory(struct device
*dev
, phys_addr_t phys_addr
,
167 dma_addr_t device_addr
, size_t size
, int flags
)
172 res
= devres_alloc(dmam_coherent_decl_release
, 0, GFP_KERNEL
);
176 rc
= dma_declare_coherent_memory(dev
, phys_addr
, device_addr
, size
,
179 devres_add(dev
, res
);
185 EXPORT_SYMBOL(dmam_declare_coherent_memory
);
188 * dmam_release_declared_memory - Managed dma_release_declared_memory().
189 * @dev: Device to release declared coherent memory for
191 * Managed dmam_release_declared_memory().
193 void dmam_release_declared_memory(struct device
*dev
)
195 WARN_ON(devres_destroy(dev
, dmam_coherent_decl_release
, NULL
, NULL
));
197 EXPORT_SYMBOL(dmam_release_declared_memory
);
202 * Create scatter-list for the already allocated DMA buffer.
204 int dma_common_get_sgtable(struct device
*dev
, struct sg_table
*sgt
,
205 void *cpu_addr
, dma_addr_t handle
, size_t size
)
207 struct page
*page
= virt_to_page(cpu_addr
);
210 ret
= sg_alloc_table(sgt
, 1, GFP_KERNEL
);
214 sg_set_page(sgt
->sgl
, page
, PAGE_ALIGN(size
), 0);
217 EXPORT_SYMBOL(dma_common_get_sgtable
);
220 * Create userspace mapping for the DMA-coherent memory.
222 int dma_common_mmap(struct device
*dev
, struct vm_area_struct
*vma
,
223 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
226 #ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP
227 unsigned long user_count
= vma_pages(vma
);
228 unsigned long count
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
229 unsigned long pfn
= page_to_pfn(virt_to_page(cpu_addr
));
230 unsigned long off
= vma
->vm_pgoff
;
232 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
234 if (dma_mmap_from_dev_coherent(dev
, vma
, cpu_addr
, size
, &ret
))
237 if (off
< count
&& user_count
<= (count
- off
)) {
238 ret
= remap_pfn_range(vma
, vma
->vm_start
,
240 user_count
<< PAGE_SHIFT
,
243 #endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
247 EXPORT_SYMBOL(dma_common_mmap
);
250 static struct vm_struct
*__dma_common_pages_remap(struct page
**pages
,
251 size_t size
, unsigned long vm_flags
, pgprot_t prot
,
254 struct vm_struct
*area
;
256 area
= get_vm_area_caller(size
, vm_flags
, caller
);
260 if (map_vm_area(area
, prot
, pages
)) {
269 * remaps an array of PAGE_SIZE pages into another vm_area
270 * Cannot be used in non-sleeping contexts
272 void *dma_common_pages_remap(struct page
**pages
, size_t size
,
273 unsigned long vm_flags
, pgprot_t prot
,
276 struct vm_struct
*area
;
278 area
= __dma_common_pages_remap(pages
, size
, vm_flags
, prot
, caller
);
288 * remaps an allocated contiguous region into another vm_area.
289 * Cannot be used in non-sleeping contexts
292 void *dma_common_contiguous_remap(struct page
*page
, size_t size
,
293 unsigned long vm_flags
,
294 pgprot_t prot
, const void *caller
)
298 struct vm_struct
*area
;
300 pages
= kmalloc(sizeof(struct page
*) << get_order(size
), GFP_KERNEL
);
304 for (i
= 0; i
< (size
>> PAGE_SHIFT
); i
++)
305 pages
[i
] = nth_page(page
, i
);
307 area
= __dma_common_pages_remap(pages
, size
, vm_flags
, prot
, caller
);
317 * unmaps a range previously mapped by dma_common_*_remap
319 void dma_common_free_remap(void *cpu_addr
, size_t size
, unsigned long vm_flags
)
321 struct vm_struct
*area
= find_vm_area(cpu_addr
);
323 if (!area
|| (area
->flags
& vm_flags
) != vm_flags
) {
324 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr
);
328 unmap_kernel_range((unsigned long)cpu_addr
, PAGE_ALIGN(size
));
334 * Common configuration to enable DMA API use for a device
336 #include <linux/pci.h>
338 int dma_configure(struct device
*dev
)
340 struct device
*bridge
= NULL
, *dma_dev
= dev
;
341 enum dev_dma_attr attr
;
344 if (dev_is_pci(dev
)) {
345 bridge
= pci_get_host_bridge_device(to_pci_dev(dev
));
347 if (IS_ENABLED(CONFIG_OF
) && dma_dev
->parent
&&
348 dma_dev
->parent
->of_node
)
349 dma_dev
= dma_dev
->parent
;
352 if (dma_dev
->of_node
) {
353 ret
= of_dma_configure(dev
, dma_dev
->of_node
);
354 } else if (has_acpi_companion(dma_dev
)) {
355 attr
= acpi_get_dma_attr(to_acpi_device_node(dma_dev
->fwnode
));
356 if (attr
!= DEV_DMA_NOT_SUPPORTED
)
357 ret
= acpi_dma_configure(dev
, attr
);
361 pci_put_host_bridge_device(bridge
);
366 void dma_deconfigure(struct device
*dev
)
368 of_dma_deconfigure(dev
);
369 acpi_dma_deconfigure(dev
);