Эх сурвалжийг харах

video: driver: add decoder input streamon support

Add decoder input streamon functionality which
include internal buffers support as well.

Change-Id: Ifb5afa51a29270705863c273d1ae08310666895d
Signed-off-by: Maheshwar Ajja <[email protected]>
Maheshwar Ajja 4 жил өмнө
parent
commit
6f3faa8d04

+ 3 - 1
Makefile

@@ -21,6 +21,7 @@ msm-vidc-objs   := driver/vidc/src/msm_vidc_v4l2.o \
                    driver/vidc/src/msm_vdec.o \
                    driver/vidc/src/msm_venc.o \
                    driver/vidc/src/msm_vidc_driver.o \
+                   driver/vidc/src/msm_vidc_buffer.o \
                    driver/vidc/src/msm_vidc_probe.o \
                    driver/vidc/src/msm_vidc_dt.o \
                    driver/vidc/src/msm_vidc_platform.o \
@@ -36,7 +37,8 @@ include $(srctree)/techpack/video/config/waipio_video.conf
 LINUXINCLUDE    += -include $(srctree)/techpack/video/config/waipio_video.h
 
 msm-vidc-objs   += driver/platform/waipio/src/msm_vidc_waipio.o \
-                   driver/variant/iris2/src/msm_vidc_iris2.o
+                   driver/variant/iris2/src/msm_vidc_iris2.o \
+                   driver/variant/iris2/src/msm_vidc_buffer_iris2.o
 endif
 endif
 

+ 1 - 1
config/waipio_video.conf

@@ -1 +1 @@
-export CONFIG_MSM_VIDC_V4L2=m
+export CONFIG_MSM_VIDC_V4L2=y

+ 20 - 0
driver/variant/iris2/inc/msm_vidc_buffer_iris2.h

