// SPDX-License-Identifier: GPL-2.0+
/*
* NVIDIA Tegra Video decoder driver
*
* Copyright (C) 2019-2022 Dmitry Osipenko <digetx@gmail.com>
*
* Based on Cedrus driver by Bootlin.
* Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
* Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*
* Based on Rockchip driver by Collabora.
* Copyright (C) 2019 Boris Brezillon <boris.brezillon@collabora.com>
*/
#include <linux/err.h>
#include <linux/slab.h>
#include "vde.h"
static const struct v4l2_ctrl_config ctrl_cfgs[] = {
{ .id = V4L2_CID_STATELESS_H264_DECODE_PARAMS, },
{ .id = V4L2_CID_STATELESS_H264_SPS, },
{ .id = V4L2_CID_STATELESS_H264_PPS, },
{
.id = V4L2_CID_STATELESS_H264_DECODE_MODE,
.min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
.max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
.def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
},
{
.id = V4L2_CID_STATELESS_H264_START_CODE,
.min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
.max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
.def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
},
{
.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
.min = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
.max = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
.def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
},
{
.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
.min = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
.max = V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
},
};
static inline struct tegra_ctx *fh_to_tegra_ctx(struct v4l2_fh *fh)
{
return container_of(fh, struct tegra_ctx, fh);
}
static void tegra_set_control_data(struct tegra_ctx *ctx, void *data, u32 id)
{
switch (id) {
case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
ctx->h264.decode_params = data;
break;
case V4L2_CID_STATELESS_H264_SPS:
ctx->h264.sps = data;
break;
case V4L2_CID_STATELESS_H264_PPS:
ctx->h264.pps = data;
break;
}
}
void tegra_vde_prepare_control_data(struct tegra_ctx *ctx, u32 id)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(ctrl_cfgs); i++) {
if (ctx->ctrls[i]->id == id) {
tegra_set_control_data(ctx, ctx->ctrls[i]->p_cur.p, id);
return;
}
}
tegra_set_control_data(ctx, NULL, id);
}
static int tegra_queue_setup(struct vb2_queue *vq,
unsigned int *nbufs,
unsigned int *num_planes,
unsigned int sizes[],
struct device *alloc_devs[])
{
struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
struct v4l2_format *f;
unsigned int i;
if (V4L2_TYPE_IS_OUTPUT(vq->type))
f = &ctx->coded_fmt;
else
f = &ctx->decoded_fmt;
if (*num_planes) {
if (*num_planes != f->fmt.pix_mp.num_planes)
return -EINVAL;
for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
if (sizes[i] < f->fmt.pix_mp.plane_fmt[i].sizeimage)
return -EINVAL;
}
} else {
*num_planes = f->fmt.pix_mp.num_planes;
for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
sizes[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
}
return 0;
}
static int tegra_buf_out_validate(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
vbuf->field = V4L2_FIELD_NONE;
return 0;
}
static void __tegra_buf_cleanup(struct vb2_buffer *vb, unsigned int i)
{
struct vb2_queue *vq = vb->vb2_queue;
struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
struct tegra_m2m_buffer *tb = vb_to_tegra_buf(vb);
while (i--) {
if (tb->a[i]) {
tegra_vde_dmabuf_cache_unmap(ctx->vde, tb->a[i], true);
tb->a[i] = NULL;
}
if (tb->iova[i]) {
tegra_vde_iommu_unmap(ctx->vde, tb->iova[i]);
tb->iova[i] = NULL;
}
}
if (tb->aux) {
tegra_vde_free_bo(tb->aux);
tb->aux = NULL;
}
}
static int tegra_buf_init(struct vb2_buffer *vb)
{
struct vb2_queue *vq = vb->vb2_queue;
struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
struct tegra_m2m_buffer *tb = vb_to_tegra_buf(vb);
struct tegra_vde *vde = ctx->vde;
enum dma_data_direction dma_dir;
struct sg_table *sgt;
unsigned int i;
int err;
if (V4L2_TYPE_IS_CAPTURE(vq->type) && vb->num_planes > 1) {
/*
* Tegra decoder writes auxiliary data for I/P frames.
* This data is needed for decoding of B frames.
*/
err = tegra_vde_alloc_bo(vde, &tb->aux, DMA_FROM_DEVICE,
vb2_plane_size(vb, 1));
if (err)
return err;
}
if (V4L2_TYPE_IS_OUTPUT(vq