Bladeren bron

video: driver: add power changes

Add or amend below functionalities
- scale clocks
- scale buses
- buffer counts
- buffer sizes

Change-Id: I3cddbb7112cd85a84cf86256134059f52b66a3af
Signed-off-by: Maheshwar Ajja <[email protected]>
Maheshwar Ajja 4 jaren geleden
bovenliggende
commit
7509e9f16b

+ 4 - 2
driver/variant/iris2/inc/msm_vidc_power_iris2.h

@@ -6,9 +6,11 @@
 #ifndef __H_MSM_VIDC_POWER_IRIS2_H__
 #define __H_MSM_VIDC_POWER_IRIS2_H__
 
+#include "msm_vidc_power.h"
 #include "msm_vidc_inst.h"
 
-u64 msm_vidc_calc_freq_iris2(struct msm_vidc_inst* inst);
-u64 msm_vidc_calc_bw_iris2(struct msm_vidc_inst* inst);
+u64 msm_vidc_calc_freq_iris2(struct msm_vidc_inst* inst, u32 data_size);
+int msm_vidc_calc_bw_iris2(struct msm_vidc_inst* inst,
+		struct vidc_bus_vote_data* vote_data);
 
 #endif

+ 2 - 2
driver/variant/iris2/src/msm_vidc_iris2.c

@@ -500,8 +500,8 @@ static struct msm_vidc_session_ops msm_session_ops = {
 	.buffer_size = msm_buffer_size_iris2,
 	.min_count = msm_buffer_min_count_iris2,
 	.extra_count = msm_buffer_extra_count_iris2,
-	.calc_freq = NULL,
-	.calc_bw = NULL,
+	.calc_freq = msm_vidc_calc_freq_iris2,
+	.calc_bw = msm_vidc_calc_bw_iris2,
 	.decide_work_route = NULL,
 	.decide_work_mode = NULL,
 	.decide_core_and_power_mode = NULL,

+ 659 - 10
driver/variant/iris2/src/msm_vidc_power_iris2.c

@@ -5,26 +5,675 @@
 
 #include "msm_vidc_power_iris2.h"
 #include "msm_vidc_inst.h"
+#include "msm_vidc_core.h"
+#include "msm_vidc_driver.h"
 #include "msm_vidc_debug.h"
 
-u64 msm_vidc_calc_freq_iris2(struct msm_vidc_inst *inst)
+u64 msm_vidc_calc_freq_iris2(struct msm_vidc_inst *inst, u32 data_size)
 {
 	u64 freq = 0;
+	struct msm_vidc_core* core;
+	struct msm_vidc_power* power;
+	u64 vsp_cycles = 0, vpp_cycles = 0, fw_cycles = 0;
+	u64 fw_vpp_cycles = 0;
+	u32 vpp_cycles_per_mb;
+	u32 mbs_per_second;
+	u32 operating_rate, vsp_factor_num = 1, vsp_factor_den = 1;
+	u32 base_cycles = 0;
+	u32 fps;
 
-	/* 240 Mhz for iris2 based video hw */
-	freq = 240 * 1000 * 1000;
-	s_vpr_h(inst->sid, "%s: freq %lu\n", __func__, freq);
+	if (!inst || !inst->core || !inst->capabilities) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return freq;
+	}
+	core = inst->core;
+	power = &inst->power;
+
+	mbs_per_second = msm_vidc_get_inst_load(inst, LOAD_POWER);
+	fps = msm_vidc_get_fps(inst);
+
+	/*
+	 * Calculate vpp, vsp, fw cycles separately for encoder and decoder.
+	 * Even though, most part is common now, in future it may change
+	 * between them.
+	 */
+	fw_cycles = fps * core->capabilities[MB_CYCLES_FW].value;
+	fw_vpp_cycles = fps * core->capabilities[MB_CYCLES_FW_VPP].value;
+
+	if (inst->domain == MSM_VIDC_ENCODER) {
+		vpp_cycles_per_mb = inst->flags & VIDC_LOW_POWER ?
+			core->capabilities[MB_CYCLES_LP].value :
+			core->capabilities[MB_CYCLES_VPP].value;
+
+		vpp_cycles = mbs_per_second * vpp_cycles_per_mb /
+			inst->capabilities->cap[PIPE].value;
+		/* 1.25 factor for IBP GOP structure */
+		if (inst->capabilities->cap[B_FRAME].value)
+			vpp_cycles += vpp_cycles / 4;
+		/* 21 / 20 is minimum overhead factor */
+		vpp_cycles += max(div_u64(vpp_cycles, 20), fw_vpp_cycles);
+		/* 1.01 is multi-pipe overhead */
+		if (inst->capabilities->cap[PIPE].value > 1)
+			vpp_cycles += div_u64(vpp_cycles, 100);
+		/*
+		 * 1080p@480fps usecase needs exactly 338MHz
+		 * without any margin left. Hence, adding 2 percent
+		 * extra to bump it to next level (366MHz).
+		 */
+		if (fps == 480)
+			vpp_cycles += div_u64(vpp_cycles * 2, 100);
+
+		/* VSP */
+		/* bitrate is based on fps, scale it using operating rate */
+		operating_rate = inst->prop.operating_rate >> 16;
+		if (operating_rate > (inst->prop.frame_rate >> 16) &&
+			(inst->prop.frame_rate >> 16)) {
+			vsp_factor_num = operating_rate;
+			vsp_factor_den = inst->prop.frame_rate >> 16;
+		}
+		vsp_cycles = div_u64(((u64)inst->prop.bitrate *
+					vsp_factor_num), vsp_factor_den);
+
+		base_cycles = core->capabilities[MB_CYCLES_VSP].value;
+		if (inst->codec == MSM_VIDC_VP9) {
+			vsp_cycles = div_u64(vsp_cycles * 170, 100);
+		} else if (inst->capabilities->cap[ENTROPY_MODE].value ==
+			V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC) {
+			vsp_cycles = div_u64(vsp_cycles * 135, 100);
+		} else {
+			base_cycles = 0;
+			vsp_cycles = div_u64(vsp_cycles, 2);
+			/* VSP FW Overhead 1.05 */
+			vsp_cycles = div_u64(vsp_cycles * 21, 20);
+		}
+
+		if (inst->capabilities->cap[STAGE].value == MSM_VIDC_STAGE_1)
+			vsp_cycles = vsp_cycles * 3;
+
+		vsp_cycles += mbs_per_second * base_cycles;
+
+	} else if (inst->domain == MSM_VIDC_DECODER) {
+		/* VPP */
+		vpp_cycles = mbs_per_second * core->capabilities[MB_CYCLES_VPP].value /
+			inst->capabilities->cap[PIPE].value;
+		/* 21 / 20 is minimum overhead factor */
+		vpp_cycles += max(vpp_cycles / 20, fw_vpp_cycles);
+		/* 1.059 is multi-pipe overhead */
+		if (inst->capabilities->cap[PIPE].value > 1)
+			vpp_cycles += div_u64(vpp_cycles * 59, 1000);
+
+		/* VSP */
+		base_cycles = core->capabilities[MB_CYCLES_VSP].value;
+		vsp_cycles = fps * data_size * 8;
+
+		if (inst->codec == MSM_VIDC_VP9) {
+			vsp_cycles = div_u64(vsp_cycles * 170, 100);
+		} else if (inst->capabilities->cap[ENTROPY_MODE].value ==
+			V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC) {
+			vsp_cycles = div_u64(vsp_cycles * 135, 100);
+		} else {
+			base_cycles = 0;
+			vsp_cycles = div_u64(vsp_cycles, 2);
+			/* VSP FW overhead 1.05 */
+			vsp_cycles = div_u64(vsp_cycles * 21, 20);
+		}
+
+		if (inst->capabilities->cap[STAGE].value == MSM_VIDC_STAGE_1)
+			vsp_cycles = vsp_cycles * 3;
+
+		vsp_cycles += mbs_per_second * base_cycles;
+
+	} else {
+		s_vpr_e(inst->sid, "%s: Unknown session type\n", __func__);
+		return msm_vidc_max_freq(inst);
+	}
+
+	freq = max(vpp_cycles, vsp_cycles);
+	freq = max(freq, fw_cycles);
+
+	s_vpr_p(inst->sid, "%s: inst %pK: filled len %d required freq %llu\n",
+		__func__, inst, data_size, freq);
 
 	return freq;
 }
 
