Commit 4ebf9693 authored by Aniket Masule's avatar Aniket Masule Committed by Mauro Carvalho Chehab

media: venus: introduce core selection

Presently the core (vcodec pipelines) assignment is static. Here we
introduce dynamic load balancing across the cores depending on the
current session load. The load on earch core is calculated and core
with minimum load is assigned to given instance. This will be
applicable on Venus v4 with more than one vcodec cores.
Signed-off-by: default avatarAniket Masule <amasule@codeaurora.org>
Co-developed-by: default avatarStanimir Varbanov <stanimir.varbanov@linaro.org>
Signed-off-by: default avatarStanimir Varbanov <stanimir.varbanov@linaro.org>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+huawei@kernel.org>
parent 7482a983
...@@ -181,6 +181,8 @@ struct venus_core { ...@@ -181,6 +181,8 @@ struct venus_core {
struct delayed_work work; struct delayed_work work;
struct venus_caps caps[MAX_CODEC_NUM]; struct venus_caps caps[MAX_CODEC_NUM];
unsigned int codecs_count; unsigned int codecs_count;
unsigned int core0_usage_count;
unsigned int core1_usage_count;
}; };
struct vdec_controls { struct vdec_controls {
...@@ -380,6 +382,7 @@ struct venus_inst { ...@@ -380,6 +382,7 @@ struct venus_inst {
const struct hfi_inst_ops *ops; const struct hfi_inst_ops *ops;
u32 session_type; u32 session_type;
union hfi_get_property hprop; union hfi_get_property hprop;
unsigned int core_acquired: 1;
}; };
#define IS_V1(core) ((core)->res->hfi_version == HFI_VERSION_1XX) #define IS_V1(core) ((core)->res->hfi_version == HFI_VERSION_1XX)
......
...@@ -723,21 +723,6 @@ int venus_helper_set_work_mode(struct venus_inst *inst, u32 mode) ...@@ -723,21 +723,6 @@ int venus_helper_set_work_mode(struct venus_inst *inst, u32 mode)
} }
EXPORT_SYMBOL_GPL(venus_helper_set_work_mode); EXPORT_SYMBOL_GPL(venus_helper_set_work_mode);
int venus_helper_set_core_usage(struct venus_inst *inst, u32 usage)
{
const u32 ptype = HFI_PROPERTY_CONFIG_VIDEOCORES_USAGE;
struct hfi_videocores_usage_type cu;
inst->clk_data.core_id = usage;
if (!IS_V4(inst->core))
return 0;
cu.video_core_enable_mask = usage;
return hfi_session_set_property(inst, ptype, &cu);
}
EXPORT_SYMBOL_GPL(venus_helper_set_core_usage);
int venus_helper_init_codec_freq_data(struct venus_inst *inst) int venus_helper_init_codec_freq_data(struct venus_inst *inst)
{ {
const struct codec_freq_data *data; const struct codec_freq_data *data;
...@@ -1112,6 +1097,8 @@ void venus_helper_vb2_stop_streaming(struct vb2_queue *q) ...@@ -1112,6 +1097,8 @@ void venus_helper_vb2_stop_streaming(struct vb2_queue *q)
else else
inst->streamon_cap = 0; inst->streamon_cap = 0;
venus_pm_release_core(inst);
mutex_unlock(&inst->lock); mutex_unlock(&inst->lock);
} }
EXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming); EXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming);
......
...@@ -34,7 +34,6 @@ int venus_helper_set_output_resolution(struct venus_inst *inst, ...@@ -34,7 +34,6 @@ int venus_helper_set_output_resolution(struct venus_inst *inst,
u32 buftype); u32 buftype);
int venus_helper_set_work_mode(struct venus_inst *inst, u32 mode); int venus_helper_set_work_mode(struct venus_inst *inst, u32 mode);
int venus_helper_init_codec_freq_data(struct venus_inst *inst); int venus_helper_init_codec_freq_data(struct venus_inst *inst);
int venus_helper_set_core_usage(struct venus_inst *inst, u32 usage);
int venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs, int venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs,
unsigned int output_bufs, unsigned int output_bufs,
unsigned int output2_bufs); unsigned int output2_bufs);
......
...@@ -550,6 +550,7 @@ struct hfi_bitrate { ...@@ -550,6 +550,7 @@ struct hfi_bitrate {
#define HFI_CAPABILITY_LCU_SIZE 0x14 #define HFI_CAPABILITY_LCU_SIZE 0x14
#define HFI_CAPABILITY_HIER_P_HYBRID_NUM_ENH_LAYERS 0x15 #define HFI_CAPABILITY_HIER_P_HYBRID_NUM_ENH_LAYERS 0x15
#define HFI_CAPABILITY_MBS_PER_SECOND_POWERSAVE 0x16 #define HFI_CAPABILITY_MBS_PER_SECOND_POWERSAVE 0x16
#define HFI_CAPABILITY_MAX_VIDEOCORES 0x2b
struct hfi_capability { struct hfi_capability {
u32 capability_type; u32 capability_type;
......
...@@ -107,4 +107,9 @@ static inline u32 frate_step(struct venus_inst *inst) ...@@ -107,4 +107,9 @@ static inline u32 frate_step(struct venus_inst *inst)
return cap_step(inst, HFI_CAPABILITY_FRAMERATE); return cap_step(inst, HFI_CAPABILITY_FRAMERATE);
} }
static inline u32 core_num_max(struct venus_inst *inst)
{
return cap_max(inst, HFI_CAPABILITY_MAX_VIDEOCORES);
}
#endif #endif
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <media/v4l2-mem2mem.h> #include <media/v4l2-mem2mem.h>
#include "core.h" #include "core.h"
#include "hfi_parser.h"
#include "hfi_venus_io.h" #include "hfi_venus_io.h"
#include "pm_helpers.h" #include "pm_helpers.h"
...@@ -480,6 +481,173 @@ static int poweron_coreid(struct venus_core *core, unsigned int coreid_mask) ...@@ -480,6 +481,173 @@ static int poweron_coreid(struct venus_core *core, unsigned int coreid_mask)
return 0; return 0;
} }
static void
min_loaded_core(struct venus_inst *inst, u32 *min_coreid, u32 *min_load)
{
u32 mbs_per_sec, load, core1_load = 0, core2_load = 0;
u32 cores_max = core_num_max(inst);
struct venus_core *core = inst->core;
struct venus_inst *inst_pos;
unsigned long vpp_freq;
u32 coreid;
mutex_lock(&core->lock);
list_for_each_entry(inst_pos, &core->instances, list) {
if (inst_pos == inst)
continue;
vpp_freq = inst_pos->clk_data.codec_freq_data->vpp_freq;
coreid = inst_pos->clk_data.core_id;
mbs_per_sec = load_per_instance(inst_pos);
load = mbs_per_sec * vpp_freq;
if ((coreid & VIDC_CORE_ID_3) == VIDC_CORE_ID_3) {
core1_load += load / 2;
core2_load += load / 2;
} else if (coreid & VIDC_CORE_ID_1) {
core1_load += load;
} else if (coreid & VIDC_CORE_ID_2) {
core2_load += load;
}
}
*min_coreid = core1_load <= core2_load ?
VIDC_CORE_ID_1 : VIDC_CORE_ID_2;
*min_load = min(core1_load, core2_load);
if (cores_max < VIDC_CORE_ID_2 || core->res->vcodec_num < 2) {
*min_coreid = VIDC_CORE_ID_1;
*min_load = core1_load;
}
mutex_unlock(&core->lock);
}
static int decide_core(struct venus_inst *inst)
{
const u32 ptype = HFI_PROPERTY_CONFIG_VIDEOCORES_USAGE;
struct venus_core *core = inst->core;
u32 min_coreid, min_load, inst_load;
struct hfi_videocores_usage_type cu;
unsigned long max_freq;
if (legacy_binding) {
if (inst->session_type == VIDC_SESSION_TYPE_DEC)
cu.video_core_enable_mask = VIDC_CORE_ID_1;
else
cu.video_core_enable_mask = VIDC_CORE_ID_2;
goto done;
}
if (inst->clk_data.core_id != VIDC_CORE_ID_DEFAULT)
return 0;
inst_load = load_per_instance(inst);
inst_load *= inst->clk_data.codec_freq_data->vpp_freq;
max_freq = core->res->freq_tbl[0].freq;
min_loaded_core(inst, &min_coreid, &min_load);
if ((inst_load + min_load) > max_freq) {
dev_warn(core->dev, "HW is overloaded, needed: %u max: %lu\n",
inst_load, max_freq);
return -EINVAL;
}
inst->clk_data.core_id = min_coreid;
cu.video_core_enable_mask = min_coreid;
done:
return hfi_session_set_property(inst, ptype, &cu);
}
static int acquire_core(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
unsigned int coreid_mask = 0;
if (inst->core_acquired)
return 0;
inst->core_acquired = true;
if (inst->clk_data.core_id & VIDC_CORE_ID_1) {
if (core->core0_usage_count++)
return 0;
coreid_mask = VIDC_CORE_ID_1;
}
if (inst->clk_data.core_id & VIDC_CORE_ID_2) {
if (core->core1_usage_count++)
return 0;
coreid_mask |= VIDC_CORE_ID_2;
}
return poweron_coreid(core, coreid_mask);
}
static int release_core(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
unsigned int coreid_mask = 0;
int ret;
if (!inst->core_acquired)
return 0;
if (inst->clk_data.core_id & VIDC_CORE_ID_1) {
if (--core->core0_usage_count)
goto done;
coreid_mask = VIDC_CORE_ID_1;
}
if (inst->clk_data.core_id & VIDC_CORE_ID_2) {
if (--core->core1_usage_count)
goto done;
coreid_mask |= VIDC_CORE_ID_2;
}
ret = poweroff_coreid(core, coreid_mask);
if (ret)
return ret;
done:
inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
inst->core_acquired = false;
return 0;
}
static int coreid_power_v4(struct venus_inst *inst, int on)
{
struct venus_core *core = inst->core;
int ret;
if (legacy_binding)
return 0;
if (on == POWER_ON) {
ret = decide_core(inst);
if (ret)
return ret;
mutex_lock(&core->lock);
ret = acquire_core(inst);
mutex_unlock(&core->lock);
} else {
mutex_lock(&core->lock);
ret = release_core(inst);
mutex_unlock(&core->lock);
}
return ret;
}
static int vdec_get_v4(struct device *dev) static int vdec_get_v4(struct device *dev)
{ {
struct venus_core *core = dev_get_drvdata(dev); struct venus_core *core = dev_get_drvdata(dev);
...@@ -661,22 +829,12 @@ static void core_put_v4(struct device *dev) ...@@ -661,22 +829,12 @@ static void core_put_v4(struct device *dev)
static int core_power_v4(struct device *dev, int on) static int core_power_v4(struct device *dev, int on)
{ {
struct venus_core *core = dev_get_drvdata(dev); struct venus_core *core = dev_get_drvdata(dev);
const unsigned int coreid_mask = VIDC_CORE_ID_1 | VIDC_CORE_ID_2;
int ret = 0; int ret = 0;
if (on == POWER_ON) { if (on == POWER_ON)
ret = core_clks_enable(core); ret = core_clks_enable(core);
if (ret) else
return ret;
if (!legacy_binding)
ret = poweron_coreid(core, coreid_mask);
} else {
if (!legacy_binding)
ret = poweroff_coreid(core, coreid_mask);
core_clks_disable(core); core_clks_disable(core);
}
return ret; return ret;
} }
...@@ -781,6 +939,7 @@ static const struct venus_pm_ops pm_ops_v4 = { ...@@ -781,6 +939,7 @@ static const struct venus_pm_ops pm_ops_v4 = {
.venc_get = venc_get_v4, .venc_get = venc_get_v4,
.venc_put = venc_put_v4, .venc_put = venc_put_v4,
.venc_power = venc_power_v4, .venc_power = venc_power_v4,
.coreid_power = coreid_power_v4,
.load_scale = load_scale_v4, .load_scale = load_scale_v4,
}; };
......
...@@ -21,6 +21,8 @@ struct venus_pm_ops { ...@@ -21,6 +21,8 @@ struct venus_pm_ops {
void (*venc_put)(struct device *dev); void (*venc_put)(struct device *dev);
int (*venc_power)(struct device *dev, int on); int (*venc_power)(struct device *dev, int on);
int (*coreid_power)(struct venus_inst *inst, int on);
int (*load_scale)(struct venus_inst *inst); int (*load_scale)(struct venus_inst *inst);
}; };
...@@ -36,4 +38,28 @@ static inline int venus_pm_load_scale(struct venus_inst *inst) ...@@ -36,4 +38,28 @@ static inline int venus_pm_load_scale(struct venus_inst *inst)
return core->pm_ops->load_scale(inst); return core->pm_ops->load_scale(inst);
} }
static inline int venus_pm_acquire_core(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
const struct venus_pm_ops *pm_ops = core->pm_ops;
int ret = 0;
if (pm_ops && pm_ops->coreid_power)
ret = pm_ops->coreid_power(inst, POWER_ON);
return ret;
}
static inline int venus_pm_release_core(struct venus_inst *inst)
{
struct venus_core *core = inst->core;
const struct venus_pm_ops *pm_ops = core->pm_ops;
int ret = 0;
if (pm_ops && pm_ops->coreid_power)
ret = pm_ops->coreid_power(inst, POWER_OFF);
return ret;
}
#endif #endif
...@@ -579,10 +579,6 @@ static int vdec_output_conf(struct venus_inst *inst) ...@@ -579,10 +579,6 @@ static int vdec_output_conf(struct venus_inst *inst)
if (ret) if (ret)
return ret; return ret;
ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_1);
if (ret)
return ret;
if (core->res->hfi_version == HFI_VERSION_1XX) { if (core->res->hfi_version == HFI_VERSION_1XX) {
ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER; ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
ret = hfi_session_set_property(inst, ptype, &en); ret = hfi_session_set_property(inst, ptype, &en);
...@@ -951,6 +947,10 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count) ...@@ -951,6 +947,10 @@ static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
mutex_lock(&inst->lock); mutex_lock(&inst->lock);
ret = venus_pm_acquire_core(inst);
if (ret)
return ret;
if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
ret = vdec_start_capture(inst); ret = vdec_start_capture(inst);
else else
...@@ -1078,6 +1078,7 @@ static void vdec_session_release(struct venus_inst *inst) ...@@ -1078,6 +1078,7 @@ static void vdec_session_release(struct venus_inst *inst)
venus_helper_free_dpb_bufs(inst); venus_helper_free_dpb_bufs(inst);
venus_pm_load_scale(inst); venus_pm_load_scale(inst);
venus_pm_release_core(inst);
INIT_LIST_HEAD(&inst->registeredbufs); INIT_LIST_HEAD(&inst->registeredbufs);
mutex_unlock(&inst->lock); mutex_unlock(&inst->lock);
...@@ -1337,6 +1338,8 @@ static int vdec_open(struct file *file) ...@@ -1337,6 +1338,8 @@ static int vdec_open(struct file *file)
inst->num_output_bufs = 1; inst->num_output_bufs = 1;
inst->codec_state = VENUS_DEC_STATE_DEINIT; inst->codec_state = VENUS_DEC_STATE_DEINIT;
inst->buf_count = 0; inst->buf_count = 0;
inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
inst->core_acquired = false;
init_waitqueue_head(&inst->reconf_wait); init_waitqueue_head(&inst->reconf_wait);
venus_helper_init_instance(inst); venus_helper_init_instance(inst);
......
...@@ -656,10 +656,6 @@ static int venc_set_properties(struct venus_inst *inst) ...@@ -656,10 +656,6 @@ static int venc_set_properties(struct venus_inst *inst)
if (ret) if (ret)
return ret; return ret;
ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_2);
if (ret)
return ret;
ptype = HFI_PROPERTY_CONFIG_FRAME_RATE; ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
frate.buffer_type = HFI_BUFFER_OUTPUT; frate.buffer_type = HFI_BUFFER_OUTPUT;
frate.framerate = inst->fps * (1 << 16); frate.framerate = inst->fps * (1 << 16);
...@@ -992,6 +988,10 @@ static int venc_start_streaming(struct vb2_queue *q, unsigned int count) ...@@ -992,6 +988,10 @@ static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
if (ret) if (ret)
goto bufs_done; goto bufs_done;
ret = venus_pm_acquire_core(inst);
if (ret)
goto deinit_sess;
ret = venc_set_properties(inst); ret = venc_set_properties(inst);
if (ret) if (ret)
goto deinit_sess; goto deinit_sess;
...@@ -1160,6 +1160,8 @@ static int venc_open(struct file *file) ...@@ -1160,6 +1160,8 @@ static int venc_open(struct file *file)
inst->core = core; inst->core = core;
inst->session_type = VIDC_SESSION_TYPE_ENC; inst->session_type = VIDC_SESSION_TYPE_ENC;
inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
inst->core_acquired = false;
venus_helper_init_instance(inst); venus_helper_init_instance(inst);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment