blob: 31482f99f37f797188792c032ac6b974856349f0 [file] [log] [blame]
Vijayanand Jitta8c234a52019-01-27 22:36:16 +05301/* Copyright (c) 2016-2019, 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 Dalyde1c64d2017-09-12 16:30:12 -070017#include <linux/pci.h>
Patrick Daly7bcb5462016-08-03 17:27:36 -070018#include <linux/vmalloc.h>
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070019#include <asm/cacheflush.h>
20#include <asm/dma-iommu.h>
Charan Teja Reddy29f61402017-02-09 20:44:29 +053021#include <linux/slab.h>
22#include <linux/vmalloc.h>
Patrick Daly1323e412017-10-05 21:16:25 -070023#include <trace/events/iommu.h>
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070024
Patrick Daly23301482017-10-12 16:18:25 -070025#include <soc/qcom/secure_buffer.h>
26#include <linux/arm-smmu-errata.h>
27
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070028/* some redundant definitions... :( TODO: move to io-pgtable-fast.h */
29#define FAST_PAGE_SHIFT 12
30#define FAST_PAGE_SIZE (1UL << FAST_PAGE_SHIFT)
31#define FAST_PAGE_MASK (~(PAGE_SIZE - 1))
32#define FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000)
Liam Mark83a9f86e2017-02-08 09:37:17 -080033#define FAST_MAIR_ATTR_IDX_CACHE 1
34#define FAST_PTE_ATTRINDX_SHIFT 2
35#define FAST_PTE_ATTRINDX_MASK 0x7
36#define FAST_PTE_SH_SHIFT 8
37#define FAST_PTE_SH_MASK (((av8l_fast_iopte)0x3) << FAST_PTE_SH_SHIFT)
38#define FAST_PTE_SH_OS (((av8l_fast_iopte)2) << FAST_PTE_SH_SHIFT)
39#define FAST_PTE_SH_IS (((av8l_fast_iopte)3) << FAST_PTE_SH_SHIFT)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070040
Mitchel Humpherys425d03d2016-06-23 13:25:12 -070041static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
42 bool coherent)
43{
44 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
45 return pgprot_noncached(prot);
46 else if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
47 return pgprot_writecombine(prot);
48 return prot;
49}
50
51static int __get_iommu_pgprot(unsigned long attrs, int prot,
52 bool coherent)
53{
54 if (!(attrs & DMA_ATTR_EXEC_MAPPING))
55 prot |= IOMMU_NOEXEC;
56 if ((attrs & DMA_ATTR_STRONGLY_ORDERED))
57 prot |= IOMMU_MMIO;
58 if (coherent)
59 prot |= IOMMU_CACHE;
60
61 return prot;
62}
63
Mitchel Humpherys9de66db2016-06-07 11:09:44 -070064static void fast_dmac_clean_range(struct dma_fast_smmu_mapping *mapping,
65 void *start, void *end)
66{
67 if (!mapping->is_smmu_pt_coherent)
68 dmac_clean_range(start, end);
69}
70
Liam Mark83a9f86e2017-02-08 09:37:17 -080071static bool __fast_is_pte_coherent(av8l_fast_iopte *ptep)
72{
73 int attr_idx = (*ptep & (FAST_PTE_ATTRINDX_MASK <<
74 FAST_PTE_ATTRINDX_SHIFT)) >>
75 FAST_PTE_ATTRINDX_SHIFT;
76
77 if ((attr_idx == FAST_MAIR_ATTR_IDX_CACHE) &&
78 (((*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_IS) ||
79 (*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_OS))
80 return true;
81
82 return false;
83}
84
85static bool is_dma_coherent(struct device *dev, unsigned long attrs)
86{
87 bool is_coherent;
88
89 if (attrs & DMA_ATTR_FORCE_COHERENT)
90 is_coherent = true;
91 else if (attrs & DMA_ATTR_FORCE_NON_COHERENT)
92 is_coherent = false;
93 else if (is_device_dma_coherent(dev))
94 is_coherent = true;
95 else
96 is_coherent = false;
97
98 return is_coherent;
99}
100
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700101/*
102 * Checks if the allocated range (ending at @end) covered the upcoming
103 * stale bit. We don't need to know exactly where the range starts since
104 * we already know where the candidate search range started. If, starting
105 * from the beginning of the candidate search range, we had to step over
106 * (or landed directly on top of) the upcoming stale bit, then we return
107 * true.
108 *
109 * Due to wrapping, there are two scenarios we'll need to check: (1) if the
110 * range [search_start, upcoming_stale] spans 0 (i.e. search_start >
111 * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale]
112 * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each
113 * of those two scenarios we need to handle three cases: (1) the bit was
114 * found before wrapping or
115 */
116static bool __bit_covered_stale(unsigned long upcoming_stale,
117 unsigned long search_start,
118 unsigned long end)
119{
120 if (search_start > upcoming_stale) {
121 if (end >= search_start) {
122 /*
123 * We started searching above upcoming_stale and we
124 * didn't wrap, so we couldn't have crossed
125 * upcoming_stale.
126 */
127 return false;
128 }
129 /*
130 * We wrapped. Did we cross (or land on top of)
131 * upcoming_stale?
132 */
133 return end >= upcoming_stale;
134 }
135
136 if (search_start <= upcoming_stale) {
137 if (end >= search_start) {
138 /*
139 * We didn't wrap. Did we cross (or land on top
140 * of) upcoming_stale?
141 */
142 return end >= upcoming_stale;
143 }
144 /*
145 * We wrapped. So we must have crossed upcoming_stale
146 * (since we started searching below it).
147 */
148 return true;
149 }
150
151 /* we should have covered all logical combinations... */
152 WARN_ON(1);
153 return true;
154}
155
156static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping,
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800157 unsigned long attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700158 size_t size)
159{
Patrick Daly23301482017-10-12 16:18:25 -0700160 unsigned long bit, prev_search_start, nbits;
161 unsigned long align;
162 unsigned long guard_len;
163 dma_addr_t iova;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700164
Patrick Daly23301482017-10-12 16:18:25 -0700165 if (mapping->min_iova_align)
Prakash Guptac2e909a2018-03-29 11:23:06 +0530166 guard_len = ALIGN(size + mapping->force_guard_page_len,
167 mapping->min_iova_align) - size;
Patrick Daly23301482017-10-12 16:18:25 -0700168 else
169 guard_len = 0;
170
171 nbits = (size + guard_len) >> FAST_PAGE_SHIFT;
172 align = (1 << get_order(size + guard_len)) - 1;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700173 bit = bitmap_find_next_zero_area(
174 mapping->bitmap, mapping->num_4k_pages, mapping->next_start,
175 nbits, align);
176 if (unlikely(bit > mapping->num_4k_pages)) {
177 /* try wrapping */
178 mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */
179 bit = bitmap_find_next_zero_area(
180 mapping->bitmap, mapping->num_4k_pages, 0, nbits,
181 align);
182 if (unlikely(bit > mapping->num_4k_pages))
183 return DMA_ERROR_CODE;
184 }
185
186 bitmap_set(mapping->bitmap, bit, nbits);
187 prev_search_start = mapping->next_start;
188 mapping->next_start = bit + nbits;
189 if (unlikely(mapping->next_start >= mapping->num_4k_pages))
190 mapping->next_start = 0;
191
192 /*
193 * If we just re-allocated a VA whose TLB hasn't been invalidated
194 * since it was last used and unmapped, we need to invalidate it
195 * here. We actually invalidate the entire TLB so that we don't
196 * have to invalidate the TLB again until we wrap back around.
197 */
198 if (mapping->have_stale_tlbs &&
199 __bit_covered_stale(mapping->upcoming_stale_bit,
200 prev_search_start,
201 bit + nbits - 1)) {
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800202 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
203
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700204 iommu_tlbiall(mapping->domain);
205 mapping->have_stale_tlbs = false;
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800206 av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700207 }
208
Patrick Daly23301482017-10-12 16:18:25 -0700209 iova = (bit << FAST_PAGE_SHIFT) + mapping->base;
210 if (guard_len &&
211 iommu_map(mapping->domain, iova + size,
212 page_to_phys(mapping->guard_page),
213 guard_len, ARM_SMMU_GUARD_PROT)) {
214
215 bitmap_clear(mapping->bitmap, bit, nbits);
216 return DMA_ERROR_CODE;
217 }
218 return iova;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700219}
220
221/*
222 * Checks whether the candidate bit will be allocated sooner than the
223 * current upcoming stale bit. We can say candidate will be upcoming
224 * sooner than the current upcoming stale bit if it lies between the
225 * starting bit of the next search range and the upcoming stale bit
226 * (allowing for wrap-around).
227 *
228 * Stated differently, we're checking the relative ordering of three
229 * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations,
230 * namely:
231 *
232 * 0 |---A---B---C---| TOP (Case 1)
233 * 0 |---A---C---B---| TOP (Case 2)
234 * 0 |---B---A---C---| TOP (Case 3)
235 * 0 |---B---C---A---| TOP (Case 4)
236 * 0 |---C---A---B---| TOP (Case 5)
237 * 0 |---C---B---A---| TOP (Case 6)
238 *
239 * Note that since we're allowing numbers to wrap, the following three
240 * scenarios are all equivalent for Case 1:
241 *
242 * 0 |---A---B---C---| TOP
243 * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.)
244 * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.)
245 *
246 * In any of these cases, if we start searching from A, we will find B
247 * before we find C.
248 *
249 * We can also find two equivalent cases for Case 2:
250 *
251 * 0 |---A---C---B---| TOP
252 * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.)
253 * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.)
254 *
255 * In any of these cases, if we start searching from A, we will find C
256 * before we find B.
257 */
258static bool __bit_is_sooner(unsigned long candidate,
259 struct dma_fast_smmu_mapping *mapping)
260{
261 unsigned long A = mapping->next_start;
262 unsigned long B = candidate;
263 unsigned long C = mapping->upcoming_stale_bit;
264
265 if ((A < B && B < C) || /* Case 1 */
266 (C < A && A < B) || /* Case 5 */
267 (B < C && C < A)) /* Case 4 */
268 return true;
269
270 if ((A < C && C < B) || /* Case 2 */
271 (B < A && A < C) || /* Case 3 */
272 (C < B && B < A)) /* Case 6 */
273 return false;
274
275 /*
276 * For simplicity, we've been ignoring the possibility of any of
277 * our three numbers being equal. Handle those cases here (they
278 * shouldn't happen very often, (I think?)).
279 */
280
281 /*
282 * If candidate is the next bit to be searched then it's definitely
283 * sooner.
284 */
285 if (A == B)
286 return true;
287
288 /*
289 * If candidate is the next upcoming stale bit we'll return false
290 * to avoid doing `upcoming = candidate' in the caller (which would
291 * be useless since they're already equal)
292 */
293 if (B == C)
294 return false;
295
296 /*
297 * If next start is the upcoming stale bit then candidate can't
298 * possibly be sooner. The "soonest" bit is already selected.
299 */
300 if (A == C)
301 return false;
302
303 /* We should have covered all logical combinations. */
304 WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C);
305 return true;
306}
307
308static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping,
309 dma_addr_t iova, size_t size)
310{
311 unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT;
Patrick Daly23301482017-10-12 16:18:25 -0700312 unsigned long nbits;
313 unsigned long guard_len;
314
Prakash Guptac2e909a2018-03-29 11:23:06 +0530315 if (mapping->min_iova_align)
316 guard_len = ALIGN(size + mapping->force_guard_page_len,
317 mapping->min_iova_align) - size;
318 else
Patrick Daly23301482017-10-12 16:18:25 -0700319 guard_len = 0;
Prakash Guptac2e909a2018-03-29 11:23:06 +0530320
321 if (guard_len)
322 iommu_unmap(mapping->domain, iova + size, guard_len);
323
Patrick Daly23301482017-10-12 16:18:25 -0700324 nbits = (size + guard_len) >> FAST_PAGE_SHIFT;
325
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700326
327 /*
328 * We don't invalidate TLBs on unmap. We invalidate TLBs on map
329 * when we're about to re-allocate a VA that was previously
330 * unmapped but hasn't yet been invalidated. So we need to keep
331 * track of which bit is the closest to being re-allocated here.
332 */
333 if (__bit_is_sooner(start_bit, mapping))
334 mapping->upcoming_stale_bit = start_bit;
335
336 bitmap_clear(mapping->bitmap, start_bit, nbits);
337 mapping->have_stale_tlbs = true;
338}
339
340
341static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off,
342 size_t size, enum dma_data_direction dir)
343{
344 __dma_map_area(page_address(page) + off, size, dir);
345}
346
347static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off,
348 size_t size, enum dma_data_direction dir)
349{
350 __dma_unmap_area(page_address(page) + off, size, dir);
351
352 /* TODO: WHAT IS THIS? */
353 /*
354 * Mark the D-cache clean for this page to avoid extra flushing.
355 */
356 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
357 set_bit(PG_dcache_clean, &page->flags);
358}
359
360static int __fast_dma_direction_to_prot(enum dma_data_direction dir)
361{
362 switch (dir) {
363 case DMA_BIDIRECTIONAL:
364 return IOMMU_READ | IOMMU_WRITE;
365 case DMA_TO_DEVICE:
366 return IOMMU_READ;
367 case DMA_FROM_DEVICE:
368 return IOMMU_WRITE;
369 default:
370 return 0;
371 }
372}
373
374static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page,
375 unsigned long offset, size_t size,
376 enum dma_data_direction dir,
377 unsigned long attrs)
378{
379 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
380 dma_addr_t iova;
381 unsigned long flags;
382 av8l_fast_iopte *pmd;
383 phys_addr_t phys_plus_off = page_to_phys(page) + offset;
384 phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE);
385 unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK;
386 size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE);
387 int nptes = len >> FAST_PAGE_SHIFT;
388 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
389 int prot = __fast_dma_direction_to_prot(dir);
Liam Mark83a9f86e2017-02-08 09:37:17 -0800390 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700391
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700392 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700393
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700394 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700395 __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map),
396 offset_from_phys_to_map, size, dir);
397
398 spin_lock_irqsave(&mapping->lock, flags);
399
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800400 iova = __fast_smmu_alloc_iova(mapping, attrs, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700401
402 if (unlikely(iova == DMA_ERROR_CODE))
403 goto fail;
404
405 pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
406
407 if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot)))
408 goto fail_free_iova;
409
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700410 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700411
412 spin_unlock_irqrestore(&mapping->lock, flags);
Patrick Daly1323e412017-10-05 21:16:25 -0700413
414 trace_map(mapping->domain, iova, phys_to_map, len, prot);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700415 return iova + offset_from_phys_to_map;
416
417fail_free_iova:
418 __fast_smmu_free_iova(mapping, iova, size);
419fail:
420 spin_unlock_irqrestore(&mapping->lock, flags);
421 return DMA_ERROR_CODE;
422}
423
424static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova,
425 size_t size, enum dma_data_direction dir,
426 unsigned long attrs)
427{
428 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
429 unsigned long flags;
430 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
431 unsigned long offset = iova & ~FAST_PAGE_MASK;
432 size_t len = ALIGN(size + offset, FAST_PAGE_SIZE);
433 int nptes = len >> FAST_PAGE_SHIFT;
434 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
435 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
Liam Mark83a9f86e2017-02-08 09:37:17 -0800436 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700437
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700438 if (!skip_sync && !is_coherent)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700439 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
440
441 spin_lock_irqsave(&mapping->lock, flags);
442 av8l_fast_unmap_public(pmd, len);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700443 fast_dmac_clean_range(mapping, pmd, pmd + nptes);
Patrick Dalyd79c4b92017-11-03 18:48:14 -0700444 __fast_smmu_free_iova(mapping, iova - offset, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700445 spin_unlock_irqrestore(&mapping->lock, flags);
Patrick Daly1323e412017-10-05 21:16:25 -0700446
447 trace_unmap(mapping->domain, iova - offset, len, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700448}
449
Liam Mark78d7fb52016-12-01 13:05:31 -0800450static void fast_smmu_sync_single_for_cpu(struct device *dev,
451 dma_addr_t iova, size_t size, enum dma_data_direction dir)
452{
453 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
454 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
455 unsigned long offset = iova & ~FAST_PAGE_MASK;
456 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
457
Liam Mark83a9f86e2017-02-08 09:37:17 -0800458 if (!__fast_is_pte_coherent(pmd))
Liam Mark78d7fb52016-12-01 13:05:31 -0800459 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
460}
461
462static void fast_smmu_sync_single_for_device(struct device *dev,
463 dma_addr_t iova, size_t size, enum dma_data_direction dir)
464{
465 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
466 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
467 unsigned long offset = iova & ~FAST_PAGE_MASK;
468 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
469
Liam Mark83a9f86e2017-02-08 09:37:17 -0800470 if (!__fast_is_pte_coherent(pmd))
Liam Mark78d7fb52016-12-01 13:05:31 -0800471 __fast_dma_page_cpu_to_dev(page, offset, size, dir);
472}
473
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700474static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
475 int nents, enum dma_data_direction dir,
476 unsigned long attrs)
477{
Patrick Daly36c547a2017-09-06 19:13:02 -0700478 /* 0 indicates error */
479 return 0;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700480}
481
482static void fast_smmu_unmap_sg(struct device *dev,
483 struct scatterlist *sg, int nents,
484 enum dma_data_direction dir,
485 unsigned long attrs)
486{
487 WARN_ON_ONCE(1);
488}
489
Liam Mark78d7fb52016-12-01 13:05:31 -0800490static void fast_smmu_sync_sg_for_cpu(struct device *dev,
491 struct scatterlist *sg, int nents, enum dma_data_direction dir)
492{
493 WARN_ON_ONCE(1);
494}
495
496static void fast_smmu_sync_sg_for_device(struct device *dev,
497 struct scatterlist *sg, int nents, enum dma_data_direction dir)
498{
499 WARN_ON_ONCE(1);
500}
501
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700502static void __fast_smmu_free_pages(struct page **pages, int count)
503{
504 int i;
505
506 for (i = 0; i < count; i++)
507 __free_page(pages[i]);
508 kvfree(pages);
509}
510
511static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
512{
513 struct page **pages;
514 unsigned int i = 0, array_size = count * sizeof(*pages);
515
516 if (array_size <= PAGE_SIZE)
517 pages = kzalloc(array_size, GFP_KERNEL);
518 else
519 pages = vzalloc(array_size);
520 if (!pages)
521 return NULL;
522
523 /* IOMMU can map any pages, so himem can also be used here */
524 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
525
526 for (i = 0; i < count; ++i) {
527 struct page *page = alloc_page(gfp);
528
529 if (!page) {
530 __fast_smmu_free_pages(pages, i);
531 return NULL;
532 }
533 pages[i] = page;
534 }
535 return pages;
536}
537
538static void *fast_smmu_alloc(struct device *dev, size_t size,
539 dma_addr_t *handle, gfp_t gfp,
540 unsigned long attrs)
541{
542 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
543 struct sg_table sgt;
544 dma_addr_t dma_addr, iova_iter;
545 void *addr;
546 av8l_fast_iopte *ptep;
547 unsigned long flags;
548 struct sg_mapping_iter miter;
Vijayanand Jitta8c234a52019-01-27 22:36:16 +0530549 size_t count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700550 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
Liam Mark83a9f86e2017-02-08 09:37:17 -0800551 bool is_coherent = is_dma_coherent(dev, attrs);
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700552 pgprot_t remap_prot = __get_dma_pgprot(attrs, PAGE_KERNEL, is_coherent);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700553 struct page **pages;
554
Vijayanand Jitta8c234a52019-01-27 22:36:16 +0530555 /*
556 * sg_alloc_table_from_pages accepts unsigned int value for count
557 * so check count doesn't exceed UINT_MAX.
558 */
559
560 if (count > UINT_MAX) {
561 dev_err(dev, "count: %zx exceeds UNIT_MAX\n", count);
562 return NULL;
563 }
564
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700565 prot = __get_iommu_pgprot(attrs, prot, is_coherent);
566
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700567 *handle = DMA_ERROR_CODE;
568
569 pages = __fast_smmu_alloc_pages(count, gfp);
570 if (!pages) {
571 dev_err(dev, "no pages\n");
572 return NULL;
573 }
574
575 size = ALIGN(size, SZ_4K);
576 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
577 dev_err(dev, "no sg tablen\n");
578 goto out_free_pages;
579 }
580
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700581 if (!is_coherent) {
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700582 /*
583 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
584 * sufficient here, so skip it by using the "wrong" direction.
585 */
586 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
587 SG_MITER_FROM_SG);
588 while (sg_miter_next(&miter))
Kyle Yan65be4a52016-10-31 15:05:00 -0700589 __dma_flush_area(miter.addr, miter.length);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700590 sg_miter_stop(&miter);
591 }
592
593 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800594 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700595 if (dma_addr == DMA_ERROR_CODE) {
596 dev_err(dev, "no iova\n");
597 spin_unlock_irqrestore(&mapping->lock, flags);
598 goto out_free_sg;
599 }
600 iova_iter = dma_addr;
601 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
602 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
603 while (sg_miter_next(&miter)) {
604 int nptes = miter.length >> FAST_PAGE_SHIFT;
605
606 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
607 if (unlikely(av8l_fast_map_public(
608 ptep, page_to_phys(miter.page),
609 miter.length, prot))) {
610 dev_err(dev, "no map public\n");
611 /* TODO: unwind previously successful mappings */
612 goto out_free_iova;
613 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700614 fast_dmac_clean_range(mapping, ptep, ptep + nptes);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700615 iova_iter += miter.length;
616 }
617 sg_miter_stop(&miter);
618 spin_unlock_irqrestore(&mapping->lock, flags);
619
620 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
621 __builtin_return_address(0));
622 if (!addr) {
623 dev_err(dev, "no common pages\n");
624 goto out_unmap;
625 }
626
627 *handle = dma_addr;
628 sg_free_table(&sgt);
629 return addr;
630
631out_unmap:
632 /* need to take the lock again for page tables and iova */
633 spin_lock_irqsave(&mapping->lock, flags);
634 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
635 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700636 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700637out_free_iova:
638 __fast_smmu_free_iova(mapping, dma_addr, size);
639 spin_unlock_irqrestore(&mapping->lock, flags);
640out_free_sg:
641 sg_free_table(&sgt);
642out_free_pages:
643 __fast_smmu_free_pages(pages, count);
644 return NULL;
645}
646
647static void fast_smmu_free(struct device *dev, size_t size,
648 void *vaddr, dma_addr_t dma_handle,
649 unsigned long attrs)
650{
651 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
652 struct vm_struct *area;
653 struct page **pages;
654 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
655 av8l_fast_iopte *ptep;
656 unsigned long flags;
657
658 size = ALIGN(size, SZ_4K);
659
660 area = find_vm_area(vaddr);
661 if (WARN_ON_ONCE(!area))
662 return;
663
664 pages = area->pages;
665 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
666 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
667 spin_lock_irqsave(&mapping->lock, flags);
668 av8l_fast_unmap_public(ptep, size);
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700669 fast_dmac_clean_range(mapping, ptep, ptep + count);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700670 __fast_smmu_free_iova(mapping, dma_handle, size);
671 spin_unlock_irqrestore(&mapping->lock, flags);
672 __fast_smmu_free_pages(pages, count);
673}
674
Patrick Daly7bcb5462016-08-03 17:27:36 -0700675static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
676 void *cpu_addr, dma_addr_t dma_addr,
677 size_t size, unsigned long attrs)
678{
679 struct vm_struct *area;
680 unsigned long uaddr = vma->vm_start;
681 struct page **pages;
682 int i, nr_pages, ret = 0;
Liam Mark83a9f86e2017-02-08 09:37:17 -0800683 bool coherent = is_dma_coherent(dev, attrs);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700684
Mitchel Humpherys425d03d2016-06-23 13:25:12 -0700685 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
686 coherent);
Patrick Daly7bcb5462016-08-03 17:27:36 -0700687 area = find_vm_area(cpu_addr);
688 if (!area)
689 return -EINVAL;
690
691 pages = area->pages;
692 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
693 for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) {
694 ret = vm_insert_page(vma, uaddr, pages[i]);
695 if (ret)
696 break;
697 uaddr += PAGE_SIZE;
698 }
699
700 return ret;
701}
702
Patrick Daly9c79f382017-06-12 18:15:25 -0700703static int fast_smmu_get_sgtable(struct device *dev, struct sg_table *sgt,
704 void *cpu_addr, dma_addr_t dma_addr,
705 size_t size, unsigned long attrs)
706{
707 unsigned int n_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
708 struct vm_struct *area;
709
710 area = find_vm_area(cpu_addr);
711 if (!area || !area->pages)
712 return -EINVAL;
713
714 return sg_alloc_table_from_pages(sgt, area->pages, n_pages, 0, size,
715 GFP_KERNEL);
716}
717
Patrick Daly199fa672017-05-04 15:30:16 -0700718static dma_addr_t fast_smmu_dma_map_resource(
719 struct device *dev, phys_addr_t phys_addr,
720 size_t size, enum dma_data_direction dir,
721 unsigned long attrs)
722{
723 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
724 size_t offset = phys_addr & ~FAST_PAGE_MASK;
725 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
726 dma_addr_t dma_addr;
727 int prot;
728 unsigned long flags;
729
730 spin_lock_irqsave(&mapping->lock, flags);
731 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, len);
732 spin_unlock_irqrestore(&mapping->lock, flags);
733
734 if (dma_addr == DMA_ERROR_CODE)
735 return dma_addr;
736
737 prot = __fast_dma_direction_to_prot(dir);
738 prot |= IOMMU_MMIO;
739
740 if (iommu_map(mapping->domain, dma_addr, phys_addr - offset,
741 len, prot)) {
742 spin_lock_irqsave(&mapping->lock, flags);
743 __fast_smmu_free_iova(mapping, dma_addr, len);
744 spin_unlock_irqrestore(&mapping->lock, flags);
745 return DMA_ERROR_CODE;
746 }
747 return dma_addr + offset;
748}
749
750static void fast_smmu_dma_unmap_resource(
751 struct device *dev, dma_addr_t addr,
752 size_t size, enum dma_data_direction dir,
753 unsigned long attrs)
754{
755 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
756 size_t offset = addr & ~FAST_PAGE_MASK;
757 size_t len = round_up(size + offset, FAST_PAGE_SIZE);
758 unsigned long flags;
759
760 iommu_unmap(mapping->domain, addr - offset, len);
761 spin_lock_irqsave(&mapping->lock, flags);
Patrick Dalyd79c4b92017-11-03 18:48:14 -0700762 __fast_smmu_free_iova(mapping, addr - offset, len);
Patrick Daly199fa672017-05-04 15:30:16 -0700763 spin_unlock_irqrestore(&mapping->lock, flags);
764}
765
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700766static int fast_smmu_mapping_error(struct device *dev,
767 dma_addr_t dma_addr)
768{
769 return dma_addr == DMA_ERROR_CODE;
770}
771
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800772static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
773 void *data)
774{
775 av8l_fast_iopte *ptep = data;
776 dma_addr_t iova;
777 unsigned long bitmap_idx;
778
779 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
780 iova = bitmap_idx << FAST_PAGE_SHIFT;
781 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
782 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
783 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530784 fast->pgtbl_pmds, bitmap_idx);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800785 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
786 32, 8, fast->bitmap, fast->bitmap_size, false);
787}
788
789static int fast_smmu_notify(struct notifier_block *self,
790 unsigned long action, void *data)
791{
792 struct dma_fast_smmu_mapping *fast = container_of(
793 self, struct dma_fast_smmu_mapping, notifier);
794
795 switch (action) {
796 case MAPPED_OVER_STALE_TLB:
797 __fast_smmu_mapped_over_stale(fast, data);
798 return NOTIFY_OK;
799 default:
800 WARN(1, "Unhandled notifier action");
801 return NOTIFY_DONE;
802 }
803}
804
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700805static const struct dma_map_ops fast_smmu_dma_ops = {
806 .alloc = fast_smmu_alloc,
807 .free = fast_smmu_free,
Patrick Daly7bcb5462016-08-03 17:27:36 -0700808 .mmap = fast_smmu_mmap_attrs,
Patrick Daly9c79f382017-06-12 18:15:25 -0700809 .get_sgtable = fast_smmu_get_sgtable,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700810 .map_page = fast_smmu_map_page,
811 .unmap_page = fast_smmu_unmap_page,
Liam Mark78d7fb52016-12-01 13:05:31 -0800812 .sync_single_for_cpu = fast_smmu_sync_single_for_cpu,
813 .sync_single_for_device = fast_smmu_sync_single_for_device,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700814 .map_sg = fast_smmu_map_sg,
815 .unmap_sg = fast_smmu_unmap_sg,
Liam Mark78d7fb52016-12-01 13:05:31 -0800816 .sync_sg_for_cpu = fast_smmu_sync_sg_for_cpu,
817 .sync_sg_for_device = fast_smmu_sync_sg_for_device,
Patrick Daly199fa672017-05-04 15:30:16 -0700818 .map_resource = fast_smmu_dma_map_resource,
819 .unmap_resource = fast_smmu_dma_unmap_resource,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700820 .mapping_error = fast_smmu_mapping_error,
821};
822
823/**
824 * __fast_smmu_create_mapping_sized
825 * @base: bottom of the VA range
826 * @size: size of the VA range in bytes
827 *
828 * Creates a mapping structure which holds information about used/unused IO
829 * address ranges, which is required to perform mapping with IOMMU aware
830 * functions. The only VA range supported is [0, 4GB).
831 *
832 * The client device need to be attached to the mapping with
833 * fast_smmu_attach_device function.
834 */
835static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530836 dma_addr_t base, u64 size)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700837{
838 struct dma_fast_smmu_mapping *fast;
839
840 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
841 if (!fast)
842 goto err;
843
844 fast->base = base;
845 fast->size = size;
846 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
847 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
848
Liam Mark4d4fbba2017-02-08 10:30:49 -0800849 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL | __GFP_NOWARN |
850 __GFP_NORETRY);
851 if (!fast->bitmap)
852 fast->bitmap = vzalloc(fast->bitmap_size);
853
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700854 if (!fast->bitmap)
855 goto err2;
856
857 spin_lock_init(&fast->lock);
858
859 return fast;
860err2:
861 kfree(fast);
862err:
863 return ERR_PTR(-ENOMEM);
864}
865
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700866/*
867 * Based off of similar code from dma-iommu.c, but modified to use a different
868 * iova allocator
869 */
870static void fast_smmu_reserve_pci_windows(struct device *dev,
871 struct dma_fast_smmu_mapping *mapping)
872{
873 struct pci_host_bridge *bridge;
874 struct resource_entry *window;
875 phys_addr_t start, end;
876 struct pci_dev *pci_dev;
877 unsigned long flags;
878
879 if (!dev_is_pci(dev))
880 return;
881
882 pci_dev = to_pci_dev(dev);
883 bridge = pci_find_host_bridge(pci_dev->bus);
884
885 spin_lock_irqsave(&mapping->lock, flags);
886 resource_list_for_each_entry(window, &bridge->windows) {
887 if (resource_type(window->res) != IORESOURCE_MEM &&
888 resource_type(window->res) != IORESOURCE_IO)
889 continue;
890
891 start = round_down(window->res->start - window->offset,
892 FAST_PAGE_SIZE);
893 end = round_up(window->res->end - window->offset,
894 FAST_PAGE_SIZE);
895 start = max_t(unsigned long, mapping->base, start);
896 end = min_t(unsigned long, mapping->base + mapping->size, end);
897 if (start >= end)
898 continue;
899
900 dev_dbg(dev, "iova allocator reserved 0x%pa-0x%pa\n",
901 &start, &end);
902
903 start = (start - mapping->base) >> FAST_PAGE_SHIFT;
904 end = (end - mapping->base) >> FAST_PAGE_SHIFT;
905 bitmap_set(mapping->bitmap, start, end - start);
906 }
907 spin_unlock_irqrestore(&mapping->lock, flags);
908}
909
Patrick Daly23301482017-10-12 16:18:25 -0700910static int fast_smmu_errata_init(struct dma_iommu_mapping *mapping)
911{
912 struct dma_fast_smmu_mapping *fast = mapping->fast;
913 int vmid = VMID_HLOS;
914 int min_iova_align = 0;
Prakash Guptac2e909a2018-03-29 11:23:06 +0530915 int force_iova_guard_page = 0;
Patrick Daly23301482017-10-12 16:18:25 -0700916
917 iommu_domain_get_attr(mapping->domain,
Prakash Guptac2e909a2018-03-29 11:23:06 +0530918 DOMAIN_ATTR_MMU500_ERRATA_MIN_ALIGN,
919 &min_iova_align);
Patrick Daly23301482017-10-12 16:18:25 -0700920 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_SECURE_VMID, &vmid);
Prakash Guptac2e909a2018-03-29 11:23:06 +0530921 iommu_domain_get_attr(mapping->domain,
922 DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE,
923 &force_iova_guard_page);
924
Patrick Daly23301482017-10-12 16:18:25 -0700925 if (vmid >= VMID_LAST || vmid < 0)
926 vmid = VMID_HLOS;
927
Prakash Guptac2e909a2018-03-29 11:23:06 +0530928 fast->min_iova_align = (min_iova_align) ? ARM_SMMU_MIN_IOVA_ALIGN :
929 PAGE_SIZE;
930
931 if (force_iova_guard_page)
932 fast->force_guard_page_len = PAGE_SIZE;
933
934 fast->guard_page =
935 arm_smmu_errata_get_guard_page(vmid);
936 if (!fast->guard_page)
937 return -ENOMEM;
938
Patrick Daly23301482017-10-12 16:18:25 -0700939 return 0;
940}
941
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700942/**
Patrick Daly1748f082017-09-05 21:32:52 -0700943 * fast_smmu_init_mapping
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700944 * @dev: valid struct device pointer
945 * @mapping: io address space mapping structure (returned from
Patrick Daly1748f082017-09-05 21:32:52 -0700946 * arm_iommu_create_mapping)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700947 *
Patrick Daly1748f082017-09-05 21:32:52 -0700948 * Called the first time a device is attached to this mapping.
949 * Not for dma client use.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700950 */
Patrick Daly1748f082017-09-05 21:32:52 -0700951int fast_smmu_init_mapping(struct device *dev,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700952 struct dma_iommu_mapping *mapping)
953{
Prakash Gupta83cc9bb42018-02-09 14:35:51 +0530954 int err = 0;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700955 struct iommu_domain *domain = mapping->domain;
956 struct iommu_pgtbl_info info;
Charan Teja Reddy29f61402017-02-09 20:44:29 +0530957 u64 size = (u64)mapping->bits << PAGE_SHIFT;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700958
Patrick Daly1748f082017-09-05 21:32:52 -0700959 if (mapping->base + size > (SZ_1G * 4ULL)) {
960 dev_err(dev, "Iova end address too large\n");
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700961 return -EINVAL;
Patrick Daly1748f082017-09-05 21:32:52 -0700962 }
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700963
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700964 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
965 if (IS_ERR(mapping->fast))
966 return -ENOMEM;
967 mapping->fast->domain = domain;
968 mapping->fast->dev = dev;
969
Patrick Daly23301482017-10-12 16:18:25 -0700970 if (fast_smmu_errata_init(mapping))
971 goto release_mapping;
972
Patrick Dalybc8b1cb2017-05-04 17:10:10 -0700973 fast_smmu_reserve_pci_windows(dev, mapping->fast);
974
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700975 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
976 &info)) {
977 dev_err(dev, "Couldn't get page table info\n");
Patrick Daly1748f082017-09-05 21:32:52 -0700978 err = -EINVAL;
Patrick Daly121a9a12017-10-11 17:43:29 -0700979 goto release_mapping;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700980 }
981 mapping->fast->pgtbl_pmds = info.pmds;
982
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700983 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT,
Patrick Daly1748f082017-09-05 21:32:52 -0700984 &mapping->fast->is_smmu_pt_coherent)) {
985 err = -EINVAL;
Patrick Daly121a9a12017-10-11 17:43:29 -0700986 goto release_mapping;
Patrick Daly1748f082017-09-05 21:32:52 -0700987 }
Mitchel Humpherys9de66db2016-06-07 11:09:44 -0700988
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800989 mapping->fast->notifier.notifier_call = fast_smmu_notify;
990 av8l_register_notify(&mapping->fast->notifier);
991
Patrick Daly1748f082017-09-05 21:32:52 -0700992 mapping->ops = &fast_smmu_dma_ops;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700993 return 0;
Patrick Daly1748f082017-09-05 21:32:52 -0700994
Patrick Daly1748f082017-09-05 21:32:52 -0700995release_mapping:
996 kfree(mapping->fast->bitmap);
997 kfree(mapping->fast);
998 return err;
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700999}
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001000
1001/**
Patrick Daly1748f082017-09-05 21:32:52 -07001002 * fast_smmu_release_mapping
1003 * @kref: dma_iommu_mapping->kref
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001004 *
Patrick Daly1748f082017-09-05 21:32:52 -07001005 * Cleans up the given iommu mapping.
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001006 */
Patrick Daly1748f082017-09-05 21:32:52 -07001007void fast_smmu_release_mapping(struct kref *kref)
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001008{
Patrick Daly1748f082017-09-05 21:32:52 -07001009 struct dma_iommu_mapping *mapping =
1010 container_of(kref, struct dma_iommu_mapping, kref);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001011
Liam Mark4d4fbba2017-02-08 10:30:49 -08001012 kvfree(mapping->fast->bitmap);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001013 kfree(mapping->fast);
Patrick Daly1748f082017-09-05 21:32:52 -07001014 iommu_domain_free(mapping->domain);
1015 kfree(mapping);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001016}