linux/drivers/media/platform/qcom/venus/vdec.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
   4 * Copyright (C) 2017 Linaro Ltd.
   5 */
   6#include <linux/clk.h>
   7#include <linux/module.h>
   8#include <linux/mod_devicetable.h>
   9#include <linux/platform_device.h>
  10#include <linux/pm_runtime.h>
  11#include <linux/slab.h>
  12#include <media/v4l2-ioctl.h>
  13#include <media/v4l2-event.h>
  14#include <media/v4l2-ctrls.h>
  15#include <media/v4l2-mem2mem.h>
  16#include <media/videobuf2-dma-sg.h>
  17
  18#include "hfi_venus_io.h"
  19#include "hfi_parser.h"
  20#include "core.h"
  21#include "helpers.h"
  22#include "vdec.h"
  23#include "pm_helpers.h"
  24
  25/*
  26 * Three resons to keep MPLANE formats (despite that the number of planes
  27 * currently is one):
  28 * - the MPLANE formats allow only one plane to be used
  29 * - the downstream driver use MPLANE formats too
  30 * - future firmware versions could add support for >1 planes
  31 */
  32static const struct venus_format vdec_formats[] = {
  33        {
  34                .pixfmt = V4L2_PIX_FMT_NV12,
  35                .num_planes = 1,
  36                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  37        }, {
  38                .pixfmt = V4L2_PIX_FMT_MPEG4,
  39                .num_planes = 1,
  40                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  41                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  42        }, {
  43                .pixfmt = V4L2_PIX_FMT_MPEG2,
  44                .num_planes = 1,
  45                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  46                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  47        }, {
  48                .pixfmt = V4L2_PIX_FMT_H263,
  49                .num_planes = 1,
  50                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  51                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  52        }, {
  53                .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
  54                .num_planes = 1,
  55                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  56                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  57        }, {
  58                .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
  59                .num_planes = 1,
  60                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  61                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  62        }, {
  63                .pixfmt = V4L2_PIX_FMT_H264,
  64                .num_planes = 1,
  65                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  66                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  67        }, {
  68                .pixfmt = V4L2_PIX_FMT_VP8,
  69                .num_planes = 1,
  70                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  71                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  72        }, {
  73                .pixfmt = V4L2_PIX_FMT_VP9,
  74                .num_planes = 1,
  75                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  76                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  77        }, {
  78                .pixfmt = V4L2_PIX_FMT_XVID,
  79                .num_planes = 1,
  80                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  81                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  82        }, {
  83                .pixfmt = V4L2_PIX_FMT_HEVC,
  84                .num_planes = 1,
  85                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  86                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  87        },
  88};
  89
  90static const struct venus_format *
  91find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
  92{
  93        const struct venus_format *fmt = vdec_formats;
  94        unsigned int size = ARRAY_SIZE(vdec_formats);
  95        unsigned int i;
  96
  97        for (i = 0; i < size; i++) {
  98                if (fmt[i].pixfmt == pixfmt)
  99                        break;
 100        }
 101
 102        if (i == size || fmt[i].type != type)
 103                return NULL;
 104
 105        if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
 106            !venus_helper_check_codec(inst, fmt[i].pixfmt))
 107                return NULL;
 108
 109        return &fmt[i];
 110}
 111
 112static const struct venus_format *
 113find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
 114{
 115        const struct venus_format *fmt = vdec_formats;
 116        unsigned int size = ARRAY_SIZE(vdec_formats);
 117        unsigned int i, k = 0;
 118
 119        if (index > size)
 120                return NULL;
 121
 122        for (i = 0; i < size; i++) {
 123                bool valid;
 124
 125                if (fmt[i].type != type)
 126                        continue;
 127                valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
 128                        venus_helper_check_codec(inst, fmt[i].pixfmt);
 129                if (k == index && valid)
 130                        break;
 131                if (valid)
 132                        k++;
 133        }
 134
 135        if (i == size)
 136                return NULL;
 137
 138        return &fmt[i];
 139}
 140
 141static const struct venus_format *
 142vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
 143{
 144        struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
 145        struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
 146        const struct venus_format *fmt;
 147        u32 szimage;
 148
 149        memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
 150        memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
 151
 152        fmt = find_format(inst, pixmp->pixelformat, f->type);
 153        if (!fmt) {
 154                if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 155                        pixmp->pixelformat = V4L2_PIX_FMT_NV12;
 156                else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 157                        pixmp->pixelformat = V4L2_PIX_FMT_H264;
 158                else
 159                        return NULL;
 160                fmt = find_format(inst, pixmp->pixelformat, f->type);
 161        }
 162
 163        pixmp->width = clamp(pixmp->width, frame_width_min(inst),
 164                             frame_width_max(inst));
 165        pixmp->height = clamp(pixmp->height, frame_height_min(inst),
 166                              frame_height_max(inst));
 167
 168        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 169                pixmp->height = ALIGN(pixmp->height, 32);
 170
 171        if (pixmp->field == V4L2_FIELD_ANY)
 172                pixmp->field = V4L2_FIELD_NONE;
 173        pixmp->num_planes = fmt->num_planes;
 174        pixmp->flags = 0;
 175
 176        szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width,
 177                                           pixmp->height);
 178
 179        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 180                pfmt[0].sizeimage = szimage;
 181                pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
 182        } else {
 183                pfmt[0].sizeimage = clamp_t(u32, pfmt[0].sizeimage, 0, SZ_8M);
 184                pfmt[0].sizeimage = max(pfmt[0].sizeimage, szimage);
 185                pfmt[0].bytesperline = 0;
 186        }
 187
 188        return fmt;
 189}
 190
 191static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
 192{
 193        struct venus_inst *inst = to_inst(file);
 194
 195        vdec_try_fmt_common(inst, f);
 196
 197        return 0;
 198}
 199
 200static int vdec_check_src_change(struct venus_inst *inst)
 201{
 202        int ret;
 203
 204        if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE &&
 205            inst->codec_state == VENUS_DEC_STATE_INIT &&
 206            !inst->reconfig)
 207                return -EINVAL;
 208
 209        if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE)
 210                return 0;
 211
 212        /*
 213         * The code snippet below is a workaround for backward compatibility
 214         * with applications which doesn't support V4L2 events. It will be
 215         * dropped in future once those applications are fixed.
 216         */
 217
 218        if (inst->codec_state != VENUS_DEC_STATE_INIT)
 219                goto done;
 220
 221        ret = wait_event_timeout(inst->reconf_wait, inst->reconfig,
 222                                 msecs_to_jiffies(100));
 223        if (!ret)
 224                return -EINVAL;
 225
 226        if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) ||
 227            !inst->reconfig)
 228                dev_dbg(inst->core->dev, "%s: wrong state\n", __func__);
 229
 230done:
 231        return 0;
 232}
 233
 234static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
 235{
 236        struct venus_inst *inst = to_inst(file);
 237        const struct venus_format *fmt = NULL;
 238        struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
 239        int ret;
 240
 241        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 242                fmt = inst->fmt_cap;
 243        else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 244                fmt = inst->fmt_out;
 245
 246        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 247                ret = vdec_check_src_change(inst);
 248                if (ret)
 249                        return ret;
 250        }
 251
 252        pixmp->pixelformat = fmt->pixfmt;
 253
 254        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 255                pixmp->width = inst->width;
 256                pixmp->height = inst->height;
 257                pixmp->colorspace = inst->colorspace;
 258                pixmp->ycbcr_enc = inst->ycbcr_enc;
 259                pixmp->quantization = inst->quantization;
 260                pixmp->xfer_func = inst->xfer_func;
 261        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 262                pixmp->width = inst->out_width;
 263                pixmp->height = inst->out_height;
 264        }
 265
 266        vdec_try_fmt_common(inst, f);
 267
 268        return 0;
 269}
 270
 271static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
 272{
 273        struct venus_inst *inst = to_inst(file);
 274        struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
 275        struct v4l2_pix_format_mplane orig_pixmp;
 276        const struct venus_format *fmt;
 277        struct v4l2_format format;
 278        u32 pixfmt_out = 0, pixfmt_cap = 0;
 279        struct vb2_queue *q;
 280
 281        q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
 282        if (!q)
 283                return -EINVAL;
 284
 285        if (vb2_is_busy(q))
 286                return -EBUSY;
 287
 288        orig_pixmp = *pixmp;
 289
 290        fmt = vdec_try_fmt_common(inst, f);
 291
 292        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 293                pixfmt_out = pixmp->pixelformat;
 294                pixfmt_cap = inst->fmt_cap->pixfmt;
 295        } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 296                pixfmt_cap = pixmp->pixelformat;
 297                pixfmt_out = inst->fmt_out->pixfmt;
 298        }
 299
 300        memset(&format, 0, sizeof(format));
 301
 302        format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
 303        format.fmt.pix_mp.pixelformat = pixfmt_out;
 304        format.fmt.pix_mp.width = orig_pixmp.width;
 305        format.fmt.pix_mp.height = orig_pixmp.height;
 306        vdec_try_fmt_common(inst, &format);
 307
 308        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 309                inst->out_width = format.fmt.pix_mp.width;
 310                inst->out_height = format.fmt.pix_mp.height;
 311                inst->colorspace = pixmp->colorspace;
 312                inst->ycbcr_enc = pixmp->ycbcr_enc;
 313                inst->quantization = pixmp->quantization;
 314                inst->xfer_func = pixmp->xfer_func;
 315                inst->input_buf_size = pixmp->plane_fmt[0].sizeimage;
 316        }
 317
 318        memset(&format, 0, sizeof(format));
 319
 320        format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 321        format.fmt.pix_mp.pixelformat = pixfmt_cap;
 322        format.fmt.pix_mp.width = orig_pixmp.width;
 323        format.fmt.pix_mp.height = orig_pixmp.height;
 324        vdec_try_fmt_common(inst, &format);
 325
 326        inst->width = format.fmt.pix_mp.width;
 327        inst->height = format.fmt.pix_mp.height;
 328
 329        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 330                inst->fmt_out = fmt;
 331        else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 332                inst->fmt_cap = fmt;
 333
 334        return 0;
 335}
 336
 337static int
 338vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
 339{
 340        struct venus_inst *inst = to_inst(file);
 341
 342        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
 343            s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 344                return -EINVAL;
 345
 346        switch (s->target) {
 347        case V4L2_SEL_TGT_CROP_BOUNDS:
 348        case V4L2_SEL_TGT_CROP_DEFAULT:
 349        case V4L2_SEL_TGT_CROP:
 350                if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 351                        return -EINVAL;
 352                s->r.width = inst->out_width;
 353                s->r.height = inst->out_height;
 354                break;
 355        case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 356        case V4L2_SEL_TGT_COMPOSE_PADDED:
 357                if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 358                        return -EINVAL;
 359                s->r.width = inst->width;
 360                s->r.height = inst->height;
 361                break;
 362        case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 363        case V4L2_SEL_TGT_COMPOSE:
 364                if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 365                        return -EINVAL;
 366                s->r.width = inst->out_width;
 367                s->r.height = inst->out_height;
 368                break;
 369        default:
 370                return -EINVAL;
 371        }
 372
 373        s->r.top = 0;
 374        s->r.left = 0;
 375
 376        return 0;
 377}
 378
 379static int
 380vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
 381{
 382        strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
 383        strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
 384        strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
 385
 386        return 0;
 387}
 388
 389static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
 390{
 391        struct venus_inst *inst = to_inst(file);
 392        const struct venus_format *fmt;
 393
 394        memset(f->reserved, 0, sizeof(f->reserved));
 395
 396        fmt = find_format_by_index(inst, f->index, f->type);
 397        if (!fmt)
 398                return -EINVAL;
 399
 400        f->pixelformat = fmt->pixfmt;
 401        f->flags = fmt->flags;
 402
 403        return 0;
 404}
 405
 406static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
 407{
 408        struct venus_inst *inst = to_inst(file);
 409        struct v4l2_captureparm *cap = &a->parm.capture;
 410        struct v4l2_fract *timeperframe = &cap->timeperframe;
 411        u64 us_per_frame, fps;
 412
 413        if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 414            a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 415                return -EINVAL;
 416
 417        memset(cap->reserved, 0, sizeof(cap->reserved));
 418        if (!timeperframe->denominator)
 419                timeperframe->denominator = inst->timeperframe.denominator;
 420        if (!timeperframe->numerator)
 421                timeperframe->numerator = inst->timeperframe.numerator;
 422        cap->readbuffers = 0;
 423        cap->extendedmode = 0;
 424        cap->capability = V4L2_CAP_TIMEPERFRAME;
 425        us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
 426        do_div(us_per_frame, timeperframe->denominator);
 427
 428        if (!us_per_frame)
 429                return -EINVAL;
 430
 431        fps = (u64)USEC_PER_SEC;
 432        do_div(fps, us_per_frame);
 433
 434        inst->fps = fps;
 435        inst->timeperframe = *timeperframe;
 436
 437        return 0;
 438}
 439
 440static int vdec_enum_framesizes(struct file *file, void *fh,
 441                                struct v4l2_frmsizeenum *fsize)
 442{
 443        struct venus_inst *inst = to_inst(file);
 444        const struct venus_format *fmt;
 445
 446        fmt = find_format(inst, fsize->pixel_format,
 447                          V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 448        if (!fmt) {
 449                fmt = find_format(inst, fsize->pixel_format,
 450                                  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
 451                if (!fmt)
 452                        return -EINVAL;
 453        }
 454
 455        if (fsize->index)
 456                return -EINVAL;
 457
 458        fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
 459
 460        fsize->stepwise.min_width = frame_width_min(inst);
 461        fsize->stepwise.max_width = frame_width_max(inst);
 462        fsize->stepwise.step_width = frame_width_step(inst);
 463        fsize->stepwise.min_height = frame_height_min(inst);
 464        fsize->stepwise.max_height = frame_height_max(inst);
 465        fsize->stepwise.step_height = frame_height_step(inst);
 466
 467        return 0;
 468}
 469
 470static int vdec_subscribe_event(struct v4l2_fh *fh,
 471                                const struct v4l2_event_subscription *sub)
 472{
 473        struct venus_inst *inst = container_of(fh, struct venus_inst, fh);
 474        int ret;
 475
 476        switch (sub->type) {
 477        case V4L2_EVENT_EOS:
 478                return v4l2_event_subscribe(fh, sub, 2, NULL);
 479        case V4L2_EVENT_SOURCE_CHANGE:
 480                ret = v4l2_src_change_event_subscribe(fh, sub);
 481                if (ret)
 482                        return ret;
 483                inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE;
 484                return 0;
 485        case V4L2_EVENT_CTRL:
 486                return v4l2_ctrl_subscribe_event(fh, sub);
 487        default:
 488                return -EINVAL;
 489        }
 490}
 491
 492static int
 493vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
 494{
 495        struct venus_inst *inst = to_inst(file);
 496        struct hfi_frame_data fdata = {0};
 497        int ret;
 498
 499        ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
 500        if (ret)
 501                return ret;
 502
 503        mutex_lock(&inst->lock);
 504
 505        if (cmd->cmd == V4L2_DEC_CMD_STOP) {
 506                /*
 507                 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
 508                 * decoder input to signal EOS.
 509                 */
 510                if (!(inst->streamon_out && inst->streamon_cap))
 511                        goto unlock;
 512
 513                fdata.buffer_type = HFI_BUFFER_INPUT;
 514                fdata.flags |= HFI_BUFFERFLAG_EOS;
 515                fdata.device_addr = 0xdeadb000;
 516
 517                ret = hfi_session_process_buf(inst, &fdata);
 518
 519                if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING)
 520                        inst->codec_state = VENUS_DEC_STATE_DRAIN;
 521        }
 522
 523unlock:
 524        mutex_unlock(&inst->lock);
 525        return ret;
 526}
 527
 528static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
 529        .vidioc_querycap = vdec_querycap,
 530        .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
 531        .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
 532        .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
 533        .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
 534        .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
 535        .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
 536        .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
 537        .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
 538        .vidioc_g_selection = vdec_g_selection,
 539        .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
 540        .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
 541        .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
 542        .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
 543        .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
 544        .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
 545        .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
 546        .vidioc_streamon = v4l2_m2m_ioctl_streamon,
 547        .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
 548        .vidioc_s_parm = vdec_s_parm,
 549        .vidioc_enum_framesizes = vdec_enum_framesizes,
 550        .vidioc_subscribe_event = vdec_subscribe_event,
 551        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 552        .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
 553        .vidioc_decoder_cmd = vdec_decoder_cmd,
 554};
 555
 556static int vdec_pm_get(struct venus_inst *inst)
 557{
 558        struct venus_core *core = inst->core;
 559        struct device *dev = core->dev_dec;
 560        int ret;
 561
 562        mutex_lock(&core->pm_lock);
 563        ret = pm_runtime_get_sync(dev);
 564        mutex_unlock(&core->pm_lock);
 565
 566        return ret < 0 ? ret : 0;
 567}
 568
 569static int vdec_pm_put(struct venus_inst *inst, bool autosuspend)
 570{
 571        struct venus_core *core = inst->core;
 572        struct device *dev = core->dev_dec;
 573        int ret;
 574
 575        mutex_lock(&core->pm_lock);
 576
 577        if (autosuspend)
 578                ret = pm_runtime_put_autosuspend(dev);
 579        else
 580                ret = pm_runtime_put_sync(dev);
 581
 582        mutex_unlock(&core->pm_lock);
 583
 584        return ret < 0 ? ret : 0;
 585}
 586
 587static int vdec_pm_get_put(struct venus_inst *inst)
 588{
 589        struct venus_core *core = inst->core;
 590        struct device *dev = core->dev_dec;
 591        int ret = 0;
 592
 593        mutex_lock(&core->pm_lock);
 594
 595        if (pm_runtime_suspended(dev)) {
 596                ret = pm_runtime_get_sync(dev);
 597                if (ret < 0)
 598                        goto error;
 599
 600                ret = pm_runtime_put_autosuspend(dev);
 601        }
 602
 603error:
 604        mutex_unlock(&core->pm_lock);
 605
 606        return ret < 0 ? ret : 0;
 607}
 608
 609static void vdec_pm_touch(struct venus_inst *inst)
 610{
 611        pm_runtime_mark_last_busy(inst->core->dev_dec);
 612}
 613
 614static int vdec_set_properties(struct venus_inst *inst)
 615{
 616        struct vdec_controls *ctr = &inst->controls.dec;
 617        struct hfi_enable en = { .enable = 1 };
 618        u32 ptype;
 619        int ret;
 620
 621        if (ctr->post_loop_deb_mode) {
 622                ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
 623                ret = hfi_session_set_property(inst, ptype, &en);
 624                if (ret)
 625                        return ret;
 626        }
 627
 628        return 0;
 629}
 630
 631#define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
 632
 633static int vdec_output_conf(struct venus_inst *inst)
 634{
 635        struct venus_core *core = inst->core;
 636        struct hfi_enable en = { .enable = 1 };
 637        u32 width = inst->out_width;
 638        u32 height = inst->out_height;
 639        u32 out_fmt, out2_fmt;
 640        bool ubwc = false;
 641        u32 ptype;
 642        int ret;
 643
 644        ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
 645        if (ret)
 646                return ret;
 647
 648        if (core->res->hfi_version == HFI_VERSION_1XX) {
 649                ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
 650                ret = hfi_session_set_property(inst, ptype, &en);
 651                if (ret)
 652                        return ret;
 653        }
 654
 655        /* Force searching UBWC formats for bigger then HD resolutions */
 656        if (width > 1920 && height > ALIGN(1080, 32))
 657                ubwc = true;
 658
 659        /* For Venus v4 UBWC format is mandatory */
 660        if (IS_V4(core))
 661                ubwc = true;
 662
 663        ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
 664                                        &out2_fmt, ubwc);
 665        if (ret)
 666                return ret;
 667
 668        inst->output_buf_size =
 669                        venus_helper_get_framesz_raw(out_fmt, width, height);
 670        inst->output2_buf_size =
 671                        venus_helper_get_framesz_raw(out2_fmt, width, height);
 672
 673        if (is_ubwc_fmt(out_fmt)) {
 674                inst->opb_buftype = HFI_BUFFER_OUTPUT2;
 675                inst->opb_fmt = out2_fmt;
 676                inst->dpb_buftype = HFI_BUFFER_OUTPUT;
 677                inst->dpb_fmt = out_fmt;
 678        } else if (is_ubwc_fmt(out2_fmt)) {
 679                inst->opb_buftype = HFI_BUFFER_OUTPUT;
 680                inst->opb_fmt = out_fmt;
 681                inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
 682                inst->dpb_fmt = out2_fmt;
 683        } else {
 684                inst->opb_buftype = HFI_BUFFER_OUTPUT;
 685                inst->opb_fmt = out_fmt;
 686                inst->dpb_buftype = 0;
 687                inst->dpb_fmt = 0;
 688        }
 689
 690        ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
 691                                          inst->opb_buftype);
 692        if (ret)
 693                return ret;
 694
 695        if (inst->dpb_fmt) {
 696                ret = venus_helper_set_multistream(inst, false, true);
 697                if (ret)
 698                        return ret;
 699
 700                ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
 701                                                  inst->dpb_buftype);
 702                if (ret)
 703                        return ret;
 704
 705                ret = venus_helper_set_output_resolution(inst, width, height,
 706                                                         HFI_BUFFER_OUTPUT2);
 707                if (ret)
 708                        return ret;
 709        }
 710
 711        if (IS_V3(core) || IS_V4(core)) {
 712                if (inst->output2_buf_size) {
 713                        ret = venus_helper_set_bufsize(inst,
 714                                                       inst->output2_buf_size,
 715                                                       HFI_BUFFER_OUTPUT2);
 716                        if (ret)
 717                                return ret;
 718                }
 719
 720                if (inst->output_buf_size) {
 721                        ret = venus_helper_set_bufsize(inst,
 722                                                       inst->output_buf_size,
 723                                                       HFI_BUFFER_OUTPUT);
 724                        if (ret)
 725                                return ret;
 726                }
 727        }
 728
 729        ret = venus_helper_set_dyn_bufmode(inst);
 730        if (ret)
 731                return ret;
 732
 733        return 0;
 734}
 735
 736static int vdec_session_init(struct venus_inst *inst)
 737{
 738        int ret;
 739
 740        ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
 741        if (ret == -EINVAL)
 742                return 0;
 743        else if (ret)
 744                return ret;
 745
 746        ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
 747                                                frame_height_min(inst));
 748        if (ret)
 749                goto deinit;
 750
 751        ret = venus_helper_init_codec_freq_data(inst);
 752        if (ret)
 753                goto deinit;
 754
 755        return 0;
 756deinit:
 757        hfi_session_deinit(inst);
 758        return ret;
 759}
 760
 761static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
 762                            unsigned int *out_num)
 763{
 764        enum hfi_version ver = inst->core->res->hfi_version;
 765        struct hfi_buffer_requirements bufreq;
 766        int ret;
 767
 768        *in_num = *out_num = 0;
 769
 770        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
 771        if (ret)
 772                return ret;
 773
 774        *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
 775
 776        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
 777        if (ret)
 778                return ret;
 779
 780        *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
 781
 782        return 0;
 783}
 784
 785static int vdec_queue_setup(struct vb2_queue *q,
 786                            unsigned int *num_buffers, unsigned int *num_planes,
 787                            unsigned int sizes[], struct device *alloc_devs[])
 788{
 789        struct venus_inst *inst = vb2_get_drv_priv(q);
 790        unsigned int in_num, out_num;
 791        int ret = 0;
 792
 793        if (*num_planes) {
 794                unsigned int output_buf_size = venus_helper_get_opb_size(inst);
 795
 796                if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
 797                    *num_planes != inst->fmt_out->num_planes)
 798                        return -EINVAL;
 799
 800                if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 801                    *num_planes != inst->fmt_cap->num_planes)
 802                        return -EINVAL;
 803
 804                if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
 805                    sizes[0] < inst->input_buf_size)
 806                        return -EINVAL;
 807
 808                if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 809                    sizes[0] < output_buf_size)
 810                        return -EINVAL;
 811
 812                return 0;
 813        }
 814
 815        ret = vdec_pm_get(inst);
 816        if (ret)
 817                return ret;
 818
 819        ret = vdec_session_init(inst);
 820        if (ret)
 821                goto put_power;
 822
 823        ret = vdec_num_buffers(inst, &in_num, &out_num);
 824        if (ret)
 825                goto put_power;
 826
 827        ret = vdec_pm_put(inst, false);
 828        if (ret)
 829                return ret;
 830
 831        switch (q->type) {
 832        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 833                *num_planes = inst->fmt_out->num_planes;
 834                sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
 835                                                    inst->out_width,
 836                                                    inst->out_height);
 837                sizes[0] = max(sizes[0], inst->input_buf_size);
 838                inst->input_buf_size = sizes[0];
 839                *num_buffers = max(*num_buffers, in_num);
 840                inst->num_input_bufs = *num_buffers;
 841                inst->num_output_bufs = out_num;
 842                break;
 843        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 844                *num_planes = inst->fmt_cap->num_planes;
 845                sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
 846                                                    inst->width,
 847                                                    inst->height);
 848                inst->output_buf_size = sizes[0];
 849                *num_buffers = max(*num_buffers, out_num);
 850                inst->num_output_bufs = *num_buffers;
 851
 852                mutex_lock(&inst->lock);
 853                if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
 854                        inst->codec_state = VENUS_DEC_STATE_STOPPED;
 855                mutex_unlock(&inst->lock);
 856                break;
 857        default:
 858                ret = -EINVAL;
 859                break;
 860        }
 861
 862        return ret;
 863
 864put_power:
 865        vdec_pm_put(inst, false);
 866        return ret;
 867}
 868
 869static int vdec_verify_conf(struct venus_inst *inst)
 870{
 871        enum hfi_version ver = inst->core->res->hfi_version;
 872        struct hfi_buffer_requirements bufreq;
 873        int ret;
 874
 875        if (!inst->num_input_bufs || !inst->num_output_bufs)
 876                return -EINVAL;
 877
 878        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
 879        if (ret)
 880                return ret;
 881
 882        if (inst->num_output_bufs < bufreq.count_actual ||
 883            inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
 884                return -EINVAL;
 885
 886        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
 887        if (ret)
 888                return ret;
 889
 890        if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
 891                return -EINVAL;
 892
 893        return 0;
 894}
 895
 896static int vdec_start_capture(struct venus_inst *inst)
 897{
 898        int ret;
 899
 900        if (!inst->streamon_out)
 901                return 0;
 902
 903        if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
 904                if (inst->reconfig)
 905                        goto reconfigure;
 906
 907                venus_helper_queue_dpb_bufs(inst);
 908                venus_helper_process_initial_cap_bufs(inst);
 909                inst->streamon_cap = 1;
 910                return 0;
 911        }
 912
 913        if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
 914                return 0;
 915
 916reconfigure:
 917        ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true);
 918        if (ret)
 919                return ret;
 920
 921        ret = vdec_output_conf(inst);
 922        if (ret)
 923                return ret;
 924
 925        ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
 926                                        VB2_MAX_FRAME, VB2_MAX_FRAME);
 927        if (ret)
 928                return ret;
 929
 930        ret = venus_helper_intbufs_realloc(inst);
 931        if (ret)
 932                goto err;
 933
 934        ret = venus_helper_alloc_dpb_bufs(inst);
 935        if (ret)
 936                goto err;
 937
 938        ret = venus_helper_queue_dpb_bufs(inst);
 939        if (ret)
 940                goto free_dpb_bufs;
 941
 942        ret = venus_helper_process_initial_cap_bufs(inst);
 943        if (ret)
 944                goto free_dpb_bufs;
 945
 946        venus_pm_load_scale(inst);
 947
 948        ret = hfi_session_continue(inst);
 949        if (ret)
 950                goto free_dpb_bufs;
 951
 952        inst->codec_state = VENUS_DEC_STATE_DECODING;
 953
 954        inst->streamon_cap = 1;
 955        inst->sequence_cap = 0;
 956        inst->reconfig = false;
 957
 958        return 0;
 959
 960free_dpb_bufs:
 961        venus_helper_free_dpb_bufs(inst);
 962err:
 963        return ret;
 964}
 965
 966static int vdec_start_output(struct venus_inst *inst)
 967{
 968        int ret;
 969
 970        if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
 971                ret = venus_helper_process_initial_out_bufs(inst);
 972                inst->codec_state = VENUS_DEC_STATE_DECODING;
 973                goto done;
 974        }
 975
 976        if (inst->codec_state == VENUS_DEC_STATE_INIT ||
 977            inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
 978                ret = venus_helper_process_initial_out_bufs(inst);
 979                goto done;
 980        }
 981
 982        if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
 983                return -EINVAL;
 984
 985        venus_helper_init_instance(inst);
 986        inst->sequence_out = 0;
 987        inst->reconfig = false;
 988
 989        ret = vdec_set_properties(inst);
 990        if (ret)
 991                return ret;
 992
 993        ret = vdec_output_conf(inst);
 994        if (ret)
 995                return ret;
 996
 997        ret = vdec_verify_conf(inst);
 998        if (ret)
 999                return ret;