-u64 msm_vidc_calc_bw_iris2(struct msm_vidc_inst *inst)
+static u64 __calculate_decoder(struct vidc_bus_vote_data *d)
 {
-	u64 freq = 0;
+	/*
+	 * XXX: Don't fool around with any of the hardcoded numbers unless you
+	 * know /exactly/ what you're doing.  Many of these numbers are
+	 * measured heuristics and hardcoded numbers taken from the firmware.
+	 */
+	/* Decoder parameters */
+	int width, height, lcu_size, fps, dpb_bpp;
+	bool unified_dpb_opb, dpb_compression_enabled = true,
+		opb_compression_enabled = false,
+		llc_ref_read_l2_cache_enabled = false,
+		llc_top_line_buf_enabled = false;
+	fp_t dpb_read_compression_factor, dpb_opb_scaling_ratio,
+		dpb_write_compression_factor, opb_write_compression_factor,
+		qsmmu_bw_overhead_factor;
+	bool is_h264_category = true;
 
-	/* 600 Mhz for iris2 based video hw */
-	freq = 600 * 1000 * 1000;
-	s_vpr_h(inst->sid, "%s: freq %lu\n", __func__, freq);
+	/* Derived parameters */
+	int lcu_per_frame, collocated_bytes_per_lcu, tnbr_per_lcu;
+	unsigned long bitrate;
+	unsigned int num_vpp_pipes;
 
-	return freq;
+	fp_t bins_to_bit_factor, vsp_read_factor, vsp_write_factor,
+		dpb_factor, dpb_write_factor, y_bw_no_ubwc_8bpp;
+	fp_t y_bw_no_ubwc_10bpp = 0, y_bw_10bpp_p010 = 0,
+	     motion_vector_complexity = 0;
+	fp_t	dpb_total = 0;
+
+	/* Output parameters */
+	struct {
+		fp_t vsp_read, vsp_write, collocated_read, collocated_write,
+			dpb_read, dpb_write, opb_read, opb_write,
+			line_buffer_read, line_buffer_write,
+			total;
+	} ddr = {0};
+
+	struct {
+		fp_t dpb_read, line_buffer_read, line_buffer_write, total;
+	} llc = {0};
+
+	unsigned long ret = 0;
+	unsigned int integer_part, frac_part;
+
+	width = max(d->input_width, BASELINE_DIMENSIONS.width);
+	height = max(d->input_height, BASELINE_DIMENSIONS.height);
+
+	fps = d->fps;
+
+	lcu_size = d->lcu_size;
+
+	dpb_bpp = __bpp(d->color_formats[0]);
+
+	unified_dpb_opb = d->num_formats == 1;
+
+	dpb_opb_scaling_ratio = fp_div(FP_INT(d->input_width * d->input_height),
+		FP_INT(d->output_width * d->output_height));
+
+	opb_compression_enabled = d->num_formats >= 2 &&
+		__ubwc(d->color_formats[1]);
+
+	integer_part = Q16_INT(d->compression_ratio);
+	frac_part = Q16_FRAC(d->compression_ratio);
+	dpb_read_compression_factor = FP(integer_part, frac_part, 100);
+
+	integer_part = Q16_INT(d->complexity_factor);
+	frac_part = Q16_FRAC(d->complexity_factor);
+	motion_vector_complexity = FP(integer_part, frac_part, 100);
+
+	dpb_write_compression_factor = dpb_read_compression_factor;
+	opb_write_compression_factor = opb_compression_enabled ?
+		dpb_write_compression_factor : FP_ONE;
+
+	num_vpp_pipes = d->num_vpp_pipes;
+
+	if (d->codec == MSM_VIDC_HEVC ||
+		d->codec == MSM_VIDC_VP9) {
+		/* H264, VP8, MPEG2 use the same settings */
+		/* HEVC, VP9 use the same setting */
+		is_h264_category = false;
+	}
+	if (d->use_sys_cache) {
+		llc_ref_read_l2_cache_enabled = true;
+		if (is_h264_category)
+			llc_top_line_buf_enabled = true;
+	}
+
+	/* Derived parameters setup */
+	lcu_per_frame = DIV_ROUND_UP(width, lcu_size) *
+		DIV_ROUND_UP(height, lcu_size);
+
+	bitrate = DIV_ROUND_UP(d->bitrate, 1000000);
+
+	bins_to_bit_factor = FP_INT(4);
+
+	vsp_write_factor = bins_to_bit_factor;
+	vsp_read_factor = bins_to_bit_factor + FP_INT(2);
+
+	collocated_bytes_per_lcu = lcu_size == 16 ? 16 :
+				lcu_size == 32 ? 64 : 256;
+
+	dpb_factor = FP(1, 50, 100);
+	dpb_write_factor = FP(1, 5, 100);
+
+	tnbr_per_lcu = lcu_size == 16 ? 128 :
+		lcu_size == 32 ? 64 : 128;
+
+	/* .... For DDR & LLC  ...... */
+	ddr.vsp_read = fp_div(fp_mult(FP_INT(bitrate),
+				vsp_read_factor), FP_INT(8));
+	ddr.vsp_write = fp_div(fp_mult(FP_INT(bitrate),
+				vsp_write_factor), FP_INT(8));
+
+	ddr.collocated_read = fp_div(FP_INT(lcu_per_frame *
+			collocated_bytes_per_lcu * fps), FP_INT(bps(1)));
+	ddr.collocated_write = ddr.collocated_read;
+
+	y_bw_no_ubwc_8bpp = fp_div(FP_INT(width * height * fps),
+		FP_INT(1000 * 1000));
+
+	if (dpb_bpp != 8) {
+		y_bw_no_ubwc_10bpp =
+			fp_div(fp_mult(y_bw_no_ubwc_8bpp, FP_INT(256)),
+				FP_INT(192));
+		y_bw_10bpp_p010 = y_bw_no_ubwc_8bpp * 2;
+	}
+
+	ddr.dpb_read = dpb_bpp == 8 ? y_bw_no_ubwc_8bpp : y_bw_no_ubwc_10bpp;
+	ddr.dpb_read = fp_div(fp_mult(ddr.dpb_read,
+			fp_mult(dpb_factor, motion_vector_complexity)),
+			dpb_read_compression_factor);
+
+	ddr.dpb_write = dpb_bpp == 8 ? y_bw_no_ubwc_8bpp : y_bw_no_ubwc_10bpp;
+	ddr.dpb_write = fp_div(fp_mult(ddr.dpb_write,
+			fp_mult(dpb_factor, dpb_write_factor)),
+			dpb_write_compression_factor);
+
+	dpb_total = ddr.dpb_read + ddr.dpb_write;
+
+	if (llc_ref_read_l2_cache_enabled) {
+		ddr.dpb_read = fp_div(ddr.dpb_read, is_h264_category ?
+					FP(1, 30, 100) : FP(1, 14, 100));
+		llc.dpb_read = dpb_total - ddr.dpb_write - ddr.dpb_read;
+	}
+
+	ddr.opb_read = FP_ZERO;
+	ddr.opb_write = unified_dpb_opb ? FP_ZERO : (dpb_bpp == 8 ?
+		y_bw_no_ubwc_8bpp : (opb_compression_enabled ?
+		y_bw_no_ubwc_10bpp : y_bw_10bpp_p010));
+	ddr.opb_write = fp_div(fp_mult(dpb_factor, ddr.opb_write),
+		fp_mult(dpb_opb_scaling_ratio, opb_write_compression_factor));
+
+	ddr.line_buffer_read =
+		fp_div(FP_INT(tnbr_per_lcu * lcu_per_frame * fps),
+			FP_INT(bps(1)));
+	/* This change is applicable for all IRIS2 targets,
+	 * but currently being done for IRIS2 with 2 pipes
+	 * only due to timeline constraints.
+	 */
+	if((num_vpp_pipes == 2) && (is_h264_category))
+		ddr.line_buffer_write = fp_div(ddr.line_buffer_read,FP_INT(2));
+	else
+		ddr.line_buffer_write = ddr.line_buffer_read;
+	if (llc_top_line_buf_enabled) {
+		llc.line_buffer_read = ddr.line_buffer_read;
+		llc.line_buffer_write = ddr.line_buffer_write;
+		ddr.line_buffer_write = ddr.line_buffer_read = FP_ZERO;
+	}
+
+	ddr.total = ddr.vsp_read + ddr.vsp_write +
+		ddr.collocated_read + ddr.collocated_write +
+		ddr.dpb_read + ddr.dpb_write +
+		ddr.opb_read + ddr.opb_write +
+		ddr.line_buffer_read + ddr.line_buffer_write;
+
+	qsmmu_bw_overhead_factor = FP(1, 3, 100);
+
+	ddr.total = fp_mult(ddr.total, qsmmu_bw_overhead_factor);
+	llc.total = llc.dpb_read + llc.line_buffer_read +
+			llc.line_buffer_write + ddr.total;
+
+	/* Dump all the variables for easier debugging */
+	if (msm_vidc_debug & VIDC_BUS) {
+		struct dump dump[] = {
+		{"DECODER PARAMETERS", "", DUMP_HEADER_MAGIC},
+		{"lcu size", "%d", lcu_size},
+		{"dpb bitdepth", "%d", dpb_bpp},
+		{"frame rate", "%d", fps},
+		{"dpb/opb unified", "%d", unified_dpb_opb},
+		{"dpb/opb downscaling ratio", DUMP_FP_FMT,
+			dpb_opb_scaling_ratio},
+		{"dpb compression", "%d", dpb_compression_enabled},
+		{"opb compression", "%d", opb_compression_enabled},
+		{"dpb read compression factor", DUMP_FP_FMT,
+			dpb_read_compression_factor},
+		{"dpb write compression factor", DUMP_FP_FMT,
+			dpb_write_compression_factor},
+		{"frame width", "%d", width},
+		{"frame height", "%d", height},
+		{"llc ref read l2 cache enabled", "%d",
+			llc_ref_read_l2_cache_enabled},
+		{"llc top line buf enabled", "%d",
+			llc_top_line_buf_enabled},
+
+		{"DERIVED PARAMETERS (1)", "", DUMP_HEADER_MAGIC},
+		{"lcus/frame", "%d", lcu_per_frame},
+		{"bitrate (Mbit/sec)", "%d", bitrate},
+		{"bins to bit factor", DUMP_FP_FMT, bins_to_bit_factor},
+		{"dpb write factor", DUMP_FP_FMT, dpb_write_factor},
+		{"vsp read factor", DUMP_FP_FMT, vsp_read_factor},
+		{"vsp write factor", DUMP_FP_FMT, vsp_write_factor},
+		{"tnbr/lcu", "%d", tnbr_per_lcu},
+		{"collocated bytes/LCU", "%d", collocated_bytes_per_lcu},
+		{"bw for NV12 8bpc)", DUMP_FP_FMT, y_bw_no_ubwc_8bpp},
+		{"bw for NV12 10bpc)", DUMP_FP_FMT, y_bw_no_ubwc_10bpp},
+
+		{"DERIVED PARAMETERS (2)", "", DUMP_HEADER_MAGIC},
+		{"mv complexity", DUMP_FP_FMT, motion_vector_complexity},
+		{"qsmmu_bw_overhead_factor", DUMP_FP_FMT,
+			qsmmu_bw_overhead_factor},
+
+		{"INTERMEDIATE DDR B/W", "", DUMP_HEADER_MAGIC},
+		{"vsp read", DUMP_FP_FMT, ddr.vsp_read},
+		{"vsp write", DUMP_FP_FMT, ddr.vsp_write},
+		{"collocated read", DUMP_FP_FMT, ddr.collocated_read},
+		{"collocated write", DUMP_FP_FMT, ddr.collocated_write},
+		{"line buffer read", DUMP_FP_FMT, ddr.line_buffer_read},
+		{"line buffer write", DUMP_FP_FMT, ddr.line_buffer_write},
+		{"opb read", DUMP_FP_FMT, ddr.opb_read},
+		{"opb write", DUMP_FP_FMT, ddr.opb_write},
+		{"dpb read", DUMP_FP_FMT, ddr.dpb_read},
+		{"dpb write", DUMP_FP_FMT, ddr.dpb_write},
+		{"dpb total", DUMP_FP_FMT, dpb_total},
+		{"INTERMEDIATE LLC B/W", "", DUMP_HEADER_MAGIC},
+		{"llc dpb read", DUMP_FP_FMT, llc.dpb_read},
+		{"llc line buffer read", DUMP_FP_FMT, llc.line_buffer_read},
+		{"llc line buffer write", DUMP_FP_FMT, llc.line_buffer_write},
+
+		};
+		__dump(dump, ARRAY_SIZE(dump));
+	}
+
+	d->calc_bw_ddr = kbps(fp_round(ddr.total));
+	d->calc_bw_llcc = kbps(fp_round(llc.total));
+
+	return ret;
+}
+
+static u64 __calculate_encoder(struct vidc_bus_vote_data *d)
+{
+	/*
+	 * XXX: Don't fool around with any of the hardcoded numbers unless you
+	 * know /exactly/ what you're doing.  Many of these numbers are
+	 * measured heuristics and hardcoded numbers taken from the firmware.
+	 */
+	/* Encoder Parameters */
+	int width, height, fps, lcu_size, bitrate, lcu_per_frame,
+		collocated_bytes_per_lcu, tnbr_per_lcu, dpb_bpp,
+		original_color_format, vertical_tile_width, rotation;
+	bool work_mode_1, original_compression_enabled,
+		low_power, cropping_or_scaling,
+		b_frames_enabled = false,
+		llc_ref_chroma_cache_enabled = false,
+		llc_top_line_buf_enabled = false,
+		llc_vpss_rot_line_buf_enabled = false;
+
+	unsigned int bins_to_bit_factor;
+	fp_t dpb_compression_factor,
+		original_compression_factor,
+		original_compression_factor_y,
+		y_bw_no_ubwc_8bpp, y_bw_no_ubwc_10bpp = 0, y_bw_10bpp_p010 = 0,
+		input_compression_factor,
+		downscaling_ratio,
+		ref_y_read_bw_factor, ref_cbcr_read_bw_factor,
+		recon_write_bw_factor,
+		total_ref_read_crcb,
+		qsmmu_bw_overhead_factor;
+	fp_t integer_part, frac_part;
+	unsigned long ret = 0;
+
+	/* Output parameters */
+	struct {
+		fp_t vsp_read, vsp_write, collocated_read, collocated_write,
+			ref_read_y, ref_read_crcb, ref_write,
+			ref_write_overlap, orig_read,
+			line_buffer_read, line_buffer_write,
+			total;
+	} ddr = {0};
+
+	struct {
+		fp_t ref_read_crcb, line_buffer, total;
+	} llc = {0};
+
+	/* Encoder Parameters setup */
+	rotation = d->rotation;
+	cropping_or_scaling = false;
+	vertical_tile_width = 960;
+	/*
+	 * recon_write_bw_factor varies according to resolution and bit-depth,
+	 * here use 1.08(1.075) for worst case.
+	 * Similar for ref_y_read_bw_factor, it can reach 1.375 for worst case,
+	 * here use 1.3 for average case, and can somewhat balance the
+	 * worst case assumption for UBWC CR factors.
+	 */
+	recon_write_bw_factor = FP(1, 8, 100);
+	ref_y_read_bw_factor = FP(1, 30, 100);
+	ref_cbcr_read_bw_factor = FP(1, 50, 100);
+
+
+	/* Derived Parameters */
+	fps = d->fps;
+	width = max(d->output_width, BASELINE_DIMENSIONS.width);
+	height = max(d->output_height, BASELINE_DIMENSIONS.height);
+	downscaling_ratio = fp_div(FP_INT(d->input_width * d->input_height),
+		FP_INT(d->output_width * d->output_height));
+	downscaling_ratio = max(downscaling_ratio, FP_ONE);
+	bitrate = d->bitrate > 0 ? DIV_ROUND_UP(d->bitrate, 1000000) :
+		__lut(width, height, fps)->bitrate;
+	lcu_size = d->lcu_size;
+	lcu_per_frame = DIV_ROUND_UP(width, lcu_size) *
+		DIV_ROUND_UP(height, lcu_size);
+	tnbr_per_lcu = 16;
+
+	dpb_bpp = __bpp(d->color_formats[0]);
+
+	y_bw_no_ubwc_8bpp = fp_div(FP_INT(width * height * fps),
+		FP_INT(1000 * 1000));
+
+	if (dpb_bpp != 8) {
+		y_bw_no_ubwc_10bpp = fp_div(fp_mult(y_bw_no_ubwc_8bpp,
+			FP_INT(256)), FP_INT(192));
+		y_bw_10bpp_p010 = y_bw_no_ubwc_8bpp * 2;
+	}
+
+	b_frames_enabled = d->b_frames_enabled;
+	original_color_format = d->num_formats >= 1 ?
+		d->color_formats[0] : MSM_VIDC_FMT_NV12_UBWC;
+
+	original_compression_enabled = __ubwc(original_color_format);
+
+	work_mode_1 = d->work_mode == MSM_VIDC_STAGE_1;
+	low_power = d->power_mode == VIDC_POWER_LOW;
+	bins_to_bit_factor = 4;
+
+	if (d->use_sys_cache) {
+		llc_ref_chroma_cache_enabled = true;
+		llc_top_line_buf_enabled = true,
+		llc_vpss_rot_line_buf_enabled = true;
+	}
+
+	integer_part = Q16_INT(d->compression_ratio);
+	frac_part = Q16_FRAC(d->compression_ratio);
+	dpb_compression_factor = FP(integer_part, frac_part, 100);
+
+	integer_part = Q16_INT(d->input_cr);
+	frac_part = Q16_FRAC(d->input_cr);
+	input_compression_factor = FP(integer_part, frac_part, 100);
+
+	original_compression_factor = original_compression_factor_y =
+		!original_compression_enabled ? FP_ONE :
+		__compression_ratio(__lut(width, height, fps), dpb_bpp);
+	/* use input cr if it is valid (not 1), otherwise use lut */
+	if (original_compression_enabled &&
+		input_compression_factor != FP_ONE) {
+		original_compression_factor = input_compression_factor;
+		/* Luma usually has lower compression factor than Chroma,
+		 * input cf is overall cf, add 1.08 factor for Luma cf
+		 */
+		original_compression_factor_y =
+			input_compression_factor > FP(1, 8, 100) ?
+			fp_div(input_compression_factor, FP(1, 8, 100)) :
+			input_compression_factor;
+	}
+
+	ddr.vsp_read = fp_div(FP_INT(bitrate * bins_to_bit_factor), FP_INT(8));
+	ddr.vsp_write = ddr.vsp_read + fp_div(FP_INT(bitrate), FP_INT(8));
+
+	collocated_bytes_per_lcu = lcu_size == 16 ? 16 :
+				lcu_size == 32 ? 64 : 256;
+
+	ddr.collocated_read = fp_div(FP_INT(lcu_per_frame *
+			collocated_bytes_per_lcu * fps), FP_INT(bps(1)));
+
+	ddr.collocated_write = ddr.collocated_read;
+
+	ddr.ref_read_y = dpb_bpp == 8 ?
+		y_bw_no_ubwc_8bpp : y_bw_no_ubwc_10bpp;
+	if (b_frames_enabled)
+		ddr.ref_read_y = ddr.ref_read_y * 2;
+	ddr.ref_read_y = fp_div(ddr.ref_read_y, dpb_compression_factor);
+
+	ddr.ref_read_crcb = fp_mult((ddr.ref_read_y / 2),
+		ref_cbcr_read_bw_factor);
+
+	if (width > vertical_tile_width) {
+		ddr.ref_read_y = fp_mult(ddr.ref_read_y,
+			ref_y_read_bw_factor);
+	}
+
+	if (llc_ref_chroma_cache_enabled) {
+	total_ref_read_crcb = ddr.ref_read_crcb;
+	ddr.ref_read_crcb = fp_div(ddr.ref_read_crcb,
+			ref_cbcr_read_bw_factor);
+		llc.ref_read_crcb = total_ref_read_crcb - ddr.ref_read_crcb;
+	}
+
+	ddr.ref_write = dpb_bpp == 8 ? y_bw_no_ubwc_8bpp : y_bw_no_ubwc_10bpp;
+	ddr.ref_write = fp_div(fp_mult(ddr.ref_write, FP(1, 50, 100)),
+			dpb_compression_factor);
+
+	if (width > vertical_tile_width) {
+		ddr.ref_write_overlap = fp_mult(ddr.ref_write,
+			(recon_write_bw_factor - FP_ONE));
+		ddr.ref_write = fp_mult(ddr.ref_write, recon_write_bw_factor);
+	}
+
+	ddr.orig_read = dpb_bpp == 8 ? y_bw_no_ubwc_8bpp :
+		(original_compression_enabled ? y_bw_no_ubwc_10bpp :
+		y_bw_10bpp_p010);
+	ddr.orig_read = fp_div(fp_mult(fp_mult(ddr.orig_read, FP(1, 50, 100)),
+		downscaling_ratio), original_compression_factor);
+	if (rotation == 90 || rotation == 270)
+		ddr.orig_read *= lcu_size == 32 ? (dpb_bpp == 8 ? 1 : 3) : 2;
+
+	ddr.line_buffer_read =
+		fp_div(FP_INT(tnbr_per_lcu * lcu_per_frame * fps),
+			FP_INT(bps(1)));
+
+	ddr.line_buffer_write = ddr.line_buffer_read;
+	if (llc_top_line_buf_enabled) {
+		llc.line_buffer = ddr.line_buffer_read + ddr.line_buffer_write;
+		ddr.line_buffer_read = ddr.line_buffer_write = FP_ZERO;
+	}
+
+	ddr.total = ddr.vsp_read + ddr.vsp_write +
+		ddr.collocated_read + ddr.collocated_write +
+		ddr.ref_read_y + ddr.ref_read_crcb +
+		ddr.ref_write + ddr.ref_write_overlap +
+		ddr.orig_read +
+		ddr.line_buffer_read + ddr.line_buffer_write;
+
+	qsmmu_bw_overhead_factor = FP(1, 3, 100);
+	ddr.total = fp_mult(ddr.total, qsmmu_bw_overhead_factor);
+	llc.total = llc.ref_read_crcb + llc.line_buffer + ddr.total;
+
+	if (msm_vidc_debug & VIDC_BUS) {
+		struct dump dump[] = {
+		{"ENCODER PARAMETERS", "", DUMP_HEADER_MAGIC},
+		{"width", "%d", width},
+		{"height", "%d", height},
+		{"fps", "%d", fps},
+		{"dpb bitdepth", "%d", dpb_bpp},
+		{"input downscaling ratio", DUMP_FP_FMT, downscaling_ratio},
+		{"rotation", "%d", rotation},
+		{"cropping or scaling", "%d", cropping_or_scaling},
+		{"low power mode", "%d", low_power},
+		{"work Mode", "%d", work_mode_1},
+		{"B frame enabled", "%d", b_frames_enabled},
+		{"original frame format", "%#x", original_color_format},
+		{"original compression enabled", "%d",
+			original_compression_enabled},
+		{"dpb compression factor", DUMP_FP_FMT,
+			dpb_compression_factor},
+		{"input compression factor", DUMP_FP_FMT,
+			input_compression_factor},
+		{"llc ref chroma cache enabled", DUMP_FP_FMT,
+		llc_ref_chroma_cache_enabled},
+		{"llc top line buf enabled", DUMP_FP_FMT,
+			llc_top_line_buf_enabled},
+		{"llc vpss rot line buf enabled ", DUMP_FP_FMT,
+			llc_vpss_rot_line_buf_enabled},
+
+		{"DERIVED PARAMETERS", "", DUMP_HEADER_MAGIC},
+		{"lcu size", "%d", lcu_size},
+		{"bitrate (Mbit/sec)", "%lu", bitrate},
+		{"bins to bit factor", "%u", bins_to_bit_factor},
+		{"original compression factor", DUMP_FP_FMT,
+			original_compression_factor},
+		{"original compression factor y", DUMP_FP_FMT,
+			original_compression_factor_y},
+		{"qsmmu_bw_overhead_factor",
+			 DUMP_FP_FMT, qsmmu_bw_overhead_factor},
+		{"bw for NV12 8bpc)", DUMP_FP_FMT, y_bw_no_ubwc_8bpp},
+		{"bw for NV12 10bpc)", DUMP_FP_FMT, y_bw_no_ubwc_10bpp},
+
+		{"INTERMEDIATE B/W DDR", "", DUMP_HEADER_MAGIC},
+		{"vsp read", DUMP_FP_FMT, ddr.vsp_read},
+		{"vsp write", DUMP_FP_FMT, ddr.vsp_write},
+		{"collocated read", DUMP_FP_FMT, ddr.collocated_read},
+		{"collocated write", DUMP_FP_FMT, ddr.collocated_write},
+		{"ref read y", DUMP_FP_FMT, ddr.ref_read_y},
+		{"ref read crcb", DUMP_FP_FMT, ddr.ref_read_crcb},
+		{"ref write", DUMP_FP_FMT, ddr.ref_write},
+		{"ref write overlap", DUMP_FP_FMT, ddr.ref_write_overlap},
+		{"original read", DUMP_FP_FMT, ddr.orig_read},
+		{"line buffer read", DUMP_FP_FMT, ddr.line_buffer_read},
+		{"line buffer write", DUMP_FP_FMT, ddr.line_buffer_write},
+		{"INTERMEDIATE LLC B/W", "", DUMP_HEADER_MAGIC},
+		{"llc ref read crcb", DUMP_FP_FMT, llc.ref_read_crcb},
+		{"llc line buffer", DUMP_FP_FMT, llc.line_buffer},
+		};
+		__dump(dump, ARRAY_SIZE(dump));
+	}
+
+	d->calc_bw_ddr = kbps(fp_round(ddr.total));
+	d->calc_bw_llcc = kbps(fp_round(llc.total));
+
+	return ret;
+}
+
+static u64 __calculate(struct msm_vidc_inst* inst, struct vidc_bus_vote_data *d)
+{
+	u64 value = 0;
+
+	switch (d->domain) {
+	case MSM_VIDC_ENCODER:
+		value = __calculate_encoder(d);
+		break;
+	case MSM_VIDC_DECODER:
+		value = __calculate_decoder(d);
+		break;
+	default:
+		s_vpr_e(inst->sid, "Unknown Domain %#x", d->domain);
+	}
+
+	return value;
+}
+
+int msm_vidc_calc_bw_iris2(struct msm_vidc_inst *inst,
+		struct vidc_bus_vote_data *vidc_data)
+{
+	int value = 0;
+
+	if (!vidc_data)
+		return value;
+
+	value = __calculate(inst, vidc_data);
+
+	return value;
 }

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

