1 /*
2 * linux/arch/arm/mm/dma-mapping.c
3 *
4 * Copyright (C) 2000-2004 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * DMA uncached mapping support.
11 */
12 #include <linux/bootmem.h>
13 #include <linux/module.h>
14 #include <linux/mm.h>
15 #include <linux/genalloc.h>
16 #include <linux/gfp.h>
17 #include <linux/errno.h>
18 #include <linux/list.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/dma-contiguous.h>
23 #include <linux/highmem.h>
24 #include <linux/memblock.h>
25 #include <linux/slab.h>
26 #include <linux/iommu.h>
27 #include <linux/io.h>
28 #include <linux/vmalloc.h>
29 #include <linux/sizes.h>
30 #include <linux/cma.h>
31
32 #include <asm/memory.h>
33 #include <asm/highmem.h>
34 #include <asm/cacheflush.h>
35 #include <asm/tlbflush.h>
36 #include <asm/mach/arch.h>
37 #include <asm/dma-iommu.h>
38 #include <asm/mach/map.h>
39 #include <asm/system_info.h>
40 #include <asm/dma-contiguous.h>
41
42 #include "mm.h"
43
44 /*
45 * The DMA API is built upon the notion of "buffer ownership". A buffer
46 * is either exclusively owned by the CPU (and therefore may be accessed
47 * by it) or exclusively owned by the DMA device. These helper functions
48 * represent the transitions between these two ownership states.
49 *
50 * Note, however, that on later ARMs, this notion does not work due to
51 * speculative prefetches. We model our approach on the assumption that
52 * the CPU does do speculative prefetches, which means we clean caches
53 * before transfers and delay cache invalidation until transfer completion.
54 *
55 */
56 static void __dma_page_cpu_to_dev(struct page *, unsigned long,
57 size_t, enum dma_data_direction);
58 static void __dma_page_dev_to_cpu(struct page *, unsigned long,
59 size_t, enum dma_data_direction);
60
61 /**
62 * arm_dma_map_page - map a portion of a page for streaming DMA
63 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
64 * @page: page that buffer resides in
65 * @offset: offset into page for start of buffer
66 * @size: size of buffer to map
67 * @dir: DMA transfer direction
68 *
69 * Ensure that any data held in the cache is appropriately discarded
70 * or written back.
71 *
72 * The device owns this memory once this call has completed. The CPU
73 * can regain ownership by calling dma_unmap_page().
74 */
arm_dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)75 static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
76 unsigned long offset, size_t size, enum dma_data_direction dir,
77 struct dma_attrs *attrs)
78 {
79 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
80 __dma_page_cpu_to_dev(page, offset, size, dir);
81 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
82 }
83
arm_coherent_dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)84 static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
85 unsigned long offset, size_t size, enum dma_data_direction dir,
86 struct dma_attrs *attrs)
87 {
88 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
89 }
90
91 /**
92 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
93 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
94 * @handle: DMA address of buffer
95 * @size: size of buffer (same as passed to dma_map_page)
96 * @dir: DMA transfer direction (same as passed to dma_map_page)
97 *
98 * Unmap a page streaming mode DMA translation. The handle and size
99 * must match what was provided in the previous dma_map_page() call.
100 * All other usages are undefined.
101 *
102 * After this call, reads by the CPU to the buffer are guaranteed to see
103 * whatever the device wrote there.
104 */
arm_dma_unmap_page(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)105 static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
106 size_t size, enum dma_data_direction dir,
107 struct dma_attrs *attrs)
108 {
109 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
110 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
111 handle & ~PAGE_MASK, size, dir);
112 }
113
arm_dma_sync_single_for_cpu(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir)114 static void arm_dma_sync_single_for_cpu(struct device *dev,
115 dma_addr_t handle, size_t size, enum dma_data_direction dir)
116 {
117 unsigned int offset = handle & (PAGE_SIZE - 1);
118 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
119 __dma_page_dev_to_cpu(page, offset, size, dir);
120 }
121
arm_dma_sync_single_for_device(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir)122 static void arm_dma_sync_single_for_device(struct device *dev,
123 dma_addr_t handle, size_t size, enum dma_data_direction dir)
124 {
125 unsigned int offset = handle & (PAGE_SIZE - 1);
126 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
127 __dma_page_cpu_to_dev(page, offset, size, dir);
128 }
129
130 struct dma_map_ops arm_dma_ops = {
131 .alloc = arm_dma_alloc,
132 .free = arm_dma_free,
133 .mmap = arm_dma_mmap,
134 .get_sgtable = arm_dma_get_sgtable,
135 .map_page = arm_dma_map_page,
136 .unmap_page = arm_dma_unmap_page,
137 .map_sg = arm_dma_map_sg,
138 .unmap_sg = arm_dma_unmap_sg,
139 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
140 .sync_single_for_device = arm_dma_sync_single_for_device,
141 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
142 .sync_sg_for_device = arm_dma_sync_sg_for_device,
143 .set_dma_mask = arm_dma_set_mask,
144 };
145 EXPORT_SYMBOL(arm_dma_ops);
146
147 static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
148 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
149 static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
150 dma_addr_t handle, struct dma_attrs *attrs);
151
152 struct dma_map_ops arm_coherent_dma_ops = {
153 .alloc = arm_coherent_dma_alloc,
154 .free = arm_coherent_dma_free,
155 .mmap = arm_dma_mmap,
156 .get_sgtable = arm_dma_get_sgtable,
157 .map_page = arm_coherent_dma_map_page,
158 .map_sg = arm_dma_map_sg,
159 .set_dma_mask = arm_dma_set_mask,
160 };
161 EXPORT_SYMBOL(arm_coherent_dma_ops);
162
__dma_supported(struct device * dev,u64 mask,bool warn)163 static int __dma_supported(struct device *dev, u64 mask, bool warn)
164 {
165 unsigned long max_dma_pfn;
166
167 /*
168 * If the mask allows for more memory than we can address,
169 * and we actually have that much memory, then we must
170 * indicate that DMA to this device is not supported.
171 */
172 if (sizeof(mask) != sizeof(dma_addr_t) &&
173 mask > (dma_addr_t)~0 &&
174 dma_to_pfn(dev, ~0) < max_pfn) {
175 if (warn) {
176 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
177 mask);
178 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
179 }
180 return 0;
181 }
182
183 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
184
185 /*
186 * Translate the device's DMA mask to a PFN limit. This
187 * PFN number includes the page which we can DMA to.
188 */
189 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
190 if (warn)
191 dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
192 mask,
193 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
194 max_dma_pfn + 1);
195 return 0;
196 }
197
198 return 1;
199 }
200
get_coherent_dma_mask(struct device * dev)201 static u64 get_coherent_dma_mask(struct device *dev)
202 {
203 u64 mask = (u64)DMA_BIT_MASK(32);
204
205 if (dev) {
206 mask = dev->coherent_dma_mask;
207
208 /*
209 * Sanity check the DMA mask - it must be non-zero, and
210 * must be able to be satisfied by a DMA allocation.
211 */
212 if (mask == 0) {
213 dev_warn(dev, "coherent DMA mask is unset\n");
214 return 0;
215 }
216
217 if (!__dma_supported(dev, mask, true))
218 return 0;
219 }
220
221 return mask;
222 }
223
__dma_clear_buffer(struct page * page,size_t size)224 static void __dma_clear_buffer(struct page *page, size_t size)
225 {
226 /*
227 * Ensure that the allocated pages are zeroed, and that any data
228 * lurking in the kernel direct-mapped region is invalidated.
229 */
230 if (PageHighMem(page)) {
231 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
232 phys_addr_t end = base + size;
233 while (size > 0) {
234 void *ptr = kmap_atomic(page);
235 memset(ptr, 0, PAGE_SIZE);
236 dmac_flush_range(ptr, ptr + PAGE_SIZE);
237 kunmap_atomic(ptr);
238 page++;
239 size -= PAGE_SIZE;
240 }
241 outer_flush_range(base, end);
242 } else {
243 void *ptr = page_address(page);
244 memset(ptr, 0, size);
245 dmac_flush_range(ptr, ptr + size);
246 outer_flush_range(__pa(ptr), __pa(ptr) + size);
247 }
248 }
249
250 /*
251 * Allocate a DMA buffer for 'dev' of size 'size' using the
252 * specified gfp mask. Note that 'size' must be page aligned.
253 */
__dma_alloc_buffer(struct device * dev,size_t size,gfp_t gfp)254 static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
255 {
256 unsigned long order = get_order(size);
257 struct page *page, *p, *e;
258
259 page = alloc_pages(gfp, order);
260 if (!page)
261 return NULL;
262
263 /*
264 * Now split the huge page and free the excess pages
265 */
266 split_page(page, order);
267 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
268 __free_page(p);
269
270 __dma_clear_buffer(page, size);
271
272 return page;
273 }
274
275 /*
276 * Free a DMA buffer. 'size' must be page aligned.
277 */
__dma_free_buffer(struct page * page,size_t size)278 static void __dma_free_buffer(struct page *page, size_t size)
279 {
280 struct page *e = page + (size >> PAGE_SHIFT);
281
282 while (page < e) {
283 __free_page(page);
284 page++;
285 }
286 }
287
288 #ifdef CONFIG_MMU
289
290 static void *__alloc_from_contiguous(struct device *dev, size_t size,
291 pgprot_t prot, struct page **ret_page,
292 const void *caller);
293
294 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
295 pgprot_t prot, struct page **ret_page,
296 const void *caller);
297
298 static void *
__dma_alloc_remap(struct page * page,size_t size,gfp_t gfp,pgprot_t prot,const void * caller)299 __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
300 const void *caller)
301 {
302 /*
303 * DMA allocation can be mapped to user space, so lets
304 * set VM_USERMAP flags too.
305 */
306 return dma_common_contiguous_remap(page, size,
307 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
308 prot, caller);
309 }
310
__dma_free_remap(void * cpu_addr,size_t size)311 static void __dma_free_remap(void *cpu_addr, size_t size)
312 {
313 dma_common_free_remap(cpu_addr, size,
314 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
315 }
316
317 #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
318 static struct gen_pool *atomic_pool;
319
320 static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
321
early_coherent_pool(char * p)322 static int __init early_coherent_pool(char *p)
323 {
324 atomic_pool_size = memparse(p, &p);
325 return 0;
326 }
327 early_param("coherent_pool", early_coherent_pool);
328
init_dma_coherent_pool_size(unsigned long size)329 void __init init_dma_coherent_pool_size(unsigned long size)
330 {
331 /*
332 * Catch any attempt to set the pool size too late.
333 */
334 BUG_ON(atomic_pool);
335
336 /*
337 * Set architecture specific coherent pool size only if
338 * it has not been changed by kernel command line parameter.
339 */
340 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
341 atomic_pool_size = size;
342 }
343
344 /*
345 * Initialise the coherent pool for atomic allocations.
346 */
atomic_pool_init(void)347 static int __init atomic_pool_init(void)
348 {
349 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
350 gfp_t gfp = GFP_KERNEL | GFP_DMA;
351 struct page *page;
352 void *ptr;
353
354 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
355 if (!atomic_pool)
356 goto out;
357
358 if (dev_get_cma_area(NULL))
359 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
360 &page, atomic_pool_init);
361 else
362 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
363 &page, atomic_pool_init);
364 if (ptr) {
365 int ret;
366
367 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
368 page_to_phys(page),
369 atomic_pool_size, -1);
370 if (ret)
371 goto destroy_genpool;
372
373 gen_pool_set_algo(atomic_pool,
374 gen_pool_first_fit_order_align,
375 (void *)PAGE_SHIFT);
376 pr_info("DMA: preallocated %zd KiB pool for atomic coherent allocations\n",
377 atomic_pool_size / 1024);
378 return 0;
379 }
380
381 destroy_genpool:
382 gen_pool_destroy(atomic_pool);
383 atomic_pool = NULL;
384 out:
385 pr_err("DMA: failed to allocate %zx KiB pool for atomic coherent allocation\n",
386 atomic_pool_size / 1024);
387 return -ENOMEM;
388 }
389 /*
390 * CMA is activated by core_initcall, so we must be called after it.
391 */
392 postcore_initcall(atomic_pool_init);
393
394 struct dma_contig_early_reserve {
395 phys_addr_t base;
396 unsigned long size;
397 };
398
399 static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
400
401 static int dma_mmu_remap_num __initdata;
402
dma_contiguous_early_fixup(phys_addr_t base,unsigned long size)403 void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
404 {
405 dma_mmu_remap[dma_mmu_remap_num].base = base;
406 dma_mmu_remap[dma_mmu_remap_num].size = size;
407 dma_mmu_remap_num++;
408 }
409
dma_contiguous_remap(void)410 void __init dma_contiguous_remap(void)
411 {
412 int i;
413 for (i = 0; i < dma_mmu_remap_num; i++) {
414 phys_addr_t start = dma_mmu_remap[i].base;
415 phys_addr_t end = start + dma_mmu_remap[i].size;
416 struct map_desc map;
417 unsigned long addr;
418
419 if (end > arm_lowmem_limit)
420 end = arm_lowmem_limit;
421 if (start >= end)
422 continue;
423
424 map.pfn = __phys_to_pfn(start);
425 map.virtual = __phys_to_virt(start);
426 map.length = end - start;
427 map.type = MT_MEMORY_DMA_READY;
428
429 /*
430 * Clear previous low-memory mapping to ensure that the
431 * TLB does not see any conflicting entries, then flush
432 * the TLB of the old entries before creating new mappings.
433 *
434 * This ensures that any speculatively loaded TLB entries
435 * (even though they may be rare) can not cause any problems,
436 * and ensures that this code is architecturally compliant.
437 */
438 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
439 addr += PMD_SIZE)
440 pmd_clear(pmd_off_k(addr));
441
442 flush_tlb_kernel_range(__phys_to_virt(start),
443 __phys_to_virt(end));
444
445 iotable_init(&map, 1);
446 }
447 }
448
__dma_update_pte(pte_t * pte,pgtable_t token,unsigned long addr,void * data)449 static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
450 void *data)
451 {
452 struct page *page = virt_to_page(addr);
453 pgprot_t prot = *(pgprot_t *)data;
454
455 set_pte_ext(pte, mk_pte(page, prot), 0);
456 return 0;
457 }
458
__dma_remap(struct page * page,size_t size,pgprot_t prot)459 static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
460 {
461 unsigned long start = (unsigned long) page_address(page);
462 unsigned end = start + size;
463
464 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
465 flush_tlb_kernel_range(start, end);
466 }
467
__alloc_remap_buffer(struct device * dev,size_t size,gfp_t gfp,pgprot_t prot,struct page ** ret_page,const void * caller)468 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
469 pgprot_t prot, struct page **ret_page,
470 const void *caller)
471 {
472 struct page *page;
473 void *ptr;
474 page = __dma_alloc_buffer(dev, size, gfp);
475 if (!page)
476 return NULL;
477
478 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
479 if (!ptr) {
480 __dma_free_buffer(page, size);
481 return NULL;
482 }
483
484 *ret_page = page;
485 return ptr;
486 }
487
__alloc_from_pool(size_t size,struct page ** ret_page)488 static void *__alloc_from_pool(size_t size, struct page **ret_page)
489 {
490 unsigned long val;
491 void *ptr = NULL;
492
493 if (!atomic_pool) {
494 WARN(1, "coherent pool not initialised!\n");
495 return NULL;
496 }
497
498 val = gen_pool_alloc(atomic_pool, size);
499 if (val) {
500 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
501
502 *ret_page = phys_to_page(phys);
503 ptr = (void *)val;
504 }
505
506 return ptr;
507 }
508
__in_atomic_pool(void * start,size_t size)509 static bool __in_atomic_pool(void *start, size_t size)
510 {
511 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
512 }
513
__free_from_pool(void * start,size_t size)514 static int __free_from_pool(void *start, size_t size)
515 {
516 if (!__in_atomic_pool(start, size))
517 return 0;
518
519 gen_pool_free(atomic_pool, (unsigned long)start, size);
520
521 return 1;
522 }
523
__alloc_from_contiguous(struct device * dev,size_t size,pgprot_t prot,struct page ** ret_page,const void * caller)524 static void *__alloc_from_contiguous(struct device *dev, size_t size,
525 pgprot_t prot, struct page **ret_page,
526 const void *caller)
527 {
528 unsigned long order = get_order(size);
529 size_t count = size >> PAGE_SHIFT;
530 struct page *page;
531 void *ptr;
532
533 page = dma_alloc_from_contiguous(dev, count, order);
534 if (!page)
535 return NULL;
536
537 __dma_clear_buffer(page, size);
538
539 if (PageHighMem(page)) {
540 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
541 if (!ptr) {
542 dma_release_from_contiguous(dev, page, count);
543 return NULL;
544 }
545 } else {
546 __dma_remap(page, size, prot);
547 ptr = page_address(page);
548 }
549 *ret_page = page;
550 return ptr;
551 }
552
__free_from_contiguous(struct device * dev,struct page * page,void * cpu_addr,size_t size)553 static void __free_from_contiguous(struct device *dev, struct page *page,
554 void *cpu_addr, size_t size)
555 {
556 if (PageHighMem(page))
557 __dma_free_remap(cpu_addr, size);
558 else
559 __dma_remap(page, size, PAGE_KERNEL);
560 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
561 }
562
__get_dma_pgprot(struct dma_attrs * attrs,pgprot_t prot)563 static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
564 {
565 prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
566 pgprot_writecombine(prot) :
567 pgprot_dmacoherent(prot);
568 return prot;
569 }
570
571 #define nommu() 0
572
573 #else /* !CONFIG_MMU */
574
575 #define nommu() 1
576
577 #define __get_dma_pgprot(attrs, prot) __pgprot(0)
578 #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
579 #define __alloc_from_pool(size, ret_page) NULL
580 #define __alloc_from_contiguous(dev, size, prot, ret, c) NULL
581 #define __free_from_pool(cpu_addr, size) 0
582 #define __free_from_contiguous(dev, page, cpu_addr, size) do { } while (0)
583 #define __dma_free_remap(cpu_addr, size) do { } while (0)
584
585 #endif /* CONFIG_MMU */
586
__alloc_simple_buffer(struct device * dev,size_t size,gfp_t gfp,struct page ** ret_page)587 static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
588 struct page **ret_page)
589 {
590 struct page *page;
591 page = __dma_alloc_buffer(dev, size, gfp);
592 if (!page)
593 return NULL;
594
595 *ret_page = page;
596 return page_address(page);
597 }
598
599
600
__dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp,pgprot_t prot,bool is_coherent,const void * caller)601 static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
602 gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller)
603 {
604 u64 mask = get_coherent_dma_mask(dev);
605 struct page *page = NULL;
606 void *addr;
607
608 #ifdef CONFIG_DMA_API_DEBUG
609 u64 limit = (mask + 1) & ~mask;
610 if (limit && size >= limit) {
611 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
612 size, mask);
613 return NULL;
614 }
615 #endif
616
617 if (!mask)
618 return NULL;
619
620 if (mask < 0xffffffffULL)
621 gfp |= GFP_DMA;
622
623 /*
624 * Following is a work-around (a.k.a. hack) to prevent pages
625 * with __GFP_COMP being passed to split_page() which cannot
626 * handle them. The real problem is that this flag probably
627 * should be 0 on ARM as it is not supported on this
628 * platform; see CONFIG_HUGETLBFS.
629 */
630 gfp &= ~(__GFP_COMP);
631
632 *handle = DMA_ERROR_CODE;
633 size = PAGE_ALIGN(size);
634
635 if (is_coherent || nommu())
636 addr = __alloc_simple_buffer(dev, size, gfp, &page);
637 else if (!(gfp & __GFP_WAIT))
638 addr = __alloc_from_pool(size, &page);
639 else if (!dev_get_cma_area(dev))
640 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
641 else
642 addr = __alloc_from_contiguous(dev, size, prot, &page, caller);
643
644 if (addr)
645 *handle = pfn_to_dma(dev, page_to_pfn(page));
646
647 return addr;
648 }
649
650 /*
651 * Allocate DMA-coherent memory space and return both the kernel remapped
652 * virtual and bus address for that space.
653 */
arm_dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp,struct dma_attrs * attrs)654 void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
655 gfp_t gfp, struct dma_attrs *attrs)
656 {
657 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
658 void *memory;
659
660 if (dma_alloc_from_coherent(dev, size, handle, &memory))
661 return memory;
662
663 return __dma_alloc(dev, size, handle, gfp, prot, false,
664 __builtin_return_address(0));
665 }
666
arm_coherent_dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp,struct dma_attrs * attrs)667 static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
668 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
669 {
670 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
671 void *memory;
672
673 if (dma_alloc_from_coherent(dev, size, handle, &memory))
674 return memory;
675
676 return __dma_alloc(dev, size, handle, gfp, prot, true,
677 __builtin_return_address(0));
678 }
679
680 /*
681 * Create userspace mapping for the DMA-coherent memory.
682 */
arm_dma_mmap(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size,struct dma_attrs * attrs)683 int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
684 void *cpu_addr, dma_addr_t dma_addr, size_t size,
685 struct dma_attrs *attrs)
686 {
687 int ret = -ENXIO;
688 #ifdef CONFIG_MMU
689 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
690 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
691 unsigned long pfn = dma_to_pfn(dev, dma_addr);
692 unsigned long off = vma->vm_pgoff;
693
694 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
695
696 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
697 return ret;
698
699 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
700 ret = remap_pfn_range(vma, vma->vm_start,
701 pfn + off,
702 vma->vm_end - vma->vm_start,
703 vma->vm_page_prot);
704 }
705 #endif /* CONFIG_MMU */
706
707 return ret;
708 }
709
710 /*
711 * Free a buffer as defined by the above mapping.
712 */
__arm_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,struct dma_attrs * attrs,bool is_coherent)713 static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
714 dma_addr_t handle, struct dma_attrs *attrs,
715 bool is_coherent)
716 {
717 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
718
719 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
720 return;
721
722 size = PAGE_ALIGN(size);
723
724 if (is_coherent || nommu()) {
725 __dma_free_buffer(page, size);
726 } else if (__free_from_pool(cpu_addr, size)) {
727 return;
728 } else if (!dev_get_cma_area(dev)) {
729 __dma_free_remap(cpu_addr, size);
730 __dma_free_buffer(page, size);
731 } else {
732 /*
733 * Non-atomic allocations cannot be freed with IRQs disabled
734 */
735 WARN_ON(irqs_disabled());
736 __free_from_contiguous(dev, page, cpu_addr, size);
737 }
738 }
739
arm_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,struct dma_attrs * attrs)740 void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
741 dma_addr_t handle, struct dma_attrs *attrs)
742 {
743 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
744 }
745
arm_coherent_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,struct dma_attrs * attrs)746 static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
747 dma_addr_t handle, struct dma_attrs *attrs)
748 {
749 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
750 }
751
752 /*
753 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
754 * that the intention is to allow exporting memory allocated via the
755 * coherent DMA APIs through the dma_buf API, which only accepts a
756 * scattertable. This presents a couple of problems:
757 * 1. Not all memory allocated via the coherent DMA APIs is backed by
758 * a struct page
759 * 2. Passing coherent DMA memory into the streaming APIs is not allowed
760 * as we will try to flush the memory through a different alias to that
761 * actually being used (and the flushes are redundant.)
762 */
arm_dma_get_sgtable(struct device * dev,struct sg_table * sgt,void * cpu_addr,dma_addr_t handle,size_t size,struct dma_attrs * attrs)763 int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
764 void *cpu_addr, dma_addr_t handle, size_t size,
765 struct dma_attrs *attrs)
766 {
767 unsigned long pfn = dma_to_pfn(dev, handle);
768 struct page *page;
769 int ret;
770
771 /* If the PFN is not valid, we do not have a struct page */
772 if (!pfn_valid(pfn))
773 return -ENXIO;
774
775 page = pfn_to_page(pfn);
776
777 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
778 if (unlikely(ret))
779 return ret;
780
781 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
782 return 0;
783 }
784
dma_cache_maint_page(struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,void (* op)(const void *,size_t,int))785 static void dma_cache_maint_page(struct page *page, unsigned long offset,
786 size_t size, enum dma_data_direction dir,
787 void (*op)(const void *, size_t, int))
788 {
789 unsigned long pfn;
790 size_t left = size;
791
792 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
793 offset %= PAGE_SIZE;
794
795 /*
796 * A single sg entry may refer to multiple physically contiguous
797 * pages. But we still need to process highmem pages individually.
798 * If highmem is not configured then the bulk of this loop gets
799 * optimized out.
800 */
801 do {
802 size_t len = left;
803 void *vaddr;
804
805 page = pfn_to_page(pfn);
806
807 if (PageHighMem(page)) {
808 if (len + offset > PAGE_SIZE)
809 len = PAGE_SIZE - offset;
810
811 if (cache_is_vipt_nonaliasing()) {
812 vaddr = kmap_atomic(page);
813 op(vaddr + offset, len, dir);
814 kunmap_atomic(vaddr);
815 } else {
816 vaddr = kmap_high_get(page);
817 if (vaddr) {
818 op(vaddr + offset, len, dir);
819 kunmap_high(page);
820 }
821 }
822 } else {
823 vaddr = page_address(page) + offset;
824 op(vaddr, len, dir);
825 }
826 offset = 0;
827 pfn++;
828 left -= len;
829 } while (left);
830 }
831
832 /*
833 * Make an area consistent for devices.
834 * Note: Drivers should NOT use this function directly, as it will break
835 * platforms with CONFIG_DMABOUNCE.
836 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
837 */
__dma_page_cpu_to_dev(struct page * page,unsigned long off,size_t size,enum dma_data_direction dir)838 static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
839 size_t size, enum dma_data_direction dir)
840 {
841 phys_addr_t paddr;
842
843 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
844
845 paddr = page_to_phys(page) + off;
846 if (dir == DMA_FROM_DEVICE) {
847 outer_inv_range(paddr, paddr + size);
848 } else {
849 outer_clean_range(paddr, paddr + size);
850 }
851 /* FIXME: non-speculating: flush on bidirectional mappings? */
852 }
853
__dma_page_dev_to_cpu(struct page * page,unsigned long off,size_t size,enum dma_data_direction dir)854 static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
855 size_t size, enum dma_data_direction dir)
856 {
857 phys_addr_t paddr = page_to_phys(page) + off;
858
859 /* FIXME: non-speculating: not required */
860 /* in any case, don't bother invalidating if DMA to device */
861 if (dir != DMA_TO_DEVICE) {
862 outer_inv_range(paddr, paddr + size);
863
864 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
865 }
866
867 /*
868 * Mark the D-cache clean for these pages to avoid extra flushing.
869 */
870 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
871 unsigned long pfn;
872 size_t left = size;
873
874 pfn = page_to_pfn(page) + off / PAGE_SIZE;
875 off %= PAGE_SIZE;
876 if (off) {
877 pfn++;
878 left -= PAGE_SIZE - off;
879 }
880 while (left >= PAGE_SIZE) {
881 page = pfn_to_page(pfn++);
882 set_bit(PG_dcache_clean, &page->flags);
883 left -= PAGE_SIZE;
884 }
885 }
886 }
887
888 /**
889 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
890 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
891 * @sg: list of buffers
892 * @nents: number of buffers to map
893 * @dir: DMA transfer direction
894 *
895 * Map a set of buffers described by scatterlist in streaming mode for DMA.
896 * This is the scatter-gather version of the dma_map_single interface.
897 * Here the scatter gather list elements are each tagged with the
898 * appropriate dma address and length. They are obtained via
899 * sg_dma_{address,length}.
900 *
901 * Device ownership issues as mentioned for dma_map_single are the same
902 * here.
903 */
arm_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs)904 int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
905 enum dma_data_direction dir, struct dma_attrs *attrs)
906 {
907 struct dma_map_ops *ops = get_dma_ops(dev);
908 struct scatterlist *s;
909 int i, j;
910
911 for_each_sg(sg, s, nents, i) {
912 #ifdef CONFIG_NEED_SG_DMA_LENGTH
913 s->dma_length = s->length;
914 #endif
915 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
916 s->length, dir, attrs);
917 if (dma_mapping_error(dev, s->dma_address))
918 goto bad_mapping;
919 }
920 return nents;
921
922 bad_mapping:
923 for_each_sg(sg, s, i, j)
924 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
925 return 0;
926 }
927
928 /**
929 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
930 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
931 * @sg: list of buffers
932 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
933 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
934 *
935 * Unmap a set of streaming mode DMA translations. Again, CPU access
936 * rules concerning calls here are the same as for dma_unmap_single().
937 */
arm_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs)938 void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
939 enum dma_data_direction dir, struct dma_attrs *attrs)
940 {
941 struct dma_map_ops *ops = get_dma_ops(dev);
942 struct scatterlist *s;
943
944 int i;
945
946 for_each_sg(sg, s, nents, i)
947 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
948 }
949
950 /**
951 * arm_dma_sync_sg_for_cpu
952 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
953 * @sg: list of buffers
954 * @nents: number of buffers to map (returned from dma_map_sg)
955 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
956 */
arm_dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)957 void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
958 int nents, enum dma_data_direction dir)
959 {
960 struct dma_map_ops *ops = get_dma_ops(dev);
961 struct scatterlist *s;
962 int i;
963
964 for_each_sg(sg, s, nents, i)
965 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
966 dir);
967 }
968
969 /**
970 * arm_dma_sync_sg_for_device
971 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
972 * @sg: list of buffers
973 * @nents: number of buffers to map (returned from dma_map_sg)
974 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
975 */
arm_dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)976 void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
977 int nents, enum dma_data_direction dir)
978 {
979 struct dma_map_ops *ops = get_dma_ops(dev);
980 struct scatterlist *s;
981 int i;
982
983 for_each_sg(sg, s, nents, i)
984 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
985 dir);
986 }
987
988 /*
989 * Return whether the given device DMA address mask can be supported
990 * properly. For example, if your device can only drive the low 24-bits
991 * during bus mastering, then you would pass 0x00ffffff as the mask
992 * to this function.
993 */
dma_supported(struct device * dev,u64 mask)994 int dma_supported(struct device *dev, u64 mask)
995 {
996 return __dma_supported(dev, mask, false);
997 }
998 EXPORT_SYMBOL(dma_supported);
999
arm_dma_set_mask(struct device * dev,u64 dma_mask)1000 int arm_dma_set_mask(struct device *dev, u64 dma_mask)
1001 {
1002 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1003 return -EIO;
1004
1005 *dev->dma_mask = dma_mask;
1006
1007 return 0;
1008 }
1009
1010 #define PREALLOC_DMA_DEBUG_ENTRIES 4096
1011
dma_debug_do_init(void)1012 static int __init dma_debug_do_init(void)
1013 {
1014 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1015 return 0;
1016 }
1017 fs_initcall(dma_debug_do_init);
1018
1019 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1020
1021 /* IOMMU */
1022
1023 static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1024
__alloc_iova(struct dma_iommu_mapping * mapping,size_t size)1025 static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1026 size_t size)
1027 {
1028 unsigned int order = get_order(size);
1029 unsigned int align = 0;
1030 unsigned int count, start;
1031 size_t mapping_size = mapping->bits << PAGE_SHIFT;
1032 unsigned long flags;
1033 dma_addr_t iova;
1034 int i;
1035
1036 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1037 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1038
1039 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1040 align = (1 << order) - 1;
1041
1042 spin_lock_irqsave(&mapping->lock, flags);
1043 for (i = 0; i < mapping->nr_bitmaps; i++) {
1044 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1045 mapping->bits, 0, count, align);
1046
1047 if (start > mapping->bits)
1048 continue;
1049
1050 bitmap_set(mapping->bitmaps[i], start, count);
1051 break;
1052 }
1053
1054 /*
1055 * No unused range found. Try to extend the existing mapping
1056 * and perform a second attempt to reserve an IO virtual
1057 * address range of size bytes.
1058 */
1059 if (i == mapping->nr_bitmaps) {
1060 if (extend_iommu_mapping(mapping)) {
1061 spin_unlock_irqrestore(&mapping->lock, flags);
1062 return DMA_ERROR_CODE;
1063 }
1064
1065 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1066 mapping->bits, 0, count, align);
1067
1068 if (start > mapping->bits) {
1069 spin_unlock_irqrestore(&mapping->lock, flags);
1070 return DMA_ERROR_CODE;
1071 }
1072
1073 bitmap_set(mapping->bitmaps[i], start, count);
1074 }
1075 spin_unlock_irqrestore(&mapping->lock, flags);
1076
1077 iova = mapping->base + (mapping_size * i);
1078 iova += start << PAGE_SHIFT;
1079
1080 return iova;
1081 }
1082
__free_iova(struct dma_iommu_mapping * mapping,dma_addr_t addr,size_t size)1083 static inline void __free_iova(struct dma_iommu_mapping *mapping,
1084 dma_addr_t addr, size_t size)
1085 {
1086 unsigned int start, count;
1087 size_t mapping_size = mapping->bits << PAGE_SHIFT;
1088 unsigned long flags;
1089 dma_addr_t bitmap_base;
1090 u32 bitmap_index;
1091
1092 if (!size)
1093 return;
1094
1095 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
1096 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1097
1098 bitmap_base = mapping->base + mapping_size * bitmap_index;
1099
1100 start = (addr - bitmap_base) >> PAGE_SHIFT;
1101
1102 if (addr + size > bitmap_base + mapping_size) {
1103 /*
1104 * The address range to be freed reaches into the iova
1105 * range of the next bitmap. This should not happen as
1106 * we don't allow this in __alloc_iova (at the
1107 * moment).
1108 */
1109 BUG();
1110 } else
1111 count = size >> PAGE_SHIFT;
1112
1113 spin_lock_irqsave(&mapping->lock, flags);
1114 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
1115 spin_unlock_irqrestore(&mapping->lock, flags);
1116 }
1117
__iommu_alloc_buffer(struct device * dev,size_t size,gfp_t gfp,struct dma_attrs * attrs)1118 static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1119 gfp_t gfp, struct dma_attrs *attrs)
1120 {
1121 struct page **pages;
1122 int count = size >> PAGE_SHIFT;
1123 int array_size = count * sizeof(struct page *);
1124 int i = 0;
1125
1126 if (array_size <= PAGE_SIZE)
1127 pages = kzalloc(array_size, gfp);
1128 else
1129 pages = vzalloc(array_size);
1130 if (!pages)
1131 return NULL;
1132
1133 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
1134 {
1135 unsigned long order = get_order(size);
1136 struct page *page;
1137
1138 page = dma_alloc_from_contiguous(dev, count, order);
1139 if (!page)
1140 goto error;
1141
1142 __dma_clear_buffer(page, size);
1143
1144 for (i = 0; i < count; i++)
1145 pages[i] = page + i;
1146
1147 return pages;
1148 }
1149
1150 /*
1151 * IOMMU can map any pages, so himem can also be used here
1152 */
1153 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1154
1155 while (count) {
1156 int j, order = __fls(count);
1157
1158 pages[i] = alloc_pages(gfp, order);
1159 while (!pages[i] && order)
1160 pages[i] = alloc_pages(gfp, --order);
1161 if (!pages[i])
1162 goto error;
1163
1164 if (order) {
1165 split_page(pages[i], order);
1166 j = 1 << order;
1167 while (--j)
1168 pages[i + j] = pages[i] + j;
1169 }
1170
1171 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
1172 i += 1 << order;
1173 count -= 1 << order;
1174 }
1175
1176 return pages;
1177 error:
1178 while (i--)
1179 if (pages[i])
1180 __free_pages(pages[i], 0);
1181 if (array_size <= PAGE_SIZE)
1182 kfree(pages);
1183 else
1184 vfree(pages);
1185 return NULL;
1186 }
1187
__iommu_free_buffer(struct device * dev,struct page ** pages,size_t size,struct dma_attrs * attrs)1188 static int __iommu_free_buffer(struct device *dev, struct page **pages,
1189 size_t size, struct dma_attrs *attrs)
1190 {
1191 int count = size >> PAGE_SHIFT;
1192 int array_size = count * sizeof(struct page *);
1193 int i;
1194
1195 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
1196 dma_release_from_contiguous(dev, pages[0], count);
1197 } else {
1198 for (i = 0; i < count; i++)
1199 if (pages[i])
1200 __free_pages(pages[i], 0);
1201 }
1202
1203 if (array_size <= PAGE_SIZE)
1204 kfree(pages);
1205 else
1206 vfree(pages);
1207 return 0;
1208 }
1209
1210 /*
1211 * Create a CPU mapping for a specified pages
1212 */
1213 static void *
__iommu_alloc_remap(struct page ** pages,size_t size,gfp_t gfp,pgprot_t prot,const void * caller)1214 __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1215 const void *caller)
1216 {
1217 return dma_common_pages_remap(pages, size,
1218 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
1219 }
1220
1221 /*
1222 * Create a mapping in device IO address space for specified pages
1223 */
1224 static dma_addr_t
__iommu_create_mapping(struct device * dev,struct page ** pages,size_t size)1225 __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1226 {
1227 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1228 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1229 dma_addr_t dma_addr, iova;
1230 int i, ret = DMA_ERROR_CODE;
1231
1232 dma_addr = __alloc_iova(mapping, size);
1233 if (dma_addr == DMA_ERROR_CODE)
1234 return dma_addr;
1235
1236 iova = dma_addr;
1237 for (i = 0; i < count; ) {
1238 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1239 phys_addr_t phys = page_to_phys(pages[i]);
1240 unsigned int len, j;
1241
1242 for (j = i + 1; j < count; j++, next_pfn++)
1243 if (page_to_pfn(pages[j]) != next_pfn)
1244 break;
1245
1246 len = (j - i) << PAGE_SHIFT;
1247 ret = iommu_map(mapping->domain, iova, phys, len,
1248 IOMMU_READ|IOMMU_WRITE);
1249 if (ret < 0)
1250 goto fail;
1251 iova += len;
1252 i = j;
1253 }
1254 return dma_addr;
1255 fail:
1256 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1257 __free_iova(mapping, dma_addr, size);
1258 return DMA_ERROR_CODE;
1259 }
1260
__iommu_remove_mapping(struct device * dev,dma_addr_t iova,size_t size)1261 static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1262 {
1263 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1264
1265 /*
1266 * add optional in-page offset from iova to size and align
1267 * result to page size
1268 */
1269 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1270 iova &= PAGE_MASK;
1271
1272 iommu_unmap(mapping->domain, iova, size);
1273 __free_iova(mapping, iova, size);
1274 return 0;
1275 }
1276
__atomic_get_pages(void * addr)1277 static struct page **__atomic_get_pages(void *addr)
1278 {
1279 struct page *page;
1280 phys_addr_t phys;
1281
1282 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1283 page = phys_to_page(phys);
1284
1285 return (struct page **)page;
1286 }
1287
__iommu_get_pages(void * cpu_addr,struct dma_attrs * attrs)1288 static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
1289 {
1290 struct vm_struct *area;
1291
1292 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1293 return __atomic_get_pages(cpu_addr);
1294
1295 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1296 return cpu_addr;
1297
1298 area = find_vm_area(cpu_addr);
1299 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1300 return area->pages;
1301 return NULL;
1302 }
1303
__iommu_alloc_atomic(struct device * dev,size_t size,dma_addr_t * handle)1304 static void *__iommu_alloc_atomic(struct device *dev, size_t size,
1305 dma_addr_t *handle)
1306 {
1307 struct page *page;
1308 void *addr;
1309
1310 addr = __alloc_from_pool(size, &page);
1311 if (!addr)
1312 return NULL;
1313
1314 *handle = __iommu_create_mapping(dev, &page, size);
1315 if (*handle == DMA_ERROR_CODE)
1316 goto err_mapping;
1317
1318 return addr;
1319
1320 err_mapping:
1321 __free_from_pool(addr, size);
1322 return NULL;
1323 }
1324
__iommu_free_atomic(struct device * dev,void * cpu_addr,dma_addr_t handle,size_t size)1325 static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
1326 dma_addr_t handle, size_t size)
1327 {
1328 __iommu_remove_mapping(dev, handle, size);
1329 __free_from_pool(cpu_addr, size);
1330 }
1331
arm_iommu_alloc_attrs(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp,struct dma_attrs * attrs)1332 static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1333 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1334 {
1335 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
1336 struct page **pages;
1337 void *addr = NULL;
1338
1339 *handle = DMA_ERROR_CODE;
1340 size = PAGE_ALIGN(size);
1341
1342 if (!(gfp & __GFP_WAIT))
1343 return __iommu_alloc_atomic(dev, size, handle);
1344
1345 /*
1346 * Following is a work-around (a.k.a. hack) to prevent pages
1347 * with __GFP_COMP being passed to split_page() which cannot
1348 * handle them. The real problem is that this flag probably
1349 * should be 0 on ARM as it is not supported on this
1350 * platform; see CONFIG_HUGETLBFS.
1351 */
1352 gfp &= ~(__GFP_COMP);
1353
1354 pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
1355 if (!pages)
1356 return NULL;
1357
1358 *handle = __iommu_create_mapping(dev, pages, size);
1359 if (*handle == DMA_ERROR_CODE)
1360 goto err_buffer;
1361
1362 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1363 return pages;
1364
1365 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1366 __builtin_return_address(0));
1367 if (!addr)
1368 goto err_mapping;
1369
1370 return addr;
1371
1372 err_mapping:
1373 __iommu_remove_mapping(dev, *handle, size);
1374 err_buffer:
1375 __iommu_free_buffer(dev, pages, size, attrs);
1376 return NULL;
1377 }
1378
arm_iommu_mmap_attrs(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size,struct dma_attrs * attrs)1379 static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1380 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1381 struct dma_attrs *attrs)
1382 {
1383 unsigned long uaddr = vma->vm_start;
1384 unsigned long usize = vma->vm_end - vma->vm_start;
1385 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1386
1387 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1388
1389 if (!pages)
1390 return -ENXIO;
1391
1392 do {
1393 int ret = vm_insert_page(vma, uaddr, *pages++);
1394 if (ret) {
1395 pr_err("Remapping memory failed: %d\n", ret);
1396 return ret;
1397 }
1398 uaddr += PAGE_SIZE;
1399 usize -= PAGE_SIZE;
1400 } while (usize > 0);
1401
1402 return 0;
1403 }
1404
1405 /*
1406 * free a page as defined by the above mapping.
1407 * Must not be called with IRQs disabled.
1408 */
arm_iommu_free_attrs(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,struct dma_attrs * attrs)1409 void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1410 dma_addr_t handle, struct dma_attrs *attrs)
1411 {
1412 struct page **pages;
1413 size = PAGE_ALIGN(size);
1414
1415 if (__in_atomic_pool(cpu_addr, size)) {
1416 __iommu_free_atomic(dev, cpu_addr, handle, size);
1417 return;
1418 }
1419
1420 pages = __iommu_get_pages(cpu_addr, attrs);
1421 if (!pages) {
1422 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1423 return;
1424 }
1425
1426 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
1427 dma_common_free_remap(cpu_addr, size,
1428 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
1429 }
1430
1431 __iommu_remove_mapping(dev, handle, size);
1432 __iommu_free_buffer(dev, pages, size, attrs);
1433 }
1434
arm_iommu_get_sgtable(struct device * dev,struct sg_table * sgt,void * cpu_addr,dma_addr_t dma_addr,size_t size,struct dma_attrs * attrs)1435 static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1436 void *cpu_addr, dma_addr_t dma_addr,
1437 size_t size, struct dma_attrs *attrs)
1438 {
1439 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1440 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1441
1442 if (!pages)
1443 return -ENXIO;
1444
1445 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1446 GFP_KERNEL);
1447 }
1448
__dma_direction_to_prot(enum dma_data_direction dir)1449 static int __dma_direction_to_prot(enum dma_data_direction dir)
1450 {
1451 int prot;
1452
1453 switch (dir) {
1454 case DMA_BIDIRECTIONAL:
1455 prot = IOMMU_READ | IOMMU_WRITE;
1456 break;
1457 case DMA_TO_DEVICE:
1458 prot = IOMMU_READ;
1459 break;
1460 case DMA_FROM_DEVICE:
1461 prot = IOMMU_WRITE;
1462 break;
1463 default:
1464 prot = 0;
1465 }
1466
1467 return prot;
1468 }
1469
1470 /*
1471 * Map a part of the scatter-gather list into contiguous io address space
1472 */
__map_sg_chunk(struct device * dev,struct scatterlist * sg,size_t size,dma_addr_t * handle,enum dma_data_direction dir,struct dma_attrs * attrs,bool is_coherent)1473 static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1474 size_t size, dma_addr_t *handle,
1475 enum dma_data_direction dir, struct dma_attrs *attrs,
1476 bool is_coherent)
1477 {
1478 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1479 dma_addr_t iova, iova_base;
1480 int ret = 0;
1481 unsigned int count;
1482 struct scatterlist *s;
1483 int prot;
1484
1485 size = PAGE_ALIGN(size);
1486 *handle = DMA_ERROR_CODE;
1487
1488 iova_base = iova = __alloc_iova(mapping, size);
1489 if (iova == DMA_ERROR_CODE)
1490 return -ENOMEM;
1491
1492 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1493 phys_addr_t phys = page_to_phys(sg_page(s));
1494 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1495
1496 if (!is_coherent &&
1497 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1498 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1499
1500 prot = __dma_direction_to_prot(dir);
1501
1502 ret = iommu_map(mapping->domain, iova, phys, len, prot);
1503 if (ret < 0)
1504 goto fail;
1505 count += len >> PAGE_SHIFT;
1506 iova += len;
1507 }
1508 *handle = iova_base;
1509
1510 return 0;
1511 fail:
1512 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1513 __free_iova(mapping, iova_base, size);
1514 return ret;
1515 }
1516
__iommu_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs,bool is_coherent)1517 static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1518 enum dma_data_direction dir, struct dma_attrs *attrs,
1519 bool is_coherent)
1520 {
1521 struct scatterlist *s = sg, *dma = sg, *start = sg;
1522 int i, count = 0;
1523 unsigned int offset = s->offset;
1524 unsigned int size = s->offset + s->length;
1525 unsigned int max = dma_get_max_seg_size(dev);
1526
1527 for (i = 1; i < nents; i++) {
1528 s = sg_next(s);
1529
1530 s->dma_address = DMA_ERROR_CODE;
1531 s->dma_length = 0;
1532
1533 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1534 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
1535 dir, attrs, is_coherent) < 0)
1536 goto bad_mapping;
1537
1538 dma->dma_address += offset;
1539 dma->dma_length = size - offset;
1540
1541 size = offset = s->offset;
1542 start = s;
1543 dma = sg_next(dma);
1544 count += 1;
1545 }
1546 size += s->length;
1547 }
1548 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1549 is_coherent) < 0)
1550 goto bad_mapping;
1551
1552 dma->dma_address += offset;
1553 dma->dma_length = size - offset;
1554
1555 return count+1;
1556
1557 bad_mapping:
1558 for_each_sg(sg, s, count, i)
1559 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1560 return 0;
1561 }
1562
1563 /**
1564 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1565 * @dev: valid struct device pointer
1566 * @sg: list of buffers
1567 * @nents: number of buffers to map
1568 * @dir: DMA transfer direction
1569 *
1570 * Map a set of i/o coherent buffers described by scatterlist in streaming
1571 * mode for DMA. The scatter gather list elements are merged together (if
1572 * possible) and tagged with the appropriate dma address and length. They are
1573 * obtained via sg_dma_{address,length}.
1574 */
arm_coherent_iommu_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs)1575 int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1576 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1577 {
1578 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1579 }
1580
1581 /**
1582 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1583 * @dev: valid struct device pointer
1584 * @sg: list of buffers
1585 * @nents: number of buffers to map
1586 * @dir: DMA transfer direction
1587 *
1588 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1589 * The scatter gather list elements are merged together (if possible) and
1590 * tagged with the appropriate dma address and length. They are obtained via
1591 * sg_dma_{address,length}.
1592 */
arm_iommu_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs)1593 int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1594 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1595 {
1596 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1597 }
1598
__iommu_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs,bool is_coherent)1599 static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1600 int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
1601 bool is_coherent)
1602 {
1603 struct scatterlist *s;
1604 int i;
1605
1606 for_each_sg(sg, s, nents, i) {
1607 if (sg_dma_len(s))
1608 __iommu_remove_mapping(dev, sg_dma_address(s),
1609 sg_dma_len(s));
1610 if (!is_coherent &&
1611 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1612 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1613 s->length, dir);
1614 }
1615 }
1616
1617 /**
1618 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1619 * @dev: valid struct device pointer
1620 * @sg: list of buffers
1621 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1622 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1623 *
1624 * Unmap a set of streaming mode DMA translations. Again, CPU access
1625 * rules concerning calls here are the same as for dma_unmap_single().
1626 */
arm_coherent_iommu_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs)1627 void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1628 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1629 {
1630 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1631 }
1632
1633 /**
1634 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1635 * @dev: valid struct device pointer
1636 * @sg: list of buffers
1637 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1638 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1639 *
1640 * Unmap a set of streaming mode DMA translations. Again, CPU access
1641 * rules concerning calls here are the same as for dma_unmap_single().
1642 */
arm_iommu_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,struct dma_attrs * attrs)1643 void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1644 enum dma_data_direction dir, struct dma_attrs *attrs)
1645 {
1646 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
1647 }
1648
1649 /**
1650 * arm_iommu_sync_sg_for_cpu
1651 * @dev: valid struct device pointer
1652 * @sg: list of buffers
1653 * @nents: number of buffers to map (returned from dma_map_sg)
1654 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1655 */
arm_iommu_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)1656 void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1657 int nents, enum dma_data_direction dir)
1658 {
1659 struct scatterlist *s;
1660 int i;
1661
1662 for_each_sg(sg, s, nents, i)
1663 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
1664
1665 }
1666
1667 /**
1668 * arm_iommu_sync_sg_for_device
1669 * @dev: valid struct device pointer
1670 * @sg: list of buffers
1671 * @nents: number of buffers to map (returned from dma_map_sg)
1672 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1673 */
arm_iommu_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)1674 void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1675 int nents, enum dma_data_direction dir)
1676 {
1677 struct scatterlist *s;
1678 int i;
1679
1680 for_each_sg(sg, s, nents, i)
1681 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1682 }
1683
1684
1685 /**
1686 * arm_coherent_iommu_map_page
1687 * @dev: valid struct device pointer
1688 * @page: page that buffer resides in
1689 * @offset: offset into page for start of buffer
1690 * @size: size of buffer to map
1691 * @dir: DMA transfer direction
1692 *
1693 * Coherent IOMMU aware version of arm_dma_map_page()
1694 */
arm_coherent_iommu_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)1695 static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1696 unsigned long offset, size_t size, enum dma_data_direction dir,
1697 struct dma_attrs *attrs)
1698 {
1699 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1700 dma_addr_t dma_addr;
1701 int ret, prot, len = PAGE_ALIGN(size + offset);
1702
1703 dma_addr = __alloc_iova(mapping, len);
1704 if (dma_addr == DMA_ERROR_CODE)
1705 return dma_addr;
1706
1707 prot = __dma_direction_to_prot(dir);
1708
1709 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
1710 if (ret < 0)
1711 goto fail;
1712
1713 return dma_addr + offset;
1714 fail:
1715 __free_iova(mapping, dma_addr, len);
1716 return DMA_ERROR_CODE;
1717 }
1718
1719 /**
1720 * arm_iommu_map_page
1721 * @dev: valid struct device pointer
1722 * @page: page that buffer resides in
1723 * @offset: offset into page for start of buffer
1724 * @size: size of buffer to map
1725 * @dir: DMA transfer direction
1726 *
1727 * IOMMU aware version of arm_dma_map_page()
1728 */
arm_iommu_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)1729 static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1730 unsigned long offset, size_t size, enum dma_data_direction dir,
1731 struct dma_attrs *attrs)
1732 {
1733 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1734 __dma_page_cpu_to_dev(page, offset, size, dir);
1735
1736 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1737 }
1738
1739 /**
1740 * arm_coherent_iommu_unmap_page
1741 * @dev: valid struct device pointer
1742 * @handle: DMA address of buffer
1743 * @size: size of buffer (same as passed to dma_map_page)
1744 * @dir: DMA transfer direction (same as passed to dma_map_page)
1745 *
1746 * Coherent IOMMU aware version of arm_dma_unmap_page()
1747 */
arm_coherent_iommu_unmap_page(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)1748 static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1749 size_t size, enum dma_data_direction dir,
1750 struct dma_attrs *attrs)
1751 {
1752 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1753 dma_addr_t iova = handle & PAGE_MASK;
1754 int offset = handle & ~PAGE_MASK;
1755 int len = PAGE_ALIGN(size + offset);
1756
1757 if (!iova)
1758 return;
1759
1760 iommu_unmap(mapping->domain, iova, len);
1761 __free_iova(mapping, iova, len);
1762 }
1763
1764 /**
1765 * arm_iommu_unmap_page
1766 * @dev: valid struct device pointer
1767 * @handle: DMA address of buffer
1768 * @size: size of buffer (same as passed to dma_map_page)
1769 * @dir: DMA transfer direction (same as passed to dma_map_page)
1770 *
1771 * IOMMU aware version of arm_dma_unmap_page()
1772 */
arm_iommu_unmap_page(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir,struct dma_attrs * attrs)1773 static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1774 size_t size, enum dma_data_direction dir,
1775 struct dma_attrs *attrs)
1776 {
1777 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1778 dma_addr_t iova = handle & PAGE_MASK;
1779 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1780 int offset = handle & ~PAGE_MASK;
1781 int len = PAGE_ALIGN(size + offset);
1782
1783 if (!iova)
1784 return;
1785
1786 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1787 __dma_page_dev_to_cpu(page, offset, size, dir);
1788
1789 iommu_unmap(mapping->domain, iova, len);
1790 __free_iova(mapping, iova, len);
1791 }
1792
arm_iommu_sync_single_for_cpu(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir)1793 static void arm_iommu_sync_single_for_cpu(struct device *dev,
1794 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1795 {
1796 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1797 dma_addr_t iova = handle & PAGE_MASK;
1798 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1799 unsigned int offset = handle & ~PAGE_MASK;
1800
1801 if (!iova)
1802 return;
1803
1804 __dma_page_dev_to_cpu(page, offset, size, dir);
1805 }
1806
arm_iommu_sync_single_for_device(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir)1807 static void arm_iommu_sync_single_for_device(struct device *dev,
1808 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1809 {
1810 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1811 dma_addr_t iova = handle & PAGE_MASK;
1812 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1813 unsigned int offset = handle & ~PAGE_MASK;
1814
1815 if (!iova)
1816 return;
1817
1818 __dma_page_cpu_to_dev(page, offset, size, dir);
1819 }
1820
1821 struct dma_map_ops iommu_ops = {
1822 .alloc = arm_iommu_alloc_attrs,
1823 .free = arm_iommu_free_attrs,
1824 .mmap = arm_iommu_mmap_attrs,
1825 .get_sgtable = arm_iommu_get_sgtable,
1826
1827 .map_page = arm_iommu_map_page,
1828 .unmap_page = arm_iommu_unmap_page,
1829 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
1830 .sync_single_for_device = arm_iommu_sync_single_for_device,
1831
1832 .map_sg = arm_iommu_map_sg,
1833 .unmap_sg = arm_iommu_unmap_sg,
1834 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
1835 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
1836
1837 .set_dma_mask = arm_dma_set_mask,
1838 };
1839
1840 struct dma_map_ops iommu_coherent_ops = {
1841 .alloc = arm_iommu_alloc_attrs,
1842 .free = arm_iommu_free_attrs,
1843 .mmap = arm_iommu_mmap_attrs,
1844 .get_sgtable = arm_iommu_get_sgtable,
1845
1846 .map_page = arm_coherent_iommu_map_page,
1847 .unmap_page = arm_coherent_iommu_unmap_page,
1848
1849 .map_sg = arm_coherent_iommu_map_sg,
1850 .unmap_sg = arm_coherent_iommu_unmap_sg,
1851
1852 .set_dma_mask = arm_dma_set_mask,
1853 };
1854
1855 /**
1856 * arm_iommu_create_mapping
1857 * @bus: pointer to the bus holding the client device (for IOMMU calls)
1858 * @base: start address of the valid IO address space
1859 * @size: maximum size of the valid IO address space
1860 *
1861 * Creates a mapping structure which holds information about used/unused
1862 * IO address ranges, which is required to perform memory allocation and
1863 * mapping with IOMMU aware functions.
1864 *
1865 * The client device need to be attached to the mapping with
1866 * arm_iommu_attach_device function.
1867 */
1868 struct dma_iommu_mapping *
arm_iommu_create_mapping(struct bus_type * bus,dma_addr_t base,size_t size)1869 arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size)
1870 {
1871 unsigned int bits = size >> PAGE_SHIFT;
1872 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
1873 struct dma_iommu_mapping *mapping;
1874 int extensions = 1;
1875 int err = -ENOMEM;
1876
1877 if (!bitmap_size)
1878 return ERR_PTR(-EINVAL);
1879
1880 if (bitmap_size > PAGE_SIZE) {
1881 extensions = bitmap_size / PAGE_SIZE;
1882 bitmap_size = PAGE_SIZE;
1883 }
1884
1885 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1886 if (!mapping)
1887 goto err;
1888
1889 mapping->bitmap_size = bitmap_size;
1890 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
1891 GFP_KERNEL);
1892 if (!mapping->bitmaps)
1893 goto err2;
1894
1895 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
1896 if (!mapping->bitmaps[0])
1897 goto err3;
1898
1899 mapping->nr_bitmaps = 1;
1900 mapping->extensions = extensions;
1901 mapping->base = base;
1902 mapping->bits = BITS_PER_BYTE * bitmap_size;
1903
1904 spin_lock_init(&mapping->lock);
1905
1906 mapping->domain = iommu_domain_alloc(bus);
1907 if (!mapping->domain)
1908 goto err4;
1909
1910 kref_init(&mapping->kref);
1911 return mapping;
1912 err4:
1913 kfree(mapping->bitmaps[0]);
1914 err3:
1915 kfree(mapping->bitmaps);
1916 err2:
1917 kfree(mapping);
1918 err:
1919 return ERR_PTR(err);
1920 }
1921 EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
1922
release_iommu_mapping(struct kref * kref)1923 static void release_iommu_mapping(struct kref *kref)
1924 {
1925 int i;
1926 struct dma_iommu_mapping *mapping =
1927 container_of(kref, struct dma_iommu_mapping, kref);
1928
1929 iommu_domain_free(mapping->domain);
1930 for (i = 0; i < mapping->nr_bitmaps; i++)
1931 kfree(mapping->bitmaps[i]);
1932 kfree(mapping->bitmaps);
1933 kfree(mapping);
1934 }
1935
extend_iommu_mapping(struct dma_iommu_mapping * mapping)1936 static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
1937 {
1938 int next_bitmap;
1939
1940 if (mapping->nr_bitmaps > mapping->extensions)
1941 return -EINVAL;
1942
1943 next_bitmap = mapping->nr_bitmaps;
1944 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
1945 GFP_ATOMIC);
1946 if (!mapping->bitmaps[next_bitmap])
1947 return -ENOMEM;
1948
1949 mapping->nr_bitmaps++;
1950
1951 return 0;
1952 }
1953
arm_iommu_release_mapping(struct dma_iommu_mapping * mapping)1954 void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1955 {
1956 if (mapping)
1957 kref_put(&mapping->kref, release_iommu_mapping);
1958 }
1959 EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
1960
1961 /**
1962 * arm_iommu_attach_device
1963 * @dev: valid struct device pointer
1964 * @mapping: io address space mapping structure (returned from
1965 * arm_iommu_create_mapping)
1966 *
1967 * Attaches specified io address space mapping to the provided device,
1968 * this replaces the dma operations (dma_map_ops pointer) with the
1969 * IOMMU aware version. More than one client might be attached to
1970 * the same io address space mapping.
1971 */
arm_iommu_attach_device(struct device * dev,struct dma_iommu_mapping * mapping)1972 int arm_iommu_attach_device(struct device *dev,
1973 struct dma_iommu_mapping *mapping)
1974 {
1975 int err;
1976
1977 err = iommu_attach_device(mapping->domain, dev);
1978 if (err)
1979 return err;
1980
1981 kref_get(&mapping->kref);
1982 dev->archdata.mapping = mapping;
1983 set_dma_ops(dev, &iommu_ops);
1984
1985 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
1986 return 0;
1987 }
1988 EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
1989
1990 /**
1991 * arm_iommu_detach_device
1992 * @dev: valid struct device pointer
1993 *
1994 * Detaches the provided device from a previously attached map.
1995 * This voids the dma operations (dma_map_ops pointer)
1996 */
arm_iommu_detach_device(struct device * dev)1997 void arm_iommu_detach_device(struct device *dev)
1998 {
1999 struct dma_iommu_mapping *mapping;
2000
2001 mapping = to_dma_iommu_mapping(dev);
2002 if (!mapping) {
2003 dev_warn(dev, "Not attached\n");
2004 return;
2005 }
2006
2007 iommu_detach_device(mapping->domain, dev);
2008 kref_put(&mapping->kref, release_iommu_mapping);
2009 dev->archdata.mapping = NULL;
2010 set_dma_ops(dev, NULL);
2011
2012 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2013 }
2014 EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
2015
2016 #endif
2017