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 "../../common/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_ref_buf - 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_sf_ref_fb - 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_dec_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 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 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 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_vdec_debug(inst->ctx, "No free fb node"); 251 } 252 } 253 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 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 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 */ 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_vdec_err(inst->ctx, "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_vdec_err(inst->ctx, "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_vdec_err(inst->ctx, "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 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_vdec_err(inst->ctx, "Invalid w/h %d/%d", vsi->pic_w, vsi->pic_h); 382 return false; 383 } 384 385 mtk_vdec_debug(inst->ctx, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d", 386 vsi->resolution_changed, vsi->pic_w, 387 vsi->pic_h, vsi->buf_w, vsi->buf_h); 388 389 mem = &inst->mv_buf; 390 if (mem->va) 391 mtk_vcodec_mem_free(inst->ctx, mem); 392 393 mem->size = ((vsi->buf_w / 64) * 394 (vsi->buf_h / 64) + 2) * 36 * 16; 395 result = mtk_vcodec_mem_alloc(inst->ctx, mem); 396 if (result) { 397 mem->size = 0; 398 mtk_vdec_err(inst->ctx, "Cannot allocate mv_buf"); 399 return false; 400 } 401 /* Set the va again */ 402 vsi->mv_buf.va = (unsigned long)mem->va; 403 vsi->mv_buf.pa = (unsigned long)mem->dma_addr; 404 vsi->mv_buf.sz = (unsigned int)mem->size; 405 406 407 mem = &inst->seg_id_buf; 408 if (mem->va) 409 mtk_vcodec_mem_free(inst->ctx, mem); 410 411 mem->size = VP9_SEG_ID_SZ; 412 result = mtk_vcodec_mem_alloc(inst->ctx, mem); 413 if (result) { 414 mem->size = 0; 415 mtk_vdec_err(inst->ctx, "Cannot allocate seg_id_buf"); 416 return false; 417 } 418 /* Set the va again */ 419 vsi->seg_id_buf.va = (unsigned long)mem->va; 420 vsi->seg_id_buf.pa = (unsigned long)mem->dma_addr; 421 vsi->seg_id_buf.sz = (unsigned int)mem->size; 422 423 424 vp9_free_all_sf_ref_fb(inst); 425 vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst); 426 427 return true; 428 } 429 430 static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst, 431 struct vdec_fb *fb) 432 { 433 struct vdec_fb_node *node; 434 435 if (!fb) { 436 mtk_vdec_err(inst->ctx, "fb == NULL"); 437 return false; 438 } 439 440 node = list_first_entry_or_null(&inst->available_fb_node_list, 441 struct vdec_fb_node, list); 442 if (node) { 443 node->fb = fb; 444 list_move_tail(&node->list, &inst->fb_disp_list); 445 } else { 446 mtk_vdec_err(inst->ctx, "No available fb node"); 447 return false; 448 } 449 450 return true; 451 } 452 453 /* If any buffer updating is signaled it should be done here. */ 454 static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst) 455 { 456 struct vdec_vp9_vsi *vsi = inst->vsi; 457 struct vp9_fb_info *frm_to_show; 458 int ref_index = 0, mask; 459 460 for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) { 461 if (mask & 1) 462 vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index], 463 vsi->new_fb_idx); 464 ++ref_index; 465 } 466 467 frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf; 468 vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--; 469 470 if (frm_to_show->fb != inst->cur_fb) { 471 /* This frame is show exist frame and no decode output 472 * copy frame data from frm_to_show to current CAPTURE 473 * buffer 474 */ 475 if ((frm_to_show->fb != NULL) && 476 (inst->cur_fb->base_y.size >= 477 frm_to_show->fb->base_y.size) && 478 (inst->cur_fb->base_c.size >= 479 frm_to_show->fb->base_c.size)) { 480 memcpy((void *)inst->cur_fb->base_y.va, 481 (void *)frm_to_show->fb->base_y.va, 482 frm_to_show->fb->base_y.size); 483 memcpy((void *)inst->cur_fb->base_c.va, 484 (void *)frm_to_show->fb->base_c.va, 485 frm_to_show->fb->base_c.size); 486 } else { 487 /* After resolution change case, current CAPTURE buffer 488 * may have less buffer size than frm_to_show buffer 489 * size 490 */ 491 if (frm_to_show->fb != NULL) 492 mtk_vdec_err(inst->ctx, 493 "base_y.size=%zu, frm_to_show: base_y.size=%zu", 494 inst->cur_fb->base_y.size, 495 frm_to_show->fb->base_y.size); 496 } 497 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) { 498 if (vsi->show_frame & BIT(0)) 499 vp9_add_to_fb_disp_list(inst, inst->cur_fb); 500 } 501 } else { 502 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) { 503 if (vsi->show_frame & BIT(0)) 504 vp9_add_to_fb_disp_list(inst, frm_to_show->fb); 505 } 506 } 507 508 /* when ref_cnt ==0, move this fb to fb_free_list. v4l2 driver will 509 * clean fb_free_list 510 */ 511 if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) { 512 if (!vp9_is_sf_ref_fb( 513 inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) { 514 struct vdec_fb *fb; 515 516 fb = vp9_rm_from_fb_use_list(inst, 517 vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va); 518 519 vp9_add_to_fb_free_list(inst, fb); 520 } else { 521 vp9_free_sf_ref_fb( 522 vsi->frm_bufs[vsi->new_fb_idx].buf.fb); 523 } 524 } 525 526 /* if this super frame and it is not last sub-frame, get next fb for 527 * sub-frame decode 528 */ 529 if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1) 530 vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst); 531 } 532 533 static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst) 534 { 535 struct mtk_vcodec_dec_ctx *ctx = inst->ctx; 536 537 mtk_vcodec_wait_for_done_ctx(inst->ctx, 538 MTK_INST_IRQ_RECEIVED, 539 WAIT_INTR_TIMEOUT_MS, 0); 540 541 if (ctx->irq_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) 542 return true; 543 else 544 return false; 545 } 546 547 static struct vdec_vp9_inst *vp9_alloc_inst(struct mtk_vcodec_dec_ctx *ctx) 548 { 549 int result; 550 struct mtk_vcodec_mem mem; 551 struct vdec_vp9_inst *inst; 552 553 memset(&mem, 0, sizeof(mem)); 554 mem.size = sizeof(struct vdec_vp9_inst); 555 result = mtk_vcodec_mem_alloc(ctx, &mem); 556 if (result) 557 return NULL; 558 559 inst = mem.va; 560 inst->mem = mem; 561 562 return inst; 563 } 564 565 static void vp9_free_inst(struct vdec_vp9_inst *inst) 566 { 567 struct mtk_vcodec_mem mem; 568 569 mem = inst->mem; 570 if (mem.va) 571 mtk_vcodec_mem_free(inst->ctx, &mem); 572 } 573 574 static bool vp9_decode_end_proc(struct vdec_vp9_inst *inst) 575 { 576 struct vdec_vp9_vsi *vsi = inst->vsi; 577 bool ret = false; 578 579 if (!vsi->show_existing_frame) { 580 ret = vp9_wait_dec_end(inst); 581 if (!ret) { 582 mtk_vdec_err(inst->ctx, "Decode failed, Decode Timeout @[%d]", 583 vsi->frm_num); 584 return false; 585 } 586 587 if (vpu_dec_end(&inst->vpu)) { 588 mtk_vdec_err(inst->ctx, "vp9_dec_vpu_end failed"); 589 return false; 590 } 591 mtk_vdec_debug(inst->ctx, "Decode Ok @%d (%d/%d)", vsi->frm_num, 592 vsi->pic_w, vsi->pic_h); 593 } else { 594 mtk_vdec_debug(inst->ctx, "Decode Ok @%d (show_existing_frame)", vsi->frm_num); 595 } 596 597 vp9_swap_frm_bufs(inst); 598 vsi->frm_num++; 599 return true; 600 } 601 602 static bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst) 603 { 604 struct vdec_vp9_vsi *vsi = inst->vsi; 605 606 if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt) 607 return true; 608 609 return false; 610 } 611 612 static struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst *inst) 613 { 614 struct vdec_fb_node *node; 615 struct vdec_fb *fb = NULL; 616 617 node = list_first_entry_or_null(&inst->fb_disp_list, 618 struct vdec_fb_node, list); 619 if (node) { 620 fb = (struct vdec_fb *)node->fb; 621 fb->status |= FB_ST_DISPLAY; 622 list_move_tail(&node->list, &inst->available_fb_node_list); 623 mtk_vdec_debug(inst->ctx, "[FB] get disp fb %p st=%d", node->fb, fb->status); 624 } else 625 mtk_vdec_debug(inst->ctx, "[FB] there is no disp fb"); 626 627 return fb; 628 } 629 630 static bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst, 631 struct vdec_fb *fb) 632 { 633 struct vdec_fb_node *node; 634 635 if (!fb) { 636 mtk_vdec_debug(inst->ctx, "fb == NULL"); 637 return false; 638 } 639 640 node = list_first_entry_or_null(&inst->available_fb_node_list, 641 struct vdec_fb_node, list); 642 if (node) { 643 node->fb = fb; 644 list_move_tail(&node->list, &inst->fb_use_list); 645 } else { 646 mtk_vdec_err(inst->ctx, "No free fb node"); 647 return false; 648 } 649 return true; 650 } 651 652 static void vp9_reset(struct vdec_vp9_inst *inst) 653 { 654 struct vdec_fb_node *node, *tmp; 655 656 list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list) 657 list_move_tail(&node->list, &inst->fb_free_list); 658 659 vp9_free_all_sf_ref_fb(inst); 660 inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst); 661 662 if (vpu_dec_reset(&inst->vpu)) 663 mtk_vdec_err(inst->ctx, "vp9_dec_vpu_reset failed"); 664 665 /* Set the va again, since vpu_dec_reset will clear mv_buf in vpu */ 666 inst->vsi->mv_buf.va = (unsigned long)inst->mv_buf.va; 667 inst->vsi->mv_buf.pa = (unsigned long)inst->mv_buf.dma_addr; 668 inst->vsi->mv_buf.sz = (unsigned long)inst->mv_buf.size; 669 670 /* Set the va again, since vpu_dec_reset will clear seg_id_buf in vpu */ 671 inst->vsi->seg_id_buf.va = (unsigned long)inst->seg_id_buf.va; 672 inst->vsi->seg_id_buf.pa = (unsigned long)inst->seg_id_buf.dma_addr; 673 inst->vsi->seg_id_buf.sz = (unsigned long)inst->seg_id_buf.size; 674 675 } 676 677 static void init_all_fb_lists(struct vdec_vp9_inst *inst) 678 { 679 int i; 680 681 INIT_LIST_HEAD(&inst->available_fb_node_list); 682 INIT_LIST_HEAD(&inst->fb_use_list); 683 INIT_LIST_HEAD(&inst->fb_free_list); 684 INIT_LIST_HEAD(&inst->fb_disp_list); 685 686 for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) { 687 INIT_LIST_HEAD(&inst->dec_fb[i].list); 688 inst->dec_fb[i].fb = NULL; 689 list_add_tail(&inst->dec_fb[i].list, 690 &inst->available_fb_node_list); 691 } 692 } 693 694 static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic) 695 { 696 pic->fb_sz[0] = inst->vsi->buf_sz_y_bs + inst->vsi->buf_len_sz_y; 697 pic->fb_sz[1] = inst->vsi->buf_sz_c_bs + inst->vsi->buf_len_sz_c; 698 699 pic->pic_w = inst->vsi->pic_w; 700 pic->pic_h = inst->vsi->pic_h; 701 pic->buf_w = inst->vsi->buf_w; 702 pic->buf_h = inst->vsi->buf_h; 703 704 mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)", 705 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h); 706 mtk_vdec_debug(inst->ctx, "fb size: Y(%d), C(%d)", pic->fb_sz[0], pic->fb_sz[1]); 707 } 708 709 static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb) 710 { 711 712 *out_fb = vp9_rm_from_fb_disp_list(inst); 713 if (*out_fb) 714 (*out_fb)->status |= FB_ST_DISPLAY; 715 } 716 717 static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb) 718 { 719 struct vdec_fb_node *node; 720 struct vdec_fb *fb = NULL; 721 722 node = list_first_entry_or_null(&inst->fb_free_list, 723 struct vdec_fb_node, list); 724 if (node) { 725 list_move_tail(&node->list, &inst->available_fb_node_list); 726 fb = (struct vdec_fb *)node->fb; 727 fb->status |= FB_ST_FREE; 728 mtk_vdec_debug(inst->ctx, "[FB] get free fb %p st=%d", node->fb, fb->status); 729 } else { 730 mtk_vdec_debug(inst->ctx, "[FB] there is no free fb"); 731 } 732 733 *out_fb = fb; 734 } 735 736 static int validate_vsi_array_indexes(struct vdec_vp9_inst *inst, 737 struct vdec_vp9_vsi *vsi) { 738 if (vsi->sf_frm_idx >= VP9_MAX_FRM_BUF_NUM - 1) { 739 mtk_vdec_err(inst->ctx, "Invalid vsi->sf_frm_idx=%u.", vsi->sf_frm_idx); 740 return -EIO; 741 } 742 if (vsi->frm_to_show_idx >= VP9_MAX_FRM_BUF_NUM) { 743 mtk_vdec_err(inst->ctx, "Invalid vsi->frm_to_show_idx=%u.", vsi->frm_to_show_idx); 744 return -EIO; 745 } 746 if (vsi->new_fb_idx >= VP9_MAX_FRM_BUF_NUM) { 747 mtk_vdec_err(inst->ctx, "Invalid vsi->new_fb_idx=%u.", vsi->new_fb_idx); 748 return -EIO; 749 } 750 return 0; 751 } 752 753 static void vdec_vp9_deinit(void *h_vdec) 754 { 755 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec; 756 struct mtk_vcodec_mem *mem; 757 int ret = 0; 758 759 ret = vpu_dec_deinit(&inst->vpu); 760 if (ret) 761 mtk_vdec_err(inst->ctx, "vpu_dec_deinit failed"); 762 763 mem = &inst->mv_buf; 764 if (mem->va) 765 mtk_vcodec_mem_free(inst->ctx, mem); 766 767 mem = &inst->seg_id_buf; 768 if (mem->va) 769 mtk_vcodec_mem_free(inst->ctx, mem); 770 771 vp9_free_all_sf_ref_fb(inst); 772 vp9_free_inst(inst); 773 } 774 775 static int vdec_vp9_init(struct mtk_vcodec_dec_ctx *ctx) 776 { 777 struct vdec_vp9_inst *inst; 778 779 inst = vp9_alloc_inst(ctx); 780 if (!inst) 781 return -ENOMEM; 782 783 inst->total_frm_cnt = 0; 784 inst->ctx = ctx; 785 786 inst->vpu.id = IPI_VDEC_VP9; 787 inst->vpu.ctx = ctx; 788 789 if (vpu_dec_init(&inst->vpu)) { 790 mtk_vdec_err(inst->ctx, "vp9_dec_vpu_init failed"); 791 goto err_deinit_inst; 792 } 793 794 inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi; 795 796 inst->vsi->show_frame |= BIT(3); 797 798 init_all_fb_lists(inst); 799 800 ctx->drv_handle = inst; 801 return 0; 802 803 err_deinit_inst: 804 vp9_free_inst(inst); 805 806 return -EINVAL; 807 } 808 809 static int vdec_vp9_decode(void *h_vdec, struct mtk_vcodec_mem *bs, 810 struct vdec_fb *fb, bool *res_chg) 811 { 812 int ret = 0; 813 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec; 814 struct vdec_vp9_vsi *vsi = inst->vsi; 815 u32 data[3]; 816 int i; 817 818 *res_chg = false; 819 820 if ((bs == NULL) && (fb == NULL)) { 821 mtk_vdec_debug(inst->ctx, "[EOS]"); 822 vp9_reset(inst); 823 return ret; 824 } 825 826 if (bs == NULL) { 827 mtk_vdec_err(inst->ctx, "bs == NULL"); 828 return -EINVAL; 829 } 830 831 mtk_vdec_debug(inst->ctx, "Input BS Size = %zu", bs->size); 832 833 while (1) { 834 struct vdec_fb *cur_fb = NULL; 835 836 data[0] = *((unsigned int *)bs->va); 837 data[1] = *((unsigned int *)(bs->va + 4)); 838 data[2] = *((unsigned int *)(bs->va + 8)); 839 840 vsi->bs = *bs; 841 842 if (fb) 843 vsi->fb = *fb; 844 845 if (!vsi->sf_init) { 846 unsigned int sf_bs_sz; 847 unsigned int sf_bs_off; 848 unsigned char *sf_bs_src; 849 unsigned char *sf_bs_dst; 850 851 sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ? 852 VP9_SUPER_FRAME_BS_SZ : bs->size; 853 sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz; 854 sf_bs_src = bs->va + bs->size - sf_bs_sz; 855 sf_bs_dst = vsi->sf_bs_buf + sf_bs_off; 856 memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz); 857 } else { 858 if ((vsi->sf_frm_cnt > 0) && 859 (vsi->sf_frm_idx < vsi->sf_frm_cnt)) { 860 unsigned int idx = vsi->sf_frm_idx; 861 862 memcpy((void *)bs->va, 863 (void *)(bs->va + 864 vsi->sf_frm_offset[idx]), 865 vsi->sf_frm_sz[idx]); 866 } 867 } 868 869 if (!(vsi->show_frame & BIT(4))) 870 memset(inst->seg_id_buf.va, 0, inst->seg_id_buf.size); 871 872 ret = vpu_dec_start(&inst->vpu, data, 3); 873 if (ret) { 874 mtk_vdec_err(inst->ctx, "vpu_dec_start failed"); 875 goto DECODE_ERROR; 876 } 877 878 if (vsi->show_frame & BIT(1)) { 879 memset(inst->seg_id_buf.va, 0, inst->seg_id_buf.size); 880 881 if (vsi->show_frame & BIT(2)) { 882 ret = vpu_dec_start(&inst->vpu, NULL, 0); 883 if (ret) { 884 mtk_vdec_err(inst->ctx, "vpu trig decoder failed"); 885 goto DECODE_ERROR; 886 } 887 } 888 } 889 890 ret = validate_vsi_array_indexes(inst, vsi); 891 if (ret) { 892 mtk_vdec_err(inst->ctx, "Invalid values from VPU."); 893 goto DECODE_ERROR; 894 } 895 896 if (vsi->resolution_changed) { 897 if (!vp9_alloc_work_buf(inst)) { 898 ret = -EIO; 899 goto DECODE_ERROR; 900 } 901 } 902 903 if (vsi->sf_frm_cnt > 0) { 904 cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb; 905 906 if (vsi->sf_frm_idx < vsi->sf_frm_cnt) 907 inst->cur_fb = cur_fb; 908 else 909 inst->cur_fb = fb; 910 } else { 911 inst->cur_fb = fb; 912 } 913 914 vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb; 915 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) 916 vp9_add_to_fb_use_list(inst, inst->cur_fb); 917 918 mtk_vdec_debug(inst->ctx, "[#pic %d]", vsi->frm_num); 919 920 if (vsi->show_existing_frame) 921 mtk_vdec_debug(inst->ctx, 922 "drv->new_fb_idx=%d, drv->frm_to_show_idx=%d", 923 vsi->new_fb_idx, vsi->frm_to_show_idx); 924 925 if (vsi->show_existing_frame && (vsi->frm_to_show_idx < 926 VP9_MAX_FRM_BUF_NUM)) { 927 mtk_vdec_debug(inst->ctx, 928 "Skip Decode drv->new_fb_idx=%d, drv->frm_to_show_idx=%d", 929 vsi->new_fb_idx, vsi->frm_to_show_idx); 930 931 vp9_ref_cnt_fb(inst, &vsi->new_fb_idx, 932 vsi->frm_to_show_idx); 933 } 934 935 /* VPU assign the buffer pointer in its address space, 936 * reassign here 937 */ 938 for (i = 0; i < ARRAY_SIZE(vsi->frm_refs); i++) { 939 unsigned int idx = vsi->frm_refs[i].idx; 940 941 vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf; 942 } 943 944 if (vsi->resolution_changed) { 945 *res_chg = true; 946 mtk_vdec_debug(inst->ctx, "VDEC_ST_RESOLUTION_CHANGED"); 947 948 ret = 0; 949 goto DECODE_ERROR; 950 } 951 952 if (!vp9_decode_end_proc(inst)) { 953 mtk_vdec_err(inst->ctx, "vp9_decode_end_proc"); 954 ret = -EINVAL; 955 goto DECODE_ERROR; 956 } 957 958 if (vp9_is_last_sub_frm(inst)) 959 break; 960 961 } 962 inst->total_frm_cnt++; 963 964 DECODE_ERROR: 965 if (ret < 0) 966 vp9_add_to_fb_free_list(inst, fb); 967 968 return ret; 969 } 970 971 static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_rect *cr) 972 { 973 cr->left = 0; 974 cr->top = 0; 975 cr->width = inst->vsi->pic_w; 976 cr->height = inst->vsi->pic_h; 977 mtk_vdec_debug(inst->ctx, "get crop info l=%d, t=%d, w=%d, h=%d\n", 978 cr->left, cr->top, cr->width, cr->height); 979 } 980 981 static int vdec_vp9_get_param(void *h_vdec, enum vdec_get_param_type type, 982 void *out) 983 { 984 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec; 985 int ret = 0; 986 987 switch (type) { 988 case GET_PARAM_DISP_FRAME_BUFFER: 989 get_disp_fb(inst, out); 990 break; 991 case GET_PARAM_FREE_FRAME_BUFFER: 992 get_free_fb(inst, out); 993 break; 994 case GET_PARAM_PIC_INFO: 995 get_pic_info(inst, out); 996 break; 997 case GET_PARAM_DPB_SIZE: 998 *((unsigned int *)out) = MAX_VP9_DPB_SIZE; 999 break; 1000 case GET_PARAM_CROP_INFO: 1001 get_crop_info(inst, out); 1002 break; 1003 default: 1004 mtk_vdec_err(inst->ctx, "not supported param type %d", type); 1005 ret = -EINVAL; 1006 break; 1007 } 1008 1009 return ret; 1010 } 1011 1012 const struct vdec_common_if vdec_vp9_if = { 1013 .init = vdec_vp9_init, 1014 .decode = vdec_vp9_decode, 1015 .get_param = vdec_vp9_get_param, 1016 .deinit = vdec_vp9_deinit, 1017 }; 1018