Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

@@ -0,0 +1,86 @@
ifeq (y, $(filter y, $(CONFIG_MACH_MT6781)))
MTK_PLATFORM := mt6781
else
MTK_PLATFORM := $(CONFIG_MTK_PLATFORM:"%"=%)
endif
ifeq (y, $(filter y, $(CONFIG_MACH_MT6893)))
MTK_PLATFORM := mt6885
endif
ifeq (y, $(filter y, $(CONFIG_MACH_MT6833)))
MTK_PLATFORM := mt6833
endif
ifeq (y, $(filter y, $(CONFIG_MACH_MT6877)))
MTK_PLATFORM := mt6877
endif
ifeq (y, $(filter y, $(CONFIG_MACH_MT6781)))
MTK_PLATFORM := mt6781
endif
obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
mtk-vcodec-enc.o \
mtk-vcodec-common.o \
$(MTK_PLATFORM)/vcodec_dvfs.o \
$(MTK_PLATFORM)/vcodec_bw.o
mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
vdec_drv_if.o \
mtk_vcodec_dec.o \
$(MTK_PLATFORM)/mtk_vcodec_dec_pm.o \
ifdef CONFIG_VIDEO_MEDIATEK_VCU
mtk-vcodec-dec-y += vdec/vdec_common_if.o \
vdec_vcu_if.o
endif
ifdef CONFIG_VIDEO_MEDIATEK_VPU
mtk-vcodec-dec-y += vdec/vdec_h264_if.o \
vdec/vdec_vp8_if.o \
vdec/vdec_vp9_if.o \
vdec_vpu_if.o
endif
mtk-vcodec-enc-y := mtk_vcodec_enc.o \
mtk_vcodec_enc_drv.o \
$(MTK_PLATFORM)/mtk_vcodec_enc_pm.o \
venc_drv_if.o
ifdef CONFIG_VIDEO_MEDIATEK_VCU
mtk-vcodec-enc-y += venc/venc_common_if.o \
venc_vcu_if.o
endif
ifdef CONFIG_VIDEO_MEDIATEK_VPU
mtk-vcodec-enc-y += venc/venc_vp8_if.o \
venc/venc_h264_if.o \
venc_vpu_if.o
endif
mtk-vcodec-common-y := $(MTK_PLATFORM)/mtk_vcodec_intr.o \
mtk_vcodec_util.o\
subdir-ccflags-y += -Werror
ifdef CONFIG_VIDEO_MEDIATEK_VCU
ccflags-y += -I$(srctree)/drivers/media/platform/mtk-vcu
endif
ifdef CONFIG_VIDEO_MEDIATEK_VPU
ccflags-y += -I$(srctree)/drivers/media/platform/mtk-vpu
endif
ifdef CONFIG_MTK_PSEUDO_M4U
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat/$(MTK_PLATFORM)/include
endif
ccflags-y += -I$(srctree)/drivers/media/platform/mtk-vcodec
ccflags-y += -I$(srctree)/drivers/media/platform/mtk-vcodec/$(MTK_PLATFORM)
ccflags-y += -I$(srctree)/drivers/media/platform/mtk-videobuf
ccflags-y += -I$(srctree)/drivers/misc/mediatek/smi
ccflags-y += -I$(srctree)/drivers/misc/mediatek/smi/$(MTK_PLATFORM)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/smi/variant
ccflags-y += -I$(srctree)/drivers/misc/mediatek/mmqos
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/qos/$(MTK_PLATFORM)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat/$(MTK_PLATFORM)/include
ccflags-y += -I$(srctree)/drivers/iommu/
ccflags-y += -I$(srctree)/arch/arm
ccflags-y += -I$(srctree)/drivers/misc/mediatek/mmdvfs
ccflags-y += -I$(srctree)/drivers/misc/mediatek/slbc
ccflags-y += -I$(srctree)/drivers/staging/android/mtk_ion
ccflags-y += -I$(srctree)/drivers/staging/android/mtk_ion/mtk
@@ -0,0 +1,405 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "smi_public.h"
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#if DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 228
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#if DEC_EMI_BW
#include <mtk_smi.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
struct mtk_pm_qos_request vdec_qos_req_bw;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,vdec_gcon");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_SCP_SYS_VDE =
devm_clk_get(&pdev->dev, "MT_SCP_SYS_VDE");
if (IS_ERR(pm->clk_MT_SCP_SYS_VDE)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_VDE\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_VDE);
}
pm->clk_MT_SCP_SYS_DIS =
devm_clk_get(&pdev->dev, "MT_SCP_SYS_DIS");
if (IS_ERR(pm->clk_MT_SCP_SYS_DIS)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_DIS\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_DIS);
}
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#if DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
smi_bus_prepare_enable(SMI_LARB1, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB1, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
struct mtk_vcodec_ctx *ctx = NULL;
int misc_offset[4] = {64, 66, 67, 65};
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset, idx;
unsigned long value;
u32 fourcc;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
if (readl(vdec_gcon_addr) == 0) {
mtk_v4l2_debug(0, "VDEC not HW break since clk off. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
return;
}
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
while (!((cg_status & 0x1) && (cg_status & 0x10))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
value = readl(vdec_gcon_addr + (0 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
0 << 2, 0, value);
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
for (offset = 64; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
for (idx = 0; idx < 4; idx++) {
offset = misc_offset[idx];
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
if (timeout == 20000)
timeout = 1000000;
else if (timeout == 1000000) {
/* v4l2_aee_print(
* "%s %p codec:0x%08x(%c%c%c%c) hw break timeout\n",
* __func__, ctx, fourcc,
* fourcc & 0xFF, (fourcc >> 8) & 0xFF,
* (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
*/
break;
}
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
}
/* sw reset */
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_prepare_vdec_dvfs(void)
{
#if DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#if DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mtk_pm_qos_add_request(&vdec_qos_req_bw, MTK_PM_QOS_MM_MEMORY_BANDWIDTH,
PM_QOS_DEFAULT_VALUE);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mtk_pm_qos_remove_request(&vdec_qos_req_bw);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq;
if (vdec_freq > target_freq_64)
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job != 0 && vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long emi_bw = 0;
long emi_bw_input = 0;
long emi_bw_output = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8L * 1920 * 1080 * 3 * 10 * vdec_freq;
emi_bw_input = 8 * vdec_freq / STD_VDEC_FREQ;
emi_bw_output = 1920 * 1088 * 3 * 30 * 10 * vdec_freq /
2 / 3 / STD_VDEC_FREQ / 1024 / 1024;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
/* transaction bytes to occupied BW */
emi_bw = emi_bw * 4 / 3;
/* bits/s to MBytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
mtk_pm_qos_update_request(&vdec_qos_req_bw, (int)emi_bw);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#if DEC_EMI_BW
mtk_pm_qos_update_request(&vdec_qos_req_bw, 0);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#if DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,332 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#if ENC_DVFS
//#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 312
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
#endif
#if ENC_EMI_BW
static unsigned int gVENCFrmTRAVC[3] = {6, 12, 6};
static unsigned int gVENCFrmTRHEVC[3] = {6, 12, 6};
struct mtk_pm_qos_request venc_qos_req_bw;
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,venc_gcon");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_SCP_SYS_VEN = devm_clk_get(&pdev->dev, "MT_SCP_SYS_VEN");
if (IS_ERR(pm->clk_MT_SCP_SYS_VEN)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_VEN\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_VEN);
}
pm->clk_MT_SCP_SYS_DIS = devm_clk_get(&pdev->dev, "MT_SCP_SYS_DIS");
if (IS_ERR(pm->clk_MT_SCP_SYS_DIS)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_DIS\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_DIS);
}
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
#if ENC_EMI_BW
/* do nothing */
#endif
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB4, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB4, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
free_hist(&venc_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mtk_pm_qos_add_request(&venc_qos_req_bw, MTK_PM_QOS_MM_MEMORY_BANDWIDTH,
PM_QOS_DEFAULT_VALUE);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mtk_pm_qos_remove_request(&venc_qos_req_bw);
#endif
}
void mtk_venc_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (ctx->async_mode == 1 && target_freq_64 > 416)
target_freq_64 = 416;
if (target_freq > 0) {
venc_freq = target_freq;
if (venc_freq > target_freq_64)
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->async_mode == 0) {
update_hist(venc_cur_job, &venc_hists, 0);
} else {
/* Set allowed time for slowmotion 4 buffer pack */
if (ctx->enc_params.operationrate > 0) {
interval = (long long)(1000 * 4 /
(int)ctx->enc_params.operationrate);
} else {
if (ctx->enc_params.framerate_denom == 0)
ctx->enc_params.framerate_denom = 1;
if (ctx->enc_params.operationrate == 0 &&
ctx->enc_params.framerate_num == 0)
ctx->enc_params.framerate_num =
ctx->enc_params.framerate_denom * 30;
interval = (long long)(1000 * 4 /
(int)(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom));
}
update_hist(venc_cur_job, &venc_hists, interval*1000);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
int f_type = 1; /* TODO */
int boost_perc = 0;
long emi_bw = 0;
if (ctx->async_mode == 1)
boost_perc = 100;
if (ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H265)
boost_perc = 150;
emi_bw = 8L * 1920 * 1080 * 3 * 10 * venc_freq;
switch (ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * gVENCFrmTRAVC[f_type] *
(100 + boost_perc) / (2 * STD_VENC_FREQ) / 100;
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * gVENCFrmTRHEVC[f_type] *
(100 + boost_perc) / (2 * STD_VENC_FREQ) / 100;
break;
default:
emi_bw = 0;
pr_debug("Unsupported encoder type for BW");
}
/* transaction bytes to occupied BW */
emi_bw = emi_bw * 4 / 3;
/* bits/s to mbytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
mtk_pm_qos_update_request(&venc_qos_req_bw, (int)emi_bw);
#endif
}
void mtk_venc_emi_bw_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
mtk_pm_qos_update_request(&venc_qos_req_bw, 0);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
#if ENC_DVFS
mutex_lock(&ctx->dev->enc_dvfs_mutex);
add_job(&ctx->id, &venc_jobs);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_begin(ctx);
mtk_venc_emi_bw_begin(ctx);
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_end(ctx);
mtk_venc_emi_bw_end(ctx);
}
/* Total job count after this one is inserted */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
/* Remaining job count after this one is done */
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,241 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
ctx->irq_status = readl(dev->enc_reg_base[VENC_LT_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
addr = dev->enc_reg_base[VENC_LT_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_lt_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6779"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_VLD,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_LT_SYS,
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,672 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT (33*1000)
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
if (hist == 0)
return 0;
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away, restart */
if (hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
hist->kcy[0] = (int)div_64(job->mhz * (job->end - job->start),
1000LL);
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
hist->kcy[hist_idx] = (int)div_64(job->mhz * (job->end - job->start),
1000LL);
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
#endif
@@ -0,0 +1,426 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#ifndef FPGA_PWRCLK_API_DISABLE
#include "smi_public.h"
#endif
#if DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include <mmdvfs_config_util.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 312
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#if DEC_EMI_BW
#include <mtk_smi.h>
#include <smi_port.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
static struct plist_head vdec_rlist;
static struct mm_qos_request vdec_mc;
static struct mm_qos_request vdec_ufo;
static struct mm_qos_request vdec_pp;
static struct mm_qos_request vdec_pred_rd;
static struct mm_qos_request vdec_pred_wr;
static struct mm_qos_request vdec_ppwrap;
static struct mm_qos_request vdec_tile;
static struct mm_qos_request vdec_vld;
static struct mm_qos_request vdec_vld2;
static struct mm_qos_request vdec_avc_mv;
static struct mm_qos_request vdec_ufo_enc;
static struct mm_qos_request vdec_rg_ctrl_dma;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,vdec_gcon");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#if DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
smi_bus_prepare_enable(SMI_LARB2, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB2, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
struct mtk_vcodec_ctx *ctx = NULL;
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset;
unsigned long value;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
while (!((cg_status & 0x1) && (cg_status & 0x10))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x",
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc);
for (offset = 68; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
if (timeout == 20000)
timeout = 1000000;
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
}
/* sw reset */
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_prepare_vdec_dvfs(void)
{
#if DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#if DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
plist_head_init(&vdec_rlist);
mm_qos_add_request(&vdec_rlist, &vdec_mc, SMI_PORT_VDEC_MC);
mm_qos_add_request(&vdec_rlist, &vdec_ufo, SMI_PORT_VDEC_UFO);
mm_qos_add_request(&vdec_rlist, &vdec_pp, SMI_PORT_VDEC_PP);
mm_qos_add_request(&vdec_rlist, &vdec_pred_rd, SMI_PORT_VDEC_PRED_RD);
mm_qos_add_request(&vdec_rlist, &vdec_pred_wr, SMI_PORT_VDEC_PRED_WR);
mm_qos_add_request(&vdec_rlist, &vdec_ppwrap, SMI_PORT_VDEC_PPWRAP);
mm_qos_add_request(&vdec_rlist, &vdec_tile, SMI_PORT_VDEC_TILE);
mm_qos_add_request(&vdec_rlist, &vdec_vld, SMI_PORT_VDEC_VLD);
mm_qos_add_request(&vdec_rlist, &vdec_vld2, SMI_PORT_VDEC_VLD2);
mm_qos_add_request(&vdec_rlist, &vdec_avc_mv, SMI_PORT_VDEC_AVC_MV);
mm_qos_add_request(&vdec_rlist, &vdec_ufo_enc, SMI_PORT_VDEC_UFO_ENC);
mm_qos_add_request(&vdec_rlist, &vdec_rg_ctrl_dma,
SMI_PORT_VDEC_RG_CTRL_DMA);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mm_qos_remove_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq;
if (vdec_freq > target_freq_64)
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f,
target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long long emi_bw = 0;
long long emi_bw_input = 0;
long long emi_bw_output = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8LL * 1920 * 1080 * 3 * 10 * vdec_freq;
emi_bw_input = 8LL * vdec_freq / STD_VDEC_FREQ;
emi_bw_output = 1920LL * 1088 * 3 * 30 * 10 * vdec_freq /
2 / 3 / STD_VDEC_FREQ / 1024 / 1024;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
case V4L2_PIX_FMT_HEIF:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
/* bits/s to MBytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
if (0) { /* UFO */
emi_bw = emi_bw * 6 / 10;
emi_bw_output = emi_bw_output * 6 / 10;
}
emi_bw = emi_bw - emi_bw_output - (emi_bw_input * 2);
if (emi_bw < 0)
emi_bw = 0;
if (0) { /* UFO */
mm_qos_set_request(&vdec_ufo, (long)emi_bw, 0, BW_COMP_DEFAULT);
mm_qos_set_request(&vdec_ufo_enc, (long)emi_bw_output, 0,
BW_COMP_DEFAULT);
} else {
mm_qos_set_request(&vdec_mc, (long)emi_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, (long)emi_bw_output, 0,
BW_COMP_NONE);
}
mm_qos_set_request(&vdec_pred_rd, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, (long)emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, (long)emi_bw_input * 2, 0,
BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#if DEC_EMI_BW
mm_qos_set_request(&vdec_mc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo_enc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#if DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,33 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,416 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#ifndef FPGA_PWRCLK_API_DISABLE
#include "smi_public.h"
#endif
#if ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include <mmdvfs_config_util.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 364
#define STD_LUMA_BW 100
#define STD_CHROMA_BW 50
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
#include <smi_port.h>
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,venc_gcon");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
#if ENC_EMI_BW
#endif
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
free_hist_by_handle(&ctx->id, &venc_hists);
#endif
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB3, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB3, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
free_hist(&venc_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu, SMI_PORT_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec, SMI_PORT_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma, SMI_PORT_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv, SMI_PORT_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv, SMI_PORT_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma, SMI_PORT_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma,
SMI_PORT_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma, SMI_PORT_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma,
SMI_PORT_VENC_REF_CHROMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist);
#endif
}
void mtk_venc_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (ctx->dev->enc_cnt > 1) {
/* Reduce available time / increase freq */
target_freq = target_freq * 2;
if (ctx->enc_params.svp_mode > 0)
target_freq = 630;
}
if (ctx->enc_params.operationrate >= 120 &&
(target_freq_64 > 450 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc ==
V4L2_PIX_FMT_H264))
target_freq_64 = 450;
if (target_freq > 0) {
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f,
target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
#if ENC_DVFS
static int mtk_venc_get_exec_cnt(struct mtk_vcodec_ctx *ctx)
{
return (int)((readl(ctx->dev->enc_reg_base[VENC_SYS] + 0x17C) &
0x7FFFFFFF) / 1000);
}
#endif
void mtk_venc_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->enc_params.operationrate < 120) {
if (ctx->enc_params.framerate_denom == 0)
ctx->enc_params.framerate_denom = 1;
if (ctx->enc_params.operationrate == 0 &&
ctx->enc_params.framerate_num == 0)
ctx->enc_params.framerate_num =
ctx->enc_params.framerate_denom * 30;
if ((ctx->enc_params.operationrate == 60 ||
((ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) >= 59 &&
(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) <= 60) ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width
== 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height
== 2160))) {
/* Set allowed time for 60fps/4K recording */
/* Use clock cycle if single instance */
if (venc_hists != 0 && venc_hists->next == 0) {
venc_cur_job->hw_kcy =
mtk_venc_get_exec_cnt(ctx);
}
if (ctx->enc_params.operationrate > 0) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
} else {
interval = (long long)(1000 * 1000 /
(int)(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom));
}
update_hist(venc_cur_job, &venc_hists,
interval);
} else {
update_hist(venc_cur_job, &venc_hists, 0);
}
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
int f_type = 1; /* TODO */
int boost_perc = 0;
int rcpu_bw = 5;
int rec_bw = 0;
int bsdma_bw = 10;
int sv_comv_bw = 5;
int rd_comv_bw = 10;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (ctx->enc_params.operationrate >= 120 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_HEIF ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width == 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height == 2160)) {
boost_perc = 100;
} else if (ctx->enc_params.operationrate == 60) {
boost_perc = 30;
}
/* Input BW scaling to venc_freq & config */
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
/* BW in venc_freq */
if (f_type == 0) {
/* I frame */
rec_bw = cur_luma_bw + cur_chroma_bw;
} else {
rec_bw = cur_luma_bw + cur_chroma_bw;
ref_luma_bw = cur_luma_bw * 4;
ref_chroma_bw = cur_chroma_bw * 4;
}
if (1) { /* UFO */
ref_chroma_bw += ref_luma_bw;
ref_luma_bw = 0;
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
#if ENC_DVFS
mutex_lock(&ctx->dev->enc_dvfs_mutex);
add_job(&ctx->id, &venc_jobs);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_begin(ctx);
mtk_venc_emi_bw_begin(ctx);
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_end(ctx);
mtk_venc_emi_bw_end(ctx);
}
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
@@ -0,0 +1,37 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,235 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
//#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
mtk_v4l2_debug(1, "id=%d", ctx->id);
ctx->irq_status = readl(dev->enc_reg_base[VENC_LT_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
addr = dev->enc_reg_base[VENC_LT_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_lt_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
@@ -0,0 +1,49 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6779"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_VLD,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_LT_SYS,
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,770 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT 33333
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* free_hist_by_handle = free the history specified by the handle
*
* Returns 0 if history is found and freed
* -1 if there is nothing to free
*/
int free_hist_by_handle(void *handle, struct codec_history **head)
{
struct codec_history *target = 0;
target = find_hist(handle, *head);
if (target == 0)
return -1;
return free_hist_(head, target);
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
long long tot_time = 0;
int i = 0;
if (hist == 0)
return 0;
/* Special SW/HW separation case for single 60FPS */
if (hist->sw_time[0] != 0) {
tot_time = 0;
for (i = 0; i < MAX_HISTORY; i++) {
/* Sum SW time*/
tot_time += hist->sw_time[i];
}
tot_time = tot_time / MAX_HISTORY;
return (hist->tot_kcy / hist->cur_cnt) +
((tot_time * 364LL) / 1000LL);
}
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (head->next == 0 &&
((hist->submit_interval < 17000LL &&
hist->submit_interval > 16000LL) ||
(hist->submit_interval < 34000LL &&
hist->submit_interval > 33000LL))) {
/* single instance 4K30 / 60fps */
deadline = now_us + hist->submit_interval;
}
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away or instance count change, restart */
if ((hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP)
|| (job->hw_kcy > 0 && hist->sw_time[prev_idx] == 0)
|| (job->hw_kcy == 0 && hist->sw_time[prev_idx] > 0)) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->sw_time, 0, sizeof(long long)*MAX_HISTORY);
if (job->hw_kcy > 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[0] =
(int)div_64(job->mhz * (job->end - job->start),
1000LL);
}
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
if (job->hw_kcy > 0) {
hist->sw_time[0] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than
* requested
*/
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st0 e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[0]);
#endif
}
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy -
hist->kcy[hist_idx] + (int)div_64(
job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
if (job->hw_kcy != 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[hist_idx] =
(int)div_64(job->mhz * (job->end - job->start), 1000LL);
}
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
if (job->hw_kcy != 0) {
hist->sw_time[hist_idx] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than requested */
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[hist_idx]);
#endif
} else {
hist->sw_time[hist_idx] = 0;
}
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,59 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long sw_time[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int hw_kcy;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
int free_hist_by_handle(void *handle, struct codec_history **head);
#endif
@@ -0,0 +1,730 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "smi_public.h"
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "mt6781/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 312
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#if DEC_EMI_BW
#include <mtk_smi.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
static struct plist_head vdec_rlist;
static struct mm_qos_request vdec_mc;
static struct mm_qos_request vdec_ufo;
static struct mm_qos_request vdec_pp;
static struct mm_qos_request vdec_pred_rd;
static struct mm_qos_request vdec_pred_wr;
static struct mm_qos_request vdec_ppwrap;
static struct mm_qos_request vdec_tile;
static struct mm_qos_request vdec_vld;
static struct mm_qos_request vdec_vld2;
static struct mm_qos_request vdec_avc_mv;
static struct mm_qos_request vdec_ufo_enc;
static struct mm_qos_request vdec_rg_ctrl_dma;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6833-vcodec-dec");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#if DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
struct mtk_vcodec_dev *dev;
unsigned long flags;
smi_bus_prepare_enable(SMI_LARB4, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
else {
dev = container_of(pm, struct mtk_vcodec_dev, pm);
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = true;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
set_swpm_vdec_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_DEC, hw_id);
if (hw_id == MTK_VDEC_CORE) {
larb_port_num = SMI_LARB4_PORT_NUM;
larb_id = 4;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid hw_id %d", hw_id);
time_check_end(MTK_FMT_DEC, hw_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_DEC, hw_id, 50);
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
unsigned long flags;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
/* avoid translation fault callback dump reg not done */
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = false;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
set_swpm_vdec_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB4, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
struct mtk_vcodec_ctx *ctx = NULL;
int misc_offset[4] = {64, 66, 67, 65};
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset, idx;
unsigned long value;
u32 fourcc;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
if (readl(vdec_gcon_addr) == 0) {
mtk_v4l2_debug(0, "VDEC not HW break since clk off. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
return;
}
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
while (!((cg_status & 0x1) && (cg_status & 0x10))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
value = readl(vdec_gcon_addr + (0 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
0 << 2, 0, value);
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
for (offset = 64; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
for (idx = 0; idx < 4; idx++) {
offset = misc_offset[idx];
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
if (timeout == 20000)
timeout = 1000000;
else if (timeout == 1000000) {
v4l2_aee_print(
"%s %p codec:0x%08x(%c%c%c%c) hw break timeout\n",
__func__, ctx, fourcc,
fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
break;
}
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
}
/* sw reset */
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_vdec_dump_addr_reg(
struct mtk_vcodec_dev *dev, int hw_id, enum mtk_dec_dump_addr_type type)
{
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
void __iomem *vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *mc_addr = dev->dec_reg_base[VDEC_MC];
void __iomem *mv_addr = dev->dec_reg_base[VDEC_MV];
unsigned long value, values[6];
int i, j, start, end;
unsigned long flags;
#define INPUT_VLD_NUM 7
const unsigned int input_vld_reg[INPUT_VLD_NUM] = {
0xB0, 0xB4, 0xB8, 0x110, 0xEC, 0xF8, 0xFC};
// RPTR, VSTART, VEND, WPTR, VBAR, VWPTR, VRPTR
#define OUTPUT_MC_NUM 2
const unsigned int output_mc_reg[OUTPUT_MC_NUM] = {
0x224, 0x228}; // PY_ADD, PC_ADD
#define REF_MC_NUM 7
const unsigned int ref_mc_base[REF_MC_NUM] = {
0x3DC, 0xB60, 0x45C, 0xBE0, 0x4DC, 0xC60, 0xD28};
// P_L0_Y, P_L0_C, B_L0_Y, B_L0_C, B_L1_Y, B_L1_C, REF
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("hw_id %d not support !!", hw_id);
return;
}
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
if (dev->dec_is_power_on[hw_id] == false) {
mtk_v4l2_err("hw %d power is off !!", hw_id);
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
return;
}
switch (type) {
case DUMP_VDEC_IN_BUF:
for (i = 0; i < INPUT_VLD_NUM; i++) {
value = readl(vld_addr + input_vld_reg[i]);
mtk_v4l2_err("[VLD] 0x%x(%d) = 0x%lx",
input_vld_reg[i], input_vld_reg[i]/4, value);
}
break;
case DUMP_VDEC_OUT_BUF:
for (i = 0; i < OUTPUT_MC_NUM; i++) {
value = readl(mc_addr + output_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_mc_reg[i], output_mc_reg[i]/4, value);
}
break;
case DUMP_VDEC_REF_BUF:
for (i = 0; i < 32; i++) {
for (j = 0; j < 6; j++) {
values[j] = readl(mc_addr +
(ref_mc_base[j] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[0] + i * 4,
ref_mc_base[0]/4 + i, values[0],
ref_mc_base[1] + i * 4,
ref_mc_base[1]/4 + i, values[1],
ref_mc_base[2] + i * 4,
ref_mc_base[2]/4 + i, values[2],
ref_mc_base[3] + i * 4,
ref_mc_base[3]/4 + i, values[3],
ref_mc_base[4] + i * 4,
ref_mc_base[4]/4 + i, values[4],
ref_mc_base[5] + i * 4,
ref_mc_base[5]/4 + i, values[5]);
}
for (i = 0; i < 4; i++)
values[i] = readl(mc_addr + i * 4);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
i * 4, i, values[0],
i * 4, i, values[1],
i * 4, i, values[2],
i * 4, i, values[3]);
for (i = 0; i < 6; i++) {
values[i] = readl(mc_addr +
(ref_mc_base[6] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[6],
ref_mc_base[6]/4 + 0, values[0],
ref_mc_base[6] + 1 * 4,
ref_mc_base[6]/4 + 1, values[1],
ref_mc_base[6] + 2 * 4,
ref_mc_base[6]/4 + 2, values[2],
ref_mc_base[6] + 3 * 4,
ref_mc_base[6]/4 + 3, values[3],
ref_mc_base[6] + 4 * 4,
ref_mc_base[6]/4 + 4, values[4],
ref_mc_base[6] + 5 * 4,
ref_mc_base[6]/4 + 5, values[5]);
break;
case DUMP_VDEC_MV_BUF:
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("not support dump MV at hw_id %d",
hw_id);
break;
}
value = readl(mv_addr + 0x20C);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
0x20C, 0x20C/4, value);
switch (fourcc) {
case V4L2_PIX_FMT_H265:
start = 0;
end = 32;
break;
case V4L2_PIX_FMT_H264:
start = 96;
end = 128;
break;
case V4L2_PIX_FMT_VP9:
start = 240;
end = 241;
break;
case V4L2_PIX_FMT_AV1:
start = 353;
end = 356;
break;
default:
start = 195;
end = 198;
}
for (i = start; i < end; i++) {
value = readl(mv_addr + i * 4);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
i * 4, i, value);
}
break;
default:
mtk_v4l2_err("unknown addr type");
}
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
#ifdef CONFIG_MTK_IOMMU_V2
enum mtk_iommu_callback_ret_t mtk_vdec_translation_fault_callback(
int port, unsigned long mva, void *data)
{
struct mtk_vcodec_dev *dev = (struct mtk_vcodec_dev *)data;
int hw_id = MTK_VDEC_CORE;
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
ctx = dev->curr_dec_ctx[hw_id];
if (ctx) {
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
mtk_v4l2_err("codec:0x%08x(%c%c%c%c) TF larb %d port %x mva 0x%lx",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF,
port >> 5, port, mva);
} else {
mtk_v4l2_err("ctx NULL codec unknown, TF larb %d port %x mva 0x%lx",
port >> 5, port, mva);
}
switch (port) {
case M4U_PORT_L4_VDEC_VLD_EXT:
case M4U_PORT_L4_VDEC_VLD2_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
break;
case M4U_PORT_L4_VDEC_PP_EXT:
case M4U_PORT_L4_VDEC_UFO_ENC_EXT:
mtk_vdec_dump_addr_reg(dev, MTK_VDEC_CORE, DUMP_VDEC_OUT_BUF);
break;
case M4U_PORT_L4_VDEC_MC_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_REF_BUF);
break;
case M4U_PORT_L4_VDEC_AVC_MV_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_MV_BUF);
break;
default:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_OUT_BUF);
}
return MTK_IOMMU_CALLBACK_HANDLED;
}
void mtk_vdec_translation_fault_callback_setting(
struct mtk_vcodec_dev *dev)
{
int i;
for (i = M4U_PORT_L4_VDEC_MC_EXT; i <= M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT; i++) {
mtk_iommu_register_fault_callback(i,
mtk_vdec_translation_fault_callback, (void *)dev);
}
}
#endif
void mtk_prepare_vdec_dvfs(void)
{
#if DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#if DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
plist_head_init(&vdec_rlist);
mm_qos_add_request(&vdec_rlist, &vdec_mc, M4U_PORT_L4_VDEC_MC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo, M4U_PORT_L4_VDEC_UFO_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pp, M4U_PORT_L4_VDEC_PP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_rd,
M4U_PORT_L4_VDEC_PRED_RD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_wr,
M4U_PORT_L4_VDEC_PRED_WR_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ppwrap,
M4U_PORT_L4_VDEC_PPWRAP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_tile, M4U_PORT_L4_VDEC_TILE_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld, M4U_PORT_L4_VDEC_VLD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld2, M4U_PORT_L4_VDEC_VLD2_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_avc_mv,
M4U_PORT_L4_VDEC_AVC_MV_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo_enc,
M4U_PORT_L4_VDEC_UFO_ENC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_rg_ctrl_dma,
M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mm_qos_remove_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
long long op_rate_to_freq = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (ctx->dec_params.operating_rate > 0 &&
(ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H264 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP9)) {
op_rate_to_freq = 312LL *
ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height *
ctx->dec_params.operating_rate /
3840LL / 2160LL / 30LL;
target_freq_64 = match_freq((int)op_rate_to_freq,
&vdec_freq_steps[0],
vdec_freq_step_size);
vdec_freq = target_freq_64;
if (vdec_cur_job != 0)
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
} else if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
if (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_MPEG4 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_MPEG2 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP8)
target_freq = (target_freq * 110) / 100;
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq;
if (vdec_freq > target_freq_64)
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job != 0 && vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
pr_debug("no vdec job, probably error handling path\n");
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long long emi_bw = 0;
long long emi_bw_input = 0;
long long emi_bw_output = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8LL * 1920 * 1080 * 2 * 10 * 3 * vdec_freq / 2;
emi_bw_input = 8LL * 3 * vdec_freq / STD_VDEC_FREQ / 2;
emi_bw_output = 1920LL * 1088 * 9 * 20 * 10 * vdec_freq /
4 / 3 / STD_VDEC_FREQ / 1024 / 1024;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
/* bits/s to MBytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
emi_bw = emi_bw - emi_bw_output - (emi_bw_input * 2);
if (emi_bw < 0)
emi_bw = 0;
mm_qos_set_request(&vdec_mc, emi_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, emi_bw_output, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, emi_bw_input * 2, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#if DEC_EMI_BW
mm_qos_set_request(&vdec_mc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo_enc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#if DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,460 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#include "mt6781/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 364
#define STD_LUMA_BW 100
#define STD_CHROMA_BW 50
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6833-vcodec-enc");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
//free_all_bw(&venc_bw);
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB7, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
set_swpm_venc_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
larb_port_num = SMI_LARB7_PORT_NUM;
larb_id = 7;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid core_id %d", core_id);
time_check_end(MTK_FMT_ENC, core_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
set_swpm_venc_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB7, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
free_hist(&venc_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_release_pmqos(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
free_hist_by_handle(&ctx->id, &venc_hists);
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu, M4U_PORT_L7_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec, M4U_PORT_L7_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma, M4U_PORT_L7_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv,
M4U_PORT_L7_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv,
M4U_PORT_L7_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma,
M4U_PORT_L7_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma,
M4U_PORT_L7_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma,
M4U_PORT_L7_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma,
M4U_PORT_L7_VENC_REF_CHROMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist);
#endif
}
void mtk_venc_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (target_freq_64 > 450)
target_freq_64 = 450;
if (target_freq > 0) {
venc_freq = target_freq;
if (venc_freq > target_freq_64)
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
#if ENC_DVFS
static int mtk_venc_get_exec_cnt(struct mtk_vcodec_ctx *ctx)
{
return (int)((readl(ctx->dev->enc_reg_base[VENC_SYS] + 0x1030) &
0x7FFFFFFF) / 1000);
}
#endif
void mtk_venc_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->enc_params.operationrate < 120) {
if (ctx->enc_params.framerate_denom == 0)
ctx->enc_params.framerate_denom = 1;
if (ctx->enc_params.operationrate == 0 &&
ctx->enc_params.framerate_num == 0)
ctx->enc_params.framerate_num =
ctx->enc_params.framerate_denom * 30;
if ((ctx->enc_params.operationrate == 60 ||
((ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) >= 59 &&
(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) <= 60) ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width
== 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height
== 2160))) {
/* Set allowed time for 60fps/4K recording */
/* Use clock cycle if single instance */
if (venc_hists != 0 && venc_hists->next == 0) {
venc_cur_job->hw_kcy =
mtk_venc_get_exec_cnt(ctx);
}
if (ctx->enc_params.operationrate > 0) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
} else {
interval = (long long)(1000 * 1000 /
(int)(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom));
}
update_hist(venc_cur_job, &venc_hists,
interval);
} else {
update_hist(venc_cur_job, &venc_hists, 0);
}
} else if (ctx->enc_params.operationrate == 120) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
int f_type = 1; /* TODO */
int boost_perc = 0;
int rcpu_bw = 5;
int rec_bw = 0;
int bsdma_bw = 10;
int sv_comv_bw = 5;
int rd_comv_bw = 10;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (ctx->enc_params.operationrate >= 60)
boost_perc = 30;
/* Input BW scaling to venc_freq & config */
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
/* BW in venc_freq */
if (f_type == 0) {
rec_bw = cur_luma_bw + cur_chroma_bw;
} else {
rec_bw = cur_luma_bw + cur_chroma_bw;
ref_luma_bw = cur_luma_bw * 4;
ref_chroma_bw = cur_chroma_bw * 4;
}
if (1) { /* UFO */
ref_chroma_bw += ref_luma_bw;
ref_luma_bw = 0;
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
if (1) { /* UFO */
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_VENC);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_VENC);
} else {
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_NONE);
}
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
#if ENC_DVFS
mutex_lock(&ctx->dev->enc_dvfs_mutex);
add_job(&ctx->id, &venc_jobs);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_begin(ctx);
mtk_venc_emi_bw_begin(ctx);
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_end(ctx);
mtk_venc_emi_bw_end(ctx);
}
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,291 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
if (ctx->dec_params.svp_mode) {
mtk_v4l2_debug(4, "svp_mode %d don't handle",
ctx->dec_params.svp_mode);
return IRQ_HANDLED;
}
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler,
IRQF_NO_THREAD | IRQF_SHARED | IRQF_PROBE_SHARED, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
struct mtk_vcodec_ctx *curr_ctx = ctx;
if (curr_ctx->type == MTK_INST_ENCODER)
mtk_vcodec_enc_timeout_dump(ctx);
else if (curr_ctx->type == MTK_INST_DECODER)
mtk_vcodec_dec_timeout_dump(ctx);
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
void mtk_vcodec_enc_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0, j = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define REG1_COUNT 13
#define REG2_COUNT 46
unsigned int Reg_1[REG1_COUNT] = {
0x14, 0xEC, 0x1C0, 0x1168, 0x11C0,
0x11C4, 0xF4, 0x5C, 0x60, 0x130,
0x24, 0x114C, 0x1164};
unsigned int Reg_2[REG2_COUNT] = {
0xEC, 0x200, 0x204, 0x208, 0x20C,
0x210, 0x214, 0x218, 0x21C,
0x220, 0x224, 0x228, 0x22C,
0x230, 0x234, 0x238, 0x23C,
0x240, 0x244, 0x248, 0x24C,
0x250, 0x254, 0x258, 0x25C,
0x260, 0x264, 0x268, 0x26C,
0x270, 0x274, 0x278, 0x27C,
0x280,
0x22C, 0x230, 0xF4, 0x1168,
0x11C0, 0x11C4, 0x1030, 0x240,
0x248, 0x250, 0x130, 0x140};
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump venc for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG1_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_1[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_1[i], value);
}
}
writel(1, dev->enc_reg_base[0] + 0xEC);
writel(0, dev->enc_reg_base[0] + 0xF4);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG2_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_2[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_2[i], value);
}
}
}
void mtk_vcodec_dec_timeout_dump(void *ctx)
{
}
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
void mtk_vcodec_enc_timeout_dump(void *ctx);
void mtk_vcodec_dec_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,66 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6833"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_UFO,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,93 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,26 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,771 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT 33333
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* free_hist_by_handle = free the history specified by the handle
*
* Returns 0 if history is found and freed
* -1 if there is nothing to free
*/
int free_hist_by_handle(void *handle, struct codec_history **head)
{
struct codec_history *target = 0;
target = find_hist(handle, *head);
if (target == 0)
return -1;
return free_hist_(head, target);
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
long long tot_time = 0;
int i = 0;
if (hist == 0)
return 0;
/* Special SW/HW separation case for single 60FPS */
if (hist->sw_time[0] != 0) {
tot_time = 0;
for (i = 0; i < MAX_HISTORY; i++) {
/* Sum SW time*/
tot_time += hist->sw_time[i];
}
tot_time = tot_time / MAX_HISTORY;
return (hist->tot_kcy / hist->cur_cnt) +
((tot_time * 364LL) / 1000LL);
}
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (head->next == 0 &&
((hist->submit_interval < 17000LL &&
hist->submit_interval > 16000LL) ||
(hist->submit_interval < 34000LL &&
hist->submit_interval > 33000LL))) {
/* single instance 4K30 / 60fps */
deadline = now_us + hist->submit_interval;
}
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away or instance count change, restart */
if ((hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP)
|| (job->hw_kcy > 0 && hist->sw_time[prev_idx] == 0)
|| (job->hw_kcy == 0 && hist->sw_time[prev_idx] > 0)) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->sw_time, 0, sizeof(long long)*MAX_HISTORY);
if (job->hw_kcy > 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[0] =
(int)div_64(job->mhz * (job->end - job->start),
1000LL);
}
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
if (job->hw_kcy > 0) {
hist->sw_time[0] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than
* requested
*/
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st0 e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[0]);
#endif
}
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
if (job->hw_kcy != 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[hist_idx] =
(int)div_64(job->mhz * (job->end - job->start), 1000LL);
}
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
if (job->hw_kcy != 0) {
hist->sw_time[hist_idx] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than requested */
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[hist_idx]);
#endif
} else {
hist->sw_time[hist_idx] = 0;
}
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,60 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long sw_time[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int hw_kcy;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
int free_hist_by_handle(void *handle, struct codec_history **head);
#endif
@@ -0,0 +1,712 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "smi_public.h"
#include "mt6785/smi_port.h"
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#ifdef DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 312
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#ifdef DEC_EMI_BW
#include <mtk_smi.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
static struct plist_head vdec_rlist;
static struct mm_qos_request vdec_mc;
static struct mm_qos_request vdec_ufo;
static struct mm_qos_request vdec_pp;
static struct mm_qos_request vdec_pred_rd;
static struct mm_qos_request vdec_pred_wr;
static struct mm_qos_request vdec_ppwrap;
static struct mm_qos_request vdec_tile;
static struct mm_qos_request vdec_vld;
static struct mm_qos_request vdec_vld2;
static struct mm_qos_request vdec_avc_mv;
static struct mm_qos_request vdec_ufo_enc;
static struct mm_qos_request vdec_rg_ctrl_dma;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,vdec_gcon");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_SCP_SYS_VDE =
devm_clk_get(&pdev->dev, "MT_SCP_SYS_VDE");
if (IS_ERR(pm->clk_MT_SCP_SYS_VDE)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_VDE\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_VDE);
}
pm->clk_MT_SCP_SYS_VEN =
devm_clk_get(&pdev->dev, "MT_SCP_SYS_VEN");
if (IS_ERR(pm->clk_MT_SCP_SYS_VEN)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_VEN\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_VEN);
}
pm->clk_MT_SCP_SYS_DIS =
devm_clk_get(&pdev->dev, "MT_SCP_SYS_DIS");
if (IS_ERR(pm->clk_MT_SCP_SYS_DIS)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_DIS\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_DIS);
}
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#ifdef DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
struct mtk_vcodec_dev *dev;
unsigned long flags;
smi_bus_prepare_enable(SMI_LARB2, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
else {
dev = container_of(pm, struct mtk_vcodec_dev, pm);
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = true;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
unsigned long flags;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
/* avoid translation fault callback dump reg not done */
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = false;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB2, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
struct mtk_vcodec_ctx *ctx = NULL;
int misc_offset[4] = {64, 66, 67, 65};
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset, idx;
unsigned long value;
u32 fourcc;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
if (readl(vdec_gcon_addr) == 0) {
mtk_v4l2_debug(0, "VDEC not HW break since clk off. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
return;
}
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
while (!((cg_status & 0x1) && (cg_status & 0x10))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
value = readl(vdec_gcon_addr + (0 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
0 << 2, 0, value);
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
for (offset = 64; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
for (idx = 0; idx < 4; idx++) {
offset = misc_offset[idx];
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
if (timeout == 20000)
timeout = 1000000;
else if (timeout == 1000000) {
/* v4l2_aee_print(
* "%s %p codec:0x%08x(%c%c%c%c) hw break timeout\n",
* __func__, ctx, fourcc,
* fourcc & 0xFF, (fourcc >> 8) & 0xFF,
* (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
*/
break;
}
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
}
/* sw reset */
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_vdec_dump_addr_reg(
struct mtk_vcodec_dev *dev, int hw_id, enum mtk_dec_dump_addr_type type)
{
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
void __iomem *vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *mc_addr = dev->dec_reg_base[VDEC_MC];
void __iomem *mv_addr = dev->dec_reg_base[VDEC_MV];
void __iomem *ufo_addr = dev->dec_reg_base[VDEC_SYS] + 0x800;
unsigned long value, values[6];
bool is_ufo = false;
int i, j, start, end;
unsigned long flags;
#define INPUT_VLD_NUM 7
const unsigned int input_vld_reg[INPUT_VLD_NUM] = {
0xB0, 0xB4, 0xB8, 0x110, 0xEC, 0xF8, 0xFC};
// RPTR, VSTART, VEND, WPTR, VBAR, VWPTR, VRPTR
#define OUTPUT_MC_NUM 2
const unsigned int output_mc_reg[OUTPUT_MC_NUM] = {
0x224, 0x228}; // PY_ADD, PC_ADD
#define OUTPUT_UFO_MC_NUM 5
const unsigned int output_ufo_mc_reg[OUTPUT_UFO_MC_NUM] = {
0xB5C, 0xAE8, 0xAEC, 0xCE4, 0xCE8};
// YC_SEP, LEN_Y, LEN_C, LEN_Y_OFFSET, LEN_C_OFFSET
#define OUTPUT_UFO_NUM 4
const unsigned int output_ufo_reg[OUTPUT_UFO_NUM] = {
0x7C, 0x80, 0x84, 0x88}; // LEN_Y, LEN_C, BS_Y, BS_C
#define REF_MC_NUM 7
const unsigned int ref_mc_base[REF_MC_NUM] = {
0x3DC, 0xB60, 0x45C, 0xBE0, 0x4DC, 0xC60, 0xD28};
// P_L0_Y, P_L0_C, B_L0_Y, B_L0_C, B_L1_Y, B_L1_C, REF
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("hw_id %d not support !!", hw_id);
return;
}
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
if (dev->dec_is_power_on[hw_id] == false) {
mtk_v4l2_err("hw %d power is off !!", hw_id);
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
return;
}
if (hw_id == MTK_VDEC_CORE && fourcc != V4L2_PIX_FMT_AV1)
is_ufo = (readl(ufo_addr + 0x08C) & 0x1) == 0x1;
switch (type) {
case DUMP_VDEC_IN_BUF:
for (i = 0; i < INPUT_VLD_NUM; i++) {
value = readl(vld_addr + input_vld_reg[i]);
mtk_v4l2_err("[VLD] 0x%x(%d) = 0x%lx",
input_vld_reg[i], input_vld_reg[i]/4, value);
}
break;
case DUMP_VDEC_OUT_BUF:
for (i = 0; i < OUTPUT_MC_NUM; i++) {
value = readl(mc_addr + output_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_mc_reg[i], output_mc_reg[i]/4, value);
}
if (is_ufo) {
for (i = 0; i < OUTPUT_UFO_MC_NUM; i++) {
value = readl(mc_addr + output_ufo_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_ufo_mc_reg[i],
output_ufo_mc_reg[i]/4, value);
}
for (i = 0; i < OUTPUT_UFO_NUM; i++) {
value = readl(ufo_addr + output_ufo_reg[i]);
mtk_v4l2_err("[UFO] 0x%x(%d) = 0x%lx",
output_ufo_reg[i],
output_ufo_reg[i]/4, value);
}
}
break;
case DUMP_VDEC_REF_BUF:
for (i = 0; i < 32; i++) {
for (j = 0; j < 6; j++) {
values[j] = readl(mc_addr +
(ref_mc_base[j] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[0] + i * 4,
ref_mc_base[0]/4 + i, values[0],
ref_mc_base[1] + i * 4,
ref_mc_base[1]/4 + i, values[1],
ref_mc_base[2] + i * 4,
ref_mc_base[2]/4 + i, values[2],
ref_mc_base[3] + i * 4,
ref_mc_base[3]/4 + i, values[3],
ref_mc_base[4] + i * 4,
ref_mc_base[4]/4 + i, values[4],
ref_mc_base[5] + i * 4,
ref_mc_base[5]/4 + i, values[5]);
}
for (i = 0; i < 4; i++)
values[i] = readl(mc_addr + i * 4);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
i * 4, i, values[0],
i * 4, i, values[1],
i * 4, i, values[2],
i * 4, i, values[3]);
for (i = 0; i < 6; i++) {
values[i] = readl(mc_addr +
(ref_mc_base[6] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[6],
ref_mc_base[6]/4 + 0, values[0],
ref_mc_base[6] + 1 * 4,
ref_mc_base[6]/4 + 1, values[1],
ref_mc_base[6] + 2 * 4,
ref_mc_base[6]/4 + 2, values[2],
ref_mc_base[6] + 3 * 4,
ref_mc_base[6]/4 + 3, values[3],
ref_mc_base[6] + 4 * 4,
ref_mc_base[6]/4 + 4, values[4],
ref_mc_base[6] + 5 * 4,
ref_mc_base[6]/4 + 5, values[5]);
break;
case DUMP_VDEC_MV_BUF:
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("not support dump MV at hw_id %d",
hw_id);
break;
}
value = readl(mv_addr + 0x20C);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
0x20C, 0x20C/4, value);
switch (fourcc) {
case V4L2_PIX_FMT_H265:
start = 0;
end = 32;
break;
case V4L2_PIX_FMT_H264:
start = 96;
end = 128;
break;
case V4L2_PIX_FMT_VP9:
start = 240;
end = 241;
break;
case V4L2_PIX_FMT_AV1:
start = 353;
end = 356;
break;
default:
start = 195;
end = 198;
}
for (i = start; i < end; i++) {
value = readl(mv_addr + i * 4);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
i * 4, i, value);
}
break;
default:
mtk_v4l2_err("unknown addr type");
}
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
#ifdef CONFIG_MTK_IOMMU_V2
enum mtk_iommu_callback_ret_t mtk_vdec_translation_fault_callback(
int port, unsigned long mva, void *data)
{
struct mtk_vcodec_dev *dev = (struct mtk_vcodec_dev *)data;
int hw_id = MTK_VDEC_CORE;
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
ctx = dev->curr_dec_ctx[hw_id];
if (ctx) {
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
mtk_v4l2_err("codec:0x%08x(%c%c%c%c) TF larb %d port %x mva 0x%lx",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF,
port >> 5, port, mva);
} else {
mtk_v4l2_err("ctx NULL codec unknown, TF larb %d port %x mva 0x%lx",
port >> 5, port, mva);
}
switch (port) {
case M4U_PORT_HW_VDEC_VLD_EXT:
case M4U_PORT_HW_VDEC_VLD2_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
break;
case M4U_PORT_HW_VDEC_PP_EXT:
case M4U_PORT_HW_VDEC_UFO_ENC_EXT:
mtk_vdec_dump_addr_reg(dev, MTK_VDEC_CORE, DUMP_VDEC_OUT_BUF);
break;
case M4U_PORT_HW_VDEC_MC_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_REF_BUF);
break;
case M4U_PORT_HW_VDEC_AVC_MV_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_MV_BUF);
break;
default:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_OUT_BUF);
}
return MTK_IOMMU_CALLBACK_HANDLED;
}
void mtk_vdec_translation_fault_callback_setting(
struct mtk_vcodec_dev *dev)
{
int i;
for (i = M4U_PORT_HW_VDEC_MC_EXT; i <= M4U_PORT_HW_VDEC_RG_CTRL_DMA_EXT; i++) {
mtk_iommu_register_fault_callback(i,
mtk_vdec_translation_fault_callback, (void *)dev);
}
}
#endif
void mtk_prepare_vdec_dvfs(void)
{
#ifdef DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#ifdef DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#ifdef DEC_EMI_BW
plist_head_init(&vdec_rlist);
mm_qos_add_request(&vdec_rlist, &vdec_mc, SMI_HW_VDEC_MC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo, SMI_HW_VDEC_UFO_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pp, SMI_HW_VDEC_PP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_rd, SMI_HW_VDEC_PRED_RD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_wr, SMI_HW_VDEC_PRED_WR_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ppwrap, SMI_HW_VDEC_PPWRAP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_tile, SMI_HW_VDEC_TILE_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld, SMI_HW_VDEC_VLD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld2, SMI_HW_VDEC_VLD2_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_avc_mv, SMI_HW_VDEC_AVC_MV_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo_enc, SMI_HW_VDEC_UFO_ENC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_rg_ctrl_dma,
SMI_HW_VDEC_RG_CTRL_DMA_EXT);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#ifdef DEC_EMI_BW
mm_qos_remove_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#ifdef DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq;
if (vdec_freq > target_freq_64)
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#ifdef DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#ifdef DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long emi_bw = 0;
long emi_bw_input = 0;
long emi_bw_output = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8L * 1920 * 1080 * 3 * 10 * vdec_freq;
emi_bw_input = 8 * vdec_freq / STD_VDEC_FREQ;
emi_bw_output = 1920 * 1088 * 3 * 30 * 10 * vdec_freq /
2 / 3 / STD_VDEC_FREQ / 1024 / 1024;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
/* bits/s to MBytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
if (0) { /* UFO */
emi_bw = emi_bw * 6 / 10;
emi_bw_output = emi_bw_output * 6 / 10;
}
emi_bw = emi_bw - emi_bw_output - (emi_bw_input * 2);
if (emi_bw < 0)
emi_bw = 0;
if (0) { /* UFO */
mm_qos_set_request(&vdec_ufo, emi_bw, 0, BW_COMP_DEFAULT);
mm_qos_set_request(&vdec_ufo_enc, emi_bw_output, 0,
BW_COMP_DEFAULT);
} else {
mm_qos_set_request(&vdec_mc, emi_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, emi_bw_output, 0, BW_COMP_NONE);
}
mm_qos_set_request(&vdec_pred_rd, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, emi_bw_input * 2, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#ifdef DEC_EMI_BW
mm_qos_set_request(&vdec_mc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo_enc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#ifdef DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,35 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,432 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#include "mt6785/smi_port.h"
#ifdef ENC_DVFS
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <linux/pm_qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 364
#define STD_LUMA_BW 100
#define STD_CHROMA_BW 50
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
#endif
#ifdef ENC_EMI_BW
#include <mtk_smi.h>
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,venc_gcon");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_SCP_SYS_VDE = devm_clk_get(&pdev->dev, "MT_SCP_SYS_VDE");
if (IS_ERR(pm->clk_MT_SCP_SYS_VDE)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_VDE\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_VDE);
}
pm->clk_MT_SCP_SYS_VEN = devm_clk_get(&pdev->dev, "MT_SCP_SYS_VEN");
if (IS_ERR(pm->clk_MT_SCP_SYS_VEN)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_VEN\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_VEN);
}
pm->clk_MT_SCP_SYS_DIS = devm_clk_get(&pdev->dev, "MT_SCP_SYS_DIS");
if (IS_ERR(pm->clk_MT_SCP_SYS_DIS)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_SCP_SYS_DIS\n");
return PTR_ERR(pm->clk_MT_SCP_SYS_DIS);
}
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
#if ENC_EMI_BW
#endif
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
#ifdef ENC_DVFS
free_hist_by_handle(&ctx->id, &venc_hists);
#endif
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB3, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB3, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#ifdef ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#ifdef ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
free_hist(&venc_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_release_pmqos(struct mtk_vcodec_ctx *ctx)
{
#ifdef ENC_DVFS
free_hist_by_handle(&ctx->id, &venc_hists);
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#ifdef ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu, SMI_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec, SMI_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma, SMI_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv, SMI_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv, SMI_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma, SMI_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma, SMI_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma, SMI_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma, SMI_VENC_REF_CHROMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#ifdef ENC_EMI_BW
#endif
}
void mtk_venc_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#ifdef ENC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (ctx->dev->enc_cnt > 1) {
/* Reduce available time / increase freq */
target_freq = target_freq * 2;
if (ctx->enc_params.svp_mode > 0)
target_freq = 630;
}
if (ctx->enc_params.operationrate >= 120 &&
(target_freq_64 > 450 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc ==
V4L2_PIX_FMT_H264))
target_freq_64 = 450;
if (target_freq > 0) {
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
static int mtk_venc_get_exec_cnt(struct mtk_vcodec_ctx *ctx)
{
return (int)((readl(ctx->dev->enc_reg_base[VENC_SYS] + 0x17C) &
0x7FFFFFFF) / 1000);
}
void mtk_venc_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#ifdef ENC_DVFS
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->enc_params.operationrate < 120) {
if (ctx->enc_params.framerate_denom == 0)
ctx->enc_params.framerate_denom = 1;
if (ctx->enc_params.operationrate == 0 &&
ctx->enc_params.framerate_num == 0)
ctx->enc_params.framerate_num =
ctx->enc_params.framerate_denom * 30;
if ((ctx->enc_params.operationrate == 60 ||
((ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) >= 59 &&
(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) <= 60) ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width
== 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height
== 2160))) {
/* Set allowed time for 60fps/4K recording */
/* Use clock cycle if single instance */
if (venc_hists != 0 && venc_hists->next == 0) {
venc_cur_job->hw_kcy =
mtk_venc_get_exec_cnt(ctx);
}
if (ctx->enc_params.operationrate > 0) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
} else {
interval = (long long)(1000 * 1000 /
(int)(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom));
}
update_hist(venc_cur_job, &venc_hists,
interval);
} else {
update_hist(venc_cur_job, &venc_hists, 0);
}
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#ifdef ENC_EMI_BW
int f_type = 1; /* TODO */
int boost_perc = 0;
int rcpu_bw = 5;
int rec_bw = 0;
int bsdma_bw = 10;
int sv_comv_bw = 5;
int rd_comv_bw = 10;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (ctx->enc_params.operationrate >= 120 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_HEIF ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width == 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height == 2160)) {
boost_perc = 100;
} else if (ctx->enc_params.operationrate == 60) {
boost_perc = 30;
}
/* Input BW scaling to venc_freq & config */
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
/* BW in venc_freq */
if (f_type == 0) {
/* I frame */
rec_bw = cur_luma_bw + cur_chroma_bw;
} else {
rec_bw = cur_luma_bw + cur_chroma_bw;
ref_luma_bw = cur_luma_bw * 4;
ref_chroma_bw = cur_chroma_bw * 4;
}
if (1) { /* UFO */
ref_chroma_bw += ref_luma_bw;
ref_luma_bw = 0;
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifdef ENC_DVFS
mutex_lock(&ctx->dev->enc_dvfs_mutex);
add_job(&ctx->id, &venc_jobs);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_begin(ctx);
mtk_venc_emi_bw_begin(ctx);
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_end(ctx);
mtk_venc_emi_bw_end(ctx);
}
/* Total job count after this one is inserted */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
/* Remaining job count after this one is done */
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,241 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
ctx->irq_status = readl(dev->enc_reg_base[VENC_LT_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
addr = dev->enc_reg_base[VENC_LT_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_lt_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
@@ -0,0 +1,49 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,65 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6785"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_LT_SYS,
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,770 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT 33333
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* free_hist_by_handle = free the history specified by the handle
*
* Returns 0 if history is found and freed
* -1 if there is nothing to free
*/
int free_hist_by_handle(void *handle, struct codec_history **head)
{
struct codec_history *target = 0;
target = find_hist(handle, *head);
if (target == 0)
return -1;
return free_hist_(head, target);
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
long long tot_time = 0;
int i = 0;
if (hist == 0)
return 0;
/* Special SW/HW separation case for single 60FPS */
if (hist->sw_time[0] != 0) {
tot_time = 0;
for (i = 0; i < MAX_HISTORY; i++) {
/* Sum SW time*/
tot_time += hist->sw_time[i];
}
tot_time = tot_time / MAX_HISTORY;
return (hist->tot_kcy / hist->cur_cnt) +
((tot_time * 364LL) / 1000LL);
}
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (head->next == 0 &&
((hist->submit_interval < 17000LL &&
hist->submit_interval > 16000LL) ||
(hist->submit_interval < 34000LL &&
hist->submit_interval > 33000LL))) {
/* single instance 4K30 / 60fps */
deadline = now_us + hist->submit_interval;
}
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away or instance count change, restart */
if ((hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP)
|| (job->hw_kcy > 0 && hist->sw_time[prev_idx] == 0)
|| (job->hw_kcy == 0 && hist->sw_time[prev_idx] > 0)) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->sw_time, 0, sizeof(long long)*MAX_HISTORY);
if (job->hw_kcy > 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[0] =
(int)div_64(job->mhz * (job->end - job->start),
1000LL);
}
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
if (job->hw_kcy > 0) {
hist->sw_time[0] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than
* requested
*/
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st0 e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[0]);
#endif
}
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
if (job->hw_kcy != 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[hist_idx] =
(int)div_64(job->mhz * (job->end - job->start), 1000LL);
}
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
if (job->hw_kcy != 0) {
hist->sw_time[hist_idx] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than requested */
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[hist_idx]);
#endif
} else {
hist->sw_time[hist_idx] = 0;
}
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,59 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long sw_time[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int hw_kcy;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
int free_hist_by_handle(void *handle, struct codec_history **head);
#endif
@@ -0,0 +1,762 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "smi_public.h"
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "mt6833/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 218
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#if DEC_EMI_BW
#include <mtk_smi.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
static struct plist_head vdec_rlist;
static struct mm_qos_request vdec_mc;
static struct mm_qos_request vdec_ufo;
static struct mm_qos_request vdec_pp;
static struct mm_qos_request vdec_pred_rd;
static struct mm_qos_request vdec_pred_wr;
static struct mm_qos_request vdec_ppwrap;
static struct mm_qos_request vdec_tile;
static struct mm_qos_request vdec_vld;
static struct mm_qos_request vdec_vld2;
static struct mm_qos_request vdec_avc_mv;
static struct mm_qos_request vdec_ufo_enc;
static struct mm_qos_request vdec_rg_ctrl_dma;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6833-vcodec-dec");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#if DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
struct mtk_vcodec_dev *dev;
unsigned long flags;
smi_bus_prepare_enable(SMI_LARB4, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
else {
dev = container_of(pm, struct mtk_vcodec_dev, pm);
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = true;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
set_swpm_vdec_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_DEC, hw_id);
if (hw_id == MTK_VDEC_CORE) {
larb_port_num = SMI_LARB4_PORT_NUM;
larb_id = 4;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid hw_id %d", hw_id);
time_check_end(MTK_FMT_DEC, hw_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_DEC, hw_id, 50);
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
unsigned long flags;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
/* avoid translation fault callback dump reg not done */
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = false;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
set_swpm_vdec_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB4, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
struct mtk_vcodec_ctx *ctx = NULL;
int misc_offset[4] = {64, 66, 67, 65};
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset, idx;
unsigned long value;
u32 fourcc;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
if (readl(vdec_gcon_addr) == 0) {
mtk_v4l2_debug(0, "VDEC not HW break since clk off. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
return;
}
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
while (!((cg_status & 0x1) && (cg_status & 0x10))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
value = readl(vdec_gcon_addr + (0 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
0 << 2, 0, value);
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
for (offset = 64; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
for (idx = 0; idx < 4; idx++) {
offset = misc_offset[idx];
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
if (timeout == 20000)
timeout = 1000000;
else if (timeout == 1000000) {
v4l2_aee_print(
"%s %p codec:0x%08x(%c%c%c%c) hw break timeout\n",
__func__, ctx, fourcc,
fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
break;
}
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
}
/* sw reset */
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_vdec_dump_addr_reg(
struct mtk_vcodec_dev *dev, int hw_id, enum mtk_dec_dump_addr_type type)
{
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
void __iomem *vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *mc_addr = dev->dec_reg_base[VDEC_MC];
void __iomem *mv_addr = dev->dec_reg_base[VDEC_MV];
unsigned long value, values[6];
int i, j, start, end;
unsigned long flags;
#define INPUT_VLD_NUM 7
const unsigned int input_vld_reg[INPUT_VLD_NUM] = {
0xB0, 0xB4, 0xB8, 0x110, 0xEC, 0xF8, 0xFC};
// RPTR, VSTART, VEND, WPTR, VBAR, VWPTR, VRPTR
#define OUTPUT_MC_NUM 2
const unsigned int output_mc_reg[OUTPUT_MC_NUM] = {
0x224, 0x228}; // PY_ADD, PC_ADD
#define REF_MC_NUM 7
const unsigned int ref_mc_base[REF_MC_NUM] = {
0x3DC, 0xB60, 0x45C, 0xBE0, 0x4DC, 0xC60, 0xD28};
// P_L0_Y, P_L0_C, B_L0_Y, B_L0_C, B_L1_Y, B_L1_C, REF
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("hw_id %d not support !!", hw_id);
return;
}
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
if (dev->dec_is_power_on[hw_id] == false) {
mtk_v4l2_err("hw %d power is off !!", hw_id);
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
return;
}
switch (type) {
case DUMP_VDEC_IN_BUF:
for (i = 0; i < INPUT_VLD_NUM; i++) {
value = readl(vld_addr + input_vld_reg[i]);
mtk_v4l2_err("[VLD] 0x%x(%d) = 0x%lx",
input_vld_reg[i], input_vld_reg[i]/4, value);
}
break;
case DUMP_VDEC_OUT_BUF:
for (i = 0; i < OUTPUT_MC_NUM; i++) {
value = readl(mc_addr + output_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_mc_reg[i], output_mc_reg[i]/4, value);
}
break;
case DUMP_VDEC_REF_BUF:
for (i = 0; i < 32; i++) {
for (j = 0; j < 6; j++) {
values[j] = readl(mc_addr +
(ref_mc_base[j] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[0] + i * 4,
ref_mc_base[0]/4 + i, values[0],
ref_mc_base[1] + i * 4,
ref_mc_base[1]/4 + i, values[1],
ref_mc_base[2] + i * 4,
ref_mc_base[2]/4 + i, values[2],
ref_mc_base[3] + i * 4,
ref_mc_base[3]/4 + i, values[3],
ref_mc_base[4] + i * 4,
ref_mc_base[4]/4 + i, values[4],
ref_mc_base[5] + i * 4,
ref_mc_base[5]/4 + i, values[5]);
}
for (i = 0; i < 4; i++)
values[i] = readl(mc_addr + i * 4);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
i * 4, i, values[0],
i * 4, i, values[1],
i * 4, i, values[2],
i * 4, i, values[3]);
for (i = 0; i < 6; i++) {
values[i] = readl(mc_addr +
(ref_mc_base[6] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[6],
ref_mc_base[6]/4 + 0, values[0],
ref_mc_base[6] + 1 * 4,
ref_mc_base[6]/4 + 1, values[1],
ref_mc_base[6] + 2 * 4,
ref_mc_base[6]/4 + 2, values[2],
ref_mc_base[6] + 3 * 4,
ref_mc_base[6]/4 + 3, values[3],
ref_mc_base[6] + 4 * 4,
ref_mc_base[6]/4 + 4, values[4],
ref_mc_base[6] + 5 * 4,
ref_mc_base[6]/4 + 5, values[5]);
break;
case DUMP_VDEC_MV_BUF:
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("not support dump MV at hw_id %d",
hw_id);
break;
}
value = readl(mv_addr + 0x20C);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
0x20C, 0x20C/4, value);
switch (fourcc) {
case V4L2_PIX_FMT_H265:
start = 0;
end = 32;
break;
case V4L2_PIX_FMT_H264:
start = 96;
end = 128;
break;
case V4L2_PIX_FMT_VP9:
start = 240;
end = 241;
break;
case V4L2_PIX_FMT_AV1:
start = 353;
end = 356;
break;
default:
start = 195;
end = 198;
}
for (i = start; i < end; i++) {
value = readl(mv_addr + i * 4);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
i * 4, i, value);
}
break;
default:
mtk_v4l2_err("unknown addr type");
}
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
#ifdef CONFIG_MTK_IOMMU_V2
enum mtk_iommu_callback_ret_t mtk_vdec_translation_fault_callback(
int port, unsigned long mva, void *data)
{
struct mtk_vcodec_dev *dev = (struct mtk_vcodec_dev *)data;
int hw_id = MTK_VDEC_CORE;
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
ctx = dev->curr_dec_ctx[hw_id];
if (ctx) {
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
mtk_v4l2_err("codec:0x%08x(%c%c%c%c) TF larb %d port %x mva 0x%lx",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF,
port >> 5, port, mva);
} else {
mtk_v4l2_err("ctx NULL codec unknown, TF larb %d port %x mva 0x%lx",
port >> 5, port, mva);
}
switch (port) {
case M4U_PORT_L4_VDEC_VLD_EXT:
case M4U_PORT_L4_VDEC_VLD2_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
break;
case M4U_PORT_L4_VDEC_PP_EXT:
case M4U_PORT_L4_VDEC_UFO_ENC_EXT:
mtk_vdec_dump_addr_reg(dev, MTK_VDEC_CORE, DUMP_VDEC_OUT_BUF);
break;
case M4U_PORT_L4_VDEC_MC_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_REF_BUF);
break;
case M4U_PORT_L4_VDEC_AVC_MV_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_MV_BUF);
break;
default:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_OUT_BUF);
}
return MTK_IOMMU_CALLBACK_HANDLED;
}
void mtk_vdec_translation_fault_callback_setting(
struct mtk_vcodec_dev *dev)
{
int i;
for (i = M4U_PORT_L4_VDEC_MC_EXT; i <= M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT; i++) {
mtk_iommu_register_fault_callback(i,
mtk_vdec_translation_fault_callback, (void *)dev);
}
}
#endif
void mtk_prepare_vdec_dvfs(void)
{
#if DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#if DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
plist_head_init(&vdec_rlist);
mm_qos_add_request(&vdec_rlist, &vdec_mc, M4U_PORT_L4_VDEC_MC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo, M4U_PORT_L4_VDEC_UFO_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pp, M4U_PORT_L4_VDEC_PP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_rd,
M4U_PORT_L4_VDEC_PRED_RD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_wr,
M4U_PORT_L4_VDEC_PRED_WR_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ppwrap,
M4U_PORT_L4_VDEC_PPWRAP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_tile, M4U_PORT_L4_VDEC_TILE_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld, M4U_PORT_L4_VDEC_VLD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld2, M4U_PORT_L4_VDEC_VLD2_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_avc_mv,
M4U_PORT_L4_VDEC_AVC_MV_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo_enc,
M4U_PORT_L4_VDEC_UFO_ENC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_rg_ctrl_dma,
M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mm_qos_remove_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
long long op_rate_to_freq = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (ctx->dec_params.operating_rate > 0 &&
(ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H264 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP9)) {
#if BITS_PER_LONG == 32
op_rate_to_freq = 312LL *
ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height *
div_u64(ctx->dec_params.operating_rate,
3840LL * 2160LL * 30LL);
#else
op_rate_to_freq = 312LL *
ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height *
ctx->dec_params.operating_rate /
3840LL / 2160LL / 30LL;
#endif
target_freq_64 = match_freq((int)op_rate_to_freq,
&vdec_freq_steps[0],
vdec_freq_step_size);
vdec_freq = target_freq_64;
if (vdec_cur_job != 0)
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
} else if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
if (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_MPEG4 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_MPEG2 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP8)
target_freq = (target_freq * 110) / 100;
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq;
if (vdec_freq > target_freq_64)
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job != 0 && vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
pr_debug("no vdec job, probably error handling path\n");
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long emi_bw = 0;
long emi_bw_input = 0;
long emi_bw_output = 0;
int is_ufo_on = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8L * 1920 * 1080 * 3 * 10 * vdec_freq;
#if BITS_PER_LONG == 32
emi_bw_input = div_u64(8 * vdec_freq, STD_VDEC_FREQ);
emi_bw_output = div_u64((1920 * 1088 * 3 * 20 * 10 * vdec_freq),
(2 * 3 * STD_VDEC_FREQ * 1024 * 1024));
#else
emi_bw_input = 8L * vdec_freq / STD_VDEC_FREQ;
emi_bw_output = 1920L * 1088 * 3 * 30 * 10 * vdec_freq /
2 / 3 / STD_VDEC_FREQ / 1024 / 1024;
#endif
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
is_ufo_on = (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H264 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP9) &&
((ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height) >=
(1920 * 1080)) ? 1 : 0;
/* bits/s to MBytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
if (is_ufo_on == 1) { /* UFO */
emi_bw = emi_bw * 6 / 10;
emi_bw_output = emi_bw_output * 6 / 10;
}
emi_bw = emi_bw - emi_bw_output - (emi_bw_input * 2);
if (emi_bw < 0)
emi_bw = 0;
if (is_ufo_on == 1) { /* UFO */
mm_qos_set_request(&vdec_ufo, emi_bw, 0, BW_COMP_DEFAULT);
mm_qos_set_request(&vdec_ufo_enc, emi_bw_output, 0,
BW_COMP_DEFAULT);
} else {
mm_qos_set_request(&vdec_pp, emi_bw_output, 0, BW_COMP_NONE);
}
mm_qos_set_request(&vdec_mc, emi_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, emi_bw_input * 2, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#if DEC_EMI_BW
mm_qos_set_request(&vdec_mc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo_enc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#if DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,35 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,465 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#include "mt6833/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 249
#define STD_LUMA_BW 70
#define STD_CHROMA_BW 35
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6833-vcodec-enc");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
//free_all_bw(&venc_bw);
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB7, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
set_swpm_venc_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
larb_port_num = SMI_LARB7_PORT_NUM;
larb_id = 7;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid core_id %d", core_id);
time_check_end(MTK_FMT_ENC, core_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
set_swpm_venc_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB7, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
free_hist(&venc_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_release_pmqos(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
free_hist_by_handle(&ctx->id, &venc_hists);
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu, M4U_PORT_L7_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec, M4U_PORT_L7_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma, M4U_PORT_L7_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv,
M4U_PORT_L7_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv,
M4U_PORT_L7_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma,
M4U_PORT_L7_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma,
M4U_PORT_L7_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma,
M4U_PORT_L7_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma,
M4U_PORT_L7_VENC_REF_CHROMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist);
#endif
}
void mtk_venc_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (target_freq_64 > 458)
target_freq_64 = 458;
if (target_freq > 0) {
venc_freq = target_freq;
if (venc_freq > target_freq_64)
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
#if ENC_DVFS
static int mtk_venc_get_exec_cnt(struct mtk_vcodec_ctx *ctx)
{
return (int)((readl(ctx->dev->enc_reg_base[VENC_SYS] + 0x17C) &
0x7FFFFFFF) / 1000);
}
#endif
void mtk_venc_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->enc_params.operationrate < 120) {
if (ctx->enc_params.framerate_denom == 0)
ctx->enc_params.framerate_denom = 1;
if (ctx->enc_params.operationrate == 0 &&
ctx->enc_params.framerate_num == 0)
ctx->enc_params.framerate_num =
ctx->enc_params.framerate_denom * 30;
if ((ctx->enc_params.operationrate == 60 ||
((ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) >= 59 &&
(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) <= 60) ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width
== 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height
== 2160))) {
/* Set allowed time for 60fps/4K recording */
/* Use clock cycle if single instance */
if (venc_hists != 0 && venc_hists->next == 0) {
venc_cur_job->hw_kcy =
mtk_venc_get_exec_cnt(ctx);
}
if (ctx->enc_params.operationrate > 0) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
} else {
interval = (long long)(1000 * 1000 /
(int)(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom));
}
update_hist(venc_cur_job, &venc_hists,
interval);
} else {
update_hist(venc_cur_job, &venc_hists, 0);
}
} else if (ctx->enc_params.operationrate == 120) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
int f_type = 1; /* TODO */
int boost_perc = 0;
int rcpu_bw = 5;
int rec_bw = 0;
int bsdma_bw = 10;
int sv_comv_bw = 5;
int rd_comv_bw = 10;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (ctx->enc_params.operationrate >= 60)
boost_perc = 30;
/* Input BW scaling to venc_freq & config */
#if BITS_PER_LONG == 32
cur_luma_bw = div_u64(STD_LUMA_BW * venc_freq * (100 + boost_perc),
(STD_VENC_FREQ * 100));
cur_chroma_bw = div_u64(STD_CHROMA_BW * venc_freq * (100 + boost_perc),
(STD_VENC_FREQ * 100));
#else
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
#endif
/* BW in venc_freq */
if (f_type == 0) {
rec_bw = cur_luma_bw + cur_chroma_bw;
} else {
rec_bw = cur_luma_bw + cur_chroma_bw;
ref_luma_bw = cur_luma_bw * 4;
ref_chroma_bw = cur_chroma_bw * 4;
}
if (1) { /* UFO */
ref_chroma_bw += ref_luma_bw;
ref_luma_bw = 0;
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
if (1) { /* UFO */
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_VENC);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_VENC);
} else {
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_NONE);
}
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
#if ENC_DVFS
mutex_lock(&ctx->dev->enc_dvfs_mutex);
add_job(&ctx->id, &venc_jobs);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_begin(ctx);
mtk_venc_emi_bw_begin(ctx);
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_end(ctx);
mtk_venc_emi_bw_end(ctx);
}
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
@@ -0,0 +1,37 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,290 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
if (ctx->dec_params.svp_mode) {
mtk_v4l2_debug(4, "svp_mode %d don't handle",
ctx->dec_params.svp_mode);
return IRQ_HANDLED;
}
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler,
IRQF_NO_THREAD | IRQF_SHARED | IRQF_PROBE_SHARED, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
struct mtk_vcodec_ctx *curr_ctx = ctx;
if (curr_ctx->type == MTK_INST_ENCODER)
mtk_vcodec_enc_timeout_dump(ctx);
else if (curr_ctx->type == MTK_INST_DECODER)
mtk_vcodec_dec_timeout_dump(ctx);
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
void mtk_vcodec_enc_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0, j = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define REG1_COUNT 13
#define REG2_COUNT 46
unsigned int Reg_1[REG1_COUNT] = {
0x14, 0xEC, 0x1C0, 0x1168, 0x11C0,
0x11C4, 0xF4, 0x5C, 0x60, 0x130,
0x24, 0x114C, 0x1164};
unsigned int Reg_2[REG2_COUNT] = {
0xEC, 0x200, 0x204, 0x208, 0x20C,
0x210, 0x214, 0x218, 0x21C,
0x220, 0x224, 0x228, 0x22C,
0x230, 0x234, 0x238, 0x23C,
0x240, 0x244, 0x248, 0x24C,
0x250, 0x254, 0x258, 0x25C,
0x260, 0x264, 0x268, 0x26C,
0x270, 0x274, 0x278, 0x27C,
0x280,
0x22C, 0x230, 0xF4, 0x1168,
0x11C0, 0x11C4, 0x1030, 0x240,
0x248, 0x250, 0x130, 0x140};
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump venc for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG1_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_1[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_1[i], value);
}
}
writel(1, dev->enc_reg_base[0] + 0xEC);
writel(0, dev->enc_reg_base[0] + 0xF4);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG2_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_2[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_2[i], value);
}
}
}
void mtk_vcodec_dec_timeout_dump(void *ctx)
{
}
@@ -0,0 +1,50 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
void mtk_vcodec_enc_timeout_dump(void *ctx);
void mtk_vcodec_dec_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,64 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6833"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,777 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT 33333
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* free_hist_by_handle = free the history specified by the handle
*
* Returns 0 if history is found and freed
* -1 if there is nothing to free
*/
int free_hist_by_handle(void *handle, struct codec_history **head)
{
struct codec_history *target = 0;
target = find_hist(handle, *head);
if (target == 0)
return -1;
return free_hist_(head, target);
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
long long tot_time = 0;
int i = 0;
if (hist == 0)
return 0;
/* Special SW/HW separation case for single 60FPS */
if (hist->sw_time[0] != 0) {
tot_time = 0;
for (i = 0; i < MAX_HISTORY; i++) {
/* Sum SW time*/
tot_time += hist->sw_time[i];
}
#if BITS_PER_LONG == 32
tot_time = div_u64(tot_time, MAX_HISTORY);
return (hist->tot_kcy / hist->cur_cnt) +
div_u64((tot_time * 364LL), 1000LL);
#else
tot_time = tot_time / MAX_HISTORY;
return (hist->tot_kcy / hist->cur_cnt) +
((tot_time * 364LL) / 1000LL);
#endif
}
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (head->next == 0 &&
((hist->submit_interval < 17000LL &&
hist->submit_interval > 16000LL) ||
(hist->submit_interval < 34000LL &&
hist->submit_interval > 33000LL))) {
/* single instance 4K30 / 60fps */
deadline = now_us + hist->submit_interval;
}
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away or instance count change, restart */
if ((hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP)
|| (job->hw_kcy > 0 && hist->sw_time[prev_idx] == 0)
|| (job->hw_kcy == 0 && hist->sw_time[prev_idx] > 0)) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->sw_time, 0, sizeof(long long)*MAX_HISTORY);
if (job->hw_kcy > 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[0] =
(int)div_64(job->mhz * (job->end - job->start),
1000LL);
}
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
if (job->hw_kcy > 0) {
hist->sw_time[0] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than
* requested
*/
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st0 e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[0]);
#endif
}
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
if (job->hw_kcy != 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[hist_idx] =
(int)div_64(job->mhz * (job->end - job->start), 1000LL);
}
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
if (job->hw_kcy != 0) {
hist->sw_time[hist_idx] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than requested */
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[hist_idx]);
#endif
} else {
hist->sw_time[hist_idx] = 0;
}
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,59 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long sw_time[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int hw_kcy;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
int free_hist_by_handle(void *handle, struct codec_history **head);
#endif
@@ -0,0 +1,793 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "smi_public.h"
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "mt6853/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 218
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#if DEC_EMI_BW
#include <mtk_smi.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
static struct plist_head vdec_rlist;
static struct mm_qos_request vdec_mc;
static struct mm_qos_request vdec_ufo;
static struct mm_qos_request vdec_pp;
static struct mm_qos_request vdec_pred_rd;
static struct mm_qos_request vdec_pred_wr;
static struct mm_qos_request vdec_ppwrap;
static struct mm_qos_request vdec_tile;
static struct mm_qos_request vdec_vld;
static struct mm_qos_request vdec_vld2;
static struct mm_qos_request vdec_avc_mv;
static struct mm_qos_request vdec_ufo_enc;
static struct mm_qos_request vdec_rg_ctrl_dma;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6853-vcodec-dec");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#if DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
struct mtk_vcodec_dev *dev;
unsigned long flags;
smi_bus_prepare_enable(SMI_LARB4, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
else {
dev = container_of(pm, struct mtk_vcodec_dev, pm);
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = true;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
set_swpm_vdec_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_DEC, hw_id);
if (hw_id == MTK_VDEC_CORE) {
larb_port_num = SMI_LARB4_PORT_NUM;
larb_id = 4;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid hw_id %d", hw_id);
time_check_end(MTK_FMT_DEC, hw_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_DEC, hw_id, 50);
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
unsigned long flags;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
/* avoid translation fault callback dump reg not done */
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = false;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
set_swpm_vdec_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB4, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0, ufo_cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
void __iomem *vdec_ufo_addr = dev->dec_reg_base[VDEC_UFO];
struct mtk_vcodec_ctx *ctx = NULL;
int misc_offset[4] = {64, 66, 67, 65};
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset, idx;
unsigned long value;
u32 fourcc;
u32 is_ufo = 0;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
if (readl(vdec_gcon_addr) == 0) {
mtk_v4l2_debug(0, "VDEC not HW break since clk off. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
return;
}
if (fourcc != V4L2_PIX_FMT_AV1)
is_ufo = readl(vdec_ufo_addr + 0x08C) & 0x1;
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
if (is_ufo)
writel((readl(vdec_ufo_addr + 0x01C) & 0xFFFFFFFD),
vdec_ufo_addr + 0x01C);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
if (is_ufo)
ufo_cg_status = readl(vdec_ufo_addr + 0x08C);
while (((cg_status & 0x11) != 0x11) ||
(is_ufo && ((ufo_cg_status & 0x11000) != 0x11000))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x(%c%c%c%c) ufo %d",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF, is_ufo);
value = readl(vdec_gcon_addr + (0 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
0 << 2, 0, value);
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
for (offset = 64; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
for (idx = 0; idx < 4; idx++) {
offset = misc_offset[idx];
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
if (is_ufo)
mtk_v4l2_err("[DEBUG][UFO] 0x%x(%d) = 0x%lx",
0x08C, 0x08C >> 2, ufo_cg_status);
if (timeout == 20000)
timeout = 1000000;
else if (timeout == 1000000) {
/* v4l2_aee_print(
* "%s %p codec:0x%08x(%c%c%c%c) hw break timeout\n",
* __func__, ctx, fourcc,
* fourcc & 0xFF, (fourcc >> 8) & 0xFF,
* (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
*/
break;
}
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
if (is_ufo)
ufo_cg_status = readl(vdec_ufo_addr + 0x08C);
}
/* sw reset */
if (is_ufo)
writel((readl(vdec_ufo_addr + 0x01C) | 0x2),
vdec_ufo_addr + 0x01C);
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_vdec_dump_addr_reg(
struct mtk_vcodec_dev *dev, int hw_id, enum mtk_dec_dump_addr_type type)
{
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
void __iomem *vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *mc_addr = dev->dec_reg_base[VDEC_MC];
void __iomem *mv_addr = dev->dec_reg_base[VDEC_MV];
void __iomem *ufo_addr = dev->dec_reg_base[VDEC_UFO];
unsigned long value, values[6];
bool is_ufo = false;
int i, j, start, end;
unsigned long flags;
#define INPUT_VLD_NUM 7
const unsigned int input_vld_reg[INPUT_VLD_NUM] = {
0xB0, 0xB4, 0xB8, 0x110, 0xEC, 0xF8, 0xFC};
// RPTR, VSTART, VEND, WPTR, VBAR, VWPTR, VRPTR
#define OUTPUT_MC_NUM 2
const unsigned int output_mc_reg[OUTPUT_MC_NUM] = {
0x224, 0x228}; // PY_ADD, PC_ADD
#define OUTPUT_UFO_MC_NUM 5
const unsigned int output_ufo_mc_reg[OUTPUT_UFO_MC_NUM] = {
0xB5C, 0xAE8, 0xAEC, 0xCE4, 0xCE8};
// YC_SEP, LEN_Y, LEN_C, LEN_Y_OFFSET, LEN_C_OFFSET
#define OUTPUT_UFO_NUM 4
const unsigned int output_ufo_reg[OUTPUT_UFO_NUM] = {
0x7C, 0x80, 0x84, 0x88}; // LEN_Y, LEN_C, BS_Y, BS_C
#define REF_MC_NUM 7
const unsigned int ref_mc_base[REF_MC_NUM] = {
0x3DC, 0xB60, 0x45C, 0xBE0, 0x4DC, 0xC60, 0xD28};
// P_L0_Y, P_L0_C, B_L0_Y, B_L0_C, B_L1_Y, B_L1_C, REF
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("hw_id %d not support !!", hw_id);
return;
}
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
if (dev->dec_is_power_on[hw_id] == false) {
mtk_v4l2_err("hw %d power is off !!", hw_id);
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
return;
}
if (hw_id == MTK_VDEC_CORE && fourcc != V4L2_PIX_FMT_AV1)
is_ufo = (readl(ufo_addr + 0x08C) & 0x1) == 0x1;
switch (type) {
case DUMP_VDEC_IN_BUF:
for (i = 0; i < INPUT_VLD_NUM; i++) {
value = readl(vld_addr + input_vld_reg[i]);
mtk_v4l2_err("[VLD] 0x%x(%d) = 0x%lx",
input_vld_reg[i], input_vld_reg[i]/4, value);
}
break;
case DUMP_VDEC_OUT_BUF:
for (i = 0; i < OUTPUT_MC_NUM; i++) {
value = readl(mc_addr + output_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_mc_reg[i], output_mc_reg[i]/4, value);
}
if (is_ufo) {
for (i = 0; i < OUTPUT_UFO_MC_NUM; i++) {
value = readl(mc_addr + output_ufo_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_ufo_mc_reg[i],
output_ufo_mc_reg[i]/4, value);
}
for (i = 0; i < OUTPUT_UFO_NUM; i++) {
value = readl(ufo_addr + output_ufo_reg[i]);
mtk_v4l2_err("[UFO] 0x%x(%d) = 0x%lx",
output_ufo_reg[i],
output_ufo_reg[i]/4, value);
}
}
break;
case DUMP_VDEC_REF_BUF:
for (i = 0; i < 32; i++) {
for (j = 0; j < 6; j++) {
values[j] = readl(mc_addr +
(ref_mc_base[j] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[0] + i * 4,
ref_mc_base[0]/4 + i, values[0],
ref_mc_base[1] + i * 4,
ref_mc_base[1]/4 + i, values[1],
ref_mc_base[2] + i * 4,
ref_mc_base[2]/4 + i, values[2],
ref_mc_base[3] + i * 4,
ref_mc_base[3]/4 + i, values[3],
ref_mc_base[4] + i * 4,
ref_mc_base[4]/4 + i, values[4],
ref_mc_base[5] + i * 4,
ref_mc_base[5]/4 + i, values[5]);
}
for (i = 0; i < 4; i++)
values[i] = readl(mc_addr + i * 4);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
i * 4, i, values[0],
i * 4, i, values[1],
i * 4, i, values[2],
i * 4, i, values[3]);
for (i = 0; i < 6; i++) {
values[i] = readl(mc_addr +
(ref_mc_base[6] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[6],
ref_mc_base[6]/4 + 0, values[0],
ref_mc_base[6] + 1 * 4,
ref_mc_base[6]/4 + 1, values[1],
ref_mc_base[6] + 2 * 4,
ref_mc_base[6]/4 + 2, values[2],
ref_mc_base[6] + 3 * 4,
ref_mc_base[6]/4 + 3, values[3],
ref_mc_base[6] + 4 * 4,
ref_mc_base[6]/4 + 4, values[4],
ref_mc_base[6] + 5 * 4,
ref_mc_base[6]/4 + 5, values[5]);
break;
case DUMP_VDEC_MV_BUF:
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("not support dump MV at hw_id %d",
hw_id);
break;
}
value = readl(mv_addr + 0x20C);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
0x20C, 0x20C/4, value);
switch (fourcc) {
case V4L2_PIX_FMT_H265:
start = 0;
end = 32;
break;
case V4L2_PIX_FMT_H264:
start = 96;
end = 128;
break;
case V4L2_PIX_FMT_VP9:
start = 240;
end = 241;
break;
case V4L2_PIX_FMT_AV1:
start = 353;
end = 356;
break;
default:
start = 195;
end = 198;
}
for (i = start; i < end; i++) {
value = readl(mv_addr + i * 4);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
i * 4, i, value);
}
break;
default:
mtk_v4l2_err("unknown addr type");
}
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
#ifdef CONFIG_MTK_IOMMU_V2
enum mtk_iommu_callback_ret_t mtk_vdec_translation_fault_callback(
int port, unsigned long mva, void *data)
{
struct mtk_vcodec_dev *dev = (struct mtk_vcodec_dev *)data;
int hw_id = MTK_VDEC_CORE;
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
ctx = dev->curr_dec_ctx[hw_id];
if (ctx) {
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
mtk_v4l2_err("codec:0x%08x(%c%c%c%c) TF larb %d port %x mva 0x%lx",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF,
port >> 5, port, mva);
} else {
mtk_v4l2_err("ctx NULL codec unknown, TF larb %d port %x mva 0x%lx",
port >> 5, port, mva);
}
switch (port) {
case M4U_PORT_L4_VDEC_VLD_EXT:
case M4U_PORT_L4_VDEC_VLD2_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
break;
case M4U_PORT_L4_VDEC_PP_EXT:
case M4U_PORT_L4_VDEC_UFO_ENC_EXT:
mtk_vdec_dump_addr_reg(dev, MTK_VDEC_CORE, DUMP_VDEC_OUT_BUF);
break;
case M4U_PORT_L4_VDEC_MC_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_REF_BUF);
break;
case M4U_PORT_L4_VDEC_AVC_MV_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_MV_BUF);
break;
default:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_OUT_BUF);
}
return MTK_IOMMU_CALLBACK_HANDLED;
}
void mtk_vdec_translation_fault_callback_setting(
struct mtk_vcodec_dev *dev)
{
int i;
for (i = M4U_PORT_L4_VDEC_MC_EXT; i <= M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT; i++) {
mtk_iommu_register_fault_callback(i,
mtk_vdec_translation_fault_callback, (void *)dev);
}
}
#endif
void mtk_prepare_vdec_dvfs(void)
{
#if DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#if DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
plist_head_init(&vdec_rlist);
mm_qos_add_request(&vdec_rlist, &vdec_mc, M4U_PORT_L4_VDEC_MC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo, M4U_PORT_L4_VDEC_UFO_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pp, M4U_PORT_L4_VDEC_PP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_rd,
M4U_PORT_L4_VDEC_PRED_RD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_wr,
M4U_PORT_L4_VDEC_PRED_WR_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ppwrap,
M4U_PORT_L4_VDEC_PPWRAP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_tile, M4U_PORT_L4_VDEC_TILE_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld, M4U_PORT_L4_VDEC_VLD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld2, M4U_PORT_L4_VDEC_VLD2_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_avc_mv,
M4U_PORT_L4_VDEC_AVC_MV_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo_enc,
M4U_PORT_L4_VDEC_UFO_ENC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_rg_ctrl_dma,
M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mm_qos_remove_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
long long op_rate_to_freq = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (ctx->dec_params.operating_rate > 0) {
if (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_MPEG4) {
op_rate_to_freq = 312LL *
ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height *
ctx->dec_params.operating_rate /
2048LL / 1088LL / 60LL;
} else {
op_rate_to_freq = 312LL *
ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height *
ctx->dec_params.operating_rate /
3840LL / 2160LL / 30LL;
}
target_freq_64 = match_freq((int)op_rate_to_freq,
&vdec_freq_steps[0],
vdec_freq_step_size);
vdec_freq = target_freq_64;
if (vdec_cur_job != 0)
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
} else if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq;
if (vdec_freq > target_freq_64)
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job != 0 && vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
pr_debug("no vdec job, probably error handling path\n");
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long emi_bw = 0;
long emi_bw_input = 0;
long emi_bw_output = 0;
int is_ufo_on = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8L * 1920 * 1080 * 2 * 10 * vdec_freq;
emi_bw_input = 8 * vdec_freq / STD_VDEC_FREQ;
emi_bw_output = 1920 * 1088 * 3 * 20 * 10 * vdec_freq /
2 / 3 / STD_VDEC_FREQ / 1024 / 1024;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
is_ufo_on = (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H264 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP9) &&
((ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height) >=
(1920 * 1080)) ? 1 : 0;
/* bits/s to MBytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
if (is_ufo_on == 1) { /* UFO */
emi_bw = emi_bw * 6 / 10;
emi_bw_output = emi_bw_output * 6 / 10;
}
emi_bw = emi_bw - emi_bw_output - (emi_bw_input * 2);
if (emi_bw < 0)
emi_bw = 0;
if (is_ufo_on == 1) { /* UFO */
mm_qos_set_request(&vdec_ufo, emi_bw, 0, BW_COMP_DEFAULT);
mm_qos_set_request(&vdec_ufo_enc, emi_bw_output, 0,
BW_COMP_DEFAULT);
} else {
mm_qos_set_request(&vdec_pp, emi_bw_output, 0, BW_COMP_NONE);
}
mm_qos_set_request(&vdec_mc, emi_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, emi_bw_input * 2, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#if DEC_EMI_BW
mm_qos_set_request(&vdec_mc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo_enc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#if DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,463 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#include "mt6853/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 249
#define STD_LUMA_BW 70
#define STD_CHROMA_BW 35
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6853-vcodec-enc");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
//free_all_bw(&venc_bw);
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB7, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
set_swpm_venc_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
larb_port_num = SMI_LARB7_PORT_NUM;
larb_id = 7;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid core_id %d", core_id);
time_check_end(MTK_FMT_ENC, core_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
set_swpm_venc_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB7, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
free_hist(&venc_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_release_pmqos(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
free_hist_by_handle(&ctx->id, &venc_hists);
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu, M4U_PORT_L7_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec, M4U_PORT_L7_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma, M4U_PORT_L7_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv,
M4U_PORT_L7_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv,
M4U_PORT_L7_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma,
M4U_PORT_L7_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma,
M4U_PORT_L7_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma,
M4U_PORT_L7_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma,
M4U_PORT_L7_VENC_REF_CHROMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist);
#endif
}
void mtk_venc_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (ctx->enc_params.operationrate >= 120 &&
target_freq_64 > 458)
target_freq_64 = 458;
if (target_freq > 0) {
venc_freq = target_freq;
if (venc_freq > target_freq_64)
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
#if ENC_DVFS
static int mtk_venc_get_exec_cnt(struct mtk_vcodec_ctx *ctx)
{
return (int)((readl(ctx->dev->enc_reg_base[VENC_SYS] + 0x17C) &
0x7FFFFFFF) / 1000);
}
#endif
void mtk_venc_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_DVFS
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->enc_params.operationrate < 120) {
if (ctx->enc_params.framerate_denom == 0)
ctx->enc_params.framerate_denom = 1;
if (ctx->enc_params.operationrate == 0 &&
ctx->enc_params.framerate_num == 0)
ctx->enc_params.framerate_num =
ctx->enc_params.framerate_denom * 30;
if ((ctx->enc_params.operationrate == 60 ||
((ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) >= 59 &&
(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom) <= 60) ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width
== 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height
== 2160))) {
/* Set allowed time for 60fps/4K recording */
/* Use clock cycle if single instance */
if (venc_hists != 0 && venc_hists->next == 0) {
venc_cur_job->hw_kcy =
mtk_venc_get_exec_cnt(ctx);
}
if (ctx->enc_params.operationrate > 0) {
interval = (long long)(1000 * 1000 /
(int)ctx->enc_params.operationrate);
} else {
interval = (long long)(1000 * 1000 /
(int)(ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom));
}
update_hist(venc_cur_job, &venc_hists,
interval);
} else {
update_hist(venc_cur_job, &venc_hists, 0);
}
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
int f_type = 1; /* TODO */
int boost_perc = 0;
int rcpu_bw = 5;
int rec_bw = 0;
int bsdma_bw = 10;
int sv_comv_bw = 5;
int rd_comv_bw = 10;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (ctx->enc_params.operationrate >= 120 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H265 ||
(ctx->q_data[MTK_Q_DATA_SRC].visible_width == 3840 &&
ctx->q_data[MTK_Q_DATA_SRC].visible_height == 2160)) {
boost_perc = 100;
} else if (ctx->enc_params.operationrate == 60) {
boost_perc = 30;
}
/* Input BW scaling to venc_freq & config */
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc)
/ STD_VENC_FREQ / 100;
/* BW in venc_freq */
if (f_type == 0) {
rec_bw = cur_luma_bw + cur_chroma_bw;
} else {
rec_bw = cur_luma_bw + cur_chroma_bw;
ref_luma_bw = cur_luma_bw * 4;
ref_chroma_bw = cur_chroma_bw * 4;
}
if (1) { /* UFO */
ref_chroma_bw += ref_luma_bw;
ref_luma_bw = 0;
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
if (1) { /* UFO */
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_VENC);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_VENC);
} else {
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_NONE);
}
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct mtk_vcodec_ctx *ctx)
{
#if ENC_EMI_BW
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
#if ENC_DVFS
mutex_lock(&ctx->dev->enc_dvfs_mutex);
add_job(&ctx->id, &venc_jobs);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_begin(ctx);
mtk_venc_emi_bw_begin(ctx);
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
mtk_venc_dvfs_end(ctx);
mtk_venc_emi_bw_end(ctx);
}
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
}
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,291 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
if (ctx->dec_params.svp_mode) {
mtk_v4l2_debug(4, "svp_mode %d don't handle",
ctx->dec_params.svp_mode);
return IRQ_HANDLED;
}
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler,
IRQF_NO_THREAD | IRQF_SHARED | IRQF_PROBE_SHARED, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
struct mtk_vcodec_ctx *curr_ctx = ctx;
if (curr_ctx->type == MTK_INST_ENCODER)
mtk_vcodec_enc_timeout_dump(ctx);
else if (curr_ctx->type == MTK_INST_DECODER)
mtk_vcodec_dec_timeout_dump(ctx);
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
void mtk_vcodec_enc_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0, j = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define REG1_COUNT 13
#define REG2_COUNT 46
unsigned int Reg_1[REG1_COUNT] = {
0x14, 0xEC, 0x1C0, 0x1168, 0x11C0,
0x11C4, 0xF4, 0x5C, 0x60, 0x130,
0x24, 0x114C, 0x1164};
unsigned int Reg_2[REG2_COUNT] = {
0xEC, 0x200, 0x204, 0x208, 0x20C,
0x210, 0x214, 0x218, 0x21C,
0x220, 0x224, 0x228, 0x22C,
0x230, 0x234, 0x238, 0x23C,
0x240, 0x244, 0x248, 0x24C,
0x250, 0x254, 0x258, 0x25C,
0x260, 0x264, 0x268, 0x26C,
0x270, 0x274, 0x278, 0x27C,
0x280,
0x22C, 0x230, 0xF4, 0x1168,
0x11C0, 0x11C4, 0x1030, 0x240,
0x248, 0x250, 0x130, 0x140};
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump venc for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG1_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_1[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_1[i], value);
}
}
writel(1, dev->enc_reg_base[0] + 0xEC);
writel(0, dev->enc_reg_base[0] + 0xF4);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG2_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_2[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_2[i], value);
}
}
}
void mtk_vcodec_dec_timeout_dump(void *ctx)
{
}
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
void mtk_vcodec_enc_timeout_dump(void *ctx);
void mtk_vcodec_dec_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6853"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_UFO,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,771 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT 33333
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* free_hist_by_handle = free the history specified by the handle
*
* Returns 0 if history is found and freed
* -1 if there is nothing to free
*/
int free_hist_by_handle(void *handle, struct codec_history **head)
{
struct codec_history *target = 0;
target = find_hist(handle, *head);
if (target == 0)
return -1;
return free_hist_(head, target);
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
long long tot_time = 0;
int i = 0;
if (hist == 0)
return 0;
/* Special SW/HW separation case for single 60FPS */
if (hist->sw_time[0] != 0) {
tot_time = 0;
for (i = 0; i < MAX_HISTORY; i++) {
/* Sum SW time*/
tot_time += hist->sw_time[i];
}
tot_time = tot_time / MAX_HISTORY;
return (hist->tot_kcy / hist->cur_cnt) +
((tot_time * 364LL) / 1000LL);
}
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (head->next == 0 &&
((hist->submit_interval < 17000LL &&
hist->submit_interval > 16000LL) ||
(hist->submit_interval < 34000LL &&
hist->submit_interval > 33000LL))) {
/* single instance 4K30 / 60fps */
deadline = now_us + hist->submit_interval;
}
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away or instance count change, restart */
if ((hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP)
|| (job->hw_kcy > 0 && hist->sw_time[prev_idx] == 0)
|| (job->hw_kcy == 0 && hist->sw_time[prev_idx] > 0)) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->sw_time, 0, sizeof(long long)*MAX_HISTORY);
if (job->hw_kcy > 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[0] =
(int)div_64(job->mhz * (job->end - job->start),
1000LL);
}
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
if (job->hw_kcy > 0) {
hist->sw_time[0] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than
* requested
*/
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st0 e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[0]);
#endif
}
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
if (job->hw_kcy != 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[hist_idx] =
(int)div_64(job->mhz * (job->end - job->start), 1000LL);
}
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
if (job->hw_kcy != 0) {
hist->sw_time[hist_idx] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than requested */
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[hist_idx]);
#endif
} else {
hist->sw_time[hist_idx] = 0;
}
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long sw_time[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int hw_kcy;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
int free_hist_by_handle(void *handle, struct codec_history **head);
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
int mtk_vdec_ion_config_buff(struct dma_buf *dmabuf);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,711 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <linux/delay.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#include "smi_port.h"
#endif
#define USE_GCE 1
#ifdef ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 250
#define STD_LUMA_BW 100
#define STD_CHROMA_BW 50
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
/*
* static struct codec_history *venc_hists;
* static struct codec_job *venc_jobs;
*/
/* 1080p60, 4k30, 4k60, 1 core 4k60*/
static u64 venc_freq_map[] = {249, 364, 458, 624};
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
#include <mtk_qos_bound.h>
#include "vcodec_bw.h"
#define CORE_NUM 1
/*
* static unsigned int gVENCFrmTRAVC[3] = {6, 12, 6};
* static unsigned int gVENCFrmTRHEVC[3] = {6, 12, 6};
*
* static long long venc_start_time;
* static struct vcodec_bw *venc_bw;
*/
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
static struct mm_qos_request venc_sub_r_luma;
static struct mm_qos_request venc_sub_w_luma;
#endif
#if 1
/* first scenario based version, will change to loading based */
struct temp_job {
int ctx_id;
int format;
int type;
int module;
int visible_width; /* temp usage only, will use kcy */
int visible_height; /* temp usage only, will use kcy */
int operation_rate;
long long submit;
int kcy;
struct temp_job *next;
};
static struct temp_job *temp_venc_jobs;
struct temp_job *new_job_from_info(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct temp_job *new_job = kmalloc(sizeof(struct temp_job), GFP_KERNEL);
if (new_job == 0)
return 0;
new_job->ctx_id = ctx->id;
new_job->format = ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;
new_job->type = 1; /* temp */
new_job->module = core_id;
new_job->visible_width = ctx->q_data[MTK_Q_DATA_SRC].visible_width;
new_job->visible_height = ctx->q_data[MTK_Q_DATA_SRC].visible_height;
new_job->operation_rate = 0;
new_job->submit = 0; /* use now - to be filled */
new_job->kcy = 0; /* retrieve hw counter - to be filled */
new_job->next = 0;
return new_job;
}
void free_job(struct temp_job *job)
{
if (job != 0)
kfree(job);
}
int add_to_tail(struct temp_job **job_list, struct temp_job *job)
{
int cnt = 0;
struct temp_job *tail = 0;
if (job == 0)
return -1;
tail = *job_list;
if (tail == 0) {
*job_list = job;
return 1;
}
cnt = 1;
while (tail->next != 0) {
cnt++;
tail = tail->next;
}
cnt++;
tail->next = job;
return cnt;
}
struct temp_job *remove_from_head(struct temp_job **job_list)
{
struct temp_job *head = *job_list;
if (head == 0)
return 0;
*job_list = head->next;
return head;
}
#endif
static struct ion_client *ion_venc_client;
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 1;
#ifdef CONFIG_MTK_SLBC
ctx->sram_data.uid = UID_MM_VENC;
ctx->sram_data.type = TP_BUFFER;
ctx->sram_data.size = 0;
ctx->sram_data.flag = FG_POWER;
#endif
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6873-vcodec-enc");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC0 = devm_clk_get(&pdev->dev, "MT_CG_VENC0");
if (IS_ERR(pm->clk_MT_CG_VENC0)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC0\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC0);
}
#endif
ion_venc_client = NULL;
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
#if ENC_EMI_BW
/* do nothing */
#endif
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
smi_bus_prepare_enable(SMI_LARB7, "VENC0");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC0);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
} else {
mtk_v4l2_err("invalid core_id %d", core_id);
time_check_end(MTK_FMT_ENC, core_id, 50);
return;
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
larb_port_num = SMI_LARB7_PORT_NUM;
larb_id = 7;
}
//enable 34bits port configs
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
#ifndef FPGA_PWRCLK_API_DISABLE
if (core_id == MTK_VENC_CORE_0) {
clk_disable_unprepare(pm->clk_MT_CG_VENC0);
smi_bus_disable_unprepare(SMI_LARB7, "VENC0");
} else
mtk_v4l2_err("invalid core_id %d", core_id);
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
temp_venc_jobs = 0;
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
/* free_hist(&venc_hists, 0); */
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu,
M4U_PORT_L7_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec,
M4U_PORT_L7_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma,
M4U_PORT_L7_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv,
M4U_PORT_L7_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv,
M4U_PORT_L7_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma,
M4U_PORT_L7_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma,
M4U_PORT_L7_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma,
M4U_PORT_L7_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma,
M4U_PORT_L7_VENC_REF_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_sub_r_luma,
M4U_PORT_L7_VENC_SUB_R_LUMA);
mm_qos_add_request(&venc_rlist, &venc_sub_w_luma,
M4U_PORT_L7_VENC_SUB_W_LUMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist);
#endif
}
void mtk_venc_dvfs_begin(struct temp_job **job_list)
{
#if 0
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (target_freq > 0) {
venc_freq = target_freq;
if (venc_freq > target_freq_64)
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
#if ENC_DVFS
struct temp_job *job = *job_list;
int area = 0;
int idx = 0;
if (job == 0)
return;
area = job->visible_width * job->visible_height;
if (area >= 3840 * 2160)
idx = 2;
else if (area >= 1920 * 1080)
if (job->operation_rate > 30)
idx = 2;
else
idx = 0;
else
idx = 0;
if (job->operation_rate >= 120)
idx = 2;
if (job->format == V4L2_PIX_FMT_HEIF)
idx = 3;
venc_freq = venc_freq_map[idx];
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq);
#endif
}
void mtk_venc_dvfs_end(struct temp_job *job)
{
#if 0
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->async_mode == 0) {
update_hist(venc_cur_job, &venc_hists, 0);
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval*1000);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
#if ENC_DVFS
if (job == 0)
return;
venc_freq = venc_freq_map[0];
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq);
#endif
}
void mtk_venc_emi_bw_begin(struct temp_job **jobs)
{
#if 0
int f_type = 1; /* TODO */
int boost_perc = 0;
long emi_bw = 0;
if (ctx->async_mode == 1)
boost_perc = 100;
if (ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_HEIF)
boost_perc = 150;
emi_bw = 8L * 1920 * 1080 * 3 * 10 * venc_freq;
switch (ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * gVENCFrmTRAVC[f_type] *
(100 + boost_perc) / (2 * STD_VENC_FREQ) / 100;
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * gVENCFrmTRHEVC[f_type] *
(100 + boost_perc) / (2 * STD_VENC_FREQ) / 100;
break;
default:
emi_bw = 0;
pr_debug("Unsupported encoder type for BW");
}
/* transaction bytes to occupied BW */
emi_bw = emi_bw * 4 / 3;
/* bits/s to mbytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
mtk_pm_qos_update_request(&venc_qos_req_bw, (int)emi_bw);
#endif
#if ENC_EMI_BW
struct temp_job *job = 0;
int id = 0;
int boost_perc = 0;
int rcpu_bw = 5 * 4 / 3;
int rec_bw = 0;
int bsdma_bw = 20 * 4 / 3;
int sv_comv_bw = 4 * 4 / 3;
int rd_comv_bw = 16 * 4 / 3;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (*jobs == 0)
return;
job = *jobs;
id = job->module;
if (job->operation_rate > 60)
boost_perc = 100;
if (job->format == V4L2_PIX_FMT_H265 ||
job->format == V4L2_PIX_FMT_HEIF ||
(job->format == V4L2_PIX_FMT_H264 &&
job->visible_width >= 2160)) {
boost_perc = 150;
}
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc) * 4/
STD_VENC_FREQ / 100 / 3;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc) * 4 /
STD_VENC_FREQ / 100 / 3;
rec_bw = cur_luma_bw + cur_chroma_bw;
if (0) { /* no UFO */
ref_luma_bw = cur_luma_bw * 1;
ref_chroma_bw = cur_chroma_bw * 1;
} else {
ref_luma_bw = 0;
ref_chroma_bw = (cur_luma_bw * 1) + (cur_chroma_bw * 1);
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_sub_r_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sub_w_luma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct temp_job *job)
{
#if ENC_EMI_BW
int core_id;
if (job == 0)
return;
core_id = job->module;
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sub_r_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sub_w_luma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
struct temp_job *mtk_venc_queue_job(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
int cnt = 0;
struct temp_job *job = new_job_from_info(ctx, core_id);
if (job != 0)
cnt = add_to_tail(&temp_venc_jobs, job);
return job;
}
struct temp_job *mtk_venc_dequeue_job(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
struct temp_job *job = remove_from_head(&temp_venc_jobs);
if (job != 0)
return job;
/* print error message */
return 0;
}
/* Total job count after this one is inserted */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
/* mutex_lock(&ctx->dev->enc_dvfs_mutex); */
struct temp_job *job = 0;
int frame_rate = 0;
job = mtk_venc_queue_job(ctx, core_id, job_cnt);
frame_rate = ctx->enc_params.operationrate;
if (frame_rate == 0) {
frame_rate = ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom;
}
if (job != NULL)
job->operation_rate = frame_rate;
if (job_cnt == 0) {
// Adjust dvfs immediately
mtk_venc_dvfs_begin(&temp_venc_jobs);
mtk_venc_emi_bw_begin(&temp_venc_jobs);
}
/* mutex_unlock(&ctx->dev_enc_dvfs_mutex); */
}
/* Remaining job count after this one is done */
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
struct temp_job *job = mtk_venc_dequeue_job(ctx, core_id, job_cnt);
mtk_venc_dvfs_end(job);
mtk_venc_emi_bw_end(job);
free_job(job);
if (job_cnt > 1) {
mtk_venc_dvfs_begin(&temp_venc_jobs);
mtk_venc_emi_bw_begin(&temp_venc_jobs);
}
}
int mtk_venc_ion_config_buff(struct dma_buf *dmabuf)
{
/* for dma-buf using ion buffer, ion will check portid in dts
* So, don't need to config buffer at user side, but remember
* set iommus attribute in dts file.
*/
#if 0
struct ion_handle *handle = NULL;
struct ion_mm_data mm_data;
int count = 0;
mtk_v4l2_debug(4, "%p", dmabuf);
if (!ion_venc_client)
ion_venc_client = ion_client_create(g_ion_device, "venc");
handle = ion_import_dma_buf(ion_venc_client, dmabuf);
if (IS_ERR(handle)) {
mtk_v4l2_err("import ion handle failed!\n");
return -1;
}
mm_data.mm_cmd = ION_MM_CONFIG_BUFFER;
mm_data.config_buffer_param.kernel_handle = handle;
mm_data.config_buffer_param.module_id = M4U_PORT_L7_VENC_RD_COMV_DISP;
mm_data.config_buffer_param.security = 0;
mm_data.config_buffer_param.coherent = 0;
while (1) {
int ion_config = 0;
ion_config = ion_kernel_ioctl(ion_venc_client,
ION_CMD_MULTIMEDIA, (unsigned long)&mm_data);
if (ion_config != 0) {
udelay(1000);
count++;
mtk_v4l2_err("re-configure buffer! count: %d\n", count);
} else {
break;
}
}
if (count > 0)
mtk_v4l2_err("re-configure buffer done! count: %d\n", count);
/* dma hold ref, ion directly free */
ion_free(ion_venc_client, handle);
#endif
return 0;
}
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
/* Frame based PMQoS */
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
int mtk_venc_ion_config_buff(struct dma_buf *dmabuf);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,398 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
if (core_id >= MTK_VDEC_HW_NUM ||
core_id < 0) {
mtk_v4l2_err("ctx %d, invalid core_id=%d", ctx->id, core_id);
return -1;
}
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, core_id %d timeout time=%ums out %d %d!",
ctx->id, ctx->type, core_id, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, core_id %d timeout interrupted by a signal %d %d",
ctx->id, ctx->type, core_id,
command, ctx->int_cond[core_id],
ctx->int_type);
status = -2;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx, int core_id)
{
ctx->int_cond[core_id] = 1;
wake_up_interruptible(&ctx->queue[core_id]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, MTK_VDEC_CORE);
if (ctx == NULL)
return IRQ_HANDLED;
if (ctx->dec_params.svp_mode) {
mtk_v4l2_debug(4, "svp_mode %d don't handle",
ctx->dec_params.svp_mode);
return IRQ_HANDLED;
}
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[VDEC_SYS]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx, MTK_VDEC_CORE);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
irqreturn_t mtk_vcodec_lat_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_LAT_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, MTK_VDEC_LAT);
if (ctx == NULL)
return IRQ_HANDLED;
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[VDEC_BASE]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC LAT ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_LAT_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_LAT_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx, MTK_VDEC_LAT);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_lat_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (ctx == NULL)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int i = 0;
int ret = 0;
for (i = 0; i < MTK_VDEC_HW_NUM; i++) {
dev->dec_irq[i] = platform_get_irq(pdev, i);
if (i == MTK_VDEC_CORE)
ret = devm_request_irq(&pdev->dev, dev->dec_irq[i],
mtk_vcodec_dec_irq_handler,
IRQF_NO_THREAD | IRQF_SHARED | IRQF_PROBE_SHARED, pdev->name, dev);
else if (i == MTK_VDEC_LAT)
ret = devm_request_irq(&pdev->dev, dev->dec_irq[i],
mtk_vcodec_lat_dec_irq_handler, 0,
pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq[%d] %d (%d)",
i, dev->dec_irq[i],
ret);
}
disable_irq(dev->dec_irq[i]);
}
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
struct mtk_vcodec_ctx *curr_ctx = ctx;
if (curr_ctx->type == MTK_INST_ENCODER)
mtk_vcodec_enc_timeout_dump(ctx);
else if (curr_ctx->type == MTK_INST_DECODER)
mtk_vcodec_dec_timeout_dump(ctx);
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
void mtk_vcodec_enc_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0, j = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define REG1_COUNT 13
#define REG2_COUNT 46
unsigned int Reg_1[REG1_COUNT] = {
0x14, 0xEC, 0x1C0, 0x1168, 0x11C0,
0x11C4, 0xF4, 0x5C, 0x60, 0x130,
0x24, 0x114C, 0x1164};
unsigned int Reg_2[REG2_COUNT] = {
0xEC, 0x200, 0x204, 0x208, 0x20C,
0x210, 0x214, 0x218, 0x21C,
0x220, 0x224, 0x228, 0x22C,
0x230, 0x234, 0x238, 0x23C,
0x240, 0x244, 0x248, 0x24C,
0x250, 0x254, 0x258, 0x25C,
0x260, 0x264, 0x268, 0x26C,
0x270, 0x274, 0x278, 0x27C,
0x280,
0x22C, 0x230, 0xF4, 0x1168,
0x11C0, 0x11C4, 0x1030, 0x240,
0x248, 0x250, 0x130, 0x140};
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump venc for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG1_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_1[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_1[i], value);
}
}
writel(1, dev->enc_reg_base[0] + 0xEC);
writel(0, dev->enc_reg_base[0] + 0xF4);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG2_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_2[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_2[i], value);
}
}
}
void mtk_vcodec_dec_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define LAT_REG_COUNT 26
#define CORE_MISC_REG_COUNT 30
#define CORE_VLD_REG_COUNT 5
unsigned int lat_reg[LAT_REG_COUNT] = {
0x5120, 0x512C, 0x5090, // BITCNT, POS, picture start
0x50E0, 0x50E4, 0x50E8,
0x1120, 0x1124, 0x1128, 0x112C, 0x196C, // input, cycle
0x108, 0x110, 0x114, 0x118, 0x11C, // SMI
0x120, 0x124, 0x128, 0x12C, 0x130, 0x134, 0x138, 0x13C,
0x854, 0x878}; // crc, wptr
unsigned int core_misc_reg[CORE_MISC_REG_COUNT] = {
0x3120, 0x312C, 0x3090, // BITCNT, POS, picture start
0x30E0, 0x30E4, 0x30E8, // error
0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C, 0x20, 0x24,
0xC8, 0xCC, 0x178, // power
0x108, 0x110, 0x114, 0x118, 0x11C, // SMI
0x120, 0x124, 0x128, 0x12C, 0x130, 0x134, 0x138, 0x13C};
unsigned int core_vld_reg[CORE_VLD_REG_COUNT] = {
0x120, 0x124, 0x128, 0x12C, 0x96C}; // input, cycle
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump vdec for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (i = 0; i < LAT_REG_COUNT; i++) {
value = readl(dev->dec_reg_base[VDEC_LAT_MISC] + lat_reg[i]);
mtk_v4l2_debug(0, "[LAT][MISC] 0x%x(%d) = 0x%lx",
lat_reg[i], ((lat_reg[i]&0x0FFF)%0x800)/4, value);
}
for (i = 0; i < CORE_MISC_REG_COUNT; i++) {
value = readl(dev->dec_reg_base[VDEC_MISC] + core_misc_reg[i]);
mtk_v4l2_debug(0, "[CORE][MISC] 0x%x(%d) = 0x%lx",
core_misc_reg[i], (core_misc_reg[i] & 0x0FFF)/4, value);
}
for (i = 0; i < CORE_VLD_REG_COUNT; i++) {
value = readl(dev->dec_reg_base[VDEC_VLD] + core_vld_reg[i]);
mtk_v4l2_debug(0, "[CORE][VLD] 0x%x(%d) = 0x%lx",
core_vld_reg[i], (core_vld_reg[i] % 0x800)/4, value);
}
}
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_dec_lat_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
void mtk_vcodec_enc_timeout_dump(void *ctx);
void mtk_vcodec_dec_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#include "ion_drv.h"
#include "mach/mt_iommu.h"
extern struct ion_device *g_ion_device;
#define MTK_PLATFORM_STR "platform:mt6873"
#define MTK_VDEC_RACING_INFO_OFFSET 0x100
#define MTK_VDEC_RACING_INFO_SIZE 68
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_SOC; /* VDEC SOC*/
struct clk *clk_MT_CG_VDEC0; /* VDEC core 0*/
struct clk *clk_MT_CG_VENC0; /* VENC core 0*/
struct clk *clk_MT_CG_VDEC1; /* VDEC core 1*/
atomic_t dec_active_cnt;
__u32 vdec_racing_info[MTK_VDEC_RACING_INFO_SIZE];
struct mutex dec_racing_info_mutex;
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
DUMP_VDEC_UBE_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_BASE,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
VDEC_LAT_MISC,
VDEC_LAT_VLD,
VDEC_RACING_CTRL,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_SYS,
VENC_C1_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,666 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT (33*1000)
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
if (hist == 0)
return 0;
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
new_mhz = div_64((*kcy) * 1000LL,
(deadline - now_us));
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away, restart */
if (hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
hist->kcy[0] = (int)div_64(job->mhz * (job->end - job->start),
1000LL);
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
hist->kcy[hist_idx] = (int)div_64(job->mhz * (job->end - job->start),
1000LL);
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
#endif
@@ -0,0 +1,773 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <soc/mediatek/smi.h>
#include <linux/slab.h>
#include "smi_public.h"
#include "mtk_vcodec_dec_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "mt6877/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if DEC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VDEC_FREQ 218
static struct mtk_pm_qos_request vdec_qos_req_f;
static u64 vdec_freq;
static u32 vdec_freq_step_size;
static u64 vdec_freq_steps[MAX_FREQ_STEP];
static struct codec_history *vdec_hists;
static struct codec_job *vdec_jobs;
#endif
#if DEC_EMI_BW
#include <mtk_smi.h>
static unsigned int h264_frm_scale[4] = {12, 24, 40, 12};
static unsigned int h265_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp9_frm_scale[4] = {12, 24, 40, 12};
static unsigned int vp8_frm_scale[4] = {12, 24, 40, 12};
static unsigned int mp24_frm_scale[5] = {16, 20, 32, 50, 16};
static struct plist_head vdec_rlist;
static struct mm_qos_request vdec_mc;
static struct mm_qos_request vdec_ufo;
static struct mm_qos_request vdec_pp;
static struct mm_qos_request vdec_pred_rd;
static struct mm_qos_request vdec_pred_wr;
static struct mm_qos_request vdec_ppwrap;
static struct mm_qos_request vdec_tile;
static struct mm_qos_request vdec_vld;
static struct mm_qos_request vdec_vld2;
static struct mm_qos_request vdec_avc_mv;
static struct mm_qos_request vdec_ufo_enc;
static struct mm_qos_request vdec_rg_ctrl_dma;
#endif
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->input_driven = 0;
ctx->user_lock_hw = 1;
}
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
pm->mtkdev = mtkdev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6877-vcodec-dec");
node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
return -1;
}
pdev = of_find_device_by_node(node);
if (WARN_ON(!pdev)) {
of_node_put(node);
return -1;
}
pm->larbvdec = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
if (pm->chip_node) {
pm->clk_MT_CG_VDEC = devm_clk_get(&pdev->dev, "MT_CG_VDEC");
if (IS_ERR(pm->clk_MT_CG_VDEC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VDEC\n");
return PTR_ERR(pm->clk_MT_CG_VDEC);
}
} else
mtk_v4l2_err("[VCODEC][ERROR] DTS went wrong...");
#endif
return ret;
}
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
{
#if DEC_DVFS
mutex_lock(&dev->dec_dvfs_mutex);
free_hist(&vdec_hists, 0);
mutex_unlock(&dev->dec_dvfs_mutex);
#endif
}
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id)
{
}
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
int ret;
struct mtk_vcodec_dev *dev;
unsigned long flags;
smi_bus_prepare_enable(SMI_LARB4, "VDEC");
ret = clk_prepare_enable(pm->clk_MT_CG_VDEC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VDEC fail %d", ret);
else {
dev = container_of(pm, struct mtk_vcodec_dev, pm);
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = true;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
set_swpm_vdec_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_DEC, hw_id);
if (hw_id == MTK_VDEC_CORE) {
larb_port_num = SMI_LARB4_PORT_NUM;
larb_id = 4;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid hw_id %d", hw_id);
time_check_end(MTK_FMT_DEC, hw_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_DEC, hw_id, 50);
#endif
}
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_dev *dev;
unsigned long flags;
dev = container_of(pm, struct mtk_vcodec_dev, pm);
mtk_vdec_hw_break(dev, hw_id);
/* avoid translation fault callback dump reg not done */
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
dev->dec_is_power_on[hw_id] = false;
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
set_swpm_vdec_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VDEC);
smi_bus_disable_unprepare(SMI_LARB4, "VDEC");
#endif
}
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id)
{
u32 cg_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC];
void __iomem *vdec_vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *vdec_gcon_addr = dev->dec_reg_base[VDEC_SYS];
struct mtk_vcodec_ctx *ctx = NULL;
int misc_offset[4] = {64, 66, 67, 65};
struct timeval tv_start;
struct timeval tv_end;
s32 usec, timeout = 20000;
int offset, idx;
unsigned long value;
u32 fourcc;
if (hw_id == MTK_VDEC_CORE) {
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
if (readl(vdec_gcon_addr) == 0) {
mtk_v4l2_debug(0, "VDEC not HW break since clk off. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
return;
}
/* hw break */
writel((readl(vdec_misc_addr + 0x0100) | 0x1),
vdec_misc_addr + 0x0100);
do_gettimeofday(&tv_start);
cg_status = readl(vdec_misc_addr + 0x0104);
while (!((cg_status & 0x1) && (cg_status & 0x10))) {
do_gettimeofday(&tv_end);
usec = (tv_end.tv_sec - tv_start.tv_sec) * 1000000 +
tv_end.tv_usec - tv_start.tv_usec;
if (usec > timeout) {
mtk_v4l2_err("VDEC HW break timeout. codec:0x%08x(%c%c%c%c)",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
value = readl(vdec_gcon_addr + (0 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
0 << 2, 0, value);
value = readl(vdec_gcon_addr + (6 << 2));
mtk_v4l2_err("[DEBUG][GCON] 0x%x(%d) = 0x%lx",
6 << 2, 6, value);
for (offset = 64; offset <= 79; offset++) {
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
for (idx = 0; idx < 4; idx++) {
offset = misc_offset[idx];
value = readl(
vdec_misc_addr + (offset << 2));
mtk_v4l2_err("[DEBUG][MISC] 0x%x(%d) = 0x%lx",
offset << 2, offset, value);
}
if (timeout == 20000)
timeout = 1000000;
else if (timeout == 1000000) {
/* v4l2_aee_print(
* "%s %p codec:0x%08x(%c%c%c%c) hw break timeout\n",
* __func__, ctx, fourcc,
* fourcc & 0xFF, (fourcc >> 8) & 0xFF,
* (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
*/
break;
}
do_gettimeofday(&tv_start);
//smi_debug_bus_hang_detect(0, "VCODEC");
}
cg_status = readl(vdec_misc_addr + 0x0104);
}
/* sw reset */
writel(0x1, vdec_vld_addr + 0x0108);
writel(0x0, vdec_vld_addr + 0x0108);
} else {
mtk_v4l2_err("hw_id (%d) is unknown\n", hw_id);
}
}
void mtk_vdec_dump_addr_reg(
struct mtk_vcodec_dev *dev, int hw_id, enum mtk_dec_dump_addr_type type)
{
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
void __iomem *vld_addr = dev->dec_reg_base[VDEC_VLD];
void __iomem *mc_addr = dev->dec_reg_base[VDEC_MC];
void __iomem *mv_addr = dev->dec_reg_base[VDEC_MV];
void __iomem *ufo_addr = dev->dec_reg_base[VDEC_UFO];
unsigned long value, values[6];
bool is_ufo = false;
int i, j, start, end;
unsigned long flags;
#define INPUT_VLD_NUM 7
const unsigned int input_vld_reg[INPUT_VLD_NUM] = {
0xB0, 0xB4, 0xB8, 0x110, 0xEC, 0xF8, 0xFC};
// RPTR, VSTART, VEND, WPTR, VBAR, VWPTR, VRPTR
#define OUTPUT_MC_NUM 2
const unsigned int output_mc_reg[OUTPUT_MC_NUM] = {
0x224, 0x228}; // PY_ADD, PC_ADD
#define OUTPUT_UFO_MC_NUM 5
const unsigned int output_ufo_mc_reg[OUTPUT_UFO_MC_NUM] = {
0xB5C, 0xAE8, 0xAEC, 0xCE4, 0xCE8};
// YC_SEP, LEN_Y, LEN_C, LEN_Y_OFFSET, LEN_C_OFFSET
#define OUTPUT_UFO_NUM 4
const unsigned int output_ufo_reg[OUTPUT_UFO_NUM] = {
0x7C, 0x80, 0x84, 0x88}; // LEN_Y, LEN_C, BS_Y, BS_C
#define REF_MC_NUM 7
const unsigned int ref_mc_base[REF_MC_NUM] = {
0x3DC, 0xB60, 0x45C, 0xBE0, 0x4DC, 0xC60, 0xD28};
// P_L0_Y, P_L0_C, B_L0_Y, B_L0_C, B_L1_Y, B_L1_C, REF
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("hw_id %d not support !!", hw_id);
return;
}
ctx = dev->curr_dec_ctx[hw_id];
if (ctx)
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
else
fourcc = 0;
spin_lock_irqsave(&dev->dec_power_lock[hw_id], flags);
if (dev->dec_is_power_on[hw_id] == false) {
mtk_v4l2_err("hw %d power is off !!", hw_id);
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
return;
}
if (hw_id == MTK_VDEC_CORE && fourcc != V4L2_PIX_FMT_AV1)
is_ufo = (readl(ufo_addr + 0x08C) & 0x1) == 0x1;
switch (type) {
case DUMP_VDEC_IN_BUF:
for (i = 0; i < INPUT_VLD_NUM; i++) {
value = readl(vld_addr + input_vld_reg[i]);
mtk_v4l2_err("[VLD] 0x%x(%d) = 0x%lx",
input_vld_reg[i], input_vld_reg[i]/4, value);
}
break;
case DUMP_VDEC_OUT_BUF:
for (i = 0; i < OUTPUT_MC_NUM; i++) {
value = readl(mc_addr + output_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_mc_reg[i], output_mc_reg[i]/4, value);
}
if (is_ufo) {
for (i = 0; i < OUTPUT_UFO_MC_NUM; i++) {
value = readl(mc_addr + output_ufo_mc_reg[i]);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx",
output_ufo_mc_reg[i],
output_ufo_mc_reg[i]/4, value);
}
for (i = 0; i < OUTPUT_UFO_NUM; i++) {
value = readl(ufo_addr + output_ufo_reg[i]);
mtk_v4l2_err("[UFO] 0x%x(%d) = 0x%lx",
output_ufo_reg[i],
output_ufo_reg[i]/4, value);
}
}
break;
case DUMP_VDEC_REF_BUF:
for (i = 0; i < 32; i++) {
for (j = 0; j < 6; j++) {
values[j] = readl(mc_addr +
(ref_mc_base[j] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[0] + i * 4,
ref_mc_base[0]/4 + i, values[0],
ref_mc_base[1] + i * 4,
ref_mc_base[1]/4 + i, values[1],
ref_mc_base[2] + i * 4,
ref_mc_base[2]/4 + i, values[2],
ref_mc_base[3] + i * 4,
ref_mc_base[3]/4 + i, values[3],
ref_mc_base[4] + i * 4,
ref_mc_base[4]/4 + i, values[4],
ref_mc_base[5] + i * 4,
ref_mc_base[5]/4 + i, values[5]);
}
for (i = 0; i < 4; i++)
values[i] = readl(mc_addr + i * 4);
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
i * 4, i, values[0],
i * 4, i, values[1],
i * 4, i, values[2],
i * 4, i, values[3]);
for (i = 0; i < 6; i++) {
values[i] = readl(mc_addr +
(ref_mc_base[6] + i * 4));
}
mtk_v4l2_err("[MC] 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx, 0x%x(%d) = 0x%lx",
ref_mc_base[6],
ref_mc_base[6]/4 + 0, values[0],
ref_mc_base[6] + 1 * 4,
ref_mc_base[6]/4 + 1, values[1],
ref_mc_base[6] + 2 * 4,
ref_mc_base[6]/4 + 2, values[2],
ref_mc_base[6] + 3 * 4,
ref_mc_base[6]/4 + 3, values[3],
ref_mc_base[6] + 4 * 4,
ref_mc_base[6]/4 + 4, values[4],
ref_mc_base[6] + 5 * 4,
ref_mc_base[6]/4 + 5, values[5]);
break;
case DUMP_VDEC_MV_BUF:
if (hw_id != MTK_VDEC_CORE) {
mtk_v4l2_err("not support dump MV at hw_id %d",
hw_id);
break;
}
value = readl(mv_addr + 0x20C);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
0x20C, 0x20C/4, value);
switch (fourcc) {
case V4L2_PIX_FMT_H265:
start = 0;
end = 32;
break;
case V4L2_PIX_FMT_H264:
start = 96;
end = 128;
break;
case V4L2_PIX_FMT_VP9:
start = 240;
end = 241;
break;
case V4L2_PIX_FMT_AV1:
start = 353;
end = 356;
break;
default:
start = 195;
end = 198;
}
for (i = start; i < end; i++) {
value = readl(mv_addr + i * 4);
mtk_v4l2_err("[MV] 0x%x(%d) = 0x%lx",
i * 4, i, value);
}
break;
default:
mtk_v4l2_err("unknown addr type");
}
spin_unlock_irqrestore(&dev->dec_power_lock[hw_id], flags);
}
#ifdef CONFIG_MTK_IOMMU_V2
enum mtk_iommu_callback_ret_t mtk_vdec_translation_fault_callback(
int port, unsigned long mva, void *data)
{
struct mtk_vcodec_dev *dev = (struct mtk_vcodec_dev *)data;
int hw_id = MTK_VDEC_CORE;
struct mtk_vcodec_ctx *ctx;
u32 fourcc;
ctx = dev->curr_dec_ctx[hw_id];
if (ctx) {
fourcc = ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
mtk_v4l2_err("codec:0x%08x(%c%c%c%c) TF larb %d port %x mva 0x%lx",
fourcc, fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF,
port >> 5, port, mva);
} else {
mtk_v4l2_err("ctx NULL codec unknown, TF larb %d port %x mva 0x%lx",
port >> 5, port, mva);
}
switch (port) {
case M4U_PORT_L4_VDEC_VLD_EXT:
case M4U_PORT_L4_VDEC_VLD2_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
break;
case M4U_PORT_L4_VDEC_PP_EXT:
case M4U_PORT_L4_VDEC_UFO_ENC_EXT:
mtk_vdec_dump_addr_reg(dev, MTK_VDEC_CORE, DUMP_VDEC_OUT_BUF);
break;
case M4U_PORT_L4_VDEC_MC_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_REF_BUF);
break;
case M4U_PORT_L4_VDEC_AVC_MV_EXT:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_MV_BUF);
break;
default:
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_IN_BUF);
mtk_vdec_dump_addr_reg(dev, hw_id, DUMP_VDEC_OUT_BUF);
}
return MTK_IOMMU_CALLBACK_HANDLED;
}
void mtk_vdec_translation_fault_callback_setting(
struct mtk_vcodec_dev *dev)
{
int i;
for (i = M4U_PORT_L4_VDEC_MC_EXT; i <= M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT; i++) {
mtk_iommu_register_fault_callback(i,
mtk_vdec_translation_fault_callback, (void *)dev);
}
}
#endif
void mtk_prepare_vdec_dvfs(void)
{
#if DEC_DVFS
int ret;
mtk_pm_qos_add_request(&vdec_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
vdec_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &vdec_freq_steps[0],
&vdec_freq_step_size);
if (ret < 0)
pr_debug("Failed to get vdec freq steps (%d)\n", ret);
#endif
}
void mtk_unprepare_vdec_dvfs(void)
{
#if DEC_DVFS
int freq_idx = 0;
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&vdec_qos_req_f);
free_hist(&vdec_hists, 0);
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
plist_head_init(&vdec_rlist);
mm_qos_add_request(&vdec_rlist, &vdec_mc, M4U_PORT_L4_VDEC_MC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo, M4U_PORT_L4_VDEC_UFO_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pp, M4U_PORT_L4_VDEC_PP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_rd,
M4U_PORT_L4_VDEC_PRED_RD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_pred_wr,
M4U_PORT_L4_VDEC_PRED_WR_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ppwrap,
M4U_PORT_L4_VDEC_PPWRAP_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_tile, M4U_PORT_L4_VDEC_TILE_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld, M4U_PORT_L4_VDEC_VLD_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_vld2, M4U_PORT_L4_VDEC_VLD2_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_avc_mv,
M4U_PORT_L4_VDEC_AVC_MV_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_ufo_enc,
M4U_PORT_L4_VDEC_UFO_ENC_EXT);
mm_qos_add_request(&vdec_rlist, &vdec_rg_ctrl_dma,
M4U_PORT_L4_VDEC_RG_CTRL_DMA_EXT);
#endif
}
void mtk_unprepare_vdec_emi_bw(void)
{
#if DEC_EMI_BW
mm_qos_remove_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_dvfs_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *vdec_cur_job = 0;
long long op_rate_to_freq = 0;
long long pixel_tput = 0;
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = move_job_to_head(&ctx->id, &vdec_jobs);
if (ctx->dec_params.operating_rate > 0) {
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
case V4L2_PIX_FMT_H265:
case V4L2_PIX_FMT_VP9:
pixel_tput = 3840LL * 2176LL * 30LL;
break;
default:
pixel_tput = 1920LL * 1088LL * 60LL;
break;
}
op_rate_to_freq = 312LL *
ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height *
ctx->dec_params.operating_rate /
pixel_tput;
target_freq_64 = match_freq((int)op_rate_to_freq,
&vdec_freq_steps[0],
vdec_freq_step_size);
vdec_freq = target_freq_64;
if (vdec_cur_job != 0)
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
} else if (vdec_cur_job != 0) {
vdec_cur_job->start = get_time_us();
target_freq = est_freq(vdec_cur_job->handle, &vdec_jobs,
vdec_hists);
target_freq_64 = match_freq(target_freq, &vdec_freq_steps[0],
vdec_freq_step_size);
if (target_freq > 0) {
vdec_freq = target_freq_64;
vdec_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &vdec_freq_steps[0],
vdec_freq_step_size);
mtk_pm_qos_update_request(&vdec_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_dvfs_end(struct mtk_vcodec_ctx *ctx)
{
#if DEC_DVFS
int freq_idx = 0;
struct codec_job *vdec_cur_job = 0;
/* vdec dvfs */
mutex_lock(&ctx->dev->dec_dvfs_mutex);
vdec_cur_job = vdec_jobs;
if (vdec_cur_job != 0 && vdec_cur_job->handle == &ctx->id) {
vdec_cur_job->end = get_time_us();
update_hist(vdec_cur_job, &vdec_hists, 0);
vdec_jobs = vdec_jobs->next;
kfree(vdec_cur_job);
} else {
/* print error log */
pr_debug("no vdec job, probably error handling path\n");
}
freq_idx = (vdec_freq_step_size == 0) ? 0 : (vdec_freq_step_size - 1);
mtk_pm_qos_update_request(&vdec_qos_req_f, vdec_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_emi_bw_begin(struct mtk_vcodec_ctx *ctx)
{
#if DEC_EMI_BW
int b_freq_idx = 0;
int f_type = 1; /* TODO */
long emi_bw = 0;
long emi_bw_input = 0;
long emi_bw_output = 0;
int is_ufo_on = 0;
if (vdec_freq_step_size > 1)
b_freq_idx = vdec_freq_step_size - 1;
emi_bw = 8L * 1920 * 1080 * 2 * 10 * vdec_freq;
emi_bw_input = 25L * vdec_freq / STD_VDEC_FREQ;
emi_bw_output = 1920L * 1088 * 3 * 20 * 10 * vdec_freq /
2 / 3 / STD_VDEC_FREQ / 1024 / 1024;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * h264_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * h265_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP8:
emi_bw = emi_bw * vp8_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_VP9:
emi_bw = emi_bw * vp9_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H263:
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
emi_bw = emi_bw * mp24_frm_scale[f_type] / (2 * STD_VDEC_FREQ);
break;
}
is_ufo_on = (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H264 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc ==
V4L2_PIX_FMT_VP9) &&
((ctx->q_data[MTK_Q_DATA_DST].coded_width *
ctx->q_data[MTK_Q_DATA_DST].coded_height) >=
(1920 * 1080)) ? 1 : 0;
/* bits/s to MBytes/s and occupied BW */
emi_bw = emi_bw * 4 / 3 / (1024 * 1024) / 8;
emi_bw_output = emi_bw_output * 4 / 3;
emi_bw_input = emi_bw_input * 4 / 3;
if (is_ufo_on == 1) { /* UFO */
emi_bw = emi_bw * 6 / 10;
emi_bw_output = emi_bw_output * 6 / 10;
}
if (is_ufo_on == 1) { /* UFO */
mm_qos_set_request(&vdec_ufo, 10, 0, BW_COMP_DEFAULT);
mm_qos_set_request(&vdec_ufo_enc, emi_bw_output, 0,
BW_COMP_DEFAULT);
} else {
/* non-UFO */
mm_qos_set_request(&vdec_pp, emi_bw_output, 0, BW_COMP_NONE);
}
mm_qos_set_request(&vdec_mc, emi_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 1, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, emi_bw_input, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, emi_bw_input * 2, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 1, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
static void mtk_vdec_emi_bw_end(void)
{
#if DEC_EMI_BW
mm_qos_set_request(&vdec_mc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pp, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_rd, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_pred_wr, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ppwrap, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_tile, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_vld2, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_avc_mv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_ufo_enc, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&vdec_rg_ctrl_dma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&vdec_rlist);
#endif
}
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id)
{
#if DEC_DVFS
mutex_lock(&ctx->dev->dec_dvfs_mutex);
add_job(&ctx->id, &vdec_jobs);
mutex_unlock(&ctx->dev->dec_dvfs_mutex);
#endif
}
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_begin(ctx);
mtk_vdec_emi_bw_begin(ctx);
}
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id)
{
mtk_vdec_dvfs_end(ctx);
mtk_vdec_emi_bw_end();
}
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,525 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#include "mt6877/smi_port.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#endif
#include "swpm_me.h"
#if ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 249
#define STD_LUMA_BW 70
#define STD_CHROMA_BW 35
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
static u64 venc_freq_map[] = {249, 343, 458, 624};
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
static struct plist_head venc_rlist;
static struct mm_qos_request venc_rcpu;
static struct mm_qos_request venc_rec;
static struct mm_qos_request venc_bsdma;
static struct mm_qos_request venc_sv_comv;
static struct mm_qos_request venc_rd_comv;
static struct mm_qos_request venc_cur_luma;
static struct mm_qos_request venc_cur_chroma;
static struct mm_qos_request venc_ref_luma;
static struct mm_qos_request venc_ref_chroma;
#endif
#if 1
/* first scenario based version, will change to loading based */
struct temp_job {
int ctx_id;
int format;
int type;
int module;
int visible_width; /* temp usage only, will use kcy */
int visible_height; /* temp usage only, will use kcy */
int operation_rate;
long long submit;
int kcy;
struct temp_job *next;
};
static struct temp_job *temp_venc_jobs;
struct temp_job *new_job_from_info(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct temp_job *new_job = kmalloc(sizeof(struct temp_job), GFP_KERNEL);
if (new_job == 0)
return 0;
new_job->ctx_id = ctx->id;
new_job->format = ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;
new_job->type = 1; /* temp */
new_job->module = core_id;
new_job->visible_width = ctx->q_data[MTK_Q_DATA_SRC].visible_width;
new_job->visible_height = ctx->q_data[MTK_Q_DATA_SRC].visible_height;
new_job->operation_rate = 0;
new_job->submit = 0; /* use now - to be filled */
new_job->kcy = 0; /* retrieve hw counter - to be filled */
new_job->next = 0;
return new_job;
}
void free_job(struct temp_job *job)
{
if (job != 0)
kfree(job);
}
int add_to_tail(struct temp_job **job_list, struct temp_job *job)
{
int cnt = 0;
struct temp_job *tail = 0;
if (job == 0)
return -1;
tail = *job_list;
if (tail == 0) {
*job_list = job;
return 1;
}
cnt = 1;
while (tail->next != 0) {
cnt++;
tail = tail->next;
}
cnt++;
tail->next = job;
return cnt;
}
struct temp_job *remove_from_head(struct temp_job **job_list)
{
struct temp_job *head = *job_list;
if (head == 0)
return 0;
*job_list = head->next;
return head;
}
#endif
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 0;
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6877-vcodec-enc");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC = devm_clk_get(&pdev->dev, "MT_CG_VENC");
if (IS_ERR(pm->clk_MT_CG_VENC)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC);
}
#endif
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
//free_all_bw(&venc_bw);
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
smi_bus_prepare_enable(SMI_LARB7, "VENC");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
set_swpm_venc_active(true);
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
larb_port_num = SMI_LARB7_PORT_NUM;
larb_id = 7;
} else {
larb_port_num = 0;
larb_id = 0;
mtk_v4l2_err("invalid core_id %d", core_id);
time_check_end(MTK_FMT_ENC, core_id, 50);
return;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
#ifndef FPGA_PWRCLK_API_DISABLE
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
set_swpm_venc_active(false);
clk_disable_unprepare(pm->clk_MT_CG_VENC);
smi_bus_disable_unprepare(SMI_LARB7, "VENC");
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
temp_venc_jobs = 0;
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
/* free_hist(&venc_hists, 0); */
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist);
mm_qos_add_request(&venc_rlist, &venc_rcpu, M4U_PORT_L7_VENC_RCPU);
mm_qos_add_request(&venc_rlist, &venc_rec, M4U_PORT_L7_VENC_REC);
mm_qos_add_request(&venc_rlist, &venc_bsdma, M4U_PORT_L7_VENC_BSDMA);
mm_qos_add_request(&venc_rlist, &venc_sv_comv,
M4U_PORT_L7_VENC_SV_COMV);
mm_qos_add_request(&venc_rlist, &venc_rd_comv,
M4U_PORT_L7_VENC_RD_COMV);
mm_qos_add_request(&venc_rlist, &venc_cur_luma,
M4U_PORT_L7_VENC_CUR_LUMA);
mm_qos_add_request(&venc_rlist, &venc_cur_chroma,
M4U_PORT_L7_VENC_CUR_CHROMA);
mm_qos_add_request(&venc_rlist, &venc_ref_luma,
M4U_PORT_L7_VENC_REF_LUMA);
mm_qos_add_request(&venc_rlist, &venc_ref_chroma,
M4U_PORT_L7_VENC_REF_CHROMA);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist);
#endif
}
void mtk_venc_dvfs_begin(struct temp_job **job_list)
{
#if ENC_DVFS
struct temp_job *job = *job_list;
int area = 0;
int idx = 0;
if (job == 0)
return;
area = job->visible_width * job->visible_height;
if (area >= 3840 * 2160)
idx = 2;
else if (area >= 1920 * 1080)
if (job->operation_rate > 30)
idx = 2;
else
idx = 0;
else
idx = 0;
if (job->operation_rate >= 120)
idx = 2;
if (job->format == V4L2_PIX_FMT_HEIF)
idx = 3;
venc_freq = venc_freq_map[idx];
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq);
#endif
}
void mtk_venc_dvfs_end(struct temp_job *job)
{
#if ENC_DVFS
if (job == 0)
return;
venc_freq = venc_freq_map[0];
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq);
#endif
}
void mtk_venc_emi_bw_begin(struct temp_job **jobs)
{
#if ENC_EMI_BW
struct temp_job *job = 0;
int id = 0;
int boost_perc = 0;
int rcpu_bw = 5 * 4 / 3;
int rec_bw = 0;
int bsdma_bw = 20 * 4 / 3;
int sv_comv_bw = 4 * 4 / 3;
int rd_comv_bw = 16 * 4 / 3;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
if (*jobs == 0)
return;
job = *jobs;
id = job->module;
if (job->operation_rate > 60)
boost_perc = 100;
if (job->format == V4L2_PIX_FMT_H265 ||
job->format == V4L2_PIX_FMT_HEIF ||
(job->format == V4L2_PIX_FMT_H264 &&
job->visible_width >= 2160)) {
boost_perc = 150;
}
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc) * 4 /
STD_VENC_FREQ / 100 / 3;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc) * 4 /
STD_VENC_FREQ / 100 / 3;
/* BW in venc_freq */
rec_bw = cur_luma_bw + cur_chroma_bw;
ref_luma_bw = cur_luma_bw * 4;
ref_chroma_bw = cur_chroma_bw * 4;
if (1) { /* UFO */
ref_chroma_bw += ref_luma_bw;
ref_luma_bw = 0;
}
mm_qos_set_request(&venc_rcpu, rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, cur_chroma_bw, 0, BW_COMP_NONE);
if (1) { /* UFO */
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_VENC);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_VENC);
} else {
mm_qos_set_request(&venc_rec, rec_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, ref_luma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, ref_chroma_bw, 0,
BW_COMP_NONE);
}
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_emi_bw_end(struct temp_job *job)
{
#if ENC_EMI_BW
int core_id;
if (job == 0)
return;
core_id = job->module;
mm_qos_set_request(&venc_rcpu, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma, 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma, 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
struct temp_job *mtk_venc_queue_job(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
int cnt = 0;
struct temp_job *job = new_job_from_info(ctx, core_id);
if (job != 0)
cnt = add_to_tail(&temp_venc_jobs, job);
return job;
}
struct temp_job *mtk_venc_dequeue_job(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
struct temp_job *job = remove_from_head(&temp_venc_jobs);
if (job != 0)
return job;
/* print error message */
return 0;
}
/* Total job count after this one is inserted */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
/* mutex_lock(&ctx->dev->enc_dvfs_mutex); */
struct temp_job *job = 0;
int frame_rate = 0;
job = mtk_venc_queue_job(ctx, core_id, job_cnt);
frame_rate = ctx->enc_params.operationrate;
if (frame_rate == 0) {
frame_rate = ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom;
}
if (job != NULL)
job->operation_rate = frame_rate;
if (job_cnt == 0) {
// Adjust dvfs immediately
mtk_venc_dvfs_begin(&temp_venc_jobs);
mtk_venc_emi_bw_begin(&temp_venc_jobs);
}
/* mutex_unlock(&ctx->dev_enc_dvfs_mutex); */
}
/* Remaining job count after this one is done */
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
struct temp_job *job = mtk_venc_dequeue_job(ctx, core_id, job_cnt);
mtk_venc_dvfs_end(job);
mtk_venc_emi_bw_end(job);
free_job(job);
if (job_cnt > 1) {
mtk_venc_dvfs_begin(&temp_venc_jobs);
mtk_venc_emi_bw_begin(&temp_venc_jobs);
}
}
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,290 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
#include "smi_public.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
core_id = 0;
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
ctx->id, ctx->type, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
//smi_debug_bus_hang_detect(0, "VCODEC");
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
ctx->id, ctx->type, command, ctx->int_cond[core_id],
ctx->int_type);
status = -1;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx)
{
ctx->int_cond[0] = 1;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, 0);
if (ctx->dec_params.svp_mode) {
mtk_v4l2_debug(4, "svp_mode %d don't handle",
ctx->dec_params.svp_mode);
return IRQ_HANDLED;
}
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[0]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (!ctx)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->dec_irq[0] = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->dec_irq[0],
mtk_vcodec_dec_irq_handler,
IRQF_NO_THREAD | IRQF_SHARED | IRQF_PROBE_SHARED, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq %d (%d)",
dev->dec_irq[0],
ret);
return -1;
}
disable_irq(dev->dec_irq[0]);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
struct mtk_vcodec_ctx *curr_ctx = ctx;
if (curr_ctx->type == MTK_INST_ENCODER)
mtk_vcodec_enc_timeout_dump(ctx);
else if (curr_ctx->type == MTK_INST_DECODER)
mtk_vcodec_dec_timeout_dump(ctx);
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
void mtk_vcodec_enc_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0, j = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define REG1_COUNT 13
#define REG2_COUNT 46
unsigned int Reg_1[REG1_COUNT] = {
0x14, 0xEC, 0x1C0, 0x1168, 0x11C0,
0x11C4, 0xF4, 0x5C, 0x60, 0x130,
0x24, 0x114C, 0x1164};
unsigned int Reg_2[REG2_COUNT] = {
0xEC, 0x200, 0x204, 0x208, 0x20C,
0x210, 0x214, 0x218, 0x21C,
0x220, 0x224, 0x228, 0x22C,
0x230, 0x234, 0x238, 0x23C,
0x240, 0x244, 0x248, 0x24C,
0x250, 0x254, 0x258, 0x25C,
0x260, 0x264, 0x268, 0x26C,
0x270, 0x274, 0x278, 0x27C,
0x280,
0x22C, 0x230, 0xF4, 0x1168,
0x11C0, 0x11C4, 0x1030, 0x240,
0x248, 0x250, 0x130, 0x140};
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump venc for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG1_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_1[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_1[i], value);
}
}
writel(1, dev->enc_reg_base[0] + 0xEC);
writel(0, dev->enc_reg_base[0] + 0xF4);
for (j = 0; j < MTK_VENC_CORE_1; j++) {
for (i = 0; i < REG2_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_2[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_2[i], value);
}
}
}
void mtk_vcodec_dec_timeout_dump(void *ctx)
{
}
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
void mtk_vcodec_enc_timeout_dump(void *ctx);
void mtk_vcodec_dec_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#define MTK_PLATFORM_STR "platform:mt6877"
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_VDEC; /* VDEC */
struct clk *clk_MT_CG_VENC; /* VENC */
struct clk *clk_MT_SCP_SYS_VDE; /* SCP_SYS_VDE */
struct clk *clk_MT_SCP_SYS_VEN; /* SCP_SYS_VEN */
struct clk *clk_MT_SCP_SYS_DIS; /* SCP_SYS_DIS */
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_UFO,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,770 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT 33333
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* free_hist_by_handle = free the history specified by the handle
*
* Returns 0 if history is found and freed
* -1 if there is nothing to free
*/
int free_hist_by_handle(void *handle, struct codec_history **head)
{
struct codec_history *target = 0;
target = find_hist(handle, *head);
if (target == 0)
return -1;
return free_hist_(head, target);
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
long long tot_time = 0;
int i = 0;
if (hist == 0)
return 0;
/* Special SW/HW separation case for single 60FPS */
if (hist->sw_time[0] != 0) {
tot_time = 0;
for (i = 0; i < MAX_HISTORY; i++) {
/* Sum SW time*/
tot_time += hist->sw_time[i];
}
tot_time = tot_time / MAX_HISTORY;
return (hist->tot_kcy / hist->cur_cnt) +
((tot_time * 364LL) / 1000LL);
}
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long exec_dur;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (head->next == 0 &&
((hist->submit_interval < 17000LL &&
hist->submit_interval > 16000LL) ||
(hist->submit_interval < 34000LL &&
hist->submit_interval > 33000LL))) {
/* single instance 4K30 / 60fps */
deadline = now_us + hist->submit_interval;
}
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
exec_dur = deadline - now_us;
exec_dur = (exec_dur > (MAX_SUBMIT * 2)) ?
(MAX_SUBMIT * 2) : exec_dur;
new_mhz = div_64((*kcy) * 1000LL, exec_dur);
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
if (*min_mhz == 0)
*min_mhz = 1;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away or instance count change, restart */
if ((hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP)
|| (job->hw_kcy > 0 && hist->sw_time[prev_idx] == 0)
|| (job->hw_kcy == 0 && hist->sw_time[prev_idx] > 0)) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->sw_time, 0, sizeof(long long)*MAX_HISTORY);
if (job->hw_kcy > 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[0] =
(int)div_64(job->mhz * (job->end - job->start),
1000LL);
}
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
if (job->hw_kcy > 0) {
hist->sw_time[0] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than
* requested
*/
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st0 e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[0]);
#endif
}
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
if (job->hw_kcy != 0) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
job->hw_kcy;
} else {
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
}
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
if (job->hw_kcy != 0) {
hist->kcy[hist_idx] = job->hw_kcy;
} else {
hist->kcy[hist_idx] =
(int)div_64(job->mhz * (job->end - job->start), 1000LL);
}
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
if (job->hw_kcy != 0) {
hist->sw_time[hist_idx] = job->end - job->start -
(job->hw_kcy * 1000 / job->mhz);
if (hist->sw_time[hist_idx] < 500) {
/* If HW is running at higher clock than requested */
hist->sw_time[hist_idx] = 1300;
}
#if SHOW_ALGO_INFO
pr_info("st e %lld,s %lld,mhz %d,hw %lld,sw %lld\n",
job->end, job->start, job->mhz,
(job->hw_kcy * 1000 / job->mhz),
hist->sw_time[hist_idx]);
#endif
} else {
hist->sw_time[hist_idx] = 0;
}
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] >= target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long sw_time[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int hw_kcy;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
int free_hist_by_handle(void *handle, struct codec_history **head);
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_DEC_PM_H_
#define _MTK_VCODEC_DEC_PM_H_
#include "mtk_vcodec_drv.h"
#define DEC_DVFS 1
#define DEC_EMI_BW 1
#define DEC_TF_CALLBACK 1
void mtk_dec_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm, int hw_id);
void mtk_vdec_hw_break(struct mtk_vcodec_dev *dev, int hw_id);
void mtk_vdec_translation_fault_callback_setting(struct mtk_vcodec_dev *dev);
void mtk_prepare_vdec_dvfs(void);
void mtk_unprepare_vdec_dvfs(void);
void mtk_prepare_vdec_emi_bw(void);
void mtk_unprepare_vdec_emi_bw(void);
void mtk_vdec_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
void mtk_vdec_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int hw_id);
int mtk_vdec_ion_config_buff(struct dma_buf *dmabuf);
#endif /* _MTK_VCODEC_DEC_PM_H_ */
@@ -0,0 +1,888 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/clk.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <linux/delay.h>
//#include <soc/mediatek/smi.h>
#include "mtk_vcodec_enc_pm.h"
#include "mtk_vcodec_util.h"
#include "mtk_vcu.h"
#include "smi_public.h"
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#include "smi_port.h"
#endif
#define USE_GCE 1
#ifdef ENC_DVFS
#include <linux/pm_qos.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "vcodec_dvfs.h"
#define STD_VENC_FREQ 273
#define STD_LUMA_BW 100
#define STD_CHROMA_BW 50
static struct mtk_pm_qos_request venc_qos_req_f;
static u64 venc_freq;
static u32 venc_freq_step_size;
static u64 venc_freq_steps[MAX_FREQ_STEP];
/*
static struct codec_history *venc_hists;
static struct codec_job *venc_jobs;
*/
static u64 venc_req_freq[2];
/* 1080p60, 4k30, 4k60, 1 core 4k60*/
static u64 venc_freq_map[] = {273, 312, 416, 624};
#endif
#if ENC_EMI_BW
#include <mtk_smi.h>
#include <mtk_qos_bound.h>
#include "vcodec_bw.h"
#define CORE_NUM 2
/*
static unsigned int gVENCFrmTRAVC[3] = {6, 12, 6};
static unsigned int gVENCFrmTRHEVC[3] = {6, 12, 6};
static long long venc_start_time;
static struct vcodec_bw *venc_bw;
*/
static struct plist_head venc_rlist[CORE_NUM];
static struct mm_qos_request venc_rcpu[CORE_NUM];
static struct mm_qos_request venc_rec[CORE_NUM];
static struct mm_qos_request venc_bsdma[CORE_NUM];
static struct mm_qos_request venc_sub_w_luma[CORE_NUM];
static struct mm_qos_request venc_sv_comv[CORE_NUM];
static struct mm_qos_request venc_rd_comv[CORE_NUM];
static struct mm_qos_request venc_nbm_rdma[CORE_NUM];
static struct mm_qos_request venc_nbm_rdma_lite[CORE_NUM];
static struct mm_qos_request venc_fcs_nbm_rdma[CORE_NUM];
static struct mm_qos_request venc_nbm_wdma[CORE_NUM];
static struct mm_qos_request venc_nbm_wdma_lite[CORE_NUM];
static struct mm_qos_request venc_fcs_nbm_wdma[CORE_NUM];
static struct mm_qos_request venc_sub_r_luma[CORE_NUM];
static struct mm_qos_request venc_cur_luma[CORE_NUM];
static struct mm_qos_request venc_cur_chroma[CORE_NUM];
static struct mm_qos_request venc_ref_luma[CORE_NUM];
static struct mm_qos_request venc_ref_chroma[CORE_NUM];
#endif
#if 1
/* first scenario based version, will change to loading based */
struct temp_job {
int ctx_id;
int format;
int type;
int module;
int visible_width; /* temp usage only, will use kcy */
int visible_height; /* temp usage only, will use kcy */
int operation_rate;
int bitratemode;
long long submit;
int kcy;
int cur_inst_cnt;
struct temp_job *next;
};
static struct temp_job *temp_venc_jobs[CORE_NUM];
struct temp_job *new_job_from_info(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct mtk_vcodec_dev *dev;
struct temp_job *new_job = kmalloc(sizeof(struct temp_job), GFP_KERNEL);
if (new_job == 0)
return 0;
dev = ctx->dev;
if (dev == 0) {
if (new_job != 0)
kfree(new_job);
return 0;
}
new_job->ctx_id = ctx->id;
new_job->format = ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;
new_job->type = 1; /* temp */
new_job->module = core_id;
new_job->visible_width = ctx->q_data[MTK_Q_DATA_SRC].visible_width;
new_job->visible_height = ctx->q_data[MTK_Q_DATA_SRC].visible_height;
new_job->operation_rate = 0;
new_job->submit = 0; /* use now - to be filled */
new_job->kcy = 0; /* retrieve hw counter - to be filled */
new_job->cur_inst_cnt = dev->enc_cnt;
new_job->next = 0;
return new_job;
}
void free_job(struct temp_job *job)
{
if (job != 0)
kfree(job);
}
int add_to_tail(struct temp_job **job_list, struct temp_job *job)
{
int cnt = 0;
struct temp_job *tail = 0;
if (job == 0)
return -1;
tail = *job_list;
if (tail == 0) {
*job_list = job;
return 1;
}
cnt = 1;
while (tail->next != 0) {
cnt++;
tail = tail->next;
}
cnt++;
tail->next = job;
return cnt;
}
struct temp_job *remove_from_head(struct temp_job **job_list)
{
struct temp_job *head = *job_list;
if (head == 0)
return 0;
*job_list = head->next;
return head;
}
#endif
static struct ion_client *ion_venc_client;
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
ctx->async_mode = 1;
#ifdef CONFIG_MTK_SLBC
ctx->sram_data.uid = UID_MM_VENC;
ctx->sram_data.type = TP_BUFFER;
ctx->sram_data.size = 0;
ctx->sram_data.flag = FG_POWER;
if (slbc_request(&ctx->sram_data) >= 0)
ctx->use_slbc = 1;
else
ctx->use_slbc = 0;
pr_debug("slbc_request %d, %p\n", &ctx->sram_data, ctx->use_slbc);
#endif
}
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
int ret = 0;
#ifndef FPGA_PWRCLK_API_DISABLE
struct device_node *node;
struct platform_device *pdev;
struct device *dev;
struct mtk_vcodec_pm *pm;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->mtkdev = mtkdev;
pm->dev = &pdev->dev;
dev = &pdev->dev;
pm->chip_node = of_find_compatible_node(NULL,
NULL, "mediatek,mt6885-vcodec-enc");
node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
if (!node) {
mtk_v4l2_err("no mediatek,larb found");
return -1;
}
pdev = of_find_device_by_node(node);
if (!pdev) {
mtk_v4l2_err("no mediatek,larb device found");
return -1;
}
pm->larbvenc = &pdev->dev;
pdev = mtkdev->plat_dev;
pm->dev = &pdev->dev;
pm->clk_MT_CG_VENC0 = devm_clk_get(&pdev->dev, "MT_CG_VENC0");
if (IS_ERR(pm->clk_MT_CG_VENC0)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC0\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC0);
}
pm->clk_MT_CG_VENC1 = devm_clk_get(&pdev->dev, "MT_CG_VENC1");
if (IS_ERR(pm->clk_MT_CG_VENC1)) {
mtk_v4l2_err("[VCODEC][ERROR] Unable to devm_clk_get MT_CG_VENC1\n");
ret = PTR_ERR(pm->clk_MT_CG_VENC1);
}
#endif
ion_venc_client = NULL;
return ret;
}
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
{
#if ENC_EMI_BW
/* do nothing */
#endif
}
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx)
{
#ifdef CONFIG_MTK_SLBC
if (ctx->use_slbc == 1) {
pr_debug("slbc_release, %p\n", &ctx->sram_data);
slbc_release(&ctx->sram_data);
}
#endif
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
int ret;
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_port_num, larb_id;
struct M4U_PORT_STRUCT port;
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0 ||
core_id == MTK_VENC_CORE_1) {
smi_bus_prepare_enable(SMI_LARB7, "VENC0");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC0);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
smi_bus_prepare_enable(SMI_LARB8, "VENC1");
ret = clk_prepare_enable(pm->clk_MT_CG_VENC1);
if (ret)
mtk_v4l2_err("clk_prepare_enable CG_VENC fail %d", ret);
} else {
mtk_v4l2_err("invalid core_id %d", core_id);
time_check_end(MTK_FMT_ENC, core_id, 50);
return;
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
#ifdef CONFIG_MTK_SLBC
if (ctx->use_slbc == 1) {
time_check_start(MTK_FMT_ENC, core_id);
ret = slbc_power_on(&ctx->sram_data);
time_check_end(MTK_FMT_ENC, core_id, 50);
}
#endif
#ifdef CONFIG_MTK_PSEUDO_M4U
time_check_start(MTK_FMT_ENC, core_id);
if (core_id == MTK_VENC_CORE_0) {
larb_port_num = SMI_LARB7_PORT_NUM;
larb_id = 7;
} else if (core_id == MTK_VENC_CORE_1) {
larb_port_num = SMI_LARB8_PORT_NUM;
larb_id = 8;
}
//enable 34bits port configs & sram settings
for (i = 0; i < larb_port_num; i++) {
if (i == 5 || i == 6 || i == 13 ||
i == 14 || i == 21 || i == 22) {
#if IS_ENABLED(CONFIG_MTK_SMI_EXT)
ret = smi_sysram_enable(MTK_M4U_ID(larb_id, i), true, "LARB_VENC");
if (ret)
mtk_v4l2_err("%#x is not ready err: %#x\n", i, ret);
#else
smi_sysram_enable(MTK_M4U_ID(larb_id, i), true, "LARB_VENC");
#endif
} else {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
}
}
time_check_end(MTK_FMT_ENC, core_id, 50);
#endif
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id)
{
struct mtk_vcodec_pm *pm = &ctx->dev->pm;
#ifdef CONFIG_MTK_SLBC
if (ctx->use_slbc == 1)
slbc_power_off(&ctx->sram_data);
#endif
#ifndef FPGA_PWRCLK_API_DISABLE
if (core_id == MTK_VENC_CORE_0 ||
core_id == MTK_VENC_CORE_1) {
clk_disable_unprepare(pm->clk_MT_CG_VENC0);
smi_bus_disable_unprepare(SMI_LARB7, "VENC0");
clk_disable_unprepare(pm->clk_MT_CG_VENC1);
smi_bus_disable_unprepare(SMI_LARB8, "VENC1");
} else
mtk_v4l2_err("invalid core_id %d", core_id);
#endif
}
void mtk_prepare_venc_dvfs(void)
{
#if ENC_DVFS
int ret;
int i;
mtk_pm_qos_add_request(&venc_qos_req_f, PM_QOS_VENC_FREQ,
PM_QOS_DEFAULT_VALUE);
venc_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VENC_FREQ, &venc_freq_steps[0],
&venc_freq_step_size);
if (ret < 0)
pr_debug("Failed to get venc freq steps (%d)\n", ret);
for (i = 0; i < CORE_NUM; i++)
temp_venc_jobs[i] = 0;
#endif
}
void mtk_unprepare_venc_dvfs(void)
{
#if ENC_DVFS
int freq_idx = 0;
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&venc_qos_req_f);
/* free_hist(&venc_hists, 0); */
/* TODO: jobs error handle */
#endif
}
void mtk_prepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
plist_head_init(&venc_rlist[0]);
mm_qos_add_request(&venc_rlist[0], &venc_rcpu[0],
M4U_PORT_L7_VENC_RCPU_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_rec[0],
M4U_PORT_L7_VENC_REC_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_bsdma[0],
M4U_PORT_L7_VENC_BSDMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_sub_w_luma[0],
M4U_PORT_L7_VENC_SUB_W_LUMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_sv_comv[0],
M4U_PORT_L7_VENC_SV_COMV_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_rd_comv[0],
M4U_PORT_L7_VENC_RD_COMV_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_nbm_rdma[0],
M4U_PORT_L7_VENC_NBM_RDMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_nbm_rdma_lite[0],
M4U_PORT_L7_VENC_NBM_RDMA_LITE_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_fcs_nbm_rdma[0],
M4U_PORT_L7_VENC_FCS_NBM_RDMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_nbm_wdma[0],
M4U_PORT_L7_VENC_NBM_WDMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_nbm_wdma_lite[0],
M4U_PORT_L7_VENC_NBM_WDMA_LITE_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_fcs_nbm_wdma[0],
M4U_PORT_L7_VENC_FCS_NBM_WDMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_sub_r_luma[0],
M4U_PORT_L7_VENC_SUB_R_LUMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_cur_luma[0],
M4U_PORT_L7_VENC_CUR_LUMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_cur_chroma[0],
M4U_PORT_L7_VENC_CUR_CHROMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_ref_luma[0],
M4U_PORT_L7_VENC_REF_LUMA_DISP);
mm_qos_add_request(&venc_rlist[0], &venc_ref_chroma[0],
M4U_PORT_L7_VENC_REF_CHROMA_DISP);
plist_head_init(&venc_rlist[1]);
mm_qos_add_request(&venc_rlist[1], &venc_rcpu[1],
M4U_PORT_L8_VENC_RCPU_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_rec[1],
M4U_PORT_L8_VENC_REC_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_bsdma[1],
M4U_PORT_L8_VENC_BSDMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_sub_w_luma[1],
M4U_PORT_L8_VENC_SUB_W_LUMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_sv_comv[1],
M4U_PORT_L8_VENC_SV_COMV_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_rd_comv[1],
M4U_PORT_L8_VENC_RD_COMV_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_nbm_rdma[1],
M4U_PORT_L8_VENC_NBM_RDMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_nbm_rdma_lite[1],
M4U_PORT_L8_VENC_NBM_RDMA_LITE_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_fcs_nbm_rdma[1],
M4U_PORT_L8_VENC_FCS_NBM_RDMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_nbm_wdma[1],
M4U_PORT_L8_VENC_NBM_WDMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_nbm_wdma_lite[1],
M4U_PORT_L8_VENC_NBM_WDMA_LITE_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_fcs_nbm_wdma[1],
M4U_PORT_L8_VENC_FCS_NBM_WDMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_sub_r_luma[1],
M4U_PORT_L8_VENC_SUB_R_LUMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_cur_luma[1],
M4U_PORT_L8_VENC_CUR_LUMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_cur_chroma[1],
M4U_PORT_L8_VENC_CUR_CHROMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_ref_luma[1],
M4U_PORT_L8_VENC_REF_LUMA_MDP);
mm_qos_add_request(&venc_rlist[1], &venc_ref_chroma[1],
M4U_PORT_L8_VENC_REF_CHROMA_MDP);
#endif
}
void mtk_unprepare_venc_emi_bw(void)
{
#if ENC_EMI_BW
mm_qos_remove_all_request(&venc_rlist[0]);
mm_qos_remove_all_request(&venc_rlist[1]);
#endif
}
void mtk_venc_dvfs_begin(struct temp_job **job_list)
{
#if 0
int target_freq = 0;
u64 target_freq_64 = 0;
struct codec_job *venc_cur_job = 0;
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = move_job_to_head(&ctx->id, &venc_jobs);
if (venc_cur_job != 0) {
venc_cur_job->start = get_time_us();
target_freq = est_freq(venc_cur_job->handle, &venc_jobs,
venc_hists);
target_freq_64 = match_freq(target_freq, &venc_freq_steps[0],
venc_freq_step_size);
if (target_freq > 0) {
venc_freq = target_freq;
if (venc_freq > target_freq_64)
venc_freq = target_freq_64;
venc_cur_job->mhz = (int)target_freq_64;
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
} else {
target_freq_64 = match_freq(DEFAULT_MHZ, &venc_freq_steps[0],
venc_freq_step_size);
mtk_pm_qos_update_request(&venc_qos_req_f, target_freq_64);
}
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
#if ENC_DVFS
struct temp_job *job = *job_list;
int area = 0;
int idx = 0;
if (job == 0)
return;
area = job->visible_width * job->visible_height;
if (area >= 3840 * 2160) {
if (job->operation_rate > 30)
idx = 2;
else
idx = 0;
} else if (area >= 1920 * 1080) {
if (job->operation_rate > 30) {
if (job->format == V4L2_PIX_FMT_H265)
idx = 1;
else /* H.264 */
idx = 2;
} else {
if (job->bitratemode == 1) /* CBR */
idx = 1;
else
idx = 0;
}
} else {
idx = 0;
}
if (job->operation_rate > 240 ||
(area >= 1920 * 1080 && job->operation_rate == 240) ||
job->cur_inst_cnt > 2)
idx = 2;
else if (job->operation_rate >= 120)
idx = 0;
if (job->format == V4L2_PIX_FMT_HEIF)
idx = 3;
mtk_v4l2_debug(2, "[Debug] freq idx %d (%d, %d, %d)",
idx, area, job->operation_rate, job->bitratemode);
venc_req_freq[job->module] = venc_freq_map[idx];
venc_freq = venc_req_freq[0];
if (venc_freq < venc_req_freq[1])
venc_freq = venc_req_freq[1];
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq);
#endif
}
void mtk_venc_dvfs_end(struct temp_job *job)
{
#if 0
int freq_idx = 0;
long long interval = 0;
struct codec_job *venc_cur_job = 0;
/* venc dvfs */
mutex_lock(&ctx->dev->enc_dvfs_mutex);
venc_cur_job = venc_jobs;
if (venc_cur_job != 0 && (venc_cur_job->handle == &ctx->id)) {
venc_cur_job->end = get_time_us();
if (ctx->async_mode == 0) {
update_hist(venc_cur_job, &venc_hists, 0);
} else {
/* Set allowed time for slowmotion 4 buffer pack */
interval = (long long)(1000 * 4 /
(int)ctx->enc_params.operationrate);
update_hist(venc_cur_job, &venc_hists, interval*1000);
}
venc_jobs = venc_jobs->next;
kfree(venc_cur_job);
} else {
/* print error log */
pr_debug("no job at venc_dvfs_end, reset freq only");
}
freq_idx = (venc_freq_step_size == 0) ? 0 : (venc_freq_step_size - 1);
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq_steps[freq_idx]);
mutex_unlock(&ctx->dev->enc_dvfs_mutex);
#endif
#if ENC_DVFS
if (job == 0)
return;
venc_req_freq[job->module] = 0;
venc_freq = venc_req_freq[0];
if (venc_freq < venc_req_freq[1])
venc_freq = venc_req_freq[1];
mtk_pm_qos_update_request(&venc_qos_req_f, venc_freq);
#endif
}
void mtk_venc_emi_bw_begin(struct temp_job **jobs)
{
#if 0
int f_type = 1; /* TODO */
int boost_perc = 0;
long emi_bw = 0;
if (ctx->async_mode == 1)
boost_perc = 100;
if (ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H265 ||
ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_HEIF)
boost_perc = 150;
emi_bw = 8L * 1920 * 1080 * 3 * 10 * venc_freq;
switch (ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
emi_bw = emi_bw * gVENCFrmTRAVC[f_type] *
(100 + boost_perc) / (2 * STD_VENC_FREQ) / 100;
break;
case V4L2_PIX_FMT_H265:
emi_bw = emi_bw * gVENCFrmTRHEVC[f_type] *
(100 + boost_perc) / (2 * STD_VENC_FREQ) / 100;
break;
default:
emi_bw = 0;
pr_debug("Unsupported encoder type for BW");
}
/* transaction bytes to occupied BW */
emi_bw = emi_bw * 4 / 3;
/* bits/s to mbytes/s */
emi_bw = emi_bw / (1024 * 1024) / 8;
mtk_pm_qos_update_request(&venc_qos_req_bw, (int)emi_bw);
#endif
#if ENC_EMI_BW
struct temp_job *job = 0;
int id = 0;
int boost_perc = 0;
int rcpu_bw = 5 * 4 / 3;
int rec_bw = 0;
int bsdma_bw = 20 * 4 / 3;
int sv_comv_bw = 4 * 4 / 3;
int rd_comv_bw = 16 * 4 / 3;
int cur_luma_bw = 0;
int cur_chroma_bw = 0;
int ref_luma_bw = 0;
int ref_chroma_bw = 0;
/* New Ports */
int sub_w_luma_bw = 0;
int nbm_rdma_bw = 0;
int nbm_rdma_lite_bw = 0;
int fcs_nbm_rdma_bw = 0;
int nbm_wdma_bw = 0;
int nbm_wdma_lite_bw = 0;
int fcs_nbm_wdma_bw = 0;
int sub_r_luma_bw = 0;
if (*jobs == 0)
return;
job = *jobs;
id = job->module;
if (job->operation_rate > 60)
boost_perc = 100;
if (job->format == V4L2_PIX_FMT_H265 ||
job->format == V4L2_PIX_FMT_HEIF ||
(job->format == V4L2_PIX_FMT_H264 &&
job->visible_width >= 2160)) {
boost_perc = 150;
}
cur_luma_bw = STD_LUMA_BW * venc_freq * (100 + boost_perc) * 4/
STD_VENC_FREQ / 100 / 3;
cur_chroma_bw = STD_CHROMA_BW * venc_freq * (100 + boost_perc) * 4 /
STD_VENC_FREQ / 100 / 3;
rec_bw = cur_luma_bw + cur_chroma_bw;
if (0) { /* no UFO */
ref_luma_bw = cur_luma_bw * 1;
ref_chroma_bw = cur_chroma_bw * 1;
} else {
ref_luma_bw = 0;
ref_chroma_bw = (cur_luma_bw * 1) + (cur_chroma_bw * 1);
}
mm_qos_set_request(&venc_rcpu[id], rcpu_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec[id], rec_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_bsdma[id], bsdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sub_w_luma[id], sub_w_luma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv[id], sv_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv[id], rd_comv_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_rdma[id], nbm_rdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_rdma_lite[id], nbm_rdma_lite_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_fcs_nbm_rdma[id], fcs_nbm_rdma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_wdma[id], nbm_wdma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_wdma_lite[id], nbm_wdma_lite_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_fcs_nbm_wdma[id], fcs_nbm_wdma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_sub_r_luma[id], sub_r_luma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma[id], cur_luma_bw, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma[id], cur_chroma_bw, 0,
BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma[id], ref_luma_bw, 0, BW_COMP_VENC);
mm_qos_set_request(&venc_ref_chroma[id], ref_chroma_bw, 0,
BW_COMP_VENC);
mm_qos_update_all_request(&venc_rlist[id]);
#endif
}
void mtk_venc_emi_bw_end(struct temp_job *job)
{
#if ENC_EMI_BW
int core_id;
if (job == 0)
return;
core_id = job->module;
mm_qos_set_request(&venc_rcpu[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rec[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_bsdma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sub_w_luma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sv_comv[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_rd_comv[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_rdma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_rdma_lite[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_fcs_nbm_rdma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_wdma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_nbm_wdma_lite[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_fcs_nbm_wdma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_sub_r_luma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_luma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_cur_chroma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_luma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&venc_ref_chroma[core_id], 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&venc_rlist[core_id]);
#endif
}
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id)
{
}
struct temp_job *mtk_venc_queue_job(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
int cnt = 0;
struct temp_job *job = new_job_from_info(ctx, core_id);
if (job != 0)
cnt = add_to_tail(&temp_venc_jobs[core_id], job);
return job;
}
struct temp_job *mtk_venc_dequeue_job(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
struct temp_job *job = remove_from_head(&temp_venc_jobs[core_id]);
if (job != 0)
return job;
/* print error message */
return 0;
}
/* Total job count after this one is inserted */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
/* mutex_lock(&ctx->dev->enc_dvfs_mutex); */
struct temp_job *job = 0;
int frame_rate = 0;
job = mtk_venc_queue_job(ctx, core_id, job_cnt);
frame_rate = ctx->enc_params.operationrate;
if (frame_rate == 0) {
frame_rate = ctx->enc_params.framerate_num /
ctx->enc_params.framerate_denom;
}
if (job != NULL) {
job->operation_rate = frame_rate;
job->bitratemode = ctx->enc_params.bitratemode;
}
if (job_cnt == 0) {
// Adjust dvfs immediately
mtk_venc_dvfs_begin(&temp_venc_jobs[core_id]);
mtk_venc_emi_bw_begin(&temp_venc_jobs[core_id]);
}
/* mutex_unlock(&ctx->dev_enc_dvfs_mutex); */
}
/* Remaining job count after this one is done */
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt)
{
struct temp_job *job = mtk_venc_dequeue_job(ctx, core_id, job_cnt);
mtk_venc_dvfs_end(job);
mtk_venc_emi_bw_end(job);
free_job(job);
if (job_cnt > 1) {
mtk_venc_dvfs_begin(&temp_venc_jobs[core_id]);
mtk_venc_emi_bw_begin(&temp_venc_jobs[core_id]);
}
}
int mtk_venc_ion_config_buff(struct dma_buf *dmabuf)
{
/* for dma-buf using ion buffer, ion will check portid in dts
* So, don't need to config buffer at user side, but remember
* set iommus attribute in dts file.
*/
#if 0
struct ion_handle *handle = NULL;
struct ion_mm_data mm_data;
int count = 0;
mtk_v4l2_debug(4, "%p", dmabuf);
if (!ion_venc_client)
ion_venc_client = ion_client_create(g_ion_device, "venc");
handle = ion_import_dma_buf(ion_venc_client, dmabuf);
if (IS_ERR(handle)) {
mtk_v4l2_err("import ion handle failed!\n");
return -1;
}
mm_data.mm_cmd = ION_MM_CONFIG_BUFFER;
mm_data.config_buffer_param.kernel_handle = handle;
mm_data.config_buffer_param.module_id = M4U_PORT_L7_VENC_RD_COMV_DISP;
mm_data.config_buffer_param.security = 0;
mm_data.config_buffer_param.coherent = 0;
while (1) {
int ion_config = 0;
ion_config = ion_kernel_ioctl(ion_venc_client,
ION_CMD_MULTIMEDIA, (unsigned long)&mm_data);
if (ion_config != 0) {
udelay(1000);
count++;
mtk_v4l2_err("re-configure buffer! count: %d\n", count);
} else {
break;
}
}
if (count > 0)
mtk_v4l2_err("re-configure buffer done! count: %d\n", count);
/* dma hold ref, ion directly free */
ion_free(ion_venc_client, handle);
#endif
return 0;
}
@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_drv.h"
#define ENC_DVFS 1
#define ENC_EMI_BW 1
void mtk_venc_init_ctx_pm(struct mtk_vcodec_ctx *ctx);
int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
void mtk_venc_deinit_ctx_pm(struct mtk_vcodec_ctx *ctx);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_prepare_venc_dvfs(void);
void mtk_unprepare_venc_dvfs(void);
void mtk_prepare_venc_emi_bw(void);
void mtk_unprepare_venc_emi_bw(void);
/* Frame based PMQoS */
void mtk_venc_pmqos_prelock(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_begin_frame(struct mtk_vcodec_ctx *ctx, int core_id);
void mtk_venc_pmqos_end_frame(struct mtk_vcodec_ctx *ctx, int core_id);
/* GCE version PMQoS */
void mtk_venc_pmqos_gce_flush(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
void mtk_venc_pmqos_gce_done(struct mtk_vcodec_ctx *ctx, int core_id,
int job_cnt);
int mtk_venc_ion_config_buff(struct dma_buf *dmabuf);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,398 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/errno.h>
#include <linux/wait.h>
#include "mtk_vcodec_intr.h"
#include "mtk_vcodec_util.h"
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms)
{
int status = 0;
#ifndef FPGA_INTERRUPT_API_DISABLE
wait_queue_head_t *waitqueue;
long timeout_jiff, ret;
if (core_id >= MTK_VDEC_HW_NUM ||
core_id < 0) {
mtk_v4l2_err("ctx %d, invalid core_id=%d", ctx->id, core_id);
return -1;
}
waitqueue = (wait_queue_head_t *)&ctx->queue[core_id];
timeout_jiff = msecs_to_jiffies(timeout_ms);
ret = wait_event_interruptible_timeout(*waitqueue,
ctx->int_cond[core_id],
timeout_jiff);
if (!ret) {
status = -1; /* timeout */
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, core_id %d timeout time=%ums out %d %d!",
ctx->id, ctx->type, core_id, command, timeout_ms,
ctx->int_cond[core_id], ctx->int_type);
} else if (-ERESTARTSYS == ret) {
mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, core_id %d timeout interrupted by a signal %d %d",
ctx->id, ctx->type, core_id,
command, ctx->int_cond[core_id],
ctx->int_type);
status = -2;
}
ctx->int_cond[core_id] = 0;
ctx->int_type = 0;
#endif
return status;
}
EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
/* Wake up context wait_queue */
void wake_up_dec_ctx(struct mtk_vcodec_ctx *ctx, int core_id)
{
ctx->int_cond[core_id] = 1;
wake_up_interruptible(&ctx->queue[core_id]);
}
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, MTK_VDEC_CORE);
if (ctx == NULL)
return IRQ_HANDLED;
if (ctx->dec_params.svp_mode) {
mtk_v4l2_debug(4, "svp_mode %d don't handle",
ctx->dec_params.svp_mode);
return IRQ_HANDLED;
}
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[VDEC_SYS]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx, MTK_VDEC_CORE);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_handler);
irqreturn_t mtk_vcodec_lat_dec_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
u32 cg_status = 0;
unsigned int dec_done_status = 0;
void __iomem *vdec_misc_addr = dev->dec_reg_base[VDEC_LAT_MISC] +
MTK_VDEC_IRQ_CFG_REG;
ctx = mtk_vcodec_get_curr_ctx(dev, MTK_VDEC_LAT);
if (ctx == NULL)
return IRQ_HANDLED;
/* check if HW active or not */
cg_status = readl(dev->dec_reg_base[VDEC_BASE]);
if ((cg_status & MTK_VDEC_HW_ACTIVE) != 0) {
mtk_v4l2_err("DEC LAT ISR, VDEC active is not 0x0 (0x%08x)",
cg_status);
return IRQ_HANDLED;
}
dec_done_status = readl(vdec_misc_addr);
ctx->irq_status = dec_done_status;
if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return IRQ_HANDLED;
/* clear interrupt */
writel((readl(vdec_misc_addr) | MTK_VDEC_IRQ_CFG),
dev->dec_reg_base[VDEC_LAT_MISC] + MTK_VDEC_IRQ_CFG_REG);
writel((readl(vdec_misc_addr) & ~MTK_VDEC_IRQ_CLR),
dev->dec_reg_base[VDEC_LAT_MISC] + MTK_VDEC_IRQ_CFG_REG);
wake_up_dec_ctx(ctx, MTK_VDEC_LAT);
mtk_v4l2_debug(4,
"%s :wake up ctx %d, dec_done_status=%x",
__func__, ctx->id, dec_done_status);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_lat_dec_irq_handler);
void clean_irq_status(unsigned int irq_status, void __iomem *addr)
{
if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
writel(MTK_VENC_IRQ_STATUS_SPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
writel(MTK_VENC_IRQ_STATUS_PPS, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
writel(MTK_VENC_IRQ_STATUS_FRM, addr);
if (irq_status & MTK_VENC_IRQ_STATUS_VPS)
writel(MTK_VENC_IRQ_STATUS_VPS, addr);
}
/* Wake up context wait_queue */
void wake_up_enc_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
{
ctx->int_cond[0] = 1;
ctx->int_type = reason;
wake_up_interruptible(&ctx->queue[0]);
}
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
struct mtk_vcodec_dev *dev = priv;
struct mtk_vcodec_ctx *ctx;
unsigned long flags;
void __iomem *addr;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_enc_ctx[0];
spin_unlock_irqrestore(&dev->irqlock, flags);
if (ctx == NULL)
return IRQ_HANDLED;
mtk_v4l2_debug(1, "id=%d", ctx->id);
addr = dev->enc_reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->enc_reg_base[VENC_SYS] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED);
#endif
return IRQ_HANDLED;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_handler);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int i = 0;
int ret = 0;
for (i = 0; i < MTK_VDEC_HW_NUM; i++) {
dev->dec_irq[i] = platform_get_irq(pdev, i);
if (i == MTK_VDEC_CORE)
ret = devm_request_irq(&pdev->dev, dev->dec_irq[i],
mtk_vcodec_dec_irq_handler,
IRQF_NO_THREAD | IRQF_SHARED | IRQF_PROBE_SHARED, pdev->name, dev);
else if (i == MTK_VDEC_LAT)
ret = devm_request_irq(&pdev->dev, dev->dec_irq[i],
mtk_vcodec_lat_dec_irq_handler, 0,
pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->dec_irq[%d] %d (%d)",
i, dev->dec_irq[i],
ret);
}
disable_irq(dev->dec_irq[i]);
}
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_dec_irq_setup);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev)
{
#ifndef FPGA_INTERRUPT_API_DISABLE
int ret = 0;
dev->enc_irq = platform_get_irq(pdev, 0);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
mtk_v4l2_debug(1, "Failed to install dev->enc_irq %d (%d)",
dev->enc_irq,
ret);
return -1;
}
disable_irq(dev->enc_irq);
#endif
return 0;
}
EXPORT_SYMBOL(mtk_vcodec_enc_irq_setup);
void mtk_vcodec_gce_timeout_dump(void *ctx)
{
struct mtk_vcodec_ctx *curr_ctx = ctx;
if (IS_ERR_OR_NULL(curr_ctx)) {
mtk_v4l2_err("invalid arguments, curr_ctx: %p", curr_ctx);
return;
}
if (curr_ctx->type == MTK_INST_ENCODER)
mtk_vcodec_enc_timeout_dump(ctx);
else if (curr_ctx->type == MTK_INST_DECODER)
mtk_vcodec_dec_timeout_dump(ctx);
}
EXPORT_SYMBOL(mtk_vcodec_gce_timeout_dump);
void mtk_vcodec_enc_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0, j = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = curr_ctx->dev;
#define REG1_COUNT 15
#define REG2_COUNT 20
unsigned int Reg_1[REG1_COUNT] = {
0x0, 0x14, 0xEC, 0x1C0, 0x1168, 0x11C0,
0x11C4, 0xF4, 0x5C, 0x60, 0x130,
0x24, 0x114C, 0x1164, 0x0140};
unsigned int Reg_2[REG2_COUNT] = {
0xEC, 0x200, 0x204, 0x208,
0x20C, 0x218, 0x21C, 0x228,
0x22C, 0x230, 0xF4, 0x1168,
0x11C0, 0x11C4, 0x1030, 0x240,
0x248, 0x250, 0x130, 0x0140};
mtk_v4l2_debug(0, "is_codec_suspending: %d",
dev->is_codec_suspending);
for (j = 0; j < MTK_VENC_HW_NUM; j++) {
for (i = 0; i < REG1_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_1[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_1[i], value);
}
}
for (j = 0; j < MTK_VENC_HW_NUM; j++) {
for (i = 0x0; i <= 0x220; i = i+4) {
value = readl(dev->enc_reg_base[j] + i);
mtk_v4l2_debug(0, "[line: %d] Core[%d] 0x%x = 0x%lx",__LINE__,
j, i, value);
}
for (i = 0x1140; i <= 0x12B0; i = i+4) {
value = readl(dev->enc_reg_base[j] + i);
mtk_v4l2_debug(0, "[line: %d] Core[%d] 0x%x = 0x%lx",__LINE__,
j, i, value);
}
}
writel(1, dev->enc_reg_base[0] + 0xEC);
writel(1, dev->enc_reg_base[1] + 0xEC);
writel(0, dev->enc_reg_base[0] + 0xF4);
writel(0, dev->enc_reg_base[1] + 0xF4);
for (j = 0; j < MTK_VENC_HW_NUM; j++) {
for (i = 0; i < REG2_COUNT; i++) {
value = readl(dev->enc_reg_base[j] + Reg_2[i]);
mtk_v4l2_debug(0, "[%d] 0x%x = 0x%lx",
j, Reg_2[i], value);
}
}
}
void mtk_vcodec_dec_timeout_dump(void *ctx)
{
unsigned long value;
int i = 0;
struct mtk_vcodec_ctx *curr_ctx = ctx;
struct mtk_vcodec_dev *dev = NULL;
#define LAT_REG_COUNT 26
#define CORE_MISC_REG_COUNT 30
#define CORE_VLD_REG_COUNT 5
unsigned int lat_reg[LAT_REG_COUNT] = {
0x5120, 0x512C, 0x5090, // BITCNT, POS, picture start
0x50E0, 0x50E4, 0x50E8,
0x1120, 0x1124, 0x1128, 0x112C, 0x196C, // input, cycle
0x108, 0x110, 0x114, 0x118, 0x11C, // SMI
0x120, 0x124, 0x128, 0x12C, 0x130, 0x134, 0x138, 0x13C,
0x854, 0x878}; // crc, wptr
unsigned int core_misc_reg[CORE_MISC_REG_COUNT] = {
0x3120, 0x312C, 0x3090, // BITCNT, POS, picture start
0x30E0, 0x30E4, 0x30E8, // error
0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C, 0x20, 0x24,
0xC8, 0xCC, 0x178, // power
0x108, 0x110, 0x114, 0x118, 0x11C, // SMI
0x120, 0x124, 0x128, 0x12C, 0x130, 0x134, 0x138, 0x13C};
unsigned int core_vld_reg[CORE_VLD_REG_COUNT] = {
0x120, 0x124, 0x128, 0x12C, 0x96C}; // input, cycle
if (ctx == NULL) {
mtk_v4l2_debug(0, "can't dump vdec for NULL ctx");
return;
}
dev = curr_ctx->dev;
mtk_v4l2_debug(0, "ctx: %p, is_codec_suspending: %d",
ctx, dev->is_codec_suspending);
for (i = 0; i < LAT_REG_COUNT; i++) {
value = readl(dev->dec_reg_base[VDEC_LAT_MISC] + lat_reg[i]);
mtk_v4l2_debug(0, "[LAT][MISC] 0x%x(%d) = 0x%lx",
lat_reg[i], ((lat_reg[i]&0x0FFF)%0x800)/4, value);
}
for (i = 0; i < CORE_MISC_REG_COUNT; i++) {
value = readl(dev->dec_reg_base[VDEC_MISC] + core_misc_reg[i]);
mtk_v4l2_debug(0, "[CORE][MISC] 0x%x(%d) = 0x%lx",
core_misc_reg[i], (core_misc_reg[i] & 0x0FFF)/4, value);
}
for (i = 0; i < CORE_VLD_REG_COUNT; i++) {
value = readl(dev->dec_reg_base[VDEC_VLD] + core_vld_reg[i]);
mtk_v4l2_debug(0, "[CORE][VLD] 0x%x(%d) = 0x%lx",
core_vld_reg[i], (core_vld_reg[i] % 0x800)/4, value);
}
}
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_INTR_H_
#define _MTK_VCODEC_INTR_H_
#include <linux/interrupt.h>
#include <linux/irq.h>
#include "mtk_vcodec_drv.h"
#include <linux/platform_device.h>
#define MTK_INST_IRQ_RECEIVED 0x1
#define MTK_VDEC_HW_ACTIVE 0x10
#define MTK_VDEC_IRQ_CFG 0x11
#define MTK_VDEC_IRQ_CLR 0x10
#define MTK_VDEC_IRQ_CFG_REG 0xA4
#define MTK_VDEC_IRQ_STATUS_DEC_SUCCESS 0x10000
#define MTK_VENC_IRQ_STATUS_SPS 0x1
#define MTK_VENC_IRQ_STATUS_PPS 0x2
#define MTK_VENC_IRQ_STATUS_FRM 0x4
#define MTK_VENC_IRQ_STATUS_DRAM 0x8
#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
#define MTK_VENC_IRQ_STATUS_VPS 0x80
#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
struct mtk_vcodec_ctx;
/* timeout is ms */
int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx,
int core_id, int command, unsigned int timeout_ms);
irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_dec_lat_irq_handler(int irq, void *priv);
irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv);
int mtk_vcodec_dec_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
int mtk_vcodec_enc_irq_setup(struct platform_device *pdev,
struct mtk_vcodec_dev *dev);
void mtk_vcodec_gce_timeout_dump(void *ctx);
void mtk_vcodec_enc_timeout_dump(void *ctx);
void mtk_vcodec_dec_timeout_dump(void *ctx);
#endif /* _MTK_VCODEC_INTR_H_ */
@@ -0,0 +1,81 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_VCODEC_PM_H_
#define _MTK_VCODEC_PM_H_
#include "ion_drv.h"
#include "mach/mt_iommu.h"
extern struct ion_device *g_ion_device;
#define MTK_PLATFORM_STR "platform:mt6885"
#define MTK_VDEC_RACING_INFO_OFFSET 0x100
#define MTK_VDEC_RACING_INFO_SIZE 68
/**
* struct mtk_vcodec_pm - Power management data structure
*/
struct mtk_vcodec_pm {
struct clk *vdec_bus_clk_src;
struct clk *vencpll;
struct clk *vcodecpll;
struct clk *univpll_d2;
struct clk *clk_cci400_sel;
struct clk *vdecpll;
struct clk *vdec_sel;
struct clk *vencpll_d2;
struct clk *venc_sel;
struct clk *univpll1_d2;
struct clk *venc_lt_sel;
struct clk *img_resz;
struct device *larbvdec;
struct device *larbvenc;
struct device *larbvenclt;
struct device *dev;
struct device_node *chip_node;
struct mtk_vcodec_dev *mtkdev;
struct clk *clk_MT_CG_SOC; /* VDEC SOC*/
struct clk *clk_MT_CG_VDEC0; /* VDEC core 0*/
struct clk *clk_MT_CG_VENC0; /* VENC core 0*/
struct clk *clk_MT_CG_VDEC1; /* VDEC core 1*/
struct clk *clk_MT_CG_VENC1; /* VENC core 1*/
atomic_t dec_active_cnt;
__u32 vdec_racing_info[MTK_VDEC_RACING_INFO_SIZE];
struct mutex dec_racing_info_mutex;
};
enum mtk_dec_dump_addr_type {
DUMP_VDEC_IN_BUF,
DUMP_VDEC_OUT_BUF,
DUMP_VDEC_REF_BUF,
DUMP_VDEC_MV_BUF,
DUMP_VDEC_UBE_BUF,
};
enum mtk_dec_dtsi_reg_idx {
VDEC_SYS,
VDEC_BASE,
VDEC_VLD,
VDEC_MC,
VDEC_MV,
VDEC_MISC,
VDEC_LAT_MISC,
VDEC_LAT_VLD,
VDEC_RACING_CTRL,
VDEC_SOC_GCON,
NUM_MAX_VDEC_REG_BASE,
};
enum mtk_enc_dtsi_reg_idx {
VENC_SYS,
VENC_C1_SYS,
NUM_MAX_VENC_REG_BASE
};
#endif /* _MTK_VCODEC_PM_H_ */
@@ -0,0 +1,92 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "vcodec_bw.h"
bool validate_bw(struct vcodec_bw *bw, int type)
{
if (bw == 0)
return false;
if (bw->smi_bw_mon[type] == 0xFFFFFFFF)
return false;
else
return true;
}
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id)
{
struct vcodec_bw *cur_bw = bw_list;
if (cur_bw == 0)
return 0;
while (cur_bw->id != id) {
cur_bw = cur_bw->next;
if (cur_bw == 0)
return 0;
}
return cur_bw;
}
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *bw = 0;
bw = find_bw_by_id(*bw_list, id);
if (bw == 0) {
bw = kmalloc(sizeof(struct vcodec_bw), GFP_KERNEL);
if (bw == 0)
return 0;
memset(bw, 0, sizeof(struct vcodec_bw));
bw->id = id;
bw->next = *bw_list;
*bw_list = bw;
}
if (*bw_list == 0)
*bw_list = bw;
return bw;
}
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id)
{
struct vcodec_bw *cur_bw = *bw_list;
struct vcodec_bw *next_bw = 0;
if (cur_bw == 0)
return 0;
if (cur_bw->id == id) {
*bw_list = cur_bw->next;
return cur_bw;
}
while (cur_bw->next != 0 && cur_bw->next->id != id)
cur_bw = cur_bw->next;
if (cur_bw->next == 0)
return 0;
if (cur_bw->next->id == id) {
next_bw = cur_bw->next;
cur_bw->next = next_bw->next;
}
return next_bw;
}
void free_all_bw(struct vcodec_bw **bw_list)
{
struct vcodec_bw *bw = 0;
while (*bw_list != 0) {
bw = *bw_list;
*bw_list = bw->next;
kfree(bw);
}
}
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_BW_H__
#define __VCODEC_BW_H__
#include <linux/types.h>
#include <linux/slab.h>
#define TYPE_CNT 3
struct vcodec_bw {
int id;
unsigned int smi_bw_mon[TYPE_CNT];
struct vcodec_bw *next;
};
bool validate_bw(struct vcodec_bw *bw, int type);
struct vcodec_bw *find_bw_by_id(struct vcodec_bw *bw_list, int id);
struct vcodec_bw *add_bw_by_id(struct vcodec_bw **bw_list, int id);
struct vcodec_bw *remove_bw_by_id(struct vcodec_bw **bw_list, int id);
void free_all_bw(struct vcodec_bw **bw_list);
#endif
@@ -0,0 +1,666 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#if !IS_ENABLED(64BIT)
#include <asm/div64.h>
#endif
#include <linux/slab.h>
#include "vcodec_dvfs.h"
#define MAX_SUBMIT (33*1000)
#define SHOW_ALGO_INFO 0
long long div_64(long long a, long long b)
{
#if IS_ENABLED(CONFIG_64BIT)
return (a/b);
#else
uint32_t rem = 0;
uint64_t dividend, divisor;
dividend = (a >= 0) ? a : (-a);
divisor = (b >= 0) ? b : (-b);
rem = do_div(dividend, divisor);
a = ((a < 0) ^ (b < 0)) ?
(0LL - (long long)dividend) :
(long long)dividend;
return a;
#endif
}
/**
* get_time_us - Get current time in us
*
* To prevent overflow, long long is used for return value
*/
long long get_time_us(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return (1000000LL * tv.tv_sec + tv.tv_usec);
}
/**
* new_hist_to_end - add a new blank codec_history to end of list
*
* The newly added codec_history is returned and user is expected to
* fill required information
*/
struct codec_history *new_hist_to_end(struct codec_history **head)
{
struct codec_history *hist;
struct codec_history *tgt = *head;
hist = kmalloc(sizeof(struct codec_history), GFP_KERNEL);
if (hist == 0)
return hist;
memset(hist, 0, sizeof(struct codec_history));
if (tgt == 0) {
*head = hist;
tgt = *head;
} else {
while (tgt->next != 0)
tgt = tgt->next;
tgt->next = hist;
}
return hist;
}
/**
* free_next_hist - free next codec_history and shift next next one forward
* (not used)
*
* Returns the new next codec_history element.
*/
struct codec_history *free_next_hist(struct codec_history *cur_hist)
{
struct codec_history *next;
if (cur_hist == 0)
return 0;
next = cur_hist->next;
if (next != 0) {
cur_hist->next = next->next;
kfree(next);
}
return cur_hist->next;
}
/**
* free_hist - remove target codec_history from list and free its memory
*
* Return 0 if free successfully
* -1 means there is error and nothing is changed
*/
int free_hist_(struct codec_history **head, struct codec_history *target)
{
struct codec_history *temp;
/* Either one is null, do nothing */
if (target == 0 || (*head) == 0)
return -1;
/* Free head */
if ((*head) == target) {
*head = target->next;
kfree(target);
} else {
/* Find target in list */
temp = *head;
while (temp->next != target && temp->next != 0)
temp = temp->next;
if (temp->next == target) {
temp->next = target->next;
kfree(target);
} else {
pr_info("VCODEC free history %p not found",
target->handle);
return -1;
}
}
return 0;
}
/**
* free_hist - Scan through the history list and free unused ones
*
* Return 0 for success
* -1 if error
*/
int free_hist(struct codec_history **head, int only_unused)
{
struct codec_history *chk_hist; /* history item to check */
struct codec_history *prev_hist;
int hist_idx;
int prev_idx;
long long cur_time;
if (head == 0)
return -1;
chk_hist = *head;
prev_hist = chk_hist;
cur_time = get_time_us();
while (chk_hist != 0) {
hist_idx = chk_hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
if (only_unused == 0 ||
(cur_time - chk_hist->end[prev_idx]) > FREE_HIST_DELAY) {
/* free head */
if (chk_hist == *head) {
*head = chk_hist->next;
kfree(chk_hist);
chk_hist = *head;
prev_hist = chk_hist;
} else { /* free other items */
prev_hist->next = chk_hist->next;
kfree(chk_hist);
chk_hist = prev_hist->next;
}
} else { /* do nothing to current, check next */
prev_hist = chk_hist;
chk_hist = chk_hist->next;
}
}
return 0;
}
/**
* find_hist = find a codec_history from the list by handle
*
* Returns the codec_history
* 0 if not found
*/
struct codec_history *find_hist(void *handle, struct codec_history *head)
{
while (head != 0 && head->handle != handle)
head = head->next;
return head;
}
/**
* find_calc_idx - helper function to find the starting (first_idx) and
* previous (prev_idx) index of current history
*
* Return 0 if found indices
* -1 if there is no codec_history or history is empty
*/
int find_calc_idx(struct codec_history *hist, int *first_idx, int *prev_idx)
{
if (hist == 0 || hist->cur_cnt == 0)
return -1;
*first_idx = hist->cur_idx - hist->cur_cnt;
if (*first_idx < 0)
*first_idx += MAX_HISTORY;
*prev_idx = hist->cur_idx - 1;
if (*prev_idx < 0)
*prev_idx += MAX_HISTORY;
return 0;
}
/**
* est_next_submit - estimate next submission time by previous history
* This function is called when a new job is submitted but
* not yet completed & added to history. So the estimation
* is really for the next next job.
*
* Return time in us. 0 means no history available, treat it as coming now
*/
long long est_next_submit(struct codec_history *hist)
{
int first_idx, prev_idx;
long long next_submit;
if (find_calc_idx(hist, &first_idx, &prev_idx) < 0)
return 0;
/* Add 2x estimated gap for next next job */
if (hist->cur_cnt == 1)
return (hist->submit[prev_idx] + MIN_SUBMIT_GAP * 2);
#if SHOW_ALGO_INFO
pr_info("%s first_idx %d(%lld) prev_idx %d(%lld)", __func__,
first_idx, hist->submit[first_idx],
prev_idx, hist->submit[prev_idx]);
#endif
/* next_submit need to *2 because it's estimating 2 gaps after
* (1 for current, 1 for next)
*/
if (hist->submit_interval == 0) {
next_submit = div_64(
(hist->submit[prev_idx] - hist->submit[first_idx]) * 2,
(hist->cur_cnt - 1));
} else {
next_submit = hist->submit_interval * 2;
}
if (next_submit > MAX_SUBMIT * 2) {
#if SHOW_ALGO_INFO
pr_info("%s %lld -> MAX SUBMIT(%d)", __func__,
next_submit, MAX_SUBMIT);
#endif
next_submit = MAX_SUBMIT * 2;
}
return hist->submit[prev_idx] + next_submit;
}
/**
* est_new_kcy - Estimate cycles required
*
* Return estimate k(10^3) cycles for new job
*/
int est_new_kcy(struct codec_history *hist)
{
if (hist == 0)
return 0;
return (hist->tot_kcy / hist->cur_cnt);
}
/**
* est_next_job - Estimate next job's required finish time and clock frequency
* required
*
* Return 0 for success
* -1 for error
*/
int est_next_job(long long now_us, long long *t_us, int *kcy, int *min_mhz,
struct codec_job *job, struct codec_history *head)
{
struct codec_history *hist;
long long deadline;
long long new_mhz;
if (t_us == 0 || kcy == 0 || min_mhz == 0 || job == 0)
return -1;
hist = find_hist(job->handle, head);
#if SHOW_ALGO_INFO
pr_info("%s find_hist %p handle %p\n", __func__, hist,
(hist == 0) ? 0 : hist->handle);
#endif
/* This is a new instance - no history yet */
if (hist == 0) {
/* Set *t_us = now_us to signal full speed */
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s no history yet, full speed\n", __func__);
#endif
} else {
*kcy += est_new_kcy(hist);
deadline = est_next_submit(hist);
if (deadline == 0)
*t_us = now_us;
else {
if (deadline > now_us) {
new_mhz = div_64((*kcy) * 1000LL,
(deadline - now_us));
if (new_mhz > *min_mhz)
*min_mhz = (int)new_mhz;
*t_us = now_us + div_64((*kcy) * 1000LL,
(*min_mhz));
} else {
/**
* Overdue, set *t_us = now_us to signal full
* speed
*/
*t_us = now_us;
}
}
/* Extra boost for first frame time */
if (hist->cur_cnt < MAX_HISTORY)
*t_us = now_us;
#if SHOW_ALGO_INFO
pr_info("%s deadline %llu, kcy %d\n", __func__, deadline, *kcy);
#endif
}
#if SHOW_ALGO_INFO
pr_info("%s now_us %lld, target_us %lld, min_mhz %d\n", __func__,
now_us, *t_us, *min_mhz);
#endif
/* Stop estimating if no more job or worst time constraint is reached */
if (job->next == 0 || (*t_us) == now_us)
return 0;
return est_next_job(now_us, t_us, kcy, min_mhz, job->next, head);
}
/**
* update_hist_item - Use a completed job to update a history item
*
* Return 1 if previous history is cleared & only new job info stays
* 0 if new job info is added to history
*/
int update_hist_item(struct codec_job *job, struct codec_history *hist)
{
int hist_idx;
int prev_idx;
if (job->handle != hist->handle) {
pr_info("VCODEC dvfs job - history mismatch\n");
return -1;
}
hist_idx = hist->cur_idx;
prev_idx = (hist_idx == 0) ? (MAX_HISTORY-1) : (hist_idx-1);
/* Previous history is too far away, restart */
if (hist->cur_cnt > 1 &&
(job->submit - hist->submit[prev_idx]) > MAX_SUBMIT_GAP) {
#if SHOW_ALGO_INFO
pr_info("%s %p, gap (%lld), reset hist\n", __func__,
hist->handle, (job->submit-hist->submit[prev_idx]));
#endif
memset(hist->kcy, 0, sizeof(int)*MAX_HISTORY);
memset(hist->submit, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->start, 0, sizeof(long long)*MAX_HISTORY);
memset(hist->end, 0, sizeof(long long)*MAX_HISTORY);
hist->kcy[0] = (int)div_64(job->mhz * (job->end - job->start),
1000LL);
hist->submit[0] = job->submit;
hist->start[0] = job->start;
hist->end[0] = job->end;
hist->cur_idx = 1; /* cur_idx = 0 + 1 (updated for next) */
hist->cur_cnt = 1;
hist->tot_kcy = hist->kcy[0];
hist->tot_time = hist->end[0] - hist->start[0];
return 1;
}
/* Update history */
if (hist->cur_cnt == MAX_HISTORY) {
hist->tot_kcy = hist->tot_kcy - hist->kcy[hist_idx] +
(int)div_64(job->mhz * (job->end - job->start), 1000);
hist->tot_time = hist->tot_time -
(hist->end[hist_idx] - hist->start[hist_idx]) +
(job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 1 kcy %d, time %llu\n", __func__,
hist->tot_kcy, hist->tot_time);
#endif
} else {
hist->cur_cnt++;
hist->tot_kcy = hist->tot_kcy +
(int)div_64(job->mhz * (job->end - job->start), 1000);
hist->tot_time = hist->tot_time + (job->end - job->start);
#if SHOW_ALGO_INFO
pr_info("%s 2 kcy %d, time %llu, cnt %d\n", __func__,
hist->tot_kcy, hist->tot_time, hist->cur_cnt);
#endif
}
hist->kcy[hist_idx] = (int)div_64(job->mhz * (job->end - job->start),
1000LL);
hist->submit[hist_idx] = job->submit;
hist->start[hist_idx] = job->start;
hist->end[hist_idx] = job->end;
#if SHOW_ALGO_INFO
pr_info("%s %p, mhz %d, sub %lld, start %lld, end %lld\n", __func__,
hist->handle, job->mhz, job->submit, job->start, job->end);
#endif
hist->cur_idx = (hist_idx + 1) % MAX_HISTORY;
return 0;
}
/**
* update_hist - Use a completed job to update corresponding history
*
* Return 0 for success
* 1 for clear old history and update success
* -1 for not updated due to error
*/
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval)
{
struct codec_history *target;
int ret;
target = find_hist(job->handle, *head);
if (target == 0) {
target = new_hist_to_end(head);
if (target == 0)
return -1;
target->handle = job->handle;
if (submit_interval > 0)
target->submit_interval = submit_interval;
#if SHOW_ALGO_INFO
pr_info("%s new history %p head %p\n", __func__, target, *head);
#endif
}
ret = update_hist_item(job, target);
if (ret == 1) {
/* Long pause, start over */
#if SHOW_ALGO_INFO
pr_info("VCODEC dvfs start over for handle %p", job->handle);
#endif
}
return ret;
}
/**
* add_job - Add a new job to job queue
*
* Return total job count after add
* -1 if error
*/
int add_job_(struct codec_job *job, struct codec_job **head)
{
int job_cnt;
struct codec_job *last_job;
/* Error case */
if (head == 0)
return -1;
/* New job is head */
if (*head == 0) {
*head = job;
return 1;
}
/* Add job to tail */
job_cnt = 1; /* at least head job exists */
last_job = *head;
while (last_job->next != 0) {
if (last_job->handle == job->handle) {
pr_info("VCODEC dvfs multiple jobs from same instance");
return -1;
}
last_job = last_job->next;
job_cnt++;
}
last_job->next = job;
job_cnt++;
return job_cnt;
}
/**
* add_job - Add a new job with only the handle when entering lock hw
*
* Return new job just added
* 0 if failed to add
*/
struct codec_job *add_job(void *handle, struct codec_job **head)
{
struct codec_job *new_job;
int add_result = 0;
new_job = kmalloc(sizeof(struct codec_job), GFP_KERNEL);
if (new_job == 0)
return 0;
memset(new_job, 0, sizeof(struct codec_job));
/* New job with handle & current time */
new_job->handle = handle;
new_job->submit = get_time_us();
add_result = add_job_(new_job, head);
if (add_result < 0) {
kfree(new_job);
new_job = 0;
}
return new_job;
}
/**
* move_job_to_head - Move the target job to head for faster access next time
* (most recently used)
*
* Return pointer to the moved job (should now be the head)
* 0 if job not found
*/
struct codec_job *move_job_to_head(void *handle, struct codec_job **head)
{
struct codec_job *prev_job;
struct codec_job *target_job;
/* Empty job list, do nothing */
if (*head == 0)
return 0;
/* Target job is head already, do nothing */
if ((*head)->handle == handle)
return *head;
/* Search for target job */
prev_job = *head;
while (prev_job->next != 0 && prev_job->next->handle != handle)
prev_job = prev_job->next;
/* Job not found */
if (prev_job->next == 0)
return 0;
/* Found job, move it to head */
target_job = prev_job->next;
prev_job->next = target_job->next;
target_job->next = *head;
*head = target_job;
return target_job;
}
/**
* est_freq - Estimate minimum running frequency that provides enough
* performance
*
* Return minimum required mhz to finish all jobs in time
*/
int est_freq(void *handle, struct codec_job **job, struct codec_history *head)
{
long long cur_time;
long long end_time;
int kcy;
int min_mhz;
int est_res;
struct codec_job *target_job;
cur_time = get_time_us();
end_time = cur_time;
kcy = 0;
min_mhz = 0;
target_job = move_job_to_head(handle, job);
/* Error case, just run at max freq */
if (target_job == 0) {
pr_info("%s job not found!\n", __func__);
return DEFAULT_MHZ;
}
if (target_job != *job)
pr_info("%s target_job != job queue head\n", __func__);
est_res = est_next_job(cur_time, &end_time, &kcy, &min_mhz, target_job,
head);
#if SHOW_ALGO_INFO
pr_info("%s res %d, min_mhz %d\n", __func__, est_res, min_mhz);
#endif
/* Error case or do it ASAP */
if (est_res == -1 || (cur_time == end_time))
return DEFAULT_MHZ;
return min_mhz;
}
/**
* match_freq - Match estimated vcodec frequency with available frequencies
*
* Match requested mhz to available mhz
*/
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt)
{
u64 res_mhz = DEFAULT_MHZ;
int i;
u64 target64;
if (freq_list == 0)
return 0;
target64 = (u64)target_mhz;
for (i = 0; i < freq_cnt ; i++) {
if (freq_list[i] > target_mhz && freq_list[i] < res_mhz)
res_mhz = freq_list[i];
}
/* target_mhz is higher than all available frequency, choose max freq */
if (res_mhz == DEFAULT_MHZ)
res_mhz = freq_list[0];
#if SHOW_ALGO_INFO
pr_info("%s %d -> %llu\n", __func__, target_mhz, res_mhz);
#endif
return res_mhz;
}
@@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __VCODEC_DVFS_H__
#define __VCODEC_DVFS_H__
#define MAX_HISTORY 10
#define MIN_SUBMIT_GAP 2000 /* 2ms */
#define MAX_SUBMIT_GAP (1000*1000) /* 1 second */
#define FREE_HIST_DELAY (5000*1000) /* Free history delay */
#define DEFAULT_MHZ 99999
struct codec_history {
void *handle;
int kcy[MAX_HISTORY];
long long submit[MAX_HISTORY];
long long start[MAX_HISTORY];
long long end[MAX_HISTORY];
long long submit_interval;
int cur_idx;
int cur_cnt;
int tot_kcy;
long long tot_time;
struct codec_history *next;
};
struct codec_job {
void *handle;
long long submit;
long long start;
long long end;
int mhz;
struct codec_job *next;
};
long long get_time_us(void);
/* Add a new job to job queue */
struct codec_job *add_job(void *handle, struct codec_job **head);
/* Move target job to queue head for processing */
struct codec_job *move_job_to_head(void *handle, struct codec_job **head);
/* Update history with completed job */
int update_hist(struct codec_job *job, struct codec_history **head,
long long submit_interval);
/* Estimate required freq from job queue and previous history */
int est_freq(void *handle, struct codec_job **job, struct codec_history *head);
u64 match_freq(int target_mhz, u64 *freq_list, u32 freq_cnt);
/* Free unused/all history */
int free_hist(struct codec_history **head, int only_unused);
#endif
Loaded 100 of 80519 files, more files were not shown because too many files have changed in this diff. Show more