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