@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ */
+
+#ifndef __H_MSM_VIDC_BUFFER_IRIS2_H__
+#define __H_MSM_VIDC_BUFFER_IRIS2_H__
+
+#include "msm_vidc_inst.h"
+
+u32 msm_vidc_decoder_scratch_size_iris2(struct msm_vidc_inst *inst);
+u32 msm_vidc_decoder_scratch_1_size_iris2(struct msm_vidc_inst *inst);
+u32 msm_vidc_decoder_persist_1_size_iris2(struct msm_vidc_inst *inst);
+
+u32 msm_vidc_encoder_scratch_size_iris2(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_scratch_1_size_iris2(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_scratch_2_size_iris2(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_persist_size_iris2(struct msm_vidc_inst *inst);
+
+#endif // __H_MSM_VIDC_BUFFER_IRIS2_H__

+ 1192 - 0
driver/variant/iris2/src/msm_vidc_buffer_iris2.c

@@ -0,0 +1,1192 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ */
+
+#include <media/msm_media_info.h>
+#include "msm_vidc_buffer_iris2.h"
+#include "msm_vidc_buffer.h"
+#include "msm_vidc_inst.h"
+#include "msm_vidc_core.h"
+#include "msm_vidc_driver.h"
+#include "msm_vidc_debug.h"
+
+#define HFI_COLOR_FORMAT_YUV420_NV12_UBWC_Y_TILE_WIDTH 32
+#define HFI_COLOR_FORMAT_YUV420_NV12_UBWC_Y_TILE_HEIGHT 8
+#define HFI_COLOR_FORMAT_YUV420_NV12_UBWC_UV_TILE_WIDTH 16
+#define HFI_COLOR_FORMAT_YUV420_NV12_UBWC_UV_TILE_HEIGHT 8
+#define HFI_COLOR_FORMAT_YUV420_TP10_UBWC_Y_TILE_WIDTH 48
+#define HFI_COLOR_FORMAT_YUV420_TP10_UBWC_Y_TILE_HEIGHT 4
+#define BUFFER_ALIGNMENT_4096_BYTES 4096
+#define VENUS_METADATA_STRIDE_MULTIPLE     64
+#define VENUS_METADATA_HEIGHT_MULTIPLE     16
+#define HFI_UBWC_CALC_METADATA_PLANE_STRIDE \
+	((metadataStride, width, metadataStrideMultiple, tileWidthInPels) \
+	metadataStride = ALIGN(((width + (tileWidthInPels - 1)) / \
+		tileWidthInPels), metadataStrideMultiple))
+#define HFI_UBWC_METADATA_PLANE_BUFHEIGHT \
+	((metadataBufHeight, height, metadataHeightMultiple, tileHeightInPels) \
+	metadataBufHeight = ALIGN(((height + (tileHeightInPels - 1)) / \
+		tileHeightInPels), metadataHeightMultiple))
+#define HFI_UBWC_METADATA_PLANE_BUFFER_SIZE \
+	((buffersize, MetadataStride, MetadataBufHeight) \
+	buffersize = ALIGN(MetadataStride * MetadataBufHeight, \
+		BUFFER_ALIGNMENT_4096_BYTES))
+#define HFI_UBWC_UV_METADATA_PLANE_STRIDE \
+	((metadataStride, width, metadataStrideMultiple, tileWidthInPels) \
+		metadataStride = ALIGN(((((width + 1) >> 1) + \
+		(tileWidthInPels - 1)) / tileWidthInPels), \
+		metadataStrideMultiple))
+#define HFI_UBWC_UV_METADATA_PLANE_BUFHEIGHT \
+	((metadataBufHeight, height, metadataHeightMultiple, tileHeightInPels) \
+	metadataBufHeight = ALIGN(((((height + 1) >> 1) + \
+		(tileHeightInPels - 1)) / tileHeightInPels), \
+		metadataHeightMultiple))
+
+#define BUFFER_ALIGNMENT_SIZE(x) x
+
+#define VENUS_DMA_ALIGNMENT BUFFER_ALIGNMENT_SIZE(256)
+
+#define MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE   64
+#define MAX_FE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE   64
+#define MAX_FE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE   64
+#define MAX_FE_NBR_DATA_LUMA_LINE_BUFFER_SIZE   640
+#define MAX_FE_NBR_DATA_CB_LINE_BUFFER_SIZE     320
+#define MAX_FE_NBR_DATA_CR_LINE_BUFFER_SIZE     320
+
+#define MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE     (128 / 8)
+#define MAX_SE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE     (128 / 8)
+#define MAX_SE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE     (128 / 8)
+
+#define MAX_PE_NBR_DATA_LCU64_LINE_BUFFER_SIZE     (64 * 2 * 3)
+#define MAX_PE_NBR_DATA_LCU32_LINE_BUFFER_SIZE     (32 * 2 * 3)
+#define MAX_PE_NBR_DATA_LCU16_LINE_BUFFER_SIZE     (16 * 2 * 3)
+
+#define MAX_TILE_COLUMNS 32     /* 8K/256 */
+
+#define VPP_CMD_MAX_SIZE (1 << 20)
+#define NUM_HW_PIC_BUF 32
+#define BIN_BUFFER_THRESHOLD (1280 * 736)
+#define H264D_MAX_SLICE 1800
+#define SIZE_H264D_BUFTAB_T  256 // sizeof(h264d_buftab_t) aligned to 256
+#define SIZE_H264D_HW_PIC_T (1 << 11) // sizeof(h264d_hw_pic_t) 32 aligned
+#define SIZE_H264D_BSE_CMD_PER_BUF (32 * 4)
+#define SIZE_H264D_VPP_CMD_PER_BUF 512
+
+// Line Buffer definitions
+/* one for luma and 1/2 for each chroma */
+#define SIZE_H264D_LB_FE_TOP_DATA(width, height) \
+	(MAX_FE_NBR_DATA_LUMA_LINE_BUFFER_SIZE * \
+	ALIGN(width, 16) * 3)
+
+#define SIZE_H264D_LB_FE_TOP_CTRL(width, height) \
+	(MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE * \
+	((width + 15) >> 4))
+
+#define SIZE_H264D_LB_FE_LEFT_CTRL(width, height) \
+	(MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE * \
+	((height + 15) >> 4))
+
+#define SIZE_H264D_LB_SE_TOP_CTRL(width, height) \
+	(MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE * \
+	((width + 15) >> 4))
+
+#define SIZE_H264D_LB_SE_LEFT_CTRL(width, height) \
+	(MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE * \
+	((height + 15) >> 4))
+
+#define SIZE_H264D_LB_PE_TOP_DATA(width, height) \
+	(MAX_PE_NBR_DATA_LCU64_LINE_BUFFER_SIZE * \
+	((width + 15) >> 4))
+
+#define SIZE_H264D_LB_VSP_TOP(width, height) \
+	((((width + 15) >> 4) << 7))
+
+#define SIZE_H264D_LB_RECON_DMA_METADATA_WR(width, height) \
+	(ALIGN(height, 16) * 32)
+
+#define SIZE_H264D_QP(width, height) \
+	(((width + 63) >> 6) * ((height + 63) >> 6) * 128)
+
+#define SIZE_HW_PIC(sizePerBuf) \
+	(NUM_HW_PIC_BUF * sizePerBuf)
+
+#define H264_CABAC_HDR_RATIO_HD_TOT 1
+#define H264_CABAC_RES_RATIO_HD_TOT 3
+
+/*
+ * some content need more bin buffer, but limit buffer
+ * size for high resolution
+ */
+#define NUM_SLIST_BUF_H264            (256 + 32)
+#define SIZE_SLIST_BUF_H264           512
+
+#define LCU_MAX_SIZE_PELS 64
+#define LCU_MIN_SIZE_PELS 16
+
+#define H265D_MAX_SLICE 3600
+#define SIZE_H265D_HW_PIC_T SIZE_H264D_HW_PIC_T
+#define SIZE_H265D_BSE_CMD_PER_BUF (16 * sizeof(u32))
+#define SIZE_H265D_VPP_CMD_PER_BUF 256
+
+#define SIZE_H265D_LB_FE_TOP_DATA(width, height) \
+	(MAX_FE_NBR_DATA_LUMA_LINE_BUFFER_SIZE * \
+	(ALIGN(width, 64) + 8) * 2)
+
+#define SIZE_H265D_LB_FE_TOP_CTRL(width, height) \
+	(MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE * \
+	(ALIGN(width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS))
+
+#define SIZE_H265D_LB_FE_LEFT_CTRL(width, height) \
+	(MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE * \
+	(ALIGN(height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS))
+
+#define SIZE_H265D_LB_SE_TOP_CTRL(width, height) \
+	((LCU_MAX_SIZE_PELS / 8 * (128 / 8)) * \
+	((width + 15) >> 4))
+
+#define SIZE_H265D_LB_SE_LEFT_CTRL(width, height) \
+	(max(((height + 16 - 1) / 8) * MAX_SE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE,\
+	max(((height + 32 - 1) / 8) * MAX_SE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE, \
+	((height + 64 - 1) / 8) * MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE)))
+
+#define SIZE_H265D_LB_PE_TOP_DATA(width, height) \
+	(MAX_PE_NBR_DATA_LCU64_LINE_BUFFER_SIZE * \
+	(ALIGN(width, LCU_MIN_SIZE_PELS) / LCU_MIN_SIZE_PELS))
+
+#define SIZE_H265D_LB_VSP_TOP(width, height) \
+	(((width + 63) >> 6) * 128)
+
+#define SIZE_H265D_LB_VSP_LEFT(width, height) \
+	(((height + 63) >> 6) * 128)
+
+#define SIZE_H265D_LB_RECON_DMA_METADATA_WR(width, height) \
+	SIZE_H264D_LB_RECON_DMA_METADATA_WR(width, height)
+
+#define SIZE_H265D_QP(width, height) SIZE_H264D_QP(width, height)
+
+#define H265_CABAC_HDR_RATIO_HD_TOT 2
+#define H265_CABAC_RES_RATIO_HD_TOT 2
+
+/*
+ * some content need more bin buffer, but limit buffer size
+ * for high resolution
+ */
+#define SIZE_SLIST_BUF_H265 (1 << 10)
+#define NUM_SLIST_BUF_H265 (80 + 20)
+#define H265_NUM_TILE_COL 32
+#define H265_NUM_TILE_ROW 128
+#define H265_NUM_TILE (H265_NUM_TILE_ROW * H265_NUM_TILE_COL + 1)
+
+#define SIZE_VPXD_LB_FE_LEFT_CTRL(width, height) \
+	max(((height + 15) >> 4) * MAX_FE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE, \
+	max(((height + 31) >> 5) * MAX_FE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE, \
+	((height + 63) >> 6) * MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE))
+#define SIZE_VPXD_LB_FE_TOP_CTRL(width, height) \
+	(((ALIGN(width, 64) + 8) * 10 * 2)) /* + small line */
+#define SIZE_VPXD_LB_SE_TOP_CTRL(width, height) \
+	(((width + 15) >> 4) * MAX_FE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE)
+#define SIZE_VPXD_LB_SE_LEFT_CTRL(width, height) \
+	max(((height + 15) >> 4) * MAX_SE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE, \
+	max(((height + 31) >> 5) * MAX_SE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE, \
+	((height + 63) >> 6) * MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE))
+#define SIZE_VPXD_LB_RECON_DMA_METADATA_WR(width, height) \
+	ALIGN((ALIGN(height, 16) / (4 / 2)) * 64, BUFFER_ALIGNMENT_SIZE(32))
+#define SIZE_VP8D_LB_FE_TOP_DATA(width, height) \
+	((ALIGN(width, 16) + 8) * 10 * 2)
+#define SIZE_VP9D_LB_FE_TOP_DATA(width, height) \
+	((ALIGN(ALIGN(width, 16), 64) + 8) * 10 * 2)
+#define SIZE_VP8D_LB_PE_TOP_DATA(width, height) \
+	((ALIGN(width, 16) >> 4) * 64)
+#define SIZE_VP9D_LB_PE_TOP_DATA(width, height) \
+	((ALIGN(ALIGN(width, 16), 64) >> 6) * 176)
+#define SIZE_VP8D_LB_VSP_TOP(width, height) \
+	(((ALIGN(width, 16) >> 4) * 64 / 2) + 256)
+#define SIZE_VP9D_LB_VSP_TOP(width, height) \
+	(((ALIGN(ALIGN(width, 16), 64) >> 6) * 64 * 8) + 256)
+
+
+#define HFI_IRIS2_VP9D_COMV_SIZE \
+	((((8192 + 63) >> 6) * ((4320 + 63) >> 6) * 8 * 8 * 2 * 8))
+
+#define VPX_DECODER_FRAME_CONCURENCY_LVL 2
+#define VPX_DECODER_FRAME_BIN_HDR_BUDGET_RATIO_NUM 1
+#define VPX_DECODER_FRAME_BIN_HDR_BUDGET_RATIO_DEN 2
+#define VPX_DECODER_FRAME_BIN_RES_BUDGET_RATIO_NUM 3
+#define VPX_DECODER_FRAME_BIN_RES_BUDGET_RATIO_DEN 2
+
+#define VP8_NUM_FRAME_INFO_BUF (5 + 1)
+#define VP9_NUM_FRAME_INFO_BUF (32)
+#define VP8_NUM_PROBABILITY_TABLE_BUF (VP8_NUM_FRAME_INFO_BUF)
+#define VP9_NUM_PROBABILITY_TABLE_BUF (VP9_NUM_FRAME_INFO_BUF + 4)
+#define VP8_PROB_TABLE_SIZE 3840
+#define VP9_PROB_TABLE_SIZE 3840
+
+#define VP9_UDC_HEADER_BUF_SIZE (3 * 128)
+#define MAX_SUPERFRAME_HEADER_LEN (34)
+#define CCE_TILE_OFFSET_SIZE ALIGN(32 * 4 * 4, BUFFER_ALIGNMENT_SIZE(32))
+
+#define QMATRIX_SIZE (sizeof(u32) * 128 + 256)
+#define MP2D_QPDUMP_SIZE 115200
+
+#define HFI_IRIS2_ENC_PERSIST_SIZE 204800
+
+#define HFI_MAX_COL_FRAME 6
+#define HFI_VENUS_VENC_TRE_WB_BUFF_SIZE (65 << 4) // bytes
+#define HFI_VENUS_VENC_DB_LINE_BUFF_PER_MB      512
+#define HFI_VENUS_VPPSG_MAX_REGISTERS  2048
+#define HFI_VENUS_WIDTH_ALIGNMENT 128
+#define HFI_VENUS_WIDTH_TEN_BIT_ALIGNMENT 192
+#define HFI_VENUS_HEIGHT_ALIGNMENT 32
+
+#define SYSTEM_LAL_TILE10 192
+#define HDR10_HIST_EXTRADATA_SIZE 4096
+
+static u32 size_vpss_lb(u32 width, u32 height, u32 num_vpp_pipes)
+{
+	u32 vpss_4tap_top_buffer_size, vpss_div2_top_buffer_size;
+	u32 vpss_4tap_left_buffer_size, vpss_div2_left_buffer_size;
+	u32 opb_wr_top_line_luma_buf_size, opb_wr_top_line_chroma_buf_size;
+	u32 opb_lb_wr_llb_y_buffer_size, opb_lb_wr_llb_uv_buffer_size;
+	u32 macrotiling_size;
+	u32 size = 0;
+
+	vpss_4tap_top_buffer_size = vpss_div2_top_buffer_size =
+		vpss_4tap_left_buffer_size = vpss_div2_left_buffer_size = 0;
+	macrotiling_size = 32;
+	opb_wr_top_line_luma_buf_size = ALIGN(width, macrotiling_size) /
+		macrotiling_size * 256;
+	opb_wr_top_line_luma_buf_size = ALIGN(opb_wr_top_line_luma_buf_size,
+		VENUS_DMA_ALIGNMENT) + (MAX_TILE_COLUMNS - 1) * 256;
+	opb_wr_top_line_luma_buf_size = max(opb_wr_top_line_luma_buf_size,
+		(32 * ALIGN(height, 16)));
+	opb_wr_top_line_chroma_buf_size = opb_wr_top_line_luma_buf_size;
+	opb_lb_wr_llb_uv_buffer_size = opb_lb_wr_llb_y_buffer_size =
+		ALIGN((ALIGN(height, 16) / 2) *
+			64, BUFFER_ALIGNMENT_SIZE(32));
+	size = num_vpp_pipes * 2 * (vpss_4tap_top_buffer_size +
+		vpss_div2_top_buffer_size) +
+		2 * (vpss_4tap_left_buffer_size +
+		vpss_div2_left_buffer_size) +
+	opb_wr_top_line_luma_buf_size +
+	opb_wr_top_line_chroma_buf_size +
+	opb_lb_wr_llb_uv_buffer_size +
+	opb_lb_wr_llb_y_buffer_size;
+
+	return size;
+}
+
+static u32 hfi_iris2_h264d_comv_size(u32 width, u32 height,
+	u32 yuv_buf_min_count)
+{
+	u32 comv_size = 0;
+	u32 frame_width_in_mbs = ((width + 15) >> 4);
+	u32 frame_height_in_mbs = ((height + 15) >> 4);
+	u32 col_mv_aligned_width = (frame_width_in_mbs << 7);
+	u32 col_zero_aligned_width = (frame_width_in_mbs << 2);
+	u32 col_zero_size = 0, size_colloc = 0;
+
+	col_mv_aligned_width = ALIGN(col_mv_aligned_width,
+		BUFFER_ALIGNMENT_SIZE(16));
+	col_zero_aligned_width = ALIGN(col_zero_aligned_width,
+		BUFFER_ALIGNMENT_SIZE(16));
+	col_zero_size = col_zero_aligned_width *
+		((frame_height_in_mbs + 1) >> 1);
+	col_zero_size = ALIGN(col_zero_size, BUFFER_ALIGNMENT_SIZE(64));
+	col_zero_size <<= 1;
+	col_zero_size = ALIGN(col_zero_size, BUFFER_ALIGNMENT_SIZE(512));
+	size_colloc = col_mv_aligned_width  * ((frame_height_in_mbs + 1) >> 1);
+	size_colloc = ALIGN(size_colloc, BUFFER_ALIGNMENT_SIZE(64));
+	size_colloc <<= 1;
+	size_colloc = ALIGN(size_colloc, BUFFER_ALIGNMENT_SIZE(512));
+	size_colloc += (col_zero_size + SIZE_H264D_BUFTAB_T * 2);
+	comv_size = size_colloc * yuv_buf_min_count;
+	comv_size += BUFFER_ALIGNMENT_SIZE(512);
+
+	return comv_size;
+}
+
+static u32 size_h264d_bse_cmd_buf(u32 height)
+{
+	u32 aligned_height = ALIGN(height, BUFFER_ALIGNMENT_SIZE(32));
+
+	return min_t(u32, (((aligned_height + 15) >> 4) * 3 * 4),
+		H264D_MAX_SLICE) *
+		SIZE_H264D_BSE_CMD_PER_BUF;
+}
+
+static u32 size_h264d_vpp_cmd_buf(u32 height)
+{
+	u32 size = 0;
+	u32 aligned_height = ALIGN(height, BUFFER_ALIGNMENT_SIZE(32));
+
+	size = min_t(u32, (((aligned_height + 15) >> 4) * 3 * 4),
+		H264D_MAX_SLICE) *
+		SIZE_H264D_VPP_CMD_PER_BUF;
+	if (size > VPP_CMD_MAX_SIZE)
+		size = VPP_CMD_MAX_SIZE;
+	return size;
+}
+
+static u32 hfi_iris2_h264d_non_comv_size(u32 width, u32 height,
+	u32 num_vpp_pipes)
+{
+	u32 size;
+	u32 size_bse, size_vpp;
+
+	size_bse = size_h264d_bse_cmd_buf(height);
+	size_vpp = size_h264d_vpp_cmd_buf(height);
+	size = ALIGN(size_bse, VENUS_DMA_ALIGNMENT) +
+		ALIGN(size_vpp, VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_HW_PIC(SIZE_H264D_HW_PIC_T), VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H264D_LB_FE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H264D_LB_FE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H264D_LB_FE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_H264D_LB_SE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H264D_LB_SE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_H264D_LB_PE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H264D_LB_VSP_TOP(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H264D_LB_RECON_DMA_METADATA_WR(width, height),
+			VENUS_DMA_ALIGNMENT) * 2 +
+		ALIGN(SIZE_H264D_QP(width, height), VENUS_DMA_ALIGNMENT);
+	size = ALIGN(size, VENUS_DMA_ALIGNMENT);
+	return size;
+}
+
+static u32 size_h264d_hw_bin_buffer(u32 width, u32 height, u32 delay,
+	u32 num_vpp_pipes)
+{
+	u32 size_yuv, size_bin_hdr, size_bin_res;
+	u32 size = 0;
+	u32 product;
+
+	product = width * height;
+	size_yuv = (product <= BIN_BUFFER_THRESHOLD) ?
+			((BIN_BUFFER_THRESHOLD * 3) >> 1) :
+			((product * 3) >> 1);
+
+	size_bin_hdr = size_yuv * H264_CABAC_HDR_RATIO_HD_TOT;
+	size_bin_res = size_yuv * H264_CABAC_RES_RATIO_HD_TOT;
+	size_bin_hdr = size_bin_hdr * (((((u32)(delay)) & 31) / 10) + 2) / 2;
+	size_bin_res = size_bin_res * (((((u32)(delay)) & 31) / 10) + 2) / 2;
+	size_bin_hdr = ALIGN(size_bin_hdr / num_vpp_pipes,
+		VENUS_DMA_ALIGNMENT) * num_vpp_pipes;
+	size_bin_res = ALIGN(size_bin_res / num_vpp_pipes,
+		VENUS_DMA_ALIGNMENT) * num_vpp_pipes;
+	size = size_bin_hdr + size_bin_res;
+	return size;
+}
+
+static u32 calculate_h264d_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, bool is_interlaced, u32 delay, u32 num_vpp_pipes)
+{
+	u32 aligned_width = ALIGN(width, BUFFER_ALIGNMENT_SIZE(16));
+	u32 aligned_height = ALIGN(height, BUFFER_ALIGNMENT_SIZE(16));
+	u32 size = 0;
+
+	if (!is_interlaced)
+		size = size_h264d_hw_bin_buffer(aligned_width, aligned_height,
+						delay, num_vpp_pipes);
+	else
+		size = 0;
+
+	return size;
+}
+
+static u32 size_h265d_bse_cmd_buf(u32 width, u32 height)
+{
+	u32 size;
+
+	size = (ALIGN(width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
+		(ALIGN(height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
+		NUM_HW_PIC_BUF;
+	size = min_t(u32, size, H265D_MAX_SLICE + 1);
+	size = 2 * size * SIZE_H265D_BSE_CMD_PER_BUF;
+	size = ALIGN(size, VENUS_DMA_ALIGNMENT);
+
+	return size;
+}
+
+static u32 size_h265d_vpp_cmd_buf(u32 width, u32 height)
+{
+	u32 size = 0;
+
+	size = (ALIGN(width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
+		(ALIGN(height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
+		NUM_HW_PIC_BUF;
+	size = min_t(u32, size, H265D_MAX_SLICE + 1);
+	size = ALIGN(size, 4);
+	size = 2 * size * SIZE_H265D_VPP_CMD_PER_BUF;
+	size = ALIGN(size, VENUS_DMA_ALIGNMENT);
+	if (size > VPP_CMD_MAX_SIZE)
+		size = VPP_CMD_MAX_SIZE;
+	return size;
+}
+
+static u32 hfi_iris2_h265d_comv_size(u32 width, u32 height,
+	u32 yuv_buf_count_min)
+{
+	u32 size = 0;
+
+	size = ALIGN(((((width + 15) >> 4) * ((height + 15) >> 4)) << 8),
+		BUFFER_ALIGNMENT_SIZE(512));
+	size *= yuv_buf_count_min;
+	size += BUFFER_ALIGNMENT_SIZE(512);
+
+	return size;
+}
+
+static u32 hfi_iris2_h265d_non_comv_size(u32 width, u32 height,
+	u32 num_vpp_pipes)
+{
+	u32 size_bse, size_vpp;
+	u32 size = 0;
+
+	size_bse = size_h265d_bse_cmd_buf(width, height);
+	size_vpp = size_h265d_vpp_cmd_buf(width, height);
+	size = ALIGN(size_bse, VENUS_DMA_ALIGNMENT) +
+		ALIGN(size_vpp, VENUS_DMA_ALIGNMENT) +
+		ALIGN(NUM_HW_PIC_BUF * 20 * 22 * 4, VENUS_DMA_ALIGNMENT) +
+		ALIGN(2 * sizeof(u16) *
+		(ALIGN(width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
+			(ALIGN(height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_HW_PIC(SIZE_H265D_HW_PIC_T), VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H265D_LB_FE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H265D_LB_FE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H265D_LB_FE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_H265D_LB_SE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_H265D_LB_SE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H265D_LB_PE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H265D_LB_VSP_TOP(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_H265D_LB_VSP_LEFT(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_H265D_LB_RECON_DMA_METADATA_WR(width, height),
+			VENUS_DMA_ALIGNMENT) * 4 +
+		ALIGN(SIZE_H265D_QP(width, height), VENUS_DMA_ALIGNMENT);
+	size = ALIGN(size, VENUS_DMA_ALIGNMENT);
+	return size;
+}
+
+static u32 size_h265d_hw_bin_buffer(u32 width, u32 height, u32 delay,
+	u32 num_vpp_pipes)
+{
+	u32 size = 0;
+	u32 size_yuv, size_bin_hdr, size_bin_res;
+	u32 product;
+
+	product = width * height;
+	size_yuv = (product <= BIN_BUFFER_THRESHOLD) ?
+		((BIN_BUFFER_THRESHOLD * 3) >> 1) :
+		((product * 3) >> 1);
+	size_bin_hdr = size_yuv * H265_CABAC_HDR_RATIO_HD_TOT;
+	size_bin_res = size_yuv * H265_CABAC_RES_RATIO_HD_TOT;
+	size_bin_hdr = size_bin_hdr * (((((u32)(delay)) & 31) / 10) + 2) / 2;
+	size_bin_res = size_bin_res * (((((u32)(delay)) & 31) / 10) + 2) / 2;
+	size_bin_hdr = ALIGN(size_bin_hdr / num_vpp_pipes,
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes;
+	size_bin_res = ALIGN(size_bin_res / num_vpp_pipes,
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes;
+	size = size_bin_hdr + size_bin_res;
+
+	return size;
+}
+
+static u32 calculate_h265d_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, bool is_interlaced, u32 delay, u32 num_vpp_pipes)
+{
+	u32 aligned_width = ALIGN(width, BUFFER_ALIGNMENT_SIZE(16));
+	u32 aligned_height = ALIGN(height, BUFFER_ALIGNMENT_SIZE(16));
+	u32 size = 0;
+
+	if (!is_interlaced)
+		size = size_h265d_hw_bin_buffer(aligned_width, aligned_height,
+						delay, num_vpp_pipes);
+	else
+		size = 0;
+
+	return size;
+}
+
+static u32 calculate_vpxd_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, bool is_interlaced, u32 delay, u32 num_vpp_pipes)
+{
+	u32 aligned_width = ALIGN(width, BUFFER_ALIGNMENT_SIZE(16));
+	u32 aligned_height = ALIGN(height, BUFFER_ALIGNMENT_SIZE(16));
+	u32 size = 0;
+	u32 size_yuv = aligned_width * aligned_height * 3 / 2;
+
+	if (!is_interlaced) {
+		/* binbuffer1_size + binbufer2_size */
+		u32 binbuffer1_size = 0, binbuffer2_size = 0;
+
+		binbuffer1_size = ALIGN(max_t(u32, size_yuv,
+			((BIN_BUFFER_THRESHOLD * 3) >> 1)) *
+			VPX_DECODER_FRAME_CONCURENCY_LVL *
+			VPX_DECODER_FRAME_BIN_HDR_BUDGET_RATIO_NUM /
+			VPX_DECODER_FRAME_BIN_HDR_BUDGET_RATIO_DEN,
+			VENUS_DMA_ALIGNMENT);
+		binbuffer2_size = ALIGN(max_t(u32, size_yuv,
+			((BIN_BUFFER_THRESHOLD * 3) >> 1)) *
+			VPX_DECODER_FRAME_CONCURENCY_LVL *
+			VPX_DECODER_FRAME_BIN_RES_BUDGET_RATIO_NUM /
+			VPX_DECODER_FRAME_BIN_RES_BUDGET_RATIO_DEN,
+			VENUS_DMA_ALIGNMENT);
+		size = binbuffer1_size + binbuffer2_size;
+		size = size * num_vpp_pipes;
+	} else {
+		size = 0;
+	}
+
+	return size;
+}
+
+static u32 calculate_mpeg2d_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, bool is_interlaced, u32 delay, u32 num_vpp_pipes)
+{
+	return 0;
+}
+
+static u32 calculate_enc_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 work_mode, u32 lcu_size, u32 num_vpp_pipes)
+{
+	u32 aligned_width, aligned_height, bitstream_size;
+	u32 total_bitbin_buffers = 0, size_singlePipe, bitbin_size = 0;
+	u32 sao_bin_buffer_size, padded_bin_size, size = 0;
+
+	aligned_width = ALIGN(width, lcu_size);
+	aligned_height = ALIGN(height, lcu_size);
+	bitstream_size = msm_vidc_encoder_output_size(inst);
+
+	bitstream_size = ALIGN(bitstream_size, VENUS_DMA_ALIGNMENT);
+	if (1) { // TODO: work_mode == HFI_WORKMODE_2) {
+		total_bitbin_buffers = 3;
+		bitbin_size = bitstream_size * 17 / 10;
+		bitbin_size = ALIGN(bitbin_size, VENUS_DMA_ALIGNMENT);
+	} else {
+		total_bitbin_buffers = 1;
+		bitstream_size = aligned_width * aligned_height * 3;
+		bitbin_size = ALIGN(bitstream_size, VENUS_DMA_ALIGNMENT);
+	}
+	if (num_vpp_pipes > 2)
+		size_singlePipe = bitbin_size / 2;
+	else
+		size_singlePipe = bitbin_size;
+	// TODO
+	//if (inst->rc_type == RATE_CONTROL_LOSSLESS)
+	//	size_singlePipe <<= 1;
+	size_singlePipe = ALIGN(size_singlePipe, VENUS_DMA_ALIGNMENT);
+	sao_bin_buffer_size = (64 * (((width + BUFFER_ALIGNMENT_SIZE(32)) *
+		(height + BUFFER_ALIGNMENT_SIZE(32))) >> 10)) + 384;
+	padded_bin_size = ALIGN(size_singlePipe, VENUS_DMA_ALIGNMENT);
+	size_singlePipe = sao_bin_buffer_size + padded_bin_size;
+	size_singlePipe = ALIGN(size_singlePipe, VENUS_DMA_ALIGNMENT);
+	bitbin_size = size_singlePipe * num_vpp_pipes;
+	size = ALIGN(bitbin_size, VENUS_DMA_ALIGNMENT) * total_bitbin_buffers
+			+ 512;
+
+	return size;
+}
+
+static u32 calculate_h264e_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 work_mode, u32 num_vpp_pipes)
+{
+	return calculate_enc_scratch_size(inst, width, height, work_mode, 16,
+		num_vpp_pipes);
+}
+
+static u32 calculate_h265e_scratch_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 work_mode, u32 num_vpp_pipes)
+{
+	return calculate_enc_scratch_size(inst, width, height, work_mode, 32,
+		num_vpp_pipes);
+}
+
+static u32 calculate_h264d_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 min_buf_count,
+	bool split_mode_enabled, u32 num_vpp_pipes)
+{
+	u32 co_mv_size = 0, nonco_mv_size = 0;
+	u32 vpss_lb_size = 0;
+	u32 size = 0;
+
+	co_mv_size = hfi_iris2_h264d_comv_size(width, height, min_buf_count);
+	nonco_mv_size = hfi_iris2_h264d_non_comv_size(width, height,
+			num_vpp_pipes);
+	if (split_mode_enabled)
+		vpss_lb_size = size_vpss_lb(width, height, num_vpp_pipes);
+	size = co_mv_size + nonco_mv_size + vpss_lb_size;
+	return size;
+}
+
+static u32 calculate_h265d_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 min_buf_count,
+	bool split_mode_enabled, u32 num_vpp_pipes)
+{
+	u32 co_mv_size = 0, nonco_mv_size = 0;
+	u32 vpss_lb_size = 0;
+	u32 size = 0;
+
+	co_mv_size = hfi_iris2_h265d_comv_size(width, height, min_buf_count);
+	nonco_mv_size =
+		hfi_iris2_h265d_non_comv_size(width, height, num_vpp_pipes);
+	if (split_mode_enabled)
+		vpss_lb_size = size_vpss_lb(width, height, num_vpp_pipes);
+
+	size = co_mv_size + nonco_mv_size + vpss_lb_size +
+			HDR10_HIST_EXTRADATA_SIZE;
+	return size;
+}
+
+static u32 calculate_vp9d_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 min_buf_count,
+	bool split_mode_enabled, u32 num_vpp_pipes)
+{
+	u32 vpss_lb_size = 0;
+	u32 size = 0;
+
+	size = ALIGN(SIZE_VPXD_LB_FE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_VPXD_LB_SE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_VP9D_LB_VSP_TOP(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VPXD_LB_FE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		2 * ALIGN(SIZE_VPXD_LB_RECON_DMA_METADATA_WR(width, height),
+				VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VPXD_LB_SE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VP9D_LB_PE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VP9D_LB_FE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT);
+	if (split_mode_enabled)
+		vpss_lb_size = size_vpss_lb(width, height, num_vpp_pipes);
+
+	size += vpss_lb_size + HDR10_HIST_EXTRADATA_SIZE;
+	return size;
+}
+
+static u32 calculate_mpeg2d_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 min_buf_count,
+	bool split_mode_enabled, u32 num_vpp_pipes)
+{
+	u32 vpss_lb_size = 0;
+	u32 size = 0;
+
+	size = ALIGN(SIZE_VPXD_LB_FE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_VPXD_LB_SE_LEFT_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes +
+		ALIGN(SIZE_VP8D_LB_VSP_TOP(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VPXD_LB_FE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		2 * ALIGN(SIZE_VPXD_LB_RECON_DMA_METADATA_WR(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VPXD_LB_SE_TOP_CTRL(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VP8D_LB_PE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT) +
+		ALIGN(SIZE_VP8D_LB_FE_TOP_DATA(width, height),
+			VENUS_DMA_ALIGNMENT);
+	if (split_mode_enabled)
+		vpss_lb_size = size_vpss_lb(width, height, num_vpp_pipes);
+
+	size += vpss_lb_size;
+	return size;
+}
+
+static u32 calculate_enc_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 lcu_size, u32 num_ref, bool ten_bit,
+	u32 num_vpp_pipes, bool is_h265)
+{
+	u32 line_buf_ctrl_size, line_buf_data_size, leftline_buf_ctrl_size;
+	u32 line_buf_sde_size, sps_pps_slice_hdr, topline_buf_ctrl_size_FE;
+	u32 leftline_buf_ctrl_size_FE, line_buf_recon_pix_size;
+	u32 leftline_buf_recon_pix_size, lambda_lut_size, override_buffer_size;
+	u32 col_mv_buf_size, vpp_reg_buffer_size, ir_buffer_size;
+	u32 vpss_line_buf, leftline_buf_meta_recony, col_rc_buf_size;
+	u32 h265e_framerc_bufsize, h265e_lcubitcnt_bufsize;
+	u32 h265e_lcubitmap_bufsize, se_stats_bufsize;
+	u32 bse_reg_buffer_size, bse_slice_cmd_buffer_size, slice_info_bufsize;
+	u32 line_buf_ctrl_size_buffid2, slice_cmd_buffer_size;
+	u32 width_lcu_num, height_lcu_num, width_coded, height_coded;
+	u32 frame_num_lcu, linebuf_meta_recon_uv, topline_bufsize_fe_1stg_sao;
+	u32 output_mv_bufsize = 0, temp_scratch_mv_bufsize = 0;
+	u32 size, bit_depth, num_LCUMB;
+	u32 vpss_lineBufferSize_1 = 0;
+	u32 width_mb_num = ((width + 15) >> 4);
+	u32 height_mb_num = ((height + 15) >> 4);
+
+	width_lcu_num = ((width)+(lcu_size)-1) / (lcu_size);
+	height_lcu_num = ((height)+(lcu_size)-1) / (lcu_size);
+	frame_num_lcu = width_lcu_num * height_lcu_num;
+	width_coded = width_lcu_num * (lcu_size);
+	height_coded = height_lcu_num * (lcu_size);
+	num_LCUMB = (height_coded / lcu_size) * ((width_coded + lcu_size * 8) / lcu_size);
+	slice_info_bufsize = (256 + (frame_num_lcu << 4));
+	slice_info_bufsize = ALIGN(slice_info_bufsize, VENUS_DMA_ALIGNMENT);
+	line_buf_ctrl_size = ALIGN(width_coded, VENUS_DMA_ALIGNMENT);
+	line_buf_ctrl_size_buffid2 = ALIGN(width_coded, VENUS_DMA_ALIGNMENT);
+
+	bit_depth = ten_bit ? 10 : 8;
+	line_buf_data_size = (((((bit_depth * width_coded + 1024) +
+	(VENUS_DMA_ALIGNMENT - 1)) & (~(VENUS_DMA_ALIGNMENT - 1))) * 1) +
+	(((((bit_depth * width_coded + 1024) >> 1) +
+	(VENUS_DMA_ALIGNMENT - 1)) &
+	(~(VENUS_DMA_ALIGNMENT - 1))) * 2));
+	leftline_buf_ctrl_size = (is_h265) ?
+		((height_coded + (BUFFER_ALIGNMENT_SIZE(32))) /
+		BUFFER_ALIGNMENT_SIZE(32) * 4 * 16) :
+		((height_coded + 15) / 16 * 5 * 16);
+	if (num_vpp_pipes > 1) {
+		leftline_buf_ctrl_size += BUFFER_ALIGNMENT_SIZE(512);
+		leftline_buf_ctrl_size = ALIGN(leftline_buf_ctrl_size,
+			BUFFER_ALIGNMENT_SIZE(512)) * num_vpp_pipes;
+	}
+	leftline_buf_ctrl_size = ALIGN(leftline_buf_ctrl_size,
+		VENUS_DMA_ALIGNMENT);
+	leftline_buf_recon_pix_size = (((ten_bit + 1) * 2 *
+		(height_coded)+VENUS_DMA_ALIGNMENT) +
+	(VENUS_DMA_ALIGNMENT << (num_vpp_pipes - 1)) - 1) &
+		(~((VENUS_DMA_ALIGNMENT << (num_vpp_pipes - 1)) - 1)) * 1;
+	topline_buf_ctrl_size_FE = (is_h265) ? (64 * (width_coded >> 5)) :
+		(VENUS_DMA_ALIGNMENT + 16 * (width_coded >> 4));
+	topline_buf_ctrl_size_FE = ALIGN(topline_buf_ctrl_size_FE,
+		VENUS_DMA_ALIGNMENT);
+	leftline_buf_ctrl_size_FE = (((VENUS_DMA_ALIGNMENT + 64 *
+		(height_coded >> 4)) +
+		(VENUS_DMA_ALIGNMENT << (num_vpp_pipes - 1)) - 1) &
+		(~((VENUS_DMA_ALIGNMENT << (num_vpp_pipes - 1)) - 1)) * 1) *
+		num_vpp_pipes;
+	leftline_buf_meta_recony = (VENUS_DMA_ALIGNMENT + 64 *
+		((height_coded) / (8 * (ten_bit ? 4 : 8))));
+	leftline_buf_meta_recony = ALIGN(leftline_buf_meta_recony,
+		VENUS_DMA_ALIGNMENT);
+	leftline_buf_meta_recony = leftline_buf_meta_recony *
+		num_vpp_pipes;
+	linebuf_meta_recon_uv = (VENUS_DMA_ALIGNMENT + 64 *
+		((height_coded) / (4 * (ten_bit ? 4 : 8))));
+	linebuf_meta_recon_uv = ALIGN(linebuf_meta_recon_uv,
+		VENUS_DMA_ALIGNMENT);
+	linebuf_meta_recon_uv = linebuf_meta_recon_uv *
+		num_vpp_pipes;
+	line_buf_recon_pix_size = ((ten_bit ? 3 : 2) * width_coded);
+	line_buf_recon_pix_size = ALIGN(line_buf_recon_pix_size,
+		VENUS_DMA_ALIGNMENT);
+	slice_cmd_buffer_size = ALIGN(20480, VENUS_DMA_ALIGNMENT);
+	sps_pps_slice_hdr = 2048 + 4096;
+	col_mv_buf_size = (is_h265) ? (16 * ((frame_num_lcu << 2) +
+		BUFFER_ALIGNMENT_SIZE(32))) :
+		(3 * 16 * (width_lcu_num * height_lcu_num +
+		BUFFER_ALIGNMENT_SIZE(32)));
+	col_mv_buf_size = ALIGN(col_mv_buf_size, VENUS_DMA_ALIGNMENT)
+		* (num_ref + 1);
+	col_rc_buf_size = (((width_mb_num + 7) >> 3) *
+		16 * 2 * height_mb_num);
+	col_rc_buf_size = ALIGN(col_rc_buf_size,
+		VENUS_DMA_ALIGNMENT) * HFI_MAX_COL_FRAME;
+	h265e_framerc_bufsize = (is_h265) ? (256 + 16 *
+		(14 + (((height_coded >> 5) + 7) >> 3))) :
+		(256 + 16 * (14 + (((height_coded >> 4) + 7) >> 3)));
+	h265e_framerc_bufsize *= 6;   /* multiply by max numtilescol*/
+	if (num_vpp_pipes > 1)
+		h265e_framerc_bufsize = ALIGN(h265e_framerc_bufsize,
+			VENUS_DMA_ALIGNMENT) * num_vpp_pipes;
+
+	h265e_framerc_bufsize = ALIGN(h265e_framerc_bufsize,
+		BUFFER_ALIGNMENT_SIZE(512)) * HFI_MAX_COL_FRAME;
+	h265e_lcubitcnt_bufsize = (256 + 4 * frame_num_lcu);
+	h265e_lcubitcnt_bufsize = ALIGN(h265e_lcubitcnt_bufsize,
+		VENUS_DMA_ALIGNMENT);
+	h265e_lcubitmap_bufsize = 256 + (frame_num_lcu >> 3);
+	h265e_lcubitmap_bufsize = ALIGN(h265e_lcubitmap_bufsize,
+		VENUS_DMA_ALIGNMENT);
+	line_buf_sde_size = 256 + 16 * (width_coded >> 4);
+	line_buf_sde_size = ALIGN(line_buf_sde_size, VENUS_DMA_ALIGNMENT);
+	if ((width_coded * height_coded) > (4096 * 2160))
+		se_stats_bufsize = 0;
+	else if ((width_coded * height_coded) > (1920 * 1088))
+		se_stats_bufsize = (40 * 4 * frame_num_lcu + 256 + 256);
+	else
+		se_stats_bufsize = (1024 * frame_num_lcu + 256 + 256);
+
+	se_stats_bufsize = ALIGN(se_stats_bufsize, VENUS_DMA_ALIGNMENT) * 2;
+	bse_slice_cmd_buffer_size = ((((8192 << 2) + 7) & (~7)) * 6);
+	bse_reg_buffer_size = ((((512 << 3) + 7) & (~7)) * 4);
+	vpp_reg_buffer_size = ((((HFI_VENUS_VPPSG_MAX_REGISTERS << 3) + 31) &
+		(~31)) * 10);
+	lambda_lut_size = (256 * 11);
+	override_buffer_size = 16 * ((num_LCUMB + 7) >> 3);
+	override_buffer_size = ALIGN(override_buffer_size,
+		VENUS_DMA_ALIGNMENT) * 2;
+	ir_buffer_size = (((frame_num_lcu << 1) + 7) & (~7)) * 3;
+	vpss_lineBufferSize_1 = ((((8192) >> 2) << 5) * num_vpp_pipes) + 64;
+	vpss_line_buf = (((((max(width_coded, height_coded) + 3) >> 2) << 5)
+		 + 256) * 16) + vpss_lineBufferSize_1;
+	topline_bufsize_fe_1stg_sao = (16 * (width_coded >> 5));
+	topline_bufsize_fe_1stg_sao = ALIGN(topline_bufsize_fe_1stg_sao,
+		VENUS_DMA_ALIGNMENT);
+	size = line_buf_ctrl_size + line_buf_data_size +
+		line_buf_ctrl_size_buffid2 + leftline_buf_ctrl_size +
+		vpss_line_buf + col_mv_buf_size + topline_buf_ctrl_size_FE +
+		leftline_buf_ctrl_size_FE + line_buf_recon_pix_size +
+		leftline_buf_recon_pix_size + leftline_buf_meta_recony +
+		linebuf_meta_recon_uv + col_rc_buf_size +
+		h265e_framerc_bufsize + h265e_lcubitcnt_bufsize +
+		h265e_lcubitmap_bufsize + line_buf_sde_size +
+		topline_bufsize_fe_1stg_sao + override_buffer_size +
+		bse_reg_buffer_size + vpp_reg_buffer_size +
+		sps_pps_slice_hdr + slice_cmd_buffer_size +
+		bse_slice_cmd_buffer_size + ir_buffer_size + slice_info_bufsize
+		+ lambda_lut_size + se_stats_bufsize + temp_scratch_mv_bufsize
+		+ output_mv_bufsize + 1024;
+	return size;
+}
+
+static u32 calculate_h264e_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 num_ref, bool ten_bit, u32 num_vpp_pipes)
+{
+	return calculate_enc_scratch1_size(inst, width, height, 16,
+		num_ref, ten_bit, num_vpp_pipes, false);
+}
+
+static u32 calculate_h265e_scratch1_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 num_ref, bool ten_bit, u32 num_vpp_pipes)
+{
+	return calculate_enc_scratch1_size(inst, width, height, 32,
+		num_ref, ten_bit, num_vpp_pipes, true);
+}
+
+static u32 hfi_ubwc_calc_metadata_plane_stride(u32 width,
+	u32 metadata_stride_multi, u32 tile_width_pels)
+{
+	return ALIGN(((width + (tile_width_pels - 1)) / tile_width_pels),
+		metadata_stride_multi);
+}
+
+static u32 hfi_ubwc_metadata_plane_bufheight(u32 height,
+	u32 metadata_height_multi, u32 tile_height_pels)
+{
+	return ALIGN(((height + (tile_height_pels - 1)) / tile_height_pels),
+		metadata_height_multi);
+}
+
+static u32 hfi_ubwc_metadata_plane_buffer_size(u32 metadata_stride,
+	u32 metadata_buf_height)
+{
+	return ALIGN(metadata_stride * metadata_buf_height,
+		BUFFER_ALIGNMENT_4096_BYTES);
+}
+
+static u32 calculate_enc_scratch2_size(struct msm_vidc_inst *inst,
+	u32 width, u32 height, u32 num_ref, bool ten_bit)
+{
+	u32 aligned_width, aligned_height, chroma_height, ref_buf_height;
+	u32 luma_size, chroma_size;
+	u32 metadata_stride, meta_buf_height, meta_size_y, meta_size_c;
+	u32 ref_luma_stride_bytes, ref_chroma_height_bytes;
+	u32 ref_buf_size = 0, ref_stride;
+	u32 size;
+
+	if (!ten_bit) {
+		aligned_height = ALIGN(height, HFI_VENUS_HEIGHT_ALIGNMENT);
+		chroma_height = height >> 1;
+		chroma_height = ALIGN(chroma_height,
+			HFI_VENUS_HEIGHT_ALIGNMENT);
+		aligned_width = ALIGN(width, HFI_VENUS_WIDTH_ALIGNMENT);
+		metadata_stride = hfi_ubwc_calc_metadata_plane_stride(width,
+			64, HFI_COLOR_FORMAT_YUV420_NV12_UBWC_Y_TILE_WIDTH);
+		meta_buf_height = hfi_ubwc_metadata_plane_bufheight(height,
+			16, HFI_COLOR_FORMAT_YUV420_NV12_UBWC_Y_TILE_HEIGHT);
+		meta_size_y = hfi_ubwc_metadata_plane_buffer_size(
+			metadata_stride, meta_buf_height);
+		meta_size_c = hfi_ubwc_metadata_plane_buffer_size(
+			metadata_stride, meta_buf_height);
+		size = (aligned_height + chroma_height) * aligned_width +
+			meta_size_y + meta_size_c;
+		size = (size * (num_ref+3)) + 4096;
+	} else {
+		ref_buf_height = (height + (HFI_VENUS_HEIGHT_ALIGNMENT - 1))
+			& (~(HFI_VENUS_HEIGHT_ALIGNMENT - 1));
+		ref_luma_stride_bytes = ((width + SYSTEM_LAL_TILE10 - 1) /
+			SYSTEM_LAL_TILE10) * SYSTEM_LAL_TILE10;
+		ref_stride = 4 * (ref_luma_stride_bytes / 3);
+		ref_stride = (ref_stride + (BUFFER_ALIGNMENT_SIZE(128) - 1)) &
+			(~(BUFFER_ALIGNMENT_SIZE(128) - 1));
+		luma_size = ref_buf_height * ref_stride;
+		ref_chroma_height_bytes = (((height + 1) >> 1) +
+			(BUFFER_ALIGNMENT_SIZE(32) - 1)) &
+			(~(BUFFER_ALIGNMENT_SIZE(32) - 1));
+		chroma_size = ref_stride * ref_chroma_height_bytes;
+		luma_size = (luma_size + (BUFFER_ALIGNMENT_4096_BYTES - 1)) &
+			(~(BUFFER_ALIGNMENT_4096_BYTES - 1));
+		chroma_size = (chroma_size +
+			(BUFFER_ALIGNMENT_4096_BYTES - 1)) &
+			(~(BUFFER_ALIGNMENT_4096_BYTES - 1));
+		ref_buf_size = luma_size + chroma_size;
+		metadata_stride = hfi_ubwc_calc_metadata_plane_stride(
+			width,
+			VENUS_METADATA_STRIDE_MULTIPLE,
+			HFI_COLOR_FORMAT_YUV420_TP10_UBWC_Y_TILE_WIDTH);
+		meta_buf_height = hfi_ubwc_metadata_plane_bufheight(
+			height,
+			VENUS_METADATA_HEIGHT_MULTIPLE,
+			HFI_COLOR_FORMAT_YUV420_TP10_UBWC_Y_TILE_HEIGHT);
+		meta_size_y = hfi_ubwc_metadata_plane_buffer_size(
+			metadata_stride, meta_buf_height);
+		meta_size_c = hfi_ubwc_metadata_plane_buffer_size(
+			metadata_stride, meta_buf_height);
+		size = ref_buf_size + meta_size_y + meta_size_c;
+		size = (size * (num_ref+3)) + 4096;
+	}
+	return size;
+}
+
+static u32 calculate_enc_persist_size(void)
+{
+	return HFI_IRIS2_ENC_PERSIST_SIZE;
+}
+
+static u32 calculate_h264d_persist1_size(void)
+{
+	u32 size = 0;
+
+	size = ALIGN((SIZE_SLIST_BUF_H264 * NUM_SLIST_BUF_H264),
+			VENUS_DMA_ALIGNMENT);
+	return size;
+}
+
+static u32 calculate_h265d_persist1_size(void)
+{
+	u32 size = 0;
+
+	size = ALIGN((SIZE_SLIST_BUF_H265 * NUM_SLIST_BUF_H265 + H265_NUM_TILE
+			* sizeof(u32)), VENUS_DMA_ALIGNMENT);
+	return size;
+}
+
+static u32 calculate_vp9d_persist1_size(void)
+{
+	u32 size = 0;
+
+	size = ALIGN(VP9_NUM_PROBABILITY_TABLE_BUF * VP9_PROB_TABLE_SIZE,
+			VENUS_DMA_ALIGNMENT) +
+			ALIGN(HFI_IRIS2_VP9D_COMV_SIZE, VENUS_DMA_ALIGNMENT) +
+			ALIGN(MAX_SUPERFRAME_HEADER_LEN, VENUS_DMA_ALIGNMENT) +
+			ALIGN(VP9_UDC_HEADER_BUF_SIZE, VENUS_DMA_ALIGNMENT) +
+			ALIGN(VP9_NUM_FRAME_INFO_BUF * CCE_TILE_OFFSET_SIZE,
+			VENUS_DMA_ALIGNMENT);
+	return size;
+}
+
+static u32 calculate_mpeg2d_persist1_size(void)
+{
+	return QMATRIX_SIZE + MP2D_QPDUMP_SIZE;
+}
+
+/* encoder internal buffers */
+u32 msm_vidc_decoder_scratch_size_iris2(struct msm_vidc_inst *inst)
+{
+	u32 size = 0;
+	u32 width, height, num_vpp_pipes;
+	struct v4l2_format *f;
+	bool is_interlaced;
+	u32 vpp_delay;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return size;
+	}
+
+	num_vpp_pipes = 0;
+	vpp_delay = 0;
+	is_interlaced = false; //(inst->pic_struct == MSM_VIDC_PIC_STRUCT_MAYBE_INTERLACED);
+	f = &inst->fmts[INPUT_PORT];
+	width = f->fmt.pix.width;
+	height = f->fmt.pix.height;
+
+	if (inst->codec == MSM_VIDC_H264) {
+		size = calculate_h264d_scratch_size(inst, width, height,
+			is_interlaced, vpp_delay, num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_HEVC) {
+		size = calculate_h265d_scratch_size(inst, width, height,
+			is_interlaced, vpp_delay, num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_MPEG2) {
+		size = calculate_mpeg2d_scratch_size(inst, width, height,
+			is_interlaced, vpp_delay, num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_VP9) {
+		size = calculate_vpxd_scratch_size(inst, width, height,
+			is_interlaced, vpp_delay, num_vpp_pipes);
+	}
+
+	return size;
+}
+
+u32 msm_vidc_decoder_scratch_1_size_iris2(struct msm_vidc_inst *inst)
+{
+	u32 size = 0;
+	u32 width, height, out_min_count, num_vpp_pipes;
+	struct v4l2_format *f;
+	u32 vpp_delay;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return size;
+	}
+
+	num_vpp_pipes = 0;
+	vpp_delay = 0;
+	f = &inst->fmts[INPUT_PORT];
+	width = f->fmt.pix.width;
+	height = f->fmt.pix.height;
+	out_min_count = inst->buffers.output.min_count;
+	out_min_count = max(vpp_delay + 1, out_min_count);
+
+	if (inst->codec == MSM_VIDC_H264) {
+		size = calculate_h264d_scratch1_size(inst, width, height,
+			out_min_count, is_secondary_output_mode(inst),
+			num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_HEVC) {
+		size = calculate_h265d_scratch1_size(inst, width, height,
+			out_min_count, is_secondary_output_mode(inst),
+			num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_MPEG2) {
+		size = calculate_mpeg2d_scratch1_size(inst, width, height,
+			out_min_count, is_secondary_output_mode(inst),
+			num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_VP9) {
+		size = calculate_vp9d_scratch1_size(inst, width, height,
+			out_min_count, is_secondary_output_mode(inst),
+			num_vpp_pipes);
+	}
+
+	return size;
+}
+
+u32 msm_vidc_decoder_persist_1_size_iris2(struct msm_vidc_inst *inst)
+{
+	u32 size = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return size;
+	}
+
+	if (inst->codec == MSM_VIDC_H264) {
+		size = calculate_h264d_persist1_size();
+	} else if (inst->codec == MSM_VIDC_HEVC) {
+		size = calculate_h265d_persist1_size();
+	} else if (inst->codec == MSM_VIDC_MPEG2) {
+		size = calculate_mpeg2d_persist1_size();
+	} else if (inst->codec == MSM_VIDC_VP9) {
+		size = calculate_vp9d_persist1_size();
+	}
+
+	return size;
+}
+
+/* encoder internal buffers */
+u32 msm_vidc_encoder_scratch_size_iris2(struct msm_vidc_inst *inst)
+{
+	u32 size = 0;
+	u32 width, height, num_vpp_pipes;
+	struct v4l2_format *f;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return size;
+	}
+
+	num_vpp_pipes = 4; //inst->core->platform_data->num_vpp_pipes;
+	f = &inst->fmts[OUTPUT_PORT];
+	width = f->fmt.pix.width;
+	height = f->fmt.pix.height;
+
+	if (inst->codec == MSM_VIDC_H264) {
+		size = calculate_h264e_scratch_size(inst, width, height,
+				2, //inst->clk_data.work_mode,
+				num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_HEVC) {
+		size = calculate_h265e_scratch_size(inst, width, height,
+				2, //inst->clk_data.work_mode,
+				num_vpp_pipes);
+	}
+
+	return size;
+}
+
+u32 msm_vidc_encoder_scratch_1_size_iris2(struct msm_vidc_inst *inst)
+{
+	u32 size = 0;
+	u32 width, height, num_ref, num_vpp_pipes;
+	bool is_tenbit = false;
+	struct v4l2_format *f;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: Instance is null!", __func__);
+		return size;
+	}
+
+	num_vpp_pipes = 4; //inst->core->platform_data->num_vpp_pipes;
+	f = &inst->fmts[OUTPUT_PORT];
+	width = f->fmt.pix.width;
+	height = f->fmt.pix.height;
+	num_ref = 4; //msm_vidc_get_num_ref_frames(inst);
+	is_tenbit = false; //(inst->bit_depth == MSM_VIDC_BIT_DEPTH_10);
+
+	if (inst->codec == MSM_VIDC_H264) {
+		size = calculate_h264e_scratch1_size(inst, width, height,
+				num_ref, is_tenbit, num_vpp_pipes);
+	} else if (inst->codec == MSM_VIDC_HEVC) {
+		size = calculate_h265e_scratch1_size(inst, width, height,
+				num_ref, is_tenbit, num_vpp_pipes);
+	}
+
+	return size;
+}
+
+u32 msm_vidc_encoder_scratch_2_size_iris2(struct msm_vidc_inst *inst)
+{
+	u32 width, height, num_ref;
+	bool is_tenbit = false;
+	struct v4l2_format *f;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return 0;
+	}
+
+	f = &inst->fmts[OUTPUT_PORT];
+	width = f->fmt.pix.width;
+	height = f->fmt.pix.height;
+	num_ref = 4; //msm_vidc_get_num_ref_frames(inst);
+	is_tenbit = false; //(inst->bit_depth == MSM_VIDC_BIT_DEPTH_10);
+
+	return calculate_enc_scratch2_size(inst, width, height,
+			num_ref, is_tenbit);
+}
+
+u32 msm_vidc_encoder_persist_size_iris2(struct msm_vidc_inst *inst)
+{
+	return calculate_enc_persist_size();
+}

+ 132 - 19
driver/variant/iris2/src/msm_vidc_iris2.c

@@ -6,10 +6,14 @@
 #include <linux/interrupt.h>
 
 #include "msm_vidc_iris2.h"
+#include "msm_vidc_buffer_iris2.h"
 #include "venus_hfi.h"
+#include "msm_vidc_inst.h"
 #include "msm_vidc_core.h"
+#include "msm_vidc_driver.h"
 #include "msm_vidc_dt.h"
 #include "msm_vidc_internal.h"
+#include "msm_vidc_buffer.h"
 #include "msm_vidc_debug.h"
 
 
@@ -490,52 +494,161 @@ static struct msm_vidc_venus_ops iris2_ops = {
 	.noc_error_info = __noc_error_info_iris2,
 };
 
-static int msm_vidc_buffer_size(struct msm_vidc_inst *inst,
-		enum msm_vidc_buffer_type type)
+static int msm_vidc_buffer_size_iris2(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type)
 {
-	int rc = 0;
+	int size = 0;
 
+	d_vpr_h("%s()\n", __func__);
 	if (!inst) {
 		d_vpr_e("%s: invalid params\n", __func__);
-		return -EINVAL;
+		return size;
 	}
 
-	d_vpr_h("%s()\n", __func__);
-	return rc;
+	if (is_decode_session(inst)) {
+		switch (buffer_type) {
+		case MSM_VIDC_BUF_INPUT:
+			size = msm_vidc_decoder_input_size(inst);
+			break;
+		case MSM_VIDC_BUF_OUTPUT:
+			size = msm_vidc_decoder_output_size(inst);
+			break;
+		case MSM_VIDC_BUF_INPUT_META:
+			size = msm_vidc_decoder_input_meta_size(inst);
+			break;
+		case MSM_VIDC_BUF_OUTPUT_META:
+			size = msm_vidc_decoder_output_meta_size(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH:
+			size = msm_vidc_decoder_scratch_size_iris2(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH_1:
+			size = msm_vidc_decoder_scratch_1_size_iris2(inst);
+			break;
+		case MSM_VIDC_BUF_PERSIST_1:
+			size = msm_vidc_decoder_persist_1_size_iris2(inst);
+			break;
+		default:
+			break;
+		}
+	} else if (is_encode_session(inst)) {
+		switch (buffer_type) {
+		case MSM_VIDC_BUF_INPUT:
+			size = msm_vidc_encoder_input_size(inst);
+			break;
+		case MSM_VIDC_BUF_OUTPUT:
+			size = msm_vidc_encoder_output_size(inst);
+			break;
+		case MSM_VIDC_BUF_INPUT_META:
+			size = msm_vidc_encoder_input_meta_size(inst);
+			break;
+		case MSM_VIDC_BUF_OUTPUT_META:
+			size = msm_vidc_encoder_output_meta_size(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH:
+			size = msm_vidc_encoder_scratch_size_iris2(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH_1:
+			size = msm_vidc_encoder_scratch_1_size_iris2(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH_2:
+			size = msm_vidc_encoder_scratch_2_size_iris2(inst);
+			break;
+		case MSM_VIDC_BUF_PERSIST:
+			size = msm_vidc_encoder_persist_size_iris2(inst);
+			break;
+		default:
+			break;
+		}
+	}
+
+	return size;
 }
 
-static int msm_vidc_buffer_min_count(struct msm_vidc_inst *inst,
-		enum msm_vidc_buffer_type type)
+static int msm_vidc_min_count_iris2(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type)
 {
-	int rc = 0;
+	int count = 0;
 
 	if (!inst) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
-
 	d_vpr_h("%s()\n", __func__);
-	return rc;
+
+	if (is_decode_session(inst)) {
+		switch (buffer_type) {
+		case MSM_VIDC_BUF_INPUT:
+		case MSM_VIDC_BUF_INPUT_META:
+			count = msm_vidc_input_min_count(inst);
+			break;
+		case MSM_VIDC_BUF_OUTPUT:
+		case MSM_VIDC_BUF_OUTPUT_META:
+			count = msm_vidc_output_min_count(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH:
+		case MSM_VIDC_BUF_SCRATCH_1:
+		case MSM_VIDC_BUF_PERSIST_1:
+			count = 1;
+			break;
+		default:
+			break;
+		}
+	} else if (is_encode_session(inst)) {
+		switch (buffer_type) {
+		case MSM_VIDC_BUF_INPUT:
+		case MSM_VIDC_BUF_INPUT_META:
+			count = msm_vidc_input_min_count(inst);
+			break;
+		case MSM_VIDC_BUF_OUTPUT:
+		case MSM_VIDC_BUF_OUTPUT_META:
+			count = msm_vidc_output_min_count(inst);
+			break;
+		case MSM_VIDC_BUF_SCRATCH:
+		case MSM_VIDC_BUF_SCRATCH_1:
+		case MSM_VIDC_BUF_SCRATCH_2:
+		case MSM_VIDC_BUF_PERSIST:
+			count = 1;
+			break;
+		default:
+			break;
+		}
+	}
+
+	return count;
 }
 
-static int msm_vidc_buffer_extra_count(struct msm_vidc_inst *inst,
-		enum msm_vidc_buffer_type type)
+static int msm_vidc_extra_count_iris2(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type)
 {
-	int rc = 0;
+	int count = 0;
 
 	if (!inst) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
-
 	d_vpr_h("%s()\n", __func__);
-	return rc;
+
+	switch (buffer_type) {
+	case MSM_VIDC_BUF_INPUT:
+	case MSM_VIDC_BUF_INPUT_META:
+		count = msm_vidc_input_extra_count(inst);
+		break;
+	case MSM_VIDC_BUF_OUTPUT:
+	case MSM_VIDC_BUF_OUTPUT_META:
+		count = msm_vidc_output_extra_count(inst);
+		break;
+	default:
+		break;
+	}
+
+	return count;
 }
 
 static struct msm_vidc_session_ops msm_session_ops = {
-	.buffer_size = msm_vidc_buffer_size,
-	.min_count = msm_vidc_buffer_min_count,
-	.extra_count = msm_vidc_buffer_extra_count,
+	.buffer_size = msm_vidc_buffer_size_iris2,
+	.min_count = msm_vidc_min_count_iris2,
+	.extra_count = msm_vidc_extra_count_iris2,
 	.calc_freq = NULL,
 	.calc_bw = NULL,
 	.decide_work_route = NULL,

+ 5 - 6
driver/vidc/inc/hfi_command.h

@@ -81,12 +81,11 @@ enum hfi_buffer_type {
 	HFI_BUFFER_BITSTREAM  = 0x00000001,
 	HFI_BUFFER_RAW        = 0x00000002,
 	HFI_BUFFER_METADATA   = 0x00000003,
-	HFI_BUFFER_DPB        = 0x00000010,
-	HFI_BUFFER_BIN        = 0x00000011,
-	HFI_BUFFER_LINE       = 0x00000012,
-	HFI_BUFFER_ARP        = 0x00000013,
-	HFI_BUFFER_COMV       = 0x00000014,
-	HFI_BUFFER_NON_COMV   = 0x00000015,
+	HFI_BUFFER_SCRATCH    = 0x00000010,
+	HFI_BUFFER_SCRATCH_1  = 0x00000011,
+	HFI_BUFFER_SCRATCH_2  = 0x00000012,
+	HFI_BUFFER_PERSIST    = 0x00000013,
+	HFI_BUFFER_PERSIST_1  = 0x00000014,
 	HFI_BUFFER_SUBCACHE   = 0x00000020,
 	HFI_BUFFER_SFR        = 0x00000021,
 };

+ 10 - 3
driver/vidc/inc/hfi_packet.h

@@ -13,6 +13,13 @@
 #include "hfi_property.h"
 #include "hfi_definition.h"
 
+u32 get_hfi_port(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type);
+u32 get_hfi_buffer_type(enum msm_vidc_domain_type domain,
+	enum msm_vidc_buffer_type buffer_type);
+u32 get_hfi_codec(struct msm_vidc_inst *inst);
+int get_hfi_buffer(struct msm_vidc_inst *inst,
+	struct msm_vidc_buffer *buffer, struct hfi_buffer *buf);
 int hfi_create_header(u8 *pkt, u32 session_id,
 	u32 header_id, u32 num_packets, u32 total_size);
 int hfi_create_packet(u8 *packet, u32 packet_size, u32 *offset,
@@ -32,8 +39,8 @@ int hfi_packet_sys_debug_config(struct msm_vidc_core *core,
 int hfi_packet_session_command(struct msm_vidc_inst *inst,
 	u32 pkt_type, u32 flags, u32 port, u32 session_id,
 	u32 payload_type, void *payload, u32 payload_size);
-int hfi_packet_create_property(struct msm_vidc_inst *inst,
-	void *pkt, u32 pkt_size, u32 pkt_type, u32 flags,
-	u32 port, u32 payload, u32 payload_type, u32 payload_size);
+int hfi_packet_session_property(struct msm_vidc_inst *inst,
+	u32 pkt_type, u32 flags, u32 port,
+	u32 payload_type, void *payload, u32 payload_size);
 #endif // _HFI_PACKET_H_
 

+ 4 - 0
driver/vidc/inc/msm_vdec.h

@@ -9,6 +9,10 @@
 #include "msm_vidc_core.h"
 #include "msm_vidc_inst.h"
 
+int msm_vdec_stop_input(struct msm_vidc_inst *inst);
+int msm_vdec_start_input(struct msm_vidc_inst *inst);
+int msm_vdec_stop_output(struct msm_vidc_inst *inst);
+int msm_vdec_start_output(struct msm_vidc_inst *inst);
 int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f);
 int msm_vdec_g_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f);
 int msm_vdec_inst_init(struct msm_vidc_inst *inst);

+ 24 - 0
driver/vidc/inc/msm_vidc_buffer.h

@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ */
+
+#ifndef __H_MSM_VIDC_BUFFER_H__
+#define __H_MSM_VIDC_BUFFER_H__
+
+#include "msm_vidc_inst.h"
+
+u32 msm_vidc_input_min_count(struct msm_vidc_inst *inst);
+u32 msm_vidc_output_min_count(struct msm_vidc_inst *inst);
+u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst);
+u32 msm_vidc_output_extra_count(struct msm_vidc_inst *inst);
+u32 msm_vidc_decoder_input_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_decoder_output_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_decoder_input_meta_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_decoder_output_meta_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_input_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_output_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_input_meta_size(struct msm_vidc_inst *inst);
+u32 msm_vidc_encoder_output_meta_size(struct msm_vidc_inst *inst);
+
+#endif // __H_MSM_VIDC_BUFFER_H__

+ 63 - 8
driver/vidc/inc/msm_vidc_driver.h

@@ -8,7 +8,6 @@
 
 #include <linux/workqueue.h>
 #include <linux/iommu.h>
-
 #include "msm_vidc_internal.h"
 #include "msm_vidc_core.h"
 #include "msm_vidc_inst.h"
@@ -23,7 +22,60 @@ static inline is_encode_session(struct msm_vidc_inst *inst)
 	return inst->domain == MSM_VIDC_ENCODER;
 }
 
+static inline is_secure_session(struct msm_vidc_inst *inst)
+{
+	return false;
+}
+
+static inline is_input_buffer(enum msm_vidc_buffer_type buffer_type)
+{
+	return buffer_type == MSM_VIDC_BUF_INPUT;
+}
+
+static inline is_output_buffer(enum msm_vidc_buffer_type buffer_type)
+{
+	return buffer_type == MSM_VIDC_BUF_OUTPUT;
+}
+
+static inline is_input_meta_buffer(enum msm_vidc_buffer_type buffer_type)
+{
+	return buffer_type == MSM_VIDC_BUF_INPUT_META;
+}
+
+static inline is_output_meta_buffer(enum msm_vidc_buffer_type buffer_type)
+{
+	return buffer_type == MSM_VIDC_BUF_OUTPUT_META;
+}
+
+static inline is_internal_buffer(enum msm_vidc_buffer_type buffer_type)
+{
+	return buffer_type == MSM_VIDC_BUF_SCRATCH ||
+		buffer_type == MSM_VIDC_BUF_SCRATCH_1 ||
+		buffer_type == MSM_VIDC_BUF_SCRATCH_2 ||
+		buffer_type == MSM_VIDC_BUF_PERSIST ||
+		buffer_type == MSM_VIDC_BUF_PERSIST_1;
+}
+
+static inline bool is_secondary_output_mode(struct msm_vidc_inst *inst)
+{
+	return false; // TODO: inst->stream_output_mode == HAL_VIDEO_DECODER_SECONDARY;
+}
+
+static inline bool is_thumbnail_session(struct msm_vidc_inst *inst)
+{
+	return false; // TODO: fix it
+}
+
+int msm_vidc_change_inst_state(struct msm_vidc_inst *inst,
+		enum msm_vidc_inst_state state);
+int msm_vidc_create_internal_buffers(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type);
+int msm_vidc_queue_internal_buffers(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type);
+int msm_vidc_release_internal_buffers(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type);
 int msm_vidc_add_session(struct msm_vidc_inst *inst);
+int msm_vidc_session_open(struct msm_vidc_inst *inst);
 int msm_vidc_core_init(struct msm_vidc_core *core);
 int msm_vidc_smmu_fault_handler(struct iommu_domain *domain,
 		struct device *dev, unsigned long iova, int flags, void *data);
@@ -34,14 +86,17 @@ void msm_vidc_pm_work_handler(struct work_struct *work);
 void msm_vidc_fw_unload_handler(struct work_struct *work);
 void msm_vidc_batch_handler(struct work_struct *work);
 int msm_vidc_setup_event_queue(struct msm_vidc_inst *inst);
-int msm_vidc_queue_init(struct msm_vidc_inst *inst);
+int msm_vidc_vb2_queue_init(struct msm_vidc_inst *inst);
 u32 msm_vidc_convert_color_fmt(u32 v4l2_fmt);
 int msm_vidc_get_control(struct msm_vidc_inst *inst, struct v4l2_ctrl *ctrl);
-int msm_vidc_get_port_from_type(u32 type);
-int msm_vidc_start_input(struct msm_vidc_inst *inst);
-int msm_vidc_start_output(struct msm_vidc_inst *inst);
-int msm_vidc_stop_input(struct msm_vidc_inst *inst);
-int msm_vidc_stop_output(struct msm_vidc_inst *inst);
-
+int msm_vidc_get_port_from_v4l2_type(u32 type);
+u32 msm_vidc_get_buffer_region(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type);
+struct msm_vidc_buffer_info *msm_vidc_get_buffer_info(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type);
+struct msm_vidc_map_info *msm_vidc_get_map_info(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type);
+struct msm_vidc_alloc_info *msm_vidc_get_alloc_info(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type);
 #endif // _MSM_VIDC_DRIVER_H_
 

+ 2 - 0
driver/vidc/inc/msm_vidc_inst.h

@@ -90,6 +90,7 @@ struct msm_vidc_inst {
 	struct v4l2_ctrl                 **ctrls;
 	u32                                num_ctrls;
 	struct vb2_queue                   vb2q[MAX_PORT];
+	struct msm_vidc_crop               crop;
 	struct msm_vidc_properties         prop;
 	struct msm_vidc_power              power;
 	struct msm_vidc_buffers            buffers;
@@ -102,6 +103,7 @@ struct msm_vidc_inst {
 	struct list_head                   input_ts;
 	struct list_head                   enc_input_crs;
 	struct list_head                   decode_bitrate_data;
+	bool                               session_created;
 	struct dentry                     *debugfs_root;
 	struct msm_vidc_debug              debug;
 };

+ 24 - 12
driver/vidc/inc/msm_vidc_internal.h

@@ -96,21 +96,23 @@ enum msm_vidc_colorformat_type {
 };
 
 enum msm_vidc_buffer_type {
-	MSM_VIDC_QUEUE             = BIT(0),
-	MSM_VIDC_INPUT             = BIT(1),
-	MSM_VIDC_OUTPUT            = BIT(2),
-	MSM_VIDC_INPUT_META        = BIT(3),
-	MSM_VIDC_OUTPUT_META       = BIT(4),
-	MSM_VIDC_DPB               = BIT(5),
-	MSM_VIDC_ARP               = BIT(6),
-	MSM_VIDC_LINE              = BIT(7),
-	MSM_VIDC_BIN               = BIT(8),
+	MSM_VIDC_BUF_QUEUE             = BIT(0),
+	MSM_VIDC_BUF_INPUT             = BIT(1),
+	MSM_VIDC_BUF_OUTPUT            = BIT(2),
+	MSM_VIDC_BUF_INPUT_META        = BIT(3),
+	MSM_VIDC_BUF_OUTPUT_META       = BIT(4),
+	MSM_VIDC_BUF_SCRATCH           = BIT(5),
+	MSM_VIDC_BUF_SCRATCH_1         = BIT(6),
+	MSM_VIDC_BUF_SCRATCH_2         = BIT(7),
+	MSM_VIDC_BUF_PERSIST           = BIT(8),
+	MSM_VIDC_BUF_PERSIST_1         = BIT(9),
 };
 
 enum msm_vidc_buffer_attributes {
-	MSM_VIDC_DEFERRED_SUBMISSION       = BIT(0),
-	MSM_VIDC_READ_ONLY                 = BIT(1),
-	MSM_VIDC_PENDING_RELEASE           = BIT(2),
+	MSM_VIDC_ATTR_DEFERRED_SUBMISSION       = BIT(0),
+	MSM_VIDC_ATTR_READ_ONLY                 = BIT(1),
+	MSM_VIDC_ATTR_PENDING_RELEASE           = BIT(2),
+	MSM_VIDC_ATTR_QUEUED                    = BIT(3),
 };
 
 enum msm_vidc_buffer_region {
@@ -385,6 +387,7 @@ struct msm_vidc_power {
 };
 
 struct msm_vidc_alloc {
+	struct list_head            list;
 	enum msm_vidc_buffer_type   buffer_type;
 	enum msm_vidc_buffer_region region;
 	u32                         size;
@@ -400,6 +403,7 @@ struct msm_vidc_alloc_info {
 };
 
 struct msm_vidc_map {
+	struct list_head            list;
 	bool                        valid;
 	enum msm_vidc_buffer_type   buffer_type;
 	enum msm_vidc_buffer_region region;
@@ -415,6 +419,7 @@ struct msm_vidc_map_info {
 };
 
 struct msm_vidc_buffer {
+	struct list_head                   list;
 	bool                               valid;
 	enum msm_vidc_buffer_type          type;
 	u32                                index;
@@ -437,6 +442,13 @@ struct msm_vidc_buffer_info {
 	u32                    size;
 };
 
+struct msm_vidc_crop {
+	u32 x;
+	u32 y;
+	u32 width;
+	u32 height;
+};
+
 struct msm_vidc_properties {
 	u32                    frame_rate;
 	u32                    operating_rate;

+ 10 - 1
driver/vidc/inc/venus_hfi.h

@@ -46,10 +46,19 @@ struct hfi_resource_syscache_info_type {
 	struct hfi_resource_subcache_type rg_subcache_entries[1];
 };
 
+int venus_hfi_queue_buffer(struct msm_vidc_inst *inst,
+	struct msm_vidc_buffer *buffer, struct msm_vidc_buffer *metabuf);
+int venus_hfi_release_buffer(struct msm_vidc_inst *inst,
+	struct msm_vidc_buffer *buffer);
+int venus_hfi_start_input(struct msm_vidc_inst *inst);
+int venus_hfi_stop_input(struct msm_vidc_inst *inst);
+int venus_hfi_start_output(struct msm_vidc_inst *inst);
+int venus_hfi_stop_output(struct msm_vidc_inst *inst);
+int venus_hfi_session_close(struct msm_vidc_inst *inst);
+int venus_hfi_session_open(struct msm_vidc_inst *inst);
 int venus_hfi_core_init(struct msm_vidc_core *core);
 int venus_hfi_core_release(struct msm_vidc_core *core);
 int venus_hfi_suspend(struct msm_vidc_core *core);
-int venus_hfi_session_open(struct msm_vidc_core *core, struct msm_vidc_inst *inst);
 void venus_hfi_work_handler(struct work_struct *work);
 void venus_hfi_pm_work_handler(struct work_struct *work);
 

+ 119 - 69
driver/vidc/src/hfi_packet.c

@@ -6,59 +6,136 @@
 #include "hfi_packet.h"
 #include "msm_vidc_core.h"
 #include "msm_vidc_inst.h"
+#include "msm_vidc_driver.h"
 #include "msm_vidc_debug.h"
 #include "msm_vidc_platform.h"
 
-static u32 get_hfi_buffer_type(enum msm_vidc_domain_type domain,
-	enum msm_vidc_buffer_type buf_type)
+u32 get_hfi_port(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type)
 {
-	switch (buf_type) {
-	case MSM_VIDC_QUEUE:
-		return 0; /* TODO */
-	case MSM_VIDC_INPUT:
+	u32 hfi_port = HFI_PORT_NONE;
+
+	if (is_decode_session(inst)) {
+		switch(buffer_type) {
+		case MSM_VIDC_BUF_INPUT:
+		case MSM_VIDC_BUF_INPUT_META:
+			hfi_port = HFI_PORT_BITSTREAM;
+			break;
+		case MSM_VIDC_BUF_OUTPUT:
+		case MSM_VIDC_BUF_OUTPUT_META:
+			hfi_port = HFI_PORT_RAW;
+			break;
+		default:
+			s_vpr_e(inst->sid, "%s: invalid buffer type %d\n",
+				__func__, buffer_type);
+			break;
+		}
+	} else if (is_encode_session(inst)) {
+		switch (buffer_type) {
+		case MSM_VIDC_BUF_INPUT:
+		case MSM_VIDC_BUF_INPUT_META:
+			hfi_port = HFI_PORT_RAW;
+			break;
+		case MSM_VIDC_BUF_OUTPUT:
+		case MSM_VIDC_BUF_OUTPUT_META:
+			hfi_port = HFI_PORT_BITSTREAM;
+			break;
+		default:
+			s_vpr_e(inst->sid, "%s: invalid buffer type %d\n",
+				__func__, buffer_type);
+			break;
+		}
+	} else {
+		s_vpr_e(inst->sid, "%s: invalid domain %#x\n",
+			__func__, inst->domain);
+	}
+
+	return hfi_port;
+}
+
+u32 get_hfi_buffer_type(enum msm_vidc_domain_type domain,
+	enum msm_vidc_buffer_type buffer_type)
+{
+	switch (buffer_type) {
+	case MSM_VIDC_BUF_INPUT:
 		if (domain == MSM_VIDC_DECODER)
-			return HFI_PORT_BITSTREAM;
+			return HFI_BUFFER_BITSTREAM;
 		else
-			return HFI_PORT_RAW;
-	case MSM_VIDC_OUTPUT:
+			return HFI_BUFFER_RAW;
+	case MSM_VIDC_BUF_OUTPUT:
 		if (domain == MSM_VIDC_DECODER)
-			return HFI_PORT_RAW;
+			return HFI_BUFFER_RAW;
 		else
-			return HFI_PORT_BITSTREAM;
-	case MSM_VIDC_INPUT_META:
-	case MSM_VIDC_OUTPUT_META:
+			return HFI_BUFFER_BITSTREAM;
+	case MSM_VIDC_BUF_INPUT_META:
+	case MSM_VIDC_BUF_OUTPUT_META:
 		return HFI_BUFFER_METADATA;
-	case MSM_VIDC_DPB:
-		return HFI_BUFFER_DPB;
-	case MSM_VIDC_ARP:
-		return HFI_BUFFER_ARP;
-	case MSM_VIDC_LINE:
-		return HFI_BUFFER_LINE;
-	case MSM_VIDC_BIN:
-		return HFI_BUFFER_BIN;
+	case MSM_VIDC_BUF_SCRATCH:
+		return HFI_BUFFER_SCRATCH;
+	case MSM_VIDC_BUF_SCRATCH_1:
+		return HFI_BUFFER_SCRATCH_1;
+	case MSM_VIDC_BUF_SCRATCH_2:
+		return HFI_BUFFER_SCRATCH_2;
+	case MSM_VIDC_BUF_PERSIST:
+		return HFI_BUFFER_PERSIST;
+	case MSM_VIDC_BUF_PERSIST_1:
+		return HFI_BUFFER_PERSIST_1;
 	default:
-		d_vpr_e("%s: Invalid buffer type %d\n",
-			__func__, buf_type);
+		d_vpr_e("invalid buffer type %d\n",
+			buffer_type);
 		return 0;
 	}
 }
 
-static u32 get_hfi_buffer_flags(enum msm_vidc_buffer_attributes attr)
+u32 get_hfi_codec(struct msm_vidc_inst *inst)
 {
-	switch (attr) {
-	case MSM_VIDC_DEFERRED_SUBMISSION:
-		return 0; /*not sure what it should be mapped to??*/
-	case MSM_VIDC_READ_ONLY:
-		return HFI_BUF_HOST_FLAG_READONLY;
-	case MSM_VIDC_PENDING_RELEASE:
-		return HFI_BUF_HOST_FLAG_RELEASE;
+	switch (inst->codec) {
+	case MSM_VIDC_H264:
+		if (inst->domain == MSM_VIDC_ENCODER)
+			return HFI_CODEC_ENCODE_AVC;
+		else
+			return HFI_CODEC_DECODE_AVC;
+	case MSM_VIDC_HEVC:
+		if (inst->domain == MSM_VIDC_ENCODER)
+			return HFI_CODEC_ENCODE_HEVC;
+		else
+			return HFI_CODEC_DECODE_HEVC;
+	case MSM_VIDC_VP9:
+		return HFI_CODEC_DECODE_VP9;
+	case MSM_VIDC_MPEG2:
+		return HFI_CODEC_DECODE_MPEG2;
 	default:
-		d_vpr_e("%s: Invalid buffer attribute %d\n",
-			__func__, attr);
+		d_vpr_e("invalid codec %d, domain %d\n",
+			inst->codec, inst->domain);
 		return 0;
 	}
 }
 
+int get_hfi_buffer(struct msm_vidc_inst *inst,
+	struct msm_vidc_buffer *buffer, struct hfi_buffer *buf)
+{
+	if (!inst || !buffer || !buf) {
+		d_vpr_e("%: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	memset(buf, 0, sizeof(struct hfi_buffer));
+	buf->type = get_hfi_buffer_type(inst->domain, buffer->type);
+	buf->index = buffer->index;
+	buf->base_address = buffer->device_addr;
+	buf->addr_offset = 0;
+	buf->buffer_size = buffer->buffer_size;
+	buf->data_offset = buffer->data_offset;
+	buf->data_size = buffer->data_size;
+	if (buffer->attr & MSM_VIDC_ATTR_READ_ONLY)
+		buf->flags |= HFI_BUF_HOST_FLAG_READONLY;
+	if (buffer->attr & MSM_VIDC_ATTR_PENDING_RELEASE)
+		buf->flags |= HFI_BUF_HOST_FLAG_RELEASE;
+	buf->timestamp = buffer->timestamp;
+
+	return 0;
+}
+
 int hfi_create_header(u8 *pkt, u32 session_id,
 	u32 header_id, u32 num_packets, u32 total_size)
 {
@@ -102,34 +179,6 @@ int hfi_create_packet(u8 *packet, u32 packet_size, u32 *offset,
 	return 0;
 }
 
-int hfi_create_buffer(u8 *packet, u32 packet_size, u32 *offset,
-	enum msm_vidc_domain_type domain, struct msm_vidc_buffer *data)
-{
-	u32 available_size = packet_size - *offset;
-	u32 buf_size = sizeof(struct hfi_buffer);
-	struct hfi_buffer *buf = (struct hfi_buffer *)packet;
-
-	if (available_size < sizeof(struct hfi_buffer)) {
-		d_vpr_e("%s: Bad buffer Size for buffer type %d\n",
-			__func__, data->type);
-		return -EINVAL;
-	}
-
-	memset(buf, 0, buf_size);
-
-	buf->type = get_hfi_buffer_type(domain, data->type);
-	buf->index = data->index;
-	buf->base_address = data->device_addr;
-	buf->addr_offset = 0;
-	buf->buffer_size = data->buffer_size;
-	buf->data_offset = data->data_offset;
-	buf->data_size = data->data_size;
-	buf->flags = get_hfi_buffer_flags(data->attr);
-	buf->timestamp = data->timestamp;
-	*offset = *offset + buf_size;
-	return 0;
-}
-
 int hfi_packet_sys_init(struct msm_vidc_core *core,
 	u8 *pkt, u32 pkt_size)
 {
@@ -474,34 +523,35 @@ err_cmd:
 	return rc;
 }
 
-int hfi_packet_create_property(struct msm_vidc_inst *inst,
-	void *pkt, u32 pkt_size, u32 pkt_type, u32 flags,
-	u32 port, u32 payload, u32 payload_type, u32 payload_size)
+int hfi_packet_session_property(struct msm_vidc_inst *inst,
+	u32 pkt_type, u32 flags, u32 port, u32 payload_type,
+	void *payload, u32 payload_size)
 {
 	int rc = 0;
 	u32 num_packets = 0, offset = 0;
 	struct msm_vidc_core *core;
 
-	if (!inst || !inst->core || !pkt) {
+	if (!inst || !inst->core || !inst->packet) {
 		d_vpr_e("%s: Invalid params\n", __func__);
 		return -EINVAL;
 	}
 
 	core = inst->core;
 	offset = sizeof(struct hfi_header);
-	rc = hfi_create_packet(pkt, pkt_size, &offset,
+	rc = hfi_create_packet(inst->packet, inst->packet_size,
+				&offset,
 				pkt_type,
 				flags,
 				payload_type,
 				port,
 				core->packet_id++,
-				&payload,
+				payload,
 				payload_size);
 	if (rc)
 		goto err_prop;
 	num_packets++;
 
-	rc = hfi_create_header(pkt, inst->session_id,
+	rc = hfi_create_header(inst->packet, inst->session_id,
 				   core->header_id++,
 				   num_packets,
 				   offset);
@@ -515,4 +565,4 @@ int hfi_packet_create_property(struct msm_vidc_inst *inst,
 err_prop:
 	d_vpr_e("%s: create packet failed\n", __func__);
 	return rc;
-}
+}

+ 250 - 20
driver/vidc/src/msm_vdec.c

@@ -13,6 +13,7 @@
 #include "msm_vidc_internal.h"
 #include "msm_vidc_platform.h"
 #include "msm_vidc_debug.h"
+#include "venus_hfi.h"
 
 static int msm_vdec_codec_change(struct msm_vidc_inst *inst, u32 codec)
 {
@@ -29,6 +30,222 @@ static int msm_vdec_codec_change(struct msm_vidc_inst *inst, u32 codec)
 	return rc;
 }
 
+static int msm_vdec_set_input_properties(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	return rc;
+}
+
+static int msm_vdec_get_input_internal_buffers(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	struct msm_vidc_core *core;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+	core = inst->core;
+
+	inst->buffers.scratch.size = call_session_op(core, buffer_size,
+			inst, MSM_VIDC_BUF_SCRATCH);
+	inst->buffers.scratch_1.size = call_session_op(core, buffer_size,
+			inst, MSM_VIDC_BUF_SCRATCH_1);
+	inst->buffers.persist_1.size = call_session_op(core, buffer_size,
+			inst, MSM_VIDC_BUF_PERSIST_1);
+
+	inst->buffers.scratch.min_count = call_session_op(core, min_count,
+			inst, MSM_VIDC_BUF_SCRATCH);
+	inst->buffers.scratch_1.min_count = call_session_op(core, min_count,
+			inst, MSM_VIDC_BUF_SCRATCH_1);
+	inst->buffers.persist_1.min_count = call_session_op(core, min_count,
+			inst, MSM_VIDC_BUF_PERSIST_1);
+
+	return rc;
+}
+
+static int msm_vdec_create_input_internal_buffers(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	rc = msm_vidc_create_internal_buffers(inst, MSM_VIDC_BUF_SCRATCH);
+	if (rc)
+		return rc;
+	rc = msm_vidc_create_internal_buffers(inst, MSM_VIDC_BUF_SCRATCH_1);
+	if (rc)
+		return rc;
+	rc = msm_vidc_create_internal_buffers(inst, MSM_VIDC_BUF_PERSIST_1);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+
+static int msm_vdec_queue_input_internal_buffers(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	rc = msm_vidc_queue_internal_buffers(inst, MSM_VIDC_BUF_SCRATCH);
+	if (rc)
+		return rc;
+	rc = msm_vidc_queue_internal_buffers(inst, MSM_VIDC_BUF_SCRATCH_1);
+	if (rc)
+		return rc;
+	rc = msm_vidc_queue_internal_buffers(inst, MSM_VIDC_BUF_PERSIST_1);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+/*
+static int msm_vdec_release_input_internal_buffers(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	rc = msm_vidc_release_internal_buffers(inst, MSM_VIDC_BUF_SCRATCH);
+	if (rc)
+		return rc;
+	rc = msm_vidc_release_internal_buffers(inst, MSM_VIDC_BUF_SCRATCH_1);
+	if (rc)
+		return rc;
+	rc = msm_vidc_release_internal_buffers(inst, MSM_VIDC_BUF_PERSIST_1);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+*/
+int msm_vdec_stop_input(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	return rc;
+}
+
+int msm_vdec_start_input(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	struct msm_vidc_core *core;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+	core = inst->core;
+
+	//rc = msm_vidc_check_session_supported(inst);
+	if (rc)
+		goto error;
+	//rc = msm_vidc_check_scaling_supported(inst);
+	if (rc)
+		goto error;
+	rc = call_session_op(core, decide_work_mode, inst);
+	if (rc)
+		goto error;
+	rc = call_session_op(core, decide_work_route, inst);
+	if (rc)
+		goto error;
+
+	/* Decide bse vpp delay after work mode */
+	//msm_vidc_set_bse_vpp_delay(inst);
+
+	rc = msm_vdec_get_input_internal_buffers(inst);
+	if (rc)
+		goto error;
+	/* check for memory after all buffers calculation */
+	//rc = msm_vidc_check_memory_supported(inst);
+	if (rc)
+		goto error;
+
+	//msm_vidc_update_dcvs(inst);
+	//msm_vidc_update_batching(inst);
+	//msm_vidc_scale_power(inst);
+
+	rc = msm_vdec_set_input_properties(inst);
+	if (rc)
+		goto error;
+	rc = msm_vdec_create_input_internal_buffers(inst);
+	if (rc)
+		goto error;
+	rc = msm_vdec_queue_input_internal_buffers(inst);
+	if (rc)
+		goto error;
+
+	rc = venus_hfi_start_input(inst);
+	if (rc)
+		goto error;
+
+	rc = msm_vidc_change_inst_state(inst, MSM_VIDC_START_INPUT);
+	if (rc)
+		goto error;
+
+	d_vpr_h("%s: done\n", __func__);
+	return 0;
+
+error:
+	msm_vdec_stop_input(inst);
+	return rc;
+}
+
+int msm_vdec_stop_output(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	return rc;
+}
+
+int msm_vdec_start_output(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s()\n", __func__);
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	return rc;
+}
+
 int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 {
 	int rc = 0;
@@ -64,16 +281,16 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 		}
 		fmt = &inst->fmts[INPUT_PORT];
 		fmt->type = INPUT_PLANE;
-		fmt->fmt.pix.width = f->fmt.pix.width;
-		fmt->fmt.pix.height = f->fmt.pix.height;
+		fmt->fmt.pix.width = ALIGN(f->fmt.pix.width, 16);
+		fmt->fmt.pix.height = ALIGN(f->fmt.pix.height, 16);
 		fmt->fmt.pix.pixelformat = f->fmt.pix.pixelformat;
 		fmt->fmt.pix.bytesperline = 0;
 		fmt->fmt.pix.sizeimage = call_session_op(core, buffer_size,
-				inst, MSM_VIDC_INPUT);
+				inst, MSM_VIDC_BUF_INPUT);
 		inst->buffers.input.min_count =
-			call_session_op(core, min_count, inst, MSM_VIDC_INPUT);
+			call_session_op(core, min_count, inst, MSM_VIDC_BUF_INPUT);
 		inst->buffers.input.extra_count =
-			call_session_op(core, extra_count, inst, MSM_VIDC_INPUT);
+			call_session_op(core, extra_count, inst, MSM_VIDC_BUF_INPUT);
 		if (inst->buffers.input.actual_count <
 			inst->buffers.input.min_count +
 			inst->buffers.input.extra_count) {
@@ -83,6 +300,11 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 		}
 		inst->buffers.input.size = fmt->fmt.pix.sizeimage;
 
+		// update crop dimensions
+		inst->crop.x = inst->crop.y = 0;
+		inst->crop.width = f->fmt.pix.width;
+		inst->crop.height = f->fmt.pix.height;
+
 		//rc = msm_vidc_check_session_supported(inst);
 		if (rc)
 			goto err_invalid_fmt;
@@ -90,10 +312,14 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 		//	mplane->pixelformat);
 		s_vpr_h(inst->sid,
 			"%s: input: codec %#x width %d height %d size %d min_count %d extra_count %d\n",
-			__func__, fmt->fmt.pix.pixelformat, fmt->fmt.pix.width,
-			fmt->fmt.pix.height, fmt->fmt.pix.sizeimage,
+			__func__, f->fmt.pix.pixelformat, f->fmt.pix.width,
+			f->fmt.pix.height, fmt->fmt.pix.sizeimage,
 			inst->buffers.input.min_count,
 			inst->buffers.input.extra_count);
+
+		//msm_vidc_update_dcvs(inst);
+		//msm_vidc_update_batching(inst);
+
 	} else if (f->type == INPUT_META_PLANE) {
 		if (inst->state == MSM_VIDC_START_INPUT) {
 			d_vpr_e("%s: invalid state %d\n", __func__, inst->state);
@@ -103,7 +329,7 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 		fmt->type = INPUT_META_PLANE;
 		fmt->fmt.meta.dataformat = V4L2_PIX_FMT_VIDC_META;
 		fmt->fmt.meta.buffersize = call_session_op(core, buffer_size,
-				inst, MSM_VIDC_INPUT_META);
+				inst, MSM_VIDC_BUF_INPUT_META);
 		inst->buffers.input_meta.min_count =
 				inst->buffers.input.min_count;
 		inst->buffers.input_meta.extra_count =
@@ -132,11 +358,11 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 			f->fmt.pix.height);
 		fmt->fmt.pix.bytesperline = fmt->fmt.pix.width;
 		fmt->fmt.pix.sizeimage = call_session_op(core, buffer_size,
-				inst, MSM_VIDC_OUTPUT);
+				inst, MSM_VIDC_BUF_OUTPUT);
 		inst->buffers.output.min_count =
-			call_session_op(core, min_count, inst, MSM_VIDC_OUTPUT);
+			call_session_op(core, min_count, inst, MSM_VIDC_BUF_OUTPUT);
 		inst->buffers.output.extra_count =
-			call_session_op(core, extra_count, inst, MSM_VIDC_OUTPUT);
+			call_session_op(core, extra_count, inst, MSM_VIDC_BUF_OUTPUT);
 		if (inst->buffers.output.actual_count <
 			inst->buffers.output.min_count +
 			inst->buffers.output.extra_count) {
@@ -164,7 +390,7 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 		fmt->type = OUTPUT_META_PLANE;
 		fmt->fmt.meta.dataformat = V4L2_PIX_FMT_VIDC_META;
 		fmt->fmt.meta.buffersize = call_session_op(core, buffer_size,
-				inst, MSM_VIDC_OUTPUT_META);
+				inst, MSM_VIDC_BUF_OUTPUT_META);
 		inst->buffers.output_meta.min_count =
 				inst->buffers.output.min_count;
 		inst->buffers.output_meta.extra_count =
@@ -177,7 +403,11 @@ int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
 			__func__, fmt->fmt.meta.buffersize,
 			inst->buffers.output_meta.min_count,
 			inst->buffers.output_meta.extra_count);
+	} else {
+		s_vpr_e(inst->sid, "%s: invalid type %d\n", __func__, f->type);
+		goto err_invalid_fmt;
 	}
+	memcpy(f, fmt, sizeof(struct v4l2_format));
 
 err_invalid_fmt:
 	return rc;
@@ -233,11 +463,11 @@ int msm_vdec_inst_init(struct msm_vidc_inst *inst)
 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
 	f->fmt.pix.bytesperline = 0;
 	f->fmt.pix.sizeimage = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_INPUT);
+			inst, MSM_VIDC_BUF_INPUT);
 	inst->buffers.input.min_count =
-			call_session_op(core, min_count, inst, MSM_VIDC_INPUT);
+			call_session_op(core, min_count, inst, MSM_VIDC_BUF_INPUT);
 	inst->buffers.input.extra_count =
-			call_session_op(core, extra_count, inst, MSM_VIDC_INPUT);
+			call_session_op(core, extra_count, inst, MSM_VIDC_BUF_INPUT);
 	inst->buffers.input.actual_count =
 			inst->buffers.input.min_count +
 			inst->buffers.input.extra_count;
@@ -247,7 +477,7 @@ int msm_vdec_inst_init(struct msm_vidc_inst *inst)
 	f->type = INPUT_META_PLANE;
 	f->fmt.meta.dataformat = V4L2_PIX_FMT_VIDC_META;
 	f->fmt.meta.buffersize = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_INPUT_META);
+			inst, MSM_VIDC_BUF_INPUT_META);
 	inst->buffers.input_meta.min_count = inst->buffers.input.min_count;
 	inst->buffers.input_meta.extra_count = inst->buffers.input.extra_count;
 	inst->buffers.input_meta.actual_count = inst->buffers.input.actual_count;
@@ -262,11 +492,11 @@ int msm_vdec_inst_init(struct msm_vidc_inst *inst)
 		msm_vidc_convert_color_fmt(f->fmt.pix.pixelformat), DEFAULT_HEIGHT);
 	f->fmt.pix.bytesperline = f->fmt.pix.width;
 	f->fmt.pix.sizeimage = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_OUTPUT);
+			inst, MSM_VIDC_BUF_OUTPUT);
 	inst->buffers.output.min_count =
-			call_session_op(core, min_count, inst, MSM_VIDC_OUTPUT);
+			call_session_op(core, min_count, inst, MSM_VIDC_BUF_OUTPUT);
 	inst->buffers.output.extra_count =
-			call_session_op(core, extra_count, inst, MSM_VIDC_OUTPUT);
+			call_session_op(core, extra_count, inst, MSM_VIDC_BUF_OUTPUT);
 	inst->buffers.output.actual_count =
 			inst->buffers.output.min_count +
 			inst->buffers.output.extra_count;
@@ -276,7 +506,7 @@ int msm_vdec_inst_init(struct msm_vidc_inst *inst)
 	f->type = OUTPUT_META_PLANE;
 	f->fmt.meta.dataformat = V4L2_PIX_FMT_VIDC_META;
 	f->fmt.meta.buffersize = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_OUTPUT_META);
+			inst, MSM_VIDC_BUF_OUTPUT_META);
 	inst->buffers.output_meta.min_count = inst->buffers.output.min_count;
 	inst->buffers.output_meta.extra_count = inst->buffers.output.extra_count;
 	inst->buffers.output_meta.actual_count = inst->buffers.output.actual_count;

+ 8 - 8
driver/vidc/src/msm_venc.c

@@ -35,11 +35,11 @@ int msm_venc_inst_init(struct msm_vidc_inst *inst)
 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
 	f->fmt.pix.bytesperline = 0;
 	f->fmt.pix.sizeimage = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_OUTPUT);
+			inst, MSM_VIDC_BUF_OUTPUT);
 	inst->buffers.output.min_count =
-			call_session_op(core, min_count, inst, MSM_VIDC_OUTPUT);
+			call_session_op(core, min_count, inst, MSM_VIDC_BUF_OUTPUT);
 	inst->buffers.output.extra_count =
-			call_session_op(core, extra_count, inst, MSM_VIDC_OUTPUT);
+			call_session_op(core, extra_count, inst, MSM_VIDC_BUF_OUTPUT);
 	inst->buffers.output.actual_count =
 			inst->buffers.output.min_count +
 			inst->buffers.output.extra_count;
@@ -49,7 +49,7 @@ int msm_venc_inst_init(struct msm_vidc_inst *inst)
 	f->type = OUTPUT_META_PLANE;
 	f->fmt.meta.dataformat = V4L2_PIX_FMT_VIDC_META;
 	f->fmt.meta.buffersize = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_OUTPUT_META);
+			inst, MSM_VIDC_BUF_OUTPUT_META);
 	inst->buffers.output_meta.min_count = inst->buffers.output.min_count;
 	inst->buffers.output_meta.extra_count = inst->buffers.output.extra_count;
 	inst->buffers.output_meta.actual_count = inst->buffers.output.actual_count;
@@ -64,11 +64,11 @@ int msm_venc_inst_init(struct msm_vidc_inst *inst)
 		msm_vidc_convert_color_fmt(f->fmt.pix.pixelformat), DEFAULT_HEIGHT);
 	f->fmt.pix.bytesperline = f->fmt.pix.width;
 	f->fmt.pix.sizeimage = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_INPUT);
+			inst, MSM_VIDC_BUF_INPUT);
 	inst->buffers.input.min_count =
-			call_session_op(core, min_count, inst, MSM_VIDC_INPUT);
+			call_session_op(core, min_count, inst, MSM_VIDC_BUF_INPUT);
 	inst->buffers.input.extra_count =
-			call_session_op(core, extra_count, inst, MSM_VIDC_INPUT);
+			call_session_op(core, extra_count, inst, MSM_VIDC_BUF_INPUT);
 	inst->buffers.input.actual_count =
 			inst->buffers.input.min_count +
 			inst->buffers.input.extra_count;
@@ -78,7 +78,7 @@ int msm_venc_inst_init(struct msm_vidc_inst *inst)
 	f->type = INPUT_META_PLANE;
 	f->fmt.meta.dataformat = V4L2_PIX_FMT_VIDC_META;
 	f->fmt.meta.buffersize = call_session_op(core, buffer_size,
-			inst, MSM_VIDC_INPUT_META);
+			inst, MSM_VIDC_BUF_INPUT_META);
 	inst->buffers.input_meta.min_count = inst->buffers.input.min_count;
 	inst->buffers.input_meta.extra_count = inst->buffers.input.extra_count;
 	inst->buffers.input_meta.actual_count = inst->buffers.input.actual_count;

+ 212 - 35
driver/vidc/src/msm_vidc.c

@@ -119,9 +119,9 @@ int msm_vidc_enum_fmt(void *instance, struct v4l2_fmtdesc *f)
 		return -EINVAL;
 
 	if (inst->domain == MSM_VIDC_DECODER)
-		return 0;//msm_vdec_enum_fmt(instance, f);
+		return -EINVAL;//msm_vdec_enum_fmt(instance, f);
 	else if (inst->domain == MSM_VIDC_ENCODER)
-		return 0;//msm_venc_enum_fmt(instance, f);
+		return -EINVAL;//msm_venc_enum_fmt(instance, f);
 	return -EINVAL;
 }
 EXPORT_SYMBOL(msm_vidc_enum_fmt);
@@ -203,6 +203,25 @@ int msm_vidc_s_fmt(void *instance, struct v4l2_format *f)
 	if (!inst || !f)
 		return -EINVAL;
 
+	if (f->type == INPUT_PLANE) {
+		if (inst->state != MSM_VIDC_OPEN &&
+		    inst->state != MSM_VIDC_START_OUTPUT) {
+			s_vpr_e(inst->sid,
+				"%s: s_fmt(%d) not allowed in %d state\n",
+				__func__, f->type, inst->state);
+			return -EINVAL;
+		}
+	} else if (f->type == OUTPUT_PLANE) {
+		if (inst->state != MSM_VIDC_OPEN &&
+		    inst->state != MSM_VIDC_START_INPUT &&
+		    inst->state != MSM_VIDC_DRAIN_START_INPUT) {
+			s_vpr_e(inst->sid,
+				"%s: s_fmt(%d) not allowed in %d state\n",
+				__func__, f->type, inst->state);
+			return -EINVAL;
+		}
+	}
+
 	if (inst->domain == MSM_VIDC_DECODER)
 		rc = msm_vdec_s_fmt(inst, f);
 	if (inst->domain == MSM_VIDC_ENCODER)
@@ -269,17 +288,41 @@ int msm_vidc_reqbufs(void *instance, struct v4l2_requestbuffers *b)
 	if (!inst || !b)
 		return -EINVAL;
 
-	port = msm_vidc_get_port_from_type(b->type);
+	mutex_lock(&inst->lock);
+
+	if (b->type == INPUT_PLANE) {
+		if (inst->state != MSM_VIDC_OPEN &&
+		    inst->state != MSM_VIDC_START_OUTPUT) {
+			s_vpr_e(inst->sid,
+				"%s: reqbufs(%d) not allowed in %d state\n",
+				__func__, b->type, inst->state);
+			return -EINVAL;
+		}
+	} else if (b->type == OUTPUT_PLANE) {
+		if (inst->state != MSM_VIDC_OPEN &&
+		    inst->state != MSM_VIDC_START_INPUT &&
+		    inst->state != MSM_VIDC_DRAIN_START_INPUT) {
+			s_vpr_e(inst->sid,
+				"%s: reqbufs(%d) not allowed in %d state\n",
+				__func__, b->type, inst->state);
+			return -EINVAL;
+		}
+	}
+
+	port = msm_vidc_get_port_from_v4l2_type(b->type);
 	if (port < 0) {
 		d_vpr_e("%s: invalid queue type %d\n", __func__, b->type);
 		return -EINVAL;
 	}
-	mutex_lock(&inst->lock);
 	rc = vb2_reqbufs(&inst->vb2q[port], b);
-	mutex_unlock(&inst->lock);
-	if (rc)
-		s_vpr_e(inst->sid, "%s: vb2_reqbufs failed, %d\n", rc);
+	if (rc) {
+		s_vpr_e(inst->sid, "%s: vb2_reqbufs(%d) failed, %d\n",
+			__func__, b->type, rc);
+		goto unlock;
+	}
 
+unlock:
+	mutex_unlock(&inst->lock);
 	return rc;
 }
 EXPORT_SYMBOL(msm_vidc_reqbufs);
@@ -297,50 +340,168 @@ int msm_vidc_dqbuf(void *instance, struct v4l2_buffer *b)
 }
 EXPORT_SYMBOL(msm_vidc_dqbuf);
 
-int msm_vidc_streamon(void *instance, enum v4l2_buf_type i)
+int msm_vidc_streamon(void *instance, enum v4l2_buf_type type)
 {
 	int rc = 0;
 	struct msm_vidc_inst *inst = instance;
+	enum msm_vidc_inst_state new_state = 0;
 	int port;
 
 	if (!inst)
 		return -EINVAL;
 
-	port = msm_vidc_get_port_from_type(i);
+	mutex_lock(&inst->lock);
+
+	if (type == INPUT_PLANE) {
+		if (inst->state != MSM_VIDC_OPEN &&
+		    inst->state != MSM_VIDC_START_OUTPUT) {
+			s_vpr_e(inst->sid,
+				"%s: streamon(%d) not allowed in %d state\n",
+				__func__, type, inst->state);
+			rc = -EINVAL;
+			goto unlock;
+		}
+	} else if (type == OUTPUT_PLANE) {
+		if (inst->state != MSM_VIDC_OPEN &&
+		    inst->state != MSM_VIDC_START_INPUT &&
+		    inst->state != MSM_VIDC_DRAIN_START_INPUT) {
+			s_vpr_e(inst->sid,
+				"%s: streamon(%d) not allowed in %d state\n",
+				__func__, type, inst->state);
+			rc = -EINVAL;
+			goto unlock;
+		}
+	}
+
+	port = msm_vidc_get_port_from_v4l2_type(type);
 	if (port < 0) {
-		d_vpr_e("%s: invalid buf type %d\n", __func__, i);
-		return -EINVAL;
+		d_vpr_e("%s: invalid buf type %d\n", __func__, type);
+		rc = -EINVAL;
+		goto unlock;
+	}
+
+	rc = vb2_streamon(&inst->vb2q[port], type);
+	if (rc) {
+		s_vpr_e(inst->sid, "%s: vb2_streamon(%d) failed, %d\n",
+			__func__, type, rc);
+		goto unlock;
+	}
+
+	if (type == INPUT_PLANE) {
+		if (inst->state == MSM_VIDC_OPEN) {
+			new_state = MSM_VIDC_START_INPUT;
+		} else if (inst->state == MSM_VIDC_START_OUTPUT) {
+			new_state = MSM_VIDC_START;
+		}
+		rc = msm_vidc_change_inst_state(inst, new_state);
+		if (rc)
+			goto unlock;
+	} else if (type == OUTPUT_PLANE) {
+		if (inst->state == MSM_VIDC_OPEN) {
+			new_state = MSM_VIDC_START_OUTPUT;
+		} else if (inst->state == MSM_VIDC_START_INPUT) {
+			new_state = MSM_VIDC_START;
+		} else if (inst->state == MSM_VIDC_DRAIN_START_INPUT) {
+			if (0 /* check if input port settings change pending */)
+				new_state = MSM_VIDC_DRC_DRAIN;
+			else
+				new_state = MSM_VIDC_DRAIN;
+		}
+		rc = msm_vidc_change_inst_state(inst, new_state);
+		if (rc)
+			goto unlock;
 	}
-	mutex_lock(&inst->lock);
-	rc = vb2_streamon(&inst->vb2q[port], i);
-	mutex_unlock(&inst->lock);
-	if (rc)
-		s_vpr_e(inst->sid, "%s: vb2_streamon failed, %d\n", rc);
 
+unlock:
+	mutex_unlock(&inst->lock);
 	return rc;
 }
 EXPORT_SYMBOL(msm_vidc_streamon);
 
-int msm_vidc_streamoff(void *instance, enum v4l2_buf_type i)
+int msm_vidc_streamoff(void *instance, enum v4l2_buf_type type)
 {
 	int rc = 0;
 	struct msm_vidc_inst *inst = instance;
+	enum msm_vidc_inst_state new_state = 0;
 	int port;
 
 	if (!inst)
 		return -EINVAL;
 
-	port = msm_vidc_get_port_from_type(i);
+	mutex_lock(&inst->lock);
+
+	if (type == INPUT_PLANE) {
+		if (inst->state == MSM_VIDC_OPEN ||
+		    inst->state == MSM_VIDC_START_OUTPUT) {
+			s_vpr_e(inst->sid,
+				"%s: streamoff(%d) not allowed in %d state\n",
+				__func__, type, inst->state);
+			rc = -EINVAL;
+			goto unlock;
+		}
+	} else if (type == OUTPUT_PLANE) {
+		if (inst->state == MSM_VIDC_OPEN ||
+		    inst->state == MSM_VIDC_START_INPUT) {
+			s_vpr_e(inst->sid,
+				"%s: streamoff(%d) not allowed in %d state\n",
+				__func__, type, inst->state);
+			rc = -EINVAL;
+			goto unlock;
+		}
+	}
+
+	port = msm_vidc_get_port_from_v4l2_type(type);
 	if (port < 0) {
-		d_vpr_e("%s: invalid buf type %d\n", __func__, i);
-		return -EINVAL;
+		d_vpr_e("%s: invalid buf type %d\n", __func__, type);
+		rc = -EINVAL;
+		goto unlock;
+	}
+
+	rc = vb2_streamoff(&inst->vb2q[port], type);
+	if (rc) {
+		s_vpr_e(inst->sid, "%s: vb2_streamoff(%d) failed, %d\n",
+			__func__, type, rc);
+		goto unlock;
 	}
-	mutex_lock(&inst->lock);
-	rc = vb2_streamoff(&inst->vb2q[port], i);
-	mutex_unlock(&inst->lock);
-	if (rc)
-		s_vpr_e(inst->sid, "%s: vb2_streamoff failed, %d\n", rc);
 
+	if (type == INPUT_PLANE) {
+		if (inst->state == MSM_VIDC_START_INPUT) {
+			new_state = MSM_VIDC_OPEN;
+		} else if (inst->state == MSM_VIDC_START) {
+			new_state = MSM_VIDC_START_OUTPUT;
+		} else if (inst->state == MSM_VIDC_DRC ||
+			   inst->state == MSM_VIDC_DRC_LAST_FLAG ||
+			   inst->state == MSM_VIDC_DRAIN ||
+		           inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
+			   inst->state == MSM_VIDC_DRC_DRAIN ||
+			   inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
+			   inst->state == MSM_VIDC_DRAIN_START_INPUT) {
+			new_state = MSM_VIDC_START_OUTPUT;
+			/* discard pending port settings change if any */
+		}
+		rc = msm_vidc_change_inst_state(inst, new_state);
+		if (rc)
+			goto unlock;
+	} else if (type == OUTPUT_PLANE) {
+		if (inst->state == MSM_VIDC_START_OUTPUT) {
+			new_state = MSM_VIDC_OPEN;
+		} else if (inst->state == MSM_VIDC_START ||
+			   inst->state == MSM_VIDC_DRAIN ||
+			   inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
+			   inst->state == MSM_VIDC_DRC ||
+			   inst->state == MSM_VIDC_DRC_LAST_FLAG ||
+			   inst->state == MSM_VIDC_DRC_DRAIN) {
+			new_state = MSM_VIDC_START_INPUT;
+		} else if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG) {
+			new_state = MSM_VIDC_DRAIN_START_INPUT;
+		}
+		rc = msm_vidc_change_inst_state(inst, new_state);
+		if (rc)
+			goto unlock;
+	}
+
+unlock:
+	mutex_unlock(&inst->lock);
 	return rc;
 }
 EXPORT_SYMBOL(msm_vidc_streamoff);
@@ -357,44 +518,46 @@ int msm_vidc_enum_framesizes(void *instance, struct v4l2_frmsizeenum *fsize)
 }
 EXPORT_SYMBOL(msm_vidc_enum_framesizes);
 
-int msm_vidc_subscribe_event(void *inst,
+int msm_vidc_subscribe_event(void *instance,
 		const struct v4l2_event_subscription *sub)
 {
 	int rc = 0;
-	struct msm_vidc_inst *vidc_inst = (struct msm_vidc_inst *)inst;
+	struct msm_vidc_inst *inst = (struct msm_vidc_inst *)instance;
 
 	if (!inst || !sub)
 		return -EINVAL;
 
-	rc = v4l2_event_subscribe(&vidc_inst->event_handler,
+	s_vpr_e(inst->sid, "%s: type %d id %d\n", __func__, sub->type, sub->id);
+	rc = v4l2_event_subscribe(&inst->event_handler,
 		sub, MAX_EVENTS, NULL);
 	return rc;
 }
 EXPORT_SYMBOL(msm_vidc_subscribe_event);
 
-int msm_vidc_unsubscribe_event(void *inst,
+int msm_vidc_unsubscribe_event(void *instance,
 		const struct v4l2_event_subscription *sub)
 {
 	int rc = 0;
-	struct msm_vidc_inst *vidc_inst = (struct msm_vidc_inst *)inst;
+	struct msm_vidc_inst *inst = (struct msm_vidc_inst *)instance;
 
 	if (!inst || !sub)
 		return -EINVAL;
 
-	rc = v4l2_event_unsubscribe(&vidc_inst->event_handler, sub);
+	s_vpr_e(inst->sid, "%s: type %d id %d\n", __func__, sub->type, sub->id);
+	rc = v4l2_event_unsubscribe(&inst->event_handler, sub);
 	return rc;
 }
 EXPORT_SYMBOL(msm_vidc_unsubscribe_event);
 
-int msm_vidc_dqevent(void *inst, struct v4l2_event *event)
+int msm_vidc_dqevent(void *instance, struct v4l2_event *event)
 {
 	int rc = 0;
-	struct msm_vidc_inst *vidc_inst = (struct msm_vidc_inst *)inst;
+	struct msm_vidc_inst *inst = (struct msm_vidc_inst *)instance;
 
 	if (!inst || !event)
 		return -EINVAL;
 
-	rc = v4l2_event_dequeue(&vidc_inst->event_handler, event, false);
+	rc = v4l2_event_dequeue(&inst->event_handler, event, false);
 	return rc;
 }
 EXPORT_SYMBOL(msm_vidc_dqevent);
@@ -455,6 +618,20 @@ void *msm_vidc_open(void *vidc_core, u32 session_type)
 	INIT_LIST_HEAD(&inst->buffers.scratch_2.list);
 	INIT_LIST_HEAD(&inst->buffers.persist.list);
 	INIT_LIST_HEAD(&inst->buffers.persist_1.list);
+	INIT_LIST_HEAD(&inst->allocations.scratch.list);
+	INIT_LIST_HEAD(&inst->allocations.scratch_1.list);
+	INIT_LIST_HEAD(&inst->allocations.scratch_2.list);
+	INIT_LIST_HEAD(&inst->allocations.persist.list);
+	INIT_LIST_HEAD(&inst->allocations.persist_1.list);
+	INIT_LIST_HEAD(&inst->maps.input.list);
+	INIT_LIST_HEAD(&inst->maps.input_meta.list);
+	INIT_LIST_HEAD(&inst->maps.output.list);
+	INIT_LIST_HEAD(&inst->maps.output_meta.list);
+	INIT_LIST_HEAD(&inst->maps.scratch.list);
+	INIT_LIST_HEAD(&inst->maps.scratch_1.list);
+	INIT_LIST_HEAD(&inst->maps.scratch_2.list);
+	INIT_LIST_HEAD(&inst->maps.persist.list);
+	INIT_LIST_HEAD(&inst->maps.persist_1.list);
 	inst->domain = session_type;
 	inst->state = MSM_VIDC_OPEN;
 	//inst->debugfs_root =
@@ -476,7 +653,7 @@ void *msm_vidc_open(void *vidc_core, u32 session_type)
 			goto error;
 	}
 
-	rc = msm_vidc_queue_init(inst);
+	rc = msm_vidc_vb2_queue_init(inst);
 	if (rc)
 		goto error;
 

+ 190 - 0
driver/vidc/src/msm_vidc_buffer.c

@@ -0,0 +1,190 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ */
+
+#include <media/msm_media_info.h>
+#include "msm_vidc_buffer.h"
+#include "msm_vidc_inst.h"
+#include "msm_vidc_core.h"
+#include "msm_vidc_driver.h"
+#include "msm_vidc_debug.h"
+
+#define MIN_INPUT_BUFFERS 4
+#define MIN_ENC_OUTPUT_BUFFERS 4
+
+u32 msm_vidc_input_min_count(struct msm_vidc_inst *inst)
+{
+	u32 input_min_count = 0;
+	//struct v4l2_ctrl *max_layer = NULL;
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return 0;
+	}
+
+	if (!is_decode_session(inst) && !is_encode_session(inst))
+		return 0;
+
+	input_min_count = MIN_INPUT_BUFFERS;
+
+	if (is_thumbnail_session(inst))
+		input_min_count = 1;
+
+	//if (is_grid_session(inst))
+	//	input_min_count = 2;
+
+	//if (is_hier_b_session(inst)) {
+		//max_layer = get_ctrl(inst,
+		//	V4L2_CID_MPEG_VIDC_VIDEO_HEVC_MAX_HIER_CODING_LAYER);
+		//input_min_count = (1 << (max_layer->val - 1)) + 2;
+	//}
+
+	return input_min_count;
+}
+
+u32 msm_vidc_output_min_count(struct msm_vidc_inst *inst)
+{
+	u32 output_min_count;
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return 0;
+	}
+
+	if (!is_decode_session(inst) && !is_encode_session(inst))
+		return 0;
+
+	if (is_thumbnail_session(inst))
+		return inst->codec == MSM_VIDC_VP9 ? 8 : 1;
+
+	if (is_decode_session(inst)) {
+		switch (inst->codec) {
+		case MSM_VIDC_H264:
+		case MSM_VIDC_HEVC:
+			output_min_count = 4;
+			break;
+		case MSM_VIDC_VP9:
+			output_min_count = 9;
+			break;
+		case MSM_VIDC_MPEG2:
+			output_min_count = 6;
+			break;
+		default:
+			output_min_count = 4;
+		}
+	} else {
+		output_min_count = MIN_ENC_OUTPUT_BUFFERS;
+	}
+
+	//if (is_vpp_delay_allowed(inst)) {
+	//	output_min_count =
+	//		max(output_min_count, (u32)MAX_BSE_VPP_DELAY);
+	//	output_min_count =
+	//		max(output_min_count, (u32)(msm_vidc_vpp_delay & 0x1F));
+	//}
+
+	return output_min_count;
+}
+
+u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst)
+{
+	u32 extra_input_count = 0;
+	struct msm_vidc_core *core;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params %pK\n", __func__, inst);
+		return 0;
+	}
+	core = inst->core;
+
+	if (is_thumbnail_session(inst))
+		return extra_input_count;
+
+	if (is_decode_session(inst)) {
+		/* add dcvs buffers */
+		/* add batching buffers */
+		extra_input_count = 6;
+	} else if (is_encode_session(inst)) {
+		/* add dcvs buffers */
+		extra_input_count = 4;
+	}
+	return extra_input_count;
+}
+
+u32 msm_vidc_output_extra_count(struct msm_vidc_inst *inst)
+{
+	u32 extra_output_count = 0;
+	struct msm_vidc_core *core;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params %pK\n", __func__, inst);
+		return 0;
+	}
+	core = inst->core;
+
+	if (is_thumbnail_session(inst))
+		return 0;
+
+	if (is_decode_session(inst)) {
+		/* add dcvs buffers */
+		/* add batching buffers */
+		extra_output_count = 6;
+	} else if (is_encode_session(inst)) {
+		/* add heif buffers */
+		//extra_output_count = 8
+	}
+	return extra_output_count;
+}
+
+u32 msm_vidc_decoder_input_size(struct msm_vidc_inst *inst)
+{
+	return ALIGN(15 * 1024 * 1024, SZ_4K);
+}
+
+u32 msm_vidc_decoder_output_size(struct msm_vidc_inst *inst)
+{
+	u32 format;
+	struct v4l2_format *f;
+
+	f = &inst->fmts[OUTPUT_PORT];
+	format = msm_vidc_convert_color_fmt(f->fmt.pix.pixelformat);
+	return VENUS_BUFFER_SIZE(format, f->fmt.pix.width,
+			f->fmt.pix.height);
+}
+
+u32 msm_vidc_decoder_input_meta_size(struct msm_vidc_inst *inst)
+{
+	return ALIGN(16 * 1024, SZ_4K);
+}
+
+u32 msm_vidc_decoder_output_meta_size(struct msm_vidc_inst *inst)
+{
+	return ALIGN(16 * 1024, SZ_4K);
+}
+
+u32 msm_vidc_encoder_input_size(struct msm_vidc_inst *inst)
+{
+	u32 format;
+	struct v4l2_format *f;
+
+	f = &inst->fmts[INPUT_PORT];
+	format = msm_vidc_convert_color_fmt(f->fmt.pix.pixelformat);
+	return VENUS_BUFFER_SIZE(format, f->fmt.pix.width,
+			f->fmt.pix.height);
+}
+
+u32 msm_vidc_encoder_output_size(struct msm_vidc_inst *inst)
+{
+	return ALIGN(15 * 1024 * 1024, SZ_4K);
+}
+
+u32 msm_vidc_encoder_input_meta_size(struct msm_vidc_inst *inst)
+{
+	return ALIGN(1 * 1024 * 1024, SZ_4K);
+}
+
+u32 msm_vidc_encoder_output_meta_size(struct msm_vidc_inst *inst)
+{
+	return ALIGN(16 * 1024, SZ_4K);
+}

+ 366 - 19
driver/vidc/src/msm_vidc_driver.c

@@ -10,10 +10,15 @@
 
 #include "msm_vidc_driver.h"
 #include "msm_vidc_internal.h"
+#include "msm_vidc_memory.h"
 #include "msm_vidc_debug.h"
 #include "venus_hfi.h"
 
-int msm_vidc_get_port_from_type(u32 type)
+void print_vidc_buffer(struct msm_vidc_inst *inst, struct msm_vidc_buffer *b)
+{
+}
+
+int msm_vidc_get_port_from_v4l2_type(u32 type)
 {
 	int port;
 
@@ -26,12 +31,167 @@ int msm_vidc_get_port_from_type(u32 type)
 	} else if (type == OUTPUT_META_PLANE) {
 		port = OUTPUT_META_PORT;
 	} else {
-		d_vpr_e("invalid type %d\n", type);
+		d_vpr_e("%s: invalid type %d\n", __func__, type);
 		port = -EINVAL;
 	}
 
 	return port;
 }
+
+u32 msm_vidc_get_buffer_region(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type)
+{
+	u32 region = MSM_VIDC_NON_SECURE;
+
+	if (!is_secure_session(inst))
+		return region;
+
+	switch (buffer_type) {
+	case MSM_VIDC_BUF_INPUT:
+		if (is_encode_session(inst))
+			region = MSM_VIDC_SECURE_PIXEL;
+		else
+			region = MSM_VIDC_SECURE_BITSTREAM;
+		break;
+	case MSM_VIDC_BUF_OUTPUT:
+		if (is_encode_session(inst))
+			region = MSM_VIDC_SECURE_BITSTREAM;
+		else
+			region = MSM_VIDC_SECURE_PIXEL;
+		break;
+	case MSM_VIDC_BUF_INPUT_META:
+	case MSM_VIDC_BUF_OUTPUT_META:
+		region = MSM_VIDC_NON_SECURE;
+		break;
+	case MSM_VIDC_BUF_SCRATCH:
+		region = MSM_VIDC_SECURE_BITSTREAM;
+		break;
+	case MSM_VIDC_BUF_SCRATCH_1:
+		region = MSM_VIDC_SECURE_NONPIXEL;
+		break;
+	case MSM_VIDC_BUF_SCRATCH_2:
+		region = MSM_VIDC_SECURE_PIXEL;
+		break;
+	case MSM_VIDC_BUF_PERSIST:
+		if (is_encode_session(inst))
+			region = MSM_VIDC_SECURE_NONPIXEL;
+		else
+			region = MSM_VIDC_SECURE_BITSTREAM;
+		break;
+	case MSM_VIDC_BUF_PERSIST_1:
+		region = MSM_VIDC_SECURE_NONPIXEL;
+		break;
+	default:
+		s_vpr_e(inst->sid, "%s: invalid buffer type %d\n",
+			__func__, buffer_type);
+	}
+	return region;
+}
+
+struct msm_vidc_buffer_info *msm_vidc_get_buffer_info(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type)
+{
+	switch (buffer_type) {
+	case MSM_VIDC_BUF_INPUT:
+		return &inst->buffers.input;
+	case MSM_VIDC_BUF_INPUT_META:
+		return &inst->buffers.input_meta;
+	case MSM_VIDC_BUF_OUTPUT:
+		return &inst->buffers.output;
+	case MSM_VIDC_BUF_OUTPUT_META:
+		return &inst->buffers.output_meta;
+	case MSM_VIDC_BUF_SCRATCH:
+		return &inst->buffers.scratch;
+	case MSM_VIDC_BUF_SCRATCH_1:
+		return &inst->buffers.scratch_1;
+	case MSM_VIDC_BUF_SCRATCH_2:
+		return &inst->buffers.scratch_2;
+	case MSM_VIDC_BUF_PERSIST:
+		return &inst->buffers.persist;
+	case MSM_VIDC_BUF_PERSIST_1:
+		return &inst->buffers.persist_1;
+	default:
+		s_vpr_e(inst->sid, "%s: invalid buffer type %d\n",
+			__func__, buffer_type);
+		return NULL;
+	}
+}
+
+struct msm_vidc_map_info *msm_vidc_get_map_info(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type)
+{
+	switch (buffer_type) {
+	case MSM_VIDC_BUF_INPUT:
+		return &inst->maps.input;
+	case MSM_VIDC_BUF_INPUT_META:
+		return &inst->maps.input_meta;
+	case MSM_VIDC_BUF_OUTPUT:
+		return &inst->maps.output;
+	case MSM_VIDC_BUF_OUTPUT_META:
+		return &inst->maps.output_meta;
+	case MSM_VIDC_BUF_SCRATCH:
+		return &inst->maps.scratch;
+	case MSM_VIDC_BUF_SCRATCH_1:
+		return &inst->maps.scratch_1;
+	case MSM_VIDC_BUF_SCRATCH_2:
+		return &inst->maps.scratch_2;
+	case MSM_VIDC_BUF_PERSIST:
+		return &inst->maps.persist;
+	case MSM_VIDC_BUF_PERSIST_1:
+		return &inst->maps.persist_1;
+	default:
+		s_vpr_e(inst->sid, "%s: invalid buffer type %d\n",
+			__func__, buffer_type);
+		return NULL;
+	}
+}
+
+struct msm_vidc_alloc_info *msm_vidc_get_alloc_info(struct msm_vidc_inst *inst,
+	enum msm_vidc_buffer_type buffer_type)
+{
+	switch (buffer_type) {
+	case MSM_VIDC_BUF_SCRATCH:
+		return &inst->allocations.scratch;
+	case MSM_VIDC_BUF_SCRATCH_1:
+		return &inst->allocations.scratch_1;
+	case MSM_VIDC_BUF_SCRATCH_2:
+		return &inst->allocations.scratch_2;
+	case MSM_VIDC_BUF_PERSIST:
+		return &inst->allocations.persist;
+	case MSM_VIDC_BUF_PERSIST_1:
+		return &inst->allocations.persist_1;
+	default:
+		s_vpr_e(inst->sid, "%s: invalid buffer type %d\n",
+			__func__, buffer_type);
+		return NULL;
+	}
+}
+
+int msm_vidc_change_inst_state(struct msm_vidc_inst *inst,
+		enum msm_vidc_inst_state request_state)
+{
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+	if (!request_state) {
+		d_vpr_e("%s: invalid request state\n", __func__);
+		return -EINVAL;
+	}
+
+	if (inst->state == MSM_VIDC_ERROR) {
+		s_vpr_h(inst->sid,
+			"inst is in bad state, can not change state to %d\n",
+			request_state);
+		return 0;
+	}
+
+	s_vpr_h(inst->sid, "state changed from %d to %d\n",
+		   inst->state, request_state);
+	inst->state = request_state;
+	return 0;
+}
+
 int msm_vidc_get_control(struct msm_vidc_inst *inst, struct v4l2_ctrl *ctrl)
 {
 	int rc = 0;
@@ -84,72 +244,226 @@ u32 msm_vidc_convert_color_fmt(u32 v4l2_fmt)
 	}
 }
 
-int msm_vidc_stop_input(struct msm_vidc_inst *inst)
+int msm_vidc_create_internal_buffers(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type)
 {
 	int rc = 0;
+	struct msm_vidc_buffer_info *buffer_info;
+	struct msm_vidc_alloc_info *alloc_info;
+	struct msm_vidc_map_info *map_info;
+	int i;
 
 	d_vpr_h("%s()\n", __func__);
-	if (!inst) {
+	if (!inst || !inst->core) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
+	if (!is_internal_buffer(buffer_type)) {
+		s_vpr_e(inst->sid, "%s: buffer type %#d is not internal\n",
+			__func__, buffer_type);
+		return 0;
+	}
 
-	return rc;
+	buffer_info = msm_vidc_get_buffer_info(inst, buffer_type);
+	if (!buffer_info)
+		return -EINVAL;
+	alloc_info = msm_vidc_get_alloc_info(inst, buffer_type);
+	if (!alloc_info)
+		return -EINVAL;
+	map_info = msm_vidc_get_map_info(inst, buffer_type);
+	if (!alloc_info)
+		return -EINVAL;
+
+	for (i = 0; i < buffer_info->min_count; i++) {
+		struct msm_vidc_buffer *buffer;
+		struct msm_vidc_alloc *alloc;
+		struct msm_vidc_map *map;
+
+		if (!buffer_info->size) {
+			d_vpr_e("%s: invalid buffer %#x\n", __func__, buffer_type);
+			return -EINVAL;
+		}
+		buffer = kzalloc(sizeof(struct msm_vidc_buffer), GFP_KERNEL);
+		if (!buffer) {
+			s_vpr_e(inst->sid, "%s: msm_vidc_buffer alloc failed\n", __func__);
+			return -ENOMEM;
+		}
+		INIT_LIST_HEAD(&buffer->list);
+		buffer->valid = true;
+		buffer->type = buffer_type;
+		buffer->index = i;
+		buffer->buffer_size = buffer_info->size;
+		list_add_tail(&buffer->list, &buffer_info->list);
+
+		alloc = kzalloc(sizeof(struct msm_vidc_alloc), GFP_KERNEL);
+		if (!alloc) {
+			s_vpr_e(inst->sid, "%s: msm_vidc_alloc alloc failed\n", __func__);
+			return -ENOMEM;
+		}
+		INIT_LIST_HEAD(&alloc->list);
+		alloc->buffer_type = buffer_type;
+		alloc->region      = msm_vidc_get_buffer_region(inst, buffer_type);
+		alloc->size        = buffer->buffer_size;
+		rc = msm_vidc_memory_alloc(inst->core, alloc);
+		if (rc)
+			return -ENOMEM;
+		list_add_tail(&alloc->list, &alloc_info->list);
+
+		map = kzalloc(sizeof(struct msm_vidc_map), GFP_KERNEL);
+		if (!map) {
+			s_vpr_e(inst->sid, "%s: msm_vidc_map alloc failed\n", __func__);
+			return -ENOMEM;
+		}
+		INIT_LIST_HEAD(&map->list);
+		map->buffer_type  = alloc->buffer_type;
+		map->region       = alloc->region;
+		map->dmabuf       = alloc->dmabuf;
+		rc = msm_vidc_memory_map(inst->core, map);
+		if (rc)
+			return -ENOMEM;
+		list_add_tail(&map->list, &map_info->list);
+
+		s_vpr_e(inst->sid, "%s: created buffer_type %d, size %d\n",
+			__func__, buffer_type, buffer_info->size);
+	}
+
+	return 0;
 }
 
-int msm_vidc_stop_output(struct msm_vidc_inst *inst)
+int msm_vidc_queue_internal_buffers(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type)
 {
 	int rc = 0;
+	struct msm_vidc_buffer_info *buffer_info;
+	struct msm_vidc_buffer *buffer, *dummy;
 
 	d_vpr_h("%s()\n", __func__);
-	if (!inst) {
+	if (!inst || !inst->core) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
+	if (!is_internal_buffer(buffer_type)) {
+		s_vpr_e(inst->sid, "%s: buffer type %#d is not internal\n",
+			__func__, buffer_type);
+		return 0;
+	}
+
+	buffer_info = msm_vidc_get_buffer_info(inst, buffer_type);
+	if (!buffer_info)
+		return -EINVAL;
+
+	list_for_each_entry_safe(buffer, dummy, &buffer_info->list, list) {
+		/* do not queue pending release buffers */
+		if (buffer->flags & MSM_VIDC_ATTR_PENDING_RELEASE)
+			continue;
+		/* do not queue already queued buffers */
+		if (buffer->attr & MSM_VIDC_ATTR_QUEUED)
+			continue;
+		rc = venus_hfi_queue_buffer(inst, buffer, NULL);
+		if (rc)
+			return rc;
+		/* mark queued */
+		buffer->attr |= MSM_VIDC_ATTR_QUEUED;
+
+		s_vpr_e(inst->sid, "%s: queued buffer_type %d, size %d\n",
+			__func__, buffer_type, buffer_info->size);
+	}
 
-	return rc;
+	return 0;
 }
 
-int msm_vidc_start_input(struct msm_vidc_inst *inst)
+int msm_vidc_release_internal_buffers(struct msm_vidc_inst *inst,
+		enum msm_vidc_buffer_type buffer_type)
 {
 	int rc = 0;
+	struct msm_vidc_buffer_info *buffer_info;
+	struct msm_vidc_buffer *buffer, *dummy;
 
 	d_vpr_h("%s()\n", __func__);
-	if (!inst) {
+	if (!inst || !inst->core) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
+	if (!is_internal_buffer(buffer_type)) {
+		s_vpr_e(inst->sid, "%s: buffer type %#d is not internal\n",
+			__func__, buffer_type);
+		return 0;
+	}
 
-	return rc;
+	buffer_info = msm_vidc_get_buffer_info(inst, buffer_type);
+	if (!buffer_info)
+		return -EINVAL;
+
+	list_for_each_entry_safe(buffer, dummy, &buffer_info->list, list) {
+		/* do not release already pending release buffers */
+		if (buffer->attr & MSM_VIDC_ATTR_PENDING_RELEASE)
+			continue;
+		/* release only queued buffers */
+		if (!(buffer->attr & MSM_VIDC_ATTR_QUEUED))
+			continue;
+		rc = venus_hfi_release_buffer(inst, buffer);
+		if (rc)
+			return rc;
+		/* mark pending release */
+		buffer->attr |= MSM_VIDC_ATTR_PENDING_RELEASE;
+
+		s_vpr_e(inst->sid, "%s: released buffer_type %d, size %d\n",
+			__func__, buffer_type, buffer_info->size);
+	}
+
+	return 0;
 }
 
-int msm_vidc_start_output(struct msm_vidc_inst *inst)
+int msm_vidc_setup_event_queue(struct msm_vidc_inst *inst)
 {
 	int rc = 0;
+	int index;
+	struct msm_vidc_core *core;
 
 	d_vpr_h("%s()\n", __func__);
-	if (!inst) {
+	if (!inst || !inst->core) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
+	core = inst->core;
+
+	// TODO: check decode is index = 0 and encode is index 1
+	if (is_decode_session(inst))
+		index = 0;
+	else if (is_encode_session(inst))
+		index = 1;
+	else
+		return -EINVAL;
+
+	v4l2_fh_init(&inst->event_handler, &core->vdev[index].vdev);
+	v4l2_fh_add(&inst->event_handler);
 
 	return rc;
 }
 
-int msm_vidc_setup_event_queue(struct msm_vidc_inst *inst)
+static int vb2q_init(struct msm_vidc_inst *inst,
+	struct vb2_queue *q, enum v4l2_buf_type type)
 {
-	int rc = 0;
+	struct msm_vidc_core *core;
 
-	d_vpr_h("%s()\n", __func__);
-	if (!inst) {
+	if (!inst || !q || !inst->core) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
+	core = inst->core;
 
-	return rc;
+	q->type = type;
+	q->io_modes = VB2_MMAP | VB2_USERPTR;
+	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	q->ops = core->vb2_ops;
+	q->mem_ops = core->vb2_mem_ops;
+	q->drv_priv = inst;
+	q->allow_zero_bytesused = 1;
+	q->copy_timestamp = 1;
+	return vb2_queue_init(q);
 }
 
-int msm_vidc_queue_init(struct msm_vidc_inst *inst)
+int msm_vidc_vb2_queue_init(struct msm_vidc_inst *inst)
 {
 	int rc = 0;
 
@@ -159,6 +473,22 @@ int msm_vidc_queue_init(struct msm_vidc_inst *inst)
 		return -EINVAL;
 	}
 
+	rc = vb2q_init(inst, &inst->vb2q[INPUT_PORT], INPUT_PLANE);
+	if (rc)
+		return rc;
+
+	rc = vb2q_init(inst, &inst->vb2q[OUTPUT_PORT], OUTPUT_PLANE);
+	if (rc)
+		return rc;
+
+	rc = vb2q_init(inst, &inst->vb2q[INPUT_META_PORT], INPUT_META_PLANE);
+	if (rc)
+		return rc;
+
+	rc = vb2q_init(inst, &inst->vb2q[OUTPUT_META_PORT], OUTPUT_META_PLANE);
+	if (rc)
+		return rc;
+
 	return rc;
 }
 
@@ -195,6 +525,23 @@ int msm_vidc_add_session(struct msm_vidc_inst *inst)
 	return rc;
 }
 
+int msm_vidc_session_open(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	rc = venus_hfi_session_open(inst);
+	if (rc)
+		return rc;
+
+	inst->session_created = true;
+	return 0;
+}
+
 int msm_vidc_core_init(struct msm_vidc_core *core)
 {
 	int rc;

+ 23 - 4
driver/vidc/src/msm_vidc_vb2.c

@@ -8,6 +8,8 @@
 #include "msm_vidc_inst.h"
 #include "msm_vidc_internal.h"
 #include "msm_vidc_driver.h"
+#include "msm_vdec.h"
+#include "msm_venc.h"
 #include "msm_vidc_debug.h"
 
 void *msm_vb2_get_userptr(struct device *dev, unsigned long vaddr,
@@ -44,7 +46,7 @@ int msm_vidc_queue_setup(struct vb2_queue *q,
 		return -EINVAL;
 	}
 
-	port = msm_vidc_get_port_from_type(q->type);
+	port = msm_vidc_get_port_from_v4l2_type(q->type);
 	if (port < 0) {
 		d_vpr_e("%s: invalid queue type %d\n", __func__, q->type);
 		return -EINVAL;
@@ -96,12 +98,29 @@ int msm_vidc_start_streaming(struct vb2_queue *q, unsigned int count)
 			__func__, q->type);
 		return 0;
 	}
+	if (!is_decode_session(inst) && !is_encode_session(inst)) {
+		s_vpr_e(inst->sid, "%s: invalid session %d\n",
+			__func__, inst->domain);
+		return -EINVAL;
+	}
 	s_vpr_h(inst->sid, "Streamon: %d\n", q->type);
 
+	if (!inst->session_created) {
+		rc = msm_vidc_session_open(inst);
+		if (rc)
+			return -EINVAL;
+	}
+
 	if (q->type == INPUT_PLANE) {
-		rc = msm_vidc_start_input(inst);
-	} else if (q->type == INPUT_PLANE) {
-		rc = msm_vidc_start_output(inst);
+		if (is_decode_session(inst))
+			rc = msm_vdec_start_input(inst);
+		//else if (is_encode_session(inst))
+		//	rc = msm_venc_start_input(inst);
+	} else if (q->type == OUTPUT_PLANE) {
+		if (is_decode_session(inst))
+			rc = msm_vdec_start_output(inst);
+		//else if (is_encode_session(inst))
+		//	rc = msm_venc_start_output(inst);
 	} else {
 		s_vpr_e(inst->sid, "%s: invalid type %d\n", __func__, q->type);
 		rc = -EINVAL;

+ 324 - 15
driver/vidc/src/venus_hfi.c

@@ -20,6 +20,7 @@
 #include "msm_vidc_dt.h"
 #include "msm_vidc_platform.h"
 #include "msm_vidc_memory.h"
+#include "msm_vidc_driver.h"
 #include "msm_vidc_debug.h"
 #include "hfi_packet.h"
 
@@ -561,6 +562,10 @@ static int __write_queue(struct msm_vidc_iface_q_info *qinfo, u8 *packet,
 		__dump_packet(packet);
 	}
 
+	// TODO: handle writing packet
+	d_vpr_e("skip writing packet\n");
+	return 0;
+
 	packet_size_in_words = (*(u32 *)packet) >> 2;
 	if (!packet_size_in_words || packet_size_in_words >
 		qinfo->q_array.mem_size>>2) {
@@ -2005,7 +2010,7 @@ static int __interface_queues_init(struct msm_vidc_core *core)
 	q_size = SHARED_QSIZE - ALIGNED_SFR_SIZE - ALIGNED_QDSS_SIZE;
 
 	memset(&alloc, 0, sizeof(alloc));
-	alloc.buffer_type = MSM_VIDC_QUEUE;
+	alloc.buffer_type = MSM_VIDC_BUF_QUEUE;
 	alloc.region     = MSM_VIDC_NON_SECURE;
 	alloc.size       = q_size;
 	alloc.cached     = false;
@@ -2078,7 +2083,7 @@ static int __interface_queues_init(struct msm_vidc_core *core)
 
 	/* sfr buffer */
 	memset(&alloc, 0, sizeof(alloc));
-	alloc.buffer_type = MSM_VIDC_QUEUE;
+	alloc.buffer_type = MSM_VIDC_BUF_QUEUE;
 	alloc.region     = MSM_VIDC_NON_SECURE;
 	alloc.size       = ALIGNED_SFR_SIZE;
 	alloc.cached     = false;
@@ -2339,15 +2344,14 @@ int venus_hfi_suspend(struct msm_vidc_core *core)
 	return rc;
 }
 
-int venus_hfi_session_open(struct msm_vidc_core *core,
-	struct msm_vidc_inst *inst)
+int venus_hfi_session_open(struct msm_vidc_inst *inst)
 {
 	int rc = 0;
+	u32 codec;
 
-	d_vpr_h("%s(): inst %p, core %p\n",
-		__func__, inst, core);
+	d_vpr_h("%s(): inst %p %p\n", __func__, inst);
 
-	if (!core || !inst) {
+	if (!inst) {
 		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
@@ -2359,14 +2363,319 @@ int venus_hfi_session_open(struct msm_vidc_core *core,
 		return -ENOMEM;
 	}
 	rc = hfi_packet_session_command(inst,
-					HFI_CMD_OPEN,
-					(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
-					HFI_HOST_FLAGS_INTR_REQUIRED),
-					HFI_PORT_NONE,
-					0 /* session_id */,
-					HFI_PAYLOAD_U32,
-					&inst->session_id /*payload*/,
-					sizeof(u32));
+				HFI_CMD_OPEN,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED),
+				HFI_PORT_NONE,
+				0, /* session_id */
+				HFI_PAYLOAD_U32,
+				&inst->session_id, /* payload */
+				sizeof(u32));
+	if (rc)
+		goto error;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		goto error;
+
+	codec = get_hfi_codec(inst);
+	rc = hfi_packet_session_property(inst,
+				HFI_PROP_CODEC,
+				HFI_HOST_FLAGS_NONE,
+				HFI_PORT_NONE,
+				HFI_PAYLOAD_U32,
+				&codec,
+				sizeof(u32));
+	if (rc)
+		goto error;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		goto error;
+
+	inst->session_created = true;
+error:
+	return rc;
+}
+
+int venus_hfi_session_close(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+
+	if (!inst || !inst->packet) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	rc = hfi_packet_session_command(inst,
+				HFI_CMD_CLOSE,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED |
+				HFI_HOST_FLAGS_NON_DISCARDABLE),
+				HFI_PORT_NONE,
+				inst->session_id,
+				HFI_PAYLOAD_NONE,
+				NULL,
+				0);
+	if (!rc)
+		__iface_cmdq_write(inst->core, inst->packet);
+
+	kfree(inst->packet);
+
+		return rc;
+}
+
+int venus_hfi_start_input(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	u32 port;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	port = is_decode_session(inst) ? HFI_PORT_BITSTREAM : HFI_PORT_RAW;
+	rc = hfi_packet_session_command(inst,
+				HFI_CMD_START,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED),
+				port,
+				inst->session_id,
+				HFI_PAYLOAD_NONE,
+				NULL,
+				0);
+	if (rc)
+		return rc;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
+int venus_hfi_stop_input(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	u32 port;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	port = is_decode_session(inst) ? HFI_PORT_BITSTREAM : HFI_PORT_RAW;
+	rc = hfi_packet_session_command(inst,
+				HFI_CMD_STOP,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED |
+				HFI_HOST_FLAGS_NON_DISCARDABLE),
+				port,
+				inst->session_id,
+				HFI_PAYLOAD_NONE,
+				NULL,
+				0);
+	if (rc)
+		return rc;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
+int venus_hfi_start_output(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	u32 port;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	port = is_decode_session(inst) ? HFI_PORT_RAW : HFI_PORT_BITSTREAM;
+	rc = hfi_packet_session_command(inst,
+				HFI_CMD_START,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED),
+				port,
+				inst->session_id,
+				HFI_PAYLOAD_NONE,
+				NULL,
+				0);
+	if (rc)
+		return rc;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
+int venus_hfi_stop_output(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	u32 port;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	port = is_decode_session(inst) ? HFI_PORT_RAW : HFI_PORT_BITSTREAM;
+	rc = hfi_packet_session_command(inst,
+				HFI_CMD_STOP,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED |
+				HFI_HOST_FLAGS_NON_DISCARDABLE),
+				port,
+				inst->session_id,
+				HFI_PAYLOAD_NONE,
+				NULL,
+				0);
+	if (rc)
+		return rc;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
+int venus_hfi_queue_buffer(struct msm_vidc_inst *inst,
+	struct msm_vidc_buffer *buffer, struct msm_vidc_buffer *metabuf)
+{
+	int rc = 0;
+	struct msm_vidc_core *core;
+	struct hfi_buffer hfi_buffer;
+	u32 num_packets = 0, offset = 0;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+	if (!inst || !inst->core || !inst->packet) {
+		d_vpr_e("%s: Invalid params\n", __func__);
+		return -EINVAL;
+	}
+	core = inst->core;
+
+	rc = get_hfi_buffer(inst, buffer, &hfi_buffer);
+	if (rc)
+		return rc;
+
+	offset = sizeof(struct hfi_header);
+	rc = hfi_create_packet(inst->packet,
+				inst->packet_size,
+				&offset,
+				HFI_CMD_BUFFER,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED),
+				HFI_PAYLOAD_STRUCTURE,
+				get_hfi_port(inst, buffer->type),
+				core->packet_id++,
+				&hfi_buffer,
+				sizeof(hfi_buffer));
+	if (rc)
+		return rc;
+	num_packets++;
+
+	if (metabuf) {
+		rc = get_hfi_buffer(inst, metabuf, &hfi_buffer);
+		if (rc)
+			return rc;
+		rc = hfi_create_packet(inst->packet,
+				inst->packet_size,
+				&offset,
+				HFI_CMD_BUFFER,
+				HFI_HOST_FLAGS_NONE,
+				HFI_PAYLOAD_STRUCTURE,
+				get_hfi_port(inst, metabuf->type),
+				core->packet_id++,
+				&hfi_buffer,
+				sizeof(hfi_buffer));
+		if (rc)
+			return rc;
+		num_packets++;
+	}
+
+	rc = hfi_create_header(inst->packet, inst->session_id,
+			   core->header_id++,
+			   num_packets,
+			   offset);
+	if (rc)
+		return rc;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
+int venus_hfi_release_buffer(struct msm_vidc_inst *inst,
+	struct msm_vidc_buffer *buffer)
+{
+	int rc = 0;
+	struct msm_vidc_core *core;
+	struct hfi_buffer hfi_buffer;
+	u32 num_packets = 0, offset = 0;
+
+	d_vpr_h("%s(): inst %p\n", __func__, inst);
+	if (!inst || !buffer) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+	if (!is_internal_buffer(buffer->type)) {
+		s_vpr_e(inst->sid, "release not allowed for buffer type %d\n",
+			buffer->type);
+		return 0;
+	}
+	core = inst->core;
+
+	rc = get_hfi_buffer(inst, buffer, &hfi_buffer);
+	if (rc)
+		return rc;
+
+	/* add pending release flag */
+	hfi_buffer.flags |= HFI_BUF_HOST_FLAG_RELEASE;
+
+	offset = sizeof(struct hfi_header);
+	rc = hfi_create_packet(inst->packet,
+				inst->packet_size,
+				&offset,
+				HFI_CMD_BUFFER,
+				(HFI_HOST_FLAGS_RESPONSE_REQUIRED |
+				HFI_HOST_FLAGS_INTR_REQUIRED),
+				HFI_PAYLOAD_STRUCTURE,
+				get_hfi_port(inst, buffer->type),
+				core->packet_id++,
+				&hfi_buffer,
+				sizeof(hfi_buffer));
+	if (rc)
+		return rc;
+	num_packets++;
+
+	rc = hfi_create_header(inst->packet, inst->session_id,
+			   core->header_id++,
+			   num_packets,
+			   offset);
+	if (rc)
+		return rc;
+
+	rc = __iface_cmdq_write(inst->core, inst->packet);
 	if (rc)
 		return rc;