]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/base/dma-mapping.c
netfilter: conntrack: rename nf_ct_iterate_cleanup
[mirror_ubuntu-artful-kernel.git] / drivers / base / dma-mapping.c
CommitLineData
9ac7849e
TH
1/*
2 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
3 *
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
6 *
7 * This file is released under the GPLv2.
8 */
9
09515ef5 10#include <linux/acpi.h>
9ac7849e 11#include <linux/dma-mapping.h>
1b6bc32f 12#include <linux/export.h>
5a0e3ad6 13#include <linux/gfp.h>
09515ef5 14#include <linux/of_device.h>
513510dd
LA
15#include <linux/slab.h>
16#include <linux/vmalloc.h>
9ac7849e
TH
17
18/*
19 * Managed DMA API
20 */
21struct dma_devres {
22 size_t size;
23 void *vaddr;
24 dma_addr_t dma_handle;
25};
26
27static void dmam_coherent_release(struct device *dev, void *res)
28{
29 struct dma_devres *this = res;
30
31 dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
32}
33
34static void dmam_noncoherent_release(struct device *dev, void *res)
35{
36 struct dma_devres *this = res;
37
38 dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
39}
40
41static int dmam_match(struct device *dev, void *res, void *match_data)
42{
43 struct dma_devres *this = res, *match = match_data;
44
45 if (this->vaddr == match->vaddr) {
46 WARN_ON(this->size != match->size ||
47 this->dma_handle != match->dma_handle);
48 return 1;
49 }
50 return 0;
51}
52
53/**
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
59 *
60 * Managed dma_alloc_coherent(). Memory allocated using this function
61 * will be automatically released on driver detach.
62 *
63 * RETURNS:
64 * Pointer to allocated memory on success, NULL on failure.
65 */
6d42d79e 66void *dmam_alloc_coherent(struct device *dev, size_t size,
9ac7849e
TH
67 dma_addr_t *dma_handle, gfp_t gfp)
68{
69 struct dma_devres *dr;
70 void *vaddr;
71
72 dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
73 if (!dr)
74 return NULL;
75
76 vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
77 if (!vaddr) {
78 devres_free(dr);
79 return NULL;
80 }
81
82 dr->vaddr = vaddr;
83 dr->dma_handle = *dma_handle;
84 dr->size = size;
85
86 devres_add(dev, dr);
87
88 return vaddr;
89}
90EXPORT_SYMBOL(dmam_alloc_coherent);
91
92/**
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
98 *
99 * Managed dma_free_coherent().
100 */
101void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
102 dma_addr_t dma_handle)
103{
104 struct dma_devres match_data = { size, vaddr, dma_handle };
105
106 dma_free_coherent(dev, size, vaddr, dma_handle);
107 WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
108 &match_data));
109}
110EXPORT_SYMBOL(dmam_free_coherent);
111
112/**
cd74da95 113 * dmam_alloc_non_coherent - Managed dma_alloc_noncoherent()
9ac7849e
TH
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
118 *
cd74da95 119 * Managed dma_alloc_noncoherent(). Memory allocated using this
9ac7849e
TH
120 * function will be automatically released on driver detach.
121 *
122 * RETURNS:
123 * Pointer to allocated memory on success, NULL on failure.
124 */
125void *dmam_alloc_noncoherent(struct device *dev, size_t size,
126 dma_addr_t *dma_handle, gfp_t gfp)
127{
128 struct dma_devres *dr;
129 void *vaddr;
130
131 dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
132 if (!dr)
133 return NULL;
134
135 vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
136 if (!vaddr) {
137 devres_free(dr);
138 return NULL;
139 }
140
141 dr->vaddr = vaddr;
142 dr->dma_handle = *dma_handle;
143 dr->size = size;
144
145 devres_add(dev, dr);
146
147 return vaddr;
148}
149EXPORT_SYMBOL(dmam_alloc_noncoherent);
150
151/**
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
157 *
158 * Managed dma_free_noncoherent().
159 */
160void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
161 dma_addr_t dma_handle)
162{
163 struct dma_devres match_data = { size, vaddr, dma_handle };
164
165 dma_free_noncoherent(dev, size, vaddr, dma_handle);
166 WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
167 &match_data));
168}
169EXPORT_SYMBOL(dmam_free_noncoherent);
170
20d666e4 171#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
9ac7849e
TH
172
173static void dmam_coherent_decl_release(struct device *dev, void *res)
174{
175 dma_release_declared_memory(dev);
176}
177
178/**
179 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
180 * @dev: Device to declare coherent memory for
88a984ba 181 * @phys_addr: Physical address of coherent memory to be declared
9ac7849e
TH
182 * @device_addr: Device address of coherent memory to be declared
183 * @size: Size of coherent memory to be declared
184 * @flags: Flags
185 *
186 * Managed dma_declare_coherent_memory().
187 *
188 * RETURNS:
189 * 0 on success, -errno on failure.
190 */
88a984ba 191int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
9ac7849e
TH
192 dma_addr_t device_addr, size_t size, int flags)
193{
194 void *res;
195 int rc;
196
197 res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
198 if (!res)
199 return -ENOMEM;
200
88a984ba 201 rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
9ac7849e 202 flags);
775115c0 203 if (rc) {
9ac7849e 204 devres_add(dev, res);
775115c0
VY
205 rc = 0;
206 } else {
9ac7849e 207 devres_free(res);
775115c0
VY
208 rc = -ENOMEM;
209 }
9ac7849e
TH
210
211 return rc;
212}
213EXPORT_SYMBOL(dmam_declare_coherent_memory);
214
215/**
216 * dmam_release_declared_memory - Managed dma_release_declared_memory().
217 * @dev: Device to release declared coherent memory for
218 *
219 * Managed dmam_release_declared_memory().
220 */
221void dmam_release_declared_memory(struct device *dev)
222{
223 WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
224}
225EXPORT_SYMBOL(dmam_release_declared_memory);
226
c6c22955
MS
227#endif
228
d2b7428e
MS
229/*
230 * Create scatter-list for the already allocated DMA buffer.
231 */
232int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
233 void *cpu_addr, dma_addr_t handle, size_t size)
234{
235 struct page *page = virt_to_page(cpu_addr);
236 int ret;
237
238 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
239 if (unlikely(ret))
240 return ret;
241
242 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
243 return 0;
244}
245EXPORT_SYMBOL(dma_common_get_sgtable);
246
64ccc9c0
MS
247/*
248 * Create userspace mapping for the DMA-coherent memory.
249 */
250int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
251 void *cpu_addr, dma_addr_t dma_addr, size_t size)
252{
253 int ret = -ENXIO;
0d4a619b 254#if defined(CONFIG_MMU) && !defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP)
95da00e3 255 unsigned long user_count = vma_pages(vma);
64ccc9c0
MS
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;
259
260 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
261
262 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
263 return ret;
264
265 if (off < count && user_count <= (count - off)) {
266 ret = remap_pfn_range(vma, vma->vm_start,
267 pfn + off,
268 user_count << PAGE_SHIFT,
269 vma->vm_page_prot);
270 }
0d4a619b 271#endif /* CONFIG_MMU && !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
64ccc9c0
MS
272
273 return ret;
274}
275EXPORT_SYMBOL(dma_common_mmap);
513510dd
LA
276
277#ifdef CONFIG_MMU
278/*
279 * remaps an array of PAGE_SIZE pages into another vm_area
280 * Cannot be used in non-sleeping contexts
281 */
282void *dma_common_pages_remap(struct page **pages, size_t size,
283 unsigned long vm_flags, pgprot_t prot,
284 const void *caller)
285{
286 struct vm_struct *area;
287
288 area = get_vm_area_caller(size, vm_flags, caller);
289 if (!area)
290 return NULL;
291
292 area->pages = pages;
293
294 if (map_vm_area(area, prot, pages)) {
295 vunmap(area->addr);
296 return NULL;
297 }
298
299 return area->addr;
300}
301
302/*
303 * remaps an allocated contiguous region into another vm_area.
304 * Cannot be used in non-sleeping contexts
305 */
306
307void *dma_common_contiguous_remap(struct page *page, size_t size,
308 unsigned long vm_flags,
309 pgprot_t prot, const void *caller)
310{
311 int i;
312 struct page **pages;
313 void *ptr;
513510dd
LA
314
315 pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
316 if (!pages)
317 return NULL;
318
0dd89119
GT
319 for (i = 0; i < (size >> PAGE_SHIFT); i++)
320 pages[i] = nth_page(page, i);
513510dd
LA
321
322 ptr = dma_common_pages_remap(pages, size, vm_flags, prot, caller);
323
324 kfree(pages);
325
326 return ptr;
327}
328
329/*
330 * unmaps a range previously mapped by dma_common_*_remap
331 */
332void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
333{
334 struct vm_struct *area = find_vm_area(cpu_addr);
335
336 if (!area || (area->flags & vm_flags) != vm_flags) {
337 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
338 return;
339 }
340
85714108 341 unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
513510dd
LA
342 vunmap(cpu_addr);
343}
344#endif
09515ef5
S
345
346/*
347 * Common configuration to enable DMA API use for a device
348 */
349#include <linux/pci.h>
350
351int dma_configure(struct device *dev)
352{
353 struct device *bridge = NULL, *dma_dev = dev;
354 enum dev_dma_attr attr;
7b07cbef 355 int ret = 0;
09515ef5
S
356
357 if (dev_is_pci(dev)) {
358 bridge = pci_get_host_bridge_device(to_pci_dev(dev));
359 dma_dev = bridge;
360 if (IS_ENABLED(CONFIG_OF) && dma_dev->parent &&
361 dma_dev->parent->of_node)
362 dma_dev = dma_dev->parent;
363 }
364
365 if (dma_dev->of_node) {
7b07cbef 366 ret = of_dma_configure(dev, dma_dev->of_node);
09515ef5
S
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)
5a1bb638 370 ret = acpi_dma_configure(dev, attr);
09515ef5
S
371 }
372
373 if (bridge)
374 pci_put_host_bridge_device(bridge);
375
7b07cbef 376 return ret;
09515ef5
S
377}
378
379void dma_deconfigure(struct device *dev)
380{
381 of_dma_deconfigure(dev);
382 acpi_dma_deconfigure(dev);
383}