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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
|
/* linux/drivers/media/video/videobuf2-ion.c
*
* Copyright (c) 2011 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*
* Implementation of Android ION memory allocator for videobuf2
*
* 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/file.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/highmem.h>
#include <linux/dma-buf.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <linux/vmalloc.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-memops.h>
#include <media/videobuf2-ion.h>
#include <linux/exynos_iovmm.h>
#include <plat/cpu.h>
#define vb2ion_err(dev, fmt, ...) \
dev_err(dev, "VB2ION: " pr_fmt(fmt), ##__VA_ARGS__)
struct vb2_ion_context {
struct device *dev;
struct ion_client *client;
unsigned long alignment;
long flags;
/* protects iommu_active_cnt and protected */
struct mutex lock;
int iommu_active_cnt;
bool protected;
};
struct vb2_ion_buf {
struct vb2_ion_context *ctx;
struct vb2_vmarea_handler handler;
struct vm_area_struct *vma;
struct ion_handle *handle;
struct dma_buf *dma_buf;
struct dma_buf_attachment *attachment;
enum dma_data_direction direction;
void *kva;
unsigned long size;
atomic_t ref;
bool cached;
bool ion;
struct vb2_ion_cookie cookie;
};
#define CACHE_FLUSH_ALL_SIZE SZ_8M
#define DMA_SYNC_SIZE SZ_512K
#define OUTER_FLUSH_ALL_SIZE SZ_1M
#define ctx_cached(ctx) (!(ctx->flags & VB2ION_CTX_UNCACHED))
#define ctx_iommu(ctx) (!!(ctx->flags & VB2ION_CTX_IOMMU))
void vb2_ion_set_cached(void *ctx, bool cached)
{
struct vb2_ion_context *vb2ctx = ctx;
if (cached)
vb2ctx->flags &= ~VB2ION_CTX_UNCACHED;
else
vb2ctx->flags |= VB2ION_CTX_UNCACHED;
}
EXPORT_SYMBOL(vb2_ion_set_cached);
#ifndef CONFIG_EXYNOS_IOVMM_V6
/*
* vb2ctx->protected, vb2ctx->iommu_active_cnt and iommu state are not
* guaranteed to be atomically changed with this function. Thus it should
* be care if this function is directly called by the drivers.
* The atomicity must be guaranteed by the caller.
*/
void __vb2_ion_set_protected_unlocked(void *ctx, bool ctx_protected)
{
struct vb2_ion_context *vb2ctx = ctx;
if (vb2ctx->protected == ctx_protected)
return;
vb2ctx->protected = ctx_protected;
if (ctx_protected) {
if (vb2ctx->iommu_active_cnt) {
dev_dbg(vb2ctx->dev, "detaching active MMU\n");
iovmm_deactivate(vb2ctx->dev);
}
} else {
if (vb2ctx->iommu_active_cnt) {
int ret;
dev_dbg(vb2ctx->dev, "re-attaching active MMU\n");
ret = iovmm_activate(vb2ctx->dev);
if (ret) {
dev_err(vb2ctx->dev,
"Failed to activate IOMMU with err %d\n", ret);
BUG();
}
}
}
}
/*
* when a context is protected, we cannot use the IOMMU since
* secure world is in charge.
*/
void vb2_ion_set_protected(void *ctx, bool ctx_protected)
{
struct vb2_ion_context *vb2ctx = ctx;
mutex_lock(&vb2ctx->lock);
__vb2_ion_set_protected_unlocked(ctx, ctx_protected);
mutex_unlock(&vb2ctx->lock);
}
EXPORT_SYMBOL(vb2_ion_set_protected);
#endif
int vb2_ion_set_alignment(void *ctx, size_t alignment)
{
struct vb2_ion_context *vb2ctx = ctx;
if ((alignment != 0) && (alignment < PAGE_SIZE))
return -EINVAL;
if (alignment & ~alignment)
return -EINVAL;
if (alignment == 0)
vb2ctx->alignment = PAGE_SIZE;
else
vb2ctx->alignment = alignment;
return 0;
}
EXPORT_SYMBOL(vb2_ion_set_alignment);
void *vb2_ion_create_context(struct device *dev, size_t alignment, long flags)
{
struct vb2_ion_context *ctx;
/* non-contigous memory without H/W virtualization is not supported */
if ((flags & VB2ION_CTX_VMCONTIG) && !(flags & VB2ION_CTX_IOMMU))
return ERR_PTR(-EINVAL);
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
if (!ctx)
return ERR_PTR(-ENOMEM);
ctx->dev = dev;
ctx->client = ion_client_create(ion_exynos, dev_name(dev));
if (IS_ERR(ctx->client)) {
void *retp = ctx->client;
kfree(ctx);
return retp;
}
vb2_ion_set_alignment(ctx, alignment);
ctx->flags = flags;
mutex_init(&ctx->lock);
return ctx;
}
EXPORT_SYMBOL(vb2_ion_create_context);
void vb2_ion_destroy_context(void *ctx)
{
struct vb2_ion_context *vb2ctx = ctx;
mutex_destroy(&vb2ctx->lock);
ion_client_destroy(vb2ctx->client);
kfree(vb2ctx);
}
EXPORT_SYMBOL(vb2_ion_destroy_context);
void *vb2_ion_private_alloc(void *alloc_ctx, size_t size, int write, int plane)
{
struct vb2_ion_context *ctx = alloc_ctx;
struct vb2_ion_buf *buf;
int heapflags = ion_heapflag(ctx->flags);
int flags = ion_flag(ctx->flags);
int ret = 0;
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf)
return ERR_PTR(-ENOMEM);
size = PAGE_ALIGN(size);
flags |= ctx_cached(ctx) ?
ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC : 0;
if ((ion_flag(ctx->flags) & VB2ION_CTX_DRM_VIDEO) ||
(ion_flag(ctx->flags) & VB2ION_CTX_DRM_MFCFW))
flags |= ION_FLAG_PROTECTED;
buf->handle = ion_alloc(ctx->client, size, ctx->alignment,
heapflags, flags);
if (IS_ERR(buf->handle)) {
ret = -ENOMEM;
goto err_alloc;
}
buf->dma_buf = ion_share_dma_buf(ctx->client, buf->handle);
if (IS_ERR(buf->dma_buf)) {
ret = PTR_ERR(buf->dma_buf);
goto err_share;
}
buf->attachment = dma_buf_attach(buf->dma_buf, ctx->dev);
if (IS_ERR(buf->attachment)) {
ret = PTR_ERR(buf->attachment);
goto err_attach;
}
buf->cookie.sgt = dma_buf_map_attachment(
buf->attachment, DMA_BIDIRECTIONAL);
if (IS_ERR(buf->cookie.sgt)) {
ret = PTR_ERR(buf->cookie.sgt);
goto err_map_dmabuf;
}
buf->ctx = ctx;
buf->size = size;
buf->cached = ctx_cached(ctx);
buf->direction = write ? DMA_FROM_DEVICE: DMA_TO_DEVICE;
buf->ion = true;
mutex_lock(&ctx->lock);
if (ctx_iommu(ctx) && !ctx->protected) {
buf->cookie.ioaddr = ion_iovmm_map(buf->attachment, 0,
buf->size, buf->direction, plane);
if (IS_ERR_VALUE(buf->cookie.ioaddr)) {
ret = (int)buf->cookie.ioaddr;
mutex_unlock(&ctx->lock);
goto err_ion_map_io;
}
}
mutex_unlock(&ctx->lock);
return &buf->cookie;
err_ion_map_io:
if (buf->kva)
ion_unmap_kernel(ctx->client, buf->handle);
dma_buf_unmap_attachment(buf->attachment, buf->cookie.sgt,
DMA_BIDIRECTIONAL);
err_map_dmabuf:
dma_buf_detach(buf->dma_buf, buf->attachment);
err_attach:
dma_buf_put(buf->dma_buf);
err_share:
ion_free(ctx->client, buf->handle);
err_alloc:
kfree(buf);
pr_err("%s: Error occured while allocating\n", __func__);
return ERR_PTR(ret);
}
void vb2_ion_private_free(void *cookie)
{
struct vb2_ion_buf *buf =
container_of(cookie, struct vb2_ion_buf, cookie);
struct vb2_ion_context *ctx;
if (WARN_ON(IS_ERR_OR_NULL(cookie)))
return;
ctx = buf->ctx;
mutex_lock(&ctx->lock);
if (ctx_iommu(ctx) && !ctx->protected)
ion_iovmm_unmap(buf->attachment, buf->cookie.ioaddr);
mutex_unlock(&ctx->lock);
dma_buf_unmap_attachment(buf->attachment, buf->cookie.sgt,
DMA_BIDIRECTIONAL);
dma_buf_detach(buf->dma_buf, buf->attachment);
dma_buf_put(buf->dma_buf);
if (buf->kva)
ion_unmap_kernel(ctx->client, buf->handle);
ion_free(ctx->client, buf->handle);
kfree(buf);
}
static void vb2_ion_put(void *buf_priv)
{
struct vb2_ion_buf *buf = buf_priv;
if (atomic_dec_and_test(&buf->ref))
vb2_ion_private_free(&buf->cookie);
}
static void *vb2_ion_alloc(void *alloc_ctx, unsigned long size, int write,
int plane, gfp_t gfp_flags)
{
struct vb2_ion_buf *buf;
void *cookie;
cookie = vb2_ion_private_alloc(alloc_ctx, size, write, plane);
if (IS_ERR(cookie))
return cookie;
buf = container_of(cookie, struct vb2_ion_buf, cookie);
buf->handler.refcount = &buf->ref;
buf->handler.put = vb2_ion_put;
buf->handler.arg = buf;
atomic_set(&buf->ref, 1);
return buf;
}
void *vb2_ion_private_vaddr(void *cookie)
{
struct vb2_ion_buf *buf =
container_of(cookie, struct vb2_ion_buf, cookie);
if (WARN_ON(IS_ERR_OR_NULL(cookie)))
return NULL;
if (!buf->kva) {
buf->kva = ion_map_kernel(buf->ctx->client, buf->handle);
if (IS_ERR_OR_NULL(buf->kva))
buf->kva = NULL;
buf->kva += buf->cookie.offset;
}
return buf->kva;
}
static void *vb2_ion_cookie(void *buf_priv)
{
struct vb2_ion_buf *buf = buf_priv;
if (WARN_ON(!buf))
return NULL;
return (void *)&buf->cookie;
}
static void *vb2_ion_vaddr(void *buf_priv)
{
struct vb2_ion_buf *buf = buf_priv;
if (WARN_ON(!buf))
return NULL;
if (buf->kva != NULL)
return buf->kva;
if (buf->handle)
return vb2_ion_private_vaddr(&buf->cookie);
if (dma_buf_begin_cpu_access(buf->dma_buf,
0, buf->size, buf->direction))
return NULL;
buf->kva = dma_buf_kmap(buf->dma_buf, buf->cookie.offset / PAGE_SIZE);
if (buf->kva == NULL)
dma_buf_end_cpu_access(buf->dma_buf, 0,
buf->size, buf->direction);
else
buf->kva += buf->cookie.offset & ~PAGE_MASK;
return buf->kva;
}
static unsigned int vb2_ion_num_users(void *buf_priv)
{
struct vb2_ion_buf *buf = buf_priv;
if (WARN_ON(!buf))
return 0;
return atomic_read(&buf->ref);
}
static int vb2_ion_mmap(void *buf_priv, struct vm_area_struct *vma)
{
struct vb2_ion_buf *buf = buf_priv;
unsigned long vm_start = vma->vm_start;
unsigned long vm_end = vma->vm_end;
struct scatterlist *sg = buf->cookie.sgt->sgl;
unsigned long size;
int ret = -EINVAL;
if (buf->size < (vm_end - vm_start))
return ret;
if (!buf->cached)
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
size = min_t(size_t, vm_end - vm_start, sg_dma_len(sg));
ret = remap_pfn_range(vma, vm_start, page_to_pfn(sg_page(sg)),
size, vma->vm_page_prot);
for (sg = sg_next(sg), vm_start += size;
!ret && sg && (vm_start < vm_end);
vm_start += size, sg = sg_next(sg)) {
size = min_t(size_t, vm_end - vm_start, sg_dma_len(sg));
ret = remap_pfn_range(vma, vm_start, page_to_pfn(sg_page(sg)),
size, vma->vm_page_prot);
}
if (ret)
return ret;
if (vm_start < vm_end)
return -EINVAL;
vma->vm_flags |= VM_DONTEXPAND;
vma->vm_private_data = &buf->handler;
vma->vm_ops = &vb2_common_vm_ops;
vma->vm_ops->open(vma);
return ret;
}
static int vb2_ion_map_dmabuf(void *mem_priv, int plane)
{
struct vb2_ion_buf *buf = mem_priv;
struct vb2_ion_context *ctx = buf->ctx;
if (WARN_ON(!buf->attachment)) {
pr_err("trying to pin a non attached buffer\n");
return -EINVAL;
}
if (WARN_ON(buf->cookie.sgt)) {
pr_err("dmabuf buffer is already pinned\n");
return 0;
}
/* get the associated scatterlist for this buffer */
buf->cookie.sgt = dma_buf_map_attachment(buf->attachment,
buf->direction);
if (IS_ERR_OR_NULL(buf->cookie.sgt)) {
pr_err("Error getting dmabuf scatterlist\n");
return -EINVAL;
}
vb2_ion_sync_for_device((void *) &buf->cookie, 0,
buf->size, buf->direction);
buf->cookie.offset = 0;
/* buf->kva = NULL; */
mutex_lock(&ctx->lock);
if (ctx_iommu(ctx) && !ctx->protected && buf->cookie.ioaddr == 0) {
buf->cookie.ioaddr = ion_iovmm_map(buf->attachment, 0,
buf->size, buf->direction, plane);
if (IS_ERR_VALUE(buf->cookie.ioaddr)) {
pr_err("buf->cookie.ioaddr is error: %pa\n",
&buf->cookie.ioaddr);
mutex_unlock(&ctx->lock);
dma_buf_unmap_attachment(buf->attachment,
buf->cookie.sgt, buf->direction);
return (int)buf->cookie.ioaddr;
}
}
mutex_unlock(&ctx->lock);
return 0;
}
static void vb2_ion_unmap_dmabuf(void *mem_priv)
{
struct vb2_ion_buf *buf = mem_priv;
if (WARN_ON(!buf->attachment)) {
pr_err("trying to unpin a not attached buffer\n");
return;
}
if (WARN_ON(!buf->cookie.sgt)) {
pr_err("dmabuf buffer is already unpinned\n");
return;
}
dma_buf_unmap_attachment(buf->attachment,
buf->cookie.sgt, buf->direction);
vb2_ion_sync_for_cpu((void *) &buf->cookie, 0,
buf->size, buf->direction);
buf->cookie.sgt = NULL;
}
static void vb2_ion_detach_dmabuf(void *mem_priv)
{
struct vb2_ion_buf *buf = mem_priv;
struct vb2_ion_context *ctx = buf->ctx;
mutex_lock(&ctx->lock);
if (buf->cookie.ioaddr && ctx_iommu(ctx) && !ctx->protected ) {
ion_iovmm_unmap(buf->attachment, buf->cookie.ioaddr);
buf->cookie.ioaddr = 0;
}
mutex_unlock(&ctx->lock);
if (buf->kva != NULL) {
dma_buf_kunmap(buf->dma_buf, 0, buf->kva);
dma_buf_end_cpu_access(buf->dma_buf, 0, buf->size, 0);
}
/* detach this attachment */
dma_buf_detach(buf->dma_buf, buf->attachment);
kfree(buf);
}
static void *vb2_ion_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
unsigned long size, int write)
{
struct vb2_ion_buf *buf;
struct dma_buf_attachment *attachment;
if (dbuf->size < size) {
WARN(1, "dbuf->size(%zd) is smaller than size(%ld)\n",
dbuf->size, size);
return ERR_PTR(-EFAULT);
}
buf = kzalloc(sizeof *buf, GFP_KERNEL);
if (!buf) {
pr_err("out of memory\n");
return ERR_PTR(-ENOMEM);
}
buf->ctx = alloc_ctx;
/* create attachment for the dmabuf with the user device */
attachment = dma_buf_attach(dbuf, buf->ctx->dev);
if (IS_ERR(attachment)) {
pr_err("failed to attach dmabuf\n");
kfree(buf);
return attachment;
}
buf->direction = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
buf->size = size;
buf->dma_buf = dbuf;
buf->ion = true;
buf->attachment = attachment;
return buf;
}
/***** V4L2_MEMORY_USERPTR support *****/
struct vb2_ion_dmabuf_data {
struct sg_table sgt;
bool is_pfnmap;
int write;
void *kva;
};
static struct sg_table *vb2_ion_map_dmabuf_userptr(
struct dma_buf_attachment *attach, enum dma_data_direction dir)
{
struct vb2_ion_dmabuf_data *priv = attach->dmabuf->priv;
return &priv->sgt;
}
static void vb2_ion_unmap_dmabuf_userptr(struct dma_buf_attachment *attach,
struct sg_table *sgt,
enum dma_data_direction dir)
{
}
static void vb2_ion_release_dmabuf_userptr(struct dma_buf *dbuf)
{
struct vb2_ion_dmabuf_data *priv = dbuf->priv;
int i;
struct scatterlist *sg;
vfree(priv->kva);
if (!priv->is_pfnmap) {
if (priv->write) {
for_each_sg(priv->sgt.sgl, sg,
priv->sgt.orig_nents, i) {
set_page_dirty_lock(sg_page(sg));
put_page(sg_page(sg));
}
} else {
for_each_sg(priv->sgt.sgl, sg,
priv->sgt.orig_nents, i)
put_page(sg_page(sg));
}
}
sg_free_table(&priv->sgt);
kfree(priv);
}
static void *vb2_ion_kmap_dmabuf_userptr(struct dma_buf *dbuf,
unsigned long page_num)
{
struct page **pages, **tmp_pages;
struct scatterlist *sgl;
int num_pages, i;
void *vaddr;
struct vb2_ion_dmabuf_data *priv = dbuf->priv;
num_pages = PAGE_ALIGN(
offset_in_page(sg_phys(priv->sgt.sgl)) + dbuf->size)
>> PAGE_SHIFT;
pages = vmalloc(sizeof(*pages) * num_pages);
if (!pages)
return NULL;
tmp_pages = pages;
for_each_sg(priv->sgt.sgl, sgl, priv->sgt.orig_nents, i) {
struct page *page = sg_page(sgl);
unsigned int n =
PAGE_ALIGN(sgl->offset + sg_dma_len(sgl)) >> PAGE_SHIFT;
for (; n > 0; n--)
*(tmp_pages++) = page++;
}
vaddr = vmap(pages, num_pages, VM_USERMAP | VM_MAP, PAGE_KERNEL);
vfree(pages);
return (vaddr) ? vaddr + offset_in_page(sg_phys(priv->sgt.sgl)) : 0;
}
void vb2_ion_kunmap_dmabuf_userptr(struct dma_buf *dbuf, unsigned long page_num,
void *vaddr)
{
vunmap((void *)((unsigned long)vaddr & PAGE_MASK));
}
static int vb2_ion_mmap_dmabuf_userptr(struct dma_buf *dbuf,
struct vm_area_struct *vma)
{
return -ENOSYS;
}
static struct dma_buf_ops vb2_ion_dmabuf_ops = {
.map_dma_buf = vb2_ion_map_dmabuf_userptr,
.unmap_dma_buf = vb2_ion_unmap_dmabuf_userptr,
.release = vb2_ion_release_dmabuf_userptr,
.kmap_atomic = vb2_ion_kmap_dmabuf_userptr,
.kmap = vb2_ion_kmap_dmabuf_userptr,
.kunmap_atomic = vb2_ion_kunmap_dmabuf_userptr,
.kunmap = vb2_ion_kunmap_dmabuf_userptr,
.mmap = vb2_ion_mmap_dmabuf_userptr,
};
static int pfnmap_digger(struct sg_table *sgt, unsigned long addr, int nr_pages,
off_t offset)
{
/* If the given user address is not normal mapping,
It must be contiguous physical mapping */
struct vm_area_struct *vma;
unsigned long *pfns;
int i, ipfn, pi, ret;
struct scatterlist *sg;
unsigned int contigs;
unsigned long pfn;
vma = find_vma(current->mm, addr);
if ((vma == NULL) ||
(vma->vm_end < (addr + (nr_pages << PAGE_SHIFT))))
return -EINVAL;
pfns = kmalloc(sizeof(*pfns) * nr_pages, GFP_KERNEL);
if (!pfns)
return -ENOMEM;
ret = follow_pfn(vma, addr, &pfns[0]); /* no side effect */
if (ret)
goto err_follow_pfn;
if (!pfn_valid(pfns[0])) {
ret = -EINVAL;
goto err_follow_pfn;
}
addr += PAGE_SIZE;
/* An element of pfns consists of
* - higher 20 bits: page frame number (pfn)
* - lower 12 bits: number of contiguous pages from the pfn
* Maximum size of a contiguous chunk: 16MB (4096 pages)
* contigs = 0 indicates no adjacent page is found yet.
* Thus, contigs = x means (x + 1) pages are contiguous.
*/
for (i = 1, pi = 0, ipfn = 0, contigs = 0; i < nr_pages; i++) {
ret = follow_pfn(vma, addr, &pfn);
if (ret)
break;
if ((pfns[ipfn] == (pfn - (i - pi))) &&
!((contigs + 1) & PAGE_MASK)) {
contigs++;
} else {
pfns[ipfn] <<= PAGE_SHIFT;
pfns[ipfn] |= contigs;
ipfn++;
pi = i;
contigs = 0;
pfns[ipfn] = pfn;
}
addr += PAGE_SIZE;
}
if (i == nr_pages) {
pfns[ipfn] <<= PAGE_SHIFT;
pfns[ipfn] |= contigs;
nr_pages = ipfn + 1;
} else {
ret = -EINVAL;
goto err_follow_pfn;
}
ret = sg_alloc_table(sgt, nr_pages, GFP_KERNEL);
if (ret)
goto err_follow_pfn;
for_each_sg(sgt->sgl, sg, nr_pages, i) {
sg_set_page(sg, phys_to_page(pfns[i]),
(((pfns[i] & ~PAGE_MASK) + 1) << PAGE_SHIFT) - offset,
offset);
offset = 0;
}
err_follow_pfn:
kfree(pfns);
return ret;
}
static struct dma_buf *vb2_ion_get_user_pages(unsigned long start,
unsigned long len,
int write)
{
size_t last_size = 0;
struct page **pages;
int nr_pages;
int ret = 0, i;
off_t start_off;
struct vb2_ion_dmabuf_data *priv;
struct scatterlist *sgl;
struct dma_buf *dbuf;
last_size = (start + len) & ~PAGE_MASK;
if (last_size == 0)
last_size = PAGE_SIZE;
start_off = offset_in_page(start);
start = round_down(start, PAGE_SIZE);
nr_pages = PFN_DOWN(PAGE_ALIGN(len + start_off));
pages = vzalloc(nr_pages * sizeof(*pages));
if (!pages)
return ERR_PTR(-ENOMEM);
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv) {
ret = -ENOMEM;
goto err_privdata;
}
ret = get_user_pages(current, current->mm, start,
nr_pages, write, 0, pages, NULL);
if (ret < 0) {
ret = pfnmap_digger(&priv->sgt, start, nr_pages, start_off);
if (ret)
goto err_pfnmap;
priv->is_pfnmap = true;
} else {
if (ret != nr_pages) {
nr_pages = ret;
ret = -EFAULT;
goto err_alloc_sg;
}
ret = sg_alloc_table(&priv->sgt, nr_pages, GFP_KERNEL);
if (ret)
goto err_alloc_sg;
sgl = priv->sgt.sgl;
sg_set_page(sgl, pages[0],
(nr_pages == 1) ? len : PAGE_SIZE - start_off,
start_off);
sg_dma_address(sgl) = page_to_phys(sg_page(sgl));
sgl = sg_next(sgl);
/* nr_pages == 1 if sgl == NULL here */
for (i = 1; i < (nr_pages - 1); i++) {
sg_set_page(sgl, pages[i], PAGE_SIZE, 0);
sg_dma_address(sgl) = page_to_phys(sg_page(sgl));
sgl = sg_next(sgl);
}
if (sgl) {
sg_set_page(sgl, pages[i], last_size, 0);
sg_dma_address(sgl) = page_to_phys(sg_page(sgl));
}
priv->is_pfnmap = false;
}
priv->write = write;
dbuf = dma_buf_export(priv, &vb2_ion_dmabuf_ops, len, O_RDWR);
if (IS_ERR(dbuf)) {
sg_free_table(&priv->sgt);
ret = PTR_ERR(dbuf);
goto err_alloc_sg;
}
vfree(pages);
return dbuf;
err_alloc_sg:
for (i = 0; i < nr_pages; i++)
put_page(pages[i]);
err_pfnmap:
kfree(priv);
err_privdata:
vfree(pages);
return ERR_PTR(ret);
}
static void vb2_ion_put_vma(struct vm_area_struct *vma)
{
while (vma) {
struct vm_area_struct *tmp;
tmp = vma;
vma = vma->vm_prev;
vb2_put_vma(tmp);
}
}
/*
* Verify the user's input and
* obtain the reference to the vma against the user's area
* Returns the last node of doubly linked list of replicated vma.
*/
static struct vm_area_struct *vb2_ion_get_vma(struct device *dev,
unsigned long vaddr, unsigned long size)
{
struct vm_area_struct *new_vma = NULL;
struct vm_area_struct *vma;
unsigned long addr = vaddr;
unsigned long len = size;
if ((vaddr + size) <= vaddr) {
vb2ion_err(dev, "size overflow in user area, [%#lx, %#lx)\n",
vaddr, size);
return NULL;
}
for (vma = find_vma(current->mm, addr);
vma && len && (addr >= vma->vm_start); vma = vma->vm_next) {
struct vm_area_struct *cur_vma;
cur_vma = vb2_get_vma(vma);
if (new_vma) {
if ((cur_vma->vm_file != new_vma->vm_file) ||
(cur_vma->vm_ops != new_vma->vm_ops)) {
vb2ion_err(dev,
"[%#lx, %#lx) crosses disparate vmas\n",
vaddr, size);
vb2_put_vma(cur_vma);
break;
}
new_vma->vm_next = cur_vma;
}
cur_vma->vm_prev = new_vma;
new_vma = cur_vma;
if ((addr + len) <= vma->vm_end) {
addr = addr + len;
len = 0;
} else {
len -= vma->vm_end - addr;
addr = vma->vm_end;
}
}
if (len) { /* error detected */
vb2ion_err(dev, "Invalid user area [%#lx, %#lx)\n",
vaddr, size);
vb2_ion_put_vma(new_vma);
return NULL;
}
while (new_vma && new_vma->vm_prev)
new_vma = new_vma->vm_prev;
return new_vma;
}
static void *vb2_ion_get_userptr(void *alloc_ctx, unsigned long vaddr,
unsigned long size, int write, int plane)
{
struct vb2_ion_context *ctx = alloc_ctx;
struct vb2_ion_buf *buf = NULL;
struct vm_area_struct *vma;
void *p_ret = NULL;
vma = vb2_ion_get_vma(ctx->dev, vaddr, size);
if (!vma) {
dev_err(ctx->dev,
"%s: Failed to holding user buffer @ %#lx/%#lx\n",
__func__, vaddr, size);
return ERR_PTR(-EINVAL);
}
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf) {
dev_err(ctx->dev, "%s: Not enough memory\n", __func__);
goto err_alloc_buf;
}
if (vma->vm_file)
buf->dma_buf = get_dma_buf_file(vma->vm_file);
if (!vma->vm_file || !buf->dma_buf) {
buf->dma_buf = vb2_ion_get_user_pages(vaddr, size, write);
if (IS_ERR(buf->dma_buf)) {
dev_err(ctx->dev,
"%s: User buffer @ %#lx/%#lx is not allocated by ION\n",
__func__, vaddr, size);
p_ret = buf->dma_buf;
goto err_get_user_pages;
}
} else {
buf->cookie.offset = vaddr - vma->vm_start;
buf->ion = true;
}
buf->ctx = ctx;
buf->direction = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
buf->size = size;
buf->vma = vma;
buf->attachment = dma_buf_attach(buf->dma_buf, ctx->dev);
if (IS_ERR(buf->attachment)) {
dev_err(ctx->dev, "%s: Failed to pin user buffer @ %#lx/%#lx\n",
__func__, vaddr, size);
p_ret = buf->attachment;
goto err_attach;
}
buf->cookie.sgt = dma_buf_map_attachment(buf->attachment,
buf->direction);
if (IS_ERR(buf->cookie.sgt)) {
dev_err(ctx->dev,
"%s: Failed to get sgt of user buffer @ %#lx/%#lx\n",
__func__, vaddr, size);
p_ret = buf->cookie.sgt;
goto err_map_attachment;
}
if (ctx_iommu(ctx))
buf->cookie.ioaddr = iovmm_map(ctx->dev, buf->cookie.sgt->sgl,
buf->cookie.offset, size, buf->direction,
plane);
if (IS_ERR_VALUE(buf->cookie.ioaddr)) {
dev_err(ctx->dev,
"%s: Failed to alloc IOVA of user buffer @ %#lx/%#lx\n",
__func__, vaddr, size);
p_ret = (void *)buf->cookie.ioaddr;
goto err_iovmm;
}
if ((pgprot_noncached(buf->vma->vm_page_prot)
== buf->vma->vm_page_prot)
|| (pgprot_writecombine(buf->vma->vm_page_prot)
== buf->vma->vm_page_prot))
buf->cached = false;
else
buf->cached = true;
return buf;
err_iovmm:
dma_buf_unmap_attachment(buf->attachment, buf->cookie.sgt,
buf->direction);
err_map_attachment:
dma_buf_detach(buf->dma_buf, buf->attachment);
err_attach:
dma_buf_put(buf->dma_buf);
err_get_user_pages:
kfree(buf);
err_alloc_buf:
vb2_ion_put_vma(vma);
return p_ret;
}
static void vb2_ion_put_userptr(void *mem_priv)
{
struct vb2_ion_buf *buf = mem_priv;
if (buf->kva) {
dma_buf_kunmap(buf->dma_buf, buf->cookie.offset / PAGE_SIZE,
buf->kva - (buf->cookie.offset & ~PAGE_SIZE));
dma_buf_end_cpu_access(buf->dma_buf, buf->cookie.offset,
buf->size, DMA_FROM_DEVICE);
}
if (ctx_iommu(buf->ctx))
iovmm_unmap(buf->ctx->dev, buf->cookie.ioaddr);
dma_buf_unmap_attachment(buf->attachment, buf->cookie.sgt,
buf->direction);
vb2_ion_put_vma(buf->vma);
dma_buf_detach(buf->dma_buf, buf->attachment);
dma_buf_put(buf->dma_buf);
kfree(buf);
}
const struct vb2_mem_ops vb2_ion_memops = {
.alloc = vb2_ion_alloc,
.put = vb2_ion_put,
.cookie = vb2_ion_cookie,
.vaddr = vb2_ion_vaddr,
.mmap = vb2_ion_mmap,
.map_dmabuf = vb2_ion_map_dmabuf,
.unmap_dmabuf = vb2_ion_unmap_dmabuf,
.attach_dmabuf = vb2_ion_attach_dmabuf,
.detach_dmabuf = vb2_ion_detach_dmabuf,
.get_userptr = vb2_ion_get_userptr,
.put_userptr = vb2_ion_put_userptr,
.num_users = vb2_ion_num_users,
};
EXPORT_SYMBOL_GPL(vb2_ion_memops);
void vb2_ion_sync_for_device(void *cookie, off_t offset, size_t size,
enum dma_data_direction dir)
{
struct vb2_ion_buf *buf = container_of(cookie,
struct vb2_ion_buf, cookie);
dev_dbg(buf->ctx->dev, "syncing for device, dmabuf: %p, kva: %p, "
"size: %zd, dir: %d\n", buf->dma_buf, buf->kva, size, dir);
if (buf->kva) {
if ((offset + size) > buf->size)
size -= (offset + size) - buf->size;
exynos_ion_sync_vaddr_for_device(buf->ctx->dev,
buf->kva, size, offset, dir);
} else if (buf->dma_buf && buf->ion) {
exynos_ion_sync_dmabuf_for_device(buf->ctx->dev,
buf->dma_buf, size, dir);
} else {
if (buf->cached)
exynos_ion_sync_sg_for_device(buf->ctx->dev, size,
buf->cookie.sgt, dir);
}
}
EXPORT_SYMBOL_GPL(vb2_ion_sync_for_device);
void vb2_ion_sync_for_cpu(void *cookie, off_t offset, size_t size,
enum dma_data_direction dir)
{
struct vb2_ion_buf *buf = container_of(cookie,
struct vb2_ion_buf, cookie);
dev_dbg(buf->ctx->dev, "syncing for cpu, dmabuf: %p, kva: %p, "
"size: %zd, dir: %d\n", buf->dma_buf, buf->kva, size, dir);
if (buf->kva) {
if ((offset + size) > buf->size)
size -= (offset + size) - buf->size;
exynos_ion_sync_vaddr_for_cpu(buf->ctx->dev,
buf->kva, size, offset, dir);
} else if (buf->dma_buf && buf->ion) {
exynos_ion_sync_dmabuf_for_cpu(buf->ctx->dev,
buf->dma_buf, size, dir);
} else {
exynos_ion_sync_sg_for_cpu(buf->ctx->dev, size,
buf->cookie.sgt, dir);
}
}
EXPORT_SYMBOL_GPL(vb2_ion_sync_for_cpu);
int vb2_ion_buf_prepare(struct vb2_buffer *vb)
{
int i;
enum dma_data_direction dir;
dir = V4L2_TYPE_IS_OUTPUT(vb->v4l2_buf.type) ?
DMA_TO_DEVICE : DMA_FROM_DEVICE;
for (i = 0; i < vb->num_planes; i++) {
struct vb2_ion_buf *buf = vb->planes[i].mem_priv;
if (!buf->vma)
return 0;
vb2_ion_sync_for_device((void *) &buf->cookie, 0,
buf->size, dir);
}
return 0;
}
EXPORT_SYMBOL_GPL(vb2_ion_buf_prepare);
int vb2_ion_buf_finish(struct vb2_buffer *vb)
{
int i;
enum dma_data_direction dir;
dir = V4L2_TYPE_IS_OUTPUT(vb->v4l2_buf.type) ?
DMA_TO_DEVICE : DMA_FROM_DEVICE;
for (i = 0; i < vb->num_planes; i++) {
struct vb2_ion_buf *buf = vb->planes[i].mem_priv;
if (!buf->vma)
return 0;
vb2_ion_sync_for_cpu((void *) &buf->cookie, 0,
buf->size, dir);
}
return 0;
}
EXPORT_SYMBOL_GPL(vb2_ion_buf_finish);
void vb2_ion_detach_iommu(void *alloc_ctx)
{
struct vb2_ion_context *ctx = alloc_ctx;
if (!ctx_iommu(ctx))
return;
mutex_lock(&ctx->lock);
BUG_ON(ctx->iommu_active_cnt == 0);
if (--ctx->iommu_active_cnt == 0 && !ctx->protected)
iovmm_deactivate(ctx->dev);
mutex_unlock(&ctx->lock);
}
EXPORT_SYMBOL_GPL(vb2_ion_detach_iommu);
int vb2_ion_attach_iommu(void *alloc_ctx)
{
struct vb2_ion_context *ctx = alloc_ctx;
int ret = 0;
if (!ctx_iommu(ctx))
return -ENOENT;
mutex_lock(&ctx->lock);
if (ctx->iommu_active_cnt == 0 && !ctx->protected)
ret = iovmm_activate(ctx->dev);
if (!ret)
ctx->iommu_active_cnt++;
mutex_unlock(&ctx->lock);
return ret;
}
EXPORT_SYMBOL_GPL(vb2_ion_attach_iommu);
MODULE_AUTHOR("Cho KyongHo <pullip.cho@samsung.com>");
MODULE_AUTHOR("Jinsung Yang <jsgood.yang@samsung.com>");
MODULE_DESCRIPTION("Android ION allocator handling routines for videobuf2");
MODULE_LICENSE("GPL");
|