blob: 3b118353ea176af270d72a95ca1f79772a3f5602 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Tejun Heo9ac78492007-01-20 16:00:26 +09002/*
3 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
4 *
5 * Copyright (c) 2006 SUSE Linux Products GmbH
6 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
Tejun Heo9ac78492007-01-20 16:00:26 +09007 */
8
Sricharan R09515ef2017-04-10 16:51:01 +05309#include <linux/acpi.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090010#include <linux/dma-mapping.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040011#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Sricharan R09515ef2017-04-10 16:51:01 +053013#include <linux/of_device.h>
Laura Abbott513510d2014-10-09 15:26:40 -070014#include <linux/slab.h>
15#include <linux/vmalloc.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090016
17/*
18 * Managed DMA API
19 */
20struct dma_devres {
21 size_t size;
22 void *vaddr;
23 dma_addr_t dma_handle;
Christoph Hellwig63d36c92017-06-12 19:15:04 +020024 unsigned long attrs;
Tejun Heo9ac78492007-01-20 16:00:26 +090025};
26
Christoph Hellwig63d36c92017-06-12 19:15:04 +020027static void dmam_release(struct device *dev, void *res)
Tejun Heo9ac78492007-01-20 16:00:26 +090028{
29 struct dma_devres *this = res;
30
Christoph Hellwig63d36c92017-06-12 19:15:04 +020031 dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
32 this->attrs);
Tejun Heo9ac78492007-01-20 16:00:26 +090033}
34
35static int dmam_match(struct device *dev, void *res, void *match_data)
36{
37 struct dma_devres *this = res, *match = match_data;
38
39 if (this->vaddr == match->vaddr) {
40 WARN_ON(this->size != match->size ||
41 this->dma_handle != match->dma_handle);
42 return 1;
43 }
44 return 0;
45}
46
47/**
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
53 *
54 * Managed dma_alloc_coherent(). Memory allocated using this function
55 * will be automatically released on driver detach.
56 *
57 * RETURNS:
58 * Pointer to allocated memory on success, NULL on failure.
59 */
Marius Cristian Eseanu6d42d792015-03-08 12:34:14 +020060void *dmam_alloc_coherent(struct device *dev, size_t size,
Tejun Heo9ac78492007-01-20 16:00:26 +090061 dma_addr_t *dma_handle, gfp_t gfp)
62{
63 struct dma_devres *dr;
64 void *vaddr;
65
Christoph Hellwig63d36c92017-06-12 19:15:04 +020066 dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
Tejun Heo9ac78492007-01-20 16:00:26 +090067 if (!dr)
68 return NULL;
69
70 vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
71 if (!vaddr) {
72 devres_free(dr);
73 return NULL;
74 }
75
76 dr->vaddr = vaddr;
77 dr->dma_handle = *dma_handle;
78 dr->size = size;
79
80 devres_add(dev, dr);
81
82 return vaddr;
83}
84EXPORT_SYMBOL(dmam_alloc_coherent);
85
86/**
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
92 *
93 * Managed dma_free_coherent().
94 */
95void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
96 dma_addr_t dma_handle)
97{
98 struct dma_devres match_data = { size, vaddr, dma_handle };
99
100 dma_free_coherent(dev, size, vaddr, dma_handle);
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200101 WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
Tejun Heo9ac78492007-01-20 16:00:26 +0900102}
103EXPORT_SYMBOL(dmam_free_coherent);
104
105/**
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200106 * dmam_alloc_attrs - Managed dma_alloc_attrs()
Tejun Heo9ac78492007-01-20 16:00:26 +0900107 * @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
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200111 * @attrs: Flags in the DMA_ATTR_* namespace.
Tejun Heo9ac78492007-01-20 16:00:26 +0900112 *
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200113 * Managed dma_alloc_attrs(). Memory allocated using this function will be
114 * automatically released on driver detach.
Tejun Heo9ac78492007-01-20 16:00:26 +0900115 *
116 * RETURNS:
117 * Pointer to allocated memory on success, NULL on failure.
118 */
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200119void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
120 gfp_t gfp, unsigned long attrs)
Tejun Heo9ac78492007-01-20 16:00:26 +0900121{
122 struct dma_devres *dr;
123 void *vaddr;
124
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200125 dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
Tejun Heo9ac78492007-01-20 16:00:26 +0900126 if (!dr)
127 return NULL;
128
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200129 vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
Tejun Heo9ac78492007-01-20 16:00:26 +0900130 if (!vaddr) {
131 devres_free(dr);
132 return NULL;
133 }
134
135 dr->vaddr = vaddr;
136 dr->dma_handle = *dma_handle;
137 dr->size = size;
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200138 dr->attrs = attrs;
Tejun Heo9ac78492007-01-20 16:00:26 +0900139
140 devres_add(dev, dr);
141
142 return vaddr;
143}
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200144EXPORT_SYMBOL(dmam_alloc_attrs);
Tejun Heo9ac78492007-01-20 16:00:26 +0900145
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800146#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
Tejun Heo9ac78492007-01-20 16:00:26 +0900147
148static void dmam_coherent_decl_release(struct device *dev, void *res)
149{
150 dma_release_declared_memory(dev);
151}
152
153/**
154 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
155 * @dev: Device to declare coherent memory for
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600156 * @phys_addr: Physical address of coherent memory to be declared
Tejun Heo9ac78492007-01-20 16:00:26 +0900157 * @device_addr: Device address of coherent memory to be declared
158 * @size: Size of coherent memory to be declared
159 * @flags: Flags
160 *
161 * Managed dma_declare_coherent_memory().
162 *
163 * RETURNS:
164 * 0 on success, -errno on failure.
165 */
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600166int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
Tejun Heo9ac78492007-01-20 16:00:26 +0900167 dma_addr_t device_addr, size_t size, int flags)
168{
169 void *res;
170 int rc;
171
172 res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
173 if (!res)
174 return -ENOMEM;
175
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600176 rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
Tejun Heo9ac78492007-01-20 16:00:26 +0900177 flags);
Christoph Hellwig2436bdc2017-08-25 17:13:09 +0200178 if (!rc)
Tejun Heo9ac78492007-01-20 16:00:26 +0900179 devres_add(dev, res);
Christoph Hellwig2436bdc2017-08-25 17:13:09 +0200180 else
Tejun Heo9ac78492007-01-20 16:00:26 +0900181 devres_free(res);
182
183 return rc;
184}
185EXPORT_SYMBOL(dmam_declare_coherent_memory);
186
187/**
188 * dmam_release_declared_memory - Managed dma_release_declared_memory().
189 * @dev: Device to release declared coherent memory for
190 *
191 * Managed dmam_release_declared_memory().
192 */
193void dmam_release_declared_memory(struct device *dev)
194{
195 WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
196}
197EXPORT_SYMBOL(dmam_release_declared_memory);
198
Marek Szyprowskic6c22952012-11-26 10:41:48 -0300199#endif
200
Marek Szyprowskid2b74282012-06-13 10:05:52 +0200201/*
202 * Create scatter-list for the already allocated DMA buffer.
203 */
204int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
205 void *cpu_addr, dma_addr_t handle, size_t size)
206{
207 struct page *page = virt_to_page(cpu_addr);
208 int ret;
209
210 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
211 if (unlikely(ret))
212 return ret;
213
214 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
215 return 0;
216}
217EXPORT_SYMBOL(dma_common_get_sgtable);
218
Marek Szyprowski64ccc9c2012-06-14 13:03:04 +0200219/*
220 * Create userspace mapping for the DMA-coherent memory.
221 */
222int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
223 void *cpu_addr, dma_addr_t dma_addr, size_t size)
224{
225 int ret = -ENXIO;
Vladimir Murzin07c75d72017-06-28 10:16:57 +0100226#ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP
Muhammad Falak R Wani95da00e2016-05-21 18:52:22 +0530227 unsigned long user_count = vma_pages(vma);
Marek Szyprowski64ccc9c2012-06-14 13:03:04 +0200228 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;
231
232 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
233
Vladimir Murzin43fc5092017-07-20 11:19:58 +0100234 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Marek Szyprowski64ccc9c2012-06-14 13:03:04 +0200235 return ret;
236
237 if (off < count && user_count <= (count - off)) {
238 ret = remap_pfn_range(vma, vma->vm_start,
239 pfn + off,
240 user_count << PAGE_SHIFT,
241 vma->vm_page_prot);
242 }
Vladimir Murzin07c75d72017-06-28 10:16:57 +0100243#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
Marek Szyprowski64ccc9c2012-06-14 13:03:04 +0200244
245 return ret;
246}
247EXPORT_SYMBOL(dma_common_mmap);
Laura Abbott513510d2014-10-09 15:26:40 -0700248
249#ifdef CONFIG_MMU
Catalin Marinas1a3389f2017-05-25 17:26:47 +0100250static struct vm_struct *__dma_common_pages_remap(struct page **pages,
251 size_t size, unsigned long vm_flags, pgprot_t prot,
252 const void *caller)
253{
254 struct vm_struct *area;
255
256 area = get_vm_area_caller(size, vm_flags, caller);
257 if (!area)
258 return NULL;
259
260 if (map_vm_area(area, prot, pages)) {
261 vunmap(area->addr);
262 return NULL;
263 }
264
265 return area;
266}
267
Laura Abbott513510d2014-10-09 15:26:40 -0700268/*
269 * remaps an array of PAGE_SIZE pages into another vm_area
270 * Cannot be used in non-sleeping contexts
271 */
272void *dma_common_pages_remap(struct page **pages, size_t size,
273 unsigned long vm_flags, pgprot_t prot,
274 const void *caller)
275{
276 struct vm_struct *area;
277
Catalin Marinas1a3389f2017-05-25 17:26:47 +0100278 area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
Laura Abbott513510d2014-10-09 15:26:40 -0700279 if (!area)
280 return NULL;
281
282 area->pages = pages;
283
Laura Abbott513510d2014-10-09 15:26:40 -0700284 return area->addr;
285}
286
287/*
288 * remaps an allocated contiguous region into another vm_area.
289 * Cannot be used in non-sleeping contexts
290 */
291
292void *dma_common_contiguous_remap(struct page *page, size_t size,
293 unsigned long vm_flags,
294 pgprot_t prot, const void *caller)
295{
296 int i;
297 struct page **pages;
Catalin Marinas1a3389f2017-05-25 17:26:47 +0100298 struct vm_struct *area;
Laura Abbott513510d2014-10-09 15:26:40 -0700299
300 pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
301 if (!pages)
302 return NULL;
303
Geliang Tang0dd89112017-03-24 22:10:49 +0800304 for (i = 0; i < (size >> PAGE_SHIFT); i++)
305 pages[i] = nth_page(page, i);
Laura Abbott513510d2014-10-09 15:26:40 -0700306
Catalin Marinas1a3389f2017-05-25 17:26:47 +0100307 area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
Laura Abbott513510d2014-10-09 15:26:40 -0700308
309 kfree(pages);
310
Catalin Marinas1a3389f2017-05-25 17:26:47 +0100311 if (!area)
312 return NULL;
313 return area->addr;
Laura Abbott513510d2014-10-09 15:26:40 -0700314}
315
316/*
317 * unmaps a range previously mapped by dma_common_*_remap
318 */
319void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
320{
321 struct vm_struct *area = find_vm_area(cpu_addr);
322
323 if (!area || (area->flags & vm_flags) != vm_flags) {
324 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
325 return;
326 }
327
Peng Fan85714102016-07-21 16:04:21 +0800328 unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
Laura Abbott513510d2014-10-09 15:26:40 -0700329 vunmap(cpu_addr);
330}
331#endif
Sricharan R09515ef2017-04-10 16:51:01 +0530332
333/*
334 * Common configuration to enable DMA API use for a device
335 */
336#include <linux/pci.h>
337
338int dma_configure(struct device *dev)
339{
340 struct device *bridge = NULL, *dma_dev = dev;
341 enum dev_dma_attr attr;
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530342 int ret = 0;
Sricharan R09515ef2017-04-10 16:51:01 +0530343
344 if (dev_is_pci(dev)) {
345 bridge = pci_get_host_bridge_device(to_pci_dev(dev));
346 dma_dev = bridge;
347 if (IS_ENABLED(CONFIG_OF) && dma_dev->parent &&
348 dma_dev->parent->of_node)
349 dma_dev = dma_dev->parent;
350 }
351
352 if (dma_dev->of_node) {
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530353 ret = of_dma_configure(dev, dma_dev->of_node);
Sricharan R09515ef2017-04-10 16:51:01 +0530354 } 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)
Sricharan R5a1bb632017-04-10 16:51:03 +0530357 ret = acpi_dma_configure(dev, attr);
Sricharan R09515ef2017-04-10 16:51:01 +0530358 }
359
360 if (bridge)
361 pci_put_host_bridge_device(bridge);
362
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530363 return ret;
Sricharan R09515ef2017-04-10 16:51:01 +0530364}
365
366void dma_deconfigure(struct device *dev)
367{
368 of_dma_deconfigure(dev);
369 acpi_dma_deconfigure(dev);
370}