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