blob: 04a5c09f468dd09efe8b0e709b207bb0f5539192 [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)
Liam Mark83a9f86e2017-02-08 09:37:17 -080028#define FAST_MAIR_ATTR_IDX_CACHE 1
29#define FAST_PTE_ATTRINDX_SHIFT 2
30#define FAST_PTE_ATTRINDX_MASK 0x7
31#define FAST_PTE_SH_SHIFT 8
32#define FAST_PTE_SH_MASK (((av8l_fast_iopte)0x3) << FAST_PTE_SH_SHIFT)
33#define FAST_PTE_SH_OS (((av8l_fast_iopte)2) << FAST_PTE_SH_SHIFT)
34#define FAST_PTE_SH_IS (((av8l_fast_iopte)3) << FAST_PTE_SH_SHIFT)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070035
Mitchel Humpherys425d03d2016-06-23 13:25:12 -070036static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
37 bool coherent)
38{
39 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
40 return pgprot_noncached(prot);
41 else if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
42 return pgprot_writecombine(prot);
43 return prot;
44}
45
46static int __get_iommu_pgprot(unsigned long attrs, int prot,
47 bool coherent)
48{
49 if (!(attrs & DMA_ATTR_EXEC_MAPPING))
50 prot |= IOMMU_NOEXEC;
51 if ((attrs & DMA_ATTR_STRONGLY_ORDERED))
52 prot |= IOMMU_MMIO;
53 if (coherent)
54 prot |= IOMMU_CACHE;
55
56 return prot;
57}
58
Mitchel Humpherys9de66db2016-06-07 11:09:44 -070059static void fast_dmac_clean_range(struct dma_fast_smmu_mapping *mapping,
60 void *start, void *end)
61{
62 if (!mapping->is_smmu_pt_coherent)
63 dmac_clean_range(start, end);
64}
65
Liam Mark83a9f86e2017-02-08 09:37:17 -080066static bool __fast_is_pte_coherent(av8l_fast_iopte *ptep)
67{
68 int attr_idx = (*ptep & (FAST_PTE_ATTRINDX_MASK <<
69 FAST_PTE_ATTRINDX_SHIFT)) >>
70 FAST_PTE_ATTRINDX_SHIFT;
71
72 if ((attr_idx == FAST_MAIR_ATTR_IDX_CACHE) &&
73 (((*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_IS) ||
74 (*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_OS))
75 return true;
76
77 return false;
78}
79
80static bool is_dma_coherent(struct device *dev, unsigned long attrs)
81{
82 bool is_coherent;
83
84 if (attrs & DMA_ATTR_FORCE_COHERENT)
85 is_coherent = true;
86 else if (attrs & DMA_ATTR_FORCE_NON_COHERENT)
87 is_coherent = false;
88 else if (is_device_dma_coherent(dev))
89 is_coherent = true;
90 else
91 is_coherent = false;
92
93 return is_coherent;
94}
95
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070096/*
97 * Checks if the allocated range (ending at @end) covered the upcoming
98 * stale bit. We don't need to know exactly where the range starts since
99 * we already know where the candidate search range started. If, starting
100 * from the beginning of the candidate search range, we had to step over
101 * (or landed directly on top of) the upcoming stale bit, then we return
102 * true.
103 *
104 * Due to wrapping, there are two scenarios we'll need to check: (1) if the
105 * range [search_start, upcoming_stale] spans 0 (i.e. search_start >
106 * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale]
107 * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each
108 * of those two scenarios we need to handle three cases: (1) the bit was
109 * found before wrapping or
110 */
111static bool __bit_covered_stale(unsigned long upcoming_stale,
112 unsigned long search_start,
113 unsigned long end)
114{
115 if (search_start > upcoming_stale) {
116 if (end >= search_start) {
117 /*
118 * We started searching above upcoming_stale and we
119 * didn't wrap, so we couldn't have crossed
120 * upcoming_stale.
121 */
122 return false;
123 }
124 /*
125 * We wrapped. Did we cross (or land on top of)
126 * upcoming_stale?
127 */
128 return end >= upcoming_stale;
129 }
130
131 if (search_start <= upcoming_stale) {
132 if (end >= search_start) {
133 /*
134 * We didn't wrap. Did we cross (or land on top
135 * of) upcoming_stale?
136 */
137 return end >= upcoming_stale;
138 }
139 /*
140 * We wrapped. So we must have crossed upcoming_stale
141 * (since we started searching below it).
142 */
143 return true;
144 }
145
146 /* we should have covered all logical combinations... */
147 WARN_ON(1);
148 return true;
149}
150
151static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping,
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800152 unsigned long attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700153 size_t size)
154{
155 unsigned long bit, prev_search_start, nbits = size >> FAST_PAGE_SHIFT;
156 unsigned long align = (1 << get_order(size)) - 1;
157
158 bit = bitmap_find_next_zero_area(
159 mapping->bitmap, mapping->num_4k_pages, mapping->next_start,
160 nbits, align);
161 if (unlikely(bit > mapping->num_4k_pages)) {
162 /* try wrapping */
163 mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */
164 bit = bitmap_find_next_zero_area(
165 mapping->bitmap, mapping->num_4k_pages, 0, nbits,
166 align);
167 if (unlikely(bit > mapping->num_4k_pages))
168 return DMA_ERROR_CODE;
169 }
170
171 bitmap_set(mapping->bitmap, bit, nbits);
172 prev_search_start = mapping->next_start;
173 mapping->next_start = bit + nbits;
174 if (unlikely(mapping->next_start >= mapping->num_4k_pages))
175 mapping->next_start = 0;
176
177 /*
178 * If we just re-allocated a VA whose TLB hasn't been invalidated
179 * since it was last used and unmapped, we need to invalidate it
180 * here. We actually invalidate the entire TLB so that we don't
181 * have to invalidate the TLB again until we wrap back around.
182 */
183 if (mapping->have_stale_tlbs &&
184 __bit_covered_stale(mapping->upcoming_stale_bit,
185 prev_search_start,
186 bit + nbits - 1)) {
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800187 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
188
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700189 iommu_tlbiall(mapping->domain);
190 mapping->have_stale_tlbs = false;
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800191 av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700192 }
193
194 return (bit << FAST_PAGE_SHIFT) + mapping->base;
195}
196
197/*
198 * Checks whether the candidate bit will be allocated sooner than the
199 * current upcoming stale bit. We can say candidate will be upcoming
200 * sooner than the current upcoming stale bit if it lies between the
201 * starting bit of the next search range and the upcoming stale bit
202 * (allowing for wrap-around).
203 *
204 * Stated differently, we're checking the relative ordering of three
205 * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations,
206 * namely:
207 *
208 * 0 |---A---B---C---| TOP (Case 1)
209 * 0 |---A---C---B---| TOP (Case 2)
210 * 0 |---B---A---C---| TOP (Case 3)
211 * 0 |---B---C---A---| TOP (Case 4)
212 * 0 |---C---A---B---| TOP (Case 5)
213 * 0 |---C---B---A---| TOP (Case 6)
214 *
215 * Note that since we're allowing numbers to wrap, the following three
216 * scenarios are all equivalent for Case 1:
217 *
218 * 0 |---A---B---C---| TOP
219 * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.)
220 * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.)
221 *
222 * In any of these cases, if we start searching from A, we will find B
223 * before we find C.
224 *
225 * We can also find two equivalent cases for Case 2:
226 *
227 * 0 |---A---C---B---| TOP
228 * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.)
229 * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.)
230 *
231 * In any of these cases, if we start searching from A, we will find C
232 * before we find B.
233 */
234static bool __bit_is_sooner(unsigned long candidate,
235 struct dma_fast_smmu_mapping *mapping)
236{
237 unsigned long A = mapping->next_start;
238 unsigned long B = candidate;
239 unsigned long C = mapping->upcoming_stale_bit;
240
241 if ((A < B && B < C) || /* Case 1 */
242 (C < A && A < B) || /* Case 5 */
243 (B < C && C < A)) /* Case 4 */
244 return true;
245
246 if ((A < C && C < B) || /* Case 2 */
247 (B < A && A < C) || /* Case 3 */
248 (C < B && B < A)) /* Case 6 */
249 return false;
250
251 /*
252 * For simplicity, we've been ignoring the possibility of any of
253 * our three numbers being equal. Handle those cases here (they
254 * shouldn't happen very often, (I think?)).
255 */
256
257 /*
258 * If candidate is the next bit to be searched then it's definitely
259 * sooner.
260 */
261 if (A == B)
262 return true;
263
264 /*
265 * If candidate is the next upcoming stale bit we'll return false
266 * to avoid doing `upcoming = candidate' in the caller (which would
267 * be useless since they're already equal)
268 */
269 if (B == C)
270 return false;
271
272 /*
273 * If next start is the upcoming stale bit then candidate can't
274 * possibly be sooner. The "soonest" bit is already selected.
275 */
276 if (A == C)
277 return false;
278
279 /* We should have covered all logical combinations. */
280 WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C);
281 return true;
282}
283
284static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping,
285 dma_addr_t iova, size_t size)
286{
287 unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT;
288 unsigned long nbits = size >> FAST_PAGE_SHIFT;
289
290 /*
291 * We don't invalidate TLBs on unmap. We invalidate TLBs on map
292 * when we're about to re-allocate a VA that was previously
293 * unmapped but hasn't yet been invalidated. So we need to keep
294 * track of which bit is the closest to being re-allocated here.
295 */
296 if (__bit_is_sooner(start_bit, mapping))
297 mapping->upcoming_stale_bit = start_bit;
298
299 bitmap_clear(mapping->bitmap, start_bit, nbits);
300 mapping->have_stale_tlbs = true;
301}
302
303
304static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off,
305 size_t size, enum dma_data_direction dir)
306{
307 __dma_map_area(page_address(page) + off, size, dir);
308}
309
310static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off,
311 size_t size, enum dma_data_direction dir)
312{
313 __dma_unmap_area(page_address(page) + off, size, dir);
314
315 /* TODO: WHAT IS THIS? */
316 /*
317 * Mark the D-cache clean for this page to avoid extra flushing.
318 */
319 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
320 set_bit(PG_dcache_clean, &page->flags);
321}
322
323static int __fast_dma_direction_to_prot(enum dma_data_direction dir)
324{
325 switch (dir) {
326 case DMA_BIDIRECTIONAL:
327 return IOMMU_READ | IOMMU_WRITE;
328 case DMA_TO_DEVICE:
329 return IOMMU_READ;
330 case DMA_FROM_DEVICE:
331 return IOMMU_WRITE;
332 default:
333 return 0;
334 }
335}
336
337static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page,
338 unsigned long offset, size_t size,
339 enum dma_data_direction dir,
340 unsigned long attrs)
341{
342 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
343 dma_addr_t iova;
344 unsigned long flags;
345 av8l_fast_iopte *pmd;
346 phys_addr_t phys_plus_off = page_to_phys(page) + offset;
347 phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE);
348 unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK;
349 size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE);
350 int nptes = len >> FAST_PAGE_SHIFT;
351 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
352 int prot = __fast_dma_direction_to_prot(dir);
Liam Mark83a9f86e2017-02-08 09:37:17 -0800353 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700354
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700355 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700356
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700357 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700358 __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map),
359 offset_from_phys_to_map, size, dir);
360
361 spin_lock_irqsave(&mapping->lock, flags);
362
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800363 iova = __fast_smmu_alloc_iova(mapping, attrs, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700364
365 if (unlikely(iova == DMA_ERROR_CODE))
366 goto fail;
367
368 pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
369
370 if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot)))
371 goto fail_free_iova;
372
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700373 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700374
375 spin_unlock_irqrestore(&mapping->lock, flags);
376 return iova + offset_from_phys_to_map;
377
378fail_free_iova:
379 __fast_smmu_free_iova(mapping, iova, size);
380fail:
381 spin_unlock_irqrestore(&mapping->lock, flags);
382 return DMA_ERROR_CODE;
383}
384
385static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova,
386 size_t size, enum dma_data_direction dir,
387 unsigned long attrs)
388{
389 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
390 unsigned long flags;
391 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
392 unsigned long offset = iova & ~FAST_PAGE_MASK;
393 size_t len = ALIGN(size + offset, FAST_PAGE_SIZE);
394 int nptes = len >> FAST_PAGE_SHIFT;
395 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
396 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
Liam Mark83a9f86e2017-02-08 09:37:17 -0800397 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700398
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700399 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700400 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
401
402 spin_lock_irqsave(&mapping->lock, flags);
403 av8l_fast_unmap_public(pmd, len);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700404 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700405 __fast_smmu_free_iova(mapping, iova, len);
406 spin_unlock_irqrestore(&mapping->lock, flags);
407}
408
Liam Mark78d7fb52016-12-01 13:05:31 -0800409static void fast_smmu_sync_single_for_cpu(struct device *dev,
410 dma_addr_t iova, size_t size, enum dma_data_direction dir)
411{
412 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
413 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
414 unsigned long offset = iova & ~FAST_PAGE_MASK;
415 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
416
Liam Mark83a9f86e2017-02-08 09:37:17 -0800417 if (!__fast_is_pte_coherent(pmd))
Liam Mark78d7fb52016-12-01 13:05:31 -0800418 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
419}
420
421static void fast_smmu_sync_single_for_device(struct device *dev,
422 dma_addr_t iova, size_t size, enum dma_data_direction dir)
423{
424 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
425 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
426 unsigned long offset = iova & ~FAST_PAGE_MASK;
427 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
428
Liam Mark83a9f86e2017-02-08 09:37:17 -0800429 if (!__fast_is_pte_coherent(pmd))
Liam Mark78d7fb52016-12-01 13:05:31 -0800430 __fast_dma_page_cpu_to_dev(page, offset, size, dir);
431}
432
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700433static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
434 int nents, enum dma_data_direction dir,
435 unsigned long attrs)
436{
437 return -EINVAL;
438}
439
440static void fast_smmu_unmap_sg(struct device *dev,
441 struct scatterlist *sg, int nents,
442 enum dma_data_direction dir,
443 unsigned long attrs)
444{
445 WARN_ON_ONCE(1);
446}
447
Liam Mark78d7fb52016-12-01 13:05:31 -0800448static void fast_smmu_sync_sg_for_cpu(struct device *dev,
449 struct scatterlist *sg, int nents, enum dma_data_direction dir)
450{
451 WARN_ON_ONCE(1);
452}
453
454static void fast_smmu_sync_sg_for_device(struct device *dev,
455 struct scatterlist *sg, int nents, enum dma_data_direction dir)
456{
457 WARN_ON_ONCE(1);
458}
459
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700460static void __fast_smmu_free_pages(struct page **pages, int count)
461{
462 int i;
463
464 for (i = 0; i < count; i++)
465 __free_page(pages[i]);
466 kvfree(pages);
467}
468
469static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
470{
471 struct page **pages;
472 unsigned int i = 0, array_size = count * sizeof(*pages);
473
474 if (array_size <= PAGE_SIZE)
475 pages = kzalloc(array_size, GFP_KERNEL);
476 else
477 pages = vzalloc(array_size);
478 if (!pages)
479 return NULL;
480
481 /* IOMMU can map any pages, so himem can also be used here */
482 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
483
484 for (i = 0; i < count; ++i) {
485 struct page *page = alloc_page(gfp);
486
487 if (!page) {
488 __fast_smmu_free_pages(pages, i);
489 return NULL;
490 }
491 pages[i] = page;
492 }
493 return pages;
494}
495
496static void *fast_smmu_alloc(struct device *dev, size_t size,
497 dma_addr_t *handle, gfp_t gfp,
498 unsigned long attrs)
499{
500 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
501 struct sg_table sgt;
502 dma_addr_t dma_addr, iova_iter;
503 void *addr;
504 av8l_fast_iopte *ptep;
505 unsigned long flags;
506 struct sg_mapping_iter miter;
507 unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
508 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
Liam Mark83a9f86e2017-02-08 09:37:17 -0800509 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700510 pgprot_t remap_prot = __get_dma_pgprot(attrs, PAGE_KERNEL, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700511 struct page **pages;
512
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700513 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
514
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700515 *handle = DMA_ERROR_CODE;
516
517 pages = __fast_smmu_alloc_pages(count, gfp);
518 if (!pages) {
519 dev_err(dev, "no pages\n");
520 return NULL;
521 }
522
523 size = ALIGN(size, SZ_4K);
524 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
525 dev_err(dev, "no sg tablen\n");
526 goto out_free_pages;
527 }
528
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700529 if (!is_coherent) {
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700530 /*
531 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
532 * sufficient here, so skip it by using the "wrong" direction.
533 */
534 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
535 SG_MITER_FROM_SG);
536 while (sg_miter_next(&miter))
Kyle Yan65be4a52016-10-31 15:05:00 -0700537 __dma_flush_area(miter.addr, miter.length);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700538 sg_miter_stop(&miter);
539 }
540
541 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800542 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700543 if (dma_addr == DMA_ERROR_CODE) {
544 dev_err(dev, "no iova\n");
545 spin_unlock_irqrestore(&mapping->lock, flags);
546 goto out_free_sg;
547 }
548 iova_iter = dma_addr;
549 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
550 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
551 while (sg_miter_next(&miter)) {
552 int nptes = miter.length >> FAST_PAGE_SHIFT;
553
554 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
555 if (unlikely(av8l_fast_map_public(
556 ptep, page_to_phys(miter.page),
557 miter.length, prot))) {
558 dev_err(dev, "no map public\n");
559 /* TODO: unwind previously successful mappings */
560 goto out_free_iova;
561 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700562 fast_dmac_clean_range(mapping, ptep, ptep + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700563 iova_iter += miter.length;
564 }
565 sg_miter_stop(&miter);
566 spin_unlock_irqrestore(&mapping->lock, flags);
567
568 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
569 __builtin_return_address(0));
570 if (!addr) {
571 dev_err(dev, "no common pages\n");
572 goto out_unmap;
573 }
574
575 *handle = dma_addr;
576 sg_free_table(&sgt);
577 return addr;
578
579out_unmap:
580 /* need to take the lock again for page tables and iova */
581 spin_lock_irqsave(&mapping->lock, flags);
582 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
583 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700584 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700585out_free_iova:
586 __fast_smmu_free_iova(mapping, dma_addr, size);
587 spin_unlock_irqrestore(&mapping->lock, flags);
588out_free_sg:
589 sg_free_table(&sgt);
590out_free_pages:
591 __fast_smmu_free_pages(pages, count);
592 return NULL;
593}
594
595static void fast_smmu_free(struct device *dev, size_t size,
596 void *vaddr, dma_addr_t dma_handle,
597 unsigned long attrs)
598{
599 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
600 struct vm_struct *area;
601 struct page **pages;
602 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
603 av8l_fast_iopte *ptep;
604 unsigned long flags;
605
606 size = ALIGN(size, SZ_4K);
607
608 area = find_vm_area(vaddr);
609 if (WARN_ON_ONCE(!area))
610 return;
611
612 pages = area->pages;
613 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
614 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
615 spin_lock_irqsave(&mapping->lock, flags);
616 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700617 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700618 __fast_smmu_free_iova(mapping, dma_handle, size);
619 spin_unlock_irqrestore(&mapping->lock, flags);
620 __fast_smmu_free_pages(pages, count);
621}
622
Patrick Daly7bcb5462016-08-03 17:27:36 -0700623static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
624 void *cpu_addr, dma_addr_t dma_addr,
625 size_t size, unsigned long attrs)
626{
627 struct vm_struct *area;
628 unsigned long uaddr = vma->vm_start;
629 struct page **pages;
630 int i, nr_pages, ret = 0;
Liam Mark83a9f86e2017-02-08 09:37:17 -0800631 bool coherent = is_dma_coherent(dev, attrs);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700632
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700633 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
634 coherent);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700635 area = find_vm_area(cpu_addr);
636 if (!area)
637 return -EINVAL;
638
639 pages = area->pages;
640 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
641 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
642 ret = vm_insert_page(vma, uaddr, pages[i]);
643 if (ret)
644 break;
645 uaddr += PAGE_SIZE;
646 }
647
648 return ret;
649}
650
Patrick Daly9c79f382017-06-12 18:15:25 -0700651static int fast_smmu_get_sgtable(struct device *dev, struct sg_table *sgt,
652 void *cpu_addr, dma_addr_t dma_addr,
653 size_t size, unsigned long attrs)
654{
655 unsigned int n_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
656 struct vm_struct *area;
657
658 area = find_vm_area(cpu_addr);
659 if (!area || !area->pages)
660 return -EINVAL;
661
662 return sg_alloc_table_from_pages(sgt, area->pages, n_pages, 0, size,
663 GFP_KERNEL);
664}
665
Patrick Daly199fa672017-05-04 15:30:16 -0700666static dma_addr_t fast_smmu_dma_map_resource(
667 struct device *dev, phys_addr_t phys_addr,
668 size_t size, enum dma_data_direction dir,
669 unsigned long attrs)
670{
671 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
672 size_t offset = phys_addr & ~FAST_PAGE_MASK;
673 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
674 dma_addr_t dma_addr;
675 int prot;
676 unsigned long flags;
677
678 spin_lock_irqsave(&mapping->lock, flags);
679 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, len);
680 spin_unlock_irqrestore(&mapping->lock, flags);
681
682 if (dma_addr == DMA_ERROR_CODE)
683 return dma_addr;
684
685 prot = __fast_dma_direction_to_prot(dir);
686 prot |= IOMMU_MMIO;
687
688 if (iommu_map(mapping->domain, dma_addr, phys_addr - offset,
689 len, prot)) {
690 spin_lock_irqsave(&mapping->lock, flags);
691 __fast_smmu_free_iova(mapping, dma_addr, len);
692 spin_unlock_irqrestore(&mapping->lock, flags);
693 return DMA_ERROR_CODE;
694 }
695 return dma_addr + offset;
696}
697
698static void fast_smmu_dma_unmap_resource(
699 struct device *dev, dma_addr_t addr,
700 size_t size, enum dma_data_direction dir,
701 unsigned long attrs)
702{
703 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
704 size_t offset = addr & ~FAST_PAGE_MASK;
705 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
706 unsigned long flags;
707
708 iommu_unmap(mapping->domain, addr - offset, len);
709 spin_lock_irqsave(&mapping->lock, flags);
710 __fast_smmu_free_iova(mapping, addr, len);
711 spin_unlock_irqrestore(&mapping->lock, flags);
712}
713
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700714static int fast_smmu_mapping_error(struct device *dev,
715 dma_addr_t dma_addr)
716{
717 return dma_addr == DMA_ERROR_CODE;
718}
719
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800720static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
721 void *data)
722{
723 av8l_fast_iopte *ptep = data;
724 dma_addr_t iova;
725 unsigned long bitmap_idx;
726
727 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
728 iova = bitmap_idx << FAST_PAGE_SHIFT;
729 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
730 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
731 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530732 fast->pgtbl_pmds, bitmap_idx);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800733 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
734 32, 8, fast->bitmap, fast->bitmap_size, false);
735}
736
737static int fast_smmu_notify(struct notifier_block *self,
738 unsigned long action, void *data)
739{
740 struct dma_fast_smmu_mapping *fast = container_of(
741 self, struct dma_fast_smmu_mapping, notifier);
742
743 switch (action) {
744 case MAPPED_OVER_STALE_TLB:
745 __fast_smmu_mapped_over_stale(fast, data);
746 return NOTIFY_OK;
747 default:
748 WARN(1, "Unhandled notifier action");
749 return NOTIFY_DONE;
750 }
751}
752
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700753static const struct dma_map_ops fast_smmu_dma_ops = {
754 .alloc = fast_smmu_alloc,
755 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700756 .mmap = fast_smmu_mmap_attrs,
Patrick Daly9c79f382017-06-12 18:15:25 -0700757 .get_sgtable = fast_smmu_get_sgtable,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700758 .map_page = fast_smmu_map_page,
759 .unmap_page = fast_smmu_unmap_page,
Liam Mark78d7fb52016-12-01 13:05:31 -0800760 .sync_single_for_cpu = fast_smmu_sync_single_for_cpu,
761 .sync_single_for_device = fast_smmu_sync_single_for_device,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700762 .map_sg = fast_smmu_map_sg,
763 .unmap_sg = fast_smmu_unmap_sg,
Liam Mark78d7fb52016-12-01 13:05:31 -0800764 .sync_sg_for_cpu = fast_smmu_sync_sg_for_cpu,
765 .sync_sg_for_device = fast_smmu_sync_sg_for_device,
Patrick Daly199fa672017-05-04 15:30:16 -0700766 .map_resource = fast_smmu_dma_map_resource,
767 .unmap_resource = fast_smmu_dma_unmap_resource,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700768 .mapping_error = fast_smmu_mapping_error,
769};
770
771/**
772 * __fast_smmu_create_mapping_sized
773 * @base: bottom of the VA range
774 * @size: size of the VA range in bytes
775 *
776 * Creates a mapping structure which holds information about used/unused IO
777 * address ranges, which is required to perform mapping with IOMMU aware
778 * functions. The only VA range supported is [0, 4GB).
779 *
780 * The client device need to be attached to the mapping with
781 * fast_smmu_attach_device function.
782 */
783static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530784 dma_addr_t base, u64 size)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700785{
786 struct dma_fast_smmu_mapping *fast;
787
788 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
789 if (!fast)
790 goto err;
791
792 fast->base = base;
793 fast->size = size;
794 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
795 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
796
Liam Mark4d4fbba2017-02-08 10:30:49 -0800797 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL | __GFP_NOWARN |
798 __GFP_NORETRY);
799 if (!fast->bitmap)
800 fast->bitmap = vzalloc(fast->bitmap_size);
801
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700802 if (!fast->bitmap)
803 goto err2;
804
805 spin_lock_init(&fast->lock);
806
807 return fast;
808err2:
809 kfree(fast);
810err:
811 return ERR_PTR(-ENOMEM);
812}
813
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700814/*
815 * Based off of similar code from dma-iommu.c, but modified to use a different
816 * iova allocator
817 */
818static void fast_smmu_reserve_pci_windows(struct device *dev,
819 struct dma_fast_smmu_mapping *mapping)
820{
821 struct pci_host_bridge *bridge;
822 struct resource_entry *window;
823 phys_addr_t start, end;
824 struct pci_dev *pci_dev;
825 unsigned long flags;
826
827 if (!dev_is_pci(dev))
828 return;
829
830 pci_dev = to_pci_dev(dev);
831 bridge = pci_find_host_bridge(pci_dev->bus);
832
833 spin_lock_irqsave(&mapping->lock, flags);
834 resource_list_for_each_entry(window, &bridge->windows) {
835 if (resource_type(window->res) != IORESOURCE_MEM &&
836 resource_type(window->res) != IORESOURCE_IO)
837 continue;
838
839 start = round_down(window->res->start - window->offset,
840 FAST_PAGE_SIZE);
841 end = round_up(window->res->end - window->offset,
842 FAST_PAGE_SIZE);
843 start = max_t(unsigned long, mapping->base, start);
844 end = min_t(unsigned long, mapping->base + mapping->size, end);
845 if (start >= end)
846 continue;
847
848 dev_dbg(dev, "iova allocator reserved 0x%pa-0x%pa\n",
849 &start, &end);
850
851 start = (start - mapping->base) >> FAST_PAGE_SHIFT;
852 end = (end - mapping->base) >> FAST_PAGE_SHIFT;
853 bitmap_set(mapping->bitmap, start, end - start);
854 }
855 spin_unlock_irqrestore(&mapping->lock, flags);
856}
857
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700858/**
Patrick Daly1748f082017-09-05 21:32:52 -0700859 * fast_smmu_init_mapping
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700860 * @dev: valid struct device pointer
861 * @mapping: io address space mapping structure (returned from
Patrick Daly1748f082017-09-05 21:32:52 -0700862 * arm_iommu_create_mapping)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700863 *
Patrick Daly1748f082017-09-05 21:32:52 -0700864 * Called the first time a device is attached to this mapping.
865 * Not for dma client use.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700866 */
Patrick Daly1748f082017-09-05 21:32:52 -0700867int fast_smmu_init_mapping(struct device *dev,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700868 struct dma_iommu_mapping *mapping)
869{
Patrick Daly1748f082017-09-05 21:32:52 -0700870 int err, atomic_domain = 1;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700871 struct iommu_domain *domain = mapping->domain;
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700872 struct iommu_group *group;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700873 struct iommu_pgtbl_info info;
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530874 u64 size = (u64)mapping->bits << PAGE_SHIFT;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700875
Patrick Daly1748f082017-09-05 21:32:52 -0700876 if (mapping->base + size > (SZ_1G * 4ULL)) {
877 dev_err(dev, "Iova end address too large\n");
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700878 return -EINVAL;
Patrick Daly1748f082017-09-05 21:32:52 -0700879 }
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700880
881 if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC,
882 &atomic_domain))
883 return -EINVAL;
884
885 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
886 if (IS_ERR(mapping->fast))
887 return -ENOMEM;
888 mapping->fast->domain = domain;
889 mapping->fast->dev = dev;
890
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700891 fast_smmu_reserve_pci_windows(dev, mapping->fast);
892
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700893 group = dev->iommu_group;
894 if (!group) {
895 dev_err(dev, "No iommu associated with device\n");
Patrick Daly1748f082017-09-05 21:32:52 -0700896 err = -ENODEV;
897 goto release_mapping;
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700898 }
899
900 if (iommu_get_domain_for_dev(dev)) {
901 dev_err(dev, "Device already attached to other iommu_domain\n");
Patrick Daly1748f082017-09-05 21:32:52 -0700902 err = -EINVAL;
903 goto release_mapping;
Mitchel Humpherysad9df1f2016-05-27 14:58:31 -0700904 }
905
Patrick Daly1748f082017-09-05 21:32:52 -0700906 /*
907 * Need to attach prior to calling DOMAIN_ATTR_PGTBL_INFO and then
908 * detach to be in the expected state. Its a bit messy.
909 */
910 if (iommu_attach_group(mapping->domain, group)) {
911 err = -EINVAL;
912 goto release_mapping;
913 }
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700914
915 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
916 &info)) {
917 dev_err(dev, "Couldn't get page table info\n");
Patrick Daly1748f082017-09-05 21:32:52 -0700918 err = -EINVAL;
919 goto detach_group;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700920 }
921 mapping->fast->pgtbl_pmds = info.pmds;
922
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700923 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT,
Patrick Daly1748f082017-09-05 21:32:52 -0700924 &mapping->fast->is_smmu_pt_coherent)) {
925 err = -EINVAL;
926 goto detach_group;
927 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700928
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800929 mapping->fast->notifier.notifier_call = fast_smmu_notify;
930 av8l_register_notify(&mapping->fast->notifier);
931
Patrick Daly1748f082017-09-05 21:32:52 -0700932 iommu_detach_group(mapping->domain, group);
933 mapping->ops = &fast_smmu_dma_ops;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700934 return 0;
Patrick Daly1748f082017-09-05 21:32:52 -0700935
936detach_group:
937 iommu_detach_group(mapping->domain, group);
938release_mapping:
939 kfree(mapping->fast->bitmap);
940 kfree(mapping->fast);
941 return err;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700942}
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700943
944/**
Patrick Daly1748f082017-09-05 21:32:52 -0700945 * fast_smmu_release_mapping
946 * @kref: dma_iommu_mapping->kref
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700947 *
Patrick Daly1748f082017-09-05 21:32:52 -0700948 * Cleans up the given iommu mapping.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700949 */
Patrick Daly1748f082017-09-05 21:32:52 -0700950void fast_smmu_release_mapping(struct kref *kref)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700951{
Patrick Daly1748f082017-09-05 21:32:52 -0700952 struct dma_iommu_mapping *mapping =
953 container_of(kref, struct dma_iommu_mapping, kref);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700954
Liam Mark4d4fbba2017-02-08 10:30:49 -0800955 kvfree(mapping->fast->bitmap);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700956 kfree(mapping->fast);
Patrick Daly1748f082017-09-05 21:32:52 -0700957 iommu_domain_free(mapping->domain);
958 kfree(mapping);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700959}