blob: 8b2be30dde50a425dc8658aeab53c72dd708cda8 [file] [log] [blame]
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -07001/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/dma-contiguous.h>
14#include <linux/dma-mapping.h>
15#include <linux/dma-mapping-fast.h>
16#include <linux/io-pgtable-fast.h>
17#include <asm/cacheflush.h>
18#include <asm/dma-iommu.h>
19
20
21/* some redundant definitions... :( TODO: move to io-pgtable-fast.h */
22#define FAST_PAGE_SHIFT 12
23#define FAST_PAGE_SIZE (1UL << FAST_PAGE_SHIFT)
24#define FAST_PAGE_MASK (~(PAGE_SIZE - 1))
25#define FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000)
26
27/*
28 * Checks if the allocated range (ending at @end) covered the upcoming
29 * stale bit. We don't need to know exactly where the range starts since
30 * we already know where the candidate search range started. If, starting
31 * from the beginning of the candidate search range, we had to step over
32 * (or landed directly on top of) the upcoming stale bit, then we return
33 * true.
34 *
35 * Due to wrapping, there are two scenarios we'll need to check: (1) if the
36 * range [search_start, upcoming_stale] spans 0 (i.e. search_start >
37 * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale]
38 * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each
39 * of those two scenarios we need to handle three cases: (1) the bit was
40 * found before wrapping or
41 */
42static bool __bit_covered_stale(unsigned long upcoming_stale,
43 unsigned long search_start,
44 unsigned long end)
45{
46 if (search_start > upcoming_stale) {
47 if (end >= search_start) {
48 /*
49 * We started searching above upcoming_stale and we
50 * didn't wrap, so we couldn't have crossed
51 * upcoming_stale.
52 */
53 return false;
54 }
55 /*
56 * We wrapped. Did we cross (or land on top of)
57 * upcoming_stale?
58 */
59 return end >= upcoming_stale;
60 }
61
62 if (search_start <= upcoming_stale) {
63 if (end >= search_start) {
64 /*
65 * We didn't wrap. Did we cross (or land on top
66 * of) upcoming_stale?
67 */
68 return end >= upcoming_stale;
69 }
70 /*
71 * We wrapped. So we must have crossed upcoming_stale
72 * (since we started searching below it).
73 */
74 return true;
75 }
76
77 /* we should have covered all logical combinations... */
78 WARN_ON(1);
79 return true;
80}
81
82static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping,
Mitchel Humpherys5c704e02015-12-21 15:06:34 -080083 unsigned long attrs,
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -070084 size_t size)
85{
86 unsigned long bit, prev_search_start, nbits = size >> FAST_PAGE_SHIFT;
87 unsigned long align = (1 << get_order(size)) - 1;
88
89 bit = bitmap_find_next_zero_area(
90 mapping->bitmap, mapping->num_4k_pages, mapping->next_start,
91 nbits, align);
92 if (unlikely(bit > mapping->num_4k_pages)) {
93 /* try wrapping */
94 mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */
95 bit = bitmap_find_next_zero_area(
96 mapping->bitmap, mapping->num_4k_pages, 0, nbits,
97 align);
98 if (unlikely(bit > mapping->num_4k_pages))
99 return DMA_ERROR_CODE;
100 }
101
102 bitmap_set(mapping->bitmap, bit, nbits);
103 prev_search_start = mapping->next_start;
104 mapping->next_start = bit + nbits;
105 if (unlikely(mapping->next_start >= mapping->num_4k_pages))
106 mapping->next_start = 0;
107
108 /*
109 * If we just re-allocated a VA whose TLB hasn't been invalidated
110 * since it was last used and unmapped, we need to invalidate it
111 * here. We actually invalidate the entire TLB so that we don't
112 * have to invalidate the TLB again until we wrap back around.
113 */
114 if (mapping->have_stale_tlbs &&
115 __bit_covered_stale(mapping->upcoming_stale_bit,
116 prev_search_start,
117 bit + nbits - 1)) {
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800118 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
119
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700120 iommu_tlbiall(mapping->domain);
121 mapping->have_stale_tlbs = false;
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800122 av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700123 }
124
125 return (bit << FAST_PAGE_SHIFT) + mapping->base;
126}
127
128/*
129 * Checks whether the candidate bit will be allocated sooner than the
130 * current upcoming stale bit. We can say candidate will be upcoming
131 * sooner than the current upcoming stale bit if it lies between the
132 * starting bit of the next search range and the upcoming stale bit
133 * (allowing for wrap-around).
134 *
135 * Stated differently, we're checking the relative ordering of three
136 * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations,
137 * namely:
138 *
139 * 0 |---A---B---C---| TOP (Case 1)
140 * 0 |---A---C---B---| TOP (Case 2)
141 * 0 |---B---A---C---| TOP (Case 3)
142 * 0 |---B---C---A---| TOP (Case 4)
143 * 0 |---C---A---B---| TOP (Case 5)
144 * 0 |---C---B---A---| TOP (Case 6)
145 *
146 * Note that since we're allowing numbers to wrap, the following three
147 * scenarios are all equivalent for Case 1:
148 *
149 * 0 |---A---B---C---| TOP
150 * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.)
151 * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.)
152 *
153 * In any of these cases, if we start searching from A, we will find B
154 * before we find C.
155 *
156 * We can also find two equivalent cases for Case 2:
157 *
158 * 0 |---A---C---B---| TOP
159 * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.)
160 * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.)
161 *
162 * In any of these cases, if we start searching from A, we will find C
163 * before we find B.
164 */
165static bool __bit_is_sooner(unsigned long candidate,
166 struct dma_fast_smmu_mapping *mapping)
167{
168 unsigned long A = mapping->next_start;
169 unsigned long B = candidate;
170 unsigned long C = mapping->upcoming_stale_bit;
171
172 if ((A < B && B < C) || /* Case 1 */
173 (C < A && A < B) || /* Case 5 */
174 (B < C && C < A)) /* Case 4 */
175 return true;
176
177 if ((A < C && C < B) || /* Case 2 */
178 (B < A && A < C) || /* Case 3 */
179 (C < B && B < A)) /* Case 6 */
180 return false;
181
182 /*
183 * For simplicity, we've been ignoring the possibility of any of
184 * our three numbers being equal. Handle those cases here (they
185 * shouldn't happen very often, (I think?)).
186 */
187
188 /*
189 * If candidate is the next bit to be searched then it's definitely
190 * sooner.
191 */
192 if (A == B)
193 return true;
194
195 /*
196 * If candidate is the next upcoming stale bit we'll return false
197 * to avoid doing `upcoming = candidate' in the caller (which would
198 * be useless since they're already equal)
199 */
200 if (B == C)
201 return false;
202
203 /*
204 * If next start is the upcoming stale bit then candidate can't
205 * possibly be sooner. The "soonest" bit is already selected.
206 */
207 if (A == C)
208 return false;
209
210 /* We should have covered all logical combinations. */
211 WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C);
212 return true;
213}
214
215static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping,
216 dma_addr_t iova, size_t size)
217{
218 unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT;
219 unsigned long nbits = size >> FAST_PAGE_SHIFT;
220
221 /*
222 * We don't invalidate TLBs on unmap. We invalidate TLBs on map
223 * when we're about to re-allocate a VA that was previously
224 * unmapped but hasn't yet been invalidated. So we need to keep
225 * track of which bit is the closest to being re-allocated here.
226 */
227 if (__bit_is_sooner(start_bit, mapping))
228 mapping->upcoming_stale_bit = start_bit;
229
230 bitmap_clear(mapping->bitmap, start_bit, nbits);
231 mapping->have_stale_tlbs = true;
232}
233
234
235static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off,
236 size_t size, enum dma_data_direction dir)
237{
238 __dma_map_area(page_address(page) + off, size, dir);
239}
240
241static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off,
242 size_t size, enum dma_data_direction dir)
243{
244 __dma_unmap_area(page_address(page) + off, size, dir);
245
246 /* TODO: WHAT IS THIS? */
247 /*
248 * Mark the D-cache clean for this page to avoid extra flushing.
249 */
250 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
251 set_bit(PG_dcache_clean, &page->flags);
252}
253
254static int __fast_dma_direction_to_prot(enum dma_data_direction dir)
255{
256 switch (dir) {
257 case DMA_BIDIRECTIONAL:
258 return IOMMU_READ | IOMMU_WRITE;
259 case DMA_TO_DEVICE:
260 return IOMMU_READ;
261 case DMA_FROM_DEVICE:
262 return IOMMU_WRITE;
263 default:
264 return 0;
265 }
266}
267
268static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page,
269 unsigned long offset, size_t size,
270 enum dma_data_direction dir,
271 unsigned long attrs)
272{
273 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
274 dma_addr_t iova;
275 unsigned long flags;
276 av8l_fast_iopte *pmd;
277 phys_addr_t phys_plus_off = page_to_phys(page) + offset;
278 phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE);
279 unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK;
280 size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE);
281 int nptes = len >> FAST_PAGE_SHIFT;
282 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
283 int prot = __fast_dma_direction_to_prot(dir);
284
285 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
286 prot |= IOMMU_MMIO;
287
288 if (!skip_sync)
289 __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map),
290 offset_from_phys_to_map, size, dir);
291
292 spin_lock_irqsave(&mapping->lock, flags);
293
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800294 iova = __fast_smmu_alloc_iova(mapping, attrs, len);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700295
296 if (unlikely(iova == DMA_ERROR_CODE))
297 goto fail;
298
299 pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
300
301 if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot)))
302 goto fail_free_iova;
303
304 if (!skip_sync) /* TODO: should ask SMMU if coherent */
305 dmac_clean_range(pmd, pmd + nptes);
306
307 spin_unlock_irqrestore(&mapping->lock, flags);
308 return iova + offset_from_phys_to_map;
309
310fail_free_iova:
311 __fast_smmu_free_iova(mapping, iova, size);
312fail:
313 spin_unlock_irqrestore(&mapping->lock, flags);
314 return DMA_ERROR_CODE;
315}
316
317static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova,
318 size_t size, enum dma_data_direction dir,
319 unsigned long attrs)
320{
321 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
322 unsigned long flags;
323 av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova);
324 unsigned long offset = iova & ~FAST_PAGE_MASK;
325 size_t len = ALIGN(size + offset, FAST_PAGE_SIZE);
326 int nptes = len >> FAST_PAGE_SHIFT;
327 struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK));
328 bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC);
329
330 if (!skip_sync)
331 __fast_dma_page_dev_to_cpu(page, offset, size, dir);
332
333 spin_lock_irqsave(&mapping->lock, flags);
334 av8l_fast_unmap_public(pmd, len);
335 if (!skip_sync) /* TODO: should ask SMMU if coherent */
336 dmac_clean_range(pmd, pmd + nptes);
337 __fast_smmu_free_iova(mapping, iova, len);
338 spin_unlock_irqrestore(&mapping->lock, flags);
339}
340
341static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg,
342 int nents, enum dma_data_direction dir,
343 unsigned long attrs)
344{
345 return -EINVAL;
346}
347
348static void fast_smmu_unmap_sg(struct device *dev,
349 struct scatterlist *sg, int nents,
350 enum dma_data_direction dir,
351 unsigned long attrs)
352{
353 WARN_ON_ONCE(1);
354}
355
356static void __fast_smmu_free_pages(struct page **pages, int count)
357{
358 int i;
359
360 for (i = 0; i < count; i++)
361 __free_page(pages[i]);
362 kvfree(pages);
363}
364
365static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp)
366{
367 struct page **pages;
368 unsigned int i = 0, array_size = count * sizeof(*pages);
369
370 if (array_size <= PAGE_SIZE)
371 pages = kzalloc(array_size, GFP_KERNEL);
372 else
373 pages = vzalloc(array_size);
374 if (!pages)
375 return NULL;
376
377 /* IOMMU can map any pages, so himem can also be used here */
378 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
379
380 for (i = 0; i < count; ++i) {
381 struct page *page = alloc_page(gfp);
382
383 if (!page) {
384 __fast_smmu_free_pages(pages, i);
385 return NULL;
386 }
387 pages[i] = page;
388 }
389 return pages;
390}
391
392static void *fast_smmu_alloc(struct device *dev, size_t size,
393 dma_addr_t *handle, gfp_t gfp,
394 unsigned long attrs)
395{
396 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
397 struct sg_table sgt;
398 dma_addr_t dma_addr, iova_iter;
399 void *addr;
400 av8l_fast_iopte *ptep;
401 unsigned long flags;
402 struct sg_mapping_iter miter;
403 unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT;
404 int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */
405 pgprot_t remap_prot = pgprot_writecombine(PAGE_KERNEL);
406 struct page **pages;
407
408 *handle = DMA_ERROR_CODE;
409
410 pages = __fast_smmu_alloc_pages(count, gfp);
411 if (!pages) {
412 dev_err(dev, "no pages\n");
413 return NULL;
414 }
415
416 size = ALIGN(size, SZ_4K);
417 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) {
418 dev_err(dev, "no sg tablen\n");
419 goto out_free_pages;
420 }
421
422 if (!(prot & IOMMU_CACHE)) {
423 /*
424 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
425 * sufficient here, so skip it by using the "wrong" direction.
426 */
427 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
428 SG_MITER_FROM_SG);
429 while (sg_miter_next(&miter))
430 __dma_flush_range(miter.addr,
431 miter.addr + miter.length);
432 sg_miter_stop(&miter);
433 }
434
435 spin_lock_irqsave(&mapping->lock, flags);
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800436 dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size);
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700437 if (dma_addr == DMA_ERROR_CODE) {
438 dev_err(dev, "no iova\n");
439 spin_unlock_irqrestore(&mapping->lock, flags);
440 goto out_free_sg;
441 }
442 iova_iter = dma_addr;
443 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents,
444 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
445 while (sg_miter_next(&miter)) {
446 int nptes = miter.length >> FAST_PAGE_SHIFT;
447
448 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter);
449 if (unlikely(av8l_fast_map_public(
450 ptep, page_to_phys(miter.page),
451 miter.length, prot))) {
452 dev_err(dev, "no map public\n");
453 /* TODO: unwind previously successful mappings */
454 goto out_free_iova;
455 }
456 dmac_clean_range(ptep, ptep + nptes);
457 iova_iter += miter.length;
458 }
459 sg_miter_stop(&miter);
460 spin_unlock_irqrestore(&mapping->lock, flags);
461
462 addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot,
463 __builtin_return_address(0));
464 if (!addr) {
465 dev_err(dev, "no common pages\n");
466 goto out_unmap;
467 }
468
469 *handle = dma_addr;
470 sg_free_table(&sgt);
471 return addr;
472
473out_unmap:
474 /* need to take the lock again for page tables and iova */
475 spin_lock_irqsave(&mapping->lock, flags);
476 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr);
477 av8l_fast_unmap_public(ptep, size);
478 dmac_clean_range(ptep, ptep + count);
479out_free_iova:
480 __fast_smmu_free_iova(mapping, dma_addr, size);
481 spin_unlock_irqrestore(&mapping->lock, flags);
482out_free_sg:
483 sg_free_table(&sgt);
484out_free_pages:
485 __fast_smmu_free_pages(pages, count);
486 return NULL;
487}
488
489static void fast_smmu_free(struct device *dev, size_t size,
490 void *vaddr, dma_addr_t dma_handle,
491 unsigned long attrs)
492{
493 struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast;
494 struct vm_struct *area;
495 struct page **pages;
496 size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT;
497 av8l_fast_iopte *ptep;
498 unsigned long flags;
499
500 size = ALIGN(size, SZ_4K);
501
502 area = find_vm_area(vaddr);
503 if (WARN_ON_ONCE(!area))
504 return;
505
506 pages = area->pages;
507 dma_common_free_remap(vaddr, size, VM_USERMAP, false);
508 ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle);
509 spin_lock_irqsave(&mapping->lock, flags);
510 av8l_fast_unmap_public(ptep, size);
511 dmac_clean_range(ptep, ptep + count);
512 __fast_smmu_free_iova(mapping, dma_handle, size);
513 spin_unlock_irqrestore(&mapping->lock, flags);
514 __fast_smmu_free_pages(pages, count);
515}
516
517static int fast_smmu_dma_supported(struct device *dev, u64 mask)
518{
519 return mask <= 0xffffffff;
520}
521
522static int fast_smmu_mapping_error(struct device *dev,
523 dma_addr_t dma_addr)
524{
525 return dma_addr == DMA_ERROR_CODE;
526}
527
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800528static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast,
529 void *data)
530{
531 av8l_fast_iopte *ptep = data;
532 dma_addr_t iova;
533 unsigned long bitmap_idx;
534
535 bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds);
536 iova = bitmap_idx << FAST_PAGE_SHIFT;
537 dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova);
538 dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx);
539 dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep,
540 fast->pgtbl_pmds, ptep - fast->pgtbl_pmds);
541 print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS,
542 32, 8, fast->bitmap, fast->bitmap_size, false);
543}
544
545static int fast_smmu_notify(struct notifier_block *self,
546 unsigned long action, void *data)
547{
548 struct dma_fast_smmu_mapping *fast = container_of(
549 self, struct dma_fast_smmu_mapping, notifier);
550
551 switch (action) {
552 case MAPPED_OVER_STALE_TLB:
553 __fast_smmu_mapped_over_stale(fast, data);
554 return NOTIFY_OK;
555 default:
556 WARN(1, "Unhandled notifier action");
557 return NOTIFY_DONE;
558 }
559}
560
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700561static const struct dma_map_ops fast_smmu_dma_ops = {
562 .alloc = fast_smmu_alloc,
563 .free = fast_smmu_free,
564 .map_page = fast_smmu_map_page,
565 .unmap_page = fast_smmu_unmap_page,
566 .map_sg = fast_smmu_map_sg,
567 .unmap_sg = fast_smmu_unmap_sg,
568 .dma_supported = fast_smmu_dma_supported,
569 .mapping_error = fast_smmu_mapping_error,
570};
571
572/**
573 * __fast_smmu_create_mapping_sized
574 * @base: bottom of the VA range
575 * @size: size of the VA range in bytes
576 *
577 * Creates a mapping structure which holds information about used/unused IO
578 * address ranges, which is required to perform mapping with IOMMU aware
579 * functions. The only VA range supported is [0, 4GB).
580 *
581 * The client device need to be attached to the mapping with
582 * fast_smmu_attach_device function.
583 */
584static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized(
585 dma_addr_t base, size_t size)
586{
587 struct dma_fast_smmu_mapping *fast;
588
589 fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL);
590 if (!fast)
591 goto err;
592
593 fast->base = base;
594 fast->size = size;
595 fast->num_4k_pages = size >> FAST_PAGE_SHIFT;
596 fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long);
597
598 fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL);
599 if (!fast->bitmap)
600 goto err2;
601
602 spin_lock_init(&fast->lock);
603
604 return fast;
605err2:
606 kfree(fast);
607err:
608 return ERR_PTR(-ENOMEM);
609}
610
611
612#define PGTBL_MEM_SIZE (SZ_4K + (4 * SZ_4K) + (2048 * SZ_4K))
613
614
615/**
616 * fast_smmu_attach_device
617 * @dev: valid struct device pointer
618 * @mapping: io address space mapping structure (returned from
619 * fast_smmu_create_mapping)
620 *
621 * Attaches specified io address space mapping to the provided device,
622 * this replaces the dma operations (dma_map_ops pointer) with the
623 * IOMMU aware version. More than one client might be attached to
624 * the same io address space mapping.
625 */
626int fast_smmu_attach_device(struct device *dev,
627 struct dma_iommu_mapping *mapping)
628{
629 int atomic_domain = 1;
630 struct iommu_domain *domain = mapping->domain;
631 struct iommu_pgtbl_info info;
632 size_t size = mapping->bits << PAGE_SHIFT;
633
634 if (mapping->base + size > (SZ_1G * 4ULL))
635 return -EINVAL;
636
637 if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC,
638 &atomic_domain))
639 return -EINVAL;
640
641 mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size);
642 if (IS_ERR(mapping->fast))
643 return -ENOMEM;
644 mapping->fast->domain = domain;
645 mapping->fast->dev = dev;
646
647 if (iommu_attach_device(domain, dev))
648 return -EINVAL;
649
650 if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO,
651 &info)) {
652 dev_err(dev, "Couldn't get page table info\n");
653 fast_smmu_detach_device(dev, mapping);
654 return -EINVAL;
655 }
656 mapping->fast->pgtbl_pmds = info.pmds;
657
Mitchel Humpherys5c704e02015-12-21 15:06:34 -0800658 mapping->fast->notifier.notifier_call = fast_smmu_notify;
659 av8l_register_notify(&mapping->fast->notifier);
660
Mitchel Humpherys0e43f0a2015-10-08 15:03:09 -0700661 dev->archdata.mapping = mapping;
662 set_dma_ops(dev, &fast_smmu_dma_ops);
663
664 return 0;
665}
666EXPORT_SYMBOL(fast_smmu_attach_device);
667
668/**
669 * fast_smmu_detach_device
670 * @dev: valid struct device pointer
671 *
672 * Detaches the provided device from a previously attached map.
673 * This voids the dma operations (dma_map_ops pointer)
674 */
675void fast_smmu_detach_device(struct device *dev,
676 struct dma_iommu_mapping *mapping)
677{
678 iommu_detach_device(mapping->domain, dev);
679 dev->archdata.mapping = NULL;
680 set_dma_ops(dev, NULL);
681
682 kfree(mapping->fast->bitmap);
683 kfree(mapping->fast);
684}
685EXPORT_SYMBOL(fast_smmu_detach_device);