blob: 560bb434283c54104d3f5af1dd71880326446216 [file] [log] [blame]
Liam Mark4d4fbba2017-02-08 10:30:49 -08001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07002 *
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>
Charan Teja Reddy29f61402017-02-09 20:44:29 +053020#include <linux/slab.h>
21#include <linux/vmalloc.h>
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070022
23/* some redundant definitions... :( TODO: move to io-pgtable-fast.h */
24#define FAST_PAGE_SHIFT 12
25#define FAST_PAGE_SIZE (1UL << FAST_PAGE_SHIFT)
26#define FAST_PAGE_MASK (~(PAGE_SIZE - 1))
27#define FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000)
28
Mitchel Humpherys425d03d2016-06-23 13:25:12 -070029static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
30 bool coherent)
31{
32 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
33 return pgprot_noncached(prot);
34 else if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
35 return pgprot_writecombine(prot);
36 return prot;
37}
38
39static int __get_iommu_pgprot(unsigned long attrs, int prot,
40 bool coherent)
41{
42 if (!(attrs & DMA_ATTR_EXEC_MAPPING))
43 prot |= IOMMU_NOEXEC;
44 if ((attrs & DMA_ATTR_STRONGLY_ORDERED))
45 prot |= IOMMU_MMIO;
46 if (coherent)
47 prot |= IOMMU_CACHE;
48
49 return prot;
50}
51
Mitchel Humpherys9de66db2016-06-07 11:09:44 -070052static void fast_dmac_clean_range(struct dma_fast_smmu_mapping *mapping,
53 void *start, void *end)
54{
55 if (!mapping->is_smmu_pt_coherent)
56 dmac_clean_range(start, end);
57}
58
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070059/*
60 * Checks if the allocated range (ending at @end) covered the upcoming
61 * stale bit. We don't need to know exactly where the range starts since
62 * we already know where the candidate search range started. If, starting
63 * from the beginning of the candidate search range, we had to step over
64 * (or landed directly on top of) the upcoming stale bit, then we return
65 * true.
66 *
67 * Due to wrapping, there are two scenarios we'll need to check: (1) if the
68 * range [search_start, upcoming_stale] spans 0 (i.e. search_start >
69 * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale]
70 * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each
71 * of those two scenarios we need to handle three cases: (1) the bit was
72 * found before wrapping or
73 */
74static bool __bit_covered_stale(unsigned long upcoming_stale,
75 unsigned long search_start,
76 unsigned long end)
77{
78 if (search_start > upcoming_stale) {
79 if (end >= search_start) {
80 /*
81 * We started searching above upcoming_stale and we
82 * didn't wrap, so we couldn't have crossed
83 * upcoming_stale.
84 */
85 return false;
86 }
87 /*
88 * We wrapped. Did we cross (or land on top of)
89 * upcoming_stale?
90 */
91 return end >= upcoming_stale;
92 }
93
94 if (search_start <= upcoming_stale) {
95 if (end >= search_start) {
96 /*
97 * We didn't wrap. Did we cross (or land on top
98 * of) upcoming_stale?
99 */
100 return end >= upcoming_stale;
101 }
102 /*
103 * We wrapped. So we must have crossed upcoming_stale
104 * (since we started searching below it).
105 */
106 return true;
107 }
108
109 /* we should have covered all logical combinations... */
110 WARN_ON(1);
111 return true;
112}
113
114static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping,
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800115 unsigned long attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700116 size_t size)
117{
118 unsigned long bit, prev_search_start, nbits = size >> FAST_PAGE_SHIFT;
119 unsigned long align = (1 << get_order(size)) - 1;
120
121 bit = bitmap_find_next_zero_area(
122 mapping->bitmap, mapping->num_4k_pages, mapping->next_start,
123 nbits, align);
124 if (unlikely(bit > mapping->num_4k_pages)) {
125 /* try wrapping */
126 mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */
127 bit = bitmap_find_next_zero_area(
128 mapping->bitmap, mapping->num_4k_pages, 0, nbits,
129 align);
130 if (unlikely(bit > mapping->num_4k_pages))
131 return DMA_ERROR_CODE;
132 }
133
134 bitmap_set(mapping->bitmap, bit, nbits);
135 prev_search_start = mapping->next_start;
136 mapping->next_start = bit + nbits;
137 if (unlikely(mapping->next_start >= mapping->num_4k_pages))
138 mapping->next_start = 0;
139
140 /*
141 * If we just re-allocated a VA whose TLB hasn't been invalidated
142 * since it was last used and unmapped, we need to invalidate it
143 * here. We actually invalidate the entire TLB so that we don't
144 * have to invalidate the TLB again until we wrap back around.
145 */
146 if (mapping->have_stale_tlbs &&
147 __bit_covered_stale(mapping->upcoming_stale_bit,
148 prev_search_start,
149 bit + nbits - 1)) {
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800150 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
151
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700152 iommu_tlbiall(mapping->domain);
153 mapping->have_stale_tlbs = false;
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800154 av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700155 }
156
157 return (bit << FAST_PAGE_SHIFT) + mapping->base;
158}
159
160/*
161 * Checks whether the candidate bit will be allocated sooner than the
162 * current upcoming stale bit. We can say candidate will be upcoming
163 * sooner than the current upcoming stale bit if it lies between the
164 * starting bit of the next search range and the upcoming stale bit
165 * (allowing for wrap-around).
166 *
167 * Stated differently, we're checking the relative ordering of three
168 * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations,
169 * namely:
170 *
171 * 0 |---A---B---C---| TOP (Case 1)
172 * 0 |---A---C---B---| TOP (Case 2)
173 * 0 |---B---A---C---| TOP (Case 3)
174 * 0 |---B---C---A---| TOP (Case 4)
175 * 0 |---C---A---B---| TOP (Case 5)
176 * 0 |---C---B---A---| TOP (Case 6)
177 *
178 * Note that since we're allowing numbers to wrap, the following three
179 * scenarios are all equivalent for Case 1:
180 *
181 * 0 |---A---B---C---| TOP
182 * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.)
183 * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.)
184 *
185 * In any of these cases, if we start searching from A, we will find B
186 * before we find C.
187 *
188 * We can also find two equivalent cases for Case 2:
189 *
190 * 0 |---A---C---B---| TOP
191 * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.)
192 * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.)
193 *
194 * In any of these cases, if we start searching from A, we will find C
195 * before we find B.
196 */
197static bool __bit_is_sooner(unsigned long candidate,
198 struct dma_fast_smmu_mapping *mapping)
199{
200 unsigned long A = mapping->next_start;
201 unsigned long B = candidate;
202 unsigned long C = mapping->upcoming_stale_bit;
203
204 if ((A < B && B < C) || /* Case 1 */
205 (C < A && A < B) || /* Case 5 */
206 (B < C && C < A)) /* Case 4 */
207 return true;
208
209 if ((A < C && C < B) || /* Case 2 */
210 (B < A && A < C) || /* Case 3 */
211 (C < B && B < A)) /* Case 6 */
212 return false;
213
214 /*
215 * For simplicity, we've been ignoring the possibility of any of
216 * our three numbers being equal. Handle those cases here (they
217 * shouldn't happen very often, (I think?)).
218 */
219
220 /*
221 * If candidate is the next bit to be searched then it's definitely
222 * sooner.
223 */
224 if (A == B)
225 return true;
226
227 /*
228 * If candidate is the next upcoming stale bit we'll return false
229 * to avoid doing `upcoming = candidate' in the caller (which would
230 * be useless since they're already equal)
231 */
232 if (B == C)
233 return false;
234
235 /*
236 * If next start is the upcoming stale bit then candidate can't
237 * possibly be sooner. The "soonest" bit is already selected.
238 */
239 if (A == C)
240 return false;
241
242 /* We should have covered all logical combinations. */
243 WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C);
244 return true;
245}
246
247static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping,
248 dma_addr_t iova, size_t size)
249{
250 unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT;
251 unsigned long nbits = size >> FAST_PAGE_SHIFT;
252
253 /*
254 * We don't invalidate TLBs on unmap. We invalidate TLBs on map
255 * when we're about to re-allocate a VA that was previously
256 * unmapped but hasn't yet been invalidated. So we need to keep
257 * track of which bit is the closest to being re-allocated here.
258 */
259 if (__bit_is_sooner(start_bit, mapping))
260 mapping->upcoming_stale_bit = start_bit;
261
262 bitmap_clear(mapping->bitmap, start_bit, nbits);
263 mapping->have_stale_tlbs = true;
264}
265
266
267static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off,
268 size_t size, enum dma_data_direction dir)
269{
270 __dma_map_area(page_address(page) + off, size, dir);
271}
272
273static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off,
274 size_t size, enum dma_data_direction dir)
275{
276 __dma_unmap_area(page_address(page) + off, size, dir);
277
278 /* TODO: WHAT IS THIS? */
279 /*
280 * Mark the D-cache clean for this page to avoid extra flushing.
281 */
282 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
283 set_bit(PG_dcache_clean, &page->flags);
284}
285
286static int __fast_dma_direction_to_prot(enum dma_data_direction dir)
287{
288 switch (dir) {
289 case DMA_BIDIRECTIONAL:
290 return IOMMU_READ | IOMMU_WRITE;
291 case DMA_TO_DEVICE:
292 return IOMMU_READ;
293 case DMA_FROM_DEVICE:
294 return IOMMU_WRITE;
295 default:
296 return 0;
297 }
298}
299
300static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page,
301 unsigned long offset, size_t size,
302 enum dma_data_direction dir,
303 unsigned long attrs)
304{
305 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
306 dma_addr_t iova;
307 unsigned long flags;
308 av8l_fast_iopte *pmd;
309 phys_addr_t phys_plus_off = page_to_phys(page) + offset;
310 phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE);
311 unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK;
312 size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE);
313 int nptes = len >> FAST_PAGE_SHIFT;
314 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
315 int prot = __fast_dma_direction_to_prot(dir);
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700316 bool is_coherent = is_device_dma_coherent(dev);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700317
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700318 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700319
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700320 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700321 __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map),
322 offset_from_phys_to_map, size, dir);
323
324 spin_lock_irqsave(&mapping->lock, flags);
325
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800326 iova = __fast_smmu_alloc_iova(mapping, attrs, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700327
328 if (unlikely(iova == DMA_ERROR_CODE))
329 goto fail;
330
331 pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
332
333 if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot)))
334 goto fail_free_iova;
335
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700336 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700337
338 spin_unlock_irqrestore(&mapping->lock, flags);
339 return iova + offset_from_phys_to_map;
340
341fail_free_iova:
342 __fast_smmu_free_iova(mapping, iova, size);
343fail:
344 spin_unlock_irqrestore(&mapping->lock, flags);
345 return DMA_ERROR_CODE;
346}
347
348static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova,
349 size_t size, enum dma_data_direction dir,
350 unsigned long attrs)
351{
352 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
353 unsigned long flags;
354 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
355 unsigned long offset = iova & ~FAST_PAGE_MASK;
356 size_t len = ALIGN(size + offset, FAST_PAGE_SIZE);
357 int nptes = len >> FAST_PAGE_SHIFT;
358 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
359 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700360 bool is_coherent = is_device_dma_coherent(dev);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700361
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700362 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700363 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
364
365 spin_lock_irqsave(&mapping->lock, flags);
366 av8l_fast_unmap_public(pmd, len);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700367 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700368 __fast_smmu_free_iova(mapping, iova, len);
369 spin_unlock_irqrestore(&mapping->lock, flags);
370}
371
Liam Mark78d7fb52016-12-01 13:05:31 -0800372static void fast_smmu_sync_single_for_cpu(struct device *dev,
373 dma_addr_t iova, size_t size, enum dma_data_direction dir)
374{
375 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
376 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
377 unsigned long offset = iova & ~FAST_PAGE_MASK;
378 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
379
380 if (!is_device_dma_coherent(dev))
381 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
382}
383
384static void fast_smmu_sync_single_for_device(struct device *dev,
385 dma_addr_t iova, size_t size, enum dma_data_direction dir)
386{
387 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
388 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
389 unsigned long offset = iova & ~FAST_PAGE_MASK;
390 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
391
392 if (!is_device_dma_coherent(dev))
393 __fast_dma_page_cpu_to_dev(page, offset, size, dir);
394}
395
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700396static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
397 int nents, enum dma_data_direction dir,
398 unsigned long attrs)
399{
400 return -EINVAL;
401}
402
403static void fast_smmu_unmap_sg(struct device *dev,
404 struct scatterlist *sg, int nents,
405 enum dma_data_direction dir,
406 unsigned long attrs)
407{
408 WARN_ON_ONCE(1);
409}
410
Liam Mark78d7fb52016-12-01 13:05:31 -0800411static void fast_smmu_sync_sg_for_cpu(struct device *dev,
412 struct scatterlist *sg, int nents, enum dma_data_direction dir)
413{
414 WARN_ON_ONCE(1);
415}
416
417static void fast_smmu_sync_sg_for_device(struct device *dev,
418 struct scatterlist *sg, int nents, enum dma_data_direction dir)
419{
420 WARN_ON_ONCE(1);
421}
422
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700423static void __fast_smmu_free_pages(struct page **pages, int count)
424{
425 int i;
426
427 for (i = 0; i < count; i++)
428 __free_page(pages[i]);
429 kvfree(pages);
430}
431
432static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
433{
434 struct page **pages;
435 unsigned int i = 0, array_size = count * sizeof(*pages);
436
437 if (array_size <= PAGE_SIZE)
438 pages = kzalloc(array_size, GFP_KERNEL);
439 else
440 pages = vzalloc(array_size);
441 if (!pages)
442 return NULL;
443
444 /* IOMMU can map any pages, so himem can also be used here */
445 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
446
447 for (i = 0; i < count; ++i) {
448 struct page *page = alloc_page(gfp);
449
450 if (!page) {
451 __fast_smmu_free_pages(pages, i);
452 return NULL;
453 }
454 pages[i] = page;
455 }
456 return pages;
457}
458
459static void *fast_smmu_alloc(struct device *dev, size_t size,
460 dma_addr_t *handle, gfp_t gfp,
461 unsigned long attrs)
462{
463 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
464 struct sg_table sgt;
465 dma_addr_t dma_addr, iova_iter;
466 void *addr;
467 av8l_fast_iopte *ptep;
468 unsigned long flags;
469 struct sg_mapping_iter miter;
470 unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
471 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700472 bool is_coherent = is_device_dma_coherent(dev);
473 pgprot_t remap_prot = __get_dma_pgprot(attrs, PAGE_KERNEL, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700474 struct page **pages;
475
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700476 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
477
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700478 *handle = DMA_ERROR_CODE;
479
480 pages = __fast_smmu_alloc_pages(count, gfp);
481 if (!pages) {
482 dev_err(dev, "no pages\n");
483 return NULL;
484 }
485
486 size = ALIGN(size, SZ_4K);
487 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
488 dev_err(dev, "no sg tablen\n");
489 goto out_free_pages;
490 }
491
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700492 if (!is_coherent) {
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700493 /*
494 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
495 * sufficient here, so skip it by using the "wrong" direction.
496 */
497 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
498 SG_MITER_FROM_SG);
499 while (sg_miter_next(&miter))
Kyle Yan65be4a52016-10-31 15:05:00 -0700500 __dma_flush_area(miter.addr, miter.length);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700501 sg_miter_stop(&miter);
502 }
503
504 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800505 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700506 if (dma_addr == DMA_ERROR_CODE) {
507 dev_err(dev, "no iova\n");
508 spin_unlock_irqrestore(&mapping->lock, flags);
509 goto out_free_sg;
510 }
511 iova_iter = dma_addr;
512 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
513 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
514 while (sg_miter_next(&miter)) {
515 int nptes = miter.length >> FAST_PAGE_SHIFT;
516
517 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
518 if (unlikely(av8l_fast_map_public(
519 ptep, page_to_phys(miter.page),
520 miter.length, prot))) {
521 dev_err(dev, "no map public\n");
522 /* TODO: unwind previously successful mappings */
523 goto out_free_iova;
524 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700525 fast_dmac_clean_range(mapping, ptep, ptep + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700526 iova_iter += miter.length;
527 }
528 sg_miter_stop(&miter);
529 spin_unlock_irqrestore(&mapping->lock, flags);
530
531 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
532 __builtin_return_address(0));
533 if (!addr) {
534 dev_err(dev, "no common pages\n");
535 goto out_unmap;
536 }
537
538 *handle = dma_addr;
539 sg_free_table(&sgt);
540 return addr;
541
542out_unmap:
543 /* need to take the lock again for page tables and iova */
544 spin_lock_irqsave(&mapping->lock, flags);
545 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
546 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700547 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700548out_free_iova:
549 __fast_smmu_free_iova(mapping, dma_addr, size);
550 spin_unlock_irqrestore(&mapping->lock, flags);
551out_free_sg:
552 sg_free_table(&sgt);
553out_free_pages:
554 __fast_smmu_free_pages(pages, count);
555 return NULL;
556}
557
558static void fast_smmu_free(struct device *dev, size_t size,
559 void *vaddr, dma_addr_t dma_handle,
560 unsigned long attrs)
561{
562 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
563 struct vm_struct *area;
564 struct page **pages;
565 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
566 av8l_fast_iopte *ptep;
567 unsigned long flags;
568
569 size = ALIGN(size, SZ_4K);
570
571 area = find_vm_area(vaddr);
572 if (WARN_ON_ONCE(!area))
573 return;
574
575 pages = area->pages;
576 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
577 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
578 spin_lock_irqsave(&mapping->lock, flags);
579 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700580 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700581 __fast_smmu_free_iova(mapping, dma_handle, size);
582 spin_unlock_irqrestore(&mapping->lock, flags);
583 __fast_smmu_free_pages(pages, count);
584}
585
Patrick Daly7bcb5462016-08-03 17:27:36 -0700586static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
587 void *cpu_addr, dma_addr_t dma_addr,
588 size_t size, unsigned long attrs)
589{
590 struct vm_struct *area;
591 unsigned long uaddr = vma->vm_start;
592 struct page **pages;
593 int i, nr_pages, ret = 0;
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700594 bool coherent = is_device_dma_coherent(dev);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700595
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700596 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
597 coherent);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700598 area = find_vm_area(cpu_addr);
599 if (!area)
600 return -EINVAL;
601
602 pages = area->pages;
603 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
604 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
605 ret = vm_insert_page(vma, uaddr, pages[i]);
606 if (ret)
607 break;
608 uaddr += PAGE_SIZE;
609 }
610
611 return ret;
612}
613
Patrick Daly199fa672017-05-04 15:30:16 -0700614static dma_addr_t fast_smmu_dma_map_resource(
615 struct device *dev, phys_addr_t phys_addr,
616 size_t size, enum dma_data_direction dir,
617 unsigned long attrs)
618{
619 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
620 size_t offset = phys_addr & ~FAST_PAGE_MASK;
621 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
622 dma_addr_t dma_addr;
623 int prot;
624 unsigned long flags;
625
626 spin_lock_irqsave(&mapping->lock, flags);
627 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, len);
628 spin_unlock_irqrestore(&mapping->lock, flags);
629
630 if (dma_addr == DMA_ERROR_CODE)
631 return dma_addr;
632
633 prot = __fast_dma_direction_to_prot(dir);
634 prot |= IOMMU_MMIO;
635
636 if (iommu_map(mapping->domain, dma_addr, phys_addr - offset,
637 len, prot)) {
638 spin_lock_irqsave(&mapping->lock, flags);
639 __fast_smmu_free_iova(mapping, dma_addr, len);
640 spin_unlock_irqrestore(&mapping->lock, flags);
641 return DMA_ERROR_CODE;
642 }
643 return dma_addr + offset;
644}
645
646static void fast_smmu_dma_unmap_resource(
647 struct device *dev, dma_addr_t addr,
648 size_t size, enum dma_data_direction dir,
649 unsigned long attrs)
650{
651 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
652 size_t offset = addr & ~FAST_PAGE_MASK;
653 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
654 unsigned long flags;
655
656 iommu_unmap(mapping->domain, addr - offset, len);
657 spin_lock_irqsave(&mapping->lock, flags);
658 __fast_smmu_free_iova(mapping, addr, len);
659 spin_unlock_irqrestore(&mapping->lock, flags);
660}
661
662
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700663static int fast_smmu_dma_supported(struct device *dev, u64 mask)
664{
665 return mask <= 0xffffffff;
666}
667
668static int fast_smmu_mapping_error(struct device *dev,
669 dma_addr_t dma_addr)
670{
671 return dma_addr == DMA_ERROR_CODE;
672}
673
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800674static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
675 void *data)
676{
677 av8l_fast_iopte *ptep = data;
678 dma_addr_t iova;
679 unsigned long bitmap_idx;
680
681 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
682 iova = bitmap_idx << FAST_PAGE_SHIFT;
683 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
684 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
685 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530686 fast->pgtbl_pmds, bitmap_idx);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800687 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
688 32, 8, fast->bitmap, fast->bitmap_size, false);
689}
690
691static int fast_smmu_notify(struct notifier_block *self,
692 unsigned long action, void *data)
693{
694 struct dma_fast_smmu_mapping *fast = container_of(
695 self, struct dma_fast_smmu_mapping, notifier);
696
697 switch (action) {
698 case MAPPED_OVER_STALE_TLB:
699 __fast_smmu_mapped_over_stale(fast, data);
700 return NOTIFY_OK;
701 default:
702 WARN(1, "Unhandled notifier action");
703 return NOTIFY_DONE;
704 }
705}
706
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700707static const struct dma_map_ops fast_smmu_dma_ops = {
708 .alloc = fast_smmu_alloc,
709 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700710 .mmap = fast_smmu_mmap_attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700711 .map_page = fast_smmu_map_page,
712 .unmap_page = fast_smmu_unmap_page,
Liam Mark78d7fb52016-12-01 13:05:31 -0800713 .sync_single_for_cpu = fast_smmu_sync_single_for_cpu,
714 .sync_single_for_device = fast_smmu_sync_single_for_device,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700715 .map_sg = fast_smmu_map_sg,
716 .unmap_sg = fast_smmu_unmap_sg,
Liam Mark78d7fb52016-12-01 13:05:31 -0800717 .sync_sg_for_cpu = fast_smmu_sync_sg_for_cpu,
718 .sync_sg_for_device = fast_smmu_sync_sg_for_device,
Patrick Daly199fa672017-05-04 15:30:16 -0700719 .map_resource = fast_smmu_dma_map_resource,
720 .unmap_resource = fast_smmu_dma_unmap_resource,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700721 .dma_supported = fast_smmu_dma_supported,
722 .mapping_error = fast_smmu_mapping_error,
723};
724
725/**
726 * __fast_smmu_create_mapping_sized
727 * @base: bottom of the VA range
728 * @size: size of the VA range in bytes
729 *
730 * Creates a mapping structure which holds information about used/unused IO
731 * address ranges, which is required to perform mapping with IOMMU aware
732 * functions. The only VA range supported is [0, 4GB).
733 *
734 * The client device need to be attached to the mapping with
735 * fast_smmu_attach_device function.
736 */
737static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530738 dma_addr_t base, u64 size)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700739{
740 struct dma_fast_smmu_mapping *fast;
741
742 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
743 if (!fast)
744 goto err;
745
746 fast->base = base;
747 fast->size = size;
748 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
749 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
750
Liam Mark4d4fbba2017-02-08 10:30:49 -0800751 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL | __GFP_NOWARN |
752 __GFP_NORETRY);
753 if (!fast->bitmap)
754 fast->bitmap = vzalloc(fast->bitmap_size);
755
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700756 if (!fast->bitmap)
757 goto err2;
758
759 spin_lock_init(&fast->lock);
760
761 return fast;
762err2:
763 kfree(fast);
764err:
765 return ERR_PTR(-ENOMEM);
766}
767
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700768/*
769 * Based off of similar code from dma-iommu.c, but modified to use a different
770 * iova allocator
771 */
772static void fast_smmu_reserve_pci_windows(struct device *dev,
773 struct dma_fast_smmu_mapping *mapping)
774{
775 struct pci_host_bridge *bridge;
776 struct resource_entry *window;
777 phys_addr_t start, end;
778 struct pci_dev *pci_dev;
779 unsigned long flags;
780
781 if (!dev_is_pci(dev))
782 return;
783
784 pci_dev = to_pci_dev(dev);
785 bridge = pci_find_host_bridge(pci_dev->bus);
786
787 spin_lock_irqsave(&mapping->lock, flags);
788 resource_list_for_each_entry(window, &bridge->windows) {
789 if (resource_type(window->res) != IORESOURCE_MEM &&
790 resource_type(window->res) != IORESOURCE_IO)
791 continue;
792
793 start = round_down(window->res->start - window->offset,
794 FAST_PAGE_SIZE);
795 end = round_up(window->res->end - window->offset,
796 FAST_PAGE_SIZE);
797 start = max_t(unsigned long, mapping->base, start);
798 end = min_t(unsigned long, mapping->base + mapping->size, end);
799 if (start >= end)
800 continue;
801
802 dev_dbg(dev, "iova allocator reserved 0x%pa-0x%pa\n",
803 &start, &end);
804
805 start = (start - mapping->base) >> FAST_PAGE_SHIFT;
806 end = (end - mapping->base) >> FAST_PAGE_SHIFT;
807 bitmap_set(mapping->bitmap, start, end - start);
808 }
809 spin_unlock_irqrestore(&mapping->lock, flags);
810}
811
812
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700813/**
814 * fast_smmu_attach_device
815 * @dev: valid struct device pointer
816 * @mapping: io address space mapping structure (returned from
817 * fast_smmu_create_mapping)
818 *
819 * Attaches specified io address space mapping to the provided device,
820 * this replaces the dma operations (dma_map_ops pointer) with the
821 * IOMMU aware version. More than one client might be attached to
822 * the same io address space mapping.
823 */
824int fast_smmu_attach_device(struct device *dev,
825 struct dma_iommu_mapping *mapping)
826{
827 int atomic_domain = 1;
828 struct iommu_domain *domain = mapping->domain;
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700829 struct iommu_group *group;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700830 struct iommu_pgtbl_info info;
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530831 u64 size = (u64)mapping->bits << PAGE_SHIFT;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700832
833 if (mapping->base + size > (SZ_1G * 4ULL))
834 return -EINVAL;
835
836 if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC,
837 &atomic_domain))
838 return -EINVAL;
839
840 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
841 if (IS_ERR(mapping->fast))
842 return -ENOMEM;
843 mapping->fast->domain = domain;
844 mapping->fast->dev = dev;
845
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700846 fast_smmu_reserve_pci_windows(dev, mapping->fast);
847
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700848 group = dev->iommu_group;
849 if (!group) {
850 dev_err(dev, "No iommu associated with device\n");
851 return -ENODEV;
852 }
853
854 if (iommu_get_domain_for_dev(dev)) {
855 dev_err(dev, "Device already attached to other iommu_domain\n");
856 return -EINVAL;
857 }
858
859 if (iommu_attach_group(mapping->domain, group))
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700860 return -EINVAL;
861
862 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
863 &info)) {
864 dev_err(dev, "Couldn't get page table info\n");
865 fast_smmu_detach_device(dev, mapping);
866 return -EINVAL;
867 }
868 mapping->fast->pgtbl_pmds = info.pmds;
869
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700870 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT,
871 &mapping->fast->is_smmu_pt_coherent))
872 return -EINVAL;
873
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800874 mapping->fast->notifier.notifier_call = fast_smmu_notify;
875 av8l_register_notify(&mapping->fast->notifier);
876
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700877 dev->archdata.mapping = mapping;
878 set_dma_ops(dev, &fast_smmu_dma_ops);
879
880 return 0;
881}
882EXPORT_SYMBOL(fast_smmu_attach_device);
883
884/**
885 * fast_smmu_detach_device
886 * @dev: valid struct device pointer
887 *
888 * Detaches the provided device from a previously attached map.
889 * This voids the dma operations (dma_map_ops pointer)
890 */
891void fast_smmu_detach_device(struct device *dev,
892 struct dma_iommu_mapping *mapping)
893{
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700894 iommu_detach_group(mapping->domain, dev->iommu_group);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700895 dev->archdata.mapping = NULL;
896 set_dma_ops(dev, NULL);
897
Liam Mark4d4fbba2017-02-08 10:30:49 -0800898 kvfree(mapping->fast->bitmap);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700899 kfree(mapping->fast);
900}
901EXPORT_SYMBOL(fast_smmu_detach_device);