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,680 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include "mtk_vcodec_intr.h"
#include "vdec_vcu_if.h"
#include "vdec_drv_base.h"
#include "mtk_vcodec_dec.h"
#include "mtk_vcodec_drv.h"
#include "vdec_drv_if.h"
static void put_fb_to_free(struct vdec_inst *inst, struct vdec_fb *fb)
{
struct ring_fb_list *list;
if (fb != NULL) {
list = &inst->vsi->list_free;
if (list->count == DEC_MAX_FB_NUM) {
mtk_vcodec_err(inst, "[FB] put fb free_list full");
return;
}
mtk_vcodec_debug(inst,
"[FB] put fb into free_list @(%p, %llx)",
fb->fb_base[0].va,
(u64)fb->fb_base[1].dma_addr);
list->fb_list[list->write_idx].vdec_fb_va = (u64)(fb->index + 1);
list->write_idx = (list->write_idx == DEC_MAX_FB_NUM - 1U) ?
0U : list->write_idx + 1U;
list->count++;
}
}
static void get_pic_info(struct vdec_inst *inst,
struct vdec_pic_info *pic)
{
if (inst == NULL)
return;
if (inst->vsi == NULL)
return;
memcpy(pic, &inst->vsi->pic, sizeof(struct vdec_pic_info));
mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d), bitdepth = %d, fourcc = %d\n",
pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h,
pic->bitdepth, pic->fourcc);
mtk_vcodec_debug(inst, "Y/C(%d, %d)", pic->fb_sz[0], pic->fb_sz[1]);
}
static void get_crop_info(struct vdec_inst *inst, struct v4l2_crop *cr)
{
if (inst == NULL)
return;
if (inst->vsi == NULL)
return;
cr->c.left = inst->vsi->crop.left;
cr->c.top = inst->vsi->crop.top;
cr->c.width = inst->vsi->crop.width;
cr->c.height = inst->vsi->crop.height;
mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
cr->c.left, cr->c.top, cr->c.width, cr->c.height);
}
static void get_dpb_size(struct vdec_inst *inst, unsigned int *dpb_sz)
{
if (inst == NULL)
return;
if (inst->vsi == NULL)
return;
*dpb_sz = inst->vsi->dec.dpb_sz + 1U;
mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
}
static int vdec_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
{
struct vdec_inst *inst = NULL;
int err = 0;
inst = kzalloc(sizeof(*inst), GFP_KERNEL);
if (!inst)
return -ENOMEM;
if (!ctx) {
err = -ENOMEM;
goto error_free_inst;
}
inst->ctx = ctx;
switch (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc) {
case V4L2_PIX_FMT_H264:
inst->vcu.id = IPI_VDEC_H264;
break;
case V4L2_PIX_FMT_H265:
inst->vcu.id = IPI_VDEC_H265;
break;
case V4L2_PIX_FMT_HEIF:
inst->vcu.id = IPI_VDEC_HEIF;
break;
case V4L2_PIX_FMT_VP8:
inst->vcu.id = IPI_VDEC_VP8;
break;
case V4L2_PIX_FMT_VP9:
inst->vcu.id = IPI_VDEC_VP9;
break;
case V4L2_PIX_FMT_MPEG4:
inst->vcu.id = IPI_VDEC_MPEG4;
break;
case V4L2_PIX_FMT_H263:
inst->vcu.id = IPI_VDEC_H263;
break;
case V4L2_PIX_FMT_MPEG1:
case V4L2_PIX_FMT_MPEG2:
inst->vcu.id = IPI_VDEC_MPEG12;
break;
case V4L2_PIX_FMT_WMV1:
case V4L2_PIX_FMT_WMV2:
case V4L2_PIX_FMT_WMV3:
case V4L2_PIX_FMT_WMVA:
case V4L2_PIX_FMT_WVC1:
inst->vcu.id = IPI_VDEC_WMV;
break;
case V4L2_PIX_FMT_RV30:
inst->vcu.id = IPI_VDEC_RV30;
break;
case V4L2_PIX_FMT_RV40:
inst->vcu.id = IPI_VDEC_RV40;
break;
case V4L2_PIX_FMT_AV1:
inst->vcu.id = IPI_VDEC_AV1;
break;
default:
mtk_vcodec_err(inst, "%s no fourcc", __func__);
break;
}
inst->vcu.dev = vcu_get_plat_device(ctx->dev->plat_dev);
if (inst->vcu.dev == NULL) {
mtk_vcodec_err(inst, "vcu device in not ready");
goto error_free_inst;
}
inst->vcu.ctx = ctx;
inst->vcu.handler = vcu_dec_ipi_handler;
*h_vdec = (unsigned long)inst;
mtk_vcodec_add_ctx_list(ctx);
err = vcu_dec_init(&inst->vcu);
if (err != 0) {
mtk_vcodec_err(inst, "%s err=%d", __func__, err);
goto error_free_inst;
}
inst->vsi = (struct vdec_vsi *)inst->vcu.vsi;
ctx->input_driven = inst->vsi->input_driven;
mtk_vcodec_debug(inst, "Decoder Instance >> %p", inst);
return 0;
error_free_inst:
if (ctx)
mtk_vcodec_del_ctx_list(ctx);
kfree(inst);
*h_vdec = (unsigned long)NULL;
return err;
}
static void vdec_deinit(unsigned long h_vdec)
{
struct vdec_inst *inst = (struct vdec_inst *)h_vdec;
mtk_vcodec_debug_enter(inst);
vcu_dec_deinit(&inst->vcu);
mtk_vcodec_del_ctx_list(inst->ctx);
kfree(inst);
}
static int vdec_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, unsigned int *src_chg)
{
struct vdec_inst *inst = (struct vdec_inst *)h_vdec;
struct vdec_vcu_inst *vcu = &inst->vcu;
int ret = 0;
unsigned int data[3];
uint64_t fb_dma[VIDEO_MAX_PLANES] = { 0 };
uint32_t num_planes;
unsigned int i = 0;
unsigned int bs_fourcc = inst->ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
unsigned int fm_fourcc = inst->ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;
unsigned int *errormap_info = &inst->ctx->errormap_info[0];
num_planes = fb ? inst->vsi->dec.fb_num_planes : 0U;
for (i = 0; i < num_planes; i++)
fb_dma[i] = (u64)fb->fb_base[i].dma_addr;
mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p num_planes %d",
inst->num_nalu, fb_dma[0], fb_dma[1], fb, num_planes);
/* bs NULL means flush decoder */
if (bs == NULL)
return vcu_dec_reset(vcu);
mtk_vcodec_debug(inst, "+ BS dma=0x%llx dmabuf=%p format=%c%c%c%c",
(uint64_t)bs->dma_addr, bs->dmabuf, bs_fourcc & 0xFF,
(bs_fourcc >> 8) & 0xFF, (bs_fourcc >> 16) & 0xFF,
(bs_fourcc >> 24) & 0xFF);
inst->vsi->dec.vdec_bs_va = (u64)(bs->index + 1);
inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
for (i = 0; i < num_planes; i++)
inst->vsi->dec.fb_dma[i] = fb_dma[i];
inst->vsi->dec.bs_fd = (uint64_t)get_mapped_fd(bs->dmabuf);
inst->vsi->dec.vdec_fb_va = (u64)0;
if (fb != NULL) {
inst->vsi->dec.vdec_fb_va = (u64)(fb->index + 1);
inst->vsi->dec.index = fb->index;
for (i = 0; i < num_planes; i++) {
inst->vsi->dec.fb_fd[i] =
(uint64_t)get_mapped_fd(fb->fb_base[i].dmabuf);
}
if (fb->dma_general_buf != 0) {
fb->general_buf_fd =
(uint32_t)get_mapped_fd(fb->dma_general_buf);
inst->vsi->general_buf_fd = fb->general_buf_fd;
inst->vsi->general_buf_size = fb->dma_general_buf->size;
inst->vsi->general_buf_dma = fb->dma_general_addr;
mtk_vcodec_debug(inst, "dma_general_buf dma_buf=%p fd=%d dma=%llx size=%lu",
fb->dma_general_buf, inst->vsi->general_buf_fd,
inst->vsi->general_buf_dma,
fb->dma_general_buf->size);
} else {
fb->general_buf_fd = -1;
inst->vsi->general_buf_fd = -1;
inst->vsi->general_buf_size = 0;
mtk_vcodec_debug(inst, "no general buf dmabuf");
}
} else {
if (!inst->ctx->input_driven)
inst->vsi->dec.index = 0xFF;
}
inst->vsi->dec.queued_frame_buf_count =
inst->ctx->dec_params.queued_frame_buf_count;
inst->vsi->dec.timestamp =
inst->ctx->dec_params.timestamp;
mtk_vcodec_debug(inst, "+ FB y_fd=%llx c_fd=%llx BS fd=%llx format=%c%c%c%c",
inst->vsi->dec.fb_fd[0], inst->vsi->dec.fb_fd[1],
inst->vsi->dec.bs_fd, fm_fourcc & 0xFF,
(fm_fourcc >> 8) & 0xFF, (fm_fourcc >> 16) & 0xFF,
(fm_fourcc >> 24) & 0xFF);
data[0] = (unsigned int)bs->size;
data[1] = (unsigned int)bs->length;
data[2] = (unsigned int)bs->flags;
ret = vcu_dec_start(vcu, data, 3, bs, fb);
*src_chg = inst->vsi->dec.vdec_changed_info;
*(errormap_info + bs->index % VB2_MAX_FRAME) =
inst->vsi->dec.error_map;
if ((*src_chg & VDEC_NEED_SEQ_HEADER) != 0U)
mtk_vcodec_err(inst, "- need first seq header -");
else if ((*src_chg & VDEC_RES_CHANGE) != 0U)
mtk_vcodec_debug(inst, "- resolution changed -");
else if ((*src_chg & VDEC_HW_NOT_SUPPORT) != 0U)
mtk_vcodec_err(inst, "- unsupported -");
/*ack timeout means vpud has crashed*/
if (ret == -EIO) {
mtk_vcodec_err(inst, "- IPI msg ack timeout -");
*src_chg = VDEC_HW_NOT_SUPPORT;
}
if (bs->dmabuf != NULL)
close_mapped_fd((unsigned int)inst->vsi->dec.bs_fd);
if (fb != NULL) {
for (i = 0; i < num_planes; i++)
close_mapped_fd((unsigned int)inst->vsi->dec.fb_fd[i]);
}
if (ret < 0 || ((*src_chg & VDEC_HW_NOT_SUPPORT) != 0U)
|| ((*src_chg & VDEC_NEED_SEQ_HEADER) != 0U))
goto err_free_fb_out;
inst->ctx->input_driven = inst->vsi->input_driven;
inst->num_nalu++;
return ret;
err_free_fb_out:
put_fb_to_free(inst, fb);
mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, ret);
return ret;
}
static void vdec_get_bs(struct vdec_inst *inst,
struct ring_bs_list *list,
struct mtk_vcodec_mem **out_bs)
{
u64 bs_index;
unsigned long vdec_bs_va;
struct mtk_vcodec_mem *bs;
if (list->count == 0) {
mtk_vcodec_debug(inst, "[BS] there is no bs");
*out_bs = NULL;
return;
}
bs_index = list->vdec_bs_va_list[list->read_idx];
vdec_bs_va = (unsigned long)inst->ctx->bs_list[bs_index];
bs = (struct mtk_vcodec_mem *)vdec_bs_va;
*out_bs = bs;
mtk_vcodec_debug(inst, "[BS] get free bs %lld %lx", bs_index, vdec_bs_va);
list->read_idx = (list->read_idx == DEC_MAX_BS_NUM - 1) ?
0 : list->read_idx + 1;
list->count--;
}
static void vdec_get_fb(struct vdec_inst *inst,
struct ring_fb_list *list,
bool disp_list, struct vdec_fb **out_fb)
{
u64 fb_index;
unsigned long vdec_fb_va;
struct vdec_fb *fb;
if (list->count == 0) {
mtk_vcodec_debug(inst, "[FB] there is no %s fb",
disp_list ? "disp" : "free");
*out_fb = NULL;
return;
}
fb_index = (u64)list->fb_list[list->read_idx].vdec_fb_va;
vdec_fb_va = (unsigned long)inst->ctx->fb_list[fb_index];
fb = (struct vdec_fb *)vdec_fb_va;
if (fb == NULL)
return;
fb->timestamp = list->fb_list[list->read_idx].timestamp;
if (disp_list)
fb->status |= FB_ST_DISPLAY;
else
fb->status |= FB_ST_FREE;
*out_fb = fb;
mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d ts=%llu %lld %lx gbuf fd %d dma %p",
disp_list ? "disp" : "free",
fb->status, list->fb_list[list->read_idx].poc,
list->fb_list[list->read_idx].timestamp,
list->fb_list[list->read_idx].vdec_fb_va, vdec_fb_va,
fb->general_buf_fd, fb->dma_general_buf);
list->read_idx = (list->read_idx == DEC_MAX_FB_NUM - 1U) ?
0U : list->read_idx + 1U;
list->count--;
}
static void get_supported_format(struct vdec_inst *inst,
struct mtk_video_fmt *video_fmt)
{
unsigned int i = 0;
inst->vcu.ctx = inst->ctx;
vcu_dec_query_cap(&inst->vcu, GET_PARAM_CAPABILITY_SUPPORTED_FORMATS,
video_fmt);
for (i = 0; i < MTK_MAX_DEC_CODECS_SUPPORT; i++) {
if (video_fmt[i].fourcc != 0) {
mtk_vcodec_debug(inst, "video_formats[%d] fourcc %d type %d num_planes %d\n",
i, video_fmt[i].fourcc, video_fmt[i].type,
video_fmt[i].num_planes);
}
}
}
static void get_frame_sizes(struct vdec_inst *inst,
struct mtk_codec_framesizes *codec_framesizes)
{
unsigned int i = 0;
inst->vcu.ctx = inst->ctx;
vcu_dec_query_cap(&inst->vcu, GET_PARAM_CAPABILITY_FRAME_SIZES,
codec_framesizes);
for (i = 0; i < MTK_MAX_DEC_CODECS_SUPPORT; i++) {
if (codec_framesizes[i].fourcc != 0) {
mtk_vcodec_debug(inst,
"codec_fs[%d] fourcc %d s %d %d %d %d %d %d P %d L %d\n",
i, codec_framesizes[i].fourcc,
codec_framesizes[i].stepwise.min_width,
codec_framesizes[i].stepwise.max_width,
codec_framesizes[i].stepwise.step_width,
codec_framesizes[i].stepwise.min_height,
codec_framesizes[i].stepwise.max_height,
codec_framesizes[i].stepwise.step_height,
codec_framesizes[i].profile,
codec_framesizes[i].level);
}
}
}
static void get_color_desc(struct vdec_inst *inst,
struct mtk_color_desc *color_desc)
{
inst->vcu.ctx = inst->ctx;
memcpy(color_desc, &inst->vsi->color_desc, sizeof(*color_desc));
}
static void get_aspect_ratio(struct vdec_inst *inst, unsigned int *aspect_ratio)
{
if (inst->vsi == NULL)
return;
inst->vcu.ctx = inst->ctx;
*aspect_ratio = inst->vsi->aspect_ratio;
}
static void get_supported_fix_buffers(struct vdec_inst *inst,
unsigned int *supported)
{
inst->vcu.ctx = inst->ctx;
if (inst->vsi != NULL)
*supported = inst->vsi->fix_buffers;
}
static void get_supported_fix_buffers_svp(struct vdec_inst *inst,
unsigned int *supported)
{
inst->vcu.ctx = inst->ctx;
if (inst->vsi != NULL)
*supported = inst->vsi->fix_buffers_svp;
}
static void get_interlacing(struct vdec_inst *inst,
unsigned int *interlacing)
{
inst->vcu.ctx = inst->ctx;
if (inst->vsi != NULL)
*interlacing = inst->vsi->interlacing;
}
static void get_codec_type(struct vdec_inst *inst,
unsigned int *codec_type)
{
inst->vcu.ctx = inst->ctx;
if (inst->vsi != NULL)
*codec_type = inst->vsi->codec_type;
}
static void get_input_driven(struct vdec_inst *inst,
unsigned int *input_driven)
{
inst->vcu.ctx = inst->ctx;
if (inst->vsi != NULL)
*input_driven = inst->vsi->input_driven;
}
static int vdec_get_param(unsigned long h_vdec,
enum vdec_get_param_type type, void *out)
{
struct vdec_inst *inst = (struct vdec_inst *)h_vdec;
int ret = 0;
if (inst == NULL)
return -EINVAL;
switch (type) {
case GET_PARAM_FREE_BITSTREAM_BUFFER:
if (inst->vsi == NULL)
return -EINVAL;
vdec_get_bs(inst, &inst->vsi->list_free_bs, out);
break;
case GET_PARAM_DISP_FRAME_BUFFER:
{
struct vdec_fb *pfb;
if (inst->vsi == NULL)
return -EINVAL;
vdec_get_fb(inst, &inst->vsi->list_disp, true, out);
pfb = *((struct vdec_fb **)out);
if (pfb != NULL) {
if (pfb->general_buf_fd >= 0) {
mtk_vcodec_debug(inst, "free pfb->general_buf_fd:%d pfb->dma_general_buf %p\n",
pfb->general_buf_fd,
pfb->dma_general_buf);
close_mapped_fd((unsigned int)
pfb->general_buf_fd);
pfb->general_buf_fd = -1;
}
}
break;
}
case GET_PARAM_FREE_FRAME_BUFFER:
{
struct vdec_fb *pfb;
int i;
if (inst->vsi == NULL)
return -EINVAL;
vdec_get_fb(inst, &inst->vsi->list_free, false, out);
pfb = *((struct vdec_fb **)out);
if (pfb != NULL) {
for (i = 0; i < pfb->num_planes; i++) {
if (pfb->fb_base[i].buf_fd >= 0) {
mtk_vcodec_debug(inst, "free pfb->fb_base[%d].buf_fd:%llx\n",
i, pfb->fb_base[i].buf_fd);
close_mapped_fd((unsigned int)
pfb->fb_base[i].buf_fd);
pfb->fb_base[i].buf_fd = -1;
}
}
if (pfb->general_buf_fd >= 0) {
mtk_vcodec_debug(inst, "free pfb->general_buf_fd:%d pfb->dma_general_buf %p\n",
pfb->general_buf_fd,
pfb->dma_general_buf);
close_mapped_fd((unsigned int)
pfb->general_buf_fd);
pfb->general_buf_fd = -1;
}
}
break;
}
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
get_dpb_size(inst, out);
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
case GET_PARAM_CAPABILITY_SUPPORTED_FORMATS:
get_supported_format(inst, out);
break;
case GET_PARAM_CAPABILITY_FRAME_SIZES:
get_frame_sizes(inst, out);
break;
case GET_PARAM_COLOR_DESC:
if (inst->vsi == NULL)
return -EINVAL;
get_color_desc(inst, out);
break;
case GET_PARAM_ASPECT_RATIO:
get_aspect_ratio(inst, out);
break;
case GET_PARAM_PLATFORM_SUPPORTED_FIX_BUFFERS:
get_supported_fix_buffers(inst, out);
break;
case GET_PARAM_PLATFORM_SUPPORTED_FIX_BUFFERS_SVP:
get_supported_fix_buffers_svp(inst, out);
break;
case GET_PARAM_INTERLACING:
get_interlacing(inst, out);
break;
case GET_PARAM_CODEC_TYPE:
get_codec_type(inst, out);
break;
case GET_PARAM_INPUT_DRIVEN:
get_input_driven(inst, out);
break;
case GET_PARAM_DEC_LOG:
if (out == NULL) {
pr_info("%s, out is null", __func__);
return -EINVAL;
}
ret = vcu_get_log(out, LOG_PROPERTY_SIZE);
break;
default:
mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
ret = -EINVAL;
break;
}
return ret;
}
static int vdec_set_param(unsigned long h_vdec,
enum vdec_set_param_type type, void *in)
{
struct vdec_inst *inst = (struct vdec_inst *)h_vdec;
uint64_t size;
int ret = 0;
if (inst == NULL)
return -EINVAL;
switch (type) {
case SET_PARAM_FRAME_SIZE:
case SET_PARAM_SET_FIXED_MAX_OUTPUT_BUFFER:
vcu_dec_set_param(&inst->vcu, (unsigned int)type, in, 2U);
break;
case SET_PARAM_DECODE_MODE:
case SET_PARAM_NAL_SIZE_LENGTH:
case SET_PARAM_WAIT_KEY_FRAME:
case SET_PARAM_OPERATING_RATE:
case SET_PARAM_TOTAL_FRAME_BUFQ_COUNT:
vcu_dec_set_param(&inst->vcu, (unsigned int)type, in, 1U);
break;
case SET_PARAM_UFO_MODE:
break;
case SET_PARAM_CRC_PATH:
if (inst->vsi == NULL)
return -EINVAL;
size = strlen((char *) *(uintptr_t *)in);
memcpy(inst->vsi->crc_path, (void *) *(uintptr_t *)in, size);
break;
case SET_PARAM_GOLDEN_PATH:
if (inst->vsi == NULL)
return -EINVAL;
size = strlen((char *) *(uintptr_t *)in);
memcpy(inst->vsi->golden_path, (void *) *(uintptr_t *)in, size);
break;
case SET_PARAM_FB_NUM_PLANES:
if (inst->vsi == NULL)
return -EINVAL;
inst->vsi->dec.fb_num_planes = *(unsigned int *)in;
break;
case SET_PARAM_DEC_LOG:
if (in == NULL) {
pr_info("%s, in is null", __func__);
return -EINVAL;
}
ret = vcu_set_log((char *) in);
break;
default:
mtk_vcodec_err(inst, "invalid set parameter type=%d\n", type);
ret = -EINVAL;
break;
}
return ret;
}
static struct vdec_common_if vdec_if = {
vdec_init,
vdec_decode,
vdec_get_param,
vdec_set_param,
vdec_deinit,
};
struct vdec_common_if *get_dec_common_if(void)
{
return &vdec_if;
}
@@ -0,0 +1,498 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include "../vdec_drv_if.h"
#include "../mtk_vcodec_util.h"
#include "../mtk_vcodec_dec.h"
#include "../mtk_vcodec_intr.h"
#include "../vdec_vpu_if.h"
#include "../vdec_drv_base.h"
#define NAL_NON_IDR_SLICE 0x01
#define NAL_IDR_SLICE 0x05
#define NAL_H264_PPS 0x08
#define NAL_TYPE(value) ((value) & 0x1F)
#define BUF_PREDICTION_SZ (32 * 1024)
#define MB_UNIT_LEN 16
/* motion vector size (bytes) for every macro block */
#define HW_MB_STORE_SZ 64
#define H264_MAX_FB_NUM 17
#define HDR_PARSING_BUF_SZ 1024
/**
* struct h264_fb - h264 decode frame buffer information
* @vdec_fb_va : virtual address of struct vdec_fb
* @y_fb_dma : dma address of Y frame buffer (luma)
* @c_fb_dma : dma address of C frame buffer (chroma)
* @poc : picture order count of frame buffer
* @reserved : for 8 bytes alignment
*/
struct h264_fb {
uint64_t vdec_fb_va;
uint64_t y_fb_dma;
uint64_t c_fb_dma;
int32_t poc;
uint32_t reserved;
};
/**
* struct h264_ring_fb_list - ring frame buffer list
* @fb_list : frame buffer arrary
* @read_idx : read index
* @write_idx : write index
* @count : buffer count in list
*/
struct h264_ring_fb_list {
struct h264_fb fb_list[H264_MAX_FB_NUM];
unsigned int read_idx;
unsigned int write_idx;
unsigned int count;
unsigned int reserved;
};
/**
* struct vdec_h264_dec_info - decode information
* @dpb_sz : decoding picture buffer size
* @resolution_changed : resoltion change happen
* @realloc_mv_buf : flag to notify driver to re-allocate mv buffer
* @reserved : for 8 bytes alignment
* @bs_dma : Input bit-stream buffer dma address
* @y_fb_dma : Y frame buffer dma address
* @c_fb_dma : C frame buffer dma address
* @vdec_fb_va : VDEC frame buffer struct virtual address
*/
struct vdec_h264_dec_info {
uint32_t dpb_sz;
uint32_t resolution_changed;
uint32_t realloc_mv_buf;
uint32_t reserved;
uint64_t bs_dma;
uint64_t y_fb_dma;
uint64_t c_fb_dma;
uint64_t vdec_fb_va;
};
/**
* struct vdec_h264_vsi - shared memory for decode information exchange
* between VPU and Host.
* The memory is allocated by VPU then mapping to Host
* in vpu_dec_init() and freed in vpu_dec_deinit()
* by VPU.
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @hdr_buf : Header parsing buffer (AP-W, VPU-R)
* @pred_buf_dma : HW working predication buffer dma address (AP-W, VPU-R)
* @mv_buf_dma : HW working motion vector buffer dma address (AP-W, VPU-R)
* @list_free : free frame buffer ring list (AP-W/R, VPU-W)
* @list_disp : display frame buffer ring list (AP-R, VPU-W)
* @dec : decode information (AP-R, VPU-W)
* @pic : picture information (AP-R, VPU-W)
* @crop : crop information (AP-R, VPU-W)
*/
struct vdec_h264_vsi {
unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
uint64_t pred_buf_dma;
uint64_t mv_buf_dma[H264_MAX_FB_NUM];
struct h264_ring_fb_list list_free;
struct h264_ring_fb_list list_disp;
struct vdec_h264_dec_info dec;
struct vdec_pic_info pic;
struct v4l2_rect crop;
};
/**
* struct vdec_h264_inst - h264 decoder instance
* @num_nalu : how many nalus be decoded
* @ctx : point to mtk_vcodec_ctx
* @pred_buf : HW working predication buffer
* @mv_buf : HW working motion vector buffer
* @vpu : VPU instance
* @vsi : VPU shared information
*/
struct vdec_h264_inst {
unsigned int num_nalu;
struct mtk_vcodec_ctx *ctx;
struct mtk_vcodec_mem pred_buf;
struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
struct vdec_vpu_inst vpu;
struct vdec_h264_vsi *vsi;
};
static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
{
return HW_MB_STORE_SZ * (width / MB_UNIT_LEN) * (height / MB_UNIT_LEN);
}
static int allocate_predication_buf(struct vdec_h264_inst *inst)
{
int err = 0;
inst->pred_buf.size = BUF_PREDICTION_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
if (err) {
mtk_vcodec_err(inst, "failed to allocate ppl buf");
return err;
}
inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
return 0;
}
static void free_predication_buf(struct vdec_h264_inst *inst)
{
struct mtk_vcodec_mem *mem = NULL;
mtk_vcodec_debug_enter(inst);
inst->vsi->pred_buf_dma = 0;
mem = &inst->pred_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
}
static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
{
int i;
int err;
struct mtk_vcodec_mem *mem = NULL;
unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
for (i = 0; i < H264_MAX_FB_NUM; i++) {
mem = &inst->mv_buf[i];
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
mem->size = buf_sz;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vcodec_err(inst, "failed to allocate mv buf");
return err;
}
inst->vsi->mv_buf_dma[i] = mem->dma_addr;
}
return 0;
}
static void free_mv_buf(struct vdec_h264_inst *inst)
{
int i;
struct mtk_vcodec_mem *mem = NULL;
for (i = 0; i < H264_MAX_FB_NUM; i++) {
inst->vsi->mv_buf_dma[i] = 0;
mem = &inst->mv_buf[i];
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
}
}
static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
{
struct h264_ring_fb_list *list;
list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
if (list->count > H264_MAX_FB_NUM ||
list->read_idx >= H264_MAX_FB_NUM ||
list->write_idx >= H264_MAX_FB_NUM) {
mtk_vcodec_err(inst, "%s list err: cnt=%d r_idx=%d w_idx=%d",
disp_list ? "disp" : "free", list->count,
list->read_idx, list->write_idx);
return -EINVAL;
}
return 0;
}
static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
{
struct h264_ring_fb_list *list;
if (fb) {
if (check_list_validity(inst, false))
return;
list = &inst->vsi->list_free;
if (list->count == H264_MAX_FB_NUM) {
mtk_vcodec_err(inst, "[FB] put fb free_list full");
return;
}
mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
fb->base_y.va, (u64)fb->base_y.dma_addr);
list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
0 : list->write_idx + 1;
list->count++;
}
}
static void get_pic_info(struct vdec_h264_inst *inst,
struct vdec_pic_info *pic)
{
*pic = inst->vsi->pic;
mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
}
static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
{
cr->left = inst->vsi->crop.left;
cr->top = inst->vsi->crop.top;
cr->width = inst->vsi->crop.width;
cr->height = inst->vsi->crop.height;
mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
cr->left, cr->top, cr->width, cr->height);
}
static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
{
*dpb_sz = inst->vsi->dec.dpb_sz;
mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
}
static int vdec_h264_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
{
struct vdec_h264_inst *inst = NULL;
int err;
inst = kzalloc(sizeof(*inst), GFP_KERNEL);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu.id = IPI_VDEC_H264;
inst->vpu.dev = ctx->dev->vpu_plat_dev;
inst->vpu.ctx = ctx;
inst->vpu.handler = vpu_dec_ipi_handler;
err = vpu_dec_init(&inst->vpu);
if (err) {
mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
goto error_free_inst;
}
inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
err = allocate_predication_buf(inst);
if (err)
goto error_deinit;
mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
*h_vdec = (unsigned long)inst;
return 0;
error_deinit:
vpu_dec_deinit(&inst->vpu);
error_free_inst:
kfree(inst);
return err;
}
static void vdec_h264_deinit(unsigned long h_vdec)
{
struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
mtk_vcodec_debug_enter(inst);
vpu_dec_deinit(&inst->vpu);
free_predication_buf(inst);
free_mv_buf(inst);
kfree(inst);
}
static int find_start_code(unsigned char *data, unsigned int data_sz)
{
if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
return 3;
if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
data[3] == 1)
return 4;
return -1;
}
static int vdec_h264_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, bool *res_chg)
{
struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
struct vdec_vpu_inst *vpu = &inst->vpu;
int nal_start_idx = 0;
int err = 0;
unsigned int nal_start;
unsigned int nal_type;
unsigned char *buf;
unsigned int buf_sz;
unsigned int data[2];
uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
/* bs NULL means flush decoder */
if (bs == NULL)
return vpu_dec_reset(vpu);
buf = (unsigned char *)bs->va;
buf_sz = bs->size;
nal_start_idx = find_start_code(buf, buf_sz);
if (nal_start_idx < 0)
goto err_free_fb_out;
nal_start = buf[nal_start_idx];
nal_type = NAL_TYPE(buf[nal_start_idx]);
mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
nal_type);
if (nal_type == NAL_H264_PPS) {
buf_sz -= nal_start_idx;
if (buf_sz > HDR_PARSING_BUF_SZ) {
err = -EILSEQ;
goto err_free_fb_out;
}
memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
}
inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
inst->vsi->dec.y_fb_dma = y_fb_dma;
inst->vsi->dec.c_fb_dma = c_fb_dma;
inst->vsi->dec.vdec_fb_va = vdec_fb_va;
data[0] = buf_sz;
data[1] = nal_start;
err = vpu_dec_start(vpu, data, 2);
if (err)
goto err_free_fb_out;
*res_chg = inst->vsi->dec.resolution_changed;
if (*res_chg) {
struct vdec_pic_info pic;
mtk_vcodec_debug(inst, "- resolution changed -");
get_pic_info(inst, &pic);
if (inst->vsi->dec.realloc_mv_buf) {
err = alloc_mv_buf(inst, &pic);
if (err)
goto err_free_fb_out;
}
}
if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
/* wait decoder done interrupt */
err = mtk_vcodec_wait_for_done_ctx(inst->ctx, 0,
MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS);
if (err)
goto err_free_fb_out;
vpu_dec_end(vpu);
}
mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
nal_type);
return 0;
err_free_fb_out:
put_fb_to_free(inst, fb);
mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
return err;
}
static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
struct h264_ring_fb_list *list,
bool disp_list, struct vdec_fb **out_fb)
{
struct vdec_fb *fb;
if (check_list_validity(inst, disp_list))
return;
if (list->count == 0) {
mtk_vcodec_debug(inst, "[FB] there is no %s fb",
disp_list ? "disp" : "free");
*out_fb = NULL;
return;
}
fb = (struct vdec_fb *)
(uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
*out_fb = fb;
mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
disp_list ? "disp" : "free",
fb->status, list->fb_list[list->read_idx].poc,
list->fb_list[list->read_idx].vdec_fb_va);
list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
0 : list->read_idx + 1;
list->count--;
}
static int vdec_h264_get_param(unsigned long h_vdec,
enum vdec_get_param_type type, void *out)
{
struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
switch (type) {
case GET_PARAM_DISP_FRAME_BUFFER:
vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
break;
case GET_PARAM_FREE_FRAME_BUFFER:
vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
break;
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
get_dpb_size(inst, out);
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
default:
mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
return -EINVAL;
}
return 0;
}
static struct vdec_common_if vdec_h264_if = {
vdec_h264_init,
vdec_h264_decode,
vdec_h264_get_param,
vdec_h264_deinit,
};
struct vdec_common_if *get_h264_dec_comm_if(void);
struct vdec_common_if *get_h264_dec_comm_if(void)
{
return &vdec_h264_if;
}
@@ -0,0 +1,625 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/slab.h>
#include "../vdec_drv_if.h"
#include "../mtk_vcodec_util.h"
#include "../mtk_vcodec_dec.h"
#include "../mtk_vcodec_intr.h"
#include "../vdec_vpu_if.h"
#include "../vdec_drv_base.h"
/* Decoding picture buffer size (3 reference frames plus current frame) */
#define VP8_DPB_SIZE 4
/* HW working buffer size (bytes) */
#define VP8_WORKING_BUF_SZ (45 * 4096)
/* HW control register address */
#define VP8_SEGID_DRAM_ADDR 0x3c
#define VP8_HW_VLD_ADDR 0x93C
#define VP8_HW_VLD_VALUE 0x940
#define VP8_BSASET 0x100
#define VP8_BSDSET 0x104
#define VP8_RW_CKEN_SET 0x0
#define VP8_RW_DCM_CON 0x18
#define VP8_WO_VLD_SRST 0x108
#define VP8_RW_MISC_SYS_SEL 0x84
#define VP8_RW_MISC_SPEC_CON 0xC8
#define VP8_WO_VLD_SRST 0x108
#define VP8_RW_VP8_CTRL 0xA4
#define VP8_RW_MISC_DCM_CON 0xEC
#define VP8_RW_MISC_SRST 0xF4
#define VP8_RW_MISC_FUNC_CON 0xCC
#define VP8_MAX_FRM_BUF_NUM 5
#define VP8_MAX_FRM_BUF_NODE_NUM (VP8_MAX_FRM_BUF_NUM * 2)
/* required buffer size (bytes) to store decode information */
#define VP8_HW_SEGMENT_DATA_SZ 272
#define VP8_HW_SEGMENT_UINT 4
#define VP8_DEC_TABLE_PROC_LOOP 96
#define VP8_DEC_TABLE_UNIT 3
#define VP8_DEC_TABLE_SZ 300
#define VP8_DEC_TABLE_OFFSET 2
#define VP8_DEC_TABLE_RW_UNIT 4
/**
* struct vdec_vp8_dec_info - decode misc information
* @working_buf_dma : working buffer dma address
* @prev_y_dma : previous decoded frame buffer Y plane address
* @cur_y_fb_dma : current plane Y frame buffer dma address
* @cur_c_fb_dma : current plane C frame buffer dma address
* @bs_dma : bitstream dma address
* @bs_sz : bitstream size
* @resolution_changed: resolution change flag 1 - changed, 0 - not change
* @show_frame : display this frame or not
* @wait_key_frame : wait key frame coming
*/
struct vdec_vp8_dec_info {
uint64_t working_buf_dma;
uint64_t prev_y_dma;
uint64_t cur_y_fb_dma;
uint64_t cur_c_fb_dma;
uint64_t bs_dma;
uint32_t bs_sz;
uint32_t resolution_changed;
uint32_t show_frame;
uint32_t wait_key_frame;
};
/**
* struct vdec_vp8_vsi - VPU shared information
* @dec : decoding information
* @pic : picture information
* @dec_table : decoder coefficient table
* @segment_buf : segmentation buffer
* @load_data : flag to indicate reload decode data
*/
struct vdec_vp8_vsi {
struct vdec_vp8_dec_info dec;
struct vdec_pic_info pic;
uint32_t dec_table[VP8_DEC_TABLE_SZ];
uint32_t segment_buf[VP8_HW_SEGMENT_DATA_SZ][VP8_HW_SEGMENT_UINT];
uint32_t load_data;
};
/**
* struct vdec_vp8_hw_reg_base - HW register base
* @sys : base address for sys
* @misc : base address for misc
* @ld : base address for ld
* @top : base address for top
* @cm : base address for cm
* @hwd : base address for hwd
* @hwb : base address for hwb
*/
struct vdec_vp8_hw_reg_base {
void __iomem *sys;
void __iomem *misc;
void __iomem *ld;
void __iomem *top;
void __iomem *cm;
void __iomem *hwd;
void __iomem *hwb;
};
/**
* struct vdec_vp8_vpu_inst - VPU instance for VP8 decode
* @wq_hd : Wait queue to wait VPU message ack
* @signaled : 1 - Host has received ack message from VPU, 0 - not recevie
* @failure : VPU execution result status 0 - success, others - fail
* @inst_addr : VPU decoder instance address
*/
struct vdec_vp8_vpu_inst {
wait_queue_head_t wq_hd;
int signaled;
int failure;
uint32_t inst_addr;
};
/* frame buffer (fb) list
* [available_fb_node_list] - decode fb are initialized to 0 and populated in
* [fb_use_list] - fb is set after decode and is moved to this list
* [fb_free_list] - fb is not needed for reference will be moved from
* [fb_use_list] to [fb_free_list] and
* once user remove fb from [fb_free_list],
* it is circulated back to [available_fb_node_list]
* [fb_disp_list] - fb is set after decode and is moved to this list
* once user remove fb from [fb_disp_list] it is
* circulated back to [available_fb_node_list]
*/
/**
* struct vdec_vp8_inst - VP8 decoder instance
* @cur_fb : current frame buffer
* @dec_fb : decode frame buffer node
* @available_fb_node_list : list to store available frame buffer node
* @fb_use_list : list to store frame buffer in use
* @fb_free_list : list to store free frame buffer
* @fb_disp_list : list to store display ready frame buffer
* @working_buf : HW decoder working buffer
* @reg_base : HW register base address
* @frm_cnt : decode frame count
* @ctx : V4L2 context
* @dev : platform device
* @vpu : VPU instance for decoder
* @vsi : VPU share information
*/
struct vdec_vp8_inst {
struct vdec_fb *cur_fb;
struct vdec_fb_node dec_fb[VP8_MAX_FRM_BUF_NODE_NUM];
struct list_head available_fb_node_list;
struct list_head fb_use_list;
struct list_head fb_free_list;
struct list_head fb_disp_list;
struct mtk_vcodec_mem working_buf;
struct vdec_vp8_hw_reg_base reg_base;
unsigned int frm_cnt;
struct mtk_vcodec_ctx *ctx;
struct vdec_vpu_inst vpu;
struct vdec_vp8_vsi *vsi;
};
static void get_hw_reg_base(struct vdec_vp8_inst *inst)
{
inst->reg_base.top = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_TOP);
inst->reg_base.cm = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_CM);
inst->reg_base.hwd = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_HWD);
inst->reg_base.sys = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_SYS);
inst->reg_base.misc = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_MISC);
inst->reg_base.ld = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_LD);
inst->reg_base.hwb = mtk_vcodec_get_dec_reg_addr(inst->ctx, VDEC_HWB);
}
static void write_hw_segmentation_data(struct vdec_vp8_inst *inst)
{
int i, j;
u32 seg_id_addr;
u32 val;
void __iomem *cm = inst->reg_base.cm;
struct vdec_vp8_vsi *vsi = inst->vsi;
seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
for (i = 0; i < ARRAY_SIZE(vsi->segment_buf); i++) {
for (j = ARRAY_SIZE(vsi->segment_buf[i]) - 1; j >= 0; j--) {
val = (1 << 16) + ((seg_id_addr + i) << 2) + j;
writel(val, cm + VP8_HW_VLD_ADDR);
val = vsi->segment_buf[i][j];
writel(val, cm + VP8_HW_VLD_VALUE);
}
}
}
static void read_hw_segmentation_data(struct vdec_vp8_inst *inst)
{
int i, j;
u32 seg_id_addr;
u32 val;
void __iomem *cm = inst->reg_base.cm;
struct vdec_vp8_vsi *vsi = inst->vsi;
seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
for (i = 0; i < ARRAY_SIZE(vsi->segment_buf); i++) {
for (j = ARRAY_SIZE(vsi->segment_buf[i]) - 1; j >= 0; j--) {
val = ((seg_id_addr + i) << 2) + j;
writel(val, cm + VP8_HW_VLD_ADDR);
val = readl(cm + VP8_HW_VLD_VALUE);
vsi->segment_buf[i][j] = val;
}
}
}
/* reset HW and enable HW read/write data function */
static void enable_hw_rw_function(struct vdec_vp8_inst *inst)
{
u32 val = 0;
void __iomem *sys = inst->reg_base.sys;
void __iomem *misc = inst->reg_base.misc;
void __iomem *ld = inst->reg_base.ld;
void __iomem *hwb = inst->reg_base.hwb;
void __iomem *hwd = inst->reg_base.hwd;
writel(0x1, sys + VP8_RW_CKEN_SET);
writel(0x101, ld + VP8_WO_VLD_SRST);
writel(0x101, hwb + VP8_WO_VLD_SRST);
writel(1, sys);
val = readl(misc + VP8_RW_MISC_SRST);
writel((val & 0xFFFFFFFE), misc + VP8_RW_MISC_SRST);
writel(0x1, misc + VP8_RW_MISC_SYS_SEL);
writel(0x17F, misc + VP8_RW_MISC_SPEC_CON);
writel(0x71201100, misc + VP8_RW_MISC_FUNC_CON);
writel(0x0, ld + VP8_WO_VLD_SRST);
writel(0x0, hwb + VP8_WO_VLD_SRST);
writel(0x1, sys + VP8_RW_DCM_CON);
writel(0x1, misc + VP8_RW_MISC_DCM_CON);
writel(0x1, hwd + VP8_RW_VP8_CTRL);
}
static void store_dec_table(struct vdec_vp8_inst *inst)
{
int i, j;
u32 addr = 0, val = 0;
void __iomem *hwd = inst->reg_base.hwd;
u32 *p = &inst->vsi->dec_table[VP8_DEC_TABLE_OFFSET];
for (i = 0; i < VP8_DEC_TABLE_PROC_LOOP; i++) {
writel(addr, hwd + VP8_BSASET);
for (j = 0; j < VP8_DEC_TABLE_UNIT ; j++) {
val = *p++;
writel(val, hwd + VP8_BSDSET);
}
addr += VP8_DEC_TABLE_RW_UNIT;
}
}
static void load_dec_table(struct vdec_vp8_inst *inst)
{
int i;
u32 addr = 0;
u32 *p = &inst->vsi->dec_table[VP8_DEC_TABLE_OFFSET];
void __iomem *hwd = inst->reg_base.hwd;
for (i = 0; i < VP8_DEC_TABLE_PROC_LOOP; i++) {
writel(addr, hwd + VP8_BSASET);
/* read total 11 bytes */
*p++ = readl(hwd + VP8_BSDSET);
*p++ = readl(hwd + VP8_BSDSET);
*p++ = readl(hwd + VP8_BSDSET) & 0xFFFFFF;
addr += VP8_DEC_TABLE_RW_UNIT;
}
}
static void get_pic_info(struct vdec_vp8_inst *inst, struct vdec_pic_info *pic)
{
*pic = inst->vsi->pic;
mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
}
static void vp8_dec_finish(struct vdec_vp8_inst *inst)
{
struct vdec_fb_node *node;
uint64_t prev_y_dma = inst->vsi->dec.prev_y_dma;
mtk_vcodec_debug(inst, "prev fb base dma=%llx", prev_y_dma);
/* put last decode ok frame to fb_free_list */
if (prev_y_dma != 0) {
list_for_each_entry(node, &inst->fb_use_list, list) {
struct vdec_fb *fb = (struct vdec_fb *)node->fb;
if (prev_y_dma == (uint64_t)fb->base_y.dma_addr) {
list_move_tail(&node->list,
&inst->fb_free_list);
break;
}
}
}
/* available_fb_node_list -> fb_use_list */
node = list_first_entry(&inst->available_fb_node_list,
struct vdec_fb_node, list);
node->fb = inst->cur_fb;
list_move_tail(&node->list, &inst->fb_use_list);
/* available_fb_node_list -> fb_disp_list */
if (inst->vsi->dec.show_frame) {
node = list_first_entry(&inst->available_fb_node_list,
struct vdec_fb_node, list);
node->fb = inst->cur_fb;
list_move_tail(&node->list, &inst->fb_disp_list);
}
}
static void move_fb_list_use_to_free(struct vdec_vp8_inst *inst)
{
struct vdec_fb_node *node, *tmp;
list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
list_move_tail(&node->list, &inst->fb_free_list);
}
static void init_list(struct vdec_vp8_inst *inst)
{
int i;
INIT_LIST_HEAD(&inst->available_fb_node_list);
INIT_LIST_HEAD(&inst->fb_use_list);
INIT_LIST_HEAD(&inst->fb_free_list);
INIT_LIST_HEAD(&inst->fb_disp_list);
for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) {
INIT_LIST_HEAD(&inst->dec_fb[i].list);
inst->dec_fb[i].fb = NULL;
list_add_tail(&inst->dec_fb[i].list,
&inst->available_fb_node_list);
}
}
static void add_fb_to_free_list(struct vdec_vp8_inst *inst, void *fb)
{
struct vdec_fb_node *node;
if (fb) {
node = list_first_entry(&inst->available_fb_node_list,
struct vdec_fb_node, list);
node->fb = fb;
list_move_tail(&node->list, &inst->fb_free_list);
}
}
static int alloc_working_buf(struct vdec_vp8_inst *inst)
{
int err;
struct mtk_vcodec_mem *mem = &inst->working_buf;
mem->size = VP8_WORKING_BUF_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vcodec_err(inst, "Cannot allocate working buffer");
return err;
}
inst->vsi->dec.working_buf_dma = (uint64_t)mem->dma_addr;
return 0;
}
static void free_working_buf(struct vdec_vp8_inst *inst)
{
struct mtk_vcodec_mem *mem = &inst->working_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
inst->vsi->dec.working_buf_dma = 0;
}
static int vdec_vp8_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
{
struct vdec_vp8_inst *inst;
int err;
inst = kzalloc(sizeof(*inst), GFP_KERNEL);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu.id = IPI_VDEC_VP8;
inst->vpu.dev = ctx->dev->vpu_plat_dev;
inst->vpu.ctx = ctx;
inst->vpu.handler = vpu_dec_ipi_handler;
err = vpu_dec_init(&inst->vpu);
if (err) {
mtk_vcodec_err(inst, "vdec_vp8 init err=%d", err);
goto error_free_inst;
}
inst->vsi = (struct vdec_vp8_vsi *)inst->vpu.vsi;
init_list(inst);
err = alloc_working_buf(inst);
if (err)
goto error_deinit;
get_hw_reg_base(inst);
mtk_vcodec_debug(inst, "VP8 Instance >> %p", inst);
*h_vdec = (unsigned long)inst;
return 0;
error_deinit:
vpu_dec_deinit(&inst->vpu);
error_free_inst:
kfree(inst);
return err;
}
static int vdec_vp8_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, bool *res_chg)
{
struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
struct vdec_vp8_dec_info *dec = &inst->vsi->dec;
struct vdec_vpu_inst *vpu = &inst->vpu;
unsigned char *bs_va;
unsigned int data;
int err = 0;
uint64_t y_fb_dma;
uint64_t c_fb_dma;
/* bs NULL means flush decoder */
if (bs == NULL) {
move_fb_list_use_to_free(inst);
return vpu_dec_reset(vpu);
}
y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx fb=%p",
inst->frm_cnt, y_fb_dma, c_fb_dma, fb);
inst->cur_fb = fb;
dec->bs_dma = (unsigned long)bs->dma_addr;
dec->bs_sz = bs->size;
dec->cur_y_fb_dma = y_fb_dma;
dec->cur_c_fb_dma = c_fb_dma;
mtk_vcodec_debug(inst, "\n + FRAME[%d] +\n", inst->frm_cnt);
write_hw_segmentation_data(inst);
enable_hw_rw_function(inst);
store_dec_table(inst);
bs_va = (unsigned char *)bs->va;
/* retrieve width/hight and scale info from header */
data = (*(bs_va + 9) << 24) | (*(bs_va + 8) << 16) |
(*(bs_va + 7) << 8) | *(bs_va + 6);
err = vpu_dec_start(vpu, &data, 1);
if (err) {
add_fb_to_free_list(inst, fb);
if (dec->wait_key_frame) {
mtk_vcodec_debug(inst, "wait key frame !");
return 0;
}
goto error;
}
if (dec->resolution_changed) {
mtk_vcodec_debug(inst, "- resolution_changed -");
*res_chg = true;
add_fb_to_free_list(inst, fb);
return 0;
}
/* wait decoder done interrupt */
mtk_vcodec_wait_for_done_ctx(inst->ctx, 0,
MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS);
if (inst->vsi->load_data)
load_dec_table(inst);
vp8_dec_finish(inst);
read_hw_segmentation_data(inst);
err = vpu_dec_end(vpu);
if (err)
goto error;
mtk_vcodec_debug(inst, "\n - FRAME[%d] - show=%d\n", inst->frm_cnt,
dec->show_frame);
inst->frm_cnt++;
*res_chg = false;
return 0;
error:
mtk_vcodec_err(inst, "\n - FRAME[%d] - err=%d\n", inst->frm_cnt, err);
return err;
}
static void get_disp_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
{
struct vdec_fb_node *node;
struct vdec_fb *fb;
node = list_first_entry_or_null(&inst->fb_disp_list,
struct vdec_fb_node, list);
if (node) {
list_move_tail(&node->list, &inst->available_fb_node_list);
fb = (struct vdec_fb *)node->fb;
fb->status |= FB_ST_DISPLAY;
mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
node->fb, fb->status);
} else {
fb = NULL;
mtk_vcodec_debug(inst, "[FB] there is no disp fb");
}
*out_fb = fb;
}
static void get_free_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
{
struct vdec_fb_node *node;
struct vdec_fb *fb;
node = list_first_entry_or_null(&inst->fb_free_list,
struct vdec_fb_node, list);
if (node) {
list_move_tail(&node->list, &inst->available_fb_node_list);
fb = (struct vdec_fb *)node->fb;
fb->status |= FB_ST_FREE;
mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
node->fb, fb->status);
} else {
fb = NULL;
mtk_vcodec_debug(inst, "[FB] there is no free fb");
}
*out_fb = fb;
}
static void get_crop_info(struct vdec_vp8_inst *inst, struct v4l2_rect *cr)
{
cr->left = 0;
cr->top = 0;
cr->width = inst->vsi->pic.pic_w;
cr->height = inst->vsi->pic.pic_h;
mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d",
cr->left, cr->top, cr->width, cr->height);
}
static int vdec_vp8_get_param(unsigned long h_vdec,
enum vdec_get_param_type type, void *out)
{
struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
switch (type) {
case GET_PARAM_DISP_FRAME_BUFFER:
get_disp_fb(inst, out);
break;
case GET_PARAM_FREE_FRAME_BUFFER:
get_free_fb(inst, out);
break;
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
*((unsigned int *)out) = VP8_DPB_SIZE;
break;
default:
mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
return -EINVAL;
}
return 0;
}
static void vdec_vp8_deinit(unsigned long h_vdec)
{
struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
mtk_vcodec_debug_enter(inst);
vpu_dec_deinit(&inst->vpu);
free_working_buf(inst);
kfree(inst);
}
static struct vdec_common_if vdec_vp8_if = {
vdec_vp8_init,
vdec_vp8_decode,
vdec_vp8_get_param,
vdec_vp8_deinit,
};
struct vdec_common_if *get_vp8_dec_comm_if(void);
struct vdec_common_if *get_vp8_dec_comm_if(void)
{
return &vdec_vp8_if;
}
@@ -0,0 +1,979 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/delay.h>
#include <linux/time.h>
#include "../mtk_vcodec_intr.h"
#include "../vdec_drv_base.h"
#include "../vdec_vpu_if.h"
#define VP9_SUPER_FRAME_BS_SZ 64
#define MAX_VP9_DPB_SIZE 9
#define REFS_PER_FRAME 3
#define MAX_NUM_REF_FRAMES 8
#define VP9_MAX_FRM_BUF_NUM 9
#define VP9_MAX_FRM_BUF_NODE_NUM (VP9_MAX_FRM_BUF_NUM * 2)
/**
* struct vp9_dram_buf - contains buffer info for vpu
* @va : cpu address
* @pa : iova address
* @sz : buffer size
* @padding : for 64 bytes alignment
*/
struct vp9_dram_buf {
unsigned long va;
unsigned long pa;
unsigned int sz;
unsigned int padding;
};
/**
* struct vp9_fb_info - contains frame buffer info
* @fb : frmae buffer
* @reserved : reserved field used by vpu
*/
struct vp9_fb_info {
struct vdec_fb *fb;
unsigned int reserved[32];
};
/**
* struct vp9_ref_cnt_buf - contains reference buffer information
* @buf : referenced frame buffer
* @ref_cnt : referenced frame buffer's reference count.
* When reference count=0, remove it from reference list
*/
struct vp9_ref_cnt_buf {
struct vp9_fb_info buf;
unsigned int ref_cnt;
};
/**
* struct vp9_fb_info - contains current frame's reference buffer information
* @buf : reference buffer
* @idx : reference buffer index to frm_bufs
* @reserved : reserved field used by vpu
*/
struct vp9_ref_buf {
struct vp9_fb_info *buf;
unsigned int idx;
unsigned int reserved[6];
};
/**
* struct vp9_fb_info - contains frame buffer info
* @fb : super frame reference frame buffer
* @used : this reference frame info entry is used
* @padding : for 64 bytes size align
*/
struct vp9_sf_ref_fb {
struct vdec_fb fb;
int used;
int padding;
};
/*
* struct vdec_vp9_vsi - shared buffer between host and VPU firmware
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @sf_bs_buf : super frame backup buffer (AP-W, VPU-R)
* @sf_ref_fb : record supoer frame reference buffer information
* (AP-R/W, VPU-R/W)
* @sf_next_ref_fb_idx : next available super frame (AP-W, VPU-R)
* @sf_frm_cnt : super frame count, filled by vpu (AP-R, VPU-W)
* @sf_frm_offset : super frame offset, filled by vpu (AP-R, VPU-W)
* @sf_frm_sz : super frame size, filled by vpu (AP-R, VPU-W)
* @sf_frm_idx : current super frame (AP-R, VPU-W)
* @sf_init : inform super frame info already parsed by vpu (AP-R, VPU-W)
* @fb : capture buffer (AP-W, VPU-R)
* @bs : bs buffer (AP-W, VPU-R)
* @cur_fb : current show capture buffer (AP-R/W, VPU-R/W)
* @pic_w : picture width (AP-R, VPU-W)
* @pic_h : picture height (AP-R, VPU-W)
* @buf_w : codec width (AP-R, VPU-W)
* @buf_h : coded height (AP-R, VPU-W)
* @buf_sz_y_bs : ufo compressed y plane size (AP-R, VPU-W)
* @buf_sz_c_bs : ufo compressed cbcr plane size (AP-R, VPU-W)
* @buf_len_sz_y : size used to store y plane ufo info (AP-R, VPU-W)
* @buf_len_sz_c : size used to store cbcr plane ufo info (AP-R, VPU-W)
* @profile : profile sparsed from vpu (AP-R, VPU-W)
* @show_frame : display this frame or not (AP-R, VPU-W)
* @show_existing_frame : inform this frame is show existing frame
* (AP-R, VPU-W)
* @frm_to_show_idx : index to show frame (AP-R, VPU-W)
* @refresh_frm_flags : indicate when frame need to refine reference count
* (AP-R, VPU-W)
* @resolution_changed : resolution change in this frame (AP-R, VPU-W)
* @frm_bufs : maintain reference buffer info (AP-R/W, VPU-R/W)
* @ref_frm_map : maintain reference buffer map info (AP-R/W, VPU-R/W)
* @new_fb_idx : index to frm_bufs array (AP-R, VPU-W)
* @frm_num : decoded frame number, include sub-frame count (AP-R, VPU-W)
* @mv_buf : motion vector working buffer (AP-W, VPU-R)
* @frm_refs : maintain three reference buffer info (AP-R/W, VPU-R/W)
*/
struct vdec_vp9_vsi {
unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUF_NUM - 1];
int sf_next_ref_fb_idx;
unsigned int sf_frm_cnt;
unsigned int sf_frm_offset[VP9_MAX_FRM_BUF_NUM - 1];
unsigned int sf_frm_sz[VP9_MAX_FRM_BUF_NUM - 1];
unsigned int sf_frm_idx;
unsigned int sf_init;
struct vdec_fb fb;
struct mtk_vcodec_mem bs;
struct vdec_fb cur_fb;
unsigned int pic_w;
unsigned int pic_h;
unsigned int buf_w;
unsigned int buf_h;
unsigned int buf_sz_y_bs;
unsigned int buf_sz_c_bs;
unsigned int buf_len_sz_y;
unsigned int buf_len_sz_c;
unsigned int profile;
unsigned int show_frame;
unsigned int show_existing_frame;
unsigned int frm_to_show_idx;
unsigned int refresh_frm_flags;
unsigned int resolution_changed;
struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUF_NUM];
int ref_frm_map[MAX_NUM_REF_FRAMES];
unsigned int new_fb_idx;
unsigned int frm_num;
struct vp9_dram_buf mv_buf;
struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
};
/*
* struct vdec_vp9_inst - vp9 decode instance
* @mv_buf : working buffer for mv
* @dec_fb : vdec_fb node to link fb to different fb_xxx_list
* @available_fb_node_list : current available vdec_fb node
* @fb_use_list : current used or referenced vdec_fb
* @fb_free_list : current available to free vdec_fb
* @fb_disp_list : current available to display vdec_fb
* @cur_fb : current frame buffer
* @ctx : current decode context
* @vpu : vpu instance information
* @vsi : shared buffer between host and VPU firmware
* @total_frm_cnt : total frame count, it do not include sub-frames in super
* frame
* @mem : instance memory information
*/
struct vdec_vp9_inst {
struct mtk_vcodec_mem mv_buf;
struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUF_NODE_NUM];
struct list_head available_fb_node_list;
struct list_head fb_use_list;
struct list_head fb_free_list;
struct list_head fb_disp_list;
struct vdec_fb *cur_fb;
struct mtk_vcodec_ctx *ctx;
struct vdec_vpu_inst vpu;
struct vdec_vp9_vsi *vsi;
unsigned int total_frm_cnt;
struct mtk_vcodec_mem mem;
};
static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
{
int i;
struct vdec_vp9_vsi *vsi = inst->vsi;
for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
if (fb == &vsi->sf_ref_fb[i].fb)
return true;
}
return false;
}
static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
*inst, void *addr)
{
struct vdec_fb *fb = NULL;
struct vdec_fb_node *node;
list_for_each_entry(node, &inst->fb_use_list, list) {
fb = (struct vdec_fb *)node->fb;
if (fb->base_y.va == addr) {
list_move_tail(&node->list,
&inst->available_fb_node_list);
break;
}
}
return fb;
}
static void vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
struct vdec_fb *fb)
{
struct vdec_fb_node *node;
if (fb) {
node = list_first_entry_or_null(&inst->available_fb_node_list,
struct vdec_fb_node, list);
if (node) {
node->fb = fb;
list_move_tail(&node->list, &inst->fb_free_list);
}
} else
mtk_vcodec_debug(inst, "No free fb node");
}
static void vp9_free_sf_ref_fb(struct vdec_fb *fb)
{
struct vp9_sf_ref_fb *sf_ref_fb =
container_of(fb, struct vp9_sf_ref_fb, fb);
sf_ref_fb->used = 0;
}
static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
int new_idx)
{
struct vdec_vp9_vsi *vsi = inst->vsi;
int ref_idx = *idx;
if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
vsi->frm_bufs[ref_idx].ref_cnt--;
if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
if (!vp9_is_sf_ref_fb(inst,
vsi->frm_bufs[ref_idx].buf.fb)) {
struct vdec_fb *fb;
fb = vp9_rm_from_fb_use_list(inst,
vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
vp9_add_to_fb_free_list(inst, fb);
} else
vp9_free_sf_ref_fb(
vsi->frm_bufs[ref_idx].buf.fb);
}
}
*idx = new_idx;
vsi->frm_bufs[new_idx].ref_cnt++;
}
static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
{
int i;
struct vdec_vp9_vsi *vsi = inst->vsi;
for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
if (vsi->sf_ref_fb[i].fb.base_y.va) {
mtk_vcodec_mem_free(inst->ctx,
&vsi->sf_ref_fb[i].fb.base_y);
mtk_vcodec_mem_free(inst->ctx,
&vsi->sf_ref_fb[i].fb.base_c);
vsi->sf_ref_fb[i].used = 0;
}
}
}
/* For each sub-frame except the last one, the driver will dynamically
* allocate reference buffer by calling vp9_get_sf_ref_fb()
* The last sub-frame will use the original fb provided by the
* vp9_dec_decode() interface
*/
static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
{
int idx;
struct mtk_vcodec_mem *mem_basy_y;
struct mtk_vcodec_mem *mem_basy_c;
struct vdec_vp9_vsi *vsi = inst->vsi;
for (idx = 0;
idx < ARRAY_SIZE(vsi->sf_ref_fb);
idx++) {
if (vsi->sf_ref_fb[idx].fb.base_y.va &&
vsi->sf_ref_fb[idx].used == 0)
return idx;
}
for (idx = 0;
idx < ARRAY_SIZE(vsi->sf_ref_fb);
idx++) {
if (vsi->sf_ref_fb[idx].fb.base_y.va == NULL)
break;
}
if (idx == ARRAY_SIZE(vsi->sf_ref_fb)) {
mtk_vcodec_err(inst, "List Full");
return -1;
}
mem_basy_y = &vsi->sf_ref_fb[idx].fb.base_y;
mem_basy_y->size = vsi->buf_sz_y_bs +
vsi->buf_len_sz_y;
if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_y)) {
mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
return -1;
}
mem_basy_c = &vsi->sf_ref_fb[idx].fb.base_c;
mem_basy_c->size = vsi->buf_sz_c_bs +
vsi->buf_len_sz_c;
if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_c)) {
mtk_vcodec_err(inst, "Cannot allocate sf_ref_fb c_buf");
return -1;
}
vsi->sf_ref_fb[idx].used = 0;
return idx;
}
static bool vp9_alloc_work_buf(struct vdec_vp9_inst *inst)
{
struct vdec_vp9_vsi *vsi = inst->vsi;
int result;
struct mtk_vcodec_mem *mem;
unsigned int max_pic_w;
unsigned int max_pic_h;
if (!(inst->ctx->dev->dec_capability &
VCODEC_CAPABILITY_4K_DISABLED)) {
max_pic_w = VCODEC_DEC_4K_CODED_WIDTH;
max_pic_h = VCODEC_DEC_4K_CODED_HEIGHT;
} else {
max_pic_w = MTK_VDEC_MAX_W;
max_pic_h = MTK_VDEC_MAX_H;
}
if ((vsi->pic_w > max_pic_w) ||
(vsi->pic_h > max_pic_h)) {
mtk_vcodec_err(inst, "Invalid w/h %d/%d",
vsi->pic_w, vsi->pic_h);
return false;
}
mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
vsi->resolution_changed,
vsi->pic_w,
vsi->pic_h,
vsi->buf_w,
vsi->buf_h);
mem = &inst->mv_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
mem->size = ((vsi->buf_w / 64) *
(vsi->buf_h / 64) + 2) * 36 * 16;
result = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (result) {
mem->size = 0;
mtk_vcodec_err(inst, "Cannot allocate mv_buf");
return false;
}
/* Set the va again */
vsi->mv_buf.va = (unsigned long)mem->va;
vsi->mv_buf.pa = (unsigned long)mem->dma_addr;
vsi->mv_buf.sz = (unsigned int)mem->size;
vp9_free_all_sf_ref_fb(inst);
vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
return true;
}
static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
struct vdec_fb *fb)
{
struct vdec_fb_node *node;
if (!fb) {
mtk_vcodec_err(inst, "fb == NULL");
return false;
}
node = list_first_entry_or_null(&inst->available_fb_node_list,
struct vdec_fb_node, list);
if (node) {
node->fb = fb;
list_move_tail(&node->list, &inst->fb_disp_list);
} else {
mtk_vcodec_err(inst, "No available fb node");
return false;
}
return true;
}
/* If any buffer updating is signaled it should be done here. */
static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
{
struct vdec_vp9_vsi *vsi = inst->vsi;
struct vp9_fb_info *frm_to_show;
int ref_index = 0, mask;
for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
if (mask & 1)
vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
vsi->new_fb_idx);
++ref_index;
}
frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
if (frm_to_show->fb != inst->cur_fb) {
/* This frame is show exist frame and no decode output
* copy frame data from frm_to_show to current CAPTURE
* buffer
*/
if ((frm_to_show->fb != NULL) &&
(inst->cur_fb->base_y.size >=
frm_to_show->fb->base_y.size)) {
memcpy((void *)inst->cur_fb->base_y.va,
(void *)frm_to_show->fb->base_y.va,
vsi->buf_w *
vsi->buf_h);
memcpy((void *)inst->cur_fb->base_c.va,
(void *)frm_to_show->fb->base_c.va,
vsi->buf_w *
vsi->buf_h / 2);
} else {
/* After resolution change case, current CAPTURE buffer
* may have less buffer size than frm_to_show buffer
* size
*/
if (frm_to_show->fb != NULL)
mtk_vcodec_err(inst,
"inst->cur_fb->base_y.size=%zu, frm_to_show->fb.base_y.size=%zu",
inst->cur_fb->base_y.size,
frm_to_show->fb->base_y.size);
}
if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
if (vsi->show_frame)
vp9_add_to_fb_disp_list(inst, inst->cur_fb);
}
} else {
if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
if (vsi->show_frame)
vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
}
}
/* when ref_cnt ==0, move this fb to fb_free_list. v4l2 driver will
* clean fb_free_list
*/
if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
if (!vp9_is_sf_ref_fb(
inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
struct vdec_fb *fb;
fb = vp9_rm_from_fb_use_list(inst,
vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
vp9_add_to_fb_free_list(inst, fb);
} else {
vp9_free_sf_ref_fb(
vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
}
}
/* if this super frame and it is not last sub-frame, get next fb for
* sub-frame decode
*/
if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
}
static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
{
struct mtk_vcodec_ctx *ctx = inst->ctx;
mtk_vcodec_wait_for_done_ctx(inst->ctx, 0,
MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS);
if (ctx->irq_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
return true;
else
return false;
}
static struct vdec_vp9_inst *vp9_alloc_inst(struct mtk_vcodec_ctx *ctx)
{
int result;
struct mtk_vcodec_mem mem;
struct vdec_vp9_inst *inst;
memset(&mem, 0, sizeof(mem));
mem.size = sizeof(struct vdec_vp9_inst);
result = mtk_vcodec_mem_alloc(ctx, &mem);
if (result)
return NULL;
inst = mem.va;
inst->mem = mem;
return inst;
}
static void vp9_free_inst(struct vdec_vp9_inst *inst)
{
struct mtk_vcodec_mem mem;
mem = inst->mem;
if (mem.va)
mtk_vcodec_mem_free(inst->ctx, &mem);
}
static bool vp9_decode_end_proc(struct vdec_vp9_inst *inst)
{
struct vdec_vp9_vsi *vsi = inst->vsi;
bool ret = false;
if (!vsi->show_existing_frame) {
ret = vp9_wait_dec_end(inst);
if (!ret) {
mtk_vcodec_err(inst, "Decode failed, Decode Timeout @[%d]",
vsi->frm_num);
return false;
}
if (vpu_dec_end(&inst->vpu)) {
mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
return false;
}
mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
vsi->pic_w, vsi->pic_h);
} else {
mtk_vcodec_debug(inst, "Decode Ok @%d (show_existing_frame)",
vsi->frm_num);
}
vp9_swap_frm_bufs(inst);
vsi->frm_num++;
return true;
}
static bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
{
struct vdec_vp9_vsi *vsi = inst->vsi;
if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
return true;
return false;
}
static struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst *inst)
{
struct vdec_fb_node *node;
struct vdec_fb *fb = NULL;
node = list_first_entry_or_null(&inst->fb_disp_list,
struct vdec_fb_node, list);
if (node) {
fb = (struct vdec_fb *)node->fb;
fb->status |= FB_ST_DISPLAY;
list_move_tail(&node->list, &inst->available_fb_node_list);
mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
node->fb, fb->status);
} else
mtk_vcodec_debug(inst, "[FB] there is no disp fb");
return fb;
}
static bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
struct vdec_fb *fb)
{
struct vdec_fb_node *node;
if (!fb) {
mtk_vcodec_debug(inst, "fb == NULL");
return false;
}
node = list_first_entry_or_null(&inst->available_fb_node_list,
struct vdec_fb_node, list);
if (node) {
node->fb = fb;
list_move_tail(&node->list, &inst->fb_use_list);
} else {
mtk_vcodec_err(inst, "No free fb node");
return false;
}
return true;
}
static void vp9_reset(struct vdec_vp9_inst *inst)
{
struct vdec_fb_node *node, *tmp;
list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
list_move_tail(&node->list, &inst->fb_free_list);
vp9_free_all_sf_ref_fb(inst);
inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
if (vpu_dec_reset(&inst->vpu))
mtk_vcodec_err(inst, "vp9_dec_vpu_reset failed");
/* Set the va again, since vpu_dec_reset will clear mv_buf in vpu */
inst->vsi->mv_buf.va = (unsigned long)inst->mv_buf.va;
inst->vsi->mv_buf.pa = (unsigned long)inst->mv_buf.dma_addr;
inst->vsi->mv_buf.sz = (unsigned long)inst->mv_buf.size;
}
static void init_all_fb_lists(struct vdec_vp9_inst *inst)
{
int i;
INIT_LIST_HEAD(&inst->available_fb_node_list);
INIT_LIST_HEAD(&inst->fb_use_list);
INIT_LIST_HEAD(&inst->fb_free_list);
INIT_LIST_HEAD(&inst->fb_disp_list);
for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) {
INIT_LIST_HEAD(&inst->dec_fb[i].list);
inst->dec_fb[i].fb = NULL;
list_add_tail(&inst->dec_fb[i].list,
&inst->available_fb_node_list);
}
}
static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
{
pic->y_bs_sz = inst->vsi->buf_sz_y_bs;
pic->c_bs_sz = inst->vsi->buf_sz_c_bs;
pic->y_len_sz = inst->vsi->buf_len_sz_y;
pic->c_len_sz = inst->vsi->buf_len_sz_c;
pic->pic_w = inst->vsi->pic_w;
pic->pic_h = inst->vsi->pic_h;
pic->buf_w = inst->vsi->buf_w;
pic->buf_h = inst->vsi->buf_h;
mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
}
static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
{
*out_fb = vp9_rm_from_fb_disp_list(inst);
if (*out_fb)
(*out_fb)->status |= FB_ST_DISPLAY;
}
static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
{
struct vdec_fb_node *node;
struct vdec_fb *fb = NULL;
node = list_first_entry_or_null(&inst->fb_free_list,
struct vdec_fb_node, list);
if (node) {
list_move_tail(&node->list, &inst->available_fb_node_list);
fb = (struct vdec_fb *)node->fb;
fb->status |= FB_ST_FREE;
mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
node->fb, fb->status);
} else
mtk_vcodec_debug(inst, "[FB] there is no free fb");
*out_fb = fb;
}
static int validate_vsi_array_indexes(struct vdec_vp9_inst *inst,
struct vdec_vp9_vsi *vsi)
{
if (vsi->sf_frm_idx >= VP9_MAX_FRM_BUF_NUM - 1) {
mtk_vcodec_err(inst, "Invalid vsi->sf_frm_idx=%u.",
vsi->sf_frm_idx);
return -EIO;
}
if (vsi->frm_to_show_idx >= VP9_MAX_FRM_BUF_NUM) {
mtk_vcodec_err(inst, "Invalid vsi->frm_to_show_idx=%u.",
vsi->frm_to_show_idx);
return -EIO;
}
if (vsi->new_fb_idx >= VP9_MAX_FRM_BUF_NUM) {
mtk_vcodec_err(inst, "Invalid vsi->new_fb_idx=%u.",
vsi->new_fb_idx);
return -EIO;
}
return 0;
}
static void vdec_vp9_deinit(unsigned long h_vdec)
{
struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
struct mtk_vcodec_mem *mem;
int ret = 0;
ret = vpu_dec_deinit(&inst->vpu);
if (ret)
mtk_vcodec_err(inst, "vpu_dec_deinit failed");
mem = &inst->mv_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
vp9_free_all_sf_ref_fb(inst);
vp9_free_inst(inst);
}
static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
{
struct vdec_vp9_inst *inst;
inst = vp9_alloc_inst(ctx);
if (!inst)
return -ENOMEM;
inst->total_frm_cnt = 0;
inst->ctx = ctx;
inst->vpu.id = IPI_VDEC_VP9;
inst->vpu.dev = ctx->dev->vpu_plat_dev;
inst->vpu.ctx = ctx;
inst->vpu.handler = vpu_dec_ipi_handler;
if (vpu_dec_init(&inst->vpu)) {
mtk_vcodec_err(inst, "vp9_dec_vpu_init failed");
goto err_deinit_inst;
}
inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
init_all_fb_lists(inst);
(*h_vdec) = (unsigned long)inst;
return 0;
err_deinit_inst:
vp9_free_inst(inst);
return -EINVAL;
}
static int vdec_vp9_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, bool *res_chg)
{
int ret = 0;
struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
struct vdec_vp9_vsi *vsi = inst->vsi;
u32 data[3];
int i;
*res_chg = false;
if ((bs == NULL) && (fb == NULL)) {
mtk_vcodec_debug(inst, "[EOS]");
vp9_reset(inst);
return ret;
}
if (bs == NULL) {
mtk_vcodec_err(inst, "bs == NULL");
return -EINVAL;
}
mtk_vcodec_debug(inst, "Input BS Size = %zu", bs->size);
while (1) {
struct vdec_fb *cur_fb = NULL;
data[0] = *((unsigned int *)bs->va);
data[1] = *((unsigned int *)(bs->va + 4));
data[2] = *((unsigned int *)(bs->va + 8));
vsi->bs = *bs;
if (fb)
vsi->fb = *fb;
if (!vsi->sf_init) {
unsigned int sf_bs_sz;
unsigned int sf_bs_off;
unsigned char *sf_bs_src;
unsigned char *sf_bs_dst;
sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
VP9_SUPER_FRAME_BS_SZ : bs->size;
sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
sf_bs_src = bs->va + bs->size - sf_bs_sz;
sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
} else {
if ((vsi->sf_frm_cnt > 0) &&
(vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
unsigned int idx = vsi->sf_frm_idx;
memcpy((void *)bs->va,
(void *)(bs->va +
vsi->sf_frm_offset[idx]),
vsi->sf_frm_sz[idx]);
}
}
ret = vpu_dec_start(&inst->vpu, data, 3);
if (ret) {
mtk_vcodec_err(inst, "vpu_dec_start failed");
goto DECODE_ERROR;
}
ret = validate_vsi_array_indexes(inst, vsi);
if (ret) {
mtk_vcodec_err(inst, "Invalid values from VPU.");
goto DECODE_ERROR;
}
if (vsi->resolution_changed) {
if (!vp9_alloc_work_buf(inst)) {
ret = -EINVAL;
goto DECODE_ERROR;
}
}
if (vsi->sf_frm_cnt > 0) {
cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
inst->cur_fb = cur_fb;
else
inst->cur_fb = fb;
} else
inst->cur_fb = fb;
vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
vp9_add_to_fb_use_list(inst, inst->cur_fb);
mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
if (vsi->show_existing_frame)
mtk_vcodec_debug(inst,
"drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
vsi->new_fb_idx, vsi->frm_to_show_idx);
if (vsi->show_existing_frame && (vsi->frm_to_show_idx <
VP9_MAX_FRM_BUF_NUM)) {
mtk_vcodec_err(inst,
"Skip Decode drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
vsi->new_fb_idx, vsi->frm_to_show_idx);
vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
vsi->frm_to_show_idx);
ret = -EINVAL;
goto DECODE_ERROR;
}
/* VPU assign the buffer pointer in its address space,
* reassign here
*/
for (i = 0; i < ARRAY_SIZE(vsi->frm_refs); i++) {
unsigned int idx = vsi->frm_refs[i].idx;
vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
}
if (vsi->resolution_changed) {
*res_chg = true;
mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
ret = 0;
goto DECODE_ERROR;
}
if (vp9_decode_end_proc(inst) != true) {
mtk_vcodec_err(inst, "vp9_decode_end_proc");
ret = -EINVAL;
goto DECODE_ERROR;
}
if (vp9_is_last_sub_frm(inst))
break;
}
inst->total_frm_cnt++;
DECODE_ERROR:
if (ret < 0)
vp9_add_to_fb_free_list(inst, fb);
return ret;
}
static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_rect *cr)
{
cr->left = 0;
cr->top = 0;
cr->width = inst->vsi->pic_w;
cr->height = inst->vsi->pic_h;
mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
cr->left, cr->top, cr->width, cr->height);
}
static int vdec_vp9_get_param(unsigned long h_vdec,
enum vdec_get_param_type type, void *out)
{
struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
int ret = 0;
switch (type) {
case GET_PARAM_DISP_FRAME_BUFFER:
get_disp_fb(inst, out);
break;
case GET_PARAM_FREE_FRAME_BUFFER:
get_free_fb(inst, out);
break;
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
*((unsigned int *)out) = MAX_VP9_DPB_SIZE;
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
default:
mtk_vcodec_err(inst, "not supported param type %d", type);
ret = -EINVAL;
break;
}
return ret;
}
static struct vdec_common_if vdec_vp9_if = {
vdec_vp9_init,
vdec_vp9_decode,
vdec_vp9_get_param,
vdec_vp9_deinit,
};
struct vdec_common_if *get_vp9_dec_comm_if(void);
struct vdec_common_if *get_vp9_dec_comm_if(void)
{
return &vdec_vp9_if;
}