-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
rkvtunnel.c
1527 lines (1275 loc) · 37.2 KB
/
rkvtunnel.c
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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) Rockchip Electronics Co., Ltd.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/fdtable.h>
#include <linux/file.h>
#include <linux/freezer.h>
#include <linux/miscdevice.h>
#include <linux/of.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/dma-buf.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/kfifo.h>
#include <linux/debugfs.h>
#include <linux/random.h>
#include <linux/sync_file.h>
#include <linux/sched/task.h>
#include <linux/sched/clock.h>
#include <asm-generic/bug.h>
#include "rkvtunnel.h"
#define DEVICE_NAME "rkvtunnel"
#define RKVT_MAX_NAME_LENGTH 128
#define RKVT_POOL_SIZE 32
#define RKVT_MAX_WAIT_MS 4
#define RKVT_FENCE_WAIT_MS 3000
#define RKVT_DBG_USER (1U << 0)
#define RKVT_DBG_BUFFERS (1U << 1)
#define RKVT_DBG_CMD (1U << 2)
#define RKVT_DBG_FILE (1U << 3)
#define rkvt_dbg(mask, x...)\
do { if (unlikely(vt_dev_dbg & mask)) pr_info(x); } while (0)
enum rkvt_buf_status_e {
RKVT_BUF_QUEUE,
RKVT_BUF_DEQUEUE,
RKVT_BUF_ACQUIRE,
RKVT_BUF_RELEASE,
RKVT_BUF_FREE,
RKVT_BUF_BUTT,
};
union rkvt_ioc_arg {
struct rkvt_alloc_id_data alloc_data;
struct rkvt_ctrl_data ctrl_data;
struct rkvt_buf_data buffer_data;
};
struct rkvt_dev {
struct device *dev;
struct miscdevice mdev;
struct mutex inst_lock; /* protect inst_list and ints_idr */
struct idr inst_idr;
struct list_head list_inst; /* manage all instances */
struct mutex session_lock; /* protect sessions */
struct list_head list_session;
char *dev_name;
int inst_id_generator;
atomic64_t cid_generator;
struct dentry *debug_root;
};
struct rkvt_session {
struct list_head dev_link;
struct rkvt_dev *vt_dev;
struct list_head list_inst; /* manage instance in session */
enum rkvt_caller_e caller;
pid_t pid;
char name[RKVT_MAX_NAME_LENGTH];
char disp_name[RKVT_MAX_NAME_LENGTH];
int disp_serial;
int cid;
struct task_struct *task;
struct dentry *debug_root;
};
struct rkvt_buffer {
struct file *file_buf[MAX_BUF_HANDLE_FDS];
int fds_pro[MAX_BUF_HANDLE_FDS];
int fds_con[MAX_BUF_HANDLE_FDS];
struct file *ready_render_fence;
struct dma_fence *rendered_fence;
struct rkvt_session *session_pro;
int cid_pro;
struct rkvt_buf_base base;
};
struct rkvt_instance {
struct kref ref;
int id;
struct rkvt_dev *vt_dev;
struct mutex lock;
struct list_head dev_link;
struct list_head session_link;
struct rkvt_session *consumer;
struct rkvt_session *producer;
wait_queue_head_t wait_consumer;
wait_queue_head_t wait_producer;
struct dentry *debug_root;
int fcount;
DECLARE_KFIFO_PTR(fifo_to_consumer, struct rkvt_buffer*);
DECLARE_KFIFO_PTR(fifo_to_producer, struct rkvt_buffer*);
struct rkvt_buffer vt_buffers[RKVT_POOL_SIZE];
atomic64_t buf_id_generator;
};
static unsigned int vt_dev_dbg;
module_param(vt_dev_dbg, uint, 0644);
MODULE_PARM_DESC(vt_dev_dbg, "bit switch for vt debug information");
static const char *
rkvt_dbg_buf_status_to_string(int status)
{
const char *status_str;
switch (status) {
case RKVT_BUF_QUEUE:
status_str = "queued";
break;
case RKVT_BUF_DEQUEUE:
status_str = "dequeued";
break;
case RKVT_BUF_ACQUIRE:
status_str = "acquired";
break;
case RKVT_BUF_RELEASE:
status_str = "released";
break;
case RKVT_BUF_FREE:
status_str = "free";
break;
default:
status_str = "unknown";
}
return status_str;
}
static int rkvt_dbg_instance_show(struct seq_file *s, void *unused)
{
struct rkvt_instance *inst = s->private;
int i;
int size_to_con;
int size_to_pro;
int ref_count;
mutex_lock(&inst->lock);
size_to_con = kfifo_len(&inst->fifo_to_consumer);
size_to_pro = kfifo_len(&inst->fifo_to_producer);
ref_count = kref_read(&inst->ref);
seq_printf(s, "tunnel (%p) id=%d, ref=%d, fcount=%d\n",
inst, inst->id, ref_count, inst->fcount);
seq_puts(s, "-----------------------------------------------\n");
if (inst->consumer)
seq_printf(s, "consumer session (%s) %p\n",
inst->consumer->disp_name, inst->consumer);
if (inst->producer)
seq_printf(s, "producer session (%s) %p\n",
inst->producer->disp_name, inst->producer);
seq_puts(s, "-----------------------------------------------\n");
seq_printf(s, "to consumer fifo size:%d\n", size_to_con);
seq_printf(s, "to producer fifo size:%d\n", size_to_pro);
seq_puts(s, "-----------------------------------------------\n");
seq_puts(s, "buffers:\n");
for (i = 0; i < RKVT_POOL_SIZE; i++) {
struct rkvt_buffer *buffer = &inst->vt_buffers[i];
int status = buffer->base.buf_status;
seq_printf(s, " buffer produce_fd[0](%d) status(%s)\n",
buffer->fds_pro[0],
rkvt_dbg_buf_status_to_string(status));
}
seq_puts(s, "-----------------------------------------------\n");
mutex_unlock(&inst->lock);
return 0;
}
static int
rkvt_dbg_instance_open(struct inode *inode, struct file *file)
{
return single_open(file,
rkvt_dbg_instance_show,
inode->i_private);
}
static const struct file_operations dbg_instance_fops = {
.open = rkvt_dbg_instance_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int rkvt_dbg_session_show(struct seq_file *s, void *unused)
{
struct rkvt_session *session = s->private;
seq_printf(s, "session(%s) %p role %s cid %d\n",
session->disp_name, session,
session->caller == RKVT_CALLER_PRODUCER ?
"producer" : (session->caller == RKVT_CALLER_CONSUMER ?
"consumer" : "invalid"), session->cid);
seq_puts(s, "-----------------------------------------------\n");
return 0;
}
static int rkvt_dbg_session_open(struct inode *inode, struct file *file)
{
return single_open(file,
rkvt_dbg_session_show,
inode->i_private);
}
static const struct file_operations debug_session_fops = {
.open = rkvt_dbg_session_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __rkvt_close_fd(struct files_struct *files, unsigned int fd)
{
struct file *file;
struct fdtable *fdt;
spin_lock(&files->file_lock);
fdt = files_fdtable(files);
if (fd >= fdt->max_fds)
goto out_unlock;
file = fdt->fd[fd];
if (!file)
goto out_unlock;
rcu_assign_pointer(fdt->fd[fd], NULL);
spin_unlock(&files->file_lock);
put_unused_fd(fd);
return filp_close(file, files);
out_unlock:
spin_unlock(&files->file_lock);
return -EBADF;
}
static int rkvt_close_fd(struct rkvt_session *session, unsigned int fd)
{
int ret;
if (!session->task)
return -ESRCH;
ret = __rkvt_close_fd(session->task->files, fd);
if (unlikely(ret == -ERESTARTSYS ||
ret == -ERESTARTNOINTR ||
ret == -ERESTARTNOHAND ||
ret == -ERESTART_RESTARTBLOCK))
ret = -EINTR;
return ret;
}
/* The function is responsible for fifo_to_consumer fifo operation
* requires external use of rkvt_instance.lock protection
*/
static void rkvt_inst_clear_consumer(struct rkvt_instance *inst)
{
struct rkvt_buffer *buffer = NULL;
int i = 0;
if (!inst)
return;
while (kfifo_get(&inst->fifo_to_consumer, &buffer)) {
/* put file */
for (i = 0; i < buffer->base.num_fds; i++) {
if (buffer->file_buf[i]) {
fput(buffer->file_buf[i]);
buffer->file_buf[i] = NULL;
}
inst->fcount--;
}
if (buffer->ready_render_fence) {
fput(buffer->ready_render_fence);
buffer->ready_render_fence = NULL;
}
rkvt_dbg(RKVT_DBG_FILE,
"vt [%d] instance trim file(%p) buffer(%p) ino(%08lu) fcount=%d\n",
inst->id, buffer->file_buf, buffer,
buffer->file_buf[i] ?
file_inode(buffer->file_buf[i])->i_ino : 0,
inst->fcount);
if (inst->producer != NULL) {
buffer->base.buf_status = RKVT_BUF_RELEASE;
kfifo_put(&inst->fifo_to_producer, buffer);
wake_up_interruptible(&inst->wait_producer);
} else {
buffer->base.buf_status = RKVT_BUF_FREE;
}
}
}
/* The function is responsible for fifo_to_consumer fifo operation
* requires external use of rkvt_instance.lock protection.
*/
static void rkvt_inst_clear_producer(struct rkvt_instance *inst)
{
struct rkvt_buffer *buffer = NULL;
if (!inst)
return;
while (kfifo_get(&inst->fifo_to_producer, &buffer)) {
if (buffer->rendered_fence) {
dma_fence_put(buffer->rendered_fence);
buffer->rendered_fence = NULL;
}
buffer->base.buf_status = RKVT_BUF_FREE;
}
}
static void rkvt_inst_destroy(struct kref *kref)
{
struct rkvt_instance *inst =
container_of(kref, struct rkvt_instance, ref);
struct rkvt_dev *vt_dev = inst->vt_dev;
list_del_init(&inst->dev_link);
idr_remove(&vt_dev->inst_idr, inst->id);
rkvt_dbg(RKVT_DBG_USER, "vt [%d] destroy\n", inst->id);
mutex_lock(&inst->lock);
rkvt_inst_clear_consumer(inst);
rkvt_inst_clear_producer(inst);
kfifo_free(&inst->fifo_to_consumer);
kfifo_free(&inst->fifo_to_producer);
mutex_unlock(&inst->lock);
debugfs_remove_recursive(inst->debug_root);
devm_kfree(vt_dev->dev, inst);
}
static struct rkvt_instance *rkvt_inst_create(struct rkvt_dev *vt_dev)
{
struct rkvt_instance *inst;
int status;
int i;
inst = devm_kzalloc(vt_dev->dev, sizeof(*inst), GFP_KERNEL);
if (!inst)
return ERR_PTR(-ENOMEM);
inst->vt_dev = vt_dev;
mutex_init(&inst->lock);
INIT_LIST_HEAD(&inst->dev_link);
INIT_LIST_HEAD(&inst->session_link);
kref_init(&inst->ref);
status = kfifo_alloc(&inst->fifo_to_consumer,
RKVT_POOL_SIZE, GFP_KERNEL);
if (status)
goto setup_fail;
status = kfifo_alloc(&inst->fifo_to_producer,
RKVT_POOL_SIZE, GFP_KERNEL);
if (status)
goto fifo_alloc_fail;
init_waitqueue_head(&inst->wait_producer);
init_waitqueue_head(&inst->wait_consumer);
for (i = 0; i < RKVT_POOL_SIZE; i++)
inst->vt_buffers[i].base.buf_status = RKVT_BUF_FREE;
/* insert it to dev instances list */
mutex_lock(&vt_dev->inst_lock);
list_add_tail(&inst->dev_link, &vt_dev->list_inst);
mutex_unlock(&vt_dev->inst_lock);
return inst;
fifo_alloc_fail:
kfifo_free(&inst->fifo_to_consumer);
setup_fail:
devm_kfree(vt_dev->dev, inst);
return ERR_PTR(status);
}
/* The function protected by rkvt_dev.session_lock by caller */
static int
rkvt_get_session_serial(const struct list_head *sessions,
const unsigned char *name)
{
int serial = -1;
struct rkvt_session *session, *n;
list_for_each_entry_safe(session, n, sessions, dev_link) {
if (strcmp(session->name, name))
continue;
serial = max(serial, session->disp_serial);
}
return serial + 1;
}
/* The function protected by rkvt_instance.lock by caller */
static void
rkvt_session_trim_locked(struct rkvt_session *session, struct rkvt_instance *inst)
{
if (!session || !inst)
return;
if (inst->producer && inst->producer == session) {
rkvt_inst_clear_producer(inst);
inst->producer = NULL;
}
if (inst->consumer && inst->consumer == session) {
rkvt_inst_clear_consumer(inst);
inst->consumer = NULL;
}
}
static int rkvt_inst_trim(struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst, *n;
int i;
mutex_lock(&vt_dev->inst_lock);
list_for_each_entry_safe(inst, n, &vt_dev->list_inst, dev_link) {
mutex_lock(&inst->lock);
rkvt_session_trim_locked(session, inst);
if (!inst->consumer && !inst->producer) {
rkvt_inst_clear_producer(inst);
rkvt_inst_clear_consumer(inst);
for (i = 0; i < RKVT_POOL_SIZE; i++)
inst->vt_buffers[i].base.buf_status = RKVT_BUF_FREE;
}
mutex_unlock(&inst->lock);
}
mutex_unlock(&vt_dev->inst_lock);
return 0;
}
static struct rkvt_session *
rkvt_session_create(struct rkvt_dev *vt_dev, const char *name)
{
struct rkvt_session *session;
struct task_struct *task = NULL;
if (!name) {
dev_err(vt_dev->dev, "%s: Name can not be null\n", __func__);
return ERR_PTR(-EINVAL);
}
session = devm_kzalloc(vt_dev->dev, sizeof(*session), GFP_KERNEL);
if (!session)
return ERR_PTR(-ENOMEM);
get_task_struct(current->group_leader);
task_lock(current->group_leader);
session->pid = task_pid_nr(current->group_leader);
if (current->group_leader->flags & PF_KTHREAD) {
put_task_struct(current->group_leader);
task = NULL;
} else {
task = current->group_leader;
}
task_unlock(current->group_leader);
session->vt_dev = vt_dev;
session->task = task;
session->caller = RKVT_CALLER_BUTT;
INIT_LIST_HEAD(&session->dev_link);
INIT_LIST_HEAD(&session->list_inst);
snprintf(session->name, RKVT_MAX_NAME_LENGTH, "%s", name);
mutex_lock(&vt_dev->session_lock);
session->disp_serial = rkvt_get_session_serial(&vt_dev->list_session, name);
snprintf(session->disp_name, RKVT_MAX_NAME_LENGTH, "%s-%d",
name, session->disp_serial);
list_add_tail(&session->dev_link, &vt_dev->list_session);
/* add debug fs */
session->debug_root = debugfs_create_file(session->disp_name,
0664,
vt_dev->debug_root,
session,
&debug_session_fops);
mutex_unlock(&vt_dev->session_lock);
rkvt_dbg(RKVT_DBG_USER, "vt session %s create\n", session->disp_name);
return session;
}
static void rkvt_session_destroy(struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst = NULL;
rkvt_dbg(RKVT_DBG_USER, "vt session %s destroy\n", session->disp_name);
mutex_lock(&vt_dev->inst_lock);
while ((inst = list_first_entry_or_null(&session->list_inst,
struct rkvt_instance, session_link))) {
list_del_init(&inst->session_link);
kref_put(&inst->ref, rkvt_inst_destroy);
}
mutex_unlock(&vt_dev->inst_lock);
mutex_lock(&vt_dev->session_lock);
if (session->task)
put_task_struct(session->task);
list_del_init(&session->dev_link);
debugfs_remove_recursive(session->debug_root);
mutex_unlock(&vt_dev->session_lock);
rkvt_inst_trim(session);
devm_kfree(vt_dev->dev, session);
}
static int rkvt_open(struct inode *inode, struct file *filep)
{
struct miscdevice *miscdev = filep->private_data;
struct rkvt_dev *vt_dev = container_of(miscdev, struct rkvt_dev, mdev);
struct rkvt_session *session;
char debug_name[64];
snprintf(debug_name, sizeof(debug_name), "%u", task_pid_nr(current->group_leader));
session = rkvt_session_create(vt_dev, debug_name);
if (IS_ERR(session))
return PTR_ERR(session);
filep->private_data = session;
return 0;
}
static int rkvt_release(struct inode *inode, struct file *filep)
{
struct rkvt_session *session = filep->private_data;
rkvt_session_destroy(session);
filep->private_data = NULL;
return 0;
}
static int rkvt_get_connected_id(struct rkvt_dev *vt_dev)
{
return atomic64_inc_return(&vt_dev->cid_generator);
}
static struct rkvt_instance *
rkvt_inst_get_by_tid(struct rkvt_dev *vt_dev, int id)
{
struct rkvt_instance *inst;
mutex_lock(&vt_dev->inst_lock);
inst = idr_find(&vt_dev->inst_idr, id);
if (!inst) {
mutex_unlock(&vt_dev->inst_lock);
dev_err(vt_dev->dev, "find rkvt [%d] by device idr err, instance is null\n", id);
return NULL;
}
kref_get(&inst->ref);
mutex_unlock(&vt_dev->inst_lock);
return inst;
}
static void rkvt_inst_put(struct rkvt_instance *inst)
{
struct rkvt_dev *vt_dev;
if (!inst)
return;
vt_dev = inst->vt_dev;
mutex_lock(&vt_dev->inst_lock);
kref_put(&inst->ref, rkvt_inst_destroy);
mutex_unlock(&vt_dev->inst_lock);
}
static int
rkvt_connect_proc(struct rkvt_ctrl_data *data, struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst;
int ret = 0;
// ref get not put in function end, because connect need hold 1 refs.
inst = rkvt_inst_get_by_tid(vt_dev, data->vt_id);
if (!inst)
return -EINVAL;
mutex_lock(&inst->lock);
if (data->caller == RKVT_CALLER_PRODUCER) {
if (inst->producer && inst->producer != session) {
dev_err(vt_dev->dev, "Connect to rkvt [%d] err, already has producer\n",
data->vt_id);
ret = -EINVAL;
goto connect_fail;
}
inst->producer = session;
} else if (data->caller == RKVT_CALLER_CONSUMER) {
if (inst->consumer && inst->consumer != session) {
dev_err(vt_dev->dev, "Connect to rkvt [%d] err, already has consumer\n",
data->vt_id);
ret = -EINVAL;
goto connect_fail;
}
inst->consumer = session;
}
mutex_unlock(&inst->lock);
session->cid = rkvt_get_connected_id(vt_dev);
session->caller = data->caller;
rkvt_dbg(RKVT_DBG_USER, "rkvt [%d] %s-%d connect, instance ref %d\n",
inst->id,
data->caller == RKVT_CALLER_PRODUCER ? "producer" : "consumer",
session->pid,
kref_read(&inst->ref));
return 0;
connect_fail:
mutex_unlock(&inst->lock);
// ref put for rkvt_instance_get_by_tid
rkvt_inst_put(inst);
return ret;
}
static int
rkvt_disconnect_proc(struct rkvt_ctrl_data *data, struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst;
inst = rkvt_inst_get_by_tid(vt_dev, data->vt_id);
if (!inst)
return -EINVAL;
if (session->caller != data->caller)
goto session_invail;
mutex_lock(&inst->lock);
if (data->caller == RKVT_CALLER_PRODUCER) {
if (!inst->producer)
goto disconnect_fail;
if (inst->producer != session)
goto disconnect_fail;
rkvt_session_trim_locked(session, inst);
inst->producer = NULL;
wake_up_interruptible(&inst->wait_producer);
} else if (data->caller == RKVT_CALLER_CONSUMER) {
if (!inst->consumer)
goto disconnect_fail;
if (inst->consumer != session)
goto disconnect_fail;
rkvt_session_trim_locked(session, inst);
inst->consumer = NULL;
wake_up_interruptible(&inst->wait_consumer);
}
mutex_unlock(&inst->lock);
rkvt_dbg(RKVT_DBG_USER, "rkvt [%d] %s-%d disconnect, instance ref %d\n",
inst->id,
data->caller == RKVT_CALLER_PRODUCER ? "producer" : "consumer",
session->pid,
kref_read(&inst->ref));
// ref put for rkvt_instance_get_by_tid
rkvt_inst_put(inst);
// ref put for connect proc
rkvt_inst_put(inst);
session->cid = -1;
return 0;
disconnect_fail:
mutex_unlock(&inst->lock);
session_invail:
// ref put for rkvt_instance_get_by_tid
rkvt_inst_put(inst);
return -EINVAL;
}
static int
rkvt_reset_proc(struct rkvt_ctrl_data *data, struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst;
long long read_buf_id;
inst = rkvt_inst_get_by_tid(vt_dev, data->vt_id);
if (!inst)
return -EINVAL;
mutex_lock(&inst->lock);
rkvt_inst_clear_consumer(inst);
rkvt_inst_clear_producer(inst);
read_buf_id = atomic64_read(&inst->buf_id_generator);
read_buf_id += 0x100;
read_buf_id &= ~0xff;
atomic64_set(&inst->buf_id_generator, read_buf_id);
mutex_unlock(&inst->lock);
rkvt_inst_put(inst);
return 0;
}
static int
rkvt_has_consumer_proc(struct rkvt_ctrl_data *data, struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst;
inst = rkvt_inst_get_by_tid(vt_dev, data->vt_id);
if (!inst)
return -EINVAL;
mutex_lock(&inst->lock);
data->ctrl_data = inst->consumer != NULL ? 1 : 0;
mutex_unlock(&inst->lock);
rkvt_inst_put(inst);
return 0;
}
static int
rkvt_ctrl_proc(struct rkvt_ctrl_data *data, struct rkvt_session *session)
{
int id = data->vt_id;
int ret = 0;
if (id < 0)
return -EINVAL;
if (data->caller == RKVT_CALLER_BUTT)
return -EINVAL;
switch (data->ctrl_cmd) {
case RKVT_CTRL_CONNECT: {
ret = rkvt_connect_proc(data, session);
break;
}
case RKVT_CTRL_DISCONNECT: {
ret = rkvt_disconnect_proc(data, session);
break;
}
case RKVT_CTRL_RESET: {
ret = rkvt_reset_proc(data, session);
break;
}
case RKVT_CTRL_HAS_CONSUMER: {
ret = rkvt_has_consumer_proc(data, session);
break;
}
default:
pr_err("unknown rkvt cmd:%d\n", data->ctrl_cmd);
return -EINVAL;
}
return ret;
}
static struct
rkvt_buffer *rkvt_buf_get(struct rkvt_instance *inst, int key)
{
struct rkvt_buffer *buffer = NULL;
int i;
mutex_lock(&inst->lock);
for (i = 0; i < RKVT_POOL_SIZE; i++) {
buffer = &inst->vt_buffers[i];
if (buffer->base.buf_status == RKVT_BUF_ACQUIRE &&
buffer->fds_con[0] == key)
break;
}
mutex_unlock(&inst->lock);
return buffer;
}
static int
rkvt_has_buf(struct rkvt_instance *inst, enum rkvt_caller_e caller)
{
int ret = 0;
if (caller == RKVT_CALLER_PRODUCER)
ret = !kfifo_is_empty(&inst->fifo_to_producer);
else
ret = !kfifo_is_empty(&inst->fifo_to_consumer);
return ret;
}
static int
rkvt_query_buf_and_wait(struct rkvt_instance *inst,
enum rkvt_caller_e caller,
int timeout_ms)
{
int ret;
wait_queue_head_t *wait_queue;
if (caller == RKVT_CALLER_PRODUCER)
wait_queue = &inst->wait_producer;
else
wait_queue = &inst->wait_consumer;
if (caller == RKVT_CALLER_PRODUCER &&
!kfifo_is_empty(&inst->fifo_to_producer))
return 0;
if (caller == RKVT_CALLER_CONSUMER &&
!kfifo_is_empty(&inst->fifo_to_consumer))
return 0;
if (timeout_ms < 0)
wait_event_interruptible(*wait_queue,
rkvt_has_buf(inst, caller));
else if (timeout_ms > 0) {
ret = wait_event_interruptible_timeout(*wait_queue,
rkvt_has_buf(inst, caller),
msecs_to_jiffies(timeout_ms));
/* timeout */
if (ret == 0)
return -EAGAIN;
} else
return -EAGAIN;
if (caller == RKVT_CALLER_PRODUCER &&
kfifo_is_empty(&inst->fifo_to_producer))
return -EAGAIN;
if (caller == RKVT_CALLER_CONSUMER &&
kfifo_is_empty(&inst->fifo_to_consumer))
return -EAGAIN;
return 0;
}
static struct rkvt_buffer *rkvt_get_free_buf(struct rkvt_instance *inst)
{
struct rkvt_buffer *buffer = NULL;
int i, status;
mutex_lock(&inst->lock);
for (i = 0; i < RKVT_POOL_SIZE; i++) {
status = inst->vt_buffers[i].base.buf_status;
if (status == RKVT_BUF_FREE || status == RKVT_BUF_DEQUEUE) {
buffer = &inst->vt_buffers[i];
memset(buffer->file_buf, 0, sizeof(buffer->file_buf));
buffer->rendered_fence = NULL;
break;
}
}
mutex_unlock(&inst->lock);
return buffer;
}
static int
rkvt_queue_buf(struct rkvt_buf_data *data, struct rkvt_session *session)
{
struct rkvt_dev *vt_dev = session->vt_dev;
struct rkvt_instance *inst = NULL;
struct rkvt_buf_base *base = NULL;
struct rkvt_buffer *buffer = NULL;
int i;
int ret = 0;
inst = rkvt_inst_get_by_tid(vt_dev, data->vt_id);
if (!inst)
return -EINVAL;
if (!inst->producer || inst->producer != session) {
ret = -EINVAL;
goto queue_fail;
}
if ((data->base.num_fds > MAX_BUF_HANDLE_FDS) ||
(data->base.num_ints > MAX_BUF_HANDLE_INTS)) {
ret = -EINVAL;
goto queue_fail;
}
rkvt_dbg(RKVT_DBG_BUFFERS, "VTQB [%d] start\n", inst->id);
base = &data->base;
buffer = rkvt_get_free_buf(inst);
for (i = 0; i < base->num_fds; i++) {
buffer->fds_con[i] = -1;
buffer->fds_pro[i] = base->fds[i];
buffer->file_buf[i] = fget(base->fds[i]);
if (!buffer->file_buf[i]) {
ret = -EBADF;
goto buf_fget_fail;
}
inst->fcount++;
rkvt_dbg(RKVT_DBG_FILE,
"VTQB [%d] fget file(%p) buf(%p) buf session(%p) ino(%08lu) fcount=%d\n",
inst->id, buffer->file_buf[i], buffer, buffer->session_pro,
buffer->file_buf[i] ? file_inode(buffer->file_buf[i])->i_ino : 0,
inst->fcount);
}
if (base->fence_fd >= 0)
buffer->ready_render_fence = fget(base->fence_fd);
// buffer id is empty, generate a new id
if (base->buffer_id == 0)
base->buffer_id = atomic64_inc_return(&inst->buf_id_generator);
buffer->base = *base;
buffer->base.buf_status = RKVT_BUF_QUEUE;
buffer->session_pro = session;
buffer->cid_pro = session->cid;
mutex_lock(&inst->lock);
if (inst->consumer) {
kfifo_put(&inst->fifo_to_consumer, buffer);
} else {
for (i = 0; i < buffer->base.num_fds; i++) {
if (buffer->file_buf[i]) {
fput(buffer->file_buf[i]);
buffer->file_buf[i] = NULL;
}
inst->fcount--;
}
if (buffer->ready_render_fence) {
fput(buffer->ready_render_fence);
buffer->ready_render_fence = NULL;
}
buffer->base.buf_status = RKVT_BUF_RELEASE;
kfifo_put(&inst->fifo_to_producer, buffer);
}
mutex_unlock(&inst->lock);
if (inst->consumer)
wake_up_interruptible(&inst->wait_consumer);
else if (inst->producer)
wake_up_interruptible(&inst->wait_producer);
rkvt_dbg(RKVT_DBG_BUFFERS, "VTQB [%d] pfd[0]:%d end\n", inst->id, buffer->fds_pro[0]);
queue_fail:
rkvt_inst_put(inst);
return ret;
buf_fget_fail:
for (i = 0; i < base->num_fds; i++) {
if (buffer->file_buf[i]) {
fput(buffer->file_buf[i]);
buffer->file_buf[i] = NULL;
inst->fcount--;
}
}
rkvt_inst_put(inst);