1000
1001        ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1002                                        VB2_MAX_FRAME, VB2_MAX_FRAME);
1003        if (ret)
1004                return ret;
1005
1006        ret = venus_helper_vb2_start_streaming(inst);
1007        if (ret)
1008                return ret;
1009
1010        ret = venus_helper_process_initial_out_bufs(inst);
1011        if (ret)
1012                return ret;
1013
1014        inst->codec_state = VENUS_DEC_STATE_INIT;
1015
1016done:
1017        inst->streamon_out = 1;
1018        return ret;
1019}
1020
1021static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1022{
1023        struct venus_inst *inst = vb2_get_drv_priv(q);
1024        int ret;
1025
1026        mutex_lock(&inst->lock);
1027
1028        if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1029                ret = vdec_start_capture(inst);
1030        } else {
1031                ret = vdec_pm_get(inst);
1032                if (ret)
1033                        goto error;
1034
1035                ret = venus_pm_acquire_core(inst);
1036                if (ret)
1037                        goto put_power;
1038
1039                ret = vdec_pm_put(inst, true);
1040                if (ret)
1041                        goto error;
1042
1043                ret = vdec_start_output(inst);
1044        }
1045
1046        if (ret)
1047                goto error;
1048
1049        mutex_unlock(&inst->lock);
1050        return 0;
1051
1052put_power:
1053        vdec_pm_put(inst, false);
1054error:
1055        venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1056        mutex_unlock(&inst->lock);
1057        return ret;
1058}
1059
1060static void vdec_cancel_dst_buffers(struct venus_inst *inst)
1061{
1062        struct vb2_v4l2_buffer *buf;
1063
1064        while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1065                v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1066}
1067
1068static int vdec_stop_capture(struct venus_inst *inst)
1069{
1070        int ret = 0;
1071
1072        switch (inst->codec_state) {
1073        case VENUS_DEC_STATE_DECODING:
1074                ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1075                /* fallthrough */
1076        case VENUS_DEC_STATE_DRAIN:
1077                vdec_cancel_dst_buffers(inst);
1078                inst->codec_state = VENUS_DEC_STATE_STOPPED;
1079                break;
1080        case VENUS_DEC_STATE_DRC:
1081                WARN_ON(1);
1082                fallthrough;
1083        case VENUS_DEC_STATE_DRC_FLUSH_DONE:
1084                inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1085                venus_helper_free_dpb_bufs(inst);
1086                break;
1087        default:
1088                break;
1089        }
1090
1091        INIT_LIST_HEAD(&inst->registeredbufs);
1092
1093        return ret;
1094}
1095
1096static int vdec_stop_output(struct venus_inst *inst)
1097{
1098        int ret = 0;
1099
1100        switch (inst->codec_state) {
1101        case VENUS_DEC_STATE_DECODING:
1102        case VENUS_DEC_STATE_DRAIN:
1103        case VENUS_DEC_STATE_STOPPED:
1104                ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1105                inst->codec_state = VENUS_DEC_STATE_SEEK;
1106                break;
1107        case VENUS_DEC_STATE_INIT:
1108        case VENUS_DEC_STATE_CAPTURE_SETUP:
1109                ret = hfi_session_flush(inst, HFI_FLUSH_INPUT, true);
1110                break;
1111        default:
1112                break;
1113        }
1114
1115        return ret;
1116}
1117
1118static void vdec_stop_streaming(struct vb2_queue *q)
1119{
1120        struct venus_inst *inst = vb2_get_drv_priv(q);
1121        int ret = -EINVAL;
1122
1123        mutex_lock(&inst->lock);
1124
1125        if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1126                ret = vdec_stop_capture(inst);
1127        else
1128                ret = vdec_stop_output(inst);
1129
1130        venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR);
1131
1132        if (ret)
1133                goto unlock;
1134
1135        if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1136                inst->streamon_out = 0;
1137        else
1138                inst->streamon_cap = 0;
1139
1140unlock:
1141        mutex_unlock(&inst->lock);
1142}
1143
1144static void vdec_session_release(struct venus_inst *inst)
1145{
1146        struct venus_core *core = inst->core;
1147        int ret, abort = 0;
1148
1149        vdec_pm_get(inst);
1150
1151        mutex_lock(&inst->lock);
1152        inst->codec_state = VENUS_DEC_STATE_DEINIT;
1153
1154        ret = hfi_session_stop(inst);
1155        abort = (ret && ret != -EINVAL) ? 1 : 0;
1156        ret = hfi_session_unload_res(inst);
1157        abort = (ret && ret != -EINVAL) ? 1 : 0;
1158        ret = venus_helper_unregister_bufs(inst);
1159        abort = (ret && ret != -EINVAL) ? 1 : 0;
1160        ret = venus_helper_intbufs_free(inst);
1161        abort = (ret && ret != -EINVAL) ? 1 : 0;
1162        ret = hfi_session_deinit(inst);
1163        abort = (ret && ret != -EINVAL) ? 1 : 0;
1164
1165        if (inst->session_error || core->sys_error)
1166                abort = 1;
1167
1168        if (abort)
1169                hfi_session_abort(inst);
1170
1171        venus_helper_free_dpb_bufs(inst);
1172        venus_pm_load_scale(inst);
1173        INIT_LIST_HEAD(&inst->registeredbufs);
1174        mutex_unlock(&inst->lock);
1175
1176        venus_pm_release_core(inst);
1177        vdec_pm_put(inst, false);
1178}
1179
1180static int vdec_buf_init(struct vb2_buffer *vb)
1181{
1182        struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1183
1184        inst->buf_count++;
1185
1186        return venus_helper_vb2_buf_init(vb);
1187}
1188
1189static void vdec_buf_cleanup(struct vb2_buffer *vb)
1190{
1191        struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1192
1193        inst->buf_count--;
1194        if (!inst->buf_count)
1195                vdec_session_release(inst);
1196}
1197
1198static void vdec_vb2_buf_queue(struct vb2_buffer *vb)
1199{
1200        struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1201
1202        vdec_pm_get_put(inst);
1203
1204        venus_helper_vb2_buf_queue(vb);
1205}
1206
1207static const struct vb2_ops vdec_vb2_ops = {
1208        .queue_setup = vdec_queue_setup,
1209        .buf_init = vdec_buf_init,
1210        .buf_cleanup = vdec_buf_cleanup,
1211        .buf_prepare = venus_helper_vb2_buf_prepare,
1212        .start_streaming = vdec_start_streaming,
1213        .stop_streaming = vdec_stop_streaming,
1214        .buf_queue = vdec_vb2_buf_queue,
1215};
1216
1217static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1218                          u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1219                          u32 hfi_flags, u64 timestamp_us)
1220{
1221        enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1222        struct vb2_v4l2_buffer *vbuf;
1223        struct vb2_buffer *vb;
1224        unsigned int type;
1225
1226        vdec_pm_touch(inst);
1227
1228        if (buf_type == HFI_BUFFER_INPUT)
1229                type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1230        else
1231                type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1232
1233        vbuf = venus_helper_find_buf(inst, type, tag);
1234        if (!vbuf)
1235                return;
1236
1237        vbuf->flags = flags;
1238        vbuf->field = V4L2_FIELD_NONE;
1239        vb = &vbuf->vb2_buf;
1240
1241        if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1242                vb2_set_plane_payload(vb, 0, bytesused);
1243                vb->planes[0].data_offset = data_offset;
1244                vb->timestamp = timestamp_us * NSEC_PER_USEC;
1245                vbuf->sequence = inst->sequence_cap++;
1246
1247                if (inst->last_buf == vb) {
1248                        inst->last_buf = NULL;
1249                        vbuf->flags |= V4L2_BUF_FLAG_LAST;
1250                        vb2_set_plane_payload(vb, 0, 0);
1251                        vb->timestamp = 0;
1252                }
1253
1254                if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1255                        const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1256
1257                        v4l2_event_queue_fh(&inst->fh, &ev);
1258
1259                        if (inst->codec_state == VENUS_DEC_STATE_DRAIN)
1260                                inst->codec_state = VENUS_DEC_STATE_STOPPED;
1261                }
1262
1263                if (!bytesused)
1264                        state = VB2_BUF_STATE_ERROR;
1265        } else {
1266                vbuf->sequence = inst->sequence_out++;
1267        }
1268
1269        venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1270
1271        if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1272                venus_helper_acquire_buf_ref(vbuf);
1273
1274        if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1275                state = VB2_BUF_STATE_ERROR;
1276
1277        if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1278                state = VB2_BUF_STATE_ERROR;
1279                vb2_set_plane_payload(vb, 0, 0);
1280                vb->timestamp = 0;
1281        }
1282
1283        v4l2_m2m_buf_done(vbuf, state);
1284}
1285
1286static void vdec_event_change(struct venus_inst *inst,
1287                              struct hfi_event_data *ev_data, bool sufficient)
1288{
1289        static const struct v4l2_event ev = {
1290                .type = V4L2_EVENT_SOURCE_CHANGE,
1291                .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1292        struct device *dev = inst->core->dev_dec;
1293        struct v4l2_format format = {};
1294
1295        mutex_lock(&inst->lock);
1296
1297        format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1298        format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1299        format.fmt.pix_mp.width = ev_data->width;
1300        format.fmt.pix_mp.height = ev_data->height;
1301
1302        vdec_try_fmt_common(inst, &format);
1303
1304        inst->width = format.fmt.pix_mp.width;
1305        inst->height = format.fmt.pix_mp.height;
1306
1307        inst->out_width = ev_data->width;
1308        inst->out_height = ev_data->height;
1309
1310        if (inst->bit_depth != ev_data->bit_depth)
1311                inst->bit_depth = ev_data->bit_depth;
1312
1313        dev_dbg(dev, "event %s sufficient resources (%ux%u)\n",
1314                sufficient ? "" : "not", ev_data->width, ev_data->height);
1315
1316        if (sufficient) {
1317                hfi_session_continue(inst);
1318        } else {
1319                switch (inst->codec_state) {
1320                case VENUS_DEC_STATE_INIT:
1321                        inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1322                        break;
1323                case VENUS_DEC_STATE_DECODING:
1324                        inst->codec_state = VENUS_DEC_STATE_DRC;
1325                        break;
1326                default:
1327                        break;
1328                }
1329        }
1330
1331        /*
1332         * The assumption is that the firmware have to return the last buffer
1333         * before this event is received in the v4l2 driver. Also the firmware
1334         * itself doesn't mark the last decoder output buffer with HFI EOS flag.
1335         */
1336
1337        if (!sufficient && inst->codec_state == VENUS_DEC_STATE_DRC) {
1338                struct vb2_v4l2_buffer *last;
1339                int ret;
1340
1341                last = v4l2_m2m_last_dst_buf(inst->m2m_ctx);
1342                if (last)
1343                        inst->last_buf = &last->vb2_buf;
1344
1345                ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false);
1346                if (ret)
1347                        dev_dbg(dev, "flush output error %d\n", ret);
1348        }
1349
1350        inst->reconfig = true;
1351        v4l2_event_queue_fh(&inst->fh, &ev);
1352        wake_up(&inst->reconf_wait);
1353
1354        mutex_unlock(&inst->lock);
1355}
1356
1357static void vdec_event_notify(struct venus_inst *inst, u32 event,
1358                              struct hfi_event_data *data)
1359{
1360        struct venus_core *core = inst->core;
1361        struct device *dev = core->dev_dec;
1362
1363        vdec_pm_touch(inst);
1364
1365        switch (event) {
1366        case EVT_SESSION_ERROR:
1367                inst->session_error = true;
1368                dev_err(dev, "dec: event session error %x\n", inst->error);
1369                break;
1370        case EVT_SYS_EVENT_CHANGE:
1371                switch (data->event_type) {
1372                case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1373                        vdec_event_change(inst, data, true);
1374                        break;
1375                case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1376                        vdec_event_change(inst, data, false);
1377                        break;
1378                case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1379                        venus_helper_release_buf_ref(inst, data->tag);
1380                        break;
1381                default:
1382                        break;
1383                }
1384                break;
1385        default:
1386                break;
1387        }
1388}
1389
1390static void vdec_flush_done(struct venus_inst *inst)
1391{
1392        if (inst->codec_state == VENUS_DEC_STATE_DRC)
1393                inst->codec_state = VENUS_DEC_STATE_DRC_FLUSH_DONE;
1394}
1395
1396static const struct hfi_inst_ops vdec_hfi_ops = {
1397        .buf_done = vdec_buf_done,
1398        .event_notify = vdec_event_notify,
1399        .flush_done = vdec_flush_done,
1400};
1401
1402static void vdec_inst_init(struct venus_inst *inst)
1403{
1404        inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1405        inst->fmt_out = &vdec_formats[6];
1406        inst->fmt_cap = &vdec_formats[0];
1407        inst->width = frame_width_min(inst);
1408        inst->height = ALIGN(frame_height_min(inst), 32);
1409        inst->out_width = frame_width_min(inst);
1410        inst->out_height = frame_height_min(inst);
1411        inst->fps = 30;
1412        inst->timeperframe.numerator = 1;
1413        inst->timeperframe.denominator = 30;
1414        inst->opb_buftype = HFI_BUFFER_OUTPUT;
1415}
1416
1417static void vdec_m2m_device_run(void *priv)
1418{
1419}
1420
1421static const struct v4l2_m2m_ops vdec_m2m_ops = {
1422        .device_run = vdec_m2m_device_run,
1423        .job_abort = venus_helper_m2m_job_abort,
1424};
1425
1426static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1427                          struct vb2_queue *dst_vq)
1428{
1429        struct venus_inst *inst = priv;
1430        int ret;
1431
1432        src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1433        src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1434        src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1435        src_vq->ops = &vdec_vb2_ops;
1436        src_vq->mem_ops = &vb2_dma_sg_memops;
1437        src_vq->drv_priv = inst;
1438        src_vq->buf_struct_size = sizeof(struct venus_buffer);
1439        src_vq->allow_zero_bytesused = 1;
1440        src_vq->min_buffers_needed = 0;
1441        src_vq->dev = inst->core->dev;
1442        ret = vb2_queue_init(src_vq);
1443        if (ret)
1444                return ret;
1445
1446        dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1447        dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1448        dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1449        dst_vq->ops = &vdec_vb2_ops;
1450        dst_vq->mem_ops = &vb2_dma_sg_memops;
1451        dst_vq->drv_priv = inst;
1452        dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1453        dst_vq->allow_zero_bytesused = 1;
1454        dst_vq->min_buffers_needed = 0;
1455        dst_vq->dev = inst->core->dev;
1456        ret = vb2_queue_init(dst_vq);
1457        if (ret) {
1458                vb2_queue_release(src_vq);
1459                return ret;
1460        }
1461
1462        return 0;
1463}
1464
1465static int vdec_open(struct file *file)
1466{
1467        struct venus_core *core = video_drvdata(file);
1468        struct venus_inst *inst;
1469        int ret;
1470
1471        inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1472        if (!inst)
1473                return -ENOMEM;
1474
1475        INIT_LIST_HEAD(&inst->dpbbufs);
1476        INIT_LIST_HEAD(&inst->registeredbufs);
1477        INIT_LIST_HEAD(&inst->internalbufs);
1478        INIT_LIST_HEAD(&inst->list);
1479        mutex_init(&inst->lock);
1480
1481        inst->core = core;
1482        inst->session_type = VIDC_SESSION_TYPE_DEC;
1483        inst->num_output_bufs = 1;
1484        inst->codec_state = VENUS_DEC_STATE_DEINIT;
1485        inst->buf_count = 0;
1486        inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1487        inst->core_acquired = false;
1488        inst->bit_depth = VIDC_BITDEPTH_8;
1489        init_waitqueue_head(&inst->reconf_wait);
1490        venus_helper_init_instance(inst);
1491
1492        ret = vdec_ctrl_init(inst);
1493        if (ret)
1494                goto err_free;
1495
1496        ret = hfi_session_create(inst, &vdec_hfi_ops);
1497        if (ret)
1498                goto err_ctrl_deinit;
1499
1500        vdec_inst_init(inst);
1501
1502        /*
1503         * create m2m device for every instance, the m2m context scheduling
1504         * is made by firmware side so we do not need to care about.
1505         */
1506        inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1507        if (IS_ERR(inst->m2m_dev)) {
1508                ret = PTR_ERR(inst->m2m_dev);
1509                goto err_session_destroy;
1510        }
1511
1512        inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1513        if (IS_ERR(inst->m2m_ctx)) {
1514                ret = PTR_ERR(inst->m2m_ctx);
1515                goto err_m2m_release;
1516        }
1517
1518        v4l2_fh_init(&inst->fh, core->vdev_dec);
1519
1520        inst->fh.ctrl_handler = &inst->ctrl_handler;
1521        v4l2_fh_add(&inst->fh);
1522        inst->fh.m2m_ctx = inst->m2m_ctx;
1523        file->private_data = &inst->fh;
1524
1525        return 0;
1526
1527err_m2m_release:
1528        v4l2_m2m_release(inst->m2m_dev);
1529err_session_destroy:
1530        hfi_session_destroy(inst);
1531err_ctrl_deinit:
1532        vdec_ctrl_deinit(inst);
1533err_free:
1534        kfree(inst);
1535        return ret;
1536}
1537
1538static int vdec_close(struct file *file)
1539{
1540        struct venus_inst *inst = to_inst(file);
1541
1542        vdec_pm_get(inst);
1543
1544        v4l2_m2m_ctx_release(inst->m2m_ctx);
1545        v4l2_m2m_release(inst->m2m_dev);
1546        vdec_ctrl_deinit(inst);
1547        hfi_session_destroy(inst);
1548        mutex_destroy(&inst->lock);
1549        v4l2_fh_del(&inst->fh);
1550        v4l2_fh_exit(&inst->fh);
1551
1552        vdec_pm_put(inst, false);
1553
1554        kfree(inst);
1555        return 0;
1556}
1557
1558static const struct v4l2_file_operations vdec_fops = {
1559        .owner = THIS_MODULE,
1560        .open = vdec_open,
1561        .release = vdec_close,
1562        .unlocked_ioctl = video_ioctl2,
1563        .poll = v4l2_m2m_fop_poll,
1564        .mmap = v4l2_m2m_fop_mmap,
1565};
1566
1567static int vdec_probe(struct platform_device *pdev)
1568{
1569        struct device *dev = &pdev->dev;
1570        struct video_device *vdev;
1571        struct venus_core *core;
1572        int ret;
1573
1574        if (!dev->parent)
1575                return -EPROBE_DEFER;
1576
1577        core = dev_get_drvdata(dev->parent);
1578        if (!core)
1579                return -EPROBE_DEFER;
1580
1581        platform_set_drvdata(pdev, core);
1582
1583        if (core->pm_ops->vdec_get) {
1584                ret = core->pm_ops->vdec_get(dev);
1585                if (ret)
1586                        return ret;
1587        }
1588
1589        vdev = video_device_alloc();
1590        if (!vdev)
1591                return -ENOMEM;
1592
1593        strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1594        vdev->release = video_device_release;
1595        vdev->fops = &vdec_fops;
1596        vdev->ioctl_ops = &vdec_ioctl_ops;
1597        vdev->vfl_dir = VFL_DIR_M2M;
1598        vdev->v4l2_dev = &core->v4l2_dev;
1599        vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1600
1601        ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1602        if (ret)
1603                goto err_vdev_release;
1604
1605        core->vdev_dec = vdev;
1606        core->dev_dec = dev;
1607
1608        video_set_drvdata(vdev, core);
1609        pm_runtime_set_autosuspend_delay(dev, 2000);
1610        pm_runtime_use_autosuspend(dev);
1611        pm_runtime_enable(dev);
1612
1613        return 0;
1614
1615err_vdev_release:
1616        video_device_release(vdev);
1617        return ret;
1618}
1619
1620static int vdec_remove(struct platform_device *pdev)
1621{
1622        struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1623
1624        video_unregister_device(core->vdev_dec);
1625        pm_runtime_disable(core->dev_dec);
1626
1627        if (core->pm_ops->vdec_put)
1628                core->pm_ops->vdec_put(core->dev_dec);
1629
1630        return 0;
1631}
1632
1633static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1634{
1635        struct venus_core *core = dev_get_drvdata(dev);
1636        const struct venus_pm_ops *pm_ops = core->pm_ops;
1637        int ret = 0;
1638
1639        if (pm_ops->vdec_power)
1640                ret = pm_ops->vdec_power(dev, POWER_OFF);
1641
1642        return ret;
1643}
1644
1645static __maybe_unused int vdec_runtime_resume(struct device *dev)
1646{
1647        struct venus_core *core = dev_get_drvdata(dev);
1648        const struct venus_pm_ops *pm_ops = core->pm_ops;
1649        int ret = 0;
1650
1651        if (pm_ops->vdec_power)
1652                ret = pm_ops->vdec_power(dev, POWER_ON);
1653
1654        return ret;
1655}
1656
1657static const struct dev_pm_ops vdec_pm_ops = {
1658        SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1659                                pm_runtime_force_resume)
1660        SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1661};
1662
1663static const struct of_device_id vdec_dt_match[] = {
1664        { .compatible = "venus-decoder" },
1665        { }
1666};
1667MODULE_DEVICE_TABLE(of, vdec_dt_match);
1668
1669static struct platform_driver qcom_venus_dec_driver = {
1670        .probe = vdec_probe,
1671        .remove = vdec_remove,
1672        .driver = {
1673                .name = "qcom-venus-decoder",
1674                .of_match_table = vdec_dt_match,
1675                .pm = &vdec_pm_ops,
1676        },
1677};
1678module_platform_driver(qcom_venus_dec_driver);
1679
1680MODULE_ALIAS("platform:qcom-venus-decoder");
1681MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1682MODULE_LICENSE("GPL v2");
1683