@@ -8,6 +8,15 @@
 
 #include "msm_vidc_inst.h"
 
+#define MIN_DEC_INPUT_BUFFERS 4
+#define MIN_DEC_OUTPUT_BUFFERS 4
+
+#define MIN_ENC_INPUT_BUFFERS 4
+#define MIN_ENC_OUTPUT_BUFFERS 4
+
+#define DCVS_ENC_EXTRA_INPUT_BUFFERS 4
+#define DCVS_DEC_EXTRA_OUTPUT_BUFFERS 4
+
 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);

+ 1 - 0
driver/vidc/inc/msm_vidc_debug.h

@@ -22,6 +22,7 @@
 extern int msm_vidc_debug;
 extern bool msm_vidc_lossless_encode;
 extern bool msm_vidc_syscache_disable;
+extern int msm_vidc_clock_voting;
 
 /* To enable messages OR these values and
  * echo the result to debugfs file.

+ 48 - 0
driver/vidc/inc/msm_vidc_driver.h

@@ -12,6 +12,8 @@
 #include "msm_vidc_core.h"
 #include "msm_vidc_inst.h"
 
+#define MSM_VIDC_SESSION_INACTIVE_THRESHOLD_MS 1000
+
 static inline is_decode_session(struct msm_vidc_inst *inst)
 {
 	return inst->domain == MSM_VIDC_DECODER;
@@ -59,16 +61,57 @@ static inline is_internal_buffer(enum msm_vidc_buffer_type buffer_type)
 		buffer_type == MSM_VIDC_BUF_VPSS;
 }
 
+static inline bool is_linear_colorformat(enum msm_vidc_colorformat_type colorformat)
+{
+	return colorformat == MSM_VIDC_FMT_NV12 ||
+		colorformat == MSM_VIDC_FMT_NV21 ||
+		colorformat == MSM_VIDC_FMT_NV12_P010;
+}
+
+static inline bool is_10bit_colorformat(enum msm_vidc_colorformat_type colorformat)
+{
+	return colorformat == MSM_VIDC_FMT_NV12_P010 ||
+		colorformat == MSM_VIDC_FMT_NV12_TP10_UBWC;
+}
+
 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_turbo_session(struct msm_vidc_inst *inst)
+{
+	return !!(inst->flags & VIDC_TURBO);
+}
+
 static inline bool is_thumbnail_session(struct msm_vidc_inst *inst)
+{
+	return !!(inst->flags & VIDC_THUMBNAIL);
+}
+
+static inline bool is_low_power_session(struct msm_vidc_inst *inst)
+{
+	return !!(inst->flags & VIDC_LOW_POWER);
+}
+
+static inline bool is_realtime_session(struct msm_vidc_inst *inst)
 {
 	return false; // TODO: fix it
 }
 
+static inline bool is_active_session(u64 prev, u64 curr)
+{
+	u64 ts_delta;
+
+	if (!prev || !curr)
+		return true;
+
+	ts_delta = (prev < curr) ? curr - prev : prev - curr;
+
+	return ((ts_delta / NSEC_PER_MSEC) <=
+			MSM_VIDC_SESSION_INACTIVE_THRESHOLD_MS);
+}
+
 void print_vidc_buffer(u32 tag, const char *str, struct msm_vidc_inst *inst,
 		struct msm_vidc_buffer *vbuf);
 void print_vb2_buffer(const char *str, struct msm_vidc_inst *inst,
@@ -99,6 +142,8 @@ int msm_vidc_remove_session(struct msm_vidc_inst *inst);
 int msm_vidc_add_session(struct msm_vidc_inst *inst);
 int msm_vidc_session_open(struct msm_vidc_inst *inst);
 int msm_vidc_session_set_codec(struct msm_vidc_inst *inst);
+int msm_vidc_session_start(struct msm_vidc_inst* inst,
+		enum msm_vidc_port_type port);
 int msm_vidc_session_stop(struct msm_vidc_inst *inst,
 		enum msm_vidc_port_type port);
 int msm_vidc_session_close(struct msm_vidc_inst *inst);
@@ -140,6 +185,9 @@ struct msm_vidc_buffer *get_meta_buffer(struct msm_vidc_inst *inst,
 struct msm_vidc_inst *get_inst(struct msm_vidc_core *core,
 		u32 session_id);
 void put_inst(struct msm_vidc_inst *inst);
+int msm_vidc_get_mbs_per_frame(struct msm_vidc_inst* inst);
+int msm_vidc_get_fps(struct msm_vidc_inst* inst);
+int msm_vidc_num_queued_bufs(struct msm_vidc_inst* inst, u32 type);
 void core_lock(struct msm_vidc_core *core, const char *function);
 void core_unlock(struct msm_vidc_core *core, const char *function);
 void inst_lock(struct msm_vidc_inst *inst, const char *function);

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

@@ -15,8 +15,9 @@ struct msm_vidc_inst;
 	((c)->session_ops->op(__VA_ARGS__)) : 0)
 
 struct msm_vidc_session_ops {
-	u64 (*calc_freq)(struct msm_vidc_inst *inst);
-	u64 (*calc_bw)(struct msm_vidc_inst *inst);
+	u64 (*calc_freq)(struct msm_vidc_inst *inst, u32 data_size);
+	int (*calc_bw)(struct msm_vidc_inst *inst,
+		struct vidc_bus_vote_data* vote_data);
 	int (*decide_work_route)(struct msm_vidc_inst *inst);
 	int (*decide_work_mode)(struct msm_vidc_inst *inst);
 	int (*decide_core_and_power_mode)(struct msm_vidc_inst *inst);
@@ -105,6 +106,8 @@ struct msm_vidc_inst {
 	enum msm_vidc_pipe_type            pipe;
 	enum msm_vidc_quality_mode         quality_mode;
 	struct msm_vidc_power              power;
+	enum msm_vidc_modes                flags;
+	struct vidc_bus_vote_data          bus_data;
 	struct msm_vidc_buffers_info       buffers;
 	struct msm_vidc_mappings_info      mappings;
 	struct msm_vidc_allocations_info   allocations;
@@ -126,6 +129,8 @@ struct msm_vidc_inst {
 	struct msm_vidc_debug              debug;
 	struct msm_vidc_inst_capability   *capabilities;
 	struct completion                  completions[MAX_SIGNAL];
+	bool                               active;
+	u64                                last_qbuf_time_ns;
 };
 
 #endif // _MSM_VIDC_INST_H_

+ 43 - 0
driver/vidc/inc/msm_vidc_internal.h

@@ -80,6 +80,7 @@
 #define BUFFER_ALIGNMENT_SIZE(x) x
 #define NUM_MBS_720P (((1280 + 15) >> 4) * ((720 + 15) >> 4))
 #define NUM_MBS_4k (((4096 + 15) >> 4) * ((2304 + 15) >> 4))
+#define MB_SIZE_IN_PIXEL (16 * 16)
 
 #define DB_H264_DISABLE_SLICE_BOUNDARY \
 		V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
@@ -554,6 +555,7 @@ struct msm_vidc_crop {
 struct msm_vidc_properties {
 	u32                    frame_rate;
 	u32                    operating_rate;
+	u32                    bitrate;
 };
 
 struct msm_vidc_subscription_params {
@@ -581,7 +583,48 @@ struct msm_vidc_decode_batch {
 	struct delayed_work    work;
 };
 
+enum msm_vidc_modes {
+	VIDC_SECURE            = BIT(0),
+	VIDC_TURBO             = BIT(1),
+	VIDC_THUMBNAIL         = BIT(2),
+	VIDC_LOW_POWER         = BIT(3),
+};
+
+enum load_calc_quirks {
+	LOAD_POWER = 0,
+	LOAD_ADMISSION_CONTROL = 1,
+};
+
+enum msm_vidc_power_mode {
+	VIDC_POWER_NORMAL = 0,
+	VIDC_POWER_LOW,
+	VIDC_POWER_TURBO,
+};
+
+struct vidc_bus_vote_data {
+	enum msm_vidc_domain_type domain;
+	enum msm_vidc_codec_type codec;
+	enum msm_vidc_power_mode power_mode;
+	u32 color_formats[2];
+	int num_formats; /* 1 = DPB-OPB unified; 2 = split */
+	int input_height, input_width, bitrate;
+	int output_height, output_width;
+	int rotation;
+	int compression_ratio;
+	int complexity_factor;
+	int input_cr;
+	u32 lcu_size;
+	u32 fps;
+	u32 work_mode;
+	bool use_sys_cache;
+	bool b_frames_enabled;
+	u64 calc_bw_ddr;
+	u64 calc_bw_llcc;
+	u32 num_vpp_pipes;
+};
+
 struct msm_vidc_power {
+	enum msm_vidc_power_mode power_mode;
 	u32                    buffer_counter;
 	u32                    min_threshold;
 	u32                    nom_threshold;

+ 242 - 1
driver/vidc/inc/msm_vidc_power.h

@@ -6,8 +6,249 @@
 #ifndef _MSM_VIDC_POWER_H_
 #define _MSM_VIDC_POWER_H_
 
+#include "fixedpoint.h"
+#include "msm_vidc_debug.h"
+#include "msm_vidc_internal.h"
 #include "msm_vidc_inst.h"
 
-int msm_vidc_scale_power(struct msm_vidc_inst *inst);
+#define COMPRESSION_RATIO_MAX 5
+
+enum vidc_bus_type {
+	PERF,
+	DDR,
+	LLCC,
+};
+
+/*
+ * Minimum dimensions for which to calculate bandwidth.
+ * This means that anything bandwidth(0, 0) ==
+ * bandwidth(BASELINE_DIMENSIONS.width, BASELINE_DIMENSIONS.height)
+ */
+static const struct {
+	int height, width;
+} BASELINE_DIMENSIONS = {
+	.width = 1280,
+	.height = 720,
+};
+
+/* converts Mbps to bps (the "b" part can be bits or bytes based on context) */
+#define kbps(__mbps) ((__mbps) * 1000)
+#define bps(__mbps) (kbps(__mbps) * 1000)
+
+#define GENERATE_COMPRESSION_PROFILE(__bpp, __worst) {              \
+	.bpp = __bpp,                                                          \
+	.ratio = __worst,                \
+}
+
+/*
+ * The below table is a structural representation of the following table:
+ *  Resolution |    Bitrate |              Compression Ratio          |
+ * ............|............|.........................................|
+ * Width Height|Average High|Avg_8bpc Worst_8bpc Avg_10bpc Worst_10bpc|
+ *  1280    720|      7   14|    1.69       1.28      1.49        1.23|
+ *  1920   1080|     20   40|    1.69       1.28      1.49        1.23|
+ *  2560   1440|     32   64|     2.2       1.26      1.97        1.22|
+ *  3840   2160|     42   84|     2.2       1.26      1.97        1.22|
+ *  4096   2160|     44   88|     2.2       1.26      1.97        1.22|
+ *  4096   2304|     48   96|     2.2       1.26      1.97        1.22|
+ */
+static struct lut {
+	int frame_size; /* width x height */
+	int frame_rate;
+	unsigned long bitrate;
+	struct {
+		int bpp;
+		fp_t ratio;
+	} compression_ratio[COMPRESSION_RATIO_MAX];
+} const LUT[] = {
+	{
+		.frame_size = 1280 * 720,
+		.frame_rate = 30,
+		.bitrate = 14,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 28, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 23, 100)),
+		}
+	},
+	{
+		.frame_size = 1280 * 720,
+		.frame_rate = 60,
+		.bitrate = 22,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 28, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 23, 100)),
+		}
+	},
+	{
+		.frame_size = 1920 * 1088,
+		.frame_rate = 30,
+		.bitrate = 40,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 28, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 23, 100)),
+		}
+	},
+	{
+		.frame_size = 1920 * 1088,
+		.frame_rate = 60,
+		.bitrate = 64,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 28, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 23, 100)),
+		}
+	},
+	{
+		.frame_size = 2560 * 1440,
+		.frame_rate = 30,
+		.bitrate = 64,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 2560 * 1440,
+		.frame_rate = 60,
+		.bitrate = 102,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 3840 * 2160,
+		.frame_rate = 30,
+		.bitrate = 84,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 3840 * 2160,
+		.frame_rate = 60,
+		.bitrate = 134,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 4096 * 2160,
+		.frame_rate = 30,
+		.bitrate = 88,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 4096 * 2160,
+		.frame_rate = 60,
+		.bitrate = 141,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 4096 * 2304,
+		.frame_rate = 30,
+		.bitrate = 96,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+	{
+		.frame_size = 4096 * 2304,
+		.frame_rate = 60,
+		.bitrate = 154,
+		.compression_ratio = {
+			GENERATE_COMPRESSION_PROFILE(8,
+					FP(1, 26, 100)),
+			GENERATE_COMPRESSION_PROFILE(10,
+					FP(1, 22, 100)),
+		}
+	},
+};
+
+static inline u32 get_type_frm_name(const char* name)
+{
+	if (!strcmp(name, "venus-llcc"))
+		return LLCC;
+	else if (!strcmp(name, "venus-ddr"))
+		return DDR;
+	else
+		return PERF;
+}
+
+#define DUMP_HEADER_MAGIC 0xdeadbeef
+#define DUMP_FP_FMT "%FP" /* special format for fp_t */
+
+struct dump {
+	char* key;
+	char* format;
+	size_t val;
+};
+
+struct lut const* __lut(int width, int height, int fps);
+fp_t __compression_ratio(struct lut const* entry, int bpp);
+void __dump(struct dump dump[], int len);
+
+static inline bool __ubwc(enum msm_vidc_colorformat_type f)
+{
+	switch (f) {
+	case MSM_VIDC_FMT_NV12_UBWC:
+	case MSM_VIDC_FMT_NV12_TP10_UBWC:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static inline int __bpp(enum msm_vidc_colorformat_type f)
+{
+	switch (f) {
+	case MSM_VIDC_FMT_NV12:
+	case MSM_VIDC_FMT_NV21:
+	case MSM_VIDC_FMT_NV12_UBWC:
+	case MSM_VIDC_FMT_RGBA8888_UBWC:
+		return 8;
+	case MSM_VIDC_FMT_NV12_P010:
+	case MSM_VIDC_FMT_NV12_TP10_UBWC:
+		return 10;
+	default:
+		d_vpr_e("Unsupported colorformat (%x)", f);
+		return INT_MAX;
+	}
+}
+
+u64 msm_vidc_max_freq(struct msm_vidc_inst* inst);
+int msm_vidc_get_inst_load(struct msm_vidc_inst* inst,
+	enum load_calc_quirks quirks);
+int msm_vidc_scale_power(struct msm_vidc_inst *inst, bool scale_buses);
 
 #endif

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

@@ -67,7 +67,7 @@ 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_scale_clocks(struct msm_vidc_inst* inst, u64 freq);
-int venus_hfi_scale_buses(struct msm_vidc_inst* inst, u64 freq);
+int venus_hfi_scale_buses(struct msm_vidc_inst* inst, u64 bw_ddr, u64 bw_llcc);
 
 void venus_hfi_work_handler(struct work_struct *work);
 void venus_hfi_pm_work_handler(struct work_struct *work);

+ 2 - 2
driver/vidc/src/msm_vdec.c

@@ -1239,7 +1239,7 @@ int msm_vdec_start_input(struct msm_vidc_inst *inst)
 	if (rc)
 		return rc;
 
-	rc = venus_hfi_start(inst, INPUT_PORT);
+	rc = msm_vidc_session_start(inst, INPUT_PORT);
 	if (rc)
 		goto error;
 
@@ -1407,7 +1407,7 @@ int msm_vdec_start_output(struct msm_vidc_inst *inst)
 	if (rc)
 		return rc;
 
-	rc = venus_hfi_start(inst, OUTPUT_PORT);
+	rc = msm_vidc_session_start(inst, OUTPUT_PORT);
 	if (rc)
 		goto error;
 

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

@@ -772,7 +772,7 @@ int msm_venc_start_input(struct msm_vidc_inst *inst)
 	if (rc)
 		return rc;
 
-	rc = venus_hfi_start(inst, INPUT_PORT);
+	rc = msm_vidc_session_start(inst, INPUT_PORT);
 	if (rc)
 		goto error;
 
@@ -860,7 +860,7 @@ int msm_venc_start_output(struct msm_vidc_inst *inst)
 	if (rc)
 		return rc;
 
-	rc = venus_hfi_start(inst, OUTPUT_PORT);
+	rc = msm_vidc_session_start(inst, OUTPUT_PORT);
 	if (rc)
 		goto error;
 

+ 4 - 2
driver/vidc/src/msm_vidc.c

@@ -14,6 +14,7 @@
 #include "msm_vidc_v4l2.h"
 #include "msm_vidc_debug.h"
 #include "msm_vidc_control.h"
+#include "msm_vidc_power.h"
 
 #define MSM_VIDC_DRV_NAME "msm_vidc_driver"
 /* kernel/msm-4.19 */
@@ -801,6 +802,7 @@ void *msm_vidc_open(void *vidc_core, u32 session_type)
 	INIT_LIST_HEAD(&inst->firmware.list);
 	inst->domain = session_type;
 	inst->state = MSM_VIDC_OPEN;
+	inst->active = true;
 	inst->request = false;
 	inst->ipsc_properties_set = false;
 	inst->opsc_properties_set = false;
@@ -828,8 +830,8 @@ void *msm_vidc_open(void *vidc_core, u32 session_type)
 	if (rc)
 		goto error;
 
-	//msm_power_setup(inst);
-	// send cmd to firmware here
+	msm_vidc_scale_power(inst, true);
+
 	rc = msm_vidc_session_open(inst);
 	if (rc)
 		goto error;

+ 106 - 21
driver/vidc/src/msm_vidc_buffer.c

@@ -11,10 +11,7 @@
 #include "msm_vidc_debug.h"
 #include "msm_vidc_internal.h"
 
-#define MIN_INPUT_BUFFERS 4
-#define MIN_ENC_OUTPUT_BUFFERS 4
-
-u32 msm_vidc_input_min_count(struct msm_vidc_inst *inst)
+u32 msm_vidc_input_min_count(struct msm_vidc_inst* inst)
 {
 	u32 input_min_count = 0;
 	//struct v4l2_ctrl *max_layer = NULL;
@@ -24,10 +21,15 @@ u32 msm_vidc_input_min_count(struct msm_vidc_inst *inst)
 		return 0;
 	}
 
-	if (!is_decode_session(inst) && !is_encode_session(inst))
+	if (is_decode_session(inst)) {
+		input_min_count = MIN_DEC_INPUT_BUFFERS;
+	} else if (is_encode_session(inst)) {
+		input_min_count = MIN_ENC_INPUT_BUFFERS;
+	} else {
+		s_vpr_e(inst->sid, "%s: invalid domain\n",
+			__func__, inst->domain);
 		return 0;
-
-	input_min_count = MIN_INPUT_BUFFERS;
+	}
 
 	if (is_thumbnail_session(inst))
 		input_min_count = 1;
@@ -90,7 +92,7 @@ u32 msm_vidc_output_min_count(struct msm_vidc_inst *inst)
 
 u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst)
 {
-	u32 extra_input_count = 0;
+	u32 count = 0;
 	struct msm_vidc_core *core;
 
 	if (!inst || !inst->core) {
@@ -99,23 +101,36 @@ u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst)
 	}
 	core = inst->core;
 
+	/*
+	 * no extra buffers for thumbnail session because
+	 * neither dcvs nor batching will be enabled
+	 */
 	if (is_thumbnail_session(inst))
-		return extra_input_count;
+		return 0;
 
 	if (is_decode_session(inst)) {
-		/* add dcvs buffers */
-		/* add batching buffers */
-		extra_input_count = 6;
+		/*
+		 * if decode batching enabled, ensure minimum batch size
+		 * count of input buffers present on input port
+		 */
+		if (core->capabilities[DECODE_BATCH].value &&
+			inst->decode_batch.enable) {
+			if (inst->buffers.input.min_count < inst->decode_batch.size) {
+				count = inst->decode_batch.size -
+					inst->buffers.input.min_count;
+			}
+		}
 	} else if (is_encode_session(inst)) {
 		/* add dcvs buffers */
-		extra_input_count = 4;
+		count = DCVS_ENC_EXTRA_INPUT_BUFFERS;
 	}
-	return extra_input_count;
+
+	return count;
 }
 
 u32 msm_vidc_output_extra_count(struct msm_vidc_inst *inst)
 {
-	u32 extra_output_count = 0;
+	u32 count = 0;
 	struct msm_vidc_core *core;
 
 	if (!inst || !inst->core) {
@@ -124,24 +139,94 @@ u32 msm_vidc_output_extra_count(struct msm_vidc_inst *inst)
 	}
 	core = inst->core;
 
+	/*
+	 * no extra buffers for thumbnail session because
+	 * neither dcvs nor batching will be enabled
+	 */
 	if (is_thumbnail_session(inst))
 		return 0;
 
 	if (is_decode_session(inst)) {
 		/* add dcvs buffers */
-		/* add batching buffers */
-		extra_output_count = 6;
+		count = DCVS_DEC_EXTRA_OUTPUT_BUFFERS;
+		/*
+		 * if decode batching enabled, ensure minimum batch size
+		 * count of extra output buffers added on output port
+		 */
+		if (core->capabilities[DECODE_BATCH].value &&
+			inst->decode_batch.enable &&
+			count < inst->decode_batch.size)
+			count = inst->decode_batch.size;
+
 	} else if (is_encode_session(inst)) {
 		/* add heif buffers */
-		//extra_output_count = 8
+		//count = 8
 	}
-	return extra_output_count;
+
+	return count;
 }
 
 u32 msm_vidc_decoder_input_size(struct msm_vidc_inst *inst)
 {
-	u32 size = ALIGN(1 * 1024 * 1024, SZ_4K);
-	return size;
+	u32 frame_size, num_mbs;
+	u32 div_factor = 1;
+	u32 base_res_mbs = NUM_MBS_4k;
+	struct v4l2_format *f;
+	u32 buffer_size_limit = 0; // TODO: fix me
+
+	if (!inst || !inst->capabilities) {
+		d_vpr_e("%s: invalid params\n");
+		return 0;
+	}
+
+	/*
+	 * Decoder input size calculation:
+	 * For 8k resolution, buffer size is calculated as 8k mbs / 4 and
+	 * for 8k cases we expect width/height to be set always.
+	 * In all other cases, buffer size is calculated as
+	 * 4k mbs for VP8/VP9 and 4k / 2 for remaining codecs.
+	 */
+	f = &inst->fmts[INPUT_PORT];
+	num_mbs = msm_vidc_get_mbs_per_frame(inst);
+	if (num_mbs > NUM_MBS_4k) {
+		div_factor = 4;
+		base_res_mbs = inst->capabilities->cap[MBPF].value;
+	} else {
+		base_res_mbs = NUM_MBS_4k;
+		if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9)
+			div_factor = 1;
+		else
+			div_factor = 2;
+	}
+
+	if (is_secure_session(inst))
+		div_factor = div_factor << 1;
+
+	/* For HEIF image, use the actual resolution to calc buffer size */
+	/* TODO: fix me
+	if (is_heif_decoder(inst)) {
+		base_res_mbs = num_mbs;
+		div_factor = 1;
+	}
+	*/
+
+	frame_size = base_res_mbs * MB_SIZE_IN_PIXEL * 3 / 2 / div_factor;
+
+	 /* multiply by 10/8 (1.25) to get size for 10 bit case */
+	if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9 ||
+		f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC)
+		frame_size = frame_size + (frame_size >> 2);
+
+	if (buffer_size_limit && (buffer_size_limit < frame_size)) {
+		frame_size = buffer_size_limit;
+		s_vpr_h(inst->sid, "input buffer size limited to %d\n",
+			frame_size);
+	} else {
+		s_vpr_h(inst->sid, "set input buffer size to %d\n",
+			frame_size);
+	}
+
+	return ALIGN(frame_size, SZ_4K);
 }
 
 u32 msm_vidc_decoder_output_size(struct msm_vidc_inst *inst)

