cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

mtk_vcodec_dec_stateful.c (18968B)


      1// SPDX-License-Identifier: GPL-2.0
      2
      3#include <media/v4l2-event.h>
      4#include <media/v4l2-mem2mem.h>
      5#include <media/videobuf2-dma-contig.h>
      6
      7#include "mtk_vcodec_drv.h"
      8#include "mtk_vcodec_dec.h"
      9#include "mtk_vcodec_intr.h"
     10#include "mtk_vcodec_util.h"
     11#include "mtk_vcodec_dec_pm.h"
     12#include "vdec_drv_if.h"
     13
     14static const struct mtk_video_fmt mtk_video_formats[] = {
     15	{
     16		.fourcc = V4L2_PIX_FMT_H264,
     17		.type = MTK_FMT_DEC,
     18		.num_planes = 1,
     19		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
     20	},
     21	{
     22		.fourcc = V4L2_PIX_FMT_VP8,
     23		.type = MTK_FMT_DEC,
     24		.num_planes = 1,
     25		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
     26	},
     27	{
     28		.fourcc = V4L2_PIX_FMT_VP9,
     29		.type = MTK_FMT_DEC,
     30		.num_planes = 1,
     31		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
     32	},
     33	{
     34		.fourcc = V4L2_PIX_FMT_MT21C,
     35		.type = MTK_FMT_FRAME,
     36		.num_planes = 2,
     37	},
     38};
     39
     40static const unsigned int num_supported_formats =
     41	ARRAY_SIZE(mtk_video_formats);
     42
     43#define DEFAULT_OUT_FMT_IDX 0
     44#define DEFAULT_CAP_FMT_IDX 3
     45
     46static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
     47	{
     48		.fourcc = V4L2_PIX_FMT_H264,
     49		.stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
     50			      MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
     51	},
     52	{
     53		.fourcc = V4L2_PIX_FMT_VP8,
     54		.stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
     55			      MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
     56	},
     57	{
     58		.fourcc = V4L2_PIX_FMT_VP9,
     59		.stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
     60			      MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
     61	},
     62};
     63
     64static const unsigned int num_supported_framesize =
     65	ARRAY_SIZE(mtk_vdec_framesizes);
     66
     67/*
     68 * This function tries to clean all display buffers, the buffers will return
     69 * in display order.
     70 * Note the buffers returned from codec driver may still be in driver's
     71 * reference list.
     72 */
     73static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
     74{
     75	struct vdec_fb *disp_frame_buffer = NULL;
     76	struct mtk_video_dec_buf *dstbuf;
     77	struct vb2_v4l2_buffer *vb;
     78
     79	mtk_v4l2_debug(3, "[%d]", ctx->id);
     80	if (vdec_if_get_param(ctx, GET_PARAM_DISP_FRAME_BUFFER,
     81			      &disp_frame_buffer)) {
     82		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER", ctx->id);
     83		return NULL;
     84	}
     85
     86	if (!disp_frame_buffer) {
     87		mtk_v4l2_debug(3, "No display frame buffer");
     88		return NULL;
     89	}
     90
     91	dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
     92			      frame_buffer);
     93	vb = &dstbuf->m2m_buf.vb;
     94	mutex_lock(&ctx->lock);
     95	if (dstbuf->used) {
     96		mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to done_list %d",
     97			       ctx->id, disp_frame_buffer->status,
     98			       vb->vb2_buf.index, dstbuf->queued_in_vb2);
     99
    100		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_DONE);
    101		ctx->decoded_frame_cnt++;
    102	}
    103	mutex_unlock(&ctx->lock);
    104	return &vb->vb2_buf;
    105}
    106
    107/*
    108 * This function tries to clean all capture buffers that are not used as
    109 * reference buffers by codec driver any more
    110 * In this case, we need re-queue buffer to vb2 buffer if user space
    111 * already returns this buffer to v4l2 or this buffer is just the output of
    112 * previous sps/pps/resolution change decode, or do nothing if user
    113 * space still owns this buffer
    114 */
    115static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
    116{
    117	struct mtk_video_dec_buf *dstbuf;
    118	struct vdec_fb *free_frame_buffer = NULL;
    119	struct vb2_v4l2_buffer *vb;
    120
    121	if (vdec_if_get_param(ctx, GET_PARAM_FREE_FRAME_BUFFER,
    122			      &free_frame_buffer)) {
    123		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
    124		return NULL;
    125	}
    126	if (!free_frame_buffer) {
    127		mtk_v4l2_debug(3, " No free frame buffer");
    128		return NULL;
    129	}
    130
    131	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p", ctx->id,
    132		       free_frame_buffer);
    133
    134	dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
    135			      frame_buffer);
    136	vb = &dstbuf->m2m_buf.vb;
    137
    138	mutex_lock(&ctx->lock);
    139	if (dstbuf->used) {
    140		if (dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2 &&
    141		    free_frame_buffer->status == FB_ST_FREE) {
    142			/*
    143			 * After decode sps/pps or non-display buffer, we don't
    144			 * need to return capture buffer to user space, but
    145			 * just re-queue this capture buffer to vb2 queue.
    146			 * This reduce overheads that dq/q unused capture
    147			 * buffer. In this case, queued_in_vb2 = true.
    148			 */
    149			mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to rdy_queue %d",
    150				       ctx->id, free_frame_buffer->status,
    151				       vb->vb2_buf.index, dstbuf->queued_in_vb2);
    152			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
    153		} else if (!dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2) {
    154			/*
    155			 * If buffer in v4l2 driver but not in vb2 queue yet,
    156			 * and we get this buffer from free_list, it means
    157			 * that codec driver do not use this buffer as
    158			 * reference buffer anymore. We should q buffer to vb2
    159			 * queue, so later work thread could get this buffer
    160			 * for decode. In this case, queued_in_vb2 = false
    161			 * means this buffer is not from previous decode
    162			 * output.
    163			 */
    164			mtk_v4l2_debug(2,
    165				       "[%d]status=%x queue id=%d to rdy_queue",
    166				       ctx->id, free_frame_buffer->status,
    167				       vb->vb2_buf.index);
    168			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
    169			dstbuf->queued_in_vb2 = true;
    170		} else {
    171			/*
    172			 * Codec driver do not need to reference this capture
    173			 * buffer and this buffer is not in v4l2 driver.
    174			 * Then we don't need to do any thing, just add log when
    175			 * we need to debug buffer flow.
    176			 * When this buffer q from user space, it could
    177			 * directly q to vb2 buffer
    178			 */
    179			mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
    180				       ctx->id, free_frame_buffer->status,
    181				       vb->vb2_buf.index, dstbuf->queued_in_vb2,
    182				       dstbuf->queued_in_v4l2);
    183		}
    184		dstbuf->used = false;
    185	}
    186	mutex_unlock(&ctx->lock);
    187	return &vb->vb2_buf;
    188}
    189
    190static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
    191{
    192	while (get_display_buffer(ctx))
    193		;
    194}
    195
    196static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
    197{
    198	while (get_free_buffer(ctx))
    199		;
    200}
    201
    202static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
    203{
    204	static const struct v4l2_event ev_src_ch = {
    205		.type = V4L2_EVENT_SOURCE_CHANGE,
    206		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
    207	};
    208
    209	mtk_v4l2_debug(1, "[%d]", ctx->id);
    210	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
    211}
    212
    213static int mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
    214{
    215	bool res_chg;
    216	int ret;
    217
    218	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
    219	if (ret)
    220		mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
    221
    222	clean_display_buffer(ctx);
    223	clean_free_buffer(ctx);
    224
    225	return 0;
    226}
    227
    228static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx *ctx,
    229				unsigned int pixelformat)
    230{
    231	const struct mtk_video_fmt *fmt;
    232	struct mtk_q_data *dst_q_data;
    233	unsigned int k;
    234
    235	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
    236	for (k = 0; k < num_supported_formats; k++) {
    237		fmt = &mtk_video_formats[k];
    238		if (fmt->fourcc == pixelformat) {
    239			mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
    240				       dst_q_data->fmt->fourcc, pixelformat);
    241			dst_q_data->fmt = fmt;
    242			return;
    243		}
    244	}
    245
    246	mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat);
    247}
    248
    249static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
    250{
    251	unsigned int dpbsize = 0;
    252	int ret;
    253
    254	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO,
    255			      &ctx->last_decoded_picinfo)) {
    256		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id);
    257		return -EINVAL;
    258	}
    259
    260	if (ctx->last_decoded_picinfo.pic_w == 0 ||
    261	    ctx->last_decoded_picinfo.pic_h == 0 ||
    262	    ctx->last_decoded_picinfo.buf_w == 0 ||
    263	    ctx->last_decoded_picinfo.buf_h == 0) {
    264		mtk_v4l2_err("Cannot get correct pic info");
    265		return -EINVAL;
    266	}
    267
    268	if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
    269	    ctx->picinfo.cap_fourcc != 0)
    270		mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
    271
    272	if (ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w ||
    273	    ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h)
    274		return 0;
    275
    276	mtk_v4l2_debug(1, "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", ctx->id,
    277		       ctx->last_decoded_picinfo.pic_w,
    278		       ctx->last_decoded_picinfo.pic_h, ctx->picinfo.pic_w,
    279		       ctx->picinfo.pic_h, ctx->last_decoded_picinfo.buf_w,
    280		       ctx->last_decoded_picinfo.buf_h);
    281
    282	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
    283	if (dpbsize == 0)
    284		mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
    285
    286	ctx->dpb_size = dpbsize;
    287
    288	return ret;
    289}
    290
    291static void mtk_vdec_worker(struct work_struct *work)
    292{
    293	struct mtk_vcodec_ctx *ctx =
    294		container_of(work, struct mtk_vcodec_ctx, decode_work);
    295	struct mtk_vcodec_dev *dev = ctx->dev;
    296	struct vb2_v4l2_buffer *src_buf, *dst_buf;
    297	struct mtk_vcodec_mem buf;
    298	struct vdec_fb *pfb;
    299	bool res_chg = false;
    300	int ret;
    301	struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
    302
    303	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
    304	if (!src_buf) {
    305		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
    306		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
    307		return;
    308	}
    309
    310	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
    311	if (!dst_buf) {
    312		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
    313		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
    314		return;
    315	}
    316
    317	dst_buf_info =
    318		container_of(dst_buf, struct mtk_video_dec_buf, m2m_buf.vb);
    319
    320	pfb = &dst_buf_info->frame_buffer;
    321	pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
    322	pfb->base_y.dma_addr =
    323		vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
    324	pfb->base_y.size = ctx->picinfo.fb_sz[0];
    325
    326	pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
    327	pfb->base_c.dma_addr =
    328		vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
    329	pfb->base_c.size = ctx->picinfo.fb_sz[1];
    330	pfb->status = 0;
    331	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
    332
    333	mtk_v4l2_debug(3,
    334		       "id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
    335		       dst_buf->vb2_buf.index, pfb, pfb->base_y.va,
    336		       &pfb->base_y.dma_addr, &pfb->base_c.dma_addr, pfb->base_y.size);
    337
    338	if (src_buf == &ctx->empty_flush_buf.vb) {
    339		mtk_v4l2_debug(1, "Got empty flush input buffer.");
    340		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
    341
    342		/* update dst buf status */
    343		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
    344		mutex_lock(&ctx->lock);
    345		dst_buf_info->used = false;
    346		mutex_unlock(&ctx->lock);
    347
    348		vdec_if_decode(ctx, NULL, NULL, &res_chg);
    349		clean_display_buffer(ctx);
    350		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
    351		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
    352			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
    353		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
    354		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
    355		clean_free_buffer(ctx);
    356		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
    357		return;
    358	}
    359
    360	src_buf_info =
    361		container_of(src_buf, struct mtk_video_dec_buf, m2m_buf.vb);
    362
    363	buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
    364	buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
    365	buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
    366	if (!buf.va) {
    367		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
    368		mtk_v4l2_err("[%d] id=%d src_addr is NULL!!", ctx->id,
    369			     src_buf->vb2_buf.index);
    370		return;
    371	}
    372	mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
    373		       ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
    374	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
    375	dst_buf->timecode = src_buf->timecode;
    376	mutex_lock(&ctx->lock);
    377	dst_buf_info->used = true;
    378	mutex_unlock(&ctx->lock);
    379	src_buf_info->used = true;
    380
    381	ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
    382
    383	if (ret) {
    384		mtk_v4l2_err(" <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
    385			     ctx->id, src_buf->vb2_buf.index, buf.size,
    386			     src_buf->vb2_buf.timestamp, dst_buf->vb2_buf.index, ret, res_chg);
    387		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
    388		if (ret == -EIO) {
    389			mutex_lock(&ctx->lock);
    390			src_buf_info->error = true;
    391			mutex_unlock(&ctx->lock);
    392		}
    393		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
    394	} else if (!res_chg) {
    395		/*
    396		 * we only return src buffer with VB2_BUF_STATE_DONE
    397		 * when decode success without resolution change
    398		 */
    399		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
    400		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
    401	}
    402
    403	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
    404	clean_display_buffer(ctx);
    405	clean_free_buffer(ctx);
    406
    407	if (!ret && res_chg) {
    408		mtk_vdec_pic_info_update(ctx);
    409		/*
    410		 * On encountering a resolution change in the stream.
    411		 * The driver must first process and decode all
    412		 * remaining buffers from before the resolution change
    413		 * point, so call flush decode here
    414		 */
    415		mtk_vdec_flush_decoder(ctx);
    416		/*
    417		 * After all buffers containing decoded frames from
    418		 * before the resolution change point ready to be
    419		 * dequeued on the CAPTURE queue, the driver sends a
    420		 * V4L2_EVENT_SOURCE_CHANGE event for source change
    421		 * type V4L2_EVENT_SRC_CH_RESOLUTION
    422		 */
    423		mtk_vdec_queue_res_chg_event(ctx);
    424	}
    425	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
    426}
    427
    428static void vb2ops_vdec_stateful_buf_queue(struct vb2_buffer *vb)
    429{
    430	struct vb2_v4l2_buffer *src_buf;
    431	struct mtk_vcodec_mem src_mem;
    432	bool res_chg = false;
    433	int ret;
    434	unsigned int dpbsize = 1, i;
    435	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
    436	struct vb2_v4l2_buffer *vb2_v4l2;
    437	struct mtk_q_data *dst_q_data;
    438
    439	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id,
    440		       vb->vb2_queue->type, vb->index, vb);
    441	/*
    442	 * check if this buffer is ready to be used after decode
    443	 */
    444	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
    445		struct mtk_video_dec_buf *buf;
    446
    447		vb2_v4l2 = to_vb2_v4l2_buffer(vb);
    448		buf = container_of(vb2_v4l2, struct mtk_video_dec_buf,
    449				   m2m_buf.vb);
    450		mutex_lock(&ctx->lock);
    451		if (!buf->used) {
    452			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
    453			buf->queued_in_vb2 = true;
    454			buf->queued_in_v4l2 = true;
    455		} else {
    456			buf->queued_in_vb2 = false;
    457			buf->queued_in_v4l2 = true;
    458		}
    459		mutex_unlock(&ctx->lock);
    460		return;
    461	}
    462
    463	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
    464
    465	if (ctx->state != MTK_STATE_INIT) {
    466		mtk_v4l2_debug(3, "[%d] already init driver %d", ctx->id,
    467			       ctx->state);
    468		return;
    469	}
    470
    471	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
    472	if (!src_buf) {
    473		mtk_v4l2_err("No src buffer");
    474		return;
    475	}
    476
    477	if (src_buf == &ctx->empty_flush_buf.vb) {
    478		/* This shouldn't happen. Just in case. */
    479		mtk_v4l2_err("Invalid flush buffer.");
    480		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
    481		return;
    482	}
    483
    484	src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
    485	src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
    486	src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
    487	mtk_v4l2_debug(2, "[%d] buf id=%d va=%p dma=%pad size=%zx", ctx->id,
    488		       src_buf->vb2_buf.index, src_mem.va, &src_mem.dma_addr,
    489		       src_mem.size);
    490
    491	ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
    492	if (ret || !res_chg) {
    493		/*
    494		 * fb == NULL means to parse SPS/PPS header or
    495		 * resolution info in src_mem. Decode can fail
    496		 * if there is no SPS header or picture info
    497		 * in bs
    498		 */
    499
    500		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
    501		if (ret == -EIO) {
    502			mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.", ctx->id);
    503			ctx->state = MTK_STATE_ABORT;
    504			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
    505		} else {
    506			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
    507		}
    508		mtk_v4l2_debug(ret ? 0 : 1,
    509			       "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
    510			       ctx->id, src_buf->vb2_buf.index, src_mem.size, ret, res_chg);
    511		return;
    512	}
    513
    514	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
    515		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id);
    516		return;
    517	}
    518
    519	ctx->last_decoded_picinfo = ctx->picinfo;
    520	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
    521	for (i = 0; i < dst_q_data->fmt->num_planes; i++) {
    522		dst_q_data->sizeimage[i] = ctx->picinfo.fb_sz[i];
    523		dst_q_data->bytesperline[i] = ctx->picinfo.buf_w;
    524	}
    525
    526	mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
    527		       ctx->id, ctx->picinfo.buf_w, ctx->picinfo.buf_h, ctx->picinfo.pic_w,
    528		       ctx->picinfo.pic_h, dst_q_data->sizeimage[0], dst_q_data->sizeimage[1]);
    529
    530	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
    531	if (dpbsize == 0)
    532		mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
    533
    534	ctx->dpb_size = dpbsize;
    535	ctx->state = MTK_STATE_HEADER;
    536	mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
    537
    538	mtk_vdec_queue_res_chg_event(ctx);
    539}
    540
    541static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
    542{
    543	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
    544	int ret = 0;
    545
    546	switch (ctrl->id) {
    547	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
    548		if (ctx->state >= MTK_STATE_HEADER) {
    549			ctrl->val = ctx->dpb_size;
    550		} else {
    551			mtk_v4l2_debug(0, "Seqinfo not ready");
    552			ctrl->val = 0;
    553		}
    554		break;
    555	default:
    556		ret = -EINVAL;
    557	}
    558	return ret;
    559}
    560
    561static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
    562	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
    563};
    564
    565static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
    566{
    567	struct v4l2_ctrl *ctrl;
    568
    569	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
    570
    571	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
    572				 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 0, 32, 1, 1);
    573	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
    574	v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
    575			       V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
    576			       V4L2_MPEG_VIDEO_VP9_PROFILE_0, 0,
    577			       V4L2_MPEG_VIDEO_VP9_PROFILE_0);
    578	/*
    579	 * H264. Baseline / Extended decoding is not supported.
    580	 */
    581	v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
    582			       V4L2_CID_MPEG_VIDEO_H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
    583			       BIT(V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
    584			       BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
    585			       V4L2_MPEG_VIDEO_H264_PROFILE_MAIN);
    586
    587	if (ctx->ctrl_hdl.error) {
    588		mtk_v4l2_err("Adding control failed %d", ctx->ctrl_hdl.error);
    589		return ctx->ctrl_hdl.error;
    590	}
    591
    592	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
    593	return 0;
    594}
    595
    596static void mtk_init_vdec_params(struct mtk_vcodec_ctx *ctx)
    597{
    598}
    599
    600static struct vb2_ops mtk_vdec_frame_vb2_ops = {
    601	.queue_setup = vb2ops_vdec_queue_setup,
    602	.buf_prepare = vb2ops_vdec_buf_prepare,
    603	.wait_prepare = vb2_ops_wait_prepare,
    604	.wait_finish = vb2_ops_wait_finish,
    605	.start_streaming = vb2ops_vdec_start_streaming,
    606
    607	.buf_queue = vb2ops_vdec_stateful_buf_queue,
    608	.buf_init = vb2ops_vdec_buf_init,
    609	.buf_finish = vb2ops_vdec_buf_finish,
    610	.stop_streaming = vb2ops_vdec_stop_streaming,
    611};
    612
    613const struct mtk_vcodec_dec_pdata mtk_vdec_8173_pdata = {
    614	.init_vdec_params = mtk_init_vdec_params,
    615	.ctrls_setup = mtk_vcodec_dec_ctrls_setup,
    616	.vdec_vb2_ops = &mtk_vdec_frame_vb2_ops,
    617	.vdec_formats = mtk_video_formats,
    618	.num_formats = &num_supported_formats,
    619	.default_out_fmt = &mtk_video_formats[DEFAULT_OUT_FMT_IDX],
    620	.default_cap_fmt = &mtk_video_formats[DEFAULT_CAP_FMT_IDX],
    621	.vdec_framesizes = mtk_vdec_framesizes,
    622	.num_framesizes = &num_supported_framesize,
    623	.worker = mtk_vdec_worker,
    624	.flush_decoder = mtk_vdec_flush_decoder,
    625	.is_subdev_supported = false,
    626	.hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
    627};