blob: e81b90c3cac799796a9d157425f8d3f56c5fd007 [file] [log] [blame]
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/dma-contiguous.h>
14#include <linux/dma-mapping.h>
15#include <linux/dma-mapping-fast.h>
16#include <linux/io-pgtable-fast.h>
Patrick Daly7bcb5462016-08-03 17:27:36 -070017#include <linux/vmalloc.h>
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070018#include <asm/cacheflush.h>
19#include <asm/dma-iommu.h>
20
21
22/* some redundant definitions... :( TODO: move to io-pgtable-fast.h */
23#define FAST_PAGE_SHIFT 12
24#define FAST_PAGE_SIZE (1UL << FAST_PAGE_SHIFT)
25#define FAST_PAGE_MASK (~(PAGE_SIZE - 1))
26#define FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000)
27
28/*
29 * Checks if the allocated range (ending at @end) covered the upcoming
30 * stale bit. We don't need to know exactly where the range starts since
31 * we already know where the candidate search range started. If, starting
32 * from the beginning of the candidate search range, we had to step over
33 * (or landed directly on top of) the upcoming stale bit, then we return
34 * true.
35 *
36 * Due to wrapping, there are two scenarios we'll need to check: (1) if the
37 * range [search_start, upcoming_stale] spans 0 (i.e. search_start >
38 * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale]
39 * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each
40 * of those two scenarios we need to handle three cases: (1) the bit was
41 * found before wrapping or
42 */
43static bool __bit_covered_stale(unsigned long upcoming_stale,
44 unsigned long search_start,
45 unsigned long end)
46{
47 if (search_start > upcoming_stale) {
48 if (end >= search_start) {
49 /*
50 * We started searching above upcoming_stale and we
51 * didn't wrap, so we couldn't have crossed
52 * upcoming_stale.
53 */
54 return false;
55 }
56 /*
57 * We wrapped. Did we cross (or land on top of)
58 * upcoming_stale?
59 */
60 return end >= upcoming_stale;
61 }
62
63 if (search_start <= upcoming_stale) {
64 if (end >= search_start) {
65 /*
66 * We didn't wrap. Did we cross (or land on top
67 * of) upcoming_stale?
68 */
69 return end >= upcoming_stale;
70 }
71 /*
72 * We wrapped. So we must have crossed upcoming_stale
73 * (since we started searching below it).
74 */
75 return true;
76 }
77
78 /* we should have covered all logical combinations... */
79 WARN_ON(1);
80 return true;
81}
82
83static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping,
Mitchel Humpherys5c704e02015-12-21 15:06:34 -080084 unsigned long attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070085 size_t size)
86{
87 unsigned long bit, prev_search_start, nbits = size >> FAST_PAGE_SHIFT;
88 unsigned long align = (1 << get_order(size)) - 1;
89
90 bit = bitmap_find_next_zero_area(
91 mapping->bitmap, mapping->num_4k_pages, mapping->next_start,
92 nbits, align);
93 if (unlikely(bit > mapping->num_4k_pages)) {
94 /* try wrapping */
95 mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */
96 bit = bitmap_find_next_zero_area(
97 mapping->bitmap, mapping->num_4k_pages, 0, nbits,
98 align);
99 if (unlikely(bit > mapping->num_4k_pages))
100 return DMA_ERROR_CODE;
101 }
102
103 bitmap_set(mapping->bitmap, bit, nbits);
104 prev_search_start = mapping->next_start;
105 mapping->next_start = bit + nbits;
106 if (unlikely(mapping->next_start >= mapping->num_4k_pages))
107 mapping->next_start = 0;
108
109 /*
110 * If we just re-allocated a VA whose TLB hasn't been invalidated
111 * since it was last used and unmapped, we need to invalidate it
112 * here. We actually invalidate the entire TLB so that we don't
113 * have to invalidate the TLB again until we wrap back around.
114 */
115 if (mapping->have_stale_tlbs &&
116 __bit_covered_stale(mapping->upcoming_stale_bit,
117 prev_search_start,
118 bit + nbits - 1)) {
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800119 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
120
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700121 iommu_tlbiall(mapping->domain);
122 mapping->have_stale_tlbs = false;
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800123 av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700124 }
125
126 return (bit << FAST_PAGE_SHIFT) + mapping->base;
127}
128
129/*
130 * Checks whether the candidate bit will be allocated sooner than the
131 * current upcoming stale bit. We can say candidate will be upcoming
132 * sooner than the current upcoming stale bit if it lies between the
133 * starting bit of the next search range and the upcoming stale bit
134 * (allowing for wrap-around).
135 *
136 * Stated differently, we're checking the relative ordering of three
137 * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations,
138 * namely:
139 *
140 * 0 |---A---B---C---| TOP (Case 1)
141 * 0 |---A---C---B---| TOP (Case 2)
142 * 0 |---B---A---C---| TOP (Case 3)
143 * 0 |---B---C---A---| TOP (Case 4)
144 * 0 |---C---A---B---| TOP (Case 5)
145 * 0 |---C---B---A---| TOP (Case 6)
146 *
147 * Note that since we're allowing numbers to wrap, the following three
148 * scenarios are all equivalent for Case 1:
149 *
150 * 0 |---A---B---C---| TOP
151 * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.)
152 * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.)
153 *
154 * In any of these cases, if we start searching from A, we will find B
155 * before we find C.
156 *
157 * We can also find two equivalent cases for Case 2:
158 *
159 * 0 |---A---C---B---| TOP
160 * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.)
161 * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.)
162 *
163 * In any of these cases, if we start searching from A, we will find C
164 * before we find B.
165 */
166static bool __bit_is_sooner(unsigned long candidate,
167 struct dma_fast_smmu_mapping *mapping)
168{
169 unsigned long A = mapping->next_start;
170 unsigned long B = candidate;
171 unsigned long C = mapping->upcoming_stale_bit;
172
173 if ((A < B && B < C) || /* Case 1 */
174 (C < A && A < B) || /* Case 5 */
175 (B < C && C < A)) /* Case 4 */
176 return true;
177
178 if ((A < C && C < B) || /* Case 2 */
179 (B < A && A < C) || /* Case 3 */
180 (C < B && B < A)) /* Case 6 */
181 return false;
182
183 /*
184 * For simplicity, we've been ignoring the possibility of any of
185 * our three numbers being equal. Handle those cases here (they
186 * shouldn't happen very often, (I think?)).
187 */
188
189 /*
190 * If candidate is the next bit to be searched then it's definitely
191 * sooner.
192 */
193 if (A == B)
194 return true;
195
196 /*
197 * If candidate is the next upcoming stale bit we'll return false
198 * to avoid doing `upcoming = candidate' in the caller (which would
199 * be useless since they're already equal)
200 */
201 if (B == C)
202 return false;
203
204 /*
205 * If next start is the upcoming stale bit then candidate can't
206 * possibly be sooner. The "soonest" bit is already selected.
207 */
208 if (A == C)
209 return false;
210
211 /* We should have covered all logical combinations. */
212 WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C);
213 return true;
214}
215
216static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping,
217 dma_addr_t iova, size_t size)
218{
219 unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT;
220 unsigned long nbits = size >> FAST_PAGE_SHIFT;
221
222 /*
223 * We don't invalidate TLBs on unmap. We invalidate TLBs on map
224 * when we're about to re-allocate a VA that was previously
225 * unmapped but hasn't yet been invalidated. So we need to keep
226 * track of which bit is the closest to being re-allocated here.
227 */
228 if (__bit_is_sooner(start_bit, mapping))
229 mapping->upcoming_stale_bit = start_bit;
230
231 bitmap_clear(mapping->bitmap, start_bit, nbits);
232 mapping->have_stale_tlbs = true;
233}
234
235
236static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off,
237 size_t size, enum dma_data_direction dir)
238{
239 __dma_map_area(page_address(page) + off, size, dir);
240}
241
242static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off,
243 size_t size, enum dma_data_direction dir)
244{
245 __dma_unmap_area(page_address(page) + off, size, dir);
246
247 /* TODO: WHAT IS THIS? */
248 /*
249 * Mark the D-cache clean for this page to avoid extra flushing.
250 */
251 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
252 set_bit(PG_dcache_clean, &page->flags);
253}
254
255static int __fast_dma_direction_to_prot(enum dma_data_direction dir)
256{
257 switch (dir) {
258 case DMA_BIDIRECTIONAL:
259 return IOMMU_READ | IOMMU_WRITE;
260 case DMA_TO_DEVICE:
261 return IOMMU_READ;
262 case DMA_FROM_DEVICE:
263 return IOMMU_WRITE;
264 default:
265 return 0;
266 }
267}
268
269static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page,
270 unsigned long offset, size_t size,
271 enum dma_data_direction dir,
272 unsigned long attrs)
273{
274 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
275 dma_addr_t iova;
276 unsigned long flags;
277 av8l_fast_iopte *pmd;
278 phys_addr_t phys_plus_off = page_to_phys(page) + offset;
279 phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE);
280 unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK;
281 size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE);
282 int nptes = len >> FAST_PAGE_SHIFT;
283 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
284 int prot = __fast_dma_direction_to_prot(dir);
285
286 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
287 prot |= IOMMU_MMIO;
288
289 if (!skip_sync)
290 __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map),
291 offset_from_phys_to_map, size, dir);
292
293 spin_lock_irqsave(&mapping->lock, flags);
294
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800295 iova = __fast_smmu_alloc_iova(mapping, attrs, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700296
297 if (unlikely(iova == DMA_ERROR_CODE))
298 goto fail;
299
300 pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
301
302 if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot)))
303 goto fail_free_iova;
304
305 if (!skip_sync) /* TODO: should ask SMMU if coherent */
306 dmac_clean_range(pmd, pmd + nptes);
307
308 spin_unlock_irqrestore(&mapping->lock, flags);
309 return iova + offset_from_phys_to_map;
310
311fail_free_iova:
312 __fast_smmu_free_iova(mapping, iova, size);
313fail:
314 spin_unlock_irqrestore(&mapping->lock, flags);
315 return DMA_ERROR_CODE;
316}
317
318static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova,
319 size_t size, enum dma_data_direction dir,
320 unsigned long attrs)
321{
322 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
323 unsigned long flags;
324 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
325 unsigned long offset = iova & ~FAST_PAGE_MASK;
326 size_t len = ALIGN(size + offset, FAST_PAGE_SIZE);
327 int nptes = len >> FAST_PAGE_SHIFT;
328 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
329 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
330
331 if (!skip_sync)
332 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
333
334 spin_lock_irqsave(&mapping->lock, flags);
335 av8l_fast_unmap_public(pmd, len);
336 if (!skip_sync) /* TODO: should ask SMMU if coherent */
337 dmac_clean_range(pmd, pmd + nptes);
338 __fast_smmu_free_iova(mapping, iova, len);
339 spin_unlock_irqrestore(&mapping->lock, flags);
340}
341
342static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
343 int nents, enum dma_data_direction dir,
344 unsigned long attrs)
345{
346 return -EINVAL;
347}
348
349static void fast_smmu_unmap_sg(struct device *dev,
350 struct scatterlist *sg, int nents,
351 enum dma_data_direction dir,
352 unsigned long attrs)
353{
354 WARN_ON_ONCE(1);
355}
356
357static void __fast_smmu_free_pages(struct page **pages, int count)
358{
359 int i;
360
361 for (i = 0; i < count; i++)
362 __free_page(pages[i]);
363 kvfree(pages);
364}
365
366static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
367{
368 struct page **pages;
369 unsigned int i = 0, array_size = count * sizeof(*pages);
370
371 if (array_size <= PAGE_SIZE)
372 pages = kzalloc(array_size, GFP_KERNEL);
373 else
374 pages = vzalloc(array_size);
375 if (!pages)
376 return NULL;
377
378 /* IOMMU can map any pages, so himem can also be used here */
379 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
380
381 for (i = 0; i < count; ++i) {
382 struct page *page = alloc_page(gfp);
383
384 if (!page) {
385 __fast_smmu_free_pages(pages, i);
386 return NULL;
387 }
388 pages[i] = page;
389 }
390 return pages;
391}
392
393static void *fast_smmu_alloc(struct device *dev, size_t size,
394 dma_addr_t *handle, gfp_t gfp,
395 unsigned long attrs)
396{
397 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
398 struct sg_table sgt;
399 dma_addr_t dma_addr, iova_iter;
400 void *addr;
401 av8l_fast_iopte *ptep;
402 unsigned long flags;
403 struct sg_mapping_iter miter;
404 unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
405 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
406 pgprot_t remap_prot = pgprot_writecombine(PAGE_KERNEL);
407 struct page **pages;
408
409 *handle = DMA_ERROR_CODE;
410
411 pages = __fast_smmu_alloc_pages(count, gfp);
412 if (!pages) {
413 dev_err(dev, "no pages\n");
414 return NULL;
415 }
416
417 size = ALIGN(size, SZ_4K);
418 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
419 dev_err(dev, "no sg tablen\n");
420 goto out_free_pages;
421 }
422
423 if (!(prot & IOMMU_CACHE)) {
424 /*
425 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
426 * sufficient here, so skip it by using the "wrong" direction.
427 */
428 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
429 SG_MITER_FROM_SG);
430 while (sg_miter_next(&miter))
431 __dma_flush_range(miter.addr,
432 miter.addr + miter.length);
433 sg_miter_stop(&miter);
434 }
435
436 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800437 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700438 if (dma_addr == DMA_ERROR_CODE) {
439 dev_err(dev, "no iova\n");
440 spin_unlock_irqrestore(&mapping->lock, flags);
441 goto out_free_sg;
442 }
443 iova_iter = dma_addr;
444 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
445 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
446 while (sg_miter_next(&miter)) {
447 int nptes = miter.length >> FAST_PAGE_SHIFT;
448
449 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
450 if (unlikely(av8l_fast_map_public(
451 ptep, page_to_phys(miter.page),
452 miter.length, prot))) {
453 dev_err(dev, "no map public\n");
454 /* TODO: unwind previously successful mappings */
455 goto out_free_iova;
456 }
457 dmac_clean_range(ptep, ptep + nptes);
458 iova_iter += miter.length;
459 }
460 sg_miter_stop(&miter);
461 spin_unlock_irqrestore(&mapping->lock, flags);
462
463 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
464 __builtin_return_address(0));
465 if (!addr) {
466 dev_err(dev, "no common pages\n");
467 goto out_unmap;
468 }
469
470 *handle = dma_addr;
471 sg_free_table(&sgt);
472 return addr;
473
474out_unmap:
475 /* need to take the lock again for page tables and iova */
476 spin_lock_irqsave(&mapping->lock, flags);
477 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
478 av8l_fast_unmap_public(ptep, size);
479 dmac_clean_range(ptep, ptep + count);
480out_free_iova:
481 __fast_smmu_free_iova(mapping, dma_addr, size);
482 spin_unlock_irqrestore(&mapping->lock, flags);
483out_free_sg:
484 sg_free_table(&sgt);
485out_free_pages:
486 __fast_smmu_free_pages(pages, count);
487 return NULL;
488}
489
490static void fast_smmu_free(struct device *dev, size_t size,
491 void *vaddr, dma_addr_t dma_handle,
492 unsigned long attrs)
493{
494 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
495 struct vm_struct *area;
496 struct page **pages;
497 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
498 av8l_fast_iopte *ptep;
499 unsigned long flags;
500
501 size = ALIGN(size, SZ_4K);
502
503 area = find_vm_area(vaddr);
504 if (WARN_ON_ONCE(!area))
505 return;
506
507 pages = area->pages;
508 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
509 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
510 spin_lock_irqsave(&mapping->lock, flags);
511 av8l_fast_unmap_public(ptep, size);
512 dmac_clean_range(ptep, ptep + count);
513 __fast_smmu_free_iova(mapping, dma_handle, size);
514 spin_unlock_irqrestore(&mapping->lock, flags);
515 __fast_smmu_free_pages(pages, count);
516}
517
Patrick Daly7bcb5462016-08-03 17:27:36 -0700518static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
519 void *cpu_addr, dma_addr_t dma_addr,
520 size_t size, unsigned long attrs)
521{
522 struct vm_struct *area;
523 unsigned long uaddr = vma->vm_start;
524 struct page **pages;
525 int i, nr_pages, ret = 0;
526
527 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
528
529 area = find_vm_area(cpu_addr);
530 if (!area)
531 return -EINVAL;
532
533 pages = area->pages;
534 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
535 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
536 ret = vm_insert_page(vma, uaddr, pages[i]);
537 if (ret)
538 break;
539 uaddr += PAGE_SIZE;
540 }
541
542 return ret;
543}
544
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700545static int fast_smmu_dma_supported(struct device *dev, u64 mask)
546{
547 return mask <= 0xffffffff;
548}
549
550static int fast_smmu_mapping_error(struct device *dev,
551 dma_addr_t dma_addr)
552{
553 return dma_addr == DMA_ERROR_CODE;
554}
555
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800556static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
557 void *data)
558{
559 av8l_fast_iopte *ptep = data;
560 dma_addr_t iova;
561 unsigned long bitmap_idx;
562
563 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
564 iova = bitmap_idx << FAST_PAGE_SHIFT;
565 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
566 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
567 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
568 fast->pgtbl_pmds, ptep - fast->pgtbl_pmds);
569 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
570 32, 8, fast->bitmap, fast->bitmap_size, false);
571}
572
573static int fast_smmu_notify(struct notifier_block *self,
574 unsigned long action, void *data)
575{
576 struct dma_fast_smmu_mapping *fast = container_of(
577 self, struct dma_fast_smmu_mapping, notifier);
578
579 switch (action) {
580 case MAPPED_OVER_STALE_TLB:
581 __fast_smmu_mapped_over_stale(fast, data);
582 return NOTIFY_OK;
583 default:
584 WARN(1, "Unhandled notifier action");
585 return NOTIFY_DONE;
586 }
587}
588
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700589static const struct dma_map_ops fast_smmu_dma_ops = {
590 .alloc = fast_smmu_alloc,
591 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700592 .mmap = fast_smmu_mmap_attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700593 .map_page = fast_smmu_map_page,
594 .unmap_page = fast_smmu_unmap_page,
595 .map_sg = fast_smmu_map_sg,
596 .unmap_sg = fast_smmu_unmap_sg,
597 .dma_supported = fast_smmu_dma_supported,
598 .mapping_error = fast_smmu_mapping_error,
599};
600
601/**
602 * __fast_smmu_create_mapping_sized
603 * @base: bottom of the VA range
604 * @size: size of the VA range in bytes
605 *
606 * Creates a mapping structure which holds information about used/unused IO
607 * address ranges, which is required to perform mapping with IOMMU aware
608 * functions. The only VA range supported is [0, 4GB).
609 *
610 * The client device need to be attached to the mapping with
611 * fast_smmu_attach_device function.
612 */
613static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
614 dma_addr_t base, size_t size)
615{
616 struct dma_fast_smmu_mapping *fast;
617
618 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
619 if (!fast)
620 goto err;
621
622 fast->base = base;
623 fast->size = size;
624 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
625 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
626
627 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL);
628 if (!fast->bitmap)
629 goto err2;
630
631 spin_lock_init(&fast->lock);
632
633 return fast;
634err2:
635 kfree(fast);
636err:
637 return ERR_PTR(-ENOMEM);
638}
639
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700640/**
641 * fast_smmu_attach_device
642 * @dev: valid struct device pointer
643 * @mapping: io address space mapping structure (returned from
644 * fast_smmu_create_mapping)
645 *
646 * Attaches specified io address space mapping to the provided device,
647 * this replaces the dma operations (dma_map_ops pointer) with the
648 * IOMMU aware version. More than one client might be attached to
649 * the same io address space mapping.
650 */
651int fast_smmu_attach_device(struct device *dev,
652 struct dma_iommu_mapping *mapping)
653{
654 int atomic_domain = 1;
655 struct iommu_domain *domain = mapping->domain;
656 struct iommu_pgtbl_info info;
657 size_t size = mapping->bits << PAGE_SHIFT;
658
659 if (mapping->base + size > (SZ_1G * 4ULL))
660 return -EINVAL;
661
662 if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC,
663 &atomic_domain))
664 return -EINVAL;
665
666 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
667 if (IS_ERR(mapping->fast))
668 return -ENOMEM;
669 mapping->fast->domain = domain;
670 mapping->fast->dev = dev;
671
672 if (iommu_attach_device(domain, dev))
673 return -EINVAL;
674
675 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
676 &info)) {
677 dev_err(dev, "Couldn't get page table info\n");
678 fast_smmu_detach_device(dev, mapping);
679 return -EINVAL;
680 }
681 mapping->fast->pgtbl_pmds = info.pmds;
682
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800683 mapping->fast->notifier.notifier_call = fast_smmu_notify;
684 av8l_register_notify(&mapping->fast->notifier);
685
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700686 dev->archdata.mapping = mapping;
687 set_dma_ops(dev, &fast_smmu_dma_ops);
688
689 return 0;
690}
691EXPORT_SYMBOL(fast_smmu_attach_device);
692
693/**
694 * fast_smmu_detach_device
695 * @dev: valid struct device pointer
696 *
697 * Detaches the provided device from a previously attached map.
698 * This voids the dma operations (dma_map_ops pointer)
699 */
700void fast_smmu_detach_device(struct device *dev,
701 struct dma_iommu_mapping *mapping)
702{
703 iommu_detach_device(mapping->domain, dev);
704 dev->archdata.mapping = NULL;
705 set_dma_ops(dev, NULL);
706
707 kfree(mapping->fast->bitmap);
708 kfree(mapping->fast);
709}
710EXPORT_SYMBOL(fast_smmu_detach_device);