hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
 *
 * author:
 *    Alpha Lin, alpha.lin@rock-chips.com
 *    Randy Li, randy.li@rock-chips.com
 *    Ding Wei, leo.ding@rock-chips.com
 *
 */
#include <asm/cacheflush.h>
#include <linux/delay.h>
#include <linux/iopoll.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/of_platform.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/regmap.h>
#include <linux/proc_fs.h>
#include <linux/nospec.h>
#include <soc/rockchip/pm_domains.h>
 
#include "mpp_debug.h"
#include "mpp_common.h"
#include "mpp_iommu.h"
#include "hack/mpp_hack_px30.h"
 
#define VEPU2_DRIVER_NAME        "mpp_vepu2"
 
#define    VEPU2_SESSION_MAX_BUFFERS        20
/* The maximum registers number of all the version */
#define VEPU2_REG_NUM                184
#define VEPU2_REG_HW_ID_INDEX        -1 /* INVALID */
#define VEPU2_REG_START_INDEX            0
#define VEPU2_REG_END_INDEX            183
#define VEPU2_REG_OUT_INDEX            (77)
#define VEPU2_REG_STRM_INDEX            (53)
 
#define VEPU2_REG_ENC_EN            0x19c
#define VEPU2_REG_ENC_EN_INDEX            (103)
#define VEPU2_ENC_START                BIT(0)
 
#define VEPU2_GET_FORMAT(x)            (((x) >> 4) & 0x3)
#define VEPU2_FORMAT_MASK            (0x30)
#define VEPU2_GET_WIDTH(x)            (((x >> 8) & 0x1ff) << 4)
#define VEPU2_GET_HEIGHT(x)            (((x >> 20) & 0x1ff) << 4)
 
#define VEPU2_FMT_RESERVED            (0)
#define VEPU2_FMT_VP8E                (1)
#define VEPU2_FMT_JPEGE                (2)
#define VEPU2_FMT_H264E                (3)
 
#define VEPU2_REG_MB_CTRL            0x1a0
#define VEPU2_REG_MB_CTRL_INDEX            (104)
 
#define VEPU2_REG_INT                0x1b4
#define VEPU2_REG_INT_INDEX            (109)
#define VEPU2_MV_SAD_WR_EN            BIT(24)
#define VEPU2_ROCON_WRITE_DIS            BIT(20)
#define VEPU2_INT_SLICE_EN            BIT(16)
#define VEPU2_CLOCK_GATE_EN            BIT(12)
#define VEPU2_INT_TIMEOUT_EN            BIT(10)
#define VEPU2_INT_CLEAR                BIT(9)
#define VEPU2_IRQ_DIS                BIT(8)
#define VEPU2_INT_TIMEOUT            BIT(6)
#define VEPU2_INT_BUF_FULL            BIT(5)
#define VEPU2_INT_BUS_ERROR            BIT(4)
#define VEPU2_INT_SLICE                BIT(2)
#define VEPU2_INT_RDY                BIT(1)
#define VEPU2_INT_RAW                BIT(0)
 
#define RKVPUE2_REG_DMV_4P_1P(i)        (0x1e0 + ((i) << 4))
#define RKVPUE2_REG_DMV_4P_1P_INDEX(i)        (120 + (i))
 
#define VEPU2_REG_CLR_CACHE_BASE        0xc10
 
#define to_vepu_task(task)        \
       container_of(task, struct vepu_task, mpp_task)
#define to_vepu_dev(dev)        \
       container_of(dev, struct vepu_dev, mpp)
 
struct vepu_task {
   struct mpp_task mpp_task;
 
   enum MPP_CLOCK_MODE clk_mode;
   u32 reg[VEPU2_REG_NUM];
 
   struct reg_offset_info off_inf;
   u32 irq_status;
   /* req for current task */
   u32 w_req_cnt;
   struct mpp_request w_reqs[MPP_MAX_MSG_NUM];
   u32 r_req_cnt;
   struct mpp_request r_reqs[MPP_MAX_MSG_NUM];
   /* image info */
   u32 width;
   u32 height;
   u32 pixels;
   struct mpp_dma_buffer *bs_buf;
   u32 offset_bs;
};
 
struct vepu_session_priv {
   struct rw_semaphore rw_sem;
   /* codec info from user */
   struct {
       /* show mode */
       u32 flag;
       /* item data */
       u64 val;
   } codec_info[ENC_INFO_BUTT];
};
 
struct vepu_dev {
   struct mpp_dev mpp;
 
   struct mpp_clk_info aclk_info;
   struct mpp_clk_info hclk_info;
   u32 default_max_load;
#ifdef CONFIG_ROCKCHIP_MPP_PROC_FS
   struct proc_dir_entry *procfs;
#endif
   struct reset_control *rst_a;
   struct reset_control *rst_h;
   /* for ccu(central control unit) */
   struct vepu_ccu *ccu;
   bool disable_work;
};
 
struct vepu_ccu {
   u32 core_num;
   /* lock for core attach */
   spinlock_t lock;
   struct mpp_dev *main_core;
   struct mpp_dev *cores[MPP_MAX_CORE_NUM];
   unsigned long core_idle;
};
 