+ 3 - 0
driver/vidc/src/msm_vidc_debug.c

@@ -15,3 +15,6 @@ EXPORT_SYMBOL(msm_vidc_lossless_encode);
 
 bool msm_vidc_syscache_disable = !true;
 EXPORT_SYMBOL(msm_vidc_syscache_disable);
+
+int msm_vidc_clock_voting = !1;
+

+ 79 - 0
driver/vidc/src/msm_vidc_driver.c

@@ -13,6 +13,7 @@
 #include "msm_vidc_internal.h"
 #include "msm_vidc_memory.h"
 #include "msm_vidc_debug.h"
+#include "msm_vidc_power.h"
 #include "venus_hfi.h"
 #include "msm_vidc.h"
 
@@ -464,6 +465,65 @@ int msm_vidc_get_control(struct msm_vidc_inst *inst, struct v4l2_ctrl *ctrl)
 	return rc;
 }
 
+int msm_vidc_get_mbs_per_frame(struct msm_vidc_inst *inst)
+{
+	int height, width;
+	struct v4l2_format *out_f;
+	struct v4l2_format *inp_f;
+
+	out_f = &inst->fmts[OUTPUT_PORT];
+	inp_f = &inst->fmts[INPUT_PORT];
+	height = max(out_f->fmt.pix_mp.height,
+			inp_f->fmt.pix_mp.height);
+	width = max(out_f->fmt.pix_mp.width,
+			inp_f->fmt.pix_mp.width);
+
+	return NUM_MBS_PER_FRAME(height, width);
+}
+
+int msm_vidc_get_fps(struct msm_vidc_inst *inst)
+{
+	int fps;
+
+	if (inst->prop.operating_rate > inst->prop.frame_rate)
+		fps = (inst->prop.operating_rate >> 16) ?
+			(inst->prop.operating_rate >> 16) : 1;
+	else
+		fps = inst->prop.frame_rate >> 16;
+
+	return fps;
+}
+
+int msm_vidc_num_queued_bufs(struct msm_vidc_inst *inst, u32 type)
+{
+	int count = 0;
+	struct msm_vidc_buffer *vbuf;
+	struct msm_vidc_buffers* buffers;
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return 0;
+	}
+	if (type == OUTPUT_MPLANE) {
+		buffers = &inst->buffers.output;
+	} else if (type == INPUT_MPLANE) {
+		buffers = &inst->buffers.input;
+	} else {
+		s_vpr_e(inst->sid, "%s: invalid buffer type %#x\n", __func__, type);
+		return -EINVAL;
+	}
+
+	list_for_each_entry(vbuf, &buffers->list, list) {
+		if (vbuf->type != type)
+			continue;
+		if (!(vbuf->attr & MSM_VIDC_ATTR_QUEUED))
+			continue;
+		count++;
+	}
+
+	return count;
+}
+
 static int vb2_buffer_to_driver(struct vb2_buffer *vb2,
 	struct msm_vidc_buffer *buf)
 {
@@ -1264,6 +1324,25 @@ int msm_vidc_session_set_codec(struct msm_vidc_inst *inst)
 	return 0;
 }
 
