This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
0064-media-intel-ipu4-virtio-IPU-virtio-backend-and-front.patch
3575 lines (3569 loc) · 94.9 KB
/
0064-media-intel-ipu4-virtio-IPU-virtio-backend-and-front.patch
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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "Yew, Chang Ching" <[email protected]>
Date: Tue, 23 Oct 2018 13:26:38 +0800
Subject: [PATCH] media: intel-ipu4: virtio: IPU virtio backend and frontend
drivers base-code
Base code to enable IPU virtualization for ACRN squashed from
following patches from
https://github.com/intel/linux-intel-lts/tree/4.14/base/drivers/media/pci/intel/virtio/
These patches added the virtio front end driver support for user OS and
virtio back end driver support for service OS.
ff9935 media: intel-ipu4: [VIRT] Poll function to return if put buf list is not empty
4943b7 media: intel-ipu4: [VIRT] Fxied failed to stream close properly issue.
a2d80c media: intel-ipu4: [VIRT] Adding x86_64 arch dependency.
7da2ee media: intel-ipu4: [VIRT] vhm_request struct removed the valid member.
1865e3 media: intel-ipu4: [VIRT] Fix vq get idx func handling of multi VQ concurrent request.
113db9 media: intel-ipu4: [VIRT] Fixed memory leak in BE get buf function.
35aa2a media: intel-ipu4: [VIRT] Implementing poll without wait at BE.
dd2c7a media: intel-ipu4: [VIRT] Implementation for 2 VQs
660300 media: intel-ipu4: [VIRT] Fixing synchronization with locks
a74061 media: intel-ipu4: [VIRT] Added 32-bit compatibility mode.
06ca2c media: intel-ipu4: [VIRT] Fixing buffer lookup issue
a67055 media: intel-ipu4: [VIRT] Removing compilation warnings & printk
1d0bf8 media: intel-ipu4: [VIRT] Virtio implementation of PUT_BUF & modify get buf list
71c663 media: intel-ipu4: [VIRT] Creating ipu mediation baseline v0.1
848b4f media: intel-ipu4: [VIRT] Buffer mapping between sos & uos
b5f40a media: intel-ipu4: [VIRT] Adding Buffer mapping & virtual stream initialization.
f08c9d media: intel-ipu4: [VIRT] Base code for IPU virtio support.
Change-Id: I3912b9838d7acef1806d2f9045602360b8f98212
Signed-off-by: Yew, Chang Ching <[email protected]>
---
drivers/media/pci/intel/virtio/Makefile | 10 +
drivers/media/pci/intel/virtio/Makefile.virt | 22 +
.../intel/virtio/intel-ipu4-para-virt-drv.c | 1330 +++++++++++++++++
.../intel/virtio/intel-ipu4-para-virt-drv.h | 38 +
.../virtio/intel-ipu4-virtio-be-bridge.c | 202 +++
.../virtio/intel-ipu4-virtio-be-bridge.h | 26 +
.../virtio/intel-ipu4-virtio-be-pipeline.c | 211 +++
.../virtio/intel-ipu4-virtio-be-pipeline.h | 27 +
.../virtio/intel-ipu4-virtio-be-stream.c | 389 +++++
.../virtio/intel-ipu4-virtio-be-stream.h | 26 +
.../pci/intel/virtio/intel-ipu4-virtio-be.c | 520 +++++++
.../pci/intel/virtio/intel-ipu4-virtio-be.h | 15 +
.../intel/virtio/intel-ipu4-virtio-common.c | 61 +
.../intel/virtio/intel-ipu4-virtio-common.h | 131 ++
.../virtio/intel-ipu4-virtio-fe-payload.c | 55 +
.../virtio/intel-ipu4-virtio-fe-payload.h | 14 +
.../virtio/intel-ipu4-virtio-fe-pipeline.c | 44 +
.../virtio/intel-ipu4-virtio-fe-pipeline.h | 19 +
.../pci/intel/virtio/intel-ipu4-virtio-fe.c | 243 +++
19 files changed, 3383 insertions(+)
create mode 100644 drivers/media/pci/intel/virtio/Makefile
create mode 100644 drivers/media/pci/intel/virtio/Makefile.virt
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-para-virt-drv.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-para-virt-drv.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be-bridge.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be-bridge.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be-pipeline.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be-pipeline.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be-stream.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be-stream.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-be.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-common.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-common.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-fe-payload.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-fe-payload.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-fe-pipeline.c
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-fe-pipeline.h
create mode 100644 drivers/media/pci/intel/virtio/intel-ipu4-virtio-fe.c
diff --git a/drivers/media/pci/intel/virtio/Makefile b/drivers/media/pci/intel/virtio/Makefile
new file mode 100644
index 000000000000..0f4eab5addfc
--- /dev/null
+++ b/drivers/media/pci/intel/virtio/Makefile
@@ -0,0 +1,10 @@
+ifneq ($(EXTERNAL_BUILD), 1)
+srcpath := $(srctree)
+endif
+
+IPU_STEP = bxtB0
+
+include $(srcpath)/$(src)/Makefile.virt
+
+ccflags-y += -I$(srcpath)/$(src)/../../../../../include/
+ccflags-y += -I$(srcpath)/$(src)/../
diff --git a/drivers/media/pci/intel/virtio/Makefile.virt b/drivers/media/pci/intel/virtio/Makefile.virt
new file mode 100644
index 000000000000..c3c30c4bf921
--- /dev/null
+++ b/drivers/media/pci/intel/virtio/Makefile.virt
@@ -0,0 +1,22 @@
+ifndef IPU_STEP
+ $(error No IPU_STEP was defined. Stopping.)
+endif
+
+TARGET_MODULE:=intel-ipu-virt-$(IPU_STEP)
+
+$(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-common.o
+
+
+ifdef CONFIG_VIDEO_INTEL_IPU_VIRTIO_BE
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-be-pipeline.o
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-be-bridge.o
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-be.o
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-be-stream.o
+else
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-fe-pipeline.o
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-fe-payload.o
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-virtio-fe.o
+ $(TARGET_MODULE)-objs += ../virtio/intel-ipu4-para-virt-drv.o
+endif
+
+obj-$(CONFIG_VIDEO_INTEL_IPU_ACRN) := $(TARGET_MODULE).o
diff --git a/drivers/media/pci/intel/virtio/intel-ipu4-para-virt-drv.c b/drivers/media/pci/intel/virtio/intel-ipu4-para-virt-drv.c
new file mode 100644
index 000000000000..3f6d541c87fe
--- /dev/null
+++ b/drivers/media/pci/intel/virtio/intel-ipu4-para-virt-drv.c
@@ -0,0 +1,1330 @@
+// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
+/*
+ * Copyright (C) 2018 Intel Corporation
+ */
+
+#include <linux/module.h>
+#include <linux/miscdevice.h>
+#include <linux/kthread.h>
+#include <linux/poll.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+#include <linux/dma-buf.h>
+#include <linux/compat.h>
+
+#include "intel-ipu4-virtio-common.h"
+#include "intel-ipu4-para-virt-drv.h"
+#include "intel-ipu4-virtio-fe-pipeline.h"
+#include "intel-ipu4-virtio-fe-payload.h"
+#include "./ici/ici-isys-stream.h"
+#include "./ici/ici-isys-pipeline-device.h"
+
+
+static dev_t virt_pipeline_dev_t;
+static struct class *virt_pipeline_class;
+static struct ici_isys_pipeline_device *pipeline_dev;
+
+static dev_t virt_stream_dev_t;
+static struct class *virt_stream_class;
+static int virt_stream_devs_registered;
+static int stream_dev_init;
+
+static struct ipu4_virtio_ctx *g_fe_priv;
+
+struct mutex fop_mutex;
+
+#ifdef CONFIG_COMPAT
+struct timeval32 {
+ __u32 tv_sec;
+ __u32 tv_usec;
+} __attribute__((__packed__));
+
+struct ici_frame_plane32 {
+ __u32 bytes_used;
+ __u32 length;
+ union {
+ compat_uptr_t userptr;
+ __s32 dmafd;
+ } mem;
+ __u32 data_offset;
+ __u32 reserved[2];
+} __attribute__((__packed__));
+
+struct ici_frame_info32 {
+ __u32 frame_type;
+ __u32 field;
+ __u32 flag;
+ __u32 frame_buf_id;
+ struct timeval32 frame_timestamp;
+ __u32 frame_sequence_id;
+ __u32 mem_type; /* _DMA or _USER_PTR */
+ struct ici_frame_plane32 frame_planes[ICI_MAX_PLANES]; /* multi-planar */
+ __u32 num_planes; /* =1 single-planar > 1 multi-planar array size */
+ __u32 reserved[2];
+} __attribute__((__packed__));
+
+#define ICI_IOC_GET_BUF32 _IOWR(MAJOR_STREAM, 3, struct ici_frame_info32)
+#define ICI_IOC_PUT_BUF32 _IOWR(MAJOR_STREAM, 4, struct ici_frame_info32)
+
+static void copy_from_user_frame_info32(struct ici_frame_info *kp, struct ici_frame_info32 __user *up)
+{
+ int i;
+ compat_uptr_t userptr;
+
+ get_user(kp->frame_type, &up->frame_type);
+ get_user(kp->field, &up->field);
+ get_user(kp->flag, &up->flag);
+ get_user(kp->frame_buf_id, &up->frame_buf_id);
+ get_user(kp->frame_timestamp.tv_sec, &up->frame_timestamp.tv_sec);
+ get_user(kp->frame_timestamp.tv_usec, &up->frame_timestamp.tv_usec);
+ get_user(kp->frame_sequence_id, &up->frame_sequence_id);
+ get_user(kp->mem_type, &up->mem_type);
+ get_user(kp->num_planes, &up->num_planes);
+ for (i = 0; i < kp->num_planes; i++) {
+ get_user(kp->frame_planes[i].bytes_used, &up->frame_planes[i].bytes_used);
+ get_user(kp->frame_planes[i].length, &up->frame_planes[i].length);
+ if (kp->mem_type == ICI_MEM_USERPTR) {
+ get_user(userptr, &up->frame_planes[i].mem.userptr);
+ kp->frame_planes[i].mem.userptr = (unsigned long) compat_ptr(userptr);
+ } else if (kp->mem_type == ICI_MEM_DMABUF) {
+ get_user(kp->frame_planes[i].mem.dmafd, &up->frame_planes[i].mem.dmafd);
+ };
+ get_user(kp->frame_planes[i].data_offset, &up->frame_planes[i].data_offset);
+ }
+}
+
+static void copy_to_user_frame_info32(struct ici_frame_info *kp, struct ici_frame_info32 __user *up)
+{
+ int i;
+ compat_uptr_t userptr;
+
+ put_user(kp->frame_type, &up->frame_type);
+ put_user(kp->field, &up->field);
+ put_user(kp->flag, &up->flag);
+ put_user(kp->frame_buf_id, &up->frame_buf_id);
+ put_user(kp->frame_timestamp.tv_sec, &up->frame_timestamp.tv_sec);
+ put_user(kp->frame_timestamp.tv_usec, &up->frame_timestamp.tv_usec);
+ put_user(kp->frame_sequence_id, &up->frame_sequence_id);
+ put_user(kp->mem_type, &up->mem_type);
+ put_user(kp->num_planes, &up->num_planes);
+ for (i = 0; i < kp->num_planes; i++) {
+ put_user(kp->frame_planes[i].bytes_used, &up->frame_planes[i].bytes_used);
+ put_user(kp->frame_planes[i].length, &up->frame_planes[i].length);
+ if (kp->mem_type == ICI_MEM_USERPTR) {
+ userptr = (unsigned long)compat_ptr(kp->frame_planes[i].mem.userptr);
+ put_user(userptr, &up->frame_planes[i].mem.userptr);
+ } else if (kp->mem_type == ICI_MEM_DMABUF) {
+ get_user(kp->frame_planes[i].mem.dmafd, &up->frame_planes[i].mem.dmafd);
+ }
+ put_user(kp->frame_planes[i].data_offset, &up->frame_planes[i].data_offset);
+ }
+}
+#endif
+
+static int get_userpages(struct device *dev, struct ici_frame_plane *frame_plane,
+ struct ici_kframe_plane *kframe_plane)
+{
+ unsigned long start, end, addr;
+ int npages, array_size;
+ struct page **pages;
+ int nr = 0;
+ int ret = 0;
+ struct sg_table *sgt;
+ unsigned int i;
+ u64 page_table_ref;
+ u64 *page_table;
+ addr = (unsigned long)frame_plane->mem.userptr;
+ start = addr & PAGE_MASK;
+ end = PAGE_ALIGN(addr + frame_plane->length);
+ npages = (end - start) >> PAGE_SHIFT;
+ array_size = npages * sizeof(struct page *);
+
+ if (!npages)
+ return -EINVAL;
+
+ page_table = kcalloc(npages, sizeof(*page_table), GFP_KERNEL);
+ if (!page_table) {
+ pr_err("Shared Page table for mediation failed\n");
+ return -ENOMEM;
+ }
+
+ pr_debug("%s:%d Number of Pages:%d frame_length:%d\n", __func__, __LINE__, npages, frame_plane->length);
+ sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
+ if (!sgt)
+ return -ENOMEM;
+ if (array_size <= PAGE_SIZE)
+ pages = kzalloc(array_size, GFP_KERNEL);
+ else
+ pages = vzalloc(array_size);
+ if (!pages)
+ return -ENOMEM;
+
+ down_read(¤t->mm->mmap_sem);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
+ nr = get_user_pages(current, current->mm,
+ start, npages, 1, 0, pages, NULL);
+#else
+ nr = get_user_pages(start, npages, FOLL_WRITE, pages, NULL);
+#endif
+ if (nr < npages)
+ goto error_free_pages;
+ /* Share physical address of pages */
+ for (i = 0; i < npages; i++)
+ page_table[i] = page_to_phys(pages[i]);
+
+ pr_debug("UOS phy page add %lld offset:%ld\n", page_table[0], addr & ~PAGE_MASK);
+ page_table_ref = virt_to_phys(page_table);
+ kframe_plane->page_table_ref = page_table_ref;
+ kframe_plane->npages = npages;
+ up_read(¤t->mm->mmap_sem);
+ return ret;
+error_free_pages:
+ if (pages) {
+ for (i = 0; i < nr; i++)
+ put_page(pages[i]);
+ }
+ kfree(sgt);
+ return -ENOMEM;
+}
+
+static struct ici_frame_buf_wrapper *frame_buf_lookup(struct ici_isys_frame_buf_list *buf_list, struct ici_frame_info *user_frame_info)
+{
+ struct ici_frame_buf_wrapper *buf;
+ int i;
+ int mem_type = user_frame_info->mem_type;
+
+ list_for_each_entry(buf, &buf_list->getbuf_list, uos_node) {
+ for (i = 0; i < user_frame_info->num_planes; i++) {
+ struct ici_frame_plane *new_plane = &user_frame_info->frame_planes[i];
+ struct ici_frame_plane *cur_plane = &buf->frame_info.frame_planes[i];
+
+ if (buf->state != ICI_BUF_PREPARED &&
+ buf->state != ICI_BUF_DONE)
+ continue;
+
+ switch (mem_type) {
+ case ICI_MEM_USERPTR:
+ if (new_plane->mem.userptr == cur_plane->mem.userptr)
+ return buf;
+ break;
+ case ICI_MEM_DMABUF:
+ if (new_plane->mem.dmafd == cur_plane->mem.dmafd)
+ return buf;
+ break;
+ }
+ //TODO: add multiplaner checks
+ }
+ }
+ return NULL;
+}
+static void put_userpages(struct ici_kframe_plane *kframe_plane)
+{
+ struct sg_table *sgt = kframe_plane->sgt;
+ struct scatterlist *sgl;
+ unsigned int i;
+ struct mm_struct *mm = current->active_mm;
+
+ if (!mm) {
+ pr_err("Failed to get active mm_struct ptr from current process.\n");
+ return;
+ }
+
+ down_read(&mm->mmap_sem);
+ for_each_sg(sgt->sgl, sgl, sgt->orig_nents, i) {
+ struct page *page = sg_page(sgl);
+
+ unsigned int npages = PAGE_ALIGN(sgl->offset + sgl->length) >> PAGE_SHIFT;
+ unsigned int page_no;
+
+ for (page_no = 0; page_no < npages; ++page_no, ++page) {
+ set_page_dirty_lock(page);
+ put_page(page);
+ }
+ }
+
+ kfree(sgt);
+ kframe_plane->sgt = NULL;
+
+ up_read(&mm->mmap_sem);
+}
+
+static void put_dma(struct ici_kframe_plane *kframe_plane)
+{
+ struct sg_table *sgt = kframe_plane->sgt;
+
+ if (WARN_ON(!kframe_plane->db_attach)) {
+ pr_err("trying to unpin a not attached buffer\n");
+ return;
+ }
+
+ if (WARN_ON(!sgt)) {
+ pr_err("dmabuf buffer is already unpinned\n");
+ return;
+ }
+
+ if (kframe_plane->kaddr) {
+ dma_buf_vunmap(kframe_plane->db_attach->dmabuf,
+ kframe_plane->kaddr);
+ kframe_plane->kaddr = NULL;
+ }
+ dma_buf_unmap_attachment(kframe_plane->db_attach, sgt,
+ DMA_BIDIRECTIONAL);
+
+ kframe_plane->dma_addr = 0;
+ kframe_plane->sgt = NULL;
+
+}
+
+static int map_dma(struct device *dev, struct ici_frame_plane *frame_plane,
+ struct ici_kframe_plane *kframe_plane)
+{
+
+ int ret = 0;
+ int fd = frame_plane->mem.dmafd;
+
+ kframe_plane->dbdbuf = dma_buf_get(fd);
+ if (!kframe_plane->dbdbuf) {
+ ret = -EINVAL;
+ goto error;
+ }
+
+ if (frame_plane->length == 0)
+ kframe_plane->length = kframe_plane->dbdbuf->size;
+ else
+ kframe_plane->length = frame_plane->length;
+
+ kframe_plane->fd = fd;
+ kframe_plane->db_attach = dma_buf_attach(kframe_plane->dbdbuf, dev);
+
+ if (IS_ERR(kframe_plane->db_attach)) {
+ ret = PTR_ERR(kframe_plane->db_attach);
+ goto error_put;
+ }
+
+ kframe_plane->sgt = dma_buf_map_attachment(kframe_plane->db_attach,
+ DMA_BIDIRECTIONAL);
+ if (IS_ERR_OR_NULL(kframe_plane->sgt)) {
+ ret = -EINVAL;
+ kframe_plane->sgt = NULL;
+ pr_err("map attachment failed\n");
+ goto error_detach;
+ }
+
+ kframe_plane->dma_addr = sg_dma_address(kframe_plane->sgt->sgl);
+ kframe_plane->kaddr = dma_buf_vmap(kframe_plane->dbdbuf);
+
+ if (!kframe_plane->kaddr) {
+ ret = -EINVAL;
+ goto error_detach;
+ }
+
+ pr_debug("MAPBUF: mapped fd %d\n", fd);
+
+ return 0;
+
+error_detach:
+ dma_buf_detach(kframe_plane->dbdbuf, kframe_plane->db_attach);
+error_put:
+ dma_buf_put(kframe_plane->dbdbuf);
+error:
+ return ret;
+}
+
+static void unmap_buf(struct ici_frame_buf_wrapper *buf)
+{
+ int i;
+
+ for (i = 0; i < buf->frame_info.num_planes; i++) {
+ struct ici_kframe_plane *kframe_plane =
+ &buf->kframe_info.planes[i];
+ switch (kframe_plane->mem_type) {
+ case ICI_MEM_USERPTR:
+ put_userpages(kframe_plane);
+ break;
+ case ICI_MEM_DMABUF:
+ put_dma(kframe_plane);
+ break;
+ default:
+ pr_debug("not supported memory type: %d\n", kframe_plane->mem_type);
+ break;
+ }
+ }
+}
+struct ici_frame_buf_wrapper *get_buf(struct virtual_stream *vstream, struct ici_frame_info *frame_info)
+{
+ int res;
+ unsigned i;
+ struct ici_frame_buf_wrapper *buf;
+
+ struct ici_kframe_plane *kframe_plane;
+ struct ici_isys_frame_buf_list *buf_list = &vstream->buf_list;
+ int mem_type = frame_info->mem_type;
+
+ if (mem_type != ICI_MEM_USERPTR && mem_type != ICI_MEM_DMABUF) {
+ pr_err("Memory type not supproted\n");
+ return NULL;
+ }
+
+ if (!frame_info->frame_planes[0].length) {
+ pr_err("User length not set\n");
+ return NULL;
+ }
+
+ buf = frame_buf_lookup(buf_list, frame_info);
+ if (buf) {
+ pr_debug("Frame buffer found in the list: %ld\n", buf->frame_info.frame_planes[0].mem.userptr);
+ buf->state = ICI_BUF_PREPARED;
+ return buf;
+ }
+ pr_debug("Creating new buffer in the list\n");
+ buf = kzalloc(sizeof(*buf), GFP_KERNEL);
+ if (!buf)
+ return NULL;
+
+ buf->buf_id = frame_info->frame_buf_id;
+ buf->uos_buf_list = buf_list;
+ memcpy(&buf->frame_info, frame_info, sizeof(buf->frame_info));
+
+ switch (mem_type) {
+ case ICI_MEM_USERPTR:
+ if (!frame_info->frame_planes[0].mem.userptr) {
+ pr_err("User pointer not define\n");
+ return NULL;
+ }
+ for (i = 0; i < frame_info->num_planes; i++) {
+ kframe_plane = &buf->kframe_info.planes[i];
+ kframe_plane->mem_type = ICI_MEM_USERPTR;
+ res = get_userpages(&vstream->strm_dev.dev, &frame_info->frame_planes[i],
+ kframe_plane);
+ if (res)
+ return NULL;
+ }
+ break;
+ case ICI_MEM_DMABUF:
+ for (i = 0; i < frame_info->num_planes; i++) {
+ kframe_plane = &buf->kframe_info.planes[i];
+ kframe_plane->mem_type = ICI_MEM_DMABUF;
+ res = map_dma(&vstream->strm_dev.dev, &frame_info->frame_planes[i],
+ kframe_plane);
+ if (res)
+ return NULL;
+ }
+
+ break;
+ }
+ mutex_lock(&buf_list->mutex);
+ buf->state = ICI_BUF_PREPARED;
+ list_add_tail(&buf->uos_node, &buf_list->getbuf_list);
+ mutex_unlock(&buf_list->mutex);
+ return buf;
+}
+
+//Call from Stream-OFF and if Stream-ON fails
+void buf_stream_cancel(struct virtual_stream *vstream)
+{
+ struct ici_isys_frame_buf_list *buf_list = &vstream->buf_list;
+ struct ici_frame_buf_wrapper *buf;
+ struct ici_frame_buf_wrapper *next_buf;
+
+ list_for_each_entry_safe(buf, next_buf,
+ &buf_list->getbuf_list, uos_node) {
+ list_del(&buf->uos_node);
+ }
+ list_for_each_entry_safe(buf, next_buf,
+ &buf_list->putbuf_list, uos_node) {
+ list_del(&buf->uos_node);
+ }
+}
+
+static int virt_isys_set_format(struct file *file, void *fh,
+ struct ici_stream_format *sf)
+{
+ struct ici_stream_device *strm_dev = fh;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ struct ipu4_virtio_req *req;
+ int rval = 0;
+ int op[10];
+
+ pr_debug("Calling Set Format\n");
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+
+ req->payload = virt_to_phys(sf);
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_SET_FORMAT, &op[0]);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_0);
+ if (rval) {
+ dev_err(&strm_dev->dev, "Failed to open virtual device\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ return rval;
+}
+
+static int virt_isys_stream_on(struct file *file, void *fh)
+{
+ struct ici_stream_device *strm_dev = fh;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ struct ipu4_virtio_req *req;
+ int rval = 0;
+ int op[10];
+ pr_debug("Calling Stream ON\n");
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_STREAM_ON, &op[0]);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_0);
+ if (rval) {
+ dev_err(&strm_dev->dev, "Failed to open virtual device\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req && !fe_ctx)
+ return -ENOMEM;
+
+ return rval;
+}
+
+static int virt_isys_stream_off(struct file *file, void *fh)
+{
+ struct ici_stream_device *strm_dev = fh;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ struct ipu4_virtio_req *req;
+ int rval = 0;
+ int op[10];
+
+ pr_debug("Calling Stream OFF\n");
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_STREAM_OFF, &op[0]);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_0);
+ if (rval) {
+ dev_err(&strm_dev->dev, "Failed to open virtual device\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ buf_stream_cancel(vstream);
+
+ return rval;
+}
+
+static int virt_isys_getbuf(struct file *file, void *fh,
+ struct ici_frame_info *user_frame_info)
+{
+ struct ici_stream_device *strm_dev = fh;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ struct ipu4_virtio_req *req;
+ struct ici_frame_buf_wrapper *buf;
+ int rval = 0;
+ int op[3];
+
+ pr_debug("Calling Get Buffer\n");
+
+ buf = get_buf(vstream, user_frame_info);
+ if (!buf) {
+ dev_err(&strm_dev->dev, "Failed to map buffer: %d\n", rval);
+ return -ENOMEM;
+ }
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+ op[2] = user_frame_info->mem_type;
+ req->payload = virt_to_phys(buf);
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_GET_BUF, &op[0]);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_0);
+ if (rval) {
+ dev_err(&strm_dev->dev, "Failed to Get Buffer\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ return rval;
+}
+
+static int virt_isys_putbuf(struct file *file, void *fh,
+ struct ici_frame_info *user_frame_info)
+{
+ struct ici_stream_device *strm_dev = fh;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ struct ipu4_virtio_req *req;
+ int rval = 0;
+ int op[2];
+
+ pr_debug("Calling Put Buffer\n");
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+ req->payload = virt_to_phys(user_frame_info);
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_PUT_BUF, &op[0]);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_0);
+ if (rval) {
+ dev_err(&strm_dev->dev, "Failed to Get Buffer\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req && !fe_ctx)
+ return -ENOMEM;
+
+ return rval;
+}
+
+static unsigned int stream_fop_poll(struct file *file, struct ici_stream_device *dev)
+{
+ struct ipu4_virtio_req *req;
+ struct virtual_stream *vstream = dev_to_vstream(dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ struct ici_stream_device *strm_dev = file->private_data;
+ int rval = 0;
+ int op[2];
+
+ dev_dbg(&strm_dev->dev, "stream_fop_poll %d\n", vstream->virt_dev_id);
+ get_device(&dev->dev);
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_POLL, &op[0]);
+
+ mutex_lock(&fop_mutex);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_1);
+ if (rval) {
+ mutex_unlock(&fop_mutex);
+ dev_err(&strm_dev->dev, "Failed to open virtual device\n");
+ kfree(req);
+ return rval;
+ }
+
+ mutex_unlock(&fop_mutex);
+
+ rval = req->func_ret;
+ kfree(req);
+
+ return rval;
+}
+
+static int virt_stream_fop_open(struct inode *inode, struct file *file)
+{
+ struct ici_stream_device *strm_dev = inode_to_intel_ipu_stream_device(inode);
+ struct ipu4_virtio_req *req;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ int rval = 0;
+ int op[3];
+ pr_debug("%s %d", __func__, vstream->virt_dev_id);
+ get_device(&strm_dev->dev);
+
+ file->private_data = strm_dev;
+
+ if (!fe_ctx)
+ return -EINVAL;
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req) {
+ dev_err(&strm_dev->dev, "Virtio Req buffer failed\n");
+ return -ENOMEM;
+ }
+
+ op[0] = vstream->virt_dev_id;
+ op[1] = 1;
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_DEVICE_OPEN, &op[0]);
+
+ mutex_lock(&fop_mutex);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_1);
+ if (rval) {
+ mutex_unlock(&fop_mutex);
+ dev_err(&strm_dev->dev, "Failed to open virtual device\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ mutex_unlock(&fop_mutex);
+
+ return rval;
+}
+
+static int virt_stream_fop_release(struct inode *inode, struct file *file)
+{
+ struct ici_stream_device *strm_dev = inode_to_intel_ipu_stream_device(inode);
+ struct ipu4_virtio_req *req;
+ struct virtual_stream *vstream = dev_to_vstream(strm_dev);
+ struct ipu4_virtio_ctx *fe_ctx = vstream->ctx;
+ int rval = 0;
+ int op[2];
+ pr_debug("%s %d", __func__, vstream->virt_dev_id);
+ put_device(&strm_dev->dev);
+
+ req = kcalloc(1, sizeof(*req), GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+
+ op[0] = vstream->virt_dev_id;
+ op[1] = 0;
+
+ intel_ipu4_virtio_create_req(req, IPU4_CMD_DEVICE_CLOSE, &op[0]);
+
+ mutex_lock(&fop_mutex);
+
+ rval = fe_ctx->bknd_ops->send_req(fe_ctx->domid, req, true, IPU_VIRTIO_QUEUE_1);
+ if (rval) {
+ mutex_unlock(&fop_mutex);
+ dev_err(&strm_dev->dev, "Failed to close virtual device\n");
+ kfree(req);
+ return rval;
+ }
+ kfree(req);
+
+ mutex_unlock(&fop_mutex);
+
+ return rval;
+}
+
+static unsigned int virt_stream_fop_poll(struct file *file,
+ struct poll_table_struct *poll)
+{
+ struct ici_stream_device *as = file->private_data;
+ unsigned int res = POLLERR | POLLHUP;
+
+ dev_dbg(&as->dev, "virt_stream_fop_poll for:%s\n", as->name);
+
+ res = stream_fop_poll(file, as);
+
+ //res = POLLIN;
+
+ dev_dbg(&as->dev, "virt_stream_fop_poll res %u\n", res);
+
+ return res;
+}
+
+static long virt_stream_ioctl32(struct file *file, unsigned int ioctl_cmd,
+ unsigned long ioctl_arg)
+{
+ union isys_ioctl_cmd_args {
+ struct ici_frame_info frame_info;
+ struct ici_stream_format sf;
+ };
+ void __user *up = compat_ptr(ioctl_arg);
+ union isys_ioctl_cmd_args *data = NULL;
+ int err = 0;
+ struct ici_stream_device *dev = file->private_data;
+
+ mutex_lock(dev->mutex);
+ switch (ioctl_cmd) {
+ case ICI_IOC_STREAM_ON:
+ pr_debug("IPU FE IOCTL STREAM_ON\n");
+ err = virt_isys_stream_on(file, dev);
+ break;
+ case ICI_IOC_STREAM_OFF:
+ pr_debug("IPU FE IOCTL STREAM_OFF\n");
+ err = virt_isys_stream_off(file, dev);
+ break;
+ case ICI_IOC_GET_BUF32:
+ pr_debug("IPU FE IOCTL GET_BUF\n");
+ data = (union isys_ioctl_cmd_args *) kzalloc(sizeof(union isys_ioctl_cmd_args), GFP_KERNEL);
+ copy_from_user_frame_info32(&data->frame_info, up);
+ err = virt_isys_getbuf(file, dev, &data->frame_info);
+ copy_to_user_frame_info32(&data->frame_info, up);
+ kfree(data);
+ if (err) {
+ mutex_unlock(dev->mutex);
+ return -EFAULT;
+ }
+ break;
+ case ICI_IOC_PUT_BUF32:
+ pr_debug("IPU FE IOCTL PUT_BUF\n");
+ data = (union isys_ioctl_cmd_args *) kzalloc(sizeof(union isys_ioctl_cmd_args), GFP_KERNEL);
+ copy_from_user_frame_info32(&data->frame_info, up);
+ err = virt_isys_putbuf(file, dev, &data->frame_info);
+ copy_to_user_frame_info32(&data->frame_info, up);
+ kfree(data);
+ if (err) {
+ mutex_unlock(dev->mutex);
+ return -EFAULT;
+ }
+ break;
+ case ICI_IOC_SET_FORMAT:
+ pr_debug("IPU FE IOCTL SET_FORMAT\n");
+ if (_IOC_SIZE(ioctl_cmd) > sizeof(union isys_ioctl_cmd_args)) {
+ mutex_unlock(dev->mutex);
+ return -ENOTTY;
+ }
+
+ data = (union isys_ioctl_cmd_args *) kzalloc(sizeof(union isys_ioctl_cmd_args), GFP_KERNEL);
+ err = copy_from_user(data, up, _IOC_SIZE(ioctl_cmd));
+ if (err) {
+ kfree(data);
+ mutex_unlock(dev->mutex);
+ return -EFAULT;
+ }
+ err = virt_isys_set_format(file, dev, &data->sf);
+ err = copy_to_user(up, data, _IOC_SIZE(ioctl_cmd));
+ if (err) {
+ kfree(data);
+ mutex_unlock(dev->mutex);
+ return -EFAULT;
+ }
+ kfree(data);
+ break;
+
+ default:
+ err = -ENOTTY;
+ break;
+ }
+
+ mutex_unlock(dev->mutex);
+
+ return 0;
+}
+
+static long virt_stream_ioctl(struct file *file, unsigned int ioctl_cmd,
+ unsigned long ioctl_arg)
+{
+ union isys_ioctl_cmd_args {
+ struct ici_frame_info frame_info;
+ struct ici_stream_format sf;
+ };
+ int err = 0;
+ union isys_ioctl_cmd_args *data = NULL;
+ struct ici_stream_device *dev = file->private_data;
+ void __user *up = (void __user *)ioctl_arg;
+
+ bool copy = (ioctl_cmd != ICI_IOC_STREAM_ON &&
+ ioctl_cmd != ICI_IOC_STREAM_OFF);
+
+ if (copy) {
+ if (_IOC_SIZE(ioctl_cmd) > sizeof(union isys_ioctl_cmd_args))
+ return -ENOTTY;
+
+ data = (union isys_ioctl_cmd_args *) kzalloc(sizeof(union isys_ioctl_cmd_args), GFP_KERNEL);
+ if (_IOC_DIR(ioctl_cmd) & _IOC_WRITE) {
+ err = copy_from_user(data, up,
+ _IOC_SIZE(ioctl_cmd));
+ if (err) {
+ kfree(data);
+ return -EFAULT;
+ }
+ }
+ }
+
+ mutex_lock(dev->mutex);
+ switch (ioctl_cmd) {
+ case ICI_IOC_STREAM_ON:
+ err = virt_isys_stream_on(file, dev);
+ break;
+ case ICI_IOC_STREAM_OFF:
+ err = virt_isys_stream_off(file, dev);
+ break;
+ case ICI_IOC_GET_BUF:
+ err = virt_isys_getbuf(file, dev, &data->frame_info);
+ break;
+ case ICI_IOC_PUT_BUF:
+ err = virt_isys_putbuf(file, dev, &data->frame_info);
+ break;
+ case ICI_IOC_SET_FORMAT:
+ err = virt_isys_set_format(file, dev, &data->sf);
+ break;