static struct mpp_hw_info vepu_v2_hw_info = {
   .reg_num = VEPU2_REG_NUM,
   .reg_id = VEPU2_REG_HW_ID_INDEX,
   .reg_start = VEPU2_REG_START_INDEX,
   .reg_end = VEPU2_REG_END_INDEX,
   .reg_en = VEPU2_REG_ENC_EN_INDEX,
};
 
/*
 * file handle translate information
 */
static const u16 trans_tbl_default[] = {
   48, 49, 50, 56, 57, 63, 64, 77, 78, 81
};
 
static const u16 trans_tbl_vp8e[] = {
   27, 44, 45, 48, 49, 50, 56, 57, 63, 64,
   76, 77, 78, 80, 81, 106, 108,
};
 
static struct mpp_trans_info trans_rk_vepu2[] = {
   [VEPU2_FMT_RESERVED] = {
       .count = 0,
       .table = NULL,
   },
   [VEPU2_FMT_VP8E] = {
       .count = ARRAY_SIZE(trans_tbl_vp8e),
       .table = trans_tbl_vp8e,
   },
   [VEPU2_FMT_JPEGE] = {
       .count = ARRAY_SIZE(trans_tbl_default),
       .table = trans_tbl_default,
   },
   [VEPU2_FMT_H264E] = {
       .count = ARRAY_SIZE(trans_tbl_default),
       .table = trans_tbl_default,
   },
};
 
static int vepu_process_reg_fd(struct mpp_session *session,
                  struct vepu_task *task,
                  struct mpp_task_msgs *msgs)
{
   int ret;
   int fd_bs;
   int fmt = VEPU2_GET_FORMAT(task->reg[VEPU2_REG_ENC_EN_INDEX]);
 
   if (session->msg_flags & MPP_FLAGS_REG_NO_OFFSET)
       fd_bs = task->reg[VEPU2_REG_OUT_INDEX];
   else
       fd_bs = task->reg[VEPU2_REG_OUT_INDEX] & 0x3ff;
 
   ret = mpp_translate_reg_address(session, &task->mpp_task,
                   fmt, task->reg, &task->off_inf);
   if (ret)
       return ret;
 
   mpp_translate_reg_offset_info(&task->mpp_task,
                     &task->off_inf, task->reg);
 
   if (fmt == VEPU2_FMT_JPEGE) {
       struct mpp_dma_buffer *bs_buf = mpp_dma_find_buffer_fd(session->dma, fd_bs);
 
       task->offset_bs = mpp_query_reg_offset_info(&task->off_inf, VEPU2_REG_OUT_INDEX);
       if (bs_buf && task->offset_bs > 0)
           mpp_dma_buf_sync(bs_buf, 0, task->offset_bs, DMA_TO_DEVICE, false);
       task->bs_buf = bs_buf;
   }
 
   return 0;
}
 
static int vepu_extract_task_msg(struct vepu_task *task,
                struct mpp_task_msgs *msgs)
{
   u32 i;
   int ret;
   struct mpp_request *req;
   struct mpp_hw_info *hw_info = task->mpp_task.hw_info;
 
   for (i = 0; i < msgs->req_cnt; i++) {
       u32 off_s, off_e;
 
       req = &msgs->reqs[i];
       if (!req->size)
           continue;
 
       switch (req->cmd) {
       case MPP_CMD_SET_REG_WRITE: {
           off_s = hw_info->reg_start * sizeof(u32);
           off_e = hw_info->reg_end * sizeof(u32);
           ret = mpp_check_req(req, 0, sizeof(task->reg),
                       off_s, off_e);
           if (ret)
               continue;
           if (copy_from_user((u8 *)task->reg + req->offset,
                      req->data, req->size)) {
               mpp_err("copy_from_user reg failed\n");
               return -EIO;
           }
           memcpy(&task->w_reqs[task->w_req_cnt++],
                  req, sizeof(*req));
       } break;
       case MPP_CMD_SET_REG_READ: {
           off_s = hw_info->reg_start * sizeof(u32);
           off_e = hw_info->reg_end * sizeof(u32);
           ret = mpp_check_req(req, 0, sizeof(task->reg),
                       off_s, off_e);
           if (ret)
               continue;
           memcpy(&task->r_reqs[task->r_req_cnt++],
                  req, sizeof(*req));
       } break;
       case MPP_CMD_SET_REG_ADDR_OFFSET: {
           mpp_extract_reg_offset_info(&task->off_inf, req);
       } break;
       default:
           break;
       }
   }
   mpp_debug(DEBUG_TASK_INFO, "w_req_cnt %d, r_req_cnt %d\n",
         task->w_req_cnt, task->r_req_cnt);
 
   return 0;
}
 
static void *vepu_alloc_task(struct mpp_session *session,
                struct mpp_task_msgs *msgs)
{
   int ret;
   struct mpp_task *mpp_task = NULL;
   struct vepu_task *task = NULL;
   struct mpp_dev *mpp = session->mpp;
 
   mpp_debug_enter();
 
   task = kzalloc(sizeof(*task), GFP_KERNEL);
   if (!task)
       return NULL;
 