+int msm_vidc_session_start(struct msm_vidc_inst* inst,
+		enum msm_vidc_port_type port)
+{
+	int rc = 0;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+
+	msm_vidc_scale_power(inst, true);
+
+	rc = venus_hfi_start(inst, port);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
 int msm_vidc_session_stop(struct msm_vidc_inst *inst,
 	enum msm_vidc_port_type port)
 {

+ 555 - 7
driver/vidc/src/msm_vidc_power.c

@@ -8,29 +8,577 @@
 #include "msm_vidc_internal.h"
 #include "msm_vidc_inst.h"
 #include "msm_vidc_core.h"
+#include "msm_vidc_dt.h"
+#include "msm_vidc_driver.h"
+#include "msm_vidc_platform.h"
+#include "msm_vidc_buffer.h"
 #include "venus_hfi.h"
 
-int msm_vidc_scale_power(struct msm_vidc_inst *inst)
+#define MSM_VIDC_MIN_UBWC_COMPLEXITY_FACTOR (1 << 16)
+#define MSM_VIDC_MAX_UBWC_COMPLEXITY_FACTOR (4 << 16)
+#define MSM_VIDC_MIN_UBWC_COMPRESSION_RATIO (1 << 16)
+#define MSM_VIDC_MAX_UBWC_COMPRESSION_RATIO (5 << 16)
+
+u64 msm_vidc_max_freq(struct msm_vidc_inst *inst)
+{
+	struct msm_vidc_core* core;
+	struct allowed_clock_rates_table *allowed_clks_tbl;
+	u64 freq = 0;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return freq;
+	}
+	core = inst->core;
+	if (!core->dt || !core->dt->allowed_clks_tbl) {
+		s_vpr_e(inst->sid, "%s: invalid params\n", __func__);
+		return freq;
+	}
+	allowed_clks_tbl = core->dt->allowed_clks_tbl;
+	freq = allowed_clks_tbl[0].clock_rate;
+
+	s_vpr_l(inst->sid, "%s: rate = %lu\n", __func__, freq);
+	return freq;
+}
+
+static int msm_vidc_get_mbps(struct msm_vidc_inst *inst,
+		enum load_calc_quirks quirks)
+{
+	int input_port_mbs, output_port_mbs;
+	int fps;
+	struct v4l2_format *f;
+
+	f = &inst->fmts[INPUT_PORT];
+	input_port_mbs = NUM_MBS_PER_FRAME(f->fmt.pix_mp.width,
+		f->fmt.pix_mp.height);
+
+	f = &inst->fmts[OUTPUT_PORT];
+	output_port_mbs = NUM_MBS_PER_FRAME(f->fmt.pix_mp.width,
+		f->fmt.pix_mp.height);
+
+	fps = inst->prop.frame_rate;
+
+	/* For admission control operating rate is ignored */
+	if (quirks == LOAD_POWER)
+		fps = max(inst->prop.operating_rate, inst->prop.frame_rate);
+
+	/* In case of fps < 1 we assume 1 */
+	fps = max(fps >> 16, 1);
+
+	return max(input_port_mbs, output_port_mbs) * fps;
+}
+
+int msm_vidc_get_inst_load(struct msm_vidc_inst *inst,
+		enum load_calc_quirks quirks)
+{
+	int load = 0;
+
+	if (inst->state == MSM_VIDC_OPEN ||
+		inst->state == MSM_VIDC_ERROR)
+		goto exit;
+
+	/*
+	 * Clock and Load calculations for REALTIME/NON-REALTIME
+	 * Operating rate will either Default or Client value.
+	 * Session admission control will be based on Load.
+	 * Power requests based of calculated Clock/Freq.
+	 * ----------------|----------------------------|
+	 * REALTIME        | Admission Control Load =   |
+	 *                 |          res * fps         |
+	 *                 | Power Request Load =       |
+	 *                 |          res * max(op, fps)|
+	 * ----------------|----------------------------|
+	 * NON-REALTIME/   | Admission Control Load = 0	|
+	 *  THUMBNAIL      | Power Request Load =       |
+	 *                 |          res * max(op, fps)|
+	 * ----------------|----------------------------|
+	 */
+	if (is_thumbnail_session(inst) ||
+		(!is_realtime_session(inst) &&
+		quirks == LOAD_ADMISSION_CONTROL)) {
+		load = 0;
+	} else {
+		load = msm_vidc_get_mbps(inst, quirks);
+	}
+
+exit:
+	return load;
+}
+
+static int fill_dynamic_stats(struct msm_vidc_inst *inst,
+	struct vidc_bus_vote_data *vote_data)
+{
+	u32 max_cr = MSM_VIDC_MIN_UBWC_COMPRESSION_RATIO;
+	u32 max_cf = MSM_VIDC_MIN_UBWC_COMPLEXITY_FACTOR;
+	u32 max_input_cr = MSM_VIDC_MIN_UBWC_COMPRESSION_RATIO;
+	u32 min_cf = MSM_VIDC_MAX_UBWC_COMPLEXITY_FACTOR;
+	u32 min_input_cr = MSM_VIDC_MAX_UBWC_COMPRESSION_RATIO;
+	u32 min_cr = MSM_VIDC_MAX_UBWC_COMPRESSION_RATIO;
+
+	/* TODO: get ubwc stats from firmware
+	if (inst->core->resources.ubwc_stats_in_fbd == 1) {
+		mutex_lock(&inst->ubwc_stats_lock);
+		if (inst->ubwc_stats.is_valid == 1) {
+			min_cr = inst->ubwc_stats.worst_cr;
+			max_cf = inst->ubwc_stats.worst_cf;
+			min_input_cr = inst->ubwc_stats.worst_cr;
+		}
+		mutex_unlock(&inst->ubwc_stats_lock);
+	}
+	*/
+	/* Sanitize CF values from HW */
+	max_cf = min_t(u32, max_cf, MSM_VIDC_MAX_UBWC_COMPLEXITY_FACTOR);
+	min_cf = max_t(u32, min_cf, MSM_VIDC_MIN_UBWC_COMPLEXITY_FACTOR);
+	max_cr = min_t(u32, max_cr, MSM_VIDC_MAX_UBWC_COMPRESSION_RATIO);
+	min_cr = max_t(u32, min_cr, MSM_VIDC_MIN_UBWC_COMPRESSION_RATIO);
+	max_input_cr = min_t(u32,
+		max_input_cr, MSM_VIDC_MAX_UBWC_COMPRESSION_RATIO);
+	min_input_cr = max_t(u32,
+		min_input_cr, MSM_VIDC_MIN_UBWC_COMPRESSION_RATIO);
+
+	vote_data->compression_ratio = min_cr;
+	vote_data->complexity_factor = max_cf;
+	vote_data->input_cr = min_input_cr;
+
+	s_vpr_l(inst->sid,
+		"Input CR = %d Recon CR = %d Complexity Factor = %d\n",
+		vote_data->input_cr, vote_data->compression_ratio,
+		vote_data->complexity_factor);
+
+	return 0;
+}
+
+static int msm_vidc_set_buses(struct msm_vidc_inst* inst)
 {
 	int rc = 0;
-	u64 freq;
 	struct msm_vidc_core* core;
+	struct msm_vidc_inst* temp;
+	u64 total_bw_ddr = 0, total_bw_llcc = 0;
+	u64 curr_time_ns;
 
 	if (!inst || !inst->core) {
-		d_vpr_e("%s: invalid params %pK\n", __func__, inst);
+		d_vpr_e("%s: invalid params\n", __func__);
 		return -EINVAL;
 	}
 	core = inst->core;
 
-	freq = call_session_op(core, calc_freq, inst);
-	rc = venus_hfi_scale_clocks(inst, freq);
+	mutex_lock(&core->lock);
+	curr_time_ns = ktime_get_ns();
+	list_for_each_entry(temp, &core->instances, list) {
+		struct msm_vidc_buffer *vbuf, *next;
+		u32 data_size = 0;
+
+		/* TODO: accessing temp without lock */
+		list_for_each_entry_safe(vbuf, next, &temp->buffers.input.list, list)
+			data_size = max(data_size, vbuf->data_size);
+		if (!data_size)
+			continue;
+
+		/* skip inactive session bus bandwidth */
+		if (!is_active_session(temp->last_qbuf_time_ns, curr_time_ns)) {
+			temp->active = false;
+			continue;
+		}
+
+		if (temp->bus_data.power_mode == VIDC_POWER_TURBO) {
+			total_bw_ddr = total_bw_llcc = INT_MAX;
+			break;
+		}
+		total_bw_ddr += temp->bus_data.calc_bw_ddr;
+		total_bw_llcc += temp->bus_data.calc_bw_llcc;
+	}
+	mutex_unlock(&core->lock);
+
+	rc = venus_hfi_scale_buses(inst, total_bw_ddr, total_bw_llcc);
 	if (rc)
 		return rc;
 
-	freq = call_session_op(core, calc_bw, inst);
-	rc = venus_hfi_scale_buses(inst, freq);
+	return 0;
+}
+
+int msm_vidc_scale_buses(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	struct msm_vidc_core *core;
+	struct vidc_bus_vote_data *vote_data;
+	struct v4l2_format *out_f;
+	struct v4l2_format *inp_f;
+	struct msm_vidc_buffer *vbuf;
+	u32 data_size = 0;
+	int codec = 0;
+
+	if (!inst || !inst->core || !inst->capabilities) {
+		d_vpr_e("%s: invalid params: %pK\n", __func__, inst);
+		return -EINVAL;
+	}
+	core = inst->core;
+	if (!core->dt) {
+		d_vpr_e("%s: invalid dt params\n", __func__);
+		return -EINVAL;
+	}
+	vote_data = &inst->bus_data;
+
+	list_for_each_entry(vbuf, &inst->buffers.input.list, list)
+		data_size = max(data_size, vbuf->data_size);
+	if (!data_size)
+		return 0;
+
+	vote_data->power_mode = VIDC_POWER_NORMAL;
+	if (inst->power.buffer_counter < DCVS_FTB_WINDOW)
+		vote_data->power_mode = VIDC_POWER_TURBO;
+	if (msm_vidc_clock_voting)
+		vote_data->power_mode = VIDC_POWER_TURBO;
+
+	if (vote_data->power_mode == VIDC_POWER_TURBO)
+		goto set_buses;
+
+	out_f = &inst->fmts[OUTPUT_PORT];
+	inp_f = &inst->fmts[INPUT_PORT];
+	switch (inst->domain) {
+	case MSM_VIDC_DECODER:
+		codec = inp_f->fmt.pix_mp.pixelformat;
+		break;
+	case MSM_VIDC_ENCODER:
+		codec = out_f->fmt.pix_mp.pixelformat;
+		break;
+	default:
+		s_vpr_e(inst->sid, "%s: invalid session_type %#x\n",
+			__func__, inst->domain);
+		break;
+	}
+
+	vote_data->codec = inst->codec;
+	vote_data->input_width = inp_f->fmt.pix_mp.width;
+	vote_data->input_height = inp_f->fmt.pix_mp.height;
+	vote_data->output_width = out_f->fmt.pix_mp.width;
+	vote_data->output_height = out_f->fmt.pix_mp.height;
+	vote_data->lcu_size = (codec == V4L2_PIX_FMT_HEVC ||
+			codec == V4L2_PIX_FMT_VP9) ? 32 : 16;
+	vote_data->fps = msm_vidc_get_fps(inst);
+
+	if (inst->domain == MSM_VIDC_ENCODER) {
+		vote_data->bitrate = inst->capabilities->cap[BIT_RATE].value;
+		vote_data->rotation = inst->capabilities->cap[ROTATION].value;
+		vote_data->b_frames_enabled =
+			inst->capabilities->cap[B_FRAME].value > 0;
+		/* scale bitrate if operating rate is larger than fps */
+		if (vote_data->fps > (inst->prop.frame_rate >> 16) &&
+			(inst->prop.frame_rate >> 16)) {
+			vote_data->bitrate = vote_data->bitrate /
+				(inst->prop.frame_rate >> 16) * vote_data->fps;
+		}
+		vote_data->num_formats = 1;
+		vote_data->color_formats[0] = v4l2_colorformat_to_driver(
+			inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat, __func__);
+	} else if (inst->domain == MSM_VIDC_DECODER) {
+		u32 color_format;
+
+		vote_data->bitrate = data_size * vote_data->fps * 8;
+		color_format = v4l2_colorformat_to_driver(
+			inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat, __func__);
+		if (is_linear_colorformat(color_format)) {
+			vote_data->num_formats = 2;
+			/*
+			 * 0 index - dpb colorformat
+			 * 1 index - opb colorformat
+			 */
+			if (is_10bit_colorformat(color_format)) {
+				vote_data->color_formats[0] = MSM_VIDC_FMT_NV12_TP10_UBWC;
+			} else {
+				vote_data->color_formats[0] = MSM_VIDC_FMT_NV12;
+			}
+			vote_data->color_formats[0] = color_format;
+		} else {
+			vote_data->num_formats = 1;
+			vote_data->color_formats[0] = color_format;
+		}
+	}
+	vote_data->work_mode = inst->stage;
+	if (core->dt->sys_cache_res_set)
+		vote_data->use_sys_cache = true;
+	vote_data->num_vpp_pipes = core->capabilities[NUM_VPP_PIPE].value;
+	fill_dynamic_stats(inst, vote_data);
+
+	call_session_op(core, calc_bw, inst, vote_data);
+
+set_buses:
+	rc = msm_vidc_set_buses(inst);
 	if (rc)
 		return rc;
 
 	return 0;
 }
+
+int msm_vidc_set_clocks(struct msm_vidc_inst* inst)
+{
+	int rc = 0;
+	struct msm_vidc_core* core;
+	struct msm_vidc_inst* temp;
+	u64 freq, rate;
+	u32 data_size;
+	bool increment, decrement;
+	u64 curr_time_ns;
+	int i = 0;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+	core = inst->core;
+	if (!core->dt || !core->dt->allowed_clks_tbl) {
+		d_vpr_e("%s: invalid dt params\n", __func__);
+		return -EINVAL;
+	}
+
+	mutex_lock(&core->lock);
+	increment = false;
+	decrement = true;
+	freq = 0;
+	curr_time_ns = ktime_get_ns();
+	list_for_each_entry(temp, &core->instances, list) {
+		struct msm_vidc_buffer* vbuf, *next;
+
+		data_size = 0;
+		list_for_each_entry_safe(vbuf, next, &temp->buffers.input.list, list)
+			data_size = max(data_size, vbuf->data_size);
+		if (!data_size)
+			continue;
+
+		/* skip inactive session clock rate */
+		if (!is_active_session(temp->last_qbuf_time_ns, curr_time_ns)) {
+			temp->active = false;
+			continue;
+		}
+		freq += temp->power.min_freq;
+
+		if (msm_vidc_clock_voting) {
+			d_vpr_l("msm_vidc_clock_voting %d\n", msm_vidc_clock_voting);
+			freq = msm_vidc_clock_voting;
+			decrement = false;
+			break;
+		}
+		/* increment even if one session requested for it */
+		if (temp->power.dcvs_flags & MSM_VIDC_DCVS_INCR)
+			increment = true;
+		/* decrement only if all sessions requested for it */
+		if (!(temp->power.dcvs_flags & MSM_VIDC_DCVS_DECR))
+			decrement = false;
+	}
+
+	/*
+	 * keep checking from lowest to highest rate until
+	 * table rate >= requested rate
+	 */
+	for (i = core->dt->allowed_clks_tbl_size - 1; i >= 0; i--) {
+		rate = core->dt->allowed_clks_tbl[i].clock_rate;
+		if (rate >= freq)
+			break;
+	}
+	if (i < 0)
+		i = 0;
+	if (increment) {
+		if (i > 0)
+			rate = core->dt->allowed_clks_tbl[i - 1].clock_rate;
+	} else if (decrement) {
+		if (i < (int) (core->dt->allowed_clks_tbl_size - 1))
+			rate = core->dt->allowed_clks_tbl[i + 1].clock_rate;
+	}
+	core->power.clk_freq = (u32)rate;
+
+	d_vpr_p("%s: clock rate %lu requested %lu increment %d decrement %d\n",
+		__func__, rate, freq, increment, decrement);
+	mutex_unlock(&core->lock);
+
+	rc = venus_hfi_scale_clocks(inst, rate);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+
+static int msm_vidc_apply_dcvs(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+	int bufs_with_fw = 0;
+	struct msm_vidc_power *power;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params %pK\n", __func__, inst);
+		return -EINVAL;
+	}
+
+	if (!inst->power.dcvs_mode || inst->decode_batch.enable) {
+		s_vpr_l(inst->sid, "Skip DCVS (dcvs %d, batching %d)\n",
+			inst->power.dcvs_mode, inst->decode_batch.enable);
+		inst->power.dcvs_flags = 0;
+		return 0;
+	}
+	power = &inst->power;
+
+	if (is_decode_session(inst)) {
+		bufs_with_fw = msm_vidc_num_queued_bufs(inst, OUTPUT_MPLANE);
+	} else {
+		bufs_with_fw = msm_vidc_num_queued_bufs(inst, INPUT_MPLANE);
+	}
+
+	/* +1 as one buffer is going to be queued after the function */
+	bufs_with_fw += 1;
+
+	/*
+	 * DCVS decides clock level based on below algorithm
+	 *
+	 * Limits :
+	 * min_threshold : Buffers required for reference by FW.
+	 * nom_threshold : Midpoint of Min and Max thresholds
+	 * max_threshold : Min Threshold + DCVS extra buffers, allocated
+	 *				   for smooth flow.
+	 * 1) When buffers outside FW are reaching client's extra buffers,
+	 *    FW is slow and will impact pipeline, Increase clock.
+	 * 2) When pending buffers with FW are less than FW requested,
+	 *    pipeline has cushion to absorb FW slowness, Decrease clocks.
+	 * 3) When DCVS has engaged(Inc or Dec) and pending buffers with FW
+	 *    transitions past the nom_threshold, switch to calculated load.
+	 *    This smoothens the clock transitions.
+	 * 4) Otherwise maintain previous Load config.
+	 */
+	if (bufs_with_fw >= power->max_threshold) {
+		power->dcvs_flags = MSM_VIDC_DCVS_INCR;
+	} else if (bufs_with_fw < power->min_threshold) {
+		power->dcvs_flags = MSM_VIDC_DCVS_DECR;
+	} else if ((power->dcvs_flags & MSM_VIDC_DCVS_DECR &&
+			   bufs_with_fw >= power->nom_threshold) ||
+			   (power->dcvs_flags & MSM_VIDC_DCVS_INCR &&
+			   bufs_with_fw <= power->nom_threshold))
+		power->dcvs_flags = 0;
+
+	s_vpr_p(inst->sid, "DCVS: bufs_with_fw %d th[%d %d %d] flags %#x\n",
+		bufs_with_fw, power->min_threshold,
+		power->nom_threshold, power->max_threshold,
+		power->dcvs_flags);
+
+	return rc;
+}
+
+int msm_vidc_scale_clocks(struct msm_vidc_inst *inst)
+{
+	struct msm_vidc_core* core;
+	struct msm_vidc_buffer *vbuf;
+	u32 data_size = 0;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return -EINVAL;
+	}
+	core = inst->core;
+
+	list_for_each_entry(vbuf, &inst->buffers.input.list, list)
+		data_size = max(data_size, vbuf->data_size);
+	if (!data_size)
+		return 0;
+
+	if (inst->power.buffer_counter < DCVS_FTB_WINDOW ||
+		is_turbo_session(inst)) {
+		inst->power.min_freq = msm_vidc_max_freq(inst);
+		inst->power.dcvs_flags = 0;
+	} else if (msm_vidc_clock_voting) {
+		inst->power.min_freq = msm_vidc_clock_voting;
+		inst->power.dcvs_flags = 0;
+	} else {
+		inst->power.min_freq =
+			call_session_op(core, calc_freq, inst, data_size);
+		msm_vidc_apply_dcvs(inst);
+	}
+	msm_vidc_set_clocks(inst);
+
+	return 0;
+}
+
+int msm_vidc_scale_power(struct msm_vidc_inst *inst, bool scale_buses)
+{
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params %pK\n", __func__, inst);
+		return -EINVAL;
+	}
+
+	if (!inst->active) {
+		/* scale buses for inactive -> active session */
+		scale_buses = true;
+		inst->active = true;
+	}
+
+	if (msm_vidc_scale_clocks(inst))
+		s_vpr_e(inst->sid, "failed to scale clock\n");
+
+	if (scale_buses) {
+		if (msm_vidc_scale_buses(inst))
+			s_vpr_e(inst->sid, "failed to scale bus\n");
+	}
+
+	return 0;
+}
+
+void msm_vidc_dcvs_data_reset(struct msm_vidc_inst *inst)
+{
+	struct msm_vidc_power *dcvs;
+	u32 min_count, actual_count;
+
+	if (!inst) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return;
+	}
+
+	dcvs = &inst->power;
+	if (inst->domain == MSM_VIDC_ENCODER) {
+		min_count = inst->buffers.input.min_count;
+		actual_count = inst->buffers.input.actual_count;
+	} else if (inst->domain == MSM_VIDC_DECODER) {
+		min_count = inst->buffers.output.min_count;
+		actual_count = inst->buffers.output.actual_count;
+	} else {
+		s_vpr_e(inst->sid, "%s: invalid domain type %d\n",
+			__func__, inst->domain);
+		return;
+	}
+
+	dcvs->min_threshold = min_count;
+	if (inst->domain == MSM_VIDC_ENCODER)
+		dcvs->max_threshold = min((min_count + DCVS_ENC_EXTRA_INPUT_BUFFERS),
+								  actual_count);
+	else
+		dcvs->max_threshold = min((min_count + DCVS_DEC_EXTRA_OUTPUT_BUFFERS),
+								  actual_count);
+
+	dcvs->dcvs_window =
+		dcvs->max_threshold < dcvs->min_threshold ? 0 :
+		dcvs->max_threshold - dcvs->min_threshold;
+	dcvs->nom_threshold = dcvs->min_threshold +
+		(dcvs->dcvs_window ?
+		(dcvs->dcvs_window / 2) : 0);
+
+	dcvs->dcvs_flags = 0;
+
+	s_vpr_p(inst->sid, "%s: DCVS: thresholds [%d %d %d] flags %#x\n",
+		__func__, dcvs->min_threshold,
+		dcvs->nom_threshold, dcvs->max_threshold,
+		dcvs->dcvs_flags);
+}
+
+void msm_vidc_power_data_reset(struct msm_vidc_inst *inst)
+{
+	int rc = 0;
+
+	if (!inst || !inst->core) {
+		d_vpr_e("%s: invalid params\n", __func__);
+		return;
+	}
+	s_vpr_h(inst->sid, "%s\n", __func__);
+
+	msm_vidc_dcvs_data_reset(inst);
+
+	inst->power.buffer_counter = 0;
+	//inst->ubwc_stats.is_valid = 0; TODO: fix it
+
+	rc = msm_vidc_scale_power(inst, true);
+	if (rc)
+		s_vpr_e(inst->sid, "%s: failed to scale power\n", __func__);
+}

