1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Linaro Ltd.
5 * Author: Stanimir Varbanov <stanimir.varbanov@linaro.org>
8 #include <linux/interconnect.h>
9 #include <linux/iopoll.h>
10 #include <linux/kernel.h>
11 #include <linux/pm_domain.h>
12 #include <linux/pm_opp.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/reset.h>
15 #include <linux/types.h>
16 #include <media/v4l2-mem2mem.h>
19 #include "hfi_parser.h"
20 #include "hfi_venus_io.h"
21 #include "pm_helpers.h"
22 #include "hfi_platform.h"
24 static bool legacy_binding;
26 static int core_clks_get(struct venus_core *core)
28 const struct venus_resources *res = core->res;
29 struct device *dev = core->dev;
32 for (i = 0; i < res->clks_num; i++) {
33 core->clks[i] = devm_clk_get(dev, res->clks[i]);
34 if (IS_ERR(core->clks[i]))
35 return PTR_ERR(core->clks[i]);
41 static int core_clks_enable(struct venus_core *core)
43 const struct venus_resources *res = core->res;
44 const struct freq_tbl *freq_tbl = core->res->freq_tbl;
45 unsigned int freq_tbl_size = core->res->freq_tbl_size;
53 freq = freq_tbl[freq_tbl_size - 1].freq;
55 for (i = 0; i < res->clks_num; i++) {
57 ret = clk_set_rate(core->clks[i], freq);
62 ret = clk_prepare_enable(core->clks[i]);
70 clk_disable_unprepare(core->clks[i]);
75 static void core_clks_disable(struct venus_core *core)
77 const struct venus_resources *res = core->res;
78 unsigned int i = res->clks_num;
81 clk_disable_unprepare(core->clks[i]);
84 static int core_clks_set_rate(struct venus_core *core, unsigned long freq)
88 ret = dev_pm_opp_set_rate(core->dev, freq);
92 ret = clk_set_rate(core->vcodec0_clks[0], freq);
96 ret = clk_set_rate(core->vcodec1_clks[0], freq);
103 static int vcodec_clks_get(struct venus_core *core, struct device *dev,
104 struct clk **clks, const char * const *id)
106 const struct venus_resources *res = core->res;
109 for (i = 0; i < res->vcodec_clks_num; i++) {
112 clks[i] = devm_clk_get(dev, id[i]);
114 return PTR_ERR(clks[i]);
120 static int vcodec_clks_enable(struct venus_core *core, struct clk **clks)
122 const struct venus_resources *res = core->res;
126 for (i = 0; i < res->vcodec_clks_num; i++) {
127 ret = clk_prepare_enable(clks[i]);
135 clk_disable_unprepare(clks[i]);
140 static void vcodec_clks_disable(struct venus_core *core, struct clk **clks)
142 const struct venus_resources *res = core->res;
143 unsigned int i = res->vcodec_clks_num;
146 clk_disable_unprepare(clks[i]);
149 static u32 load_per_instance(struct venus_inst *inst)
153 if (!inst || !(inst->state >= INST_INIT && inst->state < INST_STOP))
156 mbs = (ALIGN(inst->width, 16) / 16) * (ALIGN(inst->height, 16) / 16);
158 return mbs * inst->fps;
161 static u32 load_per_type(struct venus_core *core, u32 session_type)
163 struct venus_inst *inst = NULL;
166 list_for_each_entry(inst, &core->instances, list) {
167 if (inst->session_type != session_type)
170 mbs_per_sec += load_per_instance(inst);
176 static void mbs_to_bw(struct venus_inst *inst, u32 mbs, u32 *avg, u32 *peak)
178 const struct venus_resources *res = inst->core->res;
179 const struct bw_tbl *bw_tbl;
180 unsigned int num_rows, i;
188 if (inst->session_type == VIDC_SESSION_TYPE_ENC) {
189 num_rows = res->bw_tbl_enc_size;
190 bw_tbl = res->bw_tbl_enc;
191 } else if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
192 num_rows = res->bw_tbl_dec_size;
193 bw_tbl = res->bw_tbl_dec;
198 if (!bw_tbl || num_rows == 0)
201 for (i = 0; i < num_rows; i++) {
202 if (i != 0 && mbs > bw_tbl[i].mbs_per_sec)
205 if (inst->dpb_fmt & HFI_COLOR_FORMAT_10_BIT_BASE) {
206 *avg = bw_tbl[i].avg_10bit;
207 *peak = bw_tbl[i].peak_10bit;
209 *avg = bw_tbl[i].avg;
210 *peak = bw_tbl[i].peak;
215 static int load_scale_bw(struct venus_core *core)
217 struct venus_inst *inst = NULL;
218 u32 mbs_per_sec, avg, peak, total_avg = 0, total_peak = 0;
220 list_for_each_entry(inst, &core->instances, list) {
221 mbs_per_sec = load_per_instance(inst);
222 mbs_to_bw(inst, mbs_per_sec, &avg, &peak);
228 * keep minimum bandwidth vote for "video-mem" path,
229 * so that clks can be disabled during vdec_session_release().
230 * Actual bandwidth drop will be done during device supend
231 * so that device can power down without any warnings.
234 if (!total_avg && !total_peak)
235 total_avg = kbps_to_icc(1000);
237 dev_dbg(core->dev, VDBGL "total: avg_bw: %u, peak_bw: %u\n",
238 total_avg, total_peak);
240 return icc_set_bw(core->video_path, total_avg, total_peak);
243 static int load_scale_v1(struct venus_inst *inst)
245 struct venus_core *core = inst->core;
246 const struct freq_tbl *table = core->res->freq_tbl;
247 unsigned int num_rows = core->res->freq_tbl_size;
248 unsigned long freq = table[0].freq;
249 struct device *dev = core->dev;
254 mutex_lock(&core->lock);
255 mbs_per_sec = load_per_type(core, VIDC_SESSION_TYPE_ENC) +
256 load_per_type(core, VIDC_SESSION_TYPE_DEC);
258 if (mbs_per_sec > core->res->max_load)
259 dev_warn(dev, "HW is overloaded, needed: %d max: %d\n",
260 mbs_per_sec, core->res->max_load);
262 if (!mbs_per_sec && num_rows > 1) {
263 freq = table[num_rows - 1].freq;
267 for (i = 0; i < num_rows; i++) {
268 if (mbs_per_sec > table[i].load)
270 freq = table[i].freq;
275 ret = core_clks_set_rate(core, freq);
277 dev_err(dev, "failed to set clock rate %lu (%d)\n",
282 ret = load_scale_bw(core);
284 dev_err(dev, "failed to set bandwidth (%d)\n",
290 mutex_unlock(&core->lock);
294 static int core_get_v1(struct venus_core *core)
298 ret = core_clks_get(core);
302 ret = devm_pm_opp_set_clkname(core->dev, "core");
309 static void core_put_v1(struct venus_core *core)
313 static int core_power_v1(struct venus_core *core, int on)
318 ret = core_clks_enable(core);
320 core_clks_disable(core);
325 static const struct venus_pm_ops pm_ops_v1 = {
326 .core_get = core_get_v1,
327 .core_put = core_put_v1,
328 .core_power = core_power_v1,
329 .load_scale = load_scale_v1,
333 vcodec_control_v3(struct venus_core *core, u32 session_type, bool enable)
337 if (session_type == VIDC_SESSION_TYPE_DEC)
338 ctrl = core->wrapper_base + WRAPPER_VDEC_VCODEC_POWER_CONTROL;
340 ctrl = core->wrapper_base + WRAPPER_VENC_VCODEC_POWER_CONTROL;
348 static int vdec_get_v3(struct device *dev)
350 struct venus_core *core = dev_get_drvdata(dev);
352 return vcodec_clks_get(core, dev, core->vcodec0_clks,
353 core->res->vcodec0_clks);
356 static int vdec_power_v3(struct device *dev, int on)
358 struct venus_core *core = dev_get_drvdata(dev);
361 vcodec_control_v3(core, VIDC_SESSION_TYPE_DEC, true);
364 ret = vcodec_clks_enable(core, core->vcodec0_clks);
366 vcodec_clks_disable(core, core->vcodec0_clks);
368 vcodec_control_v3(core, VIDC_SESSION_TYPE_DEC, false);
373 static int venc_get_v3(struct device *dev)
375 struct venus_core *core = dev_get_drvdata(dev);
377 return vcodec_clks_get(core, dev, core->vcodec1_clks,
378 core->res->vcodec1_clks);
381 static int venc_power_v3(struct device *dev, int on)
383 struct venus_core *core = dev_get_drvdata(dev);
386 vcodec_control_v3(core, VIDC_SESSION_TYPE_ENC, true);
389 ret = vcodec_clks_enable(core, core->vcodec1_clks);
391 vcodec_clks_disable(core, core->vcodec1_clks);
393 vcodec_control_v3(core, VIDC_SESSION_TYPE_ENC, false);
398 static const struct venus_pm_ops pm_ops_v3 = {
399 .core_get = core_get_v1,
400 .core_put = core_put_v1,
401 .core_power = core_power_v1,
402 .vdec_get = vdec_get_v3,
403 .vdec_power = vdec_power_v3,
404 .venc_get = venc_get_v3,
405 .venc_power = venc_power_v3,
406 .load_scale = load_scale_v1,
409 static int vcodec_control_v4(struct venus_core *core, u32 coreid, bool enable)
411 void __iomem *ctrl, *stat;
416 ctrl = core->wrapper_base + WRAPPER_CORE_POWER_CONTROL_V6;
417 stat = core->wrapper_base + WRAPPER_CORE_POWER_STATUS_V6;
418 } else if (coreid == VIDC_CORE_ID_1) {
419 ctrl = core->wrapper_base + WRAPPER_VCODEC0_MMCC_POWER_CONTROL;
420 stat = core->wrapper_base + WRAPPER_VCODEC0_MMCC_POWER_STATUS;
422 ctrl = core->wrapper_base + WRAPPER_VCODEC1_MMCC_POWER_CONTROL;
423 stat = core->wrapper_base + WRAPPER_VCODEC1_MMCC_POWER_STATUS;
429 ret = readl_poll_timeout(stat, val, val & BIT(1), 1, 100);
435 ret = readl_poll_timeout(stat, val, !(val & BIT(1)), 1, 100);
443 static int poweroff_coreid(struct venus_core *core, unsigned int coreid_mask)
447 if (coreid_mask & VIDC_CORE_ID_1) {
448 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, true);
452 vcodec_clks_disable(core, core->vcodec0_clks);
454 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, false);
458 ret = pm_runtime_put_sync(core->pmdomains[1]);
463 if (coreid_mask & VIDC_CORE_ID_2) {
464 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, true);
468 vcodec_clks_disable(core, core->vcodec1_clks);
470 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, false);
474 ret = pm_runtime_put_sync(core->pmdomains[2]);
482 static int poweron_coreid(struct venus_core *core, unsigned int coreid_mask)
486 if (coreid_mask & VIDC_CORE_ID_1) {
487 ret = pm_runtime_get_sync(core->pmdomains[1]);
491 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, true);
495 ret = vcodec_clks_enable(core, core->vcodec0_clks);
499 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, false);
504 if (coreid_mask & VIDC_CORE_ID_2) {
505 ret = pm_runtime_get_sync(core->pmdomains[2]);
509 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, true);
513 ret = vcodec_clks_enable(core, core->vcodec1_clks);
517 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, false);
525 static inline int power_save_mode_enable(struct venus_inst *inst,
528 struct venc_controls *enc_ctr = &inst->controls.enc;
529 const u32 ptype = HFI_PROPERTY_CONFIG_VENC_PERF_MODE;
533 if (inst->session_type != VIDC_SESSION_TYPE_ENC)
536 if (enc_ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
539 venc_mode = enable ? HFI_VENC_PERFMODE_POWER_SAVE :
540 HFI_VENC_PERFMODE_MAX_QUALITY;
542 ret = hfi_session_set_property(inst, ptype, &venc_mode);
546 inst->flags = enable ? inst->flags | VENUS_LOW_POWER :
547 inst->flags & ~VENUS_LOW_POWER;
552 static int move_core_to_power_save_mode(struct venus_core *core,
555 struct venus_inst *inst = NULL;
557 mutex_lock(&core->lock);
558 list_for_each_entry(inst, &core->instances, list) {
559 if (inst->clk_data.core_id == core_id &&
560 inst->session_type == VIDC_SESSION_TYPE_ENC)
561 power_save_mode_enable(inst, true);
563 mutex_unlock(&core->lock);
568 min_loaded_core(struct venus_inst *inst, u32 *min_coreid, u32 *min_load, bool low_power)
570 u32 mbs_per_sec, load, core1_load = 0, core2_load = 0;
571 u32 cores_max = core_num_max(inst);
572 struct venus_core *core = inst->core;
573 struct venus_inst *inst_pos;
574 unsigned long vpp_freq;
577 mutex_lock(&core->lock);
579 list_for_each_entry(inst_pos, &core->instances, list) {
580 if (inst_pos == inst)
583 if (inst_pos->state != INST_START)
586 if (inst->session_type == VIDC_SESSION_TYPE_DEC)
587 vpp_freq = inst_pos->clk_data.vpp_freq;
588 else if (inst->session_type == VIDC_SESSION_TYPE_ENC)
589 vpp_freq = low_power ? inst_pos->clk_data.low_power_freq :
590 inst_pos->clk_data.vpp_freq;
594 coreid = inst_pos->clk_data.core_id;
596 mbs_per_sec = load_per_instance(inst_pos);
597 load = mbs_per_sec * vpp_freq;
599 if ((coreid & VIDC_CORE_ID_3) == VIDC_CORE_ID_3) {
600 core1_load += load / 2;
601 core2_load += load / 2;
602 } else if (coreid & VIDC_CORE_ID_1) {
604 } else if (coreid & VIDC_CORE_ID_2) {
609 *min_coreid = core1_load <= core2_load ?
610 VIDC_CORE_ID_1 : VIDC_CORE_ID_2;
611 *min_load = min(core1_load, core2_load);
613 if (cores_max < VIDC_CORE_ID_2 || core->res->vcodec_num < 2) {
614 *min_coreid = VIDC_CORE_ID_1;
615 *min_load = core1_load;
618 mutex_unlock(&core->lock);
621 static int decide_core(struct venus_inst *inst)
623 const u32 ptype = HFI_PROPERTY_CONFIG_VIDEOCORES_USAGE;
624 struct venus_core *core = inst->core;
625 u32 min_coreid, min_load, cur_inst_load;
626 u32 min_lp_coreid, min_lp_load, cur_inst_lp_load;
627 struct hfi_videocores_usage_type cu;
628 unsigned long max_freq;
631 if (legacy_binding) {
632 if (inst->session_type == VIDC_SESSION_TYPE_DEC)
633 cu.video_core_enable_mask = VIDC_CORE_ID_1;
635 cu.video_core_enable_mask = VIDC_CORE_ID_2;
640 if (inst->clk_data.core_id != VIDC_CORE_ID_DEFAULT)
643 cur_inst_load = load_per_instance(inst);
644 cur_inst_load *= inst->clk_data.vpp_freq;
645 /*TODO : divide this inst->load by work_route */
647 cur_inst_lp_load = load_per_instance(inst);
648 cur_inst_lp_load *= inst->clk_data.low_power_freq;
649 /*TODO : divide this inst->load by work_route */
651 max_freq = core->res->freq_tbl[0].freq;
653 min_loaded_core(inst, &min_coreid, &min_load, false);
654 min_loaded_core(inst, &min_lp_coreid, &min_lp_load, true);
656 if (cur_inst_load + min_load <= max_freq) {
657 inst->clk_data.core_id = min_coreid;
658 cu.video_core_enable_mask = min_coreid;
659 } else if (cur_inst_lp_load + min_load <= max_freq) {
660 /* Move current instance to LP and return */
661 inst->clk_data.core_id = min_coreid;
662 cu.video_core_enable_mask = min_coreid;
663 power_save_mode_enable(inst, true);
664 } else if (cur_inst_lp_load + min_lp_load <= max_freq) {
665 /* Move all instances to LP mode and return */
666 inst->clk_data.core_id = min_lp_coreid;
667 cu.video_core_enable_mask = min_lp_coreid;
668 move_core_to_power_save_mode(core, min_lp_coreid);
670 dev_warn(core->dev, "HW can't support this load");
675 ret = hfi_session_set_property(inst, ptype, &cu);
682 static int acquire_core(struct venus_inst *inst)
684 struct venus_core *core = inst->core;
685 unsigned int coreid_mask = 0;
687 if (inst->core_acquired)
690 inst->core_acquired = true;
692 if (inst->clk_data.core_id & VIDC_CORE_ID_1) {
693 if (core->core0_usage_count++)
696 coreid_mask = VIDC_CORE_ID_1;
699 if (inst->clk_data.core_id & VIDC_CORE_ID_2) {
700 if (core->core1_usage_count++)
703 coreid_mask |= VIDC_CORE_ID_2;
706 return poweron_coreid(core, coreid_mask);
709 static int release_core(struct venus_inst *inst)
711 struct venus_core *core = inst->core;
712 unsigned int coreid_mask = 0;
715 if (!inst->core_acquired)
718 if (inst->clk_data.core_id & VIDC_CORE_ID_1) {
719 if (--core->core0_usage_count)
722 coreid_mask = VIDC_CORE_ID_1;
725 if (inst->clk_data.core_id & VIDC_CORE_ID_2) {
726 if (--core->core1_usage_count)
729 coreid_mask |= VIDC_CORE_ID_2;
732 ret = poweroff_coreid(core, coreid_mask);
737 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
738 inst->core_acquired = false;
742 static int coreid_power_v4(struct venus_inst *inst, int on)
744 struct venus_core *core = inst->core;
750 if (on == POWER_ON) {
751 ret = decide_core(inst);
755 mutex_lock(&core->lock);
756 ret = acquire_core(inst);
757 mutex_unlock(&core->lock);
759 mutex_lock(&core->lock);
760 ret = release_core(inst);
761 mutex_unlock(&core->lock);
767 static int vdec_get_v4(struct device *dev)
769 struct venus_core *core = dev_get_drvdata(dev);
774 return vcodec_clks_get(core, dev, core->vcodec0_clks,
775 core->res->vcodec0_clks);
778 static void vdec_put_v4(struct device *dev)
780 struct venus_core *core = dev_get_drvdata(dev);
786 for (i = 0; i < core->res->vcodec_clks_num; i++)
787 core->vcodec0_clks[i] = NULL;
790 static int vdec_power_v4(struct device *dev, int on)
792 struct venus_core *core = dev_get_drvdata(dev);
798 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, true);
803 ret = vcodec_clks_enable(core, core->vcodec0_clks);
805 vcodec_clks_disable(core, core->vcodec0_clks);
807 vcodec_control_v4(core, VIDC_CORE_ID_1, false);
812 static int venc_get_v4(struct device *dev)
814 struct venus_core *core = dev_get_drvdata(dev);
819 return vcodec_clks_get(core, dev, core->vcodec1_clks,
820 core->res->vcodec1_clks);
823 static void venc_put_v4(struct device *dev)
825 struct venus_core *core = dev_get_drvdata(dev);
831 for (i = 0; i < core->res->vcodec_clks_num; i++)
832 core->vcodec1_clks[i] = NULL;
835 static int venc_power_v4(struct device *dev, int on)
837 struct venus_core *core = dev_get_drvdata(dev);
843 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, true);
848 ret = vcodec_clks_enable(core, core->vcodec1_clks);
850 vcodec_clks_disable(core, core->vcodec1_clks);
852 vcodec_control_v4(core, VIDC_CORE_ID_2, false);
857 static int vcodec_domains_get(struct venus_core *core)
860 struct device **opp_virt_dev;
861 struct device *dev = core->dev;
862 const struct venus_resources *res = core->res;
866 if (!res->vcodec_pmdomains_num)
869 for (i = 0; i < res->vcodec_pmdomains_num; i++) {
870 pd = dev_pm_domain_attach_by_name(dev,
871 res->vcodec_pmdomains[i]);
874 core->pmdomains[i] = pd;
878 if (!core->has_opp_table)
881 /* Attach the power domain for setting performance state */
882 ret = devm_pm_opp_attach_genpd(dev, res->opp_pmdomain, &opp_virt_dev);
886 core->opp_pmdomain = *opp_virt_dev;
887 core->opp_dl_venus = device_link_add(dev, core->opp_pmdomain,
891 if (!core->opp_dl_venus) {
899 for (i = 0; i < res->vcodec_pmdomains_num; i++) {
900 if (IS_ERR_OR_NULL(core->pmdomains[i]))
902 dev_pm_domain_detach(core->pmdomains[i], true);
908 static void vcodec_domains_put(struct venus_core *core)
910 const struct venus_resources *res = core->res;
913 if (!res->vcodec_pmdomains_num)
916 for (i = 0; i < res->vcodec_pmdomains_num; i++) {
917 if (IS_ERR_OR_NULL(core->pmdomains[i]))
919 dev_pm_domain_detach(core->pmdomains[i], true);
923 if (!core->has_opp_table)
926 if (core->opp_dl_venus)
927 device_link_del(core->opp_dl_venus);
930 static int core_resets_reset(struct venus_core *core)
932 const struct venus_resources *res = core->res;
936 if (!res->resets_num)
939 for (i = 0; i < res->resets_num; i++) {
940 ret = reset_control_assert(core->resets[i]);
944 usleep_range(150, 250);
945 ret = reset_control_deassert(core->resets[i]);
954 static int core_resets_get(struct venus_core *core)
956 struct device *dev = core->dev;
957 const struct venus_resources *res = core->res;
961 if (!res->resets_num)
964 for (i = 0; i < res->resets_num; i++) {
966 devm_reset_control_get_exclusive(dev, res->resets[i]);
967 if (IS_ERR(core->resets[i])) {
968 ret = PTR_ERR(core->resets[i]);
976 static int core_get_v4(struct venus_core *core)
978 struct device *dev = core->dev;
979 const struct venus_resources *res = core->res;
982 ret = core_clks_get(core);
986 if (!res->vcodec_pmdomains_num)
987 legacy_binding = true;
989 dev_info(dev, "%s legacy binding\n", legacy_binding ? "" : "non");
991 ret = vcodec_clks_get(core, dev, core->vcodec0_clks, res->vcodec0_clks);
995 ret = vcodec_clks_get(core, dev, core->vcodec1_clks, res->vcodec1_clks);
999 ret = core_resets_get(core);
1006 ret = devm_pm_opp_set_clkname(dev, "core");
1010 if (core->res->opp_pmdomain) {
1011 ret = devm_pm_opp_of_add_table(dev);
1013 core->has_opp_table = true;
1014 } else if (ret != -ENODEV) {
1015 dev_err(dev, "invalid OPP table in device tree\n");
1020 ret = vcodec_domains_get(core);
1027 static void core_put_v4(struct venus_core *core)
1032 vcodec_domains_put(core);
1035 static int core_power_v4(struct venus_core *core, int on)
1037 struct device *dev = core->dev;
1038 struct device *pmctrl = core->pmdomains[0];
1041 if (on == POWER_ON) {
1043 ret = pm_runtime_resume_and_get(pmctrl);
1049 ret = core_resets_reset(core);
1052 pm_runtime_put_sync(pmctrl);
1056 ret = core_clks_enable(core);
1057 if (ret < 0 && pmctrl)
1058 pm_runtime_put_sync(pmctrl);
1060 /* Drop the performance state vote */
1061 if (core->opp_pmdomain)
1062 dev_pm_opp_set_rate(dev, 0);
1064 core_clks_disable(core);
1066 ret = core_resets_reset(core);
1069 pm_runtime_put_sync(pmctrl);
1075 static unsigned long calculate_inst_freq(struct venus_inst *inst,
1076 unsigned long filled_len)
1078 unsigned long vpp_freq_per_mb = 0, vpp_freq = 0, vsp_freq = 0;
1079 u32 fps = (u32)inst->fps;
1082 mbs_per_sec = load_per_instance(inst);
1084 if (inst->state != INST_START)
1087 if (inst->session_type == VIDC_SESSION_TYPE_ENC) {
1088 vpp_freq_per_mb = inst->flags & VENUS_LOW_POWER ?
1089 inst->clk_data.low_power_freq :
1090 inst->clk_data.vpp_freq;
1092 vpp_freq = mbs_per_sec * vpp_freq_per_mb;
1094 vpp_freq = mbs_per_sec * inst->clk_data.vpp_freq;
1097 /* 21 / 20 is overhead factor */
1098 vpp_freq += vpp_freq / 20;
1099 vsp_freq = mbs_per_sec * inst->clk_data.vsp_freq;
1101 /* 10 / 7 is overhead factor */
1102 if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1103 vsp_freq += (inst->controls.enc.bitrate * 10) / 7;
1105 vsp_freq += ((fps * filled_len * 8) * 10) / 7;
1107 return max(vpp_freq, vsp_freq);
1110 static int load_scale_v4(struct venus_inst *inst)
1112 struct venus_core *core = inst->core;
1113 const struct freq_tbl *table = core->res->freq_tbl;
1114 unsigned int num_rows = core->res->freq_tbl_size;
1115 struct device *dev = core->dev;
1116 unsigned long freq = 0, freq_core1 = 0, freq_core2 = 0;
1117 unsigned long filled_len = 0;
1120 for (i = 0; i < inst->num_input_bufs; i++)
1121 filled_len = max(filled_len, inst->payloads[i]);
1123 if (inst->session_type == VIDC_SESSION_TYPE_DEC && !filled_len)
1126 freq = calculate_inst_freq(inst, filled_len);
1127 inst->clk_data.freq = freq;
1129 mutex_lock(&core->lock);
1130 list_for_each_entry(inst, &core->instances, list) {
1131 if (inst->clk_data.core_id == VIDC_CORE_ID_1) {
1132 freq_core1 += inst->clk_data.freq;
1133 } else if (inst->clk_data.core_id == VIDC_CORE_ID_2) {
1134 freq_core2 += inst->clk_data.freq;
1135 } else if (inst->clk_data.core_id == VIDC_CORE_ID_3) {
1136 freq_core1 += inst->clk_data.freq;
1137 freq_core2 += inst->clk_data.freq;
1141 freq = max(freq_core1, freq_core2);
1143 if (freq > table[0].freq) {
1144 dev_dbg(dev, VDBGL "requested clock rate: %lu scaling clock rate : %lu\n",
1145 freq, table[0].freq);
1147 freq = table[0].freq;
1151 for (i = num_rows - 1 ; i >= 0; i--) {
1152 if (freq <= table[i].freq) {
1153 freq = table[i].freq;
1160 ret = core_clks_set_rate(core, freq);
1162 dev_err(dev, "failed to set clock rate %lu (%d)\n",
1167 ret = load_scale_bw(core);
1169 dev_err(dev, "failed to set bandwidth (%d)\n",
1175 mutex_unlock(&core->lock);
1179 static const struct venus_pm_ops pm_ops_v4 = {
1180 .core_get = core_get_v4,
1181 .core_put = core_put_v4,
1182 .core_power = core_power_v4,
1183 .vdec_get = vdec_get_v4,
1184 .vdec_put = vdec_put_v4,
1185 .vdec_power = vdec_power_v4,
1186 .venc_get = venc_get_v4,
1187 .venc_put = venc_put_v4,
1188 .venc_power = venc_power_v4,
1189 .coreid_power = coreid_power_v4,
1190 .load_scale = load_scale_v4,
1193 const struct venus_pm_ops *venus_pm_get(enum hfi_version version)
1196 case HFI_VERSION_1XX:
1199 case HFI_VERSION_3XX:
1201 case HFI_VERSION_4XX:
1202 case HFI_VERSION_6XX: