-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexynos-iovmm.c
More file actions
831 lines (682 loc) · 21.8 KB
/
exynos-iovmm.c
File metadata and controls
831 lines (682 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/* linux/drivers/iommu/exynos_iovmm.c
*
* Copyright (c) 2011-2012 Samsung Electronics Co., Ltd.
* http://www.samsung.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
#define DEBUG
#endif
#include <linux/kernel.h>
#include <linux/hardirq.h>
#include <linux/slab.h>
#include <linux/scatterlist.h>
#include <linux/err.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/exynos_iovmm.h>
#include "exynos-iommu.h"
#define sg_physically_continuous(sg) (sg_next(sg) == NULL)
/* alloc_iovm_region - Allocate IO virtual memory region
* vmm: virtual memory allocator
* size: total size to allocate vm region from @vmm.
* align: alignment constraints of the allocated virtual address
* max_align: maximum alignment of allocated virtual address. allocated address
* does not need to satisfy larger alignment than max_align.
* section_offset: page size-aligned offset of iova start address within an 1MB
* boundary. The caller of alloc_iovm_region will obtain the
* allocated iova + section_offset. This is provided just for the
* physically contiguous memory.
* page_offset: must be smaller than PAGE_SIZE. Just a valut to be added to the
* allocated virtual address. This does not effect to the allocaded size
* and address.
*
* This function returns allocated IO virtual address that satisfies the given
* constraints: the caller will get the allocated virtual address plus
* (section_offset + page_offset). Returns 0 if this function is not able
* to allocate IO virtual memory.
*/
static dma_addr_t alloc_iovm_region(struct exynos_iovmm *vmm, size_t size,
size_t section_offset,
off_t page_offset)
{
u32 index = 0;
u32 vstart;
u32 vsize;
unsigned long end, i;
struct exynos_vm_region *region;
struct exynos_iommu_domain *domain = to_exynos_domain(vmm->domain);
size_t align = domain->pre_alloc ? SZ_64K : SZ_1M;
BUG_ON(page_offset >= PAGE_SIZE);
/* To avoid allocating prefetched iovm region */
vsize = (size + section_offset) >> PAGE_SHIFT;
align >>= PAGE_SHIFT;
section_offset >>= PAGE_SHIFT;
spin_lock(&vmm->bitmap_lock);
again:
index = find_next_zero_bit(vmm->vm_map,
IOVM_NUM_PAGES(vmm->iovm_size), index);
if (align) {
index = ALIGN(index, align);
if (index >= IOVM_NUM_PAGES(vmm->iovm_size)) {
pr_err("%s exceed index: %d, num_page: %d\n",
__func__, index, IOVM_NUM_PAGES(vmm->iovm_size));
spin_unlock(&vmm->bitmap_lock);
return 0;
}
if (test_bit(index, vmm->vm_map))
goto again;
}
end = index + vsize;
if (end >= IOVM_NUM_PAGES(vmm->iovm_size)) {
pr_err("%s exceed end: %d, num_page: %d\n",
__func__, end, IOVM_NUM_PAGES(vmm->iovm_size));
spin_unlock(&vmm->bitmap_lock);
return 0;
}
i = find_next_bit(vmm->vm_map, end, index);
if (i < end) {
index = i + 1;
goto again;
}
bitmap_set(vmm->vm_map, index, vsize);
spin_unlock(&vmm->bitmap_lock);
vstart = (index << PAGE_SHIFT) + vmm->iova_start + page_offset;
region = kmalloc(sizeof(*region), GFP_KERNEL);
if (unlikely(!region)) {
pr_err("%s failed to kmalloc for region:", __func__);
spin_lock(&vmm->bitmap_lock);
bitmap_clear(vmm->vm_map, index, vsize);
spin_unlock(&vmm->bitmap_lock);
return 0;
}
INIT_LIST_HEAD(®ion->node);
region->start = vstart;
region->size = vsize << PAGE_SHIFT;
region->dummy_size = region->size - size;
region->section_off = (unsigned int)(section_offset << PAGE_SHIFT);
spin_lock(&vmm->vmlist_lock);
list_add_tail(®ion->node, &vmm->regions_list);
vmm->allocated_size += region->size;
vmm->num_areas++;
vmm->num_map++;
spin_unlock(&vmm->vmlist_lock);
return region->start + region->section_off;
}
struct exynos_vm_region *find_iovm_region(struct exynos_iovmm *vmm,
dma_addr_t iova)
{
struct exynos_vm_region *region;
spin_lock(&vmm->vmlist_lock);
list_for_each_entry(region, &vmm->regions_list, node) {
if (region->start <= iova &&
(region->start + region->size) > iova) {
spin_unlock(&vmm->vmlist_lock);
return region;
}
}
spin_unlock(&vmm->vmlist_lock);
return NULL;
}
static struct exynos_vm_region *remove_iovm_region(struct exynos_iovmm *vmm,
dma_addr_t iova)
{
struct exynos_vm_region *region;
spin_lock(&vmm->vmlist_lock);
list_for_each_entry(region, &vmm->regions_list, node) {
if (region->start + region->section_off == iova) {
list_del(®ion->node);
vmm->allocated_size -= region->size;
vmm->num_areas--;
vmm->num_unmap++;
spin_unlock(&vmm->vmlist_lock);
return region;
}
}
spin_unlock(&vmm->vmlist_lock);
return NULL;
}
static void free_iovm_region(struct exynos_iovmm *vmm,
struct exynos_vm_region *region)
{
if (!region)
return;
spin_lock(&vmm->bitmap_lock);
bitmap_clear(vmm->vm_map,
(region->start - vmm->iova_start) >> PAGE_SHIFT,
region->size >> PAGE_SHIFT);
spin_unlock(&vmm->bitmap_lock);
SYSMMU_EVENT_LOG_IOVMM_UNMAP(IOVMM_TO_LOG(vmm),
region->start, region->start + region->size);
kfree(region);
}
static dma_addr_t add_iovm_region(struct exynos_iovmm *vmm,
dma_addr_t start, size_t size)
{
struct exynos_vm_region *region, *pos;
region = kzalloc(sizeof(*region), GFP_KERNEL);
if (!region)
return 0;
INIT_LIST_HEAD(®ion->node);
region->start = start;
region->size = (u32)size;
spin_lock(&vmm->vmlist_lock);
list_for_each_entry(pos, &vmm->regions_list, node) {
if ((start < (pos->start + pos->size)) &&
((start + size) > pos->start)) {
spin_unlock(&vmm->vmlist_lock);
kfree(region);
return 0;
}
}
list_add(®ion->node, &vmm->regions_list);
spin_unlock(&vmm->vmlist_lock);
return start;
}
static void show_iovm_regions(struct exynos_iovmm *vmm)
{
struct exynos_vm_region *pos;
pr_err("LISTING IOVMM REGIONS...\n");
spin_lock(&vmm->vmlist_lock);
list_for_each_entry(pos, &vmm->regions_list, node) {
pr_err("REGION: %#x (SIZE: %#x, +[%#x, %#x])\n",
pos->start, pos->size,
pos->section_off, pos->dummy_size);
}
spin_unlock(&vmm->vmlist_lock);
pr_err("END OF LISTING IOVMM REGIONS...\n");
}
int iovmm_activate(struct device *dev)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
if (!vmm) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return -EINVAL;
}
return iommu_attach_device(vmm->domain, dev);
}
void iovmm_deactivate(struct device *dev)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
if (!vmm) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return;
}
iommu_detach_device(vmm->domain, dev);
}
struct iommu_domain *get_domain_from_dev(struct device *dev)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
if (!vmm) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return NULL;
}
return vmm->domain;
}
/* iovmm_map - allocate and map IO virtual memory for the given device
* dev: device that has IO virtual address space managed by IOVMM
* sg: list of physically contiguous memory chunks. The preceding chunk needs to
* be larger than the following chunks in sg for efficient mapping and
* performance. If elements of sg are more than one, physical address of
* each chunk needs to be aligned by its size for efficent mapping and TLB
* utilization.
* offset: offset in bytes to be mapped and accessed by dev.
* size: size in bytes to be mapped and accessed by dev.
*
* This function allocates IO virtual memory for the given device and maps the
* given physical memory conveyed by sg into the allocated IO memory region.
* Returns allocated IO virtual address if it allocates and maps successfull.
* Otherwise, minus error number. Caller must check if the return value of this
* function with IS_ERR_VALUE().
*/
dma_addr_t iovmm_map(struct device *dev, struct scatterlist *sg, off_t offset,
size_t size, enum dma_data_direction direction, int prot)
{
off_t start_off;
dma_addr_t addr, start = 0;
size_t mapped_size = 0;
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
size_t section_offset = 0; /* section offset of contig. mem */
int ret = 0;
int idx;
struct scatterlist *tsg;
struct exynos_vm_region *region;
if (vmm == NULL) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return -EINVAL;
}
for (; (sg != NULL) && (sg->length < offset); sg = sg_next(sg))
offset -= sg->length;
if (sg == NULL) {
dev_err(dev, "IOVMM: invalid offset to %s.\n", __func__);
return -EINVAL;
}
tsg = sg;
start_off = offset_in_page(sg_phys(sg) + offset);
size = PAGE_ALIGN(size + start_off);
if (sg_physically_continuous(sg)) {
size_t aligned_pad_size;
phys_addr_t phys = page_to_phys(sg_page(sg));
section_offset = phys & (~SECT_MASK);
aligned_pad_size = ALIGN(phys, SECT_SIZE) - phys;
if ((sg->length - aligned_pad_size) < SECT_SIZE) {
aligned_pad_size = ALIGN(phys, LPAGE_SIZE) - phys;
if ((sg->length - aligned_pad_size) >= LPAGE_SIZE)
section_offset = phys & (~LPAGE_MASK);
else
section_offset = 0;
}
}
start = alloc_iovm_region(vmm, size, section_offset, start_off);
if (!start) {
spin_lock(&vmm->vmlist_lock);
dev_err(dev, "%s: Not enough IOVM space to allocate %#zx\n",
__func__, size);
dev_err(dev, "%s: Total %#zx, Allocated %#zx , Chunks %d\n",
__func__, vmm->iovm_size,
vmm->allocated_size, vmm->num_areas);
spin_unlock(&vmm->vmlist_lock);
ret = -ENOMEM;
goto err_map_nomem;
}
addr = start - start_off;
do {
phys_addr_t phys;
size_t len;
phys = sg_phys(sg);
len = sg->length;
/* if back to back sg entries are contiguous consolidate them */
while (sg_next(sg) &&
sg_phys(sg) + sg->length == sg_phys(sg_next(sg))) {
len += sg_next(sg)->length;
sg = sg_next(sg);
}
if (offset > 0) {
len -= offset;
phys += offset;
offset = 0;
}
if (offset_in_page(phys)) {
len += offset_in_page(phys);
phys = round_down(phys, PAGE_SIZE);
}
len = PAGE_ALIGN(len);
if (len > (size - mapped_size))
len = size - mapped_size;
ret = iommu_map(vmm->domain, addr, phys, len, prot);
if (ret) {
dev_err(dev, "iommu_map failed w/ err: %d\n", ret);
break;
}
addr += len;
mapped_size += len;
} while ((sg = sg_next(sg)) && (mapped_size < size));
BUG_ON(mapped_size > size);
if (mapped_size < size) {
dev_err(dev, "mapped_size(%#zx) is smaller than size(%#zx)\n",
mapped_size, size);
if (!ret) {
dev_err(dev, "ret: %d\n", ret);
ret = -EINVAL;
}
goto err_map_map;
}
region = find_iovm_region(vmm, start);
BUG_ON(!region);
/*
* If pretched SLPD is a fault SLPD in zero_l2_table, FLPD cache
* or prefetch buffer caches the address of zero_l2_table.
* This function replaces the zero_l2_table with new L2 page
* table to write valid mappings.
* Accessing the valid area may cause page fault since FLPD
* cache may still caches zero_l2_table for the valid area
* instead of new L2 page table that have the mapping
* information of the valid area
* Thus any replacement of zero_l2_table with other valid L2
* page table must involve FLPD cache invalidation if the System
* MMU have prefetch feature and FLPD cache (version 3.3).
* FLPD cache invalidation is performed with TLB invalidation
* by VPN without blocking. It is safe to invalidate TLB without
* blocking because the target address of TLB invalidation is
* not currently mapped.
*/
/* TODO: for sysmmu v6, remove it later */
exynos_sysmmu_tlb_invalidate(vmm->domain, region->start, region->size);
dev_dbg(dev, "IOVMM: Allocated VM region @ %#x/%#x bytes.\n",
(unsigned int)start, (unsigned int)size);
SYSMMU_EVENT_LOG_IOVMM_MAP(IOVMM_TO_LOG(vmm), start, start + size,
region->size - size);
return start;
err_map_map:
iommu_unmap(vmm->domain, start - start_off, mapped_size);
free_iovm_region(vmm, remove_iovm_region(vmm, start));
dev_err(dev,
"Failed(%d) to map IOVMM REGION %pa (SIZE: %#zx, mapped: %#zx)\n",
ret, &start, size, mapped_size);
idx = 0;
do {
pr_err("SGLIST[%d].size = %#x\n", idx++, tsg->length);
} while ((tsg = sg_next(tsg)));
show_iovm_regions(vmm);
err_map_nomem:
dev_dbg(dev,
"IOVMM: Failed to allocated VM region for %#x bytes.\n",
(unsigned int)size);
return (dma_addr_t)ret;
}
void iovmm_unmap(struct device *dev, dma_addr_t iova)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
struct exynos_vm_region *region;
size_t unmap_size;
/* This function must not be called in IRQ handlers */
BUG_ON(in_irq());
if (vmm == NULL) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return;
}
region = remove_iovm_region(vmm, iova);
if (region) {
u32 start = region->start + region->section_off;
u32 size = region->size - region->dummy_size;
/* clear page offset */
if (WARN_ON(start != iova)) {
dev_err(dev, "IOVMM: "
"iova %pa and region %#x(+%#x)@%#x(-%#x) mismatch\n",
&iova, region->size, region->dummy_size,
region->start, region->section_off);
show_iovm_regions(vmm);
/* reinsert iovm region */
add_iovm_region(vmm, region->start, region->size);
kfree(region);
return;
}
unmap_size = iommu_unmap(vmm->domain, start & SPAGE_MASK, size);
if (unlikely(unmap_size != size)) {
dev_err(dev,
"Failed to unmap REGION of %#x:\n", start);
dev_err(dev, "(SIZE: %#x, iova: %pa, unmapped: %#zx)\n",
size, &iova, unmap_size);
show_iovm_regions(vmm);
kfree(region);
BUG();
return;
}
exynos_sysmmu_tlb_invalidate(vmm->domain, region->start, region->size);
/* TODO: for sysmmu v6, remove it later */
/* 60us is required to guarantee that PTW ends itself */
udelay(60);
free_iovm_region(vmm, region);
dev_dbg(dev, "IOVMM: Unmapped %#x bytes from %#x.\n",
(unsigned int)unmap_size, (unsigned int)iova);
} else {
dev_err(dev, "IOVMM: No IOVM region %pa to free.\n", &iova);
}
}
/*
* NOTE:
* exynos_iovmm_map_userptr() should be called under current->mm.mmap_sem held.
*/
dma_addr_t exynos_iovmm_map_userptr(struct device *dev, unsigned long vaddr,
size_t size, int prot)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
unsigned long eaddr = vaddr + size;
off_t offset = offset_in_page(vaddr);
int ret = -EINVAL;
struct vm_area_struct *vma;
dma_addr_t start;
struct exynos_vm_region *region;
vma = find_vma(current->mm, vaddr);
if (!vma || (vaddr < vma->vm_start)) {
dev_err(dev, "%s: invalid address %#lx\n", __func__, vaddr);
goto err;
}
if (!!(vma->vm_flags & VM_PFNMAP))
prot |= IOMMU_PFNMAP;
/* non-cached user mapping should be treated as non-shareable mapping */
if ((pgprot_val(pgprot_writecombine(vma->vm_page_prot)) ==
pgprot_val(vma->vm_page_prot)) ||
(pgprot_val(pgprot_noncached(vma->vm_page_prot)) ==
pgprot_val(vma->vm_page_prot)))
prot &= ~IOMMU_CACHE;
else if (device_get_dma_attr(dev) == DEV_DMA_COHERENT)
prot |= IOMMU_CACHE;
while (eaddr > vma->vm_end) {
if (!!(vma->vm_flags & VM_PFNMAP)) {
dev_err(dev, "%s: non-linear pfnmap is not supported\n",
__func__);
goto err;
}
if ((vma->vm_next == NULL) ||
(vma->vm_end != vma->vm_next->vm_start)) {
dev_err(dev, "%s: invalid size %zu\n", __func__, size);
goto err;
}
vma = vma->vm_next;
}
size = PAGE_ALIGN(size + offset);
start = alloc_iovm_region(vmm, size, 0, offset);
if (!start) {
spin_lock(&vmm->vmlist_lock);
dev_err(dev, "%s: Not enough IOVM space to allocate %#zx\n",
__func__, size);
dev_err(dev, "%s: Total %#zx, Allocated %#zx , Chunks %d\n",
__func__, vmm->iovm_size,
vmm->allocated_size, vmm->num_areas);
spin_unlock(&vmm->vmlist_lock);
ret = -ENOMEM;
goto err;
}
ret = exynos_iommu_map_userptr(vmm->domain, vaddr - offset,
start - offset, size, prot);
if (ret < 0)
goto err_map;
region = find_iovm_region(vmm, start);
BUG_ON(!region);
SYSMMU_EVENT_LOG_IOVMM_MAP(IOVMM_TO_LOG(vmm), start, start + size,
region->size - size);
return start;
err_map:
free_iovm_region(vmm, remove_iovm_region(vmm, start));
err:
return (dma_addr_t)ret;
}
void exynos_iovmm_unmap_userptr(struct device *dev, dma_addr_t iova)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
struct exynos_vm_region *region;
region = remove_iovm_region(vmm, iova);
if (region) {
u32 start = region->start + region->section_off;
u32 size = region->size - region->dummy_size;
/* clear page offset */
if (WARN_ON(start != iova)) {
dev_err(dev, "IOVMM: "
"iova %pa and region %#x(+%#x)@%#x(-%#x) mismatch\n",
&iova, region->size, region->dummy_size,
region->start, region->section_off);
show_iovm_regions(vmm);
/* reinsert iovm region */
add_iovm_region(vmm, region->start, region->size);
kfree(region);
return;
}
exynos_iommu_unmap_userptr(vmm->domain,
start & SPAGE_MASK, size);
free_iovm_region(vmm, region);
} else {
dev_err(dev, "IOVMM: No IOVM region %pa to free.\n", &iova);
}
}
int iovmm_map_oto(struct device *dev, phys_addr_t phys, size_t size)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
int ret;
BUG_ON(!IS_ALIGNED(phys, PAGE_SIZE));
BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
if (vmm == NULL) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return -EINVAL;
}
if (WARN_ON((phys < vmm->iova_start) ||
(phys + size) >= (vmm->iova_start + vmm->iovm_size))) {
dev_err(dev,
"Unable to create one to one mapping for %#zx @ %pa\n",
size, &phys);
return -EINVAL;
}
if (!add_iovm_region(vmm, (dma_addr_t)phys, size))
return -EADDRINUSE;
ret = iommu_map(vmm->domain, (dma_addr_t)phys, phys, size, 0);
if (ret < 0)
free_iovm_region(vmm,
remove_iovm_region(vmm, (dma_addr_t)phys));
return ret;
}
void iovmm_unmap_oto(struct device *dev, phys_addr_t phys)
{
struct exynos_iovmm *vmm = exynos_get_iovmm(dev);
struct exynos_vm_region *region;
size_t unmap_size;
/* This function must not be called in IRQ handlers */
BUG_ON(in_irq());
BUG_ON(!IS_ALIGNED(phys, PAGE_SIZE));
if (vmm == NULL) {
dev_err(dev, "%s: IOVMM not found\n", __func__);
return;
}
region = remove_iovm_region(vmm, (dma_addr_t)phys);
if (region) {
unmap_size = iommu_unmap(vmm->domain, (dma_addr_t)phys,
region->size);
WARN_ON(unmap_size != region->size);
exynos_sysmmu_tlb_invalidate(vmm->domain, (dma_addr_t)phys,
region->size);
free_iovm_region(vmm, region);
dev_dbg(dev, "IOVMM: Unmapped %#zx bytes from %pa.\n",
unmap_size, &phys);
}
}
static struct dentry *exynos_iovmm_debugfs_root;
static struct dentry *exynos_iommu_debugfs_root;
static int exynos_iovmm_create_debugfs(void)
{
exynos_iovmm_debugfs_root = debugfs_create_dir("iovmm", NULL);
if (!exynos_iovmm_debugfs_root)
pr_err("IOVMM: Failed to create debugfs entry\n");
else
pr_info("IOVMM: Created debugfs entry at debugfs/iovmm\n");
exynos_iommu_debugfs_root = debugfs_create_dir("iommu", NULL);
if (!exynos_iommu_debugfs_root)
pr_err("IOMMU: Failed to create debugfs entry\n");
else
pr_info("IOMMU: Created debugfs entry at debugfs/iommu\n");
return 0;
}
core_initcall(exynos_iovmm_create_debugfs);
static int iovmm_debug_show(struct seq_file *s, void *unused)
{
struct exynos_iovmm *vmm = s->private;
seq_printf(s, "%10.s %10.s %10.s %6.s\n",
"VASTART", "SIZE", "FREE", "CHUNKS");
seq_puts(s, "---------------------------------------------\n");
spin_lock(&vmm->vmlist_lock);
seq_printf(s, " %#x %#10zx %#10zx %d\n",
vmm->iova_start, vmm->iovm_size,
vmm->iovm_size - vmm->allocated_size,
vmm->num_areas);
seq_puts(s, "---------------------------------------------\n");
seq_printf(s, "Total number of mappings : %d\n", vmm->num_map);
seq_printf(s, "Total number of unmappings: %d\n", vmm->num_unmap);
spin_unlock(&vmm->vmlist_lock);
return 0;
}
static int iovmm_debug_open(struct inode *inode, struct file *file)
{
return single_open(file, iovmm_debug_show, inode->i_private);
}
static ssize_t iovmm_debug_write(struct file *filp, const char __user *p,
size_t len, loff_t *off)
{
struct seq_file *s = filp->private_data;
struct exynos_iovmm *vmm = s->private;
/* clears the map count in IOVMM */
spin_lock(&vmm->vmlist_lock);
vmm->num_map = 0;
vmm->num_unmap = 0;
spin_unlock(&vmm->vmlist_lock);
return len;
}
static const struct file_operations iovmm_debug_fops = {
.open = iovmm_debug_open,
.read = seq_read,
.write = iovmm_debug_write,
.llseek = seq_lseek,
.release = single_release,
};
static void iovmm_register_debugfs(struct exynos_iovmm *vmm)
{
if (!exynos_iovmm_debugfs_root)
return;
debugfs_create_file(vmm->domain_name, 0664,
exynos_iovmm_debugfs_root, vmm, &iovmm_debug_fops);
}
struct exynos_iovmm *exynos_create_single_iovmm(const char *name,
unsigned int start, unsigned int end)
{
struct exynos_iovmm *vmm;
int ret = 0;
vmm = kzalloc(sizeof(*vmm), GFP_KERNEL);
if (!vmm) {
ret = -ENOMEM;
goto err_alloc_vmm;
}
vmm->iovm_size = (size_t)(end - start);
vmm->iova_start = start;
vmm->vm_map = kzalloc(IOVM_BITMAP_SIZE(vmm->iovm_size), GFP_KERNEL);
if (!vmm->vm_map) {
ret = -ENOMEM;
goto err_setup_domain;
}
vmm->domain = iommu_domain_alloc(&platform_bus_type);
if (!vmm->domain) {
ret = -ENOMEM;
goto err_setup_domain;
}
ret = exynos_iommu_init_event_log(IOVMM_TO_LOG(vmm), IOVMM_LOG_LEN);
if (!ret) {
iovmm_add_log_to_debugfs(exynos_iovmm_debugfs_root,
IOVMM_TO_LOG(vmm), name);
iommu_add_log_to_debugfs(exynos_iommu_debugfs_root,
IOMMU_TO_LOG(vmm->domain), name);
} else {
goto err_init_event_log;
}
spin_lock_init(&vmm->vmlist_lock);
spin_lock_init(&vmm->bitmap_lock);
INIT_LIST_HEAD(&vmm->regions_list);
vmm->domain_name = name;
iovmm_register_debugfs(vmm);
pr_debug("%s IOVMM: Created %#zx B IOVMM from %#x.\n",
name, vmm->iovm_size, vmm->iova_start);
return vmm;
err_init_event_log:
iommu_domain_free(vmm->domain);
err_setup_domain:
kfree(vmm);
err_alloc_vmm:
pr_err("%s IOVMM: Failed to create IOVMM (%d)\n", name, ret);
return ERR_PTR(ret);
}
void iovmm_set_fault_handler(struct device *dev,
iommu_fault_handler_t handler, void *token)
{
int ret;
ret = exynos_iommu_add_fault_handler(dev, handler, token);
if (ret)
dev_err(dev, "Failed to add fault handler\n");
}