blob: f7ed246f934f92c48e6a87bdb95f092c40cf5da8 [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
Liam Mark78d7fb52016-12-01 13:05:31 -0800342static void fast_smmu_sync_single_for_cpu(struct device *dev,
343 dma_addr_t iova, size_t size, enum dma_data_direction dir)
344{
345 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
346 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
347 unsigned long offset = iova & ~FAST_PAGE_MASK;
348 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
349
350 if (!is_device_dma_coherent(dev))
351 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
352}
353
354static void fast_smmu_sync_single_for_device(struct device *dev,
355 dma_addr_t iova, size_t size, enum dma_data_direction dir)
356{
357 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
358 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
359 unsigned long offset = iova & ~FAST_PAGE_MASK;
360 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
361
362 if (!is_device_dma_coherent(dev))
363 __fast_dma_page_cpu_to_dev(page, offset, size, dir);
364}
365
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700366static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
367 int nents, enum dma_data_direction dir,
368 unsigned long attrs)
369{
370 return -EINVAL;
371}
372
373static void fast_smmu_unmap_sg(struct device *dev,
374 struct scatterlist *sg, int nents,
375 enum dma_data_direction dir,
376 unsigned long attrs)
377{
378 WARN_ON_ONCE(1);
379}
380
Liam Mark78d7fb52016-12-01 13:05:31 -0800381static void fast_smmu_sync_sg_for_cpu(struct device *dev,
382 struct scatterlist *sg, int nents, enum dma_data_direction dir)
383{
384 WARN_ON_ONCE(1);
385}
386
387static void fast_smmu_sync_sg_for_device(struct device *dev,
388 struct scatterlist *sg, int nents, enum dma_data_direction dir)
389{
390 WARN_ON_ONCE(1);
391}
392
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700393static void __fast_smmu_free_pages(struct page **pages, int count)
394{
395 int i;
396
397 for (i = 0; i < count; i++)
398 __free_page(pages[i]);
399 kvfree(pages);
400}
401
402static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
403{
404 struct page **pages;
405 unsigned int i = 0, array_size = count * sizeof(*pages);
406
407 if (array_size <= PAGE_SIZE)
408 pages = kzalloc(array_size, GFP_KERNEL);
409 else
410 pages = vzalloc(array_size);
411 if (!pages)
412 return NULL;
413
414 /* IOMMU can map any pages, so himem can also be used here */
415 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
416
417 for (i = 0; i < count; ++i) {
418 struct page *page = alloc_page(gfp);
419
420 if (!page) {
421 __fast_smmu_free_pages(pages, i);
422 return NULL;
423 }
424 pages[i] = page;
425 }
426 return pages;
427}
428
429static void *fast_smmu_alloc(struct device *dev, size_t size,
430 dma_addr_t *handle, gfp_t gfp,
431 unsigned long attrs)
432{
433 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
434 struct sg_table sgt;
435 dma_addr_t dma_addr, iova_iter;
436 void *addr;
437 av8l_fast_iopte *ptep;
438 unsigned long flags;
439 struct sg_mapping_iter miter;
440 unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
441 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
442 pgprot_t remap_prot = pgprot_writecombine(PAGE_KERNEL);
443 struct page **pages;
444
445 *handle = DMA_ERROR_CODE;
446
447 pages = __fast_smmu_alloc_pages(count, gfp);
448 if (!pages) {
449 dev_err(dev, "no pages\n");
450 return NULL;
451 }
452
453 size = ALIGN(size, SZ_4K);
454 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
455 dev_err(dev, "no sg tablen\n");
456 goto out_free_pages;
457 }
458
459 if (!(prot & IOMMU_CACHE)) {
460 /*
461 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
462 * sufficient here, so skip it by using the "wrong" direction.
463 */
464 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
465 SG_MITER_FROM_SG);
466 while (sg_miter_next(&miter))
Kyle Yan65be4a52016-10-31 15:05:00 -0700467 __dma_flush_area(miter.addr, miter.length);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700468 sg_miter_stop(&miter);
469 }
470
471 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800472 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700473 if (dma_addr == DMA_ERROR_CODE) {
474 dev_err(dev, "no iova\n");
475 spin_unlock_irqrestore(&mapping->lock, flags);
476 goto out_free_sg;
477 }
478 iova_iter = dma_addr;
479 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
480 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
481 while (sg_miter_next(&miter)) {
482 int nptes = miter.length >> FAST_PAGE_SHIFT;
483
484 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
485 if (unlikely(av8l_fast_map_public(
486 ptep, page_to_phys(miter.page),
487 miter.length, prot))) {
488 dev_err(dev, "no map public\n");
489 /* TODO: unwind previously successful mappings */
490 goto out_free_iova;
491 }
492 dmac_clean_range(ptep, ptep + nptes);
493 iova_iter += miter.length;
494 }
495 sg_miter_stop(&miter);
496 spin_unlock_irqrestore(&mapping->lock, flags);
497
498 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
499 __builtin_return_address(0));
500 if (!addr) {
501 dev_err(dev, "no common pages\n");
502 goto out_unmap;
503 }
504
505 *handle = dma_addr;
506 sg_free_table(&sgt);
507 return addr;
508
509out_unmap:
510 /* need to take the lock again for page tables and iova */
511 spin_lock_irqsave(&mapping->lock, flags);
512 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
513 av8l_fast_unmap_public(ptep, size);
514 dmac_clean_range(ptep, ptep + count);
515out_free_iova:
516 __fast_smmu_free_iova(mapping, dma_addr, size);
517 spin_unlock_irqrestore(&mapping->lock, flags);
518out_free_sg:
519 sg_free_table(&sgt);
520out_free_pages:
521 __fast_smmu_free_pages(pages, count);
522 return NULL;
523}
524
525static void fast_smmu_free(struct device *dev, size_t size,
526 void *vaddr, dma_addr_t dma_handle,
527 unsigned long attrs)
528{
529 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
530 struct vm_struct *area;
531 struct page **pages;
532 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
533 av8l_fast_iopte *ptep;
534 unsigned long flags;
535
536 size = ALIGN(size, SZ_4K);
537
538 area = find_vm_area(vaddr);
539 if (WARN_ON_ONCE(!area))
540 return;
541
542 pages = area->pages;
543 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
544 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
545 spin_lock_irqsave(&mapping->lock, flags);
546 av8l_fast_unmap_public(ptep, size);
547 dmac_clean_range(ptep, ptep + count);
548 __fast_smmu_free_iova(mapping, dma_handle, size);
549 spin_unlock_irqrestore(&mapping->lock, flags);
550 __fast_smmu_free_pages(pages, count);
551}
552
Patrick Daly7bcb5462016-08-03 17:27:36 -0700553static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
554 void *cpu_addr, dma_addr_t dma_addr,
555 size_t size, unsigned long attrs)
556{
557 struct vm_struct *area;
558 unsigned long uaddr = vma->vm_start;
559 struct page **pages;
560 int i, nr_pages, ret = 0;
561
562 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
563
564 area = find_vm_area(cpu_addr);
565 if (!area)
566 return -EINVAL;
567
568 pages = area->pages;
569 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
570 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
571 ret = vm_insert_page(vma, uaddr, pages[i]);
572 if (ret)
573 break;
574 uaddr += PAGE_SIZE;
575 }
576
577 return ret;
578}
579
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700580static int fast_smmu_dma_supported(struct device *dev, u64 mask)
581{
582 return mask <= 0xffffffff;
583}
584
585static int fast_smmu_mapping_error(struct device *dev,
586 dma_addr_t dma_addr)
587{
588 return dma_addr == DMA_ERROR_CODE;
589}
590
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800591static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
592 void *data)
593{
594 av8l_fast_iopte *ptep = data;
595 dma_addr_t iova;
596 unsigned long bitmap_idx;
597
598 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
599 iova = bitmap_idx << FAST_PAGE_SHIFT;
600 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
601 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
602 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
603 fast->pgtbl_pmds, ptep - fast->pgtbl_pmds);
604 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
605 32, 8, fast->bitmap, fast->bitmap_size, false);
606}
607
608static int fast_smmu_notify(struct notifier_block *self,
609 unsigned long action, void *data)
610{
611 struct dma_fast_smmu_mapping *fast = container_of(
612 self, struct dma_fast_smmu_mapping, notifier);
613
614 switch (action) {
615 case MAPPED_OVER_STALE_TLB:
616 __fast_smmu_mapped_over_stale(fast, data);
617 return NOTIFY_OK;
618 default:
619 WARN(1, "Unhandled notifier action");
620 return NOTIFY_DONE;
621 }
622}
623
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700624static const struct dma_map_ops fast_smmu_dma_ops = {
625 .alloc = fast_smmu_alloc,
626 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700627 .mmap = fast_smmu_mmap_attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700628 .map_page = fast_smmu_map_page,
629 .unmap_page = fast_smmu_unmap_page,
Liam Mark78d7fb52016-12-01 13:05:31 -0800630 .sync_single_for_cpu = fast_smmu_sync_single_for_cpu,
631 .sync_single_for_device = fast_smmu_sync_single_for_device,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700632 .map_sg = fast_smmu_map_sg,
633 .unmap_sg = fast_smmu_unmap_sg,
Liam Mark78d7fb52016-12-01 13:05:31 -0800634 .sync_sg_for_cpu = fast_smmu_sync_sg_for_cpu,
635 .sync_sg_for_device = fast_smmu_sync_sg_for_device,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700636 .dma_supported = fast_smmu_dma_supported,
637 .mapping_error = fast_smmu_mapping_error,
638};
639
640/**
641 * __fast_smmu_create_mapping_sized
642 * @base: bottom of the VA range
643 * @size: size of the VA range in bytes
644 *
645 * Creates a mapping structure which holds information about used/unused IO
646 * address ranges, which is required to perform mapping with IOMMU aware
647 * functions. The only VA range supported is [0, 4GB).
648 *
649 * The client device need to be attached to the mapping with
650 * fast_smmu_attach_device function.
651 */
652static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
653 dma_addr_t base, size_t size)
654{
655 struct dma_fast_smmu_mapping *fast;
656
657 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
658 if (!fast)
659 goto err;
660
661 fast->base = base;
662 fast->size = size;
663 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
664 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
665
666 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL);
667 if (!fast->bitmap)
668 goto err2;
669
670 spin_lock_init(&fast->lock);
671
672 return fast;
673err2:
674 kfree(fast);
675err:
676 return ERR_PTR(-ENOMEM);
677}
678
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700679/**
680 * fast_smmu_attach_device
681 * @dev: valid struct device pointer
682 * @mapping: io address space mapping structure (returned from
683 * fast_smmu_create_mapping)
684 *
685 * Attaches specified io address space mapping to the provided device,
686 * this replaces the dma operations (dma_map_ops pointer) with the
687 * IOMMU aware version. More than one client might be attached to
688 * the same io address space mapping.
689 */
690int fast_smmu_attach_device(struct device *dev,
691 struct dma_iommu_mapping *mapping)
692{
693 int atomic_domain = 1;
694 struct iommu_domain *domain = mapping->domain;
695 struct iommu_pgtbl_info info;
696 size_t size = mapping->bits << PAGE_SHIFT;
697
698 if (mapping->base + size > (SZ_1G * 4ULL))
699 return -EINVAL;
700
701 if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC,
702 &atomic_domain))
703 return -EINVAL;
704
705 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
706 if (IS_ERR(mapping->fast))
707 return -ENOMEM;
708 mapping->fast->domain = domain;
709 mapping->fast->dev = dev;
710
711 if (iommu_attach_device(domain, dev))
712 return -EINVAL;
713
714 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
715 &info)) {
716 dev_err(dev, "Couldn't get page table info\n");
717 fast_smmu_detach_device(dev, mapping);
718 return -EINVAL;
719 }
720 mapping->fast->pgtbl_pmds = info.pmds;
721
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800722 mapping->fast->notifier.notifier_call = fast_smmu_notify;
723 av8l_register_notify(&mapping->fast->notifier);
724
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700725 dev->archdata.mapping = mapping;
726 set_dma_ops(dev, &fast_smmu_dma_ops);
727
728 return 0;
729}
730EXPORT_SYMBOL(fast_smmu_attach_device);
731
732/**
733 * fast_smmu_detach_device
734 * @dev: valid struct device pointer
735 *
736 * Detaches the provided device from a previously attached map.
737 * This voids the dma operations (dma_map_ops pointer)
738 */
739void fast_smmu_detach_device(struct device *dev,
740 struct dma_iommu_mapping *mapping)
741{
742 iommu_detach_device(mapping->domain, dev);
743 dev->archdata.mapping = NULL;
744 set_dma_ops(dev, NULL);
745
746 kfree(mapping->fast->bitmap);
747 kfree(mapping->fast);
748}
749EXPORT_SYMBOL(fast_smmu_detach_device);