   mpp_task = &task->mpp_task;
   mpp_task_init(session, mpp_task);
   mpp_task->hw_info = mpp->var->hw_info;
   mpp_task->reg = task->reg;
   /* extract reqs for current task */
   ret = vepu_extract_task_msg(task, msgs);
   if (ret)
       goto fail;
   /* process fd in register */
   if (!(msgs->flags & MPP_FLAGS_REG_FD_NO_TRANS)) {
       ret = vepu_process_reg_fd(session, task, msgs);
       if (ret)
           goto fail;
   }
   task->clk_mode = CLK_MODE_NORMAL;
   /* get resolution info */
   task->width = VEPU2_GET_WIDTH(task->reg[VEPU2_REG_ENC_EN_INDEX]);
   task->height = VEPU2_GET_HEIGHT(task->reg[VEPU2_REG_ENC_EN_INDEX]);
   task->pixels = task->width * task->height;
   mpp_debug(DEBUG_TASK_INFO, "width=%d, height=%d\n", task->width, task->height);
 
   mpp_debug_leave();
 
   return mpp_task;
 
fail:
   mpp_task_dump_mem_region(mpp, mpp_task);
   mpp_task_dump_reg(mpp, mpp_task);
   mpp_task_finalize(session, mpp_task);
   kfree(task);
   return NULL;
}
 
static void *vepu_prepare(struct mpp_dev *mpp, struct mpp_task *mpp_task)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
   struct vepu_ccu *ccu = enc->ccu;
   unsigned long core_idle;
   unsigned long flags;
   s32 core_id;
   u32 i;
 
   spin_lock_irqsave(&ccu->lock, flags);
 
   core_idle = ccu->core_idle;
 
   for (i = 0; i < ccu->core_num; i++) {
       struct mpp_dev *mpp = ccu->cores[i];
 
       if (mpp && mpp->disable)
           clear_bit(mpp->core_id, &core_idle);
   }
 
   core_id = find_first_bit(&core_idle, ccu->core_num);
   if (core_id >= ARRAY_SIZE(ccu->cores)) {
       mpp_task = NULL;
       mpp_dbg_core("core %d all busy %lx\n", core_id, ccu->core_idle);
       goto done;
   }
 
   core_id = array_index_nospec(core_id, MPP_MAX_CORE_NUM);
   clear_bit(core_id, &ccu->core_idle);
   mpp_task->mpp = ccu->cores[core_id];
   mpp_task->core_id = core_id;
 
   mpp_dbg_core("core cnt %d core %d set idle %lx -> %lx\n",
            ccu->core_num, core_id, core_idle, ccu->core_idle);
 
done:
   spin_unlock_irqrestore(&ccu->lock, flags);
 
   return mpp_task;
}
 
static int vepu_run(struct mpp_dev *mpp,
           struct mpp_task *mpp_task)
{
   u32 i;
   u32 reg_en;
   struct vepu_task *task = to_vepu_task(mpp_task);
   u32 timing_en = mpp->srv->timing_en;
 
   mpp_debug_enter();
 
   /* clear cache */
   mpp_write_relaxed(mpp, VEPU2_REG_CLR_CACHE_BASE, 1);
 
   reg_en = mpp_task->hw_info->reg_en;
   /* First, flush correct encoder format */
   mpp_write_relaxed(mpp, VEPU2_REG_ENC_EN,
             task->reg[reg_en] & VEPU2_FORMAT_MASK);
   /* Second, flush others register */
   for (i = 0; i < task->w_req_cnt; i++) {
       struct mpp_request *req = &task->w_reqs[i];
       int s = req->offset / sizeof(u32);
       int e = s + req->size / sizeof(u32);
 
       mpp_write_req(mpp, task->reg, s, e, reg_en);
   }
 
   /* flush tlb before starting hardware */
   mpp_iommu_flush_tlb(mpp->iommu_info);
 
   /* init current task */
   mpp->cur_task = mpp_task;
 
   mpp_task_run_begin(mpp_task, timing_en, MPP_WORK_TIMEOUT_DELAY);
 
   /* Last, flush the registers */
   wmb();
   mpp_write(mpp, VEPU2_REG_ENC_EN,
         task->reg[reg_en] | VEPU2_ENC_START);
 
   mpp_task_run_end(mpp_task, timing_en);
 
   mpp_debug_leave();
 
   return 0;
}
 
static int vepu_px30_run(struct mpp_dev *mpp,
           struct mpp_task *mpp_task)
{
   mpp_iommu_flush_tlb(mpp->iommu_info);
   return vepu_run(mpp, mpp_task);
}
 
static int vepu_irq(struct mpp_dev *mpp)
{
   mpp->irq_status = mpp_read(mpp, VEPU2_REG_INT);
   if (!(mpp->irq_status & VEPU2_INT_RAW))
       return IRQ_NONE;
 
   mpp_write(mpp, VEPU2_REG_INT, 0);
 
   return IRQ_WAKE_THREAD;
}
 
static int vepu_isr(struct mpp_dev *mpp)
{
   u32 err_mask;
   struct vepu_task *task = NULL;
   struct mpp_task *mpp_task = mpp->cur_task;
   unsigned long core_idle;
   struct vepu_dev *enc = to_vepu_dev(mpp);
   struct vepu_ccu *ccu = enc->ccu;
 
   /* FIXME use a spin lock here */
   if (!mpp_task) {
       dev_err(mpp->dev, "no current task\n");
       return IRQ_HANDLED;
   }
   mpp_time_diff(mpp_task);
   mpp->cur_task = NULL;
   task = to_vepu_task(mpp_task);
   task->irq_status = mpp->irq_status;
   mpp_debug(DEBUG_IRQ_STATUS, "irq_status: %08x\n",
         task->irq_status);
 
   err_mask = VEPU2_INT_TIMEOUT
       | VEPU2_INT_BUF_FULL
       | VEPU2_INT_BUS_ERROR;
 
   if (err_mask & task->irq_status)
       atomic_inc(&mpp->reset_request);
 
   mpp_task_finish(mpp_task->session, mpp_task);
   /* the whole vepu has no ccu that manage multi core */
   if (ccu) {
       core_idle = ccu->core_idle;
       set_bit(mpp->core_id, &ccu->core_idle);
 
       mpp_dbg_core("core %d isr idle %lx -> %lx\n", mpp->core_id, core_idle,
           ccu->core_idle);
   }
 
   mpp_debug_leave();
 
   return IRQ_HANDLED;
}
 
static int vepu_finish(struct mpp_dev *mpp,
              struct mpp_task *mpp_task)
{
   u32 i;
   u32 s, e;
   struct mpp_request *req;
   struct vepu_task *task = to_vepu_task(mpp_task);
 
   mpp_debug_enter();
 
   /* read register after running */
   for (i = 0; i < task->r_req_cnt; i++) {
       req = &task->r_reqs[i];
       s = req->offset / sizeof(u32);
       e = s + req->size / sizeof(u32);
       mpp_read_req(mpp, task->reg, s, e);
   }
   /* revert hack for irq status */
   task->reg[VEPU2_REG_INT_INDEX] = task->irq_status;
 
   if (task->bs_buf)
       mpp_dma_buf_sync(task->bs_buf, 0,
                task->reg[VEPU2_REG_STRM_INDEX] / 8 +
                task->offset_bs,
                DMA_FROM_DEVICE, true);
   mpp_debug_leave();
 
   return 0;
}
 
static int vepu_result(struct mpp_dev *mpp,
              struct mpp_task *mpp_task,
              struct mpp_task_msgs *msgs)
{
   u32 i;
   struct mpp_request *req;
   struct vepu_task *task = to_vepu_task(mpp_task);
 
   /* FIXME may overflow the kernel */
   for (i = 0; i < task->r_req_cnt; i++) {
       req = &task->r_reqs[i];
 
       if (copy_to_user(req->data,
                (u8 *)task->reg + req->offset,
                req->size)) {
           mpp_err("copy_to_user reg fail\n");
           return -EIO;
       }
   }
 
   return 0;
}
 
static int vepu_free_task(struct mpp_session *session,
             struct mpp_task *mpp_task)
{
   struct vepu_task *task = to_vepu_task(mpp_task);
 
   mpp_task_finalize(session, mpp_task);
   kfree(task);
 
   return 0;
}
 
static int vepu_control(struct mpp_session *session, struct mpp_request *req)
{
   switch (req->cmd) {
   case MPP_CMD_SEND_CODEC_INFO: {
       int i;
       int cnt;
       struct codec_info_elem elem;
       struct vepu_session_priv *priv;
 
       if (!session || !session->priv) {
           mpp_err("session info null\n");
           return -EINVAL;
       }
       priv = session->priv;
 
       cnt = req->size / sizeof(elem);
       cnt = (cnt > ENC_INFO_BUTT) ? ENC_INFO_BUTT : cnt;
       mpp_debug(DEBUG_IOCTL, "codec info count %d\n", cnt);
       for (i = 0; i < cnt; i++) {
           if (copy_from_user(&elem, req->data + i * sizeof(elem), sizeof(elem))) {
               mpp_err("copy_from_user failed\n");
               continue;
           }
           if (elem.type > ENC_INFO_BASE && elem.type < ENC_INFO_BUTT &&
               elem.flag > CODEC_INFO_FLAG_NULL && elem.flag < CODEC_INFO_FLAG_BUTT) {
               elem.type = array_index_nospec(elem.type, ENC_INFO_BUTT);
               priv->codec_info[elem.type].flag = elem.flag;
               priv->codec_info[elem.type].val = elem.data;
           } else {
               mpp_err("codec info invalid, type %d, flag %d\n",
                   elem.type, elem.flag);
           }
       }
   } break;
   default: {
       mpp_err("unknown mpp ioctl cmd %x\n", req->cmd);
   } break;
   }
 
   return 0;
}
 
static int vepu_free_session(struct mpp_session *session)
{
   if (session && session->priv) {
       kfree(session->priv);
       session->priv = NULL;
   }
 
   return 0;
}
 
static int vepu_init_session(struct mpp_session *session)
{
   struct vepu_session_priv *priv;
 
   if (!session) {
       mpp_err("session is null\n");
       return -EINVAL;
   }
 
   priv = kzalloc(sizeof(*priv), GFP_KERNEL);
   if (!priv)
       return -ENOMEM;
 
   init_rwsem(&priv->rw_sem);
   session->priv = priv;
 
   return 0;
}
 
#ifdef CONFIG_ROCKCHIP_MPP_PROC_FS
static int vepu_procfs_remove(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
 
   if (enc->procfs) {
       proc_remove(enc->procfs);
       enc->procfs = NULL;
   }
 
   return 0;
}
 
static int vepu_dump_session(struct mpp_session *session, struct seq_file *seq)
{
   int i;
   struct vepu_session_priv *priv = session->priv;
 
   down_read(&priv->rw_sem);
   /* item name */
   seq_puts(seq, "------------------------------------------------------");
   seq_puts(seq, "------------------------------------------------------\n");
   seq_printf(seq, "|%8s|", (const char *)"session");
   seq_printf(seq, "%8s|", (const char *)"device");
   for (i = ENC_INFO_BASE; i < ENC_INFO_BUTT; i++) {
       bool show = priv->codec_info[i].flag;
 
       if (show)
           seq_printf(seq, "%8s|", enc_info_item_name[i]);
   }
   seq_puts(seq, "\n");
   /* item data*/
   seq_printf(seq, "|%8d|", session->index);
   seq_printf(seq, "%8s|", mpp_device_name[session->device_type]);
   for (i = ENC_INFO_BASE; i < ENC_INFO_BUTT; i++) {
       u32 flag = priv->codec_info[i].flag;
 
       if (!flag)
           continue;
       if (flag == CODEC_INFO_FLAG_NUMBER) {
           u32 data = priv->codec_info[i].val;
 
           seq_printf(seq, "%8d|", data);
       } else if (flag == CODEC_INFO_FLAG_STRING) {
           const char *name = (const char *)&priv->codec_info[i].val;
 
           seq_printf(seq, "%8s|", name);
       } else {
           seq_printf(seq, "%8s|", (const char *)"null");
       }
   }
   seq_puts(seq, "\n");
   up_read(&priv->rw_sem);
 
   return 0;
}
 
static int vepu_show_session_info(struct seq_file *seq, void *offset)
{
   struct mpp_session *session = NULL, *n;
   struct mpp_dev *mpp = seq->private;
 
   mutex_lock(&mpp->srv->session_lock);
   list_for_each_entry_safe(session, n,
                &mpp->srv->session_list,
                service_link) {
       if (session->device_type != MPP_DEVICE_VEPU2 &&
           session->device_type != MPP_DEVICE_VEPU2_JPEG)
           continue;
       if (!session->priv)
           continue;
       if (mpp->dev_ops->dump_session)
           mpp->dev_ops->dump_session(session, seq);
   }
   mutex_unlock(&mpp->srv->session_lock);
 
   return 0;
}
 
static int vepu_procfs_init(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
   char name[32];
 
   if (!mpp->dev || !mpp->dev->of_node || !mpp->dev->of_node->name ||
       !mpp->srv || !mpp->srv->procfs)
       return -EINVAL;
   if (enc->ccu)
       snprintf(name, sizeof(name) - 1, "%s%d",
           mpp->dev->of_node->name, mpp->core_id);
   else
       snprintf(name, sizeof(name) - 1, "%s",
           mpp->dev->of_node->name);
 
   enc->procfs = proc_mkdir(name, mpp->srv->procfs);
   if (IS_ERR_OR_NULL(enc->procfs)) {
       mpp_err("failed on open procfs\n");
       enc->procfs = NULL;
       return -EIO;
   }
 
   /* for common mpp_dev options */
   mpp_procfs_create_common(enc->procfs, mpp);
 
   mpp_procfs_create_u32("aclk", 0644,
                 enc->procfs, &enc->aclk_info.debug_rate_hz);
   mpp_procfs_create_u32("session_buffers", 0644,
                 enc->procfs, &mpp->session_max_buffers);
   /* for show session info */
   proc_create_single_data("sessions-info", 0444,
               enc->procfs, vepu_show_session_info, mpp);
 
   return 0;
}
 
static int vepu_procfs_ccu_init(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
 
   if (!enc->procfs)
       goto done;
 
done:
   return 0;
}
#else
static inline int vepu_procfs_remove(struct mpp_dev *mpp)
{
   return 0;
}
 
static inline int vepu_procfs_init(struct mpp_dev *mpp)
{
   return 0;
}
 
static inline int vepu_procfs_ccu_init(struct mpp_dev *mpp)
{
   return 0;
}
 
static inline int vepu_dump_session(struct mpp_session *session, struct seq_file *seq)
{
   return 0;
}
#endif
 
static int vepu_init(struct mpp_dev *mpp)
{
   int ret;
   struct vepu_dev *enc = to_vepu_dev(mpp);
 
   mpp->grf_info = &mpp->srv->grf_infos[MPP_DRIVER_VEPU2];
 
   /* Get clock info from dtsi */
   ret = mpp_get_clk_info(mpp, &enc->aclk_info, "aclk_vcodec");
   if (ret)
       mpp_err("failed on clk_get aclk_vcodec\n");
   ret = mpp_get_clk_info(mpp, &enc->hclk_info, "hclk_vcodec");
   if (ret)
       mpp_err("failed on clk_get hclk_vcodec\n");
   /* Get normal max workload from dtsi */
   of_property_read_u32(mpp->dev->of_node,
                "rockchip,default-max-load", &enc->default_max_load);
   /* Set default rates */
   mpp_set_clk_info_rate_hz(&enc->aclk_info, CLK_MODE_DEFAULT, 300 * MHZ);
 
   /* Get reset control from dtsi */
   enc->rst_a = mpp_reset_control_get(mpp, RST_TYPE_A, "video_a");
   if (!enc->rst_a)
       mpp_err("No aclk reset resource define\n");
   enc->rst_h = mpp_reset_control_get(mpp, RST_TYPE_H, "video_h");
   if (!enc->rst_h)
       mpp_err("No hclk reset resource define\n");
 
   return 0;
}
 
static int vepu_px30_init(struct mpp_dev *mpp)
{
   vepu_init(mpp);
   return px30_workaround_combo_init(mpp);
}
 
static int vepu_clk_on(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
 
   mpp_clk_safe_enable(enc->aclk_info.clk);
   mpp_clk_safe_enable(enc->hclk_info.clk);
 
   return 0;
}
 
static int vepu_clk_off(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
 
   mpp_clk_safe_disable(enc->aclk_info.clk);
   mpp_clk_safe_disable(enc->hclk_info.clk);
 
   return 0;
}
 
static int vepu_get_freq(struct mpp_dev *mpp,
            struct mpp_task *mpp_task)
{
   u32 task_cnt;
   u32 workload;
   struct mpp_task *loop = NULL, *n;
   struct vepu_dev *enc = to_vepu_dev(mpp);
   struct vepu_task *task = to_vepu_task(mpp_task);
 
   /* if not set max load, consider not have advanced mode */
   if (!enc->default_max_load)
       return 0;
 
   task_cnt = 1;
   workload = task->pixels;
   /* calc workload in pending list */
   mutex_lock(&mpp->queue->pending_lock);
   list_for_each_entry_safe(loop, n,
                &mpp->queue->pending_list,
                queue_link) {
       struct vepu_task *loop_task = to_vepu_task(loop);
 
       task_cnt++;
       workload += loop_task->pixels;
   }
   mutex_unlock(&mpp->queue->pending_lock);
 
   if (workload > enc->default_max_load)
       task->clk_mode = CLK_MODE_ADVANCED;
 
   mpp_debug(DEBUG_TASK_INFO, "pending task %d, workload %d, clk_mode=%d\n",
         task_cnt, workload, task->clk_mode);
 
   return 0;
}
 
static int vepu_set_freq(struct mpp_dev *mpp,
            struct mpp_task *mpp_task)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
   struct vepu_task *task = to_vepu_task(mpp_task);
 
   mpp_clk_set_rate(&enc->aclk_info, task->clk_mode);
 
   return 0;
}
 
static int vepu_reduce_freq(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
 
   mpp_clk_set_rate(&enc->aclk_info, CLK_MODE_REDUCE);
 
   return 0;
}
 
static int vepu_reset(struct mpp_dev *mpp)
{
   struct vepu_dev *enc = to_vepu_dev(mpp);
   struct vepu_ccu *ccu = enc->ccu;
 
   mpp_write(mpp, VEPU2_REG_ENC_EN, 0);
   udelay(5);
   if (enc->rst_a && enc->rst_h) {
       /* Don't skip this or iommu won't work after reset */
       mpp_pmu_idle_request(mpp, true);
       mpp_safe_reset(enc->rst_a);
       mpp_safe_reset(enc->rst_h);
       udelay(5);
       mpp_safe_unreset(enc->rst_a);
       mpp_safe_unreset(enc->rst_h);
       mpp_pmu_idle_request(mpp, false);
   }
   mpp_write(mpp, VEPU2_REG_INT, VEPU2_INT_CLEAR);
 
   if (ccu) {
       set_bit(mpp->core_id, &ccu->core_idle);
       mpp_dbg_core("core %d reset idle %lx\n", mpp->core_id, ccu->core_idle);
   }
 
   return 0;
}
 
static struct mpp_hw_ops vepu_v2_hw_ops = {
   .init = vepu_init,
   .clk_on = vepu_clk_on,
   .clk_off = vepu_clk_off,
   .get_freq = vepu_get_freq,
   .set_freq = vepu_set_freq,
   .reduce_freq = vepu_reduce_freq,
   .reset = vepu_reset,
};
 
static struct mpp_hw_ops vepu_px30_hw_ops = {
   .init = vepu_px30_init,
   .clk_on = vepu_clk_on,
   .clk_off = vepu_clk_off,
   .set_freq = vepu_set_freq,
   .reduce_freq = vepu_reduce_freq,
   .reset = vepu_reset,
   .set_grf = px30_workaround_combo_switch_grf,
};
 
static struct mpp_dev_ops vepu_v2_dev_ops = {
   .alloc_task = vepu_alloc_task,
   .run = vepu_run,
   .irq = vepu_irq,
   .isr = vepu_isr,
   .finish = vepu_finish,
   .result = vepu_result,
   .free_task = vepu_free_task,
   .ioctl = vepu_control,
   .init_session = vepu_init_session,
   .free_session = vepu_free_session,
   .dump_session = vepu_dump_session,
};
 
static struct mpp_dev_ops vepu_px30_dev_ops = {
   .alloc_task = vepu_alloc_task,
   .run = vepu_px30_run,
   .irq = vepu_irq,
   .isr = vepu_isr,
   .finish = vepu_finish,
   .result = vepu_result,
   .free_task = vepu_free_task,
   .ioctl = vepu_control,
   .init_session = vepu_init_session,
   .free_session = vepu_free_session,
   .dump_session = vepu_dump_session,
};
 
static struct mpp_dev_ops vepu_ccu_dev_ops = {
   .alloc_task = vepu_alloc_task,
   .prepare = vepu_prepare,
   .run = vepu_run,
   .irq = vepu_irq,
   .isr = vepu_isr,
   .finish = vepu_finish,
   .result = vepu_result,
   .free_task = vepu_free_task,
   .ioctl = vepu_control,
   .init_session = vepu_init_session,
   .free_session = vepu_free_session,
   .dump_session = vepu_dump_session,
};
 
 
static const struct mpp_dev_var vepu_v2_data = {
   .device_type = MPP_DEVICE_VEPU2,
   .hw_info = &vepu_v2_hw_info,
   .trans_info = trans_rk_vepu2,
   .hw_ops = &vepu_v2_hw_ops,
   .dev_ops = &vepu_v2_dev_ops,
};
 
static const struct mpp_dev_var vepu_px30_data = {
   .device_type = MPP_DEVICE_VEPU2,
   .hw_info = &vepu_v2_hw_info,
   .trans_info = trans_rk_vepu2,
   .hw_ops = &vepu_px30_hw_ops,
   .dev_ops = &vepu_px30_dev_ops,
};
 
static const struct mpp_dev_var vepu_ccu_data = {
   .device_type = MPP_DEVICE_VEPU2_JPEG,
   .hw_info = &vepu_v2_hw_info,
   .trans_info = trans_rk_vepu2,
   .hw_ops = &vepu_v2_hw_ops,
   .dev_ops = &vepu_ccu_dev_ops,
};
 
static const struct of_device_id mpp_vepu2_dt_match[] = {
   {
       .compatible = "rockchip,vpu-encoder-v2",
       .data = &vepu_v2_data,
   },
#ifdef CONFIG_CPU_PX30
   {
       .compatible = "rockchip,vpu-encoder-px30",
       .data = &vepu_px30_data,
   },
#endif
#ifdef CONFIG_CPU_RK3588
   {
       .compatible = "rockchip,vpu-jpege-core",
       .data = &vepu_ccu_data,
   },
   {
       .compatible = "rockchip,vpu-jpege-ccu",
   },
#endif
   {},
};
 
static int vepu_ccu_probe(struct platform_device *pdev)
{
   struct vepu_ccu *ccu;
   struct device *dev = &pdev->dev;
 
   ccu = devm_kzalloc(dev, sizeof(*ccu), GFP_KERNEL);
   if (!ccu)
       return -ENOMEM;
 
   platform_set_drvdata(pdev, ccu);
   spin_lock_init(&ccu->lock);
   return 0;
}
 
static int vepu_attach_ccu(struct device *dev, struct vepu_dev *enc)
{
   struct device_node *np;
   struct platform_device *pdev;
   struct vepu_ccu *ccu;
   unsigned long flags;
 
   np = of_parse_phandle(dev->of_node, "rockchip,ccu", 0);
   if (!np || !of_device_is_available(np))
       return -ENODEV;
 
   pdev = of_find_device_by_node(np);
   of_node_put(np);
   if (!pdev)
       return -ENODEV;
 
   ccu = platform_get_drvdata(pdev);
   if (!ccu)
       return -ENOMEM;
 
   spin_lock_irqsave(&ccu->lock, flags);
   ccu->core_num++;
   ccu->cores[enc->mpp.core_id] = &enc->mpp;
   set_bit(enc->mpp.core_id, &ccu->core_idle);
   spin_unlock_irqrestore(&ccu->lock, flags);
 
   /* attach the ccu-domain to current core */
   if (!ccu->main_core) {
       /**
        * set the first device for the main-core,
        * then the domain of the main-core named ccu-domain
        */
       ccu->main_core = &enc->mpp;
   } else {
       struct mpp_iommu_info *ccu_info, *cur_info;
 
       /* set the ccu domain for current device */
       ccu_info = ccu->main_core->iommu_info;
       cur_info = enc->mpp.iommu_info;
 
       if (cur_info)
           cur_info->domain = ccu_info->domain;
       mpp_iommu_attach(cur_info);
   }
   enc->ccu = ccu;
 
   dev_info(dev, "attach ccu success\n");
   return 0;
}
 
static int vepu_core_probe(struct platform_device *pdev)
{
   struct device *dev = &pdev->dev;
   struct vepu_dev *enc = NULL;
   struct mpp_dev *mpp = NULL;
   const struct of_device_id *match = NULL;
   int ret = 0;
 
   enc = devm_kzalloc(dev, sizeof(struct vepu_dev), GFP_KERNEL);
   if (!enc)
       return -ENOMEM;
 
   mpp = &enc->mpp;
   platform_set_drvdata(pdev, mpp);
 
   if (pdev->dev.of_node) {
       match = of_match_node(mpp_vepu2_dt_match, pdev->dev.of_node);
       if (match)
           mpp->var = (struct mpp_dev_var *)match->data;
 
       mpp->core_id = of_alias_get_id(pdev->dev.of_node, "jpege");
   }
 
   ret = mpp_dev_probe(mpp, pdev);
   if (ret) {
       dev_err(dev, "probe sub driver failed\n");
       return -EINVAL;
   }
   /* current device attach to ccu */
   ret = vepu_attach_ccu(dev, enc);
   if (ret)
       return ret;
 
   ret = devm_request_threaded_irq(dev, mpp->irq,
                   mpp_dev_irq,
                   mpp_dev_isr_sched,
                   IRQF_SHARED,
                   dev_name(dev), mpp);
   if (ret) {
       dev_err(dev, "register interrupter runtime failed\n");
       return -EINVAL;
   }
 
   mpp->session_max_buffers = VEPU2_SESSION_MAX_BUFFERS;
   vepu_procfs_init(mpp);
   vepu_procfs_ccu_init(mpp);
   /* if current is main-core, register current device to mpp service */
   if (mpp == enc->ccu->main_core)
       mpp_dev_register_srv(mpp, mpp->srv);
 
   return 0;
}
 
static int vepu_probe_default(struct platform_device *pdev)
{
   struct device *dev = &pdev->dev;
   struct vepu_dev *enc = NULL;
   struct mpp_dev *mpp = NULL;
   const struct of_device_id *match = NULL;
   int ret = 0;
 
   enc = devm_kzalloc(dev, sizeof(struct vepu_dev), GFP_KERNEL);
   if (!enc)
       return -ENOMEM;
 
   mpp = &enc->mpp;
   platform_set_drvdata(pdev, mpp);
 
   if (pdev->dev.of_node) {
       match = of_match_node(mpp_vepu2_dt_match, pdev->dev.of_node);
       if (match)
           mpp->var = (struct mpp_dev_var *)match->data;
 
       mpp->core_id = of_alias_get_id(pdev->dev.of_node, "vepu");
   }
 
   ret = mpp_dev_probe(mpp, pdev);
   if (ret) {
       dev_err(dev, "probe sub driver failed\n");
       return -EINVAL;
   }
 
   ret = devm_request_threaded_irq(dev, mpp->irq,
                   mpp_dev_irq,
                   mpp_dev_isr_sched,
                   IRQF_SHARED,
                   dev_name(dev), mpp);
   if (ret) {
       dev_err(dev, "register interrupter runtime failed\n");
       return -EINVAL;
   }
 
   mpp->session_max_buffers = VEPU2_SESSION_MAX_BUFFERS;
   vepu_procfs_init(mpp);
   /* register current device to mpp service */
   mpp_dev_register_srv(mpp, mpp->srv);
 
   return 0;
}
 
static int vepu_probe(struct platform_device *pdev)
{
   int ret;
   struct device *dev = &pdev->dev;
   struct device_node *np = dev->of_node;
 
   dev_info(dev, "probing start\n");
 
   if (strstr(np->name, "ccu"))
       ret = vepu_ccu_probe(pdev);
   else if (strstr(np->name, "core"))
       ret = vepu_core_probe(pdev);
   else
       ret = vepu_probe_default(pdev);
 
   dev_info(dev, "probing finish\n");
 
   return ret;
}
 
static int vepu_remove(struct platform_device *pdev)
{
   struct device *dev = &pdev->dev;
   struct device_node *np = dev->of_node;
 
   if (strstr(np->name, "ccu")) {
       dev_info(dev, "remove ccu device\n");
   } else if (strstr(np->name, "core")) {
       struct mpp_dev *mpp = dev_get_drvdata(dev);
       struct vepu_dev *enc = to_vepu_dev(mpp);
 
       dev_info(dev, "remove core\n");
       if (enc->ccu) {
           s32 core_id = mpp->core_id;
           struct vepu_ccu *ccu = enc->ccu;
           unsigned long flags;
 
           spin_lock_irqsave(&ccu->lock, flags);
           ccu->core_num--;
           ccu->cores[core_id] = NULL;
           clear_bit(core_id, &ccu->core_idle);
           spin_unlock_irqrestore(&ccu->lock, flags);
       }
       mpp_dev_remove(&enc->mpp);
       vepu_procfs_remove(&enc->mpp);
   } else {
       struct mpp_dev *mpp = dev_get_drvdata(dev);
 
       dev_info(dev, "remove device\n");
       mpp_dev_remove(mpp);
       vepu_procfs_remove(mpp);
   }
 
   return 0;
}
 
static void vepu_shutdown(struct platform_device *pdev)
{
   struct device *dev = &pdev->dev;
 
   if (!strstr(dev_name(dev), "ccu"))
       mpp_dev_shutdown(pdev);
}
 
struct platform_driver rockchip_vepu2_driver = {
   .probe = vepu_probe,
   .remove = vepu_remove,
   .shutdown = vepu_shutdown,
   .driver = {
       .name = VEPU2_DRIVER_NAME,
       .of_match_table = of_match_ptr(mpp_vepu2_dt_match),
   },
};
EXPORT_SYMBOL(rockchip_vepu2_driver);