+ 1 - 0
driver/vidc/src/msm_vidc_vb2.c

@@ -6,6 +6,7 @@
 #include "msm_vidc_vb2.h"
 #include "msm_vidc_core.h"
 #include "msm_vidc_inst.h"
+#include "msm_vidc_internal.h"
 #include "msm_vidc_driver.h"
 #include "msm_vdec.h"
 #include "msm_venc.h"

+ 3 - 3
driver/vidc/src/venus_hfi.c

@@ -20,7 +20,7 @@
 
 #include "venus_hfi.h"
 #include "msm_vidc_core.h"
-#include "msm_vidc_bus.h"
+#include "msm_vidc_power.h"
 #include "msm_vidc_dt.h"
 #include "msm_vidc_platform.h"
 #include "msm_vidc_memory.h"
@@ -2995,7 +2995,7 @@ exit:
 	return rc;
 }
 
-int venus_hfi_scale_buses(struct msm_vidc_inst *inst, u64 freq)
+int venus_hfi_scale_buses(struct msm_vidc_inst *inst, u64 bw_ddr, u64 bw_llcc)
 {
 	int rc = 0;
 	struct msm_vidc_core* core;
@@ -3007,7 +3007,7 @@ int venus_hfi_scale_buses(struct msm_vidc_inst *inst, u64 freq)
 	core = inst->core;
 
 	mutex_lock(&core->lock);
-	rc = __vote_buses(core, freq, freq);
+	rc = __vote_buses(core, bw_ddr, bw_llcc);
 	mutex_unlock(&core->lock);
 
 	return rc;