blob: 73090df81b6fe30572431cff21aa476d9881fb5a [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
Patrick Daly23301482017-10-12 16:18:25 -070023#include <soc/qcom/secure_buffer.h>
24#include <linux/arm-smmu-errata.h>
25
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070026/* some redundant definitions... :( TODO: move to io-pgtable-fast.h */
27#define FAST_PAGE_SHIFT 12
28#define FAST_PAGE_SIZE (1UL << FAST_PAGE_SHIFT)
29#define FAST_PAGE_MASK (~(PAGE_SIZE - 1))
30#define FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000)
Liam Mark83a9f86e2017-02-08 09:37:17 -080031#define FAST_MAIR_ATTR_IDX_CACHE 1
32#define FAST_PTE_ATTRINDX_SHIFT 2
33#define FAST_PTE_ATTRINDX_MASK 0x7
34#define FAST_PTE_SH_SHIFT 8
35#define FAST_PTE_SH_MASK (((av8l_fast_iopte)0x3) << FAST_PTE_SH_SHIFT)
36#define FAST_PTE_SH_OS (((av8l_fast_iopte)2) << FAST_PTE_SH_SHIFT)
37#define FAST_PTE_SH_IS (((av8l_fast_iopte)3) << FAST_PTE_SH_SHIFT)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070038
Mitchel Humpherys425d03d2016-06-23 13:25:12 -070039static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
40 bool coherent)
41{
42 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
43 return pgprot_noncached(prot);
44 else if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
45 return pgprot_writecombine(prot);
46 return prot;
47}
48
49static int __get_iommu_pgprot(unsigned long attrs, int prot,
50 bool coherent)
51{
52 if (!(attrs & DMA_ATTR_EXEC_MAPPING))
53 prot |= IOMMU_NOEXEC;
54 if ((attrs & DMA_ATTR_STRONGLY_ORDERED))
55 prot |= IOMMU_MMIO;
56 if (coherent)
57 prot |= IOMMU_CACHE;
58
59 return prot;
60}
61
Mitchel Humpherys9de66db2016-06-07 11:09:44 -070062static void fast_dmac_clean_range(struct dma_fast_smmu_mapping *mapping,
63 void *start, void *end)
64{
65 if (!mapping->is_smmu_pt_coherent)
66 dmac_clean_range(start, end);
67}
68
Liam Mark83a9f86e2017-02-08 09:37:17 -080069static bool __fast_is_pte_coherent(av8l_fast_iopte *ptep)
70{
71 int attr_idx = (*ptep & (FAST_PTE_ATTRINDX_MASK <<
72 FAST_PTE_ATTRINDX_SHIFT)) >>
73 FAST_PTE_ATTRINDX_SHIFT;
74
75 if ((attr_idx == FAST_MAIR_ATTR_IDX_CACHE) &&
76 (((*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_IS) ||
77 (*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_OS))
78 return true;
79
80 return false;
81}
82
83static bool is_dma_coherent(struct device *dev, unsigned long attrs)
84{
85 bool is_coherent;
86
87 if (attrs & DMA_ATTR_FORCE_COHERENT)
88 is_coherent = true;
89 else if (attrs & DMA_ATTR_FORCE_NON_COHERENT)
90 is_coherent = false;
91 else if (is_device_dma_coherent(dev))
92 is_coherent = true;
93 else
94 is_coherent = false;
95
96 return is_coherent;
97}
98
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070099/*
100 * Checks if the allocated range (ending at @end) covered the upcoming
101 * stale bit. We don't need to know exactly where the range starts since
102 * we already know where the candidate search range started. If, starting
103 * from the beginning of the candidate search range, we had to step over
104 * (or landed directly on top of) the upcoming stale bit, then we return
105 * true.
106 *
107 * Due to wrapping, there are two scenarios we'll need to check: (1) if the
108 * range [search_start, upcoming_stale] spans 0 (i.e. search_start >
109 * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale]
110 * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each
111 * of those two scenarios we need to handle three cases: (1) the bit was
112 * found before wrapping or
113 */
114static bool __bit_covered_stale(unsigned long upcoming_stale,
115 unsigned long search_start,
116 unsigned long end)
117{
118 if (search_start > upcoming_stale) {
119 if (end >= search_start) {
120 /*
121 * We started searching above upcoming_stale and we
122 * didn't wrap, so we couldn't have crossed
123 * upcoming_stale.
124 */
125 return false;
126 }
127 /*
128 * We wrapped. Did we cross (or land on top of)
129 * upcoming_stale?
130 */
131 return end >= upcoming_stale;
132 }
133
134 if (search_start <= upcoming_stale) {
135 if (end >= search_start) {
136 /*
137 * We didn't wrap. Did we cross (or land on top
138 * of) upcoming_stale?
139 */
140 return end >= upcoming_stale;
141 }
142 /*
143 * We wrapped. So we must have crossed upcoming_stale
144 * (since we started searching below it).
145 */
146 return true;
147 }
148
149 /* we should have covered all logical combinations... */
150 WARN_ON(1);
151 return true;
152}
153
154static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping,
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800155 unsigned long attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700156 size_t size)
157{
Patrick Daly23301482017-10-12 16:18:25 -0700158 unsigned long bit, prev_search_start, nbits;
159 unsigned long align;
160 unsigned long guard_len;
161 dma_addr_t iova;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700162
Patrick Daly23301482017-10-12 16:18:25 -0700163 if (mapping->min_iova_align)
164 guard_len = ALIGN(size, mapping->min_iova_align) - size;
165 else
166 guard_len = 0;
167
168 nbits = (size + guard_len) >> FAST_PAGE_SHIFT;
169 align = (1 << get_order(size + guard_len)) - 1;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700170 bit = bitmap_find_next_zero_area(
171 mapping->bitmap, mapping->num_4k_pages, mapping->next_start,
172 nbits, align);
173 if (unlikely(bit > mapping->num_4k_pages)) {
174 /* try wrapping */
175 mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */
176 bit = bitmap_find_next_zero_area(
177 mapping->bitmap, mapping->num_4k_pages, 0, nbits,
178 align);
179 if (unlikely(bit > mapping->num_4k_pages))
180 return DMA_ERROR_CODE;
181 }
182
183 bitmap_set(mapping->bitmap, bit, nbits);
184 prev_search_start = mapping->next_start;
185 mapping->next_start = bit + nbits;
186 if (unlikely(mapping->next_start >= mapping->num_4k_pages))
187 mapping->next_start = 0;
188
189 /*
190 * If we just re-allocated a VA whose TLB hasn't been invalidated
191 * since it was last used and unmapped, we need to invalidate it
192 * here. We actually invalidate the entire TLB so that we don't
193 * have to invalidate the TLB again until we wrap back around.
194 */
195 if (mapping->have_stale_tlbs &&
196 __bit_covered_stale(mapping->upcoming_stale_bit,
197 prev_search_start,
198 bit + nbits - 1)) {
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800199 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
200
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700201 iommu_tlbiall(mapping->domain);
202 mapping->have_stale_tlbs = false;
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800203 av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700204 }
205
Patrick Daly23301482017-10-12 16:18:25 -0700206 iova = (bit << FAST_PAGE_SHIFT) + mapping->base;
207 if (guard_len &&
208 iommu_map(mapping->domain, iova + size,
209 page_to_phys(mapping->guard_page),
210 guard_len, ARM_SMMU_GUARD_PROT)) {
211
212 bitmap_clear(mapping->bitmap, bit, nbits);
213 return DMA_ERROR_CODE;
214 }
215 return iova;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700216}
217
218/*
219 * Checks whether the candidate bit will be allocated sooner than the
220 * current upcoming stale bit. We can say candidate will be upcoming
221 * sooner than the current upcoming stale bit if it lies between the
222 * starting bit of the next search range and the upcoming stale bit
223 * (allowing for wrap-around).
224 *
225 * Stated differently, we're checking the relative ordering of three
226 * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations,
227 * namely:
228 *
229 * 0 |---A---B---C---| TOP (Case 1)
230 * 0 |---A---C---B---| TOP (Case 2)
231 * 0 |---B---A---C---| TOP (Case 3)
232 * 0 |---B---C---A---| TOP (Case 4)
233 * 0 |---C---A---B---| TOP (Case 5)
234 * 0 |---C---B---A---| TOP (Case 6)
235 *
236 * Note that since we're allowing numbers to wrap, the following three
237 * scenarios are all equivalent for Case 1:
238 *
239 * 0 |---A---B---C---| TOP
240 * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.)
241 * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.)
242 *
243 * In any of these cases, if we start searching from A, we will find B
244 * before we find C.
245 *
246 * We can also find two equivalent cases for Case 2:
247 *
248 * 0 |---A---C---B---| TOP
249 * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.)
250 * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.)
251 *
252 * In any of these cases, if we start searching from A, we will find C
253 * before we find B.
254 */
255static bool __bit_is_sooner(unsigned long candidate,
256 struct dma_fast_smmu_mapping *mapping)
257{
258 unsigned long A = mapping->next_start;
259 unsigned long B = candidate;
260 unsigned long C = mapping->upcoming_stale_bit;
261
262 if ((A < B && B < C) || /* Case 1 */
263 (C < A && A < B) || /* Case 5 */
264 (B < C && C < A)) /* Case 4 */
265 return true;
266
267 if ((A < C && C < B) || /* Case 2 */
268 (B < A && A < C) || /* Case 3 */
269 (C < B && B < A)) /* Case 6 */
270 return false;
271
272 /*
273 * For simplicity, we've been ignoring the possibility of any of
274 * our three numbers being equal. Handle those cases here (they
275 * shouldn't happen very often, (I think?)).
276 */
277
278 /*
279 * If candidate is the next bit to be searched then it's definitely
280 * sooner.
281 */
282 if (A == B)
283 return true;
284
285 /*
286 * If candidate is the next upcoming stale bit we'll return false
287 * to avoid doing `upcoming = candidate' in the caller (which would
288 * be useless since they're already equal)
289 */
290 if (B == C)
291 return false;
292
293 /*
294 * If next start is the upcoming stale bit then candidate can't
295 * possibly be sooner. The "soonest" bit is already selected.
296 */
297 if (A == C)
298 return false;
299
300 /* We should have covered all logical combinations. */
301 WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C);
302 return true;
303}
304
305static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping,
306 dma_addr_t iova, size_t size)
307{
308 unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT;
Patrick Daly23301482017-10-12 16:18:25 -0700309 unsigned long nbits;
310 unsigned long guard_len;
311
312 if (mapping->min_iova_align)
313 guard_len = ALIGN(size, mapping->min_iova_align) - size;
314 else
315 guard_len = 0;
316 nbits = (size + guard_len) >> FAST_PAGE_SHIFT;
317
318 iommu_unmap(mapping->domain, iova + size, guard_len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700319
320 /*
321 * We don't invalidate TLBs on unmap. We invalidate TLBs on map
322 * when we're about to re-allocate a VA that was previously
323 * unmapped but hasn't yet been invalidated. So we need to keep
324 * track of which bit is the closest to being re-allocated here.
325 */
326 if (__bit_is_sooner(start_bit, mapping))
327 mapping->upcoming_stale_bit = start_bit;
328
329 bitmap_clear(mapping->bitmap, start_bit, nbits);
330 mapping->have_stale_tlbs = true;
331}
332
333
334static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off,
335 size_t size, enum dma_data_direction dir)
336{
337 __dma_map_area(page_address(page) + off, size, dir);
338}
339
340static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off,
341 size_t size, enum dma_data_direction dir)
342{
343 __dma_unmap_area(page_address(page) + off, size, dir);
344
345 /* TODO: WHAT IS THIS? */
346 /*
347 * Mark the D-cache clean for this page to avoid extra flushing.
348 */
349 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
350 set_bit(PG_dcache_clean, &page->flags);
351}
352
353static int __fast_dma_direction_to_prot(enum dma_data_direction dir)
354{
355 switch (dir) {
356 case DMA_BIDIRECTIONAL:
357 return IOMMU_READ | IOMMU_WRITE;
358 case DMA_TO_DEVICE:
359 return IOMMU_READ;
360 case DMA_FROM_DEVICE:
361 return IOMMU_WRITE;
362 default:
363 return 0;
364 }
365}
366
367static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page,
368 unsigned long offset, size_t size,
369 enum dma_data_direction dir,
370 unsigned long attrs)
371{
372 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
373 dma_addr_t iova;
374 unsigned long flags;
375 av8l_fast_iopte *pmd;
376 phys_addr_t phys_plus_off = page_to_phys(page) + offset;
377 phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE);
378 unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK;
379 size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE);
380 int nptes = len >> FAST_PAGE_SHIFT;
381 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
382 int prot = __fast_dma_direction_to_prot(dir);
Liam Mark83a9f86e2017-02-08 09:37:17 -0800383 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700384
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700385 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700386
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700387 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700388 __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map),
389 offset_from_phys_to_map, size, dir);
390
391 spin_lock_irqsave(&mapping->lock, flags);
392
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800393 iova = __fast_smmu_alloc_iova(mapping, attrs, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700394
395 if (unlikely(iova == DMA_ERROR_CODE))
396 goto fail;
397
398 pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
399
400 if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot)))
401 goto fail_free_iova;
402
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700403 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700404
405 spin_unlock_irqrestore(&mapping->lock, flags);
406 return iova + offset_from_phys_to_map;
407
408fail_free_iova:
409 __fast_smmu_free_iova(mapping, iova, size);
410fail:
411 spin_unlock_irqrestore(&mapping->lock, flags);
412 return DMA_ERROR_CODE;
413}
414
415static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova,
416 size_t size, enum dma_data_direction dir,
417 unsigned long attrs)
418{
419 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
420 unsigned long flags;
421 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
422 unsigned long offset = iova & ~FAST_PAGE_MASK;
423 size_t len = ALIGN(size + offset, FAST_PAGE_SIZE);
424 int nptes = len >> FAST_PAGE_SHIFT;
425 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
426 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
Liam Mark83a9f86e2017-02-08 09:37:17 -0800427 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700428
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700429 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700430 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
431
432 spin_lock_irqsave(&mapping->lock, flags);
433 av8l_fast_unmap_public(pmd, len);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700434 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700435 __fast_smmu_free_iova(mapping, iova, len);
436 spin_unlock_irqrestore(&mapping->lock, flags);
437}
438
Liam Mark78d7fb52016-12-01 13:05:31 -0800439static void fast_smmu_sync_single_for_cpu(struct device *dev,
440 dma_addr_t iova, size_t size, enum dma_data_direction dir)
441{
442 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
443 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
444 unsigned long offset = iova & ~FAST_PAGE_MASK;
445 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
446
Liam Mark83a9f86e2017-02-08 09:37:17 -0800447 if (!__fast_is_pte_coherent(pmd))
Liam Mark78d7fb52016-12-01 13:05:31 -0800448 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
449}
450
451static void fast_smmu_sync_single_for_device(struct device *dev,
452 dma_addr_t iova, size_t size, enum dma_data_direction dir)
453{
454 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
455 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
456 unsigned long offset = iova & ~FAST_PAGE_MASK;
457 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
458
Liam Mark83a9f86e2017-02-08 09:37:17 -0800459 if (!__fast_is_pte_coherent(pmd))
Liam Mark78d7fb52016-12-01 13:05:31 -0800460 __fast_dma_page_cpu_to_dev(page, offset, size, dir);
461}
462
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700463static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
464 int nents, enum dma_data_direction dir,
465 unsigned long attrs)
466{
Patrick Daly36c547a2017-09-06 19:13:02 -0700467 /* 0 indicates error */
468 return 0;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700469}
470
471static void fast_smmu_unmap_sg(struct device *dev,
472 struct scatterlist *sg, int nents,
473 enum dma_data_direction dir,
474 unsigned long attrs)
475{
476 WARN_ON_ONCE(1);
477}
478
Liam Mark78d7fb52016-12-01 13:05:31 -0800479static void fast_smmu_sync_sg_for_cpu(struct device *dev,
480 struct scatterlist *sg, int nents, enum dma_data_direction dir)
481{
482 WARN_ON_ONCE(1);
483}
484
485static void fast_smmu_sync_sg_for_device(struct device *dev,
486 struct scatterlist *sg, int nents, enum dma_data_direction dir)
487{
488 WARN_ON_ONCE(1);
489}
490
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700491static void __fast_smmu_free_pages(struct page **pages, int count)
492{
493 int i;
494
495 for (i = 0; i < count; i++)
496 __free_page(pages[i]);
497 kvfree(pages);
498}
499
500static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
501{
502 struct page **pages;
503 unsigned int i = 0, array_size = count * sizeof(*pages);
504
505 if (array_size <= PAGE_SIZE)
506 pages = kzalloc(array_size, GFP_KERNEL);
507 else
508 pages = vzalloc(array_size);
509 if (!pages)
510 return NULL;
511
512 /* IOMMU can map any pages, so himem can also be used here */
513 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
514
515 for (i = 0; i < count; ++i) {
516 struct page *page = alloc_page(gfp);
517
518 if (!page) {
519 __fast_smmu_free_pages(pages, i);
520 return NULL;
521 }
522 pages[i] = page;
523 }
524 return pages;
525}
526
527static void *fast_smmu_alloc(struct device *dev, size_t size,
528 dma_addr_t *handle, gfp_t gfp,
529 unsigned long attrs)
530{
531 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
532 struct sg_table sgt;
533 dma_addr_t dma_addr, iova_iter;
534 void *addr;
535 av8l_fast_iopte *ptep;
536 unsigned long flags;
537 struct sg_mapping_iter miter;
538 unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
539 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
Liam Mark83a9f86e2017-02-08 09:37:17 -0800540 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700541 pgprot_t remap_prot = __get_dma_pgprot(attrs, PAGE_KERNEL, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700542 struct page **pages;
543
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700544 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
545
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700546 *handle = DMA_ERROR_CODE;
547
548 pages = __fast_smmu_alloc_pages(count, gfp);
549 if (!pages) {
550 dev_err(dev, "no pages\n");
551 return NULL;
552 }
553
554 size = ALIGN(size, SZ_4K);
555 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
556 dev_err(dev, "no sg tablen\n");
557 goto out_free_pages;
558 }
559
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700560 if (!is_coherent) {
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700561 /*
562 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
563 * sufficient here, so skip it by using the "wrong" direction.
564 */
565 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
566 SG_MITER_FROM_SG);
567 while (sg_miter_next(&miter))
Kyle Yan65be4a52016-10-31 15:05:00 -0700568 __dma_flush_area(miter.addr, miter.length);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700569 sg_miter_stop(&miter);
570 }
571
572 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800573 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700574 if (dma_addr == DMA_ERROR_CODE) {
575 dev_err(dev, "no iova\n");
576 spin_unlock_irqrestore(&mapping->lock, flags);
577 goto out_free_sg;
578 }
579 iova_iter = dma_addr;
580 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
581 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
582 while (sg_miter_next(&miter)) {
583 int nptes = miter.length >> FAST_PAGE_SHIFT;
584
585 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
586 if (unlikely(av8l_fast_map_public(
587 ptep, page_to_phys(miter.page),
588 miter.length, prot))) {
589 dev_err(dev, "no map public\n");
590 /* TODO: unwind previously successful mappings */
591 goto out_free_iova;
592 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700593 fast_dmac_clean_range(mapping, ptep, ptep + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700594 iova_iter += miter.length;
595 }
596 sg_miter_stop(&miter);
597 spin_unlock_irqrestore(&mapping->lock, flags);
598
599 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
600 __builtin_return_address(0));
601 if (!addr) {
602 dev_err(dev, "no common pages\n");
603 goto out_unmap;
604 }
605
606 *handle = dma_addr;
607 sg_free_table(&sgt);
608 return addr;
609
610out_unmap:
611 /* need to take the lock again for page tables and iova */
612 spin_lock_irqsave(&mapping->lock, flags);
613 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
614 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700615 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700616out_free_iova:
617 __fast_smmu_free_iova(mapping, dma_addr, size);
618 spin_unlock_irqrestore(&mapping->lock, flags);
619out_free_sg:
620 sg_free_table(&sgt);
621out_free_pages:
622 __fast_smmu_free_pages(pages, count);
623 return NULL;
624}
625
626static void fast_smmu_free(struct device *dev, size_t size,
627 void *vaddr, dma_addr_t dma_handle,
628 unsigned long attrs)
629{
630 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
631 struct vm_struct *area;
632 struct page **pages;
633 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
634 av8l_fast_iopte *ptep;
635 unsigned long flags;
636
637 size = ALIGN(size, SZ_4K);
638
639 area = find_vm_area(vaddr);
640 if (WARN_ON_ONCE(!area))
641 return;
642
643 pages = area->pages;
644 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
645 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
646 spin_lock_irqsave(&mapping->lock, flags);
647 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700648 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700649 __fast_smmu_free_iova(mapping, dma_handle, size);
650 spin_unlock_irqrestore(&mapping->lock, flags);
651 __fast_smmu_free_pages(pages, count);
652}
653
Patrick Daly7bcb5462016-08-03 17:27:36 -0700654static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
655 void *cpu_addr, dma_addr_t dma_addr,
656 size_t size, unsigned long attrs)
657{
658 struct vm_struct *area;
659 unsigned long uaddr = vma->vm_start;
660 struct page **pages;
661 int i, nr_pages, ret = 0;
Liam Mark83a9f86e2017-02-08 09:37:17 -0800662 bool coherent = is_dma_coherent(dev, attrs);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700663
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700664 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
665 coherent);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700666 area = find_vm_area(cpu_addr);
667 if (!area)
668 return -EINVAL;
669
670 pages = area->pages;
671 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
672 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
673 ret = vm_insert_page(vma, uaddr, pages[i]);
674 if (ret)
675 break;
676 uaddr += PAGE_SIZE;
677 }
678
679 return ret;
680}
681
Patrick Daly9c79f382017-06-12 18:15:25 -0700682static int fast_smmu_get_sgtable(struct device *dev, struct sg_table *sgt,
683 void *cpu_addr, dma_addr_t dma_addr,
684 size_t size, unsigned long attrs)
685{
686 unsigned int n_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
687 struct vm_struct *area;
688
689 area = find_vm_area(cpu_addr);
690 if (!area || !area->pages)
691 return -EINVAL;
692
693 return sg_alloc_table_from_pages(sgt, area->pages, n_pages, 0, size,
694 GFP_KERNEL);
695}
696
Patrick Daly199fa672017-05-04 15:30:16 -0700697static dma_addr_t fast_smmu_dma_map_resource(
698 struct device *dev, phys_addr_t phys_addr,
699 size_t size, enum dma_data_direction dir,
700 unsigned long attrs)
701{
702 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
703 size_t offset = phys_addr & ~FAST_PAGE_MASK;
704 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
705 dma_addr_t dma_addr;
706 int prot;
707 unsigned long flags;
708
709 spin_lock_irqsave(&mapping->lock, flags);
710 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, len);
711 spin_unlock_irqrestore(&mapping->lock, flags);
712
713 if (dma_addr == DMA_ERROR_CODE)
714 return dma_addr;
715
716 prot = __fast_dma_direction_to_prot(dir);
717 prot |= IOMMU_MMIO;
718
719 if (iommu_map(mapping->domain, dma_addr, phys_addr - offset,
720 len, prot)) {
721 spin_lock_irqsave(&mapping->lock, flags);
722 __fast_smmu_free_iova(mapping, dma_addr, len);
723 spin_unlock_irqrestore(&mapping->lock, flags);
724 return DMA_ERROR_CODE;
725 }
726 return dma_addr + offset;
727}
728
729static void fast_smmu_dma_unmap_resource(
730 struct device *dev, dma_addr_t addr,
731 size_t size, enum dma_data_direction dir,
732 unsigned long attrs)
733{
734 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
735 size_t offset = addr & ~FAST_PAGE_MASK;
736 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
737 unsigned long flags;
738
739 iommu_unmap(mapping->domain, addr - offset, len);
740 spin_lock_irqsave(&mapping->lock, flags);
741 __fast_smmu_free_iova(mapping, addr, len);
742 spin_unlock_irqrestore(&mapping->lock, flags);
743}
744
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700745static int fast_smmu_mapping_error(struct device *dev,
746 dma_addr_t dma_addr)
747{
748 return dma_addr == DMA_ERROR_CODE;
749}
750
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800751static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
752 void *data)
753{
754 av8l_fast_iopte *ptep = data;
755 dma_addr_t iova;
756 unsigned long bitmap_idx;
757
758 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
759 iova = bitmap_idx << FAST_PAGE_SHIFT;
760 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
761 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
762 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530763 fast->pgtbl_pmds, bitmap_idx);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800764 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
765 32, 8, fast->bitmap, fast->bitmap_size, false);
766}
767
768static int fast_smmu_notify(struct notifier_block *self,
769 unsigned long action, void *data)
770{
771 struct dma_fast_smmu_mapping *fast = container_of(
772 self, struct dma_fast_smmu_mapping, notifier);
773
774 switch (action) {
775 case MAPPED_OVER_STALE_TLB:
776 __fast_smmu_mapped_over_stale(fast, data);
777 return NOTIFY_OK;
778 default:
779 WARN(1, "Unhandled notifier action");
780 return NOTIFY_DONE;
781 }
782}
783
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700784static const struct dma_map_ops fast_smmu_dma_ops = {
785 .alloc = fast_smmu_alloc,
786 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700787 .mmap = fast_smmu_mmap_attrs,
Patrick Daly9c79f382017-06-12 18:15:25 -0700788 .get_sgtable = fast_smmu_get_sgtable,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700789 .map_page = fast_smmu_map_page,
790 .unmap_page = fast_smmu_unmap_page,
Liam Mark78d7fb52016-12-01 13:05:31 -0800791 .sync_single_for_cpu = fast_smmu_sync_single_for_cpu,
792 .sync_single_for_device = fast_smmu_sync_single_for_device,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700793 .map_sg = fast_smmu_map_sg,
794 .unmap_sg = fast_smmu_unmap_sg,
Liam Mark78d7fb52016-12-01 13:05:31 -0800795 .sync_sg_for_cpu = fast_smmu_sync_sg_for_cpu,
796 .sync_sg_for_device = fast_smmu_sync_sg_for_device,
Patrick Daly199fa672017-05-04 15:30:16 -0700797 .map_resource = fast_smmu_dma_map_resource,
798 .unmap_resource = fast_smmu_dma_unmap_resource,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700799 .mapping_error = fast_smmu_mapping_error,
800};
801
802/**
803 * __fast_smmu_create_mapping_sized
804 * @base: bottom of the VA range
805 * @size: size of the VA range in bytes
806 *
807 * Creates a mapping structure which holds information about used/unused IO
808 * address ranges, which is required to perform mapping with IOMMU aware
809 * functions. The only VA range supported is [0, 4GB).
810 *
811 * The client device need to be attached to the mapping with
812 * fast_smmu_attach_device function.
813 */
814static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530815 dma_addr_t base, u64 size)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700816{
817 struct dma_fast_smmu_mapping *fast;
818
819 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
820 if (!fast)
821 goto err;
822
823 fast->base = base;
824 fast->size = size;
825 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
826 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
827
Liam Mark4d4fbba2017-02-08 10:30:49 -0800828 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL | __GFP_NOWARN |
829 __GFP_NORETRY);
830 if (!fast->bitmap)
831 fast->bitmap = vzalloc(fast->bitmap_size);
832
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700833 if (!fast->bitmap)
834 goto err2;
835
836 spin_lock_init(&fast->lock);
837
838 return fast;
839err2:
840 kfree(fast);
841err:
842 return ERR_PTR(-ENOMEM);
843}
844
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700845/*
846 * Based off of similar code from dma-iommu.c, but modified to use a different
847 * iova allocator
848 */
849static void fast_smmu_reserve_pci_windows(struct device *dev,
850 struct dma_fast_smmu_mapping *mapping)
851{
852 struct pci_host_bridge *bridge;
853 struct resource_entry *window;
854 phys_addr_t start, end;
855 struct pci_dev *pci_dev;
856 unsigned long flags;
857
858 if (!dev_is_pci(dev))
859 return;
860
861 pci_dev = to_pci_dev(dev);
862 bridge = pci_find_host_bridge(pci_dev->bus);
863
864 spin_lock_irqsave(&mapping->lock, flags);
865 resource_list_for_each_entry(window, &bridge->windows) {
866 if (resource_type(window->res) != IORESOURCE_MEM &&
867 resource_type(window->res) != IORESOURCE_IO)
868 continue;
869
870 start = round_down(window->res->start - window->offset,
871 FAST_PAGE_SIZE);
872 end = round_up(window->res->end - window->offset,
873 FAST_PAGE_SIZE);
874 start = max_t(unsigned long, mapping->base, start);
875 end = min_t(unsigned long, mapping->base + mapping->size, end);
876 if (start >= end)
877 continue;
878
879 dev_dbg(dev, "iova allocator reserved 0x%pa-0x%pa\n",
880 &start, &end);
881
882 start = (start - mapping->base) >> FAST_PAGE_SHIFT;
883 end = (end - mapping->base) >> FAST_PAGE_SHIFT;
884 bitmap_set(mapping->bitmap, start, end - start);
885 }
886 spin_unlock_irqrestore(&mapping->lock, flags);
887}
888
Patrick Daly23301482017-10-12 16:18:25 -0700889static int fast_smmu_errata_init(struct dma_iommu_mapping *mapping)
890{
891 struct dma_fast_smmu_mapping *fast = mapping->fast;
892 int vmid = VMID_HLOS;
893 int min_iova_align = 0;
894
895 iommu_domain_get_attr(mapping->domain,
896 DOMAIN_ATTR_QCOM_MMU500_ERRATA_MIN_ALIGN,
897 &min_iova_align);
898 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_SECURE_VMID, &vmid);
899 if (vmid >= VMID_LAST || vmid < 0)
900 vmid = VMID_HLOS;
901
902 if (min_iova_align) {
903 fast->min_iova_align = ARM_SMMU_MIN_IOVA_ALIGN;
904 fast->guard_page = arm_smmu_errata_get_guard_page(vmid);
905 if (!fast->guard_page)
906 return -ENOMEM;
907 }
908 return 0;
909}
910
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700911/**
Patrick Daly1748f082017-09-05 21:32:52 -0700912 * fast_smmu_init_mapping
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700913 * @dev: valid struct device pointer
914 * @mapping: io address space mapping structure (returned from
Patrick Daly1748f082017-09-05 21:32:52 -0700915 * arm_iommu_create_mapping)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700916 *
Patrick Daly1748f082017-09-05 21:32:52 -0700917 * Called the first time a device is attached to this mapping.
918 * Not for dma client use.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700919 */
Patrick Daly1748f082017-09-05 21:32:52 -0700920int fast_smmu_init_mapping(struct device *dev,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700921 struct dma_iommu_mapping *mapping)
922{
Patrick Daly0df84ac2017-10-11 17:32:41 -0700923 int err;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700924 struct iommu_domain *domain = mapping->domain;
925 struct iommu_pgtbl_info info;
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530926 u64 size = (u64)mapping->bits << PAGE_SHIFT;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700927
Patrick Daly1748f082017-09-05 21:32:52 -0700928 if (mapping->base + size > (SZ_1G * 4ULL)) {
929 dev_err(dev, "Iova end address too large\n");
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700930 return -EINVAL;
Patrick Daly1748f082017-09-05 21:32:52 -0700931 }
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700932
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700933 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
934 if (IS_ERR(mapping->fast))
935 return -ENOMEM;
936 mapping->fast->domain = domain;
937 mapping->fast->dev = dev;
938
Patrick Daly23301482017-10-12 16:18:25 -0700939 if (fast_smmu_errata_init(mapping))
940 goto release_mapping;
941
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700942 fast_smmu_reserve_pci_windows(dev, mapping->fast);
943
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700944 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
945 &info)) {
946 dev_err(dev, "Couldn't get page table info\n");
Patrick Daly1748f082017-09-05 21:32:52 -0700947 err = -EINVAL;
Patrick Daly121a9a12017-10-11 17:43:29 -0700948 goto release_mapping;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700949 }
950 mapping->fast->pgtbl_pmds = info.pmds;
951
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700952 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT,
Patrick Daly1748f082017-09-05 21:32:52 -0700953 &mapping->fast->is_smmu_pt_coherent)) {
954 err = -EINVAL;
Patrick Daly121a9a12017-10-11 17:43:29 -0700955 goto release_mapping;
Patrick Daly1748f082017-09-05 21:32:52 -0700956 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700957
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800958 mapping->fast->notifier.notifier_call = fast_smmu_notify;
959 av8l_register_notify(&mapping->fast->notifier);
960
Patrick Daly1748f082017-09-05 21:32:52 -0700961 mapping->ops = &fast_smmu_dma_ops;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700962 return 0;
Patrick Daly1748f082017-09-05 21:32:52 -0700963
Patrick Daly1748f082017-09-05 21:32:52 -0700964release_mapping:
965 kfree(mapping->fast->bitmap);
966 kfree(mapping->fast);
967 return err;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700968}
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700969
970/**
Patrick Daly1748f082017-09-05 21:32:52 -0700971 * fast_smmu_release_mapping
972 * @kref: dma_iommu_mapping->kref
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700973 *
Patrick Daly1748f082017-09-05 21:32:52 -0700974 * Cleans up the given iommu mapping.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700975 */
Patrick Daly1748f082017-09-05 21:32:52 -0700976void fast_smmu_release_mapping(struct kref *kref)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700977{
Patrick Daly1748f082017-09-05 21:32:52 -0700978 struct dma_iommu_mapping *mapping =
979 container_of(kref, struct dma_iommu_mapping, kref);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700980
Liam Mark4d4fbba2017-02-08 10:30:49 -0800981 kvfree(mapping->fast->bitmap);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700982 kfree(mapping->fast);
Patrick Daly1748f082017-09-05 21:32:52 -0700983 iommu_domain_free(mapping->domain);
984 kfree(mapping);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700985}