blob: c5cbdb788feb9573bea6461563d173e6c0fc3698 [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))
Kyle Yan65be4a52016-10-31 15:05:00 -0700431 __dma_flush_area(miter.addr, miter.length);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700432 sg_miter_stop(&miter);
433 }
434
435 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800436 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700437 if (dma_addr == DMA_ERROR_CODE) {
438 dev_err(dev, "no iova\n");
439 spin_unlock_irqrestore(&mapping->lock, flags);
440 goto out_free_sg;
441 }
442 iova_iter = dma_addr;
443 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
444 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
445 while (sg_miter_next(&miter)) {
446 int nptes = miter.length >> FAST_PAGE_SHIFT;
447
448 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
449 if (unlikely(av8l_fast_map_public(
450 ptep, page_to_phys(miter.page),
451 miter.length, prot))) {
452 dev_err(dev, "no map public\n");
453 /* TODO: unwind previously successful mappings */
454 goto out_free_iova;
455 }
456 dmac_clean_range(ptep, ptep + nptes);
457 iova_iter += miter.length;
458 }
459 sg_miter_stop(&miter);
460 spin_unlock_irqrestore(&mapping->lock, flags);
461
462 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
463 __builtin_return_address(0));
464 if (!addr) {
465 dev_err(dev, "no common pages\n");
466 goto out_unmap;
467 }
468
469 *handle = dma_addr;
470 sg_free_table(&sgt);
471 return addr;
472
473out_unmap:
474 /* need to take the lock again for page tables and iova */
475 spin_lock_irqsave(&mapping->lock, flags);
476 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
477 av8l_fast_unmap_public(ptep, size);
478 dmac_clean_range(ptep, ptep + count);
479out_free_iova:
480 __fast_smmu_free_iova(mapping, dma_addr, size);
481 spin_unlock_irqrestore(&mapping->lock, flags);
482out_free_sg:
483 sg_free_table(&sgt);
484out_free_pages:
485 __fast_smmu_free_pages(pages, count);
486 return NULL;
487}
488
489static void fast_smmu_free(struct device *dev, size_t size,
490 void *vaddr, dma_addr_t dma_handle,
491 unsigned long attrs)
492{
493 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
494 struct vm_struct *area;
495 struct page **pages;
496 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
497 av8l_fast_iopte *ptep;
498 unsigned long flags;
499
500 size = ALIGN(size, SZ_4K);
501
502 area = find_vm_area(vaddr);
503 if (WARN_ON_ONCE(!area))
504 return;
505
506 pages = area->pages;
507 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
508 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
509 spin_lock_irqsave(&mapping->lock, flags);
510 av8l_fast_unmap_public(ptep, size);
511 dmac_clean_range(ptep, ptep + count);
512 __fast_smmu_free_iova(mapping, dma_handle, size);
513 spin_unlock_irqrestore(&mapping->lock, flags);
514 __fast_smmu_free_pages(pages, count);
515}
516
Patrick Daly7bcb5462016-08-03 17:27:36 -0700517static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
518 void *cpu_addr, dma_addr_t dma_addr,
519 size_t size, unsigned long attrs)
520{
521 struct vm_struct *area;
522 unsigned long uaddr = vma->vm_start;
523 struct page **pages;
524 int i, nr_pages, ret = 0;
525
526 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
527
528 area = find_vm_area(cpu_addr);
529 if (!area)
530 return -EINVAL;
531
532 pages = area->pages;
533 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
534 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
535 ret = vm_insert_page(vma, uaddr, pages[i]);
536 if (ret)
537 break;
538 uaddr += PAGE_SIZE;
539 }
540
541 return ret;
542}
543
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700544static int fast_smmu_dma_supported(struct device *dev, u64 mask)
545{
546 return mask <= 0xffffffff;
547}
548
549static int fast_smmu_mapping_error(struct device *dev,
550 dma_addr_t dma_addr)
551{
552 return dma_addr == DMA_ERROR_CODE;
553}
554
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800555static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
556 void *data)
557{
558 av8l_fast_iopte *ptep = data;
559 dma_addr_t iova;
560 unsigned long bitmap_idx;
561
562 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
563 iova = bitmap_idx << FAST_PAGE_SHIFT;
564 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
565 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
566 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
567 fast->pgtbl_pmds, ptep - fast->pgtbl_pmds);
568 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
569 32, 8, fast->bitmap, fast->bitmap_size, false);
570}
571
572static int fast_smmu_notify(struct notifier_block *self,
573 unsigned long action, void *data)
574{
575 struct dma_fast_smmu_mapping *fast = container_of(
576 self, struct dma_fast_smmu_mapping, notifier);
577
578 switch (action) {
579 case MAPPED_OVER_STALE_TLB:
580 __fast_smmu_mapped_over_stale(fast, data);
581 return NOTIFY_OK;
582 default:
583 WARN(1, "Unhandled notifier action");
584 return NOTIFY_DONE;
585 }
586}
587
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700588static const struct dma_map_ops fast_smmu_dma_ops = {
589 .alloc = fast_smmu_alloc,
590 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700591 .mmap = fast_smmu_mmap_attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700592 .map_page = fast_smmu_map_page,
593 .unmap_page = fast_smmu_unmap_page,
594 .map_sg = fast_smmu_map_sg,
595 .unmap_sg = fast_smmu_unmap_sg,
596 .dma_supported = fast_smmu_dma_supported,
597 .mapping_error = fast_smmu_mapping_error,
598};
599
600/**
601 * __fast_smmu_create_mapping_sized
602 * @base: bottom of the VA range
603 * @size: size of the VA range in bytes
604 *
605 * Creates a mapping structure which holds information about used/unused IO
606 * address ranges, which is required to perform mapping with IOMMU aware
607 * functions. The only VA range supported is [0, 4GB).
608 *
609 * The client device need to be attached to the mapping with
610 * fast_smmu_attach_device function.
611 */
612static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
613 dma_addr_t base, size_t size)
614{
615 struct dma_fast_smmu_mapping *fast;
616
617 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
618 if (!fast)
619 goto err;
620
621 fast->base = base;
622 fast->size = size;
623 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
624 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
625
626 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL);
627 if (!fast->bitmap)
628 goto err2;
629
630 spin_lock_init(&fast->lock);
631
632 return fast;
633err2:
634 kfree(fast);
635err:
636 return ERR_PTR(-ENOMEM);
637}
638
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700639/**
640 * fast_smmu_attach_device
641 * @dev: valid struct device pointer
642 * @mapping: io address space mapping structure (returned from
643 * fast_smmu_create_mapping)
644 *
645 * Attaches specified io address space mapping to the provided device,
646 * this replaces the dma operations (dma_map_ops pointer) with the
647 * IOMMU aware version. More than one client might be attached to
648 * the same io address space mapping.
649 */
650int fast_smmu_attach_device(struct device *dev,
651 struct dma_iommu_mapping *mapping)
652{
653 int atomic_domain = 1;
654 struct iommu_domain *domain = mapping->domain;
655 struct iommu_pgtbl_info info;
656 size_t size = mapping->bits << PAGE_SHIFT;
657
658 if (mapping->base + size > (SZ_1G * 4ULL))
659 return -EINVAL;
660
661 if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC,
662 &atomic_domain))
663 return -EINVAL;
664
665 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
666 if (IS_ERR(mapping->fast))
667 return -ENOMEM;
668 mapping->fast->domain = domain;
669 mapping->fast->dev = dev;
670
671 if (iommu_attach_device(domain, dev))
672 return -EINVAL;
673
674 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
675 &info)) {
676 dev_err(dev, "Couldn't get page table info\n");
677 fast_smmu_detach_device(dev, mapping);
678 return -EINVAL;
679 }
680 mapping->fast->pgtbl_pmds = info.pmds;
681
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800682 mapping->fast->notifier.notifier_call = fast_smmu_notify;
683 av8l_register_notify(&mapping->fast->notifier);
684
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700685 dev->archdata.mapping = mapping;
686 set_dma_ops(dev, &fast_smmu_dma_ops);
687
688 return 0;
689}
690EXPORT_SYMBOL(fast_smmu_attach_device);
691
692/**
693 * fast_smmu_detach_device
694 * @dev: valid struct device pointer
695 *
696 * Detaches the provided device from a previously attached map.
697 * This voids the dma operations (dma_map_ops pointer)
698 */
699void fast_smmu_detach_device(struct device *dev,
700 struct dma_iommu_mapping *mapping)
701{
702 iommu_detach_device(mapping->domain, dev);
703 dev->archdata.mapping = NULL;
704 set_dma_ops(dev, NULL);
705
706 kfree(mapping->fast->bitmap);
707 kfree(mapping->fast);
708}
709EXPORT_SYMBOL(fast_smmu_detach_device);