Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o
mtk-vcodec-enc-y := venc/venc_vp8_if.o \
venc/venc_h264_if.o \
mtk_vcodec_enc.o \
mtk_vcodec_enc_drv.o \
mtk_vcodec_enc_pm.o \
venc_drv_if.o \
venc_vpu_if.o \
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: PC Chen <pc.chen@mediatek.com>
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
#ifndef _MTK_VCODEC_ENC_H_
#define _MTK_VCODEC_ENC_H_
#include <media/videobuf2-core.h>
#include <media/v4l2-mem2mem.h>
#include "mtk_vcodec_enc_drv.h"
#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_OFFSET 0x05C
#define MTK_VENC_IRQ_ACK_OFFSET 0x060
/**
* struct mtk_video_enc_buf - Private data related to each VB2 buffer.
* @m2m_buf: M2M buffer
* @param_change: Types of encode parameter change before encoding this
* buffer
* @enc_params: Encode parameters changed before encode this buffer
*/
struct mtk_video_enc_buf {
struct v4l2_m2m_buffer m2m_buf;
u32 param_change;
struct mtk_enc_params enc_params;
};
extern const struct v4l2_ioctl_ops mtk_venc_ioctl_ops;
extern const struct v4l2_m2m_ops mtk_venc_m2m_ops;
int mtk_venc_unlock(struct mtk_vcodec_enc_ctx *ctx);
int mtk_venc_lock(struct mtk_vcodec_enc_ctx *ctx);
int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
struct vb2_queue *dst_vq);
void mtk_vcodec_enc_release(struct mtk_vcodec_enc_ctx *ctx);
int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_enc_ctx *ctx);
void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_enc_ctx *ctx);
#endif /* _MTK_VCODEC_ENC_H_ */
@@ -0,0 +1,497 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: PC Chen <pc.chen@mediatek.com>
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <media/v4l2-event.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-dma-contig.h>
#include "mtk_vcodec_enc.h"
#include "mtk_vcodec_enc_pm.h"
#include "../common/mtk_vcodec_intr.h"
static const struct mtk_video_fmt mtk_video_formats_output[] = {
{
.fourcc = V4L2_PIX_FMT_NV12M,
.type = MTK_FMT_FRAME,
.num_planes = 2,
},
{
.fourcc = V4L2_PIX_FMT_NV21M,
.type = MTK_FMT_FRAME,
.num_planes = 2,
},
{
.fourcc = V4L2_PIX_FMT_YUV420M,
.type = MTK_FMT_FRAME,
.num_planes = 3,
},
{
.fourcc = V4L2_PIX_FMT_YVU420M,
.type = MTK_FMT_FRAME,
.num_planes = 3,
},
};
static const struct mtk_video_fmt mtk_video_formats_capture_h264[] = {
{
.fourcc = V4L2_PIX_FMT_H264,
.type = MTK_FMT_ENC,
.num_planes = 1,
},
};
static const struct mtk_video_fmt mtk_video_formats_capture_vp8[] = {
{
.fourcc = V4L2_PIX_FMT_VP8,
.type = MTK_FMT_ENC,
.num_planes = 1,
},
};
static 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);
}
static irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
{
struct mtk_vcodec_enc_dev *dev = priv;
struct mtk_vcodec_enc_ctx *ctx;
unsigned long flags;
void __iomem *addr;
int core_id;
spin_lock_irqsave(&dev->irqlock, flags);
ctx = dev->curr_ctx;
spin_unlock_irqrestore(&dev->irqlock, flags);
core_id = dev->venc_pdata->core_id;
if (core_id < 0 || core_id >= NUM_MAX_VCODEC_REG_BASE) {
mtk_v4l2_venc_err(ctx, "Invalid core id: %d, ctx id: %d", core_id, ctx->id);
return IRQ_HANDLED;
}
mtk_v4l2_venc_dbg(1, ctx, "id: %d, core id: %d", ctx->id, core_id);
addr = dev->reg_base[core_id] + MTK_VENC_IRQ_ACK_OFFSET;
ctx->irq_status = readl(dev->reg_base[core_id] +
(MTK_VENC_IRQ_STATUS_OFFSET));
clean_irq_status(ctx->irq_status, addr);
wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED, 0);
return IRQ_HANDLED;
}
static int fops_vcodec_open(struct file *file)
{
struct mtk_vcodec_enc_dev *dev = video_drvdata(file);
struct mtk_vcodec_enc_ctx *ctx = NULL;
int ret = 0;
struct vb2_queue *src_vq;
unsigned long flags;
ctx = kzalloc_obj(*ctx);
if (!ctx)
return -ENOMEM;
mutex_lock(&dev->dev_mutex);
/*
* Use simple counter to uniquely identify this context. Only
* used for logging.
*/
ctx->id = dev->id_counter++;
v4l2_fh_init(&ctx->fh, video_devdata(file));
v4l2_fh_add(&ctx->fh, file);
INIT_LIST_HEAD(&ctx->list);
ctx->dev = dev;
init_waitqueue_head(&ctx->queue[0]);
mutex_init(&ctx->q_mutex);
ctx->type = MTK_INST_ENCODER;
ret = mtk_vcodec_enc_ctrls_setup(ctx);
if (ret) {
mtk_v4l2_venc_err(ctx, "Failed to setup controls() (%d)", ret);
goto err_ctrls_setup;
}
ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_enc, ctx,
&mtk_vcodec_enc_queue_init);
if (IS_ERR((__force void *)ctx->m2m_ctx)) {
ret = PTR_ERR((__force void *)ctx->m2m_ctx);
mtk_v4l2_venc_err(ctx, "Failed to v4l2_m2m_ctx_init() (%d)", ret);
goto err_m2m_ctx_init;
}
src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
ctx->empty_flush_buf.vb.vb2_buf.vb2_queue = src_vq;
mtk_vcodec_enc_set_default_params(ctx);
if (v4l2_fh_is_singular(&ctx->fh)) {
/*
* load fireware to checks if it was loaded already and
* does nothing in that case
*/
ret = mtk_vcodec_fw_load_firmware(dev->fw_handler);
if (ret < 0) {
/*
* Return 0 if downloading firmware successfully,
* otherwise it is failed
*/
mtk_v4l2_venc_err(ctx, "vpu_load_firmware failed!");
goto err_load_fw;
}
dev->enc_capability =
mtk_vcodec_fw_get_venc_capa(dev->fw_handler);
mtk_v4l2_venc_dbg(0, ctx, "encoder capability %x", dev->enc_capability);
}
mtk_v4l2_venc_dbg(2, ctx, "Create instance [%d]@%p m2m_ctx=%p ",
ctx->id, ctx, ctx->m2m_ctx);
spin_lock_irqsave(&dev->dev_ctx_lock, flags);
list_add(&ctx->list, &dev->ctx_list);
spin_unlock_irqrestore(&dev->dev_ctx_lock, flags);
mutex_unlock(&dev->dev_mutex);
mtk_v4l2_venc_dbg(0, ctx, "%s encoder [%d]", dev_name(&dev->plat_dev->dev),
ctx->id);
return ret;
/* Deinit when failure occurred */
err_load_fw:
v4l2_m2m_ctx_release(ctx->m2m_ctx);
err_m2m_ctx_init:
v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
err_ctrls_setup:
v4l2_fh_del(&ctx->fh, file);
v4l2_fh_exit(&ctx->fh);
kfree(ctx);
mutex_unlock(&dev->dev_mutex);
return ret;
}
static int fops_vcodec_release(struct file *file)
{
struct mtk_vcodec_enc_dev *dev = video_drvdata(file);
struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
unsigned long flags;
mtk_v4l2_venc_dbg(1, ctx, "[%d] encoder", ctx->id);
mutex_lock(&dev->dev_mutex);
v4l2_m2m_ctx_release(ctx->m2m_ctx);
mtk_vcodec_enc_release(ctx);
v4l2_fh_del(&ctx->fh, file);
v4l2_fh_exit(&ctx->fh);
v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
/*
* Cancel any pending encode work before freeing the context.
* Although v4l2_m2m_ctx_release() waits for m2m job completion,
* the workqueue handler (mtk_venc_worker) may still be accessing
* the context after v4l2_m2m_job_finish() returns. Without this,
* a use-after-free occurs when the worker accesses ctx after kfree.
*/
cancel_work_sync(&ctx->encode_work);
spin_lock_irqsave(&dev->dev_ctx_lock, flags);
list_del_init(&ctx->list);
spin_unlock_irqrestore(&dev->dev_ctx_lock, flags);
kfree(ctx);
mutex_unlock(&dev->dev_mutex);
return 0;
}
static const struct v4l2_file_operations mtk_vcodec_fops = {
.owner = THIS_MODULE,
.open = fops_vcodec_open,
.release = fops_vcodec_release,
.poll = v4l2_m2m_fop_poll,
.unlocked_ioctl = video_ioctl2,
.mmap = v4l2_m2m_fop_mmap,
};
static int mtk_vcodec_probe(struct platform_device *pdev)
{
struct mtk_vcodec_enc_dev *dev;
struct video_device *vfd_enc;
phandle rproc_phandle;
enum mtk_vcodec_fw_type fw_type;
int ret;
dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
INIT_LIST_HEAD(&dev->ctx_list);
dev->plat_dev = pdev;
if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
&rproc_phandle)) {
fw_type = VPU;
} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
&rproc_phandle)) {
fw_type = SCP;
} else {
dev_err(&pdev->dev, "[MTK VCODEC] Could not get venc IPI device");
return -ENODEV;
}
dma_set_max_seg_size(&pdev->dev, UINT_MAX);
dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, ENCODER);
if (IS_ERR(dev->fw_handler))
return PTR_ERR(dev->fw_handler);
dev->venc_pdata = of_device_get_match_data(&pdev->dev);
ret = mtk_vcodec_init_enc_clk(dev);
if (ret < 0) {
dev_err(&pdev->dev, "[MTK VCODEC] Failed to get mtk vcodec clock source!");
goto err_enc_pm;
}
pm_runtime_enable(&pdev->dev);
dev->reg_base[dev->venc_pdata->core_id] =
devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(dev->reg_base[dev->venc_pdata->core_id])) {
ret = PTR_ERR(dev->reg_base[dev->venc_pdata->core_id]);
goto err_res;
}
dev->enc_irq = platform_get_irq(pdev, 0);
if (dev->enc_irq < 0) {
ret = dev->enc_irq;
goto err_res;
}
irq_set_status_flags(dev->enc_irq, IRQ_NOAUTOEN);
ret = devm_request_irq(&pdev->dev, dev->enc_irq,
mtk_vcodec_enc_irq_handler,
0, pdev->name, dev);
if (ret) {
dev_err(&pdev->dev,
"[MTK VCODEC] Failed to install dev->enc_irq %d (%d) core_id (%d)",
dev->enc_irq, ret, dev->venc_pdata->core_id);
ret = -EINVAL;
goto err_res;
}
mutex_init(&dev->enc_mutex);
mutex_init(&dev->dev_mutex);
spin_lock_init(&dev->dev_ctx_lock);
spin_lock_init(&dev->irqlock);
snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
"[MTK_V4L2_VENC]");
ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
if (ret) {
dev_err(&pdev->dev, "[MTK VCODEC] v4l2_device_register err=%d", ret);
goto err_res;
}
/* allocate video device for encoder and register it */
vfd_enc = video_device_alloc();
if (!vfd_enc) {
dev_err(&pdev->dev, "[MTK VCODEC] Failed to allocate video device");
ret = -ENOMEM;
goto err_enc_alloc;
}
vfd_enc->fops = &mtk_vcodec_fops;
vfd_enc->ioctl_ops = &mtk_venc_ioctl_ops;
vfd_enc->release = video_device_release;
vfd_enc->lock = &dev->dev_mutex;
vfd_enc->v4l2_dev = &dev->v4l2_dev;
vfd_enc->vfl_dir = VFL_DIR_M2M;
vfd_enc->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE |
V4L2_CAP_STREAMING;
snprintf(vfd_enc->name, sizeof(vfd_enc->name), "%s",
MTK_VCODEC_ENC_NAME);
video_set_drvdata(vfd_enc, dev);
dev->vfd_enc = vfd_enc;
platform_set_drvdata(pdev, dev);
dev->m2m_dev_enc = v4l2_m2m_init(&mtk_venc_m2m_ops);
if (IS_ERR((__force void *)dev->m2m_dev_enc)) {
dev_err(&pdev->dev, "[MTK VCODEC] Failed to init mem2mem enc device");
ret = PTR_ERR((__force void *)dev->m2m_dev_enc);
goto err_enc_mem_init;
}
dev->encode_workqueue =
alloc_ordered_workqueue(MTK_VCODEC_ENC_NAME,
WQ_MEM_RECLAIM |
WQ_FREEZABLE);
if (!dev->encode_workqueue) {
dev_err(&pdev->dev, "[MTK VCODEC] Failed to create encode workqueue");
ret = -EINVAL;
goto err_event_workq;
}
ret = video_register_device(vfd_enc, VFL_TYPE_VIDEO, -1);
if (ret) {
dev_err(&pdev->dev, "[MTK VCODEC] Failed to register video device");
goto err_enc_reg;
}
mtk_vcodec_dbgfs_init(dev, true);
dev_dbg(&pdev->dev, "[MTK VCODEC] encoder %d registered as /dev/video%d",
dev->venc_pdata->core_id, vfd_enc->num);
return 0;
err_enc_reg:
destroy_workqueue(dev->encode_workqueue);
err_event_workq:
v4l2_m2m_release(dev->m2m_dev_enc);
err_enc_mem_init:
video_unregister_device(vfd_enc);
err_enc_alloc:
v4l2_device_unregister(&dev->v4l2_dev);
err_res:
pm_runtime_disable(dev->pm.dev);
err_enc_pm:
mtk_vcodec_fw_release(dev->fw_handler);
return ret;
}
static const struct mtk_vcodec_enc_pdata mt8173_avc_pdata = {
.capture_formats = mtk_video_formats_capture_h264,
.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
.output_formats = mtk_video_formats_output,
.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
.min_bitrate = 64,
.max_bitrate = 60000000,
.core_id = VENC_SYS,
};
static const struct mtk_vcodec_enc_pdata mt8173_vp8_pdata = {
.capture_formats = mtk_video_formats_capture_vp8,
.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_vp8),
.output_formats = mtk_video_formats_output,
.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
.min_bitrate = 64,
.max_bitrate = 9000000,
.core_id = VENC_LT_SYS,
};
static const struct mtk_vcodec_enc_pdata mt8183_pdata = {
.uses_ext = true,
.capture_formats = mtk_video_formats_capture_h264,
.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
.output_formats = mtk_video_formats_output,
.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
.min_bitrate = 64,
.max_bitrate = 40000000,
.core_id = VENC_SYS,
};
static const struct mtk_vcodec_enc_pdata mt8188_pdata = {
.uses_ext = true,
.capture_formats = mtk_video_formats_capture_h264,
.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
.output_formats = mtk_video_formats_output,
.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
.min_bitrate = 64,
.max_bitrate = 50000000,
.core_id = VENC_SYS,
.uses_34bit = true,
};
static const struct mtk_vcodec_enc_pdata mt8192_pdata = {
.uses_ext = true,
.capture_formats = mtk_video_formats_capture_h264,
.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
.output_formats = mtk_video_formats_output,
.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
.min_bitrate = 64,
.max_bitrate = 100000000,
.core_id = VENC_SYS,
};
static const struct mtk_vcodec_enc_pdata mt8195_pdata = {
.uses_ext = true,
.capture_formats = mtk_video_formats_capture_h264,
.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
.output_formats = mtk_video_formats_output,
.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
.min_bitrate = 64,
.max_bitrate = 100000000,
.core_id = VENC_SYS,
};
static const struct of_device_id mtk_vcodec_enc_match[] = {
{.compatible = "mediatek,mt8173-vcodec-enc",
.data = &mt8173_avc_pdata},
{.compatible = "mediatek,mt8173-vcodec-enc-vp8",
.data = &mt8173_vp8_pdata},
{.compatible = "mediatek,mt8183-vcodec-enc", .data = &mt8183_pdata},
{.compatible = "mediatek,mt8188-vcodec-enc", .data = &mt8188_pdata},
{.compatible = "mediatek,mt8192-vcodec-enc", .data = &mt8192_pdata},
{.compatible = "mediatek,mt8195-vcodec-enc", .data = &mt8195_pdata},
{},
};
MODULE_DEVICE_TABLE(of, mtk_vcodec_enc_match);
static void mtk_vcodec_enc_remove(struct platform_device *pdev)
{
struct mtk_vcodec_enc_dev *dev = platform_get_drvdata(pdev);
destroy_workqueue(dev->encode_workqueue);
if (dev->m2m_dev_enc)
v4l2_m2m_release(dev->m2m_dev_enc);
if (dev->vfd_enc)
video_unregister_device(dev->vfd_enc);
mtk_vcodec_dbgfs_deinit(&dev->dbgfs);
v4l2_device_unregister(&dev->v4l2_dev);
pm_runtime_disable(dev->pm.dev);
mtk_vcodec_fw_release(dev->fw_handler);
}
static struct platform_driver mtk_vcodec_enc_driver = {
.probe = mtk_vcodec_probe,
.remove = mtk_vcodec_enc_remove,
.driver = {
.name = MTK_VCODEC_ENC_NAME,
.of_match_table = mtk_vcodec_enc_match,
},
};
module_platform_driver(mtk_vcodec_enc_driver);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Mediatek video codec V4L2 encoder driver");
@@ -0,0 +1,250 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2023 MediaTek Inc.
* Author: Yunfei Dong <yunfei.dong@mediatek.com>
*/
#ifndef _MTK_VCODEC_ENC_DRV_H_
#define _MTK_VCODEC_ENC_DRV_H_
#include "../common/mtk_vcodec_cmn_drv.h"
#include "../common/mtk_vcodec_dbgfs.h"
#include "../common/mtk_vcodec_fw_priv.h"
#include "../common/mtk_vcodec_util.h"
#define MTK_VCODEC_ENC_NAME "mtk-vcodec-enc"
#define MTK_ENC_CTX_IS_EXT(ctx) ((ctx)->dev->venc_pdata->uses_ext)
#define MTK_ENC_IOVA_IS_34BIT(ctx) ((ctx)->dev->venc_pdata->uses_34bit)
/**
* struct mtk_vcodec_enc_pdata - compatible data for each IC
*
* @uses_ext: whether the encoder uses the extended firmware messaging format
* @min_bitrate: minimum supported encoding bitrate
* @max_bitrate: maximum supported encoding bitrate
* @capture_formats: array of supported capture formats
* @num_capture_formats: number of entries in capture_formats
* @output_formats: array of supported output formats
* @num_output_formats: number of entries in output_formats
* @core_id: stand for h264 or vp8 encode index
* @uses_34bit: whether the encoder uses 34-bit iova
*/
struct mtk_vcodec_enc_pdata {
bool uses_ext;
u64 min_bitrate;
u64 max_bitrate;
const struct mtk_video_fmt *capture_formats;
size_t num_capture_formats;
const struct mtk_video_fmt *output_formats;
size_t num_output_formats;
u8 core_id;
bool uses_34bit;
};
/*
* enum mtk_encode_param - General encoding parameters type
*/
enum mtk_encode_param {
MTK_ENCODE_PARAM_NONE = 0,
MTK_ENCODE_PARAM_BITRATE = (1 << 0),
MTK_ENCODE_PARAM_FRAMERATE = (1 << 1),
MTK_ENCODE_PARAM_INTRA_PERIOD = (1 << 2),
MTK_ENCODE_PARAM_FORCE_INTRA = (1 << 3),
MTK_ENCODE_PARAM_GOP_SIZE = (1 << 4),
};
/**
* struct mtk_enc_params - General encoding parameters
* @bitrate: target bitrate in bits per second
* @num_b_frame: number of b frames between p-frame
* @rc_frame: frame based rate control
* @rc_mb: macroblock based rate control
* @seq_hdr_mode: H.264 sequence header is encoded separately or joined
* with the first frame
* @intra_period: I frame period
* @gop_size: group of picture size, it's used as the intra frame period
* @framerate_num: frame rate numerator. ex: framerate_num=30 and
* framerate_denom=1 means FPS is 30
* @framerate_denom: frame rate denominator. ex: framerate_num=30 and
* framerate_denom=1 means FPS is 30
* @h264_max_qp: Max value for H.264 quantization parameter
* @h264_profile: V4L2 defined H.264 profile
* @h264_level: V4L2 defined H.264 level
* @force_intra: force/insert intra frame
*/
struct mtk_enc_params {
unsigned int bitrate;
unsigned int num_b_frame;
unsigned int rc_frame;
unsigned int rc_mb;
unsigned int seq_hdr_mode;
unsigned int intra_period;
unsigned int gop_size;
unsigned int framerate_num;
unsigned int framerate_denom;
unsigned int h264_max_qp;
unsigned int h264_profile;
unsigned int h264_level;
unsigned int force_intra;
};
/**
* struct mtk_vcodec_enc_ctx - Context (instance) private data.
*
* @type: type of encoder instance
* @dev: pointer to the mtk_vcodec_enc_dev of the device
* @list: link to ctx_list of mtk_vcodec_enc_dev
*
* @fh: struct v4l2_fh
* @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
* @q_data: store information of input and output queue of the context
* @id: index of the context that this structure describes
* @state: state of the context
* @param_change: indicate encode parameter type
* @enc_params: encoding parameters
*
* @enc_if: hooked encoder driver interface
* @drv_handle: driver handle for specific decode/encode instance
*
* @int_cond: variable used by the waitqueue
* @int_type: type of the last interrupt
* @queue: waitqueue that can be used to wait for this context to finish
* @irq_status: irq status
*
* @ctrl_hdl: handler for v4l2 framework
* @encode_work: worker for the encoding
* @empty_flush_buf: a fake size-0 capture buffer that indicates flush. Used for encoder.
* @is_flushing: set to true if flushing is in progress.
*
* @colorspace: enum v4l2_colorspace; supplemental to pixelformat
* @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
* @quantization: enum v4l2_quantization, colorspace quantization
* @xfer_func: enum v4l2_xfer_func, colorspace transfer function
*
* @q_mutex: vb2_queue mutex.
* @vpu_inst: vpu instance pointer.
*/
struct mtk_vcodec_enc_ctx {
enum mtk_instance_type type;
struct mtk_vcodec_enc_dev *dev;
struct list_head list;
struct v4l2_fh fh;
struct v4l2_m2m_ctx *m2m_ctx;
struct mtk_q_data q_data[2];
int id;
enum mtk_instance_state state;
enum mtk_encode_param param_change;
struct mtk_enc_params enc_params;
const struct venc_common_if *enc_if;
void *drv_handle;
int int_cond[MTK_VDEC_HW_MAX];
int int_type[MTK_VDEC_HW_MAX];
wait_queue_head_t queue[MTK_VDEC_HW_MAX];
unsigned int irq_status;
struct v4l2_ctrl_handler ctrl_hdl;
struct work_struct encode_work;
struct v4l2_m2m_buffer empty_flush_buf;
bool is_flushing;
enum v4l2_colorspace colorspace;
enum v4l2_ycbcr_encoding ycbcr_enc;
enum v4l2_quantization quantization;
enum v4l2_xfer_func xfer_func;
struct mutex q_mutex;
void *vpu_inst;
};
/**
* struct mtk_vcodec_enc_dev - driver data
* @v4l2_dev: V4L2 device to register video devices for.
* @vfd_enc: Video device for encoder.
*
* @m2m_dev_enc: m2m device for encoder.
* @plat_dev: platform device
* @ctx_list: list of struct mtk_vcodec_ctx
* @curr_ctx: The context that is waiting for codec hardware
*
* @reg_base: Mapped address of MTK Vcodec registers.
* @venc_pdata: encoder IC-specific data
*
* @fw_handler: used to communicate with the firmware.
* @id_counter: used to identify current opened instance
*
* @enc_mutex: encoder hardware lock.
* @dev_mutex: video_device lock
* @dev_ctx_lock: the lock of context list
* @encode_workqueue: encode work queue
*
* @enc_irq: h264 encoder irq resource
* @irqlock: protect data access by irq handler and work thread
*
* @pm: power management control
* @enc_capability: used to identify encode capability
* @dbgfs: debug log related information
*/
struct mtk_vcodec_enc_dev {
struct v4l2_device v4l2_dev;
struct video_device *vfd_enc;
struct v4l2_m2m_dev *m2m_dev_enc;
struct platform_device *plat_dev;
struct list_head ctx_list;
struct mtk_vcodec_enc_ctx *curr_ctx;
void __iomem *reg_base[NUM_MAX_VCODEC_REG_BASE];
const struct mtk_vcodec_enc_pdata *venc_pdata;
struct mtk_vcodec_fw *fw_handler;
u64 id_counter;
/* encoder hardware mutex lock */
struct mutex enc_mutex;
struct mutex dev_mutex;
spinlock_t dev_ctx_lock;
struct workqueue_struct *encode_workqueue;
int enc_irq;
spinlock_t irqlock;
struct mtk_vcodec_pm pm;
unsigned int enc_capability;
struct mtk_vcodec_dbgfs dbgfs;
};
static inline struct mtk_vcodec_enc_ctx *file_to_enc_ctx(struct file *filp)
{
return container_of(file_to_v4l2_fh(filp), struct mtk_vcodec_enc_ctx, fh);
}
static inline struct mtk_vcodec_enc_ctx *ctrl_to_enc_ctx(struct v4l2_ctrl *ctrl)
{
return container_of(ctrl->handler, struct mtk_vcodec_enc_ctx, ctrl_hdl);
}
/* Wake up context wait_queue */
static inline void
wake_up_enc_ctx(struct mtk_vcodec_enc_ctx *ctx, unsigned int reason, unsigned int hw_id)
{
ctx->int_cond[hw_id] = 1;
ctx->int_type[hw_id] = reason;
wake_up_interruptible(&ctx->queue[hw_id]);
}
#define mtk_venc_err(ctx, fmt, args...) \
mtk_vcodec_err((ctx)->id, (ctx)->dev->plat_dev, fmt, ##args)
#define mtk_venc_debug(ctx, fmt, args...) \
mtk_vcodec_debug((ctx)->id, (ctx)->dev->plat_dev, fmt, ##args)
#define mtk_v4l2_venc_err(ctx, fmt, args...) mtk_v4l2_err((ctx)->dev->plat_dev, fmt, ##args)
#define mtk_v4l2_venc_dbg(level, ctx, fmt, args...) \
mtk_v4l2_debug((ctx)->dev->plat_dev, level, fmt, ##args)
#endif /* _MTK_VCODEC_ENC_DRV_H_ */
@@ -0,0 +1,105 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Tiffany Lin <tiffany.lin@mediatek.com>
*/
#include <linux/clk.h>
#include <linux/of.h>
#include <linux/pm_runtime.h>
#include "mtk_vcodec_enc_drv.h"
#include "mtk_vcodec_enc_pm.h"
int mtk_vcodec_init_enc_clk(struct mtk_vcodec_enc_dev *mtkdev)
{
struct platform_device *pdev;
struct mtk_vcodec_pm *pm;
struct mtk_vcodec_clk *enc_clk;
struct mtk_vcodec_clk_info *clk_info;
int ret, i;
pdev = mtkdev->plat_dev;
pm = &mtkdev->pm;
memset(pm, 0, sizeof(struct mtk_vcodec_pm));
pm->dev = &pdev->dev;
enc_clk = &pm->venc_clk;
enc_clk->clk_num = of_property_count_strings(pdev->dev.of_node,
"clock-names");
if (enc_clk->clk_num > 0) {
enc_clk->clk_info = devm_kcalloc(&pdev->dev,
enc_clk->clk_num, sizeof(*clk_info),
GFP_KERNEL);
if (!enc_clk->clk_info)
return -ENOMEM;
} else {
dev_err(pm->dev, "[MTK VCODEC] Failed to get venc clock count");
return -EINVAL;
}
for (i = 0; i < enc_clk->clk_num; i++) {
clk_info = &enc_clk->clk_info[i];
ret = of_property_read_string_index(pdev->dev.of_node,
"clock-names", i, &clk_info->clk_name);
if (ret) {
dev_err(pm->dev, "[MTK VCODEC] venc failed to get clk name %d", i);
return ret;
}
clk_info->vcodec_clk = devm_clk_get(&pdev->dev,
clk_info->clk_name);
if (IS_ERR(clk_info->vcodec_clk)) {
dev_err(pm->dev, "[MTK VCODEC] venc devm_clk_get (%d)%s fail", i,
clk_info->clk_name);
return PTR_ERR(clk_info->vcodec_clk);
}
}
return 0;
}
int mtk_vcodec_enc_pw_on(struct mtk_vcodec_pm *pm)
{
int ret;
ret = pm_runtime_resume_and_get(pm->dev);
if (ret)
dev_err(pm->dev, "pm_runtime_resume_and_get fail: %d", ret);
return ret;
}
void mtk_vcodec_enc_pw_off(struct mtk_vcodec_pm *pm)
{
pm_runtime_put(pm->dev);
}
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm *pm)
{
struct mtk_vcodec_clk *enc_clk = &pm->venc_clk;
int ret, i = 0;
for (i = 0; i < enc_clk->clk_num; i++) {
ret = clk_prepare_enable(enc_clk->clk_info[i].vcodec_clk);
if (ret) {
dev_err(pm->dev, "[MTK VCODEC] venc clk_prepare_enable %d %s fail %d", i,
enc_clk->clk_info[i].clk_name, ret);
goto clkerr;
}
}
return;
clkerr:
for (i -= 1; i >= 0; i--)
clk_disable_unprepare(enc_clk->clk_info[i].vcodec_clk);
}
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm *pm)
{
struct mtk_vcodec_clk *enc_clk = &pm->venc_clk;
int i = 0;
for (i = enc_clk->clk_num - 1; i >= 0; i--)
clk_disable_unprepare(enc_clk->clk_info[i].vcodec_clk);
}
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Tiffany Lin <tiffany.lin@mediatek.com>
*/
#ifndef _MTK_VCODEC_ENC_PM_H_
#define _MTK_VCODEC_ENC_PM_H_
#include "mtk_vcodec_enc_drv.h"
int mtk_vcodec_init_enc_clk(struct mtk_vcodec_enc_dev *dev);
int mtk_vcodec_enc_pw_on(struct mtk_vcodec_pm *pm);
void mtk_vcodec_enc_pw_off(struct mtk_vcodec_pm *pm);
void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm *pm);
void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm *pm);
#endif /* _MTK_VCODEC_ENC_PM_H_ */
@@ -0,0 +1,819 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
* Daniel Hsiao <daniel.hsiao@mediatek.com>
* PoChun Lin <pochun.lin@mediatek.com>
*/
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include "../mtk_vcodec_enc_drv.h"
#include "../../common/mtk_vcodec_intr.h"
#include "../mtk_vcodec_enc.h"
#include "../mtk_vcodec_enc_pm.h"
#include "../venc_drv_base.h"
#include "../venc_ipi_msg.h"
#include "../venc_vpu_if.h"
static const char h264_filler_marker[] = {0x0, 0x0, 0x0, 0x1, 0xc};
#define H264_FILLER_MARKER_SIZE ARRAY_SIZE(h264_filler_marker)
#define VENC_PIC_BITSTREAM_BYTE_CNT 0x0098
/*
* enum venc_h264_frame_type - h264 encoder output bitstream frame type
*/
enum venc_h264_frame_type {
VENC_H264_IDR_FRM,
VENC_H264_I_FRM,
VENC_H264_P_FRM,
VENC_H264_B_FRM,
};
/*
* enum venc_h264_vpu_work_buf - h264 encoder buffer index
*/
enum venc_h264_vpu_work_buf {
VENC_H264_VPU_WORK_BUF_RC_INFO,
VENC_H264_VPU_WORK_BUF_RC_CODE,
VENC_H264_VPU_WORK_BUF_REC_LUMA,
VENC_H264_VPU_WORK_BUF_REC_CHROMA,
VENC_H264_VPU_WORK_BUF_REF_LUMA,
VENC_H264_VPU_WORK_BUF_REF_CHROMA,
VENC_H264_VPU_WORK_BUF_MV_INFO_1,
VENC_H264_VPU_WORK_BUF_MV_INFO_2,
VENC_H264_VPU_WORK_BUF_SKIP_FRAME,
VENC_H264_VPU_WORK_BUF_MAX,
};
/*
* enum venc_h264_bs_mode - for bs_mode argument in h264_enc_vpu_encode
*/
enum venc_h264_bs_mode {
H264_BS_MODE_SPS,
H264_BS_MODE_PPS,
H264_BS_MODE_FRAME,
};
/*
* struct venc_h264_vpu_config - Structure for h264 encoder configuration
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @input_fourcc: input fourcc
* @bitrate: target bitrate (in bps)
* @pic_w: picture width. Picture size is visible stream resolution, in pixels,
* to be used for display purposes; must be smaller or equal to buffer
* size.
* @pic_h: picture height
* @buf_w: buffer width. Buffer size is stream resolution in pixels aligned to
* hardware requirements.
* @buf_h: buffer height
* @gop_size: group of picture size (idr frame)
* @intra_period: intra frame period
* @framerate: frame rate in fps
* @profile: as specified in standard
* @level: as specified in standard
* @wfd: WFD mode 1:on, 0:off
*/
struct venc_h264_vpu_config {
u32 input_fourcc;
u32 bitrate;
u32 pic_w;
u32 pic_h;
u32 buf_w;
u32 buf_h;
u32 gop_size;
u32 intra_period;
u32 framerate;
u32 profile;
u32 level;
u32 wfd;
};
/*
* struct venc_h264_vpu_buf - Structure for buffer information
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @iova: IO virtual address
* @vpua: VPU side memory addr which is used by RC_CODE
* @size: buffer size (in bytes)
*/
struct venc_h264_vpu_buf {
u32 iova;
u32 vpua;
u32 size;
};
/*
* struct venc_h264_vsi - Structure for VPU driver control and info share
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* This structure is allocated in VPU side and shared to AP side.
* @config: h264 encoder configuration
* @work_bufs: working buffer information in VPU side
* The work_bufs here is for storing the 'size' info shared to AP side.
* The similar item in struct venc_h264_inst is for memory allocation
* in AP side. The AP driver will copy the 'size' from here to the one in
* struct mtk_vcodec_mem, then invoke mtk_vcodec_mem_alloc to allocate
* the buffer. After that, bypass the 'dma_addr' to the 'iova' field here for
* register setting in VPU side.
*/
struct venc_h264_vsi {
struct venc_h264_vpu_config config;
struct venc_h264_vpu_buf work_bufs[VENC_H264_VPU_WORK_BUF_MAX];
};
/**
* struct venc_h264_vpu_config_ext - Structure for h264 encoder configuration
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @input_fourcc: input fourcc
* @bitrate: target bitrate (in bps)
* @pic_w: picture width. Picture size is visible stream resolution, in pixels,
* to be used for display purposes; must be smaller or equal to buffer
* size.
* @pic_h: picture height
* @buf_w: buffer width. Buffer size is stream resolution in pixels aligned to
* hardware requirements.
* @buf_h: buffer height
* @gop_size: group of picture size (idr frame)
* @intra_period: intra frame period
* @framerate: frame rate in fps
* @profile: as specified in standard
* @level: as specified in standard
* @wfd: WFD mode 1:on, 0:off
* @max_qp: max quant parameter
* @min_qp: min quant parameter
* @reserved: reserved configs
*/
struct venc_h264_vpu_config_ext {
u32 input_fourcc;
u32 bitrate;
u32 pic_w;
u32 pic_h;
u32 buf_w;
u32 buf_h;
u32 gop_size;
u32 intra_period;
u32 framerate;
u32 profile;
u32 level;
u32 wfd;
u32 max_qp;
u32 min_qp;
u32 reserved[8];
};
/**
* struct venc_h264_vpu_buf_34 - Structure for 34-bit buffer information
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @iova: 34-bit IO virtual address
* @vpua: VPU side memory addr which is used by RC_CODE
* @size: buffer size (in bytes)
*/
struct venc_h264_vpu_buf_34 {
u64 iova;
u32 vpua;
u32 size;
};
/**
* struct venc_h264_vsi_34 - Structure for VPU driver control and info share
* Used for 34-bit iova sharing
* @config: h264 encoder configuration
* @work_bufs: working buffer information in VPU side
*/
struct venc_h264_vsi_34 {
struct venc_h264_vpu_config_ext config;
struct venc_h264_vpu_buf_34 work_bufs[VENC_H264_VPU_WORK_BUF_MAX];
};
/*
* struct venc_h264_inst - h264 encoder AP driver instance
* @hw_base: h264 encoder hardware register base
* @work_bufs: working buffer
* @pps_buf: buffer to store the pps bitstream
* @work_buf_allocated: working buffer allocated flag
* @frm_cnt: encoded frame count
* @prepend_hdr: when the v4l2 layer send VENC_SET_PARAM_PREPEND_HEADER cmd
* through h264_enc_set_param interface, it will set this flag and prepend the
* sps/pps in h264_enc_encode function.
* @vpu_inst: VPU instance to exchange information between AP and VPU
* @vsi: driver structure allocated by VPU side and shared to AP side for
* control and info share
* @vsi_34: driver structure allocated by VPU side and shared to AP side for
* control and info share, used for 34-bit iova sharing.
* @ctx: context for v4l2 layer integration
*/
struct venc_h264_inst {
void __iomem *hw_base;
struct mtk_vcodec_mem work_bufs[VENC_H264_VPU_WORK_BUF_MAX];
struct mtk_vcodec_mem pps_buf;
bool work_buf_allocated;
unsigned int frm_cnt;
unsigned int skip_frm_cnt;
unsigned int prepend_hdr;
struct venc_vpu_inst vpu_inst;
struct venc_h264_vsi *vsi;
struct venc_h264_vsi_34 *vsi_34;
struct mtk_vcodec_enc_ctx *ctx;
};
static inline u32 h264_read_reg(struct venc_h264_inst *inst, u32 addr)
{
return readl(inst->hw_base + addr);
}
static unsigned int h264_get_profile(struct venc_h264_inst *inst,
unsigned int profile)
{
switch (profile) {
case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
return 66;
case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
return 77;
case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
return 100;
case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
mtk_venc_err(inst->ctx, "unsupported CONSTRAINED_BASELINE");
return 0;
case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
mtk_venc_err(inst->ctx, "unsupported EXTENDED");
return 0;
default:
mtk_venc_debug(inst->ctx, "unsupported profile %d", profile);
return 100;
}
}
static unsigned int h264_get_level(struct venc_h264_inst *inst,
unsigned int level)
{
switch (level) {
case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
mtk_venc_err(inst->ctx, "unsupported 1B");
return 0;
case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
return 10;
case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
return 11;
case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
return 12;
case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
return 13;
case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
return 20;
case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
return 21;
case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
return 22;
case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
return 30;
case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
return 31;
case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
return 32;
case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
return 40;
case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
return 41;
case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
return 42;
case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
return 50;
case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
return 51;
default:
mtk_venc_debug(inst->ctx, "unsupported level %d", level);
return 31;
}
}
static void h264_enc_free_work_buf(struct venc_h264_inst *inst)
{
int i;
/* Except the SKIP_FRAME buffers,
* other buffers need to be freed by AP.
*/
for (i = 0; i < VENC_H264_VPU_WORK_BUF_MAX; i++) {
if (i != VENC_H264_VPU_WORK_BUF_SKIP_FRAME && inst->work_bufs[i].va)
mtk_vcodec_mem_free(inst->ctx, &inst->work_bufs[i]);
}
if (inst->pps_buf.va)
mtk_vcodec_mem_free(inst->ctx, &inst->pps_buf);
}
static int h264_enc_alloc_work_buf(struct venc_h264_inst *inst, bool is_34bit)
{
struct venc_h264_vpu_buf *wb = NULL;
struct venc_h264_vpu_buf_34 *wb_34 = NULL;
int i;
u32 vpua, wb_size;
int ret = 0;
if (is_34bit)
wb_34 = inst->vsi_34->work_bufs;
else
wb = inst->vsi->work_bufs;
for (i = 0; i < VENC_H264_VPU_WORK_BUF_MAX; i++) {
/*
* This 'wb' structure is set by VPU side and shared to AP for
* buffer allocation and IO virtual addr mapping. For most of
* the buffers, AP will allocate the buffer according to 'size'
* field and store the IO virtual addr in 'iova' field. There
* are two exceptions:
* (1) RC_CODE buffer, it's pre-allocated in the VPU side, and
* save the VPU addr in the 'vpua' field. The AP will translate
* the VPU addr to the corresponding IO virtual addr and store
* in 'iova' field for reg setting in VPU side.
* (2) SKIP_FRAME buffer, it's pre-allocated in the VPU side,
* and save the VPU addr in the 'vpua' field. The AP will
* translate the VPU addr to the corresponding AP side virtual
* address and do some memcpy access to move to bitstream buffer
* assigned by v4l2 layer.
*/
if (is_34bit) {
inst->work_bufs[i].size = wb_34[i].size;
vpua = wb_34[i].vpua;
wb_size = wb_34[i].size;
} else {
inst->work_bufs[i].size = wb[i].size;
vpua = wb[i].vpua;
wb_size = wb[i].size;
}
if (i == VENC_H264_VPU_WORK_BUF_SKIP_FRAME) {
struct mtk_vcodec_fw *handler;
handler = inst->vpu_inst.ctx->dev->fw_handler;
inst->work_bufs[i].va =
mtk_vcodec_fw_map_dm_addr(handler, vpua);
inst->work_bufs[i].dma_addr = 0;
} else {
ret = mtk_vcodec_mem_alloc(inst->ctx,
&inst->work_bufs[i]);
if (ret) {
mtk_venc_err(inst->ctx, "cannot allocate buf %d", i);
goto err_alloc;
}
/*
* This RC_CODE is pre-allocated by VPU and saved in VPU
* addr. So we need use memcpy to copy RC_CODE from VPU
* addr into IO virtual addr in 'iova' field for reg
* setting in VPU side.
*/
if (i == VENC_H264_VPU_WORK_BUF_RC_CODE) {
struct mtk_vcodec_fw *handler;
void *tmp_va;
handler = inst->vpu_inst.ctx->dev->fw_handler;
tmp_va = mtk_vcodec_fw_map_dm_addr(handler,
vpua);
memcpy(inst->work_bufs[i].va, tmp_va, wb_size);
}
}
if (is_34bit)
wb_34[i].iova = inst->work_bufs[i].dma_addr;
else
wb[i].iova = inst->work_bufs[i].dma_addr;
mtk_venc_debug(inst->ctx, "work_buf[%d] va=0x%p iova=%pad size=%zu",
i, inst->work_bufs[i].va,
&inst->work_bufs[i].dma_addr,
inst->work_bufs[i].size);
}
/* the pps_buf is used by AP side only */
inst->pps_buf.size = 128;
ret = mtk_vcodec_mem_alloc(inst->ctx, &inst->pps_buf);
if (ret) {
mtk_venc_err(inst->ctx, "cannot allocate pps_buf");
goto err_alloc;
}
return ret;
err_alloc:
h264_enc_free_work_buf(inst);
return ret;
}
static unsigned int h264_enc_wait_venc_done(struct venc_h264_inst *inst)
{
unsigned int irq_status = 0;
struct mtk_vcodec_enc_ctx *ctx = (struct mtk_vcodec_enc_ctx *)inst->ctx;
if (!mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS, 0)) {
irq_status = ctx->irq_status;
mtk_venc_debug(ctx, "irq_status %x <-", irq_status);
}
return irq_status;
}
static int h264_frame_type(unsigned int frm_cnt, unsigned int gop_size,
unsigned int intra_period)
{
if ((gop_size != 0 && (frm_cnt % gop_size) == 0) ||
(frm_cnt == 0 && gop_size == 0)) {
/* IDR frame */
return VENC_H264_IDR_FRM;
} else if ((intra_period != 0 && (frm_cnt % intra_period) == 0) ||
(frm_cnt == 0 && intra_period == 0)) {
/* I frame */
return VENC_H264_I_FRM;
} else {
return VENC_H264_P_FRM; /* Note: B frames are not supported */
}
}
static int h264_encode_sps(struct venc_h264_inst *inst,
struct mtk_vcodec_mem *bs_buf,
unsigned int *bs_size)
{
int ret = 0;
unsigned int irq_status;
ret = vpu_enc_encode(&inst->vpu_inst, H264_BS_MODE_SPS, NULL, bs_buf, NULL);
if (ret)
return ret;
irq_status = h264_enc_wait_venc_done(inst);
if (irq_status != MTK_VENC_IRQ_STATUS_SPS) {
mtk_venc_err(inst->ctx, "expect irq status %d", MTK_VENC_IRQ_STATUS_SPS);
return -EINVAL;
}
*bs_size = h264_read_reg(inst, VENC_PIC_BITSTREAM_BYTE_CNT);
mtk_venc_debug(inst->ctx, "bs size %d <-", *bs_size);
return ret;
}
static int h264_encode_pps(struct venc_h264_inst *inst,
struct mtk_vcodec_mem *bs_buf,
unsigned int *bs_size)
{
int ret = 0;
unsigned int irq_status;
ret = vpu_enc_encode(&inst->vpu_inst, H264_BS_MODE_PPS, NULL, bs_buf, NULL);
if (ret)
return ret;
irq_status = h264_enc_wait_venc_done(inst);
if (irq_status != MTK_VENC_IRQ_STATUS_PPS) {
mtk_venc_err(inst->ctx, "expect irq status %d", MTK_VENC_IRQ_STATUS_PPS);
return -EINVAL;
}
*bs_size = h264_read_reg(inst, VENC_PIC_BITSTREAM_BYTE_CNT);
mtk_venc_debug(inst->ctx, "bs size %d <-", *bs_size);
return ret;
}
static int h264_encode_header(struct venc_h264_inst *inst,
struct mtk_vcodec_mem *bs_buf,
unsigned int *bs_size)
{
int ret = 0;
unsigned int bs_size_sps;
unsigned int bs_size_pps;
ret = h264_encode_sps(inst, bs_buf, &bs_size_sps);
if (ret)
return ret;
ret = h264_encode_pps(inst, &inst->pps_buf, &bs_size_pps);
if (ret)
return ret;
memcpy(bs_buf->va + bs_size_sps, inst->pps_buf.va, bs_size_pps);
*bs_size = bs_size_sps + bs_size_pps;
return ret;
}
static int h264_encode_frame(struct venc_h264_inst *inst,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
unsigned int *bs_size)
{
int ret = 0;
unsigned int gop_size;
unsigned int intra_period;
unsigned int irq_status;
struct venc_frame_info frame_info;
struct mtk_vcodec_enc_ctx *ctx = inst->ctx;
mtk_venc_debug(ctx, "frm_cnt = %d\n", inst->frm_cnt);
if (MTK_ENC_IOVA_IS_34BIT(ctx)) {
gop_size = inst->vsi_34->config.gop_size;
intra_period = inst->vsi_34->config.intra_period;
} else {
gop_size = inst->vsi->config.gop_size;
intra_period = inst->vsi->config.intra_period;
}
frame_info.frm_count = inst->frm_cnt;
frame_info.skip_frm_count = inst->skip_frm_cnt;
frame_info.frm_type = h264_frame_type(inst->frm_cnt, gop_size,
intra_period);
mtk_venc_debug(ctx, "frm_count = %d,skip_frm_count =%d,frm_type=%d.\n",
frame_info.frm_count, frame_info.skip_frm_count,
frame_info.frm_type);
ret = vpu_enc_encode(&inst->vpu_inst, H264_BS_MODE_FRAME,
frm_buf, bs_buf, &frame_info);
if (ret)
return ret;
/*
* skip frame case: The skip frame buffer is composed by vpu side only,
* it does not trigger the hw, so skip the wait interrupt operation.
*/
if (inst->vpu_inst.state == VEN_IPI_MSG_ENC_STATE_SKIP) {
*bs_size = inst->vpu_inst.bs_size;
memcpy(bs_buf->va,
inst->work_bufs[VENC_H264_VPU_WORK_BUF_SKIP_FRAME].va,
*bs_size);
++inst->frm_cnt;
++inst->skip_frm_cnt;
return 0;
}
irq_status = h264_enc_wait_venc_done(inst);
if (irq_status != MTK_VENC_IRQ_STATUS_FRM) {
mtk_venc_err(ctx, "irq_status=%d failed", irq_status);
return -EIO;
}
*bs_size = h264_read_reg(inst, VENC_PIC_BITSTREAM_BYTE_CNT);
++inst->frm_cnt;
mtk_venc_debug(ctx, "frm %d bs_size %d key_frm %d <-",
inst->frm_cnt, *bs_size, inst->vpu_inst.is_key_frm);
return 0;
}
static void h264_encode_filler(struct venc_h264_inst *inst, void *buf,
int size)
{
unsigned char *p = buf;
if (size < H264_FILLER_MARKER_SIZE) {
mtk_venc_err(inst->ctx, "filler size too small %d", size);
return;
}
memcpy(p, h264_filler_marker, ARRAY_SIZE(h264_filler_marker));
size -= H264_FILLER_MARKER_SIZE;
p += H264_FILLER_MARKER_SIZE;
memset(p, 0xff, size);
}
static int h264_enc_init(struct mtk_vcodec_enc_ctx *ctx)
{
const bool is_ext = MTK_ENC_CTX_IS_EXT(ctx);
int ret = 0;
struct venc_h264_inst *inst;
inst = kzalloc_obj(*inst);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu_inst.ctx = ctx;
if (is_ext)
inst->vpu_inst.id = SCP_IPI_VENC_H264;
else
inst->vpu_inst.id = IPI_VENC_H264;
inst->hw_base = mtk_vcodec_get_reg_addr(inst->ctx->dev->reg_base, VENC_SYS);
ret = vpu_enc_init(&inst->vpu_inst);
if (MTK_ENC_IOVA_IS_34BIT(ctx))
inst->vsi_34 = (struct venc_h264_vsi_34 *)inst->vpu_inst.vsi;
else
inst->vsi = (struct venc_h264_vsi *)inst->vpu_inst.vsi;
if (ret)
kfree(inst);
else
ctx->drv_handle = inst;
return ret;
}
static int h264_enc_encode(void *handle,
enum venc_start_opt opt,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_done_result *result)
{
int ret = 0;
struct venc_h264_inst *inst = (struct venc_h264_inst *)handle;
struct mtk_vcodec_enc_ctx *ctx = inst->ctx;
mtk_venc_debug(ctx, "opt %d ->", opt);
enable_irq(ctx->dev->enc_irq);
switch (opt) {
case VENC_START_OPT_ENCODE_SEQUENCE_HEADER: {
unsigned int bs_size_hdr;
ret = h264_encode_header(inst, bs_buf, &bs_size_hdr);
if (ret)
goto encode_err;
result->bs_size = bs_size_hdr;
result->is_key_frm = false;
break;
}
case VENC_START_OPT_ENCODE_FRAME: {
int hdr_sz;
int hdr_sz_ext;
int filler_sz = 0;
const int bs_alignment = 128;
struct mtk_vcodec_mem tmp_bs_buf;
unsigned int bs_size_hdr;
unsigned int bs_size_frm;
if (!inst->prepend_hdr) {
ret = h264_encode_frame(inst, frm_buf, bs_buf,
&result->bs_size);
if (ret)
goto encode_err;
result->is_key_frm = inst->vpu_inst.is_key_frm;
break;
}
mtk_venc_debug(ctx, "h264_encode_frame prepend SPS/PPS");
ret = h264_encode_header(inst, bs_buf, &bs_size_hdr);
if (ret)
goto encode_err;
hdr_sz = bs_size_hdr;
hdr_sz_ext = (hdr_sz & (bs_alignment - 1));
if (hdr_sz_ext) {
filler_sz = bs_alignment - hdr_sz_ext;
if (hdr_sz_ext + H264_FILLER_MARKER_SIZE > bs_alignment)
filler_sz += bs_alignment;
h264_encode_filler(inst, bs_buf->va + hdr_sz,
filler_sz);
}
tmp_bs_buf.va = bs_buf->va + hdr_sz + filler_sz;
tmp_bs_buf.dma_addr = bs_buf->dma_addr + hdr_sz + filler_sz;
tmp_bs_buf.size = bs_buf->size - (hdr_sz + filler_sz);
ret = h264_encode_frame(inst, frm_buf, &tmp_bs_buf,
&bs_size_frm);
if (ret)
goto encode_err;
result->bs_size = hdr_sz + filler_sz + bs_size_frm;
mtk_venc_debug(ctx, "hdr %d filler %d frame %d bs %d",
hdr_sz, filler_sz, bs_size_frm, result->bs_size);
inst->prepend_hdr = 0;
result->is_key_frm = inst->vpu_inst.is_key_frm;
break;
}
default:
mtk_venc_err(ctx, "venc_start_opt %d not supported", opt);
ret = -EINVAL;
break;
}
encode_err:
disable_irq(ctx->dev->enc_irq);
mtk_venc_debug(ctx, "opt %d <-", opt);
return ret;
}
static void h264_enc_set_vsi_configs(struct venc_h264_inst *inst,
struct venc_enc_param *enc_prm)
{
inst->vsi->config.input_fourcc = enc_prm->input_yuv_fmt;
inst->vsi->config.bitrate = enc_prm->bitrate;
inst->vsi->config.pic_w = enc_prm->width;
inst->vsi->config.pic_h = enc_prm->height;
inst->vsi->config.buf_w = enc_prm->buf_width;
inst->vsi->config.buf_h = enc_prm->buf_height;
inst->vsi->config.gop_size = enc_prm->gop_size;
inst->vsi->config.framerate = enc_prm->frm_rate;
inst->vsi->config.intra_period = enc_prm->intra_period;
inst->vsi->config.profile =
h264_get_profile(inst, enc_prm->h264_profile);
inst->vsi->config.level =
h264_get_level(inst, enc_prm->h264_level);
inst->vsi->config.wfd = 0;
}
static void h264_enc_set_vsi_34_configs(struct venc_h264_inst *inst,
struct venc_enc_param *enc_prm)
{
inst->vsi_34->config.input_fourcc = enc_prm->input_yuv_fmt;
inst->vsi_34->config.bitrate = enc_prm->bitrate;
inst->vsi_34->config.pic_w = enc_prm->width;
inst->vsi_34->config.pic_h = enc_prm->height;
inst->vsi_34->config.buf_w = enc_prm->buf_width;
inst->vsi_34->config.buf_h = enc_prm->buf_height;
inst->vsi_34->config.gop_size = enc_prm->gop_size;
inst->vsi_34->config.framerate = enc_prm->frm_rate;
inst->vsi_34->config.intra_period = enc_prm->intra_period;
inst->vsi_34->config.profile =
h264_get_profile(inst, enc_prm->h264_profile);
inst->vsi_34->config.level =
h264_get_level(inst, enc_prm->h264_level);
inst->vsi_34->config.wfd = 0;
}
static int h264_enc_set_param(void *handle,
enum venc_set_param_type type,
struct venc_enc_param *enc_prm)
{
int ret = 0;
struct venc_h264_inst *inst = (struct venc_h264_inst *)handle;
struct mtk_vcodec_enc_ctx *ctx = inst->ctx;
const bool is_34bit = MTK_ENC_IOVA_IS_34BIT(ctx);
mtk_venc_debug(ctx, "->type=%d", type);
switch (type) {
case VENC_SET_PARAM_ENC:
if (is_34bit)
h264_enc_set_vsi_34_configs(inst, enc_prm);
else
h264_enc_set_vsi_configs(inst, enc_prm);
ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
if (ret)
break;
if (inst->work_buf_allocated) {
h264_enc_free_work_buf(inst);
inst->work_buf_allocated = false;
}
ret = h264_enc_alloc_work_buf(inst, is_34bit);
if (ret)
break;
inst->work_buf_allocated = true;
break;
case VENC_SET_PARAM_PREPEND_HEADER:
inst->prepend_hdr = 1;
mtk_venc_debug(ctx, "set prepend header mode");
break;
case VENC_SET_PARAM_FORCE_INTRA:
case VENC_SET_PARAM_GOP_SIZE:
case VENC_SET_PARAM_INTRA_PERIOD:
inst->frm_cnt = 0;
inst->skip_frm_cnt = 0;
fallthrough;
default:
ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
break;
}
return ret;
}
static int h264_enc_deinit(void *handle)
{
int ret = 0;
struct venc_h264_inst *inst = (struct venc_h264_inst *)handle;
ret = vpu_enc_deinit(&inst->vpu_inst);
if (inst->work_buf_allocated)
h264_enc_free_work_buf(inst);
kfree(inst);
return ret;
}
const struct venc_common_if venc_h264_if = {
.init = h264_enc_init,
.encode = h264_enc_encode,
.set_param = h264_enc_set_param,
.deinit = h264_enc_deinit,
};
@@ -0,0 +1,441 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
* PoChun Lin <pochun.lin@mediatek.com>
*/
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include "../mtk_vcodec_enc_drv.h"
#include "../../common/mtk_vcodec_intr.h"
#include "../mtk_vcodec_enc.h"
#include "../mtk_vcodec_enc_pm.h"
#include "../venc_drv_base.h"
#include "../venc_ipi_msg.h"
#include "../venc_vpu_if.h"
#define VENC_BITSTREAM_FRAME_SIZE 0x0098
#define VENC_BITSTREAM_HEADER_LEN 0x00e8
/* This ac_tag is vp8 frame tag. */
#define MAX_AC_TAG_SIZE 10
/*
* enum venc_vp8_vpu_work_buf - vp8 encoder buffer index
*/
enum venc_vp8_vpu_work_buf {
VENC_VP8_VPU_WORK_BUF_LUMA,
VENC_VP8_VPU_WORK_BUF_LUMA2,
VENC_VP8_VPU_WORK_BUF_LUMA3,
VENC_VP8_VPU_WORK_BUF_CHROMA,
VENC_VP8_VPU_WORK_BUF_CHROMA2,
VENC_VP8_VPU_WORK_BUF_CHROMA3,
VENC_VP8_VPU_WORK_BUF_MV_INFO,
VENC_VP8_VPU_WORK_BUF_BS_HEADER,
VENC_VP8_VPU_WORK_BUF_PROB_BUF,
VENC_VP8_VPU_WORK_BUF_RC_INFO,
VENC_VP8_VPU_WORK_BUF_RC_CODE,
VENC_VP8_VPU_WORK_BUF_RC_CODE2,
VENC_VP8_VPU_WORK_BUF_RC_CODE3,
VENC_VP8_VPU_WORK_BUF_MAX,
};
/*
* struct venc_vp8_vpu_config - Structure for vp8 encoder configuration
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @input_fourcc: input fourcc
* @bitrate: target bitrate (in bps)
* @pic_w: picture width. Picture size is visible stream resolution, in pixels,
* to be used for display purposes; must be smaller or equal to buffer
* size.
* @pic_h: picture height
* @buf_w: buffer width (with 16 alignment). Buffer size is stream resolution
* in pixels aligned to hardware requirements.
* @buf_h: buffer height (with 16 alignment)
* @gop_size: group of picture size (key frame)
* @framerate: frame rate in fps
* @ts_mode: temporal scalability mode (0: disable, 1: enable)
* support three temporal layers - 0: 7.5fps 1: 7.5fps 2: 15fps.
*/
struct venc_vp8_vpu_config {
u32 input_fourcc;
u32 bitrate;
u32 pic_w;
u32 pic_h;
u32 buf_w;
u32 buf_h;
u32 gop_size;
u32 framerate;
u32 ts_mode;
};
/*
* struct venc_vp8_vpu_buf - Structure for buffer information
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @iova: IO virtual address
* @vpua: VPU side memory addr which is used by RC_CODE
* @size: buffer size (in bytes)
*/
struct venc_vp8_vpu_buf {
u32 iova;
u32 vpua;
u32 size;
};
/*
* struct venc_vp8_vsi - Structure for VPU driver control and info share
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* This structure is allocated in VPU side and shared to AP side.
* @config: vp8 encoder configuration
* @work_bufs: working buffer information in VPU side
* The work_bufs here is for storing the 'size' info shared to AP side.
* The similar item in struct venc_vp8_inst is for memory allocation
* in AP side. The AP driver will copy the 'size' from here to the one in
* struct mtk_vcodec_mem, then invoke mtk_vcodec_mem_alloc to allocate
* the buffer. After that, bypass the 'dma_addr' to the 'iova' field here for
* register setting in VPU side.
*/
struct venc_vp8_vsi {
struct venc_vp8_vpu_config config;
struct venc_vp8_vpu_buf work_bufs[VENC_VP8_VPU_WORK_BUF_MAX];
};
/*
* struct venc_vp8_inst - vp8 encoder AP driver instance
* @hw_base: vp8 encoder hardware register base
* @work_bufs: working buffer
* @work_buf_allocated: working buffer allocated flag
* @frm_cnt: encoded frame count, it's used for I-frame judgement and
* reset when force intra cmd received.
* @ts_mode: temporal scalability mode (0: disable, 1: enable)
* support three temporal layers - 0: 7.5fps 1: 7.5fps 2: 15fps.
* @vpu_inst: VPU instance to exchange information between AP and VPU
* @vsi: driver structure allocated by VPU side and shared to AP side for
* control and info share
* @ctx: context for v4l2 layer integration
*/
struct venc_vp8_inst {
void __iomem *hw_base;
struct mtk_vcodec_mem work_bufs[VENC_VP8_VPU_WORK_BUF_MAX];
bool work_buf_allocated;
unsigned int frm_cnt;
unsigned int ts_mode;
struct venc_vpu_inst vpu_inst;
struct venc_vp8_vsi *vsi;
struct mtk_vcodec_enc_ctx *ctx;
};
static inline u32 vp8_enc_read_reg(struct venc_vp8_inst *inst, u32 addr)
{
return readl(inst->hw_base + addr);
}
static void vp8_enc_free_work_buf(struct venc_vp8_inst *inst)
{
int i;
/* Buffers need to be freed by AP. */
for (i = 0; i < VENC_VP8_VPU_WORK_BUF_MAX; i++) {
if (inst->work_bufs[i].size == 0)
continue;
mtk_vcodec_mem_free(inst->ctx, &inst->work_bufs[i]);
}
}
static int vp8_enc_alloc_work_buf(struct venc_vp8_inst *inst)
{
int i;
int ret = 0;
struct venc_vp8_vpu_buf *wb = inst->vsi->work_bufs;
for (i = 0; i < VENC_VP8_VPU_WORK_BUF_MAX; i++) {
if (wb[i].size == 0)
continue;
/*
* This 'wb' structure is set by VPU side and shared to AP for
* buffer allocation and IO virtual addr mapping. For most of
* the buffers, AP will allocate the buffer according to 'size'
* field and store the IO virtual addr in 'iova' field. For the
* RC_CODEx buffers, they are pre-allocated in the VPU side
* because they are inside VPU SRAM, and save the VPU addr in
* the 'vpua' field. The AP will translate the VPU addr to the
* corresponding IO virtual addr and store in 'iova' field.
*/
inst->work_bufs[i].size = wb[i].size;
ret = mtk_vcodec_mem_alloc(inst->ctx, &inst->work_bufs[i]);
if (ret) {
mtk_venc_err(inst->ctx, "cannot alloc work_bufs[%d]", i);
goto err_alloc;
}
/*
* This RC_CODEx is pre-allocated by VPU and saved in VPU addr.
* So we need use memcpy to copy RC_CODEx from VPU addr into IO
* virtual addr in 'iova' field for reg setting in VPU side.
*/
if (i == VENC_VP8_VPU_WORK_BUF_RC_CODE ||
i == VENC_VP8_VPU_WORK_BUF_RC_CODE2 ||
i == VENC_VP8_VPU_WORK_BUF_RC_CODE3) {
struct mtk_vcodec_fw *handler;
void *tmp_va;
handler = inst->vpu_inst.ctx->dev->fw_handler;
tmp_va = mtk_vcodec_fw_map_dm_addr(handler,
wb[i].vpua);
memcpy(inst->work_bufs[i].va, tmp_va, wb[i].size);
}
wb[i].iova = inst->work_bufs[i].dma_addr;
mtk_venc_debug(inst->ctx, "work_bufs[%d] va=0x%p,iova=%pad,size=%zu",
i, inst->work_bufs[i].va,
&inst->work_bufs[i].dma_addr,
inst->work_bufs[i].size);
}
return ret;
err_alloc:
vp8_enc_free_work_buf(inst);
return ret;
}
static unsigned int vp8_enc_wait_venc_done(struct venc_vp8_inst *inst)
{
unsigned int irq_status = 0;
struct mtk_vcodec_enc_ctx *ctx = (struct mtk_vcodec_enc_ctx *)inst->ctx;
if (!mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS, 0)) {
irq_status = ctx->irq_status;
mtk_venc_debug(ctx, "isr return %x", irq_status);
}
return irq_status;
}
/*
* Compose ac_tag, bitstream header and bitstream payload into
* one bitstream buffer.
*/
static int vp8_enc_compose_one_frame(struct venc_vp8_inst *inst,
struct mtk_vcodec_mem *bs_buf,
unsigned int *bs_size)
{
unsigned int not_key;
u32 bs_frm_size;
u32 bs_hdr_len;
unsigned int ac_tag_size;
u8 ac_tag[MAX_AC_TAG_SIZE];
u32 tag;
bs_frm_size = vp8_enc_read_reg(inst, VENC_BITSTREAM_FRAME_SIZE);
bs_hdr_len = vp8_enc_read_reg(inst, VENC_BITSTREAM_HEADER_LEN);
/* if a frame is key frame, not_key is 0 */
not_key = !inst->vpu_inst.is_key_frm;
tag = (bs_hdr_len << 5) | 0x10 | not_key;
ac_tag[0] = tag & 0xff;
ac_tag[1] = (tag >> 8) & 0xff;
ac_tag[2] = (tag >> 16) & 0xff;
/* key frame */
if (not_key == 0) {
ac_tag_size = MAX_AC_TAG_SIZE;
ac_tag[3] = 0x9d;
ac_tag[4] = 0x01;
ac_tag[5] = 0x2a;
ac_tag[6] = inst->vsi->config.pic_w;
ac_tag[7] = inst->vsi->config.pic_w >> 8;
ac_tag[8] = inst->vsi->config.pic_h;
ac_tag[9] = inst->vsi->config.pic_h >> 8;
} else {
ac_tag_size = 3;
}
if (bs_buf->size < bs_hdr_len + bs_frm_size + ac_tag_size) {
mtk_venc_err(inst->ctx, "bitstream buf size is too small(%zu)", bs_buf->size);
return -EINVAL;
}
/*
* (1) The vp8 bitstream header and body are generated by the HW vp8
* encoder separately at the same time. We cannot know the bitstream
* header length in advance.
* (2) From the vp8 spec, there is no stuffing byte allowed between the
* ac tag, bitstream header and bitstream body.
*/
memmove(bs_buf->va + bs_hdr_len + ac_tag_size,
bs_buf->va, bs_frm_size);
memcpy(bs_buf->va + ac_tag_size,
inst->work_bufs[VENC_VP8_VPU_WORK_BUF_BS_HEADER].va,
bs_hdr_len);
memcpy(bs_buf->va, ac_tag, ac_tag_size);
*bs_size = bs_frm_size + bs_hdr_len + ac_tag_size;
return 0;
}
static int vp8_enc_encode_frame(struct venc_vp8_inst *inst,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
unsigned int *bs_size)
{
int ret = 0;
unsigned int irq_status;
mtk_venc_debug(inst->ctx, "->frm_cnt=%d", inst->frm_cnt);
ret = vpu_enc_encode(&inst->vpu_inst, 0, frm_buf, bs_buf, NULL);
if (ret)
return ret;
irq_status = vp8_enc_wait_venc_done(inst);
if (irq_status != MTK_VENC_IRQ_STATUS_FRM) {
mtk_venc_err(inst->ctx, "irq_status=%d failed", irq_status);
return -EIO;
}
if (vp8_enc_compose_one_frame(inst, bs_buf, bs_size)) {
mtk_venc_err(inst->ctx, "vp8_enc_compose_one_frame failed");
return -EINVAL;
}
inst->frm_cnt++;
mtk_venc_debug(inst->ctx, "<-size=%d key_frm=%d", *bs_size, inst->vpu_inst.is_key_frm);
return ret;
}
static int vp8_enc_init(struct mtk_vcodec_enc_ctx *ctx)
{
int ret = 0;
struct venc_vp8_inst *inst;
inst = kzalloc_obj(*inst);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu_inst.ctx = ctx;
inst->vpu_inst.id = IPI_VENC_VP8;
inst->hw_base = mtk_vcodec_get_reg_addr(inst->ctx->dev->reg_base, VENC_LT_SYS);
ret = vpu_enc_init(&inst->vpu_inst);
inst->vsi = (struct venc_vp8_vsi *)inst->vpu_inst.vsi;
if (ret)
kfree(inst);
else
ctx->drv_handle = inst;
return ret;
}
static int vp8_enc_encode(void *handle,
enum venc_start_opt opt,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_done_result *result)
{
int ret = 0;
struct venc_vp8_inst *inst = (struct venc_vp8_inst *)handle;
struct mtk_vcodec_enc_ctx *ctx = inst->ctx;
enable_irq(ctx->dev->enc_irq);
switch (opt) {
case VENC_START_OPT_ENCODE_FRAME:
ret = vp8_enc_encode_frame(inst, frm_buf, bs_buf,
&result->bs_size);
if (ret)
goto encode_err;
result->is_key_frm = inst->vpu_inst.is_key_frm;
break;
default:
mtk_venc_err(ctx, "opt not support:%d", opt);
ret = -EINVAL;
break;
}
encode_err:
disable_irq(ctx->dev->enc_irq);
return ret;
}
static int vp8_enc_set_param(void *handle,
enum venc_set_param_type type,
struct venc_enc_param *enc_prm)
{
int ret = 0;
struct venc_vp8_inst *inst = (struct venc_vp8_inst *)handle;
mtk_venc_debug(inst->ctx, "->type=%d", type);
switch (type) {
case VENC_SET_PARAM_ENC:
inst->vsi->config.input_fourcc = enc_prm->input_yuv_fmt;
inst->vsi->config.bitrate = enc_prm->bitrate;
inst->vsi->config.pic_w = enc_prm->width;
inst->vsi->config.pic_h = enc_prm->height;
inst->vsi->config.buf_w = enc_prm->buf_width;
inst->vsi->config.buf_h = enc_prm->buf_height;
inst->vsi->config.gop_size = enc_prm->gop_size;
inst->vsi->config.framerate = enc_prm->frm_rate;
inst->vsi->config.ts_mode = inst->ts_mode;
ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
if (ret)
break;
if (inst->work_buf_allocated) {
vp8_enc_free_work_buf(inst);
inst->work_buf_allocated = false;
}
ret = vp8_enc_alloc_work_buf(inst);
if (ret)
break;
inst->work_buf_allocated = true;
break;
/*
* VENC_SET_PARAM_TS_MODE must be called before VENC_SET_PARAM_ENC
*/
case VENC_SET_PARAM_TS_MODE:
inst->ts_mode = 1;
mtk_venc_debug(inst->ctx, "set ts_mode");
break;
default:
ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
break;
}
return ret;
}
static int vp8_enc_deinit(void *handle)
{
int ret = 0;
struct venc_vp8_inst *inst = (struct venc_vp8_inst *)handle;
ret = vpu_enc_deinit(&inst->vpu_inst);
if (inst->work_buf_allocated)
vp8_enc_free_work_buf(inst);
kfree(inst);
return ret;
}
const struct venc_common_if venc_vp8_if = {
.init = vp8_enc_init,
.encode = vp8_enc_encode,
.set_param = vp8_enc_set_param,
.deinit = vp8_enc_deinit,
};
@@ -0,0 +1,53 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
* Jungchang Tsao <jungchang.tsao@mediatek.com>
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
#ifndef _VENC_DRV_BASE_
#define _VENC_DRV_BASE_
#include "mtk_vcodec_enc_drv.h"
#include "venc_drv_if.h"
struct venc_common_if {
/**
* (*init)() - initialize driver
* @ctx: [in] mtk v4l2 context
* @handle: [out] driver handle
*/
int (*init)(struct mtk_vcodec_enc_ctx *ctx);
/**
* (*encode)() - trigger encode
* @handle: [in] driver handle
* @opt: [in] encode option
* @frm_buf: [in] frame buffer to store input frame
* @bs_buf: [in] bitstream buffer to store output bitstream
* @result: [out] encode result
*/
int (*encode)(void *handle, enum venc_start_opt opt,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_done_result *result);
/**
* (*set_param)() - set driver's parameter
* @handle: [in] driver handle
* @type: [in] parameter type
* @in: [in] buffer to store the parameter
*/
int (*set_param)(void *handle, enum venc_set_param_type type,
struct venc_enc_param *in);
/**
* (*deinit)() - deinitialize driver.
* @handle: [in] driver handle
*/
int (*deinit)(void *handle);
};
#endif
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
* Jungchang Tsao <jungchang.tsao@mediatek.com>
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include "venc_drv_base.h"
#include "venc_drv_if.h"
#include "mtk_vcodec_enc.h"
#include "mtk_vcodec_enc_pm.h"
int venc_if_init(struct mtk_vcodec_enc_ctx *ctx, unsigned int fourcc)
{
int ret = 0;
switch (fourcc) {
case V4L2_PIX_FMT_VP8:
ctx->enc_if = &venc_vp8_if;
break;
case V4L2_PIX_FMT_H264:
ctx->enc_if = &venc_h264_if;
break;
default:
return -EINVAL;
}
mtk_venc_lock(ctx);
ret = ctx->enc_if->init(ctx);
mtk_venc_unlock(ctx);
return ret;
}
int venc_if_set_param(struct mtk_vcodec_enc_ctx *ctx,
enum venc_set_param_type type, struct venc_enc_param *in)
{
int ret = 0;
mtk_venc_lock(ctx);
ret = ctx->enc_if->set_param(ctx->drv_handle, type, in);
mtk_venc_unlock(ctx);
return ret;
}
int venc_if_encode(struct mtk_vcodec_enc_ctx *ctx,
enum venc_start_opt opt, struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_done_result *result)
{
int ret = 0;
unsigned long flags;
mtk_venc_lock(ctx);
spin_lock_irqsave(&ctx->dev->irqlock, flags);
ctx->dev->curr_ctx = ctx;
spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
ret = mtk_vcodec_enc_pw_on(&ctx->dev->pm);
if (ret)
goto venc_if_encode_pw_on_err;
mtk_vcodec_enc_clock_on(&ctx->dev->pm);
ret = ctx->enc_if->encode(ctx->drv_handle, opt, frm_buf,
bs_buf, result);
mtk_vcodec_enc_clock_off(&ctx->dev->pm);
mtk_vcodec_enc_pw_off(&ctx->dev->pm);
spin_lock_irqsave(&ctx->dev->irqlock, flags);
ctx->dev->curr_ctx = NULL;
spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
venc_if_encode_pw_on_err:
mtk_venc_unlock(ctx);
return ret;
}
int venc_if_deinit(struct mtk_vcodec_enc_ctx *ctx)
{
int ret = 0;
if (!ctx->drv_handle)
return 0;
mtk_venc_lock(ctx);
ret = ctx->enc_if->deinit(ctx->drv_handle);
mtk_venc_unlock(ctx);
ctx->drv_handle = NULL;
return ret;
}
@@ -0,0 +1,169 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
* Jungchang Tsao <jungchang.tsao@mediatek.com>
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
#ifndef _VENC_DRV_IF_H_
#define _VENC_DRV_IF_H_
#include "mtk_vcodec_enc_drv.h"
/*
* enum venc_yuv_fmt - The type of input yuv format
* (VPU related: If you change the order, you must also update the VPU codes.)
* @VENC_YUV_FORMAT_I420: I420 YUV format
* @VENC_YUV_FORMAT_YV12: YV12 YUV format
* @VENC_YUV_FORMAT_NV12: NV12 YUV format
* @VENC_YUV_FORMAT_NV21: NV21 YUV format
*/
enum venc_yuv_fmt {
VENC_YUV_FORMAT_I420 = 3,
VENC_YUV_FORMAT_YV12 = 5,
VENC_YUV_FORMAT_NV12 = 6,
VENC_YUV_FORMAT_NV21 = 7,
};
/*
* enum venc_start_opt - encode frame option used in venc_if_encode()
* @VENC_START_OPT_ENCODE_SEQUENCE_HEADER: encode SPS/PPS for H264
* @VENC_START_OPT_ENCODE_FRAME: encode normal frame
*/
enum venc_start_opt {
VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
VENC_START_OPT_ENCODE_FRAME,
};
/*
* enum venc_set_param_type - The type of set parameter used in
* venc_if_set_param()
* (VPU related: If you change the order, you must also update the VPU codes.)
* @VENC_SET_PARAM_ENC: set encoder parameters
* @VENC_SET_PARAM_FORCE_INTRA: force an intra frame
* @VENC_SET_PARAM_ADJUST_BITRATE: adjust bitrate (in bps)
* @VENC_SET_PARAM_ADJUST_FRAMERATE: set frame rate
* @VENC_SET_PARAM_GOP_SIZE: set IDR interval
* @VENC_SET_PARAM_INTRA_PERIOD: set I frame interval
* @VENC_SET_PARAM_SKIP_FRAME: set H264 skip one frame
* @VENC_SET_PARAM_PREPEND_HEADER: set H264 prepend SPS/PPS before IDR
* @VENC_SET_PARAM_TS_MODE: set VP8 temporal scalability mode
*/
enum venc_set_param_type {
VENC_SET_PARAM_ENC,
VENC_SET_PARAM_FORCE_INTRA,
VENC_SET_PARAM_ADJUST_BITRATE,
VENC_SET_PARAM_ADJUST_FRAMERATE,
VENC_SET_PARAM_GOP_SIZE,
VENC_SET_PARAM_INTRA_PERIOD,
VENC_SET_PARAM_SKIP_FRAME,
VENC_SET_PARAM_PREPEND_HEADER,
VENC_SET_PARAM_TS_MODE,
};
/*
* struct venc_enc_prm - encoder settings for VENC_SET_PARAM_ENC used in
* venc_if_set_param()
* @input_fourcc: input yuv format
* @h264_profile: V4L2 defined H.264 profile
* @h264_level: V4L2 defined H.264 level
* @width: image width
* @height: image height
* @buf_width: buffer width
* @buf_height: buffer height
* @frm_rate: frame rate in fps
* @intra_period: intra frame period
* @bitrate: target bitrate in bps
* @gop_size: group of picture size
*/
struct venc_enc_param {
enum venc_yuv_fmt input_yuv_fmt;
unsigned int h264_profile;
unsigned int h264_level;
unsigned int width;
unsigned int height;
unsigned int buf_width;
unsigned int buf_height;
unsigned int frm_rate;
unsigned int intra_period;
unsigned int bitrate;
unsigned int gop_size;
};
/**
* struct venc_frame_info - per-frame information to pass to the firmware.
*
* @frm_count: sequential number for this frame
* @skip_frm_count: number of frames skipped so far while decoding
* @frm_type: type of the frame, from enum venc_h264_frame_type
*/
struct venc_frame_info {
unsigned int frm_count; /* per frame update */
unsigned int skip_frm_count; /* per frame update */
unsigned int frm_type; /* per frame update */
};
/*
* struct venc_frm_buf - frame buffer information used in venc_if_encode()
* @fb_addr: plane frame buffer addresses
*/
struct venc_frm_buf {
struct mtk_vcodec_fb fb_addr[MTK_VCODEC_MAX_PLANES];
};
/*
* struct venc_done_result - This is return information used in venc_if_encode()
* @bs_size: output bitstream size
* @is_key_frm: output is key frame or not
*/
struct venc_done_result {
unsigned int bs_size;
bool is_key_frm;
};
extern const struct venc_common_if venc_h264_if;
extern const struct venc_common_if venc_vp8_if;
/*
* venc_if_init - Create the driver handle
* @ctx: device context
* @fourcc: encoder input format
* Return: 0 if creating handle successfully, otherwise it is failed.
*/
int venc_if_init(struct mtk_vcodec_enc_ctx *ctx, unsigned int fourcc);
/*
* venc_if_deinit - Release the driver handle
* @ctx: device context
* Return: 0 if releasing handle successfully, otherwise it is failed.
*/
int venc_if_deinit(struct mtk_vcodec_enc_ctx *ctx);
/*
* venc_if_set_param - Set parameter to driver
* @ctx: device context
* @type: parameter type
* @in: input parameter
* Return: 0 if setting param successfully, otherwise it is failed.
*/
int venc_if_set_param(struct mtk_vcodec_enc_ctx *ctx,
enum venc_set_param_type type,
struct venc_enc_param *in);
/*
* venc_if_encode - Encode one frame
* @ctx: device context
* @opt: encode frame option
* @frm_buf: input frame buffer information
* @bs_buf: output bitstream buffer information
* @result: encode result
* Return: 0 if encoding frame successfully, otherwise it is failed.
*/
int venc_if_encode(struct mtk_vcodec_enc_ctx *ctx,
enum venc_start_opt opt,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_done_result *result);
#endif /* _VENC_DRV_IF_H_ */
@@ -0,0 +1,244 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
* Daniel Hsiao <daniel.hsiao@mediatek.com>
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
#ifndef _VENC_IPI_MSG_H_
#define _VENC_IPI_MSG_H_
#define AP_IPIMSG_VENC_BASE 0xC000
#define VPU_IPIMSG_VENC_BASE 0xD000
/*
* enum venc_ipi_msg_id - message id between AP and VPU
* (ipi stands for inter-processor interrupt)
* @AP_IPIMSG_ENC_XXX: AP to VPU cmd message id
* @VPU_IPIMSG_ENC_XXX_DONE: VPU ack AP cmd message id
*/
enum venc_ipi_msg_id {
AP_IPIMSG_ENC_INIT = AP_IPIMSG_VENC_BASE,
AP_IPIMSG_ENC_SET_PARAM,
AP_IPIMSG_ENC_ENCODE,
AP_IPIMSG_ENC_DEINIT,
VPU_IPIMSG_ENC_INIT_DONE = VPU_IPIMSG_VENC_BASE,
VPU_IPIMSG_ENC_SET_PARAM_DONE,
VPU_IPIMSG_ENC_ENCODE_DONE,
VPU_IPIMSG_ENC_DEINIT_DONE,
};
/**
* struct venc_ap_ipi_msg_init - AP to VPU init cmd structure
* @msg_id: message id (AP_IPIMSG_XXX_ENC_INIT)
* @reserved: reserved for future use. vpu is running in 32bit. Without
* this reserved field, if kernel run in 64bit. this struct size
* will be different between kernel and vpu
* @venc_inst: AP encoder instance
* (struct venc_vp8_inst/venc_h264_inst *)
*/
struct venc_ap_ipi_msg_init {
uint32_t msg_id;
uint32_t reserved;
uint64_t venc_inst;
};
/**
* struct venc_ap_ipi_msg_set_param - AP to VPU set_param cmd structure
* @msg_id: message id (AP_IPIMSG_XXX_ENC_SET_PARAM)
* @vpu_inst_addr: VPU encoder instance addr
* (struct venc_vp8_vsi/venc_h264_vsi *)
* @param_id: parameter id (venc_set_param_type)
* @data_item: number of items in the data array
* @data: data array to store the set parameters
*/
struct venc_ap_ipi_msg_set_param {
uint32_t msg_id;
uint32_t vpu_inst_addr;
uint32_t param_id;
uint32_t data_item;
uint32_t data[8];
};
struct venc_ap_ipi_msg_set_param_ext {
struct venc_ap_ipi_msg_set_param base;
uint32_t data_ext[24];
};
/**
* struct venc_ap_ipi_msg_enc - AP to VPU enc cmd structure
* @msg_id: message id (AP_IPIMSG_XXX_ENC_ENCODE)
* @vpu_inst_addr: VPU encoder instance addr
* (struct venc_vp8_vsi/venc_h264_vsi *)
* @bs_mode: bitstream mode for h264
* (H264_BS_MODE_SPS/H264_BS_MODE_PPS/H264_BS_MODE_FRAME)
* @input_addr: pointer to input image buffer plane
* @bs_addr: pointer to output bit stream buffer
* @bs_size: bit stream buffer size
*/
struct venc_ap_ipi_msg_enc {
uint32_t msg_id;
uint32_t vpu_inst_addr;
uint32_t bs_mode;
uint32_t input_addr[3];
uint32_t bs_addr;
uint32_t bs_size;
};
/**
* struct venc_ap_ipi_msg_enc_ext - AP to SCP extended enc cmd structure
*
* @base: base msg structure
* @data_item: number of items in the data array
* @data: data array to store the set parameters
*/
struct venc_ap_ipi_msg_enc_ext {
struct venc_ap_ipi_msg_enc base;
uint32_t data_item;
uint32_t data[32];
};
/**
* struct venc_ap_ipi_msg_enc_ext_34 - AP to SCP extended enc cmd structure
* @msg_id: message id (AP_IPIMSG_XXX_ENC_ENCODE)
* @vpu_inst_addr: VPU encoder instance addr
* @bs_mode: bitstream mode for h264
* @reserved: for struct padding
* @input_addr: input frame buffer 34 bit address
* @bs_addr: output bitstream buffer 34 bit address
* @bs_size: bitstream buffer size
* @data_item: number of items in the data array
* @data: data array to store the set parameters
*/
struct venc_ap_ipi_msg_enc_ext_34 {
u32 msg_id;
u32 vpu_inst_addr;
u32 bs_mode;
u32 reserved;
u64 input_addr[3];
u64 bs_addr;
u32 bs_size;
u32 data_item;
u32 data[32];
};
/**
* struct venc_ap_ipi_msg_deinit - AP to VPU deinit cmd structure
* @msg_id: message id (AP_IPIMSG_XXX_ENC_DEINIT)
* @vpu_inst_addr: VPU encoder instance addr
* (struct venc_vp8_vsi/venc_h264_vsi *)
*/
struct venc_ap_ipi_msg_deinit {
uint32_t msg_id;
uint32_t vpu_inst_addr;
};
/*
* enum venc_ipi_msg_status - VPU ack AP cmd status
*/
enum venc_ipi_msg_status {
VENC_IPI_MSG_STATUS_OK,
VENC_IPI_MSG_STATUS_FAIL,
};
/**
* struct venc_vpu_ipi_msg_common - VPU ack AP cmd common structure
* @msg_id: message id (VPU_IPIMSG_XXX_DONE)
* @status: cmd status (venc_ipi_msg_status)
* @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
*/
struct venc_vpu_ipi_msg_common {
uint32_t msg_id;
uint32_t status;
uint64_t venc_inst;
};
/**
* struct venc_vpu_ipi_msg_init - VPU ack AP init cmd structure
* @msg_id: message id (VPU_IPIMSG_XXX_ENC_SET_PARAM_DONE)
* @status: cmd status (venc_ipi_msg_status)
* @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
* @vpu_inst_addr: VPU encoder instance addr
* (struct venc_vp8_vsi/venc_h264_vsi *)
* @venc_abi_version: ABI version of the firmware. Kernel can use it to
* ensure that it is compatible with the firmware.
* For MT8173 the value of this field is undefined and
* should not be used.
*/
struct venc_vpu_ipi_msg_init {
uint32_t msg_id;
uint32_t status;
uint64_t venc_inst;
uint32_t vpu_inst_addr;
uint32_t venc_abi_version;
};
/**
* struct venc_vpu_ipi_msg_set_param - VPU ack AP set_param cmd structure
* @msg_id: message id (VPU_IPIMSG_XXX_ENC_SET_PARAM_DONE)
* @status: cmd status (venc_ipi_msg_status)
* @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
* @param_id: parameter id (venc_set_param_type)
* @data_item: number of items in the data array
* @data: data array to store the return result
*/
struct venc_vpu_ipi_msg_set_param {
uint32_t msg_id;
uint32_t status;
uint64_t venc_inst;
uint32_t param_id;
uint32_t data_item;
uint32_t data[6];
};
/**
* enum venc_ipi_msg_enc_state - Type of encode state
* @VEN_IPI_MSG_ENC_STATE_FRAME: one frame being encoded
* @VEN_IPI_MSG_ENC_STATE_PART: bit stream buffer full
* @VEN_IPI_MSG_ENC_STATE_SKIP: encoded skip frame
* @VEN_IPI_MSG_ENC_STATE_ERROR: encounter error
*/
enum venc_ipi_msg_enc_state {
VEN_IPI_MSG_ENC_STATE_FRAME,
VEN_IPI_MSG_ENC_STATE_PART,
VEN_IPI_MSG_ENC_STATE_SKIP,
VEN_IPI_MSG_ENC_STATE_ERROR,
};
/**
* struct venc_vpu_ipi_msg_enc - VPU ack AP enc cmd structure
* @msg_id: message id (VPU_IPIMSG_XXX_ENC_ENCODE_DONE)
* @status: cmd status (venc_ipi_msg_status)
* @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
* @state: encode state (venc_ipi_msg_enc_state)
* @is_key_frm: whether the encoded frame is key frame
* @bs_size: encoded bitstream size
* @reserved: reserved for future use. vpu is running in 32bit. Without
* this reserved field, if kernel run in 64bit. this struct size
* will be different between kernel and vpu
*/
struct venc_vpu_ipi_msg_enc {
uint32_t msg_id;
uint32_t status;
uint64_t venc_inst;
uint32_t state;
uint32_t is_key_frm;
uint32_t bs_size;
uint32_t reserved;
};
/**
* struct venc_vpu_ipi_msg_deinit - VPU ack AP deinit cmd structure
* @msg_id: message id (VPU_IPIMSG_XXX_ENC_DEINIT_DONE)
* @status: cmd status (venc_ipi_msg_status)
* @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
*/
struct venc_vpu_ipi_msg_deinit {
uint32_t msg_id;
uint32_t status;
uint64_t venc_inst;
};
#endif /* _VENC_IPI_MSG_H_ */
@@ -0,0 +1,377 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: PoChun Lin <pochun.lin@mediatek.com>
*/
#include "mtk_vcodec_enc_drv.h"
#include "venc_ipi_msg.h"
#include "venc_vpu_if.h"
static void handle_enc_init_msg(struct venc_vpu_inst *vpu, const void *data)
{
const struct venc_vpu_ipi_msg_init *msg = data;
vpu->inst_addr = msg->vpu_inst_addr;
vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler,
msg->vpu_inst_addr);
/* Firmware version field value is unspecified on MT8173. */
if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VPU)
return;
/* Check firmware version. */
mtk_venc_debug(vpu->ctx, "firmware version: 0x%x\n", msg->venc_abi_version);
switch (msg->venc_abi_version) {
case 1:
break;
default:
mtk_venc_err(vpu->ctx, "unhandled firmware version 0x%x\n",
msg->venc_abi_version);
vpu->failure = 1;
break;
}
}
static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, const void *data)
{
const struct venc_vpu_ipi_msg_enc *msg = data;
vpu->state = msg->state;
vpu->bs_size = msg->bs_size;
vpu->is_key_frm = msg->is_key_frm;
}
static bool vpu_enc_check_ap_inst(struct mtk_vcodec_enc_dev *enc_dev, struct venc_vpu_inst *vpu)
{
struct mtk_vcodec_enc_ctx *ctx;
unsigned long flags;
int ret = false;
spin_lock_irqsave(&enc_dev->dev_ctx_lock, flags);
list_for_each_entry(ctx, &enc_dev->ctx_list, list) {
if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
ret = true;
break;
}
}
spin_unlock_irqrestore(&enc_dev->dev_ctx_lock, flags);
return ret;
}
static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv)
{
struct mtk_vcodec_enc_dev *enc_dev;
const struct venc_vpu_ipi_msg_common *msg = data;
struct venc_vpu_inst *vpu;
enc_dev = (struct mtk_vcodec_enc_dev *)priv;
vpu = (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
if (!priv || !vpu) {
pr_err(MTK_DBG_V4L2_STR "venc_inst is NULL, did the SCP hang or crash?");
return;
}
mtk_venc_debug(vpu->ctx, "msg_id %x inst %p status %d", msg->msg_id, vpu, msg->status);
if (!vpu_enc_check_ap_inst(enc_dev, vpu) || msg->msg_id < VPU_IPIMSG_ENC_INIT_DONE ||
msg->msg_id > VPU_IPIMSG_ENC_DEINIT_DONE) {
mtk_v4l2_venc_err(vpu->ctx, "venc msg id not correctly => 0x%x", msg->msg_id);
vpu->failure = -EINVAL;
goto error;
}
vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
if (vpu->failure) {
mtk_venc_err(vpu->ctx, "vpu enc status failure %d", vpu->failure);
goto error;
}
switch (msg->msg_id) {
case VPU_IPIMSG_ENC_INIT_DONE:
handle_enc_init_msg(vpu, data);
break;
case VPU_IPIMSG_ENC_SET_PARAM_DONE:
break;
case VPU_IPIMSG_ENC_ENCODE_DONE:
handle_enc_encode_msg(vpu, data);
break;
case VPU_IPIMSG_ENC_DEINIT_DONE:
break;
default:
mtk_venc_err(vpu->ctx, "unknown msg id %x", msg->msg_id);
break;
}
error:
vpu->signaled = 1;
}
static int vpu_enc_send_msg(struct venc_vpu_inst *vpu, void *msg,
int len)
{
int status;
if (!vpu->ctx->dev->fw_handler) {
mtk_venc_err(vpu->ctx, "inst dev is NULL");
return -EINVAL;
}
status = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, vpu->id, msg,
len, 2000);
if (status) {
mtk_venc_err(vpu->ctx, "vpu_ipi_send msg_id %x len %d fail %d",
*(uint32_t *)msg, len, status);
return -EINVAL;
}
if (vpu->failure)
return -EINVAL;
return 0;
}
int vpu_enc_init(struct venc_vpu_inst *vpu)
{
int status;
struct venc_ap_ipi_msg_init out = { };
init_waitqueue_head(&vpu->wq_hd);
vpu->signaled = 0;
vpu->failure = 0;
vpu->ctx->vpu_inst = vpu;
status = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
vpu_enc_ipi_handler, "venc",
vpu->ctx->dev);
if (status) {
mtk_venc_err(vpu->ctx, "vpu_ipi_register fail %d", status);
return -EINVAL;
}
out.msg_id = AP_IPIMSG_ENC_INIT;
out.venc_inst = (unsigned long)vpu;
if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_INIT fail");
return -EINVAL;
}
if (IS_ERR_OR_NULL(vpu->vsi)) {
mtk_venc_err(vpu->ctx, "invalid venc vsi");
return -EINVAL;
}
return 0;
}
static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst *vpu,
struct venc_enc_param *enc_prm)
{
unsigned int img_crop_right = enc_prm->buf_width - enc_prm->width;
return img_crop_right % 16;
}
static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param *enc_prm)
{
return round_up(enc_prm->height, 16) - enc_prm->height;
}
static unsigned int venc_enc_param_num_mb(struct venc_enc_param *enc_prm)
{
return DIV_ROUND_UP(enc_prm->width, 16) *
DIV_ROUND_UP(enc_prm->height, 16);
}
int vpu_enc_set_param(struct venc_vpu_inst *vpu,
enum venc_set_param_type id,
struct venc_enc_param *enc_param)
{
const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
size_t msg_size = is_ext ?
sizeof(struct venc_ap_ipi_msg_set_param_ext) :
sizeof(struct venc_ap_ipi_msg_set_param);
struct venc_ap_ipi_msg_set_param_ext out = { };
mtk_venc_debug(vpu->ctx, "id %d ->", id);
out.base.msg_id = AP_IPIMSG_ENC_SET_PARAM;
out.base.vpu_inst_addr = vpu->inst_addr;
out.base.param_id = id;
switch (id) {
case VENC_SET_PARAM_ENC:
if (is_ext) {
out.base.data_item = 3;
out.base.data[0] =
venc_enc_param_crop_right(vpu, enc_param);
out.base.data[1] =
venc_enc_param_crop_bottom(enc_param);
out.base.data[2] = venc_enc_param_num_mb(enc_param);
} else {
out.base.data_item = 0;
}
break;
case VENC_SET_PARAM_FORCE_INTRA:
out.base.data_item = 0;
break;
case VENC_SET_PARAM_ADJUST_BITRATE:
out.base.data_item = 1;
out.base.data[0] = enc_param->bitrate;
break;
case VENC_SET_PARAM_ADJUST_FRAMERATE:
out.base.data_item = 1;
out.base.data[0] = enc_param->frm_rate;
break;
case VENC_SET_PARAM_GOP_SIZE:
out.base.data_item = 1;
out.base.data[0] = enc_param->gop_size;
break;
case VENC_SET_PARAM_INTRA_PERIOD:
out.base.data_item = 1;
out.base.data[0] = enc_param->intra_period;
break;
case VENC_SET_PARAM_SKIP_FRAME:
out.base.data_item = 0;
break;
default:
mtk_venc_err(vpu->ctx, "id %d not supported", id);
return -EINVAL;
}
if (vpu_enc_send_msg(vpu, &out, msg_size)) {
mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
return -EINVAL;
}
mtk_venc_debug(vpu->ctx, "id %d <-", id);
return 0;
}
static int vpu_enc_encode_32bits(struct venc_vpu_inst *vpu,
unsigned int bs_mode,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_frame_info *frame_info)
{
const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
size_t msg_size = is_ext ?
sizeof(struct venc_ap_ipi_msg_enc_ext) :
sizeof(struct venc_ap_ipi_msg_enc);
struct venc_ap_ipi_msg_enc_ext out = { };
mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
out.base.msg_id = AP_IPIMSG_ENC_ENCODE;
out.base.vpu_inst_addr = vpu->inst_addr;
out.base.bs_mode = bs_mode;
if (frm_buf) {
if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
(frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
(frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
out.base.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
out.base.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
out.base.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
} else {
mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
return -EINVAL;
}
}
if (bs_buf) {
out.base.bs_addr = bs_buf->dma_addr;
out.base.bs_size = bs_buf->size;
}
if (is_ext && frame_info) {
out.data_item = 3;
out.data[0] = frame_info->frm_count;
out.data[1] = frame_info->skip_frm_count;
out.data[2] = frame_info->frm_type;
}
if (vpu_enc_send_msg(vpu, &out, msg_size)) {
mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
return -EINVAL;
}
return 0;
}
static int vpu_enc_encode_34bits(struct venc_vpu_inst *vpu,
unsigned int bs_mode,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_frame_info *frame_info)
{
struct venc_ap_ipi_msg_enc_ext_34 out = { };
size_t msg_size = sizeof(struct venc_ap_ipi_msg_enc_ext_34);
mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
out.msg_id = AP_IPIMSG_ENC_ENCODE;
out.vpu_inst_addr = vpu->inst_addr;
out.bs_mode = bs_mode;
if (frm_buf) {
if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
(frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
(frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
} else {
mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
return -EINVAL;
}
}
if (bs_buf) {
out.bs_addr = bs_buf->dma_addr;
out.bs_size = bs_buf->size;
}
if (frame_info) {
out.data_item = 3;
out.data[0] = frame_info->frm_count;
out.data[1] = frame_info->skip_frm_count;
out.data[2] = frame_info->frm_type;
}
if (vpu_enc_send_msg(vpu, &out, msg_size)) {
mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
return -EINVAL;
}
return 0;
}
int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_frame_info *frame_info)
{
int ret;
if (MTK_ENC_IOVA_IS_34BIT(vpu->ctx))
ret = vpu_enc_encode_34bits(vpu, bs_mode,
frm_buf, bs_buf, frame_info);
else
ret = vpu_enc_encode_32bits(vpu, bs_mode,
frm_buf, bs_buf, frame_info);
if (ret)
return ret;
mtk_venc_debug(vpu->ctx, "bs_mode %d state %d size %d key_frm %d <-",
bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
return 0;
}
int vpu_enc_deinit(struct venc_vpu_inst *vpu)
{
struct venc_ap_ipi_msg_deinit out = { };
out.msg_id = AP_IPIMSG_ENC_DEINIT;
out.vpu_inst_addr = vpu->inst_addr;
if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_DEINIT fail");
return -EINVAL;
}
return 0;
}
@@ -0,0 +1,50 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: PoChun Lin <pochun.lin@mediatek.com>
*/
#ifndef _VENC_VPU_IF_H_
#define _VENC_VPU_IF_H_
#include "venc_drv_if.h"
/*
* struct venc_vpu_inst - encoder VPU driver instance
* @wq_hd: wait queue used for vpu cmd trigger then wait vpu interrupt done
* @signaled: flag used for checking vpu interrupt done
* @failure: flag to show vpu cmd succeeds or not
* @state: enum venc_ipi_msg_enc_state
* @bs_size: bitstream size for skip frame case usage
* @is_key_frm: key frame flag
* @inst_addr: VPU instance addr
* @vsi: driver structure allocated by VPU side and shared to AP side for
* control and info share
* @id: the id of inter-processor interrupt
* @ctx: context for v4l2 layer integration
* @dev: device for v4l2 layer integration
*/
struct venc_vpu_inst {
wait_queue_head_t wq_hd;
int signaled;
int failure;
int state;
int bs_size;
int is_key_frm;
unsigned int inst_addr;
void *vsi;
int id;
struct mtk_vcodec_enc_ctx *ctx;
};
int vpu_enc_init(struct venc_vpu_inst *vpu);
int vpu_enc_set_param(struct venc_vpu_inst *vpu,
enum venc_set_param_type id,
struct venc_enc_param *param);
int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
struct venc_frm_buf *frm_buf,
struct mtk_vcodec_mem *bs_buf,
struct venc_frame_info *frame_info);
int vpu_enc_deinit(struct venc_vpu_inst *vpu);
#endif