1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
5 * Kai-Sean Yang <kai-sean.yang@mediatek.com>
6 * Tiffany Lin <tiffany.lin@mediatek.com>
7 */
8
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/syscalls.h>
12 #include <linux/delay.h>
13 #include <linux/time.h>
14
15 #include "../mtk_vcodec_intr.h"
16 #include "../vdec_drv_base.h"
17 #include "../vdec_vpu_if.h"
18
19 #define VP9_SUPER_FRAME_BS_SZ 64
20 #define MAX_VP9_DPB_SIZE 9
21
22 #define REFS_PER_FRAME 3
23 #define MAX_NUM_REF_FRAMES 8
24 #define VP9_MAX_FRM_BUF_NUM 9
25 #define VP9_MAX_FRM_BUF_NODE_NUM (VP9_MAX_FRM_BUF_NUM * 2)
26 #define VP9_SEG_ID_SZ 0x12000
27
28 /**
29 * struct vp9_dram_buf - contains buffer info for vpu
30 * @va : cpu address
31 * @pa : iova address
32 * @sz : buffer size
33 * @padding : for 64 bytes alignment
34 */
35 struct vp9_dram_buf {
36 unsigned long va;
37 unsigned long pa;
38 unsigned int sz;
39 unsigned int padding;
40 };
41
42 /**
43 * struct vp9_fb_info - contains frame buffer info
44 * @fb : frmae buffer
45 * @reserved : reserved field used by vpu
46 */
47 struct vp9_fb_info {
48 struct vdec_fb *fb;
49 unsigned int reserved[32];
50 };
51
52 /**
53 * struct vp9_ref_cnt_buf - contains reference buffer information
54 * @buf : referenced frame buffer
55 * @ref_cnt : referenced frame buffer's reference count.
56 * When reference count=0, remove it from reference list
57 */
58 struct vp9_ref_cnt_buf {
59 struct vp9_fb_info buf;
60 unsigned int ref_cnt;
61 };
62
63 /**
64 * struct vp9_fb_info - contains current frame's reference buffer information
65 * @buf : reference buffer
66 * @idx : reference buffer index to frm_bufs
67 * @reserved : reserved field used by vpu
68 */
69 struct vp9_ref_buf {
70 struct vp9_fb_info *buf;
71 unsigned int idx;
72 unsigned int reserved[6];
73 };
74
75 /**
76 * struct vp9_fb_info - contains frame buffer info
77 * @fb : super frame reference frame buffer
78 * @used : this reference frame info entry is used
79 * @padding : for 64 bytes size align
80 */
81 struct vp9_sf_ref_fb {
82 struct vdec_fb fb;
83 int used;
84 int padding;
85 };
86
87 /*
88 * struct vdec_vp9_vsi - shared buffer between host and VPU firmware
89 * AP-W/R : AP is writer/reader on this item
90 * VPU-W/R: VPU is write/reader on this item
91 * @sf_bs_buf : super frame backup buffer (AP-W, VPU-R)
92 * @sf_ref_fb : record supoer frame reference buffer information
93 * (AP-R/W, VPU-R/W)
94 * @sf_next_ref_fb_idx : next available super frame (AP-W, VPU-R)
95 * @sf_frm_cnt : super frame count, filled by vpu (AP-R, VPU-W)
96 * @sf_frm_offset : super frame offset, filled by vpu (AP-R, VPU-W)
97 * @sf_frm_sz : super frame size, filled by vpu (AP-R, VPU-W)
98 * @sf_frm_idx : current super frame (AP-R, VPU-W)
99 * @sf_init : inform super frame info already parsed by vpu (AP-R, VPU-W)
100 * @fb : capture buffer (AP-W, VPU-R)
101 * @bs : bs buffer (AP-W, VPU-R)
102 * @cur_fb : current show capture buffer (AP-R/W, VPU-R/W)
103 * @pic_w : picture width (AP-R, VPU-W)
104 * @pic_h : picture height (AP-R, VPU-W)
105 * @buf_w : codec width (AP-R, VPU-W)
106 * @buf_h : coded height (AP-R, VPU-W)
107 * @buf_sz_y_bs : ufo compressed y plane size (AP-R, VPU-W)
108 * @buf_sz_c_bs : ufo compressed cbcr plane size (AP-R, VPU-W)
109 * @buf_len_sz_y : size used to store y plane ufo info (AP-R, VPU-W)
110 * @buf_len_sz_c : size used to store cbcr plane ufo info (AP-R, VPU-W)
111
112 * @profile : profile sparsed from vpu (AP-R, VPU-W)
113 * @show_frame : [BIT(0)] display this frame or not (AP-R, VPU-W)
114 * [BIT(1)] reset segment data or not (AP-R, VPU-W)
115 * [BIT(2)] trig decoder hardware or not (AP-R, VPU-W)
116 * [BIT(3)] ask VPU to set bits(0~4) accordingly (AP-W, VPU-R)
117 * [BIT(4)] do not reset segment data before every frame (AP-R, VPU-W)
118 * @show_existing_frame : inform this frame is show existing frame
119 * (AP-R, VPU-W)
120 * @frm_to_show_idx : index to show frame (AP-R, VPU-W)
121
122 * @refresh_frm_flags : indicate when frame need to refine reference count
123 * (AP-R, VPU-W)
124 * @resolution_changed : resolution change in this frame (AP-R, VPU-W)
125
126 * @frm_bufs : maintain reference buffer info (AP-R/W, VPU-R/W)
127 * @ref_frm_map : maintain reference buffer map info (AP-R/W, VPU-R/W)
128 * @new_fb_idx : index to frm_bufs array (AP-R, VPU-W)
129 * @frm_num : decoded frame number, include sub-frame count (AP-R, VPU-W)
130 * @mv_buf : motion vector working buffer (AP-W, VPU-R)
131 * @frm_refs : maintain three reference buffer info (AP-R/W, VPU-R/W)
132 * @seg_id_buf : segmentation map working buffer (AP-W, VPU-R)
133 */
134 struct vdec_vp9_vsi {
135 unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
136 struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUF_NUM-1];
137 int sf_next_ref_fb_idx;
138 unsigned int sf_frm_cnt;
139 unsigned int sf_frm_offset[VP9_MAX_FRM_BUF_NUM-1];
140 unsigned int sf_frm_sz[VP9_MAX_FRM_BUF_NUM-1];
141 unsigned int sf_frm_idx;
142 unsigned int sf_init;
143 struct vdec_fb fb;
144 struct mtk_vcodec_mem bs;
145 struct vdec_fb cur_fb;
146 unsigned int pic_w;
147 unsigned int pic_h;
148 unsigned int buf_w;
149 unsigned int buf_h;
150 unsigned int buf_sz_y_bs;
151 unsigned int buf_sz_c_bs;
152 unsigned int buf_len_sz_y;
153 unsigned int buf_len_sz_c;
154 unsigned int profile;
155 unsigned int show_frame;
156 unsigned int show_existing_frame;
157 unsigned int frm_to_show_idx;
158 unsigned int refresh_frm_flags;
159 unsigned int resolution_changed;
160
161 struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUF_NUM];
162 int ref_frm_map[MAX_NUM_REF_FRAMES];
163 unsigned int new_fb_idx;
164 unsigned int frm_num;
165 struct vp9_dram_buf mv_buf;
166
167 struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
168 struct vp9_dram_buf seg_id_buf;
169
170 };
171
172 /*
173 * struct vdec_vp9_inst - vp9 decode instance
174 * @mv_buf : working buffer for mv
175 * @seg_id_buf : working buffer for segmentation map
176 * @dec_fb : vdec_fb node to link fb to different fb_xxx_list
177 * @available_fb_node_list : current available vdec_fb node
178 * @fb_use_list : current used or referenced vdec_fb
179 * @fb_free_list : current available to free vdec_fb
180 * @fb_disp_list : current available to display vdec_fb
181 * @cur_fb : current frame buffer
182 * @ctx : current decode context
183 * @vpu : vpu instance information
184 * @vsi : shared buffer between host and VPU firmware
185 * @total_frm_cnt : total frame count, it do not include sub-frames in super
186 * frame
187 * @mem : instance memory information
188 */
189 struct vdec_vp9_inst {
190 struct mtk_vcodec_mem mv_buf;
191 struct mtk_vcodec_mem seg_id_buf;
192
193 struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUF_NODE_NUM];
194 struct list_head available_fb_node_list;
195 struct list_head fb_use_list;
196 struct list_head fb_free_list;
197 struct list_head fb_disp_list;
198 struct vdec_fb *cur_fb;
199 struct mtk_vcodec_ctx *ctx;
200 struct vdec_vpu_inst vpu;
201 struct vdec_vp9_vsi *vsi;
202 unsigned int total_frm_cnt;
203 struct mtk_vcodec_mem mem;
204 };
205
vp9_is_sf_ref_fb(struct vdec_vp9_inst * inst,struct vdec_fb * fb)206 static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
207 {
208 int i;
209 struct vdec_vp9_vsi *vsi = inst->vsi;
210
211 for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
212 if (fb == &vsi->sf_ref_fb[i].fb)
213 return true;
214 }
215 return false;
216 }
217
vp9_rm_from_fb_use_list(struct vdec_vp9_inst * inst,void * addr)218 static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
219 *inst, void *addr)
220 {
221 struct vdec_fb *fb = NULL;
222 struct vdec_fb_node *node;
223
224 list_for_each_entry(node, &inst->fb_use_list, list) {
225 fb = (struct vdec_fb *)node->fb;
226 if (fb->base_y.va == addr) {
227 list_move_tail(&node->list,
228 &inst->available_fb_node_list);
229 return fb;
230 }
231 }
232
233 return NULL;
234 }
235
vp9_add_to_fb_free_list(struct vdec_vp9_inst * inst,struct vdec_fb * fb)236 static void vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
237 struct vdec_fb *fb)
238 {
239 struct vdec_fb_node *node;
240
241 if (fb) {
242 node = list_first_entry_or_null(&inst->available_fb_node_list,
243 struct vdec_fb_node, list);
244
245 if (node) {
246 node->fb = fb;
247 list_move_tail(&node->list, &inst->fb_free_list);
248 }
249 } else {
250 mtk_vcodec_debug(inst, "No free fb node");
251 }
252 }
253
vp9_free_sf_ref_fb(struct vdec_fb * fb)254 static void vp9_free_sf_ref_fb(struct vdec_fb *fb)
255 {
256 struct vp9_sf_ref_fb *sf_ref_fb =
257 container_of(fb, struct vp9_sf_ref_fb, fb);
258
259 sf_ref_fb->used = 0;
260 }
261
vp9_ref_cnt_fb(struct vdec_vp9_inst * inst,int * idx,int new_idx)262 static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
263 int new_idx)
264 {
265 struct vdec_vp9_vsi *vsi = inst->vsi;
266 int ref_idx = *idx;
267
268 if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
269 vsi->frm_bufs[ref_idx].ref_cnt--;
270
271 if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
272 if (!vp9_is_sf_ref_fb(inst,
273 vsi->frm_bufs[ref_idx].buf.fb)) {
274 struct vdec_fb *fb;
275
276 fb = vp9_rm_from_fb_use_list(inst,
277 vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
278 vp9_add_to_fb_free_list(inst, fb);
279 } else
280 vp9_free_sf_ref_fb(
281 vsi->frm_bufs[ref_idx].buf.fb);
282 }
283 }
284
285 *idx = new_idx;
286 vsi->frm_bufs[new_idx].ref_cnt++;
287 }
288
vp9_free_all_sf_ref_fb(struct vdec_vp9_inst * inst)289 static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
290 {
291 int i;
292 struct vdec_vp9_vsi *vsi = inst->vsi;
293
294 for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
295 if (vsi->sf_ref_fb[i].fb.base_y.va) {
296 mtk_vcodec_mem_free(inst->ctx,
297 &vsi->sf_ref_fb[i].fb.base_y);
298 mtk_vcodec_mem_free(inst->ctx,
299 &vsi->sf_ref_fb[i].fb.base_c);
300 vsi->sf_ref_fb[i].used = 0;
301 }
302 }
303 }
304
305 /* For each sub-frame except the last one, the driver will dynamically
306 * allocate reference buffer by calling vp9_get_sf_ref_fb()
307 * The last sub-frame will use the original fb provided by the
308 * vp9_dec_decode() interface
309 */
vp9_get_sf_ref_fb(struct vdec_vp9_inst * inst)310 static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
311 {
312 int idx;
313 struct mtk_vcodec_mem *mem_basy_y;
314 struct mtk_vcodec_mem *mem_basy_c;
315 struct vdec_vp9_vsi *vsi = inst->vsi;
316
317 for (idx = 0;
318 idx < ARRAY_SIZE(vsi->sf_ref_fb);
319 idx++) {
320 if (vsi->sf_ref_fb[idx].fb.base_y.va &&
321 vsi->sf_ref_fb[idx].used == 0) {
322 return idx;
323 }
324 }
325
326 for (idx = 0;
327 idx < ARRAY_SIZE(vsi->sf_ref_fb);
328 idx++) {
329 if (vsi->sf_ref_fb[idx].fb.base_y.va == NULL)
330 break;
331 }
332
333 if (idx == ARRAY_SIZE(vsi->sf_ref_fb)) {
334 mtk_vcodec_err(inst, "List Full");
335 return -1;
336 }
337
338 mem_basy_y = &vsi->sf_ref_fb[idx].fb.base_y;
339 mem_basy_y->size = vsi->buf_sz_y_bs +
340 vsi->buf_len_sz_y;
341
342 if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_y)) {
343 mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
344 return -1;
345 }
346
347 mem_basy_c = &vsi->sf_ref_fb[idx].fb.base_c;
348 mem_basy_c->size = vsi->buf_sz_c_bs +
349 vsi->buf_len_sz_c;
350
351 if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_c)) {
352 mtk_vcodec_err(inst, "Cannot allocate sf_ref_fb c_buf");
353 return -1;
354 }
355 vsi->sf_ref_fb[idx].used = 0;
356
357 return idx;
358 }
359
vp9_alloc_work_buf(struct vdec_vp9_inst * inst)360 static bool vp9_alloc_work_buf(struct vdec_vp9_inst *inst)
361 {
362 struct vdec_vp9_vsi *vsi = inst->vsi;
363 int result;
364 struct mtk_vcodec_mem *mem;
365
366 unsigned int max_pic_w;
367 unsigned int max_pic_h;
368
369
370 if (!(inst->ctx->dev->dec_capability &
371 VCODEC_CAPABILITY_4K_DISABLED)) {
372 max_pic_w = VCODEC_DEC_4K_CODED_WIDTH;
373 max_pic_h = VCODEC_DEC_4K_CODED_HEIGHT;
374 } else {
375 max_pic_w = MTK_VDEC_MAX_W;
376 max_pic_h = MTK_VDEC_MAX_H;
377 }
378
379 if ((vsi->pic_w > max_pic_w) ||
380 (vsi->pic_h > max_pic_h)) {
381 mtk_vcodec_err(inst, "Invalid w/h %d/%d",
382 vsi->pic_w, vsi->pic_h);
383 return false;
384 }
385
386 mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
387 vsi->resolution_changed,
388 vsi->pic_w,
389 vsi->pic_h,
390 vsi->buf_w,
391 vsi->buf_h);
392
393 mem = &inst->mv_buf;
394 if (mem->va)
395 mtk_vcodec_mem_free(inst->ctx, mem);
396
397 mem->size = ((vsi->buf_w / 64) *
398 (vsi->buf_h / 64) + 2) * 36 * 16;
399 result = mtk_vcodec_mem_alloc(inst->ctx, mem);
400 if (result) {
401 mem->size = 0;
402 mtk_vcodec_err(inst, "Cannot allocate mv_buf");
403 return false;
404 }
405 /* Set the va again */
406 vsi->mv_buf.va = (unsigned long)mem->va;
407 vsi->mv_buf.pa = (unsigned long)mem->dma_addr;
408 vsi->mv_buf.sz = (unsigned int)mem->size;
409
410
411 mem = &inst->seg_id_buf;
412 if (mem->va)
413 mtk_vcodec_mem_free(inst->ctx, mem);
414
415 mem->size = VP9_SEG_ID_SZ;
416 result = mtk_vcodec_mem_alloc(inst->ctx, mem);
417 if (result) {
418 mem->size = 0;
419 mtk_vcodec_err(inst, "Cannot allocate seg_id_buf");
420 return false;
421 }
422 /* Set the va again */
423 vsi->seg_id_buf.va = (unsigned long)mem->va;
424 vsi->seg_id_buf.pa = (unsigned long)mem->dma_addr;
425 vsi->seg_id_buf.sz = (unsigned int)mem->size;
426
427
428 vp9_free_all_sf_ref_fb(inst);
429 vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
430
431 return true;
432 }
433
vp9_add_to_fb_disp_list(struct vdec_vp9_inst * inst,struct vdec_fb * fb)434 static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
435 struct vdec_fb *fb)
436 {
437 struct vdec_fb_node *node;
438
439 if (!fb) {
440 mtk_vcodec_err(inst, "fb == NULL");
441 return false;
442 }
443
444 node = list_first_entry_or_null(&inst->available_fb_node_list,
445 struct vdec_fb_node, list);
446 if (node) {
447 node->fb = fb;
448 list_move_tail(&node->list, &inst->fb_disp_list);
449 } else {
450 mtk_vcodec_err(inst, "No available fb node");
451 return false;
452 }
453
454 return true;
455 }
456
457 /* If any buffer updating is signaled it should be done here. */
vp9_swap_frm_bufs(struct vdec_vp9_inst * inst)458 static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
459 {
460 struct vdec_vp9_vsi *vsi = inst->vsi;
461 struct vp9_fb_info *frm_to_show;
462 int ref_index = 0, mask;
463
464 for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
465 if (mask & 1)
466 vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
467 vsi->new_fb_idx);
468 ++ref_index;
469 }
470
471 frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
472 vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
473
474 if (frm_to_show->fb != inst->cur_fb) {
475 /* This frame is show exist frame and no decode output
476 * copy frame data from frm_to_show to current CAPTURE
477 * buffer
478 */
479 if ((frm_to_show->fb != NULL) &&
480 (inst->cur_fb->base_y.size >=
481 frm_to_show->fb->base_y.size) &&
482 (inst->cur_fb->base_c.size >=
483 frm_to_show->fb->base_c.size)) {
484 memcpy((void *)inst->cur_fb->base_y.va,
485 (void *)frm_to_show->fb->base_y.va,
486 frm_to_show->fb->base_y.size);
487 memcpy((void *)inst->cur_fb->base_c.va,
488 (void *)frm_to_show->fb->base_c.va,
489 frm_to_show->fb->base_c.size);
490 } else {
491 /* After resolution change case, current CAPTURE buffer
492 * may have less buffer size than frm_to_show buffer
493 * size
494 */
495 if (frm_to_show->fb != NULL)
496 mtk_vcodec_err(inst,
497 "inst->cur_fb->base_y.size=%zu, frm_to_show->fb.base_y.size=%zu",
498 inst->cur_fb->base_y.size,
499 frm_to_show->fb->base_y.size);
500 }
501 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
502 if (vsi->show_frame & BIT(0))
503 vp9_add_to_fb_disp_list(inst, inst->cur_fb);
504 }
505 } else {
506 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
507 if (vsi->show_frame & BIT(0))
508 vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
509 }
510 }
511
512 /* when ref_cnt ==0, move this fb to fb_free_list. v4l2 driver will
513 * clean fb_free_list
514 */
515 if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
516 if (!vp9_is_sf_ref_fb(
517 inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
518 struct vdec_fb *fb;
519
520 fb = vp9_rm_from_fb_use_list(inst,
521 vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
522
523 vp9_add_to_fb_free_list(inst, fb);
524 } else {
525 vp9_free_sf_ref_fb(
526 vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
527 }
528 }
529
530 /* if this super frame and it is not last sub-frame, get next fb for
531 * sub-frame decode
532 */
533 if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
534 vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
535 }
536
vp9_wait_dec_end(struct vdec_vp9_inst * inst)537 static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
538 {
539 struct mtk_vcodec_ctx *ctx = inst->ctx;
540
541 mtk_vcodec_wait_for_done_ctx(inst->ctx,
542 MTK_INST_IRQ_RECEIVED,
543 WAIT_INTR_TIMEOUT_MS);
544
545 if (ctx->irq_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
546 return true;
547 else
548 return false;
549 }
550
vp9_alloc_inst(struct mtk_vcodec_ctx * ctx)551 static struct vdec_vp9_inst *vp9_alloc_inst(struct mtk_vcodec_ctx *ctx)
552 {
553 int result;
554 struct mtk_vcodec_mem mem;
555 struct vdec_vp9_inst *inst;
556
557 memset(&mem, 0, sizeof(mem));
558 mem.size = sizeof(struct vdec_vp9_inst);
559 result = mtk_vcodec_mem_alloc(ctx, &mem);
560 if (result)
561 return NULL;
562
563 inst = mem.va;
564 inst->mem = mem;
565
566 return inst;
567 }
568
vp9_free_inst(struct vdec_vp9_inst * inst)569 static void vp9_free_inst(struct vdec_vp9_inst *inst)
570 {
571 struct mtk_vcodec_mem mem;
572
573 mem = inst->mem;
574 if (mem.va)
575 mtk_vcodec_mem_free(inst->ctx, &mem);
576 }
577
vp9_decode_end_proc(struct vdec_vp9_inst * inst)578 static bool vp9_decode_end_proc(struct vdec_vp9_inst *inst)
579 {
580 struct vdec_vp9_vsi *vsi = inst->vsi;
581 bool ret = false;
582
583 if (!vsi->show_existing_frame) {
584 ret = vp9_wait_dec_end(inst);
585 if (!ret) {
586 mtk_vcodec_err(inst, "Decode failed, Decode Timeout @[%d]",
587 vsi->frm_num);
588 return false;
589 }
590
591 if (vpu_dec_end(&inst->vpu)) {
592 mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
593 return false;
594 }
595 mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
596 vsi->pic_w, vsi->pic_h);
597 } else {
598 mtk_vcodec_debug(inst, "Decode Ok @%d (show_existing_frame)",
599 vsi->frm_num);
600 }
601
602 vp9_swap_frm_bufs(inst);
603 vsi->frm_num++;
604 return true;
605 }
606
vp9_is_last_sub_frm(struct vdec_vp9_inst * inst)607 static bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
608 {
609 struct vdec_vp9_vsi *vsi = inst->vsi;
610
611 if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
612 return true;
613
614 return false;
615 }
616
vp9_rm_from_fb_disp_list(struct vdec_vp9_inst * inst)617 static struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst *inst)
618 {
619 struct vdec_fb_node *node;
620 struct vdec_fb *fb = NULL;
621
622 node = list_first_entry_or_null(&inst->fb_disp_list,
623 struct vdec_fb_node, list);
624 if (node) {
625 fb = (struct vdec_fb *)node->fb;
626 fb->status |= FB_ST_DISPLAY;
627 list_move_tail(&node->list, &inst->available_fb_node_list);
628 mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
629 node->fb, fb->status);
630 } else
631 mtk_vcodec_debug(inst, "[FB] there is no disp fb");
632
633 return fb;
634 }
635
vp9_add_to_fb_use_list(struct vdec_vp9_inst * inst,struct vdec_fb * fb)636 static bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
637 struct vdec_fb *fb)
638 {
639 struct vdec_fb_node *node;
640
641 if (!fb) {
642 mtk_vcodec_debug(inst, "fb == NULL");
643 return false;
644 }
645
646 node = list_first_entry_or_null(&inst->available_fb_node_list,
647 struct vdec_fb_node, list);
648 if (node) {
649 node->fb = fb;
650 list_move_tail(&node->list, &inst->fb_use_list);
651 } else {
652 mtk_vcodec_err(inst, "No free fb node");
653 return false;
654 }
655 return true;
656 }
657
vp9_reset(struct vdec_vp9_inst * inst)658 static void vp9_reset(struct vdec_vp9_inst *inst)
659 {
660 struct vdec_fb_node *node, *tmp;
661
662 list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
663 list_move_tail(&node->list, &inst->fb_free_list);
664
665 vp9_free_all_sf_ref_fb(inst);
666 inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
667
668 if (vpu_dec_reset(&inst->vpu))
669 mtk_vcodec_err(inst, "vp9_dec_vpu_reset failed");
670
671 /* Set the va again, since vpu_dec_reset will clear mv_buf in vpu */
672 inst->vsi->mv_buf.va = (unsigned long)inst->mv_buf.va;
673 inst->vsi->mv_buf.pa = (unsigned long)inst->mv_buf.dma_addr;
674 inst->vsi->mv_buf.sz = (unsigned long)inst->mv_buf.size;
675
676 /* Set the va again, since vpu_dec_reset will clear seg_id_buf in vpu */
677 inst->vsi->seg_id_buf.va = (unsigned long)inst->seg_id_buf.va;
678 inst->vsi->seg_id_buf.pa = (unsigned long)inst->seg_id_buf.dma_addr;
679 inst->vsi->seg_id_buf.sz = (unsigned long)inst->seg_id_buf.size;
680
681 }
682
init_all_fb_lists(struct vdec_vp9_inst * inst)683 static void init_all_fb_lists(struct vdec_vp9_inst *inst)
684 {
685 int i;
686
687 INIT_LIST_HEAD(&inst->available_fb_node_list);
688 INIT_LIST_HEAD(&inst->fb_use_list);
689 INIT_LIST_HEAD(&inst->fb_free_list);
690 INIT_LIST_HEAD(&inst->fb_disp_list);
691
692 for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) {
693 INIT_LIST_HEAD(&inst->dec_fb[i].list);
694 inst->dec_fb[i].fb = NULL;
695 list_add_tail(&inst->dec_fb[i].list,
696 &inst->available_fb_node_list);
697 }
698 }
699
get_pic_info(struct vdec_vp9_inst * inst,struct vdec_pic_info * pic)700 static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
701 {
702 pic->fb_sz[0] = inst->vsi->buf_sz_y_bs + inst->vsi->buf_len_sz_y;
703 pic->fb_sz[1] = inst->vsi->buf_sz_c_bs + inst->vsi->buf_len_sz_c;
704
705 pic->pic_w = inst->vsi->pic_w;
706 pic->pic_h = inst->vsi->pic_h;
707 pic->buf_w = inst->vsi->buf_w;
708 pic->buf_h = inst->vsi->buf_h;
709
710 mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
711 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
712 mtk_vcodec_debug(inst, "fb size: Y(%d), C(%d)",
713 pic->fb_sz[0],
714 pic->fb_sz[1]);
715 }
716
get_disp_fb(struct vdec_vp9_inst * inst,struct vdec_fb ** out_fb)717 static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
718 {
719
720 *out_fb = vp9_rm_from_fb_disp_list(inst);
721 if (*out_fb)
722 (*out_fb)->status |= FB_ST_DISPLAY;
723 }
724
get_free_fb(struct vdec_vp9_inst * inst,struct vdec_fb ** out_fb)725 static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
726 {
727 struct vdec_fb_node *node;
728 struct vdec_fb *fb = NULL;
729
730 node = list_first_entry_or_null(&inst->fb_free_list,
731 struct vdec_fb_node, list);
732 if (node) {
733 list_move_tail(&node->list, &inst->available_fb_node_list);
734 fb = (struct vdec_fb *)node->fb;
735 fb->status |= FB_ST_FREE;
736 mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
737 node->fb, fb->status);
738 } else {
739 mtk_vcodec_debug(inst, "[FB] there is no free fb");
740 }
741
742 *out_fb = fb;
743 }
744
validate_vsi_array_indexes(struct vdec_vp9_inst * inst,struct vdec_vp9_vsi * vsi)745 static int validate_vsi_array_indexes(struct vdec_vp9_inst *inst,
746 struct vdec_vp9_vsi *vsi) {
747 if (vsi->sf_frm_idx >= VP9_MAX_FRM_BUF_NUM - 1) {
748 mtk_vcodec_err(inst, "Invalid vsi->sf_frm_idx=%u.",
749 vsi->sf_frm_idx);
750 return -EIO;
751 }
752 if (vsi->frm_to_show_idx >= VP9_MAX_FRM_BUF_NUM) {
753 mtk_vcodec_err(inst, "Invalid vsi->frm_to_show_idx=%u.",
754 vsi->frm_to_show_idx);
755 return -EIO;
756 }
757 if (vsi->new_fb_idx >= VP9_MAX_FRM_BUF_NUM) {
758 mtk_vcodec_err(inst, "Invalid vsi->new_fb_idx=%u.",
759 vsi->new_fb_idx);
760 return -EIO;
761 }
762 return 0;
763 }
764
vdec_vp9_deinit(void * h_vdec)765 static void vdec_vp9_deinit(void *h_vdec)
766 {
767 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
768 struct mtk_vcodec_mem *mem;
769 int ret = 0;
770
771 ret = vpu_dec_deinit(&inst->vpu);
772 if (ret)
773 mtk_vcodec_err(inst, "vpu_dec_deinit failed");
774
775 mem = &inst->mv_buf;
776 if (mem->va)
777 mtk_vcodec_mem_free(inst->ctx, mem);
778
779 mem = &inst->seg_id_buf;
780 if (mem->va)
781 mtk_vcodec_mem_free(inst->ctx, mem);
782
783 vp9_free_all_sf_ref_fb(inst);
784 vp9_free_inst(inst);
785 }
786
vdec_vp9_init(struct mtk_vcodec_ctx * ctx)787 static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx)
788 {
789 struct vdec_vp9_inst *inst;
790
791 inst = vp9_alloc_inst(ctx);
792 if (!inst)
793 return -ENOMEM;
794
795 inst->total_frm_cnt = 0;
796 inst->ctx = ctx;
797
798 inst->vpu.id = IPI_VDEC_VP9;
799 inst->vpu.ctx = ctx;
800
801 if (vpu_dec_init(&inst->vpu)) {
802 mtk_vcodec_err(inst, "vp9_dec_vpu_init failed");
803 goto err_deinit_inst;
804 }
805
806 inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
807
808 inst->vsi->show_frame |= BIT(3);
809
810 init_all_fb_lists(inst);
811
812 ctx->drv_handle = inst;
813 return 0;
814
815 err_deinit_inst:
816 vp9_free_inst(inst);
817
818 return -EINVAL;
819 }
820
vdec_vp9_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)821 static int vdec_vp9_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
822 struct vdec_fb *fb, bool *res_chg)
823 {
824 int ret = 0;
825 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
826 struct vdec_vp9_vsi *vsi = inst->vsi;
827 u32 data[3];
828 int i;
829
830 *res_chg = false;
831
832 if ((bs == NULL) && (fb == NULL)) {
833 mtk_vcodec_debug(inst, "[EOS]");
834 vp9_reset(inst);
835 return ret;
836 }
837
838 if (bs == NULL) {
839 mtk_vcodec_err(inst, "bs == NULL");
840 return -EINVAL;
841 }
842
843 mtk_vcodec_debug(inst, "Input BS Size = %zu", bs->size);
844
845 while (1) {
846 struct vdec_fb *cur_fb = NULL;
847
848 data[0] = *((unsigned int *)bs->va);
849 data[1] = *((unsigned int *)(bs->va + 4));
850 data[2] = *((unsigned int *)(bs->va + 8));
851
852 vsi->bs = *bs;
853
854 if (fb)
855 vsi->fb = *fb;
856
857 if (!vsi->sf_init) {
858 unsigned int sf_bs_sz;
859 unsigned int sf_bs_off;
860 unsigned char *sf_bs_src;
861 unsigned char *sf_bs_dst;
862
863 sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
864 VP9_SUPER_FRAME_BS_SZ : bs->size;
865 sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
866 sf_bs_src = bs->va + bs->size - sf_bs_sz;
867 sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
868 memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
869 } else {
870 if ((vsi->sf_frm_cnt > 0) &&
871 (vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
872 unsigned int idx = vsi->sf_frm_idx;
873
874 memcpy((void *)bs->va,
875 (void *)(bs->va +
876 vsi->sf_frm_offset[idx]),
877 vsi->sf_frm_sz[idx]);
878 }
879 }
880
881 if (!(vsi->show_frame & BIT(4)))
882 memset(inst->seg_id_buf.va, 0, inst->seg_id_buf.size);
883
884 ret = vpu_dec_start(&inst->vpu, data, 3);
885 if (ret) {
886 mtk_vcodec_err(inst, "vpu_dec_start failed");
887 goto DECODE_ERROR;
888 }
889
890 if (vsi->show_frame & BIT(1)) {
891 memset(inst->seg_id_buf.va, 0, inst->seg_id_buf.size);
892
893 if (vsi->show_frame & BIT(2)) {
894 ret = vpu_dec_start(&inst->vpu, NULL, 0);
895 if (ret) {
896 mtk_vcodec_err(inst, "vpu trig decoder failed");
897 goto DECODE_ERROR;
898 }
899 }
900 }
901
902 ret = validate_vsi_array_indexes(inst, vsi);
903 if (ret) {
904 mtk_vcodec_err(inst, "Invalid values from VPU.");
905 goto DECODE_ERROR;
906 }
907
908 if (vsi->resolution_changed) {
909 if (!vp9_alloc_work_buf(inst)) {
910 ret = -EIO;
911 goto DECODE_ERROR;
912 }
913 }
914
915 if (vsi->sf_frm_cnt > 0) {
916 cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
917
918 if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
919 inst->cur_fb = cur_fb;
920 else
921 inst->cur_fb = fb;
922 } else {
923 inst->cur_fb = fb;
924 }
925
926 vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
927 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
928 vp9_add_to_fb_use_list(inst, inst->cur_fb);
929
930 mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
931
932 if (vsi->show_existing_frame)
933 mtk_vcodec_debug(inst,
934 "drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
935 vsi->new_fb_idx, vsi->frm_to_show_idx);
936
937 if (vsi->show_existing_frame && (vsi->frm_to_show_idx <
938 VP9_MAX_FRM_BUF_NUM)) {
939 mtk_vcodec_debug(inst,
940 "Skip Decode drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
941 vsi->new_fb_idx, vsi->frm_to_show_idx);
942
943 vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
944 vsi->frm_to_show_idx);
945 }
946
947 /* VPU assign the buffer pointer in its address space,
948 * reassign here
949 */
950 for (i = 0; i < ARRAY_SIZE(vsi->frm_refs); i++) {
951 unsigned int idx = vsi->frm_refs[i].idx;
952
953 vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
954 }
955
956 if (vsi->resolution_changed) {
957 *res_chg = true;
958 mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
959
960 ret = 0;
961 goto DECODE_ERROR;
962 }
963
964 if (!vp9_decode_end_proc(inst)) {
965 mtk_vcodec_err(inst, "vp9_decode_end_proc");
966 ret = -EINVAL;
967 goto DECODE_ERROR;
968 }
969
970 if (vp9_is_last_sub_frm(inst))
971 break;
972
973 }
974 inst->total_frm_cnt++;
975
976 DECODE_ERROR:
977 if (ret < 0)
978 vp9_add_to_fb_free_list(inst, fb);
979
980 return ret;
981 }
982
get_crop_info(struct vdec_vp9_inst * inst,struct v4l2_rect * cr)983 static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_rect *cr)
984 {
985 cr->left = 0;
986 cr->top = 0;
987 cr->width = inst->vsi->pic_w;
988 cr->height = inst->vsi->pic_h;
989 mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
990 cr->left, cr->top, cr->width, cr->height);
991 }
992
vdec_vp9_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)993 static int vdec_vp9_get_param(void *h_vdec, enum vdec_get_param_type type,
994 void *out)
995 {
996 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
997 int ret = 0;
998
999 switch (type) {
1000 case GET_PARAM_DISP_FRAME_BUFFER:
1001 get_disp_fb(inst, out);
1002 break;
1003 case GET_PARAM_FREE_FRAME_BUFFER:
1004 get_free_fb(inst, out);
1005 break;
1006 case GET_PARAM_PIC_INFO:
1007 get_pic_info(inst, out);
1008 break;
1009 case GET_PARAM_DPB_SIZE:
1010 *((unsigned int *)out) = MAX_VP9_DPB_SIZE;
1011 break;
1012 case GET_PARAM_CROP_INFO:
1013 get_crop_info(inst, out);
1014 break;
1015 default:
1016 mtk_vcodec_err(inst, "not supported param type %d", type);
1017 ret = -EINVAL;
1018 break;
1019 }
1020
1021 return ret;
1022 }
1023
1024 const struct vdec_common_if vdec_vp9_if = {
1025 .init = vdec_vp9_init,
1026 .decode = vdec_vp9_decode,
1027 .get_param = vdec_vp9_get_param,
1028 .deinit = vdec_vp9_deinit,
1